Files
scc/src/main.c
zzy 08a60e6e8a feat: 添加预处理器宏定义的字符串化和连接操作支持
- 实现了 # 和 ## 预处理器操作符的功能
- 添加了 token 深拷贝和移动函数以支持宏展开
- 修改预处理器展开逻辑以正确处理宏参数替换
- 增加了宏参数分割时对空白字符的处理

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

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

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

- 新增 count-file 命令用于统计代码文件数量
- 调整了输出文件的默认命名规则
- 优化了词法分析器 token 释放时的字段重置逻辑
2026-02-19 11:20:01 +08:00

267 lines
8.5 KiB
C

#include <argparse.h>
#include <scc_lexer.h>
#include <scc_pproc.h>
// #include <scc_parser.h>
// #include <ast_dump.h>
// #include <ir_dump.h>
// #include <scc_ast2ir.h>
#include <stdio.h>
typedef struct {
const char *input_file;
const char *output_file;
int verbose;
cbool emit_lex;
cbool emit_pp;
cbool emit_ast;
cbool emit_ir;
} scc_config_t;
static void setup_argparse(scc_argparse_t *argparse, scc_config_t *config,
scc_argparse_lang_t lang) {
enum {
SCC_HINT_PROG_NAME,
SCC_HINT_DESCRIPTION,
SCC_HINT_OUTPUT_FILE,
SCC_HINT_INPUT_FILE,
SCC_HINT_VERBOSE,
SCC_HINT_EMIT_LEX,
SCC_HINT_EMIT_PP,
SCC_HINT_EMIT_AST,
SCC_HINT_EMIT_IR,
};
static const char *scc_hints_en[] = {
[SCC_HINT_PROG_NAME] = "scc",
[SCC_HINT_DESCRIPTION] = "A simple C compiler",
[SCC_HINT_OUTPUT_FILE] = "Output file",
[SCC_HINT_INPUT_FILE] = "Input source file",
[SCC_HINT_VERBOSE] = "Increase verbosity (can be used multiple times)",
[SCC_HINT_EMIT_LEX] = "Generate lexer sources tokens and exit",
[SCC_HINT_EMIT_PP] = "Generate preprocessed tokens and exit",
[SCC_HINT_EMIT_AST] = "Generate AST and exit",
[SCC_HINT_EMIT_IR] = "Generate IR and exit",
};
static const char *scc_hints_zh[] = {
[SCC_HINT_PROG_NAME] = "scc",
[SCC_HINT_DESCRIPTION] = "一个简单的C编译器",
[SCC_HINT_OUTPUT_FILE] = "输出文件",
[SCC_HINT_INPUT_FILE] = "输入源文件",
[SCC_HINT_VERBOSE] = "增加详细输出(可多次使用)",
[SCC_HINT_EMIT_LEX] = "生成`源代码的词法单元`并退出",
[SCC_HINT_EMIT_PP] = "生成`预处理后的词法单元`并退出",
[SCC_HINT_EMIT_AST] = "生成`抽象语法树`并退出",
[SCC_HINT_EMIT_IR] = "生成`中间代码`并退出",
};
const char **scc_hints;
switch (lang) {
case SCC_ARGPARSE_LANG_EN:
scc_hints = scc_hints_en;
break;
case SCC_ARGPARSE_LANG_ZH:
scc_hints = scc_hints_zh;
break;
default:
scc_hints = scc_hints_en;
break;
}
scc_argparse_init(argparse, scc_hints[SCC_HINT_PROG_NAME],
scc_hints[SCC_HINT_DESCRIPTION]);
argparse->lang = lang;
scc_argparse_cmd_t *root = scc_argparse_get_root(argparse);
// -o, --output
scc_argparse_opt_t opt_output;
scc_argparse_opt_init(&opt_output, 'o', "output",
scc_hints[SCC_HINT_OUTPUT_FILE]);
scc_argparse_spec_setup_string(&opt_output.spec, &(config->output_file));
scc_argparse_cmd_add_opt(root, &opt_output);
// input file (必需)
scc_argparse_arg_t arg_input;
scc_argparse_arg_init(&arg_input, "input", scc_hints[SCC_HINT_INPUT_FILE]);
scc_argparse_spec_setup_string(&arg_input.spec, &(config->input_file));
scc_argparse_spec_set_required(&arg_input.spec, true);
scc_argparse_cmd_add_arg(root, &arg_input);
// -v, --verbose (计数)
scc_argparse_opt_t opt_verbose;
scc_argparse_opt_init(&opt_verbose, 'V', "verbose",
scc_hints[SCC_HINT_VERBOSE]);
scc_argparse_spec_setup_count(&opt_verbose.spec, &(config->verbose));
scc_argparse_cmd_add_opt(root, &opt_verbose);
// --emit-lex
scc_argparse_opt_t opt_lex;
scc_argparse_opt_init(&opt_lex, 0, "emit-lex",
scc_hints[SCC_HINT_EMIT_LEX]);
scc_argparse_spec_setup_bool(&opt_lex.spec, &(config->emit_lex));
scc_argparse_cmd_add_opt(root, &opt_lex);
// --emit-pp
scc_argparse_opt_t opt_pp;
scc_argparse_opt_init(&opt_pp, 0, "emit-pp", scc_hints[SCC_HINT_EMIT_PP]);
scc_argparse_spec_setup_bool(&opt_pp.spec, &(config->emit_pp));
scc_argparse_cmd_add_opt(root, &opt_pp);
// -T, --emit-ast
scc_argparse_opt_t opt_ast;
scc_argparse_opt_init(&opt_ast, 'T', "emit-ast",
scc_hints[SCC_HINT_EMIT_AST]);
scc_argparse_spec_setup_bool(&opt_ast.spec, &(config->emit_ast));
scc_argparse_cmd_add_opt(root, &opt_ast);
// -R, --emit-ir
scc_argparse_opt_t opt_ir;
scc_argparse_opt_init(&opt_ir, 'R', "emit-ir", scc_hints[SCC_HINT_EMIT_IR]);
scc_argparse_spec_setup_bool(&opt_ir.spec, &(config->emit_ir));
scc_argparse_cmd_add_opt(root, &opt_ir);
}
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
static void print_ring(scc_lexer_tok_ring_t *ring, int verbose) {
scc_lexer_tok_t tok = {0};
int ret = 0;
while (1) {
scc_ring_next_consume(*ring, tok, ret);
if (ret == false || tok.type == SCC_TOK_EOF) {
break;
}
if (verbose == 0) {
scc_printf("%s ", scc_get_tok_name(tok.type));
} else if (verbose >= 1) {
scc_printf("token [%-8s] `%s` at %s:%d:%d\n",
scc_get_tok_name(tok.type),
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);
SetConsoleCP(CP_UTF8);
#endif
setbuf(stdout, NULL);
#ifdef _WIN32
#define OUTPUT_DEFAULT_FILE "a.exe"
#else
#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,
};
scc_argparse_t argparse;
setup_argparse(&argparse, &config, SCC_ARGPARSE_LANG_ZH);
int ret = scc_argparse_parse(&argparse, argc, argv);
if (ret != 0) {
scc_argparse_drop(&argparse);
return ret;
}
scc_argparse_drop(&argparse);
scc_sstream_t sstream;
if (scc_sstream_init(&sstream, config.input_file, 1024)) {
return 0;
}
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, 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, true));
if (config.emit_pp) {
scc_lexer_tok_ring_t *tok_ring = scc_pproc_to_ring(&pproc, 8);
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);
scc_lexer_drop(&lexer);
scc_sstream_drop(&sstream);
// scc_parser_t parser;
// scc_parser_init(&parser, &lexer_stream, null);
// scc_ast_translation_unit_t *translation_unit =
// scc_parse_translation_unit(&parser);
// if (config.emit_ast) {
// scc_tree_dump_ctx_t tree_dump;
// scc_tree_dump_ctx_init(&tree_dump, true);
// scc_ast_dump_node(&tree_dump, (scc_ast_node_t *)translation_unit);
// scc_tree_dump_ctx_drop(&tree_dump);
// return 0;
// }
// scc_ir_builder_t ir_builder;
// scc_ast2ir(translation_unit, &ir_builder);
// if (config.emit_ir) {
// scc_ir_dump_ctx_t ir_dump_ctx;
// scc_tree_dump_ctx_t tree_dump; // 仅为 ir dump 辅助
// scc_tree_dump_ctx_init(&tree_dump, true);
// scc_ir_dump_ctx_init(&ir_dump_ctx, &tree_dump, &ir_builder.cprog,
// &ir_builder.ctx);
// // scc_ir_dump_cprog(&ir_dump_ctx);
// scc_ir_dump_cprog_linear(&ir_dump_ctx);
// scc_tree_dump_ctx_drop(&tree_dump);
// return 0;
// }
scc_printf("output exe at %s", config.output_file);
return 0;
}