Mercurial Hosting > luan
comparison src/luan/interp/LuaParser.java @ 20:d85510d92eee
add BreakStmt
git-svn-id: https://luan-java.googlecode.com/svn/trunk@21 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Sun, 02 Dec 2012 10:51:18 +0000 |
parents | a7c13c6017f7 |
children | c93d8c781853 |
comparison
equal
deleted
inserted
replaced
19:a7c13c6017f7 | 20:d85510d92eee |
---|---|
24 public class LuaParser extends BaseParser<Object> { | 24 public class LuaParser extends BaseParser<Object> { |
25 int nEquals; | 25 int nEquals; |
26 int parens = 0; | 26 int parens = 0; |
27 List<String> symbols = new ArrayList<String>(); | 27 List<String> symbols = new ArrayList<String>(); |
28 int stackSize = 0; | 28 int stackSize = 0; |
29 int loops = 0; | |
29 | 30 |
30 boolean nEquals(int n) { | 31 boolean nEquals(int n) { |
31 nEquals = n; | 32 nEquals = n; |
32 return true; | 33 return true; |
33 } | 34 } |
55 while( n-- > 0 ) { | 56 while( n-- > 0 ) { |
56 symbols.remove(symbols.size()-1); | 57 symbols.remove(symbols.size()-1); |
57 } | 58 } |
58 return true; | 59 return true; |
59 } | 60 } |
61 | |
62 boolean incLoops() { | |
63 loops++; | |
64 return true; | |
65 } | |
66 | |
67 boolean decLoops() { | |
68 loops--; | |
69 return true; | |
70 } | |
71 | |
60 | 72 |
61 public Rule Target() { | 73 public Rule Target() { |
62 return Sequence( | 74 return Sequence( |
63 Spaces(), | 75 Spaces(), |
64 FirstOf( | 76 FirstOf( |
120 Rule Stmt(Var<List<Stmt>> stmts) { | 132 Rule Stmt(Var<List<Stmt>> stmts) { |
121 return FirstOf( | 133 return FirstOf( |
122 LocalStmt(stmts), | 134 LocalStmt(stmts), |
123 Sequence( | 135 Sequence( |
124 FirstOf( | 136 FirstOf( |
137 BreakStmt(), | |
125 GenericForStmt(), | 138 GenericForStmt(), |
126 NumericForStmt(), | 139 NumericForStmt(), |
127 DoStmt(), | 140 DoStmt(), |
128 WhileStmt(), | 141 WhileStmt(), |
129 RepeatStmt(), | 142 RepeatStmt(), |
134 stmts.get().add( (Stmt)pop() ) | 147 stmts.get().add( (Stmt)pop() ) |
135 ) | 148 ) |
136 ); | 149 ); |
137 } | 150 } |
138 | 151 |
152 Rule BreakStmt() { | |
153 return Sequence( | |
154 Keyword("break"), | |
155 loops > 0, | |
156 push( new BreakStmt() ) | |
157 ); | |
158 } | |
159 | |
139 Rule GenericForStmt() { | 160 Rule GenericForStmt() { |
140 Var<Integer> stackStart = new Var<Integer>(symbols.size()); | 161 Var<Integer> stackStart = new Var<Integer>(symbols.size()); |
141 return Sequence( | 162 return Sequence( |
142 Keyword("for"), NameList(), Keyword("in"), Expr(), Keyword("do"), Block(), Keyword("end"), | 163 Keyword("for"), NameList(), Keyword("in"), Expr(), Keyword("do"), LoopBlock(), Keyword("end"), |
143 push( new GenericForStmt( stackStart.get(), symbols.size() - stackStart.get(), expr(pop(1)), (Stmt)pop() ) ), | 164 push( new GenericForStmt( stackStart.get(), symbols.size() - stackStart.get(), expr(pop(1)), (Stmt)pop() ) ), |
144 popSymbols( symbols.size() - stackStart.get() ) | 165 popSymbols( symbols.size() - stackStart.get() ) |
145 ); | 166 ); |
146 } | 167 } |
147 | 168 |
153 ',', Spaces(), | 174 ',', Spaces(), |
154 drop(), | 175 drop(), |
155 Expr() | 176 Expr() |
156 ), | 177 ), |
157 symbols.add( (String)pop(3) ), // add "for" var to symbols | 178 symbols.add( (String)pop(3) ), // add "for" var to symbols |
158 Keyword("do"), Block(), Keyword("end"), | 179 Keyword("do"), LoopBlock(), Keyword("end"), |
159 push( new NumericForStmt( symbols.size()-1, expr(pop(3)), expr(pop(2)), expr(pop(1)), (Stmt)pop() ) ), | 180 push( new NumericForStmt( symbols.size()-1, expr(pop(3)), expr(pop(2)), expr(pop(1)), (Stmt)pop() ) ), |
160 popSymbols(1) | 181 popSymbols(1) |
161 ); | 182 ); |
162 } | 183 } |
163 | 184 |
198 return new SetStmt( vars, values ); | 219 return new SetStmt( vars, values ); |
199 } | 220 } |
200 | 221 |
201 Rule WhileStmt() { | 222 Rule WhileStmt() { |
202 return Sequence( | 223 return Sequence( |
203 Keyword("while"), Expr(), Keyword("do"), Block(), Keyword("end"), | 224 Keyword("while"), Expr(), Keyword("do"), LoopBlock(), Keyword("end"), |
204 push( new WhileStmt( expr(pop(1)), (Stmt)pop() ) ) | 225 push( new WhileStmt( expr(pop(1)), (Stmt)pop() ) ) |
205 ); | 226 ); |
206 } | 227 } |
207 | 228 |
208 Rule RepeatStmt() { | 229 Rule RepeatStmt() { |
209 return Sequence( | 230 return Sequence( |
210 Keyword("repeat"), Block(), Keyword("until"), Expr(), | 231 Keyword("repeat"), LoopBlock(), Keyword("until"), Expr(), |
211 push( new RepeatStmt( (Stmt)pop(1), expr(pop()) ) ) | 232 push( new RepeatStmt( (Stmt)pop(1), expr(pop()) ) ) |
212 ); | 233 ); |
234 } | |
235 | |
236 Rule LoopBlock() { | |
237 return Sequence( incLoops(), Block(), decLoops() ); | |
213 } | 238 } |
214 | 239 |
215 Rule IfStmt() { | 240 Rule IfStmt() { |
216 Var<Integer> n = new Var<Integer>(1); | 241 Var<Integer> n = new Var<Integer>(1); |
217 return Sequence( | 242 return Sequence( |