Updated
[bpt/guile.git] / libguile / procprop.c
CommitLineData
0f2d19dd
JB
1/* Copyright (C) 1995,1996 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
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. */
0f2d19dd
JB
41\f
42
43#include <stdio.h>
44#include "_scm.h"
20e6290e
JB
45#include "alist.h"
46#include "eval.h"
67e60655
MD
47#include "procs.h"
48#include "gsubr.h"
0f2d19dd 49
20e6290e 50#include "procprop.h"
0f2d19dd
JB
51\f
52
67e60655
MD
53SCM_GLOBAL_SYMBOL (scm_sym_arity, "arity");
54
55SCM
56scm_i_procedure_arity (proc)
57{
58 int a = 0, o = 0, r = 0;
59 loop:
60 switch (SCM_TYP7 (proc))
61 {
62 case scm_tc7_subr_1o:
63 o = 1;
64 case scm_tc7_subr_0:
65 break;
66 case scm_tc7_subr_2o:
67 o = 1;
68 case scm_tc7_subr_1:
69 case scm_tc7_cxr:
70 case scm_tc7_contin:
71 a += 1;
72 break;
73 case scm_tc7_subr_2:
74 a += 2;
75 break;
76 case scm_tc7_subr_3:
77 a += 3;
78 break;
79 case scm_tc7_asubr:
80 case scm_tc7_rpsubr:
81 case scm_tc7_lsubr:
82 r = 1;
83 case scm_tc7_lsubr_2:
84 a += 2;
85 break;
86#ifdef CCLO
87 case scm_tc7_cclo:
88 if (SCM_CCLO_SUBR (proc) == scm_f_gsubr_apply)
89 {
90 int type = SCM_INUM (SCM_GSUBR_TYPE (proc));
91 a = SCM_GSUBR_REQ (type);
92 o = SCM_GSUBR_OPT (type);
93 r = SCM_GSUBR_REST (type);
94 break;
95 }
96 proc = SCM_CCLO_SUBR (proc);
97 a -= 1;
98 goto loop;
99#endif
100 case scm_tcs_closures:
101 proc = SCM_CAR (SCM_CODE (proc));
102 if (SCM_IMP (proc))
103 break;
104 while (SCM_NIMP (proc) && SCM_CONSP (proc))
105 {
106 ++a;
107 proc = SCM_CDR (proc);
108 }
109 if (SCM_NIMP (proc))
110 r = 1;
111 break;
112 }
113 return SCM_LIST3 (SCM_MAKINUM (a),
114 SCM_MAKINUM (o),
115 r ? SCM_BOOL_T : SCM_BOOL_F);
116}
117
0f2d19dd
JB
118static SCM
119scm_stand_in_scm_proc(proc)
120 SCM proc;
121{
122 SCM answer;
123 answer = scm_assoc (proc, scm_stand_in_procs);
124 if (answer == SCM_BOOL_F)
125 {
126 answer = scm_closure (scm_listify (SCM_EOL, SCM_BOOL_F, SCM_UNDEFINED),
127 SCM_EOL);
128 scm_stand_in_procs = scm_cons (scm_cons (proc, answer),
129 scm_stand_in_procs);
130 }
131 else
132 answer = SCM_CDR (answer);
133 return answer;
134}
135
136SCM_PROC(s_procedure_properties, "procedure-properties", 1, 0, 0, scm_procedure_properties);
1cc91f1b 137
0f2d19dd
JB
138SCM
139scm_procedure_properties (proc)
140 SCM proc;
0f2d19dd 141{
4f4383ab
JB
142 SCM_ASSERT (SCM_NFALSEP (scm_procedure_p (proc)),
143 proc, SCM_ARG1, s_procedure_properties);
67e60655
MD
144 return scm_acons (scm_sym_arity, scm_i_procedure_arity (proc),
145 SCM_PROCPROPS (SCM_NIMP (proc) && SCM_CLOSUREP (proc)
146 ? proc
147 : scm_stand_in_scm_proc (proc)));
0f2d19dd
JB
148}
149
150SCM_PROC(s_set_procedure_properties_x, "set-procedure-properties!", 2, 0, 0, scm_set_procedure_properties_x);
1cc91f1b 151
0f2d19dd
JB
152SCM
153scm_set_procedure_properties_x (proc, new_val)
154 SCM proc;
155 SCM new_val;
0f2d19dd
JB
156{
157 if (!(SCM_NIMP (proc) && SCM_CLOSUREP (proc)))
158 proc = scm_stand_in_scm_proc(proc);
159 SCM_ASSERT (SCM_NIMP (proc) && SCM_CLOSUREP (proc), proc, SCM_ARG1, s_set_procedure_properties_x);
a6c64c3c 160 SCM_SETPROCPROPS (proc, new_val);
0f2d19dd
JB
161 return SCM_UNSPECIFIED;
162}
163
164SCM_PROC(s_procedure_property, "procedure-property", 2, 0, 0, scm_procedure_property);
1cc91f1b 165
0f2d19dd
JB
166SCM
167scm_procedure_property (p, k)
168 SCM p;
169 SCM k;
0f2d19dd
JB
170{
171 SCM assoc;
4f4383ab
JB
172 SCM_ASSERT (SCM_NFALSEP (scm_procedure_p (p)),
173 p, SCM_ARG1, s_procedure_property);
67e60655
MD
174 if (k == scm_sym_arity)
175 return scm_i_procedure_arity (p);
176 assoc = scm_sloppy_assq (k,
177 SCM_PROCPROPS (SCM_NIMP (p) && SCM_CLOSUREP (p)
178 ? p
179 : scm_stand_in_scm_proc (p)));
0f2d19dd
JB
180 return (SCM_NIMP (assoc) ? SCM_CDR (assoc) : SCM_BOOL_F);
181}
182
183SCM_PROC(s_set_procedure_property_x, "set-procedure-property!", 3, 0, 0, scm_set_procedure_property_x);
1cc91f1b 184
0f2d19dd
JB
185SCM
186scm_set_procedure_property_x (p, k, v)
187 SCM p;
188 SCM k;
189 SCM v;
0f2d19dd
JB
190{
191 SCM assoc;
192 if (!(SCM_NIMP (p) && SCM_CLOSUREP (p)))
193 p = scm_stand_in_scm_proc(p);
194 SCM_ASSERT (SCM_NIMP (p) && SCM_CLOSUREP (p), p, SCM_ARG1, s_set_procedure_property_x);
67e60655
MD
195 if (k == scm_sym_arity)
196 scm_misc_error (s_set_procedure_property_x,
197 "arity is a read-only property",
198 SCM_EOL);
0f2d19dd
JB
199 assoc = scm_sloppy_assq (k, SCM_PROCPROPS (p));
200 if (SCM_NIMP (assoc))
201 SCM_SETCDR (assoc, v);
202 else
a6c64c3c 203 SCM_SETPROCPROPS (p, scm_acons (k, v, SCM_PROCPROPS (p)));
0f2d19dd
JB
204 return SCM_UNSPECIFIED;
205}
206
207\f
208
1cc91f1b 209
0f2d19dd
JB
210void
211scm_init_procprop ()
0f2d19dd
JB
212{
213#include "procprop.x"
214}
215