Commit | Line | Data |
---|---|---|
a48d60b1 | 1 | /* Copyright (C) 1995,1996,1998,2000,2001, 2003 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 | |
15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
16 | */ | |
1bbd0b84 | 17 | |
1bbd0b84 | 18 | |
0f2d19dd JB |
19 | \f |
20 | ||
a0599745 MD |
21 | #include "libguile/_scm.h" |
22 | ||
23 | #include "libguile/alist.h" | |
24 | #include "libguile/eval.h" | |
25 | #include "libguile/procs.h" | |
26 | #include "libguile/gsubr.h" | |
27 | #include "libguile/objects.h" | |
5540e847 | 28 | #include "libguile/smob.h" |
a0599745 MD |
29 | #include "libguile/root.h" |
30 | #include "libguile/vectors.h" | |
31 | ||
32 | #include "libguile/validate.h" | |
33 | #include "libguile/procprop.h" | |
0f2d19dd JB |
34 | \f |
35 | ||
c083a529 | 36 | SCM_GLOBAL_SYMBOL (scm_sym_system_procedure, "system-procedure"); |
67e60655 MD |
37 | SCM_GLOBAL_SYMBOL (scm_sym_arity, "arity"); |
38 | ||
08906709 MD |
39 | SCM |
40 | scm_i_procedure_arity (SCM proc) | |
67e60655 MD |
41 | { |
42 | int a = 0, o = 0, r = 0; | |
08906709 MD |
43 | if (SCM_IMP (proc)) |
44 | return SCM_BOOL_F; | |
67e60655 MD |
45 | loop: |
46 | switch (SCM_TYP7 (proc)) | |
47 | { | |
48 | case scm_tc7_subr_1o: | |
49 | o = 1; | |
50 | case scm_tc7_subr_0: | |
51 | break; | |
52 | case scm_tc7_subr_2o: | |
53 | o = 1; | |
54 | case scm_tc7_subr_1: | |
14b18ed6 | 55 | case scm_tc7_dsubr: |
67e60655 | 56 | case scm_tc7_cxr: |
67e60655 MD |
57 | a += 1; |
58 | break; | |
59 | case scm_tc7_subr_2: | |
60 | a += 2; | |
61 | break; | |
62 | case scm_tc7_subr_3: | |
63 | a += 3; | |
64 | break; | |
65 | case scm_tc7_asubr: | |
66 | case scm_tc7_rpsubr: | |
67 | case scm_tc7_lsubr: | |
68 | r = 1; | |
91517e28 | 69 | break; |
67e60655 MD |
70 | case scm_tc7_lsubr_2: |
71 | a += 2; | |
91517e28 | 72 | r = 1; |
67e60655 | 73 | break; |
5540e847 | 74 | case scm_tc7_smob: |
68b06924 | 75 | if (SCM_SMOB_APPLICABLE_P (proc)) |
362306b9 DH |
76 | { |
77 | int type = SCM_SMOB_DESCRIPTOR (proc).gsubr_type; | |
78 | a += SCM_GSUBR_REQ (type); | |
79 | o = SCM_GSUBR_OPT (type); | |
80 | r = SCM_GSUBR_REST (type); | |
81 | break; | |
82 | } | |
83 | else | |
84 | { | |
5540e847 | 85 | return SCM_BOOL_F; |
362306b9 DH |
86 | } |
87 | case scm_tc7_cclo: | |
bc36d050 | 88 | if (scm_is_eq (SCM_CCLO_SUBR (proc), scm_f_gsubr_apply)) |
67e60655 | 89 | { |
e11e83f3 | 90 | int type = scm_to_int (SCM_GSUBR_TYPE (proc)); |
08906709 | 91 | a += SCM_GSUBR_REQ (type); |
67e60655 MD |
92 | o = SCM_GSUBR_OPT (type); |
93 | r = SCM_GSUBR_REST (type); | |
94 | break; | |
95 | } | |
362306b9 DH |
96 | else |
97 | { | |
98 | proc = SCM_CCLO_SUBR (proc); | |
99 | a -= 1; | |
100 | goto loop; | |
101 | } | |
dec118c8 MD |
102 | case scm_tc7_pws: |
103 | proc = SCM_PROCEDURE (proc); | |
104 | goto loop; | |
67e60655 | 105 | case scm_tcs_closures: |
726d810a DH |
106 | proc = SCM_CLOSURE_FORMALS (proc); |
107 | if (SCM_NULLP (proc)) | |
67e60655 | 108 | break; |
0c95b57d | 109 | while (SCM_CONSP (proc)) |
67e60655 MD |
110 | { |
111 | ++a; | |
112 | proc = SCM_CDR (proc); | |
113 | } | |
726d810a | 114 | if (!SCM_NULLP (proc)) |
67e60655 MD |
115 | r = 1; |
116 | break; | |
904a077d | 117 | case scm_tcs_struct: |
815ce8d5 MD |
118 | if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC) |
119 | { | |
120 | r = 1; | |
121 | break; | |
122 | } | |
123 | else if (!SCM_I_OPERATORP (proc)) | |
08906709 | 124 | return SCM_BOOL_F; |
815ce8d5 MD |
125 | proc = (SCM_I_ENTITYP (proc) |
126 | ? SCM_ENTITY_PROCEDURE (proc) | |
127 | : SCM_OPERATOR_PROCEDURE (proc)); | |
128 | a -= 1; | |
129 | goto loop; | |
08906709 MD |
130 | default: |
131 | return SCM_BOOL_F; | |
67e60655 | 132 | } |
e11e83f3 | 133 | return scm_list_3 (scm_from_int (a), scm_from_int (o), scm_from_bool(r)); |
67e60655 MD |
134 | } |
135 | ||
0f2d19dd | 136 | static SCM |
1bbd0b84 | 137 | scm_stand_in_scm_proc(SCM proc) |
0f2d19dd JB |
138 | { |
139 | SCM answer; | |
a48d60b1 | 140 | answer = scm_assq (proc, scm_stand_in_procs); |
7888309b | 141 | if (scm_is_false (answer)) |
0f2d19dd | 142 | { |
1afff620 | 143 | answer = scm_closure (scm_list_2 (SCM_EOL, SCM_BOOL_F), SCM_EOL); |
e0c08f17 | 144 | scm_stand_in_procs = scm_acons (proc, answer, scm_stand_in_procs); |
0f2d19dd JB |
145 | } |
146 | else | |
147 | answer = SCM_CDR (answer); | |
148 | return answer; | |
149 | } | |
150 | ||
3b3b36dd | 151 | SCM_DEFINE (scm_procedure_properties, "procedure-properties", 1, 0, 0, |
1bbd0b84 | 152 | (SCM proc), |
b380b885 | 153 | "Return @var{obj}'s property list.") |
1bbd0b84 | 154 | #define FUNC_NAME s_scm_procedure_properties |
0f2d19dd | 155 | { |
34d19ef6 | 156 | SCM_VALIDATE_PROC (1, proc); |
67e60655 | 157 | return scm_acons (scm_sym_arity, scm_i_procedure_arity (proc), |
0c95b57d | 158 | SCM_PROCPROPS (SCM_CLOSUREP (proc) |
67e60655 MD |
159 | ? proc |
160 | : scm_stand_in_scm_proc (proc))); | |
0f2d19dd | 161 | } |
1bbd0b84 | 162 | #undef FUNC_NAME |
0f2d19dd | 163 | |
3b3b36dd | 164 | SCM_DEFINE (scm_set_procedure_properties_x, "set-procedure-properties!", 2, 0, 0, |
1bbd0b84 | 165 | (SCM proc, SCM new_val), |
b380b885 | 166 | "Set @var{obj}'s property list to @var{alist}.") |
1bbd0b84 | 167 | #define FUNC_NAME s_scm_set_procedure_properties_x |
0f2d19dd | 168 | { |
0c95b57d | 169 | if (!SCM_CLOSUREP (proc)) |
0f2d19dd | 170 | proc = scm_stand_in_scm_proc(proc); |
34d19ef6 | 171 | SCM_VALIDATE_CLOSURE (1, proc); |
a6c64c3c | 172 | SCM_SETPROCPROPS (proc, new_val); |
0f2d19dd JB |
173 | return SCM_UNSPECIFIED; |
174 | } | |
1bbd0b84 | 175 | #undef FUNC_NAME |
0f2d19dd | 176 | |
3b3b36dd | 177 | SCM_DEFINE (scm_procedure_property, "procedure-property", 2, 0, 0, |
1bbd0b84 | 178 | (SCM p, SCM k), |
b380b885 | 179 | "Return the property of @var{obj} with name @var{key}.") |
1bbd0b84 | 180 | #define FUNC_NAME s_scm_procedure_property |
0f2d19dd JB |
181 | { |
182 | SCM assoc; | |
bc36d050 | 183 | if (scm_is_eq (k, scm_sym_arity)) |
08906709 MD |
184 | { |
185 | SCM arity; | |
7888309b | 186 | SCM_ASSERT (scm_is_true (arity = scm_i_procedure_arity (p)), |
1bbd0b84 | 187 | p, SCM_ARG1, FUNC_NAME); |
08906709 MD |
188 | return arity; |
189 | } | |
34d19ef6 | 190 | SCM_VALIDATE_PROC (1, p); |
67e60655 | 191 | assoc = scm_sloppy_assq (k, |
0c95b57d | 192 | SCM_PROCPROPS (SCM_CLOSUREP (p) |
67e60655 MD |
193 | ? p |
194 | : scm_stand_in_scm_proc (p))); | |
0f2d19dd JB |
195 | return (SCM_NIMP (assoc) ? SCM_CDR (assoc) : SCM_BOOL_F); |
196 | } | |
1bbd0b84 | 197 | #undef FUNC_NAME |
0f2d19dd | 198 | |
3b3b36dd | 199 | SCM_DEFINE (scm_set_procedure_property_x, "set-procedure-property!", 3, 0, 0, |
1bbd0b84 | 200 | (SCM p, SCM k, SCM v), |
b380b885 MD |
201 | "In @var{obj}'s property list, set the property named @var{key} to\n" |
202 | "@var{value}.") | |
1bbd0b84 | 203 | #define FUNC_NAME s_scm_set_procedure_property_x |
0f2d19dd JB |
204 | { |
205 | SCM assoc; | |
0c95b57d | 206 | if (!SCM_CLOSUREP (p)) |
0f2d19dd | 207 | p = scm_stand_in_scm_proc(p); |
34d19ef6 | 208 | SCM_VALIDATE_CLOSURE (1, p); |
bc36d050 | 209 | if (scm_is_eq (k, scm_sym_arity)) |
1bbd0b84 | 210 | SCM_MISC_ERROR ("arity is a read-only property", SCM_EOL); |
0f2d19dd JB |
211 | assoc = scm_sloppy_assq (k, SCM_PROCPROPS (p)); |
212 | if (SCM_NIMP (assoc)) | |
213 | SCM_SETCDR (assoc, v); | |
214 | else | |
a6c64c3c | 215 | SCM_SETPROCPROPS (p, scm_acons (k, v, SCM_PROCPROPS (p))); |
0f2d19dd JB |
216 | return SCM_UNSPECIFIED; |
217 | } | |
1bbd0b84 | 218 | #undef FUNC_NAME |
0f2d19dd JB |
219 | |
220 | \f | |
221 | ||
1cc91f1b | 222 | |
0f2d19dd JB |
223 | void |
224 | scm_init_procprop () | |
0f2d19dd | 225 | { |
a0599745 | 226 | #include "libguile/procprop.x" |
0f2d19dd JB |
227 | } |
228 | ||
89e00824 ML |
229 | |
230 | /* | |
231 | Local Variables: | |
232 | c-file-style: "gnu" | |
233 | End: | |
234 | */ |