Compare commits
19 Commits
c562850645
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 9ac75ebc8c | |||
| a7742ffcb5 | |||
| bc0816e76d | |||
| 1f22745e05 | |||
| b8e2f8a4d7 | |||
| a9791962b0 | |||
| cb093cc030 | |||
| 8029d26c6b | |||
| 87abfe8900 | |||
| acaae99107 | |||
|
|
7b16c7511f | ||
|
|
f3fc2d5c41 | ||
|
|
b7264e20f2 | ||
|
|
6f2cdd8d62 | ||
| 8d0c731b72 | |||
| f95d68f3d2 | |||
| 2bc4d3bd5e | |||
| a8f989b76d | |||
| 51a8c66bc3 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1 +1,4 @@
|
||||
__pycache__
|
||||
*.c
|
||||
*.h
|
||||
main
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
from .codegen import test_codegen
|
||||
from .codegen import test_str8, test_doubly_linked_list, test_typed_array
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_codegen()
|
||||
test_str8()
|
||||
test_typed_array()
|
||||
test_doubly_linked_list()
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
from pathlib import Path
|
||||
from .utils import load_func_body_from_file
|
||||
from .datatypes import (
|
||||
CDataType,
|
||||
CStruct,
|
||||
CEnum,
|
||||
CEnumVal,
|
||||
@@ -12,10 +14,12 @@ from .datatypes import (
|
||||
CPointerType,
|
||||
CQualifier,
|
||||
CInclude,
|
||||
CUserType,
|
||||
get_datatype_string,
|
||||
)
|
||||
|
||||
|
||||
def test_codegen():
|
||||
def test_str8():
|
||||
struct = CStruct(
|
||||
name="Str8",
|
||||
cargs=[
|
||||
@@ -55,7 +59,7 @@ def test_codegen():
|
||||
)
|
||||
|
||||
header = CHeader(
|
||||
name="str.h",
|
||||
name="str",
|
||||
includes=[
|
||||
CInclude(header="aliases.h", local=True),
|
||||
],
|
||||
@@ -64,7 +68,7 @@ def test_codegen():
|
||||
)
|
||||
|
||||
source = CSource(
|
||||
name="str.c",
|
||||
name="str",
|
||||
includes=[
|
||||
CInclude(header=header, local=True),
|
||||
CInclude(header="aliases.h", local=True),
|
||||
@@ -78,5 +82,229 @@ def test_codegen():
|
||||
source.save(Path("."))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_codegen()
|
||||
def test_typed_array():
|
||||
datatypes = [CType.U8, CType.F32, CType.CHAR]
|
||||
types: list[CUserType] = []
|
||||
funcs: list[CFunc] = []
|
||||
|
||||
getter_func_body = """\
|
||||
if(idx >= arr->size) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return arr->data[idx];"""
|
||||
|
||||
setter_func_body = """\
|
||||
if(idx >= arr->size) {
|
||||
return;
|
||||
}
|
||||
|
||||
arr->data[idx] = val;"""
|
||||
|
||||
for _type in datatypes:
|
||||
struct = CStruct(
|
||||
name=f"Array{str(_type).title()}".strip(),
|
||||
cargs=[
|
||||
CArg(name="size", _type=CType.U64),
|
||||
CArg(name="data", _type=_type, pointer=CPointer(_type=CPointerType.SINGLE)),
|
||||
],
|
||||
)
|
||||
types.append(struct)
|
||||
|
||||
funcs.append(CFunc(
|
||||
name=f"array_{str(_type)}_get",
|
||||
ret_type=_type,
|
||||
args=[
|
||||
CArg(name="arr", _type=struct, pointer=CPointer(_type=CPointerType.SINGLE), qualifier=CQualifier.CONST),
|
||||
CArg(name="idx", _type=CType.U64),
|
||||
],
|
||||
body=getter_func_body,
|
||||
))
|
||||
|
||||
funcs.append(CFunc(
|
||||
name=f"array_{str(_type)}_set",
|
||||
ret_type=CType.VOID,
|
||||
args=[
|
||||
CArg(name="arr", _type=struct, pointer=CPointer(_type=CPointerType.SINGLE)),
|
||||
CArg(name="idx", _type=CType.U64),
|
||||
CArg(name="val", _type=_type)
|
||||
],
|
||||
body=setter_func_body,
|
||||
))
|
||||
|
||||
header = CHeader(
|
||||
name="typed_array",
|
||||
includes=[
|
||||
CInclude(header="aliases.h", local=True)
|
||||
],
|
||||
types=types,
|
||||
funcs=funcs
|
||||
)
|
||||
|
||||
source = CSource(
|
||||
name="typed_array",
|
||||
includes=[
|
||||
CInclude(header="aliases.h", local=True),
|
||||
CInclude(header=header, local=True)
|
||||
],
|
||||
funcs=funcs
|
||||
)
|
||||
|
||||
header.save(Path("."))
|
||||
source.save(Path("."))
|
||||
|
||||
|
||||
def test_doubly_linked_list():
|
||||
def __format_func_body(filename: Path, type_string: str):
|
||||
return load_func_body_from_file(filename).format(
|
||||
T=type_string,
|
||||
Tupper=type_string.upper(),
|
||||
Tlower=type_string.lower(),
|
||||
)
|
||||
|
||||
datatypes: dict[CDataType, list[CInclude]] = {"Str8": [CInclude(header="str8.h", local=True)]}
|
||||
snippets_dir = Path(__file__).parent / "snippets"
|
||||
|
||||
for _type, includes in datatypes.items():
|
||||
type_string = get_datatype_string(_type)
|
||||
|
||||
node = CStruct(
|
||||
name=f"{type_string}Node",
|
||||
cargs=[
|
||||
CArg(name="string", _type=type_string, pointer=CPointer(_type=CPointerType.SINGLE)),
|
||||
CArg(name="prev", _type=f"{type_string}Node", pointer=CPointer(_type=CPointerType.SINGLE)),
|
||||
CArg(name="next", _type=f"{type_string}Node", pointer=CPointer(_type=CPointerType.SINGLE)),
|
||||
],
|
||||
)
|
||||
|
||||
dl_list = CStruct(
|
||||
name=f"{type_string}List",
|
||||
cargs=[
|
||||
CArg(name="first", _type=node, pointer=CPointer(_type=CPointerType.SINGLE)),
|
||||
CArg(name="last", _type=node, pointer=CPointer(_type=CPointerType.SINGLE)),
|
||||
CArg(name="total_size", _type=CType.U64),
|
||||
CArg(name="node_count", _type=CType.U64),
|
||||
],
|
||||
)
|
||||
|
||||
get_func = CFunc(
|
||||
name=f"wapp_{type_string.lower()}_list_get",
|
||||
ret_type=node,
|
||||
args=[
|
||||
CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE), qualifier=CQualifier.CONST),
|
||||
CArg(name="index", _type=CType.U64),
|
||||
],
|
||||
body=__format_func_body(snippets_dir / "list_get", type_string),
|
||||
pointer=CPointer(CPointerType.SINGLE),
|
||||
)
|
||||
|
||||
push_front_func = CFunc(
|
||||
name=f"wapp_{type_string.lower()}_list_push_front",
|
||||
ret_type=CType.VOID,
|
||||
args=[
|
||||
CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)),
|
||||
CArg(name="node", _type=node, pointer=CPointer(CPointerType.SINGLE)),
|
||||
],
|
||||
body=__format_func_body(snippets_dir / "list_push_front", type_string),
|
||||
)
|
||||
|
||||
push_back_func = CFunc(
|
||||
name=f"wapp_{type_string.lower()}_list_push_back",
|
||||
ret_type=CType.VOID,
|
||||
args=[
|
||||
CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)),
|
||||
CArg(name="node", _type=node, pointer=CPointer(CPointerType.SINGLE)),
|
||||
],
|
||||
body=__format_func_body(snippets_dir / "list_push_back", type_string),
|
||||
)
|
||||
|
||||
insert_func = CFunc(
|
||||
name=f"wapp_{type_string.lower()}_list_insert",
|
||||
ret_type=CType.VOID,
|
||||
args=[
|
||||
CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)),
|
||||
CArg(name="node", _type=node, pointer=CPointer(CPointerType.SINGLE)),
|
||||
CArg(name="index", _type=CType.U64),
|
||||
],
|
||||
body=__format_func_body(snippets_dir / "list_insert", type_string),
|
||||
)
|
||||
|
||||
pop_front_func = CFunc(
|
||||
name=f"wapp_{type_string.lower()}_list_pop_front",
|
||||
ret_type=node,
|
||||
args=[
|
||||
CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)),
|
||||
],
|
||||
body=__format_func_body(snippets_dir / "list_pop_front", type_string),
|
||||
pointer=CPointer(CPointerType.SINGLE),
|
||||
)
|
||||
|
||||
pop_back_func = CFunc(
|
||||
name=f"wapp_{type_string.lower()}_list_pop_back",
|
||||
ret_type=node,
|
||||
args=[
|
||||
CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)),
|
||||
],
|
||||
body=__format_func_body(snippets_dir / "list_pop_back", type_string),
|
||||
pointer=CPointer(CPointerType.SINGLE),
|
||||
)
|
||||
|
||||
remove_func = CFunc(
|
||||
name=f"wapp_{type_string.lower()}_list_remove",
|
||||
ret_type=node,
|
||||
args=[
|
||||
CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)),
|
||||
CArg(name="index", _type=CType.U64),
|
||||
],
|
||||
body=__format_func_body(snippets_dir / "list_remove", type_string),
|
||||
pointer=CPointer(CPointerType.SINGLE),
|
||||
)
|
||||
|
||||
empty_func = CFunc(
|
||||
name=f"wapp_{type_string.lower()}_list_empty",
|
||||
ret_type=CType.VOID,
|
||||
args=[
|
||||
CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)),
|
||||
],
|
||||
body=__format_func_body(snippets_dir / "list_empty", type_string),
|
||||
)
|
||||
|
||||
node_to_list_func = CFunc(
|
||||
name=f"{type_string.lower()}_node_to_list",
|
||||
ret_type=dl_list,
|
||||
args=[
|
||||
CArg(name="node", _type=node, pointer=CPointer(CPointerType.SINGLE)),
|
||||
],
|
||||
body=__format_func_body(snippets_dir / "node_to_list", type_string),
|
||||
qualifiers=[CQualifier.INTERNAL],
|
||||
)
|
||||
|
||||
header = CHeader(
|
||||
name=f"{type_string.lower()}_list",
|
||||
includes=[CInclude(header="aliases.h", local=True)],
|
||||
types=[node, dl_list],
|
||||
funcs=[
|
||||
get_func,
|
||||
push_front_func,
|
||||
push_back_func,
|
||||
insert_func,
|
||||
pop_front_func,
|
||||
pop_back_func,
|
||||
remove_func,
|
||||
empty_func,
|
||||
]
|
||||
)
|
||||
|
||||
source = CSource(
|
||||
name=header.name,
|
||||
includes=[CInclude(header="aliases.h", local=True), CInclude(header, local=True), CInclude(header="stddef.h")],
|
||||
internal_funcs=[node_to_list_func],
|
||||
funcs=header.funcs
|
||||
)
|
||||
|
||||
if len(includes) > 0:
|
||||
header.includes.extend(includes)
|
||||
source.includes.extend(includes)
|
||||
|
||||
header.save(Path("."))
|
||||
source.save(Path("."))
|
||||
|
||||
@@ -73,15 +73,47 @@ class CEnum:
|
||||
values: list[CEnumVal]
|
||||
typedef: bool = False
|
||||
|
||||
def __str__(self) -> str:
|
||||
if self.typedef:
|
||||
header = "typedef enum {\n"
|
||||
footer = f"}} {self.name};\n"
|
||||
else:
|
||||
header = f"enum {self.name} {{\n"
|
||||
footer = "};\n"
|
||||
|
||||
values = ""
|
||||
for value in self.values:
|
||||
values += f" {str(value)},\n"
|
||||
|
||||
return header + values + footer
|
||||
|
||||
|
||||
@dataclass
|
||||
class CStruct:
|
||||
name: str
|
||||
cargs: list["CArg"]
|
||||
typedef_name: str | None = None
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.declare() + self.define()
|
||||
|
||||
def declare(self) -> str:
|
||||
declaration = f"typedef struct {self.name} {self.typedef_name if self.typedef_name is not None else self.name};\n"
|
||||
return declaration
|
||||
|
||||
def define(self):
|
||||
definition = f"struct {self.name} {{\n"
|
||||
args = ""
|
||||
for arg in self.cargs:
|
||||
args += f" {str(arg)};\n"
|
||||
footer = "};\n"
|
||||
|
||||
return definition + args + footer;
|
||||
|
||||
|
||||
CUserType = Union[CStruct, CEnum]
|
||||
CDataType = Union[CType, CUserType]
|
||||
CDataType = Union[CType, CUserType, str]
|
||||
|
||||
|
||||
@dataclass
|
||||
class CArg:
|
||||
@@ -93,7 +125,7 @@ class CArg:
|
||||
|
||||
def __str__(self) -> str:
|
||||
qualifier = str(self.qualifier)
|
||||
_type = get_arg_type_string(self._type)
|
||||
_type = get_datatype_string(self._type) + " "
|
||||
pointer = str(self.pointer)
|
||||
array = "[]" if self.array else ""
|
||||
|
||||
@@ -111,9 +143,11 @@ class CFunc:
|
||||
|
||||
def __str__(self) -> str:
|
||||
qualifiers = ""
|
||||
for qualifier in qualifiers:
|
||||
for qualifier in self.qualifiers:
|
||||
if qualifier == CQualifier.NONE:
|
||||
continue
|
||||
if len(qualifiers) > 0:
|
||||
qualifiers += " "
|
||||
qualifiers += f"{str(qualifier)}"
|
||||
|
||||
args = ""
|
||||
@@ -122,27 +156,48 @@ class CFunc:
|
||||
if i + 1 < len(self.args):
|
||||
args += ", "
|
||||
|
||||
return qualifiers + str(self.ret_type) + str(self.pointer) + self.name + f"({args})"
|
||||
return qualifiers + get_datatype_string(self.ret_type) + " " + str(self.pointer) + self.name + f"({args})"
|
||||
|
||||
def declare(self) -> str:
|
||||
return f"{str(self)};\n"
|
||||
|
||||
def define(self) -> str:
|
||||
return f"{str(self)} {{\n{self.body}\n}}\n"
|
||||
return f"{str(self)} {{\n{self.body}\n}}\n\n"
|
||||
|
||||
|
||||
@dataclass
|
||||
class CInclude:
|
||||
header: Union[str, "CHeader"]
|
||||
local: bool = False
|
||||
same_dir: bool = False
|
||||
|
||||
def __str__(self) -> str:
|
||||
if isinstance(self.header, CHeader):
|
||||
name = f"{self.header.name}.{self.header.extension}"
|
||||
else:
|
||||
name = self.header
|
||||
|
||||
if self.local:
|
||||
open_symbol = '"'
|
||||
close_symbol = '"'
|
||||
|
||||
if self.same_dir:
|
||||
name = f"./{name}"
|
||||
else:
|
||||
open_symbol = '<'
|
||||
close_symbol = '>'
|
||||
|
||||
return f"#include {open_symbol}{name}{close_symbol}\n"
|
||||
|
||||
|
||||
@dataclass
|
||||
class CFile:
|
||||
name: str
|
||||
extension: str
|
||||
decl_types: list[CStruct] = field(default_factory=list)
|
||||
|
||||
def save(self, output_dir: Path):
|
||||
output_file = output_dir / self.name
|
||||
output_file = output_dir / f"{self.name}.{self.extension}"
|
||||
with open(output_file, "w+") as outfile:
|
||||
outfile.write(str(self))
|
||||
|
||||
@@ -152,39 +207,59 @@ class CFile:
|
||||
|
||||
@dataclass
|
||||
class CHeader(CFile):
|
||||
includes: list[CInclude]
|
||||
types: list[CUserType]
|
||||
funcs: list[CFunc]
|
||||
extension: str = "h"
|
||||
includes: list[CInclude] = field(default_factory=list)
|
||||
types: list[CUserType] = field(default_factory=list)
|
||||
funcs: list[CFunc] = field(default_factory=list)
|
||||
|
||||
def __str__(self) -> str:
|
||||
pragma = "#pragma once\n\n"
|
||||
name_upper = self.name.upper()
|
||||
header_guard_name = f"{name_upper}_H"
|
||||
header_guard_open = f"#ifndef {header_guard_name}\n#define {header_guard_name}\n\n"
|
||||
header_guard_close = f"#endif // !{header_guard_name}\n"
|
||||
|
||||
includes = get_includes_string(self.includes)
|
||||
c_linkage_open = "#ifdef __cplusplus\nBEGIN_C_LINKAGE\n#endif // !__cplusplus\n\n"
|
||||
c_linkage_close = "\n#ifdef __cplusplus\nEND_C_LINKAGE\n#endif // !__cplusplus\n\n"
|
||||
|
||||
includes = _get_includes_string(self.includes)
|
||||
|
||||
forward_declarations = ""
|
||||
for _type in self.decl_types:
|
||||
forward_declarations += _type.declare()
|
||||
if len(forward_declarations) > 0:
|
||||
forward_declarations += "\n"
|
||||
|
||||
types = ""
|
||||
for _type in self.types:
|
||||
types += get_user_type_string(_type)
|
||||
types += str(_type) + "\n"
|
||||
|
||||
funcs = ""
|
||||
for func in self.funcs:
|
||||
funcs += func.declare()
|
||||
|
||||
return pragma + includes + types + funcs
|
||||
return header_guard_open + includes + c_linkage_open + forward_declarations + types + funcs + c_linkage_close + header_guard_close
|
||||
|
||||
|
||||
@dataclass
|
||||
class CSource(CFile):
|
||||
includes: list[CInclude]
|
||||
types: list[CUserType]
|
||||
internal_funcs: list[CFunc]
|
||||
funcs: list[CFunc]
|
||||
extension: str = "c"
|
||||
includes: list[CInclude] = field(default_factory=list)
|
||||
types: list[CUserType] = field(default_factory=list)
|
||||
internal_funcs: list[CFunc] = field(default_factory=list)
|
||||
funcs: list[CFunc] = field(default_factory=list)
|
||||
|
||||
def __str__(self) -> str:
|
||||
includes = get_includes_string(self.includes)
|
||||
includes = _get_includes_string(self.includes)
|
||||
|
||||
forward_declarations = ""
|
||||
for _type in self.decl_types:
|
||||
forward_declarations += _type.declare()
|
||||
if len(forward_declarations) > 0:
|
||||
forward_declarations += "\n"
|
||||
|
||||
types = ""
|
||||
for _type in self.types:
|
||||
types += get_user_type_string(_type)
|
||||
types += str(_type) + "\n"
|
||||
|
||||
internal_funcs_decl = ""
|
||||
internal_funcs_def = ""
|
||||
@@ -192,72 +267,29 @@ class CSource(CFile):
|
||||
internal_funcs_decl += func.declare()
|
||||
internal_funcs_def += func.define()
|
||||
|
||||
if len(internal_funcs_decl) > 0:
|
||||
internal_funcs_decl += "\n"
|
||||
|
||||
funcs = ""
|
||||
for func in self.funcs:
|
||||
funcs += func.define()
|
||||
|
||||
return includes + types + internal_funcs_decl + funcs + internal_funcs_def
|
||||
return includes + forward_declarations + types + internal_funcs_decl + funcs + internal_funcs_def
|
||||
|
||||
|
||||
def get_user_type_string(_type: Union[CStruct, CEnum]) -> str:
|
||||
type_str = ""
|
||||
if isinstance(_type, CStruct):
|
||||
type_str += cstruct_to_string(_type) + "\n"
|
||||
else:
|
||||
type_str += cenum_to_string(_type) + "\n"
|
||||
|
||||
return type_str
|
||||
|
||||
|
||||
def cenum_to_string(cenum: CEnum) -> str:
|
||||
if cenum.typedef:
|
||||
header = "typedef enum {\n"
|
||||
footer = f"}} {cenum.name};\n"
|
||||
else:
|
||||
header = f"enum {cenum.name} {{\n"
|
||||
footer = "};\n"
|
||||
|
||||
values = ""
|
||||
for value in cenum.values:
|
||||
values += f" {str(value)},\n"
|
||||
|
||||
return header + values + footer
|
||||
|
||||
|
||||
def cstruct_to_string(cstruct: CStruct) -> str:
|
||||
typedef = f"typedef struct {cstruct.name} {cstruct.name};\n"
|
||||
header = f"struct {cstruct.name} {{\n"
|
||||
args = ""
|
||||
for arg in cstruct.cargs:
|
||||
args += f" {str(arg)};\n"
|
||||
footer = "};\n"
|
||||
|
||||
return typedef + header + args + footer;
|
||||
|
||||
|
||||
def get_arg_type_string(_type: CDataType) -> str:
|
||||
def get_datatype_string(_type: CDataType) -> str:
|
||||
if isinstance(_type, CType):
|
||||
return str(_type)
|
||||
elif isinstance(_type, CStruct) or isinstance(_type, CEnum):
|
||||
return _type.name
|
||||
elif isinstance(_type, str):
|
||||
return _type
|
||||
|
||||
|
||||
def get_includes_string(includes: list[CInclude]) -> str:
|
||||
def _get_includes_string(includes: list[CInclude]) -> str:
|
||||
output = ""
|
||||
for include in includes:
|
||||
if isinstance(include.header, CHeader):
|
||||
name = include.header.name
|
||||
else:
|
||||
name = include.header
|
||||
|
||||
if include.local:
|
||||
open_symbol = '"'
|
||||
close_symbol = '"'
|
||||
else:
|
||||
open_symbol = '<'
|
||||
close_symbol = '>'
|
||||
|
||||
output += f"#include {open_symbol}{name}{close_symbol}\n"
|
||||
for include in sorted(includes, key=lambda inc: inc.local, reverse=True):
|
||||
output += str(include)
|
||||
if len(output) > 0:
|
||||
output += "\n"
|
||||
|
||||
|
||||
8
codegen/snippets/list_empty
Normal file
8
codegen/snippets/list_empty
Normal file
@@ -0,0 +1,8 @@
|
||||
if (!list) {{
|
||||
return;
|
||||
}}
|
||||
|
||||
u64 count = list->node_count;
|
||||
for (u64 i = 0; i < count; ++i) {{
|
||||
wapp_str8_list_pop_back(list);
|
||||
}}
|
||||
13
codegen/snippets/list_get
Normal file
13
codegen/snippets/list_get
Normal file
@@ -0,0 +1,13 @@
|
||||
if (index >= list->node_count) {{
|
||||
return NULL;
|
||||
}}
|
||||
|
||||
{T}Node *output = NULL;
|
||||
{T}Node *current = list->first;
|
||||
for (u64 i = 1; i <= index; ++i) {{
|
||||
current = current->next;
|
||||
}}
|
||||
|
||||
output = current;
|
||||
|
||||
return output;
|
||||
29
codegen/snippets/list_insert
Normal file
29
codegen/snippets/list_insert
Normal file
@@ -0,0 +1,29 @@
|
||||
if (!list || !node || !(node->string)) {{
|
||||
return;
|
||||
}}
|
||||
|
||||
if (index == 0) {{
|
||||
wapp_str8_list_push_front(list, node);
|
||||
return;
|
||||
}} else if (index == list->node_count) {{
|
||||
wapp_str8_list_push_back(list, node);
|
||||
return;
|
||||
}}
|
||||
|
||||
{T}Node *dst_node = wapp_str8_list_get(list, index);
|
||||
if (!dst_node) {{
|
||||
return;
|
||||
}}
|
||||
|
||||
{T}List node_list = {Tlower}_node_to_list(node);
|
||||
|
||||
list->total_size += node_list.total_size;
|
||||
list->node_count += node_list.node_count;
|
||||
|
||||
{T}Node *prev = dst_node->prev;
|
||||
|
||||
dst_node->prev = node_list.last;
|
||||
prev->next = node_list.first;
|
||||
|
||||
node_list.first->prev = prev;
|
||||
node_list.last->next = dst_node;
|
||||
21
codegen/snippets/list_pop_back
Normal file
21
codegen/snippets/list_pop_back
Normal file
@@ -0,0 +1,21 @@
|
||||
{T}Node *output = NULL;
|
||||
|
||||
if (!list || list->node_count == 0) {{
|
||||
goto RETURN_{Tupper}_LIST_POP_BACK;
|
||||
}}
|
||||
|
||||
output = list->last;
|
||||
|
||||
if (list->node_count == 1) {{
|
||||
*list = ({T}List){{0}};
|
||||
goto RETURN_{Tupper}_LIST_POP_BACK;
|
||||
}}
|
||||
|
||||
--(list->node_count);
|
||||
list->total_size -= output->string->size;
|
||||
list->last = output->prev;
|
||||
|
||||
output->prev = output->next = NULL;
|
||||
|
||||
RETURN_{Tupper}_LIST_POP_BACK:
|
||||
return output;
|
||||
21
codegen/snippets/list_pop_front
Normal file
21
codegen/snippets/list_pop_front
Normal file
@@ -0,0 +1,21 @@
|
||||
{T}Node *output = NULL;
|
||||
|
||||
if (!list || list->node_count == 0) {{
|
||||
goto RETURN_{Tupper}_LIST_POP_FRONT;
|
||||
}}
|
||||
|
||||
output = list->first;
|
||||
|
||||
if (list->node_count == 1) {{
|
||||
*list = ({T}List){{0}};
|
||||
goto RETURN_{Tupper}_LIST_POP_FRONT;
|
||||
}}
|
||||
|
||||
--(list->node_count);
|
||||
list->total_size -= output->string->size;
|
||||
list->first = output->next;
|
||||
|
||||
output->prev = output->next = NULL;
|
||||
|
||||
RETURN_{Tupper}_LIST_POP_FRONT:
|
||||
return output;
|
||||
21
codegen/snippets/list_push_back
Normal file
21
codegen/snippets/list_push_back
Normal file
@@ -0,0 +1,21 @@
|
||||
if (!list || !node || !(node->string)) {{
|
||||
return;
|
||||
}}
|
||||
|
||||
{T}List node_list = {Tlower}_node_to_list(node);
|
||||
|
||||
if (list->node_count == 0) {{
|
||||
*list = node_list;
|
||||
return;
|
||||
}}
|
||||
|
||||
list->total_size += node_list.total_size;
|
||||
list->node_count += node_list.node_count;
|
||||
|
||||
{T}Node *last = list->last;
|
||||
if (last) {{
|
||||
last->next = node_list.first;
|
||||
}}
|
||||
|
||||
list->last = node_list.last;
|
||||
node_list.first->prev = last;
|
||||
21
codegen/snippets/list_push_front
Normal file
21
codegen/snippets/list_push_front
Normal file
@@ -0,0 +1,21 @@
|
||||
if (!list || !node || !(node->string)) {{
|
||||
return;
|
||||
}}
|
||||
|
||||
{T}List node_list = {Tlower}_node_to_list(node);
|
||||
|
||||
if (list->node_count == 0) {{
|
||||
*list = node_list;
|
||||
return;
|
||||
}}
|
||||
|
||||
list->total_size += node_list.total_size;
|
||||
list->node_count += node_list.node_count;
|
||||
|
||||
{T}Node *first = list->first;
|
||||
if (first) {{
|
||||
first->prev = node_list.last;
|
||||
}}
|
||||
|
||||
list->first = node_list.first;
|
||||
node_list.last->next = first;
|
||||
28
codegen/snippets/list_remove
Normal file
28
codegen/snippets/list_remove
Normal file
@@ -0,0 +1,28 @@
|
||||
{T}Node *output = NULL;
|
||||
if (!list) {{
|
||||
goto RETURN_{Tupper}_LIST_REMOVE;
|
||||
}}
|
||||
|
||||
if (index == 0) {{
|
||||
output = wapp_str8_list_pop_front(list);
|
||||
goto RETURN_{Tupper}_LIST_REMOVE;
|
||||
}} else if (index == list->node_count) {{
|
||||
output = wapp_str8_list_pop_back(list);
|
||||
goto RETURN_{Tupper}_LIST_REMOVE;
|
||||
}}
|
||||
|
||||
output = wapp_str8_list_get(list, index);
|
||||
if (!output) {{
|
||||
goto RETURN_{Tupper}_LIST_REMOVE;
|
||||
}}
|
||||
|
||||
output->prev->next = output->next;
|
||||
output->next->prev = output->prev;
|
||||
|
||||
--(list->node_count);
|
||||
list->total_size -= output->string->size;
|
||||
|
||||
output->prev = output->next = NULL;
|
||||
|
||||
RETURN_{Tupper}_LIST_REMOVE:
|
||||
return output;
|
||||
15
codegen/snippets/node_to_list
Normal file
15
codegen/snippets/node_to_list
Normal file
@@ -0,0 +1,15 @@
|
||||
{T}List output = {{.first = node, .last = node, .total_size = node->string->size, .node_count = 1}};
|
||||
|
||||
while (output.first->prev != NULL) {{
|
||||
output.total_size += output.first->prev->string->size;
|
||||
output.first = output.first->prev;
|
||||
++(output.node_count);
|
||||
}}
|
||||
|
||||
while (output.last->next != NULL) {{
|
||||
output.total_size += output.last->next->string->size;
|
||||
output.last = output.last->next;
|
||||
++(output.node_count);
|
||||
}}
|
||||
|
||||
return output;
|
||||
6
codegen/utils.py
Normal file
6
codegen/utils.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def load_func_body_from_file(filename: Path) -> str:
|
||||
with open(filename, "r") as infile:
|
||||
return infile.read()
|
||||
Reference in New Issue
Block a user