summaryrefslogtreecommitdiffstats
path: root/src/main
diff options
context:
space:
mode:
authorShevek <[email protected]>2014-01-17 22:14:28 -0800
committerShevek <[email protected]>2014-01-17 22:14:28 -0800
commit2531a32d63de0b7872d89bdad2246d009d662190 (patch)
tree95184757386762dec2771572b6c3e4ff1fd2b6ad /src/main
parentc084e7c1b45b494bf5ebfcc5f866a39bc661de73 (diff)
Fix handling of negative exponents.
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/org/anarres/cpp/LexerSource.java23
-rw-r--r--src/main/java/org/anarres/cpp/NumericValue.java9
2 files changed, 23 insertions, 9 deletions
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;
}