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__ */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 488 | }; |
| 489 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 490 | static int |
| 491 | is_free(int v) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 492 | { |
| 493 | if ((v & (USE | DEF_FREE)) |
| 494 | && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL))) |
| 495 | return 1; |
| 496 | if (v & DEF_FREE_CLASS) |
| 497 | return 1; |
| 498 | return 0; |
| 499 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 500 | |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 501 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 502 | com_error(struct compiling *c, PyObject *exc, char *msg) |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 503 | { |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 504 | PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL; |
| 505 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 506 | if (c == NULL) { |
| 507 | /* Error occurred via symtable call to |
| 508 | is_constant_false */ |
| 509 | PyErr_SetString(exc, msg); |
| 510 | return; |
| 511 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 512 | c->c_errors++; |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 513 | if (c->c_lineno < 1 || c->c_interactive) { |
| 514 | /* Unknown line number or interactive input */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 515 | PyErr_SetString(exc, msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 516 | return; |
| 517 | } |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 518 | v = PyString_FromString(msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 519 | if (v == NULL) |
| 520 | return; /* MemoryError, too bad */ |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 521 | |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 522 | line = PyErr_ProgramText(c->c_filename, c->c_lineno); |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 523 | if (line == NULL) { |
| 524 | Py_INCREF(Py_None); |
| 525 | line = Py_None; |
| 526 | } |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 527 | if (exc == PyExc_SyntaxError) { |
| 528 | t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno, |
| 529 | Py_None, line); |
| 530 | if (t == NULL) |
| 531 | goto exit; |
| 532 | w = Py_BuildValue("(OO)", v, t); |
| 533 | if (w == NULL) |
| 534 | goto exit; |
| 535 | PyErr_SetObject(exc, w); |
| 536 | } else { |
| 537 | /* Make sure additional exceptions are printed with |
| 538 | file and line, also. */ |
| 539 | PyErr_SetObject(exc, v); |
| 540 | PyErr_SyntaxLocation(c->c_filename, c->c_lineno); |
| 541 | } |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 542 | exit: |
| 543 | Py_XDECREF(t); |
| 544 | Py_XDECREF(v); |
| 545 | Py_XDECREF(w); |
| 546 | Py_XDECREF(line); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 549 | /* Interface to the block stack */ |
| 550 | |
| 551 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 552 | block_push(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 553 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 554 | if (c->c_nblocks >= CO_MAXBLOCKS) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 555 | com_error(c, PyExc_SystemError, |
| 556 | "too many statically nested blocks"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 557 | } |
| 558 | else { |
| 559 | c->c_block[c->c_nblocks++] = type; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 564 | block_pop(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 565 | { |
| 566 | if (c->c_nblocks > 0) |
| 567 | c->c_nblocks--; |
| 568 | 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] | 569 | com_error(c, PyExc_SystemError, "bad block pop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 573 | /* Prototype forward declarations */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 574 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 575 | static int com_init(struct compiling *, char *); |
| 576 | static void com_free(struct compiling *); |
| 577 | static void com_push(struct compiling *, int); |
| 578 | static void com_pop(struct compiling *, int); |
| 579 | static void com_done(struct compiling *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 580 | static void com_node(struct compiling *, node *); |
| 581 | static void com_factor(struct compiling *, node *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 582 | static void com_addbyte(struct compiling *, int); |
| 583 | static void com_addint(struct compiling *, int); |
| 584 | static void com_addoparg(struct compiling *, int, int); |
| 585 | static void com_addfwref(struct compiling *, int, int *); |
| 586 | static void com_backpatch(struct compiling *, int); |
| 587 | static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *); |
| 588 | static int com_addconst(struct compiling *, PyObject *); |
| 589 | static int com_addname(struct compiling *, PyObject *); |
| 590 | static void com_addopname(struct compiling *, int, node *); |
| 591 | static void com_list(struct compiling *, node *, int); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 592 | static void com_list_iter(struct compiling *, node *, node *, char *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 593 | static int com_argdefs(struct compiling *, node *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 594 | static void com_assign(struct compiling *, node *, int, node *); |
| 595 | static void com_assign_name(struct compiling *, node *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 596 | static PyCodeObject *icompile(node *, struct compiling *); |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 597 | static PyCodeObject *jcompile(node *, char *, struct compiling *, |
| 598 | PyCompilerFlags *); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 599 | static PyObject *parsestrplus(struct compiling*, node *); |
| 600 | static PyObject *parsestr(struct compiling *, char *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 601 | static node *get_rawdocstring(node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 602 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 603 | static int get_ref_type(struct compiling *, char *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 604 | |
| 605 | /* symtable operations */ |
| 606 | static int symtable_build(struct compiling *, node *); |
| 607 | static int symtable_load_symbols(struct compiling *); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 608 | static struct symtable *symtable_init(void); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 609 | static void symtable_enter_scope(struct symtable *, char *, int, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 610 | static int symtable_exit_scope(struct symtable *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 611 | static int symtable_add_def(struct symtable *, char *, int); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 612 | static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 613 | |
| 614 | static void symtable_node(struct symtable *, node *); |
| 615 | static void symtable_funcdef(struct symtable *, node *); |
| 616 | static void symtable_default_args(struct symtable *, node *); |
| 617 | static void symtable_params(struct symtable *, node *); |
| 618 | static void symtable_params_fplist(struct symtable *, node *n); |
| 619 | static void symtable_global(struct symtable *, node *); |
| 620 | static void symtable_import(struct symtable *, node *); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 621 | static void symtable_assign(struct symtable *, node *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 622 | static void symtable_list_comprehension(struct symtable *, node *); |
| 623 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 624 | static int symtable_update_free_vars(struct symtable *); |
| 625 | static int symtable_undo_free(struct symtable *, PyObject *, PyObject *); |
| 626 | static int symtable_check_global(struct symtable *, PyObject *, PyObject *); |
| 627 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 628 | /* helper */ |
| 629 | static void |
| 630 | do_pad(int pad) |
| 631 | { |
| 632 | int i; |
| 633 | for (i = 0; i < pad; ++i) |
| 634 | fprintf(stderr, " "); |
| 635 | } |
| 636 | |
| 637 | static void |
| 638 | dump(node *n, int pad, int depth) |
| 639 | { |
| 640 | int i; |
| 641 | if (depth == 0) |
| 642 | return; |
| 643 | do_pad(pad); |
| 644 | fprintf(stderr, "%d: %s\n", TYPE(n), STR(n)); |
| 645 | if (depth > 0) |
| 646 | depth--; |
| 647 | for (i = 0; i < NCH(n); ++i) |
| 648 | dump(CHILD(n, i), pad + 1, depth); |
| 649 | } |
| 650 | |
| 651 | #define DUMP(N) dump(N, 0, -1) |
| 652 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 653 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 654 | com_init(struct compiling *c, char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 655 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 656 | memset((void *)c, '\0', sizeof(struct compiling)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 657 | if ((c->c_code = PyString_FromStringAndSize((char *)NULL, |
| 658 | 1000)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 659 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 660 | if ((c->c_consts = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 661 | goto fail; |
| 662 | if ((c->c_const_dict = PyDict_New()) == NULL) |
| 663 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 664 | if ((c->c_names = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 665 | goto fail; |
| 666 | if ((c->c_name_dict = PyDict_New()) == NULL) |
| 667 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 668 | if ((c->c_locals = PyDict_New()) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 669 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 670 | if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL, |
| 671 | 1000)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 672 | goto fail; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 673 | c->c_globals = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 674 | c->c_varnames = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 675 | c->c_freevars = NULL; |
| 676 | c->c_cellvars = NULL; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 677 | c->c_nlocals = 0; |
| 678 | c->c_argcount = 0; |
| 679 | c->c_flags = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 680 | c->c_nexti = 0; |
| 681 | c->c_errors = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 682 | c->c_infunction = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 683 | c->c_interactive = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 684 | c->c_loops = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 685 | c->c_begin = 0; |
| 686 | c->c_nblocks = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 687 | c->c_filename = filename; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 688 | c->c_name = "?"; |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 689 | c->c_lineno = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 690 | c->c_stacklevel = 0; |
| 691 | c->c_maxstacklevel = 0; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 692 | c->c_firstlineno = 0; |
| 693 | c->c_last_addr = 0; |
| 694 | c->c_last_line = 0; |
Barry Warsaw | 174e801 | 2001-01-22 04:35:57 +0000 | [diff] [blame] | 695 | c->c_lnotab_next = 0; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 696 | c->c_tmpname = 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 697 | c->c_nested = 0; |
| 698 | c->c_closure = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 699 | c->c_symtable = NULL; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 700 | return 1; |
| 701 | |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 702 | fail: |
| 703 | com_free(c); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 708 | com_free(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 709 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 710 | Py_XDECREF(c->c_code); |
| 711 | Py_XDECREF(c->c_consts); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 712 | Py_XDECREF(c->c_const_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 713 | Py_XDECREF(c->c_names); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 714 | Py_XDECREF(c->c_name_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 715 | Py_XDECREF(c->c_globals); |
| 716 | Py_XDECREF(c->c_locals); |
| 717 | Py_XDECREF(c->c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 718 | Py_XDECREF(c->c_freevars); |
| 719 | Py_XDECREF(c->c_cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 720 | Py_XDECREF(c->c_lnotab); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 721 | if (c->c_future) |
| 722 | PyMem_Free((void *)c->c_future); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 726 | com_push(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 727 | { |
| 728 | c->c_stacklevel += n; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 729 | if (c->c_stacklevel > c->c_maxstacklevel) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 730 | c->c_maxstacklevel = c->c_stacklevel; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 731 | /* |
| 732 | fprintf(stderr, "%s:%s:%d max stack nexti=%d level=%d n=%d\n", |
| 733 | c->c_filename, c->c_name, c->c_lineno, |
| 734 | c->c_nexti, c->c_stacklevel, n); |
| 735 | */ |
| 736 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 740 | com_pop(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 741 | { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 742 | if (c->c_stacklevel < n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 743 | c->c_stacklevel = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 744 | else |
| 745 | c->c_stacklevel -= n; |
| 746 | } |
| 747 | |
| 748 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 749 | com_done(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 750 | { |
| 751 | if (c->c_code != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 752 | _PyString_Resize(&c->c_code, c->c_nexti); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 753 | if (c->c_lnotab != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 754 | _PyString_Resize(&c->c_lnotab, c->c_lnotab_next); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 757 | static int |
| 758 | com_check_size(PyObject **s, int offset) |
| 759 | { |
| 760 | int len = PyString_GET_SIZE(*s); |
| 761 | if (offset >= len) |
| 762 | return _PyString_Resize(s, len * 2); |
| 763 | return 0; |
| 764 | } |
| 765 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 766 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 767 | com_addbyte(struct compiling *c, int byte) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 768 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 769 | /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 770 | assert(byte >= 0 && byte <= 255); |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 771 | assert(c->c_code != 0); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 772 | if (com_check_size(&c->c_code, c->c_nexti)) { |
| 773 | c->c_errors++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 774 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 775 | } |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 776 | PyString_AS_STRING(c->c_code)[c->c_nexti++] = byte; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 780 | com_addint(struct compiling *c, int x) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 781 | { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 782 | com_addbyte(c, x & 0xff); |
| 783 | com_addbyte(c, x >> 8); /* XXX x should be positive */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 787 | com_add_lnotab(struct compiling *c, int addr, int line) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 788 | { |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 789 | char *p; |
| 790 | if (c->c_lnotab == NULL) |
| 791 | return; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 792 | if (com_check_size(&c->c_lnotab, c->c_lnotab_next + 2)) { |
| 793 | c->c_errors++; |
| 794 | return; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 795 | } |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 796 | p = PyString_AS_STRING(c->c_lnotab) + c->c_lnotab_next; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 797 | *p++ = addr; |
| 798 | *p++ = line; |
| 799 | c->c_lnotab_next += 2; |
| 800 | } |
| 801 | |
| 802 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 803 | com_set_lineno(struct compiling *c, int lineno) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 804 | { |
| 805 | c->c_lineno = lineno; |
| 806 | if (c->c_firstlineno == 0) { |
| 807 | c->c_firstlineno = c->c_last_line = lineno; |
| 808 | } |
| 809 | else { |
| 810 | int incr_addr = c->c_nexti - c->c_last_addr; |
| 811 | int incr_line = lineno - c->c_last_line; |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 812 | while (incr_addr > 255) { |
| 813 | com_add_lnotab(c, 255, 0); |
| 814 | incr_addr -= 255; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 815 | } |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 816 | while (incr_line > 255) { |
| 817 | com_add_lnotab(c, incr_addr, 255); |
| 818 | incr_line -=255; |
| 819 | incr_addr = 0; |
| 820 | } |
| 821 | if (incr_addr > 0 || incr_line > 0) |
| 822 | com_add_lnotab(c, incr_addr, incr_line); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 823 | c->c_last_addr = c->c_nexti; |
| 824 | c->c_last_line = lineno; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 829 | com_addoparg(struct compiling *c, int op, int arg) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 830 | { |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 831 | int extended_arg = arg >> 16; |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 832 | if (op == SET_LINENO) { |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 833 | com_set_lineno(c, arg); |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 834 | if (Py_OptimizeFlag) |
| 835 | return; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 836 | } |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 837 | if (extended_arg){ |
| 838 | com_addbyte(c, EXTENDED_ARG); |
| 839 | com_addint(c, extended_arg); |
| 840 | arg &= 0xffff; |
| 841 | } |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 842 | com_addbyte(c, op); |
| 843 | com_addint(c, arg); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 847 | com_addfwref(struct compiling *c, int op, int *p_anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 848 | { |
| 849 | /* Compile a forward reference for backpatching */ |
| 850 | int here; |
| 851 | int anchor; |
| 852 | com_addbyte(c, op); |
| 853 | here = c->c_nexti; |
| 854 | anchor = *p_anchor; |
| 855 | *p_anchor = here; |
| 856 | com_addint(c, anchor == 0 ? 0 : here - anchor); |
| 857 | } |
| 858 | |
| 859 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 860 | com_backpatch(struct compiling *c, int anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 861 | { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 862 | unsigned char *code = (unsigned char *) PyString_AS_STRING(c->c_code); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 863 | int target = c->c_nexti; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 864 | int dist; |
| 865 | int prev; |
| 866 | for (;;) { |
| 867 | /* Make the JUMP instruction at anchor point to target */ |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 868 | prev = code[anchor] + (code[anchor+1] << 8); |
| 869 | dist = target - (anchor+2); |
| 870 | code[anchor] = dist & 0xff; |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 871 | dist >>= 8; |
| 872 | code[anchor+1] = dist; |
| 873 | dist >>= 8; |
| 874 | if (dist) { |
| 875 | com_error(c, PyExc_SystemError, |
| 876 | "com_backpatch: offset too large"); |
| 877 | break; |
| 878 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 879 | if (!prev) |
| 880 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 881 | anchor -= prev; |
| 882 | } |
| 883 | } |
| 884 | |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 885 | /* Handle literals and names uniformly */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 886 | |
| 887 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 888 | com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 889 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 890 | PyObject *w, *t, *np=NULL; |
| 891 | long n; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 892 | |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 893 | t = Py_BuildValue("(OO)", v, v->ob_type); |
| 894 | if (t == NULL) |
| 895 | goto fail; |
| 896 | w = PyDict_GetItem(dict, t); |
| 897 | if (w != NULL) { |
| 898 | n = PyInt_AsLong(w); |
| 899 | } else { |
| 900 | n = PyList_Size(list); |
| 901 | np = PyInt_FromLong(n); |
| 902 | if (np == NULL) |
| 903 | goto fail; |
| 904 | if (PyList_Append(list, v) != 0) |
| 905 | goto fail; |
| 906 | if (PyDict_SetItem(dict, t, np) != 0) |
| 907 | goto fail; |
| 908 | Py_DECREF(np); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 909 | } |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 910 | Py_DECREF(t); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 911 | return n; |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 912 | fail: |
| 913 | Py_XDECREF(np); |
| 914 | Py_XDECREF(t); |
| 915 | c->c_errors++; |
| 916 | return 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 920 | com_addconst(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 921 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 922 | 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] | 923 | } |
| 924 | |
| 925 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 926 | com_addname(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 927 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 928 | 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] | 929 | } |
| 930 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 931 | int |
| 932 | _Py_Mangle(char *p, char *name, char *buffer, size_t maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 933 | { |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 934 | /* Name mangling: __private becomes _classname__private. |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 935 | This is independent from how the name is used. */ |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 936 | size_t nlen, plen; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 937 | if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_') |
| 938 | return 0; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 939 | nlen = strlen(name); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 940 | if (nlen+2 >= maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 941 | return 0; /* Don't mangle __extremely_long_names */ |
| 942 | if (name[nlen-1] == '_' && name[nlen-2] == '_') |
| 943 | return 0; /* Don't mangle __whatever__ */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 944 | /* Strip leading underscores from class name */ |
| 945 | while (*p == '_') |
| 946 | p++; |
| 947 | if (*p == '\0') |
| 948 | return 0; /* Don't mangle if class is just underscores */ |
| 949 | plen = strlen(p); |
| 950 | if (plen + nlen >= maxlen) |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 951 | plen = maxlen-nlen-2; /* Truncate class name if too long */ |
| 952 | /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 953 | buffer[0] = '_'; |
| 954 | strncpy(buffer+1, p, plen); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 955 | strcpy(buffer+1+plen, name); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 956 | return 1; |
| 957 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 958 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 959 | static void |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 960 | com_addop_name(struct compiling *c, int op, char *name) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 961 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 962 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 963 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 964 | char buffer[MANGLE_LEN]; |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 965 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 966 | if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer))) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 967 | name = buffer; |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 968 | if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 969 | c->c_errors++; |
| 970 | i = 255; |
| 971 | } |
| 972 | else { |
| 973 | i = com_addname(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 974 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 975 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 976 | com_addoparg(c, op, i); |
| 977 | } |
| 978 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 979 | #define NAME_LOCAL 0 |
| 980 | #define NAME_GLOBAL 1 |
| 981 | #define NAME_DEFAULT 2 |
| 982 | #define NAME_CLOSURE 3 |
| 983 | |
| 984 | static int |
| 985 | com_lookup_arg(PyObject *dict, PyObject *name) |
| 986 | { |
| 987 | PyObject *v = PyDict_GetItem(dict, name); |
| 988 | if (v == NULL) |
| 989 | return -1; |
| 990 | else |
| 991 | return PyInt_AS_LONG(v); |
| 992 | } |
| 993 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 994 | static void |
| 995 | com_addop_varname(struct compiling *c, int kind, char *name) |
| 996 | { |
| 997 | PyObject *v; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 998 | int i, reftype; |
| 999 | int scope = NAME_DEFAULT; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1000 | int op = STOP_CODE; |
| 1001 | char buffer[MANGLE_LEN]; |
| 1002 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1003 | if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1004 | name = buffer; |
| 1005 | if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { |
| 1006 | c->c_errors++; |
| 1007 | i = 255; |
| 1008 | goto done; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1009 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1010 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1011 | reftype = get_ref_type(c, name); |
| 1012 | switch (reftype) { |
| 1013 | case LOCAL: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 1014 | if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1015 | scope = NAME_LOCAL; |
| 1016 | break; |
| 1017 | case GLOBAL_EXPLICIT: |
| 1018 | scope = NAME_GLOBAL; |
| 1019 | break; |
| 1020 | case GLOBAL_IMPLICIT: |
| 1021 | if (c->c_flags & CO_OPTIMIZED) |
| 1022 | scope = NAME_GLOBAL; |
| 1023 | break; |
| 1024 | case FREE: |
| 1025 | case CELL: |
| 1026 | scope = NAME_CLOSURE; |
| 1027 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | i = com_addname(c, v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1031 | if (scope == NAME_LOCAL) |
| 1032 | i = com_lookup_arg(c->c_locals, v); |
| 1033 | else if (reftype == FREE) |
| 1034 | i = com_lookup_arg(c->c_freevars, v); |
| 1035 | else if (reftype == CELL) |
| 1036 | i = com_lookup_arg(c->c_cellvars, v); |
| 1037 | if (i == -1) { |
| 1038 | c->c_errors++; /* XXX no exception set */ |
| 1039 | i = 255; |
| 1040 | goto done; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1041 | } |
| 1042 | Py_DECREF(v); |
| 1043 | |
| 1044 | switch (kind) { |
| 1045 | case VAR_LOAD: |
| 1046 | switch (scope) { |
| 1047 | case NAME_LOCAL: |
| 1048 | op = LOAD_FAST; |
| 1049 | break; |
| 1050 | case NAME_GLOBAL: |
| 1051 | op = LOAD_GLOBAL; |
| 1052 | break; |
| 1053 | case NAME_DEFAULT: |
| 1054 | op = LOAD_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1055 | break; |
| 1056 | case NAME_CLOSURE: |
| 1057 | op = LOAD_DEREF; |
| 1058 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1059 | } |
| 1060 | break; |
| 1061 | case VAR_STORE: |
| 1062 | switch (scope) { |
| 1063 | case NAME_LOCAL: |
| 1064 | op = STORE_FAST; |
| 1065 | break; |
| 1066 | case NAME_GLOBAL: |
| 1067 | op = STORE_GLOBAL; |
| 1068 | break; |
| 1069 | case NAME_DEFAULT: |
| 1070 | op = STORE_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1071 | break; |
| 1072 | case NAME_CLOSURE: |
| 1073 | op = STORE_DEREF; |
| 1074 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1075 | } |
| 1076 | break; |
| 1077 | case VAR_DELETE: |
| 1078 | switch (scope) { |
| 1079 | case NAME_LOCAL: |
| 1080 | op = DELETE_FAST; |
| 1081 | break; |
| 1082 | case NAME_GLOBAL: |
| 1083 | op = DELETE_GLOBAL; |
| 1084 | break; |
| 1085 | case NAME_DEFAULT: |
| 1086 | op = DELETE_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1087 | break; |
| 1088 | case NAME_CLOSURE: { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 1089 | char buf[500]; |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 1090 | PyOS_snprintf(buf, sizeof(buf), |
| 1091 | DEL_CLOSURE_ERROR, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1092 | com_error(c, PyExc_SyntaxError, buf); |
| 1093 | i = 255; |
| 1094 | break; |
| 1095 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1096 | } |
| 1097 | break; |
| 1098 | } |
| 1099 | done: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1100 | com_addoparg(c, op, i); |
| 1101 | } |
| 1102 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1103 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1104 | com_addopname(struct compiling *c, int op, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1105 | { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1106 | char *name; |
| 1107 | char buffer[1000]; |
| 1108 | /* XXX it is possible to write this code without the 1000 |
| 1109 | chars on the total length of dotted names, I just can't be |
| 1110 | bothered right now */ |
| 1111 | if (TYPE(n) == STAR) |
| 1112 | name = "*"; |
| 1113 | else if (TYPE(n) == dotted_name) { |
| 1114 | char *p = buffer; |
| 1115 | int i; |
| 1116 | name = buffer; |
| 1117 | for (i = 0; i < NCH(n); i += 2) { |
| 1118 | char *s = STR(CHILD(n, i)); |
| 1119 | if (p + strlen(s) > buffer + (sizeof buffer) - 2) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1120 | com_error(c, PyExc_MemoryError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1121 | "dotted_name too long"); |
Guido van Rossum | d9dfaf5 | 1995-02-17 15:04:57 +0000 | [diff] [blame] | 1122 | name = NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1123 | break; |
| 1124 | } |
| 1125 | if (p != buffer) |
| 1126 | *p++ = '.'; |
| 1127 | strcpy(p, s); |
| 1128 | p = strchr(p, '\0'); |
| 1129 | } |
| 1130 | } |
| 1131 | else { |
| 1132 | REQ(n, NAME); |
| 1133 | name = STR(n); |
| 1134 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1135 | com_addop_name(c, op, name); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1138 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1139 | parsenumber(struct compiling *co, char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1140 | { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1141 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1142 | long x; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1143 | double dx; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1144 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 1145 | Py_complex c; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1146 | int imflag; |
| 1147 | #endif |
| 1148 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1149 | errno = 0; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1150 | end = s + strlen(s) - 1; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1151 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 15ad9a6 | 1996-01-26 20:53:56 +0000 | [diff] [blame] | 1152 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1153 | #endif |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 1154 | if (*end == 'l' || *end == 'L') |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1155 | return PyLong_FromString(s, (char **)0, 0); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 1156 | if (s[0] == '0') |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1157 | x = (long) PyOS_strtoul(s, &end, 0); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 1158 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1159 | x = PyOS_strtol(s, &end, 0); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1160 | if (*end == '\0') { |
Jeremy Hylton | 71b6af9 | 2001-08-27 19:45:25 +0000 | [diff] [blame] | 1161 | if (errno != 0) |
| 1162 | return PyLong_FromString(s, (char **)0, 0); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1163 | return PyInt_FromLong(x); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1164 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1165 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1166 | #ifndef WITHOUT_COMPLEX |
| 1167 | if (imflag) { |
| 1168 | c.real = 0.; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1169 | PyFPE_START_PROTECT("atof", return 0) |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1170 | c.imag = atof(s); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1171 | PyFPE_END_PROTECT(c) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1172 | return PyComplex_FromCComplex(c); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1173 | } |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1174 | else |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1175 | #endif |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1176 | { |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1177 | PyFPE_START_PROTECT("atof", return 0) |
| 1178 | dx = atof(s); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1179 | PyFPE_END_PROTECT(dx) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1180 | return PyFloat_FromDouble(dx); |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1181 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1184 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1185 | parsestr(struct compiling *com, char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1186 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1187 | PyObject *v; |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1188 | size_t len; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1189 | char *buf; |
| 1190 | char *p; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1191 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1192 | int c; |
Guido van Rossum | 5f5e817 | 1997-04-06 03:41:40 +0000 | [diff] [blame] | 1193 | int first = *s; |
| 1194 | int quote = first; |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1195 | int rawmode = 0; |
| 1196 | int unicode = 0; |
Guido van Rossum | 05459c5 | 2002-05-28 18:47:29 +0000 | [diff] [blame] | 1197 | |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1198 | if (isalpha(quote) || quote == '_') { |
| 1199 | if (quote == 'u' || quote == 'U') { |
| 1200 | quote = *++s; |
| 1201 | unicode = 1; |
| 1202 | } |
| 1203 | if (quote == 'r' || quote == 'R') { |
| 1204 | quote = *++s; |
| 1205 | rawmode = 1; |
| 1206 | } |
| 1207 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1208 | if (quote != '\'' && quote != '\"') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1209 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1210 | return NULL; |
| 1211 | } |
| 1212 | s++; |
| 1213 | len = strlen(s); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1214 | if (len > INT_MAX) { |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1215 | com_error(com, PyExc_OverflowError, |
| 1216 | "string to parse is too long"); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1217 | return NULL; |
| 1218 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1219 | if (s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1220 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1221 | return NULL; |
| 1222 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1223 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
| 1224 | s += 2; |
| 1225 | len -= 2; |
| 1226 | if (s[--len] != quote || s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1227 | PyErr_BadInternalCall(); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1228 | return NULL; |
| 1229 | } |
| 1230 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1231 | #ifdef Py_USING_UNICODE |
Guido van Rossum | fdc8bdb | 2000-05-01 17:54:56 +0000 | [diff] [blame] | 1232 | if (unicode || Py_UnicodeFlag) { |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1233 | if (rawmode) |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1234 | v = PyUnicode_DecodeRawUnicodeEscape( |
| 1235 | s, len, NULL); |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1236 | else |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1237 | v = PyUnicode_DecodeUnicodeEscape( |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1238 | s, len, NULL); |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1239 | if (v == NULL) |
| 1240 | PyErr_SyntaxLocation(com->c_filename, com->c_lineno); |
| 1241 | return v; |
| 1242 | |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1243 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1244 | #endif |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1245 | if (rawmode || strchr(s, '\\') == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1246 | return PyString_FromStringAndSize(s, len); |
| 1247 | v = PyString_FromStringAndSize((char *)NULL, len); |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1248 | if (v == NULL) |
| 1249 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1250 | p = buf = PyString_AsString(v); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1251 | end = s + len; |
| 1252 | while (s < end) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1253 | if (*s != '\\') { |
| 1254 | *p++ = *s++; |
| 1255 | continue; |
| 1256 | } |
| 1257 | s++; |
| 1258 | switch (*s++) { |
| 1259 | /* XXX This assumes ASCII! */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1260 | case '\n': break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1261 | case '\\': *p++ = '\\'; break; |
| 1262 | case '\'': *p++ = '\''; break; |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1263 | case '\"': *p++ = '\"'; break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1264 | case 'b': *p++ = '\b'; break; |
| 1265 | case 'f': *p++ = '\014'; break; /* FF */ |
| 1266 | case 't': *p++ = '\t'; break; |
| 1267 | case 'n': *p++ = '\n'; break; |
| 1268 | case 'r': *p++ = '\r'; break; |
| 1269 | case 'v': *p++ = '\013'; break; /* VT */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1270 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 1271 | case '0': case '1': case '2': case '3': |
| 1272 | case '4': case '5': case '6': case '7': |
| 1273 | c = s[-1] - '0'; |
| 1274 | if ('0' <= *s && *s <= '7') { |
| 1275 | c = (c<<3) + *s++ - '0'; |
| 1276 | if ('0' <= *s && *s <= '7') |
| 1277 | c = (c<<3) + *s++ - '0'; |
| 1278 | } |
| 1279 | *p++ = c; |
| 1280 | break; |
| 1281 | case 'x': |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 1282 | if (isxdigit(Py_CHARMASK(s[0])) |
| 1283 | && isxdigit(Py_CHARMASK(s[1]))) { |
Guido van Rossum | ed1100f | 1997-10-20 23:24:07 +0000 | [diff] [blame] | 1284 | unsigned int x = 0; |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1285 | c = Py_CHARMASK(*s); |
| 1286 | s++; |
| 1287 | if (isdigit(c)) |
| 1288 | x = c - '0'; |
| 1289 | else if (islower(c)) |
| 1290 | x = 10 + c - 'a'; |
| 1291 | else |
| 1292 | x = 10 + c - 'A'; |
| 1293 | x = x << 4; |
| 1294 | c = Py_CHARMASK(*s); |
| 1295 | s++; |
| 1296 | if (isdigit(c)) |
| 1297 | x += c - '0'; |
| 1298 | else if (islower(c)) |
| 1299 | x += 10 + c - 'a'; |
| 1300 | else |
| 1301 | x += 10 + c - 'A'; |
Guido van Rossum | ed1100f | 1997-10-20 23:24:07 +0000 | [diff] [blame] | 1302 | *p++ = x; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1303 | break; |
| 1304 | } |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1305 | Py_DECREF(v); |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1306 | com_error(com, PyExc_ValueError, |
| 1307 | "invalid \\x escape"); |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1308 | return NULL; |
Guido van Rossum | 05459c5 | 2002-05-28 18:47:29 +0000 | [diff] [blame] | 1309 | #ifndef Py_USING_UNICODE |
| 1310 | case 'u': |
| 1311 | case 'U': |
| 1312 | case 'N': |
| 1313 | if (unicode) { |
| 1314 | Py_DECREF(v); |
| 1315 | com_error(com, PyExc_ValueError, |
| 1316 | "Unicode escapes not legal " |
| 1317 | "when Unicode disabled"); |
| 1318 | return NULL; |
| 1319 | } |
| 1320 | #endif |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1321 | default: |
| 1322 | *p++ = '\\'; |
| 1323 | *p++ = s[-1]; |
| 1324 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1325 | } |
| 1326 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1327 | _PyString_Resize(&v, (int)(p - buf)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1328 | return v; |
| 1329 | } |
| 1330 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1331 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1332 | parsestrplus(struct compiling* c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1333 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1334 | PyObject *v; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1335 | int i; |
| 1336 | REQ(CHILD(n, 0), STRING); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1337 | if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1338 | /* String literal concatenation */ |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1339 | for (i = 1; i < NCH(n); i++) { |
| 1340 | PyObject *s; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1341 | s = parsestr(c, STR(CHILD(n, i))); |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1342 | if (s == NULL) |
| 1343 | goto onError; |
| 1344 | if (PyString_Check(v) && PyString_Check(s)) { |
| 1345 | PyString_ConcatAndDel(&v, s); |
| 1346 | if (v == NULL) |
| 1347 | goto onError; |
| 1348 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1349 | #ifdef Py_USING_UNICODE |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1350 | else { |
| 1351 | PyObject *temp; |
| 1352 | temp = PyUnicode_Concat(v, s); |
| 1353 | Py_DECREF(s); |
| 1354 | if (temp == NULL) |
| 1355 | goto onError; |
| 1356 | Py_DECREF(v); |
| 1357 | v = temp; |
| 1358 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1359 | #endif |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1360 | } |
| 1361 | } |
| 1362 | return v; |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1363 | |
| 1364 | onError: |
| 1365 | Py_XDECREF(v); |
| 1366 | return NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1369 | static void |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1370 | 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] | 1371 | { |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1372 | int anchor = 0; |
| 1373 | int save_begin = c->c_begin; |
| 1374 | |
| 1375 | /* list_iter: for v in expr [list_iter] */ |
| 1376 | com_node(c, CHILD(n, 3)); /* expr */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1377 | com_addbyte(c, GET_ITER); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1378 | c->c_begin = c->c_nexti; |
| 1379 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1380 | com_addfwref(c, FOR_ITER, &anchor); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1381 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1382 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1383 | c->c_loops++; |
| 1384 | com_list_iter(c, n, e, t); |
| 1385 | c->c_loops--; |
| 1386 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 1387 | c->c_begin = save_begin; |
| 1388 | com_backpatch(c, anchor); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1389 | com_pop(c, 1); /* FOR_ITER has popped this */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | static void |
| 1393 | com_list_if(struct compiling *c, node *n, node *e, char *t) |
| 1394 | { |
| 1395 | int anchor = 0; |
| 1396 | int a = 0; |
| 1397 | /* list_iter: 'if' test [list_iter] */ |
| 1398 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 1399 | com_node(c, CHILD(n, 1)); |
| 1400 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 1401 | com_addbyte(c, POP_TOP); |
| 1402 | com_pop(c, 1); |
| 1403 | com_list_iter(c, n, e, t); |
| 1404 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 1405 | com_backpatch(c, a); |
| 1406 | /* We jump here with an extra entry which we now pop */ |
| 1407 | com_addbyte(c, POP_TOP); |
| 1408 | com_backpatch(c, anchor); |
| 1409 | } |
| 1410 | |
| 1411 | static void |
| 1412 | com_list_iter(struct compiling *c, |
| 1413 | node *p, /* parent of list_iter node */ |
| 1414 | node *e, /* element expression node */ |
| 1415 | char *t /* name of result list temp local */) |
| 1416 | { |
| 1417 | /* list_iter is the last child in a listmaker, list_for, or list_if */ |
| 1418 | node *n = CHILD(p, NCH(p)-1); |
| 1419 | if (TYPE(n) == list_iter) { |
| 1420 | n = CHILD(n, 0); |
| 1421 | switch (TYPE(n)) { |
| 1422 | case list_for: |
| 1423 | com_list_for(c, n, e, t); |
| 1424 | break; |
| 1425 | case list_if: |
| 1426 | com_list_if(c, n, e, t); |
| 1427 | break; |
| 1428 | default: |
| 1429 | com_error(c, PyExc_SystemError, |
| 1430 | "invalid list_iter node type"); |
| 1431 | } |
| 1432 | } |
| 1433 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1434 | com_addop_varname(c, VAR_LOAD, t); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1435 | com_push(c, 1); |
| 1436 | com_node(c, e); |
| 1437 | com_addoparg(c, CALL_FUNCTION, 1); |
| 1438 | com_addbyte(c, POP_TOP); |
| 1439 | com_pop(c, 2); |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | static void |
| 1444 | com_list_comprehension(struct compiling *c, node *n) |
| 1445 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1446 | /* listmaker: test list_for */ |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 1447 | char tmpname[30]; |
| 1448 | PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->c_tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1449 | com_addoparg(c, BUILD_LIST, 0); |
| 1450 | com_addbyte(c, DUP_TOP); /* leave the result on the stack */ |
| 1451 | com_push(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1452 | com_addop_name(c, LOAD_ATTR, "append"); |
| 1453 | com_addop_varname(c, VAR_STORE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1454 | com_pop(c, 1); |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1455 | com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1456 | com_addop_varname(c, VAR_DELETE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1457 | --c->c_tmpname; |
| 1458 | } |
| 1459 | |
| 1460 | static void |
| 1461 | com_listmaker(struct compiling *c, node *n) |
| 1462 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1463 | /* listmaker: test ( list_for | (',' test)* [','] ) */ |
| 1464 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1465 | com_list_comprehension(c, n); |
| 1466 | else { |
| 1467 | int len = 0; |
| 1468 | int i; |
| 1469 | for (i = 0; i < NCH(n); i += 2, len++) |
| 1470 | com_node(c, CHILD(n, i)); |
| 1471 | com_addoparg(c, BUILD_LIST, len); |
| 1472 | com_pop(c, len-1); |
| 1473 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1477 | com_dictmaker(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1478 | { |
| 1479 | int i; |
| 1480 | /* dictmaker: test ':' test (',' test ':' value)* [','] */ |
| 1481 | for (i = 0; i+2 < NCH(n); i += 4) { |
| 1482 | /* We must arrange things just right for STORE_SUBSCR. |
| 1483 | It wants the stack to look like (value) (dict) (key) */ |
| 1484 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1485 | com_push(c, 1); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1486 | com_node(c, CHILD(n, i+2)); /* value */ |
| 1487 | com_addbyte(c, ROT_TWO); |
| 1488 | com_node(c, CHILD(n, i)); /* key */ |
| 1489 | com_addbyte(c, STORE_SUBSCR); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1490 | com_pop(c, 3); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1495 | com_atom(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1496 | { |
| 1497 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1498 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1499 | int i; |
| 1500 | REQ(n, atom); |
| 1501 | ch = CHILD(n, 0); |
| 1502 | switch (TYPE(ch)) { |
| 1503 | case LPAR: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1504 | if (TYPE(CHILD(n, 1)) == RPAR) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1505 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1506 | com_push(c, 1); |
| 1507 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1508 | else |
| 1509 | com_node(c, CHILD(n, 1)); |
| 1510 | break; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1511 | case LSQB: /* '[' [listmaker] ']' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1512 | if (TYPE(CHILD(n, 1)) == RSQB) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1513 | com_addoparg(c, BUILD_LIST, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1514 | com_push(c, 1); |
| 1515 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1516 | else |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1517 | com_listmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1518 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1519 | case LBRACE: /* '{' [dictmaker] '}' */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1520 | com_addoparg(c, BUILD_MAP, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1521 | com_push(c, 1); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1522 | if (TYPE(CHILD(n, 1)) == dictmaker) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1523 | com_dictmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1524 | break; |
| 1525 | case BACKQUOTE: |
| 1526 | com_node(c, CHILD(n, 1)); |
| 1527 | com_addbyte(c, UNARY_CONVERT); |
| 1528 | break; |
| 1529 | case NUMBER: |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1530 | if ((v = parsenumber(c, STR(ch))) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1531 | i = 255; |
| 1532 | } |
| 1533 | else { |
| 1534 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1535 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1536 | } |
| 1537 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1538 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1539 | break; |
| 1540 | case STRING: |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1541 | v = parsestrplus(c, n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1542 | if (v == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1543 | c->c_errors++; |
| 1544 | i = 255; |
| 1545 | } |
| 1546 | else { |
| 1547 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1548 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1549 | } |
| 1550 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1551 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1552 | break; |
| 1553 | case NAME: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1554 | com_addop_varname(c, VAR_LOAD, STR(ch)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1555 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1556 | break; |
| 1557 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1558 | com_error(c, PyExc_SystemError, |
| 1559 | "com_atom: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1564 | com_slice(struct compiling *c, node *n, int op) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1565 | { |
| 1566 | if (NCH(n) == 1) { |
| 1567 | com_addbyte(c, op); |
| 1568 | } |
| 1569 | else if (NCH(n) == 2) { |
| 1570 | if (TYPE(CHILD(n, 0)) != COLON) { |
| 1571 | com_node(c, CHILD(n, 0)); |
| 1572 | com_addbyte(c, op+1); |
| 1573 | } |
| 1574 | else { |
| 1575 | com_node(c, CHILD(n, 1)); |
| 1576 | com_addbyte(c, op+2); |
| 1577 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1578 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1579 | } |
| 1580 | else { |
| 1581 | com_node(c, CHILD(n, 0)); |
| 1582 | com_node(c, CHILD(n, 2)); |
| 1583 | com_addbyte(c, op+3); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1584 | com_pop(c, 2); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1585 | } |
| 1586 | } |
| 1587 | |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1588 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1589 | com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn) |
| 1590 | { |
| 1591 | if (NCH(n) == 1) { |
| 1592 | com_addbyte(c, DUP_TOP); |
| 1593 | com_push(c, 1); |
| 1594 | com_addbyte(c, SLICE); |
| 1595 | com_node(c, augn); |
| 1596 | com_addbyte(c, opcode); |
| 1597 | com_pop(c, 1); |
| 1598 | com_addbyte(c, ROT_TWO); |
| 1599 | com_addbyte(c, STORE_SLICE); |
| 1600 | com_pop(c, 2); |
| 1601 | } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) { |
| 1602 | com_node(c, CHILD(n, 0)); |
| 1603 | com_addoparg(c, DUP_TOPX, 2); |
| 1604 | com_push(c, 2); |
| 1605 | com_addbyte(c, SLICE+1); |
| 1606 | com_pop(c, 1); |
| 1607 | com_node(c, augn); |
| 1608 | com_addbyte(c, opcode); |
| 1609 | com_pop(c, 1); |
| 1610 | com_addbyte(c, ROT_THREE); |
| 1611 | com_addbyte(c, STORE_SLICE+1); |
| 1612 | com_pop(c, 3); |
| 1613 | } else if (NCH(n) == 2) { |
| 1614 | com_node(c, CHILD(n, 1)); |
| 1615 | com_addoparg(c, DUP_TOPX, 2); |
| 1616 | com_push(c, 2); |
| 1617 | com_addbyte(c, SLICE+2); |
| 1618 | com_pop(c, 1); |
| 1619 | com_node(c, augn); |
| 1620 | com_addbyte(c, opcode); |
| 1621 | com_pop(c, 1); |
| 1622 | com_addbyte(c, ROT_THREE); |
| 1623 | com_addbyte(c, STORE_SLICE+2); |
| 1624 | com_pop(c, 3); |
| 1625 | } else { |
| 1626 | com_node(c, CHILD(n, 0)); |
| 1627 | com_node(c, CHILD(n, 2)); |
| 1628 | com_addoparg(c, DUP_TOPX, 3); |
| 1629 | com_push(c, 3); |
| 1630 | com_addbyte(c, SLICE+3); |
| 1631 | com_pop(c, 2); |
| 1632 | com_node(c, augn); |
| 1633 | com_addbyte(c, opcode); |
| 1634 | com_pop(c, 1); |
| 1635 | com_addbyte(c, ROT_FOUR); |
| 1636 | com_addbyte(c, STORE_SLICE+3); |
| 1637 | com_pop(c, 4); |
| 1638 | } |
| 1639 | } |
| 1640 | |
| 1641 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1642 | com_argument(struct compiling *c, node *n, PyObject **pkeywords) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1643 | { |
| 1644 | node *m; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1645 | REQ(n, argument); /* [test '='] test; really [keyword '='] test */ |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1646 | if (NCH(n) == 1) { |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1647 | if (*pkeywords != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1648 | com_error(c, PyExc_SyntaxError, |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 1649 | "non-keyword arg after keyword arg"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1650 | } |
| 1651 | else { |
| 1652 | com_node(c, CHILD(n, 0)); |
| 1653 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1654 | return; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1655 | } |
| 1656 | m = n; |
| 1657 | do { |
| 1658 | m = CHILD(m, 0); |
| 1659 | } while (NCH(m) == 1); |
| 1660 | if (TYPE(m) != NAME) { |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 1661 | /* f(lambda x: x[0] = 3) ends up getting parsed with |
| 1662 | * LHS test = lambda x: x[0], and RHS test = 3. |
| 1663 | * SF bug 132313 points out that complaining about a keyword |
| 1664 | * then is very confusing. |
| 1665 | */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1666 | com_error(c, PyExc_SyntaxError, |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 1667 | TYPE(m) == lambdef ? |
| 1668 | "lambda cannot contain assignment" : |
| 1669 | "keyword can't be an expression"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1670 | } |
| 1671 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1672 | PyObject *v = PyString_InternFromString(STR(m)); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1673 | if (v != NULL && *pkeywords == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1674 | *pkeywords = PyDict_New(); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 1675 | if (v == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1676 | c->c_errors++; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 1677 | else if (*pkeywords == NULL) { |
| 1678 | c->c_errors++; |
| 1679 | Py_DECREF(v); |
| 1680 | } else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1681 | if (PyDict_GetItem(*pkeywords, v) != NULL) |
| 1682 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1683 | "duplicate keyword argument"); |
| 1684 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1685 | if (PyDict_SetItem(*pkeywords, v, v) != 0) |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1686 | c->c_errors++; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1687 | com_addoparg(c, LOAD_CONST, com_addconst(c, v)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1688 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1689 | Py_DECREF(v); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1690 | } |
| 1691 | } |
| 1692 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1695 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1696 | com_call_function(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1697 | { |
| 1698 | if (TYPE(n) == RPAR) { |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1699 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1700 | } |
| 1701 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1702 | PyObject *keywords = NULL; |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1703 | int i, na, nk; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1704 | int lineno = n->n_lineno; |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1705 | int star_flag = 0; |
| 1706 | int starstar_flag = 0; |
| 1707 | int opcode; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1708 | REQ(n, arglist); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1709 | na = 0; |
| 1710 | nk = 0; |
| 1711 | for (i = 0; i < NCH(n); i += 2) { |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1712 | node *ch = CHILD(n, i); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1713 | if (TYPE(ch) == STAR || |
| 1714 | TYPE(ch) == DOUBLESTAR) |
| 1715 | break; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1716 | if (ch->n_lineno != lineno) { |
| 1717 | lineno = ch->n_lineno; |
| 1718 | com_addoparg(c, SET_LINENO, lineno); |
| 1719 | } |
| 1720 | com_argument(c, ch, &keywords); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1721 | if (keywords == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1722 | na++; |
| 1723 | else |
| 1724 | nk++; |
| 1725 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1726 | Py_XDECREF(keywords); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1727 | while (i < NCH(n)) { |
| 1728 | node *tok = CHILD(n, i); |
| 1729 | node *ch = CHILD(n, i+1); |
| 1730 | i += 3; |
| 1731 | switch (TYPE(tok)) { |
| 1732 | case STAR: star_flag = 1; break; |
| 1733 | case DOUBLESTAR: starstar_flag = 1; break; |
| 1734 | } |
| 1735 | com_node(c, ch); |
| 1736 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1737 | if (na > 255 || nk > 255) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1738 | com_error(c, PyExc_SyntaxError, |
| 1739 | "more than 255 arguments"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1740 | } |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1741 | if (star_flag || starstar_flag) |
Jeremy Hylton | e4fb958 | 2000-03-29 00:10:44 +0000 | [diff] [blame] | 1742 | opcode = CALL_FUNCTION_VAR - 1 + |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1743 | star_flag + (starstar_flag << 1); |
| 1744 | else |
| 1745 | opcode = CALL_FUNCTION; |
| 1746 | com_addoparg(c, opcode, na | (nk << 8)); |
| 1747 | com_pop(c, na + 2*nk + star_flag + starstar_flag); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1748 | } |
| 1749 | } |
| 1750 | |
| 1751 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1752 | com_select_member(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1753 | { |
| 1754 | com_addopname(c, LOAD_ATTR, n); |
| 1755 | } |
| 1756 | |
| 1757 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1758 | com_sliceobj(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1759 | { |
| 1760 | int i=0; |
| 1761 | int ns=2; /* number of slice arguments */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1762 | node *ch; |
| 1763 | |
| 1764 | /* first argument */ |
| 1765 | if (TYPE(CHILD(n,i)) == COLON) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1766 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1767 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1768 | i++; |
| 1769 | } |
| 1770 | else { |
| 1771 | com_node(c, CHILD(n,i)); |
| 1772 | i++; |
| 1773 | REQ(CHILD(n,i),COLON); |
| 1774 | i++; |
| 1775 | } |
| 1776 | /* second argument */ |
| 1777 | if (i < NCH(n) && TYPE(CHILD(n,i)) == test) { |
| 1778 | com_node(c, CHILD(n,i)); |
| 1779 | i++; |
| 1780 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1781 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1782 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1783 | com_push(c, 1); |
| 1784 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1785 | /* remaining arguments */ |
| 1786 | for (; i < NCH(n); i++) { |
| 1787 | ns++; |
| 1788 | ch=CHILD(n,i); |
| 1789 | REQ(ch, sliceop); |
| 1790 | if (NCH(ch) == 1) { |
| 1791 | /* right argument of ':' missing */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1792 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1793 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1794 | } |
| 1795 | else |
| 1796 | com_node(c, CHILD(ch,1)); |
| 1797 | } |
| 1798 | com_addoparg(c, BUILD_SLICE, ns); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1799 | com_pop(c, 1 + (ns == 3)); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
| 1802 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1803 | com_subscript(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1804 | { |
| 1805 | node *ch; |
| 1806 | REQ(n, subscript); |
| 1807 | ch = CHILD(n,0); |
| 1808 | /* check for rubber index */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1809 | if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) { |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 1810 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1811 | com_push(c, 1); |
| 1812 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1813 | else { |
| 1814 | /* check for slice */ |
| 1815 | if ((TYPE(ch) == COLON || NCH(n) > 1)) |
| 1816 | com_sliceobj(c, n); |
| 1817 | else { |
| 1818 | REQ(ch, test); |
| 1819 | com_node(c, ch); |
| 1820 | } |
| 1821 | } |
| 1822 | } |
| 1823 | |
| 1824 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1825 | com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1826 | { |
| 1827 | int i, op; |
| 1828 | REQ(n, subscriptlist); |
| 1829 | /* Check to make backward compatible slice behavior for '[i:j]' */ |
| 1830 | if (NCH(n) == 1) { |
| 1831 | node *sub = CHILD(n, 0); /* subscript */ |
Thomas Wouters | e8643c4 | 2000-08-05 21:37:50 +0000 | [diff] [blame] | 1832 | /* 'Basic' slice, should have exactly one colon. */ |
| 1833 | if ((TYPE(CHILD(sub, 0)) == COLON |
| 1834 | || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON)) |
| 1835 | && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop)) |
| 1836 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1837 | switch (assigning) { |
| 1838 | case OP_DELETE: |
| 1839 | op = DELETE_SLICE; |
| 1840 | break; |
| 1841 | case OP_ASSIGN: |
| 1842 | op = STORE_SLICE; |
| 1843 | break; |
| 1844 | case OP_APPLY: |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1845 | op = SLICE; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1846 | break; |
| 1847 | default: |
| 1848 | com_augassign_slice(c, sub, assigning, augn); |
| 1849 | return; |
| 1850 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1851 | com_slice(c, sub, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1852 | if (op == STORE_SLICE) |
| 1853 | com_pop(c, 2); |
| 1854 | else if (op == DELETE_SLICE) |
| 1855 | com_pop(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1856 | return; |
| 1857 | } |
| 1858 | } |
| 1859 | /* Else normal subscriptlist. Compile each subscript. */ |
| 1860 | for (i = 0; i < NCH(n); i += 2) |
| 1861 | com_subscript(c, CHILD(n, i)); |
| 1862 | /* Put multiple subscripts into a tuple */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1863 | if (NCH(n) > 1) { |
| 1864 | i = (NCH(n)+1) / 2; |
| 1865 | com_addoparg(c, BUILD_TUPLE, i); |
| 1866 | com_pop(c, i-1); |
| 1867 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1868 | switch (assigning) { |
| 1869 | case OP_DELETE: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1870 | op = DELETE_SUBSCR; |
| 1871 | i = 2; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1872 | break; |
| 1873 | default: |
| 1874 | case OP_ASSIGN: |
| 1875 | op = STORE_SUBSCR; |
| 1876 | i = 3; |
| 1877 | break; |
| 1878 | case OP_APPLY: |
| 1879 | op = BINARY_SUBSCR; |
| 1880 | i = 1; |
| 1881 | break; |
| 1882 | } |
| 1883 | if (assigning > OP_APPLY) { |
| 1884 | com_addoparg(c, DUP_TOPX, 2); |
| 1885 | com_push(c, 2); |
| 1886 | com_addbyte(c, BINARY_SUBSCR); |
| 1887 | com_pop(c, 1); |
| 1888 | com_node(c, augn); |
| 1889 | com_addbyte(c, assigning); |
| 1890 | com_pop(c, 1); |
| 1891 | com_addbyte(c, ROT_THREE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1892 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1893 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1894 | com_pop(c, i); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1898 | com_apply_trailer(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1899 | { |
| 1900 | REQ(n, trailer); |
| 1901 | switch (TYPE(CHILD(n, 0))) { |
| 1902 | case LPAR: |
| 1903 | com_call_function(c, CHILD(n, 1)); |
| 1904 | break; |
| 1905 | case DOT: |
| 1906 | com_select_member(c, CHILD(n, 1)); |
| 1907 | break; |
| 1908 | case LSQB: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1909 | com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1910 | break; |
| 1911 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1912 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1913 | "com_apply_trailer: unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1918 | com_power(struct compiling *c, node *n) |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1919 | { |
| 1920 | int i; |
| 1921 | REQ(n, power); |
| 1922 | com_atom(c, CHILD(n, 0)); |
| 1923 | for (i = 1; i < NCH(n); i++) { |
| 1924 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
| 1925 | com_factor(c, CHILD(n, i+1)); |
| 1926 | com_addbyte(c, BINARY_POWER); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1927 | com_pop(c, 1); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1928 | break; |
| 1929 | } |
| 1930 | else |
| 1931 | com_apply_trailer(c, CHILD(n, i)); |
| 1932 | } |
| 1933 | } |
| 1934 | |
| 1935 | static void |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1936 | com_invert_constant(struct compiling *c, node *n) |
| 1937 | { |
| 1938 | /* Compute the inverse of int and longs and use them directly, |
| 1939 | but be prepared to generate code for all other |
| 1940 | possibilities (invalid numbers, floats, complex). |
| 1941 | */ |
| 1942 | PyObject *num, *inv = NULL; |
| 1943 | int i; |
| 1944 | |
| 1945 | REQ(n, NUMBER); |
| 1946 | num = parsenumber(c, STR(n)); |
| 1947 | if (num == NULL) |
| 1948 | i = 255; |
| 1949 | else { |
| 1950 | inv = PyNumber_Invert(num); |
| 1951 | if (inv == NULL) { |
| 1952 | PyErr_Clear(); |
| 1953 | i = com_addconst(c, num); |
| 1954 | } else { |
| 1955 | i = com_addconst(c, inv); |
| 1956 | Py_DECREF(inv); |
| 1957 | } |
| 1958 | Py_DECREF(num); |
| 1959 | } |
| 1960 | com_addoparg(c, LOAD_CONST, i); |
| 1961 | com_push(c, 1); |
| 1962 | if (num != NULL && inv == NULL) |
| 1963 | com_addbyte(c, UNARY_INVERT); |
| 1964 | } |
| 1965 | |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 1966 | static int |
| 1967 | is_float_zero(const char *p) |
| 1968 | { |
| 1969 | int found_radix_point = 0; |
| 1970 | int ch; |
| 1971 | while ((ch = Py_CHARMASK(*p++)) != '\0') { |
| 1972 | switch (ch) { |
| 1973 | case '0': |
| 1974 | /* no reason to believe it's not 0 -- continue */ |
| 1975 | break; |
| 1976 | |
| 1977 | case 'e': case 'E': case 'j': case 'J': |
| 1978 | /* If this was a hex constant, we already would have |
| 1979 | returned 0 due to the 'x' or 'X', so 'e' or 'E' |
| 1980 | must be an exponent marker, and we haven't yet |
| 1981 | seen a non-zero digit, and it doesn't matter what |
| 1982 | the exponent is then. For 'j' or 'J' similarly, |
| 1983 | except that this is an imaginary 0 then. */ |
| 1984 | return 1; |
| 1985 | |
| 1986 | case '.': |
| 1987 | found_radix_point = 1; |
| 1988 | break; |
| 1989 | |
| 1990 | default: |
| 1991 | return 0; |
| 1992 | } |
| 1993 | } |
| 1994 | return found_radix_point; |
| 1995 | } |
| 1996 | |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1997 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1998 | com_factor(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1999 | { |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2000 | int childtype = TYPE(CHILD(n, 0)); |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2001 | node *pfactor, *ppower, *patom, *pnum; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2002 | REQ(n, factor); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2003 | /* If the unary +, -, or ~ operator is applied to a constant, |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2004 | don't generate a UNARY_xxx opcode. Just store the |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2005 | approriate value as a constant. If the value is negative, |
| 2006 | extend the string containing the constant and insert a |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2007 | negative in the 0th position -- unless we're doing unary minus |
| 2008 | of a floating zero! In that case the sign is significant, but |
| 2009 | the const dict can't distinguish +0.0 from -0.0. |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2010 | */ |
| 2011 | if ((childtype == PLUS || childtype == MINUS || childtype == TILDE) |
Fred Drake | 14ef244 | 2001-08-30 18:53:25 +0000 | [diff] [blame] | 2012 | && NCH(n) == 2 |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2013 | && TYPE((pfactor = CHILD(n, 1))) == factor |
| 2014 | && NCH(pfactor) == 1 |
| 2015 | && TYPE((ppower = CHILD(pfactor, 0))) == power |
| 2016 | && NCH(ppower) == 1 |
| 2017 | && TYPE((patom = CHILD(ppower, 0))) == atom |
| 2018 | && TYPE((pnum = CHILD(patom, 0))) == NUMBER |
| 2019 | && !(childtype == MINUS && is_float_zero(STR(pnum)))) { |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2020 | if (childtype == TILDE) { |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2021 | com_invert_constant(c, pnum); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2022 | return; |
| 2023 | } |
| 2024 | if (childtype == MINUS) { |
Tim Peters | 51e7f5c | 2002-04-22 02:33:27 +0000 | [diff] [blame] | 2025 | char *s = PyMem_Malloc(strlen(STR(pnum)) + 2); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2026 | if (s == NULL) { |
| 2027 | com_error(c, PyExc_MemoryError, ""); |
| 2028 | com_addbyte(c, 255); |
| 2029 | return; |
| 2030 | } |
| 2031 | s[0] = '-'; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2032 | strcpy(s + 1, STR(pnum)); |
Tim Peters | 51e7f5c | 2002-04-22 02:33:27 +0000 | [diff] [blame] | 2033 | PyMem_Free(STR(pnum)); |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2034 | STR(pnum) = s; |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2035 | } |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2036 | com_atom(c, patom); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2037 | } |
| 2038 | else if (childtype == PLUS) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2039 | com_factor(c, CHILD(n, 1)); |
| 2040 | com_addbyte(c, UNARY_POSITIVE); |
| 2041 | } |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2042 | else if (childtype == MINUS) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2043 | com_factor(c, CHILD(n, 1)); |
| 2044 | com_addbyte(c, UNARY_NEGATIVE); |
| 2045 | } |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2046 | else if (childtype == TILDE) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2047 | com_factor(c, CHILD(n, 1)); |
| 2048 | com_addbyte(c, UNARY_INVERT); |
| 2049 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2050 | else { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2051 | com_power(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2052 | } |
| 2053 | } |
| 2054 | |
| 2055 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2056 | com_term(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2057 | { |
| 2058 | int i; |
| 2059 | int op; |
| 2060 | REQ(n, term); |
| 2061 | com_factor(c, CHILD(n, 0)); |
| 2062 | for (i = 2; i < NCH(n); i += 2) { |
| 2063 | com_factor(c, CHILD(n, i)); |
| 2064 | switch (TYPE(CHILD(n, i-1))) { |
| 2065 | case STAR: |
| 2066 | op = BINARY_MULTIPLY; |
| 2067 | break; |
| 2068 | case SLASH: |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2069 | if (c->c_flags & CO_FUTURE_DIVISION) |
| 2070 | op = BINARY_TRUE_DIVIDE; |
| 2071 | else |
| 2072 | op = BINARY_DIVIDE; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2073 | break; |
| 2074 | case PERCENT: |
| 2075 | op = BINARY_MODULO; |
| 2076 | break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2077 | case DOUBLESLASH: |
| 2078 | op = BINARY_FLOOR_DIVIDE; |
| 2079 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2080 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2081 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2082 | "com_term: operator not *, /, // or %"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2083 | op = 255; |
| 2084 | } |
| 2085 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2086 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2087 | } |
| 2088 | } |
| 2089 | |
| 2090 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2091 | com_arith_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2092 | { |
| 2093 | int i; |
| 2094 | int op; |
| 2095 | REQ(n, arith_expr); |
| 2096 | com_term(c, CHILD(n, 0)); |
| 2097 | for (i = 2; i < NCH(n); i += 2) { |
| 2098 | com_term(c, CHILD(n, i)); |
| 2099 | switch (TYPE(CHILD(n, i-1))) { |
| 2100 | case PLUS: |
| 2101 | op = BINARY_ADD; |
| 2102 | break; |
| 2103 | case MINUS: |
| 2104 | op = BINARY_SUBTRACT; |
| 2105 | break; |
| 2106 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2107 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2108 | "com_arith_expr: operator not + or -"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2109 | op = 255; |
| 2110 | } |
| 2111 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2112 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2113 | } |
| 2114 | } |
| 2115 | |
| 2116 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2117 | com_shift_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2118 | { |
| 2119 | int i; |
| 2120 | int op; |
| 2121 | REQ(n, shift_expr); |
| 2122 | com_arith_expr(c, CHILD(n, 0)); |
| 2123 | for (i = 2; i < NCH(n); i += 2) { |
| 2124 | com_arith_expr(c, CHILD(n, i)); |
| 2125 | switch (TYPE(CHILD(n, i-1))) { |
| 2126 | case LEFTSHIFT: |
| 2127 | op = BINARY_LSHIFT; |
| 2128 | break; |
| 2129 | case RIGHTSHIFT: |
| 2130 | op = BINARY_RSHIFT; |
| 2131 | break; |
| 2132 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2133 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2134 | "com_shift_expr: operator not << or >>"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2135 | op = 255; |
| 2136 | } |
| 2137 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2138 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2139 | } |
| 2140 | } |
| 2141 | |
| 2142 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2143 | com_and_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2144 | { |
| 2145 | int i; |
| 2146 | int op; |
| 2147 | REQ(n, and_expr); |
| 2148 | com_shift_expr(c, CHILD(n, 0)); |
| 2149 | for (i = 2; i < NCH(n); i += 2) { |
| 2150 | com_shift_expr(c, CHILD(n, i)); |
| 2151 | if (TYPE(CHILD(n, i-1)) == AMPER) { |
| 2152 | op = BINARY_AND; |
| 2153 | } |
| 2154 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2155 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2156 | "com_and_expr: operator not &"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2157 | op = 255; |
| 2158 | } |
| 2159 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2160 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2165 | com_xor_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2166 | { |
| 2167 | int i; |
| 2168 | int op; |
| 2169 | REQ(n, xor_expr); |
| 2170 | com_and_expr(c, CHILD(n, 0)); |
| 2171 | for (i = 2; i < NCH(n); i += 2) { |
| 2172 | com_and_expr(c, CHILD(n, i)); |
| 2173 | if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) { |
| 2174 | op = BINARY_XOR; |
| 2175 | } |
| 2176 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2177 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2178 | "com_xor_expr: operator not ^"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2179 | op = 255; |
| 2180 | } |
| 2181 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2182 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2183 | } |
| 2184 | } |
| 2185 | |
| 2186 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2187 | com_expr(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2188 | { |
| 2189 | int i; |
| 2190 | int op; |
| 2191 | REQ(n, expr); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2192 | com_xor_expr(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2193 | for (i = 2; i < NCH(n); i += 2) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2194 | com_xor_expr(c, CHILD(n, i)); |
| 2195 | if (TYPE(CHILD(n, i-1)) == VBAR) { |
| 2196 | op = BINARY_OR; |
| 2197 | } |
| 2198 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2199 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2200 | "com_expr: expr operator not |"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2201 | op = 255; |
| 2202 | } |
| 2203 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2204 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2205 | } |
| 2206 | } |
| 2207 | |
| 2208 | static enum cmp_op |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2209 | cmp_type(node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2210 | { |
| 2211 | REQ(n, comp_op); |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2212 | /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '==' |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2213 | | 'in' | 'not' 'in' | 'is' | 'is' not' */ |
| 2214 | if (NCH(n) == 1) { |
| 2215 | n = CHILD(n, 0); |
| 2216 | switch (TYPE(n)) { |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2217 | case LESS: return PyCmp_LT; |
| 2218 | case GREATER: return PyCmp_GT; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2219 | case EQEQUAL: /* == */ |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2220 | case EQUAL: return PyCmp_EQ; |
| 2221 | case LESSEQUAL: return PyCmp_LE; |
| 2222 | case GREATEREQUAL: return PyCmp_GE; |
| 2223 | case NOTEQUAL: return PyCmp_NE; /* <> or != */ |
| 2224 | case NAME: if (strcmp(STR(n), "in") == 0) return PyCmp_IN; |
| 2225 | if (strcmp(STR(n), "is") == 0) return PyCmp_IS; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2226 | } |
| 2227 | } |
| 2228 | else if (NCH(n) == 2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2229 | switch (TYPE(CHILD(n, 0))) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2230 | case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2231 | return PyCmp_NOT_IN; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2232 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2233 | return PyCmp_IS_NOT; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2234 | } |
| 2235 | } |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2236 | return PyCmp_BAD; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
| 2239 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2240 | com_comparison(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2241 | { |
| 2242 | int i; |
| 2243 | enum cmp_op op; |
| 2244 | int anchor; |
| 2245 | REQ(n, comparison); /* comparison: expr (comp_op expr)* */ |
| 2246 | com_expr(c, CHILD(n, 0)); |
| 2247 | if (NCH(n) == 1) |
| 2248 | return; |
| 2249 | |
| 2250 | /**************************************************************** |
| 2251 | The following code is generated for all but the last |
| 2252 | comparison in a chain: |
| 2253 | |
| 2254 | label: on stack: opcode: jump to: |
| 2255 | |
| 2256 | a <code to load b> |
| 2257 | a, b DUP_TOP |
| 2258 | a, b, b ROT_THREE |
| 2259 | b, a, b COMPARE_OP |
| 2260 | b, 0-or-1 JUMP_IF_FALSE L1 |
| 2261 | b, 1 POP_TOP |
| 2262 | b |
| 2263 | |
| 2264 | We are now ready to repeat this sequence for the next |
| 2265 | comparison in the chain. |
| 2266 | |
| 2267 | For the last we generate: |
| 2268 | |
| 2269 | b <code to load c> |
| 2270 | b, c COMPARE_OP |
| 2271 | 0-or-1 |
| 2272 | |
| 2273 | If there were any jumps to L1 (i.e., there was more than one |
| 2274 | comparison), we generate: |
| 2275 | |
| 2276 | 0-or-1 JUMP_FORWARD L2 |
| 2277 | L1: b, 0 ROT_TWO |
| 2278 | 0, b POP_TOP |
| 2279 | 0 |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2280 | L2: 0-or-1 |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2281 | ****************************************************************/ |
| 2282 | |
| 2283 | anchor = 0; |
| 2284 | |
| 2285 | for (i = 2; i < NCH(n); i += 2) { |
| 2286 | com_expr(c, CHILD(n, i)); |
| 2287 | if (i+2 < NCH(n)) { |
| 2288 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2289 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2290 | com_addbyte(c, ROT_THREE); |
| 2291 | } |
| 2292 | op = cmp_type(CHILD(n, i-1)); |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2293 | if (op == PyCmp_BAD) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2294 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2295 | "com_comparison: unknown comparison op"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2296 | } |
| 2297 | com_addoparg(c, COMPARE_OP, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2298 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2299 | if (i+2 < NCH(n)) { |
| 2300 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2301 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2302 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2303 | } |
| 2304 | } |
| 2305 | |
| 2306 | if (anchor) { |
| 2307 | int anchor2 = 0; |
| 2308 | com_addfwref(c, JUMP_FORWARD, &anchor2); |
| 2309 | com_backpatch(c, anchor); |
| 2310 | com_addbyte(c, ROT_TWO); |
| 2311 | com_addbyte(c, POP_TOP); |
| 2312 | com_backpatch(c, anchor2); |
| 2313 | } |
| 2314 | } |
| 2315 | |
| 2316 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2317 | com_not_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2318 | { |
| 2319 | REQ(n, not_test); /* 'not' not_test | comparison */ |
| 2320 | if (NCH(n) == 1) { |
| 2321 | com_comparison(c, CHILD(n, 0)); |
| 2322 | } |
| 2323 | else { |
| 2324 | com_not_test(c, CHILD(n, 1)); |
| 2325 | com_addbyte(c, UNARY_NOT); |
| 2326 | } |
| 2327 | } |
| 2328 | |
| 2329 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2330 | com_and_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2331 | { |
| 2332 | int i; |
| 2333 | int anchor; |
| 2334 | REQ(n, and_test); /* not_test ('and' not_test)* */ |
| 2335 | anchor = 0; |
| 2336 | i = 0; |
| 2337 | for (;;) { |
| 2338 | com_not_test(c, CHILD(n, i)); |
| 2339 | if ((i += 2) >= NCH(n)) |
| 2340 | break; |
| 2341 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2342 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2343 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2344 | } |
| 2345 | if (anchor) |
| 2346 | com_backpatch(c, anchor); |
| 2347 | } |
| 2348 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2349 | static int |
| 2350 | com_make_closure(struct compiling *c, PyCodeObject *co) |
| 2351 | { |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 2352 | int i, free = PyCode_GetNumFree(co); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2353 | if (free == 0) |
| 2354 | return 0; |
| 2355 | for (i = 0; i < free; ++i) { |
| 2356 | /* Bypass com_addop_varname because it will generate |
| 2357 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 2358 | */ |
| 2359 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 2360 | int arg, reftype; |
| 2361 | |
| 2362 | /* Special case: If a class contains a method with a |
| 2363 | free variable that has the same name as a method, |
| 2364 | the name will be considered free *and* local in the |
| 2365 | class. It should be handled by the closure, as |
| 2366 | well as by the normal name loookup logic. |
| 2367 | */ |
| 2368 | reftype = get_ref_type(c, PyString_AS_STRING(name)); |
| 2369 | if (reftype == CELL) |
| 2370 | arg = com_lookup_arg(c->c_cellvars, name); |
| 2371 | else /* (reftype == FREE) */ |
| 2372 | arg = com_lookup_arg(c->c_freevars, name); |
| 2373 | if (arg == -1) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 2374 | fprintf(stderr, "lookup %s in %s %d %d\n" |
| 2375 | "freevars of %s: %s\n", |
| 2376 | PyObject_REPR(name), |
| 2377 | c->c_name, |
| 2378 | reftype, arg, |
| 2379 | PyString_AS_STRING(co->co_name), |
| 2380 | PyObject_REPR(co->co_freevars)); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2381 | Py_FatalError("com_make_closure()"); |
| 2382 | } |
| 2383 | com_addoparg(c, LOAD_CLOSURE, arg); |
| 2384 | |
| 2385 | } |
| 2386 | com_push(c, free); |
| 2387 | return 1; |
| 2388 | } |
| 2389 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2390 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2391 | com_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2392 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2393 | REQ(n, test); /* and_test ('or' and_test)* | lambdef */ |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2394 | if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2395 | PyCodeObject *co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2396 | int i, closure; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2397 | int ndefs = com_argdefs(c, CHILD(n, 0)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 2398 | symtable_enter_scope(c->c_symtable, "lambda", lambdef, |
| 2399 | n->n_lineno); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2400 | co = icompile(CHILD(n, 0), c); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2401 | if (co == NULL) { |
| 2402 | c->c_errors++; |
| 2403 | return; |
| 2404 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2405 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2406 | i = com_addconst(c, (PyObject *)co); |
| 2407 | closure = com_make_closure(c, co); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2408 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2409 | com_push(c, 1); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2410 | if (closure) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2411 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 2412 | com_pop(c, PyCode_GetNumFree(co)); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2413 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2414 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2415 | Py_DECREF(co); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2416 | com_pop(c, ndefs); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2417 | } |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2418 | else { |
| 2419 | int anchor = 0; |
| 2420 | int i = 0; |
| 2421 | for (;;) { |
| 2422 | com_and_test(c, CHILD(n, i)); |
| 2423 | if ((i += 2) >= NCH(n)) |
| 2424 | break; |
| 2425 | com_addfwref(c, JUMP_IF_TRUE, &anchor); |
| 2426 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2427 | com_pop(c, 1); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2428 | } |
| 2429 | if (anchor) |
| 2430 | com_backpatch(c, anchor); |
| 2431 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
| 2434 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2435 | com_list(struct compiling *c, node *n, int toplevel) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2436 | { |
| 2437 | /* exprlist: expr (',' expr)* [',']; likewise for testlist */ |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 2438 | if (NCH(n) == 1 && !toplevel) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2439 | com_node(c, CHILD(n, 0)); |
| 2440 | } |
| 2441 | else { |
| 2442 | int i; |
| 2443 | int len; |
| 2444 | len = (NCH(n) + 1) / 2; |
| 2445 | for (i = 0; i < NCH(n); i += 2) |
| 2446 | com_node(c, CHILD(n, i)); |
| 2447 | com_addoparg(c, BUILD_TUPLE, len); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2448 | com_pop(c, len-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2449 | } |
| 2450 | } |
| 2451 | |
| 2452 | |
| 2453 | /* Begin of assignment compilation */ |
| 2454 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2455 | |
| 2456 | static void |
| 2457 | com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn) |
| 2458 | { |
| 2459 | com_addbyte(c, DUP_TOP); |
| 2460 | com_push(c, 1); |
| 2461 | com_addopname(c, LOAD_ATTR, n); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2462 | com_node(c, augn); |
| 2463 | com_addbyte(c, opcode); |
| 2464 | com_pop(c, 1); |
| 2465 | com_addbyte(c, ROT_TWO); |
| 2466 | com_addopname(c, STORE_ATTR, n); |
| 2467 | com_pop(c, 2); |
| 2468 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2469 | |
| 2470 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2471 | com_assign_attr(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2472 | { |
| 2473 | com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2474 | com_pop(c, assigning ? 2 : 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2478 | 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] | 2479 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2480 | REQ(n, trailer); |
| 2481 | switch (TYPE(CHILD(n, 0))) { |
| 2482 | case LPAR: /* '(' [exprlist] ')' */ |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 2483 | if (assigning == OP_DELETE) |
| 2484 | com_error(c, PyExc_SyntaxError, |
| 2485 | "can't delete function call"); |
| 2486 | else |
| 2487 | com_error(c, PyExc_SyntaxError, |
| 2488 | "can't assign to function call"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2489 | break; |
| 2490 | case DOT: /* '.' NAME */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2491 | if (assigning > OP_APPLY) |
| 2492 | com_augassign_attr(c, CHILD(n, 1), assigning, augn); |
| 2493 | else |
| 2494 | com_assign_attr(c, CHILD(n, 1), assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2495 | break; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2496 | case LSQB: /* '[' subscriptlist ']' */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2497 | com_subscriptlist(c, CHILD(n, 1), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2498 | break; |
| 2499 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2500 | com_error(c, PyExc_SystemError, "unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2501 | } |
| 2502 | } |
| 2503 | |
| 2504 | static void |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2505 | com_assign_sequence(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2506 | { |
| 2507 | int i; |
Thomas Wouters | b59290f | 2000-08-25 05:41:11 +0000 | [diff] [blame] | 2508 | if (TYPE(n) != testlist && TYPE(n) != listmaker) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2509 | REQ(n, exprlist); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2510 | if (assigning) { |
| 2511 | i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2512 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2513 | com_push(c, i-1); |
| 2514 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2515 | for (i = 0; i < NCH(n); i += 2) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2516 | com_assign(c, CHILD(n, i), assigning, NULL); |
| 2517 | } |
| 2518 | |
| 2519 | static void |
| 2520 | com_augassign_name(struct compiling *c, node *n, int opcode, node *augn) |
| 2521 | { |
| 2522 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2523 | com_addop_varname(c, VAR_LOAD, STR(n)); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2524 | com_push(c, 1); |
| 2525 | com_node(c, augn); |
| 2526 | com_addbyte(c, opcode); |
| 2527 | com_pop(c, 1); |
| 2528 | com_assign_name(c, n, OP_ASSIGN); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2529 | } |
| 2530 | |
| 2531 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2532 | com_assign_name(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2533 | { |
| 2534 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2535 | com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2536 | if (assigning) |
| 2537 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2538 | } |
| 2539 | |
| 2540 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2541 | com_assign(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2542 | { |
| 2543 | /* Loop to avoid trivial recursion */ |
| 2544 | for (;;) { |
| 2545 | switch (TYPE(n)) { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2546 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2547 | case exprlist: |
| 2548 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 2549 | case testlist1: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2550 | if (NCH(n) > 1) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2551 | if (assigning > OP_APPLY) { |
| 2552 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 2553 | "augmented assign to tuple not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2554 | return; |
| 2555 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2556 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2557 | return; |
| 2558 | } |
| 2559 | n = CHILD(n, 0); |
| 2560 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2561 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2562 | case test: |
| 2563 | case and_test: |
| 2564 | case not_test: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2565 | case comparison: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2566 | case expr: |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2567 | case xor_expr: |
| 2568 | case and_expr: |
| 2569 | case shift_expr: |
| 2570 | case arith_expr: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2571 | case term: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2572 | case factor: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2573 | if (NCH(n) > 1) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2574 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2575 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2576 | return; |
| 2577 | } |
| 2578 | n = CHILD(n, 0); |
| 2579 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2580 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2581 | case power: /* atom trailer* ('**' power)* |
| 2582 | ('+'|'-'|'~') factor | atom trailer* */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2583 | if (TYPE(CHILD(n, 0)) != atom) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2584 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2585 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2586 | return; |
| 2587 | } |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2588 | if (NCH(n) > 1) { /* trailer or exponent present */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2589 | int i; |
| 2590 | com_node(c, CHILD(n, 0)); |
| 2591 | for (i = 1; i+1 < NCH(n); i++) { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2592 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2593 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2594 | "can't assign to operator"); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2595 | return; |
| 2596 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2597 | com_apply_trailer(c, CHILD(n, i)); |
| 2598 | } /* NB i is still alive */ |
| 2599 | com_assign_trailer(c, |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2600 | CHILD(n, i), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2601 | return; |
| 2602 | } |
| 2603 | n = CHILD(n, 0); |
| 2604 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2605 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2606 | case atom: |
| 2607 | switch (TYPE(CHILD(n, 0))) { |
| 2608 | case LPAR: |
| 2609 | n = CHILD(n, 1); |
| 2610 | if (TYPE(n) == RPAR) { |
| 2611 | /* XXX Should allow () = () ??? */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2612 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2613 | "can't assign to ()"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2614 | return; |
| 2615 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2616 | if (assigning > OP_APPLY) { |
| 2617 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2618 | "augmented assign to tuple not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2619 | return; |
| 2620 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2621 | break; |
| 2622 | case LSQB: |
| 2623 | n = CHILD(n, 1); |
| 2624 | if (TYPE(n) == RSQB) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2625 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2626 | "can't assign to []"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2627 | return; |
| 2628 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2629 | if (assigning > OP_APPLY) { |
| 2630 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2631 | "augmented assign to list not possible"); |
| 2632 | return; |
| 2633 | } |
| 2634 | if (NCH(n) > 1 |
| 2635 | && TYPE(CHILD(n, 1)) == list_for) { |
| 2636 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2637 | "can't assign to list comprehension"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2638 | return; |
| 2639 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2640 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2641 | return; |
| 2642 | case NAME: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2643 | if (assigning > OP_APPLY) |
| 2644 | com_augassign_name(c, CHILD(n, 0), |
| 2645 | assigning, augn); |
| 2646 | else |
| 2647 | com_assign_name(c, CHILD(n, 0), |
| 2648 | assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2649 | return; |
| 2650 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2651 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2652 | "can't assign to literal"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2653 | return; |
| 2654 | } |
| 2655 | break; |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2656 | |
| 2657 | case lambdef: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2658 | com_error(c, PyExc_SyntaxError, |
| 2659 | "can't assign to lambda"); |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2660 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2661 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2662 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2663 | com_error(c, PyExc_SystemError, |
| 2664 | "com_assign: bad node"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2665 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2666 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2667 | } |
| 2668 | } |
| 2669 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2670 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2671 | static void |
| 2672 | com_augassign(struct compiling *c, node *n) |
| 2673 | { |
| 2674 | int opcode; |
| 2675 | |
| 2676 | switch (STR(CHILD(CHILD(n, 1), 0))[0]) { |
| 2677 | case '+': opcode = INPLACE_ADD; break; |
| 2678 | case '-': opcode = INPLACE_SUBTRACT; break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2679 | case '/': |
| 2680 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '/') |
| 2681 | opcode = INPLACE_FLOOR_DIVIDE; |
| 2682 | else if (c->c_flags & CO_FUTURE_DIVISION) |
| 2683 | opcode = INPLACE_TRUE_DIVIDE; |
| 2684 | else |
| 2685 | opcode = INPLACE_DIVIDE; |
| 2686 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2687 | case '%': opcode = INPLACE_MODULO; break; |
| 2688 | case '<': opcode = INPLACE_LSHIFT; break; |
| 2689 | case '>': opcode = INPLACE_RSHIFT; break; |
| 2690 | case '&': opcode = INPLACE_AND; break; |
| 2691 | case '^': opcode = INPLACE_XOR; break; |
| 2692 | case '|': opcode = INPLACE_OR; break; |
| 2693 | case '*': |
| 2694 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '*') |
| 2695 | opcode = INPLACE_POWER; |
| 2696 | else |
| 2697 | opcode = INPLACE_MULTIPLY; |
| 2698 | break; |
| 2699 | default: |
| 2700 | com_error(c, PyExc_SystemError, "com_augassign: bad operator"); |
| 2701 | return; |
| 2702 | } |
| 2703 | com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2)); |
| 2704 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2705 | |
| 2706 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2707 | com_expr_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2708 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2709 | REQ(n, expr_stmt); |
| 2710 | /* testlist (('=' testlist)* | augassign testlist) */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2711 | /* Forget it if we have just a doc string here */ |
Guido van Rossum | 5f5e817 | 1997-04-06 03:41:40 +0000 | [diff] [blame] | 2712 | if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2713 | return; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2714 | if (NCH(n) == 1) { |
| 2715 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2716 | if (c->c_interactive) |
| 2717 | com_addbyte(c, PRINT_EXPR); |
| 2718 | else |
| 2719 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2720 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2721 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2722 | else if (TYPE(CHILD(n,1)) == augassign) |
| 2723 | com_augassign(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2724 | else { |
| 2725 | int i; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2726 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2727 | for (i = 0; i < NCH(n)-2; i+=2) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2728 | if (i+2 < NCH(n)-2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2729 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2730 | com_push(c, 1); |
| 2731 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2732 | com_assign(c, CHILD(n, i), OP_ASSIGN, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2733 | } |
| 2734 | } |
| 2735 | } |
| 2736 | |
| 2737 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2738 | com_assert_stmt(struct compiling *c, node *n) |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2739 | { |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2740 | int a = 0; |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2741 | int i; |
| 2742 | REQ(n, assert_stmt); /* 'assert' test [',' test] */ |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2743 | if (Py_OptimizeFlag) |
| 2744 | return; |
| 2745 | /* Generate code like |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2746 | |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2747 | if not <test>: |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2748 | raise AssertionError [, <message>] |
| 2749 | |
| 2750 | where <message> is the second test, if present. |
| 2751 | */ |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2752 | com_node(c, CHILD(n, 1)); |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2753 | com_addfwref(c, JUMP_IF_TRUE, &a); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2754 | com_addbyte(c, POP_TOP); |
| 2755 | com_pop(c, 1); |
| 2756 | /* Raise that exception! */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2757 | com_addop_name(c, LOAD_GLOBAL, "AssertionError"); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2758 | com_push(c, 1); |
| 2759 | i = NCH(n)/2; /* Either 2 or 4 */ |
| 2760 | if (i > 1) |
| 2761 | com_node(c, CHILD(n, 3)); |
| 2762 | com_addoparg(c, RAISE_VARARGS, i); |
| 2763 | com_pop(c, i); |
| 2764 | /* The interpreter does not fall through */ |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2765 | /* Jump ends up here */ |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2766 | com_backpatch(c, a); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2767 | com_addbyte(c, POP_TOP); |
| 2768 | } |
| 2769 | |
| 2770 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2771 | com_print_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2772 | { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2773 | int i = 1; |
| 2774 | node* stream = NULL; |
| 2775 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2776 | REQ(n, print_stmt); /* 'print' (test ',')* [test] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2777 | |
| 2778 | /* are we using the extended print form? */ |
| 2779 | if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) { |
| 2780 | stream = CHILD(n, 2); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2781 | com_node(c, stream); |
| 2782 | /* stack: [...] => [... stream] */ |
| 2783 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2784 | if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA) |
| 2785 | i = 4; |
| 2786 | else |
| 2787 | i = 3; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2788 | } |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2789 | for (; i < NCH(n); i += 2) { |
| 2790 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2791 | com_addbyte(c, DUP_TOP); |
| 2792 | /* stack: [stream] => [stream stream] */ |
| 2793 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2794 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2795 | /* stack: [stream stream] => [stream stream obj] */ |
| 2796 | com_addbyte(c, ROT_TWO); |
| 2797 | /* stack: [stream stream obj] => [stream obj stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2798 | com_addbyte(c, PRINT_ITEM_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2799 | /* stack: [stream obj stream] => [stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2800 | com_pop(c, 2); |
| 2801 | } |
| 2802 | else { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2803 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2804 | /* stack: [...] => [... obj] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2805 | com_addbyte(c, PRINT_ITEM); |
| 2806 | com_pop(c, 1); |
| 2807 | } |
| 2808 | } |
| 2809 | /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */ |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2810 | if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2811 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2812 | /* must pop the extra stream object off the stack */ |
| 2813 | com_addbyte(c, POP_TOP); |
| 2814 | /* stack: [... stream] => [...] */ |
| 2815 | com_pop(c, 1); |
| 2816 | } |
| 2817 | } |
| 2818 | else { |
| 2819 | if (stream != NULL) { |
| 2820 | /* this consumes the last stream object on stack */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2821 | com_addbyte(c, PRINT_NEWLINE_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2822 | /* stack: [... stream] => [...] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2823 | com_pop(c, 1); |
| 2824 | } |
| 2825 | else |
| 2826 | com_addbyte(c, PRINT_NEWLINE); |
| 2827 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
| 2830 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2831 | com_return_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2832 | { |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2833 | REQ(n, return_stmt); /* 'return' [testlist] */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2834 | if (!c->c_infunction) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2835 | com_error(c, PyExc_SyntaxError, "'return' outside function"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2836 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2837 | if (c->c_flags & CO_GENERATOR) { |
| 2838 | if (NCH(n) > 1) { |
| 2839 | com_error(c, PyExc_SyntaxError, |
| 2840 | "'return' with argument inside generator"); |
| 2841 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 2842 | } |
| 2843 | if (NCH(n) < 2) { |
| 2844 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2845 | com_push(c, 1); |
| 2846 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 2847 | else |
| 2848 | com_node(c, CHILD(n, 1)); |
| 2849 | com_addbyte(c, RETURN_VALUE); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2850 | com_pop(c, 1); |
| 2851 | } |
| 2852 | |
| 2853 | static void |
| 2854 | com_yield_stmt(struct compiling *c, node *n) |
| 2855 | { |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 2856 | int i; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2857 | REQ(n, yield_stmt); /* 'yield' testlist */ |
| 2858 | if (!c->c_infunction) { |
| 2859 | com_error(c, PyExc_SyntaxError, "'yield' outside function"); |
| 2860 | } |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 2861 | |
| 2862 | for (i = 0; i < c->c_nblocks; ++i) { |
| 2863 | if (c->c_block[i] == SETUP_FINALLY) { |
| 2864 | com_error(c, PyExc_SyntaxError, |
| 2865 | "'yield' not allowed in a 'try' block " |
| 2866 | "with a 'finally' clause"); |
| 2867 | return; |
| 2868 | } |
| 2869 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2870 | com_node(c, CHILD(n, 1)); |
| 2871 | com_addbyte(c, YIELD_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2872 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2873 | } |
| 2874 | |
| 2875 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2876 | com_raise_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2877 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2878 | int i; |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 2879 | REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */ |
| 2880 | if (NCH(n) > 1) { |
| 2881 | com_node(c, CHILD(n, 1)); |
| 2882 | if (NCH(n) > 3) { |
| 2883 | com_node(c, CHILD(n, 3)); |
| 2884 | if (NCH(n) > 5) |
| 2885 | com_node(c, CHILD(n, 5)); |
| 2886 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2887 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2888 | i = NCH(n)/2; |
| 2889 | com_addoparg(c, RAISE_VARARGS, i); |
| 2890 | com_pop(c, i); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2891 | } |
| 2892 | |
| 2893 | static void |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2894 | com_from_import(struct compiling *c, node *n) |
| 2895 | { |
| 2896 | com_addopname(c, IMPORT_FROM, CHILD(n, 0)); |
| 2897 | com_push(c, 1); |
| 2898 | if (NCH(n) > 1) { |
| 2899 | if (strcmp(STR(CHILD(n, 1)), "as") != 0) { |
| 2900 | com_error(c, PyExc_SyntaxError, "invalid syntax"); |
| 2901 | return; |
| 2902 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2903 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2904 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2905 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2906 | com_pop(c, 1); |
| 2907 | } |
| 2908 | |
| 2909 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2910 | com_import_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2911 | { |
| 2912 | int i; |
| 2913 | REQ(n, import_stmt); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2914 | /* 'import' dotted_name (',' dotted_name)* | |
| 2915 | 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2916 | if (STR(CHILD(n, 0))[0] == 'f') { |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 2917 | PyObject *tup; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2918 | /* 'from' dotted_name 'import' ... */ |
| 2919 | REQ(CHILD(n, 1), dotted_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2920 | |
| 2921 | if (TYPE(CHILD(n, 3)) == STAR) { |
| 2922 | tup = Py_BuildValue("(s)", "*"); |
| 2923 | } else { |
| 2924 | tup = PyTuple_New((NCH(n) - 2)/2); |
| 2925 | for (i = 3; i < NCH(n); i += 2) { |
| 2926 | PyTuple_SET_ITEM(tup, (i-3)/2, |
| 2927 | PyString_FromString(STR( |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 2928 | CHILD(CHILD(n, i), 0)))); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2929 | } |
| 2930 | } |
| 2931 | com_addoparg(c, LOAD_CONST, com_addconst(c, tup)); |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 2932 | Py_DECREF(tup); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2933 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2934 | com_addopname(c, IMPORT_NAME, CHILD(n, 1)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2935 | if (TYPE(CHILD(n, 3)) == STAR) |
| 2936 | com_addbyte(c, IMPORT_STAR); |
| 2937 | else { |
| 2938 | for (i = 3; i < NCH(n); i += 2) |
| 2939 | com_from_import(c, CHILD(n, i)); |
| 2940 | com_addbyte(c, POP_TOP); |
| 2941 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2942 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2943 | } |
| 2944 | else { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2945 | /* 'import' ... */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2946 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2947 | node *subn = CHILD(n, i); |
| 2948 | REQ(subn, dotted_as_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2949 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2950 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2951 | com_addopname(c, IMPORT_NAME, CHILD(subn, 0)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2952 | if (NCH(subn) > 1) { |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 2953 | int j; |
| 2954 | if (strcmp(STR(CHILD(subn, 1)), "as") != 0) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2955 | com_error(c, PyExc_SyntaxError, |
| 2956 | "invalid syntax"); |
| 2957 | return; |
| 2958 | } |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 2959 | for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2) |
| 2960 | com_addopname(c, LOAD_ATTR, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2961 | CHILD(CHILD(subn, 0), |
| 2962 | j)); |
| 2963 | com_addop_varname(c, VAR_STORE, |
| 2964 | STR(CHILD(subn, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2965 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2966 | com_addop_varname(c, VAR_STORE, |
| 2967 | STR(CHILD(CHILD(subn, 0), |
| 2968 | 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2969 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2970 | } |
| 2971 | } |
| 2972 | } |
| 2973 | |
| 2974 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2975 | com_exec_stmt(struct compiling *c, node *n) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2976 | { |
| 2977 | REQ(n, exec_stmt); |
| 2978 | /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */ |
| 2979 | com_node(c, CHILD(n, 1)); |
| 2980 | if (NCH(n) >= 4) |
| 2981 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2982 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2983 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2984 | com_push(c, 1); |
| 2985 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2986 | if (NCH(n) >= 6) |
| 2987 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2988 | else { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2989 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2990 | com_push(c, 1); |
| 2991 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2992 | com_addbyte(c, EXEC_STMT); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2993 | com_pop(c, 3); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2994 | } |
| 2995 | |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2996 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2997 | is_constant_false(struct compiling *c, node *n) |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2998 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2999 | PyObject *v; |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3000 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3001 | /* argument c will be NULL when called from symtable_node() */ |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3002 | |
| 3003 | /* Label to avoid tail recursion */ |
| 3004 | next: |
| 3005 | switch (TYPE(n)) { |
| 3006 | |
| 3007 | case suite: |
| 3008 | if (NCH(n) == 1) { |
| 3009 | n = CHILD(n, 0); |
| 3010 | goto next; |
| 3011 | } |
| 3012 | /* Fall through */ |
| 3013 | case file_input: |
| 3014 | for (i = 0; i < NCH(n); i++) { |
| 3015 | node *ch = CHILD(n, i); |
| 3016 | if (TYPE(ch) == stmt) { |
| 3017 | n = ch; |
| 3018 | goto next; |
| 3019 | } |
| 3020 | } |
| 3021 | break; |
| 3022 | |
| 3023 | case stmt: |
| 3024 | case simple_stmt: |
| 3025 | case small_stmt: |
| 3026 | n = CHILD(n, 0); |
| 3027 | goto next; |
| 3028 | |
| 3029 | case expr_stmt: |
| 3030 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3031 | case testlist1: |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3032 | case test: |
| 3033 | case and_test: |
| 3034 | case not_test: |
| 3035 | case comparison: |
| 3036 | case expr: |
| 3037 | case xor_expr: |
| 3038 | case and_expr: |
| 3039 | case shift_expr: |
| 3040 | case arith_expr: |
| 3041 | case term: |
| 3042 | case factor: |
| 3043 | case power: |
| 3044 | case atom: |
| 3045 | if (NCH(n) == 1) { |
| 3046 | n = CHILD(n, 0); |
| 3047 | goto next; |
| 3048 | } |
| 3049 | break; |
| 3050 | |
| 3051 | case NAME: |
| 3052 | if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0) |
| 3053 | return 1; |
| 3054 | break; |
| 3055 | |
| 3056 | case NUMBER: |
| 3057 | v = parsenumber(c, STR(n)); |
| 3058 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3059 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3060 | break; |
| 3061 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3062 | i = PyObject_IsTrue(v); |
| 3063 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3064 | return i == 0; |
| 3065 | |
| 3066 | case STRING: |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3067 | v = parsestr(c, STR(n)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3068 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3069 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3070 | break; |
| 3071 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3072 | i = PyObject_IsTrue(v); |
| 3073 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3074 | return i == 0; |
| 3075 | |
| 3076 | } |
| 3077 | return 0; |
| 3078 | } |
| 3079 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3080 | |
| 3081 | /* Look under n for a return stmt with an expression. |
| 3082 | * This hack is used to find illegal returns under "if 0:" blocks in |
| 3083 | * functions already known to be generators (as determined by the symtable |
| 3084 | * pass). |
| 3085 | * Return the offending return node if found, else NULL. |
| 3086 | */ |
| 3087 | static node * |
| 3088 | look_for_offending_return(node *n) |
| 3089 | { |
| 3090 | int i; |
| 3091 | |
| 3092 | for (i = 0; i < NCH(n); ++i) { |
| 3093 | node *kid = CHILD(n, i); |
| 3094 | |
| 3095 | switch (TYPE(kid)) { |
| 3096 | case classdef: |
| 3097 | case funcdef: |
| 3098 | case lambdef: |
| 3099 | /* Stuff in nested functions & classes doesn't |
| 3100 | affect the code block we started in. */ |
| 3101 | return NULL; |
| 3102 | |
| 3103 | case return_stmt: |
| 3104 | if (NCH(kid) > 1) |
| 3105 | return kid; |
| 3106 | break; |
| 3107 | |
| 3108 | default: { |
| 3109 | node *bad = look_for_offending_return(kid); |
| 3110 | if (bad != NULL) |
| 3111 | return bad; |
| 3112 | } |
| 3113 | } |
| 3114 | } |
| 3115 | |
| 3116 | return NULL; |
| 3117 | } |
| 3118 | |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3119 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3120 | com_if_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3121 | { |
| 3122 | int i; |
| 3123 | int anchor = 0; |
| 3124 | REQ(n, if_stmt); |
| 3125 | /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */ |
| 3126 | for (i = 0; i+3 < NCH(n); i+=4) { |
| 3127 | int a = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3128 | node *ch = CHILD(n, i+1); |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3129 | if (is_constant_false(c, ch)) { |
| 3130 | /* We're going to skip this block. However, if this |
| 3131 | is a generator, we have to check the dead code |
| 3132 | anyway to make sure there aren't any return stmts |
| 3133 | with expressions, in the same scope. */ |
| 3134 | if (c->c_flags & CO_GENERATOR) { |
| 3135 | node *p = look_for_offending_return(n); |
| 3136 | if (p != NULL) { |
| 3137 | int savelineno = c->c_lineno; |
| 3138 | c->c_lineno = p->n_lineno; |
| 3139 | com_error(c, PyExc_SyntaxError, |
| 3140 | "'return' with argument " |
| 3141 | "inside generator"); |
| 3142 | c->c_lineno = savelineno; |
| 3143 | } |
| 3144 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3145 | continue; |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3146 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3147 | if (i > 0) |
| 3148 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3149 | com_node(c, ch); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3150 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 3151 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3152 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3153 | com_node(c, CHILD(n, i+3)); |
| 3154 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 3155 | com_backpatch(c, a); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3156 | /* We jump here with an extra entry which we now pop */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3157 | com_addbyte(c, POP_TOP); |
| 3158 | } |
| 3159 | if (i+2 < NCH(n)) |
| 3160 | com_node(c, CHILD(n, i+2)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3161 | if (anchor) |
| 3162 | com_backpatch(c, anchor); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3163 | } |
| 3164 | |
| 3165 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3166 | com_while_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3167 | { |
| 3168 | int break_anchor = 0; |
| 3169 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3170 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3171 | REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */ |
| 3172 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3173 | block_push(c, SETUP_LOOP); |
| 3174 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3175 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3176 | com_node(c, CHILD(n, 1)); |
| 3177 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 3178 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3179 | com_pop(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3180 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3181 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3182 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3183 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3184 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3185 | com_backpatch(c, anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3186 | /* We jump here with one entry more on the stack */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3187 | com_addbyte(c, POP_TOP); |
| 3188 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3189 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3190 | if (NCH(n) > 4) |
| 3191 | com_node(c, CHILD(n, 6)); |
| 3192 | com_backpatch(c, break_anchor); |
| 3193 | } |
| 3194 | |
| 3195 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3196 | com_for_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3197 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3198 | int break_anchor = 0; |
| 3199 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3200 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3201 | REQ(n, for_stmt); |
| 3202 | /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */ |
| 3203 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3204 | block_push(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3205 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3206 | com_addbyte(c, GET_ITER); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3207 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3208 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3209 | com_addfwref(c, FOR_ITER, &anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3210 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3211 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3212 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3213 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3214 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3215 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3216 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3217 | com_backpatch(c, anchor); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3218 | com_pop(c, 1); /* FOR_ITER has popped this */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3219 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3220 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3221 | if (NCH(n) > 8) |
| 3222 | com_node(c, CHILD(n, 8)); |
| 3223 | com_backpatch(c, break_anchor); |
| 3224 | } |
| 3225 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3226 | /* Code generated for "try: S finally: Sf" is as follows: |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3227 | |
| 3228 | SETUP_FINALLY L |
| 3229 | <code for S> |
| 3230 | POP_BLOCK |
| 3231 | LOAD_CONST <nil> |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3232 | L: <code for Sf> |
| 3233 | END_FINALLY |
| 3234 | |
| 3235 | The special instructions use the block stack. Each block |
| 3236 | stack entry contains the instruction that created it (here |
| 3237 | SETUP_FINALLY), the level of the value stack at the time the |
| 3238 | block stack entry was created, and a label (here L). |
| 3239 | |
| 3240 | SETUP_FINALLY: |
| 3241 | Pushes the current value stack level and the label |
| 3242 | onto the block stack. |
| 3243 | POP_BLOCK: |
| 3244 | Pops en entry from the block stack, and pops the value |
| 3245 | stack until its level is the same as indicated on the |
| 3246 | block stack. (The label is ignored.) |
| 3247 | END_FINALLY: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3248 | Pops a variable number of entries from the *value* stack |
| 3249 | and re-raises the exception they specify. The number of |
| 3250 | entries popped depends on the (pseudo) exception type. |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3251 | |
| 3252 | The block stack is unwound when an exception is raised: |
| 3253 | when a SETUP_FINALLY entry is found, the exception is pushed |
| 3254 | onto the value stack (and the exception condition is cleared), |
| 3255 | and the interpreter jumps to the label gotten from the block |
| 3256 | stack. |
| 3257 | |
| 3258 | 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] | 3259 | (The contents of the value stack is shown in [], with the top |
| 3260 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3261 | associated value, and 'exc' the exception.) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3262 | |
| 3263 | Value stack Label Instruction Argument |
| 3264 | [] SETUP_EXCEPT L1 |
| 3265 | [] <code for S> |
| 3266 | [] POP_BLOCK |
| 3267 | [] JUMP_FORWARD L0 |
| 3268 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3269 | [tb, val, exc] L1: DUP ) |
| 3270 | [tb, val, exc, exc] <evaluate E1> ) |
| 3271 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 3272 | [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 ) |
| 3273 | [tb, val, exc, 1] POP ) |
| 3274 | [tb, val, exc] POP |
| 3275 | [tb, val] <assign to V1> (or POP if no V1) |
| 3276 | [tb] POP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3277 | [] <code for S1> |
| 3278 | JUMP_FORWARD L0 |
| 3279 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3280 | [tb, val, exc, 0] L2: POP |
| 3281 | [tb, val, exc] DUP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3282 | .............................etc....................... |
| 3283 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3284 | [tb, val, exc, 0] Ln+1: POP |
| 3285 | [tb, val, exc] END_FINALLY # re-raise exception |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3286 | |
| 3287 | [] L0: <next statement> |
| 3288 | |
| 3289 | Of course, parts are not generated if Vi or Ei is not present. |
| 3290 | */ |
| 3291 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3292 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3293 | com_try_except(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3294 | { |
| 3295 | int except_anchor = 0; |
| 3296 | int end_anchor = 0; |
| 3297 | int else_anchor = 0; |
| 3298 | int i; |
| 3299 | node *ch; |
| 3300 | |
| 3301 | com_addfwref(c, SETUP_EXCEPT, &except_anchor); |
| 3302 | block_push(c, SETUP_EXCEPT); |
| 3303 | com_node(c, CHILD(n, 2)); |
| 3304 | com_addbyte(c, POP_BLOCK); |
| 3305 | block_pop(c, SETUP_EXCEPT); |
| 3306 | com_addfwref(c, JUMP_FORWARD, &else_anchor); |
| 3307 | com_backpatch(c, except_anchor); |
| 3308 | for (i = 3; |
| 3309 | i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause; |
| 3310 | i += 3) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3311 | /* except_clause: 'except' [expr [',' var]] */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3312 | if (except_anchor == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3313 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 3314 | "default 'except:' must be last"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3315 | break; |
| 3316 | } |
| 3317 | except_anchor = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3318 | com_push(c, 3); /* tb, val, exc pushed by exception */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3319 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 3320 | if (NCH(ch) > 1) { |
| 3321 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3322 | com_push(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3323 | com_node(c, CHILD(ch, 1)); |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 3324 | com_addoparg(c, COMPARE_OP, PyCmp_EXC_MATCH); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3325 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3326 | com_addfwref(c, JUMP_IF_FALSE, &except_anchor); |
| 3327 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3328 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3329 | } |
| 3330 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3331 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3332 | if (NCH(ch) > 3) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3333 | com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3334 | else { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3335 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3336 | com_pop(c, 1); |
| 3337 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3338 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3339 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3340 | com_node(c, CHILD(n, i+2)); |
| 3341 | com_addfwref(c, JUMP_FORWARD, &end_anchor); |
| 3342 | if (except_anchor) { |
| 3343 | com_backpatch(c, except_anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3344 | /* We come in with [tb, val, exc, 0] on the |
| 3345 | stack; one pop and it's the same as |
| 3346 | expected at the start of the loop */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3347 | com_addbyte(c, POP_TOP); |
| 3348 | } |
| 3349 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3350 | /* We actually come in here with [tb, val, exc] but the |
| 3351 | END_FINALLY will zap those and jump around. |
| 3352 | The c_stacklevel does not reflect them so we need not pop |
| 3353 | anything. */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3354 | com_addbyte(c, END_FINALLY); |
| 3355 | com_backpatch(c, else_anchor); |
| 3356 | if (i < NCH(n)) |
| 3357 | com_node(c, CHILD(n, i+2)); |
| 3358 | com_backpatch(c, end_anchor); |
| 3359 | } |
| 3360 | |
| 3361 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3362 | com_try_finally(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3363 | { |
| 3364 | int finally_anchor = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3365 | node *ch; |
Guido van Rossum | 94fb82e | 1992-04-05 14:24:50 +0000 | [diff] [blame] | 3366 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3367 | com_addfwref(c, SETUP_FINALLY, &finally_anchor); |
| 3368 | block_push(c, SETUP_FINALLY); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3369 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3370 | com_addbyte(c, POP_BLOCK); |
| 3371 | block_pop(c, SETUP_FINALLY); |
| 3372 | block_push(c, END_FINALLY); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3373 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3374 | /* While the generated code pushes only one item, |
| 3375 | the try-finally handling can enter here with |
| 3376 | up to three items. OK, here are the details: |
| 3377 | 3 for an exception, 2 for RETURN, 1 for BREAK. */ |
| 3378 | com_push(c, 3); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3379 | com_backpatch(c, finally_anchor); |
| 3380 | ch = CHILD(n, NCH(n)-1); |
| 3381 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 3382 | com_node(c, ch); |
| 3383 | com_addbyte(c, END_FINALLY); |
| 3384 | block_pop(c, END_FINALLY); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3385 | com_pop(c, 3); /* Matches the com_push above */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3386 | } |
| 3387 | |
| 3388 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3389 | com_try_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3390 | { |
| 3391 | REQ(n, try_stmt); |
| 3392 | /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
| 3393 | | 'try' ':' suite 'finally' ':' suite */ |
| 3394 | if (TYPE(CHILD(n, 3)) != except_clause) |
| 3395 | com_try_finally(c, n); |
| 3396 | else |
| 3397 | com_try_except(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3398 | } |
| 3399 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3400 | static node * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3401 | get_rawdocstring(node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3402 | { |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3403 | int i; |
| 3404 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3405 | /* Label to avoid tail recursion */ |
| 3406 | next: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3407 | switch (TYPE(n)) { |
| 3408 | |
| 3409 | case suite: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3410 | if (NCH(n) == 1) { |
| 3411 | n = CHILD(n, 0); |
| 3412 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3413 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3414 | /* Fall through */ |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3415 | case file_input: |
| 3416 | for (i = 0; i < NCH(n); i++) { |
| 3417 | node *ch = CHILD(n, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3418 | if (TYPE(ch) == stmt) { |
| 3419 | n = ch; |
| 3420 | goto next; |
| 3421 | } |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3422 | } |
| 3423 | break; |
| 3424 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3425 | case stmt: |
| 3426 | case simple_stmt: |
| 3427 | case small_stmt: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3428 | n = CHILD(n, 0); |
| 3429 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3430 | |
| 3431 | case expr_stmt: |
| 3432 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3433 | case testlist1: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3434 | case test: |
| 3435 | case and_test: |
| 3436 | case not_test: |
| 3437 | case comparison: |
| 3438 | case expr: |
| 3439 | case xor_expr: |
| 3440 | case and_expr: |
| 3441 | case shift_expr: |
| 3442 | case arith_expr: |
| 3443 | case term: |
| 3444 | case factor: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3445 | case power: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3446 | if (NCH(n) == 1) { |
| 3447 | n = CHILD(n, 0); |
| 3448 | goto next; |
| 3449 | } |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3450 | break; |
| 3451 | |
| 3452 | case atom: |
| 3453 | if (TYPE(CHILD(n, 0)) == STRING) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3454 | return n; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3455 | break; |
| 3456 | |
| 3457 | } |
| 3458 | return NULL; |
| 3459 | } |
| 3460 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3461 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3462 | get_docstring(struct compiling *c, node *n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3463 | { |
Guido van Rossum | 541563e | 1999-01-28 15:08:09 +0000 | [diff] [blame] | 3464 | /* Don't generate doc-strings if run with -OO */ |
| 3465 | if (Py_OptimizeFlag > 1) |
| 3466 | return NULL; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3467 | n = get_rawdocstring(n); |
| 3468 | if (n == NULL) |
| 3469 | return NULL; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3470 | return parsestrplus(c, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3471 | } |
| 3472 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3473 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3474 | com_suite(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3475 | { |
| 3476 | REQ(n, suite); |
| 3477 | /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */ |
| 3478 | if (NCH(n) == 1) { |
| 3479 | com_node(c, CHILD(n, 0)); |
| 3480 | } |
| 3481 | else { |
| 3482 | int i; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3483 | for (i = 0; i < NCH(n) && c->c_errors == 0; i++) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3484 | node *ch = CHILD(n, i); |
| 3485 | if (TYPE(ch) == stmt) |
| 3486 | com_node(c, ch); |
| 3487 | } |
| 3488 | } |
| 3489 | } |
| 3490 | |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3491 | /* ARGSUSED */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3492 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3493 | com_continue_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3494 | { |
| 3495 | int i = c->c_nblocks; |
| 3496 | if (i-- > 0 && c->c_block[i] == SETUP_LOOP) { |
| 3497 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3498 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3499 | else if (i <= 0) { |
| 3500 | /* at the outer level */ |
| 3501 | com_error(c, PyExc_SyntaxError, |
| 3502 | "'continue' not properly in loop"); |
| 3503 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3504 | else { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3505 | int j; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3506 | for (j = i-1; j >= 0; --j) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3507 | if (c->c_block[j] == SETUP_LOOP) |
| 3508 | break; |
| 3509 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3510 | if (j >= 0) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3511 | /* there is a loop, but something interferes */ |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3512 | for (; i > j; --i) { |
| 3513 | if (c->c_block[i] == SETUP_EXCEPT || |
| 3514 | c->c_block[i] == SETUP_FINALLY) { |
| 3515 | com_addoparg(c, CONTINUE_LOOP, |
| 3516 | c->c_begin); |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3517 | return; |
| 3518 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3519 | if (c->c_block[i] == END_FINALLY) { |
| 3520 | com_error(c, PyExc_SyntaxError, |
| 3521 | "'continue' not supported inside 'finally' clause"); |
| 3522 | return; |
| 3523 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3524 | } |
| 3525 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3526 | com_error(c, PyExc_SyntaxError, |
| 3527 | "'continue' not properly in loop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3528 | } |
| 3529 | /* XXX Could allow it inside a 'finally' clause |
| 3530 | XXX if we could pop the exception still on the stack */ |
| 3531 | } |
| 3532 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3533 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3534 | com_argdefs(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3535 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3536 | int i, nch, nargs, ndefs; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3537 | if (TYPE(n) == lambdef) { |
| 3538 | /* lambdef: 'lambda' [varargslist] ':' test */ |
| 3539 | n = CHILD(n, 1); |
| 3540 | } |
| 3541 | else { |
| 3542 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */ |
| 3543 | n = CHILD(n, 2); |
| 3544 | REQ(n, parameters); /* parameters: '(' [varargslist] ')' */ |
| 3545 | n = CHILD(n, 1); |
| 3546 | } |
| 3547 | if (TYPE(n) != varargslist) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3548 | return 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3549 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3550 | (fpdef ['=' test] ',')* '*' ....... | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3551 | fpdef ['=' test] (',' fpdef ['=' test])* [','] */ |
| 3552 | nch = NCH(n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3553 | nargs = 0; |
| 3554 | ndefs = 0; |
| 3555 | for (i = 0; i < nch; i++) { |
| 3556 | int t; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3557 | if (TYPE(CHILD(n, i)) == STAR || |
| 3558 | TYPE(CHILD(n, i)) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3559 | break; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3560 | nargs++; |
| 3561 | i++; |
| 3562 | if (i >= nch) |
Guido van Rossum | a1e7e62 | 1995-09-18 21:44:04 +0000 | [diff] [blame] | 3563 | t = RPAR; /* Anything except EQUAL or COMMA */ |
| 3564 | else |
| 3565 | t = TYPE(CHILD(n, i)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3566 | if (t == EQUAL) { |
| 3567 | i++; |
| 3568 | ndefs++; |
| 3569 | com_node(c, CHILD(n, i)); |
| 3570 | i++; |
| 3571 | if (i >= nch) |
| 3572 | break; |
| 3573 | t = TYPE(CHILD(n, i)); |
| 3574 | } |
| 3575 | else { |
Guido van Rossum | 29d38cd | 1998-10-02 13:41:54 +0000 | [diff] [blame] | 3576 | /* Treat "(a=1, b)" as an error */ |
| 3577 | if (ndefs) |
| 3578 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | dfede31 | 1998-10-02 14:06:56 +0000 | [diff] [blame] | 3579 | "non-default argument follows default argument"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3580 | } |
| 3581 | if (t != COMMA) |
| 3582 | break; |
| 3583 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3584 | return ndefs; |
| 3585 | } |
| 3586 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3587 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3588 | com_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3589 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3590 | PyObject *co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3591 | int ndefs; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3592 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3593 | ndefs = com_argdefs(c, n); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3594 | symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n), |
| 3595 | n->n_lineno); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3596 | co = (PyObject *)icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3597 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3598 | if (co == NULL) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3599 | c->c_errors++; |
| 3600 | else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3601 | int closure = com_make_closure(c, (PyCodeObject *)co); |
| 3602 | int i = com_addconst(c, co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3603 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3604 | com_push(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3605 | if (closure) |
| 3606 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
| 3607 | else |
| 3608 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3609 | com_pop(c, ndefs); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3610 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3611 | com_pop(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3612 | Py_DECREF(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3613 | } |
| 3614 | } |
| 3615 | |
| 3616 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3617 | com_bases(struct compiling *c, node *n) |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3618 | { |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3619 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3620 | REQ(n, testlist); |
| 3621 | /* testlist: test (',' test)* [','] */ |
| 3622 | for (i = 0; i < NCH(n); i += 2) |
| 3623 | com_node(c, CHILD(n, i)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3624 | i = (NCH(n)+1) / 2; |
| 3625 | com_addoparg(c, BUILD_TUPLE, i); |
| 3626 | com_pop(c, i-1); |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3627 | } |
| 3628 | |
| 3629 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3630 | com_classdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3631 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3632 | int i; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3633 | PyObject *v; |
| 3634 | PyCodeObject *co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3635 | char *name; |
| 3636 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3637 | REQ(n, classdef); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3638 | /* classdef: class NAME ['(' testlist ')'] ':' suite */ |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 3639 | if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3640 | c->c_errors++; |
| 3641 | return; |
| 3642 | } |
| 3643 | /* Push the class name on the stack */ |
| 3644 | i = com_addconst(c, v); |
| 3645 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3646 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3647 | Py_DECREF(v); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3648 | /* Push the tuple of base classes on the stack */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3649 | if (TYPE(CHILD(n, 2)) != LPAR) { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3650 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3651 | com_push(c, 1); |
| 3652 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3653 | else |
| 3654 | com_bases(c, CHILD(n, 3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3655 | name = STR(CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3656 | symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3657 | co = icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3658 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3659 | if (co == NULL) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3660 | c->c_errors++; |
| 3661 | else { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3662 | int closure = com_make_closure(c, co); |
| 3663 | i = com_addconst(c, (PyObject *)co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3664 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3665 | com_push(c, 1); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3666 | if (closure) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3667 | com_addoparg(c, MAKE_CLOSURE, 0); |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 3668 | com_pop(c, PyCode_GetNumFree(co)); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3669 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3670 | com_addoparg(c, MAKE_FUNCTION, 0); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3671 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3672 | com_addbyte(c, BUILD_CLASS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3673 | com_pop(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3674 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3675 | com_pop(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3676 | Py_DECREF(co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3677 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3678 | } |
| 3679 | |
| 3680 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3681 | com_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3682 | { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3683 | loop: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3684 | if (c->c_errors) |
| 3685 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3686 | switch (TYPE(n)) { |
| 3687 | |
| 3688 | /* Definition nodes */ |
| 3689 | |
| 3690 | case funcdef: |
| 3691 | com_funcdef(c, n); |
| 3692 | break; |
| 3693 | case classdef: |
| 3694 | com_classdef(c, n); |
| 3695 | break; |
| 3696 | |
| 3697 | /* Trivial parse tree nodes */ |
| 3698 | |
| 3699 | case stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3700 | case small_stmt: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3701 | case flow_stmt: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3702 | n = CHILD(n, 0); |
| 3703 | goto loop; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3704 | |
| 3705 | case simple_stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3706 | /* small_stmt (';' small_stmt)* [';'] NEWLINE */ |
| 3707 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 3708 | { |
| 3709 | int i; |
| 3710 | for (i = 0; i < NCH(n)-1; i += 2) |
| 3711 | com_node(c, CHILD(n, i)); |
| 3712 | } |
| 3713 | break; |
| 3714 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3715 | case compound_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3716 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3717 | n = CHILD(n, 0); |
| 3718 | goto loop; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3719 | |
| 3720 | /* Statement nodes */ |
| 3721 | |
| 3722 | case expr_stmt: |
| 3723 | com_expr_stmt(c, n); |
| 3724 | break; |
| 3725 | case print_stmt: |
| 3726 | com_print_stmt(c, n); |
| 3727 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3728 | case del_stmt: /* 'del' exprlist */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3729 | com_assign(c, CHILD(n, 1), OP_DELETE, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3730 | break; |
| 3731 | case pass_stmt: |
| 3732 | break; |
| 3733 | case break_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3734 | if (c->c_loops == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3735 | com_error(c, PyExc_SyntaxError, |
| 3736 | "'break' outside loop"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3737 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3738 | com_addbyte(c, BREAK_LOOP); |
| 3739 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3740 | case continue_stmt: |
| 3741 | com_continue_stmt(c, n); |
| 3742 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3743 | case return_stmt: |
| 3744 | com_return_stmt(c, n); |
| 3745 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3746 | case yield_stmt: |
| 3747 | com_yield_stmt(c, n); |
| 3748 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3749 | case raise_stmt: |
| 3750 | com_raise_stmt(c, n); |
| 3751 | break; |
| 3752 | case import_stmt: |
| 3753 | com_import_stmt(c, n); |
| 3754 | break; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3755 | case global_stmt: |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3756 | break; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3757 | case exec_stmt: |
| 3758 | com_exec_stmt(c, n); |
| 3759 | break; |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 3760 | case assert_stmt: |
| 3761 | com_assert_stmt(c, n); |
| 3762 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3763 | case if_stmt: |
| 3764 | com_if_stmt(c, n); |
| 3765 | break; |
| 3766 | case while_stmt: |
| 3767 | com_while_stmt(c, n); |
| 3768 | break; |
| 3769 | case for_stmt: |
| 3770 | com_for_stmt(c, n); |
| 3771 | break; |
| 3772 | case try_stmt: |
| 3773 | com_try_stmt(c, n); |
| 3774 | break; |
| 3775 | case suite: |
| 3776 | com_suite(c, n); |
| 3777 | break; |
| 3778 | |
| 3779 | /* Expression nodes */ |
| 3780 | |
| 3781 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3782 | case testlist1: |
Guido van Rossum | 1c91707 | 2001-10-15 15:44:05 +0000 | [diff] [blame] | 3783 | case testlist_safe: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3784 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3785 | break; |
| 3786 | case test: |
| 3787 | com_test(c, n); |
| 3788 | break; |
| 3789 | case and_test: |
| 3790 | com_and_test(c, n); |
| 3791 | break; |
| 3792 | case not_test: |
| 3793 | com_not_test(c, n); |
| 3794 | break; |
| 3795 | case comparison: |
| 3796 | com_comparison(c, n); |
| 3797 | break; |
| 3798 | case exprlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3799 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3800 | break; |
| 3801 | case expr: |
| 3802 | com_expr(c, n); |
| 3803 | break; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 3804 | case xor_expr: |
| 3805 | com_xor_expr(c, n); |
| 3806 | break; |
| 3807 | case and_expr: |
| 3808 | com_and_expr(c, n); |
| 3809 | break; |
| 3810 | case shift_expr: |
| 3811 | com_shift_expr(c, n); |
| 3812 | break; |
| 3813 | case arith_expr: |
| 3814 | com_arith_expr(c, n); |
| 3815 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3816 | case term: |
| 3817 | com_term(c, n); |
| 3818 | break; |
| 3819 | case factor: |
| 3820 | com_factor(c, n); |
| 3821 | break; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3822 | case power: |
| 3823 | com_power(c, n); |
| 3824 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3825 | case atom: |
| 3826 | com_atom(c, n); |
| 3827 | break; |
| 3828 | |
| 3829 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3830 | com_error(c, PyExc_SystemError, |
| 3831 | "com_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3832 | } |
| 3833 | } |
| 3834 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 3835 | static void com_fplist(struct compiling *, node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3836 | |
| 3837 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3838 | com_fpdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3839 | { |
| 3840 | REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3841 | if (TYPE(CHILD(n, 0)) == LPAR) |
| 3842 | com_fplist(c, CHILD(n, 1)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3843 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3844 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3845 | com_pop(c, 1); |
| 3846 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3847 | } |
| 3848 | |
| 3849 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3850 | com_fplist(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3851 | { |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 3852 | REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3853 | if (NCH(n) == 1) { |
| 3854 | com_fpdef(c, CHILD(n, 0)); |
| 3855 | } |
| 3856 | else { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3857 | int i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 3858 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3859 | com_push(c, i-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3860 | for (i = 0; i < NCH(n); i += 2) |
| 3861 | com_fpdef(c, CHILD(n, i)); |
| 3862 | } |
| 3863 | } |
| 3864 | |
| 3865 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3866 | com_arglist(struct compiling *c, node *n) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3867 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3868 | int nch, i, narg; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3869 | int complex = 0; |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 3870 | char nbuf[30]; |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3871 | REQ(n, varargslist); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 3872 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3873 | (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3874 | nch = NCH(n); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3875 | /* Enter all arguments in table of locals */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3876 | for (i = 0, narg = 0; i < nch; i++) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3877 | node *ch = CHILD(n, i); |
| 3878 | node *fp; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3879 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3880 | break; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3881 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3882 | fp = CHILD(ch, 0); |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 3883 | if (TYPE(fp) != NAME) { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 3884 | PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i); |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 3885 | complex = 1; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3886 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3887 | narg++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3888 | /* all name updates handled by symtable */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3889 | if (++i >= nch) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3890 | break; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3891 | ch = CHILD(n, i); |
| 3892 | if (TYPE(ch) == EQUAL) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3893 | i += 2; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3894 | else |
| 3895 | REQ(ch, COMMA); |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3896 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3897 | if (complex) { |
| 3898 | /* Generate code for complex arguments only after |
| 3899 | having counted the simple arguments */ |
| 3900 | int ilocal = 0; |
| 3901 | for (i = 0; i < nch; i++) { |
| 3902 | node *ch = CHILD(n, i); |
| 3903 | node *fp; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3904 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3905 | break; |
| 3906 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3907 | fp = CHILD(ch, 0); |
| 3908 | if (TYPE(fp) != NAME) { |
| 3909 | com_addoparg(c, LOAD_FAST, ilocal); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3910 | com_push(c, 1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3911 | com_fpdef(c, ch); |
| 3912 | } |
| 3913 | ilocal++; |
| 3914 | if (++i >= nch) |
| 3915 | break; |
| 3916 | ch = CHILD(n, i); |
| 3917 | if (TYPE(ch) == EQUAL) |
| 3918 | i += 2; |
| 3919 | else |
| 3920 | REQ(ch, COMMA); |
| 3921 | } |
| 3922 | } |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3923 | } |
| 3924 | |
| 3925 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3926 | com_file_input(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3927 | { |
| 3928 | int i; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3929 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3930 | REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3931 | doc = get_docstring(c, n); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3932 | if (doc != NULL) { |
| 3933 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3934 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3935 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3936 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3937 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3938 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3939 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3940 | for (i = 0; i < NCH(n); i++) { |
| 3941 | node *ch = CHILD(n, i); |
| 3942 | if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE) |
| 3943 | com_node(c, ch); |
| 3944 | } |
| 3945 | } |
| 3946 | |
| 3947 | /* Top-level compile-node interface */ |
| 3948 | |
| 3949 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3950 | compile_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3951 | { |
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 | node *ch; |
| 3954 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3955 | c->c_name = STR(CHILD(n, 1)); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3956 | doc = get_docstring(c, CHILD(n, 4)); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3957 | if (doc != NULL) { |
| 3958 | (void) com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3959 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3960 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3961 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3962 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3963 | ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */ |
| 3964 | ch = CHILD(ch, 1); /* ')' | varargslist */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3965 | if (TYPE(ch) == varargslist) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3966 | com_arglist(c, ch); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3967 | c->c_infunction = 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3968 | com_node(c, CHILD(n, 4)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3969 | c->c_infunction = 0; |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 3970 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
| 3971 | com_push(c, 1); |
| 3972 | com_addbyte(c, RETURN_VALUE); |
| 3973 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3974 | } |
| 3975 | |
| 3976 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3977 | compile_lambdef(struct compiling *c, node *n) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 3978 | { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3979 | node *ch; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3980 | REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */ |
Guido van Rossum | 363ac7d | 1994-11-10 22:40:34 +0000 | [diff] [blame] | 3981 | c->c_name = "<lambda>"; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3982 | |
| 3983 | ch = CHILD(n, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3984 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3985 | if (TYPE(ch) == varargslist) { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3986 | com_arglist(c, ch); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3987 | ch = CHILD(n, 3); |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3988 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3989 | else |
| 3990 | ch = CHILD(n, 2); |
| 3991 | com_node(c, ch); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 3992 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3993 | com_pop(c, 1); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 3994 | } |
| 3995 | |
| 3996 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3997 | compile_classdef(struct compiling *c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3998 | { |
| 3999 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4000 | PyObject *doc; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4001 | REQ(n, classdef); |
| 4002 | /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ |
| 4003 | c->c_name = STR(CHILD(n, 1)); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4004 | c->c_private = c->c_name; |
Guido van Rossum | 340cbe7 | 2002-01-15 21:06:07 +0000 | [diff] [blame] | 4005 | /* Initialize local __module__ from global __name__ */ |
| 4006 | com_addop_name(c, LOAD_GLOBAL, "__name__"); |
| 4007 | com_addop_name(c, STORE_NAME, "__module__"); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4008 | ch = CHILD(n, NCH(n)-1); /* The suite */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4009 | doc = get_docstring(c, ch); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4010 | if (doc != NULL) { |
| 4011 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4012 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4013 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4014 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4015 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4016 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4017 | } |
| 4018 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4019 | (void) com_addconst(c, Py_None); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4020 | com_node(c, ch); |
| 4021 | com_addbyte(c, LOAD_LOCALS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4022 | com_push(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4023 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4024 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4025 | } |
| 4026 | |
| 4027 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4028 | compile_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4029 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4030 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 4031 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4032 | switch (TYPE(n)) { |
| 4033 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4034 | case single_input: /* One interactive command */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4035 | /* NEWLINE | simple_stmt | compound_stmt NEWLINE */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 4036 | c->c_interactive++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4037 | n = CHILD(n, 0); |
| 4038 | if (TYPE(n) != NEWLINE) |
| 4039 | com_node(c, n); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4040 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4041 | com_push(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4042 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4043 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 4044 | c->c_interactive--; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4045 | break; |
| 4046 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4047 | case file_input: /* A whole file, or built-in function exec() */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4048 | com_file_input(c, n); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4049 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4050 | com_push(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4051 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4052 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4053 | break; |
| 4054 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4055 | case eval_input: /* Built-in function input() */ |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4056 | com_node(c, CHILD(n, 0)); |
| 4057 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4058 | com_pop(c, 1); |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4059 | break; |
| 4060 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4061 | case lambdef: /* anonymous function definition */ |
| 4062 | compile_lambdef(c, n); |
| 4063 | break; |
| 4064 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4065 | case funcdef: /* A function definition */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4066 | compile_funcdef(c, n); |
| 4067 | break; |
| 4068 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4069 | case classdef: /* A class definition */ |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4070 | compile_classdef(c, n); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 4071 | break; |
| 4072 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4073 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4074 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 4075 | "compile_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4076 | } |
| 4077 | } |
| 4078 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4079 | static PyObject * |
| 4080 | dict_keys_inorder(PyObject *dict, int offset) |
| 4081 | { |
| 4082 | PyObject *tuple, *k, *v; |
| 4083 | int i, pos = 0, size = PyDict_Size(dict); |
| 4084 | |
| 4085 | tuple = PyTuple_New(size); |
| 4086 | if (tuple == NULL) |
| 4087 | return NULL; |
| 4088 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 4089 | i = PyInt_AS_LONG(v); |
| 4090 | Py_INCREF(k); |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4091 | assert((i - offset) < size); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4092 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 4093 | } |
| 4094 | return tuple; |
| 4095 | } |
| 4096 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4097 | PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4098 | PyNode_Compile(node *n, char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4099 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4100 | return PyNode_CompileFlags(n, filename, NULL); |
| 4101 | } |
| 4102 | |
| 4103 | PyCodeObject * |
| 4104 | PyNode_CompileFlags(node *n, char *filename, PyCompilerFlags *flags) |
| 4105 | { |
| 4106 | return jcompile(n, filename, NULL, flags); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4109 | struct symtable * |
| 4110 | PyNode_CompileSymtable(node *n, char *filename) |
| 4111 | { |
| 4112 | struct symtable *st; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4113 | PyFutureFeatures *ff; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4114 | |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4115 | ff = PyNode_Future(n, filename); |
| 4116 | if (ff == NULL) |
| 4117 | return NULL; |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4118 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4119 | st = symtable_init(); |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4120 | if (st == NULL) { |
| 4121 | PyMem_Free((void *)ff); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4122 | return NULL; |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4123 | } |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4124 | st->st_future = ff; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4125 | symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4126 | if (st->st_errors > 0) |
| 4127 | goto fail; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4128 | symtable_node(st, n); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4129 | if (st->st_errors > 0) |
| 4130 | goto fail; |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4131 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4132 | return st; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4133 | fail: |
| 4134 | PyMem_Free((void *)ff); |
| 4135 | st->st_future = NULL; |
| 4136 | PySymtable_Free(st); |
| 4137 | return NULL; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4138 | } |
| 4139 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4140 | static PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4141 | icompile(node *n, struct compiling *base) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4142 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4143 | return jcompile(n, base->c_filename, base, NULL); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4144 | } |
| 4145 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4146 | static PyCodeObject * |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4147 | jcompile(node *n, char *filename, struct compiling *base, |
| 4148 | PyCompilerFlags *flags) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4149 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4150 | struct compiling sc; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4151 | PyCodeObject *co; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4152 | if (!com_init(&sc, filename)) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4153 | return NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4154 | if (base) { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4155 | sc.c_private = base->c_private; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4156 | sc.c_symtable = base->c_symtable; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4157 | /* c_symtable still points to parent's symbols */ |
| 4158 | if (base->c_nested |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4159 | || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4160 | sc.c_nested = 1; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 4161 | sc.c_flags |= base->c_flags & PyCF_MASK; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4162 | } else { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4163 | sc.c_private = NULL; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 4164 | sc.c_future = PyNode_Future(n, filename); |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4165 | if (sc.c_future == NULL) { |
| 4166 | com_free(&sc); |
| 4167 | return NULL; |
| 4168 | } |
Jeremy Hylton | 481081e | 2001-08-14 20:01:59 +0000 | [diff] [blame] | 4169 | if (flags) { |
| 4170 | int merged = sc.c_future->ff_features | |
| 4171 | flags->cf_flags; |
| 4172 | sc.c_future->ff_features = merged; |
| 4173 | flags->cf_flags = merged; |
| 4174 | } |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4175 | if (symtable_build(&sc, n) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4176 | com_free(&sc); |
| 4177 | return NULL; |
| 4178 | } |
| 4179 | } |
| 4180 | co = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4181 | if (symtable_load_symbols(&sc) < 0) { |
| 4182 | sc.c_errors++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4183 | goto exit; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4184 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4185 | compile_node(&sc, n); |
| 4186 | com_done(&sc); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 4187 | if (sc.c_errors == 0) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4188 | PyObject *consts, *names, *varnames, *filename, *name, |
| 4189 | *freevars, *cellvars; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4190 | consts = PyList_AsTuple(sc.c_consts); |
| 4191 | names = PyList_AsTuple(sc.c_names); |
| 4192 | varnames = PyList_AsTuple(sc.c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4193 | cellvars = dict_keys_inorder(sc.c_cellvars, 0); |
| 4194 | freevars = dict_keys_inorder(sc.c_freevars, |
| 4195 | PyTuple_GET_SIZE(cellvars)); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 4196 | filename = PyString_InternFromString(sc.c_filename); |
| 4197 | name = PyString_InternFromString(sc.c_name); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4198 | if (!PyErr_Occurred()) |
| 4199 | co = PyCode_New(sc.c_argcount, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4200 | sc.c_nlocals, |
| 4201 | sc.c_maxstacklevel, |
| 4202 | sc.c_flags, |
| 4203 | sc.c_code, |
| 4204 | consts, |
| 4205 | names, |
| 4206 | varnames, |
| 4207 | freevars, |
| 4208 | cellvars, |
| 4209 | filename, |
| 4210 | name, |
| 4211 | sc.c_firstlineno, |
| 4212 | sc.c_lnotab); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4213 | Py_XDECREF(consts); |
| 4214 | Py_XDECREF(names); |
| 4215 | Py_XDECREF(varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4216 | Py_XDECREF(freevars); |
| 4217 | Py_XDECREF(cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4218 | Py_XDECREF(filename); |
| 4219 | Py_XDECREF(name); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 4220 | } |
Guido van Rossum | 6c2f0c7 | 2000-08-07 19:22:43 +0000 | [diff] [blame] | 4221 | else if (!PyErr_Occurred()) { |
| 4222 | /* This could happen if someone called PyErr_Clear() after an |
| 4223 | error was reported above. That's not supposed to happen, |
| 4224 | but I just plugged one case and I'm not sure there can't be |
| 4225 | others. In that case, raise SystemError so that at least |
| 4226 | it gets reported instead dumping core. */ |
| 4227 | PyErr_SetString(PyExc_SystemError, "lost syntax error"); |
| 4228 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4229 | exit: |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4230 | if (base == NULL) { |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4231 | PySymtable_Free(sc.c_symtable); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4232 | sc.c_symtable = NULL; |
| 4233 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4234 | com_free(&sc); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4235 | return co; |
| 4236 | } |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4237 | |
| 4238 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4239 | PyCode_Addr2Line(PyCodeObject *co, int addrq) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4240 | { |
| 4241 | int size = PyString_Size(co->co_lnotab) / 2; |
Guido van Rossum | 2174dcb | 1999-09-15 22:48:09 +0000 | [diff] [blame] | 4242 | unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4243 | int line = co->co_firstlineno; |
| 4244 | int addr = 0; |
| 4245 | while (--size >= 0) { |
| 4246 | addr += *p++; |
| 4247 | if (addr > addrq) |
| 4248 | break; |
| 4249 | line += *p++; |
| 4250 | } |
| 4251 | return line; |
| 4252 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4253 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4254 | /* The test for LOCAL must come before the test for FREE in order to |
| 4255 | handle classes where name is both local and free. The local var is |
| 4256 | a method and the free var is a free var referenced within a method. |
| 4257 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4258 | |
| 4259 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4260 | get_ref_type(struct compiling *c, char *name) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4261 | { |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4262 | char buf[350]; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4263 | PyObject *v; |
Jeremy Hylton | 5125773 | 2001-03-01 00:42:55 +0000 | [diff] [blame] | 4264 | |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4265 | if (PyDict_GetItemString(c->c_cellvars, name) != NULL) |
| 4266 | return CELL; |
| 4267 | if (PyDict_GetItemString(c->c_locals, name) != NULL) |
| 4268 | return LOCAL; |
| 4269 | if (PyDict_GetItemString(c->c_freevars, name) != NULL) |
| 4270 | return FREE; |
| 4271 | v = PyDict_GetItemString(c->c_globals, name); |
| 4272 | if (v) { |
| 4273 | if (v == Py_None) |
| 4274 | return GLOBAL_EXPLICIT; |
| 4275 | else { |
| 4276 | return GLOBAL_IMPLICIT; |
| 4277 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4278 | } |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 4279 | PyOS_snprintf(buf, sizeof(buf), |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4280 | "unknown scope for %.100s in %.100s(%s) " |
| 4281 | "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n", |
| 4282 | name, c->c_name, |
| 4283 | PyObject_REPR(c->c_symtable->st_cur->ste_id), |
| 4284 | c->c_filename, |
| 4285 | PyObject_REPR(c->c_symtable->st_cur->ste_symbols), |
| 4286 | PyObject_REPR(c->c_locals), |
| 4287 | PyObject_REPR(c->c_globals) |
| 4288 | ); |
| 4289 | |
| 4290 | Py_FatalError(buf); |
| 4291 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4292 | } |
| 4293 | |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4294 | /* Helper functions to issue warnings */ |
| 4295 | |
| 4296 | static int |
| 4297 | issue_warning(char *msg, char *filename, int lineno) |
| 4298 | { |
| 4299 | if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename, |
| 4300 | lineno, NULL, NULL) < 0) { |
| 4301 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
| 4302 | PyErr_SetString(PyExc_SyntaxError, msg); |
| 4303 | PyErr_SyntaxLocation(filename, lineno); |
| 4304 | } |
| 4305 | return -1; |
| 4306 | } |
| 4307 | return 0; |
| 4308 | } |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4309 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4310 | static int |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4311 | symtable_warn(struct symtable *st, char *msg) |
| 4312 | { |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4313 | 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] | 4314 | st->st_errors++; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4315 | return -1; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4316 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4317 | return 0; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4318 | } |
| 4319 | |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 4320 | /* Helper function for setting lineno and filename */ |
| 4321 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4322 | static int |
| 4323 | symtable_build(struct compiling *c, node *n) |
| 4324 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4325 | if ((c->c_symtable = symtable_init()) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4326 | return -1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 4327 | c->c_symtable->st_future = c->c_future; |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4328 | c->c_symtable->st_filename = c->c_filename; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4329 | symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno); |
| 4330 | if (c->c_symtable->st_errors > 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4331 | return -1; |
| 4332 | symtable_node(c->c_symtable, n); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4333 | if (c->c_symtable->st_errors > 0) |
| 4334 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4335 | /* reset for second pass */ |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4336 | c->c_symtable->st_nscopes = 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4337 | c->c_symtable->st_pass = 2; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4338 | return 0; |
| 4339 | } |
| 4340 | |
| 4341 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4342 | symtable_init_compiling_symbols(struct compiling *c) |
| 4343 | { |
| 4344 | PyObject *varnames; |
| 4345 | |
| 4346 | varnames = c->c_symtable->st_cur->ste_varnames; |
| 4347 | if (varnames == NULL) { |
| 4348 | varnames = PyList_New(0); |
| 4349 | if (varnames == NULL) |
| 4350 | return -1; |
| 4351 | c->c_symtable->st_cur->ste_varnames = varnames; |
| 4352 | Py_INCREF(varnames); |
| 4353 | } else |
| 4354 | Py_INCREF(varnames); |
| 4355 | c->c_varnames = varnames; |
| 4356 | |
| 4357 | c->c_globals = PyDict_New(); |
| 4358 | if (c->c_globals == NULL) |
| 4359 | return -1; |
| 4360 | c->c_freevars = PyDict_New(); |
| 4361 | if (c->c_freevars == NULL) |
| 4362 | return -1; |
| 4363 | c->c_cellvars = PyDict_New(); |
| 4364 | if (c->c_cellvars == NULL) |
| 4365 | return -1; |
| 4366 | return 0; |
| 4367 | } |
| 4368 | |
| 4369 | struct symbol_info { |
| 4370 | int si_nlocals; |
| 4371 | int si_ncells; |
| 4372 | int si_nfrees; |
| 4373 | int si_nimplicit; |
| 4374 | }; |
| 4375 | |
| 4376 | static void |
| 4377 | symtable_init_info(struct symbol_info *si) |
| 4378 | { |
| 4379 | si->si_nlocals = 0; |
| 4380 | si->si_ncells = 0; |
| 4381 | si->si_nfrees = 0; |
| 4382 | si->si_nimplicit = 0; |
| 4383 | } |
| 4384 | |
| 4385 | static int |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4386 | symtable_resolve_free(struct compiling *c, PyObject *name, int flags, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4387 | struct symbol_info *si) |
| 4388 | { |
| 4389 | PyObject *dict, *v; |
| 4390 | |
| 4391 | /* Seperate logic for DEF_FREE. If it occurs in a function, |
| 4392 | it indicates a local that we must allocate storage for (a |
| 4393 | cell var). If it occurs in a class, then the class has a |
| 4394 | method and a free variable with the same name. |
| 4395 | */ |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4396 | if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4397 | /* If it isn't declared locally, it can't be a cell. */ |
| 4398 | if (!(flags & (DEF_LOCAL | DEF_PARAM))) |
| 4399 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4400 | v = PyInt_FromLong(si->si_ncells++); |
| 4401 | dict = c->c_cellvars; |
| 4402 | } else { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4403 | /* If it is free anyway, then there is no need to do |
| 4404 | anything here. |
| 4405 | */ |
| 4406 | if (is_free(flags ^ DEF_FREE_CLASS) |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4407 | || (flags == DEF_FREE_CLASS)) |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4408 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4409 | v = PyInt_FromLong(si->si_nfrees++); |
| 4410 | dict = c->c_freevars; |
| 4411 | } |
| 4412 | if (v == NULL) |
| 4413 | return -1; |
| 4414 | if (PyDict_SetItem(dict, name, v) < 0) { |
| 4415 | Py_DECREF(v); |
| 4416 | return -1; |
| 4417 | } |
| 4418 | Py_DECREF(v); |
| 4419 | return 0; |
| 4420 | } |
| 4421 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4422 | /* If a variable is a cell and an argument, make sure that appears in |
| 4423 | co_cellvars before any variable to its right in varnames. |
| 4424 | */ |
| 4425 | |
| 4426 | |
| 4427 | static int |
| 4428 | symtable_cellvar_offsets(PyObject **cellvars, int argcount, |
| 4429 | PyObject *varnames, int flags) |
| 4430 | { |
| 4431 | PyObject *v, *w, *d, *list = NULL; |
| 4432 | int i, pos; |
| 4433 | |
| 4434 | if (flags & CO_VARARGS) |
| 4435 | argcount++; |
| 4436 | if (flags & CO_VARKEYWORDS) |
| 4437 | argcount++; |
| 4438 | for (i = argcount; --i >= 0; ) { |
| 4439 | v = PyList_GET_ITEM(varnames, i); |
| 4440 | if (PyDict_GetItem(*cellvars, v)) { |
| 4441 | if (list == NULL) { |
| 4442 | list = PyList_New(1); |
| 4443 | if (list == NULL) |
| 4444 | return -1; |
| 4445 | PyList_SET_ITEM(list, 0, v); |
| 4446 | Py_INCREF(v); |
| 4447 | } else |
| 4448 | PyList_Insert(list, 0, v); |
| 4449 | } |
| 4450 | } |
| 4451 | if (list == NULL || PyList_GET_SIZE(list) == 0) |
| 4452 | return 0; |
| 4453 | /* There are cellvars that are also arguments. Create a dict |
| 4454 | to replace cellvars and put the args at the front. |
| 4455 | */ |
| 4456 | d = PyDict_New(); |
| 4457 | for (i = PyList_GET_SIZE(list); --i >= 0; ) { |
| 4458 | v = PyInt_FromLong(i); |
| 4459 | if (v == NULL) |
| 4460 | goto fail; |
| 4461 | if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0) |
| 4462 | goto fail; |
| 4463 | if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0) |
| 4464 | goto fail; |
| 4465 | } |
| 4466 | pos = 0; |
| 4467 | i = PyList_GET_SIZE(list); |
| 4468 | Py_DECREF(list); |
| 4469 | while (PyDict_Next(*cellvars, &pos, &v, &w)) { |
| 4470 | w = PyInt_FromLong(i++); /* don't care about the old key */ |
| 4471 | if (PyDict_SetItem(d, v, w) < 0) { |
| 4472 | Py_DECREF(w); |
| 4473 | goto fail; |
| 4474 | } |
| 4475 | Py_DECREF(w); |
| 4476 | } |
| 4477 | Py_DECREF(*cellvars); |
| 4478 | *cellvars = d; |
| 4479 | return 1; |
| 4480 | fail: |
| 4481 | Py_DECREF(d); |
| 4482 | return -1; |
| 4483 | } |
| 4484 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4485 | static int |
| 4486 | symtable_freevar_offsets(PyObject *freevars, int offset) |
| 4487 | { |
| 4488 | PyObject *name, *v; |
| 4489 | int pos; |
| 4490 | |
| 4491 | /* The cell vars are the first elements of the closure, |
| 4492 | followed by the free vars. Update the offsets in |
| 4493 | c_freevars to account for number of cellvars. */ |
| 4494 | pos = 0; |
| 4495 | while (PyDict_Next(freevars, &pos, &name, &v)) { |
| 4496 | int i = PyInt_AS_LONG(v) + offset; |
| 4497 | PyObject *o = PyInt_FromLong(i); |
| 4498 | if (o == NULL) |
| 4499 | return -1; |
| 4500 | if (PyDict_SetItem(freevars, name, o) < 0) { |
| 4501 | Py_DECREF(o); |
| 4502 | return -1; |
| 4503 | } |
| 4504 | Py_DECREF(o); |
| 4505 | } |
| 4506 | return 0; |
| 4507 | } |
| 4508 | |
| 4509 | static int |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4510 | symtable_check_unoptimized(struct compiling *c, |
| 4511 | PySymtableEntryObject *ste, |
| 4512 | struct symbol_info *si) |
| 4513 | { |
| 4514 | char buf[300]; |
| 4515 | |
| 4516 | if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free |
| 4517 | || (ste->ste_nested && si->si_nimplicit))) |
| 4518 | return 0; |
| 4519 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4520 | #define ILLEGAL_CONTAINS "contains a nested function with free variables" |
| 4521 | |
| 4522 | #define ILLEGAL_IS "is a nested function" |
| 4523 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4524 | #define ILLEGAL_IMPORT_STAR \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4525 | "import * is not allowed in function '%.100s' because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4526 | |
| 4527 | #define ILLEGAL_BARE_EXEC \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4528 | "unqualified exec is not allowed in function '%.100s' it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4529 | |
| 4530 | #define ILLEGAL_EXEC_AND_IMPORT_STAR \ |
Neal Norwitz | 150d09d | 2002-01-29 00:56:37 +0000 | [diff] [blame] | 4531 | "function '%.100s' uses import * and bare exec, which are illegal " \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4532 | "because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4533 | |
| 4534 | /* XXX perhaps the linenos for these opt-breaking statements |
| 4535 | should be stored so the exception can point to them. */ |
| 4536 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4537 | if (ste->ste_child_free) { |
| 4538 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4539 | PyOS_snprintf(buf, sizeof(buf), |
| 4540 | ILLEGAL_IMPORT_STAR, |
| 4541 | PyString_AS_STRING(ste->ste_name), |
| 4542 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4543 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4544 | PyOS_snprintf(buf, sizeof(buf), |
| 4545 | ILLEGAL_BARE_EXEC, |
| 4546 | PyString_AS_STRING(ste->ste_name), |
| 4547 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4548 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4549 | PyOS_snprintf(buf, sizeof(buf), |
| 4550 | ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 4551 | PyString_AS_STRING(ste->ste_name), |
| 4552 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4553 | } |
| 4554 | } else { |
| 4555 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4556 | PyOS_snprintf(buf, sizeof(buf), |
| 4557 | ILLEGAL_IMPORT_STAR, |
| 4558 | PyString_AS_STRING(ste->ste_name), |
| 4559 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4560 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4561 | PyOS_snprintf(buf, sizeof(buf), |
| 4562 | ILLEGAL_BARE_EXEC, |
| 4563 | PyString_AS_STRING(ste->ste_name), |
| 4564 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4565 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4566 | PyOS_snprintf(buf, sizeof(buf), |
| 4567 | ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 4568 | PyString_AS_STRING(ste->ste_name), |
| 4569 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4570 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4571 | } |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4572 | |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4573 | PyErr_SetString(PyExc_SyntaxError, buf); |
| 4574 | PyErr_SyntaxLocation(c->c_symtable->st_filename, |
| 4575 | ste->ste_opt_lineno); |
| 4576 | return -1; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4577 | } |
| 4578 | |
| 4579 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4580 | symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste, |
| 4581 | struct symbol_info *si) |
| 4582 | { |
Jeremy Hylton | b857ba2 | 2001-08-10 21:41:33 +0000 | [diff] [blame] | 4583 | if (c->c_future) |
| 4584 | c->c_flags |= c->c_future->ff_features; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4585 | if (ste->ste_generator) |
| 4586 | c->c_flags |= CO_GENERATOR; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4587 | if (ste->ste_type != TYPE_MODULE) |
| 4588 | c->c_flags |= CO_NEWLOCALS; |
| 4589 | if (ste->ste_type == TYPE_FUNCTION) { |
| 4590 | c->c_nlocals = si->si_nlocals; |
| 4591 | if (ste->ste_optimized == 0) |
| 4592 | c->c_flags |= CO_OPTIMIZED; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4593 | else if (ste->ste_optimized != OPT_EXEC) |
| 4594 | return symtable_check_unoptimized(c, ste, si); |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4595 | } |
| 4596 | return 0; |
| 4597 | } |
| 4598 | |
| 4599 | static int |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4600 | symtable_load_symbols(struct compiling *c) |
| 4601 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4602 | static PyObject *implicit = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4603 | struct symtable *st = c->c_symtable; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4604 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4605 | PyObject *name, *varnames, *v; |
| 4606 | int i, flags, pos; |
| 4607 | struct symbol_info si; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4608 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4609 | if (implicit == NULL) { |
| 4610 | implicit = PyInt_FromLong(1); |
| 4611 | if (implicit == NULL) |
| 4612 | return -1; |
| 4613 | } |
| 4614 | v = NULL; |
| 4615 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4616 | if (symtable_init_compiling_symbols(c) < 0) |
| 4617 | goto fail; |
| 4618 | symtable_init_info(&si); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4619 | varnames = st->st_cur->ste_varnames; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4620 | si.si_nlocals = PyList_GET_SIZE(varnames); |
| 4621 | c->c_argcount = si.si_nlocals; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4622 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4623 | for (i = 0; i < si.si_nlocals; ++i) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4624 | v = PyInt_FromLong(i); |
| 4625 | if (PyDict_SetItem(c->c_locals, |
| 4626 | PyList_GET_ITEM(varnames, i), v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4627 | goto fail; |
| 4628 | Py_DECREF(v); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | /* XXX The cases below define the rules for whether a name is |
| 4632 | local or global. The logic could probably be clearer. */ |
| 4633 | pos = 0; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4634 | while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) { |
| 4635 | flags = PyInt_AS_LONG(v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4636 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4637 | if (flags & DEF_FREE_GLOBAL) |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4638 | /* undo the original DEF_FREE */ |
| 4639 | flags &= ~(DEF_FREE | DEF_FREE_CLASS); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4640 | |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4641 | /* Deal with names that need two actions: |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4642 | 1. Cell variables that are also locals. |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4643 | 2. Free variables in methods that are also class |
| 4644 | variables or declared global. |
| 4645 | */ |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4646 | if (flags & (DEF_FREE | DEF_FREE_CLASS)) |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4647 | symtable_resolve_free(c, name, flags, &si); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4648 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4649 | if (flags & DEF_STAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4650 | c->c_argcount--; |
| 4651 | c->c_flags |= CO_VARARGS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4652 | } else if (flags & DEF_DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4653 | c->c_argcount--; |
| 4654 | c->c_flags |= CO_VARKEYWORDS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4655 | } else if (flags & DEF_INTUPLE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4656 | c->c_argcount--; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4657 | else if (flags & DEF_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4658 | if (flags & DEF_PARAM) { |
| 4659 | PyErr_Format(PyExc_SyntaxError, LOCAL_GLOBAL, |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4660 | PyString_AS_STRING(name)); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 4661 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4662 | ste->ste_lineno); |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4663 | st->st_errors++; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4664 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4665 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4666 | if (PyDict_SetItem(c->c_globals, name, Py_None) < 0) |
| 4667 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4668 | } else if (flags & DEF_FREE_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4669 | si.si_nimplicit++; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4670 | if (PyDict_SetItem(c->c_globals, name, implicit) < 0) |
| 4671 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4672 | } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4673 | v = PyInt_FromLong(si.si_nlocals++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4674 | if (v == NULL) |
| 4675 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4676 | if (PyDict_SetItem(c->c_locals, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4677 | goto fail; |
| 4678 | Py_DECREF(v); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4679 | if (ste->ste_type != TYPE_CLASS) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4680 | if (PyList_Append(c->c_varnames, name) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4681 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4682 | } else if (is_free(flags)) { |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4683 | if (ste->ste_nested) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4684 | v = PyInt_FromLong(si.si_nfrees++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4685 | if (v == NULL) |
| 4686 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4687 | if (PyDict_SetItem(c->c_freevars, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4688 | goto fail; |
| 4689 | Py_DECREF(v); |
| 4690 | } else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4691 | si.si_nimplicit++; |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4692 | if (PyDict_SetItem(c->c_globals, name, |
| 4693 | implicit) < 0) |
| 4694 | goto fail; |
| 4695 | if (st->st_nscopes != 1) { |
| 4696 | v = PyInt_FromLong(flags); |
| 4697 | if (PyDict_SetItem(st->st_global, |
| 4698 | name, v)) |
| 4699 | goto fail; |
| 4700 | Py_DECREF(v); |
| 4701 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4702 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4703 | } |
| 4704 | } |
| 4705 | |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4706 | assert(PyDict_Size(c->c_freevars) == si.si_nfrees); |
| 4707 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4708 | if (si.si_ncells > 1) { /* one cell is always in order */ |
| 4709 | if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount, |
| 4710 | c->c_varnames, c->c_flags) < 0) |
| 4711 | return -1; |
| 4712 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4713 | if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0) |
| 4714 | return -1; |
| 4715 | return symtable_update_flags(c, ste, &si); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4716 | fail: |
| 4717 | /* is this always the right thing to do? */ |
| 4718 | Py_XDECREF(v); |
| 4719 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4720 | } |
| 4721 | |
| 4722 | static struct symtable * |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4723 | symtable_init() |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4724 | { |
| 4725 | struct symtable *st; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4726 | |
| 4727 | st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable)); |
| 4728 | if (st == NULL) |
| 4729 | return NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4730 | st->st_pass = 1; |
Tim Peters | ff1f852 | 2001-08-11 01:06:35 +0000 | [diff] [blame] | 4731 | |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4732 | st->st_filename = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4733 | if ((st->st_stack = PyList_New(0)) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4734 | goto fail; |
| 4735 | if ((st->st_symbols = PyDict_New()) == NULL) |
| 4736 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4737 | st->st_cur = NULL; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4738 | st->st_nscopes = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4739 | st->st_errors = 0; |
| 4740 | st->st_tmpname = 0; |
| 4741 | st->st_private = NULL; |
| 4742 | return st; |
| 4743 | fail: |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4744 | PySymtable_Free(st); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4745 | return NULL; |
| 4746 | } |
| 4747 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4748 | void |
| 4749 | PySymtable_Free(struct symtable *st) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4750 | { |
| 4751 | Py_XDECREF(st->st_symbols); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4752 | Py_XDECREF(st->st_stack); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4753 | Py_XDECREF(st->st_cur); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4754 | PyMem_Free((void *)st); |
| 4755 | } |
| 4756 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4757 | /* When the compiler exits a scope, it must should update the scope's |
| 4758 | free variable information with the list of free variables in its |
| 4759 | children. |
| 4760 | |
| 4761 | Variables that are free in children and defined in the current |
| 4762 | scope are cellvars. |
| 4763 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4764 | 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] | 4765 | false), free variables in children that are not defined here are |
| 4766 | implicit globals. |
| 4767 | |
| 4768 | */ |
| 4769 | |
| 4770 | static int |
| 4771 | symtable_update_free_vars(struct symtable *st) |
| 4772 | { |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4773 | int i, j, def; |
| 4774 | PyObject *o, *name, *list = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4775 | PySymtableEntryObject *child, *ste = st->st_cur; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4776 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4777 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4778 | def = DEF_FREE_CLASS; |
| 4779 | else |
| 4780 | def = DEF_FREE; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4781 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4782 | int pos = 0; |
| 4783 | |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4784 | if (list) |
| 4785 | PyList_SetSlice(list, 0, |
| 4786 | ((PyVarObject*)list)->ob_size, 0); |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4787 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4788 | PyList_GET_ITEM(ste->ste_children, i); |
| 4789 | while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4790 | int flags = PyInt_AS_LONG(o); |
| 4791 | if (!(is_free(flags))) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4792 | continue; /* avoids indentation */ |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4793 | if (list == NULL) { |
| 4794 | list = PyList_New(0); |
| 4795 | if (list == NULL) |
| 4796 | return -1; |
| 4797 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4798 | ste->ste_child_free = 1; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4799 | if (PyList_Append(list, name) < 0) { |
| 4800 | Py_DECREF(list); |
| 4801 | return -1; |
| 4802 | } |
| 4803 | } |
| 4804 | for (j = 0; list && j < PyList_GET_SIZE(list); j++) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4805 | PyObject *v; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4806 | name = PyList_GET_ITEM(list, j); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4807 | v = PyDict_GetItem(ste->ste_symbols, name); |
| 4808 | /* If a name N is declared global in scope A and |
| 4809 | referenced in scope B contained (perhaps |
| 4810 | indirectly) in A and there are no scopes |
| 4811 | with bindings for N between B and A, then N |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4812 | is global in B. Unless A is a class scope, |
| 4813 | because class scopes are not considered for |
| 4814 | nested scopes. |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4815 | */ |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4816 | if (v && (ste->ste_type != TYPE_CLASS)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4817 | int flags = PyInt_AS_LONG(v); |
| 4818 | if (flags & DEF_GLOBAL) { |
| 4819 | symtable_undo_free(st, child->ste_id, |
| 4820 | name); |
| 4821 | continue; |
| 4822 | } |
| 4823 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4824 | if (ste->ste_nested) { |
| 4825 | if (symtable_add_def_o(st, ste->ste_symbols, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4826 | name, def) < 0) { |
| 4827 | Py_DECREF(list); |
| 4828 | return -1; |
| 4829 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4830 | } else { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4831 | if (symtable_check_global(st, child->ste_id, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4832 | name) < 0) { |
| 4833 | Py_DECREF(list); |
| 4834 | return -1; |
| 4835 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4836 | } |
| 4837 | } |
| 4838 | } |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4839 | |
| 4840 | Py_XDECREF(list); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4841 | return 0; |
| 4842 | } |
| 4843 | |
| 4844 | /* If the current scope is a non-nested class or if name is not |
| 4845 | defined in the current, non-nested scope, then it is an implicit |
| 4846 | global in all nested scopes. |
| 4847 | */ |
| 4848 | |
| 4849 | static int |
| 4850 | symtable_check_global(struct symtable *st, PyObject *child, PyObject *name) |
| 4851 | { |
| 4852 | PyObject *o; |
| 4853 | int v; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4854 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4855 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4856 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4857 | return symtable_undo_free(st, child, name); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4858 | o = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4859 | if (o == NULL) |
| 4860 | return symtable_undo_free(st, child, name); |
| 4861 | v = PyInt_AS_LONG(o); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4862 | |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4863 | if (is_free(v) || (v & DEF_GLOBAL)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4864 | return symtable_undo_free(st, child, name); |
| 4865 | else |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4866 | return symtable_add_def_o(st, ste->ste_symbols, |
| 4867 | name, DEF_FREE); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4868 | } |
| 4869 | |
| 4870 | static int |
| 4871 | symtable_undo_free(struct symtable *st, PyObject *id, |
| 4872 | PyObject *name) |
| 4873 | { |
| 4874 | int i, v, x; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4875 | PyObject *info; |
| 4876 | PySymtableEntryObject *ste; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4877 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4878 | ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id); |
| 4879 | if (ste == NULL) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4880 | return -1; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4881 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4882 | info = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4883 | if (info == NULL) |
| 4884 | return 0; |
| 4885 | v = PyInt_AS_LONG(info); |
| 4886 | if (is_free(v)) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4887 | if (symtable_add_def_o(st, ste->ste_symbols, name, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4888 | DEF_FREE_GLOBAL) < 0) |
| 4889 | return -1; |
| 4890 | } else |
| 4891 | /* If the name is defined here or declared global, |
| 4892 | then the recursion stops. */ |
| 4893 | return 0; |
| 4894 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4895 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
| 4896 | PySymtableEntryObject *child; |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4897 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4898 | PyList_GET_ITEM(ste->ste_children, i); |
| 4899 | x = symtable_undo_free(st, child->ste_id, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4900 | if (x < 0) |
| 4901 | return x; |
| 4902 | } |
| 4903 | return 0; |
| 4904 | } |
| 4905 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4906 | /* symtable_enter_scope() gets a reference via PySymtableEntry_New(). |
| 4907 | This reference is released when the scope is exited, via the DECREF |
| 4908 | in symtable_exit_scope(). |
| 4909 | */ |
| 4910 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4911 | static int |
| 4912 | symtable_exit_scope(struct symtable *st) |
| 4913 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4914 | int end; |
| 4915 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4916 | if (st->st_pass == 1) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4917 | symtable_update_free_vars(st); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4918 | Py_DECREF(st->st_cur); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4919 | end = PyList_GET_SIZE(st->st_stack) - 1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4920 | st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack, |
| 4921 | end); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4922 | if (PySequence_DelItem(st->st_stack, end) < 0) |
| 4923 | return -1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4924 | return 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4925 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4926 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4927 | static void |
| 4928 | symtable_enter_scope(struct symtable *st, char *name, int type, |
| 4929 | int lineno) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4930 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4931 | PySymtableEntryObject *prev = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4932 | |
| 4933 | if (st->st_cur) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4934 | prev = st->st_cur; |
| 4935 | if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) { |
| 4936 | Py_DECREF(st->st_cur); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4937 | st->st_errors++; |
| 4938 | return; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4939 | } |
| 4940 | } |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4941 | st->st_cur = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4942 | PySymtableEntry_New(st, name, type, lineno); |
| 4943 | if (strcmp(name, TOP) == 0) |
| 4944 | st->st_global = st->st_cur->ste_symbols; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4945 | if (prev && st->st_pass == 1) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4946 | if (PyList_Append(prev->ste_children, |
| 4947 | (PyObject *)st->st_cur) < 0) |
| 4948 | st->st_errors++; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4949 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4950 | } |
| 4951 | |
| 4952 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4953 | symtable_lookup(struct symtable *st, char *name) |
| 4954 | { |
| 4955 | char buffer[MANGLE_LEN]; |
| 4956 | PyObject *v; |
| 4957 | int flags; |
| 4958 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 4959 | if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4960 | name = buffer; |
| 4961 | v = PyDict_GetItemString(st->st_cur->ste_symbols, name); |
| 4962 | if (v == NULL) { |
| 4963 | if (PyErr_Occurred()) |
| 4964 | return -1; |
| 4965 | else |
| 4966 | return 0; |
| 4967 | } |
| 4968 | |
| 4969 | flags = PyInt_AS_LONG(v); |
| 4970 | return flags; |
| 4971 | } |
| 4972 | |
| 4973 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4974 | symtable_add_def(struct symtable *st, char *name, int flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4975 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4976 | PyObject *s; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4977 | char buffer[MANGLE_LEN]; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 4978 | int ret; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4979 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 4980 | if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4981 | name = buffer; |
| 4982 | if ((s = PyString_InternFromString(name)) == NULL) |
| 4983 | return -1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 4984 | ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag); |
| 4985 | Py_DECREF(s); |
| 4986 | return ret; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4987 | } |
| 4988 | |
| 4989 | /* Must only be called with mangled names */ |
| 4990 | |
| 4991 | static int |
| 4992 | symtable_add_def_o(struct symtable *st, PyObject *dict, |
| 4993 | PyObject *name, int flag) |
| 4994 | { |
| 4995 | PyObject *o; |
| 4996 | int val; |
| 4997 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4998 | if ((o = PyDict_GetItem(dict, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4999 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5000 | if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5001 | PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5002 | PyString_AsString(name)); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5003 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5004 | st->st_cur->ste_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5005 | return -1; |
| 5006 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5007 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5008 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5009 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5010 | o = PyInt_FromLong(val); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5011 | if (PyDict_SetItem(dict, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5012 | Py_DECREF(o); |
| 5013 | return -1; |
| 5014 | } |
| 5015 | Py_DECREF(o); |
| 5016 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5017 | if (flag & DEF_PARAM) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5018 | if (PyList_Append(st->st_cur->ste_varnames, name) < 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5019 | return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5020 | } else if (flag & DEF_GLOBAL) { |
| 5021 | /* XXX need to update DEF_GLOBAL for other flags too; |
| 5022 | perhaps only DEF_FREE_GLOBAL */ |
| 5023 | if ((o = PyDict_GetItem(st->st_global, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5024 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5025 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5026 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5027 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5028 | o = PyInt_FromLong(val); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5029 | if (PyDict_SetItem(st->st_global, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5030 | Py_DECREF(o); |
| 5031 | return -1; |
| 5032 | } |
| 5033 | Py_DECREF(o); |
| 5034 | } |
| 5035 | return 0; |
| 5036 | } |
| 5037 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5038 | #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5039 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 5040 | /* Look for a yield stmt under n. Return 1 if found, else 0. |
| 5041 | This hack is used to look inside "if 0:" blocks (which are normally |
| 5042 | ignored) in case those are the only places a yield occurs (so that this |
| 5043 | function is a generator). */ |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5044 | static int |
| 5045 | look_for_yield(node *n) |
| 5046 | { |
| 5047 | int i; |
| 5048 | |
| 5049 | for (i = 0; i < NCH(n); ++i) { |
| 5050 | node *kid = CHILD(n, i); |
| 5051 | |
| 5052 | switch (TYPE(kid)) { |
| 5053 | |
| 5054 | case classdef: |
| 5055 | case funcdef: |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 5056 | case lambdef: |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5057 | /* Stuff in nested functions and classes can't make |
| 5058 | the parent a generator. */ |
| 5059 | return 0; |
| 5060 | |
| 5061 | case yield_stmt: |
| 5062 | return 1; |
| 5063 | |
| 5064 | default: |
| 5065 | if (look_for_yield(kid)) |
| 5066 | return 1; |
| 5067 | } |
| 5068 | } |
| 5069 | return 0; |
| 5070 | } |
| 5071 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5072 | static void |
| 5073 | symtable_node(struct symtable *st, node *n) |
| 5074 | { |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5075 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5076 | |
| 5077 | loop: |
| 5078 | switch (TYPE(n)) { |
| 5079 | case funcdef: { |
| 5080 | char *func_name = STR(CHILD(n, 1)); |
| 5081 | symtable_add_def(st, func_name, DEF_LOCAL); |
| 5082 | symtable_default_args(st, CHILD(n, 2)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5083 | symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5084 | symtable_funcdef(st, n); |
| 5085 | symtable_exit_scope(st); |
| 5086 | break; |
| 5087 | } |
| 5088 | case lambdef: |
| 5089 | if (NCH(n) == 4) |
| 5090 | symtable_default_args(st, CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5091 | symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5092 | symtable_funcdef(st, n); |
| 5093 | symtable_exit_scope(st); |
| 5094 | break; |
| 5095 | case classdef: { |
| 5096 | char *tmp, *class_name = STR(CHILD(n, 1)); |
| 5097 | symtable_add_def(st, class_name, DEF_LOCAL); |
| 5098 | if (TYPE(CHILD(n, 2)) == LPAR) { |
| 5099 | node *bases = CHILD(n, 3); |
| 5100 | int i; |
| 5101 | for (i = 0; i < NCH(bases); i += 2) { |
| 5102 | symtable_node(st, CHILD(bases, i)); |
| 5103 | } |
| 5104 | } |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5105 | symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5106 | tmp = st->st_private; |
| 5107 | st->st_private = class_name; |
| 5108 | symtable_node(st, CHILD(n, NCH(n) - 1)); |
| 5109 | st->st_private = tmp; |
| 5110 | symtable_exit_scope(st); |
| 5111 | break; |
| 5112 | } |
| 5113 | case if_stmt: |
| 5114 | for (i = 0; i + 3 < NCH(n); i += 4) { |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5115 | if (is_constant_false(NULL, (CHILD(n, i + 1)))) { |
| 5116 | if (st->st_cur->ste_generator == 0) |
| 5117 | st->st_cur->ste_generator = |
| 5118 | look_for_yield(CHILD(n, i+3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5119 | continue; |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5120 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5121 | symtable_node(st, CHILD(n, i + 1)); |
| 5122 | symtable_node(st, CHILD(n, i + 3)); |
| 5123 | } |
| 5124 | if (i + 2 < NCH(n)) |
| 5125 | symtable_node(st, CHILD(n, i + 2)); |
| 5126 | break; |
| 5127 | case global_stmt: |
| 5128 | symtable_global(st, n); |
| 5129 | break; |
| 5130 | case import_stmt: |
| 5131 | symtable_import(st, n); |
| 5132 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5133 | case exec_stmt: { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5134 | st->st_cur->ste_optimized |= OPT_EXEC; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5135 | symtable_node(st, CHILD(n, 1)); |
| 5136 | if (NCH(n) > 2) |
| 5137 | symtable_node(st, CHILD(n, 3)); |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5138 | else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5139 | st->st_cur->ste_optimized |= OPT_BARE_EXEC; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5140 | st->st_cur->ste_opt_lineno = n->n_lineno; |
| 5141 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5142 | if (NCH(n) > 4) |
| 5143 | symtable_node(st, CHILD(n, 5)); |
| 5144 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5145 | |
| 5146 | } |
Jeremy Hylton | 384639f | 2001-02-19 15:50:51 +0000 | [diff] [blame] | 5147 | case assert_stmt: |
| 5148 | if (Py_OptimizeFlag) |
| 5149 | return; |
| 5150 | if (NCH(n) == 2) { |
| 5151 | n = CHILD(n, 1); |
| 5152 | goto loop; |
| 5153 | } else { |
| 5154 | symtable_node(st, CHILD(n, 1)); |
| 5155 | n = CHILD(n, 3); |
| 5156 | goto loop; |
| 5157 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5158 | case except_clause: |
| 5159 | if (NCH(n) == 4) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5160 | symtable_assign(st, CHILD(n, 3), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5161 | if (NCH(n) > 1) { |
| 5162 | n = CHILD(n, 1); |
| 5163 | goto loop; |
| 5164 | } |
| 5165 | break; |
| 5166 | case del_stmt: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5167 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5168 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 5169 | case yield_stmt: |
| 5170 | st->st_cur->ste_generator = 1; |
| 5171 | n = CHILD(n, 1); |
| 5172 | goto loop; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5173 | case expr_stmt: |
| 5174 | if (NCH(n) == 1) |
| 5175 | n = CHILD(n, 0); |
| 5176 | else { |
| 5177 | if (TYPE(CHILD(n, 1)) == augassign) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5178 | symtable_assign(st, CHILD(n, 0), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5179 | symtable_node(st, CHILD(n, 2)); |
| 5180 | break; |
| 5181 | } else { |
| 5182 | int i; |
| 5183 | for (i = 0; i < NCH(n) - 2; i += 2) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5184 | symtable_assign(st, CHILD(n, i), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5185 | n = CHILD(n, NCH(n) - 1); |
| 5186 | } |
| 5187 | } |
| 5188 | goto loop; |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5189 | case list_iter: |
| 5190 | n = CHILD(n, 0); |
| 5191 | if (TYPE(n) == list_for) { |
| 5192 | st->st_tmpname++; |
| 5193 | symtable_list_comprehension(st, n); |
| 5194 | st->st_tmpname--; |
| 5195 | } else { |
| 5196 | REQ(n, list_if); |
| 5197 | symtable_node(st, CHILD(n, 1)); |
| 5198 | if (NCH(n) == 3) { |
| 5199 | n = CHILD(n, 2); |
| 5200 | goto loop; |
| 5201 | } |
| 5202 | } |
| 5203 | break; |
| 5204 | case for_stmt: |
| 5205 | symtable_assign(st, CHILD(n, 1), 0); |
| 5206 | for (i = 3; i < NCH(n); ++i) |
| 5207 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5208 | symtable_node(st, CHILD(n, i)); |
| 5209 | break; |
| 5210 | /* The remaining cases fall through to default except in |
| 5211 | special circumstances. This requires the individual cases |
| 5212 | to be coded with great care, even though they look like |
| 5213 | rather innocuous. Each case must double-check TYPE(n). |
| 5214 | */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5215 | case argument: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5216 | if (TYPE(n) == argument && NCH(n) == 3) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5217 | n = CHILD(n, 2); |
| 5218 | goto loop; |
| 5219 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5220 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5221 | case listmaker: |
| 5222 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5223 | st->st_tmpname++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5224 | symtable_list_comprehension(st, CHILD(n, 1)); |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5225 | symtable_node(st, CHILD(n, 0)); |
| 5226 | st->st_tmpname--; |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5227 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5228 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5229 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5230 | case atom: |
| 5231 | if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) { |
| 5232 | symtable_add_use(st, STR(CHILD(n, 0))); |
| 5233 | break; |
| 5234 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5235 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5236 | default: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5237 | /* Walk over every non-token child with a special case |
| 5238 | for one child. |
| 5239 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5240 | if (NCH(n) == 1) { |
| 5241 | n = CHILD(n, 0); |
| 5242 | goto loop; |
| 5243 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5244 | for (i = 0; i < NCH(n); ++i) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5245 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5246 | symtable_node(st, CHILD(n, i)); |
| 5247 | } |
| 5248 | } |
| 5249 | |
| 5250 | static void |
| 5251 | symtable_funcdef(struct symtable *st, node *n) |
| 5252 | { |
| 5253 | node *body; |
| 5254 | |
| 5255 | if (TYPE(n) == lambdef) { |
| 5256 | if (NCH(n) == 4) |
| 5257 | symtable_params(st, CHILD(n, 1)); |
| 5258 | } else |
| 5259 | symtable_params(st, CHILD(n, 2)); |
| 5260 | body = CHILD(n, NCH(n) - 1); |
| 5261 | symtable_node(st, body); |
| 5262 | } |
| 5263 | |
| 5264 | /* The next two functions parse the argument tuple. |
| 5265 | symtable_default_arg() checks for names in the default arguments, |
| 5266 | which are references in the defining scope. symtable_params() |
| 5267 | parses the parameter names, which are defined in the function's |
| 5268 | body. |
| 5269 | |
| 5270 | varargslist: |
| 5271 | (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) |
| 5272 | | fpdef ['=' test] (',' fpdef ['=' test])* [','] |
| 5273 | */ |
| 5274 | |
| 5275 | static void |
| 5276 | symtable_default_args(struct symtable *st, node *n) |
| 5277 | { |
| 5278 | node *c; |
| 5279 | int i; |
| 5280 | |
| 5281 | if (TYPE(n) == parameters) { |
| 5282 | n = CHILD(n, 1); |
| 5283 | if (TYPE(n) == RPAR) |
| 5284 | return; |
| 5285 | } |
| 5286 | REQ(n, varargslist); |
| 5287 | for (i = 0; i < NCH(n); i += 2) { |
| 5288 | c = CHILD(n, i); |
| 5289 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5290 | break; |
| 5291 | } |
| 5292 | if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL)) |
| 5293 | symtable_node(st, CHILD(n, i)); |
| 5294 | } |
| 5295 | } |
| 5296 | |
| 5297 | static void |
| 5298 | symtable_params(struct symtable *st, node *n) |
| 5299 | { |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5300 | int i, complex = -1, ext = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5301 | node *c = NULL; |
| 5302 | |
| 5303 | if (TYPE(n) == parameters) { |
| 5304 | n = CHILD(n, 1); |
| 5305 | if (TYPE(n) == RPAR) |
| 5306 | return; |
| 5307 | } |
| 5308 | REQ(n, varargslist); |
| 5309 | for (i = 0; i < NCH(n); i += 2) { |
| 5310 | c = CHILD(n, i); |
| 5311 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5312 | ext = 1; |
| 5313 | break; |
| 5314 | } |
| 5315 | if (TYPE(c) == test) { |
| 5316 | continue; |
| 5317 | } |
| 5318 | if (TYPE(CHILD(c, 0)) == NAME) |
| 5319 | symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM); |
| 5320 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5321 | char nbuf[30]; |
| 5322 | PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5323 | symtable_add_def(st, nbuf, DEF_PARAM); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5324 | complex = i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5325 | } |
| 5326 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5327 | if (ext) { |
| 5328 | c = CHILD(n, i); |
| 5329 | if (TYPE(c) == STAR) { |
| 5330 | i++; |
| 5331 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5332 | DEF_PARAM | DEF_STAR); |
| 5333 | i += 2; |
Jeremy Hylton | 1113cfc | 2001-01-23 00:50:52 +0000 | [diff] [blame] | 5334 | if (i >= NCH(n)) |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5335 | c = NULL; |
| 5336 | else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5337 | c = CHILD(n, i); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5338 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5339 | if (c && TYPE(c) == DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5340 | i++; |
| 5341 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5342 | DEF_PARAM | DEF_DOUBLESTAR); |
| 5343 | } |
| 5344 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5345 | if (complex >= 0) { |
| 5346 | int j; |
| 5347 | for (j = 0; j <= complex; j++) { |
| 5348 | c = CHILD(n, j); |
| 5349 | if (TYPE(c) == COMMA) |
Jeremy Hylton | 2b3f0ca | 2001-02-19 23:52:49 +0000 | [diff] [blame] | 5350 | c = CHILD(n, ++j); |
| 5351 | else if (TYPE(c) == EQUAL) |
| 5352 | c = CHILD(n, j += 3); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5353 | if (TYPE(CHILD(c, 0)) == LPAR) |
| 5354 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5355 | } |
| 5356 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5357 | } |
| 5358 | |
| 5359 | static void |
| 5360 | symtable_params_fplist(struct symtable *st, node *n) |
| 5361 | { |
| 5362 | int i; |
| 5363 | node *c; |
| 5364 | |
| 5365 | REQ(n, fplist); |
| 5366 | for (i = 0; i < NCH(n); i += 2) { |
| 5367 | c = CHILD(n, i); |
| 5368 | REQ(c, fpdef); |
| 5369 | if (NCH(c) == 1) |
| 5370 | symtable_add_def(st, STR(CHILD(c, 0)), |
| 5371 | DEF_PARAM | DEF_INTUPLE); |
| 5372 | else |
| 5373 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5374 | } |
| 5375 | |
| 5376 | } |
| 5377 | |
| 5378 | static void |
| 5379 | symtable_global(struct symtable *st, node *n) |
| 5380 | { |
| 5381 | int i; |
| 5382 | |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 5383 | /* XXX It might be helpful to warn about module-level global |
| 5384 | statements, but it's hard to tell the difference between |
| 5385 | module-level and a string passed to exec. |
| 5386 | */ |
Jeremy Hylton | c176132 | 2001-02-28 23:44:45 +0000 | [diff] [blame] | 5387 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5388 | for (i = 1; i < NCH(n); i += 2) { |
| 5389 | char *name = STR(CHILD(n, i)); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5390 | int flags; |
| 5391 | |
| 5392 | flags = symtable_lookup(st, name); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5393 | if (flags < 0) |
| 5394 | continue; |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5395 | if (flags && flags != DEF_GLOBAL) { |
| 5396 | char buf[500]; |
| 5397 | if (flags & DEF_PARAM) { |
| 5398 | PyErr_Format(PyExc_SyntaxError, |
| 5399 | "name '%.400s' is local and global", |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5400 | name); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5401 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5402 | st->st_cur->ste_lineno); |
| 5403 | st->st_errors++; |
| 5404 | return; |
Guido van Rossum | 0bba7f8 | 2001-02-28 21:55:38 +0000 | [diff] [blame] | 5405 | } |
| 5406 | else { |
| 5407 | if (flags & DEF_LOCAL) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5408 | PyOS_snprintf(buf, sizeof(buf), |
| 5409 | GLOBAL_AFTER_ASSIGN, |
| 5410 | name); |
Guido van Rossum | 0bba7f8 | 2001-02-28 21:55:38 +0000 | [diff] [blame] | 5411 | else |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5412 | PyOS_snprintf(buf, sizeof(buf), |
| 5413 | GLOBAL_AFTER_USE, name); |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 5414 | symtable_warn(st, buf); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5415 | } |
| 5416 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5417 | symtable_add_def(st, name, DEF_GLOBAL); |
| 5418 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5419 | } |
| 5420 | |
| 5421 | static void |
| 5422 | symtable_list_comprehension(struct symtable *st, node *n) |
| 5423 | { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5424 | char tmpname[30]; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5425 | |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5426 | PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", st->st_tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5427 | symtable_add_def(st, tmpname, DEF_LOCAL); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5428 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5429 | symtable_node(st, CHILD(n, 3)); |
| 5430 | if (NCH(n) == 5) |
| 5431 | symtable_node(st, CHILD(n, 4)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5432 | } |
| 5433 | |
| 5434 | static void |
| 5435 | symtable_import(struct symtable *st, node *n) |
| 5436 | { |
| 5437 | int i; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5438 | /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)* |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5439 | | 'from' dotted_name 'import' |
| 5440 | ('*' | import_as_name (',' import_as_name)*) |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5441 | import_as_name: NAME [NAME NAME] |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5442 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5443 | if (STR(CHILD(n, 0))[0] == 'f') { /* from */ |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5444 | node *dotname = CHILD(n, 1); |
| 5445 | if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) { |
| 5446 | /* check for bogus imports */ |
| 5447 | if (n->n_lineno >= st->st_future->ff_last_lineno) { |
| 5448 | PyErr_SetString(PyExc_SyntaxError, |
| 5449 | LATE_FUTURE); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5450 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5451 | n->n_lineno); |
| 5452 | st->st_errors++; |
| 5453 | return; |
| 5454 | } |
| 5455 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5456 | if (TYPE(CHILD(n, 3)) == STAR) { |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 5457 | if (st->st_cur->ste_type != TYPE_MODULE) { |
Jeremy Hylton | 6a53bd8 | 2001-08-06 20:34:25 +0000 | [diff] [blame] | 5458 | if (symtable_warn(st, |
| 5459 | "import * only allowed at module level") < 0) |
| 5460 | return; |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 5461 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5462 | st->st_cur->ste_optimized |= OPT_IMPORT_STAR; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5463 | st->st_cur->ste_opt_lineno = n->n_lineno; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5464 | } else { |
| 5465 | for (i = 3; i < NCH(n); i += 2) { |
| 5466 | node *c = CHILD(n, i); |
| 5467 | if (NCH(c) > 1) /* import as */ |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5468 | symtable_assign(st, CHILD(c, 2), |
| 5469 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5470 | else |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5471 | symtable_assign(st, CHILD(c, 0), |
| 5472 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5473 | } |
| 5474 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5475 | } else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5476 | for (i = 1; i < NCH(n); i += 2) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5477 | symtable_assign(st, CHILD(n, i), DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5478 | } |
| 5479 | } |
| 5480 | } |
| 5481 | |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5482 | /* The third argument to symatble_assign() is a flag to be passed to |
| 5483 | symtable_add_def() if it is eventually called. The flag is useful |
| 5484 | to specify the particular type of assignment that should be |
| 5485 | recorded, e.g. an assignment caused by import. |
| 5486 | */ |
| 5487 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5488 | static void |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5489 | symtable_assign(struct symtable *st, node *n, int def_flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5490 | { |
| 5491 | node *tmp; |
| 5492 | int i; |
| 5493 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5494 | loop: |
| 5495 | switch (TYPE(n)) { |
| 5496 | case lambdef: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5497 | /* invalid assignment, e.g. lambda x:x=2. The next |
| 5498 | pass will catch this error. */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5499 | return; |
| 5500 | case power: |
| 5501 | if (NCH(n) > 2) { |
| 5502 | for (i = 2; i < NCH(n); ++i) |
| 5503 | if (TYPE(CHILD(n, i)) != DOUBLESTAR) |
| 5504 | symtable_node(st, CHILD(n, i)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5505 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5506 | if (NCH(n) > 1) { |
| 5507 | symtable_node(st, CHILD(n, 0)); |
| 5508 | symtable_node(st, CHILD(n, 1)); |
| 5509 | } else { |
| 5510 | n = CHILD(n, 0); |
| 5511 | goto loop; |
| 5512 | } |
| 5513 | return; |
| 5514 | case listmaker: |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5515 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
| 5516 | /* XXX This is an error, but the next pass |
| 5517 | will catch it. */ |
| 5518 | return; |
| 5519 | } else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5520 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5521 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5522 | } |
| 5523 | return; |
| 5524 | case exprlist: |
| 5525 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 5526 | case testlist1: |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5527 | if (NCH(n) == 1) { |
| 5528 | n = CHILD(n, 0); |
| 5529 | goto loop; |
| 5530 | } |
| 5531 | else { |
| 5532 | int i; |
| 5533 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5534 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5535 | return; |
| 5536 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5537 | case atom: |
| 5538 | tmp = CHILD(n, 0); |
| 5539 | if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) { |
| 5540 | n = CHILD(n, 1); |
| 5541 | goto loop; |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 5542 | } else if (TYPE(tmp) == NAME) { |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 5543 | if (strcmp(STR(tmp), "__debug__") == 0) { |
| 5544 | PyErr_SetString(PyExc_SyntaxError, |
| 5545 | ASSIGN_DEBUG); |
| 5546 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | 86424e3 | 2001-12-04 02:41:46 +0000 | [diff] [blame] | 5547 | n->n_lineno); |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 5548 | st->st_errors++; |
| 5549 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5550 | symtable_add_def(st, STR(tmp), DEF_LOCAL | def_flag); |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 5551 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5552 | return; |
| 5553 | case dotted_as_name: |
| 5554 | if (NCH(n) == 3) |
| 5555 | symtable_add_def(st, STR(CHILD(n, 2)), |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5556 | DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5557 | else |
| 5558 | symtable_add_def(st, |
| 5559 | STR(CHILD(CHILD(n, |
| 5560 | 0), 0)), |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5561 | DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5562 | return; |
| 5563 | case dotted_name: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5564 | symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5565 | return; |
| 5566 | case NAME: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5567 | symtable_add_def(st, STR(n), DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5568 | return; |
| 5569 | default: |
| 5570 | if (NCH(n) == 0) |
| 5571 | return; |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 5572 | if (NCH(n) == 1) { |
| 5573 | n = CHILD(n, 0); |
| 5574 | goto loop; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5575 | } |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 5576 | /* Should only occur for errors like x + 1 = 1, |
| 5577 | which will be caught in the next pass. */ |
| 5578 | for (i = 0; i < NCH(n); ++i) |
| 5579 | if (TYPE(CHILD(n, i)) >= single_input) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5580 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5581 | } |
| 5582 | } |