diff core/src/luan/impl/Closure.java @ 659:f1150518c467

remove tail recursion
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 05 Apr 2016 20:30:42 -0600
parents cdc70de628b5
children b438a47196bc
line wrap: on
line diff
--- 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;
 	}
 
 }