Skip to content

Commit ae5ba36

Browse files
Vexuandrewrk
authored andcommitted
translate-c float fixes
1 parent 70a4794 commit ae5ba36

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

lib/std/c/tokenizer.zig

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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",

0 commit comments

Comments
 (0)