py_list.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. #include "pocketpy/pocketpy.h"
  2. #include "pocketpy/common/utils.h"
  3. #include "pocketpy/objects/object.h"
  4. #include "pocketpy/interpreter/vm.h"
  5. #include "pocketpy/common/sstream.h"
  6. typedef c11_vector List;
  7. void py_newlist(py_Ref 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_Ref out, int n) {
  12. py_newlist(out);
  13. List* userdata = py_touserdata(out);
  14. c11_vector__reserve(userdata, n);
  15. userdata->count = n;
  16. }
  17. py_Ref py_list__data(py_Ref self) {
  18. List* userdata = py_touserdata(self);
  19. return userdata->data;
  20. }
  21. py_Ref py_list__getitem(py_Ref self, int i) {
  22. List* userdata = py_touserdata(self);
  23. return c11__at(py_TValue, userdata, i);
  24. }
  25. void py_list__setitem(py_Ref self, int i, py_Ref val) {
  26. List* userdata = py_touserdata(self);
  27. c11__setitem(py_TValue, userdata, i, *val);
  28. }
  29. void py_list__delitem(py_Ref self, int i) {
  30. List* userdata = py_touserdata(self);
  31. c11_vector__erase(py_TValue, userdata, i);
  32. }
  33. int py_list__len(py_Ref self) {
  34. List* userdata = py_touserdata(self);
  35. return userdata->count;
  36. }
  37. void py_list__append(py_Ref self, py_Ref val) {
  38. List* userdata = py_touserdata(self);
  39. c11_vector__push(py_TValue, userdata, *val);
  40. }
  41. void py_list__clear(py_Ref self) {
  42. List* userdata = py_touserdata(self);
  43. c11_vector__clear(userdata);
  44. }
  45. void py_list__insert(py_Ref self, int i, py_Ref val) {
  46. List* userdata = py_touserdata(self);
  47. c11_vector__insert(py_TValue, userdata, i, *val);
  48. }
  49. void py_list__reverse(py_Ref self) {
  50. List* userdata = py_touserdata(self);
  51. c11__reverse(py_TValue, userdata);
  52. }
  53. ////////////////////////////////
  54. static bool _py_list__len__(int argc, py_Ref argv) {
  55. PY_CHECK_ARGC(1);
  56. py_i64 res = py_list__len(py_arg(0));
  57. py_newint(py_retval(), res);
  58. return true;
  59. }
  60. static bool _py_list__eq__(int argc, py_Ref argv) {
  61. PY_CHECK_ARGC(2);
  62. if(py_istype(py_arg(1), tp_list)) {
  63. int length0, length1;
  64. py_TValue* a0 = pk_arrayview(py_arg(0), &length0);
  65. py_TValue* a1 = pk_arrayview(py_arg(1), &length1);
  66. int res = pk_arrayequal(a0, length0, a1, length1);
  67. if(res == -1) return false;
  68. py_newbool(py_retval(), res);
  69. } else {
  70. py_newnotimplemented(py_retval());
  71. }
  72. return true;
  73. }
  74. static bool _py_list__ne__(int argc, py_Ref argv) {
  75. if(!_py_list__eq__(argc, argv)) return false;
  76. if(py_isbool(py_retval())) {
  77. bool res = py_tobool(py_retval());
  78. py_newbool(py_retval(), !res);
  79. }
  80. return true;
  81. }
  82. static bool _py_list__new__(int argc, py_Ref argv) {
  83. if(argc == 1) {
  84. py_newlist(py_retval());
  85. return true;
  86. }
  87. if(argc == 2) {
  88. int length;
  89. py_TValue* p = pk_arrayview(py_arg(1), &length);
  90. if(p) {
  91. py_newlistn(py_retval(), length);
  92. for(int i = 0; i < length; i++) {
  93. py_list__setitem(py_retval(), i, p + i);
  94. }
  95. return true;
  96. }
  97. if(!py_iter(py_arg(1))) return false;
  98. py_Ref iter = py_pushtmp();
  99. py_Ref list = py_pushtmp();
  100. *iter = *py_retval();
  101. py_newlist(list);
  102. while(true) {
  103. int res = py_next(iter);
  104. if(res == -1){
  105. py_shrink(2);
  106. return false;
  107. }
  108. if(!res) break;
  109. py_list__append(list, py_retval());
  110. }
  111. *py_retval() = *list;
  112. py_shrink(2);
  113. return true;
  114. }
  115. return TypeError("list() takes at most 1 argument");
  116. }
  117. static bool _py_list__getitem__(int argc, py_Ref argv) {
  118. PY_CHECK_ARGC(2);
  119. List* self = py_touserdata(py_arg(0));
  120. py_Ref _1 = py_arg(1);
  121. if(_1->type == tp_int) {
  122. int index = py_toint(py_arg(1));
  123. if(!pk__normalize_index(&index, self->count)) return false;
  124. *py_retval() = c11__getitem(py_TValue, self, index);
  125. return true;
  126. } else if(_1->type == tp_slice) {
  127. int start, stop, step;
  128. bool ok = pk__parse_int_slice(_1, self->count, &start, &stop, &step);
  129. if(!ok) return false;
  130. py_newlist(py_retval());
  131. List* list = py_touserdata(py_retval());
  132. PK_SLICE_LOOP(i, start, stop, step) {
  133. c11_vector__push(py_TValue, list, c11__getitem(py_TValue, self, i));
  134. }
  135. return true;
  136. } else {
  137. return TypeError("list indices must be integers");
  138. }
  139. }
  140. static bool _py_list__setitem__(int argc, py_Ref argv) {
  141. PY_CHECK_ARGC(3);
  142. PY_CHECK_ARG_TYPE(1, tp_int);
  143. List* self = py_touserdata(py_arg(0));
  144. int index = py_toint(py_arg(1));
  145. if(!pk__normalize_index(&index, self->count)) return false;
  146. c11__setitem(py_TValue, self, index, *py_arg(2));
  147. return true;
  148. }
  149. static bool _py_list__delitem__(int argc, py_Ref argv) {
  150. PY_CHECK_ARGC(2);
  151. PY_CHECK_ARG_TYPE(1, tp_int);
  152. List* self = py_touserdata(py_arg(0));
  153. int index = py_toint(py_arg(1));
  154. if(!pk__normalize_index(&index, self->count)) return false;
  155. c11_vector__erase(py_TValue, self, index);
  156. py_newnone(py_retval());
  157. return true;
  158. }
  159. static bool _py_list__add__(int argc, py_Ref argv) {
  160. PY_CHECK_ARGC(2);
  161. py_Ref _0 = py_arg(0);
  162. py_Ref _1 = py_arg(1);
  163. if(py_istype(_1, tp_list)) {
  164. List* list_0 = py_touserdata(_0);
  165. List* list_1 = py_touserdata(_1);
  166. py_newlist(py_retval());
  167. List* list = py_touserdata(py_retval());
  168. c11_vector__extend(py_TValue, list, list_0->data, list_0->count);
  169. c11_vector__extend(py_TValue, list, list_1->data, list_1->count);
  170. } else {
  171. py_newnotimplemented(py_retval());
  172. }
  173. return true;
  174. }
  175. static bool _py_list__mul__(int argc, py_Ref argv) {
  176. PY_CHECK_ARGC(2);
  177. py_Ref _0 = py_arg(0);
  178. py_Ref _1 = py_arg(1);
  179. if(py_istype(_1, tp_int)) {
  180. int n = py_toint(_1);
  181. py_newlist(py_retval());
  182. List* list = py_touserdata(py_retval());
  183. List* list_0 = py_touserdata(_0);
  184. for(int i = 0; i < n; i++) {
  185. c11_vector__extend(py_TValue, list, list_0->data, list_0->count);
  186. }
  187. } else {
  188. py_newnotimplemented(py_retval());
  189. }
  190. return true;
  191. }
  192. static bool _py_list__rmul__(int argc, py_Ref argv) { return _py_list__mul__(argc, argv); }
  193. static bool _py_list__append(int argc, py_Ref argv) {
  194. PY_CHECK_ARGC(2);
  195. py_list__append(py_arg(0), py_arg(1));
  196. py_newnone(py_retval());
  197. return true;
  198. }
  199. static bool _py_list__repr__(int argc, py_Ref argv) {
  200. List* self = py_touserdata(py_arg(0));
  201. c11_sbuf buf;
  202. c11_sbuf__ctor(&buf);
  203. c11_sbuf__write_char(&buf, '[');
  204. for(int i = 0; i < self->count; i++) {
  205. py_TValue* val = c11__at(py_TValue, self, i);
  206. bool ok = py_repr(val);
  207. if(!ok) {
  208. c11_sbuf__dtor(&buf);
  209. return false;
  210. }
  211. c11_sbuf__write_sv(&buf, py_tosv(py_retval()));
  212. if(i != self->count - 1) c11_sbuf__write_cstr(&buf, ", ");
  213. }
  214. c11_sbuf__write_char(&buf, ']');
  215. c11_sbuf__py_submit(&buf, py_retval());
  216. return true;
  217. }
  218. static bool _py_list__extend(int argc, py_Ref argv) {
  219. PY_CHECK_ARGC(2);
  220. List* self = py_touserdata(py_arg(0));
  221. PY_CHECK_ARG_TYPE(1, tp_list);
  222. List* other = py_touserdata(py_arg(1));
  223. c11_vector__extend(py_TValue, self, other->data, other->count);
  224. py_newnone(py_retval());
  225. return true;
  226. }
  227. static bool _py_list__count(int argc, py_Ref argv) {
  228. PY_CHECK_ARGC(2);
  229. int count = 0;
  230. for(int i = 0; i < py_list__len(py_arg(0)); i++) {
  231. int res = py_equal(py_list__getitem(py_arg(0), i), py_arg(1));
  232. if(res == -1) return false;
  233. if(res) count++;
  234. }
  235. py_newint(py_retval(), count);
  236. return true;
  237. }
  238. static bool _py_list__clear(int argc, py_Ref argv) {
  239. PY_CHECK_ARGC(1);
  240. py_list__clear(py_arg(0));
  241. py_newnone(py_retval());
  242. return true;
  243. }
  244. static bool _py_list__copy(int argc, py_Ref argv) {
  245. PY_CHECK_ARGC(1);
  246. py_newlist(py_retval());
  247. List* self = py_touserdata(py_arg(0));
  248. List* list = py_touserdata(py_retval());
  249. c11_vector__extend(py_TValue, list, self->data, self->count);
  250. return true;
  251. }
  252. static bool _py_list__index(int argc, py_Ref argv) {
  253. if(argc > 3) return TypeError("index() takes at most 3 arguments");
  254. int start = 0;
  255. if(argc == 3) {
  256. PY_CHECK_ARG_TYPE(2, tp_int);
  257. start = py_toint(py_arg(2));
  258. }
  259. for(int i = start; i < py_list__len(py_arg(0)); i++) {
  260. int res = py_equal(py_list__getitem(py_arg(0), i), py_arg(1));
  261. if(res == -1) return false;
  262. if(res) {
  263. py_newint(py_retval(), i);
  264. return true;
  265. }
  266. }
  267. return ValueError("list.index(x): x not in list");
  268. }
  269. static bool _py_list__reverse(int argc, py_Ref argv) {
  270. PY_CHECK_ARGC(1);
  271. List* self = py_touserdata(py_arg(0));
  272. c11__reverse(py_TValue, self);
  273. py_newnone(py_retval());
  274. return true;
  275. }
  276. static bool _py_list__remove(int argc, py_Ref argv) {
  277. PY_CHECK_ARGC(2);
  278. for(int i = 0; i < py_list__len(py_arg(0)); i++) {
  279. int res = py_equal(py_list__getitem(py_arg(0), i), py_arg(1));
  280. if(res == -1) return false;
  281. if(res) {
  282. py_list__delitem(py_arg(0), i);
  283. py_newnone(py_retval());
  284. return true;
  285. }
  286. }
  287. return ValueError("list.remove(x): x not in list");
  288. }
  289. static bool _py_list__pop(int argc, py_Ref argv) {
  290. int index;
  291. if(argc == 1) {
  292. index = -1;
  293. } else if(argc == 2) {
  294. PY_CHECK_ARG_TYPE(1, tp_int);
  295. index = py_toint(py_arg(1));
  296. } else {
  297. return TypeError("pop() takes at most 2 arguments");
  298. }
  299. List* self = py_touserdata(py_arg(0));
  300. if(self->count == 0) return IndexError("pop from empty list");
  301. if(!pk__normalize_index(&index, self->count)) return false;
  302. *py_retval() = c11__getitem(py_TValue, self, index);
  303. c11_vector__erase(py_TValue, self, index);
  304. return true;
  305. }
  306. static bool _py_list__insert(int argc, py_Ref argv) {
  307. PY_CHECK_ARGC(3);
  308. PY_CHECK_ARG_TYPE(1, tp_int);
  309. List* self = py_touserdata(py_arg(0));
  310. int index = py_toint(py_arg(1));
  311. if(index < 0) index += self->count;
  312. if(index < 0) index = 0;
  313. if(index > self->count) index = self->count;
  314. c11_vector__insert(py_TValue, self, index, *py_arg(2));
  315. py_newnone(py_retval());
  316. return true;
  317. }
  318. static int _py_lt_with_key(py_TValue* a, py_TValue* b, py_TValue* key) {
  319. if(!key) return py_less(a, b);
  320. pk_VM* vm = pk_current_vm;
  321. // project a
  322. py_push(key);
  323. py_pushnil();
  324. py_push(a);
  325. if(!py_vectorcall(1, 0)) return -1;
  326. py_push(py_retval());
  327. // project b
  328. py_push(key);
  329. py_pushnil();
  330. py_push(b);
  331. if(!py_vectorcall(1, 0)) return -1;
  332. py_push(py_retval());
  333. // binary op
  334. bool ok = pk_stack_binaryop(vm, __lt__, __gt__);
  335. if(!ok) return -1;
  336. py_shrink(2);
  337. return py_bool(py_retval());
  338. }
  339. // sort(self, key=None, reverse=False)
  340. static bool _py_list__sort(int argc, py_Ref argv) {
  341. List* self = py_touserdata(py_arg(0));
  342. py_Ref key = py_arg(1);
  343. if(py_isnone(key)) key = NULL;
  344. bool ok = c11__stable_sort(self->data,
  345. self->count,
  346. sizeof(py_TValue),
  347. (int (*)(const void*, const void*, void*))_py_lt_with_key,
  348. key);
  349. if(!ok) return false;
  350. PY_CHECK_ARG_TYPE(2, tp_bool);
  351. bool reverse = py_tobool(py_arg(2));
  352. if(reverse) c11__reverse(py_TValue, self);
  353. py_newnone(py_retval());
  354. return true;
  355. }
  356. static bool _py_list__iter__(int argc, py_Ref argv) {
  357. PY_CHECK_ARGC(1);
  358. return pk_arrayiter(argv);
  359. }
  360. static bool _py_list__contains__(int argc, py_Ref argv) {
  361. PY_CHECK_ARGC(2);
  362. return pk_arraycontains(py_arg(0), py_arg(1));
  363. }
  364. py_Type pk_list__register() {
  365. py_Type type =
  366. pk_newtype("list", tp_object, NULL, (void (*)(void*))c11_vector__dtor, false, true);
  367. py_bindmagic(type, __len__, _py_list__len__);
  368. py_bindmagic(type, __eq__, _py_list__eq__);
  369. py_bindmagic(type, __ne__, _py_list__ne__);
  370. py_bindmagic(type, __new__, _py_list__new__);
  371. py_bindmagic(type, __getitem__, _py_list__getitem__);
  372. py_bindmagic(type, __setitem__, _py_list__setitem__);
  373. py_bindmagic(type, __delitem__, _py_list__delitem__);
  374. py_bindmagic(type, __add__, _py_list__add__);
  375. py_bindmagic(type, __mul__, _py_list__mul__);
  376. py_bindmagic(type, __rmul__, _py_list__rmul__);
  377. py_bindmagic(type, __repr__, _py_list__repr__);
  378. py_bindmagic(type, __iter__, _py_list__iter__);
  379. py_bindmagic(type, __contains__, _py_list__contains__);
  380. py_bindmethod(type, "append", _py_list__append);
  381. py_bindmethod(type, "extend", _py_list__extend);
  382. py_bindmethod(type, "count", _py_list__count);
  383. py_bindmethod(type, "clear", _py_list__clear);
  384. py_bindmethod(type, "copy", _py_list__copy);
  385. py_bindmethod(type, "index", _py_list__index);
  386. py_bindmethod(type, "reverse", _py_list__reverse);
  387. py_bindmethod(type, "remove", _py_list__remove);
  388. py_bindmethod(type, "pop", _py_list__pop);
  389. py_bindmethod(type, "insert", _py_list__insert);
  390. py_bindmethod(type, "sort", _py_list__sort);
  391. py_bind(py_tpobject(type), "sort(self, key=None, reverse=False)", _py_list__sort);
  392. py_setdict(py_tpobject(type), __hash__, py_None);
  393. return type;
  394. }
  395. void pk_list__mark(void* ud, void (*marker)(py_TValue*)) {
  396. List* self = ud;
  397. for(int i = 0; i < self->count; i++) {
  398. marker(c11__at(py_TValue, self, i));
  399. }
  400. }