From 2531a32d63de0b7872d89bdad2246d009d662190 Mon Sep 17 00:00:00 2001 From: Shevek Date: Fri, 17 Jan 2014 22:14:28 -0800 Subject: Fix handling of negative exponents. --- src/main/java/org/anarres/cpp/LexerSource.java | 23 ++++++++++++++--------- src/main/java/org/anarres/cpp/NumericValue.java | 9 +++++++++ 2 files changed, 23 insertions(+), 9 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/anarres/cpp/LexerSource.java b/src/main/java/org/anarres/cpp/LexerSource.java index bdb5f27..1ba73bb 100644 --- a/src/main/java/org/anarres/cpp/LexerSource.java +++ b/src/main/java/org/anarres/cpp/LexerSource.java @@ -526,11 +526,16 @@ public class LexerSource extends Source { /* Either a decimal part, or a hex exponent. */ @Nonnull - private String _number_part(StringBuilder text, int base) + private String _number_part(StringBuilder text, int base, boolean sign) throws IOException, LexerException { StringBuilder part = new StringBuilder(); int d = read(); + if (sign && d == '-') { + text.append((char) d); + part.append((char) d); + d = read(); + } while (Character.digit(d, base) != -1) { text.append((char) d); part.append((char) d); @@ -546,12 +551,12 @@ public class LexerSource extends Source { throws IOException, LexerException { StringBuilder text = new StringBuilder(negative ? "-0" : "0"); - String integer = _number_part(text, 8); + String integer = _number_part(text, 8, false); NumericValue value = new NumericValue(8, negative, integer); int d = read(); if (d == '.') { text.append((char) d); - String fraction = _number_part(text, 16); + String fraction = _number_part(text, 8, true); value.setFractionalPart(fraction); d = read(); } @@ -565,18 +570,18 @@ public class LexerSource extends Source { LexerException { StringBuilder text = new StringBuilder(negative ? "-0" : "0"); text.append(x); - String integer = _number_part(text, 16); + String integer = _number_part(text, 16, false); NumericValue value = new NumericValue(16, negative, integer); int d = read(); if (d == '.') { text.append((char) d); - String fraction = _number_part(text, 16); + String fraction = _number_part(text, 16, false); value.setFractionalPart(fraction); d = read(); } if (d == 'P' || d == 'p') { text.append((char) d); - String exponent = _number_part(text, 10); + String exponent = _number_part(text, 16, true); value.setExponent(exponent); d = read(); } @@ -591,18 +596,18 @@ public class LexerSource extends Source { throws IOException, LexerException { StringBuilder text = new StringBuilder(negative ? "-" : ""); - String integer = _number_part(text, 10); + String integer = _number_part(text, 10, false); NumericValue value = new NumericValue(10, negative, integer); int d = read(); if (d == '.') { text.append((char) d); - String fraction = _number_part(text, 10); + String fraction = _number_part(text, 10, false); value.setFractionalPart(fraction); d = read(); } if (d == 'E' || d == 'e') { text.append((char) d); - String exponent = _number_part(text, 10); + String exponent = _number_part(text, 10, true); value.setExponent(exponent); d = read(); } diff --git a/src/main/java/org/anarres/cpp/NumericValue.java b/src/main/java/org/anarres/cpp/NumericValue.java index ad0bfc0..e972ec7 100644 --- a/src/main/java/org/anarres/cpp/NumericValue.java +++ b/src/main/java/org/anarres/cpp/NumericValue.java @@ -126,15 +126,24 @@ public class NumericValue extends Number { return intValue(); } + private double exponentValue() { + int e = Integer.parseInt(exponent, base); + return Math.pow(base, e); + } + @Override public int intValue() { int v = integer.isEmpty() ? 0 : Integer.parseInt(integer, base); + if (exponent != null) + v = (int) (v * exponentValue()); return isNegative() ? -v : v; } @Override public long longValue() { long v = integer.isEmpty() ? 0 : Long.parseLong(integer, base); + if (exponent != null) + v = (long) (v * exponentValue()); return isNegative() ? -v : v; } -- cgit v1.2.3