view src/luan/interp/ReturnStmt.java @ 48:64ecb7a3aad7

rename Lua to Luan git-svn-id: https://luan-java.googlecode.com/svn/trunk@49 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 28 Dec 2012 03:29:12 +0000
parents e3624b7cd603
children 8ede219cd111
line wrap: on
line source

package luan.interp;

import luan.Luan;
import luan.LuanException;
import luan.LuanFunction;
import luan.LuanSource;


final class ReturnStmt extends CodeImpl implements Stmt {
	private final Expressions expressions;
	private final Expr tailFnExpr;
	boolean throwReturnException = true;

	ReturnStmt(LuanSource.Element se,Expressions expressions) {
		super(se);
		if( expressions instanceof FnCall ) {  // tail call
			FnCall fnCall = (FnCall)expressions;
			this.expressions = fnCall.args;
			this.tailFnExpr = fnCall.fnExpr;
		} else {
			this.expressions = expressions;
			this.tailFnExpr = null;
		}
	}

	@Override public void eval(LuanStateImpl lua) throws LuanException {
		lua.returnValues = expressions.eval(lua);
		if( tailFnExpr != null ) {
			LuanFunction tailFn = lua.checkFunction( se, tailFnExpr.eval(lua) );
			if( tailFn instanceof Closure ) {
				lua.tailFn = (Closure)tailFn;
			} else {
				lua.returnValues =  lua.call(tailFn,tailFnExpr.se(),tailFnExpr.se().text(),lua.returnValues);
			}
		}
		if( throwReturnException )
			throw new ReturnException();
	}
}