mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
...
This commit is contained in:
parent
29c28b0281
commit
5dd6fe2387
@ -26,8 +26,8 @@ struct Vector2 {
|
||||
|
||||
static void _register(VM* vm, PyObject* mod, PyObject* type){
|
||||
vm->bind_constructor<3>(type, [](VM* vm, ArgsView args){
|
||||
float x = VAR_F(args[1]);
|
||||
float y = VAR_F(args[2]);
|
||||
float x = CAST_F(args[1]);
|
||||
float y = CAST_F(args[2]);
|
||||
return VAR_T(Vector2, x, y);
|
||||
});
|
||||
|
||||
@ -52,13 +52,13 @@ struct Vector2 {
|
||||
|
||||
vm->bind_method<1>(type, "__mul__", [](VM* vm, ArgsView args){
|
||||
Vector2& self = CAST(Vector2&, args[0]);
|
||||
f64 other = VAR_F(args[1]);
|
||||
f64 other = CAST_F(args[1]);
|
||||
return VAR_T(Vector2, self.x * other, self.y * other);
|
||||
});
|
||||
|
||||
vm->bind_method<1>(type, "__truediv__", [](VM* vm, ArgsView args){
|
||||
Vector2& self = CAST(Vector2&, args[0]);
|
||||
f64 other = VAR_F(args[1]);
|
||||
f64 other = CAST_F(args[1]);
|
||||
return VAR_T(Vector2, self.x / other, self.y / other);
|
||||
});
|
||||
|
||||
|
22
src/linalg.h
22
src/linalg.h
@ -322,8 +322,8 @@ struct PyVec2: Vec2 {
|
||||
|
||||
static void _register(VM* vm, PyObject* mod, PyObject* type){
|
||||
vm->bind_constructor<3>(type, [](VM* vm, ArgsView args){
|
||||
float x = VAR_F(args[1]);
|
||||
float y = VAR_F(args[2]);
|
||||
float x = CAST_F(args[1]);
|
||||
float y = CAST_F(args[2]);
|
||||
return VAR(Vec2(x, y));
|
||||
});
|
||||
|
||||
@ -375,9 +375,9 @@ struct PyVec3: Vec3 {
|
||||
|
||||
static void _register(VM* vm, PyObject* mod, PyObject* type){
|
||||
vm->bind_constructor<4>(type, [](VM* vm, ArgsView args){
|
||||
float x = VAR_F(args[1]);
|
||||
float y = VAR_F(args[2]);
|
||||
float z = VAR_F(args[3]);
|
||||
float x = CAST_F(args[1]);
|
||||
float y = CAST_F(args[2]);
|
||||
float z = CAST_F(args[3]);
|
||||
return VAR(Vec3(x, y, z));
|
||||
});
|
||||
|
||||
@ -421,7 +421,7 @@ struct PyMat3x3: Mat3x3{
|
||||
if(args.size() == 1+0) return VAR_T(PyMat3x3, Mat3x3::zeros());
|
||||
if(args.size() == 1+9){
|
||||
Mat3x3 mat;
|
||||
for(int i=0; i<9; i++) mat.v[i] = VAR_F(args[1+i]);
|
||||
for(int i=0; i<9; i++) mat.v[i] = CAST_F(args[1+i]);
|
||||
return VAR_T(PyMat3x3, mat);
|
||||
}
|
||||
if(args.size() == 1+1){
|
||||
@ -432,7 +432,7 @@ struct PyMat3x3: Mat3x3{
|
||||
List& b = CAST(List&, a[i]);
|
||||
if(b.size() != 3) vm->ValueError("Mat3x3.__new__ takes 3x3 list");
|
||||
for(int j=0; j<3; j++){
|
||||
mat.m[i][j] = VAR_F(b[j]);
|
||||
mat.m[i][j] = CAST_F(b[j]);
|
||||
}
|
||||
}
|
||||
return VAR_T(PyMat3x3, mat);
|
||||
@ -498,7 +498,7 @@ struct PyMat3x3: Mat3x3{
|
||||
vm->IndexError("index out of range");
|
||||
return vm->None;
|
||||
}
|
||||
self.m[i][j] = VAR_F(args[2]);
|
||||
self.m[i][j] = CAST_F(args[2]);
|
||||
return vm->None;
|
||||
});
|
||||
|
||||
@ -538,13 +538,13 @@ struct PyMat3x3: Mat3x3{
|
||||
|
||||
vm->bind_method<1>(type, "__mul__", [](VM* vm, ArgsView args){
|
||||
PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
|
||||
f64 other = VAR_F(args[1]);
|
||||
f64 other = CAST_F(args[1]);
|
||||
return VAR_T(PyMat3x3, self * other);
|
||||
});
|
||||
|
||||
vm->bind_method<1>(type, "__truediv__", [](VM* vm, ArgsView args){
|
||||
PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
|
||||
f64 other = VAR_F(args[1]);
|
||||
f64 other = CAST_F(args[1]);
|
||||
return VAR_T(PyMat3x3, self / other);
|
||||
});
|
||||
|
||||
@ -604,7 +604,7 @@ struct PyMat3x3: Mat3x3{
|
||||
/*************** affine transformations ***************/
|
||||
vm->bind_func<3>(type, "trs", [](VM* vm, ArgsView args){
|
||||
PyVec2& t = CAST(PyVec2&, args[0]);
|
||||
f64 r = VAR_F(args[1]);
|
||||
f64 r = CAST_F(args[1]);
|
||||
PyVec2& s = CAST(PyVec2&, args[2]);
|
||||
return VAR_T(PyMat3x3, Mat3x3::trs(t, r, s));
|
||||
});
|
||||
|
@ -279,7 +279,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 VAR_F(x) vm->num_to_float(x)
|
||||
#define CAST_F(x) vm->num_to_float(x)
|
||||
|
||||
/*****************************************************************/
|
||||
template<>
|
||||
|
@ -226,12 +226,12 @@ inline void init_builtins(VM* _vm) {
|
||||
_vm->bind__json__(_vm->_type("NoneType"), [](VM* vm, PyObject* obj) { return VAR("null"); });
|
||||
|
||||
_vm->bind__truediv__(_vm->tp_float, [](VM* vm, PyObject* lhs, PyObject* rhs) {
|
||||
f64 value = VAR_F(rhs);
|
||||
f64 value = CAST_F(rhs);
|
||||
return VAR(_CAST(f64, lhs) / value);
|
||||
});
|
||||
|
||||
_vm->bind__truediv__(_vm->tp_int, [](VM* vm, PyObject* lhs, PyObject* rhs) {
|
||||
f64 value = VAR_F(rhs);
|
||||
f64 value = CAST_F(rhs);
|
||||
return VAR(_CAST(i64, lhs) / value);
|
||||
});
|
||||
|
||||
@ -250,7 +250,7 @@ inline void init_builtins(VM* _vm) {
|
||||
if(flag) return VAR((f64)(1.0 / ret));
|
||||
return VAR(ret);
|
||||
}else{
|
||||
return VAR((f64)std::pow(VAR_F(lhs_), VAR_F(rhs_)));
|
||||
return VAR((f64)std::pow(CAST_F(lhs_), CAST_F(rhs_)));
|
||||
}
|
||||
};
|
||||
|
||||
@ -1022,7 +1022,7 @@ inline void add_module_time(VM* vm){
|
||||
});
|
||||
|
||||
vm->bind_func<1>(mod, "sleep", [](VM* vm, ArgsView args) {
|
||||
f64 seconds = VAR_F(args[0]);
|
||||
f64 seconds = CAST_F(args[0]);
|
||||
auto begin = std::chrono::high_resolution_clock::now();
|
||||
while(true){
|
||||
auto now = std::chrono::high_resolution_clock::now();
|
||||
@ -1075,15 +1075,15 @@ inline void add_module_math(VM* vm){
|
||||
mod->attr().set("inf", VAR(std::numeric_limits<double>::infinity()));
|
||||
mod->attr().set("nan", VAR(std::numeric_limits<double>::quiet_NaN()));
|
||||
|
||||
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(VAR_F(args[0])))));
|
||||
vm->bind_func<1>(mod, "floor", CPP_LAMBDA(VAR((i64)std::floor(VAR_F(args[0])))));
|
||||
vm->bind_func<1>(mod, "ceil", CPP_LAMBDA(VAR((i64)std::ceil(CAST_F(args[0])))));
|
||||
vm->bind_func<1>(mod, "fabs", CPP_LAMBDA(VAR(std::fabs(CAST_F(args[0])))));
|
||||
vm->bind_func<1>(mod, "floor", CPP_LAMBDA(VAR((i64)std::floor(CAST_F(args[0])))));
|
||||
vm->bind_func<1>(mod, "fsum", [](VM* vm, ArgsView args) {
|
||||
List& list = CAST(List&, args[0]);
|
||||
double sum = 0;
|
||||
double c = 0;
|
||||
for(PyObject* arg : list){
|
||||
double x = VAR_F(arg);
|
||||
double x = CAST_F(arg);
|
||||
double y = x - c;
|
||||
double t = sum + y;
|
||||
c = (t - sum) - y;
|
||||
@ -1104,29 +1104,29 @@ inline void add_module_math(VM* vm){
|
||||
return VAR(a);
|
||||
});
|
||||
|
||||
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(VAR_F(args[0])))));
|
||||
vm->bind_func<1>(mod, "isnan", CPP_LAMBDA(VAR(std::isnan(VAR_F(args[0])))));
|
||||
vm->bind_func<1>(mod, "isfinite", CPP_LAMBDA(VAR(std::isfinite(CAST_F(args[0])))));
|
||||
vm->bind_func<1>(mod, "isinf", CPP_LAMBDA(VAR(std::isinf(CAST_F(args[0])))));
|
||||
vm->bind_func<1>(mod, "isnan", CPP_LAMBDA(VAR(std::isnan(CAST_F(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(VAR_F(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(VAR_F(args[0])))));
|
||||
vm->bind_func<1>(mod, "exp", CPP_LAMBDA(VAR(std::exp(CAST_F(args[0])))));
|
||||
vm->bind_func<1>(mod, "log", CPP_LAMBDA(VAR(std::log(CAST_F(args[0])))));
|
||||
vm->bind_func<1>(mod, "log2", CPP_LAMBDA(VAR(std::log2(CAST_F(args[0])))));
|
||||
vm->bind_func<1>(mod, "log10", CPP_LAMBDA(VAR(std::log10(CAST_F(args[0])))));
|
||||
|
||||
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(VAR_F(args[0])))));
|
||||
vm->bind_func<2>(mod, "pow", CPP_LAMBDA(VAR(std::pow(CAST_F(args[0]), CAST_F(args[1])))));
|
||||
vm->bind_func<1>(mod, "sqrt", CPP_LAMBDA(VAR(std::sqrt(CAST_F(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(VAR_F(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(VAR_F(args[0]), VAR_F(args[1])))));
|
||||
vm->bind_func<1>(mod, "acos", CPP_LAMBDA(VAR(std::acos(CAST_F(args[0])))));
|
||||
vm->bind_func<1>(mod, "asin", CPP_LAMBDA(VAR(std::asin(CAST_F(args[0])))));
|
||||
vm->bind_func<1>(mod, "atan", CPP_LAMBDA(VAR(std::atan(CAST_F(args[0])))));
|
||||
vm->bind_func<2>(mod, "atan2", CPP_LAMBDA(VAR(std::atan2(CAST_F(args[0]), CAST_F(args[1])))));
|
||||
|
||||
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(VAR_F(args[0])))));
|
||||
vm->bind_func<1>(mod, "tan", CPP_LAMBDA(VAR(std::tan(VAR_F(args[0])))));
|
||||
vm->bind_func<1>(mod, "cos", CPP_LAMBDA(VAR(std::cos(CAST_F(args[0])))));
|
||||
vm->bind_func<1>(mod, "sin", CPP_LAMBDA(VAR(std::sin(CAST_F(args[0])))));
|
||||
vm->bind_func<1>(mod, "tan", CPP_LAMBDA(VAR(std::tan(CAST_F(args[0])))));
|
||||
|
||||
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(VAR_F(args[0]) * 3.1415926535897932384 / 180)));
|
||||
vm->bind_func<1>(mod, "degrees", CPP_LAMBDA(VAR(CAST_F(args[0]) * 180 / 3.1415926535897932384)));
|
||||
vm->bind_func<1>(mod, "radians", CPP_LAMBDA(VAR(CAST_F(args[0]) * 3.1415926535897932384 / 180)));
|
||||
}
|
||||
|
||||
inline void add_module_dis(VM* vm){
|
||||
|
Loading…
x
Reference in New Issue
Block a user