blueloveTH vor 1 Jahr
Ursprung
Commit
fb0ec57f38
5 geänderte Dateien mit 24 neuen und 31 gelöschten Zeilen
  1. 0 4
      include/pocketpy/interpreter/vm.h
  2. 2 2
      include/pocketpy/pocketpy.h
  3. 0 1
      src/interpreter/vm.c
  4. 5 11
      src/public/stackops.c
  5. 17 13
      src/public/vm.c

+ 0 - 4
include/pocketpy/interpreter/vm.h

@@ -60,10 +60,6 @@ typedef struct pk_VM {
     PyVar True, False, None, NotImplemented, Ellipsis;
     // last error
     py_Error* last_error;
-    // last retval
-    PyVar last_retval;
-    // registers
-    PyVar reg[8];
 
     PyObject* __curr_class;
     PyObject* __cached_object_new;

+ 2 - 2
include/pocketpy/pocketpy.h

@@ -30,7 +30,9 @@ extern pk_VM* pk_current_vm;
 void py_initialize();
 void py_finalize();
 
+/// Run a simple source string. Do not change the stack.
 int py_exec(const char*);
+/// Eval a simple expression. If succeed, the result will be pushed onto the stack.
 int py_eval(const char*);
 
 /************* Values Creation *************/
@@ -101,8 +103,6 @@ void py_setsecond(const py_Ref);
 /// Returns a reference to the i-th object from the top of the stack.
 /// i should be negative, e.g. (-1) means TOS.
 py_Ref py_peek(int i);
-/// Prepares a push and returns an uninitialized reference.
-py_Ref py_push();
 /// Pops an object from the stack.
 void py_pop();
 void py_shrink(int n);

+ 0 - 1
src/interpreter/vm.c

@@ -50,7 +50,6 @@ void pk_VM__ctor(pk_VM* self){
     self->_stderr = pk_default_stderr;
 
     self->last_error = NULL;
-    self->last_retval = PY_NULL;
 
     self->__curr_class = NULL;
     self->__cached_object_new = NULL;

+ 5 - 11
src/public/stackops.c

@@ -52,13 +52,6 @@ py_Ref py_peek(int i){
     return pk_current_vm->stack.sp + i;
 }
 
-py_Ref py_push(){
-    pk_VM* vm = pk_current_vm;
-    py_Ref top = vm->stack.sp;
-    vm->stack.sp++;
-    return top;
-}
-
 void py_pop(){
     pk_VM* vm = pk_current_vm;
     vm->stack.sp--;
@@ -70,13 +63,14 @@ void py_shrink(int n){
 }
 
 void py_pushref(const py_Ref src){
-    *py_push() = *src;
+    pk_VM* vm = pk_current_vm;
+    *vm->stack.sp++ = *src;
 }
 
 py_Ref py_pushtmp(){
-    py_Ref r = py_push();
-    py_newnull(r);
-    return r;
+    pk_VM* vm = pk_current_vm;
+    py_newnull(vm->stack.sp++);
+    return py_gettop();
 }
 
 void py_poptmp(int n){

+ 17 - 13
src/public/vm.c

@@ -7,34 +7,38 @@
 pk_VM* pk_current_vm;
 static pk_VM pk_default_vm;
 
-void py_initialize(){
+void py_initialize() {
     Pools_initialize();
     pk_StrName__initialize();
     pk_current_vm = &pk_default_vm;
     pk_VM__ctor(&pk_default_vm);
 }
 
-void py_finalize(){
+void py_finalize() {
     pk_VM__dtor(&pk_default_vm);
     pk_current_vm = NULL;
     pk_StrName__finalize();
     Pools_finalize();
 }
 
-int py_exec(const char* source){
+int py_exec(const char* source) {
     CodeObject* co = NULL;
     pk_VM* vm = pk_current_vm;
-    Frame* frame = Frame__new(
-        co,
-        &vm->main,
-        NULL,
-        vm->stack.sp,
-        vm->stack.sp,
-        co
-    );
+    Frame* frame = Frame__new(co, &vm->main, NULL, vm->stack.sp, vm->stack.sp, co);
     pk_VM__push_frame(vm, frame);
     pk_FrameResult res = pk_VM__run_top_frame(vm);
     if(res == RES_ERROR) return vm->last_error->type;
-    if(res == RES_RETURN) return 0; // vm->last_retval;
-    assert(0);  // unreachable
+    if(res == RES_RETURN) return 0;
+    PK_UNREACHABLE();
 }
+
+int py_eval(const char* source) {
+    CodeObject* co = NULL;
+    pk_VM* vm = pk_current_vm;
+    Frame* frame = Frame__new(co, &vm->main, NULL, vm->stack.sp, vm->stack.sp, co);
+    pk_VM__push_frame(vm, frame);
+    pk_FrameResult res = pk_VM__run_top_frame(vm);
+    if(res == RES_ERROR) return vm->last_error->type;
+    if(res == RES_RETURN) return 0;
+    PK_UNREACHABLE();
+}