pocketlang icon indicating copy to clipboard operation
pocketlang copied to clipboard

[Enhancement] add compile and eval for metaprogramming

Open khchen opened this issue 1 year ago • 0 comments

compile can compile the source code into a closure at runtime time. Example:

message = "Metaprogramming"
value = 12345

code = "
  def test
    return $value
  end
  print('$message')
  return test()
"
func = compile(code)
print(func())

Output:

Metaprogramming
12345

eval can evaluate an expression and returns the result. Example:

message = eval("'Meta' + 'programming'")
value = eval("123450 / 10")
func = eval("
  fn
    print(message)
    return eval('value')
  end
")
eval("print")(eval("func()"))

Output:

Metaprogramming
12345

khchen avatar Jul 30 '22 18:07 khchen