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()
0569f7b8c2 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));} ));
a5fe6233c1 2010-11-21        kinaba: 	ctx.set("print", "@v", native( (Value a){
a5fe6233c1 2010-11-21        kinaba: 		writeln(a);
8474ae68d9 2010-11-13        kinaba: 		return new IntValue(BigInt(178));
5d4cb856d8 2010-11-07        kinaba: 	}));
a5fe6233c1 2010-11-21        kinaba: 	ctx.set("if", "@v", native( (IntValue x, FunValue ft, FunValue fe){
a5fe6233c1 2010-11-21        kinaba: 		auto toRun = (x.data==0 ? fe : ft);
a5fe6233c1 2010-11-21        kinaba: 		return toRun.invoke(null, "@v", toRun.definitionContext());
a5fe6233c1 2010-11-21        kinaba: //		return toRun.invoke(pos, lay, toRun.definitionContext());
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)
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: 
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 )
a5fe6233c1 2010-11-21        kinaba: 				if(lay=="@macro")
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)
a5fe6233c1 2010-11-21        kinaba: 					if(argLay=="@macro")
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: {
a5fe6233c1 2010-11-21        kinaba: 	// similar to invoke Function, but with only one argument bound to @v
a5fe6233c1 2010-11-21        kinaba: 	Value _f = callerCtx.get(lay, "(system)", 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 )
a5fe6233c1 2010-11-21        kinaba: 			throw genex!RuntimeException(pos, "lift function must take exactly one argument at @v layer");
a5fe6233c1 2010-11-21        kinaba: 		if( ps[0].layers.length==0 || ps[0].layers.length==1 && ps[0].layers[0]=="@v" )
a5fe6233c1 2010-11-21        kinaba: 		{
a5fe6233c1 2010-11-21        kinaba: 			ctx.set(ps[0].name, "@v", v);
a5fe6233c1 2010-11-21        kinaba: 			return f.invoke(pos, "@v", ctx);
a5fe6233c1 2010-11-21        kinaba: 		}
a5fe6233c1 2010-11-21        kinaba: 		else
a5fe6233c1 2010-11-21        kinaba: 			throw genex!RuntimeException(pos, "lift function must take exactly one argument at @v layer");
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: 
1c01f44f52 2010-11-13        kinaba: /// Entry point of this module
1c01f44f52 2010-11-13        kinaba: /// If splitCtx = true, then inner variable declaration do not overwrite ctx.
1c01f44f52 2010-11-13        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)
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);
c368edbcb1 2010-11-13        kinaba: 			if( lay == "@v" )
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);
c368edbcb1 2010-11-13        kinaba: 			if( lay == "@v" )
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: 		{
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...
a5fe6233c1 2010-11-21        kinaba: 				return lift(e.pos, ctx.get(e.var, "@v", e.pos), lay, ctx);
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: 		{
a5fe6233c1 2010-11-21        kinaba: 			return invokeFunction(e.pos, eval(e.fun, ctx, true, lay), e.args, ctx, lay);
8474ae68d9 2010-11-13        kinaba: 		},
8474ae68d9 2010-11-13        kinaba: 		(FunLiteral e)
8474ae68d9 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")
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: {
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: 			{
a5fe6233c1 2010-11-21        kinaba: 				if( e.lay == "@macro" )
a5fe6233c1 2010-11-21        kinaba: 					return macroEval(e.expr, ctx, false);
a5fe6233c1 2010-11-21        kinaba: 				else
a5fe6233c1 2010-11-21        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: 
a5fe6233c1 2010-11-21        kinaba: 			if( auto f = cast(FunValue)_f )
a5fe6233c1 2010-11-21        kinaba: 				return invokeFunction(e.pos, f, e.args, ctx, "@macro", 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;
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: 	);
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)) );
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()`) );
423f308350 2010-11-07        kinaba: }
a5fe6233c1 2010-11-21        kinaba: