This commit is contained in:
blueloveTH 2024-07-01 02:15:16 +08:00
parent 97cdfbedaf
commit 96d0c432c7
6 changed files with 45 additions and 25 deletions

View File

@ -34,10 +34,10 @@ void py_initialize();
void py_finalize(); void py_finalize();
/// Run a simple source string. Do not change the stack. /// Run a simple source string. Do not change the stack.
int py_exec(const char*); bool py_exec(const char*);
/// Eval a simple expression. /// Eval a simple expression.
/// The result will be set to `vm->last_retval`. /// The result will be set to `vm->last_retval`.
int py_eval(const char*); bool py_eval(const char*);
/************* Values Creation *************/ /************* Values Creation *************/
void py_newint(py_Ref, int64_t); void py_newint(py_Ref, int64_t);
@ -90,7 +90,7 @@ double py_tofloat(const py_Ref);
bool py_castfloat(const py_Ref, double* out); bool py_castfloat(const py_Ref, double* out);
bool py_tobool(const py_Ref); bool py_tobool(const py_Ref);
const char* py_tostr(const py_Ref); const char* py_tostr(const py_Ref);
const char* py_tostrn(const py_Ref, int* out); const char* py_tostrn(const py_Ref, int* size);
void* py_touserdata(const py_Ref); void* py_touserdata(const py_Ref);
@ -100,8 +100,8 @@ void* py_touserdata(const py_Ref);
#define py_isstr(self) py_istype(self, tp_str) #define py_isstr(self) py_istype(self, tp_str)
bool py_istype(const py_Ref, py_Type); bool py_istype(const py_Ref, py_Type);
// bool py_isinstance(const py_Ref obj, py_Type type); bool py_isinstance(const py_Ref obj, py_Type type);
// bool py_issubclass(py_Type derived, py_Type base); bool py_issubclass(py_Type derived, py_Type base);
/************* References *************/ /************* References *************/
#define TypeError(x) false #define TypeError(x) false
@ -125,11 +125,20 @@ void py_bindmethod(py_Type type, const char* name, py_CFunction f);
void py_bindmethod2(py_Type type, const char* name, py_CFunction f, BindType bt); void py_bindmethod2(py_Type type, const char* name, py_CFunction f, BindType bt);
void py_bindnativefunc(py_Ref obj, const char* name, py_CFunction f); void py_bindnativefunc(py_Ref obj, const char* name, py_CFunction f);
/// Get the reference to the i-th register.
/// @lifespan: Permanent.
py_Ref py_reg(int i); py_Ref py_reg(int i);
/// Get the reference of the object's `__dict__`.
/// The object must have a `__dict__`.
/// Returns a reference to the value or NULL if not found.
/// @lifespan: Object.
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);
/// Get the reference of the i-th slot of the object.
/// The object must have slots and `i` must be in range.
/// @lifespan: Object.
py_Ref py_getslot(const py_Ref self, int i); py_Ref py_getslot(const py_Ref self, int i);
void py_setslot(py_Ref self, int i, const py_Ref val); void py_setslot(py_Ref self, int i, const py_Ref val);
@ -209,7 +218,10 @@ py_Error* py_lasterror();
void py_Error__print(py_Error*); void py_Error__print(py_Error*);
/************* Operators *************/ /************* Operators *************/
bool py_bool(const py_Ref); /// Equivalent to `bool(val)`.
/// Returns 1 if `val` is truthy, otherwise 0.
/// Returns -1 if an error occurred.
int py_bool(const py_Ref val);
int py_eq(const py_Ref, const py_Ref); int py_eq(const py_Ref, const py_Ref);
int py_ne(const py_Ref, const py_Ref); int py_ne(const py_Ref, const py_Ref);

View File

@ -534,33 +534,43 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
/*****************************************/ /*****************************************/
case OP_JUMP_FORWARD: DISPATCH_JUMP((int16_t)byte.arg); case OP_JUMP_FORWARD: DISPATCH_JUMP((int16_t)byte.arg);
case OP_POP_JUMP_IF_FALSE: { case OP_POP_JUMP_IF_FALSE: {
bool res = py_bool(TOP()); int res = py_bool(TOP());
if(res < 0) goto __ERROR;
POP(); POP();
if(!res) DISPATCH_JUMP((int16_t)byte.arg); if(!res) DISPATCH_JUMP((int16_t)byte.arg);
DISPATCH(); DISPATCH();
} }
case OP_POP_JUMP_IF_TRUE: { case OP_POP_JUMP_IF_TRUE: {
bool res = py_bool(TOP()); int res = py_bool(TOP());
if(res < 0) goto __ERROR;
POP(); POP();
if(res) DISPATCH_JUMP((int16_t)byte.arg); if(res) DISPATCH_JUMP((int16_t)byte.arg);
DISPATCH(); DISPATCH();
} }
case OP_JUMP_IF_TRUE_OR_POP: case OP_JUMP_IF_TRUE_OR_POP: {
if(py_bool(TOP())) { int res = py_bool(TOP());
if(res < 0) goto __ERROR;
if(res) {
DISPATCH_JUMP((int16_t)byte.arg); DISPATCH_JUMP((int16_t)byte.arg);
} else { } else {
POP(); POP();
DISPATCH(); DISPATCH();
} }
case OP_JUMP_IF_FALSE_OR_POP: }
if(!py_bool(TOP())) { case OP_JUMP_IF_FALSE_OR_POP: {
int res = py_bool(TOP());
if(res < 0) goto __ERROR;
if(!res) {
DISPATCH_JUMP((int16_t)byte.arg); DISPATCH_JUMP((int16_t)byte.arg);
} else { } else {
POP(); POP();
DISPATCH(); DISPATCH();
} }
case OP_SHORTCUT_IF_FALSE_OR_POP: }
if(!py_bool(TOP())) { // [b, False] case OP_SHORTCUT_IF_FALSE_OR_POP: {
int res = py_bool(TOP());
if(res < 0) goto __ERROR;
if(!res) { // [b, False]
STACK_SHRINK(2); // [] STACK_SHRINK(2); // []
PUSH(&self->False); // [False] PUSH(&self->False); // [False]
DISPATCH_JUMP((int16_t)byte.arg); DISPATCH_JUMP((int16_t)byte.arg);
@ -568,6 +578,7 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
POP(); // [b] POP(); // [b]
DISPATCH(); DISPATCH();
} }
}
case OP_LOOP_CONTINUE: case OP_LOOP_CONTINUE:
// just an alias of OP_JUMP_FORWARD // just an alias of OP_JUMP_FORWARD
DISPATCH_JUMP((int16_t)byte.arg); DISPATCH_JUMP((int16_t)byte.arg);

View File

@ -38,10 +38,10 @@ const char* py_tostr(const py_Ref self){
return py_Str__data(ud); return py_Str__data(ud);
} }
const char* py_tostrn(const py_Ref self, int* out){ const char* py_tostrn(const py_Ref self, int* size){
assert(self->type == tp_str); assert(self->type == tp_str);
py_Str* ud = PyObject__value(self->_obj); py_Str* ud = PyObject__value(self->_obj);
*out = ud->size; *size = ud->size;
return py_Str__data(ud); return py_Str__data(ud);
} }

View File

@ -6,7 +6,7 @@ bool py_isidentical(const py_Ref lhs, const py_Ref rhs) {
return false; return false;
} }
bool py_bool(const py_Ref val) { return 0; } int py_bool(const py_Ref val) { return 1; }
bool py_hash(const py_Ref val, int64_t* out) { return 0; } bool py_hash(const py_Ref val, int64_t* out) { return 0; }

View File

@ -23,15 +23,15 @@ void py_finalize() {
pk_MemoryPools__finalize(); pk_MemoryPools__finalize();
} }
int py_exec(const char* source) { PK_UNREACHABLE(); } bool py_exec(const char* source) { PK_UNREACHABLE(); }
int py_eval(const char* source) { bool py_eval(const char* source) {
CodeObject co; CodeObject co;
pk_SourceData_ src = pk_SourceData__rcnew(source, "main.py", EVAL_MODE, false); pk_SourceData_ src = pk_SourceData__rcnew(source, "main.py", EVAL_MODE, false);
Error* err = pk_compile(src, &co); Error* err = pk_compile(src, &co);
if(err) { if(err) {
PK_DECREF(src); PK_DECREF(src);
return -1; return false;
} }
pk_VM* vm = pk_current_vm; 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);
@ -39,8 +39,8 @@ int py_eval(const char* source) {
pk_FrameResult res = pk_VM__run_top_frame(vm); pk_FrameResult res = pk_VM__run_top_frame(vm);
CodeObject__dtor(&co); CodeObject__dtor(&co);
PK_DECREF(src); PK_DECREF(src);
if(res == RES_ERROR) return vm->last_error->type; if(res == RES_ERROR) return false;
if(res == RES_RETURN) return 0; if(res == RES_RETURN) return true;
PK_UNREACHABLE(); PK_UNREACHABLE();
} }

View File

@ -29,9 +29,6 @@ int main(int argc, char** argv) {
const char* source = "1 < 2"; const char* source = "1 < 2";
if(py_eval(source)) { if(py_eval(source)) {
py_Error* err = py_lasterror();
py_Error__print(err);
} else {
// handle the result // handle the result
bool _L0 = py_tobool(py_lastretval()); bool _L0 = py_tobool(py_lastretval());
printf("%d\n", _L0); printf("%d\n", _L0);