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' |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 8 | XXX Allow 'continue' inside 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 | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 13 | #ifndef NO_PRIVATE_NAME_MANGLING |
| 14 | #define PRIVATE_NAME_MANGLING |
| 15 | #endif |
| 16 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 17 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 19 | #include "node.h" |
| 20 | #include "token.h" |
| 21 | #include "graminit.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 22 | #include "compile.h" |
| 23 | #include "opcode.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 24 | #include "structmember.h" |
| 25 | |
| 26 | #include <ctype.h> |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 28 | /* Three symbols from graminit.h are also defined in Python.h, with |
| 29 | Py_ prefixes to their names. Python.h can't include graminit.h |
| 30 | (which defines too many confusing symbols), but we can check here |
| 31 | that they haven't changed (which is very unlikely, but possible). */ |
| 32 | #if Py_single_input != single_input |
Guido van Rossum | 2f75b29 | 1997-05-20 22:18:48 +0000 | [diff] [blame] | 33 | #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] | 34 | #endif |
| 35 | #if Py_file_input != file_input |
Guido van Rossum | 2f75b29 | 1997-05-20 22:18:48 +0000 | [diff] [blame] | 36 | #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] | 37 | #endif |
| 38 | #if Py_eval_input != eval_input |
Guido van Rossum | 2f75b29 | 1997-05-20 22:18:48 +0000 | [diff] [blame] | 39 | #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] | 40 | #endif |
| 41 | |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 42 | int Py_OptimizeFlag = 0; |
| 43 | |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 44 | #define OP_DELETE 0 |
| 45 | #define OP_ASSIGN 1 |
| 46 | #define OP_APPLY 2 |
| 47 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 48 | #define OFF(x) offsetof(PyCodeObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 49 | |
| 50 | static struct memberlist code_memberlist[] = { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 51 | {"co_argcount", T_INT, OFF(co_argcount), READONLY}, |
| 52 | {"co_nlocals", T_INT, OFF(co_nlocals), READONLY}, |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 53 | {"co_stacksize",T_INT, OFF(co_stacksize), READONLY}, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 54 | {"co_flags", T_INT, OFF(co_flags), READONLY}, |
Guido van Rossum | 39d942d | 1992-01-12 02:30:05 +0000 | [diff] [blame] | 55 | {"co_code", T_OBJECT, OFF(co_code), READONLY}, |
| 56 | {"co_consts", T_OBJECT, OFF(co_consts), READONLY}, |
| 57 | {"co_names", T_OBJECT, OFF(co_names), READONLY}, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 58 | {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY}, |
Guido van Rossum | 39d942d | 1992-01-12 02:30:05 +0000 | [diff] [blame] | 59 | {"co_filename", T_OBJECT, OFF(co_filename), READONLY}, |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 60 | {"co_name", T_OBJECT, OFF(co_name), READONLY}, |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 61 | {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY}, |
| 62 | {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 63 | {NULL} /* Sentinel */ |
| 64 | }; |
| 65 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 66 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 67 | code_getattr(PyCodeObject *co, char *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 68 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 69 | return PyMember_Get((char *)co, code_memberlist, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 70 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 71 | |
| 72 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 73 | code_dealloc(PyCodeObject *co) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 74 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 75 | Py_XDECREF(co->co_code); |
| 76 | Py_XDECREF(co->co_consts); |
| 77 | Py_XDECREF(co->co_names); |
Guido van Rossum | 275558c | 1997-07-25 20:13:49 +0000 | [diff] [blame] | 78 | Py_XDECREF(co->co_varnames); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 79 | Py_XDECREF(co->co_filename); |
| 80 | Py_XDECREF(co->co_name); |
Guido van Rossum | 275558c | 1997-07-25 20:13:49 +0000 | [diff] [blame] | 81 | Py_XDECREF(co->co_lnotab); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 82 | PyObject_DEL(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 85 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 86 | code_repr(PyCodeObject *co) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 87 | { |
| 88 | char buf[500]; |
| 89 | int lineno = -1; |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 90 | char *filename = "???"; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 91 | char *name = "???"; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 92 | |
Guido van Rossum | a396a88 | 2000-04-07 01:21:36 +0000 | [diff] [blame] | 93 | if (co->co_firstlineno != 0) |
| 94 | lineno = co->co_firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 95 | if (co->co_filename && PyString_Check(co->co_filename)) |
| 96 | filename = PyString_AsString(co->co_filename); |
| 97 | if (co->co_name && PyString_Check(co->co_name)) |
| 98 | name = PyString_AsString(co->co_name); |
Fred Drake | 615ae55 | 2000-06-30 16:20:13 +0000 | [diff] [blame] | 99 | sprintf(buf, "<code object %.100s at %p, file \"%.300s\", line %d>", |
| 100 | name, co, filename, lineno); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 101 | return PyString_FromString(buf); |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 104 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 105 | code_compare(PyCodeObject *co, PyCodeObject *cp) |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 106 | { |
| 107 | int cmp; |
Guido van Rossum | 4467959 | 2000-04-10 16:20:31 +0000 | [diff] [blame] | 108 | cmp = PyObject_Compare(co->co_name, cp->co_name); |
| 109 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 110 | cmp = co->co_argcount - cp->co_argcount; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 111 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 112 | cmp = co->co_nlocals - cp->co_nlocals; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 113 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 114 | cmp = co->co_flags - cp->co_flags; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 115 | if (cmp) return cmp; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 116 | cmp = PyObject_Compare(co->co_code, cp->co_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 117 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 118 | cmp = PyObject_Compare(co->co_consts, cp->co_consts); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 119 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 120 | cmp = PyObject_Compare(co->co_names, cp->co_names); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 121 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 122 | cmp = PyObject_Compare(co->co_varnames, cp->co_varnames); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 123 | return cmp; |
| 124 | } |
| 125 | |
| 126 | static long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 127 | code_hash(PyCodeObject *co) |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 128 | { |
Guido van Rossum | 4467959 | 2000-04-10 16:20:31 +0000 | [diff] [blame] | 129 | long h, h0, h1, h2, h3, h4; |
| 130 | h0 = PyObject_Hash(co->co_name); |
| 131 | if (h0 == -1) return -1; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 132 | h1 = PyObject_Hash(co->co_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 133 | if (h1 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 134 | h2 = PyObject_Hash(co->co_consts); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 135 | if (h2 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 136 | h3 = PyObject_Hash(co->co_names); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 137 | if (h3 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 138 | h4 = PyObject_Hash(co->co_varnames); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 139 | if (h4 == -1) return -1; |
Guido van Rossum | 4467959 | 2000-04-10 16:20:31 +0000 | [diff] [blame] | 140 | h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 141 | co->co_argcount ^ co->co_nlocals ^ co->co_flags; |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 142 | if (h == -1) h = -2; |
| 143 | return h; |
| 144 | } |
| 145 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 146 | PyTypeObject PyCode_Type = { |
| 147 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 148 | 0, |
| 149 | "code", |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 150 | sizeof(PyCodeObject), |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 151 | 0, |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 152 | (destructor)code_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 153 | 0, /*tp_print*/ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 154 | (getattrfunc)code_getattr, /*tp_getattr*/ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 155 | 0, /*tp_setattr*/ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 156 | (cmpfunc)code_compare, /*tp_compare*/ |
| 157 | (reprfunc)code_repr, /*tp_repr*/ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 158 | 0, /*tp_as_number*/ |
| 159 | 0, /*tp_as_sequence*/ |
| 160 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 161 | (hashfunc)code_hash, /*tp_hash*/ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 164 | #define NAME_CHARS \ |
| 165 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" |
| 166 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 167 | PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 168 | PyCode_New(int argcount, int nlocals, int stacksize, int flags, |
| 169 | PyObject *code, PyObject *consts, PyObject *names, |
| 170 | PyObject *varnames, PyObject *filename, PyObject *name, |
| 171 | int firstlineno, PyObject *lnotab) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 172 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 173 | PyCodeObject *co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 174 | int i; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 175 | PyBufferProcs *pb; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 176 | /* Check argument types */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 177 | if (argcount < 0 || nlocals < 0 || |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 178 | code == NULL || |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 179 | consts == NULL || !PyTuple_Check(consts) || |
| 180 | names == NULL || !PyTuple_Check(names) || |
| 181 | varnames == NULL || !PyTuple_Check(varnames) || |
| 182 | name == NULL || !PyString_Check(name) || |
| 183 | filename == NULL || !PyString_Check(filename) || |
| 184 | lnotab == NULL || !PyString_Check(lnotab)) { |
| 185 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 186 | return NULL; |
| 187 | } |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 188 | pb = code->ob_type->tp_as_buffer; |
| 189 | if (pb == NULL || |
| 190 | pb->bf_getreadbuffer == NULL || |
| 191 | pb->bf_getsegcount == NULL || |
| 192 | (*pb->bf_getsegcount)(code, NULL) != 1) |
| 193 | { |
| 194 | PyErr_BadInternalCall(); |
| 195 | return NULL; |
| 196 | } |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 197 | /* Make sure names and varnames are all strings, & intern them */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 198 | for (i = PyTuple_Size(names); --i >= 0; ) { |
| 199 | PyObject *v = PyTuple_GetItem(names, i); |
| 200 | if (v == NULL || !PyString_Check(v)) { |
| 201 | PyErr_BadInternalCall(); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 202 | return NULL; |
| 203 | } |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 204 | PyString_InternInPlace(&PyTuple_GET_ITEM(names, i)); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 205 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 206 | for (i = PyTuple_Size(varnames); --i >= 0; ) { |
| 207 | PyObject *v = PyTuple_GetItem(varnames, i); |
| 208 | if (v == NULL || !PyString_Check(v)) { |
| 209 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 210 | return NULL; |
| 211 | } |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 212 | PyString_InternInPlace(&PyTuple_GET_ITEM(varnames, i)); |
| 213 | } |
| 214 | /* Intern selected string constants */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 215 | for (i = PyTuple_Size(consts); --i >= 0; ) { |
| 216 | PyObject *v = PyTuple_GetItem(consts, i); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 217 | char *p; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 218 | if (!PyString_Check(v)) |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 219 | continue; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 220 | p = PyString_AsString(v); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 221 | if (strspn(p, NAME_CHARS) |
| 222 | != (size_t)PyString_Size(v)) |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 223 | continue; |
| 224 | PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 225 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 226 | co = PyObject_NEW(PyCodeObject, &PyCode_Type); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 227 | if (co != NULL) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 228 | co->co_argcount = argcount; |
| 229 | co->co_nlocals = nlocals; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 230 | co->co_stacksize = stacksize; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 231 | co->co_flags = flags; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 232 | Py_INCREF(code); |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 233 | co->co_code = code; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 234 | Py_INCREF(consts); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 235 | co->co_consts = consts; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 236 | Py_INCREF(names); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 237 | co->co_names = names; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 238 | Py_INCREF(varnames); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 239 | co->co_varnames = varnames; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 240 | Py_INCREF(filename); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 241 | co->co_filename = filename; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 242 | Py_INCREF(name); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 243 | co->co_name = name; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 244 | co->co_firstlineno = firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 245 | Py_INCREF(lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 246 | co->co_lnotab = lnotab; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 247 | } |
| 248 | return co; |
| 249 | } |
| 250 | |
| 251 | |
| 252 | /* Data structure used internally */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 253 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 254 | struct compiling { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 255 | PyObject *c_code; /* string */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 256 | PyObject *c_consts; /* list of objects */ |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 257 | PyObject *c_const_dict; /* inverse of c_consts */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 258 | PyObject *c_names; /* list of strings (names) */ |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 259 | PyObject *c_name_dict; /* inverse of c_names */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 260 | PyObject *c_globals; /* dictionary (value=None) */ |
| 261 | PyObject *c_locals; /* dictionary (value=localID) */ |
| 262 | PyObject *c_varnames; /* list (inverse of c_locals) */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 263 | int c_nlocals; /* index of next local */ |
| 264 | int c_argcount; /* number of top-level arguments */ |
| 265 | int c_flags; /* same as co_flags */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 266 | int c_nexti; /* index into c_code */ |
| 267 | int c_errors; /* counts errors occurred */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 268 | int c_infunction; /* set when compiling a function */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 269 | int c_interactive; /* generating code for interactive command */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 270 | int c_loops; /* counts nested loops */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 271 | int c_begin; /* begin of current loop, for 'continue' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 272 | int c_block[CO_MAXBLOCKS]; /* stack of block types */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 273 | int c_nblocks; /* current block stack level */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 274 | char *c_filename; /* filename of current node */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 275 | char *c_name; /* name of object (e.g. function) */ |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 276 | int c_lineno; /* Current line number */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 277 | int c_stacklevel; /* Current stack level */ |
| 278 | int c_maxstacklevel; /* Maximum stack level */ |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 279 | int c_firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 280 | PyObject *c_lnotab; /* Table mapping address to line number */ |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 281 | int c_last_addr, c_last_line, c_lnotab_next; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 282 | #ifdef PRIVATE_NAME_MANGLING |
| 283 | char *c_private; /* for private name mangling */ |
| 284 | #endif |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 285 | int c_tmpname; /* temporary local name counter */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 286 | }; |
| 287 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 288 | |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 289 | /* Error message including line number */ |
| 290 | |
| 291 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 292 | com_error(struct compiling *c, PyObject *exc, char *msg) |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 293 | { |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 294 | PyObject *v, *tb, *tmp; |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 295 | c->c_errors++; |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 296 | if (c->c_lineno <= 1) { |
| 297 | /* Unknown line number or single interactive command */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 298 | PyErr_SetString(exc, msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 299 | return; |
| 300 | } |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 301 | v = PyString_FromString(msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 302 | if (v == NULL) |
| 303 | return; /* MemoryError, too bad */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 304 | PyErr_SetObject(exc, v); |
| 305 | Py_DECREF(v); |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 306 | |
| 307 | /* add attributes for the line number and filename for the error */ |
| 308 | PyErr_Fetch(&exc, &v, &tb); |
| 309 | PyErr_NormalizeException(&exc, &v, &tb); |
| 310 | tmp = PyInt_FromLong(c->c_lineno); |
| 311 | if (tmp == NULL) |
| 312 | PyErr_Clear(); |
| 313 | else { |
| 314 | if (PyObject_SetAttrString(v, "lineno", tmp)) |
| 315 | PyErr_Clear(); |
| 316 | Py_DECREF(tmp); |
| 317 | } |
| 318 | if (c->c_filename != NULL) { |
| 319 | tmp = PyString_FromString(c->c_filename); |
| 320 | if (tmp == NULL) |
| 321 | PyErr_Clear(); |
| 322 | else { |
| 323 | if (PyObject_SetAttrString(v, "filename", tmp)) |
| 324 | PyErr_Clear(); |
| 325 | Py_DECREF(tmp); |
| 326 | } |
| 327 | } |
| 328 | PyErr_Restore(exc, v, tb); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 332 | /* Interface to the block stack */ |
| 333 | |
| 334 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 335 | block_push(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 336 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 337 | if (c->c_nblocks >= CO_MAXBLOCKS) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 338 | com_error(c, PyExc_SystemError, |
| 339 | "too many statically nested blocks"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 340 | } |
| 341 | else { |
| 342 | c->c_block[c->c_nblocks++] = type; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 347 | block_pop(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 348 | { |
| 349 | if (c->c_nblocks > 0) |
| 350 | c->c_nblocks--; |
| 351 | 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] | 352 | com_error(c, PyExc_SystemError, "bad block pop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
| 356 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 357 | /* Prototype forward declarations */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 358 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 359 | static int com_init(struct compiling *, char *); |
| 360 | static void com_free(struct compiling *); |
| 361 | static void com_push(struct compiling *, int); |
| 362 | static void com_pop(struct compiling *, int); |
| 363 | static void com_done(struct compiling *); |
| 364 | static void com_node(struct compiling *, struct _node *); |
| 365 | static void com_factor(struct compiling *, struct _node *); |
| 366 | static void com_addbyte(struct compiling *, int); |
| 367 | static void com_addint(struct compiling *, int); |
| 368 | static void com_addoparg(struct compiling *, int, int); |
| 369 | static void com_addfwref(struct compiling *, int, int *); |
| 370 | static void com_backpatch(struct compiling *, int); |
| 371 | static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *); |
| 372 | static int com_addconst(struct compiling *, PyObject *); |
| 373 | static int com_addname(struct compiling *, PyObject *); |
| 374 | static void com_addopname(struct compiling *, int, node *); |
| 375 | static void com_list(struct compiling *, node *, int); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 376 | static void com_list_iter(struct compiling *, node *, node *, char *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 377 | static int com_argdefs(struct compiling *, node *); |
| 378 | static int com_newlocal(struct compiling *, char *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 379 | static void com_assign(struct compiling *, node *, int, node *); |
| 380 | static void com_assign_name(struct compiling *, node *, int); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 381 | static PyCodeObject *icompile(struct _node *, struct compiling *); |
| 382 | static PyCodeObject *jcompile(struct _node *, char *, |
| 383 | struct compiling *); |
| 384 | static PyObject *parsestrplus(node *); |
| 385 | static PyObject *parsestr(char *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 386 | static node *get_rawdocstring(node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 387 | |
| 388 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 389 | com_init(struct compiling *c, char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 390 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 391 | memset((void *)c, '\0', sizeof(struct compiling)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 392 | if ((c->c_code = PyString_FromStringAndSize((char *)NULL, |
| 393 | 1000)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 394 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 395 | if ((c->c_consts = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 396 | goto fail; |
| 397 | if ((c->c_const_dict = PyDict_New()) == NULL) |
| 398 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 399 | if ((c->c_names = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 400 | goto fail; |
| 401 | if ((c->c_name_dict = PyDict_New()) == NULL) |
| 402 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 403 | if ((c->c_globals = PyDict_New()) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 404 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 405 | if ((c->c_locals = PyDict_New()) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 406 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 407 | if ((c->c_varnames = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 408 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 409 | if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL, |
| 410 | 1000)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 411 | goto fail; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 412 | c->c_nlocals = 0; |
| 413 | c->c_argcount = 0; |
| 414 | c->c_flags = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 415 | c->c_nexti = 0; |
| 416 | c->c_errors = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 417 | c->c_infunction = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 418 | c->c_interactive = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 419 | c->c_loops = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 420 | c->c_begin = 0; |
| 421 | c->c_nblocks = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 422 | c->c_filename = filename; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 423 | c->c_name = "?"; |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 424 | c->c_lineno = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 425 | c->c_stacklevel = 0; |
| 426 | c->c_maxstacklevel = 0; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 427 | c->c_firstlineno = 0; |
| 428 | c->c_last_addr = 0; |
| 429 | c->c_last_line = 0; |
| 430 | c-> c_lnotab_next = 0; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 431 | c->c_tmpname = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 432 | return 1; |
| 433 | |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 434 | fail: |
| 435 | com_free(c); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 440 | com_free(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 441 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 442 | Py_XDECREF(c->c_code); |
| 443 | Py_XDECREF(c->c_consts); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 444 | Py_XDECREF(c->c_const_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 445 | Py_XDECREF(c->c_names); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 446 | Py_XDECREF(c->c_name_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 447 | Py_XDECREF(c->c_globals); |
| 448 | Py_XDECREF(c->c_locals); |
| 449 | Py_XDECREF(c->c_varnames); |
| 450 | Py_XDECREF(c->c_lnotab); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 454 | com_push(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 455 | { |
| 456 | c->c_stacklevel += n; |
| 457 | if (c->c_stacklevel > c->c_maxstacklevel) |
| 458 | c->c_maxstacklevel = c->c_stacklevel; |
| 459 | } |
| 460 | |
| 461 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 462 | com_pop(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 463 | { |
| 464 | if (c->c_stacklevel < n) { |
Guido van Rossum | 90f827c | 1998-08-25 18:22:17 +0000 | [diff] [blame] | 465 | /* fprintf(stderr, |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 466 | "%s:%d: underflow! nexti=%d, level=%d, n=%d\n", |
| 467 | c->c_filename, c->c_lineno, |
Guido van Rossum | 90f827c | 1998-08-25 18:22:17 +0000 | [diff] [blame] | 468 | c->c_nexti, c->c_stacklevel, n); */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 469 | c->c_stacklevel = 0; |
| 470 | } |
| 471 | else |
| 472 | c->c_stacklevel -= n; |
| 473 | } |
| 474 | |
| 475 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 476 | com_done(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 477 | { |
| 478 | if (c->c_code != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 479 | _PyString_Resize(&c->c_code, c->c_nexti); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 480 | if (c->c_lnotab != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 481 | _PyString_Resize(&c->c_lnotab, c->c_lnotab_next); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 485 | com_addbyte(struct compiling *c, int byte) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 486 | { |
| 487 | int len; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 488 | /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 489 | if (byte < 0 || byte > 255) { |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 490 | /* |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 491 | fprintf(stderr, "XXX compiling bad byte: %d\n", byte); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 492 | fatal("com_addbyte: byte out of range"); |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 493 | */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 494 | com_error(c, PyExc_SystemError, |
| 495 | "com_addbyte: byte out of range"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 496 | } |
| 497 | if (c->c_code == NULL) |
| 498 | return; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 499 | len = PyString_Size(c->c_code); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 500 | if (c->c_nexti >= len) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 501 | if (_PyString_Resize(&c->c_code, len+1000) != 0) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 502 | c->c_errors++; |
| 503 | return; |
| 504 | } |
| 505 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 506 | PyString_AsString(c->c_code)[c->c_nexti++] = byte; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 510 | com_addint(struct compiling *c, int x) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 511 | { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 512 | com_addbyte(c, x & 0xff); |
| 513 | com_addbyte(c, x >> 8); /* XXX x should be positive */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 517 | com_add_lnotab(struct compiling *c, int addr, int line) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 518 | { |
| 519 | int size; |
| 520 | char *p; |
| 521 | if (c->c_lnotab == NULL) |
| 522 | return; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 523 | size = PyString_Size(c->c_lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 524 | if (c->c_lnotab_next+2 > size) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 525 | if (_PyString_Resize(&c->c_lnotab, size + 1000) < 0) { |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 526 | c->c_errors++; |
| 527 | return; |
| 528 | } |
| 529 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 530 | p = PyString_AsString(c->c_lnotab) + c->c_lnotab_next; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 531 | *p++ = addr; |
| 532 | *p++ = line; |
| 533 | c->c_lnotab_next += 2; |
| 534 | } |
| 535 | |
| 536 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 537 | com_set_lineno(struct compiling *c, int lineno) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 538 | { |
| 539 | c->c_lineno = lineno; |
| 540 | if (c->c_firstlineno == 0) { |
| 541 | c->c_firstlineno = c->c_last_line = lineno; |
| 542 | } |
| 543 | else { |
| 544 | int incr_addr = c->c_nexti - c->c_last_addr; |
| 545 | int incr_line = lineno - c->c_last_line; |
| 546 | while (incr_addr > 0 || incr_line > 0) { |
| 547 | int trunc_addr = incr_addr; |
| 548 | int trunc_line = incr_line; |
| 549 | if (trunc_addr > 255) |
| 550 | trunc_addr = 255; |
| 551 | if (trunc_line > 255) |
| 552 | trunc_line = 255; |
| 553 | com_add_lnotab(c, trunc_addr, trunc_line); |
| 554 | incr_addr -= trunc_addr; |
| 555 | incr_line -= trunc_line; |
| 556 | } |
| 557 | c->c_last_addr = c->c_nexti; |
| 558 | c->c_last_line = lineno; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 563 | com_addoparg(struct compiling *c, int op, int arg) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 564 | { |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 565 | int extended_arg = arg >> 16; |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 566 | if (op == SET_LINENO) { |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 567 | com_set_lineno(c, arg); |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 568 | if (Py_OptimizeFlag) |
| 569 | return; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 570 | } |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 571 | if (extended_arg){ |
| 572 | com_addbyte(c, EXTENDED_ARG); |
| 573 | com_addint(c, extended_arg); |
| 574 | arg &= 0xffff; |
| 575 | } |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 576 | com_addbyte(c, op); |
| 577 | com_addint(c, arg); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 581 | com_addfwref(struct compiling *c, int op, int *p_anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 582 | { |
| 583 | /* Compile a forward reference for backpatching */ |
| 584 | int here; |
| 585 | int anchor; |
| 586 | com_addbyte(c, op); |
| 587 | here = c->c_nexti; |
| 588 | anchor = *p_anchor; |
| 589 | *p_anchor = here; |
| 590 | com_addint(c, anchor == 0 ? 0 : here - anchor); |
| 591 | } |
| 592 | |
| 593 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 594 | com_backpatch(struct compiling *c, int anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 595 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 596 | unsigned char *code = (unsigned char *) PyString_AsString(c->c_code); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 597 | int target = c->c_nexti; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 598 | int dist; |
| 599 | int prev; |
| 600 | for (;;) { |
| 601 | /* Make the JUMP instruction at anchor point to target */ |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 602 | prev = code[anchor] + (code[anchor+1] << 8); |
| 603 | dist = target - (anchor+2); |
| 604 | code[anchor] = dist & 0xff; |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 605 | dist >>= 8; |
| 606 | code[anchor+1] = dist; |
| 607 | dist >>= 8; |
| 608 | if (dist) { |
| 609 | com_error(c, PyExc_SystemError, |
| 610 | "com_backpatch: offset too large"); |
| 611 | break; |
| 612 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 613 | if (!prev) |
| 614 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 615 | anchor -= prev; |
| 616 | } |
| 617 | } |
| 618 | |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 619 | /* Handle literals and names uniformly */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 620 | |
| 621 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 622 | com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 623 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 624 | PyObject *w, *t, *np=NULL; |
| 625 | long n; |
| 626 | |
| 627 | t = Py_BuildValue("(OO)", v, v->ob_type); |
| 628 | if (t == NULL) |
| 629 | goto fail; |
| 630 | w = PyDict_GetItem(dict, t); |
| 631 | if (w != NULL) { |
| 632 | n = PyInt_AsLong(w); |
| 633 | } else { |
| 634 | n = PyList_Size(list); |
| 635 | np = PyInt_FromLong(n); |
| 636 | if (np == NULL) |
| 637 | goto fail; |
| 638 | if (PyList_Append(list, v) != 0) |
| 639 | goto fail; |
| 640 | if (PyDict_SetItem(dict, t, np) != 0) |
| 641 | goto fail; |
| 642 | Py_DECREF(np); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 643 | } |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 644 | Py_DECREF(t); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 645 | return n; |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 646 | fail: |
| 647 | Py_XDECREF(np); |
| 648 | Py_XDECREF(t); |
| 649 | c->c_errors++; |
| 650 | return 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 654 | com_addconst(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 655 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 656 | 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] | 657 | } |
| 658 | |
| 659 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 660 | com_addname(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 661 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 662 | 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] | 663 | } |
| 664 | |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 665 | #ifdef PRIVATE_NAME_MANGLING |
| 666 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 667 | com_mangle(struct compiling *c, char *name, char *buffer, size_t maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 668 | { |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 669 | /* Name mangling: __private becomes _classname__private. |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 670 | This is independent from how the name is used. */ |
| 671 | char *p; |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 672 | size_t nlen, plen; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 673 | nlen = strlen(name); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 674 | if (nlen+2 >= maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 675 | return 0; /* Don't mangle __extremely_long_names */ |
| 676 | if (name[nlen-1] == '_' && name[nlen-2] == '_') |
| 677 | return 0; /* Don't mangle __whatever__ */ |
| 678 | p = c->c_private; |
| 679 | /* Strip leading underscores from class name */ |
| 680 | while (*p == '_') |
| 681 | p++; |
| 682 | if (*p == '\0') |
| 683 | return 0; /* Don't mangle if class is just underscores */ |
| 684 | plen = strlen(p); |
| 685 | if (plen + nlen >= maxlen) |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 686 | plen = maxlen-nlen-2; /* Truncate class name if too long */ |
| 687 | /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 688 | buffer[0] = '_'; |
| 689 | strncpy(buffer+1, p, plen); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 690 | strcpy(buffer+1+plen, name); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 691 | /* fprintf(stderr, "mangle %s -> %s\n", name, buffer); */ |
| 692 | return 1; |
| 693 | } |
| 694 | #endif |
| 695 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 696 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 697 | com_addopnamestr(struct compiling *c, int op, char *name) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 698 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 699 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 700 | int i; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 701 | #ifdef PRIVATE_NAME_MANGLING |
| 702 | char buffer[256]; |
| 703 | if (name != NULL && name[0] == '_' && name[1] == '_' && |
| 704 | c->c_private != NULL && |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 705 | com_mangle(c, name, buffer, sizeof(buffer))) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 706 | name = buffer; |
| 707 | #endif |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 708 | if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 709 | c->c_errors++; |
| 710 | i = 255; |
| 711 | } |
| 712 | else { |
| 713 | i = com_addname(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 714 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 715 | } |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 716 | /* Hack to replace *_NAME opcodes by *_GLOBAL if necessary */ |
| 717 | switch (op) { |
| 718 | case LOAD_NAME: |
| 719 | case STORE_NAME: |
| 720 | case DELETE_NAME: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 721 | if (PyDict_GetItemString(c->c_globals, name) != NULL) { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 722 | switch (op) { |
| 723 | case LOAD_NAME: op = LOAD_GLOBAL; break; |
| 724 | case STORE_NAME: op = STORE_GLOBAL; break; |
| 725 | case DELETE_NAME: op = DELETE_GLOBAL; break; |
| 726 | } |
| 727 | } |
| 728 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 729 | com_addoparg(c, op, i); |
| 730 | } |
| 731 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 732 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 733 | com_addopname(struct compiling *c, int op, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 734 | { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 735 | char *name; |
| 736 | char buffer[1000]; |
| 737 | /* XXX it is possible to write this code without the 1000 |
| 738 | chars on the total length of dotted names, I just can't be |
| 739 | bothered right now */ |
| 740 | if (TYPE(n) == STAR) |
| 741 | name = "*"; |
| 742 | else if (TYPE(n) == dotted_name) { |
| 743 | char *p = buffer; |
| 744 | int i; |
| 745 | name = buffer; |
| 746 | for (i = 0; i < NCH(n); i += 2) { |
| 747 | char *s = STR(CHILD(n, i)); |
| 748 | if (p + strlen(s) > buffer + (sizeof buffer) - 2) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 749 | com_error(c, PyExc_MemoryError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 750 | "dotted_name too long"); |
Guido van Rossum | d9dfaf5 | 1995-02-17 15:04:57 +0000 | [diff] [blame] | 751 | name = NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 752 | break; |
| 753 | } |
| 754 | if (p != buffer) |
| 755 | *p++ = '.'; |
| 756 | strcpy(p, s); |
| 757 | p = strchr(p, '\0'); |
| 758 | } |
| 759 | } |
| 760 | else { |
| 761 | REQ(n, NAME); |
| 762 | name = STR(n); |
| 763 | } |
| 764 | com_addopnamestr(c, op, name); |
| 765 | } |
| 766 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 767 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 768 | parsenumber(struct compiling *co, char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 769 | { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 770 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 771 | long x; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 772 | double dx; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 773 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 774 | Py_complex c; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 775 | int imflag; |
| 776 | #endif |
| 777 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 778 | errno = 0; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 779 | end = s + strlen(s) - 1; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 780 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 15ad9a6 | 1996-01-26 20:53:56 +0000 | [diff] [blame] | 781 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 782 | #endif |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 783 | if (*end == 'l' || *end == 'L') |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 784 | return PyLong_FromString(s, (char **)0, 0); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 785 | if (s[0] == '0') |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 786 | x = (long) PyOS_strtoul(s, &end, 0); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 787 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 788 | x = PyOS_strtol(s, &end, 0); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 789 | if (*end == '\0') { |
| 790 | if (errno != 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 791 | com_error(co, PyExc_OverflowError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 792 | "integer literal too large"); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 793 | return NULL; |
| 794 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 795 | return PyInt_FromLong(x); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 796 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 797 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 798 | #ifndef WITHOUT_COMPLEX |
| 799 | if (imflag) { |
| 800 | c.real = 0.; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 801 | PyFPE_START_PROTECT("atof", return 0) |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 802 | c.imag = atof(s); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 803 | PyFPE_END_PROTECT(c) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 804 | return PyComplex_FromCComplex(c); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 805 | } |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 806 | else |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 807 | #endif |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 808 | { |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 809 | PyFPE_START_PROTECT("atof", return 0) |
| 810 | dx = atof(s); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 811 | PyFPE_END_PROTECT(dx) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 812 | return PyFloat_FromDouble(dx); |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 813 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 816 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 817 | parsestr(char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 818 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 819 | PyObject *v; |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 820 | size_t len; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 821 | char *buf; |
| 822 | char *p; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 823 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 824 | int c; |
Guido van Rossum | 5f5e817 | 1997-04-06 03:41:40 +0000 | [diff] [blame] | 825 | int first = *s; |
| 826 | int quote = first; |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 827 | int rawmode = 0; |
| 828 | int unicode = 0; |
| 829 | if (isalpha(quote) || quote == '_') { |
| 830 | if (quote == 'u' || quote == 'U') { |
| 831 | quote = *++s; |
| 832 | unicode = 1; |
| 833 | } |
| 834 | if (quote == 'r' || quote == 'R') { |
| 835 | quote = *++s; |
| 836 | rawmode = 1; |
| 837 | } |
| 838 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 839 | if (quote != '\'' && quote != '\"') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 840 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 841 | return NULL; |
| 842 | } |
| 843 | s++; |
| 844 | len = strlen(s); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 845 | if (len > INT_MAX) { |
| 846 | PyErr_SetString(PyExc_OverflowError, "string to parse is too long"); |
| 847 | return NULL; |
| 848 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 849 | if (s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 850 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 851 | return NULL; |
| 852 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 853 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
| 854 | s += 2; |
| 855 | len -= 2; |
| 856 | if (s[--len] != quote || s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 857 | PyErr_BadInternalCall(); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 858 | return NULL; |
| 859 | } |
| 860 | } |
Guido van Rossum | fdc8bdb | 2000-05-01 17:54:56 +0000 | [diff] [blame] | 861 | if (unicode || Py_UnicodeFlag) { |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 862 | if (rawmode) |
| 863 | return PyUnicode_DecodeRawUnicodeEscape( |
| 864 | s, len, NULL); |
| 865 | else |
| 866 | return PyUnicode_DecodeUnicodeEscape( |
| 867 | s, len, NULL); |
| 868 | } |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 869 | if (rawmode || strchr(s, '\\') == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 870 | return PyString_FromStringAndSize(s, len); |
| 871 | v = PyString_FromStringAndSize((char *)NULL, len); |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 872 | if (v == NULL) |
| 873 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 874 | p = buf = PyString_AsString(v); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 875 | end = s + len; |
| 876 | while (s < end) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 877 | if (*s != '\\') { |
| 878 | *p++ = *s++; |
| 879 | continue; |
| 880 | } |
| 881 | s++; |
| 882 | switch (*s++) { |
| 883 | /* XXX This assumes ASCII! */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 884 | case '\n': break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 885 | case '\\': *p++ = '\\'; break; |
| 886 | case '\'': *p++ = '\''; break; |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 887 | case '\"': *p++ = '\"'; break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 888 | case 'b': *p++ = '\b'; break; |
| 889 | case 'f': *p++ = '\014'; break; /* FF */ |
| 890 | case 't': *p++ = '\t'; break; |
| 891 | case 'n': *p++ = '\n'; break; |
| 892 | case 'r': *p++ = '\r'; break; |
| 893 | case 'v': *p++ = '\013'; break; /* VT */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 894 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 895 | case '0': case '1': case '2': case '3': |
| 896 | case '4': case '5': case '6': case '7': |
| 897 | c = s[-1] - '0'; |
| 898 | if ('0' <= *s && *s <= '7') { |
| 899 | c = (c<<3) + *s++ - '0'; |
| 900 | if ('0' <= *s && *s <= '7') |
| 901 | c = (c<<3) + *s++ - '0'; |
| 902 | } |
| 903 | *p++ = c; |
| 904 | break; |
| 905 | case 'x': |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 906 | if (isxdigit(Py_CHARMASK(s[0])) && isxdigit(Py_CHARMASK(s[1]))) { |
Guido van Rossum | ed1100f | 1997-10-20 23:24:07 +0000 | [diff] [blame] | 907 | unsigned int x = 0; |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 908 | c = Py_CHARMASK(*s); |
| 909 | s++; |
| 910 | if (isdigit(c)) |
| 911 | x = c - '0'; |
| 912 | else if (islower(c)) |
| 913 | x = 10 + c - 'a'; |
| 914 | else |
| 915 | x = 10 + c - 'A'; |
| 916 | x = x << 4; |
| 917 | c = Py_CHARMASK(*s); |
| 918 | s++; |
| 919 | if (isdigit(c)) |
| 920 | x += c - '0'; |
| 921 | else if (islower(c)) |
| 922 | x += 10 + c - 'a'; |
| 923 | else |
| 924 | x += 10 + c - 'A'; |
Guido van Rossum | ed1100f | 1997-10-20 23:24:07 +0000 | [diff] [blame] | 925 | *p++ = x; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 926 | break; |
| 927 | } |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 928 | PyErr_SetString(PyExc_ValueError, "invalid \\x escape"); |
| 929 | Py_DECREF(v); |
| 930 | return NULL; |
| 931 | default: |
| 932 | *p++ = '\\'; |
| 933 | *p++ = s[-1]; |
| 934 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 935 | } |
| 936 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 937 | _PyString_Resize(&v, (int)(p - buf)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 938 | return v; |
| 939 | } |
| 940 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 941 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 942 | parsestrplus(node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 943 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 944 | PyObject *v; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 945 | int i; |
| 946 | REQ(CHILD(n, 0), STRING); |
| 947 | if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) { |
| 948 | /* String literal concatenation */ |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 949 | for (i = 1; i < NCH(n); i++) { |
| 950 | PyObject *s; |
| 951 | s = parsestr(STR(CHILD(n, i))); |
| 952 | if (s == NULL) |
| 953 | goto onError; |
| 954 | if (PyString_Check(v) && PyString_Check(s)) { |
| 955 | PyString_ConcatAndDel(&v, s); |
| 956 | if (v == NULL) |
| 957 | goto onError; |
| 958 | } |
| 959 | else { |
| 960 | PyObject *temp; |
| 961 | temp = PyUnicode_Concat(v, s); |
| 962 | Py_DECREF(s); |
| 963 | if (temp == NULL) |
| 964 | goto onError; |
| 965 | Py_DECREF(v); |
| 966 | v = temp; |
| 967 | } |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 968 | } |
| 969 | } |
| 970 | return v; |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 971 | |
| 972 | onError: |
| 973 | Py_XDECREF(v); |
| 974 | return NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 977 | static void |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 978 | 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] | 979 | { |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 980 | PyObject *v; |
| 981 | int anchor = 0; |
| 982 | int save_begin = c->c_begin; |
| 983 | |
| 984 | /* list_iter: for v in expr [list_iter] */ |
| 985 | com_node(c, CHILD(n, 3)); /* expr */ |
| 986 | v = PyInt_FromLong(0L); |
| 987 | if (v == NULL) |
| 988 | c->c_errors++; |
| 989 | com_addoparg(c, LOAD_CONST, com_addconst(c, v)); |
| 990 | com_push(c, 1); |
| 991 | Py_XDECREF(v); |
| 992 | c->c_begin = c->c_nexti; |
| 993 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 994 | com_addfwref(c, FOR_LOOP, &anchor); |
| 995 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 996 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 997 | c->c_loops++; |
| 998 | com_list_iter(c, n, e, t); |
| 999 | c->c_loops--; |
| 1000 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 1001 | c->c_begin = save_begin; |
| 1002 | com_backpatch(c, anchor); |
| 1003 | com_pop(c, 2); /* FOR_LOOP has popped these */ |
| 1004 | } |
| 1005 | |
| 1006 | static void |
| 1007 | com_list_if(struct compiling *c, node *n, node *e, char *t) |
| 1008 | { |
| 1009 | int anchor = 0; |
| 1010 | int a = 0; |
| 1011 | /* list_iter: 'if' test [list_iter] */ |
| 1012 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 1013 | com_node(c, CHILD(n, 1)); |
| 1014 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 1015 | com_addbyte(c, POP_TOP); |
| 1016 | com_pop(c, 1); |
| 1017 | com_list_iter(c, n, e, t); |
| 1018 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 1019 | com_backpatch(c, a); |
| 1020 | /* We jump here with an extra entry which we now pop */ |
| 1021 | com_addbyte(c, POP_TOP); |
| 1022 | com_backpatch(c, anchor); |
| 1023 | } |
| 1024 | |
| 1025 | static void |
| 1026 | com_list_iter(struct compiling *c, |
| 1027 | node *p, /* parent of list_iter node */ |
| 1028 | node *e, /* element expression node */ |
| 1029 | char *t /* name of result list temp local */) |
| 1030 | { |
| 1031 | /* list_iter is the last child in a listmaker, list_for, or list_if */ |
| 1032 | node *n = CHILD(p, NCH(p)-1); |
| 1033 | if (TYPE(n) == list_iter) { |
| 1034 | n = CHILD(n, 0); |
| 1035 | switch (TYPE(n)) { |
| 1036 | case list_for: |
| 1037 | com_list_for(c, n, e, t); |
| 1038 | break; |
| 1039 | case list_if: |
| 1040 | com_list_if(c, n, e, t); |
| 1041 | break; |
| 1042 | default: |
| 1043 | com_error(c, PyExc_SystemError, |
| 1044 | "invalid list_iter node type"); |
| 1045 | } |
| 1046 | } |
| 1047 | else { |
| 1048 | com_addopnamestr(c, LOAD_NAME, t); |
| 1049 | com_push(c, 1); |
| 1050 | com_node(c, e); |
| 1051 | com_addoparg(c, CALL_FUNCTION, 1); |
| 1052 | com_addbyte(c, POP_TOP); |
| 1053 | com_pop(c, 2); |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | static void |
| 1058 | com_list_comprehension(struct compiling *c, node *n) |
| 1059 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1060 | /* listmaker: test list_for */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1061 | char tmpname[12]; |
| 1062 | sprintf(tmpname, "__%d__", ++c->c_tmpname); |
| 1063 | com_addoparg(c, BUILD_LIST, 0); |
| 1064 | com_addbyte(c, DUP_TOP); /* leave the result on the stack */ |
| 1065 | com_push(c, 2); |
| 1066 | com_addopnamestr(c, LOAD_ATTR, "append"); |
| 1067 | com_addopnamestr(c, STORE_NAME, tmpname); |
| 1068 | com_pop(c, 1); |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1069 | com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1070 | com_addopnamestr(c, DELETE_NAME, tmpname); |
| 1071 | --c->c_tmpname; |
| 1072 | } |
| 1073 | |
| 1074 | static void |
| 1075 | com_listmaker(struct compiling *c, node *n) |
| 1076 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1077 | /* listmaker: test ( list_for | (',' test)* [','] ) */ |
| 1078 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1079 | com_list_comprehension(c, n); |
| 1080 | else { |
| 1081 | int len = 0; |
| 1082 | int i; |
| 1083 | for (i = 0; i < NCH(n); i += 2, len++) |
| 1084 | com_node(c, CHILD(n, i)); |
| 1085 | com_addoparg(c, BUILD_LIST, len); |
| 1086 | com_pop(c, len-1); |
| 1087 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1091 | com_dictmaker(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1092 | { |
| 1093 | int i; |
| 1094 | /* dictmaker: test ':' test (',' test ':' value)* [','] */ |
| 1095 | for (i = 0; i+2 < NCH(n); i += 4) { |
| 1096 | /* We must arrange things just right for STORE_SUBSCR. |
| 1097 | It wants the stack to look like (value) (dict) (key) */ |
| 1098 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1099 | com_push(c, 1); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1100 | com_node(c, CHILD(n, i+2)); /* value */ |
| 1101 | com_addbyte(c, ROT_TWO); |
| 1102 | com_node(c, CHILD(n, i)); /* key */ |
| 1103 | com_addbyte(c, STORE_SUBSCR); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1104 | com_pop(c, 3); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1109 | com_atom(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1110 | { |
| 1111 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1112 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1113 | int i; |
| 1114 | REQ(n, atom); |
| 1115 | ch = CHILD(n, 0); |
| 1116 | switch (TYPE(ch)) { |
| 1117 | case LPAR: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1118 | if (TYPE(CHILD(n, 1)) == RPAR) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1119 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1120 | com_push(c, 1); |
| 1121 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1122 | else |
| 1123 | com_node(c, CHILD(n, 1)); |
| 1124 | break; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1125 | case LSQB: /* '[' [listmaker] ']' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1126 | if (TYPE(CHILD(n, 1)) == RSQB) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1127 | com_addoparg(c, BUILD_LIST, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1128 | com_push(c, 1); |
| 1129 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1130 | else |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1131 | com_listmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1132 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1133 | case LBRACE: /* '{' [dictmaker] '}' */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1134 | com_addoparg(c, BUILD_MAP, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1135 | com_push(c, 1); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1136 | if (TYPE(CHILD(n, 1)) == dictmaker) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1137 | com_dictmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1138 | break; |
| 1139 | case BACKQUOTE: |
| 1140 | com_node(c, CHILD(n, 1)); |
| 1141 | com_addbyte(c, UNARY_CONVERT); |
| 1142 | break; |
| 1143 | case NUMBER: |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1144 | if ((v = parsenumber(c, STR(ch))) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1145 | i = 255; |
| 1146 | } |
| 1147 | else { |
| 1148 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1149 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1150 | } |
| 1151 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1152 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1153 | break; |
| 1154 | case STRING: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1155 | v = parsestrplus(n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1156 | if (v == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1157 | c->c_errors++; |
| 1158 | i = 255; |
| 1159 | } |
| 1160 | else { |
| 1161 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1162 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1163 | } |
| 1164 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1165 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1166 | break; |
| 1167 | case NAME: |
| 1168 | com_addopname(c, LOAD_NAME, ch); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1169 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1170 | break; |
| 1171 | default: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1172 | /* XXX fprintf(stderr, "node type %d\n", TYPE(ch)); */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1173 | com_error(c, PyExc_SystemError, |
| 1174 | "com_atom: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1179 | com_slice(struct compiling *c, node *n, int op) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1180 | { |
| 1181 | if (NCH(n) == 1) { |
| 1182 | com_addbyte(c, op); |
| 1183 | } |
| 1184 | else if (NCH(n) == 2) { |
| 1185 | if (TYPE(CHILD(n, 0)) != COLON) { |
| 1186 | com_node(c, CHILD(n, 0)); |
| 1187 | com_addbyte(c, op+1); |
| 1188 | } |
| 1189 | else { |
| 1190 | com_node(c, CHILD(n, 1)); |
| 1191 | com_addbyte(c, op+2); |
| 1192 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1193 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1194 | } |
| 1195 | else { |
| 1196 | com_node(c, CHILD(n, 0)); |
| 1197 | com_node(c, CHILD(n, 2)); |
| 1198 | com_addbyte(c, op+3); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1199 | com_pop(c, 2); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1200 | } |
| 1201 | } |
| 1202 | |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1203 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1204 | com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn) |
| 1205 | { |
| 1206 | if (NCH(n) == 1) { |
| 1207 | com_addbyte(c, DUP_TOP); |
| 1208 | com_push(c, 1); |
| 1209 | com_addbyte(c, SLICE); |
| 1210 | com_node(c, augn); |
| 1211 | com_addbyte(c, opcode); |
| 1212 | com_pop(c, 1); |
| 1213 | com_addbyte(c, ROT_TWO); |
| 1214 | com_addbyte(c, STORE_SLICE); |
| 1215 | com_pop(c, 2); |
| 1216 | } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) { |
| 1217 | com_node(c, CHILD(n, 0)); |
| 1218 | com_addoparg(c, DUP_TOPX, 2); |
| 1219 | com_push(c, 2); |
| 1220 | com_addbyte(c, SLICE+1); |
| 1221 | com_pop(c, 1); |
| 1222 | com_node(c, augn); |
| 1223 | com_addbyte(c, opcode); |
| 1224 | com_pop(c, 1); |
| 1225 | com_addbyte(c, ROT_THREE); |
| 1226 | com_addbyte(c, STORE_SLICE+1); |
| 1227 | com_pop(c, 3); |
| 1228 | } else if (NCH(n) == 2) { |
| 1229 | com_node(c, CHILD(n, 1)); |
| 1230 | com_addoparg(c, DUP_TOPX, 2); |
| 1231 | com_push(c, 2); |
| 1232 | com_addbyte(c, SLICE+2); |
| 1233 | com_pop(c, 1); |
| 1234 | com_node(c, augn); |
| 1235 | com_addbyte(c, opcode); |
| 1236 | com_pop(c, 1); |
| 1237 | com_addbyte(c, ROT_THREE); |
| 1238 | com_addbyte(c, STORE_SLICE+2); |
| 1239 | com_pop(c, 3); |
| 1240 | } else { |
| 1241 | com_node(c, CHILD(n, 0)); |
| 1242 | com_node(c, CHILD(n, 2)); |
| 1243 | com_addoparg(c, DUP_TOPX, 3); |
| 1244 | com_push(c, 3); |
| 1245 | com_addbyte(c, SLICE+3); |
| 1246 | com_pop(c, 2); |
| 1247 | com_node(c, augn); |
| 1248 | com_addbyte(c, opcode); |
| 1249 | com_pop(c, 1); |
| 1250 | com_addbyte(c, ROT_FOUR); |
| 1251 | com_addbyte(c, STORE_SLICE+3); |
| 1252 | com_pop(c, 4); |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1257 | com_argument(struct compiling *c, node *n, PyObject **pkeywords) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1258 | { |
| 1259 | node *m; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1260 | REQ(n, argument); /* [test '='] test; really [keyword '='] test */ |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1261 | if (NCH(n) == 1) { |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1262 | if (*pkeywords != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1263 | com_error(c, PyExc_SyntaxError, |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 1264 | "non-keyword arg after keyword arg"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1265 | } |
| 1266 | else { |
| 1267 | com_node(c, CHILD(n, 0)); |
| 1268 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1269 | return; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1270 | } |
| 1271 | m = n; |
| 1272 | do { |
| 1273 | m = CHILD(m, 0); |
| 1274 | } while (NCH(m) == 1); |
| 1275 | if (TYPE(m) != NAME) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1276 | com_error(c, PyExc_SyntaxError, |
| 1277 | "keyword can't be an expression"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1278 | } |
| 1279 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1280 | PyObject *v = PyString_InternFromString(STR(m)); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1281 | if (v != NULL && *pkeywords == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1282 | *pkeywords = PyDict_New(); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1283 | if (v == NULL || *pkeywords == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1284 | c->c_errors++; |
| 1285 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1286 | if (PyDict_GetItem(*pkeywords, v) != NULL) |
| 1287 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1288 | "duplicate keyword argument"); |
| 1289 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1290 | if (PyDict_SetItem(*pkeywords, v, v) != 0) |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1291 | c->c_errors++; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1292 | com_addoparg(c, LOAD_CONST, com_addconst(c, v)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1293 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1294 | Py_DECREF(v); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1295 | } |
| 1296 | } |
| 1297 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1300 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1301 | com_call_function(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1302 | { |
| 1303 | if (TYPE(n) == RPAR) { |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1304 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1305 | } |
| 1306 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1307 | PyObject *keywords = NULL; |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1308 | int i, na, nk; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1309 | int lineno = n->n_lineno; |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1310 | int star_flag = 0; |
| 1311 | int starstar_flag = 0; |
| 1312 | int opcode; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1313 | REQ(n, arglist); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1314 | na = 0; |
| 1315 | nk = 0; |
| 1316 | for (i = 0; i < NCH(n); i += 2) { |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1317 | node *ch = CHILD(n, i); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1318 | if (TYPE(ch) == STAR || |
| 1319 | TYPE(ch) == DOUBLESTAR) |
| 1320 | break; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1321 | if (ch->n_lineno != lineno) { |
| 1322 | lineno = ch->n_lineno; |
| 1323 | com_addoparg(c, SET_LINENO, lineno); |
| 1324 | } |
| 1325 | com_argument(c, ch, &keywords); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1326 | if (keywords == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1327 | na++; |
| 1328 | else |
| 1329 | nk++; |
| 1330 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1331 | Py_XDECREF(keywords); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1332 | while (i < NCH(n)) { |
| 1333 | node *tok = CHILD(n, i); |
| 1334 | node *ch = CHILD(n, i+1); |
| 1335 | i += 3; |
| 1336 | switch (TYPE(tok)) { |
| 1337 | case STAR: star_flag = 1; break; |
| 1338 | case DOUBLESTAR: starstar_flag = 1; break; |
| 1339 | } |
| 1340 | com_node(c, ch); |
| 1341 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1342 | if (na > 255 || nk > 255) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1343 | com_error(c, PyExc_SyntaxError, |
| 1344 | "more than 255 arguments"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1345 | } |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1346 | if (star_flag || starstar_flag) |
Jeremy Hylton | e4fb958 | 2000-03-29 00:10:44 +0000 | [diff] [blame] | 1347 | opcode = CALL_FUNCTION_VAR - 1 + |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1348 | star_flag + (starstar_flag << 1); |
| 1349 | else |
| 1350 | opcode = CALL_FUNCTION; |
| 1351 | com_addoparg(c, opcode, na | (nk << 8)); |
| 1352 | com_pop(c, na + 2*nk + star_flag + starstar_flag); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1357 | com_select_member(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1358 | { |
| 1359 | com_addopname(c, LOAD_ATTR, n); |
| 1360 | } |
| 1361 | |
| 1362 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1363 | com_sliceobj(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1364 | { |
| 1365 | int i=0; |
| 1366 | int ns=2; /* number of slice arguments */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1367 | node *ch; |
| 1368 | |
| 1369 | /* first argument */ |
| 1370 | if (TYPE(CHILD(n,i)) == COLON) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1371 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1372 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1373 | i++; |
| 1374 | } |
| 1375 | else { |
| 1376 | com_node(c, CHILD(n,i)); |
| 1377 | i++; |
| 1378 | REQ(CHILD(n,i),COLON); |
| 1379 | i++; |
| 1380 | } |
| 1381 | /* second argument */ |
| 1382 | if (i < NCH(n) && TYPE(CHILD(n,i)) == test) { |
| 1383 | com_node(c, CHILD(n,i)); |
| 1384 | i++; |
| 1385 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1386 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1387 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1388 | com_push(c, 1); |
| 1389 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1390 | /* remaining arguments */ |
| 1391 | for (; i < NCH(n); i++) { |
| 1392 | ns++; |
| 1393 | ch=CHILD(n,i); |
| 1394 | REQ(ch, sliceop); |
| 1395 | if (NCH(ch) == 1) { |
| 1396 | /* right argument of ':' missing */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1397 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1398 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1399 | } |
| 1400 | else |
| 1401 | com_node(c, CHILD(ch,1)); |
| 1402 | } |
| 1403 | com_addoparg(c, BUILD_SLICE, ns); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1404 | com_pop(c, 1 + (ns == 3)); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1408 | com_subscript(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1409 | { |
| 1410 | node *ch; |
| 1411 | REQ(n, subscript); |
| 1412 | ch = CHILD(n,0); |
| 1413 | /* check for rubber index */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1414 | if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) { |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 1415 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1416 | com_push(c, 1); |
| 1417 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1418 | else { |
| 1419 | /* check for slice */ |
| 1420 | if ((TYPE(ch) == COLON || NCH(n) > 1)) |
| 1421 | com_sliceobj(c, n); |
| 1422 | else { |
| 1423 | REQ(ch, test); |
| 1424 | com_node(c, ch); |
| 1425 | } |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1430 | com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1431 | { |
| 1432 | int i, op; |
| 1433 | REQ(n, subscriptlist); |
| 1434 | /* Check to make backward compatible slice behavior for '[i:j]' */ |
| 1435 | if (NCH(n) == 1) { |
| 1436 | node *sub = CHILD(n, 0); /* subscript */ |
Thomas Wouters | e8643c4 | 2000-08-05 21:37:50 +0000 | [diff] [blame] | 1437 | /* 'Basic' slice, should have exactly one colon. */ |
| 1438 | if ((TYPE(CHILD(sub, 0)) == COLON |
| 1439 | || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON)) |
| 1440 | && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop)) |
| 1441 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1442 | switch (assigning) { |
| 1443 | case OP_DELETE: |
| 1444 | op = DELETE_SLICE; |
| 1445 | break; |
| 1446 | case OP_ASSIGN: |
| 1447 | op = STORE_SLICE; |
| 1448 | break; |
| 1449 | case OP_APPLY: |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1450 | op = SLICE; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1451 | break; |
| 1452 | default: |
| 1453 | com_augassign_slice(c, sub, assigning, augn); |
| 1454 | return; |
| 1455 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1456 | com_slice(c, sub, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1457 | if (op == STORE_SLICE) |
| 1458 | com_pop(c, 2); |
| 1459 | else if (op == DELETE_SLICE) |
| 1460 | com_pop(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1461 | return; |
| 1462 | } |
| 1463 | } |
| 1464 | /* Else normal subscriptlist. Compile each subscript. */ |
| 1465 | for (i = 0; i < NCH(n); i += 2) |
| 1466 | com_subscript(c, CHILD(n, i)); |
| 1467 | /* Put multiple subscripts into a tuple */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1468 | if (NCH(n) > 1) { |
| 1469 | i = (NCH(n)+1) / 2; |
| 1470 | com_addoparg(c, BUILD_TUPLE, i); |
| 1471 | com_pop(c, i-1); |
| 1472 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1473 | switch (assigning) { |
| 1474 | case OP_DELETE: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1475 | op = DELETE_SUBSCR; |
| 1476 | i = 2; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1477 | break; |
| 1478 | default: |
| 1479 | case OP_ASSIGN: |
| 1480 | op = STORE_SUBSCR; |
| 1481 | i = 3; |
| 1482 | break; |
| 1483 | case OP_APPLY: |
| 1484 | op = BINARY_SUBSCR; |
| 1485 | i = 1; |
| 1486 | break; |
| 1487 | } |
| 1488 | if (assigning > OP_APPLY) { |
| 1489 | com_addoparg(c, DUP_TOPX, 2); |
| 1490 | com_push(c, 2); |
| 1491 | com_addbyte(c, BINARY_SUBSCR); |
| 1492 | com_pop(c, 1); |
| 1493 | com_node(c, augn); |
| 1494 | com_addbyte(c, assigning); |
| 1495 | com_pop(c, 1); |
| 1496 | com_addbyte(c, ROT_THREE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1497 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1498 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1499 | com_pop(c, i); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1503 | com_apply_trailer(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1504 | { |
| 1505 | REQ(n, trailer); |
| 1506 | switch (TYPE(CHILD(n, 0))) { |
| 1507 | case LPAR: |
| 1508 | com_call_function(c, CHILD(n, 1)); |
| 1509 | break; |
| 1510 | case DOT: |
| 1511 | com_select_member(c, CHILD(n, 1)); |
| 1512 | break; |
| 1513 | case LSQB: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1514 | com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1515 | break; |
| 1516 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1517 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1518 | "com_apply_trailer: unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1519 | } |
| 1520 | } |
| 1521 | |
| 1522 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1523 | com_power(struct compiling *c, node *n) |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1524 | { |
| 1525 | int i; |
| 1526 | REQ(n, power); |
| 1527 | com_atom(c, CHILD(n, 0)); |
| 1528 | for (i = 1; i < NCH(n); i++) { |
| 1529 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
| 1530 | com_factor(c, CHILD(n, i+1)); |
| 1531 | com_addbyte(c, BINARY_POWER); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1532 | com_pop(c, 1); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1533 | break; |
| 1534 | } |
| 1535 | else |
| 1536 | com_apply_trailer(c, CHILD(n, i)); |
| 1537 | } |
| 1538 | } |
| 1539 | |
| 1540 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1541 | com_factor(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1542 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1543 | REQ(n, factor); |
| 1544 | if (TYPE(CHILD(n, 0)) == PLUS) { |
| 1545 | com_factor(c, CHILD(n, 1)); |
| 1546 | com_addbyte(c, UNARY_POSITIVE); |
| 1547 | } |
| 1548 | else if (TYPE(CHILD(n, 0)) == MINUS) { |
| 1549 | com_factor(c, CHILD(n, 1)); |
| 1550 | com_addbyte(c, UNARY_NEGATIVE); |
| 1551 | } |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1552 | else if (TYPE(CHILD(n, 0)) == TILDE) { |
| 1553 | com_factor(c, CHILD(n, 1)); |
| 1554 | com_addbyte(c, UNARY_INVERT); |
| 1555 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1556 | else { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1557 | com_power(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1562 | com_term(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1563 | { |
| 1564 | int i; |
| 1565 | int op; |
| 1566 | REQ(n, term); |
| 1567 | com_factor(c, CHILD(n, 0)); |
| 1568 | for (i = 2; i < NCH(n); i += 2) { |
| 1569 | com_factor(c, CHILD(n, i)); |
| 1570 | switch (TYPE(CHILD(n, i-1))) { |
| 1571 | case STAR: |
| 1572 | op = BINARY_MULTIPLY; |
| 1573 | break; |
| 1574 | case SLASH: |
| 1575 | op = BINARY_DIVIDE; |
| 1576 | break; |
| 1577 | case PERCENT: |
| 1578 | op = BINARY_MODULO; |
| 1579 | break; |
| 1580 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1581 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1582 | "com_term: operator not *, / or %"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1583 | op = 255; |
| 1584 | } |
| 1585 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1586 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1591 | com_arith_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1592 | { |
| 1593 | int i; |
| 1594 | int op; |
| 1595 | REQ(n, arith_expr); |
| 1596 | com_term(c, CHILD(n, 0)); |
| 1597 | for (i = 2; i < NCH(n); i += 2) { |
| 1598 | com_term(c, CHILD(n, i)); |
| 1599 | switch (TYPE(CHILD(n, i-1))) { |
| 1600 | case PLUS: |
| 1601 | op = BINARY_ADD; |
| 1602 | break; |
| 1603 | case MINUS: |
| 1604 | op = BINARY_SUBTRACT; |
| 1605 | break; |
| 1606 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1607 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1608 | "com_arith_expr: operator not + or -"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1609 | op = 255; |
| 1610 | } |
| 1611 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1612 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1617 | com_shift_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1618 | { |
| 1619 | int i; |
| 1620 | int op; |
| 1621 | REQ(n, shift_expr); |
| 1622 | com_arith_expr(c, CHILD(n, 0)); |
| 1623 | for (i = 2; i < NCH(n); i += 2) { |
| 1624 | com_arith_expr(c, CHILD(n, i)); |
| 1625 | switch (TYPE(CHILD(n, i-1))) { |
| 1626 | case LEFTSHIFT: |
| 1627 | op = BINARY_LSHIFT; |
| 1628 | break; |
| 1629 | case RIGHTSHIFT: |
| 1630 | op = BINARY_RSHIFT; |
| 1631 | break; |
| 1632 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1633 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1634 | "com_shift_expr: operator not << or >>"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1635 | op = 255; |
| 1636 | } |
| 1637 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1638 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1643 | com_and_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1644 | { |
| 1645 | int i; |
| 1646 | int op; |
| 1647 | REQ(n, and_expr); |
| 1648 | com_shift_expr(c, CHILD(n, 0)); |
| 1649 | for (i = 2; i < NCH(n); i += 2) { |
| 1650 | com_shift_expr(c, CHILD(n, i)); |
| 1651 | if (TYPE(CHILD(n, i-1)) == AMPER) { |
| 1652 | op = BINARY_AND; |
| 1653 | } |
| 1654 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1655 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1656 | "com_and_expr: operator not &"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1657 | op = 255; |
| 1658 | } |
| 1659 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1660 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1665 | com_xor_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1666 | { |
| 1667 | int i; |
| 1668 | int op; |
| 1669 | REQ(n, xor_expr); |
| 1670 | com_and_expr(c, CHILD(n, 0)); |
| 1671 | for (i = 2; i < NCH(n); i += 2) { |
| 1672 | com_and_expr(c, CHILD(n, i)); |
| 1673 | if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) { |
| 1674 | op = BINARY_XOR; |
| 1675 | } |
| 1676 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1677 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1678 | "com_xor_expr: operator not ^"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1679 | op = 255; |
| 1680 | } |
| 1681 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1682 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1687 | com_expr(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1688 | { |
| 1689 | int i; |
| 1690 | int op; |
| 1691 | REQ(n, expr); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1692 | com_xor_expr(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1693 | for (i = 2; i < NCH(n); i += 2) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1694 | com_xor_expr(c, CHILD(n, i)); |
| 1695 | if (TYPE(CHILD(n, i-1)) == VBAR) { |
| 1696 | op = BINARY_OR; |
| 1697 | } |
| 1698 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1699 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1700 | "com_expr: expr operator not |"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1701 | op = 255; |
| 1702 | } |
| 1703 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1704 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | static enum cmp_op |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1709 | cmp_type(node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1710 | { |
| 1711 | REQ(n, comp_op); |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 1712 | /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '==' |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1713 | | 'in' | 'not' 'in' | 'is' | 'is' not' */ |
| 1714 | if (NCH(n) == 1) { |
| 1715 | n = CHILD(n, 0); |
| 1716 | switch (TYPE(n)) { |
| 1717 | case LESS: return LT; |
| 1718 | case GREATER: return GT; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 1719 | case EQEQUAL: /* == */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1720 | case EQUAL: return EQ; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 1721 | case LESSEQUAL: return LE; |
| 1722 | case GREATEREQUAL: return GE; |
| 1723 | case NOTEQUAL: return NE; /* <> or != */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1724 | case NAME: if (strcmp(STR(n), "in") == 0) return IN; |
| 1725 | if (strcmp(STR(n), "is") == 0) return IS; |
| 1726 | } |
| 1727 | } |
| 1728 | else if (NCH(n) == 2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1729 | switch (TYPE(CHILD(n, 0))) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1730 | case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
| 1731 | return NOT_IN; |
| 1732 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
| 1733 | return IS_NOT; |
| 1734 | } |
| 1735 | } |
| 1736 | return BAD; |
| 1737 | } |
| 1738 | |
| 1739 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1740 | com_comparison(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1741 | { |
| 1742 | int i; |
| 1743 | enum cmp_op op; |
| 1744 | int anchor; |
| 1745 | REQ(n, comparison); /* comparison: expr (comp_op expr)* */ |
| 1746 | com_expr(c, CHILD(n, 0)); |
| 1747 | if (NCH(n) == 1) |
| 1748 | return; |
| 1749 | |
| 1750 | /**************************************************************** |
| 1751 | The following code is generated for all but the last |
| 1752 | comparison in a chain: |
| 1753 | |
| 1754 | label: on stack: opcode: jump to: |
| 1755 | |
| 1756 | a <code to load b> |
| 1757 | a, b DUP_TOP |
| 1758 | a, b, b ROT_THREE |
| 1759 | b, a, b COMPARE_OP |
| 1760 | b, 0-or-1 JUMP_IF_FALSE L1 |
| 1761 | b, 1 POP_TOP |
| 1762 | b |
| 1763 | |
| 1764 | We are now ready to repeat this sequence for the next |
| 1765 | comparison in the chain. |
| 1766 | |
| 1767 | For the last we generate: |
| 1768 | |
| 1769 | b <code to load c> |
| 1770 | b, c COMPARE_OP |
| 1771 | 0-or-1 |
| 1772 | |
| 1773 | If there were any jumps to L1 (i.e., there was more than one |
| 1774 | comparison), we generate: |
| 1775 | |
| 1776 | 0-or-1 JUMP_FORWARD L2 |
| 1777 | L1: b, 0 ROT_TWO |
| 1778 | 0, b POP_TOP |
| 1779 | 0 |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1780 | L2: 0-or-1 |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1781 | ****************************************************************/ |
| 1782 | |
| 1783 | anchor = 0; |
| 1784 | |
| 1785 | for (i = 2; i < NCH(n); i += 2) { |
| 1786 | com_expr(c, CHILD(n, i)); |
| 1787 | if (i+2 < NCH(n)) { |
| 1788 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1789 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1790 | com_addbyte(c, ROT_THREE); |
| 1791 | } |
| 1792 | op = cmp_type(CHILD(n, i-1)); |
| 1793 | if (op == BAD) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1794 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1795 | "com_comparison: unknown comparison op"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1796 | } |
| 1797 | com_addoparg(c, COMPARE_OP, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1798 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1799 | if (i+2 < NCH(n)) { |
| 1800 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 1801 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1802 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1803 | } |
| 1804 | } |
| 1805 | |
| 1806 | if (anchor) { |
| 1807 | int anchor2 = 0; |
| 1808 | com_addfwref(c, JUMP_FORWARD, &anchor2); |
| 1809 | com_backpatch(c, anchor); |
| 1810 | com_addbyte(c, ROT_TWO); |
| 1811 | com_addbyte(c, POP_TOP); |
| 1812 | com_backpatch(c, anchor2); |
| 1813 | } |
| 1814 | } |
| 1815 | |
| 1816 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1817 | com_not_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1818 | { |
| 1819 | REQ(n, not_test); /* 'not' not_test | comparison */ |
| 1820 | if (NCH(n) == 1) { |
| 1821 | com_comparison(c, CHILD(n, 0)); |
| 1822 | } |
| 1823 | else { |
| 1824 | com_not_test(c, CHILD(n, 1)); |
| 1825 | com_addbyte(c, UNARY_NOT); |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1830 | com_and_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1831 | { |
| 1832 | int i; |
| 1833 | int anchor; |
| 1834 | REQ(n, and_test); /* not_test ('and' not_test)* */ |
| 1835 | anchor = 0; |
| 1836 | i = 0; |
| 1837 | for (;;) { |
| 1838 | com_not_test(c, CHILD(n, i)); |
| 1839 | if ((i += 2) >= NCH(n)) |
| 1840 | break; |
| 1841 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 1842 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1843 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1844 | } |
| 1845 | if (anchor) |
| 1846 | com_backpatch(c, anchor); |
| 1847 | } |
| 1848 | |
| 1849 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1850 | com_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1851 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1852 | REQ(n, test); /* and_test ('or' and_test)* | lambdef */ |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 1853 | if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1854 | PyObject *v; |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 1855 | int i; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1856 | int ndefs = com_argdefs(c, CHILD(n, 0)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1857 | v = (PyObject *) icompile(CHILD(n, 0), c); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 1858 | if (v == NULL) { |
| 1859 | c->c_errors++; |
| 1860 | i = 255; |
| 1861 | } |
| 1862 | else { |
| 1863 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1864 | Py_DECREF(v); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 1865 | } |
| 1866 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1867 | com_push(c, 1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1868 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1869 | com_pop(c, ndefs); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1870 | } |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 1871 | else { |
| 1872 | int anchor = 0; |
| 1873 | int i = 0; |
| 1874 | for (;;) { |
| 1875 | com_and_test(c, CHILD(n, i)); |
| 1876 | if ((i += 2) >= NCH(n)) |
| 1877 | break; |
| 1878 | com_addfwref(c, JUMP_IF_TRUE, &anchor); |
| 1879 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1880 | com_pop(c, 1); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 1881 | } |
| 1882 | if (anchor) |
| 1883 | com_backpatch(c, anchor); |
| 1884 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1888 | com_list(struct compiling *c, node *n, int toplevel) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1889 | { |
| 1890 | /* exprlist: expr (',' expr)* [',']; likewise for testlist */ |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 1891 | if (NCH(n) == 1 && !toplevel) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1892 | com_node(c, CHILD(n, 0)); |
| 1893 | } |
| 1894 | else { |
| 1895 | int i; |
| 1896 | int len; |
| 1897 | len = (NCH(n) + 1) / 2; |
| 1898 | for (i = 0; i < NCH(n); i += 2) |
| 1899 | com_node(c, CHILD(n, i)); |
| 1900 | com_addoparg(c, BUILD_TUPLE, len); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1901 | com_pop(c, len-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1902 | } |
| 1903 | } |
| 1904 | |
| 1905 | |
| 1906 | /* Begin of assignment compilation */ |
| 1907 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1908 | |
| 1909 | static void |
| 1910 | com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn) |
| 1911 | { |
| 1912 | com_addbyte(c, DUP_TOP); |
| 1913 | com_push(c, 1); |
| 1914 | com_addopname(c, LOAD_ATTR, n); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1915 | com_node(c, augn); |
| 1916 | com_addbyte(c, opcode); |
| 1917 | com_pop(c, 1); |
| 1918 | com_addbyte(c, ROT_TWO); |
| 1919 | com_addopname(c, STORE_ATTR, n); |
| 1920 | com_pop(c, 2); |
| 1921 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1922 | |
| 1923 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1924 | com_assign_attr(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1925 | { |
| 1926 | com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1927 | com_pop(c, assigning ? 2 : 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
| 1930 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1931 | 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] | 1932 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1933 | REQ(n, trailer); |
| 1934 | switch (TYPE(CHILD(n, 0))) { |
| 1935 | case LPAR: /* '(' [exprlist] ')' */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1936 | com_error(c, PyExc_SyntaxError, |
| 1937 | "can't assign to function call"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1938 | break; |
| 1939 | case DOT: /* '.' NAME */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1940 | if (assigning > OP_APPLY) |
| 1941 | com_augassign_attr(c, CHILD(n, 1), assigning, augn); |
| 1942 | else |
| 1943 | com_assign_attr(c, CHILD(n, 1), assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1944 | break; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1945 | case LSQB: /* '[' subscriptlist ']' */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1946 | com_subscriptlist(c, CHILD(n, 1), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1947 | break; |
| 1948 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1949 | com_error(c, PyExc_SystemError, "unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1950 | } |
| 1951 | } |
| 1952 | |
| 1953 | static void |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 1954 | com_assign_sequence(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1955 | { |
| 1956 | int i; |
Thomas Wouters | b59290f | 2000-08-25 05:41:11 +0000 | [diff] [blame] | 1957 | if (TYPE(n) != testlist && TYPE(n) != listmaker) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1958 | REQ(n, exprlist); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1959 | if (assigning) { |
| 1960 | i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 1961 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1962 | com_push(c, i-1); |
| 1963 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1964 | for (i = 0; i < NCH(n); i += 2) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1965 | com_assign(c, CHILD(n, i), assigning, NULL); |
| 1966 | } |
| 1967 | |
| 1968 | static void |
| 1969 | com_augassign_name(struct compiling *c, node *n, int opcode, node *augn) |
| 1970 | { |
| 1971 | REQ(n, NAME); |
| 1972 | com_addopname(c, LOAD_NAME, n); |
| 1973 | com_push(c, 1); |
| 1974 | com_node(c, augn); |
| 1975 | com_addbyte(c, opcode); |
| 1976 | com_pop(c, 1); |
| 1977 | com_assign_name(c, n, OP_ASSIGN); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1981 | com_assign_name(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1982 | { |
| 1983 | REQ(n, NAME); |
| 1984 | com_addopname(c, assigning ? STORE_NAME : DELETE_NAME, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1985 | if (assigning) |
| 1986 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1990 | com_assign(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1991 | { |
| 1992 | /* Loop to avoid trivial recursion */ |
| 1993 | for (;;) { |
| 1994 | switch (TYPE(n)) { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1995 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1996 | case exprlist: |
| 1997 | case testlist: |
| 1998 | if (NCH(n) > 1) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1999 | if (assigning > OP_APPLY) { |
| 2000 | com_error(c, PyExc_SyntaxError, |
| 2001 | "augmented assign to tuple not possible"); |
| 2002 | return; |
| 2003 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2004 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2005 | return; |
| 2006 | } |
| 2007 | n = CHILD(n, 0); |
| 2008 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2009 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2010 | case test: |
| 2011 | case and_test: |
| 2012 | case not_test: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2013 | case comparison: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2014 | case expr: |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2015 | case xor_expr: |
| 2016 | case and_expr: |
| 2017 | case shift_expr: |
| 2018 | case arith_expr: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2019 | case term: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2020 | case factor: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2021 | if (NCH(n) > 1) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2022 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2023 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2024 | return; |
| 2025 | } |
| 2026 | n = CHILD(n, 0); |
| 2027 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2028 | |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2029 | case power: /* atom trailer* ('**' power)* */ |
| 2030 | /* ('+'|'-'|'~') factor | atom trailer* */ |
| 2031 | if (TYPE(CHILD(n, 0)) != atom) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2032 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2033 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2034 | return; |
| 2035 | } |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2036 | if (NCH(n) > 1) { /* trailer or exponent present */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2037 | int i; |
| 2038 | com_node(c, CHILD(n, 0)); |
| 2039 | for (i = 1; i+1 < NCH(n); i++) { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2040 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2041 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2042 | "can't assign to operator"); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2043 | return; |
| 2044 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2045 | com_apply_trailer(c, CHILD(n, i)); |
| 2046 | } /* NB i is still alive */ |
| 2047 | com_assign_trailer(c, |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2048 | CHILD(n, i), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2049 | return; |
| 2050 | } |
| 2051 | n = CHILD(n, 0); |
| 2052 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2053 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2054 | case atom: |
| 2055 | switch (TYPE(CHILD(n, 0))) { |
| 2056 | case LPAR: |
| 2057 | n = CHILD(n, 1); |
| 2058 | if (TYPE(n) == RPAR) { |
| 2059 | /* XXX Should allow () = () ??? */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2060 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2061 | "can't assign to ()"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2062 | return; |
| 2063 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2064 | if (assigning > OP_APPLY) { |
| 2065 | com_error(c, PyExc_SyntaxError, |
| 2066 | "augmented assign to tuple not possible"); |
| 2067 | return; |
| 2068 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2069 | break; |
| 2070 | case LSQB: |
| 2071 | n = CHILD(n, 1); |
| 2072 | if (TYPE(n) == RSQB) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2073 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2074 | "can't assign to []"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2075 | return; |
| 2076 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2077 | if (assigning > OP_APPLY) { |
| 2078 | com_error(c, PyExc_SyntaxError, |
| 2079 | "augmented assign to list not possible"); |
| 2080 | return; |
| 2081 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2082 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2083 | return; |
| 2084 | case NAME: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2085 | if (assigning > OP_APPLY) |
| 2086 | com_augassign_name(c, CHILD(n, 0), |
| 2087 | assigning, augn); |
| 2088 | else |
| 2089 | com_assign_name(c, CHILD(n, 0), |
| 2090 | assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2091 | return; |
| 2092 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2093 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2094 | "can't assign to literal"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2095 | return; |
| 2096 | } |
| 2097 | break; |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2098 | |
| 2099 | case lambdef: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2100 | com_error(c, PyExc_SyntaxError, |
| 2101 | "can't assign to lambda"); |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2102 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2103 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2104 | default: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2105 | /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2106 | com_error(c, PyExc_SystemError, |
| 2107 | "com_assign: bad node"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2108 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2109 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2110 | } |
| 2111 | } |
| 2112 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2113 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2114 | static void |
| 2115 | com_augassign(struct compiling *c, node *n) |
| 2116 | { |
| 2117 | int opcode; |
| 2118 | |
| 2119 | switch (STR(CHILD(CHILD(n, 1), 0))[0]) { |
| 2120 | case '+': opcode = INPLACE_ADD; break; |
| 2121 | case '-': opcode = INPLACE_SUBTRACT; break; |
| 2122 | case '/': opcode = INPLACE_DIVIDE; break; |
| 2123 | case '%': opcode = INPLACE_MODULO; break; |
| 2124 | case '<': opcode = INPLACE_LSHIFT; break; |
| 2125 | case '>': opcode = INPLACE_RSHIFT; break; |
| 2126 | case '&': opcode = INPLACE_AND; break; |
| 2127 | case '^': opcode = INPLACE_XOR; break; |
| 2128 | case '|': opcode = INPLACE_OR; break; |
| 2129 | case '*': |
| 2130 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '*') |
| 2131 | opcode = INPLACE_POWER; |
| 2132 | else |
| 2133 | opcode = INPLACE_MULTIPLY; |
| 2134 | break; |
| 2135 | default: |
| 2136 | com_error(c, PyExc_SystemError, "com_augassign: bad operator"); |
| 2137 | return; |
| 2138 | } |
| 2139 | com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2)); |
| 2140 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2141 | |
| 2142 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2143 | com_expr_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2144 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2145 | REQ(n, expr_stmt); |
| 2146 | /* testlist (('=' testlist)* | augassign testlist) */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2147 | /* Forget it if we have just a doc string here */ |
Guido van Rossum | 5f5e817 | 1997-04-06 03:41:40 +0000 | [diff] [blame] | 2148 | if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2149 | return; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2150 | if (NCH(n) == 1) { |
| 2151 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2152 | if (c->c_interactive) |
| 2153 | com_addbyte(c, PRINT_EXPR); |
| 2154 | else |
| 2155 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2156 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2157 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2158 | else if (TYPE(CHILD(n,1)) == augassign) |
| 2159 | com_augassign(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2160 | else { |
| 2161 | int i; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2162 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2163 | for (i = 0; i < NCH(n)-2; i+=2) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2164 | if (i+2 < NCH(n)-2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2165 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2166 | com_push(c, 1); |
| 2167 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2168 | com_assign(c, CHILD(n, i), OP_ASSIGN, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2169 | } |
| 2170 | } |
| 2171 | } |
| 2172 | |
| 2173 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2174 | com_assert_stmt(struct compiling *c, node *n) |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2175 | { |
| 2176 | int a = 0, b = 0; |
| 2177 | int i; |
| 2178 | REQ(n, assert_stmt); /* 'assert' test [',' test] */ |
| 2179 | /* Generate code like for |
| 2180 | |
| 2181 | if __debug__: |
| 2182 | if not <test>: |
| 2183 | raise AssertionError [, <message>] |
| 2184 | |
| 2185 | where <message> is the second test, if present. |
| 2186 | */ |
| 2187 | if (Py_OptimizeFlag) |
| 2188 | return; |
| 2189 | com_addopnamestr(c, LOAD_GLOBAL, "__debug__"); |
| 2190 | com_push(c, 1); |
| 2191 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 2192 | com_addbyte(c, POP_TOP); |
| 2193 | com_pop(c, 1); |
| 2194 | com_node(c, CHILD(n, 1)); |
| 2195 | com_addfwref(c, JUMP_IF_TRUE, &b); |
| 2196 | com_addbyte(c, POP_TOP); |
| 2197 | com_pop(c, 1); |
| 2198 | /* Raise that exception! */ |
| 2199 | com_addopnamestr(c, LOAD_GLOBAL, "AssertionError"); |
| 2200 | com_push(c, 1); |
| 2201 | i = NCH(n)/2; /* Either 2 or 4 */ |
| 2202 | if (i > 1) |
| 2203 | com_node(c, CHILD(n, 3)); |
| 2204 | com_addoparg(c, RAISE_VARARGS, i); |
| 2205 | com_pop(c, i); |
| 2206 | /* The interpreter does not fall through */ |
| 2207 | /* All jumps converge here */ |
| 2208 | com_backpatch(c, a); |
| 2209 | com_backpatch(c, b); |
| 2210 | com_addbyte(c, POP_TOP); |
| 2211 | } |
| 2212 | |
| 2213 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2214 | com_print_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2215 | { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2216 | int i = 1; |
| 2217 | node* stream = NULL; |
| 2218 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2219 | REQ(n, print_stmt); /* 'print' (test ',')* [test] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2220 | |
| 2221 | /* are we using the extended print form? */ |
| 2222 | if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) { |
| 2223 | stream = CHILD(n, 2); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2224 | com_node(c, stream); |
| 2225 | /* stack: [...] => [... stream] */ |
| 2226 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2227 | if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA) |
| 2228 | i = 4; |
| 2229 | else |
| 2230 | i = 3; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2231 | } |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2232 | for (; i < NCH(n); i += 2) { |
| 2233 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2234 | com_addbyte(c, DUP_TOP); |
| 2235 | /* stack: [stream] => [stream stream] */ |
| 2236 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2237 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2238 | /* stack: [stream stream] => [stream stream obj] */ |
| 2239 | com_addbyte(c, ROT_TWO); |
| 2240 | /* stack: [stream stream obj] => [stream obj stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2241 | com_addbyte(c, PRINT_ITEM_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2242 | /* stack: [stream obj stream] => [stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2243 | com_pop(c, 2); |
| 2244 | } |
| 2245 | else { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2246 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2247 | /* stack: [...] => [... obj] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2248 | com_addbyte(c, PRINT_ITEM); |
| 2249 | com_pop(c, 1); |
| 2250 | } |
| 2251 | } |
| 2252 | /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */ |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2253 | if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2254 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2255 | /* must pop the extra stream object off the stack */ |
| 2256 | com_addbyte(c, POP_TOP); |
| 2257 | /* stack: [... stream] => [...] */ |
| 2258 | com_pop(c, 1); |
| 2259 | } |
| 2260 | } |
| 2261 | else { |
| 2262 | if (stream != NULL) { |
| 2263 | /* this consumes the last stream object on stack */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2264 | com_addbyte(c, PRINT_NEWLINE_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2265 | /* stack: [... stream] => [...] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2266 | com_pop(c, 1); |
| 2267 | } |
| 2268 | else |
| 2269 | com_addbyte(c, PRINT_NEWLINE); |
| 2270 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
| 2273 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2274 | com_return_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2275 | { |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2276 | REQ(n, return_stmt); /* 'return' [testlist] */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2277 | if (!c->c_infunction) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2278 | com_error(c, PyExc_SyntaxError, "'return' outside function"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2279 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2280 | if (NCH(n) < 2) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2281 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2282 | com_push(c, 1); |
| 2283 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2284 | else |
| 2285 | com_node(c, CHILD(n, 1)); |
| 2286 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2287 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2291 | com_raise_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2292 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2293 | int i; |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 2294 | REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */ |
| 2295 | if (NCH(n) > 1) { |
| 2296 | com_node(c, CHILD(n, 1)); |
| 2297 | if (NCH(n) > 3) { |
| 2298 | com_node(c, CHILD(n, 3)); |
| 2299 | if (NCH(n) > 5) |
| 2300 | com_node(c, CHILD(n, 5)); |
| 2301 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2302 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2303 | i = NCH(n)/2; |
| 2304 | com_addoparg(c, RAISE_VARARGS, i); |
| 2305 | com_pop(c, i); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2306 | } |
| 2307 | |
| 2308 | static void |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2309 | com_from_import(struct compiling *c, node *n) |
| 2310 | { |
| 2311 | com_addopname(c, IMPORT_FROM, CHILD(n, 0)); |
| 2312 | com_push(c, 1); |
| 2313 | if (NCH(n) > 1) { |
| 2314 | if (strcmp(STR(CHILD(n, 1)), "as") != 0) { |
| 2315 | com_error(c, PyExc_SyntaxError, "invalid syntax"); |
| 2316 | return; |
| 2317 | } |
| 2318 | com_addopname(c, STORE_NAME, CHILD(n, 2)); |
| 2319 | } else |
| 2320 | com_addopname(c, STORE_NAME, CHILD(n, 0)); |
| 2321 | com_pop(c, 1); |
| 2322 | } |
| 2323 | |
| 2324 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2325 | com_import_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2326 | { |
| 2327 | int i; |
| 2328 | REQ(n, import_stmt); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2329 | /* 'import' dotted_name (',' dotted_name)* | |
| 2330 | 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2331 | if (STR(CHILD(n, 0))[0] == 'f') { |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame^] | 2332 | PyObject *tup; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2333 | /* 'from' dotted_name 'import' ... */ |
| 2334 | REQ(CHILD(n, 1), dotted_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2335 | |
| 2336 | if (TYPE(CHILD(n, 3)) == STAR) { |
| 2337 | tup = Py_BuildValue("(s)", "*"); |
| 2338 | } else { |
| 2339 | tup = PyTuple_New((NCH(n) - 2)/2); |
| 2340 | for (i = 3; i < NCH(n); i += 2) { |
| 2341 | PyTuple_SET_ITEM(tup, (i-3)/2, |
| 2342 | PyString_FromString(STR( |
| 2343 | CHILD(CHILD(n, i), 0)))); |
| 2344 | } |
| 2345 | } |
| 2346 | com_addoparg(c, LOAD_CONST, com_addconst(c, tup)); |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame^] | 2347 | Py_DECREF(tup); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2348 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2349 | com_addopname(c, IMPORT_NAME, CHILD(n, 1)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2350 | if (TYPE(CHILD(n, 3)) == STAR) |
| 2351 | com_addbyte(c, IMPORT_STAR); |
| 2352 | else { |
| 2353 | for (i = 3; i < NCH(n); i += 2) |
| 2354 | com_from_import(c, CHILD(n, i)); |
| 2355 | com_addbyte(c, POP_TOP); |
| 2356 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2357 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2358 | } |
| 2359 | else { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2360 | /* 'import' ... */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2361 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2362 | node *subn = CHILD(n, i); |
| 2363 | REQ(subn, dotted_as_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2364 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2365 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2366 | com_addopname(c, IMPORT_NAME, CHILD(subn, 0)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2367 | if (NCH(subn) > 1) { |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 2368 | int j; |
| 2369 | if (strcmp(STR(CHILD(subn, 1)), "as") != 0) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2370 | com_error(c, PyExc_SyntaxError, |
| 2371 | "invalid syntax"); |
| 2372 | return; |
| 2373 | } |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 2374 | for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2) |
| 2375 | com_addopname(c, LOAD_ATTR, |
| 2376 | CHILD(CHILD(subn, 0), j)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2377 | com_addopname(c, STORE_NAME, CHILD(subn, 2)); |
| 2378 | } else |
| 2379 | com_addopname(c, STORE_NAME, |
| 2380 | CHILD(CHILD(subn, 0),0)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2381 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2382 | } |
| 2383 | } |
| 2384 | } |
| 2385 | |
| 2386 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2387 | com_global_stmt(struct compiling *c, node *n) |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 2388 | { |
| 2389 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 2390 | REQ(n, global_stmt); |
| 2391 | /* 'global' NAME (',' NAME)* */ |
| 2392 | for (i = 1; i < NCH(n); i += 2) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2393 | char *s = STR(CHILD(n, i)); |
Guido van Rossum | fe2236f | 1996-08-24 07:29:04 +0000 | [diff] [blame] | 2394 | #ifdef PRIVATE_NAME_MANGLING |
| 2395 | char buffer[256]; |
| 2396 | if (s != NULL && s[0] == '_' && s[1] == '_' && |
| 2397 | c->c_private != NULL && |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 2398 | com_mangle(c, s, buffer, sizeof(buffer))) |
Guido van Rossum | fe2236f | 1996-08-24 07:29:04 +0000 | [diff] [blame] | 2399 | s = buffer; |
| 2400 | #endif |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2401 | if (PyDict_GetItemString(c->c_locals, s) != NULL) { |
| 2402 | com_error(c, PyExc_SyntaxError, |
| 2403 | "name is local and global"); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2404 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2405 | else if (PyDict_SetItemString(c->c_globals, s, Py_None) != 0) |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 2406 | c->c_errors++; |
| 2407 | } |
| 2408 | } |
| 2409 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2410 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2411 | com_newlocal_o(struct compiling *c, PyObject *nameval) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2412 | { |
| 2413 | int i; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2414 | PyObject *ival; |
| 2415 | if (PyList_Size(c->c_varnames) != c->c_nlocals) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2416 | /* This is usually caused by an error on a previous call */ |
| 2417 | if (c->c_errors == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2418 | com_error(c, PyExc_SystemError, |
| 2419 | "mixed up var name/index"); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2420 | } |
| 2421 | return 0; |
| 2422 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2423 | ival = PyInt_FromLong(i = c->c_nlocals++); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2424 | if (ival == NULL) |
| 2425 | c->c_errors++; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2426 | else if (PyDict_SetItem(c->c_locals, nameval, ival) != 0) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2427 | c->c_errors++; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2428 | else if (PyList_Append(c->c_varnames, nameval) != 0) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2429 | c->c_errors++; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2430 | Py_XDECREF(ival); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2431 | return i; |
| 2432 | } |
| 2433 | |
| 2434 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2435 | com_addlocal_o(struct compiling *c, PyObject *nameval) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2436 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2437 | PyObject *ival = PyDict_GetItem(c->c_locals, nameval); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2438 | if (ival != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2439 | return PyInt_AsLong(ival); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2440 | return com_newlocal_o(c, nameval); |
| 2441 | } |
| 2442 | |
| 2443 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2444 | com_newlocal(struct compiling *c, char *name) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2445 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2446 | PyObject *nameval = PyString_InternFromString(name); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2447 | int i; |
| 2448 | if (nameval == NULL) { |
| 2449 | c->c_errors++; |
| 2450 | return 0; |
| 2451 | } |
| 2452 | i = com_newlocal_o(c, nameval); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2453 | Py_DECREF(nameval); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2454 | return i; |
| 2455 | } |
| 2456 | |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 2457 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2458 | com_exec_stmt(struct compiling *c, node *n) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2459 | { |
| 2460 | REQ(n, exec_stmt); |
| 2461 | /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */ |
| 2462 | com_node(c, CHILD(n, 1)); |
| 2463 | if (NCH(n) >= 4) |
| 2464 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2465 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2466 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2467 | com_push(c, 1); |
| 2468 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2469 | if (NCH(n) >= 6) |
| 2470 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2471 | else { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2472 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2473 | com_push(c, 1); |
| 2474 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2475 | com_addbyte(c, EXEC_STMT); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2476 | com_pop(c, 3); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2477 | } |
| 2478 | |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2479 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2480 | is_constant_false(struct compiling *c, node *n) |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2481 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2482 | PyObject *v; |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2483 | int i; |
| 2484 | |
| 2485 | /* Label to avoid tail recursion */ |
| 2486 | next: |
| 2487 | switch (TYPE(n)) { |
| 2488 | |
| 2489 | case suite: |
| 2490 | if (NCH(n) == 1) { |
| 2491 | n = CHILD(n, 0); |
| 2492 | goto next; |
| 2493 | } |
| 2494 | /* Fall through */ |
| 2495 | case file_input: |
| 2496 | for (i = 0; i < NCH(n); i++) { |
| 2497 | node *ch = CHILD(n, i); |
| 2498 | if (TYPE(ch) == stmt) { |
| 2499 | n = ch; |
| 2500 | goto next; |
| 2501 | } |
| 2502 | } |
| 2503 | break; |
| 2504 | |
| 2505 | case stmt: |
| 2506 | case simple_stmt: |
| 2507 | case small_stmt: |
| 2508 | n = CHILD(n, 0); |
| 2509 | goto next; |
| 2510 | |
| 2511 | case expr_stmt: |
| 2512 | case testlist: |
| 2513 | case test: |
| 2514 | case and_test: |
| 2515 | case not_test: |
| 2516 | case comparison: |
| 2517 | case expr: |
| 2518 | case xor_expr: |
| 2519 | case and_expr: |
| 2520 | case shift_expr: |
| 2521 | case arith_expr: |
| 2522 | case term: |
| 2523 | case factor: |
| 2524 | case power: |
| 2525 | case atom: |
| 2526 | if (NCH(n) == 1) { |
| 2527 | n = CHILD(n, 0); |
| 2528 | goto next; |
| 2529 | } |
| 2530 | break; |
| 2531 | |
| 2532 | case NAME: |
| 2533 | if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0) |
| 2534 | return 1; |
| 2535 | break; |
| 2536 | |
| 2537 | case NUMBER: |
| 2538 | v = parsenumber(c, STR(n)); |
| 2539 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2540 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2541 | break; |
| 2542 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2543 | i = PyObject_IsTrue(v); |
| 2544 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2545 | return i == 0; |
| 2546 | |
| 2547 | case STRING: |
Guido van Rossum | 6af0c00 | 1997-03-11 21:25:55 +0000 | [diff] [blame] | 2548 | v = parsestr(STR(n)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2549 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2550 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2551 | break; |
| 2552 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2553 | i = PyObject_IsTrue(v); |
| 2554 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2555 | return i == 0; |
| 2556 | |
| 2557 | } |
| 2558 | return 0; |
| 2559 | } |
| 2560 | |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 2561 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2562 | com_if_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2563 | { |
| 2564 | int i; |
| 2565 | int anchor = 0; |
| 2566 | REQ(n, if_stmt); |
| 2567 | /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */ |
| 2568 | for (i = 0; i+3 < NCH(n); i+=4) { |
| 2569 | int a = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2570 | node *ch = CHILD(n, i+1); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2571 | if (is_constant_false(c, ch)) |
| 2572 | continue; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2573 | if (i > 0) |
| 2574 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2575 | com_node(c, ch); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2576 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 2577 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2578 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2579 | com_node(c, CHILD(n, i+3)); |
| 2580 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 2581 | com_backpatch(c, a); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2582 | /* We jump here with an extra entry which we now pop */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2583 | com_addbyte(c, POP_TOP); |
| 2584 | } |
| 2585 | if (i+2 < NCH(n)) |
| 2586 | com_node(c, CHILD(n, i+2)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2587 | if (anchor) |
| 2588 | com_backpatch(c, anchor); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2592 | com_while_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2593 | { |
| 2594 | int break_anchor = 0; |
| 2595 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2596 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2597 | REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */ |
| 2598 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2599 | block_push(c, SETUP_LOOP); |
| 2600 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2601 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2602 | com_node(c, CHILD(n, 1)); |
| 2603 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2604 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2605 | com_pop(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2606 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2607 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2608 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2609 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 2610 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2611 | com_backpatch(c, anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2612 | /* We jump here with one entry more on the stack */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2613 | com_addbyte(c, POP_TOP); |
| 2614 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2615 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2616 | if (NCH(n) > 4) |
| 2617 | com_node(c, CHILD(n, 6)); |
| 2618 | com_backpatch(c, break_anchor); |
| 2619 | } |
| 2620 | |
| 2621 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2622 | com_for_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2623 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2624 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2625 | int break_anchor = 0; |
| 2626 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2627 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2628 | REQ(n, for_stmt); |
| 2629 | /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */ |
| 2630 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2631 | block_push(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2632 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2633 | v = PyInt_FromLong(0L); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2634 | if (v == NULL) |
| 2635 | c->c_errors++; |
| 2636 | com_addoparg(c, LOAD_CONST, com_addconst(c, v)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2637 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2638 | Py_XDECREF(v); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2639 | c->c_begin = c->c_nexti; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2640 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2641 | com_addfwref(c, FOR_LOOP, &anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2642 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2643 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2644 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2645 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2646 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2647 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 2648 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2649 | com_backpatch(c, anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2650 | com_pop(c, 2); /* FOR_LOOP has popped these */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2651 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2652 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2653 | if (NCH(n) > 8) |
| 2654 | com_node(c, CHILD(n, 8)); |
| 2655 | com_backpatch(c, break_anchor); |
| 2656 | } |
| 2657 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2658 | /* Code generated for "try: S finally: Sf" is as follows: |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2659 | |
| 2660 | SETUP_FINALLY L |
| 2661 | <code for S> |
| 2662 | POP_BLOCK |
| 2663 | LOAD_CONST <nil> |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2664 | L: <code for Sf> |
| 2665 | END_FINALLY |
| 2666 | |
| 2667 | The special instructions use the block stack. Each block |
| 2668 | stack entry contains the instruction that created it (here |
| 2669 | SETUP_FINALLY), the level of the value stack at the time the |
| 2670 | block stack entry was created, and a label (here L). |
| 2671 | |
| 2672 | SETUP_FINALLY: |
| 2673 | Pushes the current value stack level and the label |
| 2674 | onto the block stack. |
| 2675 | POP_BLOCK: |
| 2676 | Pops en entry from the block stack, and pops the value |
| 2677 | stack until its level is the same as indicated on the |
| 2678 | block stack. (The label is ignored.) |
| 2679 | END_FINALLY: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2680 | Pops a variable number of entries from the *value* stack |
| 2681 | and re-raises the exception they specify. The number of |
| 2682 | entries popped depends on the (pseudo) exception type. |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2683 | |
| 2684 | The block stack is unwound when an exception is raised: |
| 2685 | when a SETUP_FINALLY entry is found, the exception is pushed |
| 2686 | onto the value stack (and the exception condition is cleared), |
| 2687 | and the interpreter jumps to the label gotten from the block |
| 2688 | stack. |
| 2689 | |
| 2690 | 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] | 2691 | (The contents of the value stack is shown in [], with the top |
| 2692 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 2693 | associated value, and 'exc' the exception.) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2694 | |
| 2695 | Value stack Label Instruction Argument |
| 2696 | [] SETUP_EXCEPT L1 |
| 2697 | [] <code for S> |
| 2698 | [] POP_BLOCK |
| 2699 | [] JUMP_FORWARD L0 |
| 2700 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2701 | [tb, val, exc] L1: DUP ) |
| 2702 | [tb, val, exc, exc] <evaluate E1> ) |
| 2703 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 2704 | [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 ) |
| 2705 | [tb, val, exc, 1] POP ) |
| 2706 | [tb, val, exc] POP |
| 2707 | [tb, val] <assign to V1> (or POP if no V1) |
| 2708 | [tb] POP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2709 | [] <code for S1> |
| 2710 | JUMP_FORWARD L0 |
| 2711 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2712 | [tb, val, exc, 0] L2: POP |
| 2713 | [tb, val, exc] DUP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2714 | .............................etc....................... |
| 2715 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2716 | [tb, val, exc, 0] Ln+1: POP |
| 2717 | [tb, val, exc] END_FINALLY # re-raise exception |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2718 | |
| 2719 | [] L0: <next statement> |
| 2720 | |
| 2721 | Of course, parts are not generated if Vi or Ei is not present. |
| 2722 | */ |
| 2723 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2724 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2725 | com_try_except(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2726 | { |
| 2727 | int except_anchor = 0; |
| 2728 | int end_anchor = 0; |
| 2729 | int else_anchor = 0; |
| 2730 | int i; |
| 2731 | node *ch; |
| 2732 | |
| 2733 | com_addfwref(c, SETUP_EXCEPT, &except_anchor); |
| 2734 | block_push(c, SETUP_EXCEPT); |
| 2735 | com_node(c, CHILD(n, 2)); |
| 2736 | com_addbyte(c, POP_BLOCK); |
| 2737 | block_pop(c, SETUP_EXCEPT); |
| 2738 | com_addfwref(c, JUMP_FORWARD, &else_anchor); |
| 2739 | com_backpatch(c, except_anchor); |
| 2740 | for (i = 3; |
| 2741 | i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause; |
| 2742 | i += 3) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2743 | /* except_clause: 'except' [expr [',' var]] */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2744 | if (except_anchor == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2745 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2746 | "default 'except:' must be last"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2747 | break; |
| 2748 | } |
| 2749 | except_anchor = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2750 | com_push(c, 3); /* tb, val, exc pushed by exception */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2751 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 2752 | if (NCH(ch) > 1) { |
| 2753 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2754 | com_push(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2755 | com_node(c, CHILD(ch, 1)); |
| 2756 | com_addoparg(c, COMPARE_OP, EXC_MATCH); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2757 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2758 | com_addfwref(c, JUMP_IF_FALSE, &except_anchor); |
| 2759 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2760 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2761 | } |
| 2762 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2763 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2764 | if (NCH(ch) > 3) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2765 | com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2766 | else { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2767 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2768 | com_pop(c, 1); |
| 2769 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2770 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2771 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2772 | com_node(c, CHILD(n, i+2)); |
| 2773 | com_addfwref(c, JUMP_FORWARD, &end_anchor); |
| 2774 | if (except_anchor) { |
| 2775 | com_backpatch(c, except_anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2776 | /* We come in with [tb, val, exc, 0] on the |
| 2777 | stack; one pop and it's the same as |
| 2778 | expected at the start of the loop */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2779 | com_addbyte(c, POP_TOP); |
| 2780 | } |
| 2781 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2782 | /* We actually come in here with [tb, val, exc] but the |
| 2783 | END_FINALLY will zap those and jump around. |
| 2784 | The c_stacklevel does not reflect them so we need not pop |
| 2785 | anything. */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2786 | com_addbyte(c, END_FINALLY); |
| 2787 | com_backpatch(c, else_anchor); |
| 2788 | if (i < NCH(n)) |
| 2789 | com_node(c, CHILD(n, i+2)); |
| 2790 | com_backpatch(c, end_anchor); |
| 2791 | } |
| 2792 | |
| 2793 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2794 | com_try_finally(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2795 | { |
| 2796 | int finally_anchor = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2797 | node *ch; |
Guido van Rossum | 94fb82e | 1992-04-05 14:24:50 +0000 | [diff] [blame] | 2798 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2799 | com_addfwref(c, SETUP_FINALLY, &finally_anchor); |
| 2800 | block_push(c, SETUP_FINALLY); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2801 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2802 | com_addbyte(c, POP_BLOCK); |
| 2803 | block_pop(c, SETUP_FINALLY); |
| 2804 | block_push(c, END_FINALLY); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2805 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2806 | /* While the generated code pushes only one item, |
| 2807 | the try-finally handling can enter here with |
| 2808 | up to three items. OK, here are the details: |
| 2809 | 3 for an exception, 2 for RETURN, 1 for BREAK. */ |
| 2810 | com_push(c, 3); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2811 | com_backpatch(c, finally_anchor); |
| 2812 | ch = CHILD(n, NCH(n)-1); |
| 2813 | com_addoparg(c, SET_LINENO, ch->n_lineno); |
| 2814 | com_node(c, ch); |
| 2815 | com_addbyte(c, END_FINALLY); |
| 2816 | block_pop(c, END_FINALLY); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2817 | com_pop(c, 3); /* Matches the com_push above */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2818 | } |
| 2819 | |
| 2820 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2821 | com_try_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2822 | { |
| 2823 | REQ(n, try_stmt); |
| 2824 | /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
| 2825 | | 'try' ':' suite 'finally' ':' suite */ |
| 2826 | if (TYPE(CHILD(n, 3)) != except_clause) |
| 2827 | com_try_finally(c, n); |
| 2828 | else |
| 2829 | com_try_except(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2832 | static node * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2833 | get_rawdocstring(node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2834 | { |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 2835 | int i; |
| 2836 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2837 | /* Label to avoid tail recursion */ |
| 2838 | next: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2839 | switch (TYPE(n)) { |
| 2840 | |
| 2841 | case suite: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2842 | if (NCH(n) == 1) { |
| 2843 | n = CHILD(n, 0); |
| 2844 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2845 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2846 | /* Fall through */ |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 2847 | case file_input: |
| 2848 | for (i = 0; i < NCH(n); i++) { |
| 2849 | node *ch = CHILD(n, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2850 | if (TYPE(ch) == stmt) { |
| 2851 | n = ch; |
| 2852 | goto next; |
| 2853 | } |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 2854 | } |
| 2855 | break; |
| 2856 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2857 | case stmt: |
| 2858 | case simple_stmt: |
| 2859 | case small_stmt: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2860 | n = CHILD(n, 0); |
| 2861 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2862 | |
| 2863 | case expr_stmt: |
| 2864 | case testlist: |
| 2865 | case test: |
| 2866 | case and_test: |
| 2867 | case not_test: |
| 2868 | case comparison: |
| 2869 | case expr: |
| 2870 | case xor_expr: |
| 2871 | case and_expr: |
| 2872 | case shift_expr: |
| 2873 | case arith_expr: |
| 2874 | case term: |
| 2875 | case factor: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2876 | case power: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2877 | if (NCH(n) == 1) { |
| 2878 | n = CHILD(n, 0); |
| 2879 | goto next; |
| 2880 | } |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2881 | break; |
| 2882 | |
| 2883 | case atom: |
| 2884 | if (TYPE(CHILD(n, 0)) == STRING) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2885 | return n; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2886 | break; |
| 2887 | |
| 2888 | } |
| 2889 | return NULL; |
| 2890 | } |
| 2891 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2892 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2893 | get_docstring(node *n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2894 | { |
Guido van Rossum | 541563e | 1999-01-28 15:08:09 +0000 | [diff] [blame] | 2895 | /* Don't generate doc-strings if run with -OO */ |
| 2896 | if (Py_OptimizeFlag > 1) |
| 2897 | return NULL; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2898 | n = get_rawdocstring(n); |
| 2899 | if (n == NULL) |
| 2900 | return NULL; |
| 2901 | return parsestrplus(n); |
| 2902 | } |
| 2903 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2904 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2905 | com_suite(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2906 | { |
| 2907 | REQ(n, suite); |
| 2908 | /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */ |
| 2909 | if (NCH(n) == 1) { |
| 2910 | com_node(c, CHILD(n, 0)); |
| 2911 | } |
| 2912 | else { |
| 2913 | int i; |
| 2914 | for (i = 0; i < NCH(n); i++) { |
| 2915 | node *ch = CHILD(n, i); |
| 2916 | if (TYPE(ch) == stmt) |
| 2917 | com_node(c, ch); |
| 2918 | } |
| 2919 | } |
| 2920 | } |
| 2921 | |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 2922 | /* ARGSUSED */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2923 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2924 | com_continue_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2925 | { |
| 2926 | int i = c->c_nblocks; |
| 2927 | if (i-- > 0 && c->c_block[i] == SETUP_LOOP) { |
| 2928 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 2929 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 2930 | else if (i <= 0) { |
| 2931 | /* at the outer level */ |
| 2932 | com_error(c, PyExc_SyntaxError, |
| 2933 | "'continue' not properly in loop"); |
| 2934 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2935 | else { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 2936 | int j; |
| 2937 | for (j = 0; j <= i; ++j) { |
| 2938 | if (c->c_block[j] == SETUP_LOOP) |
| 2939 | break; |
| 2940 | } |
| 2941 | if (j < i+1) { |
| 2942 | /* there is a loop, but something interferes */ |
| 2943 | for (++j; j <= i; ++j) { |
| 2944 | if (c->c_block[i] == SETUP_EXCEPT |
| 2945 | || c->c_block[i] == SETUP_FINALLY) { |
| 2946 | com_error(c, PyExc_SyntaxError, |
| 2947 | "'continue' not supported inside 'try' clause"); |
| 2948 | return; |
| 2949 | } |
| 2950 | } |
| 2951 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2952 | com_error(c, PyExc_SyntaxError, |
| 2953 | "'continue' not properly in loop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2954 | } |
| 2955 | /* XXX Could allow it inside a 'finally' clause |
| 2956 | XXX if we could pop the exception still on the stack */ |
| 2957 | } |
| 2958 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2959 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2960 | com_argdefs(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2961 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2962 | int i, nch, nargs, ndefs; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2963 | if (TYPE(n) == lambdef) { |
| 2964 | /* lambdef: 'lambda' [varargslist] ':' test */ |
| 2965 | n = CHILD(n, 1); |
| 2966 | } |
| 2967 | else { |
| 2968 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */ |
| 2969 | n = CHILD(n, 2); |
| 2970 | REQ(n, parameters); /* parameters: '(' [varargslist] ')' */ |
| 2971 | n = CHILD(n, 1); |
| 2972 | } |
| 2973 | if (TYPE(n) != varargslist) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2974 | return 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2975 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2976 | (fpdef ['=' test] ',')* '*' ....... | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2977 | fpdef ['=' test] (',' fpdef ['=' test])* [','] */ |
| 2978 | nch = NCH(n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2979 | nargs = 0; |
| 2980 | ndefs = 0; |
| 2981 | for (i = 0; i < nch; i++) { |
| 2982 | int t; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2983 | if (TYPE(CHILD(n, i)) == STAR || |
| 2984 | TYPE(CHILD(n, i)) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2985 | break; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2986 | nargs++; |
| 2987 | i++; |
| 2988 | if (i >= nch) |
Guido van Rossum | a1e7e62 | 1995-09-18 21:44:04 +0000 | [diff] [blame] | 2989 | t = RPAR; /* Anything except EQUAL or COMMA */ |
| 2990 | else |
| 2991 | t = TYPE(CHILD(n, i)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2992 | if (t == EQUAL) { |
| 2993 | i++; |
| 2994 | ndefs++; |
| 2995 | com_node(c, CHILD(n, i)); |
| 2996 | i++; |
| 2997 | if (i >= nch) |
| 2998 | break; |
| 2999 | t = TYPE(CHILD(n, i)); |
| 3000 | } |
| 3001 | else { |
Guido van Rossum | 29d38cd | 1998-10-02 13:41:54 +0000 | [diff] [blame] | 3002 | /* Treat "(a=1, b)" as an error */ |
| 3003 | if (ndefs) |
| 3004 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | dfede31 | 1998-10-02 14:06:56 +0000 | [diff] [blame] | 3005 | "non-default argument follows default argument"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3006 | } |
| 3007 | if (t != COMMA) |
| 3008 | break; |
| 3009 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3010 | return ndefs; |
| 3011 | } |
| 3012 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3013 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3014 | com_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3015 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3016 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3017 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3018 | v = (PyObject *)icompile(n, c); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3019 | if (v == NULL) |
| 3020 | c->c_errors++; |
| 3021 | else { |
| 3022 | int i = com_addconst(c, v); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3023 | int ndefs = com_argdefs(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3024 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3025 | com_push(c, 1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3026 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3027 | com_pop(c, ndefs); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3028 | com_addopname(c, STORE_NAME, CHILD(n, 1)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3029 | com_pop(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3030 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3031 | } |
| 3032 | } |
| 3033 | |
| 3034 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3035 | com_bases(struct compiling *c, node *n) |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3036 | { |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3037 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3038 | REQ(n, testlist); |
| 3039 | /* testlist: test (',' test)* [','] */ |
| 3040 | for (i = 0; i < NCH(n); i += 2) |
| 3041 | com_node(c, CHILD(n, i)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3042 | i = (NCH(n)+1) / 2; |
| 3043 | com_addoparg(c, BUILD_TUPLE, i); |
| 3044 | com_pop(c, i-1); |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3045 | } |
| 3046 | |
| 3047 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3048 | com_classdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3049 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3050 | int i; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3051 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3052 | REQ(n, classdef); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3053 | /* classdef: class NAME ['(' testlist ')'] ':' suite */ |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 3054 | if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3055 | c->c_errors++; |
| 3056 | return; |
| 3057 | } |
| 3058 | /* Push the class name on the stack */ |
| 3059 | i = com_addconst(c, v); |
| 3060 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3061 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3062 | Py_DECREF(v); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3063 | /* Push the tuple of base classes on the stack */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3064 | if (TYPE(CHILD(n, 2)) != LPAR) { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3065 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3066 | com_push(c, 1); |
| 3067 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3068 | else |
| 3069 | com_bases(c, CHILD(n, 3)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3070 | v = (PyObject *)icompile(n, c); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3071 | if (v == NULL) |
| 3072 | c->c_errors++; |
| 3073 | else { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3074 | i = com_addconst(c, v); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3075 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3076 | com_push(c, 1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3077 | com_addoparg(c, MAKE_FUNCTION, 0); |
| 3078 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3079 | com_addbyte(c, BUILD_CLASS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3080 | com_pop(c, 2); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3081 | com_addopname(c, STORE_NAME, CHILD(n, 1)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3082 | Py_DECREF(v); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3083 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3084 | } |
| 3085 | |
| 3086 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3087 | com_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3088 | { |
| 3089 | switch (TYPE(n)) { |
| 3090 | |
| 3091 | /* Definition nodes */ |
| 3092 | |
| 3093 | case funcdef: |
| 3094 | com_funcdef(c, n); |
| 3095 | break; |
| 3096 | case classdef: |
| 3097 | com_classdef(c, n); |
| 3098 | break; |
| 3099 | |
| 3100 | /* Trivial parse tree nodes */ |
| 3101 | |
| 3102 | case stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3103 | case small_stmt: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3104 | case flow_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3105 | com_node(c, CHILD(n, 0)); |
| 3106 | break; |
| 3107 | |
| 3108 | case simple_stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3109 | /* small_stmt (';' small_stmt)* [';'] NEWLINE */ |
| 3110 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 3111 | { |
| 3112 | int i; |
| 3113 | for (i = 0; i < NCH(n)-1; i += 2) |
| 3114 | com_node(c, CHILD(n, i)); |
| 3115 | } |
| 3116 | break; |
| 3117 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3118 | case compound_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3119 | com_addoparg(c, SET_LINENO, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3120 | com_node(c, CHILD(n, 0)); |
| 3121 | break; |
| 3122 | |
| 3123 | /* Statement nodes */ |
| 3124 | |
| 3125 | case expr_stmt: |
| 3126 | com_expr_stmt(c, n); |
| 3127 | break; |
| 3128 | case print_stmt: |
| 3129 | com_print_stmt(c, n); |
| 3130 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3131 | case del_stmt: /* 'del' exprlist */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3132 | com_assign(c, CHILD(n, 1), OP_DELETE, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3133 | break; |
| 3134 | case pass_stmt: |
| 3135 | break; |
| 3136 | case break_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3137 | if (c->c_loops == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3138 | com_error(c, PyExc_SyntaxError, |
| 3139 | "'break' outside loop"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3140 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3141 | com_addbyte(c, BREAK_LOOP); |
| 3142 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3143 | case continue_stmt: |
| 3144 | com_continue_stmt(c, n); |
| 3145 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3146 | case return_stmt: |
| 3147 | com_return_stmt(c, n); |
| 3148 | break; |
| 3149 | case raise_stmt: |
| 3150 | com_raise_stmt(c, n); |
| 3151 | break; |
| 3152 | case import_stmt: |
| 3153 | com_import_stmt(c, n); |
| 3154 | break; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3155 | case global_stmt: |
| 3156 | com_global_stmt(c, n); |
| 3157 | break; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3158 | case exec_stmt: |
| 3159 | com_exec_stmt(c, n); |
| 3160 | break; |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 3161 | case assert_stmt: |
| 3162 | com_assert_stmt(c, n); |
| 3163 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3164 | case if_stmt: |
| 3165 | com_if_stmt(c, n); |
| 3166 | break; |
| 3167 | case while_stmt: |
| 3168 | com_while_stmt(c, n); |
| 3169 | break; |
| 3170 | case for_stmt: |
| 3171 | com_for_stmt(c, n); |
| 3172 | break; |
| 3173 | case try_stmt: |
| 3174 | com_try_stmt(c, n); |
| 3175 | break; |
| 3176 | case suite: |
| 3177 | com_suite(c, n); |
| 3178 | break; |
| 3179 | |
| 3180 | /* Expression nodes */ |
| 3181 | |
| 3182 | case testlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3183 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3184 | break; |
| 3185 | case test: |
| 3186 | com_test(c, n); |
| 3187 | break; |
| 3188 | case and_test: |
| 3189 | com_and_test(c, n); |
| 3190 | break; |
| 3191 | case not_test: |
| 3192 | com_not_test(c, n); |
| 3193 | break; |
| 3194 | case comparison: |
| 3195 | com_comparison(c, n); |
| 3196 | break; |
| 3197 | case exprlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3198 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3199 | break; |
| 3200 | case expr: |
| 3201 | com_expr(c, n); |
| 3202 | break; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 3203 | case xor_expr: |
| 3204 | com_xor_expr(c, n); |
| 3205 | break; |
| 3206 | case and_expr: |
| 3207 | com_and_expr(c, n); |
| 3208 | break; |
| 3209 | case shift_expr: |
| 3210 | com_shift_expr(c, n); |
| 3211 | break; |
| 3212 | case arith_expr: |
| 3213 | com_arith_expr(c, n); |
| 3214 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3215 | case term: |
| 3216 | com_term(c, n); |
| 3217 | break; |
| 3218 | case factor: |
| 3219 | com_factor(c, n); |
| 3220 | break; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3221 | case power: |
| 3222 | com_power(c, n); |
| 3223 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3224 | case atom: |
| 3225 | com_atom(c, n); |
| 3226 | break; |
| 3227 | |
| 3228 | default: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3229 | /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3230 | com_error(c, PyExc_SystemError, |
| 3231 | "com_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3232 | } |
| 3233 | } |
| 3234 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 3235 | static void com_fplist(struct compiling *, node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3236 | |
| 3237 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3238 | com_fpdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3239 | { |
| 3240 | REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3241 | if (TYPE(CHILD(n, 0)) == LPAR) |
| 3242 | com_fplist(c, CHILD(n, 1)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3243 | else { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3244 | com_addoparg(c, STORE_FAST, com_newlocal(c, STR(CHILD(n, 0)))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3245 | com_pop(c, 1); |
| 3246 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3247 | } |
| 3248 | |
| 3249 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3250 | com_fplist(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3251 | { |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 3252 | REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3253 | if (NCH(n) == 1) { |
| 3254 | com_fpdef(c, CHILD(n, 0)); |
| 3255 | } |
| 3256 | else { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3257 | int i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 3258 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3259 | com_push(c, i-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3260 | for (i = 0; i < NCH(n); i += 2) |
| 3261 | com_fpdef(c, CHILD(n, i)); |
| 3262 | } |
| 3263 | } |
| 3264 | |
| 3265 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3266 | com_arglist(struct compiling *c, node *n) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3267 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3268 | int nch, i; |
| 3269 | int complex = 0; |
Guido van Rossum | db9e20f | 1997-07-10 01:06:53 +0000 | [diff] [blame] | 3270 | char nbuf[10]; |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3271 | REQ(n, varargslist); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 3272 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3273 | (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3274 | nch = NCH(n); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3275 | /* Enter all arguments in table of locals */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3276 | for (i = 0; i < nch; i++) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3277 | node *ch = CHILD(n, i); |
| 3278 | node *fp; |
| 3279 | char *name; |
Peter Schneider-Kamp | 11384c6 | 2000-07-13 06:15:04 +0000 | [diff] [blame] | 3280 | PyObject *nameval; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3281 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3282 | break; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3283 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3284 | fp = CHILD(ch, 0); |
| 3285 | if (TYPE(fp) == NAME) |
| 3286 | name = STR(fp); |
| 3287 | else { |
Guido van Rossum | db9e20f | 1997-07-10 01:06:53 +0000 | [diff] [blame] | 3288 | name = nbuf; |
| 3289 | sprintf(nbuf, ".%d", i); |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 3290 | complex = 1; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3291 | } |
Peter Schneider-Kamp | 11384c6 | 2000-07-13 06:15:04 +0000 | [diff] [blame] | 3292 | nameval = PyString_InternFromString(name); |
| 3293 | if (nameval == NULL) { |
| 3294 | c->c_errors++; |
| 3295 | } |
Peter Schneider-Kamp | 9a5086c | 2000-07-13 06:24:29 +0000 | [diff] [blame] | 3296 | if (PyDict_GetItem(c->c_locals, nameval)) { |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 3297 | com_error(c, PyExc_SyntaxError, |
| 3298 | "duplicate argument in function definition"); |
Peter Schneider-Kamp | 11384c6 | 2000-07-13 06:15:04 +0000 | [diff] [blame] | 3299 | } |
| 3300 | com_newlocal_o(c, nameval); |
| 3301 | Py_DECREF(nameval); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3302 | c->c_argcount++; |
| 3303 | if (++i >= nch) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3304 | break; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3305 | ch = CHILD(n, i); |
| 3306 | if (TYPE(ch) == EQUAL) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3307 | i += 2; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3308 | else |
| 3309 | REQ(ch, COMMA); |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3310 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3311 | /* Handle *arguments */ |
| 3312 | if (i < nch) { |
| 3313 | node *ch; |
| 3314 | ch = CHILD(n, i); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3315 | if (TYPE(ch) != DOUBLESTAR) { |
| 3316 | REQ(ch, STAR); |
| 3317 | ch = CHILD(n, i+1); |
| 3318 | if (TYPE(ch) == NAME) { |
| 3319 | c->c_flags |= CO_VARARGS; |
| 3320 | i += 3; |
| 3321 | com_newlocal(c, STR(ch)); |
| 3322 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3323 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3324 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3325 | /* Handle **keywords */ |
| 3326 | if (i < nch) { |
| 3327 | node *ch; |
| 3328 | ch = CHILD(n, i); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3329 | if (TYPE(ch) != DOUBLESTAR) { |
| 3330 | REQ(ch, STAR); |
| 3331 | ch = CHILD(n, i+1); |
| 3332 | REQ(ch, STAR); |
| 3333 | ch = CHILD(n, i+2); |
| 3334 | } |
| 3335 | else |
| 3336 | ch = CHILD(n, i+1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3337 | REQ(ch, NAME); |
| 3338 | c->c_flags |= CO_VARKEYWORDS; |
| 3339 | com_newlocal(c, STR(ch)); |
| 3340 | } |
| 3341 | if (complex) { |
| 3342 | /* Generate code for complex arguments only after |
| 3343 | having counted the simple arguments */ |
| 3344 | int ilocal = 0; |
| 3345 | for (i = 0; i < nch; i++) { |
| 3346 | node *ch = CHILD(n, i); |
| 3347 | node *fp; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3348 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3349 | break; |
| 3350 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3351 | fp = CHILD(ch, 0); |
| 3352 | if (TYPE(fp) != NAME) { |
| 3353 | com_addoparg(c, LOAD_FAST, ilocal); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3354 | com_push(c, 1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3355 | com_fpdef(c, ch); |
| 3356 | } |
| 3357 | ilocal++; |
| 3358 | if (++i >= nch) |
| 3359 | break; |
| 3360 | ch = CHILD(n, i); |
| 3361 | if (TYPE(ch) == EQUAL) |
| 3362 | i += 2; |
| 3363 | else |
| 3364 | REQ(ch, COMMA); |
| 3365 | } |
| 3366 | } |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3367 | } |
| 3368 | |
| 3369 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3370 | com_file_input(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3371 | { |
| 3372 | int i; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3373 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3374 | REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */ |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3375 | doc = get_docstring(n); |
| 3376 | if (doc != NULL) { |
| 3377 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3378 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3379 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3380 | com_push(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3381 | com_addopnamestr(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3382 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3383 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3384 | for (i = 0; i < NCH(n); i++) { |
| 3385 | node *ch = CHILD(n, i); |
| 3386 | if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE) |
| 3387 | com_node(c, ch); |
| 3388 | } |
| 3389 | } |
| 3390 | |
| 3391 | /* Top-level compile-node interface */ |
| 3392 | |
| 3393 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3394 | compile_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3395 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3396 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3397 | node *ch; |
| 3398 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3399 | c->c_name = STR(CHILD(n, 1)); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3400 | doc = get_docstring(CHILD(n, 4)); |
| 3401 | if (doc != NULL) { |
| 3402 | (void) com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3403 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3404 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3405 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3406 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3407 | ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */ |
| 3408 | ch = CHILD(ch, 1); /* ')' | varargslist */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3409 | if (TYPE(ch) == varargslist) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3410 | com_arglist(c, ch); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3411 | c->c_infunction = 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3412 | com_node(c, CHILD(n, 4)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3413 | c->c_infunction = 0; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3414 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3415 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3416 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3417 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3418 | } |
| 3419 | |
| 3420 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3421 | compile_lambdef(struct compiling *c, node *n) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 3422 | { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3423 | node *ch; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3424 | REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */ |
Guido van Rossum | 363ac7d | 1994-11-10 22:40:34 +0000 | [diff] [blame] | 3425 | c->c_name = "<lambda>"; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3426 | |
| 3427 | ch = CHILD(n, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3428 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3429 | if (TYPE(ch) == varargslist) { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3430 | com_arglist(c, ch); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3431 | ch = CHILD(n, 3); |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3432 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3433 | else |
| 3434 | ch = CHILD(n, 2); |
| 3435 | com_node(c, ch); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 3436 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3437 | com_pop(c, 1); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 3438 | } |
| 3439 | |
| 3440 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3441 | compile_classdef(struct compiling *c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3442 | { |
| 3443 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3444 | PyObject *doc; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3445 | REQ(n, classdef); |
| 3446 | /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ |
| 3447 | c->c_name = STR(CHILD(n, 1)); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 3448 | #ifdef PRIVATE_NAME_MANGLING |
| 3449 | c->c_private = c->c_name; |
| 3450 | #endif |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3451 | ch = CHILD(n, NCH(n)-1); /* The suite */ |
| 3452 | doc = get_docstring(ch); |
| 3453 | if (doc != NULL) { |
| 3454 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3455 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3456 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3457 | com_push(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3458 | com_addopnamestr(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3459 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3460 | } |
| 3461 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3462 | (void) com_addconst(c, Py_None); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3463 | com_node(c, ch); |
| 3464 | com_addbyte(c, LOAD_LOCALS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3465 | com_push(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3466 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3467 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3468 | } |
| 3469 | |
| 3470 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3471 | compile_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3472 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3473 | com_addoparg(c, SET_LINENO, n->n_lineno); |
| 3474 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3475 | switch (TYPE(n)) { |
| 3476 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3477 | case single_input: /* One interactive command */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3478 | /* NEWLINE | simple_stmt | compound_stmt NEWLINE */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3479 | c->c_interactive++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3480 | n = CHILD(n, 0); |
| 3481 | if (TYPE(n) != NEWLINE) |
| 3482 | com_node(c, n); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3483 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3484 | com_push(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3485 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3486 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3487 | c->c_interactive--; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3488 | break; |
| 3489 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3490 | case file_input: /* A whole file, or built-in function exec() */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3491 | com_file_input(c, n); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3492 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3493 | com_push(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3494 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3495 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3496 | break; |
| 3497 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3498 | case eval_input: /* Built-in function input() */ |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3499 | com_node(c, CHILD(n, 0)); |
| 3500 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3501 | com_pop(c, 1); |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3502 | break; |
| 3503 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3504 | case lambdef: /* anonymous function definition */ |
| 3505 | compile_lambdef(c, n); |
| 3506 | break; |
| 3507 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3508 | case funcdef: /* A function definition */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3509 | compile_funcdef(c, n); |
| 3510 | break; |
| 3511 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 3512 | case classdef: /* A class definition */ |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3513 | compile_classdef(c, n); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3514 | break; |
| 3515 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3516 | default: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3517 | /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3518 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 3519 | "compile_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3520 | } |
| 3521 | } |
| 3522 | |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 3523 | /* Optimization for local variables in functions (and *only* functions). |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 3524 | |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 3525 | This replaces all LOAD_NAME, STORE_NAME and DELETE_NAME |
| 3526 | instructions that refer to local variables with LOAD_FAST etc. |
| 3527 | The latter instructions are much faster because they don't need to |
| 3528 | look up the variable name in a dictionary. |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 3529 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3530 | To find all local variables, we check all STORE_NAME, IMPORT_FROM |
| 3531 | and DELETE_NAME instructions. This yields all local variables, |
| 3532 | function definitions, class definitions and import statements. |
| 3533 | Argument names have already been entered into the list by the |
| 3534 | special processing for the argument list. |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3535 | |
| 3536 | All remaining LOAD_NAME instructions must refer to non-local (global |
| 3537 | or builtin) variables, so are replaced by LOAD_GLOBAL. |
| 3538 | |
| 3539 | There are two problems: 'from foo import *' and 'exec' may introduce |
| 3540 | local variables that we can't know while compiling. If this is the |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3541 | case, we can still optimize bona fide locals (since those |
| 3542 | statements will be surrounded by fast_2_locals() and |
| 3543 | locals_2_fast()), but we can't change LOAD_NAME to LOAD_GLOBAL. |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3544 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3545 | NB: this modifies the string object c->c_code! */ |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 3546 | |
| 3547 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3548 | optimize(struct compiling *c) |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 3549 | { |
Guido van Rossum | 0a697f6 | 1991-04-16 08:39:12 +0000 | [diff] [blame] | 3550 | unsigned char *next_instr, *cur_instr; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 3551 | int opcode; |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 3552 | int oparg = 0; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3553 | PyObject *name; |
| 3554 | PyObject *error_type, *error_value, *error_traceback; |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 3555 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 3556 | #define NEXTOP() (*next_instr++) |
| 3557 | #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2]) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3558 | #define GETITEM(v, i) (PyList_GetItem((v), (i))) |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 3559 | #define GETNAMEOBJ(i) (GETITEM(c->c_names, (i))) |
| 3560 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3561 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3562 | |
| 3563 | c->c_flags |= CO_OPTIMIZED; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 3564 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3565 | next_instr = (unsigned char *) PyString_AsString(c->c_code); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 3566 | for (;;) { |
| 3567 | opcode = NEXTOP(); |
| 3568 | if (opcode == STOP_CODE) |
| 3569 | break; |
| 3570 | if (HAS_ARG(opcode)) |
| 3571 | oparg = NEXTARG(); |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 3572 | dispatch_opcode1: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3573 | switch (opcode) { |
| 3574 | case STORE_NAME: |
| 3575 | case DELETE_NAME: |
| 3576 | case IMPORT_FROM: |
| 3577 | com_addlocal_o(c, GETNAMEOBJ(oparg)); |
| 3578 | break; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3579 | case IMPORT_STAR: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3580 | case EXEC_STMT: |
| 3581 | c->c_flags &= ~CO_OPTIMIZED; |
| 3582 | break; |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 3583 | case EXTENDED_ARG: |
| 3584 | opcode = NEXTOP(); |
| 3585 | oparg = oparg<<16 | NEXTARG(); |
| 3586 | goto dispatch_opcode1; |
| 3587 | break; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 3588 | } |
| 3589 | } |
| 3590 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3591 | /* TBD: Is this still necessary ? */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3592 | if (PyDict_GetItemString(c->c_locals, "*") != NULL) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3593 | c->c_flags &= ~CO_OPTIMIZED; |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 3594 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3595 | next_instr = (unsigned char *) PyString_AsString(c->c_code); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 3596 | for (;;) { |
| 3597 | cur_instr = next_instr; |
| 3598 | opcode = NEXTOP(); |
| 3599 | if (opcode == STOP_CODE) |
| 3600 | break; |
| 3601 | if (HAS_ARG(opcode)) |
| 3602 | oparg = NEXTARG(); |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 3603 | dispatch_opcode2: |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 3604 | if (opcode == LOAD_NAME || |
| 3605 | opcode == STORE_NAME || |
| 3606 | opcode == DELETE_NAME) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3607 | PyObject *v; |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 3608 | int i; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 3609 | name = GETNAMEOBJ(oparg); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3610 | v = PyDict_GetItem(c->c_locals, name); |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 3611 | if (v == NULL) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3612 | if (opcode == LOAD_NAME && |
| 3613 | (c->c_flags&CO_OPTIMIZED)) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3614 | cur_instr[0] = LOAD_GLOBAL; |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 3615 | continue; |
Guido van Rossum | 8316325 | 1991-08-16 08:58:43 +0000 | [diff] [blame] | 3616 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3617 | i = PyInt_AsLong(v); |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 3618 | if (i >> 16) /* too big for 2 bytes */ |
| 3619 | continue; |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 3620 | switch (opcode) { |
| 3621 | case LOAD_NAME: cur_instr[0] = LOAD_FAST; break; |
| 3622 | case STORE_NAME: cur_instr[0] = STORE_FAST; break; |
| 3623 | case DELETE_NAME: cur_instr[0] = DELETE_FAST; break; |
| 3624 | } |
| 3625 | cur_instr[1] = i & 0xff; |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 3626 | cur_instr[2] = i >> 8; |
| 3627 | } |
| 3628 | if (opcode == EXTENDED_ARG) { |
| 3629 | opcode = NEXTOP(); |
| 3630 | oparg = oparg<<16 | NEXTARG(); |
| 3631 | goto dispatch_opcode2; |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 3632 | } |
| 3633 | } |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 3634 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3635 | if (c->c_errors == 0) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3636 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 3637 | } |
| 3638 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3639 | PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3640 | PyNode_Compile(node *n, char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3641 | { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 3642 | return jcompile(n, filename, NULL); |
| 3643 | } |
| 3644 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3645 | static PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3646 | icompile(node *n, struct compiling *base) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 3647 | { |
| 3648 | return jcompile(n, base->c_filename, base); |
| 3649 | } |
| 3650 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3651 | static PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3652 | jcompile(node *n, char *filename, struct compiling *base) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 3653 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3654 | struct compiling sc; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3655 | PyCodeObject *co; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 3656 | if (!com_init(&sc, filename)) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3657 | return NULL; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 3658 | #ifdef PRIVATE_NAME_MANGLING |
| 3659 | if (base) |
| 3660 | sc.c_private = base->c_private; |
| 3661 | else |
| 3662 | sc.c_private = NULL; |
| 3663 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3664 | compile_node(&sc, n); |
| 3665 | com_done(&sc); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3666 | if ((TYPE(n) == funcdef || TYPE(n) == lambdef) && sc.c_errors == 0) { |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 3667 | optimize(&sc); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3668 | sc.c_flags |= CO_NEWLOCALS; |
| 3669 | } |
| 3670 | else if (TYPE(n) == classdef) |
| 3671 | sc.c_flags |= CO_NEWLOCALS; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3672 | co = NULL; |
| 3673 | if (sc.c_errors == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3674 | PyObject *consts, *names, *varnames, *filename, *name; |
| 3675 | consts = PyList_AsTuple(sc.c_consts); |
| 3676 | names = PyList_AsTuple(sc.c_names); |
| 3677 | varnames = PyList_AsTuple(sc.c_varnames); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 3678 | filename = PyString_InternFromString(sc.c_filename); |
| 3679 | name = PyString_InternFromString(sc.c_name); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3680 | if (!PyErr_Occurred()) |
| 3681 | co = PyCode_New(sc.c_argcount, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3682 | sc.c_nlocals, |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3683 | sc.c_maxstacklevel, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3684 | sc.c_flags, |
| 3685 | sc.c_code, |
| 3686 | consts, |
| 3687 | names, |
| 3688 | varnames, |
| 3689 | filename, |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 3690 | name, |
| 3691 | sc.c_firstlineno, |
| 3692 | sc.c_lnotab); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3693 | Py_XDECREF(consts); |
| 3694 | Py_XDECREF(names); |
| 3695 | Py_XDECREF(varnames); |
| 3696 | Py_XDECREF(filename); |
| 3697 | Py_XDECREF(name); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 3698 | } |
Guido van Rossum | 6c2f0c7 | 2000-08-07 19:22:43 +0000 | [diff] [blame] | 3699 | else if (!PyErr_Occurred()) { |
| 3700 | /* This could happen if someone called PyErr_Clear() after an |
| 3701 | error was reported above. That's not supposed to happen, |
| 3702 | but I just plugged one case and I'm not sure there can't be |
| 3703 | others. In that case, raise SystemError so that at least |
| 3704 | it gets reported instead dumping core. */ |
| 3705 | PyErr_SetString(PyExc_SystemError, "lost syntax error"); |
| 3706 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3707 | com_free(&sc); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3708 | return co; |
| 3709 | } |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 3710 | |
| 3711 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3712 | PyCode_Addr2Line(PyCodeObject *co, int addrq) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 3713 | { |
| 3714 | int size = PyString_Size(co->co_lnotab) / 2; |
Guido van Rossum | 2174dcb | 1999-09-15 22:48:09 +0000 | [diff] [blame] | 3715 | unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 3716 | int line = co->co_firstlineno; |
| 3717 | int addr = 0; |
| 3718 | while (--size >= 0) { |
| 3719 | addr += *p++; |
| 3720 | if (addr > addrq) |
| 3721 | break; |
| 3722 | line += *p++; |
| 3723 | } |
| 3724 | return line; |
| 3725 | } |