boolean.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Copyright (C) 1995, 1996, 2000, 2001, 2006, 2008-2011, 2018
  2. * Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include "libguile/_scm.h"
  23. #include "libguile/validate.h"
  24. #include "libguile/boolean.h"
  25. #include "libguile/tags.h"
  26. #include "verify.h"
  27. /*
  28. * These compile-time tests verify the properties needed for the
  29. * efficient test macros defined in boolean.h, which are defined in
  30. * terms of the SCM_MATCHES_BITS_IN_COMMON macro.
  31. *
  32. * See the comments preceeding the definitions of SCM_BOOL_F and
  33. * SCM_MATCHES_BITS_IN_COMMON in tags.h for more information.
  34. */
  35. verify (SCM_BITS_DIFFER_IN_EXACTLY_ONE_BIT_POSITION \
  36. (SCM_BOOL_F_BITS, SCM_BOOL_T_BITS));
  37. verify (SCM_BITS_DIFFER_IN_EXACTLY_ONE_BIT_POSITION \
  38. (SCM_ELISP_NIL_BITS, SCM_BOOL_F_BITS));
  39. verify (SCM_BITS_DIFFER_IN_EXACTLY_ONE_BIT_POSITION \
  40. (SCM_ELISP_NIL_BITS, SCM_EOL_BITS));
  41. verify (SCM_BITS_DIFFER_IN_EXACTLY_TWO_BIT_POSITIONS \
  42. (SCM_ELISP_NIL_BITS, SCM_BOOL_F_BITS, SCM_BOOL_T_BITS, \
  43. SCM_XXX_ANOTHER_BOOLEAN_DONT_USE_0));
  44. verify (SCM_BITS_DIFFER_IN_EXACTLY_TWO_BIT_POSITIONS \
  45. (SCM_ELISP_NIL_BITS, SCM_BOOL_F_BITS, SCM_EOL_BITS, \
  46. SCM_XXX_ANOTHER_LISP_FALSE_DONT_USE));
  47. SCM_DEFINE (scm_not, "not", 1, 0, 0,
  48. (SCM x),
  49. "Return @code{#t} iff @var{x} is false, else return @code{#f}.")
  50. #define FUNC_NAME s_scm_not
  51. {
  52. return scm_from_bool (scm_is_false (x));
  53. }
  54. #undef FUNC_NAME
  55. SCM_DEFINE (scm_nil_p, "nil?", 1, 0, 0,
  56. (SCM x),
  57. "Return @code{#t} if @var{x} would be interpreted as @code{nil}\n"
  58. "by Emacs Lisp code, else return @code{#f}.\n"
  59. "\n"
  60. "@example\n"
  61. "(nil? #nil) @result{} #t\n"
  62. "(nil? #f) @result{} #t\n"
  63. "(nil? '()) @result{} #t\n"
  64. "(nil? 3) @result{} #f\n"
  65. "@end example")
  66. #define FUNC_NAME s_scm_nil_p
  67. {
  68. return scm_from_bool (scm_is_lisp_false (x));
  69. }
  70. #undef FUNC_NAME
  71. SCM_DEFINE (scm_boolean_p, "boolean?", 1, 0, 0,
  72. (SCM obj),
  73. "Return @code{#t} iff @var{obj} is @code{#t} or false.")
  74. #define FUNC_NAME s_scm_boolean_p
  75. {
  76. return scm_from_bool (scm_is_bool (obj));
  77. }
  78. #undef FUNC_NAME
  79. int
  80. scm_to_bool (SCM x)
  81. {
  82. if (scm_is_false (x))
  83. return 0;
  84. else if (scm_is_eq (x, SCM_BOOL_T))
  85. return 1;
  86. else
  87. scm_wrong_type_arg (NULL, 0, x);
  88. }
  89. /* We keep this primitive as a function in addition to the same-named macro
  90. because some applications (e.g., GNU LilyPond 2.13.9) expect it to be a
  91. function. */
  92. #undef scm_is_bool
  93. int
  94. scm_is_bool (SCM obj)
  95. {
  96. /* This must match the macro definition of `scm_is_bool ()'. */
  97. return scm_is_bool_or_nil (obj);
  98. }
  99. void
  100. scm_init_boolean ()
  101. {
  102. #include "libguile/boolean.x"
  103. }
  104. /*
  105. Local Variables:
  106. c-file-style: "gnu"
  107. End:
  108. */