Update README on using libraries in non-standard locations
[bpt/guile.git] / libguile / procs.c
CommitLineData
ac51e74b 1/* Copyright (C) 1995,1996,1997,1999,2000,2001, 2006, 2008, 2009 Free Software Foundation, Inc.
0f2d19dd 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.
0f2d19dd 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.
0f2d19dd 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
92205699 15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73be1d9e 16 */
1bbd0b84 17
1bbd0b84 18
0f2d19dd 19\f
dbb605f5
LC
20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
0f2d19dd 23
a0599745 24#include "libguile/_scm.h"
0f2d19dd 25
a0599745
MD
26#include "libguile/objects.h"
27#include "libguile/strings.h"
28#include "libguile/vectors.h"
0717dfd8 29#include "libguile/smob.h"
c88a8162 30#include "libguile/deprecation.h"
bdc88419 31
a0599745
MD
32#include "libguile/validate.h"
33#include "libguile/procs.h"
3fd8807e 34#include "libguile/procprop.h"
53e28ed9 35#include "libguile/objcodes.h"
7e580328 36#include "libguile/programs.h"
0f2d19dd
JB
37\f
38
39
40/* {Procedures}
41 */
42
9de33deb 43
ac51e74b 44SCM
c014a02e 45scm_c_make_subr (const char *name, long type, SCM (*fcn) ())
0f2d19dd 46{
0f2d19dd 47 register SCM z;
17df23e3 48 SCM sname;
ac51e74b 49 SCM *meta_info;
9de33deb 50
e0923570 51 meta_info = scm_gc_malloc (2 * sizeof (*meta_info), "subr meta-info");
17df23e3
AW
52 sname = scm_from_locale_symbol (name);
53 meta_info[0] = sname;
ac51e74b
LC
54 meta_info[1] = SCM_EOL; /* properties */
55
56 z = scm_double_cell ((scm_t_bits) type, (scm_t_bits) fcn,
57 0 /* generic */, (scm_t_bits) meta_info);
9de33deb 58
17df23e3
AW
59 scm_remember_upto_here_1 (sname);
60
0f2d19dd
JB
61 return z;
62}
63
c88a8162 64SCM
c014a02e 65scm_c_define_subr (const char *name, long type, SCM (*fcn) ())
c88a8162
MV
66{
67 SCM subr = scm_c_make_subr (name, type, fcn);
cce8b2ce 68 scm_define (SCM_SNAME (subr), subr);
c88a8162
MV
69 return subr;
70}
71
9de33deb
MD
72/* This function isn't currently used since subrs are never freed. */
73/* *fixme* Need mutex here. */
74void
75scm_free_subr_entry (SCM subr)
76{
ac51e74b
LC
77 scm_gc_free (SCM_SUBR_META_INFO (subr), 2 * sizeof (SCM),
78 "subr meta-info");
9de33deb 79}
1cc91f1b 80
c88a8162
MV
81SCM
82scm_c_make_subr_with_generic (const char *name,
c014a02e 83 long type, SCM (*fcn) (), SCM *gf)
0f2d19dd 84{
c88a8162 85 SCM subr = scm_c_make_subr (name, type, fcn);
feccd2d3 86 SCM_SET_SUBR_GENERIC_LOC (subr, gf);
c88a8162 87 return subr;
0f2d19dd
JB
88}
89
9de33deb 90SCM
c88a8162 91scm_c_define_subr_with_generic (const char *name,
c014a02e 92 long type, SCM (*fcn) (), SCM *gf)
9de33deb 93{
c88a8162 94 SCM subr = scm_c_make_subr_with_generic (name, type, fcn, gf);
cce8b2ce 95 scm_define (SCM_SNAME (subr), subr);
9de33deb
MD
96 return subr;
97}
98
9de33deb 99
3b3b36dd 100SCM_DEFINE (scm_procedure_p, "procedure?", 1, 0, 0,
8cf97abf
MG
101 (SCM obj),
102 "Return @code{#t} if @var{obj} is a procedure.")
1bbd0b84 103#define FUNC_NAME s_scm_procedure_p
0f2d19dd
JB
104{
105 if (SCM_NIMP (obj))
106 switch (SCM_TYP7 (obj))
107 {
904a077d 108 case scm_tcs_struct:
bdc88419
MD
109 if (!SCM_I_OPERATORP (obj))
110 break;
0f2d19dd 111 case scm_tcs_closures:
0f2d19dd 112 case scm_tcs_subrs:
b4cd6492 113 case scm_tc7_pws:
0f2d19dd 114 return SCM_BOOL_T;
0717dfd8 115 case scm_tc7_smob:
7888309b 116 return scm_from_bool (SCM_SMOB_DESCRIPTOR (obj).apply);
0f2d19dd
JB
117 default:
118 return SCM_BOOL_F;
119 }
120 return SCM_BOOL_F;
121}
1bbd0b84 122#undef FUNC_NAME
0f2d19dd 123
3b3b36dd 124SCM_DEFINE (scm_closure_p, "closure?", 1, 0, 0,
1bbd0b84 125 (SCM obj),
8cf97abf 126 "Return @code{#t} if @var{obj} is a closure.")
1bbd0b84 127#define FUNC_NAME s_scm_closure_p
ecdb5eb2 128{
7888309b 129 return scm_from_bool (SCM_CLOSUREP (obj));
ecdb5eb2 130}
1bbd0b84 131#undef FUNC_NAME
ecdb5eb2 132
3b3b36dd 133SCM_DEFINE (scm_thunk_p, "thunk?", 1, 0, 0,
8cf97abf
MG
134 (SCM obj),
135 "Return @code{#t} if @var{obj} is a thunk.")
1bbd0b84 136#define FUNC_NAME s_scm_thunk_p
44bd53b9
MD
137{
138 if (SCM_NIMP (obj))
b4cd6492
MD
139 {
140 again:
141 switch (SCM_TYP7 (obj))
142 {
143 case scm_tcs_closures:
d2e53ed6 144 return scm_from_bool (!scm_is_pair (SCM_CLOSURE_FORMALS (obj)));
b4cd6492
MD
145 case scm_tc7_subr_0:
146 case scm_tc7_subr_1o:
147 case scm_tc7_lsubr:
148 case scm_tc7_rpsubr:
149 case scm_tc7_asubr:
b4cd6492 150 return SCM_BOOL_T;
e20d7001
LC
151 case scm_tc7_gsubr:
152 return scm_from_bool (SCM_GSUBR_REQ (SCM_GSUBR_TYPE (obj)) == 0);
b4cd6492
MD
153 case scm_tc7_pws:
154 obj = SCM_PROCEDURE (obj);
155 goto again;
156 default:
7e580328
AW
157 if (SCM_PROGRAM_P (obj) && SCM_PROGRAM_DATA (obj)->nargs == 0)
158 return SCM_BOOL_T;
159 /* otherwise fall through */
b4cd6492
MD
160 }
161 }
44bd53b9
MD
162 return SCM_BOOL_F;
163}
1bbd0b84 164#undef FUNC_NAME
44bd53b9 165
9de33deb
MD
166/* Only used internally. */
167int
168scm_subr_p (SCM obj)
169{
170 if (SCM_NIMP (obj))
171 switch (SCM_TYP7 (obj))
172 {
173 case scm_tcs_subrs:
174 return 1;
175 default:
176 ;
177 }
178 return 0;
179}
180
3b3b36dd 181SCM_DEFINE (scm_procedure_documentation, "procedure-documentation", 1, 0, 0,
1bbd0b84 182 (SCM proc),
b380b885
MD
183 "Return the documentation string associated with @code{proc}. By\n"
184 "convention, if a procedure contains more than one expression and the\n"
185 "first expression is a string constant, that string is assumed to contain\n"
186 "documentation for that procedure.")
1bbd0b84 187#define FUNC_NAME s_scm_procedure_documentation
c2c82fba
MD
188{
189 SCM code;
bc36d050 190 SCM_ASSERT (scm_is_true (scm_procedure_p (proc)),
9a09deb1 191 proc, SCM_ARG1, FUNC_NAME);
c2c82fba
MD
192 switch (SCM_TYP7 (proc))
193 {
194 case scm_tcs_closures:
f9450cdb 195 code = SCM_CLOSURE_BODY (proc);
d2e53ed6 196 if (scm_is_null (SCM_CDR (code)))
c2c82fba
MD
197 return SCM_BOOL_F;
198 code = SCM_CAR (code);
7f9994d9 199 if (scm_is_string (code))
c2c82fba 200 return code;
5c9e7dad
DH
201 else
202 return SCM_BOOL_F;
c2c82fba
MD
203 default:
204 return SCM_BOOL_F;
c2c82fba
MD
205 }
206}
1bbd0b84 207#undef FUNC_NAME
c2c82fba 208
0f2d19dd 209
b4cd6492
MD
210/* Procedure-with-setter
211 */
212
a1ec6916 213SCM_DEFINE (scm_procedure_with_setter_p, "procedure-with-setter?", 1, 0, 0,
1bbd0b84 214 (SCM obj),
8cf97abf
MG
215 "Return @code{#t} if @var{obj} is a procedure with an\n"
216 "associated setter procedure.")
1bbd0b84 217#define FUNC_NAME s_scm_procedure_with_setter_p
b4cd6492 218{
7888309b 219 return scm_from_bool(SCM_PROCEDURE_WITH_SETTER_P (obj));
b4cd6492 220}
1bbd0b84 221#undef FUNC_NAME
b4cd6492 222
a1ec6916 223SCM_DEFINE (scm_make_procedure_with_setter, "make-procedure-with-setter", 2, 0, 0,
1bbd0b84 224 (SCM procedure, SCM setter),
8cf97abf
MG
225 "Create a new procedure which behaves like @var{procedure}, but\n"
226 "with the associated setter @var{setter}.")
1bbd0b84 227#define FUNC_NAME s_scm_make_procedure_with_setter
b4cd6492 228{
3fd8807e 229 SCM name, ret;
e1f8eb2b
MD
230 SCM_VALIDATE_PROC (1, procedure);
231 SCM_VALIDATE_PROC (2, setter);
3fd8807e
AW
232 ret = scm_double_cell (scm_tc7_pws,
233 SCM_UNPACK (procedure),
234 SCM_UNPACK (setter), 0);
235 /* don't use procedure_name, because don't care enough to do a reverse
236 lookup */
237 switch (SCM_TYP7 (procedure)) {
238 case scm_tcs_subrs:
239 name = SCM_SNAME (procedure);
240 break;
241 default:
242 name = scm_procedure_property (procedure, scm_sym_name);
243 break;
244 }
245 if (scm_is_true (name))
246 scm_set_procedure_property_x (ret, scm_sym_name, name);
247 return ret;
b4cd6492 248}
1bbd0b84 249#undef FUNC_NAME
b4cd6492 250
a1ec6916 251SCM_DEFINE (scm_procedure, "procedure", 1, 0, 0,
1bbd0b84 252 (SCM proc),
8cf97abf
MG
253 "Return the procedure of @var{proc}, which must be either a\n"
254 "procedure with setter, or an operator struct.")
1bbd0b84 255#define FUNC_NAME s_scm_procedure
b4cd6492 256{
e1f8eb2b 257 SCM_VALIDATE_NIM (1, proc);
b4cd6492
MD
258 if (SCM_PROCEDURE_WITH_SETTER_P (proc))
259 return SCM_PROCEDURE (proc);
260 else if (SCM_STRUCTP (proc))
261 {
1bbd0b84 262 SCM_ASSERT (SCM_I_OPERATORP (proc), proc, SCM_ARG1, FUNC_NAME);
b4cd6492
MD
263 return proc;
264 }
1bbd0b84 265 SCM_WRONG_TYPE_ARG (1, proc);
4260a7fc 266 return SCM_BOOL_F; /* not reached */
b4cd6492 267}
1bbd0b84 268#undef FUNC_NAME
b4cd6492 269
f5267231 270SCM_GPROC (s_setter, "setter", 1, 0, 0, scm_setter, g_setter);
b4cd6492
MD
271
272SCM
273scm_setter (SCM proc)
274{
f5267231 275 SCM_GASSERT1 (SCM_NIMP (proc), g_setter, proc, SCM_ARG1, s_setter);
b4cd6492
MD
276 if (SCM_PROCEDURE_WITH_SETTER_P (proc))
277 return SCM_SETTER (proc);
278 else if (SCM_STRUCTP (proc))
279 {
c72a774a 280 SCM setter;
f5267231
MD
281 SCM_GASSERT1 (SCM_I_OPERATORP (proc),
282 g_setter, proc, SCM_ARG1, s_setter);
c72a774a
MD
283 setter = (SCM_I_ENTITYP (proc)
284 ? SCM_ENTITY_SETTER (proc)
285 : SCM_OPERATOR_SETTER (proc));
286 if (SCM_NIMP (setter))
287 return setter;
288 /* fall through */
b4cd6492 289 }
f5267231 290 SCM_WTA_DISPATCH_1 (g_setter, proc, SCM_ARG1, s_setter);
b038d983 291 return SCM_BOOL_F; /* not reached */
b4cd6492 292}
1cc91f1b 293
ac51e74b 294\f
0f2d19dd
JB
295void
296scm_init_procs ()
0f2d19dd 297{
a0599745 298#include "libguile/procs.x"
0f2d19dd 299}
89e00824
ML
300
301/*
302 Local Variables:
303 c-file-style: "gnu"
304 End:
305*/