Runtime evaluation

From Rosetta Code
Task
Runtime evaluation
You are encouraged to solve this task according to the task description, using any language you may know.

Demonstrate your language's ability for programs to execute other programs in the language provided at runtime. Show us what kind of program( fragment)s 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 specializing (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.

For a more restricted task, see Eval in environment.

ALGOL 68

This example is incorrect. Please fix the code and remove this message.

Details: It does not discuss passing in values or the environment the expression is evaluated in.

Works with: ALGOL 68G version Any - tested with release mk15-0.8b.fc9.i386 - this implementation is an interpretor, and evaluate is an extension to the standard

Variable names are generally not visible at run time with classic compilers. However ALGOL 68G is an interpretor and it retains this ability. <lang algol>print(evaluate("4.0*arctan(1.0)"))</lang> Output:

+3.14159265358979e  +0

Common Lisp

This example is incorrect. Please fix the code and remove this message.

Details: It does not discuss sandboxing, resource limiting, or debugging.

<lang lisp>(eval '(+ 4 5))</lang>

returns 9.

In Common Lisp, programs are represented as trees (s-expressions). Therefore, it is easily possible to construct a program which includes externally specified values, particularly using backquote template syntax:

<lang lisp>(defun add-four-complicated (a-number)

 (eval `(+ 4 ',a-number)))</lang>

Or you can construct a function and then call it. (If the function is used more than once, it would be good to use compile instead of eval, which compiles the code before returning the function. eval is permitted to compile as well, but compile requires it.)

<lang lisp>(defun add-four-by-function (a-number)

 (funcall (eval '(lambda (n) (+ 4 n)))) a-number)</lang>

If your program came from a file or user input, then you have it as a string, and read or read-from-string will convert it to s-expression form: <lang lisp>(eval (read-from-string "(+ 4 5)"))</lang>

Common Lisp has lexical scope, but eval always evaluates “in the null lexical environment”. In particular, it does not inherit the lexical environment from the enclosing code. (Note that eval is an ordinary function and as such does not have access to that environment anyway.)

<lang lisp>(let ((x 1))

 (eval `(+ x 1)))   ; this will fail unless x is a special variable or has a dynamic binding</lang>

Groovy

This example is incorrect. Please fix the code and remove this message.

Details: It does not discuss passing in or returning values, or the environment the expression is evaluated in.

This program evaluates the Groovy program solution to the "Yuletide Holiday" task: <lang Groovy>new GroovyShell().evaluate def inFormat = new java.text.SimpleDateFormat("yyyy-MM-dd") def checkFormat = new java.text.SimpleDateFormat("EEE")

def result = (2008..2121).findAll {

   Date date = inFormat.parse("${it}-12-25")
   checkFormat.format(date) == "Sun"

}

println result </lang>

Output:

[2011, 2016, 2022, 2033, 2039, 2044, 2050, 2061, 2067, 2072, 2078, 2089, 2095, 2101, 2107, 2112, 2118]

Perl

This example is incorrect. Please fix the code and remove this message.

Details: It does not discuss passing in or returning values, or the environment the expression is evaluated in.

<lang perl>eval '4 + 5'</lang>

Python

The exec statement allows the optional passing in of global and local names via mappings (See the link for full syntax). The example below shows exec being used to parse and execute a string containing two statements:

<lang python>>>> exec x = sum([1,2,3,4]) print x 10</lang>

Scheme

This example is incorrect. Please fix the code and remove this message.

Details: It does not discuss passing in or returning values, or the environment the expression is evaluated in.

This is very similar to the Common Lisp above. <lang scheme>> (eval '(+ 4 5)) 9 > (print (eval (read))) (+ 4 5) ;; this is input from the user. 9</lang>

Smalltalk

This example is incorrect. Please fix the code and remove this message.

Details: 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> [ 4 + 5 ] value.</lang>

Tcl

Evaluation in the current interpreter: <lang tcl>set four 4 set result1 [eval "expr {$four + 5}"]  ;# string input

set result2 [eval [list expr [list $four + 5]]] ;# list input</lang> Evaluation in a child interpreter, demonstrated in an interactive tclsh: <lang Tcl> % interp create helper helper % helper eval {set four 4} 4 % set r1 [helper eval {expr $four + 5}] 9</lang>