authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-28 15:13:43-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-28 15:13:43-07:00
logd3ffacb55caf18442422d7b28e1b5f16143f63ed
tree52768f9b17c7d56c2181ca2b6f66e5f4f56bfcbc
parent9db5b2c5c6a3406e856cc024009ba8cf79f0b942

AstGen: hook up hex float parsing to float literals

Thanks @LemonBoy!

2 files changed, 8 insertions(+), 6 deletions(-)

lib/std/fmt.zig+2-2
...@@ -1526,8 +1526,8 @@ pub const parseFloat = @import("fmt/parse_float.zig").parseFloat;...@@ -1526,8 +1526,8 @@ pub const parseFloat = @import("fmt/parse_float.zig").parseFloat;
1526pub const parseHexFloat = @import("fmt/parse_hex_float.zig").parseHexFloat;1526pub const parseHexFloat = @import("fmt/parse_hex_float.zig").parseHexFloat;
15271527
1528test {1528test {
1529 _ = @import("fmt/parse_float.zig");1529 _ = parseFloat;
1530 _ = @import("fmt/parse_hex_float.zig");1530 _ = parseHexFloat;
1531}1531}
15321532
1533pub fn charToDigit(c: u8, radix: u8) (error{InvalidCharacter}!u8) {1533pub fn charToDigit(c: u8, radix: u8) (error{InvalidCharacter}!u8) {
src/AstGen.zig+6-4
...@@ -5971,11 +5971,13 @@ fn floatLiteral(...@@ -5971,11 +5971,13 @@ fn floatLiteral(
59715971
5972 const main_token = main_tokens[node];5972 const main_token = main_tokens[node];
5973 const bytes = tree.tokenSlice(main_token);5973 const bytes = tree.tokenSlice(main_token);
5974 if (bytes.len > 2 and bytes[1] == 'x') {5974 const float_number: f128 = if (bytes.len > 2 and bytes[1] == 'x') hex: {
5975 assert(bytes[0] == '0'); // validated by tokenizer5975 assert(bytes[0] == '0'); // validated by tokenizer
5976 return astgen.failTok(main_token, "TODO implement hex floats", .{});5976 break :hex std.fmt.parseHexFloat(f128, bytes) catch |err| switch (err) {
5977 }5977 error.InvalidCharacter => unreachable, // validated by tokenizer
5978 const float_number = std.fmt.parseFloat(f128, bytes) catch |e| switch (e) {5978 error.Overflow => return astgen.failNode(node, "number literal cannot be represented in a 128-bit floating point", .{}),
5979 };
5980 } else std.fmt.parseFloat(f128, bytes) catch |err| switch (err) {
5979 error.InvalidCharacter => unreachable, // validated by tokenizer5981 error.InvalidCharacter => unreachable, // validated by tokenizer
5980 };5982 };
5981 // If the value fits into a f32 without losing any precision, store it that way.5983 // If the value fits into a f32 without losing any precision, store it that way.