pairs.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /* Copyright (C) 1995,1996,2000,2001, 2004, 2005, 2006, 2008, 2009,
  2. * 2011, 2012, 2013 Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include "libguile/_scm.h"
  23. #include "libguile/validate.h"
  24. #include "libguile/pairs.h"
  25. #include "verify.h"
  26. /* {Pairs}
  27. */
  28. /*
  29. * This compile-time test verifies the properties needed for the
  30. * efficient test macro scm_is_null_or_nil defined in pairs.h,
  31. * which is defined in terms of the SCM_MATCHES_BITS_IN_COMMON macro.
  32. *
  33. * See the comments preceeding the definitions of SCM_BOOL_F and
  34. * SCM_MATCHES_BITS_IN_COMMON in tags.h for more information.
  35. */
  36. verify (SCM_BITS_DIFFER_IN_EXACTLY_ONE_BIT_POSITION \
  37. (SCM_ELISP_NIL_BITS, SCM_EOL_BITS));
  38. #if (SCM_DEBUG_PAIR_ACCESSES == 1)
  39. #include "libguile/ports.h"
  40. #include "libguile/strings.h"
  41. void scm_error_pair_access (SCM non_pair)
  42. {
  43. static unsigned int running = 0;
  44. SCM message = scm_from_locale_string ("Non-pair accessed with SCM_C[AD]R: `~S'\n");
  45. if (!running)
  46. {
  47. running = 1;
  48. scm_simple_format (scm_current_error_port (),
  49. message, scm_list_1 (non_pair));
  50. abort ();
  51. }
  52. }
  53. #endif
  54. SCM
  55. scm_cons2 (SCM w, SCM x, SCM y)
  56. {
  57. return scm_cons (w, scm_cons (x, y));
  58. }
  59. SCM_DEFINE (scm_pair_p, "pair?", 1, 0, 0,
  60. (SCM x),
  61. "Return @code{#t} if @var{x} is a pair; otherwise return\n"
  62. "@code{#f}.")
  63. #define FUNC_NAME s_scm_pair_p
  64. {
  65. return scm_from_bool (scm_is_pair (x));
  66. }
  67. #undef FUNC_NAME
  68. SCM_DEFINE (scm_set_car_x, "set-car!", 2, 0, 0,
  69. (SCM pair, SCM value),
  70. "Stores @var{value} in the car field of @var{pair}. The value returned\n"
  71. "by @code{set-car!} is unspecified.")
  72. #define FUNC_NAME s_scm_set_car_x
  73. {
  74. SCM_VALIDATE_CONS (1, pair);
  75. SCM_SETCAR (pair, value);
  76. return SCM_UNSPECIFIED;
  77. }
  78. #undef FUNC_NAME
  79. SCM_DEFINE (scm_set_cdr_x, "set-cdr!", 2, 0, 0,
  80. (SCM pair, SCM value),
  81. "Stores @var{value} in the cdr field of @var{pair}. The value returned\n"
  82. "by @code{set-cdr!} is unspecified.")
  83. #define FUNC_NAME s_scm_set_cdr_x
  84. {
  85. SCM_VALIDATE_CONS (1, pair);
  86. SCM_SETCDR (pair, value);
  87. return SCM_UNSPECIFIED;
  88. }
  89. #undef FUNC_NAME
  90. /* Every cxr-pattern is made up of pairs of bits, starting with the two least
  91. * significant bits. If in a pair of bits the least significant of the two
  92. * bits is 0, this means CDR, otherwise CAR. The most significant bits of the
  93. * two bits is only needed to indicate when cxr-ing is ready. This is the
  94. * case, when all remaining pairs of bits equal 00. */
  95. /* The compiler should unroll this. */
  96. #define CHASE_PAIRS(tree, FUNC_NAME, pattern) \
  97. scm_t_uint32 pattern_var = pattern; \
  98. do \
  99. { \
  100. if (!scm_is_pair (tree)) \
  101. scm_wrong_type_arg_msg (FUNC_NAME, 0, tree, "pair"); \
  102. tree = (pattern_var & 1) ? SCM_CAR (tree) : SCM_CDR (tree); \
  103. pattern_var >>= 2; \
  104. } \
  105. while (pattern_var); \
  106. return tree
  107. SCM_DEFINE (scm_cddr, "cddr", 1, 0, 0, (SCM x), "")
  108. #define FUNC_NAME s_scm_cddr
  109. {
  110. CHASE_PAIRS (x, "cddr", 0x0a); /* 00001010 */
  111. }
  112. #undef FUNC_NAME
  113. SCM_DEFINE (scm_cdar, "cdar", 1, 0, 0, (SCM x), "")
  114. #define FUNC_NAME s_scm_cdar
  115. {
  116. CHASE_PAIRS (x, "cdar", 0x0b); /* 00001011 */
  117. }
  118. #undef FUNC_NAME
  119. SCM_DEFINE (scm_cadr, "cadr", 1, 0, 0, (SCM x), "")
  120. #define FUNC_NAME s_scm_cadr
  121. {
  122. CHASE_PAIRS (x, "cadr", 0x0e); /* 00001110 */
  123. }
  124. #undef FUNC_NAME
  125. SCM_DEFINE (scm_caar, "caar", 1, 0, 0, (SCM x), "")
  126. #define FUNC_NAME s_scm_caar
  127. {
  128. CHASE_PAIRS (x, "caar", 0x0f); /* 00001111 */
  129. }
  130. #undef FUNC_NAME
  131. SCM_DEFINE (scm_cdddr, "cdddr", 1, 0, 0, (SCM x), "")
  132. #define FUNC_NAME s_scm_cdddr
  133. {
  134. CHASE_PAIRS (x, "cdddr", 0x2a); /* 00101010 */
  135. }
  136. #undef FUNC_NAME
  137. SCM_DEFINE (scm_cddar, "cddar", 1, 0, 0, (SCM x), "")
  138. #define FUNC_NAME s_scm_cddar
  139. {
  140. CHASE_PAIRS (x, "cddar", 0x2b); /* 00101011 */
  141. }
  142. #undef FUNC_NAME
  143. SCM_DEFINE (scm_cdadr, "cdadr", 1, 0, 0, (SCM x), "")
  144. #define FUNC_NAME s_scm_cdadr
  145. {
  146. CHASE_PAIRS (x, "cdadr", 0x2e); /* 00101110 */
  147. }
  148. #undef FUNC_NAME
  149. SCM_DEFINE (scm_cdaar, "cdaar", 1, 0, 0, (SCM x), "")
  150. #define FUNC_NAME s_scm_cdaar
  151. {
  152. CHASE_PAIRS (x, "cdaar", 0x2f); /* 00101111 */
  153. }
  154. #undef FUNC_NAME
  155. SCM_DEFINE (scm_caddr, "caddr", 1, 0, 0, (SCM x), "")
  156. #define FUNC_NAME s_scm_caddr
  157. {
  158. CHASE_PAIRS (x, "caddr", 0x3a); /* 00111010 */
  159. }
  160. #undef FUNC_NAME
  161. SCM_DEFINE (scm_cadar, "cadar", 1, 0, 0, (SCM x), "")
  162. #define FUNC_NAME s_scm_cadar
  163. {
  164. CHASE_PAIRS (x, "cadar", 0x3b); /* 00111011 */
  165. }
  166. #undef FUNC_NAME
  167. SCM_DEFINE (scm_caadr, "caadr", 1, 0, 0, (SCM x), "")
  168. #define FUNC_NAME s_scm_caadr
  169. {
  170. CHASE_PAIRS (x, "caadr", 0x3e); /* 00111110 */
  171. }
  172. #undef FUNC_NAME
  173. SCM_DEFINE (scm_caaar, "caaar", 1, 0, 0, (SCM x), "")
  174. #define FUNC_NAME s_scm_caaar
  175. {
  176. CHASE_PAIRS (x, "caaar", 0x3f); /* 00111111 */
  177. }
  178. #undef FUNC_NAME
  179. SCM_DEFINE (scm_cddddr, "cddddr", 1, 0, 0, (SCM x), "")
  180. #define FUNC_NAME s_scm_cddddr
  181. {
  182. CHASE_PAIRS (x, "cddddr", 0xaa); /* 10101010 */
  183. }
  184. #undef FUNC_NAME
  185. SCM_DEFINE (scm_cdddar, "cdddar", 1, 0, 0, (SCM x), "")
  186. #define FUNC_NAME s_scm_cdddar
  187. {
  188. CHASE_PAIRS (x, "cdddar", 0xab); /* 10101011 */
  189. }
  190. #undef FUNC_NAME
  191. SCM_DEFINE (scm_cddadr, "cddadr", 1, 0, 0, (SCM x), "")
  192. #define FUNC_NAME s_scm_cddadr
  193. {
  194. CHASE_PAIRS (x, "cddadr", 0xae); /* 10101110 */
  195. }
  196. #undef FUNC_NAME
  197. SCM_DEFINE (scm_cddaar, "cddaar", 1, 0, 0, (SCM x), "")
  198. #define FUNC_NAME s_scm_cddaar
  199. {
  200. CHASE_PAIRS (x, "cddaar", 0xaf); /* 10101111 */
  201. }
  202. #undef FUNC_NAME
  203. SCM_DEFINE (scm_cdaddr, "cdaddr", 1, 0, 0, (SCM x), "")
  204. #define FUNC_NAME s_scm_cdaddr
  205. {
  206. CHASE_PAIRS (x, "cdaddr", 0xba); /* 10111010 */
  207. }
  208. #undef FUNC_NAME
  209. SCM_DEFINE (scm_cdadar, "cdadar", 1, 0, 0, (SCM x), "")
  210. #define FUNC_NAME s_scm_cdadar
  211. {
  212. CHASE_PAIRS (x, "cdadar", 0xbb); /* 10111011 */
  213. }
  214. #undef FUNC_NAME
  215. SCM_DEFINE (scm_cdaadr, "cdaadr", 1, 0, 0, (SCM x), "")
  216. #define FUNC_NAME s_scm_cdaadr
  217. {
  218. CHASE_PAIRS (x, "cdaadr", 0xbe); /* 10111110 */
  219. }
  220. #undef FUNC_NAME
  221. SCM_DEFINE (scm_cdaaar, "cdaaar", 1, 0, 0, (SCM x), "")
  222. #define FUNC_NAME s_scm_cdaaar
  223. {
  224. CHASE_PAIRS (x, "cdaaar", 0xbf); /* 10111111 */
  225. }
  226. #undef FUNC_NAME
  227. SCM_DEFINE (scm_cadddr, "cadddr", 1, 0, 0, (SCM x), "")
  228. #define FUNC_NAME s_scm_cadddr
  229. {
  230. CHASE_PAIRS (x, "cadddr", 0xea); /* 11101010 */
  231. }
  232. #undef FUNC_NAME
  233. SCM_DEFINE (scm_caddar, "caddar", 1, 0, 0, (SCM x), "")
  234. #define FUNC_NAME s_scm_caddar
  235. {
  236. CHASE_PAIRS (x, "caddar", 0xeb); /* 11101011 */
  237. }
  238. #undef FUNC_NAME
  239. SCM_DEFINE (scm_cadadr, "cadadr", 1, 0, 0, (SCM x), "")
  240. #define FUNC_NAME s_scm_cadadr
  241. {
  242. CHASE_PAIRS (x, "cadadr", 0xee); /* 11101110 */
  243. }
  244. #undef FUNC_NAME
  245. SCM_DEFINE (scm_cadaar, "cadaar", 1, 0, 0, (SCM x), "")
  246. #define FUNC_NAME s_scm_cadaar
  247. {
  248. CHASE_PAIRS (x, "cadaar", 0xef); /* 11101111 */
  249. }
  250. #undef FUNC_NAME
  251. SCM_DEFINE (scm_caaddr, "caaddr", 1, 0, 0, (SCM x), "")
  252. #define FUNC_NAME s_scm_caaddr
  253. {
  254. CHASE_PAIRS (x, "caaddr", 0xfa); /* 11111010 */
  255. }
  256. #undef FUNC_NAME
  257. SCM_DEFINE (scm_caadar, "caadar", 1, 0, 0, (SCM x), "")
  258. #define FUNC_NAME s_scm_caadar
  259. {
  260. CHASE_PAIRS (x, "caadar", 0xfb); /* 11111011 */
  261. }
  262. #undef FUNC_NAME
  263. SCM_DEFINE (scm_caaadr, "caaadr", 1, 0, 0, (SCM x), "")
  264. #define FUNC_NAME s_scm_caaadr
  265. {
  266. CHASE_PAIRS (x, "caaadr", 0xfe); /* 11111110 */
  267. }
  268. #undef FUNC_NAME
  269. SCM_DEFINE (scm_caaaar, "caaaar", 1, 0, 0, (SCM x), "")
  270. #define FUNC_NAME s_scm_caaaar
  271. {
  272. CHASE_PAIRS (x, "caaaar", 0xff); /* 11111111 */
  273. }
  274. #undef FUNC_NAME
  275. void
  276. scm_init_pairs ()
  277. {
  278. #include "libguile/pairs.x"
  279. scm_c_define_gsubr ("cons", 2, 0, 0, scm_cons);
  280. scm_c_define_gsubr ("car", 1, 0, 0, scm_car);
  281. scm_c_define_gsubr ("cdr", 1, 0, 0, scm_cdr);
  282. }
  283. /*
  284. Local Variables:
  285. c-file-style: "gnu"
  286. End:
  287. */