changeset 579:f22a09e98b04

clean up LuanState
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 16 Jul 2015 15:14:31 -0600
parents 60c549d43988
children 1e69d9c21461
files core/src/luan/LuanState.java core/src/luan/StackTraceElement.java core/src/luan/impl/LuanStateImpl.java
diffstat 3 files changed, 67 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
diff -r 60c549d43988 -r f22a09e98b04 core/src/luan/LuanState.java
--- a/core/src/luan/LuanState.java	Tue Jul 14 17:40:48 2015 -0600
+++ b/core/src/luan/LuanState.java	Thu Jul 16 15:14:31 2015 -0600
@@ -15,7 +15,7 @@
 
 public abstract class LuanState implements DeepCloneable {
 
-	final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>();
+	protected final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>();
 
 	private Map registry;
 	private final List<Reference<Closeable>> onClose = new ArrayList<Reference<Closeable>>();
@@ -71,15 +71,6 @@
 		throw new LuanException(this, "attempt to use '"+context()+"' (a " + Luan.type(obj) + " value) as a boolean" );
 	}
 
-	public Boolean checkBoolean(Object obj,LuanElement el) throws LuanException {
-		push(el,null);
-		try {
-			return checkBoolean(obj);
-		} finally {
-			pop();
-		}
-	}
-
 	public String checkString(Object obj) throws LuanException {
 		if( obj instanceof String )
 			return (String)obj;
@@ -92,22 +83,7 @@
 		throw new LuanException(this, "attempt to call '"+context()+"' (a " + Luan.type(obj) + " value)" );
 	}
 
-	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(this, "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) );
-	}
+	abstract public boolean isLessThan(Object o1,Object o2) throws LuanException;
 
 	public String toString(Object obj) throws LuanException {
 		if( obj instanceof LuanTable ) {
@@ -123,15 +99,6 @@
 		return obj.toString();
 	}
 
-	public String toString(Object obj,LuanElement el) throws LuanException {
-		push(el,null);
-		try {
-			return toString(obj);
-		} finally {
-			pop();
-		}
-	}
-
 	public Object index(Object obj,Object key) throws LuanException {
 		if( obj instanceof LuanTable ) {
 			LuanTable tbl = (LuanTable)obj;
@@ -146,31 +113,6 @@
 		return stackTrace.get(stackTrace.size()-1).call.text();
 	}
 
-	public void push(LuanElement el,String fnName) {
-		if( el == null )  throw new RuntimeException();
-		stackTrace.add( new StackTraceElement(el,fnName) );
-	}
-
-	public void pop() {
-		stackTrace.remove(stackTrace.size()-1);
-	}
-
-	public 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 LuanFunction getHandlerFunction(String op,LuanTable t) throws LuanException {
-		Object f = t.getHandler(op);
-		if( f == null )
-			return null;
-		return checkFunction(f);
-	}
-
 	public void dumpStack() {
 		System.err.println( stackTrace );
 	}
diff -r 60c549d43988 -r f22a09e98b04 core/src/luan/StackTraceElement.java
--- a/core/src/luan/StackTraceElement.java	Tue Jul 14 17:40:48 2015 -0600
+++ b/core/src/luan/StackTraceElement.java	Thu Jul 16 15:14:31 2015 -0600
@@ -1,11 +1,11 @@
 package luan;
 
 
-final class StackTraceElement {
+public final class StackTraceElement {
 	final LuanElement call;
 	final String fnName;
 
-	StackTraceElement(LuanElement call,String fnName) {
+	public StackTraceElement(LuanElement call,String fnName) {
 		this.call = call;
 		this.fnName = fnName;
 	}
diff -r 60c549d43988 -r f22a09e98b04 core/src/luan/impl/LuanStateImpl.java
--- a/core/src/luan/impl/LuanStateImpl.java	Tue Jul 14 17:40:48 2015 -0600
+++ b/core/src/luan/impl/LuanStateImpl.java	Thu Jul 16 15:14:31 2015 -0600
@@ -12,6 +12,7 @@
 import luan.LuanElement;
 import luan.LuanSource;
 import luan.DeepCloner;
+import luan.StackTraceElement;
 
 
 final class LuanStateImpl extends LuanState {
@@ -124,4 +125,66 @@
 		return frame.closure.fnDef.el().source;
 	}
 
+
+
+	@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(this, "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);
+	}
+
+	Boolean checkBoolean(Object obj,LuanElement el) throws LuanException {
+		push(el,null);
+		try {
+			return checkBoolean(obj);
+		} finally {
+			pop();
+		}
+	}
+
+	String toString(Object obj,LuanElement el) throws LuanException {
+		push(el,null);
+		try {
+			return toString(obj);
+		} finally {
+			pop();
+		}
+	}
+
+	void push(LuanElement el,String fnName) {
+		if( el == null )  throw new RuntimeException();
+		stackTrace.add( new StackTraceElement(el,fnName) );
+	}
+
+	void pop() {
+		stackTrace.remove(stackTrace.size()-1);
+	}
+
 }