variable.c 4.0 KB

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