diff src/luan/interp/LuaParser.java @ 24:7ee247560db5

add VarArgs git-svn-id: https://luan-java.googlecode.com/svn/trunk@25 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Thu, 06 Dec 2012 04:40:54 +0000
parents c93d8c781853
children 0e406bd9ac7b
line wrap: on
line diff
--- a/src/luan/interp/LuaParser.java	Thu Dec 06 00:35:13 2012 +0000
+++ b/src/luan/interp/LuaParser.java	Thu Dec 06 04:40:54 2012 +0000
@@ -28,6 +28,7 @@
 		final List<String> symbols = new ArrayList<String>();
 		int stackSize = 0;
 		int loops = 0;
+		boolean isVarArg = false;
 
 		Frame(Frame parent) {
 			this.parent = parent;
@@ -105,7 +106,7 @@
 				Sequence(
 					Block(),
 					EOI,
-					push( new Chunk( (Stmt)pop(), frame.stackSize, 0 ) )
+					push( new Chunk( (Stmt)pop(), frame.stackSize, 0, false ) )
 				)
 			)
 		);
@@ -391,7 +392,10 @@
 	}
 
 	Rule Expr() {
-		return OrExpr();
+		return FirstOf(
+			VarArgs(),
+			OrExpr()
+		);
 	}
 
 	Rule OrExpr() {
@@ -488,12 +492,34 @@
 	Rule Function() {
 		return Sequence(
 			action( frame = new Frame(frame) ),
-			'(', incParens(), Spaces(), Optional(NameList()), ')', decParens(), Spaces(), Block(), Keyword("end"),
-			push( new Chunk( (Stmt)pop(), frame.stackSize, symbolsSize() ) ),
+			'(', incParens(), Spaces(),
+			Optional(
+				FirstOf(
+					Sequence( NameList(), Optional( ',', Spaces(), VarArgName() ) ),
+					VarArgName()
+				)
+			),
+			')', decParens(), Spaces(), Block(), Keyword("end"),
+			push( new Chunk( (Stmt)pop(), frame.stackSize, symbolsSize(), frame.isVarArg ) ),
 			action( frame = frame.parent )
 		);
 	}
 
+	Rule VarArgName() {
+		return Sequence(
+			"...", Spaces(),
+			action( frame.isVarArg = true )
+		);
+	}
+
+	Rule VarArgs() {
+		return Sequence(
+			"...", Spaces(),
+			frame.isVarArg,
+			push( VarArgs.INSTANCE )
+		);
+	}
+
 	Rule TableExpr() {
 		return Sequence(
 			'{', incParens(), Spaces(),