Runtime evaluation: Difference between revisions
Content added Content deleted
m (→{{header|J}}: minor formatting and wording changes) |
m (Fixed lang tags.) |
||
Line 14: | Line 14: | ||
<!-- {{does not work with|ELLA ALGOL 68|Any This implementation is a compiler}} --> |
<!-- {{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. |
Variable names are generally not visible at run time with classic compilers. However '''ALGOL 68G''' is an interpretor and it retains this ability. |
||
<lang |
<lang algol68>print(evaluate("4.0*arctan(1.0)"))</lang> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 135: | 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). |
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> |
|||
⚫ | |||
... |
... |
||
2> {ok, [Form]} = erl_parse:parse_exprs(Tokens). |
2> {ok, [Form]} = erl_parse:parse_exprs(Tokens). |
||
Line 143: | Line 142: | ||
[{'X',17}] |
[{'X',17}] |
||
4> {value, Value, _} = erl_eval:expr(Form, Bindings). |
4> {value, Value, _} = erl_eval:expr(Form, Bindings). |
||
{value,57,[{'X',17}]} |
{value,57,[{'X',17}]}</lang> |
||
⚫ | |||
Line 228: | Line 226: | ||
Use monadic [http://www.jsoftware.com/help/dictionary/d601.htm <code>".</code>] (''Do'') to execute a string. |
Use monadic [http://www.jsoftware.com/help/dictionary/d601.htm <code>".</code>] (''Do'') to execute a string. |
||
⚫ | |||
<lang j> |
|||
⚫ | |||
</lang> |
|||
Only expressions are allowed as arguments for <code>".</code> |
Only expressions are allowed as arguments for <code>".</code> |
||
Line 287: | Line 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. |
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> |
<lang scheme>> (define x 37) |
||
> (define x 37) |
|||
> (eval '(+ x 5)) |
> (eval '(+ x 5)) |
||
42 |
42 |
||
Line 299: | Line 294: | ||
> (display (eval (read))) |
> (display (eval (read))) |
||
(+ 4 5) ;; this is input from the user. |
(+ 4 5) ;; this is input from the user. |
||
⚫ | |||
9 |
|||
</lang> |
|||
=={{header|Slate}}== |
=={{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: |
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> |
<lang slate>`(4 + 5) evaluate. |
||
`(4 + 5) |
`(4 + 5) evaluateIn: prototypes.</lang> |
||
`(4 + 5) evaluateIn: prototypes. |
|||
</lang> |
|||
You can also explicitly invoke the Parser on a String, to convert it into syntactic objects: |
You can also explicitly invoke the Parser on a String, to convert it into syntactic objects: |
||
⚫ | |||
<lang slate> |
|||
⚫ | |||
</lang> |
|||
You can construct a program using externally-specified values using <tt>`unquote</tt> within a quoted expression: |
You can construct a program using externally-specified values using <tt>`unquote</tt> within a quoted expression: |
||
<lang slate> |
<lang slate>define: #x -> 4. |
||
⚫ | |||
define: #x -> 4. |
|||
⚫ | |||
</lang> |
|||
Or you can obviously construct a string: |
Or you can obviously construct a string: |
||
<lang slate> |
<lang slate>define: #x -> 4. |
||
⚫ | |||
define: #x -> 4. |
|||
⚫ | |||
</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: |
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> |
<lang slate>define: #x -> 4. |
||
⚫ | |||
define: #x -> 4. |
|||
⚫ | |||
</lang> |
|||
Slate can sandbox via constructing a fresh namespace and evaluating within it, but this mechanism is not strongly secure yet. |
Slate can sandbox via constructing a fresh namespace and evaluating within it, but this mechanism is not strongly secure yet. |
||
Line 337: | Line 321: | ||
=={{header|Smalltalk}}== |
=={{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.}} |
{{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> |
<lang smalltalk>[ 4 + 5 ] value.</lang> |
||
[ 4 + 5 ] value.</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |