diff core/src/luan/LuanState.java @ 576:4723d22062ce

remove LuanBit
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 13 Jul 2015 20:38:26 -0600
parents 7c3ad6db8ac3
children 60c549d43988
line wrap: on
line diff
--- a/core/src/luan/LuanState.java	Mon Jul 13 18:34:31 2015 -0600
+++ b/core/src/luan/LuanState.java	Mon Jul 13 20:38:26 2015 -0600
@@ -64,32 +64,54 @@
 		return fn.call(this);
 	}
 
-	public final LuanBit bit(LuanElement el) {
-		return new LuanBit(this,el);
-	}
 
-	// convenience methods
-
-	private final LuanBit JAVA = bit(null);
 
 	public LuanException exception(Object msg) throws LuanException {
-		return JAVA.exception(msg);
+		return new LuanException(this,msg);
 	}
 
 	public Boolean checkBoolean(Object obj) throws LuanException {
-		return JAVA.checkBoolean(obj);
+		if( obj instanceof Boolean )
+			return (Boolean)obj;
+		throw exception( "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 {
-		return JAVA.checkString(obj);
+		if( obj instanceof String )
+			return (String)obj;
+		throw exception( "attempt to use '"+context()+"' (a " + Luan.type(obj) + " value) as a string" );
 	}
 
 	public LuanFunction checkFunction(Object obj) throws LuanException {
-		return JAVA.checkFunction(obj);
+		if( obj instanceof LuanFunction )
+			return (LuanFunction)obj;
+		throw exception( "attempt to call '"+context()+"' (a " + Luan.type(obj) + " value)" );
 	}
 
 	public boolean isLessThan(Object o1,Object o2) throws LuanException {
-		return JAVA.isLessThan(o1,o2);
+		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 exception( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) );
 	}
 
 	public String toString(Object obj) throws LuanException {
@@ -106,6 +128,15 @@
 		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;
@@ -119,4 +150,40 @@
 	public String context() {
 		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 );
+	}
+/*
+	public Number checkNumber(Object obj) throws LuanException {
+		if( obj instanceof Number )
+			return (Number)obj;
+		throw exception( "attempt to perform arithmetic on '"+context()+"' (a " + Luan.type(obj) + " value)" );
+	}
+*/
 }