1 /* Copyright (C) 1995,1996,1997,1998,2000,2001, 2006, 2008, 2011 Free Software Foundation, Inc.
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.
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.
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
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"
32 #include "libguile/validate.h"
33 #include "libguile/variable.h"
37 scm_i_variable_print (SCM exp
, SCM port
, scm_print_state
*pstate
)
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
);
49 make_variable (SCM init
)
51 return scm_cell (scm_tc7_variable
, SCM_UNPACK (init
));
54 SCM_DEFINE (scm_make_variable
, "make-variable", 1, 0, 0,
56 "Return a variable initialized to value @var{init}.")
57 #define FUNC_NAME s_scm_make_variable
59 return make_variable (init
);
64 SCM_DEFINE (scm_make_undefined_variable
, "make-undefined-variable", 0, 0, 0,
66 "Return a variable that is initially unbound.")
67 #define FUNC_NAME s_scm_make_undefined_variable
69 return make_variable (SCM_UNDEFINED
);
74 SCM_DEFINE (scm_variable_p
, "variable?", 1, 0, 0,
76 "Return @code{#t} iff @var{obj} is a variable object, else\n"
78 #define FUNC_NAME s_scm_variable_p
80 return scm_from_bool (SCM_VARIABLEP (obj
));
85 SCM_DEFINE (scm_variable_ref
, "variable-ref", 1, 0, 0,
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
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
));
101 SCM_DEFINE (scm_variable_set_x
, "variable-set!", 2, 0, 0,
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
108 SCM_VALIDATE_VARIABLE (1, var
);
109 SCM_VARIABLE_SET (var
, val
);
110 return SCM_UNSPECIFIED
;
114 SCM_DEFINE (scm_variable_unset_x
, "variable-unset!", 1, 0, 0,
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
120 SCM_VALIDATE_VARIABLE (1, var
);
121 SCM_VARIABLE_SET (var
, SCM_UNDEFINED
);
122 return SCM_UNSPECIFIED
;
126 SCM_DEFINE (scm_variable_bound_p
, "variable-bound?", 1, 0, 0,
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
132 SCM_VALIDATE_VARIABLE (1, var
);
133 return scm_from_bool (!scm_is_eq (SCM_VARIABLE_REF (var
), SCM_UNDEFINED
));
141 #include "libguile/variable.x"