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;
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;
423f308350 2010-11-07        kinaba: import polemy.runtime;
0569f7b8c2 2010-11-07        kinaba: import std.typecons;
820e7198cc 2010-11-07        kinaba: import std.stdio;
423f308350 2010-11-07        kinaba: 
5d4cb856d8 2010-11-07        kinaba: Context createGlobalContext()
423f308350 2010-11-07        kinaba: {
5d4cb856d8 2010-11-07        kinaba: 	auto ctx = new Context;
0569f7b8c2 2010-11-07        kinaba: 	ctx.add("+", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
5d4cb856d8 2010-11-07        kinaba: 		if( args.length != 2 )
0569f7b8c2 2010-11-07        kinaba: 			throw new PolemyRuntimeException("+ takes two arguments!! ["~to!string(pos)~"]");
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);
0569f7b8c2 2010-11-07        kinaba: 		throw new PolemyRuntimeException("cannot add non-integers ["~to!string(pos)~"]");
5d4cb856d8 2010-11-07        kinaba: 	}));
0569f7b8c2 2010-11-07        kinaba: 	ctx.add("-", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
5d4cb856d8 2010-11-07        kinaba: 		if( args.length != 2 )
0569f7b8c2 2010-11-07        kinaba: 			throw new PolemyRuntimeException("- takes two arguments!! ["~to!string(pos)~"]");
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);
0569f7b8c2 2010-11-07        kinaba: 		throw new PolemyRuntimeException("cannot add non-integers ["~to!string(pos)~"]");
5d4cb856d8 2010-11-07        kinaba: 	}));
0569f7b8c2 2010-11-07        kinaba: 	ctx.add("*", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
5d4cb856d8 2010-11-07        kinaba: 		if( args.length != 2 )
0569f7b8c2 2010-11-07        kinaba: 			throw new PolemyRuntimeException("* takes two arguments!! ["~to!string(pos)~"]");
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);
0569f7b8c2 2010-11-07        kinaba: 		throw new PolemyRuntimeException("cannot add non-integers ["~to!string(pos)~"]");
5d4cb856d8 2010-11-07        kinaba: 	}));
0569f7b8c2 2010-11-07        kinaba: 	ctx.add("/", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
5d4cb856d8 2010-11-07        kinaba: 		if( args.length != 2 )
0569f7b8c2 2010-11-07        kinaba: 			throw new PolemyRuntimeException("/ takes two arguments!! ["~to!string(pos)~"]");
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);
0569f7b8c2 2010-11-07        kinaba: 		throw new PolemyRuntimeException("cannot add non-integers ["~to!string(pos)~"]");
820e7198cc 2010-11-07        kinaba: 	}));
820e7198cc 2010-11-07        kinaba: 	ctx.add("print", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
820e7198cc 2010-11-07        kinaba: 		foreach(a; args)
820e7198cc 2010-11-07        kinaba: 			write(a);
820e7198cc 2010-11-07        kinaba: 		writeln("");
820e7198cc 2010-11-07        kinaba: 		return new UndefinedValue;
820e7198cc 2010-11-07        kinaba: 	}));
633e700889 2010-11-07        kinaba: 	ctx.add("if", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
633e700889 2010-11-07        kinaba: 		if( args.length != 3 )
633e700889 2010-11-07        kinaba: 			throw new PolemyRuntimeException("if takes three arguments!! ["~to!string(pos)~"]");
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] )
633e700889 2010-11-07        kinaba: 			return (x.data == 0 ? fe : ft).call(pos,[]);
633e700889 2010-11-07        kinaba: 		throw new PolemyRuntimeException("type mismatch in if ["~to!string(pos)~"]");
633e700889 2010-11-07        kinaba: 	}));
423f308350 2010-11-07        kinaba: 	return ctx;
423f308350 2010-11-07        kinaba: }
423f308350 2010-11-07        kinaba: 
0569f7b8c2 2010-11-07        kinaba: Tuple!(Value,"val",Context,"ctx") evalString(T...)(T params)
3f5dc76a75 2010-11-07        kinaba: {
3f5dc76a75 2010-11-07        kinaba: 	return eval( parserFromString(params).parseProgram() );
3f5dc76a75 2010-11-07        kinaba: }
3f5dc76a75 2010-11-07        kinaba: 
0569f7b8c2 2010-11-07        kinaba: Tuple!(Value,"val",Context,"ctx") evalFile(T...)(T params)
3f5dc76a75 2010-11-07        kinaba: {
3f5dc76a75 2010-11-07        kinaba: 	return eval( parserFromFile(params).parseProgram() );
3f5dc76a75 2010-11-07        kinaba: }
3f5dc76a75 2010-11-07        kinaba: 
0569f7b8c2 2010-11-07        kinaba: Tuple!(Value,"val",Context,"ctx") eval(Program prog)
5d4cb856d8 2010-11-07        kinaba: {
0569f7b8c2 2010-11-07        kinaba: 	Context ctx = createGlobalContext();
0569f7b8c2 2010-11-07        kinaba: 	return typeof(return)(eval(prog, ctx), ctx);
5d4cb856d8 2010-11-07        kinaba: }
5d4cb856d8 2010-11-07        kinaba: 
0569f7b8c2 2010-11-07        kinaba: Value eval(Program prog, Context ctx)
5d4cb856d8 2010-11-07        kinaba: {
0569f7b8c2 2010-11-07        kinaba: 	Value v = new UndefinedValue;
5d4cb856d8 2010-11-07        kinaba: 	foreach(s; prog)
0569f7b8c2 2010-11-07        kinaba: 		v = eval(s, ctx);
0569f7b8c2 2010-11-07        kinaba: 	return v;
5d4cb856d8 2010-11-07        kinaba: }
5d4cb856d8 2010-11-07        kinaba: 
0569f7b8c2 2010-11-07        kinaba: Value 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);
0569f7b8c2 2010-11-07        kinaba: 		return v;
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: 	{
0569f7b8c2 2010-11-07        kinaba: 		return eval(s.expr, 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);
0569f7b8c2 2010-11-07        kinaba: 			return f.call(e.pos, args);
5d4cb856d8 2010-11-07        kinaba: 		} else
5d4cb856d8 2010-11-07        kinaba: 			throw new PolemyRuntimeException(sprintf!"Non-funcion is applied at [%s]"(e.pos));
0569f7b8c2 2010-11-07        kinaba: 	}
0569f7b8c2 2010-11-07        kinaba: 	else
0569f7b8c2 2010-11-07        kinaba: 	if( auto e = cast(FunLiteralExpression)_e )
0569f7b8c2 2010-11-07        kinaba: 	{
0569f7b8c2 2010-11-07        kinaba: 		return new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
0569f7b8c2 2010-11-07        kinaba: 			if( e.params.length != args.length )
0569f7b8c2 2010-11-07        kinaba: 				throw new PolemyRuntimeException(sprintf!"Argument Number Mismatch (%d required but %d given) at [%s]"
0569f7b8c2 2010-11-07        kinaba: 					(e.params.length, args.length, e.pos));
0569f7b8c2 2010-11-07        kinaba: 			Context ctxNeo = new Context(ctx);
0569f7b8c2 2010-11-07        kinaba: 			foreach(i,p; e.params)
0569f7b8c2 2010-11-07        kinaba: 				ctxNeo.add(p, args[i]);
0569f7b8c2 2010-11-07        kinaba: 			return eval(e.funbody, ctxNeo);
0569f7b8c2 2010-11-07        kinaba: 		});
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: 
0569f7b8c2 2010-11-07        kinaba: import std.stdio;
3f5dc76a75 2010-11-07        kinaba: unittest
3f5dc76a75 2010-11-07        kinaba: {
0569f7b8c2 2010-11-07        kinaba: 	auto r = evalString(`var x = 21; x = x + x*x;`);
0569f7b8c2 2010-11-07        kinaba: 	assert( r.val == new IntValue(BigInt(21+21*21)) );
0569f7b8c2 2010-11-07        kinaba: 	assert( r.ctx["x"] == new IntValue(BigInt(21+21*21)) );
0569f7b8c2 2010-11-07        kinaba: 	assert( !collectException(r.ctx["x"]) );
0569f7b8c2 2010-11-07        kinaba: 	assert( collectException(r.ctx["y"]) );
3f5dc76a75 2010-11-07        kinaba: }
3f5dc76a75 2010-11-07        kinaba: unittest
3f5dc76a75 2010-11-07        kinaba: {
3f5dc76a75 2010-11-07        kinaba: 	assert( collectException(evalString(`var x = 21; x = x + x*y;`)) );
0569f7b8c2 2010-11-07        kinaba: 	assert( collectException(evalString(`x=1;`)) );
0569f7b8c2 2010-11-07        kinaba: }
0569f7b8c2 2010-11-07        kinaba: unittest
0569f7b8c2 2010-11-07        kinaba: {
0569f7b8c2 2010-11-07        kinaba: 	auto r = evalString(`var x = fun(a){1+a;}(2);`);
0569f7b8c2 2010-11-07        kinaba: 	assert( r.ctx["x"] == new IntValue(BigInt(3)) );
0569f7b8c2 2010-11-07        kinaba: 	assert( r.val == new IntValue(BigInt(3)) );
0569f7b8c2 2010-11-07        kinaba: }
0569f7b8c2 2010-11-07        kinaba: unittest
0569f7b8c2 2010-11-07        kinaba: {
0569f7b8c2 2010-11-07        kinaba: 	auto r = evalString(`var x = 1; var f = fun(){x=x+1;}; f(); f(); f();`);
0569f7b8c2 2010-11-07        kinaba: 	assert( r.ctx["x"] == new IntValue(BigInt(4)) );
0569f7b8c2 2010-11-07        kinaba: 	assert( r.val == new IntValue(BigInt(4)) );
820e7198cc 2010-11-07        kinaba: }
820e7198cc 2010-11-07        kinaba: unittest
820e7198cc 2010-11-07        kinaba: {
820e7198cc 2010-11-07        kinaba: 	evalString(`print("Hello, world!");`);
820e7198cc 2010-11-07        kinaba: 	evalString(`print(fun(){});`);
633e700889 2010-11-07        kinaba: }
633e700889 2010-11-07        kinaba: unittest
633e700889 2010-11-07        kinaba: {
633e700889 2010-11-07        kinaba: 	evalString(`var fac = fun(x){
633e700889 2010-11-07        kinaba: 		1;
633e700889 2010-11-07        kinaba: 	};
633e700889 2010-11-07        kinaba: 	print(fac(3));`);
633e700889 2010-11-07        kinaba: 	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: 	};
633e700889 2010-11-07        kinaba: 	print(fac(10));`);
423f308350 2010-11-07        kinaba: }