blob: 8d20f020bcc65649881b20cea50798f5dd6d4573 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
2Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Functions used by cgen output */
26
27#include <stdio.h>
28
29#include "PROTO.h"
30#include "object.h"
31#include "intobject.h"
32#include "floatobject.h"
33#include "stringobject.h"
34#include "tupleobject.h"
35#include "listobject.h"
36#include "methodobject.h"
37#include "moduleobject.h"
38#include "modsupport.h"
39#include "import.h"
40#include "cgensupport.h"
41#include "errors.h"
42
43
44/* Functions to construct return values */
45
46object *
47mknewcharobject(c)
48 int c;
49{
50 char ch[1];
51 ch[0] = c;
52 return newsizedstringobject(ch, 1);
53}
54
55/* Functions to extract arguments.
56 These needs to know the total number of arguments supplied,
57 since the argument list is a tuple only of there is more than
58 one argument. */
59
60int
61getiobjectarg(args, nargs, i, p_arg)
62 register object *args;
63 int nargs, i;
64 object **p_arg;
65{
66 if (nargs != 1) {
67 if (args == NULL || !is_tupleobject(args) ||
68 nargs != gettuplesize(args) ||
69 i < 0 || i >= nargs) {
70 return err_badarg();
71 }
72 else {
73 args = gettupleitem(args, i);
74 }
75 }
76 if (args == NULL) {
77 return err_badarg();
78 }
79 *p_arg = args;
80 return 1;
81}
82
83int
84getilongarg(args, nargs, i, p_arg)
85 register object *args;
86 int nargs, i;
87 long *p_arg;
88{
89 if (nargs != 1) {
90 if (args == NULL || !is_tupleobject(args) ||
91 nargs != gettuplesize(args) ||
92 i < 0 || i >= nargs) {
93 return err_badarg();
94 }
95 args = gettupleitem(args, i);
96 }
97 if (args == NULL || !is_intobject(args)) {
98 return err_badarg();
99 }
100 *p_arg = getintvalue(args);
101 return 1;
102}
103
104int
105getishortarg(args, nargs, i, p_arg)
106 register object *args;
107 int nargs, i;
108 short *p_arg;
109{
110 long x;
111 if (!getilongarg(args, nargs, i, &x))
112 return 0;
113 *p_arg = x;
114 return 1;
115}
116
117static int
118extractdouble(v, p_arg)
119 register object *v;
120 double *p_arg;
121{
122 if (v == NULL) {
123 /* Fall through to error return at end of function */
124 }
125 else if (is_floatobject(v)) {
126 *p_arg = GETFLOATVALUE((floatobject *)v);
127 return 1;
128 }
129 else if (is_intobject(v)) {
130 *p_arg = GETINTVALUE((intobject *)v);
131 return 1;
132 }
133 return err_badarg();
134}
135
136static int
137extractfloat(v, p_arg)
138 register object *v;
139 float *p_arg;
140{
141 if (v == NULL) {
142 /* Fall through to error return at end of function */
143 }
144 else if (is_floatobject(v)) {
145 *p_arg = GETFLOATVALUE((floatobject *)v);
146 return 1;
147 }
148 else if (is_intobject(v)) {
149 *p_arg = GETINTVALUE((intobject *)v);
150 return 1;
151 }
152 return err_badarg();
153}
154
155int
156getifloatarg(args, nargs, i, p_arg)
157 register object *args;
158 int nargs, i;
159 float *p_arg;
160{
161 object *v;
162 float x;
163 if (!getiobjectarg(args, nargs, i, &v))
164 return 0;
165 if (!extractfloat(v, &x))
166 return 0;
167 *p_arg = x;
168 return 1;
169}
170
171int
172getistringarg(args, nargs, i, p_arg)
173 object *args;
174 int nargs, i;
175 string *p_arg;
176{
177 object *v;
178 if (!getiobjectarg(args, nargs, i, &v))
179 return NULL;
180 if (!is_stringobject(v)) {
181 return err_badarg();
182 }
183 *p_arg = getstringvalue(v);
184 return 1;
185}
186
187int
188getichararg(args, nargs, i, p_arg)
189 object *args;
190 int nargs, i;
191 char *p_arg;
192{
193 string x;
194 if (!getistringarg(args, nargs, i, &x))
195 return 0;
196 if (x[0] == '\0' || x[1] != '\0') {
197 /* Not exactly one char */
198 return err_badarg();
199 }
200 *p_arg = x[0];
201 return 1;
202}
203
204int
205getilongarraysize(args, nargs, i, p_arg)
206 object *args;
207 int nargs, i;
208 long *p_arg;
209{
210 object *v;
211 if (!getiobjectarg(args, nargs, i, &v))
212 return 0;
213 if (is_tupleobject(v)) {
214 *p_arg = gettuplesize(v);
215 return 1;
216 }
217 if (is_listobject(v)) {
218 *p_arg = getlistsize(v);
219 return 1;
220 }
221 return err_badarg();
222}
223
224int
225getishortarraysize(args, nargs, i, p_arg)
226 object *args;
227 int nargs, i;
228 short *p_arg;
229{
230 long x;
231 if (!getilongarraysize(args, nargs, i, &x))
232 return 0;
233 *p_arg = x;
234 return 1;
235}
236
237/* XXX The following four are too similar. Should share more code. */
238
239int
240getilongarray(args, nargs, i, n, p_arg)
241 object *args;
242 int nargs, i;
243 int n;
244 long *p_arg; /* [n] */
245{
246 object *v, *w;
247 if (!getiobjectarg(args, nargs, i, &v))
248 return 0;
249 if (is_tupleobject(v)) {
250 if (gettuplesize(v) != n) {
251 return err_badarg();
252 }
253 for (i = 0; i < n; i++) {
254 w = gettupleitem(v, i);
255 if (!is_intobject(w)) {
256 return err_badarg();
257 }
258 p_arg[i] = getintvalue(w);
259 }
260 return 1;
261 }
262 else if (is_listobject(v)) {
263 if (getlistsize(v) != n) {
264 return err_badarg();
265 }
266 for (i = 0; i < n; i++) {
267 w = getlistitem(v, i);
268 if (!is_intobject(w)) {
269 return err_badarg();
270 }
271 p_arg[i] = getintvalue(w);
272 }
273 return 1;
274 }
275 else {
276 return err_badarg();
277 }
278}
279
280int
281getishortarray(args, nargs, i, n, p_arg)
282 object *args;
283 int nargs, i;
284 int n;
285 short *p_arg; /* [n] */
286{
287 object *v, *w;
288 if (!getiobjectarg(args, nargs, i, &v))
289 return 0;
290 if (is_tupleobject(v)) {
291 if (gettuplesize(v) != n) {
292 return err_badarg();
293 }
294 for (i = 0; i < n; i++) {
295 w = gettupleitem(v, i);
296 if (!is_intobject(w)) {
297 return err_badarg();
298 }
299 p_arg[i] = getintvalue(w);
300 }
301 return 1;
302 }
303 else if (is_listobject(v)) {
304 if (getlistsize(v) != n) {
305 return err_badarg();
306 }
307 for (i = 0; i < n; i++) {
308 w = getlistitem(v, i);
309 if (!is_intobject(w)) {
310 return err_badarg();
311 }
312 p_arg[i] = getintvalue(w);
313 }
314 return 1;
315 }
316 else {
317 return err_badarg();
318 }
319}
320
321int
322getidoublearray(args, nargs, i, n, p_arg)
323 object *args;
324 int nargs, i;
325 int n;
326 double *p_arg; /* [n] */
327{
328 object *v, *w;
329 if (!getiobjectarg(args, nargs, i, &v))
330 return 0;
331 if (is_tupleobject(v)) {
332 if (gettuplesize(v) != n) {
333 return err_badarg();
334 }
335 for (i = 0; i < n; i++) {
336 w = gettupleitem(v, i);
337 if (!extractdouble(w, &p_arg[i]))
338 return 0;
339 }
340 return 1;
341 }
342 else if (is_listobject(v)) {
343 if (getlistsize(v) != n) {
344 return err_badarg();
345 }
346 for (i = 0; i < n; i++) {
347 w = getlistitem(v, i);
348 if (!extractdouble(w, &p_arg[i]))
349 return 0;
350 }
351 return 1;
352 }
353 else {
354 return err_badarg();
355 }
356}
357
358int
359getifloatarray(args, nargs, i, n, p_arg)
360 object *args;
361 int nargs, i;
362 int n;
363 float *p_arg; /* [n] */
364{
365 object *v, *w;
366 if (!getiobjectarg(args, nargs, i, &v))
367 return 0;
368 if (is_tupleobject(v)) {
369 if (gettuplesize(v) != n) {
370 return err_badarg();
371 }
372 for (i = 0; i < n; i++) {
373 w = gettupleitem(v, i);
374 if (!extractfloat(w, &p_arg[i]))
375 return 0;
376 }
377 return 1;
378 }
379 else if (is_listobject(v)) {
380 if (getlistsize(v) != n) {
381 return err_badarg();
382 }
383 for (i = 0; i < n; i++) {
384 w = getlistitem(v, i);
385 if (!extractfloat(w, &p_arg[i]))
386 return 0;
387 }
388 return 1;
389 }
390 else {
391 return err_badarg();
392 }
393}