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 module polemy.eval; 7 module polemy.eval;
8 import polemy._common; 8 import polemy._common;
9 import polemy.lex : LexPosition; 9 import polemy.lex : LexPosition;
10 import polemy.ast; 10 import polemy.ast;
11 import polemy.parse; 11 import polemy.parse;
12 import polemy.runtime; 12 import polemy.runtime;
13 import std.typecons; 13 import std.typecons;
> 14 import std.stdio;
14 15
15 Context createGlobalContext() 16 Context createGlobalContext()
16 { 17 {
17 auto ctx = new Context; 18 auto ctx = new Context;
18 ctx.add("+", new FunValue(delegate Value(immutable LexPosition pos, Valu 19 ctx.add("+", new FunValue(delegate Value(immutable LexPosition pos, Valu
19 if( args.length != 2 ) 20 if( args.length != 2 )
20 throw new PolemyRuntimeException("+ takes two arguments! 21 throw new PolemyRuntimeException("+ takes two arguments!
................................................................................................................................................................................
42 ctx.add("/", new FunValue(delegate Value(immutable LexPosition pos, Valu 43 ctx.add("/", new FunValue(delegate Value(immutable LexPosition pos, Valu
43 if( args.length != 2 ) 44 if( args.length != 2 )
44 throw new PolemyRuntimeException("/ takes two arguments! 45 throw new PolemyRuntimeException("/ takes two arguments!
45 if( auto x = cast(IntValue)args[0] ) 46 if( auto x = cast(IntValue)args[0] )
46 if( auto y = cast(IntValue)args[1] ) 47 if( auto y = cast(IntValue)args[1] )
47 return new IntValue(x.data/y.data); 48 return new IntValue(x.data/y.data);
48 throw new PolemyRuntimeException("cannot add non-integers ["~to! 49 throw new PolemyRuntimeException("cannot add non-integers ["~to!
> 50 }));
> 51 ctx.add("print", new FunValue(delegate Value(immutable LexPosition pos,
> 52 foreach(a; args)
> 53 write(a);
> 54 writeln("");
> 55 return new UndefinedValue;
49 })); 56 }));
50 return ctx; 57 return ctx;
51 } 58 }
52 59
53 Tuple!(Value,"val",Context,"ctx") evalString(T...)(T params) 60 Tuple!(Value,"val",Context,"ctx") evalString(T...)(T params)
54 { 61 {
55 return eval( parserFromString(params).parseProgram() ); 62 return eval( parserFromString(params).parseProgram() );
................................................................................................................................................................................
167 } 174 }
168 unittest 175 unittest
169 { 176 {
170 auto r = evalString(`var x = 1; var f = fun(){x=x+1;}; f(); f(); f();`); 177 auto r = evalString(`var x = 1; var f = fun(){x=x+1;}; f(); f(); f();`);
171 assert( r.ctx["x"] == new IntValue(BigInt(4)) ); 178 assert( r.ctx["x"] == new IntValue(BigInt(4)) );
172 assert( r.val == new IntValue(BigInt(4)) ); 179 assert( r.val == new IntValue(BigInt(4)) );
173 } 180 }
> 181 unittest
> 182 {
> 183 evalString(`print("Hello, world!");`);
> 184 evalString(`print(fun(){});`);
> 185 }