Add `ChangeLog-2008' files to the distribution.
[bpt/guile.git] / libguile / variable.c
1 /* Copyright (C) 1995,1996,1997,1998,2000,2001, 2006 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
18
19 \f
20
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
28 #include "libguile/validate.h"
29 #include "libguile/variable.h"
30 \f
31
32 void
33 scm_i_variable_print (SCM exp, SCM port, scm_print_state *pstate)
34 {
35 scm_puts ("#<variable ", port);
36 scm_uintprint (SCM_UNPACK (exp), 16, port);
37 scm_puts (" value: ", port);
38 scm_iprin1 (SCM_VARIABLE_REF (exp), port, pstate);
39 scm_putc('>', port);
40 }
41
42 \f
43
44 static SCM
45 make_variable (SCM init)
46 {
47 return scm_cell (scm_tc7_variable, SCM_UNPACK (init));
48 }
49
50 SCM_DEFINE (scm_make_variable, "make-variable", 1, 0, 0,
51 (SCM init),
52 "Return a variable initialized to value @var{init}.")
53 #define FUNC_NAME s_scm_make_variable
54 {
55 return make_variable (init);
56 }
57 #undef FUNC_NAME
58
59
60 SCM_DEFINE (scm_make_undefined_variable, "make-undefined-variable", 0, 0, 0,
61 (),
62 "Return a variable that is initially unbound.")
63 #define FUNC_NAME s_scm_make_undefined_variable
64 {
65 return make_variable (SCM_UNDEFINED);
66 }
67 #undef FUNC_NAME
68
69
70 SCM_DEFINE (scm_variable_p, "variable?", 1, 0, 0,
71 (SCM obj),
72 "Return @code{#t} iff @var{obj} is a variable object, else\n"
73 "return @code{#f}.")
74 #define FUNC_NAME s_scm_variable_p
75 {
76 return scm_from_bool (SCM_VARIABLEP (obj));
77 }
78 #undef FUNC_NAME
79
80
81 SCM_DEFINE (scm_variable_ref, "variable-ref", 1, 0, 0,
82 (SCM var),
83 "Dereference @var{var} and return its value.\n"
84 "@var{var} must be a variable object; see @code{make-variable}\n"
85 "and @code{make-undefined-variable}.")
86 #define FUNC_NAME s_scm_variable_ref
87 {
88 SCM val;
89 SCM_VALIDATE_VARIABLE (1, var);
90 val = SCM_VARIABLE_REF (var);
91 if (val == SCM_UNDEFINED)
92 SCM_MISC_ERROR ("variable is unbound: ~S", scm_list_1 (var));
93 return val;
94 }
95 #undef FUNC_NAME
96
97 SCM_DEFINE (scm_variable_set_x, "variable-set!", 2, 0, 0,
98 (SCM var, SCM val),
99 "Set the value of the variable @var{var} to @var{val}.\n"
100 "@var{var} must be a variable object, @var{val} can be any\n"
101 "value. Return an unspecified value.")
102 #define FUNC_NAME s_scm_variable_set_x
103 {
104 SCM_VALIDATE_VARIABLE (1, var);
105 SCM_VARIABLE_SET (var, val);
106 return SCM_UNSPECIFIED;
107 }
108 #undef FUNC_NAME
109
110 SCM_DEFINE (scm_variable_bound_p, "variable-bound?", 1, 0, 0,
111 (SCM var),
112 "Return @code{#t} iff @var{var} is bound to a value.\n"
113 "Throws an error if @var{var} is not a variable object.")
114 #define FUNC_NAME s_scm_variable_bound_p
115 {
116 SCM_VALIDATE_VARIABLE (1, var);
117 return scm_from_bool (SCM_VARIABLE_REF (var) != SCM_UNDEFINED);
118 }
119 #undef FUNC_NAME
120
121
122 void
123 scm_init_variable ()
124 {
125 #include "libguile/variable.x"
126 }
127
128 /*
129 Local Variables:
130 c-file-style: "gnu"
131 End:
132 */