Artifact Content
Not logged in

Artifact cea762cacb86424f3fe949fbebf4b4d43121324d


/**
 * 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.lex : LexPosition;
import polemy.ast;
import polemy.parse;
import polemy.runtime;
import std.typecons;

Context createGlobalContext()
{
	auto ctx = new Context;
	ctx.add("+", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
		if( args.length != 2 )
			throw new PolemyRuntimeException("+ takes two arguments!! ["~to!string(pos)~"]");
		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 ["~to!string(pos)~"]");
	}));
	ctx.add("-", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
		if( args.length != 2 )
			throw new PolemyRuntimeException("- takes two arguments!! ["~to!string(pos)~"]");
		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 ["~to!string(pos)~"]");
	}));
	ctx.add("*", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
		if( args.length != 2 )
			throw new PolemyRuntimeException("* takes two arguments!! ["~to!string(pos)~"]");
		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 ["~to!string(pos)~"]");
	}));
	ctx.add("/", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
		if( args.length != 2 )
			throw new PolemyRuntimeException("/ takes two arguments!! ["~to!string(pos)~"]");
		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 ["~to!string(pos)~"]");
	}));
	return ctx;
}

Tuple!(Value,"val",Context,"ctx") evalString(T...)(T params)
{
	return eval( parserFromString(params).parseProgram() );
}

Tuple!(Value,"val",Context,"ctx") evalFile(T...)(T params)
{
	return eval( parserFromFile(params).parseProgram() );
}

Tuple!(Value,"val",Context,"ctx") eval(Program prog)
{
	Context ctx = createGlobalContext();
	return typeof(return)(eval(prog, ctx), ctx);
}

Value eval(Program prog, Context ctx)
{
	Value v = new UndefinedValue;
	foreach(s; prog)
		v = eval(s, ctx);
	return v;
}

Value eval(Statement _s, Context ctx)
{
	if( auto s = cast(DeclStatement)_s )
	{
		auto v = eval(s.expr, ctx);
		ctx.add(s.var, v);
		return v;
	}
	else
	if( auto s = cast(ExprStatement)_s )
	{
		return eval(s.expr, 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(e.pos, args);
		} else
			throw new PolemyRuntimeException(sprintf!"Non-funcion is applied at [%s]"(e.pos));
	}
	else
	if( auto e = cast(FunLiteralExpression)_e )
	{
		return new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
			if( e.params.length != args.length )
				throw new PolemyRuntimeException(sprintf!"Argument Number Mismatch (%d required but %d given) at [%s]"
					(e.params.length, args.length, e.pos));
			Context ctxNeo = new Context(ctx);
			foreach(i,p; e.params)
				ctxNeo.add(p, args[i]);
			return eval(e.funbody, ctxNeo);
		});
	}
	throw new PolemyRuntimeException(sprintf!"Unknown Kind of Expression %s at [%s]"(typeid(_e), _e.pos));
}

import std.stdio;
unittest
{
	auto r = evalString(`var x = 21; x = x + x*x;`);
	assert( r.val == new IntValue(BigInt(21+21*21)) );
	assert( r.ctx["x"] == new IntValue(BigInt(21+21*21)) );
	assert( !collectException(r.ctx["x"]) );
	assert( collectException(r.ctx["y"]) );
}
unittest
{
	assert( collectException(evalString(`var x = 21; x = x + x*y;`)) );
	assert( collectException(evalString(`x=1;`)) );
}
unittest
{
	auto r = evalString(`var x = fun(a){1+a;}(2);`);
	assert( r.ctx["x"] == new IntValue(BigInt(3)) );
	assert( r.val == new IntValue(BigInt(3)) );
}
unittest
{
	auto r = evalString(`var x = 1; var f = fun(){x=x+1;}; f(); f(); f();`);
	assert( r.ctx["x"] == new IntValue(BigInt(4)) );
	assert( r.val == new IntValue(BigInt(4)) );
}