Diff
Not logged in

Differences From Artifact [cea762cacb86424f]:

To Artifact [f2bd29fa05629861]:


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 +}