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