Mercurial Hosting > luan
changeset 25:0e406bd9ac7b
fix table constructors
git-svn-id: https://luan-java.googlecode.com/svn/trunk@26 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Thu, 06 Dec 2012 09:36:24 +0000 |
parents | 7ee247560db5 |
children | 409871b33355 |
files | src/luan/interp/LuaParser.java src/luan/interp/TableExpr.java |
diffstat | 2 files changed, 17 insertions(+), 37 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/interp/LuaParser.java Thu Dec 06 04:40:54 2012 +0000 +++ b/src/luan/interp/LuaParser.java Thu Dec 06 09:36:24 2012 +0000 @@ -521,68 +521,42 @@ } Rule TableExpr() { + Var<List<TableExpr.Field>> fields = new Var<List<TableExpr.Field>>(new ArrayList<TableExpr.Field>()); + Var<ExpList.Builder> builder = new Var<ExpList.Builder>(new ExpList.Builder()); return Sequence( '{', incParens(), Spaces(), - push( new ArrayList<TableExpr.Field>() ), - push( 1.0 ), // counter Optional( - Field(), + Field(fields,builder), ZeroOrMore( FieldSep(), - Field() + Field(fields,builder) ), Optional( FieldSep() ) ), '}', decParens(), - push( newTableExpr() ), - Spaces() + Spaces(), + push( new TableExpr( fields.get().toArray(new TableExpr.Field[0]), builder.get().build() ) ) ); } - TableExpr newTableExpr() { - pop(); // counter - @SuppressWarnings("unchecked") - List<TableExpr.Field> list = (List<TableExpr.Field>)pop(); - return new TableExpr(list.toArray(new TableExpr.Field[0])); - } - Rule FieldSep() { return Sequence( AnyOf(",;"), Spaces() ); } - Rule Field() { + Rule Field(Var<List<TableExpr.Field>> fields,Var<ExpList.Builder> builder) { return FirstOf( Sequence( FirstOf( SubExpr(), NameExpr() ), '=', Spaces(), Expr(), - addField() + fields.get().add( new TableExpr.Field( expr(pop(1)), expr(pop()) ) ) ), Sequence( Expr(), - addIndexedField() + addToExpList(builder.get()) ) ); } - boolean addField() { - TableExpr.Field field = new TableExpr.Field( expr(pop(1)), expr(pop()) ); - @SuppressWarnings("unchecked") - List<TableExpr.Field> list = (List<TableExpr.Field>)peek(1); - list.add(field); - return true; - } - - boolean addIndexedField() { - Expr val = expr(pop()); - double i = (Double)pop(); - TableExpr.Field field = new TableExpr.Field( new ConstExpr(new LuaNumber(i)), val ); - push( i + 1 ); - @SuppressWarnings("unchecked") - List<TableExpr.Field> list = (List<TableExpr.Field>)peek(1); - list.add(field); - return true; - } - static Expr expr(Object obj) { if( obj instanceof Expressions ) return new ExpressionsExpr((Expressions)obj); @@ -690,7 +664,6 @@ return true; } - Rule SubExpr() { return Sequence( '[', incParens(), Spaces(), Expr(), ']', decParens(), Spaces() ); }
--- a/src/luan/interp/TableExpr.java Thu Dec 06 04:40:54 2012 +0000 +++ b/src/luan/interp/TableExpr.java Thu Dec 06 09:36:24 2012 +0000 @@ -3,6 +3,7 @@ import luan.LuaException; import luan.LuaState; import luan.LuaTable; +import luan.LuaNumber; final class TableExpr implements Expr { @@ -18,9 +19,11 @@ } private final Field[] fields; + private final Expressions expressions; - TableExpr(Field[] fields) { + TableExpr(Field[] fields,Expressions expressions) { this.fields = fields; + this.expressions = expressions; } @Override public Object eval(LuaState lua) throws LuaException { @@ -28,6 +31,10 @@ for( Field field : fields ) { table.set( field.key.eval(lua), field.value.eval(lua) ); } + Object[] a = expressions.eval(lua); + for( int i=0; i<a.length; i++ ) { + table.set( new LuaNumber(i+1), a[i] ); + } return table; } }