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

@@ -10,21 +10,20 @@ typedef enum {
SCC_PP_MACRO_NONE, // 不是宏
SCC_PP_MACRO_OBJECT, // 对象宏
SCC_PP_MACRO_FUNCTION, // 函数宏
} scc_pp_macro_type_t;
typedef scc_lexer_tok_vec_t scc_pproc_macro_list_t;
} scc_pproc_macro_type_t;
typedef SCC_VEC(scc_lexer_tok_vec_t) scc_pproc_macro_extened_params_t;
// 宏定义结构
typedef struct scc_macro {
scc_cstring_t name; // 宏名称
scc_pp_macro_type_t type; // 宏类型
scc_lexer_tok_vec_t replaces; // 替换列表
scc_pproc_macro_list_t params; // 参数列表(仅函数宏)
} scc_pp_macro_t;
scc_cstring_t name; // 宏名称
scc_pproc_macro_type_t type; // 宏类型
scc_lexer_tok_vec_t replaces; // 替换列表
scc_lexer_tok_vec_t params; // 参数列表(仅函数宏)
} scc_pproc_macro_t;
typedef struct scc_macro_table {
scc_hashtable_t table; // 宏定义表
} scc_pp_macro_table_t;
} scc_pproc_macro_table_t;
/**
* @brief 创建宏对象
@@ -32,14 +31,14 @@ typedef struct scc_macro_table {
* @param type 宏类型
* @return 创建的宏对象指针失败返回NULL
*/
scc_pp_macro_t *scc_pp_macro_new(const scc_cstring_t *name,
scc_pp_macro_type_t type);
scc_pproc_macro_t *scc_pproc_macro_new(const scc_cstring_t *name,
scc_pproc_macro_type_t type);
/**
* @brief 销毁宏对象
* @param macro 要销毁的宏对象
*/
void scc_pp_macro_drop(scc_pp_macro_t *macro);
void scc_pproc_macro_drop(scc_pproc_macro_t *macro);
/**
* @brief 添加对象宏
@@ -48,9 +47,9 @@ void scc_pp_macro_drop(scc_pp_macro_t *macro);
* @param replacement 替换文本列表
* @return 成功返回true失败返回false
*/
cbool scc_pp_add_object_macro(scc_pp_macro_table_t *pp,
const scc_cstring_t *name,
const scc_pproc_macro_list_t *replacement);
cbool scc_pproc_add_object_macro(scc_pproc_macro_table_t *pp,
const scc_cstring_t *name,
const scc_lexer_tok_vec_t *replacement);
/**
* @brief 添加函数宏
@@ -60,19 +59,19 @@ cbool scc_pp_add_object_macro(scc_pp_macro_table_t *pp,
* @param replacement 替换文本列表
* @return 成功返回true失败返回false
*/
cbool scc_pp_add_function_macro(scc_pp_macro_table_t *pp,
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 *pp,
const scc_cstring_t *name,
const scc_lexer_tok_vec_t *params,
const scc_lexer_tok_vec_t *replacement);
/**
* @brief
*
* @param pp
* @param macro
* @return scc_pp_macro_t*
* @return scc_pproc_macro_t*
*/
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);
/**
* @brief 查找宏定义
@@ -80,8 +79,8 @@ scc_pp_macro_t *scc_pp_macro_table_set(scc_pp_macro_table_t *pp,
* @param name 宏名称
* @return 找到的宏对象指针未找到返回NULL
*/
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);
/**
* @brief 从预处理器中删除宏
@@ -89,9 +88,10 @@ scc_pp_macro_t *scc_pp_macro_table_get(scc_pp_macro_table_t *pp,
* @param name 宏名称
* @return 成功删除返回true未找到返回false
*/
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);
void scc_pproc_marco_table_init(scc_pproc_macro_table_t *macros);
void scc_pproc_macro_table_drop(scc_pproc_macro_table_t *macros);
void scc_pp_marco_table_init(scc_pp_macro_table_t *macros);
void scc_pp_macro_table_drop(scc_pp_macro_table_t *macros);
#endif /* __SCC_PP_MACRO_H__ */