Implement stringifying source

This commit is contained in:
Abdelrahman Said 2025-09-07 20:44:57 +01:00
parent ea09838b46
commit 3f995db65b

View File

@ -54,8 +54,10 @@ void cobject_to_string(Str8 *dst, const CObject *object) {
cinclude_to_string(dst, &(object->object.c_include)); cinclude_to_string(dst, &(object->object.c_include));
break; break;
case COBJECT_CHEADER: case COBJECT_CHEADER:
cheader_to_string(dst, &(object->object.c_header));
break; break;
case COBJECT_CSOURCE: case COBJECT_CSOURCE:
csource_to_string(dst, &(object->object.c_source));
break; break;
default: default:
break; break;
@ -437,8 +439,61 @@ void cheader_to_string(Str8 *dst, const CHeader *cheader) {
} }
void csource_to_string(Str8 *dst, const CSource *csource) { void csource_to_string(Str8 *dst, const CSource *csource) {
(void)dst; wapp_debug_assert(dst != NULL && csource != NULL, "`dst` and `csource` should not be NULL");
(void)csource;
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) { b32 cheaderinclude_to_string(Str8 *dst, const CHeaderInclude *cheaderinclude) {