Diff
Not logged in

Differences From Artifact [cea762cacb86424f]:

To Artifact [f2bd29fa05629861]:


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 }