Differences From Artifact [cea762cacb86424f]:
- File
polemy/eval.d
- 2010-11-07 14:34:29 - part of checkin [0569f7b8c2] on branch trunk - - Added function literal evaluator (i.e., closure). - Workaround for d2stacktrace's infinite-loop bug. (when std.demangle.demangle use exception inside it, it will go into an infinite loop. to avoid this, I choose to unset TraceHandler during stacktrace generation. This is far from the complete solution, but at least it should work as expected under single-thread environment...) (user: kinaba) [annotate]
To Artifact [f2bd29fa05629861]:
- File
polemy/eval.d
- 2010-11-07 15:03:38 - part of checkin [820e7198cc] on branch trunk - Made helloworld work. (user: kinaba) [annotate]
7 7 module polemy.eval;
8 8 import polemy._common;
9 9 import polemy.lex : LexPosition;
10 10 import polemy.ast;
11 11 import polemy.parse;
12 12 import polemy.runtime;
13 13 import std.typecons;
14 +import std.stdio;
14 15
15 16 Context createGlobalContext()
16 17 {
17 18 auto ctx = new Context;
18 19 ctx.add("+", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
19 20 if( args.length != 2 )
20 21 throw new PolemyRuntimeException("+ takes two arguments!! ["~to!string(pos)~"]");
................................................................................
42 43 ctx.add("/", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
43 44 if( args.length != 2 )
44 45 throw new PolemyRuntimeException("/ takes two arguments!! ["~to!string(pos)~"]");
45 46 if( auto x = cast(IntValue)args[0] )
46 47 if( auto y = cast(IntValue)args[1] )
47 48 return new IntValue(x.data/y.data);
48 49 throw new PolemyRuntimeException("cannot add non-integers ["~to!string(pos)~"]");
50 + }));
51 + ctx.add("print", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
52 + foreach(a; args)
53 + write(a);
54 + writeln("");
55 + return new UndefinedValue;
49 56 }));
50 57 return ctx;
51 58 }
52 59
53 60 Tuple!(Value,"val",Context,"ctx") evalString(T...)(T params)
54 61 {
55 62 return eval( parserFromString(params).parseProgram() );
................................................................................
167 174 }
168 175 unittest
169 176 {
170 177 auto r = evalString(`var x = 1; var f = fun(){x=x+1;}; f(); f(); f();`);
171 178 assert( r.ctx["x"] == new IntValue(BigInt(4)) );
172 179 assert( r.val == new IntValue(BigInt(4)) );
173 180 }
181 +unittest
182 +{
183 + evalString(`print("Hello, world!");`);
184 + evalString(`print(fun(){});`);
185 +}