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

improve stack trace
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 06 May 2015 14:32:29 -0600
parents 97d175772fed
children 55f9f74f1e55
line wrap: on
line source

package luan.impl;

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


final class ForStmt extends CodeImpl implements Stmt {
	private final int iVars;
	private final int nVars;
	private final Expr iterExpr;
	private final Stmt block;

	ForStmt(LuanElement el,int iVars,int nVars,Expr iterExpr,Stmt block) {
		super(el);
		this.iVars = iVars;
		this.nVars = nVars;
		this.iterExpr = iterExpr;
		this.block = block;
	}

	@Override public void eval(LuanStateImpl luan) throws LuanException {
		LuanBit bit = luan.bit(iterExpr.el());
		LuanFunction iter = bit.checkFunction( iterExpr.eval(luan) );
		String name = iterExpr.el().text();
		try {
			while(true) {
				Object vals = bit.call(iter,name,LuanFunction.NOTHING);
				if( vals==null )
					break;
				if( vals instanceof Object[] ) {
					Object[] a = (Object[])vals;
					if( a.length==0 )
						break;
					for( int i=0; i<nVars; i++ ) {
						luan.stackSet( iVars+i, i < a.length ? a[i] : null );
					}
				} else {
					luan.stackSet( iVars, vals );
					for( int i=1; i<nVars; i++ ) {
						luan.stackSet( iVars+i, null );
					}
				}
				block.eval(luan);
			}
		} catch(BreakException e) {
		} finally {
			luan.stackClear(iVars,iVars+nVars);
		}
	}

}