Update code generation to create one file for dbl_list

This commit is contained in:
Abdelrahman Said 2025-04-19 13:33:09 +01:00
parent 3c32b247c0
commit 63ed2633ea
11 changed files with 105 additions and 70 deletions

View File

@ -8,7 +8,6 @@ def main():
def gen_dbl_list():
datatypes: dict[CDataType, DblListData] = {}
make_dbl_list(datatypes)

View File

@ -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 +

View File

@ -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)

View File

@ -1 +1 @@
return ({Ttitle}Node){{.item = item}};
(({Ttitle}Node){{.item = ITEM_PTR}})

View File

@ -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) {

View File

@ -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

View File

@ -0,0 +1,6 @@
#ifndef WAPP_CONTAINERS_C
#define WAPP_CONTAINERS_C
#include "dbl_list/dbl_list.c"
#endif // !WAPP_CONTAINERS_C

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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