Upgrade codegen

This commit is contained in:
Abdelrahman Said 2025-04-17 09:03:30 +01:00
parent aa04fab6ea
commit 3c32b247c0
15 changed files with 66 additions and 46 deletions

View File

@ -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__":

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,4 +1,4 @@
{T}Node *output = NULL;
{Ttitle}Node *output = NULL;
if (!list) {{
goto RETURN_{Tupper}_LIST_REMOVE;
}}

View File

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

View File

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

View File

@ -4,13 +4,16 @@
#include "./str8_list.h"
#include "../../../common/aliases/aliases.h"
#include "./str8.h"
#include <stddef.h>
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;

View File

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