blueloveTH 1 an în urmă
părinte
comite
a6b3163635
7 a modificat fișierele cu 41 adăugiri și 34 ștergeri
  1. 13 6
      include/pocketpy/pocketpy.h
  2. 17 17
      src/interpreter/ceval.c
  3. 1 1
      src/public/error.c
  4. 1 1
      src/public/py_ops.c
  5. 1 5
      src/public/stack_ops.c
  6. 6 2
      src/public/vm.c
  7. 2 2
      src2/main.c

+ 13 - 6
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)
 

+ 17 - 17
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: {

+ 1 - 1
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;
 }
 

+ 1 - 1
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;

+ 1 - 5
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);

+ 6 - 2
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;
 }

+ 2 - 2
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