File Annotation
Not logged in
4198578702 2010-11-07        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;
423f308350 2010-11-07        kinaba: import polemy.ast;
423f308350 2010-11-07        kinaba: import polemy.runtime;
423f308350 2010-11-07        kinaba: 
5d4cb856d8 2010-11-07        kinaba: Context createGlobalContext()
5d4cb856d8 2010-11-07        kinaba: {
5d4cb856d8 2010-11-07        kinaba: 	auto ctx = new Context;
5d4cb856d8 2010-11-07        kinaba: 	ctx.add("+", new PrimitiveFunction(delegate Value(Value[] args){
5d4cb856d8 2010-11-07        kinaba: 		if( args.length != 2 )
5d4cb856d8 2010-11-07        kinaba: 			throw new PolemyRuntimeException("+ takes two arguments!!"); // TODO improve this message
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);
5d4cb856d8 2010-11-07        kinaba: 		throw new PolemyRuntimeException("cannot add non-integers"); // TODO improve this message
5d4cb856d8 2010-11-07        kinaba: 	}));
5d4cb856d8 2010-11-07        kinaba: 	ctx.add("-", new PrimitiveFunction(delegate Value(Value[] args){
5d4cb856d8 2010-11-07        kinaba: 		if( args.length != 2 )
5d4cb856d8 2010-11-07        kinaba: 			throw new PolemyRuntimeException("- takes two arguments!!"); // TODO improve this message
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);
5d4cb856d8 2010-11-07        kinaba: 		throw new PolemyRuntimeException("cannot add non-integers"); // TODO improve this message
5d4cb856d8 2010-11-07        kinaba: 	}));
5d4cb856d8 2010-11-07        kinaba: 	ctx.add("*", new PrimitiveFunction(delegate Value(Value[] args){
5d4cb856d8 2010-11-07        kinaba: 		if( args.length != 2 )
5d4cb856d8 2010-11-07        kinaba: 			throw new PolemyRuntimeException("* takes two arguments!!"); // TODO improve this message
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);
5d4cb856d8 2010-11-07        kinaba: 		throw new PolemyRuntimeException("cannot add non-integers"); // TODO improve this message
5d4cb856d8 2010-11-07        kinaba: 	}));
5d4cb856d8 2010-11-07        kinaba: 	ctx.add("/", new PrimitiveFunction(delegate Value(Value[] args){
5d4cb856d8 2010-11-07        kinaba: 		if( args.length != 2 )
5d4cb856d8 2010-11-07        kinaba: 			throw new PolemyRuntimeException("/ takes two arguments!!"); // TODO improve this message
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);
5d4cb856d8 2010-11-07        kinaba: 		throw new PolemyRuntimeException("cannot add non-integers"); // TODO improve this message
5d4cb856d8 2010-11-07        kinaba: 	}));
5d4cb856d8 2010-11-07        kinaba: 	return ctx;
5d4cb856d8 2010-11-07        kinaba: }
5d4cb856d8 2010-11-07        kinaba: 
423f308350 2010-11-07        kinaba: Context eval(Program prog)
423f308350 2010-11-07        kinaba: {
5d4cb856d8 2010-11-07        kinaba: 	return eval(prog, createGlobalContext());
423f308350 2010-11-07        kinaba: }
423f308350 2010-11-07        kinaba: 
423f308350 2010-11-07        kinaba: Context eval(Program prog, Context ctx)
423f308350 2010-11-07        kinaba: {
423f308350 2010-11-07        kinaba: 	foreach(s; prog)
423f308350 2010-11-07        kinaba: 		ctx = eval(s, ctx);
423f308350 2010-11-07        kinaba: 	return ctx;
423f308350 2010-11-07        kinaba: }
423f308350 2010-11-07        kinaba: 
423f308350 2010-11-07        kinaba: Context eval(Statement _s, Context ctx)
423f308350 2010-11-07        kinaba: {
423f308350 2010-11-07        kinaba: 	if( auto s = cast(DeclStatement)_s )
423f308350 2010-11-07        kinaba: 	{
423f308350 2010-11-07        kinaba: 		auto v = eval(s.expr, ctx);
423f308350 2010-11-07        kinaba: 		ctx.add(s.var, v);
423f308350 2010-11-07        kinaba: 		return ctx;
423f308350 2010-11-07        kinaba: 	}
423f308350 2010-11-07        kinaba: 	else
423f308350 2010-11-07        kinaba: 	if( auto s = cast(ExprStatement)_s )
423f308350 2010-11-07        kinaba: 	{
423f308350 2010-11-07        kinaba: 		eval(s.expr, ctx);
423f308350 2010-11-07        kinaba: 		return ctx;
423f308350 2010-11-07        kinaba: 	}
423f308350 2010-11-07        kinaba: 	throw new PolemyRuntimeException(sprintf!"Unknown Kind of Statement %s at [%s]"(typeid(_s), _s.pos));
423f308350 2010-11-07        kinaba: }
423f308350 2010-11-07        kinaba: 
423f308350 2010-11-07        kinaba: Value eval(Expression _e, Context ctx)
423f308350 2010-11-07        kinaba: {
423f308350 2010-11-07        kinaba: 	if( auto e = cast(StrLiteralExpression)_e )
423f308350 2010-11-07        kinaba: 	{
423f308350 2010-11-07        kinaba: 		return new StrValue(e.data);
423f308350 2010-11-07        kinaba: 	}
423f308350 2010-11-07        kinaba: 	else
423f308350 2010-11-07        kinaba: 	if( auto e = cast(IntLiteralExpression)_e )
423f308350 2010-11-07        kinaba: 	{
423f308350 2010-11-07        kinaba: 		return new IntValue(e.data);
423f308350 2010-11-07        kinaba: 	}
423f308350 2010-11-07        kinaba: 	else
423f308350 2010-11-07        kinaba: 	if( auto e = cast(VarExpression)_e )
423f308350 2010-11-07        kinaba: 	{
423f308350 2010-11-07        kinaba: 		return ctx[e.var];
423f308350 2010-11-07        kinaba: 	}
423f308350 2010-11-07        kinaba: 	else
5d4cb856d8 2010-11-07        kinaba: 	if( auto e = cast(AssignExpression)_e )
423f308350 2010-11-07        kinaba: 	{
5d4cb856d8 2010-11-07        kinaba: 		if( auto ev = cast(VarExpression)e.lhs )
423f308350 2010-11-07        kinaba: 		{
5d4cb856d8 2010-11-07        kinaba: 			Value r = eval(e.rhs, ctx);
5d4cb856d8 2010-11-07        kinaba: 			ctx[ev.var] = r;
5d4cb856d8 2010-11-07        kinaba: 			return r;
423f308350 2010-11-07        kinaba: 		}
5d4cb856d8 2010-11-07        kinaba: 		throw new PolemyRuntimeException(sprintf!"Lhs of assignment must be a variable: %s"(e.pos));
5d4cb856d8 2010-11-07        kinaba: 	}
5d4cb856d8 2010-11-07        kinaba: 	else
5d4cb856d8 2010-11-07        kinaba: 	if( auto e = cast(FuncallExpression)_e )
5d4cb856d8 2010-11-07        kinaba: 	{
5d4cb856d8 2010-11-07        kinaba: 		Value _f = eval(e.fun, ctx);
5d4cb856d8 2010-11-07        kinaba: 		if( auto f = cast(FunValue)_f ) {
5d4cb856d8 2010-11-07        kinaba: 			Value[] args;
5d4cb856d8 2010-11-07        kinaba: 			foreach(a; e.args)
5d4cb856d8 2010-11-07        kinaba: 				args ~= eval(a, ctx);
5d4cb856d8 2010-11-07        kinaba: 			return f.call(args);
5d4cb856d8 2010-11-07        kinaba: 		} else
5d4cb856d8 2010-11-07        kinaba: 			throw new PolemyRuntimeException(sprintf!"Non-funcion is applied at [%s]"(e.pos));
423f308350 2010-11-07        kinaba: 	}
423f308350 2010-11-07        kinaba: 	throw new PolemyRuntimeException(sprintf!"Unknown Kind of Expression %s at [%s]"(typeid(_e), _e.pos));
423f308350 2010-11-07        kinaba: }
423f308350 2010-11-07        kinaba: 
423f308350 2010-11-07        kinaba: 
423f308350 2010-11-07        kinaba: version(unittest) import polemy.parse;
423f308350 2010-11-07        kinaba: version(unittest) import std.stdio;
423f308350 2010-11-07        kinaba: version(unittest) import std.exception;
423f308350 2010-11-07        kinaba: unittest
423f308350 2010-11-07        kinaba: {
423f308350 2010-11-07        kinaba: 	auto parser = parserFromString(`var x = 21; x = x + x*x;`);
423f308350 2010-11-07        kinaba: 	auto prog = parser.parseProgram();
423f308350 2010-11-07        kinaba: 	auto ctx = eval(prog);
423f308350 2010-11-07        kinaba: 	assert( ctx["x"] == new IntValue(BigInt(21+21*21)) );
423f308350 2010-11-07        kinaba: 	assert( !collectException(ctx["x"]) );
423f308350 2010-11-07        kinaba: 	assert( collectException(ctx["y"]) );
423f308350 2010-11-07        kinaba: }
423f308350 2010-11-07        kinaba: unittest
423f308350 2010-11-07        kinaba: {
423f308350 2010-11-07        kinaba: 	auto parser = parserFromString(`var x = 21; x = x + x*y;`);
423f308350 2010-11-07        kinaba: 	auto prog = parser.parseProgram();
423f308350 2010-11-07        kinaba: 	assert( collectException(eval(prog)) );
423f308350 2010-11-07        kinaba: }
423f308350 2010-11-07        kinaba: unittest
423f308350 2010-11-07        kinaba: {
423f308350 2010-11-07        kinaba: 	auto parser = parserFromString(`var x = 21; y = x + x*x;`);
423f308350 2010-11-07        kinaba: 	auto prog = parser.parseProgram();
423f308350 2010-11-07        kinaba: 	assert( collectException(eval(prog)) );
423f308350 2010-11-07        kinaba: }