blob: 3a1d3f68cf5900e5e08ecb66599f3a3c40e49a45 [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
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum10dc2e81990-11-18 17:27:39 +000025/* Compile an expression node to intermediate code */
26
Guido van Rossum3f5da241990-12-20 15:06:42 +000027/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +000028 XXX Compute maximum needed stack sizes while compiling;
29 XXX then frame object can be one malloc and no stack checks are needed
30 XXX add __doc__ attribute == co_doc to code object attributes
31 XXX don't execute doc string
Guido van Rossum3f5da241990-12-20 15:06:42 +000032 XXX Generate simple jump for break/return outside 'try...finally'
Guido van Rossum681d79a1995-07-18 14:51:37 +000033 XXX get rid of SET_LINENO instructions, use JAR's table trick
34 XXX (need an option to put them back in, for debugger!)
35 XXX other JAR tricks?
Guido van Rossum3f5da241990-12-20 15:06:42 +000036*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +000037
Guido van Rossum3f5da241990-12-20 15:06:42 +000038#include "allobjects.h"
39
Guido van Rossum10dc2e81990-11-18 17:27:39 +000040#include "node.h"
41#include "token.h"
42#include "graminit.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000043#include "compile.h"
44#include "opcode.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000045#include "structmember.h"
46
47#include <ctype.h>
Guido van Rossumf1dc5661993-07-05 10:31:29 +000048#include <errno.h>
Guido van Rossum282914b1991-04-04 10:42:56 +000049
Guido van Rossum3f5da241990-12-20 15:06:42 +000050#define OFF(x) offsetof(codeobject, x)
51
52static struct memberlist code_memberlist[] = {
Guido van Rossum681d79a1995-07-18 14:51:37 +000053 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
54 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
55 {"co_flags", T_INT, OFF(co_flags), READONLY},
Guido van Rossum39d942d1992-01-12 02:30:05 +000056 {"co_code", T_OBJECT, OFF(co_code), READONLY},
57 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
58 {"co_names", T_OBJECT, OFF(co_names), READONLY},
Guido van Rossum681d79a1995-07-18 14:51:37 +000059 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
Guido van Rossum39d942d1992-01-12 02:30:05 +000060 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
Guido van Rossum9bfef441993-03-29 10:43:31 +000061 {"co_name", T_OBJECT, OFF(co_name), READONLY},
Guido van Rossum3f5da241990-12-20 15:06:42 +000062 {NULL} /* Sentinel */
63};
64
65static object *
66code_getattr(co, name)
67 codeobject *co;
68 char *name;
69{
70 return getmember((char *)co, code_memberlist, name);
71}
Guido van Rossum10dc2e81990-11-18 17:27:39 +000072
73static void
Guido van Rossum3f5da241990-12-20 15:06:42 +000074code_dealloc(co)
75 codeobject *co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +000076{
Guido van Rossum3f5da241990-12-20 15:06:42 +000077 XDECREF(co->co_code);
78 XDECREF(co->co_consts);
79 XDECREF(co->co_names);
80 XDECREF(co->co_filename);
Guido van Rossum9bfef441993-03-29 10:43:31 +000081 XDECREF(co->co_name);
Guido van Rossum681d79a1995-07-18 14:51:37 +000082 XDECREF(co->co_varnames);
Guido van Rossum3f5da241990-12-20 15:06:42 +000083 DEL(co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +000084}
85
Guido van Rossum2dff9911992-09-03 20:50:59 +000086static object *
87code_repr(co)
88 codeobject *co;
89{
90 char buf[500];
91 int lineno = -1;
92 char *p = GETSTRINGVALUE(co->co_code);
93 char *filename = "???";
Guido van Rossum9bfef441993-03-29 10:43:31 +000094 char *name = "???";
Guido van Rossum2dff9911992-09-03 20:50:59 +000095 if (*p == SET_LINENO)
96 lineno = (p[1] & 0xff) | ((p[2] & 0xff) << 8);
97 if (co->co_filename && is_stringobject(co->co_filename))
98 filename = getstringvalue(co->co_filename);
Guido van Rossum9bfef441993-03-29 10:43:31 +000099 if (co->co_name && is_stringobject(co->co_name))
100 name = getstringvalue(co->co_name);
101 sprintf(buf, "<code object %.100s at %lx, file \"%.300s\", line %d>",
102 name, (long)co, filename, lineno);
Guido van Rossum2dff9911992-09-03 20:50:59 +0000103 return newstringobject(buf);
104}
105
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000106static int
107code_compare(co, cp)
108 codeobject *co, *cp;
109{
110 int cmp;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000111 cmp = cp->co_argcount - cp->co_argcount;
112 if (cmp) return cmp;
113 cmp = cp->co_nlocals - cp->co_nlocals;
114 if (cmp) return cmp;
115 cmp = cp->co_flags - cp->co_flags;
116 if (cmp) return cmp;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000117 cmp = cmpobject((object *)co->co_code, (object *)cp->co_code);
118 if (cmp) return cmp;
119 cmp = cmpobject(co->co_consts, cp->co_consts);
120 if (cmp) return cmp;
121 cmp = cmpobject(co->co_names, cp->co_names);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000122 if (cmp) return cmp;
123 cmp = cmpobject(co->co_varnames, cp->co_varnames);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000124 return cmp;
125}
126
127static long
128code_hash(co)
129 codeobject *co;
130{
Guido van Rossum681d79a1995-07-18 14:51:37 +0000131 long h, h1, h2, h3, h4;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000132 h1 = hashobject((object *)co->co_code);
133 if (h1 == -1) return -1;
134 h2 = hashobject(co->co_consts);
135 if (h2 == -1) return -1;
136 h3 = hashobject(co->co_names);
137 if (h3 == -1) return -1;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000138 h4 = hashobject(co->co_varnames);
139 if (h4 == -1) return -1;
140 h = h1 ^ h2 ^ h3 ^ h4 ^
141 co->co_argcount ^ co->co_nlocals ^ co->co_flags;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000142 if (h == -1) h = -2;
143 return h;
144}
145
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000146typeobject Codetype = {
147 OB_HEAD_INIT(&Typetype)
148 0,
149 "code",
150 sizeof(codeobject),
151 0,
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000152 (destructor)code_dealloc, /*tp_dealloc*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000153 0, /*tp_print*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000154 (getattrfunc)code_getattr, /*tp_getattr*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000155 0, /*tp_setattr*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000156 (cmpfunc)code_compare, /*tp_compare*/
157 (reprfunc)code_repr, /*tp_repr*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000158 0, /*tp_as_number*/
159 0, /*tp_as_sequence*/
160 0, /*tp_as_mapping*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000161 (hashfunc)code_hash, /*tp_hash*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000162};
163
Guido van Rossuma082ce41991-06-04 19:41:56 +0000164codeobject *
Guido van Rossum681d79a1995-07-18 14:51:37 +0000165newcodeobject(argcount, nlocals, flags,
166 code, consts, names, varnames, filename, name)
167 int argcount;
168 int nlocals;
169 int flags;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000170 object *code;
171 object *consts;
172 object *names;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000173 object *varnames;
Guido van Rossuma082ce41991-06-04 19:41:56 +0000174 object *filename;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000175 object *name;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000176{
177 codeobject *co;
178 int i;
179 /* Check argument types */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000180 if (argcount < 0 || nlocals < 0 ||
181 code == NULL || !is_stringobject(code) ||
182 consts == NULL || !is_tupleobject(consts) ||
183 names == NULL || !is_tupleobject(names) ||
184 varnames == NULL || !is_tupleobject(varnames) ||
185 name == NULL || !is_stringobject(name) ||
186 filename == NULL || !is_stringobject(filename)) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000187 err_badcall();
188 return NULL;
189 }
Guido van Rossum681d79a1995-07-18 14:51:37 +0000190 /* Make sure names and varnames are all strings */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000191 for (i = gettuplesize(names); --i >= 0; ) {
192 object *v = gettupleitem(names, i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000193 if (v == NULL || !is_stringobject(v)) {
Guido van Rossum681d79a1995-07-18 14:51:37 +0000194 err_badcall();
195 return NULL;
196 }
197 }
198 for (i = gettuplesize(varnames); --i >= 0; ) {
199 object *v = gettupleitem(varnames, i);
200 if (v == NULL || !is_stringobject(v)) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000201 err_badcall();
202 return NULL;
203 }
204 }
205 co = NEWOBJ(codeobject, &Codetype);
206 if (co != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +0000207 co->co_argcount = argcount;
208 co->co_nlocals = nlocals;
209 co->co_flags = flags;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000210 INCREF(code);
211 co->co_code = (stringobject *)code;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000212 INCREF(consts);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000213 co->co_consts = consts;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000214 INCREF(names);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000215 co->co_names = names;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000216 INCREF(varnames);
217 co->co_varnames = varnames;
Guido van Rossuma082ce41991-06-04 19:41:56 +0000218 INCREF(filename);
219 co->co_filename = filename;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000220 INCREF(name);
221 co->co_name = name;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000222 }
223 return co;
224}
225
226
227/* Data structure used internally */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000228
229#define MAXBLOCKS 20 /* Max static block nesting within a function */
230
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000231struct compiling {
232 object *c_code; /* string */
233 object *c_consts; /* list of objects */
234 object *c_names; /* list of strings (names) */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000235 object *c_globals; /* dictionary (value=None) */
236 object *c_locals; /* dictionary (value=localID) */
237 object *c_varnames; /* list (inverse of c_locals) */
238 int c_nlocals; /* index of next local */
239 int c_argcount; /* number of top-level arguments */
240 int c_flags; /* same as co_flags */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000241 int c_nexti; /* index into c_code */
242 int c_errors; /* counts errors occurred */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000243 int c_infunction; /* set when compiling a function */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000244 int c_interactive; /* generating code for interactive command */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000245 int c_loops; /* counts nested loops */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000246 int c_begin; /* begin of current loop, for 'continue' */
247 int c_block[MAXBLOCKS]; /* stack of block types */
248 int c_nblocks; /* current block stack level */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000249 char *c_filename; /* filename of current node */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000250 char *c_name; /* name of object (e.g. function) */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000251};
252
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000253
254/* Interface to the block stack */
255
256static void
257block_push(c, type)
258 struct compiling *c;
259 int type;
260{
261 if (c->c_nblocks >= MAXBLOCKS) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +0000262 err_setstr(SystemError, "too many statically nested blocks");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000263 c->c_errors++;
264 }
265 else {
266 c->c_block[c->c_nblocks++] = type;
267 }
268}
269
270static void
271block_pop(c, type)
272 struct compiling *c;
273 int type;
274{
275 if (c->c_nblocks > 0)
276 c->c_nblocks--;
277 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
278 err_setstr(SystemError, "bad block pop");
279 c->c_errors++;
280 }
281}
282
283
Guido van Rossum681d79a1995-07-18 14:51:37 +0000284/* Prototype forward declarations */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000285
Guido van Rossum590baa41993-11-30 13:40:46 +0000286static int com_init PROTO((struct compiling *, char *));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000287static void com_free PROTO((struct compiling *));
288static void com_done PROTO((struct compiling *));
289static void com_node PROTO((struct compiling *, struct _node *));
Guido van Rossum50564e81996-01-12 01:13:16 +0000290static void com_factor PROTO((struct compiling *, struct _node *));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000291static void com_addbyte PROTO((struct compiling *, int));
292static void com_addint PROTO((struct compiling *, int));
293static void com_addoparg PROTO((struct compiling *, int, int));
294static void com_addfwref PROTO((struct compiling *, int, int *));
295static void com_backpatch PROTO((struct compiling *, int));
296static int com_add PROTO((struct compiling *, object *, object *));
297static int com_addconst PROTO((struct compiling *, object *));
298static int com_addname PROTO((struct compiling *, object *));
299static void com_addopname PROTO((struct compiling *, int, node *));
Guido van Rossum288a60f1991-12-16 13:05:10 +0000300static void com_list PROTO((struct compiling *, node *, int));
Guido van Rossum681d79a1995-07-18 14:51:37 +0000301static int com_argdefs PROTO((struct compiling *, node *));
302static int com_newlocal PROTO((struct compiling *, char *));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000303
304static int
Guido van Rossum590baa41993-11-30 13:40:46 +0000305com_init(c, filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000306 struct compiling *c;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000307 char *filename;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000308{
Guido van Rossum62d46241991-04-03 19:00:23 +0000309 if ((c->c_code = newsizedstringobject((char *)NULL, 1000)) == NULL)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000310 goto fail_3;
311 if ((c->c_consts = newlistobject(0)) == NULL)
312 goto fail_2;
313 if ((c->c_names = newlistobject(0)) == NULL)
314 goto fail_1;
Guido van Rossumc5e96291991-12-10 13:53:51 +0000315 if ((c->c_globals = newdictobject()) == NULL)
316 goto fail_0;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000317 if ((c->c_locals = newdictobject()) == NULL)
318 goto fail_00;
319 if ((c->c_varnames = newlistobject(0)) == NULL)
320 goto fail_000;
321 c->c_nlocals = 0;
322 c->c_argcount = 0;
323 c->c_flags = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000324 c->c_nexti = 0;
325 c->c_errors = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000326 c->c_infunction = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000327 c->c_interactive = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000328 c->c_loops = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000329 c->c_begin = 0;
330 c->c_nblocks = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000331 c->c_filename = filename;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000332 c->c_name = "?";
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000333 return 1;
334
Guido van Rossum681d79a1995-07-18 14:51:37 +0000335 fail_000:
336 DECREF(c->c_locals);
337 fail_00:
338 DECREF(c->c_globals);
Guido van Rossumc5e96291991-12-10 13:53:51 +0000339 fail_0:
340 DECREF(c->c_names);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000341 fail_1:
342 DECREF(c->c_consts);
343 fail_2:
344 DECREF(c->c_code);
345 fail_3:
346 return 0;
347}
348
349static void
350com_free(c)
351 struct compiling *c;
352{
353 XDECREF(c->c_code);
354 XDECREF(c->c_consts);
355 XDECREF(c->c_names);
Guido van Rossumc5e96291991-12-10 13:53:51 +0000356 XDECREF(c->c_globals);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000357 XDECREF(c->c_locals);
358 XDECREF(c->c_varnames);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000359}
360
361static void
362com_done(c)
363 struct compiling *c;
364{
365 if (c->c_code != NULL)
366 resizestring(&c->c_code, c->c_nexti);
367}
368
369static void
370com_addbyte(c, byte)
371 struct compiling *c;
372 int byte;
373{
374 int len;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000375 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000376 if (byte < 0 || byte > 255) {
Guido van Rossum01cfd441991-10-20 20:12:38 +0000377 /*
Guido van Rossum3f5da241990-12-20 15:06:42 +0000378 fprintf(stderr, "XXX compiling bad byte: %d\n", byte);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000379 fatal("com_addbyte: byte out of range");
Guido van Rossum01cfd441991-10-20 20:12:38 +0000380 */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000381 err_setstr(SystemError, "com_addbyte: byte out of range");
382 c->c_errors++;
383 }
384 if (c->c_code == NULL)
385 return;
386 len = getstringsize(c->c_code);
387 if (c->c_nexti >= len) {
388 if (resizestring(&c->c_code, len+1000) != 0) {
389 c->c_errors++;
390 return;
391 }
392 }
393 getstringvalue(c->c_code)[c->c_nexti++] = byte;
394}
395
396static void
397com_addint(c, x)
398 struct compiling *c;
399 int x;
400{
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000401 com_addbyte(c, x & 0xff);
402 com_addbyte(c, x >> 8); /* XXX x should be positive */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000403}
404
405static void
406com_addoparg(c, op, arg)
407 struct compiling *c;
408 int op;
409 int arg;
410{
411 com_addbyte(c, op);
412 com_addint(c, arg);
413}
414
415static void
416com_addfwref(c, op, p_anchor)
417 struct compiling *c;
418 int op;
419 int *p_anchor;
420{
421 /* Compile a forward reference for backpatching */
422 int here;
423 int anchor;
424 com_addbyte(c, op);
425 here = c->c_nexti;
426 anchor = *p_anchor;
427 *p_anchor = here;
428 com_addint(c, anchor == 0 ? 0 : here - anchor);
429}
430
431static void
432com_backpatch(c, anchor)
433 struct compiling *c;
434 int anchor; /* Must be nonzero */
435{
436 unsigned char *code = (unsigned char *) getstringvalue(c->c_code);
437 int target = c->c_nexti;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000438 int dist;
439 int prev;
440 for (;;) {
441 /* Make the JUMP instruction at anchor point to target */
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000442 prev = code[anchor] + (code[anchor+1] << 8);
443 dist = target - (anchor+2);
444 code[anchor] = dist & 0xff;
445 code[anchor+1] = dist >> 8;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000446 if (!prev)
447 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000448 anchor -= prev;
449 }
450}
451
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000452/* Handle literals and names uniformly */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000453
454static int
455com_add(c, list, v)
456 struct compiling *c;
457 object *list;
458 object *v;
459{
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000460 int n = getlistsize(list);
461 int i;
462 for (i = n; --i >= 0; ) {
463 object *w = getlistitem(list, i);
Guido van Rossumefc0bd01991-07-01 18:44:20 +0000464 if (v->ob_type == w->ob_type && cmpobject(v, w) == 0)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000465 return i;
466 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000467 if (addlistitem(list, v) != 0)
468 c->c_errors++;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000469 return n;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000470}
471
472static int
473com_addconst(c, v)
474 struct compiling *c;
475 object *v;
476{
477 return com_add(c, c->c_consts, v);
478}
479
480static int
481com_addname(c, v)
482 struct compiling *c;
483 object *v;
484{
485 return com_add(c, c->c_names, v);
486}
487
488static void
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000489com_addopnamestr(c, op, name)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000490 struct compiling *c;
491 int op;
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000492 char *name;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000493{
494 object *v;
495 int i;
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000496 if (name == NULL || (v = newstringobject(name)) == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000497 c->c_errors++;
498 i = 255;
499 }
500 else {
501 i = com_addname(c, v);
502 DECREF(v);
503 }
Guido van Rossumc5e96291991-12-10 13:53:51 +0000504 /* Hack to replace *_NAME opcodes by *_GLOBAL if necessary */
505 switch (op) {
506 case LOAD_NAME:
507 case STORE_NAME:
508 case DELETE_NAME:
509 if (dictlookup(c->c_globals, name) != NULL) {
510 switch (op) {
511 case LOAD_NAME: op = LOAD_GLOBAL; break;
512 case STORE_NAME: op = STORE_GLOBAL; break;
513 case DELETE_NAME: op = DELETE_GLOBAL; break;
514 }
515 }
516 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000517 com_addoparg(c, op, i);
518}
519
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000520static void
521com_addopname(c, op, n)
522 struct compiling *c;
523 int op;
524 node *n;
525{
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000526 char *name;
527 char buffer[1000];
528 /* XXX it is possible to write this code without the 1000
529 chars on the total length of dotted names, I just can't be
530 bothered right now */
531 if (TYPE(n) == STAR)
532 name = "*";
533 else if (TYPE(n) == dotted_name) {
534 char *p = buffer;
535 int i;
536 name = buffer;
537 for (i = 0; i < NCH(n); i += 2) {
538 char *s = STR(CHILD(n, i));
539 if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
540 err_setstr(MemoryError,
541 "dotted_name too long");
Guido van Rossumd9dfaf51995-02-17 15:04:57 +0000542 name = NULL;
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000543 break;
544 }
545 if (p != buffer)
546 *p++ = '.';
547 strcpy(p, s);
548 p = strchr(p, '\0');
549 }
550 }
551 else {
552 REQ(n, NAME);
553 name = STR(n);
554 }
555 com_addopnamestr(c, op, name);
556}
557
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000558static object *
559parsenumber(s)
560 char *s;
561{
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000562 extern long mystrtol PROTO((const char *, char **, int));
563 extern unsigned long mystrtoul PROTO((const char *, char **, int));
564 extern double atof PROTO((const char *));
Guido van Rossumc5e96291991-12-10 13:53:51 +0000565 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000566 long x;
Guido van Rossum50564e81996-01-12 01:13:16 +0000567#ifndef WITHOUT_COMPLEX
568 complex c;
569 int imflag;
570#endif
571
Guido van Rossum282914b1991-04-04 10:42:56 +0000572 errno = 0;
Guido van Rossumc5e96291991-12-10 13:53:51 +0000573 end = s + strlen(s) - 1;
Guido van Rossum50564e81996-01-12 01:13:16 +0000574#ifndef WITHOUT_COMPLEX
Guido van Rossum15ad9a61996-01-26 20:53:56 +0000575 imflag = *end == 'j' || *end == 'J';
Guido van Rossum50564e81996-01-12 01:13:16 +0000576#endif
Guido van Rossumf1aeab71992-03-27 17:28:26 +0000577 if (*end == 'l' || *end == 'L')
Guido van Rossumc5e96291991-12-10 13:53:51 +0000578 return long_scan(s, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +0000579 if (s[0] == '0')
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000580 x = (long) mystrtoul(s, &end, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +0000581 else
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000582 x = mystrtol(s, &end, 0);
Guido van Rossum282914b1991-04-04 10:42:56 +0000583 if (*end == '\0') {
584 if (errno != 0) {
Guido van Rossumefb087b1991-12-16 15:41:41 +0000585 err_setstr(OverflowError,
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000586 "integer literal too large");
Guido van Rossum282914b1991-04-04 10:42:56 +0000587 return NULL;
588 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000589 return newintobject(x);
Guido van Rossum282914b1991-04-04 10:42:56 +0000590 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000591 /* XXX Huge floats may silently fail */
Guido van Rossum50564e81996-01-12 01:13:16 +0000592#ifndef WITHOUT_COMPLEX
593 if (imflag) {
594 c.real = 0.;
595 c.imag = atof(s);
596 return newcomplexobject(c);
597 }
598 else
599#endif
600 return newfloatobject(atof(s));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000601}
602
603static object *
604parsestr(s)
605 char *s;
606{
607 object *v;
608 int len;
609 char *buf;
610 char *p;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000611 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000612 int c;
Guido van Rossum8054fad1993-10-26 15:19:44 +0000613 int quote = *s;
614 if (quote != '\'' && quote != '\"') {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000615 err_badcall();
616 return NULL;
617 }
618 s++;
619 len = strlen(s);
Guido van Rossum8054fad1993-10-26 15:19:44 +0000620 if (s[--len] != quote) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000621 err_badcall();
622 return NULL;
623 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000624 if (len >= 4 && s[0] == quote && s[1] == quote) {
625 s += 2;
626 len -= 2;
627 if (s[--len] != quote || s[--len] != quote) {
628 err_badcall();
629 return NULL;
630 }
631 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000632 if (strchr(s, '\\') == NULL)
633 return newsizedstringobject(s, len);
634 v = newsizedstringobject((char *)NULL, len);
635 p = buf = getstringvalue(v);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000636 end = s + len;
637 while (s < end) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000638 if (*s != '\\') {
639 *p++ = *s++;
640 continue;
641 }
642 s++;
643 switch (*s++) {
644 /* XXX This assumes ASCII! */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000645 case '\n': break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000646 case '\\': *p++ = '\\'; break;
647 case '\'': *p++ = '\''; break;
Guido van Rossum8054fad1993-10-26 15:19:44 +0000648 case '\"': *p++ = '\"'; break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000649 case 'b': *p++ = '\b'; break;
650 case 'f': *p++ = '\014'; break; /* FF */
651 case 't': *p++ = '\t'; break;
652 case 'n': *p++ = '\n'; break;
653 case 'r': *p++ = '\r'; break;
654 case 'v': *p++ = '\013'; break; /* VT */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000655 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
656 case '0': case '1': case '2': case '3':
657 case '4': case '5': case '6': case '7':
658 c = s[-1] - '0';
659 if ('0' <= *s && *s <= '7') {
660 c = (c<<3) + *s++ - '0';
661 if ('0' <= *s && *s <= '7')
662 c = (c<<3) + *s++ - '0';
663 }
664 *p++ = c;
665 break;
666 case 'x':
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000667 if (isxdigit(Py_CHARMASK(*s))) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000668 sscanf(s, "%x", &c);
669 *p++ = c;
670 do {
671 s++;
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000672 } while (isxdigit(Py_CHARMASK(*s)));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000673 break;
674 }
675 /* FALLTHROUGH */
676 default: *p++ = '\\'; *p++ = s[-1]; break;
677 }
678 }
679 resizestring(&v, (int)(p - buf));
680 return v;
681}
682
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000683static object *
684parsestrplus(n)
685 node *n;
686{
687 object *v;
688 int i;
689 REQ(CHILD(n, 0), STRING);
690 if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) {
691 /* String literal concatenation */
692 for (i = 1; i < NCH(n) && v != NULL; i++) {
693 joinstring_decref(&v, parsestr(STR(CHILD(n, i))));
694 }
695 }
696 return v;
697}
698
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000699static void
700com_list_constructor(c, n)
701 struct compiling *c;
702 node *n;
703{
704 int len;
705 int i;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000706 if (TYPE(n) != testlist)
707 REQ(n, exprlist);
708 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
709 len = (NCH(n) + 1) / 2;
710 for (i = 0; i < NCH(n); i += 2)
711 com_node(c, CHILD(n, i));
712 com_addoparg(c, BUILD_LIST, len);
713}
714
715static void
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000716com_dictmaker(c, n)
717 struct compiling *c;
718 node *n;
719{
720 int i;
721 /* dictmaker: test ':' test (',' test ':' value)* [','] */
722 for (i = 0; i+2 < NCH(n); i += 4) {
723 /* We must arrange things just right for STORE_SUBSCR.
724 It wants the stack to look like (value) (dict) (key) */
725 com_addbyte(c, DUP_TOP);
726 com_node(c, CHILD(n, i+2)); /* value */
727 com_addbyte(c, ROT_TWO);
728 com_node(c, CHILD(n, i)); /* key */
729 com_addbyte(c, STORE_SUBSCR);
730 }
731}
732
733static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000734com_atom(c, n)
735 struct compiling *c;
736 node *n;
737{
738 node *ch;
739 object *v;
740 int i;
741 REQ(n, atom);
742 ch = CHILD(n, 0);
743 switch (TYPE(ch)) {
744 case LPAR:
745 if (TYPE(CHILD(n, 1)) == RPAR)
746 com_addoparg(c, BUILD_TUPLE, 0);
747 else
748 com_node(c, CHILD(n, 1));
749 break;
750 case LSQB:
751 if (TYPE(CHILD(n, 1)) == RSQB)
752 com_addoparg(c, BUILD_LIST, 0);
753 else
754 com_list_constructor(c, CHILD(n, 1));
755 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000756 case LBRACE: /* '{' [dictmaker] '}' */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000757 com_addoparg(c, BUILD_MAP, 0);
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000758 if (TYPE(CHILD(n, 1)) != RBRACE)
759 com_dictmaker(c, CHILD(n, 1));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000760 break;
761 case BACKQUOTE:
762 com_node(c, CHILD(n, 1));
763 com_addbyte(c, UNARY_CONVERT);
764 break;
765 case NUMBER:
766 if ((v = parsenumber(STR(ch))) == NULL) {
767 c->c_errors++;
768 i = 255;
769 }
770 else {
771 i = com_addconst(c, v);
772 DECREF(v);
773 }
774 com_addoparg(c, LOAD_CONST, i);
775 break;
776 case STRING:
Guido van Rossum1f4fa501995-01-07 12:41:23 +0000777 v = parsestrplus(n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000778 if (v == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000779 c->c_errors++;
780 i = 255;
781 }
782 else {
783 i = com_addconst(c, v);
784 DECREF(v);
785 }
786 com_addoparg(c, LOAD_CONST, i);
787 break;
788 case NAME:
789 com_addopname(c, LOAD_NAME, ch);
790 break;
791 default:
792 fprintf(stderr, "node type %d\n", TYPE(ch));
793 err_setstr(SystemError, "com_atom: unexpected node type");
794 c->c_errors++;
795 }
796}
797
798static void
799com_slice(c, n, op)
800 struct compiling *c;
801 node *n;
802 int op;
803{
804 if (NCH(n) == 1) {
805 com_addbyte(c, op);
806 }
807 else if (NCH(n) == 2) {
808 if (TYPE(CHILD(n, 0)) != COLON) {
809 com_node(c, CHILD(n, 0));
810 com_addbyte(c, op+1);
811 }
812 else {
813 com_node(c, CHILD(n, 1));
814 com_addbyte(c, op+2);
815 }
816 }
817 else {
818 com_node(c, CHILD(n, 0));
819 com_node(c, CHILD(n, 2));
820 com_addbyte(c, op+3);
821 }
822}
823
824static void
825com_apply_subscript(c, n)
826 struct compiling *c;
827 node *n;
828{
829 REQ(n, subscript);
Guido van Rossum50564e81996-01-12 01:13:16 +0000830 if (TYPE(CHILD(n, 0)) == COLON || (NCH(n) > 1 && TYPE(CHILD(n, 1)) == COLON)) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000831 /* It's a slice: [expr] ':' [expr] */
832 com_slice(c, n, SLICE);
833 }
Guido van Rossum50564e81996-01-12 01:13:16 +0000834 else {
835 /* It's a list of subscripts */
836 if (NCH(n) == 1)
837 com_node(c, CHILD(n, 0));
838 else {
839 int i;
840 int len = (NCH(n)+1)/2;
841 for (i = 0; i < NCH(n); i += 2)
842 com_node(c, CHILD(n, i));
843 com_addoparg(c, BUILD_TUPLE, len);
844 }
845 com_addbyte(c, BINARY_SUBSCR);
846 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000847}
848
Guido van Rossumf10570b1995-07-07 22:53:21 +0000849static int
850com_argument(c, n, inkeywords)
851 struct compiling *c;
852 node *n; /* argument */
853 int inkeywords;
854{
855 node *m;
856 REQ(n, argument); /* [test '='] test; really [ keyword '='] keyword */
857 if (NCH(n) == 1) {
858 if (inkeywords) {
859 err_setstr(SyntaxError,
860 "non-keyword arg after keyword arg");
861 c->c_errors++;
862 }
863 else {
864 com_node(c, CHILD(n, 0));
865 }
866 return 0;
867 }
868 m = n;
869 do {
870 m = CHILD(m, 0);
871 } while (NCH(m) == 1);
872 if (TYPE(m) != NAME) {
873 err_setstr(SyntaxError, "keyword can't be an expression");
874 c->c_errors++;
875 }
876 else {
877 object *v = newstringobject(STR(m));
878 if (v == NULL)
879 c->c_errors++;
880 else {
881 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
882 DECREF(v);
883 }
884 }
885 com_node(c, CHILD(n, 2));
886 return 1;
887}
888
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000889static void
890com_call_function(c, n)
891 struct compiling *c;
Guido van Rossumf10570b1995-07-07 22:53:21 +0000892 node *n; /* EITHER arglist OR ')' */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000893{
894 if (TYPE(n) == RPAR) {
Guido van Rossumf10570b1995-07-07 22:53:21 +0000895 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000896 }
897 else {
Guido van Rossumf10570b1995-07-07 22:53:21 +0000898 int inkeywords, i, na, nk;
899 REQ(n, arglist);
900 inkeywords = 0;
901 na = 0;
902 nk = 0;
903 for (i = 0; i < NCH(n); i += 2) {
904 inkeywords = com_argument(c, CHILD(n, i), inkeywords);
905 if (!inkeywords)
906 na++;
907 else
908 nk++;
909 }
910 if (na > 255 || nk > 255) {
911 err_setstr(SyntaxError, "more than 255 arguments");
912 c->c_errors++;
913 }
914 com_addoparg(c, CALL_FUNCTION, na | (nk << 8));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000915 }
916}
917
918static void
919com_select_member(c, n)
920 struct compiling *c;
921 node *n;
922{
923 com_addopname(c, LOAD_ATTR, n);
924}
925
926static void
927com_apply_trailer(c, n)
928 struct compiling *c;
929 node *n;
930{
931 REQ(n, trailer);
932 switch (TYPE(CHILD(n, 0))) {
933 case LPAR:
934 com_call_function(c, CHILD(n, 1));
935 break;
936 case DOT:
937 com_select_member(c, CHILD(n, 1));
938 break;
939 case LSQB:
940 com_apply_subscript(c, CHILD(n, 1));
941 break;
942 default:
943 err_setstr(SystemError,
944 "com_apply_trailer: unknown trailer type");
945 c->c_errors++;
946 }
947}
948
949static void
Guido van Rossum50564e81996-01-12 01:13:16 +0000950com_power(c, n)
951 struct compiling *c;
952 node *n;
953{
954 int i;
955 REQ(n, power);
956 com_atom(c, CHILD(n, 0));
957 for (i = 1; i < NCH(n); i++) {
958 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
959 com_factor(c, CHILD(n, i+1));
960 com_addbyte(c, BINARY_POWER);
961 break;
962 }
963 else
964 com_apply_trailer(c, CHILD(n, i));
965 }
966}
967
968static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000969com_factor(c, n)
970 struct compiling *c;
971 node *n;
972{
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000973 REQ(n, factor);
974 if (TYPE(CHILD(n, 0)) == PLUS) {
975 com_factor(c, CHILD(n, 1));
976 com_addbyte(c, UNARY_POSITIVE);
977 }
978 else if (TYPE(CHILD(n, 0)) == MINUS) {
979 com_factor(c, CHILD(n, 1));
980 com_addbyte(c, UNARY_NEGATIVE);
981 }
Guido van Rossum7928cd71991-10-24 14:59:31 +0000982 else if (TYPE(CHILD(n, 0)) == TILDE) {
983 com_factor(c, CHILD(n, 1));
984 com_addbyte(c, UNARY_INVERT);
985 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000986 else {
Guido van Rossum50564e81996-01-12 01:13:16 +0000987 com_power(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000988 }
989}
990
991static void
992com_term(c, n)
993 struct compiling *c;
994 node *n;
995{
996 int i;
997 int op;
998 REQ(n, term);
999 com_factor(c, CHILD(n, 0));
1000 for (i = 2; i < NCH(n); i += 2) {
1001 com_factor(c, CHILD(n, i));
1002 switch (TYPE(CHILD(n, i-1))) {
1003 case STAR:
1004 op = BINARY_MULTIPLY;
1005 break;
1006 case SLASH:
1007 op = BINARY_DIVIDE;
1008 break;
1009 case PERCENT:
1010 op = BINARY_MODULO;
1011 break;
1012 default:
1013 err_setstr(SystemError,
Guido van Rossum7928cd71991-10-24 14:59:31 +00001014 "com_term: operator not *, / or %");
1015 c->c_errors++;
1016 op = 255;
1017 }
1018 com_addbyte(c, op);
1019 }
1020}
1021
1022static void
1023com_arith_expr(c, n)
1024 struct compiling *c;
1025 node *n;
1026{
1027 int i;
1028 int op;
1029 REQ(n, arith_expr);
1030 com_term(c, CHILD(n, 0));
1031 for (i = 2; i < NCH(n); i += 2) {
1032 com_term(c, CHILD(n, i));
1033 switch (TYPE(CHILD(n, i-1))) {
1034 case PLUS:
1035 op = BINARY_ADD;
1036 break;
1037 case MINUS:
1038 op = BINARY_SUBTRACT;
1039 break;
1040 default:
1041 err_setstr(SystemError,
1042 "com_arith_expr: operator not + or -");
1043 c->c_errors++;
1044 op = 255;
1045 }
1046 com_addbyte(c, op);
1047 }
1048}
1049
1050static void
1051com_shift_expr(c, n)
1052 struct compiling *c;
1053 node *n;
1054{
1055 int i;
1056 int op;
1057 REQ(n, shift_expr);
1058 com_arith_expr(c, CHILD(n, 0));
1059 for (i = 2; i < NCH(n); i += 2) {
1060 com_arith_expr(c, CHILD(n, i));
1061 switch (TYPE(CHILD(n, i-1))) {
1062 case LEFTSHIFT:
1063 op = BINARY_LSHIFT;
1064 break;
1065 case RIGHTSHIFT:
1066 op = BINARY_RSHIFT;
1067 break;
1068 default:
1069 err_setstr(SystemError,
1070 "com_shift_expr: operator not << or >>");
1071 c->c_errors++;
1072 op = 255;
1073 }
1074 com_addbyte(c, op);
1075 }
1076}
1077
1078static void
1079com_and_expr(c, n)
1080 struct compiling *c;
1081 node *n;
1082{
1083 int i;
1084 int op;
1085 REQ(n, and_expr);
1086 com_shift_expr(c, CHILD(n, 0));
1087 for (i = 2; i < NCH(n); i += 2) {
1088 com_shift_expr(c, CHILD(n, i));
1089 if (TYPE(CHILD(n, i-1)) == AMPER) {
1090 op = BINARY_AND;
1091 }
1092 else {
1093 err_setstr(SystemError,
1094 "com_and_expr: operator not &");
1095 c->c_errors++;
1096 op = 255;
1097 }
1098 com_addbyte(c, op);
1099 }
1100}
1101
1102static void
1103com_xor_expr(c, n)
1104 struct compiling *c;
1105 node *n;
1106{
1107 int i;
1108 int op;
1109 REQ(n, xor_expr);
1110 com_and_expr(c, CHILD(n, 0));
1111 for (i = 2; i < NCH(n); i += 2) {
1112 com_and_expr(c, CHILD(n, i));
1113 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
1114 op = BINARY_XOR;
1115 }
1116 else {
1117 err_setstr(SystemError,
1118 "com_xor_expr: operator not ^");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001119 c->c_errors++;
1120 op = 255;
1121 }
1122 com_addbyte(c, op);
1123 }
1124}
1125
1126static void
1127com_expr(c, n)
1128 struct compiling *c;
1129 node *n;
1130{
1131 int i;
1132 int op;
1133 REQ(n, expr);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001134 com_xor_expr(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001135 for (i = 2; i < NCH(n); i += 2) {
Guido van Rossum7928cd71991-10-24 14:59:31 +00001136 com_xor_expr(c, CHILD(n, i));
1137 if (TYPE(CHILD(n, i-1)) == VBAR) {
1138 op = BINARY_OR;
1139 }
1140 else {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001141 err_setstr(SystemError,
Guido van Rossum7928cd71991-10-24 14:59:31 +00001142 "com_expr: expr operator not |");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001143 c->c_errors++;
1144 op = 255;
1145 }
1146 com_addbyte(c, op);
1147 }
1148}
1149
1150static enum cmp_op
1151cmp_type(n)
1152 node *n;
1153{
1154 REQ(n, comp_op);
Guido van Rossum01cfd441991-10-20 20:12:38 +00001155 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001156 | 'in' | 'not' 'in' | 'is' | 'is' not' */
1157 if (NCH(n) == 1) {
1158 n = CHILD(n, 0);
1159 switch (TYPE(n)) {
1160 case LESS: return LT;
1161 case GREATER: return GT;
Guido van Rossum01cfd441991-10-20 20:12:38 +00001162 case EQEQUAL: /* == */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001163 case EQUAL: return EQ;
Guido van Rossum01cfd441991-10-20 20:12:38 +00001164 case LESSEQUAL: return LE;
1165 case GREATEREQUAL: return GE;
1166 case NOTEQUAL: return NE; /* <> or != */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001167 case NAME: if (strcmp(STR(n), "in") == 0) return IN;
1168 if (strcmp(STR(n), "is") == 0) return IS;
1169 }
1170 }
1171 else if (NCH(n) == 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001172 switch (TYPE(CHILD(n, 0))) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001173 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1174 return NOT_IN;
1175 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1176 return IS_NOT;
1177 }
1178 }
1179 return BAD;
1180}
1181
1182static void
1183com_comparison(c, n)
1184 struct compiling *c;
1185 node *n;
1186{
1187 int i;
1188 enum cmp_op op;
1189 int anchor;
1190 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
1191 com_expr(c, CHILD(n, 0));
1192 if (NCH(n) == 1)
1193 return;
1194
1195 /****************************************************************
1196 The following code is generated for all but the last
1197 comparison in a chain:
1198
1199 label: on stack: opcode: jump to:
1200
1201 a <code to load b>
1202 a, b DUP_TOP
1203 a, b, b ROT_THREE
1204 b, a, b COMPARE_OP
1205 b, 0-or-1 JUMP_IF_FALSE L1
1206 b, 1 POP_TOP
1207 b
1208
1209 We are now ready to repeat this sequence for the next
1210 comparison in the chain.
1211
1212 For the last we generate:
1213
1214 b <code to load c>
1215 b, c COMPARE_OP
1216 0-or-1
1217
1218 If there were any jumps to L1 (i.e., there was more than one
1219 comparison), we generate:
1220
1221 0-or-1 JUMP_FORWARD L2
1222 L1: b, 0 ROT_TWO
1223 0, b POP_TOP
1224 0
1225 L2:
1226 ****************************************************************/
1227
1228 anchor = 0;
1229
1230 for (i = 2; i < NCH(n); i += 2) {
1231 com_expr(c, CHILD(n, i));
1232 if (i+2 < NCH(n)) {
1233 com_addbyte(c, DUP_TOP);
1234 com_addbyte(c, ROT_THREE);
1235 }
1236 op = cmp_type(CHILD(n, i-1));
1237 if (op == BAD) {
1238 err_setstr(SystemError,
1239 "com_comparison: unknown comparison op");
1240 c->c_errors++;
1241 }
1242 com_addoparg(c, COMPARE_OP, op);
1243 if (i+2 < NCH(n)) {
1244 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1245 com_addbyte(c, POP_TOP);
1246 }
1247 }
1248
1249 if (anchor) {
1250 int anchor2 = 0;
1251 com_addfwref(c, JUMP_FORWARD, &anchor2);
1252 com_backpatch(c, anchor);
1253 com_addbyte(c, ROT_TWO);
1254 com_addbyte(c, POP_TOP);
1255 com_backpatch(c, anchor2);
1256 }
1257}
1258
1259static void
1260com_not_test(c, n)
1261 struct compiling *c;
1262 node *n;
1263{
1264 REQ(n, not_test); /* 'not' not_test | comparison */
1265 if (NCH(n) == 1) {
1266 com_comparison(c, CHILD(n, 0));
1267 }
1268 else {
1269 com_not_test(c, CHILD(n, 1));
1270 com_addbyte(c, UNARY_NOT);
1271 }
1272}
1273
1274static void
1275com_and_test(c, n)
1276 struct compiling *c;
1277 node *n;
1278{
1279 int i;
1280 int anchor;
1281 REQ(n, and_test); /* not_test ('and' not_test)* */
1282 anchor = 0;
1283 i = 0;
1284 for (;;) {
1285 com_not_test(c, CHILD(n, i));
1286 if ((i += 2) >= NCH(n))
1287 break;
1288 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1289 com_addbyte(c, POP_TOP);
1290 }
1291 if (anchor)
1292 com_backpatch(c, anchor);
1293}
1294
1295static void
1296com_test(c, n)
1297 struct compiling *c;
1298 node *n;
1299{
Guido van Rossum57531fe1993-11-30 14:57:42 +00001300 REQ(n, test); /* and_test ('and' and_test)* | lambdef */
1301 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
1302 object *v;
1303 int i;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001304 int ndefs = com_argdefs(c, CHILD(n, 0));
Guido van Rossum57531fe1993-11-30 14:57:42 +00001305 v = (object *) compile(CHILD(n, 0), c->c_filename);
1306 if (v == NULL) {
1307 c->c_errors++;
1308 i = 255;
1309 }
1310 else {
1311 i = com_addconst(c, v);
1312 DECREF(v);
1313 }
1314 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001315 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001316 }
Guido van Rossum57531fe1993-11-30 14:57:42 +00001317 else {
1318 int anchor = 0;
1319 int i = 0;
1320 for (;;) {
1321 com_and_test(c, CHILD(n, i));
1322 if ((i += 2) >= NCH(n))
1323 break;
1324 com_addfwref(c, JUMP_IF_TRUE, &anchor);
1325 com_addbyte(c, POP_TOP);
1326 }
1327 if (anchor)
1328 com_backpatch(c, anchor);
1329 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001330}
1331
1332static void
Guido van Rossum288a60f1991-12-16 13:05:10 +00001333com_list(c, n, toplevel)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001334 struct compiling *c;
1335 node *n;
Guido van Rossum288a60f1991-12-16 13:05:10 +00001336 int toplevel; /* If nonzero, *always* build a tuple */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001337{
1338 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
Guido van Rossum288a60f1991-12-16 13:05:10 +00001339 if (NCH(n) == 1 && !toplevel) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001340 com_node(c, CHILD(n, 0));
1341 }
1342 else {
1343 int i;
1344 int len;
1345 len = (NCH(n) + 1) / 2;
1346 for (i = 0; i < NCH(n); i += 2)
1347 com_node(c, CHILD(n, i));
1348 com_addoparg(c, BUILD_TUPLE, len);
1349 }
1350}
1351
1352
1353/* Begin of assignment compilation */
1354
1355static void com_assign_name PROTO((struct compiling *, node *, int));
1356static void com_assign PROTO((struct compiling *, node *, int));
1357
1358static void
1359com_assign_attr(c, n, assigning)
1360 struct compiling *c;
1361 node *n;
1362 int assigning;
1363{
1364 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
1365}
1366
1367static void
1368com_assign_slice(c, n, assigning)
1369 struct compiling *c;
1370 node *n;
1371 int assigning;
1372{
1373 com_slice(c, n, assigning ? STORE_SLICE : DELETE_SLICE);
1374}
1375
1376static void
1377com_assign_subscript(c, n, assigning)
1378 struct compiling *c;
1379 node *n;
1380 int assigning;
1381{
Guido van Rossum50564e81996-01-12 01:13:16 +00001382 if (NCH(n) == 1)
1383 com_node(c, CHILD(n, 0));
1384 else {
1385 int i;
1386 int len = (NCH(n)+1)/2;
1387 for (i = 0; i < NCH(n); i += 2)
1388 com_node(c, CHILD(n, i));
1389 com_addoparg(c, BUILD_TUPLE, len);
1390 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001391 com_addbyte(c, assigning ? STORE_SUBSCR : DELETE_SUBSCR);
1392}
1393
1394static void
1395com_assign_trailer(c, n, assigning)
1396 struct compiling *c;
1397 node *n;
1398 int assigning;
1399{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001400 REQ(n, trailer);
1401 switch (TYPE(CHILD(n, 0))) {
1402 case LPAR: /* '(' [exprlist] ')' */
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001403 err_setstr(SyntaxError, "can't assign to function call");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001404 c->c_errors++;
1405 break;
1406 case DOT: /* '.' NAME */
1407 com_assign_attr(c, CHILD(n, 1), assigning);
1408 break;
1409 case LSQB: /* '[' subscript ']' */
1410 n = CHILD(n, 1);
Guido van Rossum50564e81996-01-12 01:13:16 +00001411 REQ(n, subscript); /* subscript: expr (',' expr)* | [expr] ':' [expr] */
1412 if (TYPE(CHILD(n, 0)) == COLON || (NCH(n) > 1 && TYPE(CHILD(n, 1)) == COLON))
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001413 com_assign_slice(c, n, assigning);
1414 else
Guido van Rossum50564e81996-01-12 01:13:16 +00001415 com_assign_subscript(c, n, assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001416 break;
1417 default:
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001418 err_setstr(SystemError, "unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001419 c->c_errors++;
1420 }
1421}
1422
1423static void
1424com_assign_tuple(c, n, assigning)
1425 struct compiling *c;
1426 node *n;
1427 int assigning;
1428{
1429 int i;
1430 if (TYPE(n) != testlist)
1431 REQ(n, exprlist);
1432 if (assigning)
1433 com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2);
1434 for (i = 0; i < NCH(n); i += 2)
1435 com_assign(c, CHILD(n, i), assigning);
1436}
1437
1438static void
1439com_assign_list(c, n, assigning)
1440 struct compiling *c;
1441 node *n;
1442 int assigning;
1443{
1444 int i;
1445 if (assigning)
1446 com_addoparg(c, UNPACK_LIST, (NCH(n)+1)/2);
1447 for (i = 0; i < NCH(n); i += 2)
1448 com_assign(c, CHILD(n, i), assigning);
1449}
1450
1451static void
1452com_assign_name(c, n, assigning)
1453 struct compiling *c;
1454 node *n;
1455 int assigning;
1456{
1457 REQ(n, NAME);
1458 com_addopname(c, assigning ? STORE_NAME : DELETE_NAME, n);
1459}
1460
1461static void
1462com_assign(c, n, assigning)
1463 struct compiling *c;
1464 node *n;
1465 int assigning;
1466{
1467 /* Loop to avoid trivial recursion */
1468 for (;;) {
1469 switch (TYPE(n)) {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001470
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001471 case exprlist:
1472 case testlist:
1473 if (NCH(n) > 1) {
1474 com_assign_tuple(c, n, assigning);
1475 return;
1476 }
1477 n = CHILD(n, 0);
1478 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001479
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001480 case test:
1481 case and_test:
1482 case not_test:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001483 case comparison:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001484 case expr:
Guido van Rossum7928cd71991-10-24 14:59:31 +00001485 case xor_expr:
1486 case and_expr:
1487 case shift_expr:
1488 case arith_expr:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001489 case term:
Guido van Rossum50564e81996-01-12 01:13:16 +00001490 case factor:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001491 if (NCH(n) > 1) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001492 err_setstr(SyntaxError,
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001493 "can't assign to operator");
1494 c->c_errors++;
1495 return;
1496 }
1497 n = CHILD(n, 0);
1498 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001499
Guido van Rossum50564e81996-01-12 01:13:16 +00001500 case power: /* atom trailer* ('**' power)* */
1501/* ('+'|'-'|'~') factor | atom trailer* */
1502 if (TYPE(CHILD(n, 0)) != atom) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001503 err_setstr(SyntaxError,
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001504 "can't assign to operator");
1505 c->c_errors++;
1506 return;
1507 }
Guido van Rossum50564e81996-01-12 01:13:16 +00001508 if (NCH(n) > 1) { /* trailer or exponent present */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001509 int i;
1510 com_node(c, CHILD(n, 0));
1511 for (i = 1; i+1 < NCH(n); i++) {
Guido van Rossum50564e81996-01-12 01:13:16 +00001512 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
1513 err_setstr(SyntaxError,
1514 "can't assign to operator");
1515 c->c_errors++;
1516 return;
1517 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001518 com_apply_trailer(c, CHILD(n, i));
1519 } /* NB i is still alive */
1520 com_assign_trailer(c,
1521 CHILD(n, i), assigning);
1522 return;
1523 }
1524 n = CHILD(n, 0);
1525 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001526
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001527 case atom:
1528 switch (TYPE(CHILD(n, 0))) {
1529 case LPAR:
1530 n = CHILD(n, 1);
1531 if (TYPE(n) == RPAR) {
1532 /* XXX Should allow () = () ??? */
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001533 err_setstr(SyntaxError,
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001534 "can't assign to ()");
1535 c->c_errors++;
1536 return;
1537 }
1538 break;
1539 case LSQB:
1540 n = CHILD(n, 1);
1541 if (TYPE(n) == RSQB) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001542 err_setstr(SyntaxError,
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001543 "can't assign to []");
1544 c->c_errors++;
1545 return;
1546 }
1547 com_assign_list(c, n, assigning);
1548 return;
1549 case NAME:
1550 com_assign_name(c, CHILD(n, 0), assigning);
1551 return;
1552 default:
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001553 err_setstr(SyntaxError,
Guido van Rossuma9df32a1991-12-31 13:13:35 +00001554 "can't assign to literal");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001555 c->c_errors++;
1556 return;
1557 }
1558 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001559
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001560 default:
1561 fprintf(stderr, "node type %d\n", TYPE(n));
1562 err_setstr(SystemError, "com_assign: bad node");
1563 c->c_errors++;
1564 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001565
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001566 }
1567 }
1568}
1569
1570static void
1571com_expr_stmt(c, n)
1572 struct compiling *c;
1573 node *n;
1574{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001575 REQ(n, expr_stmt); /* testlist ('=' testlist)* */
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001576 com_node(c, CHILD(n, NCH(n)-1));
1577 if (NCH(n) == 1) {
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001578 if (c->c_interactive)
1579 com_addbyte(c, PRINT_EXPR);
1580 else
1581 com_addbyte(c, POP_TOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001582 }
1583 else {
1584 int i;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001585 for (i = 0; i < NCH(n)-2; i+=2) {
1586 if (i+2 < NCH(n)-2)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001587 com_addbyte(c, DUP_TOP);
1588 com_assign(c, CHILD(n, i), 1/*assign*/);
1589 }
1590 }
1591}
1592
1593static void
1594com_print_stmt(c, n)
1595 struct compiling *c;
1596 node *n;
1597{
1598 int i;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001599 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
1600 for (i = 1; i < NCH(n); i += 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001601 com_node(c, CHILD(n, i));
1602 com_addbyte(c, PRINT_ITEM);
1603 }
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001604 if (TYPE(CHILD(n, NCH(n)-1)) != COMMA)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001605 com_addbyte(c, PRINT_NEWLINE);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001606 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001607}
1608
1609static void
1610com_return_stmt(c, n)
1611 struct compiling *c;
1612 node *n;
1613{
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001614 REQ(n, return_stmt); /* 'return' [testlist] */
Guido van Rossum3f5da241990-12-20 15:06:42 +00001615 if (!c->c_infunction) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001616 err_setstr(SyntaxError, "'return' outside function");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001617 c->c_errors++;
1618 }
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001619 if (NCH(n) < 2)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001620 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
1621 else
1622 com_node(c, CHILD(n, 1));
1623 com_addbyte(c, RETURN_VALUE);
1624}
1625
1626static void
1627com_raise_stmt(c, n)
1628 struct compiling *c;
1629 node *n;
1630{
Guido van Rossumf10570b1995-07-07 22:53:21 +00001631 REQ(n, raise_stmt); /* 'raise' test [',' test [',' test]] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001632 com_node(c, CHILD(n, 1));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001633 if (NCH(n) > 3) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001634 com_node(c, CHILD(n, 3));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001635 if (NCH(n) > 5)
1636 com_node(c, CHILD(n, 5));
Guido van Rossumf10570b1995-07-07 22:53:21 +00001637 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001638 com_addoparg(c, RAISE_VARARGS, NCH(n)/2);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001639}
1640
1641static void
1642com_import_stmt(c, n)
1643 struct compiling *c;
1644 node *n;
1645{
1646 int i;
1647 REQ(n, import_stmt);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001648 /* 'import' dotted_name (',' dotted_name)* |
1649 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001650 if (STR(CHILD(n, 0))[0] == 'f') {
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001651 /* 'from' dotted_name 'import' ... */
1652 REQ(CHILD(n, 1), dotted_name);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001653 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
1654 for (i = 3; i < NCH(n); i += 2)
1655 com_addopname(c, IMPORT_FROM, CHILD(n, i));
1656 com_addbyte(c, POP_TOP);
1657 }
1658 else {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001659 /* 'import' ... */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001660 for (i = 1; i < NCH(n); i += 2) {
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001661 REQ(CHILD(n, i), dotted_name);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001662 com_addopname(c, IMPORT_NAME, CHILD(n, i));
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001663 com_addopname(c, STORE_NAME, CHILD(CHILD(n, i), 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001664 }
1665 }
1666}
1667
1668static void
Guido van Rossumc5e96291991-12-10 13:53:51 +00001669com_global_stmt(c, n)
1670 struct compiling *c;
1671 node *n;
1672{
1673 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00001674 REQ(n, global_stmt);
1675 /* 'global' NAME (',' NAME)* */
1676 for (i = 1; i < NCH(n); i += 2) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00001677 char *s = STR(CHILD(n, i));
1678 if (dictlookup(c->c_locals, s) != NULL) {
1679 err_setstr(SyntaxError, "name is local and global");
1680 c->c_errors++;
1681 }
1682 else if (dictinsert(c->c_globals, s, None) != 0)
Guido van Rossumc5e96291991-12-10 13:53:51 +00001683 c->c_errors++;
1684 }
1685}
1686
Guido van Rossum681d79a1995-07-18 14:51:37 +00001687static int
1688com_newlocal_o(c, nameval)
1689 struct compiling *c;
1690 object *nameval;
1691{
1692 int i;
1693 object *ival;
1694 if (getlistsize(c->c_varnames) != c->c_nlocals) {
1695 /* This is usually caused by an error on a previous call */
1696 if (c->c_errors == 0) {
1697 err_setstr(SystemError, "mixed up var name/index");
1698 c->c_errors++;
1699 }
1700 return 0;
1701 }
1702 ival = newintobject(i = c->c_nlocals++);
1703 if (ival == NULL)
1704 c->c_errors++;
1705 else if (mappinginsert(c->c_locals, nameval, ival) != 0)
1706 c->c_errors++;
1707 else if (addlistitem(c->c_varnames, nameval) != 0)
1708 c->c_errors++;
1709 XDECREF(ival);
1710 return i;
1711}
1712
1713static int
1714com_addlocal_o(c, nameval)
1715 struct compiling *c;
1716 object *nameval;
1717{
1718 object *ival = mappinglookup(c->c_locals, nameval);
1719 if (ival != NULL)
1720 return getintvalue(ival);
1721 return com_newlocal_o(c, nameval);
1722}
1723
1724static int
1725com_newlocal(c, name)
1726 struct compiling *c;
1727 char *name;
1728{
1729 object *nameval = newstringobject(name);
1730 int i;
1731 if (nameval == NULL) {
1732 c->c_errors++;
1733 return 0;
1734 }
1735 i = com_newlocal_o(c, nameval);
1736 DECREF(nameval);
1737 return i;
1738}
1739
Guido van Rossum25831651993-05-19 14:50:45 +00001740#define strequ(a, b) (strcmp((a), (b)) == 0)
1741
1742static void
1743com_access_stmt(c, n)
1744 struct compiling *c;
1745 node *n;
1746{
Guido van Rossum53f45241995-10-08 00:42:46 +00001747#if 0
Guido van Rossum25831651993-05-19 14:50:45 +00001748 int i, j, k, mode, imode;
1749 object *vmode;
1750 REQ(n, access_stmt);
1751 /* 'access' NAME (',' NAME)* ':' accesstype (',' accesstype)*
1752 accesstype: NAME+ */
1753
1754 /* Find where the colon is */
1755 i = 1;
1756 while (TYPE(CHILD(n,i-1)) != COLON)
1757 i += 1;
1758
1759 /* Calculate the mode mask */
1760 mode = 0;
1761 for (j = i; j < NCH(n); j += 2) {
Guido van Rossum81daa321993-05-20 14:24:46 +00001762 int r = 0, w = 0, p = 0;
1763 for (k = 0; k < NCH(CHILD(n,j)); k++) {
Guido van Rossum25831651993-05-19 14:50:45 +00001764 if (strequ(STR(CHILD(CHILD(n,j),k)), "public"))
1765 p = 0;
1766 else if (strequ(STR(CHILD(CHILD(n,j),k)), "protected"))
1767 p = 1;
1768 else if (strequ(STR(CHILD(CHILD(n,j),k)), "private"))
1769 p = 2;
1770 else if (strequ(STR(CHILD(CHILD(n,j),k)), "read"))
1771 r = 1;
1772 else if (strequ(STR(CHILD(CHILD(n,j),k)), "write"))
1773 w = 1;
1774 else /* XXX should make this an exception */
1775 fprintf(stderr, "bad access type %s\n",
1776 STR(CHILD(CHILD(n,j),k)));
1777 }
1778 if (r == 0 && w == 0)
Guido van Rossum81daa321993-05-20 14:24:46 +00001779 r = w = 1;
Guido van Rossum25831651993-05-19 14:50:45 +00001780 if (p == 0) {
1781 if (r == 1) mode |= AC_R_PUBLIC;
1782 if (w == 1) mode |= AC_W_PUBLIC;
1783 } else if (p == 1) {
1784 if (r == 1) mode |= AC_R_PROTECTED;
1785 if (w == 1) mode |= AC_W_PROTECTED;
1786 } else {
1787 if (r == 1) mode |= AC_R_PRIVATE;
1788 if (w == 1) mode |= AC_W_PRIVATE;
1789 }
1790 }
1791 vmode = newintobject((long)mode);
1792 imode = com_addconst(c, vmode);
1793 XDECREF(vmode);
1794 for (i = 1; TYPE(CHILD(n,i-1)) != COLON; i+=2) {
1795 com_addoparg(c, LOAD_CONST, imode);
1796 com_addopname(c, ACCESS_MODE, CHILD(n, i));
1797 }
Guido van Rossum53f45241995-10-08 00:42:46 +00001798#endif
Guido van Rossum25831651993-05-19 14:50:45 +00001799}
1800
Guido van Rossumc5e96291991-12-10 13:53:51 +00001801static void
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001802com_exec_stmt(c, n)
1803 struct compiling *c;
1804 node *n;
1805{
1806 REQ(n, exec_stmt);
1807 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
1808 com_node(c, CHILD(n, 1));
1809 if (NCH(n) >= 4)
1810 com_node(c, CHILD(n, 3));
1811 else
1812 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
1813 if (NCH(n) >= 6)
1814 com_node(c, CHILD(n, 5));
1815 else
1816 com_addbyte(c, DUP_TOP);
1817 com_addbyte(c, EXEC_STMT);
1818}
1819
1820static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001821com_if_stmt(c, n)
1822 struct compiling *c;
1823 node *n;
1824{
1825 int i;
1826 int anchor = 0;
1827 REQ(n, if_stmt);
1828 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
1829 for (i = 0; i+3 < NCH(n); i+=4) {
1830 int a = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001831 node *ch = CHILD(n, i+1);
1832 if (i > 0)
1833 com_addoparg(c, SET_LINENO, ch->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001834 com_node(c, CHILD(n, i+1));
1835 com_addfwref(c, JUMP_IF_FALSE, &a);
1836 com_addbyte(c, POP_TOP);
1837 com_node(c, CHILD(n, i+3));
1838 com_addfwref(c, JUMP_FORWARD, &anchor);
1839 com_backpatch(c, a);
1840 com_addbyte(c, POP_TOP);
1841 }
1842 if (i+2 < NCH(n))
1843 com_node(c, CHILD(n, i+2));
1844 com_backpatch(c, anchor);
1845}
1846
1847static void
1848com_while_stmt(c, n)
1849 struct compiling *c;
1850 node *n;
1851{
1852 int break_anchor = 0;
1853 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001854 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001855 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
1856 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001857 block_push(c, SETUP_LOOP);
1858 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001859 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001860 com_node(c, CHILD(n, 1));
1861 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1862 com_addbyte(c, POP_TOP);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001863 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001864 com_node(c, CHILD(n, 3));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001865 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001866 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1867 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001868 com_backpatch(c, anchor);
1869 com_addbyte(c, POP_TOP);
1870 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001871 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001872 if (NCH(n) > 4)
1873 com_node(c, CHILD(n, 6));
1874 com_backpatch(c, break_anchor);
1875}
1876
1877static void
1878com_for_stmt(c, n)
1879 struct compiling *c;
1880 node *n;
1881{
1882 object *v;
1883 int break_anchor = 0;
1884 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001885 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001886 REQ(n, for_stmt);
1887 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
1888 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001889 block_push(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001890 com_node(c, CHILD(n, 3));
1891 v = newintobject(0L);
1892 if (v == NULL)
1893 c->c_errors++;
1894 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
1895 XDECREF(v);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001896 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001897 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001898 com_addfwref(c, FOR_LOOP, &anchor);
1899 com_assign(c, CHILD(n, 1), 1/*assigning*/);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001900 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001901 com_node(c, CHILD(n, 5));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001902 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001903 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1904 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001905 com_backpatch(c, anchor);
1906 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001907 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001908 if (NCH(n) > 8)
1909 com_node(c, CHILD(n, 8));
1910 com_backpatch(c, break_anchor);
1911}
1912
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001913/* Code generated for "try: S finally: Sf" is as follows:
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001914
1915 SETUP_FINALLY L
1916 <code for S>
1917 POP_BLOCK
1918 LOAD_CONST <nil>
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001919 L: <code for Sf>
1920 END_FINALLY
1921
1922 The special instructions use the block stack. Each block
1923 stack entry contains the instruction that created it (here
1924 SETUP_FINALLY), the level of the value stack at the time the
1925 block stack entry was created, and a label (here L).
1926
1927 SETUP_FINALLY:
1928 Pushes the current value stack level and the label
1929 onto the block stack.
1930 POP_BLOCK:
1931 Pops en entry from the block stack, and pops the value
1932 stack until its level is the same as indicated on the
1933 block stack. (The label is ignored.)
1934 END_FINALLY:
Guido van Rossum3f5da241990-12-20 15:06:42 +00001935 Pops a variable number of entries from the *value* stack
1936 and re-raises the exception they specify. The number of
1937 entries popped depends on the (pseudo) exception type.
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001938
1939 The block stack is unwound when an exception is raised:
1940 when a SETUP_FINALLY entry is found, the exception is pushed
1941 onto the value stack (and the exception condition is cleared),
1942 and the interpreter jumps to the label gotten from the block
1943 stack.
1944
1945 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
Guido van Rossum3f5da241990-12-20 15:06:42 +00001946 (The contents of the value stack is shown in [], with the top
1947 at the right; 'tb' is trace-back info, 'val' the exception's
1948 associated value, and 'exc' the exception.)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001949
1950 Value stack Label Instruction Argument
1951 [] SETUP_EXCEPT L1
1952 [] <code for S>
1953 [] POP_BLOCK
1954 [] JUMP_FORWARD L0
1955
Guido van Rossum3f5da241990-12-20 15:06:42 +00001956 [tb, val, exc] L1: DUP )
1957 [tb, val, exc, exc] <evaluate E1> )
1958 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1959 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1960 [tb, val, exc, 1] POP )
1961 [tb, val, exc] POP
1962 [tb, val] <assign to V1> (or POP if no V1)
1963 [tb] POP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001964 [] <code for S1>
1965 JUMP_FORWARD L0
1966
Guido van Rossum3f5da241990-12-20 15:06:42 +00001967 [tb, val, exc, 0] L2: POP
1968 [tb, val, exc] DUP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001969 .............................etc.......................
1970
Guido van Rossum3f5da241990-12-20 15:06:42 +00001971 [tb, val, exc, 0] Ln+1: POP
1972 [tb, val, exc] END_FINALLY # re-raise exception
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001973
1974 [] L0: <next statement>
1975
1976 Of course, parts are not generated if Vi or Ei is not present.
1977*/
1978
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001979static void
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001980com_try_except(c, n)
1981 struct compiling *c;
1982 node *n;
1983{
1984 int except_anchor = 0;
1985 int end_anchor = 0;
1986 int else_anchor = 0;
1987 int i;
1988 node *ch;
1989
1990 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
1991 block_push(c, SETUP_EXCEPT);
1992 com_node(c, CHILD(n, 2));
1993 com_addbyte(c, POP_BLOCK);
1994 block_pop(c, SETUP_EXCEPT);
1995 com_addfwref(c, JUMP_FORWARD, &else_anchor);
1996 com_backpatch(c, except_anchor);
1997 for (i = 3;
1998 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
1999 i += 3) {
2000 /* except_clause: 'except' [expr [',' expr]] */
2001 if (except_anchor == 0) {
2002 err_setstr(SyntaxError,
2003 "default 'except:' must be last");
2004 c->c_errors++;
2005 break;
2006 }
2007 except_anchor = 0;
2008 com_addoparg(c, SET_LINENO, ch->n_lineno);
2009 if (NCH(ch) > 1) {
2010 com_addbyte(c, DUP_TOP);
2011 com_node(c, CHILD(ch, 1));
2012 com_addoparg(c, COMPARE_OP, EXC_MATCH);
2013 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
2014 com_addbyte(c, POP_TOP);
2015 }
2016 com_addbyte(c, POP_TOP);
2017 if (NCH(ch) > 3)
2018 com_assign(c, CHILD(ch, 3), 1/*assigning*/);
2019 else
2020 com_addbyte(c, POP_TOP);
2021 com_addbyte(c, POP_TOP);
2022 com_node(c, CHILD(n, i+2));
2023 com_addfwref(c, JUMP_FORWARD, &end_anchor);
2024 if (except_anchor) {
2025 com_backpatch(c, except_anchor);
2026 com_addbyte(c, POP_TOP);
2027 }
2028 }
2029 com_addbyte(c, END_FINALLY);
2030 com_backpatch(c, else_anchor);
2031 if (i < NCH(n))
2032 com_node(c, CHILD(n, i+2));
2033 com_backpatch(c, end_anchor);
2034}
2035
2036static void
2037com_try_finally(c, n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002038 struct compiling *c;
2039 node *n;
2040{
2041 int finally_anchor = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002042 node *ch;
Guido van Rossum94fb82e1992-04-05 14:24:50 +00002043
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002044 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
2045 block_push(c, SETUP_FINALLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002046 com_node(c, CHILD(n, 2));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002047 com_addbyte(c, POP_BLOCK);
2048 block_pop(c, SETUP_FINALLY);
2049 block_push(c, END_FINALLY);
2050 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
2051 com_backpatch(c, finally_anchor);
2052 ch = CHILD(n, NCH(n)-1);
2053 com_addoparg(c, SET_LINENO, ch->n_lineno);
2054 com_node(c, ch);
2055 com_addbyte(c, END_FINALLY);
2056 block_pop(c, END_FINALLY);
2057}
2058
2059static void
2060com_try_stmt(c, n)
2061 struct compiling *c;
2062 node *n;
2063{
2064 REQ(n, try_stmt);
2065 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
2066 | 'try' ':' suite 'finally' ':' suite */
2067 if (TYPE(CHILD(n, 3)) != except_clause)
2068 com_try_finally(c, n);
2069 else
2070 com_try_except(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002071}
2072
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002073static object *
2074get_docstring(n)
2075 node *n;
2076{
Guido van Rossum164d4ff1995-01-26 00:40:09 +00002077 int i;
2078
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002079 switch (TYPE(n)) {
2080
2081 case suite:
2082 if (NCH(n) == 1)
2083 return get_docstring(CHILD(n, 0));
2084 else {
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002085 for (i = 0; i < NCH(n); i++) {
2086 node *ch = CHILD(n, i);
2087 if (TYPE(ch) == stmt)
2088 return get_docstring(ch);
2089 }
2090 }
2091 break;
2092
Guido van Rossum164d4ff1995-01-26 00:40:09 +00002093 case file_input:
2094 for (i = 0; i < NCH(n); i++) {
2095 node *ch = CHILD(n, i);
2096 if (TYPE(ch) == stmt)
2097 return get_docstring(ch);
2098 }
2099 break;
2100
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002101 case stmt:
2102 case simple_stmt:
2103 case small_stmt:
2104 return get_docstring(CHILD(n, 0));
2105
2106 case expr_stmt:
2107 case testlist:
2108 case test:
2109 case and_test:
2110 case not_test:
2111 case comparison:
2112 case expr:
2113 case xor_expr:
2114 case and_expr:
2115 case shift_expr:
2116 case arith_expr:
2117 case term:
2118 case factor:
Guido van Rossum50564e81996-01-12 01:13:16 +00002119 case power:
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002120 if (NCH(n) == 1)
2121 return get_docstring(CHILD(n, 0));
2122 break;
2123
2124 case atom:
2125 if (TYPE(CHILD(n, 0)) == STRING)
2126 return parsestrplus(n);
2127 break;
2128
2129 }
2130 return NULL;
2131}
2132
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002133static void
2134com_suite(c, n)
2135 struct compiling *c;
2136 node *n;
2137{
2138 REQ(n, suite);
2139 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
2140 if (NCH(n) == 1) {
2141 com_node(c, CHILD(n, 0));
2142 }
2143 else {
2144 int i;
2145 for (i = 0; i < NCH(n); i++) {
2146 node *ch = CHILD(n, i);
2147 if (TYPE(ch) == stmt)
2148 com_node(c, ch);
2149 }
2150 }
2151}
2152
Guido van Rossumf1aeab71992-03-27 17:28:26 +00002153/* ARGSUSED */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002154static void
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002155com_continue_stmt(c, n)
2156 struct compiling *c;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00002157 node *n; /* Not used, but passed for consistency */
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002158{
2159 int i = c->c_nblocks;
2160 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
2161 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2162 }
2163 else {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00002164 err_setstr(SyntaxError, "'continue' not properly in loop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002165 c->c_errors++;
2166 }
2167 /* XXX Could allow it inside a 'finally' clause
2168 XXX if we could pop the exception still on the stack */
2169}
2170
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002171static int
Guido van Rossum681d79a1995-07-18 14:51:37 +00002172com_argdefs(c, n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002173 struct compiling *c;
2174 node *n;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002175{
Guido van Rossum681d79a1995-07-18 14:51:37 +00002176 int i, nch, nargs, ndefs;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002177 if (TYPE(n) == lambdef) {
2178 /* lambdef: 'lambda' [varargslist] ':' test */
2179 n = CHILD(n, 1);
2180 }
2181 else {
2182 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
2183 n = CHILD(n, 2);
2184 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
2185 n = CHILD(n, 1);
2186 }
2187 if (TYPE(n) != varargslist)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002188 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002189 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00002190 (fpdef ['=' test] ',')* '*' ....... |
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002191 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
2192 nch = NCH(n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002193 nargs = 0;
2194 ndefs = 0;
2195 for (i = 0; i < nch; i++) {
2196 int t;
Guido van Rossum50564e81996-01-12 01:13:16 +00002197 if (TYPE(CHILD(n, i)) == STAR || TYPE(CHILD(n, i)) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00002198 break;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002199 nargs++;
2200 i++;
2201 if (i >= nch)
Guido van Rossuma1e7e621995-09-18 21:44:04 +00002202 t = RPAR; /* Anything except EQUAL or COMMA */
2203 else
2204 t = TYPE(CHILD(n, i));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002205 if (t == EQUAL) {
2206 i++;
2207 ndefs++;
2208 com_node(c, CHILD(n, i));
2209 i++;
2210 if (i >= nch)
2211 break;
2212 t = TYPE(CHILD(n, i));
2213 }
2214 else {
2215 /* Treat "(a=1, b)" as "(a=1, b=None)" */
2216 if (ndefs) {
2217 com_addoparg(c, LOAD_CONST,
2218 com_addconst(c, None));
2219 ndefs++;
2220 }
2221 }
2222 if (t != COMMA)
2223 break;
2224 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002225 return ndefs;
2226}
2227
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002228static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002229com_funcdef(c, n)
2230 struct compiling *c;
2231 node *n;
2232{
2233 object *v;
2234 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum590baa41993-11-30 13:40:46 +00002235 v = (object *)compile(n, c->c_filename);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002236 if (v == NULL)
2237 c->c_errors++;
2238 else {
2239 int i = com_addconst(c, v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002240 int ndefs = com_argdefs(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002241 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002242 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002243 com_addopname(c, STORE_NAME, CHILD(n, 1));
2244 DECREF(v);
2245 }
2246}
2247
2248static void
Guido van Rossum25831651993-05-19 14:50:45 +00002249com_bases(c, n)
Guido van Rossumc5e96291991-12-10 13:53:51 +00002250 struct compiling *c;
2251 node *n;
2252{
Guido van Rossumf1aeab71992-03-27 17:28:26 +00002253 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00002254 REQ(n, testlist);
2255 /* testlist: test (',' test)* [','] */
2256 for (i = 0; i < NCH(n); i += 2)
2257 com_node(c, CHILD(n, i));
2258 com_addoparg(c, BUILD_TUPLE, (NCH(n)+1) / 2);
2259}
2260
2261static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002262com_classdef(c, n)
2263 struct compiling *c;
2264 node *n;
2265{
Guido van Rossum25831651993-05-19 14:50:45 +00002266 int i;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002267 object *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002268 REQ(n, classdef);
Guido van Rossum25831651993-05-19 14:50:45 +00002269 /* classdef: class NAME ['(' testlist ')'] ':' suite */
2270 if ((v = newstringobject(STR(CHILD(n, 1)))) == NULL) {
2271 c->c_errors++;
2272 return;
2273 }
2274 /* Push the class name on the stack */
2275 i = com_addconst(c, v);
2276 com_addoparg(c, LOAD_CONST, i);
2277 DECREF(v);
2278 /* Push the tuple of base classes on the stack */
2279 if (TYPE(CHILD(n, 2)) != LPAR)
Guido van Rossumc5e96291991-12-10 13:53:51 +00002280 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum25831651993-05-19 14:50:45 +00002281 else
2282 com_bases(c, CHILD(n, 3));
Guido van Rossum590baa41993-11-30 13:40:46 +00002283 v = (object *)compile(n, c->c_filename);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002284 if (v == NULL)
2285 c->c_errors++;
2286 else {
Guido van Rossum25831651993-05-19 14:50:45 +00002287 i = com_addconst(c, v);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002288 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002289 com_addoparg(c, MAKE_FUNCTION, 0);
2290 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002291 com_addbyte(c, BUILD_CLASS);
2292 com_addopname(c, STORE_NAME, CHILD(n, 1));
2293 DECREF(v);
2294 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002295}
2296
2297static void
2298com_node(c, n)
2299 struct compiling *c;
2300 node *n;
2301{
2302 switch (TYPE(n)) {
2303
2304 /* Definition nodes */
2305
2306 case funcdef:
2307 com_funcdef(c, n);
2308 break;
2309 case classdef:
2310 com_classdef(c, n);
2311 break;
2312
2313 /* Trivial parse tree nodes */
2314
2315 case stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002316 case small_stmt:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002317 case flow_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002318 com_node(c, CHILD(n, 0));
2319 break;
2320
2321 case simple_stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002322 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
2323 com_addoparg(c, SET_LINENO, n->n_lineno);
2324 {
2325 int i;
2326 for (i = 0; i < NCH(n)-1; i += 2)
2327 com_node(c, CHILD(n, i));
2328 }
2329 break;
2330
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002331 case compound_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002332 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002333 com_node(c, CHILD(n, 0));
2334 break;
2335
2336 /* Statement nodes */
2337
2338 case expr_stmt:
2339 com_expr_stmt(c, n);
2340 break;
2341 case print_stmt:
2342 com_print_stmt(c, n);
2343 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002344 case del_stmt: /* 'del' exprlist */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002345 com_assign(c, CHILD(n, 1), 0/*delete*/);
2346 break;
2347 case pass_stmt:
2348 break;
2349 case break_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002350 if (c->c_loops == 0) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00002351 err_setstr(SyntaxError, "'break' outside loop");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002352 c->c_errors++;
2353 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002354 com_addbyte(c, BREAK_LOOP);
2355 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002356 case continue_stmt:
2357 com_continue_stmt(c, n);
2358 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002359 case return_stmt:
2360 com_return_stmt(c, n);
2361 break;
2362 case raise_stmt:
2363 com_raise_stmt(c, n);
2364 break;
2365 case import_stmt:
2366 com_import_stmt(c, n);
2367 break;
Guido van Rossumc5e96291991-12-10 13:53:51 +00002368 case global_stmt:
2369 com_global_stmt(c, n);
2370 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002371 case access_stmt:
2372 com_access_stmt(c, n);
2373 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002374 case exec_stmt:
2375 com_exec_stmt(c, n);
2376 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002377 case if_stmt:
2378 com_if_stmt(c, n);
2379 break;
2380 case while_stmt:
2381 com_while_stmt(c, n);
2382 break;
2383 case for_stmt:
2384 com_for_stmt(c, n);
2385 break;
2386 case try_stmt:
2387 com_try_stmt(c, n);
2388 break;
2389 case suite:
2390 com_suite(c, n);
2391 break;
2392
2393 /* Expression nodes */
2394
2395 case testlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00002396 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002397 break;
2398 case test:
2399 com_test(c, n);
2400 break;
2401 case and_test:
2402 com_and_test(c, n);
2403 break;
2404 case not_test:
2405 com_not_test(c, n);
2406 break;
2407 case comparison:
2408 com_comparison(c, n);
2409 break;
2410 case exprlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00002411 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002412 break;
2413 case expr:
2414 com_expr(c, n);
2415 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +00002416 case xor_expr:
2417 com_xor_expr(c, n);
2418 break;
2419 case and_expr:
2420 com_and_expr(c, n);
2421 break;
2422 case shift_expr:
2423 com_shift_expr(c, n);
2424 break;
2425 case arith_expr:
2426 com_arith_expr(c, n);
2427 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002428 case term:
2429 com_term(c, n);
2430 break;
2431 case factor:
2432 com_factor(c, n);
2433 break;
Guido van Rossum50564e81996-01-12 01:13:16 +00002434 case power:
2435 com_power(c, n);
2436 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002437 case atom:
2438 com_atom(c, n);
2439 break;
2440
2441 default:
2442 fprintf(stderr, "node type %d\n", TYPE(n));
2443 err_setstr(SystemError, "com_node: unexpected node type");
2444 c->c_errors++;
2445 }
2446}
2447
2448static void com_fplist PROTO((struct compiling *, node *));
2449
2450static void
2451com_fpdef(c, n)
2452 struct compiling *c;
2453 node *n;
2454{
2455 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
2456 if (TYPE(CHILD(n, 0)) == LPAR)
2457 com_fplist(c, CHILD(n, 1));
2458 else
Guido van Rossum681d79a1995-07-18 14:51:37 +00002459 com_addoparg(c, STORE_FAST, com_newlocal(c, STR(CHILD(n, 0))));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002460}
2461
2462static void
2463com_fplist(c, n)
2464 struct compiling *c;
2465 node *n;
2466{
Guido van Rossuma9df32a1991-12-31 13:13:35 +00002467 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002468 if (NCH(n) == 1) {
2469 com_fpdef(c, CHILD(n, 0));
2470 }
2471 else {
2472 int i;
2473 com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2);
2474 for (i = 0; i < NCH(n); i += 2)
2475 com_fpdef(c, CHILD(n, i));
2476 }
2477}
2478
2479static void
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002480com_arglist(c, n)
2481 struct compiling *c;
2482 node *n;
2483{
Guido van Rossum681d79a1995-07-18 14:51:37 +00002484 int nch, i;
2485 int complex = 0;
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002486 REQ(n, varargslist);
Guido van Rossumacbefef1992-01-19 16:33:51 +00002487 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00002488 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002489 nch = NCH(n);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002490 /* Enter all arguments in table of locals */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002491 for (i = 0; i < nch; i++) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002492 node *ch = CHILD(n, i);
2493 node *fp;
2494 char *name;
Guido van Rossum50564e81996-01-12 01:13:16 +00002495 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00002496 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002497 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
2498 fp = CHILD(ch, 0);
2499 if (TYPE(fp) == NAME)
2500 name = STR(fp);
2501 else {
2502 name = "";
2503 complex= 1;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002504 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002505 com_newlocal(c, name);
2506 c->c_argcount++;
2507 if (++i >= nch)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002508 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002509 ch = CHILD(n, i);
2510 if (TYPE(ch) == EQUAL)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002511 i += 2;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002512 else
2513 REQ(ch, COMMA);
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002514 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002515 /* Handle *arguments */
2516 if (i < nch) {
2517 node *ch;
2518 ch = CHILD(n, i);
Guido van Rossum50564e81996-01-12 01:13:16 +00002519 if (TYPE(ch) != DOUBLESTAR) {
2520 REQ(ch, STAR);
2521 ch = CHILD(n, i+1);
2522 if (TYPE(ch) == NAME) {
2523 c->c_flags |= CO_VARARGS;
2524 i += 3;
2525 com_newlocal(c, STR(ch));
2526 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002527 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002528 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002529 /* Handle **keywords */
2530 if (i < nch) {
2531 node *ch;
2532 ch = CHILD(n, i);
Guido van Rossum50564e81996-01-12 01:13:16 +00002533 if (TYPE(ch) != DOUBLESTAR) {
2534 REQ(ch, STAR);
2535 ch = CHILD(n, i+1);
2536 REQ(ch, STAR);
2537 ch = CHILD(n, i+2);
2538 }
2539 else
2540 ch = CHILD(n, i+1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002541 REQ(ch, NAME);
2542 c->c_flags |= CO_VARKEYWORDS;
2543 com_newlocal(c, STR(ch));
2544 }
2545 if (complex) {
2546 /* Generate code for complex arguments only after
2547 having counted the simple arguments */
2548 int ilocal = 0;
2549 for (i = 0; i < nch; i++) {
2550 node *ch = CHILD(n, i);
2551 node *fp;
Guido van Rossum50564e81996-01-12 01:13:16 +00002552 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002553 break;
2554 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
2555 fp = CHILD(ch, 0);
2556 if (TYPE(fp) != NAME) {
2557 com_addoparg(c, LOAD_FAST, ilocal);
2558 com_fpdef(c, ch);
2559 }
2560 ilocal++;
2561 if (++i >= nch)
2562 break;
2563 ch = CHILD(n, i);
2564 if (TYPE(ch) == EQUAL)
2565 i += 2;
2566 else
2567 REQ(ch, COMMA);
2568 }
2569 }
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002570}
2571
2572static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002573com_file_input(c, n)
2574 struct compiling *c;
2575 node *n;
2576{
2577 int i;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002578 object *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002579 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002580 doc = get_docstring(n);
2581 if (doc != NULL) {
2582 int i = com_addconst(c, doc);
2583 DECREF(doc);
2584 com_addoparg(c, LOAD_CONST, i);
2585 com_addopnamestr(c, STORE_NAME, "__doc__");
2586 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002587 for (i = 0; i < NCH(n); i++) {
2588 node *ch = CHILD(n, i);
2589 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
2590 com_node(c, ch);
2591 }
2592}
2593
2594/* Top-level compile-node interface */
2595
2596static void
2597compile_funcdef(c, n)
2598 struct compiling *c;
2599 node *n;
2600{
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002601 object *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002602 node *ch;
2603 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum9bfef441993-03-29 10:43:31 +00002604 c->c_name = STR(CHILD(n, 1));
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002605 doc = get_docstring(CHILD(n, 4));
2606 if (doc != NULL) {
2607 (void) com_addconst(c, doc);
2608 DECREF(doc);
2609 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002610 else
2611 (void) com_addconst(c, None); /* No docstring */
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002612 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
2613 ch = CHILD(ch, 1); /* ')' | varargslist */
Guido van Rossum681d79a1995-07-18 14:51:37 +00002614 if (TYPE(ch) == varargslist)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002615 com_arglist(c, ch);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002616 c->c_infunction = 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002617 com_node(c, CHILD(n, 4));
Guido van Rossum3f5da241990-12-20 15:06:42 +00002618 c->c_infunction = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002619 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
2620 com_addbyte(c, RETURN_VALUE);
2621}
2622
2623static void
Guido van Rossum590baa41993-11-30 13:40:46 +00002624compile_lambdef(c, n)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002625 struct compiling *c;
2626 node *n;
2627{
Guido van Rossum590baa41993-11-30 13:40:46 +00002628 node *ch;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002629 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
Guido van Rossum363ac7d1994-11-10 22:40:34 +00002630 c->c_name = "<lambda>";
Guido van Rossum590baa41993-11-30 13:40:46 +00002631
2632 ch = CHILD(n, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002633 (void) com_addconst(c, None); /* No docstring */
2634 if (TYPE(ch) == varargslist) {
Guido van Rossum590baa41993-11-30 13:40:46 +00002635 com_arglist(c, ch);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002636 ch = CHILD(n, 3);
Guido van Rossum590baa41993-11-30 13:40:46 +00002637 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002638 else
2639 ch = CHILD(n, 2);
2640 com_node(c, ch);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002641 com_addbyte(c, RETURN_VALUE);
2642}
2643
2644static void
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002645compile_classdef(c, n)
2646 struct compiling *c;
2647 node *n;
2648{
2649 node *ch;
2650 object *doc;
2651 REQ(n, classdef);
2652 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
2653 c->c_name = STR(CHILD(n, 1));
2654 ch = CHILD(n, NCH(n)-1); /* The suite */
2655 doc = get_docstring(ch);
2656 if (doc != NULL) {
2657 int i = com_addconst(c, doc);
2658 DECREF(doc);
2659 com_addoparg(c, LOAD_CONST, i);
2660 com_addopnamestr(c, STORE_NAME, "__doc__");
2661 }
2662 else
2663 (void) com_addconst(c, None);
2664 com_node(c, ch);
2665 com_addbyte(c, LOAD_LOCALS);
2666 com_addbyte(c, RETURN_VALUE);
2667}
2668
2669static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002670compile_node(c, n)
2671 struct compiling *c;
2672 node *n;
2673{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002674 com_addoparg(c, SET_LINENO, n->n_lineno);
2675
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002676 switch (TYPE(n)) {
2677
Guido van Rossum4c417781991-01-21 16:09:22 +00002678 case single_input: /* One interactive command */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002679 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002680 c->c_interactive++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002681 n = CHILD(n, 0);
2682 if (TYPE(n) != NEWLINE)
2683 com_node(c, n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002684 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
2685 com_addbyte(c, RETURN_VALUE);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002686 c->c_interactive--;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002687 break;
2688
Guido van Rossum4c417781991-01-21 16:09:22 +00002689 case file_input: /* A whole file, or built-in function exec() */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002690 com_file_input(c, n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002691 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
2692 com_addbyte(c, RETURN_VALUE);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002693 break;
2694
Guido van Rossum590baa41993-11-30 13:40:46 +00002695 case eval_input: /* Built-in function input() */
Guido van Rossum4c417781991-01-21 16:09:22 +00002696 com_node(c, CHILD(n, 0));
2697 com_addbyte(c, RETURN_VALUE);
2698 break;
2699
Guido van Rossum590baa41993-11-30 13:40:46 +00002700 case lambdef: /* anonymous function definition */
2701 compile_lambdef(c, n);
2702 break;
2703
Guido van Rossum4c417781991-01-21 16:09:22 +00002704 case funcdef: /* A function definition */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002705 compile_funcdef(c, n);
2706 break;
2707
Guido van Rossum4c417781991-01-21 16:09:22 +00002708 case classdef: /* A class definition */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002709 compile_classdef(c, n);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002710 break;
2711
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002712 default:
2713 fprintf(stderr, "node type %d\n", TYPE(n));
2714 err_setstr(SystemError, "compile_node: unexpected node type");
2715 c->c_errors++;
2716 }
2717}
2718
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002719/* Optimization for local variables in functions (and *only* functions).
Guido van Rossum282914b1991-04-04 10:42:56 +00002720
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002721 This replaces all LOAD_NAME, STORE_NAME and DELETE_NAME
2722 instructions that refer to local variables with LOAD_FAST etc.
2723 The latter instructions are much faster because they don't need to
2724 look up the variable name in a dictionary.
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002725
Guido van Rossum681d79a1995-07-18 14:51:37 +00002726 To find all local variables, we check all STORE_NAME, IMPORT_FROM
2727 and DELETE_NAME instructions. This yields all local variables,
2728 function definitions, class definitions and import statements.
2729 Argument names have already been entered into the list by the
2730 special processing for the argument list.
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002731
2732 All remaining LOAD_NAME instructions must refer to non-local (global
2733 or builtin) variables, so are replaced by LOAD_GLOBAL.
2734
2735 There are two problems: 'from foo import *' and 'exec' may introduce
2736 local variables that we can't know while compiling. If this is the
Guido van Rossum681d79a1995-07-18 14:51:37 +00002737 case, we can still optimize bona fide locals (since those
2738 statements will be surrounded by fast_2_locals() and
2739 locals_2_fast()), but we can't change LOAD_NAME to LOAD_GLOBAL.
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002740
Guido van Rossum681d79a1995-07-18 14:51:37 +00002741 NB: this modifies the string object c->c_code! */
Guido van Rossum282914b1991-04-04 10:42:56 +00002742
2743static void
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002744optimize(c)
2745 struct compiling *c;
Guido van Rossum282914b1991-04-04 10:42:56 +00002746{
Guido van Rossum0a697f61991-04-16 08:39:12 +00002747 unsigned char *next_instr, *cur_instr;
Guido van Rossum282914b1991-04-04 10:42:56 +00002748 int opcode;
2749 int oparg;
2750 object *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002751 object *error_type, *error_value, *error_traceback;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002752
Guido van Rossum282914b1991-04-04 10:42:56 +00002753#define NEXTOP() (*next_instr++)
2754#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
2755#define GETITEM(v, i) (getlistitem((v), (i)))
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002756#define GETNAMEOBJ(i) (GETITEM(c->c_names, (i)))
2757
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002758 err_fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002759
2760 c->c_flags |= CO_OPTIMIZED;
Guido van Rossum282914b1991-04-04 10:42:56 +00002761
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002762 next_instr = (unsigned char *) getstringvalue(c->c_code);
Guido van Rossum282914b1991-04-04 10:42:56 +00002763 for (;;) {
2764 opcode = NEXTOP();
2765 if (opcode == STOP_CODE)
2766 break;
2767 if (HAS_ARG(opcode))
2768 oparg = NEXTARG();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002769 switch (opcode) {
2770 case STORE_NAME:
2771 case DELETE_NAME:
2772 case IMPORT_FROM:
2773 com_addlocal_o(c, GETNAMEOBJ(oparg));
2774 break;
2775 case EXEC_STMT:
2776 c->c_flags &= ~CO_OPTIMIZED;
2777 break;
Guido van Rossum282914b1991-04-04 10:42:56 +00002778 }
2779 }
2780
Guido van Rossum681d79a1995-07-18 14:51:37 +00002781 if (dictlookup(c->c_locals, "*") != NULL)
2782 c->c_flags &= ~CO_OPTIMIZED;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002783
2784 next_instr = (unsigned char *) getstringvalue(c->c_code);
Guido van Rossum282914b1991-04-04 10:42:56 +00002785 for (;;) {
2786 cur_instr = next_instr;
2787 opcode = NEXTOP();
2788 if (opcode == STOP_CODE)
2789 break;
2790 if (HAS_ARG(opcode))
2791 oparg = NEXTARG();
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002792 if (opcode == LOAD_NAME ||
2793 opcode == STORE_NAME ||
2794 opcode == DELETE_NAME) {
2795 object *v;
2796 int i;
Guido van Rossum282914b1991-04-04 10:42:56 +00002797 name = GETNAMEOBJ(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002798 v = dict2lookup(c->c_locals, name);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002799 if (v == NULL) {
Guido van Rossum83163251991-08-16 08:58:43 +00002800 err_clear();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002801 if (opcode == LOAD_NAME &&
2802 (c->c_flags&CO_OPTIMIZED))
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002803 cur_instr[0] = LOAD_GLOBAL;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002804 continue;
Guido van Rossum83163251991-08-16 08:58:43 +00002805 }
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002806 i = getintvalue(v);
2807 switch (opcode) {
2808 case LOAD_NAME: cur_instr[0] = LOAD_FAST; break;
2809 case STORE_NAME: cur_instr[0] = STORE_FAST; break;
2810 case DELETE_NAME: cur_instr[0] = DELETE_FAST; break;
2811 }
2812 cur_instr[1] = i & 0xff;
2813 cur_instr[2] = (i>>8) & 0xff;
Guido van Rossum282914b1991-04-04 10:42:56 +00002814 }
2815 }
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002816
Guido van Rossum681d79a1995-07-18 14:51:37 +00002817 if (c->c_errors == 0)
2818 err_restore(error_type, error_value, error_traceback);
Guido van Rossum282914b1991-04-04 10:42:56 +00002819}
2820
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002821codeobject *
Guido van Rossum590baa41993-11-30 13:40:46 +00002822compile(n, filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002823 node *n;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002824 char *filename;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002825{
2826 struct compiling sc;
2827 codeobject *co;
Guido van Rossum590baa41993-11-30 13:40:46 +00002828 if (!com_init(&sc, filename))
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002829 return NULL;
2830 compile_node(&sc, n);
2831 com_done(&sc);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002832 if ((TYPE(n) == funcdef || TYPE(n) == lambdef) && sc.c_errors == 0) {
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002833 optimize(&sc);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002834 sc.c_flags |= CO_NEWLOCALS;
2835 }
2836 else if (TYPE(n) == classdef)
2837 sc.c_flags |= CO_NEWLOCALS;
Guido van Rossum9bfef441993-03-29 10:43:31 +00002838 co = NULL;
2839 if (sc.c_errors == 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002840 object *consts, *names, *varnames, *filename, *name;
2841 consts = listtuple(sc.c_consts);
2842 names = listtuple(sc.c_names);
2843 varnames = listtuple(sc.c_varnames);
2844 filename = newstringobject(sc.c_filename);
2845 name = newstringobject(sc.c_name);
2846 if (!err_occurred())
2847 co = newcodeobject(sc.c_argcount,
2848 sc.c_nlocals,
2849 sc.c_flags,
2850 sc.c_code,
2851 consts,
2852 names,
2853 varnames,
2854 filename,
2855 name);
2856 XDECREF(consts);
2857 XDECREF(names);
2858 XDECREF(varnames);
2859 XDECREF(filename);
2860 XDECREF(name);
Guido van Rossuma082ce41991-06-04 19:41:56 +00002861 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002862 com_free(&sc);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002863 return co;
2864}