This commit is contained in:
blueloveTH 2024-06-29 19:44:38 +08:00
parent 6c46705e98
commit f53ae5e459
5 changed files with 11 additions and 10 deletions

View File

@ -30,8 +30,8 @@ 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*); int py_exec(const char*);
/// Eval a simple expression. /// Eval a simple expression. The result is pushed to the stack.
int py_eval(const char*, py_Ref out); int py_eval(const char*);
/************* Values Creation *************/ /************* Values Creation *************/
void py_newint(py_Ref, int64_t); void py_newint(py_Ref, int64_t);
@ -156,8 +156,8 @@ int py_eq(const py_Ref, const py_Ref);
int py_le(const py_Ref, const py_Ref); int py_le(const py_Ref, const py_Ref);
int py_hash(const py_Ref, int64_t* out); int py_hash(const py_Ref, int64_t* out);
int py_str(const py_Ref, py_Ref out); int py_str(const py_Ref);
int py_repr(const py_Ref, py_Ref out); int py_repr(const py_Ref);
int py_vectorcall(int argc, int kwargc); int py_vectorcall(int argc, int kwargc);
int py_call(py_Ref f, ...); int py_call(py_Ref f, ...);

View File

@ -87,9 +87,10 @@ pk_FrameResult pk_VM__run_top_frame(pk_VM* self) {
} }
case OP_PRINT_EXPR: case OP_PRINT_EXPR:
if(TOP().type != tp_none_type) { if(TOP().type != tp_none_type) {
int err = py_repr(&TOP(), &TOP()); int err = py_repr(&TOP());
if(err) goto __ERROR; if(err) goto __ERROR;
self->_stdout("%s\n", py_tostr(&TOP())); self->_stdout("%s\n", py_tostr(&TOP()));
POP();
} }
POP(); POP();
DISPATCH(); DISPATCH();

View File

@ -7,9 +7,9 @@ int py_le(const py_Ref lhs, const py_Ref rhs) { return 0; }
int py_hash(const py_Ref val, int64_t* out) { return 0; } int py_hash(const py_Ref val, int64_t* out) { return 0; }
int py_str(const py_Ref val, py_Ref out) { return 0; } int py_str(const py_Ref val) { return 0; }
int py_repr(const py_Ref val, py_Ref out) { int py_repr(const py_Ref val) {
const pk_TypeInfo* ti = c11__at(pk_TypeInfo, &pk_current_vm->types, val->type); const pk_TypeInfo* ti = c11__at(pk_TypeInfo, &pk_current_vm->types, val->type);
if(ti->m__repr__) return ti->m__repr__(1, val); if(ti->m__repr__) return ti->m__repr__(1, val);
return py_callmethod(val, __repr__); return py_callmethod(val, __repr__);

View File

@ -25,7 +25,7 @@ void py_finalize() {
int py_exec(const char* source) { PK_UNREACHABLE(); } int py_exec(const char* source) { PK_UNREACHABLE(); }
int py_eval(const char* source, py_Ref out) { int 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);

View File

@ -27,7 +27,7 @@ int main(int argc, char** argv) {
py_initialize(); py_initialize();
const char* source = "[1, 'a']"; const char* source = "[1, 'a']";
if(py_eval(source, py_pushtmp())){ if(py_eval(source)){
py_Error* err = py_getlasterror(); py_Error* err = py_getlasterror();
py_Error__print(err); py_Error__print(err);
}else{ }else{
@ -37,7 +37,7 @@ int main(int argc, char** argv) {
int _L0 = py_toint(_0); int _L0 = py_toint(_0);
const char* _L1 = py_tostr(_1); const char* _L1 = py_tostr(_1);
printf("%d, %s\n", _L0, _L1); printf("%d, %s\n", _L0, _L1);
py_poptmp(1); py_pop();
} }
py_finalize(); py_finalize();