stage1 支持更多语法 修复更多问题 暂时stage1功能结束
This commit is contained in:
1 parent
54425a50c7
commit
acfbbeee8b
11 files changed
+802
-183
No files matched your search
+38
-21
@@ -252,6 +252,30 @@ static int can_start_statement(spl_tok_type_t t) {
|
||||
}
|
||||
}
|
||||
|
||||
static int is_builtin_type_kw(spl_tok_type_t t) {
|
||||
switch (t) {
|
||||
// case KW_I8:
|
||||
// case KW_U8:
|
||||
// case KW_I16:
|
||||
// case KW_U16:
|
||||
// case KW_I32:
|
||||
// case KW_U32:
|
||||
// case KW_I64:
|
||||
// case KW_U64:
|
||||
// case KW_F32:
|
||||
// case KW_F64:
|
||||
// case KW_ISIZE:
|
||||
// case KW_USIZE:
|
||||
case KW_BOOL:
|
||||
// case KW_VOID:
|
||||
// case KW_PTR:
|
||||
case KW_ANY:
|
||||
return 1;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static spl_ast_node_ref_t parse_expr(spl_parser_t *p);
|
||||
static spl_ast_node_ref_vec_t parse_expr_list(spl_parser_t *p);
|
||||
static spl_ast_node_ref_t parse_block(spl_parser_t *p);
|
||||
@@ -662,13 +686,12 @@ static spl_ast_node_ref_t parse_type_decl(spl_parser_t *p, spl_ast_node_ref_vec_
|
||||
return 0;
|
||||
advance(p);
|
||||
|
||||
const spl_tok_t *name = peek(p);
|
||||
if (!name || name->type != TOK_IDENT) {
|
||||
PARSE_ERROR(p, SPL_DIAG_KIND_PARSE_EXPECTED, name ? name : kw, "expected type name");
|
||||
spl_ast_node_ref_t pattern = parse_pattern(p);
|
||||
if (!pattern) {
|
||||
PARSE_ERROR(p, SPL_DIAG_KIND_PARSE_EXPECTED, peek(p), "expected pattern after 'type'");
|
||||
vec_free(tag_list);
|
||||
return 0;
|
||||
}
|
||||
advance(p);
|
||||
|
||||
if (!expect(p, TOK_ASSIGN, "'=' in type declaration")) {
|
||||
vec_free(tag_list);
|
||||
@@ -678,9 +701,11 @@ static spl_ast_node_ref_t parse_type_decl(spl_parser_t *p, spl_ast_node_ref_vec_
|
||||
spl_ast_node_ref_t ref = new_node(p, SPL_AST_TYPE_DECL, kw);
|
||||
spl_ast_node_t *td = node_at(p->ast, ref);
|
||||
MOVE_VEC(td->type_decl.attr_list, tag_list);
|
||||
SET_STR_FIELD(p->ast, td->type_decl.name, name);
|
||||
td->type_decl.pattern = pattern;
|
||||
|
||||
spl_ast_node_ref_t type_expr = parse_type_expr(p);
|
||||
node_at(p->ast, ref)->type_decl.type_expr = type_expr;
|
||||
td = node_at(p->ast, ref); /* parse_type_expr 可能 push node_buckets,重新取 */
|
||||
td->type_decl.type_expr = type_expr;
|
||||
|
||||
const spl_tok_t *t = peek(p);
|
||||
if (t && (t->type == TOK_COMMA || t->type == TOK_SEMICOLON))
|
||||
@@ -909,7 +934,8 @@ static spl_ast_node_ref_t parse_type_base(spl_parser_t *p) {
|
||||
return ref;
|
||||
}
|
||||
|
||||
if (bt->type == TOK_IDENT || bt->type == KW_BOOL) {
|
||||
/* 内建类型 keyword + 普通标识符类型路径 */
|
||||
if (bt->type == TOK_IDENT || is_builtin_type_kw(bt->type)) {
|
||||
advance(p);
|
||||
spl_ast_node_ref_t ref = new_node(p, SPL_AST_TYPE_PATH, bt);
|
||||
spl_ast_node_t *tp = node_at(p->ast, ref);
|
||||
@@ -932,15 +958,6 @@ static spl_ast_node_ref_t parse_type_base(spl_parser_t *p) {
|
||||
if (bt->type == TOK_AT || bt->type == TOK_SHARP)
|
||||
return parse_type_comptime_call(p);
|
||||
|
||||
if (bt->type == KW_ANY) {
|
||||
advance(p);
|
||||
spl_ast_node_ref_t ref = new_node(p, SPL_AST_TYPE_PATH, bt);
|
||||
spl_ast_node_t *tp = node_at(p->ast, ref);
|
||||
SET_STR_FIELD(p->ast, tp->type_path.name, bt);
|
||||
vec_init(tp->type_path.generic_args);
|
||||
return ref;
|
||||
}
|
||||
|
||||
PARSE_ERROR(p, SPL_DIAG_KIND_PARSE_UNRECOGNIZED, bt, "unrecognized type");
|
||||
return 0;
|
||||
}
|
||||
@@ -1950,8 +1967,6 @@ static spl_ast_node_ref_t parse_match_expr(spl_parser_t *p) {
|
||||
if (!expect(p, TOK_RIGHT_FAT_ARROW, "'=>' in match arm"))
|
||||
break;
|
||||
|
||||
if (!expect(p, TOK_L_BRACE, "'{' in match arm body"))
|
||||
break;
|
||||
spl_ast_node_ref_t body = parse_block(p);
|
||||
|
||||
vec_push(patterns, pat);
|
||||
@@ -2164,8 +2179,12 @@ static spl_ast_node_ref_t parse_block(spl_parser_t *p) {
|
||||
block = node_at(p->ast, ref);
|
||||
if (stmt)
|
||||
vec_push(block->block.stmts, stmt);
|
||||
else
|
||||
break;
|
||||
} else if (can_start_expr(t->type)) {
|
||||
spl_ast_node_ref_t expr = parse_expr(p);
|
||||
if (!expr)
|
||||
break;
|
||||
block = node_at(p->ast, ref);
|
||||
t = peek(p);
|
||||
if (t && t->type == TOK_SEMICOLON) {
|
||||
@@ -2218,12 +2237,10 @@ static void skip_to_sync(spl_parser_t *p) {
|
||||
const spl_tok_t *t = peek(p);
|
||||
if (!t || t->type == TOK_EOF)
|
||||
return;
|
||||
if (t->type == TOK_SEMICOLON) {
|
||||
if (t->type == TOK_SEMICOLON || t->type == TOK_R_BRACE) {
|
||||
advance(p);
|
||||
return;
|
||||
}
|
||||
if (t->type == TOK_R_BRACE)
|
||||
return;
|
||||
advance(p);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user