refactor(format): 移除SCF格式相关文件

移除了libs/format目录下的所有文件,包括:
- cbuild.toml构建配置文件
- include/scf.h头文件
- include/scf_impl.h实现头文件
- src/scf.c源文件
- tests/test_scf.c测试文件
- tests/test_scf_x64.c x64架构测试文件

这些文件包含了SCF(scc format)格式的完整实现,但现在不再需要。

feat(lexer): 添加布尔字面量数字生成函数

在lexer工具头文件中添加了两个内联函数用于生成布尔值的数字字面量:
- scc_lexer_gen_number_true: 将token类型设为整数字面量,值为"1"
- scc_lexer_gen_number_false: 将token类型设为整数字面量,值为"0"

refactor(lexer): 改进词法分析器错误处理

- 移除了多余的头文件包含
- 更新错误报告方式,使用SCC_ERROR宏替代LEX_ERROR,提供更准确的错误位置信息

refactor(pproc): 更新预处理器扩展器数据结构

- 将need_rescan字段类型从int改为cbool
- 添加need_parse_defined字段用于控制defined操作符解析
- 更新函数签名以支持defined操作符解析参数
This commit is contained in:
zzy
2026-02-26 10:25:45 +08:00
parent d2eaf2247f
commit f56b13da2c
19 changed files with 215 additions and 1023 deletions

View File

@@ -11,7 +11,8 @@ typedef struct {
scc_pproc_macro_table_t *expanded_set;
scc_lexer_tok_ring_t *input;
scc_lexer_tok_vec_t output;
int need_rescan;
cbool need_rescan;
cbool need_parse_defined;
} scc_pproc_expand_t;
static inline scc_lexer_tok_ring_t
@@ -28,6 +29,7 @@ void scc_pproc_expand_by_src(scc_pproc_macro_table_t *macro_table,
const scc_pproc_macro_t *macro);
void scc_pproc_expand_by_vec(scc_pproc_macro_table_t *macro_table,
scc_lexer_tok_vec_t *input,
scc_lexer_tok_ring_t *output);
scc_lexer_tok_ring_t *output,
cbool need_parse_defined);
#endif /* __SCC_PPROC_EXPAND_H__ */

View File

@@ -152,7 +152,8 @@ void scc_pproc_parse_object_macro(scc_pproc_t *pp,
}
static void scc_pproc_parse_line_and_expand(scc_pproc_t *pp,
scc_lexer_tok_ring_t *out_ring) {
scc_lexer_tok_ring_t *out_ring,
cbool need_parse_defined) {
int ok;
scc_lexer_tok_t tok;
scc_lexer_tok_ring_t *stream = pp->cur_ring;
@@ -167,7 +168,8 @@ static void scc_pproc_parse_line_and_expand(scc_pproc_t *pp,
if (tok.type == SCC_TOK_ENDLINE)
break;
}
scc_pproc_expand_by_vec(&pp->macro_table, &org_toks, out_ring);
scc_pproc_expand_by_vec(&pp->macro_table, &org_toks, out_ring,
need_parse_defined);
}
/*
@@ -236,14 +238,15 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
if (!scc_lexer_next_non_blank(pp->cur_ring, &tok) ||
scc_get_tok_subtype(tok.type) != SCC_TOK_SUBTYPE_IDENTIFIER) {
scc_lexer_tok_drop(&tok);
LOG_ERROR("Invalid preprocessor directive");
SCC_ERROR(tok.loc, "invalid preprocessing directive");
goto ERROR;
}
int ret = keyword_cmp(scc_cstring_as_cstr(&tok.lexeme),
scc_cstring_len(&tok.lexeme));
if (ret == -1) {
scc_lexer_tok_drop(&tok);
LOG_ERROR("Expected preprocessor keyword, got %s", tok.lexeme);
SCC_ERROR(tok.loc, "expected preprocessor directive name, got '%s'",
scc_cstring_as_cstr(&tok.lexeme));
goto ERROR;
}
scc_tok_type_t type = keywords[ret].tok_type;
@@ -278,15 +281,15 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
case SCC_PP_TOK_DEFINE: {
scc_lexer_tok_drop(&tok);
scc_lexer_next_non_blank(pp->cur_ring, &tok);
if (tok.type != SCC_TOK_IDENT) {
if (scc_get_tok_subtype(tok.type) != SCC_TOK_SUBTYPE_IDENTIFIER) {
SCC_ERROR(tok.loc, "expected identifier after '#define'");
scc_lexer_tok_drop(&tok);
LOG_ERROR("expected identifier");
goto ERROR;
}
scc_lexer_tok_t next_tok;
scc_ring_peek(*pp->cur_ring, next_tok, ok);
if (!ok) {
LOG_ERROR("unexpected EOF");
SCC_ERROR(tok.loc, "unexpected end of file in macro definition");
goto ERROR;
}
if (next_tok.type == SCC_TOK_L_PAREN) {
@@ -305,7 +308,7 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
scc_lexer_next_non_blank(pp->cur_ring, &tok);
if (tok.type != SCC_TOK_IDENT) {
scc_lexer_tok_drop(&tok);
LOG_ERROR("expected identifier");
SCC_ERROR(tok.loc, "expected identifier after '#undef'");
goto ERROR;
}
scc_pproc_macro_table_remove(&pp->macro_table, &tok.lexeme);
@@ -313,7 +316,7 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
scc_lexer_next_non_blank(pp->cur_ring, &tok);
if (tok.type != SCC_TOK_ENDLINE) {
scc_lexer_tok_drop(&tok);
LOG_ERROR("expected newline");
SCC_ERROR(tok.loc, "expected newline after '#undef'");
goto ERROR;
}
scc_lexer_tok_drop(&tok);
@@ -321,7 +324,7 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
}
case SCC_PP_TOK_INCLUDE: {
scc_lexer_tok_ring_t out_ring;
scc_pproc_parse_line_and_expand(pp, &out_ring);
scc_pproc_parse_line_and_expand(pp, &out_ring, false);
scc_pproc_parse_include(pp, &tok, &out_ring);
return;
}
@@ -333,7 +336,8 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
if (!scc_lexer_next_non_blank(pp->cur_ring, &tok) ||
tok.type != SCC_TOK_IDENT) {
scc_lexer_tok_drop(&tok);
LOG_ERROR("expected identifier");
SCC_ERROR(tok.loc, "expected identifier after "
"'#ifdef'/'#ifndef'/'#elifdef'/'#elifndef'");
} else {
scc_pproc_parse_if_defined(pp, type, &tok);
scc_lexer_tok_drop(&tok);
@@ -352,7 +356,7 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
case SCC_PP_TOK_ELIF: {
scc_lexer_tok_drop(&tok);
scc_lexer_tok_ring_t out_ring;
scc_pproc_parse_line_and_expand(pp, &out_ring);
scc_pproc_parse_line_and_expand(pp, &out_ring, true);
scc_pproc_parse_if_condition(pp, type, &out_ring);
return;
}
@@ -367,7 +371,7 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
return;
}
if (scc_get_tok_subtype(tok.type) == SCC_TOK_SUBTYPE_LITERAL) {
LOG_ERROR(scc_cstring_as_cstr(&tok.lexeme));
SCC_ERROR(tok.loc, "%s", scc_cstring_as_cstr(&tok.lexeme));
}
scc_lexer_tok_drop(&tok);
}
@@ -380,13 +384,13 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
return;
}
if (scc_get_tok_subtype(tok.type) == SCC_TOK_SUBTYPE_LITERAL) {
LOG_WARN(scc_cstring_as_cstr(&tok.lexeme));
SCC_WARN(tok.loc, "%s", scc_cstring_as_cstr(&tok.lexeme));
}
scc_lexer_tok_drop(&tok);
}
return;
case SCC_PP_TOK_PRAGMA:
LOG_WARN("Pragma ignored");
SCC_WARN(tok.loc, "pragma ignored");
scc_lexer_skip_until_newline(pp->cur_ring);
return;
default:

View File

@@ -1,4 +1,5 @@
#include <pproc_expand.h>
#include <scc_lexer_utils.h>
#include <scc_pproc.h>
static scc_lexer_tok_t stringify_argument(scc_lexer_tok_vec_t *arg_tokens) {
@@ -78,6 +79,7 @@ static inline void scc_copy_expand(scc_pproc_expand_t *expand_ctx,
copyed_ctx->expanded_set = expand_ctx->expanded_set;
copyed_ctx->macro_table = expand_ctx->macro_table;
copyed_ctx->need_rescan = false;
copyed_ctx->need_parse_defined = expand_ctx->need_parse_defined;
scc_vec_init(copyed_ctx->output);
}
@@ -100,17 +102,19 @@ void scc_pproc_expand_by_src(scc_pproc_macro_table_t *macro_table,
scc_pproc_parse_macro_arguments(input, &expaned_buffer, true);
}
scc_pproc_expand_by_vec(macro_table, &expaned_buffer, output);
scc_pproc_expand_by_vec(macro_table, &expaned_buffer, output, false);
}
void scc_pproc_expand_by_vec(scc_pproc_macro_table_t *macro_table,
scc_lexer_tok_vec_t *input,
scc_lexer_tok_ring_t *output) {
scc_lexer_tok_ring_t *output,
cbool need_parse_defined) {
scc_pproc_expand_t ctx;
scc_lexer_tok_ring_t ring = scc_lexer_array_to_ring(input);
ctx.input = &ring;
ctx.macro_table = macro_table;
ctx.need_rescan = false;
ctx.need_parse_defined = need_parse_defined;
scc_vec_init(ctx.output);
scc_pproc_macro_table_t expanded_set;
ctx.expanded_set = &expanded_set;
@@ -272,10 +276,12 @@ static inline void expand_function_macro(scc_pproc_expand_t *expand_ctx,
scc_pproc_macro_extened_params_t splited_params;
scc_vec_init(splited_params);
split_arguments(&splited_params, &raw_args, macro);
Assert(scc_vec_size(splited_params) >= scc_vec_size(macro->params));
scc_pproc_macro_extened_params_t expanded_params;
scc_vec_init(expanded_params);
expand_arguments(&expanded_params, &splited_params, expand_ctx);
Assert(scc_vec_size(expanded_params) >= scc_vec_size(macro->params));
// replace
scc_vec_foreach(macro->replaces, i) {
@@ -470,6 +476,57 @@ void scc_pproc_expand_macro(scc_pproc_expand_t *expand_ctx) {
scc_vec_push(expand_ctx->output, tok);
continue;
}
if (expand_ctx->need_parse_defined &&
scc_strcmp(scc_cstring_as_cstr(&tok.lexeme), "defined") == 0) {
scc_lexer_tok_t next_tok = {0};
if (scc_lexer_next_non_blank(expand_ctx->input, &next_tok) ==
false) {
SCC_ERROR(tok.loc, "Unexpected before defined EOF");
scc_lexer_tok_drop(&tok);
}
scc_lexer_tok_drop(&tok);
if (next_tok.type == SCC_TOK_L_PAREN) {
scc_lexer_tok_drop(&next_tok);
scc_lexer_next_non_blank(expand_ctx->input, &next_tok);
if (scc_get_tok_subtype(next_tok.type) !=
SCC_TOK_SUBTYPE_IDENTIFIER) {
SCC_ERROR(next_tok.loc,
"Expected identifier before defined");
scc_lexer_tok_drop(&next_tok);
}
if (scc_pproc_macro_table_get(expand_ctx->macro_table,
&next_tok.lexeme) == null) {
scc_lexer_gen_number_false(&tok);
} else {
scc_lexer_gen_number_true(&tok);
}
scc_lexer_tok_drop(&next_tok);
scc_vec_push(expand_ctx->output, tok);
if (scc_lexer_next_non_blank(expand_ctx->input, &next_tok)) {
if (next_tok.type == SCC_TOK_R_PAREN) {
scc_lexer_tok_drop(&next_tok);
break;
} else {
SCC_ERROR(next_tok.loc, "Expected ')'");
scc_lexer_tok_drop(&next_tok);
}
}
} else if (scc_get_tok_subtype(next_tok.type) ==
SCC_TOK_SUBTYPE_IDENTIFIER) {
if (scc_pproc_macro_table_get(expand_ctx->macro_table,
&next_tok.lexeme) == null) {
scc_lexer_gen_number_false(&tok);
} else {
scc_lexer_gen_number_true(&tok);
}
scc_lexer_tok_drop(&next_tok);
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);

View File

@@ -98,37 +98,33 @@ cbool scc_pproc_parse_if_defined(scc_pproc_t *pp, scc_tok_type_t type,
return true;
}
static cbool parse_constant_condition() { return false; }
cbool scc_pproc_parse_if_condition(scc_pproc_t *pp, scc_tok_type_t type,
scc_lexer_tok_ring_t *tok_ring) {
// TODO
static int parse_constant_condition(scc_pproc_t *pp,
scc_lexer_tok_ring_t *tok_ring) {
int ok;
scc_lexer_tok_t tok = {0};
ok = scc_lexer_next_non_blank(tok_ring, &tok);
if (ok == false) {
LOG_FATAL("unexpected EOF");
}
int condition = 0;
if (tok.type == SCC_TOK_INT_LITERAL) {
condition = scc_cstring_as_cstr(&tok.lexeme)[0] == '0' ? 0 : 1;
} else {
LOG_ERROR("expected integer constant but got %s",
scc_cstring_as_cstr(&tok.lexeme));
}
scc_lexer_tok_drop(&tok);
ok = scc_lexer_next_non_blank(tok_ring, &tok);
if (ok == false) {
LOG_FATAL("unexpected EOF");
}
if (tok.type != SCC_TOK_ENDLINE) {
LOG_ERROR("expected endline");
scc_lexer_skip_until_newline(tok_ring);
} else {
scc_lexer_tok_drop(&tok);
LOG_FATAL("unexpected EOF");
}
int res = 0;
if (tok.type == SCC_TOK_INT_LITERAL) {
// got int
const char *intstr = scc_cstring_as_cstr(&tok.lexeme);
for (int i = scc_cstring_len(&tok.lexeme) - 1; i >= 0; i--) {
res = res * 10 + intstr[i] - '0';
}
return res;
}
scc_ring_free(*tok_ring);
return false;
}
cbool scc_pproc_parse_if_condition(scc_pproc_t *pp, scc_tok_type_t type,
scc_lexer_tok_ring_t *tok_ring) {
int condition = parse_constant_condition(pp, tok_ring) != 0;
// 根据指令类型更新条件编译栈
switch (type) {
@@ -143,7 +139,8 @@ cbool scc_pproc_parse_if_condition(scc_pproc_t *pp, scc_tok_type_t type,
}
case SCC_PP_TOK_ELIF: {
if (scc_vec_size(pp->if_stack) == 0) {
LOG_ERROR("#elif without #if");
SCC_ERROR(pp->cur_ring->data[pp->cur_ring->head].loc,
"#elif without #if");
return false;
}
scc_pproc_if_t *top =
@@ -169,5 +166,4 @@ cbool scc_pproc_parse_if_condition(scc_pproc_t *pp, scc_tok_type_t type,
LOG_FATAL("unexpected directive in parse_if_condition");
}
return true;
return true;
}

View File

@@ -33,15 +33,13 @@ static int switch_file_stack(scc_pproc_t *pp, scc_cstring_t *fname,
goto FOPEN;
}
}
LOG_ERROR("In %s:%d:%d include %c%s%c, the file is not found", org_fname,
pos->line, pos->col, is_system ? '<' : '\"',
scc_cstring_as_cstr(fname), is_system ? '>' : '\"');
SCC_ERROR(*pos, "include file '%s' not found", scc_cstring_as_cstr(fname));
return -1;
FOPEN:
if (scc_vec_size(pp->file_stack) >= pp->config.max_include_depth) {
LOG_FATAL("Include depth is too deep, the include depth is %d, set "
"MAX_INCLUDE_DEPTH is %d",
scc_vec_size(pp->file_stack), pp->config.max_include_depth);
SCC_FATAL(*pos, "include depth exceeds maximum (%d)",
pp->config.max_include_depth);
LOG_FATAL("Include depth is too deep...");
}
scc_pproc_file_t *file = scc_malloc(sizeof(scc_pproc_file_t));
@@ -108,6 +106,7 @@ void scc_pproc_parse_include(scc_pproc_t *pp, scc_lexer_tok_t *include_tok,
scc_cstring_free(&fname);
return;
ERROR:
LOG_ERROR("Invalid include filename need \"FILENAME\" or <FILENAME>");
SCC_ERROR(pos,
"invalid include filename, expected \"FILENAME\" or <FILENAME>");
scc_cstring_free(&line);
}