This commit is contained in:
blueloveTH 2024-06-26 02:50:02 +08:00
parent 2ac2ff807f
commit fb0ec57f38
5 changed files with 24 additions and 31 deletions

View File

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

View File

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

View File

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

View File

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

View File

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