Mercurial Hosting > luan
comparison src/luan/impl/ForStmt.java @ 166:4eaee12f6c65
move luan/interp to impl
git-svn-id: https://luan-java.googlecode.com/svn/trunk@167 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Sun, 22 Jun 2014 04:17:38 +0000 |
parents | src/luan/interp/ForStmt.java@0594c132888b |
children |
comparison
equal
deleted
inserted
replaced
165:94bbc4cbc106 | 166:4eaee12f6c65 |
---|---|
1 package luan.impl; | |
2 | |
3 import luan.Luan; | |
4 import luan.LuanException; | |
5 import luan.LuanFunction; | |
6 import luan.LuanSource; | |
7 import luan.LuanBit; | |
8 | |
9 | |
10 final class ForStmt extends CodeImpl implements Stmt { | |
11 private final int iVars; | |
12 private final int nVars; | |
13 private final Expr iterExpr; | |
14 private final Stmt block; | |
15 | |
16 ForStmt(LuanSource.Element se,int iVars,int nVars,Expr iterExpr,Stmt block) { | |
17 super(se); | |
18 this.iVars = iVars; | |
19 this.nVars = nVars; | |
20 this.iterExpr = iterExpr; | |
21 this.block = block; | |
22 } | |
23 | |
24 @Override public void eval(LuanStateImpl luan) throws LuanException { | |
25 LuanFunction iter = luan.bit(se).checkFunction( iterExpr.eval(luan) ); | |
26 LuanBit bit = luan.bit(iterExpr.se()); | |
27 String name = iterExpr.se().text(); | |
28 try { | |
29 while(true) { | |
30 Object vals = bit.call(iter,name,LuanFunction.NOTHING); | |
31 if( vals==null ) | |
32 break; | |
33 if( vals instanceof Object[] ) { | |
34 Object[] a = (Object[])vals; | |
35 if( a.length==0 ) | |
36 break; | |
37 for( int i=0; i<nVars; i++ ) { | |
38 luan.stackSet( iVars+i, i < a.length ? a[i] : null ); | |
39 } | |
40 } else { | |
41 luan.stackSet( iVars, vals ); | |
42 for( int i=1; i<nVars; i++ ) { | |
43 luan.stackSet( iVars+i, null ); | |
44 } | |
45 } | |
46 block.eval(luan); | |
47 } | |
48 } catch(BreakException e) { | |
49 } finally { | |
50 luan.stackClear(iVars,iVars+nVars); | |
51 } | |
52 } | |
53 | |
54 } |