mirror of
https://github.com/Not-Nik/raylib-zig.git
synced 2025-09-09 12:07:27 +00:00
Clean up generate_functions.py
This commit fixes the PEP8 warnings, with the only change to the PEP8 standard being the column limit change from 80 to 120. These warnings were gathered with `flake8` version 6.1.0. The complete command to see warnings was: `flake8 --max-line-length 120 generate_functions.py` Additionally, comments have been line wrapped in places, start of sentences have been capitalized, and full stops have been added to terminate sentences.
This commit is contained in:
parent
a106b9968f
commit
4dbbea186e
@ -22,8 +22,8 @@ ZIGGIFY = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
# Some c types have a different sizes on different systems
|
# Some C types have a different sizes on different systems and Zig
|
||||||
# and zig knows that so we tell it to get the system specific size for us
|
# knows that so we tell it to get the system specific size for us.
|
||||||
def c_to_zig_type(c: str) -> str:
|
def c_to_zig_type(c: str) -> str:
|
||||||
const = "const " if "const " in c else ""
|
const = "const " if "const " in c else ""
|
||||||
c = c.replace("const ", "")
|
c = c.replace("const ", "")
|
||||||
@ -38,12 +38,24 @@ def c_to_zig_type(c: str) -> str:
|
|||||||
def ziggify_type(name: str, t: str) -> str:
|
def ziggify_type(name: str, t: str) -> str:
|
||||||
NO_STRINGS = ["data", "fileData", "compData"]
|
NO_STRINGS = ["data", "fileData", "compData"]
|
||||||
|
|
||||||
single = ["value", "ptr", "bytesRead", "compDataSize", "dataSize", "outputSize", "camera", "collisionPoint", "frames", "image", "colorCount", "dst", "texture", "srcPtr", "dstPtr", "count", "codepointSize", "utf8Size", "position", "mesh", "materialCount", "material", "model", "animCount", "wave", "v1", "v2", "outAxis", "outAngle", "fileSize"]
|
single = [
|
||||||
multi = ["data", "compData", "points", "fileData", "colors", "pixels", "fontChars", "chars", "recs", "codepoints", "textList", "transforms", "animations", "samples", "LoadImageColors", "LoadImagePalette", "LoadFontData", "LoadCodepoints", "TextSplit", "LoadMaterials", "LoadModelAnimations", "LoadWaveSamples", "images"]
|
"value", "ptr", "bytesRead", "compDataSize", "dataSize", "outputSize",
|
||||||
|
"camera", "collisionPoint", "frames", "image", "colorCount", "dst",
|
||||||
|
"texture", "srcPtr", "dstPtr", "count", "codepointSize", "utf8Size",
|
||||||
|
"position", "mesh", "materialCount", "material", "model", "animCount",
|
||||||
|
"wave", "v1", "v2", "outAxis", "outAngle", "fileSize"
|
||||||
|
]
|
||||||
|
multi = [
|
||||||
|
"data", "compData", "points", "fileData", "colors", "pixels",
|
||||||
|
"fontChars", "chars", "recs", "codepoints", "textList", "transforms",
|
||||||
|
"animations", "samples", "LoadImageColors", "LoadImagePalette",
|
||||||
|
"LoadFontData", "LoadCodepoints", "TextSplit", "LoadMaterials",
|
||||||
|
"LoadModelAnimations", "LoadWaveSamples", "images"
|
||||||
|
]
|
||||||
string = False
|
string = False
|
||||||
|
|
||||||
if t.startswith("[*c]") and name not in single and name not in multi:
|
if t.startswith("[*c]") and name not in single and name not in multi:
|
||||||
if (t == "[*c]const u8" or t == "[*c]u8") and name not in NO_STRINGS: # strings are multis
|
if (t == "[*c]const u8" or t == "[*c]u8") and name not in NO_STRINGS: # Strings are multis.
|
||||||
string = True
|
string = True
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"{t} {name} not classified")
|
raise ValueError(f"{t} {name} not classified")
|
||||||
@ -92,8 +104,17 @@ def make_return_cast(source_type: str, dest_type: str, inner: str) -> str:
|
|||||||
if source_type in ZIGGIFY:
|
if source_type in ZIGGIFY:
|
||||||
return f"@as({dest_type}, {inner})"
|
return f"@as({dest_type}, {inner})"
|
||||||
|
|
||||||
# These all have to be done manually because their sizes depend on the function arguments
|
# These all have to be done manually because their sizes depend on the
|
||||||
if source_type in ["[*c]Color", "[*c]GlyphInfo", "[*c]c_int", "[*c][*c]const u8", "[*c]Material", "[*c]ModelAnimation", "[*c]f32"]:
|
# function arguments.
|
||||||
|
if source_type in [
|
||||||
|
"[*c]Color",
|
||||||
|
"[*c]GlyphInfo",
|
||||||
|
"[*c]c_int",
|
||||||
|
"[*c][*c]const u8",
|
||||||
|
"[*c]Material",
|
||||||
|
"[*c]ModelAnimation",
|
||||||
|
"[*c]f32",
|
||||||
|
]:
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"Don't know what to do {source_type} {dest_type} {inner}")
|
raise ValueError(f"Don't know what to do {source_type} {dest_type} {inner}")
|
||||||
@ -116,8 +137,8 @@ def fix_pointer(name: str, t: str):
|
|||||||
|
|
||||||
|
|
||||||
def fix_enums(arg_name, arg_type, func_name):
|
def fix_enums(arg_name, arg_type, func_name):
|
||||||
# Hacking specific enums in here
|
# Hacking specific enums in here.
|
||||||
# Raylib doesn't use the enums but rather the resulting ints
|
# Raylib doesn't use the enums but rather the resulting ints.
|
||||||
if arg_type == "int" or arg_type == "unsigned int":
|
if arg_type == "int" or arg_type == "unsigned int":
|
||||||
if arg_name == "key":
|
if arg_name == "key":
|
||||||
arg_type = "KeyboardKey"
|
arg_type = "KeyboardKey"
|
||||||
@ -130,7 +151,9 @@ def fix_enums(arg_name, arg_type, func_name):
|
|||||||
arg_type = "CameraMode"
|
arg_type = "CameraMode"
|
||||||
elif arg_name == "gesture":
|
elif arg_name == "gesture":
|
||||||
arg_type = "Gesture"
|
arg_type = "Gesture"
|
||||||
elif arg_name == "flags" and func_name in ["SetWindowState", "ClearWindowState", "SetConfigFlags"]:
|
elif arg_name == "flags" and func_name in [
|
||||||
|
"SetWindowState", "ClearWindowState", "SetConfigFlags"
|
||||||
|
]:
|
||||||
arg_type = "ConfigFlags"
|
arg_type = "ConfigFlags"
|
||||||
elif arg_name == "logLevel":
|
elif arg_name == "logLevel":
|
||||||
arg_type = "TraceLogLevel"
|
arg_type = "TraceLogLevel"
|
||||||
@ -154,7 +177,7 @@ def parse_header(header_name: str, output_file: str, ext_file: str, prefix: str,
|
|||||||
if line.startswith("typedef struct"):
|
if line.startswith("typedef struct"):
|
||||||
zig_types.add(line.split(' ')[2])
|
zig_types.add(line.split(' ')[2])
|
||||||
elif line.startswith("typedef enum"):
|
elif line.startswith("typedef enum"):
|
||||||
# don't trip the general typedef case
|
# Don't trip the general typedef case.
|
||||||
pass
|
pass
|
||||||
elif line.startswith("typedef "):
|
elif line.startswith("typedef "):
|
||||||
zig_types.add(line.split(' ')[2].replace(';', '').strip())
|
zig_types.add(line.split(' ')[2].replace(';', '').strip())
|
||||||
@ -173,14 +196,17 @@ def parse_header(header_name: str, output_file: str, ext_file: str, prefix: str,
|
|||||||
line = line.replace(",", ", ")
|
line = line.replace(",", ", ")
|
||||||
line = line.replace(" ", " ")
|
line = line.replace(" ", " ")
|
||||||
|
|
||||||
# each (.*) is some variable value
|
# Each (.*) is some variable value.
|
||||||
result = re.search(prefix + "(.*) (.*)start_arg(.*)end_arg(.*)", line.replace("(", "start_arg").replace(")", "end_arg"))
|
result = re.search(
|
||||||
|
prefix + "(.*) (.*)start_arg(.*)end_arg(.*)",
|
||||||
|
line.replace("(", "start_arg").replace(")", "end_arg"),
|
||||||
|
)
|
||||||
|
|
||||||
if result is None:
|
if result is None:
|
||||||
leftover += line
|
leftover += line
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# get whats in the (.*)'s
|
# Get whats in the (.*)'s.
|
||||||
return_type = result.group(1)
|
return_type = result.group(1)
|
||||||
func_name = result.group(2)
|
func_name = result.group(2)
|
||||||
arguments = result.group(3)
|
arguments = result.group(3)
|
||||||
@ -207,21 +233,20 @@ def parse_header(header_name: str, output_file: str, ext_file: str, prefix: str,
|
|||||||
if arg == "...":
|
if arg == "...":
|
||||||
zig_c_arguments.append("...")
|
zig_c_arguments.append("...")
|
||||||
continue
|
continue
|
||||||
# everything but the last element (for stuff like "const Vector3")
|
# Everything but the last element (for stuff like "const Vector3").
|
||||||
arg_type = " ".join(arg.split(" ")[0:-1])
|
arg_type = " ".join(arg.split(" ")[0:-1])
|
||||||
arg_name = arg.split(" ")[-1] # last element should be the name
|
arg_name = arg.split(" ")[-1] # Last element should be the name.
|
||||||
arg_type = fix_enums(arg_name, arg_type, func_name)
|
arg_type = fix_enums(arg_name, arg_type, func_name)
|
||||||
|
|
||||||
if arg_name == "type": arg_name = "ty"
|
if arg_name == "type":
|
||||||
|
arg_name = "ty"
|
||||||
|
|
||||||
arg_type = c_to_zig_type(arg_type)
|
arg_type = c_to_zig_type(arg_type)
|
||||||
arg_name, arg_type = fix_pointer(arg_name, arg_type)
|
arg_name, arg_type = fix_pointer(arg_name, arg_type)
|
||||||
zig_type = ziggify_type(arg_name, arg_type)
|
zig_type = ziggify_type(arg_name, arg_type)
|
||||||
|
|
||||||
is_rl_type = arg_type[0].isupper()
|
|
||||||
|
|
||||||
zig_types.add(arg_type)
|
zig_types.add(arg_type)
|
||||||
zig_c_arguments.append(arg_name + ": " + add_namespace_to_type(arg_type)) # put everything together
|
zig_c_arguments.append(arg_name + ": " + add_namespace_to_type(arg_type)) # Put everything together.
|
||||||
zig_arguments.append(arg_name + ": " + zig_type)
|
zig_arguments.append(arg_name + ": " + zig_type)
|
||||||
if arg_type == zig_type:
|
if arg_type == zig_type:
|
||||||
zig_call_args.append(arg_name)
|
zig_call_args.append(arg_name)
|
||||||
@ -237,7 +262,7 @@ def parse_header(header_name: str, output_file: str, ext_file: str, prefix: str,
|
|||||||
|
|
||||||
zig_name = convert_name_case(func_name)
|
zig_name = convert_name_case(func_name)
|
||||||
|
|
||||||
# Todo: ziggify return type
|
# TODO: Ziggify return type
|
||||||
zig_arguments = ", ".join(zig_arguments)
|
zig_arguments = ", ".join(zig_arguments)
|
||||||
zig_call_args = ", ".join(zig_call_args)
|
zig_call_args = ", ".join(zig_call_args)
|
||||||
|
|
||||||
@ -276,11 +301,13 @@ def parse_header(header_name: str, output_file: str, ext_file: str, prefix: str,
|
|||||||
return_cast = make_return_cast(return_type, zig_return, f"cdef.{func_name}({zig_call_args})")
|
return_cast = make_return_cast(return_type, zig_return, f"cdef.{func_name}({zig_call_args})")
|
||||||
|
|
||||||
if return_cast:
|
if return_cast:
|
||||||
zig_funcs.append(f"pub fn {zig_name}({zig_arguments}) {zig_return}" +
|
zig_funcs.append(
|
||||||
" {\n " +
|
f"pub fn {zig_name}({zig_arguments}) {zig_return}" +
|
||||||
("return " if zig_return != "void" else "") +
|
" {\n " +
|
||||||
return_cast + ";"
|
("return " if zig_return != "void" else "") +
|
||||||
"\n}")
|
return_cast + ";"
|
||||||
|
"\n}"
|
||||||
|
)
|
||||||
|
|
||||||
prelude = open(args[0], mode="r").read()
|
prelude = open(args[0], mode="r").read()
|
||||||
ext_prelude = open(args[1], mode="r").read()
|
ext_prelude = open(args[1], mode="r").read()
|
||||||
@ -295,5 +322,19 @@ def parse_header(header_name: str, output_file: str, ext_file: str, prefix: str,
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parse_header("raylib.h", "raylib-zig.zig", "raylib-zig-ext.zig", "RLAPI ", "preludes/raylib-zig-prelude.zig", "preludes/raylib-zig-ext-prelude.zig")
|
parse_header(
|
||||||
parse_header("raymath.h", "raylib-zig-math.zig", "raylib-zig-math-ext.zig", "RMAPI ", "preludes/raylib-zig-math-prelude.zig", "preludes/raylib-zig-math-ext-prelude.zig")
|
"raylib.h",
|
||||||
|
"raylib-zig.zig",
|
||||||
|
"raylib-zig-ext.zig",
|
||||||
|
"RLAPI ",
|
||||||
|
"preludes/raylib-zig-prelude.zig",
|
||||||
|
"preludes/raylib-zig-ext-prelude.zig"
|
||||||
|
)
|
||||||
|
parse_header(
|
||||||
|
"raymath.h",
|
||||||
|
"raylib-zig-math.zig",
|
||||||
|
"raylib-zig-math-ext.zig",
|
||||||
|
"RMAPI ",
|
||||||
|
"preludes/raylib-zig-math-prelude.zig",
|
||||||
|
"preludes/raylib-zig-math-ext-prelude.zig"
|
||||||
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user