* arbiters.c, async.c, regex-posix.c: Use new smob interface.
[bpt/guile.git] / libguile / gsubr.c
CommitLineData
7dc6e754 1/* Copyright (C) 1995,1996,1997,1998 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. */
0f2d19dd
JB
41\f
42
43#include <stdio.h>
44#include "_scm.h"
20e6290e 45#include "genio.h"
abae3119 46#include "procprop.h"
0f2d19dd 47
20e6290e 48#include "gsubr.h"
0f2d19dd
JB
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
952bedad 58/* #define GSUBR_TEST */
0f2d19dd 59
abae3119 60SCM scm_i_name;
80ea260c 61SCM scm_i_inner_name;
dc0938d8 62SCM scm_f_gsubr_apply;
1cc91f1b 63
0f2d19dd
JB
64SCM
65scm_make_gsubr(name, req, opt, rst, fcn)
3eeba8d4 66 const char *name;
0f2d19dd
JB
67 int req;
68 int opt;
69 int rst;
70 SCM (*fcn)();
0f2d19dd 71{
dc0938d8
MD
72 switch SCM_GSUBR_MAKTYPE(req, opt, rst) {
73 case SCM_GSUBR_MAKTYPE(0, 0, 0): return scm_make_subr(name, scm_tc7_subr_0, fcn);
74 case SCM_GSUBR_MAKTYPE(1, 0, 0): return scm_make_subr(name, scm_tc7_subr_1, fcn);
75 case SCM_GSUBR_MAKTYPE(0, 1, 0): return scm_make_subr(name, scm_tc7_subr_1o, fcn);
76 case SCM_GSUBR_MAKTYPE(1, 1, 0): return scm_make_subr(name, scm_tc7_subr_2o, fcn);
77 case SCM_GSUBR_MAKTYPE(2, 0, 0): return scm_make_subr(name, scm_tc7_subr_2, fcn);
78 case SCM_GSUBR_MAKTYPE(3, 0, 0): return scm_make_subr(name, scm_tc7_subr_3, fcn);
79 case SCM_GSUBR_MAKTYPE(0, 0, 1): return scm_make_subr(name, scm_tc7_lsubr, fcn);
80 case SCM_GSUBR_MAKTYPE(2, 0, 1): return scm_make_subr(name, scm_tc7_lsubr_2, fcn);
0f2d19dd
JB
81 default:
82 {
83 SCM symcell = scm_sysintern(name, SCM_UNDEFINED);
dc0938d8 84 SCM z, cclo = scm_makcclo(scm_f_gsubr_apply, 3L);
0f2d19dd 85 long tmp = ((((SCM_CELLPTR)(SCM_CAR(symcell)))-scm_heap_org)<<8);
dc0938d8 86 if (SCM_GSUBR_MAX < req + opt + rst) {
0f2d19dd
JB
87 fputs("ERROR in scm_make_gsubr: too many args\n", stderr);
88 exit (1);
89 }
90 if ((tmp>>8) != ((SCM_CELLPTR)(SCM_CAR(symcell))-scm_heap_org))
91 tmp = 0;
92 SCM_NEWCELL(z);
93 SCM_SUBRF(z) = fcn;
a6c64c3c 94 SCM_SETCAR (z, tmp + scm_tc7_subr_0);
dc0938d8
MD
95 SCM_GSUBR_PROC(cclo) = z;
96 SCM_GSUBR_TYPE(cclo) = SCM_MAKINUM(SCM_GSUBR_MAKTYPE(req, opt, rst));
a6c64c3c 97 SCM_SETCDR (symcell, cclo);
abae3119
MD
98#ifdef DEBUG_EXTENSIONS
99 if (SCM_REC_PROCNAMES_P)
100 scm_set_procedure_property_x (cclo, scm_i_name, SCM_CAR (symcell));
101#endif
0f2d19dd
JB
102 return cclo;
103 }
104 }
105}
106
107
108SCM_PROC(s_gsubr_apply, "gsubr-apply", 0, 0, 1, scm_gsubr_apply);
1cc91f1b 109
0f2d19dd
JB
110SCM
111scm_gsubr_apply(args)
112 SCM args;
0f2d19dd
JB
113{
114 SCM self = SCM_CAR(args);
dc0938d8 115 SCM (*fcn)() = SCM_SUBRF(SCM_GSUBR_PROC(self));
0f2d19dd 116 SCM v[10]; /* must agree with greatest supported arity */
dc0938d8
MD
117 int typ = SCM_INUM(SCM_GSUBR_TYPE(self));
118 int i, n = SCM_GSUBR_REQ(typ) + SCM_GSUBR_OPT(typ) + SCM_GSUBR_REST(typ);
adc02cce
MD
119#if 0
120 SCM_ASSERT(n <= sizeof(v)/sizeof(SCM),
121 self, "internal programming error", s_gsubr_apply);
122#endif
0f2d19dd 123 args = SCM_CDR(args);
dc0938d8 124 for (i = 0; i < SCM_GSUBR_REQ(typ); i++) {
cf7c17e9 125#ifndef SCM_RECKLESS
0f2d19dd 126 if (SCM_IMP(args))
dc0938d8 127 wnargs: scm_wrong_num_args (SCM_SNAME(SCM_GSUBR_PROC(self)));
0f2d19dd
JB
128#endif
129 v[i] = SCM_CAR(args);
130 args = SCM_CDR(args);
131 }
dc0938d8 132 for (; i < SCM_GSUBR_REQ(typ) + SCM_GSUBR_OPT(typ); i++) {
0f2d19dd
JB
133 if (SCM_NIMP(args)) {
134 v[i] = SCM_CAR(args);
135 args = SCM_CDR(args);
136 }
137 else
138 v[i] = SCM_UNDEFINED;
139 }
dc0938d8 140 if (SCM_GSUBR_REST(typ))
0f2d19dd 141 v[i] = args;
adc02cce
MD
142 else
143 SCM_ASRTGO(SCM_NULLP(args), wnargs);
0f2d19dd 144 switch (n) {
0f2d19dd
JB
145 case 2: return (*fcn)(v[0], v[1]);
146 case 3: return (*fcn)(v[0], v[1], v[2]);
147 case 4: return (*fcn)(v[0], v[1], v[2], v[3]);
148 case 5: return (*fcn)(v[0], v[1], v[2], v[3], v[4]);
149 case 6: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5]);
150 case 7: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6]);
151 case 8: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
152 case 9: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8]);
153 case 10: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9]);
154 }
cda139a7 155 return 0; /* Never reached. */
0f2d19dd
JB
156}
157
158
159#ifdef GSUBR_TEST
160/* A silly example, taking 2 required args, 1 optional, and
161 a scm_list of rest args
162 */
163SCM
164gsubr_21l(req1, req2, opt, rst)
165 SCM req1, req2, opt, rst;
166{
b7f3516f 167 scm_puts ("gsubr-2-1-l:\n req1: ", scm_cur_outp);
0f2d19dd 168 scm_display(req1, scm_cur_outp);
b7f3516f 169 scm_puts ("\n req2: ", scm_cur_outp);
0f2d19dd 170 scm_display(req2, scm_cur_outp);
b7f3516f 171 scm_puts ("\n opt: ", scm_cur_outp);
0f2d19dd 172 scm_display(opt, scm_cur_outp);
b7f3516f 173 scm_puts ("\n rest: ", scm_cur_outp);
0f2d19dd
JB
174 scm_display(rst, scm_cur_outp);
175 scm_newline(scm_cur_outp);
176 return SCM_UNSPECIFIED;
177}
178#endif
179
180
1cc91f1b 181
0f2d19dd
JB
182void
183scm_init_gsubr()
0f2d19dd 184{
dc0938d8 185 scm_f_gsubr_apply = scm_make_subr(s_gsubr_apply, scm_tc7_lsubr, scm_gsubr_apply);
abae3119
MD
186 scm_i_name = SCM_CAR (scm_sysintern ("name", SCM_UNDEFINED));
187 scm_permanent_object (scm_i_name);
80ea260c
MD
188 scm_i_inner_name = SCM_CAR (scm_sysintern ("inner-name", SCM_UNDEFINED));
189 scm_permanent_object (scm_i_inner_name);
0f2d19dd
JB
190#ifdef GSUBR_TEST
191 scm_make_gsubr("gsubr-2-1-l", 2, 1, 1, gsubr_21l); /* example */
192#endif
193}