view core/src/luan/impl/ReturnStmt.java @ 647:8e8c30b72e9b

move methods from LuanState to Luan
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 29 Mar 2016 20:39:14 -0600
parents 859c0dedc8b6
children f1150518c467
line wrap: on
line source

package luan.impl;

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


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

	ReturnStmt(Expressions expressions) {
		this.expressions = expressions;
	}

	@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));
			}
		}
		if( throwReturnException )
			throw new ReturnException();
	}
}