Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1 | /* Compile an expression node to intermediate code */ |
| 2 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3 | /* XXX TO DO: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4 | XXX add __doc__ attribute == co_doc to code object attributes? |
| 5 | 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] | 6 | XXX Generate simple jump for break/return outside 'try...finally' |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 7 | XXX Allow 'continue' inside finally clause of try-finally |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 8 | 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] | 9 | XXX other JAR tricks? |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 10 | */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 12 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 14 | #include "node.h" |
| 15 | #include "token.h" |
| 16 | #include "graminit.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 17 | #include "compile.h" |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 18 | #include "symtable.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 19 | #include "opcode.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 20 | #include "structmember.h" |
| 21 | |
| 22 | #include <ctype.h> |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 24 | /* Three symbols from graminit.h are also defined in Python.h, with |
| 25 | Py_ prefixes to their names. Python.h can't include graminit.h |
| 26 | (which defines too many confusing symbols), but we can check here |
| 27 | that they haven't changed (which is very unlikely, but possible). */ |
| 28 | #if Py_single_input != single_input |
Guido van Rossum | 2f75b29 | 1997-05-20 22:18:48 +0000 | [diff] [blame] | 29 | #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] | 30 | #endif |
| 31 | #if Py_file_input != file_input |
Guido van Rossum | 2f75b29 | 1997-05-20 22:18:48 +0000 | [diff] [blame] | 32 | #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] | 33 | #endif |
| 34 | #if Py_eval_input != eval_input |
Guido van Rossum | 2f75b29 | 1997-05-20 22:18:48 +0000 | [diff] [blame] | 35 | #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] | 36 | #endif |
| 37 | |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 38 | int Py_OptimizeFlag = 0; |
| 39 | |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 40 | #define OP_DELETE 0 |
| 41 | #define OP_ASSIGN 1 |
| 42 | #define OP_APPLY 2 |
| 43 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 44 | #define VAR_LOAD 0 |
| 45 | #define VAR_STORE 1 |
| 46 | #define VAR_DELETE 2 |
| 47 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 48 | #define DEL_CLOSURE_ERROR \ |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 49 | "can not delete variable '%.400s' referenced in nested scope" |
| 50 | |
| 51 | #define DUPLICATE_ARGUMENT \ |
| 52 | "duplicate argument '%s' in function definition" |
| 53 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 54 | #define ILLEGAL_DYNAMIC_SCOPE \ |
| 55 | "%.100s: exec or 'import *' makes names ambiguous in nested scope" |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 56 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 57 | #define GLOBAL_AFTER_ASSIGN \ |
| 58 | "name '%.400s' is assigned to before global declaration" |
| 59 | |
| 60 | #define GLOBAL_AFTER_USE \ |
| 61 | "name '%.400s' is used prior to global declaration" |
| 62 | |
| 63 | #define LOCAL_GLOBAL \ |
Neal Norwitz | 2a47c0f | 2002-01-29 00:53:41 +0000 | [diff] [blame] | 64 | "name '%.400s' is a function parameter and declared global" |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 65 | |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 66 | #define LATE_FUTURE \ |
| 67 | "from __future__ imports must occur at the beginning of the file" |
| 68 | |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 69 | #define ASSIGN_DEBUG \ |
| 70 | "can not assign to __debug__" |
| 71 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 72 | #define MANGLE_LEN 256 |
| 73 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 74 | #define OFF(x) offsetof(PyCodeObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 76 | static PyMemberDef code_memberlist[] = { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 77 | {"co_argcount", T_INT, OFF(co_argcount), READONLY}, |
| 78 | {"co_nlocals", T_INT, OFF(co_nlocals), READONLY}, |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 79 | {"co_stacksize",T_INT, OFF(co_stacksize), READONLY}, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 80 | {"co_flags", T_INT, OFF(co_flags), READONLY}, |
Guido van Rossum | 39d942d | 1992-01-12 02:30:05 +0000 | [diff] [blame] | 81 | {"co_code", T_OBJECT, OFF(co_code), READONLY}, |
| 82 | {"co_consts", T_OBJECT, OFF(co_consts), READONLY}, |
| 83 | {"co_names", T_OBJECT, OFF(co_names), READONLY}, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 84 | {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY}, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 85 | {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY}, |
| 86 | {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY}, |
Guido van Rossum | 39d942d | 1992-01-12 02:30:05 +0000 | [diff] [blame] | 87 | {"co_filename", T_OBJECT, OFF(co_filename), READONLY}, |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 88 | {"co_name", T_OBJECT, OFF(co_name), READONLY}, |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 89 | {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY}, |
| 90 | {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 91 | {NULL} /* Sentinel */ |
| 92 | }; |
| 93 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 94 | PyDoc_STRVAR(code_doc, |
| 95 | "code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\ |
| 96 | varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\ |
| 97 | \n\ |
| 98 | Create a code object. Not for the faint of heart."); |
| 99 | |
| 100 | static PyObject * |
| 101 | code_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 102 | { |
| 103 | int argcount; |
| 104 | int nlocals; |
| 105 | int stacksize; |
| 106 | int flags; |
| 107 | PyObject *code; |
| 108 | PyObject *consts; |
| 109 | PyObject *names; |
| 110 | PyObject *varnames; |
| 111 | PyObject *freevars = NULL; |
| 112 | PyObject *cellvars = NULL; |
| 113 | PyObject *filename; |
| 114 | PyObject *name; |
| 115 | int firstlineno; |
| 116 | PyObject *lnotab; |
| 117 | |
| 118 | if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code", |
| 119 | &argcount, &nlocals, &stacksize, &flags, |
| 120 | &code, |
| 121 | &PyTuple_Type, &consts, |
| 122 | &PyTuple_Type, &names, |
| 123 | &PyTuple_Type, &varnames, |
| 124 | &filename, &name, |
| 125 | &firstlineno, &lnotab, |
| 126 | &PyTuple_Type, &freevars, |
| 127 | &PyTuple_Type, &cellvars)) |
| 128 | return NULL; |
| 129 | |
| 130 | if (freevars == NULL || cellvars == NULL) { |
| 131 | PyObject *empty = PyTuple_New(0); |
| 132 | if (empty == NULL) |
| 133 | return NULL; |
| 134 | if (freevars == NULL) { |
| 135 | freevars = empty; |
| 136 | Py_INCREF(freevars); |
| 137 | } |
| 138 | if (cellvars == NULL) { |
| 139 | cellvars = empty; |
| 140 | Py_INCREF(cellvars); |
| 141 | } |
| 142 | Py_DECREF(empty); |
| 143 | } |
| 144 | |
| 145 | if (!PyObject_CheckReadBuffer(code)) { |
| 146 | PyErr_SetString(PyExc_TypeError, |
| 147 | "bytecode object must be a single-segment read-only buffer"); |
| 148 | return NULL; |
| 149 | } |
| 150 | |
| 151 | return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags, |
| 152 | code, consts, names, varnames, |
| 153 | freevars, cellvars, filename, name, |
| 154 | firstlineno, lnotab); |
| 155 | } |
| 156 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 157 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 158 | code_dealloc(PyCodeObject *co) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 159 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 160 | Py_XDECREF(co->co_code); |
| 161 | Py_XDECREF(co->co_consts); |
| 162 | Py_XDECREF(co->co_names); |
Guido van Rossum | 275558c | 1997-07-25 20:13:49 +0000 | [diff] [blame] | 163 | Py_XDECREF(co->co_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 164 | Py_XDECREF(co->co_freevars); |
| 165 | Py_XDECREF(co->co_cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 166 | Py_XDECREF(co->co_filename); |
| 167 | Py_XDECREF(co->co_name); |
Guido van Rossum | 275558c | 1997-07-25 20:13:49 +0000 | [diff] [blame] | 168 | Py_XDECREF(co->co_lnotab); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 169 | PyObject_DEL(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 172 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 173 | code_repr(PyCodeObject *co) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 174 | { |
| 175 | char buf[500]; |
| 176 | int lineno = -1; |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 177 | char *filename = "???"; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 178 | char *name = "???"; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 179 | |
Guido van Rossum | a396a88 | 2000-04-07 01:21:36 +0000 | [diff] [blame] | 180 | if (co->co_firstlineno != 0) |
| 181 | lineno = co->co_firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 182 | if (co->co_filename && PyString_Check(co->co_filename)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 183 | filename = PyString_AS_STRING(co->co_filename); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 184 | if (co->co_name && PyString_Check(co->co_name)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 185 | name = PyString_AS_STRING(co->co_name); |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 186 | PyOS_snprintf(buf, sizeof(buf), |
| 187 | "<code object %.100s at %p, file \"%.300s\", line %d>", |
| 188 | name, co, filename, lineno); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 189 | return PyString_FromString(buf); |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 192 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 193 | code_compare(PyCodeObject *co, PyCodeObject *cp) |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 194 | { |
| 195 | int cmp; |
Guido van Rossum | 4467959 | 2000-04-10 16:20:31 +0000 | [diff] [blame] | 196 | cmp = PyObject_Compare(co->co_name, cp->co_name); |
| 197 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 198 | cmp = co->co_argcount - cp->co_argcount; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 199 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 200 | cmp = co->co_nlocals - cp->co_nlocals; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 201 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 202 | cmp = co->co_flags - cp->co_flags; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 203 | if (cmp) return cmp; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 204 | cmp = PyObject_Compare(co->co_code, cp->co_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 205 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 206 | cmp = PyObject_Compare(co->co_consts, cp->co_consts); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 207 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 208 | cmp = PyObject_Compare(co->co_names, cp->co_names); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 209 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 210 | cmp = PyObject_Compare(co->co_varnames, cp->co_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 211 | if (cmp) return cmp; |
| 212 | cmp = PyObject_Compare(co->co_freevars, cp->co_freevars); |
| 213 | if (cmp) return cmp; |
| 214 | cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 215 | return cmp; |
| 216 | } |
| 217 | |
| 218 | static long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 219 | code_hash(PyCodeObject *co) |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 220 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 221 | long h, h0, h1, h2, h3, h4, h5, h6; |
Guido van Rossum | 4467959 | 2000-04-10 16:20:31 +0000 | [diff] [blame] | 222 | h0 = PyObject_Hash(co->co_name); |
| 223 | if (h0 == -1) return -1; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 224 | h1 = PyObject_Hash(co->co_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 225 | if (h1 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 226 | h2 = PyObject_Hash(co->co_consts); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 227 | if (h2 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 228 | h3 = PyObject_Hash(co->co_names); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 229 | if (h3 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 230 | h4 = PyObject_Hash(co->co_varnames); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 231 | if (h4 == -1) return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 232 | h5 = PyObject_Hash(co->co_freevars); |
| 233 | if (h5 == -1) return -1; |
| 234 | h6 = PyObject_Hash(co->co_cellvars); |
| 235 | if (h6 == -1) return -1; |
| 236 | h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 237 | co->co_argcount ^ co->co_nlocals ^ co->co_flags; |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 238 | if (h == -1) h = -2; |
| 239 | return h; |
| 240 | } |
| 241 | |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 242 | /* XXX code objects need to participate in GC? */ |
| 243 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 244 | PyTypeObject PyCode_Type = { |
| 245 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 246 | 0, |
| 247 | "code", |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 248 | sizeof(PyCodeObject), |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 249 | 0, |
Jeremy Hylton | c785f48 | 2001-09-14 20:08:07 +0000 | [diff] [blame] | 250 | (destructor)code_dealloc, /* tp_dealloc */ |
| 251 | 0, /* tp_print */ |
| 252 | 0, /* tp_getattr */ |
| 253 | 0, /* tp_setattr */ |
| 254 | (cmpfunc)code_compare, /* tp_compare */ |
| 255 | (reprfunc)code_repr, /* tp_repr */ |
| 256 | 0, /* tp_as_number */ |
| 257 | 0, /* tp_as_sequence */ |
| 258 | 0, /* tp_as_mapping */ |
| 259 | (hashfunc)code_hash, /* tp_hash */ |
| 260 | 0, /* tp_call */ |
| 261 | 0, /* tp_str */ |
| 262 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 263 | 0, /* tp_setattro */ |
| 264 | 0, /* tp_as_buffer */ |
| 265 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 266 | code_doc, /* tp_doc */ |
Jeremy Hylton | c785f48 | 2001-09-14 20:08:07 +0000 | [diff] [blame] | 267 | 0, /* tp_traverse */ |
| 268 | 0, /* tp_clear */ |
| 269 | 0, /* tp_richcompare */ |
| 270 | 0, /* tp_weaklistoffset */ |
| 271 | 0, /* tp_iter */ |
| 272 | 0, /* tp_iternext */ |
| 273 | 0, /* tp_methods */ |
| 274 | code_memberlist, /* tp_members */ |
| 275 | 0, /* tp_getset */ |
| 276 | 0, /* tp_base */ |
| 277 | 0, /* tp_dict */ |
| 278 | 0, /* tp_descr_get */ |
| 279 | 0, /* tp_descr_set */ |
| 280 | 0, /* tp_dictoffset */ |
| 281 | 0, /* tp_init */ |
| 282 | 0, /* tp_alloc */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 283 | code_new, /* tp_new */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 284 | }; |
| 285 | |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 286 | #define NAME_CHARS \ |
| 287 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" |
| 288 | |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 289 | /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */ |
| 290 | |
| 291 | static int |
| 292 | all_name_chars(unsigned char *s) |
| 293 | { |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 294 | static char ok_name_char[256]; |
| 295 | static unsigned char *name_chars = (unsigned char *)NAME_CHARS; |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 296 | |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 297 | if (ok_name_char[*name_chars] == 0) { |
| 298 | unsigned char *p; |
| 299 | for (p = name_chars; *p; p++) |
| 300 | ok_name_char[*p] = 1; |
| 301 | } |
| 302 | while (*s) { |
| 303 | if (ok_name_char[*s++] == 0) |
| 304 | return 0; |
| 305 | } |
| 306 | return 1; |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 309 | static int |
| 310 | intern_strings(PyObject *tuple) |
| 311 | { |
| 312 | int i; |
| 313 | |
| 314 | for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { |
| 315 | PyObject *v = PyTuple_GET_ITEM(tuple, i); |
| 316 | if (v == NULL || !PyString_Check(v)) { |
| 317 | Py_FatalError("non-string found in code slot"); |
| 318 | PyErr_BadInternalCall(); |
| 319 | return -1; |
| 320 | } |
| 321 | PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i)); |
| 322 | } |
| 323 | return 0; |
| 324 | } |
| 325 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 326 | PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 327 | PyCode_New(int argcount, int nlocals, int stacksize, int flags, |
| 328 | PyObject *code, PyObject *consts, PyObject *names, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 329 | PyObject *varnames, PyObject *freevars, PyObject *cellvars, |
| 330 | PyObject *filename, PyObject *name, int firstlineno, |
| 331 | PyObject *lnotab) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 332 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 333 | PyCodeObject *co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 334 | int i; |
| 335 | /* Check argument types */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 336 | if (argcount < 0 || nlocals < 0 || |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 337 | code == NULL || |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 338 | consts == NULL || !PyTuple_Check(consts) || |
| 339 | names == NULL || !PyTuple_Check(names) || |
| 340 | varnames == NULL || !PyTuple_Check(varnames) || |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 341 | freevars == NULL || !PyTuple_Check(freevars) || |
| 342 | cellvars == NULL || !PyTuple_Check(cellvars) || |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 343 | name == NULL || !PyString_Check(name) || |
| 344 | filename == NULL || !PyString_Check(filename) || |
Jeremy Hylton | 9f64caa | 2001-11-09 22:02:48 +0000 | [diff] [blame] | 345 | lnotab == NULL || !PyString_Check(lnotab) || |
| 346 | !PyObject_CheckReadBuffer(code)) { |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 347 | PyErr_BadInternalCall(); |
| 348 | return NULL; |
| 349 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 350 | intern_strings(names); |
| 351 | intern_strings(varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 352 | intern_strings(freevars); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 353 | intern_strings(cellvars); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 354 | /* Intern selected string constants */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 355 | for (i = PyTuple_Size(consts); --i >= 0; ) { |
| 356 | PyObject *v = PyTuple_GetItem(consts, i); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 357 | if (!PyString_Check(v)) |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 358 | continue; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 359 | if (!all_name_chars((unsigned char *)PyString_AS_STRING(v))) |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 360 | continue; |
| 361 | PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 362 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 363 | co = PyObject_NEW(PyCodeObject, &PyCode_Type); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 364 | if (co != NULL) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 365 | co->co_argcount = argcount; |
| 366 | co->co_nlocals = nlocals; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 367 | co->co_stacksize = stacksize; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 368 | co->co_flags = flags; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 369 | Py_INCREF(code); |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 370 | co->co_code = code; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 371 | Py_INCREF(consts); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 372 | co->co_consts = consts; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 373 | Py_INCREF(names); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 374 | co->co_names = names; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 375 | Py_INCREF(varnames); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 376 | co->co_varnames = varnames; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 377 | Py_INCREF(freevars); |
| 378 | co->co_freevars = freevars; |
| 379 | Py_INCREF(cellvars); |
| 380 | co->co_cellvars = cellvars; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 381 | Py_INCREF(filename); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 382 | co->co_filename = filename; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 383 | Py_INCREF(name); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 384 | co->co_name = name; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 385 | co->co_firstlineno = firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 386 | Py_INCREF(lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 387 | co->co_lnotab = lnotab; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 388 | } |
| 389 | return co; |
| 390 | } |
| 391 | |
| 392 | |
| 393 | /* Data structure used internally */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 394 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 395 | /* The compiler uses two passes to generate bytecodes. The first pass |
| 396 | builds the symbol table. The second pass generates the bytecode. |
| 397 | |
| 398 | The first pass uses a single symtable struct. The second pass uses |
| 399 | a compiling struct for each code block. The compiling structs |
| 400 | share a reference to the symtable. |
| 401 | |
| 402 | The two passes communicate via symtable_load_symbols() and via |
| 403 | is_local() and is_global(). The former initializes several slots |
| 404 | in the compiling struct: c_varnames, c_locals, c_nlocals, |
| 405 | c_argcount, c_globals, and c_flags. |
| 406 | */ |
| 407 | |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 408 | /* All about c_lnotab. |
| 409 | |
| 410 | c_lnotab is an array of unsigned bytes disguised as a Python string. In -O |
| 411 | mode, SET_LINENO opcodes aren't generated, and bytecode offsets are mapped |
| 412 | to source code line #s (when needed for tracebacks) via c_lnotab instead. |
| 413 | The array is conceptually a list of |
| 414 | (bytecode offset increment, line number increment) |
| 415 | pairs. The details are important and delicate, best illustrated by example: |
| 416 | |
| 417 | byte code offset source code line number |
| 418 | 0 1 |
| 419 | 6 2 |
| 420 | 50 7 |
| 421 | 350 307 |
| 422 | 361 308 |
| 423 | |
| 424 | The first trick is that these numbers aren't stored, only the increments |
| 425 | from one row to the next (this doesn't really work, but it's a start): |
| 426 | |
| 427 | 0, 1, 6, 1, 44, 5, 300, 300, 11, 1 |
| 428 | |
| 429 | The second trick is that an unsigned byte can't hold negative values, or |
| 430 | values larger than 255, so (a) there's a deep assumption that byte code |
| 431 | offsets and their corresponding line #s both increase monotonically, and (b) |
| 432 | if at least one column jumps by more than 255 from one row to the next, more |
| 433 | than one pair is written to the table. In case #b, there's no way to know |
| 434 | from looking at the table later how many were written. That's the delicate |
| 435 | part. A user of c_lnotab desiring to find the source line number |
| 436 | corresponding to a bytecode address A should do something like this |
| 437 | |
| 438 | lineno = addr = 0 |
| 439 | for addr_incr, line_incr in c_lnotab: |
| 440 | addr += addr_incr |
| 441 | if addr > A: |
| 442 | return lineno |
| 443 | lineno += line_incr |
| 444 | |
| 445 | In order for this to work, when the addr field increments by more than 255, |
| 446 | the line # increment in each pair generated must be 0 until the remaining addr |
| 447 | increment is < 256. So, in the example above, com_set_lineno should not (as |
| 448 | was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to |
| 449 | 255, 0, 45, 255, 0, 45. |
| 450 | */ |
| 451 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 452 | struct compiling { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 453 | PyObject *c_code; /* string */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 454 | PyObject *c_consts; /* list of objects */ |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 455 | PyObject *c_const_dict; /* inverse of c_consts */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 456 | PyObject *c_names; /* list of strings (names) */ |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 457 | PyObject *c_name_dict; /* inverse of c_names */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 458 | PyObject *c_globals; /* dictionary (value=None) */ |
| 459 | PyObject *c_locals; /* dictionary (value=localID) */ |
| 460 | PyObject *c_varnames; /* list (inverse of c_locals) */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 461 | PyObject *c_freevars; /* dictionary (value=None) */ |
| 462 | PyObject *c_cellvars; /* list */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 463 | int c_nlocals; /* index of next local */ |
| 464 | int c_argcount; /* number of top-level arguments */ |
| 465 | int c_flags; /* same as co_flags */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 466 | int c_nexti; /* index into c_code */ |
| 467 | int c_errors; /* counts errors occurred */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 468 | int c_infunction; /* set when compiling a function */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 469 | int c_interactive; /* generating code for interactive command */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 470 | int c_loops; /* counts nested loops */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 471 | int c_begin; /* begin of current loop, for 'continue' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 472 | int c_block[CO_MAXBLOCKS]; /* stack of block types */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 473 | int c_nblocks; /* current block stack level */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 474 | char *c_filename; /* filename of current node */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 475 | char *c_name; /* name of object (e.g. function) */ |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 476 | int c_lineno; /* Current line number */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 477 | int c_stacklevel; /* Current stack level */ |
| 478 | int c_maxstacklevel; /* Maximum stack level */ |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 479 | int c_firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 480 | PyObject *c_lnotab; /* Table mapping address to line number */ |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 481 | int c_last_addr, c_last_line, c_lnotab_next; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 482 | char *c_private; /* for private name mangling */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 483 | int c_tmpname; /* temporary local name counter */ |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 484 | int c_nested; /* Is block nested funcdef or lamdef? */ |
| 485 | int c_closure; /* Is nested w/freevars? */ |
| 486 | struct symtable *c_symtable; /* pointer to module symbol table */ |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 487 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 488 | char *c_encoding; /* source encoding (a borrowed reference) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 489 | }; |
| 490 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 491 | static int |
| 492 | is_free(int v) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 493 | { |
| 494 | if ((v & (USE | DEF_FREE)) |
| 495 | && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL))) |
| 496 | return 1; |
| 497 | if (v & DEF_FREE_CLASS) |
| 498 | return 1; |
| 499 | return 0; |
| 500 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 501 | |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 502 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 503 | com_error(struct compiling *c, PyObject *exc, char *msg) |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 504 | { |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 505 | PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL; |
| 506 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 507 | if (c == NULL) { |
| 508 | /* Error occurred via symtable call to |
| 509 | is_constant_false */ |
| 510 | PyErr_SetString(exc, msg); |
| 511 | return; |
| 512 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 513 | c->c_errors++; |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 514 | if (c->c_lineno < 1 || c->c_interactive) { |
| 515 | /* Unknown line number or interactive input */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 516 | PyErr_SetString(exc, msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 517 | return; |
| 518 | } |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 519 | v = PyString_FromString(msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 520 | if (v == NULL) |
| 521 | return; /* MemoryError, too bad */ |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 522 | |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 523 | line = PyErr_ProgramText(c->c_filename, c->c_lineno); |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 524 | if (line == NULL) { |
| 525 | Py_INCREF(Py_None); |
| 526 | line = Py_None; |
| 527 | } |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 528 | if (exc == PyExc_SyntaxError) { |
| 529 | t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno, |
| 530 | Py_None, line); |
| 531 | if (t == NULL) |
| 532 | goto exit; |
| 533 | w = Py_BuildValue("(OO)", v, t); |
| 534 | if (w == NULL) |
| 535 | goto exit; |
| 536 | PyErr_SetObject(exc, w); |
| 537 | } else { |
| 538 | /* Make sure additional exceptions are printed with |
| 539 | file and line, also. */ |
| 540 | PyErr_SetObject(exc, v); |
| 541 | PyErr_SyntaxLocation(c->c_filename, c->c_lineno); |
| 542 | } |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 543 | exit: |
| 544 | Py_XDECREF(t); |
| 545 | Py_XDECREF(v); |
| 546 | Py_XDECREF(w); |
| 547 | Py_XDECREF(line); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 550 | /* Interface to the block stack */ |
| 551 | |
| 552 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 553 | block_push(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 554 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 555 | if (c->c_nblocks >= CO_MAXBLOCKS) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 556 | com_error(c, PyExc_SystemError, |
| 557 | "too many statically nested blocks"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 558 | } |
| 559 | else { |
| 560 | c->c_block[c->c_nblocks++] = type; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 565 | block_pop(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 566 | { |
| 567 | if (c->c_nblocks > 0) |
| 568 | c->c_nblocks--; |
| 569 | 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] | 570 | com_error(c, PyExc_SystemError, "bad block pop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 574 | /* Prototype forward declarations */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 575 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 576 | static int com_init(struct compiling *, char *); |
| 577 | static void com_free(struct compiling *); |
| 578 | static void com_push(struct compiling *, int); |
| 579 | static void com_pop(struct compiling *, int); |
| 580 | static void com_done(struct compiling *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 581 | static void com_node(struct compiling *, node *); |
| 582 | static void com_factor(struct compiling *, node *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 583 | static void com_addbyte(struct compiling *, int); |
| 584 | static void com_addint(struct compiling *, int); |
| 585 | static void com_addoparg(struct compiling *, int, int); |
| 586 | static void com_addfwref(struct compiling *, int, int *); |
| 587 | static void com_backpatch(struct compiling *, int); |
| 588 | static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *); |
| 589 | static int com_addconst(struct compiling *, PyObject *); |
| 590 | static int com_addname(struct compiling *, PyObject *); |
| 591 | static void com_addopname(struct compiling *, int, node *); |
| 592 | static void com_list(struct compiling *, node *, int); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 593 | static void com_list_iter(struct compiling *, node *, node *, char *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 594 | static int com_argdefs(struct compiling *, node *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 595 | static void com_assign(struct compiling *, node *, int, node *); |
| 596 | static void com_assign_name(struct compiling *, node *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 597 | static PyCodeObject *icompile(node *, struct compiling *); |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 598 | static PyCodeObject *jcompile(node *, char *, struct compiling *, |
| 599 | PyCompilerFlags *); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 600 | static PyObject *parsestrplus(struct compiling*, node *); |
| 601 | static PyObject *parsestr(struct compiling *, char *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 602 | static node *get_rawdocstring(node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 603 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 604 | static int get_ref_type(struct compiling *, char *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 605 | |
| 606 | /* symtable operations */ |
| 607 | static int symtable_build(struct compiling *, node *); |
| 608 | static int symtable_load_symbols(struct compiling *); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 609 | static struct symtable *symtable_init(void); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 610 | static void symtable_enter_scope(struct symtable *, char *, int, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 611 | static int symtable_exit_scope(struct symtable *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 612 | static int symtable_add_def(struct symtable *, char *, int); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 613 | static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 614 | |
| 615 | static void symtable_node(struct symtable *, node *); |
| 616 | static void symtable_funcdef(struct symtable *, node *); |
| 617 | static void symtable_default_args(struct symtable *, node *); |
| 618 | static void symtable_params(struct symtable *, node *); |
| 619 | static void symtable_params_fplist(struct symtable *, node *n); |
| 620 | static void symtable_global(struct symtable *, node *); |
| 621 | static void symtable_import(struct symtable *, node *); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 622 | static void symtable_assign(struct symtable *, node *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 623 | static void symtable_list_comprehension(struct symtable *, node *); |
| 624 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 625 | static int symtable_update_free_vars(struct symtable *); |
| 626 | static int symtable_undo_free(struct symtable *, PyObject *, PyObject *); |
| 627 | static int symtable_check_global(struct symtable *, PyObject *, PyObject *); |
| 628 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 629 | /* helper */ |
| 630 | static void |
| 631 | do_pad(int pad) |
| 632 | { |
| 633 | int i; |
| 634 | for (i = 0; i < pad; ++i) |
| 635 | fprintf(stderr, " "); |
| 636 | } |
| 637 | |
| 638 | static void |
| 639 | dump(node *n, int pad, int depth) |
| 640 | { |
| 641 | int i; |
| 642 | if (depth == 0) |
| 643 | return; |
| 644 | do_pad(pad); |
| 645 | fprintf(stderr, "%d: %s\n", TYPE(n), STR(n)); |
| 646 | if (depth > 0) |
| 647 | depth--; |
| 648 | for (i = 0; i < NCH(n); ++i) |
| 649 | dump(CHILD(n, i), pad + 1, depth); |
| 650 | } |
| 651 | |
| 652 | #define DUMP(N) dump(N, 0, -1) |
| 653 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 654 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 655 | com_init(struct compiling *c, char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 656 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 657 | memset((void *)c, '\0', sizeof(struct compiling)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 658 | if ((c->c_code = PyString_FromStringAndSize((char *)NULL, |
| 659 | 1000)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 660 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 661 | if ((c->c_consts = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 662 | goto fail; |
| 663 | if ((c->c_const_dict = PyDict_New()) == NULL) |
| 664 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 665 | if ((c->c_names = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 666 | goto fail; |
| 667 | if ((c->c_name_dict = PyDict_New()) == NULL) |
| 668 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 669 | if ((c->c_locals = PyDict_New()) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 670 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 671 | if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL, |
| 672 | 1000)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 673 | goto fail; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 674 | c->c_globals = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 675 | c->c_varnames = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 676 | c->c_freevars = NULL; |
| 677 | c->c_cellvars = NULL; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 678 | c->c_nlocals = 0; |
| 679 | c->c_argcount = 0; |
| 680 | c->c_flags = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 681 | c->c_nexti = 0; |
| 682 | c->c_errors = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 683 | c->c_infunction = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 684 | c->c_interactive = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 685 | c->c_loops = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 686 | c->c_begin = 0; |
| 687 | c->c_nblocks = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 688 | c->c_filename = filename; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 689 | c->c_name = "?"; |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 690 | c->c_lineno = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 691 | c->c_stacklevel = 0; |
| 692 | c->c_maxstacklevel = 0; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 693 | c->c_firstlineno = 0; |
| 694 | c->c_last_addr = 0; |
| 695 | c->c_last_line = 0; |
Barry Warsaw | 174e801 | 2001-01-22 04:35:57 +0000 | [diff] [blame] | 696 | c->c_lnotab_next = 0; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 697 | c->c_tmpname = 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 698 | c->c_nested = 0; |
| 699 | c->c_closure = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 700 | c->c_symtable = NULL; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 701 | return 1; |
| 702 | |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 703 | fail: |
| 704 | com_free(c); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 709 | com_free(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 710 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 711 | Py_XDECREF(c->c_code); |
| 712 | Py_XDECREF(c->c_consts); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 713 | Py_XDECREF(c->c_const_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 714 | Py_XDECREF(c->c_names); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 715 | Py_XDECREF(c->c_name_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 716 | Py_XDECREF(c->c_globals); |
| 717 | Py_XDECREF(c->c_locals); |
| 718 | Py_XDECREF(c->c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 719 | Py_XDECREF(c->c_freevars); |
| 720 | Py_XDECREF(c->c_cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 721 | Py_XDECREF(c->c_lnotab); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 722 | if (c->c_future) |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 723 | PyObject_FREE((void *)c->c_future); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 727 | com_push(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 728 | { |
| 729 | c->c_stacklevel += n; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 730 | if (c->c_stacklevel > c->c_maxstacklevel) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 731 | c->c_maxstacklevel = c->c_stacklevel; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 732 | /* |
| 733 | fprintf(stderr, "%s:%s:%d max stack nexti=%d level=%d n=%d\n", |
| 734 | c->c_filename, c->c_name, c->c_lineno, |
| 735 | c->c_nexti, c->c_stacklevel, n); |
| 736 | */ |
| 737 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 741 | com_pop(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 742 | { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 743 | if (c->c_stacklevel < n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 744 | c->c_stacklevel = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 745 | else |
| 746 | c->c_stacklevel -= n; |
| 747 | } |
| 748 | |
| 749 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 750 | com_done(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 751 | { |
| 752 | if (c->c_code != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 753 | _PyString_Resize(&c->c_code, c->c_nexti); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 754 | if (c->c_lnotab != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 755 | _PyString_Resize(&c->c_lnotab, c->c_lnotab_next); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 758 | static int |
| 759 | com_check_size(PyObject **s, int offset) |
| 760 | { |
| 761 | int len = PyString_GET_SIZE(*s); |
| 762 | if (offset >= len) |
| 763 | return _PyString_Resize(s, len * 2); |
| 764 | return 0; |
| 765 | } |
| 766 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 767 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 768 | com_addbyte(struct compiling *c, int byte) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 769 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 770 | /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 771 | assert(byte >= 0 && byte <= 255); |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 772 | assert(c->c_code != 0); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 773 | if (com_check_size(&c->c_code, c->c_nexti)) { |
| 774 | c->c_errors++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 775 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 776 | } |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 777 | PyString_AS_STRING(c->c_code)[c->c_nexti++] = byte; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 781 | com_addint(struct compiling *c, int x) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 782 | { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 783 | com_addbyte(c, x & 0xff); |
| 784 | com_addbyte(c, x >> 8); /* XXX x should be positive */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 788 | com_add_lnotab(struct compiling *c, int addr, int line) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 789 | { |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 790 | char *p; |
| 791 | if (c->c_lnotab == NULL) |
| 792 | return; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 793 | if (com_check_size(&c->c_lnotab, c->c_lnotab_next + 2)) { |
| 794 | c->c_errors++; |
| 795 | return; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 796 | } |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 797 | p = PyString_AS_STRING(c->c_lnotab) + c->c_lnotab_next; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 798 | *p++ = addr; |
| 799 | *p++ = line; |
| 800 | c->c_lnotab_next += 2; |
| 801 | } |
| 802 | |
| 803 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 804 | com_set_lineno(struct compiling *c, int lineno) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 805 | { |
| 806 | c->c_lineno = lineno; |
| 807 | if (c->c_firstlineno == 0) { |
| 808 | c->c_firstlineno = c->c_last_line = lineno; |
| 809 | } |
| 810 | else { |
| 811 | int incr_addr = c->c_nexti - c->c_last_addr; |
| 812 | int incr_line = lineno - c->c_last_line; |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 813 | while (incr_addr > 255) { |
| 814 | com_add_lnotab(c, 255, 0); |
| 815 | incr_addr -= 255; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 816 | } |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 817 | while (incr_line > 255) { |
| 818 | com_add_lnotab(c, incr_addr, 255); |
| 819 | incr_line -=255; |
| 820 | incr_addr = 0; |
| 821 | } |
| 822 | if (incr_addr > 0 || incr_line > 0) |
| 823 | com_add_lnotab(c, incr_addr, incr_line); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 824 | c->c_last_addr = c->c_nexti; |
| 825 | c->c_last_line = lineno; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 830 | com_addoparg(struct compiling *c, int op, int arg) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 831 | { |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 832 | int extended_arg = arg >> 16; |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 833 | if (op == SET_LINENO) { |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 834 | com_set_lineno(c, arg); |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 835 | if (Py_OptimizeFlag) |
| 836 | return; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 837 | } |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 838 | if (extended_arg){ |
| 839 | com_addbyte(c, EXTENDED_ARG); |
| 840 | com_addint(c, extended_arg); |
| 841 | arg &= 0xffff; |
| 842 | } |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 843 | com_addbyte(c, op); |
| 844 | com_addint(c, arg); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 848 | com_addfwref(struct compiling *c, int op, int *p_anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 849 | { |
| 850 | /* Compile a forward reference for backpatching */ |
| 851 | int here; |
| 852 | int anchor; |
| 853 | com_addbyte(c, op); |
| 854 | here = c->c_nexti; |
| 855 | anchor = *p_anchor; |
| 856 | *p_anchor = here; |
| 857 | com_addint(c, anchor == 0 ? 0 : here - anchor); |
| 858 | } |
| 859 | |
| 860 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 861 | com_backpatch(struct compiling *c, int anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 862 | { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 863 | unsigned char *code = (unsigned char *) PyString_AS_STRING(c->c_code); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 864 | int target = c->c_nexti; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 865 | int dist; |
| 866 | int prev; |
| 867 | for (;;) { |
| 868 | /* Make the JUMP instruction at anchor point to target */ |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 869 | prev = code[anchor] + (code[anchor+1] << 8); |
| 870 | dist = target - (anchor+2); |
| 871 | code[anchor] = dist & 0xff; |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 872 | dist >>= 8; |
| 873 | code[anchor+1] = dist; |
| 874 | dist >>= 8; |
| 875 | if (dist) { |
| 876 | com_error(c, PyExc_SystemError, |
| 877 | "com_backpatch: offset too large"); |
| 878 | break; |
| 879 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 880 | if (!prev) |
| 881 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 882 | anchor -= prev; |
| 883 | } |
| 884 | } |
| 885 | |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 886 | /* Handle literals and names uniformly */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 887 | |
| 888 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 889 | com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 890 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 891 | PyObject *w, *t, *np=NULL; |
| 892 | long n; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 893 | |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 894 | t = Py_BuildValue("(OO)", v, v->ob_type); |
| 895 | if (t == NULL) |
| 896 | goto fail; |
| 897 | w = PyDict_GetItem(dict, t); |
| 898 | if (w != NULL) { |
| 899 | n = PyInt_AsLong(w); |
| 900 | } else { |
| 901 | n = PyList_Size(list); |
| 902 | np = PyInt_FromLong(n); |
| 903 | if (np == NULL) |
| 904 | goto fail; |
| 905 | if (PyList_Append(list, v) != 0) |
| 906 | goto fail; |
| 907 | if (PyDict_SetItem(dict, t, np) != 0) |
| 908 | goto fail; |
| 909 | Py_DECREF(np); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 910 | } |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 911 | Py_DECREF(t); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 912 | return n; |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 913 | fail: |
| 914 | Py_XDECREF(np); |
| 915 | Py_XDECREF(t); |
| 916 | c->c_errors++; |
| 917 | return 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 921 | com_addconst(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 922 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 923 | return com_add(c, c->c_consts, c->c_const_dict, v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 927 | com_addname(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 928 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 929 | return com_add(c, c->c_names, c->c_name_dict, v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 932 | int |
| 933 | _Py_Mangle(char *p, char *name, char *buffer, size_t maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 934 | { |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 935 | /* Name mangling: __private becomes _classname__private. |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 936 | This is independent from how the name is used. */ |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 937 | size_t nlen, plen; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 938 | if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_') |
| 939 | return 0; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 940 | nlen = strlen(name); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 941 | if (nlen+2 >= maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 942 | return 0; /* Don't mangle __extremely_long_names */ |
| 943 | if (name[nlen-1] == '_' && name[nlen-2] == '_') |
| 944 | return 0; /* Don't mangle __whatever__ */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 945 | /* Strip leading underscores from class name */ |
| 946 | while (*p == '_') |
| 947 | p++; |
| 948 | if (*p == '\0') |
| 949 | return 0; /* Don't mangle if class is just underscores */ |
| 950 | plen = strlen(p); |
| 951 | if (plen + nlen >= maxlen) |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 952 | plen = maxlen-nlen-2; /* Truncate class name if too long */ |
| 953 | /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 954 | buffer[0] = '_'; |
| 955 | strncpy(buffer+1, p, plen); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 956 | strcpy(buffer+1+plen, name); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 957 | return 1; |
| 958 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 959 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 960 | static void |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 961 | com_addop_name(struct compiling *c, int op, char *name) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 962 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 963 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 964 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 965 | char buffer[MANGLE_LEN]; |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 966 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 967 | if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer))) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 968 | name = buffer; |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 969 | if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 970 | c->c_errors++; |
| 971 | i = 255; |
| 972 | } |
| 973 | else { |
| 974 | i = com_addname(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 975 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 976 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 977 | com_addoparg(c, op, i); |
| 978 | } |
| 979 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 980 | #define NAME_LOCAL 0 |
| 981 | #define NAME_GLOBAL 1 |
| 982 | #define NAME_DEFAULT 2 |
| 983 | #define NAME_CLOSURE 3 |
| 984 | |
| 985 | static int |
| 986 | com_lookup_arg(PyObject *dict, PyObject *name) |
| 987 | { |
| 988 | PyObject *v = PyDict_GetItem(dict, name); |
| 989 | if (v == NULL) |
| 990 | return -1; |
| 991 | else |
| 992 | return PyInt_AS_LONG(v); |
| 993 | } |
| 994 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 995 | static void |
| 996 | com_addop_varname(struct compiling *c, int kind, char *name) |
| 997 | { |
| 998 | PyObject *v; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 999 | int i, reftype; |
| 1000 | int scope = NAME_DEFAULT; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1001 | int op = STOP_CODE; |
| 1002 | char buffer[MANGLE_LEN]; |
| 1003 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1004 | if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1005 | name = buffer; |
| 1006 | if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { |
| 1007 | c->c_errors++; |
| 1008 | i = 255; |
| 1009 | goto done; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1010 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1011 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1012 | reftype = get_ref_type(c, name); |
| 1013 | switch (reftype) { |
| 1014 | case LOCAL: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 1015 | if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1016 | scope = NAME_LOCAL; |
| 1017 | break; |
| 1018 | case GLOBAL_EXPLICIT: |
| 1019 | scope = NAME_GLOBAL; |
| 1020 | break; |
| 1021 | case GLOBAL_IMPLICIT: |
| 1022 | if (c->c_flags & CO_OPTIMIZED) |
| 1023 | scope = NAME_GLOBAL; |
| 1024 | break; |
| 1025 | case FREE: |
| 1026 | case CELL: |
| 1027 | scope = NAME_CLOSURE; |
| 1028 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | i = com_addname(c, v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1032 | if (scope == NAME_LOCAL) |
| 1033 | i = com_lookup_arg(c->c_locals, v); |
| 1034 | else if (reftype == FREE) |
| 1035 | i = com_lookup_arg(c->c_freevars, v); |
| 1036 | else if (reftype == CELL) |
| 1037 | i = com_lookup_arg(c->c_cellvars, v); |
| 1038 | if (i == -1) { |
| 1039 | c->c_errors++; /* XXX no exception set */ |
| 1040 | i = 255; |
| 1041 | goto done; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1042 | } |
| 1043 | Py_DECREF(v); |
| 1044 | |
| 1045 | switch (kind) { |
| 1046 | case VAR_LOAD: |
| 1047 | switch (scope) { |
| 1048 | case NAME_LOCAL: |
| 1049 | op = LOAD_FAST; |
| 1050 | break; |
| 1051 | case NAME_GLOBAL: |
| 1052 | op = LOAD_GLOBAL; |
| 1053 | break; |
| 1054 | case NAME_DEFAULT: |
| 1055 | op = LOAD_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1056 | break; |
| 1057 | case NAME_CLOSURE: |
| 1058 | op = LOAD_DEREF; |
| 1059 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1060 | } |
| 1061 | break; |
| 1062 | case VAR_STORE: |
| 1063 | switch (scope) { |
| 1064 | case NAME_LOCAL: |
| 1065 | op = STORE_FAST; |
| 1066 | break; |
| 1067 | case NAME_GLOBAL: |
| 1068 | op = STORE_GLOBAL; |
| 1069 | break; |
| 1070 | case NAME_DEFAULT: |
| 1071 | op = STORE_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1072 | break; |
| 1073 | case NAME_CLOSURE: |
| 1074 | op = STORE_DEREF; |
| 1075 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1076 | } |
| 1077 | break; |
| 1078 | case VAR_DELETE: |
| 1079 | switch (scope) { |
| 1080 | case NAME_LOCAL: |
| 1081 | op = DELETE_FAST; |
| 1082 | break; |
| 1083 | case NAME_GLOBAL: |
| 1084 | op = DELETE_GLOBAL; |
| 1085 | break; |
| 1086 | case NAME_DEFAULT: |
| 1087 | op = DELETE_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1088 | break; |
| 1089 | case NAME_CLOSURE: { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 1090 | char buf[500]; |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 1091 | PyOS_snprintf(buf, sizeof(buf), |
| 1092 | DEL_CLOSURE_ERROR, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1093 | com_error(c, PyExc_SyntaxError, buf); |
| 1094 | i = 255; |
| 1095 | break; |
| 1096 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1097 | } |
| 1098 | break; |
| 1099 | } |
| 1100 | done: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1101 | com_addoparg(c, op, i); |
| 1102 | } |
| 1103 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1104 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1105 | com_addopname(struct compiling *c, int op, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1106 | { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1107 | char *name; |
| 1108 | char buffer[1000]; |
| 1109 | /* XXX it is possible to write this code without the 1000 |
| 1110 | chars on the total length of dotted names, I just can't be |
| 1111 | bothered right now */ |
| 1112 | if (TYPE(n) == STAR) |
| 1113 | name = "*"; |
| 1114 | else if (TYPE(n) == dotted_name) { |
| 1115 | char *p = buffer; |
| 1116 | int i; |
| 1117 | name = buffer; |
| 1118 | for (i = 0; i < NCH(n); i += 2) { |
| 1119 | char *s = STR(CHILD(n, i)); |
| 1120 | if (p + strlen(s) > buffer + (sizeof buffer) - 2) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1121 | com_error(c, PyExc_MemoryError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1122 | "dotted_name too long"); |
Guido van Rossum | d9dfaf5 | 1995-02-17 15:04:57 +0000 | [diff] [blame] | 1123 | name = NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1124 | break; |
| 1125 | } |
| 1126 | if (p != buffer) |
| 1127 | *p++ = '.'; |
| 1128 | strcpy(p, s); |
| 1129 | p = strchr(p, '\0'); |
| 1130 | } |
| 1131 | } |
| 1132 | else { |
| 1133 | REQ(n, NAME); |
| 1134 | name = STR(n); |
| 1135 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1136 | com_addop_name(c, op, name); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1139 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1140 | parsenumber(struct compiling *co, char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1141 | { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1142 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1143 | long x; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1144 | double dx; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1145 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 1146 | Py_complex c; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1147 | int imflag; |
| 1148 | #endif |
| 1149 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1150 | errno = 0; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1151 | end = s + strlen(s) - 1; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1152 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 15ad9a6 | 1996-01-26 20:53:56 +0000 | [diff] [blame] | 1153 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1154 | #endif |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 1155 | if (*end == 'l' || *end == 'L') |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1156 | return PyLong_FromString(s, (char **)0, 0); |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 1157 | if (s[0] == '0') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1158 | x = (long) PyOS_strtoul(s, &end, 0); |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 1159 | if (x < 0 && errno == 0) { |
Guido van Rossum | 715eca9 | 2002-08-12 21:54:46 +0000 | [diff] [blame] | 1160 | if (PyErr_WarnExplicit( |
Barry Warsaw | 9f00739 | 2002-08-14 15:51:29 +0000 | [diff] [blame^] | 1161 | PyExc_FutureWarning, |
Guido van Rossum | 715eca9 | 2002-08-12 21:54:46 +0000 | [diff] [blame] | 1162 | "hex/oct constants > sys.maxint " |
| 1163 | "will return positive values " |
| 1164 | "in Python 2.4 and up", |
| 1165 | co->c_filename, |
| 1166 | co->c_lineno, |
| 1167 | NULL, |
| 1168 | NULL) < 0) |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 1169 | return NULL; |
Guido van Rossum | 3cb8e54 | 2002-08-11 14:06:15 +0000 | [diff] [blame] | 1170 | errno = 0; /* Might be changed by PyErr_Warn() */ |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 1171 | } |
| 1172 | } |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 1173 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1174 | x = PyOS_strtol(s, &end, 0); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1175 | if (*end == '\0') { |
Jeremy Hylton | 71b6af9 | 2001-08-27 19:45:25 +0000 | [diff] [blame] | 1176 | if (errno != 0) |
| 1177 | return PyLong_FromString(s, (char **)0, 0); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1178 | return PyInt_FromLong(x); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1179 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1180 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1181 | #ifndef WITHOUT_COMPLEX |
| 1182 | if (imflag) { |
| 1183 | c.real = 0.; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1184 | PyFPE_START_PROTECT("atof", return 0) |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1185 | c.imag = atof(s); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1186 | PyFPE_END_PROTECT(c) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1187 | return PyComplex_FromCComplex(c); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1188 | } |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1189 | else |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1190 | #endif |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1191 | { |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1192 | PyFPE_START_PROTECT("atof", return 0) |
| 1193 | dx = atof(s); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1194 | PyFPE_END_PROTECT(dx) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1195 | return PyFloat_FromDouble(dx); |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1196 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1199 | static PyObject * |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1200 | decode_utf8(char **sPtr, char *end, char* encoding) |
| 1201 | { |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1202 | #ifndef Py_USING_UNICODE |
Martin v. Löwis | 2863c10 | 2002-08-07 15:18:57 +0000 | [diff] [blame] | 1203 | Py_FatalError("decode_utf8 should not be called in this build."); |
| 1204 | return NULL; |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1205 | #else |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1206 | PyObject *u, *v; |
| 1207 | char *s, *t; |
| 1208 | t = s = *sPtr; |
| 1209 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 1210 | while (s < end && (*s & 0x80)) s++; |
| 1211 | *sPtr = s; |
| 1212 | u = PyUnicode_DecodeUTF8(t, s - t, NULL); |
| 1213 | if (u == NULL) |
| 1214 | return NULL; |
| 1215 | v = PyUnicode_AsEncodedString(u, encoding, NULL); |
| 1216 | Py_DECREF(u); |
| 1217 | return v; |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1218 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1222 | parsestr(struct compiling *com, char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1223 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1224 | PyObject *v; |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1225 | size_t len; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1226 | char *buf; |
| 1227 | char *p; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1228 | char *end; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 1229 | int quote = *s; |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1230 | int rawmode = 0; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1231 | char* encoding = ((com == NULL) ? NULL : com->c_encoding); |
| 1232 | int need_encoding; |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1233 | int unicode = 0; |
Guido van Rossum | 05459c5 | 2002-05-28 18:47:29 +0000 | [diff] [blame] | 1234 | |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1235 | if (isalpha(quote) || quote == '_') { |
| 1236 | if (quote == 'u' || quote == 'U') { |
| 1237 | quote = *++s; |
| 1238 | unicode = 1; |
| 1239 | } |
| 1240 | if (quote == 'r' || quote == 'R') { |
| 1241 | quote = *++s; |
| 1242 | rawmode = 1; |
| 1243 | } |
| 1244 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1245 | if (quote != '\'' && quote != '\"') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1246 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1247 | return NULL; |
| 1248 | } |
| 1249 | s++; |
| 1250 | len = strlen(s); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1251 | if (len > INT_MAX) { |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1252 | com_error(com, PyExc_OverflowError, |
| 1253 | "string to parse is too long"); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1254 | return NULL; |
| 1255 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1256 | if (s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1257 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1258 | return NULL; |
| 1259 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1260 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
| 1261 | s += 2; |
| 1262 | len -= 2; |
| 1263 | if (s[--len] != quote || s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1264 | PyErr_BadInternalCall(); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1265 | return NULL; |
| 1266 | } |
| 1267 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1268 | #ifdef Py_USING_UNICODE |
Guido van Rossum | fdc8bdb | 2000-05-01 17:54:56 +0000 | [diff] [blame] | 1269 | if (unicode || Py_UnicodeFlag) { |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1270 | PyObject *u, *w; |
| 1271 | if (encoding == NULL) { |
| 1272 | buf = s; |
| 1273 | u = NULL; |
| 1274 | } else if (strcmp(encoding, "iso-8859-1") == 0) { |
| 1275 | buf = s; |
| 1276 | u = NULL; |
| 1277 | } else { |
| 1278 | /* "\XX" may become "\u005c\uHHLL" (12 bytes) */ |
| 1279 | u = PyString_FromStringAndSize((char *)NULL, len * 4); |
| 1280 | if (u == NULL) |
| 1281 | return NULL; |
| 1282 | p = buf = PyString_AsString(u); |
| 1283 | end = s + len; |
| 1284 | while (s < end) { |
| 1285 | if (*s == '\\') { |
| 1286 | *p++ = *s++; |
| 1287 | if (*s & 0x80) { |
| 1288 | strcpy(p, "u005c"); |
| 1289 | p += 5; |
| 1290 | } |
| 1291 | } |
| 1292 | if (*s & 0x80) { /* XXX inefficient */ |
| 1293 | char *r; |
| 1294 | int rn, i; |
| 1295 | w = decode_utf8(&s, end, "utf-16-be"); |
| 1296 | if (w == NULL) { |
| 1297 | Py_DECREF(u); |
| 1298 | return NULL; |
| 1299 | } |
| 1300 | r = PyString_AsString(w); |
| 1301 | rn = PyString_Size(w); |
| 1302 | assert(rn % 2 == 0); |
| 1303 | for (i = 0; i < rn; i += 2) { |
| 1304 | sprintf(p, "\\u%02x%02x", |
| 1305 | r[i + 0] & 0xFF, |
| 1306 | r[i + 1] & 0xFF); |
| 1307 | p += 6; |
| 1308 | } |
| 1309 | Py_DECREF(w); |
| 1310 | } else { |
| 1311 | *p++ = *s++; |
| 1312 | } |
| 1313 | } |
| 1314 | len = p - buf; |
| 1315 | } |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1316 | if (rawmode) |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1317 | v = PyUnicode_DecodeRawUnicodeEscape(buf, len, NULL); |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1318 | else |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1319 | v = PyUnicode_DecodeUnicodeEscape(buf, len, NULL); |
| 1320 | Py_XDECREF(u); |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1321 | if (v == NULL) |
| 1322 | PyErr_SyntaxLocation(com->c_filename, com->c_lineno); |
| 1323 | return v; |
| 1324 | |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1325 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1326 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1327 | need_encoding = (encoding != NULL && |
| 1328 | strcmp(encoding, "utf-8") != 0 && |
| 1329 | strcmp(encoding, "iso-8859-1") != 0); |
| 1330 | if (rawmode || strchr(s, '\\') == NULL) { |
| 1331 | if (need_encoding) { |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1332 | #ifndef Py_USING_UNICODE |
| 1333 | /* This should not happen - we never see any other |
| 1334 | encoding. */ |
Martin v. Löwis | 2863c10 | 2002-08-07 15:18:57 +0000 | [diff] [blame] | 1335 | Py_FatalError("cannot deal with encodings in this build."); |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1336 | #else |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1337 | PyObject* u = PyUnicode_DecodeUTF8(s, len, NULL); |
| 1338 | if (u == NULL) |
| 1339 | return NULL; |
| 1340 | v = PyUnicode_AsEncodedString(u, encoding, NULL); |
| 1341 | Py_DECREF(u); |
| 1342 | return v; |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1343 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1344 | } else { |
| 1345 | return PyString_FromStringAndSize(s, len); |
| 1346 | } |
| 1347 | } |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 1348 | |
| 1349 | v = PyString_DecodeEscape(s, len, NULL, unicode, |
| 1350 | need_encoding ? encoding : NULL); |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1351 | if (v == NULL) |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 1352 | PyErr_SyntaxLocation(com->c_filename, com->c_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1353 | return v; |
| 1354 | } |
| 1355 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1356 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1357 | parsestrplus(struct compiling* c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1358 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1359 | PyObject *v; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1360 | int i; |
| 1361 | REQ(CHILD(n, 0), STRING); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1362 | if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1363 | /* String literal concatenation */ |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1364 | for (i = 1; i < NCH(n); i++) { |
| 1365 | PyObject *s; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1366 | s = parsestr(c, STR(CHILD(n, i))); |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1367 | if (s == NULL) |
| 1368 | goto onError; |
| 1369 | if (PyString_Check(v) && PyString_Check(s)) { |
| 1370 | PyString_ConcatAndDel(&v, s); |
| 1371 | if (v == NULL) |
| 1372 | goto onError; |
| 1373 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1374 | #ifdef Py_USING_UNICODE |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1375 | else { |
| 1376 | PyObject *temp; |
| 1377 | temp = PyUnicode_Concat(v, s); |
| 1378 | Py_DECREF(s); |
| 1379 | if (temp == NULL) |
| 1380 | goto onError; |
| 1381 | Py_DECREF(v); |
| 1382 | v = temp; |
| 1383 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1384 | #endif |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1385 | } |
| 1386 | } |
| 1387 | return v; |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1388 | |
| 1389 | onError: |
| 1390 | Py_XDECREF(v); |
| 1391 | return NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1394 | static void |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1395 | com_list_for(struct compiling *c, node *n, node *e, char *t) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1396 | { |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1397 | int anchor = 0; |
| 1398 | int save_begin = c->c_begin; |
| 1399 | |
| 1400 | /* list_iter: for v in expr [list_iter] */ |
| 1401 | com_node(c, CHILD(n, 3)); /* expr */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1402 | com_addbyte(c, GET_ITER); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1403 | c->c_begin = c->c_nexti; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1404 | com_addfwref(c, FOR_ITER, &anchor); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1405 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1406 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1407 | c->c_loops++; |
| 1408 | com_list_iter(c, n, e, t); |
| 1409 | c->c_loops--; |
| 1410 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 1411 | c->c_begin = save_begin; |
| 1412 | com_backpatch(c, anchor); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1413 | com_pop(c, 1); /* FOR_ITER has popped this */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | static void |
| 1417 | com_list_if(struct compiling *c, node *n, node *e, char *t) |
| 1418 | { |
| 1419 | int anchor = 0; |
| 1420 | int a = 0; |
| 1421 | /* list_iter: 'if' test [list_iter] */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1422 | com_node(c, CHILD(n, 1)); |
| 1423 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 1424 | com_addbyte(c, POP_TOP); |
| 1425 | com_pop(c, 1); |
| 1426 | com_list_iter(c, n, e, t); |
| 1427 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 1428 | com_backpatch(c, a); |
| 1429 | /* We jump here with an extra entry which we now pop */ |
| 1430 | com_addbyte(c, POP_TOP); |
| 1431 | com_backpatch(c, anchor); |
| 1432 | } |
| 1433 | |
| 1434 | static void |
| 1435 | com_list_iter(struct compiling *c, |
| 1436 | node *p, /* parent of list_iter node */ |
| 1437 | node *e, /* element expression node */ |
| 1438 | char *t /* name of result list temp local */) |
| 1439 | { |
| 1440 | /* list_iter is the last child in a listmaker, list_for, or list_if */ |
| 1441 | node *n = CHILD(p, NCH(p)-1); |
| 1442 | if (TYPE(n) == list_iter) { |
| 1443 | n = CHILD(n, 0); |
| 1444 | switch (TYPE(n)) { |
| 1445 | case list_for: |
| 1446 | com_list_for(c, n, e, t); |
| 1447 | break; |
| 1448 | case list_if: |
| 1449 | com_list_if(c, n, e, t); |
| 1450 | break; |
| 1451 | default: |
| 1452 | com_error(c, PyExc_SystemError, |
| 1453 | "invalid list_iter node type"); |
| 1454 | } |
| 1455 | } |
| 1456 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1457 | com_addop_varname(c, VAR_LOAD, t); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1458 | com_push(c, 1); |
| 1459 | com_node(c, e); |
| 1460 | com_addoparg(c, CALL_FUNCTION, 1); |
| 1461 | com_addbyte(c, POP_TOP); |
| 1462 | com_pop(c, 2); |
| 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | static void |
| 1467 | com_list_comprehension(struct compiling *c, node *n) |
| 1468 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1469 | /* listmaker: test list_for */ |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 1470 | char tmpname[30]; |
| 1471 | PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->c_tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1472 | com_addoparg(c, BUILD_LIST, 0); |
| 1473 | com_addbyte(c, DUP_TOP); /* leave the result on the stack */ |
| 1474 | com_push(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1475 | com_addop_name(c, LOAD_ATTR, "append"); |
| 1476 | com_addop_varname(c, VAR_STORE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1477 | com_pop(c, 1); |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1478 | com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1479 | com_addop_varname(c, VAR_DELETE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1480 | --c->c_tmpname; |
| 1481 | } |
| 1482 | |
| 1483 | static void |
| 1484 | com_listmaker(struct compiling *c, node *n) |
| 1485 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1486 | /* listmaker: test ( list_for | (',' test)* [','] ) */ |
| 1487 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1488 | com_list_comprehension(c, n); |
| 1489 | else { |
| 1490 | int len = 0; |
| 1491 | int i; |
| 1492 | for (i = 0; i < NCH(n); i += 2, len++) |
| 1493 | com_node(c, CHILD(n, i)); |
| 1494 | com_addoparg(c, BUILD_LIST, len); |
| 1495 | com_pop(c, len-1); |
| 1496 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1500 | com_dictmaker(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1501 | { |
| 1502 | int i; |
| 1503 | /* dictmaker: test ':' test (',' test ':' value)* [','] */ |
| 1504 | for (i = 0; i+2 < NCH(n); i += 4) { |
| 1505 | /* We must arrange things just right for STORE_SUBSCR. |
| 1506 | It wants the stack to look like (value) (dict) (key) */ |
| 1507 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1508 | com_push(c, 1); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1509 | com_node(c, CHILD(n, i+2)); /* value */ |
| 1510 | com_addbyte(c, ROT_TWO); |
| 1511 | com_node(c, CHILD(n, i)); /* key */ |
| 1512 | com_addbyte(c, STORE_SUBSCR); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1513 | com_pop(c, 3); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1518 | com_atom(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1519 | { |
| 1520 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1521 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1522 | int i; |
| 1523 | REQ(n, atom); |
| 1524 | ch = CHILD(n, 0); |
| 1525 | switch (TYPE(ch)) { |
| 1526 | case LPAR: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1527 | if (TYPE(CHILD(n, 1)) == RPAR) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1528 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1529 | com_push(c, 1); |
| 1530 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1531 | else |
| 1532 | com_node(c, CHILD(n, 1)); |
| 1533 | break; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1534 | case LSQB: /* '[' [listmaker] ']' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1535 | if (TYPE(CHILD(n, 1)) == RSQB) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1536 | com_addoparg(c, BUILD_LIST, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1537 | com_push(c, 1); |
| 1538 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1539 | else |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1540 | com_listmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1541 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1542 | case LBRACE: /* '{' [dictmaker] '}' */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1543 | com_addoparg(c, BUILD_MAP, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1544 | com_push(c, 1); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1545 | if (TYPE(CHILD(n, 1)) == dictmaker) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1546 | com_dictmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1547 | break; |
| 1548 | case BACKQUOTE: |
| 1549 | com_node(c, CHILD(n, 1)); |
| 1550 | com_addbyte(c, UNARY_CONVERT); |
| 1551 | break; |
| 1552 | case NUMBER: |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1553 | if ((v = parsenumber(c, STR(ch))) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1554 | i = 255; |
| 1555 | } |
| 1556 | else { |
| 1557 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1558 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1559 | } |
| 1560 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1561 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1562 | break; |
| 1563 | case STRING: |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1564 | v = parsestrplus(c, n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1565 | if (v == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1566 | c->c_errors++; |
| 1567 | i = 255; |
| 1568 | } |
| 1569 | else { |
| 1570 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1571 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1572 | } |
| 1573 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1574 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1575 | break; |
| 1576 | case NAME: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1577 | com_addop_varname(c, VAR_LOAD, STR(ch)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1578 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1579 | break; |
| 1580 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1581 | com_error(c, PyExc_SystemError, |
| 1582 | "com_atom: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1587 | com_slice(struct compiling *c, node *n, int op) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1588 | { |
| 1589 | if (NCH(n) == 1) { |
| 1590 | com_addbyte(c, op); |
| 1591 | } |
| 1592 | else if (NCH(n) == 2) { |
| 1593 | if (TYPE(CHILD(n, 0)) != COLON) { |
| 1594 | com_node(c, CHILD(n, 0)); |
| 1595 | com_addbyte(c, op+1); |
| 1596 | } |
| 1597 | else { |
| 1598 | com_node(c, CHILD(n, 1)); |
| 1599 | com_addbyte(c, op+2); |
| 1600 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1601 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1602 | } |
| 1603 | else { |
| 1604 | com_node(c, CHILD(n, 0)); |
| 1605 | com_node(c, CHILD(n, 2)); |
| 1606 | com_addbyte(c, op+3); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1607 | com_pop(c, 2); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1608 | } |
| 1609 | } |
| 1610 | |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1611 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1612 | com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn) |
| 1613 | { |
| 1614 | if (NCH(n) == 1) { |
| 1615 | com_addbyte(c, DUP_TOP); |
| 1616 | com_push(c, 1); |
| 1617 | com_addbyte(c, SLICE); |
| 1618 | com_node(c, augn); |
| 1619 | com_addbyte(c, opcode); |
| 1620 | com_pop(c, 1); |
| 1621 | com_addbyte(c, ROT_TWO); |
| 1622 | com_addbyte(c, STORE_SLICE); |
| 1623 | com_pop(c, 2); |
| 1624 | } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) { |
| 1625 | com_node(c, CHILD(n, 0)); |
| 1626 | com_addoparg(c, DUP_TOPX, 2); |
| 1627 | com_push(c, 2); |
| 1628 | com_addbyte(c, SLICE+1); |
| 1629 | com_pop(c, 1); |
| 1630 | com_node(c, augn); |
| 1631 | com_addbyte(c, opcode); |
| 1632 | com_pop(c, 1); |
| 1633 | com_addbyte(c, ROT_THREE); |
| 1634 | com_addbyte(c, STORE_SLICE+1); |
| 1635 | com_pop(c, 3); |
| 1636 | } else if (NCH(n) == 2) { |
| 1637 | com_node(c, CHILD(n, 1)); |
| 1638 | com_addoparg(c, DUP_TOPX, 2); |
| 1639 | com_push(c, 2); |
| 1640 | com_addbyte(c, SLICE+2); |
| 1641 | com_pop(c, 1); |
| 1642 | com_node(c, augn); |
| 1643 | com_addbyte(c, opcode); |
| 1644 | com_pop(c, 1); |
| 1645 | com_addbyte(c, ROT_THREE); |
| 1646 | com_addbyte(c, STORE_SLICE+2); |
| 1647 | com_pop(c, 3); |
| 1648 | } else { |
| 1649 | com_node(c, CHILD(n, 0)); |
| 1650 | com_node(c, CHILD(n, 2)); |
| 1651 | com_addoparg(c, DUP_TOPX, 3); |
| 1652 | com_push(c, 3); |
| 1653 | com_addbyte(c, SLICE+3); |
| 1654 | com_pop(c, 2); |
| 1655 | com_node(c, augn); |
| 1656 | com_addbyte(c, opcode); |
| 1657 | com_pop(c, 1); |
| 1658 | com_addbyte(c, ROT_FOUR); |
| 1659 | com_addbyte(c, STORE_SLICE+3); |
| 1660 | com_pop(c, 4); |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1665 | com_argument(struct compiling *c, node *n, PyObject **pkeywords) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1666 | { |
| 1667 | node *m; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1668 | REQ(n, argument); /* [test '='] test; really [keyword '='] test */ |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1669 | if (NCH(n) == 1) { |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1670 | if (*pkeywords != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1671 | com_error(c, PyExc_SyntaxError, |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 1672 | "non-keyword arg after keyword arg"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1673 | } |
| 1674 | else { |
| 1675 | com_node(c, CHILD(n, 0)); |
| 1676 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1677 | return; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1678 | } |
| 1679 | m = n; |
| 1680 | do { |
| 1681 | m = CHILD(m, 0); |
| 1682 | } while (NCH(m) == 1); |
| 1683 | if (TYPE(m) != NAME) { |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 1684 | /* f(lambda x: x[0] = 3) ends up getting parsed with |
| 1685 | * LHS test = lambda x: x[0], and RHS test = 3. |
| 1686 | * SF bug 132313 points out that complaining about a keyword |
| 1687 | * then is very confusing. |
| 1688 | */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1689 | com_error(c, PyExc_SyntaxError, |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 1690 | TYPE(m) == lambdef ? |
| 1691 | "lambda cannot contain assignment" : |
| 1692 | "keyword can't be an expression"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1693 | } |
| 1694 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1695 | PyObject *v = PyString_InternFromString(STR(m)); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1696 | if (v != NULL && *pkeywords == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1697 | *pkeywords = PyDict_New(); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 1698 | if (v == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1699 | c->c_errors++; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 1700 | else if (*pkeywords == NULL) { |
| 1701 | c->c_errors++; |
| 1702 | Py_DECREF(v); |
| 1703 | } else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1704 | if (PyDict_GetItem(*pkeywords, v) != NULL) |
| 1705 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1706 | "duplicate keyword argument"); |
| 1707 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1708 | if (PyDict_SetItem(*pkeywords, v, v) != 0) |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1709 | c->c_errors++; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1710 | com_addoparg(c, LOAD_CONST, com_addconst(c, v)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1711 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1712 | Py_DECREF(v); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1713 | } |
| 1714 | } |
| 1715 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1718 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1719 | com_call_function(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1720 | { |
| 1721 | if (TYPE(n) == RPAR) { |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1722 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1723 | } |
| 1724 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1725 | PyObject *keywords = NULL; |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1726 | int i, na, nk; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1727 | int lineno = n->n_lineno; |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1728 | int star_flag = 0; |
| 1729 | int starstar_flag = 0; |
| 1730 | int opcode; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1731 | REQ(n, arglist); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1732 | na = 0; |
| 1733 | nk = 0; |
| 1734 | for (i = 0; i < NCH(n); i += 2) { |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1735 | node *ch = CHILD(n, i); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1736 | if (TYPE(ch) == STAR || |
| 1737 | TYPE(ch) == DOUBLESTAR) |
| 1738 | break; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1739 | if (ch->n_lineno != lineno) { |
| 1740 | lineno = ch->n_lineno; |
| 1741 | com_addoparg(c, SET_LINENO, lineno); |
| 1742 | } |
| 1743 | com_argument(c, ch, &keywords); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1744 | if (keywords == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1745 | na++; |
| 1746 | else |
| 1747 | nk++; |
| 1748 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1749 | Py_XDECREF(keywords); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1750 | while (i < NCH(n)) { |
| 1751 | node *tok = CHILD(n, i); |
| 1752 | node *ch = CHILD(n, i+1); |
| 1753 | i += 3; |
| 1754 | switch (TYPE(tok)) { |
| 1755 | case STAR: star_flag = 1; break; |
| 1756 | case DOUBLESTAR: starstar_flag = 1; break; |
| 1757 | } |
| 1758 | com_node(c, ch); |
| 1759 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1760 | if (na > 255 || nk > 255) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1761 | com_error(c, PyExc_SyntaxError, |
| 1762 | "more than 255 arguments"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1763 | } |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1764 | if (star_flag || starstar_flag) |
Jeremy Hylton | e4fb958 | 2000-03-29 00:10:44 +0000 | [diff] [blame] | 1765 | opcode = CALL_FUNCTION_VAR - 1 + |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1766 | star_flag + (starstar_flag << 1); |
| 1767 | else |
| 1768 | opcode = CALL_FUNCTION; |
| 1769 | com_addoparg(c, opcode, na | (nk << 8)); |
| 1770 | com_pop(c, na + 2*nk + star_flag + starstar_flag); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1775 | com_select_member(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1776 | { |
| 1777 | com_addopname(c, LOAD_ATTR, n); |
| 1778 | } |
| 1779 | |
| 1780 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1781 | com_sliceobj(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1782 | { |
| 1783 | int i=0; |
| 1784 | int ns=2; /* number of slice arguments */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1785 | node *ch; |
| 1786 | |
| 1787 | /* first argument */ |
| 1788 | if (TYPE(CHILD(n,i)) == COLON) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1789 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1790 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1791 | i++; |
| 1792 | } |
| 1793 | else { |
| 1794 | com_node(c, CHILD(n,i)); |
| 1795 | i++; |
| 1796 | REQ(CHILD(n,i),COLON); |
| 1797 | i++; |
| 1798 | } |
| 1799 | /* second argument */ |
| 1800 | if (i < NCH(n) && TYPE(CHILD(n,i)) == test) { |
| 1801 | com_node(c, CHILD(n,i)); |
| 1802 | i++; |
| 1803 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1804 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1805 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1806 | com_push(c, 1); |
| 1807 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1808 | /* remaining arguments */ |
| 1809 | for (; i < NCH(n); i++) { |
| 1810 | ns++; |
| 1811 | ch=CHILD(n,i); |
| 1812 | REQ(ch, sliceop); |
| 1813 | if (NCH(ch) == 1) { |
| 1814 | /* right argument of ':' missing */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1815 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1816 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1817 | } |
| 1818 | else |
| 1819 | com_node(c, CHILD(ch,1)); |
| 1820 | } |
| 1821 | com_addoparg(c, BUILD_SLICE, ns); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1822 | com_pop(c, 1 + (ns == 3)); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1823 | } |
| 1824 | |
| 1825 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1826 | com_subscript(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1827 | { |
| 1828 | node *ch; |
| 1829 | REQ(n, subscript); |
| 1830 | ch = CHILD(n,0); |
| 1831 | /* check for rubber index */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1832 | if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) { |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 1833 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1834 | com_push(c, 1); |
| 1835 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1836 | else { |
| 1837 | /* check for slice */ |
| 1838 | if ((TYPE(ch) == COLON || NCH(n) > 1)) |
| 1839 | com_sliceobj(c, n); |
| 1840 | else { |
| 1841 | REQ(ch, test); |
| 1842 | com_node(c, ch); |
| 1843 | } |
| 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1848 | com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1849 | { |
| 1850 | int i, op; |
| 1851 | REQ(n, subscriptlist); |
| 1852 | /* Check to make backward compatible slice behavior for '[i:j]' */ |
| 1853 | if (NCH(n) == 1) { |
| 1854 | node *sub = CHILD(n, 0); /* subscript */ |
Thomas Wouters | e8643c4 | 2000-08-05 21:37:50 +0000 | [diff] [blame] | 1855 | /* 'Basic' slice, should have exactly one colon. */ |
| 1856 | if ((TYPE(CHILD(sub, 0)) == COLON |
| 1857 | || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON)) |
| 1858 | && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop)) |
| 1859 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1860 | switch (assigning) { |
| 1861 | case OP_DELETE: |
| 1862 | op = DELETE_SLICE; |
| 1863 | break; |
| 1864 | case OP_ASSIGN: |
| 1865 | op = STORE_SLICE; |
| 1866 | break; |
| 1867 | case OP_APPLY: |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1868 | op = SLICE; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1869 | break; |
| 1870 | default: |
| 1871 | com_augassign_slice(c, sub, assigning, augn); |
| 1872 | return; |
| 1873 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1874 | com_slice(c, sub, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1875 | if (op == STORE_SLICE) |
| 1876 | com_pop(c, 2); |
| 1877 | else if (op == DELETE_SLICE) |
| 1878 | com_pop(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1879 | return; |
| 1880 | } |
| 1881 | } |
| 1882 | /* Else normal subscriptlist. Compile each subscript. */ |
| 1883 | for (i = 0; i < NCH(n); i += 2) |
| 1884 | com_subscript(c, CHILD(n, i)); |
| 1885 | /* Put multiple subscripts into a tuple */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1886 | if (NCH(n) > 1) { |
| 1887 | i = (NCH(n)+1) / 2; |
| 1888 | com_addoparg(c, BUILD_TUPLE, i); |
| 1889 | com_pop(c, i-1); |
| 1890 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1891 | switch (assigning) { |
| 1892 | case OP_DELETE: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1893 | op = DELETE_SUBSCR; |
| 1894 | i = 2; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1895 | break; |
| 1896 | default: |
| 1897 | case OP_ASSIGN: |
| 1898 | op = STORE_SUBSCR; |
| 1899 | i = 3; |
| 1900 | break; |
| 1901 | case OP_APPLY: |
| 1902 | op = BINARY_SUBSCR; |
| 1903 | i = 1; |
| 1904 | break; |
| 1905 | } |
| 1906 | if (assigning > OP_APPLY) { |
| 1907 | com_addoparg(c, DUP_TOPX, 2); |
| 1908 | com_push(c, 2); |
| 1909 | com_addbyte(c, BINARY_SUBSCR); |
| 1910 | com_pop(c, 1); |
| 1911 | com_node(c, augn); |
| 1912 | com_addbyte(c, assigning); |
| 1913 | com_pop(c, 1); |
| 1914 | com_addbyte(c, ROT_THREE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1915 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1916 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1917 | com_pop(c, i); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1921 | com_apply_trailer(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1922 | { |
| 1923 | REQ(n, trailer); |
| 1924 | switch (TYPE(CHILD(n, 0))) { |
| 1925 | case LPAR: |
| 1926 | com_call_function(c, CHILD(n, 1)); |
| 1927 | break; |
| 1928 | case DOT: |
| 1929 | com_select_member(c, CHILD(n, 1)); |
| 1930 | break; |
| 1931 | case LSQB: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1932 | com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1933 | break; |
| 1934 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1935 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1936 | "com_apply_trailer: unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1937 | } |
| 1938 | } |
| 1939 | |
| 1940 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1941 | com_power(struct compiling *c, node *n) |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1942 | { |
| 1943 | int i; |
| 1944 | REQ(n, power); |
| 1945 | com_atom(c, CHILD(n, 0)); |
| 1946 | for (i = 1; i < NCH(n); i++) { |
| 1947 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
| 1948 | com_factor(c, CHILD(n, i+1)); |
| 1949 | com_addbyte(c, BINARY_POWER); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1950 | com_pop(c, 1); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1951 | break; |
| 1952 | } |
| 1953 | else |
| 1954 | com_apply_trailer(c, CHILD(n, i)); |
| 1955 | } |
| 1956 | } |
| 1957 | |
| 1958 | static void |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1959 | com_invert_constant(struct compiling *c, node *n) |
| 1960 | { |
| 1961 | /* Compute the inverse of int and longs and use them directly, |
| 1962 | but be prepared to generate code for all other |
| 1963 | possibilities (invalid numbers, floats, complex). |
| 1964 | */ |
| 1965 | PyObject *num, *inv = NULL; |
| 1966 | int i; |
| 1967 | |
| 1968 | REQ(n, NUMBER); |
| 1969 | num = parsenumber(c, STR(n)); |
| 1970 | if (num == NULL) |
| 1971 | i = 255; |
| 1972 | else { |
| 1973 | inv = PyNumber_Invert(num); |
| 1974 | if (inv == NULL) { |
| 1975 | PyErr_Clear(); |
| 1976 | i = com_addconst(c, num); |
| 1977 | } else { |
| 1978 | i = com_addconst(c, inv); |
| 1979 | Py_DECREF(inv); |
| 1980 | } |
| 1981 | Py_DECREF(num); |
| 1982 | } |
| 1983 | com_addoparg(c, LOAD_CONST, i); |
| 1984 | com_push(c, 1); |
| 1985 | if (num != NULL && inv == NULL) |
| 1986 | com_addbyte(c, UNARY_INVERT); |
| 1987 | } |
| 1988 | |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 1989 | static int |
| 1990 | is_float_zero(const char *p) |
| 1991 | { |
| 1992 | int found_radix_point = 0; |
| 1993 | int ch; |
| 1994 | while ((ch = Py_CHARMASK(*p++)) != '\0') { |
| 1995 | switch (ch) { |
| 1996 | case '0': |
| 1997 | /* no reason to believe it's not 0 -- continue */ |
| 1998 | break; |
| 1999 | |
| 2000 | case 'e': case 'E': case 'j': case 'J': |
| 2001 | /* If this was a hex constant, we already would have |
| 2002 | returned 0 due to the 'x' or 'X', so 'e' or 'E' |
| 2003 | must be an exponent marker, and we haven't yet |
| 2004 | seen a non-zero digit, and it doesn't matter what |
| 2005 | the exponent is then. For 'j' or 'J' similarly, |
| 2006 | except that this is an imaginary 0 then. */ |
| 2007 | return 1; |
| 2008 | |
| 2009 | case '.': |
| 2010 | found_radix_point = 1; |
| 2011 | break; |
| 2012 | |
| 2013 | default: |
| 2014 | return 0; |
| 2015 | } |
| 2016 | } |
| 2017 | return found_radix_point; |
| 2018 | } |
| 2019 | |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2020 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2021 | com_factor(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2022 | { |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2023 | int childtype = TYPE(CHILD(n, 0)); |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2024 | node *pfactor, *ppower, *patom, *pnum; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2025 | REQ(n, factor); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2026 | /* If the unary +, -, or ~ operator is applied to a constant, |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2027 | don't generate a UNARY_xxx opcode. Just store the |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2028 | approriate value as a constant. If the value is negative, |
| 2029 | extend the string containing the constant and insert a |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2030 | negative in the 0th position -- unless we're doing unary minus |
| 2031 | of a floating zero! In that case the sign is significant, but |
| 2032 | the const dict can't distinguish +0.0 from -0.0. |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2033 | */ |
| 2034 | if ((childtype == PLUS || childtype == MINUS || childtype == TILDE) |
Fred Drake | 14ef244 | 2001-08-30 18:53:25 +0000 | [diff] [blame] | 2035 | && NCH(n) == 2 |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2036 | && TYPE((pfactor = CHILD(n, 1))) == factor |
| 2037 | && NCH(pfactor) == 1 |
| 2038 | && TYPE((ppower = CHILD(pfactor, 0))) == power |
| 2039 | && NCH(ppower) == 1 |
| 2040 | && TYPE((patom = CHILD(ppower, 0))) == atom |
| 2041 | && TYPE((pnum = CHILD(patom, 0))) == NUMBER |
| 2042 | && !(childtype == MINUS && is_float_zero(STR(pnum)))) { |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2043 | if (childtype == TILDE) { |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2044 | com_invert_constant(c, pnum); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2045 | return; |
| 2046 | } |
| 2047 | if (childtype == MINUS) { |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 2048 | char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2049 | if (s == NULL) { |
| 2050 | com_error(c, PyExc_MemoryError, ""); |
| 2051 | com_addbyte(c, 255); |
| 2052 | return; |
| 2053 | } |
| 2054 | s[0] = '-'; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2055 | strcpy(s + 1, STR(pnum)); |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 2056 | PyObject_FREE(STR(pnum)); |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2057 | STR(pnum) = s; |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2058 | } |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2059 | com_atom(c, patom); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2060 | } |
| 2061 | else if (childtype == PLUS) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2062 | com_factor(c, CHILD(n, 1)); |
| 2063 | com_addbyte(c, UNARY_POSITIVE); |
| 2064 | } |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2065 | else if (childtype == MINUS) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2066 | com_factor(c, CHILD(n, 1)); |
| 2067 | com_addbyte(c, UNARY_NEGATIVE); |
| 2068 | } |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2069 | else if (childtype == TILDE) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2070 | com_factor(c, CHILD(n, 1)); |
| 2071 | com_addbyte(c, UNARY_INVERT); |
| 2072 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2073 | else { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2074 | com_power(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2075 | } |
| 2076 | } |
| 2077 | |
| 2078 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2079 | com_term(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2080 | { |
| 2081 | int i; |
| 2082 | int op; |
| 2083 | REQ(n, term); |
| 2084 | com_factor(c, CHILD(n, 0)); |
| 2085 | for (i = 2; i < NCH(n); i += 2) { |
| 2086 | com_factor(c, CHILD(n, i)); |
| 2087 | switch (TYPE(CHILD(n, i-1))) { |
| 2088 | case STAR: |
| 2089 | op = BINARY_MULTIPLY; |
| 2090 | break; |
| 2091 | case SLASH: |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2092 | if (c->c_flags & CO_FUTURE_DIVISION) |
| 2093 | op = BINARY_TRUE_DIVIDE; |
| 2094 | else |
| 2095 | op = BINARY_DIVIDE; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2096 | break; |
| 2097 | case PERCENT: |
| 2098 | op = BINARY_MODULO; |
| 2099 | break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2100 | case DOUBLESLASH: |
| 2101 | op = BINARY_FLOOR_DIVIDE; |
| 2102 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2103 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2104 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2105 | "com_term: operator not *, /, // or %"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2106 | op = 255; |
| 2107 | } |
| 2108 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2109 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2110 | } |
| 2111 | } |
| 2112 | |
| 2113 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2114 | com_arith_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2115 | { |
| 2116 | int i; |
| 2117 | int op; |
| 2118 | REQ(n, arith_expr); |
| 2119 | com_term(c, CHILD(n, 0)); |
| 2120 | for (i = 2; i < NCH(n); i += 2) { |
| 2121 | com_term(c, CHILD(n, i)); |
| 2122 | switch (TYPE(CHILD(n, i-1))) { |
| 2123 | case PLUS: |
| 2124 | op = BINARY_ADD; |
| 2125 | break; |
| 2126 | case MINUS: |
| 2127 | op = BINARY_SUBTRACT; |
| 2128 | break; |
| 2129 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2130 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2131 | "com_arith_expr: operator not + or -"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2132 | op = 255; |
| 2133 | } |
| 2134 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2135 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2136 | } |
| 2137 | } |
| 2138 | |
| 2139 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2140 | com_shift_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2141 | { |
| 2142 | int i; |
| 2143 | int op; |
| 2144 | REQ(n, shift_expr); |
| 2145 | com_arith_expr(c, CHILD(n, 0)); |
| 2146 | for (i = 2; i < NCH(n); i += 2) { |
| 2147 | com_arith_expr(c, CHILD(n, i)); |
| 2148 | switch (TYPE(CHILD(n, i-1))) { |
| 2149 | case LEFTSHIFT: |
| 2150 | op = BINARY_LSHIFT; |
| 2151 | break; |
| 2152 | case RIGHTSHIFT: |
| 2153 | op = BINARY_RSHIFT; |
| 2154 | break; |
| 2155 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2156 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2157 | "com_shift_expr: operator not << or >>"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2158 | op = 255; |
| 2159 | } |
| 2160 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2161 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2162 | } |
| 2163 | } |
| 2164 | |
| 2165 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2166 | com_and_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2167 | { |
| 2168 | int i; |
| 2169 | int op; |
| 2170 | REQ(n, and_expr); |
| 2171 | com_shift_expr(c, CHILD(n, 0)); |
| 2172 | for (i = 2; i < NCH(n); i += 2) { |
| 2173 | com_shift_expr(c, CHILD(n, i)); |
| 2174 | if (TYPE(CHILD(n, i-1)) == AMPER) { |
| 2175 | op = BINARY_AND; |
| 2176 | } |
| 2177 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2178 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2179 | "com_and_expr: operator not &"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2180 | op = 255; |
| 2181 | } |
| 2182 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2183 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2188 | com_xor_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2189 | { |
| 2190 | int i; |
| 2191 | int op; |
| 2192 | REQ(n, xor_expr); |
| 2193 | com_and_expr(c, CHILD(n, 0)); |
| 2194 | for (i = 2; i < NCH(n); i += 2) { |
| 2195 | com_and_expr(c, CHILD(n, i)); |
| 2196 | if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) { |
| 2197 | op = BINARY_XOR; |
| 2198 | } |
| 2199 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2200 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2201 | "com_xor_expr: operator not ^"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2202 | op = 255; |
| 2203 | } |
| 2204 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2205 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2206 | } |
| 2207 | } |
| 2208 | |
| 2209 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2210 | com_expr(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2211 | { |
| 2212 | int i; |
| 2213 | int op; |
| 2214 | REQ(n, expr); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2215 | com_xor_expr(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2216 | for (i = 2; i < NCH(n); i += 2) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2217 | com_xor_expr(c, CHILD(n, i)); |
| 2218 | if (TYPE(CHILD(n, i-1)) == VBAR) { |
| 2219 | op = BINARY_OR; |
| 2220 | } |
| 2221 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2222 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2223 | "com_expr: expr operator not |"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2224 | op = 255; |
| 2225 | } |
| 2226 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2227 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2228 | } |
| 2229 | } |
| 2230 | |
| 2231 | static enum cmp_op |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2232 | cmp_type(node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2233 | { |
| 2234 | REQ(n, comp_op); |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2235 | /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '==' |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2236 | | 'in' | 'not' 'in' | 'is' | 'is' not' */ |
| 2237 | if (NCH(n) == 1) { |
| 2238 | n = CHILD(n, 0); |
| 2239 | switch (TYPE(n)) { |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2240 | case LESS: return PyCmp_LT; |
| 2241 | case GREATER: return PyCmp_GT; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2242 | case EQEQUAL: /* == */ |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2243 | case EQUAL: return PyCmp_EQ; |
| 2244 | case LESSEQUAL: return PyCmp_LE; |
| 2245 | case GREATEREQUAL: return PyCmp_GE; |
| 2246 | case NOTEQUAL: return PyCmp_NE; /* <> or != */ |
| 2247 | case NAME: if (strcmp(STR(n), "in") == 0) return PyCmp_IN; |
| 2248 | if (strcmp(STR(n), "is") == 0) return PyCmp_IS; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2249 | } |
| 2250 | } |
| 2251 | else if (NCH(n) == 2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2252 | switch (TYPE(CHILD(n, 0))) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2253 | case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2254 | return PyCmp_NOT_IN; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2255 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2256 | return PyCmp_IS_NOT; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2257 | } |
| 2258 | } |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2259 | return PyCmp_BAD; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2263 | com_comparison(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2264 | { |
| 2265 | int i; |
| 2266 | enum cmp_op op; |
| 2267 | int anchor; |
| 2268 | REQ(n, comparison); /* comparison: expr (comp_op expr)* */ |
| 2269 | com_expr(c, CHILD(n, 0)); |
| 2270 | if (NCH(n) == 1) |
| 2271 | return; |
| 2272 | |
| 2273 | /**************************************************************** |
| 2274 | The following code is generated for all but the last |
| 2275 | comparison in a chain: |
| 2276 | |
| 2277 | label: on stack: opcode: jump to: |
| 2278 | |
| 2279 | a <code to load b> |
| 2280 | a, b DUP_TOP |
| 2281 | a, b, b ROT_THREE |
| 2282 | b, a, b COMPARE_OP |
| 2283 | b, 0-or-1 JUMP_IF_FALSE L1 |
| 2284 | b, 1 POP_TOP |
| 2285 | b |
| 2286 | |
| 2287 | We are now ready to repeat this sequence for the next |
| 2288 | comparison in the chain. |
| 2289 | |
| 2290 | For the last we generate: |
| 2291 | |
| 2292 | b <code to load c> |
| 2293 | b, c COMPARE_OP |
| 2294 | 0-or-1 |
| 2295 | |
| 2296 | If there were any jumps to L1 (i.e., there was more than one |
| 2297 | comparison), we generate: |
| 2298 | |
| 2299 | 0-or-1 JUMP_FORWARD L2 |
| 2300 | L1: b, 0 ROT_TWO |
| 2301 | 0, b POP_TOP |
| 2302 | 0 |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2303 | L2: 0-or-1 |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2304 | ****************************************************************/ |
| 2305 | |
| 2306 | anchor = 0; |
| 2307 | |
| 2308 | for (i = 2; i < NCH(n); i += 2) { |
| 2309 | com_expr(c, CHILD(n, i)); |
| 2310 | if (i+2 < NCH(n)) { |
| 2311 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2312 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2313 | com_addbyte(c, ROT_THREE); |
| 2314 | } |
| 2315 | op = cmp_type(CHILD(n, i-1)); |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2316 | if (op == PyCmp_BAD) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2317 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2318 | "com_comparison: unknown comparison op"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2319 | } |
| 2320 | com_addoparg(c, COMPARE_OP, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2321 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2322 | if (i+2 < NCH(n)) { |
| 2323 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2324 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2325 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2326 | } |
| 2327 | } |
| 2328 | |
| 2329 | if (anchor) { |
| 2330 | int anchor2 = 0; |
| 2331 | com_addfwref(c, JUMP_FORWARD, &anchor2); |
| 2332 | com_backpatch(c, anchor); |
| 2333 | com_addbyte(c, ROT_TWO); |
| 2334 | com_addbyte(c, POP_TOP); |
| 2335 | com_backpatch(c, anchor2); |
| 2336 | } |
| 2337 | } |
| 2338 | |
| 2339 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2340 | com_not_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2341 | { |
| 2342 | REQ(n, not_test); /* 'not' not_test | comparison */ |
| 2343 | if (NCH(n) == 1) { |
| 2344 | com_comparison(c, CHILD(n, 0)); |
| 2345 | } |
| 2346 | else { |
| 2347 | com_not_test(c, CHILD(n, 1)); |
| 2348 | com_addbyte(c, UNARY_NOT); |
| 2349 | } |
| 2350 | } |
| 2351 | |
| 2352 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2353 | com_and_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2354 | { |
| 2355 | int i; |
| 2356 | int anchor; |
| 2357 | REQ(n, and_test); /* not_test ('and' not_test)* */ |
| 2358 | anchor = 0; |
| 2359 | i = 0; |
| 2360 | for (;;) { |
| 2361 | com_not_test(c, CHILD(n, i)); |
| 2362 | if ((i += 2) >= NCH(n)) |
| 2363 | break; |
| 2364 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2365 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2366 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2367 | } |
| 2368 | if (anchor) |
| 2369 | com_backpatch(c, anchor); |
| 2370 | } |
| 2371 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2372 | static int |
| 2373 | com_make_closure(struct compiling *c, PyCodeObject *co) |
| 2374 | { |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 2375 | int i, free = PyCode_GetNumFree(co); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2376 | if (free == 0) |
| 2377 | return 0; |
| 2378 | for (i = 0; i < free; ++i) { |
| 2379 | /* Bypass com_addop_varname because it will generate |
| 2380 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 2381 | */ |
| 2382 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 2383 | int arg, reftype; |
| 2384 | |
| 2385 | /* Special case: If a class contains a method with a |
| 2386 | free variable that has the same name as a method, |
| 2387 | the name will be considered free *and* local in the |
| 2388 | class. It should be handled by the closure, as |
| 2389 | well as by the normal name loookup logic. |
| 2390 | */ |
| 2391 | reftype = get_ref_type(c, PyString_AS_STRING(name)); |
| 2392 | if (reftype == CELL) |
| 2393 | arg = com_lookup_arg(c->c_cellvars, name); |
| 2394 | else /* (reftype == FREE) */ |
| 2395 | arg = com_lookup_arg(c->c_freevars, name); |
| 2396 | if (arg == -1) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 2397 | fprintf(stderr, "lookup %s in %s %d %d\n" |
| 2398 | "freevars of %s: %s\n", |
| 2399 | PyObject_REPR(name), |
| 2400 | c->c_name, |
| 2401 | reftype, arg, |
| 2402 | PyString_AS_STRING(co->co_name), |
| 2403 | PyObject_REPR(co->co_freevars)); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2404 | Py_FatalError("com_make_closure()"); |
| 2405 | } |
| 2406 | com_addoparg(c, LOAD_CLOSURE, arg); |
| 2407 | |
| 2408 | } |
| 2409 | com_push(c, free); |
| 2410 | return 1; |
| 2411 | } |
| 2412 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2413 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2414 | com_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2415 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2416 | REQ(n, test); /* and_test ('or' and_test)* | lambdef */ |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2417 | if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2418 | PyCodeObject *co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2419 | int i, closure; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2420 | int ndefs = com_argdefs(c, CHILD(n, 0)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 2421 | symtable_enter_scope(c->c_symtable, "lambda", lambdef, |
| 2422 | n->n_lineno); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2423 | co = icompile(CHILD(n, 0), c); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2424 | if (co == NULL) { |
| 2425 | c->c_errors++; |
| 2426 | return; |
| 2427 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2428 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2429 | i = com_addconst(c, (PyObject *)co); |
| 2430 | closure = com_make_closure(c, co); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2431 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2432 | com_push(c, 1); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2433 | if (closure) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2434 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 2435 | com_pop(c, PyCode_GetNumFree(co)); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2436 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2437 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2438 | Py_DECREF(co); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2439 | com_pop(c, ndefs); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2440 | } |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2441 | else { |
| 2442 | int anchor = 0; |
| 2443 | int i = 0; |
| 2444 | for (;;) { |
| 2445 | com_and_test(c, CHILD(n, i)); |
| 2446 | if ((i += 2) >= NCH(n)) |
| 2447 | break; |
| 2448 | com_addfwref(c, JUMP_IF_TRUE, &anchor); |
| 2449 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2450 | com_pop(c, 1); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2451 | } |
| 2452 | if (anchor) |
| 2453 | com_backpatch(c, anchor); |
| 2454 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2455 | } |
| 2456 | |
| 2457 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2458 | com_list(struct compiling *c, node *n, int toplevel) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2459 | { |
| 2460 | /* exprlist: expr (',' expr)* [',']; likewise for testlist */ |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 2461 | if (NCH(n) == 1 && !toplevel) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2462 | com_node(c, CHILD(n, 0)); |
| 2463 | } |
| 2464 | else { |
| 2465 | int i; |
| 2466 | int len; |
| 2467 | len = (NCH(n) + 1) / 2; |
| 2468 | for (i = 0; i < NCH(n); i += 2) |
| 2469 | com_node(c, CHILD(n, i)); |
| 2470 | com_addoparg(c, BUILD_TUPLE, len); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2471 | com_pop(c, len-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2472 | } |
| 2473 | } |
| 2474 | |
| 2475 | |
| 2476 | /* Begin of assignment compilation */ |
| 2477 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2478 | |
| 2479 | static void |
| 2480 | com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn) |
| 2481 | { |
| 2482 | com_addbyte(c, DUP_TOP); |
| 2483 | com_push(c, 1); |
| 2484 | com_addopname(c, LOAD_ATTR, n); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2485 | com_node(c, augn); |
| 2486 | com_addbyte(c, opcode); |
| 2487 | com_pop(c, 1); |
| 2488 | com_addbyte(c, ROT_TWO); |
| 2489 | com_addopname(c, STORE_ATTR, n); |
| 2490 | com_pop(c, 2); |
| 2491 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2492 | |
| 2493 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2494 | com_assign_attr(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2495 | { |
| 2496 | com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2497 | com_pop(c, assigning ? 2 : 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2498 | } |
| 2499 | |
| 2500 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2501 | com_assign_trailer(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2502 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2503 | REQ(n, trailer); |
| 2504 | switch (TYPE(CHILD(n, 0))) { |
| 2505 | case LPAR: /* '(' [exprlist] ')' */ |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 2506 | if (assigning == OP_DELETE) |
| 2507 | com_error(c, PyExc_SyntaxError, |
| 2508 | "can't delete function call"); |
| 2509 | else |
| 2510 | com_error(c, PyExc_SyntaxError, |
| 2511 | "can't assign to function call"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2512 | break; |
| 2513 | case DOT: /* '.' NAME */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2514 | if (assigning > OP_APPLY) |
| 2515 | com_augassign_attr(c, CHILD(n, 1), assigning, augn); |
| 2516 | else |
| 2517 | com_assign_attr(c, CHILD(n, 1), assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2518 | break; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2519 | case LSQB: /* '[' subscriptlist ']' */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2520 | com_subscriptlist(c, CHILD(n, 1), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2521 | break; |
| 2522 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2523 | com_error(c, PyExc_SystemError, "unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2524 | } |
| 2525 | } |
| 2526 | |
| 2527 | static void |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2528 | com_assign_sequence(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2529 | { |
| 2530 | int i; |
Thomas Wouters | b59290f | 2000-08-25 05:41:11 +0000 | [diff] [blame] | 2531 | if (TYPE(n) != testlist && TYPE(n) != listmaker) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2532 | REQ(n, exprlist); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2533 | if (assigning) { |
| 2534 | i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2535 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2536 | com_push(c, i-1); |
| 2537 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2538 | for (i = 0; i < NCH(n); i += 2) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2539 | com_assign(c, CHILD(n, i), assigning, NULL); |
| 2540 | } |
| 2541 | |
| 2542 | static void |
| 2543 | com_augassign_name(struct compiling *c, node *n, int opcode, node *augn) |
| 2544 | { |
| 2545 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2546 | com_addop_varname(c, VAR_LOAD, STR(n)); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2547 | com_push(c, 1); |
| 2548 | com_node(c, augn); |
| 2549 | com_addbyte(c, opcode); |
| 2550 | com_pop(c, 1); |
| 2551 | com_assign_name(c, n, OP_ASSIGN); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2552 | } |
| 2553 | |
| 2554 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2555 | com_assign_name(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2556 | { |
| 2557 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2558 | com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2559 | if (assigning) |
| 2560 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
| 2563 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2564 | com_assign(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2565 | { |
| 2566 | /* Loop to avoid trivial recursion */ |
| 2567 | for (;;) { |
| 2568 | switch (TYPE(n)) { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2569 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2570 | case exprlist: |
| 2571 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 2572 | case testlist1: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2573 | if (NCH(n) > 1) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2574 | if (assigning > OP_APPLY) { |
| 2575 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 2576 | "augmented assign to tuple not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2577 | return; |
| 2578 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2579 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2580 | return; |
| 2581 | } |
| 2582 | n = CHILD(n, 0); |
| 2583 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2584 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2585 | case test: |
| 2586 | case and_test: |
| 2587 | case not_test: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2588 | case comparison: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2589 | case expr: |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2590 | case xor_expr: |
| 2591 | case and_expr: |
| 2592 | case shift_expr: |
| 2593 | case arith_expr: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2594 | case term: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2595 | case factor: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2596 | if (NCH(n) > 1) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2597 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2598 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2599 | return; |
| 2600 | } |
| 2601 | n = CHILD(n, 0); |
| 2602 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2603 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2604 | case power: /* atom trailer* ('**' power)* |
| 2605 | ('+'|'-'|'~') factor | atom trailer* */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2606 | if (TYPE(CHILD(n, 0)) != atom) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2607 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2608 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2609 | return; |
| 2610 | } |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2611 | if (NCH(n) > 1) { /* trailer or exponent present */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2612 | int i; |
| 2613 | com_node(c, CHILD(n, 0)); |
| 2614 | for (i = 1; i+1 < NCH(n); i++) { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2615 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2616 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2617 | "can't assign to operator"); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2618 | return; |
| 2619 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2620 | com_apply_trailer(c, CHILD(n, i)); |
| 2621 | } /* NB i is still alive */ |
| 2622 | com_assign_trailer(c, |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2623 | CHILD(n, i), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2624 | return; |
| 2625 | } |
| 2626 | n = CHILD(n, 0); |
| 2627 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2628 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2629 | case atom: |
| 2630 | switch (TYPE(CHILD(n, 0))) { |
| 2631 | case LPAR: |
| 2632 | n = CHILD(n, 1); |
| 2633 | if (TYPE(n) == RPAR) { |
| 2634 | /* XXX Should allow () = () ??? */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2635 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2636 | "can't assign to ()"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2637 | return; |
| 2638 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2639 | if (assigning > OP_APPLY) { |
| 2640 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2641 | "augmented assign to tuple not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2642 | return; |
| 2643 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2644 | break; |
| 2645 | case LSQB: |
| 2646 | n = CHILD(n, 1); |
| 2647 | if (TYPE(n) == RSQB) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2648 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2649 | "can't assign to []"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2650 | return; |
| 2651 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2652 | if (assigning > OP_APPLY) { |
| 2653 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2654 | "augmented assign to list not possible"); |
| 2655 | return; |
| 2656 | } |
| 2657 | if (NCH(n) > 1 |
| 2658 | && TYPE(CHILD(n, 1)) == list_for) { |
| 2659 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2660 | "can't assign to list comprehension"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2661 | return; |
| 2662 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2663 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2664 | return; |
| 2665 | case NAME: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2666 | if (assigning > OP_APPLY) |
| 2667 | com_augassign_name(c, CHILD(n, 0), |
| 2668 | assigning, augn); |
| 2669 | else |
| 2670 | com_assign_name(c, CHILD(n, 0), |
| 2671 | assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2672 | return; |
| 2673 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2674 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2675 | "can't assign to literal"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2676 | return; |
| 2677 | } |
| 2678 | break; |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2679 | |
| 2680 | case lambdef: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2681 | com_error(c, PyExc_SyntaxError, |
| 2682 | "can't assign to lambda"); |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2683 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2684 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2685 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2686 | com_error(c, PyExc_SystemError, |
| 2687 | "com_assign: bad node"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2688 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2689 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2690 | } |
| 2691 | } |
| 2692 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2693 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2694 | static void |
| 2695 | com_augassign(struct compiling *c, node *n) |
| 2696 | { |
| 2697 | int opcode; |
| 2698 | |
| 2699 | switch (STR(CHILD(CHILD(n, 1), 0))[0]) { |
| 2700 | case '+': opcode = INPLACE_ADD; break; |
| 2701 | case '-': opcode = INPLACE_SUBTRACT; break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2702 | case '/': |
| 2703 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '/') |
| 2704 | opcode = INPLACE_FLOOR_DIVIDE; |
| 2705 | else if (c->c_flags & CO_FUTURE_DIVISION) |
| 2706 | opcode = INPLACE_TRUE_DIVIDE; |
| 2707 | else |
| 2708 | opcode = INPLACE_DIVIDE; |
| 2709 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2710 | case '%': opcode = INPLACE_MODULO; break; |
| 2711 | case '<': opcode = INPLACE_LSHIFT; break; |
| 2712 | case '>': opcode = INPLACE_RSHIFT; break; |
| 2713 | case '&': opcode = INPLACE_AND; break; |
| 2714 | case '^': opcode = INPLACE_XOR; break; |
| 2715 | case '|': opcode = INPLACE_OR; break; |
| 2716 | case '*': |
| 2717 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '*') |
| 2718 | opcode = INPLACE_POWER; |
| 2719 | else |
| 2720 | opcode = INPLACE_MULTIPLY; |
| 2721 | break; |
| 2722 | default: |
| 2723 | com_error(c, PyExc_SystemError, "com_augassign: bad operator"); |
| 2724 | return; |
| 2725 | } |
| 2726 | com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2)); |
| 2727 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2728 | |
| 2729 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2730 | com_expr_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2731 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2732 | REQ(n, expr_stmt); |
| 2733 | /* testlist (('=' testlist)* | augassign testlist) */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2734 | /* Forget it if we have just a doc string here */ |
Guido van Rossum | 5f5e817 | 1997-04-06 03:41:40 +0000 | [diff] [blame] | 2735 | if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2736 | return; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2737 | if (NCH(n) == 1) { |
| 2738 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2739 | if (c->c_interactive) |
| 2740 | com_addbyte(c, PRINT_EXPR); |
| 2741 | else |
| 2742 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2743 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2744 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2745 | else if (TYPE(CHILD(n,1)) == augassign) |
| 2746 | com_augassign(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2747 | else { |
| 2748 | int i; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2749 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2750 | for (i = 0; i < NCH(n)-2; i+=2) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2751 | if (i+2 < NCH(n)-2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2752 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2753 | com_push(c, 1); |
| 2754 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2755 | com_assign(c, CHILD(n, i), OP_ASSIGN, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2756 | } |
| 2757 | } |
| 2758 | } |
| 2759 | |
| 2760 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2761 | com_assert_stmt(struct compiling *c, node *n) |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2762 | { |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2763 | int a = 0; |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2764 | int i; |
| 2765 | REQ(n, assert_stmt); /* 'assert' test [',' test] */ |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2766 | if (Py_OptimizeFlag) |
| 2767 | return; |
| 2768 | /* Generate code like |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2769 | |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2770 | if not <test>: |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2771 | raise AssertionError [, <message>] |
| 2772 | |
| 2773 | where <message> is the second test, if present. |
| 2774 | */ |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2775 | com_node(c, CHILD(n, 1)); |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2776 | com_addfwref(c, JUMP_IF_TRUE, &a); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2777 | com_addbyte(c, POP_TOP); |
| 2778 | com_pop(c, 1); |
| 2779 | /* Raise that exception! */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2780 | com_addop_name(c, LOAD_GLOBAL, "AssertionError"); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2781 | com_push(c, 1); |
| 2782 | i = NCH(n)/2; /* Either 2 or 4 */ |
| 2783 | if (i > 1) |
| 2784 | com_node(c, CHILD(n, 3)); |
| 2785 | com_addoparg(c, RAISE_VARARGS, i); |
| 2786 | com_pop(c, i); |
| 2787 | /* The interpreter does not fall through */ |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2788 | /* Jump ends up here */ |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2789 | com_backpatch(c, a); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2790 | com_addbyte(c, POP_TOP); |
| 2791 | } |
| 2792 | |
| 2793 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2794 | com_print_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2795 | { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2796 | int i = 1; |
| 2797 | node* stream = NULL; |
| 2798 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2799 | REQ(n, print_stmt); /* 'print' (test ',')* [test] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2800 | |
| 2801 | /* are we using the extended print form? */ |
| 2802 | if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) { |
| 2803 | stream = CHILD(n, 2); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2804 | com_node(c, stream); |
| 2805 | /* stack: [...] => [... stream] */ |
| 2806 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2807 | if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA) |
| 2808 | i = 4; |
| 2809 | else |
| 2810 | i = 3; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2811 | } |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2812 | for (; i < NCH(n); i += 2) { |
| 2813 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2814 | com_addbyte(c, DUP_TOP); |
| 2815 | /* stack: [stream] => [stream stream] */ |
| 2816 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2817 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2818 | /* stack: [stream stream] => [stream stream obj] */ |
| 2819 | com_addbyte(c, ROT_TWO); |
| 2820 | /* stack: [stream stream obj] => [stream obj stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2821 | com_addbyte(c, PRINT_ITEM_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2822 | /* stack: [stream obj stream] => [stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2823 | com_pop(c, 2); |
| 2824 | } |
| 2825 | else { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2826 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2827 | /* stack: [...] => [... obj] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2828 | com_addbyte(c, PRINT_ITEM); |
| 2829 | com_pop(c, 1); |
| 2830 | } |
| 2831 | } |
| 2832 | /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */ |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2833 | if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2834 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2835 | /* must pop the extra stream object off the stack */ |
| 2836 | com_addbyte(c, POP_TOP); |
| 2837 | /* stack: [... stream] => [...] */ |
| 2838 | com_pop(c, 1); |
| 2839 | } |
| 2840 | } |
| 2841 | else { |
| 2842 | if (stream != NULL) { |
| 2843 | /* this consumes the last stream object on stack */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2844 | com_addbyte(c, PRINT_NEWLINE_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2845 | /* stack: [... stream] => [...] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2846 | com_pop(c, 1); |
| 2847 | } |
| 2848 | else |
| 2849 | com_addbyte(c, PRINT_NEWLINE); |
| 2850 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
| 2853 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2854 | com_return_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2855 | { |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2856 | REQ(n, return_stmt); /* 'return' [testlist] */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2857 | if (!c->c_infunction) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2858 | com_error(c, PyExc_SyntaxError, "'return' outside function"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2859 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2860 | if (c->c_flags & CO_GENERATOR) { |
| 2861 | if (NCH(n) > 1) { |
| 2862 | com_error(c, PyExc_SyntaxError, |
| 2863 | "'return' with argument inside generator"); |
| 2864 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 2865 | } |
| 2866 | if (NCH(n) < 2) { |
| 2867 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2868 | com_push(c, 1); |
| 2869 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 2870 | else |
| 2871 | com_node(c, CHILD(n, 1)); |
| 2872 | com_addbyte(c, RETURN_VALUE); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2873 | com_pop(c, 1); |
| 2874 | } |
| 2875 | |
| 2876 | static void |
| 2877 | com_yield_stmt(struct compiling *c, node *n) |
| 2878 | { |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 2879 | int i; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2880 | REQ(n, yield_stmt); /* 'yield' testlist */ |
| 2881 | if (!c->c_infunction) { |
| 2882 | com_error(c, PyExc_SyntaxError, "'yield' outside function"); |
| 2883 | } |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 2884 | |
| 2885 | for (i = 0; i < c->c_nblocks; ++i) { |
| 2886 | if (c->c_block[i] == SETUP_FINALLY) { |
| 2887 | com_error(c, PyExc_SyntaxError, |
| 2888 | "'yield' not allowed in a 'try' block " |
| 2889 | "with a 'finally' clause"); |
| 2890 | return; |
| 2891 | } |
| 2892 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2893 | com_node(c, CHILD(n, 1)); |
| 2894 | com_addbyte(c, YIELD_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2895 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2896 | } |
| 2897 | |
| 2898 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2899 | com_raise_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2900 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2901 | int i; |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 2902 | REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */ |
| 2903 | if (NCH(n) > 1) { |
| 2904 | com_node(c, CHILD(n, 1)); |
| 2905 | if (NCH(n) > 3) { |
| 2906 | com_node(c, CHILD(n, 3)); |
| 2907 | if (NCH(n) > 5) |
| 2908 | com_node(c, CHILD(n, 5)); |
| 2909 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2910 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2911 | i = NCH(n)/2; |
| 2912 | com_addoparg(c, RAISE_VARARGS, i); |
| 2913 | com_pop(c, i); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2914 | } |
| 2915 | |
| 2916 | static void |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2917 | com_from_import(struct compiling *c, node *n) |
| 2918 | { |
| 2919 | com_addopname(c, IMPORT_FROM, CHILD(n, 0)); |
| 2920 | com_push(c, 1); |
| 2921 | if (NCH(n) > 1) { |
| 2922 | if (strcmp(STR(CHILD(n, 1)), "as") != 0) { |
| 2923 | com_error(c, PyExc_SyntaxError, "invalid syntax"); |
| 2924 | return; |
| 2925 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2926 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2927 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2928 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2929 | com_pop(c, 1); |
| 2930 | } |
| 2931 | |
| 2932 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2933 | com_import_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2934 | { |
| 2935 | int i; |
| 2936 | REQ(n, import_stmt); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2937 | /* 'import' dotted_name (',' dotted_name)* | |
| 2938 | 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2939 | if (STR(CHILD(n, 0))[0] == 'f') { |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 2940 | PyObject *tup; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2941 | /* 'from' dotted_name 'import' ... */ |
| 2942 | REQ(CHILD(n, 1), dotted_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2943 | |
| 2944 | if (TYPE(CHILD(n, 3)) == STAR) { |
| 2945 | tup = Py_BuildValue("(s)", "*"); |
| 2946 | } else { |
| 2947 | tup = PyTuple_New((NCH(n) - 2)/2); |
| 2948 | for (i = 3; i < NCH(n); i += 2) { |
| 2949 | PyTuple_SET_ITEM(tup, (i-3)/2, |
| 2950 | PyString_FromString(STR( |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 2951 | CHILD(CHILD(n, i), 0)))); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2952 | } |
| 2953 | } |
| 2954 | com_addoparg(c, LOAD_CONST, com_addconst(c, tup)); |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 2955 | Py_DECREF(tup); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2956 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2957 | com_addopname(c, IMPORT_NAME, CHILD(n, 1)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2958 | if (TYPE(CHILD(n, 3)) == STAR) |
| 2959 | com_addbyte(c, IMPORT_STAR); |
| 2960 | else { |
| 2961 | for (i = 3; i < NCH(n); i += 2) |
| 2962 | com_from_import(c, CHILD(n, i)); |
| 2963 | com_addbyte(c, POP_TOP); |
| 2964 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2965 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2966 | } |
| 2967 | else { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2968 | /* 'import' ... */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2969 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2970 | node *subn = CHILD(n, i); |
| 2971 | REQ(subn, dotted_as_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2972 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2973 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2974 | com_addopname(c, IMPORT_NAME, CHILD(subn, 0)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2975 | if (NCH(subn) > 1) { |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 2976 | int j; |
| 2977 | if (strcmp(STR(CHILD(subn, 1)), "as") != 0) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2978 | com_error(c, PyExc_SyntaxError, |
| 2979 | "invalid syntax"); |
| 2980 | return; |
| 2981 | } |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 2982 | for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2) |
| 2983 | com_addopname(c, LOAD_ATTR, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2984 | CHILD(CHILD(subn, 0), |
| 2985 | j)); |
| 2986 | com_addop_varname(c, VAR_STORE, |
| 2987 | STR(CHILD(subn, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2988 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2989 | com_addop_varname(c, VAR_STORE, |
| 2990 | STR(CHILD(CHILD(subn, 0), |
| 2991 | 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2992 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2993 | } |
| 2994 | } |
| 2995 | } |
| 2996 | |
| 2997 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2998 | com_exec_stmt(struct compiling *c, node *n) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2999 | { |
| 3000 | REQ(n, exec_stmt); |
| 3001 | /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */ |
| 3002 | com_node(c, CHILD(n, 1)); |
| 3003 | if (NCH(n) >= 4) |
| 3004 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3005 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3006 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3007 | com_push(c, 1); |
| 3008 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3009 | if (NCH(n) >= 6) |
| 3010 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3011 | else { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3012 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3013 | com_push(c, 1); |
| 3014 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3015 | com_addbyte(c, EXEC_STMT); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3016 | com_pop(c, 3); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3017 | } |
| 3018 | |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3019 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3020 | is_constant_false(struct compiling *c, node *n) |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3021 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3022 | PyObject *v; |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3023 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3024 | /* argument c will be NULL when called from symtable_node() */ |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3025 | |
| 3026 | /* Label to avoid tail recursion */ |
| 3027 | next: |
| 3028 | switch (TYPE(n)) { |
| 3029 | |
| 3030 | case suite: |
| 3031 | if (NCH(n) == 1) { |
| 3032 | n = CHILD(n, 0); |
| 3033 | goto next; |
| 3034 | } |
| 3035 | /* Fall through */ |
| 3036 | case file_input: |
| 3037 | for (i = 0; i < NCH(n); i++) { |
| 3038 | node *ch = CHILD(n, i); |
| 3039 | if (TYPE(ch) == stmt) { |
| 3040 | n = ch; |
| 3041 | goto next; |
| 3042 | } |
| 3043 | } |
| 3044 | break; |
| 3045 | |
| 3046 | case stmt: |
| 3047 | case simple_stmt: |
| 3048 | case small_stmt: |
| 3049 | n = CHILD(n, 0); |
| 3050 | goto next; |
| 3051 | |
| 3052 | case expr_stmt: |
| 3053 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3054 | case testlist1: |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3055 | case test: |
| 3056 | case and_test: |
| 3057 | case not_test: |
| 3058 | case comparison: |
| 3059 | case expr: |
| 3060 | case xor_expr: |
| 3061 | case and_expr: |
| 3062 | case shift_expr: |
| 3063 | case arith_expr: |
| 3064 | case term: |
| 3065 | case factor: |
| 3066 | case power: |
| 3067 | case atom: |
| 3068 | if (NCH(n) == 1) { |
| 3069 | n = CHILD(n, 0); |
| 3070 | goto next; |
| 3071 | } |
| 3072 | break; |
| 3073 | |
| 3074 | case NAME: |
| 3075 | if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0) |
| 3076 | return 1; |
| 3077 | break; |
| 3078 | |
| 3079 | case NUMBER: |
| 3080 | v = parsenumber(c, STR(n)); |
| 3081 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3082 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3083 | break; |
| 3084 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3085 | i = PyObject_IsTrue(v); |
| 3086 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3087 | return i == 0; |
| 3088 | |
| 3089 | case STRING: |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3090 | v = parsestr(c, STR(n)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3091 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3092 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3093 | break; |
| 3094 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3095 | i = PyObject_IsTrue(v); |
| 3096 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3097 | return i == 0; |
| 3098 | |
| 3099 | } |
| 3100 | return 0; |
| 3101 | } |
| 3102 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3103 | |
| 3104 | /* Look under n for a return stmt with an expression. |
| 3105 | * This hack is used to find illegal returns under "if 0:" blocks in |
| 3106 | * functions already known to be generators (as determined by the symtable |
| 3107 | * pass). |
| 3108 | * Return the offending return node if found, else NULL. |
| 3109 | */ |
| 3110 | static node * |
| 3111 | look_for_offending_return(node *n) |
| 3112 | { |
| 3113 | int i; |
| 3114 | |
| 3115 | for (i = 0; i < NCH(n); ++i) { |
| 3116 | node *kid = CHILD(n, i); |
| 3117 | |
| 3118 | switch (TYPE(kid)) { |
| 3119 | case classdef: |
| 3120 | case funcdef: |
| 3121 | case lambdef: |
| 3122 | /* Stuff in nested functions & classes doesn't |
| 3123 | affect the code block we started in. */ |
| 3124 | return NULL; |
| 3125 | |
| 3126 | case return_stmt: |
| 3127 | if (NCH(kid) > 1) |
| 3128 | return kid; |
| 3129 | break; |
| 3130 | |
| 3131 | default: { |
| 3132 | node *bad = look_for_offending_return(kid); |
| 3133 | if (bad != NULL) |
| 3134 | return bad; |
| 3135 | } |
| 3136 | } |
| 3137 | } |
| 3138 | |
| 3139 | return NULL; |
| 3140 | } |
| 3141 | |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3142 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3143 | com_if_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3144 | { |
| 3145 | int i; |
| 3146 | int anchor = 0; |
| 3147 | REQ(n, if_stmt); |
| 3148 | /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */ |
| 3149 | for (i = 0; i+3 < NCH(n); i+=4) { |
| 3150 | int a = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3151 | node *ch = CHILD(n, i+1); |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3152 | if (is_constant_false(c, ch)) { |
| 3153 | /* We're going to skip this block. However, if this |
| 3154 | is a generator, we have to check the dead code |
| 3155 | anyway to make sure there aren't any return stmts |
| 3156 | with expressions, in the same scope. */ |
| 3157 | if (c->c_flags & CO_GENERATOR) { |
| 3158 | node *p = look_for_offending_return(n); |
| 3159 | if (p != NULL) { |
| 3160 | int savelineno = c->c_lineno; |
| 3161 | c->c_lineno = p->n_lineno; |
| 3162 | com_error(c, PyExc_SyntaxError, |
| 3163 | "'return' with argument " |
| 3164 | "inside generator"); |
| 3165 | c->c_lineno = savelineno; |
| 3166 | } |
| 3167 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3168 | continue; |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3169 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3170 | if (i > 0) |
| 3171 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3172 | com_node(c, ch); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3173 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 3174 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3175 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3176 | com_node(c, CHILD(n, i+3)); |
| 3177 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 3178 | com_backpatch(c, a); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3179 | /* We jump here with an extra entry which we now pop */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3180 | com_addbyte(c, POP_TOP); |
| 3181 | } |
| 3182 | if (i+2 < NCH(n)) |
| 3183 | com_node(c, CHILD(n, i+2)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3184 | if (anchor) |
| 3185 | com_backpatch(c, anchor); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3186 | } |
| 3187 | |
| 3188 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3189 | com_while_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3190 | { |
| 3191 | int break_anchor = 0; |
| 3192 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3193 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3194 | REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */ |
| 3195 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3196 | block_push(c, SETUP_LOOP); |
| 3197 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3198 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3199 | com_node(c, CHILD(n, 1)); |
| 3200 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 3201 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3202 | com_pop(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3203 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3204 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3205 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3206 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3207 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3208 | com_backpatch(c, anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3209 | /* We jump here with one entry more on the stack */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3210 | com_addbyte(c, POP_TOP); |
| 3211 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3212 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3213 | if (NCH(n) > 4) |
| 3214 | com_node(c, CHILD(n, 6)); |
| 3215 | com_backpatch(c, break_anchor); |
| 3216 | } |
| 3217 | |
| 3218 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3219 | com_for_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3220 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3221 | int break_anchor = 0; |
| 3222 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3223 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3224 | REQ(n, for_stmt); |
| 3225 | /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */ |
| 3226 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3227 | block_push(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3228 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3229 | com_addbyte(c, GET_ITER); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3230 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3231 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3232 | com_addfwref(c, FOR_ITER, &anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3233 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3234 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3235 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3236 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3237 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3238 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3239 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3240 | com_backpatch(c, anchor); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3241 | com_pop(c, 1); /* FOR_ITER has popped this */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3242 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3243 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3244 | if (NCH(n) > 8) |
| 3245 | com_node(c, CHILD(n, 8)); |
| 3246 | com_backpatch(c, break_anchor); |
| 3247 | } |
| 3248 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3249 | /* Code generated for "try: S finally: Sf" is as follows: |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3250 | |
| 3251 | SETUP_FINALLY L |
| 3252 | <code for S> |
| 3253 | POP_BLOCK |
| 3254 | LOAD_CONST <nil> |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3255 | L: <code for Sf> |
| 3256 | END_FINALLY |
| 3257 | |
| 3258 | The special instructions use the block stack. Each block |
| 3259 | stack entry contains the instruction that created it (here |
| 3260 | SETUP_FINALLY), the level of the value stack at the time the |
| 3261 | block stack entry was created, and a label (here L). |
| 3262 | |
| 3263 | SETUP_FINALLY: |
| 3264 | Pushes the current value stack level and the label |
| 3265 | onto the block stack. |
| 3266 | POP_BLOCK: |
| 3267 | Pops en entry from the block stack, and pops the value |
| 3268 | stack until its level is the same as indicated on the |
| 3269 | block stack. (The label is ignored.) |
| 3270 | END_FINALLY: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3271 | Pops a variable number of entries from the *value* stack |
| 3272 | and re-raises the exception they specify. The number of |
| 3273 | entries popped depends on the (pseudo) exception type. |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3274 | |
| 3275 | The block stack is unwound when an exception is raised: |
| 3276 | when a SETUP_FINALLY entry is found, the exception is pushed |
| 3277 | onto the value stack (and the exception condition is cleared), |
| 3278 | and the interpreter jumps to the label gotten from the block |
| 3279 | stack. |
| 3280 | |
| 3281 | 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] | 3282 | (The contents of the value stack is shown in [], with the top |
| 3283 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3284 | associated value, and 'exc' the exception.) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3285 | |
| 3286 | Value stack Label Instruction Argument |
| 3287 | [] SETUP_EXCEPT L1 |
| 3288 | [] <code for S> |
| 3289 | [] POP_BLOCK |
| 3290 | [] JUMP_FORWARD L0 |
| 3291 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3292 | [tb, val, exc] L1: DUP ) |
| 3293 | [tb, val, exc, exc] <evaluate E1> ) |
| 3294 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 3295 | [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 ) |
| 3296 | [tb, val, exc, 1] POP ) |
| 3297 | [tb, val, exc] POP |
| 3298 | [tb, val] <assign to V1> (or POP if no V1) |
| 3299 | [tb] POP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3300 | [] <code for S1> |
| 3301 | JUMP_FORWARD L0 |
| 3302 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3303 | [tb, val, exc, 0] L2: POP |
| 3304 | [tb, val, exc] DUP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3305 | .............................etc....................... |
| 3306 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3307 | [tb, val, exc, 0] Ln+1: POP |
| 3308 | [tb, val, exc] END_FINALLY # re-raise exception |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3309 | |
| 3310 | [] L0: <next statement> |
| 3311 | |
| 3312 | Of course, parts are not generated if Vi or Ei is not present. |
| 3313 | */ |
| 3314 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3315 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3316 | com_try_except(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3317 | { |
| 3318 | int except_anchor = 0; |
| 3319 | int end_anchor = 0; |
| 3320 | int else_anchor = 0; |
| 3321 | int i; |
| 3322 | node *ch; |
| 3323 | |
| 3324 | com_addfwref(c, SETUP_EXCEPT, &except_anchor); |
| 3325 | block_push(c, SETUP_EXCEPT); |
| 3326 | com_node(c, CHILD(n, 2)); |
| 3327 | com_addbyte(c, POP_BLOCK); |
| 3328 | block_pop(c, SETUP_EXCEPT); |
| 3329 | com_addfwref(c, JUMP_FORWARD, &else_anchor); |
| 3330 | com_backpatch(c, except_anchor); |
| 3331 | for (i = 3; |
| 3332 | i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause; |
| 3333 | i += 3) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3334 | /* except_clause: 'except' [expr [',' var]] */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3335 | if (except_anchor == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3336 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 3337 | "default 'except:' must be last"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3338 | break; |
| 3339 | } |
| 3340 | except_anchor = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3341 | com_push(c, 3); /* tb, val, exc pushed by exception */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3342 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 3343 | if (NCH(ch) > 1) { |
| 3344 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3345 | com_push(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3346 | com_node(c, CHILD(ch, 1)); |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 3347 | com_addoparg(c, COMPARE_OP, PyCmp_EXC_MATCH); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3348 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3349 | com_addfwref(c, JUMP_IF_FALSE, &except_anchor); |
| 3350 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3351 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3352 | } |
| 3353 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3354 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3355 | if (NCH(ch) > 3) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3356 | com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3357 | else { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3358 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3359 | com_pop(c, 1); |
| 3360 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3361 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3362 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3363 | com_node(c, CHILD(n, i+2)); |
| 3364 | com_addfwref(c, JUMP_FORWARD, &end_anchor); |
| 3365 | if (except_anchor) { |
| 3366 | com_backpatch(c, except_anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3367 | /* We come in with [tb, val, exc, 0] on the |
| 3368 | stack; one pop and it's the same as |
| 3369 | expected at the start of the loop */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3370 | com_addbyte(c, POP_TOP); |
| 3371 | } |
| 3372 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3373 | /* We actually come in here with [tb, val, exc] but the |
| 3374 | END_FINALLY will zap those and jump around. |
| 3375 | The c_stacklevel does not reflect them so we need not pop |
| 3376 | anything. */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3377 | com_addbyte(c, END_FINALLY); |
| 3378 | com_backpatch(c, else_anchor); |
| 3379 | if (i < NCH(n)) |
| 3380 | com_node(c, CHILD(n, i+2)); |
| 3381 | com_backpatch(c, end_anchor); |
| 3382 | } |
| 3383 | |
| 3384 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3385 | com_try_finally(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3386 | { |
| 3387 | int finally_anchor = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3388 | node *ch; |
Guido van Rossum | 94fb82e | 1992-04-05 14:24:50 +0000 | [diff] [blame] | 3389 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3390 | com_addfwref(c, SETUP_FINALLY, &finally_anchor); |
| 3391 | block_push(c, SETUP_FINALLY); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3392 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3393 | com_addbyte(c, POP_BLOCK); |
| 3394 | block_pop(c, SETUP_FINALLY); |
| 3395 | block_push(c, END_FINALLY); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3396 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3397 | /* While the generated code pushes only one item, |
| 3398 | the try-finally handling can enter here with |
| 3399 | up to three items. OK, here are the details: |
| 3400 | 3 for an exception, 2 for RETURN, 1 for BREAK. */ |
| 3401 | com_push(c, 3); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3402 | com_backpatch(c, finally_anchor); |
| 3403 | ch = CHILD(n, NCH(n)-1); |
| 3404 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 3405 | com_node(c, ch); |
| 3406 | com_addbyte(c, END_FINALLY); |
| 3407 | block_pop(c, END_FINALLY); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3408 | com_pop(c, 3); /* Matches the com_push above */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3409 | } |
| 3410 | |
| 3411 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3412 | com_try_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3413 | { |
| 3414 | REQ(n, try_stmt); |
| 3415 | /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
| 3416 | | 'try' ':' suite 'finally' ':' suite */ |
| 3417 | if (TYPE(CHILD(n, 3)) != except_clause) |
| 3418 | com_try_finally(c, n); |
| 3419 | else |
| 3420 | com_try_except(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3421 | } |
| 3422 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3423 | static node * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3424 | get_rawdocstring(node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3425 | { |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3426 | int i; |
| 3427 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3428 | /* Label to avoid tail recursion */ |
| 3429 | next: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3430 | switch (TYPE(n)) { |
| 3431 | |
| 3432 | case suite: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3433 | if (NCH(n) == 1) { |
| 3434 | n = CHILD(n, 0); |
| 3435 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3436 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3437 | /* Fall through */ |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3438 | case file_input: |
| 3439 | for (i = 0; i < NCH(n); i++) { |
| 3440 | node *ch = CHILD(n, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3441 | if (TYPE(ch) == stmt) { |
| 3442 | n = ch; |
| 3443 | goto next; |
| 3444 | } |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3445 | } |
| 3446 | break; |
| 3447 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3448 | case stmt: |
| 3449 | case simple_stmt: |
| 3450 | case small_stmt: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3451 | n = CHILD(n, 0); |
| 3452 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3453 | |
| 3454 | case expr_stmt: |
| 3455 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3456 | case testlist1: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3457 | case test: |
| 3458 | case and_test: |
| 3459 | case not_test: |
| 3460 | case comparison: |
| 3461 | case expr: |
| 3462 | case xor_expr: |
| 3463 | case and_expr: |
| 3464 | case shift_expr: |
| 3465 | case arith_expr: |
| 3466 | case term: |
| 3467 | case factor: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3468 | case power: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3469 | if (NCH(n) == 1) { |
| 3470 | n = CHILD(n, 0); |
| 3471 | goto next; |
| 3472 | } |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3473 | break; |
| 3474 | |
| 3475 | case atom: |
| 3476 | if (TYPE(CHILD(n, 0)) == STRING) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3477 | return n; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3478 | break; |
| 3479 | |
| 3480 | } |
| 3481 | return NULL; |
| 3482 | } |
| 3483 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3484 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3485 | get_docstring(struct compiling *c, node *n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3486 | { |
Guido van Rossum | 541563e | 1999-01-28 15:08:09 +0000 | [diff] [blame] | 3487 | /* Don't generate doc-strings if run with -OO */ |
| 3488 | if (Py_OptimizeFlag > 1) |
| 3489 | return NULL; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3490 | n = get_rawdocstring(n); |
| 3491 | if (n == NULL) |
| 3492 | return NULL; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3493 | return parsestrplus(c, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3494 | } |
| 3495 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3496 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3497 | com_suite(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3498 | { |
| 3499 | REQ(n, suite); |
| 3500 | /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */ |
| 3501 | if (NCH(n) == 1) { |
| 3502 | com_node(c, CHILD(n, 0)); |
| 3503 | } |
| 3504 | else { |
| 3505 | int i; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3506 | for (i = 0; i < NCH(n) && c->c_errors == 0; i++) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3507 | node *ch = CHILD(n, i); |
| 3508 | if (TYPE(ch) == stmt) |
| 3509 | com_node(c, ch); |
| 3510 | } |
| 3511 | } |
| 3512 | } |
| 3513 | |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3514 | /* ARGSUSED */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3515 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3516 | com_continue_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3517 | { |
| 3518 | int i = c->c_nblocks; |
| 3519 | if (i-- > 0 && c->c_block[i] == SETUP_LOOP) { |
| 3520 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3521 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3522 | else if (i <= 0) { |
| 3523 | /* at the outer level */ |
| 3524 | com_error(c, PyExc_SyntaxError, |
| 3525 | "'continue' not properly in loop"); |
| 3526 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3527 | else { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3528 | int j; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3529 | for (j = i-1; j >= 0; --j) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3530 | if (c->c_block[j] == SETUP_LOOP) |
| 3531 | break; |
| 3532 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3533 | if (j >= 0) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3534 | /* there is a loop, but something interferes */ |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3535 | for (; i > j; --i) { |
| 3536 | if (c->c_block[i] == SETUP_EXCEPT || |
| 3537 | c->c_block[i] == SETUP_FINALLY) { |
| 3538 | com_addoparg(c, CONTINUE_LOOP, |
| 3539 | c->c_begin); |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3540 | return; |
| 3541 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3542 | if (c->c_block[i] == END_FINALLY) { |
| 3543 | com_error(c, PyExc_SyntaxError, |
| 3544 | "'continue' not supported inside 'finally' clause"); |
| 3545 | return; |
| 3546 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3547 | } |
| 3548 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3549 | com_error(c, PyExc_SyntaxError, |
| 3550 | "'continue' not properly in loop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3551 | } |
| 3552 | /* XXX Could allow it inside a 'finally' clause |
| 3553 | XXX if we could pop the exception still on the stack */ |
| 3554 | } |
| 3555 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3556 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3557 | com_argdefs(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3558 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3559 | int i, nch, nargs, ndefs; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3560 | if (TYPE(n) == lambdef) { |
| 3561 | /* lambdef: 'lambda' [varargslist] ':' test */ |
| 3562 | n = CHILD(n, 1); |
| 3563 | } |
| 3564 | else { |
| 3565 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */ |
| 3566 | n = CHILD(n, 2); |
| 3567 | REQ(n, parameters); /* parameters: '(' [varargslist] ')' */ |
| 3568 | n = CHILD(n, 1); |
| 3569 | } |
| 3570 | if (TYPE(n) != varargslist) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3571 | return 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3572 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3573 | (fpdef ['=' test] ',')* '*' ....... | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3574 | fpdef ['=' test] (',' fpdef ['=' test])* [','] */ |
| 3575 | nch = NCH(n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3576 | nargs = 0; |
| 3577 | ndefs = 0; |
| 3578 | for (i = 0; i < nch; i++) { |
| 3579 | int t; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3580 | if (TYPE(CHILD(n, i)) == STAR || |
| 3581 | TYPE(CHILD(n, i)) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3582 | break; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3583 | nargs++; |
| 3584 | i++; |
| 3585 | if (i >= nch) |
Guido van Rossum | a1e7e62 | 1995-09-18 21:44:04 +0000 | [diff] [blame] | 3586 | t = RPAR; /* Anything except EQUAL or COMMA */ |
| 3587 | else |
| 3588 | t = TYPE(CHILD(n, i)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3589 | if (t == EQUAL) { |
| 3590 | i++; |
| 3591 | ndefs++; |
| 3592 | com_node(c, CHILD(n, i)); |
| 3593 | i++; |
| 3594 | if (i >= nch) |
| 3595 | break; |
| 3596 | t = TYPE(CHILD(n, i)); |
| 3597 | } |
| 3598 | else { |
Guido van Rossum | 29d38cd | 1998-10-02 13:41:54 +0000 | [diff] [blame] | 3599 | /* Treat "(a=1, b)" as an error */ |
| 3600 | if (ndefs) |
| 3601 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | dfede31 | 1998-10-02 14:06:56 +0000 | [diff] [blame] | 3602 | "non-default argument follows default argument"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3603 | } |
| 3604 | if (t != COMMA) |
| 3605 | break; |
| 3606 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3607 | return ndefs; |
| 3608 | } |
| 3609 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3610 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3611 | com_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3612 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3613 | PyObject *co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3614 | int ndefs; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3615 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3616 | ndefs = com_argdefs(c, n); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3617 | symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n), |
| 3618 | n->n_lineno); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3619 | co = (PyObject *)icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3620 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3621 | if (co == NULL) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3622 | c->c_errors++; |
| 3623 | else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3624 | int closure = com_make_closure(c, (PyCodeObject *)co); |
| 3625 | int i = com_addconst(c, co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3626 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3627 | com_push(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3628 | if (closure) |
| 3629 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
| 3630 | else |
| 3631 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3632 | com_pop(c, ndefs); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3633 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3634 | com_pop(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3635 | Py_DECREF(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3636 | } |
| 3637 | } |
| 3638 | |
| 3639 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3640 | com_bases(struct compiling *c, node *n) |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3641 | { |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3642 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3643 | REQ(n, testlist); |
| 3644 | /* testlist: test (',' test)* [','] */ |
| 3645 | for (i = 0; i < NCH(n); i += 2) |
| 3646 | com_node(c, CHILD(n, i)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3647 | i = (NCH(n)+1) / 2; |
| 3648 | com_addoparg(c, BUILD_TUPLE, i); |
| 3649 | com_pop(c, i-1); |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3650 | } |
| 3651 | |
| 3652 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3653 | com_classdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3654 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3655 | int i; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3656 | PyObject *v; |
| 3657 | PyCodeObject *co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3658 | char *name; |
| 3659 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3660 | REQ(n, classdef); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3661 | /* classdef: class NAME ['(' testlist ')'] ':' suite */ |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 3662 | if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3663 | c->c_errors++; |
| 3664 | return; |
| 3665 | } |
| 3666 | /* Push the class name on the stack */ |
| 3667 | i = com_addconst(c, v); |
| 3668 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3669 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3670 | Py_DECREF(v); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3671 | /* Push the tuple of base classes on the stack */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3672 | if (TYPE(CHILD(n, 2)) != LPAR) { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3673 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3674 | com_push(c, 1); |
| 3675 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3676 | else |
| 3677 | com_bases(c, CHILD(n, 3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3678 | name = STR(CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3679 | symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3680 | co = icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3681 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3682 | if (co == NULL) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3683 | c->c_errors++; |
| 3684 | else { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3685 | int closure = com_make_closure(c, co); |
| 3686 | i = com_addconst(c, (PyObject *)co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3687 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3688 | com_push(c, 1); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3689 | if (closure) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3690 | com_addoparg(c, MAKE_CLOSURE, 0); |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 3691 | com_pop(c, PyCode_GetNumFree(co)); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3692 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3693 | com_addoparg(c, MAKE_FUNCTION, 0); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3694 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3695 | com_addbyte(c, BUILD_CLASS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3696 | com_pop(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3697 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3698 | com_pop(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3699 | Py_DECREF(co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3700 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3701 | } |
| 3702 | |
| 3703 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3704 | com_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3705 | { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3706 | loop: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3707 | if (c->c_errors) |
| 3708 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3709 | switch (TYPE(n)) { |
| 3710 | |
| 3711 | /* Definition nodes */ |
| 3712 | |
| 3713 | case funcdef: |
| 3714 | com_funcdef(c, n); |
| 3715 | break; |
| 3716 | case classdef: |
| 3717 | com_classdef(c, n); |
| 3718 | break; |
| 3719 | |
| 3720 | /* Trivial parse tree nodes */ |
| 3721 | |
| 3722 | case stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3723 | case small_stmt: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3724 | case flow_stmt: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3725 | n = CHILD(n, 0); |
| 3726 | goto loop; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3727 | |
| 3728 | case simple_stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3729 | /* small_stmt (';' small_stmt)* [';'] NEWLINE */ |
| 3730 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 3731 | { |
| 3732 | int i; |
| 3733 | for (i = 0; i < NCH(n)-1; i += 2) |
| 3734 | com_node(c, CHILD(n, i)); |
| 3735 | } |
| 3736 | break; |
| 3737 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3738 | case compound_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3739 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3740 | n = CHILD(n, 0); |
| 3741 | goto loop; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3742 | |
| 3743 | /* Statement nodes */ |
| 3744 | |
| 3745 | case expr_stmt: |
| 3746 | com_expr_stmt(c, n); |
| 3747 | break; |
| 3748 | case print_stmt: |
| 3749 | com_print_stmt(c, n); |
| 3750 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3751 | case del_stmt: /* 'del' exprlist */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3752 | com_assign(c, CHILD(n, 1), OP_DELETE, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3753 | break; |
| 3754 | case pass_stmt: |
| 3755 | break; |
| 3756 | case break_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3757 | if (c->c_loops == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3758 | com_error(c, PyExc_SyntaxError, |
| 3759 | "'break' outside loop"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3760 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3761 | com_addbyte(c, BREAK_LOOP); |
| 3762 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3763 | case continue_stmt: |
| 3764 | com_continue_stmt(c, n); |
| 3765 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3766 | case return_stmt: |
| 3767 | com_return_stmt(c, n); |
| 3768 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3769 | case yield_stmt: |
| 3770 | com_yield_stmt(c, n); |
| 3771 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3772 | case raise_stmt: |
| 3773 | com_raise_stmt(c, n); |
| 3774 | break; |
| 3775 | case import_stmt: |
| 3776 | com_import_stmt(c, n); |
| 3777 | break; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3778 | case global_stmt: |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3779 | break; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3780 | case exec_stmt: |
| 3781 | com_exec_stmt(c, n); |
| 3782 | break; |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 3783 | case assert_stmt: |
| 3784 | com_assert_stmt(c, n); |
| 3785 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3786 | case if_stmt: |
| 3787 | com_if_stmt(c, n); |
| 3788 | break; |
| 3789 | case while_stmt: |
| 3790 | com_while_stmt(c, n); |
| 3791 | break; |
| 3792 | case for_stmt: |
| 3793 | com_for_stmt(c, n); |
| 3794 | break; |
| 3795 | case try_stmt: |
| 3796 | com_try_stmt(c, n); |
| 3797 | break; |
| 3798 | case suite: |
| 3799 | com_suite(c, n); |
| 3800 | break; |
| 3801 | |
| 3802 | /* Expression nodes */ |
| 3803 | |
| 3804 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3805 | case testlist1: |
Guido van Rossum | 1c91707 | 2001-10-15 15:44:05 +0000 | [diff] [blame] | 3806 | case testlist_safe: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3807 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3808 | break; |
| 3809 | case test: |
| 3810 | com_test(c, n); |
| 3811 | break; |
| 3812 | case and_test: |
| 3813 | com_and_test(c, n); |
| 3814 | break; |
| 3815 | case not_test: |
| 3816 | com_not_test(c, n); |
| 3817 | break; |
| 3818 | case comparison: |
| 3819 | com_comparison(c, n); |
| 3820 | break; |
| 3821 | case exprlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3822 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3823 | break; |
| 3824 | case expr: |
| 3825 | com_expr(c, n); |
| 3826 | break; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 3827 | case xor_expr: |
| 3828 | com_xor_expr(c, n); |
| 3829 | break; |
| 3830 | case and_expr: |
| 3831 | com_and_expr(c, n); |
| 3832 | break; |
| 3833 | case shift_expr: |
| 3834 | com_shift_expr(c, n); |
| 3835 | break; |
| 3836 | case arith_expr: |
| 3837 | com_arith_expr(c, n); |
| 3838 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3839 | case term: |
| 3840 | com_term(c, n); |
| 3841 | break; |
| 3842 | case factor: |
| 3843 | com_factor(c, n); |
| 3844 | break; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3845 | case power: |
| 3846 | com_power(c, n); |
| 3847 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3848 | case atom: |
| 3849 | com_atom(c, n); |
| 3850 | break; |
| 3851 | |
| 3852 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3853 | com_error(c, PyExc_SystemError, |
| 3854 | "com_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3855 | } |
| 3856 | } |
| 3857 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 3858 | static void com_fplist(struct compiling *, node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3859 | |
| 3860 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3861 | com_fpdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3862 | { |
| 3863 | REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3864 | if (TYPE(CHILD(n, 0)) == LPAR) |
| 3865 | com_fplist(c, CHILD(n, 1)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3866 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3867 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3868 | com_pop(c, 1); |
| 3869 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3870 | } |
| 3871 | |
| 3872 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3873 | com_fplist(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3874 | { |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 3875 | REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3876 | if (NCH(n) == 1) { |
| 3877 | com_fpdef(c, CHILD(n, 0)); |
| 3878 | } |
| 3879 | else { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3880 | int i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 3881 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3882 | com_push(c, i-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3883 | for (i = 0; i < NCH(n); i += 2) |
| 3884 | com_fpdef(c, CHILD(n, i)); |
| 3885 | } |
| 3886 | } |
| 3887 | |
| 3888 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3889 | com_arglist(struct compiling *c, node *n) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3890 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3891 | int nch, i, narg; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3892 | int complex = 0; |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 3893 | char nbuf[30]; |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3894 | REQ(n, varargslist); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 3895 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3896 | (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3897 | nch = NCH(n); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3898 | /* Enter all arguments in table of locals */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3899 | for (i = 0, narg = 0; i < nch; i++) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3900 | node *ch = CHILD(n, i); |
| 3901 | node *fp; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3902 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3903 | break; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3904 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3905 | fp = CHILD(ch, 0); |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 3906 | if (TYPE(fp) != NAME) { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 3907 | PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i); |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 3908 | complex = 1; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3909 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3910 | narg++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3911 | /* all name updates handled by symtable */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3912 | if (++i >= nch) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3913 | break; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3914 | ch = CHILD(n, i); |
| 3915 | if (TYPE(ch) == EQUAL) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3916 | i += 2; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3917 | else |
| 3918 | REQ(ch, COMMA); |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3919 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3920 | if (complex) { |
| 3921 | /* Generate code for complex arguments only after |
| 3922 | having counted the simple arguments */ |
| 3923 | int ilocal = 0; |
| 3924 | for (i = 0; i < nch; i++) { |
| 3925 | node *ch = CHILD(n, i); |
| 3926 | node *fp; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3927 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3928 | break; |
| 3929 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3930 | fp = CHILD(ch, 0); |
| 3931 | if (TYPE(fp) != NAME) { |
| 3932 | com_addoparg(c, LOAD_FAST, ilocal); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3933 | com_push(c, 1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3934 | com_fpdef(c, ch); |
| 3935 | } |
| 3936 | ilocal++; |
| 3937 | if (++i >= nch) |
| 3938 | break; |
| 3939 | ch = CHILD(n, i); |
| 3940 | if (TYPE(ch) == EQUAL) |
| 3941 | i += 2; |
| 3942 | else |
| 3943 | REQ(ch, COMMA); |
| 3944 | } |
| 3945 | } |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3946 | } |
| 3947 | |
| 3948 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3949 | com_file_input(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3950 | { |
| 3951 | int i; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3952 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3953 | REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3954 | doc = get_docstring(c, n); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3955 | if (doc != NULL) { |
| 3956 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3957 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3958 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3959 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3960 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3961 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3962 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3963 | for (i = 0; i < NCH(n); i++) { |
| 3964 | node *ch = CHILD(n, i); |
| 3965 | if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE) |
| 3966 | com_node(c, ch); |
| 3967 | } |
| 3968 | } |
| 3969 | |
| 3970 | /* Top-level compile-node interface */ |
| 3971 | |
| 3972 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3973 | compile_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3974 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3975 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3976 | node *ch; |
| 3977 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3978 | c->c_name = STR(CHILD(n, 1)); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3979 | doc = get_docstring(c, CHILD(n, 4)); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3980 | if (doc != NULL) { |
| 3981 | (void) com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3982 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3983 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3984 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3985 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3986 | ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */ |
| 3987 | ch = CHILD(ch, 1); /* ')' | varargslist */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3988 | if (TYPE(ch) == varargslist) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3989 | com_arglist(c, ch); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3990 | c->c_infunction = 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3991 | com_node(c, CHILD(n, 4)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3992 | c->c_infunction = 0; |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 3993 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
| 3994 | com_push(c, 1); |
| 3995 | com_addbyte(c, RETURN_VALUE); |
| 3996 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3997 | } |
| 3998 | |
| 3999 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4000 | compile_lambdef(struct compiling *c, node *n) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 4001 | { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4002 | node *ch; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4003 | REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */ |
Guido van Rossum | 363ac7d | 1994-11-10 22:40:34 +0000 | [diff] [blame] | 4004 | c->c_name = "<lambda>"; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4005 | |
| 4006 | ch = CHILD(n, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4007 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4008 | if (TYPE(ch) == varargslist) { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4009 | com_arglist(c, ch); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4010 | ch = CHILD(n, 3); |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4011 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4012 | else |
| 4013 | ch = CHILD(n, 2); |
| 4014 | com_node(c, ch); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 4015 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4016 | com_pop(c, 1); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 4017 | } |
| 4018 | |
| 4019 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4020 | compile_classdef(struct compiling *c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4021 | { |
| 4022 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4023 | PyObject *doc; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4024 | REQ(n, classdef); |
| 4025 | /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ |
| 4026 | c->c_name = STR(CHILD(n, 1)); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4027 | c->c_private = c->c_name; |
Guido van Rossum | 340cbe7 | 2002-01-15 21:06:07 +0000 | [diff] [blame] | 4028 | /* Initialize local __module__ from global __name__ */ |
| 4029 | com_addop_name(c, LOAD_GLOBAL, "__name__"); |
| 4030 | com_addop_name(c, STORE_NAME, "__module__"); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4031 | ch = CHILD(n, NCH(n)-1); /* The suite */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4032 | doc = get_docstring(c, ch); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4033 | if (doc != NULL) { |
| 4034 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4035 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4036 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4037 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4038 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4039 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4040 | } |
| 4041 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4042 | (void) com_addconst(c, Py_None); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4043 | com_node(c, ch); |
| 4044 | com_addbyte(c, LOAD_LOCALS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4045 | com_push(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4046 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4047 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4048 | } |
| 4049 | |
| 4050 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4051 | compile_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4052 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4053 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 4054 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4055 | switch (TYPE(n)) { |
| 4056 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4057 | case single_input: /* One interactive command */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4058 | /* NEWLINE | simple_stmt | compound_stmt NEWLINE */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 4059 | c->c_interactive++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4060 | n = CHILD(n, 0); |
| 4061 | if (TYPE(n) != NEWLINE) |
| 4062 | com_node(c, n); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4063 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4064 | com_push(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4065 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4066 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 4067 | c->c_interactive--; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4068 | break; |
| 4069 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4070 | case file_input: /* A whole file, or built-in function exec() */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4071 | com_file_input(c, n); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4072 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4073 | com_push(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4074 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4075 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4076 | break; |
| 4077 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4078 | case eval_input: /* Built-in function input() */ |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4079 | com_node(c, CHILD(n, 0)); |
| 4080 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4081 | com_pop(c, 1); |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4082 | break; |
| 4083 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4084 | case lambdef: /* anonymous function definition */ |
| 4085 | compile_lambdef(c, n); |
| 4086 | break; |
| 4087 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4088 | case funcdef: /* A function definition */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4089 | compile_funcdef(c, n); |
| 4090 | break; |
| 4091 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4092 | case classdef: /* A class definition */ |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4093 | compile_classdef(c, n); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 4094 | break; |
| 4095 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4096 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4097 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 4098 | "compile_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4099 | } |
| 4100 | } |
| 4101 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4102 | static PyObject * |
| 4103 | dict_keys_inorder(PyObject *dict, int offset) |
| 4104 | { |
| 4105 | PyObject *tuple, *k, *v; |
| 4106 | int i, pos = 0, size = PyDict_Size(dict); |
| 4107 | |
| 4108 | tuple = PyTuple_New(size); |
| 4109 | if (tuple == NULL) |
| 4110 | return NULL; |
| 4111 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 4112 | i = PyInt_AS_LONG(v); |
| 4113 | Py_INCREF(k); |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4114 | assert((i - offset) < size); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4115 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 4116 | } |
| 4117 | return tuple; |
| 4118 | } |
| 4119 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4120 | PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4121 | PyNode_Compile(node *n, char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4122 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4123 | return PyNode_CompileFlags(n, filename, NULL); |
| 4124 | } |
| 4125 | |
| 4126 | PyCodeObject * |
| 4127 | PyNode_CompileFlags(node *n, char *filename, PyCompilerFlags *flags) |
| 4128 | { |
| 4129 | return jcompile(n, filename, NULL, flags); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4130 | } |
| 4131 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4132 | struct symtable * |
| 4133 | PyNode_CompileSymtable(node *n, char *filename) |
| 4134 | { |
| 4135 | struct symtable *st; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4136 | PyFutureFeatures *ff; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4137 | |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4138 | ff = PyNode_Future(n, filename); |
| 4139 | if (ff == NULL) |
| 4140 | return NULL; |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4141 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4142 | st = symtable_init(); |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4143 | if (st == NULL) { |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4144 | PyObject_FREE((void *)ff); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4145 | return NULL; |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4146 | } |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4147 | st->st_future = ff; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4148 | symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4149 | if (st->st_errors > 0) |
| 4150 | goto fail; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4151 | symtable_node(st, n); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4152 | if (st->st_errors > 0) |
| 4153 | goto fail; |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4154 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4155 | return st; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4156 | fail: |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4157 | PyObject_FREE((void *)ff); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4158 | st->st_future = NULL; |
| 4159 | PySymtable_Free(st); |
| 4160 | return NULL; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4161 | } |
| 4162 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4163 | static PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4164 | icompile(node *n, struct compiling *base) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4165 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4166 | return jcompile(n, base->c_filename, base, NULL); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4167 | } |
| 4168 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4169 | static PyCodeObject * |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4170 | jcompile(node *n, char *filename, struct compiling *base, |
| 4171 | PyCompilerFlags *flags) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4172 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4173 | struct compiling sc; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4174 | PyCodeObject *co; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4175 | if (!com_init(&sc, filename)) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4176 | return NULL; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 4177 | if (TYPE(n) == encoding_decl) { |
| 4178 | sc.c_encoding = STR(n); |
| 4179 | n = CHILD(n, 0); |
| 4180 | } else { |
| 4181 | sc.c_encoding = NULL; |
| 4182 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4183 | if (base) { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4184 | sc.c_private = base->c_private; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4185 | sc.c_symtable = base->c_symtable; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4186 | /* c_symtable still points to parent's symbols */ |
| 4187 | if (base->c_nested |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4188 | || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4189 | sc.c_nested = 1; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 4190 | sc.c_flags |= base->c_flags & PyCF_MASK; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 4191 | if (base->c_encoding != NULL) { |
| 4192 | assert(sc.c_encoding == NULL); |
| 4193 | sc.c_encoding = base->c_encoding; |
| 4194 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4195 | } else { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4196 | sc.c_private = NULL; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 4197 | sc.c_future = PyNode_Future(n, filename); |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4198 | if (sc.c_future == NULL) { |
| 4199 | com_free(&sc); |
| 4200 | return NULL; |
| 4201 | } |
Jeremy Hylton | 481081e | 2001-08-14 20:01:59 +0000 | [diff] [blame] | 4202 | if (flags) { |
| 4203 | int merged = sc.c_future->ff_features | |
| 4204 | flags->cf_flags; |
| 4205 | sc.c_future->ff_features = merged; |
| 4206 | flags->cf_flags = merged; |
| 4207 | } |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4208 | if (symtable_build(&sc, n) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4209 | com_free(&sc); |
| 4210 | return NULL; |
| 4211 | } |
| 4212 | } |
| 4213 | co = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4214 | if (symtable_load_symbols(&sc) < 0) { |
| 4215 | sc.c_errors++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4216 | goto exit; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4217 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4218 | compile_node(&sc, n); |
| 4219 | com_done(&sc); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 4220 | if (sc.c_errors == 0) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4221 | PyObject *consts, *names, *varnames, *filename, *name, |
| 4222 | *freevars, *cellvars; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4223 | consts = PyList_AsTuple(sc.c_consts); |
| 4224 | names = PyList_AsTuple(sc.c_names); |
| 4225 | varnames = PyList_AsTuple(sc.c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4226 | cellvars = dict_keys_inorder(sc.c_cellvars, 0); |
| 4227 | freevars = dict_keys_inorder(sc.c_freevars, |
| 4228 | PyTuple_GET_SIZE(cellvars)); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 4229 | filename = PyString_InternFromString(sc.c_filename); |
| 4230 | name = PyString_InternFromString(sc.c_name); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4231 | if (!PyErr_Occurred()) |
| 4232 | co = PyCode_New(sc.c_argcount, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4233 | sc.c_nlocals, |
| 4234 | sc.c_maxstacklevel, |
| 4235 | sc.c_flags, |
| 4236 | sc.c_code, |
| 4237 | consts, |
| 4238 | names, |
| 4239 | varnames, |
| 4240 | freevars, |
| 4241 | cellvars, |
| 4242 | filename, |
| 4243 | name, |
| 4244 | sc.c_firstlineno, |
| 4245 | sc.c_lnotab); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4246 | Py_XDECREF(consts); |
| 4247 | Py_XDECREF(names); |
| 4248 | Py_XDECREF(varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4249 | Py_XDECREF(freevars); |
| 4250 | Py_XDECREF(cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4251 | Py_XDECREF(filename); |
| 4252 | Py_XDECREF(name); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 4253 | } |
Guido van Rossum | 6c2f0c7 | 2000-08-07 19:22:43 +0000 | [diff] [blame] | 4254 | else if (!PyErr_Occurred()) { |
| 4255 | /* This could happen if someone called PyErr_Clear() after an |
| 4256 | error was reported above. That's not supposed to happen, |
| 4257 | but I just plugged one case and I'm not sure there can't be |
| 4258 | others. In that case, raise SystemError so that at least |
| 4259 | it gets reported instead dumping core. */ |
| 4260 | PyErr_SetString(PyExc_SystemError, "lost syntax error"); |
| 4261 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4262 | exit: |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4263 | if (base == NULL) { |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4264 | PySymtable_Free(sc.c_symtable); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4265 | sc.c_symtable = NULL; |
| 4266 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4267 | com_free(&sc); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4268 | return co; |
| 4269 | } |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4270 | |
| 4271 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4272 | PyCode_Addr2Line(PyCodeObject *co, int addrq) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4273 | { |
| 4274 | int size = PyString_Size(co->co_lnotab) / 2; |
Guido van Rossum | 2174dcb | 1999-09-15 22:48:09 +0000 | [diff] [blame] | 4275 | unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4276 | int line = co->co_firstlineno; |
| 4277 | int addr = 0; |
| 4278 | while (--size >= 0) { |
| 4279 | addr += *p++; |
| 4280 | if (addr > addrq) |
| 4281 | break; |
| 4282 | line += *p++; |
| 4283 | } |
| 4284 | return line; |
| 4285 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4286 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4287 | /* The test for LOCAL must come before the test for FREE in order to |
| 4288 | handle classes where name is both local and free. The local var is |
| 4289 | a method and the free var is a free var referenced within a method. |
| 4290 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4291 | |
| 4292 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4293 | get_ref_type(struct compiling *c, char *name) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4294 | { |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4295 | char buf[350]; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4296 | PyObject *v; |
Jeremy Hylton | 5125773 | 2001-03-01 00:42:55 +0000 | [diff] [blame] | 4297 | |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4298 | if (PyDict_GetItemString(c->c_cellvars, name) != NULL) |
| 4299 | return CELL; |
| 4300 | if (PyDict_GetItemString(c->c_locals, name) != NULL) |
| 4301 | return LOCAL; |
| 4302 | if (PyDict_GetItemString(c->c_freevars, name) != NULL) |
| 4303 | return FREE; |
| 4304 | v = PyDict_GetItemString(c->c_globals, name); |
| 4305 | if (v) { |
| 4306 | if (v == Py_None) |
| 4307 | return GLOBAL_EXPLICIT; |
| 4308 | else { |
| 4309 | return GLOBAL_IMPLICIT; |
| 4310 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4311 | } |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 4312 | PyOS_snprintf(buf, sizeof(buf), |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4313 | "unknown scope for %.100s in %.100s(%s) " |
| 4314 | "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n", |
| 4315 | name, c->c_name, |
| 4316 | PyObject_REPR(c->c_symtable->st_cur->ste_id), |
| 4317 | c->c_filename, |
| 4318 | PyObject_REPR(c->c_symtable->st_cur->ste_symbols), |
| 4319 | PyObject_REPR(c->c_locals), |
| 4320 | PyObject_REPR(c->c_globals) |
| 4321 | ); |
| 4322 | |
| 4323 | Py_FatalError(buf); |
| 4324 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4325 | } |
| 4326 | |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4327 | /* Helper functions to issue warnings */ |
| 4328 | |
| 4329 | static int |
| 4330 | issue_warning(char *msg, char *filename, int lineno) |
| 4331 | { |
| 4332 | if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename, |
| 4333 | lineno, NULL, NULL) < 0) { |
| 4334 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
| 4335 | PyErr_SetString(PyExc_SyntaxError, msg); |
| 4336 | PyErr_SyntaxLocation(filename, lineno); |
| 4337 | } |
| 4338 | return -1; |
| 4339 | } |
| 4340 | return 0; |
| 4341 | } |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4342 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4343 | static int |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4344 | symtable_warn(struct symtable *st, char *msg) |
| 4345 | { |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4346 | if (issue_warning(msg, st->st_filename, st->st_cur->ste_lineno) < 0) { |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4347 | st->st_errors++; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4348 | return -1; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4349 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4350 | return 0; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4351 | } |
| 4352 | |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 4353 | /* Helper function for setting lineno and filename */ |
| 4354 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4355 | static int |
| 4356 | symtable_build(struct compiling *c, node *n) |
| 4357 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4358 | if ((c->c_symtable = symtable_init()) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4359 | return -1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 4360 | c->c_symtable->st_future = c->c_future; |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4361 | c->c_symtable->st_filename = c->c_filename; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4362 | symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno); |
| 4363 | if (c->c_symtable->st_errors > 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4364 | return -1; |
| 4365 | symtable_node(c->c_symtable, n); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4366 | if (c->c_symtable->st_errors > 0) |
| 4367 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4368 | /* reset for second pass */ |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4369 | c->c_symtable->st_nscopes = 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4370 | c->c_symtable->st_pass = 2; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4371 | return 0; |
| 4372 | } |
| 4373 | |
| 4374 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4375 | symtable_init_compiling_symbols(struct compiling *c) |
| 4376 | { |
| 4377 | PyObject *varnames; |
| 4378 | |
| 4379 | varnames = c->c_symtable->st_cur->ste_varnames; |
| 4380 | if (varnames == NULL) { |
| 4381 | varnames = PyList_New(0); |
| 4382 | if (varnames == NULL) |
| 4383 | return -1; |
| 4384 | c->c_symtable->st_cur->ste_varnames = varnames; |
| 4385 | Py_INCREF(varnames); |
| 4386 | } else |
| 4387 | Py_INCREF(varnames); |
| 4388 | c->c_varnames = varnames; |
| 4389 | |
| 4390 | c->c_globals = PyDict_New(); |
| 4391 | if (c->c_globals == NULL) |
| 4392 | return -1; |
| 4393 | c->c_freevars = PyDict_New(); |
| 4394 | if (c->c_freevars == NULL) |
| 4395 | return -1; |
| 4396 | c->c_cellvars = PyDict_New(); |
| 4397 | if (c->c_cellvars == NULL) |
| 4398 | return -1; |
| 4399 | return 0; |
| 4400 | } |
| 4401 | |
| 4402 | struct symbol_info { |
| 4403 | int si_nlocals; |
| 4404 | int si_ncells; |
| 4405 | int si_nfrees; |
| 4406 | int si_nimplicit; |
| 4407 | }; |
| 4408 | |
| 4409 | static void |
| 4410 | symtable_init_info(struct symbol_info *si) |
| 4411 | { |
| 4412 | si->si_nlocals = 0; |
| 4413 | si->si_ncells = 0; |
| 4414 | si->si_nfrees = 0; |
| 4415 | si->si_nimplicit = 0; |
| 4416 | } |
| 4417 | |
| 4418 | static int |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4419 | symtable_resolve_free(struct compiling *c, PyObject *name, int flags, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4420 | struct symbol_info *si) |
| 4421 | { |
| 4422 | PyObject *dict, *v; |
| 4423 | |
| 4424 | /* Seperate logic for DEF_FREE. If it occurs in a function, |
| 4425 | it indicates a local that we must allocate storage for (a |
| 4426 | cell var). If it occurs in a class, then the class has a |
| 4427 | method and a free variable with the same name. |
| 4428 | */ |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4429 | if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4430 | /* If it isn't declared locally, it can't be a cell. */ |
| 4431 | if (!(flags & (DEF_LOCAL | DEF_PARAM))) |
| 4432 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4433 | v = PyInt_FromLong(si->si_ncells++); |
| 4434 | dict = c->c_cellvars; |
| 4435 | } else { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4436 | /* If it is free anyway, then there is no need to do |
| 4437 | anything here. |
| 4438 | */ |
| 4439 | if (is_free(flags ^ DEF_FREE_CLASS) |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4440 | || (flags == DEF_FREE_CLASS)) |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4441 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4442 | v = PyInt_FromLong(si->si_nfrees++); |
| 4443 | dict = c->c_freevars; |
| 4444 | } |
| 4445 | if (v == NULL) |
| 4446 | return -1; |
| 4447 | if (PyDict_SetItem(dict, name, v) < 0) { |
| 4448 | Py_DECREF(v); |
| 4449 | return -1; |
| 4450 | } |
| 4451 | Py_DECREF(v); |
| 4452 | return 0; |
| 4453 | } |
| 4454 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4455 | /* If a variable is a cell and an argument, make sure that appears in |
| 4456 | co_cellvars before any variable to its right in varnames. |
| 4457 | */ |
| 4458 | |
| 4459 | |
| 4460 | static int |
| 4461 | symtable_cellvar_offsets(PyObject **cellvars, int argcount, |
| 4462 | PyObject *varnames, int flags) |
| 4463 | { |
| 4464 | PyObject *v, *w, *d, *list = NULL; |
| 4465 | int i, pos; |
| 4466 | |
| 4467 | if (flags & CO_VARARGS) |
| 4468 | argcount++; |
| 4469 | if (flags & CO_VARKEYWORDS) |
| 4470 | argcount++; |
| 4471 | for (i = argcount; --i >= 0; ) { |
| 4472 | v = PyList_GET_ITEM(varnames, i); |
| 4473 | if (PyDict_GetItem(*cellvars, v)) { |
| 4474 | if (list == NULL) { |
| 4475 | list = PyList_New(1); |
| 4476 | if (list == NULL) |
| 4477 | return -1; |
| 4478 | PyList_SET_ITEM(list, 0, v); |
| 4479 | Py_INCREF(v); |
| 4480 | } else |
| 4481 | PyList_Insert(list, 0, v); |
| 4482 | } |
| 4483 | } |
| 4484 | if (list == NULL || PyList_GET_SIZE(list) == 0) |
| 4485 | return 0; |
| 4486 | /* There are cellvars that are also arguments. Create a dict |
| 4487 | to replace cellvars and put the args at the front. |
| 4488 | */ |
| 4489 | d = PyDict_New(); |
| 4490 | for (i = PyList_GET_SIZE(list); --i >= 0; ) { |
| 4491 | v = PyInt_FromLong(i); |
| 4492 | if (v == NULL) |
| 4493 | goto fail; |
| 4494 | if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0) |
| 4495 | goto fail; |
| 4496 | if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0) |
| 4497 | goto fail; |
| 4498 | } |
| 4499 | pos = 0; |
| 4500 | i = PyList_GET_SIZE(list); |
| 4501 | Py_DECREF(list); |
| 4502 | while (PyDict_Next(*cellvars, &pos, &v, &w)) { |
| 4503 | w = PyInt_FromLong(i++); /* don't care about the old key */ |
| 4504 | if (PyDict_SetItem(d, v, w) < 0) { |
| 4505 | Py_DECREF(w); |
| 4506 | goto fail; |
| 4507 | } |
| 4508 | Py_DECREF(w); |
| 4509 | } |
| 4510 | Py_DECREF(*cellvars); |
| 4511 | *cellvars = d; |
| 4512 | return 1; |
| 4513 | fail: |
| 4514 | Py_DECREF(d); |
| 4515 | return -1; |
| 4516 | } |
| 4517 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4518 | static int |
| 4519 | symtable_freevar_offsets(PyObject *freevars, int offset) |
| 4520 | { |
| 4521 | PyObject *name, *v; |
| 4522 | int pos; |
| 4523 | |
| 4524 | /* The cell vars are the first elements of the closure, |
| 4525 | followed by the free vars. Update the offsets in |
| 4526 | c_freevars to account for number of cellvars. */ |
| 4527 | pos = 0; |
| 4528 | while (PyDict_Next(freevars, &pos, &name, &v)) { |
| 4529 | int i = PyInt_AS_LONG(v) + offset; |
| 4530 | PyObject *o = PyInt_FromLong(i); |
| 4531 | if (o == NULL) |
| 4532 | return -1; |
| 4533 | if (PyDict_SetItem(freevars, name, o) < 0) { |
| 4534 | Py_DECREF(o); |
| 4535 | return -1; |
| 4536 | } |
| 4537 | Py_DECREF(o); |
| 4538 | } |
| 4539 | return 0; |
| 4540 | } |
| 4541 | |
| 4542 | static int |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4543 | symtable_check_unoptimized(struct compiling *c, |
| 4544 | PySymtableEntryObject *ste, |
| 4545 | struct symbol_info *si) |
| 4546 | { |
| 4547 | char buf[300]; |
| 4548 | |
| 4549 | if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free |
| 4550 | || (ste->ste_nested && si->si_nimplicit))) |
| 4551 | return 0; |
| 4552 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4553 | #define ILLEGAL_CONTAINS "contains a nested function with free variables" |
| 4554 | |
| 4555 | #define ILLEGAL_IS "is a nested function" |
| 4556 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4557 | #define ILLEGAL_IMPORT_STAR \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4558 | "import * is not allowed in function '%.100s' because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4559 | |
| 4560 | #define ILLEGAL_BARE_EXEC \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4561 | "unqualified exec is not allowed in function '%.100s' it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4562 | |
| 4563 | #define ILLEGAL_EXEC_AND_IMPORT_STAR \ |
Neal Norwitz | 150d09d | 2002-01-29 00:56:37 +0000 | [diff] [blame] | 4564 | "function '%.100s' uses import * and bare exec, which are illegal " \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4565 | "because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4566 | |
| 4567 | /* XXX perhaps the linenos for these opt-breaking statements |
| 4568 | should be stored so the exception can point to them. */ |
| 4569 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4570 | if (ste->ste_child_free) { |
| 4571 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4572 | PyOS_snprintf(buf, sizeof(buf), |
| 4573 | ILLEGAL_IMPORT_STAR, |
| 4574 | PyString_AS_STRING(ste->ste_name), |
| 4575 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4576 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4577 | PyOS_snprintf(buf, sizeof(buf), |
| 4578 | ILLEGAL_BARE_EXEC, |
| 4579 | PyString_AS_STRING(ste->ste_name), |
| 4580 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4581 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4582 | PyOS_snprintf(buf, sizeof(buf), |
| 4583 | ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 4584 | PyString_AS_STRING(ste->ste_name), |
| 4585 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4586 | } |
| 4587 | } else { |
| 4588 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4589 | PyOS_snprintf(buf, sizeof(buf), |
| 4590 | ILLEGAL_IMPORT_STAR, |
| 4591 | PyString_AS_STRING(ste->ste_name), |
| 4592 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4593 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4594 | PyOS_snprintf(buf, sizeof(buf), |
| 4595 | ILLEGAL_BARE_EXEC, |
| 4596 | PyString_AS_STRING(ste->ste_name), |
| 4597 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4598 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4599 | PyOS_snprintf(buf, sizeof(buf), |
| 4600 | ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 4601 | PyString_AS_STRING(ste->ste_name), |
| 4602 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4603 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4604 | } |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4605 | |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4606 | PyErr_SetString(PyExc_SyntaxError, buf); |
| 4607 | PyErr_SyntaxLocation(c->c_symtable->st_filename, |
| 4608 | ste->ste_opt_lineno); |
| 4609 | return -1; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4610 | } |
| 4611 | |
| 4612 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4613 | symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste, |
| 4614 | struct symbol_info *si) |
| 4615 | { |
Jeremy Hylton | b857ba2 | 2001-08-10 21:41:33 +0000 | [diff] [blame] | 4616 | if (c->c_future) |
| 4617 | c->c_flags |= c->c_future->ff_features; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4618 | if (ste->ste_generator) |
| 4619 | c->c_flags |= CO_GENERATOR; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4620 | if (ste->ste_type != TYPE_MODULE) |
| 4621 | c->c_flags |= CO_NEWLOCALS; |
| 4622 | if (ste->ste_type == TYPE_FUNCTION) { |
| 4623 | c->c_nlocals = si->si_nlocals; |
| 4624 | if (ste->ste_optimized == 0) |
| 4625 | c->c_flags |= CO_OPTIMIZED; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4626 | else if (ste->ste_optimized != OPT_EXEC) |
| 4627 | return symtable_check_unoptimized(c, ste, si); |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4628 | } |
| 4629 | return 0; |
| 4630 | } |
| 4631 | |
| 4632 | static int |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4633 | symtable_load_symbols(struct compiling *c) |
| 4634 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4635 | static PyObject *implicit = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4636 | struct symtable *st = c->c_symtable; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4637 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4638 | PyObject *name, *varnames, *v; |
| 4639 | int i, flags, pos; |
| 4640 | struct symbol_info si; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4641 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4642 | if (implicit == NULL) { |
| 4643 | implicit = PyInt_FromLong(1); |
| 4644 | if (implicit == NULL) |
| 4645 | return -1; |
| 4646 | } |
| 4647 | v = NULL; |
| 4648 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4649 | if (symtable_init_compiling_symbols(c) < 0) |
| 4650 | goto fail; |
| 4651 | symtable_init_info(&si); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4652 | varnames = st->st_cur->ste_varnames; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4653 | si.si_nlocals = PyList_GET_SIZE(varnames); |
| 4654 | c->c_argcount = si.si_nlocals; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4655 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4656 | for (i = 0; i < si.si_nlocals; ++i) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4657 | v = PyInt_FromLong(i); |
| 4658 | if (PyDict_SetItem(c->c_locals, |
| 4659 | PyList_GET_ITEM(varnames, i), v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4660 | goto fail; |
| 4661 | Py_DECREF(v); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4662 | } |
| 4663 | |
| 4664 | /* XXX The cases below define the rules for whether a name is |
| 4665 | local or global. The logic could probably be clearer. */ |
| 4666 | pos = 0; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4667 | while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) { |
| 4668 | flags = PyInt_AS_LONG(v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4669 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4670 | if (flags & DEF_FREE_GLOBAL) |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4671 | /* undo the original DEF_FREE */ |
| 4672 | flags &= ~(DEF_FREE | DEF_FREE_CLASS); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4673 | |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4674 | /* Deal with names that need two actions: |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4675 | 1. Cell variables that are also locals. |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4676 | 2. Free variables in methods that are also class |
| 4677 | variables or declared global. |
| 4678 | */ |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4679 | if (flags & (DEF_FREE | DEF_FREE_CLASS)) |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4680 | symtable_resolve_free(c, name, flags, &si); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4681 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4682 | if (flags & DEF_STAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4683 | c->c_argcount--; |
| 4684 | c->c_flags |= CO_VARARGS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4685 | } else if (flags & DEF_DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4686 | c->c_argcount--; |
| 4687 | c->c_flags |= CO_VARKEYWORDS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4688 | } else if (flags & DEF_INTUPLE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4689 | c->c_argcount--; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4690 | else if (flags & DEF_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4691 | if (flags & DEF_PARAM) { |
| 4692 | PyErr_Format(PyExc_SyntaxError, LOCAL_GLOBAL, |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4693 | PyString_AS_STRING(name)); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 4694 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4695 | ste->ste_lineno); |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4696 | st->st_errors++; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4697 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4698 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4699 | if (PyDict_SetItem(c->c_globals, name, Py_None) < 0) |
| 4700 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4701 | } else if (flags & DEF_FREE_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4702 | si.si_nimplicit++; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4703 | if (PyDict_SetItem(c->c_globals, name, implicit) < 0) |
| 4704 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4705 | } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4706 | v = PyInt_FromLong(si.si_nlocals++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4707 | if (v == NULL) |
| 4708 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4709 | if (PyDict_SetItem(c->c_locals, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4710 | goto fail; |
| 4711 | Py_DECREF(v); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4712 | if (ste->ste_type != TYPE_CLASS) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4713 | if (PyList_Append(c->c_varnames, name) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4714 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4715 | } else if (is_free(flags)) { |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4716 | if (ste->ste_nested) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4717 | v = PyInt_FromLong(si.si_nfrees++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4718 | if (v == NULL) |
| 4719 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4720 | if (PyDict_SetItem(c->c_freevars, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4721 | goto fail; |
| 4722 | Py_DECREF(v); |
| 4723 | } else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4724 | si.si_nimplicit++; |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4725 | if (PyDict_SetItem(c->c_globals, name, |
| 4726 | implicit) < 0) |
| 4727 | goto fail; |
| 4728 | if (st->st_nscopes != 1) { |
| 4729 | v = PyInt_FromLong(flags); |
| 4730 | if (PyDict_SetItem(st->st_global, |
| 4731 | name, v)) |
| 4732 | goto fail; |
| 4733 | Py_DECREF(v); |
| 4734 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4735 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4736 | } |
| 4737 | } |
| 4738 | |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4739 | assert(PyDict_Size(c->c_freevars) == si.si_nfrees); |
| 4740 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4741 | if (si.si_ncells > 1) { /* one cell is always in order */ |
| 4742 | if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount, |
| 4743 | c->c_varnames, c->c_flags) < 0) |
| 4744 | return -1; |
| 4745 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4746 | if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0) |
| 4747 | return -1; |
| 4748 | return symtable_update_flags(c, ste, &si); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4749 | fail: |
| 4750 | /* is this always the right thing to do? */ |
| 4751 | Py_XDECREF(v); |
| 4752 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4753 | } |
| 4754 | |
| 4755 | static struct symtable * |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4756 | symtable_init() |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4757 | { |
| 4758 | struct symtable *st; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4759 | |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4760 | st = (struct symtable *)PyObject_MALLOC(sizeof(struct symtable)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4761 | if (st == NULL) |
| 4762 | return NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4763 | st->st_pass = 1; |
Tim Peters | ff1f852 | 2001-08-11 01:06:35 +0000 | [diff] [blame] | 4764 | |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4765 | st->st_filename = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4766 | if ((st->st_stack = PyList_New(0)) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4767 | goto fail; |
| 4768 | if ((st->st_symbols = PyDict_New()) == NULL) |
| 4769 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4770 | st->st_cur = NULL; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4771 | st->st_nscopes = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4772 | st->st_errors = 0; |
| 4773 | st->st_tmpname = 0; |
| 4774 | st->st_private = NULL; |
| 4775 | return st; |
| 4776 | fail: |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4777 | PySymtable_Free(st); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4778 | return NULL; |
| 4779 | } |
| 4780 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4781 | void |
| 4782 | PySymtable_Free(struct symtable *st) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4783 | { |
| 4784 | Py_XDECREF(st->st_symbols); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4785 | Py_XDECREF(st->st_stack); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4786 | Py_XDECREF(st->st_cur); |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4787 | PyObject_FREE((void *)st); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4788 | } |
| 4789 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4790 | /* When the compiler exits a scope, it must should update the scope's |
| 4791 | free variable information with the list of free variables in its |
| 4792 | children. |
| 4793 | |
| 4794 | Variables that are free in children and defined in the current |
| 4795 | scope are cellvars. |
| 4796 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4797 | If the scope being exited is defined at the top-level (ste_nested is |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4798 | false), free variables in children that are not defined here are |
| 4799 | implicit globals. |
| 4800 | |
| 4801 | */ |
| 4802 | |
| 4803 | static int |
| 4804 | symtable_update_free_vars(struct symtable *st) |
| 4805 | { |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4806 | int i, j, def; |
| 4807 | PyObject *o, *name, *list = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4808 | PySymtableEntryObject *child, *ste = st->st_cur; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4809 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4810 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4811 | def = DEF_FREE_CLASS; |
| 4812 | else |
| 4813 | def = DEF_FREE; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4814 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4815 | int pos = 0; |
| 4816 | |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4817 | if (list) |
| 4818 | PyList_SetSlice(list, 0, |
| 4819 | ((PyVarObject*)list)->ob_size, 0); |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4820 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4821 | PyList_GET_ITEM(ste->ste_children, i); |
| 4822 | while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4823 | int flags = PyInt_AS_LONG(o); |
| 4824 | if (!(is_free(flags))) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4825 | continue; /* avoids indentation */ |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4826 | if (list == NULL) { |
| 4827 | list = PyList_New(0); |
| 4828 | if (list == NULL) |
| 4829 | return -1; |
| 4830 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4831 | ste->ste_child_free = 1; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4832 | if (PyList_Append(list, name) < 0) { |
| 4833 | Py_DECREF(list); |
| 4834 | return -1; |
| 4835 | } |
| 4836 | } |
| 4837 | for (j = 0; list && j < PyList_GET_SIZE(list); j++) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4838 | PyObject *v; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4839 | name = PyList_GET_ITEM(list, j); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4840 | v = PyDict_GetItem(ste->ste_symbols, name); |
| 4841 | /* If a name N is declared global in scope A and |
| 4842 | referenced in scope B contained (perhaps |
| 4843 | indirectly) in A and there are no scopes |
| 4844 | with bindings for N between B and A, then N |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4845 | is global in B. Unless A is a class scope, |
| 4846 | because class scopes are not considered for |
| 4847 | nested scopes. |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4848 | */ |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4849 | if (v && (ste->ste_type != TYPE_CLASS)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4850 | int flags = PyInt_AS_LONG(v); |
| 4851 | if (flags & DEF_GLOBAL) { |
| 4852 | symtable_undo_free(st, child->ste_id, |
| 4853 | name); |
| 4854 | continue; |
| 4855 | } |
| 4856 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4857 | if (ste->ste_nested) { |
| 4858 | if (symtable_add_def_o(st, ste->ste_symbols, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4859 | name, def) < 0) { |
| 4860 | Py_DECREF(list); |
| 4861 | return -1; |
| 4862 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4863 | } else { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4864 | if (symtable_check_global(st, child->ste_id, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4865 | name) < 0) { |
| 4866 | Py_DECREF(list); |
| 4867 | return -1; |
| 4868 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4869 | } |
| 4870 | } |
| 4871 | } |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4872 | |
| 4873 | Py_XDECREF(list); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4874 | return 0; |
| 4875 | } |
| 4876 | |
| 4877 | /* If the current scope is a non-nested class or if name is not |
| 4878 | defined in the current, non-nested scope, then it is an implicit |
| 4879 | global in all nested scopes. |
| 4880 | */ |
| 4881 | |
| 4882 | static int |
| 4883 | symtable_check_global(struct symtable *st, PyObject *child, PyObject *name) |
| 4884 | { |
| 4885 | PyObject *o; |
| 4886 | int v; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4887 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4888 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4889 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4890 | return symtable_undo_free(st, child, name); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4891 | o = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4892 | if (o == NULL) |
| 4893 | return symtable_undo_free(st, child, name); |
| 4894 | v = PyInt_AS_LONG(o); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4895 | |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4896 | if (is_free(v) || (v & DEF_GLOBAL)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4897 | return symtable_undo_free(st, child, name); |
| 4898 | else |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4899 | return symtable_add_def_o(st, ste->ste_symbols, |
| 4900 | name, DEF_FREE); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4901 | } |
| 4902 | |
| 4903 | static int |
| 4904 | symtable_undo_free(struct symtable *st, PyObject *id, |
| 4905 | PyObject *name) |
| 4906 | { |
| 4907 | int i, v, x; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4908 | PyObject *info; |
| 4909 | PySymtableEntryObject *ste; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4910 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4911 | ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id); |
| 4912 | if (ste == NULL) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4913 | return -1; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4914 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4915 | info = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4916 | if (info == NULL) |
| 4917 | return 0; |
| 4918 | v = PyInt_AS_LONG(info); |
| 4919 | if (is_free(v)) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4920 | if (symtable_add_def_o(st, ste->ste_symbols, name, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4921 | DEF_FREE_GLOBAL) < 0) |
| 4922 | return -1; |
| 4923 | } else |
| 4924 | /* If the name is defined here or declared global, |
| 4925 | then the recursion stops. */ |
| 4926 | return 0; |
| 4927 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4928 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
| 4929 | PySymtableEntryObject *child; |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4930 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4931 | PyList_GET_ITEM(ste->ste_children, i); |
| 4932 | x = symtable_undo_free(st, child->ste_id, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4933 | if (x < 0) |
| 4934 | return x; |
| 4935 | } |
| 4936 | return 0; |
| 4937 | } |
| 4938 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4939 | /* symtable_enter_scope() gets a reference via PySymtableEntry_New(). |
| 4940 | This reference is released when the scope is exited, via the DECREF |
| 4941 | in symtable_exit_scope(). |
| 4942 | */ |
| 4943 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4944 | static int |
| 4945 | symtable_exit_scope(struct symtable *st) |
| 4946 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4947 | int end; |
| 4948 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4949 | if (st->st_pass == 1) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4950 | symtable_update_free_vars(st); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4951 | Py_DECREF(st->st_cur); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4952 | end = PyList_GET_SIZE(st->st_stack) - 1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4953 | st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack, |
| 4954 | end); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4955 | if (PySequence_DelItem(st->st_stack, end) < 0) |
| 4956 | return -1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4957 | return 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4958 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4959 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4960 | static void |
| 4961 | symtable_enter_scope(struct symtable *st, char *name, int type, |
| 4962 | int lineno) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4963 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4964 | PySymtableEntryObject *prev = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4965 | |
| 4966 | if (st->st_cur) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4967 | prev = st->st_cur; |
| 4968 | if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) { |
| 4969 | Py_DECREF(st->st_cur); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4970 | st->st_errors++; |
| 4971 | return; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4972 | } |
| 4973 | } |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4974 | st->st_cur = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4975 | PySymtableEntry_New(st, name, type, lineno); |
| 4976 | if (strcmp(name, TOP) == 0) |
| 4977 | st->st_global = st->st_cur->ste_symbols; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4978 | if (prev && st->st_pass == 1) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4979 | if (PyList_Append(prev->ste_children, |
| 4980 | (PyObject *)st->st_cur) < 0) |
| 4981 | st->st_errors++; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4982 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4983 | } |
| 4984 | |
| 4985 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4986 | symtable_lookup(struct symtable *st, char *name) |
| 4987 | { |
| 4988 | char buffer[MANGLE_LEN]; |
| 4989 | PyObject *v; |
| 4990 | int flags; |
| 4991 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 4992 | if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4993 | name = buffer; |
| 4994 | v = PyDict_GetItemString(st->st_cur->ste_symbols, name); |
| 4995 | if (v == NULL) { |
| 4996 | if (PyErr_Occurred()) |
| 4997 | return -1; |
| 4998 | else |
| 4999 | return 0; |
| 5000 | } |
| 5001 | |
| 5002 | flags = PyInt_AS_LONG(v); |
| 5003 | return flags; |
| 5004 | } |
| 5005 | |
| 5006 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5007 | symtable_add_def(struct symtable *st, char *name, int flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5008 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5009 | PyObject *s; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5010 | char buffer[MANGLE_LEN]; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5011 | int ret; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5012 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 5013 | if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5014 | name = buffer; |
| 5015 | if ((s = PyString_InternFromString(name)) == NULL) |
| 5016 | return -1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5017 | ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag); |
| 5018 | Py_DECREF(s); |
| 5019 | return ret; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5020 | } |
| 5021 | |
| 5022 | /* Must only be called with mangled names */ |
| 5023 | |
| 5024 | static int |
| 5025 | symtable_add_def_o(struct symtable *st, PyObject *dict, |
| 5026 | PyObject *name, int flag) |
| 5027 | { |
| 5028 | PyObject *o; |
| 5029 | int val; |
| 5030 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5031 | if ((o = PyDict_GetItem(dict, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5032 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5033 | if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5034 | PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5035 | PyString_AsString(name)); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5036 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5037 | st->st_cur->ste_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5038 | return -1; |
| 5039 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5040 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5041 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5042 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5043 | o = PyInt_FromLong(val); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5044 | if (PyDict_SetItem(dict, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5045 | Py_DECREF(o); |
| 5046 | return -1; |
| 5047 | } |
| 5048 | Py_DECREF(o); |
| 5049 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5050 | if (flag & DEF_PARAM) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5051 | if (PyList_Append(st->st_cur->ste_varnames, name) < 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5052 | return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5053 | } else if (flag & DEF_GLOBAL) { |
| 5054 | /* XXX need to update DEF_GLOBAL for other flags too; |
| 5055 | perhaps only DEF_FREE_GLOBAL */ |
| 5056 | if ((o = PyDict_GetItem(st->st_global, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5057 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5058 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5059 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5060 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5061 | o = PyInt_FromLong(val); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5062 | if (PyDict_SetItem(st->st_global, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5063 | Py_DECREF(o); |
| 5064 | return -1; |
| 5065 | } |
| 5066 | Py_DECREF(o); |
| 5067 | } |
| 5068 | return 0; |
| 5069 | } |
| 5070 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5071 | #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5072 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 5073 | /* Look for a yield stmt under n. Return 1 if found, else 0. |
| 5074 | This hack is used to look inside "if 0:" blocks (which are normally |
| 5075 | ignored) in case those are the only places a yield occurs (so that this |
| 5076 | function is a generator). */ |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5077 | static int |
| 5078 | look_for_yield(node *n) |
| 5079 | { |
| 5080 | int i; |
| 5081 | |
| 5082 | for (i = 0; i < NCH(n); ++i) { |
| 5083 | node *kid = CHILD(n, i); |
| 5084 | |
| 5085 | switch (TYPE(kid)) { |
| 5086 | |
| 5087 | case classdef: |
| 5088 | case funcdef: |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 5089 | case lambdef: |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5090 | /* Stuff in nested functions and classes can't make |
| 5091 | the parent a generator. */ |
| 5092 | return 0; |
| 5093 | |
| 5094 | case yield_stmt: |
| 5095 | return 1; |
| 5096 | |
| 5097 | default: |
| 5098 | if (look_for_yield(kid)) |
| 5099 | return 1; |
| 5100 | } |
| 5101 | } |
| 5102 | return 0; |
| 5103 | } |
| 5104 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5105 | static void |
| 5106 | symtable_node(struct symtable *st, node *n) |
| 5107 | { |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5108 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5109 | |
| 5110 | loop: |
| 5111 | switch (TYPE(n)) { |
| 5112 | case funcdef: { |
| 5113 | char *func_name = STR(CHILD(n, 1)); |
| 5114 | symtable_add_def(st, func_name, DEF_LOCAL); |
| 5115 | symtable_default_args(st, CHILD(n, 2)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5116 | symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5117 | symtable_funcdef(st, n); |
| 5118 | symtable_exit_scope(st); |
| 5119 | break; |
| 5120 | } |
| 5121 | case lambdef: |
| 5122 | if (NCH(n) == 4) |
| 5123 | symtable_default_args(st, CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5124 | symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5125 | symtable_funcdef(st, n); |
| 5126 | symtable_exit_scope(st); |
| 5127 | break; |
| 5128 | case classdef: { |
| 5129 | char *tmp, *class_name = STR(CHILD(n, 1)); |
| 5130 | symtable_add_def(st, class_name, DEF_LOCAL); |
| 5131 | if (TYPE(CHILD(n, 2)) == LPAR) { |
| 5132 | node *bases = CHILD(n, 3); |
| 5133 | int i; |
| 5134 | for (i = 0; i < NCH(bases); i += 2) { |
| 5135 | symtable_node(st, CHILD(bases, i)); |
| 5136 | } |
| 5137 | } |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5138 | symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5139 | tmp = st->st_private; |
| 5140 | st->st_private = class_name; |
| 5141 | symtable_node(st, CHILD(n, NCH(n) - 1)); |
| 5142 | st->st_private = tmp; |
| 5143 | symtable_exit_scope(st); |
| 5144 | break; |
| 5145 | } |
| 5146 | case if_stmt: |
| 5147 | for (i = 0; i + 3 < NCH(n); i += 4) { |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5148 | if (is_constant_false(NULL, (CHILD(n, i + 1)))) { |
| 5149 | if (st->st_cur->ste_generator == 0) |
| 5150 | st->st_cur->ste_generator = |
| 5151 | look_for_yield(CHILD(n, i+3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5152 | continue; |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5153 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5154 | symtable_node(st, CHILD(n, i + 1)); |
| 5155 | symtable_node(st, CHILD(n, i + 3)); |
| 5156 | } |
| 5157 | if (i + 2 < NCH(n)) |
| 5158 | symtable_node(st, CHILD(n, i + 2)); |
| 5159 | break; |
| 5160 | case global_stmt: |
| 5161 | symtable_global(st, n); |
| 5162 | break; |
| 5163 | case import_stmt: |
| 5164 | symtable_import(st, n); |
| 5165 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5166 | case exec_stmt: { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5167 | st->st_cur->ste_optimized |= OPT_EXEC; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5168 | symtable_node(st, CHILD(n, 1)); |
| 5169 | if (NCH(n) > 2) |
| 5170 | symtable_node(st, CHILD(n, 3)); |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5171 | else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5172 | st->st_cur->ste_optimized |= OPT_BARE_EXEC; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5173 | st->st_cur->ste_opt_lineno = n->n_lineno; |
| 5174 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5175 | if (NCH(n) > 4) |
| 5176 | symtable_node(st, CHILD(n, 5)); |
| 5177 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5178 | |
| 5179 | } |
Jeremy Hylton | 384639f | 2001-02-19 15:50:51 +0000 | [diff] [blame] | 5180 | case assert_stmt: |
| 5181 | if (Py_OptimizeFlag) |
| 5182 | return; |
| 5183 | if (NCH(n) == 2) { |
| 5184 | n = CHILD(n, 1); |
| 5185 | goto loop; |
| 5186 | } else { |
| 5187 | symtable_node(st, CHILD(n, 1)); |
| 5188 | n = CHILD(n, 3); |
| 5189 | goto loop; |
| 5190 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5191 | case except_clause: |
| 5192 | if (NCH(n) == 4) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5193 | symtable_assign(st, CHILD(n, 3), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5194 | if (NCH(n) > 1) { |
| 5195 | n = CHILD(n, 1); |
| 5196 | goto loop; |
| 5197 | } |
| 5198 | break; |
| 5199 | case del_stmt: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5200 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5201 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 5202 | case yield_stmt: |
| 5203 | st->st_cur->ste_generator = 1; |
| 5204 | n = CHILD(n, 1); |
| 5205 | goto loop; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5206 | case expr_stmt: |
| 5207 | if (NCH(n) == 1) |
| 5208 | n = CHILD(n, 0); |
| 5209 | else { |
| 5210 | if (TYPE(CHILD(n, 1)) == augassign) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5211 | symtable_assign(st, CHILD(n, 0), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5212 | symtable_node(st, CHILD(n, 2)); |
| 5213 | break; |
| 5214 | } else { |
| 5215 | int i; |
| 5216 | for (i = 0; i < NCH(n) - 2; i += 2) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5217 | symtable_assign(st, CHILD(n, i), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5218 | n = CHILD(n, NCH(n) - 1); |
| 5219 | } |
| 5220 | } |
| 5221 | goto loop; |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5222 | case list_iter: |
| 5223 | n = CHILD(n, 0); |
| 5224 | if (TYPE(n) == list_for) { |
| 5225 | st->st_tmpname++; |
| 5226 | symtable_list_comprehension(st, n); |
| 5227 | st->st_tmpname--; |
| 5228 | } else { |
| 5229 | REQ(n, list_if); |
| 5230 | symtable_node(st, CHILD(n, 1)); |
| 5231 | if (NCH(n) == 3) { |
| 5232 | n = CHILD(n, 2); |
| 5233 | goto loop; |
| 5234 | } |
| 5235 | } |
| 5236 | break; |
| 5237 | case for_stmt: |
| 5238 | symtable_assign(st, CHILD(n, 1), 0); |
| 5239 | for (i = 3; i < NCH(n); ++i) |
| 5240 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5241 | symtable_node(st, CHILD(n, i)); |
| 5242 | break; |
| 5243 | /* The remaining cases fall through to default except in |
| 5244 | special circumstances. This requires the individual cases |
| 5245 | to be coded with great care, even though they look like |
| 5246 | rather innocuous. Each case must double-check TYPE(n). |
| 5247 | */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5248 | case argument: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5249 | if (TYPE(n) == argument && NCH(n) == 3) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5250 | n = CHILD(n, 2); |
| 5251 | goto loop; |
| 5252 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5253 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5254 | case listmaker: |
| 5255 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5256 | st->st_tmpname++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5257 | symtable_list_comprehension(st, CHILD(n, 1)); |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5258 | symtable_node(st, CHILD(n, 0)); |
| 5259 | st->st_tmpname--; |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5260 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5261 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5262 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5263 | case atom: |
| 5264 | if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) { |
| 5265 | symtable_add_use(st, STR(CHILD(n, 0))); |
| 5266 | break; |
| 5267 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5268 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5269 | default: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5270 | /* Walk over every non-token child with a special case |
| 5271 | for one child. |
| 5272 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5273 | if (NCH(n) == 1) { |
| 5274 | n = CHILD(n, 0); |
| 5275 | goto loop; |
| 5276 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5277 | for (i = 0; i < NCH(n); ++i) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5278 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5279 | symtable_node(st, CHILD(n, i)); |
| 5280 | } |
| 5281 | } |
| 5282 | |
| 5283 | static void |
| 5284 | symtable_funcdef(struct symtable *st, node *n) |
| 5285 | { |
| 5286 | node *body; |
| 5287 | |
| 5288 | if (TYPE(n) == lambdef) { |
| 5289 | if (NCH(n) == 4) |
| 5290 | symtable_params(st, CHILD(n, 1)); |
| 5291 | } else |
| 5292 | symtable_params(st, CHILD(n, 2)); |
| 5293 | body = CHILD(n, NCH(n) - 1); |
| 5294 | symtable_node(st, body); |
| 5295 | } |
| 5296 | |
| 5297 | /* The next two functions parse the argument tuple. |
| 5298 | symtable_default_arg() checks for names in the default arguments, |
| 5299 | which are references in the defining scope. symtable_params() |
| 5300 | parses the parameter names, which are defined in the function's |
| 5301 | body. |
| 5302 | |
| 5303 | varargslist: |
| 5304 | (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) |
| 5305 | | fpdef ['=' test] (',' fpdef ['=' test])* [','] |
| 5306 | */ |
| 5307 | |
| 5308 | static void |
| 5309 | symtable_default_args(struct symtable *st, node *n) |
| 5310 | { |
| 5311 | node *c; |
| 5312 | int i; |
| 5313 | |
| 5314 | if (TYPE(n) == parameters) { |
| 5315 | n = CHILD(n, 1); |
| 5316 | if (TYPE(n) == RPAR) |
| 5317 | return; |
| 5318 | } |
| 5319 | REQ(n, varargslist); |
| 5320 | for (i = 0; i < NCH(n); i += 2) { |
| 5321 | c = CHILD(n, i); |
| 5322 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5323 | break; |
| 5324 | } |
| 5325 | if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL)) |
| 5326 | symtable_node(st, CHILD(n, i)); |
| 5327 | } |
| 5328 | } |
| 5329 | |
| 5330 | static void |
| 5331 | symtable_params(struct symtable *st, node *n) |
| 5332 | { |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5333 | int i, complex = -1, ext = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5334 | node *c = NULL; |
| 5335 | |
| 5336 | if (TYPE(n) == parameters) { |
| 5337 | n = CHILD(n, 1); |
| 5338 | if (TYPE(n) == RPAR) |
| 5339 | return; |
| 5340 | } |
| 5341 | REQ(n, varargslist); |
| 5342 | for (i = 0; i < NCH(n); i += 2) { |
| 5343 | c = CHILD(n, i); |
| 5344 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5345 | ext = 1; |
| 5346 | break; |
| 5347 | } |
| 5348 | if (TYPE(c) == test) { |
| 5349 | continue; |
| 5350 | } |
| 5351 | if (TYPE(CHILD(c, 0)) == NAME) |
| 5352 | symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM); |
| 5353 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5354 | char nbuf[30]; |
| 5355 | PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5356 | symtable_add_def(st, nbuf, DEF_PARAM); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5357 | complex = i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5358 | } |
| 5359 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5360 | if (ext) { |
| 5361 | c = CHILD(n, i); |
| 5362 | if (TYPE(c) == STAR) { |
| 5363 | i++; |
| 5364 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5365 | DEF_PARAM | DEF_STAR); |
| 5366 | i += 2; |
Jeremy Hylton | 1113cfc | 2001-01-23 00:50:52 +0000 | [diff] [blame] | 5367 | if (i >= NCH(n)) |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5368 | c = NULL; |
| 5369 | else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5370 | c = CHILD(n, i); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5371 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5372 | if (c && TYPE(c) == DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5373 | i++; |
| 5374 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5375 | DEF_PARAM | DEF_DOUBLESTAR); |
| 5376 | } |
| 5377 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5378 | if (complex >= 0) { |
| 5379 | int j; |
| 5380 | for (j = 0; j <= complex; j++) { |
| 5381 | c = CHILD(n, j); |
| 5382 | if (TYPE(c) == COMMA) |
Jeremy Hylton | 2b3f0ca | 2001-02-19 23:52:49 +0000 | [diff] [blame] | 5383 | c = CHILD(n, ++j); |
| 5384 | else if (TYPE(c) == EQUAL) |
| 5385 | c = CHILD(n, j += 3); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5386 | if (TYPE(CHILD(c, 0)) == LPAR) |
| 5387 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5388 | } |
| 5389 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5390 | } |
| 5391 | |
| 5392 | static void |
| 5393 | symtable_params_fplist(struct symtable *st, node *n) |
| 5394 | { |
| 5395 | int i; |
| 5396 | node *c; |
| 5397 | |
| 5398 | REQ(n, fplist); |
| 5399 | for (i = 0; i < NCH(n); i += 2) { |
| 5400 | c = CHILD(n, i); |
| 5401 | REQ(c, fpdef); |
| 5402 | if (NCH(c) == 1) |
| 5403 | symtable_add_def(st, STR(CHILD(c, 0)), |
| 5404 | DEF_PARAM | DEF_INTUPLE); |
| 5405 | else |
| 5406 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5407 | } |
| 5408 | |
| 5409 | } |
| 5410 | |
| 5411 | static void |
| 5412 | symtable_global(struct symtable *st, node *n) |
| 5413 | { |
| 5414 | int i; |
| 5415 | |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 5416 | /* XXX It might be helpful to warn about module-level global |
| 5417 | statements, but it's hard to tell the difference between |
| 5418 | module-level and a string passed to exec. |
| 5419 | */ |
Jeremy Hylton | c176132 | 2001-02-28 23:44:45 +0000 | [diff] [blame] | 5420 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5421 | for (i = 1; i < NCH(n); i += 2) { |
| 5422 | char *name = STR(CHILD(n, i)); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5423 | int flags; |
| 5424 | |
| 5425 | flags = symtable_lookup(st, name); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5426 | if (flags < 0) |
| 5427 | continue; |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5428 | if (flags && flags != DEF_GLOBAL) { |
| 5429 | char buf[500]; |
| 5430 | if (flags & DEF_PARAM) { |
| 5431 | PyErr_Format(PyExc_SyntaxError, |
| 5432 | "name '%.400s' is local and global", |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5433 | name); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5434 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5435 | st->st_cur->ste_lineno); |
| 5436 | st->st_errors++; |
| 5437 | return; |
Guido van Rossum | 0bba7f8 | 2001-02-28 21:55:38 +0000 | [diff] [blame] | 5438 | } |
| 5439 | else { |
| 5440 | if (flags & DEF_LOCAL) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5441 | PyOS_snprintf(buf, sizeof(buf), |
| 5442 | GLOBAL_AFTER_ASSIGN, |
| 5443 | name); |
Guido van Rossum | 0bba7f8 | 2001-02-28 21:55:38 +0000 | [diff] [blame] | 5444 | else |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5445 | PyOS_snprintf(buf, sizeof(buf), |
| 5446 | GLOBAL_AFTER_USE, name); |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 5447 | symtable_warn(st, buf); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5448 | } |
| 5449 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5450 | symtable_add_def(st, name, DEF_GLOBAL); |
| 5451 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5452 | } |
| 5453 | |
| 5454 | static void |
| 5455 | symtable_list_comprehension(struct symtable *st, node *n) |
| 5456 | { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5457 | char tmpname[30]; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5458 | |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5459 | PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", st->st_tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5460 | symtable_add_def(st, tmpname, DEF_LOCAL); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5461 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5462 | symtable_node(st, CHILD(n, 3)); |
| 5463 | if (NCH(n) == 5) |
| 5464 | symtable_node(st, CHILD(n, 4)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5465 | } |
| 5466 | |
| 5467 | static void |
| 5468 | symtable_import(struct symtable *st, node *n) |
| 5469 | { |
| 5470 | int i; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5471 | /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)* |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5472 | | 'from' dotted_name 'import' |
| 5473 | ('*' | import_as_name (',' import_as_name)*) |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5474 | import_as_name: NAME [NAME NAME] |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5475 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5476 | if (STR(CHILD(n, 0))[0] == 'f') { /* from */ |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5477 | node *dotname = CHILD(n, 1); |
| 5478 | if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) { |
| 5479 | /* check for bogus imports */ |
| 5480 | if (n->n_lineno >= st->st_future->ff_last_lineno) { |
| 5481 | PyErr_SetString(PyExc_SyntaxError, |
| 5482 | LATE_FUTURE); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5483 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5484 | n->n_lineno); |
| 5485 | st->st_errors++; |
| 5486 | return; |
| 5487 | } |
| 5488 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5489 | if (TYPE(CHILD(n, 3)) == STAR) { |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 5490 | if (st->st_cur->ste_type != TYPE_MODULE) { |
Jeremy Hylton | 6a53bd8 | 2001-08-06 20:34:25 +0000 | [diff] [blame] | 5491 | if (symtable_warn(st, |
| 5492 | "import * only allowed at module level") < 0) |
| 5493 | return; |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 5494 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5495 | st->st_cur->ste_optimized |= OPT_IMPORT_STAR; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5496 | st->st_cur->ste_opt_lineno = n->n_lineno; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5497 | } else { |
| 5498 | for (i = 3; i < NCH(n); i += 2) { |
| 5499 | node *c = CHILD(n, i); |
| 5500 | if (NCH(c) > 1) /* import as */ |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5501 | symtable_assign(st, CHILD(c, 2), |
| 5502 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5503 | else |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5504 | symtable_assign(st, CHILD(c, 0), |
| 5505 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5506 | } |
| 5507 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5508 | } else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5509 | for (i = 1; i < NCH(n); i += 2) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5510 | symtable_assign(st, CHILD(n, i), DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5511 | } |
| 5512 | } |
| 5513 | } |
| 5514 | |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5515 | /* The third argument to symatble_assign() is a flag to be passed to |
| 5516 | symtable_add_def() if it is eventually called. The flag is useful |
| 5517 | to specify the particular type of assignment that should be |
| 5518 | recorded, e.g. an assignment caused by import. |
| 5519 | */ |
| 5520 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5521 | static void |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5522 | symtable_assign(struct symtable *st, node *n, int def_flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5523 | { |
| 5524 | node *tmp; |
| 5525 | int i; |
| 5526 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5527 | loop: |
| 5528 | switch (TYPE(n)) { |
| 5529 | case lambdef: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5530 | /* invalid assignment, e.g. lambda x:x=2. The next |
| 5531 | pass will catch this error. */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5532 | return; |
| 5533 | case power: |
| 5534 | if (NCH(n) > 2) { |
| 5535 | for (i = 2; i < NCH(n); ++i) |
| 5536 | if (TYPE(CHILD(n, i)) != DOUBLESTAR) |
| 5537 | symtable_node(st, CHILD(n, i)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5538 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5539 | if (NCH(n) > 1) { |
| 5540 | symtable_node(st, CHILD(n, 0)); |
| 5541 | symtable_node(st, CHILD(n, 1)); |
| 5542 | } else { |
| 5543 | n = CHILD(n, 0); |
| 5544 | goto loop; |
| 5545 | } |
| 5546 | return; |
| 5547 | case listmaker: |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5548 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
| 5549 | /* XXX This is an error, but the next pass |
| 5550 | will catch it. */ |
| 5551 | return; |
| 5552 | } else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5553 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5554 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5555 | } |
| 5556 | return; |
| 5557 | case exprlist: |
| 5558 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 5559 | case testlist1: |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5560 | if (NCH(n) == 1) { |
| 5561 | n = CHILD(n, 0); |
| 5562 | goto loop; |
| 5563 | } |
| 5564 | else { |
| 5565 | int i; |
| 5566 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5567 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5568 | return; |
| 5569 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5570 | case atom: |
| 5571 | tmp = CHILD(n, 0); |
| 5572 | if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) { |
| 5573 | n = CHILD(n, 1); |
| 5574 | goto loop; |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 5575 | } else if (TYPE(tmp) == NAME) { |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 5576 | if (strcmp(STR(tmp), "__debug__") == 0) { |
| 5577 | PyErr_SetString(PyExc_SyntaxError, |
| 5578 | ASSIGN_DEBUG); |
| 5579 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | 86424e3 | 2001-12-04 02:41:46 +0000 | [diff] [blame] | 5580 | n->n_lineno); |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 5581 | st->st_errors++; |
| 5582 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5583 | symtable_add_def(st, STR(tmp), DEF_LOCAL | def_flag); |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 5584 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5585 | return; |
| 5586 | case dotted_as_name: |
| 5587 | if (NCH(n) == 3) |
| 5588 | symtable_add_def(st, STR(CHILD(n, 2)), |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5589 | DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5590 | else |
| 5591 | symtable_add_def(st, |
| 5592 | STR(CHILD(CHILD(n, |
| 5593 | 0), 0)), |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5594 | DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5595 | return; |
| 5596 | case dotted_name: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5597 | symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5598 | return; |
| 5599 | case NAME: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5600 | symtable_add_def(st, STR(n), DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5601 | return; |
| 5602 | default: |
| 5603 | if (NCH(n) == 0) |
| 5604 | return; |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 5605 | if (NCH(n) == 1) { |
| 5606 | n = CHILD(n, 0); |
| 5607 | goto loop; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5608 | } |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 5609 | /* Should only occur for errors like x + 1 = 1, |
| 5610 | which will be caught in the next pass. */ |
| 5611 | for (i = 0; i < NCH(n); ++i) |
| 5612 | if (TYPE(CHILD(n, i)) >= single_input) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5613 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5614 | } |
| 5615 | } |