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

@@ -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;
}