variable.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* Copyright 1995-1998,2000-2001,2006,2008,2011,2018
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include "boolean.h"
  19. #include "deprecation.h"
  20. #include "eq.h"
  21. #include "gsubr.h"
  22. #include "list.h"
  23. #include "ports.h"
  24. #include "smob.h"
  25. #include "variable.h"
  26. void
  27. scm_i_variable_print (SCM exp, SCM port, scm_print_state *pstate)
  28. {
  29. scm_puts ("#<variable ", port);
  30. scm_uintprint (SCM_UNPACK (exp), 16, port);
  31. scm_puts (" value: ", port);
  32. scm_iprin1 (SCM_VARIABLE_REF (exp), port, pstate);
  33. scm_putc ('>', port);
  34. }
  35. static SCM
  36. make_variable (SCM init)
  37. {
  38. return scm_cell (scm_tc7_variable, SCM_UNPACK (init));
  39. }
  40. SCM_DEFINE (scm_make_variable, "make-variable", 1, 0, 0,
  41. (SCM init),
  42. "Return a variable initialized to value @var{init}.")
  43. #define FUNC_NAME s_scm_make_variable
  44. {
  45. return make_variable (init);
  46. }
  47. #undef FUNC_NAME
  48. SCM_DEFINE (scm_make_undefined_variable, "make-undefined-variable", 0, 0, 0,
  49. (),
  50. "Return a variable that is initially unbound.")
  51. #define FUNC_NAME s_scm_make_undefined_variable
  52. {
  53. return make_variable (SCM_UNDEFINED);
  54. }
  55. #undef FUNC_NAME
  56. SCM_DEFINE (scm_variable_p, "variable?", 1, 0, 0,
  57. (SCM obj),
  58. "Return @code{#t} iff @var{obj} is a variable object, else\n"
  59. "return @code{#f}.")
  60. #define FUNC_NAME s_scm_variable_p
  61. {
  62. return scm_from_bool (SCM_VARIABLEP (obj));
  63. }
  64. #undef FUNC_NAME
  65. SCM_DEFINE (scm_variable_ref, "variable-ref", 1, 0, 0,
  66. (SCM var),
  67. "Dereference @var{var} and return its value.\n"
  68. "@var{var} must be a variable object; see @code{make-variable}\n"
  69. "and @code{make-undefined-variable}.")
  70. #define FUNC_NAME s_scm_variable_ref
  71. {
  72. SCM val;
  73. SCM_VALIDATE_VARIABLE (1, var);
  74. val = SCM_VARIABLE_REF (var);
  75. if (scm_is_eq (val, SCM_UNDEFINED))
  76. SCM_MISC_ERROR ("variable is unbound: ~S", scm_list_1 (var));
  77. return val;
  78. }
  79. #undef FUNC_NAME
  80. SCM_DEFINE (scm_variable_set_x, "variable-set!", 2, 0, 0,
  81. (SCM var, SCM val),
  82. "Set the value of the variable @var{var} to @var{val}.\n"
  83. "@var{var} must be a variable object, @var{val} can be any\n"
  84. "value. Return an unspecified value.")
  85. #define FUNC_NAME s_scm_variable_set_x
  86. {
  87. SCM_VALIDATE_VARIABLE (1, var);
  88. SCM_VARIABLE_SET (var, val);
  89. return SCM_UNSPECIFIED;
  90. }
  91. #undef FUNC_NAME
  92. SCM_DEFINE (scm_variable_unset_x, "variable-unset!", 1, 0, 0,
  93. (SCM var),
  94. "Ensure that @var{var} is not bound to a value.\n"
  95. "@var{var} must be a variable object.")
  96. #define FUNC_NAME s_scm_variable_unset_x
  97. {
  98. SCM_VALIDATE_VARIABLE (1, var);
  99. SCM_VARIABLE_SET (var, SCM_UNDEFINED);
  100. return SCM_UNSPECIFIED;
  101. }
  102. #undef FUNC_NAME
  103. SCM_DEFINE (scm_variable_bound_p, "variable-bound?", 1, 0, 0,
  104. (SCM var),
  105. "Return @code{#t} iff @var{var} is bound to a value.\n"
  106. "Throws an error if @var{var} is not a variable object.")
  107. #define FUNC_NAME s_scm_variable_bound_p
  108. {
  109. SCM_VALIDATE_VARIABLE (1, var);
  110. return scm_from_bool (!scm_is_eq (SCM_VARIABLE_REF (var), SCM_UNDEFINED));
  111. }
  112. #undef FUNC_NAME
  113. void
  114. scm_init_variable ()
  115. {
  116. #include "variable.x"
  117. }