aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorShevek <[email protected]>2014-02-05 20:41:44 -0800
committerShevek <[email protected]>2014-02-05 20:41:44 -0800
commitec176d2a623f274ad8b0d7962b88a2f7a67446ca (patch)
tree0ee864d492d997b3e00582dc5dc3c686a7f351f4 /src
parentf2b03d6787e89255d68f5398a8a8e0d544f12405 (diff)
Fix #13: Negative constants are not in the C99 standard.
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/anarres/cpp/LexerSource.java31
-rw-r--r--src/main/java/org/anarres/cpp/NumericValue.java14
-rw-r--r--src/main/java/org/anarres/cpp/Preprocessor.java10
-rw-r--r--src/test/java/org/anarres/cpp/LexerSourceTest.java1
-rw-r--r--src/test/java/org/anarres/cpp/NumericValueTest.java12
5 files changed, 28 insertions, 40 deletions
diff --git a/src/main/java/org/anarres/cpp/LexerSource.java b/src/main/java/org/anarres/cpp/LexerSource.java
index 9b6fb8c..0b381b9 100644
--- a/src/main/java/org/anarres/cpp/LexerSource.java
+++ b/src/main/java/org/anarres/cpp/LexerSource.java
@@ -547,13 +547,13 @@ public class LexerSource extends Source {
/* We do not know whether know the first digit is valid. */
@Nonnull
- private Token number_hex(char x, boolean negative)
+ private Token number_hex(char x)
throws IOException,
LexerException {
- StringBuilder text = new StringBuilder(negative ? "-0" : "0");
+ StringBuilder text = new StringBuilder("0");
text.append(x);
String integer = _number_part(text, 16, false);
- NumericValue value = new NumericValue(16, negative, integer);
+ NumericValue value = new NumericValue(16, integer);
int d = read();
if (d == '.') {
text.append((char) d);
@@ -583,10 +583,10 @@ public class LexerSource extends Source {
/* We know we have at least one valid digit, but empty is not
* fine. */
@Nonnull
- private Token number_decimal(boolean negative)
+ private Token number_decimal()
throws IOException,
LexerException {
- StringBuilder text = new StringBuilder(negative ? "-" : "");
+ StringBuilder text = new StringBuilder();
String integer = _number_part(text, 10, false);
String fraction = null;
String exponent = null;
@@ -608,7 +608,7 @@ public class LexerSource extends Source {
else
base = 8;
}
- NumericValue value = new NumericValue(base, negative, integer);
+ NumericValue value = new NumericValue(base, integer);
if (fraction != null)
value.setFractionalPart(fraction);
if (exponent != null)
@@ -618,6 +618,10 @@ public class LexerSource extends Source {
}
/**
+ * Section 6.4.4.1 of C99
+ *
+ * (Not pasted here, but says that the initial negation is a separate token.)
+ *
* Section 6.4.4.2 of C99
*
* A floating constant has a significand part that may be followed
@@ -652,25 +656,20 @@ public class LexerSource extends Source {
private Token number()
throws IOException,
LexerException {
- boolean negative = false;
Token tok;
int c = read();
- if (c == '-') {
- negative = true;
- c = read();
- }
if (c == '0') {
int d = read();
if (d == 'x' || d == 'X') {
- tok = number_hex((char) d, negative);
+ tok = number_hex((char) d);
} else {
unread(d);
unread(c);
- tok = number_decimal(negative);
+ tok = number_decimal();
}
} else if (Character.isDigit(c) || c == '.') {
unread(c);
- tok = number_decimal(negative);
+ tok = number_decimal();
} else {
throw new LexerException("Asked to parse something as a number which isn't: " + (char) c);
}
@@ -797,10 +796,6 @@ public class LexerSource extends Source {
tok = new Token(ARROW);
else
unread(d);
- if (Character.isDigit(d)) {
- unread('-');
- tok = number();
- }
break;
case '*':
diff --git a/src/main/java/org/anarres/cpp/NumericValue.java b/src/main/java/org/anarres/cpp/NumericValue.java
index 2911b8a..e38d28f 100644
--- a/src/main/java/org/anarres/cpp/NumericValue.java
+++ b/src/main/java/org/anarres/cpp/NumericValue.java
@@ -35,16 +35,14 @@ public class NumericValue extends Number {
public static final int FF_SIZE = F_INT | F_LONG | F_LONGLONG | F_FLOAT | F_DOUBLE;
private final int base;
- private final boolean negative;
private final String integer;
private String fraction;
private int expbase = 0;
private String exponent;
private int flags;
- public NumericValue(int base, boolean negative, String integer) {
+ public NumericValue(int base, String integer) {
this.base = base;
- this.negative = negative;
this.integer = integer;
}
@@ -53,10 +51,6 @@ public class NumericValue extends Number {
return base;
}
- public boolean isNegative() {
- return negative;
- }
-
@Nonnull
public String getIntegerPart() {
return integer;
@@ -145,7 +139,7 @@ public class NumericValue extends Number {
v = v << exponentValue();
else if (expbase != 0)
v = (int) (v * Math.pow(expbase, exponentValue()));
- return isNegative() ? -v : v;
+ return v;
}
@Override
@@ -155,7 +149,7 @@ public class NumericValue extends Number {
v = v << exponentValue();
else if (expbase != 0)
v = (int) (v * Math.pow(expbase, exponentValue()));
- return isNegative() ? -v : v;
+ return v;
}
@Override
@@ -182,8 +176,6 @@ public class NumericValue extends Number {
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
- if (isNegative())
- buf.append('-');
switch (base) {
case 8:
buf.append('0');
diff --git a/src/main/java/org/anarres/cpp/Preprocessor.java b/src/main/java/org/anarres/cpp/Preprocessor.java
index 05bb4b4..0ad1538 100644
--- a/src/main/java/org/anarres/cpp/Preprocessor.java
+++ b/src/main/java/org/anarres/cpp/Preprocessor.java
@@ -793,7 +793,7 @@ public class Preprocessor implements Closeable {
new Token[]{new Token(NUMBER,
orig.getLine(), orig.getColumn(),
Integer.toString(orig.getLine()),
- new NumericValue(10, false, Integer.toString(orig.getLine())))}
+ new NumericValue(10, Integer.toString(orig.getLine())))}
), true);
} else if (m == __FILE__) {
StringBuilder buf = new StringBuilder("\"");
@@ -829,7 +829,7 @@ public class Preprocessor implements Closeable {
new Token[]{new Token(NUMBER,
orig.getLine(), orig.getColumn(),
Integer.toString(value),
- new NumericValue(10, false, Integer.toString(value)))}
+ new NumericValue(10, Integer.toString(value)))}
), true);
} else {
push_source(new MacroTokenSource(m, args), true);
@@ -1393,17 +1393,17 @@ public class Preprocessor implements Closeable {
+ la.getText());
tok = new Token(NUMBER,
la.getLine(), la.getColumn(),
- "0", new NumericValue(10, false, "0"));
+ "0", new NumericValue(10, "0"));
} else if (macros.containsKey(la.getText())) {
// System.out.println("Found macro");
tok = new Token(NUMBER,
la.getLine(), la.getColumn(),
- "1", new NumericValue(10, false, "1"));
+ "1", new NumericValue(10, "1"));
} else {
// System.out.println("Not found macro");
tok = new Token(NUMBER,
la.getLine(), la.getColumn(),
- "0", new NumericValue(10, false, "0"));
+ "0", new NumericValue(10, "0"));
}
if (paren) {
diff --git a/src/test/java/org/anarres/cpp/LexerSourceTest.java b/src/test/java/org/anarres/cpp/LexerSourceTest.java
index f51b253..80abdd3 100644
--- a/src/test/java/org/anarres/cpp/LexerSourceTest.java
+++ b/src/test/java/org/anarres/cpp/LexerSourceTest.java
@@ -98,5 +98,6 @@ public class LexerSourceTest {
testLexerSource("1e6", true, NUMBER);
testLexerSource("1.45e6", true, NUMBER);
testLexerSource(".45e6", true, NUMBER);
+ testLexerSource("-6", true, '-', NUMBER);
}
}
diff --git a/src/test/java/org/anarres/cpp/NumericValueTest.java b/src/test/java/org/anarres/cpp/NumericValueTest.java
index 9d6cbe4..5668452 100644
--- a/src/test/java/org/anarres/cpp/NumericValueTest.java
+++ b/src/test/java/org/anarres/cpp/NumericValueTest.java
@@ -54,16 +54,16 @@ public class NumericValueTest {
testNumericValue("0x12L", 0x12);
// Negative
- testNumericValue("-0", 0);
- testNumericValue("-1", -1);
+ // testNumericValue("-0", 0);
+ // testNumericValue("-1", -1);
// Negative hex
- testNumericValue("-0x56", -0x56);
- testNumericValue("-0x102", -0x102);
+ // testNumericValue("-0x56", -0x56);
+ // testNumericValue("-0x102", -0x102);
// Octal and negative octal
testNumericValue("0673", Integer.parseInt("673", 8));
- testNumericValue("-0673", Integer.parseInt("-673", 8));
+ // testNumericValue("-0673", Integer.parseInt("-673", 8));
// Floating point
testNumericValue(".0", 0);
@@ -75,7 +75,7 @@ public class NumericValueTest {
// Sign on exponents
testNumericValue("1e1", 1e1);
- testNumericValue("-1e1", -1e1);
+ // testNumericValue("-1e1", -1e1);
testNumericValue("1e-1", 1e-1);
// Hex numbers with decimal exponents