4198578702 2010-11-07 kinaba: /** 4198578702 2010-11-07 kinaba: * Authors: k.inaba 4198578702 2010-11-07 kinaba: * License: NYSL 0.9982 http://www.kmonos.net/nysl/ 4198578702 2010-11-07 kinaba: * 4198578702 2010-11-07 kinaba: * Evaluator for Polemy programming language. 423f308350 2010-11-07 kinaba: */ 4198578702 2010-11-07 kinaba: module polemy.eval; 4198578702 2010-11-07 kinaba: import polemy._common; 423f308350 2010-11-07 kinaba: import polemy.ast; 423f308350 2010-11-07 kinaba: import polemy.runtime; 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: Context eval(Program prog) 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: return eval(prog, new Context); 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: Context eval(Program prog, Context ctx) 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: foreach(s; prog) 423f308350 2010-11-07 kinaba: ctx = eval(s, ctx); 423f308350 2010-11-07 kinaba: return ctx; 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: Context eval(Statement _s, Context ctx) 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: if( auto s = cast(DeclStatement)_s ) 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: auto v = eval(s.expr, ctx); 423f308350 2010-11-07 kinaba: ctx.add(s.var, v); 423f308350 2010-11-07 kinaba: return ctx; 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: else 423f308350 2010-11-07 kinaba: if( auto s = cast(ExprStatement)_s ) 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: eval(s.expr, ctx); 423f308350 2010-11-07 kinaba: return ctx; 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: throw new PolemyRuntimeException(sprintf!"Unknown Kind of Statement %s at [%s]"(typeid(_s), _s.pos)); 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: Value eval(Expression _e, Context ctx) 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: if( auto e = cast(StrLiteralExpression)_e ) 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: return new StrValue(e.data); 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: else 423f308350 2010-11-07 kinaba: if( auto e = cast(IntLiteralExpression)_e ) 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: return new IntValue(e.data); 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: else 423f308350 2010-11-07 kinaba: if( auto e = cast(VarExpression)_e ) 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: return ctx[e.var]; 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: else 423f308350 2010-11-07 kinaba: if( auto e = cast(BinOpExpression)_e ) 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: if( e.op == "=" ) 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: if( auto ev = cast(VarExpression)e.lhs ) 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: Value r = eval(e.rhs, ctx); 423f308350 2010-11-07 kinaba: ctx[ev.var] = r; 423f308350 2010-11-07 kinaba: return r; 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: throw new PolemyRuntimeException(sprintf!"Lhs of assignment must be a variable: %s"(e.pos)); 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: Value l = eval(e.lhs, ctx); 423f308350 2010-11-07 kinaba: Value r = eval(e.rhs, ctx); 423f308350 2010-11-07 kinaba: if( auto lv = cast(IntValue)l ) 423f308350 2010-11-07 kinaba: if( auto rv = cast(IntValue)r ) 423f308350 2010-11-07 kinaba: final switch(e.op) 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: case "+": return new IntValue(lv.data+rv.data); 423f308350 2010-11-07 kinaba: case "-": return new IntValue(lv.data-rv.data); 423f308350 2010-11-07 kinaba: case "*": return new IntValue(lv.data*rv.data); 423f308350 2010-11-07 kinaba: case "/": return new IntValue(lv.data/rv.data); 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: else 423f308350 2010-11-07 kinaba: throw new PolemyRuntimeException(sprintf!"rhs of %s must be an integer but was %s at [%s]"(e.op, typeid(r), e.rhs.pos)); 423f308350 2010-11-07 kinaba: else 423f308350 2010-11-07 kinaba: throw new PolemyRuntimeException(sprintf!"lhs of %s must be an integer but was %s at [%s]"(e.op, typeid(l), e.lhs.pos)); 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: throw new PolemyRuntimeException(sprintf!"Unknown Kind of Expression %s at [%s]"(typeid(_e), _e.pos)); 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: version(unittest) import polemy.parse; 423f308350 2010-11-07 kinaba: version(unittest) import std.stdio; 423f308350 2010-11-07 kinaba: version(unittest) import std.exception; 423f308350 2010-11-07 kinaba: unittest 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: auto parser = parserFromString(`var x = 21; x = x + x*x;`); 423f308350 2010-11-07 kinaba: auto prog = parser.parseProgram(); 423f308350 2010-11-07 kinaba: auto ctx = eval(prog); 423f308350 2010-11-07 kinaba: assert( ctx["x"] == new IntValue(BigInt(21+21*21)) ); 423f308350 2010-11-07 kinaba: assert( !collectException(ctx["x"]) ); 423f308350 2010-11-07 kinaba: assert( collectException(ctx["y"]) ); 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: unittest 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: auto parser = parserFromString(`var x = 21; x = x + x*y;`); 423f308350 2010-11-07 kinaba: auto prog = parser.parseProgram(); 423f308350 2010-11-07 kinaba: assert( collectException(eval(prog)) ); 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: unittest 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: auto parser = parserFromString(`var x = 21; y = x + x*x;`); 423f308350 2010-11-07 kinaba: auto prog = parser.parseProgram(); 423f308350 2010-11-07 kinaba: assert( collectException(eval(prog)) ); 423f308350 2010-11-07 kinaba: }