Fix semantic analysis of negative hexadecimals

This commit is contained in:
SirYwell 2025-05-06 22:37:50 +02:00
parent 9c992efea7
commit 2edeaaaee3
No known key found for this signature in database

View file

@ -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();
}
}
}