Reify bytevector? in the correct module
[bpt/guile.git] / libguile / variable.c
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
19
20 \f
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include "libguile/_scm.h"
26 #include "libguile/eq.h"
27 #include "libguile/ports.h"
28 #include "libguile/root.h"
29 #include "libguile/smob.h"
30 #include "libguile/deprecation.h"
31
32 #include "libguile/validate.h"
33 #include "libguile/variable.h"
34 \f
35
36 void
37 scm_i_variable_print (SCM exp, SCM port, scm_print_state *pstate)
38 {
39 scm_puts_unlocked ("#<variable ", port);
40 scm_uintprint (SCM_UNPACK (exp), 16, port);
41 scm_puts_unlocked (" value: ", port);
42 scm_iprin1 (SCM_VARIABLE_REF (exp), port, pstate);
43 scm_putc_unlocked('>', port);
44 }
45
46 \f
47
48 static SCM
49 make_variable (SCM init)
50 {
51 return scm_cell (scm_tc7_variable, SCM_UNPACK (init));
52 }
53
54 SCM_DEFINE (scm_make_variable, "make-variable", 1, 0, 0,
55 (SCM init),
56 "Return a variable initialized to value @var{init}.")
57 #define FUNC_NAME s_scm_make_variable
58 {
59 return make_variable (init);
60 }
61 #undef FUNC_NAME
62
63
64 SCM_DEFINE (scm_make_undefined_variable, "make-undefined-variable", 0, 0, 0,
65 (),
66 "Return a variable that is initially unbound.")
67 #define FUNC_NAME s_scm_make_undefined_variable
68 {
69 return make_variable (SCM_UNDEFINED);
70 }
71 #undef FUNC_NAME
72
73
74 SCM_DEFINE (scm_variable_p, "variable?", 1, 0, 0,
75 (SCM obj),
76 "Return @code{#t} iff @var{obj} is a variable object, else\n"
77 "return @code{#f}.")
78 #define FUNC_NAME s_scm_variable_p
79 {
80 return scm_from_bool (SCM_VARIABLEP (obj));
81 }
82 #undef FUNC_NAME
83
84
85 SCM_DEFINE (scm_variable_ref, "variable-ref", 1, 0, 0,
86 (SCM var),
87 "Dereference @var{var} and return its value.\n"
88 "@var{var} must be a variable object; see @code{make-variable}\n"
89 "and @code{make-undefined-variable}.")
90 #define FUNC_NAME s_scm_variable_ref
91 {
92 SCM val;
93 SCM_VALIDATE_VARIABLE (1, var);
94 val = SCM_VARIABLE_REF (var);
95 if (scm_is_eq (val, SCM_UNDEFINED))
96 SCM_MISC_ERROR ("variable is unbound: ~S", scm_list_1 (var));
97 return val;
98 }
99 #undef FUNC_NAME
100
101 SCM_DEFINE (scm_variable_set_x, "variable-set!", 2, 0, 0,
102 (SCM var, SCM val),
103 "Set the value of the variable @var{var} to @var{val}.\n"
104 "@var{var} must be a variable object, @var{val} can be any\n"
105 "value. Return an unspecified value.")
106 #define FUNC_NAME s_scm_variable_set_x
107 {
108 SCM_VALIDATE_VARIABLE (1, var);
109 SCM_VARIABLE_SET (var, val);
110 return SCM_UNSPECIFIED;
111 }
112 #undef FUNC_NAME
113
114 SCM_DEFINE (scm_variable_unset_x, "variable-unset!", 1, 0, 0,
115 (SCM var),
116 "Ensure that @var{var} is not bound to a value.\n"
117 "@var{var} must be a variable object.")
118 #define FUNC_NAME s_scm_variable_unset_x
119 {
120 SCM_VALIDATE_VARIABLE (1, var);
121 SCM_VARIABLE_SET (var, SCM_UNDEFINED);
122 return SCM_UNSPECIFIED;
123 }
124 #undef FUNC_NAME
125
126 SCM_DEFINE (scm_variable_bound_p, "variable-bound?", 1, 0, 0,
127 (SCM var),
128 "Return @code{#t} iff @var{var} is bound to a value.\n"
129 "Throws an error if @var{var} is not a variable object.")
130 #define FUNC_NAME s_scm_variable_bound_p
131 {
132 SCM_VALIDATE_VARIABLE (1, var);
133 return scm_from_bool (!scm_is_eq (SCM_VARIABLE_REF (var), SCM_UNDEFINED));
134 }
135 #undef FUNC_NAME
136
137
138 void
139 scm_init_variable ()
140 {
141 #include "libguile/variable.x"
142 }
143
144 /*
145 Local Variables:
146 c-file-style: "gnu"
147 End:
148 */