AstGen: hook up hex float parsing to float literals

Thanks @LemonBoy!
This commit is contained in:
Andrew Kelley
2021-04-28 15:13:43 -07:00
parent 9db5b2c5c6
commit d3ffacb55c
2 changed files with 8 additions and 6 deletions

View File

@@ -1526,8 +1526,8 @@ pub const parseFloat = @import("fmt/parse_float.zig").parseFloat;
pub const parseHexFloat = @import("fmt/parse_hex_float.zig").parseHexFloat;
test {
_ = @import("fmt/parse_float.zig");
_ = @import("fmt/parse_hex_float.zig");
_ = parseFloat;
_ = parseHexFloat;
}
pub fn charToDigit(c: u8, radix: u8) (error{InvalidCharacter}!u8) {

View File

@@ -5971,11 +5971,13 @@ fn floatLiteral(
const main_token = main_tokens[node];
const bytes = tree.tokenSlice(main_token);
if (bytes.len > 2 and bytes[1] == 'x') {
const float_number: f128 = if (bytes.len > 2 and bytes[1] == 'x') hex: {
assert(bytes[0] == '0'); // validated by tokenizer
return astgen.failTok(main_token, "TODO implement hex floats", .{});
}
const float_number = std.fmt.parseFloat(f128, bytes) catch |e| switch (e) {
break :hex std.fmt.parseHexFloat(f128, bytes) catch |err| switch (err) {
error.InvalidCharacter => unreachable, // validated by tokenizer
error.Overflow => return astgen.failNode(node, "number literal cannot be represented in a 128-bit floating point", .{}),
};
} else std.fmt.parseFloat(f128, bytes) catch |err| switch (err) {
error.InvalidCharacter => unreachable, // validated by tokenizer
};
// If the value fits into a f32 without losing any precision, store it that way.