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: 38fcc662be 2010-11-10 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 7465fcdd7f 2010-11-09 kinaba: ctx.set("+", "@v", new FunValue(delegate Value(immutable LexPosition pos, Layer lay, Value[] args){ 5d4cb856d8 2010-11-07 kinaba: if( args.length != 2 ) 7465fcdd7f 2010-11-09 kinaba: throw genex!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); 7465fcdd7f 2010-11-09 kinaba: throw genex!RuntimeException(pos, "cannot add non-integers"); 5d4cb856d8 2010-11-07 kinaba: })); 7465fcdd7f 2010-11-09 kinaba: ctx.set("-", "@v", new FunValue(delegate Value(immutable LexPosition pos, Layer lay, Value[] args){ 5d4cb856d8 2010-11-07 kinaba: if( args.length != 2 ) 7465fcdd7f 2010-11-09 kinaba: throw genex!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); 7465fcdd7f 2010-11-09 kinaba: throw genex!RuntimeException(pos, "cannot subtract non-integers"); 5d4cb856d8 2010-11-07 kinaba: })); 7465fcdd7f 2010-11-09 kinaba: ctx.set("*", "@v", new FunValue(delegate Value(immutable LexPosition pos, Layer lay, Value[] args){ 5d4cb856d8 2010-11-07 kinaba: if( args.length != 2 ) 7465fcdd7f 2010-11-09 kinaba: throw genex!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); 7465fcdd7f 2010-11-09 kinaba: throw genex!RuntimeException(pos, "cannot multiply non-integers"); 5d4cb856d8 2010-11-07 kinaba: })); 7465fcdd7f 2010-11-09 kinaba: ctx.set("/", "@v", new FunValue(delegate Value(immutable LexPosition pos, Layer lay, Value[] args){ 5d4cb856d8 2010-11-07 kinaba: if( args.length != 2 ) 7465fcdd7f 2010-11-09 kinaba: throw genex!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); 7465fcdd7f 2010-11-09 kinaba: throw genex!RuntimeException(pos, "cannot divide non-integers"); 172a537bea 2010-11-07 kinaba: })); 7465fcdd7f 2010-11-09 kinaba: ctx.set("<", "@v", new FunValue(delegate Value(immutable LexPosition pos, Layer lay, Value[] args){ 172a537bea 2010-11-07 kinaba: if( args.length != 2 ) 7465fcdd7f 2010-11-09 kinaba: throw genex!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))); 7465fcdd7f 2010-11-09 kinaba: throw genex!RuntimeException(pos, "cannot compare non-integers"); 172a537bea 2010-11-07 kinaba: })); 7465fcdd7f 2010-11-09 kinaba: ctx.set(">", "@v", new FunValue(delegate Value(immutable LexPosition pos, Layer lay, Value[] args){ 172a537bea 2010-11-07 kinaba: if( args.length != 2 ) 7465fcdd7f 2010-11-09 kinaba: throw genex!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))); 7465fcdd7f 2010-11-09 kinaba: throw genex!RuntimeException(pos, "cannot compare non-integers"); 820e7198cc 2010-11-07 kinaba: })); 7465fcdd7f 2010-11-09 kinaba: ctx.set("print", "@v", new FunValue(delegate Value(immutable LexPosition pos, Layer lay, 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: })); 7465fcdd7f 2010-11-09 kinaba: ctx.set("if", "@v", new FunValue(delegate Value(immutable LexPosition pos, Layer lay, Value[] args){ 633e700889 2010-11-07 kinaba: if( args.length != 3 ) 7465fcdd7f 2010-11-09 kinaba: throw genex!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] ) 7465fcdd7f 2010-11-09 kinaba: return (x.data == 0 ? fe : ft).call(pos,lay,[]); 7465fcdd7f 2010-11-09 kinaba: throw genex!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: 38fcc662be 2010-11-10 kinaba: /// Entry point of this module 38fcc662be 2010-11-10 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: } 38fcc662be 2010-11-10 kinaba: 38fcc662be 2010-11-10 kinaba: /// Entry point of this module 7465fcdd7f 2010-11-09 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(); 7465fcdd7f 2010-11-09 kinaba: return typeof(return)(eval(e, ctx, false, "@v"), ctx); 423f308350 2010-11-07 kinaba: } 38fcc662be 2010-11-10 kinaba: 38fcc662be 2010-11-10 kinaba: /// Entry point of this module 38fcc662be 2010-11-10 kinaba: /// If splitCtx = true, then inner variable declaration do not overwrite ctx. 38fcc662be 2010-11-10 kinaba: /// lay is the layer ID for evaluation (standard value semantics uses "@v"). 7465fcdd7f 2010-11-09 kinaba: 7465fcdd7f 2010-11-09 kinaba: Value eval(AST _e, Table ctx, bool splitCtx, Layer lay) 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???? 7465fcdd7f 2010-11-09 kinaba: // ctx.set(e.var, "@v", new UndefinedValue, e.pos); 7465fcdd7f 2010-11-09 kinaba: Value v = eval(e.init, ctx, true, lay); 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); 7465fcdd7f 2010-11-09 kinaba: return eval(e.expr, ctx, false, lay); 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: { 7465fcdd7f 2010-11-09 kinaba: Value _f = eval(e.fun, ctx, true, lay); 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) 7465fcdd7f 2010-11-09 kinaba: args ~= eval(a, ctx, true, lay); 7465fcdd7f 2010-11-09 kinaba: return f.call(e.pos, lay, args); 5d4cb856d8 2010-11-07 kinaba: } else 7465fcdd7f 2010-11-09 kinaba: throw genex!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: { 7465fcdd7f 2010-11-09 kinaba: return new FunValue(delegate Value(immutable LexPosition pos, string lay, Value[] args){ 0569f7b8c2 2010-11-07 kinaba: if( e.params.length != args.length ) 7465fcdd7f 2010-11-09 kinaba: throw genex!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) 8e6fa743ee 2010-11-11 kinaba: ctxNeo.set(p.name, lay, args[i]); 7465fcdd7f 2010-11-09 kinaba: return eval(e.funbody, ctxNeo, true, lay); 0569f7b8c2 2010-11-07 kinaba: }); 423f308350 2010-11-07 kinaba: } 7465fcdd7f 2010-11-09 kinaba: throw genex!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)) ); 7465fcdd7f 2010-11-09 kinaba: assert_eq( r.ctx.get("x","@v"), new IntValue(BigInt(21)) ); 7465fcdd7f 2010-11-09 kinaba: assert_nothrow( r.ctx.get("x","@v") ); 7465fcdd7f 2010-11-09 kinaba: assert_throw!RuntimeException( r.ctx.get("y","@v") ); 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)) ); 7465fcdd7f 2010-11-09 kinaba: assert_eq( r.ctx.get("x","@v"), new IntValue(BigInt(21+21*21)) ); 7465fcdd7f 2010-11-09 kinaba: assert_nothrow( r.ctx.get("x","@v") ); 7465fcdd7f 2010-11-09 kinaba: assert_throw!RuntimeException( r.ctx.get("y","@v") ); 0f02103885 2010-11-09 kinaba: } 0f02103885 2010-11-09 kinaba: unittest 0f02103885 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)) ); 8de5b49cdf 2010-11-09 kinaba: } 8de5b49cdf 2010-11-09 kinaba: unittest 8de5b49cdf 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))); 7465fcdd7f 2010-11-09 kinaba: } 7465fcdd7f 2010-11-09 kinaba: 7465fcdd7f 2010-11-09 kinaba: unittest 7465fcdd7f 2010-11-09 kinaba: { 7465fcdd7f 2010-11-09 kinaba: assert_throw!Throwable( evalString(`@s "+"=fun(x,y){x-y};@s(1+2)`) ); 7465fcdd7f 2010-11-09 kinaba: assert_eq( evalString(`@s "+"=fun(x,y){x-y};1+2`).val, new IntValue(BigInt(3)) ); 7465fcdd7f 2010-11-09 kinaba: assert_eq( evalString(`@s "+"=fun(x,y){@v(@s(x)-@s(y))};1+2`).val, new IntValue(BigInt(3)) ); 7465fcdd7f 2010-11-09 kinaba: assert_eq( evalString(`@s "+"=fun(x,y){@v(@s(x)-@s(y))};@s(1+2)`).val, new IntValue(BigInt(-1)) ); 5e407d7cf8 2010-11-08 kinaba: }