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 |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame^] | 1843 | com_invert_constant(struct compiling *c, node *n) |
| 1844 | { |
| 1845 | /* Compute the inverse of int and longs and use them directly, |
| 1846 | but be prepared to generate code for all other |
| 1847 | possibilities (invalid numbers, floats, complex). |
| 1848 | */ |
| 1849 | PyObject *num, *inv = NULL; |
| 1850 | int i; |
| 1851 | |
| 1852 | REQ(n, NUMBER); |
| 1853 | num = parsenumber(c, STR(n)); |
| 1854 | if (num == NULL) |
| 1855 | i = 255; |
| 1856 | else { |
| 1857 | inv = PyNumber_Invert(num); |
| 1858 | if (inv == NULL) { |
| 1859 | PyErr_Clear(); |
| 1860 | i = com_addconst(c, num); |
| 1861 | } else { |
| 1862 | i = com_addconst(c, inv); |
| 1863 | Py_DECREF(inv); |
| 1864 | } |
| 1865 | Py_DECREF(num); |
| 1866 | } |
| 1867 | com_addoparg(c, LOAD_CONST, i); |
| 1868 | com_push(c, 1); |
| 1869 | if (num != NULL && inv == NULL) |
| 1870 | com_addbyte(c, UNARY_INVERT); |
| 1871 | } |
| 1872 | |
| 1873 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1874 | com_factor(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1875 | { |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame^] | 1876 | int childtype = TYPE(CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1877 | REQ(n, factor); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame^] | 1878 | /* If the unary +, -, or ~ operator is applied to a constant, |
| 1879 | don't generate a UNARY_xxx opcode. Just store the |
| 1880 | approriate value as a constant. If the value is negative, |
| 1881 | extend the string containing the constant and insert a |
| 1882 | negative in the 0th position. |
| 1883 | */ |
| 1884 | if ((childtype == PLUS || childtype == MINUS || childtype == TILDE) |
| 1885 | && TYPE(CHILD(n, 1)) == factor |
| 1886 | && TYPE(CHILD(CHILD(n, 1), 0)) == power |
| 1887 | && TYPE(CHILD(CHILD(CHILD(n, 1), 0), 0)) == atom |
| 1888 | && TYPE(CHILD(CHILD(CHILD(CHILD(n, 1), 0), 0), 0)) == NUMBER) { |
| 1889 | node *constant = CHILD(CHILD(CHILD(n, 1), 0), 0); |
| 1890 | if (childtype == TILDE) { |
| 1891 | com_invert_constant(c, CHILD(constant, 0)); |
| 1892 | return; |
| 1893 | } |
| 1894 | if (childtype == MINUS) { |
| 1895 | node *numnode = CHILD(constant, 0); |
| 1896 | char *s = malloc(strlen(STR(numnode)) + 2); |
| 1897 | if (s == NULL) { |
| 1898 | com_error(c, PyExc_MemoryError, ""); |
| 1899 | com_addbyte(c, 255); |
| 1900 | return; |
| 1901 | } |
| 1902 | s[0] = '-'; |
| 1903 | strcpy(s + 1, STR(numnode)); |
| 1904 | free(STR(numnode)); |
| 1905 | STR(numnode) = s; |
| 1906 | } |
| 1907 | com_atom(c, constant); |
| 1908 | } |
| 1909 | else if (childtype == PLUS) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1910 | com_factor(c, CHILD(n, 1)); |
| 1911 | com_addbyte(c, UNARY_POSITIVE); |
| 1912 | } |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame^] | 1913 | else if (childtype == MINUS) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1914 | com_factor(c, CHILD(n, 1)); |
| 1915 | com_addbyte(c, UNARY_NEGATIVE); |
| 1916 | } |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame^] | 1917 | else if (childtype == TILDE) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1918 | com_factor(c, CHILD(n, 1)); |
| 1919 | com_addbyte(c, UNARY_INVERT); |
| 1920 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1921 | else { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1922 | com_power(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1923 | } |
| 1924 | } |
| 1925 | |
| 1926 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1927 | com_term(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1928 | { |
| 1929 | int i; |
| 1930 | int op; |
| 1931 | REQ(n, term); |
| 1932 | com_factor(c, CHILD(n, 0)); |
| 1933 | for (i = 2; i < NCH(n); i += 2) { |
| 1934 | com_factor(c, CHILD(n, i)); |
| 1935 | switch (TYPE(CHILD(n, i-1))) { |
| 1936 | case STAR: |
| 1937 | op = BINARY_MULTIPLY; |
| 1938 | break; |
| 1939 | case SLASH: |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1940 | if (c->c_flags & CO_FUTURE_DIVISION) |
| 1941 | op = BINARY_TRUE_DIVIDE; |
| 1942 | else |
| 1943 | op = BINARY_DIVIDE; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1944 | break; |
| 1945 | case PERCENT: |
| 1946 | op = BINARY_MODULO; |
| 1947 | break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1948 | case DOUBLESLASH: |
| 1949 | op = BINARY_FLOOR_DIVIDE; |
| 1950 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1951 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1952 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1953 | "com_term: operator not *, /, // or %"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1954 | op = 255; |
| 1955 | } |
| 1956 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1957 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1962 | com_arith_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1963 | { |
| 1964 | int i; |
| 1965 | int op; |
| 1966 | REQ(n, arith_expr); |
| 1967 | com_term(c, CHILD(n, 0)); |
| 1968 | for (i = 2; i < NCH(n); i += 2) { |
| 1969 | com_term(c, CHILD(n, i)); |
| 1970 | switch (TYPE(CHILD(n, i-1))) { |
| 1971 | case PLUS: |
| 1972 | op = BINARY_ADD; |
| 1973 | break; |
| 1974 | case MINUS: |
| 1975 | op = BINARY_SUBTRACT; |
| 1976 | break; |
| 1977 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1978 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1979 | "com_arith_expr: operator not + or -"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1980 | op = 255; |
| 1981 | } |
| 1982 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1983 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1988 | com_shift_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1989 | { |
| 1990 | int i; |
| 1991 | int op; |
| 1992 | REQ(n, shift_expr); |
| 1993 | com_arith_expr(c, CHILD(n, 0)); |
| 1994 | for (i = 2; i < NCH(n); i += 2) { |
| 1995 | com_arith_expr(c, CHILD(n, i)); |
| 1996 | switch (TYPE(CHILD(n, i-1))) { |
| 1997 | case LEFTSHIFT: |
| 1998 | op = BINARY_LSHIFT; |
| 1999 | break; |
| 2000 | case RIGHTSHIFT: |
| 2001 | op = BINARY_RSHIFT; |
| 2002 | break; |
| 2003 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2004 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2005 | "com_shift_expr: operator not << or >>"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2006 | op = 255; |
| 2007 | } |
| 2008 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2009 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2014 | com_and_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2015 | { |
| 2016 | int i; |
| 2017 | int op; |
| 2018 | REQ(n, and_expr); |
| 2019 | com_shift_expr(c, CHILD(n, 0)); |
| 2020 | for (i = 2; i < NCH(n); i += 2) { |
| 2021 | com_shift_expr(c, CHILD(n, i)); |
| 2022 | if (TYPE(CHILD(n, i-1)) == AMPER) { |
| 2023 | op = BINARY_AND; |
| 2024 | } |
| 2025 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2026 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2027 | "com_and_expr: operator not &"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2028 | op = 255; |
| 2029 | } |
| 2030 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2031 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2032 | } |
| 2033 | } |
| 2034 | |
| 2035 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2036 | com_xor_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2037 | { |
| 2038 | int i; |
| 2039 | int op; |
| 2040 | REQ(n, xor_expr); |
| 2041 | com_and_expr(c, CHILD(n, 0)); |
| 2042 | for (i = 2; i < NCH(n); i += 2) { |
| 2043 | com_and_expr(c, CHILD(n, i)); |
| 2044 | if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) { |
| 2045 | op = BINARY_XOR; |
| 2046 | } |
| 2047 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2048 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2049 | "com_xor_expr: operator not ^"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2050 | op = 255; |
| 2051 | } |
| 2052 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2053 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2054 | } |
| 2055 | } |
| 2056 | |
| 2057 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2058 | com_expr(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2059 | { |
| 2060 | int i; |
| 2061 | int op; |
| 2062 | REQ(n, expr); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2063 | com_xor_expr(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2064 | for (i = 2; i < NCH(n); i += 2) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2065 | com_xor_expr(c, CHILD(n, i)); |
| 2066 | if (TYPE(CHILD(n, i-1)) == VBAR) { |
| 2067 | op = BINARY_OR; |
| 2068 | } |
| 2069 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2070 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2071 | "com_expr: expr operator not |"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2072 | op = 255; |
| 2073 | } |
| 2074 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2075 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2076 | } |
| 2077 | } |
| 2078 | |
| 2079 | static enum cmp_op |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2080 | cmp_type(node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2081 | { |
| 2082 | REQ(n, comp_op); |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2083 | /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '==' |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2084 | | 'in' | 'not' 'in' | 'is' | 'is' not' */ |
| 2085 | if (NCH(n) == 1) { |
| 2086 | n = CHILD(n, 0); |
| 2087 | switch (TYPE(n)) { |
| 2088 | case LESS: return LT; |
| 2089 | case GREATER: return GT; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2090 | case EQEQUAL: /* == */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2091 | case EQUAL: return EQ; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2092 | case LESSEQUAL: return LE; |
| 2093 | case GREATEREQUAL: return GE; |
| 2094 | case NOTEQUAL: return NE; /* <> or != */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2095 | case NAME: if (strcmp(STR(n), "in") == 0) return IN; |
| 2096 | if (strcmp(STR(n), "is") == 0) return IS; |
| 2097 | } |
| 2098 | } |
| 2099 | else if (NCH(n) == 2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2100 | switch (TYPE(CHILD(n, 0))) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2101 | case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 2102 | return NOT_IN; |
| 2103 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 2104 | return IS_NOT; |
| 2105 | } |
| 2106 | } |
| 2107 | return BAD; |
| 2108 | } |
| 2109 | |
| 2110 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2111 | com_comparison(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2112 | { |
| 2113 | int i; |
| 2114 | enum cmp_op op; |
| 2115 | int anchor; |
| 2116 | REQ(n, comparison); /* comparison: expr (comp_op expr)* */ |
| 2117 | com_expr(c, CHILD(n, 0)); |
| 2118 | if (NCH(n) == 1) |
| 2119 | return; |
| 2120 | |
| 2121 | /**************************************************************** |
| 2122 | The following code is generated for all but the last |
| 2123 | comparison in a chain: |
| 2124 | |
| 2125 | label: on stack: opcode: jump to: |
| 2126 | |
| 2127 | a <code to load b> |
| 2128 | a, b DUP_TOP |
| 2129 | a, b, b ROT_THREE |
| 2130 | b, a, b COMPARE_OP |
| 2131 | b, 0-or-1 JUMP_IF_FALSE L1 |
| 2132 | b, 1 POP_TOP |
| 2133 | b |
| 2134 | |
| 2135 | We are now ready to repeat this sequence for the next |
| 2136 | comparison in the chain. |
| 2137 | |
| 2138 | For the last we generate: |
| 2139 | |
| 2140 | b <code to load c> |
| 2141 | b, c COMPARE_OP |
| 2142 | 0-or-1 |
| 2143 | |
| 2144 | If there were any jumps to L1 (i.e., there was more than one |
| 2145 | comparison), we generate: |
| 2146 | |
| 2147 | 0-or-1 JUMP_FORWARD L2 |
| 2148 | L1: b, 0 ROT_TWO |
| 2149 | 0, b POP_TOP |
| 2150 | 0 |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2151 | L2: 0-or-1 |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2152 | ****************************************************************/ |
| 2153 | |
| 2154 | anchor = 0; |
| 2155 | |
| 2156 | for (i = 2; i < NCH(n); i += 2) { |
| 2157 | com_expr(c, CHILD(n, i)); |
| 2158 | if (i+2 < NCH(n)) { |
| 2159 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2160 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2161 | com_addbyte(c, ROT_THREE); |
| 2162 | } |
| 2163 | op = cmp_type(CHILD(n, i-1)); |
| 2164 | if (op == BAD) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2165 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2166 | "com_comparison: unknown comparison op"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2167 | } |
| 2168 | com_addoparg(c, COMPARE_OP, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2169 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2170 | if (i+2 < NCH(n)) { |
| 2171 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2172 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2173 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2174 | } |
| 2175 | } |
| 2176 | |
| 2177 | if (anchor) { |
| 2178 | int anchor2 = 0; |
| 2179 | com_addfwref(c, JUMP_FORWARD, &anchor2); |
| 2180 | com_backpatch(c, anchor); |
| 2181 | com_addbyte(c, ROT_TWO); |
| 2182 | com_addbyte(c, POP_TOP); |
| 2183 | com_backpatch(c, anchor2); |
| 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2188 | com_not_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2189 | { |
| 2190 | REQ(n, not_test); /* 'not' not_test | comparison */ |
| 2191 | if (NCH(n) == 1) { |
| 2192 | com_comparison(c, CHILD(n, 0)); |
| 2193 | } |
| 2194 | else { |
| 2195 | com_not_test(c, CHILD(n, 1)); |
| 2196 | com_addbyte(c, UNARY_NOT); |
| 2197 | } |
| 2198 | } |
| 2199 | |
| 2200 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2201 | com_and_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2202 | { |
| 2203 | int i; |
| 2204 | int anchor; |
| 2205 | REQ(n, and_test); /* not_test ('and' not_test)* */ |
| 2206 | anchor = 0; |
| 2207 | i = 0; |
| 2208 | for (;;) { |
| 2209 | com_not_test(c, CHILD(n, i)); |
| 2210 | if ((i += 2) >= NCH(n)) |
| 2211 | break; |
| 2212 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2213 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2214 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2215 | } |
| 2216 | if (anchor) |
| 2217 | com_backpatch(c, anchor); |
| 2218 | } |
| 2219 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2220 | static int |
| 2221 | com_make_closure(struct compiling *c, PyCodeObject *co) |
| 2222 | { |
| 2223 | int i, free = PyTuple_GET_SIZE(co->co_freevars); |
| 2224 | if (free == 0) |
| 2225 | return 0; |
| 2226 | for (i = 0; i < free; ++i) { |
| 2227 | /* Bypass com_addop_varname because it will generate |
| 2228 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 2229 | */ |
| 2230 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 2231 | int arg, reftype; |
| 2232 | |
| 2233 | /* Special case: If a class contains a method with a |
| 2234 | free variable that has the same name as a method, |
| 2235 | the name will be considered free *and* local in the |
| 2236 | class. It should be handled by the closure, as |
| 2237 | well as by the normal name loookup logic. |
| 2238 | */ |
| 2239 | reftype = get_ref_type(c, PyString_AS_STRING(name)); |
| 2240 | if (reftype == CELL) |
| 2241 | arg = com_lookup_arg(c->c_cellvars, name); |
| 2242 | else /* (reftype == FREE) */ |
| 2243 | arg = com_lookup_arg(c->c_freevars, name); |
| 2244 | if (arg == -1) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 2245 | fprintf(stderr, "lookup %s in %s %d %d\n" |
| 2246 | "freevars of %s: %s\n", |
| 2247 | PyObject_REPR(name), |
| 2248 | c->c_name, |
| 2249 | reftype, arg, |
| 2250 | PyString_AS_STRING(co->co_name), |
| 2251 | PyObject_REPR(co->co_freevars)); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2252 | Py_FatalError("com_make_closure()"); |
| 2253 | } |
| 2254 | com_addoparg(c, LOAD_CLOSURE, arg); |
| 2255 | |
| 2256 | } |
| 2257 | com_push(c, free); |
| 2258 | return 1; |
| 2259 | } |
| 2260 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2261 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2262 | com_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2263 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2264 | REQ(n, test); /* and_test ('or' and_test)* | lambdef */ |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2265 | if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2266 | PyObject *co; |
| 2267 | int i, closure; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2268 | int ndefs = com_argdefs(c, CHILD(n, 0)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 2269 | symtable_enter_scope(c->c_symtable, "lambda", lambdef, |
| 2270 | n->n_lineno); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2271 | co = (PyObject *) icompile(CHILD(n, 0), c); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2272 | if (co == NULL) { |
| 2273 | c->c_errors++; |
| 2274 | return; |
| 2275 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2276 | symtable_exit_scope(c->c_symtable); |
Tim Peters | 1e54211 | 2001-02-23 22:23:53 +0000 | [diff] [blame] | 2277 | i = com_addconst(c, co); |
| 2278 | closure = com_make_closure(c, (PyCodeObject *)co); |
| 2279 | Py_DECREF(co); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2280 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2281 | com_push(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2282 | if (closure) |
| 2283 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
| 2284 | else |
| 2285 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2286 | com_pop(c, ndefs); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2287 | } |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2288 | else { |
| 2289 | int anchor = 0; |
| 2290 | int i = 0; |
| 2291 | for (;;) { |
| 2292 | com_and_test(c, CHILD(n, i)); |
| 2293 | if ((i += 2) >= NCH(n)) |
| 2294 | break; |
| 2295 | com_addfwref(c, JUMP_IF_TRUE, &anchor); |
| 2296 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2297 | com_pop(c, 1); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2298 | } |
| 2299 | if (anchor) |
| 2300 | com_backpatch(c, anchor); |
| 2301 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
| 2304 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2305 | com_list(struct compiling *c, node *n, int toplevel) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2306 | { |
| 2307 | /* exprlist: expr (',' expr)* [',']; likewise for testlist */ |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 2308 | if (NCH(n) == 1 && !toplevel) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2309 | com_node(c, CHILD(n, 0)); |
| 2310 | } |
| 2311 | else { |
| 2312 | int i; |
| 2313 | int len; |
| 2314 | len = (NCH(n) + 1) / 2; |
| 2315 | for (i = 0; i < NCH(n); i += 2) |
| 2316 | com_node(c, CHILD(n, i)); |
| 2317 | com_addoparg(c, BUILD_TUPLE, len); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2318 | com_pop(c, len-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2319 | } |
| 2320 | } |
| 2321 | |
| 2322 | |
| 2323 | /* Begin of assignment compilation */ |
| 2324 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2325 | |
| 2326 | static void |
| 2327 | com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn) |
| 2328 | { |
| 2329 | com_addbyte(c, DUP_TOP); |
| 2330 | com_push(c, 1); |
| 2331 | com_addopname(c, LOAD_ATTR, n); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2332 | com_node(c, augn); |
| 2333 | com_addbyte(c, opcode); |
| 2334 | com_pop(c, 1); |
| 2335 | com_addbyte(c, ROT_TWO); |
| 2336 | com_addopname(c, STORE_ATTR, n); |
| 2337 | com_pop(c, 2); |
| 2338 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2339 | |
| 2340 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2341 | com_assign_attr(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2342 | { |
| 2343 | com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2344 | com_pop(c, assigning ? 2 : 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
| 2347 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2348 | 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] | 2349 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2350 | REQ(n, trailer); |
| 2351 | switch (TYPE(CHILD(n, 0))) { |
| 2352 | case LPAR: /* '(' [exprlist] ')' */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2353 | com_error(c, PyExc_SyntaxError, |
| 2354 | "can't assign to function call"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2355 | break; |
| 2356 | case DOT: /* '.' NAME */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2357 | if (assigning > OP_APPLY) |
| 2358 | com_augassign_attr(c, CHILD(n, 1), assigning, augn); |
| 2359 | else |
| 2360 | com_assign_attr(c, CHILD(n, 1), assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2361 | break; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2362 | case LSQB: /* '[' subscriptlist ']' */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2363 | com_subscriptlist(c, CHILD(n, 1), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2364 | break; |
| 2365 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2366 | com_error(c, PyExc_SystemError, "unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2367 | } |
| 2368 | } |
| 2369 | |
| 2370 | static void |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2371 | com_assign_sequence(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2372 | { |
| 2373 | int i; |
Thomas Wouters | b59290f | 2000-08-25 05:41:11 +0000 | [diff] [blame] | 2374 | if (TYPE(n) != testlist && TYPE(n) != listmaker) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2375 | REQ(n, exprlist); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2376 | if (assigning) { |
| 2377 | i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2378 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2379 | com_push(c, i-1); |
| 2380 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2381 | for (i = 0; i < NCH(n); i += 2) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2382 | com_assign(c, CHILD(n, i), assigning, NULL); |
| 2383 | } |
| 2384 | |
| 2385 | static void |
| 2386 | com_augassign_name(struct compiling *c, node *n, int opcode, node *augn) |
| 2387 | { |
| 2388 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2389 | com_addop_varname(c, VAR_LOAD, STR(n)); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2390 | com_push(c, 1); |
| 2391 | com_node(c, augn); |
| 2392 | com_addbyte(c, opcode); |
| 2393 | com_pop(c, 1); |
| 2394 | com_assign_name(c, n, OP_ASSIGN); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
| 2397 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2398 | com_assign_name(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2399 | { |
| 2400 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2401 | com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2402 | if (assigning) |
| 2403 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2404 | } |
| 2405 | |
| 2406 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2407 | com_assign(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2408 | { |
| 2409 | /* Loop to avoid trivial recursion */ |
| 2410 | for (;;) { |
| 2411 | switch (TYPE(n)) { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2412 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2413 | case exprlist: |
| 2414 | case testlist: |
| 2415 | if (NCH(n) > 1) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2416 | if (assigning > OP_APPLY) { |
| 2417 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 2418 | "augmented assign to tuple not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2419 | return; |
| 2420 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2421 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2422 | return; |
| 2423 | } |
| 2424 | n = CHILD(n, 0); |
| 2425 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2426 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2427 | case test: |
| 2428 | case and_test: |
| 2429 | case not_test: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2430 | case comparison: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2431 | case expr: |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2432 | case xor_expr: |
| 2433 | case and_expr: |
| 2434 | case shift_expr: |
| 2435 | case arith_expr: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2436 | case term: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2437 | case factor: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2438 | if (NCH(n) > 1) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2439 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2440 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2441 | return; |
| 2442 | } |
| 2443 | n = CHILD(n, 0); |
| 2444 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2445 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2446 | case power: /* atom trailer* ('**' power)* |
| 2447 | ('+'|'-'|'~') factor | atom trailer* */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2448 | if (TYPE(CHILD(n, 0)) != atom) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2449 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2450 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2451 | return; |
| 2452 | } |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2453 | if (NCH(n) > 1) { /* trailer or exponent present */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2454 | int i; |
| 2455 | com_node(c, CHILD(n, 0)); |
| 2456 | for (i = 1; i+1 < NCH(n); i++) { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2457 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2458 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2459 | "can't assign to operator"); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2460 | return; |
| 2461 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2462 | com_apply_trailer(c, CHILD(n, i)); |
| 2463 | } /* NB i is still alive */ |
| 2464 | com_assign_trailer(c, |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2465 | CHILD(n, i), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2466 | return; |
| 2467 | } |
| 2468 | n = CHILD(n, 0); |
| 2469 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2470 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2471 | case atom: |
| 2472 | switch (TYPE(CHILD(n, 0))) { |
| 2473 | case LPAR: |
| 2474 | n = CHILD(n, 1); |
| 2475 | if (TYPE(n) == RPAR) { |
| 2476 | /* XXX Should allow () = () ??? */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2477 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2478 | "can't assign to ()"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2479 | return; |
| 2480 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2481 | if (assigning > OP_APPLY) { |
| 2482 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2483 | "augmented assign to tuple not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2484 | return; |
| 2485 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2486 | break; |
| 2487 | case LSQB: |
| 2488 | n = CHILD(n, 1); |
| 2489 | if (TYPE(n) == RSQB) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2490 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2491 | "can't assign to []"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2492 | return; |
| 2493 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2494 | if (assigning > OP_APPLY) { |
| 2495 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2496 | "augmented assign to list not possible"); |
| 2497 | return; |
| 2498 | } |
| 2499 | if (NCH(n) > 1 |
| 2500 | && TYPE(CHILD(n, 1)) == list_for) { |
| 2501 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2502 | "can't assign to list comprehension"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2503 | return; |
| 2504 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2505 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2506 | return; |
| 2507 | case NAME: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2508 | if (assigning > OP_APPLY) |
| 2509 | com_augassign_name(c, CHILD(n, 0), |
| 2510 | assigning, augn); |
| 2511 | else |
| 2512 | com_assign_name(c, CHILD(n, 0), |
| 2513 | assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2514 | return; |
| 2515 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2516 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2517 | "can't assign to literal"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2518 | return; |
| 2519 | } |
| 2520 | break; |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2521 | |
| 2522 | case lambdef: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2523 | com_error(c, PyExc_SyntaxError, |
| 2524 | "can't assign to lambda"); |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2525 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2526 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2527 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2528 | com_error(c, PyExc_SystemError, |
| 2529 | "com_assign: bad node"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2530 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2531 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2532 | } |
| 2533 | } |
| 2534 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2535 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2536 | static void |
| 2537 | com_augassign(struct compiling *c, node *n) |
| 2538 | { |
| 2539 | int opcode; |
| 2540 | |
| 2541 | switch (STR(CHILD(CHILD(n, 1), 0))[0]) { |
| 2542 | case '+': opcode = INPLACE_ADD; break; |
| 2543 | case '-': opcode = INPLACE_SUBTRACT; break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2544 | case '/': |
| 2545 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '/') |
| 2546 | opcode = INPLACE_FLOOR_DIVIDE; |
| 2547 | else if (c->c_flags & CO_FUTURE_DIVISION) |
| 2548 | opcode = INPLACE_TRUE_DIVIDE; |
| 2549 | else |
| 2550 | opcode = INPLACE_DIVIDE; |
| 2551 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2552 | case '%': opcode = INPLACE_MODULO; break; |
| 2553 | case '<': opcode = INPLACE_LSHIFT; break; |
| 2554 | case '>': opcode = INPLACE_RSHIFT; break; |
| 2555 | case '&': opcode = INPLACE_AND; break; |
| 2556 | case '^': opcode = INPLACE_XOR; break; |
| 2557 | case '|': opcode = INPLACE_OR; break; |
| 2558 | case '*': |
| 2559 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '*') |
| 2560 | opcode = INPLACE_POWER; |
| 2561 | else |
| 2562 | opcode = INPLACE_MULTIPLY; |
| 2563 | break; |
| 2564 | default: |
| 2565 | com_error(c, PyExc_SystemError, "com_augassign: bad operator"); |
| 2566 | return; |
| 2567 | } |
| 2568 | com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2)); |
| 2569 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2570 | |
| 2571 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2572 | com_expr_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2573 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2574 | REQ(n, expr_stmt); |
| 2575 | /* testlist (('=' testlist)* | augassign testlist) */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2576 | /* Forget it if we have just a doc string here */ |
Guido van Rossum | 5f5e817 | 1997-04-06 03:41:40 +0000 | [diff] [blame] | 2577 | if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2578 | return; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2579 | if (NCH(n) == 1) { |
| 2580 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2581 | if (c->c_interactive) |
| 2582 | com_addbyte(c, PRINT_EXPR); |
| 2583 | else |
| 2584 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2585 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2586 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2587 | else if (TYPE(CHILD(n,1)) == augassign) |
| 2588 | com_augassign(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2589 | else { |
| 2590 | int i; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2591 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2592 | for (i = 0; i < NCH(n)-2; i+=2) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2593 | if (i+2 < NCH(n)-2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2594 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2595 | com_push(c, 1); |
| 2596 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2597 | com_assign(c, CHILD(n, i), OP_ASSIGN, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2598 | } |
| 2599 | } |
| 2600 | } |
| 2601 | |
| 2602 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2603 | com_assert_stmt(struct compiling *c, node *n) |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2604 | { |
| 2605 | int a = 0, b = 0; |
| 2606 | int i; |
| 2607 | REQ(n, assert_stmt); /* 'assert' test [',' test] */ |
| 2608 | /* Generate code like for |
| 2609 | |
| 2610 | if __debug__: |
| 2611 | if not <test>: |
| 2612 | raise AssertionError [, <message>] |
| 2613 | |
| 2614 | where <message> is the second test, if present. |
| 2615 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2616 | |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2617 | if (Py_OptimizeFlag) |
| 2618 | return; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2619 | com_addop_name(c, LOAD_GLOBAL, "__debug__"); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2620 | com_push(c, 1); |
| 2621 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 2622 | com_addbyte(c, POP_TOP); |
| 2623 | com_pop(c, 1); |
| 2624 | com_node(c, CHILD(n, 1)); |
| 2625 | com_addfwref(c, JUMP_IF_TRUE, &b); |
| 2626 | com_addbyte(c, POP_TOP); |
| 2627 | com_pop(c, 1); |
| 2628 | /* Raise that exception! */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2629 | com_addop_name(c, LOAD_GLOBAL, "AssertionError"); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2630 | com_push(c, 1); |
| 2631 | i = NCH(n)/2; /* Either 2 or 4 */ |
| 2632 | if (i > 1) |
| 2633 | com_node(c, CHILD(n, 3)); |
| 2634 | com_addoparg(c, RAISE_VARARGS, i); |
| 2635 | com_pop(c, i); |
| 2636 | /* The interpreter does not fall through */ |
| 2637 | /* All jumps converge here */ |
| 2638 | com_backpatch(c, a); |
| 2639 | com_backpatch(c, b); |
| 2640 | com_addbyte(c, POP_TOP); |
| 2641 | } |
| 2642 | |
| 2643 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2644 | com_print_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2645 | { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2646 | int i = 1; |
| 2647 | node* stream = NULL; |
| 2648 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2649 | REQ(n, print_stmt); /* 'print' (test ',')* [test] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2650 | |
| 2651 | /* are we using the extended print form? */ |
| 2652 | if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) { |
| 2653 | stream = CHILD(n, 2); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2654 | com_node(c, stream); |
| 2655 | /* stack: [...] => [... stream] */ |
| 2656 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2657 | if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA) |
| 2658 | i = 4; |
| 2659 | else |
| 2660 | i = 3; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2661 | } |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2662 | for (; i < NCH(n); i += 2) { |
| 2663 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2664 | com_addbyte(c, DUP_TOP); |
| 2665 | /* stack: [stream] => [stream stream] */ |
| 2666 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2667 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2668 | /* stack: [stream stream] => [stream stream obj] */ |
| 2669 | com_addbyte(c, ROT_TWO); |
| 2670 | /* stack: [stream stream obj] => [stream obj stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2671 | com_addbyte(c, PRINT_ITEM_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2672 | /* stack: [stream obj stream] => [stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2673 | com_pop(c, 2); |
| 2674 | } |
| 2675 | else { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2676 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2677 | /* stack: [...] => [... obj] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2678 | com_addbyte(c, PRINT_ITEM); |
| 2679 | com_pop(c, 1); |
| 2680 | } |
| 2681 | } |
| 2682 | /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */ |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2683 | if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2684 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2685 | /* must pop the extra stream object off the stack */ |
| 2686 | com_addbyte(c, POP_TOP); |
| 2687 | /* stack: [... stream] => [...] */ |
| 2688 | com_pop(c, 1); |
| 2689 | } |
| 2690 | } |
| 2691 | else { |
| 2692 | if (stream != NULL) { |
| 2693 | /* this consumes the last stream object on stack */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2694 | com_addbyte(c, PRINT_NEWLINE_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2695 | /* stack: [... stream] => [...] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2696 | com_pop(c, 1); |
| 2697 | } |
| 2698 | else |
| 2699 | com_addbyte(c, PRINT_NEWLINE); |
| 2700 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
| 2703 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2704 | com_return_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2705 | { |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2706 | REQ(n, return_stmt); /* 'return' [testlist] */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2707 | if (!c->c_infunction) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2708 | com_error(c, PyExc_SyntaxError, "'return' outside function"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2709 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2710 | if (c->c_flags & CO_GENERATOR) { |
| 2711 | if (NCH(n) > 1) { |
| 2712 | com_error(c, PyExc_SyntaxError, |
| 2713 | "'return' with argument inside generator"); |
| 2714 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 2715 | } |
| 2716 | if (NCH(n) < 2) { |
| 2717 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2718 | com_push(c, 1); |
| 2719 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 2720 | else |
| 2721 | com_node(c, CHILD(n, 1)); |
| 2722 | com_addbyte(c, RETURN_VALUE); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2723 | com_pop(c, 1); |
| 2724 | } |
| 2725 | |
| 2726 | static void |
| 2727 | com_yield_stmt(struct compiling *c, node *n) |
| 2728 | { |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 2729 | int i; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2730 | REQ(n, yield_stmt); /* 'yield' testlist */ |
| 2731 | if (!c->c_infunction) { |
| 2732 | com_error(c, PyExc_SyntaxError, "'yield' outside function"); |
| 2733 | } |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 2734 | |
| 2735 | for (i = 0; i < c->c_nblocks; ++i) { |
| 2736 | if (c->c_block[i] == SETUP_FINALLY) { |
| 2737 | com_error(c, PyExc_SyntaxError, |
| 2738 | "'yield' not allowed in a 'try' block " |
| 2739 | "with a 'finally' clause"); |
| 2740 | return; |
| 2741 | } |
| 2742 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2743 | com_node(c, CHILD(n, 1)); |
| 2744 | com_addbyte(c, YIELD_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2745 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
| 2748 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2749 | com_raise_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2750 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2751 | int i; |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 2752 | REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */ |
| 2753 | if (NCH(n) > 1) { |
| 2754 | com_node(c, CHILD(n, 1)); |
| 2755 | if (NCH(n) > 3) { |
| 2756 | com_node(c, CHILD(n, 3)); |
| 2757 | if (NCH(n) > 5) |
| 2758 | com_node(c, CHILD(n, 5)); |
| 2759 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2760 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2761 | i = NCH(n)/2; |
| 2762 | com_addoparg(c, RAISE_VARARGS, i); |
| 2763 | com_pop(c, i); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2764 | } |
| 2765 | |
| 2766 | static void |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2767 | com_from_import(struct compiling *c, node *n) |
| 2768 | { |
| 2769 | com_addopname(c, IMPORT_FROM, CHILD(n, 0)); |
| 2770 | com_push(c, 1); |
| 2771 | if (NCH(n) > 1) { |
| 2772 | if (strcmp(STR(CHILD(n, 1)), "as") != 0) { |
| 2773 | com_error(c, PyExc_SyntaxError, "invalid syntax"); |
| 2774 | return; |
| 2775 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2776 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2777 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2778 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2779 | com_pop(c, 1); |
| 2780 | } |
| 2781 | |
| 2782 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2783 | com_import_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2784 | { |
| 2785 | int i; |
| 2786 | REQ(n, import_stmt); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2787 | /* 'import' dotted_name (',' dotted_name)* | |
| 2788 | 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2789 | if (STR(CHILD(n, 0))[0] == 'f') { |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 2790 | PyObject *tup; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2791 | /* 'from' dotted_name 'import' ... */ |
| 2792 | REQ(CHILD(n, 1), dotted_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2793 | |
| 2794 | if (TYPE(CHILD(n, 3)) == STAR) { |
| 2795 | tup = Py_BuildValue("(s)", "*"); |
| 2796 | } else { |
| 2797 | tup = PyTuple_New((NCH(n) - 2)/2); |
| 2798 | for (i = 3; i < NCH(n); i += 2) { |
| 2799 | PyTuple_SET_ITEM(tup, (i-3)/2, |
| 2800 | PyString_FromString(STR( |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 2801 | CHILD(CHILD(n, i), 0)))); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2802 | } |
| 2803 | } |
| 2804 | com_addoparg(c, LOAD_CONST, com_addconst(c, tup)); |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 2805 | Py_DECREF(tup); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2806 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2807 | com_addopname(c, IMPORT_NAME, CHILD(n, 1)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2808 | if (TYPE(CHILD(n, 3)) == STAR) |
| 2809 | com_addbyte(c, IMPORT_STAR); |
| 2810 | else { |
| 2811 | for (i = 3; i < NCH(n); i += 2) |
| 2812 | com_from_import(c, CHILD(n, i)); |
| 2813 | com_addbyte(c, POP_TOP); |
| 2814 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2815 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2816 | } |
| 2817 | else { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2818 | /* 'import' ... */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2819 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2820 | node *subn = CHILD(n, i); |
| 2821 | REQ(subn, dotted_as_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2822 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2823 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2824 | com_addopname(c, IMPORT_NAME, CHILD(subn, 0)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2825 | if (NCH(subn) > 1) { |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 2826 | int j; |
| 2827 | if (strcmp(STR(CHILD(subn, 1)), "as") != 0) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2828 | com_error(c, PyExc_SyntaxError, |
| 2829 | "invalid syntax"); |
| 2830 | return; |
| 2831 | } |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 2832 | for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2) |
| 2833 | com_addopname(c, LOAD_ATTR, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2834 | CHILD(CHILD(subn, 0), |
| 2835 | j)); |
| 2836 | com_addop_varname(c, VAR_STORE, |
| 2837 | STR(CHILD(subn, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2838 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2839 | com_addop_varname(c, VAR_STORE, |
| 2840 | STR(CHILD(CHILD(subn, 0), |
| 2841 | 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2842 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2843 | } |
| 2844 | } |
| 2845 | } |
| 2846 | |
| 2847 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2848 | com_exec_stmt(struct compiling *c, node *n) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2849 | { |
| 2850 | REQ(n, exec_stmt); |
| 2851 | /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */ |
| 2852 | com_node(c, CHILD(n, 1)); |
| 2853 | if (NCH(n) >= 4) |
| 2854 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2855 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2856 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2857 | com_push(c, 1); |
| 2858 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2859 | if (NCH(n) >= 6) |
| 2860 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2861 | else { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2862 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2863 | com_push(c, 1); |
| 2864 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2865 | com_addbyte(c, EXEC_STMT); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2866 | com_pop(c, 3); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2869 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2870 | is_constant_false(struct compiling *c, node *n) |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2871 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2872 | PyObject *v; |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2873 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2874 | /* argument c will be NULL when called from symtable_node() */ |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2875 | |
| 2876 | /* Label to avoid tail recursion */ |
| 2877 | next: |
| 2878 | switch (TYPE(n)) { |
| 2879 | |
| 2880 | case suite: |
| 2881 | if (NCH(n) == 1) { |
| 2882 | n = CHILD(n, 0); |
| 2883 | goto next; |
| 2884 | } |
| 2885 | /* Fall through */ |
| 2886 | case file_input: |
| 2887 | for (i = 0; i < NCH(n); i++) { |
| 2888 | node *ch = CHILD(n, i); |
| 2889 | if (TYPE(ch) == stmt) { |
| 2890 | n = ch; |
| 2891 | goto next; |
| 2892 | } |
| 2893 | } |
| 2894 | break; |
| 2895 | |
| 2896 | case stmt: |
| 2897 | case simple_stmt: |
| 2898 | case small_stmt: |
| 2899 | n = CHILD(n, 0); |
| 2900 | goto next; |
| 2901 | |
| 2902 | case expr_stmt: |
| 2903 | case testlist: |
| 2904 | case test: |
| 2905 | case and_test: |
| 2906 | case not_test: |
| 2907 | case comparison: |
| 2908 | case expr: |
| 2909 | case xor_expr: |
| 2910 | case and_expr: |
| 2911 | case shift_expr: |
| 2912 | case arith_expr: |
| 2913 | case term: |
| 2914 | case factor: |
| 2915 | case power: |
| 2916 | case atom: |
| 2917 | if (NCH(n) == 1) { |
| 2918 | n = CHILD(n, 0); |
| 2919 | goto next; |
| 2920 | } |
| 2921 | break; |
| 2922 | |
| 2923 | case NAME: |
| 2924 | if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0) |
| 2925 | return 1; |
| 2926 | break; |
| 2927 | |
| 2928 | case NUMBER: |
| 2929 | v = parsenumber(c, STR(n)); |
| 2930 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2931 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2932 | break; |
| 2933 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2934 | i = PyObject_IsTrue(v); |
| 2935 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2936 | return i == 0; |
| 2937 | |
| 2938 | case STRING: |
Guido van Rossum | 6af0c00 | 1997-03-11 21:25:55 +0000 | [diff] [blame] | 2939 | v = parsestr(STR(n)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2940 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2941 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2942 | break; |
| 2943 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2944 | i = PyObject_IsTrue(v); |
| 2945 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2946 | return i == 0; |
| 2947 | |
| 2948 | } |
| 2949 | return 0; |
| 2950 | } |
| 2951 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 2952 | |
| 2953 | /* Look under n for a return stmt with an expression. |
| 2954 | * This hack is used to find illegal returns under "if 0:" blocks in |
| 2955 | * functions already known to be generators (as determined by the symtable |
| 2956 | * pass). |
| 2957 | * Return the offending return node if found, else NULL. |
| 2958 | */ |
| 2959 | static node * |
| 2960 | look_for_offending_return(node *n) |
| 2961 | { |
| 2962 | int i; |
| 2963 | |
| 2964 | for (i = 0; i < NCH(n); ++i) { |
| 2965 | node *kid = CHILD(n, i); |
| 2966 | |
| 2967 | switch (TYPE(kid)) { |
| 2968 | case classdef: |
| 2969 | case funcdef: |
| 2970 | case lambdef: |
| 2971 | /* Stuff in nested functions & classes doesn't |
| 2972 | affect the code block we started in. */ |
| 2973 | return NULL; |
| 2974 | |
| 2975 | case return_stmt: |
| 2976 | if (NCH(kid) > 1) |
| 2977 | return kid; |
| 2978 | break; |
| 2979 | |
| 2980 | default: { |
| 2981 | node *bad = look_for_offending_return(kid); |
| 2982 | if (bad != NULL) |
| 2983 | return bad; |
| 2984 | } |
| 2985 | } |
| 2986 | } |
| 2987 | |
| 2988 | return NULL; |
| 2989 | } |
| 2990 | |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2991 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2992 | com_if_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2993 | { |
| 2994 | int i; |
| 2995 | int anchor = 0; |
| 2996 | REQ(n, if_stmt); |
| 2997 | /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */ |
| 2998 | for (i = 0; i+3 < NCH(n); i+=4) { |
| 2999 | int a = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3000 | node *ch = CHILD(n, i+1); |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3001 | if (is_constant_false(c, ch)) { |
| 3002 | /* We're going to skip this block. However, if this |
| 3003 | is a generator, we have to check the dead code |
| 3004 | anyway to make sure there aren't any return stmts |
| 3005 | with expressions, in the same scope. */ |
| 3006 | if (c->c_flags & CO_GENERATOR) { |
| 3007 | node *p = look_for_offending_return(n); |
| 3008 | if (p != NULL) { |
| 3009 | int savelineno = c->c_lineno; |
| 3010 | c->c_lineno = p->n_lineno; |
| 3011 | com_error(c, PyExc_SyntaxError, |
| 3012 | "'return' with argument " |
| 3013 | "inside generator"); |
| 3014 | c->c_lineno = savelineno; |
| 3015 | } |
| 3016 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3017 | continue; |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3018 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3019 | if (i > 0) |
| 3020 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3021 | com_node(c, ch); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3022 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 3023 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3024 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3025 | com_node(c, CHILD(n, i+3)); |
| 3026 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 3027 | com_backpatch(c, a); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3028 | /* We jump here with an extra entry which we now pop */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3029 | com_addbyte(c, POP_TOP); |
| 3030 | } |
| 3031 | if (i+2 < NCH(n)) |
| 3032 | com_node(c, CHILD(n, i+2)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3033 | if (anchor) |
| 3034 | com_backpatch(c, anchor); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3035 | } |
| 3036 | |
| 3037 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3038 | com_while_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3039 | { |
| 3040 | int break_anchor = 0; |
| 3041 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3042 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3043 | REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */ |
| 3044 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3045 | block_push(c, SETUP_LOOP); |
| 3046 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3047 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3048 | com_node(c, CHILD(n, 1)); |
| 3049 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 3050 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3051 | com_pop(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3052 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3053 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3054 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3055 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3056 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3057 | com_backpatch(c, anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3058 | /* We jump here with one entry more on the stack */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3059 | com_addbyte(c, POP_TOP); |
| 3060 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3061 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3062 | if (NCH(n) > 4) |
| 3063 | com_node(c, CHILD(n, 6)); |
| 3064 | com_backpatch(c, break_anchor); |
| 3065 | } |
| 3066 | |
| 3067 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3068 | com_for_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3069 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3070 | int break_anchor = 0; |
| 3071 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3072 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3073 | REQ(n, for_stmt); |
| 3074 | /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */ |
| 3075 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3076 | block_push(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3077 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3078 | com_addbyte(c, GET_ITER); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3079 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3080 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3081 | com_addfwref(c, FOR_ITER, &anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3082 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3083 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3084 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3085 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3086 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3087 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3088 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3089 | com_backpatch(c, anchor); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3090 | com_pop(c, 1); /* FOR_ITER has popped this */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3091 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3092 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3093 | if (NCH(n) > 8) |
| 3094 | com_node(c, CHILD(n, 8)); |
| 3095 | com_backpatch(c, break_anchor); |
| 3096 | } |
| 3097 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3098 | /* Code generated for "try: S finally: Sf" is as follows: |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3099 | |
| 3100 | SETUP_FINALLY L |
| 3101 | <code for S> |
| 3102 | POP_BLOCK |
| 3103 | LOAD_CONST <nil> |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3104 | L: <code for Sf> |
| 3105 | END_FINALLY |
| 3106 | |
| 3107 | The special instructions use the block stack. Each block |
| 3108 | stack entry contains the instruction that created it (here |
| 3109 | SETUP_FINALLY), the level of the value stack at the time the |
| 3110 | block stack entry was created, and a label (here L). |
| 3111 | |
| 3112 | SETUP_FINALLY: |
| 3113 | Pushes the current value stack level and the label |
| 3114 | onto the block stack. |
| 3115 | POP_BLOCK: |
| 3116 | Pops en entry from the block stack, and pops the value |
| 3117 | stack until its level is the same as indicated on the |
| 3118 | block stack. (The label is ignored.) |
| 3119 | END_FINALLY: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3120 | Pops a variable number of entries from the *value* stack |
| 3121 | and re-raises the exception they specify. The number of |
| 3122 | entries popped depends on the (pseudo) exception type. |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3123 | |
| 3124 | The block stack is unwound when an exception is raised: |
| 3125 | when a SETUP_FINALLY entry is found, the exception is pushed |
| 3126 | onto the value stack (and the exception condition is cleared), |
| 3127 | and the interpreter jumps to the label gotten from the block |
| 3128 | stack. |
| 3129 | |
| 3130 | 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] | 3131 | (The contents of the value stack is shown in [], with the top |
| 3132 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3133 | associated value, and 'exc' the exception.) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3134 | |
| 3135 | Value stack Label Instruction Argument |
| 3136 | [] SETUP_EXCEPT L1 |
| 3137 | [] <code for S> |
| 3138 | [] POP_BLOCK |
| 3139 | [] JUMP_FORWARD L0 |
| 3140 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3141 | [tb, val, exc] L1: DUP ) |
| 3142 | [tb, val, exc, exc] <evaluate E1> ) |
| 3143 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 3144 | [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 ) |
| 3145 | [tb, val, exc, 1] POP ) |
| 3146 | [tb, val, exc] POP |
| 3147 | [tb, val] <assign to V1> (or POP if no V1) |
| 3148 | [tb] POP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3149 | [] <code for S1> |
| 3150 | JUMP_FORWARD L0 |
| 3151 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3152 | [tb, val, exc, 0] L2: POP |
| 3153 | [tb, val, exc] DUP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3154 | .............................etc....................... |
| 3155 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3156 | [tb, val, exc, 0] Ln+1: POP |
| 3157 | [tb, val, exc] END_FINALLY # re-raise exception |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3158 | |
| 3159 | [] L0: <next statement> |
| 3160 | |
| 3161 | Of course, parts are not generated if Vi or Ei is not present. |
| 3162 | */ |
| 3163 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3164 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3165 | com_try_except(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3166 | { |
| 3167 | int except_anchor = 0; |
| 3168 | int end_anchor = 0; |
| 3169 | int else_anchor = 0; |
| 3170 | int i; |
| 3171 | node *ch; |
| 3172 | |
| 3173 | com_addfwref(c, SETUP_EXCEPT, &except_anchor); |
| 3174 | block_push(c, SETUP_EXCEPT); |
| 3175 | com_node(c, CHILD(n, 2)); |
| 3176 | com_addbyte(c, POP_BLOCK); |
| 3177 | block_pop(c, SETUP_EXCEPT); |
| 3178 | com_addfwref(c, JUMP_FORWARD, &else_anchor); |
| 3179 | com_backpatch(c, except_anchor); |
| 3180 | for (i = 3; |
| 3181 | i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause; |
| 3182 | i += 3) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3183 | /* except_clause: 'except' [expr [',' var]] */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3184 | if (except_anchor == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3185 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 3186 | "default 'except:' must be last"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3187 | break; |
| 3188 | } |
| 3189 | except_anchor = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3190 | com_push(c, 3); /* tb, val, exc pushed by exception */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3191 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 3192 | if (NCH(ch) > 1) { |
| 3193 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3194 | com_push(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3195 | com_node(c, CHILD(ch, 1)); |
| 3196 | com_addoparg(c, COMPARE_OP, EXC_MATCH); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3197 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3198 | com_addfwref(c, JUMP_IF_FALSE, &except_anchor); |
| 3199 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3200 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3201 | } |
| 3202 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3203 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3204 | if (NCH(ch) > 3) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3205 | com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3206 | else { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3207 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3208 | com_pop(c, 1); |
| 3209 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3210 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3211 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3212 | com_node(c, CHILD(n, i+2)); |
| 3213 | com_addfwref(c, JUMP_FORWARD, &end_anchor); |
| 3214 | if (except_anchor) { |
| 3215 | com_backpatch(c, except_anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3216 | /* We come in with [tb, val, exc, 0] on the |
| 3217 | stack; one pop and it's the same as |
| 3218 | expected at the start of the loop */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3219 | com_addbyte(c, POP_TOP); |
| 3220 | } |
| 3221 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3222 | /* We actually come in here with [tb, val, exc] but the |
| 3223 | END_FINALLY will zap those and jump around. |
| 3224 | The c_stacklevel does not reflect them so we need not pop |
| 3225 | anything. */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3226 | com_addbyte(c, END_FINALLY); |
| 3227 | com_backpatch(c, else_anchor); |
| 3228 | if (i < NCH(n)) |
| 3229 | com_node(c, CHILD(n, i+2)); |
| 3230 | com_backpatch(c, end_anchor); |
| 3231 | } |
| 3232 | |
| 3233 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3234 | com_try_finally(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3235 | { |
| 3236 | int finally_anchor = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3237 | node *ch; |
Guido van Rossum | 94fb82e | 1992-04-05 14:24:50 +0000 | [diff] [blame] | 3238 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3239 | com_addfwref(c, SETUP_FINALLY, &finally_anchor); |
| 3240 | block_push(c, SETUP_FINALLY); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3241 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3242 | com_addbyte(c, POP_BLOCK); |
| 3243 | block_pop(c, SETUP_FINALLY); |
| 3244 | block_push(c, END_FINALLY); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3245 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3246 | /* While the generated code pushes only one item, |
| 3247 | the try-finally handling can enter here with |
| 3248 | up to three items. OK, here are the details: |
| 3249 | 3 for an exception, 2 for RETURN, 1 for BREAK. */ |
| 3250 | com_push(c, 3); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3251 | com_backpatch(c, finally_anchor); |
| 3252 | ch = CHILD(n, NCH(n)-1); |
| 3253 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 3254 | com_node(c, ch); |
| 3255 | com_addbyte(c, END_FINALLY); |
| 3256 | block_pop(c, END_FINALLY); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3257 | com_pop(c, 3); /* Matches the com_push above */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3258 | } |
| 3259 | |
| 3260 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3261 | com_try_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3262 | { |
| 3263 | REQ(n, try_stmt); |
| 3264 | /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
| 3265 | | 'try' ':' suite 'finally' ':' suite */ |
| 3266 | if (TYPE(CHILD(n, 3)) != except_clause) |
| 3267 | com_try_finally(c, n); |
| 3268 | else |
| 3269 | com_try_except(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3270 | } |
| 3271 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3272 | static node * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3273 | get_rawdocstring(node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3274 | { |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3275 | int i; |
| 3276 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3277 | /* Label to avoid tail recursion */ |
| 3278 | next: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3279 | switch (TYPE(n)) { |
| 3280 | |
| 3281 | case suite: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3282 | if (NCH(n) == 1) { |
| 3283 | n = CHILD(n, 0); |
| 3284 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3285 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3286 | /* Fall through */ |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3287 | case file_input: |
| 3288 | for (i = 0; i < NCH(n); i++) { |
| 3289 | node *ch = CHILD(n, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3290 | if (TYPE(ch) == stmt) { |
| 3291 | n = ch; |
| 3292 | goto next; |
| 3293 | } |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3294 | } |
| 3295 | break; |
| 3296 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3297 | case stmt: |
| 3298 | case simple_stmt: |
| 3299 | case small_stmt: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3300 | n = CHILD(n, 0); |
| 3301 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3302 | |
| 3303 | case expr_stmt: |
| 3304 | case testlist: |
| 3305 | case test: |
| 3306 | case and_test: |
| 3307 | case not_test: |
| 3308 | case comparison: |
| 3309 | case expr: |
| 3310 | case xor_expr: |
| 3311 | case and_expr: |
| 3312 | case shift_expr: |
| 3313 | case arith_expr: |
| 3314 | case term: |
| 3315 | case factor: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3316 | case power: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3317 | if (NCH(n) == 1) { |
| 3318 | n = CHILD(n, 0); |
| 3319 | goto next; |
| 3320 | } |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3321 | break; |
| 3322 | |
| 3323 | case atom: |
| 3324 | if (TYPE(CHILD(n, 0)) == STRING) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3325 | return n; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3326 | break; |
| 3327 | |
| 3328 | } |
| 3329 | return NULL; |
| 3330 | } |
| 3331 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3332 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3333 | get_docstring(node *n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3334 | { |
Guido van Rossum | 541563e | 1999-01-28 15:08:09 +0000 | [diff] [blame] | 3335 | /* Don't generate doc-strings if run with -OO */ |
| 3336 | if (Py_OptimizeFlag > 1) |
| 3337 | return NULL; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3338 | n = get_rawdocstring(n); |
| 3339 | if (n == NULL) |
| 3340 | return NULL; |
| 3341 | return parsestrplus(n); |
| 3342 | } |
| 3343 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3344 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3345 | com_suite(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3346 | { |
| 3347 | REQ(n, suite); |
| 3348 | /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */ |
| 3349 | if (NCH(n) == 1) { |
| 3350 | com_node(c, CHILD(n, 0)); |
| 3351 | } |
| 3352 | else { |
| 3353 | int i; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3354 | for (i = 0; i < NCH(n) && c->c_errors == 0; i++) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3355 | node *ch = CHILD(n, i); |
| 3356 | if (TYPE(ch) == stmt) |
| 3357 | com_node(c, ch); |
| 3358 | } |
| 3359 | } |
| 3360 | } |
| 3361 | |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3362 | /* ARGSUSED */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3363 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3364 | com_continue_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3365 | { |
| 3366 | int i = c->c_nblocks; |
| 3367 | if (i-- > 0 && c->c_block[i] == SETUP_LOOP) { |
| 3368 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3369 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3370 | else if (i <= 0) { |
| 3371 | /* at the outer level */ |
| 3372 | com_error(c, PyExc_SyntaxError, |
| 3373 | "'continue' not properly in loop"); |
| 3374 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3375 | else { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3376 | int j; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3377 | for (j = i-1; j >= 0; --j) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3378 | if (c->c_block[j] == SETUP_LOOP) |
| 3379 | break; |
| 3380 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3381 | if (j >= 0) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3382 | /* there is a loop, but something interferes */ |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3383 | for (; i > j; --i) { |
| 3384 | if (c->c_block[i] == SETUP_EXCEPT || |
| 3385 | c->c_block[i] == SETUP_FINALLY) { |
| 3386 | com_addoparg(c, CONTINUE_LOOP, |
| 3387 | c->c_begin); |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3388 | return; |
| 3389 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3390 | if (c->c_block[i] == END_FINALLY) { |
| 3391 | com_error(c, PyExc_SyntaxError, |
| 3392 | "'continue' not supported inside 'finally' clause"); |
| 3393 | return; |
| 3394 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3395 | } |
| 3396 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3397 | com_error(c, PyExc_SyntaxError, |
| 3398 | "'continue' not properly in loop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3399 | } |
| 3400 | /* XXX Could allow it inside a 'finally' clause |
| 3401 | XXX if we could pop the exception still on the stack */ |
| 3402 | } |
| 3403 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3404 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3405 | com_argdefs(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3406 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3407 | int i, nch, nargs, ndefs; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3408 | if (TYPE(n) == lambdef) { |
| 3409 | /* lambdef: 'lambda' [varargslist] ':' test */ |
| 3410 | n = CHILD(n, 1); |
| 3411 | } |
| 3412 | else { |
| 3413 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */ |
| 3414 | n = CHILD(n, 2); |
| 3415 | REQ(n, parameters); /* parameters: '(' [varargslist] ')' */ |
| 3416 | n = CHILD(n, 1); |
| 3417 | } |
| 3418 | if (TYPE(n) != varargslist) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3419 | return 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3420 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3421 | (fpdef ['=' test] ',')* '*' ....... | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3422 | fpdef ['=' test] (',' fpdef ['=' test])* [','] */ |
| 3423 | nch = NCH(n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3424 | nargs = 0; |
| 3425 | ndefs = 0; |
| 3426 | for (i = 0; i < nch; i++) { |
| 3427 | int t; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3428 | if (TYPE(CHILD(n, i)) == STAR || |
| 3429 | TYPE(CHILD(n, i)) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3430 | break; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3431 | nargs++; |
| 3432 | i++; |
| 3433 | if (i >= nch) |
Guido van Rossum | a1e7e62 | 1995-09-18 21:44:04 +0000 | [diff] [blame] | 3434 | t = RPAR; /* Anything except EQUAL or COMMA */ |
| 3435 | else |
| 3436 | t = TYPE(CHILD(n, i)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3437 | if (t == EQUAL) { |
| 3438 | i++; |
| 3439 | ndefs++; |
| 3440 | com_node(c, CHILD(n, i)); |
| 3441 | i++; |
| 3442 | if (i >= nch) |
| 3443 | break; |
| 3444 | t = TYPE(CHILD(n, i)); |
| 3445 | } |
| 3446 | else { |
Guido van Rossum | 29d38cd | 1998-10-02 13:41:54 +0000 | [diff] [blame] | 3447 | /* Treat "(a=1, b)" as an error */ |
| 3448 | if (ndefs) |
| 3449 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | dfede31 | 1998-10-02 14:06:56 +0000 | [diff] [blame] | 3450 | "non-default argument follows default argument"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3451 | } |
| 3452 | if (t != COMMA) |
| 3453 | break; |
| 3454 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3455 | return ndefs; |
| 3456 | } |
| 3457 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3458 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3459 | com_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3460 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3461 | PyObject *co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3462 | int ndefs; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3463 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3464 | ndefs = com_argdefs(c, n); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3465 | symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n), |
| 3466 | 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 | 10dc2e8 | 1990-11-18 17:27:39 +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 | int i = com_addconst(c, co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +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, ndefs); |
| 3478 | else |
| 3479 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3480 | com_pop(c, ndefs); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3481 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3482 | com_pop(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3483 | Py_DECREF(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3484 | } |
| 3485 | } |
| 3486 | |
| 3487 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3488 | com_bases(struct compiling *c, node *n) |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3489 | { |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3490 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3491 | REQ(n, testlist); |
| 3492 | /* testlist: test (',' test)* [','] */ |
| 3493 | for (i = 0; i < NCH(n); i += 2) |
| 3494 | com_node(c, CHILD(n, i)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3495 | i = (NCH(n)+1) / 2; |
| 3496 | com_addoparg(c, BUILD_TUPLE, i); |
| 3497 | com_pop(c, i-1); |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3498 | } |
| 3499 | |
| 3500 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3501 | com_classdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3502 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3503 | int i; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3504 | PyObject *co, *v; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3505 | char *name; |
| 3506 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3507 | REQ(n, classdef); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3508 | /* classdef: class NAME ['(' testlist ')'] ':' suite */ |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 3509 | if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3510 | c->c_errors++; |
| 3511 | return; |
| 3512 | } |
| 3513 | /* Push the class name on the stack */ |
| 3514 | i = com_addconst(c, v); |
| 3515 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3516 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3517 | Py_DECREF(v); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3518 | /* Push the tuple of base classes on the stack */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3519 | if (TYPE(CHILD(n, 2)) != LPAR) { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3520 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3521 | com_push(c, 1); |
| 3522 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3523 | else |
| 3524 | com_bases(c, CHILD(n, 3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3525 | name = STR(CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3526 | symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3527 | co = (PyObject *)icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3528 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3529 | if (co == NULL) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3530 | c->c_errors++; |
| 3531 | else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3532 | int closure = com_make_closure(c, (PyCodeObject *)co); |
| 3533 | i = com_addconst(c, co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3534 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3535 | com_push(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3536 | if (closure) |
| 3537 | com_addoparg(c, MAKE_CLOSURE, 0); |
| 3538 | else |
| 3539 | com_addoparg(c, MAKE_FUNCTION, 0); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3540 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3541 | com_addbyte(c, BUILD_CLASS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3542 | com_pop(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3543 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3544 | Py_DECREF(co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3545 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3546 | } |
| 3547 | |
| 3548 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3549 | com_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3550 | { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3551 | loop: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3552 | if (c->c_errors) |
| 3553 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3554 | switch (TYPE(n)) { |
| 3555 | |
| 3556 | /* Definition nodes */ |
| 3557 | |
| 3558 | case funcdef: |
| 3559 | com_funcdef(c, n); |
| 3560 | break; |
| 3561 | case classdef: |
| 3562 | com_classdef(c, n); |
| 3563 | break; |
| 3564 | |
| 3565 | /* Trivial parse tree nodes */ |
| 3566 | |
| 3567 | case stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3568 | case small_stmt: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3569 | case flow_stmt: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3570 | n = CHILD(n, 0); |
| 3571 | goto loop; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3572 | |
| 3573 | case simple_stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3574 | /* small_stmt (';' small_stmt)* [';'] NEWLINE */ |
| 3575 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 3576 | { |
| 3577 | int i; |
| 3578 | for (i = 0; i < NCH(n)-1; i += 2) |
| 3579 | com_node(c, CHILD(n, i)); |
| 3580 | } |
| 3581 | break; |
| 3582 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3583 | case compound_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3584 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3585 | n = CHILD(n, 0); |
| 3586 | goto loop; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3587 | |
| 3588 | /* Statement nodes */ |
| 3589 | |
| 3590 | case expr_stmt: |
| 3591 | com_expr_stmt(c, n); |
| 3592 | break; |
| 3593 | case print_stmt: |
| 3594 | com_print_stmt(c, n); |
| 3595 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3596 | case del_stmt: /* 'del' exprlist */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3597 | com_assign(c, CHILD(n, 1), OP_DELETE, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3598 | break; |
| 3599 | case pass_stmt: |
| 3600 | break; |
| 3601 | case break_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3602 | if (c->c_loops == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3603 | com_error(c, PyExc_SyntaxError, |
| 3604 | "'break' outside loop"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3605 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3606 | com_addbyte(c, BREAK_LOOP); |
| 3607 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3608 | case continue_stmt: |
| 3609 | com_continue_stmt(c, n); |
| 3610 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3611 | case return_stmt: |
| 3612 | com_return_stmt(c, n); |
| 3613 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3614 | case yield_stmt: |
| 3615 | com_yield_stmt(c, n); |
| 3616 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3617 | case raise_stmt: |
| 3618 | com_raise_stmt(c, n); |
| 3619 | break; |
| 3620 | case import_stmt: |
| 3621 | com_import_stmt(c, n); |
| 3622 | break; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3623 | case global_stmt: |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3624 | break; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3625 | case exec_stmt: |
| 3626 | com_exec_stmt(c, n); |
| 3627 | break; |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 3628 | case assert_stmt: |
| 3629 | com_assert_stmt(c, n); |
| 3630 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3631 | case if_stmt: |
| 3632 | com_if_stmt(c, n); |
| 3633 | break; |
| 3634 | case while_stmt: |
| 3635 | com_while_stmt(c, n); |
| 3636 | break; |
| 3637 | case for_stmt: |
| 3638 | com_for_stmt(c, n); |
| 3639 | break; |
| 3640 | case try_stmt: |
| 3641 | com_try_stmt(c, n); |
| 3642 | break; |
| 3643 | case suite: |
| 3644 | com_suite(c, n); |
| 3645 | break; |
| 3646 | |
| 3647 | /* Expression nodes */ |
| 3648 | |
| 3649 | case testlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3650 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3651 | break; |
| 3652 | case test: |
| 3653 | com_test(c, n); |
| 3654 | break; |
| 3655 | case and_test: |
| 3656 | com_and_test(c, n); |
| 3657 | break; |
| 3658 | case not_test: |
| 3659 | com_not_test(c, n); |
| 3660 | break; |
| 3661 | case comparison: |
| 3662 | com_comparison(c, n); |
| 3663 | break; |
| 3664 | case exprlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3665 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3666 | break; |
| 3667 | case expr: |
| 3668 | com_expr(c, n); |
| 3669 | break; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 3670 | case xor_expr: |
| 3671 | com_xor_expr(c, n); |
| 3672 | break; |
| 3673 | case and_expr: |
| 3674 | com_and_expr(c, n); |
| 3675 | break; |
| 3676 | case shift_expr: |
| 3677 | com_shift_expr(c, n); |
| 3678 | break; |
| 3679 | case arith_expr: |
| 3680 | com_arith_expr(c, n); |
| 3681 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3682 | case term: |
| 3683 | com_term(c, n); |
| 3684 | break; |
| 3685 | case factor: |
| 3686 | com_factor(c, n); |
| 3687 | break; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3688 | case power: |
| 3689 | com_power(c, n); |
| 3690 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3691 | case atom: |
| 3692 | com_atom(c, n); |
| 3693 | break; |
| 3694 | |
| 3695 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3696 | com_error(c, PyExc_SystemError, |
| 3697 | "com_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3698 | } |
| 3699 | } |
| 3700 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 3701 | static void com_fplist(struct compiling *, node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3702 | |
| 3703 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3704 | com_fpdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3705 | { |
| 3706 | REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3707 | if (TYPE(CHILD(n, 0)) == LPAR) |
| 3708 | com_fplist(c, CHILD(n, 1)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3709 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3710 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3711 | com_pop(c, 1); |
| 3712 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3713 | } |
| 3714 | |
| 3715 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3716 | com_fplist(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3717 | { |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 3718 | REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3719 | if (NCH(n) == 1) { |
| 3720 | com_fpdef(c, CHILD(n, 0)); |
| 3721 | } |
| 3722 | else { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3723 | int i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 3724 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3725 | com_push(c, i-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3726 | for (i = 0; i < NCH(n); i += 2) |
| 3727 | com_fpdef(c, CHILD(n, i)); |
| 3728 | } |
| 3729 | } |
| 3730 | |
| 3731 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3732 | com_arglist(struct compiling *c, node *n) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3733 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3734 | int nch, i, narg; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3735 | int complex = 0; |
Guido van Rossum | db9e20f | 1997-07-10 01:06:53 +0000 | [diff] [blame] | 3736 | char nbuf[10]; |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3737 | REQ(n, varargslist); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 3738 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3739 | (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3740 | nch = NCH(n); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3741 | /* Enter all arguments in table of locals */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3742 | for (i = 0, narg = 0; i < nch; i++) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3743 | node *ch = CHILD(n, i); |
| 3744 | node *fp; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3745 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3746 | break; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3747 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3748 | fp = CHILD(ch, 0); |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 3749 | if (TYPE(fp) != NAME) { |
Guido van Rossum | db9e20f | 1997-07-10 01:06:53 +0000 | [diff] [blame] | 3750 | sprintf(nbuf, ".%d", i); |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 3751 | complex = 1; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3752 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3753 | narg++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3754 | /* all name updates handled by symtable */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3755 | if (++i >= nch) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3756 | break; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3757 | ch = CHILD(n, i); |
| 3758 | if (TYPE(ch) == EQUAL) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3759 | i += 2; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3760 | else |
| 3761 | REQ(ch, COMMA); |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3762 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3763 | if (complex) { |
| 3764 | /* Generate code for complex arguments only after |
| 3765 | having counted the simple arguments */ |
| 3766 | int ilocal = 0; |
| 3767 | for (i = 0; i < nch; i++) { |
| 3768 | node *ch = CHILD(n, i); |
| 3769 | node *fp; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3770 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3771 | break; |
| 3772 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3773 | fp = CHILD(ch, 0); |
| 3774 | if (TYPE(fp) != NAME) { |
| 3775 | com_addoparg(c, LOAD_FAST, ilocal); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3776 | com_push(c, 1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3777 | com_fpdef(c, ch); |
| 3778 | } |
| 3779 | ilocal++; |
| 3780 | if (++i >= nch) |
| 3781 | break; |
| 3782 | ch = CHILD(n, i); |
| 3783 | if (TYPE(ch) == EQUAL) |
| 3784 | i += 2; |
| 3785 | else |
| 3786 | REQ(ch, COMMA); |
| 3787 | } |
| 3788 | } |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3789 | } |
| 3790 | |
| 3791 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3792 | com_file_input(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3793 | { |
| 3794 | int i; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3795 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3796 | REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */ |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3797 | doc = get_docstring(n); |
| 3798 | if (doc != NULL) { |
| 3799 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3800 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3801 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3802 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3803 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3804 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3805 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3806 | for (i = 0; i < NCH(n); i++) { |
| 3807 | node *ch = CHILD(n, i); |
| 3808 | if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE) |
| 3809 | com_node(c, ch); |
| 3810 | } |
| 3811 | } |
| 3812 | |
| 3813 | /* Top-level compile-node interface */ |
| 3814 | |
| 3815 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3816 | compile_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3817 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3818 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3819 | node *ch; |
| 3820 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3821 | c->c_name = STR(CHILD(n, 1)); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3822 | doc = get_docstring(CHILD(n, 4)); |
| 3823 | if (doc != NULL) { |
| 3824 | (void) com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3825 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3826 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3827 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3828 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3829 | ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */ |
| 3830 | ch = CHILD(ch, 1); /* ')' | varargslist */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3831 | if (TYPE(ch) == varargslist) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3832 | com_arglist(c, ch); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3833 | c->c_infunction = 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3834 | com_node(c, CHILD(n, 4)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3835 | c->c_infunction = 0; |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 3836 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
| 3837 | com_push(c, 1); |
| 3838 | com_addbyte(c, RETURN_VALUE); |
| 3839 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3840 | } |
| 3841 | |
| 3842 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3843 | compile_lambdef(struct compiling *c, node *n) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 3844 | { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3845 | node *ch; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3846 | REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */ |
Guido van Rossum | 363ac7d | 1994-11-10 22:40:34 +0000 | [diff] [blame] | 3847 | c->c_name = "<lambda>"; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3848 | |
| 3849 | ch = CHILD(n, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3850 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3851 | if (TYPE(ch) == varargslist) { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3852 | com_arglist(c, ch); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3853 | ch = CHILD(n, 3); |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3854 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3855 | else |
| 3856 | ch = CHILD(n, 2); |
| 3857 | com_node(c, ch); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 3858 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3859 | com_pop(c, 1); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 3860 | } |
| 3861 | |
| 3862 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3863 | compile_classdef(struct compiling *c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3864 | { |
| 3865 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3866 | PyObject *doc; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3867 | REQ(n, classdef); |
| 3868 | /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ |
| 3869 | c->c_name = STR(CHILD(n, 1)); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 3870 | c->c_private = c->c_name; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3871 | ch = CHILD(n, NCH(n)-1); /* The suite */ |
| 3872 | doc = get_docstring(ch); |
| 3873 | if (doc != NULL) { |
| 3874 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3875 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3876 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3877 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3878 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3879 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3880 | } |
| 3881 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3882 | (void) com_addconst(c, Py_None); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3883 | com_node(c, ch); |
| 3884 | com_addbyte(c, LOAD_LOCALS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3885 | com_push(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3886 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3887 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3888 | } |
| 3889 | |
| 3890 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3891 | compile_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3892 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3893 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 3894 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3895 | switch (TYPE(n)) { |
| 3896 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3897 | case single_input: /* One interactive command */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3898 | /* NEWLINE | simple_stmt | compound_stmt NEWLINE */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3899 | c->c_interactive++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3900 | n = CHILD(n, 0); |
| 3901 | if (TYPE(n) != NEWLINE) |
| 3902 | com_node(c, n); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3903 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3904 | com_push(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3905 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3906 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3907 | c->c_interactive--; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3908 | break; |
| 3909 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3910 | case file_input: /* A whole file, or built-in function exec() */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3911 | com_file_input(c, n); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3912 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3913 | com_push(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3914 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3915 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3916 | break; |
| 3917 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3918 | case eval_input: /* Built-in function input() */ |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3919 | com_node(c, CHILD(n, 0)); |
| 3920 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3921 | com_pop(c, 1); |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3922 | break; |
| 3923 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3924 | case lambdef: /* anonymous function definition */ |
| 3925 | compile_lambdef(c, n); |
| 3926 | break; |
| 3927 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3928 | case funcdef: /* A function definition */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3929 | compile_funcdef(c, n); |
| 3930 | break; |
| 3931 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3932 | case classdef: /* A class definition */ |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3933 | compile_classdef(c, n); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3934 | break; |
| 3935 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3936 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3937 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 3938 | "compile_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3939 | } |
| 3940 | } |
| 3941 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3942 | static PyObject * |
| 3943 | dict_keys_inorder(PyObject *dict, int offset) |
| 3944 | { |
| 3945 | PyObject *tuple, *k, *v; |
| 3946 | int i, pos = 0, size = PyDict_Size(dict); |
| 3947 | |
| 3948 | tuple = PyTuple_New(size); |
| 3949 | if (tuple == NULL) |
| 3950 | return NULL; |
| 3951 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 3952 | i = PyInt_AS_LONG(v); |
| 3953 | Py_INCREF(k); |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 3954 | assert((i - offset) < size); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3955 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 3956 | } |
| 3957 | return tuple; |
| 3958 | } |
| 3959 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3960 | PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3961 | PyNode_Compile(node *n, char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3962 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 3963 | return PyNode_CompileFlags(n, filename, NULL); |
| 3964 | } |
| 3965 | |
| 3966 | PyCodeObject * |
| 3967 | PyNode_CompileFlags(node *n, char *filename, PyCompilerFlags *flags) |
| 3968 | { |
| 3969 | return jcompile(n, filename, NULL, flags); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 3970 | } |
| 3971 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3972 | struct symtable * |
| 3973 | PyNode_CompileSymtable(node *n, char *filename) |
| 3974 | { |
| 3975 | struct symtable *st; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 3976 | PyFutureFeatures *ff; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3977 | |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 3978 | ff = PyNode_Future(n, filename); |
| 3979 | if (ff == NULL) |
| 3980 | return NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3981 | st = symtable_init(); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3982 | if (st == NULL) |
| 3983 | return NULL; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 3984 | st->st_future = ff; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3985 | symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 3986 | if (st->st_errors > 0) |
| 3987 | goto fail; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3988 | symtable_node(st, n); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 3989 | if (st->st_errors > 0) |
| 3990 | goto fail; |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 3991 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3992 | return st; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 3993 | fail: |
| 3994 | PyMem_Free((void *)ff); |
| 3995 | st->st_future = NULL; |
| 3996 | PySymtable_Free(st); |
| 3997 | return NULL; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3998 | } |
| 3999 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4000 | static PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4001 | icompile(node *n, struct compiling *base) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4002 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4003 | return jcompile(n, base->c_filename, base, NULL); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4004 | } |
| 4005 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4006 | static PyCodeObject * |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4007 | jcompile(node *n, char *filename, struct compiling *base, |
| 4008 | PyCompilerFlags *flags) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4009 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4010 | struct compiling sc; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4011 | PyCodeObject *co; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4012 | if (!com_init(&sc, filename)) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4013 | return NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4014 | if (base) { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4015 | sc.c_private = base->c_private; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4016 | sc.c_symtable = base->c_symtable; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4017 | /* c_symtable still points to parent's symbols */ |
| 4018 | if (base->c_nested |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4019 | || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4020 | sc.c_nested = 1; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 4021 | sc.c_flags |= base->c_flags & (CO_GENERATOR_ALLOWED | |
| 4022 | CO_FUTURE_DIVISION); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4023 | } else { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4024 | sc.c_private = NULL; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 4025 | sc.c_future = PyNode_Future(n, filename); |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4026 | if (sc.c_future == NULL) { |
| 4027 | com_free(&sc); |
| 4028 | return NULL; |
| 4029 | } |
Jeremy Hylton | b857ba2 | 2001-08-10 21:41:33 +0000 | [diff] [blame] | 4030 | if (flags) |
| 4031 | sc.c_future->ff_features |= flags->cf_flags; |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4032 | if (symtable_build(&sc, n) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4033 | com_free(&sc); |
| 4034 | return NULL; |
| 4035 | } |
| 4036 | } |
| 4037 | co = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4038 | if (symtable_load_symbols(&sc) < 0) { |
| 4039 | sc.c_errors++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4040 | goto exit; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4041 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4042 | compile_node(&sc, n); |
| 4043 | com_done(&sc); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 4044 | if (sc.c_errors == 0) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4045 | PyObject *consts, *names, *varnames, *filename, *name, |
| 4046 | *freevars, *cellvars; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4047 | consts = PyList_AsTuple(sc.c_consts); |
| 4048 | names = PyList_AsTuple(sc.c_names); |
| 4049 | varnames = PyList_AsTuple(sc.c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4050 | cellvars = dict_keys_inorder(sc.c_cellvars, 0); |
| 4051 | freevars = dict_keys_inorder(sc.c_freevars, |
| 4052 | PyTuple_GET_SIZE(cellvars)); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 4053 | filename = PyString_InternFromString(sc.c_filename); |
| 4054 | name = PyString_InternFromString(sc.c_name); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4055 | if (!PyErr_Occurred()) |
| 4056 | co = PyCode_New(sc.c_argcount, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4057 | sc.c_nlocals, |
| 4058 | sc.c_maxstacklevel, |
| 4059 | sc.c_flags, |
| 4060 | sc.c_code, |
| 4061 | consts, |
| 4062 | names, |
| 4063 | varnames, |
| 4064 | freevars, |
| 4065 | cellvars, |
| 4066 | filename, |
| 4067 | name, |
| 4068 | sc.c_firstlineno, |
| 4069 | sc.c_lnotab); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4070 | Py_XDECREF(consts); |
| 4071 | Py_XDECREF(names); |
| 4072 | Py_XDECREF(varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4073 | Py_XDECREF(freevars); |
| 4074 | Py_XDECREF(cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4075 | Py_XDECREF(filename); |
| 4076 | Py_XDECREF(name); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 4077 | } |
Guido van Rossum | 6c2f0c7 | 2000-08-07 19:22:43 +0000 | [diff] [blame] | 4078 | else if (!PyErr_Occurred()) { |
| 4079 | /* This could happen if someone called PyErr_Clear() after an |
| 4080 | error was reported above. That's not supposed to happen, |
| 4081 | but I just plugged one case and I'm not sure there can't be |
| 4082 | others. In that case, raise SystemError so that at least |
| 4083 | it gets reported instead dumping core. */ |
| 4084 | PyErr_SetString(PyExc_SystemError, "lost syntax error"); |
| 4085 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4086 | exit: |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4087 | if (base == NULL) { |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4088 | PySymtable_Free(sc.c_symtable); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4089 | sc.c_symtable = NULL; |
| 4090 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4091 | com_free(&sc); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4092 | return co; |
| 4093 | } |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4094 | |
| 4095 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4096 | PyCode_Addr2Line(PyCodeObject *co, int addrq) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4097 | { |
| 4098 | int size = PyString_Size(co->co_lnotab) / 2; |
Guido van Rossum | 2174dcb | 1999-09-15 22:48:09 +0000 | [diff] [blame] | 4099 | unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4100 | int line = co->co_firstlineno; |
| 4101 | int addr = 0; |
| 4102 | while (--size >= 0) { |
| 4103 | addr += *p++; |
| 4104 | if (addr > addrq) |
| 4105 | break; |
| 4106 | line += *p++; |
| 4107 | } |
| 4108 | return line; |
| 4109 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4110 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4111 | /* The test for LOCAL must come before the test for FREE in order to |
| 4112 | handle classes where name is both local and free. The local var is |
| 4113 | a method and the free var is a free var referenced within a method. |
| 4114 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4115 | |
| 4116 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4117 | get_ref_type(struct compiling *c, char *name) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4118 | { |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4119 | char buf[350]; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4120 | PyObject *v; |
Jeremy Hylton | 5125773 | 2001-03-01 00:42:55 +0000 | [diff] [blame] | 4121 | |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4122 | if (PyDict_GetItemString(c->c_cellvars, name) != NULL) |
| 4123 | return CELL; |
| 4124 | if (PyDict_GetItemString(c->c_locals, name) != NULL) |
| 4125 | return LOCAL; |
| 4126 | if (PyDict_GetItemString(c->c_freevars, name) != NULL) |
| 4127 | return FREE; |
| 4128 | v = PyDict_GetItemString(c->c_globals, name); |
| 4129 | if (v) { |
| 4130 | if (v == Py_None) |
| 4131 | return GLOBAL_EXPLICIT; |
| 4132 | else { |
| 4133 | return GLOBAL_IMPLICIT; |
| 4134 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4135 | } |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4136 | sprintf(buf, |
| 4137 | "unknown scope for %.100s in %.100s(%s) " |
| 4138 | "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n", |
| 4139 | name, c->c_name, |
| 4140 | PyObject_REPR(c->c_symtable->st_cur->ste_id), |
| 4141 | c->c_filename, |
| 4142 | PyObject_REPR(c->c_symtable->st_cur->ste_symbols), |
| 4143 | PyObject_REPR(c->c_locals), |
| 4144 | PyObject_REPR(c->c_globals) |
| 4145 | ); |
| 4146 | |
| 4147 | Py_FatalError(buf); |
| 4148 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4149 | } |
| 4150 | |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4151 | /* Helper functions to issue warnings */ |
| 4152 | |
| 4153 | static int |
| 4154 | issue_warning(char *msg, char *filename, int lineno) |
| 4155 | { |
| 4156 | if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename, |
| 4157 | lineno, NULL, NULL) < 0) { |
| 4158 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
| 4159 | PyErr_SetString(PyExc_SyntaxError, msg); |
| 4160 | PyErr_SyntaxLocation(filename, lineno); |
| 4161 | } |
| 4162 | return -1; |
| 4163 | } |
| 4164 | return 0; |
| 4165 | } |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4166 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4167 | static int |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4168 | symtable_warn(struct symtable *st, char *msg) |
| 4169 | { |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4170 | 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] | 4171 | st->st_errors++; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4172 | return -1; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4173 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4174 | return 0; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4175 | } |
| 4176 | |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 4177 | /* Helper function for setting lineno and filename */ |
| 4178 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4179 | static int |
| 4180 | symtable_build(struct compiling *c, node *n) |
| 4181 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4182 | if ((c->c_symtable = symtable_init()) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4183 | return -1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 4184 | c->c_symtable->st_future = c->c_future; |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4185 | c->c_symtable->st_filename = c->c_filename; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4186 | symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno); |
| 4187 | if (c->c_symtable->st_errors > 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4188 | return -1; |
| 4189 | symtable_node(c->c_symtable, n); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4190 | if (c->c_symtable->st_errors > 0) |
| 4191 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4192 | /* reset for second pass */ |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4193 | c->c_symtable->st_nscopes = 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4194 | c->c_symtable->st_pass = 2; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4195 | return 0; |
| 4196 | } |
| 4197 | |
| 4198 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4199 | symtable_init_compiling_symbols(struct compiling *c) |
| 4200 | { |
| 4201 | PyObject *varnames; |
| 4202 | |
| 4203 | varnames = c->c_symtable->st_cur->ste_varnames; |
| 4204 | if (varnames == NULL) { |
| 4205 | varnames = PyList_New(0); |
| 4206 | if (varnames == NULL) |
| 4207 | return -1; |
| 4208 | c->c_symtable->st_cur->ste_varnames = varnames; |
| 4209 | Py_INCREF(varnames); |
| 4210 | } else |
| 4211 | Py_INCREF(varnames); |
| 4212 | c->c_varnames = varnames; |
| 4213 | |
| 4214 | c->c_globals = PyDict_New(); |
| 4215 | if (c->c_globals == NULL) |
| 4216 | return -1; |
| 4217 | c->c_freevars = PyDict_New(); |
| 4218 | if (c->c_freevars == NULL) |
| 4219 | return -1; |
| 4220 | c->c_cellvars = PyDict_New(); |
| 4221 | if (c->c_cellvars == NULL) |
| 4222 | return -1; |
| 4223 | return 0; |
| 4224 | } |
| 4225 | |
| 4226 | struct symbol_info { |
| 4227 | int si_nlocals; |
| 4228 | int si_ncells; |
| 4229 | int si_nfrees; |
| 4230 | int si_nimplicit; |
| 4231 | }; |
| 4232 | |
| 4233 | static void |
| 4234 | symtable_init_info(struct symbol_info *si) |
| 4235 | { |
| 4236 | si->si_nlocals = 0; |
| 4237 | si->si_ncells = 0; |
| 4238 | si->si_nfrees = 0; |
| 4239 | si->si_nimplicit = 0; |
| 4240 | } |
| 4241 | |
| 4242 | static int |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4243 | symtable_resolve_free(struct compiling *c, PyObject *name, int flags, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4244 | struct symbol_info *si) |
| 4245 | { |
| 4246 | PyObject *dict, *v; |
| 4247 | |
| 4248 | /* Seperate logic for DEF_FREE. If it occurs in a function, |
| 4249 | it indicates a local that we must allocate storage for (a |
| 4250 | cell var). If it occurs in a class, then the class has a |
| 4251 | method and a free variable with the same name. |
| 4252 | */ |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4253 | if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4254 | /* If it isn't declared locally, it can't be a cell. */ |
| 4255 | if (!(flags & (DEF_LOCAL | DEF_PARAM))) |
| 4256 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4257 | v = PyInt_FromLong(si->si_ncells++); |
| 4258 | dict = c->c_cellvars; |
| 4259 | } else { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4260 | /* If it is free anyway, then there is no need to do |
| 4261 | anything here. |
| 4262 | */ |
| 4263 | if (is_free(flags ^ DEF_FREE_CLASS) |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4264 | || (flags == DEF_FREE_CLASS)) |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4265 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4266 | v = PyInt_FromLong(si->si_nfrees++); |
| 4267 | dict = c->c_freevars; |
| 4268 | } |
| 4269 | if (v == NULL) |
| 4270 | return -1; |
| 4271 | if (PyDict_SetItem(dict, name, v) < 0) { |
| 4272 | Py_DECREF(v); |
| 4273 | return -1; |
| 4274 | } |
| 4275 | Py_DECREF(v); |
| 4276 | return 0; |
| 4277 | } |
| 4278 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4279 | /* If a variable is a cell and an argument, make sure that appears in |
| 4280 | co_cellvars before any variable to its right in varnames. |
| 4281 | */ |
| 4282 | |
| 4283 | |
| 4284 | static int |
| 4285 | symtable_cellvar_offsets(PyObject **cellvars, int argcount, |
| 4286 | PyObject *varnames, int flags) |
| 4287 | { |
| 4288 | PyObject *v, *w, *d, *list = NULL; |
| 4289 | int i, pos; |
| 4290 | |
| 4291 | if (flags & CO_VARARGS) |
| 4292 | argcount++; |
| 4293 | if (flags & CO_VARKEYWORDS) |
| 4294 | argcount++; |
| 4295 | for (i = argcount; --i >= 0; ) { |
| 4296 | v = PyList_GET_ITEM(varnames, i); |
| 4297 | if (PyDict_GetItem(*cellvars, v)) { |
| 4298 | if (list == NULL) { |
| 4299 | list = PyList_New(1); |
| 4300 | if (list == NULL) |
| 4301 | return -1; |
| 4302 | PyList_SET_ITEM(list, 0, v); |
| 4303 | Py_INCREF(v); |
| 4304 | } else |
| 4305 | PyList_Insert(list, 0, v); |
| 4306 | } |
| 4307 | } |
| 4308 | if (list == NULL || PyList_GET_SIZE(list) == 0) |
| 4309 | return 0; |
| 4310 | /* There are cellvars that are also arguments. Create a dict |
| 4311 | to replace cellvars and put the args at the front. |
| 4312 | */ |
| 4313 | d = PyDict_New(); |
| 4314 | for (i = PyList_GET_SIZE(list); --i >= 0; ) { |
| 4315 | v = PyInt_FromLong(i); |
| 4316 | if (v == NULL) |
| 4317 | goto fail; |
| 4318 | if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0) |
| 4319 | goto fail; |
| 4320 | if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0) |
| 4321 | goto fail; |
| 4322 | } |
| 4323 | pos = 0; |
| 4324 | i = PyList_GET_SIZE(list); |
| 4325 | Py_DECREF(list); |
| 4326 | while (PyDict_Next(*cellvars, &pos, &v, &w)) { |
| 4327 | w = PyInt_FromLong(i++); /* don't care about the old key */ |
| 4328 | if (PyDict_SetItem(d, v, w) < 0) { |
| 4329 | Py_DECREF(w); |
| 4330 | goto fail; |
| 4331 | } |
| 4332 | Py_DECREF(w); |
| 4333 | } |
| 4334 | Py_DECREF(*cellvars); |
| 4335 | *cellvars = d; |
| 4336 | return 1; |
| 4337 | fail: |
| 4338 | Py_DECREF(d); |
| 4339 | return -1; |
| 4340 | } |
| 4341 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4342 | static int |
| 4343 | symtable_freevar_offsets(PyObject *freevars, int offset) |
| 4344 | { |
| 4345 | PyObject *name, *v; |
| 4346 | int pos; |
| 4347 | |
| 4348 | /* The cell vars are the first elements of the closure, |
| 4349 | followed by the free vars. Update the offsets in |
| 4350 | c_freevars to account for number of cellvars. */ |
| 4351 | pos = 0; |
| 4352 | while (PyDict_Next(freevars, &pos, &name, &v)) { |
| 4353 | int i = PyInt_AS_LONG(v) + offset; |
| 4354 | PyObject *o = PyInt_FromLong(i); |
| 4355 | if (o == NULL) |
| 4356 | return -1; |
| 4357 | if (PyDict_SetItem(freevars, name, o) < 0) { |
| 4358 | Py_DECREF(o); |
| 4359 | return -1; |
| 4360 | } |
| 4361 | Py_DECREF(o); |
| 4362 | } |
| 4363 | return 0; |
| 4364 | } |
| 4365 | |
| 4366 | static int |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4367 | symtable_check_unoptimized(struct compiling *c, |
| 4368 | PySymtableEntryObject *ste, |
| 4369 | struct symbol_info *si) |
| 4370 | { |
| 4371 | char buf[300]; |
| 4372 | |
| 4373 | if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free |
| 4374 | || (ste->ste_nested && si->si_nimplicit))) |
| 4375 | return 0; |
| 4376 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4377 | #define ILLEGAL_CONTAINS "contains a nested function with free variables" |
| 4378 | |
| 4379 | #define ILLEGAL_IS "is a nested function" |
| 4380 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4381 | #define ILLEGAL_IMPORT_STAR \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4382 | "import * is not allowed in function '%.100s' because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4383 | |
| 4384 | #define ILLEGAL_BARE_EXEC \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4385 | "unqualified exec is not allowed in function '%.100s' it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4386 | |
| 4387 | #define ILLEGAL_EXEC_AND_IMPORT_STAR \ |
| 4388 | "function '%.100s' uses import * and bare exec, which are illegal" \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4389 | "because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4390 | |
| 4391 | /* XXX perhaps the linenos for these opt-breaking statements |
| 4392 | should be stored so the exception can point to them. */ |
| 4393 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4394 | if (ste->ste_child_free) { |
| 4395 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
| 4396 | sprintf(buf, ILLEGAL_IMPORT_STAR, |
| 4397 | PyString_AS_STRING(ste->ste_name), |
| 4398 | ILLEGAL_CONTAINS); |
| 4399 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
| 4400 | sprintf(buf, ILLEGAL_BARE_EXEC, |
| 4401 | PyString_AS_STRING(ste->ste_name), |
| 4402 | ILLEGAL_CONTAINS); |
| 4403 | else { |
| 4404 | sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 4405 | PyString_AS_STRING(ste->ste_name), |
| 4406 | ILLEGAL_CONTAINS); |
| 4407 | } |
| 4408 | } else { |
| 4409 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
| 4410 | sprintf(buf, ILLEGAL_IMPORT_STAR, |
| 4411 | PyString_AS_STRING(ste->ste_name), |
| 4412 | ILLEGAL_IS); |
| 4413 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
| 4414 | sprintf(buf, ILLEGAL_BARE_EXEC, |
| 4415 | PyString_AS_STRING(ste->ste_name), |
| 4416 | ILLEGAL_IS); |
| 4417 | else { |
| 4418 | sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 4419 | PyString_AS_STRING(ste->ste_name), |
| 4420 | ILLEGAL_IS); |
| 4421 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4422 | } |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4423 | |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4424 | PyErr_SetString(PyExc_SyntaxError, buf); |
| 4425 | PyErr_SyntaxLocation(c->c_symtable->st_filename, |
| 4426 | ste->ste_opt_lineno); |
| 4427 | return -1; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4428 | } |
| 4429 | |
| 4430 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4431 | symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste, |
| 4432 | struct symbol_info *si) |
| 4433 | { |
Jeremy Hylton | b857ba2 | 2001-08-10 21:41:33 +0000 | [diff] [blame] | 4434 | if (c->c_future) |
| 4435 | c->c_flags |= c->c_future->ff_features; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4436 | if (ste->ste_generator) |
| 4437 | c->c_flags |= CO_GENERATOR; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4438 | if (ste->ste_type != TYPE_MODULE) |
| 4439 | c->c_flags |= CO_NEWLOCALS; |
| 4440 | if (ste->ste_type == TYPE_FUNCTION) { |
| 4441 | c->c_nlocals = si->si_nlocals; |
| 4442 | if (ste->ste_optimized == 0) |
| 4443 | c->c_flags |= CO_OPTIMIZED; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4444 | else if (ste->ste_optimized != OPT_EXEC) |
| 4445 | return symtable_check_unoptimized(c, ste, si); |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4446 | } |
| 4447 | return 0; |
| 4448 | } |
| 4449 | |
| 4450 | static int |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4451 | symtable_load_symbols(struct compiling *c) |
| 4452 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4453 | static PyObject *implicit = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4454 | struct symtable *st = c->c_symtable; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4455 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4456 | PyObject *name, *varnames, *v; |
| 4457 | int i, flags, pos; |
| 4458 | struct symbol_info si; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4459 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4460 | if (implicit == NULL) { |
| 4461 | implicit = PyInt_FromLong(1); |
| 4462 | if (implicit == NULL) |
| 4463 | return -1; |
| 4464 | } |
| 4465 | v = NULL; |
| 4466 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4467 | if (symtable_init_compiling_symbols(c) < 0) |
| 4468 | goto fail; |
| 4469 | symtable_init_info(&si); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4470 | varnames = st->st_cur->ste_varnames; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4471 | si.si_nlocals = PyList_GET_SIZE(varnames); |
| 4472 | c->c_argcount = si.si_nlocals; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4473 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4474 | for (i = 0; i < si.si_nlocals; ++i) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4475 | v = PyInt_FromLong(i); |
| 4476 | if (PyDict_SetItem(c->c_locals, |
| 4477 | PyList_GET_ITEM(varnames, i), v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4478 | goto fail; |
| 4479 | Py_DECREF(v); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4480 | } |
| 4481 | |
| 4482 | /* XXX The cases below define the rules for whether a name is |
| 4483 | local or global. The logic could probably be clearer. */ |
| 4484 | pos = 0; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4485 | while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) { |
| 4486 | flags = PyInt_AS_LONG(v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4487 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4488 | if (flags & DEF_FREE_GLOBAL) |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4489 | /* undo the original DEF_FREE */ |
| 4490 | flags &= ~(DEF_FREE | DEF_FREE_CLASS); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4491 | |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4492 | /* Deal with names that need two actions: |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4493 | 1. Cell variables that are also locals. |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4494 | 2. Free variables in methods that are also class |
| 4495 | variables or declared global. |
| 4496 | */ |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4497 | if (flags & (DEF_FREE | DEF_FREE_CLASS)) |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4498 | symtable_resolve_free(c, name, flags, &si); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4499 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4500 | if (flags & DEF_STAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4501 | c->c_argcount--; |
| 4502 | c->c_flags |= CO_VARARGS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4503 | } else if (flags & DEF_DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4504 | c->c_argcount--; |
| 4505 | c->c_flags |= CO_VARKEYWORDS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4506 | } else if (flags & DEF_INTUPLE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4507 | c->c_argcount--; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4508 | else if (flags & DEF_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4509 | if (flags & DEF_PARAM) { |
| 4510 | PyErr_Format(PyExc_SyntaxError, LOCAL_GLOBAL, |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4511 | PyString_AS_STRING(name)); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 4512 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4513 | ste->ste_lineno); |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4514 | st->st_errors++; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4515 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4516 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4517 | if (PyDict_SetItem(c->c_globals, name, Py_None) < 0) |
| 4518 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4519 | } else if (flags & DEF_FREE_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4520 | si.si_nimplicit++; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4521 | if (PyDict_SetItem(c->c_globals, name, implicit) < 0) |
| 4522 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4523 | } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4524 | v = PyInt_FromLong(si.si_nlocals++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4525 | if (v == NULL) |
| 4526 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4527 | if (PyDict_SetItem(c->c_locals, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4528 | goto fail; |
| 4529 | Py_DECREF(v); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4530 | if (ste->ste_type != TYPE_CLASS) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4531 | if (PyList_Append(c->c_varnames, name) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4532 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4533 | } else if (is_free(flags)) { |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4534 | if (ste->ste_nested) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4535 | v = PyInt_FromLong(si.si_nfrees++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4536 | if (v == NULL) |
| 4537 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4538 | if (PyDict_SetItem(c->c_freevars, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4539 | goto fail; |
| 4540 | Py_DECREF(v); |
| 4541 | } else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4542 | si.si_nimplicit++; |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4543 | if (PyDict_SetItem(c->c_globals, name, |
| 4544 | implicit) < 0) |
| 4545 | goto fail; |
| 4546 | if (st->st_nscopes != 1) { |
| 4547 | v = PyInt_FromLong(flags); |
| 4548 | if (PyDict_SetItem(st->st_global, |
| 4549 | name, v)) |
| 4550 | goto fail; |
| 4551 | Py_DECREF(v); |
| 4552 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4553 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4554 | } |
| 4555 | } |
| 4556 | |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4557 | assert(PyDict_Size(c->c_freevars) == si.si_nfrees); |
| 4558 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4559 | if (si.si_ncells > 1) { /* one cell is always in order */ |
| 4560 | if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount, |
| 4561 | c->c_varnames, c->c_flags) < 0) |
| 4562 | return -1; |
| 4563 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4564 | if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0) |
| 4565 | return -1; |
| 4566 | return symtable_update_flags(c, ste, &si); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4567 | fail: |
| 4568 | /* is this always the right thing to do? */ |
| 4569 | Py_XDECREF(v); |
| 4570 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4571 | } |
| 4572 | |
| 4573 | static struct symtable * |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4574 | symtable_init() |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4575 | { |
| 4576 | struct symtable *st; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4577 | |
| 4578 | st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable)); |
| 4579 | if (st == NULL) |
| 4580 | return NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4581 | st->st_pass = 1; |
Tim Peters | ff1f852 | 2001-08-11 01:06:35 +0000 | [diff] [blame] | 4582 | |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4583 | st->st_filename = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4584 | if ((st->st_stack = PyList_New(0)) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4585 | goto fail; |
| 4586 | if ((st->st_symbols = PyDict_New()) == NULL) |
| 4587 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4588 | st->st_cur = NULL; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4589 | st->st_nscopes = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4590 | st->st_errors = 0; |
| 4591 | st->st_tmpname = 0; |
| 4592 | st->st_private = NULL; |
| 4593 | return st; |
| 4594 | fail: |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4595 | PySymtable_Free(st); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4596 | return NULL; |
| 4597 | } |
| 4598 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4599 | void |
| 4600 | PySymtable_Free(struct symtable *st) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4601 | { |
| 4602 | Py_XDECREF(st->st_symbols); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4603 | Py_XDECREF(st->st_stack); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4604 | Py_XDECREF(st->st_cur); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4605 | PyMem_Free((void *)st); |
| 4606 | } |
| 4607 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4608 | /* When the compiler exits a scope, it must should update the scope's |
| 4609 | free variable information with the list of free variables in its |
| 4610 | children. |
| 4611 | |
| 4612 | Variables that are free in children and defined in the current |
| 4613 | scope are cellvars. |
| 4614 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4615 | 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] | 4616 | false), free variables in children that are not defined here are |
| 4617 | implicit globals. |
| 4618 | |
| 4619 | */ |
| 4620 | |
| 4621 | static int |
| 4622 | symtable_update_free_vars(struct symtable *st) |
| 4623 | { |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4624 | int i, j, def; |
| 4625 | PyObject *o, *name, *list = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4626 | PySymtableEntryObject *child, *ste = st->st_cur; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4627 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4628 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4629 | def = DEF_FREE_CLASS; |
| 4630 | else |
| 4631 | def = DEF_FREE; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4632 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4633 | int pos = 0; |
| 4634 | |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4635 | if (list) |
| 4636 | PyList_SetSlice(list, 0, |
| 4637 | ((PyVarObject*)list)->ob_size, 0); |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4638 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4639 | PyList_GET_ITEM(ste->ste_children, i); |
| 4640 | while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4641 | int flags = PyInt_AS_LONG(o); |
| 4642 | if (!(is_free(flags))) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4643 | continue; /* avoids indentation */ |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4644 | if (list == NULL) { |
| 4645 | list = PyList_New(0); |
| 4646 | if (list == NULL) |
| 4647 | return -1; |
| 4648 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4649 | ste->ste_child_free = 1; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4650 | if (PyList_Append(list, name) < 0) { |
| 4651 | Py_DECREF(list); |
| 4652 | return -1; |
| 4653 | } |
| 4654 | } |
| 4655 | for (j = 0; list && j < PyList_GET_SIZE(list); j++) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4656 | PyObject *v; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4657 | name = PyList_GET_ITEM(list, j); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4658 | v = PyDict_GetItem(ste->ste_symbols, name); |
| 4659 | /* If a name N is declared global in scope A and |
| 4660 | referenced in scope B contained (perhaps |
| 4661 | indirectly) in A and there are no scopes |
| 4662 | with bindings for N between B and A, then N |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4663 | is global in B. Unless A is a class scope, |
| 4664 | because class scopes are not considered for |
| 4665 | nested scopes. |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4666 | */ |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4667 | if (v && (ste->ste_type != TYPE_CLASS)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4668 | int flags = PyInt_AS_LONG(v); |
| 4669 | if (flags & DEF_GLOBAL) { |
| 4670 | symtable_undo_free(st, child->ste_id, |
| 4671 | name); |
| 4672 | continue; |
| 4673 | } |
| 4674 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4675 | if (ste->ste_nested) { |
| 4676 | if (symtable_add_def_o(st, ste->ste_symbols, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4677 | name, def) < 0) { |
| 4678 | Py_DECREF(list); |
| 4679 | return -1; |
| 4680 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4681 | } else { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4682 | if (symtable_check_global(st, child->ste_id, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4683 | name) < 0) { |
| 4684 | Py_DECREF(list); |
| 4685 | return -1; |
| 4686 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4687 | } |
| 4688 | } |
| 4689 | } |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4690 | |
| 4691 | Py_XDECREF(list); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4692 | return 0; |
| 4693 | } |
| 4694 | |
| 4695 | /* If the current scope is a non-nested class or if name is not |
| 4696 | defined in the current, non-nested scope, then it is an implicit |
| 4697 | global in all nested scopes. |
| 4698 | */ |
| 4699 | |
| 4700 | static int |
| 4701 | symtable_check_global(struct symtable *st, PyObject *child, PyObject *name) |
| 4702 | { |
| 4703 | PyObject *o; |
| 4704 | int v; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4705 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4706 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4707 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4708 | return symtable_undo_free(st, child, name); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4709 | o = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4710 | if (o == NULL) |
| 4711 | return symtable_undo_free(st, child, name); |
| 4712 | v = PyInt_AS_LONG(o); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4713 | |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4714 | if (is_free(v) || (v & DEF_GLOBAL)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4715 | return symtable_undo_free(st, child, name); |
| 4716 | else |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4717 | return symtable_add_def_o(st, ste->ste_symbols, |
| 4718 | name, DEF_FREE); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4719 | } |
| 4720 | |
| 4721 | static int |
| 4722 | symtable_undo_free(struct symtable *st, PyObject *id, |
| 4723 | PyObject *name) |
| 4724 | { |
| 4725 | int i, v, x; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4726 | PyObject *info; |
| 4727 | PySymtableEntryObject *ste; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4728 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4729 | ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id); |
| 4730 | if (ste == NULL) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4731 | return -1; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4732 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4733 | info = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4734 | if (info == NULL) |
| 4735 | return 0; |
| 4736 | v = PyInt_AS_LONG(info); |
| 4737 | if (is_free(v)) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4738 | if (symtable_add_def_o(st, ste->ste_symbols, name, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4739 | DEF_FREE_GLOBAL) < 0) |
| 4740 | return -1; |
| 4741 | } else |
| 4742 | /* If the name is defined here or declared global, |
| 4743 | then the recursion stops. */ |
| 4744 | return 0; |
| 4745 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4746 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
| 4747 | PySymtableEntryObject *child; |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4748 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4749 | PyList_GET_ITEM(ste->ste_children, i); |
| 4750 | x = symtable_undo_free(st, child->ste_id, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4751 | if (x < 0) |
| 4752 | return x; |
| 4753 | } |
| 4754 | return 0; |
| 4755 | } |
| 4756 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4757 | /* symtable_enter_scope() gets a reference via PySymtableEntry_New(). |
| 4758 | This reference is released when the scope is exited, via the DECREF |
| 4759 | in symtable_exit_scope(). |
| 4760 | */ |
| 4761 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4762 | static int |
| 4763 | symtable_exit_scope(struct symtable *st) |
| 4764 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4765 | int end; |
| 4766 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4767 | if (st->st_pass == 1) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4768 | symtable_update_free_vars(st); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4769 | Py_DECREF(st->st_cur); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4770 | end = PyList_GET_SIZE(st->st_stack) - 1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4771 | st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack, |
| 4772 | end); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4773 | if (PySequence_DelItem(st->st_stack, end) < 0) |
| 4774 | return -1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4775 | return 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4776 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4777 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4778 | static void |
| 4779 | symtable_enter_scope(struct symtable *st, char *name, int type, |
| 4780 | int lineno) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4781 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4782 | PySymtableEntryObject *prev = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4783 | |
| 4784 | if (st->st_cur) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4785 | prev = st->st_cur; |
| 4786 | if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) { |
| 4787 | Py_DECREF(st->st_cur); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4788 | st->st_errors++; |
| 4789 | return; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4790 | } |
| 4791 | } |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4792 | st->st_cur = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4793 | PySymtableEntry_New(st, name, type, lineno); |
| 4794 | if (strcmp(name, TOP) == 0) |
| 4795 | st->st_global = st->st_cur->ste_symbols; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4796 | if (prev && st->st_pass == 1) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4797 | if (PyList_Append(prev->ste_children, |
| 4798 | (PyObject *)st->st_cur) < 0) |
| 4799 | st->st_errors++; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4800 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4801 | } |
| 4802 | |
| 4803 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4804 | symtable_lookup(struct symtable *st, char *name) |
| 4805 | { |
| 4806 | char buffer[MANGLE_LEN]; |
| 4807 | PyObject *v; |
| 4808 | int flags; |
| 4809 | |
| 4810 | if (mangle(st->st_private, name, buffer, sizeof(buffer))) |
| 4811 | name = buffer; |
| 4812 | v = PyDict_GetItemString(st->st_cur->ste_symbols, name); |
| 4813 | if (v == NULL) { |
| 4814 | if (PyErr_Occurred()) |
| 4815 | return -1; |
| 4816 | else |
| 4817 | return 0; |
| 4818 | } |
| 4819 | |
| 4820 | flags = PyInt_AS_LONG(v); |
| 4821 | return flags; |
| 4822 | } |
| 4823 | |
| 4824 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4825 | symtable_add_def(struct symtable *st, char *name, int flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4826 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4827 | PyObject *s; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4828 | char buffer[MANGLE_LEN]; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 4829 | int ret; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4830 | |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4831 | if (mangle(st->st_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4832 | name = buffer; |
| 4833 | if ((s = PyString_InternFromString(name)) == NULL) |
| 4834 | return -1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 4835 | ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag); |
| 4836 | Py_DECREF(s); |
| 4837 | return ret; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4838 | } |
| 4839 | |
| 4840 | /* Must only be called with mangled names */ |
| 4841 | |
| 4842 | static int |
| 4843 | symtable_add_def_o(struct symtable *st, PyObject *dict, |
| 4844 | PyObject *name, int flag) |
| 4845 | { |
| 4846 | PyObject *o; |
| 4847 | int val; |
| 4848 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4849 | if ((o = PyDict_GetItem(dict, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4850 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4851 | if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4852 | PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 4853 | PyString_AsString(name)); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 4854 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4855 | st->st_cur->ste_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4856 | return -1; |
| 4857 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4858 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4859 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4860 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4861 | o = PyInt_FromLong(val); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4862 | if (PyDict_SetItem(dict, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4863 | Py_DECREF(o); |
| 4864 | return -1; |
| 4865 | } |
| 4866 | Py_DECREF(o); |
| 4867 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4868 | if (flag & DEF_PARAM) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4869 | if (PyList_Append(st->st_cur->ste_varnames, name) < 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4870 | return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4871 | } else if (flag & DEF_GLOBAL) { |
| 4872 | /* XXX need to update DEF_GLOBAL for other flags too; |
| 4873 | perhaps only DEF_FREE_GLOBAL */ |
| 4874 | if ((o = PyDict_GetItem(st->st_global, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4875 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4876 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4877 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4878 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4879 | o = PyInt_FromLong(val); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4880 | if (PyDict_SetItem(st->st_global, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4881 | Py_DECREF(o); |
| 4882 | return -1; |
| 4883 | } |
| 4884 | Py_DECREF(o); |
| 4885 | } |
| 4886 | return 0; |
| 4887 | } |
| 4888 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4889 | #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4890 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 4891 | /* Look for a yield stmt under n. Return 1 if found, else 0. |
| 4892 | This hack is used to look inside "if 0:" blocks (which are normally |
| 4893 | ignored) in case those are the only places a yield occurs (so that this |
| 4894 | function is a generator). */ |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 4895 | static int |
| 4896 | look_for_yield(node *n) |
| 4897 | { |
| 4898 | int i; |
| 4899 | |
| 4900 | for (i = 0; i < NCH(n); ++i) { |
| 4901 | node *kid = CHILD(n, i); |
| 4902 | |
| 4903 | switch (TYPE(kid)) { |
| 4904 | |
| 4905 | case classdef: |
| 4906 | case funcdef: |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 4907 | case lambdef: |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 4908 | /* Stuff in nested functions and classes can't make |
| 4909 | the parent a generator. */ |
| 4910 | return 0; |
| 4911 | |
| 4912 | case yield_stmt: |
| 4913 | return 1; |
| 4914 | |
| 4915 | default: |
| 4916 | if (look_for_yield(kid)) |
| 4917 | return 1; |
| 4918 | } |
| 4919 | } |
| 4920 | return 0; |
| 4921 | } |
| 4922 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4923 | static void |
| 4924 | symtable_node(struct symtable *st, node *n) |
| 4925 | { |
| 4926 | int i, start = 0; |
| 4927 | |
| 4928 | loop: |
| 4929 | switch (TYPE(n)) { |
| 4930 | case funcdef: { |
| 4931 | char *func_name = STR(CHILD(n, 1)); |
| 4932 | symtable_add_def(st, func_name, DEF_LOCAL); |
| 4933 | symtable_default_args(st, CHILD(n, 2)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4934 | symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4935 | symtable_funcdef(st, n); |
| 4936 | symtable_exit_scope(st); |
| 4937 | break; |
| 4938 | } |
| 4939 | case lambdef: |
| 4940 | if (NCH(n) == 4) |
| 4941 | symtable_default_args(st, CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4942 | symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4943 | symtable_funcdef(st, n); |
| 4944 | symtable_exit_scope(st); |
| 4945 | break; |
| 4946 | case classdef: { |
| 4947 | char *tmp, *class_name = STR(CHILD(n, 1)); |
| 4948 | symtable_add_def(st, class_name, DEF_LOCAL); |
| 4949 | if (TYPE(CHILD(n, 2)) == LPAR) { |
| 4950 | node *bases = CHILD(n, 3); |
| 4951 | int i; |
| 4952 | for (i = 0; i < NCH(bases); i += 2) { |
| 4953 | symtable_node(st, CHILD(bases, i)); |
| 4954 | } |
| 4955 | } |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4956 | symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4957 | tmp = st->st_private; |
| 4958 | st->st_private = class_name; |
| 4959 | symtable_node(st, CHILD(n, NCH(n) - 1)); |
| 4960 | st->st_private = tmp; |
| 4961 | symtable_exit_scope(st); |
| 4962 | break; |
| 4963 | } |
| 4964 | case if_stmt: |
| 4965 | for (i = 0; i + 3 < NCH(n); i += 4) { |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 4966 | if (is_constant_false(NULL, (CHILD(n, i + 1)))) { |
| 4967 | if (st->st_cur->ste_generator == 0) |
| 4968 | st->st_cur->ste_generator = |
| 4969 | look_for_yield(CHILD(n, i+3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4970 | continue; |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 4971 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4972 | symtable_node(st, CHILD(n, i + 1)); |
| 4973 | symtable_node(st, CHILD(n, i + 3)); |
| 4974 | } |
| 4975 | if (i + 2 < NCH(n)) |
| 4976 | symtable_node(st, CHILD(n, i + 2)); |
| 4977 | break; |
| 4978 | case global_stmt: |
| 4979 | symtable_global(st, n); |
| 4980 | break; |
| 4981 | case import_stmt: |
| 4982 | symtable_import(st, n); |
| 4983 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 4984 | case exec_stmt: { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4985 | st->st_cur->ste_optimized |= OPT_EXEC; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4986 | symtable_node(st, CHILD(n, 1)); |
| 4987 | if (NCH(n) > 2) |
| 4988 | symtable_node(st, CHILD(n, 3)); |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 4989 | else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4990 | st->st_cur->ste_optimized |= OPT_BARE_EXEC; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 4991 | st->st_cur->ste_opt_lineno = n->n_lineno; |
| 4992 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4993 | if (NCH(n) > 4) |
| 4994 | symtable_node(st, CHILD(n, 5)); |
| 4995 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 4996 | |
| 4997 | } |
Jeremy Hylton | 384639f | 2001-02-19 15:50:51 +0000 | [diff] [blame] | 4998 | case assert_stmt: |
| 4999 | if (Py_OptimizeFlag) |
| 5000 | return; |
| 5001 | if (NCH(n) == 2) { |
| 5002 | n = CHILD(n, 1); |
| 5003 | goto loop; |
| 5004 | } else { |
| 5005 | symtable_node(st, CHILD(n, 1)); |
| 5006 | n = CHILD(n, 3); |
| 5007 | goto loop; |
| 5008 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5009 | case except_clause: |
| 5010 | if (NCH(n) == 4) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5011 | symtable_assign(st, CHILD(n, 3), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5012 | if (NCH(n) > 1) { |
| 5013 | n = CHILD(n, 1); |
| 5014 | goto loop; |
| 5015 | } |
| 5016 | break; |
| 5017 | case del_stmt: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5018 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5019 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 5020 | case yield_stmt: |
| 5021 | st->st_cur->ste_generator = 1; |
| 5022 | n = CHILD(n, 1); |
| 5023 | goto loop; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5024 | case expr_stmt: |
| 5025 | if (NCH(n) == 1) |
| 5026 | n = CHILD(n, 0); |
| 5027 | else { |
| 5028 | if (TYPE(CHILD(n, 1)) == augassign) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5029 | symtable_assign(st, CHILD(n, 0), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5030 | symtable_node(st, CHILD(n, 2)); |
| 5031 | break; |
| 5032 | } else { |
| 5033 | int i; |
| 5034 | for (i = 0; i < NCH(n) - 2; i += 2) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5035 | symtable_assign(st, CHILD(n, i), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5036 | n = CHILD(n, NCH(n) - 1); |
| 5037 | } |
| 5038 | } |
| 5039 | goto loop; |
| 5040 | /* watchout for fall-through logic below */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5041 | case argument: |
| 5042 | if (NCH(n) == 3) { |
| 5043 | n = CHILD(n, 2); |
| 5044 | goto loop; |
| 5045 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5046 | case listmaker: |
| 5047 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5048 | st->st_tmpname++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5049 | symtable_list_comprehension(st, CHILD(n, 1)); |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5050 | symtable_node(st, CHILD(n, 0)); |
| 5051 | st->st_tmpname--; |
| 5052 | return; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5053 | } |
| 5054 | case atom: |
| 5055 | if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) { |
| 5056 | symtable_add_use(st, STR(CHILD(n, 0))); |
| 5057 | break; |
| 5058 | } |
| 5059 | case for_stmt: |
| 5060 | if (TYPE(n) == for_stmt) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5061 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5062 | start = 3; |
| 5063 | } |
| 5064 | default: |
| 5065 | if (NCH(n) == 1) { |
| 5066 | n = CHILD(n, 0); |
| 5067 | goto loop; |
| 5068 | } |
| 5069 | for (i = start; i < NCH(n); ++i) |
| 5070 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5071 | symtable_node(st, CHILD(n, i)); |
| 5072 | } |
| 5073 | } |
| 5074 | |
| 5075 | static void |
| 5076 | symtable_funcdef(struct symtable *st, node *n) |
| 5077 | { |
| 5078 | node *body; |
| 5079 | |
| 5080 | if (TYPE(n) == lambdef) { |
| 5081 | if (NCH(n) == 4) |
| 5082 | symtable_params(st, CHILD(n, 1)); |
| 5083 | } else |
| 5084 | symtable_params(st, CHILD(n, 2)); |
| 5085 | body = CHILD(n, NCH(n) - 1); |
| 5086 | symtable_node(st, body); |
| 5087 | } |
| 5088 | |
| 5089 | /* The next two functions parse the argument tuple. |
| 5090 | symtable_default_arg() checks for names in the default arguments, |
| 5091 | which are references in the defining scope. symtable_params() |
| 5092 | parses the parameter names, which are defined in the function's |
| 5093 | body. |
| 5094 | |
| 5095 | varargslist: |
| 5096 | (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) |
| 5097 | | fpdef ['=' test] (',' fpdef ['=' test])* [','] |
| 5098 | */ |
| 5099 | |
| 5100 | static void |
| 5101 | symtable_default_args(struct symtable *st, node *n) |
| 5102 | { |
| 5103 | node *c; |
| 5104 | int i; |
| 5105 | |
| 5106 | if (TYPE(n) == parameters) { |
| 5107 | n = CHILD(n, 1); |
| 5108 | if (TYPE(n) == RPAR) |
| 5109 | return; |
| 5110 | } |
| 5111 | REQ(n, varargslist); |
| 5112 | for (i = 0; i < NCH(n); i += 2) { |
| 5113 | c = CHILD(n, i); |
| 5114 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5115 | break; |
| 5116 | } |
| 5117 | if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL)) |
| 5118 | symtable_node(st, CHILD(n, i)); |
| 5119 | } |
| 5120 | } |
| 5121 | |
| 5122 | static void |
| 5123 | symtable_params(struct symtable *st, node *n) |
| 5124 | { |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5125 | int i, complex = -1, ext = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5126 | node *c = NULL; |
| 5127 | |
| 5128 | if (TYPE(n) == parameters) { |
| 5129 | n = CHILD(n, 1); |
| 5130 | if (TYPE(n) == RPAR) |
| 5131 | return; |
| 5132 | } |
| 5133 | REQ(n, varargslist); |
| 5134 | for (i = 0; i < NCH(n); i += 2) { |
| 5135 | c = CHILD(n, i); |
| 5136 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5137 | ext = 1; |
| 5138 | break; |
| 5139 | } |
| 5140 | if (TYPE(c) == test) { |
| 5141 | continue; |
| 5142 | } |
| 5143 | if (TYPE(CHILD(c, 0)) == NAME) |
| 5144 | symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM); |
| 5145 | else { |
| 5146 | char nbuf[10]; |
| 5147 | sprintf(nbuf, ".%d", i); |
| 5148 | symtable_add_def(st, nbuf, DEF_PARAM); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5149 | complex = i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5150 | } |
| 5151 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5152 | if (ext) { |
| 5153 | c = CHILD(n, i); |
| 5154 | if (TYPE(c) == STAR) { |
| 5155 | i++; |
| 5156 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5157 | DEF_PARAM | DEF_STAR); |
| 5158 | i += 2; |
Jeremy Hylton | 1113cfc | 2001-01-23 00:50:52 +0000 | [diff] [blame] | 5159 | if (i >= NCH(n)) |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5160 | c = NULL; |
| 5161 | else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5162 | c = CHILD(n, i); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5163 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5164 | if (c && TYPE(c) == DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5165 | i++; |
| 5166 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5167 | DEF_PARAM | DEF_DOUBLESTAR); |
| 5168 | } |
| 5169 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5170 | if (complex >= 0) { |
| 5171 | int j; |
| 5172 | for (j = 0; j <= complex; j++) { |
| 5173 | c = CHILD(n, j); |
| 5174 | if (TYPE(c) == COMMA) |
Jeremy Hylton | 2b3f0ca | 2001-02-19 23:52:49 +0000 | [diff] [blame] | 5175 | c = CHILD(n, ++j); |
| 5176 | else if (TYPE(c) == EQUAL) |
| 5177 | c = CHILD(n, j += 3); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5178 | if (TYPE(CHILD(c, 0)) == LPAR) |
| 5179 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5180 | } |
| 5181 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5182 | } |
| 5183 | |
| 5184 | static void |
| 5185 | symtable_params_fplist(struct symtable *st, node *n) |
| 5186 | { |
| 5187 | int i; |
| 5188 | node *c; |
| 5189 | |
| 5190 | REQ(n, fplist); |
| 5191 | for (i = 0; i < NCH(n); i += 2) { |
| 5192 | c = CHILD(n, i); |
| 5193 | REQ(c, fpdef); |
| 5194 | if (NCH(c) == 1) |
| 5195 | symtable_add_def(st, STR(CHILD(c, 0)), |
| 5196 | DEF_PARAM | DEF_INTUPLE); |
| 5197 | else |
| 5198 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5199 | } |
| 5200 | |
| 5201 | } |
| 5202 | |
| 5203 | static void |
| 5204 | symtable_global(struct symtable *st, node *n) |
| 5205 | { |
| 5206 | int i; |
| 5207 | |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 5208 | /* XXX It might be helpful to warn about module-level global |
| 5209 | statements, but it's hard to tell the difference between |
| 5210 | module-level and a string passed to exec. |
| 5211 | */ |
Jeremy Hylton | c176132 | 2001-02-28 23:44:45 +0000 | [diff] [blame] | 5212 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5213 | for (i = 1; i < NCH(n); i += 2) { |
| 5214 | char *name = STR(CHILD(n, i)); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5215 | int flags; |
| 5216 | |
| 5217 | flags = symtable_lookup(st, name); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5218 | if (flags < 0) |
| 5219 | continue; |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5220 | if (flags && flags != DEF_GLOBAL) { |
| 5221 | char buf[500]; |
| 5222 | if (flags & DEF_PARAM) { |
| 5223 | PyErr_Format(PyExc_SyntaxError, |
| 5224 | "name '%.400s' is local and global", |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5225 | name); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5226 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5227 | st->st_cur->ste_lineno); |
| 5228 | st->st_errors++; |
| 5229 | return; |
Guido van Rossum | 0bba7f8 | 2001-02-28 21:55:38 +0000 | [diff] [blame] | 5230 | } |
| 5231 | else { |
| 5232 | if (flags & DEF_LOCAL) |
| 5233 | sprintf(buf, GLOBAL_AFTER_ASSIGN, |
| 5234 | name); |
| 5235 | else |
| 5236 | sprintf(buf, GLOBAL_AFTER_USE, name); |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 5237 | symtable_warn(st, buf); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5238 | } |
| 5239 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5240 | symtable_add_def(st, name, DEF_GLOBAL); |
| 5241 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5242 | } |
| 5243 | |
| 5244 | static void |
| 5245 | symtable_list_comprehension(struct symtable *st, node *n) |
| 5246 | { |
| 5247 | char tmpname[12]; |
| 5248 | |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5249 | sprintf(tmpname, "_[%d]", st->st_tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5250 | symtable_add_def(st, tmpname, DEF_LOCAL); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5251 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5252 | symtable_node(st, CHILD(n, 3)); |
| 5253 | if (NCH(n) == 5) |
| 5254 | symtable_node(st, CHILD(n, 4)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5255 | } |
| 5256 | |
| 5257 | static void |
| 5258 | symtable_import(struct symtable *st, node *n) |
| 5259 | { |
| 5260 | int i; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5261 | /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)* |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5262 | | 'from' dotted_name 'import' |
| 5263 | ('*' | import_as_name (',' import_as_name)*) |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5264 | import_as_name: NAME [NAME NAME] |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5265 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5266 | if (STR(CHILD(n, 0))[0] == 'f') { /* from */ |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5267 | node *dotname = CHILD(n, 1); |
| 5268 | if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) { |
| 5269 | /* check for bogus imports */ |
| 5270 | if (n->n_lineno >= st->st_future->ff_last_lineno) { |
| 5271 | PyErr_SetString(PyExc_SyntaxError, |
| 5272 | LATE_FUTURE); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5273 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5274 | n->n_lineno); |
| 5275 | st->st_errors++; |
| 5276 | return; |
| 5277 | } |
| 5278 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5279 | if (TYPE(CHILD(n, 3)) == STAR) { |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 5280 | if (st->st_cur->ste_type != TYPE_MODULE) { |
Jeremy Hylton | 6a53bd8 | 2001-08-06 20:34:25 +0000 | [diff] [blame] | 5281 | if (symtable_warn(st, |
| 5282 | "import * only allowed at module level") < 0) |
| 5283 | return; |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 5284 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5285 | st->st_cur->ste_optimized |= OPT_IMPORT_STAR; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5286 | st->st_cur->ste_opt_lineno = n->n_lineno; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5287 | } else { |
| 5288 | for (i = 3; i < NCH(n); i += 2) { |
| 5289 | node *c = CHILD(n, i); |
| 5290 | if (NCH(c) > 1) /* import as */ |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5291 | symtable_assign(st, CHILD(c, 2), |
| 5292 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5293 | else |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5294 | symtable_assign(st, CHILD(c, 0), |
| 5295 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5296 | } |
| 5297 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5298 | } else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5299 | for (i = 1; i < NCH(n); i += 2) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5300 | symtable_assign(st, CHILD(n, i), DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5301 | } |
| 5302 | } |
| 5303 | } |
| 5304 | |
| 5305 | static void |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5306 | symtable_assign(struct symtable *st, node *n, int flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5307 | { |
| 5308 | node *tmp; |
| 5309 | int i; |
| 5310 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5311 | loop: |
| 5312 | switch (TYPE(n)) { |
| 5313 | case lambdef: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5314 | /* invalid assignment, e.g. lambda x:x=2. The next |
| 5315 | pass will catch this error. */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5316 | return; |
| 5317 | case power: |
| 5318 | if (NCH(n) > 2) { |
| 5319 | for (i = 2; i < NCH(n); ++i) |
| 5320 | if (TYPE(CHILD(n, i)) != DOUBLESTAR) |
| 5321 | symtable_node(st, CHILD(n, i)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5322 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5323 | if (NCH(n) > 1) { |
| 5324 | symtable_node(st, CHILD(n, 0)); |
| 5325 | symtable_node(st, CHILD(n, 1)); |
| 5326 | } else { |
| 5327 | n = CHILD(n, 0); |
| 5328 | goto loop; |
| 5329 | } |
| 5330 | return; |
| 5331 | case listmaker: |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5332 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
| 5333 | /* XXX This is an error, but the next pass |
| 5334 | will catch it. */ |
| 5335 | return; |
| 5336 | } else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5337 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5338 | symtable_assign(st, CHILD(n, i), flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5339 | } |
| 5340 | return; |
| 5341 | case exprlist: |
| 5342 | case testlist: |
| 5343 | if (NCH(n) == 1) { |
| 5344 | n = CHILD(n, 0); |
| 5345 | goto loop; |
| 5346 | } |
| 5347 | else { |
| 5348 | int i; |
| 5349 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5350 | symtable_assign(st, CHILD(n, i), flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5351 | return; |
| 5352 | } |
| 5353 | goto loop; |
| 5354 | case atom: |
| 5355 | tmp = CHILD(n, 0); |
| 5356 | if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) { |
| 5357 | n = CHILD(n, 1); |
| 5358 | goto loop; |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 5359 | } else if (TYPE(tmp) == NAME) { |
Jeremy Hylton | a830b38 | 2001-04-09 16:07:59 +0000 | [diff] [blame] | 5360 | if (strcmp(STR(tmp), "__debug__") == 0) |
| 5361 | symtable_warn(st, ASSIGN_DEBUG); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5362 | symtable_add_def(st, STR(tmp), DEF_LOCAL | flag); |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 5363 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5364 | return; |
| 5365 | case dotted_as_name: |
| 5366 | if (NCH(n) == 3) |
| 5367 | symtable_add_def(st, STR(CHILD(n, 2)), |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5368 | DEF_LOCAL | flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5369 | else |
| 5370 | symtable_add_def(st, |
| 5371 | STR(CHILD(CHILD(n, |
| 5372 | 0), 0)), |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5373 | DEF_LOCAL | flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5374 | return; |
| 5375 | case dotted_name: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5376 | symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5377 | return; |
| 5378 | case NAME: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5379 | symtable_add_def(st, STR(n), DEF_LOCAL | flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5380 | return; |
| 5381 | default: |
| 5382 | if (NCH(n) == 0) |
| 5383 | return; |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 5384 | if (NCH(n) == 1) { |
| 5385 | n = CHILD(n, 0); |
| 5386 | goto loop; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5387 | } |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 5388 | /* Should only occur for errors like x + 1 = 1, |
| 5389 | which will be caught in the next pass. */ |
| 5390 | for (i = 0; i < NCH(n); ++i) |
| 5391 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5392 | symtable_assign(st, CHILD(n, i), flag); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5393 | } |
| 5394 | } |