Commit | Line | Data |
---|---|---|
726d810a | 1 | /* Copyright (C) 1995,1996,1998,2000,2001 Free Software Foundation, Inc. |
0f2d19dd JB |
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 | |
82892bed JB |
15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
16 | * Boston, MA 02111-1307 USA | |
0f2d19dd JB |
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. | |
82892bed | 40 | * If you do not wish that, delete this exception notice. */ |
1bbd0b84 GB |
41 | |
42 | /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999, | |
43 | gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */ | |
44 | ||
0f2d19dd JB |
45 | \f |
46 | ||
a0599745 MD |
47 | #include "libguile/_scm.h" |
48 | ||
49 | #include "libguile/alist.h" | |
50 | #include "libguile/eval.h" | |
51 | #include "libguile/procs.h" | |
52 | #include "libguile/gsubr.h" | |
53 | #include "libguile/objects.h" | |
5540e847 | 54 | #include "libguile/smob.h" |
a0599745 MD |
55 | #include "libguile/root.h" |
56 | #include "libguile/vectors.h" | |
57 | ||
58 | #include "libguile/validate.h" | |
59 | #include "libguile/procprop.h" | |
0f2d19dd JB |
60 | \f |
61 | ||
c083a529 | 62 | SCM_GLOBAL_SYMBOL (scm_sym_system_procedure, "system-procedure"); |
67e60655 MD |
63 | SCM_GLOBAL_SYMBOL (scm_sym_arity, "arity"); |
64 | ||
08906709 MD |
65 | SCM |
66 | scm_i_procedure_arity (SCM proc) | |
67e60655 MD |
67 | { |
68 | int a = 0, o = 0, r = 0; | |
08906709 MD |
69 | if (SCM_IMP (proc)) |
70 | return SCM_BOOL_F; | |
67e60655 MD |
71 | loop: |
72 | switch (SCM_TYP7 (proc)) | |
73 | { | |
74 | case scm_tc7_subr_1o: | |
75 | o = 1; | |
76 | case scm_tc7_subr_0: | |
77 | break; | |
78 | case scm_tc7_subr_2o: | |
79 | o = 1; | |
80 | case scm_tc7_subr_1: | |
81 | case scm_tc7_cxr: | |
67e60655 MD |
82 | a += 1; |
83 | break; | |
84 | case scm_tc7_subr_2: | |
85 | a += 2; | |
86 | break; | |
87 | case scm_tc7_subr_3: | |
88 | a += 3; | |
89 | break; | |
90 | case scm_tc7_asubr: | |
91 | case scm_tc7_rpsubr: | |
92 | case scm_tc7_lsubr: | |
93 | r = 1; | |
91517e28 | 94 | break; |
67e60655 MD |
95 | case scm_tc7_lsubr_2: |
96 | a += 2; | |
91517e28 | 97 | r = 1; |
67e60655 | 98 | break; |
5540e847 | 99 | case scm_tc7_smob: |
68b06924 | 100 | if (SCM_SMOB_APPLICABLE_P (proc)) |
362306b9 DH |
101 | { |
102 | int type = SCM_SMOB_DESCRIPTOR (proc).gsubr_type; | |
103 | a += SCM_GSUBR_REQ (type); | |
104 | o = SCM_GSUBR_OPT (type); | |
105 | r = SCM_GSUBR_REST (type); | |
106 | break; | |
107 | } | |
108 | else | |
109 | { | |
5540e847 | 110 | return SCM_BOOL_F; |
362306b9 DH |
111 | } |
112 | case scm_tc7_cclo: | |
113 | if (SCM_EQ_P (SCM_CCLO_SUBR (proc), scm_f_gsubr_apply)) | |
67e60655 | 114 | { |
362306b9 | 115 | int type = SCM_INUM (SCM_GSUBR_TYPE (proc)); |
08906709 | 116 | a += SCM_GSUBR_REQ (type); |
67e60655 MD |
117 | o = SCM_GSUBR_OPT (type); |
118 | r = SCM_GSUBR_REST (type); | |
119 | break; | |
120 | } | |
362306b9 DH |
121 | else |
122 | { | |
123 | proc = SCM_CCLO_SUBR (proc); | |
124 | a -= 1; | |
125 | goto loop; | |
126 | } | |
dec118c8 MD |
127 | case scm_tc7_pws: |
128 | proc = SCM_PROCEDURE (proc); | |
129 | goto loop; | |
67e60655 | 130 | case scm_tcs_closures: |
726d810a DH |
131 | proc = SCM_CLOSURE_FORMALS (proc); |
132 | if (SCM_NULLP (proc)) | |
67e60655 | 133 | break; |
0c95b57d | 134 | while (SCM_CONSP (proc)) |
67e60655 MD |
135 | { |
136 | ++a; | |
137 | proc = SCM_CDR (proc); | |
138 | } | |
726d810a | 139 | if (!SCM_NULLP (proc)) |
67e60655 MD |
140 | r = 1; |
141 | break; | |
08906709 | 142 | case scm_tcs_cons_gloc: |
815ce8d5 MD |
143 | if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC) |
144 | { | |
145 | r = 1; | |
146 | break; | |
147 | } | |
148 | else if (!SCM_I_OPERATORP (proc)) | |
08906709 | 149 | return SCM_BOOL_F; |
815ce8d5 MD |
150 | proc = (SCM_I_ENTITYP (proc) |
151 | ? SCM_ENTITY_PROCEDURE (proc) | |
152 | : SCM_OPERATOR_PROCEDURE (proc)); | |
153 | a -= 1; | |
154 | goto loop; | |
08906709 MD |
155 | default: |
156 | return SCM_BOOL_F; | |
67e60655 | 157 | } |
1afff620 | 158 | return scm_list_3 (SCM_MAKINUM (a), SCM_MAKINUM (o), SCM_BOOL(r)); |
67e60655 MD |
159 | } |
160 | ||
0f2d19dd | 161 | static SCM |
1bbd0b84 | 162 | scm_stand_in_scm_proc(SCM proc) |
0f2d19dd JB |
163 | { |
164 | SCM answer; | |
165 | answer = scm_assoc (proc, scm_stand_in_procs); | |
54778cd3 | 166 | if (SCM_FALSEP (answer)) |
0f2d19dd | 167 | { |
1afff620 | 168 | answer = scm_closure (scm_list_2 (SCM_EOL, SCM_BOOL_F), SCM_EOL); |
e0c08f17 | 169 | scm_stand_in_procs = scm_acons (proc, answer, scm_stand_in_procs); |
0f2d19dd JB |
170 | } |
171 | else | |
172 | answer = SCM_CDR (answer); | |
173 | return answer; | |
174 | } | |
175 | ||
3b3b36dd | 176 | SCM_DEFINE (scm_procedure_properties, "procedure-properties", 1, 0, 0, |
1bbd0b84 | 177 | (SCM proc), |
b380b885 | 178 | "Return @var{obj}'s property list.") |
1bbd0b84 | 179 | #define FUNC_NAME s_scm_procedure_properties |
0f2d19dd | 180 | { |
3b3b36dd | 181 | SCM_VALIDATE_PROC (1,proc); |
67e60655 | 182 | return scm_acons (scm_sym_arity, scm_i_procedure_arity (proc), |
0c95b57d | 183 | SCM_PROCPROPS (SCM_CLOSUREP (proc) |
67e60655 MD |
184 | ? proc |
185 | : scm_stand_in_scm_proc (proc))); | |
0f2d19dd | 186 | } |
1bbd0b84 | 187 | #undef FUNC_NAME |
0f2d19dd | 188 | |
3b3b36dd | 189 | SCM_DEFINE (scm_set_procedure_properties_x, "set-procedure-properties!", 2, 0, 0, |
1bbd0b84 | 190 | (SCM proc, SCM new_val), |
b380b885 | 191 | "Set @var{obj}'s property list to @var{alist}.") |
1bbd0b84 | 192 | #define FUNC_NAME s_scm_set_procedure_properties_x |
0f2d19dd | 193 | { |
0c95b57d | 194 | if (!SCM_CLOSUREP (proc)) |
0f2d19dd | 195 | proc = scm_stand_in_scm_proc(proc); |
3b3b36dd | 196 | SCM_VALIDATE_CLOSURE (1,proc); |
a6c64c3c | 197 | SCM_SETPROCPROPS (proc, new_val); |
0f2d19dd JB |
198 | return SCM_UNSPECIFIED; |
199 | } | |
1bbd0b84 | 200 | #undef FUNC_NAME |
0f2d19dd | 201 | |
3b3b36dd | 202 | SCM_DEFINE (scm_procedure_property, "procedure-property", 2, 0, 0, |
1bbd0b84 | 203 | (SCM p, SCM k), |
b380b885 | 204 | "Return the property of @var{obj} with name @var{key}.") |
1bbd0b84 | 205 | #define FUNC_NAME s_scm_procedure_property |
0f2d19dd JB |
206 | { |
207 | SCM assoc; | |
54778cd3 | 208 | if (SCM_EQ_P (k, scm_sym_arity)) |
08906709 MD |
209 | { |
210 | SCM arity; | |
211 | SCM_ASSERT (SCM_NFALSEP (arity = scm_i_procedure_arity (p)), | |
1bbd0b84 | 212 | p, SCM_ARG1, FUNC_NAME); |
08906709 MD |
213 | return arity; |
214 | } | |
3b3b36dd | 215 | SCM_VALIDATE_PROC (1,p); |
67e60655 | 216 | assoc = scm_sloppy_assq (k, |
0c95b57d | 217 | SCM_PROCPROPS (SCM_CLOSUREP (p) |
67e60655 MD |
218 | ? p |
219 | : scm_stand_in_scm_proc (p))); | |
0f2d19dd JB |
220 | return (SCM_NIMP (assoc) ? SCM_CDR (assoc) : SCM_BOOL_F); |
221 | } | |
1bbd0b84 | 222 | #undef FUNC_NAME |
0f2d19dd | 223 | |
3b3b36dd | 224 | SCM_DEFINE (scm_set_procedure_property_x, "set-procedure-property!", 3, 0, 0, |
1bbd0b84 | 225 | (SCM p, SCM k, SCM v), |
b380b885 MD |
226 | "In @var{obj}'s property list, set the property named @var{key} to\n" |
227 | "@var{value}.") | |
1bbd0b84 | 228 | #define FUNC_NAME s_scm_set_procedure_property_x |
0f2d19dd JB |
229 | { |
230 | SCM assoc; | |
0c95b57d | 231 | if (!SCM_CLOSUREP (p)) |
0f2d19dd | 232 | p = scm_stand_in_scm_proc(p); |
3b3b36dd | 233 | SCM_VALIDATE_CLOSURE (1,p); |
54778cd3 | 234 | if (SCM_EQ_P (k, scm_sym_arity)) |
1bbd0b84 | 235 | SCM_MISC_ERROR ("arity is a read-only property", SCM_EOL); |
0f2d19dd JB |
236 | assoc = scm_sloppy_assq (k, SCM_PROCPROPS (p)); |
237 | if (SCM_NIMP (assoc)) | |
238 | SCM_SETCDR (assoc, v); | |
239 | else | |
a6c64c3c | 240 | SCM_SETPROCPROPS (p, scm_acons (k, v, SCM_PROCPROPS (p))); |
0f2d19dd JB |
241 | return SCM_UNSPECIFIED; |
242 | } | |
1bbd0b84 | 243 | #undef FUNC_NAME |
0f2d19dd JB |
244 | |
245 | \f | |
246 | ||
1cc91f1b | 247 | |
0f2d19dd JB |
248 | void |
249 | scm_init_procprop () | |
0f2d19dd | 250 | { |
8dc9439f | 251 | #ifndef SCM_MAGIC_SNARFER |
a0599745 | 252 | #include "libguile/procprop.x" |
8dc9439f | 253 | #endif |
0f2d19dd JB |
254 | } |
255 | ||
89e00824 ML |
256 | |
257 | /* | |
258 | Local Variables: | |
259 | c-file-style: "gnu" | |
260 | End: | |
261 | */ |