Runtime evaluation: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 63: Line 63:
The last expression evaluated in the script, a list of years found, is the return value of the '''evaluate()''' method.
The last expression evaluated in the script, a list of years found, is the return value of the '''evaluate()''' method.


'''Evaluation with variables'''<br><br>
<br>'''Evaluation with variables'''<br><br>
There are several approaches to evaluating a script with variables:
There are several approaches to evaluating a script with variables:
*'''GString''' embedded values
*'''GString''' embedded values
Line 69: Line 69:
*'''Eval''' shortcut
*'''Eval''' shortcut


'''GString embedded values'''<br>
'''GString''' embedded values<br>
Setting up the script as a '''GString''' with embedded value parsing is a "natural" ''ad hoc'' solution for Groovy programmers, but there are possible pitfalls if the script itself contains '''GString'''s.
Setting up the script as a '''GString''' with embedded value parsing is a "natural" ''ad hoc'' solution for Groovy programmers, but there are possible pitfalls if the script itself contains '''GString'''s.
<lang groovy>def startYear = 2008
<lang groovy>def startYear = 2008
Line 84: Line 84:
Notice that in the script the embedded value "${it}" must be ''quoted'' with backslash (\) to prevent parsing as a part of the script '''GString'''. However, it is still correctly parsed within the internal '''GString''' when the script is run.
Notice that in the script the embedded value "${it}" must be ''quoted'' with backslash (\) to prevent parsing as a part of the script '''GString'''. However, it is still correctly parsed within the internal '''GString''' when the script is run.


'''Binding variables'''<br>
'''Binding''' variables<br>
'''GroovyShell''' uses a '''Binding''' object to pass variable values to a script. This is the only way to pass variables if the script comes from a '''File''' or '''InputStream''', but even if the script is a string '''Binding''' avoids the nested quoting issue caused by the ''ad hoc'' use of '''GString'''.
'''GroovyShell''' uses a '''Binding''' object to pass variable values to a script. This is the only way to pass variables if the script comes from a '''File''' or '''InputStream''', but even if the script is a string '''Binding''' avoids the nested quoting issue caused by the ''ad hoc'' use of '''GString'''.
<lang groovy>def context = new Binding()
<lang groovy>def context = new Binding()
Line 114: Line 114:
println binding.yearList</lang>
println binding.yearList</lang>


'''Eval shortcut'''<br>
'''Eval''' shortcut<br>
For simple evaluation of string-based scripts with only a few variables (like this one), the '''Eval''' class has static shortcut methods that do the '''Binding''' setup and '''GroovyShell''' evaluation under the surface. '''Eval.me(script)''' evaluates a script with no variables. '''Eval.x(x,script)''', '''Eval.xy(x,y,script)''', or '''Eval.xyz(x,y,z,script)''' each evaluates a script with 1, 2, or 3 variables, respectively. Here is an example with start and end years as script variables ''x'' and ''y''.
For simple evaluation of string-based scripts with only a few variables (like this one), the '''Eval''' class has static shortcut methods that do the '''Binding''' setup and '''GroovyShell''' evaluation under the surface. '''Eval.me(script)''' evaluates a script with no variables. '''Eval.x(x,script)''', '''Eval.xy(x,y,script)''', or '''Eval.xyz(x,y,z,script)''' each evaluates a script with 1, 2, or 3 variables, respectively. Here is an example with start and end years as script variables ''x'' and ''y''.
<lang groovy>def years5 = Eval.xy(2008, 2121, '''
<lang groovy>def years5 = Eval.xy(2008, 2121, '''

Revision as of 18:22, 3 June 2009

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

<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>

There are no sandboxing facilities in standard Common Lisp. Because CL has so many shared mutable objects (packages and symbols), no attempt to design a CL sandbox (other than at the OS process level) has yet succeeded.

There are no standardized debugging facilities specific to the eval operation itself, but code evaluted may be affected by the current global declarations, particularly the optimize declaration's debug and safety qualities.

Groovy

Each of these solutions evaluates a Groovy script based on some variation of the solution to the "Yuletide Holiday" task. Each variation has been verified to give the same output:

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

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). <lang groovy>def years1 = new GroovyShell().evaluate( (2008..2121).findAll {

   Date.parse("yyyy-MM-dd", "${it}-12-25").format("EEE") == "Sun"

} )

println years1</lang>

The last expression evaluated in the script, a list of years found, is the return value of the evaluate() method.


Evaluation with variables

There are several approaches to evaluating a script with variables:

  • GString embedded values
  • Binding variables
  • Eval shortcut

GString embedded values
Setting up the script as a GString with embedded value parsing is a "natural" ad hoc solution for Groovy programmers, but there are possible pitfalls if the script itself contains GStrings. <lang groovy>def startYear = 2008 def endYear = 2121 def years2 = new GroovyShell().evaluate(""" (${startYear}..${endYear}).findAll {

   Date.parse("yyyy-MM-dd", "\${it}-12-25").format("EEE") == "Sun"

} """)

println years2</lang> The variables "startYear" and "endYear" are dynamically pulled into the script GString as embedded values.

Notice that in the script the embedded value "${it}" must be quoted with backslash (\) to prevent parsing as a part of the script GString. However, it is still correctly parsed within the internal GString when the script is run.

Binding variables
GroovyShell uses a Binding object to pass variable values to a script. This is the only way to pass variables if the script comes from a File or InputStream, but even if the script is a string Binding avoids the nested quoting issue caused by the ad hoc use of GString. <lang groovy>def context = new Binding() context.startYear = 2008 context.endYear = 2121 def years3 = new GroovyShell(context).evaluate( (startYear..endYear).findAll {

   Date.parse("yyyy-MM-dd", "${it}-12-25").format("EEE") == "Sun"

} )</lang>

We may instantiate Binding with the variables as named parameters, allowing a more terse syntax: <lang groovy>def years4 = new GroovyShell( new Binding(startYear: 2008, endYear: 2121) ).evaluate( (startYear..endYear).findAll {

   Date.parse("yyyy-MM-dd", "${it}-12-25").format("EEE") == "Sun"

} )

println years4</lang>

We may also access the Binding object after script evaluation to extract values of any global variables set during the evaluation: <lang groovy>def binding = new Binding(startYear: 2008, endYear: 2121) new GroovyShell( binding ).evaluate( yearList = (startYear..endYear).findAll {

   Date.parse("yyyy-MM-dd", "${it}-12-25").format("EEE") == "Sun"

} )

println binding.yearList</lang>

Eval shortcut
For simple evaluation of string-based scripts with only a few variables (like this one), the Eval class has static shortcut methods that do the Binding setup and GroovyShell evaluation under the surface. Eval.me(script) evaluates a script with no variables. Eval.x(x,script), Eval.xy(x,y,script), or Eval.xyz(x,y,z,script) each evaluates a script with 1, 2, or 3 variables, respectively. Here is an example with start and end years as script variables x and y. <lang groovy>def years5 = Eval.xy(2008, 2121, (x..y).findAll {

   Date.parse("yyyy-MM-dd", "${it}-12-25").format("EEE") == "Sun"

} )

println years5</lang>

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> Tcl handles sandboxing by creating new interpreters. Each interpreter is strongly isolated from all other interpreters except in that the interpreter that creates a sub-interpreter retains management control over that “slave” interpreter. The exact capabilities exposed in the slave are controlled by what commands exist in it; commands in the slave may be aliases for other commands in the master interpreter, which allows for trapping into a more highly authorized context (which can be considered analogous to a system call to an OS kernel). <lang tcl># Create an interpreter with a default set of restrictions interp create -safe restrictedContext set v "secret" interp alias restrictedContext doubleSecret {} example proc example {} {

   global v
   lappend v $v
   return

} puts [restrictedContext eval {

   append v " has been leaked"
   catch {file delete yourCriticalFile.txt} ;# Will be denied!
   doubleSecret
   expr {4 + 5}

}]; # --> 9 puts $v; # --> secret secret</lang> As can be seen, the result of the overall evaluation is the same as the result of the evaluation in the slave.

Works with: Tcl version 8.5


There are resource limits available which allow preventing 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 interp eval $i {

   set x 0
   while {1} { # Infinite loop! Bwahahahaha!
       puts "Counting up... [incr x]"
   }

}</lang> This produces the output (the last line is an error message):

Counting up... 1
Counting up... 2
Counting up... 3
Counting up... 4
Counting up... 5
Counting up... 6
Counting up... 7
Counting up... 8
Counting up... 9
Counting up... 10
command count limit exceeded