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