changeset 647:8e8c30b72e9b

move methods from LuanState to Luan
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 29 Mar 2016 20:39:14 -0600
parents cdc70de628b5
children e387e4021afe
files core/src/luan/Luan.java core/src/luan/LuanState.java core/src/luan/LuanTable.java core/src/luan/impl/BinaryOpExpr.java core/src/luan/impl/ConcatExpr.java core/src/luan/impl/EqExpr.java core/src/luan/impl/ForStmt.java core/src/luan/impl/IfStmt.java core/src/luan/impl/LeExpr.java core/src/luan/impl/LtExpr.java core/src/luan/impl/LuanStateImpl.java core/src/luan/impl/NotExpr.java core/src/luan/impl/RepeatStmt.java core/src/luan/impl/ReturnStmt.java core/src/luan/impl/UnmExpr.java core/src/luan/impl/WhileStmt.java core/src/luan/modules/JavaLuan.java core/src/luan/modules/TableLuan.java
diffstat 18 files changed, 79 insertions(+), 77 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/Luan.java	Tue Mar 29 19:58:39 2016 -0600
+++ b/core/src/luan/Luan.java	Tue Mar 29 20:39:14 2016 -0600
@@ -85,5 +85,61 @@
 		return s;
 	}
 
+
+	// from LuanState
+
+	public static Boolean checkBoolean(Object obj) throws LuanException {
+		if( obj instanceof Boolean )
+			return (Boolean)obj;
+		throw new LuanException("attempt to use a " + Luan.type(obj) + " value as a boolean" );
+	}
+
+	public static String checkString(Object obj) throws LuanException {
+		if( obj instanceof String )
+			return (String)obj;
+		throw new LuanException("attempt to use a " + Luan.type(obj) + " value as a string" );
+	}
+
+	public static LuanFunction checkFunction(Object obj) throws LuanException {
+		if( obj instanceof LuanFunction )
+			return (LuanFunction)obj;
+		throw new LuanException("attempt to call a " + Luan.type(obj) + " value" );
+	}
+
+	public static boolean isLessThan(LuanState luan,Object o1,Object o2) throws LuanException {
+		if( o1 instanceof Number && o2 instanceof Number ) {
+			Number n1 = (Number)o1;
+			Number n2 = (Number)o2;
+			return n1.doubleValue() < n2.doubleValue();
+		}
+		if( o1 instanceof String && o2 instanceof String ) {
+			String s1 = (String)o1;
+			String s2 = (String)o2;
+			return s1.compareTo(s2) < 0;
+		}
+		LuanFunction fn = getBinHandler("__lt",o1,o2);
+		if( fn != null )
+			return checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) );
+		throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) );
+	}
+
+	public static LuanFunction getBinHandler(String op,Object o1,Object o2) throws LuanException {
+		if( o1 instanceof LuanTable ) {
+			LuanFunction f1 = getHandlerFunction(op,(LuanTable)o1);
+			if( f1 != null )
+				return f1;
+		}
+		return o2 instanceof LuanTable ? getHandlerFunction(op,(LuanTable)o2) : null;
+	}
+
+	public static LuanFunction getHandlerFunction(String op,LuanTable t) throws LuanException {
+		Object f = t.getHandler(op);
+		if( f == null )
+			return null;
+		return checkFunction(f);
+	}
+
+
+
 	private Luan() {}  // never
 }
--- a/core/src/luan/LuanState.java	Tue Mar 29 19:58:39 2016 -0600
+++ b/core/src/luan/LuanState.java	Tue Mar 29 20:39:14 2016 -0600
@@ -62,26 +62,6 @@
 	}
 
 
-	public Boolean checkBoolean(Object obj) throws LuanException {
-		if( obj instanceof Boolean )
-			return (Boolean)obj;
-		throw new LuanException("attempt to use a " + Luan.type(obj) + " value as a boolean" );
-	}
-
-	public String checkString(Object obj) throws LuanException {
-		if( obj instanceof String )
-			return (String)obj;
-		throw new LuanException("attempt to use a " + Luan.type(obj) + " value as a string" );
-	}
-
-	public LuanFunction checkFunction(Object obj) throws LuanException {
-		if( obj instanceof LuanFunction )
-			return (LuanFunction)obj;
-		throw new LuanException("attempt to call a " + Luan.type(obj) + " value" );
-	}
-
-	abstract public boolean isLessThan(Object o1,Object o2) throws LuanException;
-
 	public String toString(Object obj) throws LuanException {
 		if( obj instanceof LuanTable ) {
 			LuanTable tbl = (LuanTable)obj;
@@ -110,7 +90,7 @@
 	public Number checkNumber(Object obj) throws LuanException {
 		if( obj instanceof Number )
 			return (Number)obj;
-		throw new LuanException(this, "attempt to perform arithmetic on '"+context()+"' (a " + Luan.type(obj) + " value)" );
+		throw new LuanException( "attempt to perform arithmetic on '"+context()+"' (a " + Luan.type(obj) + " value)" );
 	}
 */
 }
--- a/core/src/luan/LuanTable.java	Tue Mar 29 19:58:39 2016 -0600
+++ b/core/src/luan/LuanTable.java	Tue Mar 29 20:39:14 2016 -0600
@@ -95,8 +95,8 @@
 			LuanMeta meta = (LuanMeta)h;
 			return meta.__to_string(luan,this);
 		}
-		LuanFunction fn = luan.checkFunction(h);
-		return luan.checkString( Luan.first( fn.call(luan,new Object[]{this}) ) );
+		LuanFunction fn = Luan.checkFunction(h);
+		return Luan.checkString( Luan.first( fn.call(luan,new Object[]{this}) ) );
 	}
 
 	public String rawToString() {
@@ -247,7 +247,7 @@
 	public int length(LuanState luan) throws LuanException {
 		Object h = getHandler("__len");
 		if( h != null ) {
-			LuanFunction fn = luan.checkFunction(h);
+			LuanFunction fn = Luan.checkFunction(h);
 			return (Integer)Luan.first(fn.call(luan,new Object[]{this}));
 		}
 		return rawLength();
--- a/core/src/luan/impl/BinaryOpExpr.java	Tue Mar 29 19:58:39 2016 -0600
+++ b/core/src/luan/impl/BinaryOpExpr.java	Tue Mar 29 20:39:14 2016 -0600
@@ -16,7 +16,7 @@
 	}
 
 	Object arithmetic(LuanStateImpl luan,String op,Object o1,Object o2) throws LuanException {
-		LuanFunction fn = luan.getBinHandler(op,o1,o2);
+		LuanFunction fn = Luan.getBinHandler(op,o1,o2);
 		if( fn != null )
 			return Luan.first(fn.call(luan,new Object[]{o1,o2}));
 		String type = !(o1 instanceof Number) ? Luan.type(o1) : Luan.type(o2);
--- a/core/src/luan/impl/ConcatExpr.java	Tue Mar 29 19:58:39 2016 -0600
+++ b/core/src/luan/impl/ConcatExpr.java	Tue Mar 29 20:39:14 2016 -0600
@@ -14,7 +14,7 @@
 	@Override public Object eval(LuanStateImpl luan) throws LuanException {
 		Object o1 = op1.eval(luan);
 		Object o2 = op2.eval(luan);
-		LuanFunction fn = luan.getBinHandler("__concat",o1,o2);
+		LuanFunction fn = Luan.getBinHandler("__concat",o1,o2);
 		if( fn != null )
 			return Luan.first(fn.call(luan,new Object[]{o1,o2}));
 		String s1 = luan.toString(o1);
--- a/core/src/luan/impl/EqExpr.java	Tue Mar 29 19:58:39 2016 -0600
+++ b/core/src/luan/impl/EqExpr.java	Tue Mar 29 20:39:14 2016 -0600
@@ -43,8 +43,8 @@
 		Object f = mt1.rawGet("__eq");
 		if( f == null || !f.equals(mt2.rawGet("__eq")) )
 			return false;
-		LuanFunction fn = luan.checkFunction(f);
-		return luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) );
+		LuanFunction fn = Luan.checkFunction(f);
+		return Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) );
 	}
 
 	@Override public String toString() {
--- a/core/src/luan/impl/ForStmt.java	Tue Mar 29 19:58:39 2016 -0600
+++ b/core/src/luan/impl/ForStmt.java	Tue Mar 29 20:39:14 2016 -0600
@@ -21,7 +21,7 @@
 	@Override public void eval(LuanStateImpl luan) throws LuanException {
 		Object fnObj = iterExpr.eval(luan);
 		try {
-			LuanFunction iter = luan.checkFunction(fnObj);
+			LuanFunction iter = Luan.checkFunction(fnObj);
 			while(true) {
 				Object vals = iter.call(luan);
 				if( vals==null )
--- a/core/src/luan/impl/IfStmt.java	Tue Mar 29 19:58:39 2016 -0600
+++ b/core/src/luan/impl/IfStmt.java	Tue Mar 29 20:39:14 2016 -0600
@@ -16,7 +16,7 @@
 	}
 
 	@Override public void eval(LuanStateImpl luan) throws LuanException {
-		if( luan.checkBoolean( cnd.eval(luan) ) ) {
+		if( Luan.checkBoolean( cnd.eval(luan) ) ) {
 			thenStmt.eval(luan);
 		} else {
 			elseStmt.eval(luan);
--- a/core/src/luan/impl/LeExpr.java	Tue Mar 29 19:58:39 2016 -0600
+++ b/core/src/luan/impl/LeExpr.java	Tue Mar 29 20:39:14 2016 -0600
@@ -28,12 +28,12 @@
 			String s2 = (String)o2;
 			return s1.compareTo(s2) <= 0;
 		}
-		LuanFunction fn = luan.getBinHandler("__le",o1,o2);
+		LuanFunction fn = Luan.getBinHandler("__le",o1,o2);
 		if( fn != null )
-			return luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) );
-		fn = luan.getBinHandler("__lt",o1,o2);
+			return Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) );
+		fn = Luan.getBinHandler("__lt",o1,o2);
 		if( fn != null )
-			return !luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o2,o1})) );
+			return !Luan.checkBoolean( Luan.first(fn.call(luan,new Object[]{o2,o1})) );
 		throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) );
 	}
 }
--- a/core/src/luan/impl/LtExpr.java	Tue Mar 29 19:58:39 2016 -0600
+++ b/core/src/luan/impl/LtExpr.java	Tue Mar 29 20:39:14 2016 -0600
@@ -14,6 +14,6 @@
 	@Override public Object eval(LuanStateImpl luan) throws LuanException {
 		Object o1 = op1.eval(luan);
 		Object o2 = op2.eval(luan);
-		return luan.isLessThan(o1,o2);
+		return Luan.isLessThan(luan,o1,o2);
 	}
 }
--- a/core/src/luan/impl/LuanStateImpl.java	Tue Mar 29 19:58:39 2016 -0600
+++ b/core/src/luan/impl/LuanStateImpl.java	Tue Mar 29 20:39:14 2016 -0600
@@ -117,38 +117,4 @@
 	}
 
 
-
-	@Override public boolean isLessThan(Object o1,Object o2) throws LuanException {
-		if( o1 instanceof Number && o2 instanceof Number ) {
-			Number n1 = (Number)o1;
-			Number n2 = (Number)o2;
-			return n1.doubleValue() < n2.doubleValue();
-		}
-		if( o1 instanceof String && o2 instanceof String ) {
-			String s1 = (String)o1;
-			String s2 = (String)o2;
-			return s1.compareTo(s2) < 0;
-		}
-		LuanFunction fn = getBinHandler("__lt",o1,o2);
-		if( fn != null )
-			return checkBoolean( Luan.first(fn.call(this,new Object[]{o1,o2})) );
-		throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) );
-	}
-
-	LuanFunction getBinHandler(String op,Object o1,Object o2) throws LuanException {
-		if( o1 instanceof LuanTable ) {
-			LuanFunction f1 = getHandlerFunction(op,(LuanTable)o1);
-			if( f1 != null )
-				return f1;
-		}
-		return o2 instanceof LuanTable ? getHandlerFunction(op,(LuanTable)o2) : null;
-	}
-
-	LuanFunction getHandlerFunction(String op,LuanTable t) throws LuanException {
-		Object f = t.getHandler(op);
-		if( f == null )
-			return null;
-		return checkFunction(f);
-	}
-
 }
--- a/core/src/luan/impl/NotExpr.java	Tue Mar 29 19:58:39 2016 -0600
+++ b/core/src/luan/impl/NotExpr.java	Tue Mar 29 20:39:14 2016 -0600
@@ -11,7 +11,7 @@
 	}
 
 	@Override public Object eval(LuanStateImpl luan) throws LuanException {
-		return !luan.checkBoolean( op.eval(luan) );
+		return !Luan.checkBoolean( op.eval(luan) );
 	}
 
 	@Override public String toString() {
--- a/core/src/luan/impl/RepeatStmt.java	Tue Mar 29 19:58:39 2016 -0600
+++ b/core/src/luan/impl/RepeatStmt.java	Tue Mar 29 20:39:14 2016 -0600
@@ -17,7 +17,7 @@
 		try {
 			do {
 				doStmt.eval(luan);
-			} while( !luan.checkBoolean( cnd.eval(luan) ) );
+			} while( !Luan.checkBoolean( cnd.eval(luan) ) );
 		} catch(BreakException e) {}
 	}
 }
--- a/core/src/luan/impl/ReturnStmt.java	Tue Mar 29 19:58:39 2016 -0600
+++ b/core/src/luan/impl/ReturnStmt.java	Tue Mar 29 20:39:14 2016 -0600
@@ -18,7 +18,7 @@
 			luan.returnValues = expressions.eval(luan);
 		} else {  // tail call
 			FnCall tailFnCall = (FnCall)expressions;
-			LuanFunction tailFn = luan.checkFunction( tailFnCall.fnExpr.eval(luan) );
+			LuanFunction tailFn = Luan.checkFunction( tailFnCall.fnExpr.eval(luan) );
 			luan.returnValues = tailFnCall.args.eval(luan);
 			if( tailFn instanceof Closure ) {
 				luan.tailFn = (Closure)tailFn;
--- a/core/src/luan/impl/UnmExpr.java	Tue Mar 29 19:58:39 2016 -0600
+++ b/core/src/luan/impl/UnmExpr.java	Tue Mar 29 20:39:14 2016 -0600
@@ -18,7 +18,7 @@
 		if( o instanceof Number )
 			return -((Number)o).doubleValue();
 		if( o instanceof LuanTable ) {
-			LuanFunction fn = luan.getHandlerFunction("__unm",(LuanTable)o);
+			LuanFunction fn = Luan.getHandlerFunction("__unm",(LuanTable)o);
 			if( fn != null ) {
 				return Luan.first(fn.call(luan,new Object[]{o}));
 			}
--- a/core/src/luan/impl/WhileStmt.java	Tue Mar 29 19:58:39 2016 -0600
+++ b/core/src/luan/impl/WhileStmt.java	Tue Mar 29 20:39:14 2016 -0600
@@ -15,7 +15,7 @@
 
 	@Override public void eval(LuanStateImpl luan) throws LuanException {
 		try {
-			while( luan.checkBoolean( cnd.eval(luan) ) ) {
+			while( Luan.checkBoolean( cnd.eval(luan) ) ) {
 				doStmt.eval(luan);
 			}
 		} catch(BreakException e) {}
--- a/core/src/luan/modules/JavaLuan.java	Tue Mar 29 19:58:39 2016 -0600
+++ b/core/src/luan/modules/JavaLuan.java	Tue Mar 29 20:39:14 2016 -0600
@@ -324,7 +324,7 @@
 						Object fnObj = t.get(luan,name);
 						if( fnObj == null )
 							throw new NullPointerException("luan_proxy couldn't find method '"+name+"'");
-						LuanFunction fn = luan.checkFunction(fnObj);
+						LuanFunction fn = Luan.checkFunction(fnObj);
 						return Luan.first(fn.call(luan,args));
 					}
 				}
--- a/core/src/luan/modules/TableLuan.java	Tue Mar 29 19:58:39 2016 -0600
+++ b/core/src/luan/modules/TableLuan.java	Tue Mar 29 20:39:14 2016 -0600
@@ -57,7 +57,7 @@
 			lt = new LessThan() {
 				public boolean isLessThan(Object o1,Object o2) {
 					try {
-						return luan.isLessThan(o1,o2);
+						return Luan.isLessThan(luan,o1,o2);
 					} catch(LuanException e) {
 						throw new LuanRuntimeException(e);
 					}
@@ -67,7 +67,7 @@
 			lt = new LessThan() {
 				public boolean isLessThan(Object o1,Object o2) {
 					try {
-						return luan.checkBoolean(Luan.first(comp.call(luan,new Object[]{o1,o2})));
+						return Luan.checkBoolean(Luan.first(comp.call(luan,new Object[]{o1,o2})));
 					} catch(LuanException e) {
 						throw new LuanRuntimeException(e);
 					}