pairs.c 5.1 KB

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