changeset 446:bbad2d06f728

remove automatic conversion from string to number
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 04 May 2015 16:21:17 -0600
parents cc7d246bda2a
children 0bd42e774c50
files core/src/luan/Luan.java core/src/luan/LuanBit.java core/src/luan/LuanJavaFunction.java core/src/luan/impl/AddExpr.java core/src/luan/impl/DivExpr.java core/src/luan/impl/ModExpr.java core/src/luan/impl/MulExpr.java core/src/luan/impl/PowExpr.java core/src/luan/impl/SubExpr.java core/src/luan/impl/UnmExpr.java core/src/luan/modules/BasicLuan.java
diffstat 11 files changed, 31 insertions(+), 95 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/Luan.java	Mon May 04 15:43:36 2015 -0600
+++ b/core/src/luan/Luan.java	Mon May 04 16:21:17 2015 -0600
@@ -47,25 +47,6 @@
 		return "userdata";
 	}
 
-	public static Number toNumber(Object obj) {
-		return toNumber(obj,null);
-	}
-
-	public static Number toNumber(Object obj,Integer base) {
-		if( obj instanceof Number )
-			return (Number)obj;
-		if( obj instanceof String ) {
-			String s = (String)obj;
-			try {
-				if( base==null )
-					return Double.valueOf(s);
-				else
-					return Long.valueOf(s,base);
-			} catch(NumberFormatException e) {}
-		}
-		return null;
-	}
-
 	public static String toString(Number n) {
 		if( n instanceof Integer )
 			return n.toString();
--- a/core/src/luan/LuanBit.java	Mon May 04 15:43:36 2015 -0600
+++ b/core/src/luan/LuanBit.java	Mon May 04 16:21:17 2015 -0600
@@ -53,9 +53,8 @@
 	}
 
 	public Number checkNumber(Object obj) throws LuanException {
-		Number n = Luan.toNumber(obj);
-		if( n != null )
-			return n;
+		if( obj instanceof Number )
+			return (Number)obj;
 		if( el instanceof LuanSource.Element ) {
 			LuanSource.Element se = (LuanSource.Element)el;
 			throw exception( "attempt to perform arithmetic on '"+se.text()+"' (a " + Luan.type(obj) + " value)" );
@@ -141,7 +140,7 @@
 		LuanFunction fn = getBinHandler(op,o1,o2);
 		if( fn != null )
 			return Luan.first(call(fn,op,new Object[]{o1,o2}));
-		String type = Luan.toNumber(o1)==null ? Luan.type(o1) : Luan.type(o2);
+		String type = !(o1 instanceof Number) ? Luan.type(o1) : Luan.type(o2);
 		throw exception("attempt to perform arithmetic on a "+type+" value");
 	}
 
--- a/core/src/luan/LuanJavaFunction.java	Mon May 04 15:43:36 2015 -0600
+++ b/core/src/luan/LuanJavaFunction.java	Mon May 04 16:21:17 2015 -0600
@@ -246,12 +246,6 @@
 				Number n = (Number)obj;
 				return n.doubleValue();
 			}
-			if( obj instanceof String ) {
-				String s = (String)obj;
-				try {
-					return Double.valueOf(s);
-				} catch(NumberFormatException e) {}
-			}
 			return obj;
 		}
 		@Override public String toString() {
@@ -267,12 +261,6 @@
 				Number n = (Number)obj;
 				return n.floatValue();
 			}
-			if( obj instanceof String ) {
-				String s = (String)obj;
-				try {
-					return Float.valueOf(s);
-				} catch(NumberFormatException e) {}
-			}
 			return obj;
 		}
 		@Override public String toString() {
@@ -290,12 +278,6 @@
 				if( r==n.doubleValue() )
 					return r;
 			}
-			else if( obj instanceof String ) {
-				String s = (String)obj;
-				try {
-					return Long.valueOf(s);
-				} catch(NumberFormatException e) {}
-			}
 			return obj;
 		}
 		@Override public String toString() {
@@ -313,12 +295,6 @@
 				if( r==n.doubleValue() )
 					return r;
 			}
-			else if( obj instanceof String ) {
-				String s = (String)obj;
-				try {
-					return Integer.valueOf(s);
-				} catch(NumberFormatException e) {}
-			}
 			return obj;
 		}
 		@Override public String toString() {
@@ -336,12 +312,6 @@
 				if( r==n.doubleValue() )
 					return r;
 			}
-			else if( obj instanceof String ) {
-				String s = (String)obj;
-				try {
-					return Short.valueOf(s);
-				} catch(NumberFormatException e) {}
-			}
 			return obj;
 		}
 		@Override public String toString() {
@@ -359,12 +329,6 @@
 				if( r==n.doubleValue() )
 					return r;
 			}
-			else if( obj instanceof String ) {
-				String s = (String)obj;
-				try {
-					return Byte.valueOf(s);
-				} catch(NumberFormatException e) {}
-			}
 			return obj;
 		}
 		@Override public String toString() {
--- a/core/src/luan/impl/AddExpr.java	Mon May 04 15:43:36 2015 -0600
+++ b/core/src/luan/impl/AddExpr.java	Mon May 04 16:21:17 2015 -0600
@@ -1,6 +1,5 @@
 package luan.impl;
 
-import luan.Luan;
 import luan.LuanException;
 import luan.LuanSource;
 
@@ -14,10 +13,8 @@
 	@Override public Object eval(LuanStateImpl luan) throws LuanException {
 		Object o1 = op1.eval(luan);
 		Object o2 = op2.eval(luan);
-		Number n1 = Luan.toNumber(o1);
-		Number n2 = Luan.toNumber(o2);
-		if( n1 != null && n2 != null )
-			return n1.doubleValue() + n2.doubleValue();
+		if( o1 instanceof Number && o2 instanceof Number )
+			return ((Number)o1).doubleValue() + ((Number)o2).doubleValue();
 		return arithmetic(luan,"__add",o1,o2);
 	}
 }
--- a/core/src/luan/impl/DivExpr.java	Mon May 04 15:43:36 2015 -0600
+++ b/core/src/luan/impl/DivExpr.java	Mon May 04 16:21:17 2015 -0600
@@ -1,6 +1,5 @@
 package luan.impl;
 
-import luan.Luan;
 import luan.LuanException;
 import luan.LuanSource;
 
@@ -14,10 +13,8 @@
 	@Override public Object eval(LuanStateImpl luan) throws LuanException {
 		Object o1 = op1.eval(luan);
 		Object o2 = op2.eval(luan);
-		Number n1 = Luan.toNumber(o1);
-		Number n2 = Luan.toNumber(o2);
-		if( n1 != null && n2 != null )
-			return n1.doubleValue() / n2.doubleValue();
+		if( o1 instanceof Number && o2 instanceof Number )
+			return ((Number)o1).doubleValue() / ((Number)o2).doubleValue();
 		return arithmetic(luan,"__div",o1,o2);
 	}
 }
--- a/core/src/luan/impl/ModExpr.java	Mon May 04 15:43:36 2015 -0600
+++ b/core/src/luan/impl/ModExpr.java	Mon May 04 16:21:17 2015 -0600
@@ -1,6 +1,5 @@
 package luan.impl;
 
-import luan.Luan;
 import luan.LuanException;
 import luan.LuanSource;
 
@@ -14,11 +13,9 @@
 	@Override public Object eval(LuanStateImpl luan) throws LuanException {
 		Object o1 = op1.eval(luan);
 		Object o2 = op2.eval(luan);
-		Number n1 = Luan.toNumber(o1);
-		Number n2 = Luan.toNumber(o2);
-		if( n1 != null && n2 != null ) {
-			double d1 = n1.doubleValue();
-			double d2 = n2.doubleValue();
+		if( o1 instanceof Number && o2 instanceof Number ) {
+			double d1 = ((Number)o1).doubleValue();
+			double d2 = ((Number)o2).doubleValue();
 			return d1 - Math.floor(d1/d2)*d2;
 		}
 		return arithmetic(luan,"__mod",o1,o2);
--- a/core/src/luan/impl/MulExpr.java	Mon May 04 15:43:36 2015 -0600
+++ b/core/src/luan/impl/MulExpr.java	Mon May 04 16:21:17 2015 -0600
@@ -1,6 +1,5 @@
 package luan.impl;
 
-import luan.Luan;
 import luan.LuanException;
 import luan.LuanSource;
 
@@ -14,10 +13,8 @@
 	@Override public Object eval(LuanStateImpl luan) throws LuanException {
 		Object o1 = op1.eval(luan);
 		Object o2 = op2.eval(luan);
-		Number n1 = Luan.toNumber(o1);
-		Number n2 = Luan.toNumber(o2);
-		if( n1 != null && n2 != null )
-			return n1.doubleValue() * n2.doubleValue();
+		if( o1 instanceof Number && o2 instanceof Number )
+			return ((Number)o1).doubleValue() * ((Number)o2).doubleValue();
 		return arithmetic(luan,"__mul",o1,o2);
 	}
 }
--- a/core/src/luan/impl/PowExpr.java	Mon May 04 15:43:36 2015 -0600
+++ b/core/src/luan/impl/PowExpr.java	Mon May 04 16:21:17 2015 -0600
@@ -1,6 +1,5 @@
 package luan.impl;
 
-import luan.Luan;
 import luan.LuanException;
 import luan.LuanSource;
 
@@ -14,10 +13,8 @@
 	@Override public Object eval(LuanStateImpl luan) throws LuanException {
 		Object o1 = op1.eval(luan);
 		Object o2 = op2.eval(luan);
-		Number n1 = Luan.toNumber(o1);
-		Number n2 = Luan.toNumber(o2);
-		if( n1 != null && n2 != null )
-			return Math.pow( n1.doubleValue(), n2.doubleValue() );
+		if( o1 instanceof Number && o2 instanceof Number )
+			return Math.pow( ((Number)o1).doubleValue(), ((Number)o2).doubleValue() );
 		return arithmetic(luan,"__pow",o1,o2);
 	}
 }
--- a/core/src/luan/impl/SubExpr.java	Mon May 04 15:43:36 2015 -0600
+++ b/core/src/luan/impl/SubExpr.java	Mon May 04 16:21:17 2015 -0600
@@ -1,6 +1,5 @@
 package luan.impl;
 
-import luan.Luan;
 import luan.LuanException;
 import luan.LuanSource;
 
@@ -14,10 +13,8 @@
 	@Override public Object eval(LuanStateImpl luan) throws LuanException {
 		Object o1 = op1.eval(luan);
 		Object o2 = op2.eval(luan);
-		Number n1 = Luan.toNumber(o1);
-		Number n2 = Luan.toNumber(o2);
-		if( n1 != null && n2 != null )
-			return n1.doubleValue() - n2.doubleValue();
+		if( o1 instanceof Number && o2 instanceof Number )
+			return ((Number)o1).doubleValue() - ((Number)o2).doubleValue();
 		return arithmetic(luan,"__sub",o1,o2);
 	}
 }
--- a/core/src/luan/impl/UnmExpr.java	Mon May 04 15:43:36 2015 -0600
+++ b/core/src/luan/impl/UnmExpr.java	Mon May 04 16:21:17 2015 -0600
@@ -17,9 +17,8 @@
 
 	@Override public Object eval(LuanStateImpl luan) throws LuanException {
 		Object o = op.eval(luan);
-		Number n = Luan.toNumber(o);
-		if( n != null )
-			return -n.doubleValue();
+		if( o instanceof Number )
+			return -((Number)o).doubleValue();
 		LuanBit bit = luan.bit(se);
 		if( o instanceof LuanTable ) {
 			LuanFunction fn = bit.getHandlerFunction("__unm",(LuanTable)o);
--- a/core/src/luan/modules/BasicLuan.java	Mon May 04 15:43:36 2015 -0600
+++ b/core/src/luan/modules/BasicLuan.java	Mon May 04 16:21:17 2015 -0600
@@ -103,8 +103,19 @@
 		throw luan.exception( "bad argument #1 to 'raw_len' (table or string expected)" );
 	}
 
-	public static Number to_number(Object e,Integer base) {
-		return Luan.toNumber(e,base);
+	public static Number to_number(Object v,Integer base) {
+		try {
+			if( base == null ) {
+				if( v instanceof Number )
+					return (Number)v;
+				if( v instanceof String )
+					return Double.valueOf((String)v);
+			} else {
+				if( v instanceof String )
+					return Long.valueOf((String)v,base);
+			}
+		} catch(NumberFormatException e) {}
+		return null;
 	}
 
 	public static String to_string(LuanState luan,Object v) throws LuanException {