stage1 优化代码 完成16测试

This commit is contained in:
zzy
2026-07-07 10:40:30 +08:00
parent 69cea030dc
commit 3cf11f922e
14 changed files with 849 additions and 228 deletions

View File

@@ -1,10 +1,10 @@
/* spl_comp.c — SPL compiler main logic and codegen helpers */
#include "spl_comp.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
/* ---- Compiler context init/drop ---- */
@@ -17,14 +17,14 @@ void spl_comp_init(spl_comp_t *ctx) {
map_init(ctx->const_values, MAP_HASH_STR, MAP_CMP_STR);
spl_prog_init(&ctx->prog);
ctx->error_msg[0] = '\0';
ctx->current_type_name = NULL;
}
void spl_comp_drop(spl_comp_t *ctx) {
if (!ctx) return;
if (!ctx)
return;
spl_tok_vec_drop(&ctx->toks);
vec_for(ctx->scopes, i) {
vec_free(vec_at(ctx->scopes, i).vars);
}
vec_for(ctx->scopes, i) { vec_free(vec_at(ctx->scopes, i).vars); }
vec_free(ctx->scopes);
vec_free(ctx->funcs);
/* Free type defs — complex, leak for now in bootstrap */
@@ -38,9 +38,7 @@ void spl_comp_reset(spl_comp_t *ctx) {
/* Keep prog, reset everything else */
spl_tok_vec_drop(&ctx->toks);
vec_init(ctx->toks);
vec_for(ctx->scopes, i) {
vec_free(vec_at(ctx->scopes, i).vars);
}
vec_for(ctx->scopes, i) { vec_free(vec_at(ctx->scopes, i).vars); }
vec_free(ctx->scopes);
vec_init(ctx->scopes);
ctx->scope_depth = 0;
@@ -57,6 +55,7 @@ void spl_comp_reset(spl_comp_t *ctx) {
ctx->defer_count = 0;
ctx->next_gdata_idx = 0;
ctx->addr_of_mode = 0;
ctx->current_type_name = NULL;
free(ctx->break_patches);
ctx->break_patches = NULL;
}
@@ -86,13 +85,9 @@ spl_val_t spl_emit_jmp(spl_comp_t *ctx) {
return spl_emit(ctx, SPL_JMP, SPL_VOID, 0);
}
spl_val_t spl_emit_bz(spl_comp_t *ctx) {
return spl_emit(ctx, SPL_BZ, SPL_VOID, 0);
}
spl_val_t spl_emit_bz(spl_comp_t *ctx) { return spl_emit(ctx, SPL_BZ, SPL_VOID, 0); }
spl_val_t spl_emit_bnz(spl_comp_t *ctx) {
return spl_emit(ctx, SPL_BNZ, SPL_VOID, 0);
}
spl_val_t spl_emit_bnz(spl_comp_t *ctx) { return spl_emit(ctx, SPL_BNZ, SPL_VOID, 0); }
void spl_patch_to_here(spl_comp_t *ctx, spl_val_t addr) {
/* Compute relative offset: target - (source + 1) */
@@ -113,9 +108,7 @@ void spl_push_scope(spl_comp_t *ctx) {
void spl_pop_scope(spl_comp_t *ctx) {
if (vec_size(ctx->scopes) > 0) {
spl_scope_t *scope = &vec_at(ctx->scopes, vec_size(ctx->scopes) - 1);
vec_for(scope->vars, i) {
free(vec_at(scope->vars, i).name);
}
vec_for(scope->vars, i) { free(vec_at(scope->vars, i).name); }
vec_free(scope->vars);
ctx->scope_depth--;
ctx->scopes.size--;
@@ -166,8 +159,8 @@ int spl_get_var_slot(spl_comp_t *ctx, const char *name) {
/* ---- Function management ---- */
int spl_declare_func(spl_comp_t *ctx, const char *name, spl_type_info_t *ret_type,
int nparams, int is_extern, int is_pub) {
int spl_declare_func(spl_comp_t *ctx, const char *name, spl_type_info_t *ret_type, int nparams,
int is_extern, int is_pub) {
spl_func_info_t fi;
memset(&fi, 0, sizeof(fi));
fi.name = strdup(name);
@@ -197,7 +190,7 @@ int spl_lookup_func(spl_comp_t *ctx, const char *name) {
/* ---- String/data management ---- */
int spl_add_string(spl_comp_t *ctx, const char *str) {
return spl_add_global_data(ctx, (void*)str, strlen(str) + 1);
return spl_add_global_data(ctx, (void *)str, strlen(str) + 1);
}
int spl_add_global_data(spl_comp_t *ctx, void *data, usize size) {
@@ -295,6 +288,7 @@ int spl_compile(spl_comp_t *ctx, const char *source, const char *fname) {
/* Phase 2-4: Parse and codegen */
spl_parse_prog(ctx);
if (ctx->has_error) return -1;
if (ctx->has_error)
return -1;
return 0;
}