1 /* Copyright (C) 1995,1996,1997,1999,2000,2001, 2006, 2008, 2009 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
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
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.
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
24 #include "libguile/_scm.h"
26 #include "libguile/objects.h"
27 #include "libguile/strings.h"
28 #include "libguile/vectors.h"
29 #include "libguile/smob.h"
30 #include "libguile/deprecation.h"
32 #include "libguile/validate.h"
33 #include "libguile/procs.h"
34 #include "libguile/procprop.h"
35 #include "libguile/objcodes.h"
36 #include "libguile/programs.h"
45 scm_c_make_subr (const char *name
, long type
, SCM (*fcn
) ())
51 meta_info
= scm_gc_malloc (2 * sizeof (*meta_info
), "subr meta-info");
52 sname
= scm_from_locale_symbol (name
);
54 meta_info
[1] = SCM_EOL
; /* properties */
56 z
= scm_double_cell ((scm_t_bits
) type
, (scm_t_bits
) fcn
,
57 0 /* generic */, (scm_t_bits
) meta_info
);
59 scm_remember_upto_here_1 (sname
);
65 scm_c_define_subr (const char *name
, long type
, SCM (*fcn
) ())
67 SCM subr
= scm_c_make_subr (name
, type
, fcn
);
68 scm_define (SCM_SNAME (subr
), subr
);
72 /* This function isn't currently used since subrs are never freed. */
73 /* *fixme* Need mutex here. */
75 scm_free_subr_entry (SCM subr
)
77 scm_gc_free (SCM_SUBR_META_INFO (subr
), 2 * sizeof (SCM
),
82 scm_c_make_subr_with_generic (const char *name
,
83 long type
, SCM (*fcn
) (), SCM
*gf
)
85 SCM subr
= scm_c_make_subr (name
, type
, fcn
);
86 SCM_SET_SUBR_GENERIC_LOC (subr
, gf
);
91 scm_c_define_subr_with_generic (const char *name
,
92 long type
, SCM (*fcn
) (), SCM
*gf
)
94 SCM subr
= scm_c_make_subr_with_generic (name
, type
, fcn
, gf
);
95 scm_define (SCM_SNAME (subr
), subr
);
100 SCM_DEFINE (scm_procedure_p
, "procedure?", 1, 0, 0,
102 "Return @code{#t} if @var{obj} is a procedure.")
103 #define FUNC_NAME s_scm_procedure_p
106 switch (SCM_TYP7 (obj
))
109 if (!SCM_I_OPERATORP (obj
))
111 case scm_tcs_closures
:
116 return scm_from_bool (SCM_SMOB_DESCRIPTOR (obj
).apply
);
124 SCM_DEFINE (scm_closure_p
, "closure?", 1, 0, 0,
126 "Return @code{#t} if @var{obj} is a closure.")
127 #define FUNC_NAME s_scm_closure_p
129 return scm_from_bool (SCM_CLOSUREP (obj
));
133 SCM_DEFINE (scm_thunk_p
, "thunk?", 1, 0, 0,
135 "Return @code{#t} if @var{obj} is a thunk.")
136 #define FUNC_NAME s_scm_thunk_p
141 switch (SCM_TYP7 (obj
))
143 case scm_tcs_closures
:
144 return scm_from_bool (!scm_is_pair (SCM_CLOSURE_FORMALS (obj
)));
146 case scm_tc7_subr_1o
:
152 return scm_from_bool (SCM_GSUBR_REQ (SCM_GSUBR_TYPE (obj
)) == 0);
154 obj
= SCM_PROCEDURE (obj
);
157 if (SCM_PROGRAM_P (obj
) && SCM_PROGRAM_DATA (obj
)->nargs
== 0)
159 /* otherwise fall through */
166 /* Only used internally. */
171 switch (SCM_TYP7 (obj
))
181 SCM_DEFINE (scm_procedure_documentation
, "procedure-documentation", 1, 0, 0,
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.")
187 #define FUNC_NAME s_scm_procedure_documentation
190 SCM_ASSERT (scm_is_true (scm_procedure_p (proc
)),
191 proc
, SCM_ARG1
, FUNC_NAME
);
192 switch (SCM_TYP7 (proc
))
194 case scm_tcs_closures
:
195 code
= SCM_CLOSURE_BODY (proc
);
196 if (scm_is_null (SCM_CDR (code
)))
198 code
= SCM_CAR (code
);
199 if (scm_is_string (code
))
210 /* Procedure-with-setter
213 SCM_DEFINE (scm_procedure_with_setter_p
, "procedure-with-setter?", 1, 0, 0,
215 "Return @code{#t} if @var{obj} is a procedure with an\n"
216 "associated setter procedure.")
217 #define FUNC_NAME s_scm_procedure_with_setter_p
219 return scm_from_bool(SCM_PROCEDURE_WITH_SETTER_P (obj
));
223 SCM_DEFINE (scm_make_procedure_with_setter
, "make-procedure-with-setter", 2, 0, 0,
224 (SCM procedure
, SCM setter
),
225 "Create a new procedure which behaves like @var{procedure}, but\n"
226 "with the associated setter @var{setter}.")
227 #define FUNC_NAME s_scm_make_procedure_with_setter
230 SCM_VALIDATE_PROC (1, procedure
);
231 SCM_VALIDATE_PROC (2, setter
);
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
237 switch (SCM_TYP7 (procedure
)) {
239 name
= SCM_SNAME (procedure
);
242 name
= scm_procedure_property (procedure
, scm_sym_name
);
245 if (scm_is_true (name
))
246 scm_set_procedure_property_x (ret
, scm_sym_name
, name
);
251 SCM_DEFINE (scm_procedure
, "procedure", 1, 0, 0,
253 "Return the procedure of @var{proc}, which must be either a\n"
254 "procedure with setter, or an operator struct.")
255 #define FUNC_NAME s_scm_procedure
257 SCM_VALIDATE_NIM (1, proc
);
258 if (SCM_PROCEDURE_WITH_SETTER_P (proc
))
259 return SCM_PROCEDURE (proc
);
260 else if (SCM_STRUCTP (proc
))
262 SCM_ASSERT (SCM_I_OPERATORP (proc
), proc
, SCM_ARG1
, FUNC_NAME
);
265 SCM_WRONG_TYPE_ARG (1, proc
);
266 return SCM_BOOL_F
; /* not reached */
270 SCM_GPROC (s_setter
, "setter", 1, 0, 0, scm_setter
, g_setter
);
273 scm_setter (SCM proc
)
275 SCM_GASSERT1 (SCM_NIMP (proc
), g_setter
, proc
, SCM_ARG1
, s_setter
);
276 if (SCM_PROCEDURE_WITH_SETTER_P (proc
))
277 return SCM_SETTER (proc
);
278 else if (SCM_STRUCTP (proc
))
281 SCM_GASSERT1 (SCM_I_OPERATORP (proc
),
282 g_setter
, proc
, SCM_ARG1
, s_setter
);
283 setter
= (SCM_I_ENTITYP (proc
)
284 ? SCM_ENTITY_SETTER (proc
)
285 : SCM_OPERATOR_SETTER (proc
));
286 if (SCM_NIMP (setter
))
290 SCM_WTA_DISPATCH_1 (g_setter
, proc
, SCM_ARG1
, s_setter
);
291 return SCM_BOOL_F
; /* not reached */
298 #include "libguile/procs.x"