* srfi-1.c (scm_init_srfi_1): Extend root module map and for-each
[bpt/guile.git] / libguile / procprop.c
1 /* Copyright (C) 1995,1996,1998,2000,2001, 2003 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program 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
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
41
42
43 \f
44
45 #include "libguile/_scm.h"
46
47 #include "libguile/alist.h"
48 #include "libguile/eval.h"
49 #include "libguile/procs.h"
50 #include "libguile/gsubr.h"
51 #include "libguile/objects.h"
52 #include "libguile/smob.h"
53 #include "libguile/root.h"
54 #include "libguile/vectors.h"
55
56 #include "libguile/validate.h"
57 #include "libguile/procprop.h"
58 \f
59
60 SCM_GLOBAL_SYMBOL (scm_sym_system_procedure, "system-procedure");
61 SCM_GLOBAL_SYMBOL (scm_sym_arity, "arity");
62
63 SCM
64 scm_i_procedure_arity (SCM proc)
65 {
66 int a = 0, o = 0, r = 0;
67 if (SCM_IMP (proc))
68 return SCM_BOOL_F;
69 loop:
70 switch (SCM_TYP7 (proc))
71 {
72 case scm_tc7_subr_1o:
73 o = 1;
74 case scm_tc7_subr_0:
75 break;
76 case scm_tc7_subr_2o:
77 o = 1;
78 case scm_tc7_subr_1:
79 case scm_tc7_cxr:
80 a += 1;
81 break;
82 case scm_tc7_subr_2:
83 a += 2;
84 break;
85 case scm_tc7_subr_3:
86 a += 3;
87 break;
88 case scm_tc7_asubr:
89 case scm_tc7_rpsubr:
90 case scm_tc7_lsubr:
91 r = 1;
92 break;
93 case scm_tc7_lsubr_2:
94 a += 2;
95 r = 1;
96 break;
97 case scm_tc7_smob:
98 if (SCM_SMOB_APPLICABLE_P (proc))
99 {
100 int type = SCM_SMOB_DESCRIPTOR (proc).gsubr_type;
101 a += SCM_GSUBR_REQ (type);
102 o = SCM_GSUBR_OPT (type);
103 r = SCM_GSUBR_REST (type);
104 break;
105 }
106 else
107 {
108 return SCM_BOOL_F;
109 }
110 case scm_tc7_cclo:
111 if (SCM_EQ_P (SCM_CCLO_SUBR (proc), scm_f_gsubr_apply))
112 {
113 int type = SCM_INUM (SCM_GSUBR_TYPE (proc));
114 a += SCM_GSUBR_REQ (type);
115 o = SCM_GSUBR_OPT (type);
116 r = SCM_GSUBR_REST (type);
117 break;
118 }
119 else
120 {
121 proc = SCM_CCLO_SUBR (proc);
122 a -= 1;
123 goto loop;
124 }
125 case scm_tc7_pws:
126 proc = SCM_PROCEDURE (proc);
127 goto loop;
128 case scm_tcs_closures:
129 proc = SCM_CLOSURE_FORMALS (proc);
130 if (SCM_NULLP (proc))
131 break;
132 while (SCM_CONSP (proc))
133 {
134 ++a;
135 proc = SCM_CDR (proc);
136 }
137 if (!SCM_NULLP (proc))
138 r = 1;
139 break;
140 case scm_tcs_struct:
141 if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
142 {
143 r = 1;
144 break;
145 }
146 else if (!SCM_I_OPERATORP (proc))
147 return SCM_BOOL_F;
148 proc = (SCM_I_ENTITYP (proc)
149 ? SCM_ENTITY_PROCEDURE (proc)
150 : SCM_OPERATOR_PROCEDURE (proc));
151 a -= 1;
152 goto loop;
153 default:
154 return SCM_BOOL_F;
155 }
156 return scm_list_3 (SCM_MAKINUM (a), SCM_MAKINUM (o), SCM_BOOL(r));
157 }
158
159 static SCM
160 scm_stand_in_scm_proc(SCM proc)
161 {
162 SCM answer;
163 answer = scm_assq (proc, scm_stand_in_procs);
164 if (SCM_FALSEP (answer))
165 {
166 answer = scm_closure (scm_list_2 (SCM_EOL, SCM_BOOL_F), SCM_EOL);
167 scm_stand_in_procs = scm_acons (proc, answer, scm_stand_in_procs);
168 }
169 else
170 answer = SCM_CDR (answer);
171 return answer;
172 }
173
174 SCM_DEFINE (scm_procedure_properties, "procedure-properties", 1, 0, 0,
175 (SCM proc),
176 "Return @var{obj}'s property list.")
177 #define FUNC_NAME s_scm_procedure_properties
178 {
179 SCM_VALIDATE_PROC (1, proc);
180 return scm_acons (scm_sym_arity, scm_i_procedure_arity (proc),
181 SCM_PROCPROPS (SCM_CLOSUREP (proc)
182 ? proc
183 : scm_stand_in_scm_proc (proc)));
184 }
185 #undef FUNC_NAME
186
187 SCM_DEFINE (scm_set_procedure_properties_x, "set-procedure-properties!", 2, 0, 0,
188 (SCM proc, SCM new_val),
189 "Set @var{obj}'s property list to @var{alist}.")
190 #define FUNC_NAME s_scm_set_procedure_properties_x
191 {
192 if (!SCM_CLOSUREP (proc))
193 proc = scm_stand_in_scm_proc(proc);
194 SCM_VALIDATE_CLOSURE (1, proc);
195 SCM_SETPROCPROPS (proc, new_val);
196 return SCM_UNSPECIFIED;
197 }
198 #undef FUNC_NAME
199
200 SCM_DEFINE (scm_procedure_property, "procedure-property", 2, 0, 0,
201 (SCM p, SCM k),
202 "Return the property of @var{obj} with name @var{key}.")
203 #define FUNC_NAME s_scm_procedure_property
204 {
205 SCM assoc;
206 if (SCM_EQ_P (k, scm_sym_arity))
207 {
208 SCM arity;
209 SCM_ASSERT (SCM_NFALSEP (arity = scm_i_procedure_arity (p)),
210 p, SCM_ARG1, FUNC_NAME);
211 return arity;
212 }
213 SCM_VALIDATE_PROC (1, p);
214 assoc = scm_sloppy_assq (k,
215 SCM_PROCPROPS (SCM_CLOSUREP (p)
216 ? p
217 : scm_stand_in_scm_proc (p)));
218 return (SCM_NIMP (assoc) ? SCM_CDR (assoc) : SCM_BOOL_F);
219 }
220 #undef FUNC_NAME
221
222 SCM_DEFINE (scm_set_procedure_property_x, "set-procedure-property!", 3, 0, 0,
223 (SCM p, SCM k, SCM v),
224 "In @var{obj}'s property list, set the property named @var{key} to\n"
225 "@var{value}.")
226 #define FUNC_NAME s_scm_set_procedure_property_x
227 {
228 SCM assoc;
229 if (!SCM_CLOSUREP (p))
230 p = scm_stand_in_scm_proc(p);
231 SCM_VALIDATE_CLOSURE (1, p);
232 if (SCM_EQ_P (k, scm_sym_arity))
233 SCM_MISC_ERROR ("arity is a read-only property", SCM_EOL);
234 assoc = scm_sloppy_assq (k, SCM_PROCPROPS (p));
235 if (SCM_NIMP (assoc))
236 SCM_SETCDR (assoc, v);
237 else
238 SCM_SETPROCPROPS (p, scm_acons (k, v, SCM_PROCPROPS (p)));
239 return SCM_UNSPECIFIED;
240 }
241 #undef FUNC_NAME
242
243 \f
244
245
246 void
247 scm_init_procprop ()
248 {
249 #include "libguile/procprop.x"
250 }
251
252
253 /*
254 Local Variables:
255 c-file-style: "gnu"
256 End:
257 */