From 0892c084ee56b9665b47bf3b84ae1095c377ffd1 Mon Sep 17 00:00:00 2001 From: zzy <2450266535@qq.com> Date: Mon, 6 Jul 2026 15:18:17 +0800 Subject: [PATCH] =?UTF-8?q?stage1=20=E5=AE=8C=E6=88=9014=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stage0/spl_syscall.c | 11 +++++++++-- stage1/spl_expr.c | 28 +++++++++++++++++++++++----- stage1/test11_enum.spl | 1 - 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/stage0/spl_syscall.c b/stage0/spl_syscall.c index 1f60b2b..5d21576 100644 --- a/stage0/spl_syscall.c +++ b/stage0/spl_syscall.c @@ -8,6 +8,7 @@ */ #include "spl_syscall.h" +#include "include/core_map.h" #include "include/core_vec.h" #include "spl_ir.h" #include "spl_vm.h" @@ -121,9 +122,15 @@ static spl_val_t vm_fsize(int nargs, spl_val_t *args) { static spl_val_t vm_read_file(int nargs, spl_val_t *args) { CHECK_NARGS("vm_read_file", 1); const char *path = (const char *)(uintptr_t)args[0]; + if (path == nullptr) { + fprintf(stderr, "filepath can't be null"); + return 1; + } FILE *f = fopen(path, "rb"); - if (!f) - return 0; + if (!f) { + fprintf(stderr, "filepath %s can't be open", path); + return 1; + } fseek(f, 0, SEEK_END); long sz = ftell(f); fseek(f, 0, SEEK_SET); diff --git a/stage1/spl_expr.c b/stage1/spl_expr.c index 11ff812..b09dc61 100644 --- a/stage1/spl_expr.c +++ b/stage1/spl_expr.c @@ -195,6 +195,23 @@ static int spl_resolve_type_member(spl_comp_t *ctx, spl_type_info_t *type, * SIR opcode for binary operator * ============================================================ */ +/* Map assignment operator token (e.g. +=) to corresponding binary operator (e.g. +) */ +static spl_tok_type_t assign_to_binop(spl_tok_type_t t) { + switch (t) { + case TOK_ASSIGN_ADD: return TOK_ADD; + case TOK_ASSIGN_SUB: return TOK_SUB; + case TOK_ASSIGN_MUL: return TOK_MUL; + case TOK_ASSIGN_DIV: return TOK_DIV; + case TOK_ASSIGN_MOD: return TOK_MOD; + case TOK_ASSIGN_AND: return TOK_AND; + case TOK_ASSIGN_OR: return TOK_OR; + case TOK_ASSIGN_XOR: return TOK_XOR; + case TOK_ASSIGN_L_SH: return TOK_L_SH; + case TOK_ASSIGN_R_SH: return TOK_R_SH; + default: return (spl_tok_type_t)-1; + } +} + static int binop_to_sir(spl_tok_type_t t, spl_type_t bt) { int is_signed = (bt == SPL_I32 || bt == SPL_I64 || bt == SPL_I8 || bt == SPL_I16); switch (t) { @@ -981,11 +998,12 @@ static spl_expr_result_t parse_infix(spl_comp_t *ctx, spl_expr_result_t left, sp spl_emit(ctx, SPL_STORE, bt, 0); } else { /* Compound: left = left op right — stack: [addr, rhs] */ - spl_emit(ctx, SPL_DUP, SPL_VOID, 0); /* [addr, rhs, addr] */ - spl_emit(ctx, SPL_LOAD, bt, 0); /* [addr, rhs, old_val] */ - /* FIXME: compound needs to compute old_val op rhs, then store */ - /* For now just drop old_val and store rhs */ - spl_emit(ctx, SPL_DROP, SPL_VOID, 0); /* [addr, rhs] */ + spl_emit(ctx, SPL_PICK, SPL_VOID, 1); /* [addr, rhs, addr] */ + spl_emit(ctx, SPL_LOAD, bt, 0); /* [addr, rhs, old_val] */ + spl_emit(ctx, SPL_SWAP, SPL_VOID, 0); /* [addr, old_val, rhs] */ + int sop = binop_to_sir(assign_to_binop(op), bt); + if (sop >= 0) + spl_emit(ctx, sop, bt, 0); /* [addr, result] */ spl_emit(ctx, SPL_STORE, bt, 0); } } diff --git a/stage1/test11_enum.spl b/stage1/test11_enum.spl index e9a1e88..987ddb9 100644 --- a/stage1/test11_enum.spl +++ b/stage1/test11_enum.spl @@ -13,7 +13,6 @@ type Color = enum { type Expr = enum { val: i32, - add: struct { left: *Expr, right: *Expr }, tag: Tag, type Tag = enum {