From d3ffacb55caf18442422d7b28e1b5f16143f63ed Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 28 Apr 2021 15:13:43 -0700 Subject: [PATCH] AstGen: hook up hex float parsing to float literals Thanks @LemonBoy! --- lib/std/fmt.zig | 4 ++-- src/AstGen.zig | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 5d84138159..b28e0a3339 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -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) { diff --git a/src/AstGen.zig b/src/AstGen.zig index bc94431d2e..4d7c56548d 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -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.