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

@@ -10,22 +10,22 @@
* @brief 动态字符串结构体
* @attention 创建的字符串对象需要使用 scc_cstring_free 释放
*/
typedef struct scc_cstring {
typedef struct scc_str {
usize size; /**< 字符串当前大小(包括结尾的'\0'*/
usize cap; /**< 分配的容量 */
char *data; /**< 实际存储数据的指针 */
} scc_cstring_t;
} scc_str_t;
/**
* @brief 创建一个新的空动态字符串对象
*
* @return cstring_t 初始化后的对象
*/
static inline scc_cstring_t scc_cstring_create(void) {
return (scc_cstring_t){.data = null, .size = 0, .cap = 0};
static inline scc_str_t scc_str_empty(void) {
return (scc_str_t){.data = null, .size = 0, .cap = 0};
}
static inline void scc_cstring_init(scc_cstring_t *string) {
static inline void scc_str_init(scc_str_t *string) {
Assert(string != null);
string->data = null;
string->size = 0;
@@ -38,9 +38,9 @@ static inline void scc_cstring_init(scc_cstring_t *string) {
* @param s 输入的 C 风格字符串
* @return cstring_t 新建对象,包含输入字符串的副本
*/
static inline scc_cstring_t scc_cstring_from_cstr(const char *s) {
static inline scc_str_t scc_str_from_cstr(const char *s) {
if (s == null) {
return scc_cstring_create();
return scc_str_empty();
}
usize len = 0;
@@ -53,11 +53,11 @@ static inline scc_cstring_t scc_cstring_from_cstr(const char *s) {
scc_memcpy(data, s, len);
data[len] = '\0';
return (scc_cstring_t){.size = len + 1, .cap = len + 1, .data = data};
return (scc_str_t){.size = len + 1, .cap = len + 1, .data = data};
}
static inline scc_cstring_t scc_cstring_copy(const scc_cstring_t *s) {
return scc_cstring_from_cstr(s->data);
static inline scc_str_t scc_str_copy(const scc_str_t *s) {
return scc_str_from_cstr(s->data);
}
/**
@@ -65,7 +65,7 @@ static inline scc_cstring_t scc_cstring_copy(const scc_cstring_t *s) {
*
* @param str 要被释放的字符串指针
*/
static inline void scc_cstring_free(scc_cstring_t *str) {
static inline void scc_str_drop(scc_str_t *str) {
if (str == null) {
return;
}
@@ -84,8 +84,8 @@ static inline void scc_cstring_free(scc_cstring_t *str) {
* @param data 要追加的 C 字符串指针
* @param len 要追加的 C 字符串长度
*/
static inline void scc_cstring_append_cstr(scc_cstring_t *str, const char *data,
usize len) {
static inline void scc_str_append_cstr(scc_str_t *str, const char *data,
usize len) {
if (str == null || data == null || len == 0) {
return;
}
@@ -127,19 +127,48 @@ static inline void scc_cstring_append_cstr(scc_cstring_t *str, const char *data,
* @param str 目标动态字符串指针
* @param other 要追加的动态字符串指针
*/
static inline void scc_cstring_append(scc_cstring_t *str,
const scc_cstring_t *other) {
scc_cstring_append_cstr(str, other->data, other->size - 1);
static inline void scc_str_append(scc_str_t *str, const scc_str_t *other) {
scc_str_append_cstr(str, other->data, other->size - 1);
}
#ifndef __SCC_PURE_LIB__
#include "scc_core_impl.h"
static inline void scc_str_append_fmt(scc_str_t *str, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
usize needed = scc_vsnprintf(null, 0, fmt, args);
va_end(args);
if (needed == 0)
return;
// 确保容量足够(需要额外空间给 '\0'
if (str->size + needed + 1 > str->cap) {
usize new_cap = str->cap;
while (new_cap < str->size + needed + 1) {
new_cap = new_cap ? new_cap * 2 : (needed + 16);
}
char *new_data = (char *)scc_realloc(str->data, new_cap);
Assert(new_data != NULL);
str->data = new_data;
str->cap = new_cap;
}
va_start(args, fmt);
scc_vsnprintf(str->data + str->size - 1, needed + 1, fmt, args);
va_end(args);
str->size += needed;
// 确保结尾 '\0'scc_vsnprintf 已添加)
}
#endif
/**
* @brief 向动态字符串末尾追加一个字符
*
* @param str 目标动态字符串指针
* @param ch 要追加的字符
*/
static inline void scc_cstring_append_ch(scc_cstring_t *str, char ch) {
scc_cstring_append_cstr(str, &ch, 1);
static inline void scc_str_append_ch(scc_str_t *str, char ch) {
scc_str_append_cstr(str, &ch, 1);
}
/**
@@ -148,7 +177,7 @@ static inline void scc_cstring_append_ch(scc_cstring_t *str, char ch) {
* @param str 动态字符串指针
* @return usize 字符串实际长度
*/
static inline usize scc_cstring_len(const scc_cstring_t *str) {
static inline usize scc_str_len(const scc_str_t *str) {
if (str == null) {
return 0;
}
@@ -164,7 +193,7 @@ static inline usize scc_cstring_len(const scc_cstring_t *str) {
* @param str 动态字符串指针
* @return cbool
*/
static inline cbool scc_cstring_is_empty(const scc_cstring_t *str) {
static inline cbool scc_str_is_empty(const scc_str_t *str) {
return str == null || str->size == 0;
}
@@ -173,7 +202,7 @@ static inline cbool scc_cstring_is_empty(const scc_cstring_t *str) {
*
* @param str 动态字符串指针
*/
static inline void scc_cstring_clear(scc_cstring_t *str) {
static inline void scc_str_clear(scc_str_t *str) {
if (str) {
str->size = 1;
if (str->data) {
@@ -188,19 +217,18 @@ static inline void scc_cstring_clear(scc_cstring_t *str) {
* @param str 动态字符串指针
* @return char* 返回指向内部缓冲区的 C 风格字符串指针
*/
static inline char *scc_cstring_as_cstr(const scc_cstring_t *str) {
static inline char *scc_str_as_cstr(const scc_str_t *str) {
if (str == null || str->data == null) {
return null;
}
return str->data;
}
static inline int scc_cstring_cmp(const scc_cstring_t *str1,
const scc_cstring_t *str2) {
return scc_strcmp(scc_cstring_as_cstr(str1), scc_cstring_as_cstr(str2));
static inline int scc_str_equal(const scc_str_t *str1, const scc_str_t *str2) {
return scc_strcmp(scc_str_as_cstr(str1), scc_str_as_cstr(str2));
}
static inline char *scc_cstring_move_cstr(scc_cstring_t *str) {
static inline char *scc_str_move_cstr(scc_str_t *str) {
if (str == null || str->data == null) {
return null;
}
@@ -211,8 +239,8 @@ static inline char *scc_cstring_move_cstr(scc_cstring_t *str) {
return ret;
}
static inline scc_cstring_t scc_cstring_move(scc_cstring_t *str) {
return scc_cstring_from_cstr(scc_cstring_move_cstr(str));
static inline scc_str_t scc_str_move(scc_str_t *str) {
return scc_str_from_cstr(scc_str_move_cstr(str));
}
#endif /* __SCC_CORE_STR_H__ */