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