Runtime evaluation: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
Line 32: Line 32:
Output:
Output:
<pre>[2011, 2016, 2022, 2033, 2039, 2044, 2050, 2061, 2067, 2072, 2078, 2089, 2095, 2101, 2107, 2112, 2118]</pre>
<pre>[2011, 2016, 2022, 2033, 2039, 2044, 2050, 2061, 2067, 2072, 2078, 2089, 2095, 2101, 2107, 2112, 2118]</pre>

=={{header|Smalltalk}}==
<lang smalltalk>
[ 4 + 5 ] value.</lang>

Revision as of 20:33, 4 May 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 how you get values in and out (e.g. environments, arguments, return values), 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.

Common Lisp

Using a list to represent code

<lang lisp>(eval '(+ 4 5)) ; Evaluate the program (+ 4 5)</lang>

Using a String to represent code

<lang lisp>(eval (read-from-string "(+ 4 5)")) ; Evaluate the program (+ 4 5)</lang>

Groovy

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

result = [] (2008..2121).each {

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

}

print result </lang>

Output:

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

Smalltalk

<lang smalltalk> [ 4 + 5 ] value.</lang>