variable.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Copyright (C) 1995,1996,1997,1998,2000,2001, 2006, 2008 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
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but 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 02110-1301 USA
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include "libguile/_scm.h"
  21. #include "libguile/eq.h"
  22. #include "libguile/ports.h"
  23. #include "libguile/root.h"
  24. #include "libguile/smob.h"
  25. #include "libguile/deprecation.h"
  26. #include "libguile/validate.h"
  27. #include "libguile/variable.h"
  28. void
  29. scm_i_variable_print (SCM exp, SCM port, scm_print_state *pstate)
  30. {
  31. scm_puts ("#<variable ", port);
  32. scm_uintprint (SCM_UNPACK (exp), 16, port);
  33. scm_puts (" value: ", port);
  34. scm_iprin1 (SCM_VARIABLE_REF (exp), port, pstate);
  35. scm_putc('>', port);
  36. }
  37. static SCM
  38. make_variable (SCM init)
  39. {
  40. return scm_cell (scm_tc7_variable, SCM_UNPACK (init));
  41. }
  42. SCM_DEFINE (scm_make_variable, "make-variable", 1, 0, 0,
  43. (SCM init),
  44. "Return a variable initialized to value @var{init}.")
  45. #define FUNC_NAME s_scm_make_variable
  46. {
  47. return make_variable (init);
  48. }
  49. #undef FUNC_NAME
  50. SCM_DEFINE (scm_make_undefined_variable, "make-undefined-variable", 0, 0, 0,
  51. (),
  52. "Return a variable that is initially unbound.")
  53. #define FUNC_NAME s_scm_make_undefined_variable
  54. {
  55. return make_variable (SCM_UNDEFINED);
  56. }
  57. #undef FUNC_NAME
  58. SCM_DEFINE (scm_variable_p, "variable?", 1, 0, 0,
  59. (SCM obj),
  60. "Return @code{#t} iff @var{obj} is a variable object, else\n"
  61. "return @code{#f}.")
  62. #define FUNC_NAME s_scm_variable_p
  63. {
  64. return scm_from_bool (SCM_VARIABLEP (obj));
  65. }
  66. #undef FUNC_NAME
  67. SCM_DEFINE (scm_variable_ref, "variable-ref", 1, 0, 0,
  68. (SCM var),
  69. "Dereference @var{var} and return its value.\n"
  70. "@var{var} must be a variable object; see @code{make-variable}\n"
  71. "and @code{make-undefined-variable}.")
  72. #define FUNC_NAME s_scm_variable_ref
  73. {
  74. SCM val;
  75. SCM_VALIDATE_VARIABLE (1, var);
  76. val = SCM_VARIABLE_REF (var);
  77. if (val == SCM_UNDEFINED)
  78. SCM_MISC_ERROR ("variable is unbound: ~S", scm_list_1 (var));
  79. return val;
  80. }
  81. #undef FUNC_NAME
  82. SCM_DEFINE (scm_variable_set_x, "variable-set!", 2, 0, 0,
  83. (SCM var, SCM val),
  84. "Set the value of the variable @var{var} to @var{val}.\n"
  85. "@var{var} must be a variable object, @var{val} can be any\n"
  86. "value. Return an unspecified value.")
  87. #define FUNC_NAME s_scm_variable_set_x
  88. {
  89. SCM_VALIDATE_VARIABLE (1, var);
  90. SCM_VARIABLE_SET (var, val);
  91. return SCM_UNSPECIFIED;
  92. }
  93. #undef FUNC_NAME
  94. SCM_DEFINE (scm_variable_bound_p, "variable-bound?", 1, 0, 0,
  95. (SCM var),
  96. "Return @code{#t} iff @var{var} is bound to a value.\n"
  97. "Throws an error if @var{var} is not a variable object.")
  98. #define FUNC_NAME s_scm_variable_bound_p
  99. {
  100. SCM_VALIDATE_VARIABLE (1, var);
  101. return scm_from_bool (SCM_VARIABLE_REF (var) != SCM_UNDEFINED);
  102. }
  103. #undef FUNC_NAME
  104. void
  105. scm_init_variable ()
  106. {
  107. #include "libguile/variable.x"
  108. }
  109. /*
  110. Local Variables:
  111. c-file-style: "gnu"
  112. End:
  113. */