genstructs.py: refactor; use match/case statement

- lol, i had no idea python *FINALLY* added a switch/case equivalent.
sadly though, it looks like the generated python bytecode is
nearly identical in performance.
there's no lookup table magic for match/case, and is almost
identical to if/else if/else. it amounts to syntax sugar 😭
This commit is contained in:
CPunch 2023-11-22 16:02:00 -06:00
parent 7eece044c5
commit 7d83732e44

View File

@ -32,10 +32,10 @@ WARN_INVALID = False
class StructTranspiler: class StructTranspiler:
class StructField: class StructField:
def __init__(self, name: str, type: str, marshal: str) -> None: def __init__(self, name: str, csType: str, marshal: str) -> None:
self.marshal = marshal self.marshal = marshal
self.type = type self.type = csType
self.ctype = type # for transpilation to c self.ctype = csType # for transpilation to c
self.tags = "" self.tags = ""
self.size = 0 self.size = 0
self.padding = 0 self.padding = 0
@ -43,50 +43,51 @@ class StructTranspiler:
self.cname = self.name self.cname = self.name
self.needsPatching = False self.needsPatching = False
if type == "byte": match csType:
case "byte":
self.type = "uint8" self.type = "uint8"
self.ctype = "char" self.ctype = "char"
self.size = 1 self.size = 1
elif type == "sbyte": case "sbyte":
self.type = "int8" self.type = "int8"
self.ctype = "char" self.ctype = "char"
self.size = 1 self.size = 1
elif type == "short": case "short":
self.type = "int16" self.type = "int16"
self.ctype = "short" self.ctype = "short"
self.size = 2 self.size = 2
elif type == "int": case "int":
self.type = "int32" self.type = "int32"
self.ctype = "int" self.ctype = "int"
self.size = 4 self.size = 4
elif type == "uint": case "uint":
self.type = "uint32" self.type = "uint32"
self.ctype = "int" self.ctype = "int"
self.size = 4 self.size = 4
elif type == "float": case "float":
self.type = "float32" self.type = "float32"
self.ctype = "float" self.ctype = "float"
self.size = 4 self.size = 4
elif type == "long": case "long":
self.type = "int64" self.type = "int64"
self.ctype = "long" self.ctype = "long"
self.size = 8 self.size = 8
elif type == "ulong": case "ulong":
self.type = "uint64" self.type = "uint64"
self.ctype = "long" self.ctype = "long"
self.size = 8 self.size = 8
elif type == "byte[]": case "byte[]":
self.size = int(marshal[(marshal.find("SizeConst = ") + len("SizeConst = ")):marshal.find(")]")]) self.size = int(marshal[(marshal.find("SizeConst = ") + len("SizeConst = ")):marshal.find(")]")])
self.type = "[%d]byte" % self.size self.type = "[%d]byte" % self.size
self.ctype = "char" self.ctype = "char"
self.cname += "[%d]" % self.size self.cname += "[%d]" % self.size
elif type == "short[]": case "short[]":
self.size = int(marshal[(marshal.find("SizeConst = ") + len("SizeConst = ")):marshal.find(")]")]) self.size = int(marshal[(marshal.find("SizeConst = ") + len("SizeConst = ")):marshal.find(")]")])
self.type = "[%d]int16" % self.size self.type = "[%d]int16" % self.size
self.ctype = "short" self.ctype = "short"
self.cname += "[%d]" % self.size self.cname += "[%d]" % self.size
self.size *= 2 self.size *= 2
elif type == "string": case "string":
# all strings in fusionfall are utf16, in a uint16 array # all strings in fusionfall are utf16, in a uint16 array
self.size = int(marshal[(marshal.find("SizeConst = ") + len("SizeConst = ")):marshal.find(")]")]) self.size = int(marshal[(marshal.find("SizeConst = ") + len("SizeConst = ")):marshal.find(")]")])
self.type = "string" self.type = "string"
@ -94,35 +95,35 @@ class StructTranspiler:
self.ctype = "short" self.ctype = "short"
self.cname += "[%d]" % self.size self.cname += "[%d]" % self.size
self.size *= 2 self.size *= 2
elif type == "int[]": case "int[]":
self.size = int(marshal[(marshal.find("SizeConst = ") + len("SizeConst = ")):marshal.find(")]")]) self.size = int(marshal[(marshal.find("SizeConst = ") + len("SizeConst = ")):marshal.find(")]")])
self.type = "[%d]int32" % self.size self.type = "[%d]int32" % self.size
self.ctype = "int" self.ctype = "int"
self.cname += "[%d]" % self.size self.cname += "[%d]" % self.size
self.size *= 4 self.size *= 4
elif type == "float[]": case "float[]":
self.size = int(marshal[(marshal.find("SizeConst = ") + len("SizeConst = ")):marshal.find(")]")]) self.size = int(marshal[(marshal.find("SizeConst = ") + len("SizeConst = ")):marshal.find(")]")])
self.type = "[%d]float32" % self.size self.type = "[%d]float32" % self.size
self.ctype = "float" self.ctype = "float"
self.cname += "[%d]" % self.size self.cname += "[%d]" % self.size
self.size *= 4 self.size *= 4
elif type == "long[]": case "long[]":
self.size = int(marshal[(marshal.find("SizeConst = ") + len("SizeConst = ")):marshal.find(")]")]) self.size = int(marshal[(marshal.find("SizeConst = ") + len("SizeConst = ")):marshal.find(")]")])
self.type = "[%d]int64" % self.size self.type = "[%d]int64" % self.size
self.ctype = "long" self.ctype = "long"
self.cname += "[%d]" % self.size self.cname += "[%d]" % self.size
self.size *= 8 self.size *= 8
else: case _:
# assume it's a structure that will be defined later # assume it's a structure that will be defined later
if type.find("[]") != -1: # it's an array! if csType.find("[]") != -1: # it's an array!
type = type.replace("[]", "") csType = csType.replace("[]", "")
self.size = int(marshal[(marshal.find("SizeConst = ") + len("SizeConst = ")):marshal.find(")]")]) self.size = int(marshal[(marshal.find("SizeConst = ") + len("SizeConst = ")):marshal.find(")]")])
self.cname = self.name + "[%d]" % self.size self.cname = self.name + "[%d]" % self.size
else: else:
self.cname = self.name self.cname = self.name
self.size = 1 self.size = 1
self.type = sanitizeName(type) self.type = sanitizeName(csType)
self.ctype = sanitizeName(type) self.ctype = sanitizeName(csType)
self.needsPatching = True self.needsPatching = True
def addTag(self, tag: str) -> None: def addTag(self, tag: str) -> None: