stage1 实现部分ast2ir内容并重构stage0 引入并重构ir2vm 构建系统修改部分

This commit is contained in:
zzy committed 2026-08-28 13:01:19 +08:00
1 parent 3d57260f3f
commit 34df303b56
17 files changed
+636 -1800

No files matched your search

+71 -67
View File
@@ -80,7 +80,7 @@ static spl_ast_node_ref_t mk_bin(parser_t *p, const spl_tok_t *loc, spl_ast_node
/* 前向声明 */
static spl_ast_node_ref_t parse_expr(parser_t *p);
static spl_ast_node_ref_vec_t parse_expr_list(parser_t *p);
static spl_ast_node_ref_vec_t parse_block(parser_t *p);
static spl_ast_node_ref_t parse_block(parser_t *p);
static spl_ast_node_ref_t parse_type_expr(parser_t *p);
static spl_ast_node_ref_t parse_type_base(parser_t *p);
static spl_ast_node_ref_t parse_statement(parser_t *p);
@@ -352,8 +352,7 @@ static spl_ast_node_ref_t parse_fn_decl(parser_t *p, spl_ast_node_ref_vec_t attr
advance(p);
spl_ast_node_ref_t type_expr = 0;
spl_ast_node_ref_vec_t block;
vec_init(block);
spl_ast_node_ref_t block = 0;
int has_body = 0;
t = peek(p);
@@ -525,20 +524,20 @@ static spl_ast_node_ref_t parse_container_declaration(parser_t *p) {
// 语句
static spl_ast_node_ref_vec_t parse_block(parser_t *p) {
static spl_ast_node_ref_t parse_block(parser_t *p) {
spl_ast_node_ref_vec_t vec;
vec_init(vec);
const spl_tok_t *t = peek(p);
if (t == NULL || t->type != TOK_L_BRACE) {
SPL_FATAL(t, "expected '{'");
return vec;
return 0;
}
advance(p);
while (!p->failed) {
t = peek(p);
if (!t || t->type == TOK_EOF) {
SPL_FATAL(t, "block missing '}'");
break;
return 0;
}
if (t->type == TOK_R_BRACE) {
advance(p);
@@ -565,7 +564,9 @@ static spl_ast_node_ref_vec_t parse_block(parser_t *p) {
SPL_FATAL(t, "unrecognized statement");
}
}
return vec;
spl_ast_node_ref_t block_expr_ref = new_node(p, SPL_AST_BLOCK_EXPR, t); // FIXME t not here
node_at(p->ast, block_expr_ref)->primary_expr.block_expr = vec;
return block_expr_ref;
}
static spl_ast_node_ref_t parse_statement(parser_t *p) {
@@ -668,15 +669,18 @@ static spl_ast_node_ref_t parse_if(parser_t *p) {
spl_ast_node_ref_t expr = parse_expr(p);
p->stop_agg--;
spl_ast_node_ref_vec_t if_block = parse_block(p);
spl_ast_node_ref_vec_t else_block;
vec_init(else_block);
spl_ast_node_ref_t if_block = parse_block(p);
spl_ast_node_ref_t else_block = 0;
t = peek(p);
if (t && t->type == KW_ELSE) {
advance(p);
t = peek(p);
if (t && t->type == KW_IF) {
vec_push(else_block, parse_if(p));
spl_ast_node_ref_vec_t elseif_block;
vec_init(elseif_block);
vec_push(elseif_block, parse_if(p));
else_block = new_node(p, SPL_AST_BLOCK_EXPR, t); // FIXME t not here
node_at(p->ast, else_block)->primary_expr.block_expr = elseif_block;
} else {
else_block = parse_block(p);
}
@@ -709,7 +713,7 @@ static spl_ast_node_ref_t parse_while(parser_t *p) {
p->stop_agg++;
spl_ast_node_ref_t expr = parse_expr(p);
p->stop_agg--;
spl_ast_node_ref_vec_t block = parse_block(p);
spl_ast_node_ref_t block = parse_block(p);
spl_ast_node_ref_t s = new_node(p, SPL_AST_WHILE_STATEMENT, kw);
node_at(p->ast, s)->while_statement.expr = expr;
node_at(p->ast, s)->while_statement.while_block = block;
@@ -720,7 +724,7 @@ static spl_ast_node_ref_t parse_loop(parser_t *p) {
const spl_tok_t *kw = peek(p);
Assert(kw && kw->type == KW_LOOP);
advance(p);
spl_ast_node_ref_vec_t block = parse_block(p);
spl_ast_node_ref_t block = parse_block(p);
spl_ast_node_ref_t s = new_node(p, SPL_AST_LOOP_STATEMENT, kw);
node_at(p->ast, s)->loop_statement.loop_block = block;
return s;
@@ -768,7 +772,7 @@ static spl_ast_node_ref_t parse_for(parser_t *p) {
advance(p);
vec_push(node_at(p->ast, s)->for_statement.ident_vec, sdupn(t->lexeme, t->len));
}
spl_ast_node_ref_vec_t fblock = parse_block(p);
spl_ast_node_ref_t fblock = parse_block(p);
node_at(p->ast, s)->for_statement.block = fblock;
return s;
}
@@ -810,7 +814,10 @@ static spl_ast_node_ref_t parse_match(parser_t *p) {
spl_ast_node_ref_t s = new_node(p, SPL_AST_MATCH_STATEMENT, kw);
node_at(p->ast, s)->match_statement.expr = expr;
node_at(p->ast, s)->match_statement.paced_exprs = paced_vec;
node_at(p->ast, s)->match_statement.match_block = stmt_vec;
spl_ast_node_ref_t stmt_block = new_node(p, SPL_AST_BLOCK_EXPR, t); // FIXME t not here
node_at(p->ast, stmt_block)->primary_expr.block_expr = stmt_vec;
node_at(p->ast, s)->match_statement.match_block = stmt_block;
return s;
}
@@ -893,9 +900,7 @@ static int parse_match_arm(parser_t *p, spl_ast_node_ref_vec_t *paced_vec,
return 0;
}
spl_ast_node_ref_vec_t bvec = parse_block(p);
spl_ast_node_ref_t body = new_node(p, SPL_AST_BLOCK_EXPR, t);
node_at(p->ast, body)->primary_expr.block_expr = bvec;
spl_ast_node_ref_t body = parse_block(p);
for (usize i = 0; i < modes.size; i++) {
vec_push(*paced_vec, modes.data[i]);
vec_push(*block_vec, body);
@@ -949,8 +954,9 @@ static spl_ast_node_ref_t parse_defer(parser_t *p) {
vec_init(vec);
const spl_tok_t *t = peek(p);
// FIXME parse maybe error
if (t && t->type == TOK_L_BRACE) {
vec = parse_block(p);
vec_push(vec, parse_block(p));
} else if (t && can_start_statement(t->type)) {
spl_ast_node_ref_t st = parse_statement(p);
if (st)
@@ -992,7 +998,7 @@ static spl_ast_node_kind_t assign_op_of(spl_tok_type_t t) {
case TOK_ASSIGN_L_SH:
return SPL_AST_ASSIGN_LSHIFT_EXPR;
case TOK_ASSIGN_R_SH:
return SPL_AST_ASSIGN_USHIFT_EXPR;
return SPL_AST_ASSIGN_RSHIFT_EXPR;
default:
return SPL_AST_COUNT;
}
@@ -1473,9 +1479,7 @@ static spl_ast_node_ref_t parse_primary(parser_t *p) {
case TOK_L_BRACKET:
return parse_array_literal(p);
case TOK_L_BRACE: {
spl_ast_node_ref_vec_t bvec = parse_block(p);
spl_ast_node_ref_t pr = new_node(p, SPL_AST_BLOCK_EXPR, t);
node_at(p->ast, pr)->primary_expr.block_expr = bvec;
spl_ast_node_ref_t pr = parse_block(p);
return pr;
}
default:
@@ -1827,7 +1831,7 @@ void spl_ast_valid(spl_ast_t *ast) {
}
typedef VEC(char) dump_stack_t;
static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t *stack, int last);
static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t *stack, bool last);
void spl_ast_dump(spl_ast_t *ast, spl_ast_node_ref_t node) {
if (!ast || !node || node >= ast->node_buckets.size)
@@ -1843,7 +1847,7 @@ void spl_ast_dump(spl_ast_t *ast, spl_ast_node_ref_t node) {
vec_for(_vec, _i) { dump_node(_ast, vec_at(_vec, _i), _stack, _i + 1 == vec_size(_vec)); } \
} while (0)
static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t *stack, int last) {
static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t *stack, bool last) {
spl_ast_node_t *node = &vec_at(ast->node_buckets, node_ref);
for (usize i = 0; i < vec_size(*stack); i++)
@@ -1869,36 +1873,36 @@ static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t
dump_vec(ast, node->fn_decl.attr_list, stack);
dump_vec(ast, node->fn_decl.param_list, stack);
if (node->fn_decl.type_expr) {
last = node->kind == SPL_AST_FN_DECL ? 1 : 0;
last = node->kind == SPL_AST_FN_DECL ? true : false;
dump_node(ast, node->fn_decl.type_expr, stack, last);
}
dump_vec(ast, node->fn_decl.block, stack);
dump_node(ast, node->fn_decl.block, stack, true);
break;
case SPL_AST_TYPE_DECL:
if (node->type_decl.type_expr)
dump_node(ast, node->type_decl.type_expr, stack, 1);
dump_node(ast, node->type_decl.type_expr, stack, true);
break;
case SPL_AST_VAR_DECL:
case SPL_AST_CONST_DECL:
dump_vec(ast, node->var_const_decl.attr_list, stack);
if (node->var_const_decl.type_expr)
dump_node(ast, node->var_const_decl.type_expr, stack, 0);
dump_node(ast, node->var_const_decl.type_expr, stack, false);
if (node->var_const_decl.expr)
dump_node(ast, node->var_const_decl.expr, stack, 1);
dump_node(ast, node->var_const_decl.expr, stack, true);
break;
case SPL_AST_MEMBER_DECL:
dump_vec(ast, node->member_decl.attr_list, stack);
if (node->member_decl.type_expr)
dump_node(ast, node->member_decl.type_expr, stack, 1);
dump_node(ast, node->member_decl.type_expr, stack, true);
break;
case SPL_AST_PARAM_DECL:
dump_vec(ast, node->param_decl.attr_list, stack);
if (node->param_decl.type_expr)
dump_node(ast, node->param_decl.type_expr, stack, 1);
dump_node(ast, node->param_decl.type_expr, stack, true);
break;
case SPL_AST_ATTR_ITEM:
@@ -1907,31 +1911,31 @@ static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t
case SPL_AST_ARGG_INIT_ITEM:
if (node->aggregate_init_item.expr)
dump_node(ast, node->aggregate_init_item.expr, stack, 1);
dump_node(ast, node->aggregate_init_item.expr, stack, true);
break;
case SPL_AST_IF_STATEMENT:
if (node->if_statement.expr)
dump_node(ast, node->if_statement.expr, stack, 0);
dump_vec(ast, node->if_statement.if_block, stack);
dump_vec(ast, node->if_statement.else_block, stack);
dump_node(ast, node->if_statement.expr, stack, false);
dump_node(ast, node->if_statement.if_block, stack, false);
dump_node(ast, node->if_statement.else_block, stack, true);
break;
case SPL_AST_IFVAR_STATEMENT:
if (node->ifvar_statement.packed_expr)
dump_node(ast, node->ifvar_statement.packed_expr, stack, 0);
dump_vec(ast, node->ifvar_statement.if_block, stack);
dump_vec(ast, node->ifvar_statement.else_block, stack);
dump_node(ast, node->ifvar_statement.packed_expr, stack, false);
dump_node(ast, node->ifvar_statement.if_block, stack, false);
dump_node(ast, node->ifvar_statement.else_block, stack, true);
break;
case SPL_AST_WHILE_STATEMENT:
if (node->while_statement.expr)
dump_node(ast, node->while_statement.expr, stack, 0);
dump_vec(ast, node->while_statement.while_block, stack);
dump_node(ast, node->while_statement.expr, stack, false);
dump_node(ast, node->while_statement.while_block, stack, true);
break;
case SPL_AST_LOOP_STATEMENT:
dump_vec(ast, node->loop_statement.loop_block, stack);
dump_node(ast, node->loop_statement.loop_block, stack, true);
break;
case SPL_AST_FOR_STATEMENT:
@@ -1943,19 +1947,19 @@ static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t
i + 1 == vec_size(node->for_statement.ident_vec) ? "`-" : "|-",
node->for_statement.ident_vec.data[i]);
}
dump_vec(ast, node->for_statement.block, stack);
dump_node(ast, node->for_statement.block, stack, true);
break;
case SPL_AST_MATCH_STATEMENT:
if (node->match_statement.expr)
dump_node(ast, node->match_statement.expr, stack, 0);
dump_node(ast, node->match_statement.expr, stack, false);
dump_vec(ast, node->match_statement.paced_exprs, stack);
dump_vec(ast, node->match_statement.match_block, stack);
dump_node(ast, node->match_statement.match_block, stack, true);
break;
case SPL_AST_RET_STATEMENT:
if (node->ret_statement.expr)
dump_node(ast, node->ret_statement.expr, stack, 1);
dump_node(ast, node->ret_statement.expr, stack, true);
break;
case SPL_AST_DEFER_STATEMENT:
@@ -1964,7 +1968,7 @@ static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t
case SPL_AST_PACKED_EXPR:
if (node->packed_expr.expr)
dump_node(ast, node->packed_expr.expr, stack, 1);
dump_node(ast, node->packed_expr.expr, stack, true);
break;
case SPL_AST_ASSIGN_EXPR:
@@ -1977,7 +1981,7 @@ static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t
case SPL_AST_ASSIGN_OR_EXPR:
case SPL_AST_ASSIGN_XOR_EXPR:
case SPL_AST_ASSIGN_LSHIFT_EXPR:
case SPL_AST_ASSIGN_USHIFT_EXPR:
case SPL_AST_ASSIGN_RSHIFT_EXPR:
case SPL_AST_BOOL_OR_EXPR:
case SPL_AST_BOOL_AND_EXPR:
case SPL_AST_BIT_OR_EXPR:
@@ -1998,9 +2002,9 @@ static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t
case SPL_AST_DIV_EXPR:
case SPL_AST_MOD_EXPR:
if (node->op_expr.left)
dump_node(ast, node->op_expr.left, stack, 0);
dump_node(ast, node->op_expr.left, stack, false);
if (node->op_expr.right)
dump_node(ast, node->op_expr.right, stack, 1);
dump_node(ast, node->op_expr.right, stack, true);
break;
case SPL_AST_MINUS_EXPR:
@@ -2008,42 +2012,42 @@ static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t
case SPL_AST_BIT_NOT_EXPR:
case SPL_AST_ADDRESS_EXPR:
if (node->prefix_expr.postfix_expr)
dump_node(ast, node->prefix_expr.postfix_expr, stack, 1);
dump_node(ast, node->prefix_expr.postfix_expr, stack, true);
break;
case SPL_AST_CALL_EXPR:
if (node->postfix_expr.primary_expr)
dump_node(ast, node->postfix_expr.primary_expr, stack, 0);
dump_node(ast, node->postfix_expr.primary_expr, stack, false);
dump_vec(ast, node->postfix_expr.call_expr, stack);
break;
case SPL_AST_FIELD_EXPR:
case SPL_AST_DEREF_EXPR:
if (node->postfix_expr.primary_expr)
dump_node(ast, node->postfix_expr.primary_expr, stack, 1);
dump_node(ast, node->postfix_expr.primary_expr, stack, true);
break;
case SPL_AST_INDEX_EXPR:
if (node->postfix_expr.primary_expr)
dump_node(ast, node->postfix_expr.primary_expr, stack, 0);
dump_node(ast, node->postfix_expr.primary_expr, stack, false);
if (node->postfix_expr.index_expr)
dump_node(ast, node->postfix_expr.index_expr, stack, 1);
dump_node(ast, node->postfix_expr.index_expr, stack, true);
break;
case SPL_AST_SLICE_EXPR:
if (node->postfix_expr.primary_expr)
dump_node(ast, node->postfix_expr.primary_expr, stack, 0);
dump_node(ast, node->postfix_expr.primary_expr, stack, false);
if (node->postfix_expr.slice_expr.begin)
dump_node(ast, node->postfix_expr.slice_expr.begin, stack, 0);
dump_node(ast, node->postfix_expr.slice_expr.begin, stack, false);
if (node->postfix_expr.slice_expr.end)
dump_node(ast, node->postfix_expr.slice_expr.end, stack, 1);
dump_node(ast, node->postfix_expr.slice_expr.end, stack, true);
break;
case SPL_AST_AS_EXPR:
if (node->postfix_expr.primary_expr)
dump_node(ast, node->postfix_expr.primary_expr, stack, 0);
dump_node(ast, node->postfix_expr.primary_expr, stack, false);
if (node->postfix_expr.type_expr)
dump_node(ast, node->postfix_expr.type_expr, stack, 1);
dump_node(ast, node->postfix_expr.type_expr, stack, true);
break;
case SPL_AST_EXPR_INTEGER_LIT:
@@ -2063,12 +2067,12 @@ static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t
case SPL_AST_EXPR_EXPR:
if (node->primary_expr.expr)
dump_node(ast, node->primary_expr.expr, stack, 1);
dump_node(ast, node->primary_expr.expr, stack, true);
break;
case SPL_AST_ARRAY_LIT:
if (node->primary_expr.array_lit_expr.type_expr)
dump_node(ast, node->primary_expr.array_lit_expr.type_expr, stack, 0);
dump_node(ast, node->primary_expr.array_lit_expr.type_expr, stack, false);
dump_vec(ast, node->primary_expr.array_lit_expr.expr_list, stack);
break;
@@ -2084,7 +2088,7 @@ static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t
dump_vec(ast, node->type_expr.attr_list, stack);
dump_vec(ast, node->type_expr.fn_type.param_list, stack);
if (node->type_expr.fn_type.type_expr)
dump_node(ast, node->type_expr.fn_type.type_expr, stack, 1);
dump_node(ast, node->type_expr.fn_type.type_expr, stack, true);
break;
case SPL_AST_BASE_TYPE_PATH:
@@ -2094,21 +2098,21 @@ static void dump_node(spl_ast_t *ast, spl_ast_node_ref_t node_ref, dump_stack_t
case SPL_AST_TYPE_POINTER:
dump_vec(ast, node->type_expr.attr_list, stack);
if (node->type_expr.pointer_type.pointee)
dump_node(ast, node->type_expr.pointer_type.pointee, stack, 1);
dump_node(ast, node->type_expr.pointer_type.pointee, stack, true);
break;
case SPL_AST_TYPE_SLICE:
dump_vec(ast, node->type_expr.attr_list, stack);
if (node->type_expr.slice_type.element)
dump_node(ast, node->type_expr.slice_type.element, stack, 1);
dump_node(ast, node->type_expr.slice_type.element, stack, true);
break;
case SPL_AST_TYPE_ARRAY:
dump_vec(ast, node->type_expr.attr_list, stack);
if (node->type_expr.array_type.element)
dump_node(ast, node->type_expr.array_type.element, stack, 0);
dump_node(ast, node->type_expr.array_type.element, stack, false);
if (node->type_expr.array_type.size)
dump_node(ast, node->type_expr.array_type.size, stack, 1);
dump_node(ast, node->type_expr.array_type.size, stack, true);
break;
case SPL_AST_TYPE_STRUCT: