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); |
Fred Drake | 615ae55 | 2000-06-30 16:20:13 +0000 | [diff] [blame] | 123 | sprintf(buf, "<code object %.100s at %p, file \"%.300s\", line %d>", |
| 124 | name, co, filename, lineno); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 125 | return PyString_FromString(buf); |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 128 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 129 | code_compare(PyCodeObject *co, PyCodeObject *cp) |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 130 | { |
| 131 | int cmp; |
Guido van Rossum | 4467959 | 2000-04-10 16:20:31 +0000 | [diff] [blame] | 132 | cmp = PyObject_Compare(co->co_name, cp->co_name); |
| 133 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 134 | cmp = co->co_argcount - cp->co_argcount; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 135 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 136 | cmp = co->co_nlocals - cp->co_nlocals; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 137 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 138 | cmp = co->co_flags - cp->co_flags; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 139 | if (cmp) return cmp; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 140 | cmp = PyObject_Compare(co->co_code, cp->co_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 141 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 142 | cmp = PyObject_Compare(co->co_consts, cp->co_consts); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 143 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 144 | cmp = PyObject_Compare(co->co_names, cp->co_names); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 145 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 146 | cmp = PyObject_Compare(co->co_varnames, cp->co_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 147 | if (cmp) return cmp; |
| 148 | cmp = PyObject_Compare(co->co_freevars, cp->co_freevars); |
| 149 | if (cmp) return cmp; |
| 150 | cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 151 | return cmp; |
| 152 | } |
| 153 | |
| 154 | static long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 155 | code_hash(PyCodeObject *co) |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 156 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 157 | long h, h0, h1, h2, h3, h4, h5, h6; |
Guido van Rossum | 4467959 | 2000-04-10 16:20:31 +0000 | [diff] [blame] | 158 | h0 = PyObject_Hash(co->co_name); |
| 159 | if (h0 == -1) return -1; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 160 | h1 = PyObject_Hash(co->co_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 161 | if (h1 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 162 | h2 = PyObject_Hash(co->co_consts); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 163 | if (h2 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 164 | h3 = PyObject_Hash(co->co_names); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 165 | if (h3 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 166 | h4 = PyObject_Hash(co->co_varnames); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 167 | if (h4 == -1) return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 168 | h5 = PyObject_Hash(co->co_freevars); |
| 169 | if (h5 == -1) return -1; |
| 170 | h6 = PyObject_Hash(co->co_cellvars); |
| 171 | if (h6 == -1) return -1; |
| 172 | h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 173 | co->co_argcount ^ co->co_nlocals ^ co->co_flags; |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 174 | if (h == -1) h = -2; |
| 175 | return h; |
| 176 | } |
| 177 | |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 178 | /* XXX code objects need to participate in GC? */ |
| 179 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 180 | PyTypeObject PyCode_Type = { |
| 181 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 182 | 0, |
| 183 | "code", |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 184 | sizeof(PyCodeObject), |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 185 | 0, |
Jeremy Hylton | c785f48 | 2001-09-14 20:08:07 +0000 | [diff] [blame] | 186 | (destructor)code_dealloc, /* tp_dealloc */ |
| 187 | 0, /* tp_print */ |
| 188 | 0, /* tp_getattr */ |
| 189 | 0, /* tp_setattr */ |
| 190 | (cmpfunc)code_compare, /* tp_compare */ |
| 191 | (reprfunc)code_repr, /* tp_repr */ |
| 192 | 0, /* tp_as_number */ |
| 193 | 0, /* tp_as_sequence */ |
| 194 | 0, /* tp_as_mapping */ |
| 195 | (hashfunc)code_hash, /* tp_hash */ |
| 196 | 0, /* tp_call */ |
| 197 | 0, /* tp_str */ |
| 198 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 199 | 0, /* tp_setattro */ |
| 200 | 0, /* tp_as_buffer */ |
| 201 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 202 | 0, /* tp_doc */ |
| 203 | 0, /* tp_traverse */ |
| 204 | 0, /* tp_clear */ |
| 205 | 0, /* tp_richcompare */ |
| 206 | 0, /* tp_weaklistoffset */ |
| 207 | 0, /* tp_iter */ |
| 208 | 0, /* tp_iternext */ |
| 209 | 0, /* tp_methods */ |
| 210 | code_memberlist, /* tp_members */ |
| 211 | 0, /* tp_getset */ |
| 212 | 0, /* tp_base */ |
| 213 | 0, /* tp_dict */ |
| 214 | 0, /* tp_descr_get */ |
| 215 | 0, /* tp_descr_set */ |
| 216 | 0, /* tp_dictoffset */ |
| 217 | 0, /* tp_init */ |
| 218 | 0, /* tp_alloc */ |
| 219 | 0, /* tp_new */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 220 | }; |
| 221 | |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 222 | #define NAME_CHARS \ |
| 223 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" |
| 224 | |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 225 | /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */ |
| 226 | |
| 227 | static int |
| 228 | all_name_chars(unsigned char *s) |
| 229 | { |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 230 | static char ok_name_char[256]; |
| 231 | static unsigned char *name_chars = (unsigned char *)NAME_CHARS; |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 232 | |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 233 | if (ok_name_char[*name_chars] == 0) { |
| 234 | unsigned char *p; |
| 235 | for (p = name_chars; *p; p++) |
| 236 | ok_name_char[*p] = 1; |
| 237 | } |
| 238 | while (*s) { |
| 239 | if (ok_name_char[*s++] == 0) |
| 240 | return 0; |
| 241 | } |
| 242 | return 1; |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 245 | static int |
| 246 | intern_strings(PyObject *tuple) |
| 247 | { |
| 248 | int i; |
| 249 | |
| 250 | for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { |
| 251 | PyObject *v = PyTuple_GET_ITEM(tuple, i); |
| 252 | if (v == NULL || !PyString_Check(v)) { |
| 253 | Py_FatalError("non-string found in code slot"); |
| 254 | PyErr_BadInternalCall(); |
| 255 | return -1; |
| 256 | } |
| 257 | PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i)); |
| 258 | } |
| 259 | return 0; |
| 260 | } |
| 261 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 262 | PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 263 | PyCode_New(int argcount, int nlocals, int stacksize, int flags, |
| 264 | PyObject *code, PyObject *consts, PyObject *names, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 265 | PyObject *varnames, PyObject *freevars, PyObject *cellvars, |
| 266 | PyObject *filename, PyObject *name, int firstlineno, |
| 267 | PyObject *lnotab) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 268 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 269 | PyCodeObject *co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 270 | int i; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 271 | PyBufferProcs *pb; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 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) || |
| 282 | lnotab == NULL || !PyString_Check(lnotab)) { |
| 283 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 284 | return NULL; |
| 285 | } |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 286 | pb = code->ob_type->tp_as_buffer; |
| 287 | if (pb == NULL || |
| 288 | pb->bf_getreadbuffer == NULL || |
| 289 | pb->bf_getsegcount == NULL || |
| 290 | (*pb->bf_getsegcount)(code, NULL) != 1) |
| 291 | { |
| 292 | PyErr_BadInternalCall(); |
| 293 | return NULL; |
| 294 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 295 | intern_strings(names); |
| 296 | intern_strings(varnames); |
| 297 | if (freevars == NULL) |
| 298 | freevars = PyTuple_New(0); |
| 299 | intern_strings(freevars); |
| 300 | if (cellvars == NULL) |
| 301 | cellvars = PyTuple_New(0); |
| 302 | intern_strings(cellvars); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 303 | /* Intern selected string constants */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 304 | for (i = PyTuple_Size(consts); --i >= 0; ) { |
| 305 | PyObject *v = PyTuple_GetItem(consts, i); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 306 | if (!PyString_Check(v)) |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 307 | continue; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 308 | if (!all_name_chars((unsigned char *)PyString_AS_STRING(v))) |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 309 | continue; |
| 310 | PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 311 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 312 | co = PyObject_NEW(PyCodeObject, &PyCode_Type); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 313 | if (co != NULL) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 314 | co->co_argcount = argcount; |
| 315 | co->co_nlocals = nlocals; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 316 | co->co_stacksize = stacksize; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 317 | co->co_flags = flags; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 318 | Py_INCREF(code); |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 319 | co->co_code = code; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 320 | Py_INCREF(consts); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 321 | co->co_consts = consts; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 322 | Py_INCREF(names); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 323 | co->co_names = names; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 324 | Py_INCREF(varnames); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 325 | co->co_varnames = varnames; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 326 | Py_INCREF(freevars); |
| 327 | co->co_freevars = freevars; |
| 328 | Py_INCREF(cellvars); |
| 329 | co->co_cellvars = cellvars; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 330 | Py_INCREF(filename); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 331 | co->co_filename = filename; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 332 | Py_INCREF(name); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 333 | co->co_name = name; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 334 | co->co_firstlineno = firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 335 | Py_INCREF(lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 336 | co->co_lnotab = lnotab; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 337 | } |
| 338 | return co; |
| 339 | } |
| 340 | |
| 341 | |
| 342 | /* Data structure used internally */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 343 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 344 | /* The compiler uses two passes to generate bytecodes. The first pass |
| 345 | builds the symbol table. The second pass generates the bytecode. |
| 346 | |
| 347 | The first pass uses a single symtable struct. The second pass uses |
| 348 | a compiling struct for each code block. The compiling structs |
| 349 | share a reference to the symtable. |
| 350 | |
| 351 | The two passes communicate via symtable_load_symbols() and via |
| 352 | is_local() and is_global(). The former initializes several slots |
| 353 | in the compiling struct: c_varnames, c_locals, c_nlocals, |
| 354 | c_argcount, c_globals, and c_flags. |
| 355 | */ |
| 356 | |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 357 | /* All about c_lnotab. |
| 358 | |
| 359 | c_lnotab is an array of unsigned bytes disguised as a Python string. In -O |
| 360 | mode, SET_LINENO opcodes aren't generated, and bytecode offsets are mapped |
| 361 | to source code line #s (when needed for tracebacks) via c_lnotab instead. |
| 362 | The array is conceptually a list of |
| 363 | (bytecode offset increment, line number increment) |
| 364 | pairs. The details are important and delicate, best illustrated by example: |
| 365 | |
| 366 | byte code offset source code line number |
| 367 | 0 1 |
| 368 | 6 2 |
| 369 | 50 7 |
| 370 | 350 307 |
| 371 | 361 308 |
| 372 | |
| 373 | The first trick is that these numbers aren't stored, only the increments |
| 374 | from one row to the next (this doesn't really work, but it's a start): |
| 375 | |
| 376 | 0, 1, 6, 1, 44, 5, 300, 300, 11, 1 |
| 377 | |
| 378 | The second trick is that an unsigned byte can't hold negative values, or |
| 379 | values larger than 255, so (a) there's a deep assumption that byte code |
| 380 | offsets and their corresponding line #s both increase monotonically, and (b) |
| 381 | if at least one column jumps by more than 255 from one row to the next, more |
| 382 | than one pair is written to the table. In case #b, there's no way to know |
| 383 | from looking at the table later how many were written. That's the delicate |
| 384 | part. A user of c_lnotab desiring to find the source line number |
| 385 | corresponding to a bytecode address A should do something like this |
| 386 | |
| 387 | lineno = addr = 0 |
| 388 | for addr_incr, line_incr in c_lnotab: |
| 389 | addr += addr_incr |
| 390 | if addr > A: |
| 391 | return lineno |
| 392 | lineno += line_incr |
| 393 | |
| 394 | In order for this to work, when the addr field increments by more than 255, |
| 395 | the line # increment in each pair generated must be 0 until the remaining addr |
| 396 | increment is < 256. So, in the example above, com_set_lineno should not (as |
| 397 | was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to |
| 398 | 255, 0, 45, 255, 0, 45. |
| 399 | */ |
| 400 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 401 | struct compiling { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 402 | PyObject *c_code; /* string */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 403 | PyObject *c_consts; /* list of objects */ |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 404 | PyObject *c_const_dict; /* inverse of c_consts */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 405 | PyObject *c_names; /* list of strings (names) */ |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 406 | PyObject *c_name_dict; /* inverse of c_names */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 407 | PyObject *c_globals; /* dictionary (value=None) */ |
| 408 | PyObject *c_locals; /* dictionary (value=localID) */ |
| 409 | PyObject *c_varnames; /* list (inverse of c_locals) */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 410 | PyObject *c_freevars; /* dictionary (value=None) */ |
| 411 | PyObject *c_cellvars; /* list */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 412 | int c_nlocals; /* index of next local */ |
| 413 | int c_argcount; /* number of top-level arguments */ |
| 414 | int c_flags; /* same as co_flags */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 415 | int c_nexti; /* index into c_code */ |
| 416 | int c_errors; /* counts errors occurred */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 417 | int c_infunction; /* set when compiling a function */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 418 | int c_interactive; /* generating code for interactive command */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 419 | int c_loops; /* counts nested loops */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 420 | int c_begin; /* begin of current loop, for 'continue' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 421 | int c_block[CO_MAXBLOCKS]; /* stack of block types */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 422 | int c_nblocks; /* current block stack level */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 423 | char *c_filename; /* filename of current node */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 424 | char *c_name; /* name of object (e.g. function) */ |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 425 | int c_lineno; /* Current line number */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 426 | int c_stacklevel; /* Current stack level */ |
| 427 | int c_maxstacklevel; /* Maximum stack level */ |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 428 | int c_firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 429 | PyObject *c_lnotab; /* Table mapping address to line number */ |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 430 | int c_last_addr, c_last_line, c_lnotab_next; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 431 | char *c_private; /* for private name mangling */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 432 | int c_tmpname; /* temporary local name counter */ |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 433 | int c_nested; /* Is block nested funcdef or lamdef? */ |
| 434 | int c_closure; /* Is nested w/freevars? */ |
| 435 | struct symtable *c_symtable; /* pointer to module symbol table */ |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 436 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 437 | }; |
| 438 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 439 | static int |
| 440 | is_free(int v) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 441 | { |
| 442 | if ((v & (USE | DEF_FREE)) |
| 443 | && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL))) |
| 444 | return 1; |
| 445 | if (v & DEF_FREE_CLASS) |
| 446 | return 1; |
| 447 | return 0; |
| 448 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 449 | |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 450 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 451 | com_error(struct compiling *c, PyObject *exc, char *msg) |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 452 | { |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 453 | PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL; |
| 454 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 455 | if (c == NULL) { |
| 456 | /* Error occurred via symtable call to |
| 457 | is_constant_false */ |
| 458 | PyErr_SetString(exc, msg); |
| 459 | return; |
| 460 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 461 | c->c_errors++; |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 462 | if (c->c_lineno < 1 || c->c_interactive) { |
| 463 | /* Unknown line number or interactive input */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 464 | PyErr_SetString(exc, msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 465 | return; |
| 466 | } |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 467 | v = PyString_FromString(msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 468 | if (v == NULL) |
| 469 | return; /* MemoryError, too bad */ |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 470 | |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 471 | line = PyErr_ProgramText(c->c_filename, c->c_lineno); |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 472 | if (line == NULL) { |
| 473 | Py_INCREF(Py_None); |
| 474 | line = Py_None; |
| 475 | } |
| 476 | t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno, |
| 477 | Py_None, line); |
| 478 | if (t == NULL) |
| 479 | goto exit; |
| 480 | w = Py_BuildValue("(OO)", v, t); |
| 481 | if (w == NULL) |
| 482 | goto exit; |
| 483 | PyErr_SetObject(exc, w); |
| 484 | exit: |
| 485 | Py_XDECREF(t); |
| 486 | Py_XDECREF(v); |
| 487 | Py_XDECREF(w); |
| 488 | Py_XDECREF(line); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 491 | /* Interface to the block stack */ |
| 492 | |
| 493 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 494 | block_push(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 495 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 496 | if (c->c_nblocks >= CO_MAXBLOCKS) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 497 | com_error(c, PyExc_SystemError, |
| 498 | "too many statically nested blocks"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 499 | } |
| 500 | else { |
| 501 | c->c_block[c->c_nblocks++] = type; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 506 | block_pop(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 507 | { |
| 508 | if (c->c_nblocks > 0) |
| 509 | c->c_nblocks--; |
| 510 | 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] | 511 | com_error(c, PyExc_SystemError, "bad block pop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 515 | /* Prototype forward declarations */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 516 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 517 | static int com_init(struct compiling *, char *); |
| 518 | static void com_free(struct compiling *); |
| 519 | static void com_push(struct compiling *, int); |
| 520 | static void com_pop(struct compiling *, int); |
| 521 | static void com_done(struct compiling *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 522 | static void com_node(struct compiling *, node *); |
| 523 | static void com_factor(struct compiling *, node *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 524 | static void com_addbyte(struct compiling *, int); |
| 525 | static void com_addint(struct compiling *, int); |
| 526 | static void com_addoparg(struct compiling *, int, int); |
| 527 | static void com_addfwref(struct compiling *, int, int *); |
| 528 | static void com_backpatch(struct compiling *, int); |
| 529 | static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *); |
| 530 | static int com_addconst(struct compiling *, PyObject *); |
| 531 | static int com_addname(struct compiling *, PyObject *); |
| 532 | static void com_addopname(struct compiling *, int, node *); |
| 533 | static void com_list(struct compiling *, node *, int); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 534 | static void com_list_iter(struct compiling *, node *, node *, char *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 535 | static int com_argdefs(struct compiling *, node *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 536 | static void com_assign(struct compiling *, node *, int, node *); |
| 537 | static void com_assign_name(struct compiling *, node *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 538 | static PyCodeObject *icompile(node *, struct compiling *); |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 539 | static PyCodeObject *jcompile(node *, char *, struct compiling *, |
| 540 | PyCompilerFlags *); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 541 | static PyObject *parsestrplus(struct compiling*, node *); |
| 542 | static PyObject *parsestr(struct compiling *, char *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 543 | static node *get_rawdocstring(node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 544 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 545 | static int get_ref_type(struct compiling *, char *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 546 | |
| 547 | /* symtable operations */ |
| 548 | static int symtable_build(struct compiling *, node *); |
| 549 | static int symtable_load_symbols(struct compiling *); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 550 | static struct symtable *symtable_init(void); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 551 | static void symtable_enter_scope(struct symtable *, char *, int, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 552 | static int symtable_exit_scope(struct symtable *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 553 | static int symtable_add_def(struct symtable *, char *, int); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 554 | static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 555 | |
| 556 | static void symtable_node(struct symtable *, node *); |
| 557 | static void symtable_funcdef(struct symtable *, node *); |
| 558 | static void symtable_default_args(struct symtable *, node *); |
| 559 | static void symtable_params(struct symtable *, node *); |
| 560 | static void symtable_params_fplist(struct symtable *, node *n); |
| 561 | static void symtable_global(struct symtable *, node *); |
| 562 | static void symtable_import(struct symtable *, node *); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 563 | static void symtable_assign(struct symtable *, node *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 564 | static void symtable_list_comprehension(struct symtable *, node *); |
| 565 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 566 | static int symtable_update_free_vars(struct symtable *); |
| 567 | static int symtable_undo_free(struct symtable *, PyObject *, PyObject *); |
| 568 | static int symtable_check_global(struct symtable *, PyObject *, PyObject *); |
| 569 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 570 | /* helper */ |
| 571 | static void |
| 572 | do_pad(int pad) |
| 573 | { |
| 574 | int i; |
| 575 | for (i = 0; i < pad; ++i) |
| 576 | fprintf(stderr, " "); |
| 577 | } |
| 578 | |
| 579 | static void |
| 580 | dump(node *n, int pad, int depth) |
| 581 | { |
| 582 | int i; |
| 583 | if (depth == 0) |
| 584 | return; |
| 585 | do_pad(pad); |
| 586 | fprintf(stderr, "%d: %s\n", TYPE(n), STR(n)); |
| 587 | if (depth > 0) |
| 588 | depth--; |
| 589 | for (i = 0; i < NCH(n); ++i) |
| 590 | dump(CHILD(n, i), pad + 1, depth); |
| 591 | } |
| 592 | |
| 593 | #define DUMP(N) dump(N, 0, -1) |
| 594 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 595 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 596 | com_init(struct compiling *c, char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 597 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 598 | memset((void *)c, '\0', sizeof(struct compiling)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 599 | if ((c->c_code = PyString_FromStringAndSize((char *)NULL, |
| 600 | 1000)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 601 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 602 | if ((c->c_consts = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 603 | goto fail; |
| 604 | if ((c->c_const_dict = PyDict_New()) == NULL) |
| 605 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 606 | if ((c->c_names = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 607 | goto fail; |
| 608 | if ((c->c_name_dict = PyDict_New()) == NULL) |
| 609 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 610 | if ((c->c_locals = PyDict_New()) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 611 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 612 | if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL, |
| 613 | 1000)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 614 | goto fail; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 615 | c->c_globals = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 616 | c->c_varnames = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 617 | c->c_freevars = NULL; |
| 618 | c->c_cellvars = NULL; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 619 | c->c_nlocals = 0; |
| 620 | c->c_argcount = 0; |
| 621 | c->c_flags = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 622 | c->c_nexti = 0; |
| 623 | c->c_errors = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 624 | c->c_infunction = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 625 | c->c_interactive = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 626 | c->c_loops = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 627 | c->c_begin = 0; |
| 628 | c->c_nblocks = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 629 | c->c_filename = filename; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 630 | c->c_name = "?"; |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 631 | c->c_lineno = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 632 | c->c_stacklevel = 0; |
| 633 | c->c_maxstacklevel = 0; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 634 | c->c_firstlineno = 0; |
| 635 | c->c_last_addr = 0; |
| 636 | c->c_last_line = 0; |
Barry Warsaw | 174e801 | 2001-01-22 04:35:57 +0000 | [diff] [blame] | 637 | c->c_lnotab_next = 0; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 638 | c->c_tmpname = 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 639 | c->c_nested = 0; |
| 640 | c->c_closure = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 641 | c->c_symtable = NULL; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 642 | return 1; |
| 643 | |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 644 | fail: |
| 645 | com_free(c); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 650 | com_free(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 651 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 652 | Py_XDECREF(c->c_code); |
| 653 | Py_XDECREF(c->c_consts); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 654 | Py_XDECREF(c->c_const_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 655 | Py_XDECREF(c->c_names); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 656 | Py_XDECREF(c->c_name_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 657 | Py_XDECREF(c->c_globals); |
| 658 | Py_XDECREF(c->c_locals); |
| 659 | Py_XDECREF(c->c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 660 | Py_XDECREF(c->c_freevars); |
| 661 | Py_XDECREF(c->c_cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 662 | Py_XDECREF(c->c_lnotab); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 663 | if (c->c_future) |
| 664 | PyMem_Free((void *)c->c_future); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 668 | com_push(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 669 | { |
| 670 | c->c_stacklevel += n; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 671 | if (c->c_stacklevel > c->c_maxstacklevel) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 672 | c->c_maxstacklevel = c->c_stacklevel; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 673 | /* |
| 674 | fprintf(stderr, "%s:%s:%d max stack nexti=%d level=%d n=%d\n", |
| 675 | c->c_filename, c->c_name, c->c_lineno, |
| 676 | c->c_nexti, c->c_stacklevel, n); |
| 677 | */ |
| 678 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 682 | com_pop(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 683 | { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 684 | if (c->c_stacklevel < n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 685 | c->c_stacklevel = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 686 | else |
| 687 | c->c_stacklevel -= n; |
| 688 | } |
| 689 | |
| 690 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 691 | com_done(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 692 | { |
| 693 | if (c->c_code != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 694 | _PyString_Resize(&c->c_code, c->c_nexti); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 695 | if (c->c_lnotab != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 696 | _PyString_Resize(&c->c_lnotab, c->c_lnotab_next); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 699 | static int |
| 700 | com_check_size(PyObject **s, int offset) |
| 701 | { |
| 702 | int len = PyString_GET_SIZE(*s); |
| 703 | if (offset >= len) |
| 704 | return _PyString_Resize(s, len * 2); |
| 705 | return 0; |
| 706 | } |
| 707 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 708 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 709 | com_addbyte(struct compiling *c, int byte) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 710 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 711 | /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 712 | assert(byte >= 0 && byte <= 255); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 713 | assert(c->c_code); |
| 714 | if (com_check_size(&c->c_code, c->c_nexti)) { |
| 715 | c->c_errors++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 716 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 717 | } |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 718 | PyString_AS_STRING(c->c_code)[c->c_nexti++] = byte; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 722 | com_addint(struct compiling *c, int x) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 723 | { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 724 | com_addbyte(c, x & 0xff); |
| 725 | com_addbyte(c, x >> 8); /* XXX x should be positive */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 729 | com_add_lnotab(struct compiling *c, int addr, int line) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 730 | { |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 731 | char *p; |
| 732 | if (c->c_lnotab == NULL) |
| 733 | return; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 734 | if (com_check_size(&c->c_lnotab, c->c_lnotab_next + 2)) { |
| 735 | c->c_errors++; |
| 736 | return; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 737 | } |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 738 | p = PyString_AS_STRING(c->c_lnotab) + c->c_lnotab_next; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 739 | *p++ = addr; |
| 740 | *p++ = line; |
| 741 | c->c_lnotab_next += 2; |
| 742 | } |
| 743 | |
| 744 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 745 | com_set_lineno(struct compiling *c, int lineno) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 746 | { |
| 747 | c->c_lineno = lineno; |
| 748 | if (c->c_firstlineno == 0) { |
| 749 | c->c_firstlineno = c->c_last_line = lineno; |
| 750 | } |
| 751 | else { |
| 752 | int incr_addr = c->c_nexti - c->c_last_addr; |
| 753 | int incr_line = lineno - c->c_last_line; |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 754 | while (incr_addr > 255) { |
| 755 | com_add_lnotab(c, 255, 0); |
| 756 | incr_addr -= 255; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 757 | } |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 758 | while (incr_line > 255) { |
| 759 | com_add_lnotab(c, incr_addr, 255); |
| 760 | incr_line -=255; |
| 761 | incr_addr = 0; |
| 762 | } |
| 763 | if (incr_addr > 0 || incr_line > 0) |
| 764 | com_add_lnotab(c, incr_addr, incr_line); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 765 | c->c_last_addr = c->c_nexti; |
| 766 | c->c_last_line = lineno; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 771 | com_addoparg(struct compiling *c, int op, int arg) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 772 | { |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 773 | int extended_arg = arg >> 16; |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 774 | if (op == SET_LINENO) { |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 775 | com_set_lineno(c, arg); |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 776 | if (Py_OptimizeFlag) |
| 777 | return; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 778 | } |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 779 | if (extended_arg){ |
| 780 | com_addbyte(c, EXTENDED_ARG); |
| 781 | com_addint(c, extended_arg); |
| 782 | arg &= 0xffff; |
| 783 | } |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 784 | com_addbyte(c, op); |
| 785 | com_addint(c, arg); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 789 | com_addfwref(struct compiling *c, int op, int *p_anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 790 | { |
| 791 | /* Compile a forward reference for backpatching */ |
| 792 | int here; |
| 793 | int anchor; |
| 794 | com_addbyte(c, op); |
| 795 | here = c->c_nexti; |
| 796 | anchor = *p_anchor; |
| 797 | *p_anchor = here; |
| 798 | com_addint(c, anchor == 0 ? 0 : here - anchor); |
| 799 | } |
| 800 | |
| 801 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 802 | com_backpatch(struct compiling *c, int anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 803 | { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 804 | unsigned char *code = (unsigned char *) PyString_AS_STRING(c->c_code); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 805 | int target = c->c_nexti; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 806 | int dist; |
| 807 | int prev; |
| 808 | for (;;) { |
| 809 | /* Make the JUMP instruction at anchor point to target */ |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 810 | prev = code[anchor] + (code[anchor+1] << 8); |
| 811 | dist = target - (anchor+2); |
| 812 | code[anchor] = dist & 0xff; |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 813 | dist >>= 8; |
| 814 | code[anchor+1] = dist; |
| 815 | dist >>= 8; |
| 816 | if (dist) { |
| 817 | com_error(c, PyExc_SystemError, |
| 818 | "com_backpatch: offset too large"); |
| 819 | break; |
| 820 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 821 | if (!prev) |
| 822 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 823 | anchor -= prev; |
| 824 | } |
| 825 | } |
| 826 | |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 827 | /* Handle literals and names uniformly */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 828 | |
| 829 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 830 | com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 831 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 832 | PyObject *w, *t, *np=NULL; |
| 833 | long n; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 834 | |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 835 | t = Py_BuildValue("(OO)", v, v->ob_type); |
| 836 | if (t == NULL) |
| 837 | goto fail; |
| 838 | w = PyDict_GetItem(dict, t); |
| 839 | if (w != NULL) { |
| 840 | n = PyInt_AsLong(w); |
| 841 | } else { |
| 842 | n = PyList_Size(list); |
| 843 | np = PyInt_FromLong(n); |
| 844 | if (np == NULL) |
| 845 | goto fail; |
| 846 | if (PyList_Append(list, v) != 0) |
| 847 | goto fail; |
| 848 | if (PyDict_SetItem(dict, t, np) != 0) |
| 849 | goto fail; |
| 850 | Py_DECREF(np); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 851 | } |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 852 | Py_DECREF(t); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 853 | return n; |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 854 | fail: |
| 855 | Py_XDECREF(np); |
| 856 | Py_XDECREF(t); |
| 857 | c->c_errors++; |
| 858 | return 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 862 | com_addconst(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 863 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 864 | 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] | 865 | } |
| 866 | |
| 867 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 868 | com_addname(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 869 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 870 | 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] | 871 | } |
| 872 | |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 873 | static int |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 874 | mangle(char *p, char *name, char *buffer, size_t maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 875 | { |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 876 | /* Name mangling: __private becomes _classname__private. |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 877 | This is independent from how the name is used. */ |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 878 | size_t nlen, plen; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 879 | if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_') |
| 880 | return 0; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 881 | nlen = strlen(name); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 882 | if (nlen+2 >= maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 883 | return 0; /* Don't mangle __extremely_long_names */ |
| 884 | if (name[nlen-1] == '_' && name[nlen-2] == '_') |
| 885 | return 0; /* Don't mangle __whatever__ */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 886 | /* Strip leading underscores from class name */ |
| 887 | while (*p == '_') |
| 888 | p++; |
| 889 | if (*p == '\0') |
| 890 | return 0; /* Don't mangle if class is just underscores */ |
| 891 | plen = strlen(p); |
| 892 | if (plen + nlen >= maxlen) |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 893 | plen = maxlen-nlen-2; /* Truncate class name if too long */ |
| 894 | /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 895 | buffer[0] = '_'; |
| 896 | strncpy(buffer+1, p, plen); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 897 | strcpy(buffer+1+plen, name); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 898 | return 1; |
| 899 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 900 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 901 | static void |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 902 | com_addop_name(struct compiling *c, int op, char *name) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 903 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 904 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 905 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 906 | char buffer[MANGLE_LEN]; |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 907 | |
| 908 | if (mangle(c->c_private, name, buffer, sizeof(buffer))) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 909 | name = buffer; |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 910 | if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 911 | c->c_errors++; |
| 912 | i = 255; |
| 913 | } |
| 914 | else { |
| 915 | i = com_addname(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 916 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 917 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 918 | com_addoparg(c, op, i); |
| 919 | } |
| 920 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 921 | #define NAME_LOCAL 0 |
| 922 | #define NAME_GLOBAL 1 |
| 923 | #define NAME_DEFAULT 2 |
| 924 | #define NAME_CLOSURE 3 |
| 925 | |
| 926 | static int |
| 927 | com_lookup_arg(PyObject *dict, PyObject *name) |
| 928 | { |
| 929 | PyObject *v = PyDict_GetItem(dict, name); |
| 930 | if (v == NULL) |
| 931 | return -1; |
| 932 | else |
| 933 | return PyInt_AS_LONG(v); |
| 934 | } |
| 935 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 936 | static void |
| 937 | com_addop_varname(struct compiling *c, int kind, char *name) |
| 938 | { |
| 939 | PyObject *v; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 940 | int i, reftype; |
| 941 | int scope = NAME_DEFAULT; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 942 | int op = STOP_CODE; |
| 943 | char buffer[MANGLE_LEN]; |
| 944 | |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 945 | if (mangle(c->c_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 946 | name = buffer; |
| 947 | if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { |
| 948 | c->c_errors++; |
| 949 | i = 255; |
| 950 | goto done; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 951 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 952 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 953 | reftype = get_ref_type(c, name); |
| 954 | switch (reftype) { |
| 955 | case LOCAL: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 956 | if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 957 | scope = NAME_LOCAL; |
| 958 | break; |
| 959 | case GLOBAL_EXPLICIT: |
| 960 | scope = NAME_GLOBAL; |
| 961 | break; |
| 962 | case GLOBAL_IMPLICIT: |
| 963 | if (c->c_flags & CO_OPTIMIZED) |
| 964 | scope = NAME_GLOBAL; |
| 965 | break; |
| 966 | case FREE: |
| 967 | case CELL: |
| 968 | scope = NAME_CLOSURE; |
| 969 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | i = com_addname(c, v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 973 | if (scope == NAME_LOCAL) |
| 974 | i = com_lookup_arg(c->c_locals, v); |
| 975 | else if (reftype == FREE) |
| 976 | i = com_lookup_arg(c->c_freevars, v); |
| 977 | else if (reftype == CELL) |
| 978 | i = com_lookup_arg(c->c_cellvars, v); |
| 979 | if (i == -1) { |
| 980 | c->c_errors++; /* XXX no exception set */ |
| 981 | i = 255; |
| 982 | goto done; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 983 | } |
| 984 | Py_DECREF(v); |
| 985 | |
| 986 | switch (kind) { |
| 987 | case VAR_LOAD: |
| 988 | switch (scope) { |
| 989 | case NAME_LOCAL: |
| 990 | op = LOAD_FAST; |
| 991 | break; |
| 992 | case NAME_GLOBAL: |
| 993 | op = LOAD_GLOBAL; |
| 994 | break; |
| 995 | case NAME_DEFAULT: |
| 996 | op = LOAD_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 997 | break; |
| 998 | case NAME_CLOSURE: |
| 999 | op = LOAD_DEREF; |
| 1000 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1001 | } |
| 1002 | break; |
| 1003 | case VAR_STORE: |
| 1004 | switch (scope) { |
| 1005 | case NAME_LOCAL: |
| 1006 | op = STORE_FAST; |
| 1007 | break; |
| 1008 | case NAME_GLOBAL: |
| 1009 | op = STORE_GLOBAL; |
| 1010 | break; |
| 1011 | case NAME_DEFAULT: |
| 1012 | op = STORE_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1013 | break; |
| 1014 | case NAME_CLOSURE: |
| 1015 | op = STORE_DEREF; |
| 1016 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1017 | } |
| 1018 | break; |
| 1019 | case VAR_DELETE: |
| 1020 | switch (scope) { |
| 1021 | case NAME_LOCAL: |
| 1022 | op = DELETE_FAST; |
| 1023 | break; |
| 1024 | case NAME_GLOBAL: |
| 1025 | op = DELETE_GLOBAL; |
| 1026 | break; |
| 1027 | case NAME_DEFAULT: |
| 1028 | op = DELETE_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1029 | break; |
| 1030 | case NAME_CLOSURE: { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 1031 | char buf[500]; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1032 | sprintf(buf, DEL_CLOSURE_ERROR, name); |
| 1033 | com_error(c, PyExc_SyntaxError, buf); |
| 1034 | i = 255; |
| 1035 | break; |
| 1036 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1037 | } |
| 1038 | break; |
| 1039 | } |
| 1040 | done: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1041 | com_addoparg(c, op, i); |
| 1042 | } |
| 1043 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1044 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1045 | com_addopname(struct compiling *c, int op, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1046 | { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1047 | char *name; |
| 1048 | char buffer[1000]; |
| 1049 | /* XXX it is possible to write this code without the 1000 |
| 1050 | chars on the total length of dotted names, I just can't be |
| 1051 | bothered right now */ |
| 1052 | if (TYPE(n) == STAR) |
| 1053 | name = "*"; |
| 1054 | else if (TYPE(n) == dotted_name) { |
| 1055 | char *p = buffer; |
| 1056 | int i; |
| 1057 | name = buffer; |
| 1058 | for (i = 0; i < NCH(n); i += 2) { |
| 1059 | char *s = STR(CHILD(n, i)); |
| 1060 | if (p + strlen(s) > buffer + (sizeof buffer) - 2) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1061 | com_error(c, PyExc_MemoryError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1062 | "dotted_name too long"); |
Guido van Rossum | d9dfaf5 | 1995-02-17 15:04:57 +0000 | [diff] [blame] | 1063 | name = NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1064 | break; |
| 1065 | } |
| 1066 | if (p != buffer) |
| 1067 | *p++ = '.'; |
| 1068 | strcpy(p, s); |
| 1069 | p = strchr(p, '\0'); |
| 1070 | } |
| 1071 | } |
| 1072 | else { |
| 1073 | REQ(n, NAME); |
| 1074 | name = STR(n); |
| 1075 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1076 | com_addop_name(c, op, name); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1079 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1080 | parsenumber(struct compiling *co, char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1081 | { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1082 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1083 | long x; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1084 | double dx; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1085 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 1086 | Py_complex c; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1087 | int imflag; |
| 1088 | #endif |
| 1089 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1090 | errno = 0; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1091 | end = s + strlen(s) - 1; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1092 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 15ad9a6 | 1996-01-26 20:53:56 +0000 | [diff] [blame] | 1093 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1094 | #endif |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 1095 | if (*end == 'l' || *end == 'L') |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1096 | return PyLong_FromString(s, (char **)0, 0); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 1097 | if (s[0] == '0') |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1098 | x = (long) PyOS_strtoul(s, &end, 0); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 1099 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1100 | x = PyOS_strtol(s, &end, 0); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1101 | if (*end == '\0') { |
Jeremy Hylton | 71b6af9 | 2001-08-27 19:45:25 +0000 | [diff] [blame] | 1102 | if (errno != 0) |
| 1103 | return PyLong_FromString(s, (char **)0, 0); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1104 | return PyInt_FromLong(x); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1105 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1106 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1107 | #ifndef WITHOUT_COMPLEX |
| 1108 | if (imflag) { |
| 1109 | c.real = 0.; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1110 | PyFPE_START_PROTECT("atof", return 0) |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1111 | c.imag = atof(s); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1112 | PyFPE_END_PROTECT(c) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1113 | return PyComplex_FromCComplex(c); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1114 | } |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1115 | else |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1116 | #endif |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1117 | { |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1118 | PyFPE_START_PROTECT("atof", return 0) |
| 1119 | dx = atof(s); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1120 | PyFPE_END_PROTECT(dx) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1121 | return PyFloat_FromDouble(dx); |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1122 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1125 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1126 | parsestr(struct compiling *com, char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1127 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1128 | PyObject *v; |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1129 | size_t len; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1130 | char *buf; |
| 1131 | char *p; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1132 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1133 | int c; |
Guido van Rossum | 5f5e817 | 1997-04-06 03:41:40 +0000 | [diff] [blame] | 1134 | int first = *s; |
| 1135 | int quote = first; |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1136 | int rawmode = 0; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1137 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1138 | int unicode = 0; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1139 | #endif |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1140 | if (isalpha(quote) || quote == '_') { |
| 1141 | if (quote == 'u' || quote == 'U') { |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1142 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1143 | quote = *++s; |
| 1144 | unicode = 1; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1145 | #else |
| 1146 | com_error(com, PyExc_SyntaxError, |
| 1147 | "Unicode literals not supported in this Python"); |
| 1148 | return NULL; |
| 1149 | #endif |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1150 | } |
| 1151 | if (quote == 'r' || quote == 'R') { |
| 1152 | quote = *++s; |
| 1153 | rawmode = 1; |
| 1154 | } |
| 1155 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1156 | if (quote != '\'' && quote != '\"') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1157 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1158 | return NULL; |
| 1159 | } |
| 1160 | s++; |
| 1161 | len = strlen(s); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1162 | if (len > INT_MAX) { |
| 1163 | PyErr_SetString(PyExc_OverflowError, "string to parse is too long"); |
| 1164 | return NULL; |
| 1165 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1166 | if (s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1167 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1168 | return NULL; |
| 1169 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1170 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
| 1171 | s += 2; |
| 1172 | len -= 2; |
| 1173 | if (s[--len] != quote || s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1174 | PyErr_BadInternalCall(); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1175 | return NULL; |
| 1176 | } |
| 1177 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1178 | #ifdef Py_USING_UNICODE |
Guido van Rossum | fdc8bdb | 2000-05-01 17:54:56 +0000 | [diff] [blame] | 1179 | if (unicode || Py_UnicodeFlag) { |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1180 | if (rawmode) |
| 1181 | return PyUnicode_DecodeRawUnicodeEscape( |
| 1182 | s, len, NULL); |
| 1183 | else |
| 1184 | return PyUnicode_DecodeUnicodeEscape( |
| 1185 | s, len, NULL); |
| 1186 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1187 | #endif |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1188 | if (rawmode || strchr(s, '\\') == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1189 | return PyString_FromStringAndSize(s, len); |
| 1190 | v = PyString_FromStringAndSize((char *)NULL, len); |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1191 | if (v == NULL) |
| 1192 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1193 | p = buf = PyString_AsString(v); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1194 | end = s + len; |
| 1195 | while (s < end) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1196 | if (*s != '\\') { |
| 1197 | *p++ = *s++; |
| 1198 | continue; |
| 1199 | } |
| 1200 | s++; |
| 1201 | switch (*s++) { |
| 1202 | /* XXX This assumes ASCII! */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1203 | case '\n': break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1204 | case '\\': *p++ = '\\'; break; |
| 1205 | case '\'': *p++ = '\''; break; |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1206 | case '\"': *p++ = '\"'; break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1207 | case 'b': *p++ = '\b'; break; |
| 1208 | case 'f': *p++ = '\014'; break; /* FF */ |
| 1209 | case 't': *p++ = '\t'; break; |
| 1210 | case 'n': *p++ = '\n'; break; |
| 1211 | case 'r': *p++ = '\r'; break; |
| 1212 | case 'v': *p++ = '\013'; break; /* VT */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1213 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 1214 | case '0': case '1': case '2': case '3': |
| 1215 | case '4': case '5': case '6': case '7': |
| 1216 | c = s[-1] - '0'; |
| 1217 | if ('0' <= *s && *s <= '7') { |
| 1218 | c = (c<<3) + *s++ - '0'; |
| 1219 | if ('0' <= *s && *s <= '7') |
| 1220 | c = (c<<3) + *s++ - '0'; |
| 1221 | } |
| 1222 | *p++ = c; |
| 1223 | break; |
| 1224 | case 'x': |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 1225 | if (isxdigit(Py_CHARMASK(s[0])) |
| 1226 | && isxdigit(Py_CHARMASK(s[1]))) { |
Guido van Rossum | ed1100f | 1997-10-20 23:24:07 +0000 | [diff] [blame] | 1227 | unsigned int x = 0; |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1228 | c = Py_CHARMASK(*s); |
| 1229 | s++; |
| 1230 | if (isdigit(c)) |
| 1231 | x = c - '0'; |
| 1232 | else if (islower(c)) |
| 1233 | x = 10 + c - 'a'; |
| 1234 | else |
| 1235 | x = 10 + c - 'A'; |
| 1236 | x = x << 4; |
| 1237 | c = Py_CHARMASK(*s); |
| 1238 | s++; |
| 1239 | if (isdigit(c)) |
| 1240 | x += c - '0'; |
| 1241 | else if (islower(c)) |
| 1242 | x += 10 + c - 'a'; |
| 1243 | else |
| 1244 | x += 10 + c - 'A'; |
Guido van Rossum | ed1100f | 1997-10-20 23:24:07 +0000 | [diff] [blame] | 1245 | *p++ = x; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1246 | break; |
| 1247 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 1248 | PyErr_SetString(PyExc_ValueError, |
| 1249 | "invalid \\x escape"); |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1250 | Py_DECREF(v); |
| 1251 | return NULL; |
| 1252 | default: |
| 1253 | *p++ = '\\'; |
| 1254 | *p++ = s[-1]; |
| 1255 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1256 | } |
| 1257 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1258 | _PyString_Resize(&v, (int)(p - buf)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1259 | return v; |
| 1260 | } |
| 1261 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1262 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1263 | parsestrplus(struct compiling* c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1264 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1265 | PyObject *v; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1266 | int i; |
| 1267 | REQ(CHILD(n, 0), STRING); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1268 | if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1269 | /* String literal concatenation */ |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1270 | for (i = 1; i < NCH(n); i++) { |
| 1271 | PyObject *s; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1272 | s = parsestr(c, STR(CHILD(n, i))); |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1273 | if (s == NULL) |
| 1274 | goto onError; |
| 1275 | if (PyString_Check(v) && PyString_Check(s)) { |
| 1276 | PyString_ConcatAndDel(&v, s); |
| 1277 | if (v == NULL) |
| 1278 | goto onError; |
| 1279 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1280 | #ifdef Py_USING_UNICODE |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1281 | else { |
| 1282 | PyObject *temp; |
| 1283 | temp = PyUnicode_Concat(v, s); |
| 1284 | Py_DECREF(s); |
| 1285 | if (temp == NULL) |
| 1286 | goto onError; |
| 1287 | Py_DECREF(v); |
| 1288 | v = temp; |
| 1289 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1290 | #endif |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1291 | } |
| 1292 | } |
| 1293 | return v; |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1294 | |
| 1295 | onError: |
| 1296 | Py_XDECREF(v); |
| 1297 | return NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1300 | static void |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1301 | 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] | 1302 | { |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1303 | int anchor = 0; |
| 1304 | int save_begin = c->c_begin; |
| 1305 | |
| 1306 | /* list_iter: for v in expr [list_iter] */ |
| 1307 | com_node(c, CHILD(n, 3)); /* expr */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1308 | com_addbyte(c, GET_ITER); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1309 | c->c_begin = c->c_nexti; |
| 1310 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1311 | com_addfwref(c, FOR_ITER, &anchor); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1312 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1313 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1314 | c->c_loops++; |
| 1315 | com_list_iter(c, n, e, t); |
| 1316 | c->c_loops--; |
| 1317 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 1318 | c->c_begin = save_begin; |
| 1319 | com_backpatch(c, anchor); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1320 | com_pop(c, 1); /* FOR_ITER has popped this */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | static void |
| 1324 | com_list_if(struct compiling *c, node *n, node *e, char *t) |
| 1325 | { |
| 1326 | int anchor = 0; |
| 1327 | int a = 0; |
| 1328 | /* list_iter: 'if' test [list_iter] */ |
| 1329 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 1330 | com_node(c, CHILD(n, 1)); |
| 1331 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 1332 | com_addbyte(c, POP_TOP); |
| 1333 | com_pop(c, 1); |
| 1334 | com_list_iter(c, n, e, t); |
| 1335 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 1336 | com_backpatch(c, a); |
| 1337 | /* We jump here with an extra entry which we now pop */ |
| 1338 | com_addbyte(c, POP_TOP); |
| 1339 | com_backpatch(c, anchor); |
| 1340 | } |
| 1341 | |
| 1342 | static void |
| 1343 | com_list_iter(struct compiling *c, |
| 1344 | node *p, /* parent of list_iter node */ |
| 1345 | node *e, /* element expression node */ |
| 1346 | char *t /* name of result list temp local */) |
| 1347 | { |
| 1348 | /* list_iter is the last child in a listmaker, list_for, or list_if */ |
| 1349 | node *n = CHILD(p, NCH(p)-1); |
| 1350 | if (TYPE(n) == list_iter) { |
| 1351 | n = CHILD(n, 0); |
| 1352 | switch (TYPE(n)) { |
| 1353 | case list_for: |
| 1354 | com_list_for(c, n, e, t); |
| 1355 | break; |
| 1356 | case list_if: |
| 1357 | com_list_if(c, n, e, t); |
| 1358 | break; |
| 1359 | default: |
| 1360 | com_error(c, PyExc_SystemError, |
| 1361 | "invalid list_iter node type"); |
| 1362 | } |
| 1363 | } |
| 1364 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1365 | com_addop_varname(c, VAR_LOAD, t); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1366 | com_push(c, 1); |
| 1367 | com_node(c, e); |
| 1368 | com_addoparg(c, CALL_FUNCTION, 1); |
| 1369 | com_addbyte(c, POP_TOP); |
| 1370 | com_pop(c, 2); |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | static void |
| 1375 | com_list_comprehension(struct compiling *c, node *n) |
| 1376 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1377 | /* listmaker: test list_for */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1378 | char tmpname[12]; |
Tim Peters | 3081421 | 2001-02-17 05:30:26 +0000 | [diff] [blame] | 1379 | sprintf(tmpname, "_[%d]", ++c->c_tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1380 | com_addoparg(c, BUILD_LIST, 0); |
| 1381 | com_addbyte(c, DUP_TOP); /* leave the result on the stack */ |
| 1382 | com_push(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1383 | com_addop_name(c, LOAD_ATTR, "append"); |
| 1384 | com_addop_varname(c, VAR_STORE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1385 | com_pop(c, 1); |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1386 | com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1387 | com_addop_varname(c, VAR_DELETE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1388 | --c->c_tmpname; |
| 1389 | } |
| 1390 | |
| 1391 | static void |
| 1392 | com_listmaker(struct compiling *c, node *n) |
| 1393 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1394 | /* listmaker: test ( list_for | (',' test)* [','] ) */ |
| 1395 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1396 | com_list_comprehension(c, n); |
| 1397 | else { |
| 1398 | int len = 0; |
| 1399 | int i; |
| 1400 | for (i = 0; i < NCH(n); i += 2, len++) |
| 1401 | com_node(c, CHILD(n, i)); |
| 1402 | com_addoparg(c, BUILD_LIST, len); |
| 1403 | com_pop(c, len-1); |
| 1404 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1408 | com_dictmaker(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1409 | { |
| 1410 | int i; |
| 1411 | /* dictmaker: test ':' test (',' test ':' value)* [','] */ |
| 1412 | for (i = 0; i+2 < NCH(n); i += 4) { |
| 1413 | /* We must arrange things just right for STORE_SUBSCR. |
| 1414 | It wants the stack to look like (value) (dict) (key) */ |
| 1415 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1416 | com_push(c, 1); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1417 | com_node(c, CHILD(n, i+2)); /* value */ |
| 1418 | com_addbyte(c, ROT_TWO); |
| 1419 | com_node(c, CHILD(n, i)); /* key */ |
| 1420 | com_addbyte(c, STORE_SUBSCR); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1421 | com_pop(c, 3); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1426 | com_atom(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1427 | { |
| 1428 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1429 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1430 | int i; |
| 1431 | REQ(n, atom); |
| 1432 | ch = CHILD(n, 0); |
| 1433 | switch (TYPE(ch)) { |
| 1434 | case LPAR: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1435 | if (TYPE(CHILD(n, 1)) == RPAR) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1436 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1437 | com_push(c, 1); |
| 1438 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1439 | else |
| 1440 | com_node(c, CHILD(n, 1)); |
| 1441 | break; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1442 | case LSQB: /* '[' [listmaker] ']' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1443 | if (TYPE(CHILD(n, 1)) == RSQB) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1444 | com_addoparg(c, BUILD_LIST, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1445 | com_push(c, 1); |
| 1446 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1447 | else |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1448 | com_listmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1449 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1450 | case LBRACE: /* '{' [dictmaker] '}' */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1451 | com_addoparg(c, BUILD_MAP, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1452 | com_push(c, 1); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1453 | if (TYPE(CHILD(n, 1)) == dictmaker) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1454 | com_dictmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1455 | break; |
| 1456 | case BACKQUOTE: |
| 1457 | com_node(c, CHILD(n, 1)); |
| 1458 | com_addbyte(c, UNARY_CONVERT); |
| 1459 | break; |
| 1460 | case NUMBER: |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1461 | if ((v = parsenumber(c, STR(ch))) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1462 | i = 255; |
| 1463 | } |
| 1464 | else { |
| 1465 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1466 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1467 | } |
| 1468 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1469 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1470 | break; |
| 1471 | case STRING: |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1472 | v = parsestrplus(c, n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1473 | if (v == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1474 | c->c_errors++; |
| 1475 | i = 255; |
| 1476 | } |
| 1477 | else { |
| 1478 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1479 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1480 | } |
| 1481 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1482 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1483 | break; |
| 1484 | case NAME: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1485 | com_addop_varname(c, VAR_LOAD, STR(ch)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1486 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1487 | break; |
| 1488 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1489 | com_error(c, PyExc_SystemError, |
| 1490 | "com_atom: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1495 | com_slice(struct compiling *c, node *n, int op) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1496 | { |
| 1497 | if (NCH(n) == 1) { |
| 1498 | com_addbyte(c, op); |
| 1499 | } |
| 1500 | else if (NCH(n) == 2) { |
| 1501 | if (TYPE(CHILD(n, 0)) != COLON) { |
| 1502 | com_node(c, CHILD(n, 0)); |
| 1503 | com_addbyte(c, op+1); |
| 1504 | } |
| 1505 | else { |
| 1506 | com_node(c, CHILD(n, 1)); |
| 1507 | com_addbyte(c, op+2); |
| 1508 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1509 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1510 | } |
| 1511 | else { |
| 1512 | com_node(c, CHILD(n, 0)); |
| 1513 | com_node(c, CHILD(n, 2)); |
| 1514 | com_addbyte(c, op+3); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1515 | com_pop(c, 2); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1516 | } |
| 1517 | } |
| 1518 | |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1519 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1520 | com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn) |
| 1521 | { |
| 1522 | if (NCH(n) == 1) { |
| 1523 | com_addbyte(c, DUP_TOP); |
| 1524 | com_push(c, 1); |
| 1525 | com_addbyte(c, SLICE); |
| 1526 | com_node(c, augn); |
| 1527 | com_addbyte(c, opcode); |
| 1528 | com_pop(c, 1); |
| 1529 | com_addbyte(c, ROT_TWO); |
| 1530 | com_addbyte(c, STORE_SLICE); |
| 1531 | com_pop(c, 2); |
| 1532 | } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) { |
| 1533 | com_node(c, CHILD(n, 0)); |
| 1534 | com_addoparg(c, DUP_TOPX, 2); |
| 1535 | com_push(c, 2); |
| 1536 | com_addbyte(c, SLICE+1); |
| 1537 | com_pop(c, 1); |
| 1538 | com_node(c, augn); |
| 1539 | com_addbyte(c, opcode); |
| 1540 | com_pop(c, 1); |
| 1541 | com_addbyte(c, ROT_THREE); |
| 1542 | com_addbyte(c, STORE_SLICE+1); |
| 1543 | com_pop(c, 3); |
| 1544 | } else if (NCH(n) == 2) { |
| 1545 | com_node(c, CHILD(n, 1)); |
| 1546 | com_addoparg(c, DUP_TOPX, 2); |
| 1547 | com_push(c, 2); |
| 1548 | com_addbyte(c, SLICE+2); |
| 1549 | com_pop(c, 1); |
| 1550 | com_node(c, augn); |
| 1551 | com_addbyte(c, opcode); |
| 1552 | com_pop(c, 1); |
| 1553 | com_addbyte(c, ROT_THREE); |
| 1554 | com_addbyte(c, STORE_SLICE+2); |
| 1555 | com_pop(c, 3); |
| 1556 | } else { |
| 1557 | com_node(c, CHILD(n, 0)); |
| 1558 | com_node(c, CHILD(n, 2)); |
| 1559 | com_addoparg(c, DUP_TOPX, 3); |
| 1560 | com_push(c, 3); |
| 1561 | com_addbyte(c, SLICE+3); |
| 1562 | com_pop(c, 2); |
| 1563 | com_node(c, augn); |
| 1564 | com_addbyte(c, opcode); |
| 1565 | com_pop(c, 1); |
| 1566 | com_addbyte(c, ROT_FOUR); |
| 1567 | com_addbyte(c, STORE_SLICE+3); |
| 1568 | com_pop(c, 4); |
| 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1573 | com_argument(struct compiling *c, node *n, PyObject **pkeywords) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1574 | { |
| 1575 | node *m; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1576 | REQ(n, argument); /* [test '='] test; really [keyword '='] test */ |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1577 | if (NCH(n) == 1) { |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1578 | if (*pkeywords != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1579 | com_error(c, PyExc_SyntaxError, |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 1580 | "non-keyword arg after keyword arg"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1581 | } |
| 1582 | else { |
| 1583 | com_node(c, CHILD(n, 0)); |
| 1584 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1585 | return; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1586 | } |
| 1587 | m = n; |
| 1588 | do { |
| 1589 | m = CHILD(m, 0); |
| 1590 | } while (NCH(m) == 1); |
| 1591 | if (TYPE(m) != NAME) { |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 1592 | /* f(lambda x: x[0] = 3) ends up getting parsed with |
| 1593 | * LHS test = lambda x: x[0], and RHS test = 3. |
| 1594 | * SF bug 132313 points out that complaining about a keyword |
| 1595 | * then is very confusing. |
| 1596 | */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1597 | com_error(c, PyExc_SyntaxError, |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 1598 | TYPE(m) == lambdef ? |
| 1599 | "lambda cannot contain assignment" : |
| 1600 | "keyword can't be an expression"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1601 | } |
| 1602 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1603 | PyObject *v = PyString_InternFromString(STR(m)); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1604 | if (v != NULL && *pkeywords == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1605 | *pkeywords = PyDict_New(); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 1606 | if (v == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1607 | c->c_errors++; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 1608 | else if (*pkeywords == NULL) { |
| 1609 | c->c_errors++; |
| 1610 | Py_DECREF(v); |
| 1611 | } else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1612 | if (PyDict_GetItem(*pkeywords, v) != NULL) |
| 1613 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1614 | "duplicate keyword argument"); |
| 1615 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1616 | if (PyDict_SetItem(*pkeywords, v, v) != 0) |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1617 | c->c_errors++; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1618 | com_addoparg(c, LOAD_CONST, com_addconst(c, v)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1619 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1620 | Py_DECREF(v); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1621 | } |
| 1622 | } |
| 1623 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1626 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1627 | com_call_function(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1628 | { |
| 1629 | if (TYPE(n) == RPAR) { |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1630 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1631 | } |
| 1632 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1633 | PyObject *keywords = NULL; |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1634 | int i, na, nk; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1635 | int lineno = n->n_lineno; |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1636 | int star_flag = 0; |
| 1637 | int starstar_flag = 0; |
| 1638 | int opcode; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1639 | REQ(n, arglist); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1640 | na = 0; |
| 1641 | nk = 0; |
| 1642 | for (i = 0; i < NCH(n); i += 2) { |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1643 | node *ch = CHILD(n, i); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1644 | if (TYPE(ch) == STAR || |
| 1645 | TYPE(ch) == DOUBLESTAR) |
| 1646 | break; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1647 | if (ch->n_lineno != lineno) { |
| 1648 | lineno = ch->n_lineno; |
| 1649 | com_addoparg(c, SET_LINENO, lineno); |
| 1650 | } |
| 1651 | com_argument(c, ch, &keywords); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1652 | if (keywords == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1653 | na++; |
| 1654 | else |
| 1655 | nk++; |
| 1656 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1657 | Py_XDECREF(keywords); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1658 | while (i < NCH(n)) { |
| 1659 | node *tok = CHILD(n, i); |
| 1660 | node *ch = CHILD(n, i+1); |
| 1661 | i += 3; |
| 1662 | switch (TYPE(tok)) { |
| 1663 | case STAR: star_flag = 1; break; |
| 1664 | case DOUBLESTAR: starstar_flag = 1; break; |
| 1665 | } |
| 1666 | com_node(c, ch); |
| 1667 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1668 | if (na > 255 || nk > 255) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1669 | com_error(c, PyExc_SyntaxError, |
| 1670 | "more than 255 arguments"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1671 | } |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1672 | if (star_flag || starstar_flag) |
Jeremy Hylton | e4fb958 | 2000-03-29 00:10:44 +0000 | [diff] [blame] | 1673 | opcode = CALL_FUNCTION_VAR - 1 + |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1674 | star_flag + (starstar_flag << 1); |
| 1675 | else |
| 1676 | opcode = CALL_FUNCTION; |
| 1677 | com_addoparg(c, opcode, na | (nk << 8)); |
| 1678 | com_pop(c, na + 2*nk + star_flag + starstar_flag); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1683 | com_select_member(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1684 | { |
| 1685 | com_addopname(c, LOAD_ATTR, n); |
| 1686 | } |
| 1687 | |
| 1688 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1689 | com_sliceobj(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1690 | { |
| 1691 | int i=0; |
| 1692 | int ns=2; /* number of slice arguments */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1693 | node *ch; |
| 1694 | |
| 1695 | /* first argument */ |
| 1696 | if (TYPE(CHILD(n,i)) == COLON) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1697 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1698 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1699 | i++; |
| 1700 | } |
| 1701 | else { |
| 1702 | com_node(c, CHILD(n,i)); |
| 1703 | i++; |
| 1704 | REQ(CHILD(n,i),COLON); |
| 1705 | i++; |
| 1706 | } |
| 1707 | /* second argument */ |
| 1708 | if (i < NCH(n) && TYPE(CHILD(n,i)) == test) { |
| 1709 | com_node(c, CHILD(n,i)); |
| 1710 | i++; |
| 1711 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1712 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1713 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1714 | com_push(c, 1); |
| 1715 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1716 | /* remaining arguments */ |
| 1717 | for (; i < NCH(n); i++) { |
| 1718 | ns++; |
| 1719 | ch=CHILD(n,i); |
| 1720 | REQ(ch, sliceop); |
| 1721 | if (NCH(ch) == 1) { |
| 1722 | /* right argument of ':' missing */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1723 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1724 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1725 | } |
| 1726 | else |
| 1727 | com_node(c, CHILD(ch,1)); |
| 1728 | } |
| 1729 | com_addoparg(c, BUILD_SLICE, ns); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1730 | com_pop(c, 1 + (ns == 3)); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1734 | com_subscript(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1735 | { |
| 1736 | node *ch; |
| 1737 | REQ(n, subscript); |
| 1738 | ch = CHILD(n,0); |
| 1739 | /* check for rubber index */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1740 | if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) { |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 1741 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1742 | com_push(c, 1); |
| 1743 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1744 | else { |
| 1745 | /* check for slice */ |
| 1746 | if ((TYPE(ch) == COLON || NCH(n) > 1)) |
| 1747 | com_sliceobj(c, n); |
| 1748 | else { |
| 1749 | REQ(ch, test); |
| 1750 | com_node(c, ch); |
| 1751 | } |
| 1752 | } |
| 1753 | } |
| 1754 | |
| 1755 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1756 | com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1757 | { |
| 1758 | int i, op; |
| 1759 | REQ(n, subscriptlist); |
| 1760 | /* Check to make backward compatible slice behavior for '[i:j]' */ |
| 1761 | if (NCH(n) == 1) { |
| 1762 | node *sub = CHILD(n, 0); /* subscript */ |
Thomas Wouters | e8643c4 | 2000-08-05 21:37:50 +0000 | [diff] [blame] | 1763 | /* 'Basic' slice, should have exactly one colon. */ |
| 1764 | if ((TYPE(CHILD(sub, 0)) == COLON |
| 1765 | || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON)) |
| 1766 | && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop)) |
| 1767 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1768 | switch (assigning) { |
| 1769 | case OP_DELETE: |
| 1770 | op = DELETE_SLICE; |
| 1771 | break; |
| 1772 | case OP_ASSIGN: |
| 1773 | op = STORE_SLICE; |
| 1774 | break; |
| 1775 | case OP_APPLY: |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1776 | op = SLICE; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1777 | break; |
| 1778 | default: |
| 1779 | com_augassign_slice(c, sub, assigning, augn); |
| 1780 | return; |
| 1781 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1782 | com_slice(c, sub, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1783 | if (op == STORE_SLICE) |
| 1784 | com_pop(c, 2); |
| 1785 | else if (op == DELETE_SLICE) |
| 1786 | com_pop(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1787 | return; |
| 1788 | } |
| 1789 | } |
| 1790 | /* Else normal subscriptlist. Compile each subscript. */ |
| 1791 | for (i = 0; i < NCH(n); i += 2) |
| 1792 | com_subscript(c, CHILD(n, i)); |
| 1793 | /* Put multiple subscripts into a tuple */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1794 | if (NCH(n) > 1) { |
| 1795 | i = (NCH(n)+1) / 2; |
| 1796 | com_addoparg(c, BUILD_TUPLE, i); |
| 1797 | com_pop(c, i-1); |
| 1798 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1799 | switch (assigning) { |
| 1800 | case OP_DELETE: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1801 | op = DELETE_SUBSCR; |
| 1802 | i = 2; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1803 | break; |
| 1804 | default: |
| 1805 | case OP_ASSIGN: |
| 1806 | op = STORE_SUBSCR; |
| 1807 | i = 3; |
| 1808 | break; |
| 1809 | case OP_APPLY: |
| 1810 | op = BINARY_SUBSCR; |
| 1811 | i = 1; |
| 1812 | break; |
| 1813 | } |
| 1814 | if (assigning > OP_APPLY) { |
| 1815 | com_addoparg(c, DUP_TOPX, 2); |
| 1816 | com_push(c, 2); |
| 1817 | com_addbyte(c, BINARY_SUBSCR); |
| 1818 | com_pop(c, 1); |
| 1819 | com_node(c, augn); |
| 1820 | com_addbyte(c, assigning); |
| 1821 | com_pop(c, 1); |
| 1822 | com_addbyte(c, ROT_THREE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1823 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1824 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1825 | com_pop(c, i); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1829 | com_apply_trailer(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1830 | { |
| 1831 | REQ(n, trailer); |
| 1832 | switch (TYPE(CHILD(n, 0))) { |
| 1833 | case LPAR: |
| 1834 | com_call_function(c, CHILD(n, 1)); |
| 1835 | break; |
| 1836 | case DOT: |
| 1837 | com_select_member(c, CHILD(n, 1)); |
| 1838 | break; |
| 1839 | case LSQB: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1840 | com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1841 | break; |
| 1842 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1843 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1844 | "com_apply_trailer: unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1845 | } |
| 1846 | } |
| 1847 | |
| 1848 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1849 | com_power(struct compiling *c, node *n) |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1850 | { |
| 1851 | int i; |
| 1852 | REQ(n, power); |
| 1853 | com_atom(c, CHILD(n, 0)); |
| 1854 | for (i = 1; i < NCH(n); i++) { |
| 1855 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
| 1856 | com_factor(c, CHILD(n, i+1)); |
| 1857 | com_addbyte(c, BINARY_POWER); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1858 | com_pop(c, 1); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1859 | break; |
| 1860 | } |
| 1861 | else |
| 1862 | com_apply_trailer(c, CHILD(n, i)); |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | static void |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1867 | com_invert_constant(struct compiling *c, node *n) |
| 1868 | { |
| 1869 | /* Compute the inverse of int and longs and use them directly, |
| 1870 | but be prepared to generate code for all other |
| 1871 | possibilities (invalid numbers, floats, complex). |
| 1872 | */ |
| 1873 | PyObject *num, *inv = NULL; |
| 1874 | int i; |
| 1875 | |
| 1876 | REQ(n, NUMBER); |
| 1877 | num = parsenumber(c, STR(n)); |
| 1878 | if (num == NULL) |
| 1879 | i = 255; |
| 1880 | else { |
| 1881 | inv = PyNumber_Invert(num); |
| 1882 | if (inv == NULL) { |
| 1883 | PyErr_Clear(); |
| 1884 | i = com_addconst(c, num); |
| 1885 | } else { |
| 1886 | i = com_addconst(c, inv); |
| 1887 | Py_DECREF(inv); |
| 1888 | } |
| 1889 | Py_DECREF(num); |
| 1890 | } |
| 1891 | com_addoparg(c, LOAD_CONST, i); |
| 1892 | com_push(c, 1); |
| 1893 | if (num != NULL && inv == NULL) |
| 1894 | com_addbyte(c, UNARY_INVERT); |
| 1895 | } |
| 1896 | |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 1897 | static int |
| 1898 | is_float_zero(const char *p) |
| 1899 | { |
| 1900 | int found_radix_point = 0; |
| 1901 | int ch; |
| 1902 | while ((ch = Py_CHARMASK(*p++)) != '\0') { |
| 1903 | switch (ch) { |
| 1904 | case '0': |
| 1905 | /* no reason to believe it's not 0 -- continue */ |
| 1906 | break; |
| 1907 | |
| 1908 | case 'e': case 'E': case 'j': case 'J': |
| 1909 | /* If this was a hex constant, we already would have |
| 1910 | returned 0 due to the 'x' or 'X', so 'e' or 'E' |
| 1911 | must be an exponent marker, and we haven't yet |
| 1912 | seen a non-zero digit, and it doesn't matter what |
| 1913 | the exponent is then. For 'j' or 'J' similarly, |
| 1914 | except that this is an imaginary 0 then. */ |
| 1915 | return 1; |
| 1916 | |
| 1917 | case '.': |
| 1918 | found_radix_point = 1; |
| 1919 | break; |
| 1920 | |
| 1921 | default: |
| 1922 | return 0; |
| 1923 | } |
| 1924 | } |
| 1925 | return found_radix_point; |
| 1926 | } |
| 1927 | |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1928 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1929 | com_factor(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1930 | { |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1931 | int childtype = TYPE(CHILD(n, 0)); |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 1932 | node *pfactor, *ppower, *patom, *pnum; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1933 | REQ(n, factor); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1934 | /* If the unary +, -, or ~ operator is applied to a constant, |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 1935 | don't generate a UNARY_xxx opcode. Just store the |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1936 | approriate value as a constant. If the value is negative, |
| 1937 | extend the string containing the constant and insert a |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 1938 | negative in the 0th position -- unless we're doing unary minus |
| 1939 | of a floating zero! In that case the sign is significant, but |
| 1940 | the const dict can't distinguish +0.0 from -0.0. |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1941 | */ |
| 1942 | if ((childtype == PLUS || childtype == MINUS || childtype == TILDE) |
Fred Drake | 14ef244 | 2001-08-30 18:53:25 +0000 | [diff] [blame] | 1943 | && NCH(n) == 2 |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 1944 | && TYPE((pfactor = CHILD(n, 1))) == factor |
| 1945 | && NCH(pfactor) == 1 |
| 1946 | && TYPE((ppower = CHILD(pfactor, 0))) == power |
| 1947 | && NCH(ppower) == 1 |
| 1948 | && TYPE((patom = CHILD(ppower, 0))) == atom |
| 1949 | && TYPE((pnum = CHILD(patom, 0))) == NUMBER |
| 1950 | && !(childtype == MINUS && is_float_zero(STR(pnum)))) { |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1951 | if (childtype == TILDE) { |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 1952 | com_invert_constant(c, pnum); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1953 | return; |
| 1954 | } |
| 1955 | if (childtype == MINUS) { |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 1956 | char *s = malloc(strlen(STR(pnum)) + 2); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1957 | if (s == NULL) { |
| 1958 | com_error(c, PyExc_MemoryError, ""); |
| 1959 | com_addbyte(c, 255); |
| 1960 | return; |
| 1961 | } |
| 1962 | s[0] = '-'; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 1963 | strcpy(s + 1, STR(pnum)); |
| 1964 | free(STR(pnum)); |
| 1965 | STR(pnum) = s; |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1966 | } |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 1967 | com_atom(c, patom); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1968 | } |
| 1969 | else if (childtype == PLUS) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1970 | com_factor(c, CHILD(n, 1)); |
| 1971 | com_addbyte(c, UNARY_POSITIVE); |
| 1972 | } |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1973 | else if (childtype == MINUS) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1974 | com_factor(c, CHILD(n, 1)); |
| 1975 | com_addbyte(c, UNARY_NEGATIVE); |
| 1976 | } |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1977 | else if (childtype == TILDE) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1978 | com_factor(c, CHILD(n, 1)); |
| 1979 | com_addbyte(c, UNARY_INVERT); |
| 1980 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1981 | else { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1982 | com_power(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1983 | } |
| 1984 | } |
| 1985 | |
| 1986 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1987 | com_term(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1988 | { |
| 1989 | int i; |
| 1990 | int op; |
| 1991 | REQ(n, term); |
| 1992 | com_factor(c, CHILD(n, 0)); |
| 1993 | for (i = 2; i < NCH(n); i += 2) { |
| 1994 | com_factor(c, CHILD(n, i)); |
| 1995 | switch (TYPE(CHILD(n, i-1))) { |
| 1996 | case STAR: |
| 1997 | op = BINARY_MULTIPLY; |
| 1998 | break; |
| 1999 | case SLASH: |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2000 | if (c->c_flags & CO_FUTURE_DIVISION) |
| 2001 | op = BINARY_TRUE_DIVIDE; |
| 2002 | else |
| 2003 | op = BINARY_DIVIDE; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2004 | break; |
| 2005 | case PERCENT: |
| 2006 | op = BINARY_MODULO; |
| 2007 | break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2008 | case DOUBLESLASH: |
| 2009 | op = BINARY_FLOOR_DIVIDE; |
| 2010 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2011 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2012 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2013 | "com_term: operator not *, /, // or %"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2014 | op = 255; |
| 2015 | } |
| 2016 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2017 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2018 | } |
| 2019 | } |
| 2020 | |
| 2021 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2022 | com_arith_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2023 | { |
| 2024 | int i; |
| 2025 | int op; |
| 2026 | REQ(n, arith_expr); |
| 2027 | com_term(c, CHILD(n, 0)); |
| 2028 | for (i = 2; i < NCH(n); i += 2) { |
| 2029 | com_term(c, CHILD(n, i)); |
| 2030 | switch (TYPE(CHILD(n, i-1))) { |
| 2031 | case PLUS: |
| 2032 | op = BINARY_ADD; |
| 2033 | break; |
| 2034 | case MINUS: |
| 2035 | op = BINARY_SUBTRACT; |
| 2036 | break; |
| 2037 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2038 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2039 | "com_arith_expr: operator not + or -"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2040 | op = 255; |
| 2041 | } |
| 2042 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2043 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2048 | com_shift_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2049 | { |
| 2050 | int i; |
| 2051 | int op; |
| 2052 | REQ(n, shift_expr); |
| 2053 | com_arith_expr(c, CHILD(n, 0)); |
| 2054 | for (i = 2; i < NCH(n); i += 2) { |
| 2055 | com_arith_expr(c, CHILD(n, i)); |
| 2056 | switch (TYPE(CHILD(n, i-1))) { |
| 2057 | case LEFTSHIFT: |
| 2058 | op = BINARY_LSHIFT; |
| 2059 | break; |
| 2060 | case RIGHTSHIFT: |
| 2061 | op = BINARY_RSHIFT; |
| 2062 | break; |
| 2063 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2064 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2065 | "com_shift_expr: operator not << or >>"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2066 | op = 255; |
| 2067 | } |
| 2068 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2069 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2070 | } |
| 2071 | } |
| 2072 | |
| 2073 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2074 | com_and_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2075 | { |
| 2076 | int i; |
| 2077 | int op; |
| 2078 | REQ(n, and_expr); |
| 2079 | com_shift_expr(c, CHILD(n, 0)); |
| 2080 | for (i = 2; i < NCH(n); i += 2) { |
| 2081 | com_shift_expr(c, CHILD(n, i)); |
| 2082 | if (TYPE(CHILD(n, i-1)) == AMPER) { |
| 2083 | op = BINARY_AND; |
| 2084 | } |
| 2085 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2086 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2087 | "com_and_expr: operator not &"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2088 | op = 255; |
| 2089 | } |
| 2090 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2091 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2092 | } |
| 2093 | } |
| 2094 | |
| 2095 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2096 | com_xor_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2097 | { |
| 2098 | int i; |
| 2099 | int op; |
| 2100 | REQ(n, xor_expr); |
| 2101 | com_and_expr(c, CHILD(n, 0)); |
| 2102 | for (i = 2; i < NCH(n); i += 2) { |
| 2103 | com_and_expr(c, CHILD(n, i)); |
| 2104 | if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) { |
| 2105 | op = BINARY_XOR; |
| 2106 | } |
| 2107 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2108 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2109 | "com_xor_expr: operator not ^"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2110 | op = 255; |
| 2111 | } |
| 2112 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2113 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2114 | } |
| 2115 | } |
| 2116 | |
| 2117 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2118 | com_expr(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2119 | { |
| 2120 | int i; |
| 2121 | int op; |
| 2122 | REQ(n, expr); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2123 | com_xor_expr(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2124 | for (i = 2; i < NCH(n); i += 2) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2125 | com_xor_expr(c, CHILD(n, i)); |
| 2126 | if (TYPE(CHILD(n, i-1)) == VBAR) { |
| 2127 | op = BINARY_OR; |
| 2128 | } |
| 2129 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2130 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2131 | "com_expr: expr operator not |"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2132 | op = 255; |
| 2133 | } |
| 2134 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2135 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2136 | } |
| 2137 | } |
| 2138 | |
| 2139 | static enum cmp_op |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2140 | cmp_type(node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2141 | { |
| 2142 | REQ(n, comp_op); |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2143 | /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '==' |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2144 | | 'in' | 'not' 'in' | 'is' | 'is' not' */ |
| 2145 | if (NCH(n) == 1) { |
| 2146 | n = CHILD(n, 0); |
| 2147 | switch (TYPE(n)) { |
| 2148 | case LESS: return LT; |
| 2149 | case GREATER: return GT; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2150 | case EQEQUAL: /* == */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2151 | case EQUAL: return EQ; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2152 | case LESSEQUAL: return LE; |
| 2153 | case GREATEREQUAL: return GE; |
| 2154 | case NOTEQUAL: return NE; /* <> or != */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2155 | case NAME: if (strcmp(STR(n), "in") == 0) return IN; |
| 2156 | if (strcmp(STR(n), "is") == 0) return IS; |
| 2157 | } |
| 2158 | } |
| 2159 | else if (NCH(n) == 2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2160 | switch (TYPE(CHILD(n, 0))) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2161 | case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 2162 | return NOT_IN; |
| 2163 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 2164 | return IS_NOT; |
| 2165 | } |
| 2166 | } |
| 2167 | return BAD; |
| 2168 | } |
| 2169 | |
| 2170 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2171 | com_comparison(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2172 | { |
| 2173 | int i; |
| 2174 | enum cmp_op op; |
| 2175 | int anchor; |
| 2176 | REQ(n, comparison); /* comparison: expr (comp_op expr)* */ |
| 2177 | com_expr(c, CHILD(n, 0)); |
| 2178 | if (NCH(n) == 1) |
| 2179 | return; |
| 2180 | |
| 2181 | /**************************************************************** |
| 2182 | The following code is generated for all but the last |
| 2183 | comparison in a chain: |
| 2184 | |
| 2185 | label: on stack: opcode: jump to: |
| 2186 | |
| 2187 | a <code to load b> |
| 2188 | a, b DUP_TOP |
| 2189 | a, b, b ROT_THREE |
| 2190 | b, a, b COMPARE_OP |
| 2191 | b, 0-or-1 JUMP_IF_FALSE L1 |
| 2192 | b, 1 POP_TOP |
| 2193 | b |
| 2194 | |
| 2195 | We are now ready to repeat this sequence for the next |
| 2196 | comparison in the chain. |
| 2197 | |
| 2198 | For the last we generate: |
| 2199 | |
| 2200 | b <code to load c> |
| 2201 | b, c COMPARE_OP |
| 2202 | 0-or-1 |
| 2203 | |
| 2204 | If there were any jumps to L1 (i.e., there was more than one |
| 2205 | comparison), we generate: |
| 2206 | |
| 2207 | 0-or-1 JUMP_FORWARD L2 |
| 2208 | L1: b, 0 ROT_TWO |
| 2209 | 0, b POP_TOP |
| 2210 | 0 |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2211 | L2: 0-or-1 |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2212 | ****************************************************************/ |
| 2213 | |
| 2214 | anchor = 0; |
| 2215 | |
| 2216 | for (i = 2; i < NCH(n); i += 2) { |
| 2217 | com_expr(c, CHILD(n, i)); |
| 2218 | if (i+2 < NCH(n)) { |
| 2219 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2220 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2221 | com_addbyte(c, ROT_THREE); |
| 2222 | } |
| 2223 | op = cmp_type(CHILD(n, i-1)); |
| 2224 | if (op == BAD) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2225 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2226 | "com_comparison: unknown comparison op"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2227 | } |
| 2228 | com_addoparg(c, COMPARE_OP, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2229 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2230 | if (i+2 < NCH(n)) { |
| 2231 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2232 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2233 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2234 | } |
| 2235 | } |
| 2236 | |
| 2237 | if (anchor) { |
| 2238 | int anchor2 = 0; |
| 2239 | com_addfwref(c, JUMP_FORWARD, &anchor2); |
| 2240 | com_backpatch(c, anchor); |
| 2241 | com_addbyte(c, ROT_TWO); |
| 2242 | com_addbyte(c, POP_TOP); |
| 2243 | com_backpatch(c, anchor2); |
| 2244 | } |
| 2245 | } |
| 2246 | |
| 2247 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2248 | com_not_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2249 | { |
| 2250 | REQ(n, not_test); /* 'not' not_test | comparison */ |
| 2251 | if (NCH(n) == 1) { |
| 2252 | com_comparison(c, CHILD(n, 0)); |
| 2253 | } |
| 2254 | else { |
| 2255 | com_not_test(c, CHILD(n, 1)); |
| 2256 | com_addbyte(c, UNARY_NOT); |
| 2257 | } |
| 2258 | } |
| 2259 | |
| 2260 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2261 | com_and_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2262 | { |
| 2263 | int i; |
| 2264 | int anchor; |
| 2265 | REQ(n, and_test); /* not_test ('and' not_test)* */ |
| 2266 | anchor = 0; |
| 2267 | i = 0; |
| 2268 | for (;;) { |
| 2269 | com_not_test(c, CHILD(n, i)); |
| 2270 | if ((i += 2) >= NCH(n)) |
| 2271 | break; |
| 2272 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2273 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2274 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2275 | } |
| 2276 | if (anchor) |
| 2277 | com_backpatch(c, anchor); |
| 2278 | } |
| 2279 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2280 | static int |
| 2281 | com_make_closure(struct compiling *c, PyCodeObject *co) |
| 2282 | { |
| 2283 | int i, free = PyTuple_GET_SIZE(co->co_freevars); |
| 2284 | if (free == 0) |
| 2285 | return 0; |
| 2286 | for (i = 0; i < free; ++i) { |
| 2287 | /* Bypass com_addop_varname because it will generate |
| 2288 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 2289 | */ |
| 2290 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 2291 | int arg, reftype; |
| 2292 | |
| 2293 | /* Special case: If a class contains a method with a |
| 2294 | free variable that has the same name as a method, |
| 2295 | the name will be considered free *and* local in the |
| 2296 | class. It should be handled by the closure, as |
| 2297 | well as by the normal name loookup logic. |
| 2298 | */ |
| 2299 | reftype = get_ref_type(c, PyString_AS_STRING(name)); |
| 2300 | if (reftype == CELL) |
| 2301 | arg = com_lookup_arg(c->c_cellvars, name); |
| 2302 | else /* (reftype == FREE) */ |
| 2303 | arg = com_lookup_arg(c->c_freevars, name); |
| 2304 | if (arg == -1) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 2305 | fprintf(stderr, "lookup %s in %s %d %d\n" |
| 2306 | "freevars of %s: %s\n", |
| 2307 | PyObject_REPR(name), |
| 2308 | c->c_name, |
| 2309 | reftype, arg, |
| 2310 | PyString_AS_STRING(co->co_name), |
| 2311 | PyObject_REPR(co->co_freevars)); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2312 | Py_FatalError("com_make_closure()"); |
| 2313 | } |
| 2314 | com_addoparg(c, LOAD_CLOSURE, arg); |
| 2315 | |
| 2316 | } |
| 2317 | com_push(c, free); |
| 2318 | return 1; |
| 2319 | } |
| 2320 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2321 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2322 | com_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2323 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2324 | REQ(n, test); /* and_test ('or' and_test)* | lambdef */ |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2325 | if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2326 | PyCodeObject *co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2327 | int i, closure; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2328 | int ndefs = com_argdefs(c, CHILD(n, 0)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 2329 | symtable_enter_scope(c->c_symtable, "lambda", lambdef, |
| 2330 | n->n_lineno); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2331 | co = icompile(CHILD(n, 0), c); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2332 | if (co == NULL) { |
| 2333 | c->c_errors++; |
| 2334 | return; |
| 2335 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2336 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2337 | i = com_addconst(c, (PyObject *)co); |
| 2338 | closure = com_make_closure(c, co); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2339 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2340 | com_push(c, 1); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2341 | if (closure) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2342 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2343 | com_pop(c, PyTuple_GET_SIZE(co->co_freevars)); |
| 2344 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2345 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2346 | Py_DECREF(co); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2347 | com_pop(c, ndefs); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2348 | } |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2349 | else { |
| 2350 | int anchor = 0; |
| 2351 | int i = 0; |
| 2352 | for (;;) { |
| 2353 | com_and_test(c, CHILD(n, i)); |
| 2354 | if ((i += 2) >= NCH(n)) |
| 2355 | break; |
| 2356 | com_addfwref(c, JUMP_IF_TRUE, &anchor); |
| 2357 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2358 | com_pop(c, 1); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2359 | } |
| 2360 | if (anchor) |
| 2361 | com_backpatch(c, anchor); |
| 2362 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
| 2365 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2366 | com_list(struct compiling *c, node *n, int toplevel) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2367 | { |
| 2368 | /* exprlist: expr (',' expr)* [',']; likewise for testlist */ |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 2369 | if (NCH(n) == 1 && !toplevel) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2370 | com_node(c, CHILD(n, 0)); |
| 2371 | } |
| 2372 | else { |
| 2373 | int i; |
| 2374 | int len; |
| 2375 | len = (NCH(n) + 1) / 2; |
| 2376 | for (i = 0; i < NCH(n); i += 2) |
| 2377 | com_node(c, CHILD(n, i)); |
| 2378 | com_addoparg(c, BUILD_TUPLE, len); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2379 | com_pop(c, len-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2380 | } |
| 2381 | } |
| 2382 | |
| 2383 | |
| 2384 | /* Begin of assignment compilation */ |
| 2385 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2386 | |
| 2387 | static void |
| 2388 | com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn) |
| 2389 | { |
| 2390 | com_addbyte(c, DUP_TOP); |
| 2391 | com_push(c, 1); |
| 2392 | com_addopname(c, LOAD_ATTR, n); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2393 | com_node(c, augn); |
| 2394 | com_addbyte(c, opcode); |
| 2395 | com_pop(c, 1); |
| 2396 | com_addbyte(c, ROT_TWO); |
| 2397 | com_addopname(c, STORE_ATTR, n); |
| 2398 | com_pop(c, 2); |
| 2399 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2400 | |
| 2401 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2402 | com_assign_attr(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2403 | { |
| 2404 | com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2405 | com_pop(c, assigning ? 2 : 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
| 2408 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2409 | 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] | 2410 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2411 | REQ(n, trailer); |
| 2412 | switch (TYPE(CHILD(n, 0))) { |
| 2413 | case LPAR: /* '(' [exprlist] ')' */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2414 | com_error(c, PyExc_SyntaxError, |
| 2415 | "can't assign to function call"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2416 | break; |
| 2417 | case DOT: /* '.' NAME */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2418 | if (assigning > OP_APPLY) |
| 2419 | com_augassign_attr(c, CHILD(n, 1), assigning, augn); |
| 2420 | else |
| 2421 | com_assign_attr(c, CHILD(n, 1), assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2422 | break; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2423 | case LSQB: /* '[' subscriptlist ']' */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2424 | com_subscriptlist(c, CHILD(n, 1), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2425 | break; |
| 2426 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2427 | com_error(c, PyExc_SystemError, "unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2428 | } |
| 2429 | } |
| 2430 | |
| 2431 | static void |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2432 | com_assign_sequence(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2433 | { |
| 2434 | int i; |
Thomas Wouters | b59290f | 2000-08-25 05:41:11 +0000 | [diff] [blame] | 2435 | if (TYPE(n) != testlist && TYPE(n) != listmaker) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2436 | REQ(n, exprlist); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2437 | if (assigning) { |
| 2438 | i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2439 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2440 | com_push(c, i-1); |
| 2441 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2442 | for (i = 0; i < NCH(n); i += 2) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2443 | com_assign(c, CHILD(n, i), assigning, NULL); |
| 2444 | } |
| 2445 | |
| 2446 | static void |
| 2447 | com_augassign_name(struct compiling *c, node *n, int opcode, node *augn) |
| 2448 | { |
| 2449 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2450 | com_addop_varname(c, VAR_LOAD, STR(n)); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2451 | com_push(c, 1); |
| 2452 | com_node(c, augn); |
| 2453 | com_addbyte(c, opcode); |
| 2454 | com_pop(c, 1); |
| 2455 | com_assign_name(c, n, OP_ASSIGN); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2456 | } |
| 2457 | |
| 2458 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2459 | com_assign_name(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2460 | { |
| 2461 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2462 | com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2463 | if (assigning) |
| 2464 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
| 2467 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2468 | com_assign(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2469 | { |
| 2470 | /* Loop to avoid trivial recursion */ |
| 2471 | for (;;) { |
| 2472 | switch (TYPE(n)) { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2473 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2474 | case exprlist: |
| 2475 | case testlist: |
| 2476 | if (NCH(n) > 1) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2477 | if (assigning > OP_APPLY) { |
| 2478 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 2479 | "augmented assign to tuple not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2480 | return; |
| 2481 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2482 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2483 | return; |
| 2484 | } |
| 2485 | n = CHILD(n, 0); |
| 2486 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2487 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2488 | case test: |
| 2489 | case and_test: |
| 2490 | case not_test: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2491 | case comparison: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2492 | case expr: |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2493 | case xor_expr: |
| 2494 | case and_expr: |
| 2495 | case shift_expr: |
| 2496 | case arith_expr: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2497 | case term: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2498 | case factor: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2499 | if (NCH(n) > 1) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2500 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2501 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2502 | return; |
| 2503 | } |
| 2504 | n = CHILD(n, 0); |
| 2505 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2506 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2507 | case power: /* atom trailer* ('**' power)* |
| 2508 | ('+'|'-'|'~') factor | atom trailer* */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2509 | if (TYPE(CHILD(n, 0)) != atom) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2510 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2511 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2512 | return; |
| 2513 | } |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2514 | if (NCH(n) > 1) { /* trailer or exponent present */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2515 | int i; |
| 2516 | com_node(c, CHILD(n, 0)); |
| 2517 | for (i = 1; i+1 < NCH(n); i++) { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2518 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2519 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2520 | "can't assign to operator"); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2521 | return; |
| 2522 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2523 | com_apply_trailer(c, CHILD(n, i)); |
| 2524 | } /* NB i is still alive */ |
| 2525 | com_assign_trailer(c, |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2526 | CHILD(n, i), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2527 | return; |
| 2528 | } |
| 2529 | n = CHILD(n, 0); |
| 2530 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2531 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2532 | case atom: |
| 2533 | switch (TYPE(CHILD(n, 0))) { |
| 2534 | case LPAR: |
| 2535 | n = CHILD(n, 1); |
| 2536 | if (TYPE(n) == RPAR) { |
| 2537 | /* XXX Should allow () = () ??? */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2538 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2539 | "can't assign to ()"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2540 | return; |
| 2541 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2542 | if (assigning > OP_APPLY) { |
| 2543 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2544 | "augmented assign to tuple not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2545 | return; |
| 2546 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2547 | break; |
| 2548 | case LSQB: |
| 2549 | n = CHILD(n, 1); |
| 2550 | if (TYPE(n) == RSQB) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2551 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2552 | "can't assign to []"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2553 | return; |
| 2554 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2555 | if (assigning > OP_APPLY) { |
| 2556 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2557 | "augmented assign to list not possible"); |
| 2558 | return; |
| 2559 | } |
| 2560 | if (NCH(n) > 1 |
| 2561 | && TYPE(CHILD(n, 1)) == list_for) { |
| 2562 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2563 | "can't assign to list comprehension"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2564 | return; |
| 2565 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2566 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2567 | return; |
| 2568 | case NAME: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2569 | if (assigning > OP_APPLY) |
| 2570 | com_augassign_name(c, CHILD(n, 0), |
| 2571 | assigning, augn); |
| 2572 | else |
| 2573 | com_assign_name(c, CHILD(n, 0), |
| 2574 | assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2575 | return; |
| 2576 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2577 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2578 | "can't assign to literal"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2579 | return; |
| 2580 | } |
| 2581 | break; |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2582 | |
| 2583 | case lambdef: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2584 | com_error(c, PyExc_SyntaxError, |
| 2585 | "can't assign to lambda"); |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2586 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2587 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2588 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2589 | com_error(c, PyExc_SystemError, |
| 2590 | "com_assign: bad node"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2591 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2592 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2593 | } |
| 2594 | } |
| 2595 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2596 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2597 | static void |
| 2598 | com_augassign(struct compiling *c, node *n) |
| 2599 | { |
| 2600 | int opcode; |
| 2601 | |
| 2602 | switch (STR(CHILD(CHILD(n, 1), 0))[0]) { |
| 2603 | case '+': opcode = INPLACE_ADD; break; |
| 2604 | case '-': opcode = INPLACE_SUBTRACT; break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2605 | case '/': |
| 2606 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '/') |
| 2607 | opcode = INPLACE_FLOOR_DIVIDE; |
| 2608 | else if (c->c_flags & CO_FUTURE_DIVISION) |
| 2609 | opcode = INPLACE_TRUE_DIVIDE; |
| 2610 | else |
| 2611 | opcode = INPLACE_DIVIDE; |
| 2612 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2613 | case '%': opcode = INPLACE_MODULO; break; |
| 2614 | case '<': opcode = INPLACE_LSHIFT; break; |
| 2615 | case '>': opcode = INPLACE_RSHIFT; break; |
| 2616 | case '&': opcode = INPLACE_AND; break; |
| 2617 | case '^': opcode = INPLACE_XOR; break; |
| 2618 | case '|': opcode = INPLACE_OR; break; |
| 2619 | case '*': |
| 2620 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '*') |
| 2621 | opcode = INPLACE_POWER; |
| 2622 | else |
| 2623 | opcode = INPLACE_MULTIPLY; |
| 2624 | break; |
| 2625 | default: |
| 2626 | com_error(c, PyExc_SystemError, "com_augassign: bad operator"); |
| 2627 | return; |
| 2628 | } |
| 2629 | com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2)); |
| 2630 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2631 | |
| 2632 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2633 | com_expr_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2634 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2635 | REQ(n, expr_stmt); |
| 2636 | /* testlist (('=' testlist)* | augassign testlist) */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2637 | /* Forget it if we have just a doc string here */ |
Guido van Rossum | 5f5e817 | 1997-04-06 03:41:40 +0000 | [diff] [blame] | 2638 | if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2639 | return; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2640 | if (NCH(n) == 1) { |
| 2641 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2642 | if (c->c_interactive) |
| 2643 | com_addbyte(c, PRINT_EXPR); |
| 2644 | else |
| 2645 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2646 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2647 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2648 | else if (TYPE(CHILD(n,1)) == augassign) |
| 2649 | com_augassign(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2650 | else { |
| 2651 | int i; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2652 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2653 | for (i = 0; i < NCH(n)-2; i+=2) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2654 | if (i+2 < NCH(n)-2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2655 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2656 | com_push(c, 1); |
| 2657 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2658 | com_assign(c, CHILD(n, i), OP_ASSIGN, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2659 | } |
| 2660 | } |
| 2661 | } |
| 2662 | |
| 2663 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2664 | com_assert_stmt(struct compiling *c, node *n) |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2665 | { |
| 2666 | int a = 0, b = 0; |
| 2667 | int i; |
| 2668 | REQ(n, assert_stmt); /* 'assert' test [',' test] */ |
| 2669 | /* Generate code like for |
| 2670 | |
| 2671 | if __debug__: |
| 2672 | if not <test>: |
| 2673 | raise AssertionError [, <message>] |
| 2674 | |
| 2675 | where <message> is the second test, if present. |
| 2676 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2677 | |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2678 | if (Py_OptimizeFlag) |
| 2679 | return; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2680 | com_addop_name(c, LOAD_GLOBAL, "__debug__"); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2681 | com_push(c, 1); |
| 2682 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 2683 | com_addbyte(c, POP_TOP); |
| 2684 | com_pop(c, 1); |
| 2685 | com_node(c, CHILD(n, 1)); |
| 2686 | com_addfwref(c, JUMP_IF_TRUE, &b); |
| 2687 | com_addbyte(c, POP_TOP); |
| 2688 | com_pop(c, 1); |
| 2689 | /* Raise that exception! */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2690 | com_addop_name(c, LOAD_GLOBAL, "AssertionError"); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2691 | com_push(c, 1); |
| 2692 | i = NCH(n)/2; /* Either 2 or 4 */ |
| 2693 | if (i > 1) |
| 2694 | com_node(c, CHILD(n, 3)); |
| 2695 | com_addoparg(c, RAISE_VARARGS, i); |
| 2696 | com_pop(c, i); |
| 2697 | /* The interpreter does not fall through */ |
| 2698 | /* All jumps converge here */ |
| 2699 | com_backpatch(c, a); |
| 2700 | com_backpatch(c, b); |
| 2701 | com_addbyte(c, POP_TOP); |
| 2702 | } |
| 2703 | |
| 2704 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2705 | com_print_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2706 | { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2707 | int i = 1; |
| 2708 | node* stream = NULL; |
| 2709 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2710 | REQ(n, print_stmt); /* 'print' (test ',')* [test] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2711 | |
| 2712 | /* are we using the extended print form? */ |
| 2713 | if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) { |
| 2714 | stream = CHILD(n, 2); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2715 | com_node(c, stream); |
| 2716 | /* stack: [...] => [... stream] */ |
| 2717 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2718 | if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA) |
| 2719 | i = 4; |
| 2720 | else |
| 2721 | i = 3; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2722 | } |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2723 | for (; i < NCH(n); i += 2) { |
| 2724 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2725 | com_addbyte(c, DUP_TOP); |
| 2726 | /* stack: [stream] => [stream stream] */ |
| 2727 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2728 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2729 | /* stack: [stream stream] => [stream stream obj] */ |
| 2730 | com_addbyte(c, ROT_TWO); |
| 2731 | /* stack: [stream stream obj] => [stream obj stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2732 | com_addbyte(c, PRINT_ITEM_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2733 | /* stack: [stream obj stream] => [stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2734 | com_pop(c, 2); |
| 2735 | } |
| 2736 | else { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2737 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2738 | /* stack: [...] => [... obj] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2739 | com_addbyte(c, PRINT_ITEM); |
| 2740 | com_pop(c, 1); |
| 2741 | } |
| 2742 | } |
| 2743 | /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */ |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2744 | if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2745 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2746 | /* must pop the extra stream object off the stack */ |
| 2747 | com_addbyte(c, POP_TOP); |
| 2748 | /* stack: [... stream] => [...] */ |
| 2749 | com_pop(c, 1); |
| 2750 | } |
| 2751 | } |
| 2752 | else { |
| 2753 | if (stream != NULL) { |
| 2754 | /* this consumes the last stream object on stack */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2755 | com_addbyte(c, PRINT_NEWLINE_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2756 | /* stack: [... stream] => [...] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2757 | com_pop(c, 1); |
| 2758 | } |
| 2759 | else |
| 2760 | com_addbyte(c, PRINT_NEWLINE); |
| 2761 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
| 2764 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2765 | com_return_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2766 | { |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2767 | REQ(n, return_stmt); /* 'return' [testlist] */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2768 | if (!c->c_infunction) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2769 | com_error(c, PyExc_SyntaxError, "'return' outside function"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2770 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2771 | if (c->c_flags & CO_GENERATOR) { |
| 2772 | if (NCH(n) > 1) { |
| 2773 | com_error(c, PyExc_SyntaxError, |
| 2774 | "'return' with argument inside generator"); |
| 2775 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 2776 | } |
| 2777 | if (NCH(n) < 2) { |
| 2778 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2779 | com_push(c, 1); |
| 2780 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 2781 | else |
| 2782 | com_node(c, CHILD(n, 1)); |
| 2783 | com_addbyte(c, RETURN_VALUE); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2784 | com_pop(c, 1); |
| 2785 | } |
| 2786 | |
| 2787 | static void |
| 2788 | com_yield_stmt(struct compiling *c, node *n) |
| 2789 | { |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 2790 | int i; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2791 | REQ(n, yield_stmt); /* 'yield' testlist */ |
| 2792 | if (!c->c_infunction) { |
| 2793 | com_error(c, PyExc_SyntaxError, "'yield' outside function"); |
| 2794 | } |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 2795 | |
| 2796 | for (i = 0; i < c->c_nblocks; ++i) { |
| 2797 | if (c->c_block[i] == SETUP_FINALLY) { |
| 2798 | com_error(c, PyExc_SyntaxError, |
| 2799 | "'yield' not allowed in a 'try' block " |
| 2800 | "with a 'finally' clause"); |
| 2801 | return; |
| 2802 | } |
| 2803 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2804 | com_node(c, CHILD(n, 1)); |
| 2805 | com_addbyte(c, YIELD_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2806 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2807 | } |
| 2808 | |
| 2809 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2810 | com_raise_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2811 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2812 | int i; |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 2813 | REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */ |
| 2814 | if (NCH(n) > 1) { |
| 2815 | com_node(c, CHILD(n, 1)); |
| 2816 | if (NCH(n) > 3) { |
| 2817 | com_node(c, CHILD(n, 3)); |
| 2818 | if (NCH(n) > 5) |
| 2819 | com_node(c, CHILD(n, 5)); |
| 2820 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2821 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2822 | i = NCH(n)/2; |
| 2823 | com_addoparg(c, RAISE_VARARGS, i); |
| 2824 | com_pop(c, i); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
| 2827 | static void |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2828 | com_from_import(struct compiling *c, node *n) |
| 2829 | { |
| 2830 | com_addopname(c, IMPORT_FROM, CHILD(n, 0)); |
| 2831 | com_push(c, 1); |
| 2832 | if (NCH(n) > 1) { |
| 2833 | if (strcmp(STR(CHILD(n, 1)), "as") != 0) { |
| 2834 | com_error(c, PyExc_SyntaxError, "invalid syntax"); |
| 2835 | return; |
| 2836 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2837 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2838 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2839 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2840 | com_pop(c, 1); |
| 2841 | } |
| 2842 | |
| 2843 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2844 | com_import_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2845 | { |
| 2846 | int i; |
| 2847 | REQ(n, import_stmt); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2848 | /* 'import' dotted_name (',' dotted_name)* | |
| 2849 | 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2850 | if (STR(CHILD(n, 0))[0] == 'f') { |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 2851 | PyObject *tup; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2852 | /* 'from' dotted_name 'import' ... */ |
| 2853 | REQ(CHILD(n, 1), dotted_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2854 | |
| 2855 | if (TYPE(CHILD(n, 3)) == STAR) { |
| 2856 | tup = Py_BuildValue("(s)", "*"); |
| 2857 | } else { |
| 2858 | tup = PyTuple_New((NCH(n) - 2)/2); |
| 2859 | for (i = 3; i < NCH(n); i += 2) { |
| 2860 | PyTuple_SET_ITEM(tup, (i-3)/2, |
| 2861 | PyString_FromString(STR( |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 2862 | CHILD(CHILD(n, i), 0)))); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2863 | } |
| 2864 | } |
| 2865 | com_addoparg(c, LOAD_CONST, com_addconst(c, tup)); |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 2866 | Py_DECREF(tup); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2867 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2868 | com_addopname(c, IMPORT_NAME, CHILD(n, 1)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2869 | if (TYPE(CHILD(n, 3)) == STAR) |
| 2870 | com_addbyte(c, IMPORT_STAR); |
| 2871 | else { |
| 2872 | for (i = 3; i < NCH(n); i += 2) |
| 2873 | com_from_import(c, CHILD(n, i)); |
| 2874 | com_addbyte(c, POP_TOP); |
| 2875 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2876 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2877 | } |
| 2878 | else { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2879 | /* 'import' ... */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2880 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2881 | node *subn = CHILD(n, i); |
| 2882 | REQ(subn, dotted_as_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2883 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2884 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2885 | com_addopname(c, IMPORT_NAME, CHILD(subn, 0)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2886 | if (NCH(subn) > 1) { |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 2887 | int j; |
| 2888 | if (strcmp(STR(CHILD(subn, 1)), "as") != 0) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2889 | com_error(c, PyExc_SyntaxError, |
| 2890 | "invalid syntax"); |
| 2891 | return; |
| 2892 | } |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 2893 | for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2) |
| 2894 | com_addopname(c, LOAD_ATTR, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2895 | CHILD(CHILD(subn, 0), |
| 2896 | j)); |
| 2897 | com_addop_varname(c, VAR_STORE, |
| 2898 | STR(CHILD(subn, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2899 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2900 | com_addop_varname(c, VAR_STORE, |
| 2901 | STR(CHILD(CHILD(subn, 0), |
| 2902 | 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2903 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2904 | } |
| 2905 | } |
| 2906 | } |
| 2907 | |
| 2908 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2909 | com_exec_stmt(struct compiling *c, node *n) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2910 | { |
| 2911 | REQ(n, exec_stmt); |
| 2912 | /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */ |
| 2913 | com_node(c, CHILD(n, 1)); |
| 2914 | if (NCH(n) >= 4) |
| 2915 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2916 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2917 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2918 | com_push(c, 1); |
| 2919 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2920 | if (NCH(n) >= 6) |
| 2921 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2922 | else { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2923 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2924 | com_push(c, 1); |
| 2925 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2926 | com_addbyte(c, EXEC_STMT); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2927 | com_pop(c, 3); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2928 | } |
| 2929 | |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2930 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2931 | is_constant_false(struct compiling *c, node *n) |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2932 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2933 | PyObject *v; |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2934 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2935 | /* argument c will be NULL when called from symtable_node() */ |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2936 | |
| 2937 | /* Label to avoid tail recursion */ |
| 2938 | next: |
| 2939 | switch (TYPE(n)) { |
| 2940 | |
| 2941 | case suite: |
| 2942 | if (NCH(n) == 1) { |
| 2943 | n = CHILD(n, 0); |
| 2944 | goto next; |
| 2945 | } |
| 2946 | /* Fall through */ |
| 2947 | case file_input: |
| 2948 | for (i = 0; i < NCH(n); i++) { |
| 2949 | node *ch = CHILD(n, i); |
| 2950 | if (TYPE(ch) == stmt) { |
| 2951 | n = ch; |
| 2952 | goto next; |
| 2953 | } |
| 2954 | } |
| 2955 | break; |
| 2956 | |
| 2957 | case stmt: |
| 2958 | case simple_stmt: |
| 2959 | case small_stmt: |
| 2960 | n = CHILD(n, 0); |
| 2961 | goto next; |
| 2962 | |
| 2963 | case expr_stmt: |
| 2964 | case testlist: |
| 2965 | case test: |
| 2966 | case and_test: |
| 2967 | case not_test: |
| 2968 | case comparison: |
| 2969 | case expr: |
| 2970 | case xor_expr: |
| 2971 | case and_expr: |
| 2972 | case shift_expr: |
| 2973 | case arith_expr: |
| 2974 | case term: |
| 2975 | case factor: |
| 2976 | case power: |
| 2977 | case atom: |
| 2978 | if (NCH(n) == 1) { |
| 2979 | n = CHILD(n, 0); |
| 2980 | goto next; |
| 2981 | } |
| 2982 | break; |
| 2983 | |
| 2984 | case NAME: |
| 2985 | if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0) |
| 2986 | return 1; |
| 2987 | break; |
| 2988 | |
| 2989 | case NUMBER: |
| 2990 | v = parsenumber(c, STR(n)); |
| 2991 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2992 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2993 | break; |
| 2994 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2995 | i = PyObject_IsTrue(v); |
| 2996 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2997 | return i == 0; |
| 2998 | |
| 2999 | case STRING: |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3000 | v = parsestr(c, STR(n)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3001 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3002 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3003 | break; |
| 3004 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3005 | i = PyObject_IsTrue(v); |
| 3006 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3007 | return i == 0; |
| 3008 | |
| 3009 | } |
| 3010 | return 0; |
| 3011 | } |
| 3012 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3013 | |
| 3014 | /* Look under n for a return stmt with an expression. |
| 3015 | * This hack is used to find illegal returns under "if 0:" blocks in |
| 3016 | * functions already known to be generators (as determined by the symtable |
| 3017 | * pass). |
| 3018 | * Return the offending return node if found, else NULL. |
| 3019 | */ |
| 3020 | static node * |
| 3021 | look_for_offending_return(node *n) |
| 3022 | { |
| 3023 | int i; |
| 3024 | |
| 3025 | for (i = 0; i < NCH(n); ++i) { |
| 3026 | node *kid = CHILD(n, i); |
| 3027 | |
| 3028 | switch (TYPE(kid)) { |
| 3029 | case classdef: |
| 3030 | case funcdef: |
| 3031 | case lambdef: |
| 3032 | /* Stuff in nested functions & classes doesn't |
| 3033 | affect the code block we started in. */ |
| 3034 | return NULL; |
| 3035 | |
| 3036 | case return_stmt: |
| 3037 | if (NCH(kid) > 1) |
| 3038 | return kid; |
| 3039 | break; |
| 3040 | |
| 3041 | default: { |
| 3042 | node *bad = look_for_offending_return(kid); |
| 3043 | if (bad != NULL) |
| 3044 | return bad; |
| 3045 | } |
| 3046 | } |
| 3047 | } |
| 3048 | |
| 3049 | return NULL; |
| 3050 | } |
| 3051 | |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3052 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3053 | com_if_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3054 | { |
| 3055 | int i; |
| 3056 | int anchor = 0; |
| 3057 | REQ(n, if_stmt); |
| 3058 | /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */ |
| 3059 | for (i = 0; i+3 < NCH(n); i+=4) { |
| 3060 | int a = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3061 | node *ch = CHILD(n, i+1); |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3062 | if (is_constant_false(c, ch)) { |
| 3063 | /* We're going to skip this block. However, if this |
| 3064 | is a generator, we have to check the dead code |
| 3065 | anyway to make sure there aren't any return stmts |
| 3066 | with expressions, in the same scope. */ |
| 3067 | if (c->c_flags & CO_GENERATOR) { |
| 3068 | node *p = look_for_offending_return(n); |
| 3069 | if (p != NULL) { |
| 3070 | int savelineno = c->c_lineno; |
| 3071 | c->c_lineno = p->n_lineno; |
| 3072 | com_error(c, PyExc_SyntaxError, |
| 3073 | "'return' with argument " |
| 3074 | "inside generator"); |
| 3075 | c->c_lineno = savelineno; |
| 3076 | } |
| 3077 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3078 | continue; |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3079 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3080 | if (i > 0) |
| 3081 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3082 | com_node(c, ch); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3083 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 3084 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3085 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3086 | com_node(c, CHILD(n, i+3)); |
| 3087 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 3088 | com_backpatch(c, a); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3089 | /* We jump here with an extra entry which we now pop */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3090 | com_addbyte(c, POP_TOP); |
| 3091 | } |
| 3092 | if (i+2 < NCH(n)) |
| 3093 | com_node(c, CHILD(n, i+2)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3094 | if (anchor) |
| 3095 | com_backpatch(c, anchor); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3096 | } |
| 3097 | |
| 3098 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3099 | com_while_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3100 | { |
| 3101 | int break_anchor = 0; |
| 3102 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3103 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3104 | REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */ |
| 3105 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3106 | block_push(c, SETUP_LOOP); |
| 3107 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3108 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3109 | com_node(c, CHILD(n, 1)); |
| 3110 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 3111 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3112 | com_pop(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3113 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3114 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3115 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3116 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3117 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3118 | com_backpatch(c, anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3119 | /* We jump here with one entry more on the stack */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3120 | com_addbyte(c, POP_TOP); |
| 3121 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3122 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3123 | if (NCH(n) > 4) |
| 3124 | com_node(c, CHILD(n, 6)); |
| 3125 | com_backpatch(c, break_anchor); |
| 3126 | } |
| 3127 | |
| 3128 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3129 | com_for_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3130 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3131 | int break_anchor = 0; |
| 3132 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3133 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3134 | REQ(n, for_stmt); |
| 3135 | /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */ |
| 3136 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3137 | block_push(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3138 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3139 | com_addbyte(c, GET_ITER); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3140 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3141 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3142 | com_addfwref(c, FOR_ITER, &anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3143 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3144 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3145 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3146 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3147 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3148 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3149 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3150 | com_backpatch(c, anchor); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3151 | com_pop(c, 1); /* FOR_ITER has popped this */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3152 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3153 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3154 | if (NCH(n) > 8) |
| 3155 | com_node(c, CHILD(n, 8)); |
| 3156 | com_backpatch(c, break_anchor); |
| 3157 | } |
| 3158 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3159 | /* Code generated for "try: S finally: Sf" is as follows: |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3160 | |
| 3161 | SETUP_FINALLY L |
| 3162 | <code for S> |
| 3163 | POP_BLOCK |
| 3164 | LOAD_CONST <nil> |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3165 | L: <code for Sf> |
| 3166 | END_FINALLY |
| 3167 | |
| 3168 | The special instructions use the block stack. Each block |
| 3169 | stack entry contains the instruction that created it (here |
| 3170 | SETUP_FINALLY), the level of the value stack at the time the |
| 3171 | block stack entry was created, and a label (here L). |
| 3172 | |
| 3173 | SETUP_FINALLY: |
| 3174 | Pushes the current value stack level and the label |
| 3175 | onto the block stack. |
| 3176 | POP_BLOCK: |
| 3177 | Pops en entry from the block stack, and pops the value |
| 3178 | stack until its level is the same as indicated on the |
| 3179 | block stack. (The label is ignored.) |
| 3180 | END_FINALLY: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3181 | Pops a variable number of entries from the *value* stack |
| 3182 | and re-raises the exception they specify. The number of |
| 3183 | entries popped depends on the (pseudo) exception type. |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3184 | |
| 3185 | The block stack is unwound when an exception is raised: |
| 3186 | when a SETUP_FINALLY entry is found, the exception is pushed |
| 3187 | onto the value stack (and the exception condition is cleared), |
| 3188 | and the interpreter jumps to the label gotten from the block |
| 3189 | stack. |
| 3190 | |
| 3191 | 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] | 3192 | (The contents of the value stack is shown in [], with the top |
| 3193 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3194 | associated value, and 'exc' the exception.) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3195 | |
| 3196 | Value stack Label Instruction Argument |
| 3197 | [] SETUP_EXCEPT L1 |
| 3198 | [] <code for S> |
| 3199 | [] POP_BLOCK |
| 3200 | [] JUMP_FORWARD L0 |
| 3201 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3202 | [tb, val, exc] L1: DUP ) |
| 3203 | [tb, val, exc, exc] <evaluate E1> ) |
| 3204 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 3205 | [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 ) |
| 3206 | [tb, val, exc, 1] POP ) |
| 3207 | [tb, val, exc] POP |
| 3208 | [tb, val] <assign to V1> (or POP if no V1) |
| 3209 | [tb] POP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3210 | [] <code for S1> |
| 3211 | JUMP_FORWARD L0 |
| 3212 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3213 | [tb, val, exc, 0] L2: POP |
| 3214 | [tb, val, exc] DUP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3215 | .............................etc....................... |
| 3216 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3217 | [tb, val, exc, 0] Ln+1: POP |
| 3218 | [tb, val, exc] END_FINALLY # re-raise exception |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3219 | |
| 3220 | [] L0: <next statement> |
| 3221 | |
| 3222 | Of course, parts are not generated if Vi or Ei is not present. |
| 3223 | */ |
| 3224 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3225 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3226 | com_try_except(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3227 | { |
| 3228 | int except_anchor = 0; |
| 3229 | int end_anchor = 0; |
| 3230 | int else_anchor = 0; |
| 3231 | int i; |
| 3232 | node *ch; |
| 3233 | |
| 3234 | com_addfwref(c, SETUP_EXCEPT, &except_anchor); |
| 3235 | block_push(c, SETUP_EXCEPT); |
| 3236 | com_node(c, CHILD(n, 2)); |
| 3237 | com_addbyte(c, POP_BLOCK); |
| 3238 | block_pop(c, SETUP_EXCEPT); |
| 3239 | com_addfwref(c, JUMP_FORWARD, &else_anchor); |
| 3240 | com_backpatch(c, except_anchor); |
| 3241 | for (i = 3; |
| 3242 | i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause; |
| 3243 | i += 3) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3244 | /* except_clause: 'except' [expr [',' var]] */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3245 | if (except_anchor == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3246 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 3247 | "default 'except:' must be last"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3248 | break; |
| 3249 | } |
| 3250 | except_anchor = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3251 | com_push(c, 3); /* tb, val, exc pushed by exception */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3252 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 3253 | if (NCH(ch) > 1) { |
| 3254 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3255 | com_push(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3256 | com_node(c, CHILD(ch, 1)); |
| 3257 | com_addoparg(c, COMPARE_OP, EXC_MATCH); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3258 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3259 | com_addfwref(c, JUMP_IF_FALSE, &except_anchor); |
| 3260 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3261 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3262 | } |
| 3263 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3264 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3265 | if (NCH(ch) > 3) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3266 | com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3267 | else { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3268 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3269 | com_pop(c, 1); |
| 3270 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3271 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3272 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3273 | com_node(c, CHILD(n, i+2)); |
| 3274 | com_addfwref(c, JUMP_FORWARD, &end_anchor); |
| 3275 | if (except_anchor) { |
| 3276 | com_backpatch(c, except_anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3277 | /* We come in with [tb, val, exc, 0] on the |
| 3278 | stack; one pop and it's the same as |
| 3279 | expected at the start of the loop */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3280 | com_addbyte(c, POP_TOP); |
| 3281 | } |
| 3282 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3283 | /* We actually come in here with [tb, val, exc] but the |
| 3284 | END_FINALLY will zap those and jump around. |
| 3285 | The c_stacklevel does not reflect them so we need not pop |
| 3286 | anything. */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3287 | com_addbyte(c, END_FINALLY); |
| 3288 | com_backpatch(c, else_anchor); |
| 3289 | if (i < NCH(n)) |
| 3290 | com_node(c, CHILD(n, i+2)); |
| 3291 | com_backpatch(c, end_anchor); |
| 3292 | } |
| 3293 | |
| 3294 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3295 | com_try_finally(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3296 | { |
| 3297 | int finally_anchor = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3298 | node *ch; |
Guido van Rossum | 94fb82e | 1992-04-05 14:24:50 +0000 | [diff] [blame] | 3299 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3300 | com_addfwref(c, SETUP_FINALLY, &finally_anchor); |
| 3301 | block_push(c, SETUP_FINALLY); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3302 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3303 | com_addbyte(c, POP_BLOCK); |
| 3304 | block_pop(c, SETUP_FINALLY); |
| 3305 | block_push(c, END_FINALLY); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3306 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3307 | /* While the generated code pushes only one item, |
| 3308 | the try-finally handling can enter here with |
| 3309 | up to three items. OK, here are the details: |
| 3310 | 3 for an exception, 2 for RETURN, 1 for BREAK. */ |
| 3311 | com_push(c, 3); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3312 | com_backpatch(c, finally_anchor); |
| 3313 | ch = CHILD(n, NCH(n)-1); |
| 3314 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 3315 | com_node(c, ch); |
| 3316 | com_addbyte(c, END_FINALLY); |
| 3317 | block_pop(c, END_FINALLY); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3318 | com_pop(c, 3); /* Matches the com_push above */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3319 | } |
| 3320 | |
| 3321 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3322 | com_try_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3323 | { |
| 3324 | REQ(n, try_stmt); |
| 3325 | /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
| 3326 | | 'try' ':' suite 'finally' ':' suite */ |
| 3327 | if (TYPE(CHILD(n, 3)) != except_clause) |
| 3328 | com_try_finally(c, n); |
| 3329 | else |
| 3330 | com_try_except(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3331 | } |
| 3332 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3333 | static node * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3334 | get_rawdocstring(node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3335 | { |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3336 | int i; |
| 3337 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3338 | /* Label to avoid tail recursion */ |
| 3339 | next: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3340 | switch (TYPE(n)) { |
| 3341 | |
| 3342 | case suite: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3343 | if (NCH(n) == 1) { |
| 3344 | n = CHILD(n, 0); |
| 3345 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3346 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3347 | /* Fall through */ |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3348 | case file_input: |
| 3349 | for (i = 0; i < NCH(n); i++) { |
| 3350 | node *ch = CHILD(n, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3351 | if (TYPE(ch) == stmt) { |
| 3352 | n = ch; |
| 3353 | goto next; |
| 3354 | } |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3355 | } |
| 3356 | break; |
| 3357 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3358 | case stmt: |
| 3359 | case simple_stmt: |
| 3360 | case small_stmt: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3361 | n = CHILD(n, 0); |
| 3362 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3363 | |
| 3364 | case expr_stmt: |
| 3365 | case testlist: |
| 3366 | case test: |
| 3367 | case and_test: |
| 3368 | case not_test: |
| 3369 | case comparison: |
| 3370 | case expr: |
| 3371 | case xor_expr: |
| 3372 | case and_expr: |
| 3373 | case shift_expr: |
| 3374 | case arith_expr: |
| 3375 | case term: |
| 3376 | case factor: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3377 | case power: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3378 | if (NCH(n) == 1) { |
| 3379 | n = CHILD(n, 0); |
| 3380 | goto next; |
| 3381 | } |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3382 | break; |
| 3383 | |
| 3384 | case atom: |
| 3385 | if (TYPE(CHILD(n, 0)) == STRING) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3386 | return n; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3387 | break; |
| 3388 | |
| 3389 | } |
| 3390 | return NULL; |
| 3391 | } |
| 3392 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3393 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3394 | get_docstring(struct compiling *c, node *n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3395 | { |
Guido van Rossum | 541563e | 1999-01-28 15:08:09 +0000 | [diff] [blame] | 3396 | /* Don't generate doc-strings if run with -OO */ |
| 3397 | if (Py_OptimizeFlag > 1) |
| 3398 | return NULL; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3399 | n = get_rawdocstring(n); |
| 3400 | if (n == NULL) |
| 3401 | return NULL; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3402 | return parsestrplus(c, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3403 | } |
| 3404 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3405 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3406 | com_suite(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3407 | { |
| 3408 | REQ(n, suite); |
| 3409 | /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */ |
| 3410 | if (NCH(n) == 1) { |
| 3411 | com_node(c, CHILD(n, 0)); |
| 3412 | } |
| 3413 | else { |
| 3414 | int i; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3415 | for (i = 0; i < NCH(n) && c->c_errors == 0; i++) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3416 | node *ch = CHILD(n, i); |
| 3417 | if (TYPE(ch) == stmt) |
| 3418 | com_node(c, ch); |
| 3419 | } |
| 3420 | } |
| 3421 | } |
| 3422 | |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3423 | /* ARGSUSED */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3424 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3425 | com_continue_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3426 | { |
| 3427 | int i = c->c_nblocks; |
| 3428 | if (i-- > 0 && c->c_block[i] == SETUP_LOOP) { |
| 3429 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3430 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3431 | else if (i <= 0) { |
| 3432 | /* at the outer level */ |
| 3433 | com_error(c, PyExc_SyntaxError, |
| 3434 | "'continue' not properly in loop"); |
| 3435 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3436 | else { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3437 | int j; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3438 | for (j = i-1; j >= 0; --j) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3439 | if (c->c_block[j] == SETUP_LOOP) |
| 3440 | break; |
| 3441 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3442 | if (j >= 0) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3443 | /* there is a loop, but something interferes */ |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3444 | for (; i > j; --i) { |
| 3445 | if (c->c_block[i] == SETUP_EXCEPT || |
| 3446 | c->c_block[i] == SETUP_FINALLY) { |
| 3447 | com_addoparg(c, CONTINUE_LOOP, |
| 3448 | c->c_begin); |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3449 | return; |
| 3450 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3451 | if (c->c_block[i] == END_FINALLY) { |
| 3452 | com_error(c, PyExc_SyntaxError, |
| 3453 | "'continue' not supported inside 'finally' clause"); |
| 3454 | return; |
| 3455 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3456 | } |
| 3457 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3458 | com_error(c, PyExc_SyntaxError, |
| 3459 | "'continue' not properly in loop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3460 | } |
| 3461 | /* XXX Could allow it inside a 'finally' clause |
| 3462 | XXX if we could pop the exception still on the stack */ |
| 3463 | } |
| 3464 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3465 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3466 | com_argdefs(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3467 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3468 | int i, nch, nargs, ndefs; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3469 | if (TYPE(n) == lambdef) { |
| 3470 | /* lambdef: 'lambda' [varargslist] ':' test */ |
| 3471 | n = CHILD(n, 1); |
| 3472 | } |
| 3473 | else { |
| 3474 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */ |
| 3475 | n = CHILD(n, 2); |
| 3476 | REQ(n, parameters); /* parameters: '(' [varargslist] ')' */ |
| 3477 | n = CHILD(n, 1); |
| 3478 | } |
| 3479 | if (TYPE(n) != varargslist) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3480 | return 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3481 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3482 | (fpdef ['=' test] ',')* '*' ....... | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3483 | fpdef ['=' test] (',' fpdef ['=' test])* [','] */ |
| 3484 | nch = NCH(n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3485 | nargs = 0; |
| 3486 | ndefs = 0; |
| 3487 | for (i = 0; i < nch; i++) { |
| 3488 | int t; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3489 | if (TYPE(CHILD(n, i)) == STAR || |
| 3490 | TYPE(CHILD(n, i)) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3491 | break; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3492 | nargs++; |
| 3493 | i++; |
| 3494 | if (i >= nch) |
Guido van Rossum | a1e7e62 | 1995-09-18 21:44:04 +0000 | [diff] [blame] | 3495 | t = RPAR; /* Anything except EQUAL or COMMA */ |
| 3496 | else |
| 3497 | t = TYPE(CHILD(n, i)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3498 | if (t == EQUAL) { |
| 3499 | i++; |
| 3500 | ndefs++; |
| 3501 | com_node(c, CHILD(n, i)); |
| 3502 | i++; |
| 3503 | if (i >= nch) |
| 3504 | break; |
| 3505 | t = TYPE(CHILD(n, i)); |
| 3506 | } |
| 3507 | else { |
Guido van Rossum | 29d38cd | 1998-10-02 13:41:54 +0000 | [diff] [blame] | 3508 | /* Treat "(a=1, b)" as an error */ |
| 3509 | if (ndefs) |
| 3510 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | dfede31 | 1998-10-02 14:06:56 +0000 | [diff] [blame] | 3511 | "non-default argument follows default argument"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3512 | } |
| 3513 | if (t != COMMA) |
| 3514 | break; |
| 3515 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3516 | return ndefs; |
| 3517 | } |
| 3518 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3519 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3520 | com_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3521 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3522 | PyObject *co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3523 | int ndefs; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3524 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3525 | ndefs = com_argdefs(c, n); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3526 | symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n), |
| 3527 | n->n_lineno); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3528 | co = (PyObject *)icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3529 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3530 | if (co == NULL) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3531 | c->c_errors++; |
| 3532 | else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3533 | int closure = com_make_closure(c, (PyCodeObject *)co); |
| 3534 | int i = com_addconst(c, co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3535 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3536 | com_push(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3537 | if (closure) |
| 3538 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
| 3539 | else |
| 3540 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3541 | com_pop(c, ndefs); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3542 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3543 | com_pop(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3544 | Py_DECREF(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3545 | } |
| 3546 | } |
| 3547 | |
| 3548 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3549 | com_bases(struct compiling *c, node *n) |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3550 | { |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3551 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3552 | REQ(n, testlist); |
| 3553 | /* testlist: test (',' test)* [','] */ |
| 3554 | for (i = 0; i < NCH(n); i += 2) |
| 3555 | com_node(c, CHILD(n, i)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3556 | i = (NCH(n)+1) / 2; |
| 3557 | com_addoparg(c, BUILD_TUPLE, i); |
| 3558 | com_pop(c, i-1); |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3559 | } |
| 3560 | |
| 3561 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3562 | com_classdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3563 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3564 | int i; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3565 | PyObject *v; |
| 3566 | PyCodeObject *co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3567 | char *name; |
| 3568 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3569 | REQ(n, classdef); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3570 | /* classdef: class NAME ['(' testlist ')'] ':' suite */ |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 3571 | if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3572 | c->c_errors++; |
| 3573 | return; |
| 3574 | } |
| 3575 | /* Push the class name on the stack */ |
| 3576 | i = com_addconst(c, v); |
| 3577 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3578 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3579 | Py_DECREF(v); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3580 | /* Push the tuple of base classes on the stack */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3581 | if (TYPE(CHILD(n, 2)) != LPAR) { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3582 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3583 | com_push(c, 1); |
| 3584 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3585 | else |
| 3586 | com_bases(c, CHILD(n, 3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3587 | name = STR(CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3588 | symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3589 | co = icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3590 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3591 | if (co == NULL) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3592 | c->c_errors++; |
| 3593 | else { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3594 | int closure = com_make_closure(c, co); |
| 3595 | i = com_addconst(c, (PyObject *)co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3596 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3597 | com_push(c, 1); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3598 | if (closure) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3599 | com_addoparg(c, MAKE_CLOSURE, 0); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3600 | com_pop(c, PyTuple_GET_SIZE(co->co_freevars)); |
| 3601 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3602 | com_addoparg(c, MAKE_FUNCTION, 0); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3603 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3604 | com_addbyte(c, BUILD_CLASS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3605 | com_pop(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3606 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3607 | com_pop(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3608 | Py_DECREF(co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3609 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3610 | } |
| 3611 | |
| 3612 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3613 | com_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3614 | { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3615 | loop: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3616 | if (c->c_errors) |
| 3617 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3618 | switch (TYPE(n)) { |
| 3619 | |
| 3620 | /* Definition nodes */ |
| 3621 | |
| 3622 | case funcdef: |
| 3623 | com_funcdef(c, n); |
| 3624 | break; |
| 3625 | case classdef: |
| 3626 | com_classdef(c, n); |
| 3627 | break; |
| 3628 | |
| 3629 | /* Trivial parse tree nodes */ |
| 3630 | |
| 3631 | case stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3632 | case small_stmt: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3633 | case flow_stmt: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3634 | n = CHILD(n, 0); |
| 3635 | goto loop; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3636 | |
| 3637 | case simple_stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3638 | /* small_stmt (';' small_stmt)* [';'] NEWLINE */ |
| 3639 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 3640 | { |
| 3641 | int i; |
| 3642 | for (i = 0; i < NCH(n)-1; i += 2) |
| 3643 | com_node(c, CHILD(n, i)); |
| 3644 | } |
| 3645 | break; |
| 3646 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3647 | case compound_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3648 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3649 | n = CHILD(n, 0); |
| 3650 | goto loop; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3651 | |
| 3652 | /* Statement nodes */ |
| 3653 | |
| 3654 | case expr_stmt: |
| 3655 | com_expr_stmt(c, n); |
| 3656 | break; |
| 3657 | case print_stmt: |
| 3658 | com_print_stmt(c, n); |
| 3659 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3660 | case del_stmt: /* 'del' exprlist */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3661 | com_assign(c, CHILD(n, 1), OP_DELETE, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3662 | break; |
| 3663 | case pass_stmt: |
| 3664 | break; |
| 3665 | case break_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3666 | if (c->c_loops == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3667 | com_error(c, PyExc_SyntaxError, |
| 3668 | "'break' outside loop"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3669 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3670 | com_addbyte(c, BREAK_LOOP); |
| 3671 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3672 | case continue_stmt: |
| 3673 | com_continue_stmt(c, n); |
| 3674 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3675 | case return_stmt: |
| 3676 | com_return_stmt(c, n); |
| 3677 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3678 | case yield_stmt: |
| 3679 | com_yield_stmt(c, n); |
| 3680 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3681 | case raise_stmt: |
| 3682 | com_raise_stmt(c, n); |
| 3683 | break; |
| 3684 | case import_stmt: |
| 3685 | com_import_stmt(c, n); |
| 3686 | break; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3687 | case global_stmt: |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3688 | break; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3689 | case exec_stmt: |
| 3690 | com_exec_stmt(c, n); |
| 3691 | break; |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 3692 | case assert_stmt: |
| 3693 | com_assert_stmt(c, n); |
| 3694 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3695 | case if_stmt: |
| 3696 | com_if_stmt(c, n); |
| 3697 | break; |
| 3698 | case while_stmt: |
| 3699 | com_while_stmt(c, n); |
| 3700 | break; |
| 3701 | case for_stmt: |
| 3702 | com_for_stmt(c, n); |
| 3703 | break; |
| 3704 | case try_stmt: |
| 3705 | com_try_stmt(c, n); |
| 3706 | break; |
| 3707 | case suite: |
| 3708 | com_suite(c, n); |
| 3709 | break; |
| 3710 | |
| 3711 | /* Expression nodes */ |
| 3712 | |
| 3713 | case testlist: |
Guido van Rossum | 1c91707 | 2001-10-15 15:44:05 +0000 | [diff] [blame] | 3714 | case testlist_safe: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3715 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3716 | break; |
| 3717 | case test: |
| 3718 | com_test(c, n); |
| 3719 | break; |
| 3720 | case and_test: |
| 3721 | com_and_test(c, n); |
| 3722 | break; |
| 3723 | case not_test: |
| 3724 | com_not_test(c, n); |
| 3725 | break; |
| 3726 | case comparison: |
| 3727 | com_comparison(c, n); |
| 3728 | break; |
| 3729 | case exprlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3730 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3731 | break; |
| 3732 | case expr: |
| 3733 | com_expr(c, n); |
| 3734 | break; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 3735 | case xor_expr: |
| 3736 | com_xor_expr(c, n); |
| 3737 | break; |
| 3738 | case and_expr: |
| 3739 | com_and_expr(c, n); |
| 3740 | break; |
| 3741 | case shift_expr: |
| 3742 | com_shift_expr(c, n); |
| 3743 | break; |
| 3744 | case arith_expr: |
| 3745 | com_arith_expr(c, n); |
| 3746 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3747 | case term: |
| 3748 | com_term(c, n); |
| 3749 | break; |
| 3750 | case factor: |
| 3751 | com_factor(c, n); |
| 3752 | break; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3753 | case power: |
| 3754 | com_power(c, n); |
| 3755 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3756 | case atom: |
| 3757 | com_atom(c, n); |
| 3758 | break; |
| 3759 | |
| 3760 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3761 | com_error(c, PyExc_SystemError, |
| 3762 | "com_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3763 | } |
| 3764 | } |
| 3765 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 3766 | static void com_fplist(struct compiling *, node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3767 | |
| 3768 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3769 | com_fpdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3770 | { |
| 3771 | REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3772 | if (TYPE(CHILD(n, 0)) == LPAR) |
| 3773 | com_fplist(c, CHILD(n, 1)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3774 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3775 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3776 | com_pop(c, 1); |
| 3777 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3778 | } |
| 3779 | |
| 3780 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3781 | com_fplist(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3782 | { |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 3783 | REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3784 | if (NCH(n) == 1) { |
| 3785 | com_fpdef(c, CHILD(n, 0)); |
| 3786 | } |
| 3787 | else { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3788 | int i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 3789 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3790 | com_push(c, i-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3791 | for (i = 0; i < NCH(n); i += 2) |
| 3792 | com_fpdef(c, CHILD(n, i)); |
| 3793 | } |
| 3794 | } |
| 3795 | |
| 3796 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3797 | com_arglist(struct compiling *c, node *n) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3798 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3799 | int nch, i, narg; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3800 | int complex = 0; |
Guido van Rossum | db9e20f | 1997-07-10 01:06:53 +0000 | [diff] [blame] | 3801 | char nbuf[10]; |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3802 | REQ(n, varargslist); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 3803 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3804 | (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3805 | nch = NCH(n); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3806 | /* Enter all arguments in table of locals */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3807 | for (i = 0, narg = 0; i < nch; i++) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3808 | node *ch = CHILD(n, i); |
| 3809 | node *fp; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3810 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3811 | break; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3812 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3813 | fp = CHILD(ch, 0); |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 3814 | if (TYPE(fp) != NAME) { |
Guido van Rossum | db9e20f | 1997-07-10 01:06:53 +0000 | [diff] [blame] | 3815 | sprintf(nbuf, ".%d", i); |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 3816 | complex = 1; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3817 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3818 | narg++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3819 | /* all name updates handled by symtable */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3820 | if (++i >= nch) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3821 | break; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3822 | ch = CHILD(n, i); |
| 3823 | if (TYPE(ch) == EQUAL) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3824 | i += 2; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3825 | else |
| 3826 | REQ(ch, COMMA); |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3827 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3828 | if (complex) { |
| 3829 | /* Generate code for complex arguments only after |
| 3830 | having counted the simple arguments */ |
| 3831 | int ilocal = 0; |
| 3832 | for (i = 0; i < nch; i++) { |
| 3833 | node *ch = CHILD(n, i); |
| 3834 | node *fp; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3835 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3836 | break; |
| 3837 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3838 | fp = CHILD(ch, 0); |
| 3839 | if (TYPE(fp) != NAME) { |
| 3840 | com_addoparg(c, LOAD_FAST, ilocal); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3841 | com_push(c, 1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3842 | com_fpdef(c, ch); |
| 3843 | } |
| 3844 | ilocal++; |
| 3845 | if (++i >= nch) |
| 3846 | break; |
| 3847 | ch = CHILD(n, i); |
| 3848 | if (TYPE(ch) == EQUAL) |
| 3849 | i += 2; |
| 3850 | else |
| 3851 | REQ(ch, COMMA); |
| 3852 | } |
| 3853 | } |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3854 | } |
| 3855 | |
| 3856 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3857 | com_file_input(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3858 | { |
| 3859 | int i; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3860 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3861 | REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3862 | doc = get_docstring(c, n); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3863 | if (doc != NULL) { |
| 3864 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3865 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3866 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3867 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3868 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3869 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3870 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3871 | for (i = 0; i < NCH(n); i++) { |
| 3872 | node *ch = CHILD(n, i); |
| 3873 | if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE) |
| 3874 | com_node(c, ch); |
| 3875 | } |
| 3876 | } |
| 3877 | |
| 3878 | /* Top-level compile-node interface */ |
| 3879 | |
| 3880 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3881 | compile_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3882 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3883 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3884 | node *ch; |
| 3885 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3886 | c->c_name = STR(CHILD(n, 1)); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3887 | doc = get_docstring(c, CHILD(n, 4)); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3888 | if (doc != NULL) { |
| 3889 | (void) com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3890 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3891 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3892 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3893 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3894 | ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */ |
| 3895 | ch = CHILD(ch, 1); /* ')' | varargslist */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3896 | if (TYPE(ch) == varargslist) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3897 | com_arglist(c, ch); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3898 | c->c_infunction = 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3899 | com_node(c, CHILD(n, 4)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3900 | c->c_infunction = 0; |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 3901 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
| 3902 | com_push(c, 1); |
| 3903 | com_addbyte(c, RETURN_VALUE); |
| 3904 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3905 | } |
| 3906 | |
| 3907 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3908 | compile_lambdef(struct compiling *c, node *n) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 3909 | { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3910 | node *ch; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3911 | REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */ |
Guido van Rossum | 363ac7d | 1994-11-10 22:40:34 +0000 | [diff] [blame] | 3912 | c->c_name = "<lambda>"; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3913 | |
| 3914 | ch = CHILD(n, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3915 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3916 | if (TYPE(ch) == varargslist) { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3917 | com_arglist(c, ch); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3918 | ch = CHILD(n, 3); |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3919 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3920 | else |
| 3921 | ch = CHILD(n, 2); |
| 3922 | com_node(c, ch); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 3923 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3924 | com_pop(c, 1); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
| 3927 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3928 | compile_classdef(struct compiling *c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3929 | { |
| 3930 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3931 | PyObject *doc; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3932 | REQ(n, classdef); |
| 3933 | /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ |
| 3934 | c->c_name = STR(CHILD(n, 1)); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 3935 | c->c_private = c->c_name; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3936 | ch = CHILD(n, NCH(n)-1); /* The suite */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3937 | doc = get_docstring(c, ch); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3938 | if (doc != NULL) { |
| 3939 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3940 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3941 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3942 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3943 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3944 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3945 | } |
| 3946 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3947 | (void) com_addconst(c, Py_None); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3948 | com_node(c, ch); |
| 3949 | com_addbyte(c, LOAD_LOCALS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3950 | com_push(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3951 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3952 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3953 | } |
| 3954 | |
| 3955 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3956 | compile_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3957 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3958 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 3959 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3960 | switch (TYPE(n)) { |
| 3961 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3962 | case single_input: /* One interactive command */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3963 | /* NEWLINE | simple_stmt | compound_stmt NEWLINE */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3964 | c->c_interactive++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3965 | n = CHILD(n, 0); |
| 3966 | if (TYPE(n) != NEWLINE) |
| 3967 | com_node(c, n); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3968 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3969 | com_push(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3970 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3971 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3972 | c->c_interactive--; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3973 | break; |
| 3974 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3975 | case file_input: /* A whole file, or built-in function exec() */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3976 | com_file_input(c, n); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3977 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3978 | com_push(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3979 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3980 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3981 | break; |
| 3982 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3983 | case eval_input: /* Built-in function input() */ |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3984 | com_node(c, CHILD(n, 0)); |
| 3985 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3986 | com_pop(c, 1); |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3987 | break; |
| 3988 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3989 | case lambdef: /* anonymous function definition */ |
| 3990 | compile_lambdef(c, n); |
| 3991 | break; |
| 3992 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3993 | case funcdef: /* A function definition */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3994 | compile_funcdef(c, n); |
| 3995 | break; |
| 3996 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3997 | case classdef: /* A class definition */ |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3998 | compile_classdef(c, n); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3999 | break; |
| 4000 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4001 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4002 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 4003 | "compile_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4004 | } |
| 4005 | } |
| 4006 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4007 | static PyObject * |
| 4008 | dict_keys_inorder(PyObject *dict, int offset) |
| 4009 | { |
| 4010 | PyObject *tuple, *k, *v; |
| 4011 | int i, pos = 0, size = PyDict_Size(dict); |
| 4012 | |
| 4013 | tuple = PyTuple_New(size); |
| 4014 | if (tuple == NULL) |
| 4015 | return NULL; |
| 4016 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 4017 | i = PyInt_AS_LONG(v); |
| 4018 | Py_INCREF(k); |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4019 | assert((i - offset) < size); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4020 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 4021 | } |
| 4022 | return tuple; |
| 4023 | } |
| 4024 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4025 | PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4026 | PyNode_Compile(node *n, char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4027 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4028 | return PyNode_CompileFlags(n, filename, NULL); |
| 4029 | } |
| 4030 | |
| 4031 | PyCodeObject * |
| 4032 | PyNode_CompileFlags(node *n, char *filename, PyCompilerFlags *flags) |
| 4033 | { |
| 4034 | return jcompile(n, filename, NULL, flags); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4035 | } |
| 4036 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4037 | struct symtable * |
| 4038 | PyNode_CompileSymtable(node *n, char *filename) |
| 4039 | { |
| 4040 | struct symtable *st; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4041 | PyFutureFeatures *ff; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4042 | |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4043 | ff = PyNode_Future(n, filename); |
| 4044 | if (ff == NULL) |
| 4045 | return NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4046 | st = symtable_init(); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4047 | if (st == NULL) |
| 4048 | return NULL; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4049 | st->st_future = ff; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4050 | symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4051 | if (st->st_errors > 0) |
| 4052 | goto fail; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4053 | symtable_node(st, n); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4054 | if (st->st_errors > 0) |
| 4055 | goto fail; |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4056 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4057 | return st; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4058 | fail: |
| 4059 | PyMem_Free((void *)ff); |
| 4060 | st->st_future = NULL; |
| 4061 | PySymtable_Free(st); |
| 4062 | return NULL; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4063 | } |
| 4064 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4065 | static PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4066 | icompile(node *n, struct compiling *base) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4067 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4068 | return jcompile(n, base->c_filename, base, NULL); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4069 | } |
| 4070 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4071 | static PyCodeObject * |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4072 | jcompile(node *n, char *filename, struct compiling *base, |
| 4073 | PyCompilerFlags *flags) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4074 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4075 | struct compiling sc; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4076 | PyCodeObject *co; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4077 | if (!com_init(&sc, filename)) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4078 | return NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4079 | if (base) { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4080 | sc.c_private = base->c_private; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4081 | sc.c_symtable = base->c_symtable; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4082 | /* c_symtable still points to parent's symbols */ |
| 4083 | if (base->c_nested |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4084 | || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4085 | sc.c_nested = 1; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 4086 | sc.c_flags |= base->c_flags & PyCF_MASK; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4087 | } else { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4088 | sc.c_private = NULL; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 4089 | sc.c_future = PyNode_Future(n, filename); |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4090 | if (sc.c_future == NULL) { |
| 4091 | com_free(&sc); |
| 4092 | return NULL; |
| 4093 | } |
Jeremy Hylton | 481081e | 2001-08-14 20:01:59 +0000 | [diff] [blame] | 4094 | if (flags) { |
| 4095 | int merged = sc.c_future->ff_features | |
| 4096 | flags->cf_flags; |
| 4097 | sc.c_future->ff_features = merged; |
| 4098 | flags->cf_flags = merged; |
| 4099 | } |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4100 | if (symtable_build(&sc, n) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4101 | com_free(&sc); |
| 4102 | return NULL; |
| 4103 | } |
| 4104 | } |
| 4105 | co = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4106 | if (symtable_load_symbols(&sc) < 0) { |
| 4107 | sc.c_errors++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4108 | goto exit; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4109 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4110 | compile_node(&sc, n); |
| 4111 | com_done(&sc); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 4112 | if (sc.c_errors == 0) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4113 | PyObject *consts, *names, *varnames, *filename, *name, |
| 4114 | *freevars, *cellvars; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4115 | consts = PyList_AsTuple(sc.c_consts); |
| 4116 | names = PyList_AsTuple(sc.c_names); |
| 4117 | varnames = PyList_AsTuple(sc.c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4118 | cellvars = dict_keys_inorder(sc.c_cellvars, 0); |
| 4119 | freevars = dict_keys_inorder(sc.c_freevars, |
| 4120 | PyTuple_GET_SIZE(cellvars)); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 4121 | filename = PyString_InternFromString(sc.c_filename); |
| 4122 | name = PyString_InternFromString(sc.c_name); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4123 | if (!PyErr_Occurred()) |
| 4124 | co = PyCode_New(sc.c_argcount, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4125 | sc.c_nlocals, |
| 4126 | sc.c_maxstacklevel, |
| 4127 | sc.c_flags, |
| 4128 | sc.c_code, |
| 4129 | consts, |
| 4130 | names, |
| 4131 | varnames, |
| 4132 | freevars, |
| 4133 | cellvars, |
| 4134 | filename, |
| 4135 | name, |
| 4136 | sc.c_firstlineno, |
| 4137 | sc.c_lnotab); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4138 | Py_XDECREF(consts); |
| 4139 | Py_XDECREF(names); |
| 4140 | Py_XDECREF(varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4141 | Py_XDECREF(freevars); |
| 4142 | Py_XDECREF(cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4143 | Py_XDECREF(filename); |
| 4144 | Py_XDECREF(name); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 4145 | } |
Guido van Rossum | 6c2f0c7 | 2000-08-07 19:22:43 +0000 | [diff] [blame] | 4146 | else if (!PyErr_Occurred()) { |
| 4147 | /* This could happen if someone called PyErr_Clear() after an |
| 4148 | error was reported above. That's not supposed to happen, |
| 4149 | but I just plugged one case and I'm not sure there can't be |
| 4150 | others. In that case, raise SystemError so that at least |
| 4151 | it gets reported instead dumping core. */ |
| 4152 | PyErr_SetString(PyExc_SystemError, "lost syntax error"); |
| 4153 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4154 | exit: |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4155 | if (base == NULL) { |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4156 | PySymtable_Free(sc.c_symtable); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4157 | sc.c_symtable = NULL; |
| 4158 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4159 | com_free(&sc); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4160 | return co; |
| 4161 | } |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4162 | |
| 4163 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4164 | PyCode_Addr2Line(PyCodeObject *co, int addrq) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4165 | { |
| 4166 | int size = PyString_Size(co->co_lnotab) / 2; |
Guido van Rossum | 2174dcb | 1999-09-15 22:48:09 +0000 | [diff] [blame] | 4167 | unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4168 | int line = co->co_firstlineno; |
| 4169 | int addr = 0; |
| 4170 | while (--size >= 0) { |
| 4171 | addr += *p++; |
| 4172 | if (addr > addrq) |
| 4173 | break; |
| 4174 | line += *p++; |
| 4175 | } |
| 4176 | return line; |
| 4177 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4178 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4179 | /* The test for LOCAL must come before the test for FREE in order to |
| 4180 | handle classes where name is both local and free. The local var is |
| 4181 | a method and the free var is a free var referenced within a method. |
| 4182 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4183 | |
| 4184 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4185 | get_ref_type(struct compiling *c, char *name) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4186 | { |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4187 | char buf[350]; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4188 | PyObject *v; |
Jeremy Hylton | 5125773 | 2001-03-01 00:42:55 +0000 | [diff] [blame] | 4189 | |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4190 | if (PyDict_GetItemString(c->c_cellvars, name) != NULL) |
| 4191 | return CELL; |
| 4192 | if (PyDict_GetItemString(c->c_locals, name) != NULL) |
| 4193 | return LOCAL; |
| 4194 | if (PyDict_GetItemString(c->c_freevars, name) != NULL) |
| 4195 | return FREE; |
| 4196 | v = PyDict_GetItemString(c->c_globals, name); |
| 4197 | if (v) { |
| 4198 | if (v == Py_None) |
| 4199 | return GLOBAL_EXPLICIT; |
| 4200 | else { |
| 4201 | return GLOBAL_IMPLICIT; |
| 4202 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4203 | } |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4204 | sprintf(buf, |
| 4205 | "unknown scope for %.100s in %.100s(%s) " |
| 4206 | "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n", |
| 4207 | name, c->c_name, |
| 4208 | PyObject_REPR(c->c_symtable->st_cur->ste_id), |
| 4209 | c->c_filename, |
| 4210 | PyObject_REPR(c->c_symtable->st_cur->ste_symbols), |
| 4211 | PyObject_REPR(c->c_locals), |
| 4212 | PyObject_REPR(c->c_globals) |
| 4213 | ); |
| 4214 | |
| 4215 | Py_FatalError(buf); |
| 4216 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4217 | } |
| 4218 | |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4219 | /* Helper functions to issue warnings */ |
| 4220 | |
| 4221 | static int |
| 4222 | issue_warning(char *msg, char *filename, int lineno) |
| 4223 | { |
| 4224 | if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename, |
| 4225 | lineno, NULL, NULL) < 0) { |
| 4226 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
| 4227 | PyErr_SetString(PyExc_SyntaxError, msg); |
| 4228 | PyErr_SyntaxLocation(filename, lineno); |
| 4229 | } |
| 4230 | return -1; |
| 4231 | } |
| 4232 | return 0; |
| 4233 | } |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4234 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4235 | static int |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4236 | symtable_warn(struct symtable *st, char *msg) |
| 4237 | { |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4238 | 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] | 4239 | st->st_errors++; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4240 | return -1; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4241 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4242 | return 0; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4243 | } |
| 4244 | |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 4245 | /* Helper function for setting lineno and filename */ |
| 4246 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4247 | static int |
| 4248 | symtable_build(struct compiling *c, node *n) |
| 4249 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4250 | if ((c->c_symtable = symtable_init()) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4251 | return -1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 4252 | c->c_symtable->st_future = c->c_future; |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4253 | c->c_symtable->st_filename = c->c_filename; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4254 | symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno); |
| 4255 | if (c->c_symtable->st_errors > 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4256 | return -1; |
| 4257 | symtable_node(c->c_symtable, n); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4258 | if (c->c_symtable->st_errors > 0) |
| 4259 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4260 | /* reset for second pass */ |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4261 | c->c_symtable->st_nscopes = 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4262 | c->c_symtable->st_pass = 2; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4263 | return 0; |
| 4264 | } |
| 4265 | |
| 4266 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4267 | symtable_init_compiling_symbols(struct compiling *c) |
| 4268 | { |
| 4269 | PyObject *varnames; |
| 4270 | |
| 4271 | varnames = c->c_symtable->st_cur->ste_varnames; |
| 4272 | if (varnames == NULL) { |
| 4273 | varnames = PyList_New(0); |
| 4274 | if (varnames == NULL) |
| 4275 | return -1; |
| 4276 | c->c_symtable->st_cur->ste_varnames = varnames; |
| 4277 | Py_INCREF(varnames); |
| 4278 | } else |
| 4279 | Py_INCREF(varnames); |
| 4280 | c->c_varnames = varnames; |
| 4281 | |
| 4282 | c->c_globals = PyDict_New(); |
| 4283 | if (c->c_globals == NULL) |
| 4284 | return -1; |
| 4285 | c->c_freevars = PyDict_New(); |
| 4286 | if (c->c_freevars == NULL) |
| 4287 | return -1; |
| 4288 | c->c_cellvars = PyDict_New(); |
| 4289 | if (c->c_cellvars == NULL) |
| 4290 | return -1; |
| 4291 | return 0; |
| 4292 | } |
| 4293 | |
| 4294 | struct symbol_info { |
| 4295 | int si_nlocals; |
| 4296 | int si_ncells; |
| 4297 | int si_nfrees; |
| 4298 | int si_nimplicit; |
| 4299 | }; |
| 4300 | |
| 4301 | static void |
| 4302 | symtable_init_info(struct symbol_info *si) |
| 4303 | { |
| 4304 | si->si_nlocals = 0; |
| 4305 | si->si_ncells = 0; |
| 4306 | si->si_nfrees = 0; |
| 4307 | si->si_nimplicit = 0; |
| 4308 | } |
| 4309 | |
| 4310 | static int |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4311 | symtable_resolve_free(struct compiling *c, PyObject *name, int flags, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4312 | struct symbol_info *si) |
| 4313 | { |
| 4314 | PyObject *dict, *v; |
| 4315 | |
| 4316 | /* Seperate logic for DEF_FREE. If it occurs in a function, |
| 4317 | it indicates a local that we must allocate storage for (a |
| 4318 | cell var). If it occurs in a class, then the class has a |
| 4319 | method and a free variable with the same name. |
| 4320 | */ |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4321 | if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4322 | /* If it isn't declared locally, it can't be a cell. */ |
| 4323 | if (!(flags & (DEF_LOCAL | DEF_PARAM))) |
| 4324 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4325 | v = PyInt_FromLong(si->si_ncells++); |
| 4326 | dict = c->c_cellvars; |
| 4327 | } else { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4328 | /* If it is free anyway, then there is no need to do |
| 4329 | anything here. |
| 4330 | */ |
| 4331 | if (is_free(flags ^ DEF_FREE_CLASS) |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4332 | || (flags == DEF_FREE_CLASS)) |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4333 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4334 | v = PyInt_FromLong(si->si_nfrees++); |
| 4335 | dict = c->c_freevars; |
| 4336 | } |
| 4337 | if (v == NULL) |
| 4338 | return -1; |
| 4339 | if (PyDict_SetItem(dict, name, v) < 0) { |
| 4340 | Py_DECREF(v); |
| 4341 | return -1; |
| 4342 | } |
| 4343 | Py_DECREF(v); |
| 4344 | return 0; |
| 4345 | } |
| 4346 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4347 | /* If a variable is a cell and an argument, make sure that appears in |
| 4348 | co_cellvars before any variable to its right in varnames. |
| 4349 | */ |
| 4350 | |
| 4351 | |
| 4352 | static int |
| 4353 | symtable_cellvar_offsets(PyObject **cellvars, int argcount, |
| 4354 | PyObject *varnames, int flags) |
| 4355 | { |
| 4356 | PyObject *v, *w, *d, *list = NULL; |
| 4357 | int i, pos; |
| 4358 | |
| 4359 | if (flags & CO_VARARGS) |
| 4360 | argcount++; |
| 4361 | if (flags & CO_VARKEYWORDS) |
| 4362 | argcount++; |
| 4363 | for (i = argcount; --i >= 0; ) { |
| 4364 | v = PyList_GET_ITEM(varnames, i); |
| 4365 | if (PyDict_GetItem(*cellvars, v)) { |
| 4366 | if (list == NULL) { |
| 4367 | list = PyList_New(1); |
| 4368 | if (list == NULL) |
| 4369 | return -1; |
| 4370 | PyList_SET_ITEM(list, 0, v); |
| 4371 | Py_INCREF(v); |
| 4372 | } else |
| 4373 | PyList_Insert(list, 0, v); |
| 4374 | } |
| 4375 | } |
| 4376 | if (list == NULL || PyList_GET_SIZE(list) == 0) |
| 4377 | return 0; |
| 4378 | /* There are cellvars that are also arguments. Create a dict |
| 4379 | to replace cellvars and put the args at the front. |
| 4380 | */ |
| 4381 | d = PyDict_New(); |
| 4382 | for (i = PyList_GET_SIZE(list); --i >= 0; ) { |
| 4383 | v = PyInt_FromLong(i); |
| 4384 | if (v == NULL) |
| 4385 | goto fail; |
| 4386 | if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0) |
| 4387 | goto fail; |
| 4388 | if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0) |
| 4389 | goto fail; |
| 4390 | } |
| 4391 | pos = 0; |
| 4392 | i = PyList_GET_SIZE(list); |
| 4393 | Py_DECREF(list); |
| 4394 | while (PyDict_Next(*cellvars, &pos, &v, &w)) { |
| 4395 | w = PyInt_FromLong(i++); /* don't care about the old key */ |
| 4396 | if (PyDict_SetItem(d, v, w) < 0) { |
| 4397 | Py_DECREF(w); |
| 4398 | goto fail; |
| 4399 | } |
| 4400 | Py_DECREF(w); |
| 4401 | } |
| 4402 | Py_DECREF(*cellvars); |
| 4403 | *cellvars = d; |
| 4404 | return 1; |
| 4405 | fail: |
| 4406 | Py_DECREF(d); |
| 4407 | return -1; |
| 4408 | } |
| 4409 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4410 | static int |
| 4411 | symtable_freevar_offsets(PyObject *freevars, int offset) |
| 4412 | { |
| 4413 | PyObject *name, *v; |
| 4414 | int pos; |
| 4415 | |
| 4416 | /* The cell vars are the first elements of the closure, |
| 4417 | followed by the free vars. Update the offsets in |
| 4418 | c_freevars to account for number of cellvars. */ |
| 4419 | pos = 0; |
| 4420 | while (PyDict_Next(freevars, &pos, &name, &v)) { |
| 4421 | int i = PyInt_AS_LONG(v) + offset; |
| 4422 | PyObject *o = PyInt_FromLong(i); |
| 4423 | if (o == NULL) |
| 4424 | return -1; |
| 4425 | if (PyDict_SetItem(freevars, name, o) < 0) { |
| 4426 | Py_DECREF(o); |
| 4427 | return -1; |
| 4428 | } |
| 4429 | Py_DECREF(o); |
| 4430 | } |
| 4431 | return 0; |
| 4432 | } |
| 4433 | |
| 4434 | static int |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4435 | symtable_check_unoptimized(struct compiling *c, |
| 4436 | PySymtableEntryObject *ste, |
| 4437 | struct symbol_info *si) |
| 4438 | { |
| 4439 | char buf[300]; |
| 4440 | |
| 4441 | if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free |
| 4442 | || (ste->ste_nested && si->si_nimplicit))) |
| 4443 | return 0; |
| 4444 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4445 | #define ILLEGAL_CONTAINS "contains a nested function with free variables" |
| 4446 | |
| 4447 | #define ILLEGAL_IS "is a nested function" |
| 4448 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4449 | #define ILLEGAL_IMPORT_STAR \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4450 | "import * is not allowed in function '%.100s' because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4451 | |
| 4452 | #define ILLEGAL_BARE_EXEC \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4453 | "unqualified exec is not allowed in function '%.100s' it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4454 | |
| 4455 | #define ILLEGAL_EXEC_AND_IMPORT_STAR \ |
| 4456 | "function '%.100s' uses import * and bare exec, which are illegal" \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4457 | "because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4458 | |
| 4459 | /* XXX perhaps the linenos for these opt-breaking statements |
| 4460 | should be stored so the exception can point to them. */ |
| 4461 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4462 | if (ste->ste_child_free) { |
| 4463 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
| 4464 | sprintf(buf, ILLEGAL_IMPORT_STAR, |
| 4465 | PyString_AS_STRING(ste->ste_name), |
| 4466 | ILLEGAL_CONTAINS); |
| 4467 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
| 4468 | sprintf(buf, ILLEGAL_BARE_EXEC, |
| 4469 | PyString_AS_STRING(ste->ste_name), |
| 4470 | ILLEGAL_CONTAINS); |
| 4471 | else { |
| 4472 | sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 4473 | PyString_AS_STRING(ste->ste_name), |
| 4474 | ILLEGAL_CONTAINS); |
| 4475 | } |
| 4476 | } else { |
| 4477 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
| 4478 | sprintf(buf, ILLEGAL_IMPORT_STAR, |
| 4479 | PyString_AS_STRING(ste->ste_name), |
| 4480 | ILLEGAL_IS); |
| 4481 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
| 4482 | sprintf(buf, ILLEGAL_BARE_EXEC, |
| 4483 | PyString_AS_STRING(ste->ste_name), |
| 4484 | ILLEGAL_IS); |
| 4485 | else { |
| 4486 | sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 4487 | PyString_AS_STRING(ste->ste_name), |
| 4488 | ILLEGAL_IS); |
| 4489 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4490 | } |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4491 | |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4492 | PyErr_SetString(PyExc_SyntaxError, buf); |
| 4493 | PyErr_SyntaxLocation(c->c_symtable->st_filename, |
| 4494 | ste->ste_opt_lineno); |
| 4495 | return -1; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4496 | } |
| 4497 | |
| 4498 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4499 | symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste, |
| 4500 | struct symbol_info *si) |
| 4501 | { |
Jeremy Hylton | b857ba2 | 2001-08-10 21:41:33 +0000 | [diff] [blame] | 4502 | if (c->c_future) |
| 4503 | c->c_flags |= c->c_future->ff_features; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4504 | if (ste->ste_generator) |
| 4505 | c->c_flags |= CO_GENERATOR; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4506 | if (ste->ste_type != TYPE_MODULE) |
| 4507 | c->c_flags |= CO_NEWLOCALS; |
| 4508 | if (ste->ste_type == TYPE_FUNCTION) { |
| 4509 | c->c_nlocals = si->si_nlocals; |
| 4510 | if (ste->ste_optimized == 0) |
| 4511 | c->c_flags |= CO_OPTIMIZED; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4512 | else if (ste->ste_optimized != OPT_EXEC) |
| 4513 | return symtable_check_unoptimized(c, ste, si); |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4514 | } |
| 4515 | return 0; |
| 4516 | } |
| 4517 | |
| 4518 | static int |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4519 | symtable_load_symbols(struct compiling *c) |
| 4520 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4521 | static PyObject *implicit = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4522 | struct symtable *st = c->c_symtable; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4523 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4524 | PyObject *name, *varnames, *v; |
| 4525 | int i, flags, pos; |
| 4526 | struct symbol_info si; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4527 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4528 | if (implicit == NULL) { |
| 4529 | implicit = PyInt_FromLong(1); |
| 4530 | if (implicit == NULL) |
| 4531 | return -1; |
| 4532 | } |
| 4533 | v = NULL; |
| 4534 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4535 | if (symtable_init_compiling_symbols(c) < 0) |
| 4536 | goto fail; |
| 4537 | symtable_init_info(&si); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4538 | varnames = st->st_cur->ste_varnames; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4539 | si.si_nlocals = PyList_GET_SIZE(varnames); |
| 4540 | c->c_argcount = si.si_nlocals; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4541 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4542 | for (i = 0; i < si.si_nlocals; ++i) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4543 | v = PyInt_FromLong(i); |
| 4544 | if (PyDict_SetItem(c->c_locals, |
| 4545 | PyList_GET_ITEM(varnames, i), v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4546 | goto fail; |
| 4547 | Py_DECREF(v); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4548 | } |
| 4549 | |
| 4550 | /* XXX The cases below define the rules for whether a name is |
| 4551 | local or global. The logic could probably be clearer. */ |
| 4552 | pos = 0; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4553 | while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) { |
| 4554 | flags = PyInt_AS_LONG(v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4555 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4556 | if (flags & DEF_FREE_GLOBAL) |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4557 | /* undo the original DEF_FREE */ |
| 4558 | flags &= ~(DEF_FREE | DEF_FREE_CLASS); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4559 | |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4560 | /* Deal with names that need two actions: |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4561 | 1. Cell variables that are also locals. |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4562 | 2. Free variables in methods that are also class |
| 4563 | variables or declared global. |
| 4564 | */ |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4565 | if (flags & (DEF_FREE | DEF_FREE_CLASS)) |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4566 | symtable_resolve_free(c, name, flags, &si); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4567 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4568 | if (flags & DEF_STAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4569 | c->c_argcount--; |
| 4570 | c->c_flags |= CO_VARARGS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4571 | } else if (flags & DEF_DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4572 | c->c_argcount--; |
| 4573 | c->c_flags |= CO_VARKEYWORDS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4574 | } else if (flags & DEF_INTUPLE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4575 | c->c_argcount--; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4576 | else if (flags & DEF_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4577 | if (flags & DEF_PARAM) { |
| 4578 | PyErr_Format(PyExc_SyntaxError, LOCAL_GLOBAL, |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4579 | PyString_AS_STRING(name)); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 4580 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4581 | ste->ste_lineno); |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4582 | st->st_errors++; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4583 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4584 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4585 | if (PyDict_SetItem(c->c_globals, name, Py_None) < 0) |
| 4586 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4587 | } else if (flags & DEF_FREE_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4588 | si.si_nimplicit++; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4589 | if (PyDict_SetItem(c->c_globals, name, implicit) < 0) |
| 4590 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4591 | } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4592 | v = PyInt_FromLong(si.si_nlocals++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4593 | if (v == NULL) |
| 4594 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4595 | if (PyDict_SetItem(c->c_locals, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4596 | goto fail; |
| 4597 | Py_DECREF(v); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4598 | if (ste->ste_type != TYPE_CLASS) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4599 | if (PyList_Append(c->c_varnames, name) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4600 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4601 | } else if (is_free(flags)) { |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4602 | if (ste->ste_nested) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4603 | v = PyInt_FromLong(si.si_nfrees++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4604 | if (v == NULL) |
| 4605 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4606 | if (PyDict_SetItem(c->c_freevars, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4607 | goto fail; |
| 4608 | Py_DECREF(v); |
| 4609 | } else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4610 | si.si_nimplicit++; |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4611 | if (PyDict_SetItem(c->c_globals, name, |
| 4612 | implicit) < 0) |
| 4613 | goto fail; |
| 4614 | if (st->st_nscopes != 1) { |
| 4615 | v = PyInt_FromLong(flags); |
| 4616 | if (PyDict_SetItem(st->st_global, |
| 4617 | name, v)) |
| 4618 | goto fail; |
| 4619 | Py_DECREF(v); |
| 4620 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4621 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4622 | } |
| 4623 | } |
| 4624 | |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4625 | assert(PyDict_Size(c->c_freevars) == si.si_nfrees); |
| 4626 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4627 | if (si.si_ncells > 1) { /* one cell is always in order */ |
| 4628 | if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount, |
| 4629 | c->c_varnames, c->c_flags) < 0) |
| 4630 | return -1; |
| 4631 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4632 | if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0) |
| 4633 | return -1; |
| 4634 | return symtable_update_flags(c, ste, &si); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4635 | fail: |
| 4636 | /* is this always the right thing to do? */ |
| 4637 | Py_XDECREF(v); |
| 4638 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
| 4641 | static struct symtable * |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4642 | symtable_init() |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4643 | { |
| 4644 | struct symtable *st; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4645 | |
| 4646 | st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable)); |
| 4647 | if (st == NULL) |
| 4648 | return NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4649 | st->st_pass = 1; |
Tim Peters | ff1f852 | 2001-08-11 01:06:35 +0000 | [diff] [blame] | 4650 | |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4651 | st->st_filename = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4652 | if ((st->st_stack = PyList_New(0)) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4653 | goto fail; |
| 4654 | if ((st->st_symbols = PyDict_New()) == NULL) |
| 4655 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4656 | st->st_cur = NULL; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4657 | st->st_nscopes = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4658 | st->st_errors = 0; |
| 4659 | st->st_tmpname = 0; |
| 4660 | st->st_private = NULL; |
| 4661 | return st; |
| 4662 | fail: |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4663 | PySymtable_Free(st); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4664 | return NULL; |
| 4665 | } |
| 4666 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4667 | void |
| 4668 | PySymtable_Free(struct symtable *st) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4669 | { |
| 4670 | Py_XDECREF(st->st_symbols); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4671 | Py_XDECREF(st->st_stack); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4672 | Py_XDECREF(st->st_cur); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4673 | PyMem_Free((void *)st); |
| 4674 | } |
| 4675 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4676 | /* When the compiler exits a scope, it must should update the scope's |
| 4677 | free variable information with the list of free variables in its |
| 4678 | children. |
| 4679 | |
| 4680 | Variables that are free in children and defined in the current |
| 4681 | scope are cellvars. |
| 4682 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4683 | 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] | 4684 | false), free variables in children that are not defined here are |
| 4685 | implicit globals. |
| 4686 | |
| 4687 | */ |
| 4688 | |
| 4689 | static int |
| 4690 | symtable_update_free_vars(struct symtable *st) |
| 4691 | { |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4692 | int i, j, def; |
| 4693 | PyObject *o, *name, *list = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4694 | PySymtableEntryObject *child, *ste = st->st_cur; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4695 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4696 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4697 | def = DEF_FREE_CLASS; |
| 4698 | else |
| 4699 | def = DEF_FREE; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4700 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4701 | int pos = 0; |
| 4702 | |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4703 | if (list) |
| 4704 | PyList_SetSlice(list, 0, |
| 4705 | ((PyVarObject*)list)->ob_size, 0); |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4706 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4707 | PyList_GET_ITEM(ste->ste_children, i); |
| 4708 | while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4709 | int flags = PyInt_AS_LONG(o); |
| 4710 | if (!(is_free(flags))) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4711 | continue; /* avoids indentation */ |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4712 | if (list == NULL) { |
| 4713 | list = PyList_New(0); |
| 4714 | if (list == NULL) |
| 4715 | return -1; |
| 4716 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4717 | ste->ste_child_free = 1; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4718 | if (PyList_Append(list, name) < 0) { |
| 4719 | Py_DECREF(list); |
| 4720 | return -1; |
| 4721 | } |
| 4722 | } |
| 4723 | for (j = 0; list && j < PyList_GET_SIZE(list); j++) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4724 | PyObject *v; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4725 | name = PyList_GET_ITEM(list, j); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4726 | v = PyDict_GetItem(ste->ste_symbols, name); |
| 4727 | /* If a name N is declared global in scope A and |
| 4728 | referenced in scope B contained (perhaps |
| 4729 | indirectly) in A and there are no scopes |
| 4730 | with bindings for N between B and A, then N |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4731 | is global in B. Unless A is a class scope, |
| 4732 | because class scopes are not considered for |
| 4733 | nested scopes. |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4734 | */ |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4735 | if (v && (ste->ste_type != TYPE_CLASS)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4736 | int flags = PyInt_AS_LONG(v); |
| 4737 | if (flags & DEF_GLOBAL) { |
| 4738 | symtable_undo_free(st, child->ste_id, |
| 4739 | name); |
| 4740 | continue; |
| 4741 | } |
| 4742 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4743 | if (ste->ste_nested) { |
| 4744 | if (symtable_add_def_o(st, ste->ste_symbols, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4745 | name, def) < 0) { |
| 4746 | Py_DECREF(list); |
| 4747 | return -1; |
| 4748 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4749 | } else { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4750 | if (symtable_check_global(st, child->ste_id, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4751 | name) < 0) { |
| 4752 | Py_DECREF(list); |
| 4753 | return -1; |
| 4754 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4755 | } |
| 4756 | } |
| 4757 | } |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4758 | |
| 4759 | Py_XDECREF(list); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4760 | return 0; |
| 4761 | } |
| 4762 | |
| 4763 | /* If the current scope is a non-nested class or if name is not |
| 4764 | defined in the current, non-nested scope, then it is an implicit |
| 4765 | global in all nested scopes. |
| 4766 | */ |
| 4767 | |
| 4768 | static int |
| 4769 | symtable_check_global(struct symtable *st, PyObject *child, PyObject *name) |
| 4770 | { |
| 4771 | PyObject *o; |
| 4772 | int v; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4773 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4774 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4775 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4776 | return symtable_undo_free(st, child, name); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4777 | o = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4778 | if (o == NULL) |
| 4779 | return symtable_undo_free(st, child, name); |
| 4780 | v = PyInt_AS_LONG(o); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4781 | |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4782 | if (is_free(v) || (v & DEF_GLOBAL)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4783 | return symtable_undo_free(st, child, name); |
| 4784 | else |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4785 | return symtable_add_def_o(st, ste->ste_symbols, |
| 4786 | name, DEF_FREE); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4787 | } |
| 4788 | |
| 4789 | static int |
| 4790 | symtable_undo_free(struct symtable *st, PyObject *id, |
| 4791 | PyObject *name) |
| 4792 | { |
| 4793 | int i, v, x; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4794 | PyObject *info; |
| 4795 | PySymtableEntryObject *ste; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4796 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4797 | ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id); |
| 4798 | if (ste == NULL) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4799 | return -1; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4800 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4801 | info = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4802 | if (info == NULL) |
| 4803 | return 0; |
| 4804 | v = PyInt_AS_LONG(info); |
| 4805 | if (is_free(v)) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4806 | if (symtable_add_def_o(st, ste->ste_symbols, name, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4807 | DEF_FREE_GLOBAL) < 0) |
| 4808 | return -1; |
| 4809 | } else |
| 4810 | /* If the name is defined here or declared global, |
| 4811 | then the recursion stops. */ |
| 4812 | return 0; |
| 4813 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4814 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
| 4815 | PySymtableEntryObject *child; |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4816 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4817 | PyList_GET_ITEM(ste->ste_children, i); |
| 4818 | x = symtable_undo_free(st, child->ste_id, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4819 | if (x < 0) |
| 4820 | return x; |
| 4821 | } |
| 4822 | return 0; |
| 4823 | } |
| 4824 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4825 | /* symtable_enter_scope() gets a reference via PySymtableEntry_New(). |
| 4826 | This reference is released when the scope is exited, via the DECREF |
| 4827 | in symtable_exit_scope(). |
| 4828 | */ |
| 4829 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4830 | static int |
| 4831 | symtable_exit_scope(struct symtable *st) |
| 4832 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4833 | int end; |
| 4834 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4835 | if (st->st_pass == 1) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4836 | symtable_update_free_vars(st); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4837 | Py_DECREF(st->st_cur); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4838 | end = PyList_GET_SIZE(st->st_stack) - 1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4839 | st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack, |
| 4840 | end); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4841 | if (PySequence_DelItem(st->st_stack, end) < 0) |
| 4842 | return -1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4843 | return 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4844 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4845 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4846 | static void |
| 4847 | symtable_enter_scope(struct symtable *st, char *name, int type, |
| 4848 | int lineno) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4849 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4850 | PySymtableEntryObject *prev = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4851 | |
| 4852 | if (st->st_cur) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4853 | prev = st->st_cur; |
| 4854 | if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) { |
| 4855 | Py_DECREF(st->st_cur); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4856 | st->st_errors++; |
| 4857 | return; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4858 | } |
| 4859 | } |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4860 | st->st_cur = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4861 | PySymtableEntry_New(st, name, type, lineno); |
| 4862 | if (strcmp(name, TOP) == 0) |
| 4863 | st->st_global = st->st_cur->ste_symbols; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4864 | if (prev && st->st_pass == 1) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4865 | if (PyList_Append(prev->ste_children, |
| 4866 | (PyObject *)st->st_cur) < 0) |
| 4867 | st->st_errors++; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4868 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4869 | } |
| 4870 | |
| 4871 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4872 | symtable_lookup(struct symtable *st, char *name) |
| 4873 | { |
| 4874 | char buffer[MANGLE_LEN]; |
| 4875 | PyObject *v; |
| 4876 | int flags; |
| 4877 | |
| 4878 | if (mangle(st->st_private, name, buffer, sizeof(buffer))) |
| 4879 | name = buffer; |
| 4880 | v = PyDict_GetItemString(st->st_cur->ste_symbols, name); |
| 4881 | if (v == NULL) { |
| 4882 | if (PyErr_Occurred()) |
| 4883 | return -1; |
| 4884 | else |
| 4885 | return 0; |
| 4886 | } |
| 4887 | |
| 4888 | flags = PyInt_AS_LONG(v); |
| 4889 | return flags; |
| 4890 | } |
| 4891 | |
| 4892 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4893 | symtable_add_def(struct symtable *st, char *name, int flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4894 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4895 | PyObject *s; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4896 | char buffer[MANGLE_LEN]; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 4897 | int ret; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4898 | |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4899 | if (mangle(st->st_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4900 | name = buffer; |
| 4901 | if ((s = PyString_InternFromString(name)) == NULL) |
| 4902 | return -1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 4903 | ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag); |
| 4904 | Py_DECREF(s); |
| 4905 | return ret; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4906 | } |
| 4907 | |
| 4908 | /* Must only be called with mangled names */ |
| 4909 | |
| 4910 | static int |
| 4911 | symtable_add_def_o(struct symtable *st, PyObject *dict, |
| 4912 | PyObject *name, int flag) |
| 4913 | { |
| 4914 | PyObject *o; |
| 4915 | int val; |
| 4916 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4917 | if ((o = PyDict_GetItem(dict, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4918 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4919 | if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4920 | PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 4921 | PyString_AsString(name)); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 4922 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4923 | st->st_cur->ste_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4924 | return -1; |
| 4925 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4926 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4927 | } else |
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 | o = PyInt_FromLong(val); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4930 | if (PyDict_SetItem(dict, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4931 | Py_DECREF(o); |
| 4932 | return -1; |
| 4933 | } |
| 4934 | Py_DECREF(o); |
| 4935 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4936 | if (flag & DEF_PARAM) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4937 | if (PyList_Append(st->st_cur->ste_varnames, name) < 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4938 | return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4939 | } else if (flag & DEF_GLOBAL) { |
| 4940 | /* XXX need to update DEF_GLOBAL for other flags too; |
| 4941 | perhaps only DEF_FREE_GLOBAL */ |
| 4942 | if ((o = PyDict_GetItem(st->st_global, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4943 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4944 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4945 | } else |
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 | o = PyInt_FromLong(val); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4948 | if (PyDict_SetItem(st->st_global, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4949 | Py_DECREF(o); |
| 4950 | return -1; |
| 4951 | } |
| 4952 | Py_DECREF(o); |
| 4953 | } |
| 4954 | return 0; |
| 4955 | } |
| 4956 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4957 | #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4958 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 4959 | /* Look for a yield stmt under n. Return 1 if found, else 0. |
| 4960 | This hack is used to look inside "if 0:" blocks (which are normally |
| 4961 | ignored) in case those are the only places a yield occurs (so that this |
| 4962 | function is a generator). */ |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 4963 | static int |
| 4964 | look_for_yield(node *n) |
| 4965 | { |
| 4966 | int i; |
| 4967 | |
| 4968 | for (i = 0; i < NCH(n); ++i) { |
| 4969 | node *kid = CHILD(n, i); |
| 4970 | |
| 4971 | switch (TYPE(kid)) { |
| 4972 | |
| 4973 | case classdef: |
| 4974 | case funcdef: |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 4975 | case lambdef: |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 4976 | /* Stuff in nested functions and classes can't make |
| 4977 | the parent a generator. */ |
| 4978 | return 0; |
| 4979 | |
| 4980 | case yield_stmt: |
| 4981 | return 1; |
| 4982 | |
| 4983 | default: |
| 4984 | if (look_for_yield(kid)) |
| 4985 | return 1; |
| 4986 | } |
| 4987 | } |
| 4988 | return 0; |
| 4989 | } |
| 4990 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4991 | static void |
| 4992 | symtable_node(struct symtable *st, node *n) |
| 4993 | { |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame^] | 4994 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4995 | |
| 4996 | loop: |
| 4997 | switch (TYPE(n)) { |
| 4998 | case funcdef: { |
| 4999 | char *func_name = STR(CHILD(n, 1)); |
| 5000 | symtable_add_def(st, func_name, DEF_LOCAL); |
| 5001 | symtable_default_args(st, CHILD(n, 2)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5002 | symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5003 | symtable_funcdef(st, n); |
| 5004 | symtable_exit_scope(st); |
| 5005 | break; |
| 5006 | } |
| 5007 | case lambdef: |
| 5008 | if (NCH(n) == 4) |
| 5009 | symtable_default_args(st, CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5010 | symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5011 | symtable_funcdef(st, n); |
| 5012 | symtable_exit_scope(st); |
| 5013 | break; |
| 5014 | case classdef: { |
| 5015 | char *tmp, *class_name = STR(CHILD(n, 1)); |
| 5016 | symtable_add_def(st, class_name, DEF_LOCAL); |
| 5017 | if (TYPE(CHILD(n, 2)) == LPAR) { |
| 5018 | node *bases = CHILD(n, 3); |
| 5019 | int i; |
| 5020 | for (i = 0; i < NCH(bases); i += 2) { |
| 5021 | symtable_node(st, CHILD(bases, i)); |
| 5022 | } |
| 5023 | } |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5024 | symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5025 | tmp = st->st_private; |
| 5026 | st->st_private = class_name; |
| 5027 | symtable_node(st, CHILD(n, NCH(n) - 1)); |
| 5028 | st->st_private = tmp; |
| 5029 | symtable_exit_scope(st); |
| 5030 | break; |
| 5031 | } |
| 5032 | case if_stmt: |
| 5033 | for (i = 0; i + 3 < NCH(n); i += 4) { |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5034 | if (is_constant_false(NULL, (CHILD(n, i + 1)))) { |
| 5035 | if (st->st_cur->ste_generator == 0) |
| 5036 | st->st_cur->ste_generator = |
| 5037 | look_for_yield(CHILD(n, i+3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5038 | continue; |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5039 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5040 | symtable_node(st, CHILD(n, i + 1)); |
| 5041 | symtable_node(st, CHILD(n, i + 3)); |
| 5042 | } |
| 5043 | if (i + 2 < NCH(n)) |
| 5044 | symtable_node(st, CHILD(n, i + 2)); |
| 5045 | break; |
| 5046 | case global_stmt: |
| 5047 | symtable_global(st, n); |
| 5048 | break; |
| 5049 | case import_stmt: |
| 5050 | symtable_import(st, n); |
| 5051 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5052 | case exec_stmt: { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5053 | st->st_cur->ste_optimized |= OPT_EXEC; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5054 | symtable_node(st, CHILD(n, 1)); |
| 5055 | if (NCH(n) > 2) |
| 5056 | symtable_node(st, CHILD(n, 3)); |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5057 | else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5058 | st->st_cur->ste_optimized |= OPT_BARE_EXEC; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5059 | st->st_cur->ste_opt_lineno = n->n_lineno; |
| 5060 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5061 | if (NCH(n) > 4) |
| 5062 | symtable_node(st, CHILD(n, 5)); |
| 5063 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5064 | |
| 5065 | } |
Jeremy Hylton | 384639f | 2001-02-19 15:50:51 +0000 | [diff] [blame] | 5066 | case assert_stmt: |
| 5067 | if (Py_OptimizeFlag) |
| 5068 | return; |
| 5069 | if (NCH(n) == 2) { |
| 5070 | n = CHILD(n, 1); |
| 5071 | goto loop; |
| 5072 | } else { |
| 5073 | symtable_node(st, CHILD(n, 1)); |
| 5074 | n = CHILD(n, 3); |
| 5075 | goto loop; |
| 5076 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5077 | case except_clause: |
| 5078 | if (NCH(n) == 4) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5079 | symtable_assign(st, CHILD(n, 3), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5080 | if (NCH(n) > 1) { |
| 5081 | n = CHILD(n, 1); |
| 5082 | goto loop; |
| 5083 | } |
| 5084 | break; |
| 5085 | case del_stmt: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5086 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5087 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 5088 | case yield_stmt: |
| 5089 | st->st_cur->ste_generator = 1; |
| 5090 | n = CHILD(n, 1); |
| 5091 | goto loop; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5092 | case expr_stmt: |
| 5093 | if (NCH(n) == 1) |
| 5094 | n = CHILD(n, 0); |
| 5095 | else { |
| 5096 | if (TYPE(CHILD(n, 1)) == augassign) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5097 | symtable_assign(st, CHILD(n, 0), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5098 | symtable_node(st, CHILD(n, 2)); |
| 5099 | break; |
| 5100 | } else { |
| 5101 | int i; |
| 5102 | for (i = 0; i < NCH(n) - 2; i += 2) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5103 | symtable_assign(st, CHILD(n, i), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5104 | n = CHILD(n, NCH(n) - 1); |
| 5105 | } |
| 5106 | } |
| 5107 | goto loop; |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame^] | 5108 | case list_iter: |
| 5109 | n = CHILD(n, 0); |
| 5110 | if (TYPE(n) == list_for) { |
| 5111 | st->st_tmpname++; |
| 5112 | symtable_list_comprehension(st, n); |
| 5113 | st->st_tmpname--; |
| 5114 | } else { |
| 5115 | REQ(n, list_if); |
| 5116 | symtable_node(st, CHILD(n, 1)); |
| 5117 | if (NCH(n) == 3) { |
| 5118 | n = CHILD(n, 2); |
| 5119 | goto loop; |
| 5120 | } |
| 5121 | } |
| 5122 | break; |
| 5123 | case for_stmt: |
| 5124 | symtable_assign(st, CHILD(n, 1), 0); |
| 5125 | for (i = 3; i < NCH(n); ++i) |
| 5126 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5127 | symtable_node(st, CHILD(n, i)); |
| 5128 | break; |
| 5129 | /* The remaining cases fall through to default except in |
| 5130 | special circumstances. This requires the individual cases |
| 5131 | to be coded with great care, even though they look like |
| 5132 | rather innocuous. Each case must double-check TYPE(n). |
| 5133 | */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5134 | case argument: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame^] | 5135 | if (TYPE(n) == argument && NCH(n) == 3) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5136 | n = CHILD(n, 2); |
| 5137 | goto loop; |
| 5138 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame^] | 5139 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5140 | case listmaker: |
| 5141 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5142 | st->st_tmpname++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5143 | symtable_list_comprehension(st, CHILD(n, 1)); |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5144 | symtable_node(st, CHILD(n, 0)); |
| 5145 | st->st_tmpname--; |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame^] | 5146 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5147 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame^] | 5148 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5149 | case atom: |
| 5150 | if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) { |
| 5151 | symtable_add_use(st, STR(CHILD(n, 0))); |
| 5152 | break; |
| 5153 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame^] | 5154 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5155 | default: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame^] | 5156 | /* Walk over every non-token child with a special case |
| 5157 | for one child. |
| 5158 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5159 | if (NCH(n) == 1) { |
| 5160 | n = CHILD(n, 0); |
| 5161 | goto loop; |
| 5162 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame^] | 5163 | for (i = 0; i < NCH(n); ++i) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5164 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5165 | symtable_node(st, CHILD(n, i)); |
| 5166 | } |
| 5167 | } |
| 5168 | |
| 5169 | static void |
| 5170 | symtable_funcdef(struct symtable *st, node *n) |
| 5171 | { |
| 5172 | node *body; |
| 5173 | |
| 5174 | if (TYPE(n) == lambdef) { |
| 5175 | if (NCH(n) == 4) |
| 5176 | symtable_params(st, CHILD(n, 1)); |
| 5177 | } else |
| 5178 | symtable_params(st, CHILD(n, 2)); |
| 5179 | body = CHILD(n, NCH(n) - 1); |
| 5180 | symtable_node(st, body); |
| 5181 | } |
| 5182 | |
| 5183 | /* The next two functions parse the argument tuple. |
| 5184 | symtable_default_arg() checks for names in the default arguments, |
| 5185 | which are references in the defining scope. symtable_params() |
| 5186 | parses the parameter names, which are defined in the function's |
| 5187 | body. |
| 5188 | |
| 5189 | varargslist: |
| 5190 | (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) |
| 5191 | | fpdef ['=' test] (',' fpdef ['=' test])* [','] |
| 5192 | */ |
| 5193 | |
| 5194 | static void |
| 5195 | symtable_default_args(struct symtable *st, node *n) |
| 5196 | { |
| 5197 | node *c; |
| 5198 | int i; |
| 5199 | |
| 5200 | if (TYPE(n) == parameters) { |
| 5201 | n = CHILD(n, 1); |
| 5202 | if (TYPE(n) == RPAR) |
| 5203 | return; |
| 5204 | } |
| 5205 | REQ(n, varargslist); |
| 5206 | for (i = 0; i < NCH(n); i += 2) { |
| 5207 | c = CHILD(n, i); |
| 5208 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5209 | break; |
| 5210 | } |
| 5211 | if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL)) |
| 5212 | symtable_node(st, CHILD(n, i)); |
| 5213 | } |
| 5214 | } |
| 5215 | |
| 5216 | static void |
| 5217 | symtable_params(struct symtable *st, node *n) |
| 5218 | { |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5219 | int i, complex = -1, ext = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5220 | node *c = NULL; |
| 5221 | |
| 5222 | if (TYPE(n) == parameters) { |
| 5223 | n = CHILD(n, 1); |
| 5224 | if (TYPE(n) == RPAR) |
| 5225 | return; |
| 5226 | } |
| 5227 | REQ(n, varargslist); |
| 5228 | for (i = 0; i < NCH(n); i += 2) { |
| 5229 | c = CHILD(n, i); |
| 5230 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5231 | ext = 1; |
| 5232 | break; |
| 5233 | } |
| 5234 | if (TYPE(c) == test) { |
| 5235 | continue; |
| 5236 | } |
| 5237 | if (TYPE(CHILD(c, 0)) == NAME) |
| 5238 | symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM); |
| 5239 | else { |
| 5240 | char nbuf[10]; |
| 5241 | sprintf(nbuf, ".%d", i); |
| 5242 | symtable_add_def(st, nbuf, DEF_PARAM); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5243 | complex = i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5244 | } |
| 5245 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5246 | if (ext) { |
| 5247 | c = CHILD(n, i); |
| 5248 | if (TYPE(c) == STAR) { |
| 5249 | i++; |
| 5250 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5251 | DEF_PARAM | DEF_STAR); |
| 5252 | i += 2; |
Jeremy Hylton | 1113cfc | 2001-01-23 00:50:52 +0000 | [diff] [blame] | 5253 | if (i >= NCH(n)) |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5254 | c = NULL; |
| 5255 | else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5256 | c = CHILD(n, i); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5257 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5258 | if (c && TYPE(c) == DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5259 | i++; |
| 5260 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5261 | DEF_PARAM | DEF_DOUBLESTAR); |
| 5262 | } |
| 5263 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5264 | if (complex >= 0) { |
| 5265 | int j; |
| 5266 | for (j = 0; j <= complex; j++) { |
| 5267 | c = CHILD(n, j); |
| 5268 | if (TYPE(c) == COMMA) |
Jeremy Hylton | 2b3f0ca | 2001-02-19 23:52:49 +0000 | [diff] [blame] | 5269 | c = CHILD(n, ++j); |
| 5270 | else if (TYPE(c) == EQUAL) |
| 5271 | c = CHILD(n, j += 3); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5272 | if (TYPE(CHILD(c, 0)) == LPAR) |
| 5273 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5274 | } |
| 5275 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5276 | } |
| 5277 | |
| 5278 | static void |
| 5279 | symtable_params_fplist(struct symtable *st, node *n) |
| 5280 | { |
| 5281 | int i; |
| 5282 | node *c; |
| 5283 | |
| 5284 | REQ(n, fplist); |
| 5285 | for (i = 0; i < NCH(n); i += 2) { |
| 5286 | c = CHILD(n, i); |
| 5287 | REQ(c, fpdef); |
| 5288 | if (NCH(c) == 1) |
| 5289 | symtable_add_def(st, STR(CHILD(c, 0)), |
| 5290 | DEF_PARAM | DEF_INTUPLE); |
| 5291 | else |
| 5292 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5293 | } |
| 5294 | |
| 5295 | } |
| 5296 | |
| 5297 | static void |
| 5298 | symtable_global(struct symtable *st, node *n) |
| 5299 | { |
| 5300 | int i; |
| 5301 | |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 5302 | /* XXX It might be helpful to warn about module-level global |
| 5303 | statements, but it's hard to tell the difference between |
| 5304 | module-level and a string passed to exec. |
| 5305 | */ |
Jeremy Hylton | c176132 | 2001-02-28 23:44:45 +0000 | [diff] [blame] | 5306 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5307 | for (i = 1; i < NCH(n); i += 2) { |
| 5308 | char *name = STR(CHILD(n, i)); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5309 | int flags; |
| 5310 | |
| 5311 | flags = symtable_lookup(st, name); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5312 | if (flags < 0) |
| 5313 | continue; |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5314 | if (flags && flags != DEF_GLOBAL) { |
| 5315 | char buf[500]; |
| 5316 | if (flags & DEF_PARAM) { |
| 5317 | PyErr_Format(PyExc_SyntaxError, |
| 5318 | "name '%.400s' is local and global", |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5319 | name); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5320 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5321 | st->st_cur->ste_lineno); |
| 5322 | st->st_errors++; |
| 5323 | return; |
Guido van Rossum | 0bba7f8 | 2001-02-28 21:55:38 +0000 | [diff] [blame] | 5324 | } |
| 5325 | else { |
| 5326 | if (flags & DEF_LOCAL) |
| 5327 | sprintf(buf, GLOBAL_AFTER_ASSIGN, |
| 5328 | name); |
| 5329 | else |
| 5330 | sprintf(buf, GLOBAL_AFTER_USE, name); |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 5331 | symtable_warn(st, buf); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5332 | } |
| 5333 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5334 | symtable_add_def(st, name, DEF_GLOBAL); |
| 5335 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5336 | } |
| 5337 | |
| 5338 | static void |
| 5339 | symtable_list_comprehension(struct symtable *st, node *n) |
| 5340 | { |
| 5341 | char tmpname[12]; |
| 5342 | |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5343 | sprintf(tmpname, "_[%d]", st->st_tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5344 | symtable_add_def(st, tmpname, DEF_LOCAL); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5345 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5346 | symtable_node(st, CHILD(n, 3)); |
| 5347 | if (NCH(n) == 5) |
| 5348 | symtable_node(st, CHILD(n, 4)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5349 | } |
| 5350 | |
| 5351 | static void |
| 5352 | symtable_import(struct symtable *st, node *n) |
| 5353 | { |
| 5354 | int i; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5355 | /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)* |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5356 | | 'from' dotted_name 'import' |
| 5357 | ('*' | import_as_name (',' import_as_name)*) |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5358 | import_as_name: NAME [NAME NAME] |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5359 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5360 | if (STR(CHILD(n, 0))[0] == 'f') { /* from */ |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5361 | node *dotname = CHILD(n, 1); |
| 5362 | if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) { |
| 5363 | /* check for bogus imports */ |
| 5364 | if (n->n_lineno >= st->st_future->ff_last_lineno) { |
| 5365 | PyErr_SetString(PyExc_SyntaxError, |
| 5366 | LATE_FUTURE); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5367 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5368 | n->n_lineno); |
| 5369 | st->st_errors++; |
| 5370 | return; |
| 5371 | } |
| 5372 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5373 | if (TYPE(CHILD(n, 3)) == STAR) { |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 5374 | if (st->st_cur->ste_type != TYPE_MODULE) { |
Jeremy Hylton | 6a53bd8 | 2001-08-06 20:34:25 +0000 | [diff] [blame] | 5375 | if (symtable_warn(st, |
| 5376 | "import * only allowed at module level") < 0) |
| 5377 | return; |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 5378 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5379 | st->st_cur->ste_optimized |= OPT_IMPORT_STAR; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5380 | st->st_cur->ste_opt_lineno = n->n_lineno; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5381 | } else { |
| 5382 | for (i = 3; i < NCH(n); i += 2) { |
| 5383 | node *c = CHILD(n, i); |
| 5384 | if (NCH(c) > 1) /* import as */ |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5385 | symtable_assign(st, CHILD(c, 2), |
| 5386 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5387 | else |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5388 | symtable_assign(st, CHILD(c, 0), |
| 5389 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5390 | } |
| 5391 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5392 | } else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5393 | for (i = 1; i < NCH(n); i += 2) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5394 | symtable_assign(st, CHILD(n, i), DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5395 | } |
| 5396 | } |
| 5397 | } |
| 5398 | |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame^] | 5399 | /* The third argument to symatble_assign() is a flag to be passed to |
| 5400 | symtable_add_def() if it is eventually called. The flag is useful |
| 5401 | to specify the particular type of assignment that should be |
| 5402 | recorded, e.g. an assignment caused by import. |
| 5403 | */ |
| 5404 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5405 | static void |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame^] | 5406 | symtable_assign(struct symtable *st, node *n, int def_flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5407 | { |
| 5408 | node *tmp; |
| 5409 | int i; |
| 5410 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5411 | loop: |
| 5412 | switch (TYPE(n)) { |
| 5413 | case lambdef: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5414 | /* invalid assignment, e.g. lambda x:x=2. The next |
| 5415 | pass will catch this error. */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5416 | return; |
| 5417 | case power: |
| 5418 | if (NCH(n) > 2) { |
| 5419 | for (i = 2; i < NCH(n); ++i) |
| 5420 | if (TYPE(CHILD(n, i)) != DOUBLESTAR) |
| 5421 | symtable_node(st, CHILD(n, i)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5422 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5423 | if (NCH(n) > 1) { |
| 5424 | symtable_node(st, CHILD(n, 0)); |
| 5425 | symtable_node(st, CHILD(n, 1)); |
| 5426 | } else { |
| 5427 | n = CHILD(n, 0); |
| 5428 | goto loop; |
| 5429 | } |
| 5430 | return; |
| 5431 | case listmaker: |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5432 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
| 5433 | /* XXX This is an error, but the next pass |
| 5434 | will catch it. */ |
| 5435 | return; |
| 5436 | } else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5437 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame^] | 5438 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5439 | } |
| 5440 | return; |
| 5441 | case exprlist: |
| 5442 | case testlist: |
| 5443 | if (NCH(n) == 1) { |
| 5444 | n = CHILD(n, 0); |
| 5445 | goto loop; |
| 5446 | } |
| 5447 | else { |
| 5448 | int i; |
| 5449 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame^] | 5450 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5451 | return; |
| 5452 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5453 | case atom: |
| 5454 | tmp = CHILD(n, 0); |
| 5455 | if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) { |
| 5456 | n = CHILD(n, 1); |
| 5457 | goto loop; |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 5458 | } else if (TYPE(tmp) == NAME) { |
Jeremy Hylton | a830b38 | 2001-04-09 16:07:59 +0000 | [diff] [blame] | 5459 | if (strcmp(STR(tmp), "__debug__") == 0) |
| 5460 | symtable_warn(st, ASSIGN_DEBUG); |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame^] | 5461 | symtable_add_def(st, STR(tmp), DEF_LOCAL | def_flag); |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 5462 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5463 | return; |
| 5464 | case dotted_as_name: |
| 5465 | if (NCH(n) == 3) |
| 5466 | symtable_add_def(st, STR(CHILD(n, 2)), |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame^] | 5467 | DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5468 | else |
| 5469 | symtable_add_def(st, |
| 5470 | STR(CHILD(CHILD(n, |
| 5471 | 0), 0)), |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame^] | 5472 | DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5473 | return; |
| 5474 | case dotted_name: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame^] | 5475 | symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5476 | return; |
| 5477 | case NAME: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame^] | 5478 | symtable_add_def(st, STR(n), DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5479 | return; |
| 5480 | default: |
| 5481 | if (NCH(n) == 0) |
| 5482 | return; |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 5483 | if (NCH(n) == 1) { |
| 5484 | n = CHILD(n, 0); |
| 5485 | goto loop; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5486 | } |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 5487 | /* Should only occur for errors like x + 1 = 1, |
| 5488 | which will be caught in the next pass. */ |
| 5489 | for (i = 0; i < NCH(n); ++i) |
| 5490 | if (TYPE(CHILD(n, i)) >= single_input) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame^] | 5491 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5492 | } |
| 5493 | } |