From c9cbecc4010b3b86c4a50dc220b730c0be04f76c Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 6 Jul 2024 16:15:40 +0800 Subject: [PATCH] some fix --- src/compiler/compiler.c | 7 +++++-- src/interpreter/ceval.c | 18 +++++++++++++----- src/public/modules.c | 13 +++++++++++++ tests/01_int.py | 8 +++++--- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 05e17eef..74b5c1b4 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -879,7 +879,7 @@ static void _emit_compare(BinaryExpr* self, Ctx* ctx, c11_vector* jmps) { Ctx__emit_(ctx, OP_ROT_THREE, BC_NOARG, self->line); // [b, a, b] Ctx__emit_(ctx, OP_BINARY_OP, cmp_token2name(self->op), self->line); // [b, RES] - int index = Ctx__emit_(ctx, OP_JUMP_IF_FALSE_OR_POP, BC_NOARG, self->line); + int index = Ctx__emit_(ctx, OP_SHORTCUT_IF_FALSE_OR_POP, BC_NOARG, self->line); c11_vector__push(int, jmps, index); } @@ -953,10 +953,13 @@ static void BinaryExpr__emit_(Expr* self_, Ctx* ctx) { for(int i = 0; i < jmps.count; i++) { Ctx__patch_jump(ctx, c11__getitem(int, &jmps, i)); } + c11_vector__dtor(&jmps); } BinaryExpr* BinaryExpr__new(int line, TokenIndex op, bool inplace) { - const static ExprVt Vt = {.emit_ = BinaryExpr__emit_, .dtor = BinaryExpr__dtor}; + const static ExprVt Vt = {.emit_ = BinaryExpr__emit_, + .dtor = BinaryExpr__dtor, + .is_binary = true}; static_assert_expr_size(BinaryExpr); BinaryExpr* self = PoolExpr_alloc(); self->vt = &Vt; diff --git a/src/interpreter/ceval.c b/src/interpreter/ceval.c index aae34418..77aecc82 100644 --- a/src/interpreter/ceval.c +++ b/src/interpreter/ceval.c @@ -30,7 +30,11 @@ static bool stack_binaryop(pk_VM* self, py_Name op, py_Name rop); #define THIRD() (self->stack.sp - 3) #define FOURTH() (self->stack.sp - 4) #define STACK_SHRINK(n) (self->stack.sp -= n) -#define PUSH(v) (*self->stack.sp++ = *v) +#define PUSH(v) \ + do { \ + *self->stack.sp = *v; \ + self->stack.sp++; \ + } while(0) #define POP() (--self->stack.sp) #define POPX() (*--self->stack.sp) #define SP() (self->stack.sp) @@ -52,7 +56,7 @@ static bool stack_binaryop(pk_VM* self, py_Name op, py_Name rop); PUSH(&self->last_retval); \ goto __NEXT_FRAME; \ case RES_ERROR: goto __ERROR; \ - default: c11__unreachedable(); \ + default: c11__unreachedable(); \ } \ } while(0) @@ -86,7 +90,7 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) { c11_sbuf buf; c11_sbuf__ctor(&buf); for(py_Ref p = self->stack.begin; p != SP(); p++) { - switch(p->type){ + switch(p->type) { case 0: c11_sbuf__write_cstr(&buf, "nil"); break; case tp_int: c11_sbuf__write_i64(&buf, p->_i64); break; case tp_float: c11_sbuf__write_f64(&buf, p->_f64, -1); break; @@ -102,7 +106,7 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) { pk_sprintf(&buf, "%q", (c11_sv){data, size}); break; } - default:{ + default: { pk_sprintf(&buf, "(%t)", p->type); break; } @@ -110,7 +114,11 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) { if(p != TOP()) c11_sbuf__write_cstr(&buf, ", "); } c11_string* stack_str = c11_sbuf__submit(&buf); - printf("L%-3d: %-25s %-6d [%s]\n", Frame__lineno(frame), pk_opname(byte.op), byte.arg, stack_str->data); + printf("L%-3d: %-25s %-6d [%s]\n", + Frame__lineno(frame), + pk_opname(byte.op), + byte.arg, + stack_str->data); c11_string__delete(stack_str); #endif diff --git a/src/public/modules.c b/src/public/modules.c index 37876230..9549170c 100644 --- a/src/public/modules.c +++ b/src/public/modules.c @@ -58,8 +58,21 @@ static bool _py_builtins__repr(int argc, py_Ref argv){ return py_repr(argv); } +static bool _py_builtins__exit(int argc, py_Ref argv){ + int code = 0; + if(argc > 1) return TypeError("exit() takes at most 1 argument"); + if(argc == 1){ + PY_CHECK_ARG_TYPE(0, tp_int); + code = py_toint(argv); + } + // return py_exception("SystemExit", "%d", code); + exit(code); + return false; +} + py_TValue pk_builtins__register(){ py_Ref builtins = py_newmodule("builtins", NULL); py_bindnativefunc(builtins, "repr", _py_builtins__repr); + py_bindnativefunc(builtins, "exit", _py_builtins__exit); return *builtins; } \ No newline at end of file diff --git a/tests/01_int.py b/tests/01_int.py index 06ba18c5..e6a53481 100644 --- a/tests/01_int.py +++ b/tests/01_int.py @@ -1,3 +1,7 @@ +assert 1 < 2 < 3 +assert 4 > 3 >= 3 +assert not 1 < 2 > 3 + # test int literals assert 0xffff == 65535 assert 0xAAFFFF == 11206655 @@ -103,11 +107,9 @@ assert ~0 == -1 assert str(1) == '1' assert repr(1) == '1' -assert not 1 < 2 > 3 assert 1 < 2 < 3 assert 4 > 3 >= 3 - -exit() +assert not 1 < 2 > 3 # try: # 1 // 0