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 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 76 | static PyMemberDef code_memberlist[] = { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 77 | {"co_argcount", T_INT, OFF(co_argcount), READONLY}, |
| 78 | {"co_nlocals", T_INT, OFF(co_nlocals), READONLY}, |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 79 | {"co_stacksize",T_INT, OFF(co_stacksize), READONLY}, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 80 | {"co_flags", T_INT, OFF(co_flags), READONLY}, |
Guido van Rossum | 39d942d | 1992-01-12 02:30:05 +0000 | [diff] [blame] | 81 | {"co_code", T_OBJECT, OFF(co_code), READONLY}, |
| 82 | {"co_consts", T_OBJECT, OFF(co_consts), READONLY}, |
| 83 | {"co_names", T_OBJECT, OFF(co_names), READONLY}, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 84 | {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY}, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 85 | {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY}, |
| 86 | {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY}, |
Guido van Rossum | 39d942d | 1992-01-12 02:30:05 +0000 | [diff] [blame] | 87 | {"co_filename", T_OBJECT, OFF(co_filename), READONLY}, |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 88 | {"co_name", T_OBJECT, OFF(co_name), READONLY}, |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 89 | {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY}, |
| 90 | {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 91 | {NULL} /* Sentinel */ |
| 92 | }; |
| 93 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 94 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 95 | code_dealloc(PyCodeObject *co) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 96 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 97 | Py_XDECREF(co->co_code); |
| 98 | Py_XDECREF(co->co_consts); |
| 99 | Py_XDECREF(co->co_names); |
Guido van Rossum | 275558c | 1997-07-25 20:13:49 +0000 | [diff] [blame] | 100 | Py_XDECREF(co->co_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 101 | Py_XDECREF(co->co_freevars); |
| 102 | Py_XDECREF(co->co_cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 103 | Py_XDECREF(co->co_filename); |
| 104 | Py_XDECREF(co->co_name); |
Guido van Rossum | 275558c | 1997-07-25 20:13:49 +0000 | [diff] [blame] | 105 | Py_XDECREF(co->co_lnotab); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 106 | PyObject_DEL(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 109 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 110 | code_repr(PyCodeObject *co) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 111 | { |
| 112 | char buf[500]; |
| 113 | int lineno = -1; |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 114 | char *filename = "???"; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 115 | char *name = "???"; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 116 | |
Guido van Rossum | a396a88 | 2000-04-07 01:21:36 +0000 | [diff] [blame] | 117 | if (co->co_firstlineno != 0) |
| 118 | lineno = co->co_firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 119 | if (co->co_filename && PyString_Check(co->co_filename)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 120 | filename = PyString_AS_STRING(co->co_filename); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 121 | if (co->co_name && PyString_Check(co->co_name)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 122 | name = PyString_AS_STRING(co->co_name); |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 123 | PyOS_snprintf(buf, sizeof(buf), |
| 124 | "<code object %.100s at %p, file \"%.300s\", line %d>", |
| 125 | name, co, filename, lineno); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 126 | return PyString_FromString(buf); |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 129 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 130 | code_compare(PyCodeObject *co, PyCodeObject *cp) |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 131 | { |
| 132 | int cmp; |
Guido van Rossum | 4467959 | 2000-04-10 16:20:31 +0000 | [diff] [blame] | 133 | cmp = PyObject_Compare(co->co_name, cp->co_name); |
| 134 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 135 | cmp = co->co_argcount - cp->co_argcount; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 136 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 137 | cmp = co->co_nlocals - cp->co_nlocals; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 138 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 139 | cmp = co->co_flags - cp->co_flags; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 140 | if (cmp) return cmp; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 141 | cmp = PyObject_Compare(co->co_code, cp->co_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 142 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 143 | cmp = PyObject_Compare(co->co_consts, cp->co_consts); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 144 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 145 | cmp = PyObject_Compare(co->co_names, cp->co_names); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 146 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 147 | cmp = PyObject_Compare(co->co_varnames, cp->co_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 148 | if (cmp) return cmp; |
| 149 | cmp = PyObject_Compare(co->co_freevars, cp->co_freevars); |
| 150 | if (cmp) return cmp; |
| 151 | cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 152 | return cmp; |
| 153 | } |
| 154 | |
| 155 | static long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 156 | code_hash(PyCodeObject *co) |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 157 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 158 | long h, h0, h1, h2, h3, h4, h5, h6; |
Guido van Rossum | 4467959 | 2000-04-10 16:20:31 +0000 | [diff] [blame] | 159 | h0 = PyObject_Hash(co->co_name); |
| 160 | if (h0 == -1) return -1; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 161 | h1 = PyObject_Hash(co->co_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 162 | if (h1 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 163 | h2 = PyObject_Hash(co->co_consts); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 164 | if (h2 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 165 | h3 = PyObject_Hash(co->co_names); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 166 | if (h3 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 167 | h4 = PyObject_Hash(co->co_varnames); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 168 | if (h4 == -1) return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 169 | h5 = PyObject_Hash(co->co_freevars); |
| 170 | if (h5 == -1) return -1; |
| 171 | h6 = PyObject_Hash(co->co_cellvars); |
| 172 | if (h6 == -1) return -1; |
| 173 | h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 174 | co->co_argcount ^ co->co_nlocals ^ co->co_flags; |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 175 | if (h == -1) h = -2; |
| 176 | return h; |
| 177 | } |
| 178 | |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 179 | /* XXX code objects need to participate in GC? */ |
| 180 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 181 | PyTypeObject PyCode_Type = { |
| 182 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 183 | 0, |
| 184 | "code", |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 185 | sizeof(PyCodeObject), |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 186 | 0, |
Jeremy Hylton | c785f48 | 2001-09-14 20:08:07 +0000 | [diff] [blame] | 187 | (destructor)code_dealloc, /* tp_dealloc */ |
| 188 | 0, /* tp_print */ |
| 189 | 0, /* tp_getattr */ |
| 190 | 0, /* tp_setattr */ |
| 191 | (cmpfunc)code_compare, /* tp_compare */ |
| 192 | (reprfunc)code_repr, /* tp_repr */ |
| 193 | 0, /* tp_as_number */ |
| 194 | 0, /* tp_as_sequence */ |
| 195 | 0, /* tp_as_mapping */ |
| 196 | (hashfunc)code_hash, /* tp_hash */ |
| 197 | 0, /* tp_call */ |
| 198 | 0, /* tp_str */ |
| 199 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 200 | 0, /* tp_setattro */ |
| 201 | 0, /* tp_as_buffer */ |
| 202 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 203 | 0, /* tp_doc */ |
| 204 | 0, /* tp_traverse */ |
| 205 | 0, /* tp_clear */ |
| 206 | 0, /* tp_richcompare */ |
| 207 | 0, /* tp_weaklistoffset */ |
| 208 | 0, /* tp_iter */ |
| 209 | 0, /* tp_iternext */ |
| 210 | 0, /* tp_methods */ |
| 211 | code_memberlist, /* tp_members */ |
| 212 | 0, /* tp_getset */ |
| 213 | 0, /* tp_base */ |
| 214 | 0, /* tp_dict */ |
| 215 | 0, /* tp_descr_get */ |
| 216 | 0, /* tp_descr_set */ |
| 217 | 0, /* tp_dictoffset */ |
| 218 | 0, /* tp_init */ |
| 219 | 0, /* tp_alloc */ |
| 220 | 0, /* tp_new */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 221 | }; |
| 222 | |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 223 | #define NAME_CHARS \ |
| 224 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" |
| 225 | |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 226 | /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */ |
| 227 | |
| 228 | static int |
| 229 | all_name_chars(unsigned char *s) |
| 230 | { |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 231 | static char ok_name_char[256]; |
| 232 | static unsigned char *name_chars = (unsigned char *)NAME_CHARS; |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 233 | |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 234 | if (ok_name_char[*name_chars] == 0) { |
| 235 | unsigned char *p; |
| 236 | for (p = name_chars; *p; p++) |
| 237 | ok_name_char[*p] = 1; |
| 238 | } |
| 239 | while (*s) { |
| 240 | if (ok_name_char[*s++] == 0) |
| 241 | return 0; |
| 242 | } |
| 243 | return 1; |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 246 | static int |
| 247 | intern_strings(PyObject *tuple) |
| 248 | { |
| 249 | int i; |
| 250 | |
| 251 | for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { |
| 252 | PyObject *v = PyTuple_GET_ITEM(tuple, i); |
| 253 | if (v == NULL || !PyString_Check(v)) { |
| 254 | Py_FatalError("non-string found in code slot"); |
| 255 | PyErr_BadInternalCall(); |
| 256 | return -1; |
| 257 | } |
| 258 | PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i)); |
| 259 | } |
| 260 | return 0; |
| 261 | } |
| 262 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 263 | PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 264 | PyCode_New(int argcount, int nlocals, int stacksize, int flags, |
| 265 | PyObject *code, PyObject *consts, PyObject *names, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 266 | PyObject *varnames, PyObject *freevars, PyObject *cellvars, |
| 267 | PyObject *filename, PyObject *name, int firstlineno, |
| 268 | PyObject *lnotab) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 269 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 270 | PyCodeObject *co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 271 | int i; |
| 272 | /* Check argument types */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 273 | if (argcount < 0 || nlocals < 0 || |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 274 | code == NULL || |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 275 | consts == NULL || !PyTuple_Check(consts) || |
| 276 | names == NULL || !PyTuple_Check(names) || |
| 277 | varnames == NULL || !PyTuple_Check(varnames) || |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 278 | freevars == NULL || !PyTuple_Check(freevars) || |
| 279 | cellvars == NULL || !PyTuple_Check(cellvars) || |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 280 | name == NULL || !PyString_Check(name) || |
| 281 | filename == NULL || !PyString_Check(filename) || |
Jeremy Hylton | 9f64caa | 2001-11-09 22:02:48 +0000 | [diff] [blame] | 282 | lnotab == NULL || !PyString_Check(lnotab) || |
| 283 | !PyObject_CheckReadBuffer(code)) { |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 284 | PyErr_BadInternalCall(); |
| 285 | return NULL; |
| 286 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 287 | intern_strings(names); |
| 288 | intern_strings(varnames); |
| 289 | if (freevars == NULL) |
| 290 | freevars = PyTuple_New(0); |
| 291 | intern_strings(freevars); |
| 292 | if (cellvars == NULL) |
| 293 | cellvars = PyTuple_New(0); |
| 294 | intern_strings(cellvars); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 295 | /* Intern selected string constants */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 296 | for (i = PyTuple_Size(consts); --i >= 0; ) { |
| 297 | PyObject *v = PyTuple_GetItem(consts, i); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 298 | if (!PyString_Check(v)) |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 299 | continue; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 300 | if (!all_name_chars((unsigned char *)PyString_AS_STRING(v))) |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 301 | continue; |
| 302 | PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 303 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 304 | co = PyObject_NEW(PyCodeObject, &PyCode_Type); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 305 | if (co != NULL) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 306 | co->co_argcount = argcount; |
| 307 | co->co_nlocals = nlocals; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 308 | co->co_stacksize = stacksize; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 309 | co->co_flags = flags; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 310 | Py_INCREF(code); |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 311 | co->co_code = code; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 312 | Py_INCREF(consts); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 313 | co->co_consts = consts; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 314 | Py_INCREF(names); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 315 | co->co_names = names; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 316 | Py_INCREF(varnames); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 317 | co->co_varnames = varnames; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 318 | Py_INCREF(freevars); |
| 319 | co->co_freevars = freevars; |
| 320 | Py_INCREF(cellvars); |
| 321 | co->co_cellvars = cellvars; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 322 | Py_INCREF(filename); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 323 | co->co_filename = filename; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 324 | Py_INCREF(name); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 325 | co->co_name = name; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 326 | co->co_firstlineno = firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 327 | Py_INCREF(lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 328 | co->co_lnotab = lnotab; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 329 | } |
| 330 | return co; |
| 331 | } |
| 332 | |
| 333 | |
| 334 | /* Data structure used internally */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 335 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 336 | /* The compiler uses two passes to generate bytecodes. The first pass |
| 337 | builds the symbol table. The second pass generates the bytecode. |
| 338 | |
| 339 | The first pass uses a single symtable struct. The second pass uses |
| 340 | a compiling struct for each code block. The compiling structs |
| 341 | share a reference to the symtable. |
| 342 | |
| 343 | The two passes communicate via symtable_load_symbols() and via |
| 344 | is_local() and is_global(). The former initializes several slots |
| 345 | in the compiling struct: c_varnames, c_locals, c_nlocals, |
| 346 | c_argcount, c_globals, and c_flags. |
| 347 | */ |
| 348 | |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 349 | /* All about c_lnotab. |
| 350 | |
| 351 | c_lnotab is an array of unsigned bytes disguised as a Python string. In -O |
| 352 | mode, SET_LINENO opcodes aren't generated, and bytecode offsets are mapped |
| 353 | to source code line #s (when needed for tracebacks) via c_lnotab instead. |
| 354 | The array is conceptually a list of |
| 355 | (bytecode offset increment, line number increment) |
| 356 | pairs. The details are important and delicate, best illustrated by example: |
| 357 | |
| 358 | byte code offset source code line number |
| 359 | 0 1 |
| 360 | 6 2 |
| 361 | 50 7 |
| 362 | 350 307 |
| 363 | 361 308 |
| 364 | |
| 365 | The first trick is that these numbers aren't stored, only the increments |
| 366 | from one row to the next (this doesn't really work, but it's a start): |
| 367 | |
| 368 | 0, 1, 6, 1, 44, 5, 300, 300, 11, 1 |
| 369 | |
| 370 | The second trick is that an unsigned byte can't hold negative values, or |
| 371 | values larger than 255, so (a) there's a deep assumption that byte code |
| 372 | offsets and their corresponding line #s both increase monotonically, and (b) |
| 373 | if at least one column jumps by more than 255 from one row to the next, more |
| 374 | than one pair is written to the table. In case #b, there's no way to know |
| 375 | from looking at the table later how many were written. That's the delicate |
| 376 | part. A user of c_lnotab desiring to find the source line number |
| 377 | corresponding to a bytecode address A should do something like this |
| 378 | |
| 379 | lineno = addr = 0 |
| 380 | for addr_incr, line_incr in c_lnotab: |
| 381 | addr += addr_incr |
| 382 | if addr > A: |
| 383 | return lineno |
| 384 | lineno += line_incr |
| 385 | |
| 386 | In order for this to work, when the addr field increments by more than 255, |
| 387 | the line # increment in each pair generated must be 0 until the remaining addr |
| 388 | increment is < 256. So, in the example above, com_set_lineno should not (as |
| 389 | was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to |
| 390 | 255, 0, 45, 255, 0, 45. |
| 391 | */ |
| 392 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 393 | struct compiling { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 394 | PyObject *c_code; /* string */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 395 | PyObject *c_consts; /* list of objects */ |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 396 | PyObject *c_const_dict; /* inverse of c_consts */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 397 | PyObject *c_names; /* list of strings (names) */ |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 398 | PyObject *c_name_dict; /* inverse of c_names */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 399 | PyObject *c_globals; /* dictionary (value=None) */ |
| 400 | PyObject *c_locals; /* dictionary (value=localID) */ |
| 401 | PyObject *c_varnames; /* list (inverse of c_locals) */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 402 | PyObject *c_freevars; /* dictionary (value=None) */ |
| 403 | PyObject *c_cellvars; /* list */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 404 | int c_nlocals; /* index of next local */ |
| 405 | int c_argcount; /* number of top-level arguments */ |
| 406 | int c_flags; /* same as co_flags */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 407 | int c_nexti; /* index into c_code */ |
| 408 | int c_errors; /* counts errors occurred */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 409 | int c_infunction; /* set when compiling a function */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 410 | int c_interactive; /* generating code for interactive command */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 411 | int c_loops; /* counts nested loops */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 412 | int c_begin; /* begin of current loop, for 'continue' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 413 | int c_block[CO_MAXBLOCKS]; /* stack of block types */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 414 | int c_nblocks; /* current block stack level */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 415 | char *c_filename; /* filename of current node */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 416 | char *c_name; /* name of object (e.g. function) */ |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 417 | int c_lineno; /* Current line number */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 418 | int c_stacklevel; /* Current stack level */ |
| 419 | int c_maxstacklevel; /* Maximum stack level */ |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 420 | int c_firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 421 | PyObject *c_lnotab; /* Table mapping address to line number */ |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 422 | int c_last_addr, c_last_line, c_lnotab_next; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 423 | char *c_private; /* for private name mangling */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 424 | int c_tmpname; /* temporary local name counter */ |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 425 | int c_nested; /* Is block nested funcdef or lamdef? */ |
| 426 | int c_closure; /* Is nested w/freevars? */ |
| 427 | struct symtable *c_symtable; /* pointer to module symbol table */ |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 428 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 429 | }; |
| 430 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 431 | static int |
| 432 | is_free(int v) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 433 | { |
| 434 | if ((v & (USE | DEF_FREE)) |
| 435 | && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL))) |
| 436 | return 1; |
| 437 | if (v & DEF_FREE_CLASS) |
| 438 | return 1; |
| 439 | return 0; |
| 440 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 441 | |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 442 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 443 | com_error(struct compiling *c, PyObject *exc, char *msg) |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 444 | { |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 445 | PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL; |
| 446 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 447 | if (c == NULL) { |
| 448 | /* Error occurred via symtable call to |
| 449 | is_constant_false */ |
| 450 | PyErr_SetString(exc, msg); |
| 451 | return; |
| 452 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 453 | c->c_errors++; |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 454 | if (c->c_lineno < 1 || c->c_interactive) { |
| 455 | /* Unknown line number or interactive input */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 456 | PyErr_SetString(exc, msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 457 | return; |
| 458 | } |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 459 | v = PyString_FromString(msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 460 | if (v == NULL) |
| 461 | return; /* MemoryError, too bad */ |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 462 | |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 463 | line = PyErr_ProgramText(c->c_filename, c->c_lineno); |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 464 | if (line == NULL) { |
| 465 | Py_INCREF(Py_None); |
| 466 | line = Py_None; |
| 467 | } |
| 468 | t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno, |
| 469 | Py_None, line); |
| 470 | if (t == NULL) |
| 471 | goto exit; |
| 472 | w = Py_BuildValue("(OO)", v, t); |
| 473 | if (w == NULL) |
| 474 | goto exit; |
| 475 | PyErr_SetObject(exc, w); |
| 476 | exit: |
| 477 | Py_XDECREF(t); |
| 478 | Py_XDECREF(v); |
| 479 | Py_XDECREF(w); |
| 480 | Py_XDECREF(line); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 483 | /* Interface to the block stack */ |
| 484 | |
| 485 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 486 | block_push(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 487 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 488 | if (c->c_nblocks >= CO_MAXBLOCKS) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 489 | com_error(c, PyExc_SystemError, |
| 490 | "too many statically nested blocks"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 491 | } |
| 492 | else { |
| 493 | c->c_block[c->c_nblocks++] = type; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 498 | block_pop(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 499 | { |
| 500 | if (c->c_nblocks > 0) |
| 501 | c->c_nblocks--; |
| 502 | 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] | 503 | com_error(c, PyExc_SystemError, "bad block pop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 507 | /* Prototype forward declarations */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 508 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 509 | static int com_init(struct compiling *, char *); |
| 510 | static void com_free(struct compiling *); |
| 511 | static void com_push(struct compiling *, int); |
| 512 | static void com_pop(struct compiling *, int); |
| 513 | static void com_done(struct compiling *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 514 | static void com_node(struct compiling *, node *); |
| 515 | static void com_factor(struct compiling *, node *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 516 | static void com_addbyte(struct compiling *, int); |
| 517 | static void com_addint(struct compiling *, int); |
| 518 | static void com_addoparg(struct compiling *, int, int); |
| 519 | static void com_addfwref(struct compiling *, int, int *); |
| 520 | static void com_backpatch(struct compiling *, int); |
| 521 | static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *); |
| 522 | static int com_addconst(struct compiling *, PyObject *); |
| 523 | static int com_addname(struct compiling *, PyObject *); |
| 524 | static void com_addopname(struct compiling *, int, node *); |
| 525 | static void com_list(struct compiling *, node *, int); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 526 | static void com_list_iter(struct compiling *, node *, node *, char *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 527 | static int com_argdefs(struct compiling *, node *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 528 | static void com_assign(struct compiling *, node *, int, node *); |
| 529 | static void com_assign_name(struct compiling *, node *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 530 | static PyCodeObject *icompile(node *, struct compiling *); |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 531 | static PyCodeObject *jcompile(node *, char *, struct compiling *, |
| 532 | PyCompilerFlags *); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 533 | static PyObject *parsestrplus(struct compiling*, node *); |
| 534 | static PyObject *parsestr(struct compiling *, char *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 535 | static node *get_rawdocstring(node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 536 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 537 | static int get_ref_type(struct compiling *, char *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 538 | |
| 539 | /* symtable operations */ |
| 540 | static int symtable_build(struct compiling *, node *); |
| 541 | static int symtable_load_symbols(struct compiling *); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 542 | static struct symtable *symtable_init(void); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 543 | static void symtable_enter_scope(struct symtable *, char *, int, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 544 | static int symtable_exit_scope(struct symtable *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 545 | static int symtable_add_def(struct symtable *, char *, int); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 546 | static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 547 | |
| 548 | static void symtable_node(struct symtable *, node *); |
| 549 | static void symtable_funcdef(struct symtable *, node *); |
| 550 | static void symtable_default_args(struct symtable *, node *); |
| 551 | static void symtable_params(struct symtable *, node *); |
| 552 | static void symtable_params_fplist(struct symtable *, node *n); |
| 553 | static void symtable_global(struct symtable *, node *); |
| 554 | static void symtable_import(struct symtable *, node *); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 555 | static void symtable_assign(struct symtable *, node *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 556 | static void symtable_list_comprehension(struct symtable *, node *); |
| 557 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 558 | static int symtable_update_free_vars(struct symtable *); |
| 559 | static int symtable_undo_free(struct symtable *, PyObject *, PyObject *); |
| 560 | static int symtable_check_global(struct symtable *, PyObject *, PyObject *); |
| 561 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 562 | /* helper */ |
| 563 | static void |
| 564 | do_pad(int pad) |
| 565 | { |
| 566 | int i; |
| 567 | for (i = 0; i < pad; ++i) |
| 568 | fprintf(stderr, " "); |
| 569 | } |
| 570 | |
| 571 | static void |
| 572 | dump(node *n, int pad, int depth) |
| 573 | { |
| 574 | int i; |
| 575 | if (depth == 0) |
| 576 | return; |
| 577 | do_pad(pad); |
| 578 | fprintf(stderr, "%d: %s\n", TYPE(n), STR(n)); |
| 579 | if (depth > 0) |
| 580 | depth--; |
| 581 | for (i = 0; i < NCH(n); ++i) |
| 582 | dump(CHILD(n, i), pad + 1, depth); |
| 583 | } |
| 584 | |
| 585 | #define DUMP(N) dump(N, 0, -1) |
| 586 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 587 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 588 | com_init(struct compiling *c, char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 589 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 590 | memset((void *)c, '\0', sizeof(struct compiling)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 591 | if ((c->c_code = PyString_FromStringAndSize((char *)NULL, |
| 592 | 1000)) == 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_consts = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 595 | goto fail; |
| 596 | if ((c->c_const_dict = PyDict_New()) == NULL) |
| 597 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 598 | if ((c->c_names = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 599 | goto fail; |
| 600 | if ((c->c_name_dict = PyDict_New()) == NULL) |
| 601 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 602 | if ((c->c_locals = PyDict_New()) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 603 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 604 | if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL, |
| 605 | 1000)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 606 | goto fail; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 607 | c->c_globals = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 608 | c->c_varnames = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 609 | c->c_freevars = NULL; |
| 610 | c->c_cellvars = NULL; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 611 | c->c_nlocals = 0; |
| 612 | c->c_argcount = 0; |
| 613 | c->c_flags = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 614 | c->c_nexti = 0; |
| 615 | c->c_errors = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 616 | c->c_infunction = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 617 | c->c_interactive = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 618 | c->c_loops = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 619 | c->c_begin = 0; |
| 620 | c->c_nblocks = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 621 | c->c_filename = filename; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 622 | c->c_name = "?"; |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 623 | c->c_lineno = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 624 | c->c_stacklevel = 0; |
| 625 | c->c_maxstacklevel = 0; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 626 | c->c_firstlineno = 0; |
| 627 | c->c_last_addr = 0; |
| 628 | c->c_last_line = 0; |
Barry Warsaw | 174e801 | 2001-01-22 04:35:57 +0000 | [diff] [blame] | 629 | c->c_lnotab_next = 0; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 630 | c->c_tmpname = 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 631 | c->c_nested = 0; |
| 632 | c->c_closure = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 633 | c->c_symtable = NULL; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 634 | return 1; |
| 635 | |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 636 | fail: |
| 637 | com_free(c); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 642 | com_free(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 643 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 644 | Py_XDECREF(c->c_code); |
| 645 | Py_XDECREF(c->c_consts); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 646 | Py_XDECREF(c->c_const_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 647 | Py_XDECREF(c->c_names); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 648 | Py_XDECREF(c->c_name_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 649 | Py_XDECREF(c->c_globals); |
| 650 | Py_XDECREF(c->c_locals); |
| 651 | Py_XDECREF(c->c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 652 | Py_XDECREF(c->c_freevars); |
| 653 | Py_XDECREF(c->c_cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 654 | Py_XDECREF(c->c_lnotab); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 655 | if (c->c_future) |
| 656 | PyMem_Free((void *)c->c_future); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 660 | com_push(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 661 | { |
| 662 | c->c_stacklevel += n; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 663 | if (c->c_stacklevel > c->c_maxstacklevel) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 664 | c->c_maxstacklevel = c->c_stacklevel; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 665 | /* |
| 666 | fprintf(stderr, "%s:%s:%d max stack nexti=%d level=%d n=%d\n", |
| 667 | c->c_filename, c->c_name, c->c_lineno, |
| 668 | c->c_nexti, c->c_stacklevel, n); |
| 669 | */ |
| 670 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 674 | com_pop(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 675 | { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 676 | if (c->c_stacklevel < n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 677 | c->c_stacklevel = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 678 | else |
| 679 | c->c_stacklevel -= n; |
| 680 | } |
| 681 | |
| 682 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 683 | com_done(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 684 | { |
| 685 | if (c->c_code != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 686 | _PyString_Resize(&c->c_code, c->c_nexti); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 687 | if (c->c_lnotab != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 688 | _PyString_Resize(&c->c_lnotab, c->c_lnotab_next); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 691 | static int |
| 692 | com_check_size(PyObject **s, int offset) |
| 693 | { |
| 694 | int len = PyString_GET_SIZE(*s); |
| 695 | if (offset >= len) |
| 696 | return _PyString_Resize(s, len * 2); |
| 697 | return 0; |
| 698 | } |
| 699 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 700 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 701 | com_addbyte(struct compiling *c, int byte) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 702 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 703 | /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 704 | assert(byte >= 0 && byte <= 255); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 705 | assert(c->c_code); |
| 706 | if (com_check_size(&c->c_code, c->c_nexti)) { |
| 707 | c->c_errors++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 708 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 709 | } |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 710 | PyString_AS_STRING(c->c_code)[c->c_nexti++] = byte; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 714 | com_addint(struct compiling *c, int x) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 715 | { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 716 | com_addbyte(c, x & 0xff); |
| 717 | com_addbyte(c, x >> 8); /* XXX x should be positive */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 721 | com_add_lnotab(struct compiling *c, int addr, int line) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 722 | { |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 723 | char *p; |
| 724 | if (c->c_lnotab == NULL) |
| 725 | return; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 726 | if (com_check_size(&c->c_lnotab, c->c_lnotab_next + 2)) { |
| 727 | c->c_errors++; |
| 728 | return; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 729 | } |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 730 | p = PyString_AS_STRING(c->c_lnotab) + c->c_lnotab_next; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 731 | *p++ = addr; |
| 732 | *p++ = line; |
| 733 | c->c_lnotab_next += 2; |
| 734 | } |
| 735 | |
| 736 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 737 | com_set_lineno(struct compiling *c, int lineno) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 738 | { |
| 739 | c->c_lineno = lineno; |
| 740 | if (c->c_firstlineno == 0) { |
| 741 | c->c_firstlineno = c->c_last_line = lineno; |
| 742 | } |
| 743 | else { |
| 744 | int incr_addr = c->c_nexti - c->c_last_addr; |
| 745 | int incr_line = lineno - c->c_last_line; |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 746 | while (incr_addr > 255) { |
| 747 | com_add_lnotab(c, 255, 0); |
| 748 | incr_addr -= 255; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 749 | } |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 750 | while (incr_line > 255) { |
| 751 | com_add_lnotab(c, incr_addr, 255); |
| 752 | incr_line -=255; |
| 753 | incr_addr = 0; |
| 754 | } |
| 755 | if (incr_addr > 0 || incr_line > 0) |
| 756 | com_add_lnotab(c, incr_addr, incr_line); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 757 | c->c_last_addr = c->c_nexti; |
| 758 | c->c_last_line = lineno; |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 763 | com_addoparg(struct compiling *c, int op, int arg) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 764 | { |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 765 | int extended_arg = arg >> 16; |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 766 | if (op == SET_LINENO) { |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 767 | com_set_lineno(c, arg); |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 768 | if (Py_OptimizeFlag) |
| 769 | return; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 770 | } |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 771 | if (extended_arg){ |
| 772 | com_addbyte(c, EXTENDED_ARG); |
| 773 | com_addint(c, extended_arg); |
| 774 | arg &= 0xffff; |
| 775 | } |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 776 | com_addbyte(c, op); |
| 777 | com_addint(c, arg); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 781 | com_addfwref(struct compiling *c, int op, int *p_anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 782 | { |
| 783 | /* Compile a forward reference for backpatching */ |
| 784 | int here; |
| 785 | int anchor; |
| 786 | com_addbyte(c, op); |
| 787 | here = c->c_nexti; |
| 788 | anchor = *p_anchor; |
| 789 | *p_anchor = here; |
| 790 | com_addint(c, anchor == 0 ? 0 : here - anchor); |
| 791 | } |
| 792 | |
| 793 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 794 | com_backpatch(struct compiling *c, int anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 795 | { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 796 | unsigned char *code = (unsigned char *) PyString_AS_STRING(c->c_code); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 797 | int target = c->c_nexti; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 798 | int dist; |
| 799 | int prev; |
| 800 | for (;;) { |
| 801 | /* Make the JUMP instruction at anchor point to target */ |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 802 | prev = code[anchor] + (code[anchor+1] << 8); |
| 803 | dist = target - (anchor+2); |
| 804 | code[anchor] = dist & 0xff; |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 805 | dist >>= 8; |
| 806 | code[anchor+1] = dist; |
| 807 | dist >>= 8; |
| 808 | if (dist) { |
| 809 | com_error(c, PyExc_SystemError, |
| 810 | "com_backpatch: offset too large"); |
| 811 | break; |
| 812 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 813 | if (!prev) |
| 814 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 815 | anchor -= prev; |
| 816 | } |
| 817 | } |
| 818 | |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 819 | /* Handle literals and names uniformly */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 820 | |
| 821 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 822 | com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 823 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 824 | PyObject *w, *t, *np=NULL; |
| 825 | long n; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 826 | |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 827 | t = Py_BuildValue("(OO)", v, v->ob_type); |
| 828 | if (t == NULL) |
| 829 | goto fail; |
| 830 | w = PyDict_GetItem(dict, t); |
| 831 | if (w != NULL) { |
| 832 | n = PyInt_AsLong(w); |
| 833 | } else { |
| 834 | n = PyList_Size(list); |
| 835 | np = PyInt_FromLong(n); |
| 836 | if (np == NULL) |
| 837 | goto fail; |
| 838 | if (PyList_Append(list, v) != 0) |
| 839 | goto fail; |
| 840 | if (PyDict_SetItem(dict, t, np) != 0) |
| 841 | goto fail; |
| 842 | Py_DECREF(np); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 843 | } |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 844 | Py_DECREF(t); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 845 | return n; |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 846 | fail: |
| 847 | Py_XDECREF(np); |
| 848 | Py_XDECREF(t); |
| 849 | c->c_errors++; |
| 850 | return 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 854 | com_addconst(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 855 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 856 | 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] | 857 | } |
| 858 | |
| 859 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 860 | com_addname(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 861 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 862 | 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] | 863 | } |
| 864 | |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 865 | static int |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 866 | mangle(char *p, char *name, char *buffer, size_t maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 867 | { |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 868 | /* Name mangling: __private becomes _classname__private. |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 869 | This is independent from how the name is used. */ |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 870 | size_t nlen, plen; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 871 | if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_') |
| 872 | return 0; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 873 | nlen = strlen(name); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 874 | if (nlen+2 >= maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 875 | return 0; /* Don't mangle __extremely_long_names */ |
| 876 | if (name[nlen-1] == '_' && name[nlen-2] == '_') |
| 877 | return 0; /* Don't mangle __whatever__ */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 878 | /* Strip leading underscores from class name */ |
| 879 | while (*p == '_') |
| 880 | p++; |
| 881 | if (*p == '\0') |
| 882 | return 0; /* Don't mangle if class is just underscores */ |
| 883 | plen = strlen(p); |
| 884 | if (plen + nlen >= maxlen) |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 885 | plen = maxlen-nlen-2; /* Truncate class name if too long */ |
| 886 | /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 887 | buffer[0] = '_'; |
| 888 | strncpy(buffer+1, p, plen); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 889 | strcpy(buffer+1+plen, name); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 890 | return 1; |
| 891 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 892 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 893 | static void |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 894 | com_addop_name(struct compiling *c, int op, char *name) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 895 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 896 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 897 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 898 | char buffer[MANGLE_LEN]; |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 899 | |
| 900 | if (mangle(c->c_private, name, buffer, sizeof(buffer))) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 901 | name = buffer; |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 902 | if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 903 | c->c_errors++; |
| 904 | i = 255; |
| 905 | } |
| 906 | else { |
| 907 | i = com_addname(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 908 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 909 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 910 | com_addoparg(c, op, i); |
| 911 | } |
| 912 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 913 | #define NAME_LOCAL 0 |
| 914 | #define NAME_GLOBAL 1 |
| 915 | #define NAME_DEFAULT 2 |
| 916 | #define NAME_CLOSURE 3 |
| 917 | |
| 918 | static int |
| 919 | com_lookup_arg(PyObject *dict, PyObject *name) |
| 920 | { |
| 921 | PyObject *v = PyDict_GetItem(dict, name); |
| 922 | if (v == NULL) |
| 923 | return -1; |
| 924 | else |
| 925 | return PyInt_AS_LONG(v); |
| 926 | } |
| 927 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 928 | static void |
| 929 | com_addop_varname(struct compiling *c, int kind, char *name) |
| 930 | { |
| 931 | PyObject *v; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 932 | int i, reftype; |
| 933 | int scope = NAME_DEFAULT; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 934 | int op = STOP_CODE; |
| 935 | char buffer[MANGLE_LEN]; |
| 936 | |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 937 | if (mangle(c->c_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 938 | name = buffer; |
| 939 | if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { |
| 940 | c->c_errors++; |
| 941 | i = 255; |
| 942 | goto done; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 943 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 944 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 945 | reftype = get_ref_type(c, name); |
| 946 | switch (reftype) { |
| 947 | case LOCAL: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 948 | if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 949 | scope = NAME_LOCAL; |
| 950 | break; |
| 951 | case GLOBAL_EXPLICIT: |
| 952 | scope = NAME_GLOBAL; |
| 953 | break; |
| 954 | case GLOBAL_IMPLICIT: |
| 955 | if (c->c_flags & CO_OPTIMIZED) |
| 956 | scope = NAME_GLOBAL; |
| 957 | break; |
| 958 | case FREE: |
| 959 | case CELL: |
| 960 | scope = NAME_CLOSURE; |
| 961 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | i = com_addname(c, v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 965 | if (scope == NAME_LOCAL) |
| 966 | i = com_lookup_arg(c->c_locals, v); |
| 967 | else if (reftype == FREE) |
| 968 | i = com_lookup_arg(c->c_freevars, v); |
| 969 | else if (reftype == CELL) |
| 970 | i = com_lookup_arg(c->c_cellvars, v); |
| 971 | if (i == -1) { |
| 972 | c->c_errors++; /* XXX no exception set */ |
| 973 | i = 255; |
| 974 | goto done; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 975 | } |
| 976 | Py_DECREF(v); |
| 977 | |
| 978 | switch (kind) { |
| 979 | case VAR_LOAD: |
| 980 | switch (scope) { |
| 981 | case NAME_LOCAL: |
| 982 | op = LOAD_FAST; |
| 983 | break; |
| 984 | case NAME_GLOBAL: |
| 985 | op = LOAD_GLOBAL; |
| 986 | break; |
| 987 | case NAME_DEFAULT: |
| 988 | op = LOAD_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 989 | break; |
| 990 | case NAME_CLOSURE: |
| 991 | op = LOAD_DEREF; |
| 992 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 993 | } |
| 994 | break; |
| 995 | case VAR_STORE: |
| 996 | switch (scope) { |
| 997 | case NAME_LOCAL: |
| 998 | op = STORE_FAST; |
| 999 | break; |
| 1000 | case NAME_GLOBAL: |
| 1001 | op = STORE_GLOBAL; |
| 1002 | break; |
| 1003 | case NAME_DEFAULT: |
| 1004 | op = STORE_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1005 | break; |
| 1006 | case NAME_CLOSURE: |
| 1007 | op = STORE_DEREF; |
| 1008 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1009 | } |
| 1010 | break; |
| 1011 | case VAR_DELETE: |
| 1012 | switch (scope) { |
| 1013 | case NAME_LOCAL: |
| 1014 | op = DELETE_FAST; |
| 1015 | break; |
| 1016 | case NAME_GLOBAL: |
| 1017 | op = DELETE_GLOBAL; |
| 1018 | break; |
| 1019 | case NAME_DEFAULT: |
| 1020 | op = DELETE_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1021 | break; |
| 1022 | case NAME_CLOSURE: { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 1023 | char buf[500]; |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 1024 | PyOS_snprintf(buf, sizeof(buf), |
| 1025 | DEL_CLOSURE_ERROR, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1026 | com_error(c, PyExc_SyntaxError, buf); |
| 1027 | i = 255; |
| 1028 | break; |
| 1029 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1030 | } |
| 1031 | break; |
| 1032 | } |
| 1033 | done: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1034 | com_addoparg(c, op, i); |
| 1035 | } |
| 1036 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1037 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1038 | com_addopname(struct compiling *c, int op, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1039 | { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1040 | char *name; |
| 1041 | char buffer[1000]; |
| 1042 | /* XXX it is possible to write this code without the 1000 |
| 1043 | chars on the total length of dotted names, I just can't be |
| 1044 | bothered right now */ |
| 1045 | if (TYPE(n) == STAR) |
| 1046 | name = "*"; |
| 1047 | else if (TYPE(n) == dotted_name) { |
| 1048 | char *p = buffer; |
| 1049 | int i; |
| 1050 | name = buffer; |
| 1051 | for (i = 0; i < NCH(n); i += 2) { |
| 1052 | char *s = STR(CHILD(n, i)); |
| 1053 | if (p + strlen(s) > buffer + (sizeof buffer) - 2) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1054 | com_error(c, PyExc_MemoryError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1055 | "dotted_name too long"); |
Guido van Rossum | d9dfaf5 | 1995-02-17 15:04:57 +0000 | [diff] [blame] | 1056 | name = NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1057 | break; |
| 1058 | } |
| 1059 | if (p != buffer) |
| 1060 | *p++ = '.'; |
| 1061 | strcpy(p, s); |
| 1062 | p = strchr(p, '\0'); |
| 1063 | } |
| 1064 | } |
| 1065 | else { |
| 1066 | REQ(n, NAME); |
| 1067 | name = STR(n); |
| 1068 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1069 | com_addop_name(c, op, name); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1072 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1073 | parsenumber(struct compiling *co, char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1074 | { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1075 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1076 | long x; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1077 | double dx; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1078 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 1079 | Py_complex c; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1080 | int imflag; |
| 1081 | #endif |
| 1082 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1083 | errno = 0; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1084 | end = s + strlen(s) - 1; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1085 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 15ad9a6 | 1996-01-26 20:53:56 +0000 | [diff] [blame] | 1086 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1087 | #endif |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 1088 | if (*end == 'l' || *end == 'L') |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1089 | return PyLong_FromString(s, (char **)0, 0); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 1090 | if (s[0] == '0') |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1091 | x = (long) PyOS_strtoul(s, &end, 0); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 1092 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1093 | x = PyOS_strtol(s, &end, 0); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1094 | if (*end == '\0') { |
Jeremy Hylton | 71b6af9 | 2001-08-27 19:45:25 +0000 | [diff] [blame] | 1095 | if (errno != 0) |
| 1096 | return PyLong_FromString(s, (char **)0, 0); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1097 | return PyInt_FromLong(x); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1098 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1099 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1100 | #ifndef WITHOUT_COMPLEX |
| 1101 | if (imflag) { |
| 1102 | c.real = 0.; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1103 | PyFPE_START_PROTECT("atof", return 0) |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1104 | c.imag = atof(s); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1105 | PyFPE_END_PROTECT(c) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1106 | return PyComplex_FromCComplex(c); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1107 | } |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1108 | else |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1109 | #endif |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1110 | { |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1111 | PyFPE_START_PROTECT("atof", return 0) |
| 1112 | dx = atof(s); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1113 | PyFPE_END_PROTECT(dx) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1114 | return PyFloat_FromDouble(dx); |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1115 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1118 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1119 | parsestr(struct compiling *com, char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1120 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1121 | PyObject *v; |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1122 | size_t len; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1123 | char *buf; |
| 1124 | char *p; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1125 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1126 | int c; |
Guido van Rossum | 5f5e817 | 1997-04-06 03:41:40 +0000 | [diff] [blame] | 1127 | int first = *s; |
| 1128 | int quote = first; |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1129 | int rawmode = 0; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1130 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1131 | int unicode = 0; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1132 | #endif |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1133 | if (isalpha(quote) || quote == '_') { |
| 1134 | if (quote == 'u' || quote == 'U') { |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1135 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1136 | quote = *++s; |
| 1137 | unicode = 1; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1138 | #else |
| 1139 | com_error(com, PyExc_SyntaxError, |
| 1140 | "Unicode literals not supported in this Python"); |
| 1141 | return NULL; |
| 1142 | #endif |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1143 | } |
| 1144 | if (quote == 'r' || quote == 'R') { |
| 1145 | quote = *++s; |
| 1146 | rawmode = 1; |
| 1147 | } |
| 1148 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1149 | if (quote != '\'' && quote != '\"') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1150 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1151 | return NULL; |
| 1152 | } |
| 1153 | s++; |
| 1154 | len = strlen(s); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1155 | if (len > INT_MAX) { |
| 1156 | PyErr_SetString(PyExc_OverflowError, "string to parse is too long"); |
| 1157 | return NULL; |
| 1158 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1159 | if (s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1160 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1161 | return NULL; |
| 1162 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1163 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
| 1164 | s += 2; |
| 1165 | len -= 2; |
| 1166 | if (s[--len] != quote || s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1167 | PyErr_BadInternalCall(); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1168 | return NULL; |
| 1169 | } |
| 1170 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1171 | #ifdef Py_USING_UNICODE |
Guido van Rossum | fdc8bdb | 2000-05-01 17:54:56 +0000 | [diff] [blame] | 1172 | if (unicode || Py_UnicodeFlag) { |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1173 | if (rawmode) |
| 1174 | return PyUnicode_DecodeRawUnicodeEscape( |
| 1175 | s, len, NULL); |
| 1176 | else |
| 1177 | return PyUnicode_DecodeUnicodeEscape( |
| 1178 | s, len, NULL); |
| 1179 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1180 | #endif |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1181 | if (rawmode || strchr(s, '\\') == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1182 | return PyString_FromStringAndSize(s, len); |
| 1183 | v = PyString_FromStringAndSize((char *)NULL, len); |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1184 | if (v == NULL) |
| 1185 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1186 | p = buf = PyString_AsString(v); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1187 | end = s + len; |
| 1188 | while (s < end) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1189 | if (*s != '\\') { |
| 1190 | *p++ = *s++; |
| 1191 | continue; |
| 1192 | } |
| 1193 | s++; |
| 1194 | switch (*s++) { |
| 1195 | /* XXX This assumes ASCII! */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1196 | case '\n': break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1197 | case '\\': *p++ = '\\'; break; |
| 1198 | case '\'': *p++ = '\''; break; |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1199 | case '\"': *p++ = '\"'; break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1200 | case 'b': *p++ = '\b'; break; |
| 1201 | case 'f': *p++ = '\014'; break; /* FF */ |
| 1202 | case 't': *p++ = '\t'; break; |
| 1203 | case 'n': *p++ = '\n'; break; |
| 1204 | case 'r': *p++ = '\r'; break; |
| 1205 | case 'v': *p++ = '\013'; break; /* VT */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1206 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 1207 | case '0': case '1': case '2': case '3': |
| 1208 | case '4': case '5': case '6': case '7': |
| 1209 | c = s[-1] - '0'; |
| 1210 | if ('0' <= *s && *s <= '7') { |
| 1211 | c = (c<<3) + *s++ - '0'; |
| 1212 | if ('0' <= *s && *s <= '7') |
| 1213 | c = (c<<3) + *s++ - '0'; |
| 1214 | } |
| 1215 | *p++ = c; |
| 1216 | break; |
| 1217 | case 'x': |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 1218 | if (isxdigit(Py_CHARMASK(s[0])) |
| 1219 | && isxdigit(Py_CHARMASK(s[1]))) { |
Guido van Rossum | ed1100f | 1997-10-20 23:24:07 +0000 | [diff] [blame] | 1220 | unsigned int x = 0; |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1221 | c = Py_CHARMASK(*s); |
| 1222 | s++; |
| 1223 | if (isdigit(c)) |
| 1224 | x = c - '0'; |
| 1225 | else if (islower(c)) |
| 1226 | x = 10 + c - 'a'; |
| 1227 | else |
| 1228 | x = 10 + c - 'A'; |
| 1229 | x = x << 4; |
| 1230 | c = Py_CHARMASK(*s); |
| 1231 | s++; |
| 1232 | if (isdigit(c)) |
| 1233 | x += c - '0'; |
| 1234 | else if (islower(c)) |
| 1235 | x += 10 + c - 'a'; |
| 1236 | else |
| 1237 | x += 10 + c - 'A'; |
Guido van Rossum | ed1100f | 1997-10-20 23:24:07 +0000 | [diff] [blame] | 1238 | *p++ = x; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1239 | break; |
| 1240 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 1241 | PyErr_SetString(PyExc_ValueError, |
| 1242 | "invalid \\x escape"); |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1243 | Py_DECREF(v); |
| 1244 | return NULL; |
| 1245 | default: |
| 1246 | *p++ = '\\'; |
| 1247 | *p++ = s[-1]; |
| 1248 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1249 | } |
| 1250 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1251 | _PyString_Resize(&v, (int)(p - buf)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1252 | return v; |
| 1253 | } |
| 1254 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1255 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1256 | parsestrplus(struct compiling* c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1257 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1258 | PyObject *v; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1259 | int i; |
| 1260 | REQ(CHILD(n, 0), STRING); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1261 | if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1262 | /* String literal concatenation */ |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1263 | for (i = 1; i < NCH(n); i++) { |
| 1264 | PyObject *s; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1265 | s = parsestr(c, STR(CHILD(n, i))); |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1266 | if (s == NULL) |
| 1267 | goto onError; |
| 1268 | if (PyString_Check(v) && PyString_Check(s)) { |
| 1269 | PyString_ConcatAndDel(&v, s); |
| 1270 | if (v == NULL) |
| 1271 | goto onError; |
| 1272 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1273 | #ifdef Py_USING_UNICODE |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1274 | else { |
| 1275 | PyObject *temp; |
| 1276 | temp = PyUnicode_Concat(v, s); |
| 1277 | Py_DECREF(s); |
| 1278 | if (temp == NULL) |
| 1279 | goto onError; |
| 1280 | Py_DECREF(v); |
| 1281 | v = temp; |
| 1282 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1283 | #endif |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1284 | } |
| 1285 | } |
| 1286 | return v; |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1287 | |
| 1288 | onError: |
| 1289 | Py_XDECREF(v); |
| 1290 | return NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1293 | static void |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1294 | 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] | 1295 | { |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1296 | int anchor = 0; |
| 1297 | int save_begin = c->c_begin; |
| 1298 | |
| 1299 | /* list_iter: for v in expr [list_iter] */ |
| 1300 | com_node(c, CHILD(n, 3)); /* expr */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1301 | com_addbyte(c, GET_ITER); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1302 | c->c_begin = c->c_nexti; |
| 1303 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1304 | com_addfwref(c, FOR_ITER, &anchor); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1305 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1306 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1307 | c->c_loops++; |
| 1308 | com_list_iter(c, n, e, t); |
| 1309 | c->c_loops--; |
| 1310 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 1311 | c->c_begin = save_begin; |
| 1312 | com_backpatch(c, anchor); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1313 | com_pop(c, 1); /* FOR_ITER has popped this */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | static void |
| 1317 | com_list_if(struct compiling *c, node *n, node *e, char *t) |
| 1318 | { |
| 1319 | int anchor = 0; |
| 1320 | int a = 0; |
| 1321 | /* list_iter: 'if' test [list_iter] */ |
| 1322 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 1323 | com_node(c, CHILD(n, 1)); |
| 1324 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 1325 | com_addbyte(c, POP_TOP); |
| 1326 | com_pop(c, 1); |
| 1327 | com_list_iter(c, n, e, t); |
| 1328 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 1329 | com_backpatch(c, a); |
| 1330 | /* We jump here with an extra entry which we now pop */ |
| 1331 | com_addbyte(c, POP_TOP); |
| 1332 | com_backpatch(c, anchor); |
| 1333 | } |
| 1334 | |
| 1335 | static void |
| 1336 | com_list_iter(struct compiling *c, |
| 1337 | node *p, /* parent of list_iter node */ |
| 1338 | node *e, /* element expression node */ |
| 1339 | char *t /* name of result list temp local */) |
| 1340 | { |
| 1341 | /* list_iter is the last child in a listmaker, list_for, or list_if */ |
| 1342 | node *n = CHILD(p, NCH(p)-1); |
| 1343 | if (TYPE(n) == list_iter) { |
| 1344 | n = CHILD(n, 0); |
| 1345 | switch (TYPE(n)) { |
| 1346 | case list_for: |
| 1347 | com_list_for(c, n, e, t); |
| 1348 | break; |
| 1349 | case list_if: |
| 1350 | com_list_if(c, n, e, t); |
| 1351 | break; |
| 1352 | default: |
| 1353 | com_error(c, PyExc_SystemError, |
| 1354 | "invalid list_iter node type"); |
| 1355 | } |
| 1356 | } |
| 1357 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1358 | com_addop_varname(c, VAR_LOAD, t); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1359 | com_push(c, 1); |
| 1360 | com_node(c, e); |
| 1361 | com_addoparg(c, CALL_FUNCTION, 1); |
| 1362 | com_addbyte(c, POP_TOP); |
| 1363 | com_pop(c, 2); |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | static void |
| 1368 | com_list_comprehension(struct compiling *c, node *n) |
| 1369 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1370 | /* listmaker: test list_for */ |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 1371 | char tmpname[30]; |
| 1372 | PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->c_tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1373 | com_addoparg(c, BUILD_LIST, 0); |
| 1374 | com_addbyte(c, DUP_TOP); /* leave the result on the stack */ |
| 1375 | com_push(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1376 | com_addop_name(c, LOAD_ATTR, "append"); |
| 1377 | com_addop_varname(c, VAR_STORE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1378 | com_pop(c, 1); |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1379 | com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1380 | com_addop_varname(c, VAR_DELETE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1381 | --c->c_tmpname; |
| 1382 | } |
| 1383 | |
| 1384 | static void |
| 1385 | com_listmaker(struct compiling *c, node *n) |
| 1386 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1387 | /* listmaker: test ( list_for | (',' test)* [','] ) */ |
| 1388 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1389 | com_list_comprehension(c, n); |
| 1390 | else { |
| 1391 | int len = 0; |
| 1392 | int i; |
| 1393 | for (i = 0; i < NCH(n); i += 2, len++) |
| 1394 | com_node(c, CHILD(n, i)); |
| 1395 | com_addoparg(c, BUILD_LIST, len); |
| 1396 | com_pop(c, len-1); |
| 1397 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1401 | com_dictmaker(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1402 | { |
| 1403 | int i; |
| 1404 | /* dictmaker: test ':' test (',' test ':' value)* [','] */ |
| 1405 | for (i = 0; i+2 < NCH(n); i += 4) { |
| 1406 | /* We must arrange things just right for STORE_SUBSCR. |
| 1407 | It wants the stack to look like (value) (dict) (key) */ |
| 1408 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1409 | com_push(c, 1); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1410 | com_node(c, CHILD(n, i+2)); /* value */ |
| 1411 | com_addbyte(c, ROT_TWO); |
| 1412 | com_node(c, CHILD(n, i)); /* key */ |
| 1413 | com_addbyte(c, STORE_SUBSCR); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1414 | com_pop(c, 3); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1419 | com_atom(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1420 | { |
| 1421 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1422 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1423 | int i; |
| 1424 | REQ(n, atom); |
| 1425 | ch = CHILD(n, 0); |
| 1426 | switch (TYPE(ch)) { |
| 1427 | case LPAR: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1428 | if (TYPE(CHILD(n, 1)) == RPAR) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1429 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1430 | com_push(c, 1); |
| 1431 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1432 | else |
| 1433 | com_node(c, CHILD(n, 1)); |
| 1434 | break; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1435 | case LSQB: /* '[' [listmaker] ']' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1436 | if (TYPE(CHILD(n, 1)) == RSQB) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1437 | com_addoparg(c, BUILD_LIST, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1438 | com_push(c, 1); |
| 1439 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1440 | else |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1441 | com_listmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1442 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1443 | case LBRACE: /* '{' [dictmaker] '}' */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1444 | com_addoparg(c, BUILD_MAP, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1445 | com_push(c, 1); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1446 | if (TYPE(CHILD(n, 1)) == dictmaker) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1447 | com_dictmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1448 | break; |
| 1449 | case BACKQUOTE: |
| 1450 | com_node(c, CHILD(n, 1)); |
| 1451 | com_addbyte(c, UNARY_CONVERT); |
| 1452 | break; |
| 1453 | case NUMBER: |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1454 | if ((v = parsenumber(c, STR(ch))) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1455 | i = 255; |
| 1456 | } |
| 1457 | else { |
| 1458 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1459 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1460 | } |
| 1461 | com_addoparg(c, LOAD_CONST, i); |
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 | case STRING: |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1465 | v = parsestrplus(c, n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1466 | if (v == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1467 | c->c_errors++; |
| 1468 | i = 255; |
| 1469 | } |
| 1470 | else { |
| 1471 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1472 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1473 | } |
| 1474 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1475 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1476 | break; |
| 1477 | case NAME: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1478 | com_addop_varname(c, VAR_LOAD, STR(ch)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1479 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1480 | break; |
| 1481 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1482 | com_error(c, PyExc_SystemError, |
| 1483 | "com_atom: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1488 | com_slice(struct compiling *c, node *n, int op) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1489 | { |
| 1490 | if (NCH(n) == 1) { |
| 1491 | com_addbyte(c, op); |
| 1492 | } |
| 1493 | else if (NCH(n) == 2) { |
| 1494 | if (TYPE(CHILD(n, 0)) != COLON) { |
| 1495 | com_node(c, CHILD(n, 0)); |
| 1496 | com_addbyte(c, op+1); |
| 1497 | } |
| 1498 | else { |
| 1499 | com_node(c, CHILD(n, 1)); |
| 1500 | com_addbyte(c, op+2); |
| 1501 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1502 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1503 | } |
| 1504 | else { |
| 1505 | com_node(c, CHILD(n, 0)); |
| 1506 | com_node(c, CHILD(n, 2)); |
| 1507 | com_addbyte(c, op+3); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1508 | com_pop(c, 2); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1509 | } |
| 1510 | } |
| 1511 | |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1512 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1513 | com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn) |
| 1514 | { |
| 1515 | if (NCH(n) == 1) { |
| 1516 | com_addbyte(c, DUP_TOP); |
| 1517 | com_push(c, 1); |
| 1518 | com_addbyte(c, SLICE); |
| 1519 | com_node(c, augn); |
| 1520 | com_addbyte(c, opcode); |
| 1521 | com_pop(c, 1); |
| 1522 | com_addbyte(c, ROT_TWO); |
| 1523 | com_addbyte(c, STORE_SLICE); |
| 1524 | com_pop(c, 2); |
| 1525 | } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) { |
| 1526 | com_node(c, CHILD(n, 0)); |
| 1527 | com_addoparg(c, DUP_TOPX, 2); |
| 1528 | com_push(c, 2); |
| 1529 | com_addbyte(c, SLICE+1); |
| 1530 | com_pop(c, 1); |
| 1531 | com_node(c, augn); |
| 1532 | com_addbyte(c, opcode); |
| 1533 | com_pop(c, 1); |
| 1534 | com_addbyte(c, ROT_THREE); |
| 1535 | com_addbyte(c, STORE_SLICE+1); |
| 1536 | com_pop(c, 3); |
| 1537 | } else if (NCH(n) == 2) { |
| 1538 | com_node(c, CHILD(n, 1)); |
| 1539 | com_addoparg(c, DUP_TOPX, 2); |
| 1540 | com_push(c, 2); |
| 1541 | com_addbyte(c, SLICE+2); |
| 1542 | com_pop(c, 1); |
| 1543 | com_node(c, augn); |
| 1544 | com_addbyte(c, opcode); |
| 1545 | com_pop(c, 1); |
| 1546 | com_addbyte(c, ROT_THREE); |
| 1547 | com_addbyte(c, STORE_SLICE+2); |
| 1548 | com_pop(c, 3); |
| 1549 | } else { |
| 1550 | com_node(c, CHILD(n, 0)); |
| 1551 | com_node(c, CHILD(n, 2)); |
| 1552 | com_addoparg(c, DUP_TOPX, 3); |
| 1553 | com_push(c, 3); |
| 1554 | com_addbyte(c, SLICE+3); |
| 1555 | com_pop(c, 2); |
| 1556 | com_node(c, augn); |
| 1557 | com_addbyte(c, opcode); |
| 1558 | com_pop(c, 1); |
| 1559 | com_addbyte(c, ROT_FOUR); |
| 1560 | com_addbyte(c, STORE_SLICE+3); |
| 1561 | com_pop(c, 4); |
| 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1566 | com_argument(struct compiling *c, node *n, PyObject **pkeywords) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1567 | { |
| 1568 | node *m; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1569 | REQ(n, argument); /* [test '='] test; really [keyword '='] test */ |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1570 | if (NCH(n) == 1) { |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1571 | if (*pkeywords != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1572 | com_error(c, PyExc_SyntaxError, |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 1573 | "non-keyword arg after keyword arg"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1574 | } |
| 1575 | else { |
| 1576 | com_node(c, CHILD(n, 0)); |
| 1577 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1578 | return; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1579 | } |
| 1580 | m = n; |
| 1581 | do { |
| 1582 | m = CHILD(m, 0); |
| 1583 | } while (NCH(m) == 1); |
| 1584 | if (TYPE(m) != NAME) { |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 1585 | /* f(lambda x: x[0] = 3) ends up getting parsed with |
| 1586 | * LHS test = lambda x: x[0], and RHS test = 3. |
| 1587 | * SF bug 132313 points out that complaining about a keyword |
| 1588 | * then is very confusing. |
| 1589 | */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1590 | com_error(c, PyExc_SyntaxError, |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 1591 | TYPE(m) == lambdef ? |
| 1592 | "lambda cannot contain assignment" : |
| 1593 | "keyword can't be an expression"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1594 | } |
| 1595 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1596 | PyObject *v = PyString_InternFromString(STR(m)); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1597 | if (v != NULL && *pkeywords == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1598 | *pkeywords = PyDict_New(); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 1599 | if (v == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1600 | c->c_errors++; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 1601 | else if (*pkeywords == NULL) { |
| 1602 | c->c_errors++; |
| 1603 | Py_DECREF(v); |
| 1604 | } else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1605 | if (PyDict_GetItem(*pkeywords, v) != NULL) |
| 1606 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1607 | "duplicate keyword argument"); |
| 1608 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1609 | if (PyDict_SetItem(*pkeywords, v, v) != 0) |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1610 | c->c_errors++; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1611 | com_addoparg(c, LOAD_CONST, com_addconst(c, v)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1612 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1613 | Py_DECREF(v); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1614 | } |
| 1615 | } |
| 1616 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1619 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1620 | com_call_function(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1621 | { |
| 1622 | if (TYPE(n) == RPAR) { |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1623 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1624 | } |
| 1625 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1626 | PyObject *keywords = NULL; |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1627 | int i, na, nk; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1628 | int lineno = n->n_lineno; |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1629 | int star_flag = 0; |
| 1630 | int starstar_flag = 0; |
| 1631 | int opcode; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1632 | REQ(n, arglist); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1633 | na = 0; |
| 1634 | nk = 0; |
| 1635 | for (i = 0; i < NCH(n); i += 2) { |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1636 | node *ch = CHILD(n, i); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1637 | if (TYPE(ch) == STAR || |
| 1638 | TYPE(ch) == DOUBLESTAR) |
| 1639 | break; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1640 | if (ch->n_lineno != lineno) { |
| 1641 | lineno = ch->n_lineno; |
| 1642 | com_addoparg(c, SET_LINENO, lineno); |
| 1643 | } |
| 1644 | com_argument(c, ch, &keywords); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1645 | if (keywords == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1646 | na++; |
| 1647 | else |
| 1648 | nk++; |
| 1649 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1650 | Py_XDECREF(keywords); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1651 | while (i < NCH(n)) { |
| 1652 | node *tok = CHILD(n, i); |
| 1653 | node *ch = CHILD(n, i+1); |
| 1654 | i += 3; |
| 1655 | switch (TYPE(tok)) { |
| 1656 | case STAR: star_flag = 1; break; |
| 1657 | case DOUBLESTAR: starstar_flag = 1; break; |
| 1658 | } |
| 1659 | com_node(c, ch); |
| 1660 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1661 | if (na > 255 || nk > 255) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1662 | com_error(c, PyExc_SyntaxError, |
| 1663 | "more than 255 arguments"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1664 | } |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1665 | if (star_flag || starstar_flag) |
Jeremy Hylton | e4fb958 | 2000-03-29 00:10:44 +0000 | [diff] [blame] | 1666 | opcode = CALL_FUNCTION_VAR - 1 + |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1667 | star_flag + (starstar_flag << 1); |
| 1668 | else |
| 1669 | opcode = CALL_FUNCTION; |
| 1670 | com_addoparg(c, opcode, na | (nk << 8)); |
| 1671 | com_pop(c, na + 2*nk + star_flag + starstar_flag); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1672 | } |
| 1673 | } |
| 1674 | |
| 1675 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1676 | com_select_member(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1677 | { |
| 1678 | com_addopname(c, LOAD_ATTR, n); |
| 1679 | } |
| 1680 | |
| 1681 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1682 | com_sliceobj(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1683 | { |
| 1684 | int i=0; |
| 1685 | int ns=2; /* number of slice arguments */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1686 | node *ch; |
| 1687 | |
| 1688 | /* first argument */ |
| 1689 | if (TYPE(CHILD(n,i)) == COLON) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1690 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1691 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1692 | i++; |
| 1693 | } |
| 1694 | else { |
| 1695 | com_node(c, CHILD(n,i)); |
| 1696 | i++; |
| 1697 | REQ(CHILD(n,i),COLON); |
| 1698 | i++; |
| 1699 | } |
| 1700 | /* second argument */ |
| 1701 | if (i < NCH(n) && TYPE(CHILD(n,i)) == test) { |
| 1702 | com_node(c, CHILD(n,i)); |
| 1703 | i++; |
| 1704 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1705 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1706 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1707 | com_push(c, 1); |
| 1708 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1709 | /* remaining arguments */ |
| 1710 | for (; i < NCH(n); i++) { |
| 1711 | ns++; |
| 1712 | ch=CHILD(n,i); |
| 1713 | REQ(ch, sliceop); |
| 1714 | if (NCH(ch) == 1) { |
| 1715 | /* right argument of ':' missing */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1716 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1717 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1718 | } |
| 1719 | else |
| 1720 | com_node(c, CHILD(ch,1)); |
| 1721 | } |
| 1722 | com_addoparg(c, BUILD_SLICE, ns); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1723 | com_pop(c, 1 + (ns == 3)); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1727 | com_subscript(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1728 | { |
| 1729 | node *ch; |
| 1730 | REQ(n, subscript); |
| 1731 | ch = CHILD(n,0); |
| 1732 | /* check for rubber index */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1733 | if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) { |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 1734 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1735 | com_push(c, 1); |
| 1736 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1737 | else { |
| 1738 | /* check for slice */ |
| 1739 | if ((TYPE(ch) == COLON || NCH(n) > 1)) |
| 1740 | com_sliceobj(c, n); |
| 1741 | else { |
| 1742 | REQ(ch, test); |
| 1743 | com_node(c, ch); |
| 1744 | } |
| 1745 | } |
| 1746 | } |
| 1747 | |
| 1748 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1749 | com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1750 | { |
| 1751 | int i, op; |
| 1752 | REQ(n, subscriptlist); |
| 1753 | /* Check to make backward compatible slice behavior for '[i:j]' */ |
| 1754 | if (NCH(n) == 1) { |
| 1755 | node *sub = CHILD(n, 0); /* subscript */ |
Thomas Wouters | e8643c4 | 2000-08-05 21:37:50 +0000 | [diff] [blame] | 1756 | /* 'Basic' slice, should have exactly one colon. */ |
| 1757 | if ((TYPE(CHILD(sub, 0)) == COLON |
| 1758 | || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON)) |
| 1759 | && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop)) |
| 1760 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1761 | switch (assigning) { |
| 1762 | case OP_DELETE: |
| 1763 | op = DELETE_SLICE; |
| 1764 | break; |
| 1765 | case OP_ASSIGN: |
| 1766 | op = STORE_SLICE; |
| 1767 | break; |
| 1768 | case OP_APPLY: |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1769 | op = SLICE; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1770 | break; |
| 1771 | default: |
| 1772 | com_augassign_slice(c, sub, assigning, augn); |
| 1773 | return; |
| 1774 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1775 | com_slice(c, sub, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1776 | if (op == STORE_SLICE) |
| 1777 | com_pop(c, 2); |
| 1778 | else if (op == DELETE_SLICE) |
| 1779 | com_pop(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1780 | return; |
| 1781 | } |
| 1782 | } |
| 1783 | /* Else normal subscriptlist. Compile each subscript. */ |
| 1784 | for (i = 0; i < NCH(n); i += 2) |
| 1785 | com_subscript(c, CHILD(n, i)); |
| 1786 | /* Put multiple subscripts into a tuple */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1787 | if (NCH(n) > 1) { |
| 1788 | i = (NCH(n)+1) / 2; |
| 1789 | com_addoparg(c, BUILD_TUPLE, i); |
| 1790 | com_pop(c, i-1); |
| 1791 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1792 | switch (assigning) { |
| 1793 | case OP_DELETE: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1794 | op = DELETE_SUBSCR; |
| 1795 | i = 2; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1796 | break; |
| 1797 | default: |
| 1798 | case OP_ASSIGN: |
| 1799 | op = STORE_SUBSCR; |
| 1800 | i = 3; |
| 1801 | break; |
| 1802 | case OP_APPLY: |
| 1803 | op = BINARY_SUBSCR; |
| 1804 | i = 1; |
| 1805 | break; |
| 1806 | } |
| 1807 | if (assigning > OP_APPLY) { |
| 1808 | com_addoparg(c, DUP_TOPX, 2); |
| 1809 | com_push(c, 2); |
| 1810 | com_addbyte(c, BINARY_SUBSCR); |
| 1811 | com_pop(c, 1); |
| 1812 | com_node(c, augn); |
| 1813 | com_addbyte(c, assigning); |
| 1814 | com_pop(c, 1); |
| 1815 | com_addbyte(c, ROT_THREE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1816 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1817 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1818 | com_pop(c, i); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1822 | com_apply_trailer(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1823 | { |
| 1824 | REQ(n, trailer); |
| 1825 | switch (TYPE(CHILD(n, 0))) { |
| 1826 | case LPAR: |
| 1827 | com_call_function(c, CHILD(n, 1)); |
| 1828 | break; |
| 1829 | case DOT: |
| 1830 | com_select_member(c, CHILD(n, 1)); |
| 1831 | break; |
| 1832 | case LSQB: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1833 | com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1834 | break; |
| 1835 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1836 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1837 | "com_apply_trailer: unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1838 | } |
| 1839 | } |
| 1840 | |
| 1841 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1842 | com_power(struct compiling *c, node *n) |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1843 | { |
| 1844 | int i; |
| 1845 | REQ(n, power); |
| 1846 | com_atom(c, CHILD(n, 0)); |
| 1847 | for (i = 1; i < NCH(n); i++) { |
| 1848 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
| 1849 | com_factor(c, CHILD(n, i+1)); |
| 1850 | com_addbyte(c, BINARY_POWER); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1851 | com_pop(c, 1); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1852 | break; |
| 1853 | } |
| 1854 | else |
| 1855 | com_apply_trailer(c, CHILD(n, i)); |
| 1856 | } |
| 1857 | } |
| 1858 | |
| 1859 | static void |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1860 | com_invert_constant(struct compiling *c, node *n) |
| 1861 | { |
| 1862 | /* Compute the inverse of int and longs and use them directly, |
| 1863 | but be prepared to generate code for all other |
| 1864 | possibilities (invalid numbers, floats, complex). |
| 1865 | */ |
| 1866 | PyObject *num, *inv = NULL; |
| 1867 | int i; |
| 1868 | |
| 1869 | REQ(n, NUMBER); |
| 1870 | num = parsenumber(c, STR(n)); |
| 1871 | if (num == NULL) |
| 1872 | i = 255; |
| 1873 | else { |
| 1874 | inv = PyNumber_Invert(num); |
| 1875 | if (inv == NULL) { |
| 1876 | PyErr_Clear(); |
| 1877 | i = com_addconst(c, num); |
| 1878 | } else { |
| 1879 | i = com_addconst(c, inv); |
| 1880 | Py_DECREF(inv); |
| 1881 | } |
| 1882 | Py_DECREF(num); |
| 1883 | } |
| 1884 | com_addoparg(c, LOAD_CONST, i); |
| 1885 | com_push(c, 1); |
| 1886 | if (num != NULL && inv == NULL) |
| 1887 | com_addbyte(c, UNARY_INVERT); |
| 1888 | } |
| 1889 | |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 1890 | static int |
| 1891 | is_float_zero(const char *p) |
| 1892 | { |
| 1893 | int found_radix_point = 0; |
| 1894 | int ch; |
| 1895 | while ((ch = Py_CHARMASK(*p++)) != '\0') { |
| 1896 | switch (ch) { |
| 1897 | case '0': |
| 1898 | /* no reason to believe it's not 0 -- continue */ |
| 1899 | break; |
| 1900 | |
| 1901 | case 'e': case 'E': case 'j': case 'J': |
| 1902 | /* If this was a hex constant, we already would have |
| 1903 | returned 0 due to the 'x' or 'X', so 'e' or 'E' |
| 1904 | must be an exponent marker, and we haven't yet |
| 1905 | seen a non-zero digit, and it doesn't matter what |
| 1906 | the exponent is then. For 'j' or 'J' similarly, |
| 1907 | except that this is an imaginary 0 then. */ |
| 1908 | return 1; |
| 1909 | |
| 1910 | case '.': |
| 1911 | found_radix_point = 1; |
| 1912 | break; |
| 1913 | |
| 1914 | default: |
| 1915 | return 0; |
| 1916 | } |
| 1917 | } |
| 1918 | return found_radix_point; |
| 1919 | } |
| 1920 | |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1921 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1922 | com_factor(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1923 | { |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1924 | int childtype = TYPE(CHILD(n, 0)); |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 1925 | node *pfactor, *ppower, *patom, *pnum; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1926 | REQ(n, factor); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1927 | /* If the unary +, -, or ~ operator is applied to a constant, |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 1928 | don't generate a UNARY_xxx opcode. Just store the |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1929 | approriate value as a constant. If the value is negative, |
| 1930 | extend the string containing the constant and insert a |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 1931 | negative in the 0th position -- unless we're doing unary minus |
| 1932 | of a floating zero! In that case the sign is significant, but |
| 1933 | the const dict can't distinguish +0.0 from -0.0. |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1934 | */ |
| 1935 | if ((childtype == PLUS || childtype == MINUS || childtype == TILDE) |
Fred Drake | 14ef244 | 2001-08-30 18:53:25 +0000 | [diff] [blame] | 1936 | && NCH(n) == 2 |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 1937 | && TYPE((pfactor = CHILD(n, 1))) == factor |
| 1938 | && NCH(pfactor) == 1 |
| 1939 | && TYPE((ppower = CHILD(pfactor, 0))) == power |
| 1940 | && NCH(ppower) == 1 |
| 1941 | && TYPE((patom = CHILD(ppower, 0))) == atom |
| 1942 | && TYPE((pnum = CHILD(patom, 0))) == NUMBER |
| 1943 | && !(childtype == MINUS && is_float_zero(STR(pnum)))) { |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1944 | if (childtype == TILDE) { |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 1945 | com_invert_constant(c, pnum); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1946 | return; |
| 1947 | } |
| 1948 | if (childtype == MINUS) { |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 1949 | char *s = malloc(strlen(STR(pnum)) + 2); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1950 | if (s == NULL) { |
| 1951 | com_error(c, PyExc_MemoryError, ""); |
| 1952 | com_addbyte(c, 255); |
| 1953 | return; |
| 1954 | } |
| 1955 | s[0] = '-'; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 1956 | strcpy(s + 1, STR(pnum)); |
| 1957 | free(STR(pnum)); |
| 1958 | STR(pnum) = s; |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1959 | } |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 1960 | com_atom(c, patom); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1961 | } |
| 1962 | else if (childtype == PLUS) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1963 | com_factor(c, CHILD(n, 1)); |
| 1964 | com_addbyte(c, UNARY_POSITIVE); |
| 1965 | } |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1966 | else if (childtype == MINUS) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1967 | com_factor(c, CHILD(n, 1)); |
| 1968 | com_addbyte(c, UNARY_NEGATIVE); |
| 1969 | } |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1970 | else if (childtype == TILDE) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1971 | com_factor(c, CHILD(n, 1)); |
| 1972 | com_addbyte(c, UNARY_INVERT); |
| 1973 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1974 | else { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1975 | com_power(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1976 | } |
| 1977 | } |
| 1978 | |
| 1979 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1980 | com_term(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1981 | { |
| 1982 | int i; |
| 1983 | int op; |
| 1984 | REQ(n, term); |
| 1985 | com_factor(c, CHILD(n, 0)); |
| 1986 | for (i = 2; i < NCH(n); i += 2) { |
| 1987 | com_factor(c, CHILD(n, i)); |
| 1988 | switch (TYPE(CHILD(n, i-1))) { |
| 1989 | case STAR: |
| 1990 | op = BINARY_MULTIPLY; |
| 1991 | break; |
| 1992 | case SLASH: |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1993 | if (c->c_flags & CO_FUTURE_DIVISION) |
| 1994 | op = BINARY_TRUE_DIVIDE; |
| 1995 | else |
| 1996 | op = BINARY_DIVIDE; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1997 | break; |
| 1998 | case PERCENT: |
| 1999 | op = BINARY_MODULO; |
| 2000 | break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2001 | case DOUBLESLASH: |
| 2002 | op = BINARY_FLOOR_DIVIDE; |
| 2003 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2004 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2005 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2006 | "com_term: operator not *, /, // or %"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2007 | op = 255; |
| 2008 | } |
| 2009 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2010 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2015 | com_arith_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2016 | { |
| 2017 | int i; |
| 2018 | int op; |
| 2019 | REQ(n, arith_expr); |
| 2020 | com_term(c, CHILD(n, 0)); |
| 2021 | for (i = 2; i < NCH(n); i += 2) { |
| 2022 | com_term(c, CHILD(n, i)); |
| 2023 | switch (TYPE(CHILD(n, i-1))) { |
| 2024 | case PLUS: |
| 2025 | op = BINARY_ADD; |
| 2026 | break; |
| 2027 | case MINUS: |
| 2028 | op = BINARY_SUBTRACT; |
| 2029 | break; |
| 2030 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2031 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2032 | "com_arith_expr: operator not + or -"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2033 | op = 255; |
| 2034 | } |
| 2035 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2036 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2037 | } |
| 2038 | } |
| 2039 | |
| 2040 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2041 | com_shift_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2042 | { |
| 2043 | int i; |
| 2044 | int op; |
| 2045 | REQ(n, shift_expr); |
| 2046 | com_arith_expr(c, CHILD(n, 0)); |
| 2047 | for (i = 2; i < NCH(n); i += 2) { |
| 2048 | com_arith_expr(c, CHILD(n, i)); |
| 2049 | switch (TYPE(CHILD(n, i-1))) { |
| 2050 | case LEFTSHIFT: |
| 2051 | op = BINARY_LSHIFT; |
| 2052 | break; |
| 2053 | case RIGHTSHIFT: |
| 2054 | op = BINARY_RSHIFT; |
| 2055 | break; |
| 2056 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2057 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2058 | "com_shift_expr: operator not << or >>"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2059 | op = 255; |
| 2060 | } |
| 2061 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2062 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2063 | } |
| 2064 | } |
| 2065 | |
| 2066 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2067 | com_and_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2068 | { |
| 2069 | int i; |
| 2070 | int op; |
| 2071 | REQ(n, and_expr); |
| 2072 | com_shift_expr(c, CHILD(n, 0)); |
| 2073 | for (i = 2; i < NCH(n); i += 2) { |
| 2074 | com_shift_expr(c, CHILD(n, i)); |
| 2075 | if (TYPE(CHILD(n, i-1)) == AMPER) { |
| 2076 | op = BINARY_AND; |
| 2077 | } |
| 2078 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2079 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2080 | "com_and_expr: operator not &"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2081 | op = 255; |
| 2082 | } |
| 2083 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2084 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2085 | } |
| 2086 | } |
| 2087 | |
| 2088 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2089 | com_xor_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2090 | { |
| 2091 | int i; |
| 2092 | int op; |
| 2093 | REQ(n, xor_expr); |
| 2094 | com_and_expr(c, CHILD(n, 0)); |
| 2095 | for (i = 2; i < NCH(n); i += 2) { |
| 2096 | com_and_expr(c, CHILD(n, i)); |
| 2097 | if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) { |
| 2098 | op = BINARY_XOR; |
| 2099 | } |
| 2100 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2101 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2102 | "com_xor_expr: operator not ^"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2103 | op = 255; |
| 2104 | } |
| 2105 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2106 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2107 | } |
| 2108 | } |
| 2109 | |
| 2110 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2111 | com_expr(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2112 | { |
| 2113 | int i; |
| 2114 | int op; |
| 2115 | REQ(n, expr); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2116 | com_xor_expr(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2117 | for (i = 2; i < NCH(n); i += 2) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2118 | com_xor_expr(c, CHILD(n, i)); |
| 2119 | if (TYPE(CHILD(n, i-1)) == VBAR) { |
| 2120 | op = BINARY_OR; |
| 2121 | } |
| 2122 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2123 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2124 | "com_expr: expr operator not |"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2125 | op = 255; |
| 2126 | } |
| 2127 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2128 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2129 | } |
| 2130 | } |
| 2131 | |
| 2132 | static enum cmp_op |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2133 | cmp_type(node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2134 | { |
| 2135 | REQ(n, comp_op); |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2136 | /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '==' |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2137 | | 'in' | 'not' 'in' | 'is' | 'is' not' */ |
| 2138 | if (NCH(n) == 1) { |
| 2139 | n = CHILD(n, 0); |
| 2140 | switch (TYPE(n)) { |
| 2141 | case LESS: return LT; |
| 2142 | case GREATER: return GT; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2143 | case EQEQUAL: /* == */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2144 | case EQUAL: return EQ; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2145 | case LESSEQUAL: return LE; |
| 2146 | case GREATEREQUAL: return GE; |
| 2147 | case NOTEQUAL: return NE; /* <> or != */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2148 | case NAME: if (strcmp(STR(n), "in") == 0) return IN; |
| 2149 | if (strcmp(STR(n), "is") == 0) return IS; |
| 2150 | } |
| 2151 | } |
| 2152 | else if (NCH(n) == 2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2153 | switch (TYPE(CHILD(n, 0))) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2154 | case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 2155 | return NOT_IN; |
| 2156 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 2157 | return IS_NOT; |
| 2158 | } |
| 2159 | } |
| 2160 | return BAD; |
| 2161 | } |
| 2162 | |
| 2163 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2164 | com_comparison(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2165 | { |
| 2166 | int i; |
| 2167 | enum cmp_op op; |
| 2168 | int anchor; |
| 2169 | REQ(n, comparison); /* comparison: expr (comp_op expr)* */ |
| 2170 | com_expr(c, CHILD(n, 0)); |
| 2171 | if (NCH(n) == 1) |
| 2172 | return; |
| 2173 | |
| 2174 | /**************************************************************** |
| 2175 | The following code is generated for all but the last |
| 2176 | comparison in a chain: |
| 2177 | |
| 2178 | label: on stack: opcode: jump to: |
| 2179 | |
| 2180 | a <code to load b> |
| 2181 | a, b DUP_TOP |
| 2182 | a, b, b ROT_THREE |
| 2183 | b, a, b COMPARE_OP |
| 2184 | b, 0-or-1 JUMP_IF_FALSE L1 |
| 2185 | b, 1 POP_TOP |
| 2186 | b |
| 2187 | |
| 2188 | We are now ready to repeat this sequence for the next |
| 2189 | comparison in the chain. |
| 2190 | |
| 2191 | For the last we generate: |
| 2192 | |
| 2193 | b <code to load c> |
| 2194 | b, c COMPARE_OP |
| 2195 | 0-or-1 |
| 2196 | |
| 2197 | If there were any jumps to L1 (i.e., there was more than one |
| 2198 | comparison), we generate: |
| 2199 | |
| 2200 | 0-or-1 JUMP_FORWARD L2 |
| 2201 | L1: b, 0 ROT_TWO |
| 2202 | 0, b POP_TOP |
| 2203 | 0 |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2204 | L2: 0-or-1 |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2205 | ****************************************************************/ |
| 2206 | |
| 2207 | anchor = 0; |
| 2208 | |
| 2209 | for (i = 2; i < NCH(n); i += 2) { |
| 2210 | com_expr(c, CHILD(n, i)); |
| 2211 | if (i+2 < NCH(n)) { |
| 2212 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2213 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2214 | com_addbyte(c, ROT_THREE); |
| 2215 | } |
| 2216 | op = cmp_type(CHILD(n, i-1)); |
| 2217 | if (op == BAD) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2218 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2219 | "com_comparison: unknown comparison op"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2220 | } |
| 2221 | com_addoparg(c, COMPARE_OP, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2222 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2223 | if (i+2 < NCH(n)) { |
| 2224 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2225 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2226 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2227 | } |
| 2228 | } |
| 2229 | |
| 2230 | if (anchor) { |
| 2231 | int anchor2 = 0; |
| 2232 | com_addfwref(c, JUMP_FORWARD, &anchor2); |
| 2233 | com_backpatch(c, anchor); |
| 2234 | com_addbyte(c, ROT_TWO); |
| 2235 | com_addbyte(c, POP_TOP); |
| 2236 | com_backpatch(c, anchor2); |
| 2237 | } |
| 2238 | } |
| 2239 | |
| 2240 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2241 | com_not_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2242 | { |
| 2243 | REQ(n, not_test); /* 'not' not_test | comparison */ |
| 2244 | if (NCH(n) == 1) { |
| 2245 | com_comparison(c, CHILD(n, 0)); |
| 2246 | } |
| 2247 | else { |
| 2248 | com_not_test(c, CHILD(n, 1)); |
| 2249 | com_addbyte(c, UNARY_NOT); |
| 2250 | } |
| 2251 | } |
| 2252 | |
| 2253 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2254 | com_and_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2255 | { |
| 2256 | int i; |
| 2257 | int anchor; |
| 2258 | REQ(n, and_test); /* not_test ('and' not_test)* */ |
| 2259 | anchor = 0; |
| 2260 | i = 0; |
| 2261 | for (;;) { |
| 2262 | com_not_test(c, CHILD(n, i)); |
| 2263 | if ((i += 2) >= NCH(n)) |
| 2264 | break; |
| 2265 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2266 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2267 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2268 | } |
| 2269 | if (anchor) |
| 2270 | com_backpatch(c, anchor); |
| 2271 | } |
| 2272 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2273 | static int |
| 2274 | com_make_closure(struct compiling *c, PyCodeObject *co) |
| 2275 | { |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame^] | 2276 | int i, free = PyCode_GetNumFree(co); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2277 | if (free == 0) |
| 2278 | return 0; |
| 2279 | for (i = 0; i < free; ++i) { |
| 2280 | /* Bypass com_addop_varname because it will generate |
| 2281 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 2282 | */ |
| 2283 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 2284 | int arg, reftype; |
| 2285 | |
| 2286 | /* Special case: If a class contains a method with a |
| 2287 | free variable that has the same name as a method, |
| 2288 | the name will be considered free *and* local in the |
| 2289 | class. It should be handled by the closure, as |
| 2290 | well as by the normal name loookup logic. |
| 2291 | */ |
| 2292 | reftype = get_ref_type(c, PyString_AS_STRING(name)); |
| 2293 | if (reftype == CELL) |
| 2294 | arg = com_lookup_arg(c->c_cellvars, name); |
| 2295 | else /* (reftype == FREE) */ |
| 2296 | arg = com_lookup_arg(c->c_freevars, name); |
| 2297 | if (arg == -1) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 2298 | fprintf(stderr, "lookup %s in %s %d %d\n" |
| 2299 | "freevars of %s: %s\n", |
| 2300 | PyObject_REPR(name), |
| 2301 | c->c_name, |
| 2302 | reftype, arg, |
| 2303 | PyString_AS_STRING(co->co_name), |
| 2304 | PyObject_REPR(co->co_freevars)); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2305 | Py_FatalError("com_make_closure()"); |
| 2306 | } |
| 2307 | com_addoparg(c, LOAD_CLOSURE, arg); |
| 2308 | |
| 2309 | } |
| 2310 | com_push(c, free); |
| 2311 | return 1; |
| 2312 | } |
| 2313 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2314 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2315 | com_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2316 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2317 | REQ(n, test); /* and_test ('or' and_test)* | lambdef */ |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2318 | if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2319 | PyCodeObject *co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2320 | int i, closure; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2321 | int ndefs = com_argdefs(c, CHILD(n, 0)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 2322 | symtable_enter_scope(c->c_symtable, "lambda", lambdef, |
| 2323 | n->n_lineno); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2324 | co = icompile(CHILD(n, 0), c); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2325 | if (co == NULL) { |
| 2326 | c->c_errors++; |
| 2327 | return; |
| 2328 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2329 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2330 | i = com_addconst(c, (PyObject *)co); |
| 2331 | closure = com_make_closure(c, co); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2332 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2333 | com_push(c, 1); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2334 | if (closure) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2335 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame^] | 2336 | com_pop(c, PyCode_GetNumFree(co)); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2337 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2338 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2339 | Py_DECREF(co); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2340 | com_pop(c, ndefs); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2341 | } |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2342 | else { |
| 2343 | int anchor = 0; |
| 2344 | int i = 0; |
| 2345 | for (;;) { |
| 2346 | com_and_test(c, CHILD(n, i)); |
| 2347 | if ((i += 2) >= NCH(n)) |
| 2348 | break; |
| 2349 | com_addfwref(c, JUMP_IF_TRUE, &anchor); |
| 2350 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2351 | com_pop(c, 1); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2352 | } |
| 2353 | if (anchor) |
| 2354 | com_backpatch(c, anchor); |
| 2355 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2359 | com_list(struct compiling *c, node *n, int toplevel) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2360 | { |
| 2361 | /* exprlist: expr (',' expr)* [',']; likewise for testlist */ |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 2362 | if (NCH(n) == 1 && !toplevel) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2363 | com_node(c, CHILD(n, 0)); |
| 2364 | } |
| 2365 | else { |
| 2366 | int i; |
| 2367 | int len; |
| 2368 | len = (NCH(n) + 1) / 2; |
| 2369 | for (i = 0; i < NCH(n); i += 2) |
| 2370 | com_node(c, CHILD(n, i)); |
| 2371 | com_addoparg(c, BUILD_TUPLE, len); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2372 | com_pop(c, len-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2373 | } |
| 2374 | } |
| 2375 | |
| 2376 | |
| 2377 | /* Begin of assignment compilation */ |
| 2378 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2379 | |
| 2380 | static void |
| 2381 | com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn) |
| 2382 | { |
| 2383 | com_addbyte(c, DUP_TOP); |
| 2384 | com_push(c, 1); |
| 2385 | com_addopname(c, LOAD_ATTR, n); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2386 | com_node(c, augn); |
| 2387 | com_addbyte(c, opcode); |
| 2388 | com_pop(c, 1); |
| 2389 | com_addbyte(c, ROT_TWO); |
| 2390 | com_addopname(c, STORE_ATTR, n); |
| 2391 | com_pop(c, 2); |
| 2392 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2393 | |
| 2394 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2395 | com_assign_attr(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2396 | { |
| 2397 | com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2398 | com_pop(c, assigning ? 2 : 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2399 | } |
| 2400 | |
| 2401 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2402 | 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] | 2403 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2404 | REQ(n, trailer); |
| 2405 | switch (TYPE(CHILD(n, 0))) { |
| 2406 | case LPAR: /* '(' [exprlist] ')' */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2407 | com_error(c, PyExc_SyntaxError, |
| 2408 | "can't assign to function call"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2409 | break; |
| 2410 | case DOT: /* '.' NAME */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2411 | if (assigning > OP_APPLY) |
| 2412 | com_augassign_attr(c, CHILD(n, 1), assigning, augn); |
| 2413 | else |
| 2414 | com_assign_attr(c, CHILD(n, 1), assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2415 | break; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2416 | case LSQB: /* '[' subscriptlist ']' */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2417 | com_subscriptlist(c, CHILD(n, 1), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2418 | break; |
| 2419 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2420 | com_error(c, PyExc_SystemError, "unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2421 | } |
| 2422 | } |
| 2423 | |
| 2424 | static void |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2425 | com_assign_sequence(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2426 | { |
| 2427 | int i; |
Thomas Wouters | b59290f | 2000-08-25 05:41:11 +0000 | [diff] [blame] | 2428 | if (TYPE(n) != testlist && TYPE(n) != listmaker) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2429 | REQ(n, exprlist); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2430 | if (assigning) { |
| 2431 | i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2432 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2433 | com_push(c, i-1); |
| 2434 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2435 | for (i = 0; i < NCH(n); i += 2) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2436 | com_assign(c, CHILD(n, i), assigning, NULL); |
| 2437 | } |
| 2438 | |
| 2439 | static void |
| 2440 | com_augassign_name(struct compiling *c, node *n, int opcode, node *augn) |
| 2441 | { |
| 2442 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2443 | com_addop_varname(c, VAR_LOAD, STR(n)); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2444 | com_push(c, 1); |
| 2445 | com_node(c, augn); |
| 2446 | com_addbyte(c, opcode); |
| 2447 | com_pop(c, 1); |
| 2448 | com_assign_name(c, n, OP_ASSIGN); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
| 2451 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2452 | com_assign_name(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2453 | { |
| 2454 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2455 | com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2456 | if (assigning) |
| 2457 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
| 2460 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2461 | com_assign(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2462 | { |
| 2463 | /* Loop to avoid trivial recursion */ |
| 2464 | for (;;) { |
| 2465 | switch (TYPE(n)) { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2466 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2467 | case exprlist: |
| 2468 | case testlist: |
| 2469 | if (NCH(n) > 1) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2470 | if (assigning > OP_APPLY) { |
| 2471 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 2472 | "augmented assign to tuple not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2473 | return; |
| 2474 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2475 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2476 | return; |
| 2477 | } |
| 2478 | n = CHILD(n, 0); |
| 2479 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2480 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2481 | case test: |
| 2482 | case and_test: |
| 2483 | case not_test: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2484 | case comparison: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2485 | case expr: |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2486 | case xor_expr: |
| 2487 | case and_expr: |
| 2488 | case shift_expr: |
| 2489 | case arith_expr: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2490 | case term: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2491 | case factor: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2492 | if (NCH(n) > 1) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2493 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2494 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2495 | return; |
| 2496 | } |
| 2497 | n = CHILD(n, 0); |
| 2498 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2499 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2500 | case power: /* atom trailer* ('**' power)* |
| 2501 | ('+'|'-'|'~') factor | atom trailer* */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2502 | if (TYPE(CHILD(n, 0)) != atom) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2503 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2504 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2505 | return; |
| 2506 | } |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2507 | if (NCH(n) > 1) { /* trailer or exponent present */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2508 | int i; |
| 2509 | com_node(c, CHILD(n, 0)); |
| 2510 | for (i = 1; i+1 < NCH(n); i++) { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2511 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2512 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2513 | "can't assign to operator"); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2514 | return; |
| 2515 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2516 | com_apply_trailer(c, CHILD(n, i)); |
| 2517 | } /* NB i is still alive */ |
| 2518 | com_assign_trailer(c, |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2519 | CHILD(n, i), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2520 | return; |
| 2521 | } |
| 2522 | n = CHILD(n, 0); |
| 2523 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2524 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2525 | case atom: |
| 2526 | switch (TYPE(CHILD(n, 0))) { |
| 2527 | case LPAR: |
| 2528 | n = CHILD(n, 1); |
| 2529 | if (TYPE(n) == RPAR) { |
| 2530 | /* XXX Should allow () = () ??? */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2531 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2532 | "can't assign to ()"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2533 | return; |
| 2534 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2535 | if (assigning > OP_APPLY) { |
| 2536 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2537 | "augmented assign to tuple not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2538 | return; |
| 2539 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2540 | break; |
| 2541 | case LSQB: |
| 2542 | n = CHILD(n, 1); |
| 2543 | if (TYPE(n) == RSQB) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2544 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2545 | "can't assign to []"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2546 | return; |
| 2547 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2548 | if (assigning > OP_APPLY) { |
| 2549 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2550 | "augmented assign to list not possible"); |
| 2551 | return; |
| 2552 | } |
| 2553 | if (NCH(n) > 1 |
| 2554 | && TYPE(CHILD(n, 1)) == list_for) { |
| 2555 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2556 | "can't assign to list comprehension"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2557 | return; |
| 2558 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2559 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2560 | return; |
| 2561 | case NAME: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2562 | if (assigning > OP_APPLY) |
| 2563 | com_augassign_name(c, CHILD(n, 0), |
| 2564 | assigning, augn); |
| 2565 | else |
| 2566 | com_assign_name(c, CHILD(n, 0), |
| 2567 | assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2568 | return; |
| 2569 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2570 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2571 | "can't assign to literal"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2572 | return; |
| 2573 | } |
| 2574 | break; |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2575 | |
| 2576 | case lambdef: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2577 | com_error(c, PyExc_SyntaxError, |
| 2578 | "can't assign to lambda"); |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2579 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2580 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2581 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2582 | com_error(c, PyExc_SystemError, |
| 2583 | "com_assign: bad node"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2584 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2585 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2586 | } |
| 2587 | } |
| 2588 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2589 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2590 | static void |
| 2591 | com_augassign(struct compiling *c, node *n) |
| 2592 | { |
| 2593 | int opcode; |
| 2594 | |
| 2595 | switch (STR(CHILD(CHILD(n, 1), 0))[0]) { |
| 2596 | case '+': opcode = INPLACE_ADD; break; |
| 2597 | case '-': opcode = INPLACE_SUBTRACT; break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2598 | case '/': |
| 2599 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '/') |
| 2600 | opcode = INPLACE_FLOOR_DIVIDE; |
| 2601 | else if (c->c_flags & CO_FUTURE_DIVISION) |
| 2602 | opcode = INPLACE_TRUE_DIVIDE; |
| 2603 | else |
| 2604 | opcode = INPLACE_DIVIDE; |
| 2605 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2606 | case '%': opcode = INPLACE_MODULO; break; |
| 2607 | case '<': opcode = INPLACE_LSHIFT; break; |
| 2608 | case '>': opcode = INPLACE_RSHIFT; break; |
| 2609 | case '&': opcode = INPLACE_AND; break; |
| 2610 | case '^': opcode = INPLACE_XOR; break; |
| 2611 | case '|': opcode = INPLACE_OR; break; |
| 2612 | case '*': |
| 2613 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '*') |
| 2614 | opcode = INPLACE_POWER; |
| 2615 | else |
| 2616 | opcode = INPLACE_MULTIPLY; |
| 2617 | break; |
| 2618 | default: |
| 2619 | com_error(c, PyExc_SystemError, "com_augassign: bad operator"); |
| 2620 | return; |
| 2621 | } |
| 2622 | com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2)); |
| 2623 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2624 | |
| 2625 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2626 | com_expr_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2627 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2628 | REQ(n, expr_stmt); |
| 2629 | /* testlist (('=' testlist)* | augassign testlist) */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2630 | /* Forget it if we have just a doc string here */ |
Guido van Rossum | 5f5e817 | 1997-04-06 03:41:40 +0000 | [diff] [blame] | 2631 | if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2632 | return; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2633 | if (NCH(n) == 1) { |
| 2634 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2635 | if (c->c_interactive) |
| 2636 | com_addbyte(c, PRINT_EXPR); |
| 2637 | else |
| 2638 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2639 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2640 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2641 | else if (TYPE(CHILD(n,1)) == augassign) |
| 2642 | com_augassign(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2643 | else { |
| 2644 | int i; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2645 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2646 | for (i = 0; i < NCH(n)-2; i+=2) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2647 | if (i+2 < NCH(n)-2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2648 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2649 | com_push(c, 1); |
| 2650 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2651 | com_assign(c, CHILD(n, i), OP_ASSIGN, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2652 | } |
| 2653 | } |
| 2654 | } |
| 2655 | |
| 2656 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2657 | com_assert_stmt(struct compiling *c, node *n) |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2658 | { |
| 2659 | int a = 0, b = 0; |
| 2660 | int i; |
| 2661 | REQ(n, assert_stmt); /* 'assert' test [',' test] */ |
| 2662 | /* Generate code like for |
| 2663 | |
| 2664 | if __debug__: |
| 2665 | if not <test>: |
| 2666 | raise AssertionError [, <message>] |
| 2667 | |
| 2668 | where <message> is the second test, if present. |
| 2669 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2670 | |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2671 | if (Py_OptimizeFlag) |
| 2672 | return; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2673 | com_addop_name(c, LOAD_GLOBAL, "__debug__"); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2674 | com_push(c, 1); |
| 2675 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 2676 | com_addbyte(c, POP_TOP); |
| 2677 | com_pop(c, 1); |
| 2678 | com_node(c, CHILD(n, 1)); |
| 2679 | com_addfwref(c, JUMP_IF_TRUE, &b); |
| 2680 | com_addbyte(c, POP_TOP); |
| 2681 | com_pop(c, 1); |
| 2682 | /* Raise that exception! */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2683 | com_addop_name(c, LOAD_GLOBAL, "AssertionError"); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2684 | com_push(c, 1); |
| 2685 | i = NCH(n)/2; /* Either 2 or 4 */ |
| 2686 | if (i > 1) |
| 2687 | com_node(c, CHILD(n, 3)); |
| 2688 | com_addoparg(c, RAISE_VARARGS, i); |
| 2689 | com_pop(c, i); |
| 2690 | /* The interpreter does not fall through */ |
| 2691 | /* All jumps converge here */ |
| 2692 | com_backpatch(c, a); |
| 2693 | com_backpatch(c, b); |
| 2694 | com_addbyte(c, POP_TOP); |
| 2695 | } |
| 2696 | |
| 2697 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2698 | com_print_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2699 | { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2700 | int i = 1; |
| 2701 | node* stream = NULL; |
| 2702 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2703 | REQ(n, print_stmt); /* 'print' (test ',')* [test] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2704 | |
| 2705 | /* are we using the extended print form? */ |
| 2706 | if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) { |
| 2707 | stream = CHILD(n, 2); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2708 | com_node(c, stream); |
| 2709 | /* stack: [...] => [... stream] */ |
| 2710 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2711 | if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA) |
| 2712 | i = 4; |
| 2713 | else |
| 2714 | i = 3; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2715 | } |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2716 | for (; i < NCH(n); i += 2) { |
| 2717 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2718 | com_addbyte(c, DUP_TOP); |
| 2719 | /* stack: [stream] => [stream stream] */ |
| 2720 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2721 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2722 | /* stack: [stream stream] => [stream stream obj] */ |
| 2723 | com_addbyte(c, ROT_TWO); |
| 2724 | /* stack: [stream stream obj] => [stream obj stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2725 | com_addbyte(c, PRINT_ITEM_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2726 | /* stack: [stream obj stream] => [stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2727 | com_pop(c, 2); |
| 2728 | } |
| 2729 | else { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2730 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2731 | /* stack: [...] => [... obj] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2732 | com_addbyte(c, PRINT_ITEM); |
| 2733 | com_pop(c, 1); |
| 2734 | } |
| 2735 | } |
| 2736 | /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */ |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2737 | if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2738 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2739 | /* must pop the extra stream object off the stack */ |
| 2740 | com_addbyte(c, POP_TOP); |
| 2741 | /* stack: [... stream] => [...] */ |
| 2742 | com_pop(c, 1); |
| 2743 | } |
| 2744 | } |
| 2745 | else { |
| 2746 | if (stream != NULL) { |
| 2747 | /* this consumes the last stream object on stack */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2748 | com_addbyte(c, PRINT_NEWLINE_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2749 | /* stack: [... stream] => [...] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2750 | com_pop(c, 1); |
| 2751 | } |
| 2752 | else |
| 2753 | com_addbyte(c, PRINT_NEWLINE); |
| 2754 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2755 | } |
| 2756 | |
| 2757 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2758 | com_return_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2759 | { |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2760 | REQ(n, return_stmt); /* 'return' [testlist] */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2761 | if (!c->c_infunction) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2762 | com_error(c, PyExc_SyntaxError, "'return' outside function"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2763 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2764 | if (c->c_flags & CO_GENERATOR) { |
| 2765 | if (NCH(n) > 1) { |
| 2766 | com_error(c, PyExc_SyntaxError, |
| 2767 | "'return' with argument inside generator"); |
| 2768 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 2769 | } |
| 2770 | if (NCH(n) < 2) { |
| 2771 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2772 | com_push(c, 1); |
| 2773 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 2774 | else |
| 2775 | com_node(c, CHILD(n, 1)); |
| 2776 | com_addbyte(c, RETURN_VALUE); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2777 | com_pop(c, 1); |
| 2778 | } |
| 2779 | |
| 2780 | static void |
| 2781 | com_yield_stmt(struct compiling *c, node *n) |
| 2782 | { |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 2783 | int i; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2784 | REQ(n, yield_stmt); /* 'yield' testlist */ |
| 2785 | if (!c->c_infunction) { |
| 2786 | com_error(c, PyExc_SyntaxError, "'yield' outside function"); |
| 2787 | } |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 2788 | |
| 2789 | for (i = 0; i < c->c_nblocks; ++i) { |
| 2790 | if (c->c_block[i] == SETUP_FINALLY) { |
| 2791 | com_error(c, PyExc_SyntaxError, |
| 2792 | "'yield' not allowed in a 'try' block " |
| 2793 | "with a 'finally' clause"); |
| 2794 | return; |
| 2795 | } |
| 2796 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2797 | com_node(c, CHILD(n, 1)); |
| 2798 | com_addbyte(c, YIELD_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2799 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2800 | } |
| 2801 | |
| 2802 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2803 | com_raise_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2804 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2805 | int i; |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 2806 | REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */ |
| 2807 | if (NCH(n) > 1) { |
| 2808 | com_node(c, CHILD(n, 1)); |
| 2809 | if (NCH(n) > 3) { |
| 2810 | com_node(c, CHILD(n, 3)); |
| 2811 | if (NCH(n) > 5) |
| 2812 | com_node(c, CHILD(n, 5)); |
| 2813 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2814 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2815 | i = NCH(n)/2; |
| 2816 | com_addoparg(c, RAISE_VARARGS, i); |
| 2817 | com_pop(c, i); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2818 | } |
| 2819 | |
| 2820 | static void |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2821 | com_from_import(struct compiling *c, node *n) |
| 2822 | { |
| 2823 | com_addopname(c, IMPORT_FROM, CHILD(n, 0)); |
| 2824 | com_push(c, 1); |
| 2825 | if (NCH(n) > 1) { |
| 2826 | if (strcmp(STR(CHILD(n, 1)), "as") != 0) { |
| 2827 | com_error(c, PyExc_SyntaxError, "invalid syntax"); |
| 2828 | return; |
| 2829 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2830 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2831 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2832 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2833 | com_pop(c, 1); |
| 2834 | } |
| 2835 | |
| 2836 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2837 | com_import_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2838 | { |
| 2839 | int i; |
| 2840 | REQ(n, import_stmt); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2841 | /* 'import' dotted_name (',' dotted_name)* | |
| 2842 | 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2843 | if (STR(CHILD(n, 0))[0] == 'f') { |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 2844 | PyObject *tup; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2845 | /* 'from' dotted_name 'import' ... */ |
| 2846 | REQ(CHILD(n, 1), dotted_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2847 | |
| 2848 | if (TYPE(CHILD(n, 3)) == STAR) { |
| 2849 | tup = Py_BuildValue("(s)", "*"); |
| 2850 | } else { |
| 2851 | tup = PyTuple_New((NCH(n) - 2)/2); |
| 2852 | for (i = 3; i < NCH(n); i += 2) { |
| 2853 | PyTuple_SET_ITEM(tup, (i-3)/2, |
| 2854 | PyString_FromString(STR( |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 2855 | CHILD(CHILD(n, i), 0)))); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2856 | } |
| 2857 | } |
| 2858 | com_addoparg(c, LOAD_CONST, com_addconst(c, tup)); |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 2859 | Py_DECREF(tup); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2860 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2861 | com_addopname(c, IMPORT_NAME, CHILD(n, 1)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2862 | if (TYPE(CHILD(n, 3)) == STAR) |
| 2863 | com_addbyte(c, IMPORT_STAR); |
| 2864 | else { |
| 2865 | for (i = 3; i < NCH(n); i += 2) |
| 2866 | com_from_import(c, CHILD(n, i)); |
| 2867 | com_addbyte(c, POP_TOP); |
| 2868 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2869 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2870 | } |
| 2871 | else { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2872 | /* 'import' ... */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2873 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2874 | node *subn = CHILD(n, i); |
| 2875 | REQ(subn, dotted_as_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2876 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2877 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2878 | com_addopname(c, IMPORT_NAME, CHILD(subn, 0)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2879 | if (NCH(subn) > 1) { |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 2880 | int j; |
| 2881 | if (strcmp(STR(CHILD(subn, 1)), "as") != 0) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2882 | com_error(c, PyExc_SyntaxError, |
| 2883 | "invalid syntax"); |
| 2884 | return; |
| 2885 | } |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 2886 | for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2) |
| 2887 | com_addopname(c, LOAD_ATTR, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2888 | CHILD(CHILD(subn, 0), |
| 2889 | j)); |
| 2890 | com_addop_varname(c, VAR_STORE, |
| 2891 | STR(CHILD(subn, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2892 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2893 | com_addop_varname(c, VAR_STORE, |
| 2894 | STR(CHILD(CHILD(subn, 0), |
| 2895 | 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2896 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2897 | } |
| 2898 | } |
| 2899 | } |
| 2900 | |
| 2901 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2902 | com_exec_stmt(struct compiling *c, node *n) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2903 | { |
| 2904 | REQ(n, exec_stmt); |
| 2905 | /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */ |
| 2906 | com_node(c, CHILD(n, 1)); |
| 2907 | if (NCH(n) >= 4) |
| 2908 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2909 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2910 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2911 | com_push(c, 1); |
| 2912 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2913 | if (NCH(n) >= 6) |
| 2914 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2915 | else { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2916 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2917 | com_push(c, 1); |
| 2918 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2919 | com_addbyte(c, EXEC_STMT); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2920 | com_pop(c, 3); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2923 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2924 | is_constant_false(struct compiling *c, node *n) |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2925 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2926 | PyObject *v; |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2927 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2928 | /* argument c will be NULL when called from symtable_node() */ |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2929 | |
| 2930 | /* Label to avoid tail recursion */ |
| 2931 | next: |
| 2932 | switch (TYPE(n)) { |
| 2933 | |
| 2934 | case suite: |
| 2935 | if (NCH(n) == 1) { |
| 2936 | n = CHILD(n, 0); |
| 2937 | goto next; |
| 2938 | } |
| 2939 | /* Fall through */ |
| 2940 | case file_input: |
| 2941 | for (i = 0; i < NCH(n); i++) { |
| 2942 | node *ch = CHILD(n, i); |
| 2943 | if (TYPE(ch) == stmt) { |
| 2944 | n = ch; |
| 2945 | goto next; |
| 2946 | } |
| 2947 | } |
| 2948 | break; |
| 2949 | |
| 2950 | case stmt: |
| 2951 | case simple_stmt: |
| 2952 | case small_stmt: |
| 2953 | n = CHILD(n, 0); |
| 2954 | goto next; |
| 2955 | |
| 2956 | case expr_stmt: |
| 2957 | case testlist: |
| 2958 | case test: |
| 2959 | case and_test: |
| 2960 | case not_test: |
| 2961 | case comparison: |
| 2962 | case expr: |
| 2963 | case xor_expr: |
| 2964 | case and_expr: |
| 2965 | case shift_expr: |
| 2966 | case arith_expr: |
| 2967 | case term: |
| 2968 | case factor: |
| 2969 | case power: |
| 2970 | case atom: |
| 2971 | if (NCH(n) == 1) { |
| 2972 | n = CHILD(n, 0); |
| 2973 | goto next; |
| 2974 | } |
| 2975 | break; |
| 2976 | |
| 2977 | case NAME: |
| 2978 | if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0) |
| 2979 | return 1; |
| 2980 | break; |
| 2981 | |
| 2982 | case NUMBER: |
| 2983 | v = parsenumber(c, STR(n)); |
| 2984 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2985 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2986 | break; |
| 2987 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2988 | i = PyObject_IsTrue(v); |
| 2989 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2990 | return i == 0; |
| 2991 | |
| 2992 | case STRING: |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2993 | v = parsestr(c, STR(n)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2994 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2995 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2996 | break; |
| 2997 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2998 | i = PyObject_IsTrue(v); |
| 2999 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3000 | return i == 0; |
| 3001 | |
| 3002 | } |
| 3003 | return 0; |
| 3004 | } |
| 3005 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3006 | |
| 3007 | /* Look under n for a return stmt with an expression. |
| 3008 | * This hack is used to find illegal returns under "if 0:" blocks in |
| 3009 | * functions already known to be generators (as determined by the symtable |
| 3010 | * pass). |
| 3011 | * Return the offending return node if found, else NULL. |
| 3012 | */ |
| 3013 | static node * |
| 3014 | look_for_offending_return(node *n) |
| 3015 | { |
| 3016 | int i; |
| 3017 | |
| 3018 | for (i = 0; i < NCH(n); ++i) { |
| 3019 | node *kid = CHILD(n, i); |
| 3020 | |
| 3021 | switch (TYPE(kid)) { |
| 3022 | case classdef: |
| 3023 | case funcdef: |
| 3024 | case lambdef: |
| 3025 | /* Stuff in nested functions & classes doesn't |
| 3026 | affect the code block we started in. */ |
| 3027 | return NULL; |
| 3028 | |
| 3029 | case return_stmt: |
| 3030 | if (NCH(kid) > 1) |
| 3031 | return kid; |
| 3032 | break; |
| 3033 | |
| 3034 | default: { |
| 3035 | node *bad = look_for_offending_return(kid); |
| 3036 | if (bad != NULL) |
| 3037 | return bad; |
| 3038 | } |
| 3039 | } |
| 3040 | } |
| 3041 | |
| 3042 | return NULL; |
| 3043 | } |
| 3044 | |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3045 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3046 | com_if_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3047 | { |
| 3048 | int i; |
| 3049 | int anchor = 0; |
| 3050 | REQ(n, if_stmt); |
| 3051 | /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */ |
| 3052 | for (i = 0; i+3 < NCH(n); i+=4) { |
| 3053 | int a = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3054 | node *ch = CHILD(n, i+1); |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3055 | if (is_constant_false(c, ch)) { |
| 3056 | /* We're going to skip this block. However, if this |
| 3057 | is a generator, we have to check the dead code |
| 3058 | anyway to make sure there aren't any return stmts |
| 3059 | with expressions, in the same scope. */ |
| 3060 | if (c->c_flags & CO_GENERATOR) { |
| 3061 | node *p = look_for_offending_return(n); |
| 3062 | if (p != NULL) { |
| 3063 | int savelineno = c->c_lineno; |
| 3064 | c->c_lineno = p->n_lineno; |
| 3065 | com_error(c, PyExc_SyntaxError, |
| 3066 | "'return' with argument " |
| 3067 | "inside generator"); |
| 3068 | c->c_lineno = savelineno; |
| 3069 | } |
| 3070 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3071 | continue; |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3072 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3073 | if (i > 0) |
| 3074 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3075 | com_node(c, ch); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3076 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 3077 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3078 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3079 | com_node(c, CHILD(n, i+3)); |
| 3080 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 3081 | com_backpatch(c, a); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3082 | /* We jump here with an extra entry which we now pop */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3083 | com_addbyte(c, POP_TOP); |
| 3084 | } |
| 3085 | if (i+2 < NCH(n)) |
| 3086 | com_node(c, CHILD(n, i+2)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3087 | if (anchor) |
| 3088 | com_backpatch(c, anchor); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3089 | } |
| 3090 | |
| 3091 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3092 | com_while_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3093 | { |
| 3094 | int break_anchor = 0; |
| 3095 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3096 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3097 | REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */ |
| 3098 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3099 | block_push(c, SETUP_LOOP); |
| 3100 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3101 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3102 | com_node(c, CHILD(n, 1)); |
| 3103 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 3104 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3105 | com_pop(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3106 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3107 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3108 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3109 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3110 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3111 | com_backpatch(c, anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3112 | /* We jump here with one entry more on the stack */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3113 | com_addbyte(c, POP_TOP); |
| 3114 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3115 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3116 | if (NCH(n) > 4) |
| 3117 | com_node(c, CHILD(n, 6)); |
| 3118 | com_backpatch(c, break_anchor); |
| 3119 | } |
| 3120 | |
| 3121 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3122 | com_for_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3123 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3124 | int break_anchor = 0; |
| 3125 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3126 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3127 | REQ(n, for_stmt); |
| 3128 | /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */ |
| 3129 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3130 | block_push(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3131 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3132 | com_addbyte(c, GET_ITER); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3133 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3134 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3135 | com_addfwref(c, FOR_ITER, &anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3136 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3137 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3138 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3139 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3140 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3141 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3142 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3143 | com_backpatch(c, anchor); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3144 | com_pop(c, 1); /* FOR_ITER has popped this */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3145 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3146 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3147 | if (NCH(n) > 8) |
| 3148 | com_node(c, CHILD(n, 8)); |
| 3149 | com_backpatch(c, break_anchor); |
| 3150 | } |
| 3151 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3152 | /* Code generated for "try: S finally: Sf" is as follows: |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3153 | |
| 3154 | SETUP_FINALLY L |
| 3155 | <code for S> |
| 3156 | POP_BLOCK |
| 3157 | LOAD_CONST <nil> |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3158 | L: <code for Sf> |
| 3159 | END_FINALLY |
| 3160 | |
| 3161 | The special instructions use the block stack. Each block |
| 3162 | stack entry contains the instruction that created it (here |
| 3163 | SETUP_FINALLY), the level of the value stack at the time the |
| 3164 | block stack entry was created, and a label (here L). |
| 3165 | |
| 3166 | SETUP_FINALLY: |
| 3167 | Pushes the current value stack level and the label |
| 3168 | onto the block stack. |
| 3169 | POP_BLOCK: |
| 3170 | Pops en entry from the block stack, and pops the value |
| 3171 | stack until its level is the same as indicated on the |
| 3172 | block stack. (The label is ignored.) |
| 3173 | END_FINALLY: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3174 | Pops a variable number of entries from the *value* stack |
| 3175 | and re-raises the exception they specify. The number of |
| 3176 | entries popped depends on the (pseudo) exception type. |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3177 | |
| 3178 | The block stack is unwound when an exception is raised: |
| 3179 | when a SETUP_FINALLY entry is found, the exception is pushed |
| 3180 | onto the value stack (and the exception condition is cleared), |
| 3181 | and the interpreter jumps to the label gotten from the block |
| 3182 | stack. |
| 3183 | |
| 3184 | 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] | 3185 | (The contents of the value stack is shown in [], with the top |
| 3186 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3187 | associated value, and 'exc' the exception.) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3188 | |
| 3189 | Value stack Label Instruction Argument |
| 3190 | [] SETUP_EXCEPT L1 |
| 3191 | [] <code for S> |
| 3192 | [] POP_BLOCK |
| 3193 | [] JUMP_FORWARD L0 |
| 3194 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3195 | [tb, val, exc] L1: DUP ) |
| 3196 | [tb, val, exc, exc] <evaluate E1> ) |
| 3197 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 3198 | [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 ) |
| 3199 | [tb, val, exc, 1] POP ) |
| 3200 | [tb, val, exc] POP |
| 3201 | [tb, val] <assign to V1> (or POP if no V1) |
| 3202 | [tb] POP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3203 | [] <code for S1> |
| 3204 | JUMP_FORWARD L0 |
| 3205 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3206 | [tb, val, exc, 0] L2: POP |
| 3207 | [tb, val, exc] DUP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3208 | .............................etc....................... |
| 3209 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3210 | [tb, val, exc, 0] Ln+1: POP |
| 3211 | [tb, val, exc] END_FINALLY # re-raise exception |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3212 | |
| 3213 | [] L0: <next statement> |
| 3214 | |
| 3215 | Of course, parts are not generated if Vi or Ei is not present. |
| 3216 | */ |
| 3217 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3218 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3219 | com_try_except(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3220 | { |
| 3221 | int except_anchor = 0; |
| 3222 | int end_anchor = 0; |
| 3223 | int else_anchor = 0; |
| 3224 | int i; |
| 3225 | node *ch; |
| 3226 | |
| 3227 | com_addfwref(c, SETUP_EXCEPT, &except_anchor); |
| 3228 | block_push(c, SETUP_EXCEPT); |
| 3229 | com_node(c, CHILD(n, 2)); |
| 3230 | com_addbyte(c, POP_BLOCK); |
| 3231 | block_pop(c, SETUP_EXCEPT); |
| 3232 | com_addfwref(c, JUMP_FORWARD, &else_anchor); |
| 3233 | com_backpatch(c, except_anchor); |
| 3234 | for (i = 3; |
| 3235 | i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause; |
| 3236 | i += 3) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3237 | /* except_clause: 'except' [expr [',' var]] */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3238 | if (except_anchor == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3239 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 3240 | "default 'except:' must be last"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3241 | break; |
| 3242 | } |
| 3243 | except_anchor = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3244 | com_push(c, 3); /* tb, val, exc pushed by exception */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3245 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 3246 | if (NCH(ch) > 1) { |
| 3247 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3248 | com_push(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3249 | com_node(c, CHILD(ch, 1)); |
| 3250 | com_addoparg(c, COMPARE_OP, EXC_MATCH); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3251 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3252 | com_addfwref(c, JUMP_IF_FALSE, &except_anchor); |
| 3253 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3254 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3255 | } |
| 3256 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3257 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3258 | if (NCH(ch) > 3) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3259 | com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3260 | else { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3261 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3262 | com_pop(c, 1); |
| 3263 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3264 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3265 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3266 | com_node(c, CHILD(n, i+2)); |
| 3267 | com_addfwref(c, JUMP_FORWARD, &end_anchor); |
| 3268 | if (except_anchor) { |
| 3269 | com_backpatch(c, except_anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3270 | /* We come in with [tb, val, exc, 0] on the |
| 3271 | stack; one pop and it's the same as |
| 3272 | expected at the start of the loop */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3273 | com_addbyte(c, POP_TOP); |
| 3274 | } |
| 3275 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3276 | /* We actually come in here with [tb, val, exc] but the |
| 3277 | END_FINALLY will zap those and jump around. |
| 3278 | The c_stacklevel does not reflect them so we need not pop |
| 3279 | anything. */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3280 | com_addbyte(c, END_FINALLY); |
| 3281 | com_backpatch(c, else_anchor); |
| 3282 | if (i < NCH(n)) |
| 3283 | com_node(c, CHILD(n, i+2)); |
| 3284 | com_backpatch(c, end_anchor); |
| 3285 | } |
| 3286 | |
| 3287 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3288 | com_try_finally(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3289 | { |
| 3290 | int finally_anchor = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3291 | node *ch; |
Guido van Rossum | 94fb82e | 1992-04-05 14:24:50 +0000 | [diff] [blame] | 3292 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3293 | com_addfwref(c, SETUP_FINALLY, &finally_anchor); |
| 3294 | block_push(c, SETUP_FINALLY); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3295 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3296 | com_addbyte(c, POP_BLOCK); |
| 3297 | block_pop(c, SETUP_FINALLY); |
| 3298 | block_push(c, END_FINALLY); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3299 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3300 | /* While the generated code pushes only one item, |
| 3301 | the try-finally handling can enter here with |
| 3302 | up to three items. OK, here are the details: |
| 3303 | 3 for an exception, 2 for RETURN, 1 for BREAK. */ |
| 3304 | com_push(c, 3); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3305 | com_backpatch(c, finally_anchor); |
| 3306 | ch = CHILD(n, NCH(n)-1); |
| 3307 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 3308 | com_node(c, ch); |
| 3309 | com_addbyte(c, END_FINALLY); |
| 3310 | block_pop(c, END_FINALLY); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3311 | com_pop(c, 3); /* Matches the com_push above */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3312 | } |
| 3313 | |
| 3314 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3315 | com_try_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3316 | { |
| 3317 | REQ(n, try_stmt); |
| 3318 | /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
| 3319 | | 'try' ':' suite 'finally' ':' suite */ |
| 3320 | if (TYPE(CHILD(n, 3)) != except_clause) |
| 3321 | com_try_finally(c, n); |
| 3322 | else |
| 3323 | com_try_except(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3324 | } |
| 3325 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3326 | static node * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3327 | get_rawdocstring(node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3328 | { |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3329 | int i; |
| 3330 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3331 | /* Label to avoid tail recursion */ |
| 3332 | next: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3333 | switch (TYPE(n)) { |
| 3334 | |
| 3335 | case suite: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3336 | if (NCH(n) == 1) { |
| 3337 | n = CHILD(n, 0); |
| 3338 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3339 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3340 | /* Fall through */ |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3341 | case file_input: |
| 3342 | for (i = 0; i < NCH(n); i++) { |
| 3343 | node *ch = CHILD(n, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3344 | if (TYPE(ch) == stmt) { |
| 3345 | n = ch; |
| 3346 | goto next; |
| 3347 | } |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3348 | } |
| 3349 | break; |
| 3350 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3351 | case stmt: |
| 3352 | case simple_stmt: |
| 3353 | case small_stmt: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3354 | n = CHILD(n, 0); |
| 3355 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3356 | |
| 3357 | case expr_stmt: |
| 3358 | case testlist: |
| 3359 | case test: |
| 3360 | case and_test: |
| 3361 | case not_test: |
| 3362 | case comparison: |
| 3363 | case expr: |
| 3364 | case xor_expr: |
| 3365 | case and_expr: |
| 3366 | case shift_expr: |
| 3367 | case arith_expr: |
| 3368 | case term: |
| 3369 | case factor: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3370 | case power: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3371 | if (NCH(n) == 1) { |
| 3372 | n = CHILD(n, 0); |
| 3373 | goto next; |
| 3374 | } |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3375 | break; |
| 3376 | |
| 3377 | case atom: |
| 3378 | if (TYPE(CHILD(n, 0)) == STRING) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3379 | return n; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3380 | break; |
| 3381 | |
| 3382 | } |
| 3383 | return NULL; |
| 3384 | } |
| 3385 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3386 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3387 | get_docstring(struct compiling *c, node *n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3388 | { |
Guido van Rossum | 541563e | 1999-01-28 15:08:09 +0000 | [diff] [blame] | 3389 | /* Don't generate doc-strings if run with -OO */ |
| 3390 | if (Py_OptimizeFlag > 1) |
| 3391 | return NULL; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3392 | n = get_rawdocstring(n); |
| 3393 | if (n == NULL) |
| 3394 | return NULL; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3395 | return parsestrplus(c, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3396 | } |
| 3397 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3398 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3399 | com_suite(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3400 | { |
| 3401 | REQ(n, suite); |
| 3402 | /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */ |
| 3403 | if (NCH(n) == 1) { |
| 3404 | com_node(c, CHILD(n, 0)); |
| 3405 | } |
| 3406 | else { |
| 3407 | int i; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3408 | for (i = 0; i < NCH(n) && c->c_errors == 0; i++) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3409 | node *ch = CHILD(n, i); |
| 3410 | if (TYPE(ch) == stmt) |
| 3411 | com_node(c, ch); |
| 3412 | } |
| 3413 | } |
| 3414 | } |
| 3415 | |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3416 | /* ARGSUSED */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3417 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3418 | com_continue_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3419 | { |
| 3420 | int i = c->c_nblocks; |
| 3421 | if (i-- > 0 && c->c_block[i] == SETUP_LOOP) { |
| 3422 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3423 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3424 | else if (i <= 0) { |
| 3425 | /* at the outer level */ |
| 3426 | com_error(c, PyExc_SyntaxError, |
| 3427 | "'continue' not properly in loop"); |
| 3428 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3429 | else { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3430 | int j; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3431 | for (j = i-1; j >= 0; --j) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3432 | if (c->c_block[j] == SETUP_LOOP) |
| 3433 | break; |
| 3434 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3435 | if (j >= 0) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3436 | /* there is a loop, but something interferes */ |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3437 | for (; i > j; --i) { |
| 3438 | if (c->c_block[i] == SETUP_EXCEPT || |
| 3439 | c->c_block[i] == SETUP_FINALLY) { |
| 3440 | com_addoparg(c, CONTINUE_LOOP, |
| 3441 | c->c_begin); |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3442 | return; |
| 3443 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3444 | if (c->c_block[i] == END_FINALLY) { |
| 3445 | com_error(c, PyExc_SyntaxError, |
| 3446 | "'continue' not supported inside 'finally' clause"); |
| 3447 | return; |
| 3448 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3449 | } |
| 3450 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3451 | com_error(c, PyExc_SyntaxError, |
| 3452 | "'continue' not properly in loop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3453 | } |
| 3454 | /* XXX Could allow it inside a 'finally' clause |
| 3455 | XXX if we could pop the exception still on the stack */ |
| 3456 | } |
| 3457 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3458 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3459 | com_argdefs(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3460 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3461 | int i, nch, nargs, ndefs; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3462 | if (TYPE(n) == lambdef) { |
| 3463 | /* lambdef: 'lambda' [varargslist] ':' test */ |
| 3464 | n = CHILD(n, 1); |
| 3465 | } |
| 3466 | else { |
| 3467 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */ |
| 3468 | n = CHILD(n, 2); |
| 3469 | REQ(n, parameters); /* parameters: '(' [varargslist] ')' */ |
| 3470 | n = CHILD(n, 1); |
| 3471 | } |
| 3472 | if (TYPE(n) != varargslist) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3473 | return 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3474 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3475 | (fpdef ['=' test] ',')* '*' ....... | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3476 | fpdef ['=' test] (',' fpdef ['=' test])* [','] */ |
| 3477 | nch = NCH(n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3478 | nargs = 0; |
| 3479 | ndefs = 0; |
| 3480 | for (i = 0; i < nch; i++) { |
| 3481 | int t; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3482 | if (TYPE(CHILD(n, i)) == STAR || |
| 3483 | TYPE(CHILD(n, i)) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3484 | break; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3485 | nargs++; |
| 3486 | i++; |
| 3487 | if (i >= nch) |
Guido van Rossum | a1e7e62 | 1995-09-18 21:44:04 +0000 | [diff] [blame] | 3488 | t = RPAR; /* Anything except EQUAL or COMMA */ |
| 3489 | else |
| 3490 | t = TYPE(CHILD(n, i)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3491 | if (t == EQUAL) { |
| 3492 | i++; |
| 3493 | ndefs++; |
| 3494 | com_node(c, CHILD(n, i)); |
| 3495 | i++; |
| 3496 | if (i >= nch) |
| 3497 | break; |
| 3498 | t = TYPE(CHILD(n, i)); |
| 3499 | } |
| 3500 | else { |
Guido van Rossum | 29d38cd | 1998-10-02 13:41:54 +0000 | [diff] [blame] | 3501 | /* Treat "(a=1, b)" as an error */ |
| 3502 | if (ndefs) |
| 3503 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | dfede31 | 1998-10-02 14:06:56 +0000 | [diff] [blame] | 3504 | "non-default argument follows default argument"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3505 | } |
| 3506 | if (t != COMMA) |
| 3507 | break; |
| 3508 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3509 | return ndefs; |
| 3510 | } |
| 3511 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3512 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3513 | com_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3514 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3515 | PyObject *co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3516 | int ndefs; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3517 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3518 | ndefs = com_argdefs(c, n); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3519 | symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n), |
| 3520 | n->n_lineno); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3521 | co = (PyObject *)icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3522 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3523 | if (co == NULL) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3524 | c->c_errors++; |
| 3525 | else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3526 | int closure = com_make_closure(c, (PyCodeObject *)co); |
| 3527 | int i = com_addconst(c, co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3528 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3529 | com_push(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3530 | if (closure) |
| 3531 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
| 3532 | else |
| 3533 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3534 | com_pop(c, ndefs); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3535 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3536 | com_pop(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3537 | Py_DECREF(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3538 | } |
| 3539 | } |
| 3540 | |
| 3541 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3542 | com_bases(struct compiling *c, node *n) |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3543 | { |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3544 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3545 | REQ(n, testlist); |
| 3546 | /* testlist: test (',' test)* [','] */ |
| 3547 | for (i = 0; i < NCH(n); i += 2) |
| 3548 | com_node(c, CHILD(n, i)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3549 | i = (NCH(n)+1) / 2; |
| 3550 | com_addoparg(c, BUILD_TUPLE, i); |
| 3551 | com_pop(c, i-1); |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3555 | com_classdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3556 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3557 | int i; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3558 | PyObject *v; |
| 3559 | PyCodeObject *co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3560 | char *name; |
| 3561 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3562 | REQ(n, classdef); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3563 | /* classdef: class NAME ['(' testlist ')'] ':' suite */ |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 3564 | if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3565 | c->c_errors++; |
| 3566 | return; |
| 3567 | } |
| 3568 | /* Push the class name on the stack */ |
| 3569 | i = com_addconst(c, v); |
| 3570 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3571 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3572 | Py_DECREF(v); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3573 | /* Push the tuple of base classes on the stack */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3574 | if (TYPE(CHILD(n, 2)) != LPAR) { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3575 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3576 | com_push(c, 1); |
| 3577 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3578 | else |
| 3579 | com_bases(c, CHILD(n, 3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3580 | name = STR(CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3581 | symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3582 | co = icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3583 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3584 | if (co == NULL) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3585 | c->c_errors++; |
| 3586 | else { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3587 | int closure = com_make_closure(c, co); |
| 3588 | i = com_addconst(c, (PyObject *)co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3589 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3590 | com_push(c, 1); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3591 | if (closure) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3592 | com_addoparg(c, MAKE_CLOSURE, 0); |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame^] | 3593 | com_pop(c, PyCode_GetNumFree(co)); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3594 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3595 | com_addoparg(c, MAKE_FUNCTION, 0); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3596 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3597 | com_addbyte(c, BUILD_CLASS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3598 | com_pop(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3599 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3600 | com_pop(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3601 | Py_DECREF(co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3602 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3603 | } |
| 3604 | |
| 3605 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3606 | com_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3607 | { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3608 | loop: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3609 | if (c->c_errors) |
| 3610 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3611 | switch (TYPE(n)) { |
| 3612 | |
| 3613 | /* Definition nodes */ |
| 3614 | |
| 3615 | case funcdef: |
| 3616 | com_funcdef(c, n); |
| 3617 | break; |
| 3618 | case classdef: |
| 3619 | com_classdef(c, n); |
| 3620 | break; |
| 3621 | |
| 3622 | /* Trivial parse tree nodes */ |
| 3623 | |
| 3624 | case stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3625 | case small_stmt: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3626 | case flow_stmt: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3627 | n = CHILD(n, 0); |
| 3628 | goto loop; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3629 | |
| 3630 | case simple_stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3631 | /* small_stmt (';' small_stmt)* [';'] NEWLINE */ |
| 3632 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 3633 | { |
| 3634 | int i; |
| 3635 | for (i = 0; i < NCH(n)-1; i += 2) |
| 3636 | com_node(c, CHILD(n, i)); |
| 3637 | } |
| 3638 | break; |
| 3639 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3640 | case compound_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3641 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3642 | n = CHILD(n, 0); |
| 3643 | goto loop; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3644 | |
| 3645 | /* Statement nodes */ |
| 3646 | |
| 3647 | case expr_stmt: |
| 3648 | com_expr_stmt(c, n); |
| 3649 | break; |
| 3650 | case print_stmt: |
| 3651 | com_print_stmt(c, n); |
| 3652 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3653 | case del_stmt: /* 'del' exprlist */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3654 | com_assign(c, CHILD(n, 1), OP_DELETE, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3655 | break; |
| 3656 | case pass_stmt: |
| 3657 | break; |
| 3658 | case break_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3659 | if (c->c_loops == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3660 | com_error(c, PyExc_SyntaxError, |
| 3661 | "'break' outside loop"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3662 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3663 | com_addbyte(c, BREAK_LOOP); |
| 3664 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3665 | case continue_stmt: |
| 3666 | com_continue_stmt(c, n); |
| 3667 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3668 | case return_stmt: |
| 3669 | com_return_stmt(c, n); |
| 3670 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3671 | case yield_stmt: |
| 3672 | com_yield_stmt(c, n); |
| 3673 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3674 | case raise_stmt: |
| 3675 | com_raise_stmt(c, n); |
| 3676 | break; |
| 3677 | case import_stmt: |
| 3678 | com_import_stmt(c, n); |
| 3679 | break; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3680 | case global_stmt: |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3681 | break; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3682 | case exec_stmt: |
| 3683 | com_exec_stmt(c, n); |
| 3684 | break; |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 3685 | case assert_stmt: |
| 3686 | com_assert_stmt(c, n); |
| 3687 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3688 | case if_stmt: |
| 3689 | com_if_stmt(c, n); |
| 3690 | break; |
| 3691 | case while_stmt: |
| 3692 | com_while_stmt(c, n); |
| 3693 | break; |
| 3694 | case for_stmt: |
| 3695 | com_for_stmt(c, n); |
| 3696 | break; |
| 3697 | case try_stmt: |
| 3698 | com_try_stmt(c, n); |
| 3699 | break; |
| 3700 | case suite: |
| 3701 | com_suite(c, n); |
| 3702 | break; |
| 3703 | |
| 3704 | /* Expression nodes */ |
| 3705 | |
| 3706 | case testlist: |
Guido van Rossum | 1c91707 | 2001-10-15 15:44:05 +0000 | [diff] [blame] | 3707 | case testlist_safe: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3708 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3709 | break; |
| 3710 | case test: |
| 3711 | com_test(c, n); |
| 3712 | break; |
| 3713 | case and_test: |
| 3714 | com_and_test(c, n); |
| 3715 | break; |
| 3716 | case not_test: |
| 3717 | com_not_test(c, n); |
| 3718 | break; |
| 3719 | case comparison: |
| 3720 | com_comparison(c, n); |
| 3721 | break; |
| 3722 | case exprlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3723 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3724 | break; |
| 3725 | case expr: |
| 3726 | com_expr(c, n); |
| 3727 | break; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 3728 | case xor_expr: |
| 3729 | com_xor_expr(c, n); |
| 3730 | break; |
| 3731 | case and_expr: |
| 3732 | com_and_expr(c, n); |
| 3733 | break; |
| 3734 | case shift_expr: |
| 3735 | com_shift_expr(c, n); |
| 3736 | break; |
| 3737 | case arith_expr: |
| 3738 | com_arith_expr(c, n); |
| 3739 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3740 | case term: |
| 3741 | com_term(c, n); |
| 3742 | break; |
| 3743 | case factor: |
| 3744 | com_factor(c, n); |
| 3745 | break; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3746 | case power: |
| 3747 | com_power(c, n); |
| 3748 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3749 | case atom: |
| 3750 | com_atom(c, n); |
| 3751 | break; |
| 3752 | |
| 3753 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3754 | com_error(c, PyExc_SystemError, |
| 3755 | "com_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3756 | } |
| 3757 | } |
| 3758 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 3759 | static void com_fplist(struct compiling *, node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3760 | |
| 3761 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3762 | com_fpdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3763 | { |
| 3764 | REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3765 | if (TYPE(CHILD(n, 0)) == LPAR) |
| 3766 | com_fplist(c, CHILD(n, 1)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3767 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3768 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3769 | com_pop(c, 1); |
| 3770 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3771 | } |
| 3772 | |
| 3773 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3774 | com_fplist(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3775 | { |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 3776 | REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3777 | if (NCH(n) == 1) { |
| 3778 | com_fpdef(c, CHILD(n, 0)); |
| 3779 | } |
| 3780 | else { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3781 | int i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 3782 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3783 | com_push(c, i-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3784 | for (i = 0; i < NCH(n); i += 2) |
| 3785 | com_fpdef(c, CHILD(n, i)); |
| 3786 | } |
| 3787 | } |
| 3788 | |
| 3789 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3790 | com_arglist(struct compiling *c, node *n) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3791 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3792 | int nch, i, narg; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3793 | int complex = 0; |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 3794 | char nbuf[30]; |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3795 | REQ(n, varargslist); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 3796 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3797 | (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3798 | nch = NCH(n); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3799 | /* Enter all arguments in table of locals */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3800 | for (i = 0, narg = 0; i < nch; i++) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3801 | node *ch = CHILD(n, i); |
| 3802 | node *fp; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3803 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3804 | break; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3805 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3806 | fp = CHILD(ch, 0); |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 3807 | if (TYPE(fp) != NAME) { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 3808 | PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i); |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 3809 | complex = 1; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3810 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3811 | narg++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3812 | /* all name updates handled by symtable */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3813 | if (++i >= nch) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3814 | break; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3815 | ch = CHILD(n, i); |
| 3816 | if (TYPE(ch) == EQUAL) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3817 | i += 2; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3818 | else |
| 3819 | REQ(ch, COMMA); |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3820 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3821 | if (complex) { |
| 3822 | /* Generate code for complex arguments only after |
| 3823 | having counted the simple arguments */ |
| 3824 | int ilocal = 0; |
| 3825 | for (i = 0; i < nch; i++) { |
| 3826 | node *ch = CHILD(n, i); |
| 3827 | node *fp; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3828 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3829 | break; |
| 3830 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3831 | fp = CHILD(ch, 0); |
| 3832 | if (TYPE(fp) != NAME) { |
| 3833 | com_addoparg(c, LOAD_FAST, ilocal); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3834 | com_push(c, 1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3835 | com_fpdef(c, ch); |
| 3836 | } |
| 3837 | ilocal++; |
| 3838 | if (++i >= nch) |
| 3839 | break; |
| 3840 | ch = CHILD(n, i); |
| 3841 | if (TYPE(ch) == EQUAL) |
| 3842 | i += 2; |
| 3843 | else |
| 3844 | REQ(ch, COMMA); |
| 3845 | } |
| 3846 | } |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3847 | } |
| 3848 | |
| 3849 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3850 | com_file_input(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3851 | { |
| 3852 | int i; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3853 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3854 | REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3855 | doc = get_docstring(c, n); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3856 | if (doc != NULL) { |
| 3857 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3858 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3859 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3860 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3861 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3862 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3863 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3864 | for (i = 0; i < NCH(n); i++) { |
| 3865 | node *ch = CHILD(n, i); |
| 3866 | if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE) |
| 3867 | com_node(c, ch); |
| 3868 | } |
| 3869 | } |
| 3870 | |
| 3871 | /* Top-level compile-node interface */ |
| 3872 | |
| 3873 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3874 | compile_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3875 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3876 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3877 | node *ch; |
| 3878 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3879 | c->c_name = STR(CHILD(n, 1)); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3880 | doc = get_docstring(c, CHILD(n, 4)); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3881 | if (doc != NULL) { |
| 3882 | (void) com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3883 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3884 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3885 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3886 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3887 | ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */ |
| 3888 | ch = CHILD(ch, 1); /* ')' | varargslist */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3889 | if (TYPE(ch) == varargslist) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3890 | com_arglist(c, ch); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3891 | c->c_infunction = 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3892 | com_node(c, CHILD(n, 4)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3893 | c->c_infunction = 0; |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 3894 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
| 3895 | com_push(c, 1); |
| 3896 | com_addbyte(c, RETURN_VALUE); |
| 3897 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
| 3900 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3901 | compile_lambdef(struct compiling *c, node *n) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 3902 | { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3903 | node *ch; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3904 | REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */ |
Guido van Rossum | 363ac7d | 1994-11-10 22:40:34 +0000 | [diff] [blame] | 3905 | c->c_name = "<lambda>"; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3906 | |
| 3907 | ch = CHILD(n, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3908 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3909 | if (TYPE(ch) == varargslist) { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3910 | com_arglist(c, ch); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3911 | ch = CHILD(n, 3); |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3912 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3913 | else |
| 3914 | ch = CHILD(n, 2); |
| 3915 | com_node(c, ch); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 3916 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3917 | com_pop(c, 1); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 3918 | } |
| 3919 | |
| 3920 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3921 | compile_classdef(struct compiling *c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3922 | { |
| 3923 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3924 | PyObject *doc; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3925 | REQ(n, classdef); |
| 3926 | /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ |
| 3927 | c->c_name = STR(CHILD(n, 1)); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 3928 | c->c_private = c->c_name; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3929 | ch = CHILD(n, NCH(n)-1); /* The suite */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3930 | doc = get_docstring(c, ch); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3931 | if (doc != NULL) { |
| 3932 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3933 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3934 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3935 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3936 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3937 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3938 | } |
| 3939 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3940 | (void) com_addconst(c, Py_None); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3941 | com_node(c, ch); |
| 3942 | com_addbyte(c, LOAD_LOCALS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3943 | com_push(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3944 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3945 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3946 | } |
| 3947 | |
| 3948 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3949 | compile_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3950 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3951 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 3952 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3953 | switch (TYPE(n)) { |
| 3954 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3955 | case single_input: /* One interactive command */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3956 | /* NEWLINE | simple_stmt | compound_stmt NEWLINE */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3957 | c->c_interactive++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3958 | n = CHILD(n, 0); |
| 3959 | if (TYPE(n) != NEWLINE) |
| 3960 | com_node(c, n); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3961 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3962 | com_push(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3963 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3964 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3965 | c->c_interactive--; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3966 | break; |
| 3967 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3968 | case file_input: /* A whole file, or built-in function exec() */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3969 | com_file_input(c, n); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3970 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3971 | com_push(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3972 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3973 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3974 | break; |
| 3975 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3976 | case eval_input: /* Built-in function input() */ |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3977 | com_node(c, CHILD(n, 0)); |
| 3978 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3979 | com_pop(c, 1); |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3980 | break; |
| 3981 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3982 | case lambdef: /* anonymous function definition */ |
| 3983 | compile_lambdef(c, n); |
| 3984 | break; |
| 3985 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3986 | case funcdef: /* A function definition */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3987 | compile_funcdef(c, n); |
| 3988 | break; |
| 3989 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3990 | case classdef: /* A class definition */ |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3991 | compile_classdef(c, n); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3992 | break; |
| 3993 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3994 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3995 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 3996 | "compile_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3997 | } |
| 3998 | } |
| 3999 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4000 | static PyObject * |
| 4001 | dict_keys_inorder(PyObject *dict, int offset) |
| 4002 | { |
| 4003 | PyObject *tuple, *k, *v; |
| 4004 | int i, pos = 0, size = PyDict_Size(dict); |
| 4005 | |
| 4006 | tuple = PyTuple_New(size); |
| 4007 | if (tuple == NULL) |
| 4008 | return NULL; |
| 4009 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 4010 | i = PyInt_AS_LONG(v); |
| 4011 | Py_INCREF(k); |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4012 | assert((i - offset) < size); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4013 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 4014 | } |
| 4015 | return tuple; |
| 4016 | } |
| 4017 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4018 | PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4019 | PyNode_Compile(node *n, char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4020 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4021 | return PyNode_CompileFlags(n, filename, NULL); |
| 4022 | } |
| 4023 | |
| 4024 | PyCodeObject * |
| 4025 | PyNode_CompileFlags(node *n, char *filename, PyCompilerFlags *flags) |
| 4026 | { |
| 4027 | return jcompile(n, filename, NULL, flags); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4028 | } |
| 4029 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4030 | struct symtable * |
| 4031 | PyNode_CompileSymtable(node *n, char *filename) |
| 4032 | { |
| 4033 | struct symtable *st; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4034 | PyFutureFeatures *ff; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4035 | |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4036 | ff = PyNode_Future(n, filename); |
| 4037 | if (ff == NULL) |
| 4038 | return NULL; |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4039 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4040 | st = symtable_init(); |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4041 | if (st == NULL) { |
| 4042 | PyMem_Free((void *)ff); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4043 | return NULL; |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4044 | } |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4045 | st->st_future = ff; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4046 | symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4047 | if (st->st_errors > 0) |
| 4048 | goto fail; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4049 | symtable_node(st, n); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4050 | if (st->st_errors > 0) |
| 4051 | goto fail; |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4052 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4053 | return st; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4054 | fail: |
| 4055 | PyMem_Free((void *)ff); |
| 4056 | st->st_future = NULL; |
| 4057 | PySymtable_Free(st); |
| 4058 | return NULL; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4059 | } |
| 4060 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4061 | static PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4062 | icompile(node *n, struct compiling *base) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4063 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4064 | return jcompile(n, base->c_filename, base, NULL); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4065 | } |
| 4066 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4067 | static PyCodeObject * |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4068 | jcompile(node *n, char *filename, struct compiling *base, |
| 4069 | PyCompilerFlags *flags) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4070 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4071 | struct compiling sc; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4072 | PyCodeObject *co; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4073 | if (!com_init(&sc, filename)) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4074 | return NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4075 | if (base) { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4076 | sc.c_private = base->c_private; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4077 | sc.c_symtable = base->c_symtable; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4078 | /* c_symtable still points to parent's symbols */ |
| 4079 | if (base->c_nested |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4080 | || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4081 | sc.c_nested = 1; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 4082 | sc.c_flags |= base->c_flags & PyCF_MASK; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4083 | } else { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4084 | sc.c_private = NULL; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 4085 | sc.c_future = PyNode_Future(n, filename); |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4086 | if (sc.c_future == NULL) { |
| 4087 | com_free(&sc); |
| 4088 | return NULL; |
| 4089 | } |
Jeremy Hylton | 481081e | 2001-08-14 20:01:59 +0000 | [diff] [blame] | 4090 | if (flags) { |
| 4091 | int merged = sc.c_future->ff_features | |
| 4092 | flags->cf_flags; |
| 4093 | sc.c_future->ff_features = merged; |
| 4094 | flags->cf_flags = merged; |
| 4095 | } |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4096 | if (symtable_build(&sc, n) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4097 | com_free(&sc); |
| 4098 | return NULL; |
| 4099 | } |
| 4100 | } |
| 4101 | co = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4102 | if (symtable_load_symbols(&sc) < 0) { |
| 4103 | sc.c_errors++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4104 | goto exit; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4105 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4106 | compile_node(&sc, n); |
| 4107 | com_done(&sc); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 4108 | if (sc.c_errors == 0) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4109 | PyObject *consts, *names, *varnames, *filename, *name, |
| 4110 | *freevars, *cellvars; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4111 | consts = PyList_AsTuple(sc.c_consts); |
| 4112 | names = PyList_AsTuple(sc.c_names); |
| 4113 | varnames = PyList_AsTuple(sc.c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4114 | cellvars = dict_keys_inorder(sc.c_cellvars, 0); |
| 4115 | freevars = dict_keys_inorder(sc.c_freevars, |
| 4116 | PyTuple_GET_SIZE(cellvars)); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 4117 | filename = PyString_InternFromString(sc.c_filename); |
| 4118 | name = PyString_InternFromString(sc.c_name); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4119 | if (!PyErr_Occurred()) |
| 4120 | co = PyCode_New(sc.c_argcount, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4121 | sc.c_nlocals, |
| 4122 | sc.c_maxstacklevel, |
| 4123 | sc.c_flags, |
| 4124 | sc.c_code, |
| 4125 | consts, |
| 4126 | names, |
| 4127 | varnames, |
| 4128 | freevars, |
| 4129 | cellvars, |
| 4130 | filename, |
| 4131 | name, |
| 4132 | sc.c_firstlineno, |
| 4133 | sc.c_lnotab); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4134 | Py_XDECREF(consts); |
| 4135 | Py_XDECREF(names); |
| 4136 | Py_XDECREF(varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4137 | Py_XDECREF(freevars); |
| 4138 | Py_XDECREF(cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4139 | Py_XDECREF(filename); |
| 4140 | Py_XDECREF(name); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 4141 | } |
Guido van Rossum | 6c2f0c7 | 2000-08-07 19:22:43 +0000 | [diff] [blame] | 4142 | else if (!PyErr_Occurred()) { |
| 4143 | /* This could happen if someone called PyErr_Clear() after an |
| 4144 | error was reported above. That's not supposed to happen, |
| 4145 | but I just plugged one case and I'm not sure there can't be |
| 4146 | others. In that case, raise SystemError so that at least |
| 4147 | it gets reported instead dumping core. */ |
| 4148 | PyErr_SetString(PyExc_SystemError, "lost syntax error"); |
| 4149 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4150 | exit: |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4151 | if (base == NULL) { |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4152 | PySymtable_Free(sc.c_symtable); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4153 | sc.c_symtable = NULL; |
| 4154 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4155 | com_free(&sc); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4156 | return co; |
| 4157 | } |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4158 | |
| 4159 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4160 | PyCode_Addr2Line(PyCodeObject *co, int addrq) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4161 | { |
| 4162 | int size = PyString_Size(co->co_lnotab) / 2; |
Guido van Rossum | 2174dcb | 1999-09-15 22:48:09 +0000 | [diff] [blame] | 4163 | unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4164 | int line = co->co_firstlineno; |
| 4165 | int addr = 0; |
| 4166 | while (--size >= 0) { |
| 4167 | addr += *p++; |
| 4168 | if (addr > addrq) |
| 4169 | break; |
| 4170 | line += *p++; |
| 4171 | } |
| 4172 | return line; |
| 4173 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4174 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4175 | /* The test for LOCAL must come before the test for FREE in order to |
| 4176 | handle classes where name is both local and free. The local var is |
| 4177 | a method and the free var is a free var referenced within a method. |
| 4178 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4179 | |
| 4180 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4181 | get_ref_type(struct compiling *c, char *name) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4182 | { |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4183 | char buf[350]; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4184 | PyObject *v; |
Jeremy Hylton | 5125773 | 2001-03-01 00:42:55 +0000 | [diff] [blame] | 4185 | |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4186 | if (PyDict_GetItemString(c->c_cellvars, name) != NULL) |
| 4187 | return CELL; |
| 4188 | if (PyDict_GetItemString(c->c_locals, name) != NULL) |
| 4189 | return LOCAL; |
| 4190 | if (PyDict_GetItemString(c->c_freevars, name) != NULL) |
| 4191 | return FREE; |
| 4192 | v = PyDict_GetItemString(c->c_globals, name); |
| 4193 | if (v) { |
| 4194 | if (v == Py_None) |
| 4195 | return GLOBAL_EXPLICIT; |
| 4196 | else { |
| 4197 | return GLOBAL_IMPLICIT; |
| 4198 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4199 | } |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 4200 | PyOS_snprintf(buf, sizeof(buf), |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4201 | "unknown scope for %.100s in %.100s(%s) " |
| 4202 | "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n", |
| 4203 | name, c->c_name, |
| 4204 | PyObject_REPR(c->c_symtable->st_cur->ste_id), |
| 4205 | c->c_filename, |
| 4206 | PyObject_REPR(c->c_symtable->st_cur->ste_symbols), |
| 4207 | PyObject_REPR(c->c_locals), |
| 4208 | PyObject_REPR(c->c_globals) |
| 4209 | ); |
| 4210 | |
| 4211 | Py_FatalError(buf); |
| 4212 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4213 | } |
| 4214 | |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4215 | /* Helper functions to issue warnings */ |
| 4216 | |
| 4217 | static int |
| 4218 | issue_warning(char *msg, char *filename, int lineno) |
| 4219 | { |
| 4220 | if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename, |
| 4221 | lineno, NULL, NULL) < 0) { |
| 4222 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
| 4223 | PyErr_SetString(PyExc_SyntaxError, msg); |
| 4224 | PyErr_SyntaxLocation(filename, lineno); |
| 4225 | } |
| 4226 | return -1; |
| 4227 | } |
| 4228 | return 0; |
| 4229 | } |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4230 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4231 | static int |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4232 | symtable_warn(struct symtable *st, char *msg) |
| 4233 | { |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4234 | 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] | 4235 | st->st_errors++; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4236 | return -1; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4237 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4238 | return 0; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4239 | } |
| 4240 | |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 4241 | /* Helper function for setting lineno and filename */ |
| 4242 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4243 | static int |
| 4244 | symtable_build(struct compiling *c, node *n) |
| 4245 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4246 | if ((c->c_symtable = symtable_init()) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4247 | return -1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 4248 | c->c_symtable->st_future = c->c_future; |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4249 | c->c_symtable->st_filename = c->c_filename; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4250 | symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno); |
| 4251 | if (c->c_symtable->st_errors > 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4252 | return -1; |
| 4253 | symtable_node(c->c_symtable, n); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4254 | if (c->c_symtable->st_errors > 0) |
| 4255 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4256 | /* reset for second pass */ |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4257 | c->c_symtable->st_nscopes = 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4258 | c->c_symtable->st_pass = 2; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4259 | return 0; |
| 4260 | } |
| 4261 | |
| 4262 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4263 | symtable_init_compiling_symbols(struct compiling *c) |
| 4264 | { |
| 4265 | PyObject *varnames; |
| 4266 | |
| 4267 | varnames = c->c_symtable->st_cur->ste_varnames; |
| 4268 | if (varnames == NULL) { |
| 4269 | varnames = PyList_New(0); |
| 4270 | if (varnames == NULL) |
| 4271 | return -1; |
| 4272 | c->c_symtable->st_cur->ste_varnames = varnames; |
| 4273 | Py_INCREF(varnames); |
| 4274 | } else |
| 4275 | Py_INCREF(varnames); |
| 4276 | c->c_varnames = varnames; |
| 4277 | |
| 4278 | c->c_globals = PyDict_New(); |
| 4279 | if (c->c_globals == NULL) |
| 4280 | return -1; |
| 4281 | c->c_freevars = PyDict_New(); |
| 4282 | if (c->c_freevars == NULL) |
| 4283 | return -1; |
| 4284 | c->c_cellvars = PyDict_New(); |
| 4285 | if (c->c_cellvars == NULL) |
| 4286 | return -1; |
| 4287 | return 0; |
| 4288 | } |
| 4289 | |
| 4290 | struct symbol_info { |
| 4291 | int si_nlocals; |
| 4292 | int si_ncells; |
| 4293 | int si_nfrees; |
| 4294 | int si_nimplicit; |
| 4295 | }; |
| 4296 | |
| 4297 | static void |
| 4298 | symtable_init_info(struct symbol_info *si) |
| 4299 | { |
| 4300 | si->si_nlocals = 0; |
| 4301 | si->si_ncells = 0; |
| 4302 | si->si_nfrees = 0; |
| 4303 | si->si_nimplicit = 0; |
| 4304 | } |
| 4305 | |
| 4306 | static int |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4307 | symtable_resolve_free(struct compiling *c, PyObject *name, int flags, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4308 | struct symbol_info *si) |
| 4309 | { |
| 4310 | PyObject *dict, *v; |
| 4311 | |
| 4312 | /* Seperate logic for DEF_FREE. If it occurs in a function, |
| 4313 | it indicates a local that we must allocate storage for (a |
| 4314 | cell var). If it occurs in a class, then the class has a |
| 4315 | method and a free variable with the same name. |
| 4316 | */ |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4317 | if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4318 | /* If it isn't declared locally, it can't be a cell. */ |
| 4319 | if (!(flags & (DEF_LOCAL | DEF_PARAM))) |
| 4320 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4321 | v = PyInt_FromLong(si->si_ncells++); |
| 4322 | dict = c->c_cellvars; |
| 4323 | } else { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4324 | /* If it is free anyway, then there is no need to do |
| 4325 | anything here. |
| 4326 | */ |
| 4327 | if (is_free(flags ^ DEF_FREE_CLASS) |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4328 | || (flags == DEF_FREE_CLASS)) |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4329 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4330 | v = PyInt_FromLong(si->si_nfrees++); |
| 4331 | dict = c->c_freevars; |
| 4332 | } |
| 4333 | if (v == NULL) |
| 4334 | return -1; |
| 4335 | if (PyDict_SetItem(dict, name, v) < 0) { |
| 4336 | Py_DECREF(v); |
| 4337 | return -1; |
| 4338 | } |
| 4339 | Py_DECREF(v); |
| 4340 | return 0; |
| 4341 | } |
| 4342 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4343 | /* If a variable is a cell and an argument, make sure that appears in |
| 4344 | co_cellvars before any variable to its right in varnames. |
| 4345 | */ |
| 4346 | |
| 4347 | |
| 4348 | static int |
| 4349 | symtable_cellvar_offsets(PyObject **cellvars, int argcount, |
| 4350 | PyObject *varnames, int flags) |
| 4351 | { |
| 4352 | PyObject *v, *w, *d, *list = NULL; |
| 4353 | int i, pos; |
| 4354 | |
| 4355 | if (flags & CO_VARARGS) |
| 4356 | argcount++; |
| 4357 | if (flags & CO_VARKEYWORDS) |
| 4358 | argcount++; |
| 4359 | for (i = argcount; --i >= 0; ) { |
| 4360 | v = PyList_GET_ITEM(varnames, i); |
| 4361 | if (PyDict_GetItem(*cellvars, v)) { |
| 4362 | if (list == NULL) { |
| 4363 | list = PyList_New(1); |
| 4364 | if (list == NULL) |
| 4365 | return -1; |
| 4366 | PyList_SET_ITEM(list, 0, v); |
| 4367 | Py_INCREF(v); |
| 4368 | } else |
| 4369 | PyList_Insert(list, 0, v); |
| 4370 | } |
| 4371 | } |
| 4372 | if (list == NULL || PyList_GET_SIZE(list) == 0) |
| 4373 | return 0; |
| 4374 | /* There are cellvars that are also arguments. Create a dict |
| 4375 | to replace cellvars and put the args at the front. |
| 4376 | */ |
| 4377 | d = PyDict_New(); |
| 4378 | for (i = PyList_GET_SIZE(list); --i >= 0; ) { |
| 4379 | v = PyInt_FromLong(i); |
| 4380 | if (v == NULL) |
| 4381 | goto fail; |
| 4382 | if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0) |
| 4383 | goto fail; |
| 4384 | if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0) |
| 4385 | goto fail; |
| 4386 | } |
| 4387 | pos = 0; |
| 4388 | i = PyList_GET_SIZE(list); |
| 4389 | Py_DECREF(list); |
| 4390 | while (PyDict_Next(*cellvars, &pos, &v, &w)) { |
| 4391 | w = PyInt_FromLong(i++); /* don't care about the old key */ |
| 4392 | if (PyDict_SetItem(d, v, w) < 0) { |
| 4393 | Py_DECREF(w); |
| 4394 | goto fail; |
| 4395 | } |
| 4396 | Py_DECREF(w); |
| 4397 | } |
| 4398 | Py_DECREF(*cellvars); |
| 4399 | *cellvars = d; |
| 4400 | return 1; |
| 4401 | fail: |
| 4402 | Py_DECREF(d); |
| 4403 | return -1; |
| 4404 | } |
| 4405 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4406 | static int |
| 4407 | symtable_freevar_offsets(PyObject *freevars, int offset) |
| 4408 | { |
| 4409 | PyObject *name, *v; |
| 4410 | int pos; |
| 4411 | |
| 4412 | /* The cell vars are the first elements of the closure, |
| 4413 | followed by the free vars. Update the offsets in |
| 4414 | c_freevars to account for number of cellvars. */ |
| 4415 | pos = 0; |
| 4416 | while (PyDict_Next(freevars, &pos, &name, &v)) { |
| 4417 | int i = PyInt_AS_LONG(v) + offset; |
| 4418 | PyObject *o = PyInt_FromLong(i); |
| 4419 | if (o == NULL) |
| 4420 | return -1; |
| 4421 | if (PyDict_SetItem(freevars, name, o) < 0) { |
| 4422 | Py_DECREF(o); |
| 4423 | return -1; |
| 4424 | } |
| 4425 | Py_DECREF(o); |
| 4426 | } |
| 4427 | return 0; |
| 4428 | } |
| 4429 | |
| 4430 | static int |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4431 | symtable_check_unoptimized(struct compiling *c, |
| 4432 | PySymtableEntryObject *ste, |
| 4433 | struct symbol_info *si) |
| 4434 | { |
| 4435 | char buf[300]; |
| 4436 | |
| 4437 | if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free |
| 4438 | || (ste->ste_nested && si->si_nimplicit))) |
| 4439 | return 0; |
| 4440 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4441 | #define ILLEGAL_CONTAINS "contains a nested function with free variables" |
| 4442 | |
| 4443 | #define ILLEGAL_IS "is a nested function" |
| 4444 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4445 | #define ILLEGAL_IMPORT_STAR \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4446 | "import * is not allowed in function '%.100s' because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4447 | |
| 4448 | #define ILLEGAL_BARE_EXEC \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4449 | "unqualified exec is not allowed in function '%.100s' it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4450 | |
| 4451 | #define ILLEGAL_EXEC_AND_IMPORT_STAR \ |
| 4452 | "function '%.100s' uses import * and bare exec, which are illegal" \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4453 | "because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4454 | |
| 4455 | /* XXX perhaps the linenos for these opt-breaking statements |
| 4456 | should be stored so the exception can point to them. */ |
| 4457 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4458 | if (ste->ste_child_free) { |
| 4459 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4460 | PyOS_snprintf(buf, sizeof(buf), |
| 4461 | ILLEGAL_IMPORT_STAR, |
| 4462 | PyString_AS_STRING(ste->ste_name), |
| 4463 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4464 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4465 | PyOS_snprintf(buf, sizeof(buf), |
| 4466 | ILLEGAL_BARE_EXEC, |
| 4467 | PyString_AS_STRING(ste->ste_name), |
| 4468 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4469 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4470 | PyOS_snprintf(buf, sizeof(buf), |
| 4471 | ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 4472 | PyString_AS_STRING(ste->ste_name), |
| 4473 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4474 | } |
| 4475 | } else { |
| 4476 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4477 | PyOS_snprintf(buf, sizeof(buf), |
| 4478 | ILLEGAL_IMPORT_STAR, |
| 4479 | PyString_AS_STRING(ste->ste_name), |
| 4480 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4481 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4482 | PyOS_snprintf(buf, sizeof(buf), |
| 4483 | ILLEGAL_BARE_EXEC, |
| 4484 | PyString_AS_STRING(ste->ste_name), |
| 4485 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4486 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4487 | PyOS_snprintf(buf, sizeof(buf), |
| 4488 | ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 4489 | PyString_AS_STRING(ste->ste_name), |
| 4490 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4491 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4492 | } |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4493 | |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4494 | PyErr_SetString(PyExc_SyntaxError, buf); |
| 4495 | PyErr_SyntaxLocation(c->c_symtable->st_filename, |
| 4496 | ste->ste_opt_lineno); |
| 4497 | return -1; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4498 | } |
| 4499 | |
| 4500 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4501 | symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste, |
| 4502 | struct symbol_info *si) |
| 4503 | { |
Jeremy Hylton | b857ba2 | 2001-08-10 21:41:33 +0000 | [diff] [blame] | 4504 | if (c->c_future) |
| 4505 | c->c_flags |= c->c_future->ff_features; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4506 | if (ste->ste_generator) |
| 4507 | c->c_flags |= CO_GENERATOR; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4508 | if (ste->ste_type != TYPE_MODULE) |
| 4509 | c->c_flags |= CO_NEWLOCALS; |
| 4510 | if (ste->ste_type == TYPE_FUNCTION) { |
| 4511 | c->c_nlocals = si->si_nlocals; |
| 4512 | if (ste->ste_optimized == 0) |
| 4513 | c->c_flags |= CO_OPTIMIZED; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4514 | else if (ste->ste_optimized != OPT_EXEC) |
| 4515 | return symtable_check_unoptimized(c, ste, si); |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4516 | } |
| 4517 | return 0; |
| 4518 | } |
| 4519 | |
| 4520 | static int |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4521 | symtable_load_symbols(struct compiling *c) |
| 4522 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4523 | static PyObject *implicit = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4524 | struct symtable *st = c->c_symtable; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4525 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4526 | PyObject *name, *varnames, *v; |
| 4527 | int i, flags, pos; |
| 4528 | struct symbol_info si; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4529 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4530 | if (implicit == NULL) { |
| 4531 | implicit = PyInt_FromLong(1); |
| 4532 | if (implicit == NULL) |
| 4533 | return -1; |
| 4534 | } |
| 4535 | v = NULL; |
| 4536 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4537 | if (symtable_init_compiling_symbols(c) < 0) |
| 4538 | goto fail; |
| 4539 | symtable_init_info(&si); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4540 | varnames = st->st_cur->ste_varnames; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4541 | si.si_nlocals = PyList_GET_SIZE(varnames); |
| 4542 | c->c_argcount = si.si_nlocals; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4543 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4544 | for (i = 0; i < si.si_nlocals; ++i) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4545 | v = PyInt_FromLong(i); |
| 4546 | if (PyDict_SetItem(c->c_locals, |
| 4547 | PyList_GET_ITEM(varnames, i), v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4548 | goto fail; |
| 4549 | Py_DECREF(v); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4550 | } |
| 4551 | |
| 4552 | /* XXX The cases below define the rules for whether a name is |
| 4553 | local or global. The logic could probably be clearer. */ |
| 4554 | pos = 0; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4555 | while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) { |
| 4556 | flags = PyInt_AS_LONG(v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4557 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4558 | if (flags & DEF_FREE_GLOBAL) |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4559 | /* undo the original DEF_FREE */ |
| 4560 | flags &= ~(DEF_FREE | DEF_FREE_CLASS); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4561 | |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4562 | /* Deal with names that need two actions: |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4563 | 1. Cell variables that are also locals. |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4564 | 2. Free variables in methods that are also class |
| 4565 | variables or declared global. |
| 4566 | */ |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4567 | if (flags & (DEF_FREE | DEF_FREE_CLASS)) |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4568 | symtable_resolve_free(c, name, flags, &si); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4569 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4570 | if (flags & DEF_STAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4571 | c->c_argcount--; |
| 4572 | c->c_flags |= CO_VARARGS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4573 | } else if (flags & DEF_DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4574 | c->c_argcount--; |
| 4575 | c->c_flags |= CO_VARKEYWORDS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4576 | } else if (flags & DEF_INTUPLE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4577 | c->c_argcount--; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4578 | else if (flags & DEF_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4579 | if (flags & DEF_PARAM) { |
| 4580 | PyErr_Format(PyExc_SyntaxError, LOCAL_GLOBAL, |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4581 | PyString_AS_STRING(name)); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 4582 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4583 | ste->ste_lineno); |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4584 | st->st_errors++; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4585 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4586 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4587 | if (PyDict_SetItem(c->c_globals, name, Py_None) < 0) |
| 4588 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4589 | } else if (flags & DEF_FREE_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4590 | si.si_nimplicit++; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4591 | if (PyDict_SetItem(c->c_globals, name, implicit) < 0) |
| 4592 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4593 | } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4594 | v = PyInt_FromLong(si.si_nlocals++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4595 | if (v == NULL) |
| 4596 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4597 | if (PyDict_SetItem(c->c_locals, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4598 | goto fail; |
| 4599 | Py_DECREF(v); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4600 | if (ste->ste_type != TYPE_CLASS) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4601 | if (PyList_Append(c->c_varnames, name) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4602 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4603 | } else if (is_free(flags)) { |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4604 | if (ste->ste_nested) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4605 | v = PyInt_FromLong(si.si_nfrees++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4606 | if (v == NULL) |
| 4607 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4608 | if (PyDict_SetItem(c->c_freevars, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4609 | goto fail; |
| 4610 | Py_DECREF(v); |
| 4611 | } else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4612 | si.si_nimplicit++; |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4613 | if (PyDict_SetItem(c->c_globals, name, |
| 4614 | implicit) < 0) |
| 4615 | goto fail; |
| 4616 | if (st->st_nscopes != 1) { |
| 4617 | v = PyInt_FromLong(flags); |
| 4618 | if (PyDict_SetItem(st->st_global, |
| 4619 | name, v)) |
| 4620 | goto fail; |
| 4621 | Py_DECREF(v); |
| 4622 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4623 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4624 | } |
| 4625 | } |
| 4626 | |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4627 | assert(PyDict_Size(c->c_freevars) == si.si_nfrees); |
| 4628 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4629 | if (si.si_ncells > 1) { /* one cell is always in order */ |
| 4630 | if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount, |
| 4631 | c->c_varnames, c->c_flags) < 0) |
| 4632 | return -1; |
| 4633 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4634 | if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0) |
| 4635 | return -1; |
| 4636 | return symtable_update_flags(c, ste, &si); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4637 | fail: |
| 4638 | /* is this always the right thing to do? */ |
| 4639 | Py_XDECREF(v); |
| 4640 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4641 | } |
| 4642 | |
| 4643 | static struct symtable * |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4644 | symtable_init() |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4645 | { |
| 4646 | struct symtable *st; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4647 | |
| 4648 | st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable)); |
| 4649 | if (st == NULL) |
| 4650 | return NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4651 | st->st_pass = 1; |
Tim Peters | ff1f852 | 2001-08-11 01:06:35 +0000 | [diff] [blame] | 4652 | |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4653 | st->st_filename = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4654 | if ((st->st_stack = PyList_New(0)) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4655 | goto fail; |
| 4656 | if ((st->st_symbols = PyDict_New()) == NULL) |
| 4657 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4658 | st->st_cur = NULL; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4659 | st->st_nscopes = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4660 | st->st_errors = 0; |
| 4661 | st->st_tmpname = 0; |
| 4662 | st->st_private = NULL; |
| 4663 | return st; |
| 4664 | fail: |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4665 | PySymtable_Free(st); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4666 | return NULL; |
| 4667 | } |
| 4668 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4669 | void |
| 4670 | PySymtable_Free(struct symtable *st) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4671 | { |
| 4672 | Py_XDECREF(st->st_symbols); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4673 | Py_XDECREF(st->st_stack); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4674 | Py_XDECREF(st->st_cur); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4675 | PyMem_Free((void *)st); |
| 4676 | } |
| 4677 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4678 | /* When the compiler exits a scope, it must should update the scope's |
| 4679 | free variable information with the list of free variables in its |
| 4680 | children. |
| 4681 | |
| 4682 | Variables that are free in children and defined in the current |
| 4683 | scope are cellvars. |
| 4684 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4685 | 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] | 4686 | false), free variables in children that are not defined here are |
| 4687 | implicit globals. |
| 4688 | |
| 4689 | */ |
| 4690 | |
| 4691 | static int |
| 4692 | symtable_update_free_vars(struct symtable *st) |
| 4693 | { |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4694 | int i, j, def; |
| 4695 | PyObject *o, *name, *list = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4696 | PySymtableEntryObject *child, *ste = st->st_cur; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4697 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4698 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4699 | def = DEF_FREE_CLASS; |
| 4700 | else |
| 4701 | def = DEF_FREE; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4702 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4703 | int pos = 0; |
| 4704 | |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4705 | if (list) |
| 4706 | PyList_SetSlice(list, 0, |
| 4707 | ((PyVarObject*)list)->ob_size, 0); |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4708 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4709 | PyList_GET_ITEM(ste->ste_children, i); |
| 4710 | while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4711 | int flags = PyInt_AS_LONG(o); |
| 4712 | if (!(is_free(flags))) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4713 | continue; /* avoids indentation */ |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4714 | if (list == NULL) { |
| 4715 | list = PyList_New(0); |
| 4716 | if (list == NULL) |
| 4717 | return -1; |
| 4718 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4719 | ste->ste_child_free = 1; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4720 | if (PyList_Append(list, name) < 0) { |
| 4721 | Py_DECREF(list); |
| 4722 | return -1; |
| 4723 | } |
| 4724 | } |
| 4725 | for (j = 0; list && j < PyList_GET_SIZE(list); j++) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4726 | PyObject *v; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4727 | name = PyList_GET_ITEM(list, j); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4728 | v = PyDict_GetItem(ste->ste_symbols, name); |
| 4729 | /* If a name N is declared global in scope A and |
| 4730 | referenced in scope B contained (perhaps |
| 4731 | indirectly) in A and there are no scopes |
| 4732 | with bindings for N between B and A, then N |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4733 | is global in B. Unless A is a class scope, |
| 4734 | because class scopes are not considered for |
| 4735 | nested scopes. |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4736 | */ |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4737 | if (v && (ste->ste_type != TYPE_CLASS)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4738 | int flags = PyInt_AS_LONG(v); |
| 4739 | if (flags & DEF_GLOBAL) { |
| 4740 | symtable_undo_free(st, child->ste_id, |
| 4741 | name); |
| 4742 | continue; |
| 4743 | } |
| 4744 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4745 | if (ste->ste_nested) { |
| 4746 | if (symtable_add_def_o(st, ste->ste_symbols, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4747 | name, def) < 0) { |
| 4748 | Py_DECREF(list); |
| 4749 | return -1; |
| 4750 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4751 | } else { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4752 | if (symtable_check_global(st, child->ste_id, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4753 | name) < 0) { |
| 4754 | Py_DECREF(list); |
| 4755 | return -1; |
| 4756 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4757 | } |
| 4758 | } |
| 4759 | } |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4760 | |
| 4761 | Py_XDECREF(list); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4762 | return 0; |
| 4763 | } |
| 4764 | |
| 4765 | /* If the current scope is a non-nested class or if name is not |
| 4766 | defined in the current, non-nested scope, then it is an implicit |
| 4767 | global in all nested scopes. |
| 4768 | */ |
| 4769 | |
| 4770 | static int |
| 4771 | symtable_check_global(struct symtable *st, PyObject *child, PyObject *name) |
| 4772 | { |
| 4773 | PyObject *o; |
| 4774 | int v; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4775 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4776 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4777 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4778 | return symtable_undo_free(st, child, name); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4779 | o = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4780 | if (o == NULL) |
| 4781 | return symtable_undo_free(st, child, name); |
| 4782 | v = PyInt_AS_LONG(o); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4783 | |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4784 | if (is_free(v) || (v & DEF_GLOBAL)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4785 | return symtable_undo_free(st, child, name); |
| 4786 | else |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4787 | return symtable_add_def_o(st, ste->ste_symbols, |
| 4788 | name, DEF_FREE); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4789 | } |
| 4790 | |
| 4791 | static int |
| 4792 | symtable_undo_free(struct symtable *st, PyObject *id, |
| 4793 | PyObject *name) |
| 4794 | { |
| 4795 | int i, v, x; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4796 | PyObject *info; |
| 4797 | PySymtableEntryObject *ste; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4798 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4799 | ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id); |
| 4800 | if (ste == NULL) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4801 | return -1; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4802 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4803 | info = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4804 | if (info == NULL) |
| 4805 | return 0; |
| 4806 | v = PyInt_AS_LONG(info); |
| 4807 | if (is_free(v)) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4808 | if (symtable_add_def_o(st, ste->ste_symbols, name, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4809 | DEF_FREE_GLOBAL) < 0) |
| 4810 | return -1; |
| 4811 | } else |
| 4812 | /* If the name is defined here or declared global, |
| 4813 | then the recursion stops. */ |
| 4814 | return 0; |
| 4815 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4816 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
| 4817 | PySymtableEntryObject *child; |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4818 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4819 | PyList_GET_ITEM(ste->ste_children, i); |
| 4820 | x = symtable_undo_free(st, child->ste_id, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4821 | if (x < 0) |
| 4822 | return x; |
| 4823 | } |
| 4824 | return 0; |
| 4825 | } |
| 4826 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4827 | /* symtable_enter_scope() gets a reference via PySymtableEntry_New(). |
| 4828 | This reference is released when the scope is exited, via the DECREF |
| 4829 | in symtable_exit_scope(). |
| 4830 | */ |
| 4831 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4832 | static int |
| 4833 | symtable_exit_scope(struct symtable *st) |
| 4834 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4835 | int end; |
| 4836 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4837 | if (st->st_pass == 1) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4838 | symtable_update_free_vars(st); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4839 | Py_DECREF(st->st_cur); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4840 | end = PyList_GET_SIZE(st->st_stack) - 1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4841 | st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack, |
| 4842 | end); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4843 | if (PySequence_DelItem(st->st_stack, end) < 0) |
| 4844 | return -1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4845 | return 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4846 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4847 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4848 | static void |
| 4849 | symtable_enter_scope(struct symtable *st, char *name, int type, |
| 4850 | int lineno) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4851 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4852 | PySymtableEntryObject *prev = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4853 | |
| 4854 | if (st->st_cur) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4855 | prev = st->st_cur; |
| 4856 | if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) { |
| 4857 | Py_DECREF(st->st_cur); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4858 | st->st_errors++; |
| 4859 | return; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4860 | } |
| 4861 | } |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4862 | st->st_cur = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4863 | PySymtableEntry_New(st, name, type, lineno); |
| 4864 | if (strcmp(name, TOP) == 0) |
| 4865 | st->st_global = st->st_cur->ste_symbols; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4866 | if (prev && st->st_pass == 1) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4867 | if (PyList_Append(prev->ste_children, |
| 4868 | (PyObject *)st->st_cur) < 0) |
| 4869 | st->st_errors++; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4870 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4871 | } |
| 4872 | |
| 4873 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4874 | symtable_lookup(struct symtable *st, char *name) |
| 4875 | { |
| 4876 | char buffer[MANGLE_LEN]; |
| 4877 | PyObject *v; |
| 4878 | int flags; |
| 4879 | |
| 4880 | if (mangle(st->st_private, name, buffer, sizeof(buffer))) |
| 4881 | name = buffer; |
| 4882 | v = PyDict_GetItemString(st->st_cur->ste_symbols, name); |
| 4883 | if (v == NULL) { |
| 4884 | if (PyErr_Occurred()) |
| 4885 | return -1; |
| 4886 | else |
| 4887 | return 0; |
| 4888 | } |
| 4889 | |
| 4890 | flags = PyInt_AS_LONG(v); |
| 4891 | return flags; |
| 4892 | } |
| 4893 | |
| 4894 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4895 | symtable_add_def(struct symtable *st, char *name, int flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4896 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4897 | PyObject *s; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4898 | char buffer[MANGLE_LEN]; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 4899 | int ret; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4900 | |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4901 | if (mangle(st->st_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4902 | name = buffer; |
| 4903 | if ((s = PyString_InternFromString(name)) == NULL) |
| 4904 | return -1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 4905 | ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag); |
| 4906 | Py_DECREF(s); |
| 4907 | return ret; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4908 | } |
| 4909 | |
| 4910 | /* Must only be called with mangled names */ |
| 4911 | |
| 4912 | static int |
| 4913 | symtable_add_def_o(struct symtable *st, PyObject *dict, |
| 4914 | PyObject *name, int flag) |
| 4915 | { |
| 4916 | PyObject *o; |
| 4917 | int val; |
| 4918 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4919 | if ((o = PyDict_GetItem(dict, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4920 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4921 | if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4922 | PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 4923 | PyString_AsString(name)); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 4924 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4925 | st->st_cur->ste_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4926 | return -1; |
| 4927 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4928 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4929 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4930 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4931 | o = PyInt_FromLong(val); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4932 | if (PyDict_SetItem(dict, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4933 | Py_DECREF(o); |
| 4934 | return -1; |
| 4935 | } |
| 4936 | Py_DECREF(o); |
| 4937 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4938 | if (flag & DEF_PARAM) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4939 | if (PyList_Append(st->st_cur->ste_varnames, name) < 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4940 | return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4941 | } else if (flag & DEF_GLOBAL) { |
| 4942 | /* XXX need to update DEF_GLOBAL for other flags too; |
| 4943 | perhaps only DEF_FREE_GLOBAL */ |
| 4944 | if ((o = PyDict_GetItem(st->st_global, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4945 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4946 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4947 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4948 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4949 | o = PyInt_FromLong(val); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4950 | if (PyDict_SetItem(st->st_global, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4951 | Py_DECREF(o); |
| 4952 | return -1; |
| 4953 | } |
| 4954 | Py_DECREF(o); |
| 4955 | } |
| 4956 | return 0; |
| 4957 | } |
| 4958 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4959 | #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4960 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 4961 | /* Look for a yield stmt under n. Return 1 if found, else 0. |
| 4962 | This hack is used to look inside "if 0:" blocks (which are normally |
| 4963 | ignored) in case those are the only places a yield occurs (so that this |
| 4964 | function is a generator). */ |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 4965 | static int |
| 4966 | look_for_yield(node *n) |
| 4967 | { |
| 4968 | int i; |
| 4969 | |
| 4970 | for (i = 0; i < NCH(n); ++i) { |
| 4971 | node *kid = CHILD(n, i); |
| 4972 | |
| 4973 | switch (TYPE(kid)) { |
| 4974 | |
| 4975 | case classdef: |
| 4976 | case funcdef: |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 4977 | case lambdef: |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 4978 | /* Stuff in nested functions and classes can't make |
| 4979 | the parent a generator. */ |
| 4980 | return 0; |
| 4981 | |
| 4982 | case yield_stmt: |
| 4983 | return 1; |
| 4984 | |
| 4985 | default: |
| 4986 | if (look_for_yield(kid)) |
| 4987 | return 1; |
| 4988 | } |
| 4989 | } |
| 4990 | return 0; |
| 4991 | } |
| 4992 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4993 | static void |
| 4994 | symtable_node(struct symtable *st, node *n) |
| 4995 | { |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 4996 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4997 | |
| 4998 | loop: |
| 4999 | switch (TYPE(n)) { |
| 5000 | case funcdef: { |
| 5001 | char *func_name = STR(CHILD(n, 1)); |
| 5002 | symtable_add_def(st, func_name, DEF_LOCAL); |
| 5003 | symtable_default_args(st, CHILD(n, 2)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5004 | symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5005 | symtable_funcdef(st, n); |
| 5006 | symtable_exit_scope(st); |
| 5007 | break; |
| 5008 | } |
| 5009 | case lambdef: |
| 5010 | if (NCH(n) == 4) |
| 5011 | symtable_default_args(st, CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5012 | symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5013 | symtable_funcdef(st, n); |
| 5014 | symtable_exit_scope(st); |
| 5015 | break; |
| 5016 | case classdef: { |
| 5017 | char *tmp, *class_name = STR(CHILD(n, 1)); |
| 5018 | symtable_add_def(st, class_name, DEF_LOCAL); |
| 5019 | if (TYPE(CHILD(n, 2)) == LPAR) { |
| 5020 | node *bases = CHILD(n, 3); |
| 5021 | int i; |
| 5022 | for (i = 0; i < NCH(bases); i += 2) { |
| 5023 | symtable_node(st, CHILD(bases, i)); |
| 5024 | } |
| 5025 | } |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5026 | symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5027 | tmp = st->st_private; |
| 5028 | st->st_private = class_name; |
| 5029 | symtable_node(st, CHILD(n, NCH(n) - 1)); |
| 5030 | st->st_private = tmp; |
| 5031 | symtable_exit_scope(st); |
| 5032 | break; |
| 5033 | } |
| 5034 | case if_stmt: |
| 5035 | for (i = 0; i + 3 < NCH(n); i += 4) { |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5036 | if (is_constant_false(NULL, (CHILD(n, i + 1)))) { |
| 5037 | if (st->st_cur->ste_generator == 0) |
| 5038 | st->st_cur->ste_generator = |
| 5039 | look_for_yield(CHILD(n, i+3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5040 | continue; |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5041 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5042 | symtable_node(st, CHILD(n, i + 1)); |
| 5043 | symtable_node(st, CHILD(n, i + 3)); |
| 5044 | } |
| 5045 | if (i + 2 < NCH(n)) |
| 5046 | symtable_node(st, CHILD(n, i + 2)); |
| 5047 | break; |
| 5048 | case global_stmt: |
| 5049 | symtable_global(st, n); |
| 5050 | break; |
| 5051 | case import_stmt: |
| 5052 | symtable_import(st, n); |
| 5053 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5054 | case exec_stmt: { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5055 | st->st_cur->ste_optimized |= OPT_EXEC; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5056 | symtable_node(st, CHILD(n, 1)); |
| 5057 | if (NCH(n) > 2) |
| 5058 | symtable_node(st, CHILD(n, 3)); |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5059 | else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5060 | st->st_cur->ste_optimized |= OPT_BARE_EXEC; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5061 | st->st_cur->ste_opt_lineno = n->n_lineno; |
| 5062 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5063 | if (NCH(n) > 4) |
| 5064 | symtable_node(st, CHILD(n, 5)); |
| 5065 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5066 | |
| 5067 | } |
Jeremy Hylton | 384639f | 2001-02-19 15:50:51 +0000 | [diff] [blame] | 5068 | case assert_stmt: |
| 5069 | if (Py_OptimizeFlag) |
| 5070 | return; |
| 5071 | if (NCH(n) == 2) { |
| 5072 | n = CHILD(n, 1); |
| 5073 | goto loop; |
| 5074 | } else { |
| 5075 | symtable_node(st, CHILD(n, 1)); |
| 5076 | n = CHILD(n, 3); |
| 5077 | goto loop; |
| 5078 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5079 | case except_clause: |
| 5080 | if (NCH(n) == 4) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5081 | symtable_assign(st, CHILD(n, 3), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5082 | if (NCH(n) > 1) { |
| 5083 | n = CHILD(n, 1); |
| 5084 | goto loop; |
| 5085 | } |
| 5086 | break; |
| 5087 | case del_stmt: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5088 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5089 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 5090 | case yield_stmt: |
| 5091 | st->st_cur->ste_generator = 1; |
| 5092 | n = CHILD(n, 1); |
| 5093 | goto loop; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5094 | case expr_stmt: |
| 5095 | if (NCH(n) == 1) |
| 5096 | n = CHILD(n, 0); |
| 5097 | else { |
| 5098 | if (TYPE(CHILD(n, 1)) == augassign) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5099 | symtable_assign(st, CHILD(n, 0), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5100 | symtable_node(st, CHILD(n, 2)); |
| 5101 | break; |
| 5102 | } else { |
| 5103 | int i; |
| 5104 | for (i = 0; i < NCH(n) - 2; i += 2) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5105 | symtable_assign(st, CHILD(n, i), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5106 | n = CHILD(n, NCH(n) - 1); |
| 5107 | } |
| 5108 | } |
| 5109 | goto loop; |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5110 | case list_iter: |
| 5111 | n = CHILD(n, 0); |
| 5112 | if (TYPE(n) == list_for) { |
| 5113 | st->st_tmpname++; |
| 5114 | symtable_list_comprehension(st, n); |
| 5115 | st->st_tmpname--; |
| 5116 | } else { |
| 5117 | REQ(n, list_if); |
| 5118 | symtable_node(st, CHILD(n, 1)); |
| 5119 | if (NCH(n) == 3) { |
| 5120 | n = CHILD(n, 2); |
| 5121 | goto loop; |
| 5122 | } |
| 5123 | } |
| 5124 | break; |
| 5125 | case for_stmt: |
| 5126 | symtable_assign(st, CHILD(n, 1), 0); |
| 5127 | for (i = 3; i < NCH(n); ++i) |
| 5128 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5129 | symtable_node(st, CHILD(n, i)); |
| 5130 | break; |
| 5131 | /* The remaining cases fall through to default except in |
| 5132 | special circumstances. This requires the individual cases |
| 5133 | to be coded with great care, even though they look like |
| 5134 | rather innocuous. Each case must double-check TYPE(n). |
| 5135 | */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5136 | case argument: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5137 | if (TYPE(n) == argument && NCH(n) == 3) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5138 | n = CHILD(n, 2); |
| 5139 | goto loop; |
| 5140 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5141 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5142 | case listmaker: |
| 5143 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5144 | st->st_tmpname++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5145 | symtable_list_comprehension(st, CHILD(n, 1)); |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5146 | symtable_node(st, CHILD(n, 0)); |
| 5147 | st->st_tmpname--; |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5148 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5149 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5150 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5151 | case atom: |
| 5152 | if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) { |
| 5153 | symtable_add_use(st, STR(CHILD(n, 0))); |
| 5154 | break; |
| 5155 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5156 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5157 | default: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5158 | /* Walk over every non-token child with a special case |
| 5159 | for one child. |
| 5160 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5161 | if (NCH(n) == 1) { |
| 5162 | n = CHILD(n, 0); |
| 5163 | goto loop; |
| 5164 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5165 | for (i = 0; i < NCH(n); ++i) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5166 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5167 | symtable_node(st, CHILD(n, i)); |
| 5168 | } |
| 5169 | } |
| 5170 | |
| 5171 | static void |
| 5172 | symtable_funcdef(struct symtable *st, node *n) |
| 5173 | { |
| 5174 | node *body; |
| 5175 | |
| 5176 | if (TYPE(n) == lambdef) { |
| 5177 | if (NCH(n) == 4) |
| 5178 | symtable_params(st, CHILD(n, 1)); |
| 5179 | } else |
| 5180 | symtable_params(st, CHILD(n, 2)); |
| 5181 | body = CHILD(n, NCH(n) - 1); |
| 5182 | symtable_node(st, body); |
| 5183 | } |
| 5184 | |
| 5185 | /* The next two functions parse the argument tuple. |
| 5186 | symtable_default_arg() checks for names in the default arguments, |
| 5187 | which are references in the defining scope. symtable_params() |
| 5188 | parses the parameter names, which are defined in the function's |
| 5189 | body. |
| 5190 | |
| 5191 | varargslist: |
| 5192 | (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) |
| 5193 | | fpdef ['=' test] (',' fpdef ['=' test])* [','] |
| 5194 | */ |
| 5195 | |
| 5196 | static void |
| 5197 | symtable_default_args(struct symtable *st, node *n) |
| 5198 | { |
| 5199 | node *c; |
| 5200 | int i; |
| 5201 | |
| 5202 | if (TYPE(n) == parameters) { |
| 5203 | n = CHILD(n, 1); |
| 5204 | if (TYPE(n) == RPAR) |
| 5205 | return; |
| 5206 | } |
| 5207 | REQ(n, varargslist); |
| 5208 | for (i = 0; i < NCH(n); i += 2) { |
| 5209 | c = CHILD(n, i); |
| 5210 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5211 | break; |
| 5212 | } |
| 5213 | if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL)) |
| 5214 | symtable_node(st, CHILD(n, i)); |
| 5215 | } |
| 5216 | } |
| 5217 | |
| 5218 | static void |
| 5219 | symtable_params(struct symtable *st, node *n) |
| 5220 | { |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5221 | int i, complex = -1, ext = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5222 | node *c = NULL; |
| 5223 | |
| 5224 | if (TYPE(n) == parameters) { |
| 5225 | n = CHILD(n, 1); |
| 5226 | if (TYPE(n) == RPAR) |
| 5227 | return; |
| 5228 | } |
| 5229 | REQ(n, varargslist); |
| 5230 | for (i = 0; i < NCH(n); i += 2) { |
| 5231 | c = CHILD(n, i); |
| 5232 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5233 | ext = 1; |
| 5234 | break; |
| 5235 | } |
| 5236 | if (TYPE(c) == test) { |
| 5237 | continue; |
| 5238 | } |
| 5239 | if (TYPE(CHILD(c, 0)) == NAME) |
| 5240 | symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM); |
| 5241 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5242 | char nbuf[30]; |
| 5243 | PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5244 | symtable_add_def(st, nbuf, DEF_PARAM); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5245 | complex = i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5246 | } |
| 5247 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5248 | if (ext) { |
| 5249 | c = CHILD(n, i); |
| 5250 | if (TYPE(c) == STAR) { |
| 5251 | i++; |
| 5252 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5253 | DEF_PARAM | DEF_STAR); |
| 5254 | i += 2; |
Jeremy Hylton | 1113cfc | 2001-01-23 00:50:52 +0000 | [diff] [blame] | 5255 | if (i >= NCH(n)) |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5256 | c = NULL; |
| 5257 | else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5258 | c = CHILD(n, i); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5259 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5260 | if (c && TYPE(c) == DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5261 | i++; |
| 5262 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5263 | DEF_PARAM | DEF_DOUBLESTAR); |
| 5264 | } |
| 5265 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5266 | if (complex >= 0) { |
| 5267 | int j; |
| 5268 | for (j = 0; j <= complex; j++) { |
| 5269 | c = CHILD(n, j); |
| 5270 | if (TYPE(c) == COMMA) |
Jeremy Hylton | 2b3f0ca | 2001-02-19 23:52:49 +0000 | [diff] [blame] | 5271 | c = CHILD(n, ++j); |
| 5272 | else if (TYPE(c) == EQUAL) |
| 5273 | c = CHILD(n, j += 3); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5274 | if (TYPE(CHILD(c, 0)) == LPAR) |
| 5275 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5276 | } |
| 5277 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5278 | } |
| 5279 | |
| 5280 | static void |
| 5281 | symtable_params_fplist(struct symtable *st, node *n) |
| 5282 | { |
| 5283 | int i; |
| 5284 | node *c; |
| 5285 | |
| 5286 | REQ(n, fplist); |
| 5287 | for (i = 0; i < NCH(n); i += 2) { |
| 5288 | c = CHILD(n, i); |
| 5289 | REQ(c, fpdef); |
| 5290 | if (NCH(c) == 1) |
| 5291 | symtable_add_def(st, STR(CHILD(c, 0)), |
| 5292 | DEF_PARAM | DEF_INTUPLE); |
| 5293 | else |
| 5294 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5295 | } |
| 5296 | |
| 5297 | } |
| 5298 | |
| 5299 | static void |
| 5300 | symtable_global(struct symtable *st, node *n) |
| 5301 | { |
| 5302 | int i; |
| 5303 | |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 5304 | /* XXX It might be helpful to warn about module-level global |
| 5305 | statements, but it's hard to tell the difference between |
| 5306 | module-level and a string passed to exec. |
| 5307 | */ |
Jeremy Hylton | c176132 | 2001-02-28 23:44:45 +0000 | [diff] [blame] | 5308 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5309 | for (i = 1; i < NCH(n); i += 2) { |
| 5310 | char *name = STR(CHILD(n, i)); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5311 | int flags; |
| 5312 | |
| 5313 | flags = symtable_lookup(st, name); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5314 | if (flags < 0) |
| 5315 | continue; |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5316 | if (flags && flags != DEF_GLOBAL) { |
| 5317 | char buf[500]; |
| 5318 | if (flags & DEF_PARAM) { |
| 5319 | PyErr_Format(PyExc_SyntaxError, |
| 5320 | "name '%.400s' is local and global", |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5321 | name); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5322 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5323 | st->st_cur->ste_lineno); |
| 5324 | st->st_errors++; |
| 5325 | return; |
Guido van Rossum | 0bba7f8 | 2001-02-28 21:55:38 +0000 | [diff] [blame] | 5326 | } |
| 5327 | else { |
| 5328 | if (flags & DEF_LOCAL) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5329 | PyOS_snprintf(buf, sizeof(buf), |
| 5330 | GLOBAL_AFTER_ASSIGN, |
| 5331 | name); |
Guido van Rossum | 0bba7f8 | 2001-02-28 21:55:38 +0000 | [diff] [blame] | 5332 | else |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5333 | PyOS_snprintf(buf, sizeof(buf), |
| 5334 | GLOBAL_AFTER_USE, name); |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 5335 | symtable_warn(st, buf); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5336 | } |
| 5337 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5338 | symtable_add_def(st, name, DEF_GLOBAL); |
| 5339 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5340 | } |
| 5341 | |
| 5342 | static void |
| 5343 | symtable_list_comprehension(struct symtable *st, node *n) |
| 5344 | { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5345 | char tmpname[30]; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5346 | |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5347 | PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", st->st_tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5348 | symtable_add_def(st, tmpname, DEF_LOCAL); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5349 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5350 | symtable_node(st, CHILD(n, 3)); |
| 5351 | if (NCH(n) == 5) |
| 5352 | symtable_node(st, CHILD(n, 4)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5353 | } |
| 5354 | |
| 5355 | static void |
| 5356 | symtable_import(struct symtable *st, node *n) |
| 5357 | { |
| 5358 | int i; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5359 | /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)* |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5360 | | 'from' dotted_name 'import' |
| 5361 | ('*' | import_as_name (',' import_as_name)*) |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5362 | import_as_name: NAME [NAME NAME] |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5363 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5364 | if (STR(CHILD(n, 0))[0] == 'f') { /* from */ |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5365 | node *dotname = CHILD(n, 1); |
| 5366 | if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) { |
| 5367 | /* check for bogus imports */ |
| 5368 | if (n->n_lineno >= st->st_future->ff_last_lineno) { |
| 5369 | PyErr_SetString(PyExc_SyntaxError, |
| 5370 | LATE_FUTURE); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5371 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5372 | n->n_lineno); |
| 5373 | st->st_errors++; |
| 5374 | return; |
| 5375 | } |
| 5376 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5377 | if (TYPE(CHILD(n, 3)) == STAR) { |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 5378 | if (st->st_cur->ste_type != TYPE_MODULE) { |
Jeremy Hylton | 6a53bd8 | 2001-08-06 20:34:25 +0000 | [diff] [blame] | 5379 | if (symtable_warn(st, |
| 5380 | "import * only allowed at module level") < 0) |
| 5381 | return; |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 5382 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5383 | st->st_cur->ste_optimized |= OPT_IMPORT_STAR; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5384 | st->st_cur->ste_opt_lineno = n->n_lineno; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5385 | } else { |
| 5386 | for (i = 3; i < NCH(n); i += 2) { |
| 5387 | node *c = CHILD(n, i); |
| 5388 | if (NCH(c) > 1) /* import as */ |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5389 | symtable_assign(st, CHILD(c, 2), |
| 5390 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5391 | else |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5392 | symtable_assign(st, CHILD(c, 0), |
| 5393 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5394 | } |
| 5395 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5396 | } else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5397 | for (i = 1; i < NCH(n); i += 2) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5398 | symtable_assign(st, CHILD(n, i), DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5399 | } |
| 5400 | } |
| 5401 | } |
| 5402 | |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5403 | /* The third argument to symatble_assign() is a flag to be passed to |
| 5404 | symtable_add_def() if it is eventually called. The flag is useful |
| 5405 | to specify the particular type of assignment that should be |
| 5406 | recorded, e.g. an assignment caused by import. |
| 5407 | */ |
| 5408 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5409 | static void |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5410 | symtable_assign(struct symtable *st, node *n, int def_flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5411 | { |
| 5412 | node *tmp; |
| 5413 | int i; |
| 5414 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5415 | loop: |
| 5416 | switch (TYPE(n)) { |
| 5417 | case lambdef: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5418 | /* invalid assignment, e.g. lambda x:x=2. The next |
| 5419 | pass will catch this error. */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5420 | return; |
| 5421 | case power: |
| 5422 | if (NCH(n) > 2) { |
| 5423 | for (i = 2; i < NCH(n); ++i) |
| 5424 | if (TYPE(CHILD(n, i)) != DOUBLESTAR) |
| 5425 | symtable_node(st, CHILD(n, i)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5426 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5427 | if (NCH(n) > 1) { |
| 5428 | symtable_node(st, CHILD(n, 0)); |
| 5429 | symtable_node(st, CHILD(n, 1)); |
| 5430 | } else { |
| 5431 | n = CHILD(n, 0); |
| 5432 | goto loop; |
| 5433 | } |
| 5434 | return; |
| 5435 | case listmaker: |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5436 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
| 5437 | /* XXX This is an error, but the next pass |
| 5438 | will catch it. */ |
| 5439 | return; |
| 5440 | } else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5441 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5442 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5443 | } |
| 5444 | return; |
| 5445 | case exprlist: |
| 5446 | case testlist: |
| 5447 | if (NCH(n) == 1) { |
| 5448 | n = CHILD(n, 0); |
| 5449 | goto loop; |
| 5450 | } |
| 5451 | else { |
| 5452 | int i; |
| 5453 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5454 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5455 | return; |
| 5456 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5457 | case atom: |
| 5458 | tmp = CHILD(n, 0); |
| 5459 | if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) { |
| 5460 | n = CHILD(n, 1); |
| 5461 | goto loop; |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 5462 | } else if (TYPE(tmp) == NAME) { |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 5463 | if (strcmp(STR(tmp), "__debug__") == 0) { |
| 5464 | PyErr_SetString(PyExc_SyntaxError, |
| 5465 | ASSIGN_DEBUG); |
| 5466 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | 86424e3 | 2001-12-04 02:41:46 +0000 | [diff] [blame] | 5467 | n->n_lineno); |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 5468 | st->st_errors++; |
| 5469 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5470 | symtable_add_def(st, STR(tmp), DEF_LOCAL | def_flag); |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 5471 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5472 | return; |
| 5473 | case dotted_as_name: |
| 5474 | if (NCH(n) == 3) |
| 5475 | symtable_add_def(st, STR(CHILD(n, 2)), |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5476 | DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5477 | else |
| 5478 | symtable_add_def(st, |
| 5479 | STR(CHILD(CHILD(n, |
| 5480 | 0), 0)), |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5481 | DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5482 | return; |
| 5483 | case dotted_name: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5484 | symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5485 | return; |
| 5486 | case NAME: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5487 | symtable_add_def(st, STR(n), DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5488 | return; |
| 5489 | default: |
| 5490 | if (NCH(n) == 0) |
| 5491 | return; |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 5492 | if (NCH(n) == 1) { |
| 5493 | n = CHILD(n, 0); |
| 5494 | goto loop; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5495 | } |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 5496 | /* Should only occur for errors like x + 1 = 1, |
| 5497 | which will be caught in the next pass. */ |
| 5498 | for (i = 0; i < NCH(n); ++i) |
| 5499 | if (TYPE(CHILD(n, i)) >= single_input) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5500 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5501 | } |
| 5502 | } |