Runtime evaluation: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(10 intermediate revisions by 7 users not shown)
Line 434:
 
=={{header|Elena}}==
ELENA 56.0x:
Using ELENA Script engine:
<syntaxhighlight lang="elena">import extensions'scripting;
Line 440:
public program()
{
lscript.interpretinterpretLine("system'console.writeLine(""Hello World"")");
}</syntaxhighlight>
{{out}}
Line 513:
restore
unused . \ same as first unused; restore, foo, and my-def no longer defined</syntaxhighlight>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vb">#macro assign(sym, expr)
__fb_unquote__(__fb_eval__("#undef " + sym))
__fb_unquote__(__fb_eval__("#define " + sym + " " + __fb_quote__(__fb_eval__(expr))))
#endmacro
 
#define a, b, x
 
assign("a", 8)
assign("b", 7)
assign("x", Sqr(a) + (Sin(b*3)/2))
Print x
 
assign("x", "goodbye")
Print x
 
Sleep</syntaxhighlight>
{{out}}
<pre> 3.246754944014219
goodby</pre>
 
=={{header|Frink}}==
Line 669 ⟶ 690:
READ(FIle="my_file.txt", Row=6) string
XEQ( string ) ! executes row 6 of my_file.txt</syntaxhighlight>
 
=={{Header|Insitux}}==
 
All valid Insitux code can be evaluated at runtime using <code>eval</code>, including function definitions which remain in global scope, with access to global scope but not local.
 
<syntaxhighlight lang="insitux">
(var x 123)
[
(eval "(var y 100) (+ x y)")
y
]
</syntaxhighlight>
 
{{out}}
 
<pre>
[223 100]
</pre>
 
Error messages differ between normal code and evaluated code.
 
<b>Normal invocation error output:</b>
 
<pre>
1:6 (let time 1)
Parse Error: "time" cannot be redefined: already exists.
</pre>
 
<b><code>eval</code> invocation error output:</b>
 
<pre>
1:2 (eval "(let time 1)")
Eval Error: error within evaluated code.
Parse Error: 1695133413649 eval line 1 col 6: "time" cannot be redefined: already exists
</pre>
 
=={{header|J}}==
Line 784 ⟶ 840:
at Evaluator.eval(Evaluator.java:33)
at Evaluator.main(Evaluator.java:21)</pre>
 
===Java REPL===
Java has a REPL called jshell which can be used for runtime evaluation. It is started by entering the jshell command from the command line. Here is a typical session:
<pre>
C:\Program Files\JDK\bin> .\jshell
| Welcome to JShell -- Version 20
| For an introduction type: /help intro
 
jshell> double value = 12.0
value ==> 12.0
 
jshell> value * 3
$2 ==> 36.0
 
jshell> List<Integer> items = List.of( 1, 2, 3 )
items ==> [1, 2, 3]
 
jshell> for ( int item : items ) { System.out.print(item * item + " "); }
1 4 9
 
jshell> void helloWorld() { System.out.println("Hello World"); }
| created method helloWorld()
 
jshell> helloWorld()
Hello World
 
jshell> /exit
| Goodbye
</pre>
 
=={{header|JavaScript}}==
Line 858 ⟶ 943:
48
>>> :quit
</pre>
 
=={{header|Lang}}==
<syntaxhighlight lang="lang">
# Simple assignements are used so that rvalues are parsed as TEXT values
$code=fn.println(Hello World!)
# Returns VOID unless return or throw is explicitly used
fn.exec($code)
 
$code=return Hello World!
fn.println(fn.exec($code))
 
$code=throw $LANG_ERROR_DIV_BY_ZERO
# Will print "Dividing by 0" in the Standard Lang implementation (Error texts are not standardized)
fn.println(fn.errorText(fn.exec($code)))
 
$code=parser.op(20//0)
# Will return VOID because no error was thrown explicitly
fn.println(fn.exec($code))
</syntaxhighlight>
This is the output for the Standard Lang implementation.
{{out}}
<pre>
Hello World!
Hello World!
An error occured [error output: redacted]
Dividing by 0
An error occured [error output: redacted]
 
</pre>
 
Line 1,767 ⟶ 1,881:
 
Slate can sandbox via constructing a fresh namespace and evaluating within it, but this mechanism is not strongly secure yet.
 
=={{header|Slope}}==
You can create a list via quoted symbols and then evaluate:
<syntaxhighlight lang="slope">(eval (list '+ 1 2 3 4 5))</syntaxhighlight>
 
Or, you can evaluate a string as code:
<syntaxhighlight lang="slope">(eval "(+ 1 2 3 4 5)" #t)</syntaxhighlight>
 
=={{header|Smalltalk}}==
Line 1,932 ⟶ 2,053:
$ wren-cli
\\/"-
\_/ wren v0.34.0
> 20 + 22
42
Line 1,944 ⟶ 2,065:
 
Secondly, Wren has the ''Meta.eval'' method which can be used from within a script to execute any valid Wren code (presented to it in string form) at runtime. The string could be constructed within the script, obtained from a file or input by the user. Here's a very simple example:
<syntaxhighlight lang="ecmascriptwren">import "meta" for Meta
 
var s = "for (i in 0..4) System.print(i)"
9,476

edits