Mercurial Hosting > luan
diff core/src/luan/impl/LuanParser.java @ 664:71f8f5075df8
compile FnDef
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 07 Apr 2016 15:11:52 -0600 |
parents | b438a47196bc |
children | 41f8fdbc3a0a |
line wrap: on
line diff
--- a/core/src/luan/impl/LuanParser.java Thu Apr 07 00:01:10 2016 -0600 +++ b/core/src/luan/impl/LuanParser.java Thu Apr 07 15:11:52 2016 -0600 @@ -177,7 +177,8 @@ private FnDef newFnDef(int start,StmtString stmt) { if( !stmt.hasReturn ) stmt = new StmtString( stmt.code + "return LuanFunction.NOTHING;\n" ); - return new FnDef( toExpressions(stmt), frame.stackSize, symbolsSize(), frame.isVarArg, frame.upValueGetters.toArray(NO_UP_VALUE_GETTERS) ); +// return new FnDef( toExpressions(stmt), frame.stackSize, symbolsSize(), frame.isVarArg, frame.upValueGetters.toArray(NO_UP_VALUE_GETTERS) ); + return toFnDef( stmt, frame.stackSize, symbolsSize(), frame.isVarArg, frame.upValueGetters.toArray(NO_UP_VALUE_GETTERS) ); } FnDef Expression() throws ParseException { @@ -280,18 +281,6 @@ return new ExpString(code,true,false); } - private ExpString constExpStr(String s) { - s = s - .replace("\\","\\\\") - .replace("\"","\\\"") - .replace("\n","\\n") - .replace("\r","\\r") - .replace("\t","\\t") - .replace("\b","\\b") - ; - return new ExpString( "\""+s+"\"", true,false); - } - private ExpString callExpStr(ExpString fn,ExpString args) { String code = "Luan.checkFunction(" + fn.expr().code + ").call(luan,Luan.array(" + args.code + "))"; return new ExpString(code,false,true); @@ -1276,6 +1265,18 @@ return parser.failure(null); } + private ExpString constExpStr(String s) { + s = s + .replace("\\","\\\\") + .replace("\"","\\\"") + .replace("\n","\\n") + .replace("\r","\\r") + .replace("\t","\\t") + .replace("\b","\\b") + ; + return new ExpString( "\""+s+"\"", true,false); + } + private boolean NilLiteral(In in) throws ParseException { return Keyword("nil",in); } @@ -1532,8 +1533,8 @@ ExpString(Expressions exp) { if( exp==null ) throw new NullPointerException(); - int i = LuanImpl.addExpressions(exp); - code = "LuanImpl.getExpressions(" + i + ").eval(luan)"; + int i = LuanImpl.addObj(exp); + code = "((Expressions)LuanImpl.getObj(" + i + ")).eval(luan)"; isExpr = exp instanceof Expr; isStmt = exp instanceof StmtExp; } @@ -1577,56 +1578,17 @@ private static class StmtString { final String code; boolean hasReturn = false; -/* - StmtString(Stmt stmt) { - if( stmt==null ) throw new NullPointerException(); - int i = LuanImpl.addStmt(stmt); - code = "LuanImpl.getStmt(" + i + ").eval(luan);\n"; - } -*/ + StmtString(String code) { this.code = code; if( code.contains("LuanParser") ) throw new RuntimeException("\n"+code); } - - Stmt toStmt() { - 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 Stmt {\n" - +" @Override public void eval(LuanStateImpl luan) throws LuanException {\n" - +" Object t;\n" - +" " + code - +" }\n" - +"}\n" - ; -//System.out.println(code); - try { - Class cls = LuanJavaCompiler.compile("luan.impl."+className,"code",classCode); - return (Stmt)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 StmtString EMPTY_STMT = new StmtString(""); - private static Stmt stmt(StmtString stmtStr) { - return stmtStr==null ? null : stmtStr.toStmt(); - } - - - static Expressions toExpressions(StmtString stmt) { + static FnDef toFnDef(StmtString stmt,int stackSize,int numArgs,boolean isVarArg,UpValue.Getter[] upValueGetters) { + int i = LuanImpl.addObj(upValueGetters); String className = "EXP" + ++classCounter; String classCode = "" +"package luan.impl;\n" @@ -1635,8 +1597,12 @@ +"import luan.LuanException;\n" +"import luan.modules.PackageLuan;\n" +"\n" - +"public class " + className +" implements Expressions {\n" - +" @Override public Object eval(LuanStateImpl luan) throws LuanException {\n" + +"public class " + className +" extends FnDef {\n" + +" public "+className+"() {\n" + +" super("+stackSize+","+numArgs+","+isVarArg+",(UpValue.Getter[])LuanImpl.getObj("+i+"));\n" + +" }\n" + +"\n" + +" @Override public Object run(LuanStateImpl luan) throws LuanException {\n" +" Object t;\n" +" " + stmt.code +" }\n" @@ -1644,7 +1610,7 @@ ; try { Class cls = LuanJavaCompiler.compile("luan.impl."+className,"code",classCode); - return (Expressions)cls.newInstance(); + return (FnDef)cls.newInstance(); } catch(ClassNotFoundException e) { throw new RuntimeException(e); } catch(InstantiationException e) { @@ -1660,8 +1626,8 @@ SettableString(Settable settable) { if( settable==null ) throw new NullPointerException(); - int i = LuanImpl.addSettable(settable); - code = "LuanImpl.getSettable(" + i + ")"; + int i = LuanImpl.addObj(settable); + code = "((Settable)LuanImpl.getObj(" + i + "))"; } SettableString(String code) {