feat(ast): 添加LVALUE表达式类型支持并重构表达式创建逻辑

- 新增SCC_AST_EXPR_LVALUE表达式类型用于表示右值
- 重构表达式创建逻辑,移除旧的通用创建函数expr_create
- 使用新的初始化函数替代原有的表达式创建方式
- 更新AST转储功能以支持LVALUE表达式的输出
- 修改sizeof表达式解析逻辑,修复类型转换处理
- 优化各种表达式类型的解析和初始化过程
This commit is contained in:
zzy
2026-03-12 21:58:00 +08:00
parent c46578736a
commit 2d1032c363
4 changed files with 95 additions and 136 deletions

View File

@@ -51,6 +51,7 @@ typedef enum {
SCC_AST_EXPR_SIZE_OF, // sizeof
SCC_AST_EXPR_ALIGN_OF, // _Alignof
SCC_AST_EXPR_COMPOUND, // 复合字面量
SCC_AST_EXPR_LVALUE, // 右值
// 字面量
SCC_AST_EXPR_INT_LITERAL, // 整数字面量
SCC_AST_EXPR_FLOAT_LITERAL, // 浮点字面量

View File

@@ -439,7 +439,7 @@ static inline void scc_ast_expr_lvalue_init(scc_ast_expr_t *expr,
scc_ast_type_t *type) {
Assert(expr != null && type != null);
expr->base.loc = scc_pos_create();
expr->base.type = SCC_AST_EXPR_IDENTIFIER;
expr->base.type = SCC_AST_EXPR_LVALUE;
expr->lvalue.type = type;
}

View File

@@ -53,6 +53,7 @@ static const char *node_type_names[] = {
[SCC_AST_EXPR_SIZE_OF] = "SizeofExpr",
[SCC_AST_EXPR_ALIGN_OF] = "AlignofExpr",
[SCC_AST_EXPR_COMPOUND] = "CompoundExpr",
[SCC_AST_EXPR_LVALUE] = "LvalueExpr",
[SCC_AST_EXPR_INT_LITERAL] = "IntLiteralExpr",
[SCC_AST_EXPR_FLOAT_LITERAL] = "FloatLiteralExpr",
[SCC_AST_EXPR_CHAR_LITERAL] = "CharLiteralExpr",
@@ -461,15 +462,26 @@ static void dump_expr_impl(scc_ast_expr_t *expr, scc_tree_dump_ctx_t *ctx) {
}
break;
case SCC_AST_EXPR_COMPOUND:
// dump_child_node((scc_ast_node_t *)expr->compound_literal.type, ctx,
// false);
// // 初始化列表
// for (size_t i = 0; i < expr->compound_literal.init_list.size; i++) {
// dump_child_node(
// (scc_ast_node_t *)expr->compound_literal.init_list.data[i],
// ctx, i == expr->compound_literal.init_list.size - 1);
// }
case SCC_AST_EXPR_LVALUE:
dump_child_node((scc_ast_node_t *)expr->lvalue.type, ctx, true);
break;
case SCC_AST_EXPR_COMPOUND:;
dump_child_node((scc_ast_node_t *)expr->compound.base, ctx, false);
if (scc_vec_size(expr->compound.lhs_exprs) !=
scc_vec_size(expr->compound.rhs_exprs)) {
LOG_ERROR("compound expr lhs and rhs size not equal");
break;
}
usize size = scc_vec_size(expr->compound.lhs_exprs);
for (usize i = 0; i < size; i++) {
dump_child_node(
(scc_ast_node_t *)scc_vec_at(expr->compound.lhs_exprs, i), ctx,
false);
dump_child_node(
(scc_ast_node_t *)scc_vec_at(expr->compound.rhs_exprs, i), ctx,
i + 1 == size);
}
break;
default: