Artifact Content
Not logged in

Artifact c5ee9ab6830c78bdbbebc895ad0d144c57c13217


/**
 * Authors: k.inaba
 * License: NYSL 0.9982 http://www.kmonos.net/nysl/
 *
 * Evaluator for Polemy programming language.
 */
module polemy.eval;
import polemy._common;
import polemy.ast;
import polemy.parse;
import polemy.runtime;

Context createGlobalContext()
{
	auto ctx = new Context;
	ctx.add("+", new PrimitiveFunction(delegate Value(Value[] args){
		if( args.length != 2 )
			throw new PolemyRuntimeException("+ takes two arguments!!"); // TODO improve this message
		if( auto x = cast(IntValue)args[0] )
			if( auto y = cast(IntValue)args[1] )
				return new IntValue(x.data+y.data);
		throw new PolemyRuntimeException("cannot add non-integers"); // TODO improve this message
	}));
	ctx.add("-", new PrimitiveFunction(delegate Value(Value[] args){
		if( args.length != 2 )
			throw new PolemyRuntimeException("- takes two arguments!!"); // TODO improve this message
		if( auto x = cast(IntValue)args[0] )
			if( auto y = cast(IntValue)args[1] )
				return new IntValue(x.data-y.data);
		throw new PolemyRuntimeException("cannot add non-integers"); // TODO improve this message
	}));
	ctx.add("*", new PrimitiveFunction(delegate Value(Value[] args){
		if( args.length != 2 )
			throw new PolemyRuntimeException("* takes two arguments!!"); // TODO improve this message
		if( auto x = cast(IntValue)args[0] )
			if( auto y = cast(IntValue)args[1] )
				return new IntValue(x.data*y.data);
		throw new PolemyRuntimeException("cannot add non-integers"); // TODO improve this message
	}));
	ctx.add("/", new PrimitiveFunction(delegate Value(Value[] args){
		if( args.length != 2 )
			throw new PolemyRuntimeException("/ takes two arguments!!"); // TODO improve this message
		if( auto x = cast(IntValue)args[0] )
			if( auto y = cast(IntValue)args[1] )
				return new IntValue(x.data/y.data);
		throw new PolemyRuntimeException("cannot add non-integers"); // TODO improve this message
	}));
	return ctx;
}

Context evalString(T...)(T params)
{
	return eval( parserFromString(params).parseProgram() );
}

Context evalFile(T...)(T params)
{
	return eval( parserFromFile(params).parseProgram() );
}

Context eval(Program prog)
{
	return eval(prog, createGlobalContext());
}

Context eval(Program prog, Context ctx)
{
	foreach(s; prog)
		ctx = eval(s, ctx);
	return ctx;
}

Context eval(Statement _s, Context ctx)
{
	if( auto s = cast(DeclStatement)_s )
	{
		auto v = eval(s.expr, ctx);
		ctx.add(s.var, v);
		return ctx;
	}
	else
	if( auto s = cast(ExprStatement)_s )
	{
		eval(s.expr, ctx);
		return ctx;
	}
	throw new PolemyRuntimeException(sprintf!"Unknown Kind of Statement %s at [%s]"(typeid(_s), _s.pos));
}

Value eval(Expression _e, Context ctx)
{
	if( auto e = cast(StrLiteralExpression)_e )
	{
		return new StrValue(e.data);
	}
	else
	if( auto e = cast(IntLiteralExpression)_e )
	{
		return new IntValue(e.data);
	}
	else
	if( auto e = cast(VarExpression)_e )
	{
		return ctx[e.var];
	}
	else
	if( auto e = cast(AssignExpression)_e )
	{
		if( auto ev = cast(VarExpression)e.lhs )
		{
			Value r = eval(e.rhs, ctx);
			ctx[ev.var] = r;
			return r;
		}
		throw new PolemyRuntimeException(sprintf!"Lhs of assignment must be a variable: %s"(e.pos));
	}
	else
	if( auto e = cast(FuncallExpression)_e )
	{
		Value _f = eval(e.fun, ctx);
		if( auto f = cast(FunValue)_f ) {
			Value[] args;
			foreach(a; e.args)
				args ~= eval(a, ctx);
			return f.call(args);
		} else
			throw new PolemyRuntimeException(sprintf!"Non-funcion is applied at [%s]"(e.pos));
	}
	throw new PolemyRuntimeException(sprintf!"Unknown Kind of Expression %s at [%s]"(typeid(_e), _e.pos));
}


version(unittest) import polemy.parse;
version(unittest) import std.stdio;
version(unittest) import std.exception;
unittest
{
	auto ctx = evalString(`var x = 21; x = x + x*x;`);
	assert( ctx["x"] == new IntValue(BigInt(21+21*21)) );
	assert( !collectException(ctx["x"]) );
	assert( collectException(ctx["y"]) );
}
unittest
{
	assert( collectException(evalString(`var x = 21; x = x + x*y;`)) );
}
unittest
{
	assert( collectException(evalString(`var x = 21; y = x + x*x;`)) );
}