blob: 556275140060589a9e86792030e35eaa84b0ec62 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum10dc2e81990-11-18 17:27:39 +000032/* Compile an expression node to intermediate code */
33
Guido van Rossum3f5da241990-12-20 15:06:42 +000034/* XXX TO DO:
Guido van Rossum8b993a91997-01-17 21:04:03 +000035 XXX add __doc__ attribute == co_doc to code object attributes?
36 XXX (it's currently the first item of the co_const tuple)
Guido van Rossum3f5da241990-12-20 15:06:42 +000037 XXX Generate simple jump for break/return outside 'try...finally'
Guido van Rossum8b993a91997-01-17 21:04:03 +000038 XXX Allow 'continue' inside try-finally
Guido van Rossum681d79a1995-07-18 14:51:37 +000039 XXX get rid of SET_LINENO instructions, use JAR's table trick
40 XXX (need an option to put them back in, for debugger!)
Guido van Rossum8b993a91997-01-17 21:04:03 +000041 XXX New 1-byte opcode for loading None
42 XXX New opcode for loading the initial index for a for loop
Guido van Rossum681d79a1995-07-18 14:51:37 +000043 XXX other JAR tricks?
Guido van Rossum3f5da241990-12-20 15:06:42 +000044*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +000045
Guido van Rossum8ff077b1996-08-24 06:21:31 +000046#ifndef NO_PRIVATE_NAME_MANGLING
47#define PRIVATE_NAME_MANGLING
48#endif
49
Guido van Rossum3f5da241990-12-20 15:06:42 +000050#include "allobjects.h"
51
Guido van Rossum10dc2e81990-11-18 17:27:39 +000052#include "node.h"
53#include "token.h"
54#include "graminit.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000055#include "compile.h"
56#include "opcode.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000057#include "structmember.h"
58
59#include <ctype.h>
Guido van Rossumf1dc5661993-07-05 10:31:29 +000060#include <errno.h>
Guido van Rossum282914b1991-04-04 10:42:56 +000061
Guido van Rossum8861b741996-07-30 16:49:37 +000062#define OP_DELETE 0
63#define OP_ASSIGN 1
64#define OP_APPLY 2
65
Guido van Rossum3f5da241990-12-20 15:06:42 +000066#define OFF(x) offsetof(codeobject, x)
67
68static struct memberlist code_memberlist[] = {
Guido van Rossum681d79a1995-07-18 14:51:37 +000069 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
70 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
Guido van Rossum8b993a91997-01-17 21:04:03 +000071 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
Guido van Rossum681d79a1995-07-18 14:51:37 +000072 {"co_flags", T_INT, OFF(co_flags), READONLY},
Guido van Rossum39d942d1992-01-12 02:30:05 +000073 {"co_code", T_OBJECT, OFF(co_code), READONLY},
74 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
75 {"co_names", T_OBJECT, OFF(co_names), READONLY},
Guido van Rossum681d79a1995-07-18 14:51:37 +000076 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
Guido van Rossum39d942d1992-01-12 02:30:05 +000077 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
Guido van Rossum9bfef441993-03-29 10:43:31 +000078 {"co_name", T_OBJECT, OFF(co_name), READONLY},
Guido van Rossum3f5da241990-12-20 15:06:42 +000079 {NULL} /* Sentinel */
80};
81
82static object *
83code_getattr(co, name)
84 codeobject *co;
85 char *name;
86{
87 return getmember((char *)co, code_memberlist, name);
88}
Guido van Rossum10dc2e81990-11-18 17:27:39 +000089
90static void
Guido van Rossum3f5da241990-12-20 15:06:42 +000091code_dealloc(co)
92 codeobject *co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +000093{
Guido van Rossum3f5da241990-12-20 15:06:42 +000094 XDECREF(co->co_code);
95 XDECREF(co->co_consts);
96 XDECREF(co->co_names);
97 XDECREF(co->co_filename);
Guido van Rossum9bfef441993-03-29 10:43:31 +000098 XDECREF(co->co_name);
Guido van Rossum681d79a1995-07-18 14:51:37 +000099 XDECREF(co->co_varnames);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000100 DEL(co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000101}
102
Guido van Rossum2dff9911992-09-03 20:50:59 +0000103static object *
104code_repr(co)
105 codeobject *co;
106{
107 char buf[500];
108 int lineno = -1;
109 char *p = GETSTRINGVALUE(co->co_code);
110 char *filename = "???";
Guido van Rossum9bfef441993-03-29 10:43:31 +0000111 char *name = "???";
Guido van Rossum2dff9911992-09-03 20:50:59 +0000112 if (*p == SET_LINENO)
113 lineno = (p[1] & 0xff) | ((p[2] & 0xff) << 8);
114 if (co->co_filename && is_stringobject(co->co_filename))
115 filename = getstringvalue(co->co_filename);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000116 if (co->co_name && is_stringobject(co->co_name))
117 name = getstringvalue(co->co_name);
118 sprintf(buf, "<code object %.100s at %lx, file \"%.300s\", line %d>",
119 name, (long)co, filename, lineno);
Guido van Rossum2dff9911992-09-03 20:50:59 +0000120 return newstringobject(buf);
121}
122
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000123static int
124code_compare(co, cp)
125 codeobject *co, *cp;
126{
127 int cmp;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000128 cmp = cp->co_argcount - cp->co_argcount;
129 if (cmp) return cmp;
130 cmp = cp->co_nlocals - cp->co_nlocals;
131 if (cmp) return cmp;
132 cmp = cp->co_flags - cp->co_flags;
133 if (cmp) return cmp;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000134 cmp = cmpobject((object *)co->co_code, (object *)cp->co_code);
135 if (cmp) return cmp;
136 cmp = cmpobject(co->co_consts, cp->co_consts);
137 if (cmp) return cmp;
138 cmp = cmpobject(co->co_names, cp->co_names);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000139 if (cmp) return cmp;
140 cmp = cmpobject(co->co_varnames, cp->co_varnames);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000141 return cmp;
142}
143
144static long
145code_hash(co)
146 codeobject *co;
147{
Guido van Rossum681d79a1995-07-18 14:51:37 +0000148 long h, h1, h2, h3, h4;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000149 h1 = hashobject((object *)co->co_code);
150 if (h1 == -1) return -1;
151 h2 = hashobject(co->co_consts);
152 if (h2 == -1) return -1;
153 h3 = hashobject(co->co_names);
154 if (h3 == -1) return -1;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000155 h4 = hashobject(co->co_varnames);
156 if (h4 == -1) return -1;
157 h = h1 ^ h2 ^ h3 ^ h4 ^
158 co->co_argcount ^ co->co_nlocals ^ co->co_flags;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000159 if (h == -1) h = -2;
160 return h;
161}
162
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000163typeobject Codetype = {
164 OB_HEAD_INIT(&Typetype)
165 0,
166 "code",
167 sizeof(codeobject),
168 0,
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000169 (destructor)code_dealloc, /*tp_dealloc*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000170 0, /*tp_print*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000171 (getattrfunc)code_getattr, /*tp_getattr*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000172 0, /*tp_setattr*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000173 (cmpfunc)code_compare, /*tp_compare*/
174 (reprfunc)code_repr, /*tp_repr*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000175 0, /*tp_as_number*/
176 0, /*tp_as_sequence*/
177 0, /*tp_as_mapping*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000178 (hashfunc)code_hash, /*tp_hash*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000179};
180
Guido van Rossuma082ce41991-06-04 19:41:56 +0000181codeobject *
Guido van Rossum8b993a91997-01-17 21:04:03 +0000182newcodeobject(argcount, nlocals, stacksize, flags,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000183 code, consts, names, varnames, filename, name)
184 int argcount;
185 int nlocals;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000186 int stacksize;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000187 int flags;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000188 object *code;
189 object *consts;
190 object *names;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000191 object *varnames;
Guido van Rossuma082ce41991-06-04 19:41:56 +0000192 object *filename;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000193 object *name;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000194{
195 codeobject *co;
196 int i;
197 /* Check argument types */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000198 if (argcount < 0 || nlocals < 0 ||
199 code == NULL || !is_stringobject(code) ||
200 consts == NULL || !is_tupleobject(consts) ||
201 names == NULL || !is_tupleobject(names) ||
202 varnames == NULL || !is_tupleobject(varnames) ||
203 name == NULL || !is_stringobject(name) ||
204 filename == NULL || !is_stringobject(filename)) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000205 err_badcall();
206 return NULL;
207 }
Guido van Rossum681d79a1995-07-18 14:51:37 +0000208 /* Make sure names and varnames are all strings */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000209 for (i = gettuplesize(names); --i >= 0; ) {
210 object *v = gettupleitem(names, i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000211 if (v == NULL || !is_stringobject(v)) {
Guido van Rossum681d79a1995-07-18 14:51:37 +0000212 err_badcall();
213 return NULL;
214 }
215 }
216 for (i = gettuplesize(varnames); --i >= 0; ) {
217 object *v = gettupleitem(varnames, i);
218 if (v == NULL || !is_stringobject(v)) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000219 err_badcall();
220 return NULL;
221 }
222 }
223 co = NEWOBJ(codeobject, &Codetype);
224 if (co != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +0000225 co->co_argcount = argcount;
226 co->co_nlocals = nlocals;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000227 co->co_stacksize = stacksize;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000228 co->co_flags = flags;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000229 INCREF(code);
230 co->co_code = (stringobject *)code;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000231 INCREF(consts);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000232 co->co_consts = consts;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000233 INCREF(names);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000234 co->co_names = names;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000235 INCREF(varnames);
236 co->co_varnames = varnames;
Guido van Rossuma082ce41991-06-04 19:41:56 +0000237 INCREF(filename);
238 co->co_filename = filename;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000239 INCREF(name);
240 co->co_name = name;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000241 }
242 return co;
243}
244
245
246/* Data structure used internally */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000247
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000248struct compiling {
249 object *c_code; /* string */
250 object *c_consts; /* list of objects */
251 object *c_names; /* list of strings (names) */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000252 object *c_globals; /* dictionary (value=None) */
253 object *c_locals; /* dictionary (value=localID) */
254 object *c_varnames; /* list (inverse of c_locals) */
255 int c_nlocals; /* index of next local */
256 int c_argcount; /* number of top-level arguments */
257 int c_flags; /* same as co_flags */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000258 int c_nexti; /* index into c_code */
259 int c_errors; /* counts errors occurred */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000260 int c_infunction; /* set when compiling a function */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000261 int c_interactive; /* generating code for interactive command */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000262 int c_loops; /* counts nested loops */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000263 int c_begin; /* begin of current loop, for 'continue' */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000264 int c_block[CO_MAXBLOCKS]; /* stack of block types */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000265 int c_nblocks; /* current block stack level */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000266 char *c_filename; /* filename of current node */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000267 char *c_name; /* name of object (e.g. function) */
Guido van Rossum452a9831996-09-17 14:32:04 +0000268 int c_lineno; /* Current line number */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000269 int c_stacklevel; /* Current stack level */
270 int c_maxstacklevel; /* Maximum stack level */
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000271#ifdef PRIVATE_NAME_MANGLING
272 char *c_private; /* for private name mangling */
273#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000274};
275
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000276
Guido van Rossum452a9831996-09-17 14:32:04 +0000277/* Error message including line number */
278
279static void
280com_error(c, exc, msg)
281 struct compiling *c;
282 object *exc;
283 char *msg;
284{
285 int n = strlen(msg);
286 object *v;
287 char buffer[30];
288 char *s;
Guido van Rossum635abd21997-01-06 22:56:52 +0000289 c->c_errors++;
Guido van Rossum452a9831996-09-17 14:32:04 +0000290 if (c->c_lineno <= 1) {
291 /* Unknown line number or single interactive command */
292 err_setstr(exc, msg);
293 return;
294 }
295 sprintf(buffer, " (line %d)", c->c_lineno);
296 v = newsizedstringobject((char *)NULL, n + strlen(buffer));
297 if (v == NULL)
298 return; /* MemoryError, too bad */
299 s = GETSTRINGVALUE((stringobject *)v);
300 strcpy(s, msg);
301 strcat(s, buffer);
302 err_setval(exc, v);
303 DECREF(v);
Guido van Rossum452a9831996-09-17 14:32:04 +0000304}
305
306
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000307/* Interface to the block stack */
308
309static void
310block_push(c, type)
311 struct compiling *c;
312 int type;
313{
Guido van Rossum8b993a91997-01-17 21:04:03 +0000314 if (c->c_nblocks >= CO_MAXBLOCKS) {
Guido van Rossum452a9831996-09-17 14:32:04 +0000315 com_error(c, SystemError, "too many statically nested blocks");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000316 }
317 else {
318 c->c_block[c->c_nblocks++] = type;
319 }
320}
321
322static void
323block_pop(c, type)
324 struct compiling *c;
325 int type;
326{
327 if (c->c_nblocks > 0)
328 c->c_nblocks--;
329 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
Guido van Rossum452a9831996-09-17 14:32:04 +0000330 com_error(c, SystemError, "bad block pop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000331 }
332}
333
334
Guido van Rossum681d79a1995-07-18 14:51:37 +0000335/* Prototype forward declarations */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000336
Guido van Rossum590baa41993-11-30 13:40:46 +0000337static int com_init PROTO((struct compiling *, char *));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000338static void com_free PROTO((struct compiling *));
Guido van Rossum8b993a91997-01-17 21:04:03 +0000339static void com_push PROTO((struct compiling *, int));
340static void com_pop PROTO((struct compiling *, int));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000341static void com_done PROTO((struct compiling *));
342static void com_node PROTO((struct compiling *, struct _node *));
Guido van Rossum50564e81996-01-12 01:13:16 +0000343static void com_factor PROTO((struct compiling *, struct _node *));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000344static void com_addbyte PROTO((struct compiling *, int));
345static void com_addint PROTO((struct compiling *, int));
346static void com_addoparg PROTO((struct compiling *, int, int));
347static void com_addfwref PROTO((struct compiling *, int, int *));
348static void com_backpatch PROTO((struct compiling *, int));
349static int com_add PROTO((struct compiling *, object *, object *));
350static int com_addconst PROTO((struct compiling *, object *));
351static int com_addname PROTO((struct compiling *, object *));
352static void com_addopname PROTO((struct compiling *, int, node *));
Guido van Rossum288a60f1991-12-16 13:05:10 +0000353static void com_list PROTO((struct compiling *, node *, int));
Guido van Rossum681d79a1995-07-18 14:51:37 +0000354static int com_argdefs PROTO((struct compiling *, node *));
355static int com_newlocal PROTO((struct compiling *, char *));
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000356static codeobject *icompile PROTO((struct _node *, struct compiling *));
357static codeobject *jcompile PROTO((struct _node *, char *, struct compiling *));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000358
359static int
Guido van Rossum590baa41993-11-30 13:40:46 +0000360com_init(c, filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000361 struct compiling *c;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000362 char *filename;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000363{
Guido van Rossum62d46241991-04-03 19:00:23 +0000364 if ((c->c_code = newsizedstringobject((char *)NULL, 1000)) == NULL)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000365 goto fail_3;
366 if ((c->c_consts = newlistobject(0)) == NULL)
367 goto fail_2;
368 if ((c->c_names = newlistobject(0)) == NULL)
369 goto fail_1;
Guido van Rossumc5e96291991-12-10 13:53:51 +0000370 if ((c->c_globals = newdictobject()) == NULL)
371 goto fail_0;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000372 if ((c->c_locals = newdictobject()) == NULL)
373 goto fail_00;
374 if ((c->c_varnames = newlistobject(0)) == NULL)
375 goto fail_000;
376 c->c_nlocals = 0;
377 c->c_argcount = 0;
378 c->c_flags = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000379 c->c_nexti = 0;
380 c->c_errors = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000381 c->c_infunction = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000382 c->c_interactive = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000383 c->c_loops = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000384 c->c_begin = 0;
385 c->c_nblocks = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000386 c->c_filename = filename;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000387 c->c_name = "?";
Guido van Rossum452a9831996-09-17 14:32:04 +0000388 c->c_lineno = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000389 c->c_stacklevel = 0;
390 c->c_maxstacklevel = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000391 return 1;
392
Guido van Rossum681d79a1995-07-18 14:51:37 +0000393 fail_000:
394 DECREF(c->c_locals);
395 fail_00:
396 DECREF(c->c_globals);
Guido van Rossumc5e96291991-12-10 13:53:51 +0000397 fail_0:
398 DECREF(c->c_names);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000399 fail_1:
400 DECREF(c->c_consts);
401 fail_2:
402 DECREF(c->c_code);
403 fail_3:
404 return 0;
405}
406
407static void
408com_free(c)
409 struct compiling *c;
410{
411 XDECREF(c->c_code);
412 XDECREF(c->c_consts);
413 XDECREF(c->c_names);
Guido van Rossumc5e96291991-12-10 13:53:51 +0000414 XDECREF(c->c_globals);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000415 XDECREF(c->c_locals);
416 XDECREF(c->c_varnames);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000417}
418
419static void
Guido van Rossum8b993a91997-01-17 21:04:03 +0000420com_push(c, n)
421 struct compiling *c;
422 int n;
423{
424 c->c_stacklevel += n;
425 if (c->c_stacklevel > c->c_maxstacklevel)
426 c->c_maxstacklevel = c->c_stacklevel;
427}
428
429static void
430com_pop(c, n)
431 struct compiling *c;
432 int n;
433{
434 if (c->c_stacklevel < n) {
435 fprintf(stderr,
436 "%s:%d: underflow! nexti=%d, level=%d, n=%d\n",
437 c->c_filename, c->c_lineno,
438 c->c_nexti, c->c_stacklevel, n);
439 c->c_stacklevel = 0;
440 }
441 else
442 c->c_stacklevel -= n;
443}
444
445static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000446com_done(c)
447 struct compiling *c;
448{
449 if (c->c_code != NULL)
450 resizestring(&c->c_code, c->c_nexti);
451}
452
453static void
454com_addbyte(c, byte)
455 struct compiling *c;
456 int byte;
457{
458 int len;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000459 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000460 if (byte < 0 || byte > 255) {
Guido van Rossum01cfd441991-10-20 20:12:38 +0000461 /*
Guido van Rossum3f5da241990-12-20 15:06:42 +0000462 fprintf(stderr, "XXX compiling bad byte: %d\n", byte);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000463 fatal("com_addbyte: byte out of range");
Guido van Rossum01cfd441991-10-20 20:12:38 +0000464 */
Guido van Rossum452a9831996-09-17 14:32:04 +0000465 com_error(c, SystemError, "com_addbyte: byte out of range");
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000466 }
467 if (c->c_code == NULL)
468 return;
469 len = getstringsize(c->c_code);
470 if (c->c_nexti >= len) {
471 if (resizestring(&c->c_code, len+1000) != 0) {
472 c->c_errors++;
473 return;
474 }
475 }
476 getstringvalue(c->c_code)[c->c_nexti++] = byte;
477}
478
479static void
480com_addint(c, x)
481 struct compiling *c;
482 int x;
483{
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000484 com_addbyte(c, x & 0xff);
485 com_addbyte(c, x >> 8); /* XXX x should be positive */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000486}
487
488static void
489com_addoparg(c, op, arg)
490 struct compiling *c;
491 int op;
492 int arg;
493{
Guido van Rossum452a9831996-09-17 14:32:04 +0000494 if (op == SET_LINENO)
495 c->c_lineno = arg;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000496 com_addbyte(c, op);
497 com_addint(c, arg);
498}
499
500static void
501com_addfwref(c, op, p_anchor)
502 struct compiling *c;
503 int op;
504 int *p_anchor;
505{
506 /* Compile a forward reference for backpatching */
507 int here;
508 int anchor;
509 com_addbyte(c, op);
510 here = c->c_nexti;
511 anchor = *p_anchor;
512 *p_anchor = here;
513 com_addint(c, anchor == 0 ? 0 : here - anchor);
514}
515
516static void
517com_backpatch(c, anchor)
518 struct compiling *c;
519 int anchor; /* Must be nonzero */
520{
521 unsigned char *code = (unsigned char *) getstringvalue(c->c_code);
522 int target = c->c_nexti;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000523 int dist;
524 int prev;
525 for (;;) {
526 /* Make the JUMP instruction at anchor point to target */
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000527 prev = code[anchor] + (code[anchor+1] << 8);
528 dist = target - (anchor+2);
529 code[anchor] = dist & 0xff;
530 code[anchor+1] = dist >> 8;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000531 if (!prev)
532 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000533 anchor -= prev;
534 }
535}
536
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000537/* Handle literals and names uniformly */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000538
539static int
540com_add(c, list, v)
541 struct compiling *c;
542 object *list;
543 object *v;
544{
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000545 int n = getlistsize(list);
546 int i;
547 for (i = n; --i >= 0; ) {
548 object *w = getlistitem(list, i);
Guido van Rossumefc0bd01991-07-01 18:44:20 +0000549 if (v->ob_type == w->ob_type && cmpobject(v, w) == 0)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000550 return i;
551 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000552 if (addlistitem(list, v) != 0)
553 c->c_errors++;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000554 return n;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000555}
556
557static int
558com_addconst(c, v)
559 struct compiling *c;
560 object *v;
561{
562 return com_add(c, c->c_consts, v);
563}
564
565static int
566com_addname(c, v)
567 struct compiling *c;
568 object *v;
569{
570 return com_add(c, c->c_names, v);
571}
572
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000573#ifdef PRIVATE_NAME_MANGLING
574static int
575com_mangle(c, name, buffer, maxlen)
576 struct compiling *c;
577 char *name;
578 char *buffer;
579 int maxlen;
580{
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000581 /* Name mangling: __private becomes _classname__private.
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000582 This is independent from how the name is used. */
583 char *p;
584 int nlen, plen;
585 nlen = strlen(name);
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000586 if (nlen+2 >= maxlen)
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000587 return 0; /* Don't mangle __extremely_long_names */
588 if (name[nlen-1] == '_' && name[nlen-2] == '_')
589 return 0; /* Don't mangle __whatever__ */
590 p = c->c_private;
591 /* Strip leading underscores from class name */
592 while (*p == '_')
593 p++;
594 if (*p == '\0')
595 return 0; /* Don't mangle if class is just underscores */
596 plen = strlen(p);
597 if (plen + nlen >= maxlen)
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000598 plen = maxlen-nlen-2; /* Truncate class name if too long */
599 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000600 buffer[0] = '_';
601 strncpy(buffer+1, p, plen);
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000602 strcpy(buffer+1+plen, name);
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000603 /* fprintf(stderr, "mangle %s -> %s\n", name, buffer); */
604 return 1;
605}
606#endif
607
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000608static void
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000609com_addopnamestr(c, op, name)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000610 struct compiling *c;
611 int op;
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000612 char *name;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000613{
614 object *v;
615 int i;
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000616#ifdef PRIVATE_NAME_MANGLING
617 char buffer[256];
618 if (name != NULL && name[0] == '_' && name[1] == '_' &&
619 c->c_private != NULL &&
620 com_mangle(c, name, buffer, (int)sizeof(buffer)))
621 name = buffer;
622#endif
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000623 if (name == NULL || (v = newstringobject(name)) == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000624 c->c_errors++;
625 i = 255;
626 }
627 else {
628 i = com_addname(c, v);
629 DECREF(v);
630 }
Guido van Rossumc5e96291991-12-10 13:53:51 +0000631 /* Hack to replace *_NAME opcodes by *_GLOBAL if necessary */
632 switch (op) {
633 case LOAD_NAME:
634 case STORE_NAME:
635 case DELETE_NAME:
636 if (dictlookup(c->c_globals, name) != NULL) {
637 switch (op) {
638 case LOAD_NAME: op = LOAD_GLOBAL; break;
639 case STORE_NAME: op = STORE_GLOBAL; break;
640 case DELETE_NAME: op = DELETE_GLOBAL; break;
641 }
642 }
643 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000644 com_addoparg(c, op, i);
645}
646
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000647static void
648com_addopname(c, op, n)
649 struct compiling *c;
650 int op;
651 node *n;
652{
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000653 char *name;
654 char buffer[1000];
655 /* XXX it is possible to write this code without the 1000
656 chars on the total length of dotted names, I just can't be
657 bothered right now */
658 if (TYPE(n) == STAR)
659 name = "*";
660 else if (TYPE(n) == dotted_name) {
661 char *p = buffer;
662 int i;
663 name = buffer;
664 for (i = 0; i < NCH(n); i += 2) {
665 char *s = STR(CHILD(n, i));
666 if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
Guido van Rossum452a9831996-09-17 14:32:04 +0000667 com_error(c, MemoryError,
668 "dotted_name too long");
Guido van Rossumd9dfaf51995-02-17 15:04:57 +0000669 name = NULL;
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000670 break;
671 }
672 if (p != buffer)
673 *p++ = '.';
674 strcpy(p, s);
675 p = strchr(p, '\0');
676 }
677 }
678 else {
679 REQ(n, NAME);
680 name = STR(n);
681 }
682 com_addopnamestr(c, op, name);
683}
684
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000685static object *
Guido van Rossum452a9831996-09-17 14:32:04 +0000686parsenumber(co, s)
687 struct compiling *co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000688 char *s;
689{
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000690 extern long mystrtol PROTO((const char *, char **, int));
691 extern unsigned long mystrtoul PROTO((const char *, char **, int));
692 extern double atof PROTO((const char *));
Guido van Rossumc5e96291991-12-10 13:53:51 +0000693 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000694 long x;
Guido van Rossum50564e81996-01-12 01:13:16 +0000695#ifndef WITHOUT_COMPLEX
Guido van Rossum530956d1996-07-21 02:27:43 +0000696 Py_complex c;
Guido van Rossum50564e81996-01-12 01:13:16 +0000697 int imflag;
698#endif
699
Guido van Rossum282914b1991-04-04 10:42:56 +0000700 errno = 0;
Guido van Rossumc5e96291991-12-10 13:53:51 +0000701 end = s + strlen(s) - 1;
Guido van Rossum50564e81996-01-12 01:13:16 +0000702#ifndef WITHOUT_COMPLEX
Guido van Rossum15ad9a61996-01-26 20:53:56 +0000703 imflag = *end == 'j' || *end == 'J';
Guido van Rossum50564e81996-01-12 01:13:16 +0000704#endif
Guido van Rossumf1aeab71992-03-27 17:28:26 +0000705 if (*end == 'l' || *end == 'L')
Guido van Rossumc5e96291991-12-10 13:53:51 +0000706 return long_scan(s, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +0000707 if (s[0] == '0')
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000708 x = (long) mystrtoul(s, &end, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +0000709 else
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000710 x = mystrtol(s, &end, 0);
Guido van Rossum282914b1991-04-04 10:42:56 +0000711 if (*end == '\0') {
712 if (errno != 0) {
Guido van Rossum452a9831996-09-17 14:32:04 +0000713 com_error(co, OverflowError,
714 "integer literal too large");
Guido van Rossum282914b1991-04-04 10:42:56 +0000715 return NULL;
716 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000717 return newintobject(x);
Guido van Rossum282914b1991-04-04 10:42:56 +0000718 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000719 /* XXX Huge floats may silently fail */
Guido van Rossum50564e81996-01-12 01:13:16 +0000720#ifndef WITHOUT_COMPLEX
721 if (imflag) {
722 c.real = 0.;
723 c.imag = atof(s);
724 return newcomplexobject(c);
725 }
726 else
727#endif
728 return newfloatobject(atof(s));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000729}
730
731static object *
732parsestr(s)
733 char *s;
734{
735 object *v;
736 int len;
737 char *buf;
738 char *p;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000739 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000740 int c;
Guido van Rossum8054fad1993-10-26 15:19:44 +0000741 int quote = *s;
742 if (quote != '\'' && quote != '\"') {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000743 err_badcall();
744 return NULL;
745 }
746 s++;
747 len = strlen(s);
Guido van Rossum8054fad1993-10-26 15:19:44 +0000748 if (s[--len] != quote) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000749 err_badcall();
750 return NULL;
751 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000752 if (len >= 4 && s[0] == quote && s[1] == quote) {
753 s += 2;
754 len -= 2;
755 if (s[--len] != quote || s[--len] != quote) {
756 err_badcall();
757 return NULL;
758 }
759 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000760 if (strchr(s, '\\') == NULL)
761 return newsizedstringobject(s, len);
762 v = newsizedstringobject((char *)NULL, len);
763 p = buf = getstringvalue(v);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000764 end = s + len;
765 while (s < end) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000766 if (*s != '\\') {
767 *p++ = *s++;
768 continue;
769 }
770 s++;
771 switch (*s++) {
772 /* XXX This assumes ASCII! */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000773 case '\n': break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000774 case '\\': *p++ = '\\'; break;
775 case '\'': *p++ = '\''; break;
Guido van Rossum8054fad1993-10-26 15:19:44 +0000776 case '\"': *p++ = '\"'; break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000777 case 'b': *p++ = '\b'; break;
778 case 'f': *p++ = '\014'; break; /* FF */
779 case 't': *p++ = '\t'; break;
780 case 'n': *p++ = '\n'; break;
781 case 'r': *p++ = '\r'; break;
782 case 'v': *p++ = '\013'; break; /* VT */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000783 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
784 case '0': case '1': case '2': case '3':
785 case '4': case '5': case '6': case '7':
786 c = s[-1] - '0';
787 if ('0' <= *s && *s <= '7') {
788 c = (c<<3) + *s++ - '0';
789 if ('0' <= *s && *s <= '7')
790 c = (c<<3) + *s++ - '0';
791 }
792 *p++ = c;
793 break;
794 case 'x':
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000795 if (isxdigit(Py_CHARMASK(*s))) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000796 sscanf(s, "%x", &c);
797 *p++ = c;
798 do {
799 s++;
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000800 } while (isxdigit(Py_CHARMASK(*s)));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000801 break;
802 }
803 /* FALLTHROUGH */
804 default: *p++ = '\\'; *p++ = s[-1]; break;
805 }
806 }
807 resizestring(&v, (int)(p - buf));
808 return v;
809}
810
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000811static object *
812parsestrplus(n)
813 node *n;
814{
815 object *v;
816 int i;
817 REQ(CHILD(n, 0), STRING);
818 if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) {
819 /* String literal concatenation */
820 for (i = 1; i < NCH(n) && v != NULL; i++) {
821 joinstring_decref(&v, parsestr(STR(CHILD(n, i))));
822 }
823 }
824 return v;
825}
826
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000827static void
828com_list_constructor(c, n)
829 struct compiling *c;
830 node *n;
831{
832 int len;
833 int i;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000834 if (TYPE(n) != testlist)
835 REQ(n, exprlist);
836 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
837 len = (NCH(n) + 1) / 2;
838 for (i = 0; i < NCH(n); i += 2)
839 com_node(c, CHILD(n, i));
840 com_addoparg(c, BUILD_LIST, len);
Guido van Rossum8b993a91997-01-17 21:04:03 +0000841 com_pop(c, len-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000842}
843
844static void
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000845com_dictmaker(c, n)
846 struct compiling *c;
847 node *n;
848{
849 int i;
850 /* dictmaker: test ':' test (',' test ':' value)* [','] */
851 for (i = 0; i+2 < NCH(n); i += 4) {
852 /* We must arrange things just right for STORE_SUBSCR.
853 It wants the stack to look like (value) (dict) (key) */
854 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +0000855 com_push(c, 1);
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000856 com_node(c, CHILD(n, i+2)); /* value */
857 com_addbyte(c, ROT_TWO);
858 com_node(c, CHILD(n, i)); /* key */
859 com_addbyte(c, STORE_SUBSCR);
Guido van Rossum8b993a91997-01-17 21:04:03 +0000860 com_pop(c, 3);
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000861 }
862}
863
864static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000865com_atom(c, n)
866 struct compiling *c;
867 node *n;
868{
869 node *ch;
870 object *v;
871 int i;
872 REQ(n, atom);
873 ch = CHILD(n, 0);
874 switch (TYPE(ch)) {
875 case LPAR:
Guido van Rossum8b993a91997-01-17 21:04:03 +0000876 if (TYPE(CHILD(n, 1)) == RPAR) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000877 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +0000878 com_push(c, 1);
879 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000880 else
881 com_node(c, CHILD(n, 1));
882 break;
883 case LSQB:
Guido van Rossum8b993a91997-01-17 21:04:03 +0000884 if (TYPE(CHILD(n, 1)) == RSQB) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000885 com_addoparg(c, BUILD_LIST, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +0000886 com_push(c, 1);
887 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000888 else
889 com_list_constructor(c, CHILD(n, 1));
890 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000891 case LBRACE: /* '{' [dictmaker] '}' */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000892 com_addoparg(c, BUILD_MAP, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +0000893 com_push(c, 1);
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000894 if (TYPE(CHILD(n, 1)) != RBRACE)
895 com_dictmaker(c, CHILD(n, 1));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000896 break;
897 case BACKQUOTE:
898 com_node(c, CHILD(n, 1));
899 com_addbyte(c, UNARY_CONVERT);
900 break;
901 case NUMBER:
Guido van Rossum452a9831996-09-17 14:32:04 +0000902 if ((v = parsenumber(c, STR(ch))) == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000903 i = 255;
904 }
905 else {
906 i = com_addconst(c, v);
907 DECREF(v);
908 }
909 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +0000910 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000911 break;
912 case STRING:
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000913 v = parsestrplus(n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000914 if (v == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000915 c->c_errors++;
916 i = 255;
917 }
918 else {
919 i = com_addconst(c, v);
920 DECREF(v);
921 }
922 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +0000923 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000924 break;
925 case NAME:
926 com_addopname(c, LOAD_NAME, ch);
Guido van Rossum8b993a91997-01-17 21:04:03 +0000927 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000928 break;
929 default:
Guido van Rossum8b993a91997-01-17 21:04:03 +0000930 /* XXX fprintf(stderr, "node type %d\n", TYPE(ch)); */
Guido van Rossum452a9831996-09-17 14:32:04 +0000931 com_error(c, SystemError, "com_atom: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000932 }
933}
934
935static void
936com_slice(c, n, op)
937 struct compiling *c;
938 node *n;
939 int op;
940{
941 if (NCH(n) == 1) {
942 com_addbyte(c, op);
943 }
944 else if (NCH(n) == 2) {
945 if (TYPE(CHILD(n, 0)) != COLON) {
946 com_node(c, CHILD(n, 0));
947 com_addbyte(c, op+1);
948 }
949 else {
950 com_node(c, CHILD(n, 1));
951 com_addbyte(c, op+2);
952 }
Guido van Rossum8b993a91997-01-17 21:04:03 +0000953 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000954 }
955 else {
956 com_node(c, CHILD(n, 0));
957 com_node(c, CHILD(n, 2));
958 com_addbyte(c, op+3);
Guido van Rossum8b993a91997-01-17 21:04:03 +0000959 com_pop(c, 2);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000960 }
961}
962
Guido van Rossum635abd21997-01-06 22:56:52 +0000963static void
964com_argument(c, n, pkeywords)
Guido van Rossumf10570b1995-07-07 22:53:21 +0000965 struct compiling *c;
966 node *n; /* argument */
Guido van Rossum635abd21997-01-06 22:56:52 +0000967 object **pkeywords;
Guido van Rossumf10570b1995-07-07 22:53:21 +0000968{
969 node *m;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000970 REQ(n, argument); /* [test '='] test; really [keyword '='] test */
Guido van Rossumf10570b1995-07-07 22:53:21 +0000971 if (NCH(n) == 1) {
Guido van Rossum635abd21997-01-06 22:56:52 +0000972 if (*pkeywords != NULL) {
Guido van Rossum452a9831996-09-17 14:32:04 +0000973 com_error(c, SyntaxError,
Guido van Rossumf10570b1995-07-07 22:53:21 +0000974 "non-keyword arg after keyword arg");
Guido van Rossumf10570b1995-07-07 22:53:21 +0000975 }
976 else {
977 com_node(c, CHILD(n, 0));
978 }
Guido van Rossum635abd21997-01-06 22:56:52 +0000979 return;
Guido van Rossumf10570b1995-07-07 22:53:21 +0000980 }
981 m = n;
982 do {
983 m = CHILD(m, 0);
984 } while (NCH(m) == 1);
985 if (TYPE(m) != NAME) {
Guido van Rossum452a9831996-09-17 14:32:04 +0000986 com_error(c, SyntaxError, "keyword can't be an expression");
Guido van Rossumf10570b1995-07-07 22:53:21 +0000987 }
988 else {
989 object *v = newstringobject(STR(m));
Guido van Rossum635abd21997-01-06 22:56:52 +0000990 if (v != NULL && *pkeywords == NULL)
991 *pkeywords = newdictobject();
992 if (v == NULL || *pkeywords == NULL)
Guido van Rossumf10570b1995-07-07 22:53:21 +0000993 c->c_errors++;
994 else {
Guido van Rossum635abd21997-01-06 22:56:52 +0000995 if (dict2lookup(*pkeywords, v) != NULL)
996 com_error(c, SyntaxError,
997 "duplicate keyword argument");
998 else
999 if (dict2insert(*pkeywords, v, v) != 0)
1000 c->c_errors++;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001001 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001002 com_push(c, 1);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001003 DECREF(v);
1004 }
1005 }
1006 com_node(c, CHILD(n, 2));
Guido van Rossumf10570b1995-07-07 22:53:21 +00001007}
1008
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001009static void
1010com_call_function(c, n)
1011 struct compiling *c;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001012 node *n; /* EITHER arglist OR ')' */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001013{
1014 if (TYPE(n) == RPAR) {
Guido van Rossumf10570b1995-07-07 22:53:21 +00001015 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001016 }
1017 else {
Guido van Rossum635abd21997-01-06 22:56:52 +00001018 object *keywords = NULL;
1019 int i, na, nk;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001020 REQ(n, arglist);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001021 na = 0;
1022 nk = 0;
1023 for (i = 0; i < NCH(n); i += 2) {
Guido van Rossum635abd21997-01-06 22:56:52 +00001024 com_argument(c, CHILD(n, i), &keywords);
1025 if (keywords == NULL)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001026 na++;
1027 else
1028 nk++;
1029 }
Guido van Rossum635abd21997-01-06 22:56:52 +00001030 XDECREF(keywords);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001031 if (na > 255 || nk > 255) {
Guido van Rossum452a9831996-09-17 14:32:04 +00001032 com_error(c, SyntaxError, "more than 255 arguments");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001033 }
1034 com_addoparg(c, CALL_FUNCTION, na | (nk << 8));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001035 com_pop(c, na + 2*nk);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001036 }
1037}
1038
1039static void
1040com_select_member(c, n)
1041 struct compiling *c;
1042 node *n;
1043{
1044 com_addopname(c, LOAD_ATTR, n);
1045}
1046
1047static void
Guido van Rossum8861b741996-07-30 16:49:37 +00001048com_sliceobj(c, n)
1049 struct compiling *c;
1050 node *n;
1051{
1052 int i=0;
1053 int ns=2; /* number of slice arguments */
Guido van Rossum8861b741996-07-30 16:49:37 +00001054 node *ch;
1055
1056 /* first argument */
1057 if (TYPE(CHILD(n,i)) == COLON) {
1058 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001059 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001060 i++;
1061 }
1062 else {
1063 com_node(c, CHILD(n,i));
1064 i++;
1065 REQ(CHILD(n,i),COLON);
1066 i++;
1067 }
1068 /* second argument */
1069 if (i < NCH(n) && TYPE(CHILD(n,i)) == test) {
1070 com_node(c, CHILD(n,i));
1071 i++;
1072 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00001073 else {
1074 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
1075 com_push(c, 1);
1076 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001077 /* remaining arguments */
1078 for (; i < NCH(n); i++) {
1079 ns++;
1080 ch=CHILD(n,i);
1081 REQ(ch, sliceop);
1082 if (NCH(ch) == 1) {
1083 /* right argument of ':' missing */
1084 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001085 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001086 }
1087 else
1088 com_node(c, CHILD(ch,1));
1089 }
1090 com_addoparg(c, BUILD_SLICE, ns);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001091 com_pop(c, 1 + (ns == 3));
Guido van Rossum8861b741996-07-30 16:49:37 +00001092}
1093
1094static void
1095com_subscript(c, n)
1096 struct compiling *c;
1097 node *n;
1098{
1099 node *ch;
1100 REQ(n, subscript);
1101 ch = CHILD(n,0);
1102 /* check for rubber index */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001103 if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
Guido van Rossume449af71996-10-11 16:25:41 +00001104 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001105 com_push(c, 1);
1106 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001107 else {
1108 /* check for slice */
1109 if ((TYPE(ch) == COLON || NCH(n) > 1))
1110 com_sliceobj(c, n);
1111 else {
1112 REQ(ch, test);
1113 com_node(c, ch);
1114 }
1115 }
1116}
1117
1118static void
1119com_subscriptlist(c, n, assigning)
1120 struct compiling *c;
1121 node *n;
1122 int assigning;
1123{
1124 int i, op;
1125 REQ(n, subscriptlist);
1126 /* Check to make backward compatible slice behavior for '[i:j]' */
1127 if (NCH(n) == 1) {
1128 node *sub = CHILD(n, 0); /* subscript */
1129 /* Make it is a simple slice.
Guido van Rossum8b993a91997-01-17 21:04:03 +00001130 Should have exactly one colon. */
Guido van Rossum8861b741996-07-30 16:49:37 +00001131 if ((TYPE(CHILD(sub, 0)) == COLON
1132 || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
Guido van Rossum8b993a91997-01-17 21:04:03 +00001133 && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
1134 {
Guido van Rossum8861b741996-07-30 16:49:37 +00001135 if (assigning == OP_APPLY)
1136 op = SLICE;
1137 else
1138 op = ((assigning == OP_ASSIGN) ? STORE_SLICE : DELETE_SLICE);
1139 com_slice(c, sub, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001140 if (op == STORE_SLICE)
1141 com_pop(c, 2);
1142 else if (op == DELETE_SLICE)
1143 com_pop(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001144 return;
1145 }
1146 }
1147 /* Else normal subscriptlist. Compile each subscript. */
1148 for (i = 0; i < NCH(n); i += 2)
1149 com_subscript(c, CHILD(n, i));
1150 /* Put multiple subscripts into a tuple */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001151 if (NCH(n) > 1) {
1152 i = (NCH(n)+1) / 2;
1153 com_addoparg(c, BUILD_TUPLE, i);
1154 com_pop(c, i-1);
1155 }
1156 if (assigning == OP_APPLY) {
Guido van Rossum8861b741996-07-30 16:49:37 +00001157 op = BINARY_SUBSCR;
Guido van Rossum8b993a91997-01-17 21:04:03 +00001158 i = 1;
1159 }
1160 else if (assigning == OP_ASSIGN) {
1161 op = STORE_SUBSCR;
1162 i = 3;
1163 }
1164 else {
1165 op = DELETE_SUBSCR;
1166 i = 2;
1167 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001168 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001169 com_pop(c, i);
Guido van Rossum8861b741996-07-30 16:49:37 +00001170}
1171
1172static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001173com_apply_trailer(c, n)
1174 struct compiling *c;
1175 node *n;
1176{
1177 REQ(n, trailer);
1178 switch (TYPE(CHILD(n, 0))) {
1179 case LPAR:
1180 com_call_function(c, CHILD(n, 1));
1181 break;
1182 case DOT:
1183 com_select_member(c, CHILD(n, 1));
1184 break;
1185 case LSQB:
Guido van Rossum8861b741996-07-30 16:49:37 +00001186 com_subscriptlist(c, CHILD(n, 1), OP_APPLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001187 break;
1188 default:
Guido van Rossum452a9831996-09-17 14:32:04 +00001189 com_error(c, SystemError,
1190 "com_apply_trailer: unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001191 }
1192}
1193
1194static void
Guido van Rossum50564e81996-01-12 01:13:16 +00001195com_power(c, n)
1196 struct compiling *c;
1197 node *n;
1198{
1199 int i;
1200 REQ(n, power);
1201 com_atom(c, CHILD(n, 0));
1202 for (i = 1; i < NCH(n); i++) {
1203 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
1204 com_factor(c, CHILD(n, i+1));
1205 com_addbyte(c, BINARY_POWER);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001206 com_pop(c, 1);
Guido van Rossum50564e81996-01-12 01:13:16 +00001207 break;
1208 }
1209 else
1210 com_apply_trailer(c, CHILD(n, i));
1211 }
1212}
1213
1214static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001215com_factor(c, n)
1216 struct compiling *c;
1217 node *n;
1218{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001219 REQ(n, factor);
1220 if (TYPE(CHILD(n, 0)) == PLUS) {
1221 com_factor(c, CHILD(n, 1));
1222 com_addbyte(c, UNARY_POSITIVE);
1223 }
1224 else if (TYPE(CHILD(n, 0)) == MINUS) {
1225 com_factor(c, CHILD(n, 1));
1226 com_addbyte(c, UNARY_NEGATIVE);
1227 }
Guido van Rossum7928cd71991-10-24 14:59:31 +00001228 else if (TYPE(CHILD(n, 0)) == TILDE) {
1229 com_factor(c, CHILD(n, 1));
1230 com_addbyte(c, UNARY_INVERT);
1231 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001232 else {
Guido van Rossum50564e81996-01-12 01:13:16 +00001233 com_power(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001234 }
1235}
1236
1237static void
1238com_term(c, n)
1239 struct compiling *c;
1240 node *n;
1241{
1242 int i;
1243 int op;
1244 REQ(n, term);
1245 com_factor(c, CHILD(n, 0));
1246 for (i = 2; i < NCH(n); i += 2) {
1247 com_factor(c, CHILD(n, i));
1248 switch (TYPE(CHILD(n, i-1))) {
1249 case STAR:
1250 op = BINARY_MULTIPLY;
1251 break;
1252 case SLASH:
1253 op = BINARY_DIVIDE;
1254 break;
1255 case PERCENT:
1256 op = BINARY_MODULO;
1257 break;
1258 default:
Guido van Rossum452a9831996-09-17 14:32:04 +00001259 com_error(c, SystemError,
1260 "com_term: operator not *, / or %");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001261 op = 255;
1262 }
1263 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001264 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001265 }
1266}
1267
1268static void
1269com_arith_expr(c, n)
1270 struct compiling *c;
1271 node *n;
1272{
1273 int i;
1274 int op;
1275 REQ(n, arith_expr);
1276 com_term(c, CHILD(n, 0));
1277 for (i = 2; i < NCH(n); i += 2) {
1278 com_term(c, CHILD(n, i));
1279 switch (TYPE(CHILD(n, i-1))) {
1280 case PLUS:
1281 op = BINARY_ADD;
1282 break;
1283 case MINUS:
1284 op = BINARY_SUBTRACT;
1285 break;
1286 default:
Guido van Rossum452a9831996-09-17 14:32:04 +00001287 com_error(c, SystemError,
1288 "com_arith_expr: operator not + or -");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001289 op = 255;
1290 }
1291 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001292 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001293 }
1294}
1295
1296static void
1297com_shift_expr(c, n)
1298 struct compiling *c;
1299 node *n;
1300{
1301 int i;
1302 int op;
1303 REQ(n, shift_expr);
1304 com_arith_expr(c, CHILD(n, 0));
1305 for (i = 2; i < NCH(n); i += 2) {
1306 com_arith_expr(c, CHILD(n, i));
1307 switch (TYPE(CHILD(n, i-1))) {
1308 case LEFTSHIFT:
1309 op = BINARY_LSHIFT;
1310 break;
1311 case RIGHTSHIFT:
1312 op = BINARY_RSHIFT;
1313 break;
1314 default:
Guido van Rossum452a9831996-09-17 14:32:04 +00001315 com_error(c, SystemError,
1316 "com_shift_expr: operator not << or >>");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001317 op = 255;
1318 }
1319 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001320 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001321 }
1322}
1323
1324static void
1325com_and_expr(c, n)
1326 struct compiling *c;
1327 node *n;
1328{
1329 int i;
1330 int op;
1331 REQ(n, and_expr);
1332 com_shift_expr(c, CHILD(n, 0));
1333 for (i = 2; i < NCH(n); i += 2) {
1334 com_shift_expr(c, CHILD(n, i));
1335 if (TYPE(CHILD(n, i-1)) == AMPER) {
1336 op = BINARY_AND;
1337 }
1338 else {
Guido van Rossum452a9831996-09-17 14:32:04 +00001339 com_error(c, SystemError,
1340 "com_and_expr: operator not &");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001341 op = 255;
1342 }
1343 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001344 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001345 }
1346}
1347
1348static void
1349com_xor_expr(c, n)
1350 struct compiling *c;
1351 node *n;
1352{
1353 int i;
1354 int op;
1355 REQ(n, xor_expr);
1356 com_and_expr(c, CHILD(n, 0));
1357 for (i = 2; i < NCH(n); i += 2) {
1358 com_and_expr(c, CHILD(n, i));
1359 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
1360 op = BINARY_XOR;
1361 }
1362 else {
Guido van Rossum452a9831996-09-17 14:32:04 +00001363 com_error(c, SystemError,
1364 "com_xor_expr: operator not ^");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001365 op = 255;
1366 }
1367 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001368 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001369 }
1370}
1371
1372static void
1373com_expr(c, n)
1374 struct compiling *c;
1375 node *n;
1376{
1377 int i;
1378 int op;
1379 REQ(n, expr);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001380 com_xor_expr(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001381 for (i = 2; i < NCH(n); i += 2) {
Guido van Rossum7928cd71991-10-24 14:59:31 +00001382 com_xor_expr(c, CHILD(n, i));
1383 if (TYPE(CHILD(n, i-1)) == VBAR) {
1384 op = BINARY_OR;
1385 }
1386 else {
Guido van Rossum452a9831996-09-17 14:32:04 +00001387 com_error(c, SystemError,
1388 "com_expr: expr operator not |");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001389 op = 255;
1390 }
1391 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001392 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001393 }
1394}
1395
1396static enum cmp_op
1397cmp_type(n)
1398 node *n;
1399{
1400 REQ(n, comp_op);
Guido van Rossum01cfd441991-10-20 20:12:38 +00001401 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001402 | 'in' | 'not' 'in' | 'is' | 'is' not' */
1403 if (NCH(n) == 1) {
1404 n = CHILD(n, 0);
1405 switch (TYPE(n)) {
1406 case LESS: return LT;
1407 case GREATER: return GT;
Guido van Rossum01cfd441991-10-20 20:12:38 +00001408 case EQEQUAL: /* == */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001409 case EQUAL: return EQ;
Guido van Rossum01cfd441991-10-20 20:12:38 +00001410 case LESSEQUAL: return LE;
1411 case GREATEREQUAL: return GE;
1412 case NOTEQUAL: return NE; /* <> or != */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001413 case NAME: if (strcmp(STR(n), "in") == 0) return IN;
1414 if (strcmp(STR(n), "is") == 0) return IS;
1415 }
1416 }
1417 else if (NCH(n) == 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001418 switch (TYPE(CHILD(n, 0))) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001419 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1420 return NOT_IN;
1421 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1422 return IS_NOT;
1423 }
1424 }
1425 return BAD;
1426}
1427
1428static void
1429com_comparison(c, n)
1430 struct compiling *c;
1431 node *n;
1432{
1433 int i;
1434 enum cmp_op op;
1435 int anchor;
1436 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
1437 com_expr(c, CHILD(n, 0));
1438 if (NCH(n) == 1)
1439 return;
1440
1441 /****************************************************************
1442 The following code is generated for all but the last
1443 comparison in a chain:
1444
1445 label: on stack: opcode: jump to:
1446
1447 a <code to load b>
1448 a, b DUP_TOP
1449 a, b, b ROT_THREE
1450 b, a, b COMPARE_OP
1451 b, 0-or-1 JUMP_IF_FALSE L1
1452 b, 1 POP_TOP
1453 b
1454
1455 We are now ready to repeat this sequence for the next
1456 comparison in the chain.
1457
1458 For the last we generate:
1459
1460 b <code to load c>
1461 b, c COMPARE_OP
1462 0-or-1
1463
1464 If there were any jumps to L1 (i.e., there was more than one
1465 comparison), we generate:
1466
1467 0-or-1 JUMP_FORWARD L2
1468 L1: b, 0 ROT_TWO
1469 0, b POP_TOP
1470 0
Guido van Rossum8b993a91997-01-17 21:04:03 +00001471 L2: 0-or-1
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001472 ****************************************************************/
1473
1474 anchor = 0;
1475
1476 for (i = 2; i < NCH(n); i += 2) {
1477 com_expr(c, CHILD(n, i));
1478 if (i+2 < NCH(n)) {
1479 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001480 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001481 com_addbyte(c, ROT_THREE);
1482 }
1483 op = cmp_type(CHILD(n, i-1));
1484 if (op == BAD) {
Guido van Rossum452a9831996-09-17 14:32:04 +00001485 com_error(c, SystemError,
1486 "com_comparison: unknown comparison op");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001487 }
1488 com_addoparg(c, COMPARE_OP, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001489 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001490 if (i+2 < NCH(n)) {
1491 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1492 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001493 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001494 }
1495 }
1496
1497 if (anchor) {
1498 int anchor2 = 0;
1499 com_addfwref(c, JUMP_FORWARD, &anchor2);
1500 com_backpatch(c, anchor);
1501 com_addbyte(c, ROT_TWO);
1502 com_addbyte(c, POP_TOP);
1503 com_backpatch(c, anchor2);
1504 }
1505}
1506
1507static void
1508com_not_test(c, n)
1509 struct compiling *c;
1510 node *n;
1511{
1512 REQ(n, not_test); /* 'not' not_test | comparison */
1513 if (NCH(n) == 1) {
1514 com_comparison(c, CHILD(n, 0));
1515 }
1516 else {
1517 com_not_test(c, CHILD(n, 1));
1518 com_addbyte(c, UNARY_NOT);
1519 }
1520}
1521
1522static void
1523com_and_test(c, n)
1524 struct compiling *c;
1525 node *n;
1526{
1527 int i;
1528 int anchor;
1529 REQ(n, and_test); /* not_test ('and' not_test)* */
1530 anchor = 0;
1531 i = 0;
1532 for (;;) {
1533 com_not_test(c, CHILD(n, i));
1534 if ((i += 2) >= NCH(n))
1535 break;
1536 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1537 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001538 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001539 }
1540 if (anchor)
1541 com_backpatch(c, anchor);
1542}
1543
1544static void
1545com_test(c, n)
1546 struct compiling *c;
1547 node *n;
1548{
Guido van Rossum8b993a91997-01-17 21:04:03 +00001549 REQ(n, test); /* and_test ('or' and_test)* | lambdef */
Guido van Rossum57531fe1993-11-30 14:57:42 +00001550 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
1551 object *v;
1552 int i;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001553 int ndefs = com_argdefs(c, CHILD(n, 0));
Guido van Rossum8ff077b1996-08-24 06:21:31 +00001554 v = (object *) icompile(CHILD(n, 0), c);
Guido van Rossum57531fe1993-11-30 14:57:42 +00001555 if (v == NULL) {
1556 c->c_errors++;
1557 i = 255;
1558 }
1559 else {
1560 i = com_addconst(c, v);
1561 DECREF(v);
1562 }
1563 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001564 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001565 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001566 com_pop(c, ndefs);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001567 }
Guido van Rossum57531fe1993-11-30 14:57:42 +00001568 else {
1569 int anchor = 0;
1570 int i = 0;
1571 for (;;) {
1572 com_and_test(c, CHILD(n, i));
1573 if ((i += 2) >= NCH(n))
1574 break;
1575 com_addfwref(c, JUMP_IF_TRUE, &anchor);
1576 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001577 com_pop(c, 1);
Guido van Rossum57531fe1993-11-30 14:57:42 +00001578 }
1579 if (anchor)
1580 com_backpatch(c, anchor);
1581 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001582}
1583
1584static void
Guido van Rossum288a60f1991-12-16 13:05:10 +00001585com_list(c, n, toplevel)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001586 struct compiling *c;
1587 node *n;
Guido van Rossum288a60f1991-12-16 13:05:10 +00001588 int toplevel; /* If nonzero, *always* build a tuple */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001589{
1590 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
Guido van Rossum288a60f1991-12-16 13:05:10 +00001591 if (NCH(n) == 1 && !toplevel) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001592 com_node(c, CHILD(n, 0));
1593 }
1594 else {
1595 int i;
1596 int len;
1597 len = (NCH(n) + 1) / 2;
1598 for (i = 0; i < NCH(n); i += 2)
1599 com_node(c, CHILD(n, i));
1600 com_addoparg(c, BUILD_TUPLE, len);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001601 com_pop(c, len-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001602 }
1603}
1604
1605
1606/* Begin of assignment compilation */
1607
1608static void com_assign_name PROTO((struct compiling *, node *, int));
1609static void com_assign PROTO((struct compiling *, node *, int));
1610
1611static void
1612com_assign_attr(c, n, assigning)
1613 struct compiling *c;
1614 node *n;
1615 int assigning;
1616{
1617 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001618 com_pop(c, assigning ? 2 : 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001619}
1620
1621static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001622com_assign_trailer(c, n, assigning)
1623 struct compiling *c;
1624 node *n;
1625 int assigning;
1626{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001627 REQ(n, trailer);
1628 switch (TYPE(CHILD(n, 0))) {
1629 case LPAR: /* '(' [exprlist] ')' */
Guido van Rossum452a9831996-09-17 14:32:04 +00001630 com_error(c, SyntaxError, "can't assign to function call");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001631 break;
1632 case DOT: /* '.' NAME */
1633 com_assign_attr(c, CHILD(n, 1), assigning);
1634 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00001635 case LSQB: /* '[' subscriptlist ']' */
1636 com_subscriptlist(c, CHILD(n, 1), assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001637 break;
1638 default:
Guido van Rossum452a9831996-09-17 14:32:04 +00001639 com_error(c, SystemError, "unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001640 }
1641}
1642
1643static void
1644com_assign_tuple(c, n, assigning)
1645 struct compiling *c;
1646 node *n;
1647 int assigning;
1648{
1649 int i;
1650 if (TYPE(n) != testlist)
1651 REQ(n, exprlist);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001652 if (assigning) {
1653 i = (NCH(n)+1)/2;
1654 com_addoparg(c, UNPACK_TUPLE, i);
1655 com_push(c, i-1);
1656 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001657 for (i = 0; i < NCH(n); i += 2)
1658 com_assign(c, CHILD(n, i), assigning);
1659}
1660
1661static void
1662com_assign_list(c, n, assigning)
1663 struct compiling *c;
1664 node *n;
1665 int assigning;
1666{
1667 int i;
Guido van Rossum8b993a91997-01-17 21:04:03 +00001668 if (assigning) {
1669 i = (NCH(n)+1)/2;
1670 com_addoparg(c, UNPACK_LIST, i);
1671 com_push(c, i-1);
1672 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001673 for (i = 0; i < NCH(n); i += 2)
1674 com_assign(c, CHILD(n, i), assigning);
1675}
1676
1677static void
1678com_assign_name(c, n, assigning)
1679 struct compiling *c;
1680 node *n;
1681 int assigning;
1682{
1683 REQ(n, NAME);
1684 com_addopname(c, assigning ? STORE_NAME : DELETE_NAME, n);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001685 if (assigning)
1686 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001687}
1688
1689static void
1690com_assign(c, n, assigning)
1691 struct compiling *c;
1692 node *n;
1693 int assigning;
1694{
1695 /* Loop to avoid trivial recursion */
1696 for (;;) {
1697 switch (TYPE(n)) {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001698
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001699 case exprlist:
1700 case testlist:
1701 if (NCH(n) > 1) {
1702 com_assign_tuple(c, n, assigning);
1703 return;
1704 }
1705 n = CHILD(n, 0);
1706 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001707
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001708 case test:
1709 case and_test:
1710 case not_test:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001711 case comparison:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001712 case expr:
Guido van Rossum7928cd71991-10-24 14:59:31 +00001713 case xor_expr:
1714 case and_expr:
1715 case shift_expr:
1716 case arith_expr:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001717 case term:
Guido van Rossum50564e81996-01-12 01:13:16 +00001718 case factor:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001719 if (NCH(n) > 1) {
Guido van Rossum452a9831996-09-17 14:32:04 +00001720 com_error(c, SyntaxError,
1721 "can't assign to operator");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001722 return;
1723 }
1724 n = CHILD(n, 0);
1725 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001726
Guido van Rossum50564e81996-01-12 01:13:16 +00001727 case power: /* atom trailer* ('**' power)* */
1728/* ('+'|'-'|'~') factor | atom trailer* */
1729 if (TYPE(CHILD(n, 0)) != atom) {
Guido van Rossum452a9831996-09-17 14:32:04 +00001730 com_error(c, SyntaxError,
1731 "can't assign to operator");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001732 return;
1733 }
Guido van Rossum50564e81996-01-12 01:13:16 +00001734 if (NCH(n) > 1) { /* trailer or exponent present */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001735 int i;
1736 com_node(c, CHILD(n, 0));
1737 for (i = 1; i+1 < NCH(n); i++) {
Guido van Rossum50564e81996-01-12 01:13:16 +00001738 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
Guido van Rossum452a9831996-09-17 14:32:04 +00001739 com_error(c, SyntaxError,
1740 "can't assign to operator");
Guido van Rossum50564e81996-01-12 01:13:16 +00001741 return;
1742 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001743 com_apply_trailer(c, CHILD(n, i));
1744 } /* NB i is still alive */
1745 com_assign_trailer(c,
1746 CHILD(n, i), assigning);
1747 return;
1748 }
1749 n = CHILD(n, 0);
1750 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001751
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001752 case atom:
1753 switch (TYPE(CHILD(n, 0))) {
1754 case LPAR:
1755 n = CHILD(n, 1);
1756 if (TYPE(n) == RPAR) {
1757 /* XXX Should allow () = () ??? */
Guido van Rossum452a9831996-09-17 14:32:04 +00001758 com_error(c, SyntaxError,
1759 "can't assign to ()");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001760 return;
1761 }
1762 break;
1763 case LSQB:
1764 n = CHILD(n, 1);
1765 if (TYPE(n) == RSQB) {
Guido van Rossum452a9831996-09-17 14:32:04 +00001766 com_error(c, SyntaxError,
1767 "can't assign to []");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001768 return;
1769 }
1770 com_assign_list(c, n, assigning);
1771 return;
1772 case NAME:
1773 com_assign_name(c, CHILD(n, 0), assigning);
1774 return;
1775 default:
Guido van Rossum452a9831996-09-17 14:32:04 +00001776 com_error(c, SyntaxError,
1777 "can't assign to literal");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001778 return;
1779 }
1780 break;
Guido van Rossum15cc9a01996-08-08 18:51:04 +00001781
1782 case lambdef:
Guido van Rossum452a9831996-09-17 14:32:04 +00001783 com_error(c, SyntaxError, "can't assign to lambda");
Guido van Rossum15cc9a01996-08-08 18:51:04 +00001784 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001785
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001786 default:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001787 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
Guido van Rossum452a9831996-09-17 14:32:04 +00001788 com_error(c, SystemError, "com_assign: bad node");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001789 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001790
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001791 }
1792 }
1793}
Guido van Rossum8b993a91997-01-17 21:04:03 +00001794/* Forward */ static node *get_rawdocstring PROTO((node *));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001795
1796static void
1797com_expr_stmt(c, n)
1798 struct compiling *c;
1799 node *n;
1800{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001801 REQ(n, expr_stmt); /* testlist ('=' testlist)* */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001802 /* Forget it if we have just a doc string here */
1803 if (NCH(n) == 1 && get_rawdocstring(n) != NULL)
1804 return;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001805 com_node(c, CHILD(n, NCH(n)-1));
1806 if (NCH(n) == 1) {
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001807 if (c->c_interactive)
1808 com_addbyte(c, PRINT_EXPR);
1809 else
1810 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001811 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001812 }
1813 else {
1814 int i;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001815 for (i = 0; i < NCH(n)-2; i+=2) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00001816 if (i+2 < NCH(n)-2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001817 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001818 com_push(c, 1);
1819 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001820 com_assign(c, CHILD(n, i), OP_ASSIGN);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001821 }
1822 }
1823}
1824
1825static void
1826com_print_stmt(c, n)
1827 struct compiling *c;
1828 node *n;
1829{
1830 int i;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001831 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
1832 for (i = 1; i < NCH(n); i += 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001833 com_node(c, CHILD(n, i));
1834 com_addbyte(c, PRINT_ITEM);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001835 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001836 }
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001837 if (TYPE(CHILD(n, NCH(n)-1)) != COMMA)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001838 com_addbyte(c, PRINT_NEWLINE);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001839 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001840}
1841
1842static void
1843com_return_stmt(c, n)
1844 struct compiling *c;
1845 node *n;
1846{
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001847 REQ(n, return_stmt); /* 'return' [testlist] */
Guido van Rossum3f5da241990-12-20 15:06:42 +00001848 if (!c->c_infunction) {
Guido van Rossum452a9831996-09-17 14:32:04 +00001849 com_error(c, SyntaxError, "'return' outside function");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001850 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00001851 if (NCH(n) < 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001852 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001853 com_push(c, 1);
1854 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001855 else
1856 com_node(c, CHILD(n, 1));
1857 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001858 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001859}
1860
1861static void
1862com_raise_stmt(c, n)
1863 struct compiling *c;
1864 node *n;
1865{
Guido van Rossum8b993a91997-01-17 21:04:03 +00001866 int i;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001867 REQ(n, raise_stmt); /* 'raise' test [',' test [',' test]] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001868 com_node(c, CHILD(n, 1));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001869 if (NCH(n) > 3) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001870 com_node(c, CHILD(n, 3));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001871 if (NCH(n) > 5)
1872 com_node(c, CHILD(n, 5));
Guido van Rossumf10570b1995-07-07 22:53:21 +00001873 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00001874 i = NCH(n)/2;
1875 com_addoparg(c, RAISE_VARARGS, i);
1876 com_pop(c, i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001877}
1878
1879static void
1880com_import_stmt(c, n)
1881 struct compiling *c;
1882 node *n;
1883{
1884 int i;
1885 REQ(n, import_stmt);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001886 /* 'import' dotted_name (',' dotted_name)* |
1887 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001888 if (STR(CHILD(n, 0))[0] == 'f') {
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001889 /* 'from' dotted_name 'import' ... */
1890 REQ(CHILD(n, 1), dotted_name);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001891 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001892 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001893 for (i = 3; i < NCH(n); i += 2)
1894 com_addopname(c, IMPORT_FROM, CHILD(n, i));
1895 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001896 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001897 }
1898 else {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001899 /* 'import' ... */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001900 for (i = 1; i < NCH(n); i += 2) {
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001901 REQ(CHILD(n, i), dotted_name);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001902 com_addopname(c, IMPORT_NAME, CHILD(n, i));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001903 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001904 com_addopname(c, STORE_NAME, CHILD(CHILD(n, i), 0));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001905 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001906 }
1907 }
1908}
1909
1910static void
Guido van Rossumc5e96291991-12-10 13:53:51 +00001911com_global_stmt(c, n)
1912 struct compiling *c;
1913 node *n;
1914{
1915 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00001916 REQ(n, global_stmt);
1917 /* 'global' NAME (',' NAME)* */
1918 for (i = 1; i < NCH(n); i += 2) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00001919 char *s = STR(CHILD(n, i));
Guido van Rossumfe2236f1996-08-24 07:29:04 +00001920#ifdef PRIVATE_NAME_MANGLING
1921 char buffer[256];
1922 if (s != NULL && s[0] == '_' && s[1] == '_' &&
1923 c->c_private != NULL &&
1924 com_mangle(c, s, buffer, (int)sizeof(buffer)))
1925 s = buffer;
1926#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00001927 if (dictlookup(c->c_locals, s) != NULL) {
Guido van Rossum452a9831996-09-17 14:32:04 +00001928 com_error(c, SyntaxError, "name is local and global");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001929 }
1930 else if (dictinsert(c->c_globals, s, None) != 0)
Guido van Rossumc5e96291991-12-10 13:53:51 +00001931 c->c_errors++;
1932 }
1933}
1934
Guido van Rossum681d79a1995-07-18 14:51:37 +00001935static int
1936com_newlocal_o(c, nameval)
1937 struct compiling *c;
1938 object *nameval;
1939{
1940 int i;
1941 object *ival;
1942 if (getlistsize(c->c_varnames) != c->c_nlocals) {
1943 /* This is usually caused by an error on a previous call */
1944 if (c->c_errors == 0) {
Guido van Rossum452a9831996-09-17 14:32:04 +00001945 com_error(c, SystemError, "mixed up var name/index");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001946 }
1947 return 0;
1948 }
1949 ival = newintobject(i = c->c_nlocals++);
1950 if (ival == NULL)
1951 c->c_errors++;
1952 else if (mappinginsert(c->c_locals, nameval, ival) != 0)
1953 c->c_errors++;
1954 else if (addlistitem(c->c_varnames, nameval) != 0)
1955 c->c_errors++;
1956 XDECREF(ival);
1957 return i;
1958}
1959
1960static int
1961com_addlocal_o(c, nameval)
1962 struct compiling *c;
1963 object *nameval;
1964{
1965 object *ival = mappinglookup(c->c_locals, nameval);
1966 if (ival != NULL)
1967 return getintvalue(ival);
1968 return com_newlocal_o(c, nameval);
1969}
1970
1971static int
1972com_newlocal(c, name)
1973 struct compiling *c;
1974 char *name;
1975{
1976 object *nameval = newstringobject(name);
1977 int i;
1978 if (nameval == NULL) {
1979 c->c_errors++;
1980 return 0;
1981 }
1982 i = com_newlocal_o(c, nameval);
1983 DECREF(nameval);
1984 return i;
1985}
1986
Guido van Rossum8b993a91997-01-17 21:04:03 +00001987#ifdef SUPPORT_OBSOLETE_ACCESS
1988
Guido van Rossum25831651993-05-19 14:50:45 +00001989#define strequ(a, b) (strcmp((a), (b)) == 0)
1990
1991static void
1992com_access_stmt(c, n)
1993 struct compiling *c;
1994 node *n;
1995{
1996 int i, j, k, mode, imode;
1997 object *vmode;
1998 REQ(n, access_stmt);
1999 /* 'access' NAME (',' NAME)* ':' accesstype (',' accesstype)*
2000 accesstype: NAME+ */
2001
2002 /* Find where the colon is */
2003 i = 1;
2004 while (TYPE(CHILD(n,i-1)) != COLON)
2005 i += 1;
2006
2007 /* Calculate the mode mask */
2008 mode = 0;
2009 for (j = i; j < NCH(n); j += 2) {
Guido van Rossum81daa321993-05-20 14:24:46 +00002010 int r = 0, w = 0, p = 0;
2011 for (k = 0; k < NCH(CHILD(n,j)); k++) {
Guido van Rossum25831651993-05-19 14:50:45 +00002012 if (strequ(STR(CHILD(CHILD(n,j),k)), "public"))
2013 p = 0;
2014 else if (strequ(STR(CHILD(CHILD(n,j),k)), "protected"))
2015 p = 1;
2016 else if (strequ(STR(CHILD(CHILD(n,j),k)), "private"))
2017 p = 2;
2018 else if (strequ(STR(CHILD(CHILD(n,j),k)), "read"))
2019 r = 1;
2020 else if (strequ(STR(CHILD(CHILD(n,j),k)), "write"))
2021 w = 1;
2022 else /* XXX should make this an exception */
2023 fprintf(stderr, "bad access type %s\n",
2024 STR(CHILD(CHILD(n,j),k)));
2025 }
2026 if (r == 0 && w == 0)
Guido van Rossum81daa321993-05-20 14:24:46 +00002027 r = w = 1;
Guido van Rossum25831651993-05-19 14:50:45 +00002028 if (p == 0) {
2029 if (r == 1) mode |= AC_R_PUBLIC;
2030 if (w == 1) mode |= AC_W_PUBLIC;
2031 } else if (p == 1) {
2032 if (r == 1) mode |= AC_R_PROTECTED;
2033 if (w == 1) mode |= AC_W_PROTECTED;
2034 } else {
2035 if (r == 1) mode |= AC_R_PRIVATE;
2036 if (w == 1) mode |= AC_W_PRIVATE;
2037 }
2038 }
2039 vmode = newintobject((long)mode);
2040 imode = com_addconst(c, vmode);
2041 XDECREF(vmode);
2042 for (i = 1; TYPE(CHILD(n,i-1)) != COLON; i+=2) {
2043 com_addoparg(c, LOAD_CONST, imode);
2044 com_addopname(c, ACCESS_MODE, CHILD(n, i));
2045 }
2046}
Guido van Rossum0dfcf751996-08-12 22:00:53 +00002047#endif
Guido van Rossum25831651993-05-19 14:50:45 +00002048
Guido van Rossumc5e96291991-12-10 13:53:51 +00002049static void
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002050com_exec_stmt(c, n)
2051 struct compiling *c;
2052 node *n;
2053{
2054 REQ(n, exec_stmt);
2055 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
2056 com_node(c, CHILD(n, 1));
2057 if (NCH(n) >= 4)
2058 com_node(c, CHILD(n, 3));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002059 else {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002060 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002061 com_push(c, 1);
2062 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002063 if (NCH(n) >= 6)
2064 com_node(c, CHILD(n, 5));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002065 else {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002066 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002067 com_push(c, 1);
2068 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002069 com_addbyte(c, EXEC_STMT);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002070 com_pop(c, 3);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002071}
2072
2073static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002074com_if_stmt(c, n)
2075 struct compiling *c;
2076 node *n;
2077{
2078 int i;
2079 int anchor = 0;
2080 REQ(n, if_stmt);
2081 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
2082 for (i = 0; i+3 < NCH(n); i+=4) {
2083 int a = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002084 node *ch = CHILD(n, i+1);
2085 if (i > 0)
2086 com_addoparg(c, SET_LINENO, ch->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002087 com_node(c, CHILD(n, i+1));
2088 com_addfwref(c, JUMP_IF_FALSE, &a);
2089 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002090 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002091 com_node(c, CHILD(n, i+3));
2092 com_addfwref(c, JUMP_FORWARD, &anchor);
2093 com_backpatch(c, a);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002094 /* We jump here with an extra entry which we now pop */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002095 com_addbyte(c, POP_TOP);
2096 }
2097 if (i+2 < NCH(n))
2098 com_node(c, CHILD(n, i+2));
2099 com_backpatch(c, anchor);
2100}
2101
2102static void
2103com_while_stmt(c, n)
2104 struct compiling *c;
2105 node *n;
2106{
2107 int break_anchor = 0;
2108 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002109 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002110 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
2111 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002112 block_push(c, SETUP_LOOP);
2113 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002114 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002115 com_node(c, CHILD(n, 1));
2116 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2117 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002118 com_pop(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002119 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002120 com_node(c, CHILD(n, 3));
Guido van Rossum3f5da241990-12-20 15:06:42 +00002121 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002122 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2123 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002124 com_backpatch(c, anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002125 /* We jump here with one entry more on the stack */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002126 com_addbyte(c, POP_TOP);
2127 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002128 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002129 if (NCH(n) > 4)
2130 com_node(c, CHILD(n, 6));
2131 com_backpatch(c, break_anchor);
2132}
2133
2134static void
2135com_for_stmt(c, n)
2136 struct compiling *c;
2137 node *n;
2138{
2139 object *v;
2140 int break_anchor = 0;
2141 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002142 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002143 REQ(n, for_stmt);
2144 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
2145 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002146 block_push(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002147 com_node(c, CHILD(n, 3));
2148 v = newintobject(0L);
2149 if (v == NULL)
2150 c->c_errors++;
2151 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002152 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002153 XDECREF(v);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002154 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002155 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002156 com_addfwref(c, FOR_LOOP, &anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002157 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00002158 com_assign(c, CHILD(n, 1), OP_ASSIGN);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002159 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002160 com_node(c, CHILD(n, 5));
Guido van Rossum3f5da241990-12-20 15:06:42 +00002161 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002162 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2163 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002164 com_backpatch(c, anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002165 com_pop(c, 2); /* FOR_LOOP has popped these */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002166 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002167 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002168 if (NCH(n) > 8)
2169 com_node(c, CHILD(n, 8));
2170 com_backpatch(c, break_anchor);
2171}
2172
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002173/* Code generated for "try: S finally: Sf" is as follows:
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002174
2175 SETUP_FINALLY L
2176 <code for S>
2177 POP_BLOCK
2178 LOAD_CONST <nil>
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002179 L: <code for Sf>
2180 END_FINALLY
2181
2182 The special instructions use the block stack. Each block
2183 stack entry contains the instruction that created it (here
2184 SETUP_FINALLY), the level of the value stack at the time the
2185 block stack entry was created, and a label (here L).
2186
2187 SETUP_FINALLY:
2188 Pushes the current value stack level and the label
2189 onto the block stack.
2190 POP_BLOCK:
2191 Pops en entry from the block stack, and pops the value
2192 stack until its level is the same as indicated on the
2193 block stack. (The label is ignored.)
2194 END_FINALLY:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002195 Pops a variable number of entries from the *value* stack
2196 and re-raises the exception they specify. The number of
2197 entries popped depends on the (pseudo) exception type.
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002198
2199 The block stack is unwound when an exception is raised:
2200 when a SETUP_FINALLY entry is found, the exception is pushed
2201 onto the value stack (and the exception condition is cleared),
2202 and the interpreter jumps to the label gotten from the block
2203 stack.
2204
2205 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
Guido van Rossum3f5da241990-12-20 15:06:42 +00002206 (The contents of the value stack is shown in [], with the top
2207 at the right; 'tb' is trace-back info, 'val' the exception's
2208 associated value, and 'exc' the exception.)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002209
2210 Value stack Label Instruction Argument
2211 [] SETUP_EXCEPT L1
2212 [] <code for S>
2213 [] POP_BLOCK
2214 [] JUMP_FORWARD L0
2215
Guido van Rossum3f5da241990-12-20 15:06:42 +00002216 [tb, val, exc] L1: DUP )
2217 [tb, val, exc, exc] <evaluate E1> )
2218 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2219 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2220 [tb, val, exc, 1] POP )
2221 [tb, val, exc] POP
2222 [tb, val] <assign to V1> (or POP if no V1)
2223 [tb] POP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002224 [] <code for S1>
2225 JUMP_FORWARD L0
2226
Guido van Rossum3f5da241990-12-20 15:06:42 +00002227 [tb, val, exc, 0] L2: POP
2228 [tb, val, exc] DUP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002229 .............................etc.......................
2230
Guido van Rossum3f5da241990-12-20 15:06:42 +00002231 [tb, val, exc, 0] Ln+1: POP
2232 [tb, val, exc] END_FINALLY # re-raise exception
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002233
2234 [] L0: <next statement>
2235
2236 Of course, parts are not generated if Vi or Ei is not present.
2237*/
2238
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002239static void
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002240com_try_except(c, n)
2241 struct compiling *c;
2242 node *n;
2243{
2244 int except_anchor = 0;
2245 int end_anchor = 0;
2246 int else_anchor = 0;
2247 int i;
2248 node *ch;
2249
2250 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
2251 block_push(c, SETUP_EXCEPT);
2252 com_node(c, CHILD(n, 2));
2253 com_addbyte(c, POP_BLOCK);
2254 block_pop(c, SETUP_EXCEPT);
2255 com_addfwref(c, JUMP_FORWARD, &else_anchor);
2256 com_backpatch(c, except_anchor);
2257 for (i = 3;
2258 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
2259 i += 3) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00002260 /* except_clause: 'except' [expr [',' var]] */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002261 if (except_anchor == 0) {
Guido van Rossum452a9831996-09-17 14:32:04 +00002262 com_error(c, SyntaxError,
2263 "default 'except:' must be last");
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002264 break;
2265 }
2266 except_anchor = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00002267 com_push(c, 3); /* tb, val, exc pushed by exception */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002268 com_addoparg(c, SET_LINENO, ch->n_lineno);
2269 if (NCH(ch) > 1) {
2270 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002271 com_push(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002272 com_node(c, CHILD(ch, 1));
2273 com_addoparg(c, COMPARE_OP, EXC_MATCH);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002274 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002275 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
2276 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002277 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002278 }
2279 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002280 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002281 if (NCH(ch) > 3)
Guido van Rossum8861b741996-07-30 16:49:37 +00002282 com_assign(c, CHILD(ch, 3), OP_ASSIGN);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002283 else {
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002284 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002285 com_pop(c, 1);
2286 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002287 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002288 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002289 com_node(c, CHILD(n, i+2));
2290 com_addfwref(c, JUMP_FORWARD, &end_anchor);
2291 if (except_anchor) {
2292 com_backpatch(c, except_anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002293 /* We come in with [tb, val, exc, 0] on the
2294 stack; one pop and it's the same as
2295 expected at the start of the loop */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002296 com_addbyte(c, POP_TOP);
2297 }
2298 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002299 /* We actually come in here with [tb, val, exc] but the
2300 END_FINALLY will zap those and jump around.
2301 The c_stacklevel does not reflect them so we need not pop
2302 anything. */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002303 com_addbyte(c, END_FINALLY);
2304 com_backpatch(c, else_anchor);
2305 if (i < NCH(n))
2306 com_node(c, CHILD(n, i+2));
2307 com_backpatch(c, end_anchor);
2308}
2309
2310static void
2311com_try_finally(c, n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002312 struct compiling *c;
2313 node *n;
2314{
2315 int finally_anchor = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002316 node *ch;
Guido van Rossum94fb82e1992-04-05 14:24:50 +00002317
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002318 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
2319 block_push(c, SETUP_FINALLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002320 com_node(c, CHILD(n, 2));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002321 com_addbyte(c, POP_BLOCK);
2322 block_pop(c, SETUP_FINALLY);
2323 block_push(c, END_FINALLY);
2324 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002325 /* While the generated code pushes only one item,
2326 the try-finally handling can enter here with
2327 up to three items. OK, here are the details:
2328 3 for an exception, 2 for RETURN, 1 for BREAK. */
2329 com_push(c, 3);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002330 com_backpatch(c, finally_anchor);
2331 ch = CHILD(n, NCH(n)-1);
2332 com_addoparg(c, SET_LINENO, ch->n_lineno);
2333 com_node(c, ch);
2334 com_addbyte(c, END_FINALLY);
2335 block_pop(c, END_FINALLY);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002336 com_pop(c, 3); /* Matches the com_push above */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002337}
2338
2339static void
2340com_try_stmt(c, n)
2341 struct compiling *c;
2342 node *n;
2343{
2344 REQ(n, try_stmt);
2345 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
2346 | 'try' ':' suite 'finally' ':' suite */
2347 if (TYPE(CHILD(n, 3)) != except_clause)
2348 com_try_finally(c, n);
2349 else
2350 com_try_except(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002351}
2352
Guido van Rossum8b993a91997-01-17 21:04:03 +00002353static node *
2354get_rawdocstring(n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002355 node *n;
2356{
Guido van Rossum164d4ff1995-01-26 00:40:09 +00002357 int i;
2358
Guido van Rossum8b993a91997-01-17 21:04:03 +00002359 /* Label to avoid tail recursion */
2360 next:
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002361 switch (TYPE(n)) {
2362
2363 case suite:
Guido van Rossum8b993a91997-01-17 21:04:03 +00002364 if (NCH(n) == 1) {
2365 n = CHILD(n, 0);
2366 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002367 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002368 /* Fall through */
Guido van Rossum164d4ff1995-01-26 00:40:09 +00002369 case file_input:
2370 for (i = 0; i < NCH(n); i++) {
2371 node *ch = CHILD(n, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002372 if (TYPE(ch) == stmt) {
2373 n = ch;
2374 goto next;
2375 }
Guido van Rossum164d4ff1995-01-26 00:40:09 +00002376 }
2377 break;
2378
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002379 case stmt:
2380 case simple_stmt:
2381 case small_stmt:
Guido van Rossum8b993a91997-01-17 21:04:03 +00002382 n = CHILD(n, 0);
2383 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002384
2385 case expr_stmt:
2386 case testlist:
2387 case test:
2388 case and_test:
2389 case not_test:
2390 case comparison:
2391 case expr:
2392 case xor_expr:
2393 case and_expr:
2394 case shift_expr:
2395 case arith_expr:
2396 case term:
2397 case factor:
Guido van Rossum50564e81996-01-12 01:13:16 +00002398 case power:
Guido van Rossum8b993a91997-01-17 21:04:03 +00002399 if (NCH(n) == 1) {
2400 n = CHILD(n, 0);
2401 goto next;
2402 }
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002403 break;
2404
2405 case atom:
2406 if (TYPE(CHILD(n, 0)) == STRING)
Guido van Rossum8b993a91997-01-17 21:04:03 +00002407 return n;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002408 break;
2409
2410 }
2411 return NULL;
2412}
2413
Guido van Rossum8b993a91997-01-17 21:04:03 +00002414static object *
2415get_docstring(n)
2416 node *n;
2417{
2418 n = get_rawdocstring(n);
2419 if (n == NULL)
2420 return NULL;
2421 return parsestrplus(n);
2422}
2423
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002424static void
2425com_suite(c, n)
2426 struct compiling *c;
2427 node *n;
2428{
2429 REQ(n, suite);
2430 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
2431 if (NCH(n) == 1) {
2432 com_node(c, CHILD(n, 0));
2433 }
2434 else {
2435 int i;
2436 for (i = 0; i < NCH(n); i++) {
2437 node *ch = CHILD(n, i);
2438 if (TYPE(ch) == stmt)
2439 com_node(c, ch);
2440 }
2441 }
2442}
2443
Guido van Rossumf1aeab71992-03-27 17:28:26 +00002444/* ARGSUSED */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002445static void
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002446com_continue_stmt(c, n)
2447 struct compiling *c;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00002448 node *n; /* Not used, but passed for consistency */
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002449{
2450 int i = c->c_nblocks;
2451 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
2452 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2453 }
2454 else {
Guido van Rossum452a9831996-09-17 14:32:04 +00002455 com_error(c, SyntaxError, "'continue' not properly in loop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002456 }
2457 /* XXX Could allow it inside a 'finally' clause
2458 XXX if we could pop the exception still on the stack */
2459}
2460
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002461static int
Guido van Rossum681d79a1995-07-18 14:51:37 +00002462com_argdefs(c, n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002463 struct compiling *c;
2464 node *n;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002465{
Guido van Rossum681d79a1995-07-18 14:51:37 +00002466 int i, nch, nargs, ndefs;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002467 if (TYPE(n) == lambdef) {
2468 /* lambdef: 'lambda' [varargslist] ':' test */
2469 n = CHILD(n, 1);
2470 }
2471 else {
2472 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
2473 n = CHILD(n, 2);
2474 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
2475 n = CHILD(n, 1);
2476 }
2477 if (TYPE(n) != varargslist)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002478 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002479 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00002480 (fpdef ['=' test] ',')* '*' ....... |
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002481 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
2482 nch = NCH(n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002483 nargs = 0;
2484 ndefs = 0;
2485 for (i = 0; i < nch; i++) {
2486 int t;
Guido van Rossum50564e81996-01-12 01:13:16 +00002487 if (TYPE(CHILD(n, i)) == STAR || TYPE(CHILD(n, i)) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00002488 break;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002489 nargs++;
2490 i++;
2491 if (i >= nch)
Guido van Rossuma1e7e621995-09-18 21:44:04 +00002492 t = RPAR; /* Anything except EQUAL or COMMA */
2493 else
2494 t = TYPE(CHILD(n, i));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002495 if (t == EQUAL) {
2496 i++;
2497 ndefs++;
2498 com_node(c, CHILD(n, i));
2499 i++;
2500 if (i >= nch)
2501 break;
2502 t = TYPE(CHILD(n, i));
2503 }
2504 else {
2505 /* Treat "(a=1, b)" as "(a=1, b=None)" */
2506 if (ndefs) {
2507 com_addoparg(c, LOAD_CONST,
2508 com_addconst(c, None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002509 com_push(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002510 ndefs++;
2511 }
2512 }
2513 if (t != COMMA)
2514 break;
2515 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002516 return ndefs;
2517}
2518
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002519static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002520com_funcdef(c, n)
2521 struct compiling *c;
2522 node *n;
2523{
2524 object *v;
2525 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum8ff077b1996-08-24 06:21:31 +00002526 v = (object *)icompile(n, c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002527 if (v == NULL)
2528 c->c_errors++;
2529 else {
2530 int i = com_addconst(c, v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002531 int ndefs = com_argdefs(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002532 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002533 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002534 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002535 com_pop(c, ndefs);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002536 com_addopname(c, STORE_NAME, CHILD(n, 1));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002537 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002538 DECREF(v);
2539 }
2540}
2541
2542static void
Guido van Rossum25831651993-05-19 14:50:45 +00002543com_bases(c, n)
Guido van Rossumc5e96291991-12-10 13:53:51 +00002544 struct compiling *c;
2545 node *n;
2546{
Guido van Rossumf1aeab71992-03-27 17:28:26 +00002547 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00002548 REQ(n, testlist);
2549 /* testlist: test (',' test)* [','] */
2550 for (i = 0; i < NCH(n); i += 2)
2551 com_node(c, CHILD(n, i));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002552 i = (NCH(n)+1) / 2;
2553 com_addoparg(c, BUILD_TUPLE, i);
2554 com_pop(c, i-1);
Guido van Rossumc5e96291991-12-10 13:53:51 +00002555}
2556
2557static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002558com_classdef(c, n)
2559 struct compiling *c;
2560 node *n;
2561{
Guido van Rossum25831651993-05-19 14:50:45 +00002562 int i;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002563 object *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002564 REQ(n, classdef);
Guido van Rossum25831651993-05-19 14:50:45 +00002565 /* classdef: class NAME ['(' testlist ')'] ':' suite */
2566 if ((v = newstringobject(STR(CHILD(n, 1)))) == NULL) {
2567 c->c_errors++;
2568 return;
2569 }
2570 /* Push the class name on the stack */
2571 i = com_addconst(c, v);
2572 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002573 com_push(c, 1);
Guido van Rossum25831651993-05-19 14:50:45 +00002574 DECREF(v);
2575 /* Push the tuple of base classes on the stack */
Guido van Rossum8b993a91997-01-17 21:04:03 +00002576 if (TYPE(CHILD(n, 2)) != LPAR) {
Guido van Rossumc5e96291991-12-10 13:53:51 +00002577 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002578 com_push(c, 1);
2579 }
Guido van Rossum25831651993-05-19 14:50:45 +00002580 else
2581 com_bases(c, CHILD(n, 3));
Guido van Rossum8ff077b1996-08-24 06:21:31 +00002582 v = (object *)icompile(n, c);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002583 if (v == NULL)
2584 c->c_errors++;
2585 else {
Guido van Rossum25831651993-05-19 14:50:45 +00002586 i = com_addconst(c, v);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002587 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002588 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002589 com_addoparg(c, MAKE_FUNCTION, 0);
2590 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002591 com_addbyte(c, BUILD_CLASS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002592 com_pop(c, 2);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002593 com_addopname(c, STORE_NAME, CHILD(n, 1));
2594 DECREF(v);
2595 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002596}
2597
2598static void
2599com_node(c, n)
2600 struct compiling *c;
2601 node *n;
2602{
2603 switch (TYPE(n)) {
2604
2605 /* Definition nodes */
2606
2607 case funcdef:
2608 com_funcdef(c, n);
2609 break;
2610 case classdef:
2611 com_classdef(c, n);
2612 break;
2613
2614 /* Trivial parse tree nodes */
2615
2616 case stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002617 case small_stmt:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002618 case flow_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002619 com_node(c, CHILD(n, 0));
2620 break;
2621
2622 case simple_stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002623 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
2624 com_addoparg(c, SET_LINENO, n->n_lineno);
2625 {
2626 int i;
2627 for (i = 0; i < NCH(n)-1; i += 2)
2628 com_node(c, CHILD(n, i));
2629 }
2630 break;
2631
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002632 case compound_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002633 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002634 com_node(c, CHILD(n, 0));
2635 break;
2636
2637 /* Statement nodes */
2638
2639 case expr_stmt:
2640 com_expr_stmt(c, n);
2641 break;
2642 case print_stmt:
2643 com_print_stmt(c, n);
2644 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002645 case del_stmt: /* 'del' exprlist */
Guido van Rossum8861b741996-07-30 16:49:37 +00002646 com_assign(c, CHILD(n, 1), OP_DELETE);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002647 break;
2648 case pass_stmt:
2649 break;
2650 case break_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002651 if (c->c_loops == 0) {
Guido van Rossum452a9831996-09-17 14:32:04 +00002652 com_error(c, SyntaxError, "'break' outside loop");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002653 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002654 com_addbyte(c, BREAK_LOOP);
2655 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002656 case continue_stmt:
2657 com_continue_stmt(c, n);
2658 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002659 case return_stmt:
2660 com_return_stmt(c, n);
2661 break;
2662 case raise_stmt:
2663 com_raise_stmt(c, n);
2664 break;
2665 case import_stmt:
2666 com_import_stmt(c, n);
2667 break;
Guido van Rossumc5e96291991-12-10 13:53:51 +00002668 case global_stmt:
2669 com_global_stmt(c, n);
2670 break;
Guido van Rossum0dfcf751996-08-12 22:00:53 +00002671#ifdef SUPPORT_OBSOLETE_ACCESS
Guido van Rossum25831651993-05-19 14:50:45 +00002672 case access_stmt:
2673 com_access_stmt(c, n);
2674 break;
Guido van Rossum0dfcf751996-08-12 22:00:53 +00002675#endif
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002676 case exec_stmt:
2677 com_exec_stmt(c, n);
2678 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002679 case if_stmt:
2680 com_if_stmt(c, n);
2681 break;
2682 case while_stmt:
2683 com_while_stmt(c, n);
2684 break;
2685 case for_stmt:
2686 com_for_stmt(c, n);
2687 break;
2688 case try_stmt:
2689 com_try_stmt(c, n);
2690 break;
2691 case suite:
2692 com_suite(c, n);
2693 break;
2694
2695 /* Expression nodes */
2696
2697 case testlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00002698 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002699 break;
2700 case test:
2701 com_test(c, n);
2702 break;
2703 case and_test:
2704 com_and_test(c, n);
2705 break;
2706 case not_test:
2707 com_not_test(c, n);
2708 break;
2709 case comparison:
2710 com_comparison(c, n);
2711 break;
2712 case exprlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00002713 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002714 break;
2715 case expr:
2716 com_expr(c, n);
2717 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +00002718 case xor_expr:
2719 com_xor_expr(c, n);
2720 break;
2721 case and_expr:
2722 com_and_expr(c, n);
2723 break;
2724 case shift_expr:
2725 com_shift_expr(c, n);
2726 break;
2727 case arith_expr:
2728 com_arith_expr(c, n);
2729 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002730 case term:
2731 com_term(c, n);
2732 break;
2733 case factor:
2734 com_factor(c, n);
2735 break;
Guido van Rossum50564e81996-01-12 01:13:16 +00002736 case power:
2737 com_power(c, n);
2738 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002739 case atom:
2740 com_atom(c, n);
2741 break;
2742
2743 default:
Guido van Rossum8b993a91997-01-17 21:04:03 +00002744 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
Guido van Rossum452a9831996-09-17 14:32:04 +00002745 com_error(c, SystemError, "com_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002746 }
2747}
2748
2749static void com_fplist PROTO((struct compiling *, node *));
2750
2751static void
2752com_fpdef(c, n)
2753 struct compiling *c;
2754 node *n;
2755{
2756 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
2757 if (TYPE(CHILD(n, 0)) == LPAR)
2758 com_fplist(c, CHILD(n, 1));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002759 else {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002760 com_addoparg(c, STORE_FAST, com_newlocal(c, STR(CHILD(n, 0))));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002761 com_pop(c, 1);
2762 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002763}
2764
2765static void
2766com_fplist(c, n)
2767 struct compiling *c;
2768 node *n;
2769{
Guido van Rossuma9df32a1991-12-31 13:13:35 +00002770 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002771 if (NCH(n) == 1) {
2772 com_fpdef(c, CHILD(n, 0));
2773 }
2774 else {
Guido van Rossum8b993a91997-01-17 21:04:03 +00002775 int i = (NCH(n)+1)/2;
2776 com_addoparg(c, UNPACK_TUPLE, i);
2777 com_push(c, i-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002778 for (i = 0; i < NCH(n); i += 2)
2779 com_fpdef(c, CHILD(n, i));
2780 }
2781}
2782
2783static void
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002784com_arglist(c, n)
2785 struct compiling *c;
2786 node *n;
2787{
Guido van Rossum681d79a1995-07-18 14:51:37 +00002788 int nch, i;
2789 int complex = 0;
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002790 REQ(n, varargslist);
Guido van Rossumacbefef1992-01-19 16:33:51 +00002791 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00002792 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002793 nch = NCH(n);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002794 /* Enter all arguments in table of locals */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002795 for (i = 0; i < nch; i++) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002796 node *ch = CHILD(n, i);
2797 node *fp;
2798 char *name;
Guido van Rossum50564e81996-01-12 01:13:16 +00002799 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00002800 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002801 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
2802 fp = CHILD(ch, 0);
2803 if (TYPE(fp) == NAME)
2804 name = STR(fp);
2805 else {
2806 name = "";
Guido van Rossum530956d1996-07-21 02:27:43 +00002807 complex = 1;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002808 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002809 com_newlocal(c, name);
2810 c->c_argcount++;
2811 if (++i >= nch)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002812 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002813 ch = CHILD(n, i);
2814 if (TYPE(ch) == EQUAL)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002815 i += 2;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002816 else
2817 REQ(ch, COMMA);
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002818 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002819 /* Handle *arguments */
2820 if (i < nch) {
2821 node *ch;
2822 ch = CHILD(n, i);
Guido van Rossum50564e81996-01-12 01:13:16 +00002823 if (TYPE(ch) != DOUBLESTAR) {
2824 REQ(ch, STAR);
2825 ch = CHILD(n, i+1);
2826 if (TYPE(ch) == NAME) {
2827 c->c_flags |= CO_VARARGS;
2828 i += 3;
2829 com_newlocal(c, STR(ch));
2830 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002831 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002832 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002833 /* Handle **keywords */
2834 if (i < nch) {
2835 node *ch;
2836 ch = CHILD(n, i);
Guido van Rossum50564e81996-01-12 01:13:16 +00002837 if (TYPE(ch) != DOUBLESTAR) {
2838 REQ(ch, STAR);
2839 ch = CHILD(n, i+1);
2840 REQ(ch, STAR);
2841 ch = CHILD(n, i+2);
2842 }
2843 else
2844 ch = CHILD(n, i+1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002845 REQ(ch, NAME);
2846 c->c_flags |= CO_VARKEYWORDS;
2847 com_newlocal(c, STR(ch));
2848 }
2849 if (complex) {
2850 /* Generate code for complex arguments only after
2851 having counted the simple arguments */
2852 int ilocal = 0;
2853 for (i = 0; i < nch; i++) {
2854 node *ch = CHILD(n, i);
2855 node *fp;
Guido van Rossum50564e81996-01-12 01:13:16 +00002856 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002857 break;
2858 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
2859 fp = CHILD(ch, 0);
2860 if (TYPE(fp) != NAME) {
2861 com_addoparg(c, LOAD_FAST, ilocal);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002862 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002863 com_fpdef(c, ch);
2864 }
2865 ilocal++;
2866 if (++i >= nch)
2867 break;
2868 ch = CHILD(n, i);
2869 if (TYPE(ch) == EQUAL)
2870 i += 2;
2871 else
2872 REQ(ch, COMMA);
2873 }
2874 }
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002875}
2876
2877static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002878com_file_input(c, n)
2879 struct compiling *c;
2880 node *n;
2881{
2882 int i;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002883 object *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002884 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002885 doc = get_docstring(n);
2886 if (doc != NULL) {
2887 int i = com_addconst(c, doc);
2888 DECREF(doc);
2889 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002890 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002891 com_addopnamestr(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00002892 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002893 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002894 for (i = 0; i < NCH(n); i++) {
2895 node *ch = CHILD(n, i);
2896 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
2897 com_node(c, ch);
2898 }
2899}
2900
2901/* Top-level compile-node interface */
2902
2903static void
2904compile_funcdef(c, n)
2905 struct compiling *c;
2906 node *n;
2907{
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002908 object *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002909 node *ch;
2910 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum9bfef441993-03-29 10:43:31 +00002911 c->c_name = STR(CHILD(n, 1));
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002912 doc = get_docstring(CHILD(n, 4));
2913 if (doc != NULL) {
2914 (void) com_addconst(c, doc);
2915 DECREF(doc);
2916 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002917 else
2918 (void) com_addconst(c, None); /* No docstring */
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002919 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
2920 ch = CHILD(ch, 1); /* ')' | varargslist */
Guido van Rossum681d79a1995-07-18 14:51:37 +00002921 if (TYPE(ch) == varargslist)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002922 com_arglist(c, ch);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002923 c->c_infunction = 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002924 com_node(c, CHILD(n, 4));
Guido van Rossum3f5da241990-12-20 15:06:42 +00002925 c->c_infunction = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002926 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002927 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002928 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002929 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002930}
2931
2932static void
Guido van Rossum590baa41993-11-30 13:40:46 +00002933compile_lambdef(c, n)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002934 struct compiling *c;
2935 node *n;
2936{
Guido van Rossum590baa41993-11-30 13:40:46 +00002937 node *ch;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002938 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
Guido van Rossum363ac7d1994-11-10 22:40:34 +00002939 c->c_name = "<lambda>";
Guido van Rossum590baa41993-11-30 13:40:46 +00002940
2941 ch = CHILD(n, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002942 (void) com_addconst(c, None); /* No docstring */
2943 if (TYPE(ch) == varargslist) {
Guido van Rossum590baa41993-11-30 13:40:46 +00002944 com_arglist(c, ch);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002945 ch = CHILD(n, 3);
Guido van Rossum590baa41993-11-30 13:40:46 +00002946 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002947 else
2948 ch = CHILD(n, 2);
2949 com_node(c, ch);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002950 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002951 com_pop(c, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002952}
2953
2954static void
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002955compile_classdef(c, n)
2956 struct compiling *c;
2957 node *n;
2958{
2959 node *ch;
2960 object *doc;
2961 REQ(n, classdef);
2962 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
2963 c->c_name = STR(CHILD(n, 1));
Guido van Rossum8ff077b1996-08-24 06:21:31 +00002964#ifdef PRIVATE_NAME_MANGLING
2965 c->c_private = c->c_name;
2966#endif
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002967 ch = CHILD(n, NCH(n)-1); /* The suite */
2968 doc = get_docstring(ch);
2969 if (doc != NULL) {
2970 int i = com_addconst(c, doc);
2971 DECREF(doc);
2972 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002973 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002974 com_addopnamestr(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00002975 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002976 }
2977 else
2978 (void) com_addconst(c, None);
2979 com_node(c, ch);
2980 com_addbyte(c, LOAD_LOCALS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002981 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002982 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002983 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002984}
2985
2986static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002987compile_node(c, n)
2988 struct compiling *c;
2989 node *n;
2990{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002991 com_addoparg(c, SET_LINENO, n->n_lineno);
2992
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002993 switch (TYPE(n)) {
2994
Guido van Rossum4c417781991-01-21 16:09:22 +00002995 case single_input: /* One interactive command */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002996 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002997 c->c_interactive++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002998 n = CHILD(n, 0);
2999 if (TYPE(n) != NEWLINE)
3000 com_node(c, n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003001 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003002 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003003 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003004 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003005 c->c_interactive--;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003006 break;
3007
Guido van Rossum4c417781991-01-21 16:09:22 +00003008 case file_input: /* A whole file, or built-in function exec() */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003009 com_file_input(c, n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003010 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003011 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003012 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003013 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003014 break;
3015
Guido van Rossum590baa41993-11-30 13:40:46 +00003016 case eval_input: /* Built-in function input() */
Guido van Rossum4c417781991-01-21 16:09:22 +00003017 com_node(c, CHILD(n, 0));
3018 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003019 com_pop(c, 1);
Guido van Rossum4c417781991-01-21 16:09:22 +00003020 break;
3021
Guido van Rossum590baa41993-11-30 13:40:46 +00003022 case lambdef: /* anonymous function definition */
3023 compile_lambdef(c, n);
3024 break;
3025
Guido van Rossum4c417781991-01-21 16:09:22 +00003026 case funcdef: /* A function definition */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003027 compile_funcdef(c, n);
3028 break;
3029
Guido van Rossum4c417781991-01-21 16:09:22 +00003030 case classdef: /* A class definition */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003031 compile_classdef(c, n);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003032 break;
3033
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003034 default:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003035 /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */
Guido van Rossum452a9831996-09-17 14:32:04 +00003036 com_error(c, SystemError,
3037 "compile_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003038 }
3039}
3040
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003041/* Optimization for local variables in functions (and *only* functions).
Guido van Rossum282914b1991-04-04 10:42:56 +00003042
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003043 This replaces all LOAD_NAME, STORE_NAME and DELETE_NAME
3044 instructions that refer to local variables with LOAD_FAST etc.
3045 The latter instructions are much faster because they don't need to
3046 look up the variable name in a dictionary.
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003047
Guido van Rossum681d79a1995-07-18 14:51:37 +00003048 To find all local variables, we check all STORE_NAME, IMPORT_FROM
3049 and DELETE_NAME instructions. This yields all local variables,
3050 function definitions, class definitions and import statements.
3051 Argument names have already been entered into the list by the
3052 special processing for the argument list.
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003053
3054 All remaining LOAD_NAME instructions must refer to non-local (global
3055 or builtin) variables, so are replaced by LOAD_GLOBAL.
3056
3057 There are two problems: 'from foo import *' and 'exec' may introduce
3058 local variables that we can't know while compiling. If this is the
Guido van Rossum681d79a1995-07-18 14:51:37 +00003059 case, we can still optimize bona fide locals (since those
3060 statements will be surrounded by fast_2_locals() and
3061 locals_2_fast()), but we can't change LOAD_NAME to LOAD_GLOBAL.
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003062
Guido van Rossum681d79a1995-07-18 14:51:37 +00003063 NB: this modifies the string object c->c_code! */
Guido van Rossum282914b1991-04-04 10:42:56 +00003064
3065static void
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003066optimize(c)
3067 struct compiling *c;
Guido van Rossum282914b1991-04-04 10:42:56 +00003068{
Guido van Rossum0a697f61991-04-16 08:39:12 +00003069 unsigned char *next_instr, *cur_instr;
Guido van Rossum282914b1991-04-04 10:42:56 +00003070 int opcode;
Guido van Rossum80bb9651996-12-05 23:27:02 +00003071 int oparg = 0;
Guido van Rossum282914b1991-04-04 10:42:56 +00003072 object *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003073 object *error_type, *error_value, *error_traceback;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003074
Guido van Rossum282914b1991-04-04 10:42:56 +00003075#define NEXTOP() (*next_instr++)
3076#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
3077#define GETITEM(v, i) (getlistitem((v), (i)))
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003078#define GETNAMEOBJ(i) (GETITEM(c->c_names, (i)))
3079
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003080 err_fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003081
3082 c->c_flags |= CO_OPTIMIZED;
Guido van Rossum282914b1991-04-04 10:42:56 +00003083
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003084 next_instr = (unsigned char *) getstringvalue(c->c_code);
Guido van Rossum282914b1991-04-04 10:42:56 +00003085 for (;;) {
3086 opcode = NEXTOP();
3087 if (opcode == STOP_CODE)
3088 break;
3089 if (HAS_ARG(opcode))
3090 oparg = NEXTARG();
Guido van Rossum681d79a1995-07-18 14:51:37 +00003091 switch (opcode) {
3092 case STORE_NAME:
3093 case DELETE_NAME:
3094 case IMPORT_FROM:
3095 com_addlocal_o(c, GETNAMEOBJ(oparg));
3096 break;
3097 case EXEC_STMT:
3098 c->c_flags &= ~CO_OPTIMIZED;
3099 break;
Guido van Rossum282914b1991-04-04 10:42:56 +00003100 }
3101 }
3102
Guido van Rossum681d79a1995-07-18 14:51:37 +00003103 if (dictlookup(c->c_locals, "*") != NULL)
3104 c->c_flags &= ~CO_OPTIMIZED;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003105
3106 next_instr = (unsigned char *) getstringvalue(c->c_code);
Guido van Rossum282914b1991-04-04 10:42:56 +00003107 for (;;) {
3108 cur_instr = next_instr;
3109 opcode = NEXTOP();
3110 if (opcode == STOP_CODE)
3111 break;
3112 if (HAS_ARG(opcode))
3113 oparg = NEXTARG();
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003114 if (opcode == LOAD_NAME ||
3115 opcode == STORE_NAME ||
3116 opcode == DELETE_NAME) {
3117 object *v;
3118 int i;
Guido van Rossum282914b1991-04-04 10:42:56 +00003119 name = GETNAMEOBJ(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003120 v = dict2lookup(c->c_locals, name);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003121 if (v == NULL) {
Guido van Rossum83163251991-08-16 08:58:43 +00003122 err_clear();
Guido van Rossum681d79a1995-07-18 14:51:37 +00003123 if (opcode == LOAD_NAME &&
3124 (c->c_flags&CO_OPTIMIZED))
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003125 cur_instr[0] = LOAD_GLOBAL;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003126 continue;
Guido van Rossum83163251991-08-16 08:58:43 +00003127 }
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003128 i = getintvalue(v);
3129 switch (opcode) {
3130 case LOAD_NAME: cur_instr[0] = LOAD_FAST; break;
3131 case STORE_NAME: cur_instr[0] = STORE_FAST; break;
3132 case DELETE_NAME: cur_instr[0] = DELETE_FAST; break;
3133 }
3134 cur_instr[1] = i & 0xff;
3135 cur_instr[2] = (i>>8) & 0xff;
Guido van Rossum282914b1991-04-04 10:42:56 +00003136 }
3137 }
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003138
Guido van Rossum681d79a1995-07-18 14:51:37 +00003139 if (c->c_errors == 0)
3140 err_restore(error_type, error_value, error_traceback);
Guido van Rossum282914b1991-04-04 10:42:56 +00003141}
3142
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003143codeobject *
Guido van Rossum590baa41993-11-30 13:40:46 +00003144compile(n, filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003145 node *n;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003146 char *filename;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003147{
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003148 return jcompile(n, filename, NULL);
3149}
3150
3151static codeobject *
3152icompile(n, base)
3153 node *n;
3154 struct compiling *base;
3155{
3156 return jcompile(n, base->c_filename, base);
3157}
3158
3159static codeobject *
3160jcompile(n, filename, base)
3161 node *n;
3162 char *filename;
3163 struct compiling *base;
3164{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003165 struct compiling sc;
3166 codeobject *co;
Guido van Rossum590baa41993-11-30 13:40:46 +00003167 if (!com_init(&sc, filename))
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003168 return NULL;
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003169#ifdef PRIVATE_NAME_MANGLING
3170 if (base)
3171 sc.c_private = base->c_private;
3172 else
3173 sc.c_private = NULL;
3174#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003175 compile_node(&sc, n);
3176 com_done(&sc);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003177 if ((TYPE(n) == funcdef || TYPE(n) == lambdef) && sc.c_errors == 0) {
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00003178 optimize(&sc);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003179 sc.c_flags |= CO_NEWLOCALS;
3180 }
3181 else if (TYPE(n) == classdef)
3182 sc.c_flags |= CO_NEWLOCALS;
Guido van Rossum9bfef441993-03-29 10:43:31 +00003183 co = NULL;
3184 if (sc.c_errors == 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00003185 object *consts, *names, *varnames, *filename, *name;
3186 consts = listtuple(sc.c_consts);
3187 names = listtuple(sc.c_names);
3188 varnames = listtuple(sc.c_varnames);
3189 filename = newstringobject(sc.c_filename);
3190 name = newstringobject(sc.c_name);
3191 if (!err_occurred())
3192 co = newcodeobject(sc.c_argcount,
3193 sc.c_nlocals,
Guido van Rossum8b993a91997-01-17 21:04:03 +00003194 sc.c_maxstacklevel,
Guido van Rossum681d79a1995-07-18 14:51:37 +00003195 sc.c_flags,
3196 sc.c_code,
3197 consts,
3198 names,
3199 varnames,
3200 filename,
3201 name);
3202 XDECREF(consts);
3203 XDECREF(names);
3204 XDECREF(varnames);
3205 XDECREF(filename);
3206 XDECREF(name);
Guido van Rossuma082ce41991-06-04 19:41:56 +00003207 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003208 com_free(&sc);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003209 return co;
3210}