stage1 整理代码 提供更多错误信息
This commit is contained in:
@@ -67,10 +67,17 @@ void spl_comp_reset(spl_comp_t *ctx) {
|
||||
}
|
||||
|
||||
void spl_comp_error(spl_comp_t *ctx, const char *fmt, ...) {
|
||||
const char *loc = spl_tok_loc_str(ctx);
|
||||
int loc_len = (int)strlen(loc);
|
||||
memcpy(ctx->error_msg, loc, loc_len);
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vsnprintf(ctx->error_msg, COMP_ERROR_MAX - 1, fmt, args);
|
||||
int n = vsnprintf(ctx->error_msg + loc_len, COMP_ERROR_MAX - loc_len - 1, fmt, args);
|
||||
va_end(args);
|
||||
if (n >= 0 && loc_len + n < COMP_ERROR_MAX - 2) {
|
||||
ctx->error_msg[loc_len + n] = '\n';
|
||||
ctx->error_msg[loc_len + n + 1] = '\0';
|
||||
}
|
||||
ctx->has_error = 1;
|
||||
}
|
||||
|
||||
@@ -209,53 +216,7 @@ int spl_add_global_data(spl_comp_t *ctx, void *data, usize size) {
|
||||
return spl_prog_add_data(&ctx->prog, data, size);
|
||||
}
|
||||
|
||||
/* ---- Defer ---- */
|
||||
|
||||
void spl_emit_defer(spl_comp_t *ctx) {
|
||||
if (ctx->defer_count >= DEFER_MAX)
|
||||
return;
|
||||
|
||||
spl_val_t jmp_skip = emit_jmp_here(&ctx->emit);
|
||||
|
||||
spl_defer_entry_t *e = &ctx->defer_stack[ctx->defer_count];
|
||||
e->body_start = jmp_skip + 1;
|
||||
e->jmp_exit = 0;
|
||||
e->depth = ctx->scope_depth;
|
||||
e->count_at_decl = ctx->defer_count;
|
||||
ctx->defer_count++;
|
||||
}
|
||||
|
||||
void spl_emit_defer_epilogue(spl_comp_t *ctx, int depth) {
|
||||
for (int i = 0; i < ctx->defer_count; i++) {
|
||||
if (ctx->defer_stack[i].depth != depth)
|
||||
continue;
|
||||
spl_defer_entry_t *e = &ctx->defer_stack[i];
|
||||
|
||||
spl_val_t skip_addr = e->body_start - 1;
|
||||
spl_val_t skip_target = e->jmp_exit + 1;
|
||||
emit_patch(&ctx->emit, skip_addr, skip_target - skip_addr - 1);
|
||||
}
|
||||
|
||||
for (int i = ctx->defer_count - 1; i >= 0; i--) {
|
||||
if (ctx->defer_stack[i].depth != depth)
|
||||
continue;
|
||||
spl_defer_entry_t *e = &ctx->defer_stack[i];
|
||||
|
||||
spl_val_t here = vec_size(ctx->prog.insns);
|
||||
spl_val_t jmp_offset = (spl_val_t)((isize)e->body_start - (isize)here - 1);
|
||||
emit_jmp(&ctx->emit, jmp_offset);
|
||||
|
||||
if (e->jmp_exit > 0)
|
||||
emit_patch(&ctx->emit, e->jmp_exit, here + 1 - e->jmp_exit - 1);
|
||||
}
|
||||
|
||||
int new_count = 0;
|
||||
for (int i = 0; i < ctx->defer_count; i++) {
|
||||
if (ctx->defer_stack[i].depth != depth)
|
||||
ctx->defer_stack[new_count++] = ctx->defer_stack[i];
|
||||
}
|
||||
ctx->defer_count = new_count;
|
||||
}
|
||||
|
||||
/* ---- Register runtime natives ---- */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user