Commit ae5ba369e1

Vexu <git@vexu.eu>
2020-02-10 15:37:37
translate-c float fixes
1 parent 70a4794
Changed files (3)
lib
src-self-hosted
test
lib/std/c/tokenizer.zig
@@ -1079,6 +1079,9 @@ pub const Tokenizer = struct {
                     'x', 'X' => {
                         state = .IntegerLiteralHex;
                     },
+                    '.' => {
+                        state = .FloatFraction;
+                    },
                     else => {
                         state = .IntegerSuffix;
                         self.index -= 1;
@@ -1261,13 +1264,16 @@ pub const Tokenizer = struct {
                 .UnicodeEscape,
                 .MultiLineComment,
                 .MultiLineCommentAsterisk,
-                .FloatFraction,
-                .FloatFractionHex,
                 .FloatExponent,
-                .FloatExponentDigits,
                 .MacroString,
                 => result.id = .Invalid,
 
+                .FloatExponentDigits => result.id = if (counter == 0) .Invalid else .{ .FloatLiteral = .None },
+
+                .FloatFraction,
+                .FloatFractionHex,
+                => result.id = .{ .FloatLiteral = .None },
+
                 .IntegerLiteralOct,
                 .IntegerLiteralBinary,
                 .IntegerLiteralHex,
src-self-hosted/translate_c.zig
@@ -5122,6 +5122,8 @@ fn parseCNumLit(c: *Context, tok: *CToken, source: []const u8, source_loc: ZigCl
         cast_node.rparen_token = try appendToken(c, .RParen, ")");
         return &cast_node.base;
     } else if (tok.id == .FloatLiteral) {
+        if (lit_bytes[0] == '.')
+            lit_bytes = try std.fmt.allocPrint(c.a(), "0{}", .{lit_bytes});
         if (tok.id.FloatLiteral == .None) {
             return transCreateNodeFloat(c, lit_bytes);
         }
@@ -5493,7 +5495,7 @@ fn parseCSuffixOpExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
                     // hack to get zig fmt to render a comma in builtin calls
                     _ = try appendToken(c, .Comma, ",");
 
-                    const ptr_kind = blk:{
+                    const ptr_kind = blk: {
                         // * token
                         _ = it.prev();
                         // last token of `node`
test/translate_c.zig
@@ -621,9 +621,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
     cases.add("float suffixes",
         \\#define foo 3.14f
         \\#define bar 16.e-2l
+        \\#define FOO 0.12345
+        \\#define BAR .12345
     , &[_][]const u8{
         "pub const foo = @as(f32, 3.14);",
         "pub const bar = @as(c_longdouble, 16.e-2);",
+        "pub const FOO = 0.12345;",
+        "pub const BAR = 0.12345;",
     });
 
     cases.add("comments",