eq.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* Copyright (C) 1995,1996,1997,1998, 2000, 2002 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program 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
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. * Boston, MA 02111-1307 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. #include <stdio.h>
  42. #include "libguile/_scm.h"
  43. #include "libguile/ramap.h"
  44. #include "libguile/stackchk.h"
  45. #include "libguile/strorder.h"
  46. #include "libguile/async.h"
  47. #include "libguile/root.h"
  48. #include "libguile/smob.h"
  49. #include "libguile/unif.h"
  50. #include "libguile/vectors.h"
  51. #include "libguile/validate.h"
  52. #include "libguile/eq.h"
  53. SCM_DEFINE1 (scm_eq_p, "eq?", scm_tc7_rpsubr,
  54. (SCM x, SCM y),
  55. "Return #t iff X references the same object as Y.\n"
  56. "`eq?' is similar to `eqv?' except that in some cases\n"
  57. "it is capable of discerning distinctions finer than\n"
  58. "those detectable by `eqv?'.\n")
  59. #define FUNC_NAME s_scm_eq_p
  60. {
  61. return SCM_BOOL (SCM_EQ_P (x, y));
  62. }
  63. #undef FUNC_NAME
  64. SCM_DEFINE1 (scm_eqv_p, "eqv?", scm_tc7_rpsubr,
  65. (SCM x, SCM y),
  66. "The `eqv?' procedure defines a useful equivalence relation on objects.\n"
  67. "Briefly, it returns #t if X and Y should normally be\n"
  68. "regarded as the same object. This relation is left\n"
  69. "slightly open to interpretation, but works for comparing\n"
  70. "immediate integers, characters, and inexact numbers.\n")
  71. #define FUNC_NAME s_scm_eqv_p
  72. {
  73. if (SCM_EQ_P (x, y))
  74. return SCM_BOOL_T;
  75. if (SCM_IMP (x))
  76. return SCM_BOOL_F;
  77. if (SCM_IMP (y))
  78. return SCM_BOOL_F;
  79. /* this ensures that types and scm_length are the same. */
  80. if (SCM_CELL_TYPE (x) != SCM_CELL_TYPE (y))
  81. {
  82. /* treat mixes of real and complex types specially */
  83. if (SCM_SLOPPY_INEXACTP (x))
  84. {
  85. if (SCM_SLOPPY_REALP (x))
  86. return SCM_BOOL (SCM_SLOPPY_COMPLEXP (y)
  87. && SCM_REAL_VALUE (x) == SCM_COMPLEX_REAL (y)
  88. && 0.0 == SCM_COMPLEX_IMAG (y));
  89. else
  90. return SCM_BOOL (SCM_SLOPPY_REALP (y)
  91. && SCM_COMPLEX_REAL (x) == SCM_REAL_VALUE (y)
  92. && SCM_COMPLEX_IMAG (x) == 0.0);
  93. }
  94. return SCM_BOOL_F;
  95. }
  96. if (SCM_NUMP (x))
  97. {
  98. if (SCM_BIGP (x)) {
  99. return SCM_BOOL (0 == scm_bigcomp (x, y));
  100. } else if (SCM_SLOPPY_REALP (x)) {
  101. return SCM_BOOL (SCM_REAL_VALUE (x) == SCM_REAL_VALUE (y));
  102. } else { /* complex */
  103. return SCM_BOOL (SCM_COMPLEX_REAL (x) == SCM_COMPLEX_REAL (y)
  104. && SCM_COMPLEX_IMAG (x) == SCM_COMPLEX_IMAG (y));
  105. }
  106. }
  107. return SCM_BOOL_F;
  108. }
  109. #undef FUNC_NAME
  110. SCM_DEFINE1 (scm_equal_p, "equal?", scm_tc7_rpsubr,
  111. (SCM x, SCM y),
  112. "Return #t iff X and Y are recursively `eqv?' equivalent.\n"
  113. "`equal?' recursively compares the contents of pairs, vectors, and\n"
  114. "strings, applying `eqv?' on other objects such as numbers and\n"
  115. "symbols. A rule of thumb is that objects are generally `equal?'\n"
  116. "if they print the same. `Equal?' may fail to terminate if its\n"
  117. "arguments are circular data structures.\n")
  118. #define FUNC_NAME s_scm_equal_p
  119. {
  120. SCM_CHECK_STACK;
  121. tailrecurse:
  122. SCM_TICK;
  123. if (SCM_EQ_P (x, y))
  124. return SCM_BOOL_T;
  125. if (SCM_IMP (x))
  126. return SCM_BOOL_F;
  127. if (SCM_IMP (y))
  128. return SCM_BOOL_F;
  129. if (SCM_SLOPPY_CONSP (x) && SCM_SLOPPY_CONSP (y))
  130. {
  131. if (SCM_FALSEP (scm_equal_p (SCM_CAR (x), SCM_CAR (y))))
  132. return SCM_BOOL_F;
  133. x = SCM_CDR(x);
  134. y = SCM_CDR(y);
  135. goto tailrecurse;
  136. }
  137. if (SCM_TYP7S (x) == scm_tc7_string && SCM_TYP7S (y) == scm_tc7_string)
  138. return scm_string_equal_p (x, y);
  139. /* This ensures that types and scm_length are the same. */
  140. if (SCM_CELL_TYPE (x) != SCM_CELL_TYPE (y))
  141. {
  142. /* treat mixes of real and complex types specially */
  143. if (SCM_SLOPPY_INEXACTP (x))
  144. {
  145. if (SCM_SLOPPY_REALP (x))
  146. return SCM_BOOL (SCM_SLOPPY_COMPLEXP (y)
  147. && SCM_REAL_VALUE (x) == SCM_COMPLEX_REAL (y)
  148. && 0.0 == SCM_COMPLEX_IMAG (y));
  149. else
  150. return SCM_BOOL (SCM_SLOPPY_REALP (y)
  151. && SCM_COMPLEX_REAL (x) == SCM_REAL_VALUE (y)
  152. && SCM_COMPLEX_IMAG (x) == 0.0);
  153. }
  154. return SCM_BOOL_F;
  155. }
  156. switch (SCM_TYP7 (x))
  157. {
  158. default:
  159. return SCM_BOOL_F;
  160. case scm_tc7_vector:
  161. case scm_tc7_wvect:
  162. return scm_vector_equal_p (x, y);
  163. case scm_tc7_smob:
  164. {
  165. int i = SCM_SMOBNUM (x);
  166. if (!(i < scm_numsmob))
  167. return SCM_BOOL_F;
  168. if (scm_smobs[i].equalp)
  169. return (scm_smobs[i].equalp) (x, y);
  170. else
  171. return SCM_BOOL_F;
  172. }
  173. #ifdef HAVE_ARRAYS
  174. case scm_tc7_bvect: case scm_tc7_uvect: case scm_tc7_ivect:
  175. case scm_tc7_fvect: case scm_tc7_cvect: case scm_tc7_dvect:
  176. case scm_tc7_svect:
  177. #ifdef HAVE_LONG_LONGS
  178. case scm_tc7_llvect:
  179. #endif
  180. case scm_tc7_byvect:
  181. if (scm_tc16_array && scm_smobs[0x0ff & (scm_tc16_array >> 8)].equalp)
  182. return scm_array_equal_p (x, y);
  183. #endif
  184. }
  185. return SCM_BOOL_F;
  186. }
  187. #undef FUNC_NAME
  188. void
  189. scm_init_eq ()
  190. {
  191. #include "libguile/eq.x"
  192. }
  193. /*
  194. Local Variables:
  195. c-file-style: "gnu"
  196. End:
  197. */