From 36b3c9ff8f7447ebd628018a29f7bf1c3a6875cf Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Mon, 1 Jul 2024 01:28:18 +0800 Subject: [PATCH] some fix --- include/pocketpy/common/strname.h | 18 ++--------- include/pocketpy/pocketpy.h | 21 ++++++++++--- src/interpreter/ceval.c | 7 ++--- src/public/py_ops.c | 52 +++++++++---------------------- src/public/vm.c | 7 ++--- src2/main.c | 45 ++++++++++++++++---------- 6 files changed, 66 insertions(+), 84 deletions(-) diff --git a/include/pocketpy/common/strname.h b/include/pocketpy/common/strname.h index 4bc59303..28503789 100644 --- a/include/pocketpy/common/strname.h +++ b/include/pocketpy/common/strname.h @@ -1,6 +1,7 @@ #pragma once #include +#include "pocketpy/pocketpy.h" #include "pocketpy/common/str.h" #ifdef __cplusplus @@ -19,26 +20,11 @@ c11_string pk_StrName__rmap2(uint16_t index); void pk_StrName__initialize(); void pk_StrName__finalize(); -#ifdef __cplusplus -} -#endif - -/* global names */ -#ifdef __cplusplus -extern "C" { -namespace pkpy { -#endif - -#define MAGIC_METHOD(x) extern uint16_t x; -#include "pocketpy/xmacros/magics.h" -#undef MAGIC_METHOD - extern uint16_t pk_id_add; extern uint16_t pk_id_set; extern uint16_t pk_id_long; extern uint16_t pk_id_complex; #ifdef __cplusplus -} // namespace pkpy -} // extern "C" +} #endif diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index 8cc17c35..262cc3a8 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -37,8 +37,9 @@ void py_finalize(); /// Run a simple source string. Do not change the stack. int py_exec(const char*); -/// Eval a simple expression. The result is pushed to the stack. -int py_eval(const char*, py_Ref out); +/// Eval a simple expression. +/// The result will be set to `vm->last_retval`. +int py_eval(const char*); /************* Values Creation *************/ void py_newint(py_Ref, int64_t); @@ -115,7 +116,12 @@ py_Ref py_tpmagic(py_Type type, py_Name name); // new style decl-based bindings py_Ref py_bind(py_Ref obj, const char* sig, py_CFunction f); -py_Ref py_bind2(py_Ref obj, const char* sig, py_CFunction f, BindType bt, const char* docstring, const py_Ref upvalue); +py_Ref py_bind2(py_Ref obj, + const char* sig, + py_CFunction f, + BindType bt, + const char* docstring, + const py_Ref upvalue); // old style argc-based bindings 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); @@ -149,6 +155,9 @@ bool py_getitem(const py_Ref self, const py_Ref key, py_Ref out); bool py_setitem(py_Ref self, const py_Ref key, const py_Ref val); bool py_delitem(py_Ref self, const py_Ref key); +/// Perform a binary operation on the stack. +/// It assumes `lhs` and `rhs` are already pushed to the stack. +/// The result will be set to `vm->last_retval`. bool py_binaryop(const py_Ref lhs, const py_Ref rhs, py_Name op, py_Name rop); #define py_binaryadd(lhs, rhs) py_binaryop(lhs, rhs, __add__, __radd__) @@ -196,7 +205,7 @@ py_Ref py_newmodule(const char* name, const char* package); py_Ref py_getmodule(const char* name); /// Import a module. -int py_import(const char* name, py_Ref out); +bool py_import(const char* name, py_Ref out); /************* Errors *************/ py_Error* py_lasterror(); @@ -274,6 +283,10 @@ py_Ref py_tpfindmagic(py_Type, py_Name name); /// @lifespan: Permanent. py_Ref py_tpobject(py_Type type); +#define MAGIC_METHOD(x) extern uint16_t x; +#include "pocketpy/xmacros/magics.h" +#undef MAGIC_METHOD + #ifdef __cplusplus } #endif diff --git a/src/interpreter/ceval.c b/src/interpreter/ceval.c index 8fc9d611..8465aad8 100644 --- a/src/interpreter/ceval.c +++ b/src/interpreter/ceval.c @@ -8,8 +8,8 @@ int UnboundLocalError(py_Name name) { return -1; } int NameError(py_Name name) { return -1; } -#define AttributeError(obj, name) -#define BinaryOptError(op) +#define AttributeError(obj, name) false +#define BinaryOptError(op) false #define DISPATCH() \ do { \ @@ -724,6 +724,5 @@ bool py_binaryop(const py_Ref lhs, const py_Ref rhs, py_Name op, py_Name rop) { self->last_retval = (op == __eq__) ? self->False : self->True; return true; } - BinaryOptError(byte.arg); - return false; + return BinaryOptError(byte.arg); } diff --git a/src/public/py_ops.c b/src/public/py_ops.c index bdde6979..7060c2be 100644 --- a/src/public/py_ops.c +++ b/src/public/py_ops.c @@ -1,10 +1,8 @@ #include "pocketpy/interpreter/vm.h" #include "pocketpy/pocketpy.h" -bool py_isidentical(const py_Ref lhs, const py_Ref rhs){ - if(lhs->is_ptr && rhs->is_ptr){ - return lhs->_obj == rhs->_obj; - } +bool py_isidentical(const py_Ref lhs, const py_Ref rhs) { + if(lhs->is_ptr && rhs->is_ptr) { return lhs->_obj == rhs->_obj; } return false; } @@ -24,38 +22,16 @@ bool py_setitem(py_Ref self, const py_Ref key, const py_Ref val) { return -1; } bool py_delitem(py_Ref self, const py_Ref key) { return -1; } -int py_eq(const py_Ref lhs, const py_Ref rhs) { - bool ok = py_binaryop(lhs, rhs, __eq__, __eq__); - if(!ok) return -1; - return py_tobool(py_lastretval()); -} +#define COMPARE_OP_IMPL(name, op, rop) \ + int py_##name(const py_Ref lhs, const py_Ref rhs) { \ + bool ok = py_binaryop(lhs, rhs, op, rop); \ + if(!ok) return -1; \ + return py_tobool(py_lastretval()); \ + } -int py_ne(const py_Ref lhs, const py_Ref rhs) { - bool ok = py_binaryop(lhs, rhs, __ne__, __ne__); - if(!ok) return -1; - return py_tobool(py_lastretval()); -} - -int py_lt(const py_Ref lhs, const py_Ref rhs) { - bool ok = py_binaryop(lhs, rhs, __lt__, __gt__); - if(!ok) return -1; - return py_tobool(py_lastretval()); -} - -int py_gt(const py_Ref lhs, const py_Ref rhs) { - bool ok = py_binaryop(lhs, rhs, __gt__, __lt__); - if(!ok) return -1; - return py_tobool(py_lastretval()); -} - -int py_ge(const py_Ref lhs, const py_Ref rhs) { - bool ok = py_binaryop(lhs, rhs, __ge__, __le__); - if(!ok) return -1; - return py_tobool(py_lastretval()); -} - -int py_le(const py_Ref lhs, const py_Ref rhs) { - bool ok = py_binaryop(lhs, rhs, __le__, __ge__); - if(!ok) return -1; - return py_tobool(py_lastretval()); -} +COMPARE_OP_IMPL(eq, __eq__, __eq__) +COMPARE_OP_IMPL(ne, __ne__, __ne__) +COMPARE_OP_IMPL(lt, __lt__, __gt__) +COMPARE_OP_IMPL(le, __le__, __ge__) +COMPARE_OP_IMPL(gt, __gt__, __lt__) +COMPARE_OP_IMPL(ge, __ge__, __le__) \ No newline at end of file diff --git a/src/public/vm.c b/src/public/vm.c index a310e8a4..ab7929db 100644 --- a/src/public/vm.c +++ b/src/public/vm.c @@ -25,7 +25,7 @@ void py_finalize() { 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; pk_SourceData_ src = pk_SourceData__rcnew(source, "main.py", EVAL_MODE, false); Error* err = pk_compile(src, &co); @@ -40,10 +40,7 @@ int py_eval(const char* source, py_Ref out) { CodeObject__dtor(&co); PK_DECREF(src); if(res == RES_ERROR) return vm->last_error->type; - if(res == RES_RETURN) { - *out = vm->last_retval; - return 0; - } + if(res == RES_RETURN) return 0; PK_UNREACHABLE(); } diff --git a/src2/main.c b/src2/main.c index 8d064ef8..c976f242 100644 --- a/src2/main.c +++ b/src2/main.c @@ -1,5 +1,6 @@ #include #include +#include #include "pocketpy.h" @@ -27,32 +28,42 @@ int main(int argc, char** argv) { py_initialize(); const char* source = "1 < 2"; - py_Ref r0 = py_reg(0); - if(py_eval(source, r0)){ + if(py_eval(source)) { py_Error* err = py_lasterror(); py_Error__print(err); - }else{ + } else { // handle the result - bool _L0 = py_tobool(r0); + bool _L0 = py_tobool(py_lastretval()); printf("%d\n", _L0); } + py_Ref r0 = py_reg(0); + py_Ref r1 = py_reg(1); + + py_newint(r0, 1); + py_newfloat(r1, 2.5); + + bool ok = py_binaryadd(r0, r1); + assert(ok); + double res = py_tofloat(py_lastretval()); + printf("%f\n", res); + py_finalize(); return 0; -// if(argc != 2) goto __HELP; -// char* source = read_file(argv[1]); -// py_initialize(); + // if(argc != 2) goto __HELP; + // char* source = read_file(argv[1]); + // py_initialize(); -// if(py_exec(source)){ -// py_Error* err = py_getlasterror(); -// py_Error__print(err); -// } - -// py_finalize(); -// free(source); + // if(py_exec(source)){ + // py_Error* err = py_getlasterror(); + // py_Error__print(err); + // } -// __HELP: -// printf("Usage: pocketpy [filename]\n"); -// return 0; + // py_finalize(); + // free(source); + + // __HELP: + // printf("Usage: pocketpy [filename]\n"); + // return 0; }