feat(parser): 添加语义分析回调清理函数并完善资源管理
添加了 scc_sema_drop 函数用于清理语义分析回调结构, 并在主程序中正确初始化和释放语义分析回调, 确保资源得到适当管理。 同时修复了AST转储中的缩进问题,在函数调用括号前添加正确的缩进, 并修正了测试代码中的结构体字段初始化顺序。
This commit is contained in:
@@ -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);
|
||||
}
|
||||
} else {
|
||||
scc_tree_print_indent(ctx);
|
||||
PRINT_QUOTED_VALUE(ctx, "()");
|
||||
scc_tree_dump_printf(ctx, "\n");
|
||||
}
|
||||
scc_vec_pop(ctx->stack);
|
||||
break;
|
||||
|
||||
@@ -24,5 +24,6 @@ typedef struct scc_sema_callbacks {
|
||||
} scc_sema_callbacks_t;
|
||||
|
||||
void scc_sema_init(scc_sema_callbacks_t *callbacks);
|
||||
void scc_sema_drop(scc_sema_callbacks_t *callbacks);
|
||||
|
||||
#endif /* __SCC_SEMA_H__ */
|
||||
|
||||
@@ -14,3 +14,5 @@ void scc_sema_init(scc_sema_callbacks_t *callbacks) {
|
||||
callbacks->on_type = null;
|
||||
callbacks->got_type = null;
|
||||
}
|
||||
|
||||
void scc_sema_drop(scc_sema_callbacks_t *callbacks) {}
|
||||
|
||||
@@ -1123,6 +1123,8 @@ static void test_parser_type(void) {
|
||||
scc_ast_type_t struct_type;
|
||||
scc_ast_type_struct_init(&struct_type, null, &struct_def);
|
||||
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_type_struct_init(&struct_type, "A", &struct_def);
|
||||
SCC_CHECK_AST(&struct_type.base, "struct A { int x; }",
|
||||
|
||||
13
src/main.c
13
src/main.c
@@ -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_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_parse_translation_unit(&parser);
|
||||
|
||||
// scc_parser_drop(&parser);
|
||||
// scc_pproc_drop(&pproc);
|
||||
// scc_lexer_drop(&lexer);
|
||||
// scc_sstream_drop(&sstream);
|
||||
scc_sema_drop(&sema_callbacks);
|
||||
scc_parser_drop(&parser);
|
||||
scc_pproc_drop(&pproc);
|
||||
scc_lexer_drop(&lexer);
|
||||
scc_sstream_drop(&sstream);
|
||||
|
||||
if (config.emit_ast) {
|
||||
scc_tree_dump_ctx_t tree_dump;
|
||||
|
||||
Reference in New Issue
Block a user