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