changeset 1089:894786a03d22

check for too many arguments
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 29 Dec 2016 21:26:45 -0700
parents bae2d0c2576c
children 616761e0b9f6
files src/luan/impl/LuanImpl.java src/luan/impl/LuanParser.java
diffstat 2 files changed, 27 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
diff -r bae2d0c2576c -r 894786a03d22 src/luan/impl/LuanImpl.java
--- a/src/luan/impl/LuanImpl.java	Mon Dec 26 22:29:36 2016 -0700
+++ b/src/luan/impl/LuanImpl.java	Thu Dec 29 21:26:45 2016 -0700
@@ -174,9 +174,22 @@
 		if( !(o instanceof Object[]) )
 			return null;
 		Object[] a = (Object[])o;
+		return pick(a,i);
+	}
+
+	public static Object pick(Object[] a,int i) {
 		return i<a.length ? a[i] : null;
 	}
 
+	public static void noMore(final Object[] a,final int n) throws LuanException {
+		if( a.length > n ) {
+			for( int i=n; i<a.length; i++ ) {
+				if( a[i] != null )
+					throw new LuanException("too many arguments");
+			}
+		}
+	}
+
 	public static Object[] varArgs(Object[] a,int i) {
 		if( i >= a.length )
 			return LuanFunction.NOTHING;
diff -r bae2d0c2576c -r 894786a03d22 src/luan/impl/LuanParser.java
--- a/src/luan/impl/LuanParser.java	Mon Dec 26 22:29:36 2016 -0700
+++ b/src/luan/impl/LuanParser.java	Thu Dec 29 21:26:45 2016 -0700
@@ -1075,9 +1075,19 @@
 		Stmts stmt = new Stmts();
 		List<String> names = NameList();
 		if( names != null ) {
+/*
 			Expr args = new Expr(Val.ARRAY,false);
 			args.add( "args" );
 			stmt.addAll( makeLocalSetStmt(names,args) );
+*/
+			int n = names.size();
+			Expr t = new Expr(Val.SINGLE,false);
+			for( int i=0; i<n; i++ ) {
+				t.clear();
+				t.add( "LuanImpl.pick(args,"+i+")" );
+				stmt.addAll( addSymbol(names.get(i),t) );
+			}
+
 			if( parser.match(',') ) {
 				Spaces();
 				if( !parser.match("...") )
@@ -1085,11 +1095,15 @@
 				Spaces();
 				frame.isVarArg = true;
 				stmt.add( "final Object[] varArgs = LuanImpl.varArgs(args," + names.size() + ");  " );
+			} else {
+				stmt.add( "LuanImpl.noMore(args,"+n+");  " );
 			}
 		} else if( parser.match("...") ) {
 			Spaces();
 			frame.isVarArg = true;
 			stmt.add( "final Object[] varArgs = LuanImpl.varArgs(args,0);  " );
+		} else {
+			stmt.add( "LuanImpl.noMore(args,0);  " );
 		}
 		RequiredMatch(')');
 		Spaces();