Mercurial Hosting > luan
comparison src/luan/interp/LuaParser.java @ 40:e3624b7cd603
implement stack trace
git-svn-id: https://luan-java.googlecode.com/svn/trunk@41 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 21 Dec 2012 10:45:54 +0000 |
parents | e51906de0f11 |
children | 786699c78837 |
comparison
equal
deleted
inserted
replaced
39:e5bcb1eeafc1 | 40:e3624b7cd603 |
---|---|
17 import org.parboiled.support.ValueStack; | 17 import org.parboiled.support.ValueStack; |
18 import org.parboiled.errors.ErrorUtils; | 18 import org.parboiled.errors.ErrorUtils; |
19 import luan.Lua; | 19 import luan.Lua; |
20 import luan.LuaNumber; | 20 import luan.LuaNumber; |
21 import luan.LuaState; | 21 import luan.LuaState; |
22 import luan.LuaSource; | |
22 | 23 |
23 | 24 |
24 class LuaParser extends BaseParser<Object> { | 25 class LuaParser extends BaseParser<Object> { |
26 | |
27 LuaSource source; | |
28 | |
29 LuaSource.Element se(int start) { | |
30 return new LuaSource.Element(source,start,currentIndex()); | |
31 } | |
25 | 32 |
26 static final String _ENV = "_ENV"; | 33 static final String _ENV = "_ENV"; |
27 | 34 |
28 static final class Frame { | 35 static final class Frame { |
29 final Frame parent; | 36 final Frame parent; |
142 boolean decLoops() { | 149 boolean decLoops() { |
143 frame.loops--; | 150 frame.loops--; |
144 return true; | 151 return true; |
145 } | 152 } |
146 | 153 |
147 Chunk newChunk() { | 154 Chunk newChunk(int start) { |
148 return new Chunk( (Stmt)pop(), frame.stackSize, symbolsSize(), frame.isVarArg, frame.upValueGetters.toArray(NO_UP_VALUE_GETTERS) ); | 155 return new Chunk( se(start), (Stmt)pop(), frame.stackSize, symbolsSize(), frame.isVarArg, frame.upValueGetters.toArray(NO_UP_VALUE_GETTERS) ); |
149 } | 156 } |
150 | 157 |
151 Rule Target() { | 158 Rule Target() { |
159 Var<Integer> start = new Var<Integer>(); | |
152 return Sequence( | 160 return Sequence( |
153 Spaces(), | 161 Spaces(), |
154 FirstOf( | 162 FirstOf( |
155 Sequence( ExpList(), EOI ), | 163 Sequence( ExpList(), EOI ), |
156 Sequence( | 164 Sequence( |
165 start.set(currentIndex()), | |
157 action( frame.isVarArg = true ), | 166 action( frame.isVarArg = true ), |
158 Block(), | 167 Block(), |
159 EOI, | 168 EOI, |
160 push( newChunk() ) | 169 push( newChunk(start.get()) ) |
161 ) | 170 ) |
162 ) | 171 ) |
163 ); | 172 ); |
164 } | 173 } |
165 | 174 |
225 ) | 234 ) |
226 ); | 235 ); |
227 } | 236 } |
228 | 237 |
229 Rule ReturnStmt() { | 238 Rule ReturnStmt() { |
230 return Sequence( | 239 Var<Integer> start = new Var<Integer>(); |
240 return Sequence( | |
241 start.set(currentIndex()), | |
231 Keyword("return"), Expressions(), | 242 Keyword("return"), Expressions(), |
232 push( new ReturnStmt( (Expressions)pop() ) ) | 243 push( new ReturnStmt( se(start.get()), (Expressions)pop() ) ) |
233 ); | 244 ); |
234 } | 245 } |
235 | 246 |
236 Rule FunctionStmt() { | 247 Rule FunctionStmt() { |
237 return Sequence( | 248 return Sequence( |
239 push( new SetStmt( (Settable)pop(1), expr(pop()) ) ) | 250 push( new SetStmt( (Settable)pop(1), expr(pop()) ) ) |
240 ); | 251 ); |
241 } | 252 } |
242 | 253 |
243 Rule FnName() { | 254 Rule FnName() { |
244 return Sequence( | 255 Var<Integer> start = new Var<Integer>(); |
256 return Sequence( | |
257 start.set(currentIndex()), | |
245 push(null), // marker | 258 push(null), // marker |
246 Name(), | 259 Name(), |
247 ZeroOrMore( | 260 ZeroOrMore( |
248 '.', Spaces(), | 261 '.', Spaces(), |
249 makeVarExp(), | 262 makeVarExp(start.get()), |
250 NameExpr() | 263 NameExpr() |
251 ), | 264 ), |
252 makeSettableVar() | 265 makeSettableVar(start.get()) |
253 ); | 266 ); |
254 } | 267 } |
255 | 268 |
256 Rule LocalFunctionStmt() { | 269 Rule LocalFunctionStmt() { |
257 return Sequence( | 270 return Sequence( |
270 push( new BreakStmt() ) | 283 push( new BreakStmt() ) |
271 ); | 284 ); |
272 } | 285 } |
273 | 286 |
274 Rule GenericForStmt() { | 287 Rule GenericForStmt() { |
288 Var<Integer> start = new Var<Integer>(); | |
275 Var<Integer> stackStart = new Var<Integer>(symbolsSize()); | 289 Var<Integer> stackStart = new Var<Integer>(symbolsSize()); |
276 Var<List<String>> names = new Var<List<String>>(new ArrayList<String>()); | 290 Var<List<String>> names = new Var<List<String>>(new ArrayList<String>()); |
277 return Sequence( | 291 return Sequence( |
292 start.set(currentIndex()), | |
278 Keyword("for"), NameList(names), Keyword("in"), Expr(), Keyword("do"), | 293 Keyword("for"), NameList(names), Keyword("in"), Expr(), Keyword("do"), |
279 addSymbols(names.get()), | 294 addSymbols(names.get()), |
280 LoopBlock(), Keyword("end"), | 295 LoopBlock(), Keyword("end"), |
281 push( new GenericForStmt( stackStart.get(), symbolsSize() - stackStart.get(), expr(pop(1)), (Stmt)pop() ) ), | 296 push( new GenericForStmt( se(start.get()), stackStart.get(), symbolsSize() - stackStart.get(), expr(pop(1)), (Stmt)pop() ) ), |
282 popSymbols( symbolsSize() - stackStart.get() ) | 297 popSymbols( symbolsSize() - stackStart.get() ) |
283 ); | 298 ); |
284 } | 299 } |
285 | 300 |
286 Rule NumericForStmt() { | 301 Rule NumericForStmt() { |
287 return Sequence( | 302 Var<Integer> start = new Var<Integer>(); |
303 return Sequence( | |
304 start.set(currentIndex()), | |
288 Keyword("for"), Name(), '=', Spaces(), Expr(), Keyword("to"), Expr(), | 305 Keyword("for"), Name(), '=', Spaces(), Expr(), Keyword("to"), Expr(), |
289 push( new ConstExpr(new LuaNumber(1)) ), // default step | 306 push( new ConstExpr(new LuaNumber(1)) ), // default step |
290 Optional( | 307 Optional( |
291 Keyword("step"), | 308 Keyword("step"), |
292 drop(), | 309 drop(), |
293 Expr() | 310 Expr() |
294 ), | 311 ), |
295 addSymbol( (String)pop(3) ), // add "for" var to symbols | 312 addSymbol( (String)pop(3) ), // add "for" var to symbols |
296 Keyword("do"), LoopBlock(), Keyword("end"), | 313 Keyword("do"), LoopBlock(), Keyword("end"), |
297 push( new NumericForStmt( symbolsSize()-1, expr(pop(3)), expr(pop(2)), expr(pop(1)), (Stmt)pop() ) ), | 314 push( new NumericForStmt( se(start.get()), symbolsSize()-1, expr(pop(3)), expr(pop(2)), expr(pop(1)), (Stmt)pop() ) ), |
298 popSymbols(1) | 315 popSymbols(1) |
299 ); | 316 ); |
300 } | 317 } |
301 | 318 |
302 Rule DoStmt() { | 319 Rule DoStmt() { |
419 push(vars.get()) | 436 push(vars.get()) |
420 ); | 437 ); |
421 } | 438 } |
422 | 439 |
423 Rule SettableVar() { | 440 Rule SettableVar() { |
424 return Sequence( Var(), makeSettableVar() ); | 441 Var<Integer> start = new Var<Integer>(); |
425 } | 442 return Sequence( |
426 | 443 start.set(currentIndex()), |
427 boolean makeSettableVar() { | 444 Var(), |
445 makeSettableVar(start.get()) | |
446 ); | |
447 } | |
448 | |
449 boolean makeSettableVar(int start) { | |
428 Object obj2 = pop(); | 450 Object obj2 = pop(); |
429 if( obj2==null ) | 451 if( obj2==null ) |
430 return false; | 452 return false; |
431 Object obj1 = pop(); | 453 Object obj1 = pop(); |
432 if( obj1!=null ) { | 454 if( obj1!=null ) { |
433 Expr key = expr(obj2); | 455 Expr key = expr(obj2); |
434 Expr table = expr(obj1); | 456 Expr table = expr(obj1); |
435 return push( new SetTableEntry(table,key) ); | 457 return push( new SetTableEntry(se(start),table,key) ); |
436 } | 458 } |
437 String name = (String)obj2; | 459 String name = (String)obj2; |
438 int index = stackIndex(name); | 460 int index = stackIndex(name); |
439 if( index != -1 ) | 461 if( index != -1 ) |
440 return push( new SetLocalVar(index) ); | 462 return push( new SetLocalVar(index) ); |
441 index = upValueIndex(name); | 463 index = upValueIndex(name); |
442 if( index != -1 ) | 464 if( index != -1 ) |
443 return push( new SetUpVar(index) ); | 465 return push( new SetUpVar(index) ); |
444 return push( new SetTableEntry( env(), new ConstExpr(name) ) ); | 466 return push( new SetTableEntry( se(start), env(), new ConstExpr(name) ) ); |
445 } | 467 } |
446 | 468 |
447 Rule Expr() { | 469 Rule Expr() { |
448 return FirstOf( | 470 return FirstOf( |
449 VarArgs(), | 471 VarArgs(), |
450 OrExpr() | 472 OrExpr() |
451 ); | 473 ); |
452 } | 474 } |
453 | 475 |
454 Rule OrExpr() { | 476 Rule OrExpr() { |
455 return Sequence( | 477 Var<Integer> start = new Var<Integer>(); |
478 return Sequence( | |
479 start.set(currentIndex()), | |
456 AndExpr(), | 480 AndExpr(), |
457 ZeroOrMore( "or", Spaces(), AndExpr(), push( new OrExpr(expr(pop(1)),expr(pop())) ) ) | 481 ZeroOrMore( "or", Spaces(), AndExpr(), push( new OrExpr(se(start.get()),expr(pop(1)),expr(pop())) ) ) |
458 ); | 482 ); |
459 } | 483 } |
460 | 484 |
461 Rule AndExpr() { | 485 Rule AndExpr() { |
462 return Sequence( | 486 Var<Integer> start = new Var<Integer>(); |
487 return Sequence( | |
488 start.set(currentIndex()), | |
463 RelExpr(), | 489 RelExpr(), |
464 ZeroOrMore( "and", Spaces(), RelExpr(), push( new AndExpr(expr(pop(1)),expr(pop())) ) ) | 490 ZeroOrMore( "and", Spaces(), RelExpr(), push( new AndExpr(se(start.get()),expr(pop(1)),expr(pop())) ) ) |
465 ); | 491 ); |
466 } | 492 } |
467 | 493 |
468 Rule RelExpr() { | 494 Rule RelExpr() { |
469 return Sequence( | 495 Var<Integer> start = new Var<Integer>(); |
496 return Sequence( | |
497 start.set(currentIndex()), | |
470 ConcatExpr(), | 498 ConcatExpr(), |
471 ZeroOrMore( | 499 ZeroOrMore( |
472 FirstOf( | 500 FirstOf( |
473 Sequence( "==", Spaces(), ConcatExpr(), push( new EqExpr(expr(pop(1)),expr(pop())) ) ), | 501 Sequence( "==", Spaces(), ConcatExpr(), push( new EqExpr(se(start.get()),expr(pop(1)),expr(pop())) ) ), |
474 Sequence( "~=", Spaces(), ConcatExpr(), push( new NotExpr(new EqExpr(expr(pop(1)),expr(pop()))) ) ), | 502 Sequence( "~=", Spaces(), ConcatExpr(), push( new NotExpr(se(start.get()),new EqExpr(se(start.get()),expr(pop(1)),expr(pop()))) ) ), |
475 Sequence( "<=", Spaces(), ConcatExpr(), push( new LeExpr(expr(pop(1)),expr(pop())) ) ), | 503 Sequence( "<=", Spaces(), ConcatExpr(), push( new LeExpr(se(start.get()),expr(pop(1)),expr(pop())) ) ), |
476 Sequence( ">=", Spaces(), ConcatExpr(), push( new LeExpr(expr(pop()),expr(pop())) ) ), | 504 Sequence( ">=", Spaces(), ConcatExpr(), push( new LeExpr(se(start.get()),expr(pop()),expr(pop())) ) ), |
477 Sequence( "<", Spaces(), ConcatExpr(), push( new LtExpr(expr(pop(1)),expr(pop())) ) ), | 505 Sequence( "<", Spaces(), ConcatExpr(), push( new LtExpr(se(start.get()),expr(pop(1)),expr(pop())) ) ), |
478 Sequence( ">", Spaces(), ConcatExpr(), push( new LtExpr(expr(pop()),expr(pop())) ) ) | 506 Sequence( ">", Spaces(), ConcatExpr(), push( new LtExpr(se(start.get()),expr(pop()),expr(pop())) ) ) |
479 ) | 507 ) |
480 ) | 508 ) |
481 ); | 509 ); |
482 } | 510 } |
483 | 511 |
484 Rule ConcatExpr() { | 512 Rule ConcatExpr() { |
485 return Sequence( | 513 Var<Integer> start = new Var<Integer>(); |
514 return Sequence( | |
515 start.set(currentIndex()), | |
486 SumExpr(), | 516 SumExpr(), |
487 Optional( "..", Spaces(), ConcatExpr(), push( new ConcatExpr(expr(pop(1)),expr(pop())) ) ) | 517 Optional( "..", Spaces(), ConcatExpr(), push( new ConcatExpr(se(start.get()),expr(pop(1)),expr(pop())) ) ) |
488 ); | 518 ); |
489 } | 519 } |
490 | 520 |
491 Rule SumExpr() { | 521 Rule SumExpr() { |
492 return Sequence( | 522 Var<Integer> start = new Var<Integer>(); |
523 return Sequence( | |
524 start.set(currentIndex()), | |
493 TermExpr(), | 525 TermExpr(), |
494 ZeroOrMore( | 526 ZeroOrMore( |
495 FirstOf( | 527 FirstOf( |
496 Sequence( '+', Spaces(), TermExpr(), push( new AddExpr(expr(pop(1)),expr(pop())) ) ), | 528 Sequence( '+', Spaces(), TermExpr(), push( new AddExpr(se(start.get()),expr(pop(1)),expr(pop())) ) ), |
497 Sequence( '-', TestNot('-'), Spaces(), TermExpr(), push( new SubExpr(expr(pop(1)),expr(pop())) ) ) | 529 Sequence( '-', TestNot('-'), Spaces(), TermExpr(), push( new SubExpr(se(start.get()),expr(pop(1)),expr(pop())) ) ) |
498 ) | 530 ) |
499 ) | 531 ) |
500 ); | 532 ); |
501 } | 533 } |
502 | 534 |
503 Rule TermExpr() { | 535 Rule TermExpr() { |
504 return Sequence( | 536 Var<Integer> start = new Var<Integer>(); |
537 return Sequence( | |
538 start.set(currentIndex()), | |
505 UnaryExpr(), | 539 UnaryExpr(), |
506 ZeroOrMore( | 540 ZeroOrMore( |
507 FirstOf( | 541 FirstOf( |
508 Sequence( '*', Spaces(), UnaryExpr(), push( new MulExpr(expr(pop(1)),expr(pop())) ) ), | 542 Sequence( '*', Spaces(), UnaryExpr(), push( new MulExpr(se(start.get()),expr(pop(1)),expr(pop())) ) ), |
509 Sequence( '/', Spaces(), UnaryExpr(), push( new DivExpr(expr(pop(1)),expr(pop())) ) ), | 543 Sequence( '/', Spaces(), UnaryExpr(), push( new DivExpr(se(start.get()),expr(pop(1)),expr(pop())) ) ), |
510 Sequence( '%', Spaces(), UnaryExpr(), push( new ModExpr(expr(pop(1)),expr(pop())) ) ) | 544 Sequence( '%', Spaces(), UnaryExpr(), push( new ModExpr(se(start.get()),expr(pop(1)),expr(pop())) ) ) |
511 ) | 545 ) |
512 ) | 546 ) |
513 ); | 547 ); |
514 } | 548 } |
515 | 549 |
516 Rule UnaryExpr() { | 550 Rule UnaryExpr() { |
517 return FirstOf( | 551 Var<Integer> start = new Var<Integer>(); |
518 Sequence( '#', Spaces(), PowExpr(), push( new LenExpr(expr(pop())) ) ), | 552 return Sequence( |
519 Sequence( '-', TestNot('-'), Spaces(), PowExpr(), push( new UnmExpr(expr(pop())) ) ), | 553 start.set(currentIndex()), |
520 Sequence( "not", Spaces(), PowExpr(), push( new NotExpr(expr(pop())) ) ), | 554 FirstOf( |
521 PowExpr() | 555 Sequence( '#', Spaces(), PowExpr(), push( new LenExpr(se(start.get()),expr(pop())) ) ), |
556 Sequence( '-', TestNot('-'), Spaces(), PowExpr(), push( new UnmExpr(se(start.get()),expr(pop())) ) ), | |
557 Sequence( "not", Spaces(), PowExpr(), push( new NotExpr(se(start.get()),expr(pop())) ) ), | |
558 PowExpr() | |
559 ) | |
522 ); | 560 ); |
523 } | 561 } |
524 | 562 |
525 Rule PowExpr() { | 563 Rule PowExpr() { |
526 return Sequence( | 564 Var<Integer> start = new Var<Integer>(); |
565 return Sequence( | |
566 start.set(currentIndex()), | |
527 SingleExpr(), | 567 SingleExpr(), |
528 Optional( '^', Spaces(), PowExpr(), push( new PowExpr(expr(pop(1)),expr(pop())) ) ) | 568 Optional( '^', Spaces(), PowExpr(), push( new PowExpr(se(start.get()),expr(pop(1)),expr(pop())) ) ) |
529 ); | 569 ); |
530 } | 570 } |
531 | 571 |
532 Rule SingleExpr() { | 572 Rule SingleExpr() { |
533 return FirstOf( | 573 return FirstOf( |
541 Rule FunctionExpr() { | 581 Rule FunctionExpr() { |
542 return Sequence( "function", Spaces(), Function() ); | 582 return Sequence( "function", Spaces(), Function() ); |
543 } | 583 } |
544 | 584 |
545 Rule Function() { | 585 Rule Function() { |
586 Var<Integer> start = new Var<Integer>(); | |
546 Var<List<String>> names = new Var<List<String>>(new ArrayList<String>()); | 587 Var<List<String>> names = new Var<List<String>>(new ArrayList<String>()); |
547 return Sequence( | 588 return Sequence( |
589 start.set(currentIndex()), | |
548 '(', incParens(), Spaces(), | 590 '(', incParens(), Spaces(), |
549 action( frame = new Frame(frame) ), | 591 action( frame = new Frame(frame) ), |
550 Optional( | 592 Optional( |
551 FirstOf( | 593 FirstOf( |
552 Sequence( | 594 Sequence( |
555 ), | 597 ), |
556 VarArgName() | 598 VarArgName() |
557 ) | 599 ) |
558 ), | 600 ), |
559 ')', decParens(), Spaces(), Block(), Keyword("end"), | 601 ')', decParens(), Spaces(), Block(), Keyword("end"), |
560 push( newChunk() ), | 602 push( newChunk(start.get()) ), |
561 action( frame = frame.parent ) | 603 action( frame = frame.parent ) |
562 ); | 604 ); |
563 } | 605 } |
564 | 606 |
565 Rule VarArgName() { | 607 Rule VarArgName() { |
568 action( frame.isVarArg = true ) | 610 action( frame.isVarArg = true ) |
569 ); | 611 ); |
570 } | 612 } |
571 | 613 |
572 Rule VarArgs() { | 614 Rule VarArgs() { |
573 return Sequence( | 615 Var<Integer> start = new Var<Integer>(); |
616 return Sequence( | |
617 start.set(currentIndex()), | |
574 "...", Spaces(), | 618 "...", Spaces(), |
575 frame.isVarArg, | 619 frame.isVarArg, |
576 push( VarArgs.INSTANCE ) | 620 push( new VarArgs(se(start.get())) ) |
577 ); | 621 ); |
578 } | 622 } |
579 | 623 |
580 Rule TableExpr() { | 624 Rule TableExpr() { |
625 Var<Integer> start = new Var<Integer>(); | |
581 Var<List<TableExpr.Field>> fields = new Var<List<TableExpr.Field>>(new ArrayList<TableExpr.Field>()); | 626 Var<List<TableExpr.Field>> fields = new Var<List<TableExpr.Field>>(new ArrayList<TableExpr.Field>()); |
582 Var<ExpList.Builder> builder = new Var<ExpList.Builder>(new ExpList.Builder()); | 627 Var<ExpList.Builder> builder = new Var<ExpList.Builder>(new ExpList.Builder()); |
583 return Sequence( | 628 return Sequence( |
629 start.set(currentIndex()), | |
584 '{', incParens(), Spaces(), | 630 '{', incParens(), Spaces(), |
585 Optional( | 631 Optional( |
586 Field(fields,builder), | 632 Field(fields,builder), |
587 ZeroOrMore( | 633 ZeroOrMore( |
588 FieldSep(), | 634 FieldSep(), |
590 ), | 636 ), |
591 Optional( FieldSep() ) | 637 Optional( FieldSep() ) |
592 ), | 638 ), |
593 '}', decParens(), | 639 '}', decParens(), |
594 Spaces(), | 640 Spaces(), |
595 push( new TableExpr( fields.get().toArray(new TableExpr.Field[0]), builder.get().build() ) ) | 641 push( new TableExpr( se(start.get()), fields.get().toArray(new TableExpr.Field[0]), builder.get().build() ) ) |
596 ); | 642 ); |
597 } | 643 } |
598 | 644 |
599 Rule FieldSep() { | 645 Rule FieldSep() { |
600 return Sequence( AnyOf(",;"), Spaces() ); | 646 return Sequence( AnyOf(",;"), Spaces() ); |
619 return new ExpressionsExpr((Expressions)obj); | 665 return new ExpressionsExpr((Expressions)obj); |
620 return (Expr)obj; | 666 return (Expr)obj; |
621 } | 667 } |
622 | 668 |
623 Rule VarExp() { | 669 Rule VarExp() { |
624 return Sequence( | 670 Var<Integer> start = new Var<Integer>(); |
671 return Sequence( | |
672 start.set(currentIndex()), | |
625 Var(), | 673 Var(), |
626 makeVarExp() | 674 makeVarExp(start.get()) |
627 ); | 675 ); |
628 } | 676 } |
629 | 677 |
630 Rule Var() { | 678 Rule Var() { |
631 return Sequence( | 679 Var<Integer> start = new Var<Integer>(); |
680 return Sequence( | |
681 start.set(currentIndex()), | |
632 FirstOf( | 682 FirstOf( |
633 Sequence( | 683 Sequence( |
634 '(', incParens(), Spaces(), Expr(), ')', decParens(), Spaces(), | 684 '(', incParens(), Spaces(), Expr(), ')', decParens(), Spaces(), |
635 push(expr(pop())), | 685 push(expr(pop())), |
636 push(null) // marker | 686 push(null) // marker |
639 push(null), // marker | 689 push(null), // marker |
640 Name() | 690 Name() |
641 ) | 691 ) |
642 ), | 692 ), |
643 ZeroOrMore( | 693 ZeroOrMore( |
644 makeVarExp(), | 694 makeVarExp(start.get()), |
645 FirstOf( | 695 FirstOf( |
646 SubExpr(), | 696 SubExpr(), |
647 Sequence( '.', Spaces(), NameExpr() ), | 697 Sequence( '.', Spaces(), NameExpr() ), |
648 Sequence( | 698 Sequence( |
649 Args(), | 699 Args(start), |
650 push(null) // marker | 700 push(null) // marker |
651 ) | 701 ) |
652 ) | 702 ) |
653 ) | 703 ) |
654 ); | 704 ); |
655 } | 705 } |
656 | 706 |
657 Expr env() { | 707 Expr env() { |
658 int index = stackIndex(_ENV); | 708 int index = stackIndex(_ENV); |
659 if( index != -1 ) | 709 if( index != -1 ) |
660 return new GetLocalVar(index); | 710 return new GetLocalVar(null,index); |
661 index = upValueIndex(_ENV); | 711 index = upValueIndex(_ENV); |
662 if( index != -1 ) | 712 if( index != -1 ) |
663 return new GetUpVar(index); | 713 return new GetUpVar(null,index); |
664 throw new RuntimeException("_ENV not found"); | 714 throw new RuntimeException("_ENV not found"); |
665 } | 715 } |
666 | 716 |
667 boolean makeVarExp() { | 717 boolean makeVarExp(int start) { |
668 Object obj2 = pop(); | 718 Object obj2 = pop(); |
669 if( obj2==null ) | 719 if( obj2==null ) |
670 return true; | 720 return true; |
671 Object obj1 = pop(); | 721 Object obj1 = pop(); |
672 if( obj1 != null ) | 722 if( obj1 != null ) |
673 return push( new IndexExpr( expr(obj1), expr(obj2) ) ); | 723 return push( new IndexExpr( se(start), expr(obj1), expr(obj2) ) ); |
674 String name = (String)obj2; | 724 String name = (String)obj2; |
675 int index = stackIndex(name); | 725 int index = stackIndex(name); |
676 if( index != -1 ) | 726 if( index != -1 ) |
677 return push( new GetLocalVar(index) ); | 727 return push( new GetLocalVar(se(start),index) ); |
678 index = upValueIndex(name); | 728 index = upValueIndex(name); |
679 if( index != -1 ) | 729 if( index != -1 ) |
680 return push( new GetUpVar(index) ); | 730 return push( new GetUpVar(se(start),index) ); |
681 return push( new IndexExpr( env(), new ConstExpr(name) ) ); | 731 return push( new IndexExpr( se(start), env(), new ConstExpr(name) ) ); |
682 } | 732 } |
683 | 733 |
684 // function should be on top of the stack | 734 // function should be on top of the stack |
685 Rule Args() { | 735 Rule Args(Var<Integer> start) { |
686 return Sequence( | 736 return Sequence( |
687 FirstOf( | 737 FirstOf( |
688 Sequence( | 738 Sequence( |
689 '(', incParens(), Spaces(), Expressions(), ')', decParens(), Spaces() | 739 '(', incParens(), Spaces(), Expressions(), ')', decParens(), Spaces() |
690 ), | 740 ), |
695 Sequence( | 745 Sequence( |
696 StringLiteral(), Spaces(), | 746 StringLiteral(), Spaces(), |
697 push( new ExpList.SingleExpList(new ConstExpr(pop())) ) | 747 push( new ExpList.SingleExpList(new ConstExpr(pop())) ) |
698 ) | 748 ) |
699 ), | 749 ), |
700 push( new FnCall( expr(pop(1)), (Expressions)pop() ) ) | 750 push( new FnCall( se(start.get()), expr(pop(1)), (Expressions)pop() ) ) |
701 ); | 751 ); |
702 } | 752 } |
703 | 753 |
704 Rule Expressions() { | 754 Rule Expressions() { |
705 return FirstOf( | 755 return FirstOf( |