feat(ast): 添加完整的内置类型支持并重构类型定义
- 添加 va_list、bool、signed/unsigned 各种整数类型等内置类型 - 重新排列内置类型枚举顺序,增加 UNKNOWN 类型 - 为复合字面量、指针类型、数组类型添加初始化函数 - 添加结构体、联合体、枚举、typedef 类型的初始化函数 - 更新类型转储功能以支持新的内置类型显示 refactor(parser): 优化类型解析和声明处理逻辑 - 修改类型解析函数返回类型为通用 AST 节点 - 重构声明解析逻辑,支持变量和函数声明的不同处理路径 - 更新语法分析规则中的空格标记处理 - 简化表达式解析中的错误处理流程 fix(ast): 修复参数声明和类型转储相关问题 - 修正参数声明初始化函数中对 name 参数可为空的处理 - 修复类型转储中修饰符显示和指针类型显示问题 - 更新 AST 转储中空值检查使用正确的 null 比较
This commit is contained in:
@@ -57,7 +57,7 @@ A.2.2 Declarations
|
||||
declarator(opt) : constant-expression
|
||||
(6.7.2.2) enum-specifier:
|
||||
enum identifier(opt) { enumerator-list }
|
||||
enum identifier(opt) { enumerator-list ,}
|
||||
enum identifier(opt) { enumerator-list , }
|
||||
enum identifier
|
||||
(6.7.2.2) enumerator-list:
|
||||
enumerator
|
||||
@@ -82,7 +82,7 @@ A.2.2 Declarations
|
||||
assignment-expression ]
|
||||
direct-declarator [ type-qualifier-list static
|
||||
assignment-expression ]
|
||||
direct-declarator [ type-qualifier-list(opt) *]
|
||||
direct-declarator [ type-qualifier-list(opt) * ]
|
||||
direct-declarator ( parameter-type-list )
|
||||
direct-declarator ( identifier-list(opt) )
|
||||
(6.7.5) pointer:
|
||||
@@ -112,9 +112,9 @@ A.2.2 Declarations
|
||||
( abstract-declarator )
|
||||
direct-abstract-declarator(opt) [ type-qualifier-list (opt)
|
||||
assignment-expression(opt) ]
|
||||
direct-abstract-declarator(opt) [static type-qualifier-list(opt)
|
||||
direct-abstract-declarator(opt) [ static type-qualifier-list(opt)
|
||||
assignment-expression ]
|
||||
direct-abstract-declaratoropt [ type-qualifier-list static
|
||||
direct-abstract-declarator(opt) [ type-qualifier-list static
|
||||
assignment-expression ]
|
||||
direct-abstract-declarator(opt) [ * ]
|
||||
direct-abstract-declarator(opt) ( parameter-type-list(opt) )
|
||||
@@ -177,81 +177,46 @@ scc_ast_decl_t *scc_parse_declaration(scc_parser_t *parser) {
|
||||
cbool ok;
|
||||
scc_lexer_tok_t tok;
|
||||
|
||||
scc_ast_type_t *type = scc_parse_type(parser);
|
||||
if (type == null) {
|
||||
scc_ast_node_t *type_or_decl = _scc_parse_type(parser);
|
||||
scc_ast_decl_t *decl = null;
|
||||
if (type_or_decl == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ok = scc_parser_next_consume(parser, &tok);
|
||||
if (ok == false) {
|
||||
if (SCC_AST_IS_A(scc_ast_type_t, type_or_decl)) {
|
||||
LOG_WARN("declaration dose not declare anything");
|
||||
return null;
|
||||
}
|
||||
|
||||
scc_ast_decl_t *decl = scc_malloc(sizeof(scc_ast_decl_t));
|
||||
/*
|
||||
(6.7.5) declarator:
|
||||
pointeropt direct-declarator
|
||||
(6.7.5) direct-declarator:
|
||||
identifier
|
||||
( declarator )
|
||||
direct-declarator [ type-qualifier-listopt assignment-expressionopt ]
|
||||
direct-declarator [static type-qualifier-listopt assignment-expression ]
|
||||
direct-declarator [ type-qualifier-list static assignment-expression ]
|
||||
direct-declarator [ type-qualifier-listopt *]
|
||||
direct-declarator ( parameter-type-list )
|
||||
direct-declarator ( identifier-listopt )
|
||||
*/
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_L_PAREN)) {
|
||||
if (scc_parser_consume_if(parser, SCC_TOK_SEMICOLON)) {
|
||||
scc_ast_decl_val_init(decl, type, scc_cstring_as_cstr(&tok.lexeme),
|
||||
null);
|
||||
goto RETURN;
|
||||
} else if (scc_parser_consume_if(parser, SCC_TOK_ASSIGN)) {
|
||||
scc_ast_expr_t *init = scc_parse_expression(parser);
|
||||
scc_ast_decl_val_init(decl, type, scc_cstring_as_cstr(&tok.lexeme),
|
||||
init);
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_SEMICOLON)) {
|
||||
LOG_ERROR("expect semicolon");
|
||||
}
|
||||
goto RETURN;
|
||||
}
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
// function decl
|
||||
decl->base.type = SCC_AST_DECL_FUNC;
|
||||
decl->name = scc_cstring_as_cstr(&tok.lexeme);
|
||||
decl->func.type = scc_malloc(sizeof(scc_ast_type_t));
|
||||
decl->func.type->base.type = SCC_AST_TYPE_FUNCTION;
|
||||
scc_vec_init(decl->func.type->function.param_types);
|
||||
decl->func.type->function.return_type = type;
|
||||
|
||||
// TODO param type
|
||||
scc_parser_consume_if(parser, SCC_TOK_VOID);
|
||||
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_R_PAREN)) {
|
||||
} else if (SCC_AST_IS_A(scc_ast_decl_t, type_or_decl)) {
|
||||
decl = SCC_AST_CAST_TO(scc_ast_decl_t, type_or_decl);
|
||||
} else {
|
||||
LOG_ERROR("invalid declaration");
|
||||
return null;
|
||||
}
|
||||
|
||||
const scc_lexer_tok_t *tok_ptr = scc_parser_peek(parser);
|
||||
if (tok_ptr == null) {
|
||||
return null;
|
||||
}
|
||||
if (tok_ptr->type != SCC_TOK_L_BRACE) {
|
||||
if (tok_ptr->type == SCC_TOK_SEMICOLON) {
|
||||
decl->func.body = null;
|
||||
} else {
|
||||
return null;
|
||||
if (tok_ptr->type == SCC_TOK_SEMICOLON) {
|
||||
scc_parser_next_consume(parser, null);
|
||||
goto RETURN;
|
||||
} else if (tok_ptr->type == SCC_TOK_ASSIGN) {
|
||||
scc_parser_next_consume(parser, null);
|
||||
scc_ast_expr_t *init = scc_parse_expression(parser);
|
||||
decl->var.init = init;
|
||||
if (!scc_parser_consume_if(parser, SCC_TOK_SEMICOLON)) {
|
||||
LOG_ERROR("expect semicolon");
|
||||
}
|
||||
goto RETURN;
|
||||
} else if (tok_ptr->type == SCC_TOK_L_BRACE) {
|
||||
scc_ast_stmt_t *body = scc_parse_statement(parser);
|
||||
Assert(decl->base.type == SCC_AST_DECL_FUNC);
|
||||
decl->func.body = body;
|
||||
Assert(decl->func.type != null);
|
||||
Assert(decl->func.type->base.type == SCC_AST_TYPE_FUNCTION);
|
||||
Assert(decl->func.body != null);
|
||||
Assert(decl->func.body->base.type == SCC_AST_STMT_COMPOUND);
|
||||
} else {
|
||||
UNREACHABLE();
|
||||
}
|
||||
|
||||
decl->func.body = scc_parse_statement(parser);
|
||||
Assert(decl->func.type != null);
|
||||
Assert(decl->func.type->base.type == SCC_AST_TYPE_FUNCTION);
|
||||
Assert(decl->func.body != null);
|
||||
Assert(decl->func.body->base.type == SCC_AST_STMT_COMPOUND);
|
||||
|
||||
RETURN:
|
||||
if (decl) {
|
||||
parser->sema_callbacks.on_decl(parser->sema_callbacks.context,
|
||||
|
||||
Reference in New Issue
Block a user