PyList.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. #include "pocketpy/pocketpy.h"
  2. #include "pocketpy/common/utils.h"
  3. #include "pocketpy/interpreter/vm.h"
  4. #include "pocketpy/interpreter/types.h"
  5. #include "pocketpy/objects/iterator.h"
  6. #include "pocketpy/common/sstream.h"
  7. void py_newlist(py_OutRef out) {
  8. List* ud = py_newobject(out, tp_list, 0, sizeof(List));
  9. c11_vector__ctor(ud, sizeof(py_TValue));
  10. }
  11. void py_newlistn(py_OutRef out, int n) {
  12. py_newlist(out);
  13. List* ud = py_touserdata(out);
  14. c11_vector__reserve(ud, n);
  15. ud->length = n;
  16. }
  17. py_Ref py_list_data(py_Ref self) {
  18. List* ud = py_touserdata(self);
  19. return ud->data;
  20. }
  21. py_Ref py_list_getitem(py_Ref self, int i) {
  22. List* ud = py_touserdata(self);
  23. return c11__at(py_TValue, ud, i);
  24. }
  25. void py_list_setitem(py_Ref self, int i, py_Ref val) {
  26. List* ud = py_touserdata(self);
  27. c11__setitem(py_TValue, ud, i, *val);
  28. }
  29. void py_list_delitem(py_Ref self, int i) {
  30. List* ud = py_touserdata(self);
  31. c11_vector__erase(py_TValue, ud, i);
  32. }
  33. int py_list_len(py_Ref self) {
  34. List* ud = py_touserdata(self);
  35. return ud->length;
  36. }
  37. void py_list_swap(py_Ref self, int i, int j) {
  38. py_TValue* data = py_list_data(self);
  39. py_TValue tmp = data[i];
  40. data[i] = data[j];
  41. data[j] = tmp;
  42. }
  43. void py_list_append(py_Ref self, py_Ref val) {
  44. List* ud = py_touserdata(self);
  45. c11_vector__push(py_TValue, ud, *val);
  46. }
  47. py_ItemRef py_list_emplace(py_Ref self) {
  48. List* ud = py_touserdata(self);
  49. c11_vector__emplace(ud);
  50. return &c11_vector__back(py_TValue, ud);
  51. }
  52. void py_list_clear(py_Ref self) {
  53. List* ud = py_touserdata(self);
  54. c11_vector__clear(ud);
  55. }
  56. void py_list_insert(py_Ref self, int i, py_Ref val) {
  57. List* ud = py_touserdata(self);
  58. c11_vector__insert(py_TValue, ud, i, *val);
  59. }
  60. ////////////////////////////////
  61. static bool list__len__(int argc, py_Ref argv) {
  62. PY_CHECK_ARGC(1);
  63. py_i64 res = py_list_len(py_arg(0));
  64. py_newint(py_retval(), res);
  65. return true;
  66. }
  67. static bool list__eq__(int argc, py_Ref argv) {
  68. return pk_wrapper__arrayequal(tp_list, argc, argv);
  69. }
  70. static bool list__ne__(int argc, py_Ref argv) {
  71. if(!list__eq__(argc, argv)) return false;
  72. if(py_isbool(py_retval())) {
  73. bool res = py_tobool(py_retval());
  74. py_newbool(py_retval(), !res);
  75. }
  76. return true;
  77. }
  78. static bool list__new__(int argc, py_Ref argv) {
  79. if(argc == 1) {
  80. py_newlist(py_retval());
  81. return true;
  82. }
  83. if(argc == 2) {
  84. py_TValue* p;
  85. int length = pk_arrayview(py_arg(1), &p);
  86. if(length != -1) {
  87. py_newlistn(py_retval(), length);
  88. for(int i = 0; i < length; i++) {
  89. py_list_setitem(py_retval(), i, p + i);
  90. }
  91. return true;
  92. }
  93. if(!py_iter(py_arg(1))) return false;
  94. py_Ref iter = py_pushtmp();
  95. py_Ref list = py_pushtmp();
  96. *iter = *py_retval();
  97. py_newlist(list);
  98. while(true) {
  99. int res = py_next(iter);
  100. if(res == -1) {
  101. py_shrink(2);
  102. return false;
  103. }
  104. if(!res) break;
  105. py_list_append(list, py_retval());
  106. }
  107. *py_retval() = *list;
  108. py_shrink(2);
  109. return true;
  110. }
  111. return TypeError("list() takes at most 1 argument");
  112. }
  113. static bool list__getitem__(int argc, py_Ref argv) {
  114. PY_CHECK_ARGC(2);
  115. List* self = py_touserdata(py_arg(0));
  116. py_Ref _1 = py_arg(1);
  117. if(_1->type == tp_int) {
  118. int index = py_toint(py_arg(1));
  119. if(!pk__normalize_index(&index, self->length)) return false;
  120. *py_retval() = c11__getitem(py_TValue, self, index);
  121. return true;
  122. } else if(_1->type == tp_slice) {
  123. int start, stop, step;
  124. bool ok = pk__parse_int_slice(_1, self->length, &start, &stop, &step);
  125. if(!ok) return false;
  126. py_newlist(py_retval());
  127. List* list = py_touserdata(py_retval());
  128. PK_SLICE_LOOP(i, start, stop, step) {
  129. c11_vector__push(py_TValue, list, c11__getitem(py_TValue, self, i));
  130. }
  131. return true;
  132. } else {
  133. return TypeError("list indices must be integers");
  134. }
  135. }
  136. static bool list__setitem__(int argc, py_Ref argv) {
  137. PY_CHECK_ARGC(3);
  138. PY_CHECK_ARG_TYPE(1, tp_int);
  139. List* self = py_touserdata(py_arg(0));
  140. int index = py_toint(py_arg(1));
  141. if(!pk__normalize_index(&index, self->length)) return false;
  142. c11__setitem(py_TValue, self, index, *py_arg(2));
  143. py_newnone(py_retval());
  144. return true;
  145. }
  146. static bool list__delitem__(int argc, py_Ref argv) {
  147. PY_CHECK_ARGC(2);
  148. List* self = py_touserdata(py_arg(0));
  149. if(py_istype(py_arg(1), tp_slice)) {
  150. int start, stop, step;
  151. bool ok = pk__parse_int_slice(py_arg(1), self->length, &start, &stop, &step);
  152. if(!ok) return false;
  153. if(step != 1) return ValueError("slice step must be 1 for deletion");
  154. int n = stop - start;
  155. if(n > 0) {
  156. py_TValue* p = self->data;
  157. for(int i = stop; i < self->length; i++) {
  158. p[start + i - stop] = p[i];
  159. }
  160. self->length -= n;
  161. }
  162. py_newnone(py_retval());
  163. return true;
  164. }
  165. PY_CHECK_ARG_TYPE(1, tp_int);
  166. int index = py_toint(py_arg(1));
  167. if(!pk__normalize_index(&index, self->length)) return false;
  168. c11_vector__erase(py_TValue, self, index);
  169. py_newnone(py_retval());
  170. return true;
  171. }
  172. static bool list__add__(int argc, py_Ref argv) {
  173. PY_CHECK_ARGC(2);
  174. py_Ref _0 = py_arg(0);
  175. py_Ref _1 = py_arg(1);
  176. if(py_istype(_1, tp_list)) {
  177. List* list_0 = py_touserdata(_0);
  178. List* list_1 = py_touserdata(_1);
  179. py_newlist(py_retval());
  180. List* list = py_touserdata(py_retval());
  181. c11_vector__extend(list, list_0->data, list_0->length);
  182. c11_vector__extend(list, list_1->data, list_1->length);
  183. } else {
  184. py_newnotimplemented(py_retval());
  185. }
  186. return true;
  187. }
  188. static bool list__mul__(int argc, py_Ref argv) {
  189. PY_CHECK_ARGC(2);
  190. py_Ref _0 = py_arg(0);
  191. py_Ref _1 = py_arg(1);
  192. if(py_istype(_1, tp_int)) {
  193. int n = py_toint(_1);
  194. py_newlist(py_retval());
  195. List* list = py_touserdata(py_retval());
  196. List* list_0 = py_touserdata(_0);
  197. for(int i = 0; i < n; i++) {
  198. c11_vector__extend(list, list_0->data, list_0->length);
  199. }
  200. } else {
  201. py_newnotimplemented(py_retval());
  202. }
  203. return true;
  204. }
  205. static bool list__rmul__(int argc, py_Ref argv) { return list__mul__(argc, argv); }
  206. static bool list_append(int argc, py_Ref argv) {
  207. PY_CHECK_ARGC(2);
  208. py_list_append(py_arg(0), py_arg(1));
  209. py_newnone(py_retval());
  210. return true;
  211. }
  212. static bool list__repr__(int argc, py_Ref argv) {
  213. List* self = py_touserdata(py_arg(0));
  214. c11_sbuf buf;
  215. c11_sbuf__ctor(&buf);
  216. c11_sbuf__write_char(&buf, '[');
  217. for(int i = 0; i < self->length; i++) {
  218. py_TValue* val = c11__at(py_TValue, self, i);
  219. bool ok = py_repr(val);
  220. if(!ok) {
  221. c11_sbuf__dtor(&buf);
  222. return false;
  223. }
  224. c11_sbuf__write_sv(&buf, py_tosv(py_retval()));
  225. if(i != self->length - 1) c11_sbuf__write_cstr(&buf, ", ");
  226. }
  227. c11_sbuf__write_char(&buf, ']');
  228. c11_sbuf__py_submit(&buf, py_retval());
  229. return true;
  230. }
  231. static bool list_extend(int argc, py_Ref argv) {
  232. PY_CHECK_ARGC(2);
  233. List* self = py_touserdata(py_arg(0));
  234. py_TValue* p;
  235. int length = pk_arrayview(py_arg(1), &p);
  236. if(length >= 0) {
  237. c11_vector__extend(self, p, length);
  238. } else {
  239. // get iterator
  240. if (!py_iter(py_arg(1))) return false;
  241. py_StackRef tmp_iter = py_pushtmp();
  242. py_assign(tmp_iter, py_retval());
  243. while(true) {
  244. int res = py_next(tmp_iter);
  245. if (res == 0) break;
  246. if (res == -1) return false;
  247. assert(res == 1);
  248. c11_vector__push(py_TValue, self, *py_retval());
  249. }
  250. py_pop();
  251. }
  252. py_newnone(py_retval());
  253. return true;
  254. }
  255. static bool list_count(int argc, py_Ref argv) {
  256. PY_CHECK_ARGC(2);
  257. int count = 0;
  258. for(int i = 0; i < py_list_len(py_arg(0)); i++) {
  259. int res = py_equal(py_list_getitem(py_arg(0), i), py_arg(1));
  260. if(res == -1) return false;
  261. if(res) count++;
  262. }
  263. py_newint(py_retval(), count);
  264. return true;
  265. }
  266. static bool list_clear(int argc, py_Ref argv) {
  267. PY_CHECK_ARGC(1);
  268. py_list_clear(py_arg(0));
  269. py_newnone(py_retval());
  270. return true;
  271. }
  272. static bool list_copy(int argc, py_Ref argv) {
  273. PY_CHECK_ARGC(1);
  274. py_newlist(py_retval());
  275. List* self = py_touserdata(py_arg(0));
  276. List* list = py_touserdata(py_retval());
  277. c11_vector__extend(list, self->data, self->length);
  278. return true;
  279. }
  280. static bool list_index(int argc, py_Ref argv) {
  281. if(argc > 3) return TypeError("index() takes at most 3 arguments");
  282. int start = 0;
  283. if(argc == 3) {
  284. PY_CHECK_ARG_TYPE(2, tp_int);
  285. start = py_toint(py_arg(2));
  286. if(start < 0) start += py_list_len(py_arg(0));
  287. if(start < 0) start = 0;
  288. }
  289. for(int i = start; i < py_list_len(py_arg(0)); i++) {
  290. int res = py_equal(py_list_getitem(py_arg(0), i), py_arg(1));
  291. if(res == -1) return false;
  292. if(res) {
  293. py_newint(py_retval(), i);
  294. return true;
  295. }
  296. }
  297. return ValueError("list.index(x): x not in list");
  298. }
  299. static bool list_reverse(int argc, py_Ref argv) {
  300. PY_CHECK_ARGC(1);
  301. List* self = py_touserdata(py_arg(0));
  302. c11__reverse(py_TValue, self);
  303. py_newnone(py_retval());
  304. return true;
  305. }
  306. static bool list_remove(int argc, py_Ref argv) {
  307. PY_CHECK_ARGC(2);
  308. for(int i = 0; i < py_list_len(py_arg(0)); i++) {
  309. int res = py_equal(py_list_getitem(py_arg(0), i), py_arg(1));
  310. if(res == -1) return false;
  311. if(res) {
  312. py_list_delitem(py_arg(0), i);
  313. py_newnone(py_retval());
  314. return true;
  315. }
  316. }
  317. return ValueError("list.remove(x): x not in list");
  318. }
  319. static bool list_pop(int argc, py_Ref argv) {
  320. int index;
  321. if(argc == 1) {
  322. index = -1;
  323. } else if(argc == 2) {
  324. PY_CHECK_ARG_TYPE(1, tp_int);
  325. index = py_toint(py_arg(1));
  326. } else {
  327. return TypeError("pop() takes at most 2 arguments");
  328. }
  329. List* self = py_touserdata(py_arg(0));
  330. if(self->length == 0) return IndexError("pop from empty list");
  331. if(!pk__normalize_index(&index, self->length)) return false;
  332. *py_retval() = c11__getitem(py_TValue, self, index);
  333. c11_vector__erase(py_TValue, self, index);
  334. return true;
  335. }
  336. static bool list_insert(int argc, py_Ref argv) {
  337. PY_CHECK_ARGC(3);
  338. PY_CHECK_ARG_TYPE(1, tp_int);
  339. List* self = py_touserdata(py_arg(0));
  340. int index = py_toint(py_arg(1));
  341. if(index < 0) index += self->length;
  342. if(index < 0) index = 0;
  343. if(index > self->length) index = self->length;
  344. c11_vector__insert(py_TValue, self, index, *py_arg(2));
  345. py_newnone(py_retval());
  346. return true;
  347. }
  348. static int lt_with_key(const void* a_, const void* b_, void* extra) {
  349. py_TValue* a = (py_TValue*)a_;
  350. py_TValue* b = (py_TValue*)b_;
  351. py_TValue* key = (py_TValue*)extra;
  352. if(!key) return py_less(a, b);
  353. VM* vm = pk_current_vm;
  354. // project a
  355. py_push(key);
  356. py_pushnil();
  357. py_push(a);
  358. if(!py_vectorcall(1, 0)) return -1;
  359. py_push(py_retval());
  360. // project b
  361. py_push(key);
  362. py_pushnil();
  363. py_push(b);
  364. if(!py_vectorcall(1, 0)) return -1;
  365. py_push(py_retval());
  366. // binary op
  367. bool ok = pk_stack_binaryop(vm, __lt__, __gt__);
  368. if(!ok) return -1;
  369. py_shrink(2);
  370. return py_bool(py_retval());
  371. }
  372. // sort(self, key=None, reverse=False)
  373. static bool list_sort(int argc, py_Ref argv) {
  374. List* self = py_touserdata(py_arg(0));
  375. py_Ref key = py_arg(1);
  376. if(py_isnone(key)) key = NULL;
  377. bool ok = c11__stable_sort(self->data,
  378. self->length,
  379. sizeof(py_TValue),
  380. lt_with_key,
  381. key);
  382. if(!ok) return false;
  383. PY_CHECK_ARG_TYPE(2, tp_bool);
  384. bool reverse = py_tobool(py_arg(2));
  385. if(reverse) c11__reverse(py_TValue, self);
  386. py_newnone(py_retval());
  387. return true;
  388. }
  389. static bool list__iter__(int argc, py_Ref argv) {
  390. PY_CHECK_ARGC(1);
  391. list_iterator* ud = py_newobject(py_retval(), tp_list_iterator, 1, sizeof(list_iterator));
  392. ud->vec = py_touserdata(argv);
  393. ud->index = 0;
  394. py_setslot(py_retval(), 0, argv); // keep a reference to the object
  395. return true;
  396. }
  397. static bool list__contains__(int argc, py_Ref argv) {
  398. PY_CHECK_ARGC(2);
  399. return pk_arraycontains(py_arg(0), py_arg(1));
  400. }
  401. py_Type pk_list__register() {
  402. py_Type type =
  403. pk_newtype("list", tp_object, NULL, (void (*)(void*))c11_vector__dtor, false, true);
  404. py_bindmagic(type, __len__, list__len__);
  405. py_bindmagic(type, __eq__, list__eq__);
  406. py_bindmagic(type, __ne__, list__ne__);
  407. py_bindmagic(type, __new__, list__new__);
  408. py_bindmagic(type, __getitem__, list__getitem__);
  409. py_bindmagic(type, __setitem__, list__setitem__);
  410. py_bindmagic(type, __delitem__, list__delitem__);
  411. py_bindmagic(type, __add__, list__add__);
  412. py_bindmagic(type, __mul__, list__mul__);
  413. py_bindmagic(type, __rmul__, list__rmul__);
  414. py_bindmagic(type, __repr__, list__repr__);
  415. py_bindmagic(type, __iter__, list__iter__);
  416. py_bindmagic(type, __contains__, list__contains__);
  417. py_bindmethod(type, "append", list_append);
  418. py_bindmethod(type, "extend", list_extend);
  419. py_bindmethod(type, "count", list_count);
  420. py_bindmethod(type, "clear", list_clear);
  421. py_bindmethod(type, "copy", list_copy);
  422. py_bindmethod(type, "index", list_index);
  423. py_bindmethod(type, "reverse", list_reverse);
  424. py_bindmethod(type, "remove", list_remove);
  425. py_bindmethod(type, "pop", list_pop);
  426. py_bindmethod(type, "insert", list_insert);
  427. py_bindmethod(type, "sort", list_sort);
  428. py_bind(py_tpobject(type), "sort(self, key=None, reverse=False)", list_sort);
  429. py_setdict(py_tpobject(type), __hash__, py_None());
  430. return type;
  431. }