Mercurial Hosting > luan
comparison src/luan/interp/LuaParser.java @ 28:df923e5835b2
fix local initial assignment
git-svn-id: https://luan-java.googlecode.com/svn/trunk@29 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 07 Dec 2012 05:49:17 +0000 |
parents | 409871b33355 |
children | 8d8f4f5caef4 |
comparison
equal
deleted
inserted
replaced
27:9de9be530625 | 28:df923e5835b2 |
---|---|
67 if( frame.stackSize < symbolsSize() ) | 67 if( frame.stackSize < symbolsSize() ) |
68 frame.stackSize = symbolsSize(); | 68 frame.stackSize = symbolsSize(); |
69 return true; | 69 return true; |
70 } | 70 } |
71 | 71 |
72 boolean addSymbols(List<String> names) { | |
73 frame.symbols.addAll(names); | |
74 if( frame.stackSize < symbolsSize() ) | |
75 frame.stackSize = symbolsSize(); | |
76 return true; | |
77 } | |
78 | |
72 int index(String name) { | 79 int index(String name) { |
73 List<String> symbols = frame.symbols; | 80 List<String> symbols = frame.symbols; |
74 int i = symbols.size(); | 81 int i = symbols.size(); |
75 while( --i >= 0 ) { | 82 while( --i >= 0 ) { |
76 if( symbols.get(i).equals(name) ) | 83 if( symbols.get(i).equals(name) ) |
203 ); | 210 ); |
204 } | 211 } |
205 | 212 |
206 Rule LocalFunctionStmt() { | 213 Rule LocalFunctionStmt() { |
207 return Sequence( | 214 return Sequence( |
208 Keyword("local"), Keyword("function"), LocalName(), Function(), | 215 Keyword("local"), Keyword("function"), |
216 Name(), | |
217 addSymbol( (String)pop() ), | |
218 Function(), | |
209 push( new SetStmt( new SetLocalVar(symbolsSize()-1), expr(pop()) ) ) | 219 push( new SetStmt( new SetLocalVar(symbolsSize()-1), expr(pop()) ) ) |
210 ); | 220 ); |
211 } | 221 } |
212 | 222 |
213 Rule BreakStmt() { | 223 Rule BreakStmt() { |
218 ); | 228 ); |
219 } | 229 } |
220 | 230 |
221 Rule GenericForStmt() { | 231 Rule GenericForStmt() { |
222 Var<Integer> stackStart = new Var<Integer>(symbolsSize()); | 232 Var<Integer> stackStart = new Var<Integer>(symbolsSize()); |
223 return Sequence( | 233 Var<List<String>> names = new Var<List<String>>(new ArrayList<String>()); |
224 Keyword("for"), NameList(), Keyword("in"), Expr(), Keyword("do"), LoopBlock(), Keyword("end"), | 234 return Sequence( |
235 Keyword("for"), NameList(names), Keyword("in"), Expr(), Keyword("do"), | |
236 addSymbols(names.get()), | |
237 LoopBlock(), Keyword("end"), | |
225 push( new GenericForStmt( stackStart.get(), symbolsSize() - stackStart.get(), expr(pop(1)), (Stmt)pop() ) ), | 238 push( new GenericForStmt( stackStart.get(), symbolsSize() - stackStart.get(), expr(pop(1)), (Stmt)pop() ) ), |
226 popSymbols( symbolsSize() - stackStart.get() ) | 239 popSymbols( symbolsSize() - stackStart.get() ) |
227 ); | 240 ); |
228 } | 241 } |
229 | 242 |
248 Keyword("do"), Block(), Keyword("end") | 261 Keyword("do"), Block(), Keyword("end") |
249 ); | 262 ); |
250 } | 263 } |
251 | 264 |
252 Rule LocalStmt(Var<List<Stmt>> stmts) { | 265 Rule LocalStmt(Var<List<Stmt>> stmts) { |
253 Var<Integer> stackStart = new Var<Integer>(symbolsSize()); | 266 Var<List<String>> names = new Var<List<String>>(new ArrayList<String>()); |
254 return Sequence( | 267 return Sequence( |
255 Keyword("local"), NameList(), | 268 Keyword("local"), NameList(names), |
256 Optional( | 269 Optional( |
257 '=', Spaces(), ExpList(), | 270 '=', Spaces(), ExpList(), |
258 stmts.get().add( newSetLocalStmt(stackStart.get()) ) | 271 stmts.get().add( newSetLocalStmt(names.get().size()) ) |
259 ) | 272 ), |
260 ); | 273 addSymbols(names.get()) |
261 } | 274 ); |
262 | 275 } |
263 Rule NameList() { | 276 |
264 return Sequence( | 277 Rule NameList(Var<List<String>> names) { |
265 LocalName(), | |
266 ZeroOrMore( | |
267 ',', Spaces(), LocalName() | |
268 ) | |
269 ); | |
270 } | |
271 | |
272 Rule LocalName() { | |
273 return Sequence( | 278 return Sequence( |
274 Name(), | 279 Name(), |
275 addSymbol( (String)pop() ) | 280 names.get().add( (String)pop() ), |
276 ); | 281 ZeroOrMore( |
277 } | 282 ',', Spaces(), Name(), |
278 | 283 names.get().add( (String)pop() ) |
279 SetStmt newSetLocalStmt(int stackStart) { | 284 ) |
285 ); | |
286 } | |
287 | |
288 SetStmt newSetLocalStmt(int nVars) { | |
280 Expressions values = (Expressions)pop(); | 289 Expressions values = (Expressions)pop(); |
281 SetLocalVar[] vars = new SetLocalVar[symbolsSize()-stackStart]; | 290 SetLocalVar[] vars = new SetLocalVar[nVars]; |
291 int stackStart = symbolsSize(); | |
282 for( int i=0; i<vars.length; i++ ) { | 292 for( int i=0; i<vars.length; i++ ) { |
283 vars[i] = new SetLocalVar(stackStart+i); | 293 vars[i] = new SetLocalVar(stackStart+i); |
284 } | 294 } |
285 return new SetStmt( vars, values ); | 295 return new SetStmt( vars, values ); |
286 } | 296 } |
489 Rule FunctionExpr() { | 499 Rule FunctionExpr() { |
490 return Sequence( "function", Spaces(), Function() ); | 500 return Sequence( "function", Spaces(), Function() ); |
491 } | 501 } |
492 | 502 |
493 Rule Function() { | 503 Rule Function() { |
504 Var<List<String>> names = new Var<List<String>>(new ArrayList<String>()); | |
494 return Sequence( | 505 return Sequence( |
495 action( frame = new Frame(frame) ), | 506 action( frame = new Frame(frame) ), |
496 '(', incParens(), Spaces(), | 507 '(', incParens(), Spaces(), |
497 Optional( | 508 Optional( |
498 FirstOf( | 509 FirstOf( |
499 Sequence( NameList(), Optional( ',', Spaces(), VarArgName() ) ), | 510 Sequence( |
511 NameList(names), addSymbols(names.get()), | |
512 Optional( ',', Spaces(), VarArgName() ) | |
513 ), | |
500 VarArgName() | 514 VarArgName() |
501 ) | 515 ) |
502 ), | 516 ), |
503 ')', decParens(), Spaces(), Block(), Keyword("end"), | 517 ')', decParens(), Spaces(), Block(), Keyword("end"), |
504 push( new Chunk( (Stmt)pop(), frame.stackSize, symbolsSize(), frame.isVarArg ) ), | 518 push( new Chunk( (Stmt)pop(), frame.stackSize, symbolsSize(), frame.isVarArg ) ), |