This commit is contained in:
blueloveTH 2024-07-01 01:28:18 +08:00
parent 5847586121
commit 36b3c9ff8f
6 changed files with 66 additions and 84 deletions

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
#include "pocketpy/pocketpy.h"
#include "pocketpy/common/str.h" #include "pocketpy/common/str.h"
#ifdef __cplusplus #ifdef __cplusplus
@ -19,26 +20,11 @@ c11_string pk_StrName__rmap2(uint16_t index);
void pk_StrName__initialize(); void pk_StrName__initialize();
void pk_StrName__finalize(); 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_add;
extern uint16_t pk_id_set; extern uint16_t pk_id_set;
extern uint16_t pk_id_long; extern uint16_t pk_id_long;
extern uint16_t pk_id_complex; extern uint16_t pk_id_complex;
#ifdef __cplusplus #ifdef __cplusplus
} // namespace pkpy }
} // extern "C"
#endif #endif

View File

@ -37,8 +37,9 @@ 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. The result is pushed to the stack. /// Eval a simple expression.
int py_eval(const char*, py_Ref out); /// The result will be set to `vm->last_retval`.
int py_eval(const char*);
/************* Values Creation *************/ /************* Values Creation *************/
void py_newint(py_Ref, int64_t); 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 // new style decl-based bindings
py_Ref py_bind(py_Ref obj, const char* sig, py_CFunction f); 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 // old style argc-based bindings
void py_bindmethod(py_Type type, const char* name, py_CFunction f); 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);
@ -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_setitem(py_Ref self, const py_Ref key, const py_Ref val);
bool py_delitem(py_Ref self, const py_Ref key); 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); 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__) #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); py_Ref py_getmodule(const char* name);
/// Import a module. /// Import a module.
int py_import(const char* name, py_Ref out); bool py_import(const char* name, py_Ref out);
/************* Errors *************/ /************* Errors *************/
py_Error* py_lasterror(); py_Error* py_lasterror();
@ -274,6 +283,10 @@ py_Ref py_tpfindmagic(py_Type, py_Name name);
/// @lifespan: Permanent. /// @lifespan: Permanent.
py_Ref py_tpobject(py_Type type); 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 #ifdef __cplusplus
} }
#endif #endif

View File

@ -8,8 +8,8 @@ int UnboundLocalError(py_Name name) { return -1; }
int NameError(py_Name name) { return -1; } int NameError(py_Name name) { return -1; }
#define AttributeError(obj, name) #define AttributeError(obj, name) false
#define BinaryOptError(op) #define BinaryOptError(op) false
#define DISPATCH() \ #define DISPATCH() \
do { \ 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; self->last_retval = (op == __eq__) ? self->False : self->True;
return true; return true;
} }
BinaryOptError(byte.arg); return BinaryOptError(byte.arg);
return false;
} }

View File

@ -1,10 +1,8 @@
#include "pocketpy/interpreter/vm.h" #include "pocketpy/interpreter/vm.h"
#include "pocketpy/pocketpy.h" #include "pocketpy/pocketpy.h"
bool py_isidentical(const py_Ref lhs, const py_Ref rhs){ bool py_isidentical(const py_Ref lhs, const py_Ref rhs) {
if(lhs->is_ptr && rhs->is_ptr){ if(lhs->is_ptr && rhs->is_ptr) { return lhs->_obj == rhs->_obj; }
return lhs->_obj == rhs->_obj;
}
return false; 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; } bool py_delitem(py_Ref self, const py_Ref key) { return -1; }
int py_eq(const py_Ref lhs, const py_Ref rhs) { #define COMPARE_OP_IMPL(name, op, rop) \
bool ok = py_binaryop(lhs, rhs, __eq__, __eq__); int py_##name(const py_Ref lhs, const py_Ref rhs) { \
if(!ok) return -1; bool ok = py_binaryop(lhs, rhs, op, rop); \
return py_tobool(py_lastretval()); if(!ok) return -1; \
} return py_tobool(py_lastretval()); \
}
int py_ne(const py_Ref lhs, const py_Ref rhs) { COMPARE_OP_IMPL(eq, __eq__, __eq__)
bool ok = py_binaryop(lhs, rhs, __ne__, __ne__); COMPARE_OP_IMPL(ne, __ne__, __ne__)
if(!ok) return -1; COMPARE_OP_IMPL(lt, __lt__, __gt__)
return py_tobool(py_lastretval()); COMPARE_OP_IMPL(le, __le__, __ge__)
} COMPARE_OP_IMPL(gt, __gt__, __lt__)
COMPARE_OP_IMPL(ge, __ge__, __le__)
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());
}

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);
@ -40,10 +40,7 @@ int py_eval(const char* source, py_Ref out) {
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 vm->last_error->type;
if(res == RES_RETURN) { if(res == RES_RETURN) return 0;
*out = vm->last_retval;
return 0;
}
PK_UNREACHABLE(); PK_UNREACHABLE();
} }

View File

@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h>
#include "pocketpy.h" #include "pocketpy.h"
@ -27,32 +28,42 @@ int main(int argc, char** argv) {
py_initialize(); py_initialize();
const char* source = "1 < 2"; const char* source = "1 < 2";
py_Ref r0 = py_reg(0); if(py_eval(source)) {
if(py_eval(source, r0)){
py_Error* err = py_lasterror(); py_Error* err = py_lasterror();
py_Error__print(err); py_Error__print(err);
}else{ } else {
// handle the result // handle the result
bool _L0 = py_tobool(r0); bool _L0 = py_tobool(py_lastretval());
printf("%d\n", _L0); 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(); py_finalize();
return 0; return 0;
// if(argc != 2) goto __HELP; // if(argc != 2) goto __HELP;
// char* source = read_file(argv[1]); // char* source = read_file(argv[1]);
// py_initialize(); // py_initialize();
// if(py_exec(source)){ // if(py_exec(source)){
// py_Error* err = py_getlasterror(); // py_Error* err = py_getlasterror();
// py_Error__print(err); // py_Error__print(err);
// } // }
// py_finalize(); // py_finalize();
// free(source); // free(source);
// __HELP: // __HELP:
// printf("Usage: pocketpy [filename]\n"); // printf("Usage: pocketpy [filename]\n");
// return 0; // return 0;
} }