1
0

py_number.c 16 KB

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