This commit is contained in:
blueloveTH 2024-07-06 16:15:40 +08:00
parent 403e3c9f0e
commit c9cbecc401
4 changed files with 36 additions and 10 deletions

View File

@ -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_ROT_THREE, BC_NOARG, self->line); // [b, a, b]
Ctx__emit_(ctx, OP_BINARY_OP, cmp_token2name(self->op), self->line); Ctx__emit_(ctx, OP_BINARY_OP, cmp_token2name(self->op), self->line);
// [b, RES] // [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); 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++) { for(int i = 0; i < jmps.count; i++) {
Ctx__patch_jump(ctx, c11__getitem(int, &jmps, i)); Ctx__patch_jump(ctx, c11__getitem(int, &jmps, i));
} }
c11_vector__dtor(&jmps);
} }
BinaryExpr* BinaryExpr__new(int line, TokenIndex op, bool inplace) { 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); static_assert_expr_size(BinaryExpr);
BinaryExpr* self = PoolExpr_alloc(); BinaryExpr* self = PoolExpr_alloc();
self->vt = &Vt; self->vt = &Vt;

View File

@ -30,7 +30,11 @@ static bool stack_binaryop(pk_VM* self, py_Name op, py_Name rop);
#define THIRD() (self->stack.sp - 3) #define THIRD() (self->stack.sp - 3)
#define FOURTH() (self->stack.sp - 4) #define FOURTH() (self->stack.sp - 4)
#define STACK_SHRINK(n) (self->stack.sp -= n) #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 POP() (--self->stack.sp)
#define POPX() (*--self->stack.sp) #define POPX() (*--self->stack.sp)
#define SP() (self->stack.sp) #define SP() (self->stack.sp)
@ -110,7 +114,11 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
if(p != TOP()) c11_sbuf__write_cstr(&buf, ", "); if(p != TOP()) c11_sbuf__write_cstr(&buf, ", ");
} }
c11_string* stack_str = c11_sbuf__submit(&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); c11_string__delete(stack_str);
#endif #endif

View File

@ -58,8 +58,21 @@ static bool _py_builtins__repr(int argc, py_Ref argv){
return py_repr(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_TValue pk_builtins__register(){
py_Ref builtins = py_newmodule("builtins", NULL); py_Ref builtins = py_newmodule("builtins", NULL);
py_bindnativefunc(builtins, "repr", _py_builtins__repr); py_bindnativefunc(builtins, "repr", _py_builtins__repr);
py_bindnativefunc(builtins, "exit", _py_builtins__exit);
return *builtins; return *builtins;
} }

View File

@ -1,3 +1,7 @@
assert 1 < 2 < 3
assert 4 > 3 >= 3
assert not 1 < 2 > 3
# test int literals # test int literals
assert 0xffff == 65535 assert 0xffff == 65535
assert 0xAAFFFF == 11206655 assert 0xAAFFFF == 11206655
@ -103,11 +107,9 @@ assert ~0 == -1
assert str(1) == '1' assert str(1) == '1'
assert repr(1) == '1' assert repr(1) == '1'
assert not 1 < 2 > 3
assert 1 < 2 < 3 assert 1 < 2 < 3
assert 4 > 3 >= 3 assert 4 > 3 >= 3
assert not 1 < 2 > 3
exit()
# try: # try:
# 1 // 0 # 1 // 0