view core/src/luan/impl/ReturnStmt.java @ 576:4723d22062ce

remove LuanBit
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 13 Jul 2015 20:38:26 -0600
parents b48cfa14ba60
children abc3198c86dd
line wrap: on
line source

package luan.impl;

import luan.Luan;
import luan.LuanException;
import luan.LuanFunction;
import luan.LuanElement;


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

	ReturnStmt(LuanElement el,Expressions expressions) {
		super(el);
		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 luan) throws LuanException {
		luan.returnValues = expressions.eval(luan);
		if( tailFnExpr != null ) {
			LuanElement elTail = tailFnExpr.el();
			luan.push(elTail,elTail.text());
			try {
				LuanFunction tailFn = luan.checkFunction( tailFnExpr.eval(luan) );
				if( tailFn instanceof Closure ) {
					luan.tailFn = (Closure)tailFn;
				} else {
					luan.returnValues =  tailFn.call(luan,Luan.array(luan.returnValues));
				}
			} finally {
				luan.pop();
			}
		}
		if( throwReturnException )
			throw new ReturnException();
	}
}