changeset 684:41f791e4206d

bug fixes
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 16 Apr 2016 21:42:19 -0600
parents 67dd1449e354
children 1e4b0bc0202d
files core/src/luan/impl/LuanImpl.java core/src/luan/impl/LuanParser.java
diffstat 2 files changed, 57 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/impl/LuanImpl.java	Thu Apr 14 21:51:55 2016 -0600
+++ b/core/src/luan/impl/LuanImpl.java	Sat Apr 16 21:42:19 2016 -0600
@@ -239,4 +239,12 @@
 		return a.length==0 ? null : a[0];
 	}
 
+	public static String strconcat(String... a) {
+		StringBuilder sb = new StringBuilder();
+		for( String s : a ) {
+			sb.append(s);
+		}
+		return sb.toString();
+	}
+
 }
--- a/core/src/luan/impl/LuanParser.java	Thu Apr 14 21:51:55 2016 -0600
+++ b/core/src/luan/impl/LuanParser.java	Sat Apr 16 21:42:19 2016 -0600
@@ -289,18 +289,21 @@
 	private Stmts RequiredBlock() throws ParseException {
 		Stmts stmts = new Stmts();
 		int stackStart = symbolsSize();
-		boolean isReturn = Stmt(stmts);
-		while( !isReturn && (StmtSep() || TemplateSep(stmts)) ) {
+		do {
 			Spaces();
 			stmts.addNewLines();
-			isReturn = Stmt(stmts);
-		}
+			Stmts stmt = Stmt();
+			if( stmt != null ) {
+				stmts.addAll(stmt);
+				stmts.hasReturn = stmt.hasReturn;
+			}
+		} while( !stmts.hasReturn && (StmtSep() || TemplateSep(stmts)) );
+		Spaces();
 		while( StmtSep() )
 			Spaces();
 		stmts.addNewLines();
 		int stackEnd = symbolsSize();
 		popSymbols( stackEnd - stackStart );
-		stmts.hasReturn = isReturn;
 		return stmts;
 	}
 
@@ -318,7 +321,7 @@
 	}
 
 	private boolean EndOfLine() {
-		if( parser.match( "\r\n" ) || parser.match( '\r' ) || parser.match( '\n' ) ) {
+		if( MatchEndOfLine() ) {
 			parser.sb().append('\n');
 			return true;
 		} else {
@@ -326,13 +329,14 @@
 		}
 	}
 
-	private boolean Stmt(Stmts stmts) throws ParseException {
+	private boolean MatchEndOfLine() {
+		return parser.match( "\r\n" ) || parser.match( '\r' ) || parser.match( '\n' );
+	}
+
+	private Stmts Stmt() throws ParseException {
 		Stmts stmt;
-		if( (stmt=ReturnStmt()) != null ) {
-			stmts.addAll(stmt);
-			return true;
-		}
-		if( (stmt=FunctionStmt()) != null
+		if( (stmt=ReturnStmt()) != null
+			|| (stmt=FunctionStmt()) != null
 			|| (stmt=LocalStmt()) != null
 			|| (stmt=LocalFunctionStmt()) != null
 			|| (stmt=BreakStmt()) != null
@@ -344,9 +348,9 @@
 			|| (stmt=SetStmt()) != null
 			|| (stmt=ExpressionsStmt()) != null
 		) {
-			stmts.addAll(stmt);
+			return stmt;
 		}
-		return false;
+		return null;
 	}
 
 	private Expr indexExpStr(Expr exp1,Expr exp2) {
@@ -427,6 +431,7 @@
 		else
 			stmt.add( "LuanFunction.NOTHING" );
 		stmt.add( ";  " );
+		stmt.hasReturn = true;
 		return parser.success( stmt );
 	}
 
@@ -623,6 +628,7 @@
 		Stmts stmt = new Stmts();
 		Expr cnd;
 		Stmts block;
+		boolean hasReturn = true;
 		cnd = RequiredExpr(In.NOTHING).single();
 		RequiredKeyword("then");
 		block = RequiredBlock();
@@ -630,6 +636,8 @@
 		stmt.addAll( cnd );
 		stmt.add( ") ) {  " );
 		stmt.addAll( block );
+		if( !block.hasReturn )
+			hasReturn = false;
 		while( Keyword("elseif") ) {
 			cnd = RequiredExpr(In.NOTHING).single();
 			RequiredKeyword("then");
@@ -638,14 +646,21 @@
 			stmt.addAll( cnd );
 			stmt.add( ") ) {  " );
 			stmt.addAll( block );
+			if( !block.hasReturn )
+				hasReturn = false;
 		}
 		if( Keyword("else") ) {
 			block = RequiredBlock();
 			stmt.add( "} else {  " );
 			stmt.addAll( block );
+			if( !block.hasReturn )
+				hasReturn = false;
+		} else {
+			hasReturn = false;
 		}
 		RequiredKeyword("end");
 		stmt.add( "}  " );
+		stmt.hasReturn = hasReturn;
 		return parser.success( stmt );
 	}
 
@@ -1488,6 +1503,8 @@
 		return parser.failure(null);
 	}
 
+	private static int STR_LIM = 65000;
+
 	private Expr constExpStr(String s) {
 		int n = 0;
 		int from = 0;
@@ -1502,7 +1519,22 @@
 			.replace("\t","\\t")
 			.replace("\b","\\b")
 		;
-		s = "\"" + s + "\"";
+		if( s.length() > STR_LIM ) {
+			int len = s.length();
+			StringBuilder sb = new StringBuilder();
+			sb.append( "LuanImpl.strconcat(" );
+			int start = 0;
+			while(true) {
+				int end = start + STR_LIM;
+				if( end >= len )
+					break;
+				sb.append( "\"" ).append( s.substring(start,end) ).append( "\"," );
+				start = end;
+			}
+			sb.append( "\"" ).append( s.substring(start) ).append( "\")" );
+			s = sb.toString();
+		} else
+			s = "\"" + s + "\"";
 		while( n-- > 0 )
 			s += "\n";
 		Expr exp = new Expr(Val.SINGLE,false);
@@ -1652,7 +1684,7 @@
 		int nEquals = parser.currentIndex() - start;
 		if( !parser.match('[') )
 			return parser.failure(null);
-		EndOfLine();
+		MatchEndOfLine();
 		start = parser.currentIndex();
 		while( !LongBracketsEnd(nEquals) ) {
 			if( !parser.anyChar() )
@@ -1701,8 +1733,7 @@
 			if( Digit() ) Digit();  // optional
 			return parser.success((char)Integer.parseInt(parser.textFrom(start)));
 		}
-		if( EndOfLine() ) {
-			parser.sb().setLength(parser.sb().length()-1);
+		if( MatchEndOfLine() ) {
 			return parser.success('\n');
 		}
 		return parser.failure(null);