stage1 重制18测试用例替换成match 完成现有全部测试

This commit is contained in:
zzy
2026-07-11 21:24:52 +08:00
parent 147f26e063
commit 53ae30f1ca
9 changed files with 729 additions and 143 deletions

View File

@@ -329,13 +329,13 @@ static spl_expr_result_t parse_array_literal(spl_comp_t *ctx) {
skip_nl(ctx);
/* Parse array length */
spl_tok_t *len_tok = advance(ctx);
if (len_tok->type != TOK_INT_LITERAL) {
int len_val;
if (!spl_parse_int_literal(ctx, &len_val)) {
spl_comp_error(ctx, "expected array length");
spl_expr_result_t r = {0};
return r;
}
usize len = (usize)strtoull(len_tok->lexeme, NULL, 0);
usize len = (usize)len_val;
skip_nl(ctx);
expect(ctx, TOK_R_BRACKET);
@@ -372,19 +372,6 @@ static spl_expr_result_t parse_array_literal(spl_comp_t *ctx) {
return (spl_expr_result_t){arr_type, 0};
}
/* Determine correct STORE type for a type (handles ptr = 8 bytes). */
static spl_type_t spl_store_type(spl_type_info_t *type) {
if (!type)
return SPL_I32;
if (type->kind == TYPE_BASIC)
return type->basic_type;
if (type->kind == TYPE_PTR)
return SPL_PTR;
if (spl_type_size(type) <= sizeof(spl_val_t))
return SPL_PTR;
return SPL_I32;
}
/* Parse struct/enum literal: Type { .field = val, ... }
* Allocates temp slots for the value, returns lvalue (addr on stack).
* For types fitting in one slot, pushes the packed value directly. */
@@ -392,8 +379,12 @@ static spl_expr_result_t parse_struct_literal(spl_comp_t *ctx, spl_type_info_t *
usize sz = spl_type_size(type);
int base_offset = ctx->current_local_bytes;
ctx->current_local_bytes += (int)((sz + sizeof(spl_val_t) - 1) & ~(sizeof(spl_val_t) - 1));
if (ctx->current_local_bytes > ctx->peak_local_bytes)
ctx->peak_local_bytes = ctx->current_local_bytes;
if (ctx->current_local_bytes - base_offset < (int)sizeof(spl_val_t))
ctx->current_local_bytes = base_offset + (int)sizeof(spl_val_t);
if (ctx->current_local_bytes > ctx->peak_local_bytes)
ctx->peak_local_bytes = ctx->current_local_bytes;
advance(ctx); /* { */
skip_nl(ctx);
@@ -473,8 +464,13 @@ static spl_expr_result_t parse_struct_literal(spl_comp_t *ctx, spl_type_info_t *
* Stack: [field_addr] — store each element at computed offsets */
advance(ctx); /* [ */
skip_nl(ctx);
spl_tok_t *len_tok = advance(ctx);
usize arr_len = (usize)strtoull(len_tok->lexeme, NULL, 0);
int len_val;
if (!spl_parse_int_literal(ctx, &len_val)) {
spl_comp_error(ctx, "expected array length");
spl_expr_result_t r = {0};
return r;
}
usize arr_len = (usize)len_val;
skip_nl(ctx);
expect(ctx, TOK_R_BRACKET);
skip_nl(ctx);
@@ -483,7 +479,7 @@ static spl_expr_result_t parse_struct_literal(spl_comp_t *ctx, spl_type_info_t *
expect(ctx, TOK_L_BRACE);
skip_nl(ctx);
usize stride = spl_type_elem_stride(elem_type);
spl_type_t st = spl_store_type(elem_type);
spl_type_t st = spl_type_emit_type(elem_type);
for (usize i = 0; i < arr_len; i++) {
if (i > 0) {
if (peek(ctx)->type == TOK_COMMA)
@@ -519,7 +515,7 @@ static spl_expr_result_t parse_struct_literal(spl_comp_t *ctx, spl_type_info_t *
} else {
spl_expr_result_t fv = spl_parse_expr(ctx, PREC_MIN);
(void)fv;
spl_emit(ctx, SPL_STORE, spl_store_type(f->type), 0);
spl_emit(ctx, SPL_STORE, spl_type_emit_type(f->type), 0);
}
break;
}
@@ -649,7 +645,7 @@ static spl_expr_result_t parse_struct_literal(spl_comp_t *ctx, spl_type_info_t *
} else {
spl_expr_result_t sfv = spl_parse_expr(ctx, PREC_MIN);
(void)sfv;
spl_emit(ctx, SPL_STORE, spl_store_type(sf->type), 0);
spl_emit(ctx, SPL_STORE, spl_type_emit_type(sf->type), 0);
}
break;
}
@@ -675,7 +671,7 @@ static spl_expr_result_t parse_struct_literal(spl_comp_t *ctx, spl_type_info_t *
spl_emit(ctx, SPL_LADDR, SPL_PTR, base_offset);
spl_emit(ctx, SPL_PUSH, SPL_USIZE, DATA_OFFSET);
spl_emit(ctx, SPL_ADD, SPL_USIZE, 0);
spl_type_t bt = spl_store_type(v->data_type);
spl_type_t bt = spl_type_emit_type(v->data_type);
spl_emit(ctx, SPL_SWAP, SPL_VOID, 0);
spl_emit(ctx, SPL_STORE, bt, 0);
}
@@ -1368,10 +1364,9 @@ static spl_expr_result_t parse_infix(spl_comp_t *ctx, spl_expr_result_t left, sp
ctx->addr_of_mode = saved_addr_of_mode;
if (left.is_lvalue) {
spl_type_t bt = left.type ? (left.type->kind == TYPE_BASIC ? left.type->basic_type
: left.type->kind == TYPE_PTR ? SPL_PTR
: SPL_I32)
: SPL_I32;
spl_type_t bt = left.type
? (left.type->kind == TYPE_BASIC ? left.type->basic_type : SPL_PTR)
: SPL_I32;
if (op == TOK_ASSIGN) {
/* Simple assignment: stack is [addr, rhs] */
/* STORE pops TOS=value, TOS-1=address — already correct order */