variable.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program 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
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. * Boston, MA 02111-1307 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. #include <stdio.h>
  42. #include "libguile/_scm.h"
  43. #include "libguile/eq.h"
  44. #include "libguile/ports.h"
  45. #include "libguile/root.h"
  46. #include "libguile/smob.h"
  47. #include "libguile/validate.h"
  48. #include "libguile/variable.h"
  49. static int
  50. prin_var (SCM exp,SCM port,scm_print_state *pstate)
  51. {
  52. scm_puts ("#<variable ", port);
  53. scm_intprint(SCM_UNPACK (exp), 16, port);
  54. {
  55. SCM val_cell;
  56. val_cell = SCM_CDR(exp);
  57. if (!SCM_UNBNDP (SCM_CAR (val_cell)))
  58. {
  59. scm_puts (" name: ", port);
  60. scm_iprin1 (SCM_CAR (val_cell), port, pstate);
  61. }
  62. scm_puts (" binding: ", port);
  63. scm_iprin1 (SCM_CDR (val_cell), port, pstate);
  64. }
  65. scm_putc('>', port);
  66. return 1;
  67. }
  68. static SCM
  69. scm_markvar (SCM ptr)
  70. {
  71. return SCM_CDR (ptr);
  72. }
  73. static SCM
  74. var_equal (SCM var1, SCM var2)
  75. {
  76. return scm_equal_p (SCM_CDR (var1), SCM_CDR (var2));
  77. }
  78. int scm_tc16_variable;
  79. static SCM anonymous_variable_sym;
  80. static SCM
  81. make_vcell_variable (SCM vcell)
  82. {
  83. SCM_RETURN_NEWSMOB (scm_tc16_variable, SCM_UNPACK (vcell));
  84. }
  85. SCM_DEFINE (scm_make_variable, "make-variable", 1, 1, 0,
  86. (SCM init, SCM name_hint),
  87. "Return a variable object initialized to value INIT.\n"
  88. "If given, uses NAME-HINT as its internal (debugging)\n"
  89. "name, otherwise just treat it as an anonymous variable.\n"
  90. "Remember, of course, that multiple bindings to the same\n"
  91. "variable may exist, so NAME-HINT is just that---a hint.\n")
  92. #define FUNC_NAME s_scm_make_variable
  93. {
  94. SCM val_cell;
  95. if (SCM_UNBNDP (name_hint))
  96. name_hint = anonymous_variable_sym;
  97. SCM_NEWCELL(val_cell);
  98. SCM_DEFER_INTS;
  99. SCM_SETCAR (val_cell, name_hint);
  100. SCM_SETCDR (val_cell, init);
  101. SCM_ALLOW_INTS;
  102. return make_vcell_variable (val_cell);
  103. }
  104. #undef FUNC_NAME
  105. SCM_DEFINE (scm_make_undefined_variable, "make-undefined-variable", 0, 1, 0,
  106. (SCM name_hint),
  107. "Return a variable object initialized to an undefined value.\n"
  108. "If given, uses NAME-HINT as its internal (debugging)\n"
  109. "name, otherwise just treat it as an anonymous variable.\n"
  110. "Remember, of course, that multiple bindings to the same\n"
  111. "variable may exist, so NAME-HINT is just that---a hint.\n")
  112. #define FUNC_NAME s_scm_make_undefined_variable
  113. {
  114. SCM vcell;
  115. if (SCM_UNBNDP (name_hint))
  116. name_hint = anonymous_variable_sym;
  117. SCM_NEWCELL (vcell);
  118. SCM_DEFER_INTS;
  119. SCM_SETCAR (vcell, name_hint);
  120. SCM_SETCDR (vcell, SCM_UNDEFINED);
  121. SCM_ALLOW_INTS;
  122. return make_vcell_variable (vcell);
  123. }
  124. #undef FUNC_NAME
  125. SCM_DEFINE (scm_variable_p, "variable?", 1, 0, 0,
  126. (SCM obj),
  127. "Return #t iff OBJ is a variable object, else return #f\n")
  128. #define FUNC_NAME s_scm_variable_p
  129. {
  130. return SCM_BOOL (SCM_VARIABLEP (obj));
  131. }
  132. #undef FUNC_NAME
  133. SCM_DEFINE (scm_variable_ref, "variable-ref", 1, 0, 0,
  134. (SCM var),
  135. "Dereference VAR and return its value.\n"
  136. "VAR must be a variable object; see `make-variable' and\n"
  137. "`make-undefined-variable'")
  138. #define FUNC_NAME s_scm_variable_ref
  139. {
  140. SCM_VALIDATE_VARIABLE (1, var);
  141. return SCM_CDR (SCM_CDR (var));
  142. }
  143. #undef FUNC_NAME
  144. SCM_DEFINE (scm_variable_set_x, "variable-set!", 2, 0, 0,
  145. (SCM var, SCM val),
  146. "Set the value of the variable VAR to VAL.\n"
  147. "VAR must be a variable object, VAL can be any value.\n"
  148. "Returns an unspecified value.\n")
  149. #define FUNC_NAME s_scm_variable_set_x
  150. {
  151. SCM_VALIDATE_VARIABLE (1,var);
  152. SCM_SETCDR (SCM_CDR (var), val);
  153. return SCM_UNSPECIFIED;
  154. }
  155. #undef FUNC_NAME
  156. SCM_DEFINE (scm_builtin_variable, "builtin-variable", 1, 0, 0,
  157. (SCM name),
  158. "Return the built-in variable with the name NAME.\n"
  159. "NAME must be a symbol (not a string).\n"
  160. "Then use `variable-ref' to access its value.\n")
  161. #define FUNC_NAME s_scm_builtin_variable
  162. {
  163. SCM vcell;
  164. SCM var_slot;
  165. SCM_VALIDATE_SYMBOL (1,name);
  166. vcell = scm_sym2vcell (name, SCM_BOOL_F, SCM_BOOL_T);
  167. if (SCM_FALSEP (vcell))
  168. return SCM_BOOL_F;
  169. scm_intern_symbol (scm_symhash_vars, name);
  170. var_slot = scm_sym2ovcell (name, scm_symhash_vars);
  171. SCM_DEFER_INTS;
  172. if (SCM_IMP (SCM_CDR (var_slot))
  173. || !SCM_EQ_P (SCM_VARVCELL (var_slot), vcell))
  174. SCM_SETCDR (var_slot, make_vcell_variable (vcell));
  175. SCM_ALLOW_INTS;
  176. return SCM_CDR (var_slot);
  177. }
  178. #undef FUNC_NAME
  179. SCM_DEFINE (scm_variable_bound_p, "variable-bound?", 1, 0, 0,
  180. (SCM var),
  181. "Return #t iff VAR is bound to a value.\n"
  182. "Throws an error if VAR is not a variable object.\n")
  183. #define FUNC_NAME s_scm_variable_bound_p
  184. {
  185. SCM_VALIDATE_VARIABLE (1,var);
  186. return SCM_NEGATE_BOOL(SCM_UNBNDP (SCM_CDR (SCM_VARVCELL (var))));
  187. }
  188. #undef FUNC_NAME
  189. void
  190. scm_init_variable ()
  191. {
  192. scm_tc16_variable = scm_make_smob_type_mfpe ("variable", 0,
  193. scm_markvar, NULL, prin_var, var_equal);
  194. anonymous_variable_sym = SCM_CAR (scm_sysintern ("anonymous-variable", SCM_UNDEFINED));
  195. #include "libguile/variable.x"
  196. }
  197. /*
  198. Local Variables:
  199. c-file-style: "gnu"
  200. End:
  201. */