From a6b3163635723c1caaa71f955d52f0b22772dd2f Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 30 Jun 2024 14:27:13 +0800 Subject: [PATCH] some fix --- include/pocketpy/pocketpy.h | 19 +++++++++++++------ src/interpreter/ceval.c | 34 +++++++++++++++++----------------- src/public/error.c | 2 +- src/public/py_ops.c | 2 +- src/public/stack_ops.c | 6 +----- src/public/vm.c | 8 ++++++-- src2/main.c | 4 ++-- 7 files changed, 41 insertions(+), 34 deletions(-) diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index c00c210d..e882cb51 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -109,9 +109,7 @@ bool py_istype(const py_Ref, py_Type); /************* References *************/ #define py_arg(i) (py_Ref)((char*)argv+((i)<<4)) - -py_Ref py_getreg(int i); -void py_setreg(int i, const py_Ref val); +py_Ref py_reg(int i); py_Ref py_getdict(const py_Ref self, py_Name name); void py_setdict(py_Ref self, py_Name name, const py_Ref val); @@ -164,7 +162,7 @@ py_Ref py_getmodule(const char* name); int py_import(const char* name, py_Ref out); /************* Errors *************/ -py_Error* py_getlasterror(); +py_Error* py_lasterror(); void py_Error__print(py_Error*); /************* Operators *************/ @@ -180,8 +178,17 @@ bool py_repr(const py_Ref, py_Ref out); /// The result will be set to `vm->last_retval`. int pk_vectorcall(int argc, int kwargc, bool op_call); -bool py_call(py_Ref f, ...); -bool py_callmethod(py_Ref self, py_Name name, ...); +/// Call a function. +/// It prepares the stack and then performs a `vectorcall(argc, 0, false)`. +/// The result will be set to `vm->last_retval`. +bool py_call(py_Ref f, int argc, py_Ref argv); + +/// Call a method. +/// It prepares the stack and then performs a `vectorcall(argc+1, 0, false)`. +/// The result will be set to `vm->last_retval`. +bool py_callmethod(py_Ref self, py_Name name, int argc, py_Ref argv); +/// The return value of the most recent vectorcall. +py_Ref py_lastretval(); #define py_isnull(self) ((self)->type == 0) diff --git a/src/interpreter/ceval.c b/src/interpreter/ceval.c index 53aeaf0f..53385d62 100644 --- a/src/interpreter/ceval.c +++ b/src/interpreter/ceval.c @@ -251,16 +251,13 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) { if(ti->m__getitem__) { if(!ti->m__getitem__(2, SECOND(), SECOND())) goto __ERROR; } else { - if(!py_callmethod(SECOND(), __getitem__, TOP())) goto __ERROR; - // // [a, b] -> [?, a, b] - // PUSH(TOP()); // [a, b, b] - // *SECOND() = *THIRD(); // [a, a, b] - // bool ok = py_getunboundmethod(SECOND(), __getitem__, false, THIRD(), - // SECOND()); if(!ok) { - // // __getitem__ not found - // goto __ERROR; - // } - // py_vectorcall(2, 0, ); + // [a, b] -> [?, a, b] + PUSH(TOP()); // [a, b, b] + *SECOND() = *THIRD(); // [a, a, b] + bool ok = py_getunboundmethod(SECOND(), __getitem__, false, THIRD(), SECOND()); + // [__getitem__, self, b] + if(!ok) goto __ERROR; + vectorcall_opcall(2); } DISPATCH(); } @@ -387,18 +384,21 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) { // [x] py_Ref f = py_getdict(&self->builtins, pk_id_long); assert(f != NULL); - if(!py_call(f, TOP())) goto __ERROR; + if(!py_call(f, 1, TOP())) goto __ERROR; *TOP() = self->last_retval; DISPATCH(); } case OP_BUILD_IMAG: { - py_Ref _0 = py_getdict(&self->builtins, pk_id_complex); - assert(_0 != NULL); - py_TValue zero; - py_newint(&zero, 0); - if(!py_call(_0, &zero, TOP())) goto __ERROR; - *TOP() = self->last_retval; + // [x] + py_Ref f = py_getdict(&self->builtins, pk_id_complex); + assert(f != NULL); + py_TValue tmp = *TOP(); + *TOP() = *f; // [complex] + py_newnull(SP()++); // [complex, NULL] + py_newint(SP()++, 0); // [complex, NULL, 0] + *SP()++ = tmp; // [complex, NULL, 0, x] + vectorcall_opcall(2); // [complex(x, 0)] DISPATCH(); } case OP_BUILD_BYTES: { diff --git a/src/public/error.c b/src/public/error.c index 70933374..c6a18b92 100644 --- a/src/public/error.c +++ b/src/public/error.c @@ -5,7 +5,7 @@ #include "pocketpy/interpreter/vm.h" -py_Error* py_getlasterror(){ +py_Error* py_lasterror(){ return pk_current_vm->last_error; } diff --git a/src/public/py_ops.c b/src/public/py_ops.c index e7842aaa..b20c4749 100644 --- a/src/public/py_ops.c +++ b/src/public/py_ops.c @@ -12,7 +12,7 @@ bool py_str(const py_Ref val, py_Ref out) { return 0; } bool py_repr(const py_Ref val, py_Ref out) { const pk_TypeInfo* ti = pk_tpinfo(val); if(ti->m__repr__) return ti->m__repr__(1, val, out); - bool ok = py_callmethod(val, __repr__); + bool ok = py_callmethod(val, __repr__, 0, NULL); if(ok) { *out = pk_current_vm->last_retval; return true; diff --git a/src/public/stack_ops.c b/src/public/stack_ops.c index 90a4bc03..389acdf0 100644 --- a/src/public/stack_ops.c +++ b/src/public/stack_ops.c @@ -4,14 +4,10 @@ #include "pocketpy/objects/object.h" #include "pocketpy/interpreter/vm.h" -py_Ref py_getreg(int i){ +py_Ref py_reg(int i){ return pk_current_vm->reg + i; } -void py_setreg(int i, const py_Ref val){ - pk_current_vm->reg[i] = *val; -} - py_Ref py_getdict(const py_Ref self, py_Name name){ assert(self && self->is_ptr); return pk_NameDict__try_get(PyObject__dict(self->_obj), name); diff --git a/src/public/vm.c b/src/public/vm.c index 20d87829..3854f5a2 100644 --- a/src/public/vm.c +++ b/src/public/vm.c @@ -47,11 +47,11 @@ int py_eval(const char* source, py_Ref out) { PK_UNREACHABLE(); } -bool py_call(py_Ref callable, ...){ +bool py_call(py_Ref f, int argc, py_Ref argv){ return -1; } -bool py_callmethod(py_Ref self, py_Name name, ...){ +bool py_callmethod(py_Ref self, py_Name name, int argc, py_Ref argv){ return -1; } @@ -59,6 +59,10 @@ int pk_vectorcall(int argc, int kwargc, bool op_call){ return -1; } +py_Ref py_lastretval(){ + return &pk_current_vm->last_retval; +} + bool py_getunboundmethod(const py_Ref self, py_Name name, bool fallback, py_Ref out, py_Ref out_self){ return -1; } diff --git a/src2/main.c b/src2/main.c index 61328869..900153f0 100644 --- a/src2/main.c +++ b/src2/main.c @@ -27,9 +27,9 @@ int main(int argc, char** argv) { py_initialize(); const char* source = "1, 'a'"; - py_Ref r0 = py_getreg(0); + py_Ref r0 = py_reg(0); if(py_eval(source, r0)){ - py_Error* err = py_getlasterror(); + py_Error* err = py_lasterror(); py_Error__print(err); }else{ // handle the result