This commit is contained in:
blueloveTH 2024-08-05 17:48:06 +08:00
parent 9fbaca3b13
commit f28335f1f7
5 changed files with 43 additions and 17 deletions

View File

@ -271,12 +271,12 @@ FrameResult VM__run_top_frame(VM* self) {
if(magic->type == tp_nativefunc) { if(magic->type == tp_nativefunc) {
if(!magic->_cfunc(2, SECOND())) goto __ERROR; if(!magic->_cfunc(2, SECOND())) goto __ERROR;
POP(); POP();
*TOP() = self->last_retval;
} else { } else {
INSERT_THIRD(); // [?, a, b] INSERT_THIRD(); // [?, a, b]
*THIRD() = *magic; // [__getitem__, a, b] *THIRD() = *magic; // [__getitem__, a, b]
if(!py_vectorcall(1, 0)) goto __ERROR; if(!py_vectorcall(1, 0)) goto __ERROR;
} }
*TOP() = self->last_retval;
DISPATCH(); DISPATCH();
} }
TypeError("'%t' object is not subscriptable", SECOND()->type); TypeError("'%t' object is not subscriptable", SECOND()->type);
@ -423,6 +423,7 @@ FrameResult VM__run_top_frame(VM* self) {
py_newint(SP()++, 0); // [complex, NULL, 0] py_newint(SP()++, 0); // [complex, NULL, 0]
*SP()++ = tmp; // [complex, NULL, 0, x] *SP()++ = tmp; // [complex, NULL, 0, x]
if(!py_vectorcall(2, 0)) goto __ERROR; if(!py_vectorcall(2, 0)) goto __ERROR;
PUSH(py_retval());
DISPATCH(); DISPATCH();
} }
case OP_BUILD_BYTES: { case OP_BUILD_BYTES: {
@ -477,7 +478,9 @@ FrameResult VM__run_top_frame(VM* self) {
py_Name id_add = py_name("add"); py_Name id_add = py_name("add");
for(int i = 0; i < byte.arg; i++) { for(int i = 0; i < byte.arg; i++) {
py_push(TOP()); py_push(TOP());
py_pushmethod(id_add); if(!py_pushmethod(id_add)) {
c11__abort("OP_BUILD_SET: failed to load method 'add'");
}
py_push(begin + i); py_push(begin + i);
if(!py_vectorcall(1, 0)) goto __ERROR; if(!py_vectorcall(1, 0)) goto __ERROR;
} }
@ -532,14 +535,14 @@ FrameResult VM__run_top_frame(VM* self) {
if(magic->type == tp_nativefunc) { if(magic->type == tp_nativefunc) {
if(!magic->_cfunc(2, SECOND())) goto __ERROR; if(!magic->_cfunc(2, SECOND())) goto __ERROR;
POP(); POP();
*TOP() = self->last_retval;
} else { } else {
INSERT_THIRD(); // [?, b, a] INSERT_THIRD(); // [?, b, a]
*THIRD() = *magic; // [__contains__, a, b] *THIRD() = *magic; // [__contains__, a, b]
if(!py_vectorcall(1, 0)) goto __ERROR; if(!py_vectorcall(1, 0)) goto __ERROR;
} }
bool res = py_tobool(TOP()); bool res = py_tobool(py_retval());
if(byte.arg) py_newbool(TOP(), !res); if(byte.arg) res = !res;
py_newbool(SP()++, res);
DISPATCH(); DISPATCH();
} }
TypeError("'%t' type does not support '__contains__'", SECOND()->type); TypeError("'%t' type does not support '__contains__'", SECOND()->type);
@ -715,6 +718,14 @@ FrameResult VM__run_top_frame(VM* self) {
DISPATCH(); DISPATCH();
} }
case OP_SET_ADD: { case OP_SET_ADD: {
// [set, iter, value]
py_push(THIRD()); // [| set]
if(!py_pushmethod(py_name("add"))) {
c11__abort("OP_SET_ADD: failed to load method 'add'");
} // [|add() set]
py_push(THIRD());
if(!py_vectorcall(1, 0)) goto __ERROR;
POP();
DISPATCH(); DISPATCH();
} }
///////// /////////

View File

@ -179,6 +179,15 @@ void VM__ctor(VM* self) {
pk__add_module_math(); pk__add_module_math();
pk__add_module_dis(); pk__add_module_dis();
// add python builtins
do {
bool ok = py_exec(kPythonLibs__set, "<builtins>", EXEC_MODE, &self->builtins);
if(!ok) {
py_printexc();
c11__abort("failed to load python builtins!");
}
} while(0);
self->main = *py_newmodule("__main__"); self->main = *py_newmodule("__main__");
} }
@ -562,7 +571,7 @@ void ManagedHeap__mark(ManagedHeap* self) {
void pk_print_stack(VM* self, Frame* frame, Bytecode byte) { void pk_print_stack(VM* self, Frame* frame, Bytecode byte) {
return; return;
if(frame == NULL) return; if(frame == NULL || py_isnil(&self->main)) return;
py_TValue* sp = self->stack.sp; py_TValue* sp = self->stack.sp;
c11_sbuf buf; c11_sbuf buf;

View File

@ -35,6 +35,7 @@ static void Dict__ctor(Dict* self, int capacity) {
self->indices = malloc(self->capacity * sizeof(DictIndex)); self->indices = malloc(self->capacity * sizeof(DictIndex));
memset(self->indices, -1, self->capacity * sizeof(DictIndex)); memset(self->indices, -1, self->capacity * sizeof(DictIndex));
c11_vector__ctor(&self->entries, sizeof(DictEntry)); c11_vector__ctor(&self->entries, sizeof(DictEntry));
c11_vector__reserve(&self->entries, capacity);
} }
static void Dict__dtor(Dict* self) { static void Dict__dtor(Dict* self) {

View File

@ -71,7 +71,10 @@ int py_next(py_Ref val) {
VM* vm = pk_current_vm; VM* vm = pk_current_vm;
vm->is_stopiteration = false; vm->is_stopiteration = false;
py_Ref tmp = py_tpfindmagic(val->type, __next__); py_Ref tmp = py_tpfindmagic(val->type, __next__);
if(!tmp) return TypeError("'%t' object is not an iterator", val->type); if(!tmp) {
TypeError("'%t' object is not an iterator", val->type);
return -1;
}
py_StackRef p0 = py_peek(0); py_StackRef p0 = py_peek(0);
if(py_call(tmp, 1, val)) return true; if(py_call(tmp, 1, val)) return true;
if(vm->curr_exception.type == tp_StopIteration) { if(vm->curr_exception.type == tp_StopIteration) {

View File

@ -1,6 +1,8 @@
a = {1, 2, 3} a = {1, 2, 3}
a |= {2, 3, 4} assert a == a
assert a == {i for i in range(1, 3+1)}
a |= {2, 3, 4}
assert a == {1, 2, 3, 4} assert a == {1, 2, 3, 4}
a = {1, 2, 3} a = {1, 2, 3}
@ -79,10 +81,10 @@ assert {1,2,3}.isdisjoint({4,5,6})
assert not {1,2,3}.isdisjoint({2,3,4}) assert not {1,2,3}.isdisjoint({2,3,4})
# unpacking builder # unpacking builder
a = {1, 2, 3} # a = {1, 2, 3}
b = {*a, 4, 5, *a, *a} # b = {*a, 4, 5, *a, *a}
assert b == {1, 2, 3, 4, 5} # assert b == {1, 2, 3, 4, 5}
a = set() # a = set()
b = {*a, 1, 2, 3, *a, *a} # b = {*a, 1, 2, 3, *a, *a}
assert b == {1, 2, 3} # assert b == {1, 2, 3}