pairs.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* Copyright (C) 1995,1996,2000,2001, 2004, 2005, 2006, 2008 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. /* {Pairs}
  25. */
  26. #if (SCM_DEBUG_PAIR_ACCESSES == 1)
  27. #include "libguile/ports.h"
  28. #include "libguile/strings.h"
  29. void scm_error_pair_access (SCM non_pair)
  30. {
  31. static unsigned int running = 0;
  32. SCM message = scm_from_locale_string ("Non-pair accessed with SCM_C[AD]R: `~S'\n");
  33. if (!running)
  34. {
  35. running = 1;
  36. scm_simple_format (scm_current_error_port (),
  37. message, scm_list_1 (non_pair));
  38. abort ();
  39. }
  40. }
  41. #endif
  42. SCM_DEFINE (scm_cons, "cons", 2, 0, 0,
  43. (SCM x, SCM y),
  44. "Return a newly allocated pair whose car is @var{x} and whose\n"
  45. "cdr is @var{y}. The pair is guaranteed to be different (in the\n"
  46. "sense of @code{eq?}) from every previously existing object.")
  47. #define FUNC_NAME s_scm_cons
  48. {
  49. return scm_cell (SCM_UNPACK (x), SCM_UNPACK (y));
  50. }
  51. #undef FUNC_NAME
  52. SCM
  53. scm_cons2 (SCM w, SCM x, SCM y)
  54. {
  55. return scm_cons (w, scm_cons (x, y));
  56. }
  57. SCM_DEFINE (scm_pair_p, "pair?", 1, 0, 0,
  58. (SCM x),
  59. "Return @code{#t} if @var{x} is a pair; otherwise return\n"
  60. "@code{#f}.")
  61. #define FUNC_NAME s_scm_pair_p
  62. {
  63. return scm_from_bool (scm_is_pair (x));
  64. }
  65. #undef FUNC_NAME
  66. SCM
  67. scm_car (SCM pair)
  68. {
  69. if (!scm_is_pair (pair))
  70. scm_wrong_type_arg_msg (NULL, 0, pair, "pair");
  71. return SCM_CAR (pair);
  72. }
  73. SCM
  74. scm_cdr (SCM pair)
  75. {
  76. if (!scm_is_pair (pair))
  77. scm_wrong_type_arg_msg (NULL, 0, pair, "pair");
  78. return SCM_CDR (pair);
  79. }
  80. SCM
  81. scm_i_chase_pairs (SCM tree, scm_t_uint32 pattern)
  82. {
  83. do
  84. {
  85. if (!scm_is_pair (tree))
  86. scm_wrong_type_arg_msg (NULL, 0, tree, "pair");
  87. tree = (pattern & 1) ? SCM_CAR (tree) : SCM_CDR (tree);
  88. pattern >>= 2;
  89. }
  90. while (pattern);
  91. return tree;
  92. }
  93. SCM_DEFINE (scm_set_car_x, "set-car!", 2, 0, 0,
  94. (SCM pair, SCM value),
  95. "Stores @var{value} in the car field of @var{pair}. The value returned\n"
  96. "by @code{set-car!} is unspecified.")
  97. #define FUNC_NAME s_scm_set_car_x
  98. {
  99. SCM_VALIDATE_CONS (1, pair);
  100. SCM_SETCAR (pair, value);
  101. return SCM_UNSPECIFIED;
  102. }
  103. #undef FUNC_NAME
  104. SCM_DEFINE (scm_set_cdr_x, "set-cdr!", 2, 0, 0,
  105. (SCM pair, SCM value),
  106. "Stores @var{value} in the cdr field of @var{pair}. The value returned\n"
  107. "by @code{set-cdr!} is unspecified.")
  108. #define FUNC_NAME s_scm_set_cdr_x
  109. {
  110. SCM_VALIDATE_CONS (1, pair);
  111. SCM_SETCDR (pair, value);
  112. return SCM_UNSPECIFIED;
  113. }
  114. #undef FUNC_NAME
  115. /* Every cxr-pattern is made up of pairs of bits, starting with the two least
  116. * significant bits. If in a pair of bits the least significant of the two
  117. * bits is 0, this means CDR, otherwise CAR. The most significant bits of the
  118. * two bits is only needed to indicate when cxr-ing is ready. This is the
  119. * case, when all remaining pairs of bits equal 00. */
  120. typedef struct {
  121. const char *name;
  122. unsigned char pattern;
  123. } t_cxr;
  124. static const t_cxr cxrs[] =
  125. {
  126. {"cdr", 0x02}, /* 00000010 */
  127. {"car", 0x03}, /* 00000011 */
  128. {"cddr", 0x0a}, /* 00001010 */
  129. {"cdar", 0x0b}, /* 00001011 */
  130. {"cadr", 0x0e}, /* 00001110 */
  131. {"caar", 0x0f}, /* 00001111 */
  132. {"cdddr", 0x2a}, /* 00101010 */
  133. {"cddar", 0x2b}, /* 00101011 */
  134. {"cdadr", 0x2e}, /* 00101110 */
  135. {"cdaar", 0x2f}, /* 00101111 */
  136. {"caddr", 0x3a}, /* 00111010 */
  137. {"cadar", 0x3b}, /* 00111011 */
  138. {"caadr", 0x3e}, /* 00111110 */
  139. {"caaar", 0x3f}, /* 00111111 */
  140. {"cddddr", 0xaa}, /* 10101010 */
  141. {"cdddar", 0xab}, /* 10101011 */
  142. {"cddadr", 0xae}, /* 10101110 */
  143. {"cddaar", 0xaf}, /* 10101111 */
  144. {"cdaddr", 0xba}, /* 10111010 */
  145. {"cdadar", 0xbb}, /* 10111011 */
  146. {"cdaadr", 0xbe}, /* 10111110 */
  147. {"cdaaar", 0xbf}, /* 10111111 */
  148. {"cadddr", 0xea}, /* 11101010 */
  149. {"caddar", 0xeb}, /* 11101011 */
  150. {"cadadr", 0xee}, /* 11101110 */
  151. {"cadaar", 0xef}, /* 11101111 */
  152. {"caaddr", 0xfa}, /* 11111010 */
  153. {"caadar", 0xfb}, /* 11111011 */
  154. {"caaadr", 0xfe}, /* 11111110 */
  155. {"caaaar", 0xff}, /* 11111111 */
  156. {0, 0}
  157. };
  158. void
  159. scm_init_pairs ()
  160. {
  161. unsigned int subnr = 0;
  162. for (subnr = 0; cxrs[subnr].name; subnr++)
  163. {
  164. SCM (*pattern) () = (SCM (*) ()) (scm_t_bits) cxrs[subnr].pattern;
  165. scm_c_define_subr (cxrs[subnr].name, scm_tc7_cxr, pattern);
  166. }
  167. #include "libguile/pairs.x"
  168. }
  169. /*
  170. Local Variables:
  171. c-file-style: "gnu"
  172. End:
  173. */