1
0

cast.c 957 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "pocketpy/pocketpy.h"
  2. #include "pocketpy/common/utils.h"
  3. #include "pocketpy/objects/object.h"
  4. #include "pocketpy/interpreter/vm.h"
  5. int64_t py_toint(py_Ref self){
  6. return self->_i64;
  7. }
  8. double py_tofloat(py_Ref self){
  9. return self->_f64;
  10. }
  11. bool py_castfloat(py_Ref self, double* out){
  12. switch(self->type){
  13. case tp_int:
  14. *out = (double)self->_i64;
  15. return true;
  16. case tp_float:
  17. *out = self->_f64;
  18. return true;
  19. case tp_bool:
  20. *out = self->extra;
  21. return true;
  22. default:
  23. return false;
  24. }
  25. }
  26. bool py_tobool(py_Ref self){
  27. return self->extra;
  28. }
  29. const py_Str* py_tostr(py_Ref self){
  30. return PyObject__value(self->_obj);
  31. }
  32. const char* py_tocstr(py_Ref self){
  33. const py_Str* s = PyObject__value(self->_obj);
  34. return py_Str__data(s);
  35. }
  36. bool py_istype(const py_Ref self, py_Type type){
  37. return self->type == type;
  38. }