File Annotation
Not logged in
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: }
8de5b49cdf 2010-11-09        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: }
423f308350 2010-11-07        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").
1c01f44f52 2010-11-13        kinaba: import std.typetuple;
1c01f44f52 2010-11-13        kinaba: Value eval(AST e, Table ctx, bool splitCtx, Layer lay)
423f308350 2010-11-07        kinaba: {
1c01f44f52 2010-11-13        kinaba: 	return e.match(
1c01f44f52 2010-11-13        kinaba: 		(StrLiteral e)
1c01f44f52 2010-11-13        kinaba: 		{
c368edbcb1 2010-11-13        kinaba: 			Value v = new StrValue(e.data);
c368edbcb1 2010-11-13        kinaba: 			if( lay == "@v" )
c368edbcb1 2010-11-13        kinaba: 				return v;
c368edbcb1 2010-11-13        kinaba: 			else
c368edbcb1 2010-11-13        kinaba: 				return (cast(FunValue)ctx.get(lay, "(system)", e.pos)).call(e.pos, "@v", [v]);
1c01f44f52 2010-11-13        kinaba: 		},
1c01f44f52 2010-11-13        kinaba: 		(IntLiteral e)
1c01f44f52 2010-11-13        kinaba: 		{
c368edbcb1 2010-11-13        kinaba: 			Value v = new IntValue(e.data);
c368edbcb1 2010-11-13        kinaba: 			if( lay == "@v" )
c368edbcb1 2010-11-13        kinaba: 				return v;
c368edbcb1 2010-11-13        kinaba: 			else // are these "@v"s appropriate???
c368edbcb1 2010-11-13        kinaba: 				return (cast(FunValue)ctx.get(lay, "(system)", e.pos)).call(e.pos, "@v", [v]);
1c01f44f52 2010-11-13        kinaba: 		},
1c01f44f52 2010-11-13        kinaba: 		(VarExpression e)
1c01f44f52 2010-11-13        kinaba: 		{
c368edbcb1 2010-11-13        kinaba: 			try {
c368edbcb1 2010-11-13        kinaba: 				return ctx.get(e.var, lay, e.pos);
c368edbcb1 2010-11-13        kinaba: 			} catch( RuntimeException ) {
c368edbcb1 2010-11-13        kinaba: 				// rise
c368edbcb1 2010-11-13        kinaba: 				return (cast(FunValue)ctx.get(lay, "(system)", e.pos)).call(e.pos, "@v",
c368edbcb1 2010-11-13        kinaba: 					[ctx.get(e.var, "@v", e.pos)]
c368edbcb1 2010-11-13        kinaba: 				);
c368edbcb1 2010-11-13        kinaba: 			}
1c01f44f52 2010-11-13        kinaba: 		},
1c01f44f52 2010-11-13        kinaba: 		(LayeredExpression e)
1c01f44f52 2010-11-13        kinaba: 		{
1c01f44f52 2010-11-13        kinaba: 			return eval(e.expr, ctx, false, e.lay);
1c01f44f52 2010-11-13        kinaba: 		},
1c01f44f52 2010-11-13        kinaba: 		(LetExpression e)
1c01f44f52 2010-11-13        kinaba: 		{
1c01f44f52 2010-11-13        kinaba: 			// for letrec, we need this, but should avoid overwriting????
1c01f44f52 2010-11-13        kinaba: 			// ctx.set(e.var, "@v", new UndefinedValue, e.pos);
1c01f44f52 2010-11-13        kinaba: 			Value v = eval(e.init, ctx, true, lay);
1c01f44f52 2010-11-13        kinaba: 			if(splitCtx)
1c01f44f52 2010-11-13        kinaba: 				ctx = new Table(ctx, Table.Kind.NotPropagateSet);
1c01f44f52 2010-11-13        kinaba: 			ctx.set(e.var, (e.layer.length ? e.layer : lay), v, e.pos);
1c01f44f52 2010-11-13        kinaba: 			return eval(e.expr, ctx, false, lay);
1c01f44f52 2010-11-13        kinaba: 		},
1c01f44f52 2010-11-13        kinaba: 		(FuncallExpression e)
1c01f44f52 2010-11-13        kinaba: 		{
1c01f44f52 2010-11-13        kinaba: 			Value _f = eval(e.fun, ctx, true, lay);
1c01f44f52 2010-11-13        kinaba: 			if( auto f = cast(FunValue)_f ) {
1c01f44f52 2010-11-13        kinaba: 				Value[] args;
1c01f44f52 2010-11-13        kinaba: 				foreach(a; e.args)
1c01f44f52 2010-11-13        kinaba: 					args ~= eval(a, ctx, true, lay);
1c01f44f52 2010-11-13        kinaba: 				return f.call(e.pos, lay, args);
1c01f44f52 2010-11-13        kinaba: 			}
7465fcdd7f 2010-11-09        kinaba: 			throw genex!RuntimeException(e.pos, "Non-funcion is applied");
1c01f44f52 2010-11-13        kinaba: 		},
1c01f44f52 2010-11-13        kinaba: 		(FunLiteral e)
1c01f44f52 2010-11-13        kinaba: 		{
1c01f44f52 2010-11-13        kinaba: 			return new FunValue(delegate Value(immutable LexPosition pos, string lay, Value[] args){
1c01f44f52 2010-11-13        kinaba: 				if( e.params.length != args.length )
1c01f44f52 2010-11-13        kinaba: 					throw genex!RuntimeException(e.pos, sprintf!"Argument Number Mismatch (%d required but %d given)"
1c01f44f52 2010-11-13        kinaba: 						(e.params.length, args.length));
1c01f44f52 2010-11-13        kinaba: 				Table ctxNeo = new Table(ctx, Table.Kind.NotPropagateSet);
1c01f44f52 2010-11-13        kinaba: 				foreach(i,p; e.params)
1c01f44f52 2010-11-13        kinaba: 					ctxNeo.set(p.name, lay, args[i]);
1c01f44f52 2010-11-13        kinaba: 				return eval(e.funbody, ctxNeo, true, lay);
1c01f44f52 2010-11-13        kinaba: 			});
1c01f44f52 2010-11-13        kinaba: 		},
1c01f44f52 2010-11-13        kinaba: 		delegate Value (AST e)
1c01f44f52 2010-11-13        kinaba: 		{
1c01f44f52 2010-11-13        kinaba: 			throw genex!RuntimeException(e.pos, sprintf!"Unknown Kind of Expression %s"(typeid(e)));
1c01f44f52 2010-11-13        kinaba: 		}
1c01f44f52 2010-11-13        kinaba: 	);
5e407d7cf8 2010-11-08        kinaba: }
5e407d7cf8 2010-11-08        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
5e407d7cf8 2010-11-08        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: {
c368edbcb1 2010-11-13        kinaba: 	assert_throw!Throwable( evalString(`@@s(x){x}; @s "+"=fun(x,y){x-y};@s(1+2)`) );
c368edbcb1 2010-11-13        kinaba: 	assert_eq( evalString(`@@s(x){x}; @s "+"=fun(x,y){x-y};1+2`).val, new IntValue(BigInt(3)) );
c368edbcb1 2010-11-13        kinaba: 	assert_eq( evalString(`@@s(x){x}; @s "+"=fun(x,y){@v(@s(x)-@s(y))};1+2`).val, new IntValue(BigInt(3)) );
c368edbcb1 2010-11-13        kinaba: 	assert_eq( evalString(`@@s(x){x}; @s "+"=fun(x,y){@v(@s(x)-@s(y))};@s(1+2)`).val, new IntValue(BigInt(-1)) );
c368edbcb1 2010-11-13        kinaba: }
c368edbcb1 2010-11-13        kinaba: 
c368edbcb1 2010-11-13        kinaba: unittest
c368edbcb1 2010-11-13        kinaba: {
c368edbcb1 2010-11-13        kinaba: 	assert_eq( evalString(`@@t = fun(x){x+1}; @t(123)`).val, new IntValue(BigInt(124)) );
5e407d7cf8 2010-11-08        kinaba: }