changeset 660:e064377994b2

compile table put
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 06 Apr 2016 21:06:29 -0600
parents f1150518c467
children 1bbb08c0d8f1
files core/src/luan/impl/LuanImpl.java core/src/luan/impl/LuanParser.java
diffstat 2 files changed, 68 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/impl/LuanImpl.java	Tue Apr 05 20:30:42 2016 -0600
+++ b/core/src/luan/impl/LuanImpl.java	Wed Apr 06 21:06:29 2016 -0600
@@ -8,6 +8,7 @@
 import luan.LuanTable;
 import luan.LuanFunction;
 import luan.LuanException;
+import luan.modules.JavaLuan;
 
 
 public final class LuanImpl {
@@ -211,6 +212,18 @@
 		}
 	}
 
+	public static void put(LuanState luan,Object t,Object key,Object value) throws LuanException {
+		if( t instanceof LuanTable ) {
+			LuanTable tbl = (LuanTable)t;
+			tbl.put(luan,key,value);
+			return;
+		}
+		if( t != null && luan.hasJava() )
+			JavaLuan.__new_index(luan,t,key,value);
+		else
+			throw new LuanException( "attempt to index a " + Luan.type(t) + " value" );
+	}
+
 	public static Object[] concatArgs(Object o1,Object o2) {
 		if( o1 instanceof Object[] ) {
 			Object[] a1 = (Object[])o1;
--- a/core/src/luan/impl/LuanParser.java	Tue Apr 05 20:30:42 2016 -0600
+++ b/core/src/luan/impl/LuanParser.java	Wed Apr 06 21:06:29 2016 -0600
@@ -248,7 +248,7 @@
 			return;
 		StmtString stmt;
 		if( (stmt=ReturnStmt()) != null
-			|| (stmt=stmtStr(FunctionStmt())) != null
+			|| (stmt=FunctionStmt()) != null
 			|| (stmt=stmtStr(LocalFunctionStmt())) != null
 			|| (stmt=BreakStmt()) != null
 			|| (stmt=ForStmt()) != null
@@ -338,7 +338,7 @@
 		return parser.success( new StmtString(code) );
 	}
 
-	private Stmt FunctionStmt() throws ParseException {
+	private StmtString FunctionStmt() throws ParseException {
 		parser.begin();
 		if( !Keyword("function",In.NOTHING) )
 			return parser.failure(null);
@@ -352,10 +352,9 @@
 				return parser.failure(null);
 			var = indexVar( var.exp(), exp );
 		}
-		Settable fnName = var.settable();
 
 		FnDef fnDef = RequiredFunction(In.NOTHING);
-		return parser.success( new SetStmt(fnName,fnDef) );
+		return parser.success( var.set(new ExpString(fnDef)) );
 	}
 
 	private Stmt LocalFunctionStmt() throws ParseException {
@@ -555,17 +554,17 @@
 
 	private StmtString SetStmt() throws ParseException {
 		parser.begin();
-		List<Settable> vars = new ArrayList<Settable>();
-		Settable s = SettableVar();
-		if( s == null )
+		List<Var> vars = new ArrayList<Var>();
+		Var v = SettableVar();
+		if( v == null )
 			return parser.failure(null);
-		vars.add(s);
+		vars.add(v);
 		while( parser.match( ',' ) ) {
 			Spaces(In.NOTHING);
-			s = SettableVar();
-			if( s == null )
+			v = SettableVar();
+			if( v == null )
 				return parser.failure(null);
-			vars.add(s);
+			vars.add(v);
 		}
 		if( !parser.match( '=' ) )
 			return parser.failure(null);
@@ -574,9 +573,17 @@
 		if( values==null )
 //			throw parser.exception("Expressions expected");
 			return parser.failure(null);
-		String varsStr = varsToString(vars.toArray(new Settable[0]));
-		String code = "LuanImpl.set(luan," + varsStr + "," + values.code + "); ";
-		return parser.success( new StmtString(code) );
+		int n = vars.size();
+		if( n == 1 )
+			return parser.success( vars.get(0).set(values) );
+		StringBuilder sb = new StringBuilder();
+		sb.append( "t = " + values.code + ";\n" );
+		ExpString t = new ExpString("t",true,false);
+		sb.append( vars.get(0).set(new ExpString("t",true,false)).code );
+		for( int i=1; i<n; i++ ) {
+			sb.append( vars.get(0).set(new ExpString("LuanImpl.pick(t,"+i+")",true,false)).code );
+		}
+		return parser.success( new StmtString(sb.toString()) );
 	}
 
 	private static String varsToString(Settable[] vars) {
@@ -602,12 +609,12 @@
 		return parser.failure(null);
 	}
 
-	private Settable SettableVar() throws ParseException {
+	private Var SettableVar() throws ParseException {
 		int start = parser.begin();
 		Var var = VarZ(In.NOTHING);
-		if( var==null )
+		if( var==null || !var.isSettable() )
 			return parser.failure(null);
-		return parser.success( var.settable() );
+		return parser.success( var );
 	}
 
 	private ExpString RequiredExpr(In in) throws ParseException {
@@ -627,7 +634,7 @@
 		while( Keyword("or",in) ) {
 			exp = exp.expr();
 			ExpString exp2 = required(RelExpr(in)).expr();
-			String code = "(LuanImpl.cnd(cnd = " + exp.code + ") ? cnd : (" + exp2.code + "))";
+			String code = "(LuanImpl.cnd(t = " + exp.code + ") ? t : (" + exp2.code + "))";
 			exp = new ExpString(code,true,true);
 		}
 		return parser.success(exp);
@@ -641,7 +648,7 @@
 		while( Keyword("and",in) ) {
 			exp = exp.expr();
 			ExpString exp2 = required(RelExpr(in)).expr();
-			String code = "(LuanImpl.cnd(cnd = " + exp.code + ") ? (" + exp2.code + ") : cnd)";
+			String code = "(LuanImpl.cnd(t = " + exp.code + ") ? (" + exp2.code + ") : t)";
 			exp = new ExpString(code,true,true);
 		}
 		return parser.success(exp);
@@ -979,7 +986,9 @@
 
 	private interface Var {
 		public ExpString exp() throws ParseException;
-		public Settable settable() throws ParseException;
+//		public Settable settable() throws ParseException;
+		public boolean isSettable();
+		public StmtString set(ExpString val) throws ParseException;
 	}
 
 	private Expr env() {
@@ -1009,7 +1018,11 @@
 				throw parser.exception("name '"+name+"' not defined");
 			}
 
-			public Settable settable() throws ParseException {
+			public boolean isSettable() {
+				return true;
+			}
+
+			private Settable settable() throws ParseException {
 				int index = stackIndex(name);
 				if( index != -1 )
 					return new SetLocalVar(index);
@@ -1022,6 +1035,11 @@
 				parser.failure(null);
 				throw parser.exception("name '"+name+"' not defined");
 			}
+
+			public StmtString set(ExpString val) throws ParseException {
+				String code = new SettableString(settable()).code + ".set(luan," + val.expr().code + ");\n";
+				return new StmtString(code);
+			}
 		};
 	}
 
@@ -1032,8 +1050,12 @@
 				return expr;
 			}
 
-			public Settable settable() {
-				return null;
+			public boolean isSettable() {
+				return false;
+			}
+
+			public StmtString set(ExpString val) {
+				throw new RuntimeException();
 			}
 		};
 	}
@@ -1045,9 +1067,18 @@
 				return indexExpStr( table, key );
 			}
 
+			public boolean isSettable() {
+				return true;
+			}
+/*
 			public Settable settable() {
 				return new SetTableEntry(expr(LuanParser.exp(table)),expr(LuanParser.exp(key)));
 			}
+*/
+			public StmtString set(ExpString val) {
+				String code = "LuanImpl.put(luan," + table.expr().code + "," + key.expr().code + "," + val.expr().code + ");\n";
+				return new StmtString(code);
+			}
 		};
 	}
 
@@ -1503,40 +1534,6 @@
 		ExpString expr() {
 			return isExpr ? this : new ExpString( "Luan.first(" + code + ")", true, isStmt );
 		}
-
-		Expressions toExpressions() {
-			String superClass = isStmt ? "StmtExp" : "Expressions";
-			String className = "EXP" + ++classCounter;
-			String classCode = ""
-				+"package luan.impl;\n"
-				+"import luan.Luan;\n"
-				+"import luan.LuanFunction;\n"
-				+"import luan.LuanException;\n"
-				+"import luan.modules.PackageLuan;\n"
-				+"\n"
-				+"public class " + className +" implements " + superClass + " {\n"
-				+"	@Override public Object eval(LuanStateImpl luan) throws LuanException {\n"
-				+"		Object cnd;\n"
-				+"		return " + code + ";\n"
-				+"	}\n"
-				+"}\n"
-			;
-//System.out.println(code);
-			try {
-				Class cls = LuanJavaCompiler.compile("luan.impl."+className,code,classCode);
-				return (Expressions)cls.newInstance();
-			} catch(ClassNotFoundException e) {
-				throw new RuntimeException(e);
-			} catch(InstantiationException e) {
-				throw new RuntimeException(e);
-			} catch(IllegalAccessException e) {
-				throw new RuntimeException(e);
-			}
-		}
-	}
-
-	private static Expressions exp(ExpString expStr) {
-		return expStr==null ? null : expStr.toExpressions();
 	}
 
 	private ExpString expString(List<ExpString> list) {
@@ -1589,7 +1586,7 @@
 				+"\n"
 				+"public class " + className +" implements Stmt {\n"
 				+"	@Override public void eval(LuanStateImpl luan) throws LuanException {\n"
-				+"		Object cnd;\n"
+				+"		Object t;\n"
 				+"		" + code
 				+"	}\n"
 				+"}\n"