Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2 | /* Compile an expression node to intermediate code */ |
| 3 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4 | /* XXX TO DO: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 5 | XXX add __doc__ attribute == co_doc to code object attributes? |
| 6 | 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] | 7 | XXX Generate simple jump for break/return outside 'try...finally' |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 8 | XXX Allow 'continue' inside finally clause of try-finally |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 9 | 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] | 10 | XXX other JAR tricks? |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 11 | */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 13 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 15 | #include "node.h" |
| 16 | #include "token.h" |
| 17 | #include "graminit.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 18 | #include "compile.h" |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 19 | #include "symtable.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 20 | #include "opcode.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 21 | #include "structmember.h" |
| 22 | |
| 23 | #include <ctype.h> |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 25 | /* Three symbols from graminit.h are also defined in Python.h, with |
| 26 | Py_ prefixes to their names. Python.h can't include graminit.h |
| 27 | (which defines too many confusing symbols), but we can check here |
| 28 | that they haven't changed (which is very unlikely, but possible). */ |
| 29 | #if Py_single_input != single_input |
Guido van Rossum | 2f75b29 | 1997-05-20 22:18:48 +0000 | [diff] [blame] | 30 | #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] | 31 | #endif |
| 32 | #if Py_file_input != file_input |
Guido van Rossum | 2f75b29 | 1997-05-20 22:18:48 +0000 | [diff] [blame] | 33 | #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] | 34 | #endif |
| 35 | #if Py_eval_input != eval_input |
Guido van Rossum | 2f75b29 | 1997-05-20 22:18:48 +0000 | [diff] [blame] | 36 | #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] | 37 | #endif |
| 38 | |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 39 | int Py_OptimizeFlag = 0; |
| 40 | |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 41 | #define OP_DELETE 0 |
| 42 | #define OP_ASSIGN 1 |
| 43 | #define OP_APPLY 2 |
| 44 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 45 | #define VAR_LOAD 0 |
| 46 | #define VAR_STORE 1 |
| 47 | #define VAR_DELETE 2 |
| 48 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 49 | #define DEL_CLOSURE_ERROR \ |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 50 | "can not delete variable '%.400s' referenced in nested scope" |
| 51 | |
| 52 | #define DUPLICATE_ARGUMENT \ |
| 53 | "duplicate argument '%s' in function definition" |
| 54 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 55 | #define ILLEGAL_DYNAMIC_SCOPE \ |
| 56 | "%.100s: exec or 'import *' makes names ambiguous in nested scope" |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 57 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 58 | #define MANGLE_LEN 256 |
| 59 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 60 | #define OFF(x) offsetof(PyCodeObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 61 | |
| 62 | static struct memberlist code_memberlist[] = { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 63 | {"co_argcount", T_INT, OFF(co_argcount), READONLY}, |
| 64 | {"co_nlocals", T_INT, OFF(co_nlocals), READONLY}, |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 65 | {"co_stacksize",T_INT, OFF(co_stacksize), READONLY}, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 66 | {"co_flags", T_INT, OFF(co_flags), READONLY}, |
Guido van Rossum | 39d942d | 1992-01-12 02:30:05 +0000 | [diff] [blame] | 67 | {"co_code", T_OBJECT, OFF(co_code), READONLY}, |
| 68 | {"co_consts", T_OBJECT, OFF(co_consts), READONLY}, |
| 69 | {"co_names", T_OBJECT, OFF(co_names), READONLY}, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 70 | {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY}, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 71 | {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY}, |
| 72 | {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY}, |
Guido van Rossum | 39d942d | 1992-01-12 02:30:05 +0000 | [diff] [blame] | 73 | {"co_filename", T_OBJECT, OFF(co_filename), READONLY}, |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 74 | {"co_name", T_OBJECT, OFF(co_name), READONLY}, |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 75 | {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY}, |
| 76 | {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 77 | {NULL} /* Sentinel */ |
| 78 | }; |
| 79 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 80 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 81 | code_getattr(PyCodeObject *co, char *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 82 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 83 | return PyMember_Get((char *)co, code_memberlist, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 84 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 85 | |
| 86 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 87 | code_dealloc(PyCodeObject *co) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 88 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 89 | Py_XDECREF(co->co_code); |
| 90 | Py_XDECREF(co->co_consts); |
| 91 | Py_XDECREF(co->co_names); |
Guido van Rossum | 275558c | 1997-07-25 20:13:49 +0000 | [diff] [blame] | 92 | Py_XDECREF(co->co_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 93 | Py_XDECREF(co->co_freevars); |
| 94 | Py_XDECREF(co->co_cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 95 | Py_XDECREF(co->co_filename); |
| 96 | Py_XDECREF(co->co_name); |
Guido van Rossum | 275558c | 1997-07-25 20:13:49 +0000 | [diff] [blame] | 97 | Py_XDECREF(co->co_lnotab); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 98 | PyObject_DEL(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 101 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 102 | code_repr(PyCodeObject *co) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 103 | { |
| 104 | char buf[500]; |
| 105 | int lineno = -1; |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 106 | char *filename = "???"; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 107 | char *name = "???"; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 108 | |
Guido van Rossum | a396a88 | 2000-04-07 01:21:36 +0000 | [diff] [blame] | 109 | if (co->co_firstlineno != 0) |
| 110 | lineno = co->co_firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 111 | if (co->co_filename && PyString_Check(co->co_filename)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 112 | filename = PyString_AS_STRING(co->co_filename); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 113 | if (co->co_name && PyString_Check(co->co_name)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 114 | name = PyString_AS_STRING(co->co_name); |
Fred Drake | 615ae55 | 2000-06-30 16:20:13 +0000 | [diff] [blame] | 115 | sprintf(buf, "<code object %.100s at %p, file \"%.300s\", line %d>", |
| 116 | name, co, filename, lineno); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 117 | return PyString_FromString(buf); |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 120 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 121 | code_compare(PyCodeObject *co, PyCodeObject *cp) |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 122 | { |
| 123 | int cmp; |
Guido van Rossum | 4467959 | 2000-04-10 16:20:31 +0000 | [diff] [blame] | 124 | cmp = PyObject_Compare(co->co_name, cp->co_name); |
| 125 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 126 | cmp = co->co_argcount - cp->co_argcount; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 127 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 128 | cmp = co->co_nlocals - cp->co_nlocals; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 129 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 130 | cmp = co->co_flags - cp->co_flags; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 131 | if (cmp) return cmp; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 132 | cmp = PyObject_Compare(co->co_code, cp->co_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 133 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 134 | cmp = PyObject_Compare(co->co_consts, cp->co_consts); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 135 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 136 | cmp = PyObject_Compare(co->co_names, cp->co_names); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 137 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 138 | cmp = PyObject_Compare(co->co_varnames, cp->co_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 139 | if (cmp) return cmp; |
| 140 | cmp = PyObject_Compare(co->co_freevars, cp->co_freevars); |
| 141 | if (cmp) return cmp; |
| 142 | cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 143 | return cmp; |
| 144 | } |
| 145 | |
| 146 | static long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 147 | code_hash(PyCodeObject *co) |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 148 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 149 | long h, h0, h1, h2, h3, h4, h5, h6; |
Guido van Rossum | 4467959 | 2000-04-10 16:20:31 +0000 | [diff] [blame] | 150 | h0 = PyObject_Hash(co->co_name); |
| 151 | if (h0 == -1) return -1; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 152 | h1 = PyObject_Hash(co->co_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 153 | if (h1 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 154 | h2 = PyObject_Hash(co->co_consts); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 155 | if (h2 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 156 | h3 = PyObject_Hash(co->co_names); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 157 | if (h3 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 158 | h4 = PyObject_Hash(co->co_varnames); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 159 | if (h4 == -1) return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 160 | h5 = PyObject_Hash(co->co_freevars); |
| 161 | if (h5 == -1) return -1; |
| 162 | h6 = PyObject_Hash(co->co_cellvars); |
| 163 | if (h6 == -1) return -1; |
| 164 | h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 165 | co->co_argcount ^ co->co_nlocals ^ co->co_flags; |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 166 | if (h == -1) h = -2; |
| 167 | return h; |
| 168 | } |
| 169 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 170 | PyTypeObject PyCode_Type = { |
| 171 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 172 | 0, |
| 173 | "code", |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 174 | sizeof(PyCodeObject), |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 175 | 0, |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 176 | (destructor)code_dealloc, /*tp_dealloc*/ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 177 | 0, /*tp_print*/ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 178 | (getattrfunc)code_getattr, /*tp_getattr*/ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 179 | 0, /*tp_setattr*/ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 180 | (cmpfunc)code_compare, /*tp_compare*/ |
| 181 | (reprfunc)code_repr, /*tp_repr*/ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 182 | 0, /*tp_as_number*/ |
| 183 | 0, /*tp_as_sequence*/ |
| 184 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 185 | (hashfunc)code_hash, /*tp_hash*/ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 188 | #define NAME_CHARS \ |
| 189 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" |
| 190 | |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 191 | /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */ |
| 192 | |
| 193 | static int |
| 194 | all_name_chars(unsigned char *s) |
| 195 | { |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 196 | static char ok_name_char[256]; |
| 197 | static unsigned char *name_chars = (unsigned char *)NAME_CHARS; |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 198 | |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 199 | if (ok_name_char[*name_chars] == 0) { |
| 200 | unsigned char *p; |
| 201 | for (p = name_chars; *p; p++) |
| 202 | ok_name_char[*p] = 1; |
| 203 | } |
| 204 | while (*s) { |
| 205 | if (ok_name_char[*s++] == 0) |
| 206 | return 0; |
| 207 | } |
| 208 | return 1; |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 211 | static int |
| 212 | intern_strings(PyObject *tuple) |
| 213 | { |
| 214 | int i; |
| 215 | |
| 216 | for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { |
| 217 | PyObject *v = PyTuple_GET_ITEM(tuple, i); |
| 218 | if (v == NULL || !PyString_Check(v)) { |
| 219 | Py_FatalError("non-string found in code slot"); |
| 220 | PyErr_BadInternalCall(); |
| 221 | return -1; |
| 222 | } |
| 223 | PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i)); |
| 224 | } |
| 225 | return 0; |
| 226 | } |
| 227 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 228 | PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 229 | PyCode_New(int argcount, int nlocals, int stacksize, int flags, |
| 230 | PyObject *code, PyObject *consts, PyObject *names, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 231 | PyObject *varnames, PyObject *freevars, PyObject *cellvars, |
| 232 | PyObject *filename, PyObject *name, int firstlineno, |
| 233 | PyObject *lnotab) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 234 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 235 | PyCodeObject *co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 236 | int i; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 237 | PyBufferProcs *pb; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 238 | /* Check argument types */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 239 | if (argcount < 0 || nlocals < 0 || |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 240 | code == NULL || |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 241 | consts == NULL || !PyTuple_Check(consts) || |
| 242 | names == NULL || !PyTuple_Check(names) || |
| 243 | varnames == NULL || !PyTuple_Check(varnames) || |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 244 | freevars == NULL || !PyTuple_Check(freevars) || |
| 245 | cellvars == NULL || !PyTuple_Check(cellvars) || |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 246 | name == NULL || !PyString_Check(name) || |
| 247 | filename == NULL || !PyString_Check(filename) || |
| 248 | lnotab == NULL || !PyString_Check(lnotab)) { |
| 249 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 250 | return NULL; |
| 251 | } |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 252 | pb = code->ob_type->tp_as_buffer; |
| 253 | if (pb == NULL || |
| 254 | pb->bf_getreadbuffer == NULL || |
| 255 | pb->bf_getsegcount == NULL || |
| 256 | (*pb->bf_getsegcount)(code, NULL) != 1) |
| 257 | { |
| 258 | PyErr_BadInternalCall(); |
| 259 | return NULL; |
| 260 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 261 | intern_strings(names); |
| 262 | intern_strings(varnames); |
| 263 | if (freevars == NULL) |
| 264 | freevars = PyTuple_New(0); |
| 265 | intern_strings(freevars); |
| 266 | if (cellvars == NULL) |
| 267 | cellvars = PyTuple_New(0); |
| 268 | intern_strings(cellvars); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 269 | /* Intern selected string constants */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 270 | for (i = PyTuple_Size(consts); --i >= 0; ) { |
| 271 | PyObject *v = PyTuple_GetItem(consts, i); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 272 | if (!PyString_Check(v)) |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 273 | continue; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 274 | if (!all_name_chars((unsigned char *)PyString_AS_STRING(v))) |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 275 | continue; |
| 276 | PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 277 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 278 | co = PyObject_NEW(PyCodeObject, &PyCode_Type); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 279 | if (co != NULL) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 280 | co->co_argcount = argcount; |
| 281 | co->co_nlocals = nlocals; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 282 | co->co_stacksize = stacksize; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 283 | co->co_flags = flags; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 284 | Py_INCREF(code); |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 285 | co->co_code = code; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 286 | Py_INCREF(consts); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 287 | co->co_consts = consts; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 288 | Py_INCREF(names); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 289 | co->co_names = names; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 290 | Py_INCREF(varnames); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 291 | co->co_varnames = varnames; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 292 | Py_INCREF(freevars); |
| 293 | co->co_freevars = freevars; |
| 294 | Py_INCREF(cellvars); |
| 295 | co->co_cellvars = cellvars; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 296 | Py_INCREF(filename); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 297 | co->co_filename = filename; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 298 | Py_INCREF(name); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 299 | co->co_name = name; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 300 | co->co_firstlineno = firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 301 | Py_INCREF(lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 302 | co->co_lnotab = lnotab; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 303 | /* PyObject_Print((PyObject *)co, stderr, 0); */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 304 | } |
| 305 | return co; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | /* Data structure used internally */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 310 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 311 | /* The compiler uses two passes to generate bytecodes. The first pass |
| 312 | builds the symbol table. The second pass generates the bytecode. |
| 313 | |
| 314 | The first pass uses a single symtable struct. The second pass uses |
| 315 | a compiling struct for each code block. The compiling structs |
| 316 | share a reference to the symtable. |
| 317 | |
| 318 | The two passes communicate via symtable_load_symbols() and via |
| 319 | is_local() and is_global(). The former initializes several slots |
| 320 | in the compiling struct: c_varnames, c_locals, c_nlocals, |
| 321 | c_argcount, c_globals, and c_flags. |
| 322 | */ |
| 323 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 324 | struct compiling { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 325 | PyObject *c_code; /* string */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 326 | PyObject *c_consts; /* list of objects */ |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 327 | PyObject *c_const_dict; /* inverse of c_consts */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 328 | PyObject *c_names; /* list of strings (names) */ |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 329 | PyObject *c_name_dict; /* inverse of c_names */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 330 | PyObject *c_globals; /* dictionary (value=None) */ |
| 331 | PyObject *c_locals; /* dictionary (value=localID) */ |
| 332 | PyObject *c_varnames; /* list (inverse of c_locals) */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 333 | PyObject *c_freevars; /* dictionary (value=None) */ |
| 334 | PyObject *c_cellvars; /* list */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 335 | int c_nlocals; /* index of next local */ |
| 336 | int c_argcount; /* number of top-level arguments */ |
| 337 | int c_flags; /* same as co_flags */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 338 | int c_nexti; /* index into c_code */ |
| 339 | int c_errors; /* counts errors occurred */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 340 | int c_infunction; /* set when compiling a function */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 341 | int c_interactive; /* generating code for interactive command */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 342 | int c_loops; /* counts nested loops */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 343 | int c_begin; /* begin of current loop, for 'continue' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 344 | int c_block[CO_MAXBLOCKS]; /* stack of block types */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 345 | int c_nblocks; /* current block stack level */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 346 | char *c_filename; /* filename of current node */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 347 | char *c_name; /* name of object (e.g. function) */ |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 348 | int c_lineno; /* Current line number */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 349 | int c_stacklevel; /* Current stack level */ |
| 350 | int c_maxstacklevel; /* Maximum stack level */ |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 351 | int c_firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 352 | PyObject *c_lnotab; /* Table mapping address to line number */ |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 353 | int c_last_addr, c_last_line, c_lnotab_next; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 354 | char *c_private; /* for private name mangling */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 355 | int c_tmpname; /* temporary local name counter */ |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 356 | int c_nested; /* Is block nested funcdef or lamdef? */ |
| 357 | int c_closure; /* Is nested w/freevars? */ |
| 358 | struct symtable *c_symtable; /* pointer to module symbol table */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 359 | }; |
| 360 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 361 | int is_free(int v) |
| 362 | { |
| 363 | if ((v & (USE | DEF_FREE)) |
| 364 | && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL))) |
| 365 | return 1; |
| 366 | if (v & DEF_FREE_CLASS) |
| 367 | return 1; |
| 368 | return 0; |
| 369 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 370 | |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 371 | /* Error message including line number */ |
| 372 | |
| 373 | static void |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 374 | set_error_location(char *filename, int lineno) |
| 375 | { |
| 376 | PyObject *exc, *v, *tb, *tmp; |
| 377 | |
| 378 | /* add attributes for the line number and filename for the error */ |
| 379 | PyErr_Fetch(&exc, &v, &tb); |
| 380 | PyErr_NormalizeException(&exc, &v, &tb); |
| 381 | tmp = PyInt_FromLong(lineno); |
| 382 | if (tmp == NULL) |
| 383 | PyErr_Clear(); |
| 384 | else { |
| 385 | if (PyObject_SetAttrString(v, "lineno", tmp)) |
| 386 | PyErr_Clear(); |
| 387 | Py_DECREF(tmp); |
| 388 | } |
| 389 | if (filename != NULL) { |
| 390 | tmp = PyString_FromString(filename); |
| 391 | if (tmp == NULL) |
| 392 | PyErr_Clear(); |
| 393 | else { |
| 394 | if (PyObject_SetAttrString(v, "filename", tmp)) |
| 395 | PyErr_Clear(); |
| 396 | Py_DECREF(tmp); |
| 397 | } |
| 398 | } |
| 399 | PyErr_Restore(exc, v, tb); |
| 400 | } |
| 401 | |
| 402 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 403 | com_error(struct compiling *c, PyObject *exc, char *msg) |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 404 | { |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 405 | PyObject *v; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 406 | if (c == NULL) { |
| 407 | /* Error occurred via symtable call to |
| 408 | is_constant_false */ |
| 409 | PyErr_SetString(exc, msg); |
| 410 | return; |
| 411 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 412 | c->c_errors++; |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 413 | if (c->c_lineno <= 1) { |
| 414 | /* Unknown line number or single interactive command */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 415 | PyErr_SetString(exc, msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 416 | return; |
| 417 | } |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 418 | v = PyString_FromString(msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 419 | if (v == NULL) |
| 420 | return; /* MemoryError, too bad */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 421 | PyErr_SetObject(exc, v); |
| 422 | Py_DECREF(v); |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 423 | |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 424 | set_error_location(c->c_filename, c->c_lineno); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 428 | /* Interface to the block stack */ |
| 429 | |
| 430 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 431 | block_push(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 432 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 433 | if (c->c_nblocks >= CO_MAXBLOCKS) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 434 | com_error(c, PyExc_SystemError, |
| 435 | "too many statically nested blocks"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 436 | } |
| 437 | else { |
| 438 | c->c_block[c->c_nblocks++] = type; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 443 | block_pop(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 444 | { |
| 445 | if (c->c_nblocks > 0) |
| 446 | c->c_nblocks--; |
| 447 | 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] | 448 | com_error(c, PyExc_SystemError, "bad block pop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
| 452 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 453 | /* Prototype forward declarations */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 454 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 455 | static int com_init(struct compiling *, char *); |
| 456 | static void com_free(struct compiling *); |
| 457 | static void com_push(struct compiling *, int); |
| 458 | static void com_pop(struct compiling *, int); |
| 459 | static void com_done(struct compiling *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 460 | static void com_node(struct compiling *, node *); |
| 461 | static void com_factor(struct compiling *, node *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 462 | static void com_addbyte(struct compiling *, int); |
| 463 | static void com_addint(struct compiling *, int); |
| 464 | static void com_addoparg(struct compiling *, int, int); |
| 465 | static void com_addfwref(struct compiling *, int, int *); |
| 466 | static void com_backpatch(struct compiling *, int); |
| 467 | static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *); |
| 468 | static int com_addconst(struct compiling *, PyObject *); |
| 469 | static int com_addname(struct compiling *, PyObject *); |
| 470 | static void com_addopname(struct compiling *, int, node *); |
| 471 | static void com_list(struct compiling *, node *, int); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 472 | static void com_list_iter(struct compiling *, node *, node *, char *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 473 | static int com_argdefs(struct compiling *, node *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 474 | static void com_assign(struct compiling *, node *, int, node *); |
| 475 | static void com_assign_name(struct compiling *, node *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 476 | static PyCodeObject *icompile(node *, struct compiling *); |
| 477 | static PyCodeObject *jcompile(node *, char *, struct compiling *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 478 | static PyObject *parsestrplus(node *); |
| 479 | static PyObject *parsestr(char *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 480 | static node *get_rawdocstring(node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 481 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 482 | static int get_ref_type(struct compiling *, char *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 483 | |
| 484 | /* symtable operations */ |
| 485 | static int symtable_build(struct compiling *, node *); |
| 486 | static int symtable_load_symbols(struct compiling *); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 487 | static struct symtable *symtable_init(void); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 488 | static void symtable_enter_scope(struct symtable *, char *, int, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 489 | static int symtable_exit_scope(struct symtable *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 490 | static int symtable_add_def(struct symtable *, char *, int); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 491 | static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 492 | |
| 493 | static void symtable_node(struct symtable *, node *); |
| 494 | static void symtable_funcdef(struct symtable *, node *); |
| 495 | static void symtable_default_args(struct symtable *, node *); |
| 496 | static void symtable_params(struct symtable *, node *); |
| 497 | static void symtable_params_fplist(struct symtable *, node *n); |
| 498 | static void symtable_global(struct symtable *, node *); |
| 499 | static void symtable_import(struct symtable *, node *); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 500 | static void symtable_assign(struct symtable *, node *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 501 | static void symtable_list_comprehension(struct symtable *, node *); |
| 502 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 503 | static int symtable_update_free_vars(struct symtable *); |
| 504 | static int symtable_undo_free(struct symtable *, PyObject *, PyObject *); |
| 505 | static int symtable_check_global(struct symtable *, PyObject *, PyObject *); |
| 506 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 507 | /* helper */ |
| 508 | static void |
| 509 | do_pad(int pad) |
| 510 | { |
| 511 | int i; |
| 512 | for (i = 0; i < pad; ++i) |
| 513 | fprintf(stderr, " "); |
| 514 | } |
| 515 | |
| 516 | static void |
| 517 | dump(node *n, int pad, int depth) |
| 518 | { |
| 519 | int i; |
| 520 | if (depth == 0) |
| 521 | return; |
| 522 | do_pad(pad); |
| 523 | fprintf(stderr, "%d: %s\n", TYPE(n), STR(n)); |
| 524 | if (depth > 0) |
| 525 | depth--; |
| 526 | for (i = 0; i < NCH(n); ++i) |
| 527 | dump(CHILD(n, i), pad + 1, depth); |
| 528 | } |
| 529 | |
| 530 | #define DUMP(N) dump(N, 0, -1) |
| 531 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 532 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 533 | com_init(struct compiling *c, char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 534 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 535 | memset((void *)c, '\0', sizeof(struct compiling)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 536 | if ((c->c_code = PyString_FromStringAndSize((char *)NULL, |
| 537 | 1000)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 538 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 539 | if ((c->c_consts = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 540 | goto fail; |
| 541 | if ((c->c_const_dict = PyDict_New()) == NULL) |
| 542 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 543 | if ((c->c_names = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 544 | goto fail; |
| 545 | if ((c->c_name_dict = PyDict_New()) == NULL) |
| 546 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 547 | if ((c->c_locals = PyDict_New()) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 548 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 549 | if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL, |
| 550 | 1000)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 551 | goto fail; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 552 | c->c_globals = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 553 | c->c_varnames = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 554 | c->c_freevars = NULL; |
| 555 | c->c_cellvars = NULL; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 556 | c->c_nlocals = 0; |
| 557 | c->c_argcount = 0; |
| 558 | c->c_flags = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 559 | c->c_nexti = 0; |
| 560 | c->c_errors = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 561 | c->c_infunction = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 562 | c->c_interactive = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 563 | c->c_loops = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 564 | c->c_begin = 0; |
| 565 | c->c_nblocks = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 566 | c->c_filename = filename; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 567 | c->c_name = "?"; |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 568 | c->c_lineno = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 569 | c->c_stacklevel = 0; |
| 570 | c->c_maxstacklevel = 0; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 571 | c->c_firstlineno = 0; |
| 572 | c->c_last_addr = 0; |
| 573 | c->c_last_line = 0; |
Barry Warsaw | 174e801 | 2001-01-22 04:35:57 +0000 | [diff] [blame] | 574 | c->c_lnotab_next = 0; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 575 | c->c_tmpname = 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 576 | c->c_nested = 0; |
| 577 | c->c_closure = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 578 | c->c_symtable = NULL; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 579 | return 1; |
| 580 | |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 581 | fail: |
| 582 | com_free(c); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 587 | com_free(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 588 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 589 | Py_XDECREF(c->c_code); |
| 590 | Py_XDECREF(c->c_consts); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 591 | Py_XDECREF(c->c_const_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 592 | Py_XDECREF(c->c_names); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 593 | Py_XDECREF(c->c_name_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 594 | Py_XDECREF(c->c_globals); |
| 595 | Py_XDECREF(c->c_locals); |
| 596 | Py_XDECREF(c->c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 597 | Py_XDECREF(c->c_freevars); |
| 598 | Py_XDECREF(c->c_cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 599 | Py_XDECREF(c->c_lnotab); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 603 | com_push(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 604 | { |
| 605 | c->c_stacklevel += n; |
| 606 | if (c->c_stacklevel > c->c_maxstacklevel) |
| 607 | c->c_maxstacklevel = c->c_stacklevel; |
| 608 | } |
| 609 | |
| 610 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 611 | com_pop(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 612 | { |
| 613 | if (c->c_stacklevel < n) { |
Guido van Rossum | 90f827c | 1998-08-25 18:22:17 +0000 | [diff] [blame] | 614 | /* fprintf(stderr, |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 615 | "%s:%d: underflow! nexti=%d, level=%d, n=%d\n", |
| 616 | c->c_filename, c->c_lineno, |
Guido van Rossum | 90f827c | 1998-08-25 18:22:17 +0000 | [diff] [blame] | 617 | c->c_nexti, c->c_stacklevel, n); */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 618 | c->c_stacklevel = 0; |
| 619 | } |
| 620 | else |
| 621 | c->c_stacklevel -= n; |
| 622 | } |
| 623 | |
| 624 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 625 | com_done(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 626 | { |
| 627 | if (c->c_code != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 628 | _PyString_Resize(&c->c_code, c->c_nexti); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 629 | if (c->c_lnotab != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 630 | _PyString_Resize(&c->c_lnotab, c->c_lnotab_next); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 634 | com_addbyte(struct compiling *c, int byte) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 635 | { |
| 636 | int len; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 637 | /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 638 | assert(byte >= 0 && byte <= 255); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 639 | if (byte < 0 || byte > 255) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 640 | com_error(c, PyExc_SystemError, |
| 641 | "com_addbyte: byte out of range"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 642 | } |
| 643 | if (c->c_code == NULL) |
| 644 | return; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 645 | len = PyString_Size(c->c_code); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 646 | if (c->c_nexti >= len) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 647 | if (_PyString_Resize(&c->c_code, len+1000) != 0) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 648 | c->c_errors++; |
| 649 | return; |
| 650 | } |
| 651 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 652 | PyString_AsString(c->c_code)[c->c_nexti++] = byte; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 656 | com_addint(struct compiling *c, int x) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 657 | { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 658 | com_addbyte(c, x & 0xff); |
| 659 | com_addbyte(c, x >> 8); /* XXX x should be positive */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 663 | com_add_lnotab(struct compiling *c, int addr, int line) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 664 | { |
| 665 | int size; |
| 666 | char *p; |
| 667 | if (c->c_lnotab == NULL) |
| 668 | return; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 669 | size = PyString_Size(c->c_lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 670 | if (c->c_lnotab_next+2 > size) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 671 | if (_PyString_Resize(&c->c_lnotab, size + 1000) < 0) { |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 672 | c->c_errors++; |
| 673 | return; |
| 674 | } |
| 675 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 676 | p = PyString_AsString(c->c_lnotab) + c->c_lnotab_next; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 677 | *p++ = addr; |
| 678 | *p++ = line; |
| 679 | c->c_lnotab_next += 2; |
| 680 | } |
| 681 | |
| 682 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 683 | com_set_lineno(struct compiling *c, int lineno) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 684 | { |
| 685 | c->c_lineno = lineno; |
| 686 | if (c->c_firstlineno == 0) { |
| 687 | c->c_firstlineno = c->c_last_line = lineno; |
| 688 | } |
| 689 | else { |
| 690 | int incr_addr = c->c_nexti - c->c_last_addr; |
| 691 | int incr_line = lineno - c->c_last_line; |
| 692 | while (incr_addr > 0 || incr_line > 0) { |
| 693 | int trunc_addr = incr_addr; |
| 694 | int trunc_line = incr_line; |
| 695 | if (trunc_addr > 255) |
| 696 | trunc_addr = 255; |
| 697 | if (trunc_line > 255) |
| 698 | trunc_line = 255; |
| 699 | com_add_lnotab(c, trunc_addr, trunc_line); |
| 700 | incr_addr -= trunc_addr; |
| 701 | incr_line -= trunc_line; |
| 702 | } |
| 703 | c->c_last_addr = c->c_nexti; |
| 704 | c->c_last_line = lineno; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 709 | com_addoparg(struct compiling *c, int op, int arg) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 710 | { |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 711 | int extended_arg = arg >> 16; |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 712 | if (op == SET_LINENO) { |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 713 | com_set_lineno(c, arg); |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 714 | if (Py_OptimizeFlag) |
| 715 | return; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 716 | } |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 717 | if (extended_arg){ |
| 718 | com_addbyte(c, EXTENDED_ARG); |
| 719 | com_addint(c, extended_arg); |
| 720 | arg &= 0xffff; |
| 721 | } |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 722 | com_addbyte(c, op); |
| 723 | com_addint(c, arg); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 727 | com_addfwref(struct compiling *c, int op, int *p_anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 728 | { |
| 729 | /* Compile a forward reference for backpatching */ |
| 730 | int here; |
| 731 | int anchor; |
| 732 | com_addbyte(c, op); |
| 733 | here = c->c_nexti; |
| 734 | anchor = *p_anchor; |
| 735 | *p_anchor = here; |
| 736 | com_addint(c, anchor == 0 ? 0 : here - anchor); |
| 737 | } |
| 738 | |
| 739 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 740 | com_backpatch(struct compiling *c, int anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 741 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 742 | unsigned char *code = (unsigned char *) PyString_AsString(c->c_code); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 743 | int target = c->c_nexti; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 744 | int dist; |
| 745 | int prev; |
| 746 | for (;;) { |
| 747 | /* Make the JUMP instruction at anchor point to target */ |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 748 | prev = code[anchor] + (code[anchor+1] << 8); |
| 749 | dist = target - (anchor+2); |
| 750 | code[anchor] = dist & 0xff; |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 751 | dist >>= 8; |
| 752 | code[anchor+1] = dist; |
| 753 | dist >>= 8; |
| 754 | if (dist) { |
| 755 | com_error(c, PyExc_SystemError, |
| 756 | "com_backpatch: offset too large"); |
| 757 | break; |
| 758 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 759 | if (!prev) |
| 760 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 761 | anchor -= prev; |
| 762 | } |
| 763 | } |
| 764 | |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 765 | /* Handle literals and names uniformly */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 766 | |
| 767 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 768 | com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 769 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 770 | PyObject *w, *t, *np=NULL; |
| 771 | long n; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 772 | |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 773 | t = Py_BuildValue("(OO)", v, v->ob_type); |
| 774 | if (t == NULL) |
| 775 | goto fail; |
| 776 | w = PyDict_GetItem(dict, t); |
| 777 | if (w != NULL) { |
| 778 | n = PyInt_AsLong(w); |
| 779 | } else { |
| 780 | n = PyList_Size(list); |
| 781 | np = PyInt_FromLong(n); |
| 782 | if (np == NULL) |
| 783 | goto fail; |
| 784 | if (PyList_Append(list, v) != 0) |
| 785 | goto fail; |
| 786 | if (PyDict_SetItem(dict, t, np) != 0) |
| 787 | goto fail; |
| 788 | Py_DECREF(np); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 789 | } |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 790 | Py_DECREF(t); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 791 | return n; |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 792 | fail: |
| 793 | Py_XDECREF(np); |
| 794 | Py_XDECREF(t); |
| 795 | c->c_errors++; |
| 796 | return 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 800 | com_addconst(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 801 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 802 | 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] | 803 | } |
| 804 | |
| 805 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 806 | com_addname(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 807 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 808 | 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] | 809 | } |
| 810 | |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 811 | static int |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 812 | mangle(char *p, char *name, char *buffer, size_t maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 813 | { |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 814 | /* Name mangling: __private becomes _classname__private. |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 815 | This is independent from how the name is used. */ |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 816 | size_t nlen, plen; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 817 | if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_') |
| 818 | return 0; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 819 | nlen = strlen(name); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 820 | if (nlen+2 >= maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 821 | return 0; /* Don't mangle __extremely_long_names */ |
| 822 | if (name[nlen-1] == '_' && name[nlen-2] == '_') |
| 823 | return 0; /* Don't mangle __whatever__ */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 824 | /* Strip leading underscores from class name */ |
| 825 | while (*p == '_') |
| 826 | p++; |
| 827 | if (*p == '\0') |
| 828 | return 0; /* Don't mangle if class is just underscores */ |
| 829 | plen = strlen(p); |
| 830 | if (plen + nlen >= maxlen) |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 831 | plen = maxlen-nlen-2; /* Truncate class name if too long */ |
| 832 | /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 833 | buffer[0] = '_'; |
| 834 | strncpy(buffer+1, p, plen); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 835 | strcpy(buffer+1+plen, name); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 836 | return 1; |
| 837 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 838 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 839 | static void |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 840 | com_addop_name(struct compiling *c, int op, char *name) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 841 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 842 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 843 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 844 | char buffer[MANGLE_LEN]; |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 845 | |
| 846 | if (mangle(c->c_private, name, buffer, sizeof(buffer))) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 847 | name = buffer; |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 848 | if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 849 | c->c_errors++; |
| 850 | i = 255; |
| 851 | } |
| 852 | else { |
| 853 | i = com_addname(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 854 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 855 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 856 | com_addoparg(c, op, i); |
| 857 | } |
| 858 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 859 | #define NAME_LOCAL 0 |
| 860 | #define NAME_GLOBAL 1 |
| 861 | #define NAME_DEFAULT 2 |
| 862 | #define NAME_CLOSURE 3 |
| 863 | |
| 864 | static int |
| 865 | com_lookup_arg(PyObject *dict, PyObject *name) |
| 866 | { |
| 867 | PyObject *v = PyDict_GetItem(dict, name); |
| 868 | if (v == NULL) |
| 869 | return -1; |
| 870 | else |
| 871 | return PyInt_AS_LONG(v); |
| 872 | } |
| 873 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 874 | static void |
| 875 | com_addop_varname(struct compiling *c, int kind, char *name) |
| 876 | { |
| 877 | PyObject *v; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 878 | int i, reftype; |
| 879 | int scope = NAME_DEFAULT; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 880 | int op = STOP_CODE; |
| 881 | char buffer[MANGLE_LEN]; |
| 882 | |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 883 | if (mangle(c->c_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 884 | name = buffer; |
| 885 | if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { |
| 886 | c->c_errors++; |
| 887 | i = 255; |
| 888 | goto done; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 889 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 890 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 891 | reftype = get_ref_type(c, name); |
| 892 | switch (reftype) { |
| 893 | case LOCAL: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 894 | if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 895 | scope = NAME_LOCAL; |
| 896 | break; |
| 897 | case GLOBAL_EXPLICIT: |
| 898 | scope = NAME_GLOBAL; |
| 899 | break; |
| 900 | case GLOBAL_IMPLICIT: |
| 901 | if (c->c_flags & CO_OPTIMIZED) |
| 902 | scope = NAME_GLOBAL; |
| 903 | break; |
| 904 | case FREE: |
| 905 | case CELL: |
| 906 | scope = NAME_CLOSURE; |
| 907 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | i = com_addname(c, v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 911 | if (scope == NAME_LOCAL) |
| 912 | i = com_lookup_arg(c->c_locals, v); |
| 913 | else if (reftype == FREE) |
| 914 | i = com_lookup_arg(c->c_freevars, v); |
| 915 | else if (reftype == CELL) |
| 916 | i = com_lookup_arg(c->c_cellvars, v); |
| 917 | if (i == -1) { |
| 918 | c->c_errors++; /* XXX no exception set */ |
| 919 | i = 255; |
| 920 | goto done; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 921 | } |
| 922 | Py_DECREF(v); |
| 923 | |
| 924 | switch (kind) { |
| 925 | case VAR_LOAD: |
| 926 | switch (scope) { |
| 927 | case NAME_LOCAL: |
| 928 | op = LOAD_FAST; |
| 929 | break; |
| 930 | case NAME_GLOBAL: |
| 931 | op = LOAD_GLOBAL; |
| 932 | break; |
| 933 | case NAME_DEFAULT: |
| 934 | op = LOAD_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 935 | break; |
| 936 | case NAME_CLOSURE: |
| 937 | op = LOAD_DEREF; |
| 938 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 939 | } |
| 940 | break; |
| 941 | case VAR_STORE: |
| 942 | switch (scope) { |
| 943 | case NAME_LOCAL: |
| 944 | op = STORE_FAST; |
| 945 | break; |
| 946 | case NAME_GLOBAL: |
| 947 | op = STORE_GLOBAL; |
| 948 | break; |
| 949 | case NAME_DEFAULT: |
| 950 | op = STORE_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 951 | break; |
| 952 | case NAME_CLOSURE: |
| 953 | op = STORE_DEREF; |
| 954 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 955 | } |
| 956 | break; |
| 957 | case VAR_DELETE: |
| 958 | switch (scope) { |
| 959 | case NAME_LOCAL: |
| 960 | op = DELETE_FAST; |
| 961 | break; |
| 962 | case NAME_GLOBAL: |
| 963 | op = DELETE_GLOBAL; |
| 964 | break; |
| 965 | case NAME_DEFAULT: |
| 966 | op = DELETE_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 967 | break; |
| 968 | case NAME_CLOSURE: { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 969 | char buf[500]; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 970 | sprintf(buf, DEL_CLOSURE_ERROR, name); |
| 971 | com_error(c, PyExc_SyntaxError, buf); |
| 972 | i = 255; |
| 973 | break; |
| 974 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 975 | } |
| 976 | break; |
| 977 | } |
| 978 | done: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 979 | com_addoparg(c, op, i); |
| 980 | } |
| 981 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 982 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 983 | com_addopname(struct compiling *c, int op, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 984 | { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 985 | char *name; |
| 986 | char buffer[1000]; |
| 987 | /* XXX it is possible to write this code without the 1000 |
| 988 | chars on the total length of dotted names, I just can't be |
| 989 | bothered right now */ |
| 990 | if (TYPE(n) == STAR) |
| 991 | name = "*"; |
| 992 | else if (TYPE(n) == dotted_name) { |
| 993 | char *p = buffer; |
| 994 | int i; |
| 995 | name = buffer; |
| 996 | for (i = 0; i < NCH(n); i += 2) { |
| 997 | char *s = STR(CHILD(n, i)); |
| 998 | if (p + strlen(s) > buffer + (sizeof buffer) - 2) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 999 | com_error(c, PyExc_MemoryError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1000 | "dotted_name too long"); |
Guido van Rossum | d9dfaf5 | 1995-02-17 15:04:57 +0000 | [diff] [blame] | 1001 | name = NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1002 | break; |
| 1003 | } |
| 1004 | if (p != buffer) |
| 1005 | *p++ = '.'; |
| 1006 | strcpy(p, s); |
| 1007 | p = strchr(p, '\0'); |
| 1008 | } |
| 1009 | } |
| 1010 | else { |
| 1011 | REQ(n, NAME); |
| 1012 | name = STR(n); |
| 1013 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1014 | com_addop_name(c, op, name); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1017 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1018 | parsenumber(struct compiling *co, char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1019 | { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1020 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1021 | long x; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1022 | double dx; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1023 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 1024 | Py_complex c; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1025 | int imflag; |
| 1026 | #endif |
| 1027 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1028 | errno = 0; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1029 | end = s + strlen(s) - 1; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1030 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 15ad9a6 | 1996-01-26 20:53:56 +0000 | [diff] [blame] | 1031 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1032 | #endif |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 1033 | if (*end == 'l' || *end == 'L') |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1034 | return PyLong_FromString(s, (char **)0, 0); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 1035 | if (s[0] == '0') |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1036 | x = (long) PyOS_strtoul(s, &end, 0); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 1037 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1038 | x = PyOS_strtol(s, &end, 0); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1039 | if (*end == '\0') { |
| 1040 | if (errno != 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1041 | com_error(co, PyExc_OverflowError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1042 | "integer literal too large"); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1043 | return NULL; |
| 1044 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1045 | return PyInt_FromLong(x); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1046 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1047 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1048 | #ifndef WITHOUT_COMPLEX |
| 1049 | if (imflag) { |
| 1050 | c.real = 0.; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1051 | PyFPE_START_PROTECT("atof", return 0) |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1052 | c.imag = atof(s); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1053 | PyFPE_END_PROTECT(c) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1054 | return PyComplex_FromCComplex(c); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1055 | } |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1056 | else |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1057 | #endif |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1058 | { |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1059 | PyFPE_START_PROTECT("atof", return 0) |
| 1060 | dx = atof(s); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1061 | PyFPE_END_PROTECT(dx) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1062 | return PyFloat_FromDouble(dx); |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1063 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1066 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1067 | parsestr(char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1068 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1069 | PyObject *v; |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1070 | size_t len; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1071 | char *buf; |
| 1072 | char *p; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1073 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1074 | int c; |
Guido van Rossum | 5f5e817 | 1997-04-06 03:41:40 +0000 | [diff] [blame] | 1075 | int first = *s; |
| 1076 | int quote = first; |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1077 | int rawmode = 0; |
| 1078 | int unicode = 0; |
| 1079 | if (isalpha(quote) || quote == '_') { |
| 1080 | if (quote == 'u' || quote == 'U') { |
| 1081 | quote = *++s; |
| 1082 | unicode = 1; |
| 1083 | } |
| 1084 | if (quote == 'r' || quote == 'R') { |
| 1085 | quote = *++s; |
| 1086 | rawmode = 1; |
| 1087 | } |
| 1088 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1089 | if (quote != '\'' && quote != '\"') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1090 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1091 | return NULL; |
| 1092 | } |
| 1093 | s++; |
| 1094 | len = strlen(s); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1095 | if (len > INT_MAX) { |
| 1096 | PyErr_SetString(PyExc_OverflowError, "string to parse is too long"); |
| 1097 | return NULL; |
| 1098 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1099 | if (s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1100 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1101 | return NULL; |
| 1102 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1103 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
| 1104 | s += 2; |
| 1105 | len -= 2; |
| 1106 | if (s[--len] != quote || s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1107 | PyErr_BadInternalCall(); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1108 | return NULL; |
| 1109 | } |
| 1110 | } |
Guido van Rossum | fdc8bdb | 2000-05-01 17:54:56 +0000 | [diff] [blame] | 1111 | if (unicode || Py_UnicodeFlag) { |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1112 | if (rawmode) |
| 1113 | return PyUnicode_DecodeRawUnicodeEscape( |
| 1114 | s, len, NULL); |
| 1115 | else |
| 1116 | return PyUnicode_DecodeUnicodeEscape( |
| 1117 | s, len, NULL); |
| 1118 | } |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1119 | if (rawmode || strchr(s, '\\') == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1120 | return PyString_FromStringAndSize(s, len); |
| 1121 | v = PyString_FromStringAndSize((char *)NULL, len); |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1122 | if (v == NULL) |
| 1123 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1124 | p = buf = PyString_AsString(v); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1125 | end = s + len; |
| 1126 | while (s < end) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1127 | if (*s != '\\') { |
| 1128 | *p++ = *s++; |
| 1129 | continue; |
| 1130 | } |
| 1131 | s++; |
| 1132 | switch (*s++) { |
| 1133 | /* XXX This assumes ASCII! */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1134 | case '\n': break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1135 | case '\\': *p++ = '\\'; break; |
| 1136 | case '\'': *p++ = '\''; break; |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1137 | case '\"': *p++ = '\"'; break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1138 | case 'b': *p++ = '\b'; break; |
| 1139 | case 'f': *p++ = '\014'; break; /* FF */ |
| 1140 | case 't': *p++ = '\t'; break; |
| 1141 | case 'n': *p++ = '\n'; break; |
| 1142 | case 'r': *p++ = '\r'; break; |
| 1143 | case 'v': *p++ = '\013'; break; /* VT */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1144 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 1145 | case '0': case '1': case '2': case '3': |
| 1146 | case '4': case '5': case '6': case '7': |
| 1147 | c = s[-1] - '0'; |
| 1148 | if ('0' <= *s && *s <= '7') { |
| 1149 | c = (c<<3) + *s++ - '0'; |
| 1150 | if ('0' <= *s && *s <= '7') |
| 1151 | c = (c<<3) + *s++ - '0'; |
| 1152 | } |
| 1153 | *p++ = c; |
| 1154 | break; |
| 1155 | case 'x': |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1156 | if (isxdigit(Py_CHARMASK(s[0])) && isxdigit(Py_CHARMASK(s[1]))) { |
Guido van Rossum | ed1100f | 1997-10-20 23:24:07 +0000 | [diff] [blame] | 1157 | unsigned int x = 0; |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1158 | c = Py_CHARMASK(*s); |
| 1159 | s++; |
| 1160 | if (isdigit(c)) |
| 1161 | x = c - '0'; |
| 1162 | else if (islower(c)) |
| 1163 | x = 10 + c - 'a'; |
| 1164 | else |
| 1165 | x = 10 + c - 'A'; |
| 1166 | x = x << 4; |
| 1167 | c = Py_CHARMASK(*s); |
| 1168 | s++; |
| 1169 | if (isdigit(c)) |
| 1170 | x += c - '0'; |
| 1171 | else if (islower(c)) |
| 1172 | x += 10 + c - 'a'; |
| 1173 | else |
| 1174 | x += 10 + c - 'A'; |
Guido van Rossum | ed1100f | 1997-10-20 23:24:07 +0000 | [diff] [blame] | 1175 | *p++ = x; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1176 | break; |
| 1177 | } |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1178 | PyErr_SetString(PyExc_ValueError, "invalid \\x escape"); |
| 1179 | Py_DECREF(v); |
| 1180 | return NULL; |
| 1181 | default: |
| 1182 | *p++ = '\\'; |
| 1183 | *p++ = s[-1]; |
| 1184 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1185 | } |
| 1186 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1187 | _PyString_Resize(&v, (int)(p - buf)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1188 | return v; |
| 1189 | } |
| 1190 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1191 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1192 | parsestrplus(node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1193 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1194 | PyObject *v; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1195 | int i; |
| 1196 | REQ(CHILD(n, 0), STRING); |
| 1197 | if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) { |
| 1198 | /* String literal concatenation */ |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1199 | for (i = 1; i < NCH(n); i++) { |
| 1200 | PyObject *s; |
| 1201 | s = parsestr(STR(CHILD(n, i))); |
| 1202 | if (s == NULL) |
| 1203 | goto onError; |
| 1204 | if (PyString_Check(v) && PyString_Check(s)) { |
| 1205 | PyString_ConcatAndDel(&v, s); |
| 1206 | if (v == NULL) |
| 1207 | goto onError; |
| 1208 | } |
| 1209 | else { |
| 1210 | PyObject *temp; |
| 1211 | temp = PyUnicode_Concat(v, s); |
| 1212 | Py_DECREF(s); |
| 1213 | if (temp == NULL) |
| 1214 | goto onError; |
| 1215 | Py_DECREF(v); |
| 1216 | v = temp; |
| 1217 | } |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1218 | } |
| 1219 | } |
| 1220 | return v; |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1221 | |
| 1222 | onError: |
| 1223 | Py_XDECREF(v); |
| 1224 | return NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1227 | static void |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1228 | 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] | 1229 | { |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1230 | PyObject *v; |
| 1231 | int anchor = 0; |
| 1232 | int save_begin = c->c_begin; |
| 1233 | |
| 1234 | /* list_iter: for v in expr [list_iter] */ |
| 1235 | com_node(c, CHILD(n, 3)); /* expr */ |
| 1236 | v = PyInt_FromLong(0L); |
| 1237 | if (v == NULL) |
| 1238 | c->c_errors++; |
| 1239 | com_addoparg(c, LOAD_CONST, com_addconst(c, v)); |
| 1240 | com_push(c, 1); |
| 1241 | Py_XDECREF(v); |
| 1242 | c->c_begin = c->c_nexti; |
| 1243 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 1244 | com_addfwref(c, FOR_LOOP, &anchor); |
| 1245 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1246 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1247 | c->c_loops++; |
| 1248 | com_list_iter(c, n, e, t); |
| 1249 | c->c_loops--; |
| 1250 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 1251 | c->c_begin = save_begin; |
| 1252 | com_backpatch(c, anchor); |
| 1253 | com_pop(c, 2); /* FOR_LOOP has popped these */ |
| 1254 | } |
| 1255 | |
| 1256 | static void |
| 1257 | com_list_if(struct compiling *c, node *n, node *e, char *t) |
| 1258 | { |
| 1259 | int anchor = 0; |
| 1260 | int a = 0; |
| 1261 | /* list_iter: 'if' test [list_iter] */ |
| 1262 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 1263 | com_node(c, CHILD(n, 1)); |
| 1264 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 1265 | com_addbyte(c, POP_TOP); |
| 1266 | com_pop(c, 1); |
| 1267 | com_list_iter(c, n, e, t); |
| 1268 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 1269 | com_backpatch(c, a); |
| 1270 | /* We jump here with an extra entry which we now pop */ |
| 1271 | com_addbyte(c, POP_TOP); |
| 1272 | com_backpatch(c, anchor); |
| 1273 | } |
| 1274 | |
| 1275 | static void |
| 1276 | com_list_iter(struct compiling *c, |
| 1277 | node *p, /* parent of list_iter node */ |
| 1278 | node *e, /* element expression node */ |
| 1279 | char *t /* name of result list temp local */) |
| 1280 | { |
| 1281 | /* list_iter is the last child in a listmaker, list_for, or list_if */ |
| 1282 | node *n = CHILD(p, NCH(p)-1); |
| 1283 | if (TYPE(n) == list_iter) { |
| 1284 | n = CHILD(n, 0); |
| 1285 | switch (TYPE(n)) { |
| 1286 | case list_for: |
| 1287 | com_list_for(c, n, e, t); |
| 1288 | break; |
| 1289 | case list_if: |
| 1290 | com_list_if(c, n, e, t); |
| 1291 | break; |
| 1292 | default: |
| 1293 | com_error(c, PyExc_SystemError, |
| 1294 | "invalid list_iter node type"); |
| 1295 | } |
| 1296 | } |
| 1297 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1298 | com_addop_varname(c, VAR_LOAD, t); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1299 | com_push(c, 1); |
| 1300 | com_node(c, e); |
| 1301 | com_addoparg(c, CALL_FUNCTION, 1); |
| 1302 | com_addbyte(c, POP_TOP); |
| 1303 | com_pop(c, 2); |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | static void |
| 1308 | com_list_comprehension(struct compiling *c, node *n) |
| 1309 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1310 | /* listmaker: test list_for */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1311 | char tmpname[12]; |
Tim Peters | 3081421 | 2001-02-17 05:30:26 +0000 | [diff] [blame] | 1312 | sprintf(tmpname, "_[%d]", ++c->c_tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1313 | com_addoparg(c, BUILD_LIST, 0); |
| 1314 | com_addbyte(c, DUP_TOP); /* leave the result on the stack */ |
| 1315 | com_push(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1316 | com_addop_name(c, LOAD_ATTR, "append"); |
| 1317 | com_addop_varname(c, VAR_STORE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1318 | com_pop(c, 1); |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1319 | com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1320 | com_addop_varname(c, VAR_DELETE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1321 | --c->c_tmpname; |
| 1322 | } |
| 1323 | |
| 1324 | static void |
| 1325 | com_listmaker(struct compiling *c, node *n) |
| 1326 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1327 | /* listmaker: test ( list_for | (',' test)* [','] ) */ |
| 1328 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1329 | com_list_comprehension(c, n); |
| 1330 | else { |
| 1331 | int len = 0; |
| 1332 | int i; |
| 1333 | for (i = 0; i < NCH(n); i += 2, len++) |
| 1334 | com_node(c, CHILD(n, i)); |
| 1335 | com_addoparg(c, BUILD_LIST, len); |
| 1336 | com_pop(c, len-1); |
| 1337 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1341 | com_dictmaker(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1342 | { |
| 1343 | int i; |
| 1344 | /* dictmaker: test ':' test (',' test ':' value)* [','] */ |
| 1345 | for (i = 0; i+2 < NCH(n); i += 4) { |
| 1346 | /* We must arrange things just right for STORE_SUBSCR. |
| 1347 | It wants the stack to look like (value) (dict) (key) */ |
| 1348 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1349 | com_push(c, 1); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1350 | com_node(c, CHILD(n, i+2)); /* value */ |
| 1351 | com_addbyte(c, ROT_TWO); |
| 1352 | com_node(c, CHILD(n, i)); /* key */ |
| 1353 | com_addbyte(c, STORE_SUBSCR); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1354 | com_pop(c, 3); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1359 | com_atom(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1360 | { |
| 1361 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1362 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1363 | int i; |
| 1364 | REQ(n, atom); |
| 1365 | ch = CHILD(n, 0); |
| 1366 | switch (TYPE(ch)) { |
| 1367 | case LPAR: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1368 | if (TYPE(CHILD(n, 1)) == RPAR) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1369 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1370 | com_push(c, 1); |
| 1371 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1372 | else |
| 1373 | com_node(c, CHILD(n, 1)); |
| 1374 | break; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1375 | case LSQB: /* '[' [listmaker] ']' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1376 | if (TYPE(CHILD(n, 1)) == RSQB) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1377 | com_addoparg(c, BUILD_LIST, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1378 | com_push(c, 1); |
| 1379 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1380 | else |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1381 | com_listmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1382 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1383 | case LBRACE: /* '{' [dictmaker] '}' */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1384 | com_addoparg(c, BUILD_MAP, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1385 | com_push(c, 1); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1386 | if (TYPE(CHILD(n, 1)) == dictmaker) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1387 | com_dictmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1388 | break; |
| 1389 | case BACKQUOTE: |
| 1390 | com_node(c, CHILD(n, 1)); |
| 1391 | com_addbyte(c, UNARY_CONVERT); |
| 1392 | break; |
| 1393 | case NUMBER: |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1394 | if ((v = parsenumber(c, STR(ch))) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1395 | i = 255; |
| 1396 | } |
| 1397 | else { |
| 1398 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1399 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1400 | } |
| 1401 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1402 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1403 | break; |
| 1404 | case STRING: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1405 | v = parsestrplus(n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1406 | if (v == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1407 | c->c_errors++; |
| 1408 | i = 255; |
| 1409 | } |
| 1410 | else { |
| 1411 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1412 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1413 | } |
| 1414 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1415 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1416 | break; |
| 1417 | case NAME: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1418 | com_addop_varname(c, VAR_LOAD, STR(ch)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1419 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1420 | break; |
| 1421 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1422 | com_error(c, PyExc_SystemError, |
| 1423 | "com_atom: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1428 | com_slice(struct compiling *c, node *n, int op) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1429 | { |
| 1430 | if (NCH(n) == 1) { |
| 1431 | com_addbyte(c, op); |
| 1432 | } |
| 1433 | else if (NCH(n) == 2) { |
| 1434 | if (TYPE(CHILD(n, 0)) != COLON) { |
| 1435 | com_node(c, CHILD(n, 0)); |
| 1436 | com_addbyte(c, op+1); |
| 1437 | } |
| 1438 | else { |
| 1439 | com_node(c, CHILD(n, 1)); |
| 1440 | com_addbyte(c, op+2); |
| 1441 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1442 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1443 | } |
| 1444 | else { |
| 1445 | com_node(c, CHILD(n, 0)); |
| 1446 | com_node(c, CHILD(n, 2)); |
| 1447 | com_addbyte(c, op+3); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1448 | com_pop(c, 2); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1449 | } |
| 1450 | } |
| 1451 | |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1452 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1453 | com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn) |
| 1454 | { |
| 1455 | if (NCH(n) == 1) { |
| 1456 | com_addbyte(c, DUP_TOP); |
| 1457 | com_push(c, 1); |
| 1458 | com_addbyte(c, SLICE); |
| 1459 | com_node(c, augn); |
| 1460 | com_addbyte(c, opcode); |
| 1461 | com_pop(c, 1); |
| 1462 | com_addbyte(c, ROT_TWO); |
| 1463 | com_addbyte(c, STORE_SLICE); |
| 1464 | com_pop(c, 2); |
| 1465 | } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) { |
| 1466 | com_node(c, CHILD(n, 0)); |
| 1467 | com_addoparg(c, DUP_TOPX, 2); |
| 1468 | com_push(c, 2); |
| 1469 | com_addbyte(c, SLICE+1); |
| 1470 | com_pop(c, 1); |
| 1471 | com_node(c, augn); |
| 1472 | com_addbyte(c, opcode); |
| 1473 | com_pop(c, 1); |
| 1474 | com_addbyte(c, ROT_THREE); |
| 1475 | com_addbyte(c, STORE_SLICE+1); |
| 1476 | com_pop(c, 3); |
| 1477 | } else if (NCH(n) == 2) { |
| 1478 | com_node(c, CHILD(n, 1)); |
| 1479 | com_addoparg(c, DUP_TOPX, 2); |
| 1480 | com_push(c, 2); |
| 1481 | com_addbyte(c, SLICE+2); |
| 1482 | com_pop(c, 1); |
| 1483 | com_node(c, augn); |
| 1484 | com_addbyte(c, opcode); |
| 1485 | com_pop(c, 1); |
| 1486 | com_addbyte(c, ROT_THREE); |
| 1487 | com_addbyte(c, STORE_SLICE+2); |
| 1488 | com_pop(c, 3); |
| 1489 | } else { |
| 1490 | com_node(c, CHILD(n, 0)); |
| 1491 | com_node(c, CHILD(n, 2)); |
| 1492 | com_addoparg(c, DUP_TOPX, 3); |
| 1493 | com_push(c, 3); |
| 1494 | com_addbyte(c, SLICE+3); |
| 1495 | com_pop(c, 2); |
| 1496 | com_node(c, augn); |
| 1497 | com_addbyte(c, opcode); |
| 1498 | com_pop(c, 1); |
| 1499 | com_addbyte(c, ROT_FOUR); |
| 1500 | com_addbyte(c, STORE_SLICE+3); |
| 1501 | com_pop(c, 4); |
| 1502 | } |
| 1503 | } |
| 1504 | |
| 1505 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1506 | com_argument(struct compiling *c, node *n, PyObject **pkeywords) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1507 | { |
| 1508 | node *m; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1509 | REQ(n, argument); /* [test '='] test; really [keyword '='] test */ |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1510 | if (NCH(n) == 1) { |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1511 | if (*pkeywords != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1512 | com_error(c, PyExc_SyntaxError, |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 1513 | "non-keyword arg after keyword arg"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1514 | } |
| 1515 | else { |
| 1516 | com_node(c, CHILD(n, 0)); |
| 1517 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1518 | return; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1519 | } |
| 1520 | m = n; |
| 1521 | do { |
| 1522 | m = CHILD(m, 0); |
| 1523 | } while (NCH(m) == 1); |
| 1524 | if (TYPE(m) != NAME) { |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 1525 | /* f(lambda x: x[0] = 3) ends up getting parsed with |
| 1526 | * LHS test = lambda x: x[0], and RHS test = 3. |
| 1527 | * SF bug 132313 points out that complaining about a keyword |
| 1528 | * then is very confusing. |
| 1529 | */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1530 | com_error(c, PyExc_SyntaxError, |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 1531 | TYPE(m) == lambdef ? |
| 1532 | "lambda cannot contain assignment" : |
| 1533 | "keyword can't be an expression"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1534 | } |
| 1535 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1536 | PyObject *v = PyString_InternFromString(STR(m)); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1537 | if (v != NULL && *pkeywords == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1538 | *pkeywords = PyDict_New(); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1539 | if (v == NULL || *pkeywords == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1540 | c->c_errors++; |
| 1541 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1542 | if (PyDict_GetItem(*pkeywords, v) != NULL) |
| 1543 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1544 | "duplicate keyword argument"); |
| 1545 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1546 | if (PyDict_SetItem(*pkeywords, v, v) != 0) |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1547 | c->c_errors++; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1548 | com_addoparg(c, LOAD_CONST, com_addconst(c, v)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1549 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1550 | Py_DECREF(v); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1551 | } |
| 1552 | } |
| 1553 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1556 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1557 | com_call_function(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1558 | { |
| 1559 | if (TYPE(n) == RPAR) { |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1560 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1561 | } |
| 1562 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1563 | PyObject *keywords = NULL; |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1564 | int i, na, nk; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1565 | int lineno = n->n_lineno; |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1566 | int star_flag = 0; |
| 1567 | int starstar_flag = 0; |
| 1568 | int opcode; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1569 | REQ(n, arglist); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1570 | na = 0; |
| 1571 | nk = 0; |
| 1572 | for (i = 0; i < NCH(n); i += 2) { |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1573 | node *ch = CHILD(n, i); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1574 | if (TYPE(ch) == STAR || |
| 1575 | TYPE(ch) == DOUBLESTAR) |
| 1576 | break; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1577 | if (ch->n_lineno != lineno) { |
| 1578 | lineno = ch->n_lineno; |
| 1579 | com_addoparg(c, SET_LINENO, lineno); |
| 1580 | } |
| 1581 | com_argument(c, ch, &keywords); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1582 | if (keywords == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1583 | na++; |
| 1584 | else |
| 1585 | nk++; |
| 1586 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1587 | Py_XDECREF(keywords); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1588 | while (i < NCH(n)) { |
| 1589 | node *tok = CHILD(n, i); |
| 1590 | node *ch = CHILD(n, i+1); |
| 1591 | i += 3; |
| 1592 | switch (TYPE(tok)) { |
| 1593 | case STAR: star_flag = 1; break; |
| 1594 | case DOUBLESTAR: starstar_flag = 1; break; |
| 1595 | } |
| 1596 | com_node(c, ch); |
| 1597 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1598 | if (na > 255 || nk > 255) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1599 | com_error(c, PyExc_SyntaxError, |
| 1600 | "more than 255 arguments"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1601 | } |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1602 | if (star_flag || starstar_flag) |
Jeremy Hylton | e4fb958 | 2000-03-29 00:10:44 +0000 | [diff] [blame] | 1603 | opcode = CALL_FUNCTION_VAR - 1 + |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1604 | star_flag + (starstar_flag << 1); |
| 1605 | else |
| 1606 | opcode = CALL_FUNCTION; |
| 1607 | com_addoparg(c, opcode, na | (nk << 8)); |
| 1608 | com_pop(c, na + 2*nk + star_flag + starstar_flag); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1613 | com_select_member(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1614 | { |
| 1615 | com_addopname(c, LOAD_ATTR, n); |
| 1616 | } |
| 1617 | |
| 1618 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1619 | com_sliceobj(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1620 | { |
| 1621 | int i=0; |
| 1622 | int ns=2; /* number of slice arguments */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1623 | node *ch; |
| 1624 | |
| 1625 | /* first argument */ |
| 1626 | if (TYPE(CHILD(n,i)) == COLON) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1627 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1628 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1629 | i++; |
| 1630 | } |
| 1631 | else { |
| 1632 | com_node(c, CHILD(n,i)); |
| 1633 | i++; |
| 1634 | REQ(CHILD(n,i),COLON); |
| 1635 | i++; |
| 1636 | } |
| 1637 | /* second argument */ |
| 1638 | if (i < NCH(n) && TYPE(CHILD(n,i)) == test) { |
| 1639 | com_node(c, CHILD(n,i)); |
| 1640 | i++; |
| 1641 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1642 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1643 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1644 | com_push(c, 1); |
| 1645 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1646 | /* remaining arguments */ |
| 1647 | for (; i < NCH(n); i++) { |
| 1648 | ns++; |
| 1649 | ch=CHILD(n,i); |
| 1650 | REQ(ch, sliceop); |
| 1651 | if (NCH(ch) == 1) { |
| 1652 | /* right argument of ':' missing */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1653 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1654 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1655 | } |
| 1656 | else |
| 1657 | com_node(c, CHILD(ch,1)); |
| 1658 | } |
| 1659 | com_addoparg(c, BUILD_SLICE, ns); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1660 | com_pop(c, 1 + (ns == 3)); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1664 | com_subscript(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1665 | { |
| 1666 | node *ch; |
| 1667 | REQ(n, subscript); |
| 1668 | ch = CHILD(n,0); |
| 1669 | /* check for rubber index */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1670 | if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) { |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 1671 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1672 | com_push(c, 1); |
| 1673 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1674 | else { |
| 1675 | /* check for slice */ |
| 1676 | if ((TYPE(ch) == COLON || NCH(n) > 1)) |
| 1677 | com_sliceobj(c, n); |
| 1678 | else { |
| 1679 | REQ(ch, test); |
| 1680 | com_node(c, ch); |
| 1681 | } |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1686 | com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1687 | { |
| 1688 | int i, op; |
| 1689 | REQ(n, subscriptlist); |
| 1690 | /* Check to make backward compatible slice behavior for '[i:j]' */ |
| 1691 | if (NCH(n) == 1) { |
| 1692 | node *sub = CHILD(n, 0); /* subscript */ |
Thomas Wouters | e8643c4 | 2000-08-05 21:37:50 +0000 | [diff] [blame] | 1693 | /* 'Basic' slice, should have exactly one colon. */ |
| 1694 | if ((TYPE(CHILD(sub, 0)) == COLON |
| 1695 | || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON)) |
| 1696 | && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop)) |
| 1697 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1698 | switch (assigning) { |
| 1699 | case OP_DELETE: |
| 1700 | op = DELETE_SLICE; |
| 1701 | break; |
| 1702 | case OP_ASSIGN: |
| 1703 | op = STORE_SLICE; |
| 1704 | break; |
| 1705 | case OP_APPLY: |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1706 | op = SLICE; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1707 | break; |
| 1708 | default: |
| 1709 | com_augassign_slice(c, sub, assigning, augn); |
| 1710 | return; |
| 1711 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1712 | com_slice(c, sub, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1713 | if (op == STORE_SLICE) |
| 1714 | com_pop(c, 2); |
| 1715 | else if (op == DELETE_SLICE) |
| 1716 | com_pop(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1717 | return; |
| 1718 | } |
| 1719 | } |
| 1720 | /* Else normal subscriptlist. Compile each subscript. */ |
| 1721 | for (i = 0; i < NCH(n); i += 2) |
| 1722 | com_subscript(c, CHILD(n, i)); |
| 1723 | /* Put multiple subscripts into a tuple */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1724 | if (NCH(n) > 1) { |
| 1725 | i = (NCH(n)+1) / 2; |
| 1726 | com_addoparg(c, BUILD_TUPLE, i); |
| 1727 | com_pop(c, i-1); |
| 1728 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1729 | switch (assigning) { |
| 1730 | case OP_DELETE: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1731 | op = DELETE_SUBSCR; |
| 1732 | i = 2; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1733 | break; |
| 1734 | default: |
| 1735 | case OP_ASSIGN: |
| 1736 | op = STORE_SUBSCR; |
| 1737 | i = 3; |
| 1738 | break; |
| 1739 | case OP_APPLY: |
| 1740 | op = BINARY_SUBSCR; |
| 1741 | i = 1; |
| 1742 | break; |
| 1743 | } |
| 1744 | if (assigning > OP_APPLY) { |
| 1745 | com_addoparg(c, DUP_TOPX, 2); |
| 1746 | com_push(c, 2); |
| 1747 | com_addbyte(c, BINARY_SUBSCR); |
| 1748 | com_pop(c, 1); |
| 1749 | com_node(c, augn); |
| 1750 | com_addbyte(c, assigning); |
| 1751 | com_pop(c, 1); |
| 1752 | com_addbyte(c, ROT_THREE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1753 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1754 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1755 | com_pop(c, i); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1759 | com_apply_trailer(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1760 | { |
| 1761 | REQ(n, trailer); |
| 1762 | switch (TYPE(CHILD(n, 0))) { |
| 1763 | case LPAR: |
| 1764 | com_call_function(c, CHILD(n, 1)); |
| 1765 | break; |
| 1766 | case DOT: |
| 1767 | com_select_member(c, CHILD(n, 1)); |
| 1768 | break; |
| 1769 | case LSQB: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1770 | com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1771 | break; |
| 1772 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1773 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1774 | "com_apply_trailer: unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1775 | } |
| 1776 | } |
| 1777 | |
| 1778 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1779 | com_power(struct compiling *c, node *n) |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1780 | { |
| 1781 | int i; |
| 1782 | REQ(n, power); |
| 1783 | com_atom(c, CHILD(n, 0)); |
| 1784 | for (i = 1; i < NCH(n); i++) { |
| 1785 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
| 1786 | com_factor(c, CHILD(n, i+1)); |
| 1787 | com_addbyte(c, BINARY_POWER); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1788 | com_pop(c, 1); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1789 | break; |
| 1790 | } |
| 1791 | else |
| 1792 | com_apply_trailer(c, CHILD(n, i)); |
| 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1797 | com_factor(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1798 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1799 | REQ(n, factor); |
| 1800 | if (TYPE(CHILD(n, 0)) == PLUS) { |
| 1801 | com_factor(c, CHILD(n, 1)); |
| 1802 | com_addbyte(c, UNARY_POSITIVE); |
| 1803 | } |
| 1804 | else if (TYPE(CHILD(n, 0)) == MINUS) { |
| 1805 | com_factor(c, CHILD(n, 1)); |
| 1806 | com_addbyte(c, UNARY_NEGATIVE); |
| 1807 | } |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1808 | else if (TYPE(CHILD(n, 0)) == TILDE) { |
| 1809 | com_factor(c, CHILD(n, 1)); |
| 1810 | com_addbyte(c, UNARY_INVERT); |
| 1811 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1812 | else { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1813 | com_power(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1814 | } |
| 1815 | } |
| 1816 | |
| 1817 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1818 | com_term(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1819 | { |
| 1820 | int i; |
| 1821 | int op; |
| 1822 | REQ(n, term); |
| 1823 | com_factor(c, CHILD(n, 0)); |
| 1824 | for (i = 2; i < NCH(n); i += 2) { |
| 1825 | com_factor(c, CHILD(n, i)); |
| 1826 | switch (TYPE(CHILD(n, i-1))) { |
| 1827 | case STAR: |
| 1828 | op = BINARY_MULTIPLY; |
| 1829 | break; |
| 1830 | case SLASH: |
| 1831 | op = BINARY_DIVIDE; |
| 1832 | break; |
| 1833 | case PERCENT: |
| 1834 | op = BINARY_MODULO; |
| 1835 | break; |
| 1836 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1837 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1838 | "com_term: operator not *, / or %"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1839 | op = 255; |
| 1840 | } |
| 1841 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1842 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1843 | } |
| 1844 | } |
| 1845 | |
| 1846 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1847 | com_arith_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1848 | { |
| 1849 | int i; |
| 1850 | int op; |
| 1851 | REQ(n, arith_expr); |
| 1852 | com_term(c, CHILD(n, 0)); |
| 1853 | for (i = 2; i < NCH(n); i += 2) { |
| 1854 | com_term(c, CHILD(n, i)); |
| 1855 | switch (TYPE(CHILD(n, i-1))) { |
| 1856 | case PLUS: |
| 1857 | op = BINARY_ADD; |
| 1858 | break; |
| 1859 | case MINUS: |
| 1860 | op = BINARY_SUBTRACT; |
| 1861 | break; |
| 1862 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1863 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1864 | "com_arith_expr: operator not + or -"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1865 | op = 255; |
| 1866 | } |
| 1867 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1868 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1873 | com_shift_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1874 | { |
| 1875 | int i; |
| 1876 | int op; |
| 1877 | REQ(n, shift_expr); |
| 1878 | com_arith_expr(c, CHILD(n, 0)); |
| 1879 | for (i = 2; i < NCH(n); i += 2) { |
| 1880 | com_arith_expr(c, CHILD(n, i)); |
| 1881 | switch (TYPE(CHILD(n, i-1))) { |
| 1882 | case LEFTSHIFT: |
| 1883 | op = BINARY_LSHIFT; |
| 1884 | break; |
| 1885 | case RIGHTSHIFT: |
| 1886 | op = BINARY_RSHIFT; |
| 1887 | break; |
| 1888 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1889 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1890 | "com_shift_expr: operator not << or >>"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1891 | op = 255; |
| 1892 | } |
| 1893 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1894 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1899 | com_and_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1900 | { |
| 1901 | int i; |
| 1902 | int op; |
| 1903 | REQ(n, and_expr); |
| 1904 | com_shift_expr(c, CHILD(n, 0)); |
| 1905 | for (i = 2; i < NCH(n); i += 2) { |
| 1906 | com_shift_expr(c, CHILD(n, i)); |
| 1907 | if (TYPE(CHILD(n, i-1)) == AMPER) { |
| 1908 | op = BINARY_AND; |
| 1909 | } |
| 1910 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1911 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1912 | "com_and_expr: operator not &"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1913 | op = 255; |
| 1914 | } |
| 1915 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1916 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1917 | } |
| 1918 | } |
| 1919 | |
| 1920 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1921 | com_xor_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1922 | { |
| 1923 | int i; |
| 1924 | int op; |
| 1925 | REQ(n, xor_expr); |
| 1926 | com_and_expr(c, CHILD(n, 0)); |
| 1927 | for (i = 2; i < NCH(n); i += 2) { |
| 1928 | com_and_expr(c, CHILD(n, i)); |
| 1929 | if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) { |
| 1930 | op = BINARY_XOR; |
| 1931 | } |
| 1932 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1933 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1934 | "com_xor_expr: operator not ^"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1935 | op = 255; |
| 1936 | } |
| 1937 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1938 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1939 | } |
| 1940 | } |
| 1941 | |
| 1942 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1943 | com_expr(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1944 | { |
| 1945 | int i; |
| 1946 | int op; |
| 1947 | REQ(n, expr); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1948 | com_xor_expr(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1949 | for (i = 2; i < NCH(n); i += 2) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1950 | com_xor_expr(c, CHILD(n, i)); |
| 1951 | if (TYPE(CHILD(n, i-1)) == VBAR) { |
| 1952 | op = BINARY_OR; |
| 1953 | } |
| 1954 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1955 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1956 | "com_expr: expr operator not |"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1957 | op = 255; |
| 1958 | } |
| 1959 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1960 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | static enum cmp_op |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1965 | cmp_type(node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1966 | { |
| 1967 | REQ(n, comp_op); |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 1968 | /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '==' |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1969 | | 'in' | 'not' 'in' | 'is' | 'is' not' */ |
| 1970 | if (NCH(n) == 1) { |
| 1971 | n = CHILD(n, 0); |
| 1972 | switch (TYPE(n)) { |
| 1973 | case LESS: return LT; |
| 1974 | case GREATER: return GT; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 1975 | case EQEQUAL: /* == */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1976 | case EQUAL: return EQ; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 1977 | case LESSEQUAL: return LE; |
| 1978 | case GREATEREQUAL: return GE; |
| 1979 | case NOTEQUAL: return NE; /* <> or != */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1980 | case NAME: if (strcmp(STR(n), "in") == 0) return IN; |
| 1981 | if (strcmp(STR(n), "is") == 0) return IS; |
| 1982 | } |
| 1983 | } |
| 1984 | else if (NCH(n) == 2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1985 | switch (TYPE(CHILD(n, 0))) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1986 | case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 1987 | return NOT_IN; |
| 1988 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 1989 | return IS_NOT; |
| 1990 | } |
| 1991 | } |
| 1992 | return BAD; |
| 1993 | } |
| 1994 | |
| 1995 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1996 | com_comparison(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1997 | { |
| 1998 | int i; |
| 1999 | enum cmp_op op; |
| 2000 | int anchor; |
| 2001 | REQ(n, comparison); /* comparison: expr (comp_op expr)* */ |
| 2002 | com_expr(c, CHILD(n, 0)); |
| 2003 | if (NCH(n) == 1) |
| 2004 | return; |
| 2005 | |
| 2006 | /**************************************************************** |
| 2007 | The following code is generated for all but the last |
| 2008 | comparison in a chain: |
| 2009 | |
| 2010 | label: on stack: opcode: jump to: |
| 2011 | |
| 2012 | a <code to load b> |
| 2013 | a, b DUP_TOP |
| 2014 | a, b, b ROT_THREE |
| 2015 | b, a, b COMPARE_OP |
| 2016 | b, 0-or-1 JUMP_IF_FALSE L1 |
| 2017 | b, 1 POP_TOP |
| 2018 | b |
| 2019 | |
| 2020 | We are now ready to repeat this sequence for the next |
| 2021 | comparison in the chain. |
| 2022 | |
| 2023 | For the last we generate: |
| 2024 | |
| 2025 | b <code to load c> |
| 2026 | b, c COMPARE_OP |
| 2027 | 0-or-1 |
| 2028 | |
| 2029 | If there were any jumps to L1 (i.e., there was more than one |
| 2030 | comparison), we generate: |
| 2031 | |
| 2032 | 0-or-1 JUMP_FORWARD L2 |
| 2033 | L1: b, 0 ROT_TWO |
| 2034 | 0, b POP_TOP |
| 2035 | 0 |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2036 | L2: 0-or-1 |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2037 | ****************************************************************/ |
| 2038 | |
| 2039 | anchor = 0; |
| 2040 | |
| 2041 | for (i = 2; i < NCH(n); i += 2) { |
| 2042 | com_expr(c, CHILD(n, i)); |
| 2043 | if (i+2 < NCH(n)) { |
| 2044 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2045 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2046 | com_addbyte(c, ROT_THREE); |
| 2047 | } |
| 2048 | op = cmp_type(CHILD(n, i-1)); |
| 2049 | if (op == BAD) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2050 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2051 | "com_comparison: unknown comparison op"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2052 | } |
| 2053 | com_addoparg(c, COMPARE_OP, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2054 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2055 | if (i+2 < NCH(n)) { |
| 2056 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2057 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2058 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2059 | } |
| 2060 | } |
| 2061 | |
| 2062 | if (anchor) { |
| 2063 | int anchor2 = 0; |
| 2064 | com_addfwref(c, JUMP_FORWARD, &anchor2); |
| 2065 | com_backpatch(c, anchor); |
| 2066 | com_addbyte(c, ROT_TWO); |
| 2067 | com_addbyte(c, POP_TOP); |
| 2068 | com_backpatch(c, anchor2); |
| 2069 | } |
| 2070 | } |
| 2071 | |
| 2072 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2073 | com_not_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2074 | { |
| 2075 | REQ(n, not_test); /* 'not' not_test | comparison */ |
| 2076 | if (NCH(n) == 1) { |
| 2077 | com_comparison(c, CHILD(n, 0)); |
| 2078 | } |
| 2079 | else { |
| 2080 | com_not_test(c, CHILD(n, 1)); |
| 2081 | com_addbyte(c, UNARY_NOT); |
| 2082 | } |
| 2083 | } |
| 2084 | |
| 2085 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2086 | com_and_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2087 | { |
| 2088 | int i; |
| 2089 | int anchor; |
| 2090 | REQ(n, and_test); /* not_test ('and' not_test)* */ |
| 2091 | anchor = 0; |
| 2092 | i = 0; |
| 2093 | for (;;) { |
| 2094 | com_not_test(c, CHILD(n, i)); |
| 2095 | if ((i += 2) >= NCH(n)) |
| 2096 | break; |
| 2097 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2098 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2099 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2100 | } |
| 2101 | if (anchor) |
| 2102 | com_backpatch(c, anchor); |
| 2103 | } |
| 2104 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2105 | static int |
| 2106 | com_make_closure(struct compiling *c, PyCodeObject *co) |
| 2107 | { |
| 2108 | int i, free = PyTuple_GET_SIZE(co->co_freevars); |
| 2109 | if (free == 0) |
| 2110 | return 0; |
| 2111 | for (i = 0; i < free; ++i) { |
| 2112 | /* Bypass com_addop_varname because it will generate |
| 2113 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 2114 | */ |
| 2115 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 2116 | int arg, reftype; |
| 2117 | |
| 2118 | /* Special case: If a class contains a method with a |
| 2119 | free variable that has the same name as a method, |
| 2120 | the name will be considered free *and* local in the |
| 2121 | class. It should be handled by the closure, as |
| 2122 | well as by the normal name loookup logic. |
| 2123 | */ |
| 2124 | reftype = get_ref_type(c, PyString_AS_STRING(name)); |
| 2125 | if (reftype == CELL) |
| 2126 | arg = com_lookup_arg(c->c_cellvars, name); |
| 2127 | else /* (reftype == FREE) */ |
| 2128 | arg = com_lookup_arg(c->c_freevars, name); |
| 2129 | if (arg == -1) { |
| 2130 | fprintf(stderr, "lookup %s in %s %d %d\n", |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 2131 | PyObject_REPR(name), c->c_name, reftype, arg); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2132 | Py_FatalError("com_make_closure()"); |
| 2133 | } |
| 2134 | com_addoparg(c, LOAD_CLOSURE, arg); |
| 2135 | |
| 2136 | } |
| 2137 | com_push(c, free); |
| 2138 | return 1; |
| 2139 | } |
| 2140 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2141 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2142 | com_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2143 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2144 | REQ(n, test); /* and_test ('or' and_test)* | lambdef */ |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2145 | if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2146 | PyObject *co; |
| 2147 | int i, closure; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2148 | int ndefs = com_argdefs(c, CHILD(n, 0)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 2149 | symtable_enter_scope(c->c_symtable, "lambda", lambdef, |
| 2150 | n->n_lineno); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2151 | co = (PyObject *) icompile(CHILD(n, 0), c); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2152 | if (co == NULL) { |
| 2153 | c->c_errors++; |
| 2154 | return; |
| 2155 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2156 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2157 | if (co == NULL) { |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2158 | c->c_errors++; |
| 2159 | i = 255; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2160 | closure = 0; |
| 2161 | } else { |
| 2162 | i = com_addconst(c, co); |
| 2163 | Py_DECREF(co); |
| 2164 | closure = com_make_closure(c, (PyCodeObject *)co); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2165 | } |
| 2166 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2167 | com_push(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2168 | if (closure) |
| 2169 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
| 2170 | else |
| 2171 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2172 | com_pop(c, ndefs); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2173 | } |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2174 | else { |
| 2175 | int anchor = 0; |
| 2176 | int i = 0; |
| 2177 | for (;;) { |
| 2178 | com_and_test(c, CHILD(n, i)); |
| 2179 | if ((i += 2) >= NCH(n)) |
| 2180 | break; |
| 2181 | com_addfwref(c, JUMP_IF_TRUE, &anchor); |
| 2182 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2183 | com_pop(c, 1); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2184 | } |
| 2185 | if (anchor) |
| 2186 | com_backpatch(c, anchor); |
| 2187 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2191 | com_list(struct compiling *c, node *n, int toplevel) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2192 | { |
| 2193 | /* exprlist: expr (',' expr)* [',']; likewise for testlist */ |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 2194 | if (NCH(n) == 1 && !toplevel) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2195 | com_node(c, CHILD(n, 0)); |
| 2196 | } |
| 2197 | else { |
| 2198 | int i; |
| 2199 | int len; |
| 2200 | len = (NCH(n) + 1) / 2; |
| 2201 | for (i = 0; i < NCH(n); i += 2) |
| 2202 | com_node(c, CHILD(n, i)); |
| 2203 | com_addoparg(c, BUILD_TUPLE, len); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2204 | com_pop(c, len-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2205 | } |
| 2206 | } |
| 2207 | |
| 2208 | |
| 2209 | /* Begin of assignment compilation */ |
| 2210 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2211 | |
| 2212 | static void |
| 2213 | com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn) |
| 2214 | { |
| 2215 | com_addbyte(c, DUP_TOP); |
| 2216 | com_push(c, 1); |
| 2217 | com_addopname(c, LOAD_ATTR, n); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2218 | com_node(c, augn); |
| 2219 | com_addbyte(c, opcode); |
| 2220 | com_pop(c, 1); |
| 2221 | com_addbyte(c, ROT_TWO); |
| 2222 | com_addopname(c, STORE_ATTR, n); |
| 2223 | com_pop(c, 2); |
| 2224 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2225 | |
| 2226 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2227 | com_assign_attr(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2228 | { |
| 2229 | com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2230 | com_pop(c, assigning ? 2 : 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
| 2233 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2234 | 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] | 2235 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2236 | REQ(n, trailer); |
| 2237 | switch (TYPE(CHILD(n, 0))) { |
| 2238 | case LPAR: /* '(' [exprlist] ')' */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2239 | com_error(c, PyExc_SyntaxError, |
| 2240 | "can't assign to function call"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2241 | break; |
| 2242 | case DOT: /* '.' NAME */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2243 | if (assigning > OP_APPLY) |
| 2244 | com_augassign_attr(c, CHILD(n, 1), assigning, augn); |
| 2245 | else |
| 2246 | com_assign_attr(c, CHILD(n, 1), assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2247 | break; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2248 | case LSQB: /* '[' subscriptlist ']' */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2249 | com_subscriptlist(c, CHILD(n, 1), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2250 | break; |
| 2251 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2252 | com_error(c, PyExc_SystemError, "unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2253 | } |
| 2254 | } |
| 2255 | |
| 2256 | static void |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2257 | com_assign_sequence(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2258 | { |
| 2259 | int i; |
Thomas Wouters | b59290f | 2000-08-25 05:41:11 +0000 | [diff] [blame] | 2260 | if (TYPE(n) != testlist && TYPE(n) != listmaker) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2261 | REQ(n, exprlist); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2262 | if (assigning) { |
| 2263 | i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2264 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2265 | com_push(c, i-1); |
| 2266 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2267 | for (i = 0; i < NCH(n); i += 2) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2268 | com_assign(c, CHILD(n, i), assigning, NULL); |
| 2269 | } |
| 2270 | |
| 2271 | static void |
| 2272 | com_augassign_name(struct compiling *c, node *n, int opcode, node *augn) |
| 2273 | { |
| 2274 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2275 | com_addop_varname(c, VAR_LOAD, STR(n)); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2276 | com_push(c, 1); |
| 2277 | com_node(c, augn); |
| 2278 | com_addbyte(c, opcode); |
| 2279 | com_pop(c, 1); |
| 2280 | com_assign_name(c, n, OP_ASSIGN); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2284 | com_assign_name(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2285 | { |
| 2286 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2287 | com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2288 | if (assigning) |
| 2289 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
| 2292 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2293 | com_assign(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2294 | { |
| 2295 | /* Loop to avoid trivial recursion */ |
| 2296 | for (;;) { |
| 2297 | switch (TYPE(n)) { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2298 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2299 | case exprlist: |
| 2300 | case testlist: |
| 2301 | if (NCH(n) > 1) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2302 | if (assigning > OP_APPLY) { |
| 2303 | com_error(c, PyExc_SyntaxError, |
| 2304 | "augmented assign to tuple not possible"); |
| 2305 | return; |
| 2306 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2307 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2308 | return; |
| 2309 | } |
| 2310 | n = CHILD(n, 0); |
| 2311 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2312 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2313 | case test: |
| 2314 | case and_test: |
| 2315 | case not_test: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2316 | case comparison: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2317 | case expr: |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2318 | case xor_expr: |
| 2319 | case and_expr: |
| 2320 | case shift_expr: |
| 2321 | case arith_expr: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2322 | case term: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2323 | case factor: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2324 | if (NCH(n) > 1) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2325 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2326 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2327 | return; |
| 2328 | } |
| 2329 | n = CHILD(n, 0); |
| 2330 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2331 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2332 | case power: /* atom trailer* ('**' power)* |
| 2333 | ('+'|'-'|'~') factor | atom trailer* */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2334 | if (TYPE(CHILD(n, 0)) != atom) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2335 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2336 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2337 | return; |
| 2338 | } |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2339 | if (NCH(n) > 1) { /* trailer or exponent present */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2340 | int i; |
| 2341 | com_node(c, CHILD(n, 0)); |
| 2342 | for (i = 1; i+1 < NCH(n); i++) { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2343 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2344 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2345 | "can't assign to operator"); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2346 | return; |
| 2347 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2348 | com_apply_trailer(c, CHILD(n, i)); |
| 2349 | } /* NB i is still alive */ |
| 2350 | com_assign_trailer(c, |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2351 | CHILD(n, i), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2352 | return; |
| 2353 | } |
| 2354 | n = CHILD(n, 0); |
| 2355 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2356 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2357 | case atom: |
| 2358 | switch (TYPE(CHILD(n, 0))) { |
| 2359 | case LPAR: |
| 2360 | n = CHILD(n, 1); |
| 2361 | if (TYPE(n) == RPAR) { |
| 2362 | /* XXX Should allow () = () ??? */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2363 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2364 | "can't assign to ()"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2365 | return; |
| 2366 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2367 | if (assigning > OP_APPLY) { |
| 2368 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2369 | "augmented assign to tuple not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2370 | return; |
| 2371 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2372 | break; |
| 2373 | case LSQB: |
| 2374 | n = CHILD(n, 1); |
| 2375 | if (TYPE(n) == RSQB) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2376 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2377 | "can't assign to []"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2378 | return; |
| 2379 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2380 | if (assigning > OP_APPLY) { |
| 2381 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2382 | "augmented assign to list not possible"); |
| 2383 | return; |
| 2384 | } |
| 2385 | if (NCH(n) > 1 |
| 2386 | && TYPE(CHILD(n, 1)) == list_for) { |
| 2387 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2388 | "can't assign to list comprehension"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2389 | return; |
| 2390 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2391 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2392 | return; |
| 2393 | case NAME: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2394 | if (assigning > OP_APPLY) |
| 2395 | com_augassign_name(c, CHILD(n, 0), |
| 2396 | assigning, augn); |
| 2397 | else |
| 2398 | com_assign_name(c, CHILD(n, 0), |
| 2399 | assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2400 | return; |
| 2401 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2402 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2403 | "can't assign to literal"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2404 | return; |
| 2405 | } |
| 2406 | break; |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2407 | |
| 2408 | case lambdef: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2409 | com_error(c, PyExc_SyntaxError, |
| 2410 | "can't assign to lambda"); |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2411 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2412 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2413 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2414 | com_error(c, PyExc_SystemError, |
| 2415 | "com_assign: bad node"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2416 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2417 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2418 | } |
| 2419 | } |
| 2420 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2421 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2422 | static void |
| 2423 | com_augassign(struct compiling *c, node *n) |
| 2424 | { |
| 2425 | int opcode; |
| 2426 | |
| 2427 | switch (STR(CHILD(CHILD(n, 1), 0))[0]) { |
| 2428 | case '+': opcode = INPLACE_ADD; break; |
| 2429 | case '-': opcode = INPLACE_SUBTRACT; break; |
| 2430 | case '/': opcode = INPLACE_DIVIDE; break; |
| 2431 | case '%': opcode = INPLACE_MODULO; break; |
| 2432 | case '<': opcode = INPLACE_LSHIFT; break; |
| 2433 | case '>': opcode = INPLACE_RSHIFT; break; |
| 2434 | case '&': opcode = INPLACE_AND; break; |
| 2435 | case '^': opcode = INPLACE_XOR; break; |
| 2436 | case '|': opcode = INPLACE_OR; break; |
| 2437 | case '*': |
| 2438 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '*') |
| 2439 | opcode = INPLACE_POWER; |
| 2440 | else |
| 2441 | opcode = INPLACE_MULTIPLY; |
| 2442 | break; |
| 2443 | default: |
| 2444 | com_error(c, PyExc_SystemError, "com_augassign: bad operator"); |
| 2445 | return; |
| 2446 | } |
| 2447 | com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2)); |
| 2448 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2449 | |
| 2450 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2451 | com_expr_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2452 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2453 | REQ(n, expr_stmt); |
| 2454 | /* testlist (('=' testlist)* | augassign testlist) */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2455 | /* Forget it if we have just a doc string here */ |
Guido van Rossum | 5f5e817 | 1997-04-06 03:41:40 +0000 | [diff] [blame] | 2456 | if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2457 | return; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2458 | if (NCH(n) == 1) { |
| 2459 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2460 | if (c->c_interactive) |
| 2461 | com_addbyte(c, PRINT_EXPR); |
| 2462 | else |
| 2463 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2464 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2465 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2466 | else if (TYPE(CHILD(n,1)) == augassign) |
| 2467 | com_augassign(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2468 | else { |
| 2469 | int i; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2470 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2471 | for (i = 0; i < NCH(n)-2; i+=2) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2472 | if (i+2 < NCH(n)-2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2473 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2474 | com_push(c, 1); |
| 2475 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2476 | com_assign(c, CHILD(n, i), OP_ASSIGN, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2477 | } |
| 2478 | } |
| 2479 | } |
| 2480 | |
| 2481 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2482 | com_assert_stmt(struct compiling *c, node *n) |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2483 | { |
| 2484 | int a = 0, b = 0; |
| 2485 | int i; |
| 2486 | REQ(n, assert_stmt); /* 'assert' test [',' test] */ |
| 2487 | /* Generate code like for |
| 2488 | |
| 2489 | if __debug__: |
| 2490 | if not <test>: |
| 2491 | raise AssertionError [, <message>] |
| 2492 | |
| 2493 | where <message> is the second test, if present. |
| 2494 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2495 | |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2496 | if (Py_OptimizeFlag) |
| 2497 | return; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2498 | com_addop_name(c, LOAD_GLOBAL, "__debug__"); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2499 | com_push(c, 1); |
| 2500 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 2501 | com_addbyte(c, POP_TOP); |
| 2502 | com_pop(c, 1); |
| 2503 | com_node(c, CHILD(n, 1)); |
| 2504 | com_addfwref(c, JUMP_IF_TRUE, &b); |
| 2505 | com_addbyte(c, POP_TOP); |
| 2506 | com_pop(c, 1); |
| 2507 | /* Raise that exception! */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2508 | com_addop_name(c, LOAD_GLOBAL, "AssertionError"); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2509 | com_push(c, 1); |
| 2510 | i = NCH(n)/2; /* Either 2 or 4 */ |
| 2511 | if (i > 1) |
| 2512 | com_node(c, CHILD(n, 3)); |
| 2513 | com_addoparg(c, RAISE_VARARGS, i); |
| 2514 | com_pop(c, i); |
| 2515 | /* The interpreter does not fall through */ |
| 2516 | /* All jumps converge here */ |
| 2517 | com_backpatch(c, a); |
| 2518 | com_backpatch(c, b); |
| 2519 | com_addbyte(c, POP_TOP); |
| 2520 | } |
| 2521 | |
| 2522 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2523 | com_print_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2524 | { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2525 | int i = 1; |
| 2526 | node* stream = NULL; |
| 2527 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2528 | REQ(n, print_stmt); /* 'print' (test ',')* [test] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2529 | |
| 2530 | /* are we using the extended print form? */ |
| 2531 | if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) { |
| 2532 | stream = CHILD(n, 2); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2533 | com_node(c, stream); |
| 2534 | /* stack: [...] => [... stream] */ |
| 2535 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2536 | if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA) |
| 2537 | i = 4; |
| 2538 | else |
| 2539 | i = 3; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2540 | } |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2541 | for (; i < NCH(n); i += 2) { |
| 2542 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2543 | com_addbyte(c, DUP_TOP); |
| 2544 | /* stack: [stream] => [stream stream] */ |
| 2545 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2546 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2547 | /* stack: [stream stream] => [stream stream obj] */ |
| 2548 | com_addbyte(c, ROT_TWO); |
| 2549 | /* stack: [stream stream obj] => [stream obj stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2550 | com_addbyte(c, PRINT_ITEM_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2551 | /* stack: [stream obj stream] => [stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2552 | com_pop(c, 2); |
| 2553 | } |
| 2554 | else { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2555 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2556 | /* stack: [...] => [... obj] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2557 | com_addbyte(c, PRINT_ITEM); |
| 2558 | com_pop(c, 1); |
| 2559 | } |
| 2560 | } |
| 2561 | /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */ |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2562 | if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2563 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2564 | /* must pop the extra stream object off the stack */ |
| 2565 | com_addbyte(c, POP_TOP); |
| 2566 | /* stack: [... stream] => [...] */ |
| 2567 | com_pop(c, 1); |
| 2568 | } |
| 2569 | } |
| 2570 | else { |
| 2571 | if (stream != NULL) { |
| 2572 | /* this consumes the last stream object on stack */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2573 | com_addbyte(c, PRINT_NEWLINE_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2574 | /* stack: [... stream] => [...] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2575 | com_pop(c, 1); |
| 2576 | } |
| 2577 | else |
| 2578 | com_addbyte(c, PRINT_NEWLINE); |
| 2579 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2580 | } |
| 2581 | |
| 2582 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2583 | com_return_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2584 | { |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2585 | REQ(n, return_stmt); /* 'return' [testlist] */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2586 | if (!c->c_infunction) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2587 | com_error(c, PyExc_SyntaxError, "'return' outside function"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2588 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2589 | if (NCH(n) < 2) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2590 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2591 | com_push(c, 1); |
| 2592 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2593 | else |
| 2594 | com_node(c, CHILD(n, 1)); |
| 2595 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2596 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2597 | } |
| 2598 | |
| 2599 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2600 | com_raise_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2601 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2602 | int i; |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 2603 | REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */ |
| 2604 | if (NCH(n) > 1) { |
| 2605 | com_node(c, CHILD(n, 1)); |
| 2606 | if (NCH(n) > 3) { |
| 2607 | com_node(c, CHILD(n, 3)); |
| 2608 | if (NCH(n) > 5) |
| 2609 | com_node(c, CHILD(n, 5)); |
| 2610 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2611 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2612 | i = NCH(n)/2; |
| 2613 | com_addoparg(c, RAISE_VARARGS, i); |
| 2614 | com_pop(c, i); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2615 | } |
| 2616 | |
| 2617 | static void |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2618 | com_from_import(struct compiling *c, node *n) |
| 2619 | { |
| 2620 | com_addopname(c, IMPORT_FROM, CHILD(n, 0)); |
| 2621 | com_push(c, 1); |
| 2622 | if (NCH(n) > 1) { |
| 2623 | if (strcmp(STR(CHILD(n, 1)), "as") != 0) { |
| 2624 | com_error(c, PyExc_SyntaxError, "invalid syntax"); |
| 2625 | return; |
| 2626 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2627 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2628 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2629 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2630 | com_pop(c, 1); |
| 2631 | } |
| 2632 | |
| 2633 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2634 | com_import_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2635 | { |
| 2636 | int i; |
| 2637 | REQ(n, import_stmt); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2638 | /* 'import' dotted_name (',' dotted_name)* | |
| 2639 | 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2640 | if (STR(CHILD(n, 0))[0] == 'f') { |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 2641 | PyObject *tup; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2642 | /* 'from' dotted_name 'import' ... */ |
| 2643 | REQ(CHILD(n, 1), dotted_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2644 | |
| 2645 | if (TYPE(CHILD(n, 3)) == STAR) { |
| 2646 | tup = Py_BuildValue("(s)", "*"); |
| 2647 | } else { |
| 2648 | tup = PyTuple_New((NCH(n) - 2)/2); |
| 2649 | for (i = 3; i < NCH(n); i += 2) { |
| 2650 | PyTuple_SET_ITEM(tup, (i-3)/2, |
| 2651 | PyString_FromString(STR( |
| 2652 | CHILD(CHILD(n, i), 0)))); |
| 2653 | } |
| 2654 | } |
| 2655 | com_addoparg(c, LOAD_CONST, com_addconst(c, tup)); |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 2656 | Py_DECREF(tup); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2657 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2658 | com_addopname(c, IMPORT_NAME, CHILD(n, 1)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2659 | if (TYPE(CHILD(n, 3)) == STAR) |
| 2660 | com_addbyte(c, IMPORT_STAR); |
| 2661 | else { |
| 2662 | for (i = 3; i < NCH(n); i += 2) |
| 2663 | com_from_import(c, CHILD(n, i)); |
| 2664 | com_addbyte(c, POP_TOP); |
| 2665 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2666 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2667 | } |
| 2668 | else { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2669 | /* 'import' ... */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2670 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2671 | node *subn = CHILD(n, i); |
| 2672 | REQ(subn, dotted_as_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2673 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2674 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2675 | com_addopname(c, IMPORT_NAME, CHILD(subn, 0)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2676 | if (NCH(subn) > 1) { |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 2677 | int j; |
| 2678 | if (strcmp(STR(CHILD(subn, 1)), "as") != 0) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2679 | com_error(c, PyExc_SyntaxError, |
| 2680 | "invalid syntax"); |
| 2681 | return; |
| 2682 | } |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 2683 | for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2) |
| 2684 | com_addopname(c, LOAD_ATTR, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2685 | CHILD(CHILD(subn, 0), |
| 2686 | j)); |
| 2687 | com_addop_varname(c, VAR_STORE, |
| 2688 | STR(CHILD(subn, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2689 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2690 | com_addop_varname(c, VAR_STORE, |
| 2691 | STR(CHILD(CHILD(subn, 0), |
| 2692 | 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2693 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2694 | } |
| 2695 | } |
| 2696 | } |
| 2697 | |
| 2698 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2699 | com_exec_stmt(struct compiling *c, node *n) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2700 | { |
| 2701 | REQ(n, exec_stmt); |
| 2702 | /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */ |
| 2703 | com_node(c, CHILD(n, 1)); |
| 2704 | if (NCH(n) >= 4) |
| 2705 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2706 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2707 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2708 | com_push(c, 1); |
| 2709 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2710 | if (NCH(n) >= 6) |
| 2711 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2712 | else { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2713 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2714 | com_push(c, 1); |
| 2715 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2716 | com_addbyte(c, EXEC_STMT); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2717 | com_pop(c, 3); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2718 | } |
| 2719 | |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2720 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2721 | is_constant_false(struct compiling *c, node *n) |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2722 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2723 | PyObject *v; |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2724 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2725 | /* argument c will be NULL when called from symtable_node() */ |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2726 | |
| 2727 | /* Label to avoid tail recursion */ |
| 2728 | next: |
| 2729 | switch (TYPE(n)) { |
| 2730 | |
| 2731 | case suite: |
| 2732 | if (NCH(n) == 1) { |
| 2733 | n = CHILD(n, 0); |
| 2734 | goto next; |
| 2735 | } |
| 2736 | /* Fall through */ |
| 2737 | case file_input: |
| 2738 | for (i = 0; i < NCH(n); i++) { |
| 2739 | node *ch = CHILD(n, i); |
| 2740 | if (TYPE(ch) == stmt) { |
| 2741 | n = ch; |
| 2742 | goto next; |
| 2743 | } |
| 2744 | } |
| 2745 | break; |
| 2746 | |
| 2747 | case stmt: |
| 2748 | case simple_stmt: |
| 2749 | case small_stmt: |
| 2750 | n = CHILD(n, 0); |
| 2751 | goto next; |
| 2752 | |
| 2753 | case expr_stmt: |
| 2754 | case testlist: |
| 2755 | case test: |
| 2756 | case and_test: |
| 2757 | case not_test: |
| 2758 | case comparison: |
| 2759 | case expr: |
| 2760 | case xor_expr: |
| 2761 | case and_expr: |
| 2762 | case shift_expr: |
| 2763 | case arith_expr: |
| 2764 | case term: |
| 2765 | case factor: |
| 2766 | case power: |
| 2767 | case atom: |
| 2768 | if (NCH(n) == 1) { |
| 2769 | n = CHILD(n, 0); |
| 2770 | goto next; |
| 2771 | } |
| 2772 | break; |
| 2773 | |
| 2774 | case NAME: |
| 2775 | if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0) |
| 2776 | return 1; |
| 2777 | break; |
| 2778 | |
| 2779 | case NUMBER: |
| 2780 | v = parsenumber(c, STR(n)); |
| 2781 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2782 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2783 | break; |
| 2784 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2785 | i = PyObject_IsTrue(v); |
| 2786 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2787 | return i == 0; |
| 2788 | |
| 2789 | case STRING: |
Guido van Rossum | 6af0c00 | 1997-03-11 21:25:55 +0000 | [diff] [blame] | 2790 | v = parsestr(STR(n)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2791 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2792 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2793 | break; |
| 2794 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2795 | i = PyObject_IsTrue(v); |
| 2796 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2797 | return i == 0; |
| 2798 | |
| 2799 | } |
| 2800 | return 0; |
| 2801 | } |
| 2802 | |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2803 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2804 | com_if_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2805 | { |
| 2806 | int i; |
| 2807 | int anchor = 0; |
| 2808 | REQ(n, if_stmt); |
| 2809 | /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */ |
| 2810 | for (i = 0; i+3 < NCH(n); i+=4) { |
| 2811 | int a = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2812 | node *ch = CHILD(n, i+1); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2813 | if (is_constant_false(c, ch)) |
| 2814 | continue; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2815 | if (i > 0) |
| 2816 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2817 | com_node(c, ch); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2818 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 2819 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2820 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2821 | com_node(c, CHILD(n, i+3)); |
| 2822 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 2823 | com_backpatch(c, a); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2824 | /* We jump here with an extra entry which we now pop */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2825 | com_addbyte(c, POP_TOP); |
| 2826 | } |
| 2827 | if (i+2 < NCH(n)) |
| 2828 | com_node(c, CHILD(n, i+2)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2829 | if (anchor) |
| 2830 | com_backpatch(c, anchor); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2831 | } |
| 2832 | |
| 2833 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2834 | com_while_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2835 | { |
| 2836 | int break_anchor = 0; |
| 2837 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2838 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2839 | REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */ |
| 2840 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2841 | block_push(c, SETUP_LOOP); |
| 2842 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2843 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2844 | com_node(c, CHILD(n, 1)); |
| 2845 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2846 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2847 | com_pop(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2848 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2849 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2850 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2851 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 2852 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2853 | com_backpatch(c, anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2854 | /* We jump here with one entry more on the stack */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2855 | com_addbyte(c, POP_TOP); |
| 2856 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2857 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2858 | if (NCH(n) > 4) |
| 2859 | com_node(c, CHILD(n, 6)); |
| 2860 | com_backpatch(c, break_anchor); |
| 2861 | } |
| 2862 | |
| 2863 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2864 | com_for_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2865 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2866 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2867 | int break_anchor = 0; |
| 2868 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2869 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2870 | REQ(n, for_stmt); |
| 2871 | /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */ |
| 2872 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2873 | block_push(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2874 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2875 | v = PyInt_FromLong(0L); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2876 | if (v == NULL) |
| 2877 | c->c_errors++; |
| 2878 | com_addoparg(c, LOAD_CONST, com_addconst(c, v)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2879 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2880 | Py_XDECREF(v); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2881 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2882 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2883 | com_addfwref(c, FOR_LOOP, &anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2884 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2885 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2886 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2887 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2888 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2889 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 2890 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2891 | com_backpatch(c, anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2892 | com_pop(c, 2); /* FOR_LOOP has popped these */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2893 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2894 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2895 | if (NCH(n) > 8) |
| 2896 | com_node(c, CHILD(n, 8)); |
| 2897 | com_backpatch(c, break_anchor); |
| 2898 | } |
| 2899 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2900 | /* Code generated for "try: S finally: Sf" is as follows: |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2901 | |
| 2902 | SETUP_FINALLY L |
| 2903 | <code for S> |
| 2904 | POP_BLOCK |
| 2905 | LOAD_CONST <nil> |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2906 | L: <code for Sf> |
| 2907 | END_FINALLY |
| 2908 | |
| 2909 | The special instructions use the block stack. Each block |
| 2910 | stack entry contains the instruction that created it (here |
| 2911 | SETUP_FINALLY), the level of the value stack at the time the |
| 2912 | block stack entry was created, and a label (here L). |
| 2913 | |
| 2914 | SETUP_FINALLY: |
| 2915 | Pushes the current value stack level and the label |
| 2916 | onto the block stack. |
| 2917 | POP_BLOCK: |
| 2918 | Pops en entry from the block stack, and pops the value |
| 2919 | stack until its level is the same as indicated on the |
| 2920 | block stack. (The label is ignored.) |
| 2921 | END_FINALLY: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2922 | Pops a variable number of entries from the *value* stack |
| 2923 | and re-raises the exception they specify. The number of |
| 2924 | entries popped depends on the (pseudo) exception type. |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2925 | |
| 2926 | The block stack is unwound when an exception is raised: |
| 2927 | when a SETUP_FINALLY entry is found, the exception is pushed |
| 2928 | onto the value stack (and the exception condition is cleared), |
| 2929 | and the interpreter jumps to the label gotten from the block |
| 2930 | stack. |
| 2931 | |
| 2932 | 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] | 2933 | (The contents of the value stack is shown in [], with the top |
| 2934 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 2935 | associated value, and 'exc' the exception.) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2936 | |
| 2937 | Value stack Label Instruction Argument |
| 2938 | [] SETUP_EXCEPT L1 |
| 2939 | [] <code for S> |
| 2940 | [] POP_BLOCK |
| 2941 | [] JUMP_FORWARD L0 |
| 2942 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2943 | [tb, val, exc] L1: DUP ) |
| 2944 | [tb, val, exc, exc] <evaluate E1> ) |
| 2945 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 2946 | [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 ) |
| 2947 | [tb, val, exc, 1] POP ) |
| 2948 | [tb, val, exc] POP |
| 2949 | [tb, val] <assign to V1> (or POP if no V1) |
| 2950 | [tb] POP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2951 | [] <code for S1> |
| 2952 | JUMP_FORWARD L0 |
| 2953 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2954 | [tb, val, exc, 0] L2: POP |
| 2955 | [tb, val, exc] DUP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2956 | .............................etc....................... |
| 2957 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2958 | [tb, val, exc, 0] Ln+1: POP |
| 2959 | [tb, val, exc] END_FINALLY # re-raise exception |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2960 | |
| 2961 | [] L0: <next statement> |
| 2962 | |
| 2963 | Of course, parts are not generated if Vi or Ei is not present. |
| 2964 | */ |
| 2965 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2966 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2967 | com_try_except(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2968 | { |
| 2969 | int except_anchor = 0; |
| 2970 | int end_anchor = 0; |
| 2971 | int else_anchor = 0; |
| 2972 | int i; |
| 2973 | node *ch; |
| 2974 | |
| 2975 | com_addfwref(c, SETUP_EXCEPT, &except_anchor); |
| 2976 | block_push(c, SETUP_EXCEPT); |
| 2977 | com_node(c, CHILD(n, 2)); |
| 2978 | com_addbyte(c, POP_BLOCK); |
| 2979 | block_pop(c, SETUP_EXCEPT); |
| 2980 | com_addfwref(c, JUMP_FORWARD, &else_anchor); |
| 2981 | com_backpatch(c, except_anchor); |
| 2982 | for (i = 3; |
| 2983 | i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause; |
| 2984 | i += 3) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2985 | /* except_clause: 'except' [expr [',' var]] */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2986 | if (except_anchor == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2987 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2988 | "default 'except:' must be last"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2989 | break; |
| 2990 | } |
| 2991 | except_anchor = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2992 | com_push(c, 3); /* tb, val, exc pushed by exception */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2993 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 2994 | if (NCH(ch) > 1) { |
| 2995 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2996 | com_push(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2997 | com_node(c, CHILD(ch, 1)); |
| 2998 | com_addoparg(c, COMPARE_OP, EXC_MATCH); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2999 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3000 | com_addfwref(c, JUMP_IF_FALSE, &except_anchor); |
| 3001 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3002 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3003 | } |
| 3004 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3005 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3006 | if (NCH(ch) > 3) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3007 | com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3008 | else { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3009 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3010 | com_pop(c, 1); |
| 3011 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3012 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3013 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3014 | com_node(c, CHILD(n, i+2)); |
| 3015 | com_addfwref(c, JUMP_FORWARD, &end_anchor); |
| 3016 | if (except_anchor) { |
| 3017 | com_backpatch(c, except_anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3018 | /* We come in with [tb, val, exc, 0] on the |
| 3019 | stack; one pop and it's the same as |
| 3020 | expected at the start of the loop */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3021 | com_addbyte(c, POP_TOP); |
| 3022 | } |
| 3023 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3024 | /* We actually come in here with [tb, val, exc] but the |
| 3025 | END_FINALLY will zap those and jump around. |
| 3026 | The c_stacklevel does not reflect them so we need not pop |
| 3027 | anything. */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3028 | com_addbyte(c, END_FINALLY); |
| 3029 | com_backpatch(c, else_anchor); |
| 3030 | if (i < NCH(n)) |
| 3031 | com_node(c, CHILD(n, i+2)); |
| 3032 | com_backpatch(c, end_anchor); |
| 3033 | } |
| 3034 | |
| 3035 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3036 | com_try_finally(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3037 | { |
| 3038 | int finally_anchor = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3039 | node *ch; |
Guido van Rossum | 94fb82e | 1992-04-05 14:24:50 +0000 | [diff] [blame] | 3040 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3041 | com_addfwref(c, SETUP_FINALLY, &finally_anchor); |
| 3042 | block_push(c, SETUP_FINALLY); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3043 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3044 | com_addbyte(c, POP_BLOCK); |
| 3045 | block_pop(c, SETUP_FINALLY); |
| 3046 | block_push(c, END_FINALLY); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3047 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3048 | /* While the generated code pushes only one item, |
| 3049 | the try-finally handling can enter here with |
| 3050 | up to three items. OK, here are the details: |
| 3051 | 3 for an exception, 2 for RETURN, 1 for BREAK. */ |
| 3052 | com_push(c, 3); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3053 | com_backpatch(c, finally_anchor); |
| 3054 | ch = CHILD(n, NCH(n)-1); |
| 3055 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 3056 | com_node(c, ch); |
| 3057 | com_addbyte(c, END_FINALLY); |
| 3058 | block_pop(c, END_FINALLY); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3059 | com_pop(c, 3); /* Matches the com_push above */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3060 | } |
| 3061 | |
| 3062 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3063 | com_try_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3064 | { |
| 3065 | REQ(n, try_stmt); |
| 3066 | /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
| 3067 | | 'try' ':' suite 'finally' ':' suite */ |
| 3068 | if (TYPE(CHILD(n, 3)) != except_clause) |
| 3069 | com_try_finally(c, n); |
| 3070 | else |
| 3071 | com_try_except(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3072 | } |
| 3073 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3074 | static node * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3075 | get_rawdocstring(node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3076 | { |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3077 | int i; |
| 3078 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3079 | /* Label to avoid tail recursion */ |
| 3080 | next: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3081 | switch (TYPE(n)) { |
| 3082 | |
| 3083 | case suite: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3084 | if (NCH(n) == 1) { |
| 3085 | n = CHILD(n, 0); |
| 3086 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3087 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3088 | /* Fall through */ |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3089 | case file_input: |
| 3090 | for (i = 0; i < NCH(n); i++) { |
| 3091 | node *ch = CHILD(n, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3092 | if (TYPE(ch) == stmt) { |
| 3093 | n = ch; |
| 3094 | goto next; |
| 3095 | } |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3096 | } |
| 3097 | break; |
| 3098 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3099 | case stmt: |
| 3100 | case simple_stmt: |
| 3101 | case small_stmt: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3102 | n = CHILD(n, 0); |
| 3103 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3104 | |
| 3105 | case expr_stmt: |
| 3106 | case testlist: |
| 3107 | case test: |
| 3108 | case and_test: |
| 3109 | case not_test: |
| 3110 | case comparison: |
| 3111 | case expr: |
| 3112 | case xor_expr: |
| 3113 | case and_expr: |
| 3114 | case shift_expr: |
| 3115 | case arith_expr: |
| 3116 | case term: |
| 3117 | case factor: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3118 | case power: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3119 | if (NCH(n) == 1) { |
| 3120 | n = CHILD(n, 0); |
| 3121 | goto next; |
| 3122 | } |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3123 | break; |
| 3124 | |
| 3125 | case atom: |
| 3126 | if (TYPE(CHILD(n, 0)) == STRING) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3127 | return n; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3128 | break; |
| 3129 | |
| 3130 | } |
| 3131 | return NULL; |
| 3132 | } |
| 3133 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3134 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3135 | get_docstring(node *n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3136 | { |
Guido van Rossum | 541563e | 1999-01-28 15:08:09 +0000 | [diff] [blame] | 3137 | /* Don't generate doc-strings if run with -OO */ |
| 3138 | if (Py_OptimizeFlag > 1) |
| 3139 | return NULL; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3140 | n = get_rawdocstring(n); |
| 3141 | if (n == NULL) |
| 3142 | return NULL; |
| 3143 | return parsestrplus(n); |
| 3144 | } |
| 3145 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3146 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3147 | com_suite(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3148 | { |
| 3149 | REQ(n, suite); |
| 3150 | /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */ |
| 3151 | if (NCH(n) == 1) { |
| 3152 | com_node(c, CHILD(n, 0)); |
| 3153 | } |
| 3154 | else { |
| 3155 | int i; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3156 | for (i = 0; i < NCH(n) && c->c_errors == 0; i++) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3157 | node *ch = CHILD(n, i); |
| 3158 | if (TYPE(ch) == stmt) |
| 3159 | com_node(c, ch); |
| 3160 | } |
| 3161 | } |
| 3162 | } |
| 3163 | |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3164 | /* ARGSUSED */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3165 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3166 | com_continue_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3167 | { |
| 3168 | int i = c->c_nblocks; |
| 3169 | if (i-- > 0 && c->c_block[i] == SETUP_LOOP) { |
| 3170 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3171 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3172 | else if (i <= 0) { |
| 3173 | /* at the outer level */ |
| 3174 | com_error(c, PyExc_SyntaxError, |
| 3175 | "'continue' not properly in loop"); |
| 3176 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3177 | else { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3178 | int j; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3179 | for (j = i-1; j >= 0; --j) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3180 | if (c->c_block[j] == SETUP_LOOP) |
| 3181 | break; |
| 3182 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3183 | if (j >= 0) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3184 | /* there is a loop, but something interferes */ |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3185 | for (; i > j; --i) { |
| 3186 | if (c->c_block[i] == SETUP_EXCEPT || |
| 3187 | c->c_block[i] == SETUP_FINALLY) { |
| 3188 | com_addoparg(c, CONTINUE_LOOP, |
| 3189 | c->c_begin); |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3190 | return; |
| 3191 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3192 | if (c->c_block[i] == END_FINALLY) { |
| 3193 | com_error(c, PyExc_SyntaxError, |
| 3194 | "'continue' not supported inside 'finally' clause"); |
| 3195 | return; |
| 3196 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3197 | } |
| 3198 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3199 | com_error(c, PyExc_SyntaxError, |
| 3200 | "'continue' not properly in loop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3201 | } |
| 3202 | /* XXX Could allow it inside a 'finally' clause |
| 3203 | XXX if we could pop the exception still on the stack */ |
| 3204 | } |
| 3205 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3206 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3207 | com_argdefs(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3208 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3209 | int i, nch, nargs, ndefs; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3210 | if (TYPE(n) == lambdef) { |
| 3211 | /* lambdef: 'lambda' [varargslist] ':' test */ |
| 3212 | n = CHILD(n, 1); |
| 3213 | } |
| 3214 | else { |
| 3215 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */ |
| 3216 | n = CHILD(n, 2); |
| 3217 | REQ(n, parameters); /* parameters: '(' [varargslist] ')' */ |
| 3218 | n = CHILD(n, 1); |
| 3219 | } |
| 3220 | if (TYPE(n) != varargslist) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3221 | return 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3222 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3223 | (fpdef ['=' test] ',')* '*' ....... | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3224 | fpdef ['=' test] (',' fpdef ['=' test])* [','] */ |
| 3225 | nch = NCH(n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3226 | nargs = 0; |
| 3227 | ndefs = 0; |
| 3228 | for (i = 0; i < nch; i++) { |
| 3229 | int t; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3230 | if (TYPE(CHILD(n, i)) == STAR || |
| 3231 | TYPE(CHILD(n, i)) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3232 | break; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3233 | nargs++; |
| 3234 | i++; |
| 3235 | if (i >= nch) |
Guido van Rossum | a1e7e62 | 1995-09-18 21:44:04 +0000 | [diff] [blame] | 3236 | t = RPAR; /* Anything except EQUAL or COMMA */ |
| 3237 | else |
| 3238 | t = TYPE(CHILD(n, i)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3239 | if (t == EQUAL) { |
| 3240 | i++; |
| 3241 | ndefs++; |
| 3242 | com_node(c, CHILD(n, i)); |
| 3243 | i++; |
| 3244 | if (i >= nch) |
| 3245 | break; |
| 3246 | t = TYPE(CHILD(n, i)); |
| 3247 | } |
| 3248 | else { |
Guido van Rossum | 29d38cd | 1998-10-02 13:41:54 +0000 | [diff] [blame] | 3249 | /* Treat "(a=1, b)" as an error */ |
| 3250 | if (ndefs) |
| 3251 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | dfede31 | 1998-10-02 14:06:56 +0000 | [diff] [blame] | 3252 | "non-default argument follows default argument"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3253 | } |
| 3254 | if (t != COMMA) |
| 3255 | break; |
| 3256 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3257 | return ndefs; |
| 3258 | } |
| 3259 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3260 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3261 | com_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3262 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3263 | PyObject *co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3264 | int ndefs; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3265 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3266 | ndefs = com_argdefs(c, n); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3267 | symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n), |
| 3268 | n->n_lineno); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3269 | co = (PyObject *)icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3270 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3271 | if (co == NULL) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3272 | c->c_errors++; |
| 3273 | else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3274 | int closure = com_make_closure(c, (PyCodeObject *)co); |
| 3275 | int i = com_addconst(c, co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3276 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3277 | com_push(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3278 | if (closure) |
| 3279 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
| 3280 | else |
| 3281 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3282 | com_pop(c, ndefs); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3283 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3284 | com_pop(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3285 | Py_DECREF(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3286 | } |
| 3287 | } |
| 3288 | |
| 3289 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3290 | com_bases(struct compiling *c, node *n) |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3291 | { |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3292 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3293 | REQ(n, testlist); |
| 3294 | /* testlist: test (',' test)* [','] */ |
| 3295 | for (i = 0; i < NCH(n); i += 2) |
| 3296 | com_node(c, CHILD(n, i)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3297 | i = (NCH(n)+1) / 2; |
| 3298 | com_addoparg(c, BUILD_TUPLE, i); |
| 3299 | com_pop(c, i-1); |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3300 | } |
| 3301 | |
| 3302 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3303 | com_classdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3304 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3305 | int i; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3306 | PyObject *co, *v; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3307 | char *name; |
| 3308 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3309 | REQ(n, classdef); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3310 | /* classdef: class NAME ['(' testlist ')'] ':' suite */ |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 3311 | if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3312 | c->c_errors++; |
| 3313 | return; |
| 3314 | } |
| 3315 | /* Push the class name on the stack */ |
| 3316 | i = com_addconst(c, v); |
| 3317 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3318 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3319 | Py_DECREF(v); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3320 | /* Push the tuple of base classes on the stack */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3321 | if (TYPE(CHILD(n, 2)) != LPAR) { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3322 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3323 | com_push(c, 1); |
| 3324 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3325 | else |
| 3326 | com_bases(c, CHILD(n, 3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3327 | name = STR(CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3328 | symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3329 | co = (PyObject *)icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3330 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3331 | if (co == NULL) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3332 | c->c_errors++; |
| 3333 | else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3334 | int closure = com_make_closure(c, (PyCodeObject *)co); |
| 3335 | i = com_addconst(c, co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3336 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3337 | com_push(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3338 | if (closure) |
| 3339 | com_addoparg(c, MAKE_CLOSURE, 0); |
| 3340 | else |
| 3341 | com_addoparg(c, MAKE_FUNCTION, 0); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3342 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3343 | com_addbyte(c, BUILD_CLASS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3344 | com_pop(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3345 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3346 | Py_DECREF(co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3347 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3348 | } |
| 3349 | |
| 3350 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3351 | com_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3352 | { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3353 | loop: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3354 | if (c->c_errors) |
| 3355 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3356 | switch (TYPE(n)) { |
| 3357 | |
| 3358 | /* Definition nodes */ |
| 3359 | |
| 3360 | case funcdef: |
| 3361 | com_funcdef(c, n); |
| 3362 | break; |
| 3363 | case classdef: |
| 3364 | com_classdef(c, n); |
| 3365 | break; |
| 3366 | |
| 3367 | /* Trivial parse tree nodes */ |
| 3368 | |
| 3369 | case stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3370 | case small_stmt: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3371 | case flow_stmt: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3372 | n = CHILD(n, 0); |
| 3373 | goto loop; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3374 | |
| 3375 | case simple_stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3376 | /* small_stmt (';' small_stmt)* [';'] NEWLINE */ |
| 3377 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 3378 | { |
| 3379 | int i; |
| 3380 | for (i = 0; i < NCH(n)-1; i += 2) |
| 3381 | com_node(c, CHILD(n, i)); |
| 3382 | } |
| 3383 | break; |
| 3384 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3385 | case compound_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3386 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3387 | n = CHILD(n, 0); |
| 3388 | goto loop; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3389 | |
| 3390 | /* Statement nodes */ |
| 3391 | |
| 3392 | case expr_stmt: |
| 3393 | com_expr_stmt(c, n); |
| 3394 | break; |
| 3395 | case print_stmt: |
| 3396 | com_print_stmt(c, n); |
| 3397 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3398 | case del_stmt: /* 'del' exprlist */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3399 | com_assign(c, CHILD(n, 1), OP_DELETE, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3400 | break; |
| 3401 | case pass_stmt: |
| 3402 | break; |
| 3403 | case break_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3404 | if (c->c_loops == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3405 | com_error(c, PyExc_SyntaxError, |
| 3406 | "'break' outside loop"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3407 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3408 | com_addbyte(c, BREAK_LOOP); |
| 3409 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3410 | case continue_stmt: |
| 3411 | com_continue_stmt(c, n); |
| 3412 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3413 | case return_stmt: |
| 3414 | com_return_stmt(c, n); |
| 3415 | break; |
| 3416 | case raise_stmt: |
| 3417 | com_raise_stmt(c, n); |
| 3418 | break; |
| 3419 | case import_stmt: |
| 3420 | com_import_stmt(c, n); |
| 3421 | break; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3422 | case global_stmt: |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3423 | break; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3424 | case exec_stmt: |
| 3425 | com_exec_stmt(c, n); |
| 3426 | break; |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 3427 | case assert_stmt: |
| 3428 | com_assert_stmt(c, n); |
| 3429 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3430 | case if_stmt: |
| 3431 | com_if_stmt(c, n); |
| 3432 | break; |
| 3433 | case while_stmt: |
| 3434 | com_while_stmt(c, n); |
| 3435 | break; |
| 3436 | case for_stmt: |
| 3437 | com_for_stmt(c, n); |
| 3438 | break; |
| 3439 | case try_stmt: |
| 3440 | com_try_stmt(c, n); |
| 3441 | break; |
| 3442 | case suite: |
| 3443 | com_suite(c, n); |
| 3444 | break; |
| 3445 | |
| 3446 | /* Expression nodes */ |
| 3447 | |
| 3448 | case testlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3449 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3450 | break; |
| 3451 | case test: |
| 3452 | com_test(c, n); |
| 3453 | break; |
| 3454 | case and_test: |
| 3455 | com_and_test(c, n); |
| 3456 | break; |
| 3457 | case not_test: |
| 3458 | com_not_test(c, n); |
| 3459 | break; |
| 3460 | case comparison: |
| 3461 | com_comparison(c, n); |
| 3462 | break; |
| 3463 | case exprlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3464 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3465 | break; |
| 3466 | case expr: |
| 3467 | com_expr(c, n); |
| 3468 | break; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 3469 | case xor_expr: |
| 3470 | com_xor_expr(c, n); |
| 3471 | break; |
| 3472 | case and_expr: |
| 3473 | com_and_expr(c, n); |
| 3474 | break; |
| 3475 | case shift_expr: |
| 3476 | com_shift_expr(c, n); |
| 3477 | break; |
| 3478 | case arith_expr: |
| 3479 | com_arith_expr(c, n); |
| 3480 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3481 | case term: |
| 3482 | com_term(c, n); |
| 3483 | break; |
| 3484 | case factor: |
| 3485 | com_factor(c, n); |
| 3486 | break; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3487 | case power: |
| 3488 | com_power(c, n); |
| 3489 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3490 | case atom: |
| 3491 | com_atom(c, n); |
| 3492 | break; |
| 3493 | |
| 3494 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3495 | com_error(c, PyExc_SystemError, |
| 3496 | "com_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3497 | } |
| 3498 | } |
| 3499 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 3500 | static void com_fplist(struct compiling *, node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3501 | |
| 3502 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3503 | com_fpdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3504 | { |
| 3505 | REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3506 | if (TYPE(CHILD(n, 0)) == LPAR) |
| 3507 | com_fplist(c, CHILD(n, 1)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3508 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3509 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3510 | com_pop(c, 1); |
| 3511 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3512 | } |
| 3513 | |
| 3514 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3515 | com_fplist(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3516 | { |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 3517 | REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3518 | if (NCH(n) == 1) { |
| 3519 | com_fpdef(c, CHILD(n, 0)); |
| 3520 | } |
| 3521 | else { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3522 | int i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 3523 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3524 | com_push(c, i-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3525 | for (i = 0; i < NCH(n); i += 2) |
| 3526 | com_fpdef(c, CHILD(n, i)); |
| 3527 | } |
| 3528 | } |
| 3529 | |
| 3530 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3531 | com_arglist(struct compiling *c, node *n) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3532 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3533 | int nch, i, narg; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3534 | int complex = 0; |
Guido van Rossum | db9e20f | 1997-07-10 01:06:53 +0000 | [diff] [blame] | 3535 | char nbuf[10]; |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3536 | REQ(n, varargslist); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 3537 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3538 | (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3539 | nch = NCH(n); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3540 | /* Enter all arguments in table of locals */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3541 | for (i = 0, narg = 0; i < nch; i++) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3542 | node *ch = CHILD(n, i); |
| 3543 | node *fp; |
| 3544 | char *name; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3545 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3546 | break; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3547 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3548 | fp = CHILD(ch, 0); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3549 | if (TYPE(fp) == NAME) { |
| 3550 | PyObject *v; |
| 3551 | name = STR(fp); |
| 3552 | v = PyDict_GetItemString(c->c_cellvars, name); |
| 3553 | if (v) { |
| 3554 | com_addoparg(c, LOAD_FAST, narg); |
| 3555 | com_addoparg(c, STORE_DEREF, |
| 3556 | PyInt_AS_LONG(v)); |
| 3557 | } |
| 3558 | } else { |
Guido van Rossum | db9e20f | 1997-07-10 01:06:53 +0000 | [diff] [blame] | 3559 | name = nbuf; |
| 3560 | sprintf(nbuf, ".%d", i); |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 3561 | complex = 1; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3562 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3563 | narg++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3564 | /* all name updates handled by symtable */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3565 | if (++i >= nch) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3566 | break; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3567 | ch = CHILD(n, i); |
| 3568 | if (TYPE(ch) == EQUAL) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3569 | i += 2; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3570 | else |
| 3571 | REQ(ch, COMMA); |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3572 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3573 | /* Handle *arguments */ |
| 3574 | if (i < nch) { |
| 3575 | node *ch; |
| 3576 | ch = CHILD(n, i); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3577 | if (TYPE(ch) != DOUBLESTAR) { |
| 3578 | REQ(ch, STAR); |
| 3579 | ch = CHILD(n, i+1); |
| 3580 | if (TYPE(ch) == NAME) { |
Jeremy Hylton | 6492bf7 | 2001-02-09 22:55:26 +0000 | [diff] [blame] | 3581 | PyObject *v; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3582 | i += 3; |
Jeremy Hylton | 6492bf7 | 2001-02-09 22:55:26 +0000 | [diff] [blame] | 3583 | v = PyDict_GetItemString(c->c_cellvars, |
| 3584 | STR(ch)); |
| 3585 | if (v) { |
| 3586 | com_addoparg(c, LOAD_FAST, narg); |
| 3587 | com_addoparg(c, STORE_DEREF, |
| 3588 | PyInt_AS_LONG(v)); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3589 | } |
Jeremy Hylton | 6492bf7 | 2001-02-09 22:55:26 +0000 | [diff] [blame] | 3590 | narg++; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3591 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3592 | } |
Jeremy Hylton | 6492bf7 | 2001-02-09 22:55:26 +0000 | [diff] [blame] | 3593 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3594 | /* Handle **keywords */ |
| 3595 | if (i < nch) { |
Jeremy Hylton | 6492bf7 | 2001-02-09 22:55:26 +0000 | [diff] [blame] | 3596 | PyObject *v; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3597 | node *ch; |
| 3598 | ch = CHILD(n, i); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3599 | if (TYPE(ch) != DOUBLESTAR) { |
| 3600 | REQ(ch, STAR); |
| 3601 | ch = CHILD(n, i+1); |
| 3602 | REQ(ch, STAR); |
| 3603 | ch = CHILD(n, i+2); |
| 3604 | } |
| 3605 | else |
| 3606 | ch = CHILD(n, i+1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3607 | REQ(ch, NAME); |
Jeremy Hylton | 6492bf7 | 2001-02-09 22:55:26 +0000 | [diff] [blame] | 3608 | v = PyDict_GetItemString(c->c_cellvars, STR(ch)); |
| 3609 | if (v) { |
| 3610 | com_addoparg(c, LOAD_FAST, narg); |
| 3611 | com_addoparg(c, STORE_DEREF, PyInt_AS_LONG(v)); |
| 3612 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3613 | } |
| 3614 | if (complex) { |
| 3615 | /* Generate code for complex arguments only after |
| 3616 | having counted the simple arguments */ |
| 3617 | int ilocal = 0; |
| 3618 | for (i = 0; i < nch; i++) { |
| 3619 | node *ch = CHILD(n, i); |
| 3620 | node *fp; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3621 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3622 | break; |
| 3623 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3624 | fp = CHILD(ch, 0); |
| 3625 | if (TYPE(fp) != NAME) { |
| 3626 | com_addoparg(c, LOAD_FAST, ilocal); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3627 | com_push(c, 1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3628 | com_fpdef(c, ch); |
| 3629 | } |
| 3630 | ilocal++; |
| 3631 | if (++i >= nch) |
| 3632 | break; |
| 3633 | ch = CHILD(n, i); |
| 3634 | if (TYPE(ch) == EQUAL) |
| 3635 | i += 2; |
| 3636 | else |
| 3637 | REQ(ch, COMMA); |
| 3638 | } |
| 3639 | } |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3640 | } |
| 3641 | |
| 3642 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3643 | com_file_input(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3644 | { |
| 3645 | int i; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3646 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3647 | REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */ |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3648 | doc = get_docstring(n); |
| 3649 | if (doc != NULL) { |
| 3650 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3651 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3652 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3653 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3654 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3655 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3656 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3657 | for (i = 0; i < NCH(n); i++) { |
| 3658 | node *ch = CHILD(n, i); |
| 3659 | if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE) |
| 3660 | com_node(c, ch); |
| 3661 | } |
| 3662 | } |
| 3663 | |
| 3664 | /* Top-level compile-node interface */ |
| 3665 | |
| 3666 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3667 | compile_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3668 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3669 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3670 | node *ch; |
| 3671 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3672 | c->c_name = STR(CHILD(n, 1)); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3673 | doc = get_docstring(CHILD(n, 4)); |
| 3674 | if (doc != NULL) { |
| 3675 | (void) com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3676 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3677 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3678 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3679 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3680 | ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */ |
| 3681 | ch = CHILD(ch, 1); /* ')' | varargslist */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3682 | if (TYPE(ch) == varargslist) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3683 | com_arglist(c, ch); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3684 | c->c_infunction = 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3685 | com_node(c, CHILD(n, 4)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3686 | c->c_infunction = 0; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3687 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3688 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3689 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3690 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3691 | } |
| 3692 | |
| 3693 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3694 | compile_lambdef(struct compiling *c, node *n) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 3695 | { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3696 | node *ch; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3697 | REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */ |
Guido van Rossum | 363ac7d | 1994-11-10 22:40:34 +0000 | [diff] [blame] | 3698 | c->c_name = "<lambda>"; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3699 | |
| 3700 | ch = CHILD(n, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3701 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3702 | if (TYPE(ch) == varargslist) { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3703 | com_arglist(c, ch); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3704 | ch = CHILD(n, 3); |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3705 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3706 | else |
| 3707 | ch = CHILD(n, 2); |
| 3708 | com_node(c, ch); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 3709 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3710 | com_pop(c, 1); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 3711 | } |
| 3712 | |
| 3713 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3714 | compile_classdef(struct compiling *c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3715 | { |
| 3716 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3717 | PyObject *doc; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3718 | REQ(n, classdef); |
| 3719 | /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ |
| 3720 | c->c_name = STR(CHILD(n, 1)); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 3721 | c->c_private = c->c_name; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3722 | ch = CHILD(n, NCH(n)-1); /* The suite */ |
| 3723 | doc = get_docstring(ch); |
| 3724 | if (doc != NULL) { |
| 3725 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3726 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3727 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3728 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3729 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3730 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3731 | } |
| 3732 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3733 | (void) com_addconst(c, Py_None); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3734 | com_node(c, ch); |
| 3735 | com_addbyte(c, LOAD_LOCALS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3736 | com_push(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3737 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3738 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3739 | } |
| 3740 | |
| 3741 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3742 | compile_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3743 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3744 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 3745 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3746 | switch (TYPE(n)) { |
| 3747 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3748 | case single_input: /* One interactive command */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3749 | /* NEWLINE | simple_stmt | compound_stmt NEWLINE */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3750 | c->c_interactive++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3751 | n = CHILD(n, 0); |
| 3752 | if (TYPE(n) != NEWLINE) |
| 3753 | com_node(c, n); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3754 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3755 | com_push(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3756 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3757 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3758 | c->c_interactive--; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3759 | break; |
| 3760 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3761 | case file_input: /* A whole file, or built-in function exec() */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3762 | com_file_input(c, n); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3763 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3764 | com_push(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3765 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3766 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3767 | break; |
| 3768 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3769 | case eval_input: /* Built-in function input() */ |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3770 | com_node(c, CHILD(n, 0)); |
| 3771 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3772 | com_pop(c, 1); |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3773 | break; |
| 3774 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3775 | case lambdef: /* anonymous function definition */ |
| 3776 | compile_lambdef(c, n); |
| 3777 | break; |
| 3778 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3779 | case funcdef: /* A function definition */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3780 | compile_funcdef(c, n); |
| 3781 | break; |
| 3782 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3783 | case classdef: /* A class definition */ |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3784 | compile_classdef(c, n); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3785 | break; |
| 3786 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3787 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3788 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 3789 | "compile_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3790 | } |
| 3791 | } |
| 3792 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3793 | static PyObject * |
| 3794 | dict_keys_inorder(PyObject *dict, int offset) |
| 3795 | { |
| 3796 | PyObject *tuple, *k, *v; |
| 3797 | int i, pos = 0, size = PyDict_Size(dict); |
| 3798 | |
| 3799 | tuple = PyTuple_New(size); |
| 3800 | if (tuple == NULL) |
| 3801 | return NULL; |
| 3802 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 3803 | i = PyInt_AS_LONG(v); |
| 3804 | Py_INCREF(k); |
| 3805 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 3806 | } |
| 3807 | return tuple; |
| 3808 | } |
| 3809 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3810 | PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3811 | PyNode_Compile(node *n, char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3812 | { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 3813 | return jcompile(n, filename, NULL); |
| 3814 | } |
| 3815 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3816 | struct symtable * |
| 3817 | PyNode_CompileSymtable(node *n, char *filename) |
| 3818 | { |
| 3819 | struct symtable *st; |
| 3820 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3821 | st = symtable_init(); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3822 | if (st == NULL) |
| 3823 | return NULL; |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 3824 | assert(st->st_symbols != NULL); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3825 | symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno); |
| 3826 | if (st->st_errors > 0) { |
| 3827 | PySymtable_Free(st); |
| 3828 | return NULL; |
| 3829 | } |
| 3830 | symtable_node(st, n); |
| 3831 | if (st->st_errors > 0) { |
| 3832 | PySymtable_Free(st); |
| 3833 | return NULL; |
| 3834 | } |
| 3835 | return st; |
| 3836 | } |
| 3837 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3838 | static PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3839 | icompile(node *n, struct compiling *base) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 3840 | { |
| 3841 | return jcompile(n, base->c_filename, base); |
| 3842 | } |
| 3843 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3844 | static PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3845 | jcompile(node *n, char *filename, struct compiling *base) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 3846 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3847 | struct compiling sc; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3848 | PyCodeObject *co; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3849 | if (!com_init(&sc, filename)) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3850 | return NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3851 | if (base) { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 3852 | sc.c_private = base->c_private; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3853 | sc.c_symtable = base->c_symtable; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3854 | /* c_symtable still points to parent's symbols */ |
| 3855 | if (base->c_nested |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3856 | || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3857 | sc.c_nested = 1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3858 | } else { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 3859 | sc.c_private = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3860 | if (symtable_build(&sc, n) < 0) { |
| 3861 | com_free(&sc); |
| 3862 | return NULL; |
| 3863 | } |
| 3864 | } |
| 3865 | co = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3866 | if (symtable_load_symbols(&sc) < 0) { |
| 3867 | sc.c_errors++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3868 | goto exit; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3869 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3870 | compile_node(&sc, n); |
| 3871 | com_done(&sc); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3872 | if (sc.c_errors == 0) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3873 | PyObject *consts, *names, *varnames, *filename, *name, |
| 3874 | *freevars, *cellvars; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3875 | consts = PyList_AsTuple(sc.c_consts); |
| 3876 | names = PyList_AsTuple(sc.c_names); |
| 3877 | varnames = PyList_AsTuple(sc.c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3878 | cellvars = dict_keys_inorder(sc.c_cellvars, 0); |
| 3879 | freevars = dict_keys_inorder(sc.c_freevars, |
| 3880 | PyTuple_GET_SIZE(cellvars)); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 3881 | filename = PyString_InternFromString(sc.c_filename); |
| 3882 | name = PyString_InternFromString(sc.c_name); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3883 | if (!PyErr_Occurred()) |
| 3884 | co = PyCode_New(sc.c_argcount, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3885 | sc.c_nlocals, |
| 3886 | sc.c_maxstacklevel, |
| 3887 | sc.c_flags, |
| 3888 | sc.c_code, |
| 3889 | consts, |
| 3890 | names, |
| 3891 | varnames, |
| 3892 | freevars, |
| 3893 | cellvars, |
| 3894 | filename, |
| 3895 | name, |
| 3896 | sc.c_firstlineno, |
| 3897 | sc.c_lnotab); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3898 | Py_XDECREF(consts); |
| 3899 | Py_XDECREF(names); |
| 3900 | Py_XDECREF(varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3901 | Py_XDECREF(freevars); |
| 3902 | Py_XDECREF(cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3903 | Py_XDECREF(filename); |
| 3904 | Py_XDECREF(name); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 3905 | } |
Guido van Rossum | 6c2f0c7 | 2000-08-07 19:22:43 +0000 | [diff] [blame] | 3906 | else if (!PyErr_Occurred()) { |
| 3907 | /* This could happen if someone called PyErr_Clear() after an |
| 3908 | error was reported above. That's not supposed to happen, |
| 3909 | but I just plugged one case and I'm not sure there can't be |
| 3910 | others. In that case, raise SystemError so that at least |
| 3911 | it gets reported instead dumping core. */ |
| 3912 | PyErr_SetString(PyExc_SystemError, "lost syntax error"); |
| 3913 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3914 | exit: |
| 3915 | if (base == NULL) |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3916 | PySymtable_Free(sc.c_symtable); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3917 | com_free(&sc); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3918 | return co; |
| 3919 | } |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 3920 | |
| 3921 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3922 | PyCode_Addr2Line(PyCodeObject *co, int addrq) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 3923 | { |
| 3924 | int size = PyString_Size(co->co_lnotab) / 2; |
Guido van Rossum | 2174dcb | 1999-09-15 22:48:09 +0000 | [diff] [blame] | 3925 | unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 3926 | int line = co->co_firstlineno; |
| 3927 | int addr = 0; |
| 3928 | while (--size >= 0) { |
| 3929 | addr += *p++; |
| 3930 | if (addr > addrq) |
| 3931 | break; |
| 3932 | line += *p++; |
| 3933 | } |
| 3934 | return line; |
| 3935 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3936 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3937 | /* The test for LOCAL must come before the test for FREE in order to |
| 3938 | handle classes where name is both local and free. The local var is |
| 3939 | a method and the free var is a free var referenced within a method. |
| 3940 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3941 | |
| 3942 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3943 | get_ref_type(struct compiling *c, char *name) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3944 | { |
| 3945 | PyObject *v; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3946 | if (PyDict_GetItemString(c->c_cellvars, name) != NULL) |
| 3947 | return CELL; |
| 3948 | if (PyDict_GetItemString(c->c_locals, name) != NULL) |
| 3949 | return LOCAL; |
| 3950 | if (PyDict_GetItemString(c->c_freevars, name) != NULL) |
| 3951 | return FREE; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3952 | v = PyDict_GetItemString(c->c_globals, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3953 | if (v) { |
| 3954 | if (v == Py_None) |
| 3955 | return GLOBAL_EXPLICIT; |
| 3956 | else { |
| 3957 | return GLOBAL_IMPLICIT; |
| 3958 | } |
| 3959 | } |
| 3960 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3961 | char buf[350]; |
| 3962 | sprintf(buf, |
| 3963 | "unknown scope for %.100s in %.100s(%s) in %s", |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 3964 | name, c->c_name, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3965 | PyObject_REPR(c->c_symtable->st_cur->ste_id), |
| 3966 | c->c_filename); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3967 | Py_FatalError(buf); |
| 3968 | } |
| 3969 | return -1; /* can't get here */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3970 | } |
| 3971 | |
| 3972 | static int |
| 3973 | symtable_build(struct compiling *c, node *n) |
| 3974 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3975 | if ((c->c_symtable = symtable_init()) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3976 | return -1; |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 3977 | c->c_symtable->st_filename = c->c_filename; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3978 | symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno); |
| 3979 | if (c->c_symtable->st_errors > 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3980 | return -1; |
| 3981 | symtable_node(c->c_symtable, n); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 3982 | if (c->c_symtable->st_errors > 0) |
| 3983 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3984 | /* reset for second pass */ |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3985 | c->c_symtable->st_nscopes = 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3986 | c->c_symtable->st_pass = 2; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3987 | return 0; |
| 3988 | } |
| 3989 | |
| 3990 | static int |
| 3991 | symtable_load_symbols(struct compiling *c) |
| 3992 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3993 | static PyObject *implicit = NULL; |
| 3994 | PyObject *name, *varnames, *v; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3995 | int i, flags, pos; |
| 3996 | int nlocals, nfrees, ncells, nimplicit; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3997 | struct symtable *st = c->c_symtable; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3998 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3999 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4000 | if (implicit == NULL) { |
| 4001 | implicit = PyInt_FromLong(1); |
| 4002 | if (implicit == NULL) |
| 4003 | return -1; |
| 4004 | } |
| 4005 | v = NULL; |
| 4006 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4007 | varnames = st->st_cur->ste_varnames; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4008 | if (varnames == NULL) { |
| 4009 | varnames = PyList_New(0); |
| 4010 | if (varnames == NULL) |
| 4011 | return -1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4012 | ste->ste_varnames = varnames; |
| 4013 | Py_INCREF(varnames); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4014 | } else |
| 4015 | Py_INCREF(varnames); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4016 | c->c_varnames = varnames; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4017 | |
| 4018 | c->c_globals = PyDict_New(); |
| 4019 | if (c->c_globals == NULL) |
| 4020 | goto fail; |
| 4021 | c->c_freevars = PyDict_New(); |
| 4022 | if (c->c_freevars == NULL) |
| 4023 | goto fail; |
| 4024 | c->c_cellvars = PyDict_New(); |
| 4025 | if (c->c_cellvars == NULL) |
| 4026 | goto fail; |
| 4027 | |
| 4028 | nlocals = PyList_GET_SIZE(varnames); |
| 4029 | c->c_argcount = nlocals; |
| 4030 | nfrees = 0; |
| 4031 | ncells = 0; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4032 | nimplicit = 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4033 | for (i = 0; i < nlocals; ++i) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4034 | v = PyInt_FromLong(i); |
| 4035 | if (PyDict_SetItem(c->c_locals, |
| 4036 | PyList_GET_ITEM(varnames, i), v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4037 | goto fail; |
| 4038 | Py_DECREF(v); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4039 | } |
| 4040 | |
| 4041 | /* XXX The cases below define the rules for whether a name is |
| 4042 | local or global. The logic could probably be clearer. */ |
| 4043 | pos = 0; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4044 | while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) { |
| 4045 | flags = PyInt_AS_LONG(v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4046 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4047 | if (flags & DEF_FREE_GLOBAL) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4048 | /* undo the original DEF_FREE */ |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4049 | flags &= ~(DEF_FREE | DEF_FREE_CLASS); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4050 | |
| 4051 | /* Seperate logic for DEF_FREE. If it occurs in a |
| 4052 | function, it indicates a local that we must |
| 4053 | allocate storage for (a cell var). If it occurs in |
| 4054 | a class, then the class has a method and a free |
| 4055 | variable with the same name. |
| 4056 | */ |
| 4057 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4058 | if ((flags & (DEF_FREE | DEF_FREE_CLASS)) |
| 4059 | && (flags & (DEF_LOCAL | DEF_PARAM))) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4060 | PyObject *dict; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4061 | if (ste->ste_type == TYPE_FUNCTION) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4062 | v = PyInt_FromLong(ncells++); |
| 4063 | dict = c->c_cellvars; |
| 4064 | } else { |
| 4065 | v = PyInt_FromLong(nfrees++); |
| 4066 | dict = c->c_freevars; |
| 4067 | } |
| 4068 | if (v == NULL) |
| 4069 | return -1; |
| 4070 | if (PyDict_SetItem(dict, name, v) < 0) |
| 4071 | goto fail; |
| 4072 | Py_DECREF(v); |
| 4073 | } |
| 4074 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4075 | if (flags & DEF_STAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4076 | c->c_argcount--; |
| 4077 | c->c_flags |= CO_VARARGS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4078 | } else if (flags & DEF_DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4079 | c->c_argcount--; |
| 4080 | c->c_flags |= CO_VARKEYWORDS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4081 | } else if (flags & DEF_INTUPLE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4082 | c->c_argcount--; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4083 | else if (flags & DEF_GLOBAL) { |
| 4084 | if ((flags & DEF_PARAM) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4085 | && (PyString_AS_STRING(name)[0] != '.')){ |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4086 | PyErr_Format(PyExc_SyntaxError, |
| 4087 | "name '%.400s' is local and global", |
| 4088 | PyString_AS_STRING(name)); |
| 4089 | set_error_location(st->st_filename, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4090 | ste->ste_lineno); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4091 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4092 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4093 | if (PyDict_SetItem(c->c_globals, name, Py_None) < 0) |
| 4094 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4095 | } else if (flags & DEF_FREE_GLOBAL) { |
| 4096 | nimplicit++; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4097 | if (PyDict_SetItem(c->c_globals, name, implicit) < 0) |
| 4098 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4099 | } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4100 | v = PyInt_FromLong(nlocals++); |
| 4101 | if (v == NULL) |
| 4102 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4103 | if (PyDict_SetItem(c->c_locals, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4104 | goto fail; |
| 4105 | Py_DECREF(v); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4106 | if (ste->ste_type != TYPE_CLASS) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4107 | if (PyList_Append(c->c_varnames, name) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4108 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4109 | } else if (is_free(flags)) { |
| 4110 | if (ste->ste_nested) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4111 | v = PyInt_FromLong(nfrees++); |
| 4112 | if (v == NULL) |
| 4113 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4114 | if (PyDict_SetItem(c->c_freevars, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4115 | goto fail; |
| 4116 | Py_DECREF(v); |
| 4117 | } else { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4118 | nimplicit++; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4119 | if (PyDict_SetItem(c->c_globals, name, |
| 4120 | implicit) < 0) |
| 4121 | goto fail; |
| 4122 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4123 | } |
| 4124 | } |
| 4125 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4126 | /* The cell vars are the first elements of the closure, |
| 4127 | followed by the free vars. Update the offsets in |
| 4128 | c_freevars to account for number of cellvars. */ |
| 4129 | pos = 0; |
| 4130 | while (PyDict_Next(c->c_freevars, &pos, &name, &v)) { |
| 4131 | int i = PyInt_AS_LONG(v) + ncells; |
| 4132 | PyObject *o = PyInt_FromLong(i); |
| 4133 | if (PyDict_SetItem(c->c_freevars, name, o) < 0) { |
| 4134 | Py_DECREF(o); |
| 4135 | return -1; |
| 4136 | } |
| 4137 | Py_DECREF(o); |
| 4138 | } |
| 4139 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4140 | if (ste->ste_type == TYPE_FUNCTION) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4141 | c->c_nlocals = nlocals; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4142 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4143 | if (ste->ste_type != TYPE_MODULE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4144 | c->c_flags |= CO_NEWLOCALS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4145 | if (ste->ste_type == TYPE_FUNCTION) { |
| 4146 | if (ste->ste_optimized) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4147 | c->c_flags |= CO_OPTIMIZED; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4148 | else if (ncells || nfrees |
| 4149 | || (ste->ste_nested && nimplicit) |
| 4150 | || ste->ste_child_free) { |
| 4151 | PyErr_Format(PyExc_SyntaxError, ILLEGAL_DYNAMIC_SCOPE, |
| 4152 | PyString_AS_STRING(ste->ste_name)); |
| 4153 | set_error_location(st->st_filename, ste->ste_lineno); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4154 | return -1; |
| 4155 | } |
| 4156 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4157 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4158 | return 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4159 | |
| 4160 | fail: |
| 4161 | /* is this always the right thing to do? */ |
| 4162 | Py_XDECREF(v); |
| 4163 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4164 | } |
| 4165 | |
| 4166 | static struct symtable * |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4167 | symtable_init() |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4168 | { |
| 4169 | struct symtable *st; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4170 | |
| 4171 | st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable)); |
| 4172 | if (st == NULL) |
| 4173 | return NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4174 | st->st_pass = 1; |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4175 | st->st_filename = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4176 | if ((st->st_stack = PyList_New(0)) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4177 | goto fail; |
| 4178 | if ((st->st_symbols = PyDict_New()) == NULL) |
| 4179 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4180 | st->st_cur = NULL; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4181 | st->st_nscopes = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4182 | st->st_errors = 0; |
| 4183 | st->st_tmpname = 0; |
| 4184 | st->st_private = NULL; |
| 4185 | return st; |
| 4186 | fail: |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4187 | PySymtable_Free(st); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4188 | return NULL; |
| 4189 | } |
| 4190 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4191 | void |
| 4192 | PySymtable_Free(struct symtable *st) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4193 | { |
| 4194 | Py_XDECREF(st->st_symbols); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4195 | Py_XDECREF(st->st_stack); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4196 | PyMem_Free((void *)st); |
| 4197 | } |
| 4198 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4199 | /* When the compiler exits a scope, it must should update the scope's |
| 4200 | free variable information with the list of free variables in its |
| 4201 | children. |
| 4202 | |
| 4203 | Variables that are free in children and defined in the current |
| 4204 | scope are cellvars. |
| 4205 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4206 | 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] | 4207 | false), free variables in children that are not defined here are |
| 4208 | implicit globals. |
| 4209 | |
| 4210 | */ |
| 4211 | |
| 4212 | static int |
| 4213 | symtable_update_free_vars(struct symtable *st) |
| 4214 | { |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4215 | int i, j, def; |
| 4216 | PyObject *o, *name, *list = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4217 | PySymtableEntryObject *child, *ste = st->st_cur; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4218 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4219 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4220 | def = DEF_FREE_CLASS; |
| 4221 | else |
| 4222 | def = DEF_FREE; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4223 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4224 | int pos = 0; |
| 4225 | |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4226 | if (list) |
| 4227 | PyList_SetSlice(list, 0, |
| 4228 | ((PyVarObject*)list)->ob_size, 0); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4229 | child = (PySymtableEntryObject *)\ |
| 4230 | PyList_GET_ITEM(ste->ste_children, i); |
| 4231 | while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4232 | int v = PyInt_AS_LONG(o); |
| 4233 | if (!(is_free(v))) |
| 4234 | continue; /* avoids indentation */ |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4235 | if (list == NULL) { |
| 4236 | list = PyList_New(0); |
| 4237 | if (list == NULL) |
| 4238 | return -1; |
| 4239 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4240 | ste->ste_child_free = 1; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4241 | if (PyList_Append(list, name) < 0) { |
| 4242 | Py_DECREF(list); |
| 4243 | return -1; |
| 4244 | } |
| 4245 | } |
| 4246 | for (j = 0; list && j < PyList_GET_SIZE(list); j++) { |
| 4247 | name = PyList_GET_ITEM(list, j); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4248 | if (ste->ste_nested) { |
| 4249 | if (symtable_add_def_o(st, ste->ste_symbols, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4250 | name, def) < 0) { |
| 4251 | Py_DECREF(list); |
| 4252 | return -1; |
| 4253 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4254 | } else { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4255 | if (symtable_check_global(st, child->ste_id, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4256 | name) < 0) { |
| 4257 | Py_DECREF(list); |
| 4258 | return -1; |
| 4259 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4260 | } |
| 4261 | } |
| 4262 | } |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4263 | |
| 4264 | Py_XDECREF(list); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4265 | return 0; |
| 4266 | } |
| 4267 | |
| 4268 | /* If the current scope is a non-nested class or if name is not |
| 4269 | defined in the current, non-nested scope, then it is an implicit |
| 4270 | global in all nested scopes. |
| 4271 | */ |
| 4272 | |
| 4273 | static int |
| 4274 | symtable_check_global(struct symtable *st, PyObject *child, PyObject *name) |
| 4275 | { |
| 4276 | PyObject *o; |
| 4277 | int v; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4278 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4279 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4280 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4281 | return symtable_undo_free(st, child, name); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4282 | o = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4283 | if (o == NULL) |
| 4284 | return symtable_undo_free(st, child, name); |
| 4285 | v = PyInt_AS_LONG(o); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4286 | if (is_free(v) || (v & DEF_GLOBAL)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4287 | return symtable_undo_free(st, child, name); |
| 4288 | else |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4289 | return symtable_add_def_o(st, ste->ste_symbols, |
| 4290 | name, DEF_FREE); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4291 | } |
| 4292 | |
| 4293 | static int |
| 4294 | symtable_undo_free(struct symtable *st, PyObject *id, |
| 4295 | PyObject *name) |
| 4296 | { |
| 4297 | int i, v, x; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4298 | PyObject *info; |
| 4299 | PySymtableEntryObject *ste; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4300 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4301 | ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id); |
| 4302 | if (ste == NULL) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4303 | return -1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4304 | info = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4305 | if (info == NULL) |
| 4306 | return 0; |
| 4307 | v = PyInt_AS_LONG(info); |
| 4308 | if (is_free(v)) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4309 | if (symtable_add_def_o(st, ste->ste_symbols, name, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4310 | DEF_FREE_GLOBAL) < 0) |
| 4311 | return -1; |
| 4312 | } else |
| 4313 | /* If the name is defined here or declared global, |
| 4314 | then the recursion stops. */ |
| 4315 | return 0; |
| 4316 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4317 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
| 4318 | PySymtableEntryObject *child; |
| 4319 | child = (PySymtableEntryObject *) \ |
| 4320 | PyList_GET_ITEM(ste->ste_children, i); |
| 4321 | x = symtable_undo_free(st, child->ste_id, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4322 | if (x < 0) |
| 4323 | return x; |
| 4324 | } |
| 4325 | return 0; |
| 4326 | } |
| 4327 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4328 | static int |
| 4329 | symtable_exit_scope(struct symtable *st) |
| 4330 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4331 | int end; |
| 4332 | |
| 4333 | if (st->st_pass == 1) |
| 4334 | symtable_update_free_vars(st); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4335 | Py_DECREF(st->st_cur); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4336 | end = PyList_GET_SIZE(st->st_stack) - 1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4337 | st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack, |
| 4338 | end); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4339 | if (PySequence_DelItem(st->st_stack, end) < 0) |
| 4340 | return -1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4341 | return 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4342 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4343 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4344 | static void |
| 4345 | symtable_enter_scope(struct symtable *st, char *name, int type, |
| 4346 | int lineno) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4347 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4348 | PySymtableEntryObject *prev = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4349 | |
| 4350 | if (st->st_cur) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4351 | prev = st->st_cur; |
| 4352 | if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) { |
| 4353 | Py_DECREF(st->st_cur); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4354 | st->st_errors++; |
| 4355 | return; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4356 | } |
| 4357 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4358 | st->st_cur = (PySymtableEntryObject *)\ |
| 4359 | PySymtableEntry_New(st, name, type, lineno); |
| 4360 | if (strcmp(name, TOP) == 0) |
| 4361 | st->st_global = st->st_cur->ste_symbols; |
| 4362 | if (prev) |
| 4363 | if (PyList_Append(prev->ste_children, |
| 4364 | (PyObject *)st->st_cur) < 0) |
| 4365 | st->st_errors++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4366 | } |
| 4367 | |
| 4368 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4369 | symtable_add_def(struct symtable *st, char *name, int flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4370 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4371 | PyObject *s; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4372 | char buffer[MANGLE_LEN]; |
| 4373 | |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4374 | if (mangle(st->st_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4375 | name = buffer; |
| 4376 | if ((s = PyString_InternFromString(name)) == NULL) |
| 4377 | return -1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4378 | return symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4379 | } |
| 4380 | |
| 4381 | /* Must only be called with mangled names */ |
| 4382 | |
| 4383 | static int |
| 4384 | symtable_add_def_o(struct symtable *st, PyObject *dict, |
| 4385 | PyObject *name, int flag) |
| 4386 | { |
| 4387 | PyObject *o; |
| 4388 | int val; |
| 4389 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4390 | if ((o = PyDict_GetItem(dict, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4391 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4392 | if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4393 | PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 4394 | PyString_AsString(name)); |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4395 | set_error_location(st->st_filename, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4396 | st->st_cur->ste_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4397 | return -1; |
| 4398 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4399 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4400 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4401 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4402 | o = PyInt_FromLong(val); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4403 | if (PyDict_SetItem(dict, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4404 | Py_DECREF(o); |
| 4405 | return -1; |
| 4406 | } |
| 4407 | Py_DECREF(o); |
| 4408 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4409 | if (flag & DEF_PARAM) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4410 | if (PyList_Append(st->st_cur->ste_varnames, name) < 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4411 | return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4412 | } else if (flag & DEF_GLOBAL) { |
| 4413 | /* XXX need to update DEF_GLOBAL for other flags too; |
| 4414 | perhaps only DEF_FREE_GLOBAL */ |
| 4415 | if ((o = PyDict_GetItem(st->st_global, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4416 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4417 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4418 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4419 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4420 | o = PyInt_FromLong(val); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4421 | if (PyDict_SetItem(st->st_global, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4422 | Py_DECREF(o); |
| 4423 | return -1; |
| 4424 | } |
| 4425 | Py_DECREF(o); |
| 4426 | } |
| 4427 | return 0; |
| 4428 | } |
| 4429 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4430 | #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4431 | |
| 4432 | static void |
| 4433 | symtable_node(struct symtable *st, node *n) |
| 4434 | { |
| 4435 | int i, start = 0; |
| 4436 | |
| 4437 | loop: |
| 4438 | switch (TYPE(n)) { |
| 4439 | case funcdef: { |
| 4440 | char *func_name = STR(CHILD(n, 1)); |
| 4441 | symtable_add_def(st, func_name, DEF_LOCAL); |
| 4442 | symtable_default_args(st, CHILD(n, 2)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4443 | symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4444 | symtable_funcdef(st, n); |
| 4445 | symtable_exit_scope(st); |
| 4446 | break; |
| 4447 | } |
| 4448 | case lambdef: |
| 4449 | if (NCH(n) == 4) |
| 4450 | symtable_default_args(st, CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4451 | symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4452 | symtable_funcdef(st, n); |
| 4453 | symtable_exit_scope(st); |
| 4454 | break; |
| 4455 | case classdef: { |
| 4456 | char *tmp, *class_name = STR(CHILD(n, 1)); |
| 4457 | symtable_add_def(st, class_name, DEF_LOCAL); |
| 4458 | if (TYPE(CHILD(n, 2)) == LPAR) { |
| 4459 | node *bases = CHILD(n, 3); |
| 4460 | int i; |
| 4461 | for (i = 0; i < NCH(bases); i += 2) { |
| 4462 | symtable_node(st, CHILD(bases, i)); |
| 4463 | } |
| 4464 | } |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4465 | symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4466 | tmp = st->st_private; |
| 4467 | st->st_private = class_name; |
| 4468 | symtable_node(st, CHILD(n, NCH(n) - 1)); |
| 4469 | st->st_private = tmp; |
| 4470 | symtable_exit_scope(st); |
| 4471 | break; |
| 4472 | } |
| 4473 | case if_stmt: |
| 4474 | for (i = 0; i + 3 < NCH(n); i += 4) { |
| 4475 | if (is_constant_false(NULL, (CHILD(n, i + 1)))) |
| 4476 | continue; |
| 4477 | symtable_node(st, CHILD(n, i + 1)); |
| 4478 | symtable_node(st, CHILD(n, i + 3)); |
| 4479 | } |
| 4480 | if (i + 2 < NCH(n)) |
| 4481 | symtable_node(st, CHILD(n, i + 2)); |
| 4482 | break; |
| 4483 | case global_stmt: |
| 4484 | symtable_global(st, n); |
| 4485 | break; |
| 4486 | case import_stmt: |
| 4487 | symtable_import(st, n); |
| 4488 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 4489 | case exec_stmt: { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4490 | st->st_cur->ste_optimized = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4491 | symtable_node(st, CHILD(n, 1)); |
| 4492 | if (NCH(n) > 2) |
| 4493 | symtable_node(st, CHILD(n, 3)); |
| 4494 | if (NCH(n) > 4) |
| 4495 | symtable_node(st, CHILD(n, 5)); |
| 4496 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 4497 | |
| 4498 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4499 | case except_clause: |
| 4500 | if (NCH(n) == 4) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4501 | symtable_assign(st, CHILD(n, 3), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4502 | if (NCH(n) > 1) { |
| 4503 | n = CHILD(n, 1); |
| 4504 | goto loop; |
| 4505 | } |
| 4506 | break; |
| 4507 | case del_stmt: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4508 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4509 | break; |
| 4510 | case expr_stmt: |
| 4511 | if (NCH(n) == 1) |
| 4512 | n = CHILD(n, 0); |
| 4513 | else { |
| 4514 | if (TYPE(CHILD(n, 1)) == augassign) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4515 | symtable_assign(st, CHILD(n, 0), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4516 | symtable_node(st, CHILD(n, 2)); |
| 4517 | break; |
| 4518 | } else { |
| 4519 | int i; |
| 4520 | for (i = 0; i < NCH(n) - 2; i += 2) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4521 | symtable_assign(st, CHILD(n, i), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4522 | n = CHILD(n, NCH(n) - 1); |
| 4523 | } |
| 4524 | } |
| 4525 | goto loop; |
| 4526 | /* watchout for fall-through logic below */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4527 | case argument: |
| 4528 | if (NCH(n) == 3) { |
| 4529 | n = CHILD(n, 2); |
| 4530 | goto loop; |
| 4531 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4532 | case listmaker: |
| 4533 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
| 4534 | symtable_list_comprehension(st, CHILD(n, 1)); |
Jeremy Hylton | 5f827f4 | 2001-01-23 01:26:20 +0000 | [diff] [blame] | 4535 | n = CHILD(n, 0); |
| 4536 | goto loop; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4537 | } |
| 4538 | case atom: |
| 4539 | if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) { |
| 4540 | symtable_add_use(st, STR(CHILD(n, 0))); |
| 4541 | break; |
| 4542 | } |
| 4543 | case for_stmt: |
| 4544 | if (TYPE(n) == for_stmt) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4545 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4546 | start = 3; |
| 4547 | } |
| 4548 | default: |
| 4549 | if (NCH(n) == 1) { |
| 4550 | n = CHILD(n, 0); |
| 4551 | goto loop; |
| 4552 | } |
| 4553 | for (i = start; i < NCH(n); ++i) |
| 4554 | if (TYPE(CHILD(n, i)) >= single_input) |
| 4555 | symtable_node(st, CHILD(n, i)); |
| 4556 | } |
| 4557 | } |
| 4558 | |
| 4559 | static void |
| 4560 | symtable_funcdef(struct symtable *st, node *n) |
| 4561 | { |
| 4562 | node *body; |
| 4563 | |
| 4564 | if (TYPE(n) == lambdef) { |
| 4565 | if (NCH(n) == 4) |
| 4566 | symtable_params(st, CHILD(n, 1)); |
| 4567 | } else |
| 4568 | symtable_params(st, CHILD(n, 2)); |
| 4569 | body = CHILD(n, NCH(n) - 1); |
| 4570 | symtable_node(st, body); |
| 4571 | } |
| 4572 | |
| 4573 | /* The next two functions parse the argument tuple. |
| 4574 | symtable_default_arg() checks for names in the default arguments, |
| 4575 | which are references in the defining scope. symtable_params() |
| 4576 | parses the parameter names, which are defined in the function's |
| 4577 | body. |
| 4578 | |
| 4579 | varargslist: |
| 4580 | (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) |
| 4581 | | fpdef ['=' test] (',' fpdef ['=' test])* [','] |
| 4582 | */ |
| 4583 | |
| 4584 | static void |
| 4585 | symtable_default_args(struct symtable *st, node *n) |
| 4586 | { |
| 4587 | node *c; |
| 4588 | int i; |
| 4589 | |
| 4590 | if (TYPE(n) == parameters) { |
| 4591 | n = CHILD(n, 1); |
| 4592 | if (TYPE(n) == RPAR) |
| 4593 | return; |
| 4594 | } |
| 4595 | REQ(n, varargslist); |
| 4596 | for (i = 0; i < NCH(n); i += 2) { |
| 4597 | c = CHILD(n, i); |
| 4598 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 4599 | break; |
| 4600 | } |
| 4601 | if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL)) |
| 4602 | symtable_node(st, CHILD(n, i)); |
| 4603 | } |
| 4604 | } |
| 4605 | |
| 4606 | static void |
| 4607 | symtable_params(struct symtable *st, node *n) |
| 4608 | { |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 4609 | int i, complex = -1, ext = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4610 | node *c = NULL; |
| 4611 | |
| 4612 | if (TYPE(n) == parameters) { |
| 4613 | n = CHILD(n, 1); |
| 4614 | if (TYPE(n) == RPAR) |
| 4615 | return; |
| 4616 | } |
| 4617 | REQ(n, varargslist); |
| 4618 | for (i = 0; i < NCH(n); i += 2) { |
| 4619 | c = CHILD(n, i); |
| 4620 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 4621 | ext = 1; |
| 4622 | break; |
| 4623 | } |
| 4624 | if (TYPE(c) == test) { |
| 4625 | continue; |
| 4626 | } |
| 4627 | if (TYPE(CHILD(c, 0)) == NAME) |
| 4628 | symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM); |
| 4629 | else { |
| 4630 | char nbuf[10]; |
| 4631 | sprintf(nbuf, ".%d", i); |
| 4632 | symtable_add_def(st, nbuf, DEF_PARAM); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 4633 | complex = i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4634 | } |
| 4635 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4636 | if (ext) { |
| 4637 | c = CHILD(n, i); |
| 4638 | if (TYPE(c) == STAR) { |
| 4639 | i++; |
| 4640 | symtable_add_def(st, STR(CHILD(n, i)), |
| 4641 | DEF_PARAM | DEF_STAR); |
| 4642 | i += 2; |
Jeremy Hylton | 1113cfc | 2001-01-23 00:50:52 +0000 | [diff] [blame] | 4643 | if (i >= NCH(n)) |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 4644 | c = NULL; |
| 4645 | else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4646 | c = CHILD(n, i); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4647 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 4648 | if (c && TYPE(c) == DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4649 | i++; |
| 4650 | symtable_add_def(st, STR(CHILD(n, i)), |
| 4651 | DEF_PARAM | DEF_DOUBLESTAR); |
| 4652 | } |
| 4653 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 4654 | if (complex >= 0) { |
| 4655 | int j; |
| 4656 | for (j = 0; j <= complex; j++) { |
| 4657 | c = CHILD(n, j); |
| 4658 | if (TYPE(c) == COMMA) |
| 4659 | c = CHILD(n, ++j); |
| 4660 | if (TYPE(CHILD(c, 0)) == LPAR) |
| 4661 | symtable_params_fplist(st, CHILD(c, 1)); |
| 4662 | } |
| 4663 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4664 | } |
| 4665 | |
| 4666 | static void |
| 4667 | symtable_params_fplist(struct symtable *st, node *n) |
| 4668 | { |
| 4669 | int i; |
| 4670 | node *c; |
| 4671 | |
| 4672 | REQ(n, fplist); |
| 4673 | for (i = 0; i < NCH(n); i += 2) { |
| 4674 | c = CHILD(n, i); |
| 4675 | REQ(c, fpdef); |
| 4676 | if (NCH(c) == 1) |
| 4677 | symtable_add_def(st, STR(CHILD(c, 0)), |
| 4678 | DEF_PARAM | DEF_INTUPLE); |
| 4679 | else |
| 4680 | symtable_params_fplist(st, CHILD(c, 1)); |
| 4681 | } |
| 4682 | |
| 4683 | } |
| 4684 | |
| 4685 | static void |
| 4686 | symtable_global(struct symtable *st, node *n) |
| 4687 | { |
| 4688 | int i; |
| 4689 | |
| 4690 | for (i = 1; i < NCH(n); i += 2) |
| 4691 | symtable_add_def(st, STR(CHILD(n, i)), DEF_GLOBAL); |
| 4692 | } |
| 4693 | |
| 4694 | static void |
| 4695 | symtable_list_comprehension(struct symtable *st, node *n) |
| 4696 | { |
| 4697 | char tmpname[12]; |
| 4698 | |
Tim Peters | 3081421 | 2001-02-17 05:30:26 +0000 | [diff] [blame] | 4699 | sprintf(tmpname, "_[%d]", ++st->st_tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4700 | symtable_add_def(st, tmpname, DEF_LOCAL); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4701 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4702 | symtable_node(st, CHILD(n, 3)); |
| 4703 | if (NCH(n) == 5) |
| 4704 | symtable_node(st, CHILD(n, 4)); |
| 4705 | --st->st_tmpname; |
| 4706 | } |
| 4707 | |
| 4708 | static void |
| 4709 | symtable_import(struct symtable *st, node *n) |
| 4710 | { |
| 4711 | int i; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 4712 | /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)* |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4713 | | 'from' dotted_name 'import' |
| 4714 | ('*' | import_as_name (',' import_as_name)*) |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 4715 | import_as_name: NAME [NAME NAME] |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4716 | */ |
| 4717 | |
| 4718 | if (STR(CHILD(n, 0))[0] == 'f') { /* from */ |
| 4719 | if (TYPE(CHILD(n, 3)) == STAR) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4720 | st->st_cur->ste_optimized = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4721 | } else { |
| 4722 | for (i = 3; i < NCH(n); i += 2) { |
| 4723 | node *c = CHILD(n, i); |
| 4724 | if (NCH(c) > 1) /* import as */ |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4725 | symtable_assign(st, CHILD(c, 2), |
| 4726 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4727 | else |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4728 | symtable_assign(st, CHILD(c, 0), |
| 4729 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4730 | } |
| 4731 | } |
| 4732 | } else { |
| 4733 | for (i = 1; i < NCH(n); i += 2) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4734 | symtable_assign(st, CHILD(n, i), DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4735 | } |
| 4736 | } |
| 4737 | } |
| 4738 | |
| 4739 | static void |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4740 | symtable_assign(struct symtable *st, node *n, int flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4741 | { |
| 4742 | node *tmp; |
| 4743 | int i; |
| 4744 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4745 | loop: |
| 4746 | switch (TYPE(n)) { |
| 4747 | case lambdef: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4748 | /* invalid assignment, e.g. lambda x:x=2. The next |
| 4749 | pass will catch this error. */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4750 | return; |
| 4751 | case power: |
| 4752 | if (NCH(n) > 2) { |
| 4753 | for (i = 2; i < NCH(n); ++i) |
| 4754 | if (TYPE(CHILD(n, i)) != DOUBLESTAR) |
| 4755 | symtable_node(st, CHILD(n, i)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4756 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4757 | if (NCH(n) > 1) { |
| 4758 | symtable_node(st, CHILD(n, 0)); |
| 4759 | symtable_node(st, CHILD(n, 1)); |
| 4760 | } else { |
| 4761 | n = CHILD(n, 0); |
| 4762 | goto loop; |
| 4763 | } |
| 4764 | return; |
| 4765 | case listmaker: |
| 4766 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) |
| 4767 | symtable_list_comprehension(st, CHILD(n, 1)); |
| 4768 | else { |
| 4769 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4770 | symtable_assign(st, CHILD(n, i), flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4771 | } |
| 4772 | return; |
| 4773 | case exprlist: |
| 4774 | case testlist: |
| 4775 | if (NCH(n) == 1) { |
| 4776 | n = CHILD(n, 0); |
| 4777 | goto loop; |
| 4778 | } |
| 4779 | else { |
| 4780 | int i; |
| 4781 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4782 | symtable_assign(st, CHILD(n, i), flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4783 | return; |
| 4784 | } |
| 4785 | goto loop; |
| 4786 | case atom: |
| 4787 | tmp = CHILD(n, 0); |
| 4788 | if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) { |
| 4789 | n = CHILD(n, 1); |
| 4790 | goto loop; |
| 4791 | } else if (TYPE(tmp) == NAME) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4792 | symtable_add_def(st, STR(tmp), DEF_LOCAL | flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4793 | return; |
| 4794 | case dotted_as_name: |
| 4795 | if (NCH(n) == 3) |
| 4796 | symtable_add_def(st, STR(CHILD(n, 2)), |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4797 | DEF_LOCAL | flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4798 | else |
| 4799 | symtable_add_def(st, |
| 4800 | STR(CHILD(CHILD(n, |
| 4801 | 0), 0)), |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4802 | DEF_LOCAL | flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4803 | return; |
| 4804 | case dotted_name: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4805 | symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4806 | return; |
| 4807 | case NAME: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4808 | symtable_add_def(st, STR(n), DEF_LOCAL | flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4809 | return; |
| 4810 | default: |
| 4811 | if (NCH(n) == 0) |
| 4812 | return; |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame^] | 4813 | if (NCH(n) == 1) { |
| 4814 | n = CHILD(n, 0); |
| 4815 | goto loop; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4816 | } |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame^] | 4817 | /* Should only occur for errors like x + 1 = 1, |
| 4818 | which will be caught in the next pass. */ |
| 4819 | for (i = 0; i < NCH(n); ++i) |
| 4820 | if (TYPE(CHILD(n, i)) >= single_input) |
| 4821 | symtable_assign(st, CHILD(n, i), flag); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4822 | } |
| 4823 | } |