mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
...
This commit is contained in:
parent
eb6d3ed2ae
commit
fae084ed44
@ -26,8 +26,8 @@ struct Vector2 {
|
|||||||
|
|
||||||
static void _register(VM* vm, PyObject* mod, PyObject* type){
|
static void _register(VM* vm, PyObject* mod, PyObject* type){
|
||||||
vm->bind_constructor<3>(type, [](VM* vm, ArgsView args){
|
vm->bind_constructor<3>(type, [](VM* vm, ArgsView args){
|
||||||
float x = vm->num_to_float(args[1]);
|
float x = VAR_F(args[1]);
|
||||||
float y = vm->num_to_float(args[2]);
|
float y = VAR_F(args[2]);
|
||||||
return VAR_T(Vector2, x, y);
|
return VAR_T(Vector2, x, y);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -52,13 +52,13 @@ struct Vector2 {
|
|||||||
|
|
||||||
vm->bind_method<1>(type, "__mul__", [](VM* vm, ArgsView args){
|
vm->bind_method<1>(type, "__mul__", [](VM* vm, ArgsView args){
|
||||||
Vector2& self = CAST(Vector2&, args[0]);
|
Vector2& self = CAST(Vector2&, args[0]);
|
||||||
f64 other = vm->num_to_float(args[1]);
|
f64 other = VAR_F(args[1]);
|
||||||
return VAR_T(Vector2, self.x * other, self.y * other);
|
return VAR_T(Vector2, self.x * other, self.y * other);
|
||||||
});
|
});
|
||||||
|
|
||||||
vm->bind_method<1>(type, "__truediv__", [](VM* vm, ArgsView args){
|
vm->bind_method<1>(type, "__truediv__", [](VM* vm, ArgsView args){
|
||||||
Vector2& self = CAST(Vector2&, args[0]);
|
Vector2& self = CAST(Vector2&, args[0]);
|
||||||
f64 other = vm->num_to_float(args[1]);
|
f64 other = VAR_F(args[1]);
|
||||||
return VAR_T(Vector2, self.x / other, self.y / other);
|
return VAR_T(Vector2, self.x / other, self.y / other);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -7,9 +7,9 @@ namespace pkpy {
|
|||||||
|
|
||||||
// https://github.com/zhicheng/base64/blob/master/base64.c
|
// https://github.com/zhicheng/base64/blob/master/base64.c
|
||||||
|
|
||||||
#define BASE64_PAD '='
|
inline static const char BASE64_PAD = '=';
|
||||||
#define BASE64DE_FIRST '+'
|
inline static const char BASE64DE_FIRST = '+';
|
||||||
#define BASE64DE_LAST 'z'
|
inline static const char BASE64DE_LAST = 'z';
|
||||||
|
|
||||||
/* BASE 64 encode table */
|
/* BASE 64 encode table */
|
||||||
static const char base64en[] = {
|
static const char base64en[] = {
|
||||||
|
@ -607,4 +607,8 @@ __NEXT_STEP:;
|
|||||||
#undef POPX
|
#undef POPX
|
||||||
#undef STACK_VIEW
|
#undef STACK_VIEW
|
||||||
|
|
||||||
|
#undef DISPATCH
|
||||||
|
#undef TARGET
|
||||||
|
#undef DISPATCH_OP_CALL
|
||||||
|
|
||||||
} // namespace pkpy
|
} // namespace pkpy
|
24
src/linalg.h
24
src/linalg.h
@ -344,8 +344,8 @@ struct PyVec2: Vec2 {
|
|||||||
|
|
||||||
static void _register(VM* vm, PyObject* mod, PyObject* type){
|
static void _register(VM* vm, PyObject* mod, PyObject* type){
|
||||||
vm->bind_constructor<3>(type, [](VM* vm, ArgsView args){
|
vm->bind_constructor<3>(type, [](VM* vm, ArgsView args){
|
||||||
float x = vm->num_to_float(args[1]);
|
float x = VAR_F(args[1]);
|
||||||
float y = vm->num_to_float(args[2]);
|
float y = VAR_F(args[2]);
|
||||||
return VAR(Vec2(x, y));
|
return VAR(Vec2(x, y));
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -387,9 +387,9 @@ struct PyVec3: Vec3 {
|
|||||||
|
|
||||||
static void _register(VM* vm, PyObject* mod, PyObject* type){
|
static void _register(VM* vm, PyObject* mod, PyObject* type){
|
||||||
vm->bind_constructor<4>(type, [](VM* vm, ArgsView args){
|
vm->bind_constructor<4>(type, [](VM* vm, ArgsView args){
|
||||||
float x = vm->num_to_float(args[1]);
|
float x = VAR_F(args[1]);
|
||||||
float y = vm->num_to_float(args[2]);
|
float y = VAR_F(args[2]);
|
||||||
float z = vm->num_to_float(args[3]);
|
float z = VAR_F(args[3]);
|
||||||
return VAR(Vec3(x, y, z));
|
return VAR(Vec3(x, y, z));
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -435,7 +435,7 @@ struct PyMat3x3: Mat3x3{
|
|||||||
if(args.size() == 1+0) return VAR_T(PyMat3x3, Mat3x3::zeros());
|
if(args.size() == 1+0) return VAR_T(PyMat3x3, Mat3x3::zeros());
|
||||||
if(args.size() == 1+9){
|
if(args.size() == 1+9){
|
||||||
Mat3x3 mat;
|
Mat3x3 mat;
|
||||||
for(int i=0; i<9; i++) mat.v[i] = vm->num_to_float(args[1+i]);
|
for(int i=0; i<9; i++) mat.v[i] = VAR_F(args[1+i]);
|
||||||
return VAR_T(PyMat3x3, mat);
|
return VAR_T(PyMat3x3, mat);
|
||||||
}
|
}
|
||||||
if(args.size() == 1+1){
|
if(args.size() == 1+1){
|
||||||
@ -446,7 +446,7 @@ struct PyMat3x3: Mat3x3{
|
|||||||
List& b = CAST(List&, a[i]);
|
List& b = CAST(List&, a[i]);
|
||||||
if(b.size() != 3) vm->ValueError("Mat3x3.__new__ takes 3x3 list");
|
if(b.size() != 3) vm->ValueError("Mat3x3.__new__ takes 3x3 list");
|
||||||
for(int j=0; j<3; j++){
|
for(int j=0; j<3; j++){
|
||||||
mat.m[i][j] = vm->num_to_float(b[j]);
|
mat.m[i][j] = VAR_F(b[j]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return VAR_T(PyMat3x3, mat);
|
return VAR_T(PyMat3x3, mat);
|
||||||
@ -512,7 +512,7 @@ struct PyMat3x3: Mat3x3{
|
|||||||
vm->IndexError("index out of range");
|
vm->IndexError("index out of range");
|
||||||
return vm->None;
|
return vm->None;
|
||||||
}
|
}
|
||||||
self.m[i][j] = vm->num_to_float(args[2]);
|
self.m[i][j] = VAR_F(args[2]);
|
||||||
return vm->None;
|
return vm->None;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -552,13 +552,13 @@ struct PyMat3x3: Mat3x3{
|
|||||||
|
|
||||||
vm->bind_method<1>(type, "__mul__", [](VM* vm, ArgsView args){
|
vm->bind_method<1>(type, "__mul__", [](VM* vm, ArgsView args){
|
||||||
PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
|
PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
|
||||||
f64 other = vm->num_to_float(args[1]);
|
f64 other = VAR_F(args[1]);
|
||||||
return VAR_T(PyMat3x3, self * other);
|
return VAR_T(PyMat3x3, self * other);
|
||||||
});
|
});
|
||||||
|
|
||||||
vm->bind_method<1>(type, "__truediv__", [](VM* vm, ArgsView args){
|
vm->bind_method<1>(type, "__truediv__", [](VM* vm, ArgsView args){
|
||||||
PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
|
PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
|
||||||
f64 other = vm->num_to_float(args[1]);
|
f64 other = VAR_F(args[1]);
|
||||||
return VAR_T(PyMat3x3, self / other);
|
return VAR_T(PyMat3x3, self / other);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -628,7 +628,7 @@ struct PyMat3x3: Mat3x3{
|
|||||||
});
|
});
|
||||||
|
|
||||||
vm->bind_func<1>(type, "rotate", [](VM* vm, ArgsView args){
|
vm->bind_func<1>(type, "rotate", [](VM* vm, ArgsView args){
|
||||||
f64 angle = vm->num_to_float(args[0]);
|
f64 angle = VAR_F(args[0]);
|
||||||
return VAR_T(PyMat3x3, Mat3x3::rotate(angle));
|
return VAR_T(PyMat3x3, Mat3x3::rotate(angle));
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -639,7 +639,7 @@ struct PyMat3x3: Mat3x3{
|
|||||||
|
|
||||||
vm->bind_func<3>(type, "trs", [](VM* vm, ArgsView args){
|
vm->bind_func<3>(type, "trs", [](VM* vm, ArgsView args){
|
||||||
PyVec2& t = CAST(PyVec2&, args[0]);
|
PyVec2& t = CAST(PyVec2&, args[0]);
|
||||||
f64 r = vm->num_to_float(args[1]);
|
f64 r = VAR_F(args[1]);
|
||||||
PyVec2& s = CAST(PyVec2&, args[2]);
|
PyVec2& s = CAST(PyVec2&, args[2]);
|
||||||
return VAR_T(PyMat3x3, Mat3x3::trs(t, r, s));
|
return VAR_T(PyMat3x3, Mat3x3::trs(t, r, s));
|
||||||
});
|
});
|
||||||
|
@ -263,7 +263,7 @@ __T _py_cast(VM* vm, PyObject* obj) {
|
|||||||
#define CAST(T, x) py_cast<T>(vm, x)
|
#define CAST(T, x) py_cast<T>(vm, x)
|
||||||
#define _CAST(T, x) _py_cast<T>(vm, x)
|
#define _CAST(T, x) _py_cast<T>(vm, x)
|
||||||
|
|
||||||
#define FLOAT(x) vm->num_to_float(x)
|
#define VAR_F(x) vm->num_to_float(x)
|
||||||
|
|
||||||
/*****************************************************************/
|
/*****************************************************************/
|
||||||
template<>
|
template<>
|
||||||
|
@ -46,14 +46,14 @@ inline CodeObject_ VM::compile(Str source, Str filename, CompileMode mode, bool
|
|||||||
_vm->bind_method<1>("int", #name, [](VM* vm, ArgsView args){ \
|
_vm->bind_method<1>("int", #name, [](VM* vm, ArgsView args){ \
|
||||||
if(is_int(args[1])) return VAR(_CAST(i64, args[0]) op _CAST(i64, args[1])); \
|
if(is_int(args[1])) return VAR(_CAST(i64, args[0]) op _CAST(i64, args[1])); \
|
||||||
if(is_float(args[1])) return VAR(vm->num_to_float(args[0]) op _CAST(f64, args[1])); \
|
if(is_float(args[1])) return VAR(vm->num_to_float(args[0]) op _CAST(f64, args[1])); \
|
||||||
if constexpr(is_eq) return VAR(args[0] op args[1]); \
|
if constexpr(is_eq) return VAR(args[0] op args[1]); \
|
||||||
vm->TypeError("unsupported operand type(s) for " #op ); \
|
vm->TypeError("unsupported operand type(s) for " #op ); \
|
||||||
return vm->None; \
|
return vm->None; \
|
||||||
}); \
|
}); \
|
||||||
_vm->bind_method<1>("float", #name, [](VM* vm, ArgsView args){ \
|
_vm->bind_method<1>("float", #name, [](VM* vm, ArgsView args){ \
|
||||||
if(is_float(args[1])) return VAR(_CAST(f64, args[0]) op _CAST(f64, args[1])); \
|
if(is_float(args[1])) return VAR(_CAST(f64, args[0]) op _CAST(f64, args[1])); \
|
||||||
if(is_int(args[1])) return VAR(_CAST(f64, args[0]) op _CAST(i64, args[1])); \
|
if(is_int(args[1])) return VAR(_CAST(f64, args[0]) op _CAST(i64, args[1])); \
|
||||||
if constexpr(is_eq) return VAR(args[0] op args[1]); \
|
if constexpr(is_eq) return VAR(args[0] op args[1]); \
|
||||||
vm->TypeError("unsupported operand type(s) for " #op ); \
|
vm->TypeError("unsupported operand type(s) for " #op ); \
|
||||||
return vm->None; \
|
return vm->None; \
|
||||||
});
|
});
|
||||||
@ -228,13 +228,13 @@ inline void init_builtins(VM* _vm) {
|
|||||||
_vm->bind_method<0>("NoneType", "__json__", CPP_LAMBDA(VAR("null")));
|
_vm->bind_method<0>("NoneType", "__json__", CPP_LAMBDA(VAR("null")));
|
||||||
|
|
||||||
_vm->bind_method<1>("int", "__truediv__", [](VM* vm, ArgsView args) {
|
_vm->bind_method<1>("int", "__truediv__", [](VM* vm, ArgsView args) {
|
||||||
f64 rhs = vm->num_to_float(args[1]);
|
f64 rhs = VAR_F(args[1]);
|
||||||
if (rhs == 0) vm->ZeroDivisionError();
|
if (rhs == 0) vm->ZeroDivisionError();
|
||||||
return VAR(_CAST(i64, args[0]) / rhs);
|
return VAR(_CAST(i64, args[0]) / rhs);
|
||||||
});
|
});
|
||||||
|
|
||||||
_vm->bind_method<1>("float", "__truediv__", [](VM* vm, ArgsView args) {
|
_vm->bind_method<1>("float", "__truediv__", [](VM* vm, ArgsView args) {
|
||||||
f64 rhs = vm->num_to_float(args[1]);
|
f64 rhs = VAR_F(args[1]);
|
||||||
if (rhs == 0) vm->ZeroDivisionError();
|
if (rhs == 0) vm->ZeroDivisionError();
|
||||||
return VAR(_CAST(f64, args[0]) / rhs);
|
return VAR(_CAST(f64, args[0]) / rhs);
|
||||||
});
|
});
|
||||||
@ -254,7 +254,7 @@ inline void init_builtins(VM* _vm) {
|
|||||||
if(flag) return VAR((f64)(1.0 / ret));
|
if(flag) return VAR((f64)(1.0 / ret));
|
||||||
return VAR(ret);
|
return VAR(ret);
|
||||||
}else{
|
}else{
|
||||||
return VAR((f64)std::pow(vm->num_to_float(args[0]), vm->num_to_float(args[1])));
|
return VAR((f64)std::pow(VAR_F(args[0]), VAR_F(args[1])));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -799,7 +799,7 @@ inline void add_module_time(VM* vm){
|
|||||||
});
|
});
|
||||||
|
|
||||||
vm->bind_func<1>(mod, "sleep", [](VM* vm, ArgsView args) {
|
vm->bind_func<1>(mod, "sleep", [](VM* vm, ArgsView args) {
|
||||||
f64 seconds = FLOAT(args[0]);
|
f64 seconds = VAR_F(args[0]);
|
||||||
auto begin = std::chrono::high_resolution_clock::now();
|
auto begin = std::chrono::high_resolution_clock::now();
|
||||||
while(true){
|
while(true){
|
||||||
auto now = std::chrono::high_resolution_clock::now();
|
auto now = std::chrono::high_resolution_clock::now();
|
||||||
@ -850,15 +850,15 @@ inline void add_module_math(VM* vm){
|
|||||||
mod->attr().set("inf", VAR(std::numeric_limits<double>::infinity()));
|
mod->attr().set("inf", VAR(std::numeric_limits<double>::infinity()));
|
||||||
mod->attr().set("nan", VAR(std::numeric_limits<double>::quiet_NaN()));
|
mod->attr().set("nan", VAR(std::numeric_limits<double>::quiet_NaN()));
|
||||||
|
|
||||||
vm->bind_func<1>(mod, "ceil", CPP_LAMBDA(VAR((i64)std::ceil(vm->num_to_float(args[0])))));
|
vm->bind_func<1>(mod, "ceil", CPP_LAMBDA(VAR((i64)std::ceil(VAR_F(args[0])))));
|
||||||
vm->bind_func<1>(mod, "fabs", CPP_LAMBDA(VAR(std::fabs(vm->num_to_float(args[0])))));
|
vm->bind_func<1>(mod, "fabs", CPP_LAMBDA(VAR(std::fabs(VAR_F(args[0])))));
|
||||||
vm->bind_func<1>(mod, "floor", CPP_LAMBDA(VAR((i64)std::floor(vm->num_to_float(args[0])))));
|
vm->bind_func<1>(mod, "floor", CPP_LAMBDA(VAR((i64)std::floor(VAR_F(args[0])))));
|
||||||
vm->bind_func<1>(mod, "fsum", [](VM* vm, ArgsView args) {
|
vm->bind_func<1>(mod, "fsum", [](VM* vm, ArgsView args) {
|
||||||
List& list = CAST(List&, args[0]);
|
List& list = CAST(List&, args[0]);
|
||||||
double sum = 0;
|
double sum = 0;
|
||||||
double c = 0;
|
double c = 0;
|
||||||
for(PyObject* arg : list){
|
for(PyObject* arg : list){
|
||||||
double x = vm->num_to_float(arg);
|
double x = VAR_F(arg);
|
||||||
double y = x - c;
|
double y = x - c;
|
||||||
double t = sum + y;
|
double t = sum + y;
|
||||||
c = (t - sum) - y;
|
c = (t - sum) - y;
|
||||||
@ -879,29 +879,29 @@ inline void add_module_math(VM* vm){
|
|||||||
return VAR(a);
|
return VAR(a);
|
||||||
});
|
});
|
||||||
|
|
||||||
vm->bind_func<1>(mod, "isfinite", CPP_LAMBDA(VAR(std::isfinite(vm->num_to_float(args[0])))));
|
vm->bind_func<1>(mod, "isfinite", CPP_LAMBDA(VAR(std::isfinite(VAR_F(args[0])))));
|
||||||
vm->bind_func<1>(mod, "isinf", CPP_LAMBDA(VAR(std::isinf(vm->num_to_float(args[0])))));
|
vm->bind_func<1>(mod, "isinf", CPP_LAMBDA(VAR(std::isinf(VAR_F(args[0])))));
|
||||||
vm->bind_func<1>(mod, "isnan", CPP_LAMBDA(VAR(std::isnan(vm->num_to_float(args[0])))));
|
vm->bind_func<1>(mod, "isnan", CPP_LAMBDA(VAR(std::isnan(VAR_F(args[0])))));
|
||||||
|
|
||||||
vm->bind_func<1>(mod, "exp", CPP_LAMBDA(VAR(std::exp(vm->num_to_float(args[0])))));
|
vm->bind_func<1>(mod, "exp", CPP_LAMBDA(VAR(std::exp(VAR_F(args[0])))));
|
||||||
vm->bind_func<1>(mod, "log", CPP_LAMBDA(VAR(std::log(vm->num_to_float(args[0])))));
|
vm->bind_func<1>(mod, "log", CPP_LAMBDA(VAR(std::log(VAR_F(args[0])))));
|
||||||
vm->bind_func<1>(mod, "log2", CPP_LAMBDA(VAR(std::log2(vm->num_to_float(args[0])))));
|
vm->bind_func<1>(mod, "log2", CPP_LAMBDA(VAR(std::log2(VAR_F(args[0])))));
|
||||||
vm->bind_func<1>(mod, "log10", CPP_LAMBDA(VAR(std::log10(vm->num_to_float(args[0])))));
|
vm->bind_func<1>(mod, "log10", CPP_LAMBDA(VAR(std::log10(VAR_F(args[0])))));
|
||||||
|
|
||||||
vm->bind_func<2>(mod, "pow", CPP_LAMBDA(VAR(std::pow(vm->num_to_float(args[0]), vm->num_to_float(args[1])))));
|
vm->bind_func<2>(mod, "pow", CPP_LAMBDA(VAR(std::pow(VAR_F(args[0]), VAR_F(args[1])))));
|
||||||
vm->bind_func<1>(mod, "sqrt", CPP_LAMBDA(VAR(std::sqrt(vm->num_to_float(args[0])))));
|
vm->bind_func<1>(mod, "sqrt", CPP_LAMBDA(VAR(std::sqrt(VAR_F(args[0])))));
|
||||||
|
|
||||||
vm->bind_func<1>(mod, "acos", CPP_LAMBDA(VAR(std::acos(vm->num_to_float(args[0])))));
|
vm->bind_func<1>(mod, "acos", CPP_LAMBDA(VAR(std::acos(VAR_F(args[0])))));
|
||||||
vm->bind_func<1>(mod, "asin", CPP_LAMBDA(VAR(std::asin(vm->num_to_float(args[0])))));
|
vm->bind_func<1>(mod, "asin", CPP_LAMBDA(VAR(std::asin(VAR_F(args[0])))));
|
||||||
vm->bind_func<1>(mod, "atan", CPP_LAMBDA(VAR(std::atan(vm->num_to_float(args[0])))));
|
vm->bind_func<1>(mod, "atan", CPP_LAMBDA(VAR(std::atan(VAR_F(args[0])))));
|
||||||
vm->bind_func<2>(mod, "atan2", CPP_LAMBDA(VAR(std::atan2(vm->num_to_float(args[0]), vm->num_to_float(args[1])))));
|
vm->bind_func<2>(mod, "atan2", CPP_LAMBDA(VAR(std::atan2(VAR_F(args[0]), VAR_F(args[1])))));
|
||||||
|
|
||||||
vm->bind_func<1>(mod, "cos", CPP_LAMBDA(VAR(std::cos(vm->num_to_float(args[0])))));
|
vm->bind_func<1>(mod, "cos", CPP_LAMBDA(VAR(std::cos(VAR_F(args[0])))));
|
||||||
vm->bind_func<1>(mod, "sin", CPP_LAMBDA(VAR(std::sin(vm->num_to_float(args[0])))));
|
vm->bind_func<1>(mod, "sin", CPP_LAMBDA(VAR(std::sin(VAR_F(args[0])))));
|
||||||
vm->bind_func<1>(mod, "tan", CPP_LAMBDA(VAR(std::tan(vm->num_to_float(args[0])))));
|
vm->bind_func<1>(mod, "tan", CPP_LAMBDA(VAR(std::tan(VAR_F(args[0])))));
|
||||||
|
|
||||||
vm->bind_func<1>(mod, "degrees", CPP_LAMBDA(VAR(vm->num_to_float(args[0]) * 180 / 3.1415926535897932384)));
|
vm->bind_func<1>(mod, "degrees", CPP_LAMBDA(VAR(VAR_F(args[0]) * 180 / 3.1415926535897932384)));
|
||||||
vm->bind_func<1>(mod, "radians", CPP_LAMBDA(VAR(vm->num_to_float(args[0]) * 3.1415926535897932384 / 180)));
|
vm->bind_func<1>(mod, "radians", CPP_LAMBDA(VAR(VAR_F(args[0]) * 3.1415926535897932384 / 180)));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void add_module_dis(VM* vm){
|
inline void add_module_dis(VM* vm){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user