feat(ast): 添加复合表达式和初始化器解析支持
重构AST表达式枚举,将COMPOUND_LITERAL重命名为COMPOUND, 更新相关结构体定义以支持复合字面量的左右表达式向量表示。 添加lvalue表达式类型用于左值处理,实现完整的初始化器解析功能, 包括大括号初始化列表、成员访问和数组下标的处理逻辑。 更新表达式解析器为基于优先级的递归下降解析,修复变量声明中 初始化表达式的内存泄漏问题。 完善类型限定符和存储类说明符的重复检查机制,增强语法分析的准确性。
This commit is contained in:
@@ -39,18 +39,18 @@ typedef enum {
|
||||
scc_ast_stmt_t_END, // 结束语句
|
||||
|
||||
// 表达式
|
||||
scc_ast_expr_t_BEGIN, // 表达式开始
|
||||
SCC_AST_EXPR_BINARY, // 二元运算
|
||||
SCC_AST_EXPR_UNARY, // 一元运算
|
||||
SCC_AST_EXPR_COND, // 条件表达式 ?:
|
||||
SCC_AST_EXPR_CALL, // 函数调用
|
||||
SCC_AST_EXPR_ARRAY_SUBSCRIPT, // 数组下标
|
||||
SCC_AST_EXPR_MEMBER, // 成员访问 .
|
||||
SCC_AST_EXPR_PTR_MEMBER, // 指针成员访问 ->
|
||||
SCC_AST_EXPR_CAST, // 类型转换
|
||||
SCC_AST_EXPR_SIZE_OF, // sizeof
|
||||
SCC_AST_EXPR_ALIGN_OF, // _Alignof
|
||||
SCC_AST_EXPR_COMPOUND_LITERAL, // 复合字面量
|
||||
scc_ast_expr_t_BEGIN, // 表达式开始
|
||||
SCC_AST_EXPR_BINARY, // 二元运算
|
||||
SCC_AST_EXPR_UNARY, // 一元运算
|
||||
SCC_AST_EXPR_COND, // 条件表达式 ?:
|
||||
SCC_AST_EXPR_CALL, // 函数调用
|
||||
SCC_AST_EXPR_ARRAY_SUBSCRIPT, // 数组下标
|
||||
SCC_AST_EXPR_MEMBER, // 成员访问 .
|
||||
SCC_AST_EXPR_PTR_MEMBER, // 指针成员访问 ->
|
||||
SCC_AST_EXPR_CAST, // 类型转换
|
||||
SCC_AST_EXPR_SIZE_OF, // sizeof
|
||||
SCC_AST_EXPR_ALIGN_OF, // _Alignof
|
||||
SCC_AST_EXPR_COMPOUND, // 复合字面量
|
||||
// 字面量
|
||||
SCC_AST_EXPR_INT_LITERAL, // 整数字面量
|
||||
SCC_AST_EXPR_FLOAT_LITERAL, // 浮点字面量
|
||||
@@ -310,9 +310,10 @@ struct scc_ast_expr {
|
||||
} attr_of;
|
||||
// 复合字面量
|
||||
struct {
|
||||
scc_ast_type_t *type;
|
||||
scc_ast_expr_vec_t init_list;
|
||||
} compound_literal;
|
||||
scc_ast_expr_t *base;
|
||||
scc_ast_expr_vec_t lhs_exprs;
|
||||
scc_ast_expr_vec_t rhs_exprs;
|
||||
} compound;
|
||||
// 字面量
|
||||
struct {
|
||||
const char *lexme;
|
||||
@@ -323,6 +324,9 @@ struct scc_ast_expr {
|
||||
const char *name;
|
||||
scc_ast_decl_t *_target;
|
||||
} identifier;
|
||||
struct {
|
||||
scc_ast_type_t *type;
|
||||
} lvalue;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -369,19 +369,28 @@ static inline void scc_ast_expr_alignof_init(scc_ast_expr_t *expr,
|
||||
expr->attr_of.expr = null;
|
||||
}
|
||||
|
||||
// init can be null
|
||||
static inline void
|
||||
scc_ast_expr_compound_literal_init(scc_ast_expr_t *expr, scc_ast_type_t *type,
|
||||
scc_ast_expr_vec_t *init) {
|
||||
Assert(expr != null && type != null);
|
||||
// lhs_exprs and rhs_exprs can be null
|
||||
static inline void scc_ast_expr_compound_init(scc_ast_expr_t *expr,
|
||||
scc_ast_expr_t *base,
|
||||
scc_ast_expr_vec_t *lhs_exprs,
|
||||
scc_ast_expr_vec_t *rhs_exprs) {
|
||||
Assert(expr != null && base != null);
|
||||
expr->base.loc = scc_pos_create();
|
||||
expr->base.type = SCC_AST_EXPR_COMPOUND_LITERAL;
|
||||
expr->compound_literal.type = type;
|
||||
if (init == null) {
|
||||
scc_vec_init(expr->compound_literal.init_list);
|
||||
expr->base.type = SCC_AST_EXPR_COMPOUND;
|
||||
expr->compound.base = base;
|
||||
|
||||
if (lhs_exprs == null) {
|
||||
scc_vec_init(expr->compound.lhs_exprs);
|
||||
} else {
|
||||
expr->compound_literal.init_list = *init;
|
||||
scc_vec_init(*init);
|
||||
expr->compound.lhs_exprs = *lhs_exprs;
|
||||
scc_vec_init(*lhs_exprs);
|
||||
}
|
||||
|
||||
if (rhs_exprs == null) {
|
||||
scc_vec_init(expr->compound.rhs_exprs);
|
||||
} else {
|
||||
expr->compound.rhs_exprs = *rhs_exprs;
|
||||
scc_vec_init(*rhs_exprs);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -426,6 +435,14 @@ static inline void scc_ast_expr_identifier_init(scc_ast_expr_t *expr,
|
||||
expr->identifier._target = null;
|
||||
}
|
||||
|
||||
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->lvalue.type = type;
|
||||
}
|
||||
|
||||
// have defined builtin type
|
||||
static inline void _scc_ast_type_builtin_init(scc_ast_type_t *type,
|
||||
scc_ast_builtin_type_t builtin) {
|
||||
|
||||
@@ -52,7 +52,7 @@ static const char *node_type_names[] = {
|
||||
[SCC_AST_EXPR_CAST] = "CastExpr",
|
||||
[SCC_AST_EXPR_SIZE_OF] = "SizeofExpr",
|
||||
[SCC_AST_EXPR_ALIGN_OF] = "AlignofExpr",
|
||||
[SCC_AST_EXPR_COMPOUND_LITERAL] = "CompoundLiteralExpr",
|
||||
[SCC_AST_EXPR_COMPOUND] = "CompoundExpr",
|
||||
[SCC_AST_EXPR_INT_LITERAL] = "IntLiteralExpr",
|
||||
[SCC_AST_EXPR_FLOAT_LITERAL] = "FloatLiteralExpr",
|
||||
[SCC_AST_EXPR_CHAR_LITERAL] = "CharLiteralExpr",
|
||||
@@ -461,15 +461,15 @@ static void dump_expr_impl(scc_ast_expr_t *expr, scc_tree_dump_ctx_t *ctx) {
|
||||
}
|
||||
break;
|
||||
|
||||
case SCC_AST_EXPR_COMPOUND_LITERAL:
|
||||
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_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);
|
||||
// }
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user