feat(pproc): 实现宏展开功能并重构宏定义接口

- 新增 pproc_expand.h 头文件,定义宏展开相关的数据结构和函数接口
- 重命名宏相关类型和函数,将 scc_pp_* 前缀统一改为 scc_pproc_*
- 修改宏参数解析逻辑,支持更灵活的参数处理方式
- 实现完整的宏展开功能,包括对象宏和函数宏的展开
- 添加字符串化操作符 (#) 的支持
- 改进预处理器主循环逻辑,优化宏展开流程
- 更新单元测试用例,增加对宏参数解析和字符串化的测试
This commit is contained in:
zzy
2026-02-18 18:18:57 +08:00
parent 9d85dc130d
commit c86071416d
8 changed files with 419 additions and 132 deletions

View File

@@ -1,9 +1,9 @@
#include <pproc_macro.h>
// 创建宏对象
scc_pp_macro_t *scc_pp_macro_new(const scc_cstring_t *name,
scc_pp_macro_type_t type) {
scc_pp_macro_t *macro = scc_malloc(sizeof(scc_pp_macro_t));
scc_pproc_macro_t *scc_pproc_macro_new(const scc_cstring_t *name,
scc_pproc_macro_type_t type) {
scc_pproc_macro_t *macro = scc_malloc(sizeof(scc_pproc_macro_t));
if (!macro) {
LOG_ERROR("Failed to allocate memory for macro");
return null;
@@ -18,7 +18,7 @@ scc_pp_macro_t *scc_pp_macro_new(const scc_cstring_t *name,
}
// 销毁宏对象
void scc_pp_macro_drop(scc_pp_macro_t *macro) {
void scc_pproc_macro_drop(scc_pproc_macro_t *macro) {
if (!macro)
return;
@@ -40,23 +40,24 @@ void scc_pp_macro_drop(scc_pp_macro_t *macro) {
}
// 添加对象宏
cbool scc_pp_add_object_macro(scc_pp_macro_table_t *macros,
const scc_cstring_t *name,
const scc_pproc_macro_list_t *replacement) {
cbool scc_pproc_add_object_macro(scc_pproc_macro_table_t *macros,
const scc_cstring_t *name,
const scc_lexer_tok_vec_t *replacement) {
if (!macros || !name || !replacement)
return false;
scc_pp_macro_t *macro = scc_pp_macro_new(name, SCC_PP_MACRO_OBJECT);
scc_pproc_macro_t *macro = scc_pproc_macro_new(name, SCC_PP_MACRO_OBJECT);
if (!macro)
return false;
macro->replaces = *replacement;
// 检查是否已存在同名宏
scc_pp_macro_t *existing = scc_hashtable_get(&macros->table, &macro->name);
scc_pproc_macro_t *existing =
scc_hashtable_get(&macros->table, &macro->name);
if (existing) {
LOG_WARN("Redefining macro: %s", scc_cstring_as_cstr(&macro->name));
scc_pp_macro_drop(existing);
scc_pproc_macro_drop(existing);
}
scc_hashtable_set(&macros->table, &macro->name, macro);
@@ -64,14 +65,14 @@ cbool scc_pp_add_object_macro(scc_pp_macro_table_t *macros,
}
// 添加函数宏
cbool scc_pp_add_function_macro(scc_pp_macro_table_t *macros,
const scc_cstring_t *name,
const scc_pproc_macro_list_t *params,
const scc_pproc_macro_list_t *replacement) {
cbool scc_pproc_add_function_macro(scc_pproc_macro_table_t *macros,
const scc_cstring_t *name,
const scc_lexer_tok_vec_t *params,
const scc_lexer_tok_vec_t *replacement) {
if (!macros || !name || !params || !replacement)
return false;
scc_pp_macro_t *macro = scc_pp_macro_new(name, SCC_PP_MACRO_FUNCTION);
scc_pproc_macro_t *macro = scc_pproc_macro_new(name, SCC_PP_MACRO_FUNCTION);
if (!macro)
return false;
@@ -80,10 +81,11 @@ cbool scc_pp_add_function_macro(scc_pp_macro_table_t *macros,
macro->replaces = *replacement;
// 检查是否已存在同名宏
scc_pp_macro_t *existing = scc_hashtable_get(&macros->table, &macro->name);
scc_pproc_macro_t *existing =
scc_hashtable_get(&macros->table, &macro->name);
if (existing) {
LOG_WARN("Redefining macro: %s", scc_cstring_as_cstr(&macro->name));
scc_pp_macro_drop(existing);
scc_pproc_macro_drop(existing);
}
scc_hashtable_set(&macros->table, &macro->name, macro);
@@ -92,30 +94,30 @@ cbool scc_pp_add_function_macro(scc_pp_macro_table_t *macros,
/// marco_table
scc_pp_macro_t *scc_pp_macro_table_set(scc_pp_macro_table_t *pp,
scc_pp_macro_t *macro) {
scc_pproc_macro_t *scc_pproc_macro_table_set(scc_pproc_macro_table_t *pp,
scc_pproc_macro_t *macro) {
Assert(pp != null && macro != null);
return scc_hashtable_set(&pp->table, &macro->name, macro);
}
// 查找宏定义
scc_pp_macro_t *scc_pp_macro_table_get(scc_pp_macro_table_t *pp,
const scc_cstring_t *name) {
scc_pproc_macro_t *scc_pproc_macro_table_get(scc_pproc_macro_table_t *pp,
const scc_cstring_t *name) {
return scc_hashtable_get(&pp->table, name);
}
// 从预处理器中删除宏
cbool scc_pp_macro_table_remove(scc_pp_macro_table_t *pp,
const scc_cstring_t *name) {
cbool scc_pproc_macro_table_remove(scc_pproc_macro_table_t *pp,
const scc_cstring_t *name) {
if (!pp || !name)
return false;
scc_pp_macro_t *macro = scc_hashtable_get(&pp->table, name);
scc_pproc_macro_t *macro = scc_hashtable_get(&pp->table, name);
if (!macro)
return false;
scc_hashtable_del(&pp->table, name);
scc_pp_macro_drop(macro);
scc_pproc_macro_drop(macro);
return true;
}
@@ -134,7 +136,7 @@ static int hash_cmp(const void *key1, const void *key2) {
return scc_strcmp(scc_cstring_as_cstr(str1), scc_cstring_as_cstr(str2));
}
void scc_pp_marco_table_init(scc_pp_macro_table_t *macros) {
void scc_pproc_marco_table_init(scc_pproc_macro_table_t *macros) {
Assert(macros != null);
macros->table.hash_func = hash_func;
macros->table.key_cmp = hash_cmp;
@@ -144,11 +146,11 @@ void scc_pp_marco_table_init(scc_pp_macro_table_t *macros) {
static int macro_free(const void *key, void *value, void *context) {
(void)key;
(void)context;
scc_pp_macro_drop(value);
scc_pproc_macro_drop(value);
return 0;
}
void scc_pp_macro_table_drop(scc_pp_macro_table_t *macros) {
void scc_pproc_macro_table_drop(scc_pproc_macro_table_t *macros) {
Assert(macros != null);
scc_hashtable_foreach(&macros->table, macro_free, null);
scc_hashtable_drop(&macros->table);