refactor(pprocessor): rename macro table type and update function names

- Change `scc_macro_table_t` to `scc_pp_macro_table_t` for consistency
- Rename `scc_pp_macro_create` to `scc_pp_macro_new` for naming convention
- Remove unused `scc_pp_compress_whitespace` function
- Update macro table function names: `scc_pp_find_macro` → `scc_pp_macro_table_get`, `scc_pp_remove_macro` → `scc_pp_macro_table_remove`
- Add new `scc_pp_macro_table_set` function for setting macros
- Update all function signatures to use new type name
- Remove commented-out whitespace compression code from implementation
This commit is contained in:
zzy
2025-12-14 12:59:03 +08:00
parent ce8031b21f
commit 73d74f5e13
9 changed files with 172 additions and 131 deletions

View File

@@ -22,7 +22,7 @@ typedef struct scc_macro {
typedef struct scc_macro_table {
scc_hashtable_t table; // 宏定义表
} scc_macro_table_t;
} scc_pp_macro_table_t;
/**
* @brief 创建宏对象
@@ -30,8 +30,8 @@ typedef struct scc_macro_table {
* @param type 宏类型
* @return 创建的宏对象指针失败返回NULL
*/
scc_pp_macro_t *scc_pp_macro_create(const scc_cstring_t *name,
scc_pp_macro_type_t type);
scc_pp_macro_t *scc_pp_macro_new(const scc_cstring_t *name,
scc_pp_macro_type_t type);
/**
* @brief 销毁宏对象
@@ -39,13 +39,6 @@ scc_pp_macro_t *scc_pp_macro_create(const scc_cstring_t *name,
*/
void scc_pp_macro_drop(scc_pp_macro_t *macro);
/**
* @brief 压缩空白字符
* @param tokens token列表
* @return 压缩后的字符串
*/
scc_cstring_t scc_pp_compress_whitespace(const scc_pp_macro_list_t *tokens);
/**
* @brief 添加对象宏
* @param pp 预处理器实例
@@ -53,7 +46,8 @@ scc_cstring_t scc_pp_compress_whitespace(const scc_pp_macro_list_t *tokens);
* @param replacement 替换文本列表
* @return 成功返回true失败返回false
*/
cbool scc_pp_add_object_macro(scc_macro_table_t *pp, const scc_cstring_t *name,
cbool scc_pp_add_object_macro(scc_pp_macro_table_t *pp,
const scc_cstring_t *name,
const scc_pp_macro_list_t *replacement);
/**
@@ -64,10 +58,19 @@ cbool scc_pp_add_object_macro(scc_macro_table_t *pp, const scc_cstring_t *name,
* @param replacement 替换文本列表
* @return 成功返回true失败返回false
*/
cbool scc_pp_add_function_macro(scc_macro_table_t *pp,
cbool scc_pp_add_function_macro(scc_pp_macro_table_t *pp,
const scc_cstring_t *name,
const scc_pp_macro_list_t *params,
const scc_pp_macro_list_t *replacement);
/**
* @brief
*
* @param pp
* @param macro
* @return scc_pp_macro_t*
*/
scc_pp_macro_t *scc_pp_macro_table_set(scc_pp_macro_table_t *pp,
scc_pp_macro_t *macro);
/**
* @brief 查找宏定义
@@ -75,7 +78,8 @@ cbool scc_pp_add_function_macro(scc_macro_table_t *pp,
* @param name 宏名称
* @return 找到的宏对象指针未找到返回NULL
*/
scc_pp_macro_t *scc_pp_find_macro(scc_macro_table_t *pp, scc_cstring_t *name);
scc_pp_macro_t *scc_pp_macro_table_get(scc_pp_macro_table_t *pp,
scc_cstring_t *name);
/**
* @brief 从预处理器中删除宏
@@ -83,8 +87,9 @@ scc_pp_macro_t *scc_pp_find_macro(scc_macro_table_t *pp, scc_cstring_t *name);
* @param name 宏名称
* @return 成功删除返回true未找到返回false
*/
cbool scc_pp_remove_macro(scc_macro_table_t *pp, const scc_cstring_t *name);
cbool scc_pp_macro_table_remove(scc_pp_macro_table_t *pp,
const scc_cstring_t *name);
void scc_pp_marco_table_init(scc_macro_table_t *macros);
void scc_pp_macro_table_drop(scc_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__ */

View File

@@ -4,8 +4,10 @@
#include <libcore.h>
#include <pp_macro.h>
void scc_pp_parse_directive(scc_probe_stream_t *stream, scc_pos_t *pos,
scc_macro_table_t *macros);
cbool scc_pp_expand_macro(scc_probe_stream_t *stream, scc_macro_table_t *macros,
scc_pp_macro_table_t *macros);
cbool scc_pp_expand_macro(scc_probe_stream_t *stream,
scc_pp_macro_table_t *macros,
scc_pp_macro_table_t *expand_stack,
scc_probe_stream_t **out_stream, int depth);
#endif /* __SCC_PP_PARSE_H__ */

View File

@@ -5,21 +5,21 @@
/// https://cppreference.cn/w/c/preprocessor
#define SCC_PP_INST_TOKEN \
X(define , SCC_PP_STD, SCC_PP_TOK_DEFINE ) \
X(undef , SCC_PP_STD, SCC_PP_TOK_UNDEF ) \
X(include , SCC_PP_STD, SCC_PP_TOK_INCLUDE ) \
X(if , SCC_PP_STD, SCC_PP_TOK_IF ) \
X(ifdef , SCC_PP_STD, SCC_PP_TOK_IFDEF ) \
X(ifndef , SCC_PP_STD, SCC_PP_TOK_IFNDEF ) \
X(else , SCC_PP_STD, SCC_PP_TOK_ELSE ) \
X(elif , SCC_PP_STD, SCC_PP_TOK_ELIF ) \
X(elifdef , SCC_PP_STD, SCC_PP_TOK_ELIFDEF ) \
X(elifndef , SCC_PP_C23, SCC_PP_TOK_ELIFNDEF ) \
X(endif , SCC_PP_STD, SCC_PP_TOK_ENDIF ) \
X(line , SCC_PP_STD, SCC_PP_TOK_LINE ) \
X(embed , SCC_PP_C23, SCC_PP_TOK_EMBED ) \
X(error , SCC_PP_STD, SCC_PP_TOK_ERROR ) \
X(warning , SCC_PP_C23, SCC_PP_TOK_WARNING ) \
X(pragma , SCC_PP_STD, SCC_PP_TOK_PRAMA ) \
X(elif , SCC_PP_STD, SCC_PP_TOK_UNDEF ) \
X(elifdef , SCC_PP_STD, SCC_PP_TOK_INCLUDE ) \
X(elifndef , SCC_PP_STD, SCC_PP_TOK_IF ) \
X(else , SCC_PP_STD, SCC_PP_TOK_IFDEF ) \
X(embed , SCC_PP_STD, SCC_PP_TOK_IFNDEF ) \
X(endif , SCC_PP_STD, SCC_PP_TOK_ELSE ) \
X(error , SCC_PP_STD, SCC_PP_TOK_ELIF ) \
X(if , SCC_PP_STD, SCC_PP_TOK_ELIFDEF ) \
X(ifdef , SCC_PP_C23, SCC_PP_TOK_ELIFNDEF ) \
X(ifndef , SCC_PP_STD, SCC_PP_TOK_ENDIF ) \
X(include , SCC_PP_STD, SCC_PP_TOK_LINE ) \
X(line , SCC_PP_C23, SCC_PP_TOK_EMBED ) \
X(pragma , SCC_PP_STD, SCC_PP_TOK_ERROR ) \
X(undef , SCC_PP_C23, SCC_PP_TOK_WARNING ) \
X(warning , SCC_PP_STD, SCC_PP_TOK_PRAMA ) \
// END
/* clang-format on */

View File

@@ -28,7 +28,7 @@ typedef struct if_stack_item {
typedef struct scc_pproc {
scc_probe_stream_t *stream; // 输出流
scc_strpool_t strpool; // 字符串池
scc_macro_table_t macro_table;
scc_pp_macro_table_t macro_table;
SCC_VEC(if_stack_item_t) if_stack; // 条件编译栈
} scc_pproc_t;