Mercurial Hosting > luan
comparison src/luan/interp/ForStmt.java @ 127:bcb3a09d0caf
minor
git-svn-id: https://luan-java.googlecode.com/svn/trunk@128 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Sat, 07 Jun 2014 01:47:00 +0000 |
parents | src/luan/interp/GenericForStmt.java@2428ecfed375 |
children | 0594c132888b |
comparison
equal
deleted
inserted
replaced
126:0149bdf98fd8 | 127:bcb3a09d0caf |
---|---|
1 package luan.interp; | |
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); | |
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 } |