* procprop.h: Added declaration of scm_i_inner_name.
[bpt/guile.git] / libguile / gsubr.c
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
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 \f
42
43 #include <stdio.h>
44 #include "_scm.h"
45 #include "genio.h"
46 #include "procprop.h"
47
48 #include "gsubr.h"
49 \f
50 /*
51 * gsubr.c
52 * Provide `gsubrs' -- subrs taking a prescribed number of required, optional,
53 * and rest arguments.
54 */
55
56 #include "gsubr.h"
57
58 #define GSUBR_TEST 1
59
60 #define GSUBR_MAKTYPE(req, opt, rst) ((req)|((opt)<<4)|((rst)<<8))
61 #define GSUBR_REQ(x) ((int)(x)&0xf)
62 #define GSUBR_OPT(x) (((int)(x)&0xf0)>>4)
63 #define GSUBR_REST(x) ((int)(x)>>8)
64
65 #define GSUBR_MAX 10
66 #define GSUBR_TYPE(cclo) (SCM_VELTS(cclo)[1])
67 #define GSUBR_PROC(cclo) (SCM_VELTS(cclo)[2])
68
69 SCM scm_i_name;
70 SCM scm_i_inner_name;
71 static SCM f_gsubr_apply;
72
73 SCM
74 scm_make_gsubr(name, req, opt, rst, fcn)
75 char *name;
76 int req;
77 int opt;
78 int rst;
79 SCM (*fcn)();
80 {
81 switch GSUBR_MAKTYPE(req, opt, rst) {
82 case GSUBR_MAKTYPE(0, 0, 0): return scm_make_subr(name, scm_tc7_subr_0, fcn);
83 case GSUBR_MAKTYPE(1, 0, 0): return scm_make_subr(name, scm_tc7_subr_1, fcn);
84 case GSUBR_MAKTYPE(0, 1, 0): return scm_make_subr(name, scm_tc7_subr_1o, fcn);
85 case GSUBR_MAKTYPE(1, 1, 0): return scm_make_subr(name, scm_tc7_subr_2o, fcn);
86 case GSUBR_MAKTYPE(2, 0, 0): return scm_make_subr(name, scm_tc7_subr_2, fcn);
87 case GSUBR_MAKTYPE(3, 0, 0): return scm_make_subr(name, scm_tc7_subr_3, fcn);
88 case GSUBR_MAKTYPE(0, 0, 1): return scm_make_subr(name, scm_tc7_lsubr, fcn);
89 case GSUBR_MAKTYPE(2, 0, 1): return scm_make_subr(name, scm_tc7_lsubr_2, fcn);
90 default:
91 {
92 SCM symcell = scm_sysintern(name, SCM_UNDEFINED);
93 SCM z, cclo = scm_makcclo(f_gsubr_apply, 3L);
94 long tmp = ((((SCM_CELLPTR)(SCM_CAR(symcell)))-scm_heap_org)<<8);
95 if (GSUBR_MAX < req + opt + rst) {
96 fputs("ERROR in scm_make_gsubr: too many args\n", stderr);
97 exit (1);
98 }
99 if ((tmp>>8) != ((SCM_CELLPTR)(SCM_CAR(symcell))-scm_heap_org))
100 tmp = 0;
101 SCM_NEWCELL(z);
102 SCM_SUBRF(z) = fcn;
103 SCM_SETCAR (z, tmp + scm_tc7_subr_0);
104 GSUBR_PROC(cclo) = z;
105 GSUBR_TYPE(cclo) = SCM_MAKINUM(GSUBR_MAKTYPE(req, opt, rst));
106 SCM_SETCDR (symcell, cclo);
107 #ifdef DEBUG_EXTENSIONS
108 if (SCM_REC_PROCNAMES_P)
109 scm_set_procedure_property_x (cclo, scm_i_name, SCM_CAR (symcell));
110 #endif
111 return cclo;
112 }
113 }
114 }
115
116
117 SCM_PROC(s_gsubr_apply, "gsubr-apply", 0, 0, 1, scm_gsubr_apply);
118
119 SCM
120 scm_gsubr_apply(args)
121 SCM args;
122 {
123 SCM self = SCM_CAR(args);
124 SCM (*fcn)() = SCM_SUBRF(GSUBR_PROC(self));
125 SCM v[10]; /* must agree with greatest supported arity */
126 int typ = SCM_INUM(GSUBR_TYPE(self));
127 int i, n = GSUBR_REQ(typ) + GSUBR_OPT(typ) + GSUBR_REST(typ);
128 #if 0
129 SCM_ASSERT(n <= sizeof(v)/sizeof(SCM),
130 self, "internal programming error", s_gsubr_apply);
131 #endif
132 args = SCM_CDR(args);
133 for (i = 0; i < GSUBR_REQ(typ); i++) {
134 #ifndef RECKLESS
135 if (SCM_IMP(args))
136 wnargs: scm_wrong_num_args (SCM_SNAME(GSUBR_PROC(self)));
137 #endif
138 v[i] = SCM_CAR(args);
139 args = SCM_CDR(args);
140 }
141 for (; i < GSUBR_REQ(typ) + GSUBR_OPT(typ); i++) {
142 if (SCM_NIMP(args)) {
143 v[i] = SCM_CAR(args);
144 args = SCM_CDR(args);
145 }
146 else
147 v[i] = SCM_UNDEFINED;
148 }
149 if (GSUBR_REST(typ))
150 v[i] = args;
151 else
152 SCM_ASRTGO(SCM_NULLP(args), wnargs);
153 switch (n) {
154 case 2: return (*fcn)(v[0], v[1]);
155 case 3: return (*fcn)(v[0], v[1], v[2]);
156 case 4: return (*fcn)(v[0], v[1], v[2], v[3]);
157 case 5: return (*fcn)(v[0], v[1], v[2], v[3], v[4]);
158 case 6: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5]);
159 case 7: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6]);
160 case 8: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
161 case 9: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8]);
162 case 10: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9]);
163 }
164 }
165
166
167 #ifdef GSUBR_TEST
168 /* A silly example, taking 2 required args, 1 optional, and
169 a scm_list of rest args
170 */
171 SCM
172 gsubr_21l(req1, req2, opt, rst)
173 SCM req1, req2, opt, rst;
174 {
175 scm_gen_puts (scm_regular_string, "gsubr-2-1-l:\n req1: ", scm_cur_outp);
176 scm_display(req1, scm_cur_outp);
177 scm_gen_puts (scm_regular_string, "\n req2: ", scm_cur_outp);
178 scm_display(req2, scm_cur_outp);
179 scm_gen_puts (scm_regular_string, "\n opt: ", scm_cur_outp);
180 scm_display(opt, scm_cur_outp);
181 scm_gen_puts (scm_regular_string, "\n rest: ", scm_cur_outp);
182 scm_display(rst, scm_cur_outp);
183 scm_newline(scm_cur_outp);
184 return SCM_UNSPECIFIED;
185 }
186 #endif
187
188
189
190 void
191 scm_init_gsubr()
192 {
193 f_gsubr_apply = scm_make_subr(s_gsubr_apply, scm_tc7_lsubr, scm_gsubr_apply);
194 scm_i_name = SCM_CAR (scm_sysintern ("name", SCM_UNDEFINED));
195 scm_permanent_object (scm_i_name);
196 scm_i_inner_name = SCM_CAR (scm_sysintern ("inner-name", SCM_UNDEFINED));
197 scm_permanent_object (scm_i_inner_name);
198 #ifdef GSUBR_TEST
199 scm_make_gsubr("gsubr-2-1-l", 2, 1, 1, gsubr_21l); /* example */
200 #endif
201 }