419d8fe7a70da19d7f6cdb2c04bc0851c4a46ea4
[bpt/guile.git] / libguile / procprop.c
1 /* Copyright (C) 1995,1996,1998 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 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
45 \f
46
47 #include <stdio.h>
48 #include "_scm.h"
49
50 #include "alist.h"
51 #include "eval.h"
52 #include "procs.h"
53 #include "gsubr.h"
54 #include "objects.h"
55 #include "root.h"
56 #include "vectors.h"
57
58 #include "validate.h"
59 #include "procprop.h"
60 \f
61
62 SCM_GLOBAL_SYMBOL (scm_sym_system_procedure, "system-procedure");
63 SCM_GLOBAL_SYMBOL (scm_sym_arity, "arity");
64
65 SCM
66 scm_i_procedure_arity (SCM proc)
67 {
68 int a = 0, o = 0, r = 0;
69 if (SCM_IMP (proc))
70 return SCM_BOOL_F;
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:
82 case scm_tc7_contin:
83 a += 1;
84 break;
85 case scm_tc7_subr_2:
86 a += 2;
87 break;
88 case scm_tc7_subr_3:
89 a += 3;
90 break;
91 case scm_tc7_asubr:
92 case scm_tc7_rpsubr:
93 case scm_tc7_lsubr:
94 r = 1;
95 break;
96 case scm_tc7_lsubr_2:
97 a += 2;
98 r = 1;
99 break;
100 #ifdef CCLO
101 case scm_tc7_cclo:
102 if (SCM_CCLO_SUBR (proc) == scm_f_gsubr_apply)
103 {
104 int type = SCM_INUM (SCM_GSUBR_TYPE (proc));
105 a += SCM_GSUBR_REQ (type);
106 o = SCM_GSUBR_OPT (type);
107 r = SCM_GSUBR_REST (type);
108 break;
109 }
110 proc = SCM_CCLO_SUBR (proc);
111 a -= 1;
112 goto loop;
113 #endif
114 case scm_tc7_pws:
115 proc = SCM_PROCEDURE (proc);
116 goto loop;
117 case scm_tcs_closures:
118 proc = SCM_CAR (SCM_CODE (proc));
119 if (SCM_IMP (proc))
120 break;
121 while (SCM_CONSP (proc))
122 {
123 ++a;
124 proc = SCM_CDR (proc);
125 }
126 if (SCM_NIMP (proc))
127 r = 1;
128 break;
129 case scm_tcs_cons_gloc:
130 if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
131 {
132 r = 1;
133 break;
134 }
135 else if (!SCM_I_OPERATORP (proc))
136 return SCM_BOOL_F;
137 proc = (SCM_I_ENTITYP (proc)
138 ? SCM_ENTITY_PROCEDURE (proc)
139 : SCM_OPERATOR_PROCEDURE (proc));
140 a -= 1;
141 goto loop;
142 default:
143 return SCM_BOOL_F;
144 }
145 return SCM_LIST3 (SCM_MAKINUM (a),
146 SCM_MAKINUM (o),
147 SCM_BOOL(r));
148 }
149
150 static SCM
151 scm_stand_in_scm_proc(SCM proc)
152 {
153 SCM answer;
154 answer = scm_assoc (proc, scm_stand_in_procs);
155 if (answer == SCM_BOOL_F)
156 {
157 answer = scm_closure (scm_listify (SCM_EOL, SCM_BOOL_F, SCM_UNDEFINED),
158 SCM_EOL);
159 scm_stand_in_procs = scm_cons (scm_cons (proc, answer),
160 scm_stand_in_procs);
161 }
162 else
163 answer = SCM_CDR (answer);
164 return answer;
165 }
166
167 SCM_DEFINE (scm_procedure_properties, "procedure-properties", 1, 0, 0,
168 (SCM proc),
169 "Return @var{obj}'s property list.")
170 #define FUNC_NAME s_scm_procedure_properties
171 {
172 SCM_VALIDATE_PROC (1,proc);
173 return scm_acons (scm_sym_arity, scm_i_procedure_arity (proc),
174 SCM_PROCPROPS (SCM_CLOSUREP (proc)
175 ? proc
176 : scm_stand_in_scm_proc (proc)));
177 }
178 #undef FUNC_NAME
179
180 SCM_DEFINE (scm_set_procedure_properties_x, "set-procedure-properties!", 2, 0, 0,
181 (SCM proc, SCM new_val),
182 "Set @var{obj}'s property list to @var{alist}.")
183 #define FUNC_NAME s_scm_set_procedure_properties_x
184 {
185 if (!SCM_CLOSUREP (proc))
186 proc = scm_stand_in_scm_proc(proc);
187 SCM_VALIDATE_CLOSURE (1,proc);
188 SCM_SETPROCPROPS (proc, new_val);
189 return SCM_UNSPECIFIED;
190 }
191 #undef FUNC_NAME
192
193 SCM_DEFINE (scm_procedure_property, "procedure-property", 2, 0, 0,
194 (SCM p, SCM k),
195 "Return the property of @var{obj} with name @var{key}.")
196 #define FUNC_NAME s_scm_procedure_property
197 {
198 SCM assoc;
199 if (k == scm_sym_arity)
200 {
201 SCM arity;
202 SCM_ASSERT (SCM_NFALSEP (arity = scm_i_procedure_arity (p)),
203 p, SCM_ARG1, FUNC_NAME);
204 return arity;
205 }
206 SCM_VALIDATE_PROC (1,p);
207 assoc = scm_sloppy_assq (k,
208 SCM_PROCPROPS (SCM_CLOSUREP (p)
209 ? p
210 : scm_stand_in_scm_proc (p)));
211 return (SCM_NIMP (assoc) ? SCM_CDR (assoc) : SCM_BOOL_F);
212 }
213 #undef FUNC_NAME
214
215 SCM_DEFINE (scm_set_procedure_property_x, "set-procedure-property!", 3, 0, 0,
216 (SCM p, SCM k, SCM v),
217 "In @var{obj}'s property list, set the property named @var{key} to\n"
218 "@var{value}.")
219 #define FUNC_NAME s_scm_set_procedure_property_x
220 {
221 SCM assoc;
222 if (!SCM_CLOSUREP (p))
223 p = scm_stand_in_scm_proc(p);
224 SCM_VALIDATE_CLOSURE (1,p);
225 if (k == scm_sym_arity)
226 SCM_MISC_ERROR ("arity is a read-only property", SCM_EOL);
227 assoc = scm_sloppy_assq (k, SCM_PROCPROPS (p));
228 if (SCM_NIMP (assoc))
229 SCM_SETCDR (assoc, v);
230 else
231 SCM_SETPROCPROPS (p, scm_acons (k, v, SCM_PROCPROPS (p)));
232 return SCM_UNSPECIFIED;
233 }
234 #undef FUNC_NAME
235
236 \f
237
238
239 void
240 scm_init_procprop ()
241 {
242 #include "procprop.x"
243 }
244