Merge branch 'codegen'
This commit is contained in:
15
codegen/__main__.py
Normal file
15
codegen/__main__.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from codegen.datatypes import CDataType
|
||||
from codegen.dbl_list.make_dbl_list import DblListData, make_dbl_list
|
||||
|
||||
|
||||
def main():
|
||||
gen_dbl_list()
|
||||
|
||||
|
||||
def gen_dbl_list():
|
||||
datatypes: dict[CDataType, DblListData] = {}
|
||||
make_dbl_list(datatypes)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
5
codegen/constants.py
Normal file
5
codegen/constants.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
PACKAGE_DIR = Path(__file__).parent.resolve()
|
||||
WAPP_SRC_ROOT = PACKAGE_DIR.parent / "src"
|
343
codegen/datatypes.py
Normal file
343
codegen/datatypes.py
Normal file
@@ -0,0 +1,343 @@
|
||||
from enum import Enum
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
|
||||
class CType(Enum):
|
||||
VOID = "void"
|
||||
BOOL = "bool"
|
||||
CHAR = "char"
|
||||
C8 = "c8"
|
||||
C16 = "c16"
|
||||
C32 = "c32"
|
||||
I8 = "i8"
|
||||
I16 = "i16"
|
||||
I32 = "i32"
|
||||
I64 = "i64"
|
||||
U8 = "u8"
|
||||
U16 = "u16"
|
||||
U32 = "u32"
|
||||
U64 = "u64"
|
||||
F32 = "f32"
|
||||
F64 = "f64"
|
||||
F128 = "f128"
|
||||
IPTR = "iptr"
|
||||
UPTR = "uptr"
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.value
|
||||
|
||||
|
||||
class CQualifier(Enum):
|
||||
NONE = ""
|
||||
CONST = "const "
|
||||
EXTERNAL = "external "
|
||||
INTERNAL = "internal "
|
||||
PERSISTENT = "persistent "
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.value
|
||||
|
||||
|
||||
class CPointerType(Enum):
|
||||
NONE = ""
|
||||
SINGLE = "*"
|
||||
DOUBLE = "**"
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.value
|
||||
|
||||
|
||||
@dataclass
|
||||
class CPointer:
|
||||
_type: CPointerType = CPointerType.NONE
|
||||
qualifier: CQualifier = CQualifier.NONE
|
||||
|
||||
def __str__(self) -> str:
|
||||
return str(self._type) + str(self.qualifier)
|
||||
|
||||
|
||||
@dataclass
|
||||
class CEnumVal:
|
||||
name: str
|
||||
value: Optional[int] = None
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.name + "" if self.value is None else f" = {self.value}"
|
||||
|
||||
|
||||
@dataclass
|
||||
class CEnum:
|
||||
name: str
|
||||
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 CMacro:
|
||||
name: str
|
||||
value: str
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"#define {self.name} {self.value}\n"
|
||||
|
||||
|
||||
@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, str]
|
||||
|
||||
|
||||
@dataclass
|
||||
class CArg:
|
||||
name: str
|
||||
_type: CDataType
|
||||
array: bool = False
|
||||
pointer: CPointer = field(default_factory=CPointer)
|
||||
qualifier: CQualifier = CQualifier.NONE
|
||||
|
||||
def __str__(self) -> str:
|
||||
qualifier = str(self.qualifier)
|
||||
_type = get_datatype_string(self._type) + " "
|
||||
pointer = str(self.pointer)
|
||||
array = "[]" if self.array else ""
|
||||
|
||||
return qualifier + _type + pointer + self.name + array
|
||||
|
||||
|
||||
@dataclass
|
||||
class CFunc:
|
||||
name: str
|
||||
ret_type: CDataType
|
||||
args: list[CArg]
|
||||
body: str
|
||||
pointer: CPointer = field(default_factory=CPointer)
|
||||
qualifiers: list[CQualifier] = field(default_factory=list)
|
||||
|
||||
def __str__(self) -> str:
|
||||
qualifiers = ""
|
||||
for qualifier in self.qualifiers:
|
||||
if qualifier == CQualifier.NONE:
|
||||
continue
|
||||
if len(qualifiers) > 0:
|
||||
qualifiers += " "
|
||||
qualifiers += f"{str(qualifier)}"
|
||||
|
||||
args = ""
|
||||
for i, arg in enumerate(self.args):
|
||||
args += f"{str(arg)}"
|
||||
if i + 1 < len(self.args):
|
||||
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\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)
|
||||
macros: list[CMacro] = field(default_factory=list)
|
||||
|
||||
def save(self, output_dir: Path):
|
||||
output_file = output_dir / f"{self.name}.{self.extension}"
|
||||
with open(output_file, "w+") as outfile:
|
||||
outfile.write(str(self))
|
||||
|
||||
def __str__(self) -> str:
|
||||
return """\
|
||||
/**
|
||||
* THIS FILE IS AUTOMATICALLY GENERATED. ANY MODIFICATIONS TO IT WILL BE OVERWRITTEN
|
||||
*/
|
||||
|
||||
"""
|
||||
|
||||
|
||||
@dataclass
|
||||
class CHeader(CFile):
|
||||
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:
|
||||
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"
|
||||
|
||||
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)
|
||||
|
||||
macros = ""
|
||||
for macro in self.macros:
|
||||
macros += str(macro)
|
||||
if len(macros) > 0:
|
||||
macros += "\n"
|
||||
|
||||
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 += str(_type) + "\n"
|
||||
|
||||
funcs = ""
|
||||
for func in self.funcs:
|
||||
funcs += func.declare()
|
||||
|
||||
return (
|
||||
super().__str__() +
|
||||
header_guard_open +
|
||||
includes +
|
||||
c_linkage_open +
|
||||
macros +
|
||||
forward_declarations +
|
||||
types +
|
||||
funcs +
|
||||
c_linkage_close +
|
||||
header_guard_close
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class CSource(CFile):
|
||||
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)
|
||||
|
||||
macros = ""
|
||||
for macro in self.macros:
|
||||
macros += str(macro)
|
||||
if len(macros) > 0:
|
||||
macros += "\n"
|
||||
|
||||
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 += str(_type) + "\n"
|
||||
|
||||
internal_funcs_decl = ""
|
||||
internal_funcs_def = ""
|
||||
for func in self.internal_funcs:
|
||||
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 (
|
||||
super().__str__() +
|
||||
includes +
|
||||
macros +
|
||||
forward_declarations +
|
||||
types +
|
||||
internal_funcs_decl +
|
||||
funcs +
|
||||
internal_funcs_def
|
||||
)
|
||||
|
||||
|
||||
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:
|
||||
output = ""
|
||||
for include in sorted(includes, key=lambda inc: inc.local, reverse=True):
|
||||
output += str(include)
|
||||
if len(output) > 0:
|
||||
output += "\n"
|
||||
|
||||
return output
|
224
codegen/dbl_list/make_dbl_list.py
Normal file
224
codegen/dbl_list/make_dbl_list.py
Normal file
@@ -0,0 +1,224 @@
|
||||
from pathlib import Path
|
||||
from dataclasses import dataclass, field
|
||||
from codegen.constants import WAPP_SRC_ROOT
|
||||
from codegen.utils import load_func_body_from_file
|
||||
from codegen.datatypes import (
|
||||
CDataType,
|
||||
CMacro,
|
||||
CStruct,
|
||||
CFunc,
|
||||
CHeader,
|
||||
CSource,
|
||||
CArg,
|
||||
CType,
|
||||
CPointer,
|
||||
CPointerType,
|
||||
CQualifier,
|
||||
CInclude,
|
||||
get_datatype_string,
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class DblListData:
|
||||
hdr_decl_types: list[CStruct] = field(default_factory=list)
|
||||
src_decl_types: list[CStruct] = field(default_factory=list)
|
||||
|
||||
|
||||
def make_dbl_list(user_datatypes: dict[CDataType, DblListData] = {}):
|
||||
def __format_func_body(filename: Path, type_string: str):
|
||||
return load_func_body_from_file(filename).format(
|
||||
T=type_string,
|
||||
Ttitle=type_string.title(),
|
||||
Tupper=type_string.upper(),
|
||||
Tlower=type_string.lower(),
|
||||
)
|
||||
|
||||
out_dir = WAPP_SRC_ROOT / "containers" / "dbl_list"
|
||||
out_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
common_local_include_files = [
|
||||
(WAPP_SRC_ROOT / "common" / "aliases" / "aliases.h")
|
||||
]
|
||||
common_includes: list[CInclude] = []
|
||||
for local_file in common_local_include_files:
|
||||
common_includes.append(
|
||||
CInclude(
|
||||
header=str(local_file.relative_to(out_dir, walk_up=True)),
|
||||
local=True,
|
||||
)
|
||||
)
|
||||
|
||||
common_decl_types: list[CStruct] = []
|
||||
|
||||
datatypes: dict[CDataType, DblListData] = {
|
||||
"Str8": DblListData(
|
||||
hdr_decl_types=[
|
||||
CStruct(name="str8", cargs=[], typedef_name="Str8"),
|
||||
],
|
||||
),
|
||||
}
|
||||
datatypes.update(user_datatypes)
|
||||
|
||||
snippets_dir = Path(__file__).parent / "snippets"
|
||||
|
||||
header = CHeader(
|
||||
name="dbl_list",
|
||||
decl_types=common_decl_types,
|
||||
includes=[],
|
||||
types=[],
|
||||
funcs=[]
|
||||
)
|
||||
|
||||
source = CSource(
|
||||
name=header.name,
|
||||
decl_types=common_decl_types,
|
||||
includes=[CInclude(header, local=True, same_dir=True), CInclude(header="stddef.h")],
|
||||
internal_funcs=[],
|
||||
funcs=header.funcs
|
||||
)
|
||||
|
||||
if len(common_includes) > 0:
|
||||
header.includes.extend(common_includes)
|
||||
source.includes.extend(common_includes)
|
||||
|
||||
for _type, dbl_list_data in datatypes.items():
|
||||
type_string = get_datatype_string(_type)
|
||||
|
||||
node = CStruct(
|
||||
name=f"{type_string.title()}Node",
|
||||
cargs=[
|
||||
CArg(name="item", _type=type_string, pointer=CPointer(_type=CPointerType.SINGLE)),
|
||||
],
|
||||
)
|
||||
node.cargs.extend([
|
||||
CArg(name="prev", _type=node, pointer=CPointer(_type=CPointerType.SINGLE)),
|
||||
CArg(name="next", _type=node, pointer=CPointer(_type=CPointerType.SINGLE)),
|
||||
])
|
||||
|
||||
dl_list = CStruct(
|
||||
name=f"{type_string.title()}List",
|
||||
cargs=[
|
||||
CArg(name="first", _type=node, pointer=CPointer(_type=CPointerType.SINGLE)),
|
||||
CArg(name="last", _type=node, pointer=CPointer(_type=CPointerType.SINGLE)),
|
||||
CArg(name="node_count", _type=CType.U64),
|
||||
],
|
||||
)
|
||||
|
||||
node_macro = CMacro(
|
||||
name=f"wapp_{type_string.lower()}_list_node(ITEM_PTR)",
|
||||
value=__format_func_body(snippets_dir / "list_node", type_string),
|
||||
)
|
||||
|
||||
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.decl_types.extend(dbl_list_data.hdr_decl_types)
|
||||
header.macros.append(node_macro)
|
||||
header.types.extend([node, dl_list])
|
||||
header.funcs.extend([
|
||||
get_func,
|
||||
push_front_func,
|
||||
push_back_func,
|
||||
insert_func,
|
||||
pop_front_func,
|
||||
pop_back_func,
|
||||
remove_func,
|
||||
empty_func,
|
||||
])
|
||||
|
||||
source.decl_types.extend(dbl_list_data.src_decl_types)
|
||||
source.internal_funcs.append(node_to_list_func)
|
||||
source.funcs = header.funcs
|
||||
|
||||
header.save(out_dir)
|
||||
source.save(out_dir)
|
8
codegen/dbl_list/snippets/list_empty
Normal file
8
codegen/dbl_list/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_{Tlower}_list_pop_back(list);
|
||||
}}
|
13
codegen/dbl_list/snippets/list_get
Normal file
13
codegen/dbl_list/snippets/list_get
Normal file
@@ -0,0 +1,13 @@
|
||||
if (index >= list->node_count) {{
|
||||
return NULL;
|
||||
}}
|
||||
|
||||
{Ttitle}Node *output = NULL;
|
||||
{Ttitle}Node *current = list->first;
|
||||
for (u64 i = 1; i <= index; ++i) {{
|
||||
current = current->next;
|
||||
}}
|
||||
|
||||
output = current;
|
||||
|
||||
return output;
|
28
codegen/dbl_list/snippets/list_insert
Normal file
28
codegen/dbl_list/snippets/list_insert
Normal file
@@ -0,0 +1,28 @@
|
||||
if (!list || !node || !(node->item)) {{
|
||||
return;
|
||||
}}
|
||||
|
||||
if (index == 0) {{
|
||||
wapp_{Tlower}_list_push_front(list, node);
|
||||
return;
|
||||
}} else if (index == list->node_count) {{
|
||||
wapp_{Tlower}_list_push_back(list, node);
|
||||
return;
|
||||
}}
|
||||
|
||||
{Ttitle}Node *dst_node = wapp_{Tlower}_list_get(list, index);
|
||||
if (!dst_node) {{
|
||||
return;
|
||||
}}
|
||||
|
||||
{Ttitle}List node_list = {Tlower}_node_to_list(node);
|
||||
|
||||
list->node_count += node_list.node_count;
|
||||
|
||||
{Ttitle}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;
|
1
codegen/dbl_list/snippets/list_node
Normal file
1
codegen/dbl_list/snippets/list_node
Normal file
@@ -0,0 +1 @@
|
||||
(({Ttitle}Node){{.item = ITEM_PTR}})
|
20
codegen/dbl_list/snippets/list_pop_back
Normal file
20
codegen/dbl_list/snippets/list_pop_back
Normal file
@@ -0,0 +1,20 @@
|
||||
{Ttitle}Node *output = NULL;
|
||||
|
||||
if (!list || list->node_count == 0) {{
|
||||
goto RETURN_{Tupper}_LIST_POP_BACK;
|
||||
}}
|
||||
|
||||
output = list->last;
|
||||
|
||||
if (list->node_count == 1) {{
|
||||
*list = ({Ttitle}List){{0}};
|
||||
goto RETURN_{Tupper}_LIST_POP_BACK;
|
||||
}}
|
||||
|
||||
--(list->node_count);
|
||||
list->last = output->prev;
|
||||
|
||||
output->prev = output->next = NULL;
|
||||
|
||||
RETURN_{Tupper}_LIST_POP_BACK:
|
||||
return output;
|
20
codegen/dbl_list/snippets/list_pop_front
Normal file
20
codegen/dbl_list/snippets/list_pop_front
Normal file
@@ -0,0 +1,20 @@
|
||||
{Ttitle}Node *output = NULL;
|
||||
|
||||
if (!list || list->node_count == 0) {{
|
||||
goto RETURN_{Tupper}_LIST_POP_FRONT;
|
||||
}}
|
||||
|
||||
output = list->first;
|
||||
|
||||
if (list->node_count == 1) {{
|
||||
*list = ({Ttitle}List){{0}};
|
||||
goto RETURN_{Tupper}_LIST_POP_FRONT;
|
||||
}}
|
||||
|
||||
--(list->node_count);
|
||||
list->first = output->next;
|
||||
|
||||
output->prev = output->next = NULL;
|
||||
|
||||
RETURN_{Tupper}_LIST_POP_FRONT:
|
||||
return output;
|
20
codegen/dbl_list/snippets/list_push_back
Normal file
20
codegen/dbl_list/snippets/list_push_back
Normal file
@@ -0,0 +1,20 @@
|
||||
if (!list || !node || !(node->item)) {{
|
||||
return;
|
||||
}}
|
||||
|
||||
{Ttitle}List node_list = {Tlower}_node_to_list(node);
|
||||
|
||||
if (list->node_count == 0) {{
|
||||
*list = node_list;
|
||||
return;
|
||||
}}
|
||||
|
||||
list->node_count += node_list.node_count;
|
||||
|
||||
{Ttitle}Node *last = list->last;
|
||||
if (last) {{
|
||||
last->next = node_list.first;
|
||||
}}
|
||||
|
||||
list->last = node_list.last;
|
||||
node_list.first->prev = last;
|
20
codegen/dbl_list/snippets/list_push_front
Normal file
20
codegen/dbl_list/snippets/list_push_front
Normal file
@@ -0,0 +1,20 @@
|
||||
if (!list || !node || !(node->item)) {{
|
||||
return;
|
||||
}}
|
||||
|
||||
{Ttitle}List node_list = {Tlower}_node_to_list(node);
|
||||
|
||||
if (list->node_count == 0) {{
|
||||
*list = node_list;
|
||||
return;
|
||||
}}
|
||||
|
||||
list->node_count += node_list.node_count;
|
||||
|
||||
{Ttitle}Node *first = list->first;
|
||||
if (first) {{
|
||||
first->prev = node_list.last;
|
||||
}}
|
||||
|
||||
list->first = node_list.first;
|
||||
node_list.last->next = first;
|
27
codegen/dbl_list/snippets/list_remove
Normal file
27
codegen/dbl_list/snippets/list_remove
Normal file
@@ -0,0 +1,27 @@
|
||||
{Ttitle}Node *output = NULL;
|
||||
if (!list) {{
|
||||
goto RETURN_{Tupper}_LIST_REMOVE;
|
||||
}}
|
||||
|
||||
if (index == 0) {{
|
||||
output = wapp_{Tlower}_list_pop_front(list);
|
||||
goto RETURN_{Tupper}_LIST_REMOVE;
|
||||
}} else if (index == list->node_count) {{
|
||||
output = wapp_{Tlower}_list_pop_back(list);
|
||||
goto RETURN_{Tupper}_LIST_REMOVE;
|
||||
}}
|
||||
|
||||
output = wapp_{Tlower}_list_get(list, index);
|
||||
if (!output) {{
|
||||
goto RETURN_{Tupper}_LIST_REMOVE;
|
||||
}}
|
||||
|
||||
output->prev->next = output->next;
|
||||
output->next->prev = output->prev;
|
||||
|
||||
--(list->node_count);
|
||||
|
||||
output->prev = output->next = NULL;
|
||||
|
||||
RETURN_{Tupper}_LIST_REMOVE:
|
||||
return output;
|
13
codegen/dbl_list/snippets/node_to_list
Normal file
13
codegen/dbl_list/snippets/node_to_list
Normal file
@@ -0,0 +1,13 @@
|
||||
{Ttitle}List output = {{.first = node, .last = node, .node_count = 1}};
|
||||
|
||||
while (output.first->prev != NULL) {{
|
||||
output.first = output.first->prev;
|
||||
++(output.node_count);
|
||||
}}
|
||||
|
||||
while (output.last->next != NULL) {{
|
||||
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().rstrip()
|
Reference in New Issue
Block a user