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