feat(ast): 重构AST转储功能并添加tree_dump依赖
- 添加tree_dump库作为依赖项到ast模块 - 将ast_dump.h中的转储上下文结构替换为tree_dump.h中的统一实现 - 更新scc_ast_block_item_vec_t类型定义,支持stmt或decl的泛型指针 - 移除ast_dump.c中重复的缩进和上下文管理代码,使用tree_dump提供的功能 - 修改dump函数参数类型从scc_ast_dump_ctx_t为scc_tree_dump_ctx_t - 在libs目录下新增tree_dump库,包含完整的树形结构转储功能
This commit is contained in:
@@ -5,17 +5,6 @@
|
||||
|
||||
#include <ast_dump.h>
|
||||
|
||||
#define VERTICAL "| "
|
||||
#define BRANCH "|-"
|
||||
#define LAST_BRANCH "`-"
|
||||
#define SPACE " "
|
||||
|
||||
// 默认颜色配置
|
||||
#define DEFAULT_NODE_COLOR ANSI_FG_BLUE
|
||||
#define DEFAULT_VALUE_COLOR ANSI_FG_GREEN
|
||||
#define DEFAULT_BRANCH_COLOR ANSI_FG_YELLOW
|
||||
#define DEFAULT_RESET_COLOR ANSI_NONE
|
||||
|
||||
// 通用宏定义
|
||||
#define PRINT_COLORED(ctx, color_field, fmt, ...) \
|
||||
do { \
|
||||
@@ -38,43 +27,6 @@
|
||||
PRINT_VALUE(ctx, "'%s'", value); \
|
||||
} while (0)
|
||||
|
||||
// 扩展上下文深度
|
||||
static void ensure_context_depth(scc_ast_dump_ctx_t *ctx, int new_depth) {
|
||||
if ((size_t)new_depth >= ctx->max_depth) {
|
||||
size_t old_size = ctx->max_depth * sizeof(cbool);
|
||||
ctx->max_depth = new_depth + 16; // 预分配更多空间
|
||||
ctx->is_last_child = (cbool *)scc_realloc(
|
||||
ctx->is_last_child, ctx->max_depth * sizeof(cbool));
|
||||
scc_memset((char *)ctx->is_last_child + old_size, 0,
|
||||
ctx->max_depth * sizeof(cbool) - old_size);
|
||||
}
|
||||
}
|
||||
|
||||
// 打印缩进
|
||||
static void print_indent(scc_ast_dump_ctx_t *ctx) {
|
||||
for (int i = 0; i < ctx->depth; i++) {
|
||||
if (i == ctx->depth - 1) {
|
||||
// 最后一层打印分支符号
|
||||
if (ctx->use_color) {
|
||||
scc_printf("%s%s%s", ctx->branch_color,
|
||||
ctx->is_last_child[i] ? LAST_BRANCH : BRANCH,
|
||||
ctx->reset_color);
|
||||
} else {
|
||||
scc_printf("%s", ctx->is_last_child[i] ? LAST_BRANCH : BRANCH);
|
||||
}
|
||||
} else {
|
||||
// 中间层根据是否是最后一个子节点决定是否打印垂直线
|
||||
if (ctx->use_color) {
|
||||
scc_printf("%s%s%s", ctx->branch_color,
|
||||
ctx->is_last_child[i] ? SPACE : VERTICAL,
|
||||
ctx->reset_color);
|
||||
} else {
|
||||
scc_printf("%s", ctx->is_last_child[i] ? SPACE : VERTICAL);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取节点类型的字符串表示
|
||||
static const char *get_node_type_str(scc_ast_node_type_t type) {
|
||||
switch (type) {
|
||||
@@ -313,27 +265,23 @@ static const char *get_op_str(scc_ast_expr_op_t op) {
|
||||
|
||||
// 通用的开始节点打印函数
|
||||
static inline void start_node_dump(scc_ast_node_t *node,
|
||||
scc_ast_dump_ctx_t *ctx) {
|
||||
print_indent(ctx);
|
||||
scc_tree_dump_ctx_t *ctx) {
|
||||
scc_tree_print_indent(ctx);
|
||||
PRINT_NODE_TYPE(ctx, node);
|
||||
}
|
||||
|
||||
// 通用的结束节点打印函数
|
||||
static inline void end_node_dump(scc_ast_dump_ctx_t *ctx) { scc_printf("\n"); }
|
||||
static inline void end_node_dump(scc_tree_dump_ctx_t *ctx) { scc_printf("\n"); }
|
||||
|
||||
// 通用的递归转储辅助函数
|
||||
static inline void dump_child_node(scc_ast_node_t *child,
|
||||
scc_ast_dump_ctx_t *ctx, cbool is_last) {
|
||||
scc_tree_dump_ctx_t *ctx, cbool is_last) {
|
||||
if (!child)
|
||||
return;
|
||||
|
||||
ctx->depth++;
|
||||
ensure_context_depth(ctx, ctx->depth);
|
||||
ctx->is_last_child[ctx->depth - 1] = is_last;
|
||||
|
||||
scc_ast_dump_node(child, ctx);
|
||||
|
||||
ctx->depth--;
|
||||
scc_vec_push(ctx->stack, is_last);
|
||||
scc_ast_dump_node(ctx, child);
|
||||
scc_vec_pop(ctx->stack);
|
||||
}
|
||||
|
||||
// 用于构建复合类型名称的宏
|
||||
@@ -348,7 +296,7 @@ static inline void dump_child_node(scc_ast_node_t *child,
|
||||
} while (0)
|
||||
|
||||
// 递归转储类型
|
||||
static void dump_type_impl(scc_ast_type_t *type, scc_ast_dump_ctx_t *ctx) {
|
||||
static void dump_type_impl(scc_ast_type_t *type, scc_tree_dump_ctx_t *ctx) {
|
||||
if (!type)
|
||||
return;
|
||||
|
||||
@@ -428,7 +376,7 @@ static void dump_type_impl(scc_ast_type_t *type, scc_ast_dump_ctx_t *ctx) {
|
||||
}
|
||||
|
||||
// 递归转储表达式
|
||||
static void dump_expr_impl(scc_ast_expr_t *expr, scc_ast_dump_ctx_t *ctx) {
|
||||
static void dump_expr_impl(scc_ast_expr_t *expr, scc_tree_dump_ctx_t *ctx) {
|
||||
if (!expr)
|
||||
return;
|
||||
|
||||
@@ -506,7 +454,7 @@ static void dump_expr_impl(scc_ast_expr_t *expr, scc_ast_dump_ctx_t *ctx) {
|
||||
case SCC_AST_EXPR_PTR_MEMBER:
|
||||
dump_child_node((scc_ast_node_t *)expr->member.base, ctx, false);
|
||||
// 打印成员访问信息
|
||||
print_indent(ctx);
|
||||
scc_tree_print_indent(ctx);
|
||||
PRINT_COLORED(ctx, node_color, "Member [\"%s\"]",
|
||||
expr->member.member_name);
|
||||
scc_printf("\n");
|
||||
@@ -543,7 +491,7 @@ static void dump_expr_impl(scc_ast_expr_t *expr, scc_ast_dump_ctx_t *ctx) {
|
||||
}
|
||||
|
||||
// 递归转储语句
|
||||
static void dump_stmt_impl(scc_ast_stmt_t *stmt, scc_ast_dump_ctx_t *ctx) {
|
||||
static void dump_stmt_impl(scc_ast_stmt_t *stmt, scc_tree_dump_ctx_t *ctx) {
|
||||
if (!stmt)
|
||||
return;
|
||||
|
||||
@@ -645,7 +593,7 @@ static void dump_stmt_impl(scc_ast_stmt_t *stmt, scc_ast_dump_ctx_t *ctx) {
|
||||
}
|
||||
|
||||
// 递归转储声明
|
||||
static void dump_decl_impl(scc_ast_decl_t *decl, scc_ast_dump_ctx_t *ctx) {
|
||||
static void dump_decl_impl(scc_ast_decl_t *decl, scc_tree_dump_ctx_t *ctx) {
|
||||
if (!decl)
|
||||
return;
|
||||
|
||||
@@ -752,7 +700,7 @@ static void dump_decl_impl(scc_ast_decl_t *decl, scc_ast_dump_ctx_t *ctx) {
|
||||
|
||||
// 递归转储翻译单元
|
||||
static void dump_unit_impl(scc_ast_translation_unit_t *unit,
|
||||
scc_ast_dump_ctx_t *ctx) {
|
||||
scc_tree_dump_ctx_t *ctx) {
|
||||
if (!unit)
|
||||
return;
|
||||
|
||||
@@ -765,27 +713,7 @@ static void dump_unit_impl(scc_ast_translation_unit_t *unit,
|
||||
}
|
||||
}
|
||||
|
||||
// 实现上下文管理函数
|
||||
void scc_ast_dump_ctx_init(scc_ast_dump_ctx_t *ctx, cbool use_color) {
|
||||
scc_memset(ctx, 0, sizeof(*ctx));
|
||||
ctx->use_color = use_color;
|
||||
ctx->node_color = use_color ? DEFAULT_NODE_COLOR : "";
|
||||
ctx->value_color = use_color ? DEFAULT_VALUE_COLOR : "";
|
||||
ctx->branch_color = use_color ? DEFAULT_BRANCH_COLOR : "";
|
||||
ctx->reset_color = use_color ? DEFAULT_RESET_COLOR : "";
|
||||
|
||||
ensure_context_depth(ctx, 0);
|
||||
ctx->is_last_child[0] = true;
|
||||
}
|
||||
|
||||
void scc_ast_dump_ctx_drop(scc_ast_dump_ctx_t *ctx) {
|
||||
if (ctx->is_last_child) {
|
||||
scc_free(ctx->is_last_child);
|
||||
ctx->is_last_child = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void scc_ast_dump_node(scc_ast_node_t *node, scc_ast_dump_ctx_t *ctx) {
|
||||
void scc_ast_dump_node(scc_tree_dump_ctx_t *ctx, const scc_ast_node_t *node) {
|
||||
if (!node)
|
||||
return;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user