From 3f995db65bef3ceca5a7916b1316379b60ee12b7 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 7 Sep 2025 20:44:57 +0100 Subject: [PATCH] Implement stringifying source --- ccodegen/datatypes.c | 59 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/ccodegen/datatypes.c b/ccodegen/datatypes.c index 5a4df90..454bc80 100644 --- a/ccodegen/datatypes.c +++ b/ccodegen/datatypes.c @@ -54,8 +54,10 @@ void cobject_to_string(Str8 *dst, const CObject *object) { cinclude_to_string(dst, &(object->object.c_include)); break; case COBJECT_CHEADER: + cheader_to_string(dst, &(object->object.c_header)); break; case COBJECT_CSOURCE: + csource_to_string(dst, &(object->object.c_source)); break; default: break; @@ -437,8 +439,61 @@ void cheader_to_string(Str8 *dst, const CHeader *cheader) { } void csource_to_string(Str8 *dst, const CSource *csource) { - (void)dst; - (void)csource; + wapp_debug_assert(dst != NULL && csource != NULL, "`dst` and `csource` should not be NULL"); + + Allocator arena = wapp_mem_arena_allocator_init(MB(64)); + Str8 *output = wapp_str8_alloc_buf(&arena, KB(32)); + Str8 *internal_funcs_def = wapp_str8_alloc_buf(&arena, KB(16)); + wapp_runtime_assert(output != NULL && internal_funcs_def != NULL, "Failed to allocate buffer"); + + Str8 tmp = wapp_str8_buf(CCGEN_BUF_MAX); + + for (u64 i = 0; i < csource->includes.node_count; ++i) { + cinclude_to_string(&tmp, wapp_cinclude_list_get(&(csource->includes), i)->item); + wapp_str8_alloc_concat(&arena, output, &tmp); + } + if (csource->includes.node_count > 0) { wapp_str8_alloc_concat(&arena, output, &wapp_str8_lit_ro("\n")); } + + for (u64 i = 0; i < csource->macros.node_count; ++i) { + cmacro_to_string(&tmp, wapp_cmacro_list_get(&(csource->macros), i)->item); + wapp_str8_alloc_concat(&arena, output, &tmp); + } + if (csource->macros.node_count > 0) { wapp_str8_alloc_concat(&arena, output, &wapp_str8_lit_ro("\n")); } + + for (u64 i = 0; i < csource->decl_types.node_count; ++i) { + declare_cstruct(&tmp, wapp_cstruct_list_get(&(csource->decl_types), i)->item); + wapp_str8_alloc_concat(&arena, output, &tmp); + } + if (csource->decl_types.node_count > 0) { wapp_str8_alloc_concat(&arena, output, &wapp_str8_lit_ro("\n")); } + + for (u64 i = 0; i < csource->types.node_count; ++i) { + cusertype_to_string(&tmp, wapp_cusertype_list_get(&(csource->types), i)->item); + wapp_str8_concat_capped(&tmp, &wapp_str8_lit_ro("\n")); + wapp_str8_alloc_concat(&arena, output, &tmp); + } + + Str8RO _internal = wapp_str8_lit_ro("internal "); + for (u64 i = 0; i < csource->internal_funcs.node_count; ++i) { + declare_cfunc(&tmp, wapp_cfunc_list_get(&(csource->internal_funcs), i)->item); + wapp_str8_alloc_concat(&arena, output, &_internal); + wapp_str8_alloc_concat(&arena, output, &tmp); + + define_cfunc(&tmp, wapp_cfunc_list_get(&(csource->internal_funcs), i)->item); + wapp_str8_alloc_concat(&arena, internal_funcs_def, &_internal); + wapp_str8_alloc_concat(&arena, internal_funcs_def, &tmp); + } + if (csource->internal_funcs.node_count > 0) { wapp_str8_alloc_concat(&arena, output, &wapp_str8_lit_ro("\n")); } + + for (u64 i = 0; i < csource->funcs.node_count; ++i) { + define_cfunc(&tmp, wapp_cfunc_list_get(&(csource->funcs), i)->item); + wapp_str8_alloc_concat(&arena, output, &tmp); + } + + wapp_runtime_assert(output->size + internal_funcs_def->size <= dst->capacity, ERR_MSG); + wapp_str8_copy_str8_capped(dst, output); + wapp_str8_concat_capped(dst, internal_funcs_def); + + wapp_mem_arena_allocator_destroy(&arena); } b32 cheaderinclude_to_string(Str8 *dst, const CHeaderInclude *cheaderinclude) {