Diff
Not logged in

Differences From Artifact [0a7ac481ec249b42]:

To Artifact [c5ee9ab6830c78bd]:


3 3 * License: NYSL 0.9982 http://www.kmonos.net/nysl/ 4 4 * 5 5 * Evaluator for Polemy programming language. 6 6 */ 7 7 module polemy.eval; 8 8 import polemy._common; 9 9 import polemy.ast; 10 +import polemy.parse; 10 11 import polemy.runtime; 11 12 12 13 Context createGlobalContext() 13 14 { 14 15 auto ctx = new Context; 15 16 ctx.add("+", new PrimitiveFunction(delegate Value(Value[] args){ 16 17 if( args.length != 2 ) ................................................................................ 42 43 if( auto x = cast(IntValue)args[0] ) 43 44 if( auto y = cast(IntValue)args[1] ) 44 45 return new IntValue(x.data/y.data); 45 46 throw new PolemyRuntimeException("cannot add non-integers"); // TODO improve this message 46 47 })); 47 48 return ctx; 48 49 } 50 + 51 +Context evalString(T...)(T params) 52 +{ 53 + return eval( parserFromString(params).parseProgram() ); 54 +} 55 + 56 +Context evalFile(T...)(T params) 57 +{ 58 + return eval( parserFromFile(params).parseProgram() ); 59 +} 49 60 50 61 Context eval(Program prog) 51 62 { 52 63 return eval(prog, createGlobalContext()); 53 64 } 54 65 55 66 Context eval(Program prog, Context ctx) ................................................................................ 120 131 121 132 122 133 version(unittest) import polemy.parse; 123 134 version(unittest) import std.stdio; 124 135 version(unittest) import std.exception; 125 136 unittest 126 137 { 127 - auto parser = parserFromString(`var x = 21; x = x + x*x;`); 128 - auto prog = parser.parseProgram(); 129 - auto ctx = eval(prog); 138 + auto ctx = evalString(`var x = 21; x = x + x*x;`); 130 139 assert( ctx["x"] == new IntValue(BigInt(21+21*21)) ); 131 140 assert( !collectException(ctx["x"]) ); 132 141 assert( collectException(ctx["y"]) ); 133 142 } 134 143 unittest 135 144 { 136 - auto parser = parserFromString(`var x = 21; x = x + x*y;`); 137 - auto prog = parser.parseProgram(); 138 - assert( collectException(eval(prog)) ); 145 + assert( collectException(evalString(`var x = 21; x = x + x*y;`)) ); 139 146 } 140 147 unittest 141 148 { 142 - auto parser = parserFromString(`var x = 21; y = x + x*x;`); 143 - auto prog = parser.parseProgram(); 144 - assert( collectException(eval(prog)) ); 149 + assert( collectException(evalString(`var x = 21; y = x + x*x;`)) ); 145 150 }