This commit is contained in:
blueloveTH 2024-06-30 14:27:13 +08:00
parent 4860c08e03
commit a6b3163635
7 changed files with 41 additions and 34 deletions

View File

@ -109,9 +109,7 @@ bool py_istype(const py_Ref, py_Type);
/************* References *************/ /************* References *************/
#define py_arg(i) (py_Ref)((char*)argv+((i)<<4)) #define py_arg(i) (py_Ref)((char*)argv+((i)<<4))
py_Ref py_reg(int i);
py_Ref py_getreg(int i);
void py_setreg(int i, const py_Ref val);
py_Ref py_getdict(const py_Ref self, py_Name name); py_Ref py_getdict(const py_Ref self, py_Name name);
void py_setdict(py_Ref self, py_Name name, const py_Ref val); 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); int py_import(const char* name, py_Ref out);
/************* Errors *************/ /************* Errors *************/
py_Error* py_getlasterror(); py_Error* py_lasterror();
void py_Error__print(py_Error*); void py_Error__print(py_Error*);
/************* Operators *************/ /************* Operators *************/
@ -180,8 +178,17 @@ bool py_repr(const py_Ref, py_Ref out);
/// The result will be set to `vm->last_retval`. /// The result will be set to `vm->last_retval`.
int pk_vectorcall(int argc, int kwargc, bool op_call); int pk_vectorcall(int argc, int kwargc, bool op_call);
bool py_call(py_Ref f, ...); /// Call a function.
bool py_callmethod(py_Ref self, py_Name name, ...); /// 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) #define py_isnull(self) ((self)->type == 0)

View File

@ -251,16 +251,13 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
if(ti->m__getitem__) { if(ti->m__getitem__) {
if(!ti->m__getitem__(2, SECOND(), SECOND())) goto __ERROR; if(!ti->m__getitem__(2, SECOND(), SECOND())) goto __ERROR;
} else { } else {
if(!py_callmethod(SECOND(), __getitem__, TOP())) goto __ERROR; // [a, b] -> [?, a, b]
// // [a, b] -> [?, a, b] PUSH(TOP()); // [a, b, b]
// PUSH(TOP()); // [a, b, b] *SECOND() = *THIRD(); // [a, a, b]
// *SECOND() = *THIRD(); // [a, a, b] bool ok = py_getunboundmethod(SECOND(), __getitem__, false, THIRD(), SECOND());
// bool ok = py_getunboundmethod(SECOND(), __getitem__, false, THIRD(), // [__getitem__, self, b]
// SECOND()); if(!ok) { if(!ok) goto __ERROR;
// // __getitem__ not found vectorcall_opcall(2);
// goto __ERROR;
// }
// py_vectorcall(2, 0, );
} }
DISPATCH(); DISPATCH();
} }
@ -387,18 +384,21 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
// [x] // [x]
py_Ref f = py_getdict(&self->builtins, pk_id_long); py_Ref f = py_getdict(&self->builtins, pk_id_long);
assert(f != NULL); assert(f != NULL);
if(!py_call(f, TOP())) goto __ERROR; if(!py_call(f, 1, TOP())) goto __ERROR;
*TOP() = self->last_retval; *TOP() = self->last_retval;
DISPATCH(); DISPATCH();
} }
case OP_BUILD_IMAG: { case OP_BUILD_IMAG: {
py_Ref _0 = py_getdict(&self->builtins, pk_id_complex); // [x]
assert(_0 != NULL); py_Ref f = py_getdict(&self->builtins, pk_id_complex);
py_TValue zero; assert(f != NULL);
py_newint(&zero, 0); py_TValue tmp = *TOP();
if(!py_call(_0, &zero, TOP())) goto __ERROR; *TOP() = *f; // [complex]
*TOP() = self->last_retval; 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(); DISPATCH();
} }
case OP_BUILD_BYTES: { case OP_BUILD_BYTES: {

View File

@ -5,7 +5,7 @@
#include "pocketpy/interpreter/vm.h" #include "pocketpy/interpreter/vm.h"
py_Error* py_getlasterror(){ py_Error* py_lasterror(){
return pk_current_vm->last_error; return pk_current_vm->last_error;
} }

View File

@ -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) { bool py_repr(const py_Ref val, py_Ref out) {
const pk_TypeInfo* ti = pk_tpinfo(val); const pk_TypeInfo* ti = pk_tpinfo(val);
if(ti->m__repr__) return ti->m__repr__(1, val, out); 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) { if(ok) {
*out = pk_current_vm->last_retval; *out = pk_current_vm->last_retval;
return true; return true;

View File

@ -4,14 +4,10 @@
#include "pocketpy/objects/object.h" #include "pocketpy/objects/object.h"
#include "pocketpy/interpreter/vm.h" #include "pocketpy/interpreter/vm.h"
py_Ref py_getreg(int i){ py_Ref py_reg(int i){
return pk_current_vm->reg + 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){ py_Ref py_getdict(const py_Ref self, py_Name name){
assert(self && self->is_ptr); assert(self && self->is_ptr);
return pk_NameDict__try_get(PyObject__dict(self->_obj), name); return pk_NameDict__try_get(PyObject__dict(self->_obj), name);

View File

@ -47,11 +47,11 @@ int py_eval(const char* source, py_Ref out) {
PK_UNREACHABLE(); PK_UNREACHABLE();
} }
bool py_call(py_Ref callable, ...){ bool py_call(py_Ref f, int argc, py_Ref argv){
return -1; 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; return -1;
} }
@ -59,6 +59,10 @@ int pk_vectorcall(int argc, int kwargc, bool op_call){
return -1; 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){ bool py_getunboundmethod(const py_Ref self, py_Name name, bool fallback, py_Ref out, py_Ref out_self){
return -1; return -1;
} }

View File

@ -27,9 +27,9 @@ int main(int argc, char** argv) {
py_initialize(); py_initialize();
const char* source = "1, 'a'"; const char* source = "1, 'a'";
py_Ref r0 = py_getreg(0); py_Ref r0 = py_reg(0);
if(py_eval(source, r0)){ if(py_eval(source, r0)){
py_Error* err = py_getlasterror(); py_Error* err = py_lasterror();
py_Error__print(err); py_Error__print(err);
}else{ }else{
// handle the result // handle the result