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:
@@ -1,8 +1,8 @@
|
||||
#include <pp_macro.h>
|
||||
|
||||
// 创建宏对象
|
||||
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) {
|
||||
scc_pp_macro_t *macro = scc_malloc(sizeof(scc_pp_macro_t));
|
||||
if (!macro) {
|
||||
LOG_ERROR("Failed to allocate memory for macro");
|
||||
@@ -39,66 +39,17 @@ void scc_pp_macro_drop(scc_pp_macro_t *macro) {
|
||||
scc_free(macro);
|
||||
}
|
||||
|
||||
// // 压缩空白字符
|
||||
// scc_cstring_t scc_pp_compress_whitespace(const scc_pp_macro_list_t *tokens) {
|
||||
// scc_cstring_t combined = scc_cstring_new();
|
||||
// cbool last_was_space = false;
|
||||
|
||||
// for (usize i = 0; i < tokens->size; ++i) {
|
||||
// scc_cstring_t *token = &scc_vec_at(*tokens, i);
|
||||
// const char *str = scc_cstring_as_cstr(token);
|
||||
// usize len = scc_cstring_len(token);
|
||||
|
||||
// for (usize j = 0; j < len; ++j) {
|
||||
// char ch = str[j];
|
||||
// if (ch == ' ' || ch == '\t') {
|
||||
// if (!last_was_space && !scc_cstring_is_empty(&combined)) {
|
||||
// scc_cstring_append_ch(&combined, ' ');
|
||||
// last_was_space = true;
|
||||
// }
|
||||
// } else {
|
||||
// scc_cstring_append_ch(&combined, ch);
|
||||
// last_was_space = false;
|
||||
// }
|
||||
// }
|
||||
|
||||
// // 在 token 之间添加一个空格(除非已经是空格)
|
||||
// if (i + 1 < tokens->size && !last_was_space &&
|
||||
// !scc_cstring_is_empty(&combined)) {
|
||||
// scc_cstring_append_ch(&combined, ' ');
|
||||
// last_was_space = true;
|
||||
// }
|
||||
// }
|
||||
|
||||
// // 去除尾随空格
|
||||
// while (!scc_cstring_is_empty(&combined) &&
|
||||
// (combined.data[combined.size - 1] == ' ' ||
|
||||
// combined.data[combined.size - 1] == '\t')) {
|
||||
// combined.size--;
|
||||
// }
|
||||
|
||||
// return combined;
|
||||
// }
|
||||
|
||||
// 添加对象宏
|
||||
cbool scc_pp_add_object_macro(scc_macro_table_t *macros,
|
||||
cbool scc_pp_add_object_macro(scc_pp_macro_table_t *macros,
|
||||
const scc_cstring_t *name,
|
||||
const scc_pp_macro_list_t *replacement) {
|
||||
if (!macros || !name || !replacement)
|
||||
return false;
|
||||
|
||||
scc_pp_macro_t *macro = scc_pp_macro_create(name, SCC_PP_MACRO_OBJECT);
|
||||
scc_pp_macro_t *macro = scc_pp_macro_new(name, SCC_PP_MACRO_OBJECT);
|
||||
if (!macro)
|
||||
return false;
|
||||
|
||||
// if (replacement->size > 0) {
|
||||
// scc_cstring_t combined = scc_pp_compress_whitespace(replacement);
|
||||
// scc_vec_push(macro->replaces, combined);
|
||||
// // 释放原始 tokens
|
||||
// for (usize i = 0; i < replacement->size; ++i) {
|
||||
// scc_cstring_free(&scc_vec_at(*replacement, i));
|
||||
// }
|
||||
// }
|
||||
macro->replaces = *replacement;
|
||||
|
||||
// 检查是否已存在同名宏
|
||||
@@ -113,14 +64,14 @@ cbool scc_pp_add_object_macro(scc_macro_table_t *macros,
|
||||
}
|
||||
|
||||
// 添加函数宏
|
||||
cbool scc_pp_add_function_macro(scc_macro_table_t *macros,
|
||||
cbool scc_pp_add_function_macro(scc_pp_macro_table_t *macros,
|
||||
const scc_cstring_t *name,
|
||||
const scc_pp_macro_list_t *params,
|
||||
const scc_pp_macro_list_t *replacement) {
|
||||
if (!macros || !name || !params || !replacement)
|
||||
return false;
|
||||
|
||||
scc_pp_macro_t *macro = scc_pp_macro_create(name, SCC_PP_MACRO_FUNCTION);
|
||||
scc_pp_macro_t *macro = scc_pp_macro_new(name, SCC_PP_MACRO_FUNCTION);
|
||||
if (!macro)
|
||||
return false;
|
||||
|
||||
@@ -128,17 +79,6 @@ cbool scc_pp_add_function_macro(scc_macro_table_t *macros,
|
||||
macro->params = *params;
|
||||
macro->replaces = *replacement;
|
||||
|
||||
// if (replacement->size > 0) {
|
||||
// // 函数宏直接存储替换文本
|
||||
// scc_cstring_t combined = scc_pp_compress_whitespace(replacement);
|
||||
// scc_vec_push(macro->replaces, combined);
|
||||
|
||||
// // 释放原始 tokens
|
||||
// for (usize i = 0; i < replacement->size; ++i) {
|
||||
// scc_cstring_free(&scc_vec_at(*replacement, i));
|
||||
// }
|
||||
// }
|
||||
|
||||
// 检查是否已存在同名宏
|
||||
scc_pp_macro_t *existing = scc_hashtable_get(¯os->table, ¯o->name);
|
||||
if (existing) {
|
||||
@@ -150,13 +90,23 @@ cbool scc_pp_add_function_macro(scc_macro_table_t *macros,
|
||||
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_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) {
|
||||
return scc_hashtable_get(&pp->table, name);
|
||||
}
|
||||
|
||||
// 从预处理器中删除宏
|
||||
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) {
|
||||
if (!pp || !name)
|
||||
return false;
|
||||
|
||||
@@ -169,8 +119,6 @@ cbool scc_pp_remove_macro(scc_macro_table_t *pp, const scc_cstring_t *name) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/// marco_table
|
||||
|
||||
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));
|
||||
@@ -186,7 +134,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_macro_table_t *macros) {
|
||||
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;
|
||||
@@ -200,7 +148,7 @@ static int macro_free(const void *key, void *value, void *context) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void scc_pp_macro_table_drop(scc_macro_table_t *macros) {
|
||||
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);
|
||||
|
||||
@@ -193,7 +193,7 @@ static cbool safe_skip_backspace_if_endline(scc_probe_stream_t *stream,
|
||||
}
|
||||
|
||||
void scc_pp_parse_directive(scc_probe_stream_t *stream, scc_pos_t *pos,
|
||||
scc_macro_table_t *macros) {
|
||||
scc_pp_macro_table_t *macros) {
|
||||
Assert(stream != null);
|
||||
|
||||
scc_probe_stream_reset(stream);
|
||||
@@ -264,7 +264,7 @@ void scc_pp_parse_directive(scc_probe_stream_t *stream, scc_pos_t *pos,
|
||||
case SCC_PP_TOK_UNDEF: {
|
||||
if (scc_lex_parse_identifier(stream, pos, &name)) {
|
||||
// TODO ret value
|
||||
scc_pp_remove_macro(macros, &name);
|
||||
scc_pp_macro_table_remove(macros, &name);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -283,7 +283,6 @@ void scc_pp_parse_directive(scc_probe_stream_t *stream, scc_pos_t *pos,
|
||||
case SCC_PP_TOK_WARNING:
|
||||
case SCC_PP_TOK_PRAMA:
|
||||
// 暂时跳过这一行
|
||||
TODO();
|
||||
scc_lex_parse_skip_line(stream, pos);
|
||||
break;
|
||||
default:
|
||||
@@ -390,11 +389,14 @@ cbool scc_pp_expand_function_macro(scc_pp_macro_t *macro,
|
||||
return true;
|
||||
}
|
||||
|
||||
cbool scc_pp_expand_macro(scc_probe_stream_t *stream, scc_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) {
|
||||
// TODO self position and it maybe is a stack on #include ?
|
||||
// 递归扫描
|
||||
if (depth <= 0) {
|
||||
*out_stream = null;
|
||||
return false;
|
||||
}
|
||||
Assert(stream != null && macros != null && out_stream != null);
|
||||
@@ -406,9 +408,11 @@ cbool scc_pp_expand_macro(scc_probe_stream_t *stream, scc_macro_table_t *macros,
|
||||
ret = scc_lex_parse_identifier(stream, &pos, &identifier);
|
||||
Assert(ret == true);
|
||||
|
||||
scc_pp_macro_t *macro = scc_pp_find_macro(macros, &identifier);
|
||||
if (macro == null) {
|
||||
// 不是宏,直接输出标识符
|
||||
scc_pp_macro_t *macro = scc_pp_macro_table_get(macros, &identifier);
|
||||
// 1. 不是宏,直接输出标识符
|
||||
// 2. 检查到重复展开跳过
|
||||
if (macro == null ||
|
||||
scc_pp_macro_table_get(expand_stack, ¯o->name) != null) {
|
||||
*out_stream =
|
||||
scc_mem_probe_stream_new(scc_cstring_as_cstr(&identifier),
|
||||
scc_cstring_len(&identifier), false);
|
||||
@@ -436,15 +440,24 @@ cbool scc_pp_expand_macro(scc_probe_stream_t *stream, scc_macro_table_t *macros,
|
||||
Assert(ret == true);
|
||||
}
|
||||
|
||||
// 已经展开的将被标记并入栈
|
||||
scc_pp_macro_table_set(expand_stack,
|
||||
scc_pp_macro_new(¯o->name, macro->type));
|
||||
|
||||
// 将展开内容变换成stream
|
||||
scc_probe_stream_t *tmp_stream = scc_mem_probe_stream_new(
|
||||
scc_cstring_as_cstr(&tmp_buff), scc_cstring_len(&tmp_buff), false);
|
||||
|
||||
int ch;
|
||||
scc_cstring_t real_buff = scc_cstring_new();
|
||||
|
||||
while ((ch = scc_probe_stream_peek(tmp_stream)) != scc_stream_eof) {
|
||||
if (scc_lex_parse_is_identifier_prefix(ch)) {
|
||||
// 递归检查
|
||||
scc_probe_stream_t *tmp_out_stream;
|
||||
scc_pp_expand_macro(tmp_stream, macros, &tmp_out_stream, depth - 1);
|
||||
if (scc_pp_expand_macro(tmp_stream, macros, expand_stack,
|
||||
&tmp_out_stream, depth - 1) == false) {
|
||||
return false;
|
||||
}
|
||||
// scc_cstring_append_cstr();
|
||||
Assert(tmp_out_stream != null);
|
||||
while (scc_probe_stream_peek(tmp_out_stream) != scc_stream_eof) {
|
||||
@@ -462,6 +475,9 @@ cbool scc_pp_expand_macro(scc_probe_stream_t *stream, scc_macro_table_t *macros,
|
||||
scc_probe_stream_drop(tmp_stream);
|
||||
*out_stream = scc_mem_probe_stream_new(scc_cstring_as_cstr(&real_buff),
|
||||
scc_cstring_len(&real_buff), false);
|
||||
|
||||
// 已经展开的将被标记并出栈
|
||||
scc_pp_macro_table_remove(expand_stack, ¯o->name);
|
||||
return true;
|
||||
ERR:
|
||||
*out_stream = null;
|
||||
|
||||
@@ -30,9 +30,23 @@ RETRY:
|
||||
&stream->self->macro_table);
|
||||
goto RETRY;
|
||||
} else if (scc_lex_parse_is_identifier_prefix(ch)) {
|
||||
scc_pp_expand_macro(stream->input, &stream->self->macro_table,
|
||||
&stream->tmp_stream, MAX_MACRO_EXPANSION_DEPTH);
|
||||
scc_pp_macro_table_t tmp_table;
|
||||
scc_pp_marco_table_init(&tmp_table);
|
||||
cbool ret = scc_pp_expand_macro(
|
||||
stream->input, &stream->self->macro_table, &tmp_table,
|
||||
&stream->tmp_stream, MAX_MACRO_EXPANSION_DEPTH);
|
||||
scc_pp_macro_table_drop(&tmp_table);
|
||||
if (ret == false) {
|
||||
LOG_ERROR("macro_expand_error");
|
||||
}
|
||||
goto READ_BUF;
|
||||
} else if (scc_probe_stream_next(stream->input) == '/') {
|
||||
ch = scc_probe_stream_peek(stream->input);
|
||||
if (ch == '/') {
|
||||
scc_lex_parse_skip_line(stream->input, &stream->pos);
|
||||
} else if (ch == '*') {
|
||||
scc_lex_parse_skip_block_comment(stream->input, &stream->pos);
|
||||
}
|
||||
}
|
||||
|
||||
// 非标识符字符,直接消费并返回
|
||||
|
||||
Reference in New Issue
Block a user