changeset 659:f1150518c467

remove tail recursion
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 05 Apr 2016 20:30:42 -0600
parents e038905512d3
children e064377994b2
files core/src/luan/impl/Closure.java core/src/luan/impl/LuanStateImpl.java core/src/luan/impl/ReturnStmt.java
diffstat 3 files changed, 24 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
diff -r e038905512d3 -r f1150518c467 core/src/luan/impl/Closure.java
--- a/core/src/luan/impl/Closure.java	Tue Apr 05 18:38:29 2016 -0600
+++ b/core/src/luan/impl/Closure.java	Tue Apr 05 20:30:42 2016 -0600
@@ -46,37 +46,32 @@
 	}
 
 	private static Object call(Closure closure,LuanStateImpl luan,Object[] args) throws LuanException {
-		while(true) {
-			FnDef fnDef = closure.fnDef;
-			Object[] varArgs = null;
-			if( fnDef.isVarArg ) {
-				if( args.length > fnDef.numArgs ) {
-					varArgs = new Object[ args.length - fnDef.numArgs ];
-					for( int i=0; i<varArgs.length; i++ ) {
-						varArgs[i] = args[fnDef.numArgs+i];
-					}
-				} else {
-					varArgs = LuanFunction.NOTHING;
+		FnDef fnDef = closure.fnDef;
+		Object[] varArgs = null;
+		if( fnDef.isVarArg ) {
+			if( args.length > fnDef.numArgs ) {
+				varArgs = new Object[ args.length - fnDef.numArgs ];
+				for( int i=0; i<varArgs.length; i++ ) {
+					varArgs[i] = args[fnDef.numArgs+i];
 				}
+			} else {
+				varArgs = LuanFunction.NOTHING;
 			}
-			Object[] stack = luan.newFrame(closure,fnDef.stackSize,varArgs);
-			final int n = Math.min(args.length,fnDef.numArgs);
-			for( int i=0; i<n; i++ ) {
-				stack[i] = args[i];
-			}
-			Object returnValues;
-			try {
-				fnDef.block.eval(luan);
-			} catch(ReturnException e) {
-			} finally {
-				returnValues = luan.returnValues;
-				closure = luan.tailFn;
-				luan.popFrame();
-			}
-			if( closure == null )
-				return returnValues;
-			args = Luan.array(returnValues);
+		}
+		Object[] stack = luan.newFrame(closure,fnDef.stackSize,varArgs);
+		final int n = Math.min(args.length,fnDef.numArgs);
+		for( int i=0; i<n; i++ ) {
+			stack[i] = args[i];
 		}
+		Object returnValues;
+		try {
+			fnDef.block.eval(luan);
+		} catch(ReturnException e) {
+		} finally {
+			returnValues = luan.returnValues;
+			luan.popFrame();
+		}
+		return returnValues;
 	}
 
 }
diff -r e038905512d3 -r f1150518c467 core/src/luan/impl/LuanStateImpl.java
--- a/core/src/luan/impl/LuanStateImpl.java	Tue Apr 05 18:38:29 2016 -0600
+++ b/core/src/luan/impl/LuanStateImpl.java	Tue Apr 05 20:30:42 2016 -0600
@@ -54,7 +54,6 @@
 
 	private Frame frame = null;
 	public Object returnValues;
-	Closure tailFn;
 
 	LuanStateImpl() {}
 
@@ -71,14 +70,12 @@
 	// returns stack
 	Object[] newFrame(Closure closure, int stackSize, Object[] varArgs) {
 		returnValues = LuanFunction.NOTHING;
-		tailFn = null;
 		frame = new Frame(frame,closure,stackSize,varArgs);
 		return frame.stack;
 	}
 
 	void popFrame() {
 		returnValues = LuanFunction.NOTHING;
-		tailFn = null;
 		frame = frame.previousFrame;
 	}
 
diff -r e038905512d3 -r f1150518c467 core/src/luan/impl/ReturnStmt.java
--- a/core/src/luan/impl/ReturnStmt.java	Tue Apr 05 18:38:29 2016 -0600
+++ b/core/src/luan/impl/ReturnStmt.java	Tue Apr 05 20:30:42 2016 -0600
@@ -14,18 +14,7 @@
 	}
 
 	@Override public void eval(LuanStateImpl luan) throws LuanException {
-		if( !(expressions instanceof FnCall) ) {
-			luan.returnValues = expressions.eval(luan);
-		} else {  // tail call
-			FnCall tailFnCall = (FnCall)expressions;
-			LuanFunction tailFn = Luan.checkFunction( tailFnCall.fnExpr.eval(luan) );
-			luan.returnValues = tailFnCall.args.eval(luan);
-			if( tailFn instanceof Closure ) {
-				luan.tailFn = (Closure)tailFn;
-			} else {
-				luan.returnValues =  tailFn.call(luan,Luan.array(luan.returnValues));
-			}
-		}
+		luan.returnValues = expressions.eval(luan);
 		if( throwReturnException )
 			throw new ReturnException();
 	}