This commit is contained in:
Peter Li 2025-04-10 00:41:42 -07:00
parent e637174d0c
commit bdf783bd01
1 changed files with 116 additions and 6 deletions

View File

@ -85,13 +85,15 @@ def plos(l):
for s in l:
print('> ', s, " < ")
print('opaque')
plos(typedefs_opaque)
print('struct')
# plos(typedefs_struct)
print('pod')
plos(typedefs_pod)
if print_types:
print('struct')
plos(typedefs_struct)
print('opaque')
plos(typedefs_opaque)
print('pod')
plos(typedefs_pod)
print('enum')
plos(typedefs_enum)
print('unknown')
@ -107,13 +109,32 @@ if print_defines:
print(f"\nfunctions: {len(functions)}, structs:{len(typedefs_struct)}, enums:{len(typedefs_enum)}\n")
ostring = 'pub const c = @import("c.zig").c;\n\n'
ostring = 'pub const c = @import("c.zig").c;\n\npub const PropertiesID = u32;'
# time to generate types...
# first the opaques
typeMap = {}
typeMap = {
'Uint32': 'u32', 'float': 'f32', 'Sint32': 'i32', 'bool': 'bool', 'Uint8': 'u8', 'SDL_PropertiesID': 'PropertiesID','char': 'i8',
'size_t': 'usize',
'SDL_GPUColorComponentFlags': 'GPUColorComponentFlags',
'SDL_FColor': 'FColor',
'SDL_FlipMode': 'FlipMode'
}
typeMap_r= {}
for t in typedefs_pod:
c_typename = t.strip().strip(';').split(' ')[-1]
typename = c_typename
if typename.startswith("SDL_"):
typename = typename[4:]
typeMap[c_typename] = typename
typeMap_r[typename] = c_typename
# todo generate packed flags for each one
for t in typedefs_opaque:
c_typename = t.strip().strip(';').split(' ')[3]
typename = c_typename
@ -127,5 +148,94 @@ for t in typedefs_opaque:
brackets = "{}"
definition = f"pub const {typename} = opaque{brackets};\n"
ostring += definition
ostring += '\n'
for t in typedefs_enum:
typename = t.split("}")[1].strip('\n ;')
print("E", typename)
c_typename = typename
if typename.startswith("SDL_"):
typename = typename[4:]
typeMap[c_typename] = typename
typeMap_r[typename] = c_typename
for t in typedefs_struct:
typename = t.split("}")[1].strip('\n ;')
c_typename = typename
if typename.startswith("SDL_"):
typename = typename[4:]
typeMap[c_typename] = typename
typeMap_r[typename] = c_typename
for t in typedefs_struct:
# > typedef struct SDL_GPUViewport{
# float x; /**< The left offset of the viewport. */
# float y; /**< The top offset of the viewport. */
# float w; /**< The width of the viewport. */
# float h; /**< The height of the viewport. */
# float min_depth; /**< The minimum depth of the viewport. */
# float max_depth; /**< The maximum depth of the viewport. */
# } SDL_GPUViewport;
typename = t.split("}")[1].strip('\n ;')
c_typename = typename
if typename.startswith("SDL_"):
typename = typename[4:]
typeMap[c_typename] = typename
typeMap_r[typename] = c_typename
inner = t.strip("\n ").split('\n')[1:-1]
print("----", typename)
for i in range(0, len(inner)):
inner[i] = inner[i].replace('/*', "//")
inner[i] = inner[i].replace('*<', "")
inner[i] = inner[i].replace('*/', "")
fields = []
for txt in inner:
s = txt.split(';')
decl = s[0].strip()
comment = ""
if len(s) > 1:
comment = s[1].strip()
fieldType = " ".join(decl.split(' ')[:-1])
name = decl.split(' ')[-1]
const = False
pointer = False
if fieldType.startswith('const'):
fieldType = fieldType[5:].strip()
const = True
if name.startswith('*'):
pointer = True;
name = name.strip('*')
if fieldType != '' and name != '':
fields.append((fieldType, name, const, comment, pointer))
# not implemented
if '[' in name:
assert False
if '[' in fieldType:
assert False
tstr = "pub const " + typename + " = extern struct {\n"
for field in fields:
tstr += ' ' + field[1] + ': ' + ('*' if field[4] else '') + ('const ' if field[2] else '') + typeMap[field[0]] + ', ' + field[3] + ' \n'
tstr += '};\n'
ostring += tstr + "\n"
print (ostring)