feat(pproc): 实现预处理器条件编译和可变参数宏支持

- 添加了完整的条件编译功能,包括 #if、#elif、#else、#endif 指令
- 实现了数值常量表达式的解析和求值
- 支持嵌套条件编译和与其他指令混合使用
- 实现了可变参数宏定义和 __VA_ARGS__ 替换功能
- 改进了宏展开机制以正确处理可变参数宏
- 重构了预处理器指令处理逻辑,提高了代码可维护性
- 添加了相应的单元测试用例验证新功能
This commit is contained in:
zzy
2026-02-21 15:59:31 +08:00
parent b705e5d0ad
commit 8007825800
7 changed files with 317 additions and 134 deletions

View File

@@ -123,32 +123,44 @@ void scc_pproc_expand_by_vec(scc_pproc_macro_table_t *macro_table,
static inline void
split_arguments(scc_pproc_macro_extened_params_t *splited_params,
scc_lexer_tok_vec_t *raw_args) {
scc_lexer_tok_vec_t *raw_args, const scc_pproc_macro_t *macro) {
scc_lexer_tok_vec_t arg;
scc_vec_init(arg);
int named_count = (int)scc_vec_size(macro->params);
cbool is_variadic =
(named_count > 0 &&
scc_vec_at(macro->params, named_count - 1).type == SCC_TOK_ELLIPSIS);
scc_vec_foreach(*raw_args, i) {
scc_lexer_tok_t *raw_arg = &scc_vec_at(*raw_args, i);
if (raw_arg->type == SCC_TOK_COMMA) {
scc_lexer_tok_drop(raw_arg);
if (scc_vec_size(arg) &&
scc_vec_at(arg, scc_vec_size(arg) - 1).type == SCC_TOK_BLANK) {
scc_lexer_tok_drop(&scc_vec_pop(arg));
}
scc_vec_push(*splited_params, arg);
scc_vec_init(arg);
} else {
if (raw_arg->type != SCC_TOK_COMMA ||
(is_variadic && scc_vec_size(*splited_params) == named_count - 1)) {
if (scc_vec_size(arg) == 0 && raw_arg->type == SCC_TOK_BLANK) {
scc_lexer_tok_drop(raw_arg);
} else {
scc_vec_push(arg, *raw_arg);
}
continue;
}
scc_lexer_tok_drop(raw_arg);
if (scc_vec_size(arg) &&
scc_vec_at(arg, scc_vec_size(arg) - 1).type == SCC_TOK_BLANK) {
scc_lexer_tok_drop(&scc_vec_pop(arg));
}
scc_vec_push(*splited_params, arg);
scc_vec_init(arg);
}
if (scc_vec_size(arg) &&
scc_vec_at(arg, scc_vec_size(arg) - 1).type == SCC_TOK_BLANK) {
scc_lexer_tok_drop(&scc_vec_pop(arg));
}
scc_vec_push(*splited_params, arg);
if (is_variadic && scc_vec_size(*splited_params) == named_count - 1) {
scc_vec_init(arg);
scc_vec_push(*splited_params, arg);
}
}
static inline void
@@ -232,7 +244,7 @@ static inline void expand_function_macro(scc_pproc_expand_t *expand_ctx,
// collect, fill and expand arg
scc_pproc_macro_extened_params_t splited_params;
scc_vec_init(splited_params);
split_arguments(&splited_params, &raw_args);
split_arguments(&splited_params, &raw_args, macro);
scc_pproc_macro_extened_params_t expanded_params;
scc_vec_init(expanded_params);