diff --git a/codegen/dbl_list/gen_dbl_list.py b/codegen/dbl_list/gen_dbl_list.py index be35489..1b98353 100644 --- a/codegen/dbl_list/gen_dbl_list.py +++ b/codegen/dbl_list/gen_dbl_list.py @@ -21,15 +21,13 @@ from codegen.datatypes import ( @dataclass class DblListData: out_dir: Path - common_includes: list[CInclude] = field(default_factory=list) hdr_includes: list[CInclude] = field(default_factory=list) src_includes: list[CInclude] = field(default_factory=list) - common_decl_types: list[CStruct] = field(default_factory=list) hdr_decl_types: list[CStruct] = field(default_factory=list) src_decl_types: list[CStruct] = field(default_factory=list) -def gen_dbl_list(): +def gen_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, @@ -37,11 +35,14 @@ def gen_dbl_list(): Tlower=type_string.lower(), ) + common_includes: list[CInclude] = [ + CInclude(header="../../../common/aliases/aliases.h", local=True), + ] + + common_decl_types: list[CStruct] = [] + datatypes: dict[CDataType, DblListData] = { "Str8": DblListData( - common_includes=[ - CInclude(header="../../../common/aliases/aliases.h", local=True), - ], src_includes=[ CInclude(header="./str8.h", local=True), ], @@ -51,6 +52,8 @@ def gen_dbl_list(): out_dir=WAPP_SRC_ROOT / "core/strings/str8/", ), } + datatypes.update(user_datatypes) + snippets_dir = Path(__file__).parent / "snippets" for _type, dbl_list_data in datatypes.items(): @@ -59,7 +62,7 @@ def gen_dbl_list(): node = CStruct( name=f"{type_string}Node", cargs=[ - CArg(name="string", _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)), ], @@ -70,7 +73,6 @@ def gen_dbl_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), ], ) @@ -169,7 +171,7 @@ def gen_dbl_list(): header = CHeader( name=f"{type_string.lower()}_list", - decl_types=dbl_list_data.common_decl_types + dbl_list_data.hdr_decl_types, + decl_types=common_decl_types + dbl_list_data.hdr_decl_types, includes=[], types=[node, dl_list], funcs=[ @@ -186,15 +188,15 @@ def gen_dbl_list(): source = CSource( name=header.name, - decl_types=dbl_list_data.common_decl_types + dbl_list_data.src_decl_types, + decl_types=common_decl_types + dbl_list_data.src_decl_types, includes=[CInclude(header, local=True, same_dir=True), CInclude(header="stddef.h")], internal_funcs=[node_to_list_func], funcs=header.funcs ) - if len(dbl_list_data.common_includes) > 0: - header.includes.extend(dbl_list_data.common_includes) - source.includes.extend(dbl_list_data.common_includes) + if len(common_includes) > 0: + header.includes.extend(common_includes) + source.includes.extend(common_includes) if len(dbl_list_data.hdr_includes) > 0: header.includes.extend(dbl_list_data.hdr_includes) diff --git a/codegen/dbl_list/snippets/list_empty b/codegen/dbl_list/snippets/list_empty index c14804f..93380be 100644 --- a/codegen/dbl_list/snippets/list_empty +++ b/codegen/dbl_list/snippets/list_empty @@ -4,5 +4,5 @@ u64 count = list->node_count; for (u64 i = 0; i < count; ++i) {{ - wapp_str8_list_pop_back(list); + wapp_{T}_list_pop_back(list); }} diff --git a/codegen/dbl_list/snippets/list_insert b/codegen/dbl_list/snippets/list_insert index 0b6aa52..88f45b6 100644 --- a/codegen/dbl_list/snippets/list_insert +++ b/codegen/dbl_list/snippets/list_insert @@ -1,23 +1,22 @@ - if (!list || !node || !(node->string)) {{ + if (!list || !node || !(node->item)) {{ return; }} if (index == 0) {{ - wapp_str8_list_push_front(list, node); + wapp_{T}_list_push_front(list, node); return; }} else if (index == list->node_count) {{ - wapp_str8_list_push_back(list, node); + wapp_{T}_list_push_back(list, node); return; }} - {T}Node *dst_node = wapp_str8_list_get(list, index); + {T}Node *dst_node = wapp_{T}_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; diff --git a/codegen/dbl_list/snippets/list_pop_back b/codegen/dbl_list/snippets/list_pop_back index 3ee8a15..67e370a 100644 --- a/codegen/dbl_list/snippets/list_pop_back +++ b/codegen/dbl_list/snippets/list_pop_back @@ -12,7 +12,6 @@ }} --(list->node_count); - list->total_size -= output->string->size; list->last = output->prev; output->prev = output->next = NULL; diff --git a/codegen/dbl_list/snippets/list_pop_front b/codegen/dbl_list/snippets/list_pop_front index caba8c9..97de159 100644 --- a/codegen/dbl_list/snippets/list_pop_front +++ b/codegen/dbl_list/snippets/list_pop_front @@ -12,7 +12,6 @@ }} --(list->node_count); - list->total_size -= output->string->size; list->first = output->next; output->prev = output->next = NULL; diff --git a/codegen/dbl_list/snippets/list_push_back b/codegen/dbl_list/snippets/list_push_back index cc51dac..4d7b004 100644 --- a/codegen/dbl_list/snippets/list_push_back +++ b/codegen/dbl_list/snippets/list_push_back @@ -1,4 +1,4 @@ - if (!list || !node || !(node->string)) {{ + if (!list || !node || !(node->item)) {{ return; }} @@ -9,7 +9,6 @@ return; }} - list->total_size += node_list.total_size; list->node_count += node_list.node_count; {T}Node *last = list->last; diff --git a/codegen/dbl_list/snippets/list_push_front b/codegen/dbl_list/snippets/list_push_front index 8989beb..784f669 100644 --- a/codegen/dbl_list/snippets/list_push_front +++ b/codegen/dbl_list/snippets/list_push_front @@ -1,4 +1,4 @@ - if (!list || !node || !(node->string)) {{ + if (!list || !node || !(node->item)) {{ return; }} @@ -9,7 +9,6 @@ return; }} - list->total_size += node_list.total_size; list->node_count += node_list.node_count; {T}Node *first = list->first; diff --git a/codegen/dbl_list/snippets/list_remove b/codegen/dbl_list/snippets/list_remove index cc213c4..31d0a39 100644 --- a/codegen/dbl_list/snippets/list_remove +++ b/codegen/dbl_list/snippets/list_remove @@ -4,14 +4,14 @@ }} if (index == 0) {{ - output = wapp_str8_list_pop_front(list); + output = wapp_{T}_list_pop_front(list); goto RETURN_{Tupper}_LIST_REMOVE; }} else if (index == list->node_count) {{ - output = wapp_str8_list_pop_back(list); + output = wapp_{T}_list_pop_back(list); goto RETURN_{Tupper}_LIST_REMOVE; }} - output = wapp_str8_list_get(list, index); + output = wapp_{T}_list_get(list, index); if (!output) {{ goto RETURN_{Tupper}_LIST_REMOVE; }} @@ -20,7 +20,6 @@ output->next->prev = output->prev; --(list->node_count); - list->total_size -= output->string->size; output->prev = output->next = NULL; diff --git a/codegen/dbl_list/snippets/node_to_list b/codegen/dbl_list/snippets/node_to_list index 2c52609..6f88b81 100644 --- a/codegen/dbl_list/snippets/node_to_list +++ b/codegen/dbl_list/snippets/node_to_list @@ -1,13 +1,11 @@ - {T}List output = {{.first = node, .last = node, .total_size = node->string->size, .node_count = 1}}; + {T}List output = {{.first = node, .last = node, .total_size = node->item->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); }}