changeset 535:289908469e02

parse numeric constants to ints or longs if possible
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 31 May 2015 00:00:17 -0600
parents 115cae4ac8fd
children 3058870cbe83
files core/src/luan/impl/LuanParser.java
diffstat 1 files changed, 26 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
diff -r 115cae4ac8fd -r 289908469e02 core/src/luan/impl/LuanParser.java
--- a/core/src/luan/impl/LuanParser.java	Fri May 29 01:08:30 2015 -0600
+++ b/core/src/luan/impl/LuanParser.java	Sun May 31 00:00:17 2015 -0600
@@ -1146,15 +1146,29 @@
 
 	private Number DecNumber() {
 		int start = parser.begin();
+		boolean isInt = true;
 		if( Int() ) {
-			if( parser.match('.') )
+			if( parser.match('.') ) {
+				isInt = false;
 				Int();  // optional
+			}
 		} else if( parser.match('.') && Int() ) {
 			// ok
+			isInt = false;
 		} else
 			return parser.failure(null);
-		Exponent();  // optional
-		return parser.success(Double.valueOf(parser.textFrom(start)));
+		if( Exponent() )  // optional
+			isInt = false;
+		String s = parser.textFrom(start);
+		if( isInt ) {
+			try {
+				return parser.success(Integer.valueOf(s));
+			} catch(NumberFormatException e) {}
+			try {
+				return parser.success(Long.valueOf(s));
+			} catch(NumberFormatException e) {}
+		}
+		return parser.success(Double.valueOf(s));
 	}
 
 	private boolean Exponent() {
@@ -1180,9 +1194,11 @@
 
 	private Number HexNumber() {
 		int start = parser.begin();
+		long nLong = 0;
 		double n;
 		if( HexInt() ) {
-			n = (double)Long.parseLong(parser.textFrom(start),16);
+			nLong = Long.parseLong(parser.textFrom(start),16);
+			n = (double)nLong;
 			if( parser.match('.') ) {
 				start = parser.currentIndex();
 				if( HexInt() ) {
@@ -1203,6 +1219,12 @@
 				return parser.failure(null);
 			n *= Math.pow(2,(double)Long.parseLong(parser.textFrom(start)));
 		}
+		if( nLong == n ) {
+			int nInt = (int)nLong;
+			if( nInt == nLong )
+				return parser.success(Integer.valueOf(nInt));
+			return parser.success(Long.valueOf(nLong));
+		}
 		return parser.success(Double.valueOf(n));
 	}