changeset 655:e2be71451d04

compile TableExpr
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 05 Apr 2016 15:28:07 -0600
parents ea7dbd2dfa65
children 471be4752b9e
files core/src/luan/impl/LuanImpl.java core/src/luan/impl/LuanParser.java core/src/luan/impl/TableField.java
diffstat 3 files changed, 47 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/impl/LuanImpl.java	Tue Apr 05 14:16:14 2016 -0600
+++ b/core/src/luan/impl/LuanImpl.java	Tue Apr 05 15:28:07 2016 -0600
@@ -242,4 +242,23 @@
 		}
 	}
 
+	public static LuanTable table(Object o) {
+		LuanTable table = new LuanTable();
+		int i = 0;
+		for( Object fld : Luan.array(o) ) {
+			if( fld instanceof TableField ) {
+				TableField tblFld = (TableField)fld;
+				Object key = tblFld.key;
+				Object value = tblFld.value;
+				if( key != null && value != null )
+					table.rawPut(key,value);
+			} else {
+				i++;
+				if( fld != null )
+					table.rawPut(i,fld);
+			}
+		}
+		return table;
+	}
+
 }
--- a/core/src/luan/impl/LuanParser.java	Tue Apr 05 14:16:14 2016 -0600
+++ b/core/src/luan/impl/LuanParser.java	Tue Apr 05 15:28:07 2016 -0600
@@ -867,43 +867,45 @@
 		return parser.success( new VarArgs() );
 	}
 
-	private Expr TableExpr(In in) throws ParseException {
+	private ExpString TableExpr(In in) throws ParseException {
 		parser.begin();
 		if( !parser.match('{') )
 			return parser.failure(null);
 		Spaces(In.NOTHING);
-		List<TableExpr.Field> fields = new ArrayList<TableExpr.Field>();
-		List<Expressions> builder = new ArrayList<Expressions>();
-		Field(fields,builder);
+		List<ExpString> builder = new ArrayList<ExpString>();
+		Field(builder);
 		while( FieldSep() ) {
 			Spaces(In.NOTHING);
-			Field(fields,builder);
+			Field(builder);
 		}
-		Expressions exp = exp(TemplateExpressions(In.NOTHING));
+		ExpString exp = TemplateExpressions(In.NOTHING);
 		if( exp != null )
 			builder.add(exp);
 		if( !parser.match('}') )
 			throw parser.exception("Expected table element or '}'");
 		Spaces(in);
-		return parser.success( new TableExpr( fields.toArray(new TableExpr.Field[0]), ExpList.build(builder) ) );
+		String code = "LuanImpl.table(" + expString(builder).code + ")";
+		return parser.success( new ExpString(code,true,false) );
 	}
 
 	private boolean FieldSep() throws ParseException {
 		return parser.anyOf(",;") || EndOfLine();
 	}
 
-	private boolean Field(List<TableExpr.Field> fields,List<Expressions> builder) throws ParseException {
+	private boolean Field(List<ExpString> builder) throws ParseException {
 		parser.begin();
 		Expr exp = SubExpr(In.NOTHING);
 		if( exp==null )
 			exp = NameExpr(In.NOTHING);
 		if( exp!=null && parser.match('=') ) {
 			Spaces(In.NOTHING);
-			fields.add( new TableExpr.Field( exp, required(expr(exp(ExprZ(In.NOTHING)))) ) );
+			ExpString val = RequiredExpr(In.NOTHING).expr();
+			String code = "new TableField(" + new ExpString(exp).code + "," + val.code + ")";
+			builder.add( new ExpString(code,true,false) );
 			return parser.success();
 		}
 		parser.rollback();
-		Expressions exprs = exp(ExprZ(In.NOTHING));
+		ExpString exprs = ExprZ(In.NOTHING);
 		if( exprs != null ) {
 			builder.add(exprs);
 			return parser.success();
@@ -941,7 +943,7 @@
 		if( name != null )
 			return nameVar(name);
 		Expressions exp;
-		exp = TableExpr(in);
+		exp = exp(TableExpr(in));
 		if( exp != null )
 			return exprVar(exp);
 		exp = Literal(in);
@@ -1060,9 +1062,9 @@
 			Spaces(in);
 			return parser.success();
 		}
-		Expr exp = TableExpr(in);
+		ExpString exp = TableExpr(in);
 		if( exp != null ) {
-			builder.add(new ExpString(exp));
+			builder.add(exp);
 			return parser.success();
 		}
 		String s = StringLiteral(in);
@@ -1502,6 +1504,7 @@
 			String classCode = ""
 				+"package luan.impl;\n"
 				+"import luan.Luan;\n"
+				+"import luan.LuanFunction;\n"
 				+"import luan.LuanException;\n"
 				+"\n"
 				+"public class " + className +" implements " + superClass + " {\n"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/src/luan/impl/TableField.java	Tue Apr 05 15:28:07 2016 -0600
@@ -0,0 +1,12 @@
+package luan.impl;
+
+
+public final class TableField {
+	final Object key;
+	final Object value;
+
+	public TableField(Object key,Object value) {
+		this.key = key;
+		this.value = value;
+	}
+}