pocketlang
pocketlang copied to clipboard
[Enhancement] add compile and eval for metaprogramming
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