stage1 实现06测试

This commit is contained in:
zzy
2026-07-05 22:47:00 +08:00
parent e2e0ebc21f
commit 67c8a137dd
6 changed files with 365 additions and 132 deletions

View File

@@ -638,18 +638,37 @@ spl_expr_result_t spl_parse_expr(spl_comp_t *ctx, int min_prec) {
is_slice = 1;
advance(ctx); /* skip .. */
spl_expr_result_t end_expr = {0};
if (peek(ctx)->type != TOK_R_BRACKET) {
int has_explicit_end = (peek(ctx)->type != TOK_R_BRACKET);
if (has_explicit_end) {
end_expr = spl_parse_expr(ctx, PREC_MIN);
}
expect(ctx, TOK_R_BRACKET);
/* Push implicit end value (array length/slice len) before the outer
* type-check so it always runs regardless of left.type validity. */
if (!has_explicit_end) {
if (left.type && left.type->kind == TYPE_ARRAY) {
spl_emit(ctx, SPL_PUSH, SPL_U64, left.type->array_len);
} else if (left.type && left.type->kind == TYPE_SLICE) {
/* TYPE_SLICE: load len from struct at offset sizeof(spl_val_t) */
spl_emit(ctx, SPL_PICK, SPL_VOID, 1);
spl_emit(ctx, SPL_PUSH, SPL_U64, sizeof(spl_val_t));
spl_emit(ctx, SPL_ADD, SPL_U64, 0);
spl_emit(ctx, SPL_LOAD, SPL_I32, 0);
}
}
/* Generate slice: compute ptr = base + begin * stride, len = end - begin */
if (left.type && (left.type->kind == TYPE_ARRAY || left.type->kind == TYPE_SLICE)) {
usize stride = (left.type->kind == TYPE_ARRAY) ? sizeof(spl_val_t) : (left.type->elem ? left.type->elem->byte_size : 4);
/* For TYPE_SLICE, load data ptr from slice struct first */
/* For TYPE_SLICE: convert [struct_addr, begin, end] → [data_ptr, begin, end] */
if (left.type->kind == TYPE_SLICE) {
spl_emit(ctx, SPL_PICK, SPL_VOID, 2);
spl_emit(ctx, SPL_LOAD, SPL_PTR, 0);
spl_emit(ctx, SPL_ROT, SPL_VOID, 0);
spl_emit(ctx, SPL_ROT, SPL_VOID, 0);
spl_emit(ctx, SPL_DROP, SPL_VOID, 0);
}
emit_slice_create(ctx, stride);