Commit | Line | Data |
---|---|---|
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 | |
15 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
16 | * | |
17 | * As a special exception, the Free Software Foundation gives permission | |
18 | * for additional uses of the text contained in its release of GUILE. | |
19 | * | |
20 | * The exception is that, if you link the GUILE library with other files | |
21 | * to produce an executable, this does not by itself cause the | |
22 | * resulting executable to be covered by the GNU General Public License. | |
23 | * Your use of that executable is in no way restricted on account of | |
24 | * linking the GUILE library code into it. | |
25 | * | |
26 | * This exception does not however invalidate any other reasons why | |
27 | * the executable file might be covered by the GNU General Public License. | |
28 | * | |
29 | * This exception applies only to the code released by the | |
30 | * Free Software Foundation under the name GUILE. If you copy | |
31 | * code from other Free Software Foundation releases into a copy of | |
32 | * GUILE, as the General Public License permits, the exception does | |
33 | * not apply to the code that you add in this way. To avoid misleading | |
34 | * anyone as to the status of such modified files, you must delete | |
35 | * this exception notice from them. | |
36 | * | |
37 | * If you write modifications of your own for GUILE, it is your choice | |
38 | * whether to permit this exception to apply to your modifications. | |
39 | * If you do not wish that, delete this exception notice. | |
40 | */ | |
41 | \f | |
42 | ||
43 | #include <stdio.h> | |
44 | #include "_scm.h" | |
20e6290e | 45 | #include "genio.h" |
0f2d19dd | 46 | |
20e6290e | 47 | #include "gsubr.h" |
0f2d19dd JB |
48 | \f |
49 | /* | |
50 | * gsubr.c | |
51 | * Provide `gsubrs' -- subrs taking a prescribed number of required, optional, | |
52 | * and rest arguments. | |
53 | */ | |
54 | ||
55 | #include "gsubr.h" | |
56 | ||
57 | #define GSUBR_TEST 1 | |
58 | ||
59 | #define GSUBR_MAKTYPE(req, opt, rst) ((req)|((opt)<<4)|((rst)<<8)) | |
60 | #define GSUBR_REQ(x) ((int)(x)&0xf) | |
61 | #define GSUBR_OPT(x) (((int)(x)&0xf0)>>4) | |
62 | #define GSUBR_REST(x) ((int)(x)>>8) | |
63 | ||
64 | #define GSUBR_MAX 10 | |
65 | #define GSUBR_TYPE(cclo) (SCM_VELTS(cclo)[1]) | |
66 | #define GSUBR_PROC(cclo) (SCM_VELTS(cclo)[2]) | |
67 | ||
68 | static SCM f_gsubr_apply; | |
1cc91f1b | 69 | |
0f2d19dd JB |
70 | SCM |
71 | scm_make_gsubr(name, req, opt, rst, fcn) | |
72 | char *name; | |
73 | int req; | |
74 | int opt; | |
75 | int rst; | |
76 | SCM (*fcn)(); | |
0f2d19dd JB |
77 | { |
78 | switch GSUBR_MAKTYPE(req, opt, rst) { | |
79 | case GSUBR_MAKTYPE(0, 0, 0): return scm_make_subr(name, scm_tc7_subr_0, fcn); | |
80 | case GSUBR_MAKTYPE(1, 0, 0): return scm_make_subr(name, scm_tc7_subr_1, fcn); | |
81 | case GSUBR_MAKTYPE(0, 1, 0): return scm_make_subr(name, scm_tc7_subr_1o, fcn); | |
82 | case GSUBR_MAKTYPE(1, 1, 0): return scm_make_subr(name, scm_tc7_subr_2o, fcn); | |
83 | case GSUBR_MAKTYPE(2, 0, 0): return scm_make_subr(name, scm_tc7_subr_2, fcn); | |
84 | case GSUBR_MAKTYPE(3, 0, 0): return scm_make_subr(name, scm_tc7_subr_3, fcn); | |
85 | case GSUBR_MAKTYPE(0, 0, 1): return scm_make_subr(name, scm_tc7_lsubr, fcn); | |
86 | case GSUBR_MAKTYPE(2, 0, 1): return scm_make_subr(name, scm_tc7_lsubr_2, fcn); | |
87 | default: | |
88 | { | |
89 | SCM symcell = scm_sysintern(name, SCM_UNDEFINED); | |
90 | SCM z, cclo = scm_makcclo(f_gsubr_apply, 3L); | |
91 | long tmp = ((((SCM_CELLPTR)(SCM_CAR(symcell)))-scm_heap_org)<<8); | |
92 | if (GSUBR_MAX < req + opt + rst) { | |
93 | fputs("ERROR in scm_make_gsubr: too many args\n", stderr); | |
94 | exit (1); | |
95 | } | |
96 | if ((tmp>>8) != ((SCM_CELLPTR)(SCM_CAR(symcell))-scm_heap_org)) | |
97 | tmp = 0; | |
98 | SCM_NEWCELL(z); | |
99 | SCM_SUBRF(z) = fcn; | |
100 | SCM_CAR(z) = tmp + scm_tc7_subr_0; | |
101 | GSUBR_PROC(cclo) = z; | |
102 | GSUBR_TYPE(cclo) = SCM_MAKINUM(GSUBR_MAKTYPE(req, opt, rst)); | |
103 | SCM_CDR(symcell) = cclo; | |
104 | return cclo; | |
105 | } | |
106 | } | |
107 | } | |
108 | ||
109 | ||
110 | SCM_PROC(s_gsubr_apply, "gsubr-apply", 0, 0, 1, scm_gsubr_apply); | |
1cc91f1b | 111 | |
0f2d19dd JB |
112 | SCM |
113 | scm_gsubr_apply(args) | |
114 | SCM args; | |
0f2d19dd JB |
115 | { |
116 | SCM self = SCM_CAR(args); | |
117 | SCM (*fcn)() = SCM_SUBRF(GSUBR_PROC(self)); | |
118 | SCM v[10]; /* must agree with greatest supported arity */ | |
119 | int typ = SCM_INUM(GSUBR_TYPE(self)); | |
120 | int i, n = GSUBR_REQ(typ) + GSUBR_OPT(typ) + GSUBR_REST(typ); | |
121 | args = SCM_CDR(args); | |
122 | for (i = 0; i < GSUBR_REQ(typ); i++) { | |
123 | #ifndef RECKLESS | |
124 | if (SCM_IMP(args)) | |
f5bf2977 | 125 | scm_wrong_num_args (SCM_SNAME(GSUBR_PROC(self))); |
0f2d19dd JB |
126 | #endif |
127 | v[i] = SCM_CAR(args); | |
128 | args = SCM_CDR(args); | |
129 | } | |
130 | for (; i < GSUBR_REQ(typ) + GSUBR_OPT(typ); i++) { | |
131 | if (SCM_NIMP(args)) { | |
132 | v[i] = SCM_CAR(args); | |
133 | args = SCM_CDR(args); | |
134 | } | |
135 | else | |
136 | v[i] = SCM_UNDEFINED; | |
137 | } | |
138 | if (GSUBR_REST(typ)) | |
139 | v[i] = args; | |
140 | switch (n) { | |
141 | default: scm_wta(self, "internal programming error", s_gsubr_apply); | |
142 | case 2: return (*fcn)(v[0], v[1]); | |
143 | case 3: return (*fcn)(v[0], v[1], v[2]); | |
144 | case 4: return (*fcn)(v[0], v[1], v[2], v[3]); | |
145 | case 5: return (*fcn)(v[0], v[1], v[2], v[3], v[4]); | |
146 | case 6: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5]); | |
147 | case 7: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6]); | |
148 | case 8: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]); | |
149 | case 9: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8]); | |
150 | case 10: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9]); | |
151 | } | |
152 | } | |
153 | ||
154 | ||
155 | #ifdef GSUBR_TEST | |
156 | /* A silly example, taking 2 required args, 1 optional, and | |
157 | a scm_list of rest args | |
158 | */ | |
159 | SCM | |
160 | gsubr_21l(req1, req2, opt, rst) | |
161 | SCM req1, req2, opt, rst; | |
162 | { | |
163 | scm_gen_puts (scm_regular_string, "gsubr-2-1-l:\n req1: ", scm_cur_outp); | |
164 | scm_display(req1, scm_cur_outp); | |
165 | scm_gen_puts (scm_regular_string, "\n req2: ", scm_cur_outp); | |
166 | scm_display(req2, scm_cur_outp); | |
167 | scm_gen_puts (scm_regular_string, "\n opt: ", scm_cur_outp); | |
168 | scm_display(opt, scm_cur_outp); | |
169 | scm_gen_puts (scm_regular_string, "\n rest: ", scm_cur_outp); | |
170 | scm_display(rst, scm_cur_outp); | |
171 | scm_newline(scm_cur_outp); | |
172 | return SCM_UNSPECIFIED; | |
173 | } | |
174 | #endif | |
175 | ||
176 | ||
1cc91f1b | 177 | |
0f2d19dd JB |
178 | void |
179 | scm_init_gsubr() | |
0f2d19dd JB |
180 | { |
181 | f_gsubr_apply = scm_make_subr(s_gsubr_apply, scm_tc7_lsubr, scm_gsubr_apply); | |
182 | #ifdef GSUBR_TEST | |
183 | scm_make_gsubr("gsubr-2-1-l", 2, 1, 1, gsubr_21l); /* example */ | |
184 | #endif | |
185 | } |