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;
435fa085ec 2010-11-21        kinaba: import polemy.layer;
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()
0569f7b8c2 2010-11-07        kinaba: {
8de5b49cdf 2010-11-09        kinaba: 	auto ctx = new Table;
435fa085ec 2010-11-21        kinaba: 	ctx.set("+", ValueLayer, native( (IntValue lhs, IntValue rhs){return new IntValue(lhs.data + rhs.data);} ));
435fa085ec 2010-11-21        kinaba: 	ctx.set("-", ValueLayer, native( (IntValue lhs, IntValue rhs){return new IntValue(lhs.data - rhs.data);} ));
435fa085ec 2010-11-21        kinaba: 	ctx.set("*", ValueLayer, native( (IntValue lhs, IntValue rhs){return new IntValue(lhs.data * rhs.data);} ));
435fa085ec 2010-11-21        kinaba: 	ctx.set("/", ValueLayer, native( (IntValue lhs, IntValue rhs){return new IntValue(lhs.data / rhs.data);} ));
435fa085ec 2010-11-21        kinaba: 	ctx.set("%", ValueLayer, native( (IntValue lhs, IntValue rhs){return new IntValue(lhs.data % rhs.data);} ));
435fa085ec 2010-11-21        kinaba: 	ctx.set("||", ValueLayer, native( (IntValue lhs, IntValue rhs){return new IntValue(BigInt((lhs.data!=0) || (rhs.data!=0) ? 1:0));} ));
435fa085ec 2010-11-21        kinaba: 	ctx.set("&&", ValueLayer, native( (IntValue lhs, IntValue rhs){return new IntValue(BigInt((lhs.data!=0) && (rhs.data!=0) ? 1:0));} ));
435fa085ec 2010-11-21        kinaba: 	ctx.set("<", ValueLayer, native( (Value lhs, Value rhs){return new IntValue(BigInt(lhs < rhs ? 1: 0));} ));
435fa085ec 2010-11-21        kinaba: 	ctx.set(">", ValueLayer, native( (Value lhs, Value rhs){return new IntValue(BigInt(lhs > rhs ? 1: 0));} ));
435fa085ec 2010-11-21        kinaba: 	ctx.set("<=", ValueLayer, native( (Value lhs, Value rhs){return new IntValue(BigInt(lhs <= rhs ? 1: 0));} ));
435fa085ec 2010-11-21        kinaba: 	ctx.set(">=", ValueLayer, native( (Value lhs, Value rhs){return new IntValue(BigInt(lhs >= rhs ? 1: 0));} ));
435fa085ec 2010-11-21        kinaba: 	ctx.set("==", ValueLayer, native( (Value lhs, Value rhs){return new IntValue(BigInt(lhs == rhs ? 1: 0));} ));
435fa085ec 2010-11-21        kinaba: 	ctx.set("!=", ValueLayer, native( (Value lhs, Value rhs){return new IntValue(BigInt(lhs != rhs ? 1: 0));} ));
435fa085ec 2010-11-21        kinaba: 	ctx.set("print", ValueLayer, native( (Value a){
a5fe6233c1 2010-11-21        kinaba: 		writeln(a);
3995a5eb6a 2010-11-21        kinaba: 		return new IntValue(BigInt(0));
a5fe6233c1 2010-11-21        kinaba: 	}));
435fa085ec 2010-11-21        kinaba: 	ctx.set("if", ValueLayer, native( (IntValue x, FunValue ft, FunValue fe){
a5fe6233c1 2010-11-21        kinaba: 		auto toRun = (x.data==0 ? fe : ft);
3995a5eb6a 2010-11-21        kinaba: 		// [TODO] fill positional information
435fa085ec 2010-11-21        kinaba: 		return toRun.invoke(null, ValueLayer, toRun.definitionContext());
a5fe6233c1 2010-11-21        kinaba: //		return toRun.invoke(pos, lay, toRun.definitionContext());
5d4cb856d8 2010-11-07        kinaba: 	}));
435fa085ec 2010-11-21        kinaba: 	ctx.set("_isint", ValueLayer, native( (Value v){return new IntValue(BigInt(cast(IntValue)v is null ? 0 : 1));} ));
435fa085ec 2010-11-21        kinaba: 	ctx.set("_isstr", ValueLayer, native( (Value v){return new IntValue(BigInt(cast(StrValue)v is null ? 0 : 1));} ));
435fa085ec 2010-11-21        kinaba: 	ctx.set("_isfun", ValueLayer, native( (Value v){return new IntValue(BigInt(cast(FunValue)v is null ? 0 : 1));} ));
435fa085ec 2010-11-21        kinaba: 	ctx.set("_isundefined", ValueLayer, native( (Value v){return new IntValue(BigInt(cast(UndValue)v is null ? 0 : 1));} ));
435fa085ec 2010-11-21        kinaba: 	ctx.set("_istable", ValueLayer, native( (Value v){return new IntValue(BigInt(cast(Table)v is null ? 0 : 1));} ));
435fa085ec 2010-11-21        kinaba: 	ctx.set(".", ValueLayer, native( (Table t, StrValue s){
435fa085ec 2010-11-21        kinaba: 		return (t.has(s.data, ValueLayer) ? t.get(s.data, ValueLayer) : new UndValue);
435fa085ec 2010-11-21        kinaba: 	}) );
435fa085ec 2010-11-21        kinaba: 	ctx.set(".?", ValueLayer, native( (Table t, StrValue s){
435fa085ec 2010-11-21        kinaba: 		return new IntValue(BigInt(t.has(s.data, ValueLayer) ? 1 : 0));
515502e8d1 2010-11-20        kinaba: 	}) );
435fa085ec 2010-11-21        kinaba: 	ctx.set(".=", ValueLayer, native( (Table t, StrValue s, Value v){
515502e8d1 2010-11-20        kinaba: 		auto t2 = new Table(t, Table.Kind.NotPropagateSet);
435fa085ec 2010-11-21        kinaba: 		t2.set(s.data, ValueLayer, v);
515502e8d1 2010-11-20        kinaba: 		return t2;
515502e8d1 2010-11-20        kinaba: 	}) );
435fa085ec 2010-11-21        kinaba: 	ctx.set("{}", ValueLayer, 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: }
7de80acfb8 2010-11-09        kinaba: 
38fcc662be 2010-11-10        kinaba: /// Entry point of this module
38fcc662be 2010-11-10        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();
435fa085ec 2010-11-21        kinaba: 	return typeof(return)(eval(e, ctx, false, ValueLayer), ctx);
a5fe6233c1 2010-11-21        kinaba: }
a5fe6233c1 2010-11-21        kinaba: 
a5fe6233c1 2010-11-21        kinaba: Value invokeFunction(in LexPosition pos, Value _f, AST[] args, Table callerCtx, Layer lay, bool AlwaysMacro=false)
a5fe6233c1 2010-11-21        kinaba: {
a5fe6233c1 2010-11-21        kinaba: 	if(auto f = cast(FunValue)_f)
a5fe6233c1 2010-11-21        kinaba: 	{
a5fe6233c1 2010-11-21        kinaba: 		Table ctx = new Table(f.definitionContext(), Table.Kind.NotPropagateSet);
a5fe6233c1 2010-11-21        kinaba: 		foreach(i,p; f.params())
a5fe6233c1 2010-11-21        kinaba: 			if( p.layers.empty )
435fa085ec 2010-11-21        kinaba: 				if(lay==MacroLayer)
a5fe6233c1 2010-11-21        kinaba: 					ctx.set(p.name, lay, macroEval(args[i], callerCtx, AlwaysMacro));
a5fe6233c1 2010-11-21        kinaba: 				else
a5fe6233c1 2010-11-21        kinaba: 					ctx.set(p.name, lay, eval(args[i], callerCtx, true, lay));
a5fe6233c1 2010-11-21        kinaba: 			else
a5fe6233c1 2010-11-21        kinaba: 				foreach(argLay; p.layers)
435fa085ec 2010-11-21        kinaba: 					if(argLay==MacroLayer)
a5fe6233c1 2010-11-21        kinaba: 						ctx.set(p.name, argLay, macroEval(args[i], callerCtx, AlwaysMacro));
a5fe6233c1 2010-11-21        kinaba: 					else
a5fe6233c1 2010-11-21        kinaba: 						ctx.set(p.name, argLay, eval(args[i], callerCtx, true, argLay));
a5fe6233c1 2010-11-21        kinaba: 		return f.invoke(pos, lay, ctx);
a5fe6233c1 2010-11-21        kinaba: 	}
a5fe6233c1 2010-11-21        kinaba: 	throw genex!RuntimeException(pos, "tried to call non-function");
a5fe6233c1 2010-11-21        kinaba: }
a5fe6233c1 2010-11-21        kinaba: 
a5fe6233c1 2010-11-21        kinaba: Value lift(in LexPosition pos, Value v, Layer lay, Table callerCtx)
a5fe6233c1 2010-11-21        kinaba: {
f86026acb8 2010-11-21        kinaba: 	// functions are automatically lifterd
f86026acb8 2010-11-21        kinaba: 	if( cast(FunValue) v )
f86026acb8 2010-11-21        kinaba: 		return v;
f86026acb8 2010-11-21        kinaba: 
435fa085ec 2010-11-21        kinaba: 	// similar to invoke Function, but with only one argument bound to ValueLayer
435fa085ec 2010-11-21        kinaba: 	Value _f = callerCtx.get(lay, SystemLayer, pos);
a5fe6233c1 2010-11-21        kinaba: 	if(auto f = cast(FunValue)_f)
a5fe6233c1 2010-11-21        kinaba: 	{
a5fe6233c1 2010-11-21        kinaba: 		Table ctx = new Table(f.definitionContext(), Table.Kind.NotPropagateSet);
a5fe6233c1 2010-11-21        kinaba: 		auto ps = f.params();
a5fe6233c1 2010-11-21        kinaba: 		if( ps.length != 1 )
435fa085ec 2010-11-21        kinaba: 			throw genex!RuntimeException(pos, "lift function must take exactly one argument at "~ValueLayer~" layer");
435fa085ec 2010-11-21        kinaba: 		if( ps[0].layers.length==0 || ps[0].layers.length==1 && ps[0].layers[0]==ValueLayer )
a5fe6233c1 2010-11-21        kinaba: 		{
435fa085ec 2010-11-21        kinaba: 			ctx.set(ps[0].name, ValueLayer, v);
435fa085ec 2010-11-21        kinaba: 			return f.invoke(pos, ValueLayer, ctx);
a5fe6233c1 2010-11-21        kinaba: 		}
a5fe6233c1 2010-11-21        kinaba: 		else
435fa085ec 2010-11-21        kinaba: 			throw genex!RuntimeException(pos, "lift function must take exactly one argument at "~ValueLayer~" layer");
a5fe6233c1 2010-11-21        kinaba: 	}
a5fe6233c1 2010-11-21        kinaba: 	throw genex!RuntimeException(pos, "tried to call non-function");
38fcc662be 2010-11-10        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.
435fa085ec 2010-11-21        kinaba: /// lay is the layer ID for evaluation (standard value semantics uses ValueLayer).
8474ae68d9 2010-11-13        kinaba: 
1c01f44f52 2010-11-13        kinaba: Value eval(AST e, Table ctx, bool splitCtx, Layer lay)
1c01f44f52 2010-11-13        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);
435fa085ec 2010-11-21        kinaba: 			if( lay == ValueLayer )
c368edbcb1 2010-11-13        kinaba: 				return v;
a5fe6233c1 2010-11-21        kinaba: 			else
a5fe6233c1 2010-11-21        kinaba: 				return lift(e.pos,v,lay,ctx);
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);
435fa085ec 2010-11-21        kinaba: 			if( lay == ValueLayer )
c368edbcb1 2010-11-13        kinaba: 				return v;
8474ae68d9 2010-11-13        kinaba: 			else // rise
a5fe6233c1 2010-11-21        kinaba: 				return lift(e.pos,v,lay,ctx);
1c01f44f52 2010-11-13        kinaba: 		},
1c01f44f52 2010-11-13        kinaba: 		(VarExpression e)
1c01f44f52 2010-11-13        kinaba: 		{
435fa085ec 2010-11-21        kinaba: 			if( lay == ValueLayer )
3995a5eb6a 2010-11-21        kinaba: 				return ctx.get(e.name, lay, e.pos);
f86026acb8 2010-11-21        kinaba: 			if( ctx.has(e.name, lay, e.pos) )
3995a5eb6a 2010-11-21        kinaba: 				return ctx.get(e.name, lay, e.pos);
f86026acb8 2010-11-21        kinaba: 			else
3995a5eb6a 2010-11-21        kinaba: 				return lift(e.pos, ctx.get(e.name, ValueLayer, e.pos), lay, ctx);
1c01f44f52 2010-11-13        kinaba: 		},
3995a5eb6a 2010-11-21        kinaba: 		(LayExpression e)
1c01f44f52 2010-11-13        kinaba: 		{
3995a5eb6a 2010-11-21        kinaba: 			if( e.layer == MacroLayer )
8e3db9ef20 2010-11-20        kinaba: 				return macroEval(e.expr, ctx, false);
8e3db9ef20 2010-11-20        kinaba: 			else
3995a5eb6a 2010-11-21        kinaba: 				return eval(e.expr, ctx, true, e.layer);
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????
435fa085ec 2010-11-21        kinaba: 			// ctx.set(e.var, ValueLayer, 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);
3995a5eb6a 2010-11-21        kinaba: 			ctx.set(e.name, (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: 		{
a5fe6233c1 2010-11-21        kinaba: 			return invokeFunction(e.pos, eval(e.fun, ctx, true, lay), e.args, ctx, lay);
3f6f41b558 2010-11-20        kinaba: 		},
3f6f41b558 2010-11-20        kinaba: 		(FunLiteral e)
3f6f41b558 2010-11-20        kinaba: 		{
a5fe6233c1 2010-11-21        kinaba: 			return new UserDefinedFunValue(e, ctx);
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: {
435fa085ec 2010-11-21        kinaba: 	Layer theLayer = ValueLayer;
435fa085ec 2010-11-21        kinaba: 
3995a5eb6a 2010-11-21        kinaba: 	Table makeCons(Value a, Value d)
3995a5eb6a 2010-11-21        kinaba: 	{
3995a5eb6a 2010-11-21        kinaba: 		Table t = new Table;
3995a5eb6a 2010-11-21        kinaba: 		t.set("car", theLayer, a);
3995a5eb6a 2010-11-21        kinaba: 		t.set("cdr", theLayer, d);
3995a5eb6a 2010-11-21        kinaba: 		return t;
3995a5eb6a 2010-11-21        kinaba: 	}
3995a5eb6a 2010-11-21        kinaba: 
3f6f41b558 2010-11-20        kinaba: 	Table pos = new Table;
3995a5eb6a 2010-11-21        kinaba: 	if( e.pos !is null ) {
3995a5eb6a 2010-11-21        kinaba: 		pos.set("filename", theLayer, new StrValue(e.pos.filename));
3995a5eb6a 2010-11-21        kinaba: 		pos.set("lineno",   theLayer, new IntValue(BigInt(e.pos.lineno)));
3995a5eb6a 2010-11-21        kinaba: 		pos.set("column",   theLayer, new IntValue(BigInt(e.pos.column)));
3995a5eb6a 2010-11-21        kinaba: 	} else {
3995a5eb6a 2010-11-21        kinaba: 		pos.set("filename", theLayer, new StrValue("nullpos"));
3995a5eb6a 2010-11-21        kinaba: 		pos.set("lineno",   theLayer, new IntValue(BigInt(0)));
3995a5eb6a 2010-11-21        kinaba: 		pos.set("column",   theLayer, new IntValue(BigInt(0)));
3995a5eb6a 2010-11-21        kinaba: 	}
3995a5eb6a 2010-11-21        kinaba: 
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: 		{
f86026acb8 2010-11-21        kinaba: 			if( ctx.has(e.name, MacroLayer, e.pos) )
3995a5eb6a 2010-11-21        kinaba: 				return ctx.get(e.name, MacroLayer, e.pos);
f86026acb8 2010-11-21        kinaba: 			else {
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"));
3995a5eb6a 2010-11-21        kinaba: 				t.set("name", theLayer, new StrValue(e.name));
8e3db9ef20 2010-11-20        kinaba: 				return cast(Value)t;
8e3db9ef20 2010-11-20        kinaba: 			}
3f6f41b558 2010-11-20        kinaba: 		},
3995a5eb6a 2010-11-21        kinaba: 		(LayExpression 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;
3995a5eb6a 2010-11-21        kinaba: 				t.set("pos",   theLayer, pos);
3995a5eb6a 2010-11-21        kinaba: 				t.set("is",    theLayer, new StrValue("lay"));
3995a5eb6a 2010-11-21        kinaba: 				t.set("layer", theLayer, new StrValue(e.layer));
3995a5eb6a 2010-11-21        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: 			{
3995a5eb6a 2010-11-21        kinaba: 				if( e.layer == MacroLayer )
a5fe6233c1 2010-11-21        kinaba: 					return macroEval(e.expr, ctx, false);
a5fe6233c1 2010-11-21        kinaba: 				else
3995a5eb6a 2010-11-21        kinaba: 					return eval(e.expr, ctx, true, e.layer);
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"));
3995a5eb6a 2010-11-21        kinaba: 			t.set("name", theLayer, new StrValue(e.name));
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: 
a5fe6233c1 2010-11-21        kinaba: 			if( auto f = cast(FunValue)_f )
435fa085ec 2010-11-21        kinaba: 				return invokeFunction(e.pos, f, e.args, ctx, MacroLayer, AlwaysMacro);
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;
1c01f44f52 2010-11-13        kinaba: 			}
3995a5eb6a 2010-11-21        kinaba: 			t.set("args", theLayer, args);
8e3db9ef20 2010-11-20        kinaba: 			return cast(Value)t;
1c01f44f52 2010-11-13        kinaba: 		},
1c01f44f52 2010-11-13        kinaba: 		(FunLiteral e)
1c01f44f52 2010-11-13        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"));
3995a5eb6a 2010-11-21        kinaba: 			t.set("funbody",  theLayer, macroEval(e.funbody,ctx,AlwaysMacro));
3995a5eb6a 2010-11-21        kinaba: 			Table params = new Table;
3f6f41b558 2010-11-20        kinaba: 			foreach_reverse(p; e.params)
3f6f41b558 2010-11-20        kinaba: 			{
3995a5eb6a 2010-11-21        kinaba: 				Table lays = new Table;
3995a5eb6a 2010-11-21        kinaba: 				foreach_reverse(lay; p.layers)
3995a5eb6a 2010-11-21        kinaba: 					lays = makeCons(new StrValue(lay), lays);
3f6f41b558 2010-11-20        kinaba: 				Table kv = new Table;
3f6f41b558 2010-11-20        kinaba: 				kv.set("name", theLayer, new StrValue(p.name));
3995a5eb6a 2010-11-21        kinaba: 				kv.set("layers", theLayer, lays);
3995a5eb6a 2010-11-21        kinaba: 				Table cons = new Table;
3995a5eb6a 2010-11-21        kinaba: 				params = makeCons(kv, params);
3f6f41b558 2010-11-20        kinaba: 			}
3995a5eb6a 2010-11-21        kinaba: 			t.set("params", theLayer, params);
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: 	);
423f308350 2010-11-07        kinaba: }
423f308350 2010-11-07        kinaba: 
5e407d7cf8 2010-11-08        kinaba: unittest
5e407d7cf8 2010-11-08        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)) );
435fa085ec 2010-11-21        kinaba: 	assert_eq( r.ctx.get("x",ValueLayer), new IntValue(BigInt(21)) );
435fa085ec 2010-11-21        kinaba: 	assert_nothrow( r.ctx.get("x",ValueLayer) );
435fa085ec 2010-11-21        kinaba: 	assert_throw!RuntimeException( r.ctx.get("y",ValueLayer) );
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)) );
435fa085ec 2010-11-21        kinaba: 	assert_eq( r.ctx.get("x",ValueLayer), new IntValue(BigInt(21+21*21)) );
435fa085ec 2010-11-21        kinaba: 	assert_nothrow( r.ctx.get("x",ValueLayer) );
435fa085ec 2010-11-21        kinaba: 	assert_throw!RuntimeException( r.ctx.get("y",ValueLayer) );
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)) );
633e700889 2010-11-07        kinaba: }
633e700889 2010-11-07        kinaba: unittest
633e700889 2010-11-07        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: }
3995a5eb6a 2010-11-21        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)) );
435fa085ec 2010-11-21        kinaba: 	assert_eq( evalString(`@@s(x){x}; @s "+"=fun(x,y){@value(@s(x)-@s(y))};1+2`).val, new IntValue(BigInt(3)) );
435fa085ec 2010-11-21        kinaba: 	assert_eq( evalString(`@@s(x){x}; @s "+"=fun(x,y){@value(@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()`) );
423f308350 2010-11-07        kinaba: }
3995a5eb6a 2010-11-21        kinaba: */