pairs.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* Copyright (C) 1995,1996,2000,2001, 2004, 2005, 2006, 2008, 2009, 2011 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_DEFINE (scm_cons, "cons", 2, 0, 0,
  54. (SCM x, SCM y),
  55. "Return a newly allocated pair whose car is @var{x} and whose\n"
  56. "cdr is @var{y}. The pair is guaranteed to be different (in the\n"
  57. "sense of @code{eq?}) from every previously existing object.")
  58. #define FUNC_NAME s_scm_cons
  59. {
  60. return scm_cell (SCM_UNPACK (x), SCM_UNPACK (y));
  61. }
  62. #undef FUNC_NAME
  63. SCM
  64. scm_cons2 (SCM w, SCM x, SCM y)
  65. {
  66. return scm_cons (w, scm_cons (x, y));
  67. }
  68. SCM_DEFINE (scm_pair_p, "pair?", 1, 0, 0,
  69. (SCM x),
  70. "Return @code{#t} if @var{x} is a pair; otherwise return\n"
  71. "@code{#f}.")
  72. #define FUNC_NAME s_scm_pair_p
  73. {
  74. return scm_from_bool (scm_is_pair (x));
  75. }
  76. #undef FUNC_NAME
  77. SCM_DEFINE (scm_set_car_x, "set-car!", 2, 0, 0,
  78. (SCM pair, SCM value),
  79. "Stores @var{value} in the car field of @var{pair}. The value returned\n"
  80. "by @code{set-car!} is unspecified.")
  81. #define FUNC_NAME s_scm_set_car_x
  82. {
  83. SCM_VALIDATE_CONS (1, pair);
  84. SCM_SETCAR (pair, value);
  85. return SCM_UNSPECIFIED;
  86. }
  87. #undef FUNC_NAME
  88. SCM_DEFINE (scm_set_cdr_x, "set-cdr!", 2, 0, 0,
  89. (SCM pair, SCM value),
  90. "Stores @var{value} in the cdr field of @var{pair}. The value returned\n"
  91. "by @code{set-cdr!} is unspecified.")
  92. #define FUNC_NAME s_scm_set_cdr_x
  93. {
  94. SCM_VALIDATE_CONS (1, pair);
  95. SCM_SETCDR (pair, value);
  96. return SCM_UNSPECIFIED;
  97. }
  98. #undef FUNC_NAME
  99. /* Every cxr-pattern is made up of pairs of bits, starting with the two least
  100. * significant bits. If in a pair of bits the least significant of the two
  101. * bits is 0, this means CDR, otherwise CAR. The most significant bits of the
  102. * two bits is only needed to indicate when cxr-ing is ready. This is the
  103. * case, when all remaining pairs of bits equal 00. */
  104. /* The compiler should unroll this. */
  105. #define CHASE_PAIRS(tree, FUNC_NAME, pattern) \
  106. scm_t_uint32 pattern_var = pattern; \
  107. do \
  108. { \
  109. if (!scm_is_pair (tree)) \
  110. scm_wrong_type_arg_msg (FUNC_NAME, 0, tree, "pair"); \
  111. tree = (pattern_var & 1) ? SCM_CAR (tree) : SCM_CDR (tree); \
  112. pattern_var >>= 2; \
  113. } \
  114. while (pattern_var); \
  115. return tree
  116. SCM_DEFINE (scm_cdr, "cdr", 1, 0, 0, (SCM x), "")
  117. {
  118. CHASE_PAIRS (x, "cdr", 0x02); /* 00000010 */
  119. }
  120. SCM_DEFINE (scm_car, "car", 1, 0, 0, (SCM x), "")
  121. {
  122. CHASE_PAIRS (x, "car", 0x03); /* 00000011 */
  123. }
  124. SCM_DEFINE (scm_cddr, "cddr", 1, 0, 0, (SCM x), "")
  125. {
  126. CHASE_PAIRS (x, "cddr", 0x0a); /* 00001010 */
  127. }
  128. SCM_DEFINE (scm_cdar, "cdar", 1, 0, 0, (SCM x), "")
  129. {
  130. CHASE_PAIRS (x, "cdar", 0x0b); /* 00001011 */
  131. }
  132. SCM_DEFINE (scm_cadr, "cadr", 1, 0, 0, (SCM x), "")
  133. {
  134. CHASE_PAIRS (x, "cadr", 0x0e); /* 00001110 */
  135. }
  136. SCM_DEFINE (scm_caar, "caar", 1, 0, 0, (SCM x), "")
  137. {
  138. CHASE_PAIRS (x, "caar", 0x0f); /* 00001111 */
  139. }
  140. SCM_DEFINE (scm_cdddr, "cdddr", 1, 0, 0, (SCM x), "")
  141. {
  142. CHASE_PAIRS (x, "cdddr", 0x2a); /* 00101010 */
  143. }
  144. SCM_DEFINE (scm_cddar, "cddar", 1, 0, 0, (SCM x), "")
  145. {
  146. CHASE_PAIRS (x, "cddar", 0x2b); /* 00101011 */
  147. }
  148. SCM_DEFINE (scm_cdadr, "cdadr", 1, 0, 0, (SCM x), "")
  149. {
  150. CHASE_PAIRS (x, "cdadr", 0x2e); /* 00101110 */
  151. }
  152. SCM_DEFINE (scm_cdaar, "cdaar", 1, 0, 0, (SCM x), "")
  153. {
  154. CHASE_PAIRS (x, "cdaar", 0x2f); /* 00101111 */
  155. }
  156. SCM_DEFINE (scm_caddr, "caddr", 1, 0, 0, (SCM x), "")
  157. {
  158. CHASE_PAIRS (x, "caddr", 0x3a); /* 00111010 */
  159. }
  160. SCM_DEFINE (scm_cadar, "cadar", 1, 0, 0, (SCM x), "")
  161. {
  162. CHASE_PAIRS (x, "cadar", 0x3b); /* 00111011 */
  163. }
  164. SCM_DEFINE (scm_caadr, "caadr", 1, 0, 0, (SCM x), "")
  165. {
  166. CHASE_PAIRS (x, "caadr", 0x3e); /* 00111110 */
  167. }
  168. SCM_DEFINE (scm_caaar, "caaar", 1, 0, 0, (SCM x), "")
  169. {
  170. CHASE_PAIRS (x, "caaar", 0x3f); /* 00111111 */
  171. }
  172. SCM_DEFINE (scm_cddddr, "cddddr", 1, 0, 0, (SCM x), "")
  173. {
  174. CHASE_PAIRS (x, "cddddr", 0xaa); /* 10101010 */
  175. }
  176. SCM_DEFINE (scm_cdddar, "cdddar", 1, 0, 0, (SCM x), "")
  177. {
  178. CHASE_PAIRS (x, "cdddar", 0xab); /* 10101011 */
  179. }
  180. SCM_DEFINE (scm_cddadr, "cddadr", 1, 0, 0, (SCM x), "")
  181. {
  182. CHASE_PAIRS (x, "cddadr", 0xae); /* 10101110 */
  183. }
  184. SCM_DEFINE (scm_cddaar, "cddaar", 1, 0, 0, (SCM x), "")
  185. {
  186. CHASE_PAIRS (x, "cddaar", 0xaf); /* 10101111 */
  187. }
  188. SCM_DEFINE (scm_cdaddr, "cdaddr", 1, 0, 0, (SCM x), "")
  189. {
  190. CHASE_PAIRS (x, "cdaddr", 0xba); /* 10111010 */
  191. }
  192. SCM_DEFINE (scm_cdadar, "cdadar", 1, 0, 0, (SCM x), "")
  193. {
  194. CHASE_PAIRS (x, "cdadar", 0xbb); /* 10111011 */
  195. }
  196. SCM_DEFINE (scm_cdaadr, "cdaadr", 1, 0, 0, (SCM x), "")
  197. {
  198. CHASE_PAIRS (x, "cdaadr", 0xbe); /* 10111110 */
  199. }
  200. SCM_DEFINE (scm_cdaaar, "cdaaar", 1, 0, 0, (SCM x), "")
  201. {
  202. CHASE_PAIRS (x, "cdaaar", 0xbf); /* 10111111 */
  203. }
  204. SCM_DEFINE (scm_cadddr, "cadddr", 1, 0, 0, (SCM x), "")
  205. {
  206. CHASE_PAIRS (x, "cadddr", 0xea); /* 11101010 */
  207. }
  208. SCM_DEFINE (scm_caddar, "caddar", 1, 0, 0, (SCM x), "")
  209. {
  210. CHASE_PAIRS (x, "caddar", 0xeb); /* 11101011 */
  211. }
  212. SCM_DEFINE (scm_cadadr, "cadadr", 1, 0, 0, (SCM x), "")
  213. {
  214. CHASE_PAIRS (x, "cadadr", 0xee); /* 11101110 */
  215. }
  216. SCM_DEFINE (scm_cadaar, "cadaar", 1, 0, 0, (SCM x), "")
  217. {
  218. CHASE_PAIRS (x, "cadaar", 0xef); /* 11101111 */
  219. }
  220. SCM_DEFINE (scm_caaddr, "caaddr", 1, 0, 0, (SCM x), "")
  221. {
  222. CHASE_PAIRS (x, "caaddr", 0xfa); /* 11111010 */
  223. }
  224. SCM_DEFINE (scm_caadar, "caadar", 1, 0, 0, (SCM x), "")
  225. {
  226. CHASE_PAIRS (x, "caadar", 0xfb); /* 11111011 */
  227. }
  228. SCM_DEFINE (scm_caaadr, "caaadr", 1, 0, 0, (SCM x), "")
  229. {
  230. CHASE_PAIRS (x, "caaadr", 0xfe); /* 11111110 */
  231. }
  232. SCM_DEFINE (scm_caaaar, "caaaar", 1, 0, 0, (SCM x), "")
  233. {
  234. CHASE_PAIRS (x, "caaaar", 0xff); /* 11111111 */
  235. }
  236. void
  237. scm_init_pairs ()
  238. {
  239. #include "libguile/pairs.x"
  240. }
  241. /*
  242. Local Variables:
  243. c-file-style: "gnu"
  244. End:
  245. */