comparison src/luan/impl/LuanParser.java @ 1665:eb89db694225

fix repeat-until scoping
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 06 May 2022 18:51:48 -0600
parents 0af6a9d6d12f
children 9ef19f5ea973
comparison
equal deleted inserted replaced
1664:35e412677512 1665:eb89db694225
292 throw parser.exception(); 292 throw parser.exception();
293 parser.success(); 293 parser.success();
294 } 294 }
295 295
296 private Stmts RequiredBlock() throws ParseException { 296 private Stmts RequiredBlock() throws ParseException {
297 int stackStart = symbolsSize();
298 Stmts stmts = RequiredStmts();
299 int stackEnd = symbolsSize();
300 popSymbols( stackEnd - stackStart );
301 return stmts;
302 }
303
304 private Stmts RequiredStmts() throws ParseException {
297 Stmts stmts = new Stmts(); 305 Stmts stmts = new Stmts();
298 int stackStart = symbolsSize();
299 do { 306 do {
300 Spaces(); 307 Spaces();
301 stmts.addNewLines(); 308 stmts.addNewLines();
302 Stmts stmt = Stmt(); 309 Stmts stmt = Stmt();
303 if( stmt != null ) { 310 if( stmt != null ) {
307 } while( !stmts.hasReturn && (StmtSep() || TemplateSep(stmts)) ); 314 } while( !stmts.hasReturn && (StmtSep() || TemplateSep(stmts)) );
308 Spaces(); 315 Spaces();
309 while( StmtSep() ) 316 while( StmtSep() )
310 Spaces(); 317 Spaces();
311 stmts.addNewLines(); 318 stmts.addNewLines();
312 int stackEnd = symbolsSize();
313 popSymbols( stackEnd - stackStart );
314 return stmts; 319 return stmts;
315 } 320 }
316 321
317 private boolean StmtSep() throws ParseException { 322 private boolean StmtSep() throws ParseException {
318 return parser.match( ';' ) || EndOfLine(); 323 return parser.match( ';' ) || EndOfLine();
515 520
516 String fnVar = "fn" + ++forCounter; 521 String fnVar = "fn" + ++forCounter;
517 Expr fnExp = new Expr(null,false); 522 Expr fnExp = new Expr(null,false);
518 fnExp.add( fnVar + ".call(luan)" ); 523 fnExp.add( fnVar + ".call(luan)" );
519 Stmts stmt = new Stmts(); 524 Stmts stmt = new Stmts();
520 stmt.add( "" 525 stmt.add( "LuanFunction "+fnVar+" = Luan.checkFunction(" );
521 +"LuanFunction "+fnVar+" = Luan.checkFunction("
522 );
523 stmt.addAll( expr ); 526 stmt.addAll( expr );
524 stmt.add( "); " ); 527 stmt.add( "); " );
525 stmt.add( "while(true) { " ); 528 stmt.add( "while(true) { " );
526 stmt.addAll( makeLocalSetStmt(names,fnExp) ); 529 stmt.addAll( makeLocalSetStmt(names,fnExp) );
527 stmt.add( "if( " ); 530 stmt.add( "if( " );
617 stmt.addAll( loop ); 620 stmt.addAll( loop );
618 stmt.add( "} " ); 621 stmt.add( "} " );
619 return parser.success( stmt ); 622 return parser.success( stmt );
620 } 623 }
621 624
625 int repeatCounter = 0;
626
622 private Stmts RepeatStmt() throws ParseException { 627 private Stmts RepeatStmt() throws ParseException {
623 parser.begin(); 628 parser.begin();
624 if( !Keyword("repeat") ) 629 if( !Keyword("repeat") )
625 return parser.failure(null); 630 return parser.failure(null);
626 Stmts loop = RequiredLoopBlock(); 631 int stackStart = symbolsSize();
632 incLoops();
633 Stmts loop = RequiredStmts();
634 decLoops();
627 RequiredKeyword("until"); 635 RequiredKeyword("until");
628 Expr cnd = RequiredExpr().single(); 636 Expr cnd = RequiredExpr().single();
637 int stackEnd = symbolsSize();
638 popSymbols( stackEnd - stackStart );
639 String repeatVar = "repeat" + ++repeatCounter;
629 Stmts stmt = new Stmts(); 640 Stmts stmt = new Stmts();
641 stmt.add( "boolean "+repeatVar+"; " );
630 stmt.add( "do { " ); 642 stmt.add( "do { " );
631 stmt.addAll( loop ); 643 stmt.addAll( loop );
644 stmt.add( repeatVar+" = " );
645 stmt.addAll( cnd );
646 stmt.add( "; " );
632 stmt.add( "} while( !Luan.checkBoolean(" ); 647 stmt.add( "} while( !Luan.checkBoolean(" );
633 stmt.addAll( cnd ); 648 stmt.add( repeatVar );
634 stmt.add( ") ); " ); 649 stmt.add( ") ); " );
635 return parser.success( stmt ); 650 return parser.success( stmt );
636 } 651 }
637 652
638 private Stmts RequiredLoopBlock() throws ParseException { 653 private Stmts RequiredLoopBlock() throws ParseException {