feat(argparse): 添加列表类型参数支持
新增 scc_argparse_list_t 类型用于处理多个字符串值的参数, 并添加相应的配置函数 scc_argparse_spec_setup_list。 fix(lexer): 调整关键字标记处理逻辑 将关键字子类型从 SCC_TOK_SUBTYPE_KEYWORD 改为 SCC_TOK_SUBTYPE_IDENTIFIER,并移除相关的枚举定义。 refactor(lexer): 优化跳过换行功能实现 修改 scc_lexer_skip_until_newline 函数实现,改进循环逻辑和错误处理。 feat(pproc): 完善预处理器条件编译支持 重构条件编译状态管理,添加对 #ifdef、#ifndef、#elifdef、 #else、#endif 等指令的支持,并实现嵌套条件处理。 refactor(pproc): 优化文件包含路径处理 添加最大包含深度限制,改进包含路径添加功能, 修复文件状态结构命名。 docs(log): 更新日志模块标准库依赖 调整 stdarg.h 包含逻辑,更新编译器内置宏定义名称。 feat(core): 扩展核心类型定义 添加基础数据类型别名定义,完善类型系统支持。 feat(main): 实现命令行包含路径参数 添加 -I/--include 参数支持,允许用户指定额外的头文件搜索路径。
This commit is contained in:
@@ -208,10 +208,11 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
|
||||
scc_lexer_tok_t tok = {0};
|
||||
int ok = 0;
|
||||
scc_ring_next(*pp->cur_ring, tok, ok);
|
||||
Assert(ok == true && tok.type == SCC_TOK_SHARP);
|
||||
scc_lexer_tok_drop(&tok);
|
||||
|
||||
if (!scc_lexer_next_non_blank(pp->cur_ring, &tok) ||
|
||||
tok.type != SCC_TOK_IDENT) {
|
||||
scc_get_tok_subtype(tok.type) != SCC_TOK_SUBTYPE_IDENTIFIER) {
|
||||
scc_lexer_tok_drop(&tok);
|
||||
LOG_ERROR("Invalid preprocessor directive");
|
||||
goto ERROR;
|
||||
@@ -223,8 +224,34 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
|
||||
LOG_ERROR("Expected preprocessor keyword, got %s", tok.lexeme);
|
||||
goto ERROR;
|
||||
}
|
||||
|
||||
scc_tok_type_t type = keywords[ret].tok_type;
|
||||
|
||||
if (scc_vec_size(pp->if_stack) != 0) {
|
||||
scc_pproc_if_t *top =
|
||||
&scc_vec_at(pp->if_stack, scc_vec_size(pp->if_stack) - 1);
|
||||
pp->enable = top->active;
|
||||
}
|
||||
// 非条件指令,且当前不活动时直接跳过整行
|
||||
int is_conditional = 0;
|
||||
switch (type) {
|
||||
case SCC_PP_TOK_IFDEF:
|
||||
case SCC_PP_TOK_IFNDEF:
|
||||
case SCC_PP_TOK_ELIFDEF:
|
||||
case SCC_PP_TOK_ELIFNDEF:
|
||||
case SCC_PP_TOK_ELSE:
|
||||
case SCC_PP_TOK_ENDIF:
|
||||
case SCC_PP_TOK_IF:
|
||||
case SCC_PP_TOK_ELIF:
|
||||
is_conditional = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (!is_conditional && !pp->enable) {
|
||||
scc_lexer_skip_until_newline(pp->cur_ring);
|
||||
return;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case SCC_PP_TOK_DEFINE: {
|
||||
scc_lexer_tok_drop(&tok);
|
||||
@@ -273,15 +300,118 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
|
||||
case SCC_PP_TOK_INCLUDE:
|
||||
scc_pproc_parse_include(pp, &tok);
|
||||
return;
|
||||
case SCC_PP_TOK_IF:
|
||||
case SCC_PP_TOK_IFDEF:
|
||||
case SCC_PP_TOK_IFNDEF:
|
||||
case SCC_PP_TOK_ELSE:
|
||||
case SCC_PP_TOK_ELIF:
|
||||
case SCC_PP_TOK_IFNDEF: {
|
||||
// 解析标识符
|
||||
if (!scc_lexer_next_non_blank(pp->cur_ring, &tok) ||
|
||||
tok.type != SCC_TOK_IDENT) {
|
||||
scc_lexer_tok_drop(&tok);
|
||||
LOG_ERROR("expected identifier");
|
||||
goto ERROR;
|
||||
}
|
||||
cbool defined =
|
||||
(scc_pproc_macro_table_get(&pp->macro_table, &tok.lexeme) != null);
|
||||
cbool condition = (type == SCC_PP_TOK_IFDEF) ? defined : !defined;
|
||||
scc_lexer_tok_drop(&tok);
|
||||
|
||||
scc_pproc_if_t new_if;
|
||||
new_if.found_true = condition;
|
||||
new_if.seen_else = 0;
|
||||
new_if.active = pp->enable ? condition : 0;
|
||||
scc_vec_push(pp->if_stack, new_if);
|
||||
|
||||
pp->enable = new_if.active;
|
||||
scc_lexer_skip_until_newline(pp->cur_ring);
|
||||
return;
|
||||
}
|
||||
case SCC_PP_TOK_ELIFDEF:
|
||||
case SCC_PP_TOK_ELIFNDEF:
|
||||
case SCC_PP_TOK_ENDIF:
|
||||
goto ERROR;
|
||||
case SCC_PP_TOK_ELIFNDEF: {
|
||||
if (scc_vec_size(pp->if_stack) == 0) {
|
||||
LOG_ERROR("#elif without #if");
|
||||
goto ERROR;
|
||||
}
|
||||
scc_pproc_if_t *top =
|
||||
&scc_vec_at(pp->if_stack, scc_vec_size(pp->if_stack) - 1);
|
||||
if (top->seen_else) {
|
||||
LOG_ERROR("#elif after #else");
|
||||
goto ERROR;
|
||||
}
|
||||
// 解析标识符
|
||||
if (!scc_lexer_next_non_blank(pp->cur_ring, &tok) ||
|
||||
tok.type != SCC_TOK_IDENT) {
|
||||
scc_lexer_tok_drop(&tok);
|
||||
LOG_ERROR("expected identifier");
|
||||
goto ERROR;
|
||||
}
|
||||
int defined =
|
||||
(scc_pproc_macro_table_get(&pp->macro_table, &tok.lexeme) != NULL);
|
||||
int condition = (type == SCC_PP_TOK_ELIFDEF) ? defined : !defined;
|
||||
scc_lexer_tok_drop(&tok);
|
||||
|
||||
if (top->found_true) {
|
||||
// 前面已有真分支,本 elif 不激活
|
||||
top->active = 0;
|
||||
} else {
|
||||
if (condition) {
|
||||
top->active = 1;
|
||||
top->found_true = 1;
|
||||
} else {
|
||||
top->active = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// seen_else 仍为 0
|
||||
pp->enable = top->active;
|
||||
scc_lexer_skip_until_newline(pp->cur_ring);
|
||||
return;
|
||||
}
|
||||
case SCC_PP_TOK_ELSE: {
|
||||
if (scc_vec_size(pp->if_stack) == 0) {
|
||||
LOG_ERROR("#else without #if");
|
||||
goto ERROR;
|
||||
}
|
||||
scc_pproc_if_t *top =
|
||||
&scc_vec_at(pp->if_stack, scc_vec_size(pp->if_stack) - 1);
|
||||
if (top->seen_else) {
|
||||
LOG_ERROR("multiple #else");
|
||||
goto ERROR;
|
||||
}
|
||||
|
||||
if (top->found_true) {
|
||||
top->active = 0;
|
||||
} else {
|
||||
top->active = 1;
|
||||
top->found_true = 1;
|
||||
}
|
||||
|
||||
top->seen_else = 1;
|
||||
|
||||
pp->enable = top->active;
|
||||
scc_lexer_skip_until_newline(pp->cur_ring);
|
||||
return;
|
||||
}
|
||||
case SCC_PP_TOK_ENDIF: {
|
||||
if (scc_vec_size(pp->if_stack) == 0) {
|
||||
LOG_ERROR("#endif without #if");
|
||||
} else {
|
||||
scc_vec_pop(pp->if_stack);
|
||||
}
|
||||
if (scc_vec_size(pp->if_stack) == 0) {
|
||||
pp->enable = 1;
|
||||
} else {
|
||||
pp->enable =
|
||||
scc_vec_at(pp->if_stack, scc_vec_size(pp->if_stack) - 1).active;
|
||||
}
|
||||
scc_lexer_skip_until_newline(pp->cur_ring);
|
||||
return;
|
||||
}
|
||||
case SCC_PP_TOK_IF:
|
||||
case SCC_PP_TOK_ELIF:
|
||||
// parse constant expression
|
||||
// 暂不实现表达式求值
|
||||
LOG_ERROR("Expression in #if/#elif not implemented");
|
||||
scc_lexer_skip_until_newline(pp->cur_ring);
|
||||
return;
|
||||
case SCC_PP_TOK_LINE:
|
||||
case SCC_PP_TOK_EMBED:
|
||||
goto ERROR;
|
||||
@@ -297,6 +427,7 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
|
||||
}
|
||||
scc_lexer_tok_drop(&tok);
|
||||
}
|
||||
return;
|
||||
case SCC_PP_TOK_WARNING:
|
||||
scc_lexer_tok_drop(&tok);
|
||||
while (1) {
|
||||
@@ -309,6 +440,7 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
|
||||
}
|
||||
scc_lexer_tok_drop(&tok);
|
||||
}
|
||||
return;
|
||||
case SCC_PP_TOK_PRAGMA:
|
||||
LOG_WARN("Pragma ignored");
|
||||
break;
|
||||
|
||||
@@ -25,6 +25,7 @@ static int switch_file_stack(scc_pproc_t *pp, scc_cstring_t *fname,
|
||||
scc_cstring_free(&fpath);
|
||||
scc_cstring_t *syspath = &scc_vec_at(pp->include_paths, i);
|
||||
scc_cstring_append(&fpath, syspath);
|
||||
scc_cstring_append_ch(&fpath, '/');
|
||||
scc_cstring_append(&fpath, fname);
|
||||
ret = scc_fexists(scc_cstring_as_cstr(&fpath));
|
||||
if (ret == true) {
|
||||
@@ -36,7 +37,13 @@ static int switch_file_stack(scc_pproc_t *pp, scc_cstring_t *fname,
|
||||
scc_cstring_as_cstr(fname), is_system ? '>' : '\"');
|
||||
return -1;
|
||||
FOPEN:
|
||||
scc_pproc_file_state_t *file = scc_malloc(sizeof(scc_pproc_file_state_t));
|
||||
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_pproc_file_t *file = scc_malloc(sizeof(scc_pproc_file_t));
|
||||
Assert(file != null);
|
||||
if (scc_sstream_init(&(file->sstream), scc_cstring_as_cstr(&fpath), 1024)) {
|
||||
return -1;
|
||||
@@ -53,7 +60,7 @@ void scc_pproc_parse_include(scc_pproc_t *pp, scc_lexer_tok_t *include_tok) {
|
||||
int ok;
|
||||
scc_lexer_tok_t tok;
|
||||
scc_pos_t pos = include_tok->loc;
|
||||
scc_lexer_tok_drop(&tok);
|
||||
scc_lexer_tok_drop(include_tok);
|
||||
|
||||
scc_lexer_tok_ring_t *stream = pp->cur_ring;
|
||||
scc_lexer_tok_vec_t org_toks;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <pproc_expand.h>
|
||||
#include <scc_lexer_utils.h>
|
||||
#include <scc_pproc.h>
|
||||
|
||||
static int pproc_next_one_file(scc_pproc_t *pp, scc_lexer_tok_t *out) {
|
||||
@@ -25,13 +26,31 @@ CONTINUE:
|
||||
return true;
|
||||
}
|
||||
|
||||
if (pp->at_line_start && tok.type == SCC_TOK_SHARP) {
|
||||
// parse to #
|
||||
scc_pproc_handle_directive(pp);
|
||||
if (pp->at_line_start) {
|
||||
if (tok.type == SCC_TOK_SHARP) {
|
||||
// parse to #
|
||||
scc_pproc_handle_directive(pp);
|
||||
pp->at_line_start = true;
|
||||
goto CONTINUE;
|
||||
} else if (tok.type == SCC_TOK_BLANK) {
|
||||
scc_ring_next(*stream, *out, ok);
|
||||
scc_ring_peek(*stream, tok, ok);
|
||||
if (ok && tok.type == SCC_TOK_SHARP) {
|
||||
scc_pproc_handle_directive(pp);
|
||||
pp->at_line_start = true;
|
||||
goto CONTINUE;
|
||||
}
|
||||
scc_ring_back(*stream, ok);
|
||||
Assert(ok == true);
|
||||
scc_ring_peek(*stream, tok, ok);
|
||||
}
|
||||
}
|
||||
if (pp->enable == false) {
|
||||
scc_lexer_skip_until_newline(stream);
|
||||
pp->at_line_start = true;
|
||||
goto CONTINUE;
|
||||
}
|
||||
|
||||
pp->at_line_start = false;
|
||||
if (tok.type == SCC_TOK_IDENT) {
|
||||
// maybe expanded
|
||||
scc_pproc_macro_t *macro =
|
||||
@@ -62,7 +81,7 @@ CONTINUE:
|
||||
return false;
|
||||
}
|
||||
|
||||
scc_pproc_file_state_t *file = scc_vec_pop(pp->file_stack);
|
||||
scc_pproc_file_t *file = scc_vec_pop(pp->file_stack);
|
||||
Assert(file->ring == pp->cur_ring);
|
||||
scc_lexer_drop_ring(file->ring);
|
||||
scc_lexer_drop(&(file->lexer));
|
||||
@@ -88,6 +107,9 @@ void scc_pproc_init(scc_pproc_t *pp, scc_lexer_tok_ring_t *input) {
|
||||
scc_vec_init(pp->if_stack);
|
||||
scc_vec_init(pp->file_stack);
|
||||
pp->at_line_start = true;
|
||||
pp->enable = true;
|
||||
|
||||
pp->config.max_include_depth = 32;
|
||||
}
|
||||
|
||||
void scc_pproc_add_builtin_macros() {
|
||||
|
||||
Reference in New Issue
Block a user