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