changeset 664:71f8f5075df8

compile FnDef
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 07 Apr 2016 15:11:52 -0600
parents b438a47196bc
children 41f8fdbc3a0a
files core/src/luan/impl/Closure.java core/src/luan/impl/FnDef.java core/src/luan/impl/LuanImpl.java core/src/luan/impl/LuanParser.java core/src/luan/impl/ThemeParser.java core/src/luan/impl/UpValue.java
diffstat 6 files changed, 43 insertions(+), 118 deletions(-) [+]
line wrap: on
line diff
diff -r b438a47196bc -r 71f8f5075df8 core/src/luan/impl/Closure.java
--- a/core/src/luan/impl/Closure.java	Thu Apr 07 00:01:10 2016 -0600
+++ b/core/src/luan/impl/Closure.java	Thu Apr 07 15:11:52 2016 -0600
@@ -57,7 +57,7 @@
 		}
 		Object returnValues;
 		try {
-			return fnDef.block.eval(luan);
+			return fnDef.run(luan);
 		} catch(StackOverflowError e) {
 			throw new LuanException( "stack overflow", e );
 		} finally {
diff -r b438a47196bc -r 71f8f5075df8 core/src/luan/impl/FnDef.java
--- a/core/src/luan/impl/FnDef.java	Thu Apr 07 00:01:10 2016 -0600
+++ b/core/src/luan/impl/FnDef.java	Thu Apr 07 15:11:52 2016 -0600
@@ -3,38 +3,22 @@
 import luan.LuanException;
 
 
-final class FnDef extends CodeImpl implements Expr {
-	final Expressions block;
+public abstract class FnDef extends CodeImpl implements Expr {
 	final int stackSize;
 	final int numArgs;
 	final boolean isVarArg;
 	final UpValue.Getter[] upValueGetters;
 
-	FnDef(Expressions block,int stackSize,int numArgs,boolean isVarArg,UpValue.Getter[] upValueGetters) {
-		this.block = block;
+	public FnDef(int stackSize,int numArgs,boolean isVarArg,UpValue.Getter[] upValueGetters) {
 		this.stackSize = stackSize;
 		this.numArgs = numArgs;
 		this.isVarArg = isVarArg;
 		this.upValueGetters = upValueGetters;
-//		fixReturns(block);
 	}
-/*
-	private static void fixReturns(Stmt stmt) {
-		if( stmt instanceof ReturnStmt ) {
-			ReturnStmt rs = (ReturnStmt)stmt;
-			rs.throwReturnException = false;
-		} else if( stmt instanceof Block ) {
-			Block b = (Block)stmt;
-			fixReturns( b.stmts[b.stmts.length-1] );
-		} else if( stmt instanceof IfStmt ) {
-			IfStmt is = (IfStmt)stmt;
-			fixReturns( is.thenStmt );
-			fixReturns( is.elseStmt );
-		}
-	}
-*/
+
 	@Override public Object eval(LuanStateImpl luan) throws LuanException {
 		return new Closure(luan,this);
 	}
 
+	public abstract Object run(LuanStateImpl luan) throws LuanException;
 }
diff -r b438a47196bc -r 71f8f5075df8 core/src/luan/impl/LuanImpl.java
--- a/core/src/luan/impl/LuanImpl.java	Thu Apr 07 00:01:10 2016 -0600
+++ b/core/src/luan/impl/LuanImpl.java	Thu Apr 07 15:11:52 2016 -0600
@@ -15,42 +15,16 @@
 	private LuanImpl() {}  // never
 
 
-	private static List<Expressions> listExpressions = new ArrayList<Expressions>();
+	private static List list = new ArrayList();
 
-	static int addExpressions(Expressions exp) {
-		int i = listExpressions.size();
-		listExpressions.add(exp);
+	static int addObj(Object obj) {
+		int i = list.size();
+		list.add(obj);
 		return i;
 	}
 
-	public static Expressions getExpressions(int i) {
-		return listExpressions.get(i);
-	}
-
-/*
-	private static List<Stmt> listStmt = new ArrayList<Stmt>();
-
-	static int addStmt(Stmt stmt) {
-		int i = listStmt.size();
-		listStmt.add(stmt);
-		return i;
-	}
-
-	public static Stmt getStmt(int i) {
-		return listStmt.get(i);
-	}
-*/
-
-	private static List<Settable> listSettable = new ArrayList<Settable>();
-
-	static int addSettable(Settable settable) {
-		int i = listSettable.size();
-		listSettable.add(settable);
-		return i;
-	}
-
-	public static Settable getSettable(int i) {
-		return listSettable.get(i);
+	public static Object getObj(int i) {
+		return list.get(i);
 	}
 
 
diff -r b438a47196bc -r 71f8f5075df8 core/src/luan/impl/LuanParser.java
--- 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) {
diff -r b438a47196bc -r 71f8f5075df8 core/src/luan/impl/ThemeParser.java
--- a/core/src/luan/impl/ThemeParser.java	Thu Apr 07 00:01:10 2016 -0600
+++ b/core/src/luan/impl/ThemeParser.java	Thu Apr 07 15:11:52 2016 -0600
@@ -107,7 +107,8 @@
 	}
 
 	private FnDef newFnDef(Stmt stmt) {
-		return new FnDef( /*stmt*/null, frame.stackSize, symbolsSize(), frame.isVarArg, frame.upValueGetters.toArray(NO_UP_VALUE_GETTERS) );
+//		return new FnDef( /*stmt*/null, frame.stackSize, symbolsSize(), frame.isVarArg, frame.upValueGetters.toArray(NO_UP_VALUE_GETTERS) );
+		return null;
 	}
 
 	private int stackIndex(String name) {
diff -r b438a47196bc -r 71f8f5075df8 core/src/luan/impl/UpValue.java
--- a/core/src/luan/impl/UpValue.java	Thu Apr 07 00:01:10 2016 -0600
+++ b/core/src/luan/impl/UpValue.java	Thu Apr 07 15:11:52 2016 -0600
@@ -5,7 +5,7 @@
 import luan.LuanException;
 
 
-final class UpValue implements DeepCloneable {
+public final class UpValue implements DeepCloneable {
 	private Object[] stack;
 	private int index;
 	private boolean isClosed = false;
@@ -56,7 +56,7 @@
 		stack = null;
 	}
 
-	static interface Getter {
+	public static interface Getter {
 		public UpValue get(LuanStateImpl luan) throws LuanException;
 	}