stage1 完成14测试
This commit is contained in:
@@ -8,6 +8,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "spl_syscall.h"
|
#include "spl_syscall.h"
|
||||||
|
#include "include/core_map.h"
|
||||||
#include "include/core_vec.h"
|
#include "include/core_vec.h"
|
||||||
#include "spl_ir.h"
|
#include "spl_ir.h"
|
||||||
#include "spl_vm.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) {
|
static spl_val_t vm_read_file(int nargs, spl_val_t *args) {
|
||||||
CHECK_NARGS("vm_read_file", 1);
|
CHECK_NARGS("vm_read_file", 1);
|
||||||
const char *path = (const char *)(uintptr_t)args[0];
|
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");
|
FILE *f = fopen(path, "rb");
|
||||||
if (!f)
|
if (!f) {
|
||||||
return 0;
|
fprintf(stderr, "filepath %s can't be open", path);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
fseek(f, 0, SEEK_END);
|
fseek(f, 0, SEEK_END);
|
||||||
long sz = ftell(f);
|
long sz = ftell(f);
|
||||||
fseek(f, 0, SEEK_SET);
|
fseek(f, 0, SEEK_SET);
|
||||||
|
|||||||
@@ -195,6 +195,23 @@ static int spl_resolve_type_member(spl_comp_t *ctx, spl_type_info_t *type,
|
|||||||
* SIR opcode for binary operator
|
* 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) {
|
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);
|
int is_signed = (bt == SPL_I32 || bt == SPL_I64 || bt == SPL_I8 || bt == SPL_I16);
|
||||||
switch (t) {
|
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);
|
spl_emit(ctx, SPL_STORE, bt, 0);
|
||||||
} else {
|
} else {
|
||||||
/* Compound: left = left op right — stack: [addr, rhs] */
|
/* Compound: left = left op right — stack: [addr, rhs] */
|
||||||
spl_emit(ctx, SPL_DUP, SPL_VOID, 0); /* [addr, rhs, addr] */
|
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_LOAD, bt, 0); /* [addr, rhs, old_val] */
|
||||||
/* FIXME: compound needs to compute old_val op rhs, then store */
|
spl_emit(ctx, SPL_SWAP, SPL_VOID, 0); /* [addr, old_val, rhs] */
|
||||||
/* For now just drop old_val and store rhs */
|
int sop = binop_to_sir(assign_to_binop(op), bt);
|
||||||
spl_emit(ctx, SPL_DROP, SPL_VOID, 0); /* [addr, rhs] */
|
if (sop >= 0)
|
||||||
|
spl_emit(ctx, sop, bt, 0); /* [addr, result] */
|
||||||
spl_emit(ctx, SPL_STORE, bt, 0);
|
spl_emit(ctx, SPL_STORE, bt, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ type Color = enum {
|
|||||||
|
|
||||||
type Expr = enum {
|
type Expr = enum {
|
||||||
val: i32,
|
val: i32,
|
||||||
add: struct { left: *Expr, right: *Expr },
|
|
||||||
tag: Tag,
|
tag: Tag,
|
||||||
|
|
||||||
type Tag = enum {
|
type Tag = enum {
|
||||||
|
|||||||
Reference in New Issue
Block a user