Compare commits
3 Commits
a52ff33e30
...
b705e5d0ad
| Author | SHA1 | Date | |
|---|---|---|---|
| b705e5d0ad | |||
| 9c2b4db22a | |||
| bc0b1d23e3 |
@@ -43,6 +43,7 @@ typedef enum scc_argparse_err {
|
||||
SCC_ARGPARSE_ERR_UNKNOWN_VALUE,
|
||||
} scc_argparse_err_t;
|
||||
|
||||
typedef SCC_VEC(const char *) scc_argparse_list_t;
|
||||
// 约束规范结构体
|
||||
struct scc_argparse_spec {
|
||||
scc_argparse_val_type_t value_type; // 值类型
|
||||
@@ -50,12 +51,13 @@ struct scc_argparse_spec {
|
||||
|
||||
// 存储位置
|
||||
union {
|
||||
cbool *bool_store; // 布尔值存储
|
||||
int *int_store; // 整数存储
|
||||
float *float_store; // 浮点数存储
|
||||
const char **str_store; // 字符串存储
|
||||
char **str_alloc_store; // 字符串存储(使用alloc,需要free)
|
||||
void **ptr_store; // 通用指针存储
|
||||
cbool *bool_store; // 布尔值存储
|
||||
int *int_store; // 整数存储
|
||||
float *float_store; // 浮点数存储
|
||||
const char **str_store; // 字符串存储
|
||||
char **str_alloc_store; // 字符串存储(使用alloc,需要free)
|
||||
void **ptr_store; // 通用指针存储
|
||||
scc_argparse_list_t *vec_store; // 新增:指向字符串向量的指针
|
||||
} store;
|
||||
|
||||
// 枚举值约束
|
||||
@@ -215,6 +217,15 @@ static inline void scc_argparse_spec_setup_choices(scc_argparse_spec_t *spec,
|
||||
spec->choices.count = count;
|
||||
}
|
||||
|
||||
// 添加设置列表的辅助函数(内联)
|
||||
static inline void scc_argparse_spec_setup_list(scc_argparse_spec_t *spec,
|
||||
scc_argparse_list_t *vec) {
|
||||
spec->value_type = SCC_ARGPARSE_VAL_TYPE_LIST;
|
||||
spec->store.vec_store = vec;
|
||||
// 自动设置 flag_takes_multiple 为 true,因为列表需要多个值
|
||||
spec->flag_takes_multiple = true;
|
||||
}
|
||||
|
||||
#define SCC_ARGPARSE_MACRO_SETTER(attr) \
|
||||
static inline void scc_argparse_spec_set_##attr(scc_argparse_spec_t *spec, \
|
||||
cbool flag) { \
|
||||
|
||||
@@ -63,6 +63,10 @@ static inline void parse_cmd(scc_optparse_t *optparse,
|
||||
min_args = 0;
|
||||
max_args = 0;
|
||||
break;
|
||||
case SCC_ARGPARSE_VAL_TYPE_LIST: // 列表类型
|
||||
min_args = 1;
|
||||
max_args = 65535; // FIXME maybe INT_MAX ?
|
||||
break;
|
||||
default:
|
||||
min_args = 0;
|
||||
max_args = 0;
|
||||
@@ -161,6 +165,10 @@ static int handle_option(scc_argparse_context_t *ctx, scc_argparse_t *parser) {
|
||||
return SCC_ARGPARSE_ERR_PNT_DEFAULT;
|
||||
}
|
||||
|
||||
if (parser->need_version && scc_strcmp(opt->long_name, "version") == 0) {
|
||||
// TODO default version print
|
||||
}
|
||||
|
||||
if (opt->spec.flag_store_as_count) {
|
||||
(*opt->spec.store.int_store)++;
|
||||
}
|
||||
@@ -169,8 +177,14 @@ static int handle_option(scc_argparse_context_t *ctx, scc_argparse_t *parser) {
|
||||
*opt->spec.store.bool_store = true;
|
||||
}
|
||||
|
||||
if (ctx->result.value) {
|
||||
opt->spec.raw_value = ctx->result.value;
|
||||
if (!ctx->result.value) {
|
||||
return SCC_ARGPARSE_ERR_NONE;
|
||||
}
|
||||
opt->spec.raw_value = ctx->result.value;
|
||||
|
||||
if (opt->spec.value_type == SCC_ARGPARSE_VAL_TYPE_LIST) {
|
||||
scc_vec_push(*opt->spec.store.vec_store, ctx->result.value);
|
||||
} else {
|
||||
*opt->spec.store.str_store = ctx->result.value;
|
||||
}
|
||||
|
||||
|
||||
@@ -42,44 +42,44 @@ typedef enum scc_cstd {
|
||||
// WARNING: Using Binary Search To Fast Find Keyword
|
||||
// 你必须确保其中是按照字典序排列
|
||||
#define SCC_CKEYWORD_TABLE \
|
||||
X(asm , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_ASM , SCC_CEXT_SCC) \
|
||||
X(atomic , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_ATOMIC , SCC_CEXT_SCC) \
|
||||
X(auto , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_AUTO , SCC_CEXT_SCC) \
|
||||
X(bool , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_BOOL , SCC_CEXT_SCC) \
|
||||
X(break , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_BREAK , SCC_CSTD_C89) \
|
||||
X(case , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_CASE , SCC_CSTD_C89) \
|
||||
X(char , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_CHAR , SCC_CSTD_C89) \
|
||||
X(complex , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_COMPLEX , SCC_CEXT_SCC) \
|
||||
X(const , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_CONST , SCC_CSTD_C89) \
|
||||
X(continue , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_CONTINUE , SCC_CSTD_C89) \
|
||||
X(default , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_DEFAULT , SCC_CSTD_C89) \
|
||||
X(do , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_DO , SCC_CSTD_C89) \
|
||||
X(double , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_DOUBLE , SCC_CSTD_C89) \
|
||||
X(else , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_ELSE , SCC_CSTD_C89) \
|
||||
X(enum , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_ENUM , SCC_CSTD_C89) \
|
||||
X(extern , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_EXTERN , SCC_CSTD_C89) \
|
||||
X(float , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_FLOAT , SCC_CSTD_C89) \
|
||||
X(for , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_FOR , SCC_CSTD_C89) \
|
||||
X(goto , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_GOTO , SCC_CSTD_C89) \
|
||||
X(if , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_IF , SCC_CSTD_C89) \
|
||||
X(inline , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_INLINE , SCC_CSTD_C99) \
|
||||
X(int , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_INT , SCC_CSTD_C89) \
|
||||
X(long , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_LONG , SCC_CSTD_C89) \
|
||||
X(register , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_REGISTER , SCC_CSTD_C89) \
|
||||
X(restrict , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_RESTRICT , SCC_CSTD_C99) \
|
||||
X(return , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_RETURN , SCC_CSTD_C89) \
|
||||
X(short , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_SHORT , SCC_CSTD_C89) \
|
||||
X(signed , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_SIGNED , SCC_CSTD_C89) \
|
||||
X(sizeof , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_SIZEOF , SCC_CSTD_C89) \
|
||||
X(static , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_STATIC , SCC_CSTD_C89) \
|
||||
X(struct , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_STRUCT , SCC_CSTD_C89) \
|
||||
X(switch , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_SWITCH , SCC_CSTD_C89) \
|
||||
X(typedef , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_TYPEDEF , SCC_CSTD_C89) \
|
||||
X(union , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_UNION , SCC_CSTD_C89) \
|
||||
X(unsigned , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_UNSIGNED , SCC_CSTD_C89) \
|
||||
X(void , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_VOID , SCC_CSTD_C89) \
|
||||
X(volatile , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_VOLATILE , SCC_CSTD_C89) \
|
||||
X(while , SCC_TOK_SUBTYPE_KEYWORD , SCC_TOK_WHILE , SCC_CSTD_C89) \
|
||||
X(asm , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_ASM , SCC_CEXT_SCC) \
|
||||
X(atomic , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_ATOMIC , SCC_CEXT_SCC) \
|
||||
X(auto , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_AUTO , SCC_CEXT_SCC) \
|
||||
X(bool , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_BOOL , SCC_CEXT_SCC) \
|
||||
X(break , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_BREAK , SCC_CSTD_C89) \
|
||||
X(case , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_CASE , SCC_CSTD_C89) \
|
||||
X(char , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_CHAR , SCC_CSTD_C89) \
|
||||
X(complex , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_COMPLEX , SCC_CEXT_SCC) \
|
||||
X(const , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_CONST , SCC_CSTD_C89) \
|
||||
X(continue , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_CONTINUE , SCC_CSTD_C89) \
|
||||
X(default , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_DEFAULT , SCC_CSTD_C89) \
|
||||
X(do , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_DO , SCC_CSTD_C89) \
|
||||
X(double , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_DOUBLE , SCC_CSTD_C89) \
|
||||
X(else , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_ELSE , SCC_CSTD_C89) \
|
||||
X(enum , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_ENUM , SCC_CSTD_C89) \
|
||||
X(extern , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_EXTERN , SCC_CSTD_C89) \
|
||||
X(float , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_FLOAT , SCC_CSTD_C89) \
|
||||
X(for , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_FOR , SCC_CSTD_C89) \
|
||||
X(goto , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_GOTO , SCC_CSTD_C89) \
|
||||
X(if , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_IF , SCC_CSTD_C89) \
|
||||
X(inline , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_INLINE , SCC_CSTD_C99) \
|
||||
X(int , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_INT , SCC_CSTD_C89) \
|
||||
X(long , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_LONG , SCC_CSTD_C89) \
|
||||
X(register , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_REGISTER , SCC_CSTD_C89) \
|
||||
X(restrict , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_RESTRICT , SCC_CSTD_C99) \
|
||||
X(return , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_RETURN , SCC_CSTD_C89) \
|
||||
X(short , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_SHORT , SCC_CSTD_C89) \
|
||||
X(signed , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_SIGNED , SCC_CSTD_C89) \
|
||||
X(sizeof , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_SIZEOF , SCC_CSTD_C89) \
|
||||
X(static , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_STATIC , SCC_CSTD_C89) \
|
||||
X(struct , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_STRUCT , SCC_CSTD_C89) \
|
||||
X(switch , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_SWITCH , SCC_CSTD_C89) \
|
||||
X(typedef , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_TYPEDEF , SCC_CSTD_C89) \
|
||||
X(union , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_UNION , SCC_CSTD_C89) \
|
||||
X(unsigned , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_UNSIGNED , SCC_CSTD_C89) \
|
||||
X(void , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_VOID , SCC_CSTD_C89) \
|
||||
X(volatile , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_VOLATILE , SCC_CSTD_C89) \
|
||||
X(while , SCC_TOK_SUBTYPE_IDENTIFIER , SCC_TOK_WHILE , SCC_CSTD_C89) \
|
||||
// KEYWORD_TABLE
|
||||
|
||||
#define SCC_CTOK_TABLE \
|
||||
@@ -163,7 +163,6 @@ typedef enum scc_tok_type {
|
||||
|
||||
typedef enum scc_tok_subtype {
|
||||
SCC_TOK_SUBTYPE_INVALID, // 错误占位
|
||||
SCC_TOK_SUBTYPE_KEYWORD, // 关键字
|
||||
SCC_TOK_SUBTYPE_OPERATOR, // 操作符
|
||||
SCC_TOK_SUBTYPE_IDENTIFIER, // 标识符
|
||||
SCC_TOK_SUBTYPE_LITERAL, // 字面量
|
||||
|
||||
@@ -28,11 +28,14 @@ static inline cbool scc_lexer_next_non_blank(scc_lexer_tok_ring_t *stream,
|
||||
static inline void scc_lexer_skip_until_newline(scc_lexer_tok_ring_t *stream) {
|
||||
scc_lexer_tok_t tok;
|
||||
cbool ok;
|
||||
while (scc_lexer_peek_non_blank(stream, &tok)) {
|
||||
if (tok.type == SCC_TOK_ENDLINE)
|
||||
break;
|
||||
while (1) {
|
||||
scc_ring_next_consume(*stream, tok, ok);
|
||||
if (!ok)
|
||||
break;
|
||||
scc_tok_type_t type = tok.type;
|
||||
scc_lexer_tok_drop(&tok);
|
||||
if (type == SCC_TOK_ENDLINE)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,19 +15,21 @@
|
||||
|
||||
// 条件编译状态栈
|
||||
typedef struct {
|
||||
int active; // 当前层级是否有效(即应该输出 token)
|
||||
int skip; // 当前层级是否跳过(即不输出 token)
|
||||
// 可根据需要增加状态,如 #if 的结果、#elif 已执行等
|
||||
} scc_pproc_if_state_t;
|
||||
typedef SCC_VEC(scc_pproc_if_state_t) scc_pproc_if_stack_t;
|
||||
cbool found_true;
|
||||
cbool seen_else;
|
||||
cbool active;
|
||||
} scc_pproc_if_t;
|
||||
typedef SCC_VEC(scc_pproc_if_t) scc_pproc_if_stack_t;
|
||||
|
||||
typedef struct {
|
||||
scc_sstream_t sstream;
|
||||
scc_lexer_t lexer;
|
||||
scc_lexer_tok_ring_t *ring;
|
||||
} scc_pproc_file_state_t;
|
||||
typedef SCC_VEC(scc_pproc_file_state_t *) scc_pproc_file_stack_t;
|
||||
} scc_pproc_file_t;
|
||||
typedef SCC_VEC(scc_pproc_file_t *) scc_pproc_file_stack_t;
|
||||
|
||||
typedef SCC_VEC(scc_lexer_tok_ring_t *) scc_pproc_ring_vec_t;
|
||||
typedef SCC_VEC(scc_cstring_t) scc_pproc_cstr_vec_t;
|
||||
|
||||
typedef struct scc_pproc {
|
||||
scc_lexer_tok_ring_t *org_ring;
|
||||
@@ -35,21 +37,37 @@ typedef struct scc_pproc {
|
||||
scc_lexer_tok_ring_t expanded_ring;
|
||||
scc_strpool_t strpool;
|
||||
int at_line_start;
|
||||
int enable;
|
||||
|
||||
scc_pproc_cstr_vec_t include_paths;
|
||||
scc_pproc_macro_table_t macro_table;
|
||||
scc_pproc_if_stack_t if_stack;
|
||||
scc_pproc_file_stack_t file_stack;
|
||||
|
||||
scc_lexer_tok_ring_t ring;
|
||||
int ring_ref_count;
|
||||
|
||||
struct {
|
||||
int max_include_depth;
|
||||
} config;
|
||||
} scc_pproc_t;
|
||||
|
||||
void scc_pproc_init(scc_pproc_t *pp, scc_lexer_tok_ring_t *input);
|
||||
scc_lexer_tok_ring_t *scc_pproc_to_ring(scc_pproc_t *pp, int ring_size);
|
||||
void scc_pproc_drop(scc_pproc_t *pp);
|
||||
|
||||
static inline void scc_pproc_add_include_path(scc_pproc_t *pp,
|
||||
const scc_cstring_t *path) {
|
||||
scc_vec_push(pp->include_paths, scc_cstring_copy(path));
|
||||
}
|
||||
|
||||
static inline void scc_pproc_add_include_path_cstr(scc_pproc_t *pp,
|
||||
const char *path) {
|
||||
scc_vec_push(pp->include_paths, scc_cstring_from_cstr(path));
|
||||
}
|
||||
|
||||
void scc_pproc_handle_directive(scc_pproc_t *pp);
|
||||
void scc_pproc_parse_include(scc_pproc_t *pp);
|
||||
void scc_pproc_parse_include(scc_pproc_t *pp, scc_lexer_tok_t *include_tok);
|
||||
void scc_pproc_parse_macro_arguments(scc_lexer_tok_ring_t *ring,
|
||||
scc_lexer_tok_vec_t *args, int need_full);
|
||||
void scc_pproc_parse_function_macro(scc_pproc_t *pp,
|
||||
|
||||
@@ -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);
|
||||
@@ -271,18 +298,120 @@ void scc_pproc_handle_directive(scc_pproc_t *pp) {
|
||||
return;
|
||||
}
|
||||
case SCC_PP_TOK_INCLUDE:
|
||||
scc_lexer_tok_drop(&tok);
|
||||
scc_pproc_parse_include(pp);
|
||||
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;
|
||||
@@ -298,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) {
|
||||
@@ -310,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;
|
||||
|
||||
@@ -1,11 +1,51 @@
|
||||
#include <pproc_expand.h>
|
||||
#include <scc_pproc.h>
|
||||
|
||||
static int switch_file_stack(scc_pproc_t *pp, scc_cstring_t fname,
|
||||
int is_system) {
|
||||
scc_pproc_file_state_t *file = scc_malloc(sizeof(scc_pproc_file_state_t));
|
||||
static int switch_file_stack(scc_pproc_t *pp, scc_cstring_t *fname,
|
||||
scc_pos_t *pos, int is_system) {
|
||||
scc_cstring_t fpath = scc_cstring_create();
|
||||
int ret = 0;
|
||||
|
||||
const char *org_fname = pos->name;
|
||||
|
||||
if (!is_system) {
|
||||
const char parent[] = "/../";
|
||||
// FIXME maybe it can eazy
|
||||
scc_cstring_append_cstr(&fpath, org_fname, scc_strlen(org_fname));
|
||||
scc_cstring_append_cstr(&fpath, parent, scc_strlen(parent));
|
||||
scc_cstring_append(&fpath, fname);
|
||||
ret = scc_fexists(scc_cstring_as_cstr(&fpath));
|
||||
if (ret == true) {
|
||||
goto FOPEN;
|
||||
}
|
||||
}
|
||||
|
||||
/* system default path and -I includes path */
|
||||
scc_vec_foreach(pp->include_paths, i) {
|
||||
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) {
|
||||
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 ? '>' : '\"');
|
||||
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_pproc_file_t *file = scc_malloc(sizeof(scc_pproc_file_t));
|
||||
Assert(file != null);
|
||||
if (scc_sstream_init(&(file->sstream), fname.data, 1024)) {
|
||||
if (scc_sstream_init(&(file->sstream), scc_cstring_as_cstr(&fpath), 1024)) {
|
||||
return -1;
|
||||
}
|
||||
scc_lexer_init(&(file->lexer), scc_sstream_to_ring(&(file->sstream)));
|
||||
@@ -16,9 +56,11 @@ static int switch_file_stack(scc_pproc_t *pp, scc_cstring_t fname,
|
||||
return 0;
|
||||
}
|
||||
|
||||
void scc_pproc_parse_include(scc_pproc_t *pp) {
|
||||
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(include_tok);
|
||||
|
||||
scc_lexer_tok_ring_t *stream = pp->cur_ring;
|
||||
scc_lexer_tok_vec_t org_toks;
|
||||
@@ -74,9 +116,10 @@ void scc_pproc_parse_include(scc_pproc_t *pp) {
|
||||
}
|
||||
scc_cstring_free(&line);
|
||||
int is_system = includename[0] == '<';
|
||||
if (switch_file_stack(pp, fname, is_system)) {
|
||||
goto ERROR;
|
||||
if (switch_file_stack(pp, &fname, &pos, is_system)) {
|
||||
// LOG_ERROR()
|
||||
}
|
||||
scc_cstring_free(&fname);
|
||||
return;
|
||||
ERROR:
|
||||
LOG_ERROR("Invalid include filename need \"FILENAME\" or <FILENAME>");
|
||||
|
||||
@@ -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));
|
||||
@@ -84,9 +103,13 @@ void scc_pproc_init(scc_pproc_t *pp, scc_lexer_tok_ring_t *input) {
|
||||
pp->cur_ring = pp->org_ring;
|
||||
scc_ring_init(pp->expanded_ring, 0, 0, 0);
|
||||
scc_pproc_marco_table_init(&pp->macro_table);
|
||||
scc_vec_init(pp->include_paths);
|
||||
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() {
|
||||
|
||||
@@ -17,7 +17,7 @@ static cbool process_input(const char *input, scc_cstring_t *output) {
|
||||
scc_pproc_init(&pp, scc_lexer_to_ring(&lexer, 8, true));
|
||||
|
||||
scc_lexer_tok_ring_t *tok_ring = scc_pproc_to_ring(&pp, 8);
|
||||
*output = scc_cstring_create();
|
||||
*output = scc_cstring_from_cstr("");
|
||||
scc_lexer_tok_t tok;
|
||||
while (1) {
|
||||
scc_ring_next_consume(*tok_ring, tok, ret);
|
||||
@@ -31,7 +31,6 @@ static cbool process_input(const char *input, scc_cstring_t *output) {
|
||||
scc_pproc_drop(&pp);
|
||||
scc_lexer_drop(&lexer);
|
||||
scc_sstream_drop(&mem_stream);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -127,6 +126,12 @@ static void test_define_nested_macros(void) {
|
||||
CHECK_PP_OUTPUT_EXACT(
|
||||
"#define A 1\n#define B (A + 1)\n#define C (B + 1)\nC\n",
|
||||
"((1 + 1) + 1)\n");
|
||||
|
||||
CHECK_PP_OUTPUT_EXACT("#define A\n", "");
|
||||
CHECK_PP_OUTPUT_EXACT("#undef A\n", "");
|
||||
|
||||
CHECK_PP_OUTPUT_EXACT(" # define A 1\nA", "1");
|
||||
// CHECK_PP_OUTPUT_EXACT(" # define A 1 \nA", "1"); // TODO
|
||||
}
|
||||
|
||||
static void test_undef_macros(void) {
|
||||
@@ -225,6 +230,126 @@ static void test_edge_cases(void) {
|
||||
CHECK_PP_OUTPUT_EXACT("#define A B\n#define B C\n#define C 1\nA\n", "1\n");
|
||||
}
|
||||
|
||||
static void test_conditional_ifdef(void) {
|
||||
TEST_CASE("ifdef and ifndef");
|
||||
|
||||
// 基本 ifdef / ifndef
|
||||
CHECK_PP_OUTPUT_EXACT("#define FOO\n"
|
||||
"#ifdef FOO\n"
|
||||
"foo\n"
|
||||
"#endif\n",
|
||||
"foo\n");
|
||||
|
||||
CHECK_PP_OUTPUT_EXACT("#define FOO\n"
|
||||
"#ifndef FOO\n"
|
||||
"foo\n"
|
||||
"#endif\n",
|
||||
"");
|
||||
|
||||
CHECK_PP_OUTPUT_EXACT("#undef FOO\n"
|
||||
"#ifdef FOO\n"
|
||||
"foo\n"
|
||||
"#endif\n",
|
||||
"");
|
||||
|
||||
CHECK_PP_OUTPUT_EXACT("#undef FOO\n"
|
||||
"#ifndef FOO\n"
|
||||
"foo\n"
|
||||
"#endif\n",
|
||||
"foo\n");
|
||||
|
||||
// ifdef + else
|
||||
CHECK_PP_OUTPUT_EXACT("#define FOO\n"
|
||||
"#ifdef FOO\n"
|
||||
"foo\n"
|
||||
"#else\n"
|
||||
"bar\n"
|
||||
"#endif\n",
|
||||
"foo\n");
|
||||
|
||||
CHECK_PP_OUTPUT_EXACT("#undef FOO\n"
|
||||
"#ifdef FOO\n"
|
||||
"foo\n"
|
||||
"#else\n"
|
||||
"bar\n"
|
||||
"#endif\n",
|
||||
"bar\n");
|
||||
|
||||
// ifdef + elifdef (C23)
|
||||
CHECK_PP_OUTPUT_EXACT("#define FOO\n"
|
||||
"#ifdef FOO\n"
|
||||
"foo\n"
|
||||
"#elifdef FOO\n"
|
||||
"foo2\n"
|
||||
"#endif\n",
|
||||
"foo\n");
|
||||
|
||||
CHECK_PP_OUTPUT_EXACT("#undef FOO\n"
|
||||
"#define BAR\n"
|
||||
"#ifdef FOO\n"
|
||||
"foo\n"
|
||||
"#elifdef BAR\n"
|
||||
"bar\n"
|
||||
"#else\n"
|
||||
"none\n"
|
||||
"#endif\n",
|
||||
"bar\n");
|
||||
|
||||
CHECK_PP_OUTPUT_EXACT("#undef FOO\n"
|
||||
"#undef BAR\n"
|
||||
"#ifdef FOO\n"
|
||||
"foo\n"
|
||||
"#elifdef BAR\n"
|
||||
"bar\n"
|
||||
"#else\n"
|
||||
"none\n"
|
||||
"#endif\n",
|
||||
"none\n");
|
||||
|
||||
// 嵌套
|
||||
CHECK_PP_OUTPUT_EXACT("#define A\n"
|
||||
"#ifdef A\n"
|
||||
" #ifdef B\n"
|
||||
" inner\n"
|
||||
" #endif\n"
|
||||
" outer\n"
|
||||
"#endif\n",
|
||||
" outer\n");
|
||||
|
||||
CHECK_PP_OUTPUT_EXACT("#define B\n"
|
||||
"#ifndef A\n"
|
||||
" #ifdef B\n"
|
||||
" inner\n"
|
||||
" #endif\n"
|
||||
" outer\n"
|
||||
"#endif\n",
|
||||
" inner\n outer\n");
|
||||
|
||||
// 外层假,内层真
|
||||
CHECK_PP_OUTPUT_EXACT("#ifdef __NONE\n"
|
||||
"#define OUTER\n"
|
||||
"#endif\n"
|
||||
"#ifdef OUTER\n"
|
||||
"should not appear\n"
|
||||
"#endif\n",
|
||||
""); // 期望为空
|
||||
|
||||
// 更复杂的嵌套条件
|
||||
CHECK_PP_OUTPUT_EXACT("#define X\n"
|
||||
"#ifdef X\n"
|
||||
"x defined\n"
|
||||
"#ifdef Y\n"
|
||||
"Y defined\n"
|
||||
"#else\n"
|
||||
"Y not defined\n"
|
||||
"#endif\n"
|
||||
"after inner\n"
|
||||
"#else\n"
|
||||
"X not defined\n"
|
||||
"#endif\n",
|
||||
"x defined\nY not defined\nafter inner\n");
|
||||
}
|
||||
|
||||
#define TEST_LIST_CASE(func_name) {#func_name, func_name}
|
||||
TEST_LIST = {
|
||||
TEST_LIST_CASE(test_define_simple_no_macro),
|
||||
@@ -237,5 +362,6 @@ TEST_LIST = {
|
||||
TEST_LIST_CASE(test_define_nested_macros),
|
||||
TEST_LIST_CASE(test_undef_macros),
|
||||
TEST_LIST_CASE(hard_test_define_func_macros),
|
||||
TEST_LIST_CASE(test_conditional_ifdef),
|
||||
{NULL, NULL},
|
||||
};
|
||||
@@ -7,7 +7,12 @@
|
||||
#define __SCC_LOG_IMPL_H__
|
||||
|
||||
#include "color.h"
|
||||
#ifndef __SCC__
|
||||
#include <stdarg.h>
|
||||
#else
|
||||
// TODO
|
||||
#warning "TODO: implement stdarg.h"
|
||||
#endif
|
||||
|
||||
#ifdef __SCC_LOG_IMPL_USE_STD_IMPL__
|
||||
#include <stdio.h>
|
||||
@@ -21,7 +26,7 @@
|
||||
#define __smcc_log_unreachable() (__builtin_unreachable())
|
||||
#elif defined _MSC_VER // MSVC
|
||||
#define __smcc_log_unreachable() (__assume(false))
|
||||
#elif defined __SMCC_BUILT_IN__ // The SMCC (my compiler)
|
||||
#elif defined __SCC_BUILT_IN__ // The SCC Compiler (my compiler)
|
||||
#define __smcc_log_unreachable() (__smcc_builtin_unreachable())
|
||||
#else
|
||||
#define __smcc_log_unreachable()
|
||||
|
||||
@@ -29,14 +29,20 @@
|
||||
|
||||
// ==================== 内部辅助宏 (不直接使用) ====================
|
||||
|
||||
#define scc_ring_phys(ring, idx) ((idx) % (ring).cap)
|
||||
#define _scc_ring_phys(ring, idx) ((idx) % (ring).cap)
|
||||
#define _scc_ring_cap(ring) ((ring).cap)
|
||||
#define _scc_ring_head(ring) ((ring).head)
|
||||
#define _scc_ring_probe(ring) ((ring).probe)
|
||||
#define _scc_ring_tail(ring) ((ring).tail)
|
||||
#define _scc_ring_size(ring) ((ring).tail - (ring).head)
|
||||
#define _scc_ring_empty(ring) ((ring).head == (ring).tail)
|
||||
|
||||
/**
|
||||
* @brief 确保 probe 位置有数据可用 (尝试填充)
|
||||
* @param ring 环形缓冲区变量
|
||||
* @param ok 变量名 (如 int ok_flag) ,宏会将其设置为 true 或 false
|
||||
*/
|
||||
#define scc_ring_ensure(ring, ok) \
|
||||
#define _scc_ring_ensure(ring, ok) \
|
||||
do { \
|
||||
ok = 1; \
|
||||
if ((ring).probe < (ring).tail) \
|
||||
@@ -50,7 +56,7 @@
|
||||
ok = 0; /* 缓冲区满,无法填充 */ \
|
||||
break; \
|
||||
} \
|
||||
usize phys_tail = scc_ring_phys(ring, (ring).tail); \
|
||||
usize phys_tail = _scc_ring_phys(ring, (ring).tail); \
|
||||
if ((ring).fill == null || \
|
||||
!(ring).fill(&(ring).data[phys_tail], (ring).userdata)) { \
|
||||
ok = 0; \
|
||||
@@ -110,14 +116,14 @@
|
||||
*/
|
||||
#define scc_ring_peek(ring, val, ok) \
|
||||
do { \
|
||||
scc_ring_ensure(ring, ok); \
|
||||
_scc_ring_ensure(ring, ok); \
|
||||
if (!(ok)) \
|
||||
break; \
|
||||
if ((ring).probe >= (ring).tail) { \
|
||||
ok = 0; \
|
||||
break; \
|
||||
} \
|
||||
usize _phys = scc_ring_phys(ring, (ring).probe); \
|
||||
usize _phys = _scc_ring_phys(ring, (ring).probe); \
|
||||
val = (ring).data[_phys]; \
|
||||
} while (0)
|
||||
|
||||
@@ -129,14 +135,14 @@
|
||||
*/
|
||||
#define scc_ring_next(ring, val, ok) \
|
||||
do { \
|
||||
scc_ring_ensure(ring, ok); \
|
||||
_scc_ring_ensure(ring, ok); \
|
||||
if (!(ok)) \
|
||||
break; \
|
||||
if ((ring).probe >= (ring).tail) { \
|
||||
ok = 0; \
|
||||
break; \
|
||||
} \
|
||||
usize _phys = scc_ring_phys(ring, (ring).probe); \
|
||||
usize _phys = _scc_ring_phys(ring, (ring).probe); \
|
||||
val = (ring).data[_phys]; \
|
||||
(ring).probe++; \
|
||||
} while (0)
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
#ifndef __SCC_CORE_TYPE_H__
|
||||
#define __SCC_CORE_TYPE_H__
|
||||
|
||||
#ifndef __SCC_BUILTIN_TYPE__
|
||||
#ifndef __SCC__
|
||||
#include <assert.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
@@ -48,6 +49,29 @@ static_assert(sizeof(cbool) == 1, "cbool size must 1");
|
||||
#define __scc_null
|
||||
#define __scc_isize
|
||||
#define __scc_usize
|
||||
|
||||
/* clang-format off */
|
||||
typedef __scc_i8 i8;
|
||||
typedef __scc_i16 i16;
|
||||
typedef __scc_i32 i32;
|
||||
typedef __scc_i64 i64;
|
||||
typedef __scc_u8 u8;
|
||||
typedef __scc_u16 u16;
|
||||
typedef __scc_u32 u32;
|
||||
typedef __scc_u64 u64;
|
||||
|
||||
typedef __scc_isize isize;
|
||||
typedef __scc_usize usize;
|
||||
typedef __scc_isize pdiff;
|
||||
|
||||
typedef __scc_f32 f32;
|
||||
typedef __scc_f64 f64;
|
||||
|
||||
typedef __scc_bool cbool;
|
||||
/// void / null
|
||||
#define null __scc_null
|
||||
|
||||
/* clang-format on */
|
||||
#endif
|
||||
|
||||
typedef union scc_cvalue {
|
||||
|
||||
25
src/main.c
25
src/main.c
@@ -7,12 +7,11 @@
|
||||
// #include <ir_dump.h>
|
||||
// #include <scc_ast2ir.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
const char *input_file;
|
||||
const char *output_file;
|
||||
int verbose;
|
||||
scc_argparse_list_t include_paths;
|
||||
cbool emit_lex;
|
||||
cbool emit_pp;
|
||||
cbool emit_ast;
|
||||
@@ -26,6 +25,7 @@ static void setup_argparse(scc_argparse_t *argparse, scc_config_t *config,
|
||||
SCC_HINT_DESCRIPTION,
|
||||
SCC_HINT_OUTPUT_FILE,
|
||||
SCC_HINT_INPUT_FILE,
|
||||
SCC_HINT_INCLUDE_PATH,
|
||||
SCC_HINT_VERBOSE,
|
||||
|
||||
SCC_HINT_EMIT_LEX,
|
||||
@@ -38,6 +38,7 @@ static void setup_argparse(scc_argparse_t *argparse, scc_config_t *config,
|
||||
[SCC_HINT_DESCRIPTION] = "A simple C compiler",
|
||||
[SCC_HINT_OUTPUT_FILE] = "Output file",
|
||||
[SCC_HINT_INPUT_FILE] = "Input source file",
|
||||
[SCC_HINT_INCLUDE_PATH] = "SCC_HINT_INCLUDE_PATH",
|
||||
[SCC_HINT_VERBOSE] = "Increase verbosity (can be used multiple times)",
|
||||
[SCC_HINT_EMIT_LEX] = "Generate lexer sources tokens and exit",
|
||||
[SCC_HINT_EMIT_PP] = "Generate preprocessed tokens and exit",
|
||||
@@ -49,6 +50,7 @@ static void setup_argparse(scc_argparse_t *argparse, scc_config_t *config,
|
||||
[SCC_HINT_DESCRIPTION] = "一个简单的C编译器",
|
||||
[SCC_HINT_OUTPUT_FILE] = "输出文件",
|
||||
[SCC_HINT_INPUT_FILE] = "输入源文件",
|
||||
[SCC_HINT_INCLUDE_PATH] = "SCC_HINT_INCLUDE_PATH",
|
||||
[SCC_HINT_VERBOSE] = "增加详细输出(可多次使用)",
|
||||
[SCC_HINT_EMIT_LEX] = "生成`源代码的词法单元`并退出",
|
||||
[SCC_HINT_EMIT_PP] = "生成`预处理后的词法单元`并退出",
|
||||
@@ -88,6 +90,13 @@ static void setup_argparse(scc_argparse_t *argparse, scc_config_t *config,
|
||||
scc_argparse_spec_set_required(&arg_input.spec, true);
|
||||
scc_argparse_cmd_add_arg(root, &arg_input);
|
||||
|
||||
// -I, --include (添加额外的系统头文件搜索路径)
|
||||
scc_argparse_opt_t opt_include;
|
||||
scc_argparse_opt_init(&opt_include, 'I', "include",
|
||||
scc_hints[SCC_HINT_INCLUDE_PATH]);
|
||||
scc_argparse_spec_setup_list(&opt_include.spec, &(config->include_paths));
|
||||
scc_argparse_cmd_add_opt(root, &opt_include);
|
||||
|
||||
// -v, --verbose (计数)
|
||||
scc_argparse_opt_t opt_verbose;
|
||||
scc_argparse_opt_init(&opt_verbose, 'V', "verbose",
|
||||
@@ -175,7 +184,6 @@ int main(int argc, const char **argv, const char **envp) {
|
||||
SetConsoleOutputCP(CP_UTF8);
|
||||
SetConsoleCP(CP_UTF8);
|
||||
#endif
|
||||
setbuf(stdout, NULL);
|
||||
|
||||
#ifdef _WIN32
|
||||
#define OUTPUT_DEFAULT_FILE "a.exe"
|
||||
@@ -190,6 +198,8 @@ int main(int argc, const char **argv, const char **envp) {
|
||||
.emit_ast = false,
|
||||
.emit_ir = false,
|
||||
};
|
||||
scc_vec_init(config.include_paths);
|
||||
|
||||
scc_argparse_t argparse;
|
||||
setup_argparse(&argparse, &config, SCC_ARGPARSE_LANG_ZH);
|
||||
int ret = scc_argparse_parse(&argparse, argc, argv);
|
||||
@@ -219,6 +229,15 @@ int main(int argc, const char **argv, const char **envp) {
|
||||
|
||||
scc_pproc_t pproc;
|
||||
scc_pproc_init(&pproc, scc_lexer_to_ring(&lexer, 8, true));
|
||||
scc_vec_foreach(config.include_paths, i) {
|
||||
scc_pproc_add_include_path_cstr(&pproc,
|
||||
scc_vec_at(config.include_paths, i));
|
||||
}
|
||||
scc_lexer_tok_vec_t pproc_tok_vec;
|
||||
scc_vec_init(pproc_tok_vec);
|
||||
scc_cstring_t pproc_macro_name = scc_cstring_from_cstr("__SCC__");
|
||||
scc_pproc_add_object_macro(&(pproc.macro_table), &pproc_macro_name,
|
||||
&pproc_tok_vec);
|
||||
if (config.emit_pp) {
|
||||
scc_lexer_tok_ring_t *tok_ring = scc_pproc_to_ring(&pproc, 8);
|
||||
if (config.output_file == null) {
|
||||
|
||||
Reference in New Issue
Block a user