Runtime evaluation: Difference between revisions

no edit summary
(Added MATLAB example)
No edit summary
Line 1,061:
 
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.
 
=={{Header|Sparkling}}==
 
In Sparkling, the standard library provides functions to compile expressions and statements into functions. Each such function is considered a different top-level program, running in the execution context of it's "parent" program (i. e. the code from within which it was created).
 
Compiled expressions and statements can take arbitrary arguments and return values to the caller. As with any function, the expression or statement being compiled can refer to its arguments using the <tt>#</tt> prefix operator.
 
An expression always "returns" a value (i. e. evaluates to one) to the caller. Basically, compiling an expression is semantically (and syntactically) equivalent with creating a function with no declared arguments of which the body contains of a single <tt>return</tt> statement, returning the expression.
 
===Evaluating expressions===
====Simple====
<lang sparkling>let fn = exprtofn("13 + 37");
fn() // -> 50</lang>
 
====With arguments====
<lang sparkling>let fn = exprtofn("#0 * #1");
fn(3, 4) // -> 12</lang>
 
===Evaluating statements===
<lang sparkling>let fn = compile("for (var i = 0; i < 10; i++) { print(i); }");
fn(); // result: 0 1 2 3 4 5 6 7 8 9</lang>
 
=={{header|Tcl}}==
Anonymous user