/**
 * 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.failure;
import polemy.ast;
import polemy.parse;
import polemy.value;
import polemy.layer;
import polemy.value;
import polemy.valueconv;
import std.signals;

/// Objects for maitaining global environment and evaluation of expression on it
class Evaluator
{
public:
	/// Initialize evaluator with empty context
	this() { theContext = new Table; }

	/// Evaluate the AST
	Value evalAST(AST e)
	{
		AST[void*] mandeCache;
		return macroAndEval(e, ValueLayer, theContext, OverwriteCtx, mandeCache)[0];
	}

	/// Evaluate the string
	Value evalString(S,T...)(S str, T fn_ln_cn)
	{
		return evalAST(parseString(str,fn_ln_cn));
	}

	/// Evaluate the file
	Value evalFile(S,T...)(S filename, T ln_cn)
	{
		return evalAST(parseFile(filename,ln_cn));
	}

	/// Get the global context
	Table globalContext()
	{
		return theContext;
	}

private:
	Table theContext;

	enum : bool { CascadeCtx=false, OverwriteCtx=true };

	Value eval( AST e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx )
	{
		// dynamic-overload-resolution-pattern: modify here
		enum funName = "eval";
		alias TypeTuple!(e,lay,ctx,overwriteCtx) params;

		// dynamic-overload-resolution-pattern: dispatch
		alias typeof(__traits(getOverloads, this, funName)) ovTypes;
		alias staticMap!(firstParam, ovTypes)              fstTypes;
		alias DerivedToFront!(fstTypes)             fstTypes_sorted;
		foreach(i, T; fstTypes_sorted)
			static if( is(T == typeof(params[0])) ) {} else if( auto _x = cast(T)params[0] )
				return __traits(getOverloads, this, funName)[i](_x, params[1..$]);

		// dynamic-overload-resolution-pattern: default behavior
		assert(false, text("eval() for ",typeid(e)," [",e.pos,"] is not defined"));
	}

	Value eval( Str e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx )
	{
		if( isASTLayer(lay) )
			return ast2table(e, (AST e){return eval(e,lay,ctx);});
		if( isUserDefinedLayer(lay) )
			return lift(new StrValue(e.data), lay, ctx, e.pos);
		return new StrValue(e.data);
	}

	Value eval( Int e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx )
	{
		if( isASTLayer(lay) )
			return ast2table(e, (AST e){return eval(e,lay,ctx);});
		if( isUserDefinedLayer(lay) )
			return lift(new IntValue(e.data), lay, ctx, e.pos);
		return new IntValue(e.data);
	}

	Value eval( Var e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx )
	{
		if( isASTLayer(lay) )
			if( isMacroLayer(lay) && ctx.has(e.name,MacroLayer) )
				return ctx.get(e.name, MacroLayer, e.pos);
			else
				return ast2table(e, (AST e){return eval(e,lay,ctx);});
		if( isUserDefinedLayer(lay) && !ctx.has(e.name, lay) )
			return lift(ctx.get(e.name, ValueLayer, e.pos), lay, ctx, e.pos);
		return ctx.get(e.name, lay, e.pos);
	}

	Value eval( App e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx )
	{
		Value f = eval( e.fun, lay, ctx );
		if( isASTLayer(lay) ) {
			auto ff = cast(FunValue)f;
			if( ff !is null && isMacroLayer(lay) )
				return invokeFunction(ff, e.args, lay, ctx, e.pos, getNameIfPossible(e.fun));
			else
				return ast2table(e, (AST e){return eval(e,lay,ctx);});
		}
		return invokeFunction(f, e.args, lay, ctx, e.pos, getNameIfPossible(e.fun));
	}
	
	Value eval( Fun e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx )
	{
		if( isASTLayer(lay) )
		{
			// need this for correct scoping (outer scope macro variables must be hidden!)
			Table newCtx = new Table(ctx, Table.Kind.NotPropagateSet);
			foreach(p; e.params)
				newCtx.set(p.name, NoopLayer, null);
			return ast2table(e, (AST e){return eval(e,lay,newCtx);});
		}
		else
			return createNewFunction(e, ctx);
	}
	
	Value eval( Lay e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx )
	{
		if( isNoLayerChangeLayer(lay) )
			return ast2table(e, (AST e){return eval(e,lay,ctx);});
		else
			return eval(e.expr, e.layer, ctx);
	}
	
	Value eval( Let e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx )
	{
		Table newCtx = overwriteCtx ? ctx : new Table(ctx, Table.Kind.NotPropagateSet);
		if( isASTLayer(lay) )
			return ast2table(e, (AST ee){
				// need this for correct scoping (outer scope macro variables must be hidden!)
				if(e.name!="_" && ee is e.expr)
					newCtx.set(e.name, NoopLayer, null);
				return eval(ee,lay,newCtx);
			});
		else
		{
			Value ri = eval(e.init, lay, newCtx);
			if(e.name!="_")
				newCtx.set(e.name, e.layer.empty ? lay : e.layer, ri);
			return eval(e.expr, lay, newCtx, OverwriteCtx);
		}
	}

private:
	// little little bit incremental macro defining version.
	// enables @macro foo(x)=... in ... foo ..., only at the top level of the
	// interpreter and functions. better than nothing :P
	Tuple!(Value,AST) macroAndEval( AST e_, Layer lay, Table ctx, bool overwriteCtx
		, ref AST[void*] mandeCache) // [TODO] avoid assuming non-moving GC
	{
		assert( !isASTLayer(lay) );

		AST decodeAST(Value v, LexPosition pos)
		{
			// [TODO] more informative error message
			return polemy2d!(AST)(v, pos);
		}

		if(auto e = cast(Let)e_)
		{
			void* key = cast(void*)e.init;
			AST ai;
			if(auto p = key in mandeCache)
				ai = *p;
			else {
				ai = decodeAST(eval(e.init, RawMacroLayer, ctx), e.init.pos);
				mandeCache[key] = ai;
			}
			Value vi = eval(ai, lay, ctx);
			
			if( !overwriteCtx )
				ctx = new Table(ctx, Table.Kind.NotPropagateSet);
			string theLayer = e.layer.empty ? lay : e.layer;
			ctx.set(e.name, theLayer, vi);

			auto ave = macroAndEval( e.expr, lay, ctx, OverwriteCtx, mandeCache );
			AST  a = new Let(e.pos, e.name, e.layer, ai, ave[1]);
			return tuple(ave[0], a);
		}
		else
		{
			void* key = cast(void*)e_;
			AST a;
			if(auto p = key in mandeCache)
				a = *p;
			else {
				a = decodeAST(eval(e_, RawMacroLayer, ctx), e_.pos);
				mandeCache[key] = a;
			}
			Value v = eval(a, lay, ctx, overwriteCtx);
			return tuple(v, a);
		}
	}

private:
	string getNameIfPossible(AST e)
	{
		if(auto v = cast(Var)e)
			return v.name;
		return "";
	}
	
	Value invokeFunction(Value _f, AST[] args, Layer lay, Table ctx, LexPosition pos, string callstackmsg)
	{
		if(auto f = cast(FunValue)_f)
		{
			Table newCtx = new Table(f.definitionContext(), Table.Kind.NotPropagateSet);
			foreach(i,p; f.params())
				if( p.layers.empty )
					newCtx.set(p.name, isMacroLayer(lay)?MacroLayer:lay, eval(args[i], lay, ctx));
				else
					foreach(argLay; p.layers)
						newCtx.set(p.name, argLay, eval(args[i], argLay, ctx));
			scope _ = new PushCallStack(pos, callstackmsg);
			return f.invoke(isMacroLayer(lay)?MacroLayer:lay, newCtx, pos);
		}
		throw genex!RuntimeException(pos, text("tried to call non-function: ",_f));
	}

	Value lift(Value v, Layer lay, Table ctx, LexPosition pos)
	{
		assert( !isASTLayer(lay), "lift to the @macro layer should never happen" );

		// functions are automatically lifterd
		if( cast(FunValue) v )
			return v;

		if( !ctx.has(lay, LiftLayer) )
			throw genex!RuntimeException(pos, "lift function for "~lay~" is not registered" );

		// similar to invokeFunction, but with only one argument bound to ValueLayer
		auto _f = ctx.get(lay, LiftLayer, pos);
		if(auto f = cast(FunValue)_f)
		{
			Table newCtx = new Table(f.definitionContext(), Table.Kind.NotPropagateSet);
			auto ps = f.params();
			if( ps.length != 1 )
				throw genex!RuntimeException(pos,
					text("lift function for", lay, " must take exactly one argument of ", ValueLayer));
			if( ps[0].layers.length==0 || ps[0].layers.length==1 && ps[0].layers[0]==ValueLayer )
			{
				newCtx.set(ps[0].name, ValueLayer, v);
				scope _ = new PushCallStack(pos, lay);
				return f.invoke(ValueLayer, newCtx, pos);
			}
			else
				throw genex!RuntimeException(pos,
					text("lift function for", lay, " must take exactly one argument of ", ValueLayer));
		}
		throw genex!RuntimeException(pos,
			text("non-function ", _f, " is registered as the lift function for ", lay));
	}

	Value createNewFunction(Fun e, Table ctx)
	{
		class UserDefinedFunValue : FunValue
		{
			Fun   ast;
			Table defCtx;
			override const(Parameter[]) params() { return ast.params; }
			override Table definitionContext()   { return defCtx; }

			this(Fun ast, Table defCtx) { this.ast=ast; this.defCtx=defCtx; }
			override string toString() const
				{ return sprintf!"(function:%x:%x)"(cast(void*)ast, cast(void*)defCtx); }
			override int opCmp(Object rhs) {
				if(auto r = cast(UserDefinedFunValue)rhs) {
					if(auto c = typeid(void*).compare(cast(void*)ast, cast(void*)r.ast))
						return c;
					if(auto c = typeid(void*).compare(cast(void*)defCtx, cast(void*)r.defCtx))
						return c;
					return 0;// [TODO] avoid using pointer value...
				}
				if(auto r = cast(Value)rhs) return typeid(this).opCmp(typeid(r));
				throw genex!RuntimeException("comparison with value and something other");
			}
			override hash_t toHash() {
				return (cast(hash_t)cast(void*)ast) + (cast(hash_t)cast(void*)defCtx);
			}

			AST macroCache;
			AST[void*] mandeCache;
			static class MemokeyType
			{
				void* a; Layer b; Tuple!(string,Layer,Value)[] c;
				hash_t toHash() {
					hash_t h = structuralHash(a) + structuralHash(b);
					foreach(e; c)
						h += structuralHash(e[0])+structuralHash(e[1])+structuralHash(e[2]);
					return h;
				}
				mixin SimpleToString;
				mixin SimpleConstructor;
				mixin SimpleCompareWithoutToHash;
			}
			static Tuple!(Value,int)[MemokeyType] memo;

			override Value invoke(Layer lay, Table ctx, LexPosition pos)
			{
				if( isASTLayer(lay) )
					return eval(ast.funbody, lay, ctx);

				auto nonMemoizedRun = (){
					if( macroCache is null )
					{
						auto va = macroAndEval(e.funbody, lay, ctx, CascadeCtx, mandeCache);
						macroCache = va[1];
						return va[0];
					}
					else
						return eval(macroCache, lay, ctx);
				};

				if( !isUserDefinedLayer(lay) )
					return nonMemoizedRun();

				MemokeyType memokey = new MemokeyType(cast(void*)ast, lay, ctx.direct_entries());

				if(auto p = memokey in memo)
				{
					(*p)[1] ++;
					return (*p)[0];
				}
				else
					memo[memokey] = tuple(lift(new UndefinedValue, lay, ctx, pos), 0);

				Value r = nonMemoizedRun();

				int touched = memo[memokey][1];
				memo[memokey] = tuple(r, 12345678);
				//if(touched) {DBG("rerun :: ",r);r = nonMemoizedRun();} // twice!!
				return r;
			}
		}
		return new UserDefinedFunValue(e,ctx);
	}

public:
	/// Add primitive function to the global context
	void addPrimitive(R,T...)(string name, Layer defLay, R delegate (T) dg)
	{
		class NativeFunValue : FunValue
		{
			override const(Parameter[]) params() { return params_data; }
			override Table definitionContext()   { return theContext; }

			override string toString() { return sprintf!"(native:%x)"(dg.funcptr); }
			override int opCmp(Object rhs) {
				if(auto r = cast(NativeFunValue)rhs) return typeid(typeof(dg)).compare(&dg,&r.dg);
				if(auto r = cast(Value)rhs)          return typeid(this).opCmp(typeid(r));
				throw genex!RuntimeException("comparison with value and something other");
			}
			override hash_t toHash() const {
				return typeid(dg).getHash(&dg);
			}

			R delegate(T) dg;
			Parameter[] params_data;

			this(R delegate(T) dg)
			{
				this.dg = dg;
				foreach(i, Ti; T)
					params_data ~= new Parameter(text(i), []);
			}

			override Value invoke(Layer lay, Table ctx, LexPosition pos)
			{
				if( lay != defLay )
					throw genex!RuntimeException(pos,
						text("only ", defLay, " layer can call native function: ", name));
				T typed_args;
				foreach(i, Ti; T) {
					typed_args[i] = cast(Ti) ctx.get(text(i), ValueLayer, pos);
					if( typed_args[i] is null )
						throw genex!RuntimeException(pos,
							sprintf!"type mismatch on the argument %d of native function: %s"(i+1,name));
				}
				try {
					return dg(typed_args);
				} catch( RuntimeException e ) {
					throw e.pos is null ? new RuntimeException(pos, e.msg, e.file, e.line) : e;
				}
			}
		}
		theContext.set(name, defLay, new NativeFunValue(dg));
	}
}

version(unittest) import polemy.runtime;
unittest
{
	auto e = new Evaluator;
	enrollRuntimeLibrary(e);
	auto r = assert_nothrow( e.evalString(`var x = 21; x + x*x;`) );
	assert_eq( r, new IntValue(BigInt(21+21*21)) );
	assert_eq( e.globalContext.get("x",ValueLayer), new IntValue(BigInt(21)) );
	assert_nothrow( e.globalContext.get("x",ValueLayer) );
	assert_throw!RuntimeException( e.globalContext.get("y",ValueLayer) );
}
unittest
{
	auto e = new Evaluator;
	enrollRuntimeLibrary(e);
	auto r = assert_nothrow( e.evalString(`var x = 21; var x = x + x*x;`) );
	assert_eq( r, new IntValue(BigInt(21+21*21)) );
	assert_eq( e.globalContext.get("x",ValueLayer), new IntValue(21+21*21) );
	assert_nothrow( e.globalContext.get("x",ValueLayer) );
	assert_throw!RuntimeException( e.globalContext.get("y",ValueLayer) );
}
unittest
{
	auto e = new Evaluator;
	enrollRuntimeLibrary(e);
	assert_eq( e.evalString(`let x=1; let y=(let x=2); x`), new IntValue(1) ); 
	assert_eq( e.evalString(`let x=1; let y=(let x=2;fun(){x}); y()`), new IntValue(2) ); 
}

unittest
{
	auto e = new Evaluator;
	enrollRuntimeLibrary(e);
	assert_eq( e.evalString(`@a x=1; @b x=2; @a(x)`), new IntValue(BigInt(1)) );
	assert_eq( e.evalString(`@a x=1; @b x=2; @b(x)`), new IntValue(BigInt(2)) );
	assert_eq( e.evalString(`let x=1; let _ = (@a x=2;2); x`), new IntValue(BigInt(1)) );
	e = new Evaluator;
	assert_throw!Throwable( e.evalString(`let x=1; let _ = (@a x=2;2); @a(x)`) );
}

unittest
{
	auto e = new Evaluator;
	enrollRuntimeLibrary(e);
	assert_eq( e.evalString(`
		@@s(x){x};
		@s "+" = fun(x, y) {@value(
			@s(x) - @s(y)
		)};
		@s(1 + 2)
	`), new IntValue(BigInt(-1)) );
}

unittest
{
	auto e = new Evaluator;
	enrollRuntimeLibrary(e);
	assert_eq( e.evalString(`
@@3(x){x};
def incr(x) { x+1 };
@ 3 incr(x) {@value( if @ 3(x)+1< 3 then @3(x)+1 else 0 )};
def fb(n @value @3) { @3(n) };
fb(incr(incr(incr(0))))
	`), new IntValue(BigInt(0)) );
}

unittest
{
	auto e = new Evaluator;
	enrollRuntimeLibrary(e);
	assert_nothrow( e.evalString(`
@macro twice(x) { x; x };
def main() { twice(1) };
main()
	`) );
}
unittest
{
	auto e = new Evaluator;
	enrollRuntimeLibrary(e);
	assert_nothrow( e.evalString(`case 1`) );
	assert_nothrow( e.evalString(`case 1 when 1: 2`) );

	// this is a shorthand for
	//   @macro x = fun(){} in @macro(x)
	// so it is ok to fail, but it is really incovenient on REPL
	assert_nothrow( e.evalString(`@macro x=fun(){}`) );
}
