blob: e6918a99bd28b61e18c7e12e0fad377de9882e6f [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 Rossum681d79a1995-07-18 14:51:37 +000039 XXX get rid of SET_LINENO instructions, use JAR's table trick
40 XXX (need an option to put them back in, for debugger!)
Guido van Rossum8b993a91997-01-17 21:04:03 +000041 XXX New 1-byte opcode for loading None
42 XXX New opcode for loading the initial index for a for loop
Guido van Rossum681d79a1995-07-18 14:51:37 +000043 XXX other JAR tricks?
Guido van Rossum3f5da241990-12-20 15:06:42 +000044*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +000045
Guido van Rossum8ff077b1996-08-24 06:21:31 +000046#ifndef NO_PRIVATE_NAME_MANGLING
47#define PRIVATE_NAME_MANGLING
48#endif
49
Guido van Rossum79f25d91997-04-29 20:08:16 +000050#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000051
Guido van Rossum10dc2e81990-11-18 17:27:39 +000052#include "node.h"
53#include "token.h"
54#include "graminit.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000055#include "compile.h"
56#include "opcode.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000057#include "structmember.h"
58
59#include <ctype.h>
Guido van Rossum282914b1991-04-04 10:42:56 +000060
Guido van Rossumb05a5c71997-05-07 17:46:13 +000061/* Three symbols from graminit.h are also defined in Python.h, with
62 Py_ prefixes to their names. Python.h can't include graminit.h
63 (which defines too many confusing symbols), but we can check here
64 that they haven't changed (which is very unlikely, but possible). */
65#if Py_single_input != single_input
66#error "single_input has changed -- update Py_single_input in Python.h"
67#endif
68#if Py_file_input != file_input
69#error "file_input has changed -- update Py_file_input in Python.h"
70#endif
71#if Py_eval_input != eval_input
72#error "eval_input has changed -- update Py_eval_input in Python.h"
73#endif
74
Guido van Rossum8e793d91997-03-03 19:13:14 +000075int Py_OptimizeFlag = 0;
76
Guido van Rossum8861b741996-07-30 16:49:37 +000077#define OP_DELETE 0
78#define OP_ASSIGN 1
79#define OP_APPLY 2
80
Guido van Rossum79f25d91997-04-29 20:08:16 +000081#define OFF(x) offsetof(PyCodeObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +000082
83static struct memberlist code_memberlist[] = {
Guido van Rossum681d79a1995-07-18 14:51:37 +000084 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
85 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
Guido van Rossum8b993a91997-01-17 21:04:03 +000086 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
Guido van Rossum681d79a1995-07-18 14:51:37 +000087 {"co_flags", T_INT, OFF(co_flags), READONLY},
Guido van Rossum39d942d1992-01-12 02:30:05 +000088 {"co_code", T_OBJECT, OFF(co_code), READONLY},
89 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
90 {"co_names", T_OBJECT, OFF(co_names), READONLY},
Guido van Rossum681d79a1995-07-18 14:51:37 +000091 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
Guido van Rossum39d942d1992-01-12 02:30:05 +000092 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
Guido van Rossum9bfef441993-03-29 10:43:31 +000093 {"co_name", T_OBJECT, OFF(co_name), READONLY},
Guido van Rossumda4eb5c1997-01-24 03:43:35 +000094 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
95 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
Guido van Rossum3f5da241990-12-20 15:06:42 +000096 {NULL} /* Sentinel */
97};
98
Guido van Rossum79f25d91997-04-29 20:08:16 +000099static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +0000100code_getattr(co, name)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000101 PyCodeObject *co;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000102 char *name;
103{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000104 return PyMember_Get((char *)co, code_memberlist, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000105}
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000106
107static void
Guido van Rossum3f5da241990-12-20 15:06:42 +0000108code_dealloc(co)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000109 PyCodeObject *co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000110{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000111 Py_XDECREF(co->co_code);
112 Py_XDECREF(co->co_consts);
113 Py_XDECREF(co->co_names);
114 Py_XDECREF(co->co_filename);
115 Py_XDECREF(co->co_name);
116 Py_XDECREF(co->co_varnames);
117 PyMem_DEL(co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000118}
119
Guido van Rossum79f25d91997-04-29 20:08:16 +0000120static PyObject *
Guido van Rossum2dff9911992-09-03 20:50:59 +0000121code_repr(co)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000122 PyCodeObject *co;
Guido van Rossum2dff9911992-09-03 20:50:59 +0000123{
124 char buf[500];
125 int lineno = -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000126 char *p = PyString_AS_STRING(co->co_code);
Guido van Rossum2dff9911992-09-03 20:50:59 +0000127 char *filename = "???";
Guido van Rossum9bfef441993-03-29 10:43:31 +0000128 char *name = "???";
Guido van Rossum2dff9911992-09-03 20:50:59 +0000129 if (*p == SET_LINENO)
130 lineno = (p[1] & 0xff) | ((p[2] & 0xff) << 8);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000131 if (co->co_filename && PyString_Check(co->co_filename))
132 filename = PyString_AsString(co->co_filename);
133 if (co->co_name && PyString_Check(co->co_name))
134 name = PyString_AsString(co->co_name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000135 sprintf(buf, "<code object %.100s at %lx, file \"%.300s\", line %d>",
136 name, (long)co, filename, lineno);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000137 return PyString_FromString(buf);
Guido van Rossum2dff9911992-09-03 20:50:59 +0000138}
139
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000140static int
141code_compare(co, cp)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000142 PyCodeObject *co, *cp;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000143{
144 int cmp;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000145 cmp = cp->co_argcount - cp->co_argcount;
146 if (cmp) return cmp;
147 cmp = cp->co_nlocals - cp->co_nlocals;
148 if (cmp) return cmp;
149 cmp = cp->co_flags - cp->co_flags;
150 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000151 cmp = PyObject_Compare((PyObject *)co->co_code,
152 (PyObject *)cp->co_code);
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_consts, cp->co_consts);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000155 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000156 cmp = PyObject_Compare(co->co_names, cp->co_names);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000157 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000158 cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000159 return cmp;
160}
161
162static long
163code_hash(co)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000164 PyCodeObject *co;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000165{
Guido van Rossum681d79a1995-07-18 14:51:37 +0000166 long h, h1, h2, h3, h4;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000167 h1 = PyObject_Hash((PyObject *)co->co_code);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000168 if (h1 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000169 h2 = PyObject_Hash(co->co_consts);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000170 if (h2 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000171 h3 = PyObject_Hash(co->co_names);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000172 if (h3 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000173 h4 = PyObject_Hash(co->co_varnames);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000174 if (h4 == -1) return -1;
175 h = h1 ^ h2 ^ h3 ^ h4 ^
176 co->co_argcount ^ co->co_nlocals ^ co->co_flags;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000177 if (h == -1) h = -2;
178 return h;
179}
180
Guido van Rossum79f25d91997-04-29 20:08:16 +0000181PyTypeObject PyCode_Type = {
182 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000183 0,
184 "code",
Guido van Rossum79f25d91997-04-29 20:08:16 +0000185 sizeof(PyCodeObject),
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000186 0,
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000187 (destructor)code_dealloc, /*tp_dealloc*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000188 0, /*tp_print*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000189 (getattrfunc)code_getattr, /*tp_getattr*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000190 0, /*tp_setattr*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000191 (cmpfunc)code_compare, /*tp_compare*/
192 (reprfunc)code_repr, /*tp_repr*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000193 0, /*tp_as_number*/
194 0, /*tp_as_sequence*/
195 0, /*tp_as_mapping*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000196 (hashfunc)code_hash, /*tp_hash*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000197};
198
Guido van Rossum644a12b1997-04-09 19:24:53 +0000199#define NAME_CHARS \
200 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
201
Guido van Rossum79f25d91997-04-29 20:08:16 +0000202PyCodeObject *
203PyCode_New(argcount, nlocals, stacksize, flags,
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000204 code, consts, names, varnames, filename, name,
205 firstlineno, lnotab)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000206 int argcount;
207 int nlocals;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000208 int stacksize;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000209 int flags;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000210 PyObject *code;
211 PyObject *consts;
212 PyObject *names;
213 PyObject *varnames;
214 PyObject *filename;
215 PyObject *name;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000216 int firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000217 PyObject *lnotab;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000218{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000219 PyCodeObject *co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000220 int i;
221 /* Check argument types */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000222 if (argcount < 0 || nlocals < 0 ||
Guido van Rossum79f25d91997-04-29 20:08:16 +0000223 code == NULL || !PyString_Check(code) ||
224 consts == NULL || !PyTuple_Check(consts) ||
225 names == NULL || !PyTuple_Check(names) ||
226 varnames == NULL || !PyTuple_Check(varnames) ||
227 name == NULL || !PyString_Check(name) ||
228 filename == NULL || !PyString_Check(filename) ||
229 lnotab == NULL || !PyString_Check(lnotab)) {
230 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000231 return NULL;
232 }
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000233 /* Make sure names and varnames are all strings, & intern them */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000234 for (i = PyTuple_Size(names); --i >= 0; ) {
235 PyObject *v = PyTuple_GetItem(names, i);
236 if (v == NULL || !PyString_Check(v)) {
237 PyErr_BadInternalCall();
Guido van Rossum681d79a1995-07-18 14:51:37 +0000238 return NULL;
239 }
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000240 PyString_InternInPlace(&PyTuple_GET_ITEM(names, i));
Guido van Rossum681d79a1995-07-18 14:51:37 +0000241 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000242 for (i = PyTuple_Size(varnames); --i >= 0; ) {
243 PyObject *v = PyTuple_GetItem(varnames, i);
244 if (v == NULL || !PyString_Check(v)) {
245 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000246 return NULL;
247 }
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000248 PyString_InternInPlace(&PyTuple_GET_ITEM(varnames, i));
249 }
250 /* Intern selected string constants */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000251 for (i = PyTuple_Size(consts); --i >= 0; ) {
252 PyObject *v = PyTuple_GetItem(consts, i);
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000253 char *p;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000254 if (!PyString_Check(v))
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000255 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000256 p = PyString_AsString(v);
Guido van Rossum644a12b1997-04-09 19:24:53 +0000257 if ((int)strspn(p, NAME_CHARS)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000258 != PyString_Size(v))
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000259 continue;
260 PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000261 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000262 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000263 if (co != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +0000264 co->co_argcount = argcount;
265 co->co_nlocals = nlocals;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000266 co->co_stacksize = stacksize;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000267 co->co_flags = flags;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000268 Py_INCREF(code);
269 co->co_code = (PyStringObject *)code;
270 Py_INCREF(consts);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000271 co->co_consts = consts;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000272 Py_INCREF(names);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000273 co->co_names = names;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000274 Py_INCREF(varnames);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000275 co->co_varnames = varnames;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000276 Py_INCREF(filename);
Guido van Rossuma082ce41991-06-04 19:41:56 +0000277 co->co_filename = filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000278 Py_INCREF(name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000279 co->co_name = name;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000280 co->co_firstlineno = firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000281 Py_INCREF(lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000282 co->co_lnotab = lnotab;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000283 }
284 return co;
285}
286
287
288/* Data structure used internally */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000289
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000290struct compiling {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000291 PyObject *c_code; /* string */
292 PyObject *c_consts; /* list of objects */
293 PyObject *c_names; /* list of strings (names) */
294 PyObject *c_globals; /* dictionary (value=None) */
295 PyObject *c_locals; /* dictionary (value=localID) */
296 PyObject *c_varnames; /* list (inverse of c_locals) */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000297 int c_nlocals; /* index of next local */
298 int c_argcount; /* number of top-level arguments */
299 int c_flags; /* same as co_flags */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000300 int c_nexti; /* index into c_code */
301 int c_errors; /* counts errors occurred */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000302 int c_infunction; /* set when compiling a function */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000303 int c_interactive; /* generating code for interactive command */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000304 int c_loops; /* counts nested loops */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000305 int c_begin; /* begin of current loop, for 'continue' */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000306 int c_block[CO_MAXBLOCKS]; /* stack of block types */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000307 int c_nblocks; /* current block stack level */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000308 char *c_filename; /* filename of current node */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000309 char *c_name; /* name of object (e.g. function) */
Guido van Rossum452a9831996-09-17 14:32:04 +0000310 int c_lineno; /* Current line number */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000311 int c_stacklevel; /* Current stack level */
312 int c_maxstacklevel; /* Maximum stack level */
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000313 int c_firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000314 PyObject *c_lnotab; /* Table mapping address to line number */
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000315 int c_last_addr, c_last_line, c_lnotab_next;
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000316#ifdef PRIVATE_NAME_MANGLING
317 char *c_private; /* for private name mangling */
318#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000319};
320
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000321
Guido van Rossum452a9831996-09-17 14:32:04 +0000322/* Error message including line number */
323
324static void
325com_error(c, exc, msg)
326 struct compiling *c;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000327 PyObject *exc;
Guido van Rossum452a9831996-09-17 14:32:04 +0000328 char *msg;
329{
330 int n = strlen(msg);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000331 PyObject *v;
Guido van Rossum452a9831996-09-17 14:32:04 +0000332 char buffer[30];
333 char *s;
Guido van Rossum635abd21997-01-06 22:56:52 +0000334 c->c_errors++;
Guido van Rossum452a9831996-09-17 14:32:04 +0000335 if (c->c_lineno <= 1) {
336 /* Unknown line number or single interactive command */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000337 PyErr_SetString(exc, msg);
Guido van Rossum452a9831996-09-17 14:32:04 +0000338 return;
339 }
340 sprintf(buffer, " (line %d)", c->c_lineno);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000341 v = PyString_FromStringAndSize((char *)NULL, n + strlen(buffer));
Guido van Rossum452a9831996-09-17 14:32:04 +0000342 if (v == NULL)
343 return; /* MemoryError, too bad */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000344 s = PyString_AS_STRING((PyStringObject *)v);
Guido van Rossum452a9831996-09-17 14:32:04 +0000345 strcpy(s, msg);
346 strcat(s, buffer);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000347 PyErr_SetObject(exc, v);
348 Py_DECREF(v);
Guido van Rossum452a9831996-09-17 14:32:04 +0000349}
350
351
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000352/* Interface to the block stack */
353
354static void
355block_push(c, type)
356 struct compiling *c;
357 int type;
358{
Guido van Rossum8b993a91997-01-17 21:04:03 +0000359 if (c->c_nblocks >= CO_MAXBLOCKS) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000360 com_error(c, PyExc_SystemError,
361 "too many statically nested blocks");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000362 }
363 else {
364 c->c_block[c->c_nblocks++] = type;
365 }
366}
367
368static void
369block_pop(c, type)
370 struct compiling *c;
371 int type;
372{
373 if (c->c_nblocks > 0)
374 c->c_nblocks--;
375 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000376 com_error(c, PyExc_SystemError, "bad block pop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000377 }
378}
379
380
Guido van Rossum681d79a1995-07-18 14:51:37 +0000381/* Prototype forward declarations */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000382
Guido van Rossum79f25d91997-04-29 20:08:16 +0000383static int com_init Py_PROTO((struct compiling *, char *));
384static void com_free Py_PROTO((struct compiling *));
385static void com_push Py_PROTO((struct compiling *, int));
386static void com_pop Py_PROTO((struct compiling *, int));
387static void com_done Py_PROTO((struct compiling *));
388static void com_node Py_PROTO((struct compiling *, struct _node *));
389static void com_factor Py_PROTO((struct compiling *, struct _node *));
390static void com_addbyte Py_PROTO((struct compiling *, int));
391static void com_addint Py_PROTO((struct compiling *, int));
392static void com_addoparg Py_PROTO((struct compiling *, int, int));
393static void com_addfwref Py_PROTO((struct compiling *, int, int *));
394static void com_backpatch Py_PROTO((struct compiling *, int));
395static int com_add Py_PROTO((struct compiling *, PyObject *, PyObject *));
396static int com_addconst Py_PROTO((struct compiling *, PyObject *));
397static int com_addname Py_PROTO((struct compiling *, PyObject *));
398static void com_addopname Py_PROTO((struct compiling *, int, node *));
399static void com_list Py_PROTO((struct compiling *, node *, int));
400static int com_argdefs Py_PROTO((struct compiling *, node *));
401static int com_newlocal Py_PROTO((struct compiling *, char *));
402static PyCodeObject *icompile Py_PROTO((struct _node *, struct compiling *));
403static PyCodeObject *jcompile Py_PROTO((struct _node *, char *,
404 struct compiling *));
405static PyObject *parsestrplus Py_PROTO((node *));
406static PyObject *parsestr Py_PROTO((char *));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000407
408static int
Guido van Rossum590baa41993-11-30 13:40:46 +0000409com_init(c, filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000410 struct compiling *c;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000411 char *filename;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000412{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000413 if ((c->c_code = PyString_FromStringAndSize((char *)NULL,
414 1000)) == NULL)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000415 goto fail_3;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000416 if ((c->c_consts = PyList_New(0)) == NULL)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000417 goto fail_2;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000418 if ((c->c_names = PyList_New(0)) == NULL)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000419 goto fail_1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000420 if ((c->c_globals = PyDict_New()) == NULL)
Guido van Rossumc5e96291991-12-10 13:53:51 +0000421 goto fail_0;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000422 if ((c->c_locals = PyDict_New()) == NULL)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000423 goto fail_00;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000424 if ((c->c_varnames = PyList_New(0)) == NULL)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000425 goto fail_000;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000426 if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL,
427 1000)) == NULL)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000428 goto fail_0000;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000429 c->c_nlocals = 0;
430 c->c_argcount = 0;
431 c->c_flags = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000432 c->c_nexti = 0;
433 c->c_errors = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000434 c->c_infunction = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000435 c->c_interactive = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000436 c->c_loops = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000437 c->c_begin = 0;
438 c->c_nblocks = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000439 c->c_filename = filename;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000440 c->c_name = "?";
Guido van Rossum452a9831996-09-17 14:32:04 +0000441 c->c_lineno = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000442 c->c_stacklevel = 0;
443 c->c_maxstacklevel = 0;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000444 c->c_firstlineno = 0;
445 c->c_last_addr = 0;
446 c->c_last_line = 0;
447 c-> c_lnotab_next = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000448 return 1;
449
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000450 fail_0000:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000451 Py_DECREF(c->c_lnotab);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000452 fail_000:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000453 Py_DECREF(c->c_locals);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000454 fail_00:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000455 Py_DECREF(c->c_globals);
Guido van Rossumc5e96291991-12-10 13:53:51 +0000456 fail_0:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000457 Py_DECREF(c->c_names);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000458 fail_1:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000459 Py_DECREF(c->c_consts);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000460 fail_2:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000461 Py_DECREF(c->c_code);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000462 fail_3:
463 return 0;
464}
465
466static void
467com_free(c)
468 struct compiling *c;
469{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000470 Py_XDECREF(c->c_code);
471 Py_XDECREF(c->c_consts);
472 Py_XDECREF(c->c_names);
473 Py_XDECREF(c->c_globals);
474 Py_XDECREF(c->c_locals);
475 Py_XDECREF(c->c_varnames);
476 Py_XDECREF(c->c_lnotab);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000477}
478
479static void
Guido van Rossum8b993a91997-01-17 21:04:03 +0000480com_push(c, n)
481 struct compiling *c;
482 int n;
483{
484 c->c_stacklevel += n;
485 if (c->c_stacklevel > c->c_maxstacklevel)
486 c->c_maxstacklevel = c->c_stacklevel;
487}
488
489static void
490com_pop(c, n)
491 struct compiling *c;
492 int n;
493{
494 if (c->c_stacklevel < n) {
495 fprintf(stderr,
496 "%s:%d: underflow! nexti=%d, level=%d, n=%d\n",
497 c->c_filename, c->c_lineno,
498 c->c_nexti, c->c_stacklevel, n);
499 c->c_stacklevel = 0;
500 }
501 else
502 c->c_stacklevel -= n;
503}
504
505static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000506com_done(c)
507 struct compiling *c;
508{
509 if (c->c_code != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000510 _PyString_Resize(&c->c_code, c->c_nexti);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000511 if (c->c_lnotab != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000512 _PyString_Resize(&c->c_lnotab, c->c_lnotab_next);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000513}
514
515static void
516com_addbyte(c, byte)
517 struct compiling *c;
518 int byte;
519{
520 int len;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000521 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000522 if (byte < 0 || byte > 255) {
Guido van Rossum01cfd441991-10-20 20:12:38 +0000523 /*
Guido van Rossum3f5da241990-12-20 15:06:42 +0000524 fprintf(stderr, "XXX compiling bad byte: %d\n", byte);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000525 fatal("com_addbyte: byte out of range");
Guido van Rossum01cfd441991-10-20 20:12:38 +0000526 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000527 com_error(c, PyExc_SystemError,
528 "com_addbyte: byte out of range");
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000529 }
530 if (c->c_code == NULL)
531 return;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000532 len = PyString_Size(c->c_code);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000533 if (c->c_nexti >= len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000534 if (_PyString_Resize(&c->c_code, len+1000) != 0) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000535 c->c_errors++;
536 return;
537 }
538 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000539 PyString_AsString(c->c_code)[c->c_nexti++] = byte;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000540}
541
542static void
543com_addint(c, x)
544 struct compiling *c;
545 int x;
546{
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000547 com_addbyte(c, x & 0xff);
548 com_addbyte(c, x >> 8); /* XXX x should be positive */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000549}
550
551static void
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000552com_add_lnotab(c, addr, line)
553 struct compiling *c;
554 int addr;
555 int line;
556{
557 int size;
558 char *p;
559 if (c->c_lnotab == NULL)
560 return;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000561 size = PyString_Size(c->c_lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000562 if (c->c_lnotab_next+2 > size) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000563 if (_PyString_Resize(&c->c_lnotab, size + 1000) < 0) {
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000564 c->c_errors++;
565 return;
566 }
567 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000568 p = PyString_AsString(c->c_lnotab) + c->c_lnotab_next;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000569 *p++ = addr;
570 *p++ = line;
571 c->c_lnotab_next += 2;
572}
573
574static void
575com_set_lineno(c, lineno)
576 struct compiling *c;
577 int lineno;
578{
579 c->c_lineno = lineno;
580 if (c->c_firstlineno == 0) {
581 c->c_firstlineno = c->c_last_line = lineno;
582 }
583 else {
584 int incr_addr = c->c_nexti - c->c_last_addr;
585 int incr_line = lineno - c->c_last_line;
586 while (incr_addr > 0 || incr_line > 0) {
587 int trunc_addr = incr_addr;
588 int trunc_line = incr_line;
589 if (trunc_addr > 255)
590 trunc_addr = 255;
591 if (trunc_line > 255)
592 trunc_line = 255;
593 com_add_lnotab(c, trunc_addr, trunc_line);
594 incr_addr -= trunc_addr;
595 incr_line -= trunc_line;
596 }
597 c->c_last_addr = c->c_nexti;
598 c->c_last_line = lineno;
599 }
600}
601
602static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000603com_addoparg(c, op, arg)
604 struct compiling *c;
605 int op;
606 int arg;
607{
Guido van Rossum8e793d91997-03-03 19:13:14 +0000608 if (op == SET_LINENO) {
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000609 com_set_lineno(c, arg);
Guido van Rossum8e793d91997-03-03 19:13:14 +0000610 if (Py_OptimizeFlag)
611 return;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000612 }
Guido van Rossum8e793d91997-03-03 19:13:14 +0000613 com_addbyte(c, op);
614 com_addint(c, arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000615}
616
617static void
618com_addfwref(c, op, p_anchor)
619 struct compiling *c;
620 int op;
621 int *p_anchor;
622{
623 /* Compile a forward reference for backpatching */
624 int here;
625 int anchor;
626 com_addbyte(c, op);
627 here = c->c_nexti;
628 anchor = *p_anchor;
629 *p_anchor = here;
630 com_addint(c, anchor == 0 ? 0 : here - anchor);
631}
632
633static void
634com_backpatch(c, anchor)
635 struct compiling *c;
636 int anchor; /* Must be nonzero */
637{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000638 unsigned char *code = (unsigned char *) PyString_AsString(c->c_code);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000639 int target = c->c_nexti;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000640 int dist;
641 int prev;
642 for (;;) {
643 /* Make the JUMP instruction at anchor point to target */
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000644 prev = code[anchor] + (code[anchor+1] << 8);
645 dist = target - (anchor+2);
646 code[anchor] = dist & 0xff;
647 code[anchor+1] = dist >> 8;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000648 if (!prev)
649 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000650 anchor -= prev;
651 }
652}
653
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000654/* Handle literals and names uniformly */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000655
656static int
657com_add(c, list, v)
658 struct compiling *c;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000659 PyObject *list;
660 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000661{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000662 int n = PyList_Size(list);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000663 int i;
664 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 Rossum79f25d91997-04-29 20:08:16 +0000669 if (PyList_Append(list, v) != 0)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000670 c->c_errors++;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000671 return n;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000672}
673
674static int
675com_addconst(c, v)
676 struct compiling *c;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000677 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000678{
679 return com_add(c, c->c_consts, v);
680}
681
682static int
683com_addname(c, v)
684 struct compiling *c;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000685 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000686{
687 return com_add(c, c->c_names, v);
688}
689
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000690#ifdef PRIVATE_NAME_MANGLING
691static int
692com_mangle(c, name, buffer, maxlen)
693 struct compiling *c;
694 char *name;
695 char *buffer;
696 int maxlen;
697{
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000698 /* Name mangling: __private becomes _classname__private.
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000699 This is independent from how the name is used. */
700 char *p;
701 int nlen, plen;
702 nlen = strlen(name);
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000703 if (nlen+2 >= maxlen)
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000704 return 0; /* Don't mangle __extremely_long_names */
705 if (name[nlen-1] == '_' && name[nlen-2] == '_')
706 return 0; /* Don't mangle __whatever__ */
707 p = c->c_private;
708 /* Strip leading underscores from class name */
709 while (*p == '_')
710 p++;
711 if (*p == '\0')
712 return 0; /* Don't mangle if class is just underscores */
713 plen = strlen(p);
714 if (plen + nlen >= maxlen)
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000715 plen = maxlen-nlen-2; /* Truncate class name if too long */
716 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000717 buffer[0] = '_';
718 strncpy(buffer+1, p, plen);
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000719 strcpy(buffer+1+plen, name);
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000720 /* fprintf(stderr, "mangle %s -> %s\n", name, buffer); */
721 return 1;
722}
723#endif
724
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000725static void
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000726com_addopnamestr(c, op, name)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000727 struct compiling *c;
728 int op;
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000729 char *name;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000730{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000731 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000732 int i;
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000733#ifdef PRIVATE_NAME_MANGLING
734 char buffer[256];
735 if (name != NULL && name[0] == '_' && name[1] == '_' &&
736 c->c_private != NULL &&
737 com_mangle(c, name, buffer, (int)sizeof(buffer)))
738 name = buffer;
739#endif
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000740 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000741 c->c_errors++;
742 i = 255;
743 }
744 else {
745 i = com_addname(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000746 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000747 }
Guido van Rossumc5e96291991-12-10 13:53:51 +0000748 /* Hack to replace *_NAME opcodes by *_GLOBAL if necessary */
749 switch (op) {
750 case LOAD_NAME:
751 case STORE_NAME:
752 case DELETE_NAME:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000753 if (PyDict_GetItemString(c->c_globals, name) != NULL) {
Guido van Rossumc5e96291991-12-10 13:53:51 +0000754 switch (op) {
755 case LOAD_NAME: op = LOAD_GLOBAL; break;
756 case STORE_NAME: op = STORE_GLOBAL; break;
757 case DELETE_NAME: op = DELETE_GLOBAL; break;
758 }
759 }
760 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000761 com_addoparg(c, op, i);
762}
763
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000764static void
765com_addopname(c, op, n)
766 struct compiling *c;
767 int op;
768 node *n;
769{
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000770 char *name;
771 char buffer[1000];
772 /* XXX it is possible to write this code without the 1000
773 chars on the total length of dotted names, I just can't be
774 bothered right now */
775 if (TYPE(n) == STAR)
776 name = "*";
777 else if (TYPE(n) == dotted_name) {
778 char *p = buffer;
779 int i;
780 name = buffer;
781 for (i = 0; i < NCH(n); i += 2) {
782 char *s = STR(CHILD(n, i));
783 if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000784 com_error(c, PyExc_MemoryError,
Guido van Rossum452a9831996-09-17 14:32:04 +0000785 "dotted_name too long");
Guido van Rossumd9dfaf51995-02-17 15:04:57 +0000786 name = NULL;
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000787 break;
788 }
789 if (p != buffer)
790 *p++ = '.';
791 strcpy(p, s);
792 p = strchr(p, '\0');
793 }
794 }
795 else {
796 REQ(n, NAME);
797 name = STR(n);
798 }
799 com_addopnamestr(c, op, name);
800}
801
Guido van Rossum79f25d91997-04-29 20:08:16 +0000802static PyObject *
Guido van Rossum452a9831996-09-17 14:32:04 +0000803parsenumber(co, s)
804 struct compiling *co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000805 char *s;
806{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000807 extern long PyOS_strtol Py_PROTO((const char *, char **, int));
808 extern unsigned long PyOS_strtoul Py_PROTO((const char *,
809 char **, int));
810 extern double atof Py_PROTO((const char *));
Guido van Rossumc5e96291991-12-10 13:53:51 +0000811 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000812 long x;
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000813 double dx;
Guido van Rossum50564e81996-01-12 01:13:16 +0000814#ifndef WITHOUT_COMPLEX
Guido van Rossum530956d1996-07-21 02:27:43 +0000815 Py_complex c;
Guido van Rossum50564e81996-01-12 01:13:16 +0000816 int imflag;
817#endif
818
Guido van Rossum282914b1991-04-04 10:42:56 +0000819 errno = 0;
Guido van Rossumc5e96291991-12-10 13:53:51 +0000820 end = s + strlen(s) - 1;
Guido van Rossum50564e81996-01-12 01:13:16 +0000821#ifndef WITHOUT_COMPLEX
Guido van Rossum15ad9a61996-01-26 20:53:56 +0000822 imflag = *end == 'j' || *end == 'J';
Guido van Rossum50564e81996-01-12 01:13:16 +0000823#endif
Guido van Rossumf1aeab71992-03-27 17:28:26 +0000824 if (*end == 'l' || *end == 'L')
Guido van Rossum79f25d91997-04-29 20:08:16 +0000825 return PyLong_FromString(s, (char **)0, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +0000826 if (s[0] == '0')
Guido van Rossum79f25d91997-04-29 20:08:16 +0000827 x = (long) PyOS_strtoul(s, &end, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +0000828 else
Guido van Rossum79f25d91997-04-29 20:08:16 +0000829 x = PyOS_strtol(s, &end, 0);
Guido van Rossum282914b1991-04-04 10:42:56 +0000830 if (*end == '\0') {
831 if (errno != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000832 com_error(co, PyExc_OverflowError,
Guido van Rossum452a9831996-09-17 14:32:04 +0000833 "integer literal too large");
Guido van Rossum282914b1991-04-04 10:42:56 +0000834 return NULL;
835 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000836 return PyInt_FromLong(x);
Guido van Rossum282914b1991-04-04 10:42:56 +0000837 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000838 /* XXX Huge floats may silently fail */
Guido van Rossum50564e81996-01-12 01:13:16 +0000839#ifndef WITHOUT_COMPLEX
840 if (imflag) {
841 c.real = 0.;
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000842 PyFPE_START_PROTECT("atof", return 0)
Guido van Rossum50564e81996-01-12 01:13:16 +0000843 c.imag = atof(s);
Guido van Rossum45b83911997-03-14 04:32:50 +0000844 PyFPE_END_PROTECT(c)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000845 return PyComplex_FromCComplex(c);
Guido van Rossum50564e81996-01-12 01:13:16 +0000846 }
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000847 else {
Guido van Rossum50564e81996-01-12 01:13:16 +0000848#endif
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000849 PyFPE_START_PROTECT("atof", return 0)
850 dx = atof(s);
Guido van Rossum45b83911997-03-14 04:32:50 +0000851 PyFPE_END_PROTECT(dx)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000852 return PyFloat_FromDouble(dx);
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000853 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000854}
855
Guido van Rossum79f25d91997-04-29 20:08:16 +0000856static PyObject *
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000857parsestr(s)
858 char *s;
859{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000860 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000861 int len;
862 char *buf;
863 char *p;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000864 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000865 int c;
Guido van Rossum5f5e8171997-04-06 03:41:40 +0000866 int first = *s;
867 int quote = first;
868 if (isalpha(quote) || quote == '_')
869 quote = *++s;
Guido van Rossum8054fad1993-10-26 15:19:44 +0000870 if (quote != '\'' && quote != '\"') {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000871 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000872 return NULL;
873 }
874 s++;
875 len = strlen(s);
Guido van Rossum8054fad1993-10-26 15:19:44 +0000876 if (s[--len] != quote) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000877 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000878 return NULL;
879 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000880 if (len >= 4 && s[0] == quote && s[1] == quote) {
881 s += 2;
882 len -= 2;
883 if (s[--len] != quote || s[--len] != quote) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000884 PyErr_BadInternalCall();
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000885 return NULL;
886 }
887 }
Guido van Rossum5f5e8171997-04-06 03:41:40 +0000888 if (first != quote || strchr(s, '\\') == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000889 return PyString_FromStringAndSize(s, len);
890 v = PyString_FromStringAndSize((char *)NULL, len);
891 p = buf = PyString_AsString(v);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000892 end = s + len;
893 while (s < end) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000894 if (*s != '\\') {
895 *p++ = *s++;
896 continue;
897 }
898 s++;
899 switch (*s++) {
900 /* XXX This assumes ASCII! */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000901 case '\n': break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000902 case '\\': *p++ = '\\'; break;
903 case '\'': *p++ = '\''; break;
Guido van Rossum8054fad1993-10-26 15:19:44 +0000904 case '\"': *p++ = '\"'; break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000905 case 'b': *p++ = '\b'; break;
906 case 'f': *p++ = '\014'; break; /* FF */
907 case 't': *p++ = '\t'; break;
908 case 'n': *p++ = '\n'; break;
909 case 'r': *p++ = '\r'; break;
910 case 'v': *p++ = '\013'; break; /* VT */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000911 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
912 case '0': case '1': case '2': case '3':
913 case '4': case '5': case '6': case '7':
914 c = s[-1] - '0';
915 if ('0' <= *s && *s <= '7') {
916 c = (c<<3) + *s++ - '0';
917 if ('0' <= *s && *s <= '7')
918 c = (c<<3) + *s++ - '0';
919 }
920 *p++ = c;
921 break;
922 case 'x':
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000923 if (isxdigit(Py_CHARMASK(*s))) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000924 sscanf(s, "%x", &c);
925 *p++ = c;
926 do {
927 s++;
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000928 } while (isxdigit(Py_CHARMASK(*s)));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000929 break;
930 }
931 /* FALLTHROUGH */
932 default: *p++ = '\\'; *p++ = s[-1]; break;
933 }
934 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000935 _PyString_Resize(&v, (int)(p - buf));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000936 return v;
937}
938
Guido van Rossum79f25d91997-04-29 20:08:16 +0000939static PyObject *
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000940parsestrplus(n)
941 node *n;
942{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000943 PyObject *v;
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000944 int i;
945 REQ(CHILD(n, 0), STRING);
946 if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) {
947 /* String literal concatenation */
948 for (i = 1; i < NCH(n) && v != NULL; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000949 PyString_ConcatAndDel(&v, parsestr(STR(CHILD(n, i))));
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000950 }
951 }
952 return v;
953}
954
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000955static void
956com_list_constructor(c, n)
957 struct compiling *c;
958 node *n;
959{
960 int len;
961 int i;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000962 if (TYPE(n) != testlist)
963 REQ(n, exprlist);
964 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
965 len = (NCH(n) + 1) / 2;
966 for (i = 0; i < NCH(n); i += 2)
967 com_node(c, CHILD(n, i));
968 com_addoparg(c, BUILD_LIST, len);
Guido van Rossum8b993a91997-01-17 21:04:03 +0000969 com_pop(c, len-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000970}
971
972static void
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000973com_dictmaker(c, n)
974 struct compiling *c;
975 node *n;
976{
977 int i;
978 /* dictmaker: test ':' test (',' test ':' value)* [','] */
979 for (i = 0; i+2 < NCH(n); i += 4) {
980 /* We must arrange things just right for STORE_SUBSCR.
981 It wants the stack to look like (value) (dict) (key) */
982 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +0000983 com_push(c, 1);
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000984 com_node(c, CHILD(n, i+2)); /* value */
985 com_addbyte(c, ROT_TWO);
986 com_node(c, CHILD(n, i)); /* key */
987 com_addbyte(c, STORE_SUBSCR);
Guido van Rossum8b993a91997-01-17 21:04:03 +0000988 com_pop(c, 3);
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000989 }
990}
991
992static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000993com_atom(c, n)
994 struct compiling *c;
995 node *n;
996{
997 node *ch;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000998 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000999 int i;
1000 REQ(n, atom);
1001 ch = CHILD(n, 0);
1002 switch (TYPE(ch)) {
1003 case LPAR:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001004 if (TYPE(CHILD(n, 1)) == RPAR) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001005 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001006 com_push(c, 1);
1007 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001008 else
1009 com_node(c, CHILD(n, 1));
1010 break;
1011 case LSQB:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001012 if (TYPE(CHILD(n, 1)) == RSQB) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001013 com_addoparg(c, BUILD_LIST, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001014 com_push(c, 1);
1015 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001016 else
1017 com_list_constructor(c, CHILD(n, 1));
1018 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001019 case LBRACE: /* '{' [dictmaker] '}' */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001020 com_addoparg(c, BUILD_MAP, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001021 com_push(c, 1);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001022 if (TYPE(CHILD(n, 1)) != RBRACE)
1023 com_dictmaker(c, CHILD(n, 1));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001024 break;
1025 case BACKQUOTE:
1026 com_node(c, CHILD(n, 1));
1027 com_addbyte(c, UNARY_CONVERT);
1028 break;
1029 case NUMBER:
Guido van Rossum452a9831996-09-17 14:32:04 +00001030 if ((v = parsenumber(c, STR(ch))) == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001031 i = 255;
1032 }
1033 else {
1034 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001035 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001036 }
1037 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001038 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001039 break;
1040 case STRING:
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001041 v = parsestrplus(n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001042 if (v == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001043 c->c_errors++;
1044 i = 255;
1045 }
1046 else {
1047 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001048 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001049 }
1050 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001051 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001052 break;
1053 case NAME:
1054 com_addopname(c, LOAD_NAME, ch);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001055 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001056 break;
1057 default:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001058 /* XXX fprintf(stderr, "node type %d\n", TYPE(ch)); */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001059 com_error(c, PyExc_SystemError,
1060 "com_atom: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001061 }
1062}
1063
1064static void
1065com_slice(c, n, op)
1066 struct compiling *c;
1067 node *n;
1068 int op;
1069{
1070 if (NCH(n) == 1) {
1071 com_addbyte(c, op);
1072 }
1073 else if (NCH(n) == 2) {
1074 if (TYPE(CHILD(n, 0)) != COLON) {
1075 com_node(c, CHILD(n, 0));
1076 com_addbyte(c, op+1);
1077 }
1078 else {
1079 com_node(c, CHILD(n, 1));
1080 com_addbyte(c, op+2);
1081 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00001082 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001083 }
1084 else {
1085 com_node(c, CHILD(n, 0));
1086 com_node(c, CHILD(n, 2));
1087 com_addbyte(c, op+3);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001088 com_pop(c, 2);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001089 }
1090}
1091
Guido van Rossum635abd21997-01-06 22:56:52 +00001092static void
1093com_argument(c, n, pkeywords)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001094 struct compiling *c;
1095 node *n; /* argument */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001096 PyObject **pkeywords;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001097{
1098 node *m;
Guido van Rossum8b993a91997-01-17 21:04:03 +00001099 REQ(n, argument); /* [test '='] test; really [keyword '='] test */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001100 if (NCH(n) == 1) {
Guido van Rossum635abd21997-01-06 22:56:52 +00001101 if (*pkeywords != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001102 com_error(c, PyExc_SyntaxError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001103 "non-keyword arg after keyword arg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001104 }
1105 else {
1106 com_node(c, CHILD(n, 0));
1107 }
Guido van Rossum635abd21997-01-06 22:56:52 +00001108 return;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001109 }
1110 m = n;
1111 do {
1112 m = CHILD(m, 0);
1113 } while (NCH(m) == 1);
1114 if (TYPE(m) != NAME) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001115 com_error(c, PyExc_SyntaxError,
1116 "keyword can't be an expression");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001117 }
1118 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001119 PyObject *v = PyString_InternFromString(STR(m));
Guido van Rossum635abd21997-01-06 22:56:52 +00001120 if (v != NULL && *pkeywords == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001121 *pkeywords = PyDict_New();
Guido van Rossum635abd21997-01-06 22:56:52 +00001122 if (v == NULL || *pkeywords == NULL)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001123 c->c_errors++;
1124 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001125 if (PyDict_GetItem(*pkeywords, v) != NULL)
1126 com_error(c, PyExc_SyntaxError,
Guido van Rossum635abd21997-01-06 22:56:52 +00001127 "duplicate keyword argument");
1128 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001129 if (PyDict_SetItem(*pkeywords, v, v) != 0)
Guido van Rossum635abd21997-01-06 22:56:52 +00001130 c->c_errors++;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001131 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001132 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001133 Py_DECREF(v);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001134 }
1135 }
1136 com_node(c, CHILD(n, 2));
Guido van Rossumf10570b1995-07-07 22:53:21 +00001137}
1138
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001139static void
1140com_call_function(c, n)
1141 struct compiling *c;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001142 node *n; /* EITHER arglist OR ')' */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001143{
1144 if (TYPE(n) == RPAR) {
Guido van Rossumf10570b1995-07-07 22:53:21 +00001145 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001146 }
1147 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001148 PyObject *keywords = NULL;
Guido van Rossum635abd21997-01-06 22:56:52 +00001149 int i, na, nk;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001150 REQ(n, arglist);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001151 na = 0;
1152 nk = 0;
1153 for (i = 0; i < NCH(n); i += 2) {
Guido van Rossum635abd21997-01-06 22:56:52 +00001154 com_argument(c, CHILD(n, i), &keywords);
1155 if (keywords == NULL)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001156 na++;
1157 else
1158 nk++;
1159 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001160 Py_XDECREF(keywords);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001161 if (na > 255 || nk > 255) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001162 com_error(c, PyExc_SyntaxError,
1163 "more than 255 arguments");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001164 }
1165 com_addoparg(c, CALL_FUNCTION, na | (nk << 8));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001166 com_pop(c, na + 2*nk);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001167 }
1168}
1169
1170static void
1171com_select_member(c, n)
1172 struct compiling *c;
1173 node *n;
1174{
1175 com_addopname(c, LOAD_ATTR, n);
1176}
1177
1178static void
Guido van Rossum8861b741996-07-30 16:49:37 +00001179com_sliceobj(c, n)
1180 struct compiling *c;
1181 node *n;
1182{
1183 int i=0;
1184 int ns=2; /* number of slice arguments */
Guido van Rossum8861b741996-07-30 16:49:37 +00001185 node *ch;
1186
1187 /* first argument */
1188 if (TYPE(CHILD(n,i)) == COLON) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001189 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001190 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001191 i++;
1192 }
1193 else {
1194 com_node(c, CHILD(n,i));
1195 i++;
1196 REQ(CHILD(n,i),COLON);
1197 i++;
1198 }
1199 /* second argument */
1200 if (i < NCH(n) && TYPE(CHILD(n,i)) == test) {
1201 com_node(c, CHILD(n,i));
1202 i++;
1203 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00001204 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001205 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001206 com_push(c, 1);
1207 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001208 /* remaining arguments */
1209 for (; i < NCH(n); i++) {
1210 ns++;
1211 ch=CHILD(n,i);
1212 REQ(ch, sliceop);
1213 if (NCH(ch) == 1) {
1214 /* right argument of ':' missing */
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);
Guido van Rossum8861b741996-07-30 16:49:37 +00001217 }
1218 else
1219 com_node(c, CHILD(ch,1));
1220 }
1221 com_addoparg(c, BUILD_SLICE, ns);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001222 com_pop(c, 1 + (ns == 3));
Guido van Rossum8861b741996-07-30 16:49:37 +00001223}
1224
1225static void
1226com_subscript(c, n)
1227 struct compiling *c;
1228 node *n;
1229{
1230 node *ch;
1231 REQ(n, subscript);
1232 ch = CHILD(n,0);
1233 /* check for rubber index */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001234 if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
Guido van Rossume449af71996-10-11 16:25:41 +00001235 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001236 com_push(c, 1);
1237 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001238 else {
1239 /* check for slice */
1240 if ((TYPE(ch) == COLON || NCH(n) > 1))
1241 com_sliceobj(c, n);
1242 else {
1243 REQ(ch, test);
1244 com_node(c, ch);
1245 }
1246 }
1247}
1248
1249static void
1250com_subscriptlist(c, n, assigning)
1251 struct compiling *c;
1252 node *n;
1253 int assigning;
1254{
1255 int i, op;
1256 REQ(n, subscriptlist);
1257 /* Check to make backward compatible slice behavior for '[i:j]' */
1258 if (NCH(n) == 1) {
1259 node *sub = CHILD(n, 0); /* subscript */
1260 /* Make it is a simple slice.
Guido van Rossum8b993a91997-01-17 21:04:03 +00001261 Should have exactly one colon. */
Guido van Rossum8861b741996-07-30 16:49:37 +00001262 if ((TYPE(CHILD(sub, 0)) == COLON
1263 || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
Guido van Rossum8b993a91997-01-17 21:04:03 +00001264 && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
1265 {
Guido van Rossum8861b741996-07-30 16:49:37 +00001266 if (assigning == OP_APPLY)
1267 op = SLICE;
1268 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001269 op = ((assigning == OP_ASSIGN) ?
1270 STORE_SLICE : DELETE_SLICE);
Guido van Rossum8861b741996-07-30 16:49:37 +00001271 com_slice(c, sub, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001272 if (op == STORE_SLICE)
1273 com_pop(c, 2);
1274 else if (op == DELETE_SLICE)
1275 com_pop(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001276 return;
1277 }
1278 }
1279 /* Else normal subscriptlist. Compile each subscript. */
1280 for (i = 0; i < NCH(n); i += 2)
1281 com_subscript(c, CHILD(n, i));
1282 /* Put multiple subscripts into a tuple */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001283 if (NCH(n) > 1) {
1284 i = (NCH(n)+1) / 2;
1285 com_addoparg(c, BUILD_TUPLE, i);
1286 com_pop(c, i-1);
1287 }
1288 if (assigning == OP_APPLY) {
Guido van Rossum8861b741996-07-30 16:49:37 +00001289 op = BINARY_SUBSCR;
Guido van Rossum8b993a91997-01-17 21:04:03 +00001290 i = 1;
1291 }
1292 else if (assigning == OP_ASSIGN) {
1293 op = STORE_SUBSCR;
1294 i = 3;
1295 }
1296 else {
1297 op = DELETE_SUBSCR;
1298 i = 2;
1299 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001300 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001301 com_pop(c, i);
Guido van Rossum8861b741996-07-30 16:49:37 +00001302}
1303
1304static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001305com_apply_trailer(c, n)
1306 struct compiling *c;
1307 node *n;
1308{
1309 REQ(n, trailer);
1310 switch (TYPE(CHILD(n, 0))) {
1311 case LPAR:
1312 com_call_function(c, CHILD(n, 1));
1313 break;
1314 case DOT:
1315 com_select_member(c, CHILD(n, 1));
1316 break;
1317 case LSQB:
Guido van Rossum8861b741996-07-30 16:49:37 +00001318 com_subscriptlist(c, CHILD(n, 1), OP_APPLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001319 break;
1320 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001321 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001322 "com_apply_trailer: unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001323 }
1324}
1325
1326static void
Guido van Rossum50564e81996-01-12 01:13:16 +00001327com_power(c, n)
1328 struct compiling *c;
1329 node *n;
1330{
1331 int i;
1332 REQ(n, power);
1333 com_atom(c, CHILD(n, 0));
1334 for (i = 1; i < NCH(n); i++) {
1335 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
1336 com_factor(c, CHILD(n, i+1));
1337 com_addbyte(c, BINARY_POWER);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001338 com_pop(c, 1);
Guido van Rossum50564e81996-01-12 01:13:16 +00001339 break;
1340 }
1341 else
1342 com_apply_trailer(c, CHILD(n, i));
1343 }
1344}
1345
1346static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001347com_factor(c, n)
1348 struct compiling *c;
1349 node *n;
1350{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001351 REQ(n, factor);
1352 if (TYPE(CHILD(n, 0)) == PLUS) {
1353 com_factor(c, CHILD(n, 1));
1354 com_addbyte(c, UNARY_POSITIVE);
1355 }
1356 else if (TYPE(CHILD(n, 0)) == MINUS) {
1357 com_factor(c, CHILD(n, 1));
1358 com_addbyte(c, UNARY_NEGATIVE);
1359 }
Guido van Rossum7928cd71991-10-24 14:59:31 +00001360 else if (TYPE(CHILD(n, 0)) == TILDE) {
1361 com_factor(c, CHILD(n, 1));
1362 com_addbyte(c, UNARY_INVERT);
1363 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001364 else {
Guido van Rossum50564e81996-01-12 01:13:16 +00001365 com_power(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001366 }
1367}
1368
1369static void
1370com_term(c, n)
1371 struct compiling *c;
1372 node *n;
1373{
1374 int i;
1375 int op;
1376 REQ(n, term);
1377 com_factor(c, CHILD(n, 0));
1378 for (i = 2; i < NCH(n); i += 2) {
1379 com_factor(c, CHILD(n, i));
1380 switch (TYPE(CHILD(n, i-1))) {
1381 case STAR:
1382 op = BINARY_MULTIPLY;
1383 break;
1384 case SLASH:
1385 op = BINARY_DIVIDE;
1386 break;
1387 case PERCENT:
1388 op = BINARY_MODULO;
1389 break;
1390 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001391 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001392 "com_term: operator not *, / or %");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001393 op = 255;
1394 }
1395 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001396 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001397 }
1398}
1399
1400static void
1401com_arith_expr(c, n)
1402 struct compiling *c;
1403 node *n;
1404{
1405 int i;
1406 int op;
1407 REQ(n, arith_expr);
1408 com_term(c, CHILD(n, 0));
1409 for (i = 2; i < NCH(n); i += 2) {
1410 com_term(c, CHILD(n, i));
1411 switch (TYPE(CHILD(n, i-1))) {
1412 case PLUS:
1413 op = BINARY_ADD;
1414 break;
1415 case MINUS:
1416 op = BINARY_SUBTRACT;
1417 break;
1418 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001419 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001420 "com_arith_expr: operator not + or -");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001421 op = 255;
1422 }
1423 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001424 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001425 }
1426}
1427
1428static void
1429com_shift_expr(c, n)
1430 struct compiling *c;
1431 node *n;
1432{
1433 int i;
1434 int op;
1435 REQ(n, shift_expr);
1436 com_arith_expr(c, CHILD(n, 0));
1437 for (i = 2; i < NCH(n); i += 2) {
1438 com_arith_expr(c, CHILD(n, i));
1439 switch (TYPE(CHILD(n, i-1))) {
1440 case LEFTSHIFT:
1441 op = BINARY_LSHIFT;
1442 break;
1443 case RIGHTSHIFT:
1444 op = BINARY_RSHIFT;
1445 break;
1446 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001447 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001448 "com_shift_expr: operator not << or >>");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001449 op = 255;
1450 }
1451 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001452 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001453 }
1454}
1455
1456static void
1457com_and_expr(c, n)
1458 struct compiling *c;
1459 node *n;
1460{
1461 int i;
1462 int op;
1463 REQ(n, and_expr);
1464 com_shift_expr(c, CHILD(n, 0));
1465 for (i = 2; i < NCH(n); i += 2) {
1466 com_shift_expr(c, CHILD(n, i));
1467 if (TYPE(CHILD(n, i-1)) == AMPER) {
1468 op = BINARY_AND;
1469 }
1470 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001471 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001472 "com_and_expr: operator not &");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001473 op = 255;
1474 }
1475 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001476 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001477 }
1478}
1479
1480static void
1481com_xor_expr(c, n)
1482 struct compiling *c;
1483 node *n;
1484{
1485 int i;
1486 int op;
1487 REQ(n, xor_expr);
1488 com_and_expr(c, CHILD(n, 0));
1489 for (i = 2; i < NCH(n); i += 2) {
1490 com_and_expr(c, CHILD(n, i));
1491 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
1492 op = BINARY_XOR;
1493 }
1494 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001495 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001496 "com_xor_expr: operator not ^");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001497 op = 255;
1498 }
1499 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001500 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001501 }
1502}
1503
1504static void
1505com_expr(c, n)
1506 struct compiling *c;
1507 node *n;
1508{
1509 int i;
1510 int op;
1511 REQ(n, expr);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001512 com_xor_expr(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001513 for (i = 2; i < NCH(n); i += 2) {
Guido van Rossum7928cd71991-10-24 14:59:31 +00001514 com_xor_expr(c, CHILD(n, i));
1515 if (TYPE(CHILD(n, i-1)) == VBAR) {
1516 op = BINARY_OR;
1517 }
1518 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001519 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001520 "com_expr: expr operator not |");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001521 op = 255;
1522 }
1523 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001524 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001525 }
1526}
1527
1528static enum cmp_op
1529cmp_type(n)
1530 node *n;
1531{
1532 REQ(n, comp_op);
Guido van Rossum01cfd441991-10-20 20:12:38 +00001533 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001534 | 'in' | 'not' 'in' | 'is' | 'is' not' */
1535 if (NCH(n) == 1) {
1536 n = CHILD(n, 0);
1537 switch (TYPE(n)) {
1538 case LESS: return LT;
1539 case GREATER: return GT;
Guido van Rossum01cfd441991-10-20 20:12:38 +00001540 case EQEQUAL: /* == */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001541 case EQUAL: return EQ;
Guido van Rossum01cfd441991-10-20 20:12:38 +00001542 case LESSEQUAL: return LE;
1543 case GREATEREQUAL: return GE;
1544 case NOTEQUAL: return NE; /* <> or != */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001545 case NAME: if (strcmp(STR(n), "in") == 0) return IN;
1546 if (strcmp(STR(n), "is") == 0) return IS;
1547 }
1548 }
1549 else if (NCH(n) == 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001550 switch (TYPE(CHILD(n, 0))) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001551 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1552 return NOT_IN;
1553 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1554 return IS_NOT;
1555 }
1556 }
1557 return BAD;
1558}
1559
1560static void
1561com_comparison(c, n)
1562 struct compiling *c;
1563 node *n;
1564{
1565 int i;
1566 enum cmp_op op;
1567 int anchor;
1568 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
1569 com_expr(c, CHILD(n, 0));
1570 if (NCH(n) == 1)
1571 return;
1572
1573 /****************************************************************
1574 The following code is generated for all but the last
1575 comparison in a chain:
1576
1577 label: on stack: opcode: jump to:
1578
1579 a <code to load b>
1580 a, b DUP_TOP
1581 a, b, b ROT_THREE
1582 b, a, b COMPARE_OP
1583 b, 0-or-1 JUMP_IF_FALSE L1
1584 b, 1 POP_TOP
1585 b
1586
1587 We are now ready to repeat this sequence for the next
1588 comparison in the chain.
1589
1590 For the last we generate:
1591
1592 b <code to load c>
1593 b, c COMPARE_OP
1594 0-or-1
1595
1596 If there were any jumps to L1 (i.e., there was more than one
1597 comparison), we generate:
1598
1599 0-or-1 JUMP_FORWARD L2
1600 L1: b, 0 ROT_TWO
1601 0, b POP_TOP
1602 0
Guido van Rossum8b993a91997-01-17 21:04:03 +00001603 L2: 0-or-1
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001604 ****************************************************************/
1605
1606 anchor = 0;
1607
1608 for (i = 2; i < NCH(n); i += 2) {
1609 com_expr(c, CHILD(n, i));
1610 if (i+2 < NCH(n)) {
1611 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001612 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001613 com_addbyte(c, ROT_THREE);
1614 }
1615 op = cmp_type(CHILD(n, i-1));
1616 if (op == BAD) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001617 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001618 "com_comparison: unknown comparison op");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001619 }
1620 com_addoparg(c, COMPARE_OP, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001621 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001622 if (i+2 < NCH(n)) {
1623 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1624 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001625 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001626 }
1627 }
1628
1629 if (anchor) {
1630 int anchor2 = 0;
1631 com_addfwref(c, JUMP_FORWARD, &anchor2);
1632 com_backpatch(c, anchor);
1633 com_addbyte(c, ROT_TWO);
1634 com_addbyte(c, POP_TOP);
1635 com_backpatch(c, anchor2);
1636 }
1637}
1638
1639static void
1640com_not_test(c, n)
1641 struct compiling *c;
1642 node *n;
1643{
1644 REQ(n, not_test); /* 'not' not_test | comparison */
1645 if (NCH(n) == 1) {
1646 com_comparison(c, CHILD(n, 0));
1647 }
1648 else {
1649 com_not_test(c, CHILD(n, 1));
1650 com_addbyte(c, UNARY_NOT);
1651 }
1652}
1653
1654static void
1655com_and_test(c, n)
1656 struct compiling *c;
1657 node *n;
1658{
1659 int i;
1660 int anchor;
1661 REQ(n, and_test); /* not_test ('and' not_test)* */
1662 anchor = 0;
1663 i = 0;
1664 for (;;) {
1665 com_not_test(c, CHILD(n, i));
1666 if ((i += 2) >= NCH(n))
1667 break;
1668 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1669 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001670 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001671 }
1672 if (anchor)
1673 com_backpatch(c, anchor);
1674}
1675
1676static void
1677com_test(c, n)
1678 struct compiling *c;
1679 node *n;
1680{
Guido van Rossum8b993a91997-01-17 21:04:03 +00001681 REQ(n, test); /* and_test ('or' and_test)* | lambdef */
Guido van Rossum57531fe1993-11-30 14:57:42 +00001682 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001683 PyObject *v;
Guido van Rossum57531fe1993-11-30 14:57:42 +00001684 int i;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001685 int ndefs = com_argdefs(c, CHILD(n, 0));
Guido van Rossum79f25d91997-04-29 20:08:16 +00001686 v = (PyObject *) icompile(CHILD(n, 0), c);
Guido van Rossum57531fe1993-11-30 14:57:42 +00001687 if (v == NULL) {
1688 c->c_errors++;
1689 i = 255;
1690 }
1691 else {
1692 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001693 Py_DECREF(v);
Guido van Rossum57531fe1993-11-30 14:57:42 +00001694 }
1695 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001696 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001697 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001698 com_pop(c, ndefs);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001699 }
Guido van Rossum57531fe1993-11-30 14:57:42 +00001700 else {
1701 int anchor = 0;
1702 int i = 0;
1703 for (;;) {
1704 com_and_test(c, CHILD(n, i));
1705 if ((i += 2) >= NCH(n))
1706 break;
1707 com_addfwref(c, JUMP_IF_TRUE, &anchor);
1708 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001709 com_pop(c, 1);
Guido van Rossum57531fe1993-11-30 14:57:42 +00001710 }
1711 if (anchor)
1712 com_backpatch(c, anchor);
1713 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001714}
1715
1716static void
Guido van Rossum288a60f1991-12-16 13:05:10 +00001717com_list(c, n, toplevel)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001718 struct compiling *c;
1719 node *n;
Guido van Rossum288a60f1991-12-16 13:05:10 +00001720 int toplevel; /* If nonzero, *always* build a tuple */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001721{
1722 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
Guido van Rossum288a60f1991-12-16 13:05:10 +00001723 if (NCH(n) == 1 && !toplevel) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001724 com_node(c, CHILD(n, 0));
1725 }
1726 else {
1727 int i;
1728 int len;
1729 len = (NCH(n) + 1) / 2;
1730 for (i = 0; i < NCH(n); i += 2)
1731 com_node(c, CHILD(n, i));
1732 com_addoparg(c, BUILD_TUPLE, len);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001733 com_pop(c, len-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001734 }
1735}
1736
1737
1738/* Begin of assignment compilation */
1739
Guido van Rossum79f25d91997-04-29 20:08:16 +00001740static void com_assign_name Py_PROTO((struct compiling *, node *, int));
1741static void com_assign Py_PROTO((struct compiling *, node *, int));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001742
1743static void
1744com_assign_attr(c, n, assigning)
1745 struct compiling *c;
1746 node *n;
1747 int assigning;
1748{
1749 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001750 com_pop(c, assigning ? 2 : 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001751}
1752
1753static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001754com_assign_trailer(c, n, assigning)
1755 struct compiling *c;
1756 node *n;
1757 int assigning;
1758{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001759 REQ(n, trailer);
1760 switch (TYPE(CHILD(n, 0))) {
1761 case LPAR: /* '(' [exprlist] ')' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001762 com_error(c, PyExc_SyntaxError,
1763 "can't assign to function call");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001764 break;
1765 case DOT: /* '.' NAME */
1766 com_assign_attr(c, CHILD(n, 1), assigning);
1767 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00001768 case LSQB: /* '[' subscriptlist ']' */
1769 com_subscriptlist(c, CHILD(n, 1), assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001770 break;
1771 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001772 com_error(c, PyExc_SystemError, "unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001773 }
1774}
1775
1776static void
1777com_assign_tuple(c, n, assigning)
1778 struct compiling *c;
1779 node *n;
1780 int assigning;
1781{
1782 int i;
1783 if (TYPE(n) != testlist)
1784 REQ(n, exprlist);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001785 if (assigning) {
1786 i = (NCH(n)+1)/2;
1787 com_addoparg(c, UNPACK_TUPLE, i);
1788 com_push(c, i-1);
1789 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001790 for (i = 0; i < NCH(n); i += 2)
1791 com_assign(c, CHILD(n, i), assigning);
1792}
1793
1794static void
1795com_assign_list(c, n, assigning)
1796 struct compiling *c;
1797 node *n;
1798 int assigning;
1799{
1800 int i;
Guido van Rossum8b993a91997-01-17 21:04:03 +00001801 if (assigning) {
1802 i = (NCH(n)+1)/2;
1803 com_addoparg(c, UNPACK_LIST, i);
1804 com_push(c, i-1);
1805 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001806 for (i = 0; i < NCH(n); i += 2)
1807 com_assign(c, CHILD(n, i), assigning);
1808}
1809
1810static void
1811com_assign_name(c, n, assigning)
1812 struct compiling *c;
1813 node *n;
1814 int assigning;
1815{
1816 REQ(n, NAME);
1817 com_addopname(c, assigning ? STORE_NAME : DELETE_NAME, n);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001818 if (assigning)
1819 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001820}
1821
1822static void
1823com_assign(c, n, assigning)
1824 struct compiling *c;
1825 node *n;
1826 int assigning;
1827{
1828 /* Loop to avoid trivial recursion */
1829 for (;;) {
1830 switch (TYPE(n)) {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001831
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001832 case exprlist:
1833 case testlist:
1834 if (NCH(n) > 1) {
1835 com_assign_tuple(c, n, assigning);
1836 return;
1837 }
1838 n = CHILD(n, 0);
1839 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001840
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001841 case test:
1842 case and_test:
1843 case not_test:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001844 case comparison:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001845 case expr:
Guido van Rossum7928cd71991-10-24 14:59:31 +00001846 case xor_expr:
1847 case and_expr:
1848 case shift_expr:
1849 case arith_expr:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001850 case term:
Guido van Rossum50564e81996-01-12 01:13:16 +00001851 case factor:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001852 if (NCH(n) > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001853 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001854 "can't assign to operator");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001855 return;
1856 }
1857 n = CHILD(n, 0);
1858 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001859
Guido van Rossum50564e81996-01-12 01:13:16 +00001860 case power: /* atom trailer* ('**' power)* */
1861/* ('+'|'-'|'~') factor | atom trailer* */
1862 if (TYPE(CHILD(n, 0)) != atom) {
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 }
Guido van Rossum50564e81996-01-12 01:13:16 +00001867 if (NCH(n) > 1) { /* trailer or exponent present */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001868 int i;
1869 com_node(c, CHILD(n, 0));
1870 for (i = 1; i+1 < NCH(n); i++) {
Guido van Rossum50564e81996-01-12 01:13:16 +00001871 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001872 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001873 "can't assign to operator");
Guido van Rossum50564e81996-01-12 01:13:16 +00001874 return;
1875 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001876 com_apply_trailer(c, CHILD(n, i));
1877 } /* NB i is still alive */
1878 com_assign_trailer(c,
1879 CHILD(n, i), assigning);
1880 return;
1881 }
1882 n = CHILD(n, 0);
1883 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001884
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001885 case atom:
1886 switch (TYPE(CHILD(n, 0))) {
1887 case LPAR:
1888 n = CHILD(n, 1);
1889 if (TYPE(n) == RPAR) {
1890 /* XXX Should allow () = () ??? */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001891 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001892 "can't assign to ()");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001893 return;
1894 }
1895 break;
1896 case LSQB:
1897 n = CHILD(n, 1);
1898 if (TYPE(n) == RSQB) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001899 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001900 "can't assign to []");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001901 return;
1902 }
1903 com_assign_list(c, n, assigning);
1904 return;
1905 case NAME:
1906 com_assign_name(c, CHILD(n, 0), assigning);
1907 return;
1908 default:
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 literal");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001911 return;
1912 }
1913 break;
Guido van Rossum15cc9a01996-08-08 18:51:04 +00001914
1915 case lambdef:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001916 com_error(c, PyExc_SyntaxError,
1917 "can't assign to lambda");
Guido van Rossum15cc9a01996-08-08 18:51:04 +00001918 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001919
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001920 default:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001921 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001922 com_error(c, PyExc_SystemError,
1923 "com_assign: bad node");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001924 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001925
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001926 }
1927 }
1928}
Guido van Rossum7c531111997-03-11 18:42:21 +00001929
Guido van Rossum79f25d91997-04-29 20:08:16 +00001930/* Forward */ static node *get_rawdocstring Py_PROTO((node *));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001931
1932static void
1933com_expr_stmt(c, n)
1934 struct compiling *c;
1935 node *n;
1936{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001937 REQ(n, expr_stmt); /* testlist ('=' testlist)* */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001938 /* Forget it if we have just a doc string here */
Guido van Rossum5f5e8171997-04-06 03:41:40 +00001939 if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
Guido van Rossum8b993a91997-01-17 21:04:03 +00001940 return;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001941 com_node(c, CHILD(n, NCH(n)-1));
1942 if (NCH(n) == 1) {
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001943 if (c->c_interactive)
1944 com_addbyte(c, PRINT_EXPR);
1945 else
1946 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001947 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001948 }
1949 else {
1950 int i;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001951 for (i = 0; i < NCH(n)-2; i+=2) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00001952 if (i+2 < NCH(n)-2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001953 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001954 com_push(c, 1);
1955 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001956 com_assign(c, CHILD(n, i), OP_ASSIGN);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001957 }
1958 }
1959}
1960
1961static void
Guido van Rossum228d7f31997-04-02 05:24:36 +00001962com_assert_stmt(c, n)
1963 struct compiling *c;
1964 node *n;
1965{
1966 int a = 0, b = 0;
1967 int i;
1968 REQ(n, assert_stmt); /* 'assert' test [',' test] */
1969 /* Generate code like for
1970
1971 if __debug__:
1972 if not <test>:
1973 raise AssertionError [, <message>]
1974
1975 where <message> is the second test, if present.
1976 */
1977 if (Py_OptimizeFlag)
1978 return;
1979 com_addopnamestr(c, LOAD_GLOBAL, "__debug__");
1980 com_push(c, 1);
1981 com_addfwref(c, JUMP_IF_FALSE, &a);
1982 com_addbyte(c, POP_TOP);
1983 com_pop(c, 1);
1984 com_node(c, CHILD(n, 1));
1985 com_addfwref(c, JUMP_IF_TRUE, &b);
1986 com_addbyte(c, POP_TOP);
1987 com_pop(c, 1);
1988 /* Raise that exception! */
1989 com_addopnamestr(c, LOAD_GLOBAL, "AssertionError");
1990 com_push(c, 1);
1991 i = NCH(n)/2; /* Either 2 or 4 */
1992 if (i > 1)
1993 com_node(c, CHILD(n, 3));
1994 com_addoparg(c, RAISE_VARARGS, i);
1995 com_pop(c, i);
1996 /* The interpreter does not fall through */
1997 /* All jumps converge here */
1998 com_backpatch(c, a);
1999 com_backpatch(c, b);
2000 com_addbyte(c, POP_TOP);
2001}
2002
2003static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002004com_print_stmt(c, n)
2005 struct compiling *c;
2006 node *n;
2007{
2008 int i;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002009 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
2010 for (i = 1; i < NCH(n); i += 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002011 com_node(c, CHILD(n, i));
2012 com_addbyte(c, PRINT_ITEM);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002013 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002014 }
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002015 if (TYPE(CHILD(n, NCH(n)-1)) != COMMA)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002016 com_addbyte(c, PRINT_NEWLINE);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002017 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002018}
2019
2020static void
2021com_return_stmt(c, n)
2022 struct compiling *c;
2023 node *n;
2024{
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002025 REQ(n, return_stmt); /* 'return' [testlist] */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002026 if (!c->c_infunction) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002027 com_error(c, PyExc_SyntaxError, "'return' outside function");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002028 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002029 if (NCH(n) < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002030 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002031 com_push(c, 1);
2032 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002033 else
2034 com_node(c, CHILD(n, 1));
2035 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002036 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002037}
2038
2039static void
2040com_raise_stmt(c, n)
2041 struct compiling *c;
2042 node *n;
2043{
Guido van Rossum8b993a91997-01-17 21:04:03 +00002044 int i;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002045 REQ(n, raise_stmt); /* 'raise' test [',' test [',' test]] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002046 com_node(c, CHILD(n, 1));
Guido van Rossum681d79a1995-07-18 14:51:37 +00002047 if (NCH(n) > 3) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002048 com_node(c, CHILD(n, 3));
Guido van Rossum681d79a1995-07-18 14:51:37 +00002049 if (NCH(n) > 5)
2050 com_node(c, CHILD(n, 5));
Guido van Rossumf10570b1995-07-07 22:53:21 +00002051 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002052 i = NCH(n)/2;
2053 com_addoparg(c, RAISE_VARARGS, i);
2054 com_pop(c, i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002055}
2056
2057static void
2058com_import_stmt(c, n)
2059 struct compiling *c;
2060 node *n;
2061{
2062 int i;
2063 REQ(n, import_stmt);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002064 /* 'import' dotted_name (',' dotted_name)* |
2065 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002066 if (STR(CHILD(n, 0))[0] == 'f') {
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002067 /* 'from' dotted_name 'import' ... */
2068 REQ(CHILD(n, 1), dotted_name);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002069 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002070 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002071 for (i = 3; i < NCH(n); i += 2)
2072 com_addopname(c, IMPORT_FROM, CHILD(n, i));
2073 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002074 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002075 }
2076 else {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002077 /* 'import' ... */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002078 for (i = 1; i < NCH(n); i += 2) {
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002079 REQ(CHILD(n, i), dotted_name);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002080 com_addopname(c, IMPORT_NAME, CHILD(n, i));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002081 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002082 com_addopname(c, STORE_NAME, CHILD(CHILD(n, i), 0));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002083 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002084 }
2085 }
2086}
2087
2088static void
Guido van Rossumc5e96291991-12-10 13:53:51 +00002089com_global_stmt(c, n)
2090 struct compiling *c;
2091 node *n;
2092{
2093 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00002094 REQ(n, global_stmt);
2095 /* 'global' NAME (',' NAME)* */
2096 for (i = 1; i < NCH(n); i += 2) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002097 char *s = STR(CHILD(n, i));
Guido van Rossumfe2236f1996-08-24 07:29:04 +00002098#ifdef PRIVATE_NAME_MANGLING
2099 char buffer[256];
2100 if (s != NULL && s[0] == '_' && s[1] == '_' &&
2101 c->c_private != NULL &&
2102 com_mangle(c, s, buffer, (int)sizeof(buffer)))
2103 s = buffer;
2104#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00002105 if (PyDict_GetItemString(c->c_locals, s) != NULL) {
2106 com_error(c, PyExc_SyntaxError,
2107 "name is local and global");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002108 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002109 else if (PyDict_SetItemString(c->c_globals, s, Py_None) != 0)
Guido van Rossumc5e96291991-12-10 13:53:51 +00002110 c->c_errors++;
2111 }
2112}
2113
Guido van Rossum681d79a1995-07-18 14:51:37 +00002114static int
2115com_newlocal_o(c, nameval)
2116 struct compiling *c;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002117 PyObject *nameval;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002118{
2119 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002120 PyObject *ival;
2121 if (PyList_Size(c->c_varnames) != c->c_nlocals) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002122 /* This is usually caused by an error on a previous call */
2123 if (c->c_errors == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002124 com_error(c, PyExc_SystemError,
2125 "mixed up var name/index");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002126 }
2127 return 0;
2128 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002129 ival = PyInt_FromLong(i = c->c_nlocals++);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002130 if (ival == NULL)
2131 c->c_errors++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002132 else if (PyDict_SetItem(c->c_locals, nameval, ival) != 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002133 c->c_errors++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002134 else if (PyList_Append(c->c_varnames, nameval) != 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002135 c->c_errors++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002136 Py_XDECREF(ival);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002137 return i;
2138}
2139
2140static int
2141com_addlocal_o(c, nameval)
2142 struct compiling *c;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002143 PyObject *nameval;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002144{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002145 PyObject *ival = PyDict_GetItem(c->c_locals, nameval);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002146 if (ival != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002147 return PyInt_AsLong(ival);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002148 return com_newlocal_o(c, nameval);
2149}
2150
2151static int
2152com_newlocal(c, name)
2153 struct compiling *c;
2154 char *name;
2155{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002156 PyObject *nameval = PyString_InternFromString(name);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002157 int i;
2158 if (nameval == NULL) {
2159 c->c_errors++;
2160 return 0;
2161 }
2162 i = com_newlocal_o(c, nameval);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002163 Py_DECREF(nameval);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002164 return i;
2165}
2166
Guido van Rossumc5e96291991-12-10 13:53:51 +00002167static void
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002168com_exec_stmt(c, n)
2169 struct compiling *c;
2170 node *n;
2171{
2172 REQ(n, exec_stmt);
2173 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
2174 com_node(c, CHILD(n, 1));
2175 if (NCH(n) >= 4)
2176 com_node(c, CHILD(n, 3));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002177 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002178 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002179 com_push(c, 1);
2180 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002181 if (NCH(n) >= 6)
2182 com_node(c, CHILD(n, 5));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002183 else {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002184 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002185 com_push(c, 1);
2186 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002187 com_addbyte(c, EXEC_STMT);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002188 com_pop(c, 3);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002189}
2190
Guido van Rossum7c531111997-03-11 18:42:21 +00002191static int
2192is_constant_false(c, n)
2193 struct compiling *c;
2194 node *n;
2195{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002196 PyObject *v;
Guido van Rossum7c531111997-03-11 18:42:21 +00002197 int i;
2198
2199 /* Label to avoid tail recursion */
2200 next:
2201 switch (TYPE(n)) {
2202
2203 case suite:
2204 if (NCH(n) == 1) {
2205 n = CHILD(n, 0);
2206 goto next;
2207 }
2208 /* Fall through */
2209 case file_input:
2210 for (i = 0; i < NCH(n); i++) {
2211 node *ch = CHILD(n, i);
2212 if (TYPE(ch) == stmt) {
2213 n = ch;
2214 goto next;
2215 }
2216 }
2217 break;
2218
2219 case stmt:
2220 case simple_stmt:
2221 case small_stmt:
2222 n = CHILD(n, 0);
2223 goto next;
2224
2225 case expr_stmt:
2226 case testlist:
2227 case test:
2228 case and_test:
2229 case not_test:
2230 case comparison:
2231 case expr:
2232 case xor_expr:
2233 case and_expr:
2234 case shift_expr:
2235 case arith_expr:
2236 case term:
2237 case factor:
2238 case power:
2239 case atom:
2240 if (NCH(n) == 1) {
2241 n = CHILD(n, 0);
2242 goto next;
2243 }
2244 break;
2245
2246 case NAME:
2247 if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
2248 return 1;
2249 break;
2250
2251 case NUMBER:
2252 v = parsenumber(c, STR(n));
2253 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002254 PyErr_Clear();
Guido van Rossum7c531111997-03-11 18:42:21 +00002255 break;
2256 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002257 i = PyObject_IsTrue(v);
2258 Py_DECREF(v);
Guido van Rossum7c531111997-03-11 18:42:21 +00002259 return i == 0;
2260
2261 case STRING:
Guido van Rossum6af0c001997-03-11 21:25:55 +00002262 v = parsestr(STR(n));
Guido van Rossum7c531111997-03-11 18:42:21 +00002263 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002264 PyErr_Clear();
Guido van Rossum7c531111997-03-11 18:42:21 +00002265 break;
2266 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002267 i = PyObject_IsTrue(v);
2268 Py_DECREF(v);
Guido van Rossum7c531111997-03-11 18:42:21 +00002269 return i == 0;
2270
2271 }
2272 return 0;
2273}
2274
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002275static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002276com_if_stmt(c, n)
2277 struct compiling *c;
2278 node *n;
2279{
2280 int i;
2281 int anchor = 0;
2282 REQ(n, if_stmt);
2283 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
2284 for (i = 0; i+3 < NCH(n); i+=4) {
2285 int a = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002286 node *ch = CHILD(n, i+1);
Guido van Rossum7c531111997-03-11 18:42:21 +00002287 if (is_constant_false(c, ch))
2288 continue;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002289 if (i > 0)
2290 com_addoparg(c, SET_LINENO, ch->n_lineno);
Guido van Rossum7c531111997-03-11 18:42:21 +00002291 com_node(c, ch);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002292 com_addfwref(c, JUMP_IF_FALSE, &a);
2293 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002294 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002295 com_node(c, CHILD(n, i+3));
2296 com_addfwref(c, JUMP_FORWARD, &anchor);
2297 com_backpatch(c, a);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002298 /* We jump here with an extra entry which we now pop */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002299 com_addbyte(c, POP_TOP);
2300 }
2301 if (i+2 < NCH(n))
2302 com_node(c, CHILD(n, i+2));
Guido van Rossum7c531111997-03-11 18:42:21 +00002303 if (anchor)
2304 com_backpatch(c, anchor);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002305}
2306
2307static void
2308com_while_stmt(c, n)
2309 struct compiling *c;
2310 node *n;
2311{
2312 int break_anchor = 0;
2313 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002314 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002315 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
2316 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002317 block_push(c, SETUP_LOOP);
2318 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002319 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002320 com_node(c, CHILD(n, 1));
2321 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2322 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002323 com_pop(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002324 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002325 com_node(c, CHILD(n, 3));
Guido van Rossum3f5da241990-12-20 15:06:42 +00002326 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002327 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2328 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002329 com_backpatch(c, anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002330 /* We jump here with one entry more on the stack */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002331 com_addbyte(c, POP_TOP);
2332 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002333 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002334 if (NCH(n) > 4)
2335 com_node(c, CHILD(n, 6));
2336 com_backpatch(c, break_anchor);
2337}
2338
2339static void
2340com_for_stmt(c, n)
2341 struct compiling *c;
2342 node *n;
2343{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002344 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002345 int break_anchor = 0;
2346 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002347 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002348 REQ(n, for_stmt);
2349 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
2350 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002351 block_push(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002352 com_node(c, CHILD(n, 3));
Guido van Rossum79f25d91997-04-29 20:08:16 +00002353 v = PyInt_FromLong(0L);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002354 if (v == NULL)
2355 c->c_errors++;
2356 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002357 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002358 Py_XDECREF(v);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002359 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002360 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002361 com_addfwref(c, FOR_LOOP, &anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002362 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00002363 com_assign(c, CHILD(n, 1), OP_ASSIGN);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002364 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002365 com_node(c, CHILD(n, 5));
Guido van Rossum3f5da241990-12-20 15:06:42 +00002366 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002367 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2368 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002369 com_backpatch(c, anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002370 com_pop(c, 2); /* FOR_LOOP has popped these */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002371 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002372 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002373 if (NCH(n) > 8)
2374 com_node(c, CHILD(n, 8));
2375 com_backpatch(c, break_anchor);
2376}
2377
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002378/* Code generated for "try: S finally: Sf" is as follows:
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002379
2380 SETUP_FINALLY L
2381 <code for S>
2382 POP_BLOCK
2383 LOAD_CONST <nil>
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002384 L: <code for Sf>
2385 END_FINALLY
2386
2387 The special instructions use the block stack. Each block
2388 stack entry contains the instruction that created it (here
2389 SETUP_FINALLY), the level of the value stack at the time the
2390 block stack entry was created, and a label (here L).
2391
2392 SETUP_FINALLY:
2393 Pushes the current value stack level and the label
2394 onto the block stack.
2395 POP_BLOCK:
2396 Pops en entry from the block stack, and pops the value
2397 stack until its level is the same as indicated on the
2398 block stack. (The label is ignored.)
2399 END_FINALLY:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002400 Pops a variable number of entries from the *value* stack
2401 and re-raises the exception they specify. The number of
2402 entries popped depends on the (pseudo) exception type.
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002403
2404 The block stack is unwound when an exception is raised:
2405 when a SETUP_FINALLY entry is found, the exception is pushed
2406 onto the value stack (and the exception condition is cleared),
2407 and the interpreter jumps to the label gotten from the block
2408 stack.
2409
2410 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
Guido van Rossum3f5da241990-12-20 15:06:42 +00002411 (The contents of the value stack is shown in [], with the top
2412 at the right; 'tb' is trace-back info, 'val' the exception's
2413 associated value, and 'exc' the exception.)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002414
2415 Value stack Label Instruction Argument
2416 [] SETUP_EXCEPT L1
2417 [] <code for S>
2418 [] POP_BLOCK
2419 [] JUMP_FORWARD L0
2420
Guido van Rossum3f5da241990-12-20 15:06:42 +00002421 [tb, val, exc] L1: DUP )
2422 [tb, val, exc, exc] <evaluate E1> )
2423 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2424 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2425 [tb, val, exc, 1] POP )
2426 [tb, val, exc] POP
2427 [tb, val] <assign to V1> (or POP if no V1)
2428 [tb] POP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002429 [] <code for S1>
2430 JUMP_FORWARD L0
2431
Guido van Rossum3f5da241990-12-20 15:06:42 +00002432 [tb, val, exc, 0] L2: POP
2433 [tb, val, exc] DUP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002434 .............................etc.......................
2435
Guido van Rossum3f5da241990-12-20 15:06:42 +00002436 [tb, val, exc, 0] Ln+1: POP
2437 [tb, val, exc] END_FINALLY # re-raise exception
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002438
2439 [] L0: <next statement>
2440
2441 Of course, parts are not generated if Vi or Ei is not present.
2442*/
2443
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002444static void
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002445com_try_except(c, n)
2446 struct compiling *c;
2447 node *n;
2448{
2449 int except_anchor = 0;
2450 int end_anchor = 0;
2451 int else_anchor = 0;
2452 int i;
2453 node *ch;
2454
2455 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
2456 block_push(c, SETUP_EXCEPT);
2457 com_node(c, CHILD(n, 2));
2458 com_addbyte(c, POP_BLOCK);
2459 block_pop(c, SETUP_EXCEPT);
2460 com_addfwref(c, JUMP_FORWARD, &else_anchor);
2461 com_backpatch(c, except_anchor);
2462 for (i = 3;
2463 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
2464 i += 3) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00002465 /* except_clause: 'except' [expr [',' var]] */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002466 if (except_anchor == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002467 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002468 "default 'except:' must be last");
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002469 break;
2470 }
2471 except_anchor = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00002472 com_push(c, 3); /* tb, val, exc pushed by exception */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002473 com_addoparg(c, SET_LINENO, ch->n_lineno);
2474 if (NCH(ch) > 1) {
2475 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002476 com_push(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002477 com_node(c, CHILD(ch, 1));
2478 com_addoparg(c, COMPARE_OP, EXC_MATCH);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002479 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002480 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
2481 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002482 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002483 }
2484 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002485 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002486 if (NCH(ch) > 3)
Guido van Rossum8861b741996-07-30 16:49:37 +00002487 com_assign(c, CHILD(ch, 3), OP_ASSIGN);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002488 else {
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002489 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002490 com_pop(c, 1);
2491 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002492 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002493 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002494 com_node(c, CHILD(n, i+2));
2495 com_addfwref(c, JUMP_FORWARD, &end_anchor);
2496 if (except_anchor) {
2497 com_backpatch(c, except_anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002498 /* We come in with [tb, val, exc, 0] on the
2499 stack; one pop and it's the same as
2500 expected at the start of the loop */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002501 com_addbyte(c, POP_TOP);
2502 }
2503 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002504 /* We actually come in here with [tb, val, exc] but the
2505 END_FINALLY will zap those and jump around.
2506 The c_stacklevel does not reflect them so we need not pop
2507 anything. */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002508 com_addbyte(c, END_FINALLY);
2509 com_backpatch(c, else_anchor);
2510 if (i < NCH(n))
2511 com_node(c, CHILD(n, i+2));
2512 com_backpatch(c, end_anchor);
2513}
2514
2515static void
2516com_try_finally(c, n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002517 struct compiling *c;
2518 node *n;
2519{
2520 int finally_anchor = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002521 node *ch;
Guido van Rossum94fb82e1992-04-05 14:24:50 +00002522
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002523 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
2524 block_push(c, SETUP_FINALLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002525 com_node(c, CHILD(n, 2));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002526 com_addbyte(c, POP_BLOCK);
2527 block_pop(c, SETUP_FINALLY);
2528 block_push(c, END_FINALLY);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002529 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002530 /* While the generated code pushes only one item,
2531 the try-finally handling can enter here with
2532 up to three items. OK, here are the details:
2533 3 for an exception, 2 for RETURN, 1 for BREAK. */
2534 com_push(c, 3);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002535 com_backpatch(c, finally_anchor);
2536 ch = CHILD(n, NCH(n)-1);
2537 com_addoparg(c, SET_LINENO, ch->n_lineno);
2538 com_node(c, ch);
2539 com_addbyte(c, END_FINALLY);
2540 block_pop(c, END_FINALLY);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002541 com_pop(c, 3); /* Matches the com_push above */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002542}
2543
2544static void
2545com_try_stmt(c, n)
2546 struct compiling *c;
2547 node *n;
2548{
2549 REQ(n, try_stmt);
2550 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
2551 | 'try' ':' suite 'finally' ':' suite */
2552 if (TYPE(CHILD(n, 3)) != except_clause)
2553 com_try_finally(c, n);
2554 else
2555 com_try_except(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002556}
2557
Guido van Rossum8b993a91997-01-17 21:04:03 +00002558static node *
2559get_rawdocstring(n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002560 node *n;
2561{
Guido van Rossum164d4ff1995-01-26 00:40:09 +00002562 int i;
2563
Guido van Rossum8b993a91997-01-17 21:04:03 +00002564 /* Label to avoid tail recursion */
2565 next:
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002566 switch (TYPE(n)) {
2567
2568 case suite:
Guido van Rossum8b993a91997-01-17 21:04:03 +00002569 if (NCH(n) == 1) {
2570 n = CHILD(n, 0);
2571 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002572 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002573 /* Fall through */
Guido van Rossum164d4ff1995-01-26 00:40:09 +00002574 case file_input:
2575 for (i = 0; i < NCH(n); i++) {
2576 node *ch = CHILD(n, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002577 if (TYPE(ch) == stmt) {
2578 n = ch;
2579 goto next;
2580 }
Guido van Rossum164d4ff1995-01-26 00:40:09 +00002581 }
2582 break;
2583
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002584 case stmt:
2585 case simple_stmt:
2586 case small_stmt:
Guido van Rossum8b993a91997-01-17 21:04:03 +00002587 n = CHILD(n, 0);
2588 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002589
2590 case expr_stmt:
2591 case testlist:
2592 case test:
2593 case and_test:
2594 case not_test:
2595 case comparison:
2596 case expr:
2597 case xor_expr:
2598 case and_expr:
2599 case shift_expr:
2600 case arith_expr:
2601 case term:
2602 case factor:
Guido van Rossum50564e81996-01-12 01:13:16 +00002603 case power:
Guido van Rossum8b993a91997-01-17 21:04:03 +00002604 if (NCH(n) == 1) {
2605 n = CHILD(n, 0);
2606 goto next;
2607 }
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002608 break;
2609
2610 case atom:
2611 if (TYPE(CHILD(n, 0)) == STRING)
Guido van Rossum8b993a91997-01-17 21:04:03 +00002612 return n;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002613 break;
2614
2615 }
2616 return NULL;
2617}
2618
Guido van Rossum79f25d91997-04-29 20:08:16 +00002619static PyObject *
Guido van Rossum8b993a91997-01-17 21:04:03 +00002620get_docstring(n)
2621 node *n;
2622{
2623 n = get_rawdocstring(n);
2624 if (n == NULL)
2625 return NULL;
2626 return parsestrplus(n);
2627}
2628
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002629static void
2630com_suite(c, n)
2631 struct compiling *c;
2632 node *n;
2633{
2634 REQ(n, suite);
2635 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
2636 if (NCH(n) == 1) {
2637 com_node(c, CHILD(n, 0));
2638 }
2639 else {
2640 int i;
2641 for (i = 0; i < NCH(n); i++) {
2642 node *ch = CHILD(n, i);
2643 if (TYPE(ch) == stmt)
2644 com_node(c, ch);
2645 }
2646 }
2647}
2648
Guido van Rossumf1aeab71992-03-27 17:28:26 +00002649/* ARGSUSED */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002650static void
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002651com_continue_stmt(c, n)
2652 struct compiling *c;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00002653 node *n; /* Not used, but passed for consistency */
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002654{
2655 int i = c->c_nblocks;
2656 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
2657 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2658 }
2659 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002660 com_error(c, PyExc_SyntaxError,
2661 "'continue' not properly in loop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002662 }
2663 /* XXX Could allow it inside a 'finally' clause
2664 XXX if we could pop the exception still on the stack */
2665}
2666
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002667static int
Guido van Rossum681d79a1995-07-18 14:51:37 +00002668com_argdefs(c, n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002669 struct compiling *c;
2670 node *n;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002671{
Guido van Rossum681d79a1995-07-18 14:51:37 +00002672 int i, nch, nargs, ndefs;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002673 if (TYPE(n) == lambdef) {
2674 /* lambdef: 'lambda' [varargslist] ':' test */
2675 n = CHILD(n, 1);
2676 }
2677 else {
2678 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
2679 n = CHILD(n, 2);
2680 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
2681 n = CHILD(n, 1);
2682 }
2683 if (TYPE(n) != varargslist)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002684 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002685 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00002686 (fpdef ['=' test] ',')* '*' ....... |
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002687 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
2688 nch = NCH(n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002689 nargs = 0;
2690 ndefs = 0;
2691 for (i = 0; i < nch; i++) {
2692 int t;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002693 if (TYPE(CHILD(n, i)) == STAR ||
2694 TYPE(CHILD(n, i)) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00002695 break;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002696 nargs++;
2697 i++;
2698 if (i >= nch)
Guido van Rossuma1e7e621995-09-18 21:44:04 +00002699 t = RPAR; /* Anything except EQUAL or COMMA */
2700 else
2701 t = TYPE(CHILD(n, i));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002702 if (t == EQUAL) {
2703 i++;
2704 ndefs++;
2705 com_node(c, CHILD(n, i));
2706 i++;
2707 if (i >= nch)
2708 break;
2709 t = TYPE(CHILD(n, i));
2710 }
2711 else {
2712 /* Treat "(a=1, b)" as "(a=1, b=None)" */
2713 if (ndefs) {
2714 com_addoparg(c, LOAD_CONST,
Guido van Rossum79f25d91997-04-29 20:08:16 +00002715 com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002716 com_push(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002717 ndefs++;
2718 }
2719 }
2720 if (t != COMMA)
2721 break;
2722 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002723 return ndefs;
2724}
2725
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002726static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002727com_funcdef(c, n)
2728 struct compiling *c;
2729 node *n;
2730{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002731 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002732 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002733 v = (PyObject *)icompile(n, c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002734 if (v == NULL)
2735 c->c_errors++;
2736 else {
2737 int i = com_addconst(c, v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002738 int ndefs = com_argdefs(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002739 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002740 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002741 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002742 com_pop(c, ndefs);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002743 com_addopname(c, STORE_NAME, CHILD(n, 1));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002744 com_pop(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002745 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002746 }
2747}
2748
2749static void
Guido van Rossum25831651993-05-19 14:50:45 +00002750com_bases(c, n)
Guido van Rossumc5e96291991-12-10 13:53:51 +00002751 struct compiling *c;
2752 node *n;
2753{
Guido van Rossumf1aeab71992-03-27 17:28:26 +00002754 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00002755 REQ(n, testlist);
2756 /* testlist: test (',' test)* [','] */
2757 for (i = 0; i < NCH(n); i += 2)
2758 com_node(c, CHILD(n, i));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002759 i = (NCH(n)+1) / 2;
2760 com_addoparg(c, BUILD_TUPLE, i);
2761 com_pop(c, i-1);
Guido van Rossumc5e96291991-12-10 13:53:51 +00002762}
2763
2764static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002765com_classdef(c, n)
2766 struct compiling *c;
2767 node *n;
2768{
Guido van Rossum25831651993-05-19 14:50:45 +00002769 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002770 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002771 REQ(n, classdef);
Guido van Rossum25831651993-05-19 14:50:45 +00002772 /* classdef: class NAME ['(' testlist ')'] ':' suite */
Guido van Rossum7b89b6a1997-01-18 08:02:57 +00002773 if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
Guido van Rossum25831651993-05-19 14:50:45 +00002774 c->c_errors++;
2775 return;
2776 }
2777 /* Push the class name on the stack */
2778 i = com_addconst(c, v);
2779 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002780 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002781 Py_DECREF(v);
Guido van Rossum25831651993-05-19 14:50:45 +00002782 /* Push the tuple of base classes on the stack */
Guido van Rossum8b993a91997-01-17 21:04:03 +00002783 if (TYPE(CHILD(n, 2)) != LPAR) {
Guido van Rossumc5e96291991-12-10 13:53:51 +00002784 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002785 com_push(c, 1);
2786 }
Guido van Rossum25831651993-05-19 14:50:45 +00002787 else
2788 com_bases(c, CHILD(n, 3));
Guido van Rossum79f25d91997-04-29 20:08:16 +00002789 v = (PyObject *)icompile(n, c);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002790 if (v == NULL)
2791 c->c_errors++;
2792 else {
Guido van Rossum25831651993-05-19 14:50:45 +00002793 i = com_addconst(c, v);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002794 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002795 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002796 com_addoparg(c, MAKE_FUNCTION, 0);
2797 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002798 com_addbyte(c, BUILD_CLASS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002799 com_pop(c, 2);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002800 com_addopname(c, STORE_NAME, CHILD(n, 1));
Guido van Rossum79f25d91997-04-29 20:08:16 +00002801 Py_DECREF(v);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002802 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002803}
2804
2805static void
2806com_node(c, n)
2807 struct compiling *c;
2808 node *n;
2809{
2810 switch (TYPE(n)) {
2811
2812 /* Definition nodes */
2813
2814 case funcdef:
2815 com_funcdef(c, n);
2816 break;
2817 case classdef:
2818 com_classdef(c, n);
2819 break;
2820
2821 /* Trivial parse tree nodes */
2822
2823 case stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002824 case small_stmt:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002825 case flow_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002826 com_node(c, CHILD(n, 0));
2827 break;
2828
2829 case simple_stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002830 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
2831 com_addoparg(c, SET_LINENO, n->n_lineno);
2832 {
2833 int i;
2834 for (i = 0; i < NCH(n)-1; i += 2)
2835 com_node(c, CHILD(n, i));
2836 }
2837 break;
2838
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002839 case compound_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002840 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002841 com_node(c, CHILD(n, 0));
2842 break;
2843
2844 /* Statement nodes */
2845
2846 case expr_stmt:
2847 com_expr_stmt(c, n);
2848 break;
2849 case print_stmt:
2850 com_print_stmt(c, n);
2851 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002852 case del_stmt: /* 'del' exprlist */
Guido van Rossum8861b741996-07-30 16:49:37 +00002853 com_assign(c, CHILD(n, 1), OP_DELETE);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002854 break;
2855 case pass_stmt:
2856 break;
2857 case break_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002858 if (c->c_loops == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002859 com_error(c, PyExc_SyntaxError,
2860 "'break' outside loop");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002861 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002862 com_addbyte(c, BREAK_LOOP);
2863 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002864 case continue_stmt:
2865 com_continue_stmt(c, n);
2866 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002867 case return_stmt:
2868 com_return_stmt(c, n);
2869 break;
2870 case raise_stmt:
2871 com_raise_stmt(c, n);
2872 break;
2873 case import_stmt:
2874 com_import_stmt(c, n);
2875 break;
Guido van Rossumc5e96291991-12-10 13:53:51 +00002876 case global_stmt:
2877 com_global_stmt(c, n);
2878 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002879 case exec_stmt:
2880 com_exec_stmt(c, n);
2881 break;
Guido van Rossum228d7f31997-04-02 05:24:36 +00002882 case assert_stmt:
2883 com_assert_stmt(c, n);
2884 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002885 case if_stmt:
2886 com_if_stmt(c, n);
2887 break;
2888 case while_stmt:
2889 com_while_stmt(c, n);
2890 break;
2891 case for_stmt:
2892 com_for_stmt(c, n);
2893 break;
2894 case try_stmt:
2895 com_try_stmt(c, n);
2896 break;
2897 case suite:
2898 com_suite(c, n);
2899 break;
2900
2901 /* Expression nodes */
2902
2903 case testlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00002904 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002905 break;
2906 case test:
2907 com_test(c, n);
2908 break;
2909 case and_test:
2910 com_and_test(c, n);
2911 break;
2912 case not_test:
2913 com_not_test(c, n);
2914 break;
2915 case comparison:
2916 com_comparison(c, n);
2917 break;
2918 case exprlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00002919 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002920 break;
2921 case expr:
2922 com_expr(c, n);
2923 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +00002924 case xor_expr:
2925 com_xor_expr(c, n);
2926 break;
2927 case and_expr:
2928 com_and_expr(c, n);
2929 break;
2930 case shift_expr:
2931 com_shift_expr(c, n);
2932 break;
2933 case arith_expr:
2934 com_arith_expr(c, n);
2935 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002936 case term:
2937 com_term(c, n);
2938 break;
2939 case factor:
2940 com_factor(c, n);
2941 break;
Guido van Rossum50564e81996-01-12 01:13:16 +00002942 case power:
2943 com_power(c, n);
2944 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002945 case atom:
2946 com_atom(c, n);
2947 break;
2948
2949 default:
Guido van Rossum8b993a91997-01-17 21:04:03 +00002950 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002951 com_error(c, PyExc_SystemError,
2952 "com_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002953 }
2954}
2955
Guido van Rossum79f25d91997-04-29 20:08:16 +00002956static void com_fplist Py_PROTO((struct compiling *, node *));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002957
2958static void
2959com_fpdef(c, n)
2960 struct compiling *c;
2961 node *n;
2962{
2963 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
2964 if (TYPE(CHILD(n, 0)) == LPAR)
2965 com_fplist(c, CHILD(n, 1));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002966 else {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002967 com_addoparg(c, STORE_FAST, com_newlocal(c, STR(CHILD(n, 0))));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002968 com_pop(c, 1);
2969 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002970}
2971
2972static void
2973com_fplist(c, n)
2974 struct compiling *c;
2975 node *n;
2976{
Guido van Rossuma9df32a1991-12-31 13:13:35 +00002977 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002978 if (NCH(n) == 1) {
2979 com_fpdef(c, CHILD(n, 0));
2980 }
2981 else {
Guido van Rossum8b993a91997-01-17 21:04:03 +00002982 int i = (NCH(n)+1)/2;
2983 com_addoparg(c, UNPACK_TUPLE, i);
2984 com_push(c, i-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002985 for (i = 0; i < NCH(n); i += 2)
2986 com_fpdef(c, CHILD(n, i));
2987 }
2988}
2989
2990static void
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002991com_arglist(c, n)
2992 struct compiling *c;
2993 node *n;
2994{
Guido van Rossum681d79a1995-07-18 14:51:37 +00002995 int nch, i;
2996 int complex = 0;
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002997 REQ(n, varargslist);
Guido van Rossumacbefef1992-01-19 16:33:51 +00002998 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00002999 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003000 nch = NCH(n);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003001 /* Enter all arguments in table of locals */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003002 for (i = 0; i < nch; i++) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00003003 node *ch = CHILD(n, i);
3004 node *fp;
3005 char *name;
Guido van Rossum50564e81996-01-12 01:13:16 +00003006 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00003007 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003008 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3009 fp = CHILD(ch, 0);
3010 if (TYPE(fp) == NAME)
3011 name = STR(fp);
3012 else {
3013 name = "";
Guido van Rossum530956d1996-07-21 02:27:43 +00003014 complex = 1;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003015 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003016 com_newlocal(c, name);
3017 c->c_argcount++;
3018 if (++i >= nch)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003019 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003020 ch = CHILD(n, i);
3021 if (TYPE(ch) == EQUAL)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003022 i += 2;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003023 else
3024 REQ(ch, COMMA);
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003025 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003026 /* Handle *arguments */
3027 if (i < nch) {
3028 node *ch;
3029 ch = CHILD(n, i);
Guido van Rossum50564e81996-01-12 01:13:16 +00003030 if (TYPE(ch) != DOUBLESTAR) {
3031 REQ(ch, STAR);
3032 ch = CHILD(n, i+1);
3033 if (TYPE(ch) == NAME) {
3034 c->c_flags |= CO_VARARGS;
3035 i += 3;
3036 com_newlocal(c, STR(ch));
3037 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003038 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003039 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003040 /* Handle **keywords */
3041 if (i < nch) {
3042 node *ch;
3043 ch = CHILD(n, i);
Guido van Rossum50564e81996-01-12 01:13:16 +00003044 if (TYPE(ch) != DOUBLESTAR) {
3045 REQ(ch, STAR);
3046 ch = CHILD(n, i+1);
3047 REQ(ch, STAR);
3048 ch = CHILD(n, i+2);
3049 }
3050 else
3051 ch = CHILD(n, i+1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003052 REQ(ch, NAME);
3053 c->c_flags |= CO_VARKEYWORDS;
3054 com_newlocal(c, STR(ch));
3055 }
3056 if (complex) {
3057 /* Generate code for complex arguments only after
3058 having counted the simple arguments */
3059 int ilocal = 0;
3060 for (i = 0; i < nch; i++) {
3061 node *ch = CHILD(n, i);
3062 node *fp;
Guido van Rossum50564e81996-01-12 01:13:16 +00003063 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003064 break;
3065 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3066 fp = CHILD(ch, 0);
3067 if (TYPE(fp) != NAME) {
3068 com_addoparg(c, LOAD_FAST, ilocal);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003069 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003070 com_fpdef(c, ch);
3071 }
3072 ilocal++;
3073 if (++i >= nch)
3074 break;
3075 ch = CHILD(n, i);
3076 if (TYPE(ch) == EQUAL)
3077 i += 2;
3078 else
3079 REQ(ch, COMMA);
3080 }
3081 }
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003082}
3083
3084static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003085com_file_input(c, n)
3086 struct compiling *c;
3087 node *n;
3088{
3089 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003090 PyObject *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003091 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003092 doc = get_docstring(n);
3093 if (doc != NULL) {
3094 int i = com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003095 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003096 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003097 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003098 com_addopnamestr(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00003099 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003100 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003101 for (i = 0; i < NCH(n); i++) {
3102 node *ch = CHILD(n, i);
3103 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
3104 com_node(c, ch);
3105 }
3106}
3107
3108/* Top-level compile-node interface */
3109
3110static void
3111compile_funcdef(c, n)
3112 struct compiling *c;
3113 node *n;
3114{
Guido van Rossum79f25d91997-04-29 20:08:16 +00003115 PyObject *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003116 node *ch;
3117 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum9bfef441993-03-29 10:43:31 +00003118 c->c_name = STR(CHILD(n, 1));
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003119 doc = get_docstring(CHILD(n, 4));
3120 if (doc != NULL) {
3121 (void) com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003122 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003123 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003124 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00003125 (void) com_addconst(c, Py_None); /* No docstring */
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003126 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
3127 ch = CHILD(ch, 1); /* ')' | varargslist */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003128 if (TYPE(ch) == varargslist)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003129 com_arglist(c, ch);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003130 c->c_infunction = 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003131 com_node(c, CHILD(n, 4));
Guido van Rossum3f5da241990-12-20 15:06:42 +00003132 c->c_infunction = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003133 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003134 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003135 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003136 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003137}
3138
3139static void
Guido van Rossum590baa41993-11-30 13:40:46 +00003140compile_lambdef(c, n)
Guido van Rossum12d12c51993-10-26 17:58:25 +00003141 struct compiling *c;
3142 node *n;
3143{
Guido van Rossum590baa41993-11-30 13:40:46 +00003144 node *ch;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003145 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
Guido van Rossum363ac7d1994-11-10 22:40:34 +00003146 c->c_name = "<lambda>";
Guido van Rossum590baa41993-11-30 13:40:46 +00003147
3148 ch = CHILD(n, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003149 (void) com_addconst(c, Py_None); /* No docstring */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003150 if (TYPE(ch) == varargslist) {
Guido van Rossum590baa41993-11-30 13:40:46 +00003151 com_arglist(c, ch);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003152 ch = CHILD(n, 3);
Guido van Rossum590baa41993-11-30 13:40:46 +00003153 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003154 else
3155 ch = CHILD(n, 2);
3156 com_node(c, ch);
Guido van Rossum12d12c51993-10-26 17:58:25 +00003157 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003158 com_pop(c, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00003159}
3160
3161static void
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003162compile_classdef(c, n)
3163 struct compiling *c;
3164 node *n;
3165{
3166 node *ch;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003167 PyObject *doc;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003168 REQ(n, classdef);
3169 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3170 c->c_name = STR(CHILD(n, 1));
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003171#ifdef PRIVATE_NAME_MANGLING
3172 c->c_private = c->c_name;
3173#endif
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003174 ch = CHILD(n, NCH(n)-1); /* The suite */
3175 doc = get_docstring(ch);
3176 if (doc != NULL) {
3177 int i = com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003178 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003179 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003180 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003181 com_addopnamestr(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00003182 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003183 }
3184 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00003185 (void) com_addconst(c, Py_None);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003186 com_node(c, ch);
3187 com_addbyte(c, LOAD_LOCALS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003188 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003189 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003190 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003191}
3192
3193static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003194compile_node(c, n)
3195 struct compiling *c;
3196 node *n;
3197{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003198 com_addoparg(c, SET_LINENO, n->n_lineno);
3199
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003200 switch (TYPE(n)) {
3201
Guido van Rossum4c417781991-01-21 16:09:22 +00003202 case single_input: /* One interactive command */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003203 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003204 c->c_interactive++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003205 n = CHILD(n, 0);
3206 if (TYPE(n) != NEWLINE)
3207 com_node(c, n);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003208 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003209 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003210 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003211 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003212 c->c_interactive--;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003213 break;
3214
Guido van Rossum4c417781991-01-21 16:09:22 +00003215 case file_input: /* A whole file, or built-in function exec() */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003216 com_file_input(c, n);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003217 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003218 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003219 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003220 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003221 break;
3222
Guido van Rossum590baa41993-11-30 13:40:46 +00003223 case eval_input: /* Built-in function input() */
Guido van Rossum4c417781991-01-21 16:09:22 +00003224 com_node(c, CHILD(n, 0));
3225 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003226 com_pop(c, 1);
Guido van Rossum4c417781991-01-21 16:09:22 +00003227 break;
3228
Guido van Rossum590baa41993-11-30 13:40:46 +00003229 case lambdef: /* anonymous function definition */
3230 compile_lambdef(c, n);
3231 break;
3232
Guido van Rossum4c417781991-01-21 16:09:22 +00003233 case funcdef: /* A function definition */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003234 compile_funcdef(c, n);
3235 break;
3236
Guido van Rossum4c417781991-01-21 16:09:22 +00003237 case classdef: /* A class definition */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003238 compile_classdef(c, n);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003239 break;
3240
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003241 default:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003242 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
Guido van Rossum79f25d91997-04-29 20:08:16 +00003243 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00003244 "compile_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003245 }
3246}
3247
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003248/* Optimization for local variables in functions (and *only* functions).
Guido van Rossum282914b1991-04-04 10:42:56 +00003249
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003250 This replaces all LOAD_NAME, STORE_NAME and DELETE_NAME
3251 instructions that refer to local variables with LOAD_FAST etc.
3252 The latter instructions are much faster because they don't need to
3253 look up the variable name in a dictionary.
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003254
Guido van Rossum681d79a1995-07-18 14:51:37 +00003255 To find all local variables, we check all STORE_NAME, IMPORT_FROM
3256 and DELETE_NAME instructions. This yields all local variables,
3257 function definitions, class definitions and import statements.
3258 Argument names have already been entered into the list by the
3259 special processing for the argument list.
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003260
3261 All remaining LOAD_NAME instructions must refer to non-local (global
3262 or builtin) variables, so are replaced by LOAD_GLOBAL.
3263
3264 There are two problems: 'from foo import *' and 'exec' may introduce
3265 local variables that we can't know while compiling. If this is the
Guido van Rossum681d79a1995-07-18 14:51:37 +00003266 case, we can still optimize bona fide locals (since those
3267 statements will be surrounded by fast_2_locals() and
3268 locals_2_fast()), but we can't change LOAD_NAME to LOAD_GLOBAL.
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003269
Guido van Rossum681d79a1995-07-18 14:51:37 +00003270 NB: this modifies the string object c->c_code! */
Guido van Rossum282914b1991-04-04 10:42:56 +00003271
3272static void
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003273optimize(c)
3274 struct compiling *c;
Guido van Rossum282914b1991-04-04 10:42:56 +00003275{
Guido van Rossum0a697f61991-04-16 08:39:12 +00003276 unsigned char *next_instr, *cur_instr;
Guido van Rossum282914b1991-04-04 10:42:56 +00003277 int opcode;
Guido van Rossum80bb9651996-12-05 23:27:02 +00003278 int oparg = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003279 PyObject *name;
3280 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003281
Guido van Rossum282914b1991-04-04 10:42:56 +00003282#define NEXTOP() (*next_instr++)
3283#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Guido van Rossum79f25d91997-04-29 20:08:16 +00003284#define GETITEM(v, i) (PyList_GetItem((v), (i)))
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003285#define GETNAMEOBJ(i) (GETITEM(c->c_names, (i)))
3286
Guido van Rossum79f25d91997-04-29 20:08:16 +00003287 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003288
3289 c->c_flags |= CO_OPTIMIZED;
Guido van Rossum282914b1991-04-04 10:42:56 +00003290
Guido van Rossum79f25d91997-04-29 20:08:16 +00003291 next_instr = (unsigned char *) PyString_AsString(c->c_code);
Guido van Rossum282914b1991-04-04 10:42:56 +00003292 for (;;) {
3293 opcode = NEXTOP();
3294 if (opcode == STOP_CODE)
3295 break;
3296 if (HAS_ARG(opcode))
3297 oparg = NEXTARG();
Guido van Rossum681d79a1995-07-18 14:51:37 +00003298 switch (opcode) {
3299 case STORE_NAME:
3300 case DELETE_NAME:
3301 case IMPORT_FROM:
3302 com_addlocal_o(c, GETNAMEOBJ(oparg));
3303 break;
3304 case EXEC_STMT:
3305 c->c_flags &= ~CO_OPTIMIZED;
3306 break;
Guido van Rossum282914b1991-04-04 10:42:56 +00003307 }
3308 }
3309
Guido van Rossum79f25d91997-04-29 20:08:16 +00003310 if (PyDict_GetItemString(c->c_locals, "*") != NULL)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003311 c->c_flags &= ~CO_OPTIMIZED;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003312
Guido van Rossum79f25d91997-04-29 20:08:16 +00003313 next_instr = (unsigned char *) PyString_AsString(c->c_code);
Guido van Rossum282914b1991-04-04 10:42:56 +00003314 for (;;) {
3315 cur_instr = next_instr;
3316 opcode = NEXTOP();
3317 if (opcode == STOP_CODE)
3318 break;
3319 if (HAS_ARG(opcode))
3320 oparg = NEXTARG();
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003321 if (opcode == LOAD_NAME ||
3322 opcode == STORE_NAME ||
3323 opcode == DELETE_NAME) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003324 PyObject *v;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003325 int i;
Guido van Rossum282914b1991-04-04 10:42:56 +00003326 name = GETNAMEOBJ(oparg);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003327 v = PyDict_GetItem(c->c_locals, name);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003328 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003329 PyErr_Clear();
Guido van Rossum681d79a1995-07-18 14:51:37 +00003330 if (opcode == LOAD_NAME &&
3331 (c->c_flags&CO_OPTIMIZED))
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003332 cur_instr[0] = LOAD_GLOBAL;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003333 continue;
Guido van Rossum83163251991-08-16 08:58:43 +00003334 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00003335 i = PyInt_AsLong(v);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003336 switch (opcode) {
3337 case LOAD_NAME: cur_instr[0] = LOAD_FAST; break;
3338 case STORE_NAME: cur_instr[0] = STORE_FAST; break;
3339 case DELETE_NAME: cur_instr[0] = DELETE_FAST; break;
3340 }
3341 cur_instr[1] = i & 0xff;
3342 cur_instr[2] = (i>>8) & 0xff;
Guido van Rossum282914b1991-04-04 10:42:56 +00003343 }
3344 }
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003345
Guido van Rossum681d79a1995-07-18 14:51:37 +00003346 if (c->c_errors == 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +00003347 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum282914b1991-04-04 10:42:56 +00003348}
3349
Guido van Rossum79f25d91997-04-29 20:08:16 +00003350PyCodeObject *
3351PyNode_Compile(n, filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003352 node *n;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003353 char *filename;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003354{
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003355 return jcompile(n, filename, NULL);
3356}
3357
Guido van Rossum79f25d91997-04-29 20:08:16 +00003358static PyCodeObject *
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003359icompile(n, base)
3360 node *n;
3361 struct compiling *base;
3362{
3363 return jcompile(n, base->c_filename, base);
3364}
3365
Guido van Rossum79f25d91997-04-29 20:08:16 +00003366static PyCodeObject *
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003367jcompile(n, filename, base)
3368 node *n;
3369 char *filename;
3370 struct compiling *base;
3371{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003372 struct compiling sc;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003373 PyCodeObject *co;
Guido van Rossum590baa41993-11-30 13:40:46 +00003374 if (!com_init(&sc, filename))
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003375 return NULL;
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003376#ifdef PRIVATE_NAME_MANGLING
3377 if (base)
3378 sc.c_private = base->c_private;
3379 else
3380 sc.c_private = NULL;
3381#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003382 compile_node(&sc, n);
3383 com_done(&sc);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003384 if ((TYPE(n) == funcdef || TYPE(n) == lambdef) && sc.c_errors == 0) {
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003385 optimize(&sc);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003386 sc.c_flags |= CO_NEWLOCALS;
3387 }
3388 else if (TYPE(n) == classdef)
3389 sc.c_flags |= CO_NEWLOCALS;
Guido van Rossum9bfef441993-03-29 10:43:31 +00003390 co = NULL;
3391 if (sc.c_errors == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003392 PyObject *consts, *names, *varnames, *filename, *name;
3393 consts = PyList_AsTuple(sc.c_consts);
3394 names = PyList_AsTuple(sc.c_names);
3395 varnames = PyList_AsTuple(sc.c_varnames);
Guido van Rossum7b89b6a1997-01-18 08:02:57 +00003396 filename = PyString_InternFromString(sc.c_filename);
3397 name = PyString_InternFromString(sc.c_name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003398 if (!PyErr_Occurred())
3399 co = PyCode_New(sc.c_argcount,
Guido van Rossum681d79a1995-07-18 14:51:37 +00003400 sc.c_nlocals,
Guido van Rossum8b993a91997-01-17 21:04:03 +00003401 sc.c_maxstacklevel,
Guido van Rossum681d79a1995-07-18 14:51:37 +00003402 sc.c_flags,
3403 sc.c_code,
3404 consts,
3405 names,
3406 varnames,
3407 filename,
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00003408 name,
3409 sc.c_firstlineno,
3410 sc.c_lnotab);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003411 Py_XDECREF(consts);
3412 Py_XDECREF(names);
3413 Py_XDECREF(varnames);
3414 Py_XDECREF(filename);
3415 Py_XDECREF(name);
Guido van Rossuma082ce41991-06-04 19:41:56 +00003416 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003417 com_free(&sc);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003418 return co;
3419}
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00003420
3421int
3422PyCode_Addr2Line(co, addrq)
3423 PyCodeObject *co;
3424 int addrq;
3425{
3426 int size = PyString_Size(co->co_lnotab) / 2;
3427 char *p = PyString_AsString(co->co_lnotab);
3428 int line = co->co_firstlineno;
3429 int addr = 0;
3430 while (--size >= 0) {
3431 addr += *p++;
3432 if (addr > addrq)
3433 break;
3434 line += *p++;
3435 }
3436 return line;
3437}