feat(parser): 使用静态数组初始化测试向量

- 将多个测试用例中的 `scc_vec_unsafe_from_array` 替换为
  `scc_vec_unsafe_from_static_array` 以提高性能
- 此更改影响了 `test_parser_unit` 和 `test_parser_type` 函数中的多个位置

feat(sccf): 添加SCC格式支持和相关工具

- 创建新的 sccf 库用于处理 SCCF (SCC Format) 文件格式
- 实现了基本的文件格式定义,包括头部、段表、符号表等结构
- 添加了构建器和链接器的基本框架
- 包含格式化工具的初始实现

refactor(main): 修复输出文件检查逻辑

- 修正主函数中输出文件检查条件,确保在 fp 为 null 时正确处理
- 更新输出消息显示逻辑以匹配文件操作状态
This commit is contained in:
zzy
2026-03-16 11:02:32 +08:00
parent 45c59e0b65
commit cabd1710ed
12 changed files with 614 additions and 27 deletions

View File

@@ -112,7 +112,7 @@ static void test_parser_unit(void) {
scc_ast_decl_t void_decl;
scc_ast_decl_param_init(&void_decl, &scc_ast_builtin_type_void, null);
scc_ast_decl_t *array[] = {&void_decl};
scc_vec_unsafe_from_array(func_params, array);
scc_vec_unsafe_from_static_array(func_params, array);
scc_ast_type_function_init(&func_type, &scc_ast_builtin_type_int,
&func_params);
// 构造复合语句块(空)
@@ -134,7 +134,7 @@ static void test_parser_unit(void) {
scc_ast_decl_t void_decl;
scc_ast_decl_param_init(&void_decl, &scc_ast_builtin_type_void, null);
scc_ast_decl_t *array[] = {&void_decl};
scc_vec_unsafe_from_array(func_params, array);
scc_vec_unsafe_from_static_array(func_params, array);
scc_ast_type_function_init(&func_type, &scc_ast_builtin_type_int,
&func_params);
@@ -179,7 +179,7 @@ static void test_parser_unit(void) {
scc_ast_decl_t void_decl;
scc_ast_decl_param_init(&void_decl, &scc_ast_builtin_type_void, null);
scc_ast_decl_t *array[] = {&void_decl};
scc_vec_unsafe_from_array(func_params, array);
scc_vec_unsafe_from_static_array(func_params, array);
scc_ast_type_function_init(&func_type, &scc_ast_builtin_type_int,
&func_params);
@@ -374,7 +374,7 @@ static void test_parser_unit(void) {
scc_ast_decl_param_init(
&param2, (scc_ast_type_t *)&scc_ast_builtin_type_va_list, null);
scc_ast_decl_t *params_array[] = {&param0, &param1, &param2};
scc_vec_unsafe_from_array(params, params_array);
scc_vec_unsafe_from_static_array(params, params_array);
scc_ast_type_t decl_func_type;
scc_ast_type_function_init(&decl_func_type,
(scc_ast_type_t *)&scc_ast_builtin_type_int,
@@ -398,7 +398,7 @@ static void test_parser_unit(void) {
scc_ast_node_t *array[] = {&typedef_decl.base, &i32a_decl.base};
scc_ast_stmt_t stmt;
scc_ast_block_item_vec_t items;
scc_vec_unsafe_from_array(items, array);
scc_vec_unsafe_from_static_array(items, array);
scc_ast_stmt_compound_init(&stmt, &items);
SCC_CHECK_AST_WITH_SEMA(&stmt.base, "{typedef int int32_t;int32_t a;}",
scc_parse_statement);
@@ -414,7 +414,7 @@ static void test_parser_unit(void) {
scc_ast_decl_val_init(&void_ptr_a_decl, &void_ptr_type, "a", null);
scc_ast_node_t *array2[] = {&void_ptr_decl.base, &void_ptr_a_decl.base};
scc_vec_unsafe_from_array(items, array2);
scc_vec_unsafe_from_static_array(items, array2);
scc_ast_stmt_compound_init(&stmt, &items);
SCC_CHECK_AST_WITH_SEMA(&stmt.base,
"{typedef void* void_ptr; void_ptr a;}",
@@ -452,7 +452,7 @@ static void test_parser_unit(void) {
scc_ast_node_t *array[] = {&typedef_decl.base, &typedef_impl_decl.base};
scc_ast_stmt_t stmt;
scc_ast_block_item_vec_t items;
scc_vec_unsafe_from_array(items, array);
scc_vec_unsafe_from_static_array(items, array);
scc_ast_stmt_compound_init(&stmt, &items);
SCC_CHECK_AST_WITH_SEMA(
&stmt.base, "{typedef struct { int x; } struct_t; struct_t a;}",
@@ -474,7 +474,7 @@ static void test_parser_unit(void) {
scc_ast_decl_param_init(&param3, &scc_ast_builtin_type_va_list, null);
scc_ast_decl_t *params_array[] = {&param1, &param2, &param3};
scc_ast_decl_vec_t func_params;
scc_vec_unsafe_from_array(func_params, params_array);
scc_vec_unsafe_from_static_array(func_params, params_array);
scc_ast_type_t func_type;
scc_ast_type_t return_type;
@@ -487,7 +487,7 @@ static void test_parser_unit(void) {
scc_ast_decl_t *decls_array[] = {&type_decl, &func_decl};
scc_ast_translation_unit_t tu;
scc_ast_decl_vec_t decls;
scc_vec_unsafe_from_array(decls, decls_array);
scc_vec_unsafe_from_static_array(decls, decls_array);
scc_ast_translation_unit_init(&tu, &decls);
SCC_CHECK_AST_WITH_SEMA(&tu.base,
"typedef long long size_t;"
@@ -500,7 +500,7 @@ static void test_parser_unit(void) {
scc_ast_decl_typedef_init(&type_func_ptr_decl, "func_t",
&type_func_ptr_type);
scc_ast_decl_t *decls_array2[] = {&type_decl, &type_func_ptr_decl};
scc_vec_unsafe_from_array(decls, decls_array2);
scc_vec_unsafe_from_static_array(decls, decls_array2);
scc_ast_translation_unit_init(&tu, &decls);
SCC_CHECK_AST_WITH_SEMA(
&tu.base,
@@ -578,11 +578,11 @@ static void test_parser_unit(void) {
scc_ast_expr_vec_t lhs_exprs;
scc_ast_expr_t *lhs_array[] = {&lhs1, &lhs2, &lhs3};
scc_vec_unsafe_from_array(lhs_exprs, lhs_array);
scc_vec_unsafe_from_static_array(lhs_exprs, lhs_array);
scc_ast_expr_vec_t rhs_exprs;
scc_ast_expr_t *rhs_array[] = {&rhs1, &rhs2, &rhs3};
scc_vec_unsafe_from_array(rhs_exprs, rhs_array);
scc_vec_unsafe_from_static_array(rhs_exprs, rhs_array);
scc_ast_expr_t expr;
scc_ast_expr_compound_init(&expr, &lvalue, &lhs_exprs, &rhs_exprs);
@@ -608,9 +608,9 @@ static void test_parser_unit(void) {
scc_ast_expr_array_subscript_init(&lhs7, &lhs6, &rl0);
scc_ast_expr_t *lhs_array_hard[] = {&lhs5, &lhs7};
scc_vec_unsafe_from_array(lhs_exprs, lhs_array_hard);
scc_vec_unsafe_from_static_array(lhs_exprs, lhs_array_hard);
scc_ast_expr_t *rhs_array_hard[] = {&rhs2, &rhs3};
scc_vec_unsafe_from_array(rhs_exprs, rhs_array_hard);
scc_vec_unsafe_from_static_array(rhs_exprs, rhs_array_hard);
scc_ast_expr_compound_init(&expr, &lvalue, &lhs_exprs, &rhs_exprs);
SCC_CHECK_AST(&expr.base, "(void){.a.b = 0, .c[0] = 0}",
scc_parse_expression);
@@ -834,7 +834,7 @@ static void test_parser_unit(void) {
scc_ast_decl_t void_decl;
scc_ast_decl_param_init(&void_decl, &scc_ast_builtin_type_void, null);
scc_ast_decl_t *array[] = {&void_decl};
scc_vec_unsafe_from_array(func_params, array);
scc_vec_unsafe_from_static_array(func_params, array);
scc_ast_type_function_init(&func_type, &ptr_to_array,
&func_params); // 无参数
@@ -863,7 +863,7 @@ static void test_parser_unit(void) {
scc_ast_decl_param_init(&param1, &scc_ast_builtin_type_va_list, null);
scc_ast_decl_t *func_hard_array[] = {&param1, &param2, &param3};
scc_ast_decl_vec_t func_hard_params;
scc_vec_unsafe_from_array(func_hard_params, func_hard_array);
scc_vec_unsafe_from_static_array(func_hard_params, func_hard_array);
scc_ast_type_function_init(&func_hard_type, &ptr_to_array,
&func_hard_params);
scc_ast_decl_func_init(&func_hard_decl, &func_hard_type, "bar", null);
@@ -873,7 +873,7 @@ static void test_parser_unit(void) {
&typedef_func_decl,
&func_hard_decl,
};
scc_vec_unsafe_from_array(decls, decls_array);
scc_vec_unsafe_from_static_array(decls, decls_array);
scc_ast_translation_unit_t tu;
scc_ast_translation_unit_init(&tu, &decls);
// SCC_CHECK_AST_WITH_SEMA(
@@ -1241,7 +1241,7 @@ static void test_parser_type(void) {
scc_ast_decl_param_init(&void_decl, &scc_ast_builtin_type_void,
null);
scc_ast_decl_t *array[] = {&void_decl};
scc_vec_unsafe_from_array(func_params, array);
scc_vec_unsafe_from_static_array(func_params, array);
scc_ast_type_function_init(
&func_void, (scc_ast_type_t *)&scc_ast_builtin_type_int,
&func_params);
@@ -1486,7 +1486,7 @@ static void test_parser_type(void) {
scc_ast_decl_t void_decl;
scc_ast_decl_param_init(&void_decl, &scc_ast_builtin_type_void, null);
scc_ast_decl_t *array[] = {&void_decl};
scc_vec_unsafe_from_array(func_params, array);
scc_vec_unsafe_from_static_array(func_params, array);
scc_ast_type_function_init(&func_void,
(scc_ast_type_t *)&scc_ast_builtin_type_int,
&func_params);
@@ -1575,7 +1575,7 @@ static void test_parser_type(void) {
scc_ast_decl_t void_decl;
scc_ast_decl_param_init(&void_decl, &scc_ast_builtin_type_void, null);
scc_ast_decl_t *array[] = {&void_decl};
scc_vec_unsafe_from_array(func_params, array);
scc_vec_unsafe_from_static_array(func_params, array);
scc_ast_type_function_init(&func_type, &ptr_to_array,
&func_params); // 无参数
@@ -1590,7 +1590,7 @@ static void test_parser_type(void) {
// 1) 函数类型:返回 int无参数
scc_ast_type_t func_type2;
scc_ast_decl_vec_t func_params2;
scc_vec_unsafe_from_array(func_params2, array);
scc_vec_unsafe_from_static_array(func_params2, array);
scc_ast_type_function_init(&func_type2,
(scc_ast_type_t *)&scc_ast_builtin_type_int,
&func_params2);
@@ -1678,7 +1678,7 @@ static void test_parser_type(void) {
scc_ast_expr_identifier_init(&blue, "BLUE");
scc_ast_expr_vec_t enumerators;
scc_ast_expr_t *array[] = {&red, &green, &blue};
scc_vec_unsafe_from_array(enumerators, array);
scc_vec_unsafe_from_static_array(enumerators, array);
scc_ast_decl_t enum_def;
scc_ast_decl_enum_init(&enum_def, null, &enumerators);
@@ -1687,7 +1687,7 @@ static void test_parser_type(void) {
SCC_CHECK_AST(&enum_type.base, "enum { RED, GREEN, BLUE }",
scc_parse_type_name);
scc_vec_unsafe_from_array(enumerators, array);
scc_vec_unsafe_from_static_array(enumerators, array);
scc_ast_decl_enum_init(&enum_def, "E", &enumerators);
scc_ast_type_enum_init(&enum_type, "E", &enum_def);
SCC_CHECK_AST(&enum_type.base, "enum E { RED, GREEN, BLUE, }",