8de5b49cdf 2010-11-09 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; 0569f7b8c2 2010-11-07 kinaba: import polemy.lex : LexPosition; 423f308350 2010-11-07 kinaba: import polemy.ast; 3f5dc76a75 2010-11-07 kinaba: import polemy.parse; b0d8d7875b 2010-11-08 kinaba: import polemy.value; 0569f7b8c2 2010-11-07 kinaba: import std.typecons; 820e7198cc 2010-11-07 kinaba: import std.stdio; 423f308350 2010-11-07 kinaba: 8de5b49cdf 2010-11-09 kinaba: Table createGlobalContext() 423f308350 2010-11-07 kinaba: { 8de5b49cdf 2010-11-09 kinaba: auto ctx = new Table; 8de5b49cdf 2010-11-09 kinaba: // [TODO] autogenerate these typechecks 8de5b49cdf 2010-11-09 kinaba: ctx.set("+", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){ 5d4cb856d8 2010-11-07 kinaba: if( args.length != 2 ) 8de5b49cdf 2010-11-09 kinaba: throw new RuntimeException(pos, "+ takes two arguments!!"); 5d4cb856d8 2010-11-07 kinaba: if( auto x = cast(IntValue)args[0] ) 5d4cb856d8 2010-11-07 kinaba: if( auto y = cast(IntValue)args[1] ) 5d4cb856d8 2010-11-07 kinaba: return new IntValue(x.data+y.data); 8de5b49cdf 2010-11-09 kinaba: throw new RuntimeException(pos, "cannot add non-integers"); 5d4cb856d8 2010-11-07 kinaba: })); 8de5b49cdf 2010-11-09 kinaba: ctx.set("-", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){ 5d4cb856d8 2010-11-07 kinaba: if( args.length != 2 ) 8de5b49cdf 2010-11-09 kinaba: throw new RuntimeException(pos, "- takes two arguments!!"); 5d4cb856d8 2010-11-07 kinaba: if( auto x = cast(IntValue)args[0] ) 5d4cb856d8 2010-11-07 kinaba: if( auto y = cast(IntValue)args[1] ) 5d4cb856d8 2010-11-07 kinaba: return new IntValue(x.data-y.data); 8de5b49cdf 2010-11-09 kinaba: throw new RuntimeException(pos, "cannot subtract non-integers"); 5d4cb856d8 2010-11-07 kinaba: })); 8de5b49cdf 2010-11-09 kinaba: ctx.set("*", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){ 5d4cb856d8 2010-11-07 kinaba: if( args.length != 2 ) 8de5b49cdf 2010-11-09 kinaba: throw new RuntimeException(pos, "* takes two arguments!!"); 5d4cb856d8 2010-11-07 kinaba: if( auto x = cast(IntValue)args[0] ) 5d4cb856d8 2010-11-07 kinaba: if( auto y = cast(IntValue)args[1] ) 5d4cb856d8 2010-11-07 kinaba: return new IntValue(x.data*y.data); 8de5b49cdf 2010-11-09 kinaba: throw new RuntimeException(pos, "cannot multiply non-integers"); 5d4cb856d8 2010-11-07 kinaba: })); 8de5b49cdf 2010-11-09 kinaba: ctx.set("/", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){ 5d4cb856d8 2010-11-07 kinaba: if( args.length != 2 ) 8de5b49cdf 2010-11-09 kinaba: throw new RuntimeException(pos, "/ takes two arguments!!"); 5d4cb856d8 2010-11-07 kinaba: if( auto x = cast(IntValue)args[0] ) 5d4cb856d8 2010-11-07 kinaba: if( auto y = cast(IntValue)args[1] ) 5d4cb856d8 2010-11-07 kinaba: return new IntValue(x.data/y.data); 8de5b49cdf 2010-11-09 kinaba: throw new RuntimeException(pos, "cannot divide non-integers"); 172a537bea 2010-11-07 kinaba: })); 8de5b49cdf 2010-11-09 kinaba: ctx.set("<", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){ 172a537bea 2010-11-07 kinaba: if( args.length != 2 ) 8de5b49cdf 2010-11-09 kinaba: throw new RuntimeException(pos, "< takes two arguments!!"); 172a537bea 2010-11-07 kinaba: if( auto x = cast(IntValue)args[0] ) 172a537bea 2010-11-07 kinaba: if( auto y = cast(IntValue)args[1] ) 172a537bea 2010-11-07 kinaba: return new IntValue(BigInt(to!int(x.data < y.data))); 8de5b49cdf 2010-11-09 kinaba: throw new RuntimeException(pos, "cannot compare non-integers"); 172a537bea 2010-11-07 kinaba: })); 8de5b49cdf 2010-11-09 kinaba: ctx.set(">", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){ 172a537bea 2010-11-07 kinaba: if( args.length != 2 ) 8de5b49cdf 2010-11-09 kinaba: throw new RuntimeException(pos, "> takes two arguments!!"); 172a537bea 2010-11-07 kinaba: if( auto x = cast(IntValue)args[0] ) 172a537bea 2010-11-07 kinaba: if( auto y = cast(IntValue)args[1] ) 172a537bea 2010-11-07 kinaba: return new IntValue(BigInt(to!int(x.data>y.data))); 8de5b49cdf 2010-11-09 kinaba: throw new RuntimeException(pos, "cannot compare non-integers"); 820e7198cc 2010-11-07 kinaba: })); 8de5b49cdf 2010-11-09 kinaba: ctx.set("print", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){ 820e7198cc 2010-11-07 kinaba: foreach(a; args) 820e7198cc 2010-11-07 kinaba: write(a); 820e7198cc 2010-11-07 kinaba: writeln(""); 8de5b49cdf 2010-11-09 kinaba: return new IntValue(BigInt(178)); 633e700889 2010-11-07 kinaba: })); 8de5b49cdf 2010-11-09 kinaba: ctx.set("if", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){ 633e700889 2010-11-07 kinaba: if( args.length != 3 ) 8de5b49cdf 2010-11-09 kinaba: throw new RuntimeException(pos, "if takes three arguments!!"); 633e700889 2010-11-07 kinaba: if( auto x = cast(IntValue)args[0] ) 633e700889 2010-11-07 kinaba: if( auto ft = cast(FunValue)args[1] ) 633e700889 2010-11-07 kinaba: if( auto fe = cast(FunValue)args[2] ) 633e700889 2010-11-07 kinaba: return (x.data == 0 ? fe : ft).call(pos,[]); 8de5b49cdf 2010-11-09 kinaba: throw new RuntimeException(pos, "type mismatch in if"); 0569f7b8c2 2010-11-07 kinaba: })); 423f308350 2010-11-07 kinaba: return ctx; 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: 8de5b49cdf 2010-11-09 kinaba: /// Entry point of this module 8de5b49cdf 2010-11-09 kinaba: 8de5b49cdf 2010-11-09 kinaba: Tuple!(Value,"val",Table,"ctx") evalString(S,T...)(S str, T fn_ln_cn) 0569f7b8c2 2010-11-07 kinaba: { 8de5b49cdf 2010-11-09 kinaba: return eval( polemy.parse.parseString(str, fn_ln_cn) ); 0569f7b8c2 2010-11-07 kinaba: } 0569f7b8c2 2010-11-07 kinaba: 7de80acfb8 2010-11-09 kinaba: Tuple!(Value,"val",Table,"ctx") evalFile(S, T...)(S filename, T ln_cn) 0569f7b8c2 2010-11-07 kinaba: { 8de5b49cdf 2010-11-09 kinaba: return eval( polemy.parse.parseFile(filename, ln_cn) ); 0569f7b8c2 2010-11-07 kinaba: } 0569f7b8c2 2010-11-07 kinaba: 8de5b49cdf 2010-11-09 kinaba: Tuple!(Value,"val",Table,"ctx") eval(AST e) 423f308350 2010-11-07 kinaba: { 8de5b49cdf 2010-11-09 kinaba: Table ctx = createGlobalContext(); 8de5b49cdf 2010-11-09 kinaba: return typeof(return)(eval(e, ctx), ctx); 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: dc93ad8cf6 2010-11-09 kinaba: Value eval(AST _e, Table ctx, bool splitCtx = false, Layer lay="@val") 423f308350 2010-11-07 kinaba: { 8de5b49cdf 2010-11-09 kinaba: if( auto e = cast(StrLiteral)_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 8de5b49cdf 2010-11-09 kinaba: if( auto e = cast(IntLiteral)_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: { dc93ad8cf6 2010-11-09 kinaba: return ctx.get(e.var, lay, e.pos); dc93ad8cf6 2010-11-09 kinaba: } dc93ad8cf6 2010-11-09 kinaba: else dc93ad8cf6 2010-11-09 kinaba: if( auto e = cast(LayeredExpression)_e ) dc93ad8cf6 2010-11-09 kinaba: { dc93ad8cf6 2010-11-09 kinaba: return eval(e.expr, ctx, false, e.lay); 5d4cb856d8 2010-11-07 kinaba: } 5d4cb856d8 2010-11-07 kinaba: else 8de5b49cdf 2010-11-09 kinaba: if( auto e = cast(LetExpression)_e ) 5d4cb856d8 2010-11-07 kinaba: { 8de5b49cdf 2010-11-09 kinaba: // for letrec, we need this, but should avoid overwriting???? 8de5b49cdf 2010-11-09 kinaba: // ctx.set(e.var, "@val", new UndefinedValue, e.pos); 8de5b49cdf 2010-11-09 kinaba: Value v = eval(e.init, ctx, true); 0f02103885 2010-11-09 kinaba: if(splitCtx) 0f02103885 2010-11-09 kinaba: ctx = new Table(ctx, Table.Kind.NotPropagateSet); dc93ad8cf6 2010-11-09 kinaba: ctx.set(e.var, (e.layer.length ? e.layer : lay), v, e.pos); 8de5b49cdf 2010-11-09 kinaba: return eval(e.expr, ctx); 5d4cb856d8 2010-11-07 kinaba: } 5d4cb856d8 2010-11-07 kinaba: else 5d4cb856d8 2010-11-07 kinaba: if( auto e = cast(FuncallExpression)_e ) 5d4cb856d8 2010-11-07 kinaba: { 5d4cb856d8 2010-11-07 kinaba: Value _f = eval(e.fun, ctx); 5d4cb856d8 2010-11-07 kinaba: if( auto f = cast(FunValue)_f ) { 5d4cb856d8 2010-11-07 kinaba: Value[] args; 5d4cb856d8 2010-11-07 kinaba: foreach(a; e.args) 5d4cb856d8 2010-11-07 kinaba: args ~= eval(a, ctx); 0569f7b8c2 2010-11-07 kinaba: return f.call(e.pos, args); 5d4cb856d8 2010-11-07 kinaba: } else 8de5b49cdf 2010-11-09 kinaba: throw new RuntimeException(e.pos, "Non-funcion is applied"); 0569f7b8c2 2010-11-07 kinaba: } 0569f7b8c2 2010-11-07 kinaba: else 8de5b49cdf 2010-11-09 kinaba: if( auto e = cast(FunLiteral)_e ) 0569f7b8c2 2010-11-07 kinaba: { 0569f7b8c2 2010-11-07 kinaba: return new FunValue(delegate Value(immutable LexPosition pos, Value[] args){ 0569f7b8c2 2010-11-07 kinaba: if( e.params.length != args.length ) 8de5b49cdf 2010-11-09 kinaba: throw new RuntimeException(e.pos, sprintf!"Argument Number Mismatch (%d required but %d given)" 8de5b49cdf 2010-11-09 kinaba: (e.params.length, args.length)); 8de5b49cdf 2010-11-09 kinaba: Table ctxNeo = new Table(ctx, Table.Kind.NotPropagateSet); 0569f7b8c2 2010-11-07 kinaba: foreach(i,p; e.params) 8de5b49cdf 2010-11-09 kinaba: ctxNeo.set(p, "@val", args[i]); 0569f7b8c2 2010-11-07 kinaba: return eval(e.funbody, ctxNeo); 0569f7b8c2 2010-11-07 kinaba: }); 423f308350 2010-11-07 kinaba: } 8de5b49cdf 2010-11-09 kinaba: throw new RuntimeException(_e.pos, sprintf!"Unknown Kind of Expression %s"(typeid(_e))); 8de5b49cdf 2010-11-09 kinaba: } 8de5b49cdf 2010-11-09 kinaba: 8de5b49cdf 2010-11-09 kinaba: unittest 8de5b49cdf 2010-11-09 kinaba: { 8de5b49cdf 2010-11-09 kinaba: auto r = assert_nothrow( evalString(`var x = 21; x + x*x;`) ); 8de5b49cdf 2010-11-09 kinaba: assert_eq( r.val, new IntValue(BigInt(21+21*21)) ); 8de5b49cdf 2010-11-09 kinaba: assert_eq( r.ctx.get("x","@val"), new IntValue(BigInt(21)) ); 8de5b49cdf 2010-11-09 kinaba: assert_nothrow( r.ctx.get("x","@val") ); 8de5b49cdf 2010-11-09 kinaba: assert_throw!RuntimeException( r.ctx.get("y","@val") ); 8de5b49cdf 2010-11-09 kinaba: } 8de5b49cdf 2010-11-09 kinaba: unittest 8de5b49cdf 2010-11-09 kinaba: { 8de5b49cdf 2010-11-09 kinaba: auto r = assert_nothrow( evalString(`var x = 21; var x = x + x*x;`) ); 8de5b49cdf 2010-11-09 kinaba: assert_eq( r.val, new IntValue(BigInt(21+21*21)) ); 8de5b49cdf 2010-11-09 kinaba: assert_eq( r.ctx.get("x","@val"), new IntValue(BigInt(21+21*21)) ); 8de5b49cdf 2010-11-09 kinaba: assert_nothrow( r.ctx.get("x","@val") ); 8de5b49cdf 2010-11-09 kinaba: assert_throw!RuntimeException( r.ctx.get("y","@val") ); 8de5b49cdf 2010-11-09 kinaba: } 8de5b49cdf 2010-11-09 kinaba: unittest 8de5b49cdf 2010-11-09 kinaba: { 0f02103885 2010-11-09 kinaba: assert_eq( evalString(`let x=1; let y=(let x=2); x`).val, new IntValue(BigInt(1)) ); 0f02103885 2010-11-09 kinaba: assert_eq( evalString(`let x=1; let y=(let x=2;fun(){x}); y()`).val, new IntValue(BigInt(2)) ); 0f02103885 2010-11-09 kinaba: } 0f02103885 2010-11-09 kinaba: unittest 0f02103885 2010-11-09 kinaba: { dc93ad8cf6 2010-11-09 kinaba: assert_eq( evalString(`@a x=1; @b x=2; @a(x)`).val, new IntValue(BigInt(1)) ); dc93ad8cf6 2010-11-09 kinaba: assert_eq( evalString(`@a x=1; @b x=2; @b(x)`).val, new IntValue(BigInt(2)) ); dc93ad8cf6 2010-11-09 kinaba: assert_eq( evalString(`let x=1; let _ = (@a x=2;2); x`).val, new IntValue(BigInt(1)) ); dc93ad8cf6 2010-11-09 kinaba: assert_throw!Error( evalString(`let x=1; let _ = (@a x=2;2); @a(x)`) ); dc93ad8cf6 2010-11-09 kinaba: } dc93ad8cf6 2010-11-09 kinaba: dc93ad8cf6 2010-11-09 kinaba: unittest dc93ad8cf6 2010-11-09 kinaba: { 2459e9a821 2010-11-09 kinaba: assert_eq( evalString(`var fac = fun(x){ 633e700889 2010-11-07 kinaba: if(x) 633e700889 2010-11-07 kinaba: { x*fac(x-1); } 633e700889 2010-11-07 kinaba: else 633e700889 2010-11-07 kinaba: { 1; }; 633e700889 2010-11-07 kinaba: }; 2459e9a821 2010-11-09 kinaba: fac(10);`).val, new IntValue(BigInt(10*9*8*5040))); 2459e9a821 2010-11-09 kinaba: assert_eq( evalString(`var fib = fun(x){ 172a537bea 2010-11-07 kinaba: if(x<2) 172a537bea 2010-11-07 kinaba: { 1; } 172a537bea 2010-11-07 kinaba: else 172a537bea 2010-11-07 kinaba: { fib(x-1) + fib(x-2); }; 172a537bea 2010-11-07 kinaba: }; 2459e9a821 2010-11-09 kinaba: fib(10);`).val, new IntValue(BigInt(89))); 423f308350 2010-11-07 kinaba: }