diff --git a/codegen/__main__.py b/codegen/__main__.py index ee75b61..b03bdc9 100644 --- a/codegen/__main__.py +++ b/codegen/__main__.py @@ -1,16 +1,15 @@ -from pathlib import Path 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(): - datatypes: dict[CDataType, DblListData] = { - "int": DblListData( - out_dir=Path("/Users/abdelrahman/dev/personal/wizapp-stdlib"), - ), - } + gen_dbl_list() - gen_dbl_list(datatypes) + +def gen_dbl_list(): + datatypes: dict[CDataType, DblListData] = {} + + make_dbl_list(datatypes) if __name__ == "__main__": diff --git a/codegen/constants.py b/codegen/constants.py index d092b2c..ebad100 100644 --- a/codegen/constants.py +++ b/codegen/constants.py @@ -1,5 +1,5 @@ from pathlib import Path -PACKAGE_DIR = Path(__file__).parent +PACKAGE_DIR = Path(__file__).parent.resolve() WAPP_SRC_ROOT = PACKAGE_DIR.parent / "src" diff --git a/codegen/dbl_list/gen_dbl_list.py b/codegen/dbl_list/make_dbl_list.py similarity index 84% rename from codegen/dbl_list/gen_dbl_list.py rename to codegen/dbl_list/make_dbl_list.py index 1b98353..f979e2c 100644 --- a/codegen/dbl_list/gen_dbl_list.py +++ b/codegen/dbl_list/make_dbl_list.py @@ -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) diff --git a/codegen/dbl_list/snippets/list_get b/codegen/dbl_list/snippets/list_get index c65985e..0fde3ac 100644 --- a/codegen/dbl_list/snippets/list_get +++ b/codegen/dbl_list/snippets/list_get @@ -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; }} diff --git a/codegen/dbl_list/snippets/list_insert b/codegen/dbl_list/snippets/list_insert index e8eb294..5ae0d84 100644 --- a/codegen/dbl_list/snippets/list_insert +++ b/codegen/dbl_list/snippets/list_insert @@ -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; diff --git a/codegen/dbl_list/snippets/list_node b/codegen/dbl_list/snippets/list_node new file mode 100644 index 0000000..8a9e407 --- /dev/null +++ b/codegen/dbl_list/snippets/list_node @@ -0,0 +1 @@ + return ({Ttitle}Node){{.item = item}}; diff --git a/codegen/dbl_list/snippets/list_pop_back b/codegen/dbl_list/snippets/list_pop_back index 67e370a..849136d 100644 --- a/codegen/dbl_list/snippets/list_pop_back +++ b/codegen/dbl_list/snippets/list_pop_back @@ -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; }} diff --git a/codegen/dbl_list/snippets/list_pop_front b/codegen/dbl_list/snippets/list_pop_front index 97de159..8770e3f 100644 --- a/codegen/dbl_list/snippets/list_pop_front +++ b/codegen/dbl_list/snippets/list_pop_front @@ -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; }} diff --git a/codegen/dbl_list/snippets/list_push_back b/codegen/dbl_list/snippets/list_push_back index 4d7b004..35abf52 100644 --- a/codegen/dbl_list/snippets/list_push_back +++ b/codegen/dbl_list/snippets/list_push_back @@ -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; }} diff --git a/codegen/dbl_list/snippets/list_push_front b/codegen/dbl_list/snippets/list_push_front index 784f669..f021cfc 100644 --- a/codegen/dbl_list/snippets/list_push_front +++ b/codegen/dbl_list/snippets/list_push_front @@ -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; }} diff --git a/codegen/dbl_list/snippets/list_remove b/codegen/dbl_list/snippets/list_remove index 90095b5..611729a 100644 --- a/codegen/dbl_list/snippets/list_remove +++ b/codegen/dbl_list/snippets/list_remove @@ -1,4 +1,4 @@ - {T}Node *output = NULL; + {Ttitle}Node *output = NULL; if (!list) {{ goto RETURN_{Tupper}_LIST_REMOVE; }} diff --git a/codegen/dbl_list/snippets/node_to_list b/codegen/dbl_list/snippets/node_to_list index b11a634..b47d7e5 100644 --- a/codegen/dbl_list/snippets/node_to_list +++ b/codegen/dbl_list/snippets/node_to_list @@ -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; diff --git a/codegen/utils.py b/codegen/utils.py index 47b6b16..8a8c814 100644 --- a/codegen/utils.py +++ b/codegen/utils.py @@ -3,4 +3,4 @@ from pathlib import Path def load_func_body_from_file(filename: Path) -> str: with open(filename, "r") as infile: - return infile.read().strip() + return infile.read().rstrip() diff --git a/src/core/strings/str8/str8_list.c b/src/core/strings/str8/str8_list.c index a648311..0950acd 100644 --- a/src/core/strings/str8/str8_list.c +++ b/src/core/strings/str8/str8_list.c @@ -4,13 +4,16 @@ #include "./str8_list.h" #include "../../../common/aliases/aliases.h" -#include "./str8.h" #include 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) { -if (index >= list->node_count) { + if (index >= list->node_count) { return NULL; } @@ -26,7 +29,7 @@ if (index >= list->node_count) { } void wapp_str8_list_push_front(Str8List *list, Str8Node *node) { -if (!list || !node || !(node->item)) { + if (!list || !node || !(node->item)) { return; } @@ -49,7 +52,7 @@ if (!list || !node || !(node->item)) { } void wapp_str8_list_push_back(Str8List *list, Str8Node *node) { -if (!list || !node || !(node->item)) { + if (!list || !node || !(node->item)) { return; } @@ -72,7 +75,7 @@ if (!list || !node || !(node->item)) { } void wapp_str8_list_insert(Str8List *list, Str8Node *node, u64 index) { -if (!list || !node || !(node->item)) { + if (!list || !node || !(node->item)) { return; } @@ -103,7 +106,7 @@ if (!list || !node || !(node->item)) { } Str8Node *wapp_str8_list_pop_front(Str8List *list) { -Str8Node *output = NULL; + Str8Node *output = NULL; if (!list || list->node_count == 0) { goto RETURN_STR8_LIST_POP_FRONT; @@ -126,7 +129,7 @@ RETURN_STR8_LIST_POP_FRONT: } Str8Node *wapp_str8_list_pop_back(Str8List *list) { -Str8Node *output = NULL; + Str8Node *output = NULL; if (!list || list->node_count == 0) { goto RETURN_STR8_LIST_POP_BACK; @@ -149,7 +152,7 @@ RETURN_STR8_LIST_POP_BACK: } Str8Node *wapp_str8_list_remove(Str8List *list, u64 index) { -Str8Node *output = NULL; + Str8Node *output = NULL; if (!list) { goto RETURN_STR8_LIST_REMOVE; } @@ -179,7 +182,7 @@ RETURN_STR8_LIST_REMOVE: } void wapp_str8_list_empty(Str8List *list) { -if (!list) { + if (!list) { return; } @@ -190,7 +193,7 @@ if (!list) { } internal Str8List str8_node_to_list(Str8Node *node) { -Str8List output = {.first = node, .last = node, .node_count = 1}; + Str8List output = {.first = node, .last = node, .node_count = 1}; while (output.first->prev != NULL) { output.first = output.first->prev; diff --git a/src/core/strings/str8/str8_list.h b/src/core/strings/str8/str8_list.h index 529c5c0..7c816b2 100644 --- a/src/core/strings/str8/str8_list.h +++ b/src/core/strings/str8/str8_list.h @@ -27,6 +27,7 @@ struct Str8List { u64 node_count; }; +Str8Node wapp_str8_list_node(Str8 *item); 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_back(Str8List *list, Str8Node *node);