Upgrade codegen

This commit is contained in:
Abdelrahman Said
2025-04-17 09:03:30 +01:00
parent aa04fab6ea
commit 3c32b247c0
15 changed files with 66 additions and 46 deletions

View File

@@ -27,29 +27,28 @@ class DblListData:
src_decl_types: list[CStruct] = field(default_factory=list)
def gen_dbl_list(user_datatypes: dict[CDataType, DblListData] = {}):
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(),
)
common_includes: list[CInclude] = [
CInclude(header="../../../common/aliases/aliases.h", local=True),
common_local_include_files = [
(WAPP_SRC_ROOT / "common" / "aliases" / "aliases.h")
]
common_includes: list[CInclude] = []
common_decl_types: list[CStruct] = []
datatypes: dict[CDataType, DblListData] = {
"Str8": DblListData(
src_includes=[
CInclude(header="./str8.h", local=True),
],
hdr_decl_types=[
CStruct(name="str8", cargs=[], typedef_name="Str8"),
],
out_dir=WAPP_SRC_ROOT / "core/strings/str8/",
out_dir=WAPP_SRC_ROOT / "core" / "strings" / "str8",
),
}
datatypes.update(user_datatypes)
@@ -60,16 +59,18 @@ def gen_dbl_list(user_datatypes: dict[CDataType, DblListData] = {}):
type_string = get_datatype_string(_type)
node = CStruct(
name=f"{type_string}Node",
name=f"{type_string.title()}Node",
cargs=[
CArg(name="item", _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)),
],
)
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}List",
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)),
@@ -77,6 +78,15 @@ def gen_dbl_list(user_datatypes: dict[CDataType, DblListData] = {}):
],
)
node_func = CFunc(
name=f"wapp_{type_string.lower()}_list_node",
ret_type=node,
args=[
CArg(name="item", _type=type_string, pointer=CPointer(CPointerType.SINGLE)),
],
body=__format_func_body(snippets_dir / "list_node", type_string),
)
get_func = CFunc(
name=f"wapp_{type_string.lower()}_list_get",
ret_type=node,
@@ -175,6 +185,7 @@ def gen_dbl_list(user_datatypes: dict[CDataType, DblListData] = {}):
includes=[],
types=[node, dl_list],
funcs=[
node_func,
get_func,
push_front_func,
push_back_func,
@@ -198,6 +209,11 @@ def gen_dbl_list(user_datatypes: dict[CDataType, DblListData] = {}):
header.includes.extend(common_includes)
source.includes.extend(common_includes)
for include_file in common_local_include_files:
include = CInclude(header=str(include_file.relative_to(dbl_list_data.out_dir, walk_up=True)), local=True)
header.includes.append(include)
source.includes.append(include)
if len(dbl_list_data.hdr_includes) > 0:
header.includes.extend(dbl_list_data.hdr_includes)

View File

@@ -2,8 +2,8 @@
return NULL;
}}
{T}Node *output = NULL;
{T}Node *current = list->first;
{Ttitle}Node *output = NULL;
{Ttitle}Node *current = list->first;
for (u64 i = 1; i <= index; ++i) {{
current = current->next;
}}

View File

@@ -10,16 +10,16 @@
return;
}}
{T}Node *dst_node = wapp_{Tlower}_list_get(list, index);
{Ttitle}Node *dst_node = wapp_{Tlower}_list_get(list, index);
if (!dst_node) {{
return;
}}
{T}List node_list = {Tlower}_node_to_list(node);
{Ttitle}List node_list = {Tlower}_node_to_list(node);
list->node_count += node_list.node_count;
{T}Node *prev = dst_node->prev;
{Ttitle}Node *prev = dst_node->prev;
dst_node->prev = node_list.last;
prev->next = node_list.first;

View File

@@ -0,0 +1 @@
return ({Ttitle}Node){{.item = item}};

View File

@@ -1,4 +1,4 @@
{T}Node *output = NULL;
{Ttitle}Node *output = NULL;
if (!list || list->node_count == 0) {{
goto RETURN_{Tupper}_LIST_POP_BACK;
@@ -7,7 +7,7 @@
output = list->last;
if (list->node_count == 1) {{
*list = ({T}List){{0}};
*list = ({Ttitle}List){{0}};
goto RETURN_{Tupper}_LIST_POP_BACK;
}}

View File

@@ -1,4 +1,4 @@
{T}Node *output = NULL;
{Ttitle}Node *output = NULL;
if (!list || list->node_count == 0) {{
goto RETURN_{Tupper}_LIST_POP_FRONT;
@@ -7,7 +7,7 @@
output = list->first;
if (list->node_count == 1) {{
*list = ({T}List){{0}};
*list = ({Ttitle}List){{0}};
goto RETURN_{Tupper}_LIST_POP_FRONT;
}}

View File

@@ -2,7 +2,7 @@
return;
}}
{T}List node_list = {Tlower}_node_to_list(node);
{Ttitle}List node_list = {Tlower}_node_to_list(node);
if (list->node_count == 0) {{
*list = node_list;
@@ -11,7 +11,7 @@
list->node_count += node_list.node_count;
{T}Node *last = list->last;
{Ttitle}Node *last = list->last;
if (last) {{
last->next = node_list.first;
}}

View File

@@ -2,7 +2,7 @@
return;
}}
{T}List node_list = {Tlower}_node_to_list(node);
{Ttitle}List node_list = {Tlower}_node_to_list(node);
if (list->node_count == 0) {{
*list = node_list;
@@ -11,7 +11,7 @@
list->node_count += node_list.node_count;
{T}Node *first = list->first;
{Ttitle}Node *first = list->first;
if (first) {{
first->prev = node_list.last;
}}

View File

@@ -1,4 +1,4 @@
{T}Node *output = NULL;
{Ttitle}Node *output = NULL;
if (!list) {{
goto RETURN_{Tupper}_LIST_REMOVE;
}}

View File

@@ -1,4 +1,4 @@
{T}List output = {{.first = node, .last = node, .node_count = 1}};
{Ttitle}List output = {{.first = node, .last = node, .node_count = 1}};
while (output.first->prev != NULL) {{
output.first = output.first->prev;