changeset 28:df923e5835b2

fix local initial assignment git-svn-id: https://luan-java.googlecode.com/svn/trunk@29 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 07 Dec 2012 05:49:17 +0000
parents 9de9be530625
children 9bc66d09ea48
files src/luan/interp/LuaParser.java
diffstat 1 files changed, 38 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
diff -r 9de9be530625 -r df923e5835b2 src/luan/interp/LuaParser.java
--- a/src/luan/interp/LuaParser.java	Thu Dec 06 20:12:43 2012 +0000
+++ b/src/luan/interp/LuaParser.java	Fri Dec 07 05:49:17 2012 +0000
@@ -69,6 +69,13 @@
 		return true;
 	}
 
+	boolean addSymbols(List<String> names) {
+		frame.symbols.addAll(names);
+		if( frame.stackSize < symbolsSize() )
+			frame.stackSize = symbolsSize();
+		return true;
+	}
+
 	int index(String name) {
 		List<String> symbols = frame.symbols;
 		int i = symbols.size();
@@ -205,7 +212,10 @@
 
 	Rule LocalFunctionStmt() {
 		return Sequence(
-			Keyword("local"), Keyword("function"), LocalName(), Function(),
+			Keyword("local"), Keyword("function"),
+			Name(),
+			addSymbol( (String)pop() ),
+			Function(),
 			push( new SetStmt( new SetLocalVar(symbolsSize()-1), expr(pop()) ) )
 		);
 	}
@@ -220,8 +230,11 @@
 
 	Rule GenericForStmt() {
 		Var<Integer> stackStart = new Var<Integer>(symbolsSize());
+		Var<List<String>> names = new Var<List<String>>(new ArrayList<String>());
 		return Sequence(
-			Keyword("for"), NameList(), Keyword("in"), Expr(), Keyword("do"), LoopBlock(), Keyword("end"),
+			Keyword("for"), NameList(names), Keyword("in"), Expr(), Keyword("do"),
+			addSymbols(names.get()),
+			LoopBlock(), Keyword("end"),
 			push( new GenericForStmt( stackStart.get(), symbolsSize() - stackStart.get(), expr(pop(1)), (Stmt)pop() ) ),
 			popSymbols( symbolsSize() - stackStart.get() )
 		);
@@ -250,35 +263,32 @@
 	}
 
 	Rule LocalStmt(Var<List<Stmt>> stmts) {
-		Var<Integer> stackStart = new Var<Integer>(symbolsSize());
+		Var<List<String>> names = new Var<List<String>>(new ArrayList<String>());
 		return Sequence(
-			Keyword("local"), NameList(),
+			Keyword("local"), NameList(names),
 			Optional(
 				'=', Spaces(), ExpList(),
-				stmts.get().add( newSetLocalStmt(stackStart.get()) )
+				stmts.get().add( newSetLocalStmt(names.get().size()) )
+			),
+			addSymbols(names.get())
+		);
+	}
+
+	Rule NameList(Var<List<String>> names) {
+		return Sequence(
+			Name(),
+			names.get().add( (String)pop() ),
+			ZeroOrMore(
+				',', Spaces(), Name(),
+				names.get().add( (String)pop() )
 			)
 		);
 	}
 
-	Rule NameList() {
-		return Sequence(
-			LocalName(),
-			ZeroOrMore(
-				',', Spaces(), LocalName()
-			)
-		);
-	}
-
-	Rule LocalName() {
-		return Sequence(
-			Name(),
-			addSymbol( (String)pop() )
-		);
-	}
-
-	SetStmt newSetLocalStmt(int stackStart) {
+	SetStmt newSetLocalStmt(int nVars) {
 		Expressions values = (Expressions)pop();
-		SetLocalVar[] vars = new SetLocalVar[symbolsSize()-stackStart];
+		SetLocalVar[] vars = new SetLocalVar[nVars];
+		int stackStart = symbolsSize();
 		for( int i=0; i<vars.length; i++ ) {
 			vars[i] = new SetLocalVar(stackStart+i);
 		}
@@ -491,12 +501,16 @@
 	}
 
 	Rule Function() {
+		Var<List<String>> names = new Var<List<String>>(new ArrayList<String>());
 		return Sequence(
 			action( frame = new Frame(frame) ),
 			'(', incParens(), Spaces(),
 			Optional(
 				FirstOf(
-					Sequence( NameList(), Optional( ',', Spaces(), VarArgName() ) ),
+					Sequence(
+						NameList(names), addSymbols(names.get()),
+						Optional( ',', Spaces(), VarArgName() )
+					),
 					VarArgName()
 				)
 			),