1
0

py_number.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #include "pocketpy/interpreter/vm.h"
  2. #include "pocketpy/pocketpy.h"
  3. #include <math.h>
  4. #define DEF_NUM_BINARY_OP(name, op, rint, rfloat) \
  5. static bool _py_int##name(int argc, py_Ref argv) { \
  6. py_checkargc(2); \
  7. if(py_isint(&argv[1])) { \
  8. int64_t lhs = py_toint(&argv[0]); \
  9. int64_t rhs = py_toint(&argv[1]); \
  10. rint(py_retval(), lhs op rhs); \
  11. } else if(py_isfloat(&argv[1])) { \
  12. int64_t lhs = py_toint(&argv[0]); \
  13. double rhs = py_tofloat(&argv[1]); \
  14. rfloat(py_retval(), lhs op rhs); \
  15. } else { \
  16. py_newnotimplemented(py_retval()); \
  17. } \
  18. return true; \
  19. } \
  20. static bool _py_float##name(int argc, py_Ref argv) { \
  21. py_checkargc(2); \
  22. double lhs = py_tofloat(&argv[0]); \
  23. double rhs; \
  24. if(py_castfloat(&argv[1], &rhs)) { \
  25. rfloat(py_retval(), lhs op rhs); \
  26. } else { \
  27. py_newnotimplemented(py_retval()); \
  28. } \
  29. return true; \
  30. }
  31. DEF_NUM_BINARY_OP(__add__, +, py_newint, py_newfloat)
  32. DEF_NUM_BINARY_OP(__sub__, -, py_newint, py_newfloat)
  33. DEF_NUM_BINARY_OP(__mul__, *, py_newint, py_newfloat)
  34. DEF_NUM_BINARY_OP(__eq__, ==, py_newbool, py_newbool)
  35. DEF_NUM_BINARY_OP(__ne__, ==, py_newbool, py_newbool)
  36. DEF_NUM_BINARY_OP(__lt__, <, py_newbool, py_newbool)
  37. DEF_NUM_BINARY_OP(__le__, <=, py_newbool, py_newbool)
  38. DEF_NUM_BINARY_OP(__gt__, >, py_newbool, py_newbool)
  39. DEF_NUM_BINARY_OP(__ge__, >=, py_newbool, py_newbool)
  40. #undef DEF_NUM_BINARY_OP
  41. static bool _py_int__neg__(int argc, py_Ref argv) {
  42. py_checkargc(1);
  43. int64_t val = py_toint(&argv[0]);
  44. py_newint(py_retval(), -val);
  45. return true;
  46. }
  47. static bool _py_float__neg__(int argc, py_Ref argv) {
  48. py_checkargc(1);
  49. double val = py_tofloat(&argv[0]);
  50. py_newfloat(py_retval(), -val);
  51. return true;
  52. }
  53. static bool _py_int__truediv__(int argc, py_Ref argv) {
  54. py_checkargc(2);
  55. int64_t lhs = py_toint(&argv[0]);
  56. double rhs;
  57. if(py_castfloat(&argv[1], &rhs)) {
  58. py_newfloat(py_retval(), lhs / rhs);
  59. } else {
  60. py_newnotimplemented(py_retval());
  61. }
  62. return true;
  63. }
  64. static bool _py_float__truediv__(int argc, py_Ref argv) {
  65. py_checkargc(2);
  66. double lhs = py_tofloat(&argv[0]);
  67. double rhs;
  68. if(py_castfloat(&argv[1], &rhs)) {
  69. py_newfloat(py_retval(), lhs / rhs);
  70. } else {
  71. py_newnotimplemented(py_retval());
  72. }
  73. return true;
  74. }
  75. #define ZeroDivisionError(msg) false
  76. static bool _py_number__pow__(int argc, py_Ref argv) {
  77. py_checkargc(2);
  78. if(py_isint(&argv[0]) && py_isint(&argv[1])) {
  79. int64_t lhs = py_toint(&argv[0]);
  80. int64_t rhs = py_toint(&argv[1]);
  81. if(rhs < 0) {
  82. if(lhs == 0) {
  83. return ZeroDivisionError("0.0 cannot be raised to a negative power");
  84. } else {
  85. py_newfloat(py_retval(), pow(lhs, rhs));
  86. }
  87. } else {
  88. // rhs >= 0
  89. int64_t ret = 1;
  90. while(true){
  91. if(rhs & 1) ret *= lhs;
  92. rhs >>= 1;
  93. if(!rhs) break;
  94. lhs *= lhs; // place this here to avoid overflow
  95. }
  96. py_newint(py_retval(), ret);
  97. }
  98. } else {
  99. double lhs, rhs;
  100. py_castfloat(&argv[0], &lhs);
  101. if(py_castfloat(&argv[1], &rhs)) {
  102. py_newfloat(py_retval(), pow(lhs, rhs));
  103. } else {
  104. py_newnotimplemented(py_retval());
  105. }
  106. }
  107. return true;
  108. }
  109. static bool _py_int__floordiv__(int argc, py_Ref argv) {
  110. py_checkargc(2);
  111. int64_t lhs = py_toint(&argv[0]);
  112. if(py_isint(&argv[1])) {
  113. int64_t rhs = py_toint(&argv[1]);
  114. if(rhs == 0) return -1;
  115. py_newint(py_retval(), lhs / rhs);
  116. } else {
  117. py_newnotimplemented(py_retval());
  118. }
  119. return true;
  120. }
  121. static bool _py_int__mod__(int argc, py_Ref argv) {
  122. py_checkargc(2);
  123. int64_t lhs = py_toint(&argv[0]);
  124. if(py_isint(&argv[1])) {
  125. int64_t rhs = py_toint(&argv[1]);
  126. if(rhs == 0) return ZeroDivisionError("integer division or modulo by zero");
  127. py_newint(py_retval(), lhs % rhs);
  128. } else {
  129. py_newnotimplemented(py_retval());
  130. }
  131. return true;
  132. }
  133. static bool _py_int__invert__(int argc, py_Ref argv) {
  134. py_checkargc(1);
  135. int64_t val = py_toint(&argv[0]);
  136. py_newint(py_retval(), ~val);
  137. return true;
  138. }
  139. static bool _py_int__bit_length(int argc, py_Ref argv) {
  140. py_checkargc(1);
  141. int64_t x = py_toint(py_arg(0));
  142. if(x < 0) x = -x;
  143. int bits = 0;
  144. while(x) {
  145. x >>= 1;
  146. bits++;
  147. }
  148. py_newint(py_retval(), bits);
  149. return true;
  150. }
  151. #define DEF_INT_BITWISE_OP(name, op) \
  152. static bool _py_int##name(int argc, py_Ref argv) { \
  153. py_checkargc(2); \
  154. int64_t lhs = py_toint(&argv[0]); \
  155. if(py_isint(&argv[1])) { \
  156. int64_t rhs = py_toint(&argv[1]); \
  157. py_newint(py_retval(), lhs op rhs); \
  158. } else { \
  159. py_newnotimplemented(py_retval()); \
  160. } \
  161. return true; \
  162. }
  163. DEF_INT_BITWISE_OP(__and__, &)
  164. DEF_INT_BITWISE_OP(__or__, |)
  165. DEF_INT_BITWISE_OP(__xor__, ^)
  166. DEF_INT_BITWISE_OP(__lshift__, <<)
  167. DEF_INT_BITWISE_OP(__rshift__, >>)
  168. #undef DEF_INT_BITWISE_OP
  169. static bool _py_int__repr__(int argc, py_Ref argv) {
  170. py_checkargc(1);
  171. int64_t val = py_toint(&argv[0]);
  172. char buf[32];
  173. int size = snprintf(buf, sizeof(buf), "%lld", (long long)val);
  174. py_newstrn(py_retval(), buf, size);
  175. return true;
  176. }
  177. static bool _py_float__repr__(int argc, py_Ref argv) {
  178. py_checkargc(1);
  179. double val = py_tofloat(&argv[0]);
  180. char buf[32];
  181. int size = snprintf(buf, sizeof(buf), "%f", val);
  182. py_newstrn(py_retval(), buf, size);
  183. return true;
  184. }
  185. union c11_8bytes {
  186. py_i64 _i64;
  187. py_f64 _f64;
  188. union {
  189. uint32_t upper;
  190. uint32_t lower;
  191. } bits;
  192. };
  193. static py_i64 c11_8bytes__hash(union c11_8bytes u) {
  194. // https://stackoverflow.com/questions/664014/what-integer-hash-function-are-good-that-accepts-an-integer-hash-key
  195. const uint32_t C = 2654435761;
  196. u.bits.upper *= C;
  197. u.bits.lower *= C;
  198. return u._i64;
  199. }
  200. static bool _py_int__hash__(int argc, py_Ref argv) {
  201. py_checkargc(1);
  202. int64_t val = py_toint(&argv[0]);
  203. union c11_8bytes u = {._i64 = val};
  204. py_newint(py_retval(), c11_8bytes__hash(u));
  205. return true;
  206. }
  207. static bool _py_float__hash__(int argc, py_Ref argv) {
  208. py_checkargc(1);
  209. double val = py_tofloat(&argv[0]);
  210. union c11_8bytes u = {._f64 = val};
  211. py_newint(py_retval(), c11_8bytes__hash(u));
  212. return true;
  213. }
  214. static bool _py_int__new__(int argc, py_Ref argv) {
  215. if(argc == 1 + 0) {
  216. // int() == 0
  217. py_newint(py_retval(), 0);
  218. return true;
  219. }
  220. // 1 arg
  221. if(argc == 1 + 1) {
  222. switch(argv[1].type) {
  223. case tp_float: {
  224. // int(1.1) == 1
  225. py_newint(py_retval(), (int64_t)py_tofloat(&argv[1]));
  226. return true;
  227. }
  228. case tp_int: {
  229. // int(1) == 1
  230. *py_retval() = argv[1];
  231. return true;
  232. }
  233. case tp_bool: {
  234. // int(True) == 1
  235. py_newint(py_retval(), (int64_t)py_tobool(&argv[1]));
  236. return true;
  237. }
  238. case tp_str: break; // leave to the next block
  239. default: return TypeError("invalid arguments for int()");
  240. }
  241. }
  242. // 2+ args -> error
  243. if(argc > 1 + 2) return TypeError("int() takes at most 2 arguments");
  244. // 1 or 2 args with str
  245. int base = 10;
  246. if(argc == 1 + 2) {
  247. if(!py_checktype(py_arg(2), tp_int)) return false;
  248. base = py_toint(py_arg(2));
  249. }
  250. if(!py_checktype(py_arg(1), tp_str)) return false;
  251. int size;
  252. const char* data = py_tostrn(py_arg(1), &size);
  253. bool negative = false;
  254. if(size && (data[0] == '+' || data[0] == '-')) {
  255. negative = data[0] == '-';
  256. data++;
  257. size--;
  258. }
  259. py_i64 val;
  260. if(c11__parse_uint((c11_sv){data, size}, &val, base) != IntParsing_SUCCESS) {
  261. return ValueError("invalid literal for int() with base %d: %q", base, data);
  262. }
  263. py_newint(py_retval(), negative ? -val : val);
  264. return true;
  265. }
  266. static bool _py_float__new__(int argc, py_Ref argv) {
  267. if(argc == 1 + 0) {
  268. // float() == 0.0
  269. py_newfloat(py_retval(), 0.0);
  270. return true;
  271. }
  272. if(argc > 1 + 1) return TypeError("float() takes at most 1 argument");
  273. // 1 arg
  274. switch(argv[1].type) {
  275. case tp_int: {
  276. // float(1) == 1.0
  277. py_newfloat(py_retval(), py_toint(&argv[1]));
  278. return true;
  279. }
  280. case tp_float: {
  281. // float(1.1) == 1.1
  282. *py_retval() = argv[1];
  283. return true;
  284. }
  285. case tp_bool: {
  286. // float(True) == 1.0
  287. py_newfloat(py_retval(), py_tobool(&argv[1]));
  288. return true;
  289. }
  290. case tp_str: break; // leave to the next block
  291. default: return TypeError("invalid arguments for float()");
  292. }
  293. // str to float
  294. int size;
  295. const char* data = py_tostrn(py_arg(1), &size);
  296. if(c11__streq(data, "inf")){
  297. py_newfloat(py_retval(), INFINITY);
  298. return true;
  299. }
  300. if(c11__streq(data, "-inf")){
  301. py_newfloat(py_retval(), -INFINITY);
  302. return true;
  303. }
  304. char* p_end;
  305. py_f64 float_out = strtod(data, &p_end);
  306. if(p_end != data + size){
  307. return ValueError("invalid literal for float(): %q", data);
  308. }
  309. py_newfloat(py_retval(), float_out);
  310. return true;
  311. }
  312. void pk_number__register() {
  313. /****** tp_int & tp_float ******/
  314. py_bindmagic(tp_int, __add__, _py_int__add__);
  315. py_bindmagic(tp_float, __add__, _py_float__add__);
  316. py_bindmagic(tp_int, __sub__, _py_int__sub__);
  317. py_bindmagic(tp_float, __sub__, _py_float__sub__);
  318. py_bindmagic(tp_int, __mul__, _py_int__mul__);
  319. py_bindmagic(tp_float, __mul__, _py_float__mul__);
  320. py_bindmagic(tp_int, __eq__, _py_int__eq__);
  321. py_bindmagic(tp_float, __eq__, _py_float__eq__);
  322. py_bindmagic(tp_int, __ne__, _py_int__ne__);
  323. py_bindmagic(tp_float, __ne__, _py_float__ne__);
  324. py_bindmagic(tp_int, __lt__, _py_int__lt__);
  325. py_bindmagic(tp_float, __lt__, _py_float__lt__);
  326. py_bindmagic(tp_int, __le__, _py_int__le__);
  327. py_bindmagic(tp_float, __le__, _py_float__le__);
  328. py_bindmagic(tp_int, __gt__, _py_int__gt__);
  329. py_bindmagic(tp_float, __gt__, _py_float__gt__);
  330. py_bindmagic(tp_int, __ge__, _py_int__ge__);
  331. py_bindmagic(tp_float, __ge__, _py_float__ge__);
  332. // __neg__
  333. py_bindmagic(tp_int, __neg__, _py_int__neg__);
  334. py_bindmagic(tp_float, __neg__, _py_float__neg__);
  335. // __repr__
  336. py_bindmagic(tp_int, __repr__, _py_int__repr__);
  337. py_bindmagic(tp_float, __repr__, _py_float__repr__);
  338. // __hash__
  339. py_bindmagic(tp_int, __hash__, _py_int__hash__);
  340. py_bindmagic(tp_float, __hash__, _py_float__hash__);
  341. // __new__
  342. py_bindmagic(tp_int, __new__, _py_int__new__);
  343. py_bindmagic(tp_float, __new__, _py_float__new__);
  344. // __truediv__
  345. py_bindmagic(tp_int, __truediv__, _py_int__truediv__);
  346. py_bindmagic(tp_float, __truediv__, _py_float__truediv__);
  347. // __pow__
  348. py_bindmagic(tp_int, __pow__, _py_number__pow__);
  349. py_bindmagic(tp_float, __pow__, _py_number__pow__);
  350. // __floordiv__ & __mod__
  351. py_bindmagic(tp_int, __floordiv__, _py_int__floordiv__);
  352. py_bindmagic(tp_int, __mod__, _py_int__mod__);
  353. // int.__invert__ & int.<BITWISE OP>
  354. py_bindmagic(tp_int, __invert__, _py_int__invert__);
  355. py_bindmagic(tp_int, __and__, _py_int__and__);
  356. py_bindmagic(tp_int, __or__, _py_int__or__);
  357. py_bindmagic(tp_int, __xor__, _py_int__xor__);
  358. py_bindmagic(tp_int, __lshift__, _py_int__lshift__);
  359. py_bindmagic(tp_int, __rshift__, _py_int__rshift__);
  360. // int.bit_length
  361. py_bindmethod(tp_int, "bit_length", _py_int__bit_length);
  362. }