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 \ |
| 64 | "name '%.400s' is a function paramter and declared global" |
| 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 | |
| 76 | static struct memberlist 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 | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 94 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 95 | code_getattr(PyCodeObject *co, char *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 96 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 97 | return PyMember_Get((char *)co, code_memberlist, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 98 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 99 | |
| 100 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 101 | code_dealloc(PyCodeObject *co) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 102 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 103 | Py_XDECREF(co->co_code); |
| 104 | Py_XDECREF(co->co_consts); |
| 105 | Py_XDECREF(co->co_names); |
Guido van Rossum | 275558c | 1997-07-25 20:13:49 +0000 | [diff] [blame] | 106 | Py_XDECREF(co->co_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 107 | Py_XDECREF(co->co_freevars); |
| 108 | Py_XDECREF(co->co_cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 109 | Py_XDECREF(co->co_filename); |
| 110 | Py_XDECREF(co->co_name); |
Guido van Rossum | 275558c | 1997-07-25 20:13:49 +0000 | [diff] [blame] | 111 | Py_XDECREF(co->co_lnotab); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 112 | PyObject_DEL(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 115 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 116 | code_repr(PyCodeObject *co) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 117 | { |
| 118 | char buf[500]; |
| 119 | int lineno = -1; |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 120 | char *filename = "???"; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 121 | char *name = "???"; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 122 | |
Guido van Rossum | a396a88 | 2000-04-07 01:21:36 +0000 | [diff] [blame] | 123 | if (co->co_firstlineno != 0) |
| 124 | lineno = co->co_firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 125 | if (co->co_filename && PyString_Check(co->co_filename)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 126 | filename = PyString_AS_STRING(co->co_filename); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 127 | if (co->co_name && PyString_Check(co->co_name)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 128 | name = PyString_AS_STRING(co->co_name); |
Fred Drake | 615ae55 | 2000-06-30 16:20:13 +0000 | [diff] [blame] | 129 | sprintf(buf, "<code object %.100s at %p, file \"%.300s\", line %d>", |
| 130 | name, co, filename, lineno); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 131 | return PyString_FromString(buf); |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 134 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 135 | code_compare(PyCodeObject *co, PyCodeObject *cp) |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 136 | { |
| 137 | int cmp; |
Guido van Rossum | 4467959 | 2000-04-10 16:20:31 +0000 | [diff] [blame] | 138 | cmp = PyObject_Compare(co->co_name, cp->co_name); |
| 139 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 140 | cmp = co->co_argcount - cp->co_argcount; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 141 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 142 | cmp = co->co_nlocals - cp->co_nlocals; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 143 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 144 | cmp = co->co_flags - cp->co_flags; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 145 | if (cmp) return cmp; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 146 | cmp = PyObject_Compare(co->co_code, cp->co_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 147 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 148 | cmp = PyObject_Compare(co->co_consts, cp->co_consts); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 149 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 150 | cmp = PyObject_Compare(co->co_names, cp->co_names); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 151 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 152 | cmp = PyObject_Compare(co->co_varnames, cp->co_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 153 | if (cmp) return cmp; |
| 154 | cmp = PyObject_Compare(co->co_freevars, cp->co_freevars); |
| 155 | if (cmp) return cmp; |
| 156 | cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 157 | return cmp; |
| 158 | } |
| 159 | |
| 160 | static long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 161 | code_hash(PyCodeObject *co) |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 162 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 163 | long h, h0, h1, h2, h3, h4, h5, h6; |
Guido van Rossum | 4467959 | 2000-04-10 16:20:31 +0000 | [diff] [blame] | 164 | h0 = PyObject_Hash(co->co_name); |
| 165 | if (h0 == -1) return -1; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 166 | h1 = PyObject_Hash(co->co_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 167 | if (h1 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 168 | h2 = PyObject_Hash(co->co_consts); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 169 | if (h2 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 170 | h3 = PyObject_Hash(co->co_names); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 171 | if (h3 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 172 | h4 = PyObject_Hash(co->co_varnames); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 173 | if (h4 == -1) return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 174 | h5 = PyObject_Hash(co->co_freevars); |
| 175 | if (h5 == -1) return -1; |
| 176 | h6 = PyObject_Hash(co->co_cellvars); |
| 177 | if (h6 == -1) return -1; |
| 178 | h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 179 | co->co_argcount ^ co->co_nlocals ^ co->co_flags; |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 180 | if (h == -1) h = -2; |
| 181 | return h; |
| 182 | } |
| 183 | |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 184 | /* XXX code objects need to participate in GC? */ |
| 185 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 186 | PyTypeObject PyCode_Type = { |
| 187 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 188 | 0, |
| 189 | "code", |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 190 | sizeof(PyCodeObject), |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 191 | 0, |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 192 | (destructor)code_dealloc, /*tp_dealloc*/ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 193 | 0, /*tp_print*/ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 194 | (getattrfunc)code_getattr, /*tp_getattr*/ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 195 | 0, /*tp_setattr*/ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 196 | (cmpfunc)code_compare, /*tp_compare*/ |
| 197 | (reprfunc)code_repr, /*tp_repr*/ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 198 | 0, /*tp_as_number*/ |
| 199 | 0, /*tp_as_sequence*/ |
| 200 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 201 | (hashfunc)code_hash, /*tp_hash*/ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 202 | }; |
| 203 | |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 204 | #define NAME_CHARS \ |
| 205 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" |
| 206 | |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 207 | /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */ |
| 208 | |
| 209 | static int |
| 210 | all_name_chars(unsigned char *s) |
| 211 | { |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 212 | static char ok_name_char[256]; |
| 213 | static unsigned char *name_chars = (unsigned char *)NAME_CHARS; |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 214 | |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 215 | if (ok_name_char[*name_chars] == 0) { |
| 216 | unsigned char *p; |
| 217 | for (p = name_chars; *p; p++) |
| 218 | ok_name_char[*p] = 1; |
| 219 | } |
| 220 | while (*s) { |
| 221 | if (ok_name_char[*s++] == 0) |
| 222 | return 0; |
| 223 | } |
| 224 | return 1; |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 227 | static int |
| 228 | intern_strings(PyObject *tuple) |
| 229 | { |
| 230 | int i; |
| 231 | |
| 232 | for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { |
| 233 | PyObject *v = PyTuple_GET_ITEM(tuple, i); |
| 234 | if (v == NULL || !PyString_Check(v)) { |
| 235 | Py_FatalError("non-string found in code slot"); |
| 236 | PyErr_BadInternalCall(); |
| 237 | return -1; |
| 238 | } |
| 239 | PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i)); |
| 240 | } |
| 241 | return 0; |
| 242 | } |
| 243 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 244 | PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 245 | PyCode_New(int argcount, int nlocals, int stacksize, int flags, |
| 246 | PyObject *code, PyObject *consts, PyObject *names, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 247 | PyObject *varnames, PyObject *freevars, PyObject *cellvars, |
| 248 | PyObject *filename, PyObject *name, int firstlineno, |
| 249 | PyObject *lnotab) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 250 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 251 | PyCodeObject *co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 252 | int i; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 253 | PyBufferProcs *pb; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 254 | /* Check argument types */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 255 | if (argcount < 0 || nlocals < 0 || |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 256 | code == NULL || |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 257 | consts == NULL || !PyTuple_Check(consts) || |
| 258 | names == NULL || !PyTuple_Check(names) || |
| 259 | varnames == NULL || !PyTuple_Check(varnames) || |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 260 | freevars == NULL || !PyTuple_Check(freevars) || |
| 261 | cellvars == NULL || !PyTuple_Check(cellvars) || |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 262 | name == NULL || !PyString_Check(name) || |
| 263 | filename == NULL || !PyString_Check(filename) || |
| 264 | lnotab == NULL || !PyString_Check(lnotab)) { |
| 265 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 266 | return NULL; |
| 267 | } |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 268 | pb = code->ob_type->tp_as_buffer; |
| 269 | if (pb == NULL || |
| 270 | pb->bf_getreadbuffer == NULL || |
| 271 | pb->bf_getsegcount == NULL || |
| 272 | (*pb->bf_getsegcount)(code, NULL) != 1) |
| 273 | { |
| 274 | PyErr_BadInternalCall(); |
| 275 | return NULL; |
| 276 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 277 | intern_strings(names); |
| 278 | intern_strings(varnames); |
| 279 | if (freevars == NULL) |
| 280 | freevars = PyTuple_New(0); |
| 281 | intern_strings(freevars); |
| 282 | if (cellvars == NULL) |
| 283 | cellvars = PyTuple_New(0); |
| 284 | intern_strings(cellvars); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 285 | /* Intern selected string constants */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 286 | for (i = PyTuple_Size(consts); --i >= 0; ) { |
| 287 | PyObject *v = PyTuple_GetItem(consts, i); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 288 | if (!PyString_Check(v)) |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 289 | continue; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 290 | if (!all_name_chars((unsigned char *)PyString_AS_STRING(v))) |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 291 | continue; |
| 292 | PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 293 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 294 | co = PyObject_NEW(PyCodeObject, &PyCode_Type); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 295 | if (co != NULL) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 296 | co->co_argcount = argcount; |
| 297 | co->co_nlocals = nlocals; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 298 | co->co_stacksize = stacksize; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 299 | co->co_flags = flags; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 300 | Py_INCREF(code); |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 301 | co->co_code = code; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 302 | Py_INCREF(consts); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 303 | co->co_consts = consts; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 304 | Py_INCREF(names); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 305 | co->co_names = names; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 306 | Py_INCREF(varnames); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 307 | co->co_varnames = varnames; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 308 | Py_INCREF(freevars); |
| 309 | co->co_freevars = freevars; |
| 310 | Py_INCREF(cellvars); |
| 311 | co->co_cellvars = cellvars; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 312 | Py_INCREF(filename); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 313 | co->co_filename = filename; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 314 | Py_INCREF(name); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 315 | co->co_name = name; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 316 | co->co_firstlineno = firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 317 | Py_INCREF(lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 318 | co->co_lnotab = lnotab; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 319 | } |
| 320 | return co; |
| 321 | } |
| 322 | |
| 323 | |
| 324 | /* Data structure used internally */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 325 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 326 | /* The compiler uses two passes to generate bytecodes. The first pass |
| 327 | builds the symbol table. The second pass generates the bytecode. |
| 328 | |
| 329 | The first pass uses a single symtable struct. The second pass uses |
| 330 | a compiling struct for each code block. The compiling structs |
| 331 | share a reference to the symtable. |
| 332 | |
| 333 | The two passes communicate via symtable_load_symbols() and via |
| 334 | is_local() and is_global(). The former initializes several slots |
| 335 | in the compiling struct: c_varnames, c_locals, c_nlocals, |
| 336 | c_argcount, c_globals, and c_flags. |
| 337 | */ |
| 338 | |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 339 | /* All about c_lnotab. |
| 340 | |
| 341 | c_lnotab is an array of unsigned bytes disguised as a Python string. In -O |
| 342 | mode, SET_LINENO opcodes aren't generated, and bytecode offsets are mapped |
| 343 | to source code line #s (when needed for tracebacks) via c_lnotab instead. |
| 344 | The array is conceptually a list of |
| 345 | (bytecode offset increment, line number increment) |
| 346 | pairs. The details are important and delicate, best illustrated by example: |
| 347 | |
| 348 | byte code offset source code line number |
| 349 | 0 1 |
| 350 | 6 2 |
| 351 | 50 7 |
| 352 | 350 307 |
| 353 | 361 308 |
| 354 | |
| 355 | The first trick is that these numbers aren't stored, only the increments |
| 356 | from one row to the next (this doesn't really work, but it's a start): |
| 357 | |
| 358 | 0, 1, 6, 1, 44, 5, 300, 300, 11, 1 |
| 359 | |
| 360 | The second trick is that an unsigned byte can't hold negative values, or |
| 361 | values larger than 255, so (a) there's a deep assumption that byte code |
| 362 | offsets and their corresponding line #s both increase monotonically, and (b) |
| 363 | if at least one column jumps by more than 255 from one row to the next, more |
| 364 | than one pair is written to the table. In case #b, there's no way to know |
| 365 | from looking at the table later how many were written. That's the delicate |
| 366 | part. A user of c_lnotab desiring to find the source line number |
| 367 | corresponding to a bytecode address A should do something like this |
| 368 | |
| 369 | lineno = addr = 0 |
| 370 | for addr_incr, line_incr in c_lnotab: |
| 371 | addr += addr_incr |
| 372 | if addr > A: |
| 373 | return lineno |
| 374 | lineno += line_incr |
| 375 | |
| 376 | In order for this to work, when the addr field increments by more than 255, |
| 377 | the line # increment in each pair generated must be 0 until the remaining addr |
| 378 | increment is < 256. So, in the example above, com_set_lineno should not (as |
| 379 | was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to |
| 380 | 255, 0, 45, 255, 0, 45. |
| 381 | */ |
| 382 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 383 | struct compiling { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 384 | PyObject *c_code; /* string */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 385 | PyObject *c_consts; /* list of objects */ |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 386 | PyObject *c_const_dict; /* inverse of c_consts */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 387 | PyObject *c_names; /* list of strings (names) */ |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 388 | PyObject *c_name_dict; /* inverse of c_names */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 389 | PyObject *c_globals; /* dictionary (value=None) */ |
| 390 | PyObject *c_locals; /* dictionary (value=localID) */ |
| 391 | PyObject *c_varnames; /* list (inverse of c_locals) */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 392 | PyObject *c_freevars; /* dictionary (value=None) */ |
| 393 | PyObject *c_cellvars; /* list */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 394 | int c_nlocals; /* index of next local */ |
| 395 | int c_argcount; /* number of top-level arguments */ |
| 396 | int c_flags; /* same as co_flags */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 397 | int c_nexti; /* index into c_code */ |
| 398 | int c_errors; /* counts errors occurred */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 399 | int c_infunction; /* set when compiling a function */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 400 | int c_interactive; /* generating code for interactive command */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 401 | int c_loops; /* counts nested loops */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 402 | int c_begin; /* begin of current loop, for 'continue' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 403 | int c_block[CO_MAXBLOCKS]; /* stack of block types */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 404 | int c_nblocks; /* current block stack level */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 405 | char *c_filename; /* filename of current node */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 406 | char *c_name; /* name of object (e.g. function) */ |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 407 | int c_lineno; /* Current line number */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 408 | int c_stacklevel; /* Current stack level */ |
| 409 | int c_maxstacklevel; /* Maximum stack level */ |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 410 | int c_firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 411 | PyObject *c_lnotab; /* Table mapping address to line number */ |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 412 | int c_last_addr, c_last_line, c_lnotab_next; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 413 | char *c_private; /* for private name mangling */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 414 | int c_tmpname; /* temporary local name counter */ |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 415 | int c_nested; /* Is block nested funcdef or lamdef? */ |
| 416 | int c_closure; /* Is nested w/freevars? */ |
| 417 | struct symtable *c_symtable; /* pointer to module symbol table */ |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 418 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 419 | }; |
| 420 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 421 | static int |
| 422 | is_free(int v) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 423 | { |
| 424 | if ((v & (USE | DEF_FREE)) |
| 425 | && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL))) |
| 426 | return 1; |
| 427 | if (v & DEF_FREE_CLASS) |
| 428 | return 1; |
| 429 | return 0; |
| 430 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 431 | |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 432 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 433 | com_error(struct compiling *c, PyObject *exc, char *msg) |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 434 | { |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 435 | PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL; |
| 436 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 437 | if (c == NULL) { |
| 438 | /* Error occurred via symtable call to |
| 439 | is_constant_false */ |
| 440 | PyErr_SetString(exc, msg); |
| 441 | return; |
| 442 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 443 | c->c_errors++; |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 444 | if (c->c_lineno < 1 || c->c_interactive) { |
| 445 | /* Unknown line number or interactive input */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 446 | PyErr_SetString(exc, msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 447 | return; |
| 448 | } |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 449 | v = PyString_FromString(msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 450 | if (v == NULL) |
| 451 | return; /* MemoryError, too bad */ |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 452 | |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 453 | line = PyErr_ProgramText(c->c_filename, c->c_lineno); |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 454 | if (line == NULL) { |
| 455 | Py_INCREF(Py_None); |
| 456 | line = Py_None; |
| 457 | } |
| 458 | t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno, |
| 459 | Py_None, line); |
| 460 | if (t == NULL) |
| 461 | goto exit; |
| 462 | w = Py_BuildValue("(OO)", v, t); |
| 463 | if (w == NULL) |
| 464 | goto exit; |
| 465 | PyErr_SetObject(exc, w); |
| 466 | exit: |
| 467 | Py_XDECREF(t); |
| 468 | Py_XDECREF(v); |
| 469 | Py_XDECREF(w); |
| 470 | Py_XDECREF(line); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 473 | /* Interface to the block stack */ |
| 474 | |
| 475 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 476 | block_push(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 477 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 478 | if (c->c_nblocks >= CO_MAXBLOCKS) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 479 | com_error(c, PyExc_SystemError, |
| 480 | "too many statically nested blocks"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 481 | } |
| 482 | else { |
| 483 | c->c_block[c->c_nblocks++] = type; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 488 | block_pop(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 489 | { |
| 490 | if (c->c_nblocks > 0) |
| 491 | c->c_nblocks--; |
| 492 | 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] | 493 | com_error(c, PyExc_SystemError, "bad block pop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 497 | /* Prototype forward declarations */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 498 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 499 | static int com_init(struct compiling *, char *); |
| 500 | static void com_free(struct compiling *); |
| 501 | static void com_push(struct compiling *, int); |
| 502 | static void com_pop(struct compiling *, int); |
| 503 | static void com_done(struct compiling *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 504 | static void com_node(struct compiling *, node *); |
| 505 | static void com_factor(struct compiling *, node *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 506 | static void com_addbyte(struct compiling *, int); |
| 507 | static void com_addint(struct compiling *, int); |
| 508 | static void com_addoparg(struct compiling *, int, int); |
| 509 | static void com_addfwref(struct compiling *, int, int *); |
| 510 | static void com_backpatch(struct compiling *, int); |
| 511 | static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *); |
| 512 | static int com_addconst(struct compiling *, PyObject *); |
| 513 | static int com_addname(struct compiling *, PyObject *); |
| 514 | static void com_addopname(struct compiling *, int, node *); |
| 515 | static void com_list(struct compiling *, node *, int); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 516 | static void com_list_iter(struct compiling *, node *, node *, char *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 517 | static int com_argdefs(struct compiling *, node *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 518 | static void com_assign(struct compiling *, node *, int, node *); |
| 519 | static void com_assign_name(struct compiling *, node *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 520 | static PyCodeObject *icompile(node *, struct compiling *); |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 521 | static PyCodeObject *jcompile(node *, char *, struct compiling *, |
| 522 | PyCompilerFlags *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 523 | static PyObject *parsestrplus(node *); |
| 524 | static PyObject *parsestr(char *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 525 | static node *get_rawdocstring(node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 526 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 527 | static int get_ref_type(struct compiling *, char *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 528 | |
| 529 | /* symtable operations */ |
| 530 | static int symtable_build(struct compiling *, node *); |
| 531 | static int symtable_load_symbols(struct compiling *); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 532 | static struct symtable *symtable_init(void); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 533 | static void symtable_enter_scope(struct symtable *, char *, int, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 534 | static int symtable_exit_scope(struct symtable *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 535 | static int symtable_add_def(struct symtable *, char *, int); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 536 | static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 537 | |
| 538 | static void symtable_node(struct symtable *, node *); |
| 539 | static void symtable_funcdef(struct symtable *, node *); |
| 540 | static void symtable_default_args(struct symtable *, node *); |
| 541 | static void symtable_params(struct symtable *, node *); |
| 542 | static void symtable_params_fplist(struct symtable *, node *n); |
| 543 | static void symtable_global(struct symtable *, node *); |
| 544 | static void symtable_import(struct symtable *, node *); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 545 | static void symtable_assign(struct symtable *, node *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 546 | static void symtable_list_comprehension(struct symtable *, node *); |
| 547 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 548 | static int symtable_update_free_vars(struct symtable *); |
| 549 | static int symtable_undo_free(struct symtable *, PyObject *, PyObject *); |
| 550 | static int symtable_check_global(struct symtable *, PyObject *, PyObject *); |
| 551 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 552 | /* helper */ |
| 553 | static void |
| 554 | do_pad(int pad) |
| 555 | { |
| 556 | int i; |
| 557 | for (i = 0; i < pad; ++i) |
| 558 | fprintf(stderr, " "); |
| 559 | } |
| 560 | |
| 561 | static void |
| 562 | dump(node *n, int pad, int depth) |
| 563 | { |
| 564 | int i; |
| 565 | if (depth == 0) |
| 566 | return; |
| 567 | do_pad(pad); |
| 568 | fprintf(stderr, "%d: %s\n", TYPE(n), STR(n)); |
| 569 | if (depth > 0) |
| 570 | depth--; |
| 571 | for (i = 0; i < NCH(n); ++i) |
| 572 | dump(CHILD(n, i), pad + 1, depth); |
| 573 | } |
| 574 | |
| 575 | #define DUMP(N) dump(N, 0, -1) |
| 576 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 577 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 578 | com_init(struct compiling *c, char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 579 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 580 | memset((void *)c, '\0', sizeof(struct compiling)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 581 | if ((c->c_code = PyString_FromStringAndSize((char *)NULL, |
| 582 | 1000)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 583 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 584 | if ((c->c_consts = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 585 | goto fail; |
| 586 | if ((c->c_const_dict = PyDict_New()) == NULL) |
| 587 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 588 | if ((c->c_names = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 589 | goto fail; |
| 590 | if ((c->c_name_dict = PyDict_New()) == NULL) |
| 591 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 592 | if ((c->c_locals = PyDict_New()) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 593 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 594 | if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL, |
| 595 | 1000)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 596 | goto fail; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 597 | c->c_globals = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 598 | c->c_varnames = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 599 | c->c_freevars = NULL; |
| 600 | c->c_cellvars = NULL; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 601 | c->c_nlocals = 0; |
| 602 | c->c_argcount = 0; |
| 603 | c->c_flags = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 604 | c->c_nexti = 0; |
| 605 | c->c_errors = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 606 | c->c_infunction = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 607 | c->c_interactive = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 608 | c->c_loops = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 609 | c->c_begin = 0; |
| 610 | c->c_nblocks = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 611 | c->c_filename = filename; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 612 | c->c_name = "?"; |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 613 | c->c_lineno = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 614 | c->c_stacklevel = 0; |
| 615 | c->c_maxstacklevel = 0; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 616 | c->c_firstlineno = 0; |
| 617 | c->c_last_addr = 0; |
| 618 | c->c_last_line = 0; |
Barry Warsaw | 174e801 | 2001-01-22 04:35:57 +0000 | [diff] [blame] | 619 | c->c_lnotab_next = 0; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 620 | c->c_tmpname = 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 621 | c->c_nested = 0; |
| 622 | c->c_closure = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 623 | c->c_symtable = NULL; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 624 | return 1; |
| 625 | |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 626 | fail: |
| 627 | com_free(c); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 632 | com_free(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 633 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 634 | Py_XDECREF(c->c_code); |
| 635 | Py_XDECREF(c->c_consts); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 636 | Py_XDECREF(c->c_const_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 637 | Py_XDECREF(c->c_names); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 638 | Py_XDECREF(c->c_name_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 639 | Py_XDECREF(c->c_globals); |
| 640 | Py_XDECREF(c->c_locals); |
| 641 | Py_XDECREF(c->c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 642 | Py_XDECREF(c->c_freevars); |
| 643 | Py_XDECREF(c->c_cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 644 | Py_XDECREF(c->c_lnotab); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 645 | if (c->c_future) |
| 646 | PyMem_Free((void *)c->c_future); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 650 | com_push(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 651 | { |
| 652 | c->c_stacklevel += n; |
| 653 | if (c->c_stacklevel > c->c_maxstacklevel) |
| 654 | c->c_maxstacklevel = c->c_stacklevel; |
| 655 | } |
| 656 | |
| 657 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 658 | com_pop(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 659 | { |
| 660 | if (c->c_stacklevel < n) { |
Guido van Rossum | 90f827c | 1998-08-25 18:22:17 +0000 | [diff] [blame] | 661 | /* fprintf(stderr, |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 662 | "%s:%d: underflow! nexti=%d, level=%d, n=%d\n", |
| 663 | c->c_filename, c->c_lineno, |
Guido van Rossum | 90f827c | 1998-08-25 18:22:17 +0000 | [diff] [blame] | 664 | c->c_nexti, c->c_stacklevel, n); */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 665 | c->c_stacklevel = 0; |
| 666 | } |
| 667 | else |
| 668 | c->c_stacklevel -= n; |
| 669 | } |
| 670 | |
| 671 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 672 | com_done(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 673 | { |
| 674 | if (c->c_code != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 675 | _PyString_Resize(&c->c_code, c->c_nexti); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 676 | if (c->c_lnotab != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 677 | _PyString_Resize(&c->c_lnotab, c->c_lnotab_next); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 681 | com_addbyte(struct compiling *c, int byte) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 682 | { |
| 683 | int len; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 684 | /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 685 | assert(byte >= 0 && byte <= 255); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 686 | if (byte < 0 || byte > 255) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 687 | com_error(c, PyExc_SystemError, |
| 688 | "com_addbyte: byte out of range"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 689 | } |
| 690 | if (c->c_code == NULL) |
| 691 | return; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 692 | len = PyString_Size(c->c_code); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 693 | if (c->c_nexti >= len) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 694 | if (_PyString_Resize(&c->c_code, len+1000) != 0) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 695 | c->c_errors++; |
| 696 | return; |
| 697 | } |
| 698 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 699 | PyString_AsString(c->c_code)[c->c_nexti++] = byte; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 703 | com_addint(struct compiling *c, int x) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 704 | { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 705 | com_addbyte(c, x & 0xff); |
| 706 | com_addbyte(c, x >> 8); /* XXX x should be positive */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 710 | com_add_lnotab(struct compiling *c, int addr, int line) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 711 | { |
| 712 | int size; |
| 713 | char *p; |
| 714 | if (c->c_lnotab == NULL) |
| 715 | return; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 716 | size = PyString_Size(c->c_lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 717 | if (c->c_lnotab_next+2 > size) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 718 | if (_PyString_Resize(&c->c_lnotab, size + 1000) < 0) { |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 719 | c->c_errors++; |
| 720 | return; |
| 721 | } |
| 722 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 723 | p = PyString_AsString(c->c_lnotab) + c->c_lnotab_next; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 724 | *p++ = addr; |
| 725 | *p++ = line; |
| 726 | c->c_lnotab_next += 2; |
| 727 | } |
| 728 | |
| 729 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 730 | com_set_lineno(struct compiling *c, int lineno) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 731 | { |
| 732 | c->c_lineno = lineno; |
| 733 | if (c->c_firstlineno == 0) { |
| 734 | c->c_firstlineno = c->c_last_line = lineno; |
| 735 | } |
| 736 | else { |
| 737 | int incr_addr = c->c_nexti - c->c_last_addr; |
| 738 | int incr_line = lineno - c->c_last_line; |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 739 | while (incr_addr > 255) { |
| 740 | com_add_lnotab(c, 255, 0); |
| 741 | incr_addr -= 255; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 742 | } |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 743 | while (incr_line > 255) { |
| 744 | com_add_lnotab(c, incr_addr, 255); |
| 745 | incr_line -=255; |
| 746 | incr_addr = 0; |
| 747 | } |
| 748 | if (incr_addr > 0 || incr_line > 0) |
| 749 | com_add_lnotab(c, incr_addr, incr_line); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 750 | c->c_last_addr = c->c_nexti; |
| 751 | c->c_last_line = lineno; |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 756 | com_addoparg(struct compiling *c, int op, int arg) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 757 | { |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 758 | int extended_arg = arg >> 16; |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 759 | if (op == SET_LINENO) { |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 760 | com_set_lineno(c, arg); |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 761 | if (Py_OptimizeFlag) |
| 762 | return; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 763 | } |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 764 | if (extended_arg){ |
| 765 | com_addbyte(c, EXTENDED_ARG); |
| 766 | com_addint(c, extended_arg); |
| 767 | arg &= 0xffff; |
| 768 | } |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 769 | com_addbyte(c, op); |
| 770 | com_addint(c, arg); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 774 | com_addfwref(struct compiling *c, int op, int *p_anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 775 | { |
| 776 | /* Compile a forward reference for backpatching */ |
| 777 | int here; |
| 778 | int anchor; |
| 779 | com_addbyte(c, op); |
| 780 | here = c->c_nexti; |
| 781 | anchor = *p_anchor; |
| 782 | *p_anchor = here; |
| 783 | com_addint(c, anchor == 0 ? 0 : here - anchor); |
| 784 | } |
| 785 | |
| 786 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 787 | com_backpatch(struct compiling *c, int anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 788 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 789 | unsigned char *code = (unsigned char *) PyString_AsString(c->c_code); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 790 | int target = c->c_nexti; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 791 | int dist; |
| 792 | int prev; |
| 793 | for (;;) { |
| 794 | /* Make the JUMP instruction at anchor point to target */ |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 795 | prev = code[anchor] + (code[anchor+1] << 8); |
| 796 | dist = target - (anchor+2); |
| 797 | code[anchor] = dist & 0xff; |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 798 | dist >>= 8; |
| 799 | code[anchor+1] = dist; |
| 800 | dist >>= 8; |
| 801 | if (dist) { |
| 802 | com_error(c, PyExc_SystemError, |
| 803 | "com_backpatch: offset too large"); |
| 804 | break; |
| 805 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 806 | if (!prev) |
| 807 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 808 | anchor -= prev; |
| 809 | } |
| 810 | } |
| 811 | |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 812 | /* Handle literals and names uniformly */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 813 | |
| 814 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 815 | com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 816 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 817 | PyObject *w, *t, *np=NULL; |
| 818 | long n; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 819 | |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 820 | t = Py_BuildValue("(OO)", v, v->ob_type); |
| 821 | if (t == NULL) |
| 822 | goto fail; |
| 823 | w = PyDict_GetItem(dict, t); |
| 824 | if (w != NULL) { |
| 825 | n = PyInt_AsLong(w); |
| 826 | } else { |
| 827 | n = PyList_Size(list); |
| 828 | np = PyInt_FromLong(n); |
| 829 | if (np == NULL) |
| 830 | goto fail; |
| 831 | if (PyList_Append(list, v) != 0) |
| 832 | goto fail; |
| 833 | if (PyDict_SetItem(dict, t, np) != 0) |
| 834 | goto fail; |
| 835 | Py_DECREF(np); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 836 | } |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 837 | Py_DECREF(t); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 838 | return n; |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 839 | fail: |
| 840 | Py_XDECREF(np); |
| 841 | Py_XDECREF(t); |
| 842 | c->c_errors++; |
| 843 | return 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 847 | com_addconst(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 848 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 849 | 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] | 850 | } |
| 851 | |
| 852 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 853 | com_addname(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 854 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 855 | 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] | 856 | } |
| 857 | |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 858 | static int |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 859 | mangle(char *p, char *name, char *buffer, size_t maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 860 | { |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 861 | /* Name mangling: __private becomes _classname__private. |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 862 | This is independent from how the name is used. */ |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 863 | size_t nlen, plen; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 864 | if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_') |
| 865 | return 0; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 866 | nlen = strlen(name); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 867 | if (nlen+2 >= maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 868 | return 0; /* Don't mangle __extremely_long_names */ |
| 869 | if (name[nlen-1] == '_' && name[nlen-2] == '_') |
| 870 | return 0; /* Don't mangle __whatever__ */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 871 | /* Strip leading underscores from class name */ |
| 872 | while (*p == '_') |
| 873 | p++; |
| 874 | if (*p == '\0') |
| 875 | return 0; /* Don't mangle if class is just underscores */ |
| 876 | plen = strlen(p); |
| 877 | if (plen + nlen >= maxlen) |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 878 | plen = maxlen-nlen-2; /* Truncate class name if too long */ |
| 879 | /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 880 | buffer[0] = '_'; |
| 881 | strncpy(buffer+1, p, plen); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 882 | strcpy(buffer+1+plen, name); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 883 | return 1; |
| 884 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 885 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 886 | static void |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 887 | com_addop_name(struct compiling *c, int op, char *name) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 888 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 889 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 890 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 891 | char buffer[MANGLE_LEN]; |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 892 | |
| 893 | if (mangle(c->c_private, name, buffer, sizeof(buffer))) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 894 | name = buffer; |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 895 | if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 896 | c->c_errors++; |
| 897 | i = 255; |
| 898 | } |
| 899 | else { |
| 900 | i = com_addname(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 901 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 902 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 903 | com_addoparg(c, op, i); |
| 904 | } |
| 905 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 906 | #define NAME_LOCAL 0 |
| 907 | #define NAME_GLOBAL 1 |
| 908 | #define NAME_DEFAULT 2 |
| 909 | #define NAME_CLOSURE 3 |
| 910 | |
| 911 | static int |
| 912 | com_lookup_arg(PyObject *dict, PyObject *name) |
| 913 | { |
| 914 | PyObject *v = PyDict_GetItem(dict, name); |
| 915 | if (v == NULL) |
| 916 | return -1; |
| 917 | else |
| 918 | return PyInt_AS_LONG(v); |
| 919 | } |
| 920 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 921 | static void |
| 922 | com_addop_varname(struct compiling *c, int kind, char *name) |
| 923 | { |
| 924 | PyObject *v; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 925 | int i, reftype; |
| 926 | int scope = NAME_DEFAULT; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 927 | int op = STOP_CODE; |
| 928 | char buffer[MANGLE_LEN]; |
| 929 | |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 930 | if (mangle(c->c_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 931 | name = buffer; |
| 932 | if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { |
| 933 | c->c_errors++; |
| 934 | i = 255; |
| 935 | goto done; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 936 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 937 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 938 | reftype = get_ref_type(c, name); |
| 939 | switch (reftype) { |
| 940 | case LOCAL: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 941 | if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 942 | scope = NAME_LOCAL; |
| 943 | break; |
| 944 | case GLOBAL_EXPLICIT: |
| 945 | scope = NAME_GLOBAL; |
| 946 | break; |
| 947 | case GLOBAL_IMPLICIT: |
| 948 | if (c->c_flags & CO_OPTIMIZED) |
| 949 | scope = NAME_GLOBAL; |
| 950 | break; |
| 951 | case FREE: |
| 952 | case CELL: |
| 953 | scope = NAME_CLOSURE; |
| 954 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | i = com_addname(c, v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 958 | if (scope == NAME_LOCAL) |
| 959 | i = com_lookup_arg(c->c_locals, v); |
| 960 | else if (reftype == FREE) |
| 961 | i = com_lookup_arg(c->c_freevars, v); |
| 962 | else if (reftype == CELL) |
| 963 | i = com_lookup_arg(c->c_cellvars, v); |
| 964 | if (i == -1) { |
| 965 | c->c_errors++; /* XXX no exception set */ |
| 966 | i = 255; |
| 967 | goto done; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 968 | } |
| 969 | Py_DECREF(v); |
| 970 | |
| 971 | switch (kind) { |
| 972 | case VAR_LOAD: |
| 973 | switch (scope) { |
| 974 | case NAME_LOCAL: |
| 975 | op = LOAD_FAST; |
| 976 | break; |
| 977 | case NAME_GLOBAL: |
| 978 | op = LOAD_GLOBAL; |
| 979 | break; |
| 980 | case NAME_DEFAULT: |
| 981 | op = LOAD_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 982 | break; |
| 983 | case NAME_CLOSURE: |
| 984 | op = LOAD_DEREF; |
| 985 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 986 | } |
| 987 | break; |
| 988 | case VAR_STORE: |
| 989 | switch (scope) { |
| 990 | case NAME_LOCAL: |
| 991 | op = STORE_FAST; |
| 992 | break; |
| 993 | case NAME_GLOBAL: |
| 994 | op = STORE_GLOBAL; |
| 995 | break; |
| 996 | case NAME_DEFAULT: |
| 997 | op = STORE_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 998 | break; |
| 999 | case NAME_CLOSURE: |
| 1000 | op = STORE_DEREF; |
| 1001 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1002 | } |
| 1003 | break; |
| 1004 | case VAR_DELETE: |
| 1005 | switch (scope) { |
| 1006 | case NAME_LOCAL: |
| 1007 | op = DELETE_FAST; |
| 1008 | break; |
| 1009 | case NAME_GLOBAL: |
| 1010 | op = DELETE_GLOBAL; |
| 1011 | break; |
| 1012 | case NAME_DEFAULT: |
| 1013 | op = DELETE_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1014 | break; |
| 1015 | case NAME_CLOSURE: { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 1016 | char buf[500]; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1017 | sprintf(buf, DEL_CLOSURE_ERROR, name); |
| 1018 | com_error(c, PyExc_SyntaxError, buf); |
| 1019 | i = 255; |
| 1020 | break; |
| 1021 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1022 | } |
| 1023 | break; |
| 1024 | } |
| 1025 | done: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1026 | com_addoparg(c, op, i); |
| 1027 | } |
| 1028 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1029 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1030 | com_addopname(struct compiling *c, int op, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1031 | { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1032 | char *name; |
| 1033 | char buffer[1000]; |
| 1034 | /* XXX it is possible to write this code without the 1000 |
| 1035 | chars on the total length of dotted names, I just can't be |
| 1036 | bothered right now */ |
| 1037 | if (TYPE(n) == STAR) |
| 1038 | name = "*"; |
| 1039 | else if (TYPE(n) == dotted_name) { |
| 1040 | char *p = buffer; |
| 1041 | int i; |
| 1042 | name = buffer; |
| 1043 | for (i = 0; i < NCH(n); i += 2) { |
| 1044 | char *s = STR(CHILD(n, i)); |
| 1045 | if (p + strlen(s) > buffer + (sizeof buffer) - 2) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1046 | com_error(c, PyExc_MemoryError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1047 | "dotted_name too long"); |
Guido van Rossum | d9dfaf5 | 1995-02-17 15:04:57 +0000 | [diff] [blame] | 1048 | name = NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1049 | break; |
| 1050 | } |
| 1051 | if (p != buffer) |
| 1052 | *p++ = '.'; |
| 1053 | strcpy(p, s); |
| 1054 | p = strchr(p, '\0'); |
| 1055 | } |
| 1056 | } |
| 1057 | else { |
| 1058 | REQ(n, NAME); |
| 1059 | name = STR(n); |
| 1060 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1061 | com_addop_name(c, op, name); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1064 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1065 | parsenumber(struct compiling *co, char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1066 | { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1067 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1068 | long x; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1069 | double dx; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1070 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 1071 | Py_complex c; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1072 | int imflag; |
| 1073 | #endif |
| 1074 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1075 | errno = 0; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1076 | end = s + strlen(s) - 1; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1077 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 15ad9a6 | 1996-01-26 20:53:56 +0000 | [diff] [blame] | 1078 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1079 | #endif |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 1080 | if (*end == 'l' || *end == 'L') |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1081 | return PyLong_FromString(s, (char **)0, 0); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 1082 | if (s[0] == '0') |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1083 | x = (long) PyOS_strtoul(s, &end, 0); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 1084 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1085 | x = PyOS_strtol(s, &end, 0); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1086 | if (*end == '\0') { |
| 1087 | if (errno != 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1088 | com_error(co, PyExc_OverflowError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1089 | "integer literal too large"); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1090 | return NULL; |
| 1091 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1092 | return PyInt_FromLong(x); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1093 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1094 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1095 | #ifndef WITHOUT_COMPLEX |
| 1096 | if (imflag) { |
| 1097 | c.real = 0.; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1098 | PyFPE_START_PROTECT("atof", return 0) |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1099 | c.imag = atof(s); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1100 | PyFPE_END_PROTECT(c) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1101 | return PyComplex_FromCComplex(c); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1102 | } |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1103 | else |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1104 | #endif |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1105 | { |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1106 | PyFPE_START_PROTECT("atof", return 0) |
| 1107 | dx = atof(s); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1108 | PyFPE_END_PROTECT(dx) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1109 | return PyFloat_FromDouble(dx); |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1110 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1113 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1114 | parsestr(char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1115 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1116 | PyObject *v; |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1117 | size_t len; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1118 | char *buf; |
| 1119 | char *p; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1120 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1121 | int c; |
Guido van Rossum | 5f5e817 | 1997-04-06 03:41:40 +0000 | [diff] [blame] | 1122 | int first = *s; |
| 1123 | int quote = first; |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1124 | int rawmode = 0; |
| 1125 | int unicode = 0; |
| 1126 | if (isalpha(quote) || quote == '_') { |
| 1127 | if (quote == 'u' || quote == 'U') { |
| 1128 | quote = *++s; |
| 1129 | unicode = 1; |
| 1130 | } |
| 1131 | if (quote == 'r' || quote == 'R') { |
| 1132 | quote = *++s; |
| 1133 | rawmode = 1; |
| 1134 | } |
| 1135 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1136 | if (quote != '\'' && quote != '\"') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1137 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1138 | return NULL; |
| 1139 | } |
| 1140 | s++; |
| 1141 | len = strlen(s); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1142 | if (len > INT_MAX) { |
| 1143 | PyErr_SetString(PyExc_OverflowError, "string to parse is too long"); |
| 1144 | return NULL; |
| 1145 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1146 | if (s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1147 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1148 | return NULL; |
| 1149 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1150 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
| 1151 | s += 2; |
| 1152 | len -= 2; |
| 1153 | if (s[--len] != quote || s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1154 | PyErr_BadInternalCall(); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1155 | return NULL; |
| 1156 | } |
| 1157 | } |
Guido van Rossum | fdc8bdb | 2000-05-01 17:54:56 +0000 | [diff] [blame] | 1158 | if (unicode || Py_UnicodeFlag) { |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1159 | if (rawmode) |
| 1160 | return PyUnicode_DecodeRawUnicodeEscape( |
| 1161 | s, len, NULL); |
| 1162 | else |
| 1163 | return PyUnicode_DecodeUnicodeEscape( |
| 1164 | s, len, NULL); |
| 1165 | } |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1166 | if (rawmode || strchr(s, '\\') == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1167 | return PyString_FromStringAndSize(s, len); |
| 1168 | v = PyString_FromStringAndSize((char *)NULL, len); |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1169 | if (v == NULL) |
| 1170 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1171 | p = buf = PyString_AsString(v); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1172 | end = s + len; |
| 1173 | while (s < end) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1174 | if (*s != '\\') { |
| 1175 | *p++ = *s++; |
| 1176 | continue; |
| 1177 | } |
| 1178 | s++; |
| 1179 | switch (*s++) { |
| 1180 | /* XXX This assumes ASCII! */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1181 | case '\n': break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1182 | case '\\': *p++ = '\\'; break; |
| 1183 | case '\'': *p++ = '\''; break; |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1184 | case '\"': *p++ = '\"'; break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1185 | case 'b': *p++ = '\b'; break; |
| 1186 | case 'f': *p++ = '\014'; break; /* FF */ |
| 1187 | case 't': *p++ = '\t'; break; |
| 1188 | case 'n': *p++ = '\n'; break; |
| 1189 | case 'r': *p++ = '\r'; break; |
| 1190 | case 'v': *p++ = '\013'; break; /* VT */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1191 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 1192 | case '0': case '1': case '2': case '3': |
| 1193 | case '4': case '5': case '6': case '7': |
| 1194 | c = s[-1] - '0'; |
| 1195 | if ('0' <= *s && *s <= '7') { |
| 1196 | c = (c<<3) + *s++ - '0'; |
| 1197 | if ('0' <= *s && *s <= '7') |
| 1198 | c = (c<<3) + *s++ - '0'; |
| 1199 | } |
| 1200 | *p++ = c; |
| 1201 | break; |
| 1202 | case 'x': |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 1203 | if (isxdigit(Py_CHARMASK(s[0])) |
| 1204 | && isxdigit(Py_CHARMASK(s[1]))) { |
Guido van Rossum | ed1100f | 1997-10-20 23:24:07 +0000 | [diff] [blame] | 1205 | unsigned int x = 0; |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1206 | c = Py_CHARMASK(*s); |
| 1207 | s++; |
| 1208 | if (isdigit(c)) |
| 1209 | x = c - '0'; |
| 1210 | else if (islower(c)) |
| 1211 | x = 10 + c - 'a'; |
| 1212 | else |
| 1213 | x = 10 + c - 'A'; |
| 1214 | x = x << 4; |
| 1215 | c = Py_CHARMASK(*s); |
| 1216 | s++; |
| 1217 | if (isdigit(c)) |
| 1218 | x += c - '0'; |
| 1219 | else if (islower(c)) |
| 1220 | x += 10 + c - 'a'; |
| 1221 | else |
| 1222 | x += 10 + c - 'A'; |
Guido van Rossum | ed1100f | 1997-10-20 23:24:07 +0000 | [diff] [blame] | 1223 | *p++ = x; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1224 | break; |
| 1225 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 1226 | PyErr_SetString(PyExc_ValueError, |
| 1227 | "invalid \\x escape"); |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1228 | Py_DECREF(v); |
| 1229 | return NULL; |
| 1230 | default: |
| 1231 | *p++ = '\\'; |
| 1232 | *p++ = s[-1]; |
| 1233 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1234 | } |
| 1235 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1236 | _PyString_Resize(&v, (int)(p - buf)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1237 | return v; |
| 1238 | } |
| 1239 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1240 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1241 | parsestrplus(node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1242 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1243 | PyObject *v; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1244 | int i; |
| 1245 | REQ(CHILD(n, 0), STRING); |
| 1246 | if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) { |
| 1247 | /* String literal concatenation */ |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1248 | for (i = 1; i < NCH(n); i++) { |
| 1249 | PyObject *s; |
| 1250 | s = parsestr(STR(CHILD(n, i))); |
| 1251 | if (s == NULL) |
| 1252 | goto onError; |
| 1253 | if (PyString_Check(v) && PyString_Check(s)) { |
| 1254 | PyString_ConcatAndDel(&v, s); |
| 1255 | if (v == NULL) |
| 1256 | goto onError; |
| 1257 | } |
| 1258 | else { |
| 1259 | PyObject *temp; |
| 1260 | temp = PyUnicode_Concat(v, s); |
| 1261 | Py_DECREF(s); |
| 1262 | if (temp == NULL) |
| 1263 | goto onError; |
| 1264 | Py_DECREF(v); |
| 1265 | v = temp; |
| 1266 | } |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1267 | } |
| 1268 | } |
| 1269 | return v; |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1270 | |
| 1271 | onError: |
| 1272 | Py_XDECREF(v); |
| 1273 | return NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1276 | static void |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1277 | 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] | 1278 | { |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1279 | int anchor = 0; |
| 1280 | int save_begin = c->c_begin; |
| 1281 | |
| 1282 | /* list_iter: for v in expr [list_iter] */ |
| 1283 | com_node(c, CHILD(n, 3)); /* expr */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1284 | com_addbyte(c, GET_ITER); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1285 | c->c_begin = c->c_nexti; |
| 1286 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1287 | com_addfwref(c, FOR_ITER, &anchor); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1288 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1289 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1290 | c->c_loops++; |
| 1291 | com_list_iter(c, n, e, t); |
| 1292 | c->c_loops--; |
| 1293 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 1294 | c->c_begin = save_begin; |
| 1295 | com_backpatch(c, anchor); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1296 | com_pop(c, 1); /* FOR_ITER has popped this */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | static void |
| 1300 | com_list_if(struct compiling *c, node *n, node *e, char *t) |
| 1301 | { |
| 1302 | int anchor = 0; |
| 1303 | int a = 0; |
| 1304 | /* list_iter: 'if' test [list_iter] */ |
| 1305 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 1306 | com_node(c, CHILD(n, 1)); |
| 1307 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 1308 | com_addbyte(c, POP_TOP); |
| 1309 | com_pop(c, 1); |
| 1310 | com_list_iter(c, n, e, t); |
| 1311 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 1312 | com_backpatch(c, a); |
| 1313 | /* We jump here with an extra entry which we now pop */ |
| 1314 | com_addbyte(c, POP_TOP); |
| 1315 | com_backpatch(c, anchor); |
| 1316 | } |
| 1317 | |
| 1318 | static void |
| 1319 | com_list_iter(struct compiling *c, |
| 1320 | node *p, /* parent of list_iter node */ |
| 1321 | node *e, /* element expression node */ |
| 1322 | char *t /* name of result list temp local */) |
| 1323 | { |
| 1324 | /* list_iter is the last child in a listmaker, list_for, or list_if */ |
| 1325 | node *n = CHILD(p, NCH(p)-1); |
| 1326 | if (TYPE(n) == list_iter) { |
| 1327 | n = CHILD(n, 0); |
| 1328 | switch (TYPE(n)) { |
| 1329 | case list_for: |
| 1330 | com_list_for(c, n, e, t); |
| 1331 | break; |
| 1332 | case list_if: |
| 1333 | com_list_if(c, n, e, t); |
| 1334 | break; |
| 1335 | default: |
| 1336 | com_error(c, PyExc_SystemError, |
| 1337 | "invalid list_iter node type"); |
| 1338 | } |
| 1339 | } |
| 1340 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1341 | com_addop_varname(c, VAR_LOAD, t); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1342 | com_push(c, 1); |
| 1343 | com_node(c, e); |
| 1344 | com_addoparg(c, CALL_FUNCTION, 1); |
| 1345 | com_addbyte(c, POP_TOP); |
| 1346 | com_pop(c, 2); |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | static void |
| 1351 | com_list_comprehension(struct compiling *c, node *n) |
| 1352 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1353 | /* listmaker: test list_for */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1354 | char tmpname[12]; |
Tim Peters | 3081421 | 2001-02-17 05:30:26 +0000 | [diff] [blame] | 1355 | sprintf(tmpname, "_[%d]", ++c->c_tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1356 | com_addoparg(c, BUILD_LIST, 0); |
| 1357 | com_addbyte(c, DUP_TOP); /* leave the result on the stack */ |
| 1358 | com_push(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1359 | com_addop_name(c, LOAD_ATTR, "append"); |
| 1360 | com_addop_varname(c, VAR_STORE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1361 | com_pop(c, 1); |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1362 | com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1363 | com_addop_varname(c, VAR_DELETE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1364 | --c->c_tmpname; |
| 1365 | } |
| 1366 | |
| 1367 | static void |
| 1368 | com_listmaker(struct compiling *c, node *n) |
| 1369 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1370 | /* listmaker: test ( list_for | (',' test)* [','] ) */ |
| 1371 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1372 | com_list_comprehension(c, n); |
| 1373 | else { |
| 1374 | int len = 0; |
| 1375 | int i; |
| 1376 | for (i = 0; i < NCH(n); i += 2, len++) |
| 1377 | com_node(c, CHILD(n, i)); |
| 1378 | com_addoparg(c, BUILD_LIST, len); |
| 1379 | com_pop(c, len-1); |
| 1380 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1384 | com_dictmaker(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1385 | { |
| 1386 | int i; |
| 1387 | /* dictmaker: test ':' test (',' test ':' value)* [','] */ |
| 1388 | for (i = 0; i+2 < NCH(n); i += 4) { |
| 1389 | /* We must arrange things just right for STORE_SUBSCR. |
| 1390 | It wants the stack to look like (value) (dict) (key) */ |
| 1391 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1392 | com_push(c, 1); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1393 | com_node(c, CHILD(n, i+2)); /* value */ |
| 1394 | com_addbyte(c, ROT_TWO); |
| 1395 | com_node(c, CHILD(n, i)); /* key */ |
| 1396 | com_addbyte(c, STORE_SUBSCR); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1397 | com_pop(c, 3); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1402 | com_atom(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1403 | { |
| 1404 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1405 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1406 | int i; |
| 1407 | REQ(n, atom); |
| 1408 | ch = CHILD(n, 0); |
| 1409 | switch (TYPE(ch)) { |
| 1410 | case LPAR: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1411 | if (TYPE(CHILD(n, 1)) == RPAR) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1412 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1413 | com_push(c, 1); |
| 1414 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1415 | else |
| 1416 | com_node(c, CHILD(n, 1)); |
| 1417 | break; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1418 | case LSQB: /* '[' [listmaker] ']' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1419 | if (TYPE(CHILD(n, 1)) == RSQB) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1420 | com_addoparg(c, BUILD_LIST, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1421 | com_push(c, 1); |
| 1422 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1423 | else |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1424 | com_listmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1425 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1426 | case LBRACE: /* '{' [dictmaker] '}' */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1427 | com_addoparg(c, BUILD_MAP, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1428 | com_push(c, 1); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1429 | if (TYPE(CHILD(n, 1)) == dictmaker) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1430 | com_dictmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1431 | break; |
| 1432 | case BACKQUOTE: |
| 1433 | com_node(c, CHILD(n, 1)); |
| 1434 | com_addbyte(c, UNARY_CONVERT); |
| 1435 | break; |
| 1436 | case NUMBER: |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1437 | if ((v = parsenumber(c, STR(ch))) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1438 | i = 255; |
| 1439 | } |
| 1440 | else { |
| 1441 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1442 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1443 | } |
| 1444 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1445 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1446 | break; |
| 1447 | case STRING: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1448 | v = parsestrplus(n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1449 | if (v == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1450 | c->c_errors++; |
| 1451 | i = 255; |
| 1452 | } |
| 1453 | else { |
| 1454 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1455 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1456 | } |
| 1457 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1458 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1459 | break; |
| 1460 | case NAME: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1461 | com_addop_varname(c, VAR_LOAD, STR(ch)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1462 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1463 | break; |
| 1464 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1465 | com_error(c, PyExc_SystemError, |
| 1466 | "com_atom: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1471 | com_slice(struct compiling *c, node *n, int op) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1472 | { |
| 1473 | if (NCH(n) == 1) { |
| 1474 | com_addbyte(c, op); |
| 1475 | } |
| 1476 | else if (NCH(n) == 2) { |
| 1477 | if (TYPE(CHILD(n, 0)) != COLON) { |
| 1478 | com_node(c, CHILD(n, 0)); |
| 1479 | com_addbyte(c, op+1); |
| 1480 | } |
| 1481 | else { |
| 1482 | com_node(c, CHILD(n, 1)); |
| 1483 | com_addbyte(c, op+2); |
| 1484 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1485 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1486 | } |
| 1487 | else { |
| 1488 | com_node(c, CHILD(n, 0)); |
| 1489 | com_node(c, CHILD(n, 2)); |
| 1490 | com_addbyte(c, op+3); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1491 | com_pop(c, 2); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1492 | } |
| 1493 | } |
| 1494 | |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1495 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1496 | com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn) |
| 1497 | { |
| 1498 | if (NCH(n) == 1) { |
| 1499 | com_addbyte(c, DUP_TOP); |
| 1500 | com_push(c, 1); |
| 1501 | com_addbyte(c, SLICE); |
| 1502 | com_node(c, augn); |
| 1503 | com_addbyte(c, opcode); |
| 1504 | com_pop(c, 1); |
| 1505 | com_addbyte(c, ROT_TWO); |
| 1506 | com_addbyte(c, STORE_SLICE); |
| 1507 | com_pop(c, 2); |
| 1508 | } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) { |
| 1509 | com_node(c, CHILD(n, 0)); |
| 1510 | com_addoparg(c, DUP_TOPX, 2); |
| 1511 | com_push(c, 2); |
| 1512 | com_addbyte(c, SLICE+1); |
| 1513 | com_pop(c, 1); |
| 1514 | com_node(c, augn); |
| 1515 | com_addbyte(c, opcode); |
| 1516 | com_pop(c, 1); |
| 1517 | com_addbyte(c, ROT_THREE); |
| 1518 | com_addbyte(c, STORE_SLICE+1); |
| 1519 | com_pop(c, 3); |
| 1520 | } else if (NCH(n) == 2) { |
| 1521 | com_node(c, CHILD(n, 1)); |
| 1522 | com_addoparg(c, DUP_TOPX, 2); |
| 1523 | com_push(c, 2); |
| 1524 | com_addbyte(c, SLICE+2); |
| 1525 | com_pop(c, 1); |
| 1526 | com_node(c, augn); |
| 1527 | com_addbyte(c, opcode); |
| 1528 | com_pop(c, 1); |
| 1529 | com_addbyte(c, ROT_THREE); |
| 1530 | com_addbyte(c, STORE_SLICE+2); |
| 1531 | com_pop(c, 3); |
| 1532 | } else { |
| 1533 | com_node(c, CHILD(n, 0)); |
| 1534 | com_node(c, CHILD(n, 2)); |
| 1535 | com_addoparg(c, DUP_TOPX, 3); |
| 1536 | com_push(c, 3); |
| 1537 | com_addbyte(c, SLICE+3); |
| 1538 | com_pop(c, 2); |
| 1539 | com_node(c, augn); |
| 1540 | com_addbyte(c, opcode); |
| 1541 | com_pop(c, 1); |
| 1542 | com_addbyte(c, ROT_FOUR); |
| 1543 | com_addbyte(c, STORE_SLICE+3); |
| 1544 | com_pop(c, 4); |
| 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1549 | com_argument(struct compiling *c, node *n, PyObject **pkeywords) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1550 | { |
| 1551 | node *m; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1552 | REQ(n, argument); /* [test '='] test; really [keyword '='] test */ |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1553 | if (NCH(n) == 1) { |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1554 | if (*pkeywords != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1555 | com_error(c, PyExc_SyntaxError, |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 1556 | "non-keyword arg after keyword arg"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1557 | } |
| 1558 | else { |
| 1559 | com_node(c, CHILD(n, 0)); |
| 1560 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1561 | return; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1562 | } |
| 1563 | m = n; |
| 1564 | do { |
| 1565 | m = CHILD(m, 0); |
| 1566 | } while (NCH(m) == 1); |
| 1567 | if (TYPE(m) != NAME) { |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 1568 | /* f(lambda x: x[0] = 3) ends up getting parsed with |
| 1569 | * LHS test = lambda x: x[0], and RHS test = 3. |
| 1570 | * SF bug 132313 points out that complaining about a keyword |
| 1571 | * then is very confusing. |
| 1572 | */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1573 | com_error(c, PyExc_SyntaxError, |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 1574 | TYPE(m) == lambdef ? |
| 1575 | "lambda cannot contain assignment" : |
| 1576 | "keyword can't be an expression"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1577 | } |
| 1578 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1579 | PyObject *v = PyString_InternFromString(STR(m)); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1580 | if (v != NULL && *pkeywords == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1581 | *pkeywords = PyDict_New(); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 1582 | if (v == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1583 | c->c_errors++; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 1584 | else if (*pkeywords == NULL) { |
| 1585 | c->c_errors++; |
| 1586 | Py_DECREF(v); |
| 1587 | } else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1588 | if (PyDict_GetItem(*pkeywords, v) != NULL) |
| 1589 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1590 | "duplicate keyword argument"); |
| 1591 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1592 | if (PyDict_SetItem(*pkeywords, v, v) != 0) |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1593 | c->c_errors++; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1594 | com_addoparg(c, LOAD_CONST, com_addconst(c, v)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1595 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1596 | Py_DECREF(v); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1597 | } |
| 1598 | } |
| 1599 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1602 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1603 | com_call_function(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1604 | { |
| 1605 | if (TYPE(n) == RPAR) { |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1606 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1607 | } |
| 1608 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1609 | PyObject *keywords = NULL; |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1610 | int i, na, nk; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1611 | int lineno = n->n_lineno; |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1612 | int star_flag = 0; |
| 1613 | int starstar_flag = 0; |
| 1614 | int opcode; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1615 | REQ(n, arglist); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1616 | na = 0; |
| 1617 | nk = 0; |
| 1618 | for (i = 0; i < NCH(n); i += 2) { |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1619 | node *ch = CHILD(n, i); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1620 | if (TYPE(ch) == STAR || |
| 1621 | TYPE(ch) == DOUBLESTAR) |
| 1622 | break; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1623 | if (ch->n_lineno != lineno) { |
| 1624 | lineno = ch->n_lineno; |
| 1625 | com_addoparg(c, SET_LINENO, lineno); |
| 1626 | } |
| 1627 | com_argument(c, ch, &keywords); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1628 | if (keywords == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1629 | na++; |
| 1630 | else |
| 1631 | nk++; |
| 1632 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1633 | Py_XDECREF(keywords); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1634 | while (i < NCH(n)) { |
| 1635 | node *tok = CHILD(n, i); |
| 1636 | node *ch = CHILD(n, i+1); |
| 1637 | i += 3; |
| 1638 | switch (TYPE(tok)) { |
| 1639 | case STAR: star_flag = 1; break; |
| 1640 | case DOUBLESTAR: starstar_flag = 1; break; |
| 1641 | } |
| 1642 | com_node(c, ch); |
| 1643 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1644 | if (na > 255 || nk > 255) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1645 | com_error(c, PyExc_SyntaxError, |
| 1646 | "more than 255 arguments"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1647 | } |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1648 | if (star_flag || starstar_flag) |
Jeremy Hylton | e4fb958 | 2000-03-29 00:10:44 +0000 | [diff] [blame] | 1649 | opcode = CALL_FUNCTION_VAR - 1 + |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1650 | star_flag + (starstar_flag << 1); |
| 1651 | else |
| 1652 | opcode = CALL_FUNCTION; |
| 1653 | com_addoparg(c, opcode, na | (nk << 8)); |
| 1654 | com_pop(c, na + 2*nk + star_flag + starstar_flag); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1659 | com_select_member(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1660 | { |
| 1661 | com_addopname(c, LOAD_ATTR, n); |
| 1662 | } |
| 1663 | |
| 1664 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1665 | com_sliceobj(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1666 | { |
| 1667 | int i=0; |
| 1668 | int ns=2; /* number of slice arguments */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1669 | node *ch; |
| 1670 | |
| 1671 | /* first argument */ |
| 1672 | if (TYPE(CHILD(n,i)) == COLON) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1673 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1674 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1675 | i++; |
| 1676 | } |
| 1677 | else { |
| 1678 | com_node(c, CHILD(n,i)); |
| 1679 | i++; |
| 1680 | REQ(CHILD(n,i),COLON); |
| 1681 | i++; |
| 1682 | } |
| 1683 | /* second argument */ |
| 1684 | if (i < NCH(n) && TYPE(CHILD(n,i)) == test) { |
| 1685 | com_node(c, CHILD(n,i)); |
| 1686 | i++; |
| 1687 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1688 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1689 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1690 | com_push(c, 1); |
| 1691 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1692 | /* remaining arguments */ |
| 1693 | for (; i < NCH(n); i++) { |
| 1694 | ns++; |
| 1695 | ch=CHILD(n,i); |
| 1696 | REQ(ch, sliceop); |
| 1697 | if (NCH(ch) == 1) { |
| 1698 | /* right argument of ':' missing */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1699 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1700 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1701 | } |
| 1702 | else |
| 1703 | com_node(c, CHILD(ch,1)); |
| 1704 | } |
| 1705 | com_addoparg(c, BUILD_SLICE, ns); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1706 | com_pop(c, 1 + (ns == 3)); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1710 | com_subscript(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1711 | { |
| 1712 | node *ch; |
| 1713 | REQ(n, subscript); |
| 1714 | ch = CHILD(n,0); |
| 1715 | /* check for rubber index */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1716 | if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) { |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 1717 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1718 | com_push(c, 1); |
| 1719 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1720 | else { |
| 1721 | /* check for slice */ |
| 1722 | if ((TYPE(ch) == COLON || NCH(n) > 1)) |
| 1723 | com_sliceobj(c, n); |
| 1724 | else { |
| 1725 | REQ(ch, test); |
| 1726 | com_node(c, ch); |
| 1727 | } |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1732 | com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1733 | { |
| 1734 | int i, op; |
| 1735 | REQ(n, subscriptlist); |
| 1736 | /* Check to make backward compatible slice behavior for '[i:j]' */ |
| 1737 | if (NCH(n) == 1) { |
| 1738 | node *sub = CHILD(n, 0); /* subscript */ |
Thomas Wouters | e8643c4 | 2000-08-05 21:37:50 +0000 | [diff] [blame] | 1739 | /* 'Basic' slice, should have exactly one colon. */ |
| 1740 | if ((TYPE(CHILD(sub, 0)) == COLON |
| 1741 | || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON)) |
| 1742 | && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop)) |
| 1743 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1744 | switch (assigning) { |
| 1745 | case OP_DELETE: |
| 1746 | op = DELETE_SLICE; |
| 1747 | break; |
| 1748 | case OP_ASSIGN: |
| 1749 | op = STORE_SLICE; |
| 1750 | break; |
| 1751 | case OP_APPLY: |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1752 | op = SLICE; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1753 | break; |
| 1754 | default: |
| 1755 | com_augassign_slice(c, sub, assigning, augn); |
| 1756 | return; |
| 1757 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1758 | com_slice(c, sub, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1759 | if (op == STORE_SLICE) |
| 1760 | com_pop(c, 2); |
| 1761 | else if (op == DELETE_SLICE) |
| 1762 | com_pop(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1763 | return; |
| 1764 | } |
| 1765 | } |
| 1766 | /* Else normal subscriptlist. Compile each subscript. */ |
| 1767 | for (i = 0; i < NCH(n); i += 2) |
| 1768 | com_subscript(c, CHILD(n, i)); |
| 1769 | /* Put multiple subscripts into a tuple */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1770 | if (NCH(n) > 1) { |
| 1771 | i = (NCH(n)+1) / 2; |
| 1772 | com_addoparg(c, BUILD_TUPLE, i); |
| 1773 | com_pop(c, i-1); |
| 1774 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1775 | switch (assigning) { |
| 1776 | case OP_DELETE: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1777 | op = DELETE_SUBSCR; |
| 1778 | i = 2; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1779 | break; |
| 1780 | default: |
| 1781 | case OP_ASSIGN: |
| 1782 | op = STORE_SUBSCR; |
| 1783 | i = 3; |
| 1784 | break; |
| 1785 | case OP_APPLY: |
| 1786 | op = BINARY_SUBSCR; |
| 1787 | i = 1; |
| 1788 | break; |
| 1789 | } |
| 1790 | if (assigning > OP_APPLY) { |
| 1791 | com_addoparg(c, DUP_TOPX, 2); |
| 1792 | com_push(c, 2); |
| 1793 | com_addbyte(c, BINARY_SUBSCR); |
| 1794 | com_pop(c, 1); |
| 1795 | com_node(c, augn); |
| 1796 | com_addbyte(c, assigning); |
| 1797 | com_pop(c, 1); |
| 1798 | com_addbyte(c, ROT_THREE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1799 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1800 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1801 | com_pop(c, i); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1805 | com_apply_trailer(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1806 | { |
| 1807 | REQ(n, trailer); |
| 1808 | switch (TYPE(CHILD(n, 0))) { |
| 1809 | case LPAR: |
| 1810 | com_call_function(c, CHILD(n, 1)); |
| 1811 | break; |
| 1812 | case DOT: |
| 1813 | com_select_member(c, CHILD(n, 1)); |
| 1814 | break; |
| 1815 | case LSQB: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1816 | com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1817 | break; |
| 1818 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1819 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1820 | "com_apply_trailer: unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1821 | } |
| 1822 | } |
| 1823 | |
| 1824 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1825 | com_power(struct compiling *c, node *n) |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1826 | { |
| 1827 | int i; |
| 1828 | REQ(n, power); |
| 1829 | com_atom(c, CHILD(n, 0)); |
| 1830 | for (i = 1; i < NCH(n); i++) { |
| 1831 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
| 1832 | com_factor(c, CHILD(n, i+1)); |
| 1833 | com_addbyte(c, BINARY_POWER); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1834 | com_pop(c, 1); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1835 | break; |
| 1836 | } |
| 1837 | else |
| 1838 | com_apply_trailer(c, CHILD(n, i)); |
| 1839 | } |
| 1840 | } |
| 1841 | |
| 1842 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1843 | com_factor(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1844 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1845 | REQ(n, factor); |
| 1846 | if (TYPE(CHILD(n, 0)) == PLUS) { |
| 1847 | com_factor(c, CHILD(n, 1)); |
| 1848 | com_addbyte(c, UNARY_POSITIVE); |
| 1849 | } |
| 1850 | else if (TYPE(CHILD(n, 0)) == MINUS) { |
| 1851 | com_factor(c, CHILD(n, 1)); |
| 1852 | com_addbyte(c, UNARY_NEGATIVE); |
| 1853 | } |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1854 | else if (TYPE(CHILD(n, 0)) == TILDE) { |
| 1855 | com_factor(c, CHILD(n, 1)); |
| 1856 | com_addbyte(c, UNARY_INVERT); |
| 1857 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1858 | else { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1859 | com_power(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1860 | } |
| 1861 | } |
| 1862 | |
| 1863 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1864 | com_term(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1865 | { |
| 1866 | int i; |
| 1867 | int op; |
| 1868 | REQ(n, term); |
| 1869 | com_factor(c, CHILD(n, 0)); |
| 1870 | for (i = 2; i < NCH(n); i += 2) { |
| 1871 | com_factor(c, CHILD(n, i)); |
| 1872 | switch (TYPE(CHILD(n, i-1))) { |
| 1873 | case STAR: |
| 1874 | op = BINARY_MULTIPLY; |
| 1875 | break; |
| 1876 | case SLASH: |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1877 | if (c->c_flags & CO_FUTURE_DIVISION) |
| 1878 | op = BINARY_TRUE_DIVIDE; |
| 1879 | else |
| 1880 | op = BINARY_DIVIDE; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1881 | break; |
| 1882 | case PERCENT: |
| 1883 | op = BINARY_MODULO; |
| 1884 | break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1885 | case DOUBLESLASH: |
| 1886 | op = BINARY_FLOOR_DIVIDE; |
| 1887 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1888 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1889 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1890 | "com_term: operator not *, /, // or %"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1891 | op = 255; |
| 1892 | } |
| 1893 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1894 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1899 | com_arith_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1900 | { |
| 1901 | int i; |
| 1902 | int op; |
| 1903 | REQ(n, arith_expr); |
| 1904 | com_term(c, CHILD(n, 0)); |
| 1905 | for (i = 2; i < NCH(n); i += 2) { |
| 1906 | com_term(c, CHILD(n, i)); |
| 1907 | switch (TYPE(CHILD(n, i-1))) { |
| 1908 | case PLUS: |
| 1909 | op = BINARY_ADD; |
| 1910 | break; |
| 1911 | case MINUS: |
| 1912 | op = BINARY_SUBTRACT; |
| 1913 | break; |
| 1914 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1915 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1916 | "com_arith_expr: operator not + or -"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1917 | op = 255; |
| 1918 | } |
| 1919 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1920 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1921 | } |
| 1922 | } |
| 1923 | |
| 1924 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1925 | com_shift_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1926 | { |
| 1927 | int i; |
| 1928 | int op; |
| 1929 | REQ(n, shift_expr); |
| 1930 | com_arith_expr(c, CHILD(n, 0)); |
| 1931 | for (i = 2; i < NCH(n); i += 2) { |
| 1932 | com_arith_expr(c, CHILD(n, i)); |
| 1933 | switch (TYPE(CHILD(n, i-1))) { |
| 1934 | case LEFTSHIFT: |
| 1935 | op = BINARY_LSHIFT; |
| 1936 | break; |
| 1937 | case RIGHTSHIFT: |
| 1938 | op = BINARY_RSHIFT; |
| 1939 | break; |
| 1940 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1941 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1942 | "com_shift_expr: operator not << or >>"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1943 | op = 255; |
| 1944 | } |
| 1945 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1946 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1947 | } |
| 1948 | } |
| 1949 | |
| 1950 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1951 | com_and_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1952 | { |
| 1953 | int i; |
| 1954 | int op; |
| 1955 | REQ(n, and_expr); |
| 1956 | com_shift_expr(c, CHILD(n, 0)); |
| 1957 | for (i = 2; i < NCH(n); i += 2) { |
| 1958 | com_shift_expr(c, CHILD(n, i)); |
| 1959 | if (TYPE(CHILD(n, i-1)) == AMPER) { |
| 1960 | op = BINARY_AND; |
| 1961 | } |
| 1962 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1963 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1964 | "com_and_expr: operator not &"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1965 | op = 255; |
| 1966 | } |
| 1967 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1968 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1969 | } |
| 1970 | } |
| 1971 | |
| 1972 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1973 | com_xor_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1974 | { |
| 1975 | int i; |
| 1976 | int op; |
| 1977 | REQ(n, xor_expr); |
| 1978 | com_and_expr(c, CHILD(n, 0)); |
| 1979 | for (i = 2; i < NCH(n); i += 2) { |
| 1980 | com_and_expr(c, CHILD(n, i)); |
| 1981 | if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) { |
| 1982 | op = BINARY_XOR; |
| 1983 | } |
| 1984 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1985 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1986 | "com_xor_expr: operator not ^"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1987 | op = 255; |
| 1988 | } |
| 1989 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1990 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1991 | } |
| 1992 | } |
| 1993 | |
| 1994 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1995 | com_expr(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1996 | { |
| 1997 | int i; |
| 1998 | int op; |
| 1999 | REQ(n, expr); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2000 | com_xor_expr(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2001 | for (i = 2; i < NCH(n); i += 2) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2002 | com_xor_expr(c, CHILD(n, i)); |
| 2003 | if (TYPE(CHILD(n, i-1)) == VBAR) { |
| 2004 | op = BINARY_OR; |
| 2005 | } |
| 2006 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2007 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2008 | "com_expr: expr operator not |"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2009 | op = 255; |
| 2010 | } |
| 2011 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2012 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2013 | } |
| 2014 | } |
| 2015 | |
| 2016 | static enum cmp_op |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2017 | cmp_type(node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2018 | { |
| 2019 | REQ(n, comp_op); |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2020 | /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '==' |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2021 | | 'in' | 'not' 'in' | 'is' | 'is' not' */ |
| 2022 | if (NCH(n) == 1) { |
| 2023 | n = CHILD(n, 0); |
| 2024 | switch (TYPE(n)) { |
| 2025 | case LESS: return LT; |
| 2026 | case GREATER: return GT; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2027 | case EQEQUAL: /* == */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2028 | case EQUAL: return EQ; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2029 | case LESSEQUAL: return LE; |
| 2030 | case GREATEREQUAL: return GE; |
| 2031 | case NOTEQUAL: return NE; /* <> or != */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2032 | case NAME: if (strcmp(STR(n), "in") == 0) return IN; |
| 2033 | if (strcmp(STR(n), "is") == 0) return IS; |
| 2034 | } |
| 2035 | } |
| 2036 | else if (NCH(n) == 2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2037 | switch (TYPE(CHILD(n, 0))) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2038 | case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 2039 | return NOT_IN; |
| 2040 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 2041 | return IS_NOT; |
| 2042 | } |
| 2043 | } |
| 2044 | return BAD; |
| 2045 | } |
| 2046 | |
| 2047 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2048 | com_comparison(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2049 | { |
| 2050 | int i; |
| 2051 | enum cmp_op op; |
| 2052 | int anchor; |
| 2053 | REQ(n, comparison); /* comparison: expr (comp_op expr)* */ |
| 2054 | com_expr(c, CHILD(n, 0)); |
| 2055 | if (NCH(n) == 1) |
| 2056 | return; |
| 2057 | |
| 2058 | /**************************************************************** |
| 2059 | The following code is generated for all but the last |
| 2060 | comparison in a chain: |
| 2061 | |
| 2062 | label: on stack: opcode: jump to: |
| 2063 | |
| 2064 | a <code to load b> |
| 2065 | a, b DUP_TOP |
| 2066 | a, b, b ROT_THREE |
| 2067 | b, a, b COMPARE_OP |
| 2068 | b, 0-or-1 JUMP_IF_FALSE L1 |
| 2069 | b, 1 POP_TOP |
| 2070 | b |
| 2071 | |
| 2072 | We are now ready to repeat this sequence for the next |
| 2073 | comparison in the chain. |
| 2074 | |
| 2075 | For the last we generate: |
| 2076 | |
| 2077 | b <code to load c> |
| 2078 | b, c COMPARE_OP |
| 2079 | 0-or-1 |
| 2080 | |
| 2081 | If there were any jumps to L1 (i.e., there was more than one |
| 2082 | comparison), we generate: |
| 2083 | |
| 2084 | 0-or-1 JUMP_FORWARD L2 |
| 2085 | L1: b, 0 ROT_TWO |
| 2086 | 0, b POP_TOP |
| 2087 | 0 |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2088 | L2: 0-or-1 |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2089 | ****************************************************************/ |
| 2090 | |
| 2091 | anchor = 0; |
| 2092 | |
| 2093 | for (i = 2; i < NCH(n); i += 2) { |
| 2094 | com_expr(c, CHILD(n, i)); |
| 2095 | if (i+2 < NCH(n)) { |
| 2096 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2097 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2098 | com_addbyte(c, ROT_THREE); |
| 2099 | } |
| 2100 | op = cmp_type(CHILD(n, i-1)); |
| 2101 | if (op == BAD) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2102 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2103 | "com_comparison: unknown comparison op"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2104 | } |
| 2105 | com_addoparg(c, COMPARE_OP, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2106 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2107 | if (i+2 < NCH(n)) { |
| 2108 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2109 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2110 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2111 | } |
| 2112 | } |
| 2113 | |
| 2114 | if (anchor) { |
| 2115 | int anchor2 = 0; |
| 2116 | com_addfwref(c, JUMP_FORWARD, &anchor2); |
| 2117 | com_backpatch(c, anchor); |
| 2118 | com_addbyte(c, ROT_TWO); |
| 2119 | com_addbyte(c, POP_TOP); |
| 2120 | com_backpatch(c, anchor2); |
| 2121 | } |
| 2122 | } |
| 2123 | |
| 2124 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2125 | com_not_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2126 | { |
| 2127 | REQ(n, not_test); /* 'not' not_test | comparison */ |
| 2128 | if (NCH(n) == 1) { |
| 2129 | com_comparison(c, CHILD(n, 0)); |
| 2130 | } |
| 2131 | else { |
| 2132 | com_not_test(c, CHILD(n, 1)); |
| 2133 | com_addbyte(c, UNARY_NOT); |
| 2134 | } |
| 2135 | } |
| 2136 | |
| 2137 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2138 | com_and_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2139 | { |
| 2140 | int i; |
| 2141 | int anchor; |
| 2142 | REQ(n, and_test); /* not_test ('and' not_test)* */ |
| 2143 | anchor = 0; |
| 2144 | i = 0; |
| 2145 | for (;;) { |
| 2146 | com_not_test(c, CHILD(n, i)); |
| 2147 | if ((i += 2) >= NCH(n)) |
| 2148 | break; |
| 2149 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2150 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2151 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2152 | } |
| 2153 | if (anchor) |
| 2154 | com_backpatch(c, anchor); |
| 2155 | } |
| 2156 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2157 | static int |
| 2158 | com_make_closure(struct compiling *c, PyCodeObject *co) |
| 2159 | { |
| 2160 | int i, free = PyTuple_GET_SIZE(co->co_freevars); |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 2161 | /* If the code is compiled with st->st_nested_scopes == 0, |
| 2162 | then no variable will ever be added to co_freevars. |
| 2163 | */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2164 | if (free == 0) |
| 2165 | return 0; |
| 2166 | for (i = 0; i < free; ++i) { |
| 2167 | /* Bypass com_addop_varname because it will generate |
| 2168 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 2169 | */ |
| 2170 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 2171 | int arg, reftype; |
| 2172 | |
| 2173 | /* Special case: If a class contains a method with a |
| 2174 | free variable that has the same name as a method, |
| 2175 | the name will be considered free *and* local in the |
| 2176 | class. It should be handled by the closure, as |
| 2177 | well as by the normal name loookup logic. |
| 2178 | */ |
| 2179 | reftype = get_ref_type(c, PyString_AS_STRING(name)); |
| 2180 | if (reftype == CELL) |
| 2181 | arg = com_lookup_arg(c->c_cellvars, name); |
| 2182 | else /* (reftype == FREE) */ |
| 2183 | arg = com_lookup_arg(c->c_freevars, name); |
| 2184 | if (arg == -1) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 2185 | fprintf(stderr, "lookup %s in %s %d %d\n" |
| 2186 | "freevars of %s: %s\n", |
| 2187 | PyObject_REPR(name), |
| 2188 | c->c_name, |
| 2189 | reftype, arg, |
| 2190 | PyString_AS_STRING(co->co_name), |
| 2191 | PyObject_REPR(co->co_freevars)); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2192 | Py_FatalError("com_make_closure()"); |
| 2193 | } |
| 2194 | com_addoparg(c, LOAD_CLOSURE, arg); |
| 2195 | |
| 2196 | } |
| 2197 | com_push(c, free); |
| 2198 | return 1; |
| 2199 | } |
| 2200 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2201 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2202 | com_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2203 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2204 | REQ(n, test); /* and_test ('or' and_test)* | lambdef */ |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2205 | if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2206 | PyObject *co; |
| 2207 | int i, closure; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2208 | int ndefs = com_argdefs(c, CHILD(n, 0)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 2209 | symtable_enter_scope(c->c_symtable, "lambda", lambdef, |
| 2210 | n->n_lineno); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2211 | co = (PyObject *) icompile(CHILD(n, 0), c); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2212 | if (co == NULL) { |
| 2213 | c->c_errors++; |
| 2214 | return; |
| 2215 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2216 | symtable_exit_scope(c->c_symtable); |
Tim Peters | 1e54211 | 2001-02-23 22:23:53 +0000 | [diff] [blame] | 2217 | i = com_addconst(c, co); |
| 2218 | closure = com_make_closure(c, (PyCodeObject *)co); |
| 2219 | Py_DECREF(co); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2220 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2221 | com_push(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2222 | if (closure) |
| 2223 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
| 2224 | else |
| 2225 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2226 | com_pop(c, ndefs); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2227 | } |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2228 | else { |
| 2229 | int anchor = 0; |
| 2230 | int i = 0; |
| 2231 | for (;;) { |
| 2232 | com_and_test(c, CHILD(n, i)); |
| 2233 | if ((i += 2) >= NCH(n)) |
| 2234 | break; |
| 2235 | com_addfwref(c, JUMP_IF_TRUE, &anchor); |
| 2236 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2237 | com_pop(c, 1); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2238 | } |
| 2239 | if (anchor) |
| 2240 | com_backpatch(c, anchor); |
| 2241 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
| 2244 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2245 | com_list(struct compiling *c, node *n, int toplevel) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2246 | { |
| 2247 | /* exprlist: expr (',' expr)* [',']; likewise for testlist */ |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 2248 | if (NCH(n) == 1 && !toplevel) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2249 | com_node(c, CHILD(n, 0)); |
| 2250 | } |
| 2251 | else { |
| 2252 | int i; |
| 2253 | int len; |
| 2254 | len = (NCH(n) + 1) / 2; |
| 2255 | for (i = 0; i < NCH(n); i += 2) |
| 2256 | com_node(c, CHILD(n, i)); |
| 2257 | com_addoparg(c, BUILD_TUPLE, len); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2258 | com_pop(c, len-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2259 | } |
| 2260 | } |
| 2261 | |
| 2262 | |
| 2263 | /* Begin of assignment compilation */ |
| 2264 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2265 | |
| 2266 | static void |
| 2267 | com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn) |
| 2268 | { |
| 2269 | com_addbyte(c, DUP_TOP); |
| 2270 | com_push(c, 1); |
| 2271 | com_addopname(c, LOAD_ATTR, n); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2272 | com_node(c, augn); |
| 2273 | com_addbyte(c, opcode); |
| 2274 | com_pop(c, 1); |
| 2275 | com_addbyte(c, ROT_TWO); |
| 2276 | com_addopname(c, STORE_ATTR, n); |
| 2277 | com_pop(c, 2); |
| 2278 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2279 | |
| 2280 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2281 | com_assign_attr(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2282 | { |
| 2283 | com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2284 | com_pop(c, assigning ? 2 : 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
| 2287 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2288 | 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] | 2289 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2290 | REQ(n, trailer); |
| 2291 | switch (TYPE(CHILD(n, 0))) { |
| 2292 | case LPAR: /* '(' [exprlist] ')' */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2293 | com_error(c, PyExc_SyntaxError, |
| 2294 | "can't assign to function call"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2295 | break; |
| 2296 | case DOT: /* '.' NAME */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2297 | if (assigning > OP_APPLY) |
| 2298 | com_augassign_attr(c, CHILD(n, 1), assigning, augn); |
| 2299 | else |
| 2300 | com_assign_attr(c, CHILD(n, 1), assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2301 | break; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2302 | case LSQB: /* '[' subscriptlist ']' */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2303 | com_subscriptlist(c, CHILD(n, 1), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2304 | break; |
| 2305 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2306 | com_error(c, PyExc_SystemError, "unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2307 | } |
| 2308 | } |
| 2309 | |
| 2310 | static void |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2311 | com_assign_sequence(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2312 | { |
| 2313 | int i; |
Thomas Wouters | b59290f | 2000-08-25 05:41:11 +0000 | [diff] [blame] | 2314 | if (TYPE(n) != testlist && TYPE(n) != listmaker) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2315 | REQ(n, exprlist); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2316 | if (assigning) { |
| 2317 | i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2318 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2319 | com_push(c, i-1); |
| 2320 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2321 | for (i = 0; i < NCH(n); i += 2) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2322 | com_assign(c, CHILD(n, i), assigning, NULL); |
| 2323 | } |
| 2324 | |
| 2325 | static void |
| 2326 | com_augassign_name(struct compiling *c, node *n, int opcode, node *augn) |
| 2327 | { |
| 2328 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2329 | com_addop_varname(c, VAR_LOAD, STR(n)); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2330 | com_push(c, 1); |
| 2331 | com_node(c, augn); |
| 2332 | com_addbyte(c, opcode); |
| 2333 | com_pop(c, 1); |
| 2334 | com_assign_name(c, n, OP_ASSIGN); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2338 | com_assign_name(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2339 | { |
| 2340 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2341 | com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2342 | if (assigning) |
| 2343 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2344 | } |
| 2345 | |
| 2346 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2347 | com_assign(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2348 | { |
| 2349 | /* Loop to avoid trivial recursion */ |
| 2350 | for (;;) { |
| 2351 | switch (TYPE(n)) { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2352 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2353 | case exprlist: |
| 2354 | case testlist: |
| 2355 | if (NCH(n) > 1) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2356 | if (assigning > OP_APPLY) { |
| 2357 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 2358 | "augmented assign to tuple not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2359 | return; |
| 2360 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2361 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2362 | return; |
| 2363 | } |
| 2364 | n = CHILD(n, 0); |
| 2365 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2366 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2367 | case test: |
| 2368 | case and_test: |
| 2369 | case not_test: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2370 | case comparison: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2371 | case expr: |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2372 | case xor_expr: |
| 2373 | case and_expr: |
| 2374 | case shift_expr: |
| 2375 | case arith_expr: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2376 | case term: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2377 | case factor: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2378 | if (NCH(n) > 1) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2379 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2380 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2381 | return; |
| 2382 | } |
| 2383 | n = CHILD(n, 0); |
| 2384 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2385 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2386 | case power: /* atom trailer* ('**' power)* |
| 2387 | ('+'|'-'|'~') factor | atom trailer* */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2388 | if (TYPE(CHILD(n, 0)) != atom) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2389 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2390 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2391 | return; |
| 2392 | } |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2393 | if (NCH(n) > 1) { /* trailer or exponent present */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2394 | int i; |
| 2395 | com_node(c, CHILD(n, 0)); |
| 2396 | for (i = 1; i+1 < NCH(n); i++) { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2397 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2398 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2399 | "can't assign to operator"); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2400 | return; |
| 2401 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2402 | com_apply_trailer(c, CHILD(n, i)); |
| 2403 | } /* NB i is still alive */ |
| 2404 | com_assign_trailer(c, |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2405 | CHILD(n, i), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2406 | return; |
| 2407 | } |
| 2408 | n = CHILD(n, 0); |
| 2409 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2410 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2411 | case atom: |
| 2412 | switch (TYPE(CHILD(n, 0))) { |
| 2413 | case LPAR: |
| 2414 | n = CHILD(n, 1); |
| 2415 | if (TYPE(n) == RPAR) { |
| 2416 | /* XXX Should allow () = () ??? */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2417 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2418 | "can't assign to ()"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2419 | return; |
| 2420 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2421 | if (assigning > OP_APPLY) { |
| 2422 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2423 | "augmented assign to tuple not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2424 | return; |
| 2425 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2426 | break; |
| 2427 | case LSQB: |
| 2428 | n = CHILD(n, 1); |
| 2429 | if (TYPE(n) == RSQB) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2430 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2431 | "can't assign to []"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2432 | return; |
| 2433 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2434 | if (assigning > OP_APPLY) { |
| 2435 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2436 | "augmented assign to list not possible"); |
| 2437 | return; |
| 2438 | } |
| 2439 | if (NCH(n) > 1 |
| 2440 | && TYPE(CHILD(n, 1)) == list_for) { |
| 2441 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2442 | "can't assign to list comprehension"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2443 | return; |
| 2444 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2445 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2446 | return; |
| 2447 | case NAME: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2448 | if (assigning > OP_APPLY) |
| 2449 | com_augassign_name(c, CHILD(n, 0), |
| 2450 | assigning, augn); |
| 2451 | else |
| 2452 | com_assign_name(c, CHILD(n, 0), |
| 2453 | assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2454 | return; |
| 2455 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2456 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2457 | "can't assign to literal"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2458 | return; |
| 2459 | } |
| 2460 | break; |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2461 | |
| 2462 | case lambdef: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2463 | com_error(c, PyExc_SyntaxError, |
| 2464 | "can't assign to lambda"); |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2465 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2466 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2467 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2468 | com_error(c, PyExc_SystemError, |
| 2469 | "com_assign: bad node"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2470 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2471 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2472 | } |
| 2473 | } |
| 2474 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2475 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2476 | static void |
| 2477 | com_augassign(struct compiling *c, node *n) |
| 2478 | { |
| 2479 | int opcode; |
| 2480 | |
| 2481 | switch (STR(CHILD(CHILD(n, 1), 0))[0]) { |
| 2482 | case '+': opcode = INPLACE_ADD; break; |
| 2483 | case '-': opcode = INPLACE_SUBTRACT; break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2484 | case '/': |
| 2485 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '/') |
| 2486 | opcode = INPLACE_FLOOR_DIVIDE; |
| 2487 | else if (c->c_flags & CO_FUTURE_DIVISION) |
| 2488 | opcode = INPLACE_TRUE_DIVIDE; |
| 2489 | else |
| 2490 | opcode = INPLACE_DIVIDE; |
| 2491 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2492 | case '%': opcode = INPLACE_MODULO; break; |
| 2493 | case '<': opcode = INPLACE_LSHIFT; break; |
| 2494 | case '>': opcode = INPLACE_RSHIFT; break; |
| 2495 | case '&': opcode = INPLACE_AND; break; |
| 2496 | case '^': opcode = INPLACE_XOR; break; |
| 2497 | case '|': opcode = INPLACE_OR; break; |
| 2498 | case '*': |
| 2499 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '*') |
| 2500 | opcode = INPLACE_POWER; |
| 2501 | else |
| 2502 | opcode = INPLACE_MULTIPLY; |
| 2503 | break; |
| 2504 | default: |
| 2505 | com_error(c, PyExc_SystemError, "com_augassign: bad operator"); |
| 2506 | return; |
| 2507 | } |
| 2508 | com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2)); |
| 2509 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2510 | |
| 2511 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2512 | com_expr_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2513 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2514 | REQ(n, expr_stmt); |
| 2515 | /* testlist (('=' testlist)* | augassign testlist) */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2516 | /* Forget it if we have just a doc string here */ |
Guido van Rossum | 5f5e817 | 1997-04-06 03:41:40 +0000 | [diff] [blame] | 2517 | if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2518 | return; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2519 | if (NCH(n) == 1) { |
| 2520 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2521 | if (c->c_interactive) |
| 2522 | com_addbyte(c, PRINT_EXPR); |
| 2523 | else |
| 2524 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2525 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2526 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2527 | else if (TYPE(CHILD(n,1)) == augassign) |
| 2528 | com_augassign(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2529 | else { |
| 2530 | int i; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2531 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2532 | for (i = 0; i < NCH(n)-2; i+=2) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2533 | if (i+2 < NCH(n)-2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2534 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2535 | com_push(c, 1); |
| 2536 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2537 | com_assign(c, CHILD(n, i), OP_ASSIGN, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2538 | } |
| 2539 | } |
| 2540 | } |
| 2541 | |
| 2542 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2543 | com_assert_stmt(struct compiling *c, node *n) |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2544 | { |
| 2545 | int a = 0, b = 0; |
| 2546 | int i; |
| 2547 | REQ(n, assert_stmt); /* 'assert' test [',' test] */ |
| 2548 | /* Generate code like for |
| 2549 | |
| 2550 | if __debug__: |
| 2551 | if not <test>: |
| 2552 | raise AssertionError [, <message>] |
| 2553 | |
| 2554 | where <message> is the second test, if present. |
| 2555 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2556 | |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2557 | if (Py_OptimizeFlag) |
| 2558 | return; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2559 | com_addop_name(c, LOAD_GLOBAL, "__debug__"); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2560 | com_push(c, 1); |
| 2561 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 2562 | com_addbyte(c, POP_TOP); |
| 2563 | com_pop(c, 1); |
| 2564 | com_node(c, CHILD(n, 1)); |
| 2565 | com_addfwref(c, JUMP_IF_TRUE, &b); |
| 2566 | com_addbyte(c, POP_TOP); |
| 2567 | com_pop(c, 1); |
| 2568 | /* Raise that exception! */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2569 | com_addop_name(c, LOAD_GLOBAL, "AssertionError"); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2570 | com_push(c, 1); |
| 2571 | i = NCH(n)/2; /* Either 2 or 4 */ |
| 2572 | if (i > 1) |
| 2573 | com_node(c, CHILD(n, 3)); |
| 2574 | com_addoparg(c, RAISE_VARARGS, i); |
| 2575 | com_pop(c, i); |
| 2576 | /* The interpreter does not fall through */ |
| 2577 | /* All jumps converge here */ |
| 2578 | com_backpatch(c, a); |
| 2579 | com_backpatch(c, b); |
| 2580 | com_addbyte(c, POP_TOP); |
| 2581 | } |
| 2582 | |
| 2583 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2584 | com_print_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2585 | { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2586 | int i = 1; |
| 2587 | node* stream = NULL; |
| 2588 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2589 | REQ(n, print_stmt); /* 'print' (test ',')* [test] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2590 | |
| 2591 | /* are we using the extended print form? */ |
| 2592 | if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) { |
| 2593 | stream = CHILD(n, 2); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2594 | com_node(c, stream); |
| 2595 | /* stack: [...] => [... stream] */ |
| 2596 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2597 | if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA) |
| 2598 | i = 4; |
| 2599 | else |
| 2600 | i = 3; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2601 | } |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2602 | for (; i < NCH(n); i += 2) { |
| 2603 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2604 | com_addbyte(c, DUP_TOP); |
| 2605 | /* stack: [stream] => [stream stream] */ |
| 2606 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2607 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2608 | /* stack: [stream stream] => [stream stream obj] */ |
| 2609 | com_addbyte(c, ROT_TWO); |
| 2610 | /* stack: [stream stream obj] => [stream obj stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2611 | com_addbyte(c, PRINT_ITEM_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2612 | /* stack: [stream obj stream] => [stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2613 | com_pop(c, 2); |
| 2614 | } |
| 2615 | else { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2616 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2617 | /* stack: [...] => [... obj] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2618 | com_addbyte(c, PRINT_ITEM); |
| 2619 | com_pop(c, 1); |
| 2620 | } |
| 2621 | } |
| 2622 | /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */ |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2623 | if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2624 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2625 | /* must pop the extra stream object off the stack */ |
| 2626 | com_addbyte(c, POP_TOP); |
| 2627 | /* stack: [... stream] => [...] */ |
| 2628 | com_pop(c, 1); |
| 2629 | } |
| 2630 | } |
| 2631 | else { |
| 2632 | if (stream != NULL) { |
| 2633 | /* this consumes the last stream object on stack */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2634 | com_addbyte(c, PRINT_NEWLINE_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2635 | /* stack: [... stream] => [...] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2636 | com_pop(c, 1); |
| 2637 | } |
| 2638 | else |
| 2639 | com_addbyte(c, PRINT_NEWLINE); |
| 2640 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
| 2643 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2644 | com_return_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2645 | { |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2646 | REQ(n, return_stmt); /* 'return' [testlist] */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2647 | if (!c->c_infunction) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2648 | com_error(c, PyExc_SyntaxError, "'return' outside function"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2649 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2650 | if (c->c_flags & CO_GENERATOR) { |
| 2651 | if (NCH(n) > 1) { |
| 2652 | com_error(c, PyExc_SyntaxError, |
| 2653 | "'return' with argument inside generator"); |
| 2654 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 2655 | } |
| 2656 | if (NCH(n) < 2) { |
| 2657 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2658 | com_push(c, 1); |
| 2659 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 2660 | else |
| 2661 | com_node(c, CHILD(n, 1)); |
| 2662 | com_addbyte(c, RETURN_VALUE); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2663 | com_pop(c, 1); |
| 2664 | } |
| 2665 | |
| 2666 | static void |
| 2667 | com_yield_stmt(struct compiling *c, node *n) |
| 2668 | { |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 2669 | int i; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2670 | REQ(n, yield_stmt); /* 'yield' testlist */ |
| 2671 | if (!c->c_infunction) { |
| 2672 | com_error(c, PyExc_SyntaxError, "'yield' outside function"); |
| 2673 | } |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 2674 | |
| 2675 | for (i = 0; i < c->c_nblocks; ++i) { |
| 2676 | if (c->c_block[i] == SETUP_FINALLY) { |
| 2677 | com_error(c, PyExc_SyntaxError, |
| 2678 | "'yield' not allowed in a 'try' block " |
| 2679 | "with a 'finally' clause"); |
| 2680 | return; |
| 2681 | } |
| 2682 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2683 | com_node(c, CHILD(n, 1)); |
| 2684 | com_addbyte(c, YIELD_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2685 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2686 | } |
| 2687 | |
| 2688 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2689 | com_raise_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2690 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2691 | int i; |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 2692 | REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */ |
| 2693 | if (NCH(n) > 1) { |
| 2694 | com_node(c, CHILD(n, 1)); |
| 2695 | if (NCH(n) > 3) { |
| 2696 | com_node(c, CHILD(n, 3)); |
| 2697 | if (NCH(n) > 5) |
| 2698 | com_node(c, CHILD(n, 5)); |
| 2699 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2700 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2701 | i = NCH(n)/2; |
| 2702 | com_addoparg(c, RAISE_VARARGS, i); |
| 2703 | com_pop(c, i); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
| 2706 | static void |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2707 | com_from_import(struct compiling *c, node *n) |
| 2708 | { |
| 2709 | com_addopname(c, IMPORT_FROM, CHILD(n, 0)); |
| 2710 | com_push(c, 1); |
| 2711 | if (NCH(n) > 1) { |
| 2712 | if (strcmp(STR(CHILD(n, 1)), "as") != 0) { |
| 2713 | com_error(c, PyExc_SyntaxError, "invalid syntax"); |
| 2714 | return; |
| 2715 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2716 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2717 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2718 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2719 | com_pop(c, 1); |
| 2720 | } |
| 2721 | |
| 2722 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2723 | com_import_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2724 | { |
| 2725 | int i; |
| 2726 | REQ(n, import_stmt); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2727 | /* 'import' dotted_name (',' dotted_name)* | |
| 2728 | 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2729 | if (STR(CHILD(n, 0))[0] == 'f') { |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 2730 | PyObject *tup; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2731 | /* 'from' dotted_name 'import' ... */ |
| 2732 | REQ(CHILD(n, 1), dotted_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2733 | |
| 2734 | if (TYPE(CHILD(n, 3)) == STAR) { |
| 2735 | tup = Py_BuildValue("(s)", "*"); |
| 2736 | } else { |
| 2737 | tup = PyTuple_New((NCH(n) - 2)/2); |
| 2738 | for (i = 3; i < NCH(n); i += 2) { |
| 2739 | PyTuple_SET_ITEM(tup, (i-3)/2, |
| 2740 | PyString_FromString(STR( |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 2741 | CHILD(CHILD(n, i), 0)))); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2742 | } |
| 2743 | } |
| 2744 | com_addoparg(c, LOAD_CONST, com_addconst(c, tup)); |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 2745 | Py_DECREF(tup); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2746 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2747 | com_addopname(c, IMPORT_NAME, CHILD(n, 1)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2748 | if (TYPE(CHILD(n, 3)) == STAR) |
| 2749 | com_addbyte(c, IMPORT_STAR); |
| 2750 | else { |
| 2751 | for (i = 3; i < NCH(n); i += 2) |
| 2752 | com_from_import(c, CHILD(n, i)); |
| 2753 | com_addbyte(c, POP_TOP); |
| 2754 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2755 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2756 | } |
| 2757 | else { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2758 | /* 'import' ... */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2759 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2760 | node *subn = CHILD(n, i); |
| 2761 | REQ(subn, dotted_as_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2762 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2763 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2764 | com_addopname(c, IMPORT_NAME, CHILD(subn, 0)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2765 | if (NCH(subn) > 1) { |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 2766 | int j; |
| 2767 | if (strcmp(STR(CHILD(subn, 1)), "as") != 0) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2768 | com_error(c, PyExc_SyntaxError, |
| 2769 | "invalid syntax"); |
| 2770 | return; |
| 2771 | } |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 2772 | for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2) |
| 2773 | com_addopname(c, LOAD_ATTR, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2774 | CHILD(CHILD(subn, 0), |
| 2775 | j)); |
| 2776 | com_addop_varname(c, VAR_STORE, |
| 2777 | STR(CHILD(subn, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2778 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2779 | com_addop_varname(c, VAR_STORE, |
| 2780 | STR(CHILD(CHILD(subn, 0), |
| 2781 | 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2782 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2783 | } |
| 2784 | } |
| 2785 | } |
| 2786 | |
| 2787 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2788 | com_exec_stmt(struct compiling *c, node *n) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2789 | { |
| 2790 | REQ(n, exec_stmt); |
| 2791 | /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */ |
| 2792 | com_node(c, CHILD(n, 1)); |
| 2793 | if (NCH(n) >= 4) |
| 2794 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2795 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2796 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2797 | com_push(c, 1); |
| 2798 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2799 | if (NCH(n) >= 6) |
| 2800 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2801 | else { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2802 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2803 | com_push(c, 1); |
| 2804 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2805 | com_addbyte(c, EXEC_STMT); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2806 | com_pop(c, 3); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2807 | } |
| 2808 | |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2809 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2810 | is_constant_false(struct compiling *c, node *n) |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2811 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2812 | PyObject *v; |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2813 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2814 | /* argument c will be NULL when called from symtable_node() */ |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2815 | |
| 2816 | /* Label to avoid tail recursion */ |
| 2817 | next: |
| 2818 | switch (TYPE(n)) { |
| 2819 | |
| 2820 | case suite: |
| 2821 | if (NCH(n) == 1) { |
| 2822 | n = CHILD(n, 0); |
| 2823 | goto next; |
| 2824 | } |
| 2825 | /* Fall through */ |
| 2826 | case file_input: |
| 2827 | for (i = 0; i < NCH(n); i++) { |
| 2828 | node *ch = CHILD(n, i); |
| 2829 | if (TYPE(ch) == stmt) { |
| 2830 | n = ch; |
| 2831 | goto next; |
| 2832 | } |
| 2833 | } |
| 2834 | break; |
| 2835 | |
| 2836 | case stmt: |
| 2837 | case simple_stmt: |
| 2838 | case small_stmt: |
| 2839 | n = CHILD(n, 0); |
| 2840 | goto next; |
| 2841 | |
| 2842 | case expr_stmt: |
| 2843 | case testlist: |
| 2844 | case test: |
| 2845 | case and_test: |
| 2846 | case not_test: |
| 2847 | case comparison: |
| 2848 | case expr: |
| 2849 | case xor_expr: |
| 2850 | case and_expr: |
| 2851 | case shift_expr: |
| 2852 | case arith_expr: |
| 2853 | case term: |
| 2854 | case factor: |
| 2855 | case power: |
| 2856 | case atom: |
| 2857 | if (NCH(n) == 1) { |
| 2858 | n = CHILD(n, 0); |
| 2859 | goto next; |
| 2860 | } |
| 2861 | break; |
| 2862 | |
| 2863 | case NAME: |
| 2864 | if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0) |
| 2865 | return 1; |
| 2866 | break; |
| 2867 | |
| 2868 | case NUMBER: |
| 2869 | v = parsenumber(c, STR(n)); |
| 2870 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2871 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2872 | break; |
| 2873 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2874 | i = PyObject_IsTrue(v); |
| 2875 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2876 | return i == 0; |
| 2877 | |
| 2878 | case STRING: |
Guido van Rossum | 6af0c00 | 1997-03-11 21:25:55 +0000 | [diff] [blame] | 2879 | v = parsestr(STR(n)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2880 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2881 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2882 | break; |
| 2883 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2884 | i = PyObject_IsTrue(v); |
| 2885 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2886 | return i == 0; |
| 2887 | |
| 2888 | } |
| 2889 | return 0; |
| 2890 | } |
| 2891 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 2892 | |
| 2893 | /* Look under n for a return stmt with an expression. |
| 2894 | * This hack is used to find illegal returns under "if 0:" blocks in |
| 2895 | * functions already known to be generators (as determined by the symtable |
| 2896 | * pass). |
| 2897 | * Return the offending return node if found, else NULL. |
| 2898 | */ |
| 2899 | static node * |
| 2900 | look_for_offending_return(node *n) |
| 2901 | { |
| 2902 | int i; |
| 2903 | |
| 2904 | for (i = 0; i < NCH(n); ++i) { |
| 2905 | node *kid = CHILD(n, i); |
| 2906 | |
| 2907 | switch (TYPE(kid)) { |
| 2908 | case classdef: |
| 2909 | case funcdef: |
| 2910 | case lambdef: |
| 2911 | /* Stuff in nested functions & classes doesn't |
| 2912 | affect the code block we started in. */ |
| 2913 | return NULL; |
| 2914 | |
| 2915 | case return_stmt: |
| 2916 | if (NCH(kid) > 1) |
| 2917 | return kid; |
| 2918 | break; |
| 2919 | |
| 2920 | default: { |
| 2921 | node *bad = look_for_offending_return(kid); |
| 2922 | if (bad != NULL) |
| 2923 | return bad; |
| 2924 | } |
| 2925 | } |
| 2926 | } |
| 2927 | |
| 2928 | return NULL; |
| 2929 | } |
| 2930 | |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2931 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2932 | com_if_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2933 | { |
| 2934 | int i; |
| 2935 | int anchor = 0; |
| 2936 | REQ(n, if_stmt); |
| 2937 | /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */ |
| 2938 | for (i = 0; i+3 < NCH(n); i+=4) { |
| 2939 | int a = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2940 | node *ch = CHILD(n, i+1); |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 2941 | if (is_constant_false(c, ch)) { |
| 2942 | /* We're going to skip this block. However, if this |
| 2943 | is a generator, we have to check the dead code |
| 2944 | anyway to make sure there aren't any return stmts |
| 2945 | with expressions, in the same scope. */ |
| 2946 | if (c->c_flags & CO_GENERATOR) { |
| 2947 | node *p = look_for_offending_return(n); |
| 2948 | if (p != NULL) { |
| 2949 | int savelineno = c->c_lineno; |
| 2950 | c->c_lineno = p->n_lineno; |
| 2951 | com_error(c, PyExc_SyntaxError, |
| 2952 | "'return' with argument " |
| 2953 | "inside generator"); |
| 2954 | c->c_lineno = savelineno; |
| 2955 | } |
| 2956 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2957 | continue; |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 2958 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2959 | if (i > 0) |
| 2960 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2961 | com_node(c, ch); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2962 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 2963 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2964 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2965 | com_node(c, CHILD(n, i+3)); |
| 2966 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 2967 | com_backpatch(c, a); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2968 | /* We jump here with an extra entry which we now pop */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2969 | com_addbyte(c, POP_TOP); |
| 2970 | } |
| 2971 | if (i+2 < NCH(n)) |
| 2972 | com_node(c, CHILD(n, i+2)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2973 | if (anchor) |
| 2974 | com_backpatch(c, anchor); |
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_while_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2979 | { |
| 2980 | int break_anchor = 0; |
| 2981 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2982 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2983 | REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */ |
| 2984 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2985 | block_push(c, SETUP_LOOP); |
| 2986 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2987 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2988 | com_node(c, CHILD(n, 1)); |
| 2989 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2990 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2991 | com_pop(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2992 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2993 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2994 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2995 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 2996 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2997 | com_backpatch(c, anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2998 | /* We jump here with one entry more on the stack */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2999 | com_addbyte(c, POP_TOP); |
| 3000 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3001 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3002 | if (NCH(n) > 4) |
| 3003 | com_node(c, CHILD(n, 6)); |
| 3004 | com_backpatch(c, break_anchor); |
| 3005 | } |
| 3006 | |
| 3007 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3008 | com_for_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3009 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3010 | int break_anchor = 0; |
| 3011 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3012 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3013 | REQ(n, for_stmt); |
| 3014 | /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */ |
| 3015 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3016 | block_push(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3017 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3018 | com_addbyte(c, GET_ITER); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3019 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3020 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3021 | com_addfwref(c, FOR_ITER, &anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3022 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3023 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3024 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3025 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3026 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3027 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3028 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3029 | com_backpatch(c, anchor); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3030 | com_pop(c, 1); /* FOR_ITER has popped this */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3031 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3032 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3033 | if (NCH(n) > 8) |
| 3034 | com_node(c, CHILD(n, 8)); |
| 3035 | com_backpatch(c, break_anchor); |
| 3036 | } |
| 3037 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3038 | /* Code generated for "try: S finally: Sf" is as follows: |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3039 | |
| 3040 | SETUP_FINALLY L |
| 3041 | <code for S> |
| 3042 | POP_BLOCK |
| 3043 | LOAD_CONST <nil> |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3044 | L: <code for Sf> |
| 3045 | END_FINALLY |
| 3046 | |
| 3047 | The special instructions use the block stack. Each block |
| 3048 | stack entry contains the instruction that created it (here |
| 3049 | SETUP_FINALLY), the level of the value stack at the time the |
| 3050 | block stack entry was created, and a label (here L). |
| 3051 | |
| 3052 | SETUP_FINALLY: |
| 3053 | Pushes the current value stack level and the label |
| 3054 | onto the block stack. |
| 3055 | POP_BLOCK: |
| 3056 | Pops en entry from the block stack, and pops the value |
| 3057 | stack until its level is the same as indicated on the |
| 3058 | block stack. (The label is ignored.) |
| 3059 | END_FINALLY: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3060 | Pops a variable number of entries from the *value* stack |
| 3061 | and re-raises the exception they specify. The number of |
| 3062 | entries popped depends on the (pseudo) exception type. |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3063 | |
| 3064 | The block stack is unwound when an exception is raised: |
| 3065 | when a SETUP_FINALLY entry is found, the exception is pushed |
| 3066 | onto the value stack (and the exception condition is cleared), |
| 3067 | and the interpreter jumps to the label gotten from the block |
| 3068 | stack. |
| 3069 | |
| 3070 | 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] | 3071 | (The contents of the value stack is shown in [], with the top |
| 3072 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3073 | associated value, and 'exc' the exception.) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3074 | |
| 3075 | Value stack Label Instruction Argument |
| 3076 | [] SETUP_EXCEPT L1 |
| 3077 | [] <code for S> |
| 3078 | [] POP_BLOCK |
| 3079 | [] JUMP_FORWARD L0 |
| 3080 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3081 | [tb, val, exc] L1: DUP ) |
| 3082 | [tb, val, exc, exc] <evaluate E1> ) |
| 3083 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 3084 | [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 ) |
| 3085 | [tb, val, exc, 1] POP ) |
| 3086 | [tb, val, exc] POP |
| 3087 | [tb, val] <assign to V1> (or POP if no V1) |
| 3088 | [tb] POP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3089 | [] <code for S1> |
| 3090 | JUMP_FORWARD L0 |
| 3091 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3092 | [tb, val, exc, 0] L2: POP |
| 3093 | [tb, val, exc] DUP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3094 | .............................etc....................... |
| 3095 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3096 | [tb, val, exc, 0] Ln+1: POP |
| 3097 | [tb, val, exc] END_FINALLY # re-raise exception |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3098 | |
| 3099 | [] L0: <next statement> |
| 3100 | |
| 3101 | Of course, parts are not generated if Vi or Ei is not present. |
| 3102 | */ |
| 3103 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3104 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3105 | com_try_except(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3106 | { |
| 3107 | int except_anchor = 0; |
| 3108 | int end_anchor = 0; |
| 3109 | int else_anchor = 0; |
| 3110 | int i; |
| 3111 | node *ch; |
| 3112 | |
| 3113 | com_addfwref(c, SETUP_EXCEPT, &except_anchor); |
| 3114 | block_push(c, SETUP_EXCEPT); |
| 3115 | com_node(c, CHILD(n, 2)); |
| 3116 | com_addbyte(c, POP_BLOCK); |
| 3117 | block_pop(c, SETUP_EXCEPT); |
| 3118 | com_addfwref(c, JUMP_FORWARD, &else_anchor); |
| 3119 | com_backpatch(c, except_anchor); |
| 3120 | for (i = 3; |
| 3121 | i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause; |
| 3122 | i += 3) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3123 | /* except_clause: 'except' [expr [',' var]] */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3124 | if (except_anchor == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3125 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 3126 | "default 'except:' must be last"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3127 | break; |
| 3128 | } |
| 3129 | except_anchor = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3130 | com_push(c, 3); /* tb, val, exc pushed by exception */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3131 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 3132 | if (NCH(ch) > 1) { |
| 3133 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3134 | com_push(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3135 | com_node(c, CHILD(ch, 1)); |
| 3136 | com_addoparg(c, COMPARE_OP, EXC_MATCH); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3137 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3138 | com_addfwref(c, JUMP_IF_FALSE, &except_anchor); |
| 3139 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3140 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3141 | } |
| 3142 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3143 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3144 | if (NCH(ch) > 3) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3145 | com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3146 | else { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3147 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3148 | com_pop(c, 1); |
| 3149 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3150 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3151 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3152 | com_node(c, CHILD(n, i+2)); |
| 3153 | com_addfwref(c, JUMP_FORWARD, &end_anchor); |
| 3154 | if (except_anchor) { |
| 3155 | com_backpatch(c, except_anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3156 | /* We come in with [tb, val, exc, 0] on the |
| 3157 | stack; one pop and it's the same as |
| 3158 | expected at the start of the loop */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3159 | com_addbyte(c, POP_TOP); |
| 3160 | } |
| 3161 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3162 | /* We actually come in here with [tb, val, exc] but the |
| 3163 | END_FINALLY will zap those and jump around. |
| 3164 | The c_stacklevel does not reflect them so we need not pop |
| 3165 | anything. */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3166 | com_addbyte(c, END_FINALLY); |
| 3167 | com_backpatch(c, else_anchor); |
| 3168 | if (i < NCH(n)) |
| 3169 | com_node(c, CHILD(n, i+2)); |
| 3170 | com_backpatch(c, end_anchor); |
| 3171 | } |
| 3172 | |
| 3173 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3174 | com_try_finally(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3175 | { |
| 3176 | int finally_anchor = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3177 | node *ch; |
Guido van Rossum | 94fb82e | 1992-04-05 14:24:50 +0000 | [diff] [blame] | 3178 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3179 | com_addfwref(c, SETUP_FINALLY, &finally_anchor); |
| 3180 | block_push(c, SETUP_FINALLY); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3181 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3182 | com_addbyte(c, POP_BLOCK); |
| 3183 | block_pop(c, SETUP_FINALLY); |
| 3184 | block_push(c, END_FINALLY); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3185 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3186 | /* While the generated code pushes only one item, |
| 3187 | the try-finally handling can enter here with |
| 3188 | up to three items. OK, here are the details: |
| 3189 | 3 for an exception, 2 for RETURN, 1 for BREAK. */ |
| 3190 | com_push(c, 3); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3191 | com_backpatch(c, finally_anchor); |
| 3192 | ch = CHILD(n, NCH(n)-1); |
| 3193 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 3194 | com_node(c, ch); |
| 3195 | com_addbyte(c, END_FINALLY); |
| 3196 | block_pop(c, END_FINALLY); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3197 | com_pop(c, 3); /* Matches the com_push above */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3198 | } |
| 3199 | |
| 3200 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3201 | com_try_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3202 | { |
| 3203 | REQ(n, try_stmt); |
| 3204 | /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
| 3205 | | 'try' ':' suite 'finally' ':' suite */ |
| 3206 | if (TYPE(CHILD(n, 3)) != except_clause) |
| 3207 | com_try_finally(c, n); |
| 3208 | else |
| 3209 | com_try_except(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3210 | } |
| 3211 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3212 | static node * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3213 | get_rawdocstring(node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3214 | { |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3215 | int i; |
| 3216 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3217 | /* Label to avoid tail recursion */ |
| 3218 | next: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3219 | switch (TYPE(n)) { |
| 3220 | |
| 3221 | case suite: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3222 | if (NCH(n) == 1) { |
| 3223 | n = CHILD(n, 0); |
| 3224 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3225 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3226 | /* Fall through */ |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3227 | case file_input: |
| 3228 | for (i = 0; i < NCH(n); i++) { |
| 3229 | node *ch = CHILD(n, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3230 | if (TYPE(ch) == stmt) { |
| 3231 | n = ch; |
| 3232 | goto next; |
| 3233 | } |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3234 | } |
| 3235 | break; |
| 3236 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3237 | case stmt: |
| 3238 | case simple_stmt: |
| 3239 | case small_stmt: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3240 | n = CHILD(n, 0); |
| 3241 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3242 | |
| 3243 | case expr_stmt: |
| 3244 | case testlist: |
| 3245 | case test: |
| 3246 | case and_test: |
| 3247 | case not_test: |
| 3248 | case comparison: |
| 3249 | case expr: |
| 3250 | case xor_expr: |
| 3251 | case and_expr: |
| 3252 | case shift_expr: |
| 3253 | case arith_expr: |
| 3254 | case term: |
| 3255 | case factor: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3256 | case power: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3257 | if (NCH(n) == 1) { |
| 3258 | n = CHILD(n, 0); |
| 3259 | goto next; |
| 3260 | } |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3261 | break; |
| 3262 | |
| 3263 | case atom: |
| 3264 | if (TYPE(CHILD(n, 0)) == STRING) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3265 | return n; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3266 | break; |
| 3267 | |
| 3268 | } |
| 3269 | return NULL; |
| 3270 | } |
| 3271 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3272 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3273 | get_docstring(node *n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3274 | { |
Guido van Rossum | 541563e | 1999-01-28 15:08:09 +0000 | [diff] [blame] | 3275 | /* Don't generate doc-strings if run with -OO */ |
| 3276 | if (Py_OptimizeFlag > 1) |
| 3277 | return NULL; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3278 | n = get_rawdocstring(n); |
| 3279 | if (n == NULL) |
| 3280 | return NULL; |
| 3281 | return parsestrplus(n); |
| 3282 | } |
| 3283 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3284 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3285 | com_suite(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3286 | { |
| 3287 | REQ(n, suite); |
| 3288 | /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */ |
| 3289 | if (NCH(n) == 1) { |
| 3290 | com_node(c, CHILD(n, 0)); |
| 3291 | } |
| 3292 | else { |
| 3293 | int i; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3294 | for (i = 0; i < NCH(n) && c->c_errors == 0; i++) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3295 | node *ch = CHILD(n, i); |
| 3296 | if (TYPE(ch) == stmt) |
| 3297 | com_node(c, ch); |
| 3298 | } |
| 3299 | } |
| 3300 | } |
| 3301 | |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3302 | /* ARGSUSED */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3303 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3304 | com_continue_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3305 | { |
| 3306 | int i = c->c_nblocks; |
| 3307 | if (i-- > 0 && c->c_block[i] == SETUP_LOOP) { |
| 3308 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3309 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3310 | else if (i <= 0) { |
| 3311 | /* at the outer level */ |
| 3312 | com_error(c, PyExc_SyntaxError, |
| 3313 | "'continue' not properly in loop"); |
| 3314 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3315 | else { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3316 | int j; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3317 | for (j = i-1; j >= 0; --j) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3318 | if (c->c_block[j] == SETUP_LOOP) |
| 3319 | break; |
| 3320 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3321 | if (j >= 0) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3322 | /* there is a loop, but something interferes */ |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3323 | for (; i > j; --i) { |
| 3324 | if (c->c_block[i] == SETUP_EXCEPT || |
| 3325 | c->c_block[i] == SETUP_FINALLY) { |
| 3326 | com_addoparg(c, CONTINUE_LOOP, |
| 3327 | c->c_begin); |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3328 | return; |
| 3329 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3330 | if (c->c_block[i] == END_FINALLY) { |
| 3331 | com_error(c, PyExc_SyntaxError, |
| 3332 | "'continue' not supported inside 'finally' clause"); |
| 3333 | return; |
| 3334 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3335 | } |
| 3336 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3337 | com_error(c, PyExc_SyntaxError, |
| 3338 | "'continue' not properly in loop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3339 | } |
| 3340 | /* XXX Could allow it inside a 'finally' clause |
| 3341 | XXX if we could pop the exception still on the stack */ |
| 3342 | } |
| 3343 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3344 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3345 | com_argdefs(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3346 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3347 | int i, nch, nargs, ndefs; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3348 | if (TYPE(n) == lambdef) { |
| 3349 | /* lambdef: 'lambda' [varargslist] ':' test */ |
| 3350 | n = CHILD(n, 1); |
| 3351 | } |
| 3352 | else { |
| 3353 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */ |
| 3354 | n = CHILD(n, 2); |
| 3355 | REQ(n, parameters); /* parameters: '(' [varargslist] ')' */ |
| 3356 | n = CHILD(n, 1); |
| 3357 | } |
| 3358 | if (TYPE(n) != varargslist) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3359 | return 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3360 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3361 | (fpdef ['=' test] ',')* '*' ....... | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3362 | fpdef ['=' test] (',' fpdef ['=' test])* [','] */ |
| 3363 | nch = NCH(n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3364 | nargs = 0; |
| 3365 | ndefs = 0; |
| 3366 | for (i = 0; i < nch; i++) { |
| 3367 | int t; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3368 | if (TYPE(CHILD(n, i)) == STAR || |
| 3369 | TYPE(CHILD(n, i)) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3370 | break; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3371 | nargs++; |
| 3372 | i++; |
| 3373 | if (i >= nch) |
Guido van Rossum | a1e7e62 | 1995-09-18 21:44:04 +0000 | [diff] [blame] | 3374 | t = RPAR; /* Anything except EQUAL or COMMA */ |
| 3375 | else |
| 3376 | t = TYPE(CHILD(n, i)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3377 | if (t == EQUAL) { |
| 3378 | i++; |
| 3379 | ndefs++; |
| 3380 | com_node(c, CHILD(n, i)); |
| 3381 | i++; |
| 3382 | if (i >= nch) |
| 3383 | break; |
| 3384 | t = TYPE(CHILD(n, i)); |
| 3385 | } |
| 3386 | else { |
Guido van Rossum | 29d38cd | 1998-10-02 13:41:54 +0000 | [diff] [blame] | 3387 | /* Treat "(a=1, b)" as an error */ |
| 3388 | if (ndefs) |
| 3389 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | dfede31 | 1998-10-02 14:06:56 +0000 | [diff] [blame] | 3390 | "non-default argument follows default argument"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3391 | } |
| 3392 | if (t != COMMA) |
| 3393 | break; |
| 3394 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3395 | return ndefs; |
| 3396 | } |
| 3397 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3398 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3399 | com_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3400 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3401 | PyObject *co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3402 | int ndefs; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3403 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3404 | ndefs = com_argdefs(c, n); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3405 | symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n), |
| 3406 | n->n_lineno); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3407 | co = (PyObject *)icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3408 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3409 | if (co == NULL) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3410 | c->c_errors++; |
| 3411 | else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3412 | int closure = com_make_closure(c, (PyCodeObject *)co); |
| 3413 | int i = com_addconst(c, co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3414 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3415 | com_push(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3416 | if (closure) |
| 3417 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
| 3418 | else |
| 3419 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3420 | com_pop(c, ndefs); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3421 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3422 | com_pop(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3423 | Py_DECREF(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3424 | } |
| 3425 | } |
| 3426 | |
| 3427 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3428 | com_bases(struct compiling *c, node *n) |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3429 | { |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3430 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3431 | REQ(n, testlist); |
| 3432 | /* testlist: test (',' test)* [','] */ |
| 3433 | for (i = 0; i < NCH(n); i += 2) |
| 3434 | com_node(c, CHILD(n, i)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3435 | i = (NCH(n)+1) / 2; |
| 3436 | com_addoparg(c, BUILD_TUPLE, i); |
| 3437 | com_pop(c, i-1); |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3438 | } |
| 3439 | |
| 3440 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3441 | com_classdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3442 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3443 | int i; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3444 | PyObject *co, *v; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3445 | char *name; |
| 3446 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3447 | REQ(n, classdef); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3448 | /* classdef: class NAME ['(' testlist ')'] ':' suite */ |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 3449 | if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3450 | c->c_errors++; |
| 3451 | return; |
| 3452 | } |
| 3453 | /* Push the class name on the stack */ |
| 3454 | i = com_addconst(c, v); |
| 3455 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3456 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3457 | Py_DECREF(v); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3458 | /* Push the tuple of base classes on the stack */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3459 | if (TYPE(CHILD(n, 2)) != LPAR) { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3460 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3461 | com_push(c, 1); |
| 3462 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3463 | else |
| 3464 | com_bases(c, CHILD(n, 3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3465 | name = STR(CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3466 | symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3467 | co = (PyObject *)icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3468 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3469 | if (co == NULL) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3470 | c->c_errors++; |
| 3471 | else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3472 | int closure = com_make_closure(c, (PyCodeObject *)co); |
| 3473 | i = com_addconst(c, co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3474 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3475 | com_push(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3476 | if (closure) |
| 3477 | com_addoparg(c, MAKE_CLOSURE, 0); |
| 3478 | else |
| 3479 | com_addoparg(c, MAKE_FUNCTION, 0); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3480 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3481 | com_addbyte(c, BUILD_CLASS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3482 | com_pop(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3483 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3484 | Py_DECREF(co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3485 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3489 | com_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3490 | { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3491 | loop: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3492 | if (c->c_errors) |
| 3493 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3494 | switch (TYPE(n)) { |
| 3495 | |
| 3496 | /* Definition nodes */ |
| 3497 | |
| 3498 | case funcdef: |
| 3499 | com_funcdef(c, n); |
| 3500 | break; |
| 3501 | case classdef: |
| 3502 | com_classdef(c, n); |
| 3503 | break; |
| 3504 | |
| 3505 | /* Trivial parse tree nodes */ |
| 3506 | |
| 3507 | case stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3508 | case small_stmt: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3509 | case flow_stmt: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3510 | n = CHILD(n, 0); |
| 3511 | goto loop; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3512 | |
| 3513 | case simple_stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3514 | /* small_stmt (';' small_stmt)* [';'] NEWLINE */ |
| 3515 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 3516 | { |
| 3517 | int i; |
| 3518 | for (i = 0; i < NCH(n)-1; i += 2) |
| 3519 | com_node(c, CHILD(n, i)); |
| 3520 | } |
| 3521 | break; |
| 3522 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3523 | case compound_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3524 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3525 | n = CHILD(n, 0); |
| 3526 | goto loop; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3527 | |
| 3528 | /* Statement nodes */ |
| 3529 | |
| 3530 | case expr_stmt: |
| 3531 | com_expr_stmt(c, n); |
| 3532 | break; |
| 3533 | case print_stmt: |
| 3534 | com_print_stmt(c, n); |
| 3535 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3536 | case del_stmt: /* 'del' exprlist */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3537 | com_assign(c, CHILD(n, 1), OP_DELETE, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3538 | break; |
| 3539 | case pass_stmt: |
| 3540 | break; |
| 3541 | case break_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3542 | if (c->c_loops == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3543 | com_error(c, PyExc_SyntaxError, |
| 3544 | "'break' outside loop"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3545 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3546 | com_addbyte(c, BREAK_LOOP); |
| 3547 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3548 | case continue_stmt: |
| 3549 | com_continue_stmt(c, n); |
| 3550 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3551 | case return_stmt: |
| 3552 | com_return_stmt(c, n); |
| 3553 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3554 | case yield_stmt: |
| 3555 | com_yield_stmt(c, n); |
| 3556 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3557 | case raise_stmt: |
| 3558 | com_raise_stmt(c, n); |
| 3559 | break; |
| 3560 | case import_stmt: |
| 3561 | com_import_stmt(c, n); |
| 3562 | break; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3563 | case global_stmt: |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3564 | break; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3565 | case exec_stmt: |
| 3566 | com_exec_stmt(c, n); |
| 3567 | break; |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 3568 | case assert_stmt: |
| 3569 | com_assert_stmt(c, n); |
| 3570 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3571 | case if_stmt: |
| 3572 | com_if_stmt(c, n); |
| 3573 | break; |
| 3574 | case while_stmt: |
| 3575 | com_while_stmt(c, n); |
| 3576 | break; |
| 3577 | case for_stmt: |
| 3578 | com_for_stmt(c, n); |
| 3579 | break; |
| 3580 | case try_stmt: |
| 3581 | com_try_stmt(c, n); |
| 3582 | break; |
| 3583 | case suite: |
| 3584 | com_suite(c, n); |
| 3585 | break; |
| 3586 | |
| 3587 | /* Expression nodes */ |
| 3588 | |
| 3589 | case testlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3590 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3591 | break; |
| 3592 | case test: |
| 3593 | com_test(c, n); |
| 3594 | break; |
| 3595 | case and_test: |
| 3596 | com_and_test(c, n); |
| 3597 | break; |
| 3598 | case not_test: |
| 3599 | com_not_test(c, n); |
| 3600 | break; |
| 3601 | case comparison: |
| 3602 | com_comparison(c, n); |
| 3603 | break; |
| 3604 | case exprlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3605 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3606 | break; |
| 3607 | case expr: |
| 3608 | com_expr(c, n); |
| 3609 | break; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 3610 | case xor_expr: |
| 3611 | com_xor_expr(c, n); |
| 3612 | break; |
| 3613 | case and_expr: |
| 3614 | com_and_expr(c, n); |
| 3615 | break; |
| 3616 | case shift_expr: |
| 3617 | com_shift_expr(c, n); |
| 3618 | break; |
| 3619 | case arith_expr: |
| 3620 | com_arith_expr(c, n); |
| 3621 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3622 | case term: |
| 3623 | com_term(c, n); |
| 3624 | break; |
| 3625 | case factor: |
| 3626 | com_factor(c, n); |
| 3627 | break; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3628 | case power: |
| 3629 | com_power(c, n); |
| 3630 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3631 | case atom: |
| 3632 | com_atom(c, n); |
| 3633 | break; |
| 3634 | |
| 3635 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3636 | com_error(c, PyExc_SystemError, |
| 3637 | "com_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3638 | } |
| 3639 | } |
| 3640 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 3641 | static void com_fplist(struct compiling *, node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3642 | |
| 3643 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3644 | com_fpdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3645 | { |
| 3646 | REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3647 | if (TYPE(CHILD(n, 0)) == LPAR) |
| 3648 | com_fplist(c, CHILD(n, 1)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3649 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3650 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3651 | com_pop(c, 1); |
| 3652 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3653 | } |
| 3654 | |
| 3655 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3656 | com_fplist(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3657 | { |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 3658 | REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3659 | if (NCH(n) == 1) { |
| 3660 | com_fpdef(c, CHILD(n, 0)); |
| 3661 | } |
| 3662 | else { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3663 | int i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 3664 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3665 | com_push(c, i-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3666 | for (i = 0; i < NCH(n); i += 2) |
| 3667 | com_fpdef(c, CHILD(n, i)); |
| 3668 | } |
| 3669 | } |
| 3670 | |
| 3671 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3672 | com_arglist(struct compiling *c, node *n) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3673 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3674 | int nch, i, narg; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3675 | int complex = 0; |
Guido van Rossum | db9e20f | 1997-07-10 01:06:53 +0000 | [diff] [blame] | 3676 | char nbuf[10]; |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3677 | REQ(n, varargslist); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 3678 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3679 | (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3680 | nch = NCH(n); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3681 | /* Enter all arguments in table of locals */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3682 | for (i = 0, narg = 0; i < nch; i++) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3683 | node *ch = CHILD(n, i); |
| 3684 | node *fp; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3685 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3686 | break; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3687 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3688 | fp = CHILD(ch, 0); |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 3689 | if (TYPE(fp) != NAME) { |
Guido van Rossum | db9e20f | 1997-07-10 01:06:53 +0000 | [diff] [blame] | 3690 | sprintf(nbuf, ".%d", i); |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 3691 | complex = 1; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3692 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3693 | narg++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3694 | /* all name updates handled by symtable */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3695 | if (++i >= nch) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3696 | break; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3697 | ch = CHILD(n, i); |
| 3698 | if (TYPE(ch) == EQUAL) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3699 | i += 2; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3700 | else |
| 3701 | REQ(ch, COMMA); |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3702 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3703 | if (complex) { |
| 3704 | /* Generate code for complex arguments only after |
| 3705 | having counted the simple arguments */ |
| 3706 | int ilocal = 0; |
| 3707 | for (i = 0; i < nch; i++) { |
| 3708 | node *ch = CHILD(n, i); |
| 3709 | node *fp; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3710 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3711 | break; |
| 3712 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3713 | fp = CHILD(ch, 0); |
| 3714 | if (TYPE(fp) != NAME) { |
| 3715 | com_addoparg(c, LOAD_FAST, ilocal); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3716 | com_push(c, 1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3717 | com_fpdef(c, ch); |
| 3718 | } |
| 3719 | ilocal++; |
| 3720 | if (++i >= nch) |
| 3721 | break; |
| 3722 | ch = CHILD(n, i); |
| 3723 | if (TYPE(ch) == EQUAL) |
| 3724 | i += 2; |
| 3725 | else |
| 3726 | REQ(ch, COMMA); |
| 3727 | } |
| 3728 | } |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3729 | } |
| 3730 | |
| 3731 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3732 | com_file_input(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3733 | { |
| 3734 | int i; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3735 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3736 | REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */ |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3737 | doc = get_docstring(n); |
| 3738 | if (doc != NULL) { |
| 3739 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3740 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3741 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3742 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3743 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3744 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3745 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3746 | for (i = 0; i < NCH(n); i++) { |
| 3747 | node *ch = CHILD(n, i); |
| 3748 | if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE) |
| 3749 | com_node(c, ch); |
| 3750 | } |
| 3751 | } |
| 3752 | |
| 3753 | /* Top-level compile-node interface */ |
| 3754 | |
| 3755 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3756 | compile_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3757 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3758 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3759 | node *ch; |
| 3760 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3761 | c->c_name = STR(CHILD(n, 1)); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3762 | doc = get_docstring(CHILD(n, 4)); |
| 3763 | if (doc != NULL) { |
| 3764 | (void) com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3765 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3766 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3767 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3768 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3769 | ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */ |
| 3770 | ch = CHILD(ch, 1); /* ')' | varargslist */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3771 | if (TYPE(ch) == varargslist) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3772 | com_arglist(c, ch); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3773 | c->c_infunction = 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3774 | com_node(c, CHILD(n, 4)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3775 | c->c_infunction = 0; |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 3776 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
| 3777 | com_push(c, 1); |
| 3778 | com_addbyte(c, RETURN_VALUE); |
| 3779 | com_pop(c, 1); |
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 | compile_lambdef(struct compiling *c, node *n) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 3784 | { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3785 | node *ch; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3786 | REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */ |
Guido van Rossum | 363ac7d | 1994-11-10 22:40:34 +0000 | [diff] [blame] | 3787 | c->c_name = "<lambda>"; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3788 | |
| 3789 | ch = CHILD(n, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3790 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3791 | if (TYPE(ch) == varargslist) { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3792 | com_arglist(c, ch); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3793 | ch = CHILD(n, 3); |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3794 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3795 | else |
| 3796 | ch = CHILD(n, 2); |
| 3797 | com_node(c, ch); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 3798 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3799 | com_pop(c, 1); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 3800 | } |
| 3801 | |
| 3802 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3803 | compile_classdef(struct compiling *c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3804 | { |
| 3805 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3806 | PyObject *doc; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3807 | REQ(n, classdef); |
| 3808 | /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ |
| 3809 | c->c_name = STR(CHILD(n, 1)); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 3810 | c->c_private = c->c_name; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3811 | ch = CHILD(n, NCH(n)-1); /* The suite */ |
| 3812 | doc = get_docstring(ch); |
| 3813 | if (doc != NULL) { |
| 3814 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3815 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3816 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3817 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3818 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3819 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3820 | } |
| 3821 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3822 | (void) com_addconst(c, Py_None); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3823 | com_node(c, ch); |
| 3824 | com_addbyte(c, LOAD_LOCALS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3825 | com_push(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3826 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3827 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3828 | } |
| 3829 | |
| 3830 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3831 | compile_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3832 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3833 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 3834 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3835 | switch (TYPE(n)) { |
| 3836 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3837 | case single_input: /* One interactive command */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3838 | /* NEWLINE | simple_stmt | compound_stmt NEWLINE */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3839 | c->c_interactive++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3840 | n = CHILD(n, 0); |
| 3841 | if (TYPE(n) != NEWLINE) |
| 3842 | com_node(c, n); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3843 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3844 | com_push(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3845 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3846 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3847 | c->c_interactive--; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3848 | break; |
| 3849 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3850 | case file_input: /* A whole file, or built-in function exec() */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3851 | com_file_input(c, n); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3852 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3853 | com_push(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3854 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3855 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3856 | break; |
| 3857 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3858 | case eval_input: /* Built-in function input() */ |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3859 | com_node(c, CHILD(n, 0)); |
| 3860 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3861 | com_pop(c, 1); |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3862 | break; |
| 3863 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3864 | case lambdef: /* anonymous function definition */ |
| 3865 | compile_lambdef(c, n); |
| 3866 | break; |
| 3867 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3868 | case funcdef: /* A function definition */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3869 | compile_funcdef(c, n); |
| 3870 | break; |
| 3871 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3872 | case classdef: /* A class definition */ |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3873 | compile_classdef(c, n); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3874 | break; |
| 3875 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3876 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3877 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 3878 | "compile_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3879 | } |
| 3880 | } |
| 3881 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3882 | static PyObject * |
| 3883 | dict_keys_inorder(PyObject *dict, int offset) |
| 3884 | { |
| 3885 | PyObject *tuple, *k, *v; |
| 3886 | int i, pos = 0, size = PyDict_Size(dict); |
| 3887 | |
| 3888 | tuple = PyTuple_New(size); |
| 3889 | if (tuple == NULL) |
| 3890 | return NULL; |
| 3891 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 3892 | i = PyInt_AS_LONG(v); |
| 3893 | Py_INCREF(k); |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 3894 | assert((i - offset) < size); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3895 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 3896 | } |
| 3897 | return tuple; |
| 3898 | } |
| 3899 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3900 | PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3901 | PyNode_Compile(node *n, char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3902 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 3903 | return PyNode_CompileFlags(n, filename, NULL); |
| 3904 | } |
| 3905 | |
| 3906 | PyCodeObject * |
| 3907 | PyNode_CompileFlags(node *n, char *filename, PyCompilerFlags *flags) |
| 3908 | { |
| 3909 | return jcompile(n, filename, NULL, flags); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 3910 | } |
| 3911 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3912 | struct symtable * |
| 3913 | PyNode_CompileSymtable(node *n, char *filename) |
| 3914 | { |
| 3915 | struct symtable *st; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 3916 | PyFutureFeatures *ff; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3917 | |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 3918 | ff = PyNode_Future(n, filename); |
| 3919 | if (ff == NULL) |
| 3920 | return NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3921 | st = symtable_init(); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3922 | if (st == NULL) |
| 3923 | return NULL; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 3924 | st->st_future = ff; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3925 | symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 3926 | if (st->st_errors > 0) |
| 3927 | goto fail; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3928 | symtable_node(st, n); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 3929 | if (st->st_errors > 0) |
| 3930 | goto fail; |
| 3931 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3932 | return st; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 3933 | fail: |
| 3934 | PyMem_Free((void *)ff); |
| 3935 | st->st_future = NULL; |
| 3936 | PySymtable_Free(st); |
| 3937 | return NULL; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3938 | } |
| 3939 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3940 | static PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3941 | icompile(node *n, struct compiling *base) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 3942 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 3943 | return jcompile(n, base->c_filename, base, NULL); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 3944 | } |
| 3945 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3946 | static PyCodeObject * |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 3947 | jcompile(node *n, char *filename, struct compiling *base, |
| 3948 | PyCompilerFlags *flags) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 3949 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3950 | struct compiling sc; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3951 | PyCodeObject *co; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3952 | if (!com_init(&sc, filename)) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3953 | return NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3954 | if (base) { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 3955 | sc.c_private = base->c_private; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3956 | sc.c_symtable = base->c_symtable; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3957 | /* c_symtable still points to parent's symbols */ |
| 3958 | if (base->c_nested |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3959 | || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3960 | sc.c_nested = 1; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 3961 | sc.c_flags |= base->c_flags & (CO_GENERATOR_ALLOWED | |
| 3962 | CO_FUTURE_DIVISION); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3963 | } else { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 3964 | sc.c_private = NULL; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 3965 | sc.c_future = PyNode_Future(n, filename); |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 3966 | if (sc.c_future == NULL) { |
| 3967 | com_free(&sc); |
| 3968 | return NULL; |
| 3969 | } |
Jeremy Hylton | b857ba2 | 2001-08-10 21:41:33 +0000 | [diff] [blame] | 3970 | if (flags) |
| 3971 | sc.c_future->ff_features |= flags->cf_flags; |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 3972 | if (symtable_build(&sc, n) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3973 | com_free(&sc); |
| 3974 | return NULL; |
| 3975 | } |
| 3976 | } |
| 3977 | co = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3978 | if (symtable_load_symbols(&sc) < 0) { |
| 3979 | sc.c_errors++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3980 | goto exit; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3981 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3982 | compile_node(&sc, n); |
| 3983 | com_done(&sc); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3984 | if (sc.c_errors == 0) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3985 | PyObject *consts, *names, *varnames, *filename, *name, |
| 3986 | *freevars, *cellvars; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3987 | consts = PyList_AsTuple(sc.c_consts); |
| 3988 | names = PyList_AsTuple(sc.c_names); |
| 3989 | varnames = PyList_AsTuple(sc.c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3990 | cellvars = dict_keys_inorder(sc.c_cellvars, 0); |
| 3991 | freevars = dict_keys_inorder(sc.c_freevars, |
| 3992 | PyTuple_GET_SIZE(cellvars)); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 3993 | filename = PyString_InternFromString(sc.c_filename); |
| 3994 | name = PyString_InternFromString(sc.c_name); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3995 | if (!PyErr_Occurred()) |
| 3996 | co = PyCode_New(sc.c_argcount, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3997 | sc.c_nlocals, |
| 3998 | sc.c_maxstacklevel, |
| 3999 | sc.c_flags, |
| 4000 | sc.c_code, |
| 4001 | consts, |
| 4002 | names, |
| 4003 | varnames, |
| 4004 | freevars, |
| 4005 | cellvars, |
| 4006 | filename, |
| 4007 | name, |
| 4008 | sc.c_firstlineno, |
| 4009 | sc.c_lnotab); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4010 | Py_XDECREF(consts); |
| 4011 | Py_XDECREF(names); |
| 4012 | Py_XDECREF(varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4013 | Py_XDECREF(freevars); |
| 4014 | Py_XDECREF(cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4015 | Py_XDECREF(filename); |
| 4016 | Py_XDECREF(name); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 4017 | } |
Guido van Rossum | 6c2f0c7 | 2000-08-07 19:22:43 +0000 | [diff] [blame] | 4018 | else if (!PyErr_Occurred()) { |
| 4019 | /* This could happen if someone called PyErr_Clear() after an |
| 4020 | error was reported above. That's not supposed to happen, |
| 4021 | but I just plugged one case and I'm not sure there can't be |
| 4022 | others. In that case, raise SystemError so that at least |
| 4023 | it gets reported instead dumping core. */ |
| 4024 | PyErr_SetString(PyExc_SystemError, "lost syntax error"); |
| 4025 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4026 | exit: |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4027 | if (base == NULL) { |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4028 | PySymtable_Free(sc.c_symtable); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4029 | sc.c_symtable = NULL; |
| 4030 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4031 | com_free(&sc); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4032 | return co; |
| 4033 | } |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4034 | |
| 4035 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4036 | PyCode_Addr2Line(PyCodeObject *co, int addrq) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4037 | { |
| 4038 | int size = PyString_Size(co->co_lnotab) / 2; |
Guido van Rossum | 2174dcb | 1999-09-15 22:48:09 +0000 | [diff] [blame] | 4039 | unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4040 | int line = co->co_firstlineno; |
| 4041 | int addr = 0; |
| 4042 | while (--size >= 0) { |
| 4043 | addr += *p++; |
| 4044 | if (addr > addrq) |
| 4045 | break; |
| 4046 | line += *p++; |
| 4047 | } |
| 4048 | return line; |
| 4049 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4050 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4051 | /* The test for LOCAL must come before the test for FREE in order to |
| 4052 | handle classes where name is both local and free. The local var is |
| 4053 | a method and the free var is a free var referenced within a method. |
| 4054 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4055 | |
| 4056 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4057 | get_ref_type(struct compiling *c, char *name) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4058 | { |
| 4059 | PyObject *v; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4060 | if (c->c_symtable->st_nested_scopes) { |
| 4061 | if (PyDict_GetItemString(c->c_cellvars, name) != NULL) |
| 4062 | return CELL; |
| 4063 | if (PyDict_GetItemString(c->c_locals, name) != NULL) |
| 4064 | return LOCAL; |
| 4065 | if (PyDict_GetItemString(c->c_freevars, name) != NULL) |
| 4066 | return FREE; |
| 4067 | v = PyDict_GetItemString(c->c_globals, name); |
| 4068 | if (v) { |
| 4069 | if (v == Py_None) |
| 4070 | return GLOBAL_EXPLICIT; |
| 4071 | else { |
| 4072 | return GLOBAL_IMPLICIT; |
| 4073 | } |
| 4074 | } |
| 4075 | } else { |
| 4076 | if (PyDict_GetItemString(c->c_locals, name) != NULL) |
| 4077 | return LOCAL; |
| 4078 | v = PyDict_GetItemString(c->c_globals, name); |
| 4079 | if (v) { |
| 4080 | if (v == Py_None) |
| 4081 | return GLOBAL_EXPLICIT; |
| 4082 | else { |
| 4083 | return GLOBAL_IMPLICIT; |
| 4084 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4085 | } |
| 4086 | } |
| 4087 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4088 | char buf[350]; |
| 4089 | sprintf(buf, |
Jeremy Hylton | 5125773 | 2001-03-01 00:42:55 +0000 | [diff] [blame] | 4090 | "unknown scope for %.100s in %.100s(%s) " |
| 4091 | "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n", |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 4092 | name, c->c_name, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4093 | PyObject_REPR(c->c_symtable->st_cur->ste_id), |
Jeremy Hylton | 5125773 | 2001-03-01 00:42:55 +0000 | [diff] [blame] | 4094 | c->c_filename, |
| 4095 | PyObject_REPR(c->c_symtable->st_cur->ste_symbols), |
| 4096 | PyObject_REPR(c->c_locals), |
| 4097 | PyObject_REPR(c->c_globals) |
| 4098 | ); |
| 4099 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4100 | Py_FatalError(buf); |
| 4101 | } |
| 4102 | return -1; /* can't get here */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4103 | } |
| 4104 | |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4105 | /* Helper functions to issue warnings */ |
| 4106 | |
| 4107 | static int |
| 4108 | issue_warning(char *msg, char *filename, int lineno) |
| 4109 | { |
| 4110 | if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename, |
| 4111 | lineno, NULL, NULL) < 0) { |
| 4112 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
| 4113 | PyErr_SetString(PyExc_SyntaxError, msg); |
| 4114 | PyErr_SyntaxLocation(filename, lineno); |
| 4115 | } |
| 4116 | return -1; |
| 4117 | } |
| 4118 | return 0; |
| 4119 | } |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4120 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4121 | static int |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4122 | symtable_warn(struct symtable *st, char *msg) |
| 4123 | { |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4124 | 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] | 4125 | st->st_errors++; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4126 | return -1; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4127 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4128 | return 0; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4129 | } |
| 4130 | |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 4131 | /* Helper function for setting lineno and filename */ |
| 4132 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4133 | static int |
| 4134 | symtable_build(struct compiling *c, node *n) |
| 4135 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4136 | if ((c->c_symtable = symtable_init()) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4137 | return -1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 4138 | c->c_symtable->st_future = c->c_future; |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4139 | c->c_symtable->st_filename = c->c_filename; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4140 | symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno); |
| 4141 | if (c->c_symtable->st_errors > 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4142 | return -1; |
| 4143 | symtable_node(c->c_symtable, n); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4144 | if (c->c_symtable->st_errors > 0) |
| 4145 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4146 | /* reset for second pass */ |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4147 | c->c_symtable->st_nscopes = 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4148 | c->c_symtable->st_pass = 2; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4149 | return 0; |
| 4150 | } |
| 4151 | |
| 4152 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4153 | symtable_init_compiling_symbols(struct compiling *c) |
| 4154 | { |
| 4155 | PyObject *varnames; |
| 4156 | |
| 4157 | varnames = c->c_symtable->st_cur->ste_varnames; |
| 4158 | if (varnames == NULL) { |
| 4159 | varnames = PyList_New(0); |
| 4160 | if (varnames == NULL) |
| 4161 | return -1; |
| 4162 | c->c_symtable->st_cur->ste_varnames = varnames; |
| 4163 | Py_INCREF(varnames); |
| 4164 | } else |
| 4165 | Py_INCREF(varnames); |
| 4166 | c->c_varnames = varnames; |
| 4167 | |
| 4168 | c->c_globals = PyDict_New(); |
| 4169 | if (c->c_globals == NULL) |
| 4170 | return -1; |
| 4171 | c->c_freevars = PyDict_New(); |
| 4172 | if (c->c_freevars == NULL) |
| 4173 | return -1; |
| 4174 | c->c_cellvars = PyDict_New(); |
| 4175 | if (c->c_cellvars == NULL) |
| 4176 | return -1; |
| 4177 | return 0; |
| 4178 | } |
| 4179 | |
| 4180 | struct symbol_info { |
| 4181 | int si_nlocals; |
| 4182 | int si_ncells; |
| 4183 | int si_nfrees; |
| 4184 | int si_nimplicit; |
| 4185 | }; |
| 4186 | |
| 4187 | static void |
| 4188 | symtable_init_info(struct symbol_info *si) |
| 4189 | { |
| 4190 | si->si_nlocals = 0; |
| 4191 | si->si_ncells = 0; |
| 4192 | si->si_nfrees = 0; |
| 4193 | si->si_nimplicit = 0; |
| 4194 | } |
| 4195 | |
| 4196 | static int |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4197 | symtable_resolve_free(struct compiling *c, PyObject *name, int flags, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4198 | struct symbol_info *si) |
| 4199 | { |
| 4200 | PyObject *dict, *v; |
| 4201 | |
| 4202 | /* Seperate logic for DEF_FREE. If it occurs in a function, |
| 4203 | it indicates a local that we must allocate storage for (a |
| 4204 | cell var). If it occurs in a class, then the class has a |
| 4205 | method and a free variable with the same name. |
| 4206 | */ |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4207 | if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4208 | /* If it isn't declared locally, it can't be a cell. */ |
| 4209 | if (!(flags & (DEF_LOCAL | DEF_PARAM))) |
| 4210 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4211 | v = PyInt_FromLong(si->si_ncells++); |
| 4212 | dict = c->c_cellvars; |
| 4213 | } else { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4214 | /* If it is free anyway, then there is no need to do |
| 4215 | anything here. |
| 4216 | */ |
| 4217 | if (is_free(flags ^ DEF_FREE_CLASS) |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4218 | || (flags == DEF_FREE_CLASS)) |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4219 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4220 | v = PyInt_FromLong(si->si_nfrees++); |
| 4221 | dict = c->c_freevars; |
| 4222 | } |
| 4223 | if (v == NULL) |
| 4224 | return -1; |
| 4225 | if (PyDict_SetItem(dict, name, v) < 0) { |
| 4226 | Py_DECREF(v); |
| 4227 | return -1; |
| 4228 | } |
| 4229 | Py_DECREF(v); |
| 4230 | return 0; |
| 4231 | } |
| 4232 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4233 | /* If a variable is a cell and an argument, make sure that appears in |
| 4234 | co_cellvars before any variable to its right in varnames. |
| 4235 | */ |
| 4236 | |
| 4237 | |
| 4238 | static int |
| 4239 | symtable_cellvar_offsets(PyObject **cellvars, int argcount, |
| 4240 | PyObject *varnames, int flags) |
| 4241 | { |
| 4242 | PyObject *v, *w, *d, *list = NULL; |
| 4243 | int i, pos; |
| 4244 | |
| 4245 | if (flags & CO_VARARGS) |
| 4246 | argcount++; |
| 4247 | if (flags & CO_VARKEYWORDS) |
| 4248 | argcount++; |
| 4249 | for (i = argcount; --i >= 0; ) { |
| 4250 | v = PyList_GET_ITEM(varnames, i); |
| 4251 | if (PyDict_GetItem(*cellvars, v)) { |
| 4252 | if (list == NULL) { |
| 4253 | list = PyList_New(1); |
| 4254 | if (list == NULL) |
| 4255 | return -1; |
| 4256 | PyList_SET_ITEM(list, 0, v); |
| 4257 | Py_INCREF(v); |
| 4258 | } else |
| 4259 | PyList_Insert(list, 0, v); |
| 4260 | } |
| 4261 | } |
| 4262 | if (list == NULL || PyList_GET_SIZE(list) == 0) |
| 4263 | return 0; |
| 4264 | /* There are cellvars that are also arguments. Create a dict |
| 4265 | to replace cellvars and put the args at the front. |
| 4266 | */ |
| 4267 | d = PyDict_New(); |
| 4268 | for (i = PyList_GET_SIZE(list); --i >= 0; ) { |
| 4269 | v = PyInt_FromLong(i); |
| 4270 | if (v == NULL) |
| 4271 | goto fail; |
| 4272 | if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0) |
| 4273 | goto fail; |
| 4274 | if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0) |
| 4275 | goto fail; |
| 4276 | } |
| 4277 | pos = 0; |
| 4278 | i = PyList_GET_SIZE(list); |
| 4279 | Py_DECREF(list); |
| 4280 | while (PyDict_Next(*cellvars, &pos, &v, &w)) { |
| 4281 | w = PyInt_FromLong(i++); /* don't care about the old key */ |
| 4282 | if (PyDict_SetItem(d, v, w) < 0) { |
| 4283 | Py_DECREF(w); |
| 4284 | goto fail; |
| 4285 | } |
| 4286 | Py_DECREF(w); |
| 4287 | } |
| 4288 | Py_DECREF(*cellvars); |
| 4289 | *cellvars = d; |
| 4290 | return 1; |
| 4291 | fail: |
| 4292 | Py_DECREF(d); |
| 4293 | return -1; |
| 4294 | } |
| 4295 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4296 | static int |
| 4297 | symtable_freevar_offsets(PyObject *freevars, int offset) |
| 4298 | { |
| 4299 | PyObject *name, *v; |
| 4300 | int pos; |
| 4301 | |
| 4302 | /* The cell vars are the first elements of the closure, |
| 4303 | followed by the free vars. Update the offsets in |
| 4304 | c_freevars to account for number of cellvars. */ |
| 4305 | pos = 0; |
| 4306 | while (PyDict_Next(freevars, &pos, &name, &v)) { |
| 4307 | int i = PyInt_AS_LONG(v) + offset; |
| 4308 | PyObject *o = PyInt_FromLong(i); |
| 4309 | if (o == NULL) |
| 4310 | return -1; |
| 4311 | if (PyDict_SetItem(freevars, name, o) < 0) { |
| 4312 | Py_DECREF(o); |
| 4313 | return -1; |
| 4314 | } |
| 4315 | Py_DECREF(o); |
| 4316 | } |
| 4317 | return 0; |
| 4318 | } |
| 4319 | |
| 4320 | static int |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4321 | symtable_check_unoptimized(struct compiling *c, |
| 4322 | PySymtableEntryObject *ste, |
| 4323 | struct symbol_info *si) |
| 4324 | { |
| 4325 | char buf[300]; |
| 4326 | |
| 4327 | if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free |
| 4328 | || (ste->ste_nested && si->si_nimplicit))) |
| 4329 | return 0; |
| 4330 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4331 | #define ILLEGAL_CONTAINS "contains a nested function with free variables" |
| 4332 | |
| 4333 | #define ILLEGAL_IS "is a nested function" |
| 4334 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4335 | #define ILLEGAL_IMPORT_STAR \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4336 | "import * is not allowed in function '%.100s' because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4337 | |
| 4338 | #define ILLEGAL_BARE_EXEC \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4339 | "unqualified exec is not allowed in function '%.100s' it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4340 | |
| 4341 | #define ILLEGAL_EXEC_AND_IMPORT_STAR \ |
| 4342 | "function '%.100s' uses import * and bare exec, which are illegal" \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4343 | "because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4344 | |
| 4345 | /* XXX perhaps the linenos for these opt-breaking statements |
| 4346 | should be stored so the exception can point to them. */ |
| 4347 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4348 | if (ste->ste_child_free) { |
| 4349 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
| 4350 | sprintf(buf, ILLEGAL_IMPORT_STAR, |
| 4351 | PyString_AS_STRING(ste->ste_name), |
| 4352 | ILLEGAL_CONTAINS); |
| 4353 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
| 4354 | sprintf(buf, ILLEGAL_BARE_EXEC, |
| 4355 | PyString_AS_STRING(ste->ste_name), |
| 4356 | ILLEGAL_CONTAINS); |
| 4357 | else { |
| 4358 | sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 4359 | PyString_AS_STRING(ste->ste_name), |
| 4360 | ILLEGAL_CONTAINS); |
| 4361 | } |
| 4362 | } else { |
| 4363 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
| 4364 | sprintf(buf, ILLEGAL_IMPORT_STAR, |
| 4365 | PyString_AS_STRING(ste->ste_name), |
| 4366 | ILLEGAL_IS); |
| 4367 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
| 4368 | sprintf(buf, ILLEGAL_BARE_EXEC, |
| 4369 | PyString_AS_STRING(ste->ste_name), |
| 4370 | ILLEGAL_IS); |
| 4371 | else { |
| 4372 | sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 4373 | PyString_AS_STRING(ste->ste_name), |
| 4374 | ILLEGAL_IS); |
| 4375 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4376 | } |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4377 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4378 | if (c->c_symtable->st_nested_scopes) { |
| 4379 | PyErr_SetString(PyExc_SyntaxError, buf); |
| 4380 | PyErr_SyntaxLocation(c->c_symtable->st_filename, |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 4381 | ste->ste_opt_lineno); |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4382 | return -1; |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4383 | } |
| 4384 | else { |
| 4385 | return issue_warning(buf, c->c_filename, ste->ste_lineno); |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4386 | } |
| 4387 | return 0; |
| 4388 | } |
| 4389 | |
| 4390 | static int |
| 4391 | symtable_check_shadow(struct symtable *st, PyObject *name, int flags) |
| 4392 | { |
| 4393 | char buf[500]; |
| 4394 | PyObject *children, *v; |
Jeremy Hylton | c176132 | 2001-02-28 23:44:45 +0000 | [diff] [blame] | 4395 | PySymtableEntryObject *child = NULL; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4396 | int i; |
| 4397 | |
| 4398 | if (!(flags & DEF_BOUND)) |
| 4399 | return 0; |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4400 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4401 | /* The semantics of this code will change with nested scopes. |
| 4402 | It is defined in the current scope and referenced in a |
| 4403 | child scope. Under the old rules, the child will see a |
| 4404 | global. Under the new rules, the child will see the |
| 4405 | binding in the current scope. |
| 4406 | */ |
| 4407 | |
| 4408 | /* Find name of child function that has free variable */ |
| 4409 | children = st->st_cur->ste_children; |
| 4410 | for (i = 0; i < PyList_GET_SIZE(children); i++) { |
| 4411 | int cflags; |
| 4412 | child = (PySymtableEntryObject *)PyList_GET_ITEM(children, i); |
| 4413 | v = PyDict_GetItem(child->ste_symbols, name); |
| 4414 | if (v == NULL) |
| 4415 | continue; |
| 4416 | cflags = PyInt_AS_LONG(v); |
| 4417 | if (!(cflags & DEF_BOUND)) |
| 4418 | break; |
| 4419 | } |
Jeremy Hylton | c176132 | 2001-02-28 23:44:45 +0000 | [diff] [blame] | 4420 | |
| 4421 | assert(child != NULL); |
| 4422 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4423 | sprintf(buf, "local name '%.100s' in '%.100s' shadows " |
| 4424 | "use of '%.100s' as global in nested scope '%.100s'", |
| 4425 | PyString_AS_STRING(name), |
| 4426 | PyString_AS_STRING(st->st_cur->ste_name), |
| 4427 | PyString_AS_STRING(name), |
| 4428 | PyString_AS_STRING(child->ste_name) |
| 4429 | ); |
| 4430 | |
| 4431 | return symtable_warn(st, buf); |
| 4432 | } |
| 4433 | |
| 4434 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4435 | symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste, |
| 4436 | struct symbol_info *si) |
| 4437 | { |
Jeremy Hylton | b857ba2 | 2001-08-10 21:41:33 +0000 | [diff] [blame] | 4438 | if (c->c_future) |
| 4439 | c->c_flags |= c->c_future->ff_features; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4440 | if (ste->ste_generator) |
| 4441 | c->c_flags |= CO_GENERATOR; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4442 | if (ste->ste_type != TYPE_MODULE) |
| 4443 | c->c_flags |= CO_NEWLOCALS; |
| 4444 | if (ste->ste_type == TYPE_FUNCTION) { |
| 4445 | c->c_nlocals = si->si_nlocals; |
| 4446 | if (ste->ste_optimized == 0) |
| 4447 | c->c_flags |= CO_OPTIMIZED; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4448 | else if (ste->ste_optimized != OPT_EXEC) |
| 4449 | return symtable_check_unoptimized(c, ste, si); |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4450 | } |
| 4451 | return 0; |
| 4452 | } |
| 4453 | |
| 4454 | static int |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4455 | symtable_load_symbols(struct compiling *c) |
| 4456 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4457 | static PyObject *implicit = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4458 | struct symtable *st = c->c_symtable; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4459 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4460 | PyObject *name, *varnames, *v; |
| 4461 | int i, flags, pos; |
| 4462 | struct symbol_info si; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4463 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4464 | if (implicit == NULL) { |
| 4465 | implicit = PyInt_FromLong(1); |
| 4466 | if (implicit == NULL) |
| 4467 | return -1; |
| 4468 | } |
| 4469 | v = NULL; |
| 4470 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4471 | if (symtable_init_compiling_symbols(c) < 0) |
| 4472 | goto fail; |
| 4473 | symtable_init_info(&si); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4474 | varnames = st->st_cur->ste_varnames; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4475 | si.si_nlocals = PyList_GET_SIZE(varnames); |
| 4476 | c->c_argcount = si.si_nlocals; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4477 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4478 | for (i = 0; i < si.si_nlocals; ++i) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4479 | v = PyInt_FromLong(i); |
| 4480 | if (PyDict_SetItem(c->c_locals, |
| 4481 | PyList_GET_ITEM(varnames, i), v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4482 | goto fail; |
| 4483 | Py_DECREF(v); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4484 | } |
| 4485 | |
| 4486 | /* XXX The cases below define the rules for whether a name is |
| 4487 | local or global. The logic could probably be clearer. */ |
| 4488 | pos = 0; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4489 | while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) { |
| 4490 | flags = PyInt_AS_LONG(v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4491 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4492 | if (st->st_nested_scopes == 0 |
| 4493 | && (flags & (DEF_FREE | DEF_FREE_CLASS))) { |
| 4494 | if (symtable_check_shadow(st, name, flags) < 0) |
| 4495 | goto fail; |
| 4496 | } |
| 4497 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4498 | if (flags & DEF_FREE_GLOBAL) |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4499 | /* undo the original DEF_FREE */ |
| 4500 | flags &= ~(DEF_FREE | DEF_FREE_CLASS); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4501 | |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4502 | /* Deal with names that need two actions: |
| 4503 | 1. Cell variables, which are also locals. |
| 4504 | 2. Free variables in methods that are also class |
| 4505 | variables or declared global. |
| 4506 | */ |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4507 | if (st->st_nested_scopes) { |
| 4508 | if (flags & (DEF_FREE | DEF_FREE_CLASS)) { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4509 | symtable_resolve_free(c, name, flags, &si); |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4510 | } |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4511 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4512 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4513 | if (flags & DEF_STAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4514 | c->c_argcount--; |
| 4515 | c->c_flags |= CO_VARARGS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4516 | } else if (flags & DEF_DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4517 | c->c_argcount--; |
| 4518 | c->c_flags |= CO_VARKEYWORDS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4519 | } else if (flags & DEF_INTUPLE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4520 | c->c_argcount--; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4521 | else if (flags & DEF_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4522 | if (flags & DEF_PARAM) { |
| 4523 | PyErr_Format(PyExc_SyntaxError, LOCAL_GLOBAL, |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4524 | PyString_AS_STRING(name)); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 4525 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4526 | ste->ste_lineno); |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4527 | st->st_errors++; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4528 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4529 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4530 | if (PyDict_SetItem(c->c_globals, name, Py_None) < 0) |
| 4531 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4532 | } else if (flags & DEF_FREE_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4533 | si.si_nimplicit++; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4534 | if (PyDict_SetItem(c->c_globals, name, implicit) < 0) |
| 4535 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4536 | } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4537 | v = PyInt_FromLong(si.si_nlocals++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4538 | if (v == NULL) |
| 4539 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4540 | if (PyDict_SetItem(c->c_locals, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4541 | goto fail; |
| 4542 | Py_DECREF(v); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4543 | if (ste->ste_type != TYPE_CLASS) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4544 | if (PyList_Append(c->c_varnames, name) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4545 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4546 | } else if (is_free(flags)) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4547 | if (ste->ste_nested && st->st_nested_scopes) { |
| 4548 | v = PyInt_FromLong(si.si_nfrees++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4549 | if (v == NULL) |
| 4550 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4551 | if (PyDict_SetItem(c->c_freevars, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4552 | goto fail; |
| 4553 | Py_DECREF(v); |
| 4554 | } else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4555 | si.si_nimplicit++; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4556 | if (PyDict_SetItem(c->c_globals, name, |
| 4557 | implicit) < 0) |
| 4558 | goto fail; |
Jeremy Hylton | 5125773 | 2001-03-01 00:42:55 +0000 | [diff] [blame] | 4559 | if (st->st_nscopes != 1) { |
| 4560 | v = PyInt_FromLong(flags); |
| 4561 | if (PyDict_SetItem(st->st_global, |
| 4562 | name, v)) |
| 4563 | goto fail; |
| 4564 | Py_DECREF(v); |
| 4565 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4566 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4567 | } |
| 4568 | } |
| 4569 | |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4570 | assert(PyDict_Size(c->c_freevars) == si.si_nfrees); |
| 4571 | |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4572 | if (st->st_nested_scopes == 0) |
| 4573 | assert(si.si_nfrees == 0); |
| 4574 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4575 | if (si.si_ncells > 1) { /* one cell is always in order */ |
| 4576 | if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount, |
| 4577 | c->c_varnames, c->c_flags) < 0) |
| 4578 | return -1; |
| 4579 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4580 | if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0) |
| 4581 | return -1; |
| 4582 | return symtable_update_flags(c, ste, &si); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4583 | fail: |
| 4584 | /* is this always the right thing to do? */ |
| 4585 | Py_XDECREF(v); |
| 4586 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4587 | } |
| 4588 | |
| 4589 | static struct symtable * |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4590 | symtable_init() |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4591 | { |
| 4592 | struct symtable *st; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4593 | |
| 4594 | st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable)); |
| 4595 | if (st == NULL) |
| 4596 | return NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4597 | st->st_pass = 1; |
Tim Peters | ff1f852 | 2001-08-11 01:06:35 +0000 | [diff] [blame^] | 4598 | |
| 4599 | /* XXX Tim: Jeremy deleted the next line and everything went to hell. |
| 4600 | XXX It should probably get fixed by getting rid of st_nested_scopes |
| 4601 | XXX entirely. */ |
| 4602 | st->st_nested_scopes = 1; |
| 4603 | |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4604 | st->st_filename = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4605 | if ((st->st_stack = PyList_New(0)) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4606 | goto fail; |
| 4607 | if ((st->st_symbols = PyDict_New()) == NULL) |
| 4608 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4609 | st->st_cur = NULL; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4610 | st->st_nscopes = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4611 | st->st_errors = 0; |
| 4612 | st->st_tmpname = 0; |
| 4613 | st->st_private = NULL; |
| 4614 | return st; |
| 4615 | fail: |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4616 | PySymtable_Free(st); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4617 | return NULL; |
| 4618 | } |
| 4619 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4620 | void |
| 4621 | PySymtable_Free(struct symtable *st) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4622 | { |
| 4623 | Py_XDECREF(st->st_symbols); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4624 | Py_XDECREF(st->st_stack); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4625 | Py_XDECREF(st->st_cur); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4626 | PyMem_Free((void *)st); |
| 4627 | } |
| 4628 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4629 | /* When the compiler exits a scope, it must should update the scope's |
| 4630 | free variable information with the list of free variables in its |
| 4631 | children. |
| 4632 | |
| 4633 | Variables that are free in children and defined in the current |
| 4634 | scope are cellvars. |
| 4635 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4636 | 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] | 4637 | false), free variables in children that are not defined here are |
| 4638 | implicit globals. |
| 4639 | |
| 4640 | */ |
| 4641 | |
| 4642 | static int |
| 4643 | symtable_update_free_vars(struct symtable *st) |
| 4644 | { |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4645 | int i, j, def; |
| 4646 | PyObject *o, *name, *list = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4647 | PySymtableEntryObject *child, *ste = st->st_cur; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4648 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4649 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4650 | def = DEF_FREE_CLASS; |
| 4651 | else |
| 4652 | def = DEF_FREE; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4653 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4654 | int pos = 0; |
| 4655 | |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4656 | if (list) |
| 4657 | PyList_SetSlice(list, 0, |
| 4658 | ((PyVarObject*)list)->ob_size, 0); |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4659 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4660 | PyList_GET_ITEM(ste->ste_children, i); |
| 4661 | while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4662 | int flags = PyInt_AS_LONG(o); |
| 4663 | if (!(is_free(flags))) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4664 | continue; /* avoids indentation */ |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4665 | if (list == NULL) { |
| 4666 | list = PyList_New(0); |
| 4667 | if (list == NULL) |
| 4668 | return -1; |
| 4669 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4670 | ste->ste_child_free = 1; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4671 | if (PyList_Append(list, name) < 0) { |
| 4672 | Py_DECREF(list); |
| 4673 | return -1; |
| 4674 | } |
| 4675 | } |
| 4676 | for (j = 0; list && j < PyList_GET_SIZE(list); j++) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4677 | PyObject *v; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4678 | name = PyList_GET_ITEM(list, j); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4679 | v = PyDict_GetItem(ste->ste_symbols, name); |
| 4680 | /* If a name N is declared global in scope A and |
| 4681 | referenced in scope B contained (perhaps |
| 4682 | indirectly) in A and there are no scopes |
| 4683 | with bindings for N between B and A, then N |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4684 | is global in B. Unless A is a class scope, |
| 4685 | because class scopes are not considered for |
| 4686 | nested scopes. |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4687 | */ |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4688 | if (v && (ste->ste_type != TYPE_CLASS)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4689 | int flags = PyInt_AS_LONG(v); |
| 4690 | if (flags & DEF_GLOBAL) { |
| 4691 | symtable_undo_free(st, child->ste_id, |
| 4692 | name); |
| 4693 | continue; |
| 4694 | } |
| 4695 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4696 | if (ste->ste_nested) { |
| 4697 | if (symtable_add_def_o(st, ste->ste_symbols, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4698 | name, def) < 0) { |
| 4699 | Py_DECREF(list); |
| 4700 | return -1; |
| 4701 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4702 | } else { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4703 | if (symtable_check_global(st, child->ste_id, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4704 | name) < 0) { |
| 4705 | Py_DECREF(list); |
| 4706 | return -1; |
| 4707 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4708 | } |
| 4709 | } |
| 4710 | } |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4711 | |
| 4712 | Py_XDECREF(list); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4713 | return 0; |
| 4714 | } |
| 4715 | |
| 4716 | /* If the current scope is a non-nested class or if name is not |
| 4717 | defined in the current, non-nested scope, then it is an implicit |
| 4718 | global in all nested scopes. |
| 4719 | */ |
| 4720 | |
| 4721 | static int |
| 4722 | symtable_check_global(struct symtable *st, PyObject *child, PyObject *name) |
| 4723 | { |
| 4724 | PyObject *o; |
| 4725 | int v; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4726 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4727 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4728 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4729 | return symtable_undo_free(st, child, name); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4730 | o = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4731 | if (o == NULL) |
| 4732 | return symtable_undo_free(st, child, name); |
| 4733 | v = PyInt_AS_LONG(o); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4734 | |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4735 | if (is_free(v) || (v & DEF_GLOBAL)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4736 | return symtable_undo_free(st, child, name); |
| 4737 | else |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4738 | return symtable_add_def_o(st, ste->ste_symbols, |
| 4739 | name, DEF_FREE); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4740 | } |
| 4741 | |
| 4742 | static int |
| 4743 | symtable_undo_free(struct symtable *st, PyObject *id, |
| 4744 | PyObject *name) |
| 4745 | { |
| 4746 | int i, v, x; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4747 | PyObject *info; |
| 4748 | PySymtableEntryObject *ste; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4749 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4750 | ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id); |
| 4751 | if (ste == NULL) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4752 | return -1; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4753 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4754 | info = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4755 | if (info == NULL) |
| 4756 | return 0; |
| 4757 | v = PyInt_AS_LONG(info); |
| 4758 | if (is_free(v)) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4759 | if (symtable_add_def_o(st, ste->ste_symbols, name, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4760 | DEF_FREE_GLOBAL) < 0) |
| 4761 | return -1; |
| 4762 | } else |
| 4763 | /* If the name is defined here or declared global, |
| 4764 | then the recursion stops. */ |
| 4765 | return 0; |
| 4766 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4767 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
| 4768 | PySymtableEntryObject *child; |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4769 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4770 | PyList_GET_ITEM(ste->ste_children, i); |
| 4771 | x = symtable_undo_free(st, child->ste_id, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4772 | if (x < 0) |
| 4773 | return x; |
| 4774 | } |
| 4775 | return 0; |
| 4776 | } |
| 4777 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4778 | /* symtable_enter_scope() gets a reference via PySymtableEntry_New(). |
| 4779 | This reference is released when the scope is exited, via the DECREF |
| 4780 | in symtable_exit_scope(). |
| 4781 | */ |
| 4782 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4783 | static int |
| 4784 | symtable_exit_scope(struct symtable *st) |
| 4785 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4786 | int end; |
| 4787 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4788 | if (st->st_pass == 1) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4789 | symtable_update_free_vars(st); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4790 | Py_DECREF(st->st_cur); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4791 | end = PyList_GET_SIZE(st->st_stack) - 1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4792 | st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack, |
| 4793 | end); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4794 | if (PySequence_DelItem(st->st_stack, end) < 0) |
| 4795 | return -1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4796 | return 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4797 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4798 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4799 | static void |
| 4800 | symtable_enter_scope(struct symtable *st, char *name, int type, |
| 4801 | int lineno) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4802 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4803 | PySymtableEntryObject *prev = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4804 | |
| 4805 | if (st->st_cur) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4806 | prev = st->st_cur; |
| 4807 | if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) { |
| 4808 | Py_DECREF(st->st_cur); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4809 | st->st_errors++; |
| 4810 | return; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4811 | } |
| 4812 | } |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4813 | st->st_cur = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4814 | PySymtableEntry_New(st, name, type, lineno); |
| 4815 | if (strcmp(name, TOP) == 0) |
| 4816 | st->st_global = st->st_cur->ste_symbols; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4817 | if (prev && st->st_pass == 1) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4818 | if (PyList_Append(prev->ste_children, |
| 4819 | (PyObject *)st->st_cur) < 0) |
| 4820 | st->st_errors++; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4821 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4822 | } |
| 4823 | |
| 4824 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4825 | symtable_lookup(struct symtable *st, char *name) |
| 4826 | { |
| 4827 | char buffer[MANGLE_LEN]; |
| 4828 | PyObject *v; |
| 4829 | int flags; |
| 4830 | |
| 4831 | if (mangle(st->st_private, name, buffer, sizeof(buffer))) |
| 4832 | name = buffer; |
| 4833 | v = PyDict_GetItemString(st->st_cur->ste_symbols, name); |
| 4834 | if (v == NULL) { |
| 4835 | if (PyErr_Occurred()) |
| 4836 | return -1; |
| 4837 | else |
| 4838 | return 0; |
| 4839 | } |
| 4840 | |
| 4841 | flags = PyInt_AS_LONG(v); |
| 4842 | return flags; |
| 4843 | } |
| 4844 | |
| 4845 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4846 | symtable_add_def(struct symtable *st, char *name, int flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4847 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4848 | PyObject *s; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4849 | char buffer[MANGLE_LEN]; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 4850 | int ret; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4851 | |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4852 | if (mangle(st->st_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4853 | name = buffer; |
| 4854 | if ((s = PyString_InternFromString(name)) == NULL) |
| 4855 | return -1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 4856 | ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag); |
| 4857 | Py_DECREF(s); |
| 4858 | return ret; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4859 | } |
| 4860 | |
| 4861 | /* Must only be called with mangled names */ |
| 4862 | |
| 4863 | static int |
| 4864 | symtable_add_def_o(struct symtable *st, PyObject *dict, |
| 4865 | PyObject *name, int flag) |
| 4866 | { |
| 4867 | PyObject *o; |
| 4868 | int val; |
| 4869 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4870 | if ((o = PyDict_GetItem(dict, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4871 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4872 | if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4873 | PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 4874 | PyString_AsString(name)); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 4875 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4876 | st->st_cur->ste_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4877 | return -1; |
| 4878 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4879 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4880 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4881 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4882 | o = PyInt_FromLong(val); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4883 | if (PyDict_SetItem(dict, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4884 | Py_DECREF(o); |
| 4885 | return -1; |
| 4886 | } |
| 4887 | Py_DECREF(o); |
| 4888 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4889 | if (flag & DEF_PARAM) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4890 | if (PyList_Append(st->st_cur->ste_varnames, name) < 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4891 | return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4892 | } else if (flag & DEF_GLOBAL) { |
| 4893 | /* XXX need to update DEF_GLOBAL for other flags too; |
| 4894 | perhaps only DEF_FREE_GLOBAL */ |
| 4895 | if ((o = PyDict_GetItem(st->st_global, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4896 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4897 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4898 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4899 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4900 | o = PyInt_FromLong(val); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4901 | if (PyDict_SetItem(st->st_global, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4902 | Py_DECREF(o); |
| 4903 | return -1; |
| 4904 | } |
| 4905 | Py_DECREF(o); |
| 4906 | } |
| 4907 | return 0; |
| 4908 | } |
| 4909 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4910 | #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4911 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 4912 | /* Look for a yield stmt under n. Return 1 if found, else 0. |
| 4913 | This hack is used to look inside "if 0:" blocks (which are normally |
| 4914 | ignored) in case those are the only places a yield occurs (so that this |
| 4915 | function is a generator). */ |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 4916 | static int |
| 4917 | look_for_yield(node *n) |
| 4918 | { |
| 4919 | int i; |
| 4920 | |
| 4921 | for (i = 0; i < NCH(n); ++i) { |
| 4922 | node *kid = CHILD(n, i); |
| 4923 | |
| 4924 | switch (TYPE(kid)) { |
| 4925 | |
| 4926 | case classdef: |
| 4927 | case funcdef: |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 4928 | case lambdef: |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 4929 | /* Stuff in nested functions and classes can't make |
| 4930 | the parent a generator. */ |
| 4931 | return 0; |
| 4932 | |
| 4933 | case yield_stmt: |
| 4934 | return 1; |
| 4935 | |
| 4936 | default: |
| 4937 | if (look_for_yield(kid)) |
| 4938 | return 1; |
| 4939 | } |
| 4940 | } |
| 4941 | return 0; |
| 4942 | } |
| 4943 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4944 | static void |
| 4945 | symtable_node(struct symtable *st, node *n) |
| 4946 | { |
| 4947 | int i, start = 0; |
| 4948 | |
| 4949 | loop: |
| 4950 | switch (TYPE(n)) { |
| 4951 | case funcdef: { |
| 4952 | char *func_name = STR(CHILD(n, 1)); |
| 4953 | symtable_add_def(st, func_name, DEF_LOCAL); |
| 4954 | symtable_default_args(st, CHILD(n, 2)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4955 | symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4956 | symtable_funcdef(st, n); |
| 4957 | symtable_exit_scope(st); |
| 4958 | break; |
| 4959 | } |
| 4960 | case lambdef: |
| 4961 | if (NCH(n) == 4) |
| 4962 | symtable_default_args(st, CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4963 | symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4964 | symtable_funcdef(st, n); |
| 4965 | symtable_exit_scope(st); |
| 4966 | break; |
| 4967 | case classdef: { |
| 4968 | char *tmp, *class_name = STR(CHILD(n, 1)); |
| 4969 | symtable_add_def(st, class_name, DEF_LOCAL); |
| 4970 | if (TYPE(CHILD(n, 2)) == LPAR) { |
| 4971 | node *bases = CHILD(n, 3); |
| 4972 | int i; |
| 4973 | for (i = 0; i < NCH(bases); i += 2) { |
| 4974 | symtable_node(st, CHILD(bases, i)); |
| 4975 | } |
| 4976 | } |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4977 | symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4978 | tmp = st->st_private; |
| 4979 | st->st_private = class_name; |
| 4980 | symtable_node(st, CHILD(n, NCH(n) - 1)); |
| 4981 | st->st_private = tmp; |
| 4982 | symtable_exit_scope(st); |
| 4983 | break; |
| 4984 | } |
| 4985 | case if_stmt: |
| 4986 | for (i = 0; i + 3 < NCH(n); i += 4) { |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 4987 | if (is_constant_false(NULL, (CHILD(n, i + 1)))) { |
| 4988 | if (st->st_cur->ste_generator == 0) |
| 4989 | st->st_cur->ste_generator = |
| 4990 | look_for_yield(CHILD(n, i+3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4991 | continue; |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 4992 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4993 | symtable_node(st, CHILD(n, i + 1)); |
| 4994 | symtable_node(st, CHILD(n, i + 3)); |
| 4995 | } |
| 4996 | if (i + 2 < NCH(n)) |
| 4997 | symtable_node(st, CHILD(n, i + 2)); |
| 4998 | break; |
| 4999 | case global_stmt: |
| 5000 | symtable_global(st, n); |
| 5001 | break; |
| 5002 | case import_stmt: |
| 5003 | symtable_import(st, n); |
| 5004 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5005 | case exec_stmt: { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5006 | st->st_cur->ste_optimized |= OPT_EXEC; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5007 | symtable_node(st, CHILD(n, 1)); |
| 5008 | if (NCH(n) > 2) |
| 5009 | symtable_node(st, CHILD(n, 3)); |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5010 | else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5011 | st->st_cur->ste_optimized |= OPT_BARE_EXEC; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5012 | st->st_cur->ste_opt_lineno = n->n_lineno; |
| 5013 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5014 | if (NCH(n) > 4) |
| 5015 | symtable_node(st, CHILD(n, 5)); |
| 5016 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5017 | |
| 5018 | } |
Jeremy Hylton | 384639f | 2001-02-19 15:50:51 +0000 | [diff] [blame] | 5019 | case assert_stmt: |
| 5020 | if (Py_OptimizeFlag) |
| 5021 | return; |
| 5022 | if (NCH(n) == 2) { |
| 5023 | n = CHILD(n, 1); |
| 5024 | goto loop; |
| 5025 | } else { |
| 5026 | symtable_node(st, CHILD(n, 1)); |
| 5027 | n = CHILD(n, 3); |
| 5028 | goto loop; |
| 5029 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5030 | case except_clause: |
| 5031 | if (NCH(n) == 4) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5032 | symtable_assign(st, CHILD(n, 3), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5033 | if (NCH(n) > 1) { |
| 5034 | n = CHILD(n, 1); |
| 5035 | goto loop; |
| 5036 | } |
| 5037 | break; |
| 5038 | case del_stmt: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5039 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5040 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 5041 | case yield_stmt: |
| 5042 | st->st_cur->ste_generator = 1; |
| 5043 | n = CHILD(n, 1); |
| 5044 | goto loop; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5045 | case expr_stmt: |
| 5046 | if (NCH(n) == 1) |
| 5047 | n = CHILD(n, 0); |
| 5048 | else { |
| 5049 | if (TYPE(CHILD(n, 1)) == augassign) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5050 | symtable_assign(st, CHILD(n, 0), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5051 | symtable_node(st, CHILD(n, 2)); |
| 5052 | break; |
| 5053 | } else { |
| 5054 | int i; |
| 5055 | for (i = 0; i < NCH(n) - 2; i += 2) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5056 | symtable_assign(st, CHILD(n, i), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5057 | n = CHILD(n, NCH(n) - 1); |
| 5058 | } |
| 5059 | } |
| 5060 | goto loop; |
| 5061 | /* watchout for fall-through logic below */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5062 | case argument: |
| 5063 | if (NCH(n) == 3) { |
| 5064 | n = CHILD(n, 2); |
| 5065 | goto loop; |
| 5066 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5067 | case listmaker: |
| 5068 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5069 | st->st_tmpname++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5070 | symtable_list_comprehension(st, CHILD(n, 1)); |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5071 | symtable_node(st, CHILD(n, 0)); |
| 5072 | st->st_tmpname--; |
| 5073 | return; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5074 | } |
| 5075 | case atom: |
| 5076 | if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) { |
| 5077 | symtable_add_use(st, STR(CHILD(n, 0))); |
| 5078 | break; |
| 5079 | } |
| 5080 | case for_stmt: |
| 5081 | if (TYPE(n) == for_stmt) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5082 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5083 | start = 3; |
| 5084 | } |
| 5085 | default: |
| 5086 | if (NCH(n) == 1) { |
| 5087 | n = CHILD(n, 0); |
| 5088 | goto loop; |
| 5089 | } |
| 5090 | for (i = start; i < NCH(n); ++i) |
| 5091 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5092 | symtable_node(st, CHILD(n, i)); |
| 5093 | } |
| 5094 | } |
| 5095 | |
| 5096 | static void |
| 5097 | symtable_funcdef(struct symtable *st, node *n) |
| 5098 | { |
| 5099 | node *body; |
| 5100 | |
| 5101 | if (TYPE(n) == lambdef) { |
| 5102 | if (NCH(n) == 4) |
| 5103 | symtable_params(st, CHILD(n, 1)); |
| 5104 | } else |
| 5105 | symtable_params(st, CHILD(n, 2)); |
| 5106 | body = CHILD(n, NCH(n) - 1); |
| 5107 | symtable_node(st, body); |
| 5108 | } |
| 5109 | |
| 5110 | /* The next two functions parse the argument tuple. |
| 5111 | symtable_default_arg() checks for names in the default arguments, |
| 5112 | which are references in the defining scope. symtable_params() |
| 5113 | parses the parameter names, which are defined in the function's |
| 5114 | body. |
| 5115 | |
| 5116 | varargslist: |
| 5117 | (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) |
| 5118 | | fpdef ['=' test] (',' fpdef ['=' test])* [','] |
| 5119 | */ |
| 5120 | |
| 5121 | static void |
| 5122 | symtable_default_args(struct symtable *st, node *n) |
| 5123 | { |
| 5124 | node *c; |
| 5125 | int i; |
| 5126 | |
| 5127 | if (TYPE(n) == parameters) { |
| 5128 | n = CHILD(n, 1); |
| 5129 | if (TYPE(n) == RPAR) |
| 5130 | return; |
| 5131 | } |
| 5132 | REQ(n, varargslist); |
| 5133 | for (i = 0; i < NCH(n); i += 2) { |
| 5134 | c = CHILD(n, i); |
| 5135 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5136 | break; |
| 5137 | } |
| 5138 | if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL)) |
| 5139 | symtable_node(st, CHILD(n, i)); |
| 5140 | } |
| 5141 | } |
| 5142 | |
| 5143 | static void |
| 5144 | symtable_params(struct symtable *st, node *n) |
| 5145 | { |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5146 | int i, complex = -1, ext = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5147 | node *c = NULL; |
| 5148 | |
| 5149 | if (TYPE(n) == parameters) { |
| 5150 | n = CHILD(n, 1); |
| 5151 | if (TYPE(n) == RPAR) |
| 5152 | return; |
| 5153 | } |
| 5154 | REQ(n, varargslist); |
| 5155 | for (i = 0; i < NCH(n); i += 2) { |
| 5156 | c = CHILD(n, i); |
| 5157 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5158 | ext = 1; |
| 5159 | break; |
| 5160 | } |
| 5161 | if (TYPE(c) == test) { |
| 5162 | continue; |
| 5163 | } |
| 5164 | if (TYPE(CHILD(c, 0)) == NAME) |
| 5165 | symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM); |
| 5166 | else { |
| 5167 | char nbuf[10]; |
| 5168 | sprintf(nbuf, ".%d", i); |
| 5169 | symtable_add_def(st, nbuf, DEF_PARAM); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5170 | complex = i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5171 | } |
| 5172 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5173 | if (ext) { |
| 5174 | c = CHILD(n, i); |
| 5175 | if (TYPE(c) == STAR) { |
| 5176 | i++; |
| 5177 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5178 | DEF_PARAM | DEF_STAR); |
| 5179 | i += 2; |
Jeremy Hylton | 1113cfc | 2001-01-23 00:50:52 +0000 | [diff] [blame] | 5180 | if (i >= NCH(n)) |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5181 | c = NULL; |
| 5182 | else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5183 | c = CHILD(n, i); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5184 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5185 | if (c && TYPE(c) == DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5186 | i++; |
| 5187 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5188 | DEF_PARAM | DEF_DOUBLESTAR); |
| 5189 | } |
| 5190 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5191 | if (complex >= 0) { |
| 5192 | int j; |
| 5193 | for (j = 0; j <= complex; j++) { |
| 5194 | c = CHILD(n, j); |
| 5195 | if (TYPE(c) == COMMA) |
Jeremy Hylton | 2b3f0ca | 2001-02-19 23:52:49 +0000 | [diff] [blame] | 5196 | c = CHILD(n, ++j); |
| 5197 | else if (TYPE(c) == EQUAL) |
| 5198 | c = CHILD(n, j += 3); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5199 | if (TYPE(CHILD(c, 0)) == LPAR) |
| 5200 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5201 | } |
| 5202 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5203 | } |
| 5204 | |
| 5205 | static void |
| 5206 | symtable_params_fplist(struct symtable *st, node *n) |
| 5207 | { |
| 5208 | int i; |
| 5209 | node *c; |
| 5210 | |
| 5211 | REQ(n, fplist); |
| 5212 | for (i = 0; i < NCH(n); i += 2) { |
| 5213 | c = CHILD(n, i); |
| 5214 | REQ(c, fpdef); |
| 5215 | if (NCH(c) == 1) |
| 5216 | symtable_add_def(st, STR(CHILD(c, 0)), |
| 5217 | DEF_PARAM | DEF_INTUPLE); |
| 5218 | else |
| 5219 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5220 | } |
| 5221 | |
| 5222 | } |
| 5223 | |
| 5224 | static void |
| 5225 | symtable_global(struct symtable *st, node *n) |
| 5226 | { |
| 5227 | int i; |
| 5228 | |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 5229 | /* XXX It might be helpful to warn about module-level global |
| 5230 | statements, but it's hard to tell the difference between |
| 5231 | module-level and a string passed to exec. |
| 5232 | */ |
Jeremy Hylton | c176132 | 2001-02-28 23:44:45 +0000 | [diff] [blame] | 5233 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5234 | for (i = 1; i < NCH(n); i += 2) { |
| 5235 | char *name = STR(CHILD(n, i)); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5236 | int flags; |
| 5237 | |
| 5238 | flags = symtable_lookup(st, name); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5239 | if (flags < 0) |
| 5240 | continue; |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5241 | if (flags && flags != DEF_GLOBAL) { |
| 5242 | char buf[500]; |
| 5243 | if (flags & DEF_PARAM) { |
| 5244 | PyErr_Format(PyExc_SyntaxError, |
| 5245 | "name '%.400s' is local and global", |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5246 | name); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5247 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5248 | st->st_cur->ste_lineno); |
| 5249 | st->st_errors++; |
| 5250 | return; |
Guido van Rossum | 0bba7f8 | 2001-02-28 21:55:38 +0000 | [diff] [blame] | 5251 | } |
| 5252 | else { |
| 5253 | if (flags & DEF_LOCAL) |
| 5254 | sprintf(buf, GLOBAL_AFTER_ASSIGN, |
| 5255 | name); |
| 5256 | else |
| 5257 | sprintf(buf, GLOBAL_AFTER_USE, name); |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 5258 | symtable_warn(st, buf); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5259 | } |
| 5260 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5261 | symtable_add_def(st, name, DEF_GLOBAL); |
| 5262 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5263 | } |
| 5264 | |
| 5265 | static void |
| 5266 | symtable_list_comprehension(struct symtable *st, node *n) |
| 5267 | { |
| 5268 | char tmpname[12]; |
| 5269 | |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5270 | sprintf(tmpname, "_[%d]", st->st_tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5271 | symtable_add_def(st, tmpname, DEF_LOCAL); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5272 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5273 | symtable_node(st, CHILD(n, 3)); |
| 5274 | if (NCH(n) == 5) |
| 5275 | symtable_node(st, CHILD(n, 4)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5276 | } |
| 5277 | |
| 5278 | static void |
| 5279 | symtable_import(struct symtable *st, node *n) |
| 5280 | { |
| 5281 | int i; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5282 | /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)* |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5283 | | 'from' dotted_name 'import' |
| 5284 | ('*' | import_as_name (',' import_as_name)*) |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5285 | import_as_name: NAME [NAME NAME] |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5286 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5287 | if (STR(CHILD(n, 0))[0] == 'f') { /* from */ |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5288 | node *dotname = CHILD(n, 1); |
| 5289 | if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) { |
| 5290 | /* check for bogus imports */ |
| 5291 | if (n->n_lineno >= st->st_future->ff_last_lineno) { |
| 5292 | PyErr_SetString(PyExc_SyntaxError, |
| 5293 | LATE_FUTURE); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5294 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5295 | n->n_lineno); |
| 5296 | st->st_errors++; |
| 5297 | return; |
| 5298 | } |
| 5299 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5300 | if (TYPE(CHILD(n, 3)) == STAR) { |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 5301 | if (st->st_cur->ste_type != TYPE_MODULE) { |
Jeremy Hylton | 6a53bd8 | 2001-08-06 20:34:25 +0000 | [diff] [blame] | 5302 | if (symtable_warn(st, |
| 5303 | "import * only allowed at module level") < 0) |
| 5304 | return; |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 5305 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5306 | st->st_cur->ste_optimized |= OPT_IMPORT_STAR; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5307 | st->st_cur->ste_opt_lineno = n->n_lineno; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5308 | } else { |
| 5309 | for (i = 3; i < NCH(n); i += 2) { |
| 5310 | node *c = CHILD(n, i); |
| 5311 | if (NCH(c) > 1) /* import as */ |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5312 | symtable_assign(st, CHILD(c, 2), |
| 5313 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5314 | else |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5315 | symtable_assign(st, CHILD(c, 0), |
| 5316 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5317 | } |
| 5318 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5319 | } else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5320 | for (i = 1; i < NCH(n); i += 2) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5321 | symtable_assign(st, CHILD(n, i), DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5322 | } |
| 5323 | } |
| 5324 | } |
| 5325 | |
| 5326 | static void |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5327 | symtable_assign(struct symtable *st, node *n, int flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5328 | { |
| 5329 | node *tmp; |
| 5330 | int i; |
| 5331 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5332 | loop: |
| 5333 | switch (TYPE(n)) { |
| 5334 | case lambdef: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5335 | /* invalid assignment, e.g. lambda x:x=2. The next |
| 5336 | pass will catch this error. */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5337 | return; |
| 5338 | case power: |
| 5339 | if (NCH(n) > 2) { |
| 5340 | for (i = 2; i < NCH(n); ++i) |
| 5341 | if (TYPE(CHILD(n, i)) != DOUBLESTAR) |
| 5342 | symtable_node(st, CHILD(n, i)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5343 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5344 | if (NCH(n) > 1) { |
| 5345 | symtable_node(st, CHILD(n, 0)); |
| 5346 | symtable_node(st, CHILD(n, 1)); |
| 5347 | } else { |
| 5348 | n = CHILD(n, 0); |
| 5349 | goto loop; |
| 5350 | } |
| 5351 | return; |
| 5352 | case listmaker: |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5353 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
| 5354 | /* XXX This is an error, but the next pass |
| 5355 | will catch it. */ |
| 5356 | return; |
| 5357 | } else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5358 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5359 | symtable_assign(st, CHILD(n, i), flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5360 | } |
| 5361 | return; |
| 5362 | case exprlist: |
| 5363 | case testlist: |
| 5364 | if (NCH(n) == 1) { |
| 5365 | n = CHILD(n, 0); |
| 5366 | goto loop; |
| 5367 | } |
| 5368 | else { |
| 5369 | int i; |
| 5370 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5371 | symtable_assign(st, CHILD(n, i), flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5372 | return; |
| 5373 | } |
| 5374 | goto loop; |
| 5375 | case atom: |
| 5376 | tmp = CHILD(n, 0); |
| 5377 | if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) { |
| 5378 | n = CHILD(n, 1); |
| 5379 | goto loop; |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 5380 | } else if (TYPE(tmp) == NAME) { |
Jeremy Hylton | a830b38 | 2001-04-09 16:07:59 +0000 | [diff] [blame] | 5381 | if (strcmp(STR(tmp), "__debug__") == 0) |
| 5382 | symtable_warn(st, ASSIGN_DEBUG); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5383 | symtable_add_def(st, STR(tmp), DEF_LOCAL | flag); |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 5384 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5385 | return; |
| 5386 | case dotted_as_name: |
| 5387 | if (NCH(n) == 3) |
| 5388 | symtable_add_def(st, STR(CHILD(n, 2)), |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5389 | DEF_LOCAL | flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5390 | else |
| 5391 | symtable_add_def(st, |
| 5392 | STR(CHILD(CHILD(n, |
| 5393 | 0), 0)), |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5394 | DEF_LOCAL | flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5395 | return; |
| 5396 | case dotted_name: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5397 | symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5398 | return; |
| 5399 | case NAME: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5400 | symtable_add_def(st, STR(n), DEF_LOCAL | flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5401 | return; |
| 5402 | default: |
| 5403 | if (NCH(n) == 0) |
| 5404 | return; |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 5405 | if (NCH(n) == 1) { |
| 5406 | n = CHILD(n, 0); |
| 5407 | goto loop; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5408 | } |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 5409 | /* Should only occur for errors like x + 1 = 1, |
| 5410 | which will be caught in the next pass. */ |
| 5411 | for (i = 0; i < NCH(n); ++i) |
| 5412 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5413 | symtable_assign(st, CHILD(n, i), flag); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5414 | } |
| 5415 | } |