Mercurial Hosting > luan
comparison core/src/luan/impl/LuanParser.java @ 653:538b0ae08faa
compile IfStmt and BreakStmt
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 05 Apr 2016 13:01:04 -0600 |
parents | 067d9470184d |
children | ea7dbd2dfa65 |
comparison
equal
deleted
inserted
replaced
652:067d9470184d | 653:538b0ae08faa |
---|---|
252 || (stmt=BreakStmt()) != null | 252 || (stmt=BreakStmt()) != null |
253 || (stmt=ForStmt()) != null | 253 || (stmt=ForStmt()) != null |
254 || (stmt=DoStmt()) != null | 254 || (stmt=DoStmt()) != null |
255 || (stmt=WhileStmt()) != null | 255 || (stmt=WhileStmt()) != null |
256 || (stmt=RepeatStmt()) != null | 256 || (stmt=RepeatStmt()) != null |
257 || (stmt=stmtStr(IfStmt())) != null | 257 || (stmt=IfStmt()) != null |
258 || (stmt=SetStmt()) != null | 258 || (stmt=SetStmt()) != null |
259 || (stmt=ExpressionsStmt()) != null | 259 || (stmt=ExpressionsStmt()) != null |
260 ) { | 260 ) { |
261 stmts.append(stmt.code); | 261 stmts.append(stmt.code); |
262 } | 262 } |
349 parser.begin(); | 349 parser.begin(); |
350 if( !Keyword("break",In.NOTHING) ) | 350 if( !Keyword("break",In.NOTHING) ) |
351 return parser.failure(null); | 351 return parser.failure(null); |
352 if( frame.loops <= 0 ) | 352 if( frame.loops <= 0 ) |
353 throw parser.exception("'break' outside of loop"); | 353 throw parser.exception("'break' outside of loop"); |
354 // return parser.success( new StmtString("break;\n") ); | 354 return parser.success( new StmtString("break;\n") ); |
355 return parser.success( new StmtString(new BreakStmt()) ); | |
356 } | 355 } |
357 | 356 |
358 int forCounter = 0; | 357 int forCounter = 0; |
359 | 358 |
360 private StmtString ForStmt() throws ParseException { | 359 private StmtString ForStmt() throws ParseException { |
388 +"}\n" | 387 +"}\n" |
389 +"$Luan.set($luan," + varsStr + ", "+fnVar+".call($luan) );\n" | 388 +"$Luan.set($luan," + varsStr + ", "+fnVar+".call($luan) );\n" |
390 +"if( " + firstVar.code + "==null ) break;\n" | 389 +"if( " + firstVar.code + "==null ) break;\n" |
391 + loop.code | 390 + loop.code |
392 +"}" | 391 +"}" |
393 +"} catch(BreakException e) {\n" | |
394 +"} finally {\n" | 392 +"} finally {\n" |
395 +"$luan.stackClear("+stackStart+","+symbolsSize()+");\n" | 393 +"$luan.stackClear("+stackStart+","+symbolsSize()+");\n" |
396 +"}\n" | 394 +"}\n" |
397 ; | 395 ; |
398 StmtString stmt = new StmtString(code); | 396 StmtString stmt = new StmtString(code); |
475 ExpString cnd = RequiredExpr(In.NOTHING).expr(); | 473 ExpString cnd = RequiredExpr(In.NOTHING).expr(); |
476 RequiredKeyword("do",In.NOTHING); | 474 RequiredKeyword("do",In.NOTHING); |
477 StmtString loop = RequiredLoopBlock(); | 475 StmtString loop = RequiredLoopBlock(); |
478 RequiredKeyword("end",In.NOTHING); | 476 RequiredKeyword("end",In.NOTHING); |
479 String code = "" | 477 String code = "" |
480 +"try {\n" | 478 +"while( $Luan.checkBoolean(" + cnd.code + ") ) {\n" |
481 +" while( $Luan.checkBoolean(" + cnd.code + ") ) {\n" | |
482 + loop.code | 479 + loop.code |
483 +" }\n" | 480 +"}\n" |
484 +"} catch(BreakException e) {}\n" | |
485 ; | 481 ; |
486 return parser.success( new StmtString(code) ); | 482 return parser.success( new StmtString(code) ); |
487 } | 483 } |
488 | 484 |
489 private StmtString RepeatStmt() throws ParseException { | 485 private StmtString RepeatStmt() throws ParseException { |
492 return parser.failure(null); | 488 return parser.failure(null); |
493 StmtString loop =RequiredLoopBlock(); | 489 StmtString loop =RequiredLoopBlock(); |
494 RequiredKeyword("until",In.NOTHING); | 490 RequiredKeyword("until",In.NOTHING); |
495 ExpString cnd = RequiredExpr(In.NOTHING).expr(); | 491 ExpString cnd = RequiredExpr(In.NOTHING).expr(); |
496 String code = "" | 492 String code = "" |
497 +"try {\n" | 493 +"do {\n" |
498 +" do {\n" | |
499 + loop.code | 494 + loop.code |
500 +" } while( !$Luan.checkBoolean(" + cnd.code + ") );\n" | 495 +"} while( !$Luan.checkBoolean(" + cnd.code + ") );\n" |
501 +"} catch(BreakException e) {}\n" | |
502 ; | 496 ; |
503 return parser.success( new StmtString(code) ); | 497 return parser.success( new StmtString(code) ); |
504 } | 498 } |
505 | 499 |
506 private StmtString RequiredLoopBlock() throws ParseException { | 500 private StmtString RequiredLoopBlock() throws ParseException { |
508 StmtString stmt = RequiredBlock(); | 502 StmtString stmt = RequiredBlock(); |
509 decLoops(); | 503 decLoops(); |
510 return stmt; | 504 return stmt; |
511 } | 505 } |
512 | 506 |
513 private Stmt IfStmt() throws ParseException { | 507 private StmtString IfStmt() throws ParseException { |
514 parser.begin(); | 508 parser.begin(); |
515 if( !Keyword("if",In.NOTHING) ) | 509 if( !Keyword("if",In.NOTHING) ) |
516 return parser.failure(null); | 510 return parser.failure(null); |
517 return parser.success( IfStmt2() ); | 511 StringBuilder sb = new StringBuilder(); |
518 } | 512 ExpString cnd; |
519 | 513 StmtString block; |
520 private Stmt IfStmt2() throws ParseException { | 514 cnd = RequiredExpr(In.NOTHING).expr(); |
521 parser.currentIndex(); | |
522 Expr cnd = expr(exp(RequiredExpr(In.NOTHING))); | |
523 RequiredKeyword("then",In.NOTHING); | 515 RequiredKeyword("then",In.NOTHING); |
524 Stmt thenBlock = stmt(RequiredBlock()); | 516 block = RequiredBlock(); |
525 Stmt elseBlock; | 517 sb.append( "if( $Luan.checkBoolean(" ).append( cnd.code ).append( ") ) {\n" ).append( block.code ); |
526 if( Keyword("elseif",In.NOTHING) ) { | 518 while( Keyword("elseif",In.NOTHING) ) { |
527 elseBlock = IfStmt2(); | 519 cnd = RequiredExpr(In.NOTHING).expr(); |
528 } else { | 520 RequiredKeyword("then",In.NOTHING); |
529 elseBlock = Keyword("else",In.NOTHING) ? stmt(RequiredBlock()) : Stmt.EMPTY; | 521 block = RequiredBlock(); |
530 RequiredKeyword("end",In.NOTHING); | 522 sb.append( "} else if( $Luan.checkBoolean(" ).append( cnd.code ).append( ") ) {\n" ).append( block.code ); |
531 } | 523 } |
532 return new IfStmt(cnd,thenBlock,elseBlock); | 524 if( Keyword("else",In.NOTHING) ) { |
525 block = RequiredBlock(); | |
526 sb.append( "} else {\n" ).append( block.code ); | |
527 } | |
528 RequiredKeyword("end",In.NOTHING); | |
529 sb.append( "}\n" ); | |
530 return parser.success( new StmtString(sb.toString()) ); | |
533 } | 531 } |
534 | 532 |
535 private StmtString SetStmt() throws ParseException { | 533 private StmtString SetStmt() throws ParseException { |
536 parser.begin(); | 534 parser.begin(); |
537 List<Settable> vars = new ArrayList<Settable>(); | 535 List<Settable> vars = new ArrayList<Settable>(); |
554 // throw parser.exception("Expressions expected"); | 552 // throw parser.exception("Expressions expected"); |
555 return parser.failure(null); | 553 return parser.failure(null); |
556 String varsStr = varsToString(vars.toArray(new Settable[0])); | 554 String varsStr = varsToString(vars.toArray(new Settable[0])); |
557 String code = "$Luan.set($luan," + varsStr + "," + values.code + "); "; | 555 String code = "$Luan.set($luan," + varsStr + "," + values.code + "); "; |
558 return parser.success( new StmtString(code) ); | 556 return parser.success( new StmtString(code) ); |
559 //qqq | |
560 } | 557 } |
561 | 558 |
562 private static String varsToString(Settable[] vars) { | 559 private static String varsToString(Settable[] vars) { |
563 StringBuilder sb = new StringBuilder(); | 560 StringBuilder sb = new StringBuilder(); |
564 sb.append( "new Settable[]{" ); | 561 sb.append( "new Settable[]{" ); |
1518 } | 1515 } |
1519 | 1516 |
1520 private ExpString exp(List<ExpString> list) { | 1517 private ExpString exp(List<ExpString> list) { |
1521 switch(list.size()) { | 1518 switch(list.size()) { |
1522 case 0: | 1519 case 0: |
1523 return new ExpString("null",true,false); | 1520 return new ExpString("LuanFunction.NOTHING",false,false); |
1524 case 1: | 1521 case 1: |
1525 return list.get(0).expr(); | 1522 return list.get(0); |
1526 default: | 1523 default: |
1527 int lastI = list.size() - 1; | 1524 int lastI = list.size() - 1; |
1528 StringBuilder sb = new StringBuilder(); | 1525 StringBuilder sb = new StringBuilder(); |
1529 sb.append( "new Object[]{" ); | 1526 sb.append( "new Object[]{" ); |
1530 for( int i=0; i<lastI; i++ ) { | 1527 for( int i=0; i<lastI; i++ ) { |