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