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 | |
| 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 | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 25 | /* Compile an expression node to intermediate code */ |
| 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | /* XXX TO DO: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 28 | XXX Compute maximum needed stack sizes while compiling; |
| 29 | XXX then frame object can be one malloc and no stack checks are needed |
| 30 | XXX add __doc__ attribute == co_doc to code object attributes |
| 31 | XXX don't execute doc string |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 32 | XXX Generate simple jump for break/return outside 'try...finally' |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 33 | XXX get rid of SET_LINENO instructions, use JAR's table trick |
| 34 | XXX (need an option to put them back in, for debugger!) |
| 35 | XXX other JAR tricks? |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 36 | */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 38 | #ifndef NO_PRIVATE_NAME_MANGLING |
| 39 | #define PRIVATE_NAME_MANGLING |
| 40 | #endif |
| 41 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 42 | #include "allobjects.h" |
| 43 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 44 | #include "node.h" |
| 45 | #include "token.h" |
| 46 | #include "graminit.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 47 | #include "compile.h" |
| 48 | #include "opcode.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 49 | #include "structmember.h" |
| 50 | |
| 51 | #include <ctype.h> |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 52 | #include <errno.h> |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 54 | #define OP_DELETE 0 |
| 55 | #define OP_ASSIGN 1 |
| 56 | #define OP_APPLY 2 |
| 57 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 58 | #define OFF(x) offsetof(codeobject, x) |
| 59 | |
| 60 | static struct memberlist code_memberlist[] = { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 61 | {"co_argcount", T_INT, OFF(co_argcount), READONLY}, |
| 62 | {"co_nlocals", T_INT, OFF(co_nlocals), READONLY}, |
| 63 | {"co_flags", T_INT, OFF(co_flags), READONLY}, |
Guido van Rossum | 39d942d | 1992-01-12 02:30:05 +0000 | [diff] [blame] | 64 | {"co_code", T_OBJECT, OFF(co_code), READONLY}, |
| 65 | {"co_consts", T_OBJECT, OFF(co_consts), READONLY}, |
| 66 | {"co_names", T_OBJECT, OFF(co_names), READONLY}, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 67 | {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY}, |
Guido van Rossum | 39d942d | 1992-01-12 02:30:05 +0000 | [diff] [blame] | 68 | {"co_filename", T_OBJECT, OFF(co_filename), READONLY}, |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 69 | {"co_name", T_OBJECT, OFF(co_name), READONLY}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 70 | {NULL} /* Sentinel */ |
| 71 | }; |
| 72 | |
| 73 | static object * |
| 74 | code_getattr(co, name) |
| 75 | codeobject *co; |
| 76 | char *name; |
| 77 | { |
| 78 | return getmember((char *)co, code_memberlist, name); |
| 79 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 80 | |
| 81 | static void |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 82 | code_dealloc(co) |
| 83 | codeobject *co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 84 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 85 | XDECREF(co->co_code); |
| 86 | XDECREF(co->co_consts); |
| 87 | XDECREF(co->co_names); |
| 88 | XDECREF(co->co_filename); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 89 | XDECREF(co->co_name); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 90 | XDECREF(co->co_varnames); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 91 | DEL(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 94 | static object * |
| 95 | code_repr(co) |
| 96 | codeobject *co; |
| 97 | { |
| 98 | char buf[500]; |
| 99 | int lineno = -1; |
| 100 | char *p = GETSTRINGVALUE(co->co_code); |
| 101 | char *filename = "???"; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 102 | char *name = "???"; |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 103 | if (*p == SET_LINENO) |
| 104 | lineno = (p[1] & 0xff) | ((p[2] & 0xff) << 8); |
| 105 | if (co->co_filename && is_stringobject(co->co_filename)) |
| 106 | filename = getstringvalue(co->co_filename); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 107 | if (co->co_name && is_stringobject(co->co_name)) |
| 108 | name = getstringvalue(co->co_name); |
| 109 | sprintf(buf, "<code object %.100s at %lx, file \"%.300s\", line %d>", |
| 110 | name, (long)co, filename, lineno); |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 111 | return newstringobject(buf); |
| 112 | } |
| 113 | |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 114 | static int |
| 115 | code_compare(co, cp) |
| 116 | codeobject *co, *cp; |
| 117 | { |
| 118 | int cmp; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 119 | cmp = cp->co_argcount - cp->co_argcount; |
| 120 | if (cmp) return cmp; |
| 121 | cmp = cp->co_nlocals - cp->co_nlocals; |
| 122 | if (cmp) return cmp; |
| 123 | cmp = cp->co_flags - cp->co_flags; |
| 124 | if (cmp) return cmp; |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 125 | cmp = cmpobject((object *)co->co_code, (object *)cp->co_code); |
| 126 | if (cmp) return cmp; |
| 127 | cmp = cmpobject(co->co_consts, cp->co_consts); |
| 128 | if (cmp) return cmp; |
| 129 | cmp = cmpobject(co->co_names, cp->co_names); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 130 | if (cmp) return cmp; |
| 131 | cmp = cmpobject(co->co_varnames, cp->co_varnames); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 132 | return cmp; |
| 133 | } |
| 134 | |
| 135 | static long |
| 136 | code_hash(co) |
| 137 | codeobject *co; |
| 138 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 139 | long h, h1, h2, h3, h4; |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 140 | h1 = hashobject((object *)co->co_code); |
| 141 | if (h1 == -1) return -1; |
| 142 | h2 = hashobject(co->co_consts); |
| 143 | if (h2 == -1) return -1; |
| 144 | h3 = hashobject(co->co_names); |
| 145 | if (h3 == -1) return -1; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 146 | h4 = hashobject(co->co_varnames); |
| 147 | if (h4 == -1) return -1; |
| 148 | h = h1 ^ h2 ^ h3 ^ h4 ^ |
| 149 | co->co_argcount ^ co->co_nlocals ^ co->co_flags; |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 150 | if (h == -1) h = -2; |
| 151 | return h; |
| 152 | } |
| 153 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 154 | typeobject Codetype = { |
| 155 | OB_HEAD_INIT(&Typetype) |
| 156 | 0, |
| 157 | "code", |
| 158 | sizeof(codeobject), |
| 159 | 0, |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 160 | (destructor)code_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 161 | 0, /*tp_print*/ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 162 | (getattrfunc)code_getattr, /*tp_getattr*/ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 163 | 0, /*tp_setattr*/ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 164 | (cmpfunc)code_compare, /*tp_compare*/ |
| 165 | (reprfunc)code_repr, /*tp_repr*/ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 166 | 0, /*tp_as_number*/ |
| 167 | 0, /*tp_as_sequence*/ |
| 168 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 169 | (hashfunc)code_hash, /*tp_hash*/ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 172 | codeobject * |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 173 | newcodeobject(argcount, nlocals, flags, |
| 174 | code, consts, names, varnames, filename, name) |
| 175 | int argcount; |
| 176 | int nlocals; |
| 177 | int flags; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 178 | object *code; |
| 179 | object *consts; |
| 180 | object *names; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 181 | object *varnames; |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 182 | object *filename; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 183 | object *name; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 184 | { |
| 185 | codeobject *co; |
| 186 | int i; |
| 187 | /* Check argument types */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 188 | if (argcount < 0 || nlocals < 0 || |
| 189 | code == NULL || !is_stringobject(code) || |
| 190 | consts == NULL || !is_tupleobject(consts) || |
| 191 | names == NULL || !is_tupleobject(names) || |
| 192 | varnames == NULL || !is_tupleobject(varnames) || |
| 193 | name == NULL || !is_stringobject(name) || |
| 194 | filename == NULL || !is_stringobject(filename)) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 195 | err_badcall(); |
| 196 | return NULL; |
| 197 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 198 | /* Make sure names and varnames are all strings */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 199 | for (i = gettuplesize(names); --i >= 0; ) { |
| 200 | object *v = gettupleitem(names, i); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 201 | if (v == NULL || !is_stringobject(v)) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 202 | err_badcall(); |
| 203 | return NULL; |
| 204 | } |
| 205 | } |
| 206 | for (i = gettuplesize(varnames); --i >= 0; ) { |
| 207 | object *v = gettupleitem(varnames, i); |
| 208 | if (v == NULL || !is_stringobject(v)) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 209 | err_badcall(); |
| 210 | return NULL; |
| 211 | } |
| 212 | } |
| 213 | co = NEWOBJ(codeobject, &Codetype); |
| 214 | if (co != NULL) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 215 | co->co_argcount = argcount; |
| 216 | co->co_nlocals = nlocals; |
| 217 | co->co_flags = flags; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 218 | INCREF(code); |
| 219 | co->co_code = (stringobject *)code; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 220 | INCREF(consts); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 221 | co->co_consts = consts; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 222 | INCREF(names); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 223 | co->co_names = names; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 224 | INCREF(varnames); |
| 225 | co->co_varnames = varnames; |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 226 | INCREF(filename); |
| 227 | co->co_filename = filename; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 228 | INCREF(name); |
| 229 | co->co_name = name; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 230 | } |
| 231 | return co; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /* Data structure used internally */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 236 | |
| 237 | #define MAXBLOCKS 20 /* Max static block nesting within a function */ |
| 238 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 239 | struct compiling { |
| 240 | object *c_code; /* string */ |
| 241 | object *c_consts; /* list of objects */ |
| 242 | object *c_names; /* list of strings (names) */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 243 | object *c_globals; /* dictionary (value=None) */ |
| 244 | object *c_locals; /* dictionary (value=localID) */ |
| 245 | object *c_varnames; /* list (inverse of c_locals) */ |
| 246 | int c_nlocals; /* index of next local */ |
| 247 | int c_argcount; /* number of top-level arguments */ |
| 248 | int c_flags; /* same as co_flags */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 249 | int c_nexti; /* index into c_code */ |
| 250 | int c_errors; /* counts errors occurred */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 251 | int c_infunction; /* set when compiling a function */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 252 | int c_interactive; /* generating code for interactive command */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 253 | int c_loops; /* counts nested loops */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 254 | int c_begin; /* begin of current loop, for 'continue' */ |
| 255 | int c_block[MAXBLOCKS]; /* stack of block types */ |
| 256 | int c_nblocks; /* current block stack level */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 257 | char *c_filename; /* filename of current node */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 258 | char *c_name; /* name of object (e.g. function) */ |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 259 | int c_lineno; /* Current line number */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 260 | #ifdef PRIVATE_NAME_MANGLING |
| 261 | char *c_private; /* for private name mangling */ |
| 262 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 263 | }; |
| 264 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 265 | |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 266 | /* Error message including line number */ |
| 267 | |
| 268 | static void |
| 269 | com_error(c, exc, msg) |
| 270 | struct compiling *c; |
| 271 | object *exc; |
| 272 | char *msg; |
| 273 | { |
| 274 | int n = strlen(msg); |
| 275 | object *v; |
| 276 | char buffer[30]; |
| 277 | char *s; |
| 278 | if (c->c_lineno <= 1) { |
| 279 | /* Unknown line number or single interactive command */ |
| 280 | err_setstr(exc, msg); |
| 281 | return; |
| 282 | } |
| 283 | sprintf(buffer, " (line %d)", c->c_lineno); |
| 284 | v = newsizedstringobject((char *)NULL, n + strlen(buffer)); |
| 285 | if (v == NULL) |
| 286 | return; /* MemoryError, too bad */ |
| 287 | s = GETSTRINGVALUE((stringobject *)v); |
| 288 | strcpy(s, msg); |
| 289 | strcat(s, buffer); |
| 290 | err_setval(exc, v); |
| 291 | DECREF(v); |
| 292 | c->c_errors++; |
| 293 | } |
| 294 | |
| 295 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 296 | /* Interface to the block stack */ |
| 297 | |
| 298 | static void |
| 299 | block_push(c, type) |
| 300 | struct compiling *c; |
| 301 | int type; |
| 302 | { |
| 303 | if (c->c_nblocks >= MAXBLOCKS) { |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 304 | com_error(c, SystemError, "too many statically nested blocks"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 305 | } |
| 306 | else { |
| 307 | c->c_block[c->c_nblocks++] = type; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | static void |
| 312 | block_pop(c, type) |
| 313 | struct compiling *c; |
| 314 | int type; |
| 315 | { |
| 316 | if (c->c_nblocks > 0) |
| 317 | c->c_nblocks--; |
| 318 | if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) { |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 319 | com_error(c, SystemError, "bad block pop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
| 323 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 324 | /* Prototype forward declarations */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 325 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 326 | static int com_init PROTO((struct compiling *, char *)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 327 | static void com_free PROTO((struct compiling *)); |
| 328 | static void com_done PROTO((struct compiling *)); |
| 329 | static void com_node PROTO((struct compiling *, struct _node *)); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 330 | static void com_factor PROTO((struct compiling *, struct _node *)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 331 | static void com_addbyte PROTO((struct compiling *, int)); |
| 332 | static void com_addint PROTO((struct compiling *, int)); |
| 333 | static void com_addoparg PROTO((struct compiling *, int, int)); |
| 334 | static void com_addfwref PROTO((struct compiling *, int, int *)); |
| 335 | static void com_backpatch PROTO((struct compiling *, int)); |
| 336 | static int com_add PROTO((struct compiling *, object *, object *)); |
| 337 | static int com_addconst PROTO((struct compiling *, object *)); |
| 338 | static int com_addname PROTO((struct compiling *, object *)); |
| 339 | static void com_addopname PROTO((struct compiling *, int, node *)); |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 340 | static void com_list PROTO((struct compiling *, node *, int)); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 341 | static int com_argdefs PROTO((struct compiling *, node *)); |
| 342 | static int com_newlocal PROTO((struct compiling *, char *)); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 343 | static codeobject *icompile PROTO((struct _node *, struct compiling *)); |
| 344 | static codeobject *jcompile PROTO((struct _node *, char *, struct compiling *)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 345 | |
| 346 | static int |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 347 | com_init(c, filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 348 | struct compiling *c; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 349 | char *filename; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 350 | { |
Guido van Rossum | 62d4624 | 1991-04-03 19:00:23 +0000 | [diff] [blame] | 351 | if ((c->c_code = newsizedstringobject((char *)NULL, 1000)) == NULL) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 352 | goto fail_3; |
| 353 | if ((c->c_consts = newlistobject(0)) == NULL) |
| 354 | goto fail_2; |
| 355 | if ((c->c_names = newlistobject(0)) == NULL) |
| 356 | goto fail_1; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 357 | if ((c->c_globals = newdictobject()) == NULL) |
| 358 | goto fail_0; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 359 | if ((c->c_locals = newdictobject()) == NULL) |
| 360 | goto fail_00; |
| 361 | if ((c->c_varnames = newlistobject(0)) == NULL) |
| 362 | goto fail_000; |
| 363 | c->c_nlocals = 0; |
| 364 | c->c_argcount = 0; |
| 365 | c->c_flags = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 366 | c->c_nexti = 0; |
| 367 | c->c_errors = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 368 | c->c_infunction = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 369 | c->c_interactive = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 370 | c->c_loops = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 371 | c->c_begin = 0; |
| 372 | c->c_nblocks = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 373 | c->c_filename = filename; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 374 | c->c_name = "?"; |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 375 | c->c_lineno = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 376 | return 1; |
| 377 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 378 | fail_000: |
| 379 | DECREF(c->c_locals); |
| 380 | fail_00: |
| 381 | DECREF(c->c_globals); |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 382 | fail_0: |
| 383 | DECREF(c->c_names); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 384 | fail_1: |
| 385 | DECREF(c->c_consts); |
| 386 | fail_2: |
| 387 | DECREF(c->c_code); |
| 388 | fail_3: |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | static void |
| 393 | com_free(c) |
| 394 | struct compiling *c; |
| 395 | { |
| 396 | XDECREF(c->c_code); |
| 397 | XDECREF(c->c_consts); |
| 398 | XDECREF(c->c_names); |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 399 | XDECREF(c->c_globals); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 400 | XDECREF(c->c_locals); |
| 401 | XDECREF(c->c_varnames); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | static void |
| 405 | com_done(c) |
| 406 | struct compiling *c; |
| 407 | { |
| 408 | if (c->c_code != NULL) |
| 409 | resizestring(&c->c_code, c->c_nexti); |
| 410 | } |
| 411 | |
| 412 | static void |
| 413 | com_addbyte(c, byte) |
| 414 | struct compiling *c; |
| 415 | int byte; |
| 416 | { |
| 417 | int len; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 418 | /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 419 | if (byte < 0 || byte > 255) { |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 420 | /* |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 421 | fprintf(stderr, "XXX compiling bad byte: %d\n", byte); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 422 | fatal("com_addbyte: byte out of range"); |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 423 | */ |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 424 | com_error(c, SystemError, "com_addbyte: byte out of range"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 425 | } |
| 426 | if (c->c_code == NULL) |
| 427 | return; |
| 428 | len = getstringsize(c->c_code); |
| 429 | if (c->c_nexti >= len) { |
| 430 | if (resizestring(&c->c_code, len+1000) != 0) { |
| 431 | c->c_errors++; |
| 432 | return; |
| 433 | } |
| 434 | } |
| 435 | getstringvalue(c->c_code)[c->c_nexti++] = byte; |
| 436 | } |
| 437 | |
| 438 | static void |
| 439 | com_addint(c, x) |
| 440 | struct compiling *c; |
| 441 | int x; |
| 442 | { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 443 | com_addbyte(c, x & 0xff); |
| 444 | com_addbyte(c, x >> 8); /* XXX x should be positive */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | static void |
| 448 | com_addoparg(c, op, arg) |
| 449 | struct compiling *c; |
| 450 | int op; |
| 451 | int arg; |
| 452 | { |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 453 | if (op == SET_LINENO) |
| 454 | c->c_lineno = arg; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 455 | com_addbyte(c, op); |
| 456 | com_addint(c, arg); |
| 457 | } |
| 458 | |
| 459 | static void |
| 460 | com_addfwref(c, op, p_anchor) |
| 461 | struct compiling *c; |
| 462 | int op; |
| 463 | int *p_anchor; |
| 464 | { |
| 465 | /* Compile a forward reference for backpatching */ |
| 466 | int here; |
| 467 | int anchor; |
| 468 | com_addbyte(c, op); |
| 469 | here = c->c_nexti; |
| 470 | anchor = *p_anchor; |
| 471 | *p_anchor = here; |
| 472 | com_addint(c, anchor == 0 ? 0 : here - anchor); |
| 473 | } |
| 474 | |
| 475 | static void |
| 476 | com_backpatch(c, anchor) |
| 477 | struct compiling *c; |
| 478 | int anchor; /* Must be nonzero */ |
| 479 | { |
| 480 | unsigned char *code = (unsigned char *) getstringvalue(c->c_code); |
| 481 | int target = c->c_nexti; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 482 | int dist; |
| 483 | int prev; |
| 484 | for (;;) { |
| 485 | /* Make the JUMP instruction at anchor point to target */ |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 486 | prev = code[anchor] + (code[anchor+1] << 8); |
| 487 | dist = target - (anchor+2); |
| 488 | code[anchor] = dist & 0xff; |
| 489 | code[anchor+1] = dist >> 8; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 490 | if (!prev) |
| 491 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 492 | anchor -= prev; |
| 493 | } |
| 494 | } |
| 495 | |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 496 | /* Handle literals and names uniformly */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 497 | |
| 498 | static int |
| 499 | com_add(c, list, v) |
| 500 | struct compiling *c; |
| 501 | object *list; |
| 502 | object *v; |
| 503 | { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 504 | int n = getlistsize(list); |
| 505 | int i; |
| 506 | for (i = n; --i >= 0; ) { |
| 507 | object *w = getlistitem(list, i); |
Guido van Rossum | efc0bd0 | 1991-07-01 18:44:20 +0000 | [diff] [blame] | 508 | if (v->ob_type == w->ob_type && cmpobject(v, w) == 0) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 509 | return i; |
| 510 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 511 | if (addlistitem(list, v) != 0) |
| 512 | c->c_errors++; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 513 | return n; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | static int |
| 517 | com_addconst(c, v) |
| 518 | struct compiling *c; |
| 519 | object *v; |
| 520 | { |
| 521 | return com_add(c, c->c_consts, v); |
| 522 | } |
| 523 | |
| 524 | static int |
| 525 | com_addname(c, v) |
| 526 | struct compiling *c; |
| 527 | object *v; |
| 528 | { |
| 529 | return com_add(c, c->c_names, v); |
| 530 | } |
| 531 | |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 532 | #ifdef PRIVATE_NAME_MANGLING |
| 533 | static int |
| 534 | com_mangle(c, name, buffer, maxlen) |
| 535 | struct compiling *c; |
| 536 | char *name; |
| 537 | char *buffer; |
| 538 | int maxlen; |
| 539 | { |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 540 | /* Name mangling: __private becomes _classname__private. |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 541 | This is independent from how the name is used. */ |
| 542 | char *p; |
| 543 | int nlen, plen; |
| 544 | nlen = strlen(name); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 545 | if (nlen+2 >= maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 546 | return 0; /* Don't mangle __extremely_long_names */ |
| 547 | if (name[nlen-1] == '_' && name[nlen-2] == '_') |
| 548 | return 0; /* Don't mangle __whatever__ */ |
| 549 | p = c->c_private; |
| 550 | /* Strip leading underscores from class name */ |
| 551 | while (*p == '_') |
| 552 | p++; |
| 553 | if (*p == '\0') |
| 554 | return 0; /* Don't mangle if class is just underscores */ |
| 555 | plen = strlen(p); |
| 556 | if (plen + nlen >= maxlen) |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 557 | plen = maxlen-nlen-2; /* Truncate class name if too long */ |
| 558 | /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 559 | buffer[0] = '_'; |
| 560 | strncpy(buffer+1, p, plen); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 561 | strcpy(buffer+1+plen, name); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 562 | /* fprintf(stderr, "mangle %s -> %s\n", name, buffer); */ |
| 563 | return 1; |
| 564 | } |
| 565 | #endif |
| 566 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 567 | static void |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 568 | com_addopnamestr(c, op, name) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 569 | struct compiling *c; |
| 570 | int op; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 571 | char *name; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 572 | { |
| 573 | object *v; |
| 574 | int i; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 575 | #ifdef PRIVATE_NAME_MANGLING |
| 576 | char buffer[256]; |
| 577 | if (name != NULL && name[0] == '_' && name[1] == '_' && |
| 578 | c->c_private != NULL && |
| 579 | com_mangle(c, name, buffer, (int)sizeof(buffer))) |
| 580 | name = buffer; |
| 581 | #endif |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 582 | if (name == NULL || (v = newstringobject(name)) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 583 | c->c_errors++; |
| 584 | i = 255; |
| 585 | } |
| 586 | else { |
| 587 | i = com_addname(c, v); |
| 588 | DECREF(v); |
| 589 | } |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 590 | /* Hack to replace *_NAME opcodes by *_GLOBAL if necessary */ |
| 591 | switch (op) { |
| 592 | case LOAD_NAME: |
| 593 | case STORE_NAME: |
| 594 | case DELETE_NAME: |
| 595 | if (dictlookup(c->c_globals, name) != NULL) { |
| 596 | switch (op) { |
| 597 | case LOAD_NAME: op = LOAD_GLOBAL; break; |
| 598 | case STORE_NAME: op = STORE_GLOBAL; break; |
| 599 | case DELETE_NAME: op = DELETE_GLOBAL; break; |
| 600 | } |
| 601 | } |
| 602 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 603 | com_addoparg(c, op, i); |
| 604 | } |
| 605 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 606 | static void |
| 607 | com_addopname(c, op, n) |
| 608 | struct compiling *c; |
| 609 | int op; |
| 610 | node *n; |
| 611 | { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 612 | char *name; |
| 613 | char buffer[1000]; |
| 614 | /* XXX it is possible to write this code without the 1000 |
| 615 | chars on the total length of dotted names, I just can't be |
| 616 | bothered right now */ |
| 617 | if (TYPE(n) == STAR) |
| 618 | name = "*"; |
| 619 | else if (TYPE(n) == dotted_name) { |
| 620 | char *p = buffer; |
| 621 | int i; |
| 622 | name = buffer; |
| 623 | for (i = 0; i < NCH(n); i += 2) { |
| 624 | char *s = STR(CHILD(n, i)); |
| 625 | if (p + strlen(s) > buffer + (sizeof buffer) - 2) { |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 626 | com_error(c, MemoryError, |
| 627 | "dotted_name too long"); |
Guido van Rossum | d9dfaf5 | 1995-02-17 15:04:57 +0000 | [diff] [blame] | 628 | name = NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 629 | break; |
| 630 | } |
| 631 | if (p != buffer) |
| 632 | *p++ = '.'; |
| 633 | strcpy(p, s); |
| 634 | p = strchr(p, '\0'); |
| 635 | } |
| 636 | } |
| 637 | else { |
| 638 | REQ(n, NAME); |
| 639 | name = STR(n); |
| 640 | } |
| 641 | com_addopnamestr(c, op, name); |
| 642 | } |
| 643 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 644 | static object * |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 645 | parsenumber(co, s) |
| 646 | struct compiling *co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 647 | char *s; |
| 648 | { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 649 | extern long mystrtol PROTO((const char *, char **, int)); |
| 650 | extern unsigned long mystrtoul PROTO((const char *, char **, int)); |
| 651 | extern double atof PROTO((const char *)); |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 652 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 653 | long x; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 654 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 655 | Py_complex c; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 656 | int imflag; |
| 657 | #endif |
| 658 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 659 | errno = 0; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 660 | end = s + strlen(s) - 1; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 661 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 15ad9a6 | 1996-01-26 20:53:56 +0000 | [diff] [blame] | 662 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 663 | #endif |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 664 | if (*end == 'l' || *end == 'L') |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 665 | return long_scan(s, 0); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 666 | if (s[0] == '0') |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 667 | x = (long) mystrtoul(s, &end, 0); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 668 | else |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 669 | x = mystrtol(s, &end, 0); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 670 | if (*end == '\0') { |
| 671 | if (errno != 0) { |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 672 | com_error(co, OverflowError, |
| 673 | "integer literal too large"); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 674 | return NULL; |
| 675 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 676 | return newintobject(x); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 677 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 678 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 679 | #ifndef WITHOUT_COMPLEX |
| 680 | if (imflag) { |
| 681 | c.real = 0.; |
| 682 | c.imag = atof(s); |
| 683 | return newcomplexobject(c); |
| 684 | } |
| 685 | else |
| 686 | #endif |
| 687 | return newfloatobject(atof(s)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | static object * |
| 691 | parsestr(s) |
| 692 | char *s; |
| 693 | { |
| 694 | object *v; |
| 695 | int len; |
| 696 | char *buf; |
| 697 | char *p; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 698 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 699 | int c; |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 700 | int quote = *s; |
| 701 | if (quote != '\'' && quote != '\"') { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 702 | err_badcall(); |
| 703 | return NULL; |
| 704 | } |
| 705 | s++; |
| 706 | len = strlen(s); |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 707 | if (s[--len] != quote) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 708 | err_badcall(); |
| 709 | return NULL; |
| 710 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 711 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
| 712 | s += 2; |
| 713 | len -= 2; |
| 714 | if (s[--len] != quote || s[--len] != quote) { |
| 715 | err_badcall(); |
| 716 | return NULL; |
| 717 | } |
| 718 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 719 | if (strchr(s, '\\') == NULL) |
| 720 | return newsizedstringobject(s, len); |
| 721 | v = newsizedstringobject((char *)NULL, len); |
| 722 | p = buf = getstringvalue(v); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 723 | end = s + len; |
| 724 | while (s < end) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 725 | if (*s != '\\') { |
| 726 | *p++ = *s++; |
| 727 | continue; |
| 728 | } |
| 729 | s++; |
| 730 | switch (*s++) { |
| 731 | /* XXX This assumes ASCII! */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 732 | case '\n': break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 733 | case '\\': *p++ = '\\'; break; |
| 734 | case '\'': *p++ = '\''; break; |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 735 | case '\"': *p++ = '\"'; break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 736 | case 'b': *p++ = '\b'; break; |
| 737 | case 'f': *p++ = '\014'; break; /* FF */ |
| 738 | case 't': *p++ = '\t'; break; |
| 739 | case 'n': *p++ = '\n'; break; |
| 740 | case 'r': *p++ = '\r'; break; |
| 741 | case 'v': *p++ = '\013'; break; /* VT */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 742 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 743 | case '0': case '1': case '2': case '3': |
| 744 | case '4': case '5': case '6': case '7': |
| 745 | c = s[-1] - '0'; |
| 746 | if ('0' <= *s && *s <= '7') { |
| 747 | c = (c<<3) + *s++ - '0'; |
| 748 | if ('0' <= *s && *s <= '7') |
| 749 | c = (c<<3) + *s++ - '0'; |
| 750 | } |
| 751 | *p++ = c; |
| 752 | break; |
| 753 | case 'x': |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 754 | if (isxdigit(Py_CHARMASK(*s))) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 755 | sscanf(s, "%x", &c); |
| 756 | *p++ = c; |
| 757 | do { |
| 758 | s++; |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 759 | } while (isxdigit(Py_CHARMASK(*s))); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 760 | break; |
| 761 | } |
| 762 | /* FALLTHROUGH */ |
| 763 | default: *p++ = '\\'; *p++ = s[-1]; break; |
| 764 | } |
| 765 | } |
| 766 | resizestring(&v, (int)(p - buf)); |
| 767 | return v; |
| 768 | } |
| 769 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 770 | static object * |
| 771 | parsestrplus(n) |
| 772 | node *n; |
| 773 | { |
| 774 | object *v; |
| 775 | int i; |
| 776 | REQ(CHILD(n, 0), STRING); |
| 777 | if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) { |
| 778 | /* String literal concatenation */ |
| 779 | for (i = 1; i < NCH(n) && v != NULL; i++) { |
| 780 | joinstring_decref(&v, parsestr(STR(CHILD(n, i)))); |
| 781 | } |
| 782 | } |
| 783 | return v; |
| 784 | } |
| 785 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 786 | static void |
| 787 | com_list_constructor(c, n) |
| 788 | struct compiling *c; |
| 789 | node *n; |
| 790 | { |
| 791 | int len; |
| 792 | int i; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 793 | if (TYPE(n) != testlist) |
| 794 | REQ(n, exprlist); |
| 795 | /* exprlist: expr (',' expr)* [',']; likewise for testlist */ |
| 796 | len = (NCH(n) + 1) / 2; |
| 797 | for (i = 0; i < NCH(n); i += 2) |
| 798 | com_node(c, CHILD(n, i)); |
| 799 | com_addoparg(c, BUILD_LIST, len); |
| 800 | } |
| 801 | |
| 802 | static void |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 803 | com_dictmaker(c, n) |
| 804 | struct compiling *c; |
| 805 | node *n; |
| 806 | { |
| 807 | int i; |
| 808 | /* dictmaker: test ':' test (',' test ':' value)* [','] */ |
| 809 | for (i = 0; i+2 < NCH(n); i += 4) { |
| 810 | /* We must arrange things just right for STORE_SUBSCR. |
| 811 | It wants the stack to look like (value) (dict) (key) */ |
| 812 | com_addbyte(c, DUP_TOP); |
| 813 | com_node(c, CHILD(n, i+2)); /* value */ |
| 814 | com_addbyte(c, ROT_TWO); |
| 815 | com_node(c, CHILD(n, i)); /* key */ |
| 816 | com_addbyte(c, STORE_SUBSCR); |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 821 | com_atom(c, n) |
| 822 | struct compiling *c; |
| 823 | node *n; |
| 824 | { |
| 825 | node *ch; |
| 826 | object *v; |
| 827 | int i; |
| 828 | REQ(n, atom); |
| 829 | ch = CHILD(n, 0); |
| 830 | switch (TYPE(ch)) { |
| 831 | case LPAR: |
| 832 | if (TYPE(CHILD(n, 1)) == RPAR) |
| 833 | com_addoparg(c, BUILD_TUPLE, 0); |
| 834 | else |
| 835 | com_node(c, CHILD(n, 1)); |
| 836 | break; |
| 837 | case LSQB: |
| 838 | if (TYPE(CHILD(n, 1)) == RSQB) |
| 839 | com_addoparg(c, BUILD_LIST, 0); |
| 840 | else |
| 841 | com_list_constructor(c, CHILD(n, 1)); |
| 842 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 843 | case LBRACE: /* '{' [dictmaker] '}' */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 844 | com_addoparg(c, BUILD_MAP, 0); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 845 | if (TYPE(CHILD(n, 1)) != RBRACE) |
| 846 | com_dictmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 847 | break; |
| 848 | case BACKQUOTE: |
| 849 | com_node(c, CHILD(n, 1)); |
| 850 | com_addbyte(c, UNARY_CONVERT); |
| 851 | break; |
| 852 | case NUMBER: |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 853 | if ((v = parsenumber(c, STR(ch))) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 854 | i = 255; |
| 855 | } |
| 856 | else { |
| 857 | i = com_addconst(c, v); |
| 858 | DECREF(v); |
| 859 | } |
| 860 | com_addoparg(c, LOAD_CONST, i); |
| 861 | break; |
| 862 | case STRING: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 863 | v = parsestrplus(n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 864 | if (v == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 865 | c->c_errors++; |
| 866 | i = 255; |
| 867 | } |
| 868 | else { |
| 869 | i = com_addconst(c, v); |
| 870 | DECREF(v); |
| 871 | } |
| 872 | com_addoparg(c, LOAD_CONST, i); |
| 873 | break; |
| 874 | case NAME: |
| 875 | com_addopname(c, LOAD_NAME, ch); |
| 876 | break; |
| 877 | default: |
| 878 | fprintf(stderr, "node type %d\n", TYPE(ch)); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 879 | com_error(c, SystemError, "com_atom: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | |
| 883 | static void |
| 884 | com_slice(c, n, op) |
| 885 | struct compiling *c; |
| 886 | node *n; |
| 887 | int op; |
| 888 | { |
| 889 | if (NCH(n) == 1) { |
| 890 | com_addbyte(c, op); |
| 891 | } |
| 892 | else if (NCH(n) == 2) { |
| 893 | if (TYPE(CHILD(n, 0)) != COLON) { |
| 894 | com_node(c, CHILD(n, 0)); |
| 895 | com_addbyte(c, op+1); |
| 896 | } |
| 897 | else { |
| 898 | com_node(c, CHILD(n, 1)); |
| 899 | com_addbyte(c, op+2); |
| 900 | } |
| 901 | } |
| 902 | else { |
| 903 | com_node(c, CHILD(n, 0)); |
| 904 | com_node(c, CHILD(n, 2)); |
| 905 | com_addbyte(c, op+3); |
| 906 | } |
| 907 | } |
| 908 | |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 909 | static int |
| 910 | com_argument(c, n, inkeywords) |
| 911 | struct compiling *c; |
| 912 | node *n; /* argument */ |
| 913 | int inkeywords; |
| 914 | { |
| 915 | node *m; |
| 916 | REQ(n, argument); /* [test '='] test; really [ keyword '='] keyword */ |
| 917 | if (NCH(n) == 1) { |
| 918 | if (inkeywords) { |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 919 | com_error(c, SyntaxError, |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 920 | "non-keyword arg after keyword arg"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 921 | } |
| 922 | else { |
| 923 | com_node(c, CHILD(n, 0)); |
| 924 | } |
| 925 | return 0; |
| 926 | } |
| 927 | m = n; |
| 928 | do { |
| 929 | m = CHILD(m, 0); |
| 930 | } while (NCH(m) == 1); |
| 931 | if (TYPE(m) != NAME) { |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 932 | com_error(c, SyntaxError, "keyword can't be an expression"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 933 | } |
| 934 | else { |
| 935 | object *v = newstringobject(STR(m)); |
| 936 | if (v == NULL) |
| 937 | c->c_errors++; |
| 938 | else { |
| 939 | com_addoparg(c, LOAD_CONST, com_addconst(c, v)); |
| 940 | DECREF(v); |
| 941 | } |
| 942 | } |
| 943 | com_node(c, CHILD(n, 2)); |
| 944 | return 1; |
| 945 | } |
| 946 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 947 | static void |
| 948 | com_call_function(c, n) |
| 949 | struct compiling *c; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 950 | node *n; /* EITHER arglist OR ')' */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 951 | { |
| 952 | if (TYPE(n) == RPAR) { |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 953 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 954 | } |
| 955 | else { |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 956 | int inkeywords, i, na, nk; |
| 957 | REQ(n, arglist); |
| 958 | inkeywords = 0; |
| 959 | na = 0; |
| 960 | nk = 0; |
| 961 | for (i = 0; i < NCH(n); i += 2) { |
| 962 | inkeywords = com_argument(c, CHILD(n, i), inkeywords); |
| 963 | if (!inkeywords) |
| 964 | na++; |
| 965 | else |
| 966 | nk++; |
| 967 | } |
| 968 | if (na > 255 || nk > 255) { |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 969 | com_error(c, SyntaxError, "more than 255 arguments"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 970 | } |
| 971 | com_addoparg(c, CALL_FUNCTION, na | (nk << 8)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | |
| 975 | static void |
| 976 | com_select_member(c, n) |
| 977 | struct compiling *c; |
| 978 | node *n; |
| 979 | { |
| 980 | com_addopname(c, LOAD_ATTR, n); |
| 981 | } |
| 982 | |
| 983 | static void |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 984 | com_sliceobj(c, n) |
| 985 | struct compiling *c; |
| 986 | node *n; |
| 987 | { |
| 988 | int i=0; |
| 989 | int ns=2; /* number of slice arguments */ |
| 990 | int first_missing=0; |
| 991 | node *ch; |
| 992 | |
| 993 | /* first argument */ |
| 994 | if (TYPE(CHILD(n,i)) == COLON) { |
| 995 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 996 | i++; |
| 997 | } |
| 998 | else { |
| 999 | com_node(c, CHILD(n,i)); |
| 1000 | i++; |
| 1001 | REQ(CHILD(n,i),COLON); |
| 1002 | i++; |
| 1003 | } |
| 1004 | /* second argument */ |
| 1005 | if (i < NCH(n) && TYPE(CHILD(n,i)) == test) { |
| 1006 | com_node(c, CHILD(n,i)); |
| 1007 | i++; |
| 1008 | } |
| 1009 | else com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 1010 | /* remaining arguments */ |
| 1011 | for (; i < NCH(n); i++) { |
| 1012 | ns++; |
| 1013 | ch=CHILD(n,i); |
| 1014 | REQ(ch, sliceop); |
| 1015 | if (NCH(ch) == 1) { |
| 1016 | /* right argument of ':' missing */ |
| 1017 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 1018 | } |
| 1019 | else |
| 1020 | com_node(c, CHILD(ch,1)); |
| 1021 | } |
| 1022 | com_addoparg(c, BUILD_SLICE, ns); |
| 1023 | } |
| 1024 | |
| 1025 | static void |
| 1026 | com_subscript(c, n) |
| 1027 | struct compiling *c; |
| 1028 | node *n; |
| 1029 | { |
| 1030 | node *ch; |
| 1031 | REQ(n, subscript); |
| 1032 | ch = CHILD(n,0); |
| 1033 | /* check for rubber index */ |
| 1034 | if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) |
| 1035 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipses)); |
| 1036 | else { |
| 1037 | /* check for slice */ |
| 1038 | if ((TYPE(ch) == COLON || NCH(n) > 1)) |
| 1039 | com_sliceobj(c, n); |
| 1040 | else { |
| 1041 | REQ(ch, test); |
| 1042 | com_node(c, ch); |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | static void |
| 1048 | com_subscriptlist(c, n, assigning) |
| 1049 | struct compiling *c; |
| 1050 | node *n; |
| 1051 | int assigning; |
| 1052 | { |
| 1053 | int i, op; |
| 1054 | REQ(n, subscriptlist); |
| 1055 | /* Check to make backward compatible slice behavior for '[i:j]' */ |
| 1056 | if (NCH(n) == 1) { |
| 1057 | node *sub = CHILD(n, 0); /* subscript */ |
| 1058 | /* Make it is a simple slice. |
| 1059 | should have exactly one colon. */ |
| 1060 | if ((TYPE(CHILD(sub, 0)) == COLON |
| 1061 | || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON)) |
| 1062 | && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop)) { |
| 1063 | if (assigning == OP_APPLY) |
| 1064 | op = SLICE; |
| 1065 | else |
| 1066 | op = ((assigning == OP_ASSIGN) ? STORE_SLICE : DELETE_SLICE); |
| 1067 | com_slice(c, sub, op); |
| 1068 | return; |
| 1069 | } |
| 1070 | } |
| 1071 | /* Else normal subscriptlist. Compile each subscript. */ |
| 1072 | for (i = 0; i < NCH(n); i += 2) |
| 1073 | com_subscript(c, CHILD(n, i)); |
| 1074 | /* Put multiple subscripts into a tuple */ |
| 1075 | if (NCH(n) > 1) |
| 1076 | com_addoparg(c, BUILD_TUPLE, (NCH(n)+1) / 2); |
| 1077 | if (assigning == OP_APPLY) |
| 1078 | op = BINARY_SUBSCR; |
| 1079 | else |
| 1080 | op = ((assigning == OP_ASSIGN) ? STORE_SUBSCR : DELETE_SUBSCR); |
| 1081 | com_addbyte(c, op); |
| 1082 | } |
| 1083 | |
| 1084 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1085 | com_apply_trailer(c, n) |
| 1086 | struct compiling *c; |
| 1087 | node *n; |
| 1088 | { |
| 1089 | REQ(n, trailer); |
| 1090 | switch (TYPE(CHILD(n, 0))) { |
| 1091 | case LPAR: |
| 1092 | com_call_function(c, CHILD(n, 1)); |
| 1093 | break; |
| 1094 | case DOT: |
| 1095 | com_select_member(c, CHILD(n, 1)); |
| 1096 | break; |
| 1097 | case LSQB: |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1098 | com_subscriptlist(c, CHILD(n, 1), OP_APPLY); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1099 | break; |
| 1100 | default: |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1101 | com_error(c, SystemError, |
| 1102 | "com_apply_trailer: unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | static void |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1107 | com_power(c, n) |
| 1108 | struct compiling *c; |
| 1109 | node *n; |
| 1110 | { |
| 1111 | int i; |
| 1112 | REQ(n, power); |
| 1113 | com_atom(c, CHILD(n, 0)); |
| 1114 | for (i = 1; i < NCH(n); i++) { |
| 1115 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
| 1116 | com_factor(c, CHILD(n, i+1)); |
| 1117 | com_addbyte(c, BINARY_POWER); |
| 1118 | break; |
| 1119 | } |
| 1120 | else |
| 1121 | com_apply_trailer(c, CHILD(n, i)); |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1126 | com_factor(c, n) |
| 1127 | struct compiling *c; |
| 1128 | node *n; |
| 1129 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1130 | REQ(n, factor); |
| 1131 | if (TYPE(CHILD(n, 0)) == PLUS) { |
| 1132 | com_factor(c, CHILD(n, 1)); |
| 1133 | com_addbyte(c, UNARY_POSITIVE); |
| 1134 | } |
| 1135 | else if (TYPE(CHILD(n, 0)) == MINUS) { |
| 1136 | com_factor(c, CHILD(n, 1)); |
| 1137 | com_addbyte(c, UNARY_NEGATIVE); |
| 1138 | } |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1139 | else if (TYPE(CHILD(n, 0)) == TILDE) { |
| 1140 | com_factor(c, CHILD(n, 1)); |
| 1141 | com_addbyte(c, UNARY_INVERT); |
| 1142 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1143 | else { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1144 | com_power(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | static void |
| 1149 | com_term(c, n) |
| 1150 | struct compiling *c; |
| 1151 | node *n; |
| 1152 | { |
| 1153 | int i; |
| 1154 | int op; |
| 1155 | REQ(n, term); |
| 1156 | com_factor(c, CHILD(n, 0)); |
| 1157 | for (i = 2; i < NCH(n); i += 2) { |
| 1158 | com_factor(c, CHILD(n, i)); |
| 1159 | switch (TYPE(CHILD(n, i-1))) { |
| 1160 | case STAR: |
| 1161 | op = BINARY_MULTIPLY; |
| 1162 | break; |
| 1163 | case SLASH: |
| 1164 | op = BINARY_DIVIDE; |
| 1165 | break; |
| 1166 | case PERCENT: |
| 1167 | op = BINARY_MODULO; |
| 1168 | break; |
| 1169 | default: |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1170 | com_error(c, SystemError, |
| 1171 | "com_term: operator not *, / or %"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1172 | op = 255; |
| 1173 | } |
| 1174 | com_addbyte(c, op); |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | static void |
| 1179 | com_arith_expr(c, n) |
| 1180 | struct compiling *c; |
| 1181 | node *n; |
| 1182 | { |
| 1183 | int i; |
| 1184 | int op; |
| 1185 | REQ(n, arith_expr); |
| 1186 | com_term(c, CHILD(n, 0)); |
| 1187 | for (i = 2; i < NCH(n); i += 2) { |
| 1188 | com_term(c, CHILD(n, i)); |
| 1189 | switch (TYPE(CHILD(n, i-1))) { |
| 1190 | case PLUS: |
| 1191 | op = BINARY_ADD; |
| 1192 | break; |
| 1193 | case MINUS: |
| 1194 | op = BINARY_SUBTRACT; |
| 1195 | break; |
| 1196 | default: |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1197 | com_error(c, SystemError, |
| 1198 | "com_arith_expr: operator not + or -"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1199 | op = 255; |
| 1200 | } |
| 1201 | com_addbyte(c, op); |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | static void |
| 1206 | com_shift_expr(c, n) |
| 1207 | struct compiling *c; |
| 1208 | node *n; |
| 1209 | { |
| 1210 | int i; |
| 1211 | int op; |
| 1212 | REQ(n, shift_expr); |
| 1213 | com_arith_expr(c, CHILD(n, 0)); |
| 1214 | for (i = 2; i < NCH(n); i += 2) { |
| 1215 | com_arith_expr(c, CHILD(n, i)); |
| 1216 | switch (TYPE(CHILD(n, i-1))) { |
| 1217 | case LEFTSHIFT: |
| 1218 | op = BINARY_LSHIFT; |
| 1219 | break; |
| 1220 | case RIGHTSHIFT: |
| 1221 | op = BINARY_RSHIFT; |
| 1222 | break; |
| 1223 | default: |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1224 | com_error(c, SystemError, |
| 1225 | "com_shift_expr: operator not << or >>"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1226 | op = 255; |
| 1227 | } |
| 1228 | com_addbyte(c, op); |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | static void |
| 1233 | com_and_expr(c, n) |
| 1234 | struct compiling *c; |
| 1235 | node *n; |
| 1236 | { |
| 1237 | int i; |
| 1238 | int op; |
| 1239 | REQ(n, and_expr); |
| 1240 | com_shift_expr(c, CHILD(n, 0)); |
| 1241 | for (i = 2; i < NCH(n); i += 2) { |
| 1242 | com_shift_expr(c, CHILD(n, i)); |
| 1243 | if (TYPE(CHILD(n, i-1)) == AMPER) { |
| 1244 | op = BINARY_AND; |
| 1245 | } |
| 1246 | else { |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1247 | com_error(c, SystemError, |
| 1248 | "com_and_expr: operator not &"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1249 | op = 255; |
| 1250 | } |
| 1251 | com_addbyte(c, op); |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | static void |
| 1256 | com_xor_expr(c, n) |
| 1257 | struct compiling *c; |
| 1258 | node *n; |
| 1259 | { |
| 1260 | int i; |
| 1261 | int op; |
| 1262 | REQ(n, xor_expr); |
| 1263 | com_and_expr(c, CHILD(n, 0)); |
| 1264 | for (i = 2; i < NCH(n); i += 2) { |
| 1265 | com_and_expr(c, CHILD(n, i)); |
| 1266 | if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) { |
| 1267 | op = BINARY_XOR; |
| 1268 | } |
| 1269 | else { |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1270 | com_error(c, SystemError, |
| 1271 | "com_xor_expr: operator not ^"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1272 | op = 255; |
| 1273 | } |
| 1274 | com_addbyte(c, op); |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | static void |
| 1279 | com_expr(c, n) |
| 1280 | struct compiling *c; |
| 1281 | node *n; |
| 1282 | { |
| 1283 | int i; |
| 1284 | int op; |
| 1285 | REQ(n, expr); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1286 | com_xor_expr(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1287 | for (i = 2; i < NCH(n); i += 2) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1288 | com_xor_expr(c, CHILD(n, i)); |
| 1289 | if (TYPE(CHILD(n, i-1)) == VBAR) { |
| 1290 | op = BINARY_OR; |
| 1291 | } |
| 1292 | else { |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1293 | com_error(c, SystemError, |
| 1294 | "com_expr: expr operator not |"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1295 | op = 255; |
| 1296 | } |
| 1297 | com_addbyte(c, op); |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | static enum cmp_op |
| 1302 | cmp_type(n) |
| 1303 | node *n; |
| 1304 | { |
| 1305 | REQ(n, comp_op); |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 1306 | /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '==' |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1307 | | 'in' | 'not' 'in' | 'is' | 'is' not' */ |
| 1308 | if (NCH(n) == 1) { |
| 1309 | n = CHILD(n, 0); |
| 1310 | switch (TYPE(n)) { |
| 1311 | case LESS: return LT; |
| 1312 | case GREATER: return GT; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 1313 | case EQEQUAL: /* == */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1314 | case EQUAL: return EQ; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 1315 | case LESSEQUAL: return LE; |
| 1316 | case GREATEREQUAL: return GE; |
| 1317 | case NOTEQUAL: return NE; /* <> or != */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1318 | case NAME: if (strcmp(STR(n), "in") == 0) return IN; |
| 1319 | if (strcmp(STR(n), "is") == 0) return IS; |
| 1320 | } |
| 1321 | } |
| 1322 | else if (NCH(n) == 2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1323 | switch (TYPE(CHILD(n, 0))) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1324 | case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 1325 | return NOT_IN; |
| 1326 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 1327 | return IS_NOT; |
| 1328 | } |
| 1329 | } |
| 1330 | return BAD; |
| 1331 | } |
| 1332 | |
| 1333 | static void |
| 1334 | com_comparison(c, n) |
| 1335 | struct compiling *c; |
| 1336 | node *n; |
| 1337 | { |
| 1338 | int i; |
| 1339 | enum cmp_op op; |
| 1340 | int anchor; |
| 1341 | REQ(n, comparison); /* comparison: expr (comp_op expr)* */ |
| 1342 | com_expr(c, CHILD(n, 0)); |
| 1343 | if (NCH(n) == 1) |
| 1344 | return; |
| 1345 | |
| 1346 | /**************************************************************** |
| 1347 | The following code is generated for all but the last |
| 1348 | comparison in a chain: |
| 1349 | |
| 1350 | label: on stack: opcode: jump to: |
| 1351 | |
| 1352 | a <code to load b> |
| 1353 | a, b DUP_TOP |
| 1354 | a, b, b ROT_THREE |
| 1355 | b, a, b COMPARE_OP |
| 1356 | b, 0-or-1 JUMP_IF_FALSE L1 |
| 1357 | b, 1 POP_TOP |
| 1358 | b |
| 1359 | |
| 1360 | We are now ready to repeat this sequence for the next |
| 1361 | comparison in the chain. |
| 1362 | |
| 1363 | For the last we generate: |
| 1364 | |
| 1365 | b <code to load c> |
| 1366 | b, c COMPARE_OP |
| 1367 | 0-or-1 |
| 1368 | |
| 1369 | If there were any jumps to L1 (i.e., there was more than one |
| 1370 | comparison), we generate: |
| 1371 | |
| 1372 | 0-or-1 JUMP_FORWARD L2 |
| 1373 | L1: b, 0 ROT_TWO |
| 1374 | 0, b POP_TOP |
| 1375 | 0 |
| 1376 | L2: |
| 1377 | ****************************************************************/ |
| 1378 | |
| 1379 | anchor = 0; |
| 1380 | |
| 1381 | for (i = 2; i < NCH(n); i += 2) { |
| 1382 | com_expr(c, CHILD(n, i)); |
| 1383 | if (i+2 < NCH(n)) { |
| 1384 | com_addbyte(c, DUP_TOP); |
| 1385 | com_addbyte(c, ROT_THREE); |
| 1386 | } |
| 1387 | op = cmp_type(CHILD(n, i-1)); |
| 1388 | if (op == BAD) { |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1389 | com_error(c, SystemError, |
| 1390 | "com_comparison: unknown comparison op"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1391 | } |
| 1392 | com_addoparg(c, COMPARE_OP, op); |
| 1393 | if (i+2 < NCH(n)) { |
| 1394 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 1395 | com_addbyte(c, POP_TOP); |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | if (anchor) { |
| 1400 | int anchor2 = 0; |
| 1401 | com_addfwref(c, JUMP_FORWARD, &anchor2); |
| 1402 | com_backpatch(c, anchor); |
| 1403 | com_addbyte(c, ROT_TWO); |
| 1404 | com_addbyte(c, POP_TOP); |
| 1405 | com_backpatch(c, anchor2); |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | static void |
| 1410 | com_not_test(c, n) |
| 1411 | struct compiling *c; |
| 1412 | node *n; |
| 1413 | { |
| 1414 | REQ(n, not_test); /* 'not' not_test | comparison */ |
| 1415 | if (NCH(n) == 1) { |
| 1416 | com_comparison(c, CHILD(n, 0)); |
| 1417 | } |
| 1418 | else { |
| 1419 | com_not_test(c, CHILD(n, 1)); |
| 1420 | com_addbyte(c, UNARY_NOT); |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | static void |
| 1425 | com_and_test(c, n) |
| 1426 | struct compiling *c; |
| 1427 | node *n; |
| 1428 | { |
| 1429 | int i; |
| 1430 | int anchor; |
| 1431 | REQ(n, and_test); /* not_test ('and' not_test)* */ |
| 1432 | anchor = 0; |
| 1433 | i = 0; |
| 1434 | for (;;) { |
| 1435 | com_not_test(c, CHILD(n, i)); |
| 1436 | if ((i += 2) >= NCH(n)) |
| 1437 | break; |
| 1438 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 1439 | com_addbyte(c, POP_TOP); |
| 1440 | } |
| 1441 | if (anchor) |
| 1442 | com_backpatch(c, anchor); |
| 1443 | } |
| 1444 | |
| 1445 | static void |
| 1446 | com_test(c, n) |
| 1447 | struct compiling *c; |
| 1448 | node *n; |
| 1449 | { |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 1450 | REQ(n, test); /* and_test ('and' and_test)* | lambdef */ |
| 1451 | if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) { |
| 1452 | object *v; |
| 1453 | int i; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1454 | int ndefs = com_argdefs(c, CHILD(n, 0)); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1455 | v = (object *) icompile(CHILD(n, 0), c); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 1456 | if (v == NULL) { |
| 1457 | c->c_errors++; |
| 1458 | i = 255; |
| 1459 | } |
| 1460 | else { |
| 1461 | i = com_addconst(c, v); |
| 1462 | DECREF(v); |
| 1463 | } |
| 1464 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1465 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1466 | } |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 1467 | else { |
| 1468 | int anchor = 0; |
| 1469 | int i = 0; |
| 1470 | for (;;) { |
| 1471 | com_and_test(c, CHILD(n, i)); |
| 1472 | if ((i += 2) >= NCH(n)) |
| 1473 | break; |
| 1474 | com_addfwref(c, JUMP_IF_TRUE, &anchor); |
| 1475 | com_addbyte(c, POP_TOP); |
| 1476 | } |
| 1477 | if (anchor) |
| 1478 | com_backpatch(c, anchor); |
| 1479 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | static void |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 1483 | com_list(c, n, toplevel) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1484 | struct compiling *c; |
| 1485 | node *n; |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 1486 | int toplevel; /* If nonzero, *always* build a tuple */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1487 | { |
| 1488 | /* exprlist: expr (',' expr)* [',']; likewise for testlist */ |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 1489 | if (NCH(n) == 1 && !toplevel) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1490 | com_node(c, CHILD(n, 0)); |
| 1491 | } |
| 1492 | else { |
| 1493 | int i; |
| 1494 | int len; |
| 1495 | len = (NCH(n) + 1) / 2; |
| 1496 | for (i = 0; i < NCH(n); i += 2) |
| 1497 | com_node(c, CHILD(n, i)); |
| 1498 | com_addoparg(c, BUILD_TUPLE, len); |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | |
| 1503 | /* Begin of assignment compilation */ |
| 1504 | |
| 1505 | static void com_assign_name PROTO((struct compiling *, node *, int)); |
| 1506 | static void com_assign PROTO((struct compiling *, node *, int)); |
| 1507 | |
| 1508 | static void |
| 1509 | com_assign_attr(c, n, assigning) |
| 1510 | struct compiling *c; |
| 1511 | node *n; |
| 1512 | int assigning; |
| 1513 | { |
| 1514 | com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n); |
| 1515 | } |
| 1516 | |
| 1517 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1518 | com_assign_trailer(c, n, assigning) |
| 1519 | struct compiling *c; |
| 1520 | node *n; |
| 1521 | int assigning; |
| 1522 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1523 | REQ(n, trailer); |
| 1524 | switch (TYPE(CHILD(n, 0))) { |
| 1525 | case LPAR: /* '(' [exprlist] ')' */ |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1526 | com_error(c, SyntaxError, "can't assign to function call"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1527 | break; |
| 1528 | case DOT: /* '.' NAME */ |
| 1529 | com_assign_attr(c, CHILD(n, 1), assigning); |
| 1530 | break; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1531 | case LSQB: /* '[' subscriptlist ']' */ |
| 1532 | com_subscriptlist(c, CHILD(n, 1), assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1533 | break; |
| 1534 | default: |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1535 | com_error(c, SystemError, "unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | static void |
| 1540 | com_assign_tuple(c, n, assigning) |
| 1541 | struct compiling *c; |
| 1542 | node *n; |
| 1543 | int assigning; |
| 1544 | { |
| 1545 | int i; |
| 1546 | if (TYPE(n) != testlist) |
| 1547 | REQ(n, exprlist); |
| 1548 | if (assigning) |
| 1549 | com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2); |
| 1550 | for (i = 0; i < NCH(n); i += 2) |
| 1551 | com_assign(c, CHILD(n, i), assigning); |
| 1552 | } |
| 1553 | |
| 1554 | static void |
| 1555 | com_assign_list(c, n, assigning) |
| 1556 | struct compiling *c; |
| 1557 | node *n; |
| 1558 | int assigning; |
| 1559 | { |
| 1560 | int i; |
| 1561 | if (assigning) |
| 1562 | com_addoparg(c, UNPACK_LIST, (NCH(n)+1)/2); |
| 1563 | for (i = 0; i < NCH(n); i += 2) |
| 1564 | com_assign(c, CHILD(n, i), assigning); |
| 1565 | } |
| 1566 | |
| 1567 | static void |
| 1568 | com_assign_name(c, n, assigning) |
| 1569 | struct compiling *c; |
| 1570 | node *n; |
| 1571 | int assigning; |
| 1572 | { |
| 1573 | REQ(n, NAME); |
| 1574 | com_addopname(c, assigning ? STORE_NAME : DELETE_NAME, n); |
| 1575 | } |
| 1576 | |
| 1577 | static void |
| 1578 | com_assign(c, n, assigning) |
| 1579 | struct compiling *c; |
| 1580 | node *n; |
| 1581 | int assigning; |
| 1582 | { |
| 1583 | /* Loop to avoid trivial recursion */ |
| 1584 | for (;;) { |
| 1585 | switch (TYPE(n)) { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1586 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1587 | case exprlist: |
| 1588 | case testlist: |
| 1589 | if (NCH(n) > 1) { |
| 1590 | com_assign_tuple(c, n, assigning); |
| 1591 | return; |
| 1592 | } |
| 1593 | n = CHILD(n, 0); |
| 1594 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1595 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1596 | case test: |
| 1597 | case and_test: |
| 1598 | case not_test: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1599 | case comparison: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1600 | case expr: |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1601 | case xor_expr: |
| 1602 | case and_expr: |
| 1603 | case shift_expr: |
| 1604 | case arith_expr: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1605 | case term: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1606 | case factor: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1607 | if (NCH(n) > 1) { |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1608 | com_error(c, SyntaxError, |
| 1609 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1610 | return; |
| 1611 | } |
| 1612 | n = CHILD(n, 0); |
| 1613 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1614 | |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1615 | case power: /* atom trailer* ('**' power)* */ |
| 1616 | /* ('+'|'-'|'~') factor | atom trailer* */ |
| 1617 | if (TYPE(CHILD(n, 0)) != atom) { |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1618 | com_error(c, SyntaxError, |
| 1619 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1620 | return; |
| 1621 | } |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1622 | if (NCH(n) > 1) { /* trailer or exponent present */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1623 | int i; |
| 1624 | com_node(c, CHILD(n, 0)); |
| 1625 | for (i = 1; i+1 < NCH(n); i++) { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1626 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1627 | com_error(c, SyntaxError, |
| 1628 | "can't assign to operator"); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1629 | return; |
| 1630 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1631 | com_apply_trailer(c, CHILD(n, i)); |
| 1632 | } /* NB i is still alive */ |
| 1633 | com_assign_trailer(c, |
| 1634 | CHILD(n, i), assigning); |
| 1635 | return; |
| 1636 | } |
| 1637 | n = CHILD(n, 0); |
| 1638 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1639 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1640 | case atom: |
| 1641 | switch (TYPE(CHILD(n, 0))) { |
| 1642 | case LPAR: |
| 1643 | n = CHILD(n, 1); |
| 1644 | if (TYPE(n) == RPAR) { |
| 1645 | /* XXX Should allow () = () ??? */ |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1646 | com_error(c, SyntaxError, |
| 1647 | "can't assign to ()"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1648 | return; |
| 1649 | } |
| 1650 | break; |
| 1651 | case LSQB: |
| 1652 | n = CHILD(n, 1); |
| 1653 | if (TYPE(n) == RSQB) { |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1654 | com_error(c, SyntaxError, |
| 1655 | "can't assign to []"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1656 | return; |
| 1657 | } |
| 1658 | com_assign_list(c, n, assigning); |
| 1659 | return; |
| 1660 | case NAME: |
| 1661 | com_assign_name(c, CHILD(n, 0), assigning); |
| 1662 | return; |
| 1663 | default: |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1664 | com_error(c, SyntaxError, |
| 1665 | "can't assign to literal"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1666 | return; |
| 1667 | } |
| 1668 | break; |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 1669 | |
| 1670 | case lambdef: |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1671 | com_error(c, SyntaxError, "can't assign to lambda"); |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 1672 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1673 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1674 | default: |
| 1675 | fprintf(stderr, "node type %d\n", TYPE(n)); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1676 | com_error(c, SystemError, "com_assign: bad node"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1677 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1678 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1679 | } |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | static void |
| 1684 | com_expr_stmt(c, n) |
| 1685 | struct compiling *c; |
| 1686 | node *n; |
| 1687 | { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1688 | REQ(n, expr_stmt); /* testlist ('=' testlist)* */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1689 | com_node(c, CHILD(n, NCH(n)-1)); |
| 1690 | if (NCH(n) == 1) { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1691 | if (c->c_interactive) |
| 1692 | com_addbyte(c, PRINT_EXPR); |
| 1693 | else |
| 1694 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1695 | } |
| 1696 | else { |
| 1697 | int i; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1698 | for (i = 0; i < NCH(n)-2; i+=2) { |
| 1699 | if (i+2 < NCH(n)-2) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1700 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1701 | com_assign(c, CHILD(n, i), OP_ASSIGN); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1702 | } |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | static void |
| 1707 | com_print_stmt(c, n) |
| 1708 | struct compiling *c; |
| 1709 | node *n; |
| 1710 | { |
| 1711 | int i; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1712 | REQ(n, print_stmt); /* 'print' (test ',')* [test] */ |
| 1713 | for (i = 1; i < NCH(n); i += 2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1714 | com_node(c, CHILD(n, i)); |
| 1715 | com_addbyte(c, PRINT_ITEM); |
| 1716 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1717 | if (TYPE(CHILD(n, NCH(n)-1)) != COMMA) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1718 | com_addbyte(c, PRINT_NEWLINE); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1719 | /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | static void |
| 1723 | com_return_stmt(c, n) |
| 1724 | struct compiling *c; |
| 1725 | node *n; |
| 1726 | { |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1727 | REQ(n, return_stmt); /* 'return' [testlist] */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1728 | if (!c->c_infunction) { |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1729 | com_error(c, SyntaxError, "'return' outside function"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1730 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1731 | if (NCH(n) < 2) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1732 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 1733 | else |
| 1734 | com_node(c, CHILD(n, 1)); |
| 1735 | com_addbyte(c, RETURN_VALUE); |
| 1736 | } |
| 1737 | |
| 1738 | static void |
| 1739 | com_raise_stmt(c, n) |
| 1740 | struct compiling *c; |
| 1741 | node *n; |
| 1742 | { |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1743 | REQ(n, raise_stmt); /* 'raise' test [',' test [',' test]] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1744 | com_node(c, CHILD(n, 1)); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1745 | if (NCH(n) > 3) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1746 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1747 | if (NCH(n) > 5) |
| 1748 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1749 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1750 | com_addoparg(c, RAISE_VARARGS, NCH(n)/2); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | static void |
| 1754 | com_import_stmt(c, n) |
| 1755 | struct compiling *c; |
| 1756 | node *n; |
| 1757 | { |
| 1758 | int i; |
| 1759 | REQ(n, import_stmt); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1760 | /* 'import' dotted_name (',' dotted_name)* | |
| 1761 | 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1762 | if (STR(CHILD(n, 0))[0] == 'f') { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1763 | /* 'from' dotted_name 'import' ... */ |
| 1764 | REQ(CHILD(n, 1), dotted_name); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1765 | com_addopname(c, IMPORT_NAME, CHILD(n, 1)); |
| 1766 | for (i = 3; i < NCH(n); i += 2) |
| 1767 | com_addopname(c, IMPORT_FROM, CHILD(n, i)); |
| 1768 | com_addbyte(c, POP_TOP); |
| 1769 | } |
| 1770 | else { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1771 | /* 'import' ... */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1772 | for (i = 1; i < NCH(n); i += 2) { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1773 | REQ(CHILD(n, i), dotted_name); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1774 | com_addopname(c, IMPORT_NAME, CHILD(n, i)); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1775 | com_addopname(c, STORE_NAME, CHILD(CHILD(n, i), 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1776 | } |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | static void |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1781 | com_global_stmt(c, n) |
| 1782 | struct compiling *c; |
| 1783 | node *n; |
| 1784 | { |
| 1785 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1786 | REQ(n, global_stmt); |
| 1787 | /* 'global' NAME (',' NAME)* */ |
| 1788 | for (i = 1; i < NCH(n); i += 2) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1789 | char *s = STR(CHILD(n, i)); |
Guido van Rossum | fe2236f | 1996-08-24 07:29:04 +0000 | [diff] [blame] | 1790 | #ifdef PRIVATE_NAME_MANGLING |
| 1791 | char buffer[256]; |
| 1792 | if (s != NULL && s[0] == '_' && s[1] == '_' && |
| 1793 | c->c_private != NULL && |
| 1794 | com_mangle(c, s, buffer, (int)sizeof(buffer))) |
| 1795 | s = buffer; |
| 1796 | #endif |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1797 | if (dictlookup(c->c_locals, s) != NULL) { |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1798 | com_error(c, SyntaxError, "name is local and global"); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1799 | } |
| 1800 | else if (dictinsert(c->c_globals, s, None) != 0) |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1801 | c->c_errors++; |
| 1802 | } |
| 1803 | } |
| 1804 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1805 | static int |
| 1806 | com_newlocal_o(c, nameval) |
| 1807 | struct compiling *c; |
| 1808 | object *nameval; |
| 1809 | { |
| 1810 | int i; |
| 1811 | object *ival; |
| 1812 | if (getlistsize(c->c_varnames) != c->c_nlocals) { |
| 1813 | /* This is usually caused by an error on a previous call */ |
| 1814 | if (c->c_errors == 0) { |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1815 | com_error(c, SystemError, "mixed up var name/index"); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1816 | } |
| 1817 | return 0; |
| 1818 | } |
| 1819 | ival = newintobject(i = c->c_nlocals++); |
| 1820 | if (ival == NULL) |
| 1821 | c->c_errors++; |
| 1822 | else if (mappinginsert(c->c_locals, nameval, ival) != 0) |
| 1823 | c->c_errors++; |
| 1824 | else if (addlistitem(c->c_varnames, nameval) != 0) |
| 1825 | c->c_errors++; |
| 1826 | XDECREF(ival); |
| 1827 | return i; |
| 1828 | } |
| 1829 | |
| 1830 | static int |
| 1831 | com_addlocal_o(c, nameval) |
| 1832 | struct compiling *c; |
| 1833 | object *nameval; |
| 1834 | { |
| 1835 | object *ival = mappinglookup(c->c_locals, nameval); |
| 1836 | if (ival != NULL) |
| 1837 | return getintvalue(ival); |
| 1838 | return com_newlocal_o(c, nameval); |
| 1839 | } |
| 1840 | |
| 1841 | static int |
| 1842 | com_newlocal(c, name) |
| 1843 | struct compiling *c; |
| 1844 | char *name; |
| 1845 | { |
| 1846 | object *nameval = newstringobject(name); |
| 1847 | int i; |
| 1848 | if (nameval == NULL) { |
| 1849 | c->c_errors++; |
| 1850 | return 0; |
| 1851 | } |
| 1852 | i = com_newlocal_o(c, nameval); |
| 1853 | DECREF(nameval); |
| 1854 | return i; |
| 1855 | } |
| 1856 | |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1857 | #define strequ(a, b) (strcmp((a), (b)) == 0) |
| 1858 | |
Guido van Rossum | 0dfcf75 | 1996-08-12 22:00:53 +0000 | [diff] [blame] | 1859 | #ifdef SUPPORT_OBSOLETE_ACCESS |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1860 | static void |
| 1861 | com_access_stmt(c, n) |
| 1862 | struct compiling *c; |
| 1863 | node *n; |
| 1864 | { |
| 1865 | int i, j, k, mode, imode; |
| 1866 | object *vmode; |
| 1867 | REQ(n, access_stmt); |
| 1868 | /* 'access' NAME (',' NAME)* ':' accesstype (',' accesstype)* |
| 1869 | accesstype: NAME+ */ |
| 1870 | |
| 1871 | /* Find where the colon is */ |
| 1872 | i = 1; |
| 1873 | while (TYPE(CHILD(n,i-1)) != COLON) |
| 1874 | i += 1; |
| 1875 | |
| 1876 | /* Calculate the mode mask */ |
| 1877 | mode = 0; |
| 1878 | for (j = i; j < NCH(n); j += 2) { |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 1879 | int r = 0, w = 0, p = 0; |
| 1880 | for (k = 0; k < NCH(CHILD(n,j)); k++) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1881 | if (strequ(STR(CHILD(CHILD(n,j),k)), "public")) |
| 1882 | p = 0; |
| 1883 | else if (strequ(STR(CHILD(CHILD(n,j),k)), "protected")) |
| 1884 | p = 1; |
| 1885 | else if (strequ(STR(CHILD(CHILD(n,j),k)), "private")) |
| 1886 | p = 2; |
| 1887 | else if (strequ(STR(CHILD(CHILD(n,j),k)), "read")) |
| 1888 | r = 1; |
| 1889 | else if (strequ(STR(CHILD(CHILD(n,j),k)), "write")) |
| 1890 | w = 1; |
| 1891 | else /* XXX should make this an exception */ |
| 1892 | fprintf(stderr, "bad access type %s\n", |
| 1893 | STR(CHILD(CHILD(n,j),k))); |
| 1894 | } |
| 1895 | if (r == 0 && w == 0) |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 1896 | r = w = 1; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1897 | if (p == 0) { |
| 1898 | if (r == 1) mode |= AC_R_PUBLIC; |
| 1899 | if (w == 1) mode |= AC_W_PUBLIC; |
| 1900 | } else if (p == 1) { |
| 1901 | if (r == 1) mode |= AC_R_PROTECTED; |
| 1902 | if (w == 1) mode |= AC_W_PROTECTED; |
| 1903 | } else { |
| 1904 | if (r == 1) mode |= AC_R_PRIVATE; |
| 1905 | if (w == 1) mode |= AC_W_PRIVATE; |
| 1906 | } |
| 1907 | } |
| 1908 | vmode = newintobject((long)mode); |
| 1909 | imode = com_addconst(c, vmode); |
| 1910 | XDECREF(vmode); |
| 1911 | for (i = 1; TYPE(CHILD(n,i-1)) != COLON; i+=2) { |
| 1912 | com_addoparg(c, LOAD_CONST, imode); |
| 1913 | com_addopname(c, ACCESS_MODE, CHILD(n, i)); |
| 1914 | } |
| 1915 | } |
Guido van Rossum | 0dfcf75 | 1996-08-12 22:00:53 +0000 | [diff] [blame] | 1916 | #endif |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1917 | |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1918 | static void |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1919 | com_exec_stmt(c, n) |
| 1920 | struct compiling *c; |
| 1921 | node *n; |
| 1922 | { |
| 1923 | REQ(n, exec_stmt); |
| 1924 | /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */ |
| 1925 | com_node(c, CHILD(n, 1)); |
| 1926 | if (NCH(n) >= 4) |
| 1927 | com_node(c, CHILD(n, 3)); |
| 1928 | else |
| 1929 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 1930 | if (NCH(n) >= 6) |
| 1931 | com_node(c, CHILD(n, 5)); |
| 1932 | else |
| 1933 | com_addbyte(c, DUP_TOP); |
| 1934 | com_addbyte(c, EXEC_STMT); |
| 1935 | } |
| 1936 | |
| 1937 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1938 | com_if_stmt(c, n) |
| 1939 | struct compiling *c; |
| 1940 | node *n; |
| 1941 | { |
| 1942 | int i; |
| 1943 | int anchor = 0; |
| 1944 | REQ(n, if_stmt); |
| 1945 | /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */ |
| 1946 | for (i = 0; i+3 < NCH(n); i+=4) { |
| 1947 | int a = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1948 | node *ch = CHILD(n, i+1); |
| 1949 | if (i > 0) |
| 1950 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1951 | com_node(c, CHILD(n, i+1)); |
| 1952 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 1953 | com_addbyte(c, POP_TOP); |
| 1954 | com_node(c, CHILD(n, i+3)); |
| 1955 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 1956 | com_backpatch(c, a); |
| 1957 | com_addbyte(c, POP_TOP); |
| 1958 | } |
| 1959 | if (i+2 < NCH(n)) |
| 1960 | com_node(c, CHILD(n, i+2)); |
| 1961 | com_backpatch(c, anchor); |
| 1962 | } |
| 1963 | |
| 1964 | static void |
| 1965 | com_while_stmt(c, n) |
| 1966 | struct compiling *c; |
| 1967 | node *n; |
| 1968 | { |
| 1969 | int break_anchor = 0; |
| 1970 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1971 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1972 | REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */ |
| 1973 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1974 | block_push(c, SETUP_LOOP); |
| 1975 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1976 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1977 | com_node(c, CHILD(n, 1)); |
| 1978 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 1979 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1980 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1981 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1982 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1983 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 1984 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1985 | com_backpatch(c, anchor); |
| 1986 | com_addbyte(c, POP_TOP); |
| 1987 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1988 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1989 | if (NCH(n) > 4) |
| 1990 | com_node(c, CHILD(n, 6)); |
| 1991 | com_backpatch(c, break_anchor); |
| 1992 | } |
| 1993 | |
| 1994 | static void |
| 1995 | com_for_stmt(c, n) |
| 1996 | struct compiling *c; |
| 1997 | node *n; |
| 1998 | { |
| 1999 | object *v; |
| 2000 | int break_anchor = 0; |
| 2001 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2002 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2003 | REQ(n, for_stmt); |
| 2004 | /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */ |
| 2005 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2006 | block_push(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2007 | com_node(c, CHILD(n, 3)); |
| 2008 | v = newintobject(0L); |
| 2009 | if (v == NULL) |
| 2010 | c->c_errors++; |
| 2011 | com_addoparg(c, LOAD_CONST, com_addconst(c, v)); |
| 2012 | XDECREF(v); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2013 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2014 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2015 | com_addfwref(c, FOR_LOOP, &anchor); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2016 | com_assign(c, CHILD(n, 1), OP_ASSIGN); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2017 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2018 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2019 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2020 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 2021 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2022 | com_backpatch(c, anchor); |
| 2023 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2024 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2025 | if (NCH(n) > 8) |
| 2026 | com_node(c, CHILD(n, 8)); |
| 2027 | com_backpatch(c, break_anchor); |
| 2028 | } |
| 2029 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2030 | /* Code generated for "try: S finally: Sf" is as follows: |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2031 | |
| 2032 | SETUP_FINALLY L |
| 2033 | <code for S> |
| 2034 | POP_BLOCK |
| 2035 | LOAD_CONST <nil> |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2036 | L: <code for Sf> |
| 2037 | END_FINALLY |
| 2038 | |
| 2039 | The special instructions use the block stack. Each block |
| 2040 | stack entry contains the instruction that created it (here |
| 2041 | SETUP_FINALLY), the level of the value stack at the time the |
| 2042 | block stack entry was created, and a label (here L). |
| 2043 | |
| 2044 | SETUP_FINALLY: |
| 2045 | Pushes the current value stack level and the label |
| 2046 | onto the block stack. |
| 2047 | POP_BLOCK: |
| 2048 | Pops en entry from the block stack, and pops the value |
| 2049 | stack until its level is the same as indicated on the |
| 2050 | block stack. (The label is ignored.) |
| 2051 | END_FINALLY: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2052 | Pops a variable number of entries from the *value* stack |
| 2053 | and re-raises the exception they specify. The number of |
| 2054 | entries popped depends on the (pseudo) exception type. |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2055 | |
| 2056 | The block stack is unwound when an exception is raised: |
| 2057 | when a SETUP_FINALLY entry is found, the exception is pushed |
| 2058 | onto the value stack (and the exception condition is cleared), |
| 2059 | and the interpreter jumps to the label gotten from the block |
| 2060 | stack. |
| 2061 | |
| 2062 | Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...": |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2063 | (The contents of the value stack is shown in [], with the top |
| 2064 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 2065 | associated value, and 'exc' the exception.) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2066 | |
| 2067 | Value stack Label Instruction Argument |
| 2068 | [] SETUP_EXCEPT L1 |
| 2069 | [] <code for S> |
| 2070 | [] POP_BLOCK |
| 2071 | [] JUMP_FORWARD L0 |
| 2072 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2073 | [tb, val, exc] L1: DUP ) |
| 2074 | [tb, val, exc, exc] <evaluate E1> ) |
| 2075 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 2076 | [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 ) |
| 2077 | [tb, val, exc, 1] POP ) |
| 2078 | [tb, val, exc] POP |
| 2079 | [tb, val] <assign to V1> (or POP if no V1) |
| 2080 | [tb] POP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2081 | [] <code for S1> |
| 2082 | JUMP_FORWARD L0 |
| 2083 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2084 | [tb, val, exc, 0] L2: POP |
| 2085 | [tb, val, exc] DUP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2086 | .............................etc....................... |
| 2087 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2088 | [tb, val, exc, 0] Ln+1: POP |
| 2089 | [tb, val, exc] END_FINALLY # re-raise exception |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2090 | |
| 2091 | [] L0: <next statement> |
| 2092 | |
| 2093 | Of course, parts are not generated if Vi or Ei is not present. |
| 2094 | */ |
| 2095 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2096 | static void |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2097 | com_try_except(c, n) |
| 2098 | struct compiling *c; |
| 2099 | node *n; |
| 2100 | { |
| 2101 | int except_anchor = 0; |
| 2102 | int end_anchor = 0; |
| 2103 | int else_anchor = 0; |
| 2104 | int i; |
| 2105 | node *ch; |
| 2106 | |
| 2107 | com_addfwref(c, SETUP_EXCEPT, &except_anchor); |
| 2108 | block_push(c, SETUP_EXCEPT); |
| 2109 | com_node(c, CHILD(n, 2)); |
| 2110 | com_addbyte(c, POP_BLOCK); |
| 2111 | block_pop(c, SETUP_EXCEPT); |
| 2112 | com_addfwref(c, JUMP_FORWARD, &else_anchor); |
| 2113 | com_backpatch(c, except_anchor); |
| 2114 | for (i = 3; |
| 2115 | i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause; |
| 2116 | i += 3) { |
| 2117 | /* except_clause: 'except' [expr [',' expr]] */ |
| 2118 | if (except_anchor == 0) { |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2119 | com_error(c, SyntaxError, |
| 2120 | "default 'except:' must be last"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2121 | break; |
| 2122 | } |
| 2123 | except_anchor = 0; |
| 2124 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 2125 | if (NCH(ch) > 1) { |
| 2126 | com_addbyte(c, DUP_TOP); |
| 2127 | com_node(c, CHILD(ch, 1)); |
| 2128 | com_addoparg(c, COMPARE_OP, EXC_MATCH); |
| 2129 | com_addfwref(c, JUMP_IF_FALSE, &except_anchor); |
| 2130 | com_addbyte(c, POP_TOP); |
| 2131 | } |
| 2132 | com_addbyte(c, POP_TOP); |
| 2133 | if (NCH(ch) > 3) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2134 | com_assign(c, CHILD(ch, 3), OP_ASSIGN); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2135 | else |
| 2136 | com_addbyte(c, POP_TOP); |
| 2137 | com_addbyte(c, POP_TOP); |
| 2138 | com_node(c, CHILD(n, i+2)); |
| 2139 | com_addfwref(c, JUMP_FORWARD, &end_anchor); |
| 2140 | if (except_anchor) { |
| 2141 | com_backpatch(c, except_anchor); |
| 2142 | com_addbyte(c, POP_TOP); |
| 2143 | } |
| 2144 | } |
| 2145 | com_addbyte(c, END_FINALLY); |
| 2146 | com_backpatch(c, else_anchor); |
| 2147 | if (i < NCH(n)) |
| 2148 | com_node(c, CHILD(n, i+2)); |
| 2149 | com_backpatch(c, end_anchor); |
| 2150 | } |
| 2151 | |
| 2152 | static void |
| 2153 | com_try_finally(c, n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2154 | struct compiling *c; |
| 2155 | node *n; |
| 2156 | { |
| 2157 | int finally_anchor = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2158 | node *ch; |
Guido van Rossum | 94fb82e | 1992-04-05 14:24:50 +0000 | [diff] [blame] | 2159 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2160 | com_addfwref(c, SETUP_FINALLY, &finally_anchor); |
| 2161 | block_push(c, SETUP_FINALLY); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2162 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2163 | com_addbyte(c, POP_BLOCK); |
| 2164 | block_pop(c, SETUP_FINALLY); |
| 2165 | block_push(c, END_FINALLY); |
| 2166 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 2167 | com_backpatch(c, finally_anchor); |
| 2168 | ch = CHILD(n, NCH(n)-1); |
| 2169 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 2170 | com_node(c, ch); |
| 2171 | com_addbyte(c, END_FINALLY); |
| 2172 | block_pop(c, END_FINALLY); |
| 2173 | } |
| 2174 | |
| 2175 | static void |
| 2176 | com_try_stmt(c, n) |
| 2177 | struct compiling *c; |
| 2178 | node *n; |
| 2179 | { |
| 2180 | REQ(n, try_stmt); |
| 2181 | /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
| 2182 | | 'try' ':' suite 'finally' ':' suite */ |
| 2183 | if (TYPE(CHILD(n, 3)) != except_clause) |
| 2184 | com_try_finally(c, n); |
| 2185 | else |
| 2186 | com_try_except(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2189 | static object * |
| 2190 | get_docstring(n) |
| 2191 | node *n; |
| 2192 | { |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 2193 | int i; |
| 2194 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2195 | switch (TYPE(n)) { |
| 2196 | |
| 2197 | case suite: |
| 2198 | if (NCH(n) == 1) |
| 2199 | return get_docstring(CHILD(n, 0)); |
| 2200 | else { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2201 | for (i = 0; i < NCH(n); i++) { |
| 2202 | node *ch = CHILD(n, i); |
| 2203 | if (TYPE(ch) == stmt) |
| 2204 | return get_docstring(ch); |
| 2205 | } |
| 2206 | } |
| 2207 | break; |
| 2208 | |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 2209 | case file_input: |
| 2210 | for (i = 0; i < NCH(n); i++) { |
| 2211 | node *ch = CHILD(n, i); |
| 2212 | if (TYPE(ch) == stmt) |
| 2213 | return get_docstring(ch); |
| 2214 | } |
| 2215 | break; |
| 2216 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2217 | case stmt: |
| 2218 | case simple_stmt: |
| 2219 | case small_stmt: |
| 2220 | return get_docstring(CHILD(n, 0)); |
| 2221 | |
| 2222 | case expr_stmt: |
| 2223 | case testlist: |
| 2224 | case test: |
| 2225 | case and_test: |
| 2226 | case not_test: |
| 2227 | case comparison: |
| 2228 | case expr: |
| 2229 | case xor_expr: |
| 2230 | case and_expr: |
| 2231 | case shift_expr: |
| 2232 | case arith_expr: |
| 2233 | case term: |
| 2234 | case factor: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2235 | case power: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2236 | if (NCH(n) == 1) |
| 2237 | return get_docstring(CHILD(n, 0)); |
| 2238 | break; |
| 2239 | |
| 2240 | case atom: |
| 2241 | if (TYPE(CHILD(n, 0)) == STRING) |
| 2242 | return parsestrplus(n); |
| 2243 | break; |
| 2244 | |
| 2245 | } |
| 2246 | return NULL; |
| 2247 | } |
| 2248 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2249 | static void |
| 2250 | com_suite(c, n) |
| 2251 | struct compiling *c; |
| 2252 | node *n; |
| 2253 | { |
| 2254 | REQ(n, suite); |
| 2255 | /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */ |
| 2256 | if (NCH(n) == 1) { |
| 2257 | com_node(c, CHILD(n, 0)); |
| 2258 | } |
| 2259 | else { |
| 2260 | int i; |
| 2261 | for (i = 0; i < NCH(n); i++) { |
| 2262 | node *ch = CHILD(n, i); |
| 2263 | if (TYPE(ch) == stmt) |
| 2264 | com_node(c, ch); |
| 2265 | } |
| 2266 | } |
| 2267 | } |
| 2268 | |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 2269 | /* ARGSUSED */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2270 | static void |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2271 | com_continue_stmt(c, n) |
| 2272 | struct compiling *c; |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 2273 | node *n; /* Not used, but passed for consistency */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2274 | { |
| 2275 | int i = c->c_nblocks; |
| 2276 | if (i-- > 0 && c->c_block[i] == SETUP_LOOP) { |
| 2277 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 2278 | } |
| 2279 | else { |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2280 | com_error(c, SyntaxError, "'continue' not properly in loop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2281 | } |
| 2282 | /* XXX Could allow it inside a 'finally' clause |
| 2283 | XXX if we could pop the exception still on the stack */ |
| 2284 | } |
| 2285 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2286 | static int |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2287 | com_argdefs(c, n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2288 | struct compiling *c; |
| 2289 | node *n; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2290 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2291 | int i, nch, nargs, ndefs; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2292 | if (TYPE(n) == lambdef) { |
| 2293 | /* lambdef: 'lambda' [varargslist] ':' test */ |
| 2294 | n = CHILD(n, 1); |
| 2295 | } |
| 2296 | else { |
| 2297 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */ |
| 2298 | n = CHILD(n, 2); |
| 2299 | REQ(n, parameters); /* parameters: '(' [varargslist] ')' */ |
| 2300 | n = CHILD(n, 1); |
| 2301 | } |
| 2302 | if (TYPE(n) != varargslist) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2303 | return 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2304 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2305 | (fpdef ['=' test] ',')* '*' ....... | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2306 | fpdef ['=' test] (',' fpdef ['=' test])* [','] */ |
| 2307 | nch = NCH(n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2308 | nargs = 0; |
| 2309 | ndefs = 0; |
| 2310 | for (i = 0; i < nch; i++) { |
| 2311 | int t; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2312 | if (TYPE(CHILD(n, i)) == STAR || TYPE(CHILD(n, i)) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2313 | break; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2314 | nargs++; |
| 2315 | i++; |
| 2316 | if (i >= nch) |
Guido van Rossum | a1e7e62 | 1995-09-18 21:44:04 +0000 | [diff] [blame] | 2317 | t = RPAR; /* Anything except EQUAL or COMMA */ |
| 2318 | else |
| 2319 | t = TYPE(CHILD(n, i)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2320 | if (t == EQUAL) { |
| 2321 | i++; |
| 2322 | ndefs++; |
| 2323 | com_node(c, CHILD(n, i)); |
| 2324 | i++; |
| 2325 | if (i >= nch) |
| 2326 | break; |
| 2327 | t = TYPE(CHILD(n, i)); |
| 2328 | } |
| 2329 | else { |
| 2330 | /* Treat "(a=1, b)" as "(a=1, b=None)" */ |
| 2331 | if (ndefs) { |
| 2332 | com_addoparg(c, LOAD_CONST, |
| 2333 | com_addconst(c, None)); |
| 2334 | ndefs++; |
| 2335 | } |
| 2336 | } |
| 2337 | if (t != COMMA) |
| 2338 | break; |
| 2339 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2340 | return ndefs; |
| 2341 | } |
| 2342 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2343 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2344 | com_funcdef(c, n) |
| 2345 | struct compiling *c; |
| 2346 | node *n; |
| 2347 | { |
| 2348 | object *v; |
| 2349 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 2350 | v = (object *)icompile(n, c); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2351 | if (v == NULL) |
| 2352 | c->c_errors++; |
| 2353 | else { |
| 2354 | int i = com_addconst(c, v); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2355 | int ndefs = com_argdefs(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2356 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2357 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2358 | com_addopname(c, STORE_NAME, CHILD(n, 1)); |
| 2359 | DECREF(v); |
| 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | static void |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2364 | com_bases(c, n) |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 2365 | struct compiling *c; |
| 2366 | node *n; |
| 2367 | { |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 2368 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 2369 | REQ(n, testlist); |
| 2370 | /* testlist: test (',' test)* [','] */ |
| 2371 | for (i = 0; i < NCH(n); i += 2) |
| 2372 | com_node(c, CHILD(n, i)); |
| 2373 | com_addoparg(c, BUILD_TUPLE, (NCH(n)+1) / 2); |
| 2374 | } |
| 2375 | |
| 2376 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2377 | com_classdef(c, n) |
| 2378 | struct compiling *c; |
| 2379 | node *n; |
| 2380 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2381 | int i; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2382 | object *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2383 | REQ(n, classdef); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2384 | /* classdef: class NAME ['(' testlist ')'] ':' suite */ |
| 2385 | if ((v = newstringobject(STR(CHILD(n, 1)))) == NULL) { |
| 2386 | c->c_errors++; |
| 2387 | return; |
| 2388 | } |
| 2389 | /* Push the class name on the stack */ |
| 2390 | i = com_addconst(c, v); |
| 2391 | com_addoparg(c, LOAD_CONST, i); |
| 2392 | DECREF(v); |
| 2393 | /* Push the tuple of base classes on the stack */ |
| 2394 | if (TYPE(CHILD(n, 2)) != LPAR) |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 2395 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2396 | else |
| 2397 | com_bases(c, CHILD(n, 3)); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 2398 | v = (object *)icompile(n, c); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2399 | if (v == NULL) |
| 2400 | c->c_errors++; |
| 2401 | else { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2402 | i = com_addconst(c, v); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2403 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2404 | com_addoparg(c, MAKE_FUNCTION, 0); |
| 2405 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2406 | com_addbyte(c, BUILD_CLASS); |
| 2407 | com_addopname(c, STORE_NAME, CHILD(n, 1)); |
| 2408 | DECREF(v); |
| 2409 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
| 2412 | static void |
| 2413 | com_node(c, n) |
| 2414 | struct compiling *c; |
| 2415 | node *n; |
| 2416 | { |
| 2417 | switch (TYPE(n)) { |
| 2418 | |
| 2419 | /* Definition nodes */ |
| 2420 | |
| 2421 | case funcdef: |
| 2422 | com_funcdef(c, n); |
| 2423 | break; |
| 2424 | case classdef: |
| 2425 | com_classdef(c, n); |
| 2426 | break; |
| 2427 | |
| 2428 | /* Trivial parse tree nodes */ |
| 2429 | |
| 2430 | case stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2431 | case small_stmt: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2432 | case flow_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2433 | com_node(c, CHILD(n, 0)); |
| 2434 | break; |
| 2435 | |
| 2436 | case simple_stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2437 | /* small_stmt (';' small_stmt)* [';'] NEWLINE */ |
| 2438 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 2439 | { |
| 2440 | int i; |
| 2441 | for (i = 0; i < NCH(n)-1; i += 2) |
| 2442 | com_node(c, CHILD(n, i)); |
| 2443 | } |
| 2444 | break; |
| 2445 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2446 | case compound_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2447 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2448 | com_node(c, CHILD(n, 0)); |
| 2449 | break; |
| 2450 | |
| 2451 | /* Statement nodes */ |
| 2452 | |
| 2453 | case expr_stmt: |
| 2454 | com_expr_stmt(c, n); |
| 2455 | break; |
| 2456 | case print_stmt: |
| 2457 | com_print_stmt(c, n); |
| 2458 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2459 | case del_stmt: /* 'del' exprlist */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2460 | com_assign(c, CHILD(n, 1), OP_DELETE); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2461 | break; |
| 2462 | case pass_stmt: |
| 2463 | break; |
| 2464 | case break_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2465 | if (c->c_loops == 0) { |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2466 | com_error(c, SyntaxError, "'break' outside loop"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2467 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2468 | com_addbyte(c, BREAK_LOOP); |
| 2469 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2470 | case continue_stmt: |
| 2471 | com_continue_stmt(c, n); |
| 2472 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2473 | case return_stmt: |
| 2474 | com_return_stmt(c, n); |
| 2475 | break; |
| 2476 | case raise_stmt: |
| 2477 | com_raise_stmt(c, n); |
| 2478 | break; |
| 2479 | case import_stmt: |
| 2480 | com_import_stmt(c, n); |
| 2481 | break; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 2482 | case global_stmt: |
| 2483 | com_global_stmt(c, n); |
| 2484 | break; |
Guido van Rossum | 0dfcf75 | 1996-08-12 22:00:53 +0000 | [diff] [blame] | 2485 | #ifdef SUPPORT_OBSOLETE_ACCESS |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2486 | case access_stmt: |
| 2487 | com_access_stmt(c, n); |
| 2488 | break; |
Guido van Rossum | 0dfcf75 | 1996-08-12 22:00:53 +0000 | [diff] [blame] | 2489 | #endif |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2490 | case exec_stmt: |
| 2491 | com_exec_stmt(c, n); |
| 2492 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2493 | case if_stmt: |
| 2494 | com_if_stmt(c, n); |
| 2495 | break; |
| 2496 | case while_stmt: |
| 2497 | com_while_stmt(c, n); |
| 2498 | break; |
| 2499 | case for_stmt: |
| 2500 | com_for_stmt(c, n); |
| 2501 | break; |
| 2502 | case try_stmt: |
| 2503 | com_try_stmt(c, n); |
| 2504 | break; |
| 2505 | case suite: |
| 2506 | com_suite(c, n); |
| 2507 | break; |
| 2508 | |
| 2509 | /* Expression nodes */ |
| 2510 | |
| 2511 | case testlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 2512 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2513 | break; |
| 2514 | case test: |
| 2515 | com_test(c, n); |
| 2516 | break; |
| 2517 | case and_test: |
| 2518 | com_and_test(c, n); |
| 2519 | break; |
| 2520 | case not_test: |
| 2521 | com_not_test(c, n); |
| 2522 | break; |
| 2523 | case comparison: |
| 2524 | com_comparison(c, n); |
| 2525 | break; |
| 2526 | case exprlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 2527 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2528 | break; |
| 2529 | case expr: |
| 2530 | com_expr(c, n); |
| 2531 | break; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2532 | case xor_expr: |
| 2533 | com_xor_expr(c, n); |
| 2534 | break; |
| 2535 | case and_expr: |
| 2536 | com_and_expr(c, n); |
| 2537 | break; |
| 2538 | case shift_expr: |
| 2539 | com_shift_expr(c, n); |
| 2540 | break; |
| 2541 | case arith_expr: |
| 2542 | com_arith_expr(c, n); |
| 2543 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2544 | case term: |
| 2545 | com_term(c, n); |
| 2546 | break; |
| 2547 | case factor: |
| 2548 | com_factor(c, n); |
| 2549 | break; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2550 | case power: |
| 2551 | com_power(c, n); |
| 2552 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2553 | case atom: |
| 2554 | com_atom(c, n); |
| 2555 | break; |
| 2556 | |
| 2557 | default: |
| 2558 | fprintf(stderr, "node type %d\n", TYPE(n)); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2559 | com_error(c, SystemError, "com_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2560 | } |
| 2561 | } |
| 2562 | |
| 2563 | static void com_fplist PROTO((struct compiling *, node *)); |
| 2564 | |
| 2565 | static void |
| 2566 | com_fpdef(c, n) |
| 2567 | struct compiling *c; |
| 2568 | node *n; |
| 2569 | { |
| 2570 | REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 2571 | if (TYPE(CHILD(n, 0)) == LPAR) |
| 2572 | com_fplist(c, CHILD(n, 1)); |
| 2573 | else |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2574 | com_addoparg(c, STORE_FAST, com_newlocal(c, STR(CHILD(n, 0)))); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
| 2577 | static void |
| 2578 | com_fplist(c, n) |
| 2579 | struct compiling *c; |
| 2580 | node *n; |
| 2581 | { |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 2582 | REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2583 | if (NCH(n) == 1) { |
| 2584 | com_fpdef(c, CHILD(n, 0)); |
| 2585 | } |
| 2586 | else { |
| 2587 | int i; |
| 2588 | com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2); |
| 2589 | for (i = 0; i < NCH(n); i += 2) |
| 2590 | com_fpdef(c, CHILD(n, i)); |
| 2591 | } |
| 2592 | } |
| 2593 | |
| 2594 | static void |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2595 | com_arglist(c, n) |
| 2596 | struct compiling *c; |
| 2597 | node *n; |
| 2598 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2599 | int nch, i; |
| 2600 | int complex = 0; |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2601 | REQ(n, varargslist); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 2602 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2603 | (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2604 | nch = NCH(n); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2605 | /* Enter all arguments in table of locals */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2606 | for (i = 0; i < nch; i++) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2607 | node *ch = CHILD(n, i); |
| 2608 | node *fp; |
| 2609 | char *name; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2610 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2611 | break; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2612 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 2613 | fp = CHILD(ch, 0); |
| 2614 | if (TYPE(fp) == NAME) |
| 2615 | name = STR(fp); |
| 2616 | else { |
| 2617 | name = ""; |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 2618 | complex = 1; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2619 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2620 | com_newlocal(c, name); |
| 2621 | c->c_argcount++; |
| 2622 | if (++i >= nch) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2623 | break; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2624 | ch = CHILD(n, i); |
| 2625 | if (TYPE(ch) == EQUAL) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2626 | i += 2; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2627 | else |
| 2628 | REQ(ch, COMMA); |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2629 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2630 | /* Handle *arguments */ |
| 2631 | if (i < nch) { |
| 2632 | node *ch; |
| 2633 | ch = CHILD(n, i); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2634 | if (TYPE(ch) != DOUBLESTAR) { |
| 2635 | REQ(ch, STAR); |
| 2636 | ch = CHILD(n, i+1); |
| 2637 | if (TYPE(ch) == NAME) { |
| 2638 | c->c_flags |= CO_VARARGS; |
| 2639 | i += 3; |
| 2640 | com_newlocal(c, STR(ch)); |
| 2641 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2642 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2643 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2644 | /* Handle **keywords */ |
| 2645 | if (i < nch) { |
| 2646 | node *ch; |
| 2647 | ch = CHILD(n, i); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2648 | if (TYPE(ch) != DOUBLESTAR) { |
| 2649 | REQ(ch, STAR); |
| 2650 | ch = CHILD(n, i+1); |
| 2651 | REQ(ch, STAR); |
| 2652 | ch = CHILD(n, i+2); |
| 2653 | } |
| 2654 | else |
| 2655 | ch = CHILD(n, i+1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2656 | REQ(ch, NAME); |
| 2657 | c->c_flags |= CO_VARKEYWORDS; |
| 2658 | com_newlocal(c, STR(ch)); |
| 2659 | } |
| 2660 | if (complex) { |
| 2661 | /* Generate code for complex arguments only after |
| 2662 | having counted the simple arguments */ |
| 2663 | int ilocal = 0; |
| 2664 | for (i = 0; i < nch; i++) { |
| 2665 | node *ch = CHILD(n, i); |
| 2666 | node *fp; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2667 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2668 | break; |
| 2669 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 2670 | fp = CHILD(ch, 0); |
| 2671 | if (TYPE(fp) != NAME) { |
| 2672 | com_addoparg(c, LOAD_FAST, ilocal); |
| 2673 | com_fpdef(c, ch); |
| 2674 | } |
| 2675 | ilocal++; |
| 2676 | if (++i >= nch) |
| 2677 | break; |
| 2678 | ch = CHILD(n, i); |
| 2679 | if (TYPE(ch) == EQUAL) |
| 2680 | i += 2; |
| 2681 | else |
| 2682 | REQ(ch, COMMA); |
| 2683 | } |
| 2684 | } |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
| 2687 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2688 | com_file_input(c, n) |
| 2689 | struct compiling *c; |
| 2690 | node *n; |
| 2691 | { |
| 2692 | int i; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2693 | object *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2694 | REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */ |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2695 | doc = get_docstring(n); |
| 2696 | if (doc != NULL) { |
| 2697 | int i = com_addconst(c, doc); |
| 2698 | DECREF(doc); |
| 2699 | com_addoparg(c, LOAD_CONST, i); |
| 2700 | com_addopnamestr(c, STORE_NAME, "__doc__"); |
| 2701 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2702 | for (i = 0; i < NCH(n); i++) { |
| 2703 | node *ch = CHILD(n, i); |
| 2704 | if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE) |
| 2705 | com_node(c, ch); |
| 2706 | } |
| 2707 | } |
| 2708 | |
| 2709 | /* Top-level compile-node interface */ |
| 2710 | |
| 2711 | static void |
| 2712 | compile_funcdef(c, n) |
| 2713 | struct compiling *c; |
| 2714 | node *n; |
| 2715 | { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2716 | object *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2717 | node *ch; |
| 2718 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2719 | c->c_name = STR(CHILD(n, 1)); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2720 | doc = get_docstring(CHILD(n, 4)); |
| 2721 | if (doc != NULL) { |
| 2722 | (void) com_addconst(c, doc); |
| 2723 | DECREF(doc); |
| 2724 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2725 | else |
| 2726 | (void) com_addconst(c, None); /* No docstring */ |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2727 | ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */ |
| 2728 | ch = CHILD(ch, 1); /* ')' | varargslist */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2729 | if (TYPE(ch) == varargslist) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 2730 | com_arglist(c, ch); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2731 | c->c_infunction = 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2732 | com_node(c, CHILD(n, 4)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2733 | c->c_infunction = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2734 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 2735 | com_addbyte(c, RETURN_VALUE); |
| 2736 | } |
| 2737 | |
| 2738 | static void |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2739 | compile_lambdef(c, n) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2740 | struct compiling *c; |
| 2741 | node *n; |
| 2742 | { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2743 | node *ch; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2744 | REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */ |
Guido van Rossum | 363ac7d | 1994-11-10 22:40:34 +0000 | [diff] [blame] | 2745 | c->c_name = "<lambda>"; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2746 | |
| 2747 | ch = CHILD(n, 1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2748 | (void) com_addconst(c, None); /* No docstring */ |
| 2749 | if (TYPE(ch) == varargslist) { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2750 | com_arglist(c, ch); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2751 | ch = CHILD(n, 3); |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2752 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2753 | else |
| 2754 | ch = CHILD(n, 2); |
| 2755 | com_node(c, ch); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2756 | com_addbyte(c, RETURN_VALUE); |
| 2757 | } |
| 2758 | |
| 2759 | static void |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2760 | compile_classdef(c, n) |
| 2761 | struct compiling *c; |
| 2762 | node *n; |
| 2763 | { |
| 2764 | node *ch; |
| 2765 | object *doc; |
| 2766 | REQ(n, classdef); |
| 2767 | /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ |
| 2768 | c->c_name = STR(CHILD(n, 1)); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 2769 | #ifdef PRIVATE_NAME_MANGLING |
| 2770 | c->c_private = c->c_name; |
| 2771 | #endif |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2772 | ch = CHILD(n, NCH(n)-1); /* The suite */ |
| 2773 | doc = get_docstring(ch); |
| 2774 | if (doc != NULL) { |
| 2775 | int i = com_addconst(c, doc); |
| 2776 | DECREF(doc); |
| 2777 | com_addoparg(c, LOAD_CONST, i); |
| 2778 | com_addopnamestr(c, STORE_NAME, "__doc__"); |
| 2779 | } |
| 2780 | else |
| 2781 | (void) com_addconst(c, None); |
| 2782 | com_node(c, ch); |
| 2783 | com_addbyte(c, LOAD_LOCALS); |
| 2784 | com_addbyte(c, RETURN_VALUE); |
| 2785 | } |
| 2786 | |
| 2787 | static void |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2788 | compile_node(c, n) |
| 2789 | struct compiling *c; |
| 2790 | node *n; |
| 2791 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2792 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 2793 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2794 | switch (TYPE(n)) { |
| 2795 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 2796 | case single_input: /* One interactive command */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2797 | /* NEWLINE | simple_stmt | compound_stmt NEWLINE */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2798 | c->c_interactive++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2799 | n = CHILD(n, 0); |
| 2800 | if (TYPE(n) != NEWLINE) |
| 2801 | com_node(c, n); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2802 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 2803 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2804 | c->c_interactive--; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2805 | break; |
| 2806 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 2807 | case file_input: /* A whole file, or built-in function exec() */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2808 | com_file_input(c, n); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2809 | com_addoparg(c, LOAD_CONST, com_addconst(c, None)); |
| 2810 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2811 | break; |
| 2812 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2813 | case eval_input: /* Built-in function input() */ |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 2814 | com_node(c, CHILD(n, 0)); |
| 2815 | com_addbyte(c, RETURN_VALUE); |
| 2816 | break; |
| 2817 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2818 | case lambdef: /* anonymous function definition */ |
| 2819 | compile_lambdef(c, n); |
| 2820 | break; |
| 2821 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 2822 | case funcdef: /* A function definition */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2823 | compile_funcdef(c, n); |
| 2824 | break; |
| 2825 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 2826 | case classdef: /* A class definition */ |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2827 | compile_classdef(c, n); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2828 | break; |
| 2829 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2830 | default: |
| 2831 | fprintf(stderr, "node type %d\n", TYPE(n)); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2832 | com_error(c, SystemError, |
| 2833 | "compile_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2834 | } |
| 2835 | } |
| 2836 | |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2837 | /* Optimization for local variables in functions (and *only* functions). |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2838 | |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2839 | This replaces all LOAD_NAME, STORE_NAME and DELETE_NAME |
| 2840 | instructions that refer to local variables with LOAD_FAST etc. |
| 2841 | The latter instructions are much faster because they don't need to |
| 2842 | look up the variable name in a dictionary. |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2843 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2844 | To find all local variables, we check all STORE_NAME, IMPORT_FROM |
| 2845 | and DELETE_NAME instructions. This yields all local variables, |
| 2846 | function definitions, class definitions and import statements. |
| 2847 | Argument names have already been entered into the list by the |
| 2848 | special processing for the argument list. |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2849 | |
| 2850 | All remaining LOAD_NAME instructions must refer to non-local (global |
| 2851 | or builtin) variables, so are replaced by LOAD_GLOBAL. |
| 2852 | |
| 2853 | There are two problems: 'from foo import *' and 'exec' may introduce |
| 2854 | local variables that we can't know while compiling. If this is the |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2855 | case, we can still optimize bona fide locals (since those |
| 2856 | statements will be surrounded by fast_2_locals() and |
| 2857 | locals_2_fast()), but we can't change LOAD_NAME to LOAD_GLOBAL. |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2858 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2859 | NB: this modifies the string object c->c_code! */ |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2860 | |
| 2861 | static void |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2862 | optimize(c) |
| 2863 | struct compiling *c; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2864 | { |
Guido van Rossum | 0a697f6 | 1991-04-16 08:39:12 +0000 | [diff] [blame] | 2865 | unsigned char *next_instr, *cur_instr; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2866 | int opcode; |
| 2867 | int oparg; |
| 2868 | object *name; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2869 | object *error_type, *error_value, *error_traceback; |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2870 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2871 | #define NEXTOP() (*next_instr++) |
| 2872 | #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2]) |
| 2873 | #define GETITEM(v, i) (getlistitem((v), (i))) |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2874 | #define GETNAMEOBJ(i) (GETITEM(c->c_names, (i))) |
| 2875 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2876 | err_fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2877 | |
| 2878 | c->c_flags |= CO_OPTIMIZED; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2879 | |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2880 | next_instr = (unsigned char *) getstringvalue(c->c_code); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2881 | for (;;) { |
| 2882 | opcode = NEXTOP(); |
| 2883 | if (opcode == STOP_CODE) |
| 2884 | break; |
| 2885 | if (HAS_ARG(opcode)) |
| 2886 | oparg = NEXTARG(); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2887 | switch (opcode) { |
| 2888 | case STORE_NAME: |
| 2889 | case DELETE_NAME: |
| 2890 | case IMPORT_FROM: |
| 2891 | com_addlocal_o(c, GETNAMEOBJ(oparg)); |
| 2892 | break; |
| 2893 | case EXEC_STMT: |
| 2894 | c->c_flags &= ~CO_OPTIMIZED; |
| 2895 | break; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2896 | } |
| 2897 | } |
| 2898 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2899 | if (dictlookup(c->c_locals, "*") != NULL) |
| 2900 | c->c_flags &= ~CO_OPTIMIZED; |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2901 | |
| 2902 | next_instr = (unsigned char *) getstringvalue(c->c_code); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2903 | for (;;) { |
| 2904 | cur_instr = next_instr; |
| 2905 | opcode = NEXTOP(); |
| 2906 | if (opcode == STOP_CODE) |
| 2907 | break; |
| 2908 | if (HAS_ARG(opcode)) |
| 2909 | oparg = NEXTARG(); |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2910 | if (opcode == LOAD_NAME || |
| 2911 | opcode == STORE_NAME || |
| 2912 | opcode == DELETE_NAME) { |
| 2913 | object *v; |
| 2914 | int i; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2915 | name = GETNAMEOBJ(oparg); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2916 | v = dict2lookup(c->c_locals, name); |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2917 | if (v == NULL) { |
Guido van Rossum | 8316325 | 1991-08-16 08:58:43 +0000 | [diff] [blame] | 2918 | err_clear(); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2919 | if (opcode == LOAD_NAME && |
| 2920 | (c->c_flags&CO_OPTIMIZED)) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2921 | cur_instr[0] = LOAD_GLOBAL; |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2922 | continue; |
Guido van Rossum | 8316325 | 1991-08-16 08:58:43 +0000 | [diff] [blame] | 2923 | } |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2924 | i = getintvalue(v); |
| 2925 | switch (opcode) { |
| 2926 | case LOAD_NAME: cur_instr[0] = LOAD_FAST; break; |
| 2927 | case STORE_NAME: cur_instr[0] = STORE_FAST; break; |
| 2928 | case DELETE_NAME: cur_instr[0] = DELETE_FAST; break; |
| 2929 | } |
| 2930 | cur_instr[1] = i & 0xff; |
| 2931 | cur_instr[2] = (i>>8) & 0xff; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2932 | } |
| 2933 | } |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2934 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2935 | if (c->c_errors == 0) |
| 2936 | err_restore(error_type, error_value, error_traceback); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2939 | codeobject * |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2940 | compile(n, filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2941 | node *n; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2942 | char *filename; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2943 | { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 2944 | return jcompile(n, filename, NULL); |
| 2945 | } |
| 2946 | |
| 2947 | static codeobject * |
| 2948 | icompile(n, base) |
| 2949 | node *n; |
| 2950 | struct compiling *base; |
| 2951 | { |
| 2952 | return jcompile(n, base->c_filename, base); |
| 2953 | } |
| 2954 | |
| 2955 | static codeobject * |
| 2956 | jcompile(n, filename, base) |
| 2957 | node *n; |
| 2958 | char *filename; |
| 2959 | struct compiling *base; |
| 2960 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2961 | struct compiling sc; |
| 2962 | codeobject *co; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 2963 | if (!com_init(&sc, filename)) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2964 | return NULL; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 2965 | #ifdef PRIVATE_NAME_MANGLING |
| 2966 | if (base) |
| 2967 | sc.c_private = base->c_private; |
| 2968 | else |
| 2969 | sc.c_private = NULL; |
| 2970 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2971 | compile_node(&sc, n); |
| 2972 | com_done(&sc); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2973 | if ((TYPE(n) == funcdef || TYPE(n) == lambdef) && sc.c_errors == 0) { |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 2974 | optimize(&sc); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2975 | sc.c_flags |= CO_NEWLOCALS; |
| 2976 | } |
| 2977 | else if (TYPE(n) == classdef) |
| 2978 | sc.c_flags |= CO_NEWLOCALS; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2979 | co = NULL; |
| 2980 | if (sc.c_errors == 0) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2981 | object *consts, *names, *varnames, *filename, *name; |
| 2982 | consts = listtuple(sc.c_consts); |
| 2983 | names = listtuple(sc.c_names); |
| 2984 | varnames = listtuple(sc.c_varnames); |
| 2985 | filename = newstringobject(sc.c_filename); |
| 2986 | name = newstringobject(sc.c_name); |
| 2987 | if (!err_occurred()) |
| 2988 | co = newcodeobject(sc.c_argcount, |
| 2989 | sc.c_nlocals, |
| 2990 | sc.c_flags, |
| 2991 | sc.c_code, |
| 2992 | consts, |
| 2993 | names, |
| 2994 | varnames, |
| 2995 | filename, |
| 2996 | name); |
| 2997 | XDECREF(consts); |
| 2998 | XDECREF(names); |
| 2999 | XDECREF(varnames); |
| 3000 | XDECREF(filename); |
| 3001 | XDECREF(name); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 3002 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3003 | com_free(&sc); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3004 | return co; |
| 3005 | } |