Runtime evaluation: Difference between revisions

add E example
(add Ruby)
(add E example)
Line 94:
 
There are no standardized debugging facilities specific to the <code>eval</code> operation itself, but code evaluted may be affected by the current [http://www.lispworks.com/documentation/HyperSpec/Body/03_c.htm global declarations], particularly the [http://www.lispworks.com/documentation/HyperSpec/Body/d_optimi.htm <code>optimize</code> declaration]'s <code>debug</code> and <code>safety</code> qualities.
 
=={{header|E}}==
 
In E, eval is a method of expression ASTs (EExprs). (Other types of program fragment ASTs such as methods and patterns may not be directly evaluated, and must be inserted into an expression.)
 
The lexical environment is provided as a parameter and cannot be omitted. The evaluated program has no access to anything but the provided environment.
 
<lang e>? e`1 + 1`.eval(safeScope)
# value: 2</lang>
 
<code>eval</code> returns the value of the expression. <code>evalToPair</code> also returns the modified environment for use with further evaluation, e.g. for implementing a [[REPL]].
 
<lang e>? def [value, env] := e`def x := 1 + 1`.evalToPair(safeScope)
# value: [2, ...]
 
? e`x`.eval(env)
# value: 2</lang>
 
Eval from a string may be done by invoking the parser.
 
<lang e>? def prog := <elang:syntax.makeEParser>.run("1 + 1")
# value: e`1.add(1)`
 
? prog.eval(safeScope)
# value: 2</lang>
 
=={{header|Groovy}}==