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