#run directive

The #run directive is used to perform compile time execution and metaprogramming. If you want to run a function at compile time, type in #run function(); to run a function named function at compile time. Compile time execution runs the code in an interpreted bytecode mode. Any snippet of code can be run at compile-time, from a video playing sound, to a metaprogram that grabs compile information from a build server then compiles to code, to just about anything limited by your imagination.

function :: () {
  print("This is function :: ()\n");
}

#run function(); // executes function at compile time.

Any arbitrary set of code computed at compile-time through the #run directive. In this example, we compute PI through running compute_pi at compile-time execution.

PI :: #run compute_pi();
compute_pi :: () -> float {
  // calculate pi using the leibniz formula.
  n := 1.0;
  s := 1.0;
  pi := 0.0;

  for 0..10000 {
    pi +=  1.0 / (s*n);
    n += 2.0;
    s = -s;
  }
  return pi*4.0;
}

Any arbitrary set of #run directives may be executed, even values that depend on one another. Circular dependencies (e.g. where a depends on b and b depends on a) will result in a compiler error.

a := #run f1();
b := #run f2(a);

f1 :: () => 1000;
f2 :: (a)=> a + 1;
print("a=%, b=%\n", a, b); // this prints out a=1000, b=1001
1 Like