|
@@ -7,7 +7,9 @@
|
|
# - otherwise snake_case
|
|
# - otherwise snake_case
|
|
#-------------------------------------------------------------------------------
|
|
#-------------------------------------------------------------------------------
|
|
import gen_ir
|
|
import gen_ir
|
|
-import re, os, shutil, sys
|
|
|
|
|
|
+import os, shutil, sys
|
|
|
|
+
|
|
|
|
+import gen_util as util
|
|
|
|
|
|
module_names = {
|
|
module_names = {
|
|
'sg_': 'gfx',
|
|
'sg_': 'gfx',
|
|
@@ -90,8 +92,6 @@ prim_defaults = {
|
|
'size_t': '0'
|
|
'size_t': '0'
|
|
}
|
|
}
|
|
|
|
|
|
-re_1d_array = re.compile("^(?:const )?\w*\s\*?\[\d*\]$")
|
|
|
|
-re_2d_array = re.compile("^(?:const )?\w*\s\*?\[\d*\]\[\d*\]$")
|
|
|
|
|
|
|
|
struct_types = []
|
|
struct_types = []
|
|
enum_types = []
|
|
enum_types = []
|
|
@@ -145,24 +145,6 @@ def check_override(name, default=None):
|
|
def check_ignore(name):
|
|
def check_ignore(name):
|
|
return name in ignores
|
|
return name in ignores
|
|
|
|
|
|
-# PREFIX_BLA_BLUB to bla_blub
|
|
|
|
-def as_snake_case(s, prefix):
|
|
|
|
- outp = s.lower()
|
|
|
|
- if outp.startswith(prefix):
|
|
|
|
- outp = outp[len(prefix):]
|
|
|
|
- return outp
|
|
|
|
-
|
|
|
|
-# prefix_bla_blub => blaBlub
|
|
|
|
-def as_camel_case(s, prefix):
|
|
|
|
- outp = s.lower()
|
|
|
|
- if outp.startswith(prefix):
|
|
|
|
- outp = outp[len(prefix):]
|
|
|
|
- parts = outp.split('_')
|
|
|
|
- outp = parts[0]
|
|
|
|
- for part in parts[1:]:
|
|
|
|
- outp += part.capitalize()
|
|
|
|
- return outp
|
|
|
|
-
|
|
|
|
# PREFIX_ENUM_BLA => Bla, _PREFIX_ENUM_BLA => Bla
|
|
# PREFIX_ENUM_BLA => Bla, _PREFIX_ENUM_BLA => Bla
|
|
def as_enum_item_name(s):
|
|
def as_enum_item_name(s):
|
|
outp = s.lstrip('_')
|
|
outp = s.lstrip('_')
|
|
@@ -184,15 +166,6 @@ def is_struct_type(s):
|
|
def is_enum_type(s):
|
|
def is_enum_type(s):
|
|
return s in enum_types
|
|
return s in enum_types
|
|
|
|
|
|
-def is_string_ptr(s):
|
|
|
|
- return s == "const char *"
|
|
|
|
-
|
|
|
|
-def is_const_void_ptr(s):
|
|
|
|
- return s == "const void *"
|
|
|
|
-
|
|
|
|
-def is_void_ptr(s):
|
|
|
|
- return s == "void *"
|
|
|
|
-
|
|
|
|
def is_const_prim_ptr(s):
|
|
def is_const_prim_ptr(s):
|
|
for prim_type in prim_types:
|
|
for prim_type in prim_types:
|
|
if s == f"const {prim_type} *":
|
|
if s == f"const {prim_type} *":
|
|
@@ -211,31 +184,9 @@ def is_const_struct_ptr(s):
|
|
return True
|
|
return True
|
|
return False
|
|
return False
|
|
|
|
|
|
-def is_func_ptr(s):
|
|
|
|
- return '(*)' in s
|
|
|
|
-
|
|
|
|
-def is_1d_array_type(s):
|
|
|
|
- return re_1d_array.match(s) is not None
|
|
|
|
-
|
|
|
|
-def is_2d_array_type(s):
|
|
|
|
- return re_2d_array.match(s) is not None
|
|
|
|
-
|
|
|
|
def type_default_value(s):
|
|
def type_default_value(s):
|
|
return prim_defaults[s]
|
|
return prim_defaults[s]
|
|
|
|
|
|
-def extract_array_type(s):
|
|
|
|
- return s[:s.index('[')].strip()
|
|
|
|
-
|
|
|
|
-def extract_array_sizes(s):
|
|
|
|
- return s[s.index('['):].replace('[', ' ').replace(']', ' ').split()
|
|
|
|
-
|
|
|
|
-def extract_ptr_type(s):
|
|
|
|
- tokens = s.split()
|
|
|
|
- if tokens[0] == 'const':
|
|
|
|
- return tokens[1]
|
|
|
|
- else:
|
|
|
|
- return tokens[0]
|
|
|
|
-
|
|
|
|
def as_c_arg_type(arg_type, prefix):
|
|
def as_c_arg_type(arg_type, prefix):
|
|
if arg_type == "void":
|
|
if arg_type == "void":
|
|
return "void"
|
|
return "void"
|
|
@@ -245,18 +196,18 @@ def as_c_arg_type(arg_type, prefix):
|
|
return as_zig_struct_type(arg_type, prefix)
|
|
return as_zig_struct_type(arg_type, prefix)
|
|
elif is_enum_type(arg_type):
|
|
elif is_enum_type(arg_type):
|
|
return as_zig_enum_type(arg_type, prefix)
|
|
return as_zig_enum_type(arg_type, prefix)
|
|
- elif is_void_ptr(arg_type):
|
|
|
|
|
|
+ elif util.is_void_ptr(arg_type):
|
|
return "?*anyopaque"
|
|
return "?*anyopaque"
|
|
- elif is_const_void_ptr(arg_type):
|
|
|
|
|
|
+ elif util.is_const_void_ptr(arg_type):
|
|
return "?*const anyopaque"
|
|
return "?*const anyopaque"
|
|
- elif is_string_ptr(arg_type):
|
|
|
|
|
|
+ elif util.is_string_ptr(arg_type):
|
|
return "[*c]const u8"
|
|
return "[*c]const u8"
|
|
elif is_const_struct_ptr(arg_type):
|
|
elif is_const_struct_ptr(arg_type):
|
|
- return f"[*c]const {as_zig_struct_type(extract_ptr_type(arg_type), prefix)}"
|
|
|
|
|
|
+ return f"[*c]const {as_zig_struct_type(util.extract_ptr_type(arg_type), prefix)}"
|
|
elif is_prim_ptr(arg_type):
|
|
elif is_prim_ptr(arg_type):
|
|
- return f"[*c] {as_zig_prim_type(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):
|
|
elif is_const_prim_ptr(arg_type):
|
|
- return f"[*c]const {as_zig_prim_type(extract_ptr_type(arg_type))}"
|
|
|
|
|
|
+ return f"[*c]const {as_zig_prim_type(util.extract_ptr_type(arg_type))}"
|
|
else:
|
|
else:
|
|
sys.exit(f"Error as_c_arg_type(): {arg_type}")
|
|
sys.exit(f"Error as_c_arg_type(): {arg_type}")
|
|
|
|
|
|
@@ -274,19 +225,19 @@ def as_zig_arg_type(arg_prefix, arg_type, prefix):
|
|
return pre + as_zig_struct_type(arg_type, prefix)
|
|
return pre + as_zig_struct_type(arg_type, prefix)
|
|
elif is_enum_type(arg_type):
|
|
elif is_enum_type(arg_type):
|
|
return pre + as_zig_enum_type(arg_type, prefix)
|
|
return pre + as_zig_enum_type(arg_type, prefix)
|
|
- elif is_void_ptr(arg_type):
|
|
|
|
|
|
+ elif util.is_void_ptr(arg_type):
|
|
return pre + "?*anyopaque"
|
|
return pre + "?*anyopaque"
|
|
- elif is_const_void_ptr(arg_type):
|
|
|
|
|
|
+ elif util.is_const_void_ptr(arg_type):
|
|
return pre + "?*const anyopaque"
|
|
return pre + "?*const anyopaque"
|
|
- elif is_string_ptr(arg_type):
|
|
|
|
|
|
+ elif util.is_string_ptr(arg_type):
|
|
return pre + "[:0]const u8"
|
|
return pre + "[:0]const u8"
|
|
elif is_const_struct_ptr(arg_type):
|
|
elif is_const_struct_ptr(arg_type):
|
|
# not a bug, pass const structs by value
|
|
# not a bug, pass const structs by value
|
|
- return pre + f"{as_zig_struct_type(extract_ptr_type(arg_type), prefix)}"
|
|
|
|
|
|
+ return pre + f"{as_zig_struct_type(util.extract_ptr_type(arg_type), prefix)}"
|
|
elif is_prim_ptr(arg_type):
|
|
elif is_prim_ptr(arg_type):
|
|
- return pre + f"* {as_zig_prim_type(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):
|
|
elif is_const_prim_ptr(arg_type):
|
|
- return pre + f"*const {as_zig_prim_type(extract_ptr_type(arg_type))}"
|
|
|
|
|
|
+ return pre + f"*const {as_zig_prim_type(util.extract_ptr_type(arg_type))}"
|
|
else:
|
|
else:
|
|
sys.exit(f"ERROR as_zig_arg_type(): {arg_type}")
|
|
sys.exit(f"ERROR as_zig_arg_type(): {arg_type}")
|
|
|
|
|
|
@@ -313,9 +264,9 @@ def funcptr_result_c(field_type):
|
|
res_type = field_type[:field_type.index('(*)')].strip()
|
|
res_type = field_type[:field_type.index('(*)')].strip()
|
|
if res_type == 'void':
|
|
if res_type == 'void':
|
|
return 'void'
|
|
return 'void'
|
|
- elif is_const_void_ptr(res_type):
|
|
|
|
|
|
+ elif util.is_const_void_ptr(res_type):
|
|
return '?*const anyopaque'
|
|
return '?*const anyopaque'
|
|
- elif is_void_ptr(res_type):
|
|
|
|
|
|
+ elif util.is_void_ptr(res_type):
|
|
return '?*anyopaque'
|
|
return '?*anyopaque'
|
|
else:
|
|
else:
|
|
sys.exit(f"ERROR funcptr_result_c(): {field_type}")
|
|
sys.exit(f"ERROR funcptr_result_c(): {field_type}")
|
|
@@ -368,19 +319,19 @@ def gen_struct(decl, prefix):
|
|
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):
|
|
elif is_enum_type(field_type):
|
|
l(f" {field_name}: {as_zig_enum_type(field_type, prefix)} = .{enum_default_item(field_type)},")
|
|
l(f" {field_name}: {as_zig_enum_type(field_type, prefix)} = .{enum_default_item(field_type)},")
|
|
- elif is_string_ptr(field_type):
|
|
|
|
|
|
+ elif util.is_string_ptr(field_type):
|
|
l(f" {field_name}: [*c]const u8 = null,")
|
|
l(f" {field_name}: [*c]const u8 = null,")
|
|
- elif is_const_void_ptr(field_type):
|
|
|
|
|
|
+ elif util.is_const_void_ptr(field_type):
|
|
l(f" {field_name}: ?*const anyopaque = null,")
|
|
l(f" {field_name}: ?*const anyopaque = null,")
|
|
- elif is_void_ptr(field_type):
|
|
|
|
|
|
+ elif util.is_void_ptr(field_type):
|
|
l(f" {field_name}: ?*anyopaque = null,")
|
|
l(f" {field_name}: ?*anyopaque = null,")
|
|
elif is_const_prim_ptr(field_type):
|
|
elif is_const_prim_ptr(field_type):
|
|
- l(f" {field_name}: ?[*]const {as_zig_prim_type(extract_ptr_type(field_type))} = null,")
|
|
|
|
- elif is_func_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}: ?fn({funcptr_args_c(field_type, prefix)}) callconv(.C) {funcptr_result_c(field_type)} = null,")
|
|
l(f" {field_name}: ?fn({funcptr_args_c(field_type, prefix)}) callconv(.C) {funcptr_result_c(field_type)} = null,")
|
|
- elif is_1d_array_type(field_type):
|
|
|
|
- array_type = extract_array_type(field_type)
|
|
|
|
- array_sizes = extract_array_sizes(field_type)
|
|
|
|
|
|
+ elif util.is_1d_array_type(field_type):
|
|
|
|
+ array_type = util.extract_array_type(field_type)
|
|
|
|
+ array_sizes = util.extract_array_sizes(field_type)
|
|
if is_prim_type(array_type) or is_struct_type(array_type):
|
|
if is_prim_type(array_type) or is_struct_type(array_type):
|
|
if is_prim_type(array_type):
|
|
if is_prim_type(array_type):
|
|
zig_type = as_zig_prim_type(array_type)
|
|
zig_type = as_zig_prim_type(array_type)
|
|
@@ -396,13 +347,13 @@ def gen_struct(decl, prefix):
|
|
t0 = f"[{array_sizes[0]}]{zig_type}"
|
|
t0 = f"[{array_sizes[0]}]{zig_type}"
|
|
t1 = f"[_]{zig_type}"
|
|
t1 = f"[_]{zig_type}"
|
|
l(f" {field_name}: {t0} = {t1}{{{def_val}}} ** {array_sizes[0]},")
|
|
l(f" {field_name}: {t0} = {t1}{{{def_val}}} ** {array_sizes[0]},")
|
|
- elif is_const_void_ptr(array_type):
|
|
|
|
|
|
+ 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:
|
|
else:
|
|
sys.exit(f"ERROR gen_struct: array {field_name}: {field_type} => {array_type} [{array_sizes[0]}]")
|
|
sys.exit(f"ERROR gen_struct: array {field_name}: {field_type} => {array_type} [{array_sizes[0]}]")
|
|
- elif is_2d_array_type(field_type):
|
|
|
|
- array_type = extract_array_type(field_type)
|
|
|
|
- array_sizes = extract_array_sizes(field_type)
|
|
|
|
|
|
+ elif util.is_2d_array_type(field_type):
|
|
|
|
+ array_type = util.extract_array_type(field_type)
|
|
|
|
+ array_sizes = util.extract_array_sizes(field_type)
|
|
if is_prim_type(array_type):
|
|
if is_prim_type(array_type):
|
|
zig_type = as_zig_prim_type(array_type)
|
|
zig_type = as_zig_prim_type(array_type)
|
|
def_val = type_default_value(array_type)
|
|
def_val = type_default_value(array_type)
|
|
@@ -420,7 +371,7 @@ def gen_struct(decl, prefix):
|
|
def gen_consts(decl, prefix):
|
|
def gen_consts(decl, prefix):
|
|
for item in decl['items']:
|
|
for item in decl['items']:
|
|
item_name = check_override(item['name'])
|
|
item_name = check_override(item['name'])
|
|
- l(f"pub const {as_snake_case(item_name, prefix)} = {item['value']};")
|
|
|
|
|
|
+ l(f"pub const {util.as_lower_snake_case(item_name, prefix)} = {item['value']};")
|
|
|
|
|
|
def gen_enum(decl, prefix):
|
|
def gen_enum(decl, prefix):
|
|
enum_name = check_override(decl['name'])
|
|
enum_name = check_override(decl['name'])
|
|
@@ -439,7 +390,7 @@ def gen_func_c(decl, prefix):
|
|
|
|
|
|
def gen_func_zig(decl, prefix):
|
|
def gen_func_zig(decl, prefix):
|
|
c_func_name = decl['name']
|
|
c_func_name = decl['name']
|
|
- zig_func_name = as_camel_case(check_override(decl['name']), prefix)
|
|
|
|
|
|
+ zig_func_name = util.as_lower_camel_case(check_override(decl['name']), prefix)
|
|
zig_res_type = funcdecl_result_zig(decl, prefix)
|
|
zig_res_type = funcdecl_result_zig(decl, prefix)
|
|
l(f"pub fn {zig_func_name}({funcdecl_args_zig(decl, prefix)}) {zig_res_type} {{")
|
|
l(f"pub fn {zig_func_name}({funcdecl_args_zig(decl, prefix)}) {zig_res_type} {{")
|
|
if is_zig_string(zig_res_type):
|
|
if is_zig_string(zig_res_type):
|
|
@@ -456,7 +407,7 @@ def gen_func_zig(decl, prefix):
|
|
arg_type = param_decl['type']
|
|
arg_type = param_decl['type']
|
|
if is_const_struct_ptr(arg_type):
|
|
if is_const_struct_ptr(arg_type):
|
|
s += f"&{arg_name}"
|
|
s += f"&{arg_name}"
|
|
- elif is_string_ptr(arg_type):
|
|
|
|
|
|
+ elif util.is_string_ptr(arg_type):
|
|
s += f"@ptrCast([*c]const u8,{arg_name})"
|
|
s += f"@ptrCast([*c]const u8,{arg_name})"
|
|
else:
|
|
else:
|
|
s += arg_name
|
|
s += arg_name
|