Przeglądaj źródła

Update zig bindgen produce source that matches "zig fmt" style

for #39, #40
Janne Hellsten 2 lat temu
rodzic
commit
2a3bf607ba
1 zmienionych plików z 12 dodań i 12 usunięć
  1. 12 12
      bindgen/gen_zig.py

+ 12 - 12
bindgen/gen_zig.py

@@ -212,7 +212,7 @@ def as_c_arg_type(arg_type, prefix):
     elif is_const_struct_ptr(arg_type):
         return f"[*c]const {as_zig_struct_type(util.extract_ptr_type(arg_type), prefix)}"
     elif is_prim_ptr(arg_type):
-        return f"[*c] {as_zig_prim_type(util.extract_ptr_type(arg_type))}"
+        return f"[*c]{as_zig_prim_type(util.extract_ptr_type(arg_type))}"
     elif is_const_prim_ptr(arg_type):
         return f"[*c]const {as_zig_prim_type(util.extract_ptr_type(arg_type))}"
     else:
@@ -242,7 +242,7 @@ def as_zig_arg_type(arg_prefix, arg_type, prefix):
         # not a bug, pass const structs by value
         return pre + f"{as_zig_struct_type(util.extract_ptr_type(arg_type), prefix)}"
     elif is_prim_ptr(arg_type):
-        return pre + f"* {as_zig_prim_type(util.extract_ptr_type(arg_type))}"
+        return pre + f"*{as_zig_prim_type(util.extract_ptr_type(arg_type))}"
     elif is_const_prim_ptr(arg_type):
         return pre + f"*const {as_zig_prim_type(util.extract_ptr_type(arg_type))}"
     else:
@@ -323,7 +323,7 @@ def gen_struct(decl, prefix):
         if is_prim_type(field_type):
             l(f"    {field_name}: {as_zig_prim_type(field_type)} = {type_default_value(field_type)},")
         elif is_struct_type(field_type):
-            l(f"    {field_name}: {as_zig_struct_type(field_type, prefix)} = .{{ }},")
+            l(f"    {field_name}: {as_zig_struct_type(field_type, prefix)} = .{{}},")
         elif is_enum_type(field_type):
             l(f"    {field_name}: {as_zig_enum_type(field_type, prefix)} = .{enum_default_item(field_type)},")
         elif util.is_string_ptr(field_type):
@@ -335,7 +335,7 @@ def gen_struct(decl, prefix):
         elif is_const_prim_ptr(field_type):
             l(f"    {field_name}: ?[*]const {as_zig_prim_type(util.extract_ptr_type(field_type))} = null,")
         elif util.is_func_ptr(field_type):
-            l(f"    {field_name}: ?*const fn({funcptr_args_c(field_type, prefix)}) callconv(.C) {funcptr_result_c(field_type)} = null,")
+            l(f"    {field_name}: ?*const fn ({funcptr_args_c(field_type, prefix)}) callconv(.C) {funcptr_result_c(field_type)} = null,")
         elif util.is_1d_array_type(field_type):
             array_type = util.extract_array_type(field_type)
             array_sizes = util.extract_array_sizes(field_type)
@@ -355,7 +355,7 @@ def gen_struct(decl, prefix):
                 t1 = f"[_]{zig_type}"
                 l(f"    {field_name}: {t0} = {t1}{{{def_val}}} ** {array_sizes[0]},")
             elif util.is_const_void_ptr(array_type):
-                l(f"    {field_name}: [{array_sizes[0]}]?*const anyopaque = [_]?*const anyopaque {{ null }} ** {array_sizes[0]},")
+                l(f"    {field_name}: [{array_sizes[0]}]?*const anyopaque = [_]?*const anyopaque{{null}} ** {array_sizes[0]},")
             else:
                 sys.exit(f"ERROR gen_struct: array {field_name}: {field_type} => {array_type} [{array_sizes[0]}]")
         elif util.is_2d_array_type(field_type):
@@ -366,11 +366,11 @@ def gen_struct(decl, prefix):
                 def_val = type_default_value(array_type)
             elif is_struct_type(array_type):
                 zig_type = as_zig_struct_type(array_type, prefix)
-                def_val = ".{ }"
+                def_val = ".{}"
             else:
                 sys.exit(f"ERROR gen_struct is_2d_array_type: {array_type}")
             t0 = f"[{array_sizes[0]}][{array_sizes[1]}]{zig_type}"
-            l(f"    {field_name}: {t0} = [_][{array_sizes[1]}]{zig_type}{{[_]{zig_type}{{ {def_val} }}**{array_sizes[1]}}}**{array_sizes[0]},")
+            l(f"    {field_name}: {t0} = [_][{array_sizes[1]}]{zig_type}{{[_]{zig_type}{{{def_val}}} ** {array_sizes[1]}}} ** {array_sizes[0]},")
         else:
             sys.exit(f"ERROR gen_struct: {field_name}: {field_type};")
     l("};")
@@ -419,7 +419,7 @@ def gen_func_zig(decl, prefix):
             if is_const_struct_ptr(arg_type):
                 s += f"&{arg_name}"
             elif util.is_string_ptr(arg_type):
-                s += f"@ptrCast([*c]const u8,{arg_name})"
+                s += f"@ptrCast({arg_name})"
             else:
                 s += arg_name
         if is_zig_string(zig_res_type):
@@ -452,7 +452,7 @@ def gen_imports(inp, dep_prefixes):
 def gen_helpers(inp):
     l('// helper function to convert a C string to a Zig string slice')
     l('fn cStrToZig(c_str: [*c]const u8) [:0]const u8 {')
-    l('  return @import("std").mem.span(c_str);')
+    l('    return @import("std").mem.span(c_str);')
     l('}')
     if inp['prefix'] in ['sg_', 'sdtx_', 'sshape_']:
         l('// helper function to convert "anything" to a Range struct')
@@ -471,14 +471,14 @@ def gen_helpers(inp):
         l('        },')
         l('        else => {')
         l('            @compileError("Cannot convert to range!");')
-        l('        }')
+        l('        },')
         l('    }')
         l('}')
         l('')
     if inp['prefix'] == 'sdtx_':
         l('// std.fmt compatible Writer')
         l('pub const Writer = struct {')
-        l('    pub const Error = error { };')
+        l('    pub const Error = error{};')
         l('    pub fn writeAll(self: Writer, bytes: []const u8) Error!void {')
         l('        _ = self;')
         l('        for (bytes) |byte| {')
@@ -488,7 +488,7 @@ def gen_helpers(inp):
         l('    pub fn writeByteNTimes(self: Writer, byte: u8, n: u64) Error!void {')
         l('        _ = self;')
         l('        var i: u64 = 0;')
-        l('        while (i < n): (i += 1) {')
+        l('        while (i < n) : (i += 1) {')
         l('            putc(byte);')
         l('        }')
         l('    }')