- 新增预处理器库(pproc),替代原有的pprocessor模块 - 实现完整的宏定义解析功能,支持对象宏和函数宏 - 添加条件编译指令处理(#if、#ifdef、#ifndef、#else、#elif、#endif) - 实现宏展开机制,包括嵌套宏和递归宏处理 - 添加宏定义测试用例,覆盖基本功能和复杂场景 - 在cbuild.toml中更新依赖配置,移除parser、ast、ast2ir、ir等未完成模块 - 新增lexer工具函数用于token流处理 - 添加宏定义表管理功能,支持宏的创建、查找、删除操作 - 实现宏参数解析和替换列表处理
156 lines
4.5 KiB
C
156 lines
4.5 KiB
C
#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));
|
|
if (!macro) {
|
|
LOG_ERROR("Failed to allocate memory for macro");
|
|
return null;
|
|
}
|
|
|
|
macro->name = scc_cstring_copy(name);
|
|
macro->type = type;
|
|
scc_vec_init(macro->params);
|
|
scc_vec_init(macro->replaces);
|
|
|
|
return macro;
|
|
}
|
|
|
|
// 销毁宏对象
|
|
void scc_pp_macro_drop(scc_pp_macro_t *macro) {
|
|
if (!macro)
|
|
return;
|
|
|
|
scc_cstring_free(¯o->name);
|
|
|
|
// 释放参数列表
|
|
for (usize i = 0; i < macro->params.size; ++i) {
|
|
scc_lexer_tok_drop(&scc_vec_at(macro->params, i));
|
|
}
|
|
scc_vec_free(macro->params);
|
|
|
|
// 释放替换列表
|
|
for (usize i = 0; i < macro->replaces.size; ++i) {
|
|
scc_lexer_tok_drop(&scc_vec_at(macro->replaces, i));
|
|
}
|
|
scc_vec_free(macro->replaces);
|
|
|
|
scc_free(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) {
|
|
if (!macros || !name || !replacement)
|
|
return false;
|
|
|
|
scc_pp_macro_t *macro = scc_pp_macro_new(name, SCC_PP_MACRO_OBJECT);
|
|
if (!macro)
|
|
return false;
|
|
|
|
macro->replaces = *replacement;
|
|
|
|
// 检查是否已存在同名宏
|
|
scc_pp_macro_t *existing = scc_hashtable_get(¯os->table, ¯o->name);
|
|
if (existing) {
|
|
LOG_WARN("Redefining macro: %s", scc_cstring_as_cstr(¯o->name));
|
|
scc_pp_macro_drop(existing);
|
|
}
|
|
|
|
scc_hashtable_set(¯os->table, ¯o->name, macro);
|
|
return true;
|
|
}
|
|
|
|
// 添加函数宏
|
|
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) {
|
|
if (!macros || !name || !params || !replacement)
|
|
return false;
|
|
|
|
scc_pp_macro_t *macro = scc_pp_macro_new(name, SCC_PP_MACRO_FUNCTION);
|
|
if (!macro)
|
|
return false;
|
|
|
|
// 复制参数列表
|
|
macro->params = *params;
|
|
macro->replaces = *replacement;
|
|
|
|
// 检查是否已存在同名宏
|
|
scc_pp_macro_t *existing = scc_hashtable_get(¯os->table, ¯o->name);
|
|
if (existing) {
|
|
LOG_WARN("Redefining macro: %s", scc_cstring_as_cstr(¯o->name));
|
|
scc_pp_macro_drop(existing);
|
|
}
|
|
|
|
scc_hashtable_set(¯os->table, ¯o->name, macro);
|
|
return true;
|
|
}
|
|
|
|
/// marco_table
|
|
|
|
scc_pp_macro_t *scc_pp_macro_table_set(scc_pp_macro_table_t *pp,
|
|
scc_pp_macro_t *macro) {
|
|
Assert(pp != null && macro != null);
|
|
return scc_hashtable_set(&pp->table, ¯o->name, macro);
|
|
}
|
|
|
|
// 查找宏定义
|
|
scc_pp_macro_t *scc_pp_macro_table_get(scc_pp_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) {
|
|
if (!pp || !name)
|
|
return false;
|
|
|
|
scc_pp_macro_t *macro = scc_hashtable_get(&pp->table, name);
|
|
if (!macro)
|
|
return false;
|
|
|
|
scc_hashtable_del(&pp->table, name);
|
|
scc_pp_macro_drop(macro);
|
|
return true;
|
|
}
|
|
|
|
static u32 hash_func(const void *key) {
|
|
const scc_cstring_t *string = (const scc_cstring_t *)key;
|
|
return scc_strhash32(scc_cstring_as_cstr(string));
|
|
}
|
|
|
|
static int hash_cmp(const void *key1, const void *key2) {
|
|
const scc_cstring_t *str1 = (const scc_cstring_t *)key1;
|
|
const scc_cstring_t *str2 = (const scc_cstring_t *)key2;
|
|
|
|
if (str1->size != str2->size) {
|
|
return str1->size - str2->size;
|
|
}
|
|
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) {
|
|
Assert(macros != null);
|
|
macros->table.hash_func = hash_func;
|
|
macros->table.key_cmp = hash_cmp;
|
|
scc_hashtable_init(¯os->table);
|
|
}
|
|
|
|
static int macro_free(const void *key, void *value, void *context) {
|
|
(void)key;
|
|
(void)context;
|
|
scc_pp_macro_drop(value);
|
|
return 0;
|
|
}
|
|
|
|
void scc_pp_macro_table_drop(scc_pp_macro_table_t *macros) {
|
|
Assert(macros != null);
|
|
scc_hashtable_foreach(¯os->table, macro_free, null);
|
|
scc_hashtable_drop(¯os->table);
|
|
}
|