boolean.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #ifndef SCM_BOOLEAN_H
  2. #define SCM_BOOLEAN_H
  3. /* Copyright 1995-1996,2000,2006,2008-2010,2013,2018
  4. Free Software Foundation, Inc.
  5. This file is part of Guile.
  6. Guile is free software: you can redistribute it and/or modify it
  7. under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Guile is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with Guile. If not, see
  16. <https://www.gnu.org/licenses/>. */
  17. #include "libguile/scm.h"
  18. /* Boolean Values. Obviously there are #t and #f, but there is also nil to deal
  19. * with. We choose to treat nil as a false boolean. All options might silently
  20. * break existing code, but this one seems most responsible.
  21. *
  22. */
  23. /*
  24. * Use these macros if it's important (for correctness)
  25. * that #nil MUST be considered true
  26. */
  27. #define scm_is_false_and_not_nil(x) (scm_is_eq ((x), SCM_BOOL_F))
  28. #define scm_is_true_or_nil(x) (!scm_is_eq ((x), SCM_BOOL_F))
  29. /*
  30. * Use these macros if #nil will never be tested,
  31. * for increased efficiency.
  32. */
  33. #define scm_is_false_assume_not_nil(x) (scm_is_eq ((x), SCM_BOOL_F))
  34. #define scm_is_true_assume_not_nil(x) (!scm_is_eq ((x), SCM_BOOL_F))
  35. /*
  36. * See the comments preceeding the definitions of SCM_BOOL_F and
  37. * SCM_MATCHES_BITS_IN_COMMON in scm.h for more information on
  38. * how the following macro works.
  39. */
  40. #define scm_is_false_or_nil(x) \
  41. (SCM_MATCHES_BITS_IN_COMMON ((x), SCM_ELISP_NIL, SCM_BOOL_F))
  42. #define scm_is_true_and_not_nil(x) (!scm_is_false_or_nil (x))
  43. /* #nil is false. */
  44. #define scm_is_false(x) (scm_is_false_or_nil (x))
  45. #define scm_is_true(x) (!scm_is_false (x))
  46. /*
  47. * Since we know SCM_BOOL_F and SCM_BOOL_T differ by exactly one bit,
  48. * and that SCM_BOOL_F and SCM_ELISP_NIL differ by exactly one bit,
  49. * and that they of course can't be the same bit (or else SCM_BOOL_T
  50. * and SCM_ELISP_NIL be would equal), it follows that SCM_BOOL_T and
  51. * SCM_ELISP_NIL differ by exactly two bits, and these are the bits
  52. * which will be ignored by SCM_MATCHES_BITS_IN_COMMON below.
  53. *
  54. * See the comments preceeding the definitions of SCM_BOOL_F and
  55. * SCM_MATCHES_BITS_IN_COMMON in scm.h for more information.
  56. *
  57. * If SCM_ENABLE_ELISP is true, then scm_is_bool_or_nil(x)
  58. * returns 1 if and only if x is one of the following: SCM_BOOL_F,
  59. * SCM_BOOL_T, SCM_ELISP_NIL, or SCM_XXX_ANOTHER_BOOLEAN_DONT_USE_0.
  60. * Otherwise, it returns 0.
  61. */
  62. #define scm_is_bool_or_nil(x) \
  63. (SCM_MATCHES_BITS_IN_COMMON ((x), SCM_BOOL_T, SCM_ELISP_NIL))
  64. #define scm_is_bool_and_not_nil(x) \
  65. (SCM_MATCHES_BITS_IN_COMMON ((x), SCM_BOOL_F, SCM_BOOL_T))
  66. SCM_API int scm_is_bool (SCM);
  67. #define scm_is_bool(x) (scm_is_bool_or_nil (x))
  68. #define scm_from_bool(x) ((x) ? SCM_BOOL_T : SCM_BOOL_F)
  69. SCM_API int scm_to_bool (SCM x);
  70. /* Older spellings for the above routines, kept around for
  71. compatibility. */
  72. #define SCM_FALSEP(x) (scm_is_false (x))
  73. #define SCM_NFALSEP(x) (scm_is_true (x))
  74. #define SCM_BOOLP(x) (scm_is_bool (x))
  75. #define SCM_BOOL(x) (scm_from_bool (x))
  76. #define SCM_NEGATE_BOOL(f) (scm_from_bool (!(f)))
  77. #define SCM_BOOL_NOT(x) (scm_not (x))
  78. /*
  79. * The following macros efficiently implement boolean truth testing as
  80. * expected by most lisps, which treat '() aka SCM_EOL as false.
  81. *
  82. * Since we know SCM_ELISP_NIL and SCM_BOOL_F differ by exactly one
  83. * bit, and that SCM_ELISP_NIL and SCM_EOL differ by exactly one bit,
  84. * and that they of course can't be the same bit (or else SCM_BOOL_F
  85. * and SCM_EOL be would equal), it follows that SCM_BOOL_F and SCM_EOL
  86. * differ by exactly two bits, and these are the bits which will be
  87. * ignored by SCM_MATCHES_BITS_IN_COMMON below.
  88. *
  89. * See the comments preceeding the definitions of SCM_BOOL_F and
  90. * SCM_MATCHES_BITS_IN_COMMON in scm.h for more information.
  91. *
  92. * scm_is_lisp_false(x) returns 1 if and only if x is one of the
  93. * following: SCM_BOOL_F, SCM_ELISP_NIL, SCM_EOL or
  94. * SCM_XXX_ANOTHER_LISP_FALSE_DONT_USE. Otherwise, it returns 0.
  95. */
  96. #define scm_is_lisp_false(x) \
  97. (SCM_MATCHES_BITS_IN_COMMON ((x), SCM_BOOL_F, SCM_EOL))
  98. SCM_API SCM scm_not (SCM x);
  99. SCM_API SCM scm_boolean_p (SCM obj);
  100. SCM_API SCM scm_nil_p (SCM obj);
  101. #define SCM_VALIDATE_BOOL(pos, flag) \
  102. do { \
  103. SCM_ASSERT_TYPE (scm_is_bool (flag), flag, pos, FUNC_NAME, "boolean"); \
  104. } while (0)
  105. #define SCM_VALIDATE_BOOL_COPY(pos, flag, cvar) \
  106. do { \
  107. SCM_ASSERT (scm_is_bool (flag), flag, pos, FUNC_NAME); \
  108. cvar = scm_to_bool (flag); \
  109. } while (0)
  110. SCM_INTERNAL void scm_init_boolean (void);
  111. #endif /* SCM_BOOLEAN_H */