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