Artifact 70846dfa5638c8e780de6dce0c7de94e2e0182d1
/**
* 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;
import std.stdio;
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)~"]");
}));
ctx.add("print", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
foreach(a; args)
write(a);
writeln("");
return new UndefinedValue;
}));
ctx.add("if", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
if( args.length != 3 )
throw new PolemyRuntimeException("if takes three arguments!! ["~to!string(pos)~"]");
if( auto x = cast(IntValue)args[0] )
if( auto ft = cast(FunValue)args[1] )
if( auto fe = cast(FunValue)args[2] )
return (x.data == 0 ? fe : ft).call(pos,[]);
throw new PolemyRuntimeException("type mismatch in if ["~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)) );
}
unittest
{
evalString(`print("Hello, world!");`);
evalString(`print(fun(){});`);
}
unittest
{
evalString(`var fac = fun(x){
1;
};
print(fac(3));`);
evalString(`var fac = fun(x){
if(x)
{ x*fac(x-1); }
else
{ 1; };
};
print(fac(10));`);
}