blob: 9871b0ff03deaca30a0563e0efe1d5d2f2c93bee [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum10dc2e81990-11-18 17:27:39 +000032/* Compile an expression node to intermediate code */
33
Guido van Rossum3f5da241990-12-20 15:06:42 +000034/* XXX TO DO:
Guido van Rossum8b993a91997-01-17 21:04:03 +000035 XXX add __doc__ attribute == co_doc to code object attributes?
36 XXX (it's currently the first item of the co_const tuple)
Guido van Rossum3f5da241990-12-20 15:06:42 +000037 XXX Generate simple jump for break/return outside 'try...finally'
Guido van Rossum8b993a91997-01-17 21:04:03 +000038 XXX Allow 'continue' inside try-finally
Guido van Rossum8b993a91997-01-17 21:04:03 +000039 XXX New opcode for loading the initial index for a for loop
Guido van Rossum681d79a1995-07-18 14:51:37 +000040 XXX other JAR tricks?
Guido van Rossum3f5da241990-12-20 15:06:42 +000041*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +000042
Guido van Rossum8ff077b1996-08-24 06:21:31 +000043#ifndef NO_PRIVATE_NAME_MANGLING
44#define PRIVATE_NAME_MANGLING
45#endif
46
Guido van Rossum79f25d91997-04-29 20:08:16 +000047#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000048
Guido van Rossum10dc2e81990-11-18 17:27:39 +000049#include "node.h"
50#include "token.h"
51#include "graminit.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000052#include "compile.h"
53#include "opcode.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000054#include "structmember.h"
55
56#include <ctype.h>
Guido van Rossum282914b1991-04-04 10:42:56 +000057
Guido van Rossumb05a5c71997-05-07 17:46:13 +000058/* Three symbols from graminit.h are also defined in Python.h, with
59 Py_ prefixes to their names. Python.h can't include graminit.h
60 (which defines too many confusing symbols), but we can check here
61 that they haven't changed (which is very unlikely, but possible). */
62#if Py_single_input != single_input
Guido van Rossum2f75b291997-05-20 22:18:48 +000063 #error "single_input has changed -- update Py_single_input in Python.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000064#endif
65#if Py_file_input != file_input
Guido van Rossum2f75b291997-05-20 22:18:48 +000066 #error "file_input has changed -- update Py_file_input in Python.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000067#endif
68#if Py_eval_input != eval_input
Guido van Rossum2f75b291997-05-20 22:18:48 +000069 #error "eval_input has changed -- update Py_eval_input in Python.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000070#endif
71
Guido van Rossum8e793d91997-03-03 19:13:14 +000072int Py_OptimizeFlag = 0;
73
Guido van Rossum8861b741996-07-30 16:49:37 +000074#define OP_DELETE 0
75#define OP_ASSIGN 1
76#define OP_APPLY 2
77
Guido van Rossum79f25d91997-04-29 20:08:16 +000078#define OFF(x) offsetof(PyCodeObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +000079
80static struct memberlist code_memberlist[] = {
Guido van Rossum681d79a1995-07-18 14:51:37 +000081 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
82 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
Guido van Rossum8b993a91997-01-17 21:04:03 +000083 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
Guido van Rossum681d79a1995-07-18 14:51:37 +000084 {"co_flags", T_INT, OFF(co_flags), READONLY},
Guido van Rossum39d942d1992-01-12 02:30:05 +000085 {"co_code", T_OBJECT, OFF(co_code), READONLY},
86 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
87 {"co_names", T_OBJECT, OFF(co_names), READONLY},
Guido van Rossum681d79a1995-07-18 14:51:37 +000088 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
Guido van Rossum39d942d1992-01-12 02:30:05 +000089 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
Guido van Rossum9bfef441993-03-29 10:43:31 +000090 {"co_name", T_OBJECT, OFF(co_name), READONLY},
Guido van Rossumda4eb5c1997-01-24 03:43:35 +000091 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
92 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
Guido van Rossum3f5da241990-12-20 15:06:42 +000093 {NULL} /* Sentinel */
94};
95
Guido van Rossum79f25d91997-04-29 20:08:16 +000096static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +000097code_getattr(co, name)
Guido van Rossum79f25d91997-04-29 20:08:16 +000098 PyCodeObject *co;
Guido van Rossum3f5da241990-12-20 15:06:42 +000099 char *name;
100{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000101 return PyMember_Get((char *)co, code_memberlist, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000102}
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000103
104static void
Guido van Rossum3f5da241990-12-20 15:06:42 +0000105code_dealloc(co)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000106 PyCodeObject *co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000107{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000108 Py_XDECREF(co->co_code);
109 Py_XDECREF(co->co_consts);
110 Py_XDECREF(co->co_names);
Guido van Rossum275558c1997-07-25 20:13:49 +0000111 Py_XDECREF(co->co_varnames);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000112 Py_XDECREF(co->co_filename);
113 Py_XDECREF(co->co_name);
Guido van Rossum275558c1997-07-25 20:13:49 +0000114 Py_XDECREF(co->co_lnotab);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000115 PyMem_DEL(co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000116}
117
Guido van Rossum79f25d91997-04-29 20:08:16 +0000118static PyObject *
Guido van Rossum2dff9911992-09-03 20:50:59 +0000119code_repr(co)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000120 PyCodeObject *co;
Guido van Rossum2dff9911992-09-03 20:50:59 +0000121{
122 char buf[500];
123 int lineno = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000124 char *p = PyString_AS_STRING(co->co_code);
Guido van Rossum2dff9911992-09-03 20:50:59 +0000125 char *filename = "???";
Guido van Rossum9bfef441993-03-29 10:43:31 +0000126 char *name = "???";
Guido van Rossum2dff9911992-09-03 20:50:59 +0000127 if (*p == SET_LINENO)
128 lineno = (p[1] & 0xff) | ((p[2] & 0xff) << 8);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000129 if (co->co_filename && PyString_Check(co->co_filename))
130 filename = PyString_AsString(co->co_filename);
131 if (co->co_name && PyString_Check(co->co_name))
132 name = PyString_AsString(co->co_name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000133 sprintf(buf, "<code object %.100s at %lx, file \"%.300s\", line %d>",
134 name, (long)co, filename, lineno);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000135 return PyString_FromString(buf);
Guido van Rossum2dff9911992-09-03 20:50:59 +0000136}
137
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000138static int
139code_compare(co, cp)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000140 PyCodeObject *co, *cp;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000141{
142 int cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000143 cmp = co->co_argcount - cp->co_argcount;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000144 if (cmp) return cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000145 cmp = co->co_nlocals - cp->co_nlocals;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000146 if (cmp) return cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000147 cmp = co->co_flags - cp->co_flags;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000148 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000149 cmp = PyObject_Compare((PyObject *)co->co_code,
150 (PyObject *)cp->co_code);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000151 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000152 cmp = PyObject_Compare(co->co_consts, cp->co_consts);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000153 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000154 cmp = PyObject_Compare(co->co_names, cp->co_names);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000155 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000156 cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000157 return cmp;
158}
159
160static long
161code_hash(co)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000162 PyCodeObject *co;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000163{
Guido van Rossum681d79a1995-07-18 14:51:37 +0000164 long h, h1, h2, h3, h4;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000165 h1 = PyObject_Hash((PyObject *)co->co_code);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000166 if (h1 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000167 h2 = PyObject_Hash(co->co_consts);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000168 if (h2 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000169 h3 = PyObject_Hash(co->co_names);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000170 if (h3 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000171 h4 = PyObject_Hash(co->co_varnames);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000172 if (h4 == -1) return -1;
173 h = h1 ^ h2 ^ h3 ^ h4 ^
174 co->co_argcount ^ co->co_nlocals ^ co->co_flags;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000175 if (h == -1) h = -2;
176 return h;
177}
178
Guido van Rossum79f25d91997-04-29 20:08:16 +0000179PyTypeObject PyCode_Type = {
180 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000181 0,
182 "code",
Guido van Rossum79f25d91997-04-29 20:08:16 +0000183 sizeof(PyCodeObject),
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000184 0,
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000185 (destructor)code_dealloc, /*tp_dealloc*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000186 0, /*tp_print*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000187 (getattrfunc)code_getattr, /*tp_getattr*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000188 0, /*tp_setattr*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000189 (cmpfunc)code_compare, /*tp_compare*/
190 (reprfunc)code_repr, /*tp_repr*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000191 0, /*tp_as_number*/
192 0, /*tp_as_sequence*/
193 0, /*tp_as_mapping*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000194 (hashfunc)code_hash, /*tp_hash*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000195};
196
Guido van Rossum644a12b1997-04-09 19:24:53 +0000197#define NAME_CHARS \
198 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
199
Guido van Rossum79f25d91997-04-29 20:08:16 +0000200PyCodeObject *
201PyCode_New(argcount, nlocals, stacksize, flags,
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000202 code, consts, names, varnames, filename, name,
203 firstlineno, lnotab)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000204 int argcount;
205 int nlocals;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000206 int stacksize;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000207 int flags;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000208 PyObject *code;
209 PyObject *consts;
210 PyObject *names;
211 PyObject *varnames;
212 PyObject *filename;
213 PyObject *name;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000214 int firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000215 PyObject *lnotab;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000216{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000217 PyCodeObject *co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000218 int i;
219 /* Check argument types */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000220 if (argcount < 0 || nlocals < 0 ||
Guido van Rossum79f25d91997-04-29 20:08:16 +0000221 code == NULL || !PyString_Check(code) ||
222 consts == NULL || !PyTuple_Check(consts) ||
223 names == NULL || !PyTuple_Check(names) ||
224 varnames == NULL || !PyTuple_Check(varnames) ||
225 name == NULL || !PyString_Check(name) ||
226 filename == NULL || !PyString_Check(filename) ||
227 lnotab == NULL || !PyString_Check(lnotab)) {
228 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000229 return NULL;
230 }
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000231 /* Make sure names and varnames are all strings, & intern them */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000232 for (i = PyTuple_Size(names); --i >= 0; ) {
233 PyObject *v = PyTuple_GetItem(names, i);
234 if (v == NULL || !PyString_Check(v)) {
235 PyErr_BadInternalCall();
Guido van Rossum681d79a1995-07-18 14:51:37 +0000236 return NULL;
237 }
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000238 PyString_InternInPlace(&PyTuple_GET_ITEM(names, i));
Guido van Rossum681d79a1995-07-18 14:51:37 +0000239 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000240 for (i = PyTuple_Size(varnames); --i >= 0; ) {
241 PyObject *v = PyTuple_GetItem(varnames, i);
242 if (v == NULL || !PyString_Check(v)) {
243 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000244 return NULL;
245 }
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000246 PyString_InternInPlace(&PyTuple_GET_ITEM(varnames, i));
247 }
248 /* Intern selected string constants */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000249 for (i = PyTuple_Size(consts); --i >= 0; ) {
250 PyObject *v = PyTuple_GetItem(consts, i);
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000251 char *p;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000252 if (!PyString_Check(v))
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000253 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000254 p = PyString_AsString(v);
Guido van Rossum644a12b1997-04-09 19:24:53 +0000255 if ((int)strspn(p, NAME_CHARS)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000256 != PyString_Size(v))
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000257 continue;
258 PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000259 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000260 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000261 if (co != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +0000262 co->co_argcount = argcount;
263 co->co_nlocals = nlocals;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000264 co->co_stacksize = stacksize;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000265 co->co_flags = flags;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000266 Py_INCREF(code);
267 co->co_code = (PyStringObject *)code;
268 Py_INCREF(consts);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000269 co->co_consts = consts;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000270 Py_INCREF(names);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000271 co->co_names = names;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000272 Py_INCREF(varnames);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000273 co->co_varnames = varnames;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000274 Py_INCREF(filename);
Guido van Rossuma082ce41991-06-04 19:41:56 +0000275 co->co_filename = filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000276 Py_INCREF(name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000277 co->co_name = name;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000278 co->co_firstlineno = firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000279 Py_INCREF(lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000280 co->co_lnotab = lnotab;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000281 }
282 return co;
283}
284
285
286/* Data structure used internally */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000287
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000288struct compiling {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000289 PyObject *c_code; /* string */
290 PyObject *c_consts; /* list of objects */
291 PyObject *c_names; /* list of strings (names) */
292 PyObject *c_globals; /* dictionary (value=None) */
293 PyObject *c_locals; /* dictionary (value=localID) */
294 PyObject *c_varnames; /* list (inverse of c_locals) */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000295 int c_nlocals; /* index of next local */
296 int c_argcount; /* number of top-level arguments */
297 int c_flags; /* same as co_flags */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000298 int c_nexti; /* index into c_code */
299 int c_errors; /* counts errors occurred */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000300 int c_infunction; /* set when compiling a function */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000301 int c_interactive; /* generating code for interactive command */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000302 int c_loops; /* counts nested loops */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000303 int c_begin; /* begin of current loop, for 'continue' */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000304 int c_block[CO_MAXBLOCKS]; /* stack of block types */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000305 int c_nblocks; /* current block stack level */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000306 char *c_filename; /* filename of current node */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000307 char *c_name; /* name of object (e.g. function) */
Guido van Rossum452a9831996-09-17 14:32:04 +0000308 int c_lineno; /* Current line number */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000309 int c_stacklevel; /* Current stack level */
310 int c_maxstacklevel; /* Maximum stack level */
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000311 int c_firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000312 PyObject *c_lnotab; /* Table mapping address to line number */
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000313 int c_last_addr, c_last_line, c_lnotab_next;
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000314#ifdef PRIVATE_NAME_MANGLING
315 char *c_private; /* for private name mangling */
316#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000317};
318
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000319
Guido van Rossum452a9831996-09-17 14:32:04 +0000320/* Error message including line number */
321
322static void
323com_error(c, exc, msg)
324 struct compiling *c;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000325 PyObject *exc;
Guido van Rossum452a9831996-09-17 14:32:04 +0000326 char *msg;
327{
328 int n = strlen(msg);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000329 PyObject *v;
Guido van Rossum452a9831996-09-17 14:32:04 +0000330 char buffer[30];
331 char *s;
Guido van Rossum635abd21997-01-06 22:56:52 +0000332 c->c_errors++;
Guido van Rossum452a9831996-09-17 14:32:04 +0000333 if (c->c_lineno <= 1) {
334 /* Unknown line number or single interactive command */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000335 PyErr_SetString(exc, msg);
Guido van Rossum452a9831996-09-17 14:32:04 +0000336 return;
337 }
338 sprintf(buffer, " (line %d)", c->c_lineno);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000339 v = PyString_FromStringAndSize((char *)NULL, n + strlen(buffer));
Guido van Rossum452a9831996-09-17 14:32:04 +0000340 if (v == NULL)
341 return; /* MemoryError, too bad */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000342 s = PyString_AS_STRING((PyStringObject *)v);
Guido van Rossum452a9831996-09-17 14:32:04 +0000343 strcpy(s, msg);
344 strcat(s, buffer);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000345 PyErr_SetObject(exc, v);
346 Py_DECREF(v);
Guido van Rossum452a9831996-09-17 14:32:04 +0000347}
348
349
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000350/* Interface to the block stack */
351
352static void
353block_push(c, type)
354 struct compiling *c;
355 int type;
356{
Guido van Rossum8b993a91997-01-17 21:04:03 +0000357 if (c->c_nblocks >= CO_MAXBLOCKS) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000358 com_error(c, PyExc_SystemError,
359 "too many statically nested blocks");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000360 }
361 else {
362 c->c_block[c->c_nblocks++] = type;
363 }
364}
365
366static void
367block_pop(c, type)
368 struct compiling *c;
369 int type;
370{
371 if (c->c_nblocks > 0)
372 c->c_nblocks--;
373 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000374 com_error(c, PyExc_SystemError, "bad block pop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000375 }
376}
377
378
Guido van Rossum681d79a1995-07-18 14:51:37 +0000379/* Prototype forward declarations */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000380
Guido van Rossum79f25d91997-04-29 20:08:16 +0000381static int com_init Py_PROTO((struct compiling *, char *));
382static void com_free Py_PROTO((struct compiling *));
383static void com_push Py_PROTO((struct compiling *, int));
384static void com_pop Py_PROTO((struct compiling *, int));
385static void com_done Py_PROTO((struct compiling *));
386static void com_node Py_PROTO((struct compiling *, struct _node *));
387static void com_factor Py_PROTO((struct compiling *, struct _node *));
388static void com_addbyte Py_PROTO((struct compiling *, int));
389static void com_addint Py_PROTO((struct compiling *, int));
390static void com_addoparg Py_PROTO((struct compiling *, int, int));
391static void com_addfwref Py_PROTO((struct compiling *, int, int *));
392static void com_backpatch Py_PROTO((struct compiling *, int));
393static int com_add Py_PROTO((struct compiling *, PyObject *, PyObject *));
394static int com_addconst Py_PROTO((struct compiling *, PyObject *));
395static int com_addname Py_PROTO((struct compiling *, PyObject *));
396static void com_addopname Py_PROTO((struct compiling *, int, node *));
397static void com_list Py_PROTO((struct compiling *, node *, int));
398static int com_argdefs Py_PROTO((struct compiling *, node *));
399static int com_newlocal Py_PROTO((struct compiling *, char *));
400static PyCodeObject *icompile Py_PROTO((struct _node *, struct compiling *));
401static PyCodeObject *jcompile Py_PROTO((struct _node *, char *,
402 struct compiling *));
403static PyObject *parsestrplus Py_PROTO((node *));
404static PyObject *parsestr Py_PROTO((char *));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000405
406static int
Guido van Rossum590baa41993-11-30 13:40:46 +0000407com_init(c, filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000408 struct compiling *c;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000409 char *filename;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000410{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000411 if ((c->c_code = PyString_FromStringAndSize((char *)NULL,
412 1000)) == NULL)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000413 goto fail_3;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000414 if ((c->c_consts = PyList_New(0)) == NULL)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000415 goto fail_2;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000416 if ((c->c_names = PyList_New(0)) == NULL)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000417 goto fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000418 if ((c->c_globals = PyDict_New()) == NULL)
Guido van Rossumc5e96291991-12-10 13:53:51 +0000419 goto fail_0;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000420 if ((c->c_locals = PyDict_New()) == NULL)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000421 goto fail_00;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000422 if ((c->c_varnames = PyList_New(0)) == NULL)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000423 goto fail_000;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000424 if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL,
425 1000)) == NULL)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000426 goto fail_0000;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000427 c->c_nlocals = 0;
428 c->c_argcount = 0;
429 c->c_flags = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000430 c->c_nexti = 0;
431 c->c_errors = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000432 c->c_infunction = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000433 c->c_interactive = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000434 c->c_loops = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000435 c->c_begin = 0;
436 c->c_nblocks = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000437 c->c_filename = filename;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000438 c->c_name = "?";
Guido van Rossum452a9831996-09-17 14:32:04 +0000439 c->c_lineno = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000440 c->c_stacklevel = 0;
441 c->c_maxstacklevel = 0;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000442 c->c_firstlineno = 0;
443 c->c_last_addr = 0;
444 c->c_last_line = 0;
445 c-> c_lnotab_next = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000446 return 1;
447
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000448 fail_0000:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000449 Py_DECREF(c->c_lnotab);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000450 fail_000:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000451 Py_DECREF(c->c_locals);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000452 fail_00:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000453 Py_DECREF(c->c_globals);
Guido van Rossumc5e96291991-12-10 13:53:51 +0000454 fail_0:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000455 Py_DECREF(c->c_names);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000456 fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000457 Py_DECREF(c->c_consts);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000458 fail_2:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000459 Py_DECREF(c->c_code);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000460 fail_3:
461 return 0;
462}
463
464static void
465com_free(c)
466 struct compiling *c;
467{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000468 Py_XDECREF(c->c_code);
469 Py_XDECREF(c->c_consts);
470 Py_XDECREF(c->c_names);
471 Py_XDECREF(c->c_globals);
472 Py_XDECREF(c->c_locals);
473 Py_XDECREF(c->c_varnames);
474 Py_XDECREF(c->c_lnotab);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000475}
476
477static void
Guido van Rossum8b993a91997-01-17 21:04:03 +0000478com_push(c, n)
479 struct compiling *c;
480 int n;
481{
482 c->c_stacklevel += n;
483 if (c->c_stacklevel > c->c_maxstacklevel)
484 c->c_maxstacklevel = c->c_stacklevel;
485}
486
487static void
488com_pop(c, n)
489 struct compiling *c;
490 int n;
491{
492 if (c->c_stacklevel < n) {
Guido van Rossum90f827c1998-08-25 18:22:17 +0000493 /* fprintf(stderr,
Guido van Rossum8b993a91997-01-17 21:04:03 +0000494 "%s:%d: underflow! nexti=%d, level=%d, n=%d\n",
495 c->c_filename, c->c_lineno,
Guido van Rossum90f827c1998-08-25 18:22:17 +0000496 c->c_nexti, c->c_stacklevel, n); */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000497 c->c_stacklevel = 0;
498 }
499 else
500 c->c_stacklevel -= n;
501}
502
503static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000504com_done(c)
505 struct compiling *c;
506{
507 if (c->c_code != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000508 _PyString_Resize(&c->c_code, c->c_nexti);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000509 if (c->c_lnotab != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000510 _PyString_Resize(&c->c_lnotab, c->c_lnotab_next);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000511}
512
513static void
514com_addbyte(c, byte)
515 struct compiling *c;
516 int byte;
517{
518 int len;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000519 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000520 if (byte < 0 || byte > 255) {
Guido van Rossum01cfd441991-10-20 20:12:38 +0000521 /*
Guido van Rossum3f5da241990-12-20 15:06:42 +0000522 fprintf(stderr, "XXX compiling bad byte: %d\n", byte);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000523 fatal("com_addbyte: byte out of range");
Guido van Rossum01cfd441991-10-20 20:12:38 +0000524 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000525 com_error(c, PyExc_SystemError,
526 "com_addbyte: byte out of range");
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000527 }
528 if (c->c_code == NULL)
529 return;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000530 len = PyString_Size(c->c_code);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000531 if (c->c_nexti >= len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000532 if (_PyString_Resize(&c->c_code, len+1000) != 0) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000533 c->c_errors++;
534 return;
535 }
536 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000537 PyString_AsString(c->c_code)[c->c_nexti++] = byte;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000538}
539
540static void
541com_addint(c, x)
542 struct compiling *c;
543 int x;
544{
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000545 com_addbyte(c, x & 0xff);
546 com_addbyte(c, x >> 8); /* XXX x should be positive */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000547}
548
549static void
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000550com_add_lnotab(c, addr, line)
551 struct compiling *c;
552 int addr;
553 int line;
554{
555 int size;
556 char *p;
557 if (c->c_lnotab == NULL)
558 return;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000559 size = PyString_Size(c->c_lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000560 if (c->c_lnotab_next+2 > size) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000561 if (_PyString_Resize(&c->c_lnotab, size + 1000) < 0) {
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000562 c->c_errors++;
563 return;
564 }
565 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000566 p = PyString_AsString(c->c_lnotab) + c->c_lnotab_next;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000567 *p++ = addr;
568 *p++ = line;
569 c->c_lnotab_next += 2;
570}
571
572static void
573com_set_lineno(c, lineno)
574 struct compiling *c;
575 int lineno;
576{
577 c->c_lineno = lineno;
578 if (c->c_firstlineno == 0) {
579 c->c_firstlineno = c->c_last_line = lineno;
580 }
581 else {
582 int incr_addr = c->c_nexti - c->c_last_addr;
583 int incr_line = lineno - c->c_last_line;
584 while (incr_addr > 0 || incr_line > 0) {
585 int trunc_addr = incr_addr;
586 int trunc_line = incr_line;
587 if (trunc_addr > 255)
588 trunc_addr = 255;
589 if (trunc_line > 255)
590 trunc_line = 255;
591 com_add_lnotab(c, trunc_addr, trunc_line);
592 incr_addr -= trunc_addr;
593 incr_line -= trunc_line;
594 }
595 c->c_last_addr = c->c_nexti;
596 c->c_last_line = lineno;
597 }
598}
599
600static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000601com_addoparg(c, op, arg)
602 struct compiling *c;
603 int op;
604 int arg;
605{
Guido van Rossum8e793d91997-03-03 19:13:14 +0000606 if (op == SET_LINENO) {
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000607 com_set_lineno(c, arg);
Guido van Rossum8e793d91997-03-03 19:13:14 +0000608 if (Py_OptimizeFlag)
609 return;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000610 }
Guido van Rossum8e793d91997-03-03 19:13:14 +0000611 com_addbyte(c, op);
612 com_addint(c, arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000613}
614
615static void
616com_addfwref(c, op, p_anchor)
617 struct compiling *c;
618 int op;
619 int *p_anchor;
620{
621 /* Compile a forward reference for backpatching */
622 int here;
623 int anchor;
624 com_addbyte(c, op);
625 here = c->c_nexti;
626 anchor = *p_anchor;
627 *p_anchor = here;
628 com_addint(c, anchor == 0 ? 0 : here - anchor);
629}
630
631static void
632com_backpatch(c, anchor)
633 struct compiling *c;
634 int anchor; /* Must be nonzero */
635{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000636 unsigned char *code = (unsigned char *) PyString_AsString(c->c_code);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000637 int target = c->c_nexti;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000638 int dist;
639 int prev;
640 for (;;) {
641 /* Make the JUMP instruction at anchor point to target */
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000642 prev = code[anchor] + (code[anchor+1] << 8);
643 dist = target - (anchor+2);
644 code[anchor] = dist & 0xff;
645 code[anchor+1] = dist >> 8;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000646 if (!prev)
647 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000648 anchor -= prev;
649 }
650}
651
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000652/* Handle literals and names uniformly */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000653
654static int
655com_add(c, list, v)
656 struct compiling *c;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000657 PyObject *list;
658 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000659{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000660 int n = PyList_Size(list);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000661 int i;
Guido van Rossumd295f121998-04-09 21:39:57 +0000662 /* XXX This is quadratic in the number of names per compilation unit.
663 XXX Should use a dictionary. */
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000664 for (i = n; --i >= 0; ) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000665 PyObject *w = PyList_GetItem(list, i);
666 if (v->ob_type == w->ob_type && PyObject_Compare(v, w) == 0)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000667 return i;
668 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000669 /* Check for error from PyObject_Compare */
670 if (PyErr_Occurred() || PyList_Append(list, v) != 0)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000671 c->c_errors++;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000672 return n;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000673}
674
675static int
676com_addconst(c, v)
677 struct compiling *c;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000678 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000679{
680 return com_add(c, c->c_consts, v);
681}
682
683static int
684com_addname(c, v)
685 struct compiling *c;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000686 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000687{
688 return com_add(c, c->c_names, v);
689}
690
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000691#ifdef PRIVATE_NAME_MANGLING
692static int
693com_mangle(c, name, buffer, maxlen)
694 struct compiling *c;
695 char *name;
696 char *buffer;
697 int maxlen;
698{
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000699 /* Name mangling: __private becomes _classname__private.
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000700 This is independent from how the name is used. */
701 char *p;
702 int nlen, plen;
703 nlen = strlen(name);
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000704 if (nlen+2 >= maxlen)
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000705 return 0; /* Don't mangle __extremely_long_names */
706 if (name[nlen-1] == '_' && name[nlen-2] == '_')
707 return 0; /* Don't mangle __whatever__ */
708 p = c->c_private;
709 /* Strip leading underscores from class name */
710 while (*p == '_')
711 p++;
712 if (*p == '\0')
713 return 0; /* Don't mangle if class is just underscores */
714 plen = strlen(p);
715 if (plen + nlen >= maxlen)
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000716 plen = maxlen-nlen-2; /* Truncate class name if too long */
717 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000718 buffer[0] = '_';
719 strncpy(buffer+1, p, plen);
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000720 strcpy(buffer+1+plen, name);
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000721 /* fprintf(stderr, "mangle %s -> %s\n", name, buffer); */
722 return 1;
723}
724#endif
725
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000726static void
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000727com_addopnamestr(c, op, name)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000728 struct compiling *c;
729 int op;
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000730 char *name;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000731{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000732 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000733 int i;
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000734#ifdef PRIVATE_NAME_MANGLING
735 char buffer[256];
736 if (name != NULL && name[0] == '_' && name[1] == '_' &&
737 c->c_private != NULL &&
738 com_mangle(c, name, buffer, (int)sizeof(buffer)))
739 name = buffer;
740#endif
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000741 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000742 c->c_errors++;
743 i = 255;
744 }
745 else {
746 i = com_addname(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000747 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000748 }
Guido van Rossumc5e96291991-12-10 13:53:51 +0000749 /* Hack to replace *_NAME opcodes by *_GLOBAL if necessary */
750 switch (op) {
751 case LOAD_NAME:
752 case STORE_NAME:
753 case DELETE_NAME:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000754 if (PyDict_GetItemString(c->c_globals, name) != NULL) {
Guido van Rossumc5e96291991-12-10 13:53:51 +0000755 switch (op) {
756 case LOAD_NAME: op = LOAD_GLOBAL; break;
757 case STORE_NAME: op = STORE_GLOBAL; break;
758 case DELETE_NAME: op = DELETE_GLOBAL; break;
759 }
760 }
761 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000762 com_addoparg(c, op, i);
763}
764
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000765static void
766com_addopname(c, op, n)
767 struct compiling *c;
768 int op;
769 node *n;
770{
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000771 char *name;
772 char buffer[1000];
773 /* XXX it is possible to write this code without the 1000
774 chars on the total length of dotted names, I just can't be
775 bothered right now */
776 if (TYPE(n) == STAR)
777 name = "*";
778 else if (TYPE(n) == dotted_name) {
779 char *p = buffer;
780 int i;
781 name = buffer;
782 for (i = 0; i < NCH(n); i += 2) {
783 char *s = STR(CHILD(n, i));
784 if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000785 com_error(c, PyExc_MemoryError,
Guido van Rossum452a9831996-09-17 14:32:04 +0000786 "dotted_name too long");
Guido van Rossumd9dfaf51995-02-17 15:04:57 +0000787 name = NULL;
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000788 break;
789 }
790 if (p != buffer)
791 *p++ = '.';
792 strcpy(p, s);
793 p = strchr(p, '\0');
794 }
795 }
796 else {
797 REQ(n, NAME);
798 name = STR(n);
799 }
800 com_addopnamestr(c, op, name);
801}
802
Guido van Rossum79f25d91997-04-29 20:08:16 +0000803static PyObject *
Guido van Rossum452a9831996-09-17 14:32:04 +0000804parsenumber(co, s)
805 struct compiling *co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000806 char *s;
807{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000808 extern long PyOS_strtol Py_PROTO((const char *, char **, int));
809 extern unsigned long PyOS_strtoul Py_PROTO((const char *,
810 char **, int));
811 extern double atof Py_PROTO((const char *));
Guido van Rossumc5e96291991-12-10 13:53:51 +0000812 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000813 long x;
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000814 double dx;
Guido van Rossum50564e81996-01-12 01:13:16 +0000815#ifndef WITHOUT_COMPLEX
Guido van Rossum530956d1996-07-21 02:27:43 +0000816 Py_complex c;
Guido van Rossum50564e81996-01-12 01:13:16 +0000817 int imflag;
818#endif
819
Guido van Rossum282914b1991-04-04 10:42:56 +0000820 errno = 0;
Guido van Rossumc5e96291991-12-10 13:53:51 +0000821 end = s + strlen(s) - 1;
Guido van Rossum50564e81996-01-12 01:13:16 +0000822#ifndef WITHOUT_COMPLEX
Guido van Rossum15ad9a61996-01-26 20:53:56 +0000823 imflag = *end == 'j' || *end == 'J';
Guido van Rossum50564e81996-01-12 01:13:16 +0000824#endif
Guido van Rossumf1aeab71992-03-27 17:28:26 +0000825 if (*end == 'l' || *end == 'L')
Guido van Rossum79f25d91997-04-29 20:08:16 +0000826 return PyLong_FromString(s, (char **)0, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +0000827 if (s[0] == '0')
Guido van Rossum79f25d91997-04-29 20:08:16 +0000828 x = (long) PyOS_strtoul(s, &end, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +0000829 else
Guido van Rossum79f25d91997-04-29 20:08:16 +0000830 x = PyOS_strtol(s, &end, 0);
Guido van Rossum282914b1991-04-04 10:42:56 +0000831 if (*end == '\0') {
832 if (errno != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000833 com_error(co, PyExc_OverflowError,
Guido van Rossum452a9831996-09-17 14:32:04 +0000834 "integer literal too large");
Guido van Rossum282914b1991-04-04 10:42:56 +0000835 return NULL;
836 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000837 return PyInt_FromLong(x);
Guido van Rossum282914b1991-04-04 10:42:56 +0000838 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000839 /* XXX Huge floats may silently fail */
Guido van Rossum50564e81996-01-12 01:13:16 +0000840#ifndef WITHOUT_COMPLEX
841 if (imflag) {
842 c.real = 0.;
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000843 PyFPE_START_PROTECT("atof", return 0)
Guido van Rossum50564e81996-01-12 01:13:16 +0000844 c.imag = atof(s);
Guido van Rossum45b83911997-03-14 04:32:50 +0000845 PyFPE_END_PROTECT(c)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000846 return PyComplex_FromCComplex(c);
Guido van Rossum50564e81996-01-12 01:13:16 +0000847 }
Guido van Rossumac1fc951997-10-08 15:23:55 +0000848 else
Guido van Rossum50564e81996-01-12 01:13:16 +0000849#endif
Guido van Rossumac1fc951997-10-08 15:23:55 +0000850 {
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000851 PyFPE_START_PROTECT("atof", return 0)
852 dx = atof(s);
Guido van Rossum45b83911997-03-14 04:32:50 +0000853 PyFPE_END_PROTECT(dx)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000854 return PyFloat_FromDouble(dx);
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000855 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000856}
857
Guido van Rossum79f25d91997-04-29 20:08:16 +0000858static PyObject *
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000859parsestr(s)
860 char *s;
861{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000862 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000863 int len;
864 char *buf;
865 char *p;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000866 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000867 int c;
Guido van Rossum5f5e8171997-04-06 03:41:40 +0000868 int first = *s;
869 int quote = first;
870 if (isalpha(quote) || quote == '_')
871 quote = *++s;
Guido van Rossum8054fad1993-10-26 15:19:44 +0000872 if (quote != '\'' && quote != '\"') {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000873 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000874 return NULL;
875 }
876 s++;
877 len = strlen(s);
Guido van Rossum8054fad1993-10-26 15:19:44 +0000878 if (s[--len] != quote) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000879 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000880 return NULL;
881 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000882 if (len >= 4 && s[0] == quote && s[1] == quote) {
883 s += 2;
884 len -= 2;
885 if (s[--len] != quote || s[--len] != quote) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000886 PyErr_BadInternalCall();
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000887 return NULL;
888 }
889 }
Guido van Rossum5f5e8171997-04-06 03:41:40 +0000890 if (first != quote || strchr(s, '\\') == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000891 return PyString_FromStringAndSize(s, len);
892 v = PyString_FromStringAndSize((char *)NULL, len);
893 p = buf = PyString_AsString(v);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000894 end = s + len;
895 while (s < end) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000896 if (*s != '\\') {
897 *p++ = *s++;
898 continue;
899 }
900 s++;
901 switch (*s++) {
902 /* XXX This assumes ASCII! */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000903 case '\n': break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000904 case '\\': *p++ = '\\'; break;
905 case '\'': *p++ = '\''; break;
Guido van Rossum8054fad1993-10-26 15:19:44 +0000906 case '\"': *p++ = '\"'; break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000907 case 'b': *p++ = '\b'; break;
908 case 'f': *p++ = '\014'; break; /* FF */
909 case 't': *p++ = '\t'; break;
910 case 'n': *p++ = '\n'; break;
911 case 'r': *p++ = '\r'; break;
912 case 'v': *p++ = '\013'; break; /* VT */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000913 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
914 case '0': case '1': case '2': case '3':
915 case '4': case '5': case '6': case '7':
916 c = s[-1] - '0';
917 if ('0' <= *s && *s <= '7') {
918 c = (c<<3) + *s++ - '0';
919 if ('0' <= *s && *s <= '7')
920 c = (c<<3) + *s++ - '0';
921 }
922 *p++ = c;
923 break;
924 case 'x':
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000925 if (isxdigit(Py_CHARMASK(*s))) {
Guido van Rossumed1100f1997-10-20 23:24:07 +0000926 unsigned int x = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000927 do {
Guido van Rossumed1100f1997-10-20 23:24:07 +0000928 c = Py_CHARMASK(*s);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000929 s++;
Guido van Rossumed1100f1997-10-20 23:24:07 +0000930 x = (x<<4) & ~0xF;
931 if (isdigit(c))
932 x += c - '0';
933 else if (islower(c))
934 x += 10 + c - 'a';
935 else
936 x += 10 + c - 'A';
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000937 } while (isxdigit(Py_CHARMASK(*s)));
Guido van Rossumed1100f1997-10-20 23:24:07 +0000938 *p++ = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000939 break;
940 }
941 /* FALLTHROUGH */
942 default: *p++ = '\\'; *p++ = s[-1]; break;
943 }
944 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000945 _PyString_Resize(&v, (int)(p - buf));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000946 return v;
947}
948
Guido van Rossum79f25d91997-04-29 20:08:16 +0000949static PyObject *
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000950parsestrplus(n)
951 node *n;
952{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000953 PyObject *v;
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000954 int i;
955 REQ(CHILD(n, 0), STRING);
956 if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) {
957 /* String literal concatenation */
958 for (i = 1; i < NCH(n) && v != NULL; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000959 PyString_ConcatAndDel(&v, parsestr(STR(CHILD(n, i))));
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000960 }
961 }
962 return v;
963}
964
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000965static void
966com_list_constructor(c, n)
967 struct compiling *c;
968 node *n;
969{
970 int len;
971 int i;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000972 if (TYPE(n) != testlist)
973 REQ(n, exprlist);
974 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
975 len = (NCH(n) + 1) / 2;
976 for (i = 0; i < NCH(n); i += 2)
977 com_node(c, CHILD(n, i));
978 com_addoparg(c, BUILD_LIST, len);
Guido van Rossum8b993a91997-01-17 21:04:03 +0000979 com_pop(c, len-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000980}
981
982static void
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000983com_dictmaker(c, n)
984 struct compiling *c;
985 node *n;
986{
987 int i;
988 /* dictmaker: test ':' test (',' test ':' value)* [','] */
989 for (i = 0; i+2 < NCH(n); i += 4) {
990 /* We must arrange things just right for STORE_SUBSCR.
991 It wants the stack to look like (value) (dict) (key) */
992 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +0000993 com_push(c, 1);
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000994 com_node(c, CHILD(n, i+2)); /* value */
995 com_addbyte(c, ROT_TWO);
996 com_node(c, CHILD(n, i)); /* key */
997 com_addbyte(c, STORE_SUBSCR);
Guido van Rossum8b993a91997-01-17 21:04:03 +0000998 com_pop(c, 3);
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000999 }
1000}
1001
1002static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001003com_atom(c, n)
1004 struct compiling *c;
1005 node *n;
1006{
1007 node *ch;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001008 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001009 int i;
1010 REQ(n, atom);
1011 ch = CHILD(n, 0);
1012 switch (TYPE(ch)) {
1013 case LPAR:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001014 if (TYPE(CHILD(n, 1)) == RPAR) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001015 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001016 com_push(c, 1);
1017 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001018 else
1019 com_node(c, CHILD(n, 1));
1020 break;
1021 case LSQB:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001022 if (TYPE(CHILD(n, 1)) == RSQB) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001023 com_addoparg(c, BUILD_LIST, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001024 com_push(c, 1);
1025 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001026 else
1027 com_list_constructor(c, CHILD(n, 1));
1028 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001029 case LBRACE: /* '{' [dictmaker] '}' */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001030 com_addoparg(c, BUILD_MAP, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001031 com_push(c, 1);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001032 if (TYPE(CHILD(n, 1)) != RBRACE)
1033 com_dictmaker(c, CHILD(n, 1));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001034 break;
1035 case BACKQUOTE:
1036 com_node(c, CHILD(n, 1));
1037 com_addbyte(c, UNARY_CONVERT);
1038 break;
1039 case NUMBER:
Guido van Rossum452a9831996-09-17 14:32:04 +00001040 if ((v = parsenumber(c, STR(ch))) == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001041 i = 255;
1042 }
1043 else {
1044 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001045 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001046 }
1047 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001048 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001049 break;
1050 case STRING:
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001051 v = parsestrplus(n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001052 if (v == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001053 c->c_errors++;
1054 i = 255;
1055 }
1056 else {
1057 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001058 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001059 }
1060 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001061 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001062 break;
1063 case NAME:
1064 com_addopname(c, LOAD_NAME, ch);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001065 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001066 break;
1067 default:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001068 /* XXX fprintf(stderr, "node type %d\n", TYPE(ch)); */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001069 com_error(c, PyExc_SystemError,
1070 "com_atom: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001071 }
1072}
1073
1074static void
1075com_slice(c, n, op)
1076 struct compiling *c;
1077 node *n;
1078 int op;
1079{
1080 if (NCH(n) == 1) {
1081 com_addbyte(c, op);
1082 }
1083 else if (NCH(n) == 2) {
1084 if (TYPE(CHILD(n, 0)) != COLON) {
1085 com_node(c, CHILD(n, 0));
1086 com_addbyte(c, op+1);
1087 }
1088 else {
1089 com_node(c, CHILD(n, 1));
1090 com_addbyte(c, op+2);
1091 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00001092 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001093 }
1094 else {
1095 com_node(c, CHILD(n, 0));
1096 com_node(c, CHILD(n, 2));
1097 com_addbyte(c, op+3);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001098 com_pop(c, 2);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001099 }
1100}
1101
Guido van Rossum635abd21997-01-06 22:56:52 +00001102static void
1103com_argument(c, n, pkeywords)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001104 struct compiling *c;
1105 node *n; /* argument */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001106 PyObject **pkeywords;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001107{
1108 node *m;
Guido van Rossum8b993a91997-01-17 21:04:03 +00001109 REQ(n, argument); /* [test '='] test; really [keyword '='] test */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001110 if (NCH(n) == 1) {
Guido van Rossum635abd21997-01-06 22:56:52 +00001111 if (*pkeywords != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001112 com_error(c, PyExc_SyntaxError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001113 "non-keyword arg after keyword arg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001114 }
1115 else {
1116 com_node(c, CHILD(n, 0));
1117 }
Guido van Rossum635abd21997-01-06 22:56:52 +00001118 return;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001119 }
1120 m = n;
1121 do {
1122 m = CHILD(m, 0);
1123 } while (NCH(m) == 1);
1124 if (TYPE(m) != NAME) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001125 com_error(c, PyExc_SyntaxError,
1126 "keyword can't be an expression");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001127 }
1128 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001129 PyObject *v = PyString_InternFromString(STR(m));
Guido van Rossum635abd21997-01-06 22:56:52 +00001130 if (v != NULL && *pkeywords == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001131 *pkeywords = PyDict_New();
Guido van Rossum635abd21997-01-06 22:56:52 +00001132 if (v == NULL || *pkeywords == NULL)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001133 c->c_errors++;
1134 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001135 if (PyDict_GetItem(*pkeywords, v) != NULL)
1136 com_error(c, PyExc_SyntaxError,
Guido van Rossum635abd21997-01-06 22:56:52 +00001137 "duplicate keyword argument");
1138 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001139 if (PyDict_SetItem(*pkeywords, v, v) != 0)
Guido van Rossum635abd21997-01-06 22:56:52 +00001140 c->c_errors++;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001141 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001142 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001143 Py_DECREF(v);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001144 }
1145 }
1146 com_node(c, CHILD(n, 2));
Guido van Rossumf10570b1995-07-07 22:53:21 +00001147}
1148
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001149static void
1150com_call_function(c, n)
1151 struct compiling *c;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001152 node *n; /* EITHER arglist OR ')' */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001153{
1154 if (TYPE(n) == RPAR) {
Guido van Rossumf10570b1995-07-07 22:53:21 +00001155 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001156 }
1157 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001158 PyObject *keywords = NULL;
Guido van Rossum635abd21997-01-06 22:56:52 +00001159 int i, na, nk;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001160 REQ(n, arglist);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001161 na = 0;
1162 nk = 0;
1163 for (i = 0; i < NCH(n); i += 2) {
Guido van Rossum635abd21997-01-06 22:56:52 +00001164 com_argument(c, CHILD(n, i), &keywords);
1165 if (keywords == NULL)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001166 na++;
1167 else
1168 nk++;
1169 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001170 Py_XDECREF(keywords);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001171 if (na > 255 || nk > 255) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001172 com_error(c, PyExc_SyntaxError,
1173 "more than 255 arguments");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001174 }
1175 com_addoparg(c, CALL_FUNCTION, na | (nk << 8));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001176 com_pop(c, na + 2*nk);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001177 }
1178}
1179
1180static void
1181com_select_member(c, n)
1182 struct compiling *c;
1183 node *n;
1184{
1185 com_addopname(c, LOAD_ATTR, n);
1186}
1187
1188static void
Guido van Rossum8861b741996-07-30 16:49:37 +00001189com_sliceobj(c, n)
1190 struct compiling *c;
1191 node *n;
1192{
1193 int i=0;
1194 int ns=2; /* number of slice arguments */
Guido van Rossum8861b741996-07-30 16:49:37 +00001195 node *ch;
1196
1197 /* first argument */
1198 if (TYPE(CHILD(n,i)) == COLON) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001199 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001200 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001201 i++;
1202 }
1203 else {
1204 com_node(c, CHILD(n,i));
1205 i++;
1206 REQ(CHILD(n,i),COLON);
1207 i++;
1208 }
1209 /* second argument */
1210 if (i < NCH(n) && TYPE(CHILD(n,i)) == test) {
1211 com_node(c, CHILD(n,i));
1212 i++;
1213 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00001214 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001215 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001216 com_push(c, 1);
1217 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001218 /* remaining arguments */
1219 for (; i < NCH(n); i++) {
1220 ns++;
1221 ch=CHILD(n,i);
1222 REQ(ch, sliceop);
1223 if (NCH(ch) == 1) {
1224 /* right argument of ':' missing */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001225 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001226 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001227 }
1228 else
1229 com_node(c, CHILD(ch,1));
1230 }
1231 com_addoparg(c, BUILD_SLICE, ns);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001232 com_pop(c, 1 + (ns == 3));
Guido van Rossum8861b741996-07-30 16:49:37 +00001233}
1234
1235static void
1236com_subscript(c, n)
1237 struct compiling *c;
1238 node *n;
1239{
1240 node *ch;
1241 REQ(n, subscript);
1242 ch = CHILD(n,0);
1243 /* check for rubber index */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001244 if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
Guido van Rossume449af71996-10-11 16:25:41 +00001245 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001246 com_push(c, 1);
1247 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001248 else {
1249 /* check for slice */
1250 if ((TYPE(ch) == COLON || NCH(n) > 1))
1251 com_sliceobj(c, n);
1252 else {
1253 REQ(ch, test);
1254 com_node(c, ch);
1255 }
1256 }
1257}
1258
1259static void
1260com_subscriptlist(c, n, assigning)
1261 struct compiling *c;
1262 node *n;
1263 int assigning;
1264{
1265 int i, op;
1266 REQ(n, subscriptlist);
1267 /* Check to make backward compatible slice behavior for '[i:j]' */
1268 if (NCH(n) == 1) {
1269 node *sub = CHILD(n, 0); /* subscript */
1270 /* Make it is a simple slice.
Guido van Rossum8b993a91997-01-17 21:04:03 +00001271 Should have exactly one colon. */
Guido van Rossum8861b741996-07-30 16:49:37 +00001272 if ((TYPE(CHILD(sub, 0)) == COLON
1273 || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
Guido van Rossum8b993a91997-01-17 21:04:03 +00001274 && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
1275 {
Guido van Rossum8861b741996-07-30 16:49:37 +00001276 if (assigning == OP_APPLY)
1277 op = SLICE;
1278 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001279 op = ((assigning == OP_ASSIGN) ?
1280 STORE_SLICE : DELETE_SLICE);
Guido van Rossum8861b741996-07-30 16:49:37 +00001281 com_slice(c, sub, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001282 if (op == STORE_SLICE)
1283 com_pop(c, 2);
1284 else if (op == DELETE_SLICE)
1285 com_pop(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001286 return;
1287 }
1288 }
1289 /* Else normal subscriptlist. Compile each subscript. */
1290 for (i = 0; i < NCH(n); i += 2)
1291 com_subscript(c, CHILD(n, i));
1292 /* Put multiple subscripts into a tuple */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001293 if (NCH(n) > 1) {
1294 i = (NCH(n)+1) / 2;
1295 com_addoparg(c, BUILD_TUPLE, i);
1296 com_pop(c, i-1);
1297 }
1298 if (assigning == OP_APPLY) {
Guido van Rossum8861b741996-07-30 16:49:37 +00001299 op = BINARY_SUBSCR;
Guido van Rossum8b993a91997-01-17 21:04:03 +00001300 i = 1;
1301 }
1302 else if (assigning == OP_ASSIGN) {
1303 op = STORE_SUBSCR;
1304 i = 3;
1305 }
1306 else {
1307 op = DELETE_SUBSCR;
1308 i = 2;
1309 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001310 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001311 com_pop(c, i);
Guido van Rossum8861b741996-07-30 16:49:37 +00001312}
1313
1314static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001315com_apply_trailer(c, n)
1316 struct compiling *c;
1317 node *n;
1318{
1319 REQ(n, trailer);
1320 switch (TYPE(CHILD(n, 0))) {
1321 case LPAR:
1322 com_call_function(c, CHILD(n, 1));
1323 break;
1324 case DOT:
1325 com_select_member(c, CHILD(n, 1));
1326 break;
1327 case LSQB:
Guido van Rossum8861b741996-07-30 16:49:37 +00001328 com_subscriptlist(c, CHILD(n, 1), OP_APPLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001329 break;
1330 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001331 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001332 "com_apply_trailer: unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001333 }
1334}
1335
1336static void
Guido van Rossum50564e81996-01-12 01:13:16 +00001337com_power(c, n)
1338 struct compiling *c;
1339 node *n;
1340{
1341 int i;
1342 REQ(n, power);
1343 com_atom(c, CHILD(n, 0));
1344 for (i = 1; i < NCH(n); i++) {
1345 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
1346 com_factor(c, CHILD(n, i+1));
1347 com_addbyte(c, BINARY_POWER);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001348 com_pop(c, 1);
Guido van Rossum50564e81996-01-12 01:13:16 +00001349 break;
1350 }
1351 else
1352 com_apply_trailer(c, CHILD(n, i));
1353 }
1354}
1355
1356static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001357com_factor(c, n)
1358 struct compiling *c;
1359 node *n;
1360{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001361 REQ(n, factor);
1362 if (TYPE(CHILD(n, 0)) == PLUS) {
1363 com_factor(c, CHILD(n, 1));
1364 com_addbyte(c, UNARY_POSITIVE);
1365 }
1366 else if (TYPE(CHILD(n, 0)) == MINUS) {
1367 com_factor(c, CHILD(n, 1));
1368 com_addbyte(c, UNARY_NEGATIVE);
1369 }
Guido van Rossum7928cd71991-10-24 14:59:31 +00001370 else if (TYPE(CHILD(n, 0)) == TILDE) {
1371 com_factor(c, CHILD(n, 1));
1372 com_addbyte(c, UNARY_INVERT);
1373 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001374 else {
Guido van Rossum50564e81996-01-12 01:13:16 +00001375 com_power(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001376 }
1377}
1378
1379static void
1380com_term(c, n)
1381 struct compiling *c;
1382 node *n;
1383{
1384 int i;
1385 int op;
1386 REQ(n, term);
1387 com_factor(c, CHILD(n, 0));
1388 for (i = 2; i < NCH(n); i += 2) {
1389 com_factor(c, CHILD(n, i));
1390 switch (TYPE(CHILD(n, i-1))) {
1391 case STAR:
1392 op = BINARY_MULTIPLY;
1393 break;
1394 case SLASH:
1395 op = BINARY_DIVIDE;
1396 break;
1397 case PERCENT:
1398 op = BINARY_MODULO;
1399 break;
1400 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001401 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001402 "com_term: operator not *, / or %");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001403 op = 255;
1404 }
1405 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001406 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001407 }
1408}
1409
1410static void
1411com_arith_expr(c, n)
1412 struct compiling *c;
1413 node *n;
1414{
1415 int i;
1416 int op;
1417 REQ(n, arith_expr);
1418 com_term(c, CHILD(n, 0));
1419 for (i = 2; i < NCH(n); i += 2) {
1420 com_term(c, CHILD(n, i));
1421 switch (TYPE(CHILD(n, i-1))) {
1422 case PLUS:
1423 op = BINARY_ADD;
1424 break;
1425 case MINUS:
1426 op = BINARY_SUBTRACT;
1427 break;
1428 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001429 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001430 "com_arith_expr: operator not + or -");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001431 op = 255;
1432 }
1433 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001434 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001435 }
1436}
1437
1438static void
1439com_shift_expr(c, n)
1440 struct compiling *c;
1441 node *n;
1442{
1443 int i;
1444 int op;
1445 REQ(n, shift_expr);
1446 com_arith_expr(c, CHILD(n, 0));
1447 for (i = 2; i < NCH(n); i += 2) {
1448 com_arith_expr(c, CHILD(n, i));
1449 switch (TYPE(CHILD(n, i-1))) {
1450 case LEFTSHIFT:
1451 op = BINARY_LSHIFT;
1452 break;
1453 case RIGHTSHIFT:
1454 op = BINARY_RSHIFT;
1455 break;
1456 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001457 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001458 "com_shift_expr: operator not << or >>");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001459 op = 255;
1460 }
1461 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001462 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001463 }
1464}
1465
1466static void
1467com_and_expr(c, n)
1468 struct compiling *c;
1469 node *n;
1470{
1471 int i;
1472 int op;
1473 REQ(n, and_expr);
1474 com_shift_expr(c, CHILD(n, 0));
1475 for (i = 2; i < NCH(n); i += 2) {
1476 com_shift_expr(c, CHILD(n, i));
1477 if (TYPE(CHILD(n, i-1)) == AMPER) {
1478 op = BINARY_AND;
1479 }
1480 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001481 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001482 "com_and_expr: operator not &");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001483 op = 255;
1484 }
1485 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001486 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001487 }
1488}
1489
1490static void
1491com_xor_expr(c, n)
1492 struct compiling *c;
1493 node *n;
1494{
1495 int i;
1496 int op;
1497 REQ(n, xor_expr);
1498 com_and_expr(c, CHILD(n, 0));
1499 for (i = 2; i < NCH(n); i += 2) {
1500 com_and_expr(c, CHILD(n, i));
1501 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
1502 op = BINARY_XOR;
1503 }
1504 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001505 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001506 "com_xor_expr: operator not ^");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001507 op = 255;
1508 }
1509 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001510 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001511 }
1512}
1513
1514static void
1515com_expr(c, n)
1516 struct compiling *c;
1517 node *n;
1518{
1519 int i;
1520 int op;
1521 REQ(n, expr);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001522 com_xor_expr(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001523 for (i = 2; i < NCH(n); i += 2) {
Guido van Rossum7928cd71991-10-24 14:59:31 +00001524 com_xor_expr(c, CHILD(n, i));
1525 if (TYPE(CHILD(n, i-1)) == VBAR) {
1526 op = BINARY_OR;
1527 }
1528 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001529 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001530 "com_expr: expr operator not |");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001531 op = 255;
1532 }
1533 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001534 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001535 }
1536}
1537
1538static enum cmp_op
1539cmp_type(n)
1540 node *n;
1541{
1542 REQ(n, comp_op);
Guido van Rossum01cfd441991-10-20 20:12:38 +00001543 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001544 | 'in' | 'not' 'in' | 'is' | 'is' not' */
1545 if (NCH(n) == 1) {
1546 n = CHILD(n, 0);
1547 switch (TYPE(n)) {
1548 case LESS: return LT;
1549 case GREATER: return GT;
Guido van Rossum01cfd441991-10-20 20:12:38 +00001550 case EQEQUAL: /* == */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001551 case EQUAL: return EQ;
Guido van Rossum01cfd441991-10-20 20:12:38 +00001552 case LESSEQUAL: return LE;
1553 case GREATEREQUAL: return GE;
1554 case NOTEQUAL: return NE; /* <> or != */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001555 case NAME: if (strcmp(STR(n), "in") == 0) return IN;
1556 if (strcmp(STR(n), "is") == 0) return IS;
1557 }
1558 }
1559 else if (NCH(n) == 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001560 switch (TYPE(CHILD(n, 0))) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001561 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1562 return NOT_IN;
1563 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1564 return IS_NOT;
1565 }
1566 }
1567 return BAD;
1568}
1569
1570static void
1571com_comparison(c, n)
1572 struct compiling *c;
1573 node *n;
1574{
1575 int i;
1576 enum cmp_op op;
1577 int anchor;
1578 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
1579 com_expr(c, CHILD(n, 0));
1580 if (NCH(n) == 1)
1581 return;
1582
1583 /****************************************************************
1584 The following code is generated for all but the last
1585 comparison in a chain:
1586
1587 label: on stack: opcode: jump to:
1588
1589 a <code to load b>
1590 a, b DUP_TOP
1591 a, b, b ROT_THREE
1592 b, a, b COMPARE_OP
1593 b, 0-or-1 JUMP_IF_FALSE L1
1594 b, 1 POP_TOP
1595 b
1596
1597 We are now ready to repeat this sequence for the next
1598 comparison in the chain.
1599
1600 For the last we generate:
1601
1602 b <code to load c>
1603 b, c COMPARE_OP
1604 0-or-1
1605
1606 If there were any jumps to L1 (i.e., there was more than one
1607 comparison), we generate:
1608
1609 0-or-1 JUMP_FORWARD L2
1610 L1: b, 0 ROT_TWO
1611 0, b POP_TOP
1612 0
Guido van Rossum8b993a91997-01-17 21:04:03 +00001613 L2: 0-or-1
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001614 ****************************************************************/
1615
1616 anchor = 0;
1617
1618 for (i = 2; i < NCH(n); i += 2) {
1619 com_expr(c, CHILD(n, i));
1620 if (i+2 < NCH(n)) {
1621 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001622 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001623 com_addbyte(c, ROT_THREE);
1624 }
1625 op = cmp_type(CHILD(n, i-1));
1626 if (op == BAD) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001627 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001628 "com_comparison: unknown comparison op");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001629 }
1630 com_addoparg(c, COMPARE_OP, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001631 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001632 if (i+2 < NCH(n)) {
1633 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1634 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001635 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001636 }
1637 }
1638
1639 if (anchor) {
1640 int anchor2 = 0;
1641 com_addfwref(c, JUMP_FORWARD, &anchor2);
1642 com_backpatch(c, anchor);
1643 com_addbyte(c, ROT_TWO);
1644 com_addbyte(c, POP_TOP);
1645 com_backpatch(c, anchor2);
1646 }
1647}
1648
1649static void
1650com_not_test(c, n)
1651 struct compiling *c;
1652 node *n;
1653{
1654 REQ(n, not_test); /* 'not' not_test | comparison */
1655 if (NCH(n) == 1) {
1656 com_comparison(c, CHILD(n, 0));
1657 }
1658 else {
1659 com_not_test(c, CHILD(n, 1));
1660 com_addbyte(c, UNARY_NOT);
1661 }
1662}
1663
1664static void
1665com_and_test(c, n)
1666 struct compiling *c;
1667 node *n;
1668{
1669 int i;
1670 int anchor;
1671 REQ(n, and_test); /* not_test ('and' not_test)* */
1672 anchor = 0;
1673 i = 0;
1674 for (;;) {
1675 com_not_test(c, CHILD(n, i));
1676 if ((i += 2) >= NCH(n))
1677 break;
1678 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1679 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001680 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001681 }
1682 if (anchor)
1683 com_backpatch(c, anchor);
1684}
1685
1686static void
1687com_test(c, n)
1688 struct compiling *c;
1689 node *n;
1690{
Guido van Rossum8b993a91997-01-17 21:04:03 +00001691 REQ(n, test); /* and_test ('or' and_test)* | lambdef */
Guido van Rossum57531fe1993-11-30 14:57:42 +00001692 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001693 PyObject *v;
Guido van Rossum57531fe1993-11-30 14:57:42 +00001694 int i;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001695 int ndefs = com_argdefs(c, CHILD(n, 0));
Guido van Rossum79f25d91997-04-29 20:08:16 +00001696 v = (PyObject *) icompile(CHILD(n, 0), c);
Guido van Rossum57531fe1993-11-30 14:57:42 +00001697 if (v == NULL) {
1698 c->c_errors++;
1699 i = 255;
1700 }
1701 else {
1702 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001703 Py_DECREF(v);
Guido van Rossum57531fe1993-11-30 14:57:42 +00001704 }
1705 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001706 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001707 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001708 com_pop(c, ndefs);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001709 }
Guido van Rossum57531fe1993-11-30 14:57:42 +00001710 else {
1711 int anchor = 0;
1712 int i = 0;
1713 for (;;) {
1714 com_and_test(c, CHILD(n, i));
1715 if ((i += 2) >= NCH(n))
1716 break;
1717 com_addfwref(c, JUMP_IF_TRUE, &anchor);
1718 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001719 com_pop(c, 1);
Guido van Rossum57531fe1993-11-30 14:57:42 +00001720 }
1721 if (anchor)
1722 com_backpatch(c, anchor);
1723 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001724}
1725
1726static void
Guido van Rossum288a60f1991-12-16 13:05:10 +00001727com_list(c, n, toplevel)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001728 struct compiling *c;
1729 node *n;
Guido van Rossum288a60f1991-12-16 13:05:10 +00001730 int toplevel; /* If nonzero, *always* build a tuple */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001731{
1732 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
Guido van Rossum288a60f1991-12-16 13:05:10 +00001733 if (NCH(n) == 1 && !toplevel) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001734 com_node(c, CHILD(n, 0));
1735 }
1736 else {
1737 int i;
1738 int len;
1739 len = (NCH(n) + 1) / 2;
1740 for (i = 0; i < NCH(n); i += 2)
1741 com_node(c, CHILD(n, i));
1742 com_addoparg(c, BUILD_TUPLE, len);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001743 com_pop(c, len-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001744 }
1745}
1746
1747
1748/* Begin of assignment compilation */
1749
Guido van Rossum79f25d91997-04-29 20:08:16 +00001750static void com_assign_name Py_PROTO((struct compiling *, node *, int));
1751static void com_assign Py_PROTO((struct compiling *, node *, int));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001752
1753static void
1754com_assign_attr(c, n, assigning)
1755 struct compiling *c;
1756 node *n;
1757 int assigning;
1758{
1759 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001760 com_pop(c, assigning ? 2 : 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001761}
1762
1763static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001764com_assign_trailer(c, n, assigning)
1765 struct compiling *c;
1766 node *n;
1767 int assigning;
1768{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001769 REQ(n, trailer);
1770 switch (TYPE(CHILD(n, 0))) {
1771 case LPAR: /* '(' [exprlist] ')' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001772 com_error(c, PyExc_SyntaxError,
1773 "can't assign to function call");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001774 break;
1775 case DOT: /* '.' NAME */
1776 com_assign_attr(c, CHILD(n, 1), assigning);
1777 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00001778 case LSQB: /* '[' subscriptlist ']' */
1779 com_subscriptlist(c, CHILD(n, 1), assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001780 break;
1781 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001782 com_error(c, PyExc_SystemError, "unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001783 }
1784}
1785
1786static void
1787com_assign_tuple(c, n, assigning)
1788 struct compiling *c;
1789 node *n;
1790 int assigning;
1791{
1792 int i;
1793 if (TYPE(n) != testlist)
1794 REQ(n, exprlist);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001795 if (assigning) {
1796 i = (NCH(n)+1)/2;
1797 com_addoparg(c, UNPACK_TUPLE, i);
1798 com_push(c, i-1);
1799 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001800 for (i = 0; i < NCH(n); i += 2)
1801 com_assign(c, CHILD(n, i), assigning);
1802}
1803
1804static void
1805com_assign_list(c, n, assigning)
1806 struct compiling *c;
1807 node *n;
1808 int assigning;
1809{
1810 int i;
Guido van Rossum8b993a91997-01-17 21:04:03 +00001811 if (assigning) {
1812 i = (NCH(n)+1)/2;
1813 com_addoparg(c, UNPACK_LIST, i);
1814 com_push(c, i-1);
1815 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001816 for (i = 0; i < NCH(n); i += 2)
1817 com_assign(c, CHILD(n, i), assigning);
1818}
1819
1820static void
1821com_assign_name(c, n, assigning)
1822 struct compiling *c;
1823 node *n;
1824 int assigning;
1825{
1826 REQ(n, NAME);
1827 com_addopname(c, assigning ? STORE_NAME : DELETE_NAME, n);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001828 if (assigning)
1829 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001830}
1831
1832static void
1833com_assign(c, n, assigning)
1834 struct compiling *c;
1835 node *n;
1836 int assigning;
1837{
1838 /* Loop to avoid trivial recursion */
1839 for (;;) {
1840 switch (TYPE(n)) {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001841
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001842 case exprlist:
1843 case testlist:
1844 if (NCH(n) > 1) {
1845 com_assign_tuple(c, n, assigning);
1846 return;
1847 }
1848 n = CHILD(n, 0);
1849 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001850
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001851 case test:
1852 case and_test:
1853 case not_test:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001854 case comparison:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001855 case expr:
Guido van Rossum7928cd71991-10-24 14:59:31 +00001856 case xor_expr:
1857 case and_expr:
1858 case shift_expr:
1859 case arith_expr:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001860 case term:
Guido van Rossum50564e81996-01-12 01:13:16 +00001861 case factor:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001862 if (NCH(n) > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001863 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001864 "can't assign to operator");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001865 return;
1866 }
1867 n = CHILD(n, 0);
1868 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001869
Guido van Rossum50564e81996-01-12 01:13:16 +00001870 case power: /* atom trailer* ('**' power)* */
1871/* ('+'|'-'|'~') factor | atom trailer* */
1872 if (TYPE(CHILD(n, 0)) != atom) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001873 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001874 "can't assign to operator");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001875 return;
1876 }
Guido van Rossum50564e81996-01-12 01:13:16 +00001877 if (NCH(n) > 1) { /* trailer or exponent present */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001878 int i;
1879 com_node(c, CHILD(n, 0));
1880 for (i = 1; i+1 < NCH(n); i++) {
Guido van Rossum50564e81996-01-12 01:13:16 +00001881 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001882 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001883 "can't assign to operator");
Guido van Rossum50564e81996-01-12 01:13:16 +00001884 return;
1885 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001886 com_apply_trailer(c, CHILD(n, i));
1887 } /* NB i is still alive */
1888 com_assign_trailer(c,
1889 CHILD(n, i), assigning);
1890 return;
1891 }
1892 n = CHILD(n, 0);
1893 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001894
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001895 case atom:
1896 switch (TYPE(CHILD(n, 0))) {
1897 case LPAR:
1898 n = CHILD(n, 1);
1899 if (TYPE(n) == RPAR) {
1900 /* XXX Should allow () = () ??? */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001901 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001902 "can't assign to ()");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001903 return;
1904 }
1905 break;
1906 case LSQB:
1907 n = CHILD(n, 1);
1908 if (TYPE(n) == RSQB) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001909 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001910 "can't assign to []");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001911 return;
1912 }
1913 com_assign_list(c, n, assigning);
1914 return;
1915 case NAME:
1916 com_assign_name(c, CHILD(n, 0), assigning);
1917 return;
1918 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001919 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001920 "can't assign to literal");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001921 return;
1922 }
1923 break;
Guido van Rossum15cc9a01996-08-08 18:51:04 +00001924
1925 case lambdef:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001926 com_error(c, PyExc_SyntaxError,
1927 "can't assign to lambda");
Guido van Rossum15cc9a01996-08-08 18:51:04 +00001928 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001929
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001930 default:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001931 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001932 com_error(c, PyExc_SystemError,
1933 "com_assign: bad node");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001934 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001935
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001936 }
1937 }
1938}
Guido van Rossum7c531111997-03-11 18:42:21 +00001939
Guido van Rossum79f25d91997-04-29 20:08:16 +00001940/* Forward */ static node *get_rawdocstring Py_PROTO((node *));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001941
1942static void
1943com_expr_stmt(c, n)
1944 struct compiling *c;
1945 node *n;
1946{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001947 REQ(n, expr_stmt); /* testlist ('=' testlist)* */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001948 /* Forget it if we have just a doc string here */
Guido van Rossum5f5e8171997-04-06 03:41:40 +00001949 if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
Guido van Rossum8b993a91997-01-17 21:04:03 +00001950 return;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001951 com_node(c, CHILD(n, NCH(n)-1));
1952 if (NCH(n) == 1) {
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001953 if (c->c_interactive)
1954 com_addbyte(c, PRINT_EXPR);
1955 else
1956 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001957 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001958 }
1959 else {
1960 int i;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001961 for (i = 0; i < NCH(n)-2; i+=2) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00001962 if (i+2 < NCH(n)-2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001963 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001964 com_push(c, 1);
1965 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001966 com_assign(c, CHILD(n, i), OP_ASSIGN);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001967 }
1968 }
1969}
1970
1971static void
Guido van Rossum228d7f31997-04-02 05:24:36 +00001972com_assert_stmt(c, n)
1973 struct compiling *c;
1974 node *n;
1975{
1976 int a = 0, b = 0;
1977 int i;
1978 REQ(n, assert_stmt); /* 'assert' test [',' test] */
1979 /* Generate code like for
1980
1981 if __debug__:
1982 if not <test>:
1983 raise AssertionError [, <message>]
1984
1985 where <message> is the second test, if present.
1986 */
1987 if (Py_OptimizeFlag)
1988 return;
1989 com_addopnamestr(c, LOAD_GLOBAL, "__debug__");
1990 com_push(c, 1);
1991 com_addfwref(c, JUMP_IF_FALSE, &a);
1992 com_addbyte(c, POP_TOP);
1993 com_pop(c, 1);
1994 com_node(c, CHILD(n, 1));
1995 com_addfwref(c, JUMP_IF_TRUE, &b);
1996 com_addbyte(c, POP_TOP);
1997 com_pop(c, 1);
1998 /* Raise that exception! */
1999 com_addopnamestr(c, LOAD_GLOBAL, "AssertionError");
2000 com_push(c, 1);
2001 i = NCH(n)/2; /* Either 2 or 4 */
2002 if (i > 1)
2003 com_node(c, CHILD(n, 3));
2004 com_addoparg(c, RAISE_VARARGS, i);
2005 com_pop(c, i);
2006 /* The interpreter does not fall through */
2007 /* All jumps converge here */
2008 com_backpatch(c, a);
2009 com_backpatch(c, b);
2010 com_addbyte(c, POP_TOP);
2011}
2012
2013static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002014com_print_stmt(c, n)
2015 struct compiling *c;
2016 node *n;
2017{
2018 int i;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002019 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
2020 for (i = 1; i < NCH(n); i += 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002021 com_node(c, CHILD(n, i));
2022 com_addbyte(c, PRINT_ITEM);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002023 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002024 }
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002025 if (TYPE(CHILD(n, NCH(n)-1)) != COMMA)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002026 com_addbyte(c, PRINT_NEWLINE);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002027 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002028}
2029
2030static void
2031com_return_stmt(c, n)
2032 struct compiling *c;
2033 node *n;
2034{
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002035 REQ(n, return_stmt); /* 'return' [testlist] */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002036 if (!c->c_infunction) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002037 com_error(c, PyExc_SyntaxError, "'return' outside function");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002038 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002039 if (NCH(n) < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002040 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002041 com_push(c, 1);
2042 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002043 else
2044 com_node(c, CHILD(n, 1));
2045 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002046 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002047}
2048
2049static void
2050com_raise_stmt(c, n)
2051 struct compiling *c;
2052 node *n;
2053{
Guido van Rossum8b993a91997-01-17 21:04:03 +00002054 int i;
Guido van Rossumd295f121998-04-09 21:39:57 +00002055 REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */
2056 if (NCH(n) > 1) {
2057 com_node(c, CHILD(n, 1));
2058 if (NCH(n) > 3) {
2059 com_node(c, CHILD(n, 3));
2060 if (NCH(n) > 5)
2061 com_node(c, CHILD(n, 5));
2062 }
Guido van Rossumf10570b1995-07-07 22:53:21 +00002063 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002064 i = NCH(n)/2;
2065 com_addoparg(c, RAISE_VARARGS, i);
2066 com_pop(c, i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002067}
2068
2069static void
2070com_import_stmt(c, n)
2071 struct compiling *c;
2072 node *n;
2073{
2074 int i;
2075 REQ(n, import_stmt);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002076 /* 'import' dotted_name (',' dotted_name)* |
2077 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002078 if (STR(CHILD(n, 0))[0] == 'f') {
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002079 /* 'from' dotted_name 'import' ... */
2080 REQ(CHILD(n, 1), dotted_name);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002081 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002082 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002083 for (i = 3; i < NCH(n); i += 2)
2084 com_addopname(c, IMPORT_FROM, CHILD(n, i));
2085 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002086 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002087 }
2088 else {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002089 /* 'import' ... */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002090 for (i = 1; i < NCH(n); i += 2) {
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002091 REQ(CHILD(n, i), dotted_name);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002092 com_addopname(c, IMPORT_NAME, CHILD(n, i));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002093 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002094 com_addopname(c, STORE_NAME, CHILD(CHILD(n, i), 0));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002095 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002096 }
2097 }
2098}
2099
2100static void
Guido van Rossumc5e96291991-12-10 13:53:51 +00002101com_global_stmt(c, n)
2102 struct compiling *c;
2103 node *n;
2104{
2105 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00002106 REQ(n, global_stmt);
2107 /* 'global' NAME (',' NAME)* */
2108 for (i = 1; i < NCH(n); i += 2) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002109 char *s = STR(CHILD(n, i));
Guido van Rossumfe2236f1996-08-24 07:29:04 +00002110#ifdef PRIVATE_NAME_MANGLING
2111 char buffer[256];
2112 if (s != NULL && s[0] == '_' && s[1] == '_' &&
2113 c->c_private != NULL &&
2114 com_mangle(c, s, buffer, (int)sizeof(buffer)))
2115 s = buffer;
2116#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00002117 if (PyDict_GetItemString(c->c_locals, s) != NULL) {
2118 com_error(c, PyExc_SyntaxError,
2119 "name is local and global");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002120 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002121 else if (PyDict_SetItemString(c->c_globals, s, Py_None) != 0)
Guido van Rossumc5e96291991-12-10 13:53:51 +00002122 c->c_errors++;
2123 }
2124}
2125
Guido van Rossum681d79a1995-07-18 14:51:37 +00002126static int
2127com_newlocal_o(c, nameval)
2128 struct compiling *c;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002129 PyObject *nameval;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002130{
2131 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002132 PyObject *ival;
2133 if (PyList_Size(c->c_varnames) != c->c_nlocals) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002134 /* This is usually caused by an error on a previous call */
2135 if (c->c_errors == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002136 com_error(c, PyExc_SystemError,
2137 "mixed up var name/index");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002138 }
2139 return 0;
2140 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002141 ival = PyInt_FromLong(i = c->c_nlocals++);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002142 if (ival == NULL)
2143 c->c_errors++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002144 else if (PyDict_SetItem(c->c_locals, nameval, ival) != 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002145 c->c_errors++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002146 else if (PyList_Append(c->c_varnames, nameval) != 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002147 c->c_errors++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002148 Py_XDECREF(ival);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002149 return i;
2150}
2151
2152static int
2153com_addlocal_o(c, nameval)
2154 struct compiling *c;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002155 PyObject *nameval;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002156{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002157 PyObject *ival = PyDict_GetItem(c->c_locals, nameval);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002158 if (ival != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002159 return PyInt_AsLong(ival);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002160 return com_newlocal_o(c, nameval);
2161}
2162
2163static int
2164com_newlocal(c, name)
2165 struct compiling *c;
2166 char *name;
2167{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002168 PyObject *nameval = PyString_InternFromString(name);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002169 int i;
2170 if (nameval == NULL) {
2171 c->c_errors++;
2172 return 0;
2173 }
2174 i = com_newlocal_o(c, nameval);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002175 Py_DECREF(nameval);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002176 return i;
2177}
2178
Guido van Rossumc5e96291991-12-10 13:53:51 +00002179static void
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002180com_exec_stmt(c, n)
2181 struct compiling *c;
2182 node *n;
2183{
2184 REQ(n, exec_stmt);
2185 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
2186 com_node(c, CHILD(n, 1));
2187 if (NCH(n) >= 4)
2188 com_node(c, CHILD(n, 3));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002189 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002190 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002191 com_push(c, 1);
2192 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002193 if (NCH(n) >= 6)
2194 com_node(c, CHILD(n, 5));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002195 else {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002196 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002197 com_push(c, 1);
2198 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002199 com_addbyte(c, EXEC_STMT);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002200 com_pop(c, 3);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002201}
2202
Guido van Rossum7c531111997-03-11 18:42:21 +00002203static int
2204is_constant_false(c, n)
2205 struct compiling *c;
2206 node *n;
2207{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002208 PyObject *v;
Guido van Rossum7c531111997-03-11 18:42:21 +00002209 int i;
2210
2211 /* Label to avoid tail recursion */
2212 next:
2213 switch (TYPE(n)) {
2214
2215 case suite:
2216 if (NCH(n) == 1) {
2217 n = CHILD(n, 0);
2218 goto next;
2219 }
2220 /* Fall through */
2221 case file_input:
2222 for (i = 0; i < NCH(n); i++) {
2223 node *ch = CHILD(n, i);
2224 if (TYPE(ch) == stmt) {
2225 n = ch;
2226 goto next;
2227 }
2228 }
2229 break;
2230
2231 case stmt:
2232 case simple_stmt:
2233 case small_stmt:
2234 n = CHILD(n, 0);
2235 goto next;
2236
2237 case expr_stmt:
2238 case testlist:
2239 case test:
2240 case and_test:
2241 case not_test:
2242 case comparison:
2243 case expr:
2244 case xor_expr:
2245 case and_expr:
2246 case shift_expr:
2247 case arith_expr:
2248 case term:
2249 case factor:
2250 case power:
2251 case atom:
2252 if (NCH(n) == 1) {
2253 n = CHILD(n, 0);
2254 goto next;
2255 }
2256 break;
2257
2258 case NAME:
2259 if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
2260 return 1;
2261 break;
2262
2263 case NUMBER:
2264 v = parsenumber(c, STR(n));
2265 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002266 PyErr_Clear();
Guido van Rossum7c531111997-03-11 18:42:21 +00002267 break;
2268 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002269 i = PyObject_IsTrue(v);
2270 Py_DECREF(v);
Guido van Rossum7c531111997-03-11 18:42:21 +00002271 return i == 0;
2272
2273 case STRING:
Guido van Rossum6af0c001997-03-11 21:25:55 +00002274 v = parsestr(STR(n));
Guido van Rossum7c531111997-03-11 18:42:21 +00002275 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002276 PyErr_Clear();
Guido van Rossum7c531111997-03-11 18:42:21 +00002277 break;
2278 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002279 i = PyObject_IsTrue(v);
2280 Py_DECREF(v);
Guido van Rossum7c531111997-03-11 18:42:21 +00002281 return i == 0;
2282
2283 }
2284 return 0;
2285}
2286
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002287static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002288com_if_stmt(c, n)
2289 struct compiling *c;
2290 node *n;
2291{
2292 int i;
2293 int anchor = 0;
2294 REQ(n, if_stmt);
2295 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
2296 for (i = 0; i+3 < NCH(n); i+=4) {
2297 int a = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002298 node *ch = CHILD(n, i+1);
Guido van Rossum7c531111997-03-11 18:42:21 +00002299 if (is_constant_false(c, ch))
2300 continue;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002301 if (i > 0)
2302 com_addoparg(c, SET_LINENO, ch->n_lineno);
Guido van Rossum7c531111997-03-11 18:42:21 +00002303 com_node(c, ch);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002304 com_addfwref(c, JUMP_IF_FALSE, &a);
2305 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002306 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002307 com_node(c, CHILD(n, i+3));
2308 com_addfwref(c, JUMP_FORWARD, &anchor);
2309 com_backpatch(c, a);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002310 /* We jump here with an extra entry which we now pop */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002311 com_addbyte(c, POP_TOP);
2312 }
2313 if (i+2 < NCH(n))
2314 com_node(c, CHILD(n, i+2));
Guido van Rossum7c531111997-03-11 18:42:21 +00002315 if (anchor)
2316 com_backpatch(c, anchor);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002317}
2318
2319static void
2320com_while_stmt(c, n)
2321 struct compiling *c;
2322 node *n;
2323{
2324 int break_anchor = 0;
2325 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002326 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002327 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
2328 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002329 block_push(c, SETUP_LOOP);
2330 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002331 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002332 com_node(c, CHILD(n, 1));
2333 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2334 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002335 com_pop(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002336 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002337 com_node(c, CHILD(n, 3));
Guido van Rossum3f5da241990-12-20 15:06:42 +00002338 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002339 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2340 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002341 com_backpatch(c, anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002342 /* We jump here with one entry more on the stack */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002343 com_addbyte(c, POP_TOP);
2344 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002345 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002346 if (NCH(n) > 4)
2347 com_node(c, CHILD(n, 6));
2348 com_backpatch(c, break_anchor);
2349}
2350
2351static void
2352com_for_stmt(c, n)
2353 struct compiling *c;
2354 node *n;
2355{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002356 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002357 int break_anchor = 0;
2358 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002359 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002360 REQ(n, for_stmt);
2361 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
2362 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002363 block_push(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002364 com_node(c, CHILD(n, 3));
Guido van Rossum79f25d91997-04-29 20:08:16 +00002365 v = PyInt_FromLong(0L);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002366 if (v == NULL)
2367 c->c_errors++;
2368 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002369 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002370 Py_XDECREF(v);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002371 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002372 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002373 com_addfwref(c, FOR_LOOP, &anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002374 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00002375 com_assign(c, CHILD(n, 1), OP_ASSIGN);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002376 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002377 com_node(c, CHILD(n, 5));
Guido van Rossum3f5da241990-12-20 15:06:42 +00002378 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002379 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2380 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002381 com_backpatch(c, anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002382 com_pop(c, 2); /* FOR_LOOP has popped these */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002383 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002384 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002385 if (NCH(n) > 8)
2386 com_node(c, CHILD(n, 8));
2387 com_backpatch(c, break_anchor);
2388}
2389
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002390/* Code generated for "try: S finally: Sf" is as follows:
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002391
2392 SETUP_FINALLY L
2393 <code for S>
2394 POP_BLOCK
2395 LOAD_CONST <nil>
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002396 L: <code for Sf>
2397 END_FINALLY
2398
2399 The special instructions use the block stack. Each block
2400 stack entry contains the instruction that created it (here
2401 SETUP_FINALLY), the level of the value stack at the time the
2402 block stack entry was created, and a label (here L).
2403
2404 SETUP_FINALLY:
2405 Pushes the current value stack level and the label
2406 onto the block stack.
2407 POP_BLOCK:
2408 Pops en entry from the block stack, and pops the value
2409 stack until its level is the same as indicated on the
2410 block stack. (The label is ignored.)
2411 END_FINALLY:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002412 Pops a variable number of entries from the *value* stack
2413 and re-raises the exception they specify. The number of
2414 entries popped depends on the (pseudo) exception type.
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002415
2416 The block stack is unwound when an exception is raised:
2417 when a SETUP_FINALLY entry is found, the exception is pushed
2418 onto the value stack (and the exception condition is cleared),
2419 and the interpreter jumps to the label gotten from the block
2420 stack.
2421
2422 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
Guido van Rossum3f5da241990-12-20 15:06:42 +00002423 (The contents of the value stack is shown in [], with the top
2424 at the right; 'tb' is trace-back info, 'val' the exception's
2425 associated value, and 'exc' the exception.)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002426
2427 Value stack Label Instruction Argument
2428 [] SETUP_EXCEPT L1
2429 [] <code for S>
2430 [] POP_BLOCK
2431 [] JUMP_FORWARD L0
2432
Guido van Rossum3f5da241990-12-20 15:06:42 +00002433 [tb, val, exc] L1: DUP )
2434 [tb, val, exc, exc] <evaluate E1> )
2435 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2436 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2437 [tb, val, exc, 1] POP )
2438 [tb, val, exc] POP
2439 [tb, val] <assign to V1> (or POP if no V1)
2440 [tb] POP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002441 [] <code for S1>
2442 JUMP_FORWARD L0
2443
Guido van Rossum3f5da241990-12-20 15:06:42 +00002444 [tb, val, exc, 0] L2: POP
2445 [tb, val, exc] DUP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002446 .............................etc.......................
2447
Guido van Rossum3f5da241990-12-20 15:06:42 +00002448 [tb, val, exc, 0] Ln+1: POP
2449 [tb, val, exc] END_FINALLY # re-raise exception
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002450
2451 [] L0: <next statement>
2452
2453 Of course, parts are not generated if Vi or Ei is not present.
2454*/
2455
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002456static void
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002457com_try_except(c, n)
2458 struct compiling *c;
2459 node *n;
2460{
2461 int except_anchor = 0;
2462 int end_anchor = 0;
2463 int else_anchor = 0;
2464 int i;
2465 node *ch;
2466
2467 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
2468 block_push(c, SETUP_EXCEPT);
2469 com_node(c, CHILD(n, 2));
2470 com_addbyte(c, POP_BLOCK);
2471 block_pop(c, SETUP_EXCEPT);
2472 com_addfwref(c, JUMP_FORWARD, &else_anchor);
2473 com_backpatch(c, except_anchor);
2474 for (i = 3;
2475 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
2476 i += 3) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00002477 /* except_clause: 'except' [expr [',' var]] */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002478 if (except_anchor == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002479 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002480 "default 'except:' must be last");
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002481 break;
2482 }
2483 except_anchor = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00002484 com_push(c, 3); /* tb, val, exc pushed by exception */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002485 com_addoparg(c, SET_LINENO, ch->n_lineno);
2486 if (NCH(ch) > 1) {
2487 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002488 com_push(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002489 com_node(c, CHILD(ch, 1));
2490 com_addoparg(c, COMPARE_OP, EXC_MATCH);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002491 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002492 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
2493 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002494 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002495 }
2496 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002497 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002498 if (NCH(ch) > 3)
Guido van Rossum8861b741996-07-30 16:49:37 +00002499 com_assign(c, CHILD(ch, 3), OP_ASSIGN);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002500 else {
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002501 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002502 com_pop(c, 1);
2503 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002504 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002505 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002506 com_node(c, CHILD(n, i+2));
2507 com_addfwref(c, JUMP_FORWARD, &end_anchor);
2508 if (except_anchor) {
2509 com_backpatch(c, except_anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002510 /* We come in with [tb, val, exc, 0] on the
2511 stack; one pop and it's the same as
2512 expected at the start of the loop */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002513 com_addbyte(c, POP_TOP);
2514 }
2515 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002516 /* We actually come in here with [tb, val, exc] but the
2517 END_FINALLY will zap those and jump around.
2518 The c_stacklevel does not reflect them so we need not pop
2519 anything. */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002520 com_addbyte(c, END_FINALLY);
2521 com_backpatch(c, else_anchor);
2522 if (i < NCH(n))
2523 com_node(c, CHILD(n, i+2));
2524 com_backpatch(c, end_anchor);
2525}
2526
2527static void
2528com_try_finally(c, n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002529 struct compiling *c;
2530 node *n;
2531{
2532 int finally_anchor = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002533 node *ch;
Guido van Rossum94fb82e1992-04-05 14:24:50 +00002534
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002535 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
2536 block_push(c, SETUP_FINALLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002537 com_node(c, CHILD(n, 2));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002538 com_addbyte(c, POP_BLOCK);
2539 block_pop(c, SETUP_FINALLY);
2540 block_push(c, END_FINALLY);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002541 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002542 /* While the generated code pushes only one item,
2543 the try-finally handling can enter here with
2544 up to three items. OK, here are the details:
2545 3 for an exception, 2 for RETURN, 1 for BREAK. */
2546 com_push(c, 3);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002547 com_backpatch(c, finally_anchor);
2548 ch = CHILD(n, NCH(n)-1);
2549 com_addoparg(c, SET_LINENO, ch->n_lineno);
2550 com_node(c, ch);
2551 com_addbyte(c, END_FINALLY);
2552 block_pop(c, END_FINALLY);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002553 com_pop(c, 3); /* Matches the com_push above */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002554}
2555
2556static void
2557com_try_stmt(c, n)
2558 struct compiling *c;
2559 node *n;
2560{
2561 REQ(n, try_stmt);
2562 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
2563 | 'try' ':' suite 'finally' ':' suite */
2564 if (TYPE(CHILD(n, 3)) != except_clause)
2565 com_try_finally(c, n);
2566 else
2567 com_try_except(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002568}
2569
Guido van Rossum8b993a91997-01-17 21:04:03 +00002570static node *
2571get_rawdocstring(n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002572 node *n;
2573{
Guido van Rossum164d4ff1995-01-26 00:40:09 +00002574 int i;
2575
Guido van Rossum8b993a91997-01-17 21:04:03 +00002576 /* Label to avoid tail recursion */
2577 next:
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002578 switch (TYPE(n)) {
2579
2580 case suite:
Guido van Rossum8b993a91997-01-17 21:04:03 +00002581 if (NCH(n) == 1) {
2582 n = CHILD(n, 0);
2583 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002584 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002585 /* Fall through */
Guido van Rossum164d4ff1995-01-26 00:40:09 +00002586 case file_input:
2587 for (i = 0; i < NCH(n); i++) {
2588 node *ch = CHILD(n, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002589 if (TYPE(ch) == stmt) {
2590 n = ch;
2591 goto next;
2592 }
Guido van Rossum164d4ff1995-01-26 00:40:09 +00002593 }
2594 break;
2595
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002596 case stmt:
2597 case simple_stmt:
2598 case small_stmt:
Guido van Rossum8b993a91997-01-17 21:04:03 +00002599 n = CHILD(n, 0);
2600 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002601
2602 case expr_stmt:
2603 case testlist:
2604 case test:
2605 case and_test:
2606 case not_test:
2607 case comparison:
2608 case expr:
2609 case xor_expr:
2610 case and_expr:
2611 case shift_expr:
2612 case arith_expr:
2613 case term:
2614 case factor:
Guido van Rossum50564e81996-01-12 01:13:16 +00002615 case power:
Guido van Rossum8b993a91997-01-17 21:04:03 +00002616 if (NCH(n) == 1) {
2617 n = CHILD(n, 0);
2618 goto next;
2619 }
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002620 break;
2621
2622 case atom:
2623 if (TYPE(CHILD(n, 0)) == STRING)
Guido van Rossum8b993a91997-01-17 21:04:03 +00002624 return n;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002625 break;
2626
2627 }
2628 return NULL;
2629}
2630
Guido van Rossum79f25d91997-04-29 20:08:16 +00002631static PyObject *
Guido van Rossum8b993a91997-01-17 21:04:03 +00002632get_docstring(n)
2633 node *n;
2634{
2635 n = get_rawdocstring(n);
2636 if (n == NULL)
2637 return NULL;
2638 return parsestrplus(n);
2639}
2640
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002641static void
2642com_suite(c, n)
2643 struct compiling *c;
2644 node *n;
2645{
2646 REQ(n, suite);
2647 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
2648 if (NCH(n) == 1) {
2649 com_node(c, CHILD(n, 0));
2650 }
2651 else {
2652 int i;
2653 for (i = 0; i < NCH(n); i++) {
2654 node *ch = CHILD(n, i);
2655 if (TYPE(ch) == stmt)
2656 com_node(c, ch);
2657 }
2658 }
2659}
2660
Guido van Rossumf1aeab71992-03-27 17:28:26 +00002661/* ARGSUSED */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002662static void
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002663com_continue_stmt(c, n)
2664 struct compiling *c;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00002665 node *n; /* Not used, but passed for consistency */
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002666{
2667 int i = c->c_nblocks;
2668 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
2669 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2670 }
2671 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002672 com_error(c, PyExc_SyntaxError,
2673 "'continue' not properly in loop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002674 }
2675 /* XXX Could allow it inside a 'finally' clause
2676 XXX if we could pop the exception still on the stack */
2677}
2678
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002679static int
Guido van Rossum681d79a1995-07-18 14:51:37 +00002680com_argdefs(c, n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002681 struct compiling *c;
2682 node *n;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002683{
Guido van Rossum681d79a1995-07-18 14:51:37 +00002684 int i, nch, nargs, ndefs;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002685 if (TYPE(n) == lambdef) {
2686 /* lambdef: 'lambda' [varargslist] ':' test */
2687 n = CHILD(n, 1);
2688 }
2689 else {
2690 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
2691 n = CHILD(n, 2);
2692 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
2693 n = CHILD(n, 1);
2694 }
2695 if (TYPE(n) != varargslist)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002696 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002697 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00002698 (fpdef ['=' test] ',')* '*' ....... |
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002699 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
2700 nch = NCH(n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002701 nargs = 0;
2702 ndefs = 0;
2703 for (i = 0; i < nch; i++) {
2704 int t;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002705 if (TYPE(CHILD(n, i)) == STAR ||
2706 TYPE(CHILD(n, i)) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00002707 break;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002708 nargs++;
2709 i++;
2710 if (i >= nch)
Guido van Rossuma1e7e621995-09-18 21:44:04 +00002711 t = RPAR; /* Anything except EQUAL or COMMA */
2712 else
2713 t = TYPE(CHILD(n, i));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002714 if (t == EQUAL) {
2715 i++;
2716 ndefs++;
2717 com_node(c, CHILD(n, i));
2718 i++;
2719 if (i >= nch)
2720 break;
2721 t = TYPE(CHILD(n, i));
2722 }
2723 else {
Guido van Rossum29d38cd1998-10-02 13:41:54 +00002724 /* Treat "(a=1, b)" as an error */
2725 if (ndefs)
2726 com_error(c, PyExc_SyntaxError,
Guido van Rossumdfede311998-10-02 14:06:56 +00002727 "non-default argument follows default argument");
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002728 }
2729 if (t != COMMA)
2730 break;
2731 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002732 return ndefs;
2733}
2734
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002735static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002736com_funcdef(c, n)
2737 struct compiling *c;
2738 node *n;
2739{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002740 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002741 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002742 v = (PyObject *)icompile(n, c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002743 if (v == NULL)
2744 c->c_errors++;
2745 else {
2746 int i = com_addconst(c, v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002747 int ndefs = com_argdefs(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002748 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002749 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002750 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002751 com_pop(c, ndefs);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002752 com_addopname(c, STORE_NAME, CHILD(n, 1));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002753 com_pop(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002754 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002755 }
2756}
2757
2758static void
Guido van Rossum25831651993-05-19 14:50:45 +00002759com_bases(c, n)
Guido van Rossumc5e96291991-12-10 13:53:51 +00002760 struct compiling *c;
2761 node *n;
2762{
Guido van Rossumf1aeab71992-03-27 17:28:26 +00002763 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00002764 REQ(n, testlist);
2765 /* testlist: test (',' test)* [','] */
2766 for (i = 0; i < NCH(n); i += 2)
2767 com_node(c, CHILD(n, i));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002768 i = (NCH(n)+1) / 2;
2769 com_addoparg(c, BUILD_TUPLE, i);
2770 com_pop(c, i-1);
Guido van Rossumc5e96291991-12-10 13:53:51 +00002771}
2772
2773static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002774com_classdef(c, n)
2775 struct compiling *c;
2776 node *n;
2777{
Guido van Rossum25831651993-05-19 14:50:45 +00002778 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002779 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002780 REQ(n, classdef);
Guido van Rossum25831651993-05-19 14:50:45 +00002781 /* classdef: class NAME ['(' testlist ')'] ':' suite */
Guido van Rossum7b89b6a1997-01-18 08:02:57 +00002782 if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
Guido van Rossum25831651993-05-19 14:50:45 +00002783 c->c_errors++;
2784 return;
2785 }
2786 /* Push the class name on the stack */
2787 i = com_addconst(c, v);
2788 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002789 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002790 Py_DECREF(v);
Guido van Rossum25831651993-05-19 14:50:45 +00002791 /* Push the tuple of base classes on the stack */
Guido van Rossum8b993a91997-01-17 21:04:03 +00002792 if (TYPE(CHILD(n, 2)) != LPAR) {
Guido van Rossumc5e96291991-12-10 13:53:51 +00002793 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002794 com_push(c, 1);
2795 }
Guido van Rossum25831651993-05-19 14:50:45 +00002796 else
2797 com_bases(c, CHILD(n, 3));
Guido van Rossum79f25d91997-04-29 20:08:16 +00002798 v = (PyObject *)icompile(n, c);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002799 if (v == NULL)
2800 c->c_errors++;
2801 else {
Guido van Rossum25831651993-05-19 14:50:45 +00002802 i = com_addconst(c, v);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002803 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002804 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002805 com_addoparg(c, MAKE_FUNCTION, 0);
2806 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002807 com_addbyte(c, BUILD_CLASS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002808 com_pop(c, 2);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002809 com_addopname(c, STORE_NAME, CHILD(n, 1));
Guido van Rossum79f25d91997-04-29 20:08:16 +00002810 Py_DECREF(v);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002811 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002812}
2813
2814static void
2815com_node(c, n)
2816 struct compiling *c;
2817 node *n;
2818{
2819 switch (TYPE(n)) {
2820
2821 /* Definition nodes */
2822
2823 case funcdef:
2824 com_funcdef(c, n);
2825 break;
2826 case classdef:
2827 com_classdef(c, n);
2828 break;
2829
2830 /* Trivial parse tree nodes */
2831
2832 case stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002833 case small_stmt:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002834 case flow_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002835 com_node(c, CHILD(n, 0));
2836 break;
2837
2838 case simple_stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002839 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
2840 com_addoparg(c, SET_LINENO, n->n_lineno);
2841 {
2842 int i;
2843 for (i = 0; i < NCH(n)-1; i += 2)
2844 com_node(c, CHILD(n, i));
2845 }
2846 break;
2847
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002848 case compound_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002849 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002850 com_node(c, CHILD(n, 0));
2851 break;
2852
2853 /* Statement nodes */
2854
2855 case expr_stmt:
2856 com_expr_stmt(c, n);
2857 break;
2858 case print_stmt:
2859 com_print_stmt(c, n);
2860 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002861 case del_stmt: /* 'del' exprlist */
Guido van Rossum8861b741996-07-30 16:49:37 +00002862 com_assign(c, CHILD(n, 1), OP_DELETE);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002863 break;
2864 case pass_stmt:
2865 break;
2866 case break_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002867 if (c->c_loops == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002868 com_error(c, PyExc_SyntaxError,
2869 "'break' outside loop");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002870 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002871 com_addbyte(c, BREAK_LOOP);
2872 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002873 case continue_stmt:
2874 com_continue_stmt(c, n);
2875 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002876 case return_stmt:
2877 com_return_stmt(c, n);
2878 break;
2879 case raise_stmt:
2880 com_raise_stmt(c, n);
2881 break;
2882 case import_stmt:
2883 com_import_stmt(c, n);
2884 break;
Guido van Rossumc5e96291991-12-10 13:53:51 +00002885 case global_stmt:
2886 com_global_stmt(c, n);
2887 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002888 case exec_stmt:
2889 com_exec_stmt(c, n);
2890 break;
Guido van Rossum228d7f31997-04-02 05:24:36 +00002891 case assert_stmt:
2892 com_assert_stmt(c, n);
2893 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002894 case if_stmt:
2895 com_if_stmt(c, n);
2896 break;
2897 case while_stmt:
2898 com_while_stmt(c, n);
2899 break;
2900 case for_stmt:
2901 com_for_stmt(c, n);
2902 break;
2903 case try_stmt:
2904 com_try_stmt(c, n);
2905 break;
2906 case suite:
2907 com_suite(c, n);
2908 break;
2909
2910 /* Expression nodes */
2911
2912 case testlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00002913 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002914 break;
2915 case test:
2916 com_test(c, n);
2917 break;
2918 case and_test:
2919 com_and_test(c, n);
2920 break;
2921 case not_test:
2922 com_not_test(c, n);
2923 break;
2924 case comparison:
2925 com_comparison(c, n);
2926 break;
2927 case exprlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00002928 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002929 break;
2930 case expr:
2931 com_expr(c, n);
2932 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +00002933 case xor_expr:
2934 com_xor_expr(c, n);
2935 break;
2936 case and_expr:
2937 com_and_expr(c, n);
2938 break;
2939 case shift_expr:
2940 com_shift_expr(c, n);
2941 break;
2942 case arith_expr:
2943 com_arith_expr(c, n);
2944 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002945 case term:
2946 com_term(c, n);
2947 break;
2948 case factor:
2949 com_factor(c, n);
2950 break;
Guido van Rossum50564e81996-01-12 01:13:16 +00002951 case power:
2952 com_power(c, n);
2953 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002954 case atom:
2955 com_atom(c, n);
2956 break;
2957
2958 default:
Guido van Rossum8b993a91997-01-17 21:04:03 +00002959 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002960 com_error(c, PyExc_SystemError,
2961 "com_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002962 }
2963}
2964
Guido van Rossum79f25d91997-04-29 20:08:16 +00002965static void com_fplist Py_PROTO((struct compiling *, node *));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002966
2967static void
2968com_fpdef(c, n)
2969 struct compiling *c;
2970 node *n;
2971{
2972 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
2973 if (TYPE(CHILD(n, 0)) == LPAR)
2974 com_fplist(c, CHILD(n, 1));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002975 else {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002976 com_addoparg(c, STORE_FAST, com_newlocal(c, STR(CHILD(n, 0))));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002977 com_pop(c, 1);
2978 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002979}
2980
2981static void
2982com_fplist(c, n)
2983 struct compiling *c;
2984 node *n;
2985{
Guido van Rossuma9df32a1991-12-31 13:13:35 +00002986 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002987 if (NCH(n) == 1) {
2988 com_fpdef(c, CHILD(n, 0));
2989 }
2990 else {
Guido van Rossum8b993a91997-01-17 21:04:03 +00002991 int i = (NCH(n)+1)/2;
2992 com_addoparg(c, UNPACK_TUPLE, i);
2993 com_push(c, i-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002994 for (i = 0; i < NCH(n); i += 2)
2995 com_fpdef(c, CHILD(n, i));
2996 }
2997}
2998
2999static void
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003000com_arglist(c, n)
3001 struct compiling *c;
3002 node *n;
3003{
Guido van Rossum681d79a1995-07-18 14:51:37 +00003004 int nch, i;
3005 int complex = 0;
Guido van Rossumdb9e20f1997-07-10 01:06:53 +00003006 char nbuf[10];
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003007 REQ(n, varargslist);
Guido van Rossumacbefef1992-01-19 16:33:51 +00003008 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00003009 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003010 nch = NCH(n);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003011 /* Enter all arguments in table of locals */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003012 for (i = 0; i < nch; i++) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00003013 node *ch = CHILD(n, i);
3014 node *fp;
3015 char *name;
Guido van Rossum50564e81996-01-12 01:13:16 +00003016 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00003017 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003018 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3019 fp = CHILD(ch, 0);
3020 if (TYPE(fp) == NAME)
3021 name = STR(fp);
3022 else {
Guido van Rossumdb9e20f1997-07-10 01:06:53 +00003023 name = nbuf;
3024 sprintf(nbuf, ".%d", i);
Guido van Rossum530956d1996-07-21 02:27:43 +00003025 complex = 1;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003026 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003027 com_newlocal(c, name);
3028 c->c_argcount++;
3029 if (++i >= nch)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003030 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003031 ch = CHILD(n, i);
3032 if (TYPE(ch) == EQUAL)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003033 i += 2;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003034 else
3035 REQ(ch, COMMA);
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003036 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003037 /* Handle *arguments */
3038 if (i < nch) {
3039 node *ch;
3040 ch = CHILD(n, i);
Guido van Rossum50564e81996-01-12 01:13:16 +00003041 if (TYPE(ch) != DOUBLESTAR) {
3042 REQ(ch, STAR);
3043 ch = CHILD(n, i+1);
3044 if (TYPE(ch) == NAME) {
3045 c->c_flags |= CO_VARARGS;
3046 i += 3;
3047 com_newlocal(c, STR(ch));
3048 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003049 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003050 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003051 /* Handle **keywords */
3052 if (i < nch) {
3053 node *ch;
3054 ch = CHILD(n, i);
Guido van Rossum50564e81996-01-12 01:13:16 +00003055 if (TYPE(ch) != DOUBLESTAR) {
3056 REQ(ch, STAR);
3057 ch = CHILD(n, i+1);
3058 REQ(ch, STAR);
3059 ch = CHILD(n, i+2);
3060 }
3061 else
3062 ch = CHILD(n, i+1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003063 REQ(ch, NAME);
3064 c->c_flags |= CO_VARKEYWORDS;
3065 com_newlocal(c, STR(ch));
3066 }
3067 if (complex) {
3068 /* Generate code for complex arguments only after
3069 having counted the simple arguments */
3070 int ilocal = 0;
3071 for (i = 0; i < nch; i++) {
3072 node *ch = CHILD(n, i);
3073 node *fp;
Guido van Rossum50564e81996-01-12 01:13:16 +00003074 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003075 break;
3076 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3077 fp = CHILD(ch, 0);
3078 if (TYPE(fp) != NAME) {
3079 com_addoparg(c, LOAD_FAST, ilocal);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003080 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003081 com_fpdef(c, ch);
3082 }
3083 ilocal++;
3084 if (++i >= nch)
3085 break;
3086 ch = CHILD(n, i);
3087 if (TYPE(ch) == EQUAL)
3088 i += 2;
3089 else
3090 REQ(ch, COMMA);
3091 }
3092 }
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003093}
3094
3095static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003096com_file_input(c, n)
3097 struct compiling *c;
3098 node *n;
3099{
3100 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003101 PyObject *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003102 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003103 doc = get_docstring(n);
3104 if (doc != NULL) {
3105 int i = com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003106 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003107 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003108 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003109 com_addopnamestr(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00003110 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003111 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003112 for (i = 0; i < NCH(n); i++) {
3113 node *ch = CHILD(n, i);
3114 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
3115 com_node(c, ch);
3116 }
3117}
3118
3119/* Top-level compile-node interface */
3120
3121static void
3122compile_funcdef(c, n)
3123 struct compiling *c;
3124 node *n;
3125{
Guido van Rossum79f25d91997-04-29 20:08:16 +00003126 PyObject *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003127 node *ch;
3128 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum9bfef441993-03-29 10:43:31 +00003129 c->c_name = STR(CHILD(n, 1));
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003130 doc = get_docstring(CHILD(n, 4));
3131 if (doc != NULL) {
3132 (void) com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003133 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003134 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003135 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00003136 (void) com_addconst(c, Py_None); /* No docstring */
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003137 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
3138 ch = CHILD(ch, 1); /* ')' | varargslist */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003139 if (TYPE(ch) == varargslist)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003140 com_arglist(c, ch);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003141 c->c_infunction = 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003142 com_node(c, CHILD(n, 4));
Guido van Rossum3f5da241990-12-20 15:06:42 +00003143 c->c_infunction = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003144 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003145 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003146 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003147 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003148}
3149
3150static void
Guido van Rossum590baa41993-11-30 13:40:46 +00003151compile_lambdef(c, n)
Guido van Rossum12d12c51993-10-26 17:58:25 +00003152 struct compiling *c;
3153 node *n;
3154{
Guido van Rossum590baa41993-11-30 13:40:46 +00003155 node *ch;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003156 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
Guido van Rossum363ac7d1994-11-10 22:40:34 +00003157 c->c_name = "<lambda>";
Guido van Rossum590baa41993-11-30 13:40:46 +00003158
3159 ch = CHILD(n, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003160 (void) com_addconst(c, Py_None); /* No docstring */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003161 if (TYPE(ch) == varargslist) {
Guido van Rossum590baa41993-11-30 13:40:46 +00003162 com_arglist(c, ch);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003163 ch = CHILD(n, 3);
Guido van Rossum590baa41993-11-30 13:40:46 +00003164 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003165 else
3166 ch = CHILD(n, 2);
3167 com_node(c, ch);
Guido van Rossum12d12c51993-10-26 17:58:25 +00003168 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003169 com_pop(c, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00003170}
3171
3172static void
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003173compile_classdef(c, n)
3174 struct compiling *c;
3175 node *n;
3176{
3177 node *ch;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003178 PyObject *doc;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003179 REQ(n, classdef);
3180 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3181 c->c_name = STR(CHILD(n, 1));
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003182#ifdef PRIVATE_NAME_MANGLING
3183 c->c_private = c->c_name;
3184#endif
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003185 ch = CHILD(n, NCH(n)-1); /* The suite */
3186 doc = get_docstring(ch);
3187 if (doc != NULL) {
3188 int i = com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003189 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003190 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003191 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003192 com_addopnamestr(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00003193 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003194 }
3195 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00003196 (void) com_addconst(c, Py_None);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003197 com_node(c, ch);
3198 com_addbyte(c, LOAD_LOCALS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003199 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003200 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003201 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003202}
3203
3204static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003205compile_node(c, n)
3206 struct compiling *c;
3207 node *n;
3208{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003209 com_addoparg(c, SET_LINENO, n->n_lineno);
3210
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003211 switch (TYPE(n)) {
3212
Guido van Rossum4c417781991-01-21 16:09:22 +00003213 case single_input: /* One interactive command */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003214 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003215 c->c_interactive++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003216 n = CHILD(n, 0);
3217 if (TYPE(n) != NEWLINE)
3218 com_node(c, n);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003219 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003220 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003221 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003222 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003223 c->c_interactive--;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003224 break;
3225
Guido van Rossum4c417781991-01-21 16:09:22 +00003226 case file_input: /* A whole file, or built-in function exec() */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003227 com_file_input(c, n);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003228 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003229 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003230 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003231 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003232 break;
3233
Guido van Rossum590baa41993-11-30 13:40:46 +00003234 case eval_input: /* Built-in function input() */
Guido van Rossum4c417781991-01-21 16:09:22 +00003235 com_node(c, CHILD(n, 0));
3236 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003237 com_pop(c, 1);
Guido van Rossum4c417781991-01-21 16:09:22 +00003238 break;
3239
Guido van Rossum590baa41993-11-30 13:40:46 +00003240 case lambdef: /* anonymous function definition */
3241 compile_lambdef(c, n);
3242 break;
3243
Guido van Rossum4c417781991-01-21 16:09:22 +00003244 case funcdef: /* A function definition */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003245 compile_funcdef(c, n);
3246 break;
3247
Guido van Rossum4c417781991-01-21 16:09:22 +00003248 case classdef: /* A class definition */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003249 compile_classdef(c, n);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003250 break;
3251
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003252 default:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003253 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
Guido van Rossum79f25d91997-04-29 20:08:16 +00003254 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00003255 "compile_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003256 }
3257}
3258
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003259/* Optimization for local variables in functions (and *only* functions).
Guido van Rossum282914b1991-04-04 10:42:56 +00003260
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003261 This replaces all LOAD_NAME, STORE_NAME and DELETE_NAME
3262 instructions that refer to local variables with LOAD_FAST etc.
3263 The latter instructions are much faster because they don't need to
3264 look up the variable name in a dictionary.
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003265
Guido van Rossum681d79a1995-07-18 14:51:37 +00003266 To find all local variables, we check all STORE_NAME, IMPORT_FROM
3267 and DELETE_NAME instructions. This yields all local variables,
3268 function definitions, class definitions and import statements.
3269 Argument names have already been entered into the list by the
3270 special processing for the argument list.
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003271
3272 All remaining LOAD_NAME instructions must refer to non-local (global
3273 or builtin) variables, so are replaced by LOAD_GLOBAL.
3274
3275 There are two problems: 'from foo import *' and 'exec' may introduce
3276 local variables that we can't know while compiling. If this is the
Guido van Rossum681d79a1995-07-18 14:51:37 +00003277 case, we can still optimize bona fide locals (since those
3278 statements will be surrounded by fast_2_locals() and
3279 locals_2_fast()), but we can't change LOAD_NAME to LOAD_GLOBAL.
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003280
Guido van Rossum681d79a1995-07-18 14:51:37 +00003281 NB: this modifies the string object c->c_code! */
Guido van Rossum282914b1991-04-04 10:42:56 +00003282
3283static void
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003284optimize(c)
3285 struct compiling *c;
Guido van Rossum282914b1991-04-04 10:42:56 +00003286{
Guido van Rossum0a697f61991-04-16 08:39:12 +00003287 unsigned char *next_instr, *cur_instr;
Guido van Rossum282914b1991-04-04 10:42:56 +00003288 int opcode;
Guido van Rossum80bb9651996-12-05 23:27:02 +00003289 int oparg = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003290 PyObject *name;
3291 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003292
Guido van Rossum282914b1991-04-04 10:42:56 +00003293#define NEXTOP() (*next_instr++)
3294#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Guido van Rossum79f25d91997-04-29 20:08:16 +00003295#define GETITEM(v, i) (PyList_GetItem((v), (i)))
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003296#define GETNAMEOBJ(i) (GETITEM(c->c_names, (i)))
3297
Guido van Rossum79f25d91997-04-29 20:08:16 +00003298 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003299
3300 c->c_flags |= CO_OPTIMIZED;
Guido van Rossum282914b1991-04-04 10:42:56 +00003301
Guido van Rossum79f25d91997-04-29 20:08:16 +00003302 next_instr = (unsigned char *) PyString_AsString(c->c_code);
Guido van Rossum282914b1991-04-04 10:42:56 +00003303 for (;;) {
3304 opcode = NEXTOP();
3305 if (opcode == STOP_CODE)
3306 break;
3307 if (HAS_ARG(opcode))
3308 oparg = NEXTARG();
Guido van Rossum681d79a1995-07-18 14:51:37 +00003309 switch (opcode) {
3310 case STORE_NAME:
3311 case DELETE_NAME:
3312 case IMPORT_FROM:
3313 com_addlocal_o(c, GETNAMEOBJ(oparg));
3314 break;
3315 case EXEC_STMT:
3316 c->c_flags &= ~CO_OPTIMIZED;
3317 break;
Guido van Rossum282914b1991-04-04 10:42:56 +00003318 }
3319 }
3320
Guido van Rossum79f25d91997-04-29 20:08:16 +00003321 if (PyDict_GetItemString(c->c_locals, "*") != NULL)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003322 c->c_flags &= ~CO_OPTIMIZED;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003323
Guido van Rossum79f25d91997-04-29 20:08:16 +00003324 next_instr = (unsigned char *) PyString_AsString(c->c_code);
Guido van Rossum282914b1991-04-04 10:42:56 +00003325 for (;;) {
3326 cur_instr = next_instr;
3327 opcode = NEXTOP();
3328 if (opcode == STOP_CODE)
3329 break;
3330 if (HAS_ARG(opcode))
3331 oparg = NEXTARG();
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003332 if (opcode == LOAD_NAME ||
3333 opcode == STORE_NAME ||
3334 opcode == DELETE_NAME) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003335 PyObject *v;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003336 int i;
Guido van Rossum282914b1991-04-04 10:42:56 +00003337 name = GETNAMEOBJ(oparg);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003338 v = PyDict_GetItem(c->c_locals, name);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003339 if (v == NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00003340 if (opcode == LOAD_NAME &&
3341 (c->c_flags&CO_OPTIMIZED))
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003342 cur_instr[0] = LOAD_GLOBAL;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003343 continue;
Guido van Rossum83163251991-08-16 08:58:43 +00003344 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00003345 i = PyInt_AsLong(v);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003346 switch (opcode) {
3347 case LOAD_NAME: cur_instr[0] = LOAD_FAST; break;
3348 case STORE_NAME: cur_instr[0] = STORE_FAST; break;
3349 case DELETE_NAME: cur_instr[0] = DELETE_FAST; break;
3350 }
3351 cur_instr[1] = i & 0xff;
3352 cur_instr[2] = (i>>8) & 0xff;
Guido van Rossum282914b1991-04-04 10:42:56 +00003353 }
3354 }
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003355
Guido van Rossum681d79a1995-07-18 14:51:37 +00003356 if (c->c_errors == 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +00003357 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum282914b1991-04-04 10:42:56 +00003358}
3359
Guido van Rossum79f25d91997-04-29 20:08:16 +00003360PyCodeObject *
3361PyNode_Compile(n, filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003362 node *n;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003363 char *filename;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003364{
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003365 return jcompile(n, filename, NULL);
3366}
3367
Guido van Rossum79f25d91997-04-29 20:08:16 +00003368static PyCodeObject *
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003369icompile(n, base)
3370 node *n;
3371 struct compiling *base;
3372{
3373 return jcompile(n, base->c_filename, base);
3374}
3375
Guido van Rossum79f25d91997-04-29 20:08:16 +00003376static PyCodeObject *
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003377jcompile(n, filename, base)
3378 node *n;
3379 char *filename;
3380 struct compiling *base;
3381{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003382 struct compiling sc;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003383 PyCodeObject *co;
Guido van Rossum590baa41993-11-30 13:40:46 +00003384 if (!com_init(&sc, filename))
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003385 return NULL;
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003386#ifdef PRIVATE_NAME_MANGLING
3387 if (base)
3388 sc.c_private = base->c_private;
3389 else
3390 sc.c_private = NULL;
3391#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003392 compile_node(&sc, n);
3393 com_done(&sc);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003394 if ((TYPE(n) == funcdef || TYPE(n) == lambdef) && sc.c_errors == 0) {
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003395 optimize(&sc);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003396 sc.c_flags |= CO_NEWLOCALS;
3397 }
3398 else if (TYPE(n) == classdef)
3399 sc.c_flags |= CO_NEWLOCALS;
Guido van Rossum9bfef441993-03-29 10:43:31 +00003400 co = NULL;
3401 if (sc.c_errors == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003402 PyObject *consts, *names, *varnames, *filename, *name;
3403 consts = PyList_AsTuple(sc.c_consts);
3404 names = PyList_AsTuple(sc.c_names);
3405 varnames = PyList_AsTuple(sc.c_varnames);
Guido van Rossum7b89b6a1997-01-18 08:02:57 +00003406 filename = PyString_InternFromString(sc.c_filename);
3407 name = PyString_InternFromString(sc.c_name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003408 if (!PyErr_Occurred())
3409 co = PyCode_New(sc.c_argcount,
Guido van Rossum681d79a1995-07-18 14:51:37 +00003410 sc.c_nlocals,
Guido van Rossum8b993a91997-01-17 21:04:03 +00003411 sc.c_maxstacklevel,
Guido van Rossum681d79a1995-07-18 14:51:37 +00003412 sc.c_flags,
3413 sc.c_code,
3414 consts,
3415 names,
3416 varnames,
3417 filename,
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00003418 name,
3419 sc.c_firstlineno,
3420 sc.c_lnotab);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003421 Py_XDECREF(consts);
3422 Py_XDECREF(names);
3423 Py_XDECREF(varnames);
3424 Py_XDECREF(filename);
3425 Py_XDECREF(name);
Guido van Rossuma082ce41991-06-04 19:41:56 +00003426 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003427 com_free(&sc);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003428 return co;
3429}
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00003430
3431int
3432PyCode_Addr2Line(co, addrq)
3433 PyCodeObject *co;
3434 int addrq;
3435{
3436 int size = PyString_Size(co->co_lnotab) / 2;
3437 char *p = PyString_AsString(co->co_lnotab);
3438 int line = co->co_firstlineno;
3439 int addr = 0;
3440 while (--size >= 0) {
3441 addr += *p++;
3442 if (addr > addrq)
3443 break;
3444 line += *p++;
3445 }
3446 return line;
3447}