Runtime evaluation: Difference between revisions

m
Fixed lang tags.
m (→‎{{header|J}}: minor formatting and wording changes)
m (Fixed lang tags.)
Line 14:
<!-- {{does not work with|ELLA ALGOL 68|Any This implementation is a compiler}} -->
Variable names are generally not visible at run time with classic compilers. However '''ALGOL 68G''' is an interpretor and it retains this ability.
<lang algolalgol68>print(evaluate("4.0*arctan(1.0)"))</lang>
Output:
<pre>
Line 135:
 
Erlang eval is a bit complex/verbose and requires the interaction of 3 modules: <tt>erl_scan</tt> (tokenizes), <tt>erl_parse</tt> (returns an abstract form) and <tt>erl_eval</tt> (variable binding, evaluate abstract form, etc).
<lang erlang>1> {ok, Tokens, _} = erl_scan:string("X + 4 * lists:sum([1,2,3,4]).").
<lang erlang>
1> {ok, Tokens, _} = erl_scan:string("X + 4 * lists:sum([1,2,3,4]).").
...
2> {ok, [Form]} = erl_parse:parse_exprs(Tokens).
Line 143 ⟶ 142:
[{'X',17}]
4> {value, Value, _} = erl_eval:expr(Form, Bindings).
{value,57,[{'X',17}]}</lang>
</lang>
 
 
Line 228 ⟶ 226:
Use monadic [http://www.jsoftware.com/help/dictionary/d601.htm <code>".</code>] (''Do'') to execute a string.
 
<lang j>". 'a =: +/ 1 2 3' NB. execute a string to sum 1, 2 and 3 and assign to noun a</lang>
<lang j>
". 'a =: +/ 1 2 3' NB. execute a string to sum 1, 2 and 3 and assign to noun a
</lang>
 
Only expressions are allowed as arguments for <code>".</code>
Line 287 ⟶ 283:
In Scheme, the expression passed to eval is evaluated in the current interaction environment, unless otherwise specified. The result is read back as a Scheme value.
 
<lang scheme>> (define x 37)
> (define x 37)
> (eval '(+ x 5))
42
Line 299 ⟶ 294:
> (display (eval (read)))
(+ 4 5) ;; this is input from the user.
9</lang>
9
</lang>
 
=={{header|Slate}}==
 
In Slate, programs are represented as Syntax Node trees, with methods defined on the various syntactic types. The backtick syntax provides a convenient quoting mechanism, and as objects, they have convenient methods defined for evaluation or evaluation within a specific environment:
<lang slate>`(4 + 5) evaluate.
`(4 + 5) evaluateevaluateIn: prototypes.</lang>
`(4 + 5) evaluateIn: prototypes.
</lang>
 
You can also explicitly invoke the Parser on a String, to convert it into syntactic objects:
<lang slate>(Syntax Parser newOn: '4 + 5') upToEnd do: [| :each | print: each evaluate]</lang>
<lang slate>
(Syntax Parser newOn: '4 + 5') upToEnd do: [| :each | print: each evaluate]
</lang>
 
You can construct a program using externally-specified values using <tt>`unquote</tt> within a quoted expression:
<lang slate>define: #x -> 4.
`(x `unquote + 5) evaluate.</lang>
define: #x -> 4.
`(x `unquote + 5) evaluate.
</lang>
 
Or you can obviously construct a string:
<lang slate>define: #x -> 4.
(Syntax Parser newOn: x printString ; ' + 5') </lang>
define: #x -> 4.
(Syntax Parser newOn: x printString ; ' + 5')
</lang>
 
The <tt>evaluate</tt> method also takes into consideration the current lexical scope, unless another environment is specified. The following returns 10, no matter what binding <tt>x</tt> has in the local namespace:
<lang slate>define: #x -> 4.
[| x | x: 5. `(x `unquote + 5) evaluate] do.</lang>
define: #x -> 4.
[| x | x: 5. `(x `unquote + 5) evaluate] do.
</lang>
 
Slate can sandbox via constructing a fresh namespace and evaluating within it, but this mechanism is not strongly secure yet.
Line 337 ⟶ 321:
=={{header|Smalltalk}}==
{{incorrect|Smalltalk|It does not discuss passing in or returning values, or the environment the expression is evaluated in., and it does not show taking in an arbitrary program as from user input.}}
<lang smalltalk>[ 4 + 5 ] value.</lang>
[ 4 + 5 ] value.</lang>
 
=={{header|Tcl}}==
Anonymous user