feat: 添加预处理器宏定义的字符串化和连接操作支持

- 实现了 # 和 ## 预处理器操作符的功能
- 添加了 token 深拷贝和移动函数以支持宏展开
- 修改预处理器展开逻辑以正确处理宏参数替换
- 增加了宏参数分割时对空白字符的处理

fix: 修复预处理器宏展开中的内存管理和逻辑错误

- 修正了宏展开集合的数据结构初始化方式
- 修复了函数式宏调用时括号匹配的判断逻辑
- 改进了宏参数解析过程中空白字符的处理
- 解决了 token 在宏展开过程中的所有权管理问题

chore: 为 justfile 添加文件统计命令并优化构建配置

- 新增 count-file 命令用于统计代码文件数量
- 调整了输出文件的默认命名规则
- 优化了词法分析器 token 释放时的字段重置逻辑
This commit is contained in:
zzy
2026-02-19 11:20:01 +08:00
parent c86071416d
commit 08a60e6e8a
9 changed files with 332 additions and 81 deletions

View File

@@ -143,9 +143,33 @@ static void print_ring(scc_lexer_tok_ring_t *ring, int verbose) {
scc_cstring_as_cstr(&tok.lexeme), tok.loc.name,
tok.loc.line, tok.loc.col);
}
scc_lexer_tok_drop(&tok);
}
}
static void print_file(scc_lexer_tok_ring_t *ring, const char *file_name) {
scc_lexer_tok_t tok = {0};
int ret = 0;
scc_file_t fp = scc_fopen(file_name, SCC_FILE_WRITE);
if (fp == null) {
LOG_FATAL("Failed to open file %s", file_name);
return;
}
while (1) {
scc_ring_next_consume(*ring, tok, ret);
if (ret == false || tok.type == SCC_TOK_EOF) {
break;
}
usize ret = scc_fwrite(fp, scc_cstring_as_cstr(&tok.lexeme),
scc_cstring_len(&tok.lexeme));
if (ret != scc_cstring_len(&tok.lexeme)) {
LOG_FATAL("Failed to write to file %s", file_name);
}
scc_lexer_tok_drop(&tok);
}
scc_fclose(fp);
}
int main(int argc, const char **argv, const char **envp) {
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
@@ -153,14 +177,16 @@ int main(int argc, const char **argv, const char **envp) {
#endif
setbuf(stdout, NULL);
scc_config_t config = {
.input_file = NULL,
#ifdef _WIN32
.output_file = "a.exe",
#define OUTPUT_DEFAULT_FILE "a.exe"
#else
.output_file = "a.out",
#define OUTPUT_DEFAULT_FILE "a.out"
#endif
scc_config_t config = {
.input_file = null,
.verbose = 0,
.output_file = null,
.emit_ast = false,
.emit_ir = false,
};
@@ -181,16 +207,26 @@ int main(int argc, const char **argv, const char **envp) {
scc_lexer_t lexer;
scc_lexer_init(&lexer, scc_sstream_to_ring(&sstream));
if (config.emit_lex) {
scc_lexer_tok_ring_t *tok_ring = scc_lexer_to_ring(&lexer, 8, false);
print_ring(tok_ring, config.verbose);
scc_lexer_tok_ring_t *tok_ring = scc_lexer_to_ring(
&lexer, 8, config.output_file == null ? false : true);
if (config.output_file == null) {
print_ring(tok_ring, config.verbose);
} else {
print_file(tok_ring, config.output_file);
}
return 0;
}
scc_pproc_t pproc;
scc_pproc_init(&pproc, scc_lexer_to_ring(&lexer, 8, false));
scc_pproc_init(&pproc, scc_lexer_to_ring(&lexer, 8, true));
if (config.emit_pp) {
scc_lexer_tok_ring_t *tok_ring = scc_pproc_to_ring(&pproc, 8);
print_ring(tok_ring, config.verbose);
if (config.output_file == null) {
print_ring(tok_ring, config.verbose);
} else {
print_file(tok_ring, config.output_file);
}
return 0;
}
scc_pproc_drop(&pproc);