From 7b16c7511fbdf385cc64e98cb449d6c1f10d7f23 Mon Sep 17 00:00:00 2001 From: Abdelrahman Said Date: Sat, 1 Mar 2025 22:43:43 +0000 Subject: [PATCH] Update list example --- codegen/codegen.py | 64 ++++++++++++---------------------------------- 1 file changed, 16 insertions(+), 48 deletions(-) diff --git a/codegen/codegen.py b/codegen/codegen.py index 87a29fd..3f7cf6d 100644 --- a/codegen/codegen.py +++ b/codegen/codegen.py @@ -154,6 +154,13 @@ def test_typed_array(): def test_doubly_linked_list(): + def __format_func_body(filename: Path, type_string: str): + return load_func_body_from_file(filename).format( + T=type_string, + Tupper=type_string.upper(), + Tlower=type_string.lower(), + ) + datatypes: dict[CDataType, list[CInclude]] = {"Str8": [CInclude(header="str8.h", local=True)]} snippets_dir = Path("snippets") @@ -186,11 +193,7 @@ def test_doubly_linked_list(): CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE), qualifier=CQualifier.CONST), CArg(name="index", _type=CType.U64), ], - body=load_func_body_from_file(Path(__file__).parent / snippets_dir / "list_get").format( - T=type_string, - Tupper=type_string.upper(), - Tlower=type_string.lower() - ), + body=__format_func_body(Path(__file__).parent / snippets_dir / "list_get", type_string), pointer=CPointer(CPointerType.SINGLE), ) @@ -201,11 +204,7 @@ def test_doubly_linked_list(): CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)), CArg(name="node", _type=node, pointer=CPointer(CPointerType.SINGLE)), ], - body=load_func_body_from_file(Path(__file__).parent / snippets_dir / "list_push_front").format( - T=type_string, - Tupper=type_string.upper(), - Tlower=type_string.lower() - ), + body=__format_func_body(Path(__file__).parent / snippets_dir / "list_push_front", type_string), ) push_back_func = CFunc( @@ -215,11 +214,7 @@ def test_doubly_linked_list(): CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)), CArg(name="node", _type=node, pointer=CPointer(CPointerType.SINGLE)), ], - body=load_func_body_from_file(Path(__file__).parent / snippets_dir / "list_push_back").format( - T=type_string, - Tupper=type_string.upper(), - Tlower=type_string.lower() - ), + body=__format_func_body(Path(__file__).parent / snippets_dir / "list_push_back", type_string), ) insert_func = CFunc( @@ -230,11 +225,7 @@ def test_doubly_linked_list(): CArg(name="node", _type=node, pointer=CPointer(CPointerType.SINGLE)), CArg(name="index", _type=CType.U64), ], - body=load_func_body_from_file(Path(__file__).parent / snippets_dir / "list_insert").format( - T=type_string, - Tupper=type_string.upper(), - Tlower=type_string.lower() - ), + body=__format_func_body(Path(__file__).parent / snippets_dir / "list_insert", type_string), ) pop_front_func = CFunc( @@ -243,11 +234,7 @@ def test_doubly_linked_list(): args=[ CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)), ], - body=load_func_body_from_file(Path(__file__).parent / snippets_dir / "list_pop_front").format( - T=type_string, - Tupper=type_string.upper(), - Tlower=type_string.lower() - ), + body=__format_func_body(Path(__file__).parent / snippets_dir / "list_pop_front", type_string), pointer=CPointer(CPointerType.SINGLE), ) @@ -257,11 +244,7 @@ def test_doubly_linked_list(): args=[ CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)), ], - body=load_func_body_from_file(Path(__file__).parent / snippets_dir / "list_pop_back").format( - T=type_string, - Tupper=type_string.upper(), - Tlower=type_string.lower() - ), + body=__format_func_body(Path(__file__).parent / snippets_dir / "list_pop_back", type_string), pointer=CPointer(CPointerType.SINGLE), ) @@ -272,11 +255,7 @@ def test_doubly_linked_list(): CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)), CArg(name="index", _type=CType.U64), ], - body=load_func_body_from_file(Path(__file__).parent / snippets_dir / "list_remove").format( - T=type_string, - Tupper=type_string.upper(), - Tlower=type_string.lower() - ), + body=__format_func_body(Path(__file__).parent / snippets_dir / "list_remove", type_string), pointer=CPointer(CPointerType.SINGLE), ) @@ -286,11 +265,7 @@ def test_doubly_linked_list(): args=[ CArg(name="list", _type=dl_list, pointer=CPointer(CPointerType.SINGLE)), ], - body=load_func_body_from_file(Path(__file__).parent / snippets_dir / "list_empty").format( - T=type_string, - Tupper=type_string.upper(), - Tlower=type_string.lower() - ), + body=__format_func_body(Path(__file__).parent / snippets_dir / "list_empty", type_string), ) node_to_list_func = CFunc( @@ -299,11 +274,7 @@ def test_doubly_linked_list(): args=[ CArg(name="node", _type=node, pointer=CPointer(CPointerType.SINGLE)), ], - body=load_func_body_from_file(Path(__file__).parent / snippets_dir / "node_to_list").format( - T=type_string, - Tupper=type_string.upper(), - Tlower=type_string.lower(), - ), + body=__format_func_body(Path(__file__).parent / snippets_dir / "node_to_list", type_string), qualifiers=[CQualifier.INTERNAL], ) @@ -334,9 +305,6 @@ def test_doubly_linked_list(): header.includes.extend(includes) source.includes.extend(includes) - header.includes = sorted(header.includes, key=lambda inc: inc.local, reverse=True) - source.includes = sorted(source.includes, key=lambda inc: inc.local, reverse=True) - header.save(Path(".")) source.save(Path("."))