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

@ -1,16 +1,15 @@
from pathlib import Path
from codegen.datatypes import CDataType from codegen.datatypes import CDataType
from codegen.dbl_list.gen_dbl_list import DblListData, gen_dbl_list from codegen.dbl_list.make_dbl_list import DblListData, make_dbl_list
def main(): def main():
datatypes: dict[CDataType, DblListData] = { gen_dbl_list()
"int": DblListData(
out_dir=Path("/Users/abdelrahman/dev/personal/wizapp-stdlib"),
),
}
gen_dbl_list(datatypes)
def gen_dbl_list():
datatypes: dict[CDataType, DblListData] = {}
make_dbl_list(datatypes)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -1,5 +1,5 @@
from pathlib import Path from pathlib import Path
PACKAGE_DIR = Path(__file__).parent PACKAGE_DIR = Path(__file__).parent.resolve()
WAPP_SRC_ROOT = PACKAGE_DIR.parent / "src" WAPP_SRC_ROOT = PACKAGE_DIR.parent / "src"

View File

@ -27,29 +27,28 @@ class DblListData:
src_decl_types: list[CStruct] = field(default_factory=list) 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): def __format_func_body(filename: Path, type_string: str):
return load_func_body_from_file(filename).format( return load_func_body_from_file(filename).format(
T=type_string, T=type_string,
Ttitle=type_string.title(),
Tupper=type_string.upper(), Tupper=type_string.upper(),
Tlower=type_string.lower(), Tlower=type_string.lower(),
) )
common_includes: list[CInclude] = [ common_local_include_files = [
CInclude(header="../../../common/aliases/aliases.h", local=True), (WAPP_SRC_ROOT / "common" / "aliases" / "aliases.h")
] ]
common_includes: list[CInclude] = []
common_decl_types: list[CStruct] = [] common_decl_types: list[CStruct] = []
datatypes: dict[CDataType, DblListData] = { datatypes: dict[CDataType, DblListData] = {
"Str8": DblListData( "Str8": DblListData(
src_includes=[
CInclude(header="./str8.h", local=True),
],
hdr_decl_types=[ hdr_decl_types=[
CStruct(name="str8", cargs=[], typedef_name="Str8"), 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) datatypes.update(user_datatypes)
@ -60,16 +59,18 @@ def gen_dbl_list(user_datatypes: dict[CDataType, DblListData] = {}):
type_string = get_datatype_string(_type) type_string = get_datatype_string(_type)
node = CStruct( node = CStruct(
name=f"{type_string}Node", name=f"{type_string.title()}Node",
cargs=[ cargs=[
CArg(name="item", _type=type_string, pointer=CPointer(_type=CPointerType.SINGLE)), 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( dl_list = CStruct(
name=f"{type_string}List", name=f"{type_string.title()}List",
cargs=[ cargs=[
CArg(name="first", _type=node, pointer=CPointer(_type=CPointerType.SINGLE)), CArg(name="first", _type=node, pointer=CPointer(_type=CPointerType.SINGLE)),
CArg(name="last", _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( get_func = CFunc(
name=f"wapp_{type_string.lower()}_list_get", name=f"wapp_{type_string.lower()}_list_get",
ret_type=node, ret_type=node,
@ -175,6 +185,7 @@ def gen_dbl_list(user_datatypes: dict[CDataType, DblListData] = {}):
includes=[], includes=[],
types=[node, dl_list], types=[node, dl_list],
funcs=[ funcs=[
node_func,
get_func, get_func,
push_front_func, push_front_func,
push_back_func, push_back_func,
@ -198,6 +209,11 @@ def gen_dbl_list(user_datatypes: dict[CDataType, DblListData] = {}):
header.includes.extend(common_includes) header.includes.extend(common_includes)
source.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: if len(dbl_list_data.hdr_includes) > 0:
header.includes.extend(dbl_list_data.hdr_includes) header.includes.extend(dbl_list_data.hdr_includes)

View File

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

View File

@ -10,16 +10,16 @@
return; return;
}} }}
{T}Node *dst_node = wapp_{Tlower}_list_get(list, index); {Ttitle}Node *dst_node = wapp_{Tlower}_list_get(list, index);
if (!dst_node) {{ if (!dst_node) {{
return; 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; 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; dst_node->prev = node_list.last;
prev->next = node_list.first; 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) {{ if (!list || list->node_count == 0) {{
goto RETURN_{Tupper}_LIST_POP_BACK; goto RETURN_{Tupper}_LIST_POP_BACK;
@ -7,7 +7,7 @@
output = list->last; output = list->last;
if (list->node_count == 1) {{ if (list->node_count == 1) {{
*list = ({T}List){{0}}; *list = ({Ttitle}List){{0}};
goto RETURN_{Tupper}_LIST_POP_BACK; 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) {{ if (!list || list->node_count == 0) {{
goto RETURN_{Tupper}_LIST_POP_FRONT; goto RETURN_{Tupper}_LIST_POP_FRONT;
@ -7,7 +7,7 @@
output = list->first; output = list->first;
if (list->node_count == 1) {{ if (list->node_count == 1) {{
*list = ({T}List){{0}}; *list = ({Ttitle}List){{0}};
goto RETURN_{Tupper}_LIST_POP_FRONT; goto RETURN_{Tupper}_LIST_POP_FRONT;
}} }}

View File

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

View File

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

View File

@ -1,4 +1,4 @@
{T}Node *output = NULL; {Ttitle}Node *output = NULL;
if (!list) {{ if (!list) {{
goto RETURN_{Tupper}_LIST_REMOVE; 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) {{ while (output.first->prev != NULL) {{
output.first = output.first->prev; output.first = output.first->prev;

View File

@ -3,4 +3,4 @@ from pathlib import Path
def load_func_body_from_file(filename: Path) -> str: def load_func_body_from_file(filename: Path) -> str:
with open(filename, "r") as infile: with open(filename, "r") as infile:
return infile.read().strip() return infile.read().rstrip()

View File

@ -4,11 +4,14 @@
#include "./str8_list.h" #include "./str8_list.h"
#include "../../../common/aliases/aliases.h" #include "../../../common/aliases/aliases.h"
#include "./str8.h"
#include <stddef.h> #include <stddef.h>
internal Str8List str8_node_to_list(Str8Node *node); internal Str8List str8_node_to_list(Str8Node *node);
Str8Node wapp_str8_list_node(Str8 *item) {
return (Str8Node){.item = item};
}
Str8Node *wapp_str8_list_get(const Str8List *list, u64 index) { Str8Node *wapp_str8_list_get(const Str8List *list, u64 index) {
if (index >= list->node_count) { if (index >= list->node_count) {
return NULL; return NULL;

View File

@ -27,6 +27,7 @@ struct Str8List {
u64 node_count; u64 node_count;
}; };
Str8Node wapp_str8_list_node(Str8 *item);
Str8Node *wapp_str8_list_get(const Str8List *list, u64 index); Str8Node *wapp_str8_list_get(const Str8List *list, u64 index);
void wapp_str8_list_push_front(Str8List *list, Str8Node *node); void wapp_str8_list_push_front(Str8List *list, Str8Node *node);
void wapp_str8_list_push_back(Str8List *list, Str8Node *node); void wapp_str8_list_push_back(Str8List *list, Str8Node *node);