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

@@ -44,28 +44,34 @@ static inline int keyword_cmp(const char *name, int len) {
return -1; // Not a keyword.
}
void scc_pproc_parse_macro_arguments(scc_pproc_t *pp,
scc_pproc_macro_list_t *args) {
Assert(pp != null && args != null);
void scc_pproc_parse_macro_arguments(scc_lexer_tok_ring_t *ring,
scc_lexer_tok_vec_t *args, int need_full) {
Assert(ring != null && args != null);
scc_lexer_tok_t tok = {0};
scc_vec_init(*args);
int depth = 0;
int ok;
do {
scc_lexer_next_non_blank(pp->cur_ring, &tok);
if (tok.type == SCC_TOK_L_PAREN) {
depth++;
} else if (tok.type == SCC_TOK_R_PAREN) {
scc_ring_next_consume(*ring, tok, ok);
if (!ok) {
return;
}
// scc_lexer_next_non_blank(ring, &tok);
if (tok.type == SCC_TOK_R_PAREN) {
depth--;
}
if (depth > 1) {
if (depth > 0 || need_full) {
scc_vec_push(*args, tok);
} else {
scc_lexer_tok_drop(&tok);
}
if (tok.type == SCC_TOK_L_PAREN) {
depth++;
}
} while (depth);
}
static inline void fill_replacements(scc_pproc_t *pp, scc_pp_macro_t *macro) {
static inline void fill_replacements(scc_pproc_t *pp,
scc_pproc_macro_t *macro) {
int ok;
scc_lexer_tok_t tok;
ok = scc_lexer_next_non_blank(pp->cur_ring, &tok);
@@ -88,50 +94,56 @@ static inline void fill_replacements(scc_pproc_t *pp, scc_pp_macro_t *macro) {
void scc_pproc_parse_function_macro(scc_pproc_t *pp,
const scc_lexer_tok_t *ident) {
scc_pproc_macro_list_t args;
scc_pproc_parse_macro_arguments(pp, &args);
scc_pp_macro_t *macro =
scc_pp_macro_new(&ident->lexeme, SCC_PP_MACRO_FUNCTION);
scc_lexer_tok_vec_t args;
scc_vec_init(args);
scc_pproc_parse_macro_arguments(pp->cur_ring, &args, false);
scc_pproc_macro_t *macro =
scc_pproc_macro_new(&ident->lexeme, SCC_PP_MACRO_FUNCTION);
/*
check and set params
1. identifier-list(opt)
2. ...
3. identifier-list , ...
*/
int idx = 0;
scc_vec_foreach(args, i) {
scc_lexer_tok_t *arg = &scc_vec_at(args, i);
if (arg->type == SCC_TOK_COMMA) {
scc_lexer_tok_drop(arg);
if (i % 2 != 1) {
if (idx++ % 2 != 1) {
LOG_FATAL("ERROR");
}
} else if (arg->type == SCC_TOK_IDENT) {
if (i % 2 != 0) {
if (idx++ % 2 != 0) {
LOG_FATAL("ERROR");
}
scc_vec_push(macro->params, *arg);
} else if (arg->type == SCC_TOK_ELLIPSIS) {
if (i % 2 != 0) {
if (idx++ % 2 != 0) {
LOG_FATAL("ERROR");
}
scc_cstring_t va_args = scc_cstring_from_cstr("__VA_ARGS__");
scc_cstring_free(&arg->lexeme);
arg->lexeme = va_args;
scc_vec_push(macro->params, *arg);
} else if (scc_get_tok_subtype(arg->type) ==
SCC_TOK_SUBTYPE_EMPTYSPACE ||
scc_get_tok_subtype(arg->type) == SCC_TOK_SUBTYPE_COMMENT) {
scc_lexer_tok_drop(arg);
} else {
LOG_FATAL("ERROR");
}
}
fill_replacements(pp, macro);
scc_pp_macro_table_set(&pp->macro_table, macro);
scc_pproc_macro_table_set(&pp->macro_table, macro);
}
void scc_pproc_parse_object_macro(scc_pproc_t *pp,
const scc_lexer_tok_t *ident) {
scc_pp_macro_t *macro =
scc_pp_macro_new(&ident->lexeme, SCC_PP_MACRO_OBJECT);
scc_pproc_macro_t *macro =
scc_pproc_macro_new(&ident->lexeme, SCC_PP_MACRO_OBJECT);
fill_replacements(pp, macro);
scc_pp_macro_table_set(&pp->macro_table, macro);
scc_pproc_macro_table_set(&pp->macro_table, macro);
}
/*
@@ -245,7 +257,7 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
LOG_ERROR("expected identifier");
goto ERROR;
}
scc_pp_macro_table_remove(&pp->macro_table, &tok.lexeme);
scc_pproc_macro_table_remove(&pp->macro_table, &tok.lexeme);
scc_lexer_tok_drop(&tok);
scc_lexer_next_non_blank(pp->cur_ring, &tok);
if (tok.type != SCC_TOK_ENDLINE) {
@@ -254,7 +266,7 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
goto ERROR;
}
scc_lexer_tok_drop(&tok);
break;
return;
}
case SCC_PP_TOK_INCLUDE:
case SCC_PP_TOK_IF:

View File

@@ -1,32 +1,263 @@
#include <pproc_expand.h>
#include <scc_pproc.h>
typedef struct {
static scc_lexer_tok_t stringify_argument(scc_lexer_tok_vec_t *arg_tokens) {
scc_cstring_t str = scc_cstring_create();
scc_cstring_append_ch(&str, '\"'); // 左引号
} scc_expand_t;
void scc_pproc_expand_macro(scc_pproc_t *pp, const scc_pp_macro_t *macro) {
if (macro->type == SCC_PP_MACRO_NONE) {
UNREACHABLE();
}
if (macro->type == SCC_PP_MACRO_OBJECT) {
scc_vec_foreach(macro->replaces, i) {
scc_lexer_tok_t tok = scc_vec_at(macro->replaces, i);
if (tok.type == SCC_TOK_BLANK) {
tok.lexeme = scc_cstring_from_cstr(" ");
} else {
tok.lexeme = scc_cstring_copy(&tok.lexeme);
}
scc_vec_push(pp->cache, tok);
int need_space = 0; // 是否需要插入空格
for (usize i = 0; i < arg_tokens->size; i++) {
scc_lexer_tok_t *tok = &scc_vec_at(*arg_tokens, i);
if (tok->type == SCC_TOK_BLANK) {
need_space = 1; // 标记遇到空白
continue;
}
pp->cache_pos = 0;
return;
// 需要空格且当前不是第一个有效token插入一个空格
if (need_space && i > 0) {
scc_cstring_append_ch(&str, ' ');
need_space = 0;
}
// 对字符串/字符常量内的 " 和 \ 进行转义
if (tok->type == SCC_TOK_STRING_LITERAL ||
tok->type == SCC_TOK_CHAR_LITERAL) {
// 注意lex包含两端的引号需要跳过首尾转义内部字符
// 简化:暂不处理内部转义,直接追加
}
scc_cstring_append(&str, &tok->lexeme);
need_space = 0;
}
scc_cstring_append_ch(&str, '\"'); // 右引号
scc_lexer_tok_t result;
result.type = SCC_TOK_STRING_LITERAL;
result.lexeme = str;
return result;
}
static inline void scc_copy_expand(scc_pproc_expand_t *expand_ctx,
scc_pproc_expand_t *copyed_ctx,
scc_lexer_tok_ring_t *ring) {
copyed_ctx->input = ring;
copyed_ctx->expanded_set = expand_ctx->expanded_set;
copyed_ctx->macro_table = expand_ctx->macro_table;
copyed_ctx->need_rescan = false;
scc_vec_init(copyed_ctx->output);
}
void scc_pproc_expand_by_src(scc_pproc_t *pp, const scc_pproc_macro_t *macro) {
scc_pproc_expand_t ctx;
scc_lexer_tok_vec_t expaned_buffer;
scc_vec_init(expaned_buffer);
int ok;
scc_lexer_tok_t tok;
scc_ring_next_consume(*pp->cur_ring, tok, ok);
if (macro->type == SCC_PP_MACRO_NONE || ok == false) {
UNREACHABLE();
} else if (macro->type == SCC_PP_MACRO_OBJECT) {
scc_vec_push(expaned_buffer, tok);
} else if (macro->type == SCC_PP_MACRO_FUNCTION) {
scc_vec_push(expaned_buffer, tok);
scc_pproc_parse_macro_arguments(pp->cur_ring, &expaned_buffer, true);
}
scc_lexer_tok_ring_t ring = scc_lexer_array_to_ring(&expaned_buffer);
ctx.input = &ring;
ctx.macro_table = &pp->macro_table;
ctx.need_rescan = false;
scc_vec_init(ctx.output);
scc_pproc_marco_table_init(&ctx.expanded_set);
scc_pproc_expand_macro(&ctx);
pp->expanded_ring = scc_lexer_array_to_ring(&ctx.output);
scc_pproc_macro_table_drop(&ctx.expanded_set);
}
static inline void
split_arguments(scc_pproc_macro_extened_params_t *splited_params,
scc_lexer_tok_vec_t *raw_args) {
scc_lexer_tok_vec_t arg;
scc_vec_init(arg);
scc_vec_foreach(*raw_args, i) {
scc_lexer_tok_t *raw_arg = &scc_vec_at(*raw_args, i);
if (raw_arg->type == SCC_TOK_COMMA) {
scc_lexer_tok_drop(raw_arg);
scc_vec_push(*splited_params, arg);
scc_vec_init(arg);
} else {
if (raw_arg->type == SCC_TOK_BLANK ||
scc_get_tok_subtype(raw_arg->type) ==
SCC_TOK_SUBTYPE_EMPTYSPACE) {
scc_lexer_tok_drop(raw_arg);
}
scc_vec_push(arg, *raw_arg);
}
}
scc_vec_push(*splited_params, arg);
}
static inline void
expand_arguments(scc_pproc_macro_extened_params_t *expanded_params,
scc_pproc_macro_extened_params_t *splited_params,
scc_pproc_expand_t *expand_ctx) {
scc_vec_foreach(*splited_params, i) {
scc_pproc_expand_t ctx;
scc_lexer_tok_vec_t splite_param = scc_vec_at(*splited_params, i);
scc_lexer_tok_vec_t expanded_param;
scc_vec_init(expanded_param);
scc_vec_foreach(splite_param, j) {
scc_lexer_tok_t tok = scc_vec_at(splite_param, j);
tok.lexeme = scc_cstring_copy(&tok.lexeme);
scc_vec_push(expanded_param, tok);
}
scc_lexer_tok_ring_t ring = scc_lexer_array_to_ring(&expanded_param);
scc_copy_expand(expand_ctx, &ctx, &ring);
scc_pproc_expand_macro(&ctx);
scc_vec_push(*expanded_params, ctx.output);
}
}
static inline void
expanded_params_free(scc_pproc_macro_extened_params_t *expanded_params) {
scc_vec_foreach(*expanded_params, i) {
scc_lexer_tok_vec_t expanded_param = scc_vec_at(*expanded_params, i);
scc_vec_foreach(expanded_param, j) {
scc_lexer_tok_t tok = scc_vec_at(expanded_param, j);
scc_lexer_tok_drop(&tok);
}
scc_vec_free(expanded_param);
}
scc_vec_free(*expanded_params);
}
static inline void expand_function_macro(scc_pproc_expand_t *expand_ctx,
const scc_pproc_macro_t *macro) {
Assert(macro->type == SCC_PP_MACRO_FUNCTION);
scc_lexer_tok_vec_t raw_args;
scc_vec_init(raw_args);
scc_pproc_parse_macro_arguments(expand_ctx->input, &raw_args, false);
// collect, fill and expand arg
scc_pproc_macro_extened_params_t splited_params;
scc_vec_init(splited_params);
split_arguments(&splited_params, &raw_args);
scc_pproc_macro_extened_params_t expanded_params;
scc_vec_init(expanded_params);
expand_arguments(&expanded_params, &splited_params, expand_ctx);
// replace
scc_vec_foreach(macro->replaces, i) {
scc_lexer_tok_t tok = scc_vec_at(macro->replaces, i);
scc_lexer_tok_t prev_tok = {0};
if (i >= 1) {
prev_tok = scc_vec_at(macro->replaces, i - 1);
}
if (tok.type == SCC_TOK_BLANK) {
tok.lexeme = scc_cstring_from_cstr(" ");
scc_vec_push(expand_ctx->output, tok);
continue;
}
scc_vec_foreach(macro->params, j) {
if (scc_cstring_cmp(&tok.lexeme,
&(scc_vec_at(macro->params, j).lexeme)) == 0) {
if (j >= scc_vec_size(expanded_params)) {
LOG_ERROR("Invalid macro parameter");
goto CONTINUE;
}
if (prev_tok.type == SCC_TOK_SHARP) {
// # stringify
scc_lexer_tok_t out =
stringify_argument(&scc_vec_at(splited_params, j));
scc_vec_pop(expand_ctx->output);
scc_vec_push(expand_ctx->output, out);
} else {
scc_lexer_tok_vec_t expanded_param =
scc_vec_at(expanded_params, j);
scc_vec_foreach(expanded_param, k) {
tok = scc_vec_at(expanded_param, k);
tok.lexeme = scc_cstring_copy(&tok.lexeme);
scc_vec_push(expand_ctx->output, tok);
}
}
goto CONTINUE;
}
}
tok.lexeme = scc_cstring_copy(&tok.lexeme);
scc_vec_push(expand_ctx->output, tok);
CONTINUE:
continue;
}
expanded_params_free(&splited_params);
expanded_params_free(&expanded_params);
}
static inline void expand_object_macro(scc_pproc_expand_t *expand_ctx,
const scc_pproc_macro_t *macro) {
scc_vec_foreach(macro->replaces, i) {
scc_lexer_tok_t tok = scc_vec_at(macro->replaces, i);
if (tok.type == SCC_TOK_BLANK) {
tok.lexeme = scc_cstring_from_cstr(" ");
} else {
tok.lexeme = scc_cstring_copy(&tok.lexeme);
}
scc_vec_push(expand_ctx->output, tok);
}
}
void scc_pproc_expand_macro(scc_pproc_expand_t *expand_ctx) {
int ok;
scc_lexer_tok_t tok;
while (1) {
scc_ring_next_consume(*expand_ctx->input, tok, ok);
if (!ok) {
return;
}
if (tok.type != SCC_TOK_IDENT) {
scc_vec_push(expand_ctx->output, tok);
continue;
}
// maybe expanded
scc_pproc_macro_t *macro =
scc_pproc_macro_table_get(expand_ctx->macro_table, &tok.lexeme);
if (macro == null || scc_pproc_macro_table_get(
&expand_ctx->expanded_set, &macro->name)) {
scc_vec_push(expand_ctx->output, tok);
continue;
}
expand_ctx->need_rescan = true;
scc_pproc_macro_t *expanded_macro =
scc_pproc_macro_new(&macro->name, macro->type);
if (expanded_macro == null) {
LOG_FATAL("Out of memory");
}
scc_pproc_macro_table_set(&expand_ctx->expanded_set, expanded_macro);
if (macro->type == SCC_PP_MACRO_OBJECT) {
expand_object_macro(expand_ctx, macro);
} else if (macro->type == SCC_PP_MACRO_FUNCTION) {
expand_function_macro(expand_ctx, macro);
} else {
UNREACHABLE();
}
RESCAN:
scc_pproc_macro_table_remove(&expand_ctx->expanded_set, &macro->name);
// TODO expand # and ##
continue;
}
if (expand_ctx->need_rescan) {
expand_ctx->need_rescan = false;
scc_pproc_expand_t rescan_ctx;
scc_lexer_tok_ring_t ring =
scc_lexer_array_to_ring(&expand_ctx->output);
scc_copy_expand(expand_ctx, &rescan_ctx, &ring);
scc_pproc_expand_macro(&rescan_ctx);
scc_ring_free(ring);
expand_ctx->output = rescan_ctx.output;
}
Assert(macro->type == SCC_PP_MACRO_FUNCTION);
// Check params match
scc_pproc_macro_list_t args;
scc_pproc_parse_macro_arguments(pp, &args);
scc_vec_foreach(args, i) {}
scc_vec_foreach(macro->params, i) {}
scc_vec_foreach(macro->replaces, i) {}
}

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);

View File

@@ -5,30 +5,44 @@ static int pproc_next(scc_pproc_t *pp, scc_lexer_tok_t *out) {
scc_lexer_tok_t tok = {0};
int ok = 0;
CONTINUE:
if (scc_vec_size(pp->cache)) {
// use cache?
*out = scc_vec_at(pp->cache, pp->cache_pos);
pp->cache_pos++;
if (pp->cache_pos == scc_vec_size(pp->cache)) {
pp->cache_pos = 0;
scc_vec_free(pp->cache);
if (pp->expanded_ring.cap) {
scc_ring_next_consume(pp->expanded_ring, *out, ok);
if (ok == false) {
scc_ring_free(pp->expanded_ring);
goto CONTINUE;
} else {
return true;
}
return true;
}
scc_ring_peek(*stream, tok, ok);
if (tok.type == SCC_TOK_SHARP && tok.loc.col == 1) {
if (tok.type == SCC_TOK_BLANK ||
scc_get_tok_subtype(tok.type) == SCC_TOK_SUBTYPE_COMMENT) {
scc_cstring_free(&tok.lexeme);
scc_ring_next_consume(*stream, *out, ok);
out->lexeme = scc_cstring_from_cstr(" ");
return true;
} else if (tok.type == SCC_TOK_ENDLINE) {
scc_ring_next_consume(*stream, *out, ok);
pp->at_line_start = true;
return true;
}
if (pp->at_line_start && tok.type == SCC_TOK_SHARP) {
// parse to #
scc_pproc_handle_directive(pp);
goto CONTINUE;
} else if (tok.type == SCC_TOK_IDENT) {
}
pp->at_line_start = false;
if (tok.type == SCC_TOK_IDENT) {
// maybe expanded
scc_pp_macro_t *macro =
scc_pp_macro_table_get(&pp->macro_table, &tok.lexeme);
scc_ring_next_consume(*stream, *out, ok);
scc_pproc_macro_t *macro =
scc_pproc_macro_table_get(&pp->macro_table, &tok.lexeme);
if (macro == null) {
scc_ring_next_consume(*stream, *out, ok);
return ok;
}
scc_pproc_expand_macro(pp, macro);
scc_pproc_expand_by_src(pp, macro);
goto CONTINUE;
} else {
// continue
@@ -41,11 +55,11 @@ CONTINUE:
void scc_pproc_init(scc_pproc_t *pp, scc_lexer_tok_ring_t *input) {
Assert(pp != null && input != null);
pp->cur_ring = input;
scc_pp_marco_table_init(&pp->macro_table);
scc_ring_init(pp->expanded_ring, 0, 0, 0);
scc_pproc_marco_table_init(&pp->macro_table);
scc_vec_init(pp->if_stack);
scc_vec_init(pp->file_stack);
scc_vec_init(pp->cache);
pp->cache_pos = 0;
pp->at_line_start = true;
}
static cbool fill_token(scc_lexer_tok_t *tok, void *userdata) {
@@ -64,5 +78,5 @@ void scc_pproc_drop(scc_pproc_t *pp) {
if (pp == null)
return;
scc_lexer_drop_ring(pp->cur_ring);
scc_pp_macro_table_drop(&pp->macro_table);
scc_pproc_macro_table_drop(&pp->macro_table);
}