stage1 实现更多ast2ir 修复bug 提供内置操作
This commit is contained in:
1 parent
ab6e99c958
commit
49ccdec3dd
10 files changed
+396
-76
No files matched your search
+25
-14
@@ -311,7 +311,8 @@ static void parse_tag_list(parser_t *p, spl_ast_node_ref_vec_t *out, int is_exte
|
||||
t = peek(p);
|
||||
if (t && t->type == TOK_L_PAREN) {
|
||||
advance(p);
|
||||
tag->tag_item.args = parse_expr_list(p);
|
||||
spl_ast_node_ref_vec_t args = parse_expr_list(p);
|
||||
node_at(p->ast, ref)->tag_item.args = args;
|
||||
t = peek(p);
|
||||
if (!t || t->type != TOK_R_PAREN) {
|
||||
SPL_FATAL(t, "expected ')' in tag args");
|
||||
@@ -474,7 +475,8 @@ static spl_ast_node_ref_t parse_type_decl(parser_t *p, spl_ast_node_ref_vec_t ta
|
||||
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.type_expr = parse_type_expr(p);
|
||||
spl_ast_node_ref_t type_expr = parse_type_expr(p);
|
||||
node_at(p->ast, ref)->type_decl.type_expr = type_expr;
|
||||
|
||||
t = peek(p);
|
||||
if (t && (t->type == TOK_COMMA || t->type == TOK_SEMICOLON)) {
|
||||
@@ -645,13 +647,15 @@ static spl_ast_node_ref_t parse_var_pattern_atom(parser_t *p) {
|
||||
return 0;
|
||||
}
|
||||
advance(p);
|
||||
|
||||
spl_ast_node_ref_t ref = new_node(p, SPL_AST_PATTERN_DOT, t);
|
||||
spl_ast_node_t *pat = node_at(p->ast, ref);
|
||||
SET_STR_FIELD(p->ast, pat->pattern_dot.name, name);
|
||||
|
||||
if (peek(p) && peek(p)->type == TOK_L_BRACKET) {
|
||||
advance(p);
|
||||
pat->pattern_dot.sub_pattern = parse_var_pattern(p);
|
||||
spl_ast_node_ref_t sub = parse_var_pattern(p);
|
||||
node_at(p->ast, ref)->pattern_dot.sub_pattern = sub;
|
||||
t = peek(p);
|
||||
if (!t || t->type != TOK_R_BRACKET) {
|
||||
SPL_FATAL(t, "expected ']' in pattern");
|
||||
@@ -778,8 +782,8 @@ static spl_ast_node_ref_t parse_type_base(parser_t *p) {
|
||||
|
||||
if (peek(p) && peek(p)->type == TOK_COLON) {
|
||||
advance(p);
|
||||
fn_type = node_at(p->ast, ref);
|
||||
fn_type->type_fn.ret_type = parse_type_expr(p);
|
||||
spl_ast_node_ref_t ret = parse_type_expr(p);
|
||||
node_at(p->ast, ref)->type_fn.ret_type = ret;
|
||||
}
|
||||
return ref;
|
||||
}
|
||||
@@ -948,7 +952,8 @@ static spl_ast_node_ref_t parse_type_expr(parser_t *p) {
|
||||
if (t->type == TOK_MUL) {
|
||||
advance(p);
|
||||
spl_ast_node_ref_t ref = new_node(p, SPL_AST_TYPE_POINTER, t);
|
||||
node_at(p->ast, ref)->type_pointer.pointee = parse_type_expr(p);
|
||||
spl_ast_node_ref_t pointee = parse_type_expr(p);
|
||||
node_at(p->ast, ref)->type_pointer.pointee = pointee;
|
||||
return ref;
|
||||
}
|
||||
|
||||
@@ -1292,7 +1297,10 @@ static spl_ast_node_ref_t parse_postfix(parser_t *p) {
|
||||
advance(p);
|
||||
spl_ast_node_ref_t ref = new_node(p, SPL_AST_EXPR_CALL, t);
|
||||
node_at(p->ast, ref)->call.callee = expr;
|
||||
node_at(p->ast, ref)->call.args = parse_expr_list(p);
|
||||
|
||||
spl_ast_node_ref_vec_t args = parse_expr_list(p);
|
||||
node_at(p->ast, ref)->call.args = args;
|
||||
|
||||
t = peek(p);
|
||||
if (!t || t->type != TOK_R_PAREN) {
|
||||
SPL_FATAL(t, "expected ')' in call");
|
||||
@@ -1415,14 +1423,15 @@ static spl_ast_node_ref_t parse_primary(parser_t *p) {
|
||||
case TOK_IDENT: {
|
||||
advance(p);
|
||||
if (peek(p) && peek(p)->type == TOK_L_BRACE && !p->stop_agg) {
|
||||
// 具名聚合字面量:Identifier { fields }
|
||||
spl_ast_node_ref_t ref = new_node(p, SPL_AST_EXPR_AGGREGATE_LIT, t);
|
||||
spl_ast_node_t *agg = node_at(p->ast, ref);
|
||||
agg->aggregate_literal.name = intern_string(p->ast, t->lexeme, t->len);
|
||||
agg->aggregate_literal.fields = parse_aggregate_fields(p);
|
||||
|
||||
spl_ast_node_ref_vec_t fields = parse_aggregate_fields(p);
|
||||
node_at(p->ast, ref)->aggregate_literal.fields = fields;
|
||||
return ref;
|
||||
}
|
||||
// 普通标识符引用
|
||||
/* 普通标识符 */
|
||||
spl_ast_node_ref_t ref = new_node(p, SPL_AST_EXPR_IDENT, t);
|
||||
node_at(p->ast, ref)->ident.name = intern_string(p->ast, t->lexeme, t->len);
|
||||
return ref;
|
||||
@@ -1435,11 +1444,11 @@ static spl_ast_node_ref_t parse_primary(parser_t *p) {
|
||||
return 0;
|
||||
}
|
||||
if (nx->type == TOK_L_BRACE && !p->stop_agg) {
|
||||
// 匿名聚合字面量:. { fields }
|
||||
spl_ast_node_ref_t ref = new_node(p, SPL_AST_EXPR_AGGREGATE_LIT, t);
|
||||
spl_ast_node_t *agg = node_at(p->ast, ref);
|
||||
agg->aggregate_literal.name = NULL;
|
||||
agg->aggregate_literal.fields = parse_aggregate_fields(p);
|
||||
node_at(p->ast, ref)->aggregate_literal.name = NULL;
|
||||
|
||||
spl_ast_node_ref_vec_t fields = parse_aggregate_fields(p);
|
||||
node_at(p->ast, ref)->aggregate_literal.fields = fields;
|
||||
return ref;
|
||||
} else if (nx->type == TOK_L_BRACKET) {
|
||||
// 匿名数组字面量:. [ elems ]
|
||||
@@ -1934,6 +1943,8 @@ static spl_ast_node_ref_t parse_match_expr(parser_t *p) {
|
||||
spl_ast_node_t *arm_node = node_at(p->ast, arm);
|
||||
arm_node->match_arm.pattern = patterns.data[i];
|
||||
arm_node->match_arm.body = bodies.data[i];
|
||||
|
||||
me = node_at(p->ast, ref); /* 每次迭代重新获取 防止 new_node 扩容 */
|
||||
vec_push(me->match_expr.arms, arm);
|
||||
}
|
||||
vec_free(patterns);
|
||||
|
||||
Reference in new issue
Block a user