Fix semantic analysis of negative hexadecimals
This commit is contained in:
parent
9c992efea7
commit
2edeaaaee3
1 changed files with 17 additions and 5 deletions
|
|
@ -14,14 +14,18 @@ public record LiteralTree(String value, int base, Span span) implements Expressi
|
||||||
}
|
}
|
||||||
|
|
||||||
public OptionalLong parseValue() {
|
public OptionalLong parseValue() {
|
||||||
int begin = 0;
|
|
||||||
int end = value.length();
|
int end = value.length();
|
||||||
if (base == 16) {
|
return switch (base) {
|
||||||
begin = 2; // ignore 0x
|
case 16 -> parseHex(end);
|
||||||
}
|
case 10 -> parseDec(end);
|
||||||
|
default -> throw new IllegalArgumentException("unexpected base " + base);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private OptionalLong parseDec(int end) {
|
||||||
long l;
|
long l;
|
||||||
try {
|
try {
|
||||||
l = Long.parseLong(value, begin, end, base);
|
l = Long.parseLong(value, 0, end, base);
|
||||||
} catch (NumberFormatException _) {
|
} catch (NumberFormatException _) {
|
||||||
return OptionalLong.empty();
|
return OptionalLong.empty();
|
||||||
}
|
}
|
||||||
|
|
@ -31,4 +35,12 @@ public record LiteralTree(String value, int base, Span span) implements Expressi
|
||||||
return OptionalLong.of(l);
|
return OptionalLong.of(l);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private OptionalLong parseHex(int end) {
|
||||||
|
try {
|
||||||
|
return OptionalLong.of(Integer.parseUnsignedInt(value, 2, end, 16));
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
return OptionalLong.empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue