Commit d3ffacb55c

Andrew Kelley <andrew@ziglang.org>
2021-04-29 00:13:43
AstGen: hook up hex float parsing to float literals
Thanks @LemonBoy!
1 parent 9db5b2c
Changed files (2)
lib
src
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) {
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.