prompt and abort inlining
[bpt/guile.git] / libguile / procs.c
CommitLineData
81ba12d7 1/* Copyright (C) 1995,1996,1997,1999,2000,2001, 2006, 2008, 2009 Free Software Foundation, Inc.
0f2d19dd 2 *
73be1d9e 3 * This library is free software; you can redistribute it and/or
53befeb7
NJ
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.
0f2d19dd 7 *
53befeb7
NJ
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
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
53befeb7
NJ
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
73be1d9e 17 */
1bbd0b84 18
1bbd0b84 19
0f2d19dd 20\f
dbb605f5
LC
21#ifdef HAVE_CONFIG_H
22# include <config.h>
23#endif
0f2d19dd 24
a0599745 25#include "libguile/_scm.h"
0f2d19dd 26
a0599745
MD
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
3b3b36dd 44SCM_DEFINE (scm_procedure_p, "procedure?", 1, 0, 0,
8cf97abf
MG
45 (SCM obj),
46 "Return @code{#t} if @var{obj} is a procedure.")
1bbd0b84 47#define FUNC_NAME s_scm_procedure_p
0f2d19dd
JB
48{
49 if (SCM_NIMP (obj))
50 switch (SCM_TYP7 (obj))
51 {
904a077d 52 case scm_tcs_struct:
b6cf4d02
AW
53 if (!((SCM_OBJ_CLASS_FLAGS (obj) & SCM_CLASSF_PURE_GENERIC)
54 || SCM_STRUCT_APPLICABLE_P (obj)))
bdc88419 55 break;
2fb924f6 56 case scm_tc7_program:
0f2d19dd 57 return SCM_BOOL_T;
0717dfd8 58 case scm_tc7_smob:
7888309b 59 return scm_from_bool (SCM_SMOB_DESCRIPTOR (obj).apply);
0f2d19dd
JB
60 default:
61 return SCM_BOOL_F;
62 }
63 return SCM_BOOL_F;
64}
1bbd0b84 65#undef FUNC_NAME
0f2d19dd 66
3b3b36dd 67SCM_DEFINE (scm_thunk_p, "thunk?", 1, 0, 0,
8cf97abf
MG
68 (SCM obj),
69 "Return @code{#t} if @var{obj} is a thunk.")
1bbd0b84 70#define FUNC_NAME s_scm_thunk_p
44bd53b9 71{
314b8716
AW
72 int req, opt, rest;
73 return scm_from_bool (scm_i_procedure_arity (obj, &req, &opt, &rest)
74 && req == 0);
44bd53b9 75}
1bbd0b84 76#undef FUNC_NAME
44bd53b9 77
2a0db0e3
AW
78SCM_SYMBOL (sym_documentation, "documentation");
79
3b3b36dd 80SCM_DEFINE (scm_procedure_documentation, "procedure-documentation", 1, 0, 0,
1bbd0b84 81 (SCM proc),
b380b885
MD
82 "Return the documentation string associated with @code{proc}. By\n"
83 "convention, if a procedure contains more than one expression and the\n"
84 "first expression is a string constant, that string is assumed to contain\n"
85 "documentation for that procedure.")
1bbd0b84 86#define FUNC_NAME s_scm_procedure_documentation
c2c82fba 87{
314b8716 88 SCM_VALIDATE_PROC (SCM_ARG1, proc);
2a0db0e3
AW
89 if (SCM_PROGRAM_P (proc))
90 return scm_assq_ref (scm_program_properties (proc), sym_documentation);
314b8716
AW
91 else
92 return SCM_BOOL_F;
c2c82fba 93}
1bbd0b84 94#undef FUNC_NAME
c2c82fba 95
0f2d19dd 96
b4cd6492
MD
97/* Procedure-with-setter
98 */
99
ea68d342
AW
100static SCM pws_vtable;
101
102
a1ec6916 103SCM_DEFINE (scm_procedure_with_setter_p, "procedure-with-setter?", 1, 0, 0,
1bbd0b84 104 (SCM obj),
8cf97abf
MG
105 "Return @code{#t} if @var{obj} is a procedure with an\n"
106 "associated setter procedure.")
1bbd0b84 107#define FUNC_NAME s_scm_procedure_with_setter_p
b4cd6492 108{
ea68d342 109 return scm_from_bool (SCM_STRUCTP (obj) && SCM_STRUCT_SETTER_P (obj));
b4cd6492 110}
1bbd0b84 111#undef FUNC_NAME
b4cd6492 112
a1ec6916 113SCM_DEFINE (scm_make_procedure_with_setter, "make-procedure-with-setter", 2, 0, 0,
1bbd0b84 114 (SCM procedure, SCM setter),
8cf97abf
MG
115 "Create a new procedure which behaves like @var{procedure}, but\n"
116 "with the associated setter @var{setter}.")
1bbd0b84 117#define FUNC_NAME s_scm_make_procedure_with_setter
b4cd6492 118{
3fd8807e 119 SCM name, ret;
e1f8eb2b
MD
120 SCM_VALIDATE_PROC (1, procedure);
121 SCM_VALIDATE_PROC (2, setter);
ea68d342
AW
122 ret = scm_make_struct (pws_vtable, SCM_INUM0,
123 scm_list_2 (procedure, setter));
124
3fd8807e
AW
125 /* don't use procedure_name, because don't care enough to do a reverse
126 lookup */
cc7005bc 127 name = scm_procedure_property (procedure, scm_sym_name);
3fd8807e
AW
128 if (scm_is_true (name))
129 scm_set_procedure_property_x (ret, scm_sym_name, name);
130 return ret;
b4cd6492 131}
1bbd0b84 132#undef FUNC_NAME
b4cd6492 133
a1ec6916 134SCM_DEFINE (scm_procedure, "procedure", 1, 0, 0,
1bbd0b84 135 (SCM proc),
ea68d342
AW
136 "Return the procedure of @var{proc}, which must be an\n"
137 "applicable struct.")
1bbd0b84 138#define FUNC_NAME s_scm_procedure
b4cd6492 139{
e1f8eb2b 140 SCM_VALIDATE_NIM (1, proc);
ea68d342
AW
141 SCM_ASSERT (SCM_STRUCT_APPLICABLE_P (proc), proc, SCM_ARG1, FUNC_NAME);
142 return SCM_STRUCT_PROCEDURE (proc);
b4cd6492 143}
1bbd0b84 144#undef FUNC_NAME
b4cd6492 145
ea68d342
AW
146SCM_PRIMITIVE_GENERIC (scm_setter, "setter", 1, 0, 0,
147 (SCM proc),
148 "Return the setter of @var{proc}, which must be an\n"
149 "applicable struct with a setter.")
150#define FUNC_NAME s_scm_setter
b4cd6492 151{
ea68d342
AW
152 SCM_GASSERT1 (SCM_STRUCTP (proc), g_scm_setter, proc, SCM_ARG1, FUNC_NAME);
153 if (SCM_STRUCT_SETTER_P (proc))
154 return SCM_STRUCT_SETTER (proc);
155 if (SCM_PUREGENERICP (proc))
156 /* FIXME: might not be an accessor */
157 return SCM_GENERIC_SETTER (proc);
158 SCM_WTA_DISPATCH_1 (g_scm_setter, proc, SCM_ARG1, FUNC_NAME);
b038d983 159 return SCM_BOOL_F; /* not reached */
b4cd6492 160}
ea68d342 161#undef FUNC_NAME
1cc91f1b 162
ac51e74b 163\f
0f2d19dd
JB
164void
165scm_init_procs ()
0f2d19dd 166{
bbd41a6a
AW
167 pws_vtable =
168 scm_c_make_struct (scm_applicable_struct_with_setter_vtable_vtable,
169 0,
170 1,
171 SCM_UNPACK (scm_from_locale_symbol ("pwpw")));
ea68d342 172
a0599745 173#include "libguile/procs.x"
0f2d19dd 174}
89e00824
ML
175
176/*
177 Local Variables:
178 c-file-style: "gnu"
179 End:
180*/