cons.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /* Declarations for the cons type.
  2. This file is part of khipu.
  3. khipu is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #ifndef __KP_CONS__
  14. #define __KP_CONS__ 1
  15. #include "interp.hpp"
  16. #include "initop.hpp"
  17. KP_DECLS_BEGIN
  18. /* Conses do not possess a header, and pointers to conses are
  19. * marked specially as well. This means, on the other hand,
  20. * that they work a bit differently than other objects. */
  21. struct alignas (8) cons
  22. {
  23. static const int code = typecode::CONS;
  24. object car;
  25. object cdr;
  26. object as_obj () const
  27. {
  28. #ifdef KP_ARCH_WIDE
  29. return (ptrtype (this, typecode::CONS));
  30. #else
  31. return ((object)this | 2);
  32. #endif
  33. }
  34. static result<object> make (interpreter *interp, object car, object cdr);
  35. struct iter_base
  36. {
  37. int state = 0;
  38. inline object _Advance (object& fast, object& slow);
  39. };
  40. struct unsafe_iter : public iter_base
  41. {
  42. object fast;
  43. object slow;
  44. unsafe_iter (object cons) : fast (cons), slow (cons)
  45. {
  46. }
  47. inline bool valid () const;
  48. inline object& operator* ();
  49. inline object operator* () const;
  50. bool circular () const
  51. {
  52. return (this->fast == UNBOUND);
  53. }
  54. unsafe_iter& operator++ ()
  55. {
  56. this->_Advance (this->fast, this->slow);
  57. return (*this);
  58. }
  59. unsafe_iter operator++ (int)
  60. {
  61. unsafe_iter rv = *this;
  62. ++*this;
  63. return (rv);
  64. }
  65. object node () const
  66. {
  67. return (this->fast);
  68. }
  69. object prev_node () const
  70. {
  71. return (this->slow);
  72. }
  73. };
  74. struct safe_iter : public iter_base
  75. {
  76. valref fast;
  77. valref slow;
  78. valref value;
  79. inline safe_iter (interpreter *interp, object cons);
  80. safe_iter (interpreter *interp, const safe_iter& right) :
  81. fast (interp, *right.fast), slow (interp, *right.slow),
  82. value (interp, *right.value)
  83. {
  84. }
  85. void reset (const safe_iter& right)
  86. {
  87. *this->slow = *right.slow;
  88. *this->fast = *right.fast;
  89. *this->value = *right.value;
  90. }
  91. inline bool valid () const;
  92. inline object operator* () const;
  93. bool circular () const
  94. {
  95. return (*this->fast == UNBOUND);
  96. }
  97. safe_iter& operator++ ()
  98. {
  99. *this->value = this->_Advance (*this->fast, *this->slow);
  100. return (*this);
  101. }
  102. safe_iter operator++ (int)
  103. {
  104. safe_iter rv { interpreter::self (), *this };
  105. ++*this;
  106. return (rv);
  107. }
  108. object node () const
  109. {
  110. return (*this->fast);
  111. }
  112. object prev_node () const
  113. {
  114. return (*this->slow);
  115. }
  116. };
  117. typedef safe_iter iterator;
  118. // GC-specific methods.
  119. bool gc_mark ();
  120. bool old_gen_p () const;
  121. };
  122. static_assert (alignof (cons) % 8 == 0 && sizeof (cons) == 2 * sizeof (object),
  123. "invalid alignment/size for conses");
  124. KP_EXPORT const object NIL;
  125. #ifdef KP_ARCH_WIDE
  126. inline constexpr bool xcons_p (object obj)
  127. {
  128. return (itype (obj) == typecode::CONS);
  129. }
  130. #else
  131. inline constexpr bool xcons_p (object obj)
  132. {
  133. return ((obj & 3) == 2);
  134. }
  135. #endif
  136. inline bool cons_p (object obj)
  137. {
  138. return (obj != NIL && xcons_p (obj));
  139. }
  140. inline bool atom_p (object obj)
  141. {
  142. return (!cons_p (obj));
  143. }
  144. inline cons* as_cons (object obj)
  145. {
  146. return ((cons *)unmask (obj));
  147. }
  148. // Fast, unsafe access.
  149. inline object& xcar (object obj)
  150. {
  151. return (as_cons(obj)->car);
  152. }
  153. inline object& xcdr (object obj)
  154. {
  155. return (as_cons(obj)->cdr);
  156. }
  157. inline object& xcadr (object obj)
  158. {
  159. return (xcar (xcdr (obj)));
  160. }
  161. inline object& xcddr (object obj)
  162. {
  163. return (xcdr (xcdr (obj)));
  164. }
  165. object cons::iter_base::_Advance (object& fast, object& slow)
  166. {
  167. fast = xcdr (fast);
  168. if (fast == slow)
  169. fast = UNBOUND;
  170. else if (this->state == 1)
  171. slow = xcdr (slow);
  172. this->state ^= 1;
  173. return (xcons_p (fast) ? xcar (fast) : UNBOUND);
  174. }
  175. bool cons::unsafe_iter::valid () const
  176. {
  177. return (cons_p (this->fast));
  178. }
  179. object& cons::unsafe_iter::operator* ()
  180. {
  181. return (xcar (this->fast));
  182. }
  183. object cons::unsafe_iter::operator* () const
  184. {
  185. return (xcar (this->fast));
  186. }
  187. object cons::safe_iter::operator* () const
  188. {
  189. return (xcar (*this->fast));
  190. }
  191. cons::safe_iter::safe_iter (interpreter *interp, object cns) :
  192. fast (interp, cns), slow (interp, cns), value (interp, xcar (cns))
  193. {
  194. }
  195. bool cons::safe_iter::valid () const
  196. {
  197. return (cons_p (*this->fast));
  198. }
  199. // Allocate a single cons.
  200. KP_EXPORT result<object> alloc_cons (interpreter *interp);
  201. /* Allocate N conses, filling each of their CARs with FILL. If non-null,
  202. * store the last CDR in *TAIL. */
  203. KP_EXPORT result<object> alloc_cons (interpreter *interp, uint32_t n,
  204. object fill = 0, object **tail = nullptr);
  205. // Same as above, only this fill the CARs with the elements in *ARGV.
  206. KP_EXPORT result<object> alloc_cons (interpreter *interp, uint32_t n,
  207. object *argv, object **tail);
  208. // Return the length of the list CONS. Place its tail in DOTC.
  209. KP_EXPORT int32_t len_L (interpreter *interp, object cons, object& dotc);
  210. // Same as above, only it raises an exception if CONS is not a proper list.
  211. KP_EXPORT result<int32_t> len_L (interpreter *interp, object cons);
  212. // Index a list.
  213. KP_EXPORT result<object> get_L (interpreter *interp,
  214. object cons, object idx, object dfl);
  215. // Get the subsequence of a list.
  216. KP_EXPORT result<object> subseq_L (interpreter *interp,
  217. object cons, object i1, object i2);
  218. // Destructively set an object inside a list.
  219. KP_EXPORT result<object> nput_L (interpreter *interp,
  220. object cons, object idx, object val);
  221. // Copy a list.
  222. KP_EXPORT result<object> copy_L (interpreter *interp, object obj, bool deep);
  223. // Concatenate lists L1 and L2.
  224. KP_EXPORT result<object> add_LL (interpreter *interp, object l1, object l2);
  225. // Concate ARGC lists inside ARGV.
  226. KP_EXPORT result<object> concat_L (interpreter *interp, object *argv, int argc);
  227. // Compute the hashcode of a list.
  228. KP_EXPORT result<uint32_t> hash_L (interpreter *interp, object obj);
  229. // Iterator interface for a list.
  230. KP_EXPORT result<object> iter_L (interpreter *interp,
  231. object obj, object token, bool adv);
  232. // Write a list to a stream.
  233. KP_EXPORT result<int64_t> write_L (interpreter *interp,
  234. stream *strm, object obj, io_info& info);
  235. // Serialize a list in a stream.
  236. KP_EXPORT result<int64_t> pack_L (interpreter *interp,
  237. stream *strm, object obj, pack_info& info);
  238. // Deserialize a list from a stream.
  239. KP_EXPORT result<object> unpack_L (interpreter *interp,
  240. stream *strm, pack_info& info, bool save);
  241. // Reverse a list.
  242. KP_EXPORT result<object> reverse_L (interpreter *interp, object obj);
  243. // Destructively reverse a list.
  244. KP_EXPORT result<object> nreverse_L (interpreter *interp, object obj);
  245. // Destructively sort a list.
  246. KP_EXPORT result<object> nsort_L (interpreter *interp,
  247. object obj, comparator& c);
  248. // Test for list equality.
  249. KP_EXPORT result<bool> eq_LL (interpreter *interp, object l1, object l2);
  250. // Compare lists L1 and L2.
  251. KP_EXPORT result<int> cmp_LL (interpreter *interp, object l1, object l2);
  252. // Return the IDX'th cdr of list LST.
  253. KP_EXPORT result<object> nthcdr (interpreter *interp, object idx, object lst);
  254. // Get the last cdr of list LST.
  255. KP_EXPORT result<object> last_L (interpreter *interp, object lst);
  256. // Destructively concatenate ARGC lists in ARGV.
  257. KP_EXPORT result<object> nconcat (interpreter *interp, object *argv, int argc);
  258. // Destructively reverse LST and concatenate TAIL into it.
  259. KP_EXPORT result<object> nrevconc (interpreter *interp,
  260. object lst, object tail);
  261. // Set the car of LST to OBJ.
  262. KP_EXPORT result<object> nputcar (interpreter *interp, object lst, object obj);
  263. // Set the cdr of LST to OBJ.
  264. KP_EXPORT result<object> nputcdr (interpreter *interp, object lst, object obj);
  265. // Mutate an object inside the list.
  266. KP_EXPORT result<object> nzap_L (interpreter *interp, object obj, object key,
  267. uint32_t flags, object fn,
  268. object *argv, int argc);
  269. // Find an element in a list.
  270. KP_EXPORT result<object> find_L (interpreter *interp, object obj,
  271. object key, object start,
  272. object end, object test);
  273. // Init OP for conses.
  274. KP_EXPORT init_op init_cons;
  275. KP_DECLS_END
  276. #endif