1 /* -*- tab-width:4; -*- */
2 /*
3  * The booleans
4  */
5 
6 #include "s.h"
7 
8 /*S* (not OBJ) => BOOLEAN */
9 /*D* Returns #t if obj is false, and returns #f otherwise. */
scm_not(SOBJ x)10 SOBJ scm_not(SOBJ x)
11 {
12   return SCM_EQ(x, scm_false) ? scm_true : scm_false;
13 }
14 
15 /*S* (boolean? OBJ) => BOOLEAN */
16 /*D* Returns #t if obj is either #t or #f and returns #f otherwise. */
scm_booleanp(SOBJ x)17 SOBJ scm_booleanp(SOBJ x)
18 {
19   return SCM_BOOLEANP(x) ? scm_true: scm_false;
20 }
21 
22 /*S* (eq? OBJ1 OBJ2) => BOOLEAN */
23 /*D* Returns #t if OBJ1 and OBJ2 represents the same cell. */
scm_eq(SOBJ x,SOBJ y)24 SOBJ scm_eq(SOBJ x, SOBJ y)
25 {
26   return( (x == y) ? scm_true : scm_false );
27 }
28 /*S* (eqv? OBJ1 OBJ2) => BOOLEAN */
29 /*D* Returns #t if OBJ1 and OBJ2 should normally be regarded as the
30   same object, #f otherwise. */
scm_eqv(SOBJ x,SOBJ y)31 SOBJ scm_eqv(SOBJ x, SOBJ y)
32 {
33   if (x == y)  return(scm_true);
34 
35   if ((SCM_SYMBOLP(x) || SCM_ATOMP(x)) && (SCM_SYMBOLP(y) || SCM_ATOMP(y))) {
36 	if (SCM_SYMBOLP(x)) x = SCM_SYM_NAME(x);
37 	if (SCM_SYMBOLP(y)) y = SCM_SYM_NAME(y);
38 	return(SCM_MKBOOL(x == y));
39   }
40   if (SCM_FNUMP(x) && SCM_FNUMP(y)) {
41 	return(SCM_MKBOOL(SCM_FNUM(x) == SCM_FNUM(y)));
42   }
43   if (SCM_EXACTP(x) && SCM_EXACTP(y)) {
44 	return(SCM_MKBOOL(scm_cmpnum(x,y) == 0));
45   }
46   return(scm_false);
47 }
48 
49 /*S* (equal? obj1 obj2) => BOOLEAN */
50 /*D* Equal? recursively compares the contents of pairs, vectors, and
51   strings, applying eqv? on other objects such as numbers and symbols.
52   A rule of thumb is that objects are generally equal? if they print
53   the same.  Equal? may fail to terminate if its arguments are
54   circular data structures. */
scm_equal(SOBJ x,SOBJ y)55 SOBJ scm_equal(SOBJ x, SOBJ y)
56 {
57   int t;
58 Top:
59   if (scm_eqv(x, y) == scm_true) return scm_true;
60 
61   switch (SCM_OBJTYPE(x)) {
62   case SOBJ_T_PAIR:
63 	if (SCM_PAIRP(y)) {
64 	  if (scm_equal(SCM_CAR(x), SCM_CAR(y)) == scm_false) return scm_false;
65 	  x = SCM_CDR(x); y = SCM_CDR(y);
66 	  goto Top;
67 	}
68 	break;
69   default:
70 	if ((t = SCM_OBJTYPE(x)) == SCM_OBJTYPE(y)) {
71 	  t = SCM_OBJTYPE(x);
72 	  if (scm_type_hook[t].compare) {
73 		return((*scm_type_hook[t].compare) (x,y));
74 	  }
75 	}
76   }
77   return scm_false;
78 }
79 
scm_init_boolean()80 void scm_init_boolean()
81 {
82   /* this are inlined
83   scm_add_cprim("not",			scm_not, 		1);
84   scm_add_cprim("boolean?",		scm_booleanp, 	1);
85   scm_add_cprim("eq?",			scm_eq,			2);
86   */
87   scm_add_cprim("eqv?",			scm_eqv,		2);
88   scm_add_cprim("equal?",		scm_equal,		2);
89 }
90