Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
2
Task/Runtime-evaluation/00-META.yaml
Normal file
2
Task/Runtime-evaluation/00-META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Runtime_evaluation
|
||||
9
Task/Runtime-evaluation/00-TASK.txt
Normal file
9
Task/Runtime-evaluation/00-TASK.txt
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
;Task:
|
||||
Demonstrate a language's ability for programs to execute code written in the language provided at runtime.
|
||||
|
||||
Show what kind of program fragments are permitted (e.g. expressions vs. statements), and how to 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 customizing (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 constrained task giving a specific program fragment to evaluate, see [[Eval in environment]].
|
||||
<br><br>
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
;Init Routine
|
||||
*=$0801
|
||||
db $0E,$08,$0A,$00,$9E,$20,$28,$32,$30,$36,$34,$29,$00,$00,$00
|
||||
*=$0810 ;Start at $0810
|
||||
|
||||
|
||||
LDA #$A9 ;opcode for LDA immediate
|
||||
STA smc_test
|
||||
|
||||
LDA #'A'
|
||||
STA smc_test+1
|
||||
|
||||
lda #$20 ;opcode for JSR
|
||||
STA smc_test+2
|
||||
|
||||
lda #<CHROUT
|
||||
STA smc_test+3
|
||||
|
||||
lda #>CHROUT
|
||||
STA smc_test+4
|
||||
|
||||
|
||||
smc_test:
|
||||
nop ;gets overwritten with LDA
|
||||
nop ;gets overwritten with #$41
|
||||
nop ;gets overwritten with JSR
|
||||
nop ;gets overwritten with <CHROUT
|
||||
nop ;gets overwritten with >CHROUT
|
||||
|
||||
|
||||
rts ;return to basic
|
||||
|
|
@ -0,0 +1 @@
|
|||
print(evaluate("4.0*arctan(1.0)"))
|
||||
35
Task/Runtime-evaluation/ALGOL-68/runtime-evaluation-2.alg
Normal file
35
Task/Runtime-evaluation/ALGOL-68/runtime-evaluation-2.alg
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
# procedure to call the Algol 68G evaluate procedure #
|
||||
# the environment of the evaluation will be the caller's environment #
|
||||
# with "code", "x" and "y" defined as the procedure parameters #
|
||||
PROC ev = ( STRING code, INT x, INT y )STRING: evaluate( code );
|
||||
|
||||
BEGIN
|
||||
|
||||
INT i := 1;
|
||||
INT j := 2;
|
||||
REAL x := 4.2;
|
||||
REAL y := 0.7164;
|
||||
|
||||
# evaluates "i + j" in the current environment #
|
||||
print( ( evaluate( "i + j" ), newline ) );
|
||||
|
||||
# evaluates "x + y" in the environment of the procedure body of ev #
|
||||
print( ( ev( "x + y", i, j ), newline ) );
|
||||
|
||||
# evaluates "x + y" in the current environment, so shows a different #
|
||||
# result to the previous call #
|
||||
print( ( evaluate( "x + y" ), newline ) );
|
||||
|
||||
# prints "code" because code is defined in the environment of the #
|
||||
# call to evaluate (in ev) although it is not defined in this #
|
||||
# environment #
|
||||
print( ( ev( "code", 1, 2 ), newline ) );
|
||||
|
||||
# prints "code + codecode + code" - see above #
|
||||
print( ( ev( "code + code", 1, 2 ), newline ) )
|
||||
|
||||
END
|
||||
|
||||
# if this next call was executed, a runtime error would occur as x and y #
|
||||
# do not exist anymore #
|
||||
# ;print( ( evaluate( "x + y" ), newline ) ) #
|
||||
5
Task/Runtime-evaluation/Arturo/runtime-evaluation.arturo
Normal file
5
Task/Runtime-evaluation/Arturo/runtime-evaluation.arturo
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
a: {print ["The result is:" 2+3]}
|
||||
do a
|
||||
|
||||
userCode: input "Give me some code: "
|
||||
do userCode
|
||||
32
Task/Runtime-evaluation/AutoHotkey/runtime-evaluation.ahk
Normal file
32
Task/Runtime-evaluation/AutoHotkey/runtime-evaluation.ahk
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
; requires AutoHotkey_H or AutoHotkey.dll
|
||||
msgbox % eval("3 + 4")
|
||||
msgbox % eval("4 + 4")
|
||||
return
|
||||
|
||||
|
||||
eval(expression)
|
||||
{
|
||||
global script
|
||||
script =
|
||||
(
|
||||
expression(){
|
||||
return %expression%
|
||||
}
|
||||
)
|
||||
renameFunction("expression", "") ; remove any previous expressions
|
||||
gosub load ; cannot use addScript inside a function yet
|
||||
exp := "expression"
|
||||
return %exp%()
|
||||
}
|
||||
|
||||
load:
|
||||
DllCall(A_AhkPath "\addScript","Str",script,"Uchar",0,"Cdecl UInt")
|
||||
return
|
||||
|
||||
renameFunction(funcName, newname){
|
||||
static
|
||||
x%newname% := newname ; store newname in a static variable so its memory is not freed
|
||||
strput(newname, &x%newname%, strlen(newname) + 1)
|
||||
if fnp := FindFunc(funcName)
|
||||
numput(&x%newname%, fnp+0, 0, "uint")
|
||||
}
|
||||
2
Task/Runtime-evaluation/BASIC/runtime-evaluation-1.basic
Normal file
2
Task/Runtime-evaluation/BASIC/runtime-evaluation-1.basic
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
10 REM load the next program
|
||||
20 LOAD "PROG2"
|
||||
5
Task/Runtime-evaluation/BASIC/runtime-evaluation-2.basic
Normal file
5
Task/Runtime-evaluation/BASIC/runtime-evaluation-2.basic
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
10 LET f$=CHR$ 187+"(x)+"+CHR$ 178+"(x*3)/2": REM LET f$="SQR (x)+SIN (x*3)/2"
|
||||
20 FOR x=0 TO 2 STEP 0.2
|
||||
30 LET y=VAL f$
|
||||
40 PRINT y
|
||||
50 NEXT x
|
||||
1
Task/Runtime-evaluation/BASIC/runtime-evaluation-3.basic
Normal file
1
Task/Runtime-evaluation/BASIC/runtime-evaluation-3.basic
Normal file
|
|
@ -0,0 +1 @@
|
|||
10 LET f= SQR (x)+SIN (x*3)/2
|
||||
1
Task/Runtime-evaluation/BASIC/runtime-evaluation-4.basic
Normal file
1
Task/Runtime-evaluation/BASIC/runtime-evaluation-4.basic
Normal file
|
|
@ -0,0 +1 @@
|
|||
10 LET f$=" SQR (x)+SIN (x*3)/2"
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
expr$ = "PI^2 + 1"
|
||||
PRINT EVAL(expr$)
|
||||
16
Task/Runtime-evaluation/BBC-BASIC/runtime-evaluation-2.basic
Normal file
16
Task/Runtime-evaluation/BBC-BASIC/runtime-evaluation-2.basic
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
exec$ = "PRINT ""Hello world!"""
|
||||
bbc$ = FNtokenise(exec$)
|
||||
|
||||
tmpfile$ = @tmp$+"temp.bbc"
|
||||
tmpfile% = OPENOUT(tmpfile$)
|
||||
BPUT#tmpfile%, bbc$+CHR$0
|
||||
CLOSE #tmpfile%
|
||||
|
||||
CALL tmpfile$
|
||||
END
|
||||
|
||||
DEF FNtokenise(A$)
|
||||
LOCAL A%
|
||||
A% = EVAL("0:"+A$)
|
||||
A$ = $(!332+2)
|
||||
= CHR$(LENA$+4) + CHR$0 + CHR$0 + A$ + CHR$13
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
blsq ) {5 5 .+}e!
|
||||
10
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
blsq ) 1 10r@{5.+}m[
|
||||
{6 7 8 9 10 11 12 13 14 15}
|
||||
blsq ) 1 10r@{5.+}6 0sam[
|
||||
{7 8 9 10 11 12 13 14 15 16}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
blsq ) 1 10r@"5.+"psm[
|
||||
{6 7 8 9 10 11 12 13 14 15}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
blsq ) "[m}+.5{@r01 1"<-pe
|
||||
{6 7 8 9 10 11 12 13 14 15}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
blsq ) {3 2}(?*)[+e!
|
||||
6
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
blsq ) ?+
|
||||
ERROR: Burlesque: (.+) Invalid arguments!
|
||||
blsq ) ?+to
|
||||
"Error"
|
||||
blsq ) (?+)
|
||||
?+
|
||||
blsq ) (?+)to
|
||||
"Ident"
|
||||
|
|
@ -0,0 +1 @@
|
|||
(eval '(+ 4 5)) ; returns 9
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(defun add-four-complicated (a-number)
|
||||
(eval `(+ 4 ',a-number)))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(defun add-four-by-function (a-number)
|
||||
(funcall (eval '(lambda (n) (+ 4 n)))) a-number)
|
||||
|
|
@ -0,0 +1 @@
|
|||
(eval (read-from-string "(+ 4 5)"))
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(let ((x 11) (y 22))
|
||||
;; This is an error! Inside the eval, x and y are unbound!
|
||||
(format t "~%x + y = ~a" (eval '(+ x y))))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(let ((x 11) (y 22))
|
||||
(format t "~%x + y = ~a" (eval `(+ ,x ,y))))
|
||||
|
|
@ -0,0 +1 @@
|
|||
(system:run-shell-command ...)
|
||||
|
|
@ -0,0 +1 @@
|
|||
(find-symbol "FOO" "BAR")
|
||||
2
Task/Runtime-evaluation/E/runtime-evaluation-1.e
Normal file
2
Task/Runtime-evaluation/E/runtime-evaluation-1.e
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
? e`1 + 1`.eval(safeScope)
|
||||
# value: 2
|
||||
5
Task/Runtime-evaluation/E/runtime-evaluation-2.e
Normal file
5
Task/Runtime-evaluation/E/runtime-evaluation-2.e
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
? def [value, env] := e`def x := 1 + 1`.evalToPair(safeScope)
|
||||
# value: [2, ...]
|
||||
|
||||
? e`x`.eval(env)
|
||||
# value: 2
|
||||
5
Task/Runtime-evaluation/E/runtime-evaluation-3.e
Normal file
5
Task/Runtime-evaluation/E/runtime-evaluation-3.e
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
? def prog := <elang:syntax.makeEParser>.run("1 + 1")
|
||||
# value: e`1.add(1)`
|
||||
|
||||
? prog.eval(safeScope)
|
||||
# value: 2
|
||||
6
Task/Runtime-evaluation/EchoLisp/runtime-evaluation.l
Normal file
6
Task/Runtime-evaluation/EchoLisp/runtime-evaluation.l
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
(eval (list * 6 7))
|
||||
→ 42
|
||||
(eval '(* 6 7)) ;; quoted argument
|
||||
→ 42
|
||||
(eval (read-from-string "(* 6 7)"))
|
||||
→ 42
|
||||
6
Task/Runtime-evaluation/Elena/runtime-evaluation.elena
Normal file
6
Task/Runtime-evaluation/Elena/runtime-evaluation.elena
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
import extensions'scripting;
|
||||
|
||||
public program()
|
||||
{
|
||||
lscript.interpret("system'console.writeLine(""Hello World"")");
|
||||
}
|
||||
6
Task/Runtime-evaluation/Elixir/runtime-evaluation.elixir
Normal file
6
Task/Runtime-evaluation/Elixir/runtime-evaluation.elixir
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
iex(1)> Code.eval_string("x + 4 * Enum.sum([1,2,3,4])", [x: 17])
|
||||
{57, [x: 17]}
|
||||
iex(2)> Code.eval_string("c = a + b", [a: 1, b: 2])
|
||||
{3, [a: 1, b: 2, c: 3]}
|
||||
iex(3)> Code.eval_string("a = a + b", [a: 1, b: 2])
|
||||
{3, [a: 3, b: 2]}
|
||||
10
Task/Runtime-evaluation/Erlang/runtime-evaluation.erl
Normal file
10
Task/Runtime-evaluation/Erlang/runtime-evaluation.erl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
1> {ok, Tokens, _} = erl_scan:string("X + 4 * lists:sum([1,2,3,4]).").
|
||||
...
|
||||
2> {ok, [Form]} = erl_parse:parse_exprs(Tokens).
|
||||
...
|
||||
3> Bindings = erl_eval:add_binding('X', 17, erl_eval:new_bindings()).
|
||||
[{'X',17}]
|
||||
4> {value, Value, _} = erl_eval:expr(Form, Bindings).
|
||||
{value,57,[{'X',17}]}
|
||||
5> Value.
|
||||
57
|
||||
4
Task/Runtime-evaluation/Forth/runtime-evaluation-1.fth
Normal file
4
Task/Runtime-evaluation/Forth/runtime-evaluation-1.fth
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
s" variable foo 1e fatan 4e f*" evaluate
|
||||
|
||||
f. \ 3.14159...
|
||||
1 foo !
|
||||
10
Task/Runtime-evaluation/Forth/runtime-evaluation-2.fth
Normal file
10
Task/Runtime-evaluation/Forth/runtime-evaluation-2.fth
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
unused . \ show how much dictionary space is available
|
||||
marker restore
|
||||
|
||||
create foo 30 allot
|
||||
: my-def 30 0 do cr i . ." test" loop ;
|
||||
|
||||
unused . \ lower than before
|
||||
|
||||
restore
|
||||
unused . \ same as first unused; restore, foo, and my-def no longer defined
|
||||
16
Task/Runtime-evaluation/FreeBASIC/runtime-evaluation.basic
Normal file
16
Task/Runtime-evaluation/FreeBASIC/runtime-evaluation.basic
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#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
|
||||
1
Task/Runtime-evaluation/Frink/runtime-evaluation.frink
Normal file
1
Task/Runtime-evaluation/Frink/runtime-evaluation.frink
Normal file
|
|
@ -0,0 +1 @@
|
|||
eval["length = 1234 feet + 2 inches"]
|
||||
10
Task/Runtime-evaluation/GW-BASIC/runtime-evaluation.basic
Normal file
10
Task/Runtime-evaluation/GW-BASIC/runtime-evaluation.basic
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
10 LINE INPUT "Type an expression: ",A$
|
||||
20 OPEN "CHAIN.TMP" FOR OUTPUT AS #1
|
||||
30 PRINT #1, "70 LET Y=("+A$+")"
|
||||
40 CLOSE #1
|
||||
50 CHAIN MERGE "CHAIN.TMP",60,ALL
|
||||
60 FOR X=0 TO 5
|
||||
70 REM
|
||||
80 PRINT X,Y
|
||||
90 NEXT X
|
||||
100 GOTO 10
|
||||
25
Task/Runtime-evaluation/Go/runtime-evaluation.go
Normal file
25
Task/Runtime-evaluation/Go/runtime-evaluation.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package main
|
||||
import (
|
||||
"fmt"
|
||||
"bitbucket.org/binet/go-eval/pkg/eval"
|
||||
"go/token"
|
||||
)
|
||||
|
||||
func main() {
|
||||
w := eval.NewWorld();
|
||||
fset := token.NewFileSet();
|
||||
|
||||
code, err := w.Compile(fset, "1 + 2")
|
||||
if err != nil {
|
||||
fmt.Println("Compile error");
|
||||
return
|
||||
}
|
||||
|
||||
val, err := code.Run();
|
||||
if err != nil {
|
||||
fmt.Println("Run time error");
|
||||
return;
|
||||
}
|
||||
fmt.Println("Return value:", val) //prints, well, 3
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
def years1 = new GroovyShell().evaluate('''
|
||||
(2008..2121).findAll {
|
||||
Date.parse("yyyy-MM-dd", "${it}-12-25").format("EEE") == "Sun"
|
||||
}
|
||||
''')
|
||||
|
||||
println years1
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
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"
|
||||
}
|
||||
''')
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
def years5 = Eval.xy(2008, 2121, '''
|
||||
(x..y).findAll {
|
||||
Date.parse("yyyy-MM-dd", "${it}-12-25").format("EEE") == "Sun"
|
||||
}
|
||||
''')
|
||||
|
||||
println years5
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
Procedure Main()
|
||||
local bAdd := {|Label,n1,n2| Qout( Label ), QQout( n1 + n2 )}
|
||||
Eval( bAdd, "5+5 = ", 5, 5 )
|
||||
Eval( bAdd, "5-5 = ", 5, -5 )
|
||||
return
|
||||
|
||||
Upon execution you see:
|
||||
5+5 = 10
|
||||
5-5 = 0
|
||||
8
Task/Runtime-evaluation/HicEst/runtime-evaluation.hicest
Normal file
8
Task/Runtime-evaluation/HicEst/runtime-evaluation.hicest
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
value = XEQ( " temp = 1 + 2 + 3 ") ! value is assigned 6
|
||||
! temp is undefined outside XEQ, if it was not defined before.
|
||||
|
||||
XEQ(" WRITE(Messagebox) 'Hello World !' ")
|
||||
|
||||
OPEN(FIle="my_file.txt")
|
||||
READ(FIle="my_file.txt", Row=6) string
|
||||
XEQ( string ) ! executes row 6 of my_file.txt
|
||||
1
Task/Runtime-evaluation/J/runtime-evaluation-1.j
Normal file
1
Task/Runtime-evaluation/J/runtime-evaluation-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
". 'a =: +/ 1 2 3' NB. execute a string to sum 1, 2 and 3 and assign to noun a
|
||||
1
Task/Runtime-evaluation/J/runtime-evaluation-2.j
Normal file
1
Task/Runtime-evaluation/J/runtime-evaluation-2.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
monad :'+/y' 1 2 3
|
||||
86
Task/Runtime-evaluation/Java/runtime-evaluation.java
Normal file
86
Task/Runtime-evaluation/Java/runtime-evaluation.java
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.URI;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.tools.FileObject;
|
||||
import javax.tools.ForwardingJavaFileManager;
|
||||
import javax.tools.JavaCompiler;
|
||||
import javax.tools.JavaFileObject;
|
||||
import javax.tools.SimpleJavaFileObject;
|
||||
import javax.tools.StandardJavaFileManager;
|
||||
import javax.tools.StandardLocation;
|
||||
import javax.tools.ToolProvider;
|
||||
|
||||
public class Evaluator{
|
||||
public static void main(String[] args){
|
||||
new Evaluator().eval(
|
||||
"SayHello",
|
||||
"public class SayHello{public void speak(){System.out.println(\"Hello world\");}}",
|
||||
"speak"
|
||||
);
|
||||
}
|
||||
|
||||
void eval(String className, String classCode, String methodName){
|
||||
Map<String, ByteArrayOutputStream> classCache = new HashMap<>();
|
||||
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
||||
|
||||
if ( null == compiler )
|
||||
throw new RuntimeException("Could not get a compiler.");
|
||||
|
||||
StandardJavaFileManager sfm = compiler.getStandardFileManager(null, null, null);
|
||||
ForwardingJavaFileManager<StandardJavaFileManager> fjfm = new ForwardingJavaFileManager<StandardJavaFileManager>(sfm){
|
||||
@Override
|
||||
public JavaFileObject getJavaFileForOutput(Location location, String className, JavaFileObject.Kind kind, FileObject sibling)
|
||||
throws IOException{
|
||||
if (StandardLocation.CLASS_OUTPUT == location && JavaFileObject.Kind.CLASS == kind)
|
||||
return new SimpleJavaFileObject(URI.create("mem:///" + className + ".class"), JavaFileObject.Kind.CLASS){
|
||||
@Override
|
||||
public OutputStream openOutputStream()
|
||||
throws IOException{
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
classCache.put(className, baos);
|
||||
return baos;
|
||||
}
|
||||
};
|
||||
else
|
||||
throw new IllegalArgumentException("Unexpected output file requested: " + location + ", " + className + ", " + kind);
|
||||
}
|
||||
};
|
||||
List<JavaFileObject> files = new LinkedList<JavaFileObject>(){{
|
||||
add(
|
||||
new SimpleJavaFileObject(URI.create("string:///" + className + ".java"), JavaFileObject.Kind.SOURCE){
|
||||
@Override
|
||||
public CharSequence getCharContent(boolean ignoreEncodingErrors){
|
||||
return classCode;
|
||||
}
|
||||
}
|
||||
);
|
||||
}};
|
||||
|
||||
// Now we can compile!
|
||||
compiler.getTask(null, fjfm, null, null, null, files).call();
|
||||
|
||||
try{
|
||||
Class<?> clarse = new ClassLoader(){
|
||||
@Override
|
||||
public Class<?> findClass(String name){
|
||||
if (! name.startsWith(className))
|
||||
throw new IllegalArgumentException("This class loader is for " + className + " - could not handle \"" + name + '"');
|
||||
byte[] bytes = classCache.get(name).toByteArray();
|
||||
return defineClass(name, bytes, 0, bytes.length);
|
||||
}
|
||||
}.loadClass(className);
|
||||
|
||||
// Invoke a method on the thing we compiled
|
||||
clarse.getMethod(methodName).invoke(clarse.newInstance());
|
||||
|
||||
}catch(ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException x){
|
||||
throw new RuntimeException("Run failed: " + x, x);
|
||||
}
|
||||
}
|
||||
}
|
||||
5
Task/Runtime-evaluation/JavaScript/runtime-evaluation.js
Normal file
5
Task/Runtime-evaluation/JavaScript/runtime-evaluation.js
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
var foo = eval('{value: 42}');
|
||||
eval('var bar = "Hello, world!";');
|
||||
|
||||
typeof foo; // 'object'
|
||||
typeof bar; // 'string'
|
||||
17
Task/Runtime-evaluation/Jsish/runtime-evaluation.jsish
Normal file
17
Task/Runtime-evaluation/Jsish/runtime-evaluation.jsish
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
/* Runtime evaluation, in Jsish */
|
||||
var foo = eval('{value: 42}');
|
||||
eval('var bar = "Hello, world!";');
|
||||
|
||||
;typeof foo;
|
||||
;foo.value;
|
||||
;typeof bar;
|
||||
;bar;
|
||||
|
||||
/*
|
||||
=!EXPECTSTART!=
|
||||
typeof foo ==> object
|
||||
foo.value ==> 42
|
||||
typeof bar ==> string
|
||||
bar ==> Hello, world!
|
||||
=!EXPECTEND!=
|
||||
*/
|
||||
1
Task/Runtime-evaluation/Julia/runtime-evaluation-1.julia
Normal file
1
Task/Runtime-evaluation/Julia/runtime-evaluation-1.julia
Normal file
|
|
@ -0,0 +1 @@
|
|||
include("myfile.jl")
|
||||
6
Task/Runtime-evaluation/Julia/runtime-evaluation-2.julia
Normal file
6
Task/Runtime-evaluation/Julia/runtime-evaluation-2.julia
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
include_string("""
|
||||
x = sum([1, 2, 3])
|
||||
@show x
|
||||
""")
|
||||
|
||||
@show typeof(x) # Int64
|
||||
15
Task/Runtime-evaluation/Lang/runtime-evaluation.lang
Normal file
15
Task/Runtime-evaluation/Lang/runtime-evaluation.lang
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
# 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))
|
||||
30
Task/Runtime-evaluation/Lasso/runtime-evaluation.lasso
Normal file
30
Task/Runtime-evaluation/Lasso/runtime-evaluation.lasso
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
//code, fragment name, autocollect, inplaintext
|
||||
local(mycode = "'Hello world, it is '+date")
|
||||
sourcefile('['+#mycode+']','arbritraty_name', true, true)->invoke
|
||||
|
||||
'\r'
|
||||
|
||||
|
||||
var(x = 100)
|
||||
local(mycode = "Outside Lasso\r['Hello world, var x is '+var(x)]")
|
||||
// autocollect (3rd param): return any output generated
|
||||
// inplaintext (4th param): if true, assumes this is mixed Lasso and plain text,
|
||||
// requires Lasso code to be in square brackets or other supported code block demarkation.
|
||||
sourcefile(#mycode,'arbritraty_name', true, true)->invoke
|
||||
|
||||
'\r'
|
||||
|
||||
var(y = 2)
|
||||
local(mycode = "'Hello world, is there output?\r'
|
||||
var(x) *= var(y)")
|
||||
// autocollect (3rd param): as false, no output returned
|
||||
// inplaintext (4th param): as false, assumes this is Lasso code, no mixed-mode Lasso and text.
|
||||
sourcefile(#mycode,'arbritraty_name', false, false)->invoke
|
||||
'var x is now: '+$x
|
||||
|
||||
'\r'
|
||||
|
||||
var(z = 3)
|
||||
local(mycode = "var(x) *= var(z)")
|
||||
sourcefile(#mycode,'arbritraty_name', false, false)->invoke
|
||||
'var x is now: '+$x
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
'Dimension a numerical and string array
|
||||
Dim myArray(5)
|
||||
Dim myStringArray$(5)
|
||||
|
||||
'Fill both arrays with the appropriate data
|
||||
For i = 0 To 5
|
||||
myArray(i) = i
|
||||
myStringArray$(i) = "String - " + str$(i)
|
||||
Next i
|
||||
|
||||
'Set two variables with the names of each array
|
||||
numArrayName$ = "myArray"
|
||||
strArrayName$ = "myStringArray"
|
||||
|
||||
'Retrieve the array data by evaluating a string
|
||||
'that correlates to the array
|
||||
For i = 0 To 5
|
||||
Print Eval$(numArrayName$ + "(" + str$(i) + ")")
|
||||
Print Eval$(strArrayName$ + "$(" + str$(i) + ")")
|
||||
Next i
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
Struct myStruct, value As long, _
|
||||
string As ptr
|
||||
myStruct.value.struct = 10
|
||||
myStruct.string.struct = "Hello World!"
|
||||
|
||||
structName$ = "myStruct"
|
||||
numElement$ = "value"
|
||||
strElement$ = "string"
|
||||
|
||||
Print Eval$(structName$ + "." + numElement$ + "." + "struct")
|
||||
|
||||
'Pay close attention that this is EVAL() because we are
|
||||
'retrieving the PTR to the string which is essentially a ulong
|
||||
Print Winstring(Eval(structName$ + "." + strElement$ + "." + "struct"))
|
||||
5
Task/Runtime-evaluation/Lua/runtime-evaluation-1.lua
Normal file
5
Task/Runtime-evaluation/Lua/runtime-evaluation-1.lua
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
f = loadstring(s) -- load a string as a function. Returns a function.
|
||||
|
||||
one = loadstring"return 1" -- one() returns 1
|
||||
|
||||
two = loadstring"return ..." -- two() returns the arguments passed to it
|
||||
2
Task/Runtime-evaluation/Lua/runtime-evaluation-2.lua
Normal file
2
Task/Runtime-evaluation/Lua/runtime-evaluation-2.lua
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
f = load("return 42")
|
||||
f() --> returns 42
|
||||
6
Task/Runtime-evaluation/Lua/runtime-evaluation-3.lua
Normal file
6
Task/Runtime-evaluation/Lua/runtime-evaluation-3.lua
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
> f = load("return 42")
|
||||
> f()
|
||||
42
|
||||
> n = load("return 42")()
|
||||
> n
|
||||
42
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
Module checkit {
|
||||
Module dummy {
|
||||
i++
|
||||
Print Number
|
||||
}
|
||||
\\ using Stack New { } we open a new stack for values, and old one connected back at the end
|
||||
\\ using block For This {} we erase any new definition, so we erase i (which Local make a new one)
|
||||
a$={
|
||||
Stack New {
|
||||
For this {
|
||||
Local i
|
||||
for i=1 to 10 : print i : next i
|
||||
}
|
||||
}
|
||||
If valid(k) then print k
|
||||
}
|
||||
i=500
|
||||
k=600
|
||||
Push 1000
|
||||
inline a$
|
||||
Print i=500
|
||||
Print Number=1000
|
||||
\\ eval an expression
|
||||
Print Eval("i+k")
|
||||
\\ eval a function
|
||||
Print Function("{read x : = x**2}", 2)=4
|
||||
Dim k(10)=123
|
||||
\\ eval array only
|
||||
Print array("k()", 2)=123
|
||||
Push 10, 10
|
||||
\\ call a module by make it inline first
|
||||
inline code dummy, dummy
|
||||
Print i=502
|
||||
}
|
||||
CheckIt
|
||||
37
Task/Runtime-evaluation/MATLAB/runtime-evaluation.m
Normal file
37
Task/Runtime-evaluation/MATLAB/runtime-evaluation.m
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
function testEval
|
||||
fprintf('Expressions:\n')
|
||||
x = eval('5+10^2')
|
||||
eval('y = (x-100).*[1 2 3]')
|
||||
eval('z = strcat(''my'', '' string'')')
|
||||
try
|
||||
w eval(' = 45')
|
||||
catch
|
||||
fprintf('Runtime error: interpretation of w is a function\n\n')
|
||||
end
|
||||
% eval('v') = 5
|
||||
% Invalid at compile-time as MATLAB interprets as using eval as a variable
|
||||
|
||||
fprintf('Other Statements:\n')
|
||||
nl = sprintf('\n');
|
||||
eval(['for k = 1:20' nl ...
|
||||
'fprintf(''%.3f\n'', k)' nl ...
|
||||
'if k == 3' nl ...
|
||||
'break' nl ...
|
||||
'end' nl ...
|
||||
'end'])
|
||||
true == eval('1')
|
||||
try
|
||||
true eval(' == 1')
|
||||
catch
|
||||
fprintf('Runtime error: interpretation of == 1 is of input to true\n\n')
|
||||
end
|
||||
|
||||
fprintf('Programming on the fly:\n')
|
||||
userIn = true;
|
||||
codeBlock = '';
|
||||
while userIn
|
||||
userIn = input('Enter next line of code: ', 's');
|
||||
codeBlock = [codeBlock nl userIn];
|
||||
end
|
||||
eval(codeBlock)
|
||||
end
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
Print[ToExpression["1 + 1"]];
|
||||
Print[ToExpression["Print[\"Hello, world!\"]; 10!"]];
|
||||
x = 5;
|
||||
Print[ToExpression["x!"]];
|
||||
Print[ToExpression["Module[{x = 8}, x!]"]];
|
||||
Print[MemoryConstrained[ToExpression["Range[5]"], 10000, {}]];
|
||||
Print[MemoryConstrained[ToExpression["Range[10^5]"], 10000, {}]];
|
||||
Print[TimeConstrained[ToExpression["Pause[1]; True"], 2, False]];
|
||||
Print[TimeConstrained[ToExpression["Pause[60]; True"], 2, False]];
|
||||
17
Task/Runtime-evaluation/Maxima/runtime-evaluation.maxima
Normal file
17
Task/Runtime-evaluation/Maxima/runtime-evaluation.maxima
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
/* Here is how to create a function and return a value at runtime. In the first example,
|
||||
the function is made global, i.e. it still exists after the statement is run. In the second example, the function
|
||||
is declared local. The evaluated string may read or write any variable defined before eval_string is run. */
|
||||
|
||||
kill(f)$
|
||||
|
||||
eval_string("block(f(x) := x^2 + 1, f(2))");
|
||||
5
|
||||
|
||||
fundef(f);
|
||||
/* f(x) := x^2 + 1 */
|
||||
|
||||
eval_string("block([f], local(f), f(x) := x^3 + 1, f(2))");
|
||||
9
|
||||
|
||||
fundef(f);
|
||||
/* f(x) := x^2 + 1 */
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
exec("println \"hello, world\"")
|
||||
exec("a = 1\nif a = 1\nprintln \"a is 1\"\nend\nprintln \"test\"")
|
||||
34
Task/Runtime-evaluation/Nim/runtime-evaluation.nim
Normal file
34
Task/Runtime-evaluation/Nim/runtime-evaluation.nim
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import ../compiler/[nimeval, llstream, ast], strformat, os
|
||||
|
||||
let std = findNimStdLibCompileTime()
|
||||
let modules = [std, std / "pure", std / "std", std / "core"]
|
||||
var intr = createInterpreter("script", modules)
|
||||
|
||||
#dynamic variable
|
||||
let varname = commandLineParams()[0]
|
||||
let expr = commandLineParams()[1]
|
||||
|
||||
#wrap the naked variable name and expression in a definition and proc,respectively to create valid code
|
||||
#for simplicity, the variable will always be an int, but one could of course define the type at runtime
|
||||
#globals and procs must be exported with * to be accessable
|
||||
#we also need to import any modules needed by the runtime code
|
||||
intr.evalScript(llStreamOpen(&"import math,sugar; var {varname}*:int; proc output*():auto = {expr}"))
|
||||
|
||||
for i in 0..2:
|
||||
#set 'varname' to a value
|
||||
intr.getGlobalValue(intr.selectUniqueSymbol(varname)).intval = i
|
||||
#evaluate the expression and get the result
|
||||
let output = intr.callRoutine(intr.selectRoutine("output"), [])
|
||||
#depending on the expression, the result could be any type
|
||||
#as an example, here we check for int,float,or string
|
||||
case output.kind
|
||||
of nkIntLit:
|
||||
echo expr, " = ", output.intval
|
||||
of nkFloatLit:
|
||||
echo expr, " = ", output.floatval
|
||||
of nkStrLit:
|
||||
echo expr, " = ", output.strval
|
||||
else:
|
||||
discard
|
||||
|
||||
destroyInterpreter(intr)
|
||||
5
Task/Runtime-evaluation/Oforth/runtime-evaluation-1.fth
Normal file
5
Task/Runtime-evaluation/Oforth/runtime-evaluation-1.fth
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
"[ [ $a, 12], [$b, 1.2], [ $c, [ $aaa, $bbb, $ccc ] ], [ $torun, #first ] ]" perform .s
|
||||
[1] (List) [[a, 12], [b, 1.2], [c, [aaa, bbb, ccc]], [torun, #first]]
|
||||
|
||||
"12 13 +" perform
|
||||
[1:interpreter] ExCompiler : Can't evaluate <+>
|
||||
4
Task/Runtime-evaluation/Oforth/runtime-evaluation-2.fth
Normal file
4
Task/Runtime-evaluation/Oforth/runtime-evaluation-2.fth
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
"12 13 + println" eval
|
||||
25
|
||||
": newFunction(a) a + ; 12 10 newFunction println" eval
|
||||
22
|
||||
3
Task/Runtime-evaluation/OoRexx/runtime-evaluation.rexx
Normal file
3
Task/Runtime-evaluation/OoRexx/runtime-evaluation.rexx
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
a = .array~of(1, 2, 3)
|
||||
ins = "loop num over a; say num; end"
|
||||
interpret ins
|
||||
50
Task/Runtime-evaluation/OxygenBasic/runtime-evaluation.basic
Normal file
50
Task/Runtime-evaluation/OxygenBasic/runtime-evaluation.basic
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
function ExecSeries(string s,double b,e,i) as string
|
||||
'===================================================
|
||||
'
|
||||
sys a,p
|
||||
string v,u,tab,cr,er
|
||||
'
|
||||
'PREPARE OUTPUT BUFFER
|
||||
'
|
||||
p=1
|
||||
cr=chr(13) chr(10)
|
||||
tab=chr(9)
|
||||
v=nuls 4096
|
||||
mid v,p,s+cr+cr
|
||||
p+=4+len s
|
||||
'
|
||||
double x,y,z 'shared variables
|
||||
'
|
||||
'COMPILE
|
||||
'
|
||||
a=compile s
|
||||
er=error
|
||||
if er then
|
||||
print "runtime error: " er : exit function
|
||||
end if
|
||||
'
|
||||
'EXECUTE
|
||||
'
|
||||
for x=b to e step i
|
||||
if p+128>=len v then
|
||||
v+=nuls len(v) 'extend buffer
|
||||
end if
|
||||
call a
|
||||
u=str(x) tab str(y) cr
|
||||
mid v,p,u : p+=len u
|
||||
next
|
||||
'
|
||||
freememory a 'release compiled code
|
||||
'
|
||||
return left v,p-1 'results
|
||||
'
|
||||
end function
|
||||
|
||||
'=====
|
||||
'TESTS
|
||||
'=====
|
||||
|
||||
'Expression, StartVal, EndVal stepVal, Increment
|
||||
|
||||
print ExecSeries "y=x*x*x", 1, 10, 1
|
||||
print ExecSeries "y=sqrt x",1, 9 , 1
|
||||
17
Task/Runtime-evaluation/Oz/runtime-evaluation.oz
Normal file
17
Task/Runtime-evaluation/Oz/runtime-evaluation.oz
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
declare
|
||||
%% simplest case: just evaluate expressions without bindings
|
||||
R1 = {Compiler.virtualStringToValue "{Abs ~42}"}
|
||||
{Show R1}
|
||||
|
||||
%% eval expressions with additional bindings and
|
||||
%% the possibility to kill the evaluation by calling KillProc
|
||||
KillProc
|
||||
R2 = {Compiler.evalExpression "{Abs A}" unit('A':~42) ?KillProc}
|
||||
{Show R2}
|
||||
|
||||
%% full control: add and remove bindings, eval expressions or
|
||||
%% statements, set compiler switches etc.
|
||||
Engine = {New Compiler.engine init}
|
||||
{Engine enqueue(setSwitch(expression false))} %% statements instead of expr.
|
||||
{Engine enqueue(mergeEnv(env('A':42 'System':System)))}
|
||||
{Engine enqueue(feedVirtualString("{System.show A}"))}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
runme(f)={
|
||||
f()
|
||||
};
|
||||
|
||||
runme( ()->print("Hello world!") )
|
||||
5
Task/Runtime-evaluation/PHP/runtime-evaluation.php
Normal file
5
Task/Runtime-evaluation/PHP/runtime-evaluation.php
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
$code = 'echo "hello world"';
|
||||
eval($code);
|
||||
$code = 'return "hello world"';
|
||||
print eval($code);
|
||||
2
Task/Runtime-evaluation/Perl/runtime-evaluation.pl
Normal file
2
Task/Runtime-evaluation/Perl/runtime-evaluation.pl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
my ($a, $b) = (-5, 7);
|
||||
$ans = eval 'abs($a * $b)'; # => 35
|
||||
21
Task/Runtime-evaluation/Phix/runtime-evaluation-1.phix
Normal file
21
Task/Runtime-evaluation/Phix/runtime-evaluation-1.phix
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Runtime_evaluation.exw</span>
|
||||
<span style="color: #008080;">without</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.1"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">include</span> <span style="color: #7060A8;">eval</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> <span style="color: #000080;font-style:italic;">-- (not an autoinclude, pulls in around 90% of the interpreter/compiler proper)</span>
|
||||
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">code</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
|
||||
integer i = 0
|
||||
bool r_init = false
|
||||
sequence r
|
||||
if not r_init then r = {} end if
|
||||
for k=1 to 4 do
|
||||
i += k
|
||||
r &= k
|
||||
end for
|
||||
"""</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">eval</span><span style="color: #0000FF;">(</span><span style="color: #000000;">code</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"i"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"r"</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- prints {10,{1,2,3,4}}</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">eval</span><span style="color: #0000FF;">(</span><span style="color: #000000;">code</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"r"</span><span style="color: #0000FF;">},{{</span><span style="color: #008000;">"r_init"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">"r"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">}}})</span> <span style="color: #000080;font-style:italic;">-- prints {5,1,2,3,4}</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">eval</span><span style="color: #0000FF;">(</span><span style="color: #000000;">code</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"i"</span><span style="color: #0000FF;">},{{</span><span style="color: #008000;">"i"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">}})</span> <span style="color: #000080;font-style:italic;">-- prints {25}</span>
|
||||
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">eval</span><span style="color: #0000FF;">(</span><span style="color: #008000;">`puts(1,"Hello World\n")`</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- prints Hello World</span>
|
||||
<!--
|
||||
7
Task/Runtime-evaluation/Phix/runtime-evaluation-2.phix
Normal file
7
Task/Runtime-evaluation/Phix/runtime-evaluation-2.phix
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">eval_expression</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">expr</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">object</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">res</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">eval</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"object x = %s"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">expr</span><span style="color: #0000FF;">}),{</span><span style="color: #008000;">"x"</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">eval_expression</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"3+4"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- prints 7</span>
|
||||
<!--
|
||||
9
Task/Runtime-evaluation/Pike/runtime-evaluation.pike
Normal file
9
Task/Runtime-evaluation/Pike/runtime-evaluation.pike
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
program demo = compile_string(#"
|
||||
string name=\"demo\";
|
||||
string hello()
|
||||
{
|
||||
return(\"hello, i am \"+name);
|
||||
}");
|
||||
|
||||
demo()->hello();
|
||||
Result: "hello, i am demo"
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
$test2plus2 = '2 + 2 -eq 4'
|
||||
Invoke-Expression $test2plus2
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
$say = {"Hello, world!"}
|
||||
& $say
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
$say = {param ([string]$Subject) "Hello, $Subject!"}
|
||||
& $say -Subject "my friend"
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
$say = {param ([string]$Exclamation, [string]$Subject) "$Exclamation, $Subject!"}
|
||||
& $say -Exclamation "Goodbye" -Subject "cruel world"
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
$title = "Dong Work For Yuda"
|
||||
$scriptblock = {$title}
|
||||
$closedScriptblock = $scriptblock.GetNewClosure()
|
||||
|
||||
& $scriptblock
|
||||
& $closedScriptblock
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
$title = "I'm Too Sexy"
|
||||
& $scriptblock
|
||||
& $closedScriptblock
|
||||
5
Task/Runtime-evaluation/Python/runtime-evaluation-1.py
Normal file
5
Task/Runtime-evaluation/Python/runtime-evaluation-1.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
>>> exec '''
|
||||
x = sum([1,2,3,4])
|
||||
print x
|
||||
'''
|
||||
10
|
||||
5
Task/Runtime-evaluation/Python/runtime-evaluation-2.py
Normal file
5
Task/Runtime-evaluation/Python/runtime-evaluation-2.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
>>> exec('''
|
||||
x = sum([1,2,3,4])
|
||||
print(x)
|
||||
''')
|
||||
10
|
||||
|
|
@ -0,0 +1 @@
|
|||
10 $ "1 swap times [ i 1+ * ]" quackery echo
|
||||
3
Task/Runtime-evaluation/R/runtime-evaluation-1.r
Normal file
3
Task/Runtime-evaluation/R/runtime-evaluation-1.r
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
expr1 <- quote(a+b*c)
|
||||
expr2 <- parse(text="a+b*c")[[1]]
|
||||
expr3 <- call("+", quote(`a`), call("*", quote(`b`), quote(`c`)))
|
||||
3
Task/Runtime-evaluation/R/runtime-evaluation-2.r
Normal file
3
Task/Runtime-evaluation/R/runtime-evaluation-2.r
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
> a <- 1; b <- 2; c <- 3
|
||||
> eval(expr1)
|
||||
[1] 7
|
||||
11
Task/Runtime-evaluation/R/runtime-evaluation-3.r
Normal file
11
Task/Runtime-evaluation/R/runtime-evaluation-3.r
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
> env <- as.environment(list(a=1, b=3, c=2))
|
||||
> evalq(a, env)
|
||||
[1] 1
|
||||
> eval(expr1, env) #this fails; env has only emptyenv() as a parent so can't find "+"
|
||||
Error in eval(expr, envir, enclos) : could not find function "+"
|
||||
> parent.env(env) <- sys.frame()
|
||||
> eval(expr1, env) # eval in env, enclosed in the current context
|
||||
[1] 7
|
||||
> assign("b", 5, env) # assign() can assign into environments
|
||||
> eval(expr1, env)
|
||||
[1] 11
|
||||
3
Task/Runtime-evaluation/REBOL/runtime-evaluation.rebol
Normal file
3
Task/Runtime-evaluation/REBOL/runtime-evaluation.rebol
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
a: -5
|
||||
b: 7
|
||||
answer: do [abs a * b] ; => 35
|
||||
20
Task/Runtime-evaluation/REXX/runtime-evaluation.rexx
Normal file
20
Task/Runtime-evaluation/REXX/runtime-evaluation.rexx
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/*REXX program illustrates the ability to execute code entered at runtime (from C.L.)*/
|
||||
numeric digits 10000000 /*ten million digits should do it. */
|
||||
bee=51
|
||||
stuff= 'bee=min(-2,44); say 13*2 "[from inside the box.]"; abc=abs(bee)'
|
||||
interpret stuff
|
||||
say 'bee=' bee
|
||||
say 'abc=' abc
|
||||
say
|
||||
/* [↓] now, we hear from the user. */
|
||||
say 'enter an expression:'
|
||||
pull expression
|
||||
say
|
||||
say 'expression entered is:' expression
|
||||
say
|
||||
|
||||
interpret '?='expression
|
||||
|
||||
say 'length of result='length(?)
|
||||
say ' left 50 bytes of result='left(?,50)"···"
|
||||
say 'right 50 bytes of result=···'right(?, 50) /*stick a fork in it, we're all done. */
|
||||
8
Task/Runtime-evaluation/Racket/runtime-evaluation.rkt
Normal file
8
Task/Runtime-evaluation/Racket/runtime-evaluation.rkt
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#lang racket
|
||||
(require racket/sandbox)
|
||||
(define e (make-evaluator 'racket))
|
||||
(e '(define + *))
|
||||
(e '(+ 10 20))
|
||||
(+ 10 20)
|
||||
;; (e '(delete-file "/etc/passwd"))
|
||||
;; --> delete-file: `delete' access denied for /etc/passwd
|
||||
4
Task/Runtime-evaluation/Raku/runtime-evaluation.raku
Normal file
4
Task/Runtime-evaluation/Raku/runtime-evaluation.raku
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
use MONKEY-SEE-NO-EVAL;
|
||||
|
||||
my ($a, $b) = (-5, 7);
|
||||
my $ans = EVAL 'abs($a * $b)'; # => 35
|
||||
5
Task/Runtime-evaluation/Ring/runtime-evaluation-1.ring
Normal file
5
Task/Runtime-evaluation/Ring/runtime-evaluation-1.ring
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
Eval("nOutput = 5+2*5 " )
|
||||
See "5+2*5 = " + nOutput + nl
|
||||
Eval("for x = 1 to 10 see x + nl next")
|
||||
Eval("func test see 'message from test!' ")
|
||||
test()
|
||||
12
Task/Runtime-evaluation/Ring/runtime-evaluation-2.ring
Normal file
12
Task/Runtime-evaluation/Ring/runtime-evaluation-2.ring
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
5+2*5 = 15
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
6
|
||||
7
|
||||
8
|
||||
9
|
||||
10
|
||||
message from test!
|
||||
9
Task/Runtime-evaluation/Ring/runtime-evaluation-3.ring
Normal file
9
Task/Runtime-evaluation/Ring/runtime-evaluation-3.ring
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
while true
|
||||
see nl + "code:> "
|
||||
give cCode
|
||||
try
|
||||
eval(cCode)
|
||||
catch
|
||||
see cCatchError
|
||||
done
|
||||
end
|
||||
20
Task/Runtime-evaluation/Ring/runtime-evaluation-4.ring
Normal file
20
Task/Runtime-evaluation/Ring/runtime-evaluation-4.ring
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
code:> see "hello world"
|
||||
hello world
|
||||
code:> for x = 1 to 10 see x + nl next
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
6
|
||||
7
|
||||
8
|
||||
9
|
||||
10
|
||||
|
||||
code:> func test see "Hello from test" + nl
|
||||
|
||||
code:> test()
|
||||
Hello from test
|
||||
|
||||
code:> bye
|
||||
2
Task/Runtime-evaluation/Ruby/runtime-evaluation-1.rb
Normal file
2
Task/Runtime-evaluation/Ruby/runtime-evaluation-1.rb
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
a, b = 5, -7
|
||||
ans = eval "(a * b).abs" # => 35
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue