pairs.c 7.2 KB

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