feat(ast): 更新AST dump功能以使用新的树转储接口
- 将头文件中的tree_dump.h替换为scc_tree_dump.h - 修改函数签名将scc_tree_dump_ctx_t改为scc_tree_dump_t - 移除过时的宏定义和内联函数实现 - 使用新的scc_tree_dump_* API替代旧的PRINT_*宏 - 简化类型、表达式、语句和声明的转储逻辑 - 统一使用新的树转储接口进行节点和值的输出 feat(ast2ir): 实现逻辑运算符和一元运算符的IR转换 - 添加scc_ast2ir_logical_expr函数处理&&和||运算符 - 实现短路求值逻辑,包含分支控制流 - 添加对一元正号运算符的支持 - 实现取地址和间接寻址运算符 - 添加字符字面量解析支持转义序列 fix(ir): 修复字符串常量构建中的长度计算错误 - 修正数组长度计算从len+1改为len-1 - 调整字符串内容复制逻辑跳过引号边界 - 修正内存分配大小与实际数据长度匹配 refactor(ir): 更新IR转储模块使用统一的树转储接口 - 将IR转储上下文中的tree_dump_ctx_t替换为scc_tree_dump_t - 更新初始化函数签名以使用新的转储接口类型
This commit is contained in:
49
src/main.c
49
src/main.c
@@ -24,7 +24,7 @@ static void print_ring(scc_lexer_tok_ring_t *ring, int verbose) {
|
||||
} else if (verbose >= 1) {
|
||||
scc_printf(
|
||||
"token [%-8s] `%s` at %s:%d:%d\n", scc_get_tok_name(tok.type),
|
||||
tok.type != SCC_TOK_ENDLINE ? scc_cstring_as_cstr(&tok.lexeme)
|
||||
tok.type != SCC_TOK_ENDLINE ? scc_str_as_cstr(&tok.lexeme)
|
||||
: "\\n",
|
||||
tok.loc.name, tok.loc.line, tok.loc.col);
|
||||
}
|
||||
@@ -42,11 +42,11 @@ static void print_file(scc_lexer_tok_ring_t *ring, scc_file_t fp) {
|
||||
break;
|
||||
}
|
||||
if (fp == scc_stdout) {
|
||||
scc_printf("%s", scc_cstring_as_cstr(&tok.lexeme));
|
||||
scc_printf("%s", scc_str_as_cstr(&tok.lexeme));
|
||||
} else {
|
||||
usize ret = scc_fwrite(fp, scc_cstring_as_cstr(&tok.lexeme),
|
||||
scc_cstring_len(&tok.lexeme));
|
||||
if (ret != scc_cstring_len(&tok.lexeme)) {
|
||||
usize ret = scc_fwrite(fp, scc_str_as_cstr(&tok.lexeme),
|
||||
scc_str_len(&tok.lexeme));
|
||||
if (ret != scc_str_len(&tok.lexeme)) {
|
||||
LOG_FATAL("Failed to write to file");
|
||||
}
|
||||
}
|
||||
@@ -55,6 +55,10 @@ static void print_file(scc_lexer_tok_ring_t *ring, scc_file_t fp) {
|
||||
scc_fclose(fp);
|
||||
}
|
||||
|
||||
static void tree_dump_output(const char *str, usize len, void *user) {
|
||||
scc_fprintf(user, "%.*s", (int)len, str);
|
||||
}
|
||||
|
||||
void init_platform(void);
|
||||
|
||||
int main(int argc, const char **argv, const char **envp) {
|
||||
@@ -139,7 +143,7 @@ int main(int argc, const char **argv, const char **envp) {
|
||||
scc_lexer_tok_vec_t pproc_tok_vec;
|
||||
scc_vec_init(pproc_tok_vec);
|
||||
scc_lexer_tok_t tok = {
|
||||
.lexeme = scc_cstring_from_cstr("1"),
|
||||
.lexeme = scc_str_from_cstr("1"),
|
||||
.type = SCC_TOK_INT_LITERAL,
|
||||
.loc.name = "<internal>",
|
||||
.loc.line = 0,
|
||||
@@ -147,10 +151,10 @@ int main(int argc, const char **argv, const char **envp) {
|
||||
.loc.offset = 0,
|
||||
};
|
||||
scc_vec_push(pproc_tok_vec, tok);
|
||||
scc_cstring_t pproc_predefined_macros[] = {
|
||||
scc_cstring_from_cstr("__SCC__"),
|
||||
scc_cstring_from_cstr("_WIN64"),
|
||||
scc_cstring_from_cstr("__x86_64__"),
|
||||
scc_str_t pproc_predefined_macros[] = {
|
||||
scc_str_from_cstr("__SCC__"),
|
||||
scc_str_from_cstr("_WIN64"),
|
||||
scc_str_from_cstr("__x86_64__"),
|
||||
};
|
||||
for (usize i = 0; i < SCC_ARRLEN(pproc_predefined_macros); i += 1) {
|
||||
scc_vec_init(pproc_tok_vec);
|
||||
@@ -193,16 +197,16 @@ sstream_drop:
|
||||
}
|
||||
|
||||
if (config.emit_ast) {
|
||||
scc_tree_dump_ctx_t tree_dump;
|
||||
scc_tree_dump_t tree_dump;
|
||||
if (fp == null) {
|
||||
scc_tree_dump_ctx_init(&tree_dump, true, (void *)scc_fprintf,
|
||||
(void *)scc_stdout);
|
||||
scc_tree_dump_init(&tree_dump, true);
|
||||
} else {
|
||||
scc_tree_dump_ctx_init(&tree_dump, false, (void *)scc_fprintf,
|
||||
(void *)fp);
|
||||
scc_tree_dump_init(&tree_dump, false);
|
||||
}
|
||||
scc_ast_dump_node(&tree_dump, (scc_ast_node_t *)translation_unit);
|
||||
scc_tree_dump_ctx_drop(&tree_dump);
|
||||
scc_tree_dump_flush(&tree_dump, tree_dump_output,
|
||||
fp == null ? scc_stdout : fp);
|
||||
scc_tree_dump_drop(&tree_dump);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -216,18 +220,19 @@ sstream_drop:
|
||||
|
||||
if (config.emit_ir) {
|
||||
scc_ir_dump_ctx_t ir_dump_ctx;
|
||||
scc_tree_dump_ctx_t tree_dump;
|
||||
scc_tree_dump_t tree_dump;
|
||||
if (fp == null) {
|
||||
scc_tree_dump_ctx_init(&tree_dump, true, (void *)scc_fprintf,
|
||||
(void *)scc_stdout);
|
||||
scc_tree_dump_init(&tree_dump, true);
|
||||
} else {
|
||||
scc_tree_dump_ctx_init(&tree_dump, false, (void *)scc_fprintf,
|
||||
(void *)fp);
|
||||
scc_tree_dump_init(&tree_dump, false);
|
||||
}
|
||||
scc_ir_dump_ctx_init(&ir_dump_ctx, &tree_dump, &cprog);
|
||||
// scc_ir_dump_cprog(&ir_dump_ctx);
|
||||
scc_ir_dump_cprog_linear(&ir_dump_ctx);
|
||||
scc_tree_dump_ctx_drop(&tree_dump);
|
||||
|
||||
scc_tree_dump_flush(&tree_dump, tree_dump_output,
|
||||
fp == null ? scc_stdout : fp);
|
||||
scc_tree_dump_drop(&tree_dump);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user