stage1 整理代码 提供更多错误信息

This commit is contained in:
zzy
2026-07-21 09:56:19 +08:00
parent 459b6188a9
commit 7c5bb41e09
8 changed files with 129 additions and 72 deletions

View File

@@ -46,6 +46,17 @@ static int parse_params_decl(spl_comp_t *ctx, char pnames[][256], int ptypes[])
static int parse_fn_body(spl_comp_t *ctx, const char *fn_name, int ret_type_idx, int nparams,
char pnames[][256], int ptypes[], int is_pub) {
int fi = spl_declare_func(ctx, fn_name, ret_type_idx, nparams, 0, is_pub);
{
spl_func_info_t *f = &vec_at(ctx->funcs, fi);
f->param_type_indices = calloc(nparams, sizeof(int));
f->param_names = calloc(nparams, sizeof(char *));
for (int i = 0; i < nparams; i++) {
f->param_type_indices[i] = ptypes[i];
f->param_names[i] = strdup(pnames[i]);
}
}
ctx->current_func_idx = fi;
ctx->current_ret_type_idx = ret_type_idx;
fa_init(&ctx->emit.frame);
@@ -87,16 +98,6 @@ static int parse_fn_body(spl_comp_t *ctx, const char *fn_name, int ret_type_idx,
spl_emit_defer_epilogue(ctx, ctx->scope_depth);
spl_pop_scope(ctx);
{
spl_func_info_t *f = &vec_at(ctx->funcs, fi);
f->param_type_indices = calloc(nparams, sizeof(int));
f->param_names = calloc(nparams, sizeof(char *));
for (int i = 0; i < nparams; i++) {
f->param_type_indices[i] = ptypes[i];
f->param_names[i] = strdup(pnames[i]);
}
}
emit_return(&ctx->emit, &ctx->tctx, ctx->current_ret_type_idx);
spl_prog_end_func(&ctx->prog, fi);
ctx->current_func_idx = -1;
@@ -207,10 +208,13 @@ static void skip_type_decl(spl_comp_t *ctx) {
int bd = 1;
advance(ctx);
while (bd > 0 && ctx->tok_idx < vec_size(ctx->toks)) {
if (peek(ctx)->type == TOK_L_BRACE)
spl_tok_type_t tt = peek(ctx)->type;
if (tt == TOK_L_BRACE)
bd++;
else if (peek(ctx)->type == TOK_R_BRACE)
else if (tt == TOK_R_BRACE)
bd--;
else if (tt == TOK_EOF)
break;
advance(ctx);
}
} else {
@@ -247,6 +251,8 @@ static void parse_type_body(spl_comp_t *ctx, int container_type_idx, int is_enum
advance(ctx);
} else if (tt == KW_TYPE && depth == 1) {
parse_type_decl(ctx);
} else if (tt == TOK_EOF) {
break;
} else {
advance(ctx);
}
@@ -276,10 +282,34 @@ static void parse_type_body(spl_comp_t *ctx, int container_type_idx, int is_enum
spl_tok_t *name_tok = advance(ctx);
char mname[256];
spl_tok_copy_name(name_tok, mname, sizeof(mname));
/* Count parameters for forward reference */
int pcount = 0;
if (peek(ctx)->type == TOK_L_PAREN) {
advance(ctx); /* ( */
skip_nl(ctx);
if (peek(ctx)->type != TOK_R_PAREN) {
pcount = 1;
for (;;) {
advance(ctx); /* skip param name or type token */
skip_nl(ctx);
if (peek(ctx)->type == TOK_R_PAREN) break;
if (peek(ctx)->type == TOK_COMMA) {
advance(ctx); /* , */
skip_nl(ctx);
pcount++;
}
}
}
advance(ctx); /* ) */
}
char qualified[512];
snprintf(qualified, sizeof(qualified), "%s.%s", cname, mname);
int fi = spl_declare_func(ctx, qualified, -1, 0, 0, 0);
int fi = spl_declare_func(ctx, qualified, -1, pcount, 0, 0);
spl_type_add_method(&ctx->tctx, container_type_idx, mname, fi);
} else if (tt == TOK_EOF) {
break;
} else {
advance(ctx);
}
@@ -335,7 +365,9 @@ static void parse_type_body(spl_comp_t *ctx, int container_type_idx, int is_enum
spl_tok_copy_name(vtok, vname, sizeof(vname));
spl_type_add_variant(&ctx->tctx, container_type_idx, vname,
dtype >= 0 ? dtype : -1);
} else {
} else if (tt == TOK_EOF) {
break;
} else {
char vname[256];
spl_tok_copy_name(vtok, vname, sizeof(vname));
spl_type_add_variant(&ctx->tctx, container_type_idx, vname, -1);
@@ -480,7 +512,7 @@ void spl_parse_prog(spl_comp_t *ctx) {
break;
}
default: {
int prev = ctx->tok_idx;
usize prev = ctx->tok_idx;
spl_parse_stmt(ctx);
if (ctx->tok_idx == prev)
advance(ctx);