feat(ast2ir): 添加左值标识支持以改善表达式处理
- 在 scc_ast2ir_expr 函数中添加 is_lvalue 参数来区分左值和右值表达式 - 更新二元表达式处理逻辑,特别是赋值操作符的处理 - 改进标识符表达式的处理,根据是否为左值决定返回存储位置还是加载值 - 修复哈希比较函数的实现 - 移除调试相关的注释代码 refactor(parser): 优化语法分析器错误处理和控制流 - 移除不必要的错误恢复辅助注释 - 修改表达式解析的控制流程,将直接返回改为使用 break 语句 - 添加语义分析回调,在解析完成后进行标识符查找和验证 refactor(sema): 增强语义分析阶段的符号表管理 - 改进标识符查找逻辑,增加对非变量标识符的检查 - 扩展声明处理范围,包括变量和参数声明的符号表注册 - 为函数声明添加作用域管理 fix(parser): 修正单元测试中的类型定义 - 将 long long 类型定义改为 int 类型,解决测试兼容性问题 refactor(sccf): 重构文件格式定义和构建器实现 - 重命名符号类型枚举值 OBJECT 为 EXTERN - 重命名段类型枚举值 RELOC 为 RELOCS - 修正结构体字段命名的一致性问题 - 重新设计 SCCF 构建器的数据结构和API - 添加符号表、字符串表和重定位表的构建支持 refactor(target): 重命名Windows PE相关类型定义 - 将 scc_winpe_* 类型重命名为 scc_pe_* 以保持命名一致性 chore: 添加 sccf2target 模块用于格式转换 - 创建新的库模块用于 SCCF 到目标格式的转换 - 实现 PE 格式转换的基本功能 - 添加示例程序演示格式转换过程
This commit is contained in:
@@ -321,7 +321,6 @@ static scc_ast_expr_op_t map_token_to_assign_op(scc_tok_type_t type) {
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------------------- 错误恢复辅助 ---------------------------- */
|
||||
// 跳过直到遇到同步 token(分号、右括号、逗号、EOF)
|
||||
static void parser_sync(scc_parser_t *parser) {
|
||||
const scc_lexer_tok_t *tok_ptr;
|
||||
@@ -340,7 +339,6 @@ static void parser_sync(scc_parser_t *parser) {
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------------------------- 通用二元解析器 ---------------------------- */
|
||||
static scc_ast_expr_t *parse_expression_with_precedence(scc_parser_t *parser,
|
||||
int min_prec) {
|
||||
// 从最底层(cast-expression)开始
|
||||
@@ -790,7 +788,7 @@ static scc_ast_expr_t *parse_primary_expression(scc_parser_t *parser) {
|
||||
Assert(expr != null);
|
||||
scc_ast_expr_identifier_init(expr, scc_cstring_as_cstr(&tok.lexeme),
|
||||
tok.loc);
|
||||
return expr;
|
||||
break;
|
||||
}
|
||||
case SCC_TOK_INT_LITERAL: {
|
||||
if (!scc_parser_next_consume(parser, &tok))
|
||||
@@ -799,7 +797,7 @@ static scc_ast_expr_t *parse_primary_expression(scc_parser_t *parser) {
|
||||
Assert(expr != null);
|
||||
scc_ast_expr_literal_int_init(expr, scc_cstring_as_cstr(&tok.lexeme),
|
||||
false, tok.loc);
|
||||
return expr;
|
||||
break;
|
||||
}
|
||||
case SCC_TOK_FLOAT_LITERAL: {
|
||||
if (!scc_parser_next_consume(parser, &tok))
|
||||
@@ -808,7 +806,7 @@ static scc_ast_expr_t *parse_primary_expression(scc_parser_t *parser) {
|
||||
Assert(expr != null);
|
||||
scc_ast_expr_literal_float_init(expr, scc_cstring_as_cstr(&tok.lexeme),
|
||||
false, tok.loc);
|
||||
return expr;
|
||||
break;
|
||||
}
|
||||
case SCC_TOK_CHAR_LITERAL: {
|
||||
if (!scc_parser_next_consume(parser, &tok))
|
||||
@@ -817,7 +815,7 @@ static scc_ast_expr_t *parse_primary_expression(scc_parser_t *parser) {
|
||||
Assert(expr != null);
|
||||
scc_ast_expr_literal_char_init(expr, scc_cstring_as_cstr(&tok.lexeme),
|
||||
false, tok.loc);
|
||||
return expr;
|
||||
break;
|
||||
}
|
||||
case SCC_TOK_STRING_LITERAL: {
|
||||
scc_cstring_t string = scc_cstring_create();
|
||||
@@ -841,7 +839,7 @@ static scc_ast_expr_t *parse_primary_expression(scc_parser_t *parser) {
|
||||
// FIXME loc
|
||||
scc_ast_expr_literal_string_init(expr, scc_cstring_as_cstr(&string),
|
||||
true, tok.loc);
|
||||
return expr;
|
||||
break;
|
||||
}
|
||||
case SCC_TOK_L_PAREN:
|
||||
scc_parser_next_consume(parser, null);
|
||||
@@ -850,10 +848,16 @@ static scc_ast_expr_t *parse_primary_expression(scc_parser_t *parser) {
|
||||
SCC_ERROR(scc_parser_got_current_pos(parser),
|
||||
"Expected ')' after expression");
|
||||
}
|
||||
return expr;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (expr == null) {
|
||||
return null;
|
||||
}
|
||||
scc_parse_expr_sema(parser, expr);
|
||||
return expr;
|
||||
}
|
||||
|
||||
scc_ast_expr_t *scc_parse_expression(scc_parser_t *parser) {
|
||||
|
||||
@@ -18,15 +18,21 @@ static void expr_callback(void *context, scc_ast_node_type_t node_type,
|
||||
if (node_type == SCC_AST_UNKNOWN || node == null) {
|
||||
return;
|
||||
}
|
||||
scc_ast_expr_t *decl = SCC_AST_CAST_TO(scc_ast_expr_t, node);
|
||||
scc_ast_expr_t *expr = SCC_AST_CAST_TO(scc_ast_expr_t, node);
|
||||
if (node_type == SCC_AST_EXPR_IDENTIFIER) {
|
||||
scc_ast_node_t *node =
|
||||
scc_sema_symtab_lookup_symbol(sema_symtab, decl->identifier.name);
|
||||
scc_sema_symtab_lookup_symbol(sema_symtab, expr->identifier.name);
|
||||
if (node == null) {
|
||||
SCC_ERROR(decl->base.loc, "Identifier '%s' not found",
|
||||
decl->identifier.name);
|
||||
SCC_ERROR(expr->base.loc, "Identifier '%s' not found",
|
||||
expr->identifier.name);
|
||||
} else if (!SCC_AST_IS_A(scc_ast_decl_t, node)) {
|
||||
SCC_ERROR(expr->base.loc, "Identifier '%s' is not a variable",
|
||||
expr->identifier.name);
|
||||
} else {
|
||||
expr->identifier._target = SCC_AST_CAST_TO(scc_ast_decl_t, node);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -50,6 +56,8 @@ static void stmt_callback(void *context, scc_ast_node_type_t node_type,
|
||||
static void decl_callback(void *context, scc_ast_node_type_t node_type,
|
||||
void *node) {
|
||||
scc_sema_symtab_t *sema_symtab = context;
|
||||
|
||||
// Function declaration scope
|
||||
if (node_type == scc_ast_decl_t_BEGIN) {
|
||||
scc_sema_symtab_enter_scope(sema_symtab);
|
||||
return;
|
||||
@@ -67,27 +75,31 @@ static void decl_callback(void *context, scc_ast_node_type_t node_type,
|
||||
if (decl->name == null) {
|
||||
return;
|
||||
}
|
||||
if (decl->base.type == SCC_AST_DECL_STRUCT) {
|
||||
if (node_type == SCC_AST_DECL_STRUCT) {
|
||||
scc_ast_type_struct_init(type, decl->name, decl, decl->base.loc);
|
||||
scc_cstring_t name = scc_cstring_from_cstr("$S_");
|
||||
scc_cstring_append_cstr(&name, decl->name, scc_strlen(decl->name));
|
||||
scc_sema_symtab_add_symbol(sema_symtab, scc_cstring_as_cstr(&name),
|
||||
&type->base);
|
||||
} else if (decl->base.type == SCC_AST_DECL_UNION) {
|
||||
} else if (node_type == SCC_AST_DECL_UNION) {
|
||||
scc_ast_type_union_init(type, decl->name, decl, decl->base.loc);
|
||||
scc_cstring_t name = scc_cstring_from_cstr("$U_");
|
||||
scc_cstring_append_cstr(&name, decl->name, scc_strlen(decl->name));
|
||||
scc_sema_symtab_add_symbol(sema_symtab, scc_cstring_as_cstr(&name),
|
||||
&type->base);
|
||||
} else if (decl->base.type == SCC_AST_DECL_ENUM) {
|
||||
} else if (node_type == SCC_AST_DECL_ENUM) {
|
||||
scc_ast_type_enum_init(type, decl->name, decl, decl->base.loc);
|
||||
scc_cstring_t name = scc_cstring_from_cstr("$E_");
|
||||
scc_cstring_append_cstr(&name, decl->name, scc_strlen(decl->name));
|
||||
scc_sema_symtab_add_symbol(sema_symtab, scc_cstring_as_cstr(&name),
|
||||
&type->base);
|
||||
} else if (decl->base.type == SCC_AST_DECL_TYPEDEF) {
|
||||
} else if (node_type == SCC_AST_DECL_TYPEDEF) {
|
||||
scc_ast_type_typedef_init(type, decl->name, decl, decl->base.loc);
|
||||
scc_sema_symtab_add_symbol(sema_symtab, decl->name, &type->base);
|
||||
} else if (node_type == SCC_AST_DECL_VAR) {
|
||||
scc_sema_symtab_add_symbol(sema_symtab, decl->name, &decl->base);
|
||||
} else if (node_type == SCC_AST_DECL_PARAM) {
|
||||
scc_sema_symtab_add_symbol(sema_symtab, decl->name, &decl->base);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -467,7 +467,7 @@ static void test_parser_unit(void) {
|
||||
scc_ast_translation_unit_init(&tu, &decls, scc_pos_create());
|
||||
SCC_CHECK_AST_WITH_SEMA(
|
||||
&tu.base,
|
||||
"typedef long long size_t;"
|
||||
"typedef int size_t;"
|
||||
"typedef void *(*func_t)(size_t a, int b, ...);",
|
||||
scc_parse_translation_unit);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user