From 63ed2633eae9215fe542d51759e849ab2e5c8855 Mon Sep 17 00:00:00 2001
From: Abdelrahman <said.abdelrahman89@gmail.com>
Date: Sat, 19 Apr 2025 13:33:09 +0100
Subject: [PATCH] Update code generation to create one file for dbl_list

---
 codegen/__main__.py                           |   1 -
 codegen/datatypes.py                          |  24 ++++
 codegen/dbl_list/make_dbl_list.py             | 104 +++++++++---------
 codegen/dbl_list/snippets/list_node           |   2 +-
 .../dbl_list/dbl_list.c}                      |  10 +-
 .../dbl_list/dbl_list.h}                      |  11 +-
 src/containers/wapp_containers.c              |   6 +
 src/containers/wapp_containers.h              |   7 ++
 src/core/strings/str8/str8.h                  |   6 +-
 src/core/wapp_core.c                          |   2 +-
 src/core/wapp_core.h                          |   2 +-
 11 files changed, 105 insertions(+), 70 deletions(-)
 rename src/{core/strings/str8/str8_list.c => containers/dbl_list/dbl_list.c} (96%)
 rename src/{core/strings/str8/str8_list.h => containers/dbl_list/dbl_list.h} (84%)
 create mode 100644 src/containers/wapp_containers.c
 create mode 100644 src/containers/wapp_containers.h

diff --git a/codegen/__main__.py b/codegen/__main__.py
index b03bdc9..4d833c3 100644
--- a/codegen/__main__.py
+++ b/codegen/__main__.py
@@ -8,7 +8,6 @@ def main():
 
 def gen_dbl_list():
     datatypes: dict[CDataType, DblListData] = {}
-
     make_dbl_list(datatypes)
 
 
diff --git a/codegen/datatypes.py b/codegen/datatypes.py
index fab1b9d..e28f258 100644
--- a/codegen/datatypes.py
+++ b/codegen/datatypes.py
@@ -88,6 +88,15 @@ class CEnum:
         return header + values + footer
 
 
+@dataclass
+class CMacro:
+    name: str
+    value: str
+
+    def __str__(self) -> str:
+        return f"#define {self.name} {self.value}\n"
+
+
 @dataclass
 class CStruct:
     name: str
@@ -195,6 +204,7 @@ class CFile:
     name: str
     extension: str
     decl_types: list[CStruct] = field(default_factory=list)
+    macros: list[CMacro] = field(default_factory=list)
 
     def save(self, output_dir: Path):
         output_file = output_dir / f"{self.name}.{self.extension}"
@@ -228,6 +238,12 @@ class CHeader(CFile):
 
         includes = _get_includes_string(self.includes)
 
+        macros = ""
+        for macro in self.macros:
+            macros += str(macro)
+        if len(macros) > 0:
+            macros += "\n"
+
         forward_declarations = ""
         for _type in self.decl_types:
             forward_declarations += _type.declare()
@@ -247,6 +263,7 @@ class CHeader(CFile):
             header_guard_open +
             includes +
             c_linkage_open +
+            macros +
             forward_declarations +
             types +
             funcs +
@@ -266,6 +283,12 @@ class CSource(CFile):
     def __str__(self) -> str:
         includes = _get_includes_string(self.includes)
 
+        macros = ""
+        for macro in self.macros:
+            macros += str(macro)
+        if len(macros) > 0:
+            macros += "\n"
+
         forward_declarations = ""
         for _type in self.decl_types:
             forward_declarations += _type.declare()
@@ -292,6 +315,7 @@ class CSource(CFile):
         return (
             super().__str__() +
             includes +
+            macros +
             forward_declarations +
             types +
             internal_funcs_decl +
diff --git a/codegen/dbl_list/make_dbl_list.py b/codegen/dbl_list/make_dbl_list.py
index f979e2c..e7f9111 100644
--- a/codegen/dbl_list/make_dbl_list.py
+++ b/codegen/dbl_list/make_dbl_list.py
@@ -4,6 +4,7 @@ from codegen.constants import WAPP_SRC_ROOT
 from codegen.utils import load_func_body_from_file
 from codegen.datatypes import (
     CDataType,
+    CMacro,
     CStruct,
     CFunc,
     CHeader,
@@ -20,9 +21,6 @@ from codegen.datatypes import (
 
 @dataclass
 class DblListData:
-    out_dir: Path
-    hdr_includes: list[CInclude] = field(default_factory=list)
-    src_includes: list[CInclude] = field(default_factory=list)
     hdr_decl_types: list[CStruct] = field(default_factory=list)
     src_decl_types: list[CStruct] = field(default_factory=list)
 
@@ -36,10 +34,20 @@ def make_dbl_list(user_datatypes: dict[CDataType, DblListData] = {}):
             Tlower=type_string.lower(),
         )
 
+    out_dir = WAPP_SRC_ROOT / "containers" / "dbl_list"
+    out_dir.mkdir(parents=True, exist_ok=True)
+
     common_local_include_files = [
         (WAPP_SRC_ROOT / "common" / "aliases" / "aliases.h")
     ]
     common_includes: list[CInclude] = []
+    for local_file in common_local_include_files:
+        common_includes.append(
+            CInclude(
+                header=str(local_file.relative_to(out_dir, walk_up=True)),
+                local=True,
+            )
+        )
 
     common_decl_types: list[CStruct] = []
 
@@ -48,13 +56,32 @@ def make_dbl_list(user_datatypes: dict[CDataType, DblListData] = {}):
             hdr_decl_types=[
                 CStruct(name="str8", cargs=[], typedef_name="Str8"),
             ],
-            out_dir=WAPP_SRC_ROOT / "core" / "strings" / "str8",
         ),
     }
     datatypes.update(user_datatypes)
 
     snippets_dir = Path(__file__).parent / "snippets"
 
+    header = CHeader(
+        name="dbl_list",
+        decl_types=common_decl_types,
+        includes=[],
+        types=[],
+        funcs=[]
+    )
+
+    source = CSource(
+        name=header.name,
+        decl_types=common_decl_types,
+        includes=[CInclude(header, local=True, same_dir=True), CInclude(header="stddef.h")],
+        internal_funcs=[],
+        funcs=header.funcs
+    )
+
+    if len(common_includes) > 0:
+        header.includes.extend(common_includes)
+        source.includes.extend(common_includes)
+
     for _type, dbl_list_data in datatypes.items():
         type_string = get_datatype_string(_type)
 
@@ -78,13 +105,9 @@ def make_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),
+        node_macro = CMacro(
+            name=f"wapp_{type_string.lower()}_list_node(ITEM_PTR)",
+            value=__format_func_body(snippets_dir / "list_node", type_string),
         )
 
         get_func = CFunc(
@@ -179,46 +202,23 @@ def make_dbl_list(user_datatypes: dict[CDataType, DblListData] = {}):
             qualifiers=[CQualifier.INTERNAL],
         )
 
-        header = CHeader(
-            name=f"{type_string.lower()}_list",
-            decl_types=common_decl_types + dbl_list_data.hdr_decl_types,
-            includes=[],
-            types=[node, dl_list],
-            funcs=[
-                node_func,
-                get_func,
-                push_front_func,
-                push_back_func,
-                insert_func,
-                pop_front_func,
-                pop_back_func,
-                remove_func,
-                empty_func,
-           ]
-        )
+        header.decl_types.extend(dbl_list_data.hdr_decl_types)
+        header.macros.append(node_macro)
+        header.types.extend([node, dl_list])
+        header.funcs.extend([
+            get_func,
+            push_front_func,
+            push_back_func,
+            insert_func,
+            pop_front_func,
+            pop_back_func,
+            remove_func,
+            empty_func,
+        ])
 
-        source = CSource(
-            name=header.name,
-            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
-        )
+        source.decl_types.extend(dbl_list_data.src_decl_types)
+        source.internal_funcs.append(node_to_list_func)
+        source.funcs = header.funcs
 
-        if len(common_includes) > 0:
-            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)
-
-        if len(dbl_list_data.src_includes) > 0:
-            source.includes.extend(dbl_list_data.src_includes)
-
-        header.save(dbl_list_data.out_dir)
-        source.save(dbl_list_data.out_dir)
+    header.save(out_dir)
+    source.save(out_dir)
diff --git a/codegen/dbl_list/snippets/list_node b/codegen/dbl_list/snippets/list_node
index 8a9e407..b689b0e 100644
--- a/codegen/dbl_list/snippets/list_node
+++ b/codegen/dbl_list/snippets/list_node
@@ -1 +1 @@
-  return ({Ttitle}Node){{.item = item}};
+(({Ttitle}Node){{.item = ITEM_PTR}})
diff --git a/src/core/strings/str8/str8_list.c b/src/containers/dbl_list/dbl_list.c
similarity index 96%
rename from src/core/strings/str8/str8_list.c
rename to src/containers/dbl_list/dbl_list.c
index 0950acd..9ddc08e 100644
--- a/src/core/strings/str8/str8_list.c
+++ b/src/containers/dbl_list/dbl_list.c
@@ -2,15 +2,13 @@
  * THIS FILE IS AUTOMATICALLY GENERATED. ANY MODIFICATIONS TO IT WILL BE OVERWRITTEN
  */
 
-#include "./str8_list.h"
-#include "../../../common/aliases/aliases.h"
+#include "./dbl_list.h"
+#include "../../common/aliases/aliases.h"
 #include <stddef.h>
 
-internal Str8List str8_node_to_list(Str8Node *node);
+typedef struct str8 Str8;
 
-Str8Node wapp_str8_list_node(Str8 *item) {
-  return (Str8Node){.item = item};
-}
+internal Str8List str8_node_to_list(Str8Node *node);
 
 Str8Node *wapp_str8_list_get(const Str8List *list, u64 index) {
   if (index >= list->node_count) {
diff --git a/src/core/strings/str8/str8_list.h b/src/containers/dbl_list/dbl_list.h
similarity index 84%
rename from src/core/strings/str8/str8_list.h
rename to src/containers/dbl_list/dbl_list.h
index 7c816b2..a99121e 100644
--- a/src/core/strings/str8/str8_list.h
+++ b/src/containers/dbl_list/dbl_list.h
@@ -2,15 +2,17 @@
  * THIS FILE IS AUTOMATICALLY GENERATED. ANY MODIFICATIONS TO IT WILL BE OVERWRITTEN
  */
 
-#ifndef STR8_LIST_H
-#define STR8_LIST_H
+#ifndef DBL_LIST_H
+#define DBL_LIST_H
 
-#include "../../../common/aliases/aliases.h"
+#include "../../common/aliases/aliases.h"
 
 #ifdef __cplusplus
 BEGIN_C_LINKAGE
 #endif // !__cplusplus
 
+#define wapp_str8_list_node(ITEM_PTR) ((Str8Node){.item = ITEM_PTR})
+
 typedef struct str8 Str8;
 
 typedef struct Str8Node Str8Node;
@@ -27,7 +29,6 @@ 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);
@@ -41,4 +42,4 @@ void wapp_str8_list_empty(Str8List *list);
 END_C_LINKAGE
 #endif // !__cplusplus
 
-#endif // !STR8_LIST_H
+#endif // !DBL_LIST_H
diff --git a/src/containers/wapp_containers.c b/src/containers/wapp_containers.c
new file mode 100644
index 0000000..6fc30b2
--- /dev/null
+++ b/src/containers/wapp_containers.c
@@ -0,0 +1,6 @@
+#ifndef WAPP_CONTAINERS_C
+#define WAPP_CONTAINERS_C
+
+#include "dbl_list/dbl_list.c"
+
+#endif // !WAPP_CONTAINERS_C
diff --git a/src/containers/wapp_containers.h b/src/containers/wapp_containers.h
new file mode 100644
index 0000000..0c48a9f
--- /dev/null
+++ b/src/containers/wapp_containers.h
@@ -0,0 +1,7 @@
+#ifndef WAPP_CONTAINERS_H
+#define WAPP_CONTAINERS_H
+
+#include "dbl_list/dbl_list.h"
+#include "../common/wapp_common.h"
+
+#endif // !WAPP_CONTAINERS_H
diff --git a/src/core/strings/str8/str8.h b/src/core/strings/str8/str8.h
index 725b65d..647b795 100644
--- a/src/core/strings/str8/str8.h
+++ b/src/core/strings/str8/str8.h
@@ -1,8 +1,8 @@
 #ifndef STR8_H
 #define STR8_H
 
-#include "./str8_list.h"
 #include "../../../common/aliases/aliases.h"
+#include "../../../containers/dbl_list/dbl_list.h"
 #include "../../mem/allocator/mem_allocator.h"
 #include <string.h>
 #include <stdbool.h>
@@ -91,8 +91,8 @@ Str8     *wapp_str8_join(const Allocator *allocator, const Str8List *list, Str8R
 /**
  * Str8 list utilities
  */
-#define wapp_str8_node_from_cstr(STRING) ((Str8Node){.item = &wapp_str8_lit(STRING)})
-#define wapp_str8_node_from_str8(STRING) ((Str8Node){.item = &(STRING)})
+#define wapp_str8_node_from_cstr(STRING) wapp_str8_list_node(&wapp_str8_lit(STRING))
+#define wapp_str8_node_from_str8(STRING) wapp_str8_list_node(&(STRING))
 u64 wapp_str8_list_total_size(const Str8List *list);
 
 #ifdef __cplusplus
diff --git a/src/core/wapp_core.c b/src/core/wapp_core.c
index d41bc27..dfab160 100644
--- a/src/core/wapp_core.c
+++ b/src/core/wapp_core.c
@@ -3,7 +3,6 @@
 
 #include "wapp_core.h"
 #include "strings/str8/str8.c"
-#include "strings/str8/str8_list.c"
 #include "os/shell/termcolour/posix/termcolour_posix.c"
 #include "os/shell/termcolour/win/termcolour_win.c"
 #include "os/shell/termcolour/termcolour.c"
@@ -18,5 +17,6 @@
 #include "mem/allocator/mem_allocator.c"
 #include "mem/arena/mem_arena.c"
 #include "mem/arena/mem_arena_allocator.c"
+#include "../containers/wapp_containers.c"
 
 #endif // !WAPP_CORE_C
diff --git a/src/core/wapp_core.h b/src/core/wapp_core.h
index d698b47..e1903a9 100644
--- a/src/core/wapp_core.h
+++ b/src/core/wapp_core.h
@@ -2,7 +2,6 @@
 #define WAPP_CORE_H
 
 #include "strings/str8/str8.h"
-#include "strings/str8/str8_list.h"
 #include "os/shell/termcolour/termcolour.h"
 #include "os/shell/termcolour/terminal_colours.h"
 #include "os/shell/commander/commander.h"
@@ -18,5 +17,6 @@
 #include "mem/arena/mem_arena_allocator.h"
 #include "mem/arena/mem_arena.h"
 #include "../common/wapp_common.h"
+#include "../containers/wapp_containers.h"
 
 #endif // !WAPP_CORE_H