Mercurial Hosting > luan
changeset 671:82f130eba7b0
improve declarations
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 12 Apr 2016 14:44:10 -0600 |
parents | 58ebfec6178b |
children | d3e5414bdf4c |
files | core/src/luan/impl/LuanParser.java core/src/luan/impl/Pointer.java |
diffstat | 2 files changed, 79 insertions(+), 66 deletions(-) [+] |
line wrap: on
line diff
--- a/core/src/luan/impl/LuanParser.java Tue Apr 12 01:05:57 2016 -0600 +++ b/core/src/luan/impl/LuanParser.java Tue Apr 12 14:44:10 2016 -0600 @@ -22,32 +22,54 @@ private class LocalSym implements Sym { final String name; final String javaName; - String pointer = null; + boolean isPointer = false; LocalSym(String name) { this.name = name; this.javaName = name + "_" + (++symCounter); } - String declaration() { - if( pointer==null ) - return "Object " + javaName + " = null;\n"; - else - return "final Pointer " + javaName + " = " + pointer + ";\n"; - } - - String ref() { - if( pointer==null ) - return javaName; - else - return javaName + ".o"; + StmtString declaration(ExpString value) { + StmtString stmt = new StmtString(); + if( value==null ) { + stmt.list.add( new Object() { + @Override public String toString() { + if( !isPointer ) + return "Object " + javaName + ";\n"; + else + return "final Pointer " + javaName + " = new Pointer();\n"; + } + } ); + } else { + stmt.list.add( new Object() { + @Override public String toString() { + if( !isPointer ) + return "Object " + javaName + " = "; + else + return "final Pointer " + javaName + " = new Pointer("; + } + } ); + stmt.list.addAll(value.list); + stmt.list.add( new Object() { + @Override public String toString() { + if( !isPointer ) + return ";\n"; + else + return ");\n"; + } + } ); + } + return stmt; } @Override public ExpString exp() { ExpString exp = new ExpString(true,false); exp.list.add( new Object() { @Override public String toString() { - return ref(); + if( !isPointer ) + return javaName; + else + return javaName + ".o"; } } ); return exp; @@ -69,15 +91,11 @@ return "upValues[" + i + "] = " + value + ";\n"; } - String ref() { - return "upValues[" + i + "].o"; - } - @Override public ExpString exp() { ExpString exp = new ExpString(true,false); exp.list.add( new Object() { @Override public String toString() { - return ref(); + return "upValues[" + i + "].o"; } } ); return exp; @@ -129,7 +147,7 @@ for( Frame f=parent; f!=null; f=f.parent ) { LocalSym sym = f.getLocalSym(name); if( sym != null ) { - sym.pointer = "new Pointer()"; + sym.isPointer = true; return addUpSym(name,sym.javaName); } } @@ -178,8 +196,8 @@ UpSym upSym = frame.addUpSym( "-ADDED-" ,"new Pointer()"); if( name != null ) { LocalSym sym = frame.addLocalSym( name ); - sym.pointer = "upValues[" + upSym.i + "]"; - top.list.add( sym.declaration() ); + sym.isPointer = true; + top.list.add( "final Pointer " + sym.javaName + " = upValues[" + upSym.i + "];\n" ); } } @@ -187,19 +205,9 @@ return frame.symbols.size(); } - private void addSymbol(String name,StmtString stmt) { + private StmtString addSymbol(String name,ExpString value) { final LocalSym sym = frame.addLocalSym(name); - stmt.list.add( new Object() { - @Override public String toString() { - return sym.declaration(); - } - } ); - } - - private void addSymbols(List<String> names,StmtString stmt) { - for( String name : names ) { - addSymbol(name,stmt); - } + return sym.declaration(value); } private Sym getSym(String name) { @@ -446,9 +454,9 @@ return parser.failure(null); StmtString stmt = new StmtString(); String name = RequiredName(In.NOTHING); - addSymbol( name, stmt ); + stmt.list.addAll( addSymbol(name,null).list ); ExpString fnDef = RequiredFunction(In.NOTHING); - stmt.list.addAll( nameVar(name).set( fnDef ).list ); + stmt.list.addAll( nameVar(name).set(fnDef).list ); /* Settable s = new SetLocalVar(symbolsSize()-1); StmtString stmt = new StmtString(); @@ -494,15 +502,9 @@ stmt.list.addAll( expr.list ); stmt.list.add( ");\n" ); stmt.list.add( "while(true) {\n" ); - List<Var> vars = new ArrayList<Var>(); - for( String name : names ) { - addSymbol(name, stmt); - vars.add(nameVar(name)); - } - ExpString firstVar = vars.get(0).exp(); - stmt.list.addAll( makeSetStmt(vars,fnExp).list ); + stmt.list.addAll( makeLocalSetStmt(names,fnExp).list ); stmt.list.add( "if( " ); - stmt.list.addAll( firstVar.list ); + stmt.list.addAll( nameVar(names.get(0)).exp().list ); stmt.list.add( "==null ) break;\n" ); StmtString loop = RequiredLoopBlock(); RequiredKeyword("end",In.NOTHING); @@ -532,17 +534,18 @@ throw parser.exception("Invalid local statement"); } StmtString stmt = new StmtString(); - addSymbols(names,stmt); if( parser.match( '=' ) ) { Spaces(In.NOTHING); ExpString values = ExpStringList(In.NOTHING); if( values==null ) throw parser.exception("Expressions expected"); - List<Var> vars = new ArrayList<Var>(); + stmt.list.addAll( makeLocalSetStmt(names,values).list ); + } else { + ExpString value = new ExpString(true,false); + value.list.add( "null" ); for( String name : names ) { - vars.add(nameVar(name)); + stmt.list.addAll( addSymbol(name,value).list ); } - stmt.list.addAll( makeSetStmt(vars,values).list ); } return parser.success(stmt); } @@ -693,17 +696,26 @@ } return stmt; } -/* - private static String varsToString(Settable[] vars) { - StringBuilder sb = new StringBuilder(); - sb.append( "new Settable[]{" ); - for( Settable v : vars ) { - sb.append( settableToString(v) ).append( ',' ); + + private StmtString makeLocalSetStmt(List<String> names,ExpString values) throws ParseException { + int n = names.size(); + if( n == 1 ) + return addSymbol(names.get(0),values.expr()); + StmtString stmt = new StmtString(); + stmt.list.add( "t = " ); + stmt.list.addAll( values.list ); + stmt.list.add( ";\n" ); + ExpString t = new ExpString(values.isExpr,false); + t.list.add( "t" ); + stmt.list.addAll( addSymbol(names.get(0),t.expr()).list ); + for( int i=1; i<n; i++ ) { + t.list.clear(); + t.list.add( "LuanImpl.pick(t,"+i+")" ); + stmt.list.addAll( addSymbol(names.get(i),t).list ); } - sb.append( "}" ); - return sb.toString(); + return stmt; } -*/ + private StmtString ExpressionsStmt() throws ParseException { parser.begin(); ExpString exp = ExprZ(In.NOTHING); @@ -1047,21 +1059,16 @@ StmtString stmt = new StmtString(); List<String> names = NameList(in); if( names != null ) { - addSymbols(names,stmt); - List<Var> vars = new ArrayList<Var>(); - for( String name : names ) { - vars.add(nameVar(name)); - } ExpString args = new ExpString(false,false); args.list.add( "args" ); - stmt.list.addAll( makeSetStmt(vars,args).list ); + stmt.list.addAll( makeLocalSetStmt(names,args).list ); if( parser.match(',') ) { Spaces(inParens); if( !parser.match("...") ) throw parser.exception(); Spaces(inParens); frame.isVarArg = true; - stmt.list.add( "final Object[] varArgs = LuanImpl.varArgs(args," + vars.size() + ");\n" ); + stmt.list.add( "final Object[] varArgs = LuanImpl.varArgs(args," + names.size() + ");\n" ); } } else if( parser.match("...") ) { Spaces(inParens); @@ -1863,8 +1870,8 @@ +"}\n" ; try { -System.out.println(parser.sourceName); -System.out.println(classCode); +//System.out.println(parser.sourceName); +//System.out.println(classCode); return LuanJavaCompiler.compile("luan.impl."+className,parser.sourceName,classCode); } catch(ClassNotFoundException e) { throw new RuntimeException(e);
--- a/core/src/luan/impl/Pointer.java Tue Apr 12 01:05:57 2016 -0600 +++ b/core/src/luan/impl/Pointer.java Tue Apr 12 14:44:10 2016 -0600 @@ -7,6 +7,12 @@ public final class Pointer implements DeepCloneable { public Object o; + public Pointer() {} + + public Pointer(Object o) { + this.o = o; + } + @Override public Pointer shallowClone() { return new Pointer(); }