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