feat(ast): 添加内置表达式支持并完善解析器功能
- 添加SCC_AST_EXPR_BUILTIN枚举值用于表示内置表达式 - 定义scc_ast_builtin_expr_type_t枚举包含va_start、va_end、va_copy、va_arg等内置函数类型 - 在AST表达式结构中添加builtin字段以支持内置表达式存储 - 实现scc_ast_decl_unsafe_val_init内联函数用于不安全的声明初始化 - 修改sizeof和alignof表达式初始化函数以支持类型或表达式参数 - 修复默认语句的字段引用错误(default_stmt而非case_stmt) - 改进词法分析器对整数字面量后缀(U、L、LL等)的处理逻辑 - 重构解析器中的声明解析逻辑,统一使用scc_parse_declarator函数 - 完善结构体、联合体和枚举类型的声明解析,支持仅有名称的前向声明 - 优化初始化列表解析,支持复合字面量的成员访问语法 - 更新参数类型列表解析以正确处理参数声明 - 修复括号表达式的解析逻辑,区分类型转换和普通括号表达式
This commit is contained in:
@@ -52,6 +52,7 @@ typedef enum {
|
||||
SCC_AST_EXPR_ALIGN_OF, // _Alignof
|
||||
SCC_AST_EXPR_COMPOUND, // 复合字面量
|
||||
SCC_AST_EXPR_LVALUE, // 右值
|
||||
SCC_AST_EXPR_BUILTIN, // 内置表达式
|
||||
// 字面量
|
||||
SCC_AST_EXPR_INT_LITERAL, // 整数字面量
|
||||
SCC_AST_EXPR_FLOAT_LITERAL, // 浮点字面量
|
||||
@@ -258,6 +259,13 @@ typedef enum scc_ast_expr_op {
|
||||
SCC_AST_OP_PTR_MEMBER_ACCESS, // ->
|
||||
} scc_ast_expr_op_t;
|
||||
|
||||
typedef enum {
|
||||
SCC_AST_EXPR_BUILTIN_VA_START,
|
||||
SCC_AST_EXPR_BUILTIN_VA_END,
|
||||
SCC_AST_EXPR_BUILTIN_VA_COPY,
|
||||
SCC_AST_EXPR_BUILTIN_VA_ARG,
|
||||
} scc_ast_builtin_expr_type_t;
|
||||
|
||||
/**
|
||||
* @brief 表达式节点
|
||||
*/
|
||||
@@ -328,6 +336,11 @@ struct scc_ast_expr {
|
||||
struct {
|
||||
scc_ast_type_t *type;
|
||||
} lvalue;
|
||||
// 内置表达式
|
||||
struct {
|
||||
scc_ast_builtin_expr_type_t type;
|
||||
scc_ast_expr_vec_t args;
|
||||
} builtin;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -20,6 +20,19 @@ scc_ast_translation_unit_init(scc_ast_translation_unit_t *translation_unit,
|
||||
}
|
||||
}
|
||||
|
||||
// name and var_init can be null
|
||||
static inline void scc_ast_decl_unsafe_val_init(scc_ast_decl_t *decl,
|
||||
scc_ast_type_t *type,
|
||||
const char *name,
|
||||
scc_ast_expr_t *var_init) {
|
||||
Assert(decl != null && type != null);
|
||||
decl->base.loc = scc_pos_create();
|
||||
decl->base.type = SCC_AST_DECL_VAR;
|
||||
decl->name = name;
|
||||
decl->var.type = type;
|
||||
decl->var.init = var_init;
|
||||
}
|
||||
|
||||
// var_init can be null
|
||||
static inline void scc_ast_decl_val_init(scc_ast_decl_t *decl,
|
||||
scc_ast_type_t *type, const char *name,
|
||||
@@ -212,7 +225,7 @@ static inline void scc_ast_stmt_default_init(scc_ast_stmt_t *stmt,
|
||||
Assert(stmt != null && body != null);
|
||||
stmt->base.loc = scc_pos_create();
|
||||
stmt->base.type = SCC_AST_STMT_DEFAULT;
|
||||
stmt->case_stmt.stmt = body;
|
||||
stmt->default_stmt.stmt = body;
|
||||
}
|
||||
|
||||
static inline void scc_ast_stmt_break_init(scc_ast_stmt_t *stmt) {
|
||||
@@ -351,22 +364,26 @@ static inline void scc_ast_expr_cast_init(scc_ast_expr_t *expr,
|
||||
expr->cast.expr = operand;
|
||||
}
|
||||
|
||||
// type and target_expr can be null but it only one of them can be null
|
||||
static inline void scc_ast_expr_sizeof_init(scc_ast_expr_t *expr,
|
||||
scc_ast_type_t *type) {
|
||||
Assert(expr != null && type != null);
|
||||
scc_ast_type_t *type,
|
||||
scc_ast_expr_t *target_expr) {
|
||||
Assert(expr != null);
|
||||
expr->base.loc = scc_pos_create();
|
||||
expr->base.type = SCC_AST_EXPR_SIZE_OF;
|
||||
expr->attr_of.type = type;
|
||||
expr->attr_of.expr = null;
|
||||
expr->attr_of.expr = target_expr;
|
||||
}
|
||||
|
||||
// type and target_expr can be null but it only one of them can be null
|
||||
static inline void scc_ast_expr_alignof_init(scc_ast_expr_t *expr,
|
||||
scc_ast_type_t *type) {
|
||||
Assert(expr != null && type != null);
|
||||
scc_ast_type_t *type,
|
||||
scc_ast_expr_t *target_expr) {
|
||||
Assert(expr != null);
|
||||
expr->base.loc = scc_pos_create();
|
||||
expr->base.type = SCC_AST_EXPR_SIZE_OF;
|
||||
expr->attr_of.type = type;
|
||||
expr->attr_of.expr = null;
|
||||
expr->attr_of.expr = target_expr;
|
||||
}
|
||||
|
||||
// lhs_exprs and rhs_exprs can be null
|
||||
|
||||
@@ -484,6 +484,10 @@ static void dump_expr_impl(scc_ast_expr_t *expr, scc_tree_dump_ctx_t *ctx) {
|
||||
}
|
||||
break;
|
||||
|
||||
case SCC_AST_EXPR_BUILTIN:
|
||||
// PRINT_QUOTED_VALUE(ctx, scc_ast_builtin_type_name(expr->builtin));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user