authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-02-10 16:37:37+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-10 11:02:29-05:00
logae5ba369e193ccf3f8103476efed34a6ff141a8d
tree5dfcbb3223248734413afc1899e3f5adfb1103c4
parent70a4794c57425d75505583e03919df983c4d8ccd

translate-c float fixes


3 files changed, 16 insertions(+), 4 deletions(-)

lib/std/c/tokenizer.zig+9-3
......@@ -1079,6 +1079,9 @@ pub const Tokenizer = struct {
10791079 'x', 'X' => {
10801080 state = .IntegerLiteralHex;
10811081 },
1082 '.' => {
1083 state = .FloatFraction;
1084 },
10821085 else => {
10831086 state = .IntegerSuffix;
10841087 self.index -= 1;
......@@ -1261,13 +1264,16 @@ pub const Tokenizer = struct {
12611264 .UnicodeEscape,
12621265 .MultiLineComment,
12631266 .MultiLineCommentAsterisk,
1264 .FloatFraction,
1265 .FloatFractionHex,
12661267 .FloatExponent,
1267 .FloatExponentDigits,
12681268 .MacroString,
12691269 => result.id = .Invalid,
12701270
1271 .FloatExponentDigits => result.id = if (counter == 0) .Invalid else .{ .FloatLiteral = .None },
1272
1273 .FloatFraction,
1274 .FloatFractionHex,
1275 => result.id = .{ .FloatLiteral = .None },
1276
12711277 .IntegerLiteralOct,
12721278 .IntegerLiteralBinary,
12731279 .IntegerLiteralHex,
src-self-hosted/translate_c.zig+3-1
......@@ -5122,6 +5122,8 @@ fn parseCNumLit(c: *Context, tok: *CToken, source: []const u8, source_loc: ZigCl
51225122 cast_node.rparen_token = try appendToken(c, .RParen, ")");
51235123 return &cast_node.base;
51245124 } else if (tok.id == .FloatLiteral) {
5125 if (lit_bytes[0] == '.')
5126 lit_bytes = try std.fmt.allocPrint(c.a(), "0{}", .{lit_bytes});
51255127 if (tok.id.FloatLiteral == .None) {
51265128 return transCreateNodeFloat(c, lit_bytes);
51275129 }
......@@ -5493,7 +5495,7 @@ fn parseCSuffixOpExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
54935495 // hack to get zig fmt to render a comma in builtin calls
54945496 _ = try appendToken(c, .Comma, ",");
54955497
5496 const ptr_kind = blk:{
5498 const ptr_kind = blk: {
54975499 // * token
54985500 _ = it.prev();
54995501 // last token of `node`
test/translate_c.zig+4
......@@ -621,9 +621,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
621621 cases.add("float suffixes",
622622 \\#define foo 3.14f
623623 \\#define bar 16.e-2l
624 \\#define FOO 0.12345
625 \\#define BAR .12345
624626 , &[_][]const u8{
625627 "pub const foo = @as(f32, 3.14);",
626628 "pub const bar = @as(c_longdouble, 16.e-2);",
629 "pub const FOO = 0.12345;",
630 "pub const BAR = 0.12345;",
627631 });
628632
629633 cases.add("comments",