Runtime evaluation: Difference between revisions

→‎{{header|ALGOL 68}}: added an expanded example
m (→‎Groovy - Evaluation with variables: Typo fix in sample code)
(→‎{{header|ALGOL 68}}: added an expanded example)
Line 6:
 
=={{header|ALGOL 68}}==
 
{{incorrect|ALGOL 68|It does not discuss passing in values or the environment the expression is evaluated in.}}
 
<!-- {{does not work with|ALGOL 68|Standard - variable names are not visible at run time with classic compilers.}} -->
Line 19 ⟶ 17:
+3.14159265358979e +0
</pre>
 
This example demonstrates the use of variables and that the Algol 68G evaluate uses the normal Algol 68 scoping rules:
 
{{works with|ALGOL 68G|tested with release 2.8.win32}}
<lang algol68># procedure to call the Algol 68G evaluate procedure #
# the environment of the evaluation will be the caller's environment #
# with "code", "x" and "y" defined as the procedure parameters #
PROC ev = ( STRING code, INT x, INT y )STRING: evaluate( code );
 
BEGIN
 
INT i := 1;
INT j := 2;
REAL x := 4.2;
REAL y := 0.7164;
 
# evaluates "i + j" in the current environment #
print( ( evaluate( "i + j" ), newline ) );
 
# evaluates "x + y" in the environment of the procedure body of ev #
print( ( ev( "x + y", i, j ), newline ) );
 
# evaluates "x + y" in the current environment, so shows a different #
# result to the previous call #
print( ( evaluate( "x + y" ), newline ) );
 
# prints "code" because code is defined in the environment of the #
# call to evaluate (in ev) although it is not defined in this #
# environment #
print( ( ev( "code", 1, 2 ), newline ) );
 
# prints "code + codecode + code" - see above #
print( ( ev( "code + code", 1, 2 ), newline ) )
 
END
 
# if this next call was executed, a runtime error would occur as x and y #
# do not exist anymore #
# ;print( ( evaluate( "x + y" ), newline ) ) #</lang>
 
Output:
<pre>
+3
+3
+4.91640000000000e +0
code
code + codecode + code
</pre>
 
=={{header|AutoHotkey}}==
{{works with | AutoHotkey_H}}
3,045

edits