Runtime evaluation: Difference between revisions

m
{{out}}
m ({{out}})
Line 1:
{{task}}
{{task}} Demonstrate your language's ability for programs to execute code written in the language provided at runtime. Show us what kind of program fragments are permitted (e.g. expressions vs. statements), how you get values in and out (e.g. environments, arguments, return values), if applicable what lexical/static environment the program is evaluated in, and what facilities for restricting (e.g. sandboxes, resource limits) or customizing (e.g. debugging facilities) the execution.
Demonstrate your language's ability for programs to execute code written in the language provided at runtime.
{{task}} Demonstrate your language's ability for programs to execute code written in the language provided at runtime. Show us what kind of program fragments are permitted (e.g. expressions vs. statements), how you get values in and out (e.g. environments, arguments, return values), if applicable what lexical/static environment the program is evaluated in, and what facilities for restricting (e.g. sandboxes, resource limits) or customizing (e.g. debugging facilities) the execution.
 
You may not invoke a separate evaluator program, or invoke a compiler and then its output, unless the interface of that program, and the syntax and means of executing it, are considered part of your language/library/platform.
Line 13 ⟶ 15:
Variable names are generally not visible at run time with classic compilers. However '''ALGOL 68G''' is an interpretor and it retains this ability.
<lang algol68>print(evaluate("4.0*arctan(1.0)"))</lang>
{{out}}
Output:
<pre>
+3.14159265358979e +0
Line 57 ⟶ 59:
# ;print( ( evaluate( "x + y" ), newline ) ) #</lang>
 
{{out}}
Output:
<pre>
+3
Line 164 ⟶ 166:
<lang bbcbasic> expr$ = "PI^2 + 1"
PRINT EVAL(expr$)</lang>
{{out}}
Output:
<pre>
10.8696044
Line 188 ⟶ 190:
= CHR$(LENA$+4) + CHR$0 + CHR$0 + A$ + CHR$13
</lang>
{{out}}
Output:
<pre>
Hello world!
Line 404 ⟶ 406:
 
=={{header|Groovy}}==
Each of these solutions evaluates a Groovy script based on some variation of the solution to the "[[Yuletide_Holiday#Groovy|Yuletide Holiday]]" task. Each variation has been verified to give the same output:<br>
Each variation has been verified to give the same output:
<pre>[2011, 2016, 2022, 2033, 2039, 2044, 2050, 2061, 2067, 2072, 2078, 2089, 2095, 2101, 2107, 2112, 2118]</pre>
 
===Simple evaluation===
The '''GroovyShell''' class allows the evaluation of a string or of the text contents of a '''File''' or '''InputStream''' as a ''Groovy script''. A script is a either a set of statements to be executed in order, or a Groovy class with a '''main()''' method, or a Groovy '''Thread''' subclass or '''Runnable''' implementation. The return value is the value of the last statement executed, or the value of an explicit '''return''' statement (if any).
A script is a either a set of statements to be executed in order, or a Groovy class with a '''main()''' method, or a Groovy '''Thread''' subclass or '''Runnable''' implementation.
The return value is the value of the last statement executed, or the value of an explicit '''return''' statement (if any).
<lang groovy>def years1 = new GroovyShell().evaluate('''
(2008..2121).findAll {
Line 548 ⟶ 553:
Local variables (#) maintain scope behaviour as normal.
 
Output is governed by the "autocollect" boolean, the third parameter in the sourcefile invocation.
the third parameter in the sourcefile invocation.
 
<lang Lasso>//code, fragment name, autocollect, inplaintext
Line 654 ⟶ 660:
 
=={{header|MATLAB}}==
The eval and evalin functions handles any kind of code. It can handle multi-line code, although it needs the lines to be separated by the newline character. It can even allow you to program at runtime, as illustrated in the last example in the code and output below. Errors can occur when mixing eval statements with regular code, especially "compile-time" errors if the code appears to be missing key elements (ending brackets or end statements, etc). Some of these are also demonstrated.
Errors can occur when mixing eval statements with regular code, especially "compile-time" errors if the code appears to be missing key elements (ending brackets or end statements, etc). Some of these are also demonstrated.
<lang MATLAB>function testEval
fprintf('Expressions:\n')
Line 781 ⟶ 788:
 
=={{header|OxygenBasic}}==
Runtime (secondary) compiling is possible, with some restrictions. For instance static variables may not be created by the compiled code, but parental variables are visible to it. This demo produces tables of Y values, given a formula, and a range of X values to step through.
For instance, static variables may not be created by the compiled code, but parental variables are visible to it.
This demo produces tables of Y values, given a formula, and a range of X values to step through.
 
<lang oxygenbasic>
Line 1,038 ⟶ 1,047:
 
/*stick a fork in it, we're done.*/</lang>
'''output'''{{out}} with the input of: <tt> 2**44497 - 1 </tt>
<br>which happens to be the 27th Mersenne prime.
<pre style="height:35ex">
Line 1,150 ⟶ 1,159:
end</lang>
 
{{out}}
Output:
 
<pre> page 8</pre>
 
The built in function code() compiles complete SNOBOL4 source statements, or even complete programs. The compiled program is returned (as a value of type CODE), and when executed the program is executed in the then-current environment and has access to the then-current variables. Labels in the compiled program are added to the current program. Programs of type CODE are executed by a variant of the goto clause:
The compiled program is returned (as a value of type CODE), and when executed the program is executed in the then-current environment and has access to the then-current variables.
Labels in the compiled program are added to the current program.
Programs of type CODE are executed by a variant of the goto clause:
 
<lang SNOBOL4> compiled = code(' output = "Hello, world."') :s<compiled>
Line 1,161 ⟶ 1,172:
When passing programs to code(), semicolons are used to separate lines.
 
The calling (already-compiled) program can call, for example, functions that are defined in the code compiled at runtime, and can include gotos to labels only defined in the code compiled at runtime. Likewise, the code compiled at runtime has access to not just variables, but also files, functions, etc., that are in the already-compiled program.
Likewise, the code compiled at runtime has access to not just variables, but also files, functions, etc., that are in the already-compiled program.
 
=={{Header|Sparkling}}==
Line 1,221 ⟶ 1,233:
===Evaluation within limits===
{{works with|Tcl|8.5}}
Even stronger protection of the master interpreter is available from Tcl 8.5 onwards through the setting of resource limits on the slaves. These allow the master to prevent the evaluated script from going berserk:
These allow the master to prevent the evaluated script from going berserk:
<lang tcl>set i [interp create]
interp limit $i commands -value [expr [$i eval info cmdcount]+20] -granularity 1
Line 1,230 ⟶ 1,243:
}
}</lang>
This produces the output{{out}} (the last line is an error message):
<pre>Counting up... 1
Counting up... 2
Anonymous user