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;
// last error
py_Error* last_error;
// last retval
PyVar last_retval;
// registers
PyVar reg[8];
PyObject* __curr_class;
PyObject* __cached_object_new;

View File

@ -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);

View File

@ -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;

View File

@ -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){

View File

@ -24,17 +24,21 @@ void py_finalize(){
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();
}