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:
zzy
2026-01-28 17:29:56 +08:00
parent 79ee7a657a
commit 135e874a76
8 changed files with 135 additions and 108 deletions

View File

@@ -4,6 +4,6 @@ version = "0.1.0"
authors = [] authors = []
description = "" description = ""
# dependencies = [] dependencies = [{ name = "tree_dump", path = "../tree_dump" }]
# features = {} # features = {}
# default_features = [] # default_features = []

View File

@@ -138,8 +138,8 @@ typedef SCC_VEC(scc_ast_expr_t *) scc_ast_expr_vec_t;
typedef SCC_VEC(scc_ast_stmt_t *) scc_ast_stmt_vec_t; typedef SCC_VEC(scc_ast_stmt_t *) scc_ast_stmt_vec_t;
typedef SCC_VEC(scc_ast_decl_t *) scc_ast_decl_vec_t; typedef SCC_VEC(scc_ast_decl_t *) scc_ast_decl_vec_t;
// 通过指针实现泛型 // 通过指针实现泛型 only stmt or decl
typedef SCC_VEC(scc_ast_node_type_t *) scc_ast_block_item_vec_t; typedef SCC_VEC(scc_ast_node_t *) scc_ast_block_item_vec_t;
/** /**
* @brief 类型表示 * @brief 类型表示

View File

@@ -7,21 +7,7 @@
#define __SCC_AST_DUMP_H__ #define __SCC_AST_DUMP_H__
#include "ast_def.h" #include "ast_def.h"
#include <tree_dump.h>
typedef SCC_VEC(u8) scc_ast_dump_stack_t;
/**
* @brief AST dump 上下文结构
*/
typedef struct {
int depth; ///< 当前深度
cbool *is_last_child; ///< 每层是否为最后子节点
cbool use_color; ///< 是否使用颜色输出
size_t max_depth; ///< 分配的最大深度
const char *node_color; ///< 节点类型颜色
const char *value_color; ///< 值颜色
const char *branch_color; ///< 分支符号颜色
const char *reset_color; ///< 重置颜色
} scc_ast_dump_ctx_t;
/** /**
* @brief 以指定格式 dump AST * @brief 以指定格式 dump AST
@@ -29,9 +15,6 @@ typedef struct {
* @param node AST 节点(可以是任意类型的节点) * @param node AST 节点(可以是任意类型的节点)
* @param ctx dump 上下文 * @param ctx dump 上下文
*/ */
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);
void scc_ast_dump_ctx_init(scc_ast_dump_ctx_t *ctx, cbool use_color);
void scc_ast_dump_ctx_drop(scc_ast_dump_ctx_t *ctx);
#endif /* __SCC_AST_DUMP_H__ */ #endif /* __SCC_AST_DUMP_H__ */

View File

@@ -5,17 +5,6 @@
#include <ast_dump.h> #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, ...) \ #define PRINT_COLORED(ctx, color_field, fmt, ...) \
do { \ do { \
@@ -38,43 +27,6 @@
PRINT_VALUE(ctx, "'%s'", value); \ PRINT_VALUE(ctx, "'%s'", value); \
} while (0) } 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) { static const char *get_node_type_str(scc_ast_node_type_t type) {
switch (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, static inline void start_node_dump(scc_ast_node_t *node,
scc_ast_dump_ctx_t *ctx) { scc_tree_dump_ctx_t *ctx) {
print_indent(ctx); scc_tree_print_indent(ctx);
PRINT_NODE_TYPE(ctx, node); 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, 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) if (!child)
return; return;
ctx->depth++; scc_vec_push(ctx->stack, is_last);
ensure_context_depth(ctx, ctx->depth); scc_ast_dump_node(ctx, child);
ctx->is_last_child[ctx->depth - 1] = is_last; scc_vec_pop(ctx->stack);
scc_ast_dump_node(child, ctx);
ctx->depth--;
} }
// 用于构建复合类型名称的宏 // 用于构建复合类型名称的宏
@@ -348,7 +296,7 @@ static inline void dump_child_node(scc_ast_node_t *child,
} while (0) } 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) if (!type)
return; 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) if (!expr)
return; 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: case SCC_AST_EXPR_PTR_MEMBER:
dump_child_node((scc_ast_node_t *)expr->member.base, ctx, false); 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\"]", PRINT_COLORED(ctx, node_color, "Member [\"%s\"]",
expr->member.member_name); expr->member.member_name);
scc_printf("\n"); 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) if (!stmt)
return; 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) if (!decl)
return; 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, 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) if (!unit)
return; return;
@@ -765,27 +713,7 @@ static void dump_unit_impl(scc_ast_translation_unit_t *unit,
} }
} }
// 实现上下文管理函数 void scc_ast_dump_node(scc_tree_dump_ctx_t *ctx, const scc_ast_node_t *node) {
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) {
if (!node) if (!node)
return; return;

View File

@@ -0,0 +1,9 @@
[package]
name = "tree_dump"
version = "0.1.0"
authors = []
description = ""
# dependencies = []
# features = {}
# default_features = []

View File

@@ -0,0 +1,91 @@
#ifndef __SCC_TREE_DUMP_H__
#define __SCC_TREE_DUMP_H__
#include <scc_core.h>
#define SCC_TREE_DUMP_VERTICAL "| "
#define SCC_TREE_DUMP_BRANCH "|-"
#define SCC_TREE_DUMP_LAST_BRANCH "`-"
#define SCC_TREE_DUMP_SPACE " "
#define SCC_TREE_DUMP_NODE_COLOR ANSI_FG_BLUE
#define SCC_TREE_DUMP_VALUE_COLOR ANSI_FG_GREEN
#define SCC_TREE_DUMP_BRANCH_COLOR ANSI_FG_YELLOW
#define SCC_TREE_DUMP_RESET_COLOR ANSI_NONE
// #define ANSI_FMT
typedef SCC_VEC(cbool) scc_ast_dump_stack_t;
typedef struct {
scc_ast_dump_stack_t stack; ///< 每层是否为最后子节点
cbool use_color; ///< 是否使用颜色输出
const char *vertical;
const char *branch;
const char *last_branch;
const char *space;
const char *node_color; ///< 节点类型颜色
const char *value_color; ///< 值颜色
const char *branch_color; ///< 分支符号颜色
const char *reset_color; ///< 重置颜色
} scc_tree_dump_ctx_t;
static inline void scc_tree_dump_ctx_init(scc_tree_dump_ctx_t *ctx,
cbool use_color) {
ctx->use_color = use_color;
scc_vec_init(ctx->stack);
ctx->vertical = SCC_TREE_DUMP_VERTICAL;
ctx->branch = SCC_TREE_DUMP_BRANCH;
ctx->last_branch = SCC_TREE_DUMP_LAST_BRANCH;
ctx->space = SCC_TREE_DUMP_SPACE;
ctx->node_color = use_color ? SCC_TREE_DUMP_NODE_COLOR : "";
ctx->value_color = use_color ? SCC_TREE_DUMP_VALUE_COLOR : "";
ctx->branch_color = use_color ? SCC_TREE_DUMP_BRANCH_COLOR : "";
ctx->reset_color = use_color ? SCC_TREE_DUMP_RESET_COLOR : "";
}
static inline void scc_tree_dump_ctx_drop(scc_tree_dump_ctx_t *ctx) {
scc_vec_free(ctx->stack);
}
// 打印缩进
static void scc_tree_print_indent(scc_tree_dump_ctx_t *ctx) {
scc_vec_foreach(ctx->stack, i) {
cbool last_child = scc_vec_at(ctx->stack, i);
const char *data = null;
if (i + 1 == scc_vec_size(ctx->stack)) {
// 最后一层打印分支符号
data = last_child ? ctx->last_branch : ctx->branch;
} else {
data = last_child ? ctx->space : ctx->vertical;
}
Assert(data != null);
if (ctx->use_color) {
scc_printf("%s%s%s", ctx->branch_color, data, ctx->reset_color);
} else {
scc_printf("%s", data);
}
}
}
// 推入新的层级到栈中
static inline void scc_tree_dump_push_level(scc_tree_dump_ctx_t *ctx,
cbool is_last_child) {
scc_vec_push(ctx->stack, is_last_child);
}
// 弹出当前层级
static inline void scc_tree_dump_pop_level(scc_tree_dump_ctx_t *ctx) {
scc_vec_pop(ctx->stack);
}
// 获取当前层级深度
static inline usize scc_tree_dump_depth(scc_tree_dump_ctx_t *ctx) {
return scc_vec_size(ctx->stack);
}
#endif /* __SCC_TREE_DUMP_H__ */

View File

@@ -0,0 +1,6 @@
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}

View File

@@ -0,0 +1,10 @@
#include <stdio.h>
void test_example() {
printf("Test passed!\n");
}
int main() {
test_example();
return 0;
}