blob: aca1705b211a853e6afd63a7a6a54fc446be46a1 [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'
Guido van Rossum8b993a91997-01-17 21:04:03 +00008 XXX Allow 'continue' inside 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 Rossum8ff077b1996-08-24 06:21:31 +000013#ifndef NO_PRIVATE_NAME_MANGLING
14#define PRIVATE_NAME_MANGLING
15#endif
16
Guido van Rossum79f25d91997-04-29 20:08:16 +000017#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000018
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019#include "node.h"
20#include "token.h"
21#include "graminit.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000022#include "compile.h"
23#include "opcode.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000024#include "structmember.h"
25
26#include <ctype.h>
Guido van Rossum282914b1991-04-04 10:42:56 +000027
Guido van Rossumb05a5c71997-05-07 17:46:13 +000028/* Three symbols from graminit.h are also defined in Python.h, with
29 Py_ prefixes to their names. Python.h can't include graminit.h
30 (which defines too many confusing symbols), but we can check here
31 that they haven't changed (which is very unlikely, but possible). */
32#if Py_single_input != single_input
Guido van Rossum2f75b291997-05-20 22:18:48 +000033 #error "single_input has changed -- update Py_single_input in Python.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000034#endif
35#if Py_file_input != file_input
Guido van Rossum2f75b291997-05-20 22:18:48 +000036 #error "file_input has changed -- update Py_file_input in Python.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000037#endif
38#if Py_eval_input != eval_input
Guido van Rossum2f75b291997-05-20 22:18:48 +000039 #error "eval_input has changed -- update Py_eval_input in Python.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000040#endif
41
Guido van Rossum8e793d91997-03-03 19:13:14 +000042int Py_OptimizeFlag = 0;
43
Guido van Rossum8861b741996-07-30 16:49:37 +000044#define OP_DELETE 0
45#define OP_ASSIGN 1
46#define OP_APPLY 2
47
Guido van Rossum79f25d91997-04-29 20:08:16 +000048#define OFF(x) offsetof(PyCodeObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +000049
50static struct memberlist code_memberlist[] = {
Guido van Rossum681d79a1995-07-18 14:51:37 +000051 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
52 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
Guido van Rossum8b993a91997-01-17 21:04:03 +000053 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
Guido van Rossum681d79a1995-07-18 14:51:37 +000054 {"co_flags", T_INT, OFF(co_flags), READONLY},
Guido van Rossum39d942d1992-01-12 02:30:05 +000055 {"co_code", T_OBJECT, OFF(co_code), READONLY},
56 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
57 {"co_names", T_OBJECT, OFF(co_names), READONLY},
Guido van Rossum681d79a1995-07-18 14:51:37 +000058 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
Guido van Rossum39d942d1992-01-12 02:30:05 +000059 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
Guido van Rossum9bfef441993-03-29 10:43:31 +000060 {"co_name", T_OBJECT, OFF(co_name), READONLY},
Guido van Rossumda4eb5c1997-01-24 03:43:35 +000061 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
62 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
Guido van Rossum3f5da241990-12-20 15:06:42 +000063 {NULL} /* Sentinel */
64};
65
Guido van Rossum79f25d91997-04-29 20:08:16 +000066static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000067code_getattr(PyCodeObject *co, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +000068{
Guido van Rossum79f25d91997-04-29 20:08:16 +000069 return PyMember_Get((char *)co, code_memberlist, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +000070}
Guido van Rossum10dc2e81990-11-18 17:27:39 +000071
72static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000073code_dealloc(PyCodeObject *co)
Guido van Rossum10dc2e81990-11-18 17:27:39 +000074{
Guido van Rossum79f25d91997-04-29 20:08:16 +000075 Py_XDECREF(co->co_code);
76 Py_XDECREF(co->co_consts);
77 Py_XDECREF(co->co_names);
Guido van Rossum275558c1997-07-25 20:13:49 +000078 Py_XDECREF(co->co_varnames);
Guido van Rossum79f25d91997-04-29 20:08:16 +000079 Py_XDECREF(co->co_filename);
80 Py_XDECREF(co->co_name);
Guido van Rossum275558c1997-07-25 20:13:49 +000081 Py_XDECREF(co->co_lnotab);
Guido van Rossumb18618d2000-05-03 23:44:39 +000082 PyObject_DEL(co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +000083}
84
Guido van Rossum79f25d91997-04-29 20:08:16 +000085static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000086code_repr(PyCodeObject *co)
Guido van Rossum2dff9911992-09-03 20:50:59 +000087{
88 char buf[500];
89 int lineno = -1;
Guido van Rossum2dff9911992-09-03 20:50:59 +000090 char *filename = "???";
Guido van Rossum9bfef441993-03-29 10:43:31 +000091 char *name = "???";
Guido van Rossumd076c731998-10-07 19:42:25 +000092
Guido van Rossuma396a882000-04-07 01:21:36 +000093 if (co->co_firstlineno != 0)
94 lineno = co->co_firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +000095 if (co->co_filename && PyString_Check(co->co_filename))
96 filename = PyString_AsString(co->co_filename);
97 if (co->co_name && PyString_Check(co->co_name))
98 name = PyString_AsString(co->co_name);
Fred Drake615ae552000-06-30 16:20:13 +000099 sprintf(buf, "<code object %.100s at %p, file \"%.300s\", line %d>",
100 name, co, filename, lineno);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000101 return PyString_FromString(buf);
Guido van Rossum2dff9911992-09-03 20:50:59 +0000102}
103
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000104static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000105code_compare(PyCodeObject *co, PyCodeObject *cp)
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000106{
107 int cmp;
Guido van Rossum44679592000-04-10 16:20:31 +0000108 cmp = PyObject_Compare(co->co_name, cp->co_name);
109 if (cmp) return cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000110 cmp = co->co_argcount - cp->co_argcount;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000111 if (cmp) return cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000112 cmp = co->co_nlocals - cp->co_nlocals;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000113 if (cmp) return cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000114 cmp = co->co_flags - cp->co_flags;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000115 if (cmp) return cmp;
Guido van Rossumd076c731998-10-07 19:42:25 +0000116 cmp = PyObject_Compare(co->co_code, cp->co_code);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000117 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000118 cmp = PyObject_Compare(co->co_consts, cp->co_consts);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000119 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000120 cmp = PyObject_Compare(co->co_names, cp->co_names);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000121 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000122 cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000123 return cmp;
124}
125
126static long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000127code_hash(PyCodeObject *co)
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000128{
Guido van Rossum44679592000-04-10 16:20:31 +0000129 long h, h0, h1, h2, h3, h4;
130 h0 = PyObject_Hash(co->co_name);
131 if (h0 == -1) return -1;
Guido van Rossumd076c731998-10-07 19:42:25 +0000132 h1 = PyObject_Hash(co->co_code);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000133 if (h1 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000134 h2 = PyObject_Hash(co->co_consts);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000135 if (h2 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000136 h3 = PyObject_Hash(co->co_names);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000137 if (h3 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000138 h4 = PyObject_Hash(co->co_varnames);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000139 if (h4 == -1) return -1;
Guido van Rossum44679592000-04-10 16:20:31 +0000140 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^
Guido van Rossum681d79a1995-07-18 14:51:37 +0000141 co->co_argcount ^ co->co_nlocals ^ co->co_flags;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000142 if (h == -1) h = -2;
143 return h;
144}
145
Guido van Rossum79f25d91997-04-29 20:08:16 +0000146PyTypeObject PyCode_Type = {
147 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000148 0,
149 "code",
Guido van Rossum79f25d91997-04-29 20:08:16 +0000150 sizeof(PyCodeObject),
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000151 0,
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000152 (destructor)code_dealloc, /*tp_dealloc*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000153 0, /*tp_print*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000154 (getattrfunc)code_getattr, /*tp_getattr*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000155 0, /*tp_setattr*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000156 (cmpfunc)code_compare, /*tp_compare*/
157 (reprfunc)code_repr, /*tp_repr*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000158 0, /*tp_as_number*/
159 0, /*tp_as_sequence*/
160 0, /*tp_as_mapping*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000161 (hashfunc)code_hash, /*tp_hash*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000162};
163
Guido van Rossum644a12b1997-04-09 19:24:53 +0000164#define NAME_CHARS \
165 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
166
Guido van Rossum79f25d91997-04-29 20:08:16 +0000167PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000168PyCode_New(int argcount, int nlocals, int stacksize, int flags,
169 PyObject *code, PyObject *consts, PyObject *names,
170 PyObject *varnames, PyObject *filename, PyObject *name,
171 int firstlineno, PyObject *lnotab)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000172{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000173 PyCodeObject *co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000174 int i;
Guido van Rossumd076c731998-10-07 19:42:25 +0000175 PyBufferProcs *pb;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000176 /* Check argument types */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000177 if (argcount < 0 || nlocals < 0 ||
Guido van Rossumd076c731998-10-07 19:42:25 +0000178 code == NULL ||
Guido van Rossum79f25d91997-04-29 20:08:16 +0000179 consts == NULL || !PyTuple_Check(consts) ||
180 names == NULL || !PyTuple_Check(names) ||
181 varnames == NULL || !PyTuple_Check(varnames) ||
182 name == NULL || !PyString_Check(name) ||
183 filename == NULL || !PyString_Check(filename) ||
184 lnotab == NULL || !PyString_Check(lnotab)) {
185 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000186 return NULL;
187 }
Guido van Rossumd076c731998-10-07 19:42:25 +0000188 pb = code->ob_type->tp_as_buffer;
189 if (pb == NULL ||
190 pb->bf_getreadbuffer == NULL ||
191 pb->bf_getsegcount == NULL ||
192 (*pb->bf_getsegcount)(code, NULL) != 1)
193 {
194 PyErr_BadInternalCall();
195 return NULL;
196 }
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000197 /* Make sure names and varnames are all strings, & intern them */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000198 for (i = PyTuple_Size(names); --i >= 0; ) {
199 PyObject *v = PyTuple_GetItem(names, i);
200 if (v == NULL || !PyString_Check(v)) {
201 PyErr_BadInternalCall();
Guido van Rossum681d79a1995-07-18 14:51:37 +0000202 return NULL;
203 }
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000204 PyString_InternInPlace(&PyTuple_GET_ITEM(names, i));
Guido van Rossum681d79a1995-07-18 14:51:37 +0000205 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000206 for (i = PyTuple_Size(varnames); --i >= 0; ) {
207 PyObject *v = PyTuple_GetItem(varnames, i);
208 if (v == NULL || !PyString_Check(v)) {
209 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000210 return NULL;
211 }
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000212 PyString_InternInPlace(&PyTuple_GET_ITEM(varnames, i));
213 }
214 /* Intern selected string constants */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000215 for (i = PyTuple_Size(consts); --i >= 0; ) {
216 PyObject *v = PyTuple_GetItem(consts, i);
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000217 char *p;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000218 if (!PyString_Check(v))
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000219 continue;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000220 p = PyString_AsString(v);
Guido van Rossum582acec2000-06-28 22:07:35 +0000221 if (strspn(p, NAME_CHARS)
222 != (size_t)PyString_Size(v))
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000223 continue;
224 PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000225 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000226 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000227 if (co != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +0000228 co->co_argcount = argcount;
229 co->co_nlocals = nlocals;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000230 co->co_stacksize = stacksize;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000231 co->co_flags = flags;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000232 Py_INCREF(code);
Guido van Rossumd076c731998-10-07 19:42:25 +0000233 co->co_code = code;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000234 Py_INCREF(consts);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000235 co->co_consts = consts;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000236 Py_INCREF(names);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000237 co->co_names = names;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000238 Py_INCREF(varnames);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000239 co->co_varnames = varnames;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000240 Py_INCREF(filename);
Guido van Rossuma082ce41991-06-04 19:41:56 +0000241 co->co_filename = filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000242 Py_INCREF(name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000243 co->co_name = name;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000244 co->co_firstlineno = firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000245 Py_INCREF(lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000246 co->co_lnotab = lnotab;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000247 }
248 return co;
249}
250
251
252/* Data structure used internally */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000253
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000254struct compiling {
Fred Drakefd1f1be2000-09-08 16:31:24 +0000255 PyObject *c_code; /* string */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000256 PyObject *c_consts; /* list of objects */
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000257 PyObject *c_const_dict; /* inverse of c_consts */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000258 PyObject *c_names; /* list of strings (names) */
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000259 PyObject *c_name_dict; /* inverse of c_names */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000260 PyObject *c_globals; /* dictionary (value=None) */
261 PyObject *c_locals; /* dictionary (value=localID) */
262 PyObject *c_varnames; /* list (inverse of c_locals) */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000263 int c_nlocals; /* index of next local */
264 int c_argcount; /* number of top-level arguments */
265 int c_flags; /* same as co_flags */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000266 int c_nexti; /* index into c_code */
267 int c_errors; /* counts errors occurred */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000268 int c_infunction; /* set when compiling a function */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000269 int c_interactive; /* generating code for interactive command */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000270 int c_loops; /* counts nested loops */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000271 int c_begin; /* begin of current loop, for 'continue' */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000272 int c_block[CO_MAXBLOCKS]; /* stack of block types */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000273 int c_nblocks; /* current block stack level */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000274 char *c_filename; /* filename of current node */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000275 char *c_name; /* name of object (e.g. function) */
Guido van Rossum452a9831996-09-17 14:32:04 +0000276 int c_lineno; /* Current line number */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000277 int c_stacklevel; /* Current stack level */
278 int c_maxstacklevel; /* Maximum stack level */
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000279 int c_firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000280 PyObject *c_lnotab; /* Table mapping address to line number */
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000281 int c_last_addr, c_last_line, c_lnotab_next;
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000282#ifdef PRIVATE_NAME_MANGLING
283 char *c_private; /* for private name mangling */
284#endif
Skip Montanaro803d6e52000-08-12 18:09:51 +0000285 int c_tmpname; /* temporary local name counter */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000286};
287
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000288
Guido van Rossum452a9831996-09-17 14:32:04 +0000289/* Error message including line number */
290
291static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000292com_error(struct compiling *c, PyObject *exc, char *msg)
Guido van Rossum452a9831996-09-17 14:32:04 +0000293{
Fred Drakedcf08e02000-08-15 15:49:44 +0000294 PyObject *v, *tb, *tmp;
Guido van Rossum635abd21997-01-06 22:56:52 +0000295 c->c_errors++;
Guido van Rossum452a9831996-09-17 14:32:04 +0000296 if (c->c_lineno <= 1) {
297 /* Unknown line number or single interactive command */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000298 PyErr_SetString(exc, msg);
Guido van Rossum452a9831996-09-17 14:32:04 +0000299 return;
300 }
Fred Drakedcf08e02000-08-15 15:49:44 +0000301 v = PyString_FromString(msg);
Guido van Rossum452a9831996-09-17 14:32:04 +0000302 if (v == NULL)
303 return; /* MemoryError, too bad */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000304 PyErr_SetObject(exc, v);
305 Py_DECREF(v);
Fred Drakedcf08e02000-08-15 15:49:44 +0000306
307 /* add attributes for the line number and filename for the error */
308 PyErr_Fetch(&exc, &v, &tb);
309 PyErr_NormalizeException(&exc, &v, &tb);
310 tmp = PyInt_FromLong(c->c_lineno);
311 if (tmp == NULL)
312 PyErr_Clear();
313 else {
314 if (PyObject_SetAttrString(v, "lineno", tmp))
315 PyErr_Clear();
316 Py_DECREF(tmp);
317 }
318 if (c->c_filename != NULL) {
319 tmp = PyString_FromString(c->c_filename);
320 if (tmp == NULL)
321 PyErr_Clear();
322 else {
323 if (PyObject_SetAttrString(v, "filename", tmp))
324 PyErr_Clear();
325 Py_DECREF(tmp);
326 }
327 }
328 PyErr_Restore(exc, v, tb);
Guido van Rossum452a9831996-09-17 14:32:04 +0000329}
330
331
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000332/* Interface to the block stack */
333
334static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000335block_push(struct compiling *c, int type)
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000336{
Guido van Rossum8b993a91997-01-17 21:04:03 +0000337 if (c->c_nblocks >= CO_MAXBLOCKS) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000338 com_error(c, PyExc_SystemError,
339 "too many statically nested blocks");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000340 }
341 else {
342 c->c_block[c->c_nblocks++] = type;
343 }
344}
345
346static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000347block_pop(struct compiling *c, int type)
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000348{
349 if (c->c_nblocks > 0)
350 c->c_nblocks--;
351 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000352 com_error(c, PyExc_SystemError, "bad block pop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000353 }
354}
355
356
Guido van Rossum681d79a1995-07-18 14:51:37 +0000357/* Prototype forward declarations */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000358
Tim Petersdbd9ba62000-07-09 03:09:57 +0000359static int com_init(struct compiling *, char *);
360static void com_free(struct compiling *);
361static void com_push(struct compiling *, int);
362static void com_pop(struct compiling *, int);
363static void com_done(struct compiling *);
364static void com_node(struct compiling *, struct _node *);
365static void com_factor(struct compiling *, struct _node *);
366static void com_addbyte(struct compiling *, int);
367static void com_addint(struct compiling *, int);
368static void com_addoparg(struct compiling *, int, int);
369static void com_addfwref(struct compiling *, int, int *);
370static void com_backpatch(struct compiling *, int);
371static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *);
372static int com_addconst(struct compiling *, PyObject *);
373static int com_addname(struct compiling *, PyObject *);
374static void com_addopname(struct compiling *, int, node *);
375static void com_list(struct compiling *, node *, int);
Skip Montanaro803d6e52000-08-12 18:09:51 +0000376static void com_list_iter(struct compiling *, node *, node *, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000377static int com_argdefs(struct compiling *, node *);
378static int com_newlocal(struct compiling *, char *);
Thomas Wouters434d0822000-08-24 20:11:32 +0000379static void com_assign(struct compiling *, node *, int, node *);
380static void com_assign_name(struct compiling *, node *, int);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000381static PyCodeObject *icompile(struct _node *, struct compiling *);
382static PyCodeObject *jcompile(struct _node *, char *,
383 struct compiling *);
384static PyObject *parsestrplus(node *);
385static PyObject *parsestr(char *);
Thomas Wouters434d0822000-08-24 20:11:32 +0000386static node *get_rawdocstring(node *);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000387
388static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000389com_init(struct compiling *c, char *filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000390{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000391 memset((void *)c, '\0', sizeof(struct compiling));
Guido van Rossum79f25d91997-04-29 20:08:16 +0000392 if ((c->c_code = PyString_FromStringAndSize((char *)NULL,
393 1000)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000394 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000395 if ((c->c_consts = PyList_New(0)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000396 goto fail;
397 if ((c->c_const_dict = PyDict_New()) == NULL)
398 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000399 if ((c->c_names = PyList_New(0)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000400 goto fail;
401 if ((c->c_name_dict = PyDict_New()) == NULL)
402 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000403 if ((c->c_globals = PyDict_New()) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000404 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000405 if ((c->c_locals = PyDict_New()) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000406 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000407 if ((c->c_varnames = PyList_New(0)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000408 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000409 if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL,
410 1000)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000411 goto fail;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000412 c->c_nlocals = 0;
413 c->c_argcount = 0;
414 c->c_flags = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000415 c->c_nexti = 0;
416 c->c_errors = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000417 c->c_infunction = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000418 c->c_interactive = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000419 c->c_loops = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000420 c->c_begin = 0;
421 c->c_nblocks = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000422 c->c_filename = filename;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000423 c->c_name = "?";
Guido van Rossum452a9831996-09-17 14:32:04 +0000424 c->c_lineno = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000425 c->c_stacklevel = 0;
426 c->c_maxstacklevel = 0;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000427 c->c_firstlineno = 0;
428 c->c_last_addr = 0;
429 c->c_last_line = 0;
430 c-> c_lnotab_next = 0;
Skip Montanaro803d6e52000-08-12 18:09:51 +0000431 c->c_tmpname = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000432 return 1;
433
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000434 fail:
435 com_free(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000436 return 0;
437}
438
439static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000440com_free(struct compiling *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000441{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000442 Py_XDECREF(c->c_code);
443 Py_XDECREF(c->c_consts);
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000444 Py_XDECREF(c->c_const_dict);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000445 Py_XDECREF(c->c_names);
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000446 Py_XDECREF(c->c_name_dict);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000447 Py_XDECREF(c->c_globals);
448 Py_XDECREF(c->c_locals);
449 Py_XDECREF(c->c_varnames);
450 Py_XDECREF(c->c_lnotab);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000451}
452
453static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000454com_push(struct compiling *c, int n)
Guido van Rossum8b993a91997-01-17 21:04:03 +0000455{
456 c->c_stacklevel += n;
457 if (c->c_stacklevel > c->c_maxstacklevel)
458 c->c_maxstacklevel = c->c_stacklevel;
459}
460
461static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000462com_pop(struct compiling *c, int n)
Guido van Rossum8b993a91997-01-17 21:04:03 +0000463{
464 if (c->c_stacklevel < n) {
Guido van Rossum90f827c1998-08-25 18:22:17 +0000465 /* fprintf(stderr,
Guido van Rossum8b993a91997-01-17 21:04:03 +0000466 "%s:%d: underflow! nexti=%d, level=%d, n=%d\n",
467 c->c_filename, c->c_lineno,
Guido van Rossum90f827c1998-08-25 18:22:17 +0000468 c->c_nexti, c->c_stacklevel, n); */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000469 c->c_stacklevel = 0;
470 }
471 else
472 c->c_stacklevel -= n;
473}
474
475static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000476com_done(struct compiling *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000477{
478 if (c->c_code != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000479 _PyString_Resize(&c->c_code, c->c_nexti);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000480 if (c->c_lnotab != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000481 _PyString_Resize(&c->c_lnotab, c->c_lnotab_next);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000482}
483
484static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000485com_addbyte(struct compiling *c, int byte)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000486{
487 int len;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000488 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000489 if (byte < 0 || byte > 255) {
Guido van Rossum01cfd441991-10-20 20:12:38 +0000490 /*
Guido van Rossum3f5da241990-12-20 15:06:42 +0000491 fprintf(stderr, "XXX compiling bad byte: %d\n", byte);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000492 fatal("com_addbyte: byte out of range");
Guido van Rossum01cfd441991-10-20 20:12:38 +0000493 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000494 com_error(c, PyExc_SystemError,
495 "com_addbyte: byte out of range");
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000496 }
497 if (c->c_code == NULL)
498 return;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000499 len = PyString_Size(c->c_code);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000500 if (c->c_nexti >= len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000501 if (_PyString_Resize(&c->c_code, len+1000) != 0) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000502 c->c_errors++;
503 return;
504 }
505 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000506 PyString_AsString(c->c_code)[c->c_nexti++] = byte;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000507}
508
509static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000510com_addint(struct compiling *c, int x)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000511{
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000512 com_addbyte(c, x & 0xff);
513 com_addbyte(c, x >> 8); /* XXX x should be positive */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000514}
515
516static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000517com_add_lnotab(struct compiling *c, int addr, int line)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000518{
519 int size;
520 char *p;
521 if (c->c_lnotab == NULL)
522 return;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000523 size = PyString_Size(c->c_lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000524 if (c->c_lnotab_next+2 > size) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000525 if (_PyString_Resize(&c->c_lnotab, size + 1000) < 0) {
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000526 c->c_errors++;
527 return;
528 }
529 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000530 p = PyString_AsString(c->c_lnotab) + c->c_lnotab_next;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000531 *p++ = addr;
532 *p++ = line;
533 c->c_lnotab_next += 2;
534}
535
536static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000537com_set_lineno(struct compiling *c, int lineno)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000538{
539 c->c_lineno = lineno;
540 if (c->c_firstlineno == 0) {
541 c->c_firstlineno = c->c_last_line = lineno;
542 }
543 else {
544 int incr_addr = c->c_nexti - c->c_last_addr;
545 int incr_line = lineno - c->c_last_line;
546 while (incr_addr > 0 || incr_line > 0) {
547 int trunc_addr = incr_addr;
548 int trunc_line = incr_line;
549 if (trunc_addr > 255)
550 trunc_addr = 255;
551 if (trunc_line > 255)
552 trunc_line = 255;
553 com_add_lnotab(c, trunc_addr, trunc_line);
554 incr_addr -= trunc_addr;
555 incr_line -= trunc_line;
556 }
557 c->c_last_addr = c->c_nexti;
558 c->c_last_line = lineno;
559 }
560}
561
562static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000563com_addoparg(struct compiling *c, int op, int arg)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000564{
Fred Drakeef8ace32000-08-24 00:32:09 +0000565 int extended_arg = arg >> 16;
Guido van Rossum8e793d91997-03-03 19:13:14 +0000566 if (op == SET_LINENO) {
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000567 com_set_lineno(c, arg);
Guido van Rossum8e793d91997-03-03 19:13:14 +0000568 if (Py_OptimizeFlag)
569 return;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000570 }
Fred Drakeef8ace32000-08-24 00:32:09 +0000571 if (extended_arg){
572 com_addbyte(c, EXTENDED_ARG);
573 com_addint(c, extended_arg);
574 arg &= 0xffff;
575 }
Guido van Rossum8e793d91997-03-03 19:13:14 +0000576 com_addbyte(c, op);
577 com_addint(c, arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000578}
579
580static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000581com_addfwref(struct compiling *c, int op, int *p_anchor)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000582{
583 /* Compile a forward reference for backpatching */
584 int here;
585 int anchor;
586 com_addbyte(c, op);
587 here = c->c_nexti;
588 anchor = *p_anchor;
589 *p_anchor = here;
590 com_addint(c, anchor == 0 ? 0 : here - anchor);
591}
592
593static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000594com_backpatch(struct compiling *c, int anchor)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000595{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000596 unsigned char *code = (unsigned char *) PyString_AsString(c->c_code);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000597 int target = c->c_nexti;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000598 int dist;
599 int prev;
600 for (;;) {
601 /* Make the JUMP instruction at anchor point to target */
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000602 prev = code[anchor] + (code[anchor+1] << 8);
603 dist = target - (anchor+2);
604 code[anchor] = dist & 0xff;
Fred Drakeef8ace32000-08-24 00:32:09 +0000605 dist >>= 8;
606 code[anchor+1] = dist;
607 dist >>= 8;
608 if (dist) {
609 com_error(c, PyExc_SystemError,
610 "com_backpatch: offset too large");
611 break;
612 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000613 if (!prev)
614 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000615 anchor -= prev;
616 }
617}
618
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000619/* Handle literals and names uniformly */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000620
621static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000622com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000623{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000624 PyObject *w, *t, *np=NULL;
625 long n;
626
627 t = Py_BuildValue("(OO)", v, v->ob_type);
628 if (t == NULL)
629 goto fail;
630 w = PyDict_GetItem(dict, t);
631 if (w != NULL) {
632 n = PyInt_AsLong(w);
633 } else {
634 n = PyList_Size(list);
635 np = PyInt_FromLong(n);
636 if (np == NULL)
637 goto fail;
638 if (PyList_Append(list, v) != 0)
639 goto fail;
640 if (PyDict_SetItem(dict, t, np) != 0)
641 goto fail;
642 Py_DECREF(np);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000643 }
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000644 Py_DECREF(t);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000645 return n;
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000646 fail:
647 Py_XDECREF(np);
648 Py_XDECREF(t);
649 c->c_errors++;
650 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000651}
652
653static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000654com_addconst(struct compiling *c, PyObject *v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000655{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000656 return com_add(c, c->c_consts, c->c_const_dict, v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000657}
658
659static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000660com_addname(struct compiling *c, PyObject *v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000661{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000662 return com_add(c, c->c_names, c->c_name_dict, v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000663}
664
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000665#ifdef PRIVATE_NAME_MANGLING
666static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000667com_mangle(struct compiling *c, char *name, char *buffer, size_t maxlen)
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000668{
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000669 /* Name mangling: __private becomes _classname__private.
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000670 This is independent from how the name is used. */
671 char *p;
Guido van Rossum582acec2000-06-28 22:07:35 +0000672 size_t nlen, plen;
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000673 nlen = strlen(name);
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000674 if (nlen+2 >= maxlen)
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000675 return 0; /* Don't mangle __extremely_long_names */
676 if (name[nlen-1] == '_' && name[nlen-2] == '_')
677 return 0; /* Don't mangle __whatever__ */
678 p = c->c_private;
679 /* Strip leading underscores from class name */
680 while (*p == '_')
681 p++;
682 if (*p == '\0')
683 return 0; /* Don't mangle if class is just underscores */
684 plen = strlen(p);
685 if (plen + nlen >= maxlen)
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000686 plen = maxlen-nlen-2; /* Truncate class name if too long */
687 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000688 buffer[0] = '_';
689 strncpy(buffer+1, p, plen);
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000690 strcpy(buffer+1+plen, name);
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000691 /* fprintf(stderr, "mangle %s -> %s\n", name, buffer); */
692 return 1;
693}
694#endif
695
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000696static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000697com_addopnamestr(struct compiling *c, int op, char *name)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000698{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000699 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000700 int i;
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000701#ifdef PRIVATE_NAME_MANGLING
702 char buffer[256];
703 if (name != NULL && name[0] == '_' && name[1] == '_' &&
704 c->c_private != NULL &&
Guido van Rossum582acec2000-06-28 22:07:35 +0000705 com_mangle(c, name, buffer, sizeof(buffer)))
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000706 name = buffer;
707#endif
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000708 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000709 c->c_errors++;
710 i = 255;
711 }
712 else {
713 i = com_addname(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000714 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000715 }
Guido van Rossumc5e96291991-12-10 13:53:51 +0000716 /* Hack to replace *_NAME opcodes by *_GLOBAL if necessary */
717 switch (op) {
718 case LOAD_NAME:
719 case STORE_NAME:
720 case DELETE_NAME:
Guido van Rossum79f25d91997-04-29 20:08:16 +0000721 if (PyDict_GetItemString(c->c_globals, name) != NULL) {
Guido van Rossumc5e96291991-12-10 13:53:51 +0000722 switch (op) {
723 case LOAD_NAME: op = LOAD_GLOBAL; break;
724 case STORE_NAME: op = STORE_GLOBAL; break;
725 case DELETE_NAME: op = DELETE_GLOBAL; break;
726 }
727 }
728 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000729 com_addoparg(c, op, i);
730}
731
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000732static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000733com_addopname(struct compiling *c, int op, node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000734{
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000735 char *name;
736 char buffer[1000];
737 /* XXX it is possible to write this code without the 1000
738 chars on the total length of dotted names, I just can't be
739 bothered right now */
740 if (TYPE(n) == STAR)
741 name = "*";
742 else if (TYPE(n) == dotted_name) {
743 char *p = buffer;
744 int i;
745 name = buffer;
746 for (i = 0; i < NCH(n); i += 2) {
747 char *s = STR(CHILD(n, i));
748 if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000749 com_error(c, PyExc_MemoryError,
Guido van Rossum452a9831996-09-17 14:32:04 +0000750 "dotted_name too long");
Guido van Rossumd9dfaf51995-02-17 15:04:57 +0000751 name = NULL;
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000752 break;
753 }
754 if (p != buffer)
755 *p++ = '.';
756 strcpy(p, s);
757 p = strchr(p, '\0');
758 }
759 }
760 else {
761 REQ(n, NAME);
762 name = STR(n);
763 }
764 com_addopnamestr(c, op, name);
765}
766
Guido van Rossum79f25d91997-04-29 20:08:16 +0000767static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000768parsenumber(struct compiling *co, char *s)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000769{
Guido van Rossumc5e96291991-12-10 13:53:51 +0000770 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000771 long x;
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000772 double dx;
Guido van Rossum50564e81996-01-12 01:13:16 +0000773#ifndef WITHOUT_COMPLEX
Guido van Rossum530956d1996-07-21 02:27:43 +0000774 Py_complex c;
Guido van Rossum50564e81996-01-12 01:13:16 +0000775 int imflag;
776#endif
777
Guido van Rossum282914b1991-04-04 10:42:56 +0000778 errno = 0;
Guido van Rossumc5e96291991-12-10 13:53:51 +0000779 end = s + strlen(s) - 1;
Guido van Rossum50564e81996-01-12 01:13:16 +0000780#ifndef WITHOUT_COMPLEX
Guido van Rossum15ad9a61996-01-26 20:53:56 +0000781 imflag = *end == 'j' || *end == 'J';
Guido van Rossum50564e81996-01-12 01:13:16 +0000782#endif
Guido van Rossumf1aeab71992-03-27 17:28:26 +0000783 if (*end == 'l' || *end == 'L')
Guido van Rossum79f25d91997-04-29 20:08:16 +0000784 return PyLong_FromString(s, (char **)0, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +0000785 if (s[0] == '0')
Guido van Rossum79f25d91997-04-29 20:08:16 +0000786 x = (long) PyOS_strtoul(s, &end, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +0000787 else
Guido van Rossum79f25d91997-04-29 20:08:16 +0000788 x = PyOS_strtol(s, &end, 0);
Guido van Rossum282914b1991-04-04 10:42:56 +0000789 if (*end == '\0') {
790 if (errno != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000791 com_error(co, PyExc_OverflowError,
Guido van Rossum452a9831996-09-17 14:32:04 +0000792 "integer literal too large");
Guido van Rossum282914b1991-04-04 10:42:56 +0000793 return NULL;
794 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000795 return PyInt_FromLong(x);
Guido van Rossum282914b1991-04-04 10:42:56 +0000796 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000797 /* XXX Huge floats may silently fail */
Guido van Rossum50564e81996-01-12 01:13:16 +0000798#ifndef WITHOUT_COMPLEX
799 if (imflag) {
800 c.real = 0.;
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000801 PyFPE_START_PROTECT("atof", return 0)
Guido van Rossum50564e81996-01-12 01:13:16 +0000802 c.imag = atof(s);
Guido van Rossum45b83911997-03-14 04:32:50 +0000803 PyFPE_END_PROTECT(c)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000804 return PyComplex_FromCComplex(c);
Guido van Rossum50564e81996-01-12 01:13:16 +0000805 }
Guido van Rossumac1fc951997-10-08 15:23:55 +0000806 else
Guido van Rossum50564e81996-01-12 01:13:16 +0000807#endif
Guido van Rossumac1fc951997-10-08 15:23:55 +0000808 {
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000809 PyFPE_START_PROTECT("atof", return 0)
810 dx = atof(s);
Guido van Rossum45b83911997-03-14 04:32:50 +0000811 PyFPE_END_PROTECT(dx)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000812 return PyFloat_FromDouble(dx);
Guido van Rossum0ae748d1997-02-14 22:58:07 +0000813 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000814}
815
Guido van Rossum79f25d91997-04-29 20:08:16 +0000816static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000817parsestr(char *s)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000818{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000819 PyObject *v;
Guido van Rossum582acec2000-06-28 22:07:35 +0000820 size_t len;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000821 char *buf;
822 char *p;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000823 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000824 int c;
Guido van Rossum5f5e8171997-04-06 03:41:40 +0000825 int first = *s;
826 int quote = first;
Guido van Rossum5aa88f02000-03-10 23:01:36 +0000827 int rawmode = 0;
828 int unicode = 0;
829 if (isalpha(quote) || quote == '_') {
830 if (quote == 'u' || quote == 'U') {
831 quote = *++s;
832 unicode = 1;
833 }
834 if (quote == 'r' || quote == 'R') {
835 quote = *++s;
836 rawmode = 1;
837 }
838 }
Guido van Rossum8054fad1993-10-26 15:19:44 +0000839 if (quote != '\'' && quote != '\"') {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000840 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000841 return NULL;
842 }
843 s++;
844 len = strlen(s);
Guido van Rossum582acec2000-06-28 22:07:35 +0000845 if (len > INT_MAX) {
846 PyErr_SetString(PyExc_OverflowError, "string to parse is too long");
847 return NULL;
848 }
Guido van Rossum8054fad1993-10-26 15:19:44 +0000849 if (s[--len] != quote) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000850 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000851 return NULL;
852 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000853 if (len >= 4 && s[0] == quote && s[1] == quote) {
854 s += 2;
855 len -= 2;
856 if (s[--len] != quote || s[--len] != quote) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000857 PyErr_BadInternalCall();
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000858 return NULL;
859 }
860 }
Guido van Rossumfdc8bdb2000-05-01 17:54:56 +0000861 if (unicode || Py_UnicodeFlag) {
Guido van Rossum5aa88f02000-03-10 23:01:36 +0000862 if (rawmode)
863 return PyUnicode_DecodeRawUnicodeEscape(
864 s, len, NULL);
865 else
866 return PyUnicode_DecodeUnicodeEscape(
867 s, len, NULL);
868 }
Fredrik Lundh1fa0b892000-09-02 20:11:27 +0000869 if (rawmode || strchr(s, '\\') == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000870 return PyString_FromStringAndSize(s, len);
871 v = PyString_FromStringAndSize((char *)NULL, len);
Fredrik Lundh1fa0b892000-09-02 20:11:27 +0000872 if (v == NULL)
873 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000874 p = buf = PyString_AsString(v);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000875 end = s + len;
876 while (s < end) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000877 if (*s != '\\') {
878 *p++ = *s++;
879 continue;
880 }
881 s++;
882 switch (*s++) {
883 /* XXX This assumes ASCII! */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000884 case '\n': break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000885 case '\\': *p++ = '\\'; break;
886 case '\'': *p++ = '\''; break;
Guido van Rossum8054fad1993-10-26 15:19:44 +0000887 case '\"': *p++ = '\"'; break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000888 case 'b': *p++ = '\b'; break;
889 case 'f': *p++ = '\014'; break; /* FF */
890 case 't': *p++ = '\t'; break;
891 case 'n': *p++ = '\n'; break;
892 case 'r': *p++ = '\r'; break;
893 case 'v': *p++ = '\013'; break; /* VT */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000894 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
895 case '0': case '1': case '2': case '3':
896 case '4': case '5': case '6': case '7':
897 c = s[-1] - '0';
898 if ('0' <= *s && *s <= '7') {
899 c = (c<<3) + *s++ - '0';
900 if ('0' <= *s && *s <= '7')
901 c = (c<<3) + *s++ - '0';
902 }
903 *p++ = c;
904 break;
905 case 'x':
Fredrik Lundh1fa0b892000-09-02 20:11:27 +0000906 if (isxdigit(Py_CHARMASK(s[0])) && isxdigit(Py_CHARMASK(s[1]))) {
Guido van Rossumed1100f1997-10-20 23:24:07 +0000907 unsigned int x = 0;
Fredrik Lundh1fa0b892000-09-02 20:11:27 +0000908 c = Py_CHARMASK(*s);
909 s++;
910 if (isdigit(c))
911 x = c - '0';
912 else if (islower(c))
913 x = 10 + c - 'a';
914 else
915 x = 10 + c - 'A';
916 x = x << 4;
917 c = Py_CHARMASK(*s);
918 s++;
919 if (isdigit(c))
920 x += c - '0';
921 else if (islower(c))
922 x += 10 + c - 'a';
923 else
924 x += 10 + c - 'A';
Guido van Rossumed1100f1997-10-20 23:24:07 +0000925 *p++ = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000926 break;
927 }
Fredrik Lundh1fa0b892000-09-02 20:11:27 +0000928 PyErr_SetString(PyExc_ValueError, "invalid \\x escape");
929 Py_DECREF(v);
930 return NULL;
931 default:
932 *p++ = '\\';
933 *p++ = s[-1];
934 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000935 }
936 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000937 _PyString_Resize(&v, (int)(p - buf));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000938 return v;
939}
940
Guido van Rossum79f25d91997-04-29 20:08:16 +0000941static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000942parsestrplus(node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000943{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000944 PyObject *v;
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000945 int i;
946 REQ(CHILD(n, 0), STRING);
947 if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) {
948 /* String literal concatenation */
Fred Drake4e998bc2000-04-13 14:10:44 +0000949 for (i = 1; i < NCH(n); i++) {
950 PyObject *s;
951 s = parsestr(STR(CHILD(n, i)));
952 if (s == NULL)
953 goto onError;
954 if (PyString_Check(v) && PyString_Check(s)) {
955 PyString_ConcatAndDel(&v, s);
956 if (v == NULL)
957 goto onError;
958 }
959 else {
960 PyObject *temp;
961 temp = PyUnicode_Concat(v, s);
962 Py_DECREF(s);
963 if (temp == NULL)
964 goto onError;
965 Py_DECREF(v);
966 v = temp;
967 }
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000968 }
969 }
970 return v;
Fred Drake4e998bc2000-04-13 14:10:44 +0000971
972 onError:
973 Py_XDECREF(v);
974 return NULL;
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000975}
976
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000977static void
Skip Montanaro803d6e52000-08-12 18:09:51 +0000978com_list_for(struct compiling *c, node *n, node *e, char *t)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000979{
Skip Montanaro803d6e52000-08-12 18:09:51 +0000980 PyObject *v;
981 int anchor = 0;
982 int save_begin = c->c_begin;
983
984 /* list_iter: for v in expr [list_iter] */
985 com_node(c, CHILD(n, 3)); /* expr */
986 v = PyInt_FromLong(0L);
987 if (v == NULL)
988 c->c_errors++;
989 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
990 com_push(c, 1);
991 Py_XDECREF(v);
992 c->c_begin = c->c_nexti;
993 com_addoparg(c, SET_LINENO, n->n_lineno);
994 com_addfwref(c, FOR_LOOP, &anchor);
995 com_push(c, 1);
Thomas Wouters434d0822000-08-24 20:11:32 +0000996 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
Skip Montanaro803d6e52000-08-12 18:09:51 +0000997 c->c_loops++;
998 com_list_iter(c, n, e, t);
999 c->c_loops--;
1000 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1001 c->c_begin = save_begin;
1002 com_backpatch(c, anchor);
1003 com_pop(c, 2); /* FOR_LOOP has popped these */
1004}
1005
1006static void
1007com_list_if(struct compiling *c, node *n, node *e, char *t)
1008{
1009 int anchor = 0;
1010 int a = 0;
1011 /* list_iter: 'if' test [list_iter] */
1012 com_addoparg(c, SET_LINENO, n->n_lineno);
1013 com_node(c, CHILD(n, 1));
1014 com_addfwref(c, JUMP_IF_FALSE, &a);
1015 com_addbyte(c, POP_TOP);
1016 com_pop(c, 1);
1017 com_list_iter(c, n, e, t);
1018 com_addfwref(c, JUMP_FORWARD, &anchor);
1019 com_backpatch(c, a);
1020 /* We jump here with an extra entry which we now pop */
1021 com_addbyte(c, POP_TOP);
1022 com_backpatch(c, anchor);
1023}
1024
1025static void
1026com_list_iter(struct compiling *c,
1027 node *p, /* parent of list_iter node */
1028 node *e, /* element expression node */
1029 char *t /* name of result list temp local */)
1030{
1031 /* list_iter is the last child in a listmaker, list_for, or list_if */
1032 node *n = CHILD(p, NCH(p)-1);
1033 if (TYPE(n) == list_iter) {
1034 n = CHILD(n, 0);
1035 switch (TYPE(n)) {
1036 case list_for:
1037 com_list_for(c, n, e, t);
1038 break;
1039 case list_if:
1040 com_list_if(c, n, e, t);
1041 break;
1042 default:
1043 com_error(c, PyExc_SystemError,
1044 "invalid list_iter node type");
1045 }
1046 }
1047 else {
1048 com_addopnamestr(c, LOAD_NAME, t);
1049 com_push(c, 1);
1050 com_node(c, e);
1051 com_addoparg(c, CALL_FUNCTION, 1);
1052 com_addbyte(c, POP_TOP);
1053 com_pop(c, 2);
1054 }
1055}
1056
1057static void
1058com_list_comprehension(struct compiling *c, node *n)
1059{
Skip Montanaro46dfa5f2000-08-22 02:43:07 +00001060 /* listmaker: test list_for */
Skip Montanaro803d6e52000-08-12 18:09:51 +00001061 char tmpname[12];
1062 sprintf(tmpname, "__%d__", ++c->c_tmpname);
1063 com_addoparg(c, BUILD_LIST, 0);
1064 com_addbyte(c, DUP_TOP); /* leave the result on the stack */
1065 com_push(c, 2);
1066 com_addopnamestr(c, LOAD_ATTR, "append");
1067 com_addopnamestr(c, STORE_NAME, tmpname);
1068 com_pop(c, 1);
Skip Montanaro46dfa5f2000-08-22 02:43:07 +00001069 com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001070 com_addopnamestr(c, DELETE_NAME, tmpname);
1071 --c->c_tmpname;
1072}
1073
1074static void
1075com_listmaker(struct compiling *c, node *n)
1076{
Skip Montanaro46dfa5f2000-08-22 02:43:07 +00001077 /* listmaker: test ( list_for | (',' test)* [','] ) */
1078 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for)
Skip Montanaro803d6e52000-08-12 18:09:51 +00001079 com_list_comprehension(c, n);
1080 else {
1081 int len = 0;
1082 int i;
1083 for (i = 0; i < NCH(n); i += 2, len++)
1084 com_node(c, CHILD(n, i));
1085 com_addoparg(c, BUILD_LIST, len);
1086 com_pop(c, len-1);
1087 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001088}
1089
1090static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001091com_dictmaker(struct compiling *c, node *n)
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001092{
1093 int i;
1094 /* dictmaker: test ':' test (',' test ':' value)* [','] */
1095 for (i = 0; i+2 < NCH(n); i += 4) {
1096 /* We must arrange things just right for STORE_SUBSCR.
1097 It wants the stack to look like (value) (dict) (key) */
1098 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001099 com_push(c, 1);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001100 com_node(c, CHILD(n, i+2)); /* value */
1101 com_addbyte(c, ROT_TWO);
1102 com_node(c, CHILD(n, i)); /* key */
1103 com_addbyte(c, STORE_SUBSCR);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001104 com_pop(c, 3);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001105 }
1106}
1107
1108static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001109com_atom(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001110{
1111 node *ch;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001112 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001113 int i;
1114 REQ(n, atom);
1115 ch = CHILD(n, 0);
1116 switch (TYPE(ch)) {
1117 case LPAR:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001118 if (TYPE(CHILD(n, 1)) == RPAR) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001119 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001120 com_push(c, 1);
1121 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001122 else
1123 com_node(c, CHILD(n, 1));
1124 break;
Skip Montanaro803d6e52000-08-12 18:09:51 +00001125 case LSQB: /* '[' [listmaker] ']' */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001126 if (TYPE(CHILD(n, 1)) == RSQB) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001127 com_addoparg(c, BUILD_LIST, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001128 com_push(c, 1);
1129 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001130 else
Skip Montanaro803d6e52000-08-12 18:09:51 +00001131 com_listmaker(c, CHILD(n, 1));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001132 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001133 case LBRACE: /* '{' [dictmaker] '}' */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001134 com_addoparg(c, BUILD_MAP, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001135 com_push(c, 1);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001136 if (TYPE(CHILD(n, 1)) == dictmaker)
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001137 com_dictmaker(c, CHILD(n, 1));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001138 break;
1139 case BACKQUOTE:
1140 com_node(c, CHILD(n, 1));
1141 com_addbyte(c, UNARY_CONVERT);
1142 break;
1143 case NUMBER:
Guido van Rossum452a9831996-09-17 14:32:04 +00001144 if ((v = parsenumber(c, STR(ch))) == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001145 i = 255;
1146 }
1147 else {
1148 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001149 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001150 }
1151 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001152 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001153 break;
1154 case STRING:
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001155 v = parsestrplus(n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001156 if (v == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001157 c->c_errors++;
1158 i = 255;
1159 }
1160 else {
1161 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001162 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001163 }
1164 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001165 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001166 break;
1167 case NAME:
1168 com_addopname(c, LOAD_NAME, ch);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001169 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001170 break;
1171 default:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001172 /* XXX fprintf(stderr, "node type %d\n", TYPE(ch)); */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001173 com_error(c, PyExc_SystemError,
1174 "com_atom: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001175 }
1176}
1177
1178static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001179com_slice(struct compiling *c, node *n, int op)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001180{
1181 if (NCH(n) == 1) {
1182 com_addbyte(c, op);
1183 }
1184 else if (NCH(n) == 2) {
1185 if (TYPE(CHILD(n, 0)) != COLON) {
1186 com_node(c, CHILD(n, 0));
1187 com_addbyte(c, op+1);
1188 }
1189 else {
1190 com_node(c, CHILD(n, 1));
1191 com_addbyte(c, op+2);
1192 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00001193 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001194 }
1195 else {
1196 com_node(c, CHILD(n, 0));
1197 com_node(c, CHILD(n, 2));
1198 com_addbyte(c, op+3);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001199 com_pop(c, 2);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001200 }
1201}
1202
Guido van Rossum635abd21997-01-06 22:56:52 +00001203static void
Thomas Wouters434d0822000-08-24 20:11:32 +00001204com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn)
1205{
1206 if (NCH(n) == 1) {
1207 com_addbyte(c, DUP_TOP);
1208 com_push(c, 1);
1209 com_addbyte(c, SLICE);
1210 com_node(c, augn);
1211 com_addbyte(c, opcode);
1212 com_pop(c, 1);
1213 com_addbyte(c, ROT_TWO);
1214 com_addbyte(c, STORE_SLICE);
1215 com_pop(c, 2);
1216 } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) {
1217 com_node(c, CHILD(n, 0));
1218 com_addoparg(c, DUP_TOPX, 2);
1219 com_push(c, 2);
1220 com_addbyte(c, SLICE+1);
1221 com_pop(c, 1);
1222 com_node(c, augn);
1223 com_addbyte(c, opcode);
1224 com_pop(c, 1);
1225 com_addbyte(c, ROT_THREE);
1226 com_addbyte(c, STORE_SLICE+1);
1227 com_pop(c, 3);
1228 } else if (NCH(n) == 2) {
1229 com_node(c, CHILD(n, 1));
1230 com_addoparg(c, DUP_TOPX, 2);
1231 com_push(c, 2);
1232 com_addbyte(c, SLICE+2);
1233 com_pop(c, 1);
1234 com_node(c, augn);
1235 com_addbyte(c, opcode);
1236 com_pop(c, 1);
1237 com_addbyte(c, ROT_THREE);
1238 com_addbyte(c, STORE_SLICE+2);
1239 com_pop(c, 3);
1240 } else {
1241 com_node(c, CHILD(n, 0));
1242 com_node(c, CHILD(n, 2));
1243 com_addoparg(c, DUP_TOPX, 3);
1244 com_push(c, 3);
1245 com_addbyte(c, SLICE+3);
1246 com_pop(c, 2);
1247 com_node(c, augn);
1248 com_addbyte(c, opcode);
1249 com_pop(c, 1);
1250 com_addbyte(c, ROT_FOUR);
1251 com_addbyte(c, STORE_SLICE+3);
1252 com_pop(c, 4);
1253 }
1254}
1255
1256static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001257com_argument(struct compiling *c, node *n, PyObject **pkeywords)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001258{
1259 node *m;
Guido van Rossum8b993a91997-01-17 21:04:03 +00001260 REQ(n, argument); /* [test '='] test; really [keyword '='] test */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001261 if (NCH(n) == 1) {
Guido van Rossum635abd21997-01-06 22:56:52 +00001262 if (*pkeywords != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001263 com_error(c, PyExc_SyntaxError,
Fred Drakedcf08e02000-08-15 15:49:44 +00001264 "non-keyword arg after keyword arg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001265 }
1266 else {
1267 com_node(c, CHILD(n, 0));
1268 }
Guido van Rossum635abd21997-01-06 22:56:52 +00001269 return;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001270 }
1271 m = n;
1272 do {
1273 m = CHILD(m, 0);
1274 } while (NCH(m) == 1);
1275 if (TYPE(m) != NAME) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001276 com_error(c, PyExc_SyntaxError,
1277 "keyword can't be an expression");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001278 }
1279 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001280 PyObject *v = PyString_InternFromString(STR(m));
Guido van Rossum635abd21997-01-06 22:56:52 +00001281 if (v != NULL && *pkeywords == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001282 *pkeywords = PyDict_New();
Guido van Rossum635abd21997-01-06 22:56:52 +00001283 if (v == NULL || *pkeywords == NULL)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001284 c->c_errors++;
1285 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001286 if (PyDict_GetItem(*pkeywords, v) != NULL)
1287 com_error(c, PyExc_SyntaxError,
Guido van Rossum635abd21997-01-06 22:56:52 +00001288 "duplicate keyword argument");
1289 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001290 if (PyDict_SetItem(*pkeywords, v, v) != 0)
Guido van Rossum635abd21997-01-06 22:56:52 +00001291 c->c_errors++;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001292 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001293 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001294 Py_DECREF(v);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001295 }
1296 }
1297 com_node(c, CHILD(n, 2));
Guido van Rossumf10570b1995-07-07 22:53:21 +00001298}
1299
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001300static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001301com_call_function(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001302{
1303 if (TYPE(n) == RPAR) {
Guido van Rossumf10570b1995-07-07 22:53:21 +00001304 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001305 }
1306 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001307 PyObject *keywords = NULL;
Guido van Rossum635abd21997-01-06 22:56:52 +00001308 int i, na, nk;
Guido van Rossumca906051998-12-10 16:56:22 +00001309 int lineno = n->n_lineno;
Jeremy Hylton76901512000-03-28 23:49:17 +00001310 int star_flag = 0;
1311 int starstar_flag = 0;
1312 int opcode;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001313 REQ(n, arglist);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001314 na = 0;
1315 nk = 0;
1316 for (i = 0; i < NCH(n); i += 2) {
Guido van Rossumca906051998-12-10 16:56:22 +00001317 node *ch = CHILD(n, i);
Jeremy Hylton76901512000-03-28 23:49:17 +00001318 if (TYPE(ch) == STAR ||
1319 TYPE(ch) == DOUBLESTAR)
1320 break;
Guido van Rossumca906051998-12-10 16:56:22 +00001321 if (ch->n_lineno != lineno) {
1322 lineno = ch->n_lineno;
1323 com_addoparg(c, SET_LINENO, lineno);
1324 }
1325 com_argument(c, ch, &keywords);
Guido van Rossum635abd21997-01-06 22:56:52 +00001326 if (keywords == NULL)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001327 na++;
1328 else
1329 nk++;
1330 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001331 Py_XDECREF(keywords);
Jeremy Hylton76901512000-03-28 23:49:17 +00001332 while (i < NCH(n)) {
1333 node *tok = CHILD(n, i);
1334 node *ch = CHILD(n, i+1);
1335 i += 3;
1336 switch (TYPE(tok)) {
1337 case STAR: star_flag = 1; break;
1338 case DOUBLESTAR: starstar_flag = 1; break;
1339 }
1340 com_node(c, ch);
1341 }
Guido van Rossumf10570b1995-07-07 22:53:21 +00001342 if (na > 255 || nk > 255) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001343 com_error(c, PyExc_SyntaxError,
1344 "more than 255 arguments");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001345 }
Jeremy Hylton76901512000-03-28 23:49:17 +00001346 if (star_flag || starstar_flag)
Jeremy Hyltone4fb9582000-03-29 00:10:44 +00001347 opcode = CALL_FUNCTION_VAR - 1 +
Jeremy Hylton76901512000-03-28 23:49:17 +00001348 star_flag + (starstar_flag << 1);
1349 else
1350 opcode = CALL_FUNCTION;
1351 com_addoparg(c, opcode, na | (nk << 8));
1352 com_pop(c, na + 2*nk + star_flag + starstar_flag);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001353 }
1354}
1355
1356static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001357com_select_member(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001358{
1359 com_addopname(c, LOAD_ATTR, n);
1360}
1361
1362static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001363com_sliceobj(struct compiling *c, node *n)
Guido van Rossum8861b741996-07-30 16:49:37 +00001364{
1365 int i=0;
1366 int ns=2; /* number of slice arguments */
Guido van Rossum8861b741996-07-30 16:49:37 +00001367 node *ch;
1368
1369 /* first argument */
1370 if (TYPE(CHILD(n,i)) == COLON) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001371 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001372 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001373 i++;
1374 }
1375 else {
1376 com_node(c, CHILD(n,i));
1377 i++;
1378 REQ(CHILD(n,i),COLON);
1379 i++;
1380 }
1381 /* second argument */
1382 if (i < NCH(n) && TYPE(CHILD(n,i)) == test) {
1383 com_node(c, CHILD(n,i));
1384 i++;
1385 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00001386 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001387 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001388 com_push(c, 1);
1389 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001390 /* remaining arguments */
1391 for (; i < NCH(n); i++) {
1392 ns++;
1393 ch=CHILD(n,i);
1394 REQ(ch, sliceop);
1395 if (NCH(ch) == 1) {
1396 /* right argument of ':' missing */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001397 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001398 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001399 }
1400 else
1401 com_node(c, CHILD(ch,1));
1402 }
1403 com_addoparg(c, BUILD_SLICE, ns);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001404 com_pop(c, 1 + (ns == 3));
Guido van Rossum8861b741996-07-30 16:49:37 +00001405}
1406
1407static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001408com_subscript(struct compiling *c, node *n)
Guido van Rossum8861b741996-07-30 16:49:37 +00001409{
1410 node *ch;
1411 REQ(n, subscript);
1412 ch = CHILD(n,0);
1413 /* check for rubber index */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001414 if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
Guido van Rossume449af71996-10-11 16:25:41 +00001415 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001416 com_push(c, 1);
1417 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001418 else {
1419 /* check for slice */
1420 if ((TYPE(ch) == COLON || NCH(n) > 1))
1421 com_sliceobj(c, n);
1422 else {
1423 REQ(ch, test);
1424 com_node(c, ch);
1425 }
1426 }
1427}
1428
1429static void
Thomas Wouters434d0822000-08-24 20:11:32 +00001430com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn)
Guido van Rossum8861b741996-07-30 16:49:37 +00001431{
1432 int i, op;
1433 REQ(n, subscriptlist);
1434 /* Check to make backward compatible slice behavior for '[i:j]' */
1435 if (NCH(n) == 1) {
1436 node *sub = CHILD(n, 0); /* subscript */
Thomas Wouterse8643c42000-08-05 21:37:50 +00001437 /* 'Basic' slice, should have exactly one colon. */
1438 if ((TYPE(CHILD(sub, 0)) == COLON
1439 || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
1440 && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
1441 {
Thomas Wouters434d0822000-08-24 20:11:32 +00001442 switch (assigning) {
1443 case OP_DELETE:
1444 op = DELETE_SLICE;
1445 break;
1446 case OP_ASSIGN:
1447 op = STORE_SLICE;
1448 break;
1449 case OP_APPLY:
Guido van Rossum8861b741996-07-30 16:49:37 +00001450 op = SLICE;
Thomas Wouters434d0822000-08-24 20:11:32 +00001451 break;
1452 default:
1453 com_augassign_slice(c, sub, assigning, augn);
1454 return;
1455 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001456 com_slice(c, sub, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001457 if (op == STORE_SLICE)
1458 com_pop(c, 2);
1459 else if (op == DELETE_SLICE)
1460 com_pop(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001461 return;
1462 }
1463 }
1464 /* Else normal subscriptlist. Compile each subscript. */
1465 for (i = 0; i < NCH(n); i += 2)
1466 com_subscript(c, CHILD(n, i));
1467 /* Put multiple subscripts into a tuple */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001468 if (NCH(n) > 1) {
1469 i = (NCH(n)+1) / 2;
1470 com_addoparg(c, BUILD_TUPLE, i);
1471 com_pop(c, i-1);
1472 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001473 switch (assigning) {
1474 case OP_DELETE:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001475 op = DELETE_SUBSCR;
1476 i = 2;
Thomas Wouters434d0822000-08-24 20:11:32 +00001477 break;
1478 default:
1479 case OP_ASSIGN:
1480 op = STORE_SUBSCR;
1481 i = 3;
1482 break;
1483 case OP_APPLY:
1484 op = BINARY_SUBSCR;
1485 i = 1;
1486 break;
1487 }
1488 if (assigning > OP_APPLY) {
1489 com_addoparg(c, DUP_TOPX, 2);
1490 com_push(c, 2);
1491 com_addbyte(c, BINARY_SUBSCR);
1492 com_pop(c, 1);
1493 com_node(c, augn);
1494 com_addbyte(c, assigning);
1495 com_pop(c, 1);
1496 com_addbyte(c, ROT_THREE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001497 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001498 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001499 com_pop(c, i);
Guido van Rossum8861b741996-07-30 16:49:37 +00001500}
1501
1502static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001503com_apply_trailer(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001504{
1505 REQ(n, trailer);
1506 switch (TYPE(CHILD(n, 0))) {
1507 case LPAR:
1508 com_call_function(c, CHILD(n, 1));
1509 break;
1510 case DOT:
1511 com_select_member(c, CHILD(n, 1));
1512 break;
1513 case LSQB:
Thomas Wouters434d0822000-08-24 20:11:32 +00001514 com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001515 break;
1516 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001517 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001518 "com_apply_trailer: unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001519 }
1520}
1521
1522static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001523com_power(struct compiling *c, node *n)
Guido van Rossum50564e81996-01-12 01:13:16 +00001524{
1525 int i;
1526 REQ(n, power);
1527 com_atom(c, CHILD(n, 0));
1528 for (i = 1; i < NCH(n); i++) {
1529 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
1530 com_factor(c, CHILD(n, i+1));
1531 com_addbyte(c, BINARY_POWER);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001532 com_pop(c, 1);
Guido van Rossum50564e81996-01-12 01:13:16 +00001533 break;
1534 }
1535 else
1536 com_apply_trailer(c, CHILD(n, i));
1537 }
1538}
1539
1540static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001541com_factor(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001542{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001543 REQ(n, factor);
1544 if (TYPE(CHILD(n, 0)) == PLUS) {
1545 com_factor(c, CHILD(n, 1));
1546 com_addbyte(c, UNARY_POSITIVE);
1547 }
1548 else if (TYPE(CHILD(n, 0)) == MINUS) {
1549 com_factor(c, CHILD(n, 1));
1550 com_addbyte(c, UNARY_NEGATIVE);
1551 }
Guido van Rossum7928cd71991-10-24 14:59:31 +00001552 else if (TYPE(CHILD(n, 0)) == TILDE) {
1553 com_factor(c, CHILD(n, 1));
1554 com_addbyte(c, UNARY_INVERT);
1555 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001556 else {
Guido van Rossum50564e81996-01-12 01:13:16 +00001557 com_power(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001558 }
1559}
1560
1561static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001562com_term(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001563{
1564 int i;
1565 int op;
1566 REQ(n, term);
1567 com_factor(c, CHILD(n, 0));
1568 for (i = 2; i < NCH(n); i += 2) {
1569 com_factor(c, CHILD(n, i));
1570 switch (TYPE(CHILD(n, i-1))) {
1571 case STAR:
1572 op = BINARY_MULTIPLY;
1573 break;
1574 case SLASH:
1575 op = BINARY_DIVIDE;
1576 break;
1577 case PERCENT:
1578 op = BINARY_MODULO;
1579 break;
1580 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001581 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001582 "com_term: operator not *, / or %");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001583 op = 255;
1584 }
1585 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001586 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001587 }
1588}
1589
1590static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001591com_arith_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001592{
1593 int i;
1594 int op;
1595 REQ(n, arith_expr);
1596 com_term(c, CHILD(n, 0));
1597 for (i = 2; i < NCH(n); i += 2) {
1598 com_term(c, CHILD(n, i));
1599 switch (TYPE(CHILD(n, i-1))) {
1600 case PLUS:
1601 op = BINARY_ADD;
1602 break;
1603 case MINUS:
1604 op = BINARY_SUBTRACT;
1605 break;
1606 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001607 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001608 "com_arith_expr: operator not + or -");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001609 op = 255;
1610 }
1611 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001612 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001613 }
1614}
1615
1616static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001617com_shift_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001618{
1619 int i;
1620 int op;
1621 REQ(n, shift_expr);
1622 com_arith_expr(c, CHILD(n, 0));
1623 for (i = 2; i < NCH(n); i += 2) {
1624 com_arith_expr(c, CHILD(n, i));
1625 switch (TYPE(CHILD(n, i-1))) {
1626 case LEFTSHIFT:
1627 op = BINARY_LSHIFT;
1628 break;
1629 case RIGHTSHIFT:
1630 op = BINARY_RSHIFT;
1631 break;
1632 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001633 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001634 "com_shift_expr: operator not << or >>");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001635 op = 255;
1636 }
1637 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001638 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001639 }
1640}
1641
1642static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001643com_and_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001644{
1645 int i;
1646 int op;
1647 REQ(n, and_expr);
1648 com_shift_expr(c, CHILD(n, 0));
1649 for (i = 2; i < NCH(n); i += 2) {
1650 com_shift_expr(c, CHILD(n, i));
1651 if (TYPE(CHILD(n, i-1)) == AMPER) {
1652 op = BINARY_AND;
1653 }
1654 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001655 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001656 "com_and_expr: operator not &");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001657 op = 255;
1658 }
1659 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001660 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001661 }
1662}
1663
1664static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001665com_xor_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001666{
1667 int i;
1668 int op;
1669 REQ(n, xor_expr);
1670 com_and_expr(c, CHILD(n, 0));
1671 for (i = 2; i < NCH(n); i += 2) {
1672 com_and_expr(c, CHILD(n, i));
1673 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
1674 op = BINARY_XOR;
1675 }
1676 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001677 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001678 "com_xor_expr: operator not ^");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001679 op = 255;
1680 }
1681 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001682 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001683 }
1684}
1685
1686static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001687com_expr(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001688{
1689 int i;
1690 int op;
1691 REQ(n, expr);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001692 com_xor_expr(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001693 for (i = 2; i < NCH(n); i += 2) {
Guido van Rossum7928cd71991-10-24 14:59:31 +00001694 com_xor_expr(c, CHILD(n, i));
1695 if (TYPE(CHILD(n, i-1)) == VBAR) {
1696 op = BINARY_OR;
1697 }
1698 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001699 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001700 "com_expr: expr operator not |");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001701 op = 255;
1702 }
1703 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001704 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001705 }
1706}
1707
1708static enum cmp_op
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001709cmp_type(node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001710{
1711 REQ(n, comp_op);
Guido van Rossum01cfd441991-10-20 20:12:38 +00001712 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001713 | 'in' | 'not' 'in' | 'is' | 'is' not' */
1714 if (NCH(n) == 1) {
1715 n = CHILD(n, 0);
1716 switch (TYPE(n)) {
1717 case LESS: return LT;
1718 case GREATER: return GT;
Guido van Rossum01cfd441991-10-20 20:12:38 +00001719 case EQEQUAL: /* == */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001720 case EQUAL: return EQ;
Guido van Rossum01cfd441991-10-20 20:12:38 +00001721 case LESSEQUAL: return LE;
1722 case GREATEREQUAL: return GE;
1723 case NOTEQUAL: return NE; /* <> or != */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001724 case NAME: if (strcmp(STR(n), "in") == 0) return IN;
1725 if (strcmp(STR(n), "is") == 0) return IS;
1726 }
1727 }
1728 else if (NCH(n) == 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001729 switch (TYPE(CHILD(n, 0))) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001730 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1731 return NOT_IN;
1732 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1733 return IS_NOT;
1734 }
1735 }
1736 return BAD;
1737}
1738
1739static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001740com_comparison(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001741{
1742 int i;
1743 enum cmp_op op;
1744 int anchor;
1745 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
1746 com_expr(c, CHILD(n, 0));
1747 if (NCH(n) == 1)
1748 return;
1749
1750 /****************************************************************
1751 The following code is generated for all but the last
1752 comparison in a chain:
1753
1754 label: on stack: opcode: jump to:
1755
1756 a <code to load b>
1757 a, b DUP_TOP
1758 a, b, b ROT_THREE
1759 b, a, b COMPARE_OP
1760 b, 0-or-1 JUMP_IF_FALSE L1
1761 b, 1 POP_TOP
1762 b
1763
1764 We are now ready to repeat this sequence for the next
1765 comparison in the chain.
1766
1767 For the last we generate:
1768
1769 b <code to load c>
1770 b, c COMPARE_OP
1771 0-or-1
1772
1773 If there were any jumps to L1 (i.e., there was more than one
1774 comparison), we generate:
1775
1776 0-or-1 JUMP_FORWARD L2
1777 L1: b, 0 ROT_TWO
1778 0, b POP_TOP
1779 0
Guido van Rossum8b993a91997-01-17 21:04:03 +00001780 L2: 0-or-1
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001781 ****************************************************************/
1782
1783 anchor = 0;
1784
1785 for (i = 2; i < NCH(n); i += 2) {
1786 com_expr(c, CHILD(n, i));
1787 if (i+2 < NCH(n)) {
1788 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001789 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001790 com_addbyte(c, ROT_THREE);
1791 }
1792 op = cmp_type(CHILD(n, i-1));
1793 if (op == BAD) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001794 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001795 "com_comparison: unknown comparison op");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001796 }
1797 com_addoparg(c, COMPARE_OP, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001798 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001799 if (i+2 < NCH(n)) {
1800 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1801 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001802 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001803 }
1804 }
1805
1806 if (anchor) {
1807 int anchor2 = 0;
1808 com_addfwref(c, JUMP_FORWARD, &anchor2);
1809 com_backpatch(c, anchor);
1810 com_addbyte(c, ROT_TWO);
1811 com_addbyte(c, POP_TOP);
1812 com_backpatch(c, anchor2);
1813 }
1814}
1815
1816static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001817com_not_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001818{
1819 REQ(n, not_test); /* 'not' not_test | comparison */
1820 if (NCH(n) == 1) {
1821 com_comparison(c, CHILD(n, 0));
1822 }
1823 else {
1824 com_not_test(c, CHILD(n, 1));
1825 com_addbyte(c, UNARY_NOT);
1826 }
1827}
1828
1829static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001830com_and_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001831{
1832 int i;
1833 int anchor;
1834 REQ(n, and_test); /* not_test ('and' not_test)* */
1835 anchor = 0;
1836 i = 0;
1837 for (;;) {
1838 com_not_test(c, CHILD(n, i));
1839 if ((i += 2) >= NCH(n))
1840 break;
1841 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1842 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001843 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001844 }
1845 if (anchor)
1846 com_backpatch(c, anchor);
1847}
1848
1849static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001850com_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001851{
Guido van Rossum8b993a91997-01-17 21:04:03 +00001852 REQ(n, test); /* and_test ('or' and_test)* | lambdef */
Guido van Rossum57531fe1993-11-30 14:57:42 +00001853 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001854 PyObject *v;
Guido van Rossum57531fe1993-11-30 14:57:42 +00001855 int i;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001856 int ndefs = com_argdefs(c, CHILD(n, 0));
Guido van Rossum79f25d91997-04-29 20:08:16 +00001857 v = (PyObject *) icompile(CHILD(n, 0), c);
Guido van Rossum57531fe1993-11-30 14:57:42 +00001858 if (v == NULL) {
1859 c->c_errors++;
1860 i = 255;
1861 }
1862 else {
1863 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001864 Py_DECREF(v);
Guido van Rossum57531fe1993-11-30 14:57:42 +00001865 }
1866 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001867 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001868 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001869 com_pop(c, ndefs);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001870 }
Guido van Rossum57531fe1993-11-30 14:57:42 +00001871 else {
1872 int anchor = 0;
1873 int i = 0;
1874 for (;;) {
1875 com_and_test(c, CHILD(n, i));
1876 if ((i += 2) >= NCH(n))
1877 break;
1878 com_addfwref(c, JUMP_IF_TRUE, &anchor);
1879 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001880 com_pop(c, 1);
Guido van Rossum57531fe1993-11-30 14:57:42 +00001881 }
1882 if (anchor)
1883 com_backpatch(c, anchor);
1884 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001885}
1886
1887static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001888com_list(struct compiling *c, node *n, int toplevel)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001889{
1890 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
Guido van Rossum288a60f1991-12-16 13:05:10 +00001891 if (NCH(n) == 1 && !toplevel) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001892 com_node(c, CHILD(n, 0));
1893 }
1894 else {
1895 int i;
1896 int len;
1897 len = (NCH(n) + 1) / 2;
1898 for (i = 0; i < NCH(n); i += 2)
1899 com_node(c, CHILD(n, i));
1900 com_addoparg(c, BUILD_TUPLE, len);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001901 com_pop(c, len-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001902 }
1903}
1904
1905
1906/* Begin of assignment compilation */
1907
Thomas Wouters434d0822000-08-24 20:11:32 +00001908
1909static void
1910com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn)
1911{
1912 com_addbyte(c, DUP_TOP);
1913 com_push(c, 1);
1914 com_addopname(c, LOAD_ATTR, n);
Thomas Wouters434d0822000-08-24 20:11:32 +00001915 com_node(c, augn);
1916 com_addbyte(c, opcode);
1917 com_pop(c, 1);
1918 com_addbyte(c, ROT_TWO);
1919 com_addopname(c, STORE_ATTR, n);
1920 com_pop(c, 2);
1921}
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001922
1923static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001924com_assign_attr(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001925{
1926 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001927 com_pop(c, assigning ? 2 : 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001928}
1929
1930static void
Thomas Wouters434d0822000-08-24 20:11:32 +00001931com_assign_trailer(struct compiling *c, node *n, int assigning, node *augn)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001932{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001933 REQ(n, trailer);
1934 switch (TYPE(CHILD(n, 0))) {
1935 case LPAR: /* '(' [exprlist] ')' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001936 com_error(c, PyExc_SyntaxError,
1937 "can't assign to function call");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001938 break;
1939 case DOT: /* '.' NAME */
Thomas Wouters434d0822000-08-24 20:11:32 +00001940 if (assigning > OP_APPLY)
1941 com_augassign_attr(c, CHILD(n, 1), assigning, augn);
1942 else
1943 com_assign_attr(c, CHILD(n, 1), assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001944 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00001945 case LSQB: /* '[' subscriptlist ']' */
Thomas Wouters434d0822000-08-24 20:11:32 +00001946 com_subscriptlist(c, CHILD(n, 1), assigning, augn);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001947 break;
1948 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001949 com_error(c, PyExc_SystemError, "unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001950 }
1951}
1952
1953static void
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001954com_assign_sequence(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001955{
1956 int i;
Thomas Woutersb59290f2000-08-25 05:41:11 +00001957 if (TYPE(n) != testlist && TYPE(n) != listmaker)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001958 REQ(n, exprlist);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001959 if (assigning) {
1960 i = (NCH(n)+1)/2;
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001961 com_addoparg(c, UNPACK_SEQUENCE, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001962 com_push(c, i-1);
1963 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001964 for (i = 0; i < NCH(n); i += 2)
Thomas Wouters434d0822000-08-24 20:11:32 +00001965 com_assign(c, CHILD(n, i), assigning, NULL);
1966}
1967
1968static void
1969com_augassign_name(struct compiling *c, node *n, int opcode, node *augn)
1970{
1971 REQ(n, NAME);
1972 com_addopname(c, LOAD_NAME, n);
1973 com_push(c, 1);
1974 com_node(c, augn);
1975 com_addbyte(c, opcode);
1976 com_pop(c, 1);
1977 com_assign_name(c, n, OP_ASSIGN);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001978}
1979
1980static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001981com_assign_name(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001982{
1983 REQ(n, NAME);
1984 com_addopname(c, assigning ? STORE_NAME : DELETE_NAME, n);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001985 if (assigning)
1986 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001987}
1988
1989static void
Thomas Wouters434d0822000-08-24 20:11:32 +00001990com_assign(struct compiling *c, node *n, int assigning, node *augn)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001991{
1992 /* Loop to avoid trivial recursion */
1993 for (;;) {
1994 switch (TYPE(n)) {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001995
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001996 case exprlist:
1997 case testlist:
1998 if (NCH(n) > 1) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001999 if (assigning > OP_APPLY) {
2000 com_error(c, PyExc_SyntaxError,
2001 "augmented assign to tuple not possible");
2002 return;
2003 }
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002004 com_assign_sequence(c, n, assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002005 return;
2006 }
2007 n = CHILD(n, 0);
2008 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002009
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002010 case test:
2011 case and_test:
2012 case not_test:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002013 case comparison:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002014 case expr:
Guido van Rossum7928cd71991-10-24 14:59:31 +00002015 case xor_expr:
2016 case and_expr:
2017 case shift_expr:
2018 case arith_expr:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002019 case term:
Guido van Rossum50564e81996-01-12 01:13:16 +00002020 case factor:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002021 if (NCH(n) > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002022 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002023 "can't assign to operator");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002024 return;
2025 }
2026 n = CHILD(n, 0);
2027 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002028
Guido van Rossum50564e81996-01-12 01:13:16 +00002029 case power: /* atom trailer* ('**' power)* */
2030/* ('+'|'-'|'~') factor | atom trailer* */
2031 if (TYPE(CHILD(n, 0)) != atom) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002032 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002033 "can't assign to operator");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002034 return;
2035 }
Guido van Rossum50564e81996-01-12 01:13:16 +00002036 if (NCH(n) > 1) { /* trailer or exponent present */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002037 int i;
2038 com_node(c, CHILD(n, 0));
2039 for (i = 1; i+1 < NCH(n); i++) {
Guido van Rossum50564e81996-01-12 01:13:16 +00002040 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002041 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002042 "can't assign to operator");
Guido van Rossum50564e81996-01-12 01:13:16 +00002043 return;
2044 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002045 com_apply_trailer(c, CHILD(n, i));
2046 } /* NB i is still alive */
2047 com_assign_trailer(c,
Thomas Wouters434d0822000-08-24 20:11:32 +00002048 CHILD(n, i), assigning, augn);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002049 return;
2050 }
2051 n = CHILD(n, 0);
2052 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002053
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002054 case atom:
2055 switch (TYPE(CHILD(n, 0))) {
2056 case LPAR:
2057 n = CHILD(n, 1);
2058 if (TYPE(n) == RPAR) {
2059 /* XXX Should allow () = () ??? */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002060 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002061 "can't assign to ()");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002062 return;
2063 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002064 if (assigning > OP_APPLY) {
2065 com_error(c, PyExc_SyntaxError,
2066 "augmented assign to tuple not possible");
2067 return;
2068 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002069 break;
2070 case LSQB:
2071 n = CHILD(n, 1);
2072 if (TYPE(n) == RSQB) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002073 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002074 "can't assign to []");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002075 return;
2076 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002077 if (assigning > OP_APPLY) {
2078 com_error(c, PyExc_SyntaxError,
2079 "augmented assign to list not possible");
2080 return;
2081 }
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002082 com_assign_sequence(c, n, assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002083 return;
2084 case NAME:
Thomas Wouters434d0822000-08-24 20:11:32 +00002085 if (assigning > OP_APPLY)
2086 com_augassign_name(c, CHILD(n, 0),
2087 assigning, augn);
2088 else
2089 com_assign_name(c, CHILD(n, 0),
2090 assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002091 return;
2092 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002093 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002094 "can't assign to literal");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002095 return;
2096 }
2097 break;
Guido van Rossum15cc9a01996-08-08 18:51:04 +00002098
2099 case lambdef:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002100 com_error(c, PyExc_SyntaxError,
2101 "can't assign to lambda");
Guido van Rossum15cc9a01996-08-08 18:51:04 +00002102 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002103
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002104 default:
Guido van Rossum8b993a91997-01-17 21:04:03 +00002105 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002106 com_error(c, PyExc_SystemError,
2107 "com_assign: bad node");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002108 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002109
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002110 }
2111 }
2112}
Guido van Rossum7c531111997-03-11 18:42:21 +00002113
Thomas Wouters434d0822000-08-24 20:11:32 +00002114static void
2115com_augassign(struct compiling *c, node *n)
2116{
2117 int opcode;
2118
2119 switch (STR(CHILD(CHILD(n, 1), 0))[0]) {
2120 case '+': opcode = INPLACE_ADD; break;
2121 case '-': opcode = INPLACE_SUBTRACT; break;
2122 case '/': opcode = INPLACE_DIVIDE; break;
2123 case '%': opcode = INPLACE_MODULO; break;
2124 case '<': opcode = INPLACE_LSHIFT; break;
2125 case '>': opcode = INPLACE_RSHIFT; break;
2126 case '&': opcode = INPLACE_AND; break;
2127 case '^': opcode = INPLACE_XOR; break;
2128 case '|': opcode = INPLACE_OR; break;
2129 case '*':
2130 if (STR(CHILD(CHILD(n, 1), 0))[1] == '*')
2131 opcode = INPLACE_POWER;
2132 else
2133 opcode = INPLACE_MULTIPLY;
2134 break;
2135 default:
2136 com_error(c, PyExc_SystemError, "com_augassign: bad operator");
2137 return;
2138 }
2139 com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2));
2140}
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002141
2142static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002143com_expr_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002144{
Thomas Wouters434d0822000-08-24 20:11:32 +00002145 REQ(n, expr_stmt);
2146 /* testlist (('=' testlist)* | augassign testlist) */
Guido van Rossum8b993a91997-01-17 21:04:03 +00002147 /* Forget it if we have just a doc string here */
Guido van Rossum5f5e8171997-04-06 03:41:40 +00002148 if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
Guido van Rossum8b993a91997-01-17 21:04:03 +00002149 return;
Thomas Wouters434d0822000-08-24 20:11:32 +00002150 if (NCH(n) == 1) {
2151 com_node(c, CHILD(n, NCH(n)-1));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002152 if (c->c_interactive)
2153 com_addbyte(c, PRINT_EXPR);
2154 else
2155 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002156 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002157 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002158 else if (TYPE(CHILD(n,1)) == augassign)
2159 com_augassign(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002160 else {
2161 int i;
Thomas Wouters434d0822000-08-24 20:11:32 +00002162 com_node(c, CHILD(n, NCH(n)-1));
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002163 for (i = 0; i < NCH(n)-2; i+=2) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00002164 if (i+2 < NCH(n)-2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002165 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002166 com_push(c, 1);
2167 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002168 com_assign(c, CHILD(n, i), OP_ASSIGN, NULL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002169 }
2170 }
2171}
2172
2173static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002174com_assert_stmt(struct compiling *c, node *n)
Guido van Rossum228d7f31997-04-02 05:24:36 +00002175{
2176 int a = 0, b = 0;
2177 int i;
2178 REQ(n, assert_stmt); /* 'assert' test [',' test] */
2179 /* Generate code like for
2180
2181 if __debug__:
2182 if not <test>:
2183 raise AssertionError [, <message>]
2184
2185 where <message> is the second test, if present.
2186 */
2187 if (Py_OptimizeFlag)
2188 return;
2189 com_addopnamestr(c, LOAD_GLOBAL, "__debug__");
2190 com_push(c, 1);
2191 com_addfwref(c, JUMP_IF_FALSE, &a);
2192 com_addbyte(c, POP_TOP);
2193 com_pop(c, 1);
2194 com_node(c, CHILD(n, 1));
2195 com_addfwref(c, JUMP_IF_TRUE, &b);
2196 com_addbyte(c, POP_TOP);
2197 com_pop(c, 1);
2198 /* Raise that exception! */
2199 com_addopnamestr(c, LOAD_GLOBAL, "AssertionError");
2200 com_push(c, 1);
2201 i = NCH(n)/2; /* Either 2 or 4 */
2202 if (i > 1)
2203 com_node(c, CHILD(n, 3));
2204 com_addoparg(c, RAISE_VARARGS, i);
2205 com_pop(c, i);
2206 /* The interpreter does not fall through */
2207 /* All jumps converge here */
2208 com_backpatch(c, a);
2209 com_backpatch(c, b);
2210 com_addbyte(c, POP_TOP);
2211}
2212
2213static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002214com_print_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002215{
Barry Warsaw29c574e2000-08-21 15:38:56 +00002216 int i = 1;
2217 node* stream = NULL;
2218
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002219 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002220
2221 /* are we using the extended print form? */
2222 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2223 stream = CHILD(n, 2);
Barry Warsaw24703a02000-08-21 17:07:20 +00002224 com_node(c, stream);
2225 /* stack: [...] => [... stream] */
2226 com_push(c, 1);
Barry Warsaw29c574e2000-08-21 15:38:56 +00002227 if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA)
2228 i = 4;
2229 else
2230 i = 3;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002231 }
Barry Warsaw29c574e2000-08-21 15:38:56 +00002232 for (; i < NCH(n); i += 2) {
2233 if (stream != NULL) {
Barry Warsaw24703a02000-08-21 17:07:20 +00002234 com_addbyte(c, DUP_TOP);
2235 /* stack: [stream] => [stream stream] */
2236 com_push(c, 1);
Barry Warsaw29c574e2000-08-21 15:38:56 +00002237 com_node(c, CHILD(n, i));
Barry Warsaw24703a02000-08-21 17:07:20 +00002238 /* stack: [stream stream] => [stream stream obj] */
2239 com_addbyte(c, ROT_TWO);
2240 /* stack: [stream stream obj] => [stream obj stream] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002241 com_addbyte(c, PRINT_ITEM_TO);
Barry Warsaw24703a02000-08-21 17:07:20 +00002242 /* stack: [stream obj stream] => [stream] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002243 com_pop(c, 2);
2244 }
2245 else {
Barry Warsaw29c574e2000-08-21 15:38:56 +00002246 com_node(c, CHILD(n, i));
Barry Warsaw24703a02000-08-21 17:07:20 +00002247 /* stack: [...] => [... obj] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002248 com_addbyte(c, PRINT_ITEM);
2249 com_pop(c, 1);
2250 }
2251 }
2252 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
Barry Warsaw24703a02000-08-21 17:07:20 +00002253 if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) {
Barry Warsaw29c574e2000-08-21 15:38:56 +00002254 if (stream != NULL) {
Barry Warsaw24703a02000-08-21 17:07:20 +00002255 /* must pop the extra stream object off the stack */
2256 com_addbyte(c, POP_TOP);
2257 /* stack: [... stream] => [...] */
2258 com_pop(c, 1);
2259 }
2260 }
2261 else {
2262 if (stream != NULL) {
2263 /* this consumes the last stream object on stack */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002264 com_addbyte(c, PRINT_NEWLINE_TO);
Barry Warsaw24703a02000-08-21 17:07:20 +00002265 /* stack: [... stream] => [...] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002266 com_pop(c, 1);
2267 }
2268 else
2269 com_addbyte(c, PRINT_NEWLINE);
2270 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002271}
2272
2273static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002274com_return_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002275{
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002276 REQ(n, return_stmt); /* 'return' [testlist] */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002277 if (!c->c_infunction) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002278 com_error(c, PyExc_SyntaxError, "'return' outside function");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002279 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002280 if (NCH(n) < 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002281 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002282 com_push(c, 1);
2283 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002284 else
2285 com_node(c, CHILD(n, 1));
2286 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002287 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002288}
2289
2290static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002291com_raise_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002292{
Guido van Rossum8b993a91997-01-17 21:04:03 +00002293 int i;
Guido van Rossumd295f121998-04-09 21:39:57 +00002294 REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */
2295 if (NCH(n) > 1) {
2296 com_node(c, CHILD(n, 1));
2297 if (NCH(n) > 3) {
2298 com_node(c, CHILD(n, 3));
2299 if (NCH(n) > 5)
2300 com_node(c, CHILD(n, 5));
2301 }
Guido van Rossumf10570b1995-07-07 22:53:21 +00002302 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002303 i = NCH(n)/2;
2304 com_addoparg(c, RAISE_VARARGS, i);
2305 com_pop(c, i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002306}
2307
2308static void
Thomas Wouters52152252000-08-17 22:55:00 +00002309com_from_import(struct compiling *c, node *n)
2310{
2311 com_addopname(c, IMPORT_FROM, CHILD(n, 0));
2312 com_push(c, 1);
2313 if (NCH(n) > 1) {
2314 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2315 com_error(c, PyExc_SyntaxError, "invalid syntax");
2316 return;
2317 }
2318 com_addopname(c, STORE_NAME, CHILD(n, 2));
2319 } else
2320 com_addopname(c, STORE_NAME, CHILD(n, 0));
2321 com_pop(c, 1);
2322}
2323
2324static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002325com_import_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002326{
2327 int i;
2328 REQ(n, import_stmt);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002329 /* 'import' dotted_name (',' dotted_name)* |
2330 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002331 if (STR(CHILD(n, 0))[0] == 'f') {
Guido van Rossum83fb0732000-11-27 22:22:36 +00002332 PyObject *tup;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002333 /* 'from' dotted_name 'import' ... */
2334 REQ(CHILD(n, 1), dotted_name);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002335
2336 if (TYPE(CHILD(n, 3)) == STAR) {
2337 tup = Py_BuildValue("(s)", "*");
2338 } else {
2339 tup = PyTuple_New((NCH(n) - 2)/2);
2340 for (i = 3; i < NCH(n); i += 2) {
2341 PyTuple_SET_ITEM(tup, (i-3)/2,
2342 PyString_FromString(STR(
2343 CHILD(CHILD(n, i), 0))));
2344 }
2345 }
2346 com_addoparg(c, LOAD_CONST, com_addconst(c, tup));
Guido van Rossum83fb0732000-11-27 22:22:36 +00002347 Py_DECREF(tup);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002348 com_push(c, 1);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002349 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
Thomas Wouters52152252000-08-17 22:55:00 +00002350 if (TYPE(CHILD(n, 3)) == STAR)
2351 com_addbyte(c, IMPORT_STAR);
2352 else {
2353 for (i = 3; i < NCH(n); i += 2)
2354 com_from_import(c, CHILD(n, i));
2355 com_addbyte(c, POP_TOP);
2356 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002357 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002358 }
2359 else {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002360 /* 'import' ... */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002361 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters52152252000-08-17 22:55:00 +00002362 node *subn = CHILD(n, i);
2363 REQ(subn, dotted_as_name);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002364 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002365 com_push(c, 1);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002366 com_addopname(c, IMPORT_NAME, CHILD(subn, 0));
Thomas Wouters52152252000-08-17 22:55:00 +00002367 if (NCH(subn) > 1) {
Thomas Wouterse753ef82000-08-27 20:16:32 +00002368 int j;
2369 if (strcmp(STR(CHILD(subn, 1)), "as") != 0) {
Thomas Wouters52152252000-08-17 22:55:00 +00002370 com_error(c, PyExc_SyntaxError,
2371 "invalid syntax");
2372 return;
2373 }
Thomas Wouterse753ef82000-08-27 20:16:32 +00002374 for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2)
2375 com_addopname(c, LOAD_ATTR,
2376 CHILD(CHILD(subn, 0), j));
Thomas Wouters52152252000-08-17 22:55:00 +00002377 com_addopname(c, STORE_NAME, CHILD(subn, 2));
2378 } else
2379 com_addopname(c, STORE_NAME,
2380 CHILD(CHILD(subn, 0),0));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002381 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002382 }
2383 }
2384}
2385
2386static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002387com_global_stmt(struct compiling *c, node *n)
Guido van Rossumc5e96291991-12-10 13:53:51 +00002388{
2389 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00002390 REQ(n, global_stmt);
2391 /* 'global' NAME (',' NAME)* */
2392 for (i = 1; i < NCH(n); i += 2) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002393 char *s = STR(CHILD(n, i));
Guido van Rossumfe2236f1996-08-24 07:29:04 +00002394#ifdef PRIVATE_NAME_MANGLING
2395 char buffer[256];
2396 if (s != NULL && s[0] == '_' && s[1] == '_' &&
2397 c->c_private != NULL &&
Guido van Rossum582acec2000-06-28 22:07:35 +00002398 com_mangle(c, s, buffer, sizeof(buffer)))
Guido van Rossumfe2236f1996-08-24 07:29:04 +00002399 s = buffer;
2400#endif
Guido van Rossum79f25d91997-04-29 20:08:16 +00002401 if (PyDict_GetItemString(c->c_locals, s) != NULL) {
2402 com_error(c, PyExc_SyntaxError,
2403 "name is local and global");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002404 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002405 else if (PyDict_SetItemString(c->c_globals, s, Py_None) != 0)
Guido van Rossumc5e96291991-12-10 13:53:51 +00002406 c->c_errors++;
2407 }
2408}
2409
Guido van Rossum681d79a1995-07-18 14:51:37 +00002410static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002411com_newlocal_o(struct compiling *c, PyObject *nameval)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002412{
2413 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002414 PyObject *ival;
2415 if (PyList_Size(c->c_varnames) != c->c_nlocals) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002416 /* This is usually caused by an error on a previous call */
2417 if (c->c_errors == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002418 com_error(c, PyExc_SystemError,
2419 "mixed up var name/index");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002420 }
2421 return 0;
2422 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002423 ival = PyInt_FromLong(i = c->c_nlocals++);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002424 if (ival == NULL)
2425 c->c_errors++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002426 else if (PyDict_SetItem(c->c_locals, nameval, ival) != 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002427 c->c_errors++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002428 else if (PyList_Append(c->c_varnames, nameval) != 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002429 c->c_errors++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002430 Py_XDECREF(ival);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002431 return i;
2432}
2433
2434static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002435com_addlocal_o(struct compiling *c, PyObject *nameval)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002436{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002437 PyObject *ival = PyDict_GetItem(c->c_locals, nameval);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002438 if (ival != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00002439 return PyInt_AsLong(ival);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002440 return com_newlocal_o(c, nameval);
2441}
2442
2443static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002444com_newlocal(struct compiling *c, char *name)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002445{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002446 PyObject *nameval = PyString_InternFromString(name);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002447 int i;
2448 if (nameval == NULL) {
2449 c->c_errors++;
2450 return 0;
2451 }
2452 i = com_newlocal_o(c, nameval);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002453 Py_DECREF(nameval);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002454 return i;
2455}
2456
Guido van Rossumc5e96291991-12-10 13:53:51 +00002457static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002458com_exec_stmt(struct compiling *c, node *n)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002459{
2460 REQ(n, exec_stmt);
2461 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
2462 com_node(c, CHILD(n, 1));
2463 if (NCH(n) >= 4)
2464 com_node(c, CHILD(n, 3));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002465 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002466 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002467 com_push(c, 1);
2468 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002469 if (NCH(n) >= 6)
2470 com_node(c, CHILD(n, 5));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002471 else {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002472 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002473 com_push(c, 1);
2474 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002475 com_addbyte(c, EXEC_STMT);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002476 com_pop(c, 3);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002477}
2478
Guido van Rossum7c531111997-03-11 18:42:21 +00002479static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002480is_constant_false(struct compiling *c, node *n)
Guido van Rossum7c531111997-03-11 18:42:21 +00002481{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002482 PyObject *v;
Guido van Rossum7c531111997-03-11 18:42:21 +00002483 int i;
2484
2485 /* Label to avoid tail recursion */
2486 next:
2487 switch (TYPE(n)) {
2488
2489 case suite:
2490 if (NCH(n) == 1) {
2491 n = CHILD(n, 0);
2492 goto next;
2493 }
2494 /* Fall through */
2495 case file_input:
2496 for (i = 0; i < NCH(n); i++) {
2497 node *ch = CHILD(n, i);
2498 if (TYPE(ch) == stmt) {
2499 n = ch;
2500 goto next;
2501 }
2502 }
2503 break;
2504
2505 case stmt:
2506 case simple_stmt:
2507 case small_stmt:
2508 n = CHILD(n, 0);
2509 goto next;
2510
2511 case expr_stmt:
2512 case testlist:
2513 case test:
2514 case and_test:
2515 case not_test:
2516 case comparison:
2517 case expr:
2518 case xor_expr:
2519 case and_expr:
2520 case shift_expr:
2521 case arith_expr:
2522 case term:
2523 case factor:
2524 case power:
2525 case atom:
2526 if (NCH(n) == 1) {
2527 n = CHILD(n, 0);
2528 goto next;
2529 }
2530 break;
2531
2532 case NAME:
2533 if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
2534 return 1;
2535 break;
2536
2537 case NUMBER:
2538 v = parsenumber(c, STR(n));
2539 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002540 PyErr_Clear();
Guido van Rossum7c531111997-03-11 18:42:21 +00002541 break;
2542 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002543 i = PyObject_IsTrue(v);
2544 Py_DECREF(v);
Guido van Rossum7c531111997-03-11 18:42:21 +00002545 return i == 0;
2546
2547 case STRING:
Guido van Rossum6af0c001997-03-11 21:25:55 +00002548 v = parsestr(STR(n));
Guido van Rossum7c531111997-03-11 18:42:21 +00002549 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002550 PyErr_Clear();
Guido van Rossum7c531111997-03-11 18:42:21 +00002551 break;
2552 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002553 i = PyObject_IsTrue(v);
2554 Py_DECREF(v);
Guido van Rossum7c531111997-03-11 18:42:21 +00002555 return i == 0;
2556
2557 }
2558 return 0;
2559}
2560
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002561static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002562com_if_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002563{
2564 int i;
2565 int anchor = 0;
2566 REQ(n, if_stmt);
2567 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
2568 for (i = 0; i+3 < NCH(n); i+=4) {
2569 int a = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002570 node *ch = CHILD(n, i+1);
Guido van Rossum7c531111997-03-11 18:42:21 +00002571 if (is_constant_false(c, ch))
2572 continue;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002573 if (i > 0)
2574 com_addoparg(c, SET_LINENO, ch->n_lineno);
Guido van Rossum7c531111997-03-11 18:42:21 +00002575 com_node(c, ch);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002576 com_addfwref(c, JUMP_IF_FALSE, &a);
2577 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002578 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002579 com_node(c, CHILD(n, i+3));
2580 com_addfwref(c, JUMP_FORWARD, &anchor);
2581 com_backpatch(c, a);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002582 /* We jump here with an extra entry which we now pop */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002583 com_addbyte(c, POP_TOP);
2584 }
2585 if (i+2 < NCH(n))
2586 com_node(c, CHILD(n, i+2));
Guido van Rossum7c531111997-03-11 18:42:21 +00002587 if (anchor)
2588 com_backpatch(c, anchor);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002589}
2590
2591static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002592com_while_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002593{
2594 int break_anchor = 0;
2595 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002596 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002597 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
2598 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002599 block_push(c, SETUP_LOOP);
2600 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002601 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002602 com_node(c, CHILD(n, 1));
2603 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2604 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002605 com_pop(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002606 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002607 com_node(c, CHILD(n, 3));
Guido van Rossum3f5da241990-12-20 15:06:42 +00002608 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002609 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2610 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002611 com_backpatch(c, anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002612 /* We jump here with one entry more on the stack */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002613 com_addbyte(c, POP_TOP);
2614 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002615 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002616 if (NCH(n) > 4)
2617 com_node(c, CHILD(n, 6));
2618 com_backpatch(c, break_anchor);
2619}
2620
2621static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002622com_for_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002623{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002624 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002625 int break_anchor = 0;
2626 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002627 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002628 REQ(n, for_stmt);
2629 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
2630 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002631 block_push(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002632 com_node(c, CHILD(n, 3));
Guido van Rossum79f25d91997-04-29 20:08:16 +00002633 v = PyInt_FromLong(0L);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002634 if (v == NULL)
2635 c->c_errors++;
2636 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002637 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002638 Py_XDECREF(v);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002639 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002640 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002641 com_addfwref(c, FOR_LOOP, &anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002642 com_push(c, 1);
Thomas Wouters434d0822000-08-24 20:11:32 +00002643 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002644 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002645 com_node(c, CHILD(n, 5));
Guido van Rossum3f5da241990-12-20 15:06:42 +00002646 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002647 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2648 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002649 com_backpatch(c, anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002650 com_pop(c, 2); /* FOR_LOOP has popped these */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002651 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002652 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002653 if (NCH(n) > 8)
2654 com_node(c, CHILD(n, 8));
2655 com_backpatch(c, break_anchor);
2656}
2657
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002658/* Code generated for "try: S finally: Sf" is as follows:
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002659
2660 SETUP_FINALLY L
2661 <code for S>
2662 POP_BLOCK
2663 LOAD_CONST <nil>
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002664 L: <code for Sf>
2665 END_FINALLY
2666
2667 The special instructions use the block stack. Each block
2668 stack entry contains the instruction that created it (here
2669 SETUP_FINALLY), the level of the value stack at the time the
2670 block stack entry was created, and a label (here L).
2671
2672 SETUP_FINALLY:
2673 Pushes the current value stack level and the label
2674 onto the block stack.
2675 POP_BLOCK:
2676 Pops en entry from the block stack, and pops the value
2677 stack until its level is the same as indicated on the
2678 block stack. (The label is ignored.)
2679 END_FINALLY:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002680 Pops a variable number of entries from the *value* stack
2681 and re-raises the exception they specify. The number of
2682 entries popped depends on the (pseudo) exception type.
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002683
2684 The block stack is unwound when an exception is raised:
2685 when a SETUP_FINALLY entry is found, the exception is pushed
2686 onto the value stack (and the exception condition is cleared),
2687 and the interpreter jumps to the label gotten from the block
2688 stack.
2689
2690 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
Guido van Rossum3f5da241990-12-20 15:06:42 +00002691 (The contents of the value stack is shown in [], with the top
2692 at the right; 'tb' is trace-back info, 'val' the exception's
2693 associated value, and 'exc' the exception.)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002694
2695 Value stack Label Instruction Argument
2696 [] SETUP_EXCEPT L1
2697 [] <code for S>
2698 [] POP_BLOCK
2699 [] JUMP_FORWARD L0
2700
Guido van Rossum3f5da241990-12-20 15:06:42 +00002701 [tb, val, exc] L1: DUP )
2702 [tb, val, exc, exc] <evaluate E1> )
2703 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2704 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2705 [tb, val, exc, 1] POP )
2706 [tb, val, exc] POP
2707 [tb, val] <assign to V1> (or POP if no V1)
2708 [tb] POP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002709 [] <code for S1>
2710 JUMP_FORWARD L0
2711
Guido van Rossum3f5da241990-12-20 15:06:42 +00002712 [tb, val, exc, 0] L2: POP
2713 [tb, val, exc] DUP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002714 .............................etc.......................
2715
Guido van Rossum3f5da241990-12-20 15:06:42 +00002716 [tb, val, exc, 0] Ln+1: POP
2717 [tb, val, exc] END_FINALLY # re-raise exception
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002718
2719 [] L0: <next statement>
2720
2721 Of course, parts are not generated if Vi or Ei is not present.
2722*/
2723
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002724static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002725com_try_except(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002726{
2727 int except_anchor = 0;
2728 int end_anchor = 0;
2729 int else_anchor = 0;
2730 int i;
2731 node *ch;
2732
2733 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
2734 block_push(c, SETUP_EXCEPT);
2735 com_node(c, CHILD(n, 2));
2736 com_addbyte(c, POP_BLOCK);
2737 block_pop(c, SETUP_EXCEPT);
2738 com_addfwref(c, JUMP_FORWARD, &else_anchor);
2739 com_backpatch(c, except_anchor);
2740 for (i = 3;
2741 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
2742 i += 3) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00002743 /* except_clause: 'except' [expr [',' var]] */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002744 if (except_anchor == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002745 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002746 "default 'except:' must be last");
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002747 break;
2748 }
2749 except_anchor = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00002750 com_push(c, 3); /* tb, val, exc pushed by exception */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002751 com_addoparg(c, SET_LINENO, ch->n_lineno);
2752 if (NCH(ch) > 1) {
2753 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002754 com_push(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002755 com_node(c, CHILD(ch, 1));
2756 com_addoparg(c, COMPARE_OP, EXC_MATCH);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002757 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002758 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
2759 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002760 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002761 }
2762 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002763 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002764 if (NCH(ch) > 3)
Thomas Wouters434d0822000-08-24 20:11:32 +00002765 com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002766 else {
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002767 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002768 com_pop(c, 1);
2769 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002770 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002771 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002772 com_node(c, CHILD(n, i+2));
2773 com_addfwref(c, JUMP_FORWARD, &end_anchor);
2774 if (except_anchor) {
2775 com_backpatch(c, except_anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002776 /* We come in with [tb, val, exc, 0] on the
2777 stack; one pop and it's the same as
2778 expected at the start of the loop */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002779 com_addbyte(c, POP_TOP);
2780 }
2781 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002782 /* We actually come in here with [tb, val, exc] but the
2783 END_FINALLY will zap those and jump around.
2784 The c_stacklevel does not reflect them so we need not pop
2785 anything. */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002786 com_addbyte(c, END_FINALLY);
2787 com_backpatch(c, else_anchor);
2788 if (i < NCH(n))
2789 com_node(c, CHILD(n, i+2));
2790 com_backpatch(c, end_anchor);
2791}
2792
2793static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002794com_try_finally(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002795{
2796 int finally_anchor = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002797 node *ch;
Guido van Rossum94fb82e1992-04-05 14:24:50 +00002798
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002799 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
2800 block_push(c, SETUP_FINALLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002801 com_node(c, CHILD(n, 2));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002802 com_addbyte(c, POP_BLOCK);
2803 block_pop(c, SETUP_FINALLY);
2804 block_push(c, END_FINALLY);
Guido van Rossum79f25d91997-04-29 20:08:16 +00002805 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002806 /* While the generated code pushes only one item,
2807 the try-finally handling can enter here with
2808 up to three items. OK, here are the details:
2809 3 for an exception, 2 for RETURN, 1 for BREAK. */
2810 com_push(c, 3);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002811 com_backpatch(c, finally_anchor);
2812 ch = CHILD(n, NCH(n)-1);
2813 com_addoparg(c, SET_LINENO, ch->n_lineno);
2814 com_node(c, ch);
2815 com_addbyte(c, END_FINALLY);
2816 block_pop(c, END_FINALLY);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002817 com_pop(c, 3); /* Matches the com_push above */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002818}
2819
2820static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002821com_try_stmt(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002822{
2823 REQ(n, try_stmt);
2824 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
2825 | 'try' ':' suite 'finally' ':' suite */
2826 if (TYPE(CHILD(n, 3)) != except_clause)
2827 com_try_finally(c, n);
2828 else
2829 com_try_except(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002830}
2831
Guido van Rossum8b993a91997-01-17 21:04:03 +00002832static node *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002833get_rawdocstring(node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002834{
Guido van Rossum164d4ff1995-01-26 00:40:09 +00002835 int i;
2836
Guido van Rossum8b993a91997-01-17 21:04:03 +00002837 /* Label to avoid tail recursion */
2838 next:
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002839 switch (TYPE(n)) {
2840
2841 case suite:
Guido van Rossum8b993a91997-01-17 21:04:03 +00002842 if (NCH(n) == 1) {
2843 n = CHILD(n, 0);
2844 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002845 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002846 /* Fall through */
Guido van Rossum164d4ff1995-01-26 00:40:09 +00002847 case file_input:
2848 for (i = 0; i < NCH(n); i++) {
2849 node *ch = CHILD(n, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002850 if (TYPE(ch) == stmt) {
2851 n = ch;
2852 goto next;
2853 }
Guido van Rossum164d4ff1995-01-26 00:40:09 +00002854 }
2855 break;
2856
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002857 case stmt:
2858 case simple_stmt:
2859 case small_stmt:
Guido van Rossum8b993a91997-01-17 21:04:03 +00002860 n = CHILD(n, 0);
2861 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002862
2863 case expr_stmt:
2864 case testlist:
2865 case test:
2866 case and_test:
2867 case not_test:
2868 case comparison:
2869 case expr:
2870 case xor_expr:
2871 case and_expr:
2872 case shift_expr:
2873 case arith_expr:
2874 case term:
2875 case factor:
Guido van Rossum50564e81996-01-12 01:13:16 +00002876 case power:
Guido van Rossum8b993a91997-01-17 21:04:03 +00002877 if (NCH(n) == 1) {
2878 n = CHILD(n, 0);
2879 goto next;
2880 }
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002881 break;
2882
2883 case atom:
2884 if (TYPE(CHILD(n, 0)) == STRING)
Guido van Rossum8b993a91997-01-17 21:04:03 +00002885 return n;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002886 break;
2887
2888 }
2889 return NULL;
2890}
2891
Guido van Rossum79f25d91997-04-29 20:08:16 +00002892static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002893get_docstring(node *n)
Guido van Rossum8b993a91997-01-17 21:04:03 +00002894{
Guido van Rossum541563e1999-01-28 15:08:09 +00002895 /* Don't generate doc-strings if run with -OO */
2896 if (Py_OptimizeFlag > 1)
2897 return NULL;
Guido van Rossum8b993a91997-01-17 21:04:03 +00002898 n = get_rawdocstring(n);
2899 if (n == NULL)
2900 return NULL;
2901 return parsestrplus(n);
2902}
2903
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002904static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002905com_suite(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002906{
2907 REQ(n, suite);
2908 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
2909 if (NCH(n) == 1) {
2910 com_node(c, CHILD(n, 0));
2911 }
2912 else {
2913 int i;
2914 for (i = 0; i < NCH(n); i++) {
2915 node *ch = CHILD(n, i);
2916 if (TYPE(ch) == stmt)
2917 com_node(c, ch);
2918 }
2919 }
2920}
2921
Guido van Rossumf1aeab71992-03-27 17:28:26 +00002922/* ARGSUSED */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002923static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002924com_continue_stmt(struct compiling *c, node *n)
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002925{
2926 int i = c->c_nblocks;
2927 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
2928 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2929 }
Fred Drakefd1f1be2000-09-08 16:31:24 +00002930 else if (i <= 0) {
2931 /* at the outer level */
2932 com_error(c, PyExc_SyntaxError,
2933 "'continue' not properly in loop");
2934 }
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002935 else {
Fred Drakefd1f1be2000-09-08 16:31:24 +00002936 int j;
2937 for (j = 0; j <= i; ++j) {
2938 if (c->c_block[j] == SETUP_LOOP)
2939 break;
2940 }
2941 if (j < i+1) {
2942 /* there is a loop, but something interferes */
2943 for (++j; j <= i; ++j) {
2944 if (c->c_block[i] == SETUP_EXCEPT
2945 || c->c_block[i] == SETUP_FINALLY) {
2946 com_error(c, PyExc_SyntaxError,
2947 "'continue' not supported inside 'try' clause");
2948 return;
2949 }
2950 }
2951 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002952 com_error(c, PyExc_SyntaxError,
2953 "'continue' not properly in loop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002954 }
2955 /* XXX Could allow it inside a 'finally' clause
2956 XXX if we could pop the exception still on the stack */
2957}
2958
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002959static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002960com_argdefs(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002961{
Guido van Rossum681d79a1995-07-18 14:51:37 +00002962 int i, nch, nargs, ndefs;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002963 if (TYPE(n) == lambdef) {
2964 /* lambdef: 'lambda' [varargslist] ':' test */
2965 n = CHILD(n, 1);
2966 }
2967 else {
2968 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
2969 n = CHILD(n, 2);
2970 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
2971 n = CHILD(n, 1);
2972 }
2973 if (TYPE(n) != varargslist)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002974 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002975 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00002976 (fpdef ['=' test] ',')* '*' ....... |
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002977 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
2978 nch = NCH(n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002979 nargs = 0;
2980 ndefs = 0;
2981 for (i = 0; i < nch; i++) {
2982 int t;
Guido van Rossum79f25d91997-04-29 20:08:16 +00002983 if (TYPE(CHILD(n, i)) == STAR ||
2984 TYPE(CHILD(n, i)) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00002985 break;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002986 nargs++;
2987 i++;
2988 if (i >= nch)
Guido van Rossuma1e7e621995-09-18 21:44:04 +00002989 t = RPAR; /* Anything except EQUAL or COMMA */
2990 else
2991 t = TYPE(CHILD(n, i));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002992 if (t == EQUAL) {
2993 i++;
2994 ndefs++;
2995 com_node(c, CHILD(n, i));
2996 i++;
2997 if (i >= nch)
2998 break;
2999 t = TYPE(CHILD(n, i));
3000 }
3001 else {
Guido van Rossum29d38cd1998-10-02 13:41:54 +00003002 /* Treat "(a=1, b)" as an error */
3003 if (ndefs)
3004 com_error(c, PyExc_SyntaxError,
Guido van Rossumdfede311998-10-02 14:06:56 +00003005 "non-default argument follows default argument");
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003006 }
3007 if (t != COMMA)
3008 break;
3009 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003010 return ndefs;
3011}
3012
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003013static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003014com_funcdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003015{
Guido van Rossum79f25d91997-04-29 20:08:16 +00003016 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003017 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum79f25d91997-04-29 20:08:16 +00003018 v = (PyObject *)icompile(n, c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003019 if (v == NULL)
3020 c->c_errors++;
3021 else {
3022 int i = com_addconst(c, v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003023 int ndefs = com_argdefs(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003024 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003025 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003026 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003027 com_pop(c, ndefs);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003028 com_addopname(c, STORE_NAME, CHILD(n, 1));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003029 com_pop(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003030 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003031 }
3032}
3033
3034static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003035com_bases(struct compiling *c, node *n)
Guido van Rossumc5e96291991-12-10 13:53:51 +00003036{
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003037 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003038 REQ(n, testlist);
3039 /* testlist: test (',' test)* [','] */
3040 for (i = 0; i < NCH(n); i += 2)
3041 com_node(c, CHILD(n, i));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003042 i = (NCH(n)+1) / 2;
3043 com_addoparg(c, BUILD_TUPLE, i);
3044 com_pop(c, i-1);
Guido van Rossumc5e96291991-12-10 13:53:51 +00003045}
3046
3047static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003048com_classdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003049{
Guido van Rossum25831651993-05-19 14:50:45 +00003050 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003051 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003052 REQ(n, classdef);
Guido van Rossum25831651993-05-19 14:50:45 +00003053 /* classdef: class NAME ['(' testlist ')'] ':' suite */
Guido van Rossum7b89b6a1997-01-18 08:02:57 +00003054 if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
Guido van Rossum25831651993-05-19 14:50:45 +00003055 c->c_errors++;
3056 return;
3057 }
3058 /* Push the class name on the stack */
3059 i = com_addconst(c, v);
3060 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003061 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003062 Py_DECREF(v);
Guido van Rossum25831651993-05-19 14:50:45 +00003063 /* Push the tuple of base classes on the stack */
Guido van Rossum8b993a91997-01-17 21:04:03 +00003064 if (TYPE(CHILD(n, 2)) != LPAR) {
Guido van Rossumc5e96291991-12-10 13:53:51 +00003065 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003066 com_push(c, 1);
3067 }
Guido van Rossum25831651993-05-19 14:50:45 +00003068 else
3069 com_bases(c, CHILD(n, 3));
Guido van Rossum79f25d91997-04-29 20:08:16 +00003070 v = (PyObject *)icompile(n, c);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003071 if (v == NULL)
3072 c->c_errors++;
3073 else {
Guido van Rossum25831651993-05-19 14:50:45 +00003074 i = com_addconst(c, v);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003075 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003076 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003077 com_addoparg(c, MAKE_FUNCTION, 0);
3078 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003079 com_addbyte(c, BUILD_CLASS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003080 com_pop(c, 2);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003081 com_addopname(c, STORE_NAME, CHILD(n, 1));
Guido van Rossum79f25d91997-04-29 20:08:16 +00003082 Py_DECREF(v);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003083 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003084}
3085
3086static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003087com_node(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003088{
3089 switch (TYPE(n)) {
3090
3091 /* Definition nodes */
3092
3093 case funcdef:
3094 com_funcdef(c, n);
3095 break;
3096 case classdef:
3097 com_classdef(c, n);
3098 break;
3099
3100 /* Trivial parse tree nodes */
3101
3102 case stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003103 case small_stmt:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003104 case flow_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003105 com_node(c, CHILD(n, 0));
3106 break;
3107
3108 case simple_stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003109 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
3110 com_addoparg(c, SET_LINENO, n->n_lineno);
3111 {
3112 int i;
3113 for (i = 0; i < NCH(n)-1; i += 2)
3114 com_node(c, CHILD(n, i));
3115 }
3116 break;
3117
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003118 case compound_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003119 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003120 com_node(c, CHILD(n, 0));
3121 break;
3122
3123 /* Statement nodes */
3124
3125 case expr_stmt:
3126 com_expr_stmt(c, n);
3127 break;
3128 case print_stmt:
3129 com_print_stmt(c, n);
3130 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003131 case del_stmt: /* 'del' exprlist */
Thomas Wouters434d0822000-08-24 20:11:32 +00003132 com_assign(c, CHILD(n, 1), OP_DELETE, NULL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003133 break;
3134 case pass_stmt:
3135 break;
3136 case break_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003137 if (c->c_loops == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003138 com_error(c, PyExc_SyntaxError,
3139 "'break' outside loop");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003140 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003141 com_addbyte(c, BREAK_LOOP);
3142 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003143 case continue_stmt:
3144 com_continue_stmt(c, n);
3145 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003146 case return_stmt:
3147 com_return_stmt(c, n);
3148 break;
3149 case raise_stmt:
3150 com_raise_stmt(c, n);
3151 break;
3152 case import_stmt:
3153 com_import_stmt(c, n);
3154 break;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003155 case global_stmt:
3156 com_global_stmt(c, n);
3157 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003158 case exec_stmt:
3159 com_exec_stmt(c, n);
3160 break;
Guido van Rossum228d7f31997-04-02 05:24:36 +00003161 case assert_stmt:
3162 com_assert_stmt(c, n);
3163 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003164 case if_stmt:
3165 com_if_stmt(c, n);
3166 break;
3167 case while_stmt:
3168 com_while_stmt(c, n);
3169 break;
3170 case for_stmt:
3171 com_for_stmt(c, n);
3172 break;
3173 case try_stmt:
3174 com_try_stmt(c, n);
3175 break;
3176 case suite:
3177 com_suite(c, n);
3178 break;
3179
3180 /* Expression nodes */
3181
3182 case testlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00003183 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003184 break;
3185 case test:
3186 com_test(c, n);
3187 break;
3188 case and_test:
3189 com_and_test(c, n);
3190 break;
3191 case not_test:
3192 com_not_test(c, n);
3193 break;
3194 case comparison:
3195 com_comparison(c, n);
3196 break;
3197 case exprlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00003198 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003199 break;
3200 case expr:
3201 com_expr(c, n);
3202 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +00003203 case xor_expr:
3204 com_xor_expr(c, n);
3205 break;
3206 case and_expr:
3207 com_and_expr(c, n);
3208 break;
3209 case shift_expr:
3210 com_shift_expr(c, n);
3211 break;
3212 case arith_expr:
3213 com_arith_expr(c, n);
3214 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003215 case term:
3216 com_term(c, n);
3217 break;
3218 case factor:
3219 com_factor(c, n);
3220 break;
Guido van Rossum50564e81996-01-12 01:13:16 +00003221 case power:
3222 com_power(c, n);
3223 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003224 case atom:
3225 com_atom(c, n);
3226 break;
3227
3228 default:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003229 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
Guido van Rossum79f25d91997-04-29 20:08:16 +00003230 com_error(c, PyExc_SystemError,
3231 "com_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003232 }
3233}
3234
Tim Petersdbd9ba62000-07-09 03:09:57 +00003235static void com_fplist(struct compiling *, node *);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003236
3237static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003238com_fpdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003239{
3240 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
3241 if (TYPE(CHILD(n, 0)) == LPAR)
3242 com_fplist(c, CHILD(n, 1));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003243 else {
Guido van Rossum681d79a1995-07-18 14:51:37 +00003244 com_addoparg(c, STORE_FAST, com_newlocal(c, STR(CHILD(n, 0))));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003245 com_pop(c, 1);
3246 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003247}
3248
3249static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003250com_fplist(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003251{
Guido van Rossuma9df32a1991-12-31 13:13:35 +00003252 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003253 if (NCH(n) == 1) {
3254 com_fpdef(c, CHILD(n, 0));
3255 }
3256 else {
Guido van Rossum8b993a91997-01-17 21:04:03 +00003257 int i = (NCH(n)+1)/2;
Thomas Wouters0be5aab2000-08-11 22:15:52 +00003258 com_addoparg(c, UNPACK_SEQUENCE, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003259 com_push(c, i-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003260 for (i = 0; i < NCH(n); i += 2)
3261 com_fpdef(c, CHILD(n, i));
3262 }
3263}
3264
3265static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003266com_arglist(struct compiling *c, node *n)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003267{
Guido van Rossum681d79a1995-07-18 14:51:37 +00003268 int nch, i;
3269 int complex = 0;
Guido van Rossumdb9e20f1997-07-10 01:06:53 +00003270 char nbuf[10];
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003271 REQ(n, varargslist);
Guido van Rossumacbefef1992-01-19 16:33:51 +00003272 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00003273 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003274 nch = NCH(n);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003275 /* Enter all arguments in table of locals */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003276 for (i = 0; i < nch; i++) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00003277 node *ch = CHILD(n, i);
3278 node *fp;
3279 char *name;
Peter Schneider-Kamp11384c62000-07-13 06:15:04 +00003280 PyObject *nameval;
Guido van Rossum50564e81996-01-12 01:13:16 +00003281 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00003282 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003283 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3284 fp = CHILD(ch, 0);
3285 if (TYPE(fp) == NAME)
3286 name = STR(fp);
3287 else {
Guido van Rossumdb9e20f1997-07-10 01:06:53 +00003288 name = nbuf;
3289 sprintf(nbuf, ".%d", i);
Guido van Rossum530956d1996-07-21 02:27:43 +00003290 complex = 1;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003291 }
Peter Schneider-Kamp11384c62000-07-13 06:15:04 +00003292 nameval = PyString_InternFromString(name);
3293 if (nameval == NULL) {
3294 c->c_errors++;
3295 }
Peter Schneider-Kamp9a5086c2000-07-13 06:24:29 +00003296 if (PyDict_GetItem(c->c_locals, nameval)) {
Fred Drakedcf08e02000-08-15 15:49:44 +00003297 com_error(c, PyExc_SyntaxError,
3298 "duplicate argument in function definition");
Peter Schneider-Kamp11384c62000-07-13 06:15:04 +00003299 }
3300 com_newlocal_o(c, nameval);
3301 Py_DECREF(nameval);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003302 c->c_argcount++;
3303 if (++i >= nch)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003304 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003305 ch = CHILD(n, i);
3306 if (TYPE(ch) == EQUAL)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003307 i += 2;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003308 else
3309 REQ(ch, COMMA);
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003310 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003311 /* Handle *arguments */
3312 if (i < nch) {
3313 node *ch;
3314 ch = CHILD(n, i);
Guido van Rossum50564e81996-01-12 01:13:16 +00003315 if (TYPE(ch) != DOUBLESTAR) {
3316 REQ(ch, STAR);
3317 ch = CHILD(n, i+1);
3318 if (TYPE(ch) == NAME) {
3319 c->c_flags |= CO_VARARGS;
3320 i += 3;
3321 com_newlocal(c, STR(ch));
3322 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003323 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003324 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003325 /* Handle **keywords */
3326 if (i < nch) {
3327 node *ch;
3328 ch = CHILD(n, i);
Guido van Rossum50564e81996-01-12 01:13:16 +00003329 if (TYPE(ch) != DOUBLESTAR) {
3330 REQ(ch, STAR);
3331 ch = CHILD(n, i+1);
3332 REQ(ch, STAR);
3333 ch = CHILD(n, i+2);
3334 }
3335 else
3336 ch = CHILD(n, i+1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003337 REQ(ch, NAME);
3338 c->c_flags |= CO_VARKEYWORDS;
3339 com_newlocal(c, STR(ch));
3340 }
3341 if (complex) {
3342 /* Generate code for complex arguments only after
3343 having counted the simple arguments */
3344 int ilocal = 0;
3345 for (i = 0; i < nch; i++) {
3346 node *ch = CHILD(n, i);
3347 node *fp;
Guido van Rossum50564e81996-01-12 01:13:16 +00003348 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003349 break;
3350 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3351 fp = CHILD(ch, 0);
3352 if (TYPE(fp) != NAME) {
3353 com_addoparg(c, LOAD_FAST, ilocal);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003354 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003355 com_fpdef(c, ch);
3356 }
3357 ilocal++;
3358 if (++i >= nch)
3359 break;
3360 ch = CHILD(n, i);
3361 if (TYPE(ch) == EQUAL)
3362 i += 2;
3363 else
3364 REQ(ch, COMMA);
3365 }
3366 }
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003367}
3368
3369static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003370com_file_input(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003371{
3372 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003373 PyObject *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003374 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003375 doc = get_docstring(n);
3376 if (doc != NULL) {
3377 int i = com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003378 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003379 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003380 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003381 com_addopnamestr(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00003382 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003383 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003384 for (i = 0; i < NCH(n); i++) {
3385 node *ch = CHILD(n, i);
3386 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
3387 com_node(c, ch);
3388 }
3389}
3390
3391/* Top-level compile-node interface */
3392
3393static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003394compile_funcdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003395{
Guido van Rossum79f25d91997-04-29 20:08:16 +00003396 PyObject *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003397 node *ch;
3398 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum9bfef441993-03-29 10:43:31 +00003399 c->c_name = STR(CHILD(n, 1));
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003400 doc = get_docstring(CHILD(n, 4));
3401 if (doc != NULL) {
3402 (void) com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003403 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003404 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003405 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00003406 (void) com_addconst(c, Py_None); /* No docstring */
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003407 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
3408 ch = CHILD(ch, 1); /* ')' | varargslist */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003409 if (TYPE(ch) == varargslist)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003410 com_arglist(c, ch);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003411 c->c_infunction = 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003412 com_node(c, CHILD(n, 4));
Guido van Rossum3f5da241990-12-20 15:06:42 +00003413 c->c_infunction = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003414 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003415 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003416 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003417 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003418}
3419
3420static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003421compile_lambdef(struct compiling *c, node *n)
Guido van Rossum12d12c51993-10-26 17:58:25 +00003422{
Guido van Rossum590baa41993-11-30 13:40:46 +00003423 node *ch;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003424 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
Guido van Rossum363ac7d1994-11-10 22:40:34 +00003425 c->c_name = "<lambda>";
Guido van Rossum590baa41993-11-30 13:40:46 +00003426
3427 ch = CHILD(n, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003428 (void) com_addconst(c, Py_None); /* No docstring */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003429 if (TYPE(ch) == varargslist) {
Guido van Rossum590baa41993-11-30 13:40:46 +00003430 com_arglist(c, ch);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003431 ch = CHILD(n, 3);
Guido van Rossum590baa41993-11-30 13:40:46 +00003432 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003433 else
3434 ch = CHILD(n, 2);
3435 com_node(c, ch);
Guido van Rossum12d12c51993-10-26 17:58:25 +00003436 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003437 com_pop(c, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00003438}
3439
3440static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003441compile_classdef(struct compiling *c, node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003442{
3443 node *ch;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003444 PyObject *doc;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003445 REQ(n, classdef);
3446 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3447 c->c_name = STR(CHILD(n, 1));
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003448#ifdef PRIVATE_NAME_MANGLING
3449 c->c_private = c->c_name;
3450#endif
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003451 ch = CHILD(n, NCH(n)-1); /* The suite */
3452 doc = get_docstring(ch);
3453 if (doc != NULL) {
3454 int i = com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003455 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003456 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003457 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003458 com_addopnamestr(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00003459 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003460 }
3461 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00003462 (void) com_addconst(c, Py_None);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003463 com_node(c, ch);
3464 com_addbyte(c, LOAD_LOCALS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003465 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003466 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003467 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003468}
3469
3470static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003471compile_node(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003472{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003473 com_addoparg(c, SET_LINENO, n->n_lineno);
3474
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003475 switch (TYPE(n)) {
3476
Guido van Rossum4c417781991-01-21 16:09:22 +00003477 case single_input: /* One interactive command */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003478 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003479 c->c_interactive++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003480 n = CHILD(n, 0);
3481 if (TYPE(n) != NEWLINE)
3482 com_node(c, n);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003483 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003484 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003485 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003486 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003487 c->c_interactive--;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003488 break;
3489
Guido van Rossum4c417781991-01-21 16:09:22 +00003490 case file_input: /* A whole file, or built-in function exec() */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003491 com_file_input(c, n);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003492 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003493 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003494 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003495 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003496 break;
3497
Guido van Rossum590baa41993-11-30 13:40:46 +00003498 case eval_input: /* Built-in function input() */
Guido van Rossum4c417781991-01-21 16:09:22 +00003499 com_node(c, CHILD(n, 0));
3500 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003501 com_pop(c, 1);
Guido van Rossum4c417781991-01-21 16:09:22 +00003502 break;
3503
Guido van Rossum590baa41993-11-30 13:40:46 +00003504 case lambdef: /* anonymous function definition */
3505 compile_lambdef(c, n);
3506 break;
3507
Guido van Rossum4c417781991-01-21 16:09:22 +00003508 case funcdef: /* A function definition */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003509 compile_funcdef(c, n);
3510 break;
3511
Guido van Rossum4c417781991-01-21 16:09:22 +00003512 case classdef: /* A class definition */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003513 compile_classdef(c, n);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003514 break;
3515
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003516 default:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003517 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
Guido van Rossum79f25d91997-04-29 20:08:16 +00003518 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00003519 "compile_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003520 }
3521}
3522
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003523/* Optimization for local variables in functions (and *only* functions).
Guido van Rossum282914b1991-04-04 10:42:56 +00003524
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003525 This replaces all LOAD_NAME, STORE_NAME and DELETE_NAME
3526 instructions that refer to local variables with LOAD_FAST etc.
3527 The latter instructions are much faster because they don't need to
3528 look up the variable name in a dictionary.
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003529
Guido van Rossum681d79a1995-07-18 14:51:37 +00003530 To find all local variables, we check all STORE_NAME, IMPORT_FROM
3531 and DELETE_NAME instructions. This yields all local variables,
3532 function definitions, class definitions and import statements.
3533 Argument names have already been entered into the list by the
3534 special processing for the argument list.
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003535
3536 All remaining LOAD_NAME instructions must refer to non-local (global
3537 or builtin) variables, so are replaced by LOAD_GLOBAL.
3538
3539 There are two problems: 'from foo import *' and 'exec' may introduce
3540 local variables that we can't know while compiling. If this is the
Guido van Rossum681d79a1995-07-18 14:51:37 +00003541 case, we can still optimize bona fide locals (since those
3542 statements will be surrounded by fast_2_locals() and
3543 locals_2_fast()), but we can't change LOAD_NAME to LOAD_GLOBAL.
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003544
Guido van Rossum681d79a1995-07-18 14:51:37 +00003545 NB: this modifies the string object c->c_code! */
Guido van Rossum282914b1991-04-04 10:42:56 +00003546
3547static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003548optimize(struct compiling *c)
Guido van Rossum282914b1991-04-04 10:42:56 +00003549{
Guido van Rossum0a697f61991-04-16 08:39:12 +00003550 unsigned char *next_instr, *cur_instr;
Guido van Rossum282914b1991-04-04 10:42:56 +00003551 int opcode;
Guido van Rossum80bb9651996-12-05 23:27:02 +00003552 int oparg = 0;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003553 PyObject *name;
3554 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003555
Guido van Rossum282914b1991-04-04 10:42:56 +00003556#define NEXTOP() (*next_instr++)
3557#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Guido van Rossum79f25d91997-04-29 20:08:16 +00003558#define GETITEM(v, i) (PyList_GetItem((v), (i)))
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003559#define GETNAMEOBJ(i) (GETITEM(c->c_names, (i)))
3560
Guido van Rossum79f25d91997-04-29 20:08:16 +00003561 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003562
3563 c->c_flags |= CO_OPTIMIZED;
Guido van Rossum282914b1991-04-04 10:42:56 +00003564
Guido van Rossum79f25d91997-04-29 20:08:16 +00003565 next_instr = (unsigned char *) PyString_AsString(c->c_code);
Guido van Rossum282914b1991-04-04 10:42:56 +00003566 for (;;) {
3567 opcode = NEXTOP();
3568 if (opcode == STOP_CODE)
3569 break;
3570 if (HAS_ARG(opcode))
3571 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00003572 dispatch_opcode1:
Guido van Rossum681d79a1995-07-18 14:51:37 +00003573 switch (opcode) {
3574 case STORE_NAME:
3575 case DELETE_NAME:
3576 case IMPORT_FROM:
3577 com_addlocal_o(c, GETNAMEOBJ(oparg));
3578 break;
Thomas Wouters52152252000-08-17 22:55:00 +00003579 case IMPORT_STAR:
Guido van Rossum681d79a1995-07-18 14:51:37 +00003580 case EXEC_STMT:
3581 c->c_flags &= ~CO_OPTIMIZED;
3582 break;
Fred Drakeef8ace32000-08-24 00:32:09 +00003583 case EXTENDED_ARG:
3584 opcode = NEXTOP();
3585 oparg = oparg<<16 | NEXTARG();
3586 goto dispatch_opcode1;
3587 break;
Guido van Rossum282914b1991-04-04 10:42:56 +00003588 }
3589 }
3590
Thomas Wouters52152252000-08-17 22:55:00 +00003591 /* TBD: Is this still necessary ? */
Guido van Rossum79f25d91997-04-29 20:08:16 +00003592 if (PyDict_GetItemString(c->c_locals, "*") != NULL)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003593 c->c_flags &= ~CO_OPTIMIZED;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003594
Guido van Rossum79f25d91997-04-29 20:08:16 +00003595 next_instr = (unsigned char *) PyString_AsString(c->c_code);
Guido van Rossum282914b1991-04-04 10:42:56 +00003596 for (;;) {
3597 cur_instr = next_instr;
3598 opcode = NEXTOP();
3599 if (opcode == STOP_CODE)
3600 break;
3601 if (HAS_ARG(opcode))
3602 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00003603 dispatch_opcode2:
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003604 if (opcode == LOAD_NAME ||
3605 opcode == STORE_NAME ||
3606 opcode == DELETE_NAME) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003607 PyObject *v;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003608 int i;
Guido van Rossum282914b1991-04-04 10:42:56 +00003609 name = GETNAMEOBJ(oparg);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003610 v = PyDict_GetItem(c->c_locals, name);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003611 if (v == NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00003612 if (opcode == LOAD_NAME &&
3613 (c->c_flags&CO_OPTIMIZED))
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003614 cur_instr[0] = LOAD_GLOBAL;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003615 continue;
Guido van Rossum83163251991-08-16 08:58:43 +00003616 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00003617 i = PyInt_AsLong(v);
Fred Drakeef8ace32000-08-24 00:32:09 +00003618 if (i >> 16) /* too big for 2 bytes */
3619 continue;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003620 switch (opcode) {
3621 case LOAD_NAME: cur_instr[0] = LOAD_FAST; break;
3622 case STORE_NAME: cur_instr[0] = STORE_FAST; break;
3623 case DELETE_NAME: cur_instr[0] = DELETE_FAST; break;
3624 }
3625 cur_instr[1] = i & 0xff;
Fred Drakeef8ace32000-08-24 00:32:09 +00003626 cur_instr[2] = i >> 8;
3627 }
3628 if (opcode == EXTENDED_ARG) {
3629 opcode = NEXTOP();
3630 oparg = oparg<<16 | NEXTARG();
3631 goto dispatch_opcode2;
Guido van Rossum282914b1991-04-04 10:42:56 +00003632 }
3633 }
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003634
Guido van Rossum681d79a1995-07-18 14:51:37 +00003635 if (c->c_errors == 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +00003636 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum282914b1991-04-04 10:42:56 +00003637}
3638
Guido van Rossum79f25d91997-04-29 20:08:16 +00003639PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003640PyNode_Compile(node *n, char *filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003641{
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003642 return jcompile(n, filename, NULL);
3643}
3644
Guido van Rossum79f25d91997-04-29 20:08:16 +00003645static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003646icompile(node *n, struct compiling *base)
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003647{
3648 return jcompile(n, base->c_filename, base);
3649}
3650
Guido van Rossum79f25d91997-04-29 20:08:16 +00003651static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003652jcompile(node *n, char *filename, struct compiling *base)
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003653{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003654 struct compiling sc;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003655 PyCodeObject *co;
Guido van Rossum590baa41993-11-30 13:40:46 +00003656 if (!com_init(&sc, filename))
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003657 return NULL;
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003658#ifdef PRIVATE_NAME_MANGLING
3659 if (base)
3660 sc.c_private = base->c_private;
3661 else
3662 sc.c_private = NULL;
3663#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003664 compile_node(&sc, n);
3665 com_done(&sc);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003666 if ((TYPE(n) == funcdef || TYPE(n) == lambdef) && sc.c_errors == 0) {
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003667 optimize(&sc);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003668 sc.c_flags |= CO_NEWLOCALS;
3669 }
3670 else if (TYPE(n) == classdef)
3671 sc.c_flags |= CO_NEWLOCALS;
Guido van Rossum9bfef441993-03-29 10:43:31 +00003672 co = NULL;
3673 if (sc.c_errors == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003674 PyObject *consts, *names, *varnames, *filename, *name;
3675 consts = PyList_AsTuple(sc.c_consts);
3676 names = PyList_AsTuple(sc.c_names);
3677 varnames = PyList_AsTuple(sc.c_varnames);
Guido van Rossum7b89b6a1997-01-18 08:02:57 +00003678 filename = PyString_InternFromString(sc.c_filename);
3679 name = PyString_InternFromString(sc.c_name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003680 if (!PyErr_Occurred())
3681 co = PyCode_New(sc.c_argcount,
Guido van Rossum681d79a1995-07-18 14:51:37 +00003682 sc.c_nlocals,
Guido van Rossum8b993a91997-01-17 21:04:03 +00003683 sc.c_maxstacklevel,
Guido van Rossum681d79a1995-07-18 14:51:37 +00003684 sc.c_flags,
3685 sc.c_code,
3686 consts,
3687 names,
3688 varnames,
3689 filename,
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00003690 name,
3691 sc.c_firstlineno,
3692 sc.c_lnotab);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003693 Py_XDECREF(consts);
3694 Py_XDECREF(names);
3695 Py_XDECREF(varnames);
3696 Py_XDECREF(filename);
3697 Py_XDECREF(name);
Guido van Rossuma082ce41991-06-04 19:41:56 +00003698 }
Guido van Rossum6c2f0c72000-08-07 19:22:43 +00003699 else if (!PyErr_Occurred()) {
3700 /* This could happen if someone called PyErr_Clear() after an
3701 error was reported above. That's not supposed to happen,
3702 but I just plugged one case and I'm not sure there can't be
3703 others. In that case, raise SystemError so that at least
3704 it gets reported instead dumping core. */
3705 PyErr_SetString(PyExc_SystemError, "lost syntax error");
3706 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003707 com_free(&sc);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003708 return co;
3709}
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00003710
3711int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003712PyCode_Addr2Line(PyCodeObject *co, int addrq)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00003713{
3714 int size = PyString_Size(co->co_lnotab) / 2;
Guido van Rossum2174dcb1999-09-15 22:48:09 +00003715 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00003716 int line = co->co_firstlineno;
3717 int addr = 0;
3718 while (--size >= 0) {
3719 addr += *p++;
3720 if (addr > addrq)
3721 break;
3722 line += *p++;
3723 }
3724 return line;
3725}