Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1 | /* Compile an expression node to intermediate code */ |
| 2 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3 | /* XXX TO DO: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4 | XXX add __doc__ attribute == co_doc to code object attributes? |
| 5 | XXX (it's currently the first item of the co_const tuple) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 6 | XXX Generate simple jump for break/return outside 'try...finally' |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 7 | XXX Allow 'continue' inside finally clause of try-finally |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 8 | XXX New opcode for loading the initial index for a for loop |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 9 | XXX other JAR tricks? |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 10 | */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 12 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 14 | #include "node.h" |
| 15 | #include "token.h" |
| 16 | #include "graminit.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 17 | #include "compile.h" |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 18 | #include "symtable.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 19 | #include "opcode.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 20 | #include "structmember.h" |
| 21 | |
| 22 | #include <ctype.h> |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 24 | /* Three symbols from graminit.h are also defined in Python.h, with |
| 25 | Py_ prefixes to their names. Python.h can't include graminit.h |
| 26 | (which defines too many confusing symbols), but we can check here |
| 27 | that they haven't changed (which is very unlikely, but possible). */ |
| 28 | #if Py_single_input != single_input |
Guido van Rossum | 2f75b29 | 1997-05-20 22:18:48 +0000 | [diff] [blame] | 29 | #error "single_input has changed -- update Py_single_input in Python.h" |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 30 | #endif |
| 31 | #if Py_file_input != file_input |
Guido van Rossum | 2f75b29 | 1997-05-20 22:18:48 +0000 | [diff] [blame] | 32 | #error "file_input has changed -- update Py_file_input in Python.h" |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 33 | #endif |
| 34 | #if Py_eval_input != eval_input |
Guido van Rossum | 2f75b29 | 1997-05-20 22:18:48 +0000 | [diff] [blame] | 35 | #error "eval_input has changed -- update Py_eval_input in Python.h" |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 36 | #endif |
| 37 | |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 38 | int Py_OptimizeFlag = 0; |
| 39 | |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 40 | #define OP_DELETE 0 |
| 41 | #define OP_ASSIGN 1 |
| 42 | #define OP_APPLY 2 |
| 43 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 44 | #define VAR_LOAD 0 |
| 45 | #define VAR_STORE 1 |
| 46 | #define VAR_DELETE 2 |
| 47 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 48 | #define DEL_CLOSURE_ERROR \ |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 49 | "can not delete variable '%.400s' referenced in nested scope" |
| 50 | |
| 51 | #define DUPLICATE_ARGUMENT \ |
| 52 | "duplicate argument '%s' in function definition" |
| 53 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 54 | #define ILLEGAL_DYNAMIC_SCOPE \ |
| 55 | "%.100s: exec or 'import *' makes names ambiguous in nested scope" |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 56 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 57 | #define GLOBAL_AFTER_ASSIGN \ |
| 58 | "name '%.400s' is assigned to before global declaration" |
| 59 | |
| 60 | #define GLOBAL_AFTER_USE \ |
| 61 | "name '%.400s' is used prior to global declaration" |
| 62 | |
| 63 | #define LOCAL_GLOBAL \ |
Neal Norwitz | 2a47c0f | 2002-01-29 00:53:41 +0000 | [diff] [blame] | 64 | "name '%.400s' is a function parameter and declared global" |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 65 | |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 66 | #define LATE_FUTURE \ |
| 67 | "from __future__ imports must occur at the beginning of the file" |
| 68 | |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 69 | #define ASSIGN_DEBUG \ |
| 70 | "can not assign to __debug__" |
| 71 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 72 | #define MANGLE_LEN 256 |
| 73 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 74 | #define OFF(x) offsetof(PyCodeObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 76 | static PyMemberDef code_memberlist[] = { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 77 | {"co_argcount", T_INT, OFF(co_argcount), READONLY}, |
| 78 | {"co_nlocals", T_INT, OFF(co_nlocals), READONLY}, |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 79 | {"co_stacksize",T_INT, OFF(co_stacksize), READONLY}, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 80 | {"co_flags", T_INT, OFF(co_flags), READONLY}, |
Guido van Rossum | 39d942d | 1992-01-12 02:30:05 +0000 | [diff] [blame] | 81 | {"co_code", T_OBJECT, OFF(co_code), READONLY}, |
| 82 | {"co_consts", T_OBJECT, OFF(co_consts), READONLY}, |
| 83 | {"co_names", T_OBJECT, OFF(co_names), READONLY}, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 84 | {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY}, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 85 | {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY}, |
| 86 | {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY}, |
Guido van Rossum | 39d942d | 1992-01-12 02:30:05 +0000 | [diff] [blame] | 87 | {"co_filename", T_OBJECT, OFF(co_filename), READONLY}, |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 88 | {"co_name", T_OBJECT, OFF(co_name), READONLY}, |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 89 | {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY}, |
| 90 | {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 91 | {NULL} /* Sentinel */ |
| 92 | }; |
| 93 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 94 | PyDoc_STRVAR(code_doc, |
| 95 | "code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\ |
| 96 | varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\ |
| 97 | \n\ |
| 98 | Create a code object. Not for the faint of heart."); |
| 99 | |
| 100 | static PyObject * |
| 101 | code_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 102 | { |
| 103 | int argcount; |
| 104 | int nlocals; |
| 105 | int stacksize; |
| 106 | int flags; |
| 107 | PyObject *code; |
| 108 | PyObject *consts; |
| 109 | PyObject *names; |
| 110 | PyObject *varnames; |
| 111 | PyObject *freevars = NULL; |
| 112 | PyObject *cellvars = NULL; |
| 113 | PyObject *filename; |
| 114 | PyObject *name; |
| 115 | int firstlineno; |
| 116 | PyObject *lnotab; |
| 117 | |
| 118 | if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code", |
| 119 | &argcount, &nlocals, &stacksize, &flags, |
| 120 | &code, |
| 121 | &PyTuple_Type, &consts, |
| 122 | &PyTuple_Type, &names, |
| 123 | &PyTuple_Type, &varnames, |
| 124 | &filename, &name, |
| 125 | &firstlineno, &lnotab, |
| 126 | &PyTuple_Type, &freevars, |
| 127 | &PyTuple_Type, &cellvars)) |
| 128 | return NULL; |
| 129 | |
| 130 | if (freevars == NULL || cellvars == NULL) { |
| 131 | PyObject *empty = PyTuple_New(0); |
| 132 | if (empty == NULL) |
| 133 | return NULL; |
| 134 | if (freevars == NULL) { |
| 135 | freevars = empty; |
| 136 | Py_INCREF(freevars); |
| 137 | } |
| 138 | if (cellvars == NULL) { |
| 139 | cellvars = empty; |
| 140 | Py_INCREF(cellvars); |
| 141 | } |
| 142 | Py_DECREF(empty); |
| 143 | } |
| 144 | |
| 145 | if (!PyObject_CheckReadBuffer(code)) { |
| 146 | PyErr_SetString(PyExc_TypeError, |
| 147 | "bytecode object must be a single-segment read-only buffer"); |
| 148 | return NULL; |
| 149 | } |
| 150 | |
| 151 | return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags, |
| 152 | code, consts, names, varnames, |
| 153 | freevars, cellvars, filename, name, |
| 154 | firstlineno, lnotab); |
| 155 | } |
| 156 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 157 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 158 | code_dealloc(PyCodeObject *co) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 159 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 160 | Py_XDECREF(co->co_code); |
| 161 | Py_XDECREF(co->co_consts); |
| 162 | Py_XDECREF(co->co_names); |
Guido van Rossum | 275558c | 1997-07-25 20:13:49 +0000 | [diff] [blame] | 163 | Py_XDECREF(co->co_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 164 | Py_XDECREF(co->co_freevars); |
| 165 | Py_XDECREF(co->co_cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 166 | Py_XDECREF(co->co_filename); |
| 167 | Py_XDECREF(co->co_name); |
Guido van Rossum | 275558c | 1997-07-25 20:13:49 +0000 | [diff] [blame] | 168 | Py_XDECREF(co->co_lnotab); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 169 | PyObject_DEL(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 172 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 173 | code_repr(PyCodeObject *co) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 174 | { |
| 175 | char buf[500]; |
| 176 | int lineno = -1; |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 177 | char *filename = "???"; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 178 | char *name = "???"; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 179 | |
Guido van Rossum | a396a88 | 2000-04-07 01:21:36 +0000 | [diff] [blame] | 180 | if (co->co_firstlineno != 0) |
| 181 | lineno = co->co_firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 182 | if (co->co_filename && PyString_Check(co->co_filename)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 183 | filename = PyString_AS_STRING(co->co_filename); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 184 | if (co->co_name && PyString_Check(co->co_name)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 185 | name = PyString_AS_STRING(co->co_name); |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 186 | PyOS_snprintf(buf, sizeof(buf), |
| 187 | "<code object %.100s at %p, file \"%.300s\", line %d>", |
| 188 | name, co, filename, lineno); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 189 | return PyString_FromString(buf); |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 192 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 193 | code_compare(PyCodeObject *co, PyCodeObject *cp) |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 194 | { |
| 195 | int cmp; |
Guido van Rossum | 4467959 | 2000-04-10 16:20:31 +0000 | [diff] [blame] | 196 | cmp = PyObject_Compare(co->co_name, cp->co_name); |
| 197 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 198 | cmp = co->co_argcount - cp->co_argcount; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 199 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 200 | cmp = co->co_nlocals - cp->co_nlocals; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 201 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 202 | cmp = co->co_flags - cp->co_flags; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 203 | if (cmp) return cmp; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 204 | cmp = PyObject_Compare(co->co_code, cp->co_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 205 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 206 | cmp = PyObject_Compare(co->co_consts, cp->co_consts); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 207 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 208 | cmp = PyObject_Compare(co->co_names, cp->co_names); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 209 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 210 | cmp = PyObject_Compare(co->co_varnames, cp->co_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 211 | if (cmp) return cmp; |
| 212 | cmp = PyObject_Compare(co->co_freevars, cp->co_freevars); |
| 213 | if (cmp) return cmp; |
| 214 | cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 215 | return cmp; |
| 216 | } |
| 217 | |
| 218 | static long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 219 | code_hash(PyCodeObject *co) |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 220 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 221 | long h, h0, h1, h2, h3, h4, h5, h6; |
Guido van Rossum | 4467959 | 2000-04-10 16:20:31 +0000 | [diff] [blame] | 222 | h0 = PyObject_Hash(co->co_name); |
| 223 | if (h0 == -1) return -1; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 224 | h1 = PyObject_Hash(co->co_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 225 | if (h1 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 226 | h2 = PyObject_Hash(co->co_consts); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 227 | if (h2 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 228 | h3 = PyObject_Hash(co->co_names); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 229 | if (h3 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 230 | h4 = PyObject_Hash(co->co_varnames); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 231 | if (h4 == -1) return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 232 | h5 = PyObject_Hash(co->co_freevars); |
| 233 | if (h5 == -1) return -1; |
| 234 | h6 = PyObject_Hash(co->co_cellvars); |
| 235 | if (h6 == -1) return -1; |
| 236 | h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 237 | co->co_argcount ^ co->co_nlocals ^ co->co_flags; |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 238 | if (h == -1) h = -2; |
| 239 | return h; |
| 240 | } |
| 241 | |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 242 | /* XXX code objects need to participate in GC? */ |
| 243 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 244 | PyTypeObject PyCode_Type = { |
| 245 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 246 | 0, |
| 247 | "code", |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 248 | sizeof(PyCodeObject), |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 249 | 0, |
Jeremy Hylton | c785f48 | 2001-09-14 20:08:07 +0000 | [diff] [blame] | 250 | (destructor)code_dealloc, /* tp_dealloc */ |
| 251 | 0, /* tp_print */ |
| 252 | 0, /* tp_getattr */ |
| 253 | 0, /* tp_setattr */ |
| 254 | (cmpfunc)code_compare, /* tp_compare */ |
| 255 | (reprfunc)code_repr, /* tp_repr */ |
| 256 | 0, /* tp_as_number */ |
| 257 | 0, /* tp_as_sequence */ |
| 258 | 0, /* tp_as_mapping */ |
| 259 | (hashfunc)code_hash, /* tp_hash */ |
| 260 | 0, /* tp_call */ |
| 261 | 0, /* tp_str */ |
| 262 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 263 | 0, /* tp_setattro */ |
| 264 | 0, /* tp_as_buffer */ |
| 265 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 266 | code_doc, /* tp_doc */ |
Jeremy Hylton | c785f48 | 2001-09-14 20:08:07 +0000 | [diff] [blame] | 267 | 0, /* tp_traverse */ |
| 268 | 0, /* tp_clear */ |
| 269 | 0, /* tp_richcompare */ |
| 270 | 0, /* tp_weaklistoffset */ |
| 271 | 0, /* tp_iter */ |
| 272 | 0, /* tp_iternext */ |
| 273 | 0, /* tp_methods */ |
| 274 | code_memberlist, /* tp_members */ |
| 275 | 0, /* tp_getset */ |
| 276 | 0, /* tp_base */ |
| 277 | 0, /* tp_dict */ |
| 278 | 0, /* tp_descr_get */ |
| 279 | 0, /* tp_descr_set */ |
| 280 | 0, /* tp_dictoffset */ |
| 281 | 0, /* tp_init */ |
| 282 | 0, /* tp_alloc */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 283 | code_new, /* tp_new */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 284 | }; |
| 285 | |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 286 | #define NAME_CHARS \ |
| 287 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" |
| 288 | |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 289 | /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */ |
| 290 | |
| 291 | static int |
| 292 | all_name_chars(unsigned char *s) |
| 293 | { |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 294 | static char ok_name_char[256]; |
| 295 | static unsigned char *name_chars = (unsigned char *)NAME_CHARS; |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 296 | |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 297 | if (ok_name_char[*name_chars] == 0) { |
| 298 | unsigned char *p; |
| 299 | for (p = name_chars; *p; p++) |
| 300 | ok_name_char[*p] = 1; |
| 301 | } |
| 302 | while (*s) { |
| 303 | if (ok_name_char[*s++] == 0) |
| 304 | return 0; |
| 305 | } |
| 306 | return 1; |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 309 | static int |
| 310 | intern_strings(PyObject *tuple) |
| 311 | { |
| 312 | int i; |
| 313 | |
| 314 | for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { |
| 315 | PyObject *v = PyTuple_GET_ITEM(tuple, i); |
| 316 | if (v == NULL || !PyString_Check(v)) { |
| 317 | Py_FatalError("non-string found in code slot"); |
| 318 | PyErr_BadInternalCall(); |
| 319 | return -1; |
| 320 | } |
| 321 | PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i)); |
| 322 | } |
| 323 | return 0; |
| 324 | } |
| 325 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 326 | PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 327 | PyCode_New(int argcount, int nlocals, int stacksize, int flags, |
| 328 | PyObject *code, PyObject *consts, PyObject *names, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 329 | PyObject *varnames, PyObject *freevars, PyObject *cellvars, |
| 330 | PyObject *filename, PyObject *name, int firstlineno, |
| 331 | PyObject *lnotab) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 332 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 333 | PyCodeObject *co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 334 | int i; |
| 335 | /* Check argument types */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 336 | if (argcount < 0 || nlocals < 0 || |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 337 | code == NULL || |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 338 | consts == NULL || !PyTuple_Check(consts) || |
| 339 | names == NULL || !PyTuple_Check(names) || |
| 340 | varnames == NULL || !PyTuple_Check(varnames) || |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 341 | freevars == NULL || !PyTuple_Check(freevars) || |
| 342 | cellvars == NULL || !PyTuple_Check(cellvars) || |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 343 | name == NULL || !PyString_Check(name) || |
| 344 | filename == NULL || !PyString_Check(filename) || |
Jeremy Hylton | 9f64caa | 2001-11-09 22:02:48 +0000 | [diff] [blame] | 345 | lnotab == NULL || !PyString_Check(lnotab) || |
| 346 | !PyObject_CheckReadBuffer(code)) { |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 347 | PyErr_BadInternalCall(); |
| 348 | return NULL; |
| 349 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 350 | intern_strings(names); |
| 351 | intern_strings(varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 352 | intern_strings(freevars); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 353 | intern_strings(cellvars); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 354 | /* Intern selected string constants */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 355 | for (i = PyTuple_Size(consts); --i >= 0; ) { |
| 356 | PyObject *v = PyTuple_GetItem(consts, i); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 357 | if (!PyString_Check(v)) |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 358 | continue; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 359 | if (!all_name_chars((unsigned char *)PyString_AS_STRING(v))) |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 360 | continue; |
| 361 | PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 362 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 363 | co = PyObject_NEW(PyCodeObject, &PyCode_Type); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 364 | if (co != NULL) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 365 | co->co_argcount = argcount; |
| 366 | co->co_nlocals = nlocals; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 367 | co->co_stacksize = stacksize; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 368 | co->co_flags = flags; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 369 | Py_INCREF(code); |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 370 | co->co_code = code; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 371 | Py_INCREF(consts); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 372 | co->co_consts = consts; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 373 | Py_INCREF(names); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 374 | co->co_names = names; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 375 | Py_INCREF(varnames); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 376 | co->co_varnames = varnames; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 377 | Py_INCREF(freevars); |
| 378 | co->co_freevars = freevars; |
| 379 | Py_INCREF(cellvars); |
| 380 | co->co_cellvars = cellvars; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 381 | Py_INCREF(filename); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 382 | co->co_filename = filename; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 383 | Py_INCREF(name); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 384 | co->co_name = name; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 385 | co->co_firstlineno = firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 386 | Py_INCREF(lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 387 | co->co_lnotab = lnotab; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 388 | } |
| 389 | return co; |
| 390 | } |
| 391 | |
| 392 | |
| 393 | /* Data structure used internally */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 394 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 395 | /* The compiler uses two passes to generate bytecodes. The first pass |
| 396 | builds the symbol table. The second pass generates the bytecode. |
| 397 | |
| 398 | The first pass uses a single symtable struct. The second pass uses |
| 399 | a compiling struct for each code block. The compiling structs |
| 400 | share a reference to the symtable. |
| 401 | |
| 402 | The two passes communicate via symtable_load_symbols() and via |
| 403 | is_local() and is_global(). The former initializes several slots |
| 404 | in the compiling struct: c_varnames, c_locals, c_nlocals, |
| 405 | c_argcount, c_globals, and c_flags. |
| 406 | */ |
| 407 | |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 408 | /* All about c_lnotab. |
| 409 | |
| 410 | c_lnotab is an array of unsigned bytes disguised as a Python string. In -O |
| 411 | mode, SET_LINENO opcodes aren't generated, and bytecode offsets are mapped |
| 412 | to source code line #s (when needed for tracebacks) via c_lnotab instead. |
| 413 | The array is conceptually a list of |
| 414 | (bytecode offset increment, line number increment) |
| 415 | pairs. The details are important and delicate, best illustrated by example: |
| 416 | |
| 417 | byte code offset source code line number |
| 418 | 0 1 |
| 419 | 6 2 |
| 420 | 50 7 |
| 421 | 350 307 |
| 422 | 361 308 |
| 423 | |
| 424 | The first trick is that these numbers aren't stored, only the increments |
| 425 | from one row to the next (this doesn't really work, but it's a start): |
| 426 | |
| 427 | 0, 1, 6, 1, 44, 5, 300, 300, 11, 1 |
| 428 | |
| 429 | The second trick is that an unsigned byte can't hold negative values, or |
| 430 | values larger than 255, so (a) there's a deep assumption that byte code |
| 431 | offsets and their corresponding line #s both increase monotonically, and (b) |
| 432 | if at least one column jumps by more than 255 from one row to the next, more |
| 433 | than one pair is written to the table. In case #b, there's no way to know |
| 434 | from looking at the table later how many were written. That's the delicate |
| 435 | part. A user of c_lnotab desiring to find the source line number |
| 436 | corresponding to a bytecode address A should do something like this |
| 437 | |
| 438 | lineno = addr = 0 |
| 439 | for addr_incr, line_incr in c_lnotab: |
| 440 | addr += addr_incr |
| 441 | if addr > A: |
| 442 | return lineno |
| 443 | lineno += line_incr |
| 444 | |
| 445 | In order for this to work, when the addr field increments by more than 255, |
| 446 | the line # increment in each pair generated must be 0 until the remaining addr |
| 447 | increment is < 256. So, in the example above, com_set_lineno should not (as |
| 448 | was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to |
| 449 | 255, 0, 45, 255, 0, 45. |
| 450 | */ |
| 451 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 452 | struct compiling { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 453 | PyObject *c_code; /* string */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 454 | PyObject *c_consts; /* list of objects */ |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 455 | PyObject *c_const_dict; /* inverse of c_consts */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 456 | PyObject *c_names; /* list of strings (names) */ |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 457 | PyObject *c_name_dict; /* inverse of c_names */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 458 | PyObject *c_globals; /* dictionary (value=None) */ |
| 459 | PyObject *c_locals; /* dictionary (value=localID) */ |
| 460 | PyObject *c_varnames; /* list (inverse of c_locals) */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 461 | PyObject *c_freevars; /* dictionary (value=None) */ |
| 462 | PyObject *c_cellvars; /* list */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 463 | int c_nlocals; /* index of next local */ |
| 464 | int c_argcount; /* number of top-level arguments */ |
| 465 | int c_flags; /* same as co_flags */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 466 | int c_nexti; /* index into c_code */ |
| 467 | int c_errors; /* counts errors occurred */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 468 | int c_infunction; /* set when compiling a function */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 469 | int c_interactive; /* generating code for interactive command */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 470 | int c_loops; /* counts nested loops */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 471 | int c_begin; /* begin of current loop, for 'continue' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 472 | int c_block[CO_MAXBLOCKS]; /* stack of block types */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 473 | int c_nblocks; /* current block stack level */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 474 | char *c_filename; /* filename of current node */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 475 | char *c_name; /* name of object (e.g. function) */ |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 476 | int c_lineno; /* Current line number */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 477 | int c_stacklevel; /* Current stack level */ |
| 478 | int c_maxstacklevel; /* Maximum stack level */ |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 479 | int c_firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 480 | PyObject *c_lnotab; /* Table mapping address to line number */ |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 481 | int c_last_addr, c_last_line, c_lnotab_next; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 482 | char *c_private; /* for private name mangling */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 483 | int c_tmpname; /* temporary local name counter */ |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 484 | int c_nested; /* Is block nested funcdef or lamdef? */ |
| 485 | int c_closure; /* Is nested w/freevars? */ |
| 486 | struct symtable *c_symtable; /* pointer to module symbol table */ |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 487 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 488 | char *c_encoding; /* source encoding (a borrowed reference) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 489 | }; |
| 490 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 491 | static int |
| 492 | is_free(int v) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 493 | { |
| 494 | if ((v & (USE | DEF_FREE)) |
| 495 | && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL))) |
| 496 | return 1; |
| 497 | if (v & DEF_FREE_CLASS) |
| 498 | return 1; |
| 499 | return 0; |
| 500 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 501 | |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 502 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 503 | com_error(struct compiling *c, PyObject *exc, char *msg) |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 504 | { |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 505 | PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL; |
| 506 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 507 | if (c == NULL) { |
| 508 | /* Error occurred via symtable call to |
| 509 | is_constant_false */ |
| 510 | PyErr_SetString(exc, msg); |
| 511 | return; |
| 512 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 513 | c->c_errors++; |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 514 | if (c->c_lineno < 1 || c->c_interactive) { |
| 515 | /* Unknown line number or interactive input */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 516 | PyErr_SetString(exc, msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 517 | return; |
| 518 | } |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 519 | v = PyString_FromString(msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 520 | if (v == NULL) |
| 521 | return; /* MemoryError, too bad */ |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 522 | |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 523 | line = PyErr_ProgramText(c->c_filename, c->c_lineno); |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 524 | if (line == NULL) { |
| 525 | Py_INCREF(Py_None); |
| 526 | line = Py_None; |
| 527 | } |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 528 | if (exc == PyExc_SyntaxError) { |
| 529 | t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno, |
| 530 | Py_None, line); |
| 531 | if (t == NULL) |
| 532 | goto exit; |
| 533 | w = Py_BuildValue("(OO)", v, t); |
| 534 | if (w == NULL) |
| 535 | goto exit; |
| 536 | PyErr_SetObject(exc, w); |
| 537 | } else { |
| 538 | /* Make sure additional exceptions are printed with |
| 539 | file and line, also. */ |
| 540 | PyErr_SetObject(exc, v); |
| 541 | PyErr_SyntaxLocation(c->c_filename, c->c_lineno); |
| 542 | } |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 543 | exit: |
| 544 | Py_XDECREF(t); |
| 545 | Py_XDECREF(v); |
| 546 | Py_XDECREF(w); |
| 547 | Py_XDECREF(line); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 550 | /* Interface to the block stack */ |
| 551 | |
| 552 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 553 | block_push(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 554 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 555 | if (c->c_nblocks >= CO_MAXBLOCKS) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 556 | com_error(c, PyExc_SystemError, |
| 557 | "too many statically nested blocks"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 558 | } |
| 559 | else { |
| 560 | c->c_block[c->c_nblocks++] = type; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 565 | block_pop(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 566 | { |
| 567 | if (c->c_nblocks > 0) |
| 568 | c->c_nblocks--; |
| 569 | if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 570 | com_error(c, PyExc_SystemError, "bad block pop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 574 | /* Prototype forward declarations */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 575 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 576 | static int com_init(struct compiling *, char *); |
| 577 | static void com_free(struct compiling *); |
| 578 | static void com_push(struct compiling *, int); |
| 579 | static void com_pop(struct compiling *, int); |
| 580 | static void com_done(struct compiling *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 581 | static void com_node(struct compiling *, node *); |
| 582 | static void com_factor(struct compiling *, node *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 583 | static void com_addbyte(struct compiling *, int); |
| 584 | static void com_addint(struct compiling *, int); |
| 585 | static void com_addoparg(struct compiling *, int, int); |
| 586 | static void com_addfwref(struct compiling *, int, int *); |
| 587 | static void com_backpatch(struct compiling *, int); |
| 588 | static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *); |
| 589 | static int com_addconst(struct compiling *, PyObject *); |
| 590 | static int com_addname(struct compiling *, PyObject *); |
| 591 | static void com_addopname(struct compiling *, int, node *); |
| 592 | static void com_list(struct compiling *, node *, int); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 593 | static void com_list_iter(struct compiling *, node *, node *, char *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 594 | static int com_argdefs(struct compiling *, node *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 595 | static void com_assign(struct compiling *, node *, int, node *); |
| 596 | static void com_assign_name(struct compiling *, node *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 597 | static PyCodeObject *icompile(node *, struct compiling *); |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 598 | static PyCodeObject *jcompile(node *, char *, struct compiling *, |
| 599 | PyCompilerFlags *); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 600 | static PyObject *parsestrplus(struct compiling*, node *); |
| 601 | static PyObject *parsestr(struct compiling *, char *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 602 | static node *get_rawdocstring(node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 603 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 604 | static int get_ref_type(struct compiling *, char *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 605 | |
| 606 | /* symtable operations */ |
| 607 | static int symtable_build(struct compiling *, node *); |
| 608 | static int symtable_load_symbols(struct compiling *); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 609 | static struct symtable *symtable_init(void); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 610 | static void symtable_enter_scope(struct symtable *, char *, int, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 611 | static int symtable_exit_scope(struct symtable *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 612 | static int symtable_add_def(struct symtable *, char *, int); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 613 | static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 614 | |
| 615 | static void symtable_node(struct symtable *, node *); |
| 616 | static void symtable_funcdef(struct symtable *, node *); |
| 617 | static void symtable_default_args(struct symtable *, node *); |
| 618 | static void symtable_params(struct symtable *, node *); |
| 619 | static void symtable_params_fplist(struct symtable *, node *n); |
| 620 | static void symtable_global(struct symtable *, node *); |
| 621 | static void symtable_import(struct symtable *, node *); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 622 | static void symtable_assign(struct symtable *, node *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 623 | static void symtable_list_comprehension(struct symtable *, node *); |
| 624 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 625 | static int symtable_update_free_vars(struct symtable *); |
| 626 | static int symtable_undo_free(struct symtable *, PyObject *, PyObject *); |
| 627 | static int symtable_check_global(struct symtable *, PyObject *, PyObject *); |
| 628 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 629 | /* helper */ |
| 630 | static void |
| 631 | do_pad(int pad) |
| 632 | { |
| 633 | int i; |
| 634 | for (i = 0; i < pad; ++i) |
| 635 | fprintf(stderr, " "); |
| 636 | } |
| 637 | |
| 638 | static void |
| 639 | dump(node *n, int pad, int depth) |
| 640 | { |
| 641 | int i; |
| 642 | if (depth == 0) |
| 643 | return; |
| 644 | do_pad(pad); |
| 645 | fprintf(stderr, "%d: %s\n", TYPE(n), STR(n)); |
| 646 | if (depth > 0) |
| 647 | depth--; |
| 648 | for (i = 0; i < NCH(n); ++i) |
| 649 | dump(CHILD(n, i), pad + 1, depth); |
| 650 | } |
| 651 | |
| 652 | #define DUMP(N) dump(N, 0, -1) |
| 653 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 654 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 655 | com_init(struct compiling *c, char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 656 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 657 | memset((void *)c, '\0', sizeof(struct compiling)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 658 | if ((c->c_code = PyString_FromStringAndSize((char *)NULL, |
| 659 | 1000)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 660 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 661 | if ((c->c_consts = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 662 | goto fail; |
| 663 | if ((c->c_const_dict = PyDict_New()) == NULL) |
| 664 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 665 | if ((c->c_names = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 666 | goto fail; |
| 667 | if ((c->c_name_dict = PyDict_New()) == NULL) |
| 668 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 669 | if ((c->c_locals = PyDict_New()) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 670 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 671 | if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL, |
| 672 | 1000)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 673 | goto fail; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 674 | c->c_globals = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 675 | c->c_varnames = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 676 | c->c_freevars = NULL; |
| 677 | c->c_cellvars = NULL; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 678 | c->c_nlocals = 0; |
| 679 | c->c_argcount = 0; |
| 680 | c->c_flags = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 681 | c->c_nexti = 0; |
| 682 | c->c_errors = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 683 | c->c_infunction = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 684 | c->c_interactive = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 685 | c->c_loops = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 686 | c->c_begin = 0; |
| 687 | c->c_nblocks = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 688 | c->c_filename = filename; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 689 | c->c_name = "?"; |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 690 | c->c_lineno = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 691 | c->c_stacklevel = 0; |
| 692 | c->c_maxstacklevel = 0; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 693 | c->c_firstlineno = 0; |
| 694 | c->c_last_addr = 0; |
| 695 | c->c_last_line = 0; |
Barry Warsaw | 174e801 | 2001-01-22 04:35:57 +0000 | [diff] [blame] | 696 | c->c_lnotab_next = 0; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 697 | c->c_tmpname = 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 698 | c->c_nested = 0; |
| 699 | c->c_closure = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 700 | c->c_symtable = NULL; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 701 | return 1; |
| 702 | |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 703 | fail: |
| 704 | com_free(c); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 709 | com_free(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 710 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 711 | Py_XDECREF(c->c_code); |
| 712 | Py_XDECREF(c->c_consts); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 713 | Py_XDECREF(c->c_const_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 714 | Py_XDECREF(c->c_names); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 715 | Py_XDECREF(c->c_name_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 716 | Py_XDECREF(c->c_globals); |
| 717 | Py_XDECREF(c->c_locals); |
| 718 | Py_XDECREF(c->c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 719 | Py_XDECREF(c->c_freevars); |
| 720 | Py_XDECREF(c->c_cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 721 | Py_XDECREF(c->c_lnotab); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 722 | if (c->c_future) |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 723 | PyObject_FREE((void *)c->c_future); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 727 | com_push(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 728 | { |
| 729 | c->c_stacklevel += n; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 730 | if (c->c_stacklevel > c->c_maxstacklevel) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 731 | c->c_maxstacklevel = c->c_stacklevel; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 732 | /* |
| 733 | fprintf(stderr, "%s:%s:%d max stack nexti=%d level=%d n=%d\n", |
| 734 | c->c_filename, c->c_name, c->c_lineno, |
| 735 | c->c_nexti, c->c_stacklevel, n); |
| 736 | */ |
| 737 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 741 | com_pop(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 742 | { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 743 | if (c->c_stacklevel < n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 744 | c->c_stacklevel = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 745 | else |
| 746 | c->c_stacklevel -= n; |
| 747 | } |
| 748 | |
| 749 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 750 | com_done(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 751 | { |
| 752 | if (c->c_code != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 753 | _PyString_Resize(&c->c_code, c->c_nexti); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 754 | if (c->c_lnotab != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 755 | _PyString_Resize(&c->c_lnotab, c->c_lnotab_next); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 758 | static int |
| 759 | com_check_size(PyObject **s, int offset) |
| 760 | { |
| 761 | int len = PyString_GET_SIZE(*s); |
| 762 | if (offset >= len) |
| 763 | return _PyString_Resize(s, len * 2); |
| 764 | return 0; |
| 765 | } |
| 766 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 767 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 768 | com_addbyte(struct compiling *c, int byte) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 769 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 770 | /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 771 | assert(byte >= 0 && byte <= 255); |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 772 | assert(c->c_code != 0); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 773 | if (com_check_size(&c->c_code, c->c_nexti)) { |
| 774 | c->c_errors++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 775 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 776 | } |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 777 | PyString_AS_STRING(c->c_code)[c->c_nexti++] = byte; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 781 | com_addint(struct compiling *c, int x) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 782 | { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 783 | com_addbyte(c, x & 0xff); |
| 784 | com_addbyte(c, x >> 8); /* XXX x should be positive */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 788 | com_add_lnotab(struct compiling *c, int addr, int line) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 789 | { |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 790 | char *p; |
| 791 | if (c->c_lnotab == NULL) |
| 792 | return; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 793 | if (com_check_size(&c->c_lnotab, c->c_lnotab_next + 2)) { |
| 794 | c->c_errors++; |
| 795 | return; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 796 | } |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 797 | p = PyString_AS_STRING(c->c_lnotab) + c->c_lnotab_next; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 798 | *p++ = addr; |
| 799 | *p++ = line; |
| 800 | c->c_lnotab_next += 2; |
| 801 | } |
| 802 | |
| 803 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 804 | com_set_lineno(struct compiling *c, int lineno) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 805 | { |
| 806 | c->c_lineno = lineno; |
| 807 | if (c->c_firstlineno == 0) { |
| 808 | c->c_firstlineno = c->c_last_line = lineno; |
| 809 | } |
| 810 | else { |
| 811 | int incr_addr = c->c_nexti - c->c_last_addr; |
| 812 | int incr_line = lineno - c->c_last_line; |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 813 | while (incr_addr > 255) { |
| 814 | com_add_lnotab(c, 255, 0); |
| 815 | incr_addr -= 255; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 816 | } |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 817 | while (incr_line > 255) { |
| 818 | com_add_lnotab(c, incr_addr, 255); |
| 819 | incr_line -=255; |
| 820 | incr_addr = 0; |
| 821 | } |
| 822 | if (incr_addr > 0 || incr_line > 0) |
| 823 | com_add_lnotab(c, incr_addr, incr_line); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 824 | c->c_last_addr = c->c_nexti; |
| 825 | c->c_last_line = lineno; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 830 | com_addoparg(struct compiling *c, int op, int arg) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 831 | { |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 832 | int extended_arg = arg >> 16; |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 833 | if (op == SET_LINENO) { |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 834 | com_set_lineno(c, arg); |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 835 | if (Py_OptimizeFlag) |
| 836 | return; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 837 | } |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 838 | if (extended_arg){ |
| 839 | com_addbyte(c, EXTENDED_ARG); |
| 840 | com_addint(c, extended_arg); |
| 841 | arg &= 0xffff; |
| 842 | } |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 843 | com_addbyte(c, op); |
| 844 | com_addint(c, arg); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 848 | com_addfwref(struct compiling *c, int op, int *p_anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 849 | { |
| 850 | /* Compile a forward reference for backpatching */ |
| 851 | int here; |
| 852 | int anchor; |
| 853 | com_addbyte(c, op); |
| 854 | here = c->c_nexti; |
| 855 | anchor = *p_anchor; |
| 856 | *p_anchor = here; |
| 857 | com_addint(c, anchor == 0 ? 0 : here - anchor); |
| 858 | } |
| 859 | |
| 860 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 861 | com_backpatch(struct compiling *c, int anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 862 | { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 863 | unsigned char *code = (unsigned char *) PyString_AS_STRING(c->c_code); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 864 | int target = c->c_nexti; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 865 | int dist; |
| 866 | int prev; |
| 867 | for (;;) { |
| 868 | /* Make the JUMP instruction at anchor point to target */ |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 869 | prev = code[anchor] + (code[anchor+1] << 8); |
| 870 | dist = target - (anchor+2); |
| 871 | code[anchor] = dist & 0xff; |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 872 | dist >>= 8; |
| 873 | code[anchor+1] = dist; |
| 874 | dist >>= 8; |
| 875 | if (dist) { |
| 876 | com_error(c, PyExc_SystemError, |
| 877 | "com_backpatch: offset too large"); |
| 878 | break; |
| 879 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 880 | if (!prev) |
| 881 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 882 | anchor -= prev; |
| 883 | } |
| 884 | } |
| 885 | |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 886 | /* Handle literals and names uniformly */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 887 | |
| 888 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 889 | com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 890 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 891 | PyObject *w, *t, *np=NULL; |
| 892 | long n; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 893 | |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 894 | t = Py_BuildValue("(OO)", v, v->ob_type); |
| 895 | if (t == NULL) |
| 896 | goto fail; |
| 897 | w = PyDict_GetItem(dict, t); |
| 898 | if (w != NULL) { |
| 899 | n = PyInt_AsLong(w); |
| 900 | } else { |
| 901 | n = PyList_Size(list); |
| 902 | np = PyInt_FromLong(n); |
| 903 | if (np == NULL) |
| 904 | goto fail; |
| 905 | if (PyList_Append(list, v) != 0) |
| 906 | goto fail; |
| 907 | if (PyDict_SetItem(dict, t, np) != 0) |
| 908 | goto fail; |
| 909 | Py_DECREF(np); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 910 | } |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 911 | Py_DECREF(t); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 912 | return n; |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 913 | fail: |
| 914 | Py_XDECREF(np); |
| 915 | Py_XDECREF(t); |
| 916 | c->c_errors++; |
| 917 | return 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 921 | com_addconst(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 922 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 923 | return com_add(c, c->c_consts, c->c_const_dict, v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 927 | com_addname(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 928 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 929 | return com_add(c, c->c_names, c->c_name_dict, v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 932 | int |
| 933 | _Py_Mangle(char *p, char *name, char *buffer, size_t maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 934 | { |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 935 | /* Name mangling: __private becomes _classname__private. |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 936 | This is independent from how the name is used. */ |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 937 | size_t nlen, plen; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 938 | if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_') |
| 939 | return 0; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 940 | nlen = strlen(name); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 941 | if (nlen+2 >= maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 942 | return 0; /* Don't mangle __extremely_long_names */ |
| 943 | if (name[nlen-1] == '_' && name[nlen-2] == '_') |
| 944 | return 0; /* Don't mangle __whatever__ */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 945 | /* Strip leading underscores from class name */ |
| 946 | while (*p == '_') |
| 947 | p++; |
| 948 | if (*p == '\0') |
| 949 | return 0; /* Don't mangle if class is just underscores */ |
| 950 | plen = strlen(p); |
| 951 | if (plen + nlen >= maxlen) |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 952 | plen = maxlen-nlen-2; /* Truncate class name if too long */ |
| 953 | /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 954 | buffer[0] = '_'; |
| 955 | strncpy(buffer+1, p, plen); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 956 | strcpy(buffer+1+plen, name); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 957 | return 1; |
| 958 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 959 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 960 | static void |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 961 | com_addop_name(struct compiling *c, int op, char *name) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 962 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 963 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 964 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 965 | char buffer[MANGLE_LEN]; |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 966 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 967 | if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer))) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 968 | name = buffer; |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 969 | if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 970 | c->c_errors++; |
| 971 | i = 255; |
| 972 | } |
| 973 | else { |
| 974 | i = com_addname(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 975 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 976 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 977 | com_addoparg(c, op, i); |
| 978 | } |
| 979 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 980 | #define NAME_LOCAL 0 |
| 981 | #define NAME_GLOBAL 1 |
| 982 | #define NAME_DEFAULT 2 |
| 983 | #define NAME_CLOSURE 3 |
| 984 | |
| 985 | static int |
| 986 | com_lookup_arg(PyObject *dict, PyObject *name) |
| 987 | { |
| 988 | PyObject *v = PyDict_GetItem(dict, name); |
| 989 | if (v == NULL) |
| 990 | return -1; |
| 991 | else |
| 992 | return PyInt_AS_LONG(v); |
| 993 | } |
| 994 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 995 | static void |
| 996 | com_addop_varname(struct compiling *c, int kind, char *name) |
| 997 | { |
| 998 | PyObject *v; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 999 | int i, reftype; |
| 1000 | int scope = NAME_DEFAULT; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1001 | int op = STOP_CODE; |
| 1002 | char buffer[MANGLE_LEN]; |
| 1003 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1004 | if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1005 | name = buffer; |
| 1006 | if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { |
| 1007 | c->c_errors++; |
| 1008 | i = 255; |
| 1009 | goto done; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1010 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1011 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1012 | reftype = get_ref_type(c, name); |
| 1013 | switch (reftype) { |
| 1014 | case LOCAL: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 1015 | if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1016 | scope = NAME_LOCAL; |
| 1017 | break; |
| 1018 | case GLOBAL_EXPLICIT: |
| 1019 | scope = NAME_GLOBAL; |
| 1020 | break; |
| 1021 | case GLOBAL_IMPLICIT: |
| 1022 | if (c->c_flags & CO_OPTIMIZED) |
| 1023 | scope = NAME_GLOBAL; |
| 1024 | break; |
| 1025 | case FREE: |
| 1026 | case CELL: |
| 1027 | scope = NAME_CLOSURE; |
| 1028 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | i = com_addname(c, v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1032 | if (scope == NAME_LOCAL) |
| 1033 | i = com_lookup_arg(c->c_locals, v); |
| 1034 | else if (reftype == FREE) |
| 1035 | i = com_lookup_arg(c->c_freevars, v); |
| 1036 | else if (reftype == CELL) |
| 1037 | i = com_lookup_arg(c->c_cellvars, v); |
| 1038 | if (i == -1) { |
| 1039 | c->c_errors++; /* XXX no exception set */ |
| 1040 | i = 255; |
| 1041 | goto done; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1042 | } |
| 1043 | Py_DECREF(v); |
| 1044 | |
| 1045 | switch (kind) { |
| 1046 | case VAR_LOAD: |
| 1047 | switch (scope) { |
| 1048 | case NAME_LOCAL: |
| 1049 | op = LOAD_FAST; |
| 1050 | break; |
| 1051 | case NAME_GLOBAL: |
| 1052 | op = LOAD_GLOBAL; |
| 1053 | break; |
| 1054 | case NAME_DEFAULT: |
| 1055 | op = LOAD_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1056 | break; |
| 1057 | case NAME_CLOSURE: |
| 1058 | op = LOAD_DEREF; |
| 1059 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1060 | } |
| 1061 | break; |
| 1062 | case VAR_STORE: |
| 1063 | switch (scope) { |
| 1064 | case NAME_LOCAL: |
| 1065 | op = STORE_FAST; |
| 1066 | break; |
| 1067 | case NAME_GLOBAL: |
| 1068 | op = STORE_GLOBAL; |
| 1069 | break; |
| 1070 | case NAME_DEFAULT: |
| 1071 | op = STORE_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1072 | break; |
| 1073 | case NAME_CLOSURE: |
| 1074 | op = STORE_DEREF; |
| 1075 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1076 | } |
| 1077 | break; |
| 1078 | case VAR_DELETE: |
| 1079 | switch (scope) { |
| 1080 | case NAME_LOCAL: |
| 1081 | op = DELETE_FAST; |
| 1082 | break; |
| 1083 | case NAME_GLOBAL: |
| 1084 | op = DELETE_GLOBAL; |
| 1085 | break; |
| 1086 | case NAME_DEFAULT: |
| 1087 | op = DELETE_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1088 | break; |
| 1089 | case NAME_CLOSURE: { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 1090 | char buf[500]; |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 1091 | PyOS_snprintf(buf, sizeof(buf), |
| 1092 | DEL_CLOSURE_ERROR, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1093 | com_error(c, PyExc_SyntaxError, buf); |
| 1094 | i = 255; |
| 1095 | break; |
| 1096 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1097 | } |
| 1098 | break; |
| 1099 | } |
| 1100 | done: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1101 | com_addoparg(c, op, i); |
| 1102 | } |
| 1103 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1104 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1105 | com_addopname(struct compiling *c, int op, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1106 | { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1107 | char *name; |
| 1108 | char buffer[1000]; |
| 1109 | /* XXX it is possible to write this code without the 1000 |
| 1110 | chars on the total length of dotted names, I just can't be |
| 1111 | bothered right now */ |
| 1112 | if (TYPE(n) == STAR) |
| 1113 | name = "*"; |
| 1114 | else if (TYPE(n) == dotted_name) { |
| 1115 | char *p = buffer; |
| 1116 | int i; |
| 1117 | name = buffer; |
| 1118 | for (i = 0; i < NCH(n); i += 2) { |
| 1119 | char *s = STR(CHILD(n, i)); |
| 1120 | if (p + strlen(s) > buffer + (sizeof buffer) - 2) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1121 | com_error(c, PyExc_MemoryError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1122 | "dotted_name too long"); |
Guido van Rossum | d9dfaf5 | 1995-02-17 15:04:57 +0000 | [diff] [blame] | 1123 | name = NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1124 | break; |
| 1125 | } |
| 1126 | if (p != buffer) |
| 1127 | *p++ = '.'; |
| 1128 | strcpy(p, s); |
| 1129 | p = strchr(p, '\0'); |
| 1130 | } |
| 1131 | } |
| 1132 | else { |
| 1133 | REQ(n, NAME); |
| 1134 | name = STR(n); |
| 1135 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1136 | com_addop_name(c, op, name); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1139 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1140 | parsenumber(struct compiling *co, char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1141 | { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1142 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1143 | long x; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1144 | double dx; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1145 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 1146 | Py_complex c; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1147 | int imflag; |
| 1148 | #endif |
| 1149 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1150 | errno = 0; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1151 | end = s + strlen(s) - 1; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1152 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 15ad9a6 | 1996-01-26 20:53:56 +0000 | [diff] [blame] | 1153 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1154 | #endif |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 1155 | if (*end == 'l' || *end == 'L') |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1156 | return PyLong_FromString(s, (char **)0, 0); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 1157 | if (s[0] == '0') |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1158 | x = (long) PyOS_strtoul(s, &end, 0); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 1159 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1160 | x = PyOS_strtol(s, &end, 0); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1161 | if (*end == '\0') { |
Jeremy Hylton | 71b6af9 | 2001-08-27 19:45:25 +0000 | [diff] [blame] | 1162 | if (errno != 0) |
| 1163 | return PyLong_FromString(s, (char **)0, 0); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1164 | return PyInt_FromLong(x); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1165 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1166 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1167 | #ifndef WITHOUT_COMPLEX |
| 1168 | if (imflag) { |
| 1169 | c.real = 0.; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1170 | PyFPE_START_PROTECT("atof", return 0) |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1171 | c.imag = atof(s); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1172 | PyFPE_END_PROTECT(c) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1173 | return PyComplex_FromCComplex(c); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1174 | } |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1175 | else |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1176 | #endif |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1177 | { |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1178 | PyFPE_START_PROTECT("atof", return 0) |
| 1179 | dx = atof(s); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1180 | PyFPE_END_PROTECT(dx) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1181 | return PyFloat_FromDouble(dx); |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1182 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1185 | static PyObject * |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1186 | decode_utf8(char **sPtr, char *end, char* encoding) |
| 1187 | { |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1188 | #ifndef Py_USING_UNICODE |
Martin v. Löwis | 2863c10 | 2002-08-07 15:18:57 +0000 | [diff] [blame] | 1189 | Py_FatalError("decode_utf8 should not be called in this build."); |
| 1190 | return NULL; |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1191 | #else |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1192 | PyObject *u, *v; |
| 1193 | char *s, *t; |
| 1194 | t = s = *sPtr; |
| 1195 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 1196 | while (s < end && (*s & 0x80)) s++; |
| 1197 | *sPtr = s; |
| 1198 | u = PyUnicode_DecodeUTF8(t, s - t, NULL); |
| 1199 | if (u == NULL) |
| 1200 | return NULL; |
| 1201 | v = PyUnicode_AsEncodedString(u, encoding, NULL); |
| 1202 | Py_DECREF(u); |
| 1203 | return v; |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1204 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1208 | parsestr(struct compiling *com, char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1209 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1210 | PyObject *v; |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1211 | size_t len; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1212 | char *buf; |
| 1213 | char *p; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1214 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1215 | int c; |
Guido van Rossum | 5f5e817 | 1997-04-06 03:41:40 +0000 | [diff] [blame] | 1216 | int first = *s; |
| 1217 | int quote = first; |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1218 | int rawmode = 0; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1219 | char* encoding = ((com == NULL) ? NULL : com->c_encoding); |
| 1220 | int need_encoding; |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1221 | int unicode = 0; |
Guido van Rossum | 05459c5 | 2002-05-28 18:47:29 +0000 | [diff] [blame] | 1222 | |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1223 | if (isalpha(quote) || quote == '_') { |
| 1224 | if (quote == 'u' || quote == 'U') { |
| 1225 | quote = *++s; |
| 1226 | unicode = 1; |
| 1227 | } |
| 1228 | if (quote == 'r' || quote == 'R') { |
| 1229 | quote = *++s; |
| 1230 | rawmode = 1; |
| 1231 | } |
| 1232 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1233 | if (quote != '\'' && quote != '\"') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1234 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1235 | return NULL; |
| 1236 | } |
| 1237 | s++; |
| 1238 | len = strlen(s); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1239 | if (len > INT_MAX) { |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1240 | com_error(com, PyExc_OverflowError, |
| 1241 | "string to parse is too long"); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1242 | return NULL; |
| 1243 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1244 | if (s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1245 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1246 | return NULL; |
| 1247 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1248 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
| 1249 | s += 2; |
| 1250 | len -= 2; |
| 1251 | if (s[--len] != quote || s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1252 | PyErr_BadInternalCall(); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1253 | return NULL; |
| 1254 | } |
| 1255 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1256 | #ifdef Py_USING_UNICODE |
Guido van Rossum | fdc8bdb | 2000-05-01 17:54:56 +0000 | [diff] [blame] | 1257 | if (unicode || Py_UnicodeFlag) { |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1258 | PyObject *u, *w; |
| 1259 | if (encoding == NULL) { |
| 1260 | buf = s; |
| 1261 | u = NULL; |
| 1262 | } else if (strcmp(encoding, "iso-8859-1") == 0) { |
| 1263 | buf = s; |
| 1264 | u = NULL; |
| 1265 | } else { |
| 1266 | /* "\XX" may become "\u005c\uHHLL" (12 bytes) */ |
| 1267 | u = PyString_FromStringAndSize((char *)NULL, len * 4); |
| 1268 | if (u == NULL) |
| 1269 | return NULL; |
| 1270 | p = buf = PyString_AsString(u); |
| 1271 | end = s + len; |
| 1272 | while (s < end) { |
| 1273 | if (*s == '\\') { |
| 1274 | *p++ = *s++; |
| 1275 | if (*s & 0x80) { |
| 1276 | strcpy(p, "u005c"); |
| 1277 | p += 5; |
| 1278 | } |
| 1279 | } |
| 1280 | if (*s & 0x80) { /* XXX inefficient */ |
| 1281 | char *r; |
| 1282 | int rn, i; |
| 1283 | w = decode_utf8(&s, end, "utf-16-be"); |
| 1284 | if (w == NULL) { |
| 1285 | Py_DECREF(u); |
| 1286 | return NULL; |
| 1287 | } |
| 1288 | r = PyString_AsString(w); |
| 1289 | rn = PyString_Size(w); |
| 1290 | assert(rn % 2 == 0); |
| 1291 | for (i = 0; i < rn; i += 2) { |
| 1292 | sprintf(p, "\\u%02x%02x", |
| 1293 | r[i + 0] & 0xFF, |
| 1294 | r[i + 1] & 0xFF); |
| 1295 | p += 6; |
| 1296 | } |
| 1297 | Py_DECREF(w); |
| 1298 | } else { |
| 1299 | *p++ = *s++; |
| 1300 | } |
| 1301 | } |
| 1302 | len = p - buf; |
| 1303 | } |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1304 | if (rawmode) |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1305 | v = PyUnicode_DecodeRawUnicodeEscape(buf, len, NULL); |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1306 | else |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1307 | v = PyUnicode_DecodeUnicodeEscape(buf, len, NULL); |
| 1308 | Py_XDECREF(u); |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1309 | if (v == NULL) |
| 1310 | PyErr_SyntaxLocation(com->c_filename, com->c_lineno); |
| 1311 | return v; |
| 1312 | |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1313 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1314 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1315 | need_encoding = (encoding != NULL && |
| 1316 | strcmp(encoding, "utf-8") != 0 && |
| 1317 | strcmp(encoding, "iso-8859-1") != 0); |
| 1318 | if (rawmode || strchr(s, '\\') == NULL) { |
| 1319 | if (need_encoding) { |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1320 | #ifndef Py_USING_UNICODE |
| 1321 | /* This should not happen - we never see any other |
| 1322 | encoding. */ |
Martin v. Löwis | 2863c10 | 2002-08-07 15:18:57 +0000 | [diff] [blame] | 1323 | Py_FatalError("cannot deal with encodings in this build."); |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1324 | #else |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1325 | PyObject* u = PyUnicode_DecodeUTF8(s, len, NULL); |
| 1326 | if (u == NULL) |
| 1327 | return NULL; |
| 1328 | v = PyUnicode_AsEncodedString(u, encoding, NULL); |
| 1329 | Py_DECREF(u); |
| 1330 | return v; |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1331 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1332 | } else { |
| 1333 | return PyString_FromStringAndSize(s, len); |
| 1334 | } |
| 1335 | } |
| 1336 | v = PyString_FromStringAndSize((char *)NULL, /* XXX 4 is enough? */ |
| 1337 | need_encoding ? len * 4 : len); |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1338 | if (v == NULL) |
| 1339 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1340 | p = buf = PyString_AsString(v); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1341 | end = s + len; |
| 1342 | while (s < end) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1343 | if (*s != '\\') { |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1344 | ORDINAL: |
| 1345 | if (need_encoding && (*s & 0x80)) { |
| 1346 | char *r; |
| 1347 | int rn; |
| 1348 | PyObject* w = decode_utf8(&s, end, encoding); |
| 1349 | if (w == NULL) |
| 1350 | return NULL; |
| 1351 | r = PyString_AsString(w); |
| 1352 | rn = PyString_Size(w); |
| 1353 | memcpy(p, r, rn); |
| 1354 | p += rn; |
| 1355 | Py_DECREF(w); |
| 1356 | } else { |
| 1357 | *p++ = *s++; |
| 1358 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1359 | continue; |
| 1360 | } |
| 1361 | s++; |
| 1362 | switch (*s++) { |
| 1363 | /* XXX This assumes ASCII! */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1364 | case '\n': break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1365 | case '\\': *p++ = '\\'; break; |
| 1366 | case '\'': *p++ = '\''; break; |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1367 | case '\"': *p++ = '\"'; break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1368 | case 'b': *p++ = '\b'; break; |
| 1369 | case 'f': *p++ = '\014'; break; /* FF */ |
| 1370 | case 't': *p++ = '\t'; break; |
| 1371 | case 'n': *p++ = '\n'; break; |
| 1372 | case 'r': *p++ = '\r'; break; |
| 1373 | case 'v': *p++ = '\013'; break; /* VT */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1374 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 1375 | case '0': case '1': case '2': case '3': |
| 1376 | case '4': case '5': case '6': case '7': |
| 1377 | c = s[-1] - '0'; |
| 1378 | if ('0' <= *s && *s <= '7') { |
| 1379 | c = (c<<3) + *s++ - '0'; |
| 1380 | if ('0' <= *s && *s <= '7') |
| 1381 | c = (c<<3) + *s++ - '0'; |
| 1382 | } |
| 1383 | *p++ = c; |
| 1384 | break; |
| 1385 | case 'x': |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 1386 | if (isxdigit(Py_CHARMASK(s[0])) |
| 1387 | && isxdigit(Py_CHARMASK(s[1]))) { |
Guido van Rossum | ed1100f | 1997-10-20 23:24:07 +0000 | [diff] [blame] | 1388 | unsigned int x = 0; |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1389 | c = Py_CHARMASK(*s); |
| 1390 | s++; |
| 1391 | if (isdigit(c)) |
| 1392 | x = c - '0'; |
| 1393 | else if (islower(c)) |
| 1394 | x = 10 + c - 'a'; |
| 1395 | else |
| 1396 | x = 10 + c - 'A'; |
| 1397 | x = x << 4; |
| 1398 | c = Py_CHARMASK(*s); |
| 1399 | s++; |
| 1400 | if (isdigit(c)) |
| 1401 | x += c - '0'; |
| 1402 | else if (islower(c)) |
| 1403 | x += 10 + c - 'a'; |
| 1404 | else |
| 1405 | x += 10 + c - 'A'; |
Guido van Rossum | ed1100f | 1997-10-20 23:24:07 +0000 | [diff] [blame] | 1406 | *p++ = x; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1407 | break; |
| 1408 | } |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1409 | Py_DECREF(v); |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1410 | com_error(com, PyExc_ValueError, |
| 1411 | "invalid \\x escape"); |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1412 | return NULL; |
Guido van Rossum | 05459c5 | 2002-05-28 18:47:29 +0000 | [diff] [blame] | 1413 | #ifndef Py_USING_UNICODE |
| 1414 | case 'u': |
| 1415 | case 'U': |
| 1416 | case 'N': |
| 1417 | if (unicode) { |
| 1418 | Py_DECREF(v); |
| 1419 | com_error(com, PyExc_ValueError, |
| 1420 | "Unicode escapes not legal " |
| 1421 | "when Unicode disabled"); |
| 1422 | return NULL; |
| 1423 | } |
| 1424 | #endif |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1425 | default: |
| 1426 | *p++ = '\\'; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1427 | s--; |
| 1428 | goto ORDINAL; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1429 | } |
| 1430 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1431 | _PyString_Resize(&v, (int)(p - buf)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1432 | return v; |
| 1433 | } |
| 1434 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1435 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1436 | parsestrplus(struct compiling* c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1437 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1438 | PyObject *v; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1439 | int i; |
| 1440 | REQ(CHILD(n, 0), STRING); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1441 | if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1442 | /* String literal concatenation */ |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1443 | for (i = 1; i < NCH(n); i++) { |
| 1444 | PyObject *s; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1445 | s = parsestr(c, STR(CHILD(n, i))); |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1446 | if (s == NULL) |
| 1447 | goto onError; |
| 1448 | if (PyString_Check(v) && PyString_Check(s)) { |
| 1449 | PyString_ConcatAndDel(&v, s); |
| 1450 | if (v == NULL) |
| 1451 | goto onError; |
| 1452 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1453 | #ifdef Py_USING_UNICODE |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1454 | else { |
| 1455 | PyObject *temp; |
| 1456 | temp = PyUnicode_Concat(v, s); |
| 1457 | Py_DECREF(s); |
| 1458 | if (temp == NULL) |
| 1459 | goto onError; |
| 1460 | Py_DECREF(v); |
| 1461 | v = temp; |
| 1462 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1463 | #endif |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1464 | } |
| 1465 | } |
| 1466 | return v; |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1467 | |
| 1468 | onError: |
| 1469 | Py_XDECREF(v); |
| 1470 | return NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1473 | static void |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1474 | 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] | 1475 | { |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1476 | int anchor = 0; |
| 1477 | int save_begin = c->c_begin; |
| 1478 | |
| 1479 | /* list_iter: for v in expr [list_iter] */ |
| 1480 | com_node(c, CHILD(n, 3)); /* expr */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1481 | com_addbyte(c, GET_ITER); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1482 | c->c_begin = c->c_nexti; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1483 | com_addfwref(c, FOR_ITER, &anchor); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1484 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1485 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1486 | c->c_loops++; |
| 1487 | com_list_iter(c, n, e, t); |
| 1488 | c->c_loops--; |
| 1489 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 1490 | c->c_begin = save_begin; |
| 1491 | com_backpatch(c, anchor); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1492 | com_pop(c, 1); /* FOR_ITER has popped this */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | static void |
| 1496 | com_list_if(struct compiling *c, node *n, node *e, char *t) |
| 1497 | { |
| 1498 | int anchor = 0; |
| 1499 | int a = 0; |
| 1500 | /* list_iter: 'if' test [list_iter] */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1501 | com_node(c, CHILD(n, 1)); |
| 1502 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 1503 | com_addbyte(c, POP_TOP); |
| 1504 | com_pop(c, 1); |
| 1505 | com_list_iter(c, n, e, t); |
| 1506 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 1507 | com_backpatch(c, a); |
| 1508 | /* We jump here with an extra entry which we now pop */ |
| 1509 | com_addbyte(c, POP_TOP); |
| 1510 | com_backpatch(c, anchor); |
| 1511 | } |
| 1512 | |
| 1513 | static void |
| 1514 | com_list_iter(struct compiling *c, |
| 1515 | node *p, /* parent of list_iter node */ |
| 1516 | node *e, /* element expression node */ |
| 1517 | char *t /* name of result list temp local */) |
| 1518 | { |
| 1519 | /* list_iter is the last child in a listmaker, list_for, or list_if */ |
| 1520 | node *n = CHILD(p, NCH(p)-1); |
| 1521 | if (TYPE(n) == list_iter) { |
| 1522 | n = CHILD(n, 0); |
| 1523 | switch (TYPE(n)) { |
| 1524 | case list_for: |
| 1525 | com_list_for(c, n, e, t); |
| 1526 | break; |
| 1527 | case list_if: |
| 1528 | com_list_if(c, n, e, t); |
| 1529 | break; |
| 1530 | default: |
| 1531 | com_error(c, PyExc_SystemError, |
| 1532 | "invalid list_iter node type"); |
| 1533 | } |
| 1534 | } |
| 1535 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1536 | com_addop_varname(c, VAR_LOAD, t); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1537 | com_push(c, 1); |
| 1538 | com_node(c, e); |
| 1539 | com_addoparg(c, CALL_FUNCTION, 1); |
| 1540 | com_addbyte(c, POP_TOP); |
| 1541 | com_pop(c, 2); |
| 1542 | } |
| 1543 | } |
| 1544 | |
| 1545 | static void |
| 1546 | com_list_comprehension(struct compiling *c, node *n) |
| 1547 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1548 | /* listmaker: test list_for */ |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 1549 | char tmpname[30]; |
| 1550 | PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->c_tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1551 | com_addoparg(c, BUILD_LIST, 0); |
| 1552 | com_addbyte(c, DUP_TOP); /* leave the result on the stack */ |
| 1553 | com_push(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1554 | com_addop_name(c, LOAD_ATTR, "append"); |
| 1555 | com_addop_varname(c, VAR_STORE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1556 | com_pop(c, 1); |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1557 | com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1558 | com_addop_varname(c, VAR_DELETE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1559 | --c->c_tmpname; |
| 1560 | } |
| 1561 | |
| 1562 | static void |
| 1563 | com_listmaker(struct compiling *c, node *n) |
| 1564 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1565 | /* listmaker: test ( list_for | (',' test)* [','] ) */ |
| 1566 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1567 | com_list_comprehension(c, n); |
| 1568 | else { |
| 1569 | int len = 0; |
| 1570 | int i; |
| 1571 | for (i = 0; i < NCH(n); i += 2, len++) |
| 1572 | com_node(c, CHILD(n, i)); |
| 1573 | com_addoparg(c, BUILD_LIST, len); |
| 1574 | com_pop(c, len-1); |
| 1575 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1579 | com_dictmaker(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1580 | { |
| 1581 | int i; |
| 1582 | /* dictmaker: test ':' test (',' test ':' value)* [','] */ |
| 1583 | for (i = 0; i+2 < NCH(n); i += 4) { |
| 1584 | /* We must arrange things just right for STORE_SUBSCR. |
| 1585 | It wants the stack to look like (value) (dict) (key) */ |
| 1586 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1587 | com_push(c, 1); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1588 | com_node(c, CHILD(n, i+2)); /* value */ |
| 1589 | com_addbyte(c, ROT_TWO); |
| 1590 | com_node(c, CHILD(n, i)); /* key */ |
| 1591 | com_addbyte(c, STORE_SUBSCR); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1592 | com_pop(c, 3); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1593 | } |
| 1594 | } |
| 1595 | |
| 1596 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1597 | com_atom(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1598 | { |
| 1599 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1600 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1601 | int i; |
| 1602 | REQ(n, atom); |
| 1603 | ch = CHILD(n, 0); |
| 1604 | switch (TYPE(ch)) { |
| 1605 | case LPAR: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1606 | if (TYPE(CHILD(n, 1)) == RPAR) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1607 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1608 | com_push(c, 1); |
| 1609 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1610 | else |
| 1611 | com_node(c, CHILD(n, 1)); |
| 1612 | break; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1613 | case LSQB: /* '[' [listmaker] ']' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1614 | if (TYPE(CHILD(n, 1)) == RSQB) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1615 | com_addoparg(c, BUILD_LIST, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1616 | com_push(c, 1); |
| 1617 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1618 | else |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1619 | com_listmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1620 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1621 | case LBRACE: /* '{' [dictmaker] '}' */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1622 | com_addoparg(c, BUILD_MAP, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1623 | com_push(c, 1); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1624 | if (TYPE(CHILD(n, 1)) == dictmaker) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1625 | com_dictmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1626 | break; |
| 1627 | case BACKQUOTE: |
| 1628 | com_node(c, CHILD(n, 1)); |
| 1629 | com_addbyte(c, UNARY_CONVERT); |
| 1630 | break; |
| 1631 | case NUMBER: |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1632 | if ((v = parsenumber(c, STR(ch))) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1633 | i = 255; |
| 1634 | } |
| 1635 | else { |
| 1636 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1637 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1638 | } |
| 1639 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1640 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1641 | break; |
| 1642 | case STRING: |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1643 | v = parsestrplus(c, n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1644 | if (v == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1645 | c->c_errors++; |
| 1646 | i = 255; |
| 1647 | } |
| 1648 | else { |
| 1649 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1650 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1651 | } |
| 1652 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1653 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1654 | break; |
| 1655 | case NAME: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1656 | com_addop_varname(c, VAR_LOAD, STR(ch)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1657 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1658 | break; |
| 1659 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1660 | com_error(c, PyExc_SystemError, |
| 1661 | "com_atom: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1662 | } |
| 1663 | } |
| 1664 | |
| 1665 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1666 | com_slice(struct compiling *c, node *n, int op) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1667 | { |
| 1668 | if (NCH(n) == 1) { |
| 1669 | com_addbyte(c, op); |
| 1670 | } |
| 1671 | else if (NCH(n) == 2) { |
| 1672 | if (TYPE(CHILD(n, 0)) != COLON) { |
| 1673 | com_node(c, CHILD(n, 0)); |
| 1674 | com_addbyte(c, op+1); |
| 1675 | } |
| 1676 | else { |
| 1677 | com_node(c, CHILD(n, 1)); |
| 1678 | com_addbyte(c, op+2); |
| 1679 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1680 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1681 | } |
| 1682 | else { |
| 1683 | com_node(c, CHILD(n, 0)); |
| 1684 | com_node(c, CHILD(n, 2)); |
| 1685 | com_addbyte(c, op+3); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1686 | com_pop(c, 2); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1687 | } |
| 1688 | } |
| 1689 | |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1690 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1691 | com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn) |
| 1692 | { |
| 1693 | if (NCH(n) == 1) { |
| 1694 | com_addbyte(c, DUP_TOP); |
| 1695 | com_push(c, 1); |
| 1696 | com_addbyte(c, SLICE); |
| 1697 | com_node(c, augn); |
| 1698 | com_addbyte(c, opcode); |
| 1699 | com_pop(c, 1); |
| 1700 | com_addbyte(c, ROT_TWO); |
| 1701 | com_addbyte(c, STORE_SLICE); |
| 1702 | com_pop(c, 2); |
| 1703 | } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) { |
| 1704 | com_node(c, CHILD(n, 0)); |
| 1705 | com_addoparg(c, DUP_TOPX, 2); |
| 1706 | com_push(c, 2); |
| 1707 | com_addbyte(c, SLICE+1); |
| 1708 | com_pop(c, 1); |
| 1709 | com_node(c, augn); |
| 1710 | com_addbyte(c, opcode); |
| 1711 | com_pop(c, 1); |
| 1712 | com_addbyte(c, ROT_THREE); |
| 1713 | com_addbyte(c, STORE_SLICE+1); |
| 1714 | com_pop(c, 3); |
| 1715 | } else if (NCH(n) == 2) { |
| 1716 | com_node(c, CHILD(n, 1)); |
| 1717 | com_addoparg(c, DUP_TOPX, 2); |
| 1718 | com_push(c, 2); |
| 1719 | com_addbyte(c, SLICE+2); |
| 1720 | com_pop(c, 1); |
| 1721 | com_node(c, augn); |
| 1722 | com_addbyte(c, opcode); |
| 1723 | com_pop(c, 1); |
| 1724 | com_addbyte(c, ROT_THREE); |
| 1725 | com_addbyte(c, STORE_SLICE+2); |
| 1726 | com_pop(c, 3); |
| 1727 | } else { |
| 1728 | com_node(c, CHILD(n, 0)); |
| 1729 | com_node(c, CHILD(n, 2)); |
| 1730 | com_addoparg(c, DUP_TOPX, 3); |
| 1731 | com_push(c, 3); |
| 1732 | com_addbyte(c, SLICE+3); |
| 1733 | com_pop(c, 2); |
| 1734 | com_node(c, augn); |
| 1735 | com_addbyte(c, opcode); |
| 1736 | com_pop(c, 1); |
| 1737 | com_addbyte(c, ROT_FOUR); |
| 1738 | com_addbyte(c, STORE_SLICE+3); |
| 1739 | com_pop(c, 4); |
| 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1744 | com_argument(struct compiling *c, node *n, PyObject **pkeywords) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1745 | { |
| 1746 | node *m; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1747 | REQ(n, argument); /* [test '='] test; really [keyword '='] test */ |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1748 | if (NCH(n) == 1) { |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1749 | if (*pkeywords != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1750 | com_error(c, PyExc_SyntaxError, |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 1751 | "non-keyword arg after keyword arg"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1752 | } |
| 1753 | else { |
| 1754 | com_node(c, CHILD(n, 0)); |
| 1755 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1756 | return; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1757 | } |
| 1758 | m = n; |
| 1759 | do { |
| 1760 | m = CHILD(m, 0); |
| 1761 | } while (NCH(m) == 1); |
| 1762 | if (TYPE(m) != NAME) { |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 1763 | /* f(lambda x: x[0] = 3) ends up getting parsed with |
| 1764 | * LHS test = lambda x: x[0], and RHS test = 3. |
| 1765 | * SF bug 132313 points out that complaining about a keyword |
| 1766 | * then is very confusing. |
| 1767 | */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1768 | com_error(c, PyExc_SyntaxError, |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 1769 | TYPE(m) == lambdef ? |
| 1770 | "lambda cannot contain assignment" : |
| 1771 | "keyword can't be an expression"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1772 | } |
| 1773 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1774 | PyObject *v = PyString_InternFromString(STR(m)); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1775 | if (v != NULL && *pkeywords == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1776 | *pkeywords = PyDict_New(); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 1777 | if (v == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1778 | c->c_errors++; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 1779 | else if (*pkeywords == NULL) { |
| 1780 | c->c_errors++; |
| 1781 | Py_DECREF(v); |
| 1782 | } else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1783 | if (PyDict_GetItem(*pkeywords, v) != NULL) |
| 1784 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1785 | "duplicate keyword argument"); |
| 1786 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1787 | if (PyDict_SetItem(*pkeywords, v, v) != 0) |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1788 | c->c_errors++; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1789 | com_addoparg(c, LOAD_CONST, com_addconst(c, v)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1790 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1791 | Py_DECREF(v); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1792 | } |
| 1793 | } |
| 1794 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1797 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1798 | com_call_function(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1799 | { |
| 1800 | if (TYPE(n) == RPAR) { |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1801 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1802 | } |
| 1803 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1804 | PyObject *keywords = NULL; |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1805 | int i, na, nk; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1806 | int lineno = n->n_lineno; |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1807 | int star_flag = 0; |
| 1808 | int starstar_flag = 0; |
| 1809 | int opcode; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1810 | REQ(n, arglist); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1811 | na = 0; |
| 1812 | nk = 0; |
| 1813 | for (i = 0; i < NCH(n); i += 2) { |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1814 | node *ch = CHILD(n, i); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1815 | if (TYPE(ch) == STAR || |
| 1816 | TYPE(ch) == DOUBLESTAR) |
| 1817 | break; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1818 | if (ch->n_lineno != lineno) { |
| 1819 | lineno = ch->n_lineno; |
| 1820 | com_addoparg(c, SET_LINENO, lineno); |
| 1821 | } |
| 1822 | com_argument(c, ch, &keywords); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1823 | if (keywords == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1824 | na++; |
| 1825 | else |
| 1826 | nk++; |
| 1827 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1828 | Py_XDECREF(keywords); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1829 | while (i < NCH(n)) { |
| 1830 | node *tok = CHILD(n, i); |
| 1831 | node *ch = CHILD(n, i+1); |
| 1832 | i += 3; |
| 1833 | switch (TYPE(tok)) { |
| 1834 | case STAR: star_flag = 1; break; |
| 1835 | case DOUBLESTAR: starstar_flag = 1; break; |
| 1836 | } |
| 1837 | com_node(c, ch); |
| 1838 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1839 | if (na > 255 || nk > 255) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1840 | com_error(c, PyExc_SyntaxError, |
| 1841 | "more than 255 arguments"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1842 | } |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1843 | if (star_flag || starstar_flag) |
Jeremy Hylton | e4fb958 | 2000-03-29 00:10:44 +0000 | [diff] [blame] | 1844 | opcode = CALL_FUNCTION_VAR - 1 + |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1845 | star_flag + (starstar_flag << 1); |
| 1846 | else |
| 1847 | opcode = CALL_FUNCTION; |
| 1848 | com_addoparg(c, opcode, na | (nk << 8)); |
| 1849 | com_pop(c, na + 2*nk + star_flag + starstar_flag); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1850 | } |
| 1851 | } |
| 1852 | |
| 1853 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1854 | com_select_member(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1855 | { |
| 1856 | com_addopname(c, LOAD_ATTR, n); |
| 1857 | } |
| 1858 | |
| 1859 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1860 | com_sliceobj(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1861 | { |
| 1862 | int i=0; |
| 1863 | int ns=2; /* number of slice arguments */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1864 | node *ch; |
| 1865 | |
| 1866 | /* first argument */ |
| 1867 | if (TYPE(CHILD(n,i)) == COLON) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1868 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1869 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1870 | i++; |
| 1871 | } |
| 1872 | else { |
| 1873 | com_node(c, CHILD(n,i)); |
| 1874 | i++; |
| 1875 | REQ(CHILD(n,i),COLON); |
| 1876 | i++; |
| 1877 | } |
| 1878 | /* second argument */ |
| 1879 | if (i < NCH(n) && TYPE(CHILD(n,i)) == test) { |
| 1880 | com_node(c, CHILD(n,i)); |
| 1881 | i++; |
| 1882 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1883 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1884 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1885 | com_push(c, 1); |
| 1886 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1887 | /* remaining arguments */ |
| 1888 | for (; i < NCH(n); i++) { |
| 1889 | ns++; |
| 1890 | ch=CHILD(n,i); |
| 1891 | REQ(ch, sliceop); |
| 1892 | if (NCH(ch) == 1) { |
| 1893 | /* right argument of ':' missing */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1894 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1895 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1896 | } |
| 1897 | else |
| 1898 | com_node(c, CHILD(ch,1)); |
| 1899 | } |
| 1900 | com_addoparg(c, BUILD_SLICE, ns); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1901 | com_pop(c, 1 + (ns == 3)); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1905 | com_subscript(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1906 | { |
| 1907 | node *ch; |
| 1908 | REQ(n, subscript); |
| 1909 | ch = CHILD(n,0); |
| 1910 | /* check for rubber index */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1911 | if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) { |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 1912 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1913 | com_push(c, 1); |
| 1914 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1915 | else { |
| 1916 | /* check for slice */ |
| 1917 | if ((TYPE(ch) == COLON || NCH(n) > 1)) |
| 1918 | com_sliceobj(c, n); |
| 1919 | else { |
| 1920 | REQ(ch, test); |
| 1921 | com_node(c, ch); |
| 1922 | } |
| 1923 | } |
| 1924 | } |
| 1925 | |
| 1926 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1927 | com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1928 | { |
| 1929 | int i, op; |
| 1930 | REQ(n, subscriptlist); |
| 1931 | /* Check to make backward compatible slice behavior for '[i:j]' */ |
| 1932 | if (NCH(n) == 1) { |
| 1933 | node *sub = CHILD(n, 0); /* subscript */ |
Thomas Wouters | e8643c4 | 2000-08-05 21:37:50 +0000 | [diff] [blame] | 1934 | /* 'Basic' slice, should have exactly one colon. */ |
| 1935 | if ((TYPE(CHILD(sub, 0)) == COLON |
| 1936 | || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON)) |
| 1937 | && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop)) |
| 1938 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1939 | switch (assigning) { |
| 1940 | case OP_DELETE: |
| 1941 | op = DELETE_SLICE; |
| 1942 | break; |
| 1943 | case OP_ASSIGN: |
| 1944 | op = STORE_SLICE; |
| 1945 | break; |
| 1946 | case OP_APPLY: |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1947 | op = SLICE; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1948 | break; |
| 1949 | default: |
| 1950 | com_augassign_slice(c, sub, assigning, augn); |
| 1951 | return; |
| 1952 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1953 | com_slice(c, sub, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1954 | if (op == STORE_SLICE) |
| 1955 | com_pop(c, 2); |
| 1956 | else if (op == DELETE_SLICE) |
| 1957 | com_pop(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1958 | return; |
| 1959 | } |
| 1960 | } |
| 1961 | /* Else normal subscriptlist. Compile each subscript. */ |
| 1962 | for (i = 0; i < NCH(n); i += 2) |
| 1963 | com_subscript(c, CHILD(n, i)); |
| 1964 | /* Put multiple subscripts into a tuple */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1965 | if (NCH(n) > 1) { |
| 1966 | i = (NCH(n)+1) / 2; |
| 1967 | com_addoparg(c, BUILD_TUPLE, i); |
| 1968 | com_pop(c, i-1); |
| 1969 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1970 | switch (assigning) { |
| 1971 | case OP_DELETE: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1972 | op = DELETE_SUBSCR; |
| 1973 | i = 2; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1974 | break; |
| 1975 | default: |
| 1976 | case OP_ASSIGN: |
| 1977 | op = STORE_SUBSCR; |
| 1978 | i = 3; |
| 1979 | break; |
| 1980 | case OP_APPLY: |
| 1981 | op = BINARY_SUBSCR; |
| 1982 | i = 1; |
| 1983 | break; |
| 1984 | } |
| 1985 | if (assigning > OP_APPLY) { |
| 1986 | com_addoparg(c, DUP_TOPX, 2); |
| 1987 | com_push(c, 2); |
| 1988 | com_addbyte(c, BINARY_SUBSCR); |
| 1989 | com_pop(c, 1); |
| 1990 | com_node(c, augn); |
| 1991 | com_addbyte(c, assigning); |
| 1992 | com_pop(c, 1); |
| 1993 | com_addbyte(c, ROT_THREE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1994 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1995 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1996 | com_pop(c, i); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2000 | com_apply_trailer(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2001 | { |
| 2002 | REQ(n, trailer); |
| 2003 | switch (TYPE(CHILD(n, 0))) { |
| 2004 | case LPAR: |
| 2005 | com_call_function(c, CHILD(n, 1)); |
| 2006 | break; |
| 2007 | case DOT: |
| 2008 | com_select_member(c, CHILD(n, 1)); |
| 2009 | break; |
| 2010 | case LSQB: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2011 | com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2012 | break; |
| 2013 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2014 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2015 | "com_apply_trailer: unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2016 | } |
| 2017 | } |
| 2018 | |
| 2019 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2020 | com_power(struct compiling *c, node *n) |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2021 | { |
| 2022 | int i; |
| 2023 | REQ(n, power); |
| 2024 | com_atom(c, CHILD(n, 0)); |
| 2025 | for (i = 1; i < NCH(n); i++) { |
| 2026 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
| 2027 | com_factor(c, CHILD(n, i+1)); |
| 2028 | com_addbyte(c, BINARY_POWER); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2029 | com_pop(c, 1); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2030 | break; |
| 2031 | } |
| 2032 | else |
| 2033 | com_apply_trailer(c, CHILD(n, i)); |
| 2034 | } |
| 2035 | } |
| 2036 | |
| 2037 | static void |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2038 | com_invert_constant(struct compiling *c, node *n) |
| 2039 | { |
| 2040 | /* Compute the inverse of int and longs and use them directly, |
| 2041 | but be prepared to generate code for all other |
| 2042 | possibilities (invalid numbers, floats, complex). |
| 2043 | */ |
| 2044 | PyObject *num, *inv = NULL; |
| 2045 | int i; |
| 2046 | |
| 2047 | REQ(n, NUMBER); |
| 2048 | num = parsenumber(c, STR(n)); |
| 2049 | if (num == NULL) |
| 2050 | i = 255; |
| 2051 | else { |
| 2052 | inv = PyNumber_Invert(num); |
| 2053 | if (inv == NULL) { |
| 2054 | PyErr_Clear(); |
| 2055 | i = com_addconst(c, num); |
| 2056 | } else { |
| 2057 | i = com_addconst(c, inv); |
| 2058 | Py_DECREF(inv); |
| 2059 | } |
| 2060 | Py_DECREF(num); |
| 2061 | } |
| 2062 | com_addoparg(c, LOAD_CONST, i); |
| 2063 | com_push(c, 1); |
| 2064 | if (num != NULL && inv == NULL) |
| 2065 | com_addbyte(c, UNARY_INVERT); |
| 2066 | } |
| 2067 | |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2068 | static int |
| 2069 | is_float_zero(const char *p) |
| 2070 | { |
| 2071 | int found_radix_point = 0; |
| 2072 | int ch; |
| 2073 | while ((ch = Py_CHARMASK(*p++)) != '\0') { |
| 2074 | switch (ch) { |
| 2075 | case '0': |
| 2076 | /* no reason to believe it's not 0 -- continue */ |
| 2077 | break; |
| 2078 | |
| 2079 | case 'e': case 'E': case 'j': case 'J': |
| 2080 | /* If this was a hex constant, we already would have |
| 2081 | returned 0 due to the 'x' or 'X', so 'e' or 'E' |
| 2082 | must be an exponent marker, and we haven't yet |
| 2083 | seen a non-zero digit, and it doesn't matter what |
| 2084 | the exponent is then. For 'j' or 'J' similarly, |
| 2085 | except that this is an imaginary 0 then. */ |
| 2086 | return 1; |
| 2087 | |
| 2088 | case '.': |
| 2089 | found_radix_point = 1; |
| 2090 | break; |
| 2091 | |
| 2092 | default: |
| 2093 | return 0; |
| 2094 | } |
| 2095 | } |
| 2096 | return found_radix_point; |
| 2097 | } |
| 2098 | |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2099 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2100 | com_factor(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2101 | { |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2102 | int childtype = TYPE(CHILD(n, 0)); |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2103 | node *pfactor, *ppower, *patom, *pnum; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2104 | REQ(n, factor); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2105 | /* If the unary +, -, or ~ operator is applied to a constant, |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2106 | don't generate a UNARY_xxx opcode. Just store the |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2107 | approriate value as a constant. If the value is negative, |
| 2108 | extend the string containing the constant and insert a |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2109 | negative in the 0th position -- unless we're doing unary minus |
| 2110 | of a floating zero! In that case the sign is significant, but |
| 2111 | the const dict can't distinguish +0.0 from -0.0. |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2112 | */ |
| 2113 | if ((childtype == PLUS || childtype == MINUS || childtype == TILDE) |
Fred Drake | 14ef244 | 2001-08-30 18:53:25 +0000 | [diff] [blame] | 2114 | && NCH(n) == 2 |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2115 | && TYPE((pfactor = CHILD(n, 1))) == factor |
| 2116 | && NCH(pfactor) == 1 |
| 2117 | && TYPE((ppower = CHILD(pfactor, 0))) == power |
| 2118 | && NCH(ppower) == 1 |
| 2119 | && TYPE((patom = CHILD(ppower, 0))) == atom |
| 2120 | && TYPE((pnum = CHILD(patom, 0))) == NUMBER |
| 2121 | && !(childtype == MINUS && is_float_zero(STR(pnum)))) { |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2122 | if (childtype == TILDE) { |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2123 | com_invert_constant(c, pnum); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2124 | return; |
| 2125 | } |
| 2126 | if (childtype == MINUS) { |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 2127 | char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2128 | if (s == NULL) { |
| 2129 | com_error(c, PyExc_MemoryError, ""); |
| 2130 | com_addbyte(c, 255); |
| 2131 | return; |
| 2132 | } |
| 2133 | s[0] = '-'; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2134 | strcpy(s + 1, STR(pnum)); |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 2135 | PyObject_FREE(STR(pnum)); |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2136 | STR(pnum) = s; |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2137 | } |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2138 | com_atom(c, patom); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2139 | } |
| 2140 | else if (childtype == PLUS) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2141 | com_factor(c, CHILD(n, 1)); |
| 2142 | com_addbyte(c, UNARY_POSITIVE); |
| 2143 | } |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2144 | else if (childtype == MINUS) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2145 | com_factor(c, CHILD(n, 1)); |
| 2146 | com_addbyte(c, UNARY_NEGATIVE); |
| 2147 | } |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2148 | else if (childtype == TILDE) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2149 | com_factor(c, CHILD(n, 1)); |
| 2150 | com_addbyte(c, UNARY_INVERT); |
| 2151 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2152 | else { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2153 | com_power(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2154 | } |
| 2155 | } |
| 2156 | |
| 2157 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2158 | com_term(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2159 | { |
| 2160 | int i; |
| 2161 | int op; |
| 2162 | REQ(n, term); |
| 2163 | com_factor(c, CHILD(n, 0)); |
| 2164 | for (i = 2; i < NCH(n); i += 2) { |
| 2165 | com_factor(c, CHILD(n, i)); |
| 2166 | switch (TYPE(CHILD(n, i-1))) { |
| 2167 | case STAR: |
| 2168 | op = BINARY_MULTIPLY; |
| 2169 | break; |
| 2170 | case SLASH: |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2171 | if (c->c_flags & CO_FUTURE_DIVISION) |
| 2172 | op = BINARY_TRUE_DIVIDE; |
| 2173 | else |
| 2174 | op = BINARY_DIVIDE; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2175 | break; |
| 2176 | case PERCENT: |
| 2177 | op = BINARY_MODULO; |
| 2178 | break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2179 | case DOUBLESLASH: |
| 2180 | op = BINARY_FLOOR_DIVIDE; |
| 2181 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2182 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2183 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2184 | "com_term: operator not *, /, // or %"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2185 | op = 255; |
| 2186 | } |
| 2187 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2188 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2189 | } |
| 2190 | } |
| 2191 | |
| 2192 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2193 | com_arith_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2194 | { |
| 2195 | int i; |
| 2196 | int op; |
| 2197 | REQ(n, arith_expr); |
| 2198 | com_term(c, CHILD(n, 0)); |
| 2199 | for (i = 2; i < NCH(n); i += 2) { |
| 2200 | com_term(c, CHILD(n, i)); |
| 2201 | switch (TYPE(CHILD(n, i-1))) { |
| 2202 | case PLUS: |
| 2203 | op = BINARY_ADD; |
| 2204 | break; |
| 2205 | case MINUS: |
| 2206 | op = BINARY_SUBTRACT; |
| 2207 | break; |
| 2208 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2209 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2210 | "com_arith_expr: operator not + or -"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2211 | op = 255; |
| 2212 | } |
| 2213 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2214 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2215 | } |
| 2216 | } |
| 2217 | |
| 2218 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2219 | com_shift_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2220 | { |
| 2221 | int i; |
| 2222 | int op; |
| 2223 | REQ(n, shift_expr); |
| 2224 | com_arith_expr(c, CHILD(n, 0)); |
| 2225 | for (i = 2; i < NCH(n); i += 2) { |
| 2226 | com_arith_expr(c, CHILD(n, i)); |
| 2227 | switch (TYPE(CHILD(n, i-1))) { |
| 2228 | case LEFTSHIFT: |
| 2229 | op = BINARY_LSHIFT; |
| 2230 | break; |
| 2231 | case RIGHTSHIFT: |
| 2232 | op = BINARY_RSHIFT; |
| 2233 | break; |
| 2234 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2235 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2236 | "com_shift_expr: operator not << or >>"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2237 | op = 255; |
| 2238 | } |
| 2239 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2240 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2241 | } |
| 2242 | } |
| 2243 | |
| 2244 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2245 | com_and_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2246 | { |
| 2247 | int i; |
| 2248 | int op; |
| 2249 | REQ(n, and_expr); |
| 2250 | com_shift_expr(c, CHILD(n, 0)); |
| 2251 | for (i = 2; i < NCH(n); i += 2) { |
| 2252 | com_shift_expr(c, CHILD(n, i)); |
| 2253 | if (TYPE(CHILD(n, i-1)) == AMPER) { |
| 2254 | op = BINARY_AND; |
| 2255 | } |
| 2256 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2257 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2258 | "com_and_expr: operator not &"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2259 | op = 255; |
| 2260 | } |
| 2261 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2262 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2263 | } |
| 2264 | } |
| 2265 | |
| 2266 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2267 | com_xor_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2268 | { |
| 2269 | int i; |
| 2270 | int op; |
| 2271 | REQ(n, xor_expr); |
| 2272 | com_and_expr(c, CHILD(n, 0)); |
| 2273 | for (i = 2; i < NCH(n); i += 2) { |
| 2274 | com_and_expr(c, CHILD(n, i)); |
| 2275 | if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) { |
| 2276 | op = BINARY_XOR; |
| 2277 | } |
| 2278 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2279 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2280 | "com_xor_expr: operator not ^"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2281 | op = 255; |
| 2282 | } |
| 2283 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2284 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2285 | } |
| 2286 | } |
| 2287 | |
| 2288 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2289 | com_expr(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2290 | { |
| 2291 | int i; |
| 2292 | int op; |
| 2293 | REQ(n, expr); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2294 | com_xor_expr(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2295 | for (i = 2; i < NCH(n); i += 2) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2296 | com_xor_expr(c, CHILD(n, i)); |
| 2297 | if (TYPE(CHILD(n, i-1)) == VBAR) { |
| 2298 | op = BINARY_OR; |
| 2299 | } |
| 2300 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2301 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2302 | "com_expr: expr operator not |"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2303 | op = 255; |
| 2304 | } |
| 2305 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2306 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2307 | } |
| 2308 | } |
| 2309 | |
| 2310 | static enum cmp_op |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2311 | cmp_type(node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2312 | { |
| 2313 | REQ(n, comp_op); |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2314 | /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '==' |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2315 | | 'in' | 'not' 'in' | 'is' | 'is' not' */ |
| 2316 | if (NCH(n) == 1) { |
| 2317 | n = CHILD(n, 0); |
| 2318 | switch (TYPE(n)) { |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2319 | case LESS: return PyCmp_LT; |
| 2320 | case GREATER: return PyCmp_GT; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2321 | case EQEQUAL: /* == */ |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2322 | case EQUAL: return PyCmp_EQ; |
| 2323 | case LESSEQUAL: return PyCmp_LE; |
| 2324 | case GREATEREQUAL: return PyCmp_GE; |
| 2325 | case NOTEQUAL: return PyCmp_NE; /* <> or != */ |
| 2326 | case NAME: if (strcmp(STR(n), "in") == 0) return PyCmp_IN; |
| 2327 | if (strcmp(STR(n), "is") == 0) return PyCmp_IS; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2328 | } |
| 2329 | } |
| 2330 | else if (NCH(n) == 2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2331 | switch (TYPE(CHILD(n, 0))) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2332 | case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2333 | return PyCmp_NOT_IN; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2334 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2335 | return PyCmp_IS_NOT; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2336 | } |
| 2337 | } |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2338 | return PyCmp_BAD; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2339 | } |
| 2340 | |
| 2341 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2342 | com_comparison(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2343 | { |
| 2344 | int i; |
| 2345 | enum cmp_op op; |
| 2346 | int anchor; |
| 2347 | REQ(n, comparison); /* comparison: expr (comp_op expr)* */ |
| 2348 | com_expr(c, CHILD(n, 0)); |
| 2349 | if (NCH(n) == 1) |
| 2350 | return; |
| 2351 | |
| 2352 | /**************************************************************** |
| 2353 | The following code is generated for all but the last |
| 2354 | comparison in a chain: |
| 2355 | |
| 2356 | label: on stack: opcode: jump to: |
| 2357 | |
| 2358 | a <code to load b> |
| 2359 | a, b DUP_TOP |
| 2360 | a, b, b ROT_THREE |
| 2361 | b, a, b COMPARE_OP |
| 2362 | b, 0-or-1 JUMP_IF_FALSE L1 |
| 2363 | b, 1 POP_TOP |
| 2364 | b |
| 2365 | |
| 2366 | We are now ready to repeat this sequence for the next |
| 2367 | comparison in the chain. |
| 2368 | |
| 2369 | For the last we generate: |
| 2370 | |
| 2371 | b <code to load c> |
| 2372 | b, c COMPARE_OP |
| 2373 | 0-or-1 |
| 2374 | |
| 2375 | If there were any jumps to L1 (i.e., there was more than one |
| 2376 | comparison), we generate: |
| 2377 | |
| 2378 | 0-or-1 JUMP_FORWARD L2 |
| 2379 | L1: b, 0 ROT_TWO |
| 2380 | 0, b POP_TOP |
| 2381 | 0 |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2382 | L2: 0-or-1 |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2383 | ****************************************************************/ |
| 2384 | |
| 2385 | anchor = 0; |
| 2386 | |
| 2387 | for (i = 2; i < NCH(n); i += 2) { |
| 2388 | com_expr(c, CHILD(n, i)); |
| 2389 | if (i+2 < NCH(n)) { |
| 2390 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2391 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2392 | com_addbyte(c, ROT_THREE); |
| 2393 | } |
| 2394 | op = cmp_type(CHILD(n, i-1)); |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2395 | if (op == PyCmp_BAD) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2396 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2397 | "com_comparison: unknown comparison op"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2398 | } |
| 2399 | com_addoparg(c, COMPARE_OP, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2400 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2401 | if (i+2 < NCH(n)) { |
| 2402 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2403 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2404 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2405 | } |
| 2406 | } |
| 2407 | |
| 2408 | if (anchor) { |
| 2409 | int anchor2 = 0; |
| 2410 | com_addfwref(c, JUMP_FORWARD, &anchor2); |
| 2411 | com_backpatch(c, anchor); |
| 2412 | com_addbyte(c, ROT_TWO); |
| 2413 | com_addbyte(c, POP_TOP); |
| 2414 | com_backpatch(c, anchor2); |
| 2415 | } |
| 2416 | } |
| 2417 | |
| 2418 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2419 | com_not_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2420 | { |
| 2421 | REQ(n, not_test); /* 'not' not_test | comparison */ |
| 2422 | if (NCH(n) == 1) { |
| 2423 | com_comparison(c, CHILD(n, 0)); |
| 2424 | } |
| 2425 | else { |
| 2426 | com_not_test(c, CHILD(n, 1)); |
| 2427 | com_addbyte(c, UNARY_NOT); |
| 2428 | } |
| 2429 | } |
| 2430 | |
| 2431 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2432 | com_and_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2433 | { |
| 2434 | int i; |
| 2435 | int anchor; |
| 2436 | REQ(n, and_test); /* not_test ('and' not_test)* */ |
| 2437 | anchor = 0; |
| 2438 | i = 0; |
| 2439 | for (;;) { |
| 2440 | com_not_test(c, CHILD(n, i)); |
| 2441 | if ((i += 2) >= NCH(n)) |
| 2442 | break; |
| 2443 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2444 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2445 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2446 | } |
| 2447 | if (anchor) |
| 2448 | com_backpatch(c, anchor); |
| 2449 | } |
| 2450 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2451 | static int |
| 2452 | com_make_closure(struct compiling *c, PyCodeObject *co) |
| 2453 | { |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 2454 | int i, free = PyCode_GetNumFree(co); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2455 | if (free == 0) |
| 2456 | return 0; |
| 2457 | for (i = 0; i < free; ++i) { |
| 2458 | /* Bypass com_addop_varname because it will generate |
| 2459 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 2460 | */ |
| 2461 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 2462 | int arg, reftype; |
| 2463 | |
| 2464 | /* Special case: If a class contains a method with a |
| 2465 | free variable that has the same name as a method, |
| 2466 | the name will be considered free *and* local in the |
| 2467 | class. It should be handled by the closure, as |
| 2468 | well as by the normal name loookup logic. |
| 2469 | */ |
| 2470 | reftype = get_ref_type(c, PyString_AS_STRING(name)); |
| 2471 | if (reftype == CELL) |
| 2472 | arg = com_lookup_arg(c->c_cellvars, name); |
| 2473 | else /* (reftype == FREE) */ |
| 2474 | arg = com_lookup_arg(c->c_freevars, name); |
| 2475 | if (arg == -1) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 2476 | fprintf(stderr, "lookup %s in %s %d %d\n" |
| 2477 | "freevars of %s: %s\n", |
| 2478 | PyObject_REPR(name), |
| 2479 | c->c_name, |
| 2480 | reftype, arg, |
| 2481 | PyString_AS_STRING(co->co_name), |
| 2482 | PyObject_REPR(co->co_freevars)); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2483 | Py_FatalError("com_make_closure()"); |
| 2484 | } |
| 2485 | com_addoparg(c, LOAD_CLOSURE, arg); |
| 2486 | |
| 2487 | } |
| 2488 | com_push(c, free); |
| 2489 | return 1; |
| 2490 | } |
| 2491 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2492 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2493 | com_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2494 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2495 | REQ(n, test); /* and_test ('or' and_test)* | lambdef */ |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2496 | if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2497 | PyCodeObject *co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2498 | int i, closure; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2499 | int ndefs = com_argdefs(c, CHILD(n, 0)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 2500 | symtable_enter_scope(c->c_symtable, "lambda", lambdef, |
| 2501 | n->n_lineno); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2502 | co = icompile(CHILD(n, 0), c); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2503 | if (co == NULL) { |
| 2504 | c->c_errors++; |
| 2505 | return; |
| 2506 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2507 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2508 | i = com_addconst(c, (PyObject *)co); |
| 2509 | closure = com_make_closure(c, co); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2510 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2511 | com_push(c, 1); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2512 | if (closure) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2513 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 2514 | com_pop(c, PyCode_GetNumFree(co)); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2515 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2516 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2517 | Py_DECREF(co); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2518 | com_pop(c, ndefs); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2519 | } |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2520 | else { |
| 2521 | int anchor = 0; |
| 2522 | int i = 0; |
| 2523 | for (;;) { |
| 2524 | com_and_test(c, CHILD(n, i)); |
| 2525 | if ((i += 2) >= NCH(n)) |
| 2526 | break; |
| 2527 | com_addfwref(c, JUMP_IF_TRUE, &anchor); |
| 2528 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2529 | com_pop(c, 1); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2530 | } |
| 2531 | if (anchor) |
| 2532 | com_backpatch(c, anchor); |
| 2533 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2537 | com_list(struct compiling *c, node *n, int toplevel) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2538 | { |
| 2539 | /* exprlist: expr (',' expr)* [',']; likewise for testlist */ |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 2540 | if (NCH(n) == 1 && !toplevel) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2541 | com_node(c, CHILD(n, 0)); |
| 2542 | } |
| 2543 | else { |
| 2544 | int i; |
| 2545 | int len; |
| 2546 | len = (NCH(n) + 1) / 2; |
| 2547 | for (i = 0; i < NCH(n); i += 2) |
| 2548 | com_node(c, CHILD(n, i)); |
| 2549 | com_addoparg(c, BUILD_TUPLE, len); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2550 | com_pop(c, len-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2551 | } |
| 2552 | } |
| 2553 | |
| 2554 | |
| 2555 | /* Begin of assignment compilation */ |
| 2556 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2557 | |
| 2558 | static void |
| 2559 | com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn) |
| 2560 | { |
| 2561 | com_addbyte(c, DUP_TOP); |
| 2562 | com_push(c, 1); |
| 2563 | com_addopname(c, LOAD_ATTR, n); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2564 | com_node(c, augn); |
| 2565 | com_addbyte(c, opcode); |
| 2566 | com_pop(c, 1); |
| 2567 | com_addbyte(c, ROT_TWO); |
| 2568 | com_addopname(c, STORE_ATTR, n); |
| 2569 | com_pop(c, 2); |
| 2570 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2571 | |
| 2572 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2573 | com_assign_attr(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2574 | { |
| 2575 | com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2576 | com_pop(c, assigning ? 2 : 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2577 | } |
| 2578 | |
| 2579 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2580 | 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] | 2581 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2582 | REQ(n, trailer); |
| 2583 | switch (TYPE(CHILD(n, 0))) { |
| 2584 | case LPAR: /* '(' [exprlist] ')' */ |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 2585 | if (assigning == OP_DELETE) |
| 2586 | com_error(c, PyExc_SyntaxError, |
| 2587 | "can't delete function call"); |
| 2588 | else |
| 2589 | com_error(c, PyExc_SyntaxError, |
| 2590 | "can't assign to function call"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2591 | break; |
| 2592 | case DOT: /* '.' NAME */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2593 | if (assigning > OP_APPLY) |
| 2594 | com_augassign_attr(c, CHILD(n, 1), assigning, augn); |
| 2595 | else |
| 2596 | com_assign_attr(c, CHILD(n, 1), assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2597 | break; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2598 | case LSQB: /* '[' subscriptlist ']' */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2599 | com_subscriptlist(c, CHILD(n, 1), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2600 | break; |
| 2601 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2602 | com_error(c, PyExc_SystemError, "unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2603 | } |
| 2604 | } |
| 2605 | |
| 2606 | static void |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2607 | com_assign_sequence(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2608 | { |
| 2609 | int i; |
Thomas Wouters | b59290f | 2000-08-25 05:41:11 +0000 | [diff] [blame] | 2610 | if (TYPE(n) != testlist && TYPE(n) != listmaker) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2611 | REQ(n, exprlist); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2612 | if (assigning) { |
| 2613 | i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2614 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2615 | com_push(c, i-1); |
| 2616 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2617 | for (i = 0; i < NCH(n); i += 2) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2618 | com_assign(c, CHILD(n, i), assigning, NULL); |
| 2619 | } |
| 2620 | |
| 2621 | static void |
| 2622 | com_augassign_name(struct compiling *c, node *n, int opcode, node *augn) |
| 2623 | { |
| 2624 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2625 | com_addop_varname(c, VAR_LOAD, STR(n)); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2626 | com_push(c, 1); |
| 2627 | com_node(c, augn); |
| 2628 | com_addbyte(c, opcode); |
| 2629 | com_pop(c, 1); |
| 2630 | com_assign_name(c, n, OP_ASSIGN); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
| 2633 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2634 | com_assign_name(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2635 | { |
| 2636 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2637 | com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2638 | if (assigning) |
| 2639 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2640 | } |
| 2641 | |
| 2642 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2643 | com_assign(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2644 | { |
| 2645 | /* Loop to avoid trivial recursion */ |
| 2646 | for (;;) { |
| 2647 | switch (TYPE(n)) { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2648 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2649 | case exprlist: |
| 2650 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 2651 | case testlist1: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2652 | if (NCH(n) > 1) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2653 | if (assigning > OP_APPLY) { |
| 2654 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 2655 | "augmented assign to tuple not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2656 | return; |
| 2657 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2658 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2659 | return; |
| 2660 | } |
| 2661 | n = CHILD(n, 0); |
| 2662 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2663 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2664 | case test: |
| 2665 | case and_test: |
| 2666 | case not_test: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2667 | case comparison: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2668 | case expr: |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2669 | case xor_expr: |
| 2670 | case and_expr: |
| 2671 | case shift_expr: |
| 2672 | case arith_expr: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2673 | case term: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2674 | case factor: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2675 | if (NCH(n) > 1) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2676 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2677 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2678 | return; |
| 2679 | } |
| 2680 | n = CHILD(n, 0); |
| 2681 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2682 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2683 | case power: /* atom trailer* ('**' power)* |
| 2684 | ('+'|'-'|'~') factor | atom trailer* */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2685 | if (TYPE(CHILD(n, 0)) != atom) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2686 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2687 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2688 | return; |
| 2689 | } |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2690 | if (NCH(n) > 1) { /* trailer or exponent present */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2691 | int i; |
| 2692 | com_node(c, CHILD(n, 0)); |
| 2693 | for (i = 1; i+1 < NCH(n); i++) { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2694 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2695 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2696 | "can't assign to operator"); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2697 | return; |
| 2698 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2699 | com_apply_trailer(c, CHILD(n, i)); |
| 2700 | } /* NB i is still alive */ |
| 2701 | com_assign_trailer(c, |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2702 | CHILD(n, i), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2703 | return; |
| 2704 | } |
| 2705 | n = CHILD(n, 0); |
| 2706 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2707 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2708 | case atom: |
| 2709 | switch (TYPE(CHILD(n, 0))) { |
| 2710 | case LPAR: |
| 2711 | n = CHILD(n, 1); |
| 2712 | if (TYPE(n) == RPAR) { |
| 2713 | /* XXX Should allow () = () ??? */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2714 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2715 | "can't assign to ()"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2716 | return; |
| 2717 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2718 | if (assigning > OP_APPLY) { |
| 2719 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2720 | "augmented assign to tuple not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2721 | return; |
| 2722 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2723 | break; |
| 2724 | case LSQB: |
| 2725 | n = CHILD(n, 1); |
| 2726 | if (TYPE(n) == RSQB) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2727 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2728 | "can't assign to []"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2729 | return; |
| 2730 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2731 | if (assigning > OP_APPLY) { |
| 2732 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2733 | "augmented assign to list not possible"); |
| 2734 | return; |
| 2735 | } |
| 2736 | if (NCH(n) > 1 |
| 2737 | && TYPE(CHILD(n, 1)) == list_for) { |
| 2738 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2739 | "can't assign to list comprehension"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2740 | return; |
| 2741 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2742 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2743 | return; |
| 2744 | case NAME: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2745 | if (assigning > OP_APPLY) |
| 2746 | com_augassign_name(c, CHILD(n, 0), |
| 2747 | assigning, augn); |
| 2748 | else |
| 2749 | com_assign_name(c, CHILD(n, 0), |
| 2750 | assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2751 | return; |
| 2752 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2753 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2754 | "can't assign to literal"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2755 | return; |
| 2756 | } |
| 2757 | break; |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2758 | |
| 2759 | case lambdef: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2760 | com_error(c, PyExc_SyntaxError, |
| 2761 | "can't assign to lambda"); |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2762 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2763 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2764 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2765 | com_error(c, PyExc_SystemError, |
| 2766 | "com_assign: bad node"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2767 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2768 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2769 | } |
| 2770 | } |
| 2771 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2772 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2773 | static void |
| 2774 | com_augassign(struct compiling *c, node *n) |
| 2775 | { |
| 2776 | int opcode; |
| 2777 | |
| 2778 | switch (STR(CHILD(CHILD(n, 1), 0))[0]) { |
| 2779 | case '+': opcode = INPLACE_ADD; break; |
| 2780 | case '-': opcode = INPLACE_SUBTRACT; break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2781 | case '/': |
| 2782 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '/') |
| 2783 | opcode = INPLACE_FLOOR_DIVIDE; |
| 2784 | else if (c->c_flags & CO_FUTURE_DIVISION) |
| 2785 | opcode = INPLACE_TRUE_DIVIDE; |
| 2786 | else |
| 2787 | opcode = INPLACE_DIVIDE; |
| 2788 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2789 | case '%': opcode = INPLACE_MODULO; break; |
| 2790 | case '<': opcode = INPLACE_LSHIFT; break; |
| 2791 | case '>': opcode = INPLACE_RSHIFT; break; |
| 2792 | case '&': opcode = INPLACE_AND; break; |
| 2793 | case '^': opcode = INPLACE_XOR; break; |
| 2794 | case '|': opcode = INPLACE_OR; break; |
| 2795 | case '*': |
| 2796 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '*') |
| 2797 | opcode = INPLACE_POWER; |
| 2798 | else |
| 2799 | opcode = INPLACE_MULTIPLY; |
| 2800 | break; |
| 2801 | default: |
| 2802 | com_error(c, PyExc_SystemError, "com_augassign: bad operator"); |
| 2803 | return; |
| 2804 | } |
| 2805 | com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2)); |
| 2806 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2807 | |
| 2808 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2809 | com_expr_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2810 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2811 | REQ(n, expr_stmt); |
| 2812 | /* testlist (('=' testlist)* | augassign testlist) */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2813 | /* Forget it if we have just a doc string here */ |
Guido van Rossum | 5f5e817 | 1997-04-06 03:41:40 +0000 | [diff] [blame] | 2814 | if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2815 | return; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2816 | if (NCH(n) == 1) { |
| 2817 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2818 | if (c->c_interactive) |
| 2819 | com_addbyte(c, PRINT_EXPR); |
| 2820 | else |
| 2821 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2822 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2823 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2824 | else if (TYPE(CHILD(n,1)) == augassign) |
| 2825 | com_augassign(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2826 | else { |
| 2827 | int i; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2828 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2829 | for (i = 0; i < NCH(n)-2; i+=2) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2830 | if (i+2 < NCH(n)-2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2831 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2832 | com_push(c, 1); |
| 2833 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2834 | com_assign(c, CHILD(n, i), OP_ASSIGN, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2835 | } |
| 2836 | } |
| 2837 | } |
| 2838 | |
| 2839 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2840 | com_assert_stmt(struct compiling *c, node *n) |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2841 | { |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2842 | int a = 0; |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2843 | int i; |
| 2844 | REQ(n, assert_stmt); /* 'assert' test [',' test] */ |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2845 | if (Py_OptimizeFlag) |
| 2846 | return; |
| 2847 | /* Generate code like |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2848 | |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2849 | if not <test>: |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2850 | raise AssertionError [, <message>] |
| 2851 | |
| 2852 | where <message> is the second test, if present. |
| 2853 | */ |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2854 | com_node(c, CHILD(n, 1)); |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2855 | com_addfwref(c, JUMP_IF_TRUE, &a); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2856 | com_addbyte(c, POP_TOP); |
| 2857 | com_pop(c, 1); |
| 2858 | /* Raise that exception! */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2859 | com_addop_name(c, LOAD_GLOBAL, "AssertionError"); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2860 | com_push(c, 1); |
| 2861 | i = NCH(n)/2; /* Either 2 or 4 */ |
| 2862 | if (i > 1) |
| 2863 | com_node(c, CHILD(n, 3)); |
| 2864 | com_addoparg(c, RAISE_VARARGS, i); |
| 2865 | com_pop(c, i); |
| 2866 | /* The interpreter does not fall through */ |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2867 | /* Jump ends up here */ |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2868 | com_backpatch(c, a); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2869 | com_addbyte(c, POP_TOP); |
| 2870 | } |
| 2871 | |
| 2872 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2873 | com_print_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2874 | { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2875 | int i = 1; |
| 2876 | node* stream = NULL; |
| 2877 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2878 | REQ(n, print_stmt); /* 'print' (test ',')* [test] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2879 | |
| 2880 | /* are we using the extended print form? */ |
| 2881 | if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) { |
| 2882 | stream = CHILD(n, 2); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2883 | com_node(c, stream); |
| 2884 | /* stack: [...] => [... stream] */ |
| 2885 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2886 | if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA) |
| 2887 | i = 4; |
| 2888 | else |
| 2889 | i = 3; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2890 | } |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2891 | for (; i < NCH(n); i += 2) { |
| 2892 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2893 | com_addbyte(c, DUP_TOP); |
| 2894 | /* stack: [stream] => [stream stream] */ |
| 2895 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2896 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2897 | /* stack: [stream stream] => [stream stream obj] */ |
| 2898 | com_addbyte(c, ROT_TWO); |
| 2899 | /* stack: [stream stream obj] => [stream obj stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2900 | com_addbyte(c, PRINT_ITEM_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2901 | /* stack: [stream obj stream] => [stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2902 | com_pop(c, 2); |
| 2903 | } |
| 2904 | else { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2905 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2906 | /* stack: [...] => [... obj] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2907 | com_addbyte(c, PRINT_ITEM); |
| 2908 | com_pop(c, 1); |
| 2909 | } |
| 2910 | } |
| 2911 | /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */ |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2912 | if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2913 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2914 | /* must pop the extra stream object off the stack */ |
| 2915 | com_addbyte(c, POP_TOP); |
| 2916 | /* stack: [... stream] => [...] */ |
| 2917 | com_pop(c, 1); |
| 2918 | } |
| 2919 | } |
| 2920 | else { |
| 2921 | if (stream != NULL) { |
| 2922 | /* this consumes the last stream object on stack */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2923 | com_addbyte(c, PRINT_NEWLINE_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2924 | /* stack: [... stream] => [...] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2925 | com_pop(c, 1); |
| 2926 | } |
| 2927 | else |
| 2928 | com_addbyte(c, PRINT_NEWLINE); |
| 2929 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2930 | } |
| 2931 | |
| 2932 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2933 | com_return_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2934 | { |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2935 | REQ(n, return_stmt); /* 'return' [testlist] */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2936 | if (!c->c_infunction) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2937 | com_error(c, PyExc_SyntaxError, "'return' outside function"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2938 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2939 | if (c->c_flags & CO_GENERATOR) { |
| 2940 | if (NCH(n) > 1) { |
| 2941 | com_error(c, PyExc_SyntaxError, |
| 2942 | "'return' with argument inside generator"); |
| 2943 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 2944 | } |
| 2945 | if (NCH(n) < 2) { |
| 2946 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2947 | com_push(c, 1); |
| 2948 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 2949 | else |
| 2950 | com_node(c, CHILD(n, 1)); |
| 2951 | com_addbyte(c, RETURN_VALUE); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2952 | com_pop(c, 1); |
| 2953 | } |
| 2954 | |
| 2955 | static void |
| 2956 | com_yield_stmt(struct compiling *c, node *n) |
| 2957 | { |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 2958 | int i; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2959 | REQ(n, yield_stmt); /* 'yield' testlist */ |
| 2960 | if (!c->c_infunction) { |
| 2961 | com_error(c, PyExc_SyntaxError, "'yield' outside function"); |
| 2962 | } |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 2963 | |
| 2964 | for (i = 0; i < c->c_nblocks; ++i) { |
| 2965 | if (c->c_block[i] == SETUP_FINALLY) { |
| 2966 | com_error(c, PyExc_SyntaxError, |
| 2967 | "'yield' not allowed in a 'try' block " |
| 2968 | "with a 'finally' clause"); |
| 2969 | return; |
| 2970 | } |
| 2971 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2972 | com_node(c, CHILD(n, 1)); |
| 2973 | com_addbyte(c, YIELD_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2974 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2975 | } |
| 2976 | |
| 2977 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2978 | com_raise_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2979 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2980 | int i; |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 2981 | REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */ |
| 2982 | if (NCH(n) > 1) { |
| 2983 | com_node(c, CHILD(n, 1)); |
| 2984 | if (NCH(n) > 3) { |
| 2985 | com_node(c, CHILD(n, 3)); |
| 2986 | if (NCH(n) > 5) |
| 2987 | com_node(c, CHILD(n, 5)); |
| 2988 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2989 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2990 | i = NCH(n)/2; |
| 2991 | com_addoparg(c, RAISE_VARARGS, i); |
| 2992 | com_pop(c, i); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
| 2995 | static void |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2996 | com_from_import(struct compiling *c, node *n) |
| 2997 | { |
| 2998 | com_addopname(c, IMPORT_FROM, CHILD(n, 0)); |
| 2999 | com_push(c, 1); |
| 3000 | if (NCH(n) > 1) { |
| 3001 | if (strcmp(STR(CHILD(n, 1)), "as") != 0) { |
| 3002 | com_error(c, PyExc_SyntaxError, "invalid syntax"); |
| 3003 | return; |
| 3004 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3005 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3006 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3007 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3008 | com_pop(c, 1); |
| 3009 | } |
| 3010 | |
| 3011 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3012 | com_import_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3013 | { |
| 3014 | int i; |
| 3015 | REQ(n, import_stmt); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3016 | /* 'import' dotted_name (',' dotted_name)* | |
| 3017 | 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3018 | if (STR(CHILD(n, 0))[0] == 'f') { |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 3019 | PyObject *tup; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3020 | /* 'from' dotted_name 'import' ... */ |
| 3021 | REQ(CHILD(n, 1), dotted_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 3022 | |
| 3023 | if (TYPE(CHILD(n, 3)) == STAR) { |
| 3024 | tup = Py_BuildValue("(s)", "*"); |
| 3025 | } else { |
| 3026 | tup = PyTuple_New((NCH(n) - 2)/2); |
| 3027 | for (i = 3; i < NCH(n); i += 2) { |
| 3028 | PyTuple_SET_ITEM(tup, (i-3)/2, |
| 3029 | PyString_FromString(STR( |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 3030 | CHILD(CHILD(n, i), 0)))); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 3031 | } |
| 3032 | } |
| 3033 | com_addoparg(c, LOAD_CONST, com_addconst(c, tup)); |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 3034 | Py_DECREF(tup); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3035 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 3036 | com_addopname(c, IMPORT_NAME, CHILD(n, 1)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3037 | if (TYPE(CHILD(n, 3)) == STAR) |
| 3038 | com_addbyte(c, IMPORT_STAR); |
| 3039 | else { |
| 3040 | for (i = 3; i < NCH(n); i += 2) |
| 3041 | com_from_import(c, CHILD(n, i)); |
| 3042 | com_addbyte(c, POP_TOP); |
| 3043 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3044 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3045 | } |
| 3046 | else { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3047 | /* 'import' ... */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3048 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3049 | node *subn = CHILD(n, i); |
| 3050 | REQ(subn, dotted_as_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 3051 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3052 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 3053 | com_addopname(c, IMPORT_NAME, CHILD(subn, 0)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3054 | if (NCH(subn) > 1) { |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 3055 | int j; |
| 3056 | if (strcmp(STR(CHILD(subn, 1)), "as") != 0) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3057 | com_error(c, PyExc_SyntaxError, |
| 3058 | "invalid syntax"); |
| 3059 | return; |
| 3060 | } |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 3061 | for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2) |
| 3062 | com_addopname(c, LOAD_ATTR, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3063 | CHILD(CHILD(subn, 0), |
| 3064 | j)); |
| 3065 | com_addop_varname(c, VAR_STORE, |
| 3066 | STR(CHILD(subn, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3067 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3068 | com_addop_varname(c, VAR_STORE, |
| 3069 | STR(CHILD(CHILD(subn, 0), |
| 3070 | 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3071 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3072 | } |
| 3073 | } |
| 3074 | } |
| 3075 | |
| 3076 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3077 | com_exec_stmt(struct compiling *c, node *n) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3078 | { |
| 3079 | REQ(n, exec_stmt); |
| 3080 | /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */ |
| 3081 | com_node(c, CHILD(n, 1)); |
| 3082 | if (NCH(n) >= 4) |
| 3083 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3084 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3085 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3086 | com_push(c, 1); |
| 3087 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3088 | if (NCH(n) >= 6) |
| 3089 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3090 | else { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3091 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3092 | com_push(c, 1); |
| 3093 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3094 | com_addbyte(c, EXEC_STMT); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3095 | com_pop(c, 3); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3096 | } |
| 3097 | |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3098 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3099 | is_constant_false(struct compiling *c, node *n) |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3100 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3101 | PyObject *v; |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3102 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3103 | /* argument c will be NULL when called from symtable_node() */ |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3104 | |
| 3105 | /* Label to avoid tail recursion */ |
| 3106 | next: |
| 3107 | switch (TYPE(n)) { |
| 3108 | |
| 3109 | case suite: |
| 3110 | if (NCH(n) == 1) { |
| 3111 | n = CHILD(n, 0); |
| 3112 | goto next; |
| 3113 | } |
| 3114 | /* Fall through */ |
| 3115 | case file_input: |
| 3116 | for (i = 0; i < NCH(n); i++) { |
| 3117 | node *ch = CHILD(n, i); |
| 3118 | if (TYPE(ch) == stmt) { |
| 3119 | n = ch; |
| 3120 | goto next; |
| 3121 | } |
| 3122 | } |
| 3123 | break; |
| 3124 | |
| 3125 | case stmt: |
| 3126 | case simple_stmt: |
| 3127 | case small_stmt: |
| 3128 | n = CHILD(n, 0); |
| 3129 | goto next; |
| 3130 | |
| 3131 | case expr_stmt: |
| 3132 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3133 | case testlist1: |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3134 | case test: |
| 3135 | case and_test: |
| 3136 | case not_test: |
| 3137 | case comparison: |
| 3138 | case expr: |
| 3139 | case xor_expr: |
| 3140 | case and_expr: |
| 3141 | case shift_expr: |
| 3142 | case arith_expr: |
| 3143 | case term: |
| 3144 | case factor: |
| 3145 | case power: |
| 3146 | case atom: |
| 3147 | if (NCH(n) == 1) { |
| 3148 | n = CHILD(n, 0); |
| 3149 | goto next; |
| 3150 | } |
| 3151 | break; |
| 3152 | |
| 3153 | case NAME: |
| 3154 | if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0) |
| 3155 | return 1; |
| 3156 | break; |
| 3157 | |
| 3158 | case NUMBER: |
| 3159 | v = parsenumber(c, STR(n)); |
| 3160 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3161 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3162 | break; |
| 3163 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3164 | i = PyObject_IsTrue(v); |
| 3165 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3166 | return i == 0; |
| 3167 | |
| 3168 | case STRING: |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3169 | v = parsestr(c, STR(n)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3170 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3171 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3172 | break; |
| 3173 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3174 | i = PyObject_IsTrue(v); |
| 3175 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3176 | return i == 0; |
| 3177 | |
| 3178 | } |
| 3179 | return 0; |
| 3180 | } |
| 3181 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3182 | |
| 3183 | /* Look under n for a return stmt with an expression. |
| 3184 | * This hack is used to find illegal returns under "if 0:" blocks in |
| 3185 | * functions already known to be generators (as determined by the symtable |
| 3186 | * pass). |
| 3187 | * Return the offending return node if found, else NULL. |
| 3188 | */ |
| 3189 | static node * |
| 3190 | look_for_offending_return(node *n) |
| 3191 | { |
| 3192 | int i; |
| 3193 | |
| 3194 | for (i = 0; i < NCH(n); ++i) { |
| 3195 | node *kid = CHILD(n, i); |
| 3196 | |
| 3197 | switch (TYPE(kid)) { |
| 3198 | case classdef: |
| 3199 | case funcdef: |
| 3200 | case lambdef: |
| 3201 | /* Stuff in nested functions & classes doesn't |
| 3202 | affect the code block we started in. */ |
| 3203 | return NULL; |
| 3204 | |
| 3205 | case return_stmt: |
| 3206 | if (NCH(kid) > 1) |
| 3207 | return kid; |
| 3208 | break; |
| 3209 | |
| 3210 | default: { |
| 3211 | node *bad = look_for_offending_return(kid); |
| 3212 | if (bad != NULL) |
| 3213 | return bad; |
| 3214 | } |
| 3215 | } |
| 3216 | } |
| 3217 | |
| 3218 | return NULL; |
| 3219 | } |
| 3220 | |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3221 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3222 | com_if_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3223 | { |
| 3224 | int i; |
| 3225 | int anchor = 0; |
| 3226 | REQ(n, if_stmt); |
| 3227 | /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */ |
| 3228 | for (i = 0; i+3 < NCH(n); i+=4) { |
| 3229 | int a = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3230 | node *ch = CHILD(n, i+1); |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3231 | if (is_constant_false(c, ch)) { |
| 3232 | /* We're going to skip this block. However, if this |
| 3233 | is a generator, we have to check the dead code |
| 3234 | anyway to make sure there aren't any return stmts |
| 3235 | with expressions, in the same scope. */ |
| 3236 | if (c->c_flags & CO_GENERATOR) { |
| 3237 | node *p = look_for_offending_return(n); |
| 3238 | if (p != NULL) { |
| 3239 | int savelineno = c->c_lineno; |
| 3240 | c->c_lineno = p->n_lineno; |
| 3241 | com_error(c, PyExc_SyntaxError, |
| 3242 | "'return' with argument " |
| 3243 | "inside generator"); |
| 3244 | c->c_lineno = savelineno; |
| 3245 | } |
| 3246 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3247 | continue; |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3248 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3249 | if (i > 0) |
| 3250 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3251 | com_node(c, ch); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3252 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 3253 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3254 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3255 | com_node(c, CHILD(n, i+3)); |
| 3256 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 3257 | com_backpatch(c, a); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3258 | /* We jump here with an extra entry which we now pop */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3259 | com_addbyte(c, POP_TOP); |
| 3260 | } |
| 3261 | if (i+2 < NCH(n)) |
| 3262 | com_node(c, CHILD(n, i+2)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3263 | if (anchor) |
| 3264 | com_backpatch(c, anchor); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3265 | } |
| 3266 | |
| 3267 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3268 | com_while_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3269 | { |
| 3270 | int break_anchor = 0; |
| 3271 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3272 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3273 | REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */ |
| 3274 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3275 | block_push(c, SETUP_LOOP); |
| 3276 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3277 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3278 | com_node(c, CHILD(n, 1)); |
| 3279 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 3280 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3281 | com_pop(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3282 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3283 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3284 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3285 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3286 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3287 | com_backpatch(c, anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3288 | /* We jump here with one entry more on the stack */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3289 | com_addbyte(c, POP_TOP); |
| 3290 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3291 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3292 | if (NCH(n) > 4) |
| 3293 | com_node(c, CHILD(n, 6)); |
| 3294 | com_backpatch(c, break_anchor); |
| 3295 | } |
| 3296 | |
| 3297 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3298 | com_for_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3299 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3300 | int break_anchor = 0; |
| 3301 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3302 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3303 | REQ(n, for_stmt); |
| 3304 | /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */ |
| 3305 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3306 | block_push(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3307 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3308 | com_addbyte(c, GET_ITER); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3309 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3310 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3311 | com_addfwref(c, FOR_ITER, &anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3312 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3313 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3314 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3315 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3316 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3317 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3318 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3319 | com_backpatch(c, anchor); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3320 | com_pop(c, 1); /* FOR_ITER has popped this */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3321 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3322 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3323 | if (NCH(n) > 8) |
| 3324 | com_node(c, CHILD(n, 8)); |
| 3325 | com_backpatch(c, break_anchor); |
| 3326 | } |
| 3327 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3328 | /* Code generated for "try: S finally: Sf" is as follows: |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3329 | |
| 3330 | SETUP_FINALLY L |
| 3331 | <code for S> |
| 3332 | POP_BLOCK |
| 3333 | LOAD_CONST <nil> |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3334 | L: <code for Sf> |
| 3335 | END_FINALLY |
| 3336 | |
| 3337 | The special instructions use the block stack. Each block |
| 3338 | stack entry contains the instruction that created it (here |
| 3339 | SETUP_FINALLY), the level of the value stack at the time the |
| 3340 | block stack entry was created, and a label (here L). |
| 3341 | |
| 3342 | SETUP_FINALLY: |
| 3343 | Pushes the current value stack level and the label |
| 3344 | onto the block stack. |
| 3345 | POP_BLOCK: |
| 3346 | Pops en entry from the block stack, and pops the value |
| 3347 | stack until its level is the same as indicated on the |
| 3348 | block stack. (The label is ignored.) |
| 3349 | END_FINALLY: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3350 | Pops a variable number of entries from the *value* stack |
| 3351 | and re-raises the exception they specify. The number of |
| 3352 | entries popped depends on the (pseudo) exception type. |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3353 | |
| 3354 | The block stack is unwound when an exception is raised: |
| 3355 | when a SETUP_FINALLY entry is found, the exception is pushed |
| 3356 | onto the value stack (and the exception condition is cleared), |
| 3357 | and the interpreter jumps to the label gotten from the block |
| 3358 | stack. |
| 3359 | |
| 3360 | 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] | 3361 | (The contents of the value stack is shown in [], with the top |
| 3362 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3363 | associated value, and 'exc' the exception.) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3364 | |
| 3365 | Value stack Label Instruction Argument |
| 3366 | [] SETUP_EXCEPT L1 |
| 3367 | [] <code for S> |
| 3368 | [] POP_BLOCK |
| 3369 | [] JUMP_FORWARD L0 |
| 3370 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3371 | [tb, val, exc] L1: DUP ) |
| 3372 | [tb, val, exc, exc] <evaluate E1> ) |
| 3373 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 3374 | [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 ) |
| 3375 | [tb, val, exc, 1] POP ) |
| 3376 | [tb, val, exc] POP |
| 3377 | [tb, val] <assign to V1> (or POP if no V1) |
| 3378 | [tb] POP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3379 | [] <code for S1> |
| 3380 | JUMP_FORWARD L0 |
| 3381 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3382 | [tb, val, exc, 0] L2: POP |
| 3383 | [tb, val, exc] DUP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3384 | .............................etc....................... |
| 3385 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3386 | [tb, val, exc, 0] Ln+1: POP |
| 3387 | [tb, val, exc] END_FINALLY # re-raise exception |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3388 | |
| 3389 | [] L0: <next statement> |
| 3390 | |
| 3391 | Of course, parts are not generated if Vi or Ei is not present. |
| 3392 | */ |
| 3393 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3394 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3395 | com_try_except(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3396 | { |
| 3397 | int except_anchor = 0; |
| 3398 | int end_anchor = 0; |
| 3399 | int else_anchor = 0; |
| 3400 | int i; |
| 3401 | node *ch; |
| 3402 | |
| 3403 | com_addfwref(c, SETUP_EXCEPT, &except_anchor); |
| 3404 | block_push(c, SETUP_EXCEPT); |
| 3405 | com_node(c, CHILD(n, 2)); |
| 3406 | com_addbyte(c, POP_BLOCK); |
| 3407 | block_pop(c, SETUP_EXCEPT); |
| 3408 | com_addfwref(c, JUMP_FORWARD, &else_anchor); |
| 3409 | com_backpatch(c, except_anchor); |
| 3410 | for (i = 3; |
| 3411 | i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause; |
| 3412 | i += 3) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3413 | /* except_clause: 'except' [expr [',' var]] */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3414 | if (except_anchor == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3415 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 3416 | "default 'except:' must be last"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3417 | break; |
| 3418 | } |
| 3419 | except_anchor = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3420 | com_push(c, 3); /* tb, val, exc pushed by exception */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3421 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 3422 | if (NCH(ch) > 1) { |
| 3423 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3424 | com_push(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3425 | com_node(c, CHILD(ch, 1)); |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 3426 | com_addoparg(c, COMPARE_OP, PyCmp_EXC_MATCH); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3427 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3428 | com_addfwref(c, JUMP_IF_FALSE, &except_anchor); |
| 3429 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3430 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3431 | } |
| 3432 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3433 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3434 | if (NCH(ch) > 3) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3435 | com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3436 | else { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3437 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3438 | com_pop(c, 1); |
| 3439 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3440 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3441 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3442 | com_node(c, CHILD(n, i+2)); |
| 3443 | com_addfwref(c, JUMP_FORWARD, &end_anchor); |
| 3444 | if (except_anchor) { |
| 3445 | com_backpatch(c, except_anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3446 | /* We come in with [tb, val, exc, 0] on the |
| 3447 | stack; one pop and it's the same as |
| 3448 | expected at the start of the loop */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3449 | com_addbyte(c, POP_TOP); |
| 3450 | } |
| 3451 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3452 | /* We actually come in here with [tb, val, exc] but the |
| 3453 | END_FINALLY will zap those and jump around. |
| 3454 | The c_stacklevel does not reflect them so we need not pop |
| 3455 | anything. */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3456 | com_addbyte(c, END_FINALLY); |
| 3457 | com_backpatch(c, else_anchor); |
| 3458 | if (i < NCH(n)) |
| 3459 | com_node(c, CHILD(n, i+2)); |
| 3460 | com_backpatch(c, end_anchor); |
| 3461 | } |
| 3462 | |
| 3463 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3464 | com_try_finally(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3465 | { |
| 3466 | int finally_anchor = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3467 | node *ch; |
Guido van Rossum | 94fb82e | 1992-04-05 14:24:50 +0000 | [diff] [blame] | 3468 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3469 | com_addfwref(c, SETUP_FINALLY, &finally_anchor); |
| 3470 | block_push(c, SETUP_FINALLY); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3471 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3472 | com_addbyte(c, POP_BLOCK); |
| 3473 | block_pop(c, SETUP_FINALLY); |
| 3474 | block_push(c, END_FINALLY); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3475 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3476 | /* While the generated code pushes only one item, |
| 3477 | the try-finally handling can enter here with |
| 3478 | up to three items. OK, here are the details: |
| 3479 | 3 for an exception, 2 for RETURN, 1 for BREAK. */ |
| 3480 | com_push(c, 3); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3481 | com_backpatch(c, finally_anchor); |
| 3482 | ch = CHILD(n, NCH(n)-1); |
| 3483 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 3484 | com_node(c, ch); |
| 3485 | com_addbyte(c, END_FINALLY); |
| 3486 | block_pop(c, END_FINALLY); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3487 | com_pop(c, 3); /* Matches the com_push above */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3488 | } |
| 3489 | |
| 3490 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3491 | com_try_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3492 | { |
| 3493 | REQ(n, try_stmt); |
| 3494 | /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
| 3495 | | 'try' ':' suite 'finally' ':' suite */ |
| 3496 | if (TYPE(CHILD(n, 3)) != except_clause) |
| 3497 | com_try_finally(c, n); |
| 3498 | else |
| 3499 | com_try_except(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3500 | } |
| 3501 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3502 | static node * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3503 | get_rawdocstring(node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3504 | { |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3505 | int i; |
| 3506 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3507 | /* Label to avoid tail recursion */ |
| 3508 | next: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3509 | switch (TYPE(n)) { |
| 3510 | |
| 3511 | case suite: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3512 | if (NCH(n) == 1) { |
| 3513 | n = CHILD(n, 0); |
| 3514 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3515 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3516 | /* Fall through */ |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3517 | case file_input: |
| 3518 | for (i = 0; i < NCH(n); i++) { |
| 3519 | node *ch = CHILD(n, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3520 | if (TYPE(ch) == stmt) { |
| 3521 | n = ch; |
| 3522 | goto next; |
| 3523 | } |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3524 | } |
| 3525 | break; |
| 3526 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3527 | case stmt: |
| 3528 | case simple_stmt: |
| 3529 | case small_stmt: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3530 | n = CHILD(n, 0); |
| 3531 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3532 | |
| 3533 | case expr_stmt: |
| 3534 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3535 | case testlist1: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3536 | case test: |
| 3537 | case and_test: |
| 3538 | case not_test: |
| 3539 | case comparison: |
| 3540 | case expr: |
| 3541 | case xor_expr: |
| 3542 | case and_expr: |
| 3543 | case shift_expr: |
| 3544 | case arith_expr: |
| 3545 | case term: |
| 3546 | case factor: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3547 | case power: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3548 | if (NCH(n) == 1) { |
| 3549 | n = CHILD(n, 0); |
| 3550 | goto next; |
| 3551 | } |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3552 | break; |
| 3553 | |
| 3554 | case atom: |
| 3555 | if (TYPE(CHILD(n, 0)) == STRING) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3556 | return n; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3557 | break; |
| 3558 | |
| 3559 | } |
| 3560 | return NULL; |
| 3561 | } |
| 3562 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3563 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3564 | get_docstring(struct compiling *c, node *n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3565 | { |
Guido van Rossum | 541563e | 1999-01-28 15:08:09 +0000 | [diff] [blame] | 3566 | /* Don't generate doc-strings if run with -OO */ |
| 3567 | if (Py_OptimizeFlag > 1) |
| 3568 | return NULL; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3569 | n = get_rawdocstring(n); |
| 3570 | if (n == NULL) |
| 3571 | return NULL; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3572 | return parsestrplus(c, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3573 | } |
| 3574 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3575 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3576 | com_suite(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3577 | { |
| 3578 | REQ(n, suite); |
| 3579 | /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */ |
| 3580 | if (NCH(n) == 1) { |
| 3581 | com_node(c, CHILD(n, 0)); |
| 3582 | } |
| 3583 | else { |
| 3584 | int i; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3585 | for (i = 0; i < NCH(n) && c->c_errors == 0; i++) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3586 | node *ch = CHILD(n, i); |
| 3587 | if (TYPE(ch) == stmt) |
| 3588 | com_node(c, ch); |
| 3589 | } |
| 3590 | } |
| 3591 | } |
| 3592 | |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3593 | /* ARGSUSED */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3594 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3595 | com_continue_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3596 | { |
| 3597 | int i = c->c_nblocks; |
| 3598 | if (i-- > 0 && c->c_block[i] == SETUP_LOOP) { |
| 3599 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3600 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3601 | else if (i <= 0) { |
| 3602 | /* at the outer level */ |
| 3603 | com_error(c, PyExc_SyntaxError, |
| 3604 | "'continue' not properly in loop"); |
| 3605 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3606 | else { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3607 | int j; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3608 | for (j = i-1; j >= 0; --j) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3609 | if (c->c_block[j] == SETUP_LOOP) |
| 3610 | break; |
| 3611 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3612 | if (j >= 0) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3613 | /* there is a loop, but something interferes */ |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3614 | for (; i > j; --i) { |
| 3615 | if (c->c_block[i] == SETUP_EXCEPT || |
| 3616 | c->c_block[i] == SETUP_FINALLY) { |
| 3617 | com_addoparg(c, CONTINUE_LOOP, |
| 3618 | c->c_begin); |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3619 | return; |
| 3620 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3621 | if (c->c_block[i] == END_FINALLY) { |
| 3622 | com_error(c, PyExc_SyntaxError, |
| 3623 | "'continue' not supported inside 'finally' clause"); |
| 3624 | return; |
| 3625 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3626 | } |
| 3627 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3628 | com_error(c, PyExc_SyntaxError, |
| 3629 | "'continue' not properly in loop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3630 | } |
| 3631 | /* XXX Could allow it inside a 'finally' clause |
| 3632 | XXX if we could pop the exception still on the stack */ |
| 3633 | } |
| 3634 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3635 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3636 | com_argdefs(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3637 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3638 | int i, nch, nargs, ndefs; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3639 | if (TYPE(n) == lambdef) { |
| 3640 | /* lambdef: 'lambda' [varargslist] ':' test */ |
| 3641 | n = CHILD(n, 1); |
| 3642 | } |
| 3643 | else { |
| 3644 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */ |
| 3645 | n = CHILD(n, 2); |
| 3646 | REQ(n, parameters); /* parameters: '(' [varargslist] ')' */ |
| 3647 | n = CHILD(n, 1); |
| 3648 | } |
| 3649 | if (TYPE(n) != varargslist) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3650 | return 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3651 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3652 | (fpdef ['=' test] ',')* '*' ....... | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3653 | fpdef ['=' test] (',' fpdef ['=' test])* [','] */ |
| 3654 | nch = NCH(n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3655 | nargs = 0; |
| 3656 | ndefs = 0; |
| 3657 | for (i = 0; i < nch; i++) { |
| 3658 | int t; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3659 | if (TYPE(CHILD(n, i)) == STAR || |
| 3660 | TYPE(CHILD(n, i)) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3661 | break; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3662 | nargs++; |
| 3663 | i++; |
| 3664 | if (i >= nch) |
Guido van Rossum | a1e7e62 | 1995-09-18 21:44:04 +0000 | [diff] [blame] | 3665 | t = RPAR; /* Anything except EQUAL or COMMA */ |
| 3666 | else |
| 3667 | t = TYPE(CHILD(n, i)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3668 | if (t == EQUAL) { |
| 3669 | i++; |
| 3670 | ndefs++; |
| 3671 | com_node(c, CHILD(n, i)); |
| 3672 | i++; |
| 3673 | if (i >= nch) |
| 3674 | break; |
| 3675 | t = TYPE(CHILD(n, i)); |
| 3676 | } |
| 3677 | else { |
Guido van Rossum | 29d38cd | 1998-10-02 13:41:54 +0000 | [diff] [blame] | 3678 | /* Treat "(a=1, b)" as an error */ |
| 3679 | if (ndefs) |
| 3680 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | dfede31 | 1998-10-02 14:06:56 +0000 | [diff] [blame] | 3681 | "non-default argument follows default argument"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3682 | } |
| 3683 | if (t != COMMA) |
| 3684 | break; |
| 3685 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3686 | return ndefs; |
| 3687 | } |
| 3688 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3689 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3690 | com_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3691 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3692 | PyObject *co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3693 | int ndefs; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3694 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3695 | ndefs = com_argdefs(c, n); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3696 | symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n), |
| 3697 | n->n_lineno); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3698 | co = (PyObject *)icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3699 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3700 | if (co == NULL) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3701 | c->c_errors++; |
| 3702 | else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3703 | int closure = com_make_closure(c, (PyCodeObject *)co); |
| 3704 | int i = com_addconst(c, co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3705 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3706 | com_push(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3707 | if (closure) |
| 3708 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
| 3709 | else |
| 3710 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3711 | com_pop(c, ndefs); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3712 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3713 | com_pop(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3714 | Py_DECREF(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3715 | } |
| 3716 | } |
| 3717 | |
| 3718 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3719 | com_bases(struct compiling *c, node *n) |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3720 | { |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3721 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3722 | REQ(n, testlist); |
| 3723 | /* testlist: test (',' test)* [','] */ |
| 3724 | for (i = 0; i < NCH(n); i += 2) |
| 3725 | com_node(c, CHILD(n, i)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3726 | i = (NCH(n)+1) / 2; |
| 3727 | com_addoparg(c, BUILD_TUPLE, i); |
| 3728 | com_pop(c, i-1); |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3729 | } |
| 3730 | |
| 3731 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3732 | com_classdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3733 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3734 | int i; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3735 | PyObject *v; |
| 3736 | PyCodeObject *co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3737 | char *name; |
| 3738 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3739 | REQ(n, classdef); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3740 | /* classdef: class NAME ['(' testlist ')'] ':' suite */ |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 3741 | if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3742 | c->c_errors++; |
| 3743 | return; |
| 3744 | } |
| 3745 | /* Push the class name on the stack */ |
| 3746 | i = com_addconst(c, v); |
| 3747 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3748 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3749 | Py_DECREF(v); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3750 | /* Push the tuple of base classes on the stack */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3751 | if (TYPE(CHILD(n, 2)) != LPAR) { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3752 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3753 | com_push(c, 1); |
| 3754 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3755 | else |
| 3756 | com_bases(c, CHILD(n, 3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3757 | name = STR(CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3758 | symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3759 | co = icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3760 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3761 | if (co == NULL) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3762 | c->c_errors++; |
| 3763 | else { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3764 | int closure = com_make_closure(c, co); |
| 3765 | i = com_addconst(c, (PyObject *)co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3766 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3767 | com_push(c, 1); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3768 | if (closure) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3769 | com_addoparg(c, MAKE_CLOSURE, 0); |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 3770 | com_pop(c, PyCode_GetNumFree(co)); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3771 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3772 | com_addoparg(c, MAKE_FUNCTION, 0); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3773 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3774 | com_addbyte(c, BUILD_CLASS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3775 | com_pop(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3776 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3777 | com_pop(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3778 | Py_DECREF(co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3779 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3780 | } |
| 3781 | |
| 3782 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3783 | com_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3784 | { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3785 | loop: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3786 | if (c->c_errors) |
| 3787 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3788 | switch (TYPE(n)) { |
| 3789 | |
| 3790 | /* Definition nodes */ |
| 3791 | |
| 3792 | case funcdef: |
| 3793 | com_funcdef(c, n); |
| 3794 | break; |
| 3795 | case classdef: |
| 3796 | com_classdef(c, n); |
| 3797 | break; |
| 3798 | |
| 3799 | /* Trivial parse tree nodes */ |
| 3800 | |
| 3801 | case stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3802 | case small_stmt: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3803 | case flow_stmt: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3804 | n = CHILD(n, 0); |
| 3805 | goto loop; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3806 | |
| 3807 | case simple_stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3808 | /* small_stmt (';' small_stmt)* [';'] NEWLINE */ |
| 3809 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 3810 | { |
| 3811 | int i; |
| 3812 | for (i = 0; i < NCH(n)-1; i += 2) |
| 3813 | com_node(c, CHILD(n, i)); |
| 3814 | } |
| 3815 | break; |
| 3816 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3817 | case compound_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3818 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3819 | n = CHILD(n, 0); |
| 3820 | goto loop; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3821 | |
| 3822 | /* Statement nodes */ |
| 3823 | |
| 3824 | case expr_stmt: |
| 3825 | com_expr_stmt(c, n); |
| 3826 | break; |
| 3827 | case print_stmt: |
| 3828 | com_print_stmt(c, n); |
| 3829 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3830 | case del_stmt: /* 'del' exprlist */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3831 | com_assign(c, CHILD(n, 1), OP_DELETE, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3832 | break; |
| 3833 | case pass_stmt: |
| 3834 | break; |
| 3835 | case break_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3836 | if (c->c_loops == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3837 | com_error(c, PyExc_SyntaxError, |
| 3838 | "'break' outside loop"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3839 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3840 | com_addbyte(c, BREAK_LOOP); |
| 3841 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3842 | case continue_stmt: |
| 3843 | com_continue_stmt(c, n); |
| 3844 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3845 | case return_stmt: |
| 3846 | com_return_stmt(c, n); |
| 3847 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3848 | case yield_stmt: |
| 3849 | com_yield_stmt(c, n); |
| 3850 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3851 | case raise_stmt: |
| 3852 | com_raise_stmt(c, n); |
| 3853 | break; |
| 3854 | case import_stmt: |
| 3855 | com_import_stmt(c, n); |
| 3856 | break; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3857 | case global_stmt: |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3858 | break; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3859 | case exec_stmt: |
| 3860 | com_exec_stmt(c, n); |
| 3861 | break; |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 3862 | case assert_stmt: |
| 3863 | com_assert_stmt(c, n); |
| 3864 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3865 | case if_stmt: |
| 3866 | com_if_stmt(c, n); |
| 3867 | break; |
| 3868 | case while_stmt: |
| 3869 | com_while_stmt(c, n); |
| 3870 | break; |
| 3871 | case for_stmt: |
| 3872 | com_for_stmt(c, n); |
| 3873 | break; |
| 3874 | case try_stmt: |
| 3875 | com_try_stmt(c, n); |
| 3876 | break; |
| 3877 | case suite: |
| 3878 | com_suite(c, n); |
| 3879 | break; |
| 3880 | |
| 3881 | /* Expression nodes */ |
| 3882 | |
| 3883 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3884 | case testlist1: |
Guido van Rossum | 1c91707 | 2001-10-15 15:44:05 +0000 | [diff] [blame] | 3885 | case testlist_safe: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3886 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3887 | break; |
| 3888 | case test: |
| 3889 | com_test(c, n); |
| 3890 | break; |
| 3891 | case and_test: |
| 3892 | com_and_test(c, n); |
| 3893 | break; |
| 3894 | case not_test: |
| 3895 | com_not_test(c, n); |
| 3896 | break; |
| 3897 | case comparison: |
| 3898 | com_comparison(c, n); |
| 3899 | break; |
| 3900 | case exprlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3901 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3902 | break; |
| 3903 | case expr: |
| 3904 | com_expr(c, n); |
| 3905 | break; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 3906 | case xor_expr: |
| 3907 | com_xor_expr(c, n); |
| 3908 | break; |
| 3909 | case and_expr: |
| 3910 | com_and_expr(c, n); |
| 3911 | break; |
| 3912 | case shift_expr: |
| 3913 | com_shift_expr(c, n); |
| 3914 | break; |
| 3915 | case arith_expr: |
| 3916 | com_arith_expr(c, n); |
| 3917 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3918 | case term: |
| 3919 | com_term(c, n); |
| 3920 | break; |
| 3921 | case factor: |
| 3922 | com_factor(c, n); |
| 3923 | break; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3924 | case power: |
| 3925 | com_power(c, n); |
| 3926 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3927 | case atom: |
| 3928 | com_atom(c, n); |
| 3929 | break; |
| 3930 | |
| 3931 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3932 | com_error(c, PyExc_SystemError, |
| 3933 | "com_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3934 | } |
| 3935 | } |
| 3936 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 3937 | static void com_fplist(struct compiling *, node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3938 | |
| 3939 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3940 | com_fpdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3941 | { |
| 3942 | REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3943 | if (TYPE(CHILD(n, 0)) == LPAR) |
| 3944 | com_fplist(c, CHILD(n, 1)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3945 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3946 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3947 | com_pop(c, 1); |
| 3948 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3949 | } |
| 3950 | |
| 3951 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3952 | com_fplist(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3953 | { |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 3954 | REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3955 | if (NCH(n) == 1) { |
| 3956 | com_fpdef(c, CHILD(n, 0)); |
| 3957 | } |
| 3958 | else { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3959 | int i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 3960 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3961 | com_push(c, i-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3962 | for (i = 0; i < NCH(n); i += 2) |
| 3963 | com_fpdef(c, CHILD(n, i)); |
| 3964 | } |
| 3965 | } |
| 3966 | |
| 3967 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3968 | com_arglist(struct compiling *c, node *n) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3969 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3970 | int nch, i, narg; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3971 | int complex = 0; |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 3972 | char nbuf[30]; |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3973 | REQ(n, varargslist); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 3974 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3975 | (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3976 | nch = NCH(n); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3977 | /* Enter all arguments in table of locals */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3978 | for (i = 0, narg = 0; i < nch; i++) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3979 | node *ch = CHILD(n, i); |
| 3980 | node *fp; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3981 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3982 | break; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3983 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3984 | fp = CHILD(ch, 0); |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 3985 | if (TYPE(fp) != NAME) { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 3986 | PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i); |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 3987 | complex = 1; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3988 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3989 | narg++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3990 | /* all name updates handled by symtable */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3991 | if (++i >= nch) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3992 | break; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3993 | ch = CHILD(n, i); |
| 3994 | if (TYPE(ch) == EQUAL) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3995 | i += 2; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3996 | else |
| 3997 | REQ(ch, COMMA); |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3998 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3999 | if (complex) { |
| 4000 | /* Generate code for complex arguments only after |
| 4001 | having counted the simple arguments */ |
| 4002 | int ilocal = 0; |
| 4003 | for (i = 0; i < nch; i++) { |
| 4004 | node *ch = CHILD(n, i); |
| 4005 | node *fp; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 4006 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4007 | break; |
| 4008 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 4009 | fp = CHILD(ch, 0); |
| 4010 | if (TYPE(fp) != NAME) { |
| 4011 | com_addoparg(c, LOAD_FAST, ilocal); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4012 | com_push(c, 1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4013 | com_fpdef(c, ch); |
| 4014 | } |
| 4015 | ilocal++; |
| 4016 | if (++i >= nch) |
| 4017 | break; |
| 4018 | ch = CHILD(n, i); |
| 4019 | if (TYPE(ch) == EQUAL) |
| 4020 | i += 2; |
| 4021 | else |
| 4022 | REQ(ch, COMMA); |
| 4023 | } |
| 4024 | } |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4025 | } |
| 4026 | |
| 4027 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4028 | com_file_input(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4029 | { |
| 4030 | int i; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4031 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4032 | REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4033 | doc = get_docstring(c, n); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4034 | if (doc != NULL) { |
| 4035 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4036 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4037 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4038 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4039 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4040 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4041 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4042 | for (i = 0; i < NCH(n); i++) { |
| 4043 | node *ch = CHILD(n, i); |
| 4044 | if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE) |
| 4045 | com_node(c, ch); |
| 4046 | } |
| 4047 | } |
| 4048 | |
| 4049 | /* Top-level compile-node interface */ |
| 4050 | |
| 4051 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4052 | compile_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4053 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4054 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4055 | node *ch; |
| 4056 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 4057 | c->c_name = STR(CHILD(n, 1)); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4058 | doc = get_docstring(c, CHILD(n, 4)); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4059 | if (doc != NULL) { |
| 4060 | (void) com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4061 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4062 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4063 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4064 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4065 | ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */ |
| 4066 | ch = CHILD(ch, 1); /* ')' | varargslist */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4067 | if (TYPE(ch) == varargslist) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4068 | com_arglist(c, ch); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4069 | c->c_infunction = 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4070 | com_node(c, CHILD(n, 4)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4071 | c->c_infunction = 0; |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 4072 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
| 4073 | com_push(c, 1); |
| 4074 | com_addbyte(c, RETURN_VALUE); |
| 4075 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4076 | } |
| 4077 | |
| 4078 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4079 | compile_lambdef(struct compiling *c, node *n) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 4080 | { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4081 | node *ch; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4082 | REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */ |
Guido van Rossum | 363ac7d | 1994-11-10 22:40:34 +0000 | [diff] [blame] | 4083 | c->c_name = "<lambda>"; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4084 | |
| 4085 | ch = CHILD(n, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4086 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4087 | if (TYPE(ch) == varargslist) { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4088 | com_arglist(c, ch); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4089 | ch = CHILD(n, 3); |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4090 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4091 | else |
| 4092 | ch = CHILD(n, 2); |
| 4093 | com_node(c, ch); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 4094 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4095 | com_pop(c, 1); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 4096 | } |
| 4097 | |
| 4098 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4099 | compile_classdef(struct compiling *c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4100 | { |
| 4101 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4102 | PyObject *doc; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4103 | REQ(n, classdef); |
| 4104 | /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ |
| 4105 | c->c_name = STR(CHILD(n, 1)); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4106 | c->c_private = c->c_name; |
Guido van Rossum | 340cbe7 | 2002-01-15 21:06:07 +0000 | [diff] [blame] | 4107 | /* Initialize local __module__ from global __name__ */ |
| 4108 | com_addop_name(c, LOAD_GLOBAL, "__name__"); |
| 4109 | com_addop_name(c, STORE_NAME, "__module__"); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4110 | ch = CHILD(n, NCH(n)-1); /* The suite */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4111 | doc = get_docstring(c, ch); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4112 | if (doc != NULL) { |
| 4113 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4114 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4115 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4116 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4117 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4118 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4119 | } |
| 4120 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4121 | (void) com_addconst(c, Py_None); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4122 | com_node(c, ch); |
| 4123 | com_addbyte(c, LOAD_LOCALS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4124 | com_push(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4125 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4126 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4127 | } |
| 4128 | |
| 4129 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4130 | compile_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4131 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4132 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 4133 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4134 | switch (TYPE(n)) { |
| 4135 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4136 | case single_input: /* One interactive command */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4137 | /* NEWLINE | simple_stmt | compound_stmt NEWLINE */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 4138 | c->c_interactive++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4139 | n = CHILD(n, 0); |
| 4140 | if (TYPE(n) != NEWLINE) |
| 4141 | com_node(c, n); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4142 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4143 | com_push(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4144 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4145 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 4146 | c->c_interactive--; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4147 | break; |
| 4148 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4149 | case file_input: /* A whole file, or built-in function exec() */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4150 | com_file_input(c, n); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4151 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4152 | com_push(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4153 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4154 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4155 | break; |
| 4156 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4157 | case eval_input: /* Built-in function input() */ |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4158 | com_node(c, CHILD(n, 0)); |
| 4159 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4160 | com_pop(c, 1); |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4161 | break; |
| 4162 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4163 | case lambdef: /* anonymous function definition */ |
| 4164 | compile_lambdef(c, n); |
| 4165 | break; |
| 4166 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4167 | case funcdef: /* A function definition */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4168 | compile_funcdef(c, n); |
| 4169 | break; |
| 4170 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4171 | case classdef: /* A class definition */ |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4172 | compile_classdef(c, n); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 4173 | break; |
| 4174 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4175 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4176 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 4177 | "compile_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4178 | } |
| 4179 | } |
| 4180 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4181 | static PyObject * |
| 4182 | dict_keys_inorder(PyObject *dict, int offset) |
| 4183 | { |
| 4184 | PyObject *tuple, *k, *v; |
| 4185 | int i, pos = 0, size = PyDict_Size(dict); |
| 4186 | |
| 4187 | tuple = PyTuple_New(size); |
| 4188 | if (tuple == NULL) |
| 4189 | return NULL; |
| 4190 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 4191 | i = PyInt_AS_LONG(v); |
| 4192 | Py_INCREF(k); |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4193 | assert((i - offset) < size); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4194 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 4195 | } |
| 4196 | return tuple; |
| 4197 | } |
| 4198 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4199 | PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4200 | PyNode_Compile(node *n, char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4201 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4202 | return PyNode_CompileFlags(n, filename, NULL); |
| 4203 | } |
| 4204 | |
| 4205 | PyCodeObject * |
| 4206 | PyNode_CompileFlags(node *n, char *filename, PyCompilerFlags *flags) |
| 4207 | { |
| 4208 | return jcompile(n, filename, NULL, flags); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4211 | struct symtable * |
| 4212 | PyNode_CompileSymtable(node *n, char *filename) |
| 4213 | { |
| 4214 | struct symtable *st; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4215 | PyFutureFeatures *ff; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4216 | |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4217 | ff = PyNode_Future(n, filename); |
| 4218 | if (ff == NULL) |
| 4219 | return NULL; |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4220 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4221 | st = symtable_init(); |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4222 | if (st == NULL) { |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4223 | PyObject_FREE((void *)ff); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4224 | return NULL; |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4225 | } |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4226 | st->st_future = ff; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4227 | symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4228 | if (st->st_errors > 0) |
| 4229 | goto fail; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4230 | symtable_node(st, n); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4231 | if (st->st_errors > 0) |
| 4232 | goto fail; |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4233 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4234 | return st; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4235 | fail: |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4236 | PyObject_FREE((void *)ff); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4237 | st->st_future = NULL; |
| 4238 | PySymtable_Free(st); |
| 4239 | return NULL; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4240 | } |
| 4241 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4242 | static PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4243 | icompile(node *n, struct compiling *base) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4244 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4245 | return jcompile(n, base->c_filename, base, NULL); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4246 | } |
| 4247 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4248 | static PyCodeObject * |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4249 | jcompile(node *n, char *filename, struct compiling *base, |
| 4250 | PyCompilerFlags *flags) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4251 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4252 | struct compiling sc; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4253 | PyCodeObject *co; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4254 | if (!com_init(&sc, filename)) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4255 | return NULL; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 4256 | if (TYPE(n) == encoding_decl) { |
| 4257 | sc.c_encoding = STR(n); |
| 4258 | n = CHILD(n, 0); |
| 4259 | } else { |
| 4260 | sc.c_encoding = NULL; |
| 4261 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4262 | if (base) { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4263 | sc.c_private = base->c_private; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4264 | sc.c_symtable = base->c_symtable; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4265 | /* c_symtable still points to parent's symbols */ |
| 4266 | if (base->c_nested |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4267 | || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4268 | sc.c_nested = 1; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 4269 | sc.c_flags |= base->c_flags & PyCF_MASK; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 4270 | if (base->c_encoding != NULL) { |
| 4271 | assert(sc.c_encoding == NULL); |
| 4272 | sc.c_encoding = base->c_encoding; |
| 4273 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4274 | } else { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4275 | sc.c_private = NULL; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 4276 | sc.c_future = PyNode_Future(n, filename); |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4277 | if (sc.c_future == NULL) { |
| 4278 | com_free(&sc); |
| 4279 | return NULL; |
| 4280 | } |
Jeremy Hylton | 481081e | 2001-08-14 20:01:59 +0000 | [diff] [blame] | 4281 | if (flags) { |
| 4282 | int merged = sc.c_future->ff_features | |
| 4283 | flags->cf_flags; |
| 4284 | sc.c_future->ff_features = merged; |
| 4285 | flags->cf_flags = merged; |
| 4286 | } |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4287 | if (symtable_build(&sc, n) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4288 | com_free(&sc); |
| 4289 | return NULL; |
| 4290 | } |
| 4291 | } |
| 4292 | co = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4293 | if (symtable_load_symbols(&sc) < 0) { |
| 4294 | sc.c_errors++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4295 | goto exit; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4296 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4297 | compile_node(&sc, n); |
| 4298 | com_done(&sc); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 4299 | if (sc.c_errors == 0) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4300 | PyObject *consts, *names, *varnames, *filename, *name, |
| 4301 | *freevars, *cellvars; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4302 | consts = PyList_AsTuple(sc.c_consts); |
| 4303 | names = PyList_AsTuple(sc.c_names); |
| 4304 | varnames = PyList_AsTuple(sc.c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4305 | cellvars = dict_keys_inorder(sc.c_cellvars, 0); |
| 4306 | freevars = dict_keys_inorder(sc.c_freevars, |
| 4307 | PyTuple_GET_SIZE(cellvars)); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 4308 | filename = PyString_InternFromString(sc.c_filename); |
| 4309 | name = PyString_InternFromString(sc.c_name); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4310 | if (!PyErr_Occurred()) |
| 4311 | co = PyCode_New(sc.c_argcount, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4312 | sc.c_nlocals, |
| 4313 | sc.c_maxstacklevel, |
| 4314 | sc.c_flags, |
| 4315 | sc.c_code, |
| 4316 | consts, |
| 4317 | names, |
| 4318 | varnames, |
| 4319 | freevars, |
| 4320 | cellvars, |
| 4321 | filename, |
| 4322 | name, |
| 4323 | sc.c_firstlineno, |
| 4324 | sc.c_lnotab); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4325 | Py_XDECREF(consts); |
| 4326 | Py_XDECREF(names); |
| 4327 | Py_XDECREF(varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4328 | Py_XDECREF(freevars); |
| 4329 | Py_XDECREF(cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4330 | Py_XDECREF(filename); |
| 4331 | Py_XDECREF(name); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 4332 | } |
Guido van Rossum | 6c2f0c7 | 2000-08-07 19:22:43 +0000 | [diff] [blame] | 4333 | else if (!PyErr_Occurred()) { |
| 4334 | /* This could happen if someone called PyErr_Clear() after an |
| 4335 | error was reported above. That's not supposed to happen, |
| 4336 | but I just plugged one case and I'm not sure there can't be |
| 4337 | others. In that case, raise SystemError so that at least |
| 4338 | it gets reported instead dumping core. */ |
| 4339 | PyErr_SetString(PyExc_SystemError, "lost syntax error"); |
| 4340 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4341 | exit: |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4342 | if (base == NULL) { |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4343 | PySymtable_Free(sc.c_symtable); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4344 | sc.c_symtable = NULL; |
| 4345 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4346 | com_free(&sc); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4347 | return co; |
| 4348 | } |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4349 | |
| 4350 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4351 | PyCode_Addr2Line(PyCodeObject *co, int addrq) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4352 | { |
| 4353 | int size = PyString_Size(co->co_lnotab) / 2; |
Guido van Rossum | 2174dcb | 1999-09-15 22:48:09 +0000 | [diff] [blame] | 4354 | unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4355 | int line = co->co_firstlineno; |
| 4356 | int addr = 0; |
| 4357 | while (--size >= 0) { |
| 4358 | addr += *p++; |
| 4359 | if (addr > addrq) |
| 4360 | break; |
| 4361 | line += *p++; |
| 4362 | } |
| 4363 | return line; |
| 4364 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4365 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4366 | /* The test for LOCAL must come before the test for FREE in order to |
| 4367 | handle classes where name is both local and free. The local var is |
| 4368 | a method and the free var is a free var referenced within a method. |
| 4369 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4370 | |
| 4371 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4372 | get_ref_type(struct compiling *c, char *name) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4373 | { |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4374 | char buf[350]; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4375 | PyObject *v; |
Jeremy Hylton | 5125773 | 2001-03-01 00:42:55 +0000 | [diff] [blame] | 4376 | |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4377 | if (PyDict_GetItemString(c->c_cellvars, name) != NULL) |
| 4378 | return CELL; |
| 4379 | if (PyDict_GetItemString(c->c_locals, name) != NULL) |
| 4380 | return LOCAL; |
| 4381 | if (PyDict_GetItemString(c->c_freevars, name) != NULL) |
| 4382 | return FREE; |
| 4383 | v = PyDict_GetItemString(c->c_globals, name); |
| 4384 | if (v) { |
| 4385 | if (v == Py_None) |
| 4386 | return GLOBAL_EXPLICIT; |
| 4387 | else { |
| 4388 | return GLOBAL_IMPLICIT; |
| 4389 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4390 | } |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 4391 | PyOS_snprintf(buf, sizeof(buf), |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4392 | "unknown scope for %.100s in %.100s(%s) " |
| 4393 | "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n", |
| 4394 | name, c->c_name, |
| 4395 | PyObject_REPR(c->c_symtable->st_cur->ste_id), |
| 4396 | c->c_filename, |
| 4397 | PyObject_REPR(c->c_symtable->st_cur->ste_symbols), |
| 4398 | PyObject_REPR(c->c_locals), |
| 4399 | PyObject_REPR(c->c_globals) |
| 4400 | ); |
| 4401 | |
| 4402 | Py_FatalError(buf); |
| 4403 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4404 | } |
| 4405 | |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4406 | /* Helper functions to issue warnings */ |
| 4407 | |
| 4408 | static int |
| 4409 | issue_warning(char *msg, char *filename, int lineno) |
| 4410 | { |
| 4411 | if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename, |
| 4412 | lineno, NULL, NULL) < 0) { |
| 4413 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
| 4414 | PyErr_SetString(PyExc_SyntaxError, msg); |
| 4415 | PyErr_SyntaxLocation(filename, lineno); |
| 4416 | } |
| 4417 | return -1; |
| 4418 | } |
| 4419 | return 0; |
| 4420 | } |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4421 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4422 | static int |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4423 | symtable_warn(struct symtable *st, char *msg) |
| 4424 | { |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4425 | 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] | 4426 | st->st_errors++; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4427 | return -1; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4428 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4429 | return 0; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4430 | } |
| 4431 | |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 4432 | /* Helper function for setting lineno and filename */ |
| 4433 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4434 | static int |
| 4435 | symtable_build(struct compiling *c, node *n) |
| 4436 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4437 | if ((c->c_symtable = symtable_init()) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4438 | return -1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 4439 | c->c_symtable->st_future = c->c_future; |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4440 | c->c_symtable->st_filename = c->c_filename; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4441 | symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno); |
| 4442 | if (c->c_symtable->st_errors > 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4443 | return -1; |
| 4444 | symtable_node(c->c_symtable, n); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4445 | if (c->c_symtable->st_errors > 0) |
| 4446 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4447 | /* reset for second pass */ |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4448 | c->c_symtable->st_nscopes = 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4449 | c->c_symtable->st_pass = 2; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4450 | return 0; |
| 4451 | } |
| 4452 | |
| 4453 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4454 | symtable_init_compiling_symbols(struct compiling *c) |
| 4455 | { |
| 4456 | PyObject *varnames; |
| 4457 | |
| 4458 | varnames = c->c_symtable->st_cur->ste_varnames; |
| 4459 | if (varnames == NULL) { |
| 4460 | varnames = PyList_New(0); |
| 4461 | if (varnames == NULL) |
| 4462 | return -1; |
| 4463 | c->c_symtable->st_cur->ste_varnames = varnames; |
| 4464 | Py_INCREF(varnames); |
| 4465 | } else |
| 4466 | Py_INCREF(varnames); |
| 4467 | c->c_varnames = varnames; |
| 4468 | |
| 4469 | c->c_globals = PyDict_New(); |
| 4470 | if (c->c_globals == NULL) |
| 4471 | return -1; |
| 4472 | c->c_freevars = PyDict_New(); |
| 4473 | if (c->c_freevars == NULL) |
| 4474 | return -1; |
| 4475 | c->c_cellvars = PyDict_New(); |
| 4476 | if (c->c_cellvars == NULL) |
| 4477 | return -1; |
| 4478 | return 0; |
| 4479 | } |
| 4480 | |
| 4481 | struct symbol_info { |
| 4482 | int si_nlocals; |
| 4483 | int si_ncells; |
| 4484 | int si_nfrees; |
| 4485 | int si_nimplicit; |
| 4486 | }; |
| 4487 | |
| 4488 | static void |
| 4489 | symtable_init_info(struct symbol_info *si) |
| 4490 | { |
| 4491 | si->si_nlocals = 0; |
| 4492 | si->si_ncells = 0; |
| 4493 | si->si_nfrees = 0; |
| 4494 | si->si_nimplicit = 0; |
| 4495 | } |
| 4496 | |
| 4497 | static int |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4498 | symtable_resolve_free(struct compiling *c, PyObject *name, int flags, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4499 | struct symbol_info *si) |
| 4500 | { |
| 4501 | PyObject *dict, *v; |
| 4502 | |
| 4503 | /* Seperate logic for DEF_FREE. If it occurs in a function, |
| 4504 | it indicates a local that we must allocate storage for (a |
| 4505 | cell var). If it occurs in a class, then the class has a |
| 4506 | method and a free variable with the same name. |
| 4507 | */ |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4508 | if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4509 | /* If it isn't declared locally, it can't be a cell. */ |
| 4510 | if (!(flags & (DEF_LOCAL | DEF_PARAM))) |
| 4511 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4512 | v = PyInt_FromLong(si->si_ncells++); |
| 4513 | dict = c->c_cellvars; |
| 4514 | } else { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4515 | /* If it is free anyway, then there is no need to do |
| 4516 | anything here. |
| 4517 | */ |
| 4518 | if (is_free(flags ^ DEF_FREE_CLASS) |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4519 | || (flags == DEF_FREE_CLASS)) |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4520 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4521 | v = PyInt_FromLong(si->si_nfrees++); |
| 4522 | dict = c->c_freevars; |
| 4523 | } |
| 4524 | if (v == NULL) |
| 4525 | return -1; |
| 4526 | if (PyDict_SetItem(dict, name, v) < 0) { |
| 4527 | Py_DECREF(v); |
| 4528 | return -1; |
| 4529 | } |
| 4530 | Py_DECREF(v); |
| 4531 | return 0; |
| 4532 | } |
| 4533 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4534 | /* If a variable is a cell and an argument, make sure that appears in |
| 4535 | co_cellvars before any variable to its right in varnames. |
| 4536 | */ |
| 4537 | |
| 4538 | |
| 4539 | static int |
| 4540 | symtable_cellvar_offsets(PyObject **cellvars, int argcount, |
| 4541 | PyObject *varnames, int flags) |
| 4542 | { |
| 4543 | PyObject *v, *w, *d, *list = NULL; |
| 4544 | int i, pos; |
| 4545 | |
| 4546 | if (flags & CO_VARARGS) |
| 4547 | argcount++; |
| 4548 | if (flags & CO_VARKEYWORDS) |
| 4549 | argcount++; |
| 4550 | for (i = argcount; --i >= 0; ) { |
| 4551 | v = PyList_GET_ITEM(varnames, i); |
| 4552 | if (PyDict_GetItem(*cellvars, v)) { |
| 4553 | if (list == NULL) { |
| 4554 | list = PyList_New(1); |
| 4555 | if (list == NULL) |
| 4556 | return -1; |
| 4557 | PyList_SET_ITEM(list, 0, v); |
| 4558 | Py_INCREF(v); |
| 4559 | } else |
| 4560 | PyList_Insert(list, 0, v); |
| 4561 | } |
| 4562 | } |
| 4563 | if (list == NULL || PyList_GET_SIZE(list) == 0) |
| 4564 | return 0; |
| 4565 | /* There are cellvars that are also arguments. Create a dict |
| 4566 | to replace cellvars and put the args at the front. |
| 4567 | */ |
| 4568 | d = PyDict_New(); |
| 4569 | for (i = PyList_GET_SIZE(list); --i >= 0; ) { |
| 4570 | v = PyInt_FromLong(i); |
| 4571 | if (v == NULL) |
| 4572 | goto fail; |
| 4573 | if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0) |
| 4574 | goto fail; |
| 4575 | if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0) |
| 4576 | goto fail; |
| 4577 | } |
| 4578 | pos = 0; |
| 4579 | i = PyList_GET_SIZE(list); |
| 4580 | Py_DECREF(list); |
| 4581 | while (PyDict_Next(*cellvars, &pos, &v, &w)) { |
| 4582 | w = PyInt_FromLong(i++); /* don't care about the old key */ |
| 4583 | if (PyDict_SetItem(d, v, w) < 0) { |
| 4584 | Py_DECREF(w); |
| 4585 | goto fail; |
| 4586 | } |
| 4587 | Py_DECREF(w); |
| 4588 | } |
| 4589 | Py_DECREF(*cellvars); |
| 4590 | *cellvars = d; |
| 4591 | return 1; |
| 4592 | fail: |
| 4593 | Py_DECREF(d); |
| 4594 | return -1; |
| 4595 | } |
| 4596 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4597 | static int |
| 4598 | symtable_freevar_offsets(PyObject *freevars, int offset) |
| 4599 | { |
| 4600 | PyObject *name, *v; |
| 4601 | int pos; |
| 4602 | |
| 4603 | /* The cell vars are the first elements of the closure, |
| 4604 | followed by the free vars. Update the offsets in |
| 4605 | c_freevars to account for number of cellvars. */ |
| 4606 | pos = 0; |
| 4607 | while (PyDict_Next(freevars, &pos, &name, &v)) { |
| 4608 | int i = PyInt_AS_LONG(v) + offset; |
| 4609 | PyObject *o = PyInt_FromLong(i); |
| 4610 | if (o == NULL) |
| 4611 | return -1; |
| 4612 | if (PyDict_SetItem(freevars, name, o) < 0) { |
| 4613 | Py_DECREF(o); |
| 4614 | return -1; |
| 4615 | } |
| 4616 | Py_DECREF(o); |
| 4617 | } |
| 4618 | return 0; |
| 4619 | } |
| 4620 | |
| 4621 | static int |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4622 | symtable_check_unoptimized(struct compiling *c, |
| 4623 | PySymtableEntryObject *ste, |
| 4624 | struct symbol_info *si) |
| 4625 | { |
| 4626 | char buf[300]; |
| 4627 | |
| 4628 | if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free |
| 4629 | || (ste->ste_nested && si->si_nimplicit))) |
| 4630 | return 0; |
| 4631 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4632 | #define ILLEGAL_CONTAINS "contains a nested function with free variables" |
| 4633 | |
| 4634 | #define ILLEGAL_IS "is a nested function" |
| 4635 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4636 | #define ILLEGAL_IMPORT_STAR \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4637 | "import * is not allowed in function '%.100s' because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4638 | |
| 4639 | #define ILLEGAL_BARE_EXEC \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4640 | "unqualified exec is not allowed in function '%.100s' it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4641 | |
| 4642 | #define ILLEGAL_EXEC_AND_IMPORT_STAR \ |
Neal Norwitz | 150d09d | 2002-01-29 00:56:37 +0000 | [diff] [blame] | 4643 | "function '%.100s' uses import * and bare exec, which are illegal " \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4644 | "because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4645 | |
| 4646 | /* XXX perhaps the linenos for these opt-breaking statements |
| 4647 | should be stored so the exception can point to them. */ |
| 4648 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4649 | if (ste->ste_child_free) { |
| 4650 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4651 | PyOS_snprintf(buf, sizeof(buf), |
| 4652 | ILLEGAL_IMPORT_STAR, |
| 4653 | PyString_AS_STRING(ste->ste_name), |
| 4654 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4655 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4656 | PyOS_snprintf(buf, sizeof(buf), |
| 4657 | ILLEGAL_BARE_EXEC, |
| 4658 | PyString_AS_STRING(ste->ste_name), |
| 4659 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4660 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4661 | PyOS_snprintf(buf, sizeof(buf), |
| 4662 | ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 4663 | PyString_AS_STRING(ste->ste_name), |
| 4664 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4665 | } |
| 4666 | } else { |
| 4667 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4668 | PyOS_snprintf(buf, sizeof(buf), |
| 4669 | ILLEGAL_IMPORT_STAR, |
| 4670 | PyString_AS_STRING(ste->ste_name), |
| 4671 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4672 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4673 | PyOS_snprintf(buf, sizeof(buf), |
| 4674 | ILLEGAL_BARE_EXEC, |
| 4675 | PyString_AS_STRING(ste->ste_name), |
| 4676 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4677 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4678 | PyOS_snprintf(buf, sizeof(buf), |
| 4679 | ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 4680 | PyString_AS_STRING(ste->ste_name), |
| 4681 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4682 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4683 | } |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4684 | |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4685 | PyErr_SetString(PyExc_SyntaxError, buf); |
| 4686 | PyErr_SyntaxLocation(c->c_symtable->st_filename, |
| 4687 | ste->ste_opt_lineno); |
| 4688 | return -1; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4689 | } |
| 4690 | |
| 4691 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4692 | symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste, |
| 4693 | struct symbol_info *si) |
| 4694 | { |
Jeremy Hylton | b857ba2 | 2001-08-10 21:41:33 +0000 | [diff] [blame] | 4695 | if (c->c_future) |
| 4696 | c->c_flags |= c->c_future->ff_features; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4697 | if (ste->ste_generator) |
| 4698 | c->c_flags |= CO_GENERATOR; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4699 | if (ste->ste_type != TYPE_MODULE) |
| 4700 | c->c_flags |= CO_NEWLOCALS; |
| 4701 | if (ste->ste_type == TYPE_FUNCTION) { |
| 4702 | c->c_nlocals = si->si_nlocals; |
| 4703 | if (ste->ste_optimized == 0) |
| 4704 | c->c_flags |= CO_OPTIMIZED; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4705 | else if (ste->ste_optimized != OPT_EXEC) |
| 4706 | return symtable_check_unoptimized(c, ste, si); |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4707 | } |
| 4708 | return 0; |
| 4709 | } |
| 4710 | |
| 4711 | static int |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4712 | symtable_load_symbols(struct compiling *c) |
| 4713 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4714 | static PyObject *implicit = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4715 | struct symtable *st = c->c_symtable; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4716 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4717 | PyObject *name, *varnames, *v; |
| 4718 | int i, flags, pos; |
| 4719 | struct symbol_info si; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4720 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4721 | if (implicit == NULL) { |
| 4722 | implicit = PyInt_FromLong(1); |
| 4723 | if (implicit == NULL) |
| 4724 | return -1; |
| 4725 | } |
| 4726 | v = NULL; |
| 4727 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4728 | if (symtable_init_compiling_symbols(c) < 0) |
| 4729 | goto fail; |
| 4730 | symtable_init_info(&si); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4731 | varnames = st->st_cur->ste_varnames; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4732 | si.si_nlocals = PyList_GET_SIZE(varnames); |
| 4733 | c->c_argcount = si.si_nlocals; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4734 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4735 | for (i = 0; i < si.si_nlocals; ++i) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4736 | v = PyInt_FromLong(i); |
| 4737 | if (PyDict_SetItem(c->c_locals, |
| 4738 | PyList_GET_ITEM(varnames, i), v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4739 | goto fail; |
| 4740 | Py_DECREF(v); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4741 | } |
| 4742 | |
| 4743 | /* XXX The cases below define the rules for whether a name is |
| 4744 | local or global. The logic could probably be clearer. */ |
| 4745 | pos = 0; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4746 | while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) { |
| 4747 | flags = PyInt_AS_LONG(v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4748 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4749 | if (flags & DEF_FREE_GLOBAL) |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4750 | /* undo the original DEF_FREE */ |
| 4751 | flags &= ~(DEF_FREE | DEF_FREE_CLASS); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4752 | |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4753 | /* Deal with names that need two actions: |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4754 | 1. Cell variables that are also locals. |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4755 | 2. Free variables in methods that are also class |
| 4756 | variables or declared global. |
| 4757 | */ |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4758 | if (flags & (DEF_FREE | DEF_FREE_CLASS)) |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4759 | symtable_resolve_free(c, name, flags, &si); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4760 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4761 | if (flags & DEF_STAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4762 | c->c_argcount--; |
| 4763 | c->c_flags |= CO_VARARGS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4764 | } else if (flags & DEF_DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4765 | c->c_argcount--; |
| 4766 | c->c_flags |= CO_VARKEYWORDS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4767 | } else if (flags & DEF_INTUPLE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4768 | c->c_argcount--; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4769 | else if (flags & DEF_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4770 | if (flags & DEF_PARAM) { |
| 4771 | PyErr_Format(PyExc_SyntaxError, LOCAL_GLOBAL, |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4772 | PyString_AS_STRING(name)); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 4773 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4774 | ste->ste_lineno); |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4775 | st->st_errors++; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4776 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4777 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4778 | if (PyDict_SetItem(c->c_globals, name, Py_None) < 0) |
| 4779 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4780 | } else if (flags & DEF_FREE_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4781 | si.si_nimplicit++; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4782 | if (PyDict_SetItem(c->c_globals, name, implicit) < 0) |
| 4783 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4784 | } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4785 | v = PyInt_FromLong(si.si_nlocals++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4786 | if (v == NULL) |
| 4787 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4788 | if (PyDict_SetItem(c->c_locals, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4789 | goto fail; |
| 4790 | Py_DECREF(v); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4791 | if (ste->ste_type != TYPE_CLASS) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4792 | if (PyList_Append(c->c_varnames, name) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4793 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4794 | } else if (is_free(flags)) { |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4795 | if (ste->ste_nested) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4796 | v = PyInt_FromLong(si.si_nfrees++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4797 | if (v == NULL) |
| 4798 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4799 | if (PyDict_SetItem(c->c_freevars, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4800 | goto fail; |
| 4801 | Py_DECREF(v); |
| 4802 | } else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4803 | si.si_nimplicit++; |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4804 | if (PyDict_SetItem(c->c_globals, name, |
| 4805 | implicit) < 0) |
| 4806 | goto fail; |
| 4807 | if (st->st_nscopes != 1) { |
| 4808 | v = PyInt_FromLong(flags); |
| 4809 | if (PyDict_SetItem(st->st_global, |
| 4810 | name, v)) |
| 4811 | goto fail; |
| 4812 | Py_DECREF(v); |
| 4813 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4814 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4815 | } |
| 4816 | } |
| 4817 | |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4818 | assert(PyDict_Size(c->c_freevars) == si.si_nfrees); |
| 4819 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4820 | if (si.si_ncells > 1) { /* one cell is always in order */ |
| 4821 | if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount, |
| 4822 | c->c_varnames, c->c_flags) < 0) |
| 4823 | return -1; |
| 4824 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4825 | if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0) |
| 4826 | return -1; |
| 4827 | return symtable_update_flags(c, ste, &si); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4828 | fail: |
| 4829 | /* is this always the right thing to do? */ |
| 4830 | Py_XDECREF(v); |
| 4831 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4832 | } |
| 4833 | |
| 4834 | static struct symtable * |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4835 | symtable_init() |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4836 | { |
| 4837 | struct symtable *st; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4838 | |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4839 | st = (struct symtable *)PyObject_MALLOC(sizeof(struct symtable)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4840 | if (st == NULL) |
| 4841 | return NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4842 | st->st_pass = 1; |
Tim Peters | ff1f852 | 2001-08-11 01:06:35 +0000 | [diff] [blame] | 4843 | |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4844 | st->st_filename = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4845 | if ((st->st_stack = PyList_New(0)) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4846 | goto fail; |
| 4847 | if ((st->st_symbols = PyDict_New()) == NULL) |
| 4848 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4849 | st->st_cur = NULL; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4850 | st->st_nscopes = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4851 | st->st_errors = 0; |
| 4852 | st->st_tmpname = 0; |
| 4853 | st->st_private = NULL; |
| 4854 | return st; |
| 4855 | fail: |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4856 | PySymtable_Free(st); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4857 | return NULL; |
| 4858 | } |
| 4859 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4860 | void |
| 4861 | PySymtable_Free(struct symtable *st) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4862 | { |
| 4863 | Py_XDECREF(st->st_symbols); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4864 | Py_XDECREF(st->st_stack); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4865 | Py_XDECREF(st->st_cur); |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4866 | PyObject_FREE((void *)st); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4867 | } |
| 4868 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4869 | /* When the compiler exits a scope, it must should update the scope's |
| 4870 | free variable information with the list of free variables in its |
| 4871 | children. |
| 4872 | |
| 4873 | Variables that are free in children and defined in the current |
| 4874 | scope are cellvars. |
| 4875 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4876 | 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] | 4877 | false), free variables in children that are not defined here are |
| 4878 | implicit globals. |
| 4879 | |
| 4880 | */ |
| 4881 | |
| 4882 | static int |
| 4883 | symtable_update_free_vars(struct symtable *st) |
| 4884 | { |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4885 | int i, j, def; |
| 4886 | PyObject *o, *name, *list = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4887 | PySymtableEntryObject *child, *ste = st->st_cur; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4888 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4889 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4890 | def = DEF_FREE_CLASS; |
| 4891 | else |
| 4892 | def = DEF_FREE; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4893 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4894 | int pos = 0; |
| 4895 | |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4896 | if (list) |
| 4897 | PyList_SetSlice(list, 0, |
| 4898 | ((PyVarObject*)list)->ob_size, 0); |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4899 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4900 | PyList_GET_ITEM(ste->ste_children, i); |
| 4901 | while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4902 | int flags = PyInt_AS_LONG(o); |
| 4903 | if (!(is_free(flags))) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4904 | continue; /* avoids indentation */ |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4905 | if (list == NULL) { |
| 4906 | list = PyList_New(0); |
| 4907 | if (list == NULL) |
| 4908 | return -1; |
| 4909 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4910 | ste->ste_child_free = 1; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4911 | if (PyList_Append(list, name) < 0) { |
| 4912 | Py_DECREF(list); |
| 4913 | return -1; |
| 4914 | } |
| 4915 | } |
| 4916 | for (j = 0; list && j < PyList_GET_SIZE(list); j++) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4917 | PyObject *v; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4918 | name = PyList_GET_ITEM(list, j); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4919 | v = PyDict_GetItem(ste->ste_symbols, name); |
| 4920 | /* If a name N is declared global in scope A and |
| 4921 | referenced in scope B contained (perhaps |
| 4922 | indirectly) in A and there are no scopes |
| 4923 | with bindings for N between B and A, then N |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4924 | is global in B. Unless A is a class scope, |
| 4925 | because class scopes are not considered for |
| 4926 | nested scopes. |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4927 | */ |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4928 | if (v && (ste->ste_type != TYPE_CLASS)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4929 | int flags = PyInt_AS_LONG(v); |
| 4930 | if (flags & DEF_GLOBAL) { |
| 4931 | symtable_undo_free(st, child->ste_id, |
| 4932 | name); |
| 4933 | continue; |
| 4934 | } |
| 4935 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4936 | if (ste->ste_nested) { |
| 4937 | if (symtable_add_def_o(st, ste->ste_symbols, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4938 | name, def) < 0) { |
| 4939 | Py_DECREF(list); |
| 4940 | return -1; |
| 4941 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4942 | } else { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4943 | if (symtable_check_global(st, child->ste_id, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4944 | name) < 0) { |
| 4945 | Py_DECREF(list); |
| 4946 | return -1; |
| 4947 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4948 | } |
| 4949 | } |
| 4950 | } |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4951 | |
| 4952 | Py_XDECREF(list); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4953 | return 0; |
| 4954 | } |
| 4955 | |
| 4956 | /* If the current scope is a non-nested class or if name is not |
| 4957 | defined in the current, non-nested scope, then it is an implicit |
| 4958 | global in all nested scopes. |
| 4959 | */ |
| 4960 | |
| 4961 | static int |
| 4962 | symtable_check_global(struct symtable *st, PyObject *child, PyObject *name) |
| 4963 | { |
| 4964 | PyObject *o; |
| 4965 | int v; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4966 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4967 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4968 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4969 | return symtable_undo_free(st, child, name); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4970 | o = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4971 | if (o == NULL) |
| 4972 | return symtable_undo_free(st, child, name); |
| 4973 | v = PyInt_AS_LONG(o); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4974 | |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4975 | if (is_free(v) || (v & DEF_GLOBAL)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4976 | return symtable_undo_free(st, child, name); |
| 4977 | else |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4978 | return symtable_add_def_o(st, ste->ste_symbols, |
| 4979 | name, DEF_FREE); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4980 | } |
| 4981 | |
| 4982 | static int |
| 4983 | symtable_undo_free(struct symtable *st, PyObject *id, |
| 4984 | PyObject *name) |
| 4985 | { |
| 4986 | int i, v, x; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4987 | PyObject *info; |
| 4988 | PySymtableEntryObject *ste; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4989 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4990 | ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id); |
| 4991 | if (ste == NULL) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4992 | return -1; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4993 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4994 | info = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4995 | if (info == NULL) |
| 4996 | return 0; |
| 4997 | v = PyInt_AS_LONG(info); |
| 4998 | if (is_free(v)) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4999 | if (symtable_add_def_o(st, ste->ste_symbols, name, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5000 | DEF_FREE_GLOBAL) < 0) |
| 5001 | return -1; |
| 5002 | } else |
| 5003 | /* If the name is defined here or declared global, |
| 5004 | then the recursion stops. */ |
| 5005 | return 0; |
| 5006 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5007 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
| 5008 | PySymtableEntryObject *child; |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 5009 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5010 | PyList_GET_ITEM(ste->ste_children, i); |
| 5011 | x = symtable_undo_free(st, child->ste_id, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5012 | if (x < 0) |
| 5013 | return x; |
| 5014 | } |
| 5015 | return 0; |
| 5016 | } |
| 5017 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5018 | /* symtable_enter_scope() gets a reference via PySymtableEntry_New(). |
| 5019 | This reference is released when the scope is exited, via the DECREF |
| 5020 | in symtable_exit_scope(). |
| 5021 | */ |
| 5022 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5023 | static int |
| 5024 | symtable_exit_scope(struct symtable *st) |
| 5025 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5026 | int end; |
| 5027 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 5028 | if (st->st_pass == 1) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5029 | symtable_update_free_vars(st); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5030 | Py_DECREF(st->st_cur); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5031 | end = PyList_GET_SIZE(st->st_stack) - 1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5032 | st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack, |
| 5033 | end); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5034 | if (PySequence_DelItem(st->st_stack, end) < 0) |
| 5035 | return -1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5036 | return 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5037 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5038 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5039 | static void |
| 5040 | symtable_enter_scope(struct symtable *st, char *name, int type, |
| 5041 | int lineno) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5042 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5043 | PySymtableEntryObject *prev = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5044 | |
| 5045 | if (st->st_cur) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5046 | prev = st->st_cur; |
| 5047 | if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) { |
| 5048 | Py_DECREF(st->st_cur); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5049 | st->st_errors++; |
| 5050 | return; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5051 | } |
| 5052 | } |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 5053 | st->st_cur = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5054 | PySymtableEntry_New(st, name, type, lineno); |
| 5055 | if (strcmp(name, TOP) == 0) |
| 5056 | st->st_global = st->st_cur->ste_symbols; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 5057 | if (prev && st->st_pass == 1) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5058 | if (PyList_Append(prev->ste_children, |
| 5059 | (PyObject *)st->st_cur) < 0) |
| 5060 | st->st_errors++; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 5061 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5062 | } |
| 5063 | |
| 5064 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5065 | symtable_lookup(struct symtable *st, char *name) |
| 5066 | { |
| 5067 | char buffer[MANGLE_LEN]; |
| 5068 | PyObject *v; |
| 5069 | int flags; |
| 5070 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 5071 | if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5072 | name = buffer; |
| 5073 | v = PyDict_GetItemString(st->st_cur->ste_symbols, name); |
| 5074 | if (v == NULL) { |
| 5075 | if (PyErr_Occurred()) |
| 5076 | return -1; |
| 5077 | else |
| 5078 | return 0; |
| 5079 | } |
| 5080 | |
| 5081 | flags = PyInt_AS_LONG(v); |
| 5082 | return flags; |
| 5083 | } |
| 5084 | |
| 5085 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5086 | symtable_add_def(struct symtable *st, char *name, int flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5087 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5088 | PyObject *s; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5089 | char buffer[MANGLE_LEN]; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5090 | int ret; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5091 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 5092 | if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5093 | name = buffer; |
| 5094 | if ((s = PyString_InternFromString(name)) == NULL) |
| 5095 | return -1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5096 | ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag); |
| 5097 | Py_DECREF(s); |
| 5098 | return ret; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5099 | } |
| 5100 | |
| 5101 | /* Must only be called with mangled names */ |
| 5102 | |
| 5103 | static int |
| 5104 | symtable_add_def_o(struct symtable *st, PyObject *dict, |
| 5105 | PyObject *name, int flag) |
| 5106 | { |
| 5107 | PyObject *o; |
| 5108 | int val; |
| 5109 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5110 | if ((o = PyDict_GetItem(dict, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5111 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5112 | if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5113 | PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5114 | PyString_AsString(name)); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5115 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5116 | st->st_cur->ste_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5117 | return -1; |
| 5118 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5119 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5120 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5121 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5122 | o = PyInt_FromLong(val); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5123 | if (PyDict_SetItem(dict, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5124 | Py_DECREF(o); |
| 5125 | return -1; |
| 5126 | } |
| 5127 | Py_DECREF(o); |
| 5128 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5129 | if (flag & DEF_PARAM) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5130 | if (PyList_Append(st->st_cur->ste_varnames, name) < 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5131 | return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5132 | } else if (flag & DEF_GLOBAL) { |
| 5133 | /* XXX need to update DEF_GLOBAL for other flags too; |
| 5134 | perhaps only DEF_FREE_GLOBAL */ |
| 5135 | if ((o = PyDict_GetItem(st->st_global, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5136 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5137 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5138 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5139 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5140 | o = PyInt_FromLong(val); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5141 | if (PyDict_SetItem(st->st_global, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5142 | Py_DECREF(o); |
| 5143 | return -1; |
| 5144 | } |
| 5145 | Py_DECREF(o); |
| 5146 | } |
| 5147 | return 0; |
| 5148 | } |
| 5149 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5150 | #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5151 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 5152 | /* Look for a yield stmt under n. Return 1 if found, else 0. |
| 5153 | This hack is used to look inside "if 0:" blocks (which are normally |
| 5154 | ignored) in case those are the only places a yield occurs (so that this |
| 5155 | function is a generator). */ |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5156 | static int |
| 5157 | look_for_yield(node *n) |
| 5158 | { |
| 5159 | int i; |
| 5160 | |
| 5161 | for (i = 0; i < NCH(n); ++i) { |
| 5162 | node *kid = CHILD(n, i); |
| 5163 | |
| 5164 | switch (TYPE(kid)) { |
| 5165 | |
| 5166 | case classdef: |
| 5167 | case funcdef: |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 5168 | case lambdef: |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5169 | /* Stuff in nested functions and classes can't make |
| 5170 | the parent a generator. */ |
| 5171 | return 0; |
| 5172 | |
| 5173 | case yield_stmt: |
| 5174 | return 1; |
| 5175 | |
| 5176 | default: |
| 5177 | if (look_for_yield(kid)) |
| 5178 | return 1; |
| 5179 | } |
| 5180 | } |
| 5181 | return 0; |
| 5182 | } |
| 5183 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5184 | static void |
| 5185 | symtable_node(struct symtable *st, node *n) |
| 5186 | { |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5187 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5188 | |
| 5189 | loop: |
| 5190 | switch (TYPE(n)) { |
| 5191 | case funcdef: { |
| 5192 | char *func_name = STR(CHILD(n, 1)); |
| 5193 | symtable_add_def(st, func_name, DEF_LOCAL); |
| 5194 | symtable_default_args(st, CHILD(n, 2)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5195 | symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5196 | symtable_funcdef(st, n); |
| 5197 | symtable_exit_scope(st); |
| 5198 | break; |
| 5199 | } |
| 5200 | case lambdef: |
| 5201 | if (NCH(n) == 4) |
| 5202 | symtable_default_args(st, CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5203 | symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5204 | symtable_funcdef(st, n); |
| 5205 | symtable_exit_scope(st); |
| 5206 | break; |
| 5207 | case classdef: { |
| 5208 | char *tmp, *class_name = STR(CHILD(n, 1)); |
| 5209 | symtable_add_def(st, class_name, DEF_LOCAL); |
| 5210 | if (TYPE(CHILD(n, 2)) == LPAR) { |
| 5211 | node *bases = CHILD(n, 3); |
| 5212 | int i; |
| 5213 | for (i = 0; i < NCH(bases); i += 2) { |
| 5214 | symtable_node(st, CHILD(bases, i)); |
| 5215 | } |
| 5216 | } |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5217 | symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5218 | tmp = st->st_private; |
| 5219 | st->st_private = class_name; |
| 5220 | symtable_node(st, CHILD(n, NCH(n) - 1)); |
| 5221 | st->st_private = tmp; |
| 5222 | symtable_exit_scope(st); |
| 5223 | break; |
| 5224 | } |
| 5225 | case if_stmt: |
| 5226 | for (i = 0; i + 3 < NCH(n); i += 4) { |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5227 | if (is_constant_false(NULL, (CHILD(n, i + 1)))) { |
| 5228 | if (st->st_cur->ste_generator == 0) |
| 5229 | st->st_cur->ste_generator = |
| 5230 | look_for_yield(CHILD(n, i+3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5231 | continue; |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5232 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5233 | symtable_node(st, CHILD(n, i + 1)); |
| 5234 | symtable_node(st, CHILD(n, i + 3)); |
| 5235 | } |
| 5236 | if (i + 2 < NCH(n)) |
| 5237 | symtable_node(st, CHILD(n, i + 2)); |
| 5238 | break; |
| 5239 | case global_stmt: |
| 5240 | symtable_global(st, n); |
| 5241 | break; |
| 5242 | case import_stmt: |
| 5243 | symtable_import(st, n); |
| 5244 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5245 | case exec_stmt: { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5246 | st->st_cur->ste_optimized |= OPT_EXEC; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5247 | symtable_node(st, CHILD(n, 1)); |
| 5248 | if (NCH(n) > 2) |
| 5249 | symtable_node(st, CHILD(n, 3)); |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5250 | else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5251 | st->st_cur->ste_optimized |= OPT_BARE_EXEC; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5252 | st->st_cur->ste_opt_lineno = n->n_lineno; |
| 5253 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5254 | if (NCH(n) > 4) |
| 5255 | symtable_node(st, CHILD(n, 5)); |
| 5256 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5257 | |
| 5258 | } |
Jeremy Hylton | 384639f | 2001-02-19 15:50:51 +0000 | [diff] [blame] | 5259 | case assert_stmt: |
| 5260 | if (Py_OptimizeFlag) |
| 5261 | return; |
| 5262 | if (NCH(n) == 2) { |
| 5263 | n = CHILD(n, 1); |
| 5264 | goto loop; |
| 5265 | } else { |
| 5266 | symtable_node(st, CHILD(n, 1)); |
| 5267 | n = CHILD(n, 3); |
| 5268 | goto loop; |
| 5269 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5270 | case except_clause: |
| 5271 | if (NCH(n) == 4) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5272 | symtable_assign(st, CHILD(n, 3), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5273 | if (NCH(n) > 1) { |
| 5274 | n = CHILD(n, 1); |
| 5275 | goto loop; |
| 5276 | } |
| 5277 | break; |
| 5278 | case del_stmt: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5279 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5280 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 5281 | case yield_stmt: |
| 5282 | st->st_cur->ste_generator = 1; |
| 5283 | n = CHILD(n, 1); |
| 5284 | goto loop; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5285 | case expr_stmt: |
| 5286 | if (NCH(n) == 1) |
| 5287 | n = CHILD(n, 0); |
| 5288 | else { |
| 5289 | if (TYPE(CHILD(n, 1)) == augassign) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5290 | symtable_assign(st, CHILD(n, 0), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5291 | symtable_node(st, CHILD(n, 2)); |
| 5292 | break; |
| 5293 | } else { |
| 5294 | int i; |
| 5295 | for (i = 0; i < NCH(n) - 2; i += 2) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5296 | symtable_assign(st, CHILD(n, i), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5297 | n = CHILD(n, NCH(n) - 1); |
| 5298 | } |
| 5299 | } |
| 5300 | goto loop; |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5301 | case list_iter: |
| 5302 | n = CHILD(n, 0); |
| 5303 | if (TYPE(n) == list_for) { |
| 5304 | st->st_tmpname++; |
| 5305 | symtable_list_comprehension(st, n); |
| 5306 | st->st_tmpname--; |
| 5307 | } else { |
| 5308 | REQ(n, list_if); |
| 5309 | symtable_node(st, CHILD(n, 1)); |
| 5310 | if (NCH(n) == 3) { |
| 5311 | n = CHILD(n, 2); |
| 5312 | goto loop; |
| 5313 | } |
| 5314 | } |
| 5315 | break; |
| 5316 | case for_stmt: |
| 5317 | symtable_assign(st, CHILD(n, 1), 0); |
| 5318 | for (i = 3; i < NCH(n); ++i) |
| 5319 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5320 | symtable_node(st, CHILD(n, i)); |
| 5321 | break; |
| 5322 | /* The remaining cases fall through to default except in |
| 5323 | special circumstances. This requires the individual cases |
| 5324 | to be coded with great care, even though they look like |
| 5325 | rather innocuous. Each case must double-check TYPE(n). |
| 5326 | */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5327 | case argument: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5328 | if (TYPE(n) == argument && NCH(n) == 3) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5329 | n = CHILD(n, 2); |
| 5330 | goto loop; |
| 5331 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5332 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5333 | case listmaker: |
| 5334 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5335 | st->st_tmpname++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5336 | symtable_list_comprehension(st, CHILD(n, 1)); |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5337 | symtable_node(st, CHILD(n, 0)); |
| 5338 | st->st_tmpname--; |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5339 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5340 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5341 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5342 | case atom: |
| 5343 | if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) { |
| 5344 | symtable_add_use(st, STR(CHILD(n, 0))); |
| 5345 | break; |
| 5346 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5347 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5348 | default: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5349 | /* Walk over every non-token child with a special case |
| 5350 | for one child. |
| 5351 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5352 | if (NCH(n) == 1) { |
| 5353 | n = CHILD(n, 0); |
| 5354 | goto loop; |
| 5355 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5356 | for (i = 0; i < NCH(n); ++i) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5357 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5358 | symtable_node(st, CHILD(n, i)); |
| 5359 | } |
| 5360 | } |
| 5361 | |
| 5362 | static void |
| 5363 | symtable_funcdef(struct symtable *st, node *n) |
| 5364 | { |
| 5365 | node *body; |
| 5366 | |
| 5367 | if (TYPE(n) == lambdef) { |
| 5368 | if (NCH(n) == 4) |
| 5369 | symtable_params(st, CHILD(n, 1)); |
| 5370 | } else |
| 5371 | symtable_params(st, CHILD(n, 2)); |
| 5372 | body = CHILD(n, NCH(n) - 1); |
| 5373 | symtable_node(st, body); |
| 5374 | } |
| 5375 | |
| 5376 | /* The next two functions parse the argument tuple. |
| 5377 | symtable_default_arg() checks for names in the default arguments, |
| 5378 | which are references in the defining scope. symtable_params() |
| 5379 | parses the parameter names, which are defined in the function's |
| 5380 | body. |
| 5381 | |
| 5382 | varargslist: |
| 5383 | (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) |
| 5384 | | fpdef ['=' test] (',' fpdef ['=' test])* [','] |
| 5385 | */ |
| 5386 | |
| 5387 | static void |
| 5388 | symtable_default_args(struct symtable *st, node *n) |
| 5389 | { |
| 5390 | node *c; |
| 5391 | int i; |
| 5392 | |
| 5393 | if (TYPE(n) == parameters) { |
| 5394 | n = CHILD(n, 1); |
| 5395 | if (TYPE(n) == RPAR) |
| 5396 | return; |
| 5397 | } |
| 5398 | REQ(n, varargslist); |
| 5399 | for (i = 0; i < NCH(n); i += 2) { |
| 5400 | c = CHILD(n, i); |
| 5401 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5402 | break; |
| 5403 | } |
| 5404 | if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL)) |
| 5405 | symtable_node(st, CHILD(n, i)); |
| 5406 | } |
| 5407 | } |
| 5408 | |
| 5409 | static void |
| 5410 | symtable_params(struct symtable *st, node *n) |
| 5411 | { |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5412 | int i, complex = -1, ext = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5413 | node *c = NULL; |
| 5414 | |
| 5415 | if (TYPE(n) == parameters) { |
| 5416 | n = CHILD(n, 1); |
| 5417 | if (TYPE(n) == RPAR) |
| 5418 | return; |
| 5419 | } |
| 5420 | REQ(n, varargslist); |
| 5421 | for (i = 0; i < NCH(n); i += 2) { |
| 5422 | c = CHILD(n, i); |
| 5423 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5424 | ext = 1; |
| 5425 | break; |
| 5426 | } |
| 5427 | if (TYPE(c) == test) { |
| 5428 | continue; |
| 5429 | } |
| 5430 | if (TYPE(CHILD(c, 0)) == NAME) |
| 5431 | symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM); |
| 5432 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5433 | char nbuf[30]; |
| 5434 | PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5435 | symtable_add_def(st, nbuf, DEF_PARAM); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5436 | complex = i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5437 | } |
| 5438 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5439 | if (ext) { |
| 5440 | c = CHILD(n, i); |
| 5441 | if (TYPE(c) == STAR) { |
| 5442 | i++; |
| 5443 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5444 | DEF_PARAM | DEF_STAR); |
| 5445 | i += 2; |
Jeremy Hylton | 1113cfc | 2001-01-23 00:50:52 +0000 | [diff] [blame] | 5446 | if (i >= NCH(n)) |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5447 | c = NULL; |
| 5448 | else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5449 | c = CHILD(n, i); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5450 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5451 | if (c && TYPE(c) == DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5452 | i++; |
| 5453 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5454 | DEF_PARAM | DEF_DOUBLESTAR); |
| 5455 | } |
| 5456 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5457 | if (complex >= 0) { |
| 5458 | int j; |
| 5459 | for (j = 0; j <= complex; j++) { |
| 5460 | c = CHILD(n, j); |
| 5461 | if (TYPE(c) == COMMA) |
Jeremy Hylton | 2b3f0ca | 2001-02-19 23:52:49 +0000 | [diff] [blame] | 5462 | c = CHILD(n, ++j); |
| 5463 | else if (TYPE(c) == EQUAL) |
| 5464 | c = CHILD(n, j += 3); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5465 | if (TYPE(CHILD(c, 0)) == LPAR) |
| 5466 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5467 | } |
| 5468 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5469 | } |
| 5470 | |
| 5471 | static void |
| 5472 | symtable_params_fplist(struct symtable *st, node *n) |
| 5473 | { |
| 5474 | int i; |
| 5475 | node *c; |
| 5476 | |
| 5477 | REQ(n, fplist); |
| 5478 | for (i = 0; i < NCH(n); i += 2) { |
| 5479 | c = CHILD(n, i); |
| 5480 | REQ(c, fpdef); |
| 5481 | if (NCH(c) == 1) |
| 5482 | symtable_add_def(st, STR(CHILD(c, 0)), |
| 5483 | DEF_PARAM | DEF_INTUPLE); |
| 5484 | else |
| 5485 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5486 | } |
| 5487 | |
| 5488 | } |
| 5489 | |
| 5490 | static void |
| 5491 | symtable_global(struct symtable *st, node *n) |
| 5492 | { |
| 5493 | int i; |
| 5494 | |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 5495 | /* XXX It might be helpful to warn about module-level global |
| 5496 | statements, but it's hard to tell the difference between |
| 5497 | module-level and a string passed to exec. |
| 5498 | */ |
Jeremy Hylton | c176132 | 2001-02-28 23:44:45 +0000 | [diff] [blame] | 5499 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5500 | for (i = 1; i < NCH(n); i += 2) { |
| 5501 | char *name = STR(CHILD(n, i)); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5502 | int flags; |
| 5503 | |
| 5504 | flags = symtable_lookup(st, name); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5505 | if (flags < 0) |
| 5506 | continue; |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5507 | if (flags && flags != DEF_GLOBAL) { |
| 5508 | char buf[500]; |
| 5509 | if (flags & DEF_PARAM) { |
| 5510 | PyErr_Format(PyExc_SyntaxError, |
| 5511 | "name '%.400s' is local and global", |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5512 | name); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5513 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5514 | st->st_cur->ste_lineno); |
| 5515 | st->st_errors++; |
| 5516 | return; |
Guido van Rossum | 0bba7f8 | 2001-02-28 21:55:38 +0000 | [diff] [blame] | 5517 | } |
| 5518 | else { |
| 5519 | if (flags & DEF_LOCAL) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5520 | PyOS_snprintf(buf, sizeof(buf), |
| 5521 | GLOBAL_AFTER_ASSIGN, |
| 5522 | name); |
Guido van Rossum | 0bba7f8 | 2001-02-28 21:55:38 +0000 | [diff] [blame] | 5523 | else |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5524 | PyOS_snprintf(buf, sizeof(buf), |
| 5525 | GLOBAL_AFTER_USE, name); |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 5526 | symtable_warn(st, buf); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5527 | } |
| 5528 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5529 | symtable_add_def(st, name, DEF_GLOBAL); |
| 5530 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5531 | } |
| 5532 | |
| 5533 | static void |
| 5534 | symtable_list_comprehension(struct symtable *st, node *n) |
| 5535 | { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5536 | char tmpname[30]; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5537 | |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5538 | PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", st->st_tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5539 | symtable_add_def(st, tmpname, DEF_LOCAL); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5540 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5541 | symtable_node(st, CHILD(n, 3)); |
| 5542 | if (NCH(n) == 5) |
| 5543 | symtable_node(st, CHILD(n, 4)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5544 | } |
| 5545 | |
| 5546 | static void |
| 5547 | symtable_import(struct symtable *st, node *n) |
| 5548 | { |
| 5549 | int i; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5550 | /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)* |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5551 | | 'from' dotted_name 'import' |
| 5552 | ('*' | import_as_name (',' import_as_name)*) |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5553 | import_as_name: NAME [NAME NAME] |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5554 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5555 | if (STR(CHILD(n, 0))[0] == 'f') { /* from */ |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5556 | node *dotname = CHILD(n, 1); |
| 5557 | if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) { |
| 5558 | /* check for bogus imports */ |
| 5559 | if (n->n_lineno >= st->st_future->ff_last_lineno) { |
| 5560 | PyErr_SetString(PyExc_SyntaxError, |
| 5561 | LATE_FUTURE); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5562 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5563 | n->n_lineno); |
| 5564 | st->st_errors++; |
| 5565 | return; |
| 5566 | } |
| 5567 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5568 | if (TYPE(CHILD(n, 3)) == STAR) { |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 5569 | if (st->st_cur->ste_type != TYPE_MODULE) { |
Jeremy Hylton | 6a53bd8 | 2001-08-06 20:34:25 +0000 | [diff] [blame] | 5570 | if (symtable_warn(st, |
| 5571 | "import * only allowed at module level") < 0) |
| 5572 | return; |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 5573 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5574 | st->st_cur->ste_optimized |= OPT_IMPORT_STAR; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5575 | st->st_cur->ste_opt_lineno = n->n_lineno; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5576 | } else { |
| 5577 | for (i = 3; i < NCH(n); i += 2) { |
| 5578 | node *c = CHILD(n, i); |
| 5579 | if (NCH(c) > 1) /* import as */ |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5580 | symtable_assign(st, CHILD(c, 2), |
| 5581 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5582 | else |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5583 | symtable_assign(st, CHILD(c, 0), |
| 5584 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5585 | } |
| 5586 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5587 | } else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5588 | for (i = 1; i < NCH(n); i += 2) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5589 | symtable_assign(st, CHILD(n, i), DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5590 | } |
| 5591 | } |
| 5592 | } |
| 5593 | |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5594 | /* The third argument to symatble_assign() is a flag to be passed to |
| 5595 | symtable_add_def() if it is eventually called. The flag is useful |
| 5596 | to specify the particular type of assignment that should be |
| 5597 | recorded, e.g. an assignment caused by import. |
| 5598 | */ |
| 5599 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5600 | static void |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5601 | symtable_assign(struct symtable *st, node *n, int def_flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5602 | { |
| 5603 | node *tmp; |
| 5604 | int i; |
| 5605 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5606 | loop: |
| 5607 | switch (TYPE(n)) { |
| 5608 | case lambdef: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5609 | /* invalid assignment, e.g. lambda x:x=2. The next |
| 5610 | pass will catch this error. */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5611 | return; |
| 5612 | case power: |
| 5613 | if (NCH(n) > 2) { |
| 5614 | for (i = 2; i < NCH(n); ++i) |
| 5615 | if (TYPE(CHILD(n, i)) != DOUBLESTAR) |
| 5616 | symtable_node(st, CHILD(n, i)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5617 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5618 | if (NCH(n) > 1) { |
| 5619 | symtable_node(st, CHILD(n, 0)); |
| 5620 | symtable_node(st, CHILD(n, 1)); |
| 5621 | } else { |
| 5622 | n = CHILD(n, 0); |
| 5623 | goto loop; |
| 5624 | } |
| 5625 | return; |
| 5626 | case listmaker: |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5627 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
| 5628 | /* XXX This is an error, but the next pass |
| 5629 | will catch it. */ |
| 5630 | return; |
| 5631 | } else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5632 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5633 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5634 | } |
| 5635 | return; |
| 5636 | case exprlist: |
| 5637 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 5638 | case testlist1: |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5639 | if (NCH(n) == 1) { |
| 5640 | n = CHILD(n, 0); |
| 5641 | goto loop; |
| 5642 | } |
| 5643 | else { |
| 5644 | int i; |
| 5645 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5646 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5647 | return; |
| 5648 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5649 | case atom: |
| 5650 | tmp = CHILD(n, 0); |
| 5651 | if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) { |
| 5652 | n = CHILD(n, 1); |
| 5653 | goto loop; |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 5654 | } else if (TYPE(tmp) == NAME) { |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 5655 | if (strcmp(STR(tmp), "__debug__") == 0) { |
| 5656 | PyErr_SetString(PyExc_SyntaxError, |
| 5657 | ASSIGN_DEBUG); |
| 5658 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | 86424e3 | 2001-12-04 02:41:46 +0000 | [diff] [blame] | 5659 | n->n_lineno); |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 5660 | st->st_errors++; |
| 5661 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5662 | symtable_add_def(st, STR(tmp), DEF_LOCAL | def_flag); |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 5663 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5664 | return; |
| 5665 | case dotted_as_name: |
| 5666 | if (NCH(n) == 3) |
| 5667 | symtable_add_def(st, STR(CHILD(n, 2)), |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5668 | DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5669 | else |
| 5670 | symtable_add_def(st, |
| 5671 | STR(CHILD(CHILD(n, |
| 5672 | 0), 0)), |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5673 | DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5674 | return; |
| 5675 | case dotted_name: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5676 | symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5677 | return; |
| 5678 | case NAME: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5679 | symtable_add_def(st, STR(n), DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5680 | return; |
| 5681 | default: |
| 5682 | if (NCH(n) == 0) |
| 5683 | return; |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 5684 | if (NCH(n) == 1) { |
| 5685 | n = CHILD(n, 0); |
| 5686 | goto loop; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5687 | } |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 5688 | /* Should only occur for errors like x + 1 = 1, |
| 5689 | which will be caught in the next pass. */ |
| 5690 | for (i = 0; i < NCH(n); ++i) |
| 5691 | if (TYPE(CHILD(n, i)) >= single_input) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5692 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5693 | } |
| 5694 | } |