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() {
|
||||
int begin = 0;
|
||||
int end = value.length();
|
||||
if (base == 16) {
|
||||
begin = 2; // ignore 0x
|
||||
return switch (base) {
|
||||
case 16 -> parseHex(end);
|
||||
case 10 -> parseDec(end);
|
||||
default -> throw new IllegalArgumentException("unexpected base " + base);
|
||||
};
|
||||
}
|
||||
|
||||
private OptionalLong parseDec(int end) {
|
||||
long l;
|
||||
try {
|
||||
l = Long.parseLong(value, begin, end, base);
|
||||
l = Long.parseLong(value, 0, end, base);
|
||||
} catch (NumberFormatException _) {
|
||||
return OptionalLong.empty();
|
||||
}
|
||||
|
|
@ -31,4 +35,12 @@ public record LiteralTree(String value, int base, Span span) implements Expressi
|
|||
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