*** empty log message ***
[bpt/guile.git] / libguile / evalext.c
CommitLineData
93f26b7b 1/* Copyright (C) 1998,1999,2000,2001,2002, 2003 Free Software Foundation, Inc.
40cf7e92 2 *
73be1d9e
MV
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.
40cf7e92 7 *
73be1d9e
MV
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.
40cf7e92 12 *
73be1d9e
MV
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
1bbd0b84 17
1bbd0b84 18
40cf7e92
MD
19\f
20
a0599745
MD
21#include "libguile/_scm.h"
22#include "libguile/eval.h"
23#include "libguile/macros.h"
24#include "libguile/modules.h"
7e73eaee 25#include "libguile/fluids.h"
40cf7e92 26
a0599745
MD
27#include "libguile/validate.h"
28#include "libguile/evalext.h"
40cf7e92 29
a70fb265
MD
30SCM_SYMBOL (scm_sym_setter, "setter");
31
32SCM
e81d98ec 33scm_m_generalized_set_x (SCM xorig, SCM env SCM_UNUSED)
a70fb265
MD
34{
35 SCM x = SCM_CDR (xorig);
160bb34a 36 SCM_ASSYNT (2 == scm_ilength (x), scm_s_expression, scm_s_set_x);
0c95b57d 37 if (SCM_SYMBOLP (SCM_CAR (x)))
a70fb265 38 return scm_cons (SCM_IM_SET_X, x);
0c95b57d 39 else if (SCM_CONSP (SCM_CAR (x)))
1afff620
KN
40 return scm_cons (scm_list_2 (scm_sym_setter, SCM_CAAR (x)),
41 scm_append (scm_list_2 (SCM_CDAR (x), SCM_CDR (x))));
db4b4ca6
DH
42 else
43 scm_misc_error (scm_s_set_x, scm_s_variable, SCM_EOL);
a70fb265
MD
44}
45
5ec1d2c8 46SCM_DEFINE (scm_defined_p, "defined?", 1, 1, 0,
1bbd0b84 47 (SCM sym, SCM env),
67dc6a4e 48 "Return @code{#t} if @var{sym} is defined in the lexical "
826e91f3
MV
49 "environment @var{env}. When @var{env} is not specified, "
50 "look in the top-level environment as defined by the "
67dc6a4e 51 "current module.")
5ec1d2c8 52#define FUNC_NAME s_scm_defined_p
40cf7e92 53{
86d31dfe 54 SCM var;
40cf7e92 55
34d19ef6 56 SCM_VALIDATE_SYMBOL (1, sym);
40cf7e92 57
b325a6c8 58 if (SCM_UNBNDP (env))
86d31dfe
MV
59 var = scm_sym2var (sym, scm_current_module_lookup_closure (),
60 SCM_BOOL_F);
b325a6c8
MD
61 else
62 {
63 SCM frames = env;
64 register SCM b;
65 for (; SCM_NIMP (frames); frames = SCM_CDR (frames))
66 {
1bbd0b84 67 SCM_ASSERT (SCM_CONSP (frames), env, SCM_ARG2, FUNC_NAME);
b325a6c8
MD
68 b = SCM_CAR (frames);
69 if (SCM_NFALSEP (scm_procedure_p (b)))
70 break;
c1bfcf60 71 SCM_ASSERT (SCM_CONSP (b), env, SCM_ARG2, FUNC_NAME);
b325a6c8
MD
72 for (b = SCM_CAR (b); SCM_NIMP (b); b = SCM_CDR (b))
73 {
74 if (SCM_NCONSP (b))
75 {
54778cd3 76 if (SCM_EQ_P (b, sym))
b325a6c8
MD
77 return SCM_BOOL_T;
78 else
79 break;
80 }
54778cd3 81 if (SCM_EQ_P (SCM_CAR (b), sym))
b325a6c8
MD
82 return SCM_BOOL_T;
83 }
84 }
86d31dfe
MV
85 var = scm_sym2var (sym,
86 SCM_NIMP (frames) ? SCM_CAR (frames) : SCM_BOOL_F,
87 SCM_BOOL_F);
b325a6c8
MD
88 }
89
86d31dfe 90 return (SCM_FALSEP (var) || SCM_UNBNDP (SCM_VARIABLE_REF (var))
b325a6c8
MD
91 ? SCM_BOOL_F
92 : SCM_BOOL_T);
40cf7e92 93}
1bbd0b84 94#undef FUNC_NAME
40cf7e92 95
63dd3413 96#if (SCM_ENABLE_DEPRECATED == 1)
b8229a3b
MS
97
98SCM_SYNTAX (s_undefine, "undefine", scm_makacro, scm_m_undefine);
40cf7e92
MD
99
100SCM
6e8d25a6 101scm_m_undefine (SCM x, SCM env)
40cf7e92
MD
102{
103 SCM arg1 = x;
104 x = SCM_CDR (x);
160bb34a 105 SCM_ASSYNT (SCM_TOP_LEVEL (env), "bad placement ", s_undefine);
54778cd3 106 SCM_ASSYNT (SCM_CONSP (x) && SCM_NULLP (SCM_CDR (x)),
160bb34a 107 scm_s_expression, s_undefine);
40cf7e92 108 x = SCM_CAR (x);
160bb34a 109 SCM_ASSYNT (SCM_SYMBOLP (x), scm_s_variable, s_undefine);
86d31dfe
MV
110 arg1 = scm_sym2var (x, scm_env_top_level (env), SCM_BOOL_F);
111 SCM_ASSYNT (SCM_NFALSEP (arg1) && !SCM_UNBNDP (SCM_VARIABLE_REF (arg1)),
160bb34a 112 "variable already unbound ", s_undefine);
86d31dfe 113 SCM_VARIABLE_SET (arg1, SCM_UNDEFINED);
40cf7e92 114#ifdef SICP
86d31dfe 115 return x;
40cf7e92
MD
116#else
117 return SCM_UNSPECIFIED;
118#endif
119}
120
63dd3413
DH
121#endif
122
1bbd0b84 123SCM_REGISTER_PROC (s_map_in_order, "map-in-order", 2, 0, 1, scm_map);
285302e1 124
93f26b7b
MD
125SCM_DEFINE (scm_self_evaluating_p, "self-evaluating?", 1, 0, 0,
126 (SCM obj),
127 "Return #t for objects which Guile considers self-evaluating")
128#define FUNC_NAME s_scm_self_evaluating_p
129{
130 switch (SCM_ITAG3 (obj))
131 {
132 case scm_tc3_int_1:
133 case scm_tc3_int_2:
134 /* inum */
135 return SCM_BOOL_T;
136 case scm_tc3_imm24:
137 /* characters, booleans, other immediates */
138 return SCM_BOOL (!SCM_NULLP (obj));
139 case scm_tc3_cons:
140 switch (SCM_TYP7 (obj))
141 {
142 case scm_tcs_closures:
143 case scm_tc7_vector:
144 case scm_tc7_wvect:
9e443ab6 145#if SCM_HAVE_ARRAYS
93f26b7b
MD
146 case scm_tc7_bvect:
147 case scm_tc7_byvect:
148 case scm_tc7_svect:
149 case scm_tc7_ivect:
150 case scm_tc7_uvect:
151 case scm_tc7_fvect:
152 case scm_tc7_dvect:
153 case scm_tc7_cvect:
65fb6c49 154#if SCM_SIZEOF_LONG_LONG != 0
93f26b7b
MD
155 case scm_tc7_llvect:
156#endif
157#endif
158 case scm_tc7_string:
159 case scm_tc7_smob:
160 case scm_tc7_cclo:
161 case scm_tc7_pws:
162 case scm_tcs_subrs:
163 case scm_tcs_struct:
164 return SCM_BOOL_T;
165 default:
166 return SCM_BOOL_F;
167 }
168 }
169 SCM_MISC_ERROR ("Internal error: Object ~S has unknown type",
170 scm_list_1 (obj));
171 return SCM_UNSPECIFIED; /* never reached */
172}
173#undef FUNC_NAME
174
40cf7e92
MD
175void
176scm_init_evalext ()
177{
a70fb265 178 scm_make_synt (scm_s_set_x, scm_makmmacro, scm_m_generalized_set_x);
a0599745 179#include "libguile/evalext.x"
40cf7e92 180}
89e00824
ML
181
182/*
183 Local Variables:
184 c-file-style: "gnu"
185 End:
186*/