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