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:
zzy
2026-04-03 20:10:51 +08:00
parent 78e7c800ba
commit ca187c78f1
42 changed files with 1264 additions and 1212 deletions

View File

@@ -185,7 +185,7 @@ typedef enum scc_tok_subtype {
*/
struct scc_lexer_token {
scc_tok_type_t type;
scc_cstring_t lexeme;
scc_str_t lexeme;
scc_pos_t loc;
};
@@ -199,7 +199,7 @@ static inline void scc_lexer_tok_drop(scc_lexer_tok_t *tok) {
tok->loc.line = 0;
tok->loc.name = null;
tok->loc.offset = 0;
scc_cstring_free(&tok->lexeme);
scc_str_drop(&tok->lexeme);
}
static inline cbool scc_lexer_tok_match(const scc_lexer_tok_t *tok,
@@ -211,7 +211,7 @@ static inline cbool scc_lexer_tok_match(const scc_lexer_tok_t *tok,
static inline scc_lexer_tok_t scc_lexer_tok_copy(const scc_lexer_tok_t *src) {
Assert(src != null);
scc_lexer_tok_t dst = *src;
dst.lexeme = scc_cstring_copy(&src->lexeme);
dst.lexeme = scc_str_copy(&src->lexeme);
return dst;
}

View File

@@ -6,13 +6,13 @@
static inline void scc_lexer_gen_number_true(scc_lexer_tok_t *tok) {
Assert(tok != null && tok->type == SCC_TOK_UNKNOWN);
tok->type = SCC_TOK_INT_LITERAL;
tok->lexeme = scc_cstring_from_cstr("1");
tok->lexeme = scc_str_from_cstr("1");
}
static inline void scc_lexer_gen_number_false(scc_lexer_tok_t *tok) {
Assert(tok != null && tok->type == SCC_TOK_UNKNOWN);
tok->type = SCC_TOK_INT_LITERAL;
tok->lexeme = scc_cstring_from_cstr("0");
tok->lexeme = scc_str_from_cstr("0");
}
static inline cbool scc_lexer_peek_non_blank(scc_lexer_tok_ring_t *stream,

View File

@@ -74,13 +74,13 @@ static inline cbool peek_char(scc_lexer_t *lexer, scc_sstream_char_t *out) {
}
/* 从环形缓冲区消费一个字符并将它追加到lexeme中 */
static inline cbool next_char(scc_lexer_t *lexer, scc_cstring_t *lexeme,
static inline cbool next_char(scc_lexer_t *lexer, scc_str_t *lexeme,
scc_sstream_char_t *out) {
cbool ok;
scc_ring_next(*lexer->stream_ref, *out, ok);
if (!ok)
return false;
scc_cstring_append_ch(lexeme, out->character);
scc_str_append_ch(lexeme, out->character);
return true;
}
@@ -88,7 +88,7 @@ static inline cbool next_char(scc_lexer_t *lexer, scc_cstring_t *lexeme,
void scc_lexer_get_token(scc_lexer_t *lexer, scc_lexer_tok_t *token) {
scc_sstream_char_t cur = {0};
scc_cstring_t lex = scc_cstring_create(); // 临时lexeme
scc_str_t lex = scc_str_empty(); // 临时lexeme
// 尝试预览第一个字符
if (!peek_char(lexer, &cur)) {
@@ -165,7 +165,7 @@ void scc_lexer_get_token(scc_lexer_t *lexer, scc_lexer_tok_t *token) {
scc_ring_consume(*lexer->stream_ref);
}
// 检查是否为关键字
int idx = keyword_cmp(scc_cstring_as_cstr(&lex), scc_cstring_len(&lex));
int idx = keyword_cmp(scc_str_as_cstr(&lex), scc_str_len(&lex));
if (idx != -1) {
token->type = keywords[idx].tok_type;
}
@@ -478,8 +478,8 @@ void scc_lexer_get_token(scc_lexer_t *lexer, scc_lexer_tok_t *token) {
token->loc = start_loc;
token->lexeme = lex; // 转移所有权
LEX_DEBUG("get token `%s` (%s) at %s:%d:%d", scc_get_tok_name(token->type),
scc_cstring_as_cstr(&token->lexeme), token->loc.name,
token->loc.line, token->loc.col);
scc_str_as_cstr(&token->lexeme), token->loc.name, token->loc.line,
token->loc.col);
}
// scc_lexer_get_token maybe got invalid (with parser)

View File

@@ -58,10 +58,9 @@ int main(int argc, char *argv[]) {
}
LOG_INFO("get token [%-8s] `%s` at %s:%d:%d",
scc_get_tok_name(token.type),
scc_cstring_as_cstr(&token.lexeme), token.loc.name,
token.loc.line, token.loc.col);
scc_cstring_free(&token.lexeme);
scc_get_tok_name(token.type), scc_str_as_cstr(&token.lexeme),
token.loc.name, token.loc.line, token.loc.col);
scc_str_drop(&token.lexeme);
}
scc_sstream_drop_ring(ref);
scc_sstream_drop(&stream);

View File

@@ -4,7 +4,7 @@
#include <utest/acutest.h>
// 辅助函数:释放 token 的 lexeme
static void free_token(scc_lexer_tok_t *tok) { scc_cstring_free(&tok->lexeme); }
static void free_token(scc_lexer_tok_t *tok) { scc_str_drop(&tok->lexeme); }
// 单 token 测试宏(检查类型)
#define TEST_TOKEN(input, expected_type) \
@@ -18,12 +18,11 @@ static void free_token(scc_lexer_tok_t *tok) { scc_cstring_free(&tok->lexeme); }
scc_lexer_get_token(&lexer, &token); \
\
TEST_CHECK(token.type == expected_type && \
scc_strcmp(input, scc_cstring_as_cstr(&token.lexeme)) == \
0); \
scc_strcmp(input, scc_str_as_cstr(&token.lexeme)) == 0); \
TEST_MSG("Input: '%s'", input); \
TEST_MSG("Expected: %s `%s`", scc_get_tok_name(expected_type), input); \
TEST_MSG("Got: %s `%s`", scc_get_tok_name(token.type), \
scc_cstring_as_cstr(&token.lexeme)); \
scc_str_as_cstr(&token.lexeme)); \
\
free_token(&token); \
scc_sstream_drop_ring(ref); \