view core/src/luan/impl/ReturnStmt.java @ 460:b48cfa14ba60

improve stack trace
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 06 May 2015 14:32:29 -0600
parents 7580379cdc79
children 4723d22062ce
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();
			LuanFunction tailFn = luan.bit(elTail).checkFunction( tailFnExpr.eval(luan) );
			if( tailFn instanceof Closure ) {
				luan.tailFn = (Closure)tailFn;
			} else {
				luan.returnValues =  luan.bit(elTail).call(tailFn,elTail.text(),Luan.array(luan.returnValues));
			}
		}
		if( throwReturnException )
			throw new ReturnException();
	}
}