CAST(f64, x) accept i64 now

This commit is contained in:
BLUELOVETH 2023-07-07 18:27:38 +08:00
parent b520abf7a2
commit 4cb6241598
4 changed files with 30 additions and 31 deletions

View File

@ -223,7 +223,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_F(x) vm->num_to_float(x)
#define CAST_F(x) py_cast<f64>(vm, x)
#define CAST_DEFAULT(T, x, default_value) (x != vm->None) ? py_cast<T>(vm, x) : (default_value)
/*****************************************************************/

View File

@ -382,10 +382,7 @@ public:
check_type(obj, tp_int); // if failed, redirect to check_type to raise TypeError
}
void check_float(PyObject* obj){
if(is_float(obj)) return;
check_type(obj, tp_float); // if failed, redirect to check_type to raise TypeError
}
void check_int_or_float(PyObject* obj);
PyObject* _t(Type t){
return _all_types[t.index].obj;
@ -433,7 +430,6 @@ public:
PyObject* vectorcall(int ARGC, int KWARGC=0, bool op_call=false);
CodeObject_ compile(Str source, Str filename, CompileMode mode, bool unknown_global_scope=false);
PyObject* py_negate(PyObject* obj);
f64 num_to_float(PyObject* obj);
bool py_bool(PyObject* obj);
i64 py_hash(PyObject* obj);
PyObject* py_list(PyObject*);
@ -498,24 +494,32 @@ PY_CAST_INT(unsigned long)
PY_CAST_INT(unsigned long long)
template<> inline float py_cast<float>(VM* vm, PyObject* obj){
vm->check_float(obj);
if(is_float(obj)){
i64 bits = PK_BITS(obj) & Number::c1;
return BitsCvt(bits)._float;
}
if(is_int(obj)){
return (float)_py_cast<i64>(vm, obj);
}
vm->check_int_or_float(obj); // error!
return 0;
}
template<> inline float _py_cast<float>(VM* vm, PyObject* obj){
PK_UNUSED(vm);
i64 bits = PK_BITS(obj) & Number::c1;
return BitsCvt(bits)._float;
return py_cast<float>(vm, obj);
}
template<> inline double py_cast<double>(VM* vm, PyObject* obj){
vm->check_float(obj);
if(is_float(obj)){
i64 bits = PK_BITS(obj) & Number::c1;
return BitsCvt(bits)._float;
}
if(is_int(obj)){
return (float)_py_cast<i64>(vm, obj);
}
vm->check_int_or_float(obj); // error!
return 0;
}
template<> inline double _py_cast<double>(VM* vm, PyObject* obj){
PK_UNUSED(vm);
i64 bits = PK_BITS(obj) & Number::c1;
return BitsCvt(bits)._float;
return py_cast<double>(vm, obj);
}

View File

@ -18,7 +18,7 @@ namespace pkpy{
#define BIND_VEC_FLOAT_OP(D, name, op) \
vm->bind_method<1>(type, #name, [](VM* vm, ArgsView args){ \
PyVec##D& self = _CAST(PyVec##D&, args[0]); \
f64 other = vm->num_to_float(args[1]); \
f64 other = CAST(f64, args[1]); \
return VAR(self op other); \
});
@ -41,7 +41,7 @@ namespace pkpy{
return VAR(self.name); \
}, [](VM* vm, ArgsView args){ \
PyVec##D& self = _CAST(PyVec##D&, args[0]); \
self.name = vm->num_to_float(args[1]); \
self.name = CAST(f64, args[1]); \
return vm->None; \
}));
@ -72,7 +72,7 @@ namespace pkpy{
vm->bind_method<1>(type, "rotate", [](VM* vm, ArgsView args){
Vec2 self = _CAST(PyVec2&, args[0]);
float radian = vm->num_to_float(args[1]);
float radian = CAST(f64, args[1]);
float cr = cosf(radian);
float sr = sinf(radian);
Mat3x3 rotate(cr, -sr, 0.0f,
@ -289,7 +289,7 @@ namespace pkpy{
return VAR(self.field); \
}, [](VM* vm, ArgsView args){ \
PyMat3x3& self = _CAST(PyMat3x3&, args[0]); \
self.field = vm->num_to_float(args[1]); \
self.field = CAST(f64, args[1]); \
return vm->None; \
}));

View File

@ -278,16 +278,11 @@ PyObject* VM::py_negate(PyObject* obj){
return call_method(obj, __neg__);
}
f64 VM::num_to_float(PyObject* obj){
if(is_float(obj)){
return _CAST(f64, obj);
} else if (is_int(obj)){
return (f64)_CAST(i64, obj);
}
void VM::check_int_or_float(PyObject *obj){
if(!is_tagged(obj)){
TypeError("expected 'int' or 'float', got " + OBJ_NAME(_t(obj)).escape());
return 0;
}
}
bool VM::py_bool(PyObject* obj){
if(is_non_tagged_type(obj, tp_bool)) return obj == True;
@ -416,7 +411,7 @@ PyObject* VM::format(Str spec, PyObject* obj){
if(type != 'f' && dot >= 0) ValueError("precision not allowed in the format specifier");
Str ret;
if(type == 'f'){
f64 val = num_to_float(obj);
f64 val = CAST(f64, obj);
if(precision < 0) precision = 6;
std::stringstream ss;
ss << std::fixed << std::setprecision(precision) << val;