blob: 2f98067ac899839209c8b62d0c9b1c6b2754f47d [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002/* Compile an expression node to intermediate code */
3
Guido van Rossum3f5da241990-12-20 15:06:42 +00004/* XXX TO DO:
Guido van Rossum8b993a91997-01-17 21:04:03 +00005 XXX add __doc__ attribute == co_doc to code object attributes?
6 XXX (it's currently the first item of the co_const tuple)
Guido van Rossum3f5da241990-12-20 15:06:42 +00007 XXX Generate simple jump for break/return outside 'try...finally'
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00008 XXX Allow 'continue' inside finally clause of try-finally
Guido van Rossum8b993a91997-01-17 21:04:03 +00009 XXX New opcode for loading the initial index for a for loop
Guido van Rossum681d79a1995-07-18 14:51:37 +000010 XXX other JAR tricks?
Guido van Rossum3f5da241990-12-20 15:06:42 +000011*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +000012
Guido van Rossum79f25d91997-04-29 20:08:16 +000013#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000014
Guido van Rossum10dc2e81990-11-18 17:27:39 +000015#include "node.h"
16#include "token.h"
17#include "graminit.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000018#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000019#include "symtable.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000020#include "opcode.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000021#include "structmember.h"
22
23#include <ctype.h>
Guido van Rossum282914b1991-04-04 10:42:56 +000024
Guido van Rossumb05a5c71997-05-07 17:46:13 +000025/* Three symbols from graminit.h are also defined in Python.h, with
26 Py_ prefixes to their names. Python.h can't include graminit.h
27 (which defines too many confusing symbols), but we can check here
28 that they haven't changed (which is very unlikely, but possible). */
29#if Py_single_input != single_input
Guido van Rossum2f75b291997-05-20 22:18:48 +000030 #error "single_input has changed -- update Py_single_input in Python.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000031#endif
32#if Py_file_input != file_input
Guido van Rossum2f75b291997-05-20 22:18:48 +000033 #error "file_input has changed -- update Py_file_input in Python.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000034#endif
35#if Py_eval_input != eval_input
Guido van Rossum2f75b291997-05-20 22:18:48 +000036 #error "eval_input has changed -- update Py_eval_input in Python.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000037#endif
38
Guido van Rossum8e793d91997-03-03 19:13:14 +000039int Py_OptimizeFlag = 0;
40
Guido van Rossum8861b741996-07-30 16:49:37 +000041#define OP_DELETE 0
42#define OP_ASSIGN 1
43#define OP_APPLY 2
44
Jeremy Hyltone36f7782001-01-19 03:21:30 +000045#define VAR_LOAD 0
46#define VAR_STORE 1
47#define VAR_DELETE 2
48
Jeremy Hylton64949cb2001-01-25 20:06:59 +000049#define DEL_CLOSURE_ERROR \
Jeremy Hyltoneab156f2001-01-30 01:24:43 +000050"can not delete variable '%.400s' referenced in nested scope"
51
52#define DUPLICATE_ARGUMENT \
53"duplicate argument '%s' in function definition"
54
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000055#define ILLEGAL_DYNAMIC_SCOPE \
56"%.100s: exec or 'import *' makes names ambiguous in nested scope"
Jeremy Hyltoneab156f2001-01-30 01:24:43 +000057
Jeremy Hyltone36f7782001-01-19 03:21:30 +000058#define MANGLE_LEN 256
59
Guido van Rossum79f25d91997-04-29 20:08:16 +000060#define OFF(x) offsetof(PyCodeObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +000061
62static struct memberlist code_memberlist[] = {
Guido van Rossum681d79a1995-07-18 14:51:37 +000063 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
64 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
Guido van Rossum8b993a91997-01-17 21:04:03 +000065 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
Guido van Rossum681d79a1995-07-18 14:51:37 +000066 {"co_flags", T_INT, OFF(co_flags), READONLY},
Guido van Rossum39d942d1992-01-12 02:30:05 +000067 {"co_code", T_OBJECT, OFF(co_code), READONLY},
68 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
69 {"co_names", T_OBJECT, OFF(co_names), READONLY},
Guido van Rossum681d79a1995-07-18 14:51:37 +000070 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
Jeremy Hylton64949cb2001-01-25 20:06:59 +000071 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
72 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
Guido van Rossum39d942d1992-01-12 02:30:05 +000073 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
Guido van Rossum9bfef441993-03-29 10:43:31 +000074 {"co_name", T_OBJECT, OFF(co_name), READONLY},
Guido van Rossumda4eb5c1997-01-24 03:43:35 +000075 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
76 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
Guido van Rossum3f5da241990-12-20 15:06:42 +000077 {NULL} /* Sentinel */
78};
79
Guido van Rossum79f25d91997-04-29 20:08:16 +000080static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000081code_getattr(PyCodeObject *co, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +000082{
Guido van Rossum79f25d91997-04-29 20:08:16 +000083 return PyMember_Get((char *)co, code_memberlist, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +000084}
Guido van Rossum10dc2e81990-11-18 17:27:39 +000085
86static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000087code_dealloc(PyCodeObject *co)
Guido van Rossum10dc2e81990-11-18 17:27:39 +000088{
Guido van Rossum79f25d91997-04-29 20:08:16 +000089 Py_XDECREF(co->co_code);
90 Py_XDECREF(co->co_consts);
91 Py_XDECREF(co->co_names);
Guido van Rossum275558c1997-07-25 20:13:49 +000092 Py_XDECREF(co->co_varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +000093 Py_XDECREF(co->co_freevars);
94 Py_XDECREF(co->co_cellvars);
Guido van Rossum79f25d91997-04-29 20:08:16 +000095 Py_XDECREF(co->co_filename);
96 Py_XDECREF(co->co_name);
Guido van Rossum275558c1997-07-25 20:13:49 +000097 Py_XDECREF(co->co_lnotab);
Guido van Rossumb18618d2000-05-03 23:44:39 +000098 PyObject_DEL(co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +000099}
100
Guido van Rossum79f25d91997-04-29 20:08:16 +0000101static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000102code_repr(PyCodeObject *co)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000103{
104 char buf[500];
105 int lineno = -1;
Guido van Rossum2dff9911992-09-03 20:50:59 +0000106 char *filename = "???";
Guido van Rossum9bfef441993-03-29 10:43:31 +0000107 char *name = "???";
Guido van Rossumd076c731998-10-07 19:42:25 +0000108
Guido van Rossuma396a882000-04-07 01:21:36 +0000109 if (co->co_firstlineno != 0)
110 lineno = co->co_firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000111 if (co->co_filename && PyString_Check(co->co_filename))
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000112 filename = PyString_AS_STRING(co->co_filename);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000113 if (co->co_name && PyString_Check(co->co_name))
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000114 name = PyString_AS_STRING(co->co_name);
Fred Drake615ae552000-06-30 16:20:13 +0000115 sprintf(buf, "<code object %.100s at %p, file \"%.300s\", line %d>",
116 name, co, filename, lineno);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000117 return PyString_FromString(buf);
Guido van Rossum2dff9911992-09-03 20:50:59 +0000118}
119
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000120static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000121code_compare(PyCodeObject *co, PyCodeObject *cp)
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000122{
123 int cmp;
Guido van Rossum44679592000-04-10 16:20:31 +0000124 cmp = PyObject_Compare(co->co_name, cp->co_name);
125 if (cmp) return cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000126 cmp = co->co_argcount - cp->co_argcount;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000127 if (cmp) return cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000128 cmp = co->co_nlocals - cp->co_nlocals;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000129 if (cmp) return cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000130 cmp = co->co_flags - cp->co_flags;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000131 if (cmp) return cmp;
Guido van Rossumd076c731998-10-07 19:42:25 +0000132 cmp = PyObject_Compare(co->co_code, cp->co_code);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000133 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000134 cmp = PyObject_Compare(co->co_consts, cp->co_consts);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000135 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000136 cmp = PyObject_Compare(co->co_names, cp->co_names);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000137 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000138 cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000139 if (cmp) return cmp;
140 cmp = PyObject_Compare(co->co_freevars, cp->co_freevars);
141 if (cmp) return cmp;
142 cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000143 return cmp;
144}
145
146static long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000147code_hash(PyCodeObject *co)
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000148{
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000149 long h, h0, h1, h2, h3, h4, h5, h6;
Guido van Rossum44679592000-04-10 16:20:31 +0000150 h0 = PyObject_Hash(co->co_name);
151 if (h0 == -1) return -1;
Guido van Rossumd076c731998-10-07 19:42:25 +0000152 h1 = PyObject_Hash(co->co_code);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000153 if (h1 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000154 h2 = PyObject_Hash(co->co_consts);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000155 if (h2 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000156 h3 = PyObject_Hash(co->co_names);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000157 if (h3 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000158 h4 = PyObject_Hash(co->co_varnames);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000159 if (h4 == -1) return -1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000160 h5 = PyObject_Hash(co->co_freevars);
161 if (h5 == -1) return -1;
162 h6 = PyObject_Hash(co->co_cellvars);
163 if (h6 == -1) return -1;
164 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
Guido van Rossum681d79a1995-07-18 14:51:37 +0000165 co->co_argcount ^ co->co_nlocals ^ co->co_flags;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000166 if (h == -1) h = -2;
167 return h;
168}
169
Guido van Rossum79f25d91997-04-29 20:08:16 +0000170PyTypeObject PyCode_Type = {
171 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000172 0,
173 "code",
Guido van Rossum79f25d91997-04-29 20:08:16 +0000174 sizeof(PyCodeObject),
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000175 0,
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000176 (destructor)code_dealloc, /*tp_dealloc*/
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000177 0, /*tp_print*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000178 (getattrfunc)code_getattr, /*tp_getattr*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000179 0, /*tp_setattr*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000180 (cmpfunc)code_compare, /*tp_compare*/
181 (reprfunc)code_repr, /*tp_repr*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000182 0, /*tp_as_number*/
183 0, /*tp_as_sequence*/
184 0, /*tp_as_mapping*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000185 (hashfunc)code_hash, /*tp_hash*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000186};
187
Guido van Rossum644a12b1997-04-09 19:24:53 +0000188#define NAME_CHARS \
189 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
190
Guido van Rossumfc5ce612001-01-19 00:24:06 +0000191/* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
192
193static int
194all_name_chars(unsigned char *s)
195{
Guido van Rossumcd90c202001-02-09 15:06:42 +0000196 static char ok_name_char[256];
197 static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
Guido van Rossumfc5ce612001-01-19 00:24:06 +0000198
Guido van Rossumcd90c202001-02-09 15:06:42 +0000199 if (ok_name_char[*name_chars] == 0) {
200 unsigned char *p;
201 for (p = name_chars; *p; p++)
202 ok_name_char[*p] = 1;
203 }
204 while (*s) {
205 if (ok_name_char[*s++] == 0)
206 return 0;
207 }
208 return 1;
Guido van Rossumfc5ce612001-01-19 00:24:06 +0000209}
210
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000211static int
212intern_strings(PyObject *tuple)
213{
214 int i;
215
216 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
217 PyObject *v = PyTuple_GET_ITEM(tuple, i);
218 if (v == NULL || !PyString_Check(v)) {
219 Py_FatalError("non-string found in code slot");
220 PyErr_BadInternalCall();
221 return -1;
222 }
223 PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
224 }
225 return 0;
226}
227
Guido van Rossum79f25d91997-04-29 20:08:16 +0000228PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000229PyCode_New(int argcount, int nlocals, int stacksize, int flags,
230 PyObject *code, PyObject *consts, PyObject *names,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000231 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
232 PyObject *filename, PyObject *name, int firstlineno,
233 PyObject *lnotab)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000234{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000235 PyCodeObject *co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000236 int i;
Guido van Rossumd076c731998-10-07 19:42:25 +0000237 PyBufferProcs *pb;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000238 /* Check argument types */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000239 if (argcount < 0 || nlocals < 0 ||
Guido van Rossumd076c731998-10-07 19:42:25 +0000240 code == NULL ||
Guido van Rossum79f25d91997-04-29 20:08:16 +0000241 consts == NULL || !PyTuple_Check(consts) ||
242 names == NULL || !PyTuple_Check(names) ||
243 varnames == NULL || !PyTuple_Check(varnames) ||
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000244 freevars == NULL || !PyTuple_Check(freevars) ||
245 cellvars == NULL || !PyTuple_Check(cellvars) ||
Guido van Rossum79f25d91997-04-29 20:08:16 +0000246 name == NULL || !PyString_Check(name) ||
247 filename == NULL || !PyString_Check(filename) ||
248 lnotab == NULL || !PyString_Check(lnotab)) {
249 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000250 return NULL;
251 }
Guido van Rossumd076c731998-10-07 19:42:25 +0000252 pb = code->ob_type->tp_as_buffer;
253 if (pb == NULL ||
254 pb->bf_getreadbuffer == NULL ||
255 pb->bf_getsegcount == NULL ||
256 (*pb->bf_getsegcount)(code, NULL) != 1)
257 {
258 PyErr_BadInternalCall();
259 return NULL;
260 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000261 intern_strings(names);
262 intern_strings(varnames);
263 if (freevars == NULL)
264 freevars = PyTuple_New(0);
265 intern_strings(freevars);
266 if (cellvars == NULL)
267 cellvars = PyTuple_New(0);
268 intern_strings(cellvars);
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000269 /* Intern selected string constants */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000270 for (i = PyTuple_Size(consts); --i >= 0; ) {
271 PyObject *v = PyTuple_GetItem(consts, i);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000272 if (!PyString_Check(v))
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000273 continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000274 if (!all_name_chars((unsigned char *)PyString_AS_STRING(v)))
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000275 continue;
276 PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000277 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000278 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000279 if (co != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +0000280 co->co_argcount = argcount;
281 co->co_nlocals = nlocals;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000282 co->co_stacksize = stacksize;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000283 co->co_flags = flags;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000284 Py_INCREF(code);
Guido van Rossumd076c731998-10-07 19:42:25 +0000285 co->co_code = code;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000286 Py_INCREF(consts);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000287 co->co_consts = consts;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000288 Py_INCREF(names);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000289 co->co_names = names;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000290 Py_INCREF(varnames);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000291 co->co_varnames = varnames;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000292 Py_INCREF(freevars);
293 co->co_freevars = freevars;
294 Py_INCREF(cellvars);
295 co->co_cellvars = cellvars;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000296 Py_INCREF(filename);
Guido van Rossuma082ce41991-06-04 19:41:56 +0000297 co->co_filename = filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000298 Py_INCREF(name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000299 co->co_name = name;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000300 co->co_firstlineno = firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000301 Py_INCREF(lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000302 co->co_lnotab = lnotab;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000303/* PyObject_Print((PyObject *)co, stderr, 0); */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000304 }
305 return co;
306}
307
308
309/* Data structure used internally */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000310
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000311/* The compiler uses two passes to generate bytecodes. The first pass
312 builds the symbol table. The second pass generates the bytecode.
313
314 The first pass uses a single symtable struct. The second pass uses
315 a compiling struct for each code block. The compiling structs
316 share a reference to the symtable.
317
318 The two passes communicate via symtable_load_symbols() and via
319 is_local() and is_global(). The former initializes several slots
320 in the compiling struct: c_varnames, c_locals, c_nlocals,
321 c_argcount, c_globals, and c_flags.
322*/
323
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000324struct compiling {
Fred Drakefd1f1be2000-09-08 16:31:24 +0000325 PyObject *c_code; /* string */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000326 PyObject *c_consts; /* list of objects */
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000327 PyObject *c_const_dict; /* inverse of c_consts */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000328 PyObject *c_names; /* list of strings (names) */
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000329 PyObject *c_name_dict; /* inverse of c_names */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000330 PyObject *c_globals; /* dictionary (value=None) */
331 PyObject *c_locals; /* dictionary (value=localID) */
332 PyObject *c_varnames; /* list (inverse of c_locals) */
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000333 PyObject *c_freevars; /* dictionary (value=None) */
334 PyObject *c_cellvars; /* list */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000335 int c_nlocals; /* index of next local */
336 int c_argcount; /* number of top-level arguments */
337 int c_flags; /* same as co_flags */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000338 int c_nexti; /* index into c_code */
339 int c_errors; /* counts errors occurred */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000340 int c_infunction; /* set when compiling a function */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000341 int c_interactive; /* generating code for interactive command */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000342 int c_loops; /* counts nested loops */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000343 int c_begin; /* begin of current loop, for 'continue' */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000344 int c_block[CO_MAXBLOCKS]; /* stack of block types */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000345 int c_nblocks; /* current block stack level */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000346 char *c_filename; /* filename of current node */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000347 char *c_name; /* name of object (e.g. function) */
Guido van Rossum452a9831996-09-17 14:32:04 +0000348 int c_lineno; /* Current line number */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000349 int c_stacklevel; /* Current stack level */
350 int c_maxstacklevel; /* Maximum stack level */
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000351 int c_firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000352 PyObject *c_lnotab; /* Table mapping address to line number */
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000353 int c_last_addr, c_last_line, c_lnotab_next;
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000354 char *c_private; /* for private name mangling */
Skip Montanaro803d6e52000-08-12 18:09:51 +0000355 int c_tmpname; /* temporary local name counter */
Guido van Rossumcd90c202001-02-09 15:06:42 +0000356 int c_nested; /* Is block nested funcdef or lamdef? */
357 int c_closure; /* Is nested w/freevars? */
358 struct symtable *c_symtable; /* pointer to module symbol table */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000359};
360
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000361int is_free(int v)
362{
363 if ((v & (USE | DEF_FREE))
364 && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL)))
365 return 1;
366 if (v & DEF_FREE_CLASS)
367 return 1;
368 return 0;
369}
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000370
Guido van Rossum452a9831996-09-17 14:32:04 +0000371/* Error message including line number */
372
373static void
Jeremy Hylton5acc0c02001-02-02 20:01:10 +0000374set_error_location(char *filename, int lineno)
375{
376 PyObject *exc, *v, *tb, *tmp;
377
378 /* add attributes for the line number and filename for the error */
379 PyErr_Fetch(&exc, &v, &tb);
380 PyErr_NormalizeException(&exc, &v, &tb);
381 tmp = PyInt_FromLong(lineno);
382 if (tmp == NULL)
383 PyErr_Clear();
384 else {
385 if (PyObject_SetAttrString(v, "lineno", tmp))
386 PyErr_Clear();
387 Py_DECREF(tmp);
388 }
389 if (filename != NULL) {
390 tmp = PyString_FromString(filename);
391 if (tmp == NULL)
392 PyErr_Clear();
393 else {
394 if (PyObject_SetAttrString(v, "filename", tmp))
395 PyErr_Clear();
396 Py_DECREF(tmp);
397 }
398 }
399 PyErr_Restore(exc, v, tb);
400}
401
402static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000403com_error(struct compiling *c, PyObject *exc, char *msg)
Guido van Rossum452a9831996-09-17 14:32:04 +0000404{
Jeremy Hylton5acc0c02001-02-02 20:01:10 +0000405 PyObject *v;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000406 if (c == NULL) {
407 /* Error occurred via symtable call to
408 is_constant_false */
409 PyErr_SetString(exc, msg);
410 return;
411 }
Guido van Rossum635abd21997-01-06 22:56:52 +0000412 c->c_errors++;
Guido van Rossum452a9831996-09-17 14:32:04 +0000413 if (c->c_lineno <= 1) {
414 /* Unknown line number or single interactive command */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000415 PyErr_SetString(exc, msg);
Guido van Rossum452a9831996-09-17 14:32:04 +0000416 return;
417 }
Fred Drakedcf08e02000-08-15 15:49:44 +0000418 v = PyString_FromString(msg);
Guido van Rossum452a9831996-09-17 14:32:04 +0000419 if (v == NULL)
420 return; /* MemoryError, too bad */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000421 PyErr_SetObject(exc, v);
422 Py_DECREF(v);
Fred Drakedcf08e02000-08-15 15:49:44 +0000423
Jeremy Hylton5acc0c02001-02-02 20:01:10 +0000424 set_error_location(c->c_filename, c->c_lineno);
Guido van Rossum452a9831996-09-17 14:32:04 +0000425}
426
427
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000428/* Interface to the block stack */
429
430static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000431block_push(struct compiling *c, int type)
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000432{
Guido van Rossum8b993a91997-01-17 21:04:03 +0000433 if (c->c_nblocks >= CO_MAXBLOCKS) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000434 com_error(c, PyExc_SystemError,
435 "too many statically nested blocks");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000436 }
437 else {
438 c->c_block[c->c_nblocks++] = type;
439 }
440}
441
442static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000443block_pop(struct compiling *c, int type)
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000444{
445 if (c->c_nblocks > 0)
446 c->c_nblocks--;
447 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000448 com_error(c, PyExc_SystemError, "bad block pop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000449 }
450}
451
452
Guido van Rossum681d79a1995-07-18 14:51:37 +0000453/* Prototype forward declarations */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000454
Tim Petersdbd9ba62000-07-09 03:09:57 +0000455static int com_init(struct compiling *, char *);
456static void com_free(struct compiling *);
457static void com_push(struct compiling *, int);
458static void com_pop(struct compiling *, int);
459static void com_done(struct compiling *);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000460static void com_node(struct compiling *, node *);
461static void com_factor(struct compiling *, node *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000462static void com_addbyte(struct compiling *, int);
463static void com_addint(struct compiling *, int);
464static void com_addoparg(struct compiling *, int, int);
465static void com_addfwref(struct compiling *, int, int *);
466static void com_backpatch(struct compiling *, int);
467static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *);
468static int com_addconst(struct compiling *, PyObject *);
469static int com_addname(struct compiling *, PyObject *);
470static void com_addopname(struct compiling *, int, node *);
471static void com_list(struct compiling *, node *, int);
Skip Montanaro803d6e52000-08-12 18:09:51 +0000472static void com_list_iter(struct compiling *, node *, node *, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000473static int com_argdefs(struct compiling *, node *);
Thomas Wouters434d0822000-08-24 20:11:32 +0000474static void com_assign(struct compiling *, node *, int, node *);
475static void com_assign_name(struct compiling *, node *, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000476static PyCodeObject *icompile(node *, struct compiling *);
477static PyCodeObject *jcompile(node *, char *, struct compiling *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000478static PyObject *parsestrplus(node *);
479static PyObject *parsestr(char *);
Thomas Wouters434d0822000-08-24 20:11:32 +0000480static node *get_rawdocstring(node *);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000481
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000482static int get_ref_type(struct compiling *, char *);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000483
484/* symtable operations */
485static int symtable_build(struct compiling *, node *);
486static int symtable_load_symbols(struct compiling *);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000487static struct symtable *symtable_init(void);
Jeremy Hylton4b38da62001-02-02 18:19:15 +0000488static void symtable_enter_scope(struct symtable *, char *, int, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000489static int symtable_exit_scope(struct symtable *);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000490static int symtable_add_def(struct symtable *, char *, int);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000491static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000492
493static void symtable_node(struct symtable *, node *);
494static void symtable_funcdef(struct symtable *, node *);
495static void symtable_default_args(struct symtable *, node *);
496static void symtable_params(struct symtable *, node *);
497static void symtable_params_fplist(struct symtable *, node *n);
498static void symtable_global(struct symtable *, node *);
499static void symtable_import(struct symtable *, node *);
Jeremy Hyltoneab156f2001-01-30 01:24:43 +0000500static void symtable_assign(struct symtable *, node *, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000501static void symtable_list_comprehension(struct symtable *, node *);
502
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000503static int symtable_update_free_vars(struct symtable *);
504static int symtable_undo_free(struct symtable *, PyObject *, PyObject *);
505static int symtable_check_global(struct symtable *, PyObject *, PyObject *);
506
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000507/* helper */
508static void
509do_pad(int pad)
510{
511 int i;
512 for (i = 0; i < pad; ++i)
513 fprintf(stderr, " ");
514}
515
516static void
517dump(node *n, int pad, int depth)
518{
519 int i;
520 if (depth == 0)
521 return;
522 do_pad(pad);
523 fprintf(stderr, "%d: %s\n", TYPE(n), STR(n));
524 if (depth > 0)
525 depth--;
526 for (i = 0; i < NCH(n); ++i)
527 dump(CHILD(n, i), pad + 1, depth);
528}
529
530#define DUMP(N) dump(N, 0, -1)
531
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000532static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000533com_init(struct compiling *c, char *filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000534{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000535 memset((void *)c, '\0', sizeof(struct compiling));
Guido van Rossum79f25d91997-04-29 20:08:16 +0000536 if ((c->c_code = PyString_FromStringAndSize((char *)NULL,
537 1000)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000538 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000539 if ((c->c_consts = PyList_New(0)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000540 goto fail;
541 if ((c->c_const_dict = PyDict_New()) == NULL)
542 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000543 if ((c->c_names = PyList_New(0)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000544 goto fail;
545 if ((c->c_name_dict = PyDict_New()) == NULL)
546 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000547 if ((c->c_locals = PyDict_New()) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000548 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000549 if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL,
550 1000)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000551 goto fail;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000552 c->c_globals = NULL;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000553 c->c_varnames = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000554 c->c_freevars = NULL;
555 c->c_cellvars = NULL;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000556 c->c_nlocals = 0;
557 c->c_argcount = 0;
558 c->c_flags = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000559 c->c_nexti = 0;
560 c->c_errors = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000561 c->c_infunction = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000562 c->c_interactive = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000563 c->c_loops = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000564 c->c_begin = 0;
565 c->c_nblocks = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000566 c->c_filename = filename;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000567 c->c_name = "?";
Guido van Rossum452a9831996-09-17 14:32:04 +0000568 c->c_lineno = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000569 c->c_stacklevel = 0;
570 c->c_maxstacklevel = 0;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000571 c->c_firstlineno = 0;
572 c->c_last_addr = 0;
573 c->c_last_line = 0;
Barry Warsaw174e8012001-01-22 04:35:57 +0000574 c->c_lnotab_next = 0;
Skip Montanaro803d6e52000-08-12 18:09:51 +0000575 c->c_tmpname = 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000576 c->c_nested = 0;
577 c->c_closure = 0;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000578 c->c_symtable = NULL;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000579 return 1;
580
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000581 fail:
582 com_free(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000583 return 0;
584}
585
586static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000587com_free(struct compiling *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000588{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000589 Py_XDECREF(c->c_code);
590 Py_XDECREF(c->c_consts);
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000591 Py_XDECREF(c->c_const_dict);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000592 Py_XDECREF(c->c_names);
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000593 Py_XDECREF(c->c_name_dict);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000594 Py_XDECREF(c->c_globals);
595 Py_XDECREF(c->c_locals);
596 Py_XDECREF(c->c_varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000597 Py_XDECREF(c->c_freevars);
598 Py_XDECREF(c->c_cellvars);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000599 Py_XDECREF(c->c_lnotab);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000600}
601
602static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000603com_push(struct compiling *c, int n)
Guido van Rossum8b993a91997-01-17 21:04:03 +0000604{
605 c->c_stacklevel += n;
606 if (c->c_stacklevel > c->c_maxstacklevel)
607 c->c_maxstacklevel = c->c_stacklevel;
608}
609
610static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000611com_pop(struct compiling *c, int n)
Guido van Rossum8b993a91997-01-17 21:04:03 +0000612{
613 if (c->c_stacklevel < n) {
Guido van Rossum90f827c1998-08-25 18:22:17 +0000614 /* fprintf(stderr,
Guido van Rossum8b993a91997-01-17 21:04:03 +0000615 "%s:%d: underflow! nexti=%d, level=%d, n=%d\n",
616 c->c_filename, c->c_lineno,
Guido van Rossum90f827c1998-08-25 18:22:17 +0000617 c->c_nexti, c->c_stacklevel, n); */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000618 c->c_stacklevel = 0;
619 }
620 else
621 c->c_stacklevel -= n;
622}
623
624static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000625com_done(struct compiling *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000626{
627 if (c->c_code != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000628 _PyString_Resize(&c->c_code, c->c_nexti);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000629 if (c->c_lnotab != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000630 _PyString_Resize(&c->c_lnotab, c->c_lnotab_next);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000631}
632
633static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000634com_addbyte(struct compiling *c, int byte)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000635{
636 int len;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000637 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000638 assert(byte >= 0 && byte <= 255);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000639 if (byte < 0 || byte > 255) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000640 com_error(c, PyExc_SystemError,
641 "com_addbyte: byte out of range");
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000642 }
643 if (c->c_code == NULL)
644 return;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000645 len = PyString_Size(c->c_code);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000646 if (c->c_nexti >= len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000647 if (_PyString_Resize(&c->c_code, len+1000) != 0) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000648 c->c_errors++;
649 return;
650 }
651 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000652 PyString_AsString(c->c_code)[c->c_nexti++] = byte;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000653}
654
655static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000656com_addint(struct compiling *c, int x)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000657{
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000658 com_addbyte(c, x & 0xff);
659 com_addbyte(c, x >> 8); /* XXX x should be positive */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000660}
661
662static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000663com_add_lnotab(struct compiling *c, int addr, int line)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000664{
665 int size;
666 char *p;
667 if (c->c_lnotab == NULL)
668 return;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000669 size = PyString_Size(c->c_lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000670 if (c->c_lnotab_next+2 > size) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000671 if (_PyString_Resize(&c->c_lnotab, size + 1000) < 0) {
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000672 c->c_errors++;
673 return;
674 }
675 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000676 p = PyString_AsString(c->c_lnotab) + c->c_lnotab_next;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000677 *p++ = addr;
678 *p++ = line;
679 c->c_lnotab_next += 2;
680}
681
682static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000683com_set_lineno(struct compiling *c, int lineno)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000684{
685 c->c_lineno = lineno;
686 if (c->c_firstlineno == 0) {
687 c->c_firstlineno = c->c_last_line = lineno;
688 }
689 else {
690 int incr_addr = c->c_nexti - c->c_last_addr;
691 int incr_line = lineno - c->c_last_line;
692 while (incr_addr > 0 || incr_line > 0) {
693 int trunc_addr = incr_addr;
694 int trunc_line = incr_line;
695 if (trunc_addr > 255)
696 trunc_addr = 255;
697 if (trunc_line > 255)
698 trunc_line = 255;
699 com_add_lnotab(c, trunc_addr, trunc_line);
700 incr_addr -= trunc_addr;
701 incr_line -= trunc_line;
702 }
703 c->c_last_addr = c->c_nexti;
704 c->c_last_line = lineno;
705 }
706}
707
708static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000709com_addoparg(struct compiling *c, int op, int arg)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000710{
Fred Drakeef8ace32000-08-24 00:32:09 +0000711 int extended_arg = arg >> 16;
Guido van Rossum8e793d91997-03-03 19:13:14 +0000712 if (op == SET_LINENO) {
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000713 com_set_lineno(c, arg);
Guido van Rossum8e793d91997-03-03 19:13:14 +0000714 if (Py_OptimizeFlag)
715 return;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000716 }
Fred Drakeef8ace32000-08-24 00:32:09 +0000717 if (extended_arg){
718 com_addbyte(c, EXTENDED_ARG);
719 com_addint(c, extended_arg);
720 arg &= 0xffff;
721 }
Guido van Rossum8e793d91997-03-03 19:13:14 +0000722 com_addbyte(c, op);
723 com_addint(c, arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000724}
725
726static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000727com_addfwref(struct compiling *c, int op, int *p_anchor)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000728{
729 /* Compile a forward reference for backpatching */
730 int here;
731 int anchor;
732 com_addbyte(c, op);
733 here = c->c_nexti;
734 anchor = *p_anchor;
735 *p_anchor = here;
736 com_addint(c, anchor == 0 ? 0 : here - anchor);
737}
738
739static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000740com_backpatch(struct compiling *c, int anchor)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000741{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000742 unsigned char *code = (unsigned char *) PyString_AsString(c->c_code);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000743 int target = c->c_nexti;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000744 int dist;
745 int prev;
746 for (;;) {
747 /* Make the JUMP instruction at anchor point to target */
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000748 prev = code[anchor] + (code[anchor+1] << 8);
749 dist = target - (anchor+2);
750 code[anchor] = dist & 0xff;
Fred Drakeef8ace32000-08-24 00:32:09 +0000751 dist >>= 8;
752 code[anchor+1] = dist;
753 dist >>= 8;
754 if (dist) {
755 com_error(c, PyExc_SystemError,
756 "com_backpatch: offset too large");
757 break;
758 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000759 if (!prev)
760 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000761 anchor -= prev;
762 }
763}
764
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000765/* Handle literals and names uniformly */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000766
767static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000768com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000769{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000770 PyObject *w, *t, *np=NULL;
771 long n;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000772
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000773 t = Py_BuildValue("(OO)", v, v->ob_type);
774 if (t == NULL)
775 goto fail;
776 w = PyDict_GetItem(dict, t);
777 if (w != NULL) {
778 n = PyInt_AsLong(w);
779 } else {
780 n = PyList_Size(list);
781 np = PyInt_FromLong(n);
782 if (np == NULL)
783 goto fail;
784 if (PyList_Append(list, v) != 0)
785 goto fail;
786 if (PyDict_SetItem(dict, t, np) != 0)
787 goto fail;
788 Py_DECREF(np);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000789 }
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000790 Py_DECREF(t);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000791 return n;
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000792 fail:
793 Py_XDECREF(np);
794 Py_XDECREF(t);
795 c->c_errors++;
796 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000797}
798
799static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000800com_addconst(struct compiling *c, PyObject *v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000801{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000802 return com_add(c, c->c_consts, c->c_const_dict, v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000803}
804
805static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000806com_addname(struct compiling *c, PyObject *v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000807{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000808 return com_add(c, c->c_names, c->c_name_dict, v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000809}
810
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000811static int
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000812mangle(char *p, char *name, char *buffer, size_t maxlen)
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000813{
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000814 /* Name mangling: __private becomes _classname__private.
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000815 This is independent from how the name is used. */
Guido van Rossum582acec2000-06-28 22:07:35 +0000816 size_t nlen, plen;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000817 if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_')
818 return 0;
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000819 nlen = strlen(name);
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000820 if (nlen+2 >= maxlen)
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000821 return 0; /* Don't mangle __extremely_long_names */
822 if (name[nlen-1] == '_' && name[nlen-2] == '_')
823 return 0; /* Don't mangle __whatever__ */
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000824 /* Strip leading underscores from class name */
825 while (*p == '_')
826 p++;
827 if (*p == '\0')
828 return 0; /* Don't mangle if class is just underscores */
829 plen = strlen(p);
830 if (plen + nlen >= maxlen)
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000831 plen = maxlen-nlen-2; /* Truncate class name if too long */
832 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000833 buffer[0] = '_';
834 strncpy(buffer+1, p, plen);
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000835 strcpy(buffer+1+plen, name);
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000836 return 1;
837}
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000838
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000839static void
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000840com_addop_name(struct compiling *c, int op, char *name)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000841{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000842 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000843 int i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000844 char buffer[MANGLE_LEN];
Jeremy Hyltoneab156f2001-01-30 01:24:43 +0000845
846 if (mangle(c->c_private, name, buffer, sizeof(buffer)))
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000847 name = buffer;
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000848 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000849 c->c_errors++;
850 i = 255;
851 }
852 else {
853 i = com_addname(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000854 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000855 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000856 com_addoparg(c, op, i);
857}
858
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000859#define NAME_LOCAL 0
860#define NAME_GLOBAL 1
861#define NAME_DEFAULT 2
862#define NAME_CLOSURE 3
863
864static int
865com_lookup_arg(PyObject *dict, PyObject *name)
866{
867 PyObject *v = PyDict_GetItem(dict, name);
868 if (v == NULL)
869 return -1;
870 else
871 return PyInt_AS_LONG(v);
872}
873
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000874static void
875com_addop_varname(struct compiling *c, int kind, char *name)
876{
877 PyObject *v;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000878 int i, reftype;
879 int scope = NAME_DEFAULT;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000880 int op = STOP_CODE;
881 char buffer[MANGLE_LEN];
882
Jeremy Hyltoneab156f2001-01-30 01:24:43 +0000883 if (mangle(c->c_private, name, buffer, sizeof(buffer)))
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000884 name = buffer;
885 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
886 c->c_errors++;
887 i = 255;
888 goto done;
Guido van Rossumc5e96291991-12-10 13:53:51 +0000889 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000890
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000891 reftype = get_ref_type(c, name);
892 switch (reftype) {
893 case LOCAL:
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000894 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION)
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000895 scope = NAME_LOCAL;
896 break;
897 case GLOBAL_EXPLICIT:
898 scope = NAME_GLOBAL;
899 break;
900 case GLOBAL_IMPLICIT:
901 if (c->c_flags & CO_OPTIMIZED)
902 scope = NAME_GLOBAL;
903 break;
904 case FREE:
905 case CELL:
906 scope = NAME_CLOSURE;
907 break;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000908 }
909
910 i = com_addname(c, v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000911 if (scope == NAME_LOCAL)
912 i = com_lookup_arg(c->c_locals, v);
913 else if (reftype == FREE)
914 i = com_lookup_arg(c->c_freevars, v);
915 else if (reftype == CELL)
916 i = com_lookup_arg(c->c_cellvars, v);
917 if (i == -1) {
918 c->c_errors++; /* XXX no exception set */
919 i = 255;
920 goto done;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000921 }
922 Py_DECREF(v);
923
924 switch (kind) {
925 case VAR_LOAD:
926 switch (scope) {
927 case NAME_LOCAL:
928 op = LOAD_FAST;
929 break;
930 case NAME_GLOBAL:
931 op = LOAD_GLOBAL;
932 break;
933 case NAME_DEFAULT:
934 op = LOAD_NAME;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000935 break;
936 case NAME_CLOSURE:
937 op = LOAD_DEREF;
938 break;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000939 }
940 break;
941 case VAR_STORE:
942 switch (scope) {
943 case NAME_LOCAL:
944 op = STORE_FAST;
945 break;
946 case NAME_GLOBAL:
947 op = STORE_GLOBAL;
948 break;
949 case NAME_DEFAULT:
950 op = STORE_NAME;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000951 break;
952 case NAME_CLOSURE:
953 op = STORE_DEREF;
954 break;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000955 }
956 break;
957 case VAR_DELETE:
958 switch (scope) {
959 case NAME_LOCAL:
960 op = DELETE_FAST;
961 break;
962 case NAME_GLOBAL:
963 op = DELETE_GLOBAL;
964 break;
965 case NAME_DEFAULT:
966 op = DELETE_NAME;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000967 break;
968 case NAME_CLOSURE: {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +0000969 char buf[500];
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000970 sprintf(buf, DEL_CLOSURE_ERROR, name);
971 com_error(c, PyExc_SyntaxError, buf);
972 i = 255;
973 break;
974 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000975 }
976 break;
977 }
978done:
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000979 com_addoparg(c, op, i);
980}
981
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000982static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000983com_addopname(struct compiling *c, int op, node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000984{
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000985 char *name;
986 char buffer[1000];
987 /* XXX it is possible to write this code without the 1000
988 chars on the total length of dotted names, I just can't be
989 bothered right now */
990 if (TYPE(n) == STAR)
991 name = "*";
992 else if (TYPE(n) == dotted_name) {
993 char *p = buffer;
994 int i;
995 name = buffer;
996 for (i = 0; i < NCH(n); i += 2) {
997 char *s = STR(CHILD(n, i));
998 if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000999 com_error(c, PyExc_MemoryError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001000 "dotted_name too long");
Guido van Rossumd9dfaf51995-02-17 15:04:57 +00001001 name = NULL;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001002 break;
1003 }
1004 if (p != buffer)
1005 *p++ = '.';
1006 strcpy(p, s);
1007 p = strchr(p, '\0');
1008 }
1009 }
1010 else {
1011 REQ(n, NAME);
1012 name = STR(n);
1013 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001014 com_addop_name(c, op, name);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001015}
1016
Guido van Rossum79f25d91997-04-29 20:08:16 +00001017static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001018parsenumber(struct compiling *co, char *s)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001019{
Guido van Rossumc5e96291991-12-10 13:53:51 +00001020 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001021 long x;
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001022 double dx;
Guido van Rossum50564e81996-01-12 01:13:16 +00001023#ifndef WITHOUT_COMPLEX
Guido van Rossum530956d1996-07-21 02:27:43 +00001024 Py_complex c;
Guido van Rossum50564e81996-01-12 01:13:16 +00001025 int imflag;
1026#endif
1027
Guido van Rossum282914b1991-04-04 10:42:56 +00001028 errno = 0;
Guido van Rossumc5e96291991-12-10 13:53:51 +00001029 end = s + strlen(s) - 1;
Guido van Rossum50564e81996-01-12 01:13:16 +00001030#ifndef WITHOUT_COMPLEX
Guido van Rossum15ad9a61996-01-26 20:53:56 +00001031 imflag = *end == 'j' || *end == 'J';
Guido van Rossum50564e81996-01-12 01:13:16 +00001032#endif
Guido van Rossumf1aeab71992-03-27 17:28:26 +00001033 if (*end == 'l' || *end == 'L')
Guido van Rossum79f25d91997-04-29 20:08:16 +00001034 return PyLong_FromString(s, (char **)0, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +00001035 if (s[0] == '0')
Guido van Rossum79f25d91997-04-29 20:08:16 +00001036 x = (long) PyOS_strtoul(s, &end, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +00001037 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001038 x = PyOS_strtol(s, &end, 0);
Guido van Rossum282914b1991-04-04 10:42:56 +00001039 if (*end == '\0') {
1040 if (errno != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001041 com_error(co, PyExc_OverflowError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001042 "integer literal too large");
Guido van Rossum282914b1991-04-04 10:42:56 +00001043 return NULL;
1044 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001045 return PyInt_FromLong(x);
Guido van Rossum282914b1991-04-04 10:42:56 +00001046 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001047 /* XXX Huge floats may silently fail */
Guido van Rossum50564e81996-01-12 01:13:16 +00001048#ifndef WITHOUT_COMPLEX
1049 if (imflag) {
1050 c.real = 0.;
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001051 PyFPE_START_PROTECT("atof", return 0)
Guido van Rossum50564e81996-01-12 01:13:16 +00001052 c.imag = atof(s);
Guido van Rossum45b83911997-03-14 04:32:50 +00001053 PyFPE_END_PROTECT(c)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001054 return PyComplex_FromCComplex(c);
Guido van Rossum50564e81996-01-12 01:13:16 +00001055 }
Guido van Rossumac1fc951997-10-08 15:23:55 +00001056 else
Guido van Rossum50564e81996-01-12 01:13:16 +00001057#endif
Guido van Rossumac1fc951997-10-08 15:23:55 +00001058 {
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001059 PyFPE_START_PROTECT("atof", return 0)
1060 dx = atof(s);
Guido van Rossum45b83911997-03-14 04:32:50 +00001061 PyFPE_END_PROTECT(dx)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001062 return PyFloat_FromDouble(dx);
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001063 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001064}
1065
Guido van Rossum79f25d91997-04-29 20:08:16 +00001066static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001067parsestr(char *s)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001068{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001069 PyObject *v;
Guido van Rossum582acec2000-06-28 22:07:35 +00001070 size_t len;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001071 char *buf;
1072 char *p;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001073 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001074 int c;
Guido van Rossum5f5e8171997-04-06 03:41:40 +00001075 int first = *s;
1076 int quote = first;
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001077 int rawmode = 0;
1078 int unicode = 0;
1079 if (isalpha(quote) || quote == '_') {
1080 if (quote == 'u' || quote == 'U') {
1081 quote = *++s;
1082 unicode = 1;
1083 }
1084 if (quote == 'r' || quote == 'R') {
1085 quote = *++s;
1086 rawmode = 1;
1087 }
1088 }
Guido van Rossum8054fad1993-10-26 15:19:44 +00001089 if (quote != '\'' && quote != '\"') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001090 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001091 return NULL;
1092 }
1093 s++;
1094 len = strlen(s);
Guido van Rossum582acec2000-06-28 22:07:35 +00001095 if (len > INT_MAX) {
1096 PyErr_SetString(PyExc_OverflowError, "string to parse is too long");
1097 return NULL;
1098 }
Guido van Rossum8054fad1993-10-26 15:19:44 +00001099 if (s[--len] != quote) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001100 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001101 return NULL;
1102 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001103 if (len >= 4 && s[0] == quote && s[1] == quote) {
1104 s += 2;
1105 len -= 2;
1106 if (s[--len] != quote || s[--len] != quote) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001107 PyErr_BadInternalCall();
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001108 return NULL;
1109 }
1110 }
Guido van Rossumfdc8bdb2000-05-01 17:54:56 +00001111 if (unicode || Py_UnicodeFlag) {
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001112 if (rawmode)
1113 return PyUnicode_DecodeRawUnicodeEscape(
1114 s, len, NULL);
1115 else
1116 return PyUnicode_DecodeUnicodeEscape(
1117 s, len, NULL);
1118 }
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001119 if (rawmode || strchr(s, '\\') == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001120 return PyString_FromStringAndSize(s, len);
1121 v = PyString_FromStringAndSize((char *)NULL, len);
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001122 if (v == NULL)
1123 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001124 p = buf = PyString_AsString(v);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001125 end = s + len;
1126 while (s < end) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001127 if (*s != '\\') {
1128 *p++ = *s++;
1129 continue;
1130 }
1131 s++;
1132 switch (*s++) {
1133 /* XXX This assumes ASCII! */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001134 case '\n': break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001135 case '\\': *p++ = '\\'; break;
1136 case '\'': *p++ = '\''; break;
Guido van Rossum8054fad1993-10-26 15:19:44 +00001137 case '\"': *p++ = '\"'; break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001138 case 'b': *p++ = '\b'; break;
1139 case 'f': *p++ = '\014'; break; /* FF */
1140 case 't': *p++ = '\t'; break;
1141 case 'n': *p++ = '\n'; break;
1142 case 'r': *p++ = '\r'; break;
1143 case 'v': *p++ = '\013'; break; /* VT */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001144 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
1145 case '0': case '1': case '2': case '3':
1146 case '4': case '5': case '6': case '7':
1147 c = s[-1] - '0';
1148 if ('0' <= *s && *s <= '7') {
1149 c = (c<<3) + *s++ - '0';
1150 if ('0' <= *s && *s <= '7')
1151 c = (c<<3) + *s++ - '0';
1152 }
1153 *p++ = c;
1154 break;
1155 case 'x':
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001156 if (isxdigit(Py_CHARMASK(s[0])) && isxdigit(Py_CHARMASK(s[1]))) {
Guido van Rossumed1100f1997-10-20 23:24:07 +00001157 unsigned int x = 0;
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001158 c = Py_CHARMASK(*s);
1159 s++;
1160 if (isdigit(c))
1161 x = c - '0';
1162 else if (islower(c))
1163 x = 10 + c - 'a';
1164 else
1165 x = 10 + c - 'A';
1166 x = x << 4;
1167 c = Py_CHARMASK(*s);
1168 s++;
1169 if (isdigit(c))
1170 x += c - '0';
1171 else if (islower(c))
1172 x += 10 + c - 'a';
1173 else
1174 x += 10 + c - 'A';
Guido van Rossumed1100f1997-10-20 23:24:07 +00001175 *p++ = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001176 break;
1177 }
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001178 PyErr_SetString(PyExc_ValueError, "invalid \\x escape");
1179 Py_DECREF(v);
1180 return NULL;
1181 default:
1182 *p++ = '\\';
1183 *p++ = s[-1];
1184 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001185 }
1186 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001187 _PyString_Resize(&v, (int)(p - buf));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001188 return v;
1189}
1190
Guido van Rossum79f25d91997-04-29 20:08:16 +00001191static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001192parsestrplus(node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001193{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001194 PyObject *v;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001195 int i;
1196 REQ(CHILD(n, 0), STRING);
1197 if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) {
1198 /* String literal concatenation */
Fred Drake4e998bc2000-04-13 14:10:44 +00001199 for (i = 1; i < NCH(n); i++) {
1200 PyObject *s;
1201 s = parsestr(STR(CHILD(n, i)));
1202 if (s == NULL)
1203 goto onError;
1204 if (PyString_Check(v) && PyString_Check(s)) {
1205 PyString_ConcatAndDel(&v, s);
1206 if (v == NULL)
1207 goto onError;
1208 }
1209 else {
1210 PyObject *temp;
1211 temp = PyUnicode_Concat(v, s);
1212 Py_DECREF(s);
1213 if (temp == NULL)
1214 goto onError;
1215 Py_DECREF(v);
1216 v = temp;
1217 }
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001218 }
1219 }
1220 return v;
Fred Drake4e998bc2000-04-13 14:10:44 +00001221
1222 onError:
1223 Py_XDECREF(v);
1224 return NULL;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001225}
1226
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001227static void
Skip Montanaro803d6e52000-08-12 18:09:51 +00001228com_list_for(struct compiling *c, node *n, node *e, char *t)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001229{
Skip Montanaro803d6e52000-08-12 18:09:51 +00001230 PyObject *v;
1231 int anchor = 0;
1232 int save_begin = c->c_begin;
1233
1234 /* list_iter: for v in expr [list_iter] */
1235 com_node(c, CHILD(n, 3)); /* expr */
1236 v = PyInt_FromLong(0L);
1237 if (v == NULL)
1238 c->c_errors++;
1239 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
1240 com_push(c, 1);
1241 Py_XDECREF(v);
1242 c->c_begin = c->c_nexti;
1243 com_addoparg(c, SET_LINENO, n->n_lineno);
1244 com_addfwref(c, FOR_LOOP, &anchor);
1245 com_push(c, 1);
Thomas Wouters434d0822000-08-24 20:11:32 +00001246 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001247 c->c_loops++;
1248 com_list_iter(c, n, e, t);
1249 c->c_loops--;
1250 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1251 c->c_begin = save_begin;
1252 com_backpatch(c, anchor);
1253 com_pop(c, 2); /* FOR_LOOP has popped these */
1254}
1255
1256static void
1257com_list_if(struct compiling *c, node *n, node *e, char *t)
1258{
1259 int anchor = 0;
1260 int a = 0;
1261 /* list_iter: 'if' test [list_iter] */
1262 com_addoparg(c, SET_LINENO, n->n_lineno);
1263 com_node(c, CHILD(n, 1));
1264 com_addfwref(c, JUMP_IF_FALSE, &a);
1265 com_addbyte(c, POP_TOP);
1266 com_pop(c, 1);
1267 com_list_iter(c, n, e, t);
1268 com_addfwref(c, JUMP_FORWARD, &anchor);
1269 com_backpatch(c, a);
1270 /* We jump here with an extra entry which we now pop */
1271 com_addbyte(c, POP_TOP);
1272 com_backpatch(c, anchor);
1273}
1274
1275static void
1276com_list_iter(struct compiling *c,
1277 node *p, /* parent of list_iter node */
1278 node *e, /* element expression node */
1279 char *t /* name of result list temp local */)
1280{
1281 /* list_iter is the last child in a listmaker, list_for, or list_if */
1282 node *n = CHILD(p, NCH(p)-1);
1283 if (TYPE(n) == list_iter) {
1284 n = CHILD(n, 0);
1285 switch (TYPE(n)) {
1286 case list_for:
1287 com_list_for(c, n, e, t);
1288 break;
1289 case list_if:
1290 com_list_if(c, n, e, t);
1291 break;
1292 default:
1293 com_error(c, PyExc_SystemError,
1294 "invalid list_iter node type");
1295 }
1296 }
1297 else {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001298 com_addop_varname(c, VAR_LOAD, t);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001299 com_push(c, 1);
1300 com_node(c, e);
1301 com_addoparg(c, CALL_FUNCTION, 1);
1302 com_addbyte(c, POP_TOP);
1303 com_pop(c, 2);
1304 }
1305}
1306
1307static void
1308com_list_comprehension(struct compiling *c, node *n)
1309{
Skip Montanaro46dfa5f2000-08-22 02:43:07 +00001310 /* listmaker: test list_for */
Skip Montanaro803d6e52000-08-12 18:09:51 +00001311 char tmpname[12];
Tim Peters30814212001-02-17 05:30:26 +00001312 sprintf(tmpname, "_[%d]", ++c->c_tmpname);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001313 com_addoparg(c, BUILD_LIST, 0);
1314 com_addbyte(c, DUP_TOP); /* leave the result on the stack */
1315 com_push(c, 2);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001316 com_addop_name(c, LOAD_ATTR, "append");
1317 com_addop_varname(c, VAR_STORE, tmpname);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001318 com_pop(c, 1);
Skip Montanaro46dfa5f2000-08-22 02:43:07 +00001319 com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001320 com_addop_varname(c, VAR_DELETE, tmpname);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001321 --c->c_tmpname;
1322}
1323
1324static void
1325com_listmaker(struct compiling *c, node *n)
1326{
Skip Montanaro46dfa5f2000-08-22 02:43:07 +00001327 /* listmaker: test ( list_for | (',' test)* [','] ) */
1328 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for)
Skip Montanaro803d6e52000-08-12 18:09:51 +00001329 com_list_comprehension(c, n);
1330 else {
1331 int len = 0;
1332 int i;
1333 for (i = 0; i < NCH(n); i += 2, len++)
1334 com_node(c, CHILD(n, i));
1335 com_addoparg(c, BUILD_LIST, len);
1336 com_pop(c, len-1);
1337 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001338}
1339
1340static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001341com_dictmaker(struct compiling *c, node *n)
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001342{
1343 int i;
1344 /* dictmaker: test ':' test (',' test ':' value)* [','] */
1345 for (i = 0; i+2 < NCH(n); i += 4) {
1346 /* We must arrange things just right for STORE_SUBSCR.
1347 It wants the stack to look like (value) (dict) (key) */
1348 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001349 com_push(c, 1);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001350 com_node(c, CHILD(n, i+2)); /* value */
1351 com_addbyte(c, ROT_TWO);
1352 com_node(c, CHILD(n, i)); /* key */
1353 com_addbyte(c, STORE_SUBSCR);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001354 com_pop(c, 3);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001355 }
1356}
1357
1358static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001359com_atom(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001360{
1361 node *ch;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001362 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001363 int i;
1364 REQ(n, atom);
1365 ch = CHILD(n, 0);
1366 switch (TYPE(ch)) {
1367 case LPAR:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001368 if (TYPE(CHILD(n, 1)) == RPAR) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001369 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001370 com_push(c, 1);
1371 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001372 else
1373 com_node(c, CHILD(n, 1));
1374 break;
Skip Montanaro803d6e52000-08-12 18:09:51 +00001375 case LSQB: /* '[' [listmaker] ']' */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001376 if (TYPE(CHILD(n, 1)) == RSQB) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001377 com_addoparg(c, BUILD_LIST, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001378 com_push(c, 1);
1379 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001380 else
Skip Montanaro803d6e52000-08-12 18:09:51 +00001381 com_listmaker(c, CHILD(n, 1));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001382 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001383 case LBRACE: /* '{' [dictmaker] '}' */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001384 com_addoparg(c, BUILD_MAP, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001385 com_push(c, 1);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001386 if (TYPE(CHILD(n, 1)) == dictmaker)
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001387 com_dictmaker(c, CHILD(n, 1));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001388 break;
1389 case BACKQUOTE:
1390 com_node(c, CHILD(n, 1));
1391 com_addbyte(c, UNARY_CONVERT);
1392 break;
1393 case NUMBER:
Guido van Rossum452a9831996-09-17 14:32:04 +00001394 if ((v = parsenumber(c, STR(ch))) == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001395 i = 255;
1396 }
1397 else {
1398 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001399 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001400 }
1401 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001402 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001403 break;
1404 case STRING:
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001405 v = parsestrplus(n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001406 if (v == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001407 c->c_errors++;
1408 i = 255;
1409 }
1410 else {
1411 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001412 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001413 }
1414 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001415 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001416 break;
1417 case NAME:
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001418 com_addop_varname(c, VAR_LOAD, STR(ch));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001419 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001420 break;
1421 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001422 com_error(c, PyExc_SystemError,
1423 "com_atom: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001424 }
1425}
1426
1427static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001428com_slice(struct compiling *c, node *n, int op)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001429{
1430 if (NCH(n) == 1) {
1431 com_addbyte(c, op);
1432 }
1433 else if (NCH(n) == 2) {
1434 if (TYPE(CHILD(n, 0)) != COLON) {
1435 com_node(c, CHILD(n, 0));
1436 com_addbyte(c, op+1);
1437 }
1438 else {
1439 com_node(c, CHILD(n, 1));
1440 com_addbyte(c, op+2);
1441 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00001442 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001443 }
1444 else {
1445 com_node(c, CHILD(n, 0));
1446 com_node(c, CHILD(n, 2));
1447 com_addbyte(c, op+3);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001448 com_pop(c, 2);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001449 }
1450}
1451
Guido van Rossum635abd21997-01-06 22:56:52 +00001452static void
Thomas Wouters434d0822000-08-24 20:11:32 +00001453com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn)
1454{
1455 if (NCH(n) == 1) {
1456 com_addbyte(c, DUP_TOP);
1457 com_push(c, 1);
1458 com_addbyte(c, SLICE);
1459 com_node(c, augn);
1460 com_addbyte(c, opcode);
1461 com_pop(c, 1);
1462 com_addbyte(c, ROT_TWO);
1463 com_addbyte(c, STORE_SLICE);
1464 com_pop(c, 2);
1465 } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) {
1466 com_node(c, CHILD(n, 0));
1467 com_addoparg(c, DUP_TOPX, 2);
1468 com_push(c, 2);
1469 com_addbyte(c, SLICE+1);
1470 com_pop(c, 1);
1471 com_node(c, augn);
1472 com_addbyte(c, opcode);
1473 com_pop(c, 1);
1474 com_addbyte(c, ROT_THREE);
1475 com_addbyte(c, STORE_SLICE+1);
1476 com_pop(c, 3);
1477 } else if (NCH(n) == 2) {
1478 com_node(c, CHILD(n, 1));
1479 com_addoparg(c, DUP_TOPX, 2);
1480 com_push(c, 2);
1481 com_addbyte(c, SLICE+2);
1482 com_pop(c, 1);
1483 com_node(c, augn);
1484 com_addbyte(c, opcode);
1485 com_pop(c, 1);
1486 com_addbyte(c, ROT_THREE);
1487 com_addbyte(c, STORE_SLICE+2);
1488 com_pop(c, 3);
1489 } else {
1490 com_node(c, CHILD(n, 0));
1491 com_node(c, CHILD(n, 2));
1492 com_addoparg(c, DUP_TOPX, 3);
1493 com_push(c, 3);
1494 com_addbyte(c, SLICE+3);
1495 com_pop(c, 2);
1496 com_node(c, augn);
1497 com_addbyte(c, opcode);
1498 com_pop(c, 1);
1499 com_addbyte(c, ROT_FOUR);
1500 com_addbyte(c, STORE_SLICE+3);
1501 com_pop(c, 4);
1502 }
1503}
1504
1505static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001506com_argument(struct compiling *c, node *n, PyObject **pkeywords)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001507{
1508 node *m;
Guido van Rossum8b993a91997-01-17 21:04:03 +00001509 REQ(n, argument); /* [test '='] test; really [keyword '='] test */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001510 if (NCH(n) == 1) {
Guido van Rossum635abd21997-01-06 22:56:52 +00001511 if (*pkeywords != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001512 com_error(c, PyExc_SyntaxError,
Fred Drakedcf08e02000-08-15 15:49:44 +00001513 "non-keyword arg after keyword arg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001514 }
1515 else {
1516 com_node(c, CHILD(n, 0));
1517 }
Guido van Rossum635abd21997-01-06 22:56:52 +00001518 return;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001519 }
1520 m = n;
1521 do {
1522 m = CHILD(m, 0);
1523 } while (NCH(m) == 1);
1524 if (TYPE(m) != NAME) {
Tim Peters4e303782001-02-18 04:45:10 +00001525 /* f(lambda x: x[0] = 3) ends up getting parsed with
1526 * LHS test = lambda x: x[0], and RHS test = 3.
1527 * SF bug 132313 points out that complaining about a keyword
1528 * then is very confusing.
1529 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001530 com_error(c, PyExc_SyntaxError,
Tim Peters4e303782001-02-18 04:45:10 +00001531 TYPE(m) == lambdef ?
1532 "lambda cannot contain assignment" :
1533 "keyword can't be an expression");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001534 }
1535 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001536 PyObject *v = PyString_InternFromString(STR(m));
Guido van Rossum635abd21997-01-06 22:56:52 +00001537 if (v != NULL && *pkeywords == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001538 *pkeywords = PyDict_New();
Guido van Rossum635abd21997-01-06 22:56:52 +00001539 if (v == NULL || *pkeywords == NULL)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001540 c->c_errors++;
1541 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001542 if (PyDict_GetItem(*pkeywords, v) != NULL)
1543 com_error(c, PyExc_SyntaxError,
Guido van Rossum635abd21997-01-06 22:56:52 +00001544 "duplicate keyword argument");
1545 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001546 if (PyDict_SetItem(*pkeywords, v, v) != 0)
Guido van Rossum635abd21997-01-06 22:56:52 +00001547 c->c_errors++;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001548 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001549 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001550 Py_DECREF(v);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001551 }
1552 }
1553 com_node(c, CHILD(n, 2));
Guido van Rossumf10570b1995-07-07 22:53:21 +00001554}
1555
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001556static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001557com_call_function(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001558{
1559 if (TYPE(n) == RPAR) {
Guido van Rossumf10570b1995-07-07 22:53:21 +00001560 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001561 }
1562 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001563 PyObject *keywords = NULL;
Guido van Rossum635abd21997-01-06 22:56:52 +00001564 int i, na, nk;
Guido van Rossumca906051998-12-10 16:56:22 +00001565 int lineno = n->n_lineno;
Jeremy Hylton76901512000-03-28 23:49:17 +00001566 int star_flag = 0;
1567 int starstar_flag = 0;
1568 int opcode;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001569 REQ(n, arglist);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001570 na = 0;
1571 nk = 0;
1572 for (i = 0; i < NCH(n); i += 2) {
Guido van Rossumca906051998-12-10 16:56:22 +00001573 node *ch = CHILD(n, i);
Jeremy Hylton76901512000-03-28 23:49:17 +00001574 if (TYPE(ch) == STAR ||
1575 TYPE(ch) == DOUBLESTAR)
1576 break;
Guido van Rossumca906051998-12-10 16:56:22 +00001577 if (ch->n_lineno != lineno) {
1578 lineno = ch->n_lineno;
1579 com_addoparg(c, SET_LINENO, lineno);
1580 }
1581 com_argument(c, ch, &keywords);
Guido van Rossum635abd21997-01-06 22:56:52 +00001582 if (keywords == NULL)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001583 na++;
1584 else
1585 nk++;
1586 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001587 Py_XDECREF(keywords);
Jeremy Hylton76901512000-03-28 23:49:17 +00001588 while (i < NCH(n)) {
1589 node *tok = CHILD(n, i);
1590 node *ch = CHILD(n, i+1);
1591 i += 3;
1592 switch (TYPE(tok)) {
1593 case STAR: star_flag = 1; break;
1594 case DOUBLESTAR: starstar_flag = 1; break;
1595 }
1596 com_node(c, ch);
1597 }
Guido van Rossumf10570b1995-07-07 22:53:21 +00001598 if (na > 255 || nk > 255) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001599 com_error(c, PyExc_SyntaxError,
1600 "more than 255 arguments");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001601 }
Jeremy Hylton76901512000-03-28 23:49:17 +00001602 if (star_flag || starstar_flag)
Jeremy Hyltone4fb9582000-03-29 00:10:44 +00001603 opcode = CALL_FUNCTION_VAR - 1 +
Jeremy Hylton76901512000-03-28 23:49:17 +00001604 star_flag + (starstar_flag << 1);
1605 else
1606 opcode = CALL_FUNCTION;
1607 com_addoparg(c, opcode, na | (nk << 8));
1608 com_pop(c, na + 2*nk + star_flag + starstar_flag);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001609 }
1610}
1611
1612static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001613com_select_member(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001614{
1615 com_addopname(c, LOAD_ATTR, n);
1616}
1617
1618static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001619com_sliceobj(struct compiling *c, node *n)
Guido van Rossum8861b741996-07-30 16:49:37 +00001620{
1621 int i=0;
1622 int ns=2; /* number of slice arguments */
Guido van Rossum8861b741996-07-30 16:49:37 +00001623 node *ch;
1624
1625 /* first argument */
1626 if (TYPE(CHILD(n,i)) == COLON) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001627 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001628 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001629 i++;
1630 }
1631 else {
1632 com_node(c, CHILD(n,i));
1633 i++;
1634 REQ(CHILD(n,i),COLON);
1635 i++;
1636 }
1637 /* second argument */
1638 if (i < NCH(n) && TYPE(CHILD(n,i)) == test) {
1639 com_node(c, CHILD(n,i));
1640 i++;
1641 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00001642 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001643 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001644 com_push(c, 1);
1645 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001646 /* remaining arguments */
1647 for (; i < NCH(n); i++) {
1648 ns++;
1649 ch=CHILD(n,i);
1650 REQ(ch, sliceop);
1651 if (NCH(ch) == 1) {
1652 /* right argument of ':' missing */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001653 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001654 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001655 }
1656 else
1657 com_node(c, CHILD(ch,1));
1658 }
1659 com_addoparg(c, BUILD_SLICE, ns);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001660 com_pop(c, 1 + (ns == 3));
Guido van Rossum8861b741996-07-30 16:49:37 +00001661}
1662
1663static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001664com_subscript(struct compiling *c, node *n)
Guido van Rossum8861b741996-07-30 16:49:37 +00001665{
1666 node *ch;
1667 REQ(n, subscript);
1668 ch = CHILD(n,0);
1669 /* check for rubber index */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001670 if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
Guido van Rossume449af71996-10-11 16:25:41 +00001671 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001672 com_push(c, 1);
1673 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001674 else {
1675 /* check for slice */
1676 if ((TYPE(ch) == COLON || NCH(n) > 1))
1677 com_sliceobj(c, n);
1678 else {
1679 REQ(ch, test);
1680 com_node(c, ch);
1681 }
1682 }
1683}
1684
1685static void
Thomas Wouters434d0822000-08-24 20:11:32 +00001686com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn)
Guido van Rossum8861b741996-07-30 16:49:37 +00001687{
1688 int i, op;
1689 REQ(n, subscriptlist);
1690 /* Check to make backward compatible slice behavior for '[i:j]' */
1691 if (NCH(n) == 1) {
1692 node *sub = CHILD(n, 0); /* subscript */
Thomas Wouterse8643c42000-08-05 21:37:50 +00001693 /* 'Basic' slice, should have exactly one colon. */
1694 if ((TYPE(CHILD(sub, 0)) == COLON
1695 || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
1696 && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
1697 {
Thomas Wouters434d0822000-08-24 20:11:32 +00001698 switch (assigning) {
1699 case OP_DELETE:
1700 op = DELETE_SLICE;
1701 break;
1702 case OP_ASSIGN:
1703 op = STORE_SLICE;
1704 break;
1705 case OP_APPLY:
Guido van Rossum8861b741996-07-30 16:49:37 +00001706 op = SLICE;
Thomas Wouters434d0822000-08-24 20:11:32 +00001707 break;
1708 default:
1709 com_augassign_slice(c, sub, assigning, augn);
1710 return;
1711 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001712 com_slice(c, sub, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001713 if (op == STORE_SLICE)
1714 com_pop(c, 2);
1715 else if (op == DELETE_SLICE)
1716 com_pop(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001717 return;
1718 }
1719 }
1720 /* Else normal subscriptlist. Compile each subscript. */
1721 for (i = 0; i < NCH(n); i += 2)
1722 com_subscript(c, CHILD(n, i));
1723 /* Put multiple subscripts into a tuple */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001724 if (NCH(n) > 1) {
1725 i = (NCH(n)+1) / 2;
1726 com_addoparg(c, BUILD_TUPLE, i);
1727 com_pop(c, i-1);
1728 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001729 switch (assigning) {
1730 case OP_DELETE:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001731 op = DELETE_SUBSCR;
1732 i = 2;
Thomas Wouters434d0822000-08-24 20:11:32 +00001733 break;
1734 default:
1735 case OP_ASSIGN:
1736 op = STORE_SUBSCR;
1737 i = 3;
1738 break;
1739 case OP_APPLY:
1740 op = BINARY_SUBSCR;
1741 i = 1;
1742 break;
1743 }
1744 if (assigning > OP_APPLY) {
1745 com_addoparg(c, DUP_TOPX, 2);
1746 com_push(c, 2);
1747 com_addbyte(c, BINARY_SUBSCR);
1748 com_pop(c, 1);
1749 com_node(c, augn);
1750 com_addbyte(c, assigning);
1751 com_pop(c, 1);
1752 com_addbyte(c, ROT_THREE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001753 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001754 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001755 com_pop(c, i);
Guido van Rossum8861b741996-07-30 16:49:37 +00001756}
1757
1758static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001759com_apply_trailer(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001760{
1761 REQ(n, trailer);
1762 switch (TYPE(CHILD(n, 0))) {
1763 case LPAR:
1764 com_call_function(c, CHILD(n, 1));
1765 break;
1766 case DOT:
1767 com_select_member(c, CHILD(n, 1));
1768 break;
1769 case LSQB:
Thomas Wouters434d0822000-08-24 20:11:32 +00001770 com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001771 break;
1772 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001773 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001774 "com_apply_trailer: unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001775 }
1776}
1777
1778static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001779com_power(struct compiling *c, node *n)
Guido van Rossum50564e81996-01-12 01:13:16 +00001780{
1781 int i;
1782 REQ(n, power);
1783 com_atom(c, CHILD(n, 0));
1784 for (i = 1; i < NCH(n); i++) {
1785 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
1786 com_factor(c, CHILD(n, i+1));
1787 com_addbyte(c, BINARY_POWER);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001788 com_pop(c, 1);
Guido van Rossum50564e81996-01-12 01:13:16 +00001789 break;
1790 }
1791 else
1792 com_apply_trailer(c, CHILD(n, i));
1793 }
1794}
1795
1796static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001797com_factor(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001798{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001799 REQ(n, factor);
1800 if (TYPE(CHILD(n, 0)) == PLUS) {
1801 com_factor(c, CHILD(n, 1));
1802 com_addbyte(c, UNARY_POSITIVE);
1803 }
1804 else if (TYPE(CHILD(n, 0)) == MINUS) {
1805 com_factor(c, CHILD(n, 1));
1806 com_addbyte(c, UNARY_NEGATIVE);
1807 }
Guido van Rossum7928cd71991-10-24 14:59:31 +00001808 else if (TYPE(CHILD(n, 0)) == TILDE) {
1809 com_factor(c, CHILD(n, 1));
1810 com_addbyte(c, UNARY_INVERT);
1811 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001812 else {
Guido van Rossum50564e81996-01-12 01:13:16 +00001813 com_power(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001814 }
1815}
1816
1817static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001818com_term(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001819{
1820 int i;
1821 int op;
1822 REQ(n, term);
1823 com_factor(c, CHILD(n, 0));
1824 for (i = 2; i < NCH(n); i += 2) {
1825 com_factor(c, CHILD(n, i));
1826 switch (TYPE(CHILD(n, i-1))) {
1827 case STAR:
1828 op = BINARY_MULTIPLY;
1829 break;
1830 case SLASH:
1831 op = BINARY_DIVIDE;
1832 break;
1833 case PERCENT:
1834 op = BINARY_MODULO;
1835 break;
1836 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001837 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001838 "com_term: operator not *, / or %");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001839 op = 255;
1840 }
1841 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001842 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001843 }
1844}
1845
1846static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001847com_arith_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001848{
1849 int i;
1850 int op;
1851 REQ(n, arith_expr);
1852 com_term(c, CHILD(n, 0));
1853 for (i = 2; i < NCH(n); i += 2) {
1854 com_term(c, CHILD(n, i));
1855 switch (TYPE(CHILD(n, i-1))) {
1856 case PLUS:
1857 op = BINARY_ADD;
1858 break;
1859 case MINUS:
1860 op = BINARY_SUBTRACT;
1861 break;
1862 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001863 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001864 "com_arith_expr: operator not + or -");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001865 op = 255;
1866 }
1867 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001868 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001869 }
1870}
1871
1872static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001873com_shift_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001874{
1875 int i;
1876 int op;
1877 REQ(n, shift_expr);
1878 com_arith_expr(c, CHILD(n, 0));
1879 for (i = 2; i < NCH(n); i += 2) {
1880 com_arith_expr(c, CHILD(n, i));
1881 switch (TYPE(CHILD(n, i-1))) {
1882 case LEFTSHIFT:
1883 op = BINARY_LSHIFT;
1884 break;
1885 case RIGHTSHIFT:
1886 op = BINARY_RSHIFT;
1887 break;
1888 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001889 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001890 "com_shift_expr: operator not << or >>");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001891 op = 255;
1892 }
1893 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001894 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001895 }
1896}
1897
1898static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001899com_and_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001900{
1901 int i;
1902 int op;
1903 REQ(n, and_expr);
1904 com_shift_expr(c, CHILD(n, 0));
1905 for (i = 2; i < NCH(n); i += 2) {
1906 com_shift_expr(c, CHILD(n, i));
1907 if (TYPE(CHILD(n, i-1)) == AMPER) {
1908 op = BINARY_AND;
1909 }
1910 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001911 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001912 "com_and_expr: operator not &");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001913 op = 255;
1914 }
1915 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001916 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001917 }
1918}
1919
1920static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001921com_xor_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001922{
1923 int i;
1924 int op;
1925 REQ(n, xor_expr);
1926 com_and_expr(c, CHILD(n, 0));
1927 for (i = 2; i < NCH(n); i += 2) {
1928 com_and_expr(c, CHILD(n, i));
1929 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
1930 op = BINARY_XOR;
1931 }
1932 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001933 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001934 "com_xor_expr: operator not ^");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001935 op = 255;
1936 }
1937 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001938 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001939 }
1940}
1941
1942static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001943com_expr(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001944{
1945 int i;
1946 int op;
1947 REQ(n, expr);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001948 com_xor_expr(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001949 for (i = 2; i < NCH(n); i += 2) {
Guido van Rossum7928cd71991-10-24 14:59:31 +00001950 com_xor_expr(c, CHILD(n, i));
1951 if (TYPE(CHILD(n, i-1)) == VBAR) {
1952 op = BINARY_OR;
1953 }
1954 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001955 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001956 "com_expr: expr operator not |");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001957 op = 255;
1958 }
1959 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001960 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001961 }
1962}
1963
1964static enum cmp_op
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001965cmp_type(node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001966{
1967 REQ(n, comp_op);
Guido van Rossum01cfd441991-10-20 20:12:38 +00001968 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001969 | 'in' | 'not' 'in' | 'is' | 'is' not' */
1970 if (NCH(n) == 1) {
1971 n = CHILD(n, 0);
1972 switch (TYPE(n)) {
1973 case LESS: return LT;
1974 case GREATER: return GT;
Guido van Rossum01cfd441991-10-20 20:12:38 +00001975 case EQEQUAL: /* == */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001976 case EQUAL: return EQ;
Guido van Rossum01cfd441991-10-20 20:12:38 +00001977 case LESSEQUAL: return LE;
1978 case GREATEREQUAL: return GE;
1979 case NOTEQUAL: return NE; /* <> or != */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001980 case NAME: if (strcmp(STR(n), "in") == 0) return IN;
1981 if (strcmp(STR(n), "is") == 0) return IS;
1982 }
1983 }
1984 else if (NCH(n) == 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001985 switch (TYPE(CHILD(n, 0))) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001986 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1987 return NOT_IN;
1988 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1989 return IS_NOT;
1990 }
1991 }
1992 return BAD;
1993}
1994
1995static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001996com_comparison(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001997{
1998 int i;
1999 enum cmp_op op;
2000 int anchor;
2001 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
2002 com_expr(c, CHILD(n, 0));
2003 if (NCH(n) == 1)
2004 return;
2005
2006 /****************************************************************
2007 The following code is generated for all but the last
2008 comparison in a chain:
2009
2010 label: on stack: opcode: jump to:
2011
2012 a <code to load b>
2013 a, b DUP_TOP
2014 a, b, b ROT_THREE
2015 b, a, b COMPARE_OP
2016 b, 0-or-1 JUMP_IF_FALSE L1
2017 b, 1 POP_TOP
2018 b
2019
2020 We are now ready to repeat this sequence for the next
2021 comparison in the chain.
2022
2023 For the last we generate:
2024
2025 b <code to load c>
2026 b, c COMPARE_OP
2027 0-or-1
2028
2029 If there were any jumps to L1 (i.e., there was more than one
2030 comparison), we generate:
2031
2032 0-or-1 JUMP_FORWARD L2
2033 L1: b, 0 ROT_TWO
2034 0, b POP_TOP
2035 0
Guido van Rossum8b993a91997-01-17 21:04:03 +00002036 L2: 0-or-1
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002037 ****************************************************************/
2038
2039 anchor = 0;
2040
2041 for (i = 2; i < NCH(n); i += 2) {
2042 com_expr(c, CHILD(n, i));
2043 if (i+2 < NCH(n)) {
2044 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002045 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002046 com_addbyte(c, ROT_THREE);
2047 }
2048 op = cmp_type(CHILD(n, i-1));
2049 if (op == BAD) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002050 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002051 "com_comparison: unknown comparison op");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002052 }
2053 com_addoparg(c, COMPARE_OP, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002054 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002055 if (i+2 < NCH(n)) {
2056 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2057 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002058 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002059 }
2060 }
2061
2062 if (anchor) {
2063 int anchor2 = 0;
2064 com_addfwref(c, JUMP_FORWARD, &anchor2);
2065 com_backpatch(c, anchor);
2066 com_addbyte(c, ROT_TWO);
2067 com_addbyte(c, POP_TOP);
2068 com_backpatch(c, anchor2);
2069 }
2070}
2071
2072static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002073com_not_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002074{
2075 REQ(n, not_test); /* 'not' not_test | comparison */
2076 if (NCH(n) == 1) {
2077 com_comparison(c, CHILD(n, 0));
2078 }
2079 else {
2080 com_not_test(c, CHILD(n, 1));
2081 com_addbyte(c, UNARY_NOT);
2082 }
2083}
2084
2085static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002086com_and_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002087{
2088 int i;
2089 int anchor;
2090 REQ(n, and_test); /* not_test ('and' not_test)* */
2091 anchor = 0;
2092 i = 0;
2093 for (;;) {
2094 com_not_test(c, CHILD(n, i));
2095 if ((i += 2) >= NCH(n))
2096 break;
2097 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2098 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002099 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002100 }
2101 if (anchor)
2102 com_backpatch(c, anchor);
2103}
2104
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002105static int
2106com_make_closure(struct compiling *c, PyCodeObject *co)
2107{
2108 int i, free = PyTuple_GET_SIZE(co->co_freevars);
2109 if (free == 0)
2110 return 0;
2111 for (i = 0; i < free; ++i) {
2112 /* Bypass com_addop_varname because it will generate
2113 LOAD_DEREF but LOAD_CLOSURE is needed.
2114 */
2115 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
2116 int arg, reftype;
2117
2118 /* Special case: If a class contains a method with a
2119 free variable that has the same name as a method,
2120 the name will be considered free *and* local in the
2121 class. It should be handled by the closure, as
2122 well as by the normal name loookup logic.
2123 */
2124 reftype = get_ref_type(c, PyString_AS_STRING(name));
2125 if (reftype == CELL)
2126 arg = com_lookup_arg(c->c_cellvars, name);
2127 else /* (reftype == FREE) */
2128 arg = com_lookup_arg(c->c_freevars, name);
2129 if (arg == -1) {
2130 fprintf(stderr, "lookup %s in %s %d %d\n",
Jeremy Hylton483638c2001-02-01 20:20:45 +00002131 PyObject_REPR(name), c->c_name, reftype, arg);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002132 Py_FatalError("com_make_closure()");
2133 }
2134 com_addoparg(c, LOAD_CLOSURE, arg);
2135
2136 }
2137 com_push(c, free);
2138 return 1;
2139}
2140
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002141static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002142com_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002143{
Guido van Rossum8b993a91997-01-17 21:04:03 +00002144 REQ(n, test); /* and_test ('or' and_test)* | lambdef */
Guido van Rossum57531fe1993-11-30 14:57:42 +00002145 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002146 PyObject *co;
2147 int i, closure;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002148 int ndefs = com_argdefs(c, CHILD(n, 0));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00002149 symtable_enter_scope(c->c_symtable, "lambda", lambdef,
2150 n->n_lineno);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002151 co = (PyObject *) icompile(CHILD(n, 0), c);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00002152 if (co == NULL) {
2153 c->c_errors++;
2154 return;
2155 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002156 symtable_exit_scope(c->c_symtable);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002157 if (co == NULL) {
Guido van Rossum57531fe1993-11-30 14:57:42 +00002158 c->c_errors++;
2159 i = 255;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002160 closure = 0;
2161 } else {
2162 i = com_addconst(c, co);
2163 Py_DECREF(co);
2164 closure = com_make_closure(c, (PyCodeObject *)co);
Guido van Rossum57531fe1993-11-30 14:57:42 +00002165 }
2166 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002167 com_push(c, 1);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002168 if (closure)
2169 com_addoparg(c, MAKE_CLOSURE, ndefs);
2170 else
2171 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002172 com_pop(c, ndefs);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002173 }
Guido van Rossum57531fe1993-11-30 14:57:42 +00002174 else {
2175 int anchor = 0;
2176 int i = 0;
2177 for (;;) {
2178 com_and_test(c, CHILD(n, i));
2179 if ((i += 2) >= NCH(n))
2180 break;
2181 com_addfwref(c, JUMP_IF_TRUE, &anchor);
2182 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002183 com_pop(c, 1);
Guido van Rossum57531fe1993-11-30 14:57:42 +00002184 }
2185 if (anchor)
2186 com_backpatch(c, anchor);
2187 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002188}
2189
2190static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002191com_list(struct compiling *c, node *n, int toplevel)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002192{
2193 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
Guido van Rossum288a60f1991-12-16 13:05:10 +00002194 if (NCH(n) == 1 && !toplevel) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002195 com_node(c, CHILD(n, 0));
2196 }
2197 else {
2198 int i;
2199 int len;
2200 len = (NCH(n) + 1) / 2;
2201 for (i = 0; i < NCH(n); i += 2)
2202 com_node(c, CHILD(n, i));
2203 com_addoparg(c, BUILD_TUPLE, len);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002204 com_pop(c, len-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002205 }
2206}
2207
2208
2209/* Begin of assignment compilation */
2210
Thomas Wouters434d0822000-08-24 20:11:32 +00002211
2212static void
2213com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn)
2214{
2215 com_addbyte(c, DUP_TOP);
2216 com_push(c, 1);
2217 com_addopname(c, LOAD_ATTR, n);
Thomas Wouters434d0822000-08-24 20:11:32 +00002218 com_node(c, augn);
2219 com_addbyte(c, opcode);
2220 com_pop(c, 1);
2221 com_addbyte(c, ROT_TWO);
2222 com_addopname(c, STORE_ATTR, n);
2223 com_pop(c, 2);
2224}
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002225
2226static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002227com_assign_attr(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002228{
2229 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002230 com_pop(c, assigning ? 2 : 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002231}
2232
2233static void
Thomas Wouters434d0822000-08-24 20:11:32 +00002234com_assign_trailer(struct compiling *c, node *n, int assigning, node *augn)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002235{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002236 REQ(n, trailer);
2237 switch (TYPE(CHILD(n, 0))) {
2238 case LPAR: /* '(' [exprlist] ')' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002239 com_error(c, PyExc_SyntaxError,
2240 "can't assign to function call");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002241 break;
2242 case DOT: /* '.' NAME */
Thomas Wouters434d0822000-08-24 20:11:32 +00002243 if (assigning > OP_APPLY)
2244 com_augassign_attr(c, CHILD(n, 1), assigning, augn);
2245 else
2246 com_assign_attr(c, CHILD(n, 1), assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002247 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002248 case LSQB: /* '[' subscriptlist ']' */
Thomas Wouters434d0822000-08-24 20:11:32 +00002249 com_subscriptlist(c, CHILD(n, 1), assigning, augn);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002250 break;
2251 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002252 com_error(c, PyExc_SystemError, "unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002253 }
2254}
2255
2256static void
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002257com_assign_sequence(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002258{
2259 int i;
Thomas Woutersb59290f2000-08-25 05:41:11 +00002260 if (TYPE(n) != testlist && TYPE(n) != listmaker)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002261 REQ(n, exprlist);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002262 if (assigning) {
2263 i = (NCH(n)+1)/2;
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002264 com_addoparg(c, UNPACK_SEQUENCE, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002265 com_push(c, i-1);
2266 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002267 for (i = 0; i < NCH(n); i += 2)
Thomas Wouters434d0822000-08-24 20:11:32 +00002268 com_assign(c, CHILD(n, i), assigning, NULL);
2269}
2270
2271static void
2272com_augassign_name(struct compiling *c, node *n, int opcode, node *augn)
2273{
2274 REQ(n, NAME);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002275 com_addop_varname(c, VAR_LOAD, STR(n));
Thomas Wouters434d0822000-08-24 20:11:32 +00002276 com_push(c, 1);
2277 com_node(c, augn);
2278 com_addbyte(c, opcode);
2279 com_pop(c, 1);
2280 com_assign_name(c, n, OP_ASSIGN);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002281}
2282
2283static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002284com_assign_name(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002285{
2286 REQ(n, NAME);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002287 com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002288 if (assigning)
2289 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002290}
2291
2292static void
Thomas Wouters434d0822000-08-24 20:11:32 +00002293com_assign(struct compiling *c, node *n, int assigning, node *augn)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002294{
2295 /* Loop to avoid trivial recursion */
2296 for (;;) {
2297 switch (TYPE(n)) {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002298
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002299 case exprlist:
2300 case testlist:
2301 if (NCH(n) > 1) {
Thomas Wouters434d0822000-08-24 20:11:32 +00002302 if (assigning > OP_APPLY) {
2303 com_error(c, PyExc_SyntaxError,
2304 "augmented assign to tuple not possible");
2305 return;
2306 }
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002307 com_assign_sequence(c, n, assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002308 return;
2309 }
2310 n = CHILD(n, 0);
2311 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002312
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002313 case test:
2314 case and_test:
2315 case not_test:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002316 case comparison:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002317 case expr:
Guido van Rossum7928cd71991-10-24 14:59:31 +00002318 case xor_expr:
2319 case and_expr:
2320 case shift_expr:
2321 case arith_expr:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002322 case term:
Guido van Rossum50564e81996-01-12 01:13:16 +00002323 case factor:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002324 if (NCH(n) > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002325 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002326 "can't assign to operator");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002327 return;
2328 }
2329 n = CHILD(n, 0);
2330 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002331
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00002332 case power: /* atom trailer* ('**' power)*
2333 ('+'|'-'|'~') factor | atom trailer* */
Guido van Rossum50564e81996-01-12 01:13:16 +00002334 if (TYPE(CHILD(n, 0)) != atom) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002335 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002336 "can't assign to operator");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002337 return;
2338 }
Guido van Rossum50564e81996-01-12 01:13:16 +00002339 if (NCH(n) > 1) { /* trailer or exponent present */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002340 int i;
2341 com_node(c, CHILD(n, 0));
2342 for (i = 1; i+1 < NCH(n); i++) {
Guido van Rossum50564e81996-01-12 01:13:16 +00002343 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002344 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002345 "can't assign to operator");
Guido van Rossum50564e81996-01-12 01:13:16 +00002346 return;
2347 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002348 com_apply_trailer(c, CHILD(n, i));
2349 } /* NB i is still alive */
2350 com_assign_trailer(c,
Thomas Wouters434d0822000-08-24 20:11:32 +00002351 CHILD(n, i), assigning, augn);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002352 return;
2353 }
2354 n = CHILD(n, 0);
2355 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002356
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002357 case atom:
2358 switch (TYPE(CHILD(n, 0))) {
2359 case LPAR:
2360 n = CHILD(n, 1);
2361 if (TYPE(n) == RPAR) {
2362 /* XXX Should allow () = () ??? */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002363 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002364 "can't assign to ()");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002365 return;
2366 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002367 if (assigning > OP_APPLY) {
2368 com_error(c, PyExc_SyntaxError,
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002369 "augmented assign to tuple not possible");
Thomas Wouters434d0822000-08-24 20:11:32 +00002370 return;
2371 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002372 break;
2373 case LSQB:
2374 n = CHILD(n, 1);
2375 if (TYPE(n) == RSQB) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002376 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002377 "can't assign to []");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002378 return;
2379 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002380 if (assigning > OP_APPLY) {
2381 com_error(c, PyExc_SyntaxError,
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002382 "augmented assign to list not possible");
2383 return;
2384 }
2385 if (NCH(n) > 1
2386 && TYPE(CHILD(n, 1)) == list_for) {
2387 com_error(c, PyExc_SyntaxError,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002388 "can't assign to list comprehension");
Thomas Wouters434d0822000-08-24 20:11:32 +00002389 return;
2390 }
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002391 com_assign_sequence(c, n, assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002392 return;
2393 case NAME:
Thomas Wouters434d0822000-08-24 20:11:32 +00002394 if (assigning > OP_APPLY)
2395 com_augassign_name(c, CHILD(n, 0),
2396 assigning, augn);
2397 else
2398 com_assign_name(c, CHILD(n, 0),
2399 assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002400 return;
2401 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002402 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002403 "can't assign to literal");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002404 return;
2405 }
2406 break;
Guido van Rossum15cc9a01996-08-08 18:51:04 +00002407
2408 case lambdef:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002409 com_error(c, PyExc_SyntaxError,
2410 "can't assign to lambda");
Guido van Rossum15cc9a01996-08-08 18:51:04 +00002411 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002412
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002413 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002414 com_error(c, PyExc_SystemError,
2415 "com_assign: bad node");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002416 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002417
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002418 }
2419 }
2420}
Guido van Rossum7c531111997-03-11 18:42:21 +00002421
Thomas Wouters434d0822000-08-24 20:11:32 +00002422static void
2423com_augassign(struct compiling *c, node *n)
2424{
2425 int opcode;
2426
2427 switch (STR(CHILD(CHILD(n, 1), 0))[0]) {
2428 case '+': opcode = INPLACE_ADD; break;
2429 case '-': opcode = INPLACE_SUBTRACT; break;
2430 case '/': opcode = INPLACE_DIVIDE; break;
2431 case '%': opcode = INPLACE_MODULO; break;
2432 case '<': opcode = INPLACE_LSHIFT; break;
2433 case '>': opcode = INPLACE_RSHIFT; break;
2434 case '&': opcode = INPLACE_AND; break;
2435 case '^': opcode = INPLACE_XOR; break;
2436 case '|': opcode = INPLACE_OR; break;
2437 case '*':
2438 if (STR(CHILD(CHILD(n, 1), 0))[1] == '*')
2439 opcode = INPLACE_POWER;
2440 else
2441 opcode = INPLACE_MULTIPLY;
2442 break;
2443 default:
2444 com_error(c, PyExc_SystemError, "com_augassign: bad operator");
2445 return;
2446 }
2447 com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2));
2448}
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002449
2450static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002451com_expr_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002452{
Thomas Wouters434d0822000-08-24 20:11:32 +00002453 REQ(n, expr_stmt);
2454 /* testlist (('=' testlist)* | augassign testlist) */
Guido van Rossum8b993a91997-01-17 21:04:03 +00002455 /* Forget it if we have just a doc string here */
Guido van Rossum5f5e8171997-04-06 03:41:40 +00002456 if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
Guido van Rossum8b993a91997-01-17 21:04:03 +00002457 return;
Thomas Wouters434d0822000-08-24 20:11:32 +00002458 if (NCH(n) == 1) {
2459 com_node(c, CHILD(n, NCH(n)-1));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002460 if (c->c_interactive)
2461 com_addbyte(c, PRINT_EXPR);
2462 else
2463 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002464 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002465 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002466 else if (TYPE(CHILD(n,1)) == augassign)
2467 com_augassign(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002468 else {
2469 int i;
Thomas Wouters434d0822000-08-24 20:11:32 +00002470 com_node(c, CHILD(n, NCH(n)-1));
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002471 for (i = 0; i < NCH(n)-2; i+=2) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00002472 if (i+2 < NCH(n)-2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002473 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002474 com_push(c, 1);
2475 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002476 com_assign(c, CHILD(n, i), OP_ASSIGN, NULL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002477 }
2478 }
2479}
2480
2481static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002482com_assert_stmt(struct compiling *c, node *n)
Guido van Rossum228d7f31997-04-02 05:24:36 +00002483{
2484 int a = 0, b = 0;
2485 int i;
2486 REQ(n, assert_stmt); /* 'assert' test [',' test] */
2487 /* Generate code like for
2488
2489 if __debug__:
2490 if not <test>:
2491 raise AssertionError [, <message>]
2492
2493 where <message> is the second test, if present.
2494 */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002495
Guido van Rossum228d7f31997-04-02 05:24:36 +00002496 if (Py_OptimizeFlag)
2497 return;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002498 com_addop_name(c, LOAD_GLOBAL, "__debug__");
Guido van Rossum228d7f31997-04-02 05:24:36 +00002499 com_push(c, 1);
2500 com_addfwref(c, JUMP_IF_FALSE, &a);
2501 com_addbyte(c, POP_TOP);
2502 com_pop(c, 1);
2503 com_node(c, CHILD(n, 1));
2504 com_addfwref(c, JUMP_IF_TRUE, &b);
2505 com_addbyte(c, POP_TOP);
2506 com_pop(c, 1);
2507 /* Raise that exception! */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002508 com_addop_name(c, LOAD_GLOBAL, "AssertionError");
Guido van Rossum228d7f31997-04-02 05:24:36 +00002509 com_push(c, 1);
2510 i = NCH(n)/2; /* Either 2 or 4 */
2511 if (i > 1)
2512 com_node(c, CHILD(n, 3));
2513 com_addoparg(c, RAISE_VARARGS, i);
2514 com_pop(c, i);
2515 /* The interpreter does not fall through */
2516 /* All jumps converge here */
2517 com_backpatch(c, a);
2518 com_backpatch(c, b);
2519 com_addbyte(c, POP_TOP);
2520}
2521
2522static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002523com_print_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002524{
Barry Warsaw29c574e2000-08-21 15:38:56 +00002525 int i = 1;
2526 node* stream = NULL;
2527
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002528 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002529
2530 /* are we using the extended print form? */
2531 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2532 stream = CHILD(n, 2);
Barry Warsaw24703a02000-08-21 17:07:20 +00002533 com_node(c, stream);
2534 /* stack: [...] => [... stream] */
2535 com_push(c, 1);
Barry Warsaw29c574e2000-08-21 15:38:56 +00002536 if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA)
2537 i = 4;
2538 else
2539 i = 3;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002540 }
Barry Warsaw29c574e2000-08-21 15:38:56 +00002541 for (; i < NCH(n); i += 2) {
2542 if (stream != NULL) {
Barry Warsaw24703a02000-08-21 17:07:20 +00002543 com_addbyte(c, DUP_TOP);
2544 /* stack: [stream] => [stream stream] */
2545 com_push(c, 1);
Barry Warsaw29c574e2000-08-21 15:38:56 +00002546 com_node(c, CHILD(n, i));
Barry Warsaw24703a02000-08-21 17:07:20 +00002547 /* stack: [stream stream] => [stream stream obj] */
2548 com_addbyte(c, ROT_TWO);
2549 /* stack: [stream stream obj] => [stream obj stream] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002550 com_addbyte(c, PRINT_ITEM_TO);
Barry Warsaw24703a02000-08-21 17:07:20 +00002551 /* stack: [stream obj stream] => [stream] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002552 com_pop(c, 2);
2553 }
2554 else {
Barry Warsaw29c574e2000-08-21 15:38:56 +00002555 com_node(c, CHILD(n, i));
Barry Warsaw24703a02000-08-21 17:07:20 +00002556 /* stack: [...] => [... obj] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002557 com_addbyte(c, PRINT_ITEM);
2558 com_pop(c, 1);
2559 }
2560 }
2561 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
Barry Warsaw24703a02000-08-21 17:07:20 +00002562 if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) {
Barry Warsaw29c574e2000-08-21 15:38:56 +00002563 if (stream != NULL) {
Barry Warsaw24703a02000-08-21 17:07:20 +00002564 /* must pop the extra stream object off the stack */
2565 com_addbyte(c, POP_TOP);
2566 /* stack: [... stream] => [...] */
2567 com_pop(c, 1);
2568 }
2569 }
2570 else {
2571 if (stream != NULL) {
2572 /* this consumes the last stream object on stack */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002573 com_addbyte(c, PRINT_NEWLINE_TO);
Barry Warsaw24703a02000-08-21 17:07:20 +00002574 /* stack: [... stream] => [...] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002575 com_pop(c, 1);
2576 }
2577 else
2578 com_addbyte(c, PRINT_NEWLINE);
2579 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002580}
2581
2582static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002583com_return_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002584{
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002585 REQ(n, return_stmt); /* 'return' [testlist] */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002586 if (!c->c_infunction) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002587 com_error(c, PyExc_SyntaxError, "'return' outside function");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002588 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002589 if (NCH(n) < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002590 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002591 com_push(c, 1);
2592 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002593 else
2594 com_node(c, CHILD(n, 1));
2595 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002596 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002597}
2598
2599static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002600com_raise_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002601{
Guido van Rossum8b993a91997-01-17 21:04:03 +00002602 int i;
Guido van Rossumd295f121998-04-09 21:39:57 +00002603 REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */
2604 if (NCH(n) > 1) {
2605 com_node(c, CHILD(n, 1));
2606 if (NCH(n) > 3) {
2607 com_node(c, CHILD(n, 3));
2608 if (NCH(n) > 5)
2609 com_node(c, CHILD(n, 5));
2610 }
Guido van Rossumf10570b1995-07-07 22:53:21 +00002611 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002612 i = NCH(n)/2;
2613 com_addoparg(c, RAISE_VARARGS, i);
2614 com_pop(c, i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002615}
2616
2617static void
Thomas Wouters52152252000-08-17 22:55:00 +00002618com_from_import(struct compiling *c, node *n)
2619{
2620 com_addopname(c, IMPORT_FROM, CHILD(n, 0));
2621 com_push(c, 1);
2622 if (NCH(n) > 1) {
2623 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2624 com_error(c, PyExc_SyntaxError, "invalid syntax");
2625 return;
2626 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002627 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2)));
Thomas Wouters52152252000-08-17 22:55:00 +00002628 } else
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002629 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
Thomas Wouters52152252000-08-17 22:55:00 +00002630 com_pop(c, 1);
2631}
2632
2633static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002634com_import_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002635{
2636 int i;
2637 REQ(n, import_stmt);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002638 /* 'import' dotted_name (',' dotted_name)* |
2639 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002640 if (STR(CHILD(n, 0))[0] == 'f') {
Guido van Rossum83fb0732000-11-27 22:22:36 +00002641 PyObject *tup;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002642 /* 'from' dotted_name 'import' ... */
2643 REQ(CHILD(n, 1), dotted_name);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002644
2645 if (TYPE(CHILD(n, 3)) == STAR) {
2646 tup = Py_BuildValue("(s)", "*");
2647 } else {
2648 tup = PyTuple_New((NCH(n) - 2)/2);
2649 for (i = 3; i < NCH(n); i += 2) {
2650 PyTuple_SET_ITEM(tup, (i-3)/2,
2651 PyString_FromString(STR(
2652 CHILD(CHILD(n, i), 0))));
2653 }
2654 }
2655 com_addoparg(c, LOAD_CONST, com_addconst(c, tup));
Guido van Rossum83fb0732000-11-27 22:22:36 +00002656 Py_DECREF(tup);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002657 com_push(c, 1);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002658 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
Thomas Wouters52152252000-08-17 22:55:00 +00002659 if (TYPE(CHILD(n, 3)) == STAR)
2660 com_addbyte(c, IMPORT_STAR);
2661 else {
2662 for (i = 3; i < NCH(n); i += 2)
2663 com_from_import(c, CHILD(n, i));
2664 com_addbyte(c, POP_TOP);
2665 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002666 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002667 }
2668 else {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002669 /* 'import' ... */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002670 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters52152252000-08-17 22:55:00 +00002671 node *subn = CHILD(n, i);
2672 REQ(subn, dotted_as_name);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002673 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002674 com_push(c, 1);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002675 com_addopname(c, IMPORT_NAME, CHILD(subn, 0));
Thomas Wouters52152252000-08-17 22:55:00 +00002676 if (NCH(subn) > 1) {
Thomas Wouterse753ef82000-08-27 20:16:32 +00002677 int j;
2678 if (strcmp(STR(CHILD(subn, 1)), "as") != 0) {
Thomas Wouters52152252000-08-17 22:55:00 +00002679 com_error(c, PyExc_SyntaxError,
2680 "invalid syntax");
2681 return;
2682 }
Thomas Wouterse753ef82000-08-27 20:16:32 +00002683 for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2)
2684 com_addopname(c, LOAD_ATTR,
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002685 CHILD(CHILD(subn, 0),
2686 j));
2687 com_addop_varname(c, VAR_STORE,
2688 STR(CHILD(subn, 2)));
Thomas Wouters52152252000-08-17 22:55:00 +00002689 } else
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002690 com_addop_varname(c, VAR_STORE,
2691 STR(CHILD(CHILD(subn, 0),
2692 0)));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002693 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002694 }
2695 }
2696}
2697
2698static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002699com_exec_stmt(struct compiling *c, node *n)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002700{
2701 REQ(n, exec_stmt);
2702 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
2703 com_node(c, CHILD(n, 1));
2704 if (NCH(n) >= 4)
2705 com_node(c, CHILD(n, 3));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002706 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002707 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002708 com_push(c, 1);
2709 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002710 if (NCH(n) >= 6)
2711 com_node(c, CHILD(n, 5));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002712 else {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002713 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002714 com_push(c, 1);
2715 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002716 com_addbyte(c, EXEC_STMT);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002717 com_pop(c, 3);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002718}
2719
Guido van Rossum7c531111997-03-11 18:42:21 +00002720static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002721is_constant_false(struct compiling *c, node *n)
Guido van Rossum7c531111997-03-11 18:42:21 +00002722{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002723 PyObject *v;
Guido van Rossum7c531111997-03-11 18:42:21 +00002724 int i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002725 /* argument c will be NULL when called from symtable_node() */
Guido van Rossum7c531111997-03-11 18:42:21 +00002726
2727 /* Label to avoid tail recursion */
2728 next:
2729 switch (TYPE(n)) {
2730
2731 case suite:
2732 if (NCH(n) == 1) {
2733 n = CHILD(n, 0);
2734 goto next;
2735 }
2736 /* Fall through */
2737 case file_input:
2738 for (i = 0; i < NCH(n); i++) {
2739 node *ch = CHILD(n, i);
2740 if (TYPE(ch) == stmt) {
2741 n = ch;
2742 goto next;
2743 }
2744 }
2745 break;
2746
2747 case stmt:
2748 case simple_stmt:
2749 case small_stmt:
2750 n = CHILD(n, 0);
2751 goto next;
2752
2753 case expr_stmt:
2754 case testlist:
2755 case test:
2756 case and_test:
2757 case not_test:
2758 case comparison:
2759 case expr:
2760 case xor_expr:
2761 case and_expr:
2762 case shift_expr:
2763 case arith_expr:
2764 case term:
2765 case factor:
2766 case power:
2767 case atom:
2768 if (NCH(n) == 1) {
2769 n = CHILD(n, 0);
2770 goto next;
2771 }
2772 break;
2773
2774 case NAME:
2775 if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
2776 return 1;
2777 break;
2778
2779 case NUMBER:
2780 v = parsenumber(c, STR(n));
2781 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002782 PyErr_Clear();
Guido van Rossum7c531111997-03-11 18:42:21 +00002783 break;
2784 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002785 i = PyObject_IsTrue(v);
2786 Py_DECREF(v);
Guido van Rossum7c531111997-03-11 18:42:21 +00002787 return i == 0;
2788
2789 case STRING:
Guido van Rossum6af0c001997-03-11 21:25:55 +00002790 v = parsestr(STR(n));
Guido van Rossum7c531111997-03-11 18:42:21 +00002791 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002792 PyErr_Clear();
Guido van Rossum7c531111997-03-11 18:42:21 +00002793 break;
2794 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002795 i = PyObject_IsTrue(v);
2796 Py_DECREF(v);
Guido van Rossum7c531111997-03-11 18:42:21 +00002797 return i == 0;
2798
2799 }
2800 return 0;
2801}
2802
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002803static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002804com_if_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002805{
2806 int i;
2807 int anchor = 0;
2808 REQ(n, if_stmt);
2809 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
2810 for (i = 0; i+3 < NCH(n); i+=4) {
2811 int a = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002812 node *ch = CHILD(n, i+1);
Guido van Rossum7c531111997-03-11 18:42:21 +00002813 if (is_constant_false(c, ch))
2814 continue;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002815 if (i > 0)
2816 com_addoparg(c, SET_LINENO, ch->n_lineno);
Guido van Rossum7c531111997-03-11 18:42:21 +00002817 com_node(c, ch);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002818 com_addfwref(c, JUMP_IF_FALSE, &a);
2819 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002820 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002821 com_node(c, CHILD(n, i+3));
2822 com_addfwref(c, JUMP_FORWARD, &anchor);
2823 com_backpatch(c, a);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002824 /* We jump here with an extra entry which we now pop */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002825 com_addbyte(c, POP_TOP);
2826 }
2827 if (i+2 < NCH(n))
2828 com_node(c, CHILD(n, i+2));
Guido van Rossum7c531111997-03-11 18:42:21 +00002829 if (anchor)
2830 com_backpatch(c, anchor);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002831}
2832
2833static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002834com_while_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002835{
2836 int break_anchor = 0;
2837 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002838 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002839 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
2840 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002841 block_push(c, SETUP_LOOP);
2842 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002843 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002844 com_node(c, CHILD(n, 1));
2845 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2846 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002847 com_pop(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002848 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002849 com_node(c, CHILD(n, 3));
Guido van Rossum3f5da241990-12-20 15:06:42 +00002850 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002851 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2852 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002853 com_backpatch(c, anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002854 /* We jump here with one entry more on the stack */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002855 com_addbyte(c, POP_TOP);
2856 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002857 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002858 if (NCH(n) > 4)
2859 com_node(c, CHILD(n, 6));
2860 com_backpatch(c, break_anchor);
2861}
2862
2863static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002864com_for_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002865{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002866 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002867 int break_anchor = 0;
2868 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002869 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002870 REQ(n, for_stmt);
2871 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
2872 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002873 block_push(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002874 com_node(c, CHILD(n, 3));
Guido van Rossum79f25d91997-04-29 20:08:16 +00002875 v = PyInt_FromLong(0L);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002876 if (v == NULL)
2877 c->c_errors++;
2878 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002879 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002880 Py_XDECREF(v);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002881 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002882 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002883 com_addfwref(c, FOR_LOOP, &anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002884 com_push(c, 1);
Thomas Wouters434d0822000-08-24 20:11:32 +00002885 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002886 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002887 com_node(c, CHILD(n, 5));
Guido van Rossum3f5da241990-12-20 15:06:42 +00002888 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002889 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2890 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002891 com_backpatch(c, anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002892 com_pop(c, 2); /* FOR_LOOP has popped these */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002893 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002894 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002895 if (NCH(n) > 8)
2896 com_node(c, CHILD(n, 8));
2897 com_backpatch(c, break_anchor);
2898}
2899
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002900/* Code generated for "try: S finally: Sf" is as follows:
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002901
2902 SETUP_FINALLY L
2903 <code for S>
2904 POP_BLOCK
2905 LOAD_CONST <nil>
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002906 L: <code for Sf>
2907 END_FINALLY
2908
2909 The special instructions use the block stack. Each block
2910 stack entry contains the instruction that created it (here
2911 SETUP_FINALLY), the level of the value stack at the time the
2912 block stack entry was created, and a label (here L).
2913
2914 SETUP_FINALLY:
2915 Pushes the current value stack level and the label
2916 onto the block stack.
2917 POP_BLOCK:
2918 Pops en entry from the block stack, and pops the value
2919 stack until its level is the same as indicated on the
2920 block stack. (The label is ignored.)
2921 END_FINALLY:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002922 Pops a variable number of entries from the *value* stack
2923 and re-raises the exception they specify. The number of
2924 entries popped depends on the (pseudo) exception type.
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002925
2926 The block stack is unwound when an exception is raised:
2927 when a SETUP_FINALLY entry is found, the exception is pushed
2928 onto the value stack (and the exception condition is cleared),
2929 and the interpreter jumps to the label gotten from the block
2930 stack.
2931
2932 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
Guido van Rossum3f5da241990-12-20 15:06:42 +00002933 (The contents of the value stack is shown in [], with the top
2934 at the right; 'tb' is trace-back info, 'val' the exception's
2935 associated value, and 'exc' the exception.)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002936
2937 Value stack Label Instruction Argument
2938 [] SETUP_EXCEPT L1
2939 [] <code for S>
2940 [] POP_BLOCK
2941 [] JUMP_FORWARD L0
2942
Guido van Rossum3f5da241990-12-20 15:06:42 +00002943 [tb, val, exc] L1: DUP )
2944 [tb, val, exc, exc] <evaluate E1> )
2945 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2946 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2947 [tb, val, exc, 1] POP )
2948 [tb, val, exc] POP
2949 [tb, val] <assign to V1> (or POP if no V1)
2950 [tb] POP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002951 [] <code for S1>
2952 JUMP_FORWARD L0
2953
Guido van Rossum3f5da241990-12-20 15:06:42 +00002954 [tb, val, exc, 0] L2: POP
2955 [tb, val, exc] DUP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002956 .............................etc.......................
2957
Guido van Rossum3f5da241990-12-20 15:06:42 +00002958 [tb, val, exc, 0] Ln+1: POP
2959 [tb, val, exc] END_FINALLY # re-raise exception
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002960
2961 [] L0: <next statement>
2962
2963 Of course, parts are not generated if Vi or Ei is not present.
2964*/
2965
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002966static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002967com_try_except(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002968{
2969 int except_anchor = 0;
2970 int end_anchor = 0;
2971 int else_anchor = 0;
2972 int i;
2973 node *ch;
2974
2975 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
2976 block_push(c, SETUP_EXCEPT);
2977 com_node(c, CHILD(n, 2));
2978 com_addbyte(c, POP_BLOCK);
2979 block_pop(c, SETUP_EXCEPT);
2980 com_addfwref(c, JUMP_FORWARD, &else_anchor);
2981 com_backpatch(c, except_anchor);
2982 for (i = 3;
2983 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
2984 i += 3) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00002985 /* except_clause: 'except' [expr [',' var]] */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002986 if (except_anchor == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002987 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002988 "default 'except:' must be last");
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002989 break;
2990 }
2991 except_anchor = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00002992 com_push(c, 3); /* tb, val, exc pushed by exception */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002993 com_addoparg(c, SET_LINENO, ch->n_lineno);
2994 if (NCH(ch) > 1) {
2995 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002996 com_push(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002997 com_node(c, CHILD(ch, 1));
2998 com_addoparg(c, COMPARE_OP, EXC_MATCH);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002999 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003000 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
3001 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003002 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003003 }
3004 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003005 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003006 if (NCH(ch) > 3)
Thomas Wouters434d0822000-08-24 20:11:32 +00003007 com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003008 else {
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003009 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003010 com_pop(c, 1);
3011 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003012 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003013 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003014 com_node(c, CHILD(n, i+2));
3015 com_addfwref(c, JUMP_FORWARD, &end_anchor);
3016 if (except_anchor) {
3017 com_backpatch(c, except_anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003018 /* We come in with [tb, val, exc, 0] on the
3019 stack; one pop and it's the same as
3020 expected at the start of the loop */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003021 com_addbyte(c, POP_TOP);
3022 }
3023 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00003024 /* We actually come in here with [tb, val, exc] but the
3025 END_FINALLY will zap those and jump around.
3026 The c_stacklevel does not reflect them so we need not pop
3027 anything. */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003028 com_addbyte(c, END_FINALLY);
3029 com_backpatch(c, else_anchor);
3030 if (i < NCH(n))
3031 com_node(c, CHILD(n, i+2));
3032 com_backpatch(c, end_anchor);
3033}
3034
3035static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003036com_try_finally(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003037{
3038 int finally_anchor = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003039 node *ch;
Guido van Rossum94fb82e1992-04-05 14:24:50 +00003040
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003041 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
3042 block_push(c, SETUP_FINALLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003043 com_node(c, CHILD(n, 2));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003044 com_addbyte(c, POP_BLOCK);
3045 block_pop(c, SETUP_FINALLY);
3046 block_push(c, END_FINALLY);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003047 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003048 /* While the generated code pushes only one item,
3049 the try-finally handling can enter here with
3050 up to three items. OK, here are the details:
3051 3 for an exception, 2 for RETURN, 1 for BREAK. */
3052 com_push(c, 3);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003053 com_backpatch(c, finally_anchor);
3054 ch = CHILD(n, NCH(n)-1);
3055 com_addoparg(c, SET_LINENO, ch->n_lineno);
3056 com_node(c, ch);
3057 com_addbyte(c, END_FINALLY);
3058 block_pop(c, END_FINALLY);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003059 com_pop(c, 3); /* Matches the com_push above */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003060}
3061
3062static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003063com_try_stmt(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003064{
3065 REQ(n, try_stmt);
3066 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
3067 | 'try' ':' suite 'finally' ':' suite */
3068 if (TYPE(CHILD(n, 3)) != except_clause)
3069 com_try_finally(c, n);
3070 else
3071 com_try_except(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003072}
3073
Guido van Rossum8b993a91997-01-17 21:04:03 +00003074static node *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003075get_rawdocstring(node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003076{
Guido van Rossum164d4ff1995-01-26 00:40:09 +00003077 int i;
3078
Guido van Rossum8b993a91997-01-17 21:04:03 +00003079 /* Label to avoid tail recursion */
3080 next:
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003081 switch (TYPE(n)) {
3082
3083 case suite:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003084 if (NCH(n) == 1) {
3085 n = CHILD(n, 0);
3086 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003087 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00003088 /* Fall through */
Guido van Rossum164d4ff1995-01-26 00:40:09 +00003089 case file_input:
3090 for (i = 0; i < NCH(n); i++) {
3091 node *ch = CHILD(n, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003092 if (TYPE(ch) == stmt) {
3093 n = ch;
3094 goto next;
3095 }
Guido van Rossum164d4ff1995-01-26 00:40:09 +00003096 }
3097 break;
3098
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003099 case stmt:
3100 case simple_stmt:
3101 case small_stmt:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003102 n = CHILD(n, 0);
3103 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003104
3105 case expr_stmt:
3106 case testlist:
3107 case test:
3108 case and_test:
3109 case not_test:
3110 case comparison:
3111 case expr:
3112 case xor_expr:
3113 case and_expr:
3114 case shift_expr:
3115 case arith_expr:
3116 case term:
3117 case factor:
Guido van Rossum50564e81996-01-12 01:13:16 +00003118 case power:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003119 if (NCH(n) == 1) {
3120 n = CHILD(n, 0);
3121 goto next;
3122 }
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003123 break;
3124
3125 case atom:
3126 if (TYPE(CHILD(n, 0)) == STRING)
Guido van Rossum8b993a91997-01-17 21:04:03 +00003127 return n;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003128 break;
3129
3130 }
3131 return NULL;
3132}
3133
Guido van Rossum79f25d91997-04-29 20:08:16 +00003134static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003135get_docstring(node *n)
Guido van Rossum8b993a91997-01-17 21:04:03 +00003136{
Guido van Rossum541563e1999-01-28 15:08:09 +00003137 /* Don't generate doc-strings if run with -OO */
3138 if (Py_OptimizeFlag > 1)
3139 return NULL;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003140 n = get_rawdocstring(n);
3141 if (n == NULL)
3142 return NULL;
3143 return parsestrplus(n);
3144}
3145
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003146static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003147com_suite(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003148{
3149 REQ(n, suite);
3150 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
3151 if (NCH(n) == 1) {
3152 com_node(c, CHILD(n, 0));
3153 }
3154 else {
3155 int i;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003156 for (i = 0; i < NCH(n) && c->c_errors == 0; i++) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003157 node *ch = CHILD(n, i);
3158 if (TYPE(ch) == stmt)
3159 com_node(c, ch);
3160 }
3161 }
3162}
3163
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003164/* ARGSUSED */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003165static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003166com_continue_stmt(struct compiling *c, node *n)
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003167{
3168 int i = c->c_nblocks;
3169 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
3170 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3171 }
Fred Drakefd1f1be2000-09-08 16:31:24 +00003172 else if (i <= 0) {
3173 /* at the outer level */
3174 com_error(c, PyExc_SyntaxError,
3175 "'continue' not properly in loop");
3176 }
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003177 else {
Fred Drakefd1f1be2000-09-08 16:31:24 +00003178 int j;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003179 for (j = i-1; j >= 0; --j) {
Fred Drakefd1f1be2000-09-08 16:31:24 +00003180 if (c->c_block[j] == SETUP_LOOP)
3181 break;
3182 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003183 if (j >= 0) {
Fred Drakefd1f1be2000-09-08 16:31:24 +00003184 /* there is a loop, but something interferes */
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003185 for (; i > j; --i) {
3186 if (c->c_block[i] == SETUP_EXCEPT ||
3187 c->c_block[i] == SETUP_FINALLY) {
3188 com_addoparg(c, CONTINUE_LOOP,
3189 c->c_begin);
Fred Drakefd1f1be2000-09-08 16:31:24 +00003190 return;
3191 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003192 if (c->c_block[i] == END_FINALLY) {
3193 com_error(c, PyExc_SyntaxError,
3194 "'continue' not supported inside 'finally' clause");
3195 return;
3196 }
Fred Drakefd1f1be2000-09-08 16:31:24 +00003197 }
3198 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00003199 com_error(c, PyExc_SyntaxError,
3200 "'continue' not properly in loop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003201 }
3202 /* XXX Could allow it inside a 'finally' clause
3203 XXX if we could pop the exception still on the stack */
3204}
3205
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003206static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003207com_argdefs(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003208{
Guido van Rossum681d79a1995-07-18 14:51:37 +00003209 int i, nch, nargs, ndefs;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003210 if (TYPE(n) == lambdef) {
3211 /* lambdef: 'lambda' [varargslist] ':' test */
3212 n = CHILD(n, 1);
3213 }
3214 else {
3215 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
3216 n = CHILD(n, 2);
3217 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
3218 n = CHILD(n, 1);
3219 }
3220 if (TYPE(n) != varargslist)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003221 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003222 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00003223 (fpdef ['=' test] ',')* '*' ....... |
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003224 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
3225 nch = NCH(n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003226 nargs = 0;
3227 ndefs = 0;
3228 for (i = 0; i < nch; i++) {
3229 int t;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003230 if (TYPE(CHILD(n, i)) == STAR ||
3231 TYPE(CHILD(n, i)) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00003232 break;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003233 nargs++;
3234 i++;
3235 if (i >= nch)
Guido van Rossuma1e7e621995-09-18 21:44:04 +00003236 t = RPAR; /* Anything except EQUAL or COMMA */
3237 else
3238 t = TYPE(CHILD(n, i));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003239 if (t == EQUAL) {
3240 i++;
3241 ndefs++;
3242 com_node(c, CHILD(n, i));
3243 i++;
3244 if (i >= nch)
3245 break;
3246 t = TYPE(CHILD(n, i));
3247 }
3248 else {
Guido van Rossum29d38cd1998-10-02 13:41:54 +00003249 /* Treat "(a=1, b)" as an error */
3250 if (ndefs)
3251 com_error(c, PyExc_SyntaxError,
Guido van Rossumdfede311998-10-02 14:06:56 +00003252 "non-default argument follows default argument");
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003253 }
3254 if (t != COMMA)
3255 break;
3256 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003257 return ndefs;
3258}
3259
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003260static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003261com_funcdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003262{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003263 PyObject *co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003264 int ndefs;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003265 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003266 ndefs = com_argdefs(c, n);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003267 symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n),
3268 n->n_lineno);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003269 co = (PyObject *)icompile(n, c);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003270 symtable_exit_scope(c->c_symtable);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003271 if (co == NULL)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003272 c->c_errors++;
3273 else {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003274 int closure = com_make_closure(c, (PyCodeObject *)co);
3275 int i = com_addconst(c, co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003276 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003277 com_push(c, 1);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003278 if (closure)
3279 com_addoparg(c, MAKE_CLOSURE, ndefs);
3280 else
3281 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003282 com_pop(c, ndefs);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003283 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003284 com_pop(c, 1);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003285 Py_DECREF(co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003286 }
3287}
3288
3289static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003290com_bases(struct compiling *c, node *n)
Guido van Rossumc5e96291991-12-10 13:53:51 +00003291{
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003292 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003293 REQ(n, testlist);
3294 /* testlist: test (',' test)* [','] */
3295 for (i = 0; i < NCH(n); i += 2)
3296 com_node(c, CHILD(n, i));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003297 i = (NCH(n)+1) / 2;
3298 com_addoparg(c, BUILD_TUPLE, i);
3299 com_pop(c, i-1);
Guido van Rossumc5e96291991-12-10 13:53:51 +00003300}
3301
3302static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003303com_classdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003304{
Guido van Rossum25831651993-05-19 14:50:45 +00003305 int i;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003306 PyObject *co, *v;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003307 char *name;
3308
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003309 REQ(n, classdef);
Guido van Rossum25831651993-05-19 14:50:45 +00003310 /* classdef: class NAME ['(' testlist ')'] ':' suite */
Guido van Rossum7b89b6a1997-01-18 08:02:57 +00003311 if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
Guido van Rossum25831651993-05-19 14:50:45 +00003312 c->c_errors++;
3313 return;
3314 }
3315 /* Push the class name on the stack */
3316 i = com_addconst(c, v);
3317 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003318 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003319 Py_DECREF(v);
Guido van Rossum25831651993-05-19 14:50:45 +00003320 /* Push the tuple of base classes on the stack */
Guido van Rossum8b993a91997-01-17 21:04:03 +00003321 if (TYPE(CHILD(n, 2)) != LPAR) {
Guido van Rossumc5e96291991-12-10 13:53:51 +00003322 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003323 com_push(c, 1);
3324 }
Guido van Rossum25831651993-05-19 14:50:45 +00003325 else
3326 com_bases(c, CHILD(n, 3));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003327 name = STR(CHILD(n, 1));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003328 symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003329 co = (PyObject *)icompile(n, c);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003330 symtable_exit_scope(c->c_symtable);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003331 if (co == NULL)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003332 c->c_errors++;
3333 else {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003334 int closure = com_make_closure(c, (PyCodeObject *)co);
3335 i = com_addconst(c, co);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003336 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003337 com_push(c, 1);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003338 if (closure)
3339 com_addoparg(c, MAKE_CLOSURE, 0);
3340 else
3341 com_addoparg(c, MAKE_FUNCTION, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003342 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003343 com_addbyte(c, BUILD_CLASS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003344 com_pop(c, 2);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003345 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003346 Py_DECREF(co);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003347 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003348}
3349
3350static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003351com_node(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003352{
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003353 loop:
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003354 if (c->c_errors)
3355 return;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003356 switch (TYPE(n)) {
3357
3358 /* Definition nodes */
3359
3360 case funcdef:
3361 com_funcdef(c, n);
3362 break;
3363 case classdef:
3364 com_classdef(c, n);
3365 break;
3366
3367 /* Trivial parse tree nodes */
3368
3369 case stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003370 case small_stmt:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003371 case flow_stmt:
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003372 n = CHILD(n, 0);
3373 goto loop;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003374
3375 case simple_stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003376 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
3377 com_addoparg(c, SET_LINENO, n->n_lineno);
3378 {
3379 int i;
3380 for (i = 0; i < NCH(n)-1; i += 2)
3381 com_node(c, CHILD(n, i));
3382 }
3383 break;
3384
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003385 case compound_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003386 com_addoparg(c, SET_LINENO, n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003387 n = CHILD(n, 0);
3388 goto loop;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003389
3390 /* Statement nodes */
3391
3392 case expr_stmt:
3393 com_expr_stmt(c, n);
3394 break;
3395 case print_stmt:
3396 com_print_stmt(c, n);
3397 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003398 case del_stmt: /* 'del' exprlist */
Thomas Wouters434d0822000-08-24 20:11:32 +00003399 com_assign(c, CHILD(n, 1), OP_DELETE, NULL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003400 break;
3401 case pass_stmt:
3402 break;
3403 case break_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003404 if (c->c_loops == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003405 com_error(c, PyExc_SyntaxError,
3406 "'break' outside loop");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003407 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003408 com_addbyte(c, BREAK_LOOP);
3409 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003410 case continue_stmt:
3411 com_continue_stmt(c, n);
3412 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003413 case return_stmt:
3414 com_return_stmt(c, n);
3415 break;
3416 case raise_stmt:
3417 com_raise_stmt(c, n);
3418 break;
3419 case import_stmt:
3420 com_import_stmt(c, n);
3421 break;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003422 case global_stmt:
Guido van Rossumc5e96291991-12-10 13:53:51 +00003423 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003424 case exec_stmt:
3425 com_exec_stmt(c, n);
3426 break;
Guido van Rossum228d7f31997-04-02 05:24:36 +00003427 case assert_stmt:
3428 com_assert_stmt(c, n);
3429 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003430 case if_stmt:
3431 com_if_stmt(c, n);
3432 break;
3433 case while_stmt:
3434 com_while_stmt(c, n);
3435 break;
3436 case for_stmt:
3437 com_for_stmt(c, n);
3438 break;
3439 case try_stmt:
3440 com_try_stmt(c, n);
3441 break;
3442 case suite:
3443 com_suite(c, n);
3444 break;
3445
3446 /* Expression nodes */
3447
3448 case testlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00003449 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003450 break;
3451 case test:
3452 com_test(c, n);
3453 break;
3454 case and_test:
3455 com_and_test(c, n);
3456 break;
3457 case not_test:
3458 com_not_test(c, n);
3459 break;
3460 case comparison:
3461 com_comparison(c, n);
3462 break;
3463 case exprlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00003464 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003465 break;
3466 case expr:
3467 com_expr(c, n);
3468 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +00003469 case xor_expr:
3470 com_xor_expr(c, n);
3471 break;
3472 case and_expr:
3473 com_and_expr(c, n);
3474 break;
3475 case shift_expr:
3476 com_shift_expr(c, n);
3477 break;
3478 case arith_expr:
3479 com_arith_expr(c, n);
3480 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003481 case term:
3482 com_term(c, n);
3483 break;
3484 case factor:
3485 com_factor(c, n);
3486 break;
Guido van Rossum50564e81996-01-12 01:13:16 +00003487 case power:
3488 com_power(c, n);
3489 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003490 case atom:
3491 com_atom(c, n);
3492 break;
3493
3494 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00003495 com_error(c, PyExc_SystemError,
3496 "com_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003497 }
3498}
3499
Tim Petersdbd9ba62000-07-09 03:09:57 +00003500static void com_fplist(struct compiling *, node *);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003501
3502static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003503com_fpdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003504{
3505 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
3506 if (TYPE(CHILD(n, 0)) == LPAR)
3507 com_fplist(c, CHILD(n, 1));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003508 else {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003509 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003510 com_pop(c, 1);
3511 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003512}
3513
3514static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003515com_fplist(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003516{
Guido van Rossuma9df32a1991-12-31 13:13:35 +00003517 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003518 if (NCH(n) == 1) {
3519 com_fpdef(c, CHILD(n, 0));
3520 }
3521 else {
Guido van Rossum8b993a91997-01-17 21:04:03 +00003522 int i = (NCH(n)+1)/2;
Thomas Wouters0be5aab2000-08-11 22:15:52 +00003523 com_addoparg(c, UNPACK_SEQUENCE, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003524 com_push(c, i-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003525 for (i = 0; i < NCH(n); i += 2)
3526 com_fpdef(c, CHILD(n, i));
3527 }
3528}
3529
3530static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003531com_arglist(struct compiling *c, node *n)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003532{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003533 int nch, i, narg;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003534 int complex = 0;
Guido van Rossumdb9e20f1997-07-10 01:06:53 +00003535 char nbuf[10];
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003536 REQ(n, varargslist);
Guido van Rossumacbefef1992-01-19 16:33:51 +00003537 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00003538 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003539 nch = NCH(n);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003540 /* Enter all arguments in table of locals */
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003541 for (i = 0, narg = 0; i < nch; i++) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00003542 node *ch = CHILD(n, i);
3543 node *fp;
3544 char *name;
Guido van Rossum50564e81996-01-12 01:13:16 +00003545 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00003546 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003547 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3548 fp = CHILD(ch, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003549 if (TYPE(fp) == NAME) {
3550 PyObject *v;
3551 name = STR(fp);
3552 v = PyDict_GetItemString(c->c_cellvars, name);
3553 if (v) {
3554 com_addoparg(c, LOAD_FAST, narg);
3555 com_addoparg(c, STORE_DEREF,
3556 PyInt_AS_LONG(v));
3557 }
3558 } else {
Guido van Rossumdb9e20f1997-07-10 01:06:53 +00003559 name = nbuf;
3560 sprintf(nbuf, ".%d", i);
Guido van Rossum530956d1996-07-21 02:27:43 +00003561 complex = 1;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003562 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003563 narg++;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003564 /* all name updates handled by symtable */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003565 if (++i >= nch)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003566 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003567 ch = CHILD(n, i);
3568 if (TYPE(ch) == EQUAL)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003569 i += 2;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003570 else
3571 REQ(ch, COMMA);
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003572 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003573 /* Handle *arguments */
3574 if (i < nch) {
3575 node *ch;
3576 ch = CHILD(n, i);
Guido van Rossum50564e81996-01-12 01:13:16 +00003577 if (TYPE(ch) != DOUBLESTAR) {
3578 REQ(ch, STAR);
3579 ch = CHILD(n, i+1);
3580 if (TYPE(ch) == NAME) {
Jeremy Hylton6492bf72001-02-09 22:55:26 +00003581 PyObject *v;
Guido van Rossum50564e81996-01-12 01:13:16 +00003582 i += 3;
Jeremy Hylton6492bf72001-02-09 22:55:26 +00003583 v = PyDict_GetItemString(c->c_cellvars,
3584 STR(ch));
3585 if (v) {
3586 com_addoparg(c, LOAD_FAST, narg);
3587 com_addoparg(c, STORE_DEREF,
3588 PyInt_AS_LONG(v));
Guido van Rossum50564e81996-01-12 01:13:16 +00003589 }
Jeremy Hylton6492bf72001-02-09 22:55:26 +00003590 narg++;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003591 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003592 }
Jeremy Hylton6492bf72001-02-09 22:55:26 +00003593 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003594 /* Handle **keywords */
3595 if (i < nch) {
Jeremy Hylton6492bf72001-02-09 22:55:26 +00003596 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003597 node *ch;
3598 ch = CHILD(n, i);
Guido van Rossum50564e81996-01-12 01:13:16 +00003599 if (TYPE(ch) != DOUBLESTAR) {
3600 REQ(ch, STAR);
3601 ch = CHILD(n, i+1);
3602 REQ(ch, STAR);
3603 ch = CHILD(n, i+2);
3604 }
3605 else
3606 ch = CHILD(n, i+1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003607 REQ(ch, NAME);
Jeremy Hylton6492bf72001-02-09 22:55:26 +00003608 v = PyDict_GetItemString(c->c_cellvars, STR(ch));
3609 if (v) {
3610 com_addoparg(c, LOAD_FAST, narg);
3611 com_addoparg(c, STORE_DEREF, PyInt_AS_LONG(v));
3612 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003613 }
3614 if (complex) {
3615 /* Generate code for complex arguments only after
3616 having counted the simple arguments */
3617 int ilocal = 0;
3618 for (i = 0; i < nch; i++) {
3619 node *ch = CHILD(n, i);
3620 node *fp;
Guido van Rossum50564e81996-01-12 01:13:16 +00003621 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003622 break;
3623 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3624 fp = CHILD(ch, 0);
3625 if (TYPE(fp) != NAME) {
3626 com_addoparg(c, LOAD_FAST, ilocal);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003627 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003628 com_fpdef(c, ch);
3629 }
3630 ilocal++;
3631 if (++i >= nch)
3632 break;
3633 ch = CHILD(n, i);
3634 if (TYPE(ch) == EQUAL)
3635 i += 2;
3636 else
3637 REQ(ch, COMMA);
3638 }
3639 }
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003640}
3641
3642static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003643com_file_input(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003644{
3645 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003646 PyObject *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003647 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003648 doc = get_docstring(n);
3649 if (doc != NULL) {
3650 int i = com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003651 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003652 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003653 com_push(c, 1);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003654 com_addop_name(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00003655 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003656 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003657 for (i = 0; i < NCH(n); i++) {
3658 node *ch = CHILD(n, i);
3659 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
3660 com_node(c, ch);
3661 }
3662}
3663
3664/* Top-level compile-node interface */
3665
3666static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003667compile_funcdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003668{
Guido van Rossum79f25d91997-04-29 20:08:16 +00003669 PyObject *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003670 node *ch;
3671 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum9bfef441993-03-29 10:43:31 +00003672 c->c_name = STR(CHILD(n, 1));
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003673 doc = get_docstring(CHILD(n, 4));
3674 if (doc != NULL) {
3675 (void) com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003676 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003677 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003678 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00003679 (void) com_addconst(c, Py_None); /* No docstring */
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003680 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
3681 ch = CHILD(ch, 1); /* ')' | varargslist */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003682 if (TYPE(ch) == varargslist)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003683 com_arglist(c, ch);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003684 c->c_infunction = 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003685 com_node(c, CHILD(n, 4));
Guido van Rossum3f5da241990-12-20 15:06:42 +00003686 c->c_infunction = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003687 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003688 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003689 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003690 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003691}
3692
3693static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003694compile_lambdef(struct compiling *c, node *n)
Guido van Rossum12d12c51993-10-26 17:58:25 +00003695{
Guido van Rossum590baa41993-11-30 13:40:46 +00003696 node *ch;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003697 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
Guido van Rossum363ac7d1994-11-10 22:40:34 +00003698 c->c_name = "<lambda>";
Guido van Rossum590baa41993-11-30 13:40:46 +00003699
3700 ch = CHILD(n, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003701 (void) com_addconst(c, Py_None); /* No docstring */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003702 if (TYPE(ch) == varargslist) {
Guido van Rossum590baa41993-11-30 13:40:46 +00003703 com_arglist(c, ch);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003704 ch = CHILD(n, 3);
Guido van Rossum590baa41993-11-30 13:40:46 +00003705 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003706 else
3707 ch = CHILD(n, 2);
3708 com_node(c, ch);
Guido van Rossum12d12c51993-10-26 17:58:25 +00003709 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003710 com_pop(c, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00003711}
3712
3713static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003714compile_classdef(struct compiling *c, node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003715{
3716 node *ch;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003717 PyObject *doc;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003718 REQ(n, classdef);
3719 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3720 c->c_name = STR(CHILD(n, 1));
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003721 c->c_private = c->c_name;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003722 ch = CHILD(n, NCH(n)-1); /* The suite */
3723 doc = get_docstring(ch);
3724 if (doc != NULL) {
3725 int i = com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003726 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003727 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003728 com_push(c, 1);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003729 com_addop_name(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00003730 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003731 }
3732 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00003733 (void) com_addconst(c, Py_None);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003734 com_node(c, ch);
3735 com_addbyte(c, LOAD_LOCALS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003736 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003737 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003738 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003739}
3740
3741static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003742compile_node(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003743{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003744 com_addoparg(c, SET_LINENO, n->n_lineno);
3745
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003746 switch (TYPE(n)) {
3747
Guido van Rossum4c417781991-01-21 16:09:22 +00003748 case single_input: /* One interactive command */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003749 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003750 c->c_interactive++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003751 n = CHILD(n, 0);
3752 if (TYPE(n) != NEWLINE)
3753 com_node(c, n);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003754 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003755 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003756 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003757 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003758 c->c_interactive--;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003759 break;
3760
Guido van Rossum4c417781991-01-21 16:09:22 +00003761 case file_input: /* A whole file, or built-in function exec() */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003762 com_file_input(c, n);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003763 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003764 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003765 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003766 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003767 break;
3768
Guido van Rossum590baa41993-11-30 13:40:46 +00003769 case eval_input: /* Built-in function input() */
Guido van Rossum4c417781991-01-21 16:09:22 +00003770 com_node(c, CHILD(n, 0));
3771 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003772 com_pop(c, 1);
Guido van Rossum4c417781991-01-21 16:09:22 +00003773 break;
3774
Guido van Rossum590baa41993-11-30 13:40:46 +00003775 case lambdef: /* anonymous function definition */
3776 compile_lambdef(c, n);
3777 break;
3778
Guido van Rossum4c417781991-01-21 16:09:22 +00003779 case funcdef: /* A function definition */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003780 compile_funcdef(c, n);
3781 break;
3782
Guido van Rossum4c417781991-01-21 16:09:22 +00003783 case classdef: /* A class definition */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003784 compile_classdef(c, n);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003785 break;
3786
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003787 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00003788 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00003789 "compile_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003790 }
3791}
3792
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003793static PyObject *
3794dict_keys_inorder(PyObject *dict, int offset)
3795{
3796 PyObject *tuple, *k, *v;
3797 int i, pos = 0, size = PyDict_Size(dict);
3798
3799 tuple = PyTuple_New(size);
3800 if (tuple == NULL)
3801 return NULL;
3802 while (PyDict_Next(dict, &pos, &k, &v)) {
3803 i = PyInt_AS_LONG(v);
3804 Py_INCREF(k);
3805 PyTuple_SET_ITEM(tuple, i - offset, k);
3806 }
3807 return tuple;
3808}
3809
Guido van Rossum79f25d91997-04-29 20:08:16 +00003810PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003811PyNode_Compile(node *n, char *filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003812{
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003813 return jcompile(n, filename, NULL);
3814}
3815
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003816struct symtable *
3817PyNode_CompileSymtable(node *n, char *filename)
3818{
3819 struct symtable *st;
3820
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003821 st = symtable_init();
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003822 if (st == NULL)
3823 return NULL;
Jeremy Hylton5acc0c02001-02-02 20:01:10 +00003824 assert(st->st_symbols != NULL);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003825 symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno);
3826 if (st->st_errors > 0) {
3827 PySymtable_Free(st);
3828 return NULL;
3829 }
3830 symtable_node(st, n);
3831 if (st->st_errors > 0) {
3832 PySymtable_Free(st);
3833 return NULL;
3834 }
3835 return st;
3836}
3837
Guido van Rossum79f25d91997-04-29 20:08:16 +00003838static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003839icompile(node *n, struct compiling *base)
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003840{
3841 return jcompile(n, base->c_filename, base);
3842}
3843
Guido van Rossum79f25d91997-04-29 20:08:16 +00003844static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003845jcompile(node *n, char *filename, struct compiling *base)
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003846{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003847 struct compiling sc;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003848 PyCodeObject *co;
Guido van Rossum590baa41993-11-30 13:40:46 +00003849 if (!com_init(&sc, filename))
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003850 return NULL;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003851 if (base) {
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003852 sc.c_private = base->c_private;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003853 sc.c_symtable = base->c_symtable;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003854 /* c_symtable still points to parent's symbols */
3855 if (base->c_nested
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003856 || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION))
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003857 sc.c_nested = 1;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003858 } else {
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003859 sc.c_private = NULL;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003860 if (symtable_build(&sc, n) < 0) {
3861 com_free(&sc);
3862 return NULL;
3863 }
3864 }
3865 co = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003866 if (symtable_load_symbols(&sc) < 0) {
3867 sc.c_errors++;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003868 goto exit;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003869 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003870 compile_node(&sc, n);
3871 com_done(&sc);
Guido van Rossum9bfef441993-03-29 10:43:31 +00003872 if (sc.c_errors == 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003873 PyObject *consts, *names, *varnames, *filename, *name,
3874 *freevars, *cellvars;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003875 consts = PyList_AsTuple(sc.c_consts);
3876 names = PyList_AsTuple(sc.c_names);
3877 varnames = PyList_AsTuple(sc.c_varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003878 cellvars = dict_keys_inorder(sc.c_cellvars, 0);
3879 freevars = dict_keys_inorder(sc.c_freevars,
3880 PyTuple_GET_SIZE(cellvars));
Guido van Rossum7b89b6a1997-01-18 08:02:57 +00003881 filename = PyString_InternFromString(sc.c_filename);
3882 name = PyString_InternFromString(sc.c_name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003883 if (!PyErr_Occurred())
3884 co = PyCode_New(sc.c_argcount,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003885 sc.c_nlocals,
3886 sc.c_maxstacklevel,
3887 sc.c_flags,
3888 sc.c_code,
3889 consts,
3890 names,
3891 varnames,
3892 freevars,
3893 cellvars,
3894 filename,
3895 name,
3896 sc.c_firstlineno,
3897 sc.c_lnotab);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003898 Py_XDECREF(consts);
3899 Py_XDECREF(names);
3900 Py_XDECREF(varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003901 Py_XDECREF(freevars);
3902 Py_XDECREF(cellvars);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003903 Py_XDECREF(filename);
3904 Py_XDECREF(name);
Guido van Rossuma082ce41991-06-04 19:41:56 +00003905 }
Guido van Rossum6c2f0c72000-08-07 19:22:43 +00003906 else if (!PyErr_Occurred()) {
3907 /* This could happen if someone called PyErr_Clear() after an
3908 error was reported above. That's not supposed to happen,
3909 but I just plugged one case and I'm not sure there can't be
3910 others. In that case, raise SystemError so that at least
3911 it gets reported instead dumping core. */
3912 PyErr_SetString(PyExc_SystemError, "lost syntax error");
3913 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003914 exit:
3915 if (base == NULL)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003916 PySymtable_Free(sc.c_symtable);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003917 com_free(&sc);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003918 return co;
3919}
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00003920
3921int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003922PyCode_Addr2Line(PyCodeObject *co, int addrq)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00003923{
3924 int size = PyString_Size(co->co_lnotab) / 2;
Guido van Rossum2174dcb1999-09-15 22:48:09 +00003925 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00003926 int line = co->co_firstlineno;
3927 int addr = 0;
3928 while (--size >= 0) {
3929 addr += *p++;
3930 if (addr > addrq)
3931 break;
3932 line += *p++;
3933 }
3934 return line;
3935}
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003936
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003937/* The test for LOCAL must come before the test for FREE in order to
3938 handle classes where name is both local and free. The local var is
3939 a method and the free var is a free var referenced within a method.
3940*/
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003941
3942static int
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003943get_ref_type(struct compiling *c, char *name)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003944{
3945 PyObject *v;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003946 if (PyDict_GetItemString(c->c_cellvars, name) != NULL)
3947 return CELL;
3948 if (PyDict_GetItemString(c->c_locals, name) != NULL)
3949 return LOCAL;
3950 if (PyDict_GetItemString(c->c_freevars, name) != NULL)
3951 return FREE;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003952 v = PyDict_GetItemString(c->c_globals, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003953 if (v) {
3954 if (v == Py_None)
3955 return GLOBAL_EXPLICIT;
3956 else {
3957 return GLOBAL_IMPLICIT;
3958 }
3959 }
3960 {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003961 char buf[350];
3962 sprintf(buf,
3963 "unknown scope for %.100s in %.100s(%s) in %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00003964 name, c->c_name,
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003965 PyObject_REPR(c->c_symtable->st_cur->ste_id),
3966 c->c_filename);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003967 Py_FatalError(buf);
3968 }
3969 return -1; /* can't get here */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003970}
3971
3972static int
3973symtable_build(struct compiling *c, node *n)
3974{
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003975 if ((c->c_symtable = symtable_init()) == NULL)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003976 return -1;
Jeremy Hylton5acc0c02001-02-02 20:01:10 +00003977 c->c_symtable->st_filename = c->c_filename;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003978 symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno);
3979 if (c->c_symtable->st_errors > 0)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003980 return -1;
3981 symtable_node(c->c_symtable, n);
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00003982 if (c->c_symtable->st_errors > 0)
3983 return -1;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003984 /* reset for second pass */
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003985 c->c_symtable->st_nscopes = 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003986 c->c_symtable->st_pass = 2;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003987 return 0;
3988}
3989
3990static int
3991symtable_load_symbols(struct compiling *c)
3992{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003993 static PyObject *implicit = NULL;
3994 PyObject *name, *varnames, *v;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003995 int i, flags, pos;
3996 int nlocals, nfrees, ncells, nimplicit;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003997 struct symtable *st = c->c_symtable;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003998 PySymtableEntryObject *ste = st->st_cur;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003999
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004000 if (implicit == NULL) {
4001 implicit = PyInt_FromLong(1);
4002 if (implicit == NULL)
4003 return -1;
4004 }
4005 v = NULL;
4006
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004007 varnames = st->st_cur->ste_varnames;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004008 if (varnames == NULL) {
4009 varnames = PyList_New(0);
4010 if (varnames == NULL)
4011 return -1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004012 ste->ste_varnames = varnames;
4013 Py_INCREF(varnames);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004014 } else
4015 Py_INCREF(varnames);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004016 c->c_varnames = varnames;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004017
4018 c->c_globals = PyDict_New();
4019 if (c->c_globals == NULL)
4020 goto fail;
4021 c->c_freevars = PyDict_New();
4022 if (c->c_freevars == NULL)
4023 goto fail;
4024 c->c_cellvars = PyDict_New();
4025 if (c->c_cellvars == NULL)
4026 goto fail;
4027
4028 nlocals = PyList_GET_SIZE(varnames);
4029 c->c_argcount = nlocals;
4030 nfrees = 0;
4031 ncells = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004032 nimplicit = 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004033 for (i = 0; i < nlocals; ++i) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004034 v = PyInt_FromLong(i);
4035 if (PyDict_SetItem(c->c_locals,
4036 PyList_GET_ITEM(varnames, i), v) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004037 goto fail;
4038 Py_DECREF(v);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004039 }
4040
4041 /* XXX The cases below define the rules for whether a name is
4042 local or global. The logic could probably be clearer. */
4043 pos = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004044 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
4045 flags = PyInt_AS_LONG(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004046
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004047 if (flags & DEF_FREE_GLOBAL)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004048 /* undo the original DEF_FREE */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004049 flags &= ~(DEF_FREE | DEF_FREE_CLASS);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004050
4051 /* Seperate logic for DEF_FREE. If it occurs in a
4052 function, it indicates a local that we must
4053 allocate storage for (a cell var). If it occurs in
4054 a class, then the class has a method and a free
4055 variable with the same name.
4056 */
4057
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004058 if ((flags & (DEF_FREE | DEF_FREE_CLASS))
4059 && (flags & (DEF_LOCAL | DEF_PARAM))) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004060 PyObject *dict;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004061 if (ste->ste_type == TYPE_FUNCTION) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004062 v = PyInt_FromLong(ncells++);
4063 dict = c->c_cellvars;
4064 } else {
4065 v = PyInt_FromLong(nfrees++);
4066 dict = c->c_freevars;
4067 }
4068 if (v == NULL)
4069 return -1;
4070 if (PyDict_SetItem(dict, name, v) < 0)
4071 goto fail;
4072 Py_DECREF(v);
4073 }
4074
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004075 if (flags & DEF_STAR) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004076 c->c_argcount--;
4077 c->c_flags |= CO_VARARGS;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004078 } else if (flags & DEF_DOUBLESTAR) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004079 c->c_argcount--;
4080 c->c_flags |= CO_VARKEYWORDS;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004081 } else if (flags & DEF_INTUPLE)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004082 c->c_argcount--;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004083 else if (flags & DEF_GLOBAL) {
4084 if ((flags & DEF_PARAM)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004085 && (PyString_AS_STRING(name)[0] != '.')){
Jeremy Hylton5acc0c02001-02-02 20:01:10 +00004086 PyErr_Format(PyExc_SyntaxError,
4087 "name '%.400s' is local and global",
4088 PyString_AS_STRING(name));
4089 set_error_location(st->st_filename,
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004090 ste->ste_lineno);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004091 goto fail;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004092 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004093 if (PyDict_SetItem(c->c_globals, name, Py_None) < 0)
4094 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004095 } else if (flags & DEF_FREE_GLOBAL) {
4096 nimplicit++;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004097 if (PyDict_SetItem(c->c_globals, name, implicit) < 0)
4098 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004099 } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004100 v = PyInt_FromLong(nlocals++);
4101 if (v == NULL)
4102 goto fail;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004103 if (PyDict_SetItem(c->c_locals, name, v) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004104 goto fail;
4105 Py_DECREF(v);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004106 if (ste->ste_type != TYPE_CLASS)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004107 if (PyList_Append(c->c_varnames, name) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004108 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004109 } else if (is_free(flags)) {
4110 if (ste->ste_nested) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004111 v = PyInt_FromLong(nfrees++);
4112 if (v == NULL)
4113 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004114 if (PyDict_SetItem(c->c_freevars, name, v) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004115 goto fail;
4116 Py_DECREF(v);
4117 } else {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004118 nimplicit++;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004119 if (PyDict_SetItem(c->c_globals, name,
4120 implicit) < 0)
4121 goto fail;
4122 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004123 }
4124 }
4125
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004126 /* The cell vars are the first elements of the closure,
4127 followed by the free vars. Update the offsets in
4128 c_freevars to account for number of cellvars. */
4129 pos = 0;
4130 while (PyDict_Next(c->c_freevars, &pos, &name, &v)) {
4131 int i = PyInt_AS_LONG(v) + ncells;
4132 PyObject *o = PyInt_FromLong(i);
4133 if (PyDict_SetItem(c->c_freevars, name, o) < 0) {
4134 Py_DECREF(o);
4135 return -1;
4136 }
4137 Py_DECREF(o);
4138 }
4139
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004140 if (ste->ste_type == TYPE_FUNCTION)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004141 c->c_nlocals = nlocals;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004142
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004143 if (ste->ste_type != TYPE_MODULE)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004144 c->c_flags |= CO_NEWLOCALS;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004145 if (ste->ste_type == TYPE_FUNCTION) {
4146 if (ste->ste_optimized)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004147 c->c_flags |= CO_OPTIMIZED;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004148 else if (ncells || nfrees
4149 || (ste->ste_nested && nimplicit)
4150 || ste->ste_child_free) {
4151 PyErr_Format(PyExc_SyntaxError, ILLEGAL_DYNAMIC_SCOPE,
4152 PyString_AS_STRING(ste->ste_name));
4153 set_error_location(st->st_filename, ste->ste_lineno);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004154 return -1;
4155 }
4156 }
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004157
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004158 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004159
4160 fail:
4161 /* is this always the right thing to do? */
4162 Py_XDECREF(v);
4163 return -1;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004164}
4165
4166static struct symtable *
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004167symtable_init()
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004168{
4169 struct symtable *st;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004170
4171 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
4172 if (st == NULL)
4173 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004174 st->st_pass = 1;
Jeremy Hylton5acc0c02001-02-02 20:01:10 +00004175 st->st_filename = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004176 if ((st->st_stack = PyList_New(0)) == NULL)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004177 goto fail;
4178 if ((st->st_symbols = PyDict_New()) == NULL)
4179 goto fail;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004180 st->st_cur = NULL;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004181 st->st_nscopes = 0;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004182 st->st_errors = 0;
4183 st->st_tmpname = 0;
4184 st->st_private = NULL;
4185 return st;
4186 fail:
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004187 PySymtable_Free(st);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004188 return NULL;
4189}
4190
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004191void
4192PySymtable_Free(struct symtable *st)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004193{
4194 Py_XDECREF(st->st_symbols);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004195 Py_XDECREF(st->st_stack);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004196 PyMem_Free((void *)st);
4197}
4198
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004199/* When the compiler exits a scope, it must should update the scope's
4200 free variable information with the list of free variables in its
4201 children.
4202
4203 Variables that are free in children and defined in the current
4204 scope are cellvars.
4205
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004206 If the scope being exited is defined at the top-level (ste_nested is
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004207 false), free variables in children that are not defined here are
4208 implicit globals.
4209
4210*/
4211
4212static int
4213symtable_update_free_vars(struct symtable *st)
4214{
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004215 int i, j, def;
4216 PyObject *o, *name, *list = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004217 PySymtableEntryObject *child, *ste = st->st_cur;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004218
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004219 if (ste->ste_type == TYPE_CLASS)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004220 def = DEF_FREE_CLASS;
4221 else
4222 def = DEF_FREE;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004223 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004224 int pos = 0;
4225
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004226 if (list)
4227 PyList_SetSlice(list, 0,
4228 ((PyVarObject*)list)->ob_size, 0);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004229 child = (PySymtableEntryObject *)\
4230 PyList_GET_ITEM(ste->ste_children, i);
4231 while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004232 int v = PyInt_AS_LONG(o);
4233 if (!(is_free(v)))
4234 continue; /* avoids indentation */
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004235 if (list == NULL) {
4236 list = PyList_New(0);
4237 if (list == NULL)
4238 return -1;
4239 }
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004240 ste->ste_child_free = 1;
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004241 if (PyList_Append(list, name) < 0) {
4242 Py_DECREF(list);
4243 return -1;
4244 }
4245 }
4246 for (j = 0; list && j < PyList_GET_SIZE(list); j++) {
4247 name = PyList_GET_ITEM(list, j);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004248 if (ste->ste_nested) {
4249 if (symtable_add_def_o(st, ste->ste_symbols,
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004250 name, def) < 0) {
4251 Py_DECREF(list);
4252 return -1;
4253 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004254 } else {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004255 if (symtable_check_global(st, child->ste_id,
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004256 name) < 0) {
4257 Py_DECREF(list);
4258 return -1;
4259 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004260 }
4261 }
4262 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004263
4264 Py_XDECREF(list);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004265 return 0;
4266}
4267
4268/* If the current scope is a non-nested class or if name is not
4269 defined in the current, non-nested scope, then it is an implicit
4270 global in all nested scopes.
4271*/
4272
4273static int
4274symtable_check_global(struct symtable *st, PyObject *child, PyObject *name)
4275{
4276 PyObject *o;
4277 int v;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004278 PySymtableEntryObject *ste = st->st_cur;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004279
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004280 if (ste->ste_type == TYPE_CLASS)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004281 return symtable_undo_free(st, child, name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004282 o = PyDict_GetItem(ste->ste_symbols, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004283 if (o == NULL)
4284 return symtable_undo_free(st, child, name);
4285 v = PyInt_AS_LONG(o);
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004286 if (is_free(v) || (v & DEF_GLOBAL))
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004287 return symtable_undo_free(st, child, name);
4288 else
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004289 return symtable_add_def_o(st, ste->ste_symbols,
4290 name, DEF_FREE);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004291}
4292
4293static int
4294symtable_undo_free(struct symtable *st, PyObject *id,
4295 PyObject *name)
4296{
4297 int i, v, x;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004298 PyObject *info;
4299 PySymtableEntryObject *ste;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004300
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004301 ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id);
4302 if (ste == NULL)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004303 return -1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004304 info = PyDict_GetItem(ste->ste_symbols, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004305 if (info == NULL)
4306 return 0;
4307 v = PyInt_AS_LONG(info);
4308 if (is_free(v)) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004309 if (symtable_add_def_o(st, ste->ste_symbols, name,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004310 DEF_FREE_GLOBAL) < 0)
4311 return -1;
4312 } else
4313 /* If the name is defined here or declared global,
4314 then the recursion stops. */
4315 return 0;
4316
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004317 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
4318 PySymtableEntryObject *child;
4319 child = (PySymtableEntryObject *) \
4320 PyList_GET_ITEM(ste->ste_children, i);
4321 x = symtable_undo_free(st, child->ste_id, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004322 if (x < 0)
4323 return x;
4324 }
4325 return 0;
4326}
4327
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004328static int
4329symtable_exit_scope(struct symtable *st)
4330{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004331 int end;
4332
4333 if (st->st_pass == 1)
4334 symtable_update_free_vars(st);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004335 Py_DECREF(st->st_cur);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004336 end = PyList_GET_SIZE(st->st_stack) - 1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004337 st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack,
4338 end);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004339 if (PySequence_DelItem(st->st_stack, end) < 0)
4340 return -1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004341 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004342}
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004343
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004344static void
4345symtable_enter_scope(struct symtable *st, char *name, int type,
4346 int lineno)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004347{
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004348 PySymtableEntryObject *prev = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004349
4350 if (st->st_cur) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004351 prev = st->st_cur;
4352 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
4353 Py_DECREF(st->st_cur);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004354 st->st_errors++;
4355 return;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004356 }
4357 }
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004358 st->st_cur = (PySymtableEntryObject *)\
4359 PySymtableEntry_New(st, name, type, lineno);
4360 if (strcmp(name, TOP) == 0)
4361 st->st_global = st->st_cur->ste_symbols;
4362 if (prev)
4363 if (PyList_Append(prev->ste_children,
4364 (PyObject *)st->st_cur) < 0)
4365 st->st_errors++;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004366}
4367
4368static int
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004369symtable_add_def(struct symtable *st, char *name, int flag)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004370{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004371 PyObject *s;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004372 char buffer[MANGLE_LEN];
4373
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004374 if (mangle(st->st_private, name, buffer, sizeof(buffer)))
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004375 name = buffer;
4376 if ((s = PyString_InternFromString(name)) == NULL)
4377 return -1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004378 return symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004379}
4380
4381/* Must only be called with mangled names */
4382
4383static int
4384symtable_add_def_o(struct symtable *st, PyObject *dict,
4385 PyObject *name, int flag)
4386{
4387 PyObject *o;
4388 int val;
4389
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004390 if ((o = PyDict_GetItem(dict, name))) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004391 val = PyInt_AS_LONG(o);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004392 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004393 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
Jeremy Hylton483638c2001-02-01 20:20:45 +00004394 PyString_AsString(name));
Jeremy Hylton5acc0c02001-02-02 20:01:10 +00004395 set_error_location(st->st_filename,
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004396 st->st_cur->ste_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004397 return -1;
4398 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004399 val |= flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004400 } else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004401 val = flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004402 o = PyInt_FromLong(val);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004403 if (PyDict_SetItem(dict, name, o) < 0) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004404 Py_DECREF(o);
4405 return -1;
4406 }
4407 Py_DECREF(o);
4408
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004409 if (flag & DEF_PARAM) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004410 if (PyList_Append(st->st_cur->ste_varnames, name) < 0)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004411 return -1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004412 } else if (flag & DEF_GLOBAL) {
4413 /* XXX need to update DEF_GLOBAL for other flags too;
4414 perhaps only DEF_FREE_GLOBAL */
4415 if ((o = PyDict_GetItem(st->st_global, name))) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004416 val = PyInt_AS_LONG(o);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004417 val |= flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004418 } else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004419 val = flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004420 o = PyInt_FromLong(val);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004421 if (PyDict_SetItem(st->st_global, name, o) < 0) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004422 Py_DECREF(o);
4423 return -1;
4424 }
4425 Py_DECREF(o);
4426 }
4427 return 0;
4428}
4429
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004430#define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004431
4432static void
4433symtable_node(struct symtable *st, node *n)
4434{
4435 int i, start = 0;
4436
4437 loop:
4438 switch (TYPE(n)) {
4439 case funcdef: {
4440 char *func_name = STR(CHILD(n, 1));
4441 symtable_add_def(st, func_name, DEF_LOCAL);
4442 symtable_default_args(st, CHILD(n, 2));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004443 symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004444 symtable_funcdef(st, n);
4445 symtable_exit_scope(st);
4446 break;
4447 }
4448 case lambdef:
4449 if (NCH(n) == 4)
4450 symtable_default_args(st, CHILD(n, 1));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004451 symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004452 symtable_funcdef(st, n);
4453 symtable_exit_scope(st);
4454 break;
4455 case classdef: {
4456 char *tmp, *class_name = STR(CHILD(n, 1));
4457 symtable_add_def(st, class_name, DEF_LOCAL);
4458 if (TYPE(CHILD(n, 2)) == LPAR) {
4459 node *bases = CHILD(n, 3);
4460 int i;
4461 for (i = 0; i < NCH(bases); i += 2) {
4462 symtable_node(st, CHILD(bases, i));
4463 }
4464 }
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004465 symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004466 tmp = st->st_private;
4467 st->st_private = class_name;
4468 symtable_node(st, CHILD(n, NCH(n) - 1));
4469 st->st_private = tmp;
4470 symtable_exit_scope(st);
4471 break;
4472 }
4473 case if_stmt:
4474 for (i = 0; i + 3 < NCH(n); i += 4) {
4475 if (is_constant_false(NULL, (CHILD(n, i + 1))))
4476 continue;
4477 symtable_node(st, CHILD(n, i + 1));
4478 symtable_node(st, CHILD(n, i + 3));
4479 }
4480 if (i + 2 < NCH(n))
4481 symtable_node(st, CHILD(n, i + 2));
4482 break;
4483 case global_stmt:
4484 symtable_global(st, n);
4485 break;
4486 case import_stmt:
4487 symtable_import(st, n);
4488 break;
Jeremy Hylton483638c2001-02-01 20:20:45 +00004489 case exec_stmt: {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004490 st->st_cur->ste_optimized = 0;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004491 symtable_node(st, CHILD(n, 1));
4492 if (NCH(n) > 2)
4493 symtable_node(st, CHILD(n, 3));
4494 if (NCH(n) > 4)
4495 symtable_node(st, CHILD(n, 5));
4496 break;
Jeremy Hylton483638c2001-02-01 20:20:45 +00004497
4498 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004499 case except_clause:
4500 if (NCH(n) == 4)
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004501 symtable_assign(st, CHILD(n, 3), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004502 if (NCH(n) > 1) {
4503 n = CHILD(n, 1);
4504 goto loop;
4505 }
4506 break;
4507 case del_stmt:
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004508 symtable_assign(st, CHILD(n, 1), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004509 break;
4510 case expr_stmt:
4511 if (NCH(n) == 1)
4512 n = CHILD(n, 0);
4513 else {
4514 if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004515 symtable_assign(st, CHILD(n, 0), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004516 symtable_node(st, CHILD(n, 2));
4517 break;
4518 } else {
4519 int i;
4520 for (i = 0; i < NCH(n) - 2; i += 2)
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004521 symtable_assign(st, CHILD(n, i), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004522 n = CHILD(n, NCH(n) - 1);
4523 }
4524 }
4525 goto loop;
4526 /* watchout for fall-through logic below */
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004527 case argument:
4528 if (NCH(n) == 3) {
4529 n = CHILD(n, 2);
4530 goto loop;
4531 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004532 case listmaker:
4533 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
4534 symtable_list_comprehension(st, CHILD(n, 1));
Jeremy Hylton5f827f42001-01-23 01:26:20 +00004535 n = CHILD(n, 0);
4536 goto loop;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004537 }
4538 case atom:
4539 if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) {
4540 symtable_add_use(st, STR(CHILD(n, 0)));
4541 break;
4542 }
4543 case for_stmt:
4544 if (TYPE(n) == for_stmt) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004545 symtable_assign(st, CHILD(n, 1), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004546 start = 3;
4547 }
4548 default:
4549 if (NCH(n) == 1) {
4550 n = CHILD(n, 0);
4551 goto loop;
4552 }
4553 for (i = start; i < NCH(n); ++i)
4554 if (TYPE(CHILD(n, i)) >= single_input)
4555 symtable_node(st, CHILD(n, i));
4556 }
4557}
4558
4559static void
4560symtable_funcdef(struct symtable *st, node *n)
4561{
4562 node *body;
4563
4564 if (TYPE(n) == lambdef) {
4565 if (NCH(n) == 4)
4566 symtable_params(st, CHILD(n, 1));
4567 } else
4568 symtable_params(st, CHILD(n, 2));
4569 body = CHILD(n, NCH(n) - 1);
4570 symtable_node(st, body);
4571}
4572
4573/* The next two functions parse the argument tuple.
4574 symtable_default_arg() checks for names in the default arguments,
4575 which are references in the defining scope. symtable_params()
4576 parses the parameter names, which are defined in the function's
4577 body.
4578
4579 varargslist:
4580 (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
4581 | fpdef ['=' test] (',' fpdef ['=' test])* [',']
4582*/
4583
4584static void
4585symtable_default_args(struct symtable *st, node *n)
4586{
4587 node *c;
4588 int i;
4589
4590 if (TYPE(n) == parameters) {
4591 n = CHILD(n, 1);
4592 if (TYPE(n) == RPAR)
4593 return;
4594 }
4595 REQ(n, varargslist);
4596 for (i = 0; i < NCH(n); i += 2) {
4597 c = CHILD(n, i);
4598 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
4599 break;
4600 }
4601 if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL))
4602 symtable_node(st, CHILD(n, i));
4603 }
4604}
4605
4606static void
4607symtable_params(struct symtable *st, node *n)
4608{
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00004609 int i, complex = -1, ext = 0;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004610 node *c = NULL;
4611
4612 if (TYPE(n) == parameters) {
4613 n = CHILD(n, 1);
4614 if (TYPE(n) == RPAR)
4615 return;
4616 }
4617 REQ(n, varargslist);
4618 for (i = 0; i < NCH(n); i += 2) {
4619 c = CHILD(n, i);
4620 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
4621 ext = 1;
4622 break;
4623 }
4624 if (TYPE(c) == test) {
4625 continue;
4626 }
4627 if (TYPE(CHILD(c, 0)) == NAME)
4628 symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM);
4629 else {
4630 char nbuf[10];
4631 sprintf(nbuf, ".%d", i);
4632 symtable_add_def(st, nbuf, DEF_PARAM);
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00004633 complex = i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004634 }
4635 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004636 if (ext) {
4637 c = CHILD(n, i);
4638 if (TYPE(c) == STAR) {
4639 i++;
4640 symtable_add_def(st, STR(CHILD(n, i)),
4641 DEF_PARAM | DEF_STAR);
4642 i += 2;
Jeremy Hylton1113cfc2001-01-23 00:50:52 +00004643 if (i >= NCH(n))
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00004644 c = NULL;
4645 else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004646 c = CHILD(n, i);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004647 }
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00004648 if (c && TYPE(c) == DOUBLESTAR) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004649 i++;
4650 symtable_add_def(st, STR(CHILD(n, i)),
4651 DEF_PARAM | DEF_DOUBLESTAR);
4652 }
4653 }
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00004654 if (complex >= 0) {
4655 int j;
4656 for (j = 0; j <= complex; j++) {
4657 c = CHILD(n, j);
4658 if (TYPE(c) == COMMA)
4659 c = CHILD(n, ++j);
4660 if (TYPE(CHILD(c, 0)) == LPAR)
4661 symtable_params_fplist(st, CHILD(c, 1));
4662 }
4663 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004664}
4665
4666static void
4667symtable_params_fplist(struct symtable *st, node *n)
4668{
4669 int i;
4670 node *c;
4671
4672 REQ(n, fplist);
4673 for (i = 0; i < NCH(n); i += 2) {
4674 c = CHILD(n, i);
4675 REQ(c, fpdef);
4676 if (NCH(c) == 1)
4677 symtable_add_def(st, STR(CHILD(c, 0)),
4678 DEF_PARAM | DEF_INTUPLE);
4679 else
4680 symtable_params_fplist(st, CHILD(c, 1));
4681 }
4682
4683}
4684
4685static void
4686symtable_global(struct symtable *st, node *n)
4687{
4688 int i;
4689
4690 for (i = 1; i < NCH(n); i += 2)
4691 symtable_add_def(st, STR(CHILD(n, i)), DEF_GLOBAL);
4692}
4693
4694static void
4695symtable_list_comprehension(struct symtable *st, node *n)
4696{
4697 char tmpname[12];
4698
Tim Peters30814212001-02-17 05:30:26 +00004699 sprintf(tmpname, "_[%d]", ++st->st_tmpname);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004700 symtable_add_def(st, tmpname, DEF_LOCAL);
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004701 symtable_assign(st, CHILD(n, 1), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004702 symtable_node(st, CHILD(n, 3));
4703 if (NCH(n) == 5)
4704 symtable_node(st, CHILD(n, 4));
4705 --st->st_tmpname;
4706}
4707
4708static void
4709symtable_import(struct symtable *st, node *n)
4710{
4711 int i;
Jeremy Hylton483638c2001-02-01 20:20:45 +00004712 /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004713 | 'from' dotted_name 'import'
4714 ('*' | import_as_name (',' import_as_name)*)
Jeremy Hylton483638c2001-02-01 20:20:45 +00004715 import_as_name: NAME [NAME NAME]
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004716 */
4717
4718 if (STR(CHILD(n, 0))[0] == 'f') { /* from */
4719 if (TYPE(CHILD(n, 3)) == STAR) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004720 st->st_cur->ste_optimized = 0;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004721 } else {
4722 for (i = 3; i < NCH(n); i += 2) {
4723 node *c = CHILD(n, i);
4724 if (NCH(c) > 1) /* import as */
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004725 symtable_assign(st, CHILD(c, 2),
4726 DEF_IMPORT);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004727 else
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004728 symtable_assign(st, CHILD(c, 0),
4729 DEF_IMPORT);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004730 }
4731 }
4732 } else {
4733 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004734 symtable_assign(st, CHILD(n, i), DEF_IMPORT);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004735 }
4736 }
4737}
4738
4739static void
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004740symtable_assign(struct symtable *st, node *n, int flag)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004741{
4742 node *tmp;
4743 int i;
4744
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004745 loop:
4746 switch (TYPE(n)) {
4747 case lambdef:
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004748 /* invalid assignment, e.g. lambda x:x=2. The next
4749 pass will catch this error. */
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004750 return;
4751 case power:
4752 if (NCH(n) > 2) {
4753 for (i = 2; i < NCH(n); ++i)
4754 if (TYPE(CHILD(n, i)) != DOUBLESTAR)
4755 symtable_node(st, CHILD(n, i));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004756 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004757 if (NCH(n) > 1) {
4758 symtable_node(st, CHILD(n, 0));
4759 symtable_node(st, CHILD(n, 1));
4760 } else {
4761 n = CHILD(n, 0);
4762 goto loop;
4763 }
4764 return;
4765 case listmaker:
4766 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for)
4767 symtable_list_comprehension(st, CHILD(n, 1));
4768 else {
4769 for (i = 0; i < NCH(n); i += 2)
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004770 symtable_assign(st, CHILD(n, i), flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004771 }
4772 return;
4773 case exprlist:
4774 case testlist:
4775 if (NCH(n) == 1) {
4776 n = CHILD(n, 0);
4777 goto loop;
4778 }
4779 else {
4780 int i;
4781 for (i = 0; i < NCH(n); i += 2)
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004782 symtable_assign(st, CHILD(n, i), flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004783 return;
4784 }
4785 goto loop;
4786 case atom:
4787 tmp = CHILD(n, 0);
4788 if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) {
4789 n = CHILD(n, 1);
4790 goto loop;
4791 } else if (TYPE(tmp) == NAME)
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004792 symtable_add_def(st, STR(tmp), DEF_LOCAL | flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004793 return;
4794 case dotted_as_name:
4795 if (NCH(n) == 3)
4796 symtable_add_def(st, STR(CHILD(n, 2)),
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004797 DEF_LOCAL | flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004798 else
4799 symtable_add_def(st,
4800 STR(CHILD(CHILD(n,
4801 0), 0)),
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004802 DEF_LOCAL | flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004803 return;
4804 case dotted_name:
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004805 symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004806 return;
4807 case NAME:
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004808 symtable_add_def(st, STR(n), DEF_LOCAL | flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004809 return;
4810 default:
4811 if (NCH(n) == 0)
4812 return;
Jeremy Hylton17820c42001-02-19 15:33:10 +00004813 if (NCH(n) == 1) {
4814 n = CHILD(n, 0);
4815 goto loop;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004816 }
Jeremy Hylton17820c42001-02-19 15:33:10 +00004817 /* Should only occur for errors like x + 1 = 1,
4818 which will be caught in the next pass. */
4819 for (i = 0; i < NCH(n); ++i)
4820 if (TYPE(CHILD(n, i)) >= single_input)
4821 symtable_assign(st, CHILD(n, i), flag);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004822 }
4823}