changeset 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
files core/src/luan/impl/ExpList.java core/src/luan/impl/LuanParser.java
diffstat 2 files changed, 28 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
diff -r 067d9470184d -r 538b0ae08faa core/src/luan/impl/ExpList.java
--- a/core/src/luan/impl/ExpList.java	Mon Apr 04 23:26:06 2016 -0600
+++ b/core/src/luan/impl/ExpList.java	Tue Apr 05 13:01:04 2016 -0600
@@ -17,7 +17,7 @@
 		}
 	};
 
-	static Expr[] toArray(List<Expressions> list) {
+	private static Expr[] toArray(List<Expressions> list) {
 		Expr[] a = new Expr[list.size()];
 		for( int i=0; i<a.length; i++ ) {
 			Expressions exprs = list.get(i);
diff -r 067d9470184d -r 538b0ae08faa core/src/luan/impl/LuanParser.java
--- a/core/src/luan/impl/LuanParser.java	Mon Apr 04 23:26:06 2016 -0600
+++ b/core/src/luan/impl/LuanParser.java	Tue Apr 05 13:01:04 2016 -0600
@@ -254,7 +254,7 @@
 			|| (stmt=DoStmt()) != null
 			|| (stmt=WhileStmt()) != null
 			|| (stmt=RepeatStmt()) != null
-			|| (stmt=stmtStr(IfStmt())) != null
+			|| (stmt=IfStmt()) != null
 			|| (stmt=SetStmt()) != null
 			|| (stmt=ExpressionsStmt()) != null
 		) {
@@ -351,8 +351,7 @@
 			return parser.failure(null);
 		if( frame.loops <= 0 )
 			throw parser.exception("'break' outside of loop");
-//		return parser.success( new StmtString("break;\n") );
-		return parser.success( new StmtString(new BreakStmt()) );
+		return parser.success( new StmtString("break;\n") );
 	}
 
 	int forCounter = 0;
@@ -390,7 +389,6 @@
 			+"if( " + firstVar.code + "==null )  break;\n"
 			+ loop.code
 			+"}"
-			+"} catch(BreakException e) {\n"
 			+"} finally {\n"
 			+"$luan.stackClear("+stackStart+","+symbolsSize()+");\n"
 			+"}\n"
@@ -477,11 +475,9 @@
 		StmtString loop = RequiredLoopBlock();
 		RequiredKeyword("end",In.NOTHING);
 		String code = ""
-			+"try {\n"
-			+"	while( $Luan.checkBoolean(" + cnd.code + ") ) {\n"
+			+"while( $Luan.checkBoolean(" + cnd.code + ") ) {\n"
 			+ loop.code
-			+"	}\n"
-			+"} catch(BreakException e) {}\n"
+			+"}\n"
 		;
 		return parser.success( new StmtString(code) );
 	}
@@ -494,11 +490,9 @@
 		RequiredKeyword("until",In.NOTHING);
 		ExpString cnd = RequiredExpr(In.NOTHING).expr();
 		String code = ""
-			+"try {\n"
-			+"	do {\n"
+			+"do {\n"
 			+ loop.code
-			+"	} while( !$Luan.checkBoolean(" + cnd.code + ") );\n"
-			+"} catch(BreakException e) {}\n"
+			+"} while( !$Luan.checkBoolean(" + cnd.code + ") );\n"
 		;
 		return parser.success( new StmtString(code) );
 	}
@@ -510,26 +504,30 @@
 		return stmt;
 	}
 
-	private Stmt IfStmt() throws ParseException {
+	private StmtString IfStmt() throws ParseException {
 		parser.begin();
 		if( !Keyword("if",In.NOTHING) )
 			return parser.failure(null);
-		return parser.success( IfStmt2() );
-	}
-
-	private Stmt IfStmt2() throws ParseException {
-		parser.currentIndex();
-		Expr cnd = expr(exp(RequiredExpr(In.NOTHING)));
+		StringBuilder sb = new StringBuilder();
+		ExpString cnd;
+		StmtString block;
+		cnd = RequiredExpr(In.NOTHING).expr();
 		RequiredKeyword("then",In.NOTHING);
-		Stmt thenBlock = stmt(RequiredBlock());
-		Stmt elseBlock;
-		if( Keyword("elseif",In.NOTHING) ) {
-			elseBlock = IfStmt2();
-		} else {
-			elseBlock = Keyword("else",In.NOTHING) ? stmt(RequiredBlock()) : Stmt.EMPTY;
-			RequiredKeyword("end",In.NOTHING);
+		block = RequiredBlock();
+		sb.append( "if( $Luan.checkBoolean(" ).append( cnd.code ).append( ") ) {\n" ).append( block.code );
+		while( Keyword("elseif",In.NOTHING) ) {
+			cnd = RequiredExpr(In.NOTHING).expr();
+			RequiredKeyword("then",In.NOTHING);
+			block = RequiredBlock();
+			sb.append( "} else if( $Luan.checkBoolean(" ).append( cnd.code ).append( ") ) {\n" ).append( block.code );
 		}
-		return new IfStmt(cnd,thenBlock,elseBlock);
+		if( Keyword("else",In.NOTHING) ) {
+			block = RequiredBlock();
+			sb.append( "} else {\n" ).append( block.code );
+		}
+		RequiredKeyword("end",In.NOTHING);
+		sb.append( "}\n" );
+		return parser.success( new StmtString(sb.toString()) );
 	}
 
 	private StmtString SetStmt() throws ParseException {
@@ -556,7 +554,6 @@
 		String varsStr = varsToString(vars.toArray(new Settable[0]));
 		String code = "$Luan.set($luan," + varsStr + "," + values.code + "); ";
 		return parser.success( new StmtString(code) );
-//qqq
 	}
 
 	private static String varsToString(Settable[] vars) {
@@ -1520,9 +1517,9 @@
 	private ExpString exp(List<ExpString> list) {
 		switch(list.size()) {
 		case 0:
-			return new ExpString("null",true,false);
+			return new ExpString("LuanFunction.NOTHING",false,false);
 		case 1:
-			return list.get(0).expr();
+			return list.get(0);
 		default:
 			int lastI = list.size() - 1;
 			StringBuilder sb = new StringBuilder();