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;
3464a035ec 2010-11-20        kinaba: import polemy.failure;
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;
0569f7b8c2 2010-11-07        kinaba: 
38fcc662be 2010-11-10        kinaba: ///
8de5b49cdf 2010-11-09        kinaba: Table createGlobalContext()
172a537bea 2010-11-07        kinaba: {
8de5b49cdf 2010-11-09        kinaba: 	auto ctx = new Table;
8474ae68d9 2010-11-13        kinaba: 	ctx.set("+", "@v", native( (IntValue lhs, IntValue rhs){return new IntValue(lhs.data + rhs.data);} ));
8474ae68d9 2010-11-13        kinaba: 	ctx.set("-", "@v", native( (IntValue lhs, IntValue rhs){return new IntValue(lhs.data - rhs.data);} ));
8474ae68d9 2010-11-13        kinaba: 	ctx.set("*", "@v", native( (IntValue lhs, IntValue rhs){return new IntValue(lhs.data * rhs.data);} ));
8474ae68d9 2010-11-13        kinaba: 	ctx.set("/", "@v", native( (IntValue lhs, IntValue rhs){return new IntValue(lhs.data / rhs.data);} ));
8474ae68d9 2010-11-13        kinaba: 	ctx.set("%", "@v", native( (IntValue lhs, IntValue rhs){return new IntValue(lhs.data % rhs.data);} ));
078444a806 2010-11-13        kinaba: 	ctx.set("||", "@v", native( (IntValue lhs, IntValue rhs){return new IntValue(BigInt((lhs.data!=0) || (rhs.data!=0) ? 1:0));} ));
078444a806 2010-11-13        kinaba: 	ctx.set("&&", "@v", native( (IntValue lhs, IntValue rhs){return new IntValue(BigInt((lhs.data!=0) && (rhs.data!=0) ? 1:0));} ));
078444a806 2010-11-13        kinaba: 	ctx.set("<", "@v", native( (Value lhs, Value rhs){return new IntValue(BigInt(lhs < rhs ? 1: 0));} ));
078444a806 2010-11-13        kinaba: 	ctx.set(">", "@v", native( (Value lhs, Value rhs){return new IntValue(BigInt(lhs > rhs ? 1: 0));} ));
078444a806 2010-11-13        kinaba: 	ctx.set("<=", "@v", native( (Value lhs, Value rhs){return new IntValue(BigInt(lhs <= rhs ? 1: 0));} ));
078444a806 2010-11-13        kinaba: 	ctx.set(">=", "@v", native( (Value lhs, Value rhs){return new IntValue(BigInt(lhs >= rhs ? 1: 0));} ));
078444a806 2010-11-13        kinaba: 	ctx.set("==", "@v", native( (Value lhs, Value rhs){return new IntValue(BigInt(lhs == rhs ? 1: 0));} ));
078444a806 2010-11-13        kinaba: 	ctx.set("!=", "@v", native( (Value lhs, Value rhs){return new IntValue(BigInt(lhs != rhs ? 1: 0));} ));
7465fcdd7f 2010-11-09        kinaba: 	ctx.set("print", "@v", new FunValue(delegate Value(immutable LexPosition pos, Layer lay, Value[] args){
8de5b49cdf 2010-11-09        kinaba: 		foreach(a; args)
8de5b49cdf 2010-11-09        kinaba: 			write(a);
8de5b49cdf 2010-11-09        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){
8de5b49cdf 2010-11-09        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] )
8de5b49cdf 2010-11-09        kinaba: 		if( auto ft = cast(FunValue)args[1] )
8de5b49cdf 2010-11-09        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");
5d4cb856d8 2010-11-07        kinaba: 	}));
3a2762fca5 2010-11-13        kinaba: 	ctx.set("_isint", "@v", native( (Value v){return new IntValue(BigInt(cast(IntValue)v is null ? 0 : 1));} ));
3a2762fca5 2010-11-13        kinaba: 	ctx.set("_isstr", "@v", native( (Value v){return new IntValue(BigInt(cast(StrValue)v is null ? 0 : 1));} ));
3a2762fca5 2010-11-13        kinaba: 	ctx.set("_isfun", "@v", native( (Value v){return new IntValue(BigInt(cast(FunValue)v is null ? 0 : 1));} ));
5afe8e3f26 2010-11-13        kinaba: 	ctx.set("_isundefined", "@v", native( (Value v){return new IntValue(BigInt(cast(UndValue)v is null ? 0 : 1));} ));
515502e8d1 2010-11-20        kinaba: 	ctx.set("_istable", "@v", native( (Value v){return new IntValue(BigInt(cast(Table)v is null ? 0 : 1));} ));
515502e8d1 2010-11-20        kinaba: 	ctx.set(".", "@v", native( (Table t, StrValue s){
515502e8d1 2010-11-20        kinaba: 		return (t.has(s.data, "@v") ? t.get(s.data, "@v") : new UndValue);
515502e8d1 2010-11-20        kinaba: 	}) );
515502e8d1 2010-11-20        kinaba: 	ctx.set(".?", "@v", native( (Table t, StrValue s){
515502e8d1 2010-11-20        kinaba: 		return new IntValue(BigInt(t.has(s.data, "@v") ? 1 : 0));
515502e8d1 2010-11-20        kinaba: 	}) );
515502e8d1 2010-11-20        kinaba: 	ctx.set(".=", "@v", native( (Table t, StrValue s, Value v){
515502e8d1 2010-11-20        kinaba: 		auto t2 = new Table(t, Table.Kind.NotPropagateSet);
515502e8d1 2010-11-20        kinaba: 		t2.set(s.data, "@v", v);
515502e8d1 2010-11-20        kinaba: 		return t2;
515502e8d1 2010-11-20        kinaba: 	}) );
515502e8d1 2010-11-20        kinaba: 	ctx.set("{}", "@v", native( (){
515502e8d1 2010-11-20        kinaba: 		return new Table;
515502e8d1 2010-11-20        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)
0569f7b8c2 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);
7465fcdd7f 2010-11-09        kinaba: }
7465fcdd7f 2010-11-09        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").
8474ae68d9 2010-11-13        kinaba: 
1c01f44f52 2010-11-13        kinaba: Value eval(AST e, Table ctx, bool splitCtx, Layer lay)
7465fcdd7f 2010-11-09        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;
8474ae68d9 2010-11-13        kinaba: 			else // rise
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;
8474ae68d9 2010-11-13        kinaba: 			else // rise
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: 		{
078444a806 2010-11-13        kinaba: 			if( lay == "@v" )
078444a806 2010-11-13        kinaba: 				return ctx.get(e.var, lay, e.pos);
c368edbcb1 2010-11-13        kinaba: 			try {
c368edbcb1 2010-11-13        kinaba: 				return ctx.get(e.var, lay, e.pos);
078444a806 2010-11-13        kinaba: 			} catch( Throwable ) { // [TODO] more precise...
8474ae68d9 2010-11-13        kinaba: 				// rise from @v
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: 		{
8e3db9ef20 2010-11-20        kinaba: 			if( e.lay == "@macro" )
8e3db9ef20 2010-11-20        kinaba: 				return macroEval(e.expr, ctx, false);
8e3db9ef20 2010-11-20        kinaba: 			else
060f267779 2010-11-20        kinaba: 				return eval(e.expr, ctx, true, 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: 			if(splitCtx)
1c01f44f52 2010-11-13        kinaba: 				ctx = new Table(ctx, Table.Kind.NotPropagateSet);
060f267779 2010-11-20        kinaba: 			Value v = eval(e.init, ctx, true, lay);
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: 		{
5afe8e3f26 2010-11-13        kinaba: 			Value[Value[]][Layer] memo;
8e3db9ef20 2010-11-20        kinaba: 			AST macroMemo = null; // cache
5afe8e3f26 2010-11-13        kinaba: 
8474ae68d9 2010-11-13        kinaba: 			// funvalue need not be rised
5afe8e3f26 2010-11-13        kinaba: 			// no, need to be rised !!  suppose @t(fib)("int")
1c01f44f52 2010-11-13        kinaba: 			return new FunValue(delegate Value(immutable LexPosition pos, string lay, Value[] args){
5afe8e3f26 2010-11-13        kinaba: 				// TODO: only auto raised ones need memo? no?
5afe8e3f26 2010-11-13        kinaba: 				// auto memoization
8e3db9ef20 2010-11-20        kinaba: 				if( lay != "@v" && lay != "@macro" )
5afe8e3f26 2010-11-13        kinaba: 				{
5afe8e3f26 2010-11-13        kinaba: 					if( auto memolay = lay in memo )
5afe8e3f26 2010-11-13        kinaba: 						if( auto pv = args in *memolay )
5afe8e3f26 2010-11-13        kinaba: 							return *pv;
5afe8e3f26 2010-11-13        kinaba: 					memo[lay][args] = (cast(FunValue)ctx.get(lay, "(system)", e.pos)).call(e.pos, "@v",
5afe8e3f26 2010-11-13        kinaba: 						[new UndValue]
5afe8e3f26 2010-11-13        kinaba: 					);
5afe8e3f26 2010-11-13        kinaba: 				}
5afe8e3f26 2010-11-13        kinaba: 
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]);
8e3db9ef20 2010-11-20        kinaba: 
8e3db9ef20 2010-11-20        kinaba: 				// @macro run!!!
8e3db9ef20 2010-11-20        kinaba: 				if( lay == "@macro" )
8e3db9ef20 2010-11-20        kinaba: 					return macroEval(e.funbody, ctxNeo, false);
8e3db9ef20 2010-11-20        kinaba: 				if( macroMemo is null )
8e3db9ef20 2010-11-20        kinaba: 					macroMemo = tableToAST("@v",macroEval(e.funbody, ctxNeo, true));
8e3db9ef20 2010-11-20        kinaba: 				auto v = eval(macroMemo, ctxNeo, true, lay);
8e3db9ef20 2010-11-20        kinaba: 
8e3db9ef20 2010-11-20        kinaba: 				//auto v = eval(e.funbody, ctxNeo, true, lay);
5afe8e3f26 2010-11-13        kinaba: 				// auto memoization
8e3db9ef20 2010-11-20        kinaba: 				if( lay != "@v" && lay != "@macro" )
5afe8e3f26 2010-11-13        kinaba: 					memo[lay][args] = v;
5afe8e3f26 2010-11-13        kinaba: 				return v;
1c01f44f52 2010-11-13        kinaba: 			});
3f6f41b558 2010-11-20        kinaba: 		},
3f6f41b558 2010-11-20        kinaba: 		delegate Value (AST e)
3f6f41b558 2010-11-20        kinaba: 		{
3f6f41b558 2010-11-20        kinaba: 			throw genex!RuntimeException(e.pos, sprintf!"Unknown Kind of Expression %s"(typeid(e)));
3f6f41b558 2010-11-20        kinaba: 		}
3f6f41b558 2010-11-20        kinaba: 	);
3f6f41b558 2010-11-20        kinaba: }
3f6f41b558 2010-11-20        kinaba: 
3f6f41b558 2010-11-20        kinaba: // [TODO] Optimization
3f6f41b558 2010-11-20        kinaba: Value macroEval(AST e, Table ctx, bool AlwaysMacro)
3f6f41b558 2010-11-20        kinaba: {
3f6f41b558 2010-11-20        kinaba: 	Layer theLayer = "@v";
3f6f41b558 2010-11-20        kinaba: 
3f6f41b558 2010-11-20        kinaba: 	Table pos = new Table;
3f6f41b558 2010-11-20        kinaba: 	pos.set("filename", theLayer, new StrValue(e.pos.filename));
3f6f41b558 2010-11-20        kinaba: 	pos.set("lineno",   theLayer, new IntValue(BigInt(e.pos.lineno)));
3f6f41b558 2010-11-20        kinaba: 	pos.set("column",   theLayer, new IntValue(BigInt(e.pos.column)));
3f6f41b558 2010-11-20        kinaba: 	return e.match(
3f6f41b558 2010-11-20        kinaba: 		(StrLiteral e)
3f6f41b558 2010-11-20        kinaba: 		{
3f6f41b558 2010-11-20        kinaba: 			Table t = new Table;
3f6f41b558 2010-11-20        kinaba: 			t.set("pos",  theLayer, pos);
3f6f41b558 2010-11-20        kinaba: 			t.set("is",   theLayer, new StrValue("str"));
3f6f41b558 2010-11-20        kinaba: 			t.set("data", theLayer, new StrValue(e.data));
3f6f41b558 2010-11-20        kinaba: 			return t;
3f6f41b558 2010-11-20        kinaba: 		},
3f6f41b558 2010-11-20        kinaba: 		(IntLiteral e)
3f6f41b558 2010-11-20        kinaba: 		{
3f6f41b558 2010-11-20        kinaba: 			Table t = new Table;
3f6f41b558 2010-11-20        kinaba: 			t.set("pos",  theLayer, pos);
3f6f41b558 2010-11-20        kinaba: 			t.set("is",   theLayer, new StrValue("int"));
3f6f41b558 2010-11-20        kinaba: 			t.set("data", theLayer, new IntValue(e.data));
3f6f41b558 2010-11-20        kinaba: 			return t;
3f6f41b558 2010-11-20        kinaba: 		},
3f6f41b558 2010-11-20        kinaba: 		(VarExpression e)
3f6f41b558 2010-11-20        kinaba: 		{
8e3db9ef20 2010-11-20        kinaba: 			try {
8e3db9ef20 2010-11-20        kinaba: 				return ctx.get(e.var, "@macro", e.pos);
8e3db9ef20 2010-11-20        kinaba: 			} catch( Throwable ) {// [TODO] more precies...
8e3db9ef20 2010-11-20        kinaba: 				Table t = new Table;
8e3db9ef20 2010-11-20        kinaba: 				t.set("pos",  theLayer, pos);
8e3db9ef20 2010-11-20        kinaba: 				t.set("is",   theLayer, new StrValue("var"));
8e3db9ef20 2010-11-20        kinaba: 				t.set("name", theLayer, new StrValue(e.var));
8e3db9ef20 2010-11-20        kinaba: 				return cast(Value)t;
8e3db9ef20 2010-11-20        kinaba: 			}
3f6f41b558 2010-11-20        kinaba: 		},
3f6f41b558 2010-11-20        kinaba: 		(LayeredExpression e)
3f6f41b558 2010-11-20        kinaba: 		{
3f6f41b558 2010-11-20        kinaba: 			if( AlwaysMacro )
3f6f41b558 2010-11-20        kinaba: 			{
3f6f41b558 2010-11-20        kinaba: 				Table t = new Table;
3f6f41b558 2010-11-20        kinaba: 				t.set("pos",  theLayer, pos);
3f6f41b558 2010-11-20        kinaba: 				t.set("is",   theLayer, new StrValue("lay"));
8e3db9ef20 2010-11-20        kinaba: 				t.set("layer",   theLayer, new StrValue(e.lay));
3f6f41b558 2010-11-20        kinaba: 				t.set("expr", theLayer, macroEval(e.expr,ctx,AlwaysMacro));
3f6f41b558 2010-11-20        kinaba: 				return cast(Value)t;
3f6f41b558 2010-11-20        kinaba: 			}
3f6f41b558 2010-11-20        kinaba: 			else
3f6f41b558 2010-11-20        kinaba: 			{
3f6f41b558 2010-11-20        kinaba: 				return eval(e.expr, ctx, true, e.lay);
3f6f41b558 2010-11-20        kinaba: 			}
3f6f41b558 2010-11-20        kinaba: 		},
3f6f41b558 2010-11-20        kinaba: 		(LetExpression e)
3f6f41b558 2010-11-20        kinaba: 		{
3f6f41b558 2010-11-20        kinaba: 			Table t = new Table;
3f6f41b558 2010-11-20        kinaba: 			t.set("pos",  theLayer, pos);
3f6f41b558 2010-11-20        kinaba: 			t.set("is",   theLayer, new StrValue("let"));
3f6f41b558 2010-11-20        kinaba: 			t.set("name", theLayer, new StrValue(e.var));
3f6f41b558 2010-11-20        kinaba: 			t.set("init", theLayer, macroEval(e.init,ctx,AlwaysMacro));
3f6f41b558 2010-11-20        kinaba: 			t.set("expr", theLayer, macroEval(e.expr,ctx,AlwaysMacro));
3f6f41b558 2010-11-20        kinaba: 			return t;
3f6f41b558 2010-11-20        kinaba: 		},
3f6f41b558 2010-11-20        kinaba: 		(FuncallExpression e)
3f6f41b558 2010-11-20        kinaba: 		{
8e3db9ef20 2010-11-20        kinaba: 			Value _f = macroEval(e.fun,ctx,AlwaysMacro);
8e3db9ef20 2010-11-20        kinaba: 
8e3db9ef20 2010-11-20        kinaba: 			// copy & pase from normal eval
8e3db9ef20 2010-11-20        kinaba: 			// [TODO] sync with @layerd parameters.
8e3db9ef20 2010-11-20        kinaba: 			if( auto f = cast(FunValue)_f ) {
8e3db9ef20 2010-11-20        kinaba: 				Value[] args;
8e3db9ef20 2010-11-20        kinaba: 				foreach(a; e.args)
8e3db9ef20 2010-11-20        kinaba: 					args ~= macroEval(a, ctx, AlwaysMacro);
8e3db9ef20 2010-11-20        kinaba: 				return f.call(e.pos, "@macro", args); // explicit @macro is the best???
8e3db9ef20 2010-11-20        kinaba: 			}
8e3db9ef20 2010-11-20        kinaba: 
3f6f41b558 2010-11-20        kinaba: 			Table t = new Table;
3f6f41b558 2010-11-20        kinaba: 			t.set("pos",  theLayer, pos);
3f6f41b558 2010-11-20        kinaba: 			t.set("is",   theLayer, new StrValue("app"));
8e3db9ef20 2010-11-20        kinaba: 			t.set("fun",  theLayer, _f);
3f6f41b558 2010-11-20        kinaba: 			Table args = new Table;
3f6f41b558 2010-11-20        kinaba: 			foreach_reverse(a; e.args) {
3f6f41b558 2010-11-20        kinaba: 				Table cons = new Table;
3f6f41b558 2010-11-20        kinaba: 				cons.set("car",theLayer,macroEval(a,ctx,AlwaysMacro));
3f6f41b558 2010-11-20        kinaba: 				cons.set("cdr",theLayer,args);
3f6f41b558 2010-11-20        kinaba: 				args = cons;
3f6f41b558 2010-11-20        kinaba: 			}
3f6f41b558 2010-11-20        kinaba: 			t.set("arg", theLayer, args);
8e3db9ef20 2010-11-20        kinaba: 			return cast(Value)t;
3f6f41b558 2010-11-20        kinaba: 		},
3f6f41b558 2010-11-20        kinaba: 		(FunLiteral e)
3f6f41b558 2010-11-20        kinaba: 		{
3f6f41b558 2010-11-20        kinaba: 			Table t = new Table;
3f6f41b558 2010-11-20        kinaba: 			t.set("pos",   theLayer, pos);
3f6f41b558 2010-11-20        kinaba: 			t.set("is",    theLayer, new StrValue("fun"));
3f6f41b558 2010-11-20        kinaba: 			t.set("body",  theLayer, macroEval(e.funbody,ctx,AlwaysMacro));
3f6f41b558 2010-11-20        kinaba: 			Table param = new Table;
3f6f41b558 2010-11-20        kinaba: 			foreach_reverse(p; e.params)
3f6f41b558 2010-11-20        kinaba: 			{
3f6f41b558 2010-11-20        kinaba: 				Table cons = new Table;
3f6f41b558 2010-11-20        kinaba: 				Table kv = new Table;
3f6f41b558 2010-11-20        kinaba: 				kv.set("name", theLayer, new StrValue(p.name));
3f6f41b558 2010-11-20        kinaba: 				foreach_reverse(lay; p.layers)
3f6f41b558 2010-11-20        kinaba: 				{
3f6f41b558 2010-11-20        kinaba: 					Table cons2 = new Table;
3f6f41b558 2010-11-20        kinaba: 					cons2.set("car", theLayer, new StrValue(lay));
3f6f41b558 2010-11-20        kinaba: 					cons2.set("cdr", theLayer, kv);
3f6f41b558 2010-11-20        kinaba: 					kv = cons2;
3f6f41b558 2010-11-20        kinaba: 				}
3f6f41b558 2010-11-20        kinaba: 				cons.set("car", theLayer, kv);
3f6f41b558 2010-11-20        kinaba: 				cons.set("cdr", theLayer, param);
3f6f41b558 2010-11-20        kinaba: 				param = cons;
3f6f41b558 2010-11-20        kinaba: 			}
3f6f41b558 2010-11-20        kinaba: 			t.set("param", theLayer, param);
3f6f41b558 2010-11-20        kinaba: 			return t;
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)) );
078444a806 2010-11-13        kinaba: 	assert_throw!Throwable( 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: 	};
8e3db9ef20 2010-11-20        kinaba: 	fib(5);`).val, new IntValue(BigInt(8)));
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_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)) );
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_eq( evalString(`@@t = fun(x){x+1}; @t(123)`).val, new IntValue(BigInt(124)) );
060f267779 2010-11-20        kinaba: 	// there was a bug that declaration in the first line of function definition
060f267779 2010-11-20        kinaba: 	// cannot be recursive
060f267779 2010-11-20        kinaba: 	assert_nothrow( evalString(`def foo() {
060f267779 2010-11-20        kinaba:   def bar(y) { if(y<1) {0} else {bar(0)} };
060f267779 2010-11-20        kinaba:   bar(1)
060f267779 2010-11-20        kinaba: }; foo()`) );
5e407d7cf8 2010-11-08        kinaba: }