py_list.c 13 KB

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