changeset 1665:eb89db694225

fix repeat-until scoping
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 06 May 2022 18:51:48 -0600
parents 35e412677512
children 8f38abaf779f
files src/luan/impl/LuanParser.java
diffstat 1 files changed, 23 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
diff -r 35e412677512 -r eb89db694225 src/luan/impl/LuanParser.java
--- a/src/luan/impl/LuanParser.java	Thu May 05 18:41:52 2022 -0600
+++ b/src/luan/impl/LuanParser.java	Fri May 06 18:51:48 2022 -0600
@@ -294,8 +294,15 @@
 	}
 
 	private Stmts RequiredBlock() throws ParseException {
+		int stackStart = symbolsSize();
+		Stmts stmts = RequiredStmts();
+		int stackEnd = symbolsSize();
+		popSymbols( stackEnd - stackStart );
+		return stmts;
+	}
+
+	private Stmts RequiredStmts() throws ParseException {
 		Stmts stmts = new Stmts();
-		int stackStart = symbolsSize();
 		do {
 			Spaces();
 			stmts.addNewLines();
@@ -309,8 +316,6 @@
 		while( StmtSep() )
 			Spaces();
 		stmts.addNewLines();
-		int stackEnd = symbolsSize();
-		popSymbols( stackEnd - stackStart );
 		return stmts;
 	}
 
@@ -517,9 +522,7 @@
 		Expr fnExp = new Expr(null,false);
 		fnExp.add( fnVar + ".call(luan)" );
 		Stmts stmt = new Stmts();
-		stmt.add( ""
-			+"LuanFunction "+fnVar+" = Luan.checkFunction("
-		);
+		stmt.add( "LuanFunction "+fnVar+" = Luan.checkFunction(" );
 		stmt.addAll( expr );
 		stmt.add( ");  " );
 		stmt.add( "while(true) {  " );
@@ -619,18 +622,30 @@
 		return parser.success( stmt );
 	}
 
+	int repeatCounter = 0;
+
 	private Stmts RepeatStmt() throws ParseException {
 		parser.begin();
 		if( !Keyword("repeat") )
 			return parser.failure(null);
-		Stmts loop = RequiredLoopBlock();
+		int stackStart = symbolsSize();
+		incLoops();
+		Stmts loop = RequiredStmts();
+		decLoops();
 		RequiredKeyword("until");
 		Expr cnd = RequiredExpr().single();
+		int stackEnd = symbolsSize();
+		popSymbols( stackEnd - stackStart );
+		String repeatVar = "repeat" + ++repeatCounter;
 		Stmts stmt = new Stmts();
+		stmt.add( "boolean "+repeatVar+";  " );
 		stmt.add( "do {  " );
 		stmt.addAll( loop );
+		stmt.add( repeatVar+" = " );
+		stmt.addAll( cnd );
+		stmt.add( ";  " );
 		stmt.add( "} while( !Luan.checkBoolean(" );
-		stmt.addAll( cnd );
+		stmt.add( repeatVar );
 		stmt.add( ") );  " );
 		return parser.success( stmt );
 	}