Diff
Not logged in

Differences From Artifact [0a7ac481ec249b42]:

To Artifact [c5ee9ab6830c78bd]:


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