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