view core/src/luan/impl/ReturnStmt.java @ 645:859c0dedc8b6

remove LuanSource
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 29 Mar 2016 18:09:51 -0600
parents abc3198c86dd
children 8e8c30b72e9b
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();
	}
}