feat(parser): 添加语义分析回调清理函数并完善资源管理

添加了 scc_sema_drop 函数用于清理语义分析回调结构,
并在主程序中正确初始化和释放语义分析回调,
确保资源得到适当管理。

同时修复了AST转储中的缩进问题,在函数调用括号前添加正确的缩进,
并修正了测试代码中的结构体字段初始化顺序。
This commit is contained in:
zzy
2026-03-11 22:07:52 +08:00
parent e6511c508c
commit 30ac2de73b
5 changed files with 15 additions and 5 deletions

View File

@@ -355,7 +355,9 @@ static void dump_type_impl(scc_ast_type_t *type, scc_tree_dump_ctx_t *ctx) {
dump_type_impl(param->param.type, ctx); dump_type_impl(param->param.type, ctx);
} }
} else { } else {
scc_tree_print_indent(ctx);
PRINT_QUOTED_VALUE(ctx, "()"); PRINT_QUOTED_VALUE(ctx, "()");
scc_tree_dump_printf(ctx, "\n");
} }
scc_vec_pop(ctx->stack); scc_vec_pop(ctx->stack);
break; break;

View File

@@ -24,5 +24,6 @@ typedef struct scc_sema_callbacks {
} scc_sema_callbacks_t; } scc_sema_callbacks_t;
void scc_sema_init(scc_sema_callbacks_t *callbacks); void scc_sema_init(scc_sema_callbacks_t *callbacks);
void scc_sema_drop(scc_sema_callbacks_t *callbacks);
#endif /* __SCC_SEMA_H__ */ #endif /* __SCC_SEMA_H__ */

View File

@@ -14,3 +14,5 @@ void scc_sema_init(scc_sema_callbacks_t *callbacks) {
callbacks->on_type = null; callbacks->on_type = null;
callbacks->got_type = null; callbacks->got_type = null;
} }
void scc_sema_drop(scc_sema_callbacks_t *callbacks) {}

View File

@@ -1123,6 +1123,8 @@ static void test_parser_type(void) {
scc_ast_type_t struct_type; scc_ast_type_t struct_type;
scc_ast_type_struct_init(&struct_type, null, &struct_def); scc_ast_type_struct_init(&struct_type, null, &struct_def);
SCC_CHECK_AST(&struct_type.base, "struct { int x; }", _scc_parse_type); SCC_CHECK_AST(&struct_type.base, "struct { int x; }", _scc_parse_type);
scc_vec_init(fields);
scc_vec_push(fields, &field);
scc_ast_decl_struct_init(&struct_def, "A", &fields); scc_ast_decl_struct_init(&struct_def, "A", &fields);
scc_ast_type_struct_init(&struct_type, "A", &struct_def); scc_ast_type_struct_init(&struct_type, "A", &struct_def);
SCC_CHECK_AST(&struct_type.base, "struct A { int x; }", SCC_CHECK_AST(&struct_type.base, "struct A { int x; }",

View File

@@ -271,14 +271,17 @@ int main(int argc, const char **argv, const char **envp) {
scc_lexer_tok_ring_t *tok_ring = scc_pproc_to_ring(&pproc, 8, false, false); scc_lexer_tok_ring_t *tok_ring = scc_pproc_to_ring(&pproc, 8, false, false);
scc_parser_t parser; scc_parser_t parser;
scc_parser_init(&parser, tok_ring, null); scc_sema_callbacks_t sema_callbacks;
scc_sema_init(&sema_callbacks);
scc_parser_init(&parser, tok_ring, &sema_callbacks);
scc_ast_translation_unit_t *translation_unit = scc_ast_translation_unit_t *translation_unit =
scc_parse_translation_unit(&parser); scc_parse_translation_unit(&parser);
// scc_parser_drop(&parser); scc_sema_drop(&sema_callbacks);
// scc_pproc_drop(&pproc); scc_parser_drop(&parser);
// scc_lexer_drop(&lexer); scc_pproc_drop(&pproc);
// scc_sstream_drop(&sstream); scc_lexer_drop(&lexer);
scc_sstream_drop(&sstream);
if (config.emit_ast) { if (config.emit_ast) {
scc_tree_dump_ctx_t tree_dump; scc_tree_dump_ctx_t tree_dump;