Some concept sample code in the Merg-E language.

in #web32 days ago (edited)

Just a quick share to showcase (roughly) what Merg-E code looks like in the latest draft.

There have been some changes since my last blog post that I'm going to document later. There have been some major changes that I need to properly document, but I thought sharing
a code example of a program with module that should work when I finish the first (slow) interpreter.

It isn't my primary project, so I felt I should share my progress on language definition.

Here is an example myApp.mrg:

app myApp lang ambient merge {

  lang.use lang {
    use as use;
    data_state.mutable as mutable;
    share.shared as shared;
    type.uint64_t as int; 
    await.all as await_all;
    type.blocker as blocker;
    flow.while as while;
    flow.switch as switch;
    type.exception.range_error as range_error;
    };

  use ambient {
    io.cout as cout;
    io.cerr as cerr;
    io.endl as endl;
    };

  mutable main ()::{}{
      mutable int counter = 0;
      shared mutable int ok_count = 0;
      mutable blocker all_primes;
      mutable merge utils.is_prime as is_prime(x int)::{ok_count: ok_count}{};
      while counter < 10 {
        counter += 1;
        all_primes += is_prime(counter);
        }
      await_all all_primes;
      cout "counted " ok_count " prime numbers" endl;
      }!!{
          switch state.exception {
            range_error : {
               cerr "Something went wrong" endl; 
            }
          };
        };
  };

With the matching utils.mrm module that is merged in:

ns utils lang def {
  lang.use lang {
      data_state.mutable as mutable;
      type.function_def as function_def;
      type.uint64_t as int;
      type.bool as bool;
      flow.if as if;
      flow.raise as raise;
      flow.while as while;
      type.blocker as blocker;
      await.all as await_all;
      type.exception.range_error as range_error;
      lock as lock;
  };

    
  mutable def is_prime = (x int)::{ok_count: int}{} {
    if x < 1 {
      raise range_error;
      }!!{};
    mutable int candidate=2;
    mutable bool isprime = True;
    if x ==1 {
      isprime = False;
      }; 
    while candidate * candidate <= x {
      if x % candidate == 0 {
        isprime = False;
        };
      candidate += 1;
      };  
    if isprime {
      mutable blocker lck;
      lck += lock(ok_count) {
        ok_count +=1;
        };
      };
      await lck;
    };
  };

This example is pretty useless. It counts the number of prime numbers from 1 till 10 and prints the result to stdout. It doesn't touch on many major parts of the language that are important, but it is the first sample that I'll be able to use to test an interpreter with.