blob: 2f6c46b9a5121388cc3bd652519055f16d3d62cd [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
575 imflag = *end == 'i' || *end == 'I' || *end == 'j' || *end == 'J';
576#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{
973 int i;
974 REQ(n, factor);
975 if (TYPE(CHILD(n, 0)) == PLUS) {
976 com_factor(c, CHILD(n, 1));
977 com_addbyte(c, UNARY_POSITIVE);
978 }
979 else if (TYPE(CHILD(n, 0)) == MINUS) {
980 com_factor(c, CHILD(n, 1));
981 com_addbyte(c, UNARY_NEGATIVE);
982 }
Guido van Rossum7928cd71991-10-24 14:59:31 +0000983 else if (TYPE(CHILD(n, 0)) == TILDE) {
984 com_factor(c, CHILD(n, 1));
985 com_addbyte(c, UNARY_INVERT);
986 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000987 else {
Guido van Rossum50564e81996-01-12 01:13:16 +0000988 com_power(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000989 }
990}
991
992static void
993com_term(c, n)
994 struct compiling *c;
995 node *n;
996{
997 int i;
998 int op;
999 REQ(n, term);
1000 com_factor(c, CHILD(n, 0));
1001 for (i = 2; i < NCH(n); i += 2) {
1002 com_factor(c, CHILD(n, i));
1003 switch (TYPE(CHILD(n, i-1))) {
1004 case STAR:
1005 op = BINARY_MULTIPLY;
1006 break;
1007 case SLASH:
1008 op = BINARY_DIVIDE;
1009 break;
1010 case PERCENT:
1011 op = BINARY_MODULO;
1012 break;
1013 default:
1014 err_setstr(SystemError,
Guido van Rossum7928cd71991-10-24 14:59:31 +00001015 "com_term: operator not *, / or %");
1016 c->c_errors++;
1017 op = 255;
1018 }
1019 com_addbyte(c, op);
1020 }
1021}
1022
1023static void
1024com_arith_expr(c, n)
1025 struct compiling *c;
1026 node *n;
1027{
1028 int i;
1029 int op;
1030 REQ(n, arith_expr);
1031 com_term(c, CHILD(n, 0));
1032 for (i = 2; i < NCH(n); i += 2) {
1033 com_term(c, CHILD(n, i));
1034 switch (TYPE(CHILD(n, i-1))) {
1035 case PLUS:
1036 op = BINARY_ADD;
1037 break;
1038 case MINUS:
1039 op = BINARY_SUBTRACT;
1040 break;
1041 default:
1042 err_setstr(SystemError,
1043 "com_arith_expr: operator not + or -");
1044 c->c_errors++;
1045 op = 255;
1046 }
1047 com_addbyte(c, op);
1048 }
1049}
1050
1051static void
1052com_shift_expr(c, n)
1053 struct compiling *c;
1054 node *n;
1055{
1056 int i;
1057 int op;
1058 REQ(n, shift_expr);
1059 com_arith_expr(c, CHILD(n, 0));
1060 for (i = 2; i < NCH(n); i += 2) {
1061 com_arith_expr(c, CHILD(n, i));
1062 switch (TYPE(CHILD(n, i-1))) {
1063 case LEFTSHIFT:
1064 op = BINARY_LSHIFT;
1065 break;
1066 case RIGHTSHIFT:
1067 op = BINARY_RSHIFT;
1068 break;
1069 default:
1070 err_setstr(SystemError,
1071 "com_shift_expr: operator not << or >>");
1072 c->c_errors++;
1073 op = 255;
1074 }
1075 com_addbyte(c, op);
1076 }
1077}
1078
1079static void
1080com_and_expr(c, n)
1081 struct compiling *c;
1082 node *n;
1083{
1084 int i;
1085 int op;
1086 REQ(n, and_expr);
1087 com_shift_expr(c, CHILD(n, 0));
1088 for (i = 2; i < NCH(n); i += 2) {
1089 com_shift_expr(c, CHILD(n, i));
1090 if (TYPE(CHILD(n, i-1)) == AMPER) {
1091 op = BINARY_AND;
1092 }
1093 else {
1094 err_setstr(SystemError,
1095 "com_and_expr: operator not &");
1096 c->c_errors++;
1097 op = 255;
1098 }
1099 com_addbyte(c, op);
1100 }
1101}
1102
1103static void
1104com_xor_expr(c, n)
1105 struct compiling *c;
1106 node *n;
1107{
1108 int i;
1109 int op;
1110 REQ(n, xor_expr);
1111 com_and_expr(c, CHILD(n, 0));
1112 for (i = 2; i < NCH(n); i += 2) {
1113 com_and_expr(c, CHILD(n, i));
1114 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
1115 op = BINARY_XOR;
1116 }
1117 else {
1118 err_setstr(SystemError,
1119 "com_xor_expr: operator not ^");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001120 c->c_errors++;
1121 op = 255;
1122 }
1123 com_addbyte(c, op);
1124 }
1125}
1126
1127static void
1128com_expr(c, n)
1129 struct compiling *c;
1130 node *n;
1131{
1132 int i;
1133 int op;
1134 REQ(n, expr);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001135 com_xor_expr(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001136 for (i = 2; i < NCH(n); i += 2) {
Guido van Rossum7928cd71991-10-24 14:59:31 +00001137 com_xor_expr(c, CHILD(n, i));
1138 if (TYPE(CHILD(n, i-1)) == VBAR) {
1139 op = BINARY_OR;
1140 }
1141 else {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001142 err_setstr(SystemError,
Guido van Rossum7928cd71991-10-24 14:59:31 +00001143 "com_expr: expr operator not |");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001144 c->c_errors++;
1145 op = 255;
1146 }
1147 com_addbyte(c, op);
1148 }
1149}
1150
1151static enum cmp_op
1152cmp_type(n)
1153 node *n;
1154{
1155 REQ(n, comp_op);
Guido van Rossum01cfd441991-10-20 20:12:38 +00001156 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001157 | 'in' | 'not' 'in' | 'is' | 'is' not' */
1158 if (NCH(n) == 1) {
1159 n = CHILD(n, 0);
1160 switch (TYPE(n)) {
1161 case LESS: return LT;
1162 case GREATER: return GT;
Guido van Rossum01cfd441991-10-20 20:12:38 +00001163 case EQEQUAL: /* == */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001164 case EQUAL: return EQ;
Guido van Rossum01cfd441991-10-20 20:12:38 +00001165 case LESSEQUAL: return LE;
1166 case GREATEREQUAL: return GE;
1167 case NOTEQUAL: return NE; /* <> or != */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001168 case NAME: if (strcmp(STR(n), "in") == 0) return IN;
1169 if (strcmp(STR(n), "is") == 0) return IS;
1170 }
1171 }
1172 else if (NCH(n) == 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001173 switch (TYPE(CHILD(n, 0))) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001174 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1175 return NOT_IN;
1176 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1177 return IS_NOT;
1178 }
1179 }
1180 return BAD;
1181}
1182
1183static void
1184com_comparison(c, n)
1185 struct compiling *c;
1186 node *n;
1187{
1188 int i;
1189 enum cmp_op op;
1190 int anchor;
1191 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
1192 com_expr(c, CHILD(n, 0));
1193 if (NCH(n) == 1)
1194 return;
1195
1196 /****************************************************************
1197 The following code is generated for all but the last
1198 comparison in a chain:
1199
1200 label: on stack: opcode: jump to:
1201
1202 a <code to load b>
1203 a, b DUP_TOP
1204 a, b, b ROT_THREE
1205 b, a, b COMPARE_OP
1206 b, 0-or-1 JUMP_IF_FALSE L1
1207 b, 1 POP_TOP
1208 b
1209
1210 We are now ready to repeat this sequence for the next
1211 comparison in the chain.
1212
1213 For the last we generate:
1214
1215 b <code to load c>
1216 b, c COMPARE_OP
1217 0-or-1
1218
1219 If there were any jumps to L1 (i.e., there was more than one
1220 comparison), we generate:
1221
1222 0-or-1 JUMP_FORWARD L2
1223 L1: b, 0 ROT_TWO
1224 0, b POP_TOP
1225 0
1226 L2:
1227 ****************************************************************/
1228
1229 anchor = 0;
1230
1231 for (i = 2; i < NCH(n); i += 2) {
1232 com_expr(c, CHILD(n, i));
1233 if (i+2 < NCH(n)) {
1234 com_addbyte(c, DUP_TOP);
1235 com_addbyte(c, ROT_THREE);
1236 }
1237 op = cmp_type(CHILD(n, i-1));
1238 if (op == BAD) {
1239 err_setstr(SystemError,
1240 "com_comparison: unknown comparison op");
1241 c->c_errors++;
1242 }
1243 com_addoparg(c, COMPARE_OP, op);
1244 if (i+2 < NCH(n)) {
1245 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1246 com_addbyte(c, POP_TOP);
1247 }
1248 }
1249
1250 if (anchor) {
1251 int anchor2 = 0;
1252 com_addfwref(c, JUMP_FORWARD, &anchor2);
1253 com_backpatch(c, anchor);
1254 com_addbyte(c, ROT_TWO);
1255 com_addbyte(c, POP_TOP);
1256 com_backpatch(c, anchor2);
1257 }
1258}
1259
1260static void
1261com_not_test(c, n)
1262 struct compiling *c;
1263 node *n;
1264{
1265 REQ(n, not_test); /* 'not' not_test | comparison */
1266 if (NCH(n) == 1) {
1267 com_comparison(c, CHILD(n, 0));
1268 }
1269 else {
1270 com_not_test(c, CHILD(n, 1));
1271 com_addbyte(c, UNARY_NOT);
1272 }
1273}
1274
1275static void
1276com_and_test(c, n)
1277 struct compiling *c;
1278 node *n;
1279{
1280 int i;
1281 int anchor;
1282 REQ(n, and_test); /* not_test ('and' not_test)* */
1283 anchor = 0;
1284 i = 0;
1285 for (;;) {
1286 com_not_test(c, CHILD(n, i));
1287 if ((i += 2) >= NCH(n))
1288 break;
1289 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1290 com_addbyte(c, POP_TOP);
1291 }
1292 if (anchor)
1293 com_backpatch(c, anchor);
1294}
1295
1296static void
1297com_test(c, n)
1298 struct compiling *c;
1299 node *n;
1300{
Guido van Rossum57531fe1993-11-30 14:57:42 +00001301 REQ(n, test); /* and_test ('and' and_test)* | lambdef */
1302 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
1303 object *v;
1304 int i;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001305 int ndefs = com_argdefs(c, CHILD(n, 0));
Guido van Rossum57531fe1993-11-30 14:57:42 +00001306 v = (object *) compile(CHILD(n, 0), c->c_filename);
1307 if (v == NULL) {
1308 c->c_errors++;
1309 i = 255;
1310 }
1311 else {
1312 i = com_addconst(c, v);
1313 DECREF(v);
1314 }
1315 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001316 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001317 }
Guido van Rossum57531fe1993-11-30 14:57:42 +00001318 else {
1319 int anchor = 0;
1320 int i = 0;
1321 for (;;) {
1322 com_and_test(c, CHILD(n, i));
1323 if ((i += 2) >= NCH(n))
1324 break;
1325 com_addfwref(c, JUMP_IF_TRUE, &anchor);
1326 com_addbyte(c, POP_TOP);
1327 }
1328 if (anchor)
1329 com_backpatch(c, anchor);
1330 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001331}
1332
1333static void
Guido van Rossum288a60f1991-12-16 13:05:10 +00001334com_list(c, n, toplevel)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001335 struct compiling *c;
1336 node *n;
Guido van Rossum288a60f1991-12-16 13:05:10 +00001337 int toplevel; /* If nonzero, *always* build a tuple */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001338{
1339 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
Guido van Rossum288a60f1991-12-16 13:05:10 +00001340 if (NCH(n) == 1 && !toplevel) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001341 com_node(c, CHILD(n, 0));
1342 }
1343 else {
1344 int i;
1345 int len;
1346 len = (NCH(n) + 1) / 2;
1347 for (i = 0; i < NCH(n); i += 2)
1348 com_node(c, CHILD(n, i));
1349 com_addoparg(c, BUILD_TUPLE, len);
1350 }
1351}
1352
1353
1354/* Begin of assignment compilation */
1355
1356static void com_assign_name PROTO((struct compiling *, node *, int));
1357static void com_assign PROTO((struct compiling *, node *, int));
1358
1359static void
1360com_assign_attr(c, n, assigning)
1361 struct compiling *c;
1362 node *n;
1363 int assigning;
1364{
1365 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
1366}
1367
1368static void
1369com_assign_slice(c, n, assigning)
1370 struct compiling *c;
1371 node *n;
1372 int assigning;
1373{
1374 com_slice(c, n, assigning ? STORE_SLICE : DELETE_SLICE);
1375}
1376
1377static void
1378com_assign_subscript(c, n, assigning)
1379 struct compiling *c;
1380 node *n;
1381 int assigning;
1382{
Guido van Rossum50564e81996-01-12 01:13:16 +00001383 if (NCH(n) == 1)
1384 com_node(c, CHILD(n, 0));
1385 else {
1386 int i;
1387 int len = (NCH(n)+1)/2;
1388 for (i = 0; i < NCH(n); i += 2)
1389 com_node(c, CHILD(n, i));
1390 com_addoparg(c, BUILD_TUPLE, len);
1391 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001392 com_addbyte(c, assigning ? STORE_SUBSCR : DELETE_SUBSCR);
1393}
1394
1395static void
1396com_assign_trailer(c, n, assigning)
1397 struct compiling *c;
1398 node *n;
1399 int assigning;
1400{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001401 REQ(n, trailer);
1402 switch (TYPE(CHILD(n, 0))) {
1403 case LPAR: /* '(' [exprlist] ')' */
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001404 err_setstr(SyntaxError, "can't assign to function call");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001405 c->c_errors++;
1406 break;
1407 case DOT: /* '.' NAME */
1408 com_assign_attr(c, CHILD(n, 1), assigning);
1409 break;
1410 case LSQB: /* '[' subscript ']' */
1411 n = CHILD(n, 1);
Guido van Rossum50564e81996-01-12 01:13:16 +00001412 REQ(n, subscript); /* subscript: expr (',' expr)* | [expr] ':' [expr] */
1413 if (TYPE(CHILD(n, 0)) == COLON || (NCH(n) > 1 && TYPE(CHILD(n, 1)) == COLON))
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001414 com_assign_slice(c, n, assigning);
1415 else
Guido van Rossum50564e81996-01-12 01:13:16 +00001416 com_assign_subscript(c, n, assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001417 break;
1418 default:
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001419 err_setstr(SystemError, "unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001420 c->c_errors++;
1421 }
1422}
1423
1424static void
1425com_assign_tuple(c, n, assigning)
1426 struct compiling *c;
1427 node *n;
1428 int assigning;
1429{
1430 int i;
1431 if (TYPE(n) != testlist)
1432 REQ(n, exprlist);
1433 if (assigning)
1434 com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2);
1435 for (i = 0; i < NCH(n); i += 2)
1436 com_assign(c, CHILD(n, i), assigning);
1437}
1438
1439static void
1440com_assign_list(c, n, assigning)
1441 struct compiling *c;
1442 node *n;
1443 int assigning;
1444{
1445 int i;
1446 if (assigning)
1447 com_addoparg(c, UNPACK_LIST, (NCH(n)+1)/2);
1448 for (i = 0; i < NCH(n); i += 2)
1449 com_assign(c, CHILD(n, i), assigning);
1450}
1451
1452static void
1453com_assign_name(c, n, assigning)
1454 struct compiling *c;
1455 node *n;
1456 int assigning;
1457{
1458 REQ(n, NAME);
1459 com_addopname(c, assigning ? STORE_NAME : DELETE_NAME, n);
1460}
1461
1462static void
1463com_assign(c, n, assigning)
1464 struct compiling *c;
1465 node *n;
1466 int assigning;
1467{
1468 /* Loop to avoid trivial recursion */
1469 for (;;) {
1470 switch (TYPE(n)) {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001471
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001472 case exprlist:
1473 case testlist:
1474 if (NCH(n) > 1) {
1475 com_assign_tuple(c, n, assigning);
1476 return;
1477 }
1478 n = CHILD(n, 0);
1479 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001480
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001481 case test:
1482 case and_test:
1483 case not_test:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001484 case comparison:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001485 case expr:
Guido van Rossum7928cd71991-10-24 14:59:31 +00001486 case xor_expr:
1487 case and_expr:
1488 case shift_expr:
1489 case arith_expr:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001490 case term:
Guido van Rossum50564e81996-01-12 01:13:16 +00001491 case factor:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001492 if (NCH(n) > 1) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001493 err_setstr(SyntaxError,
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001494 "can't assign to operator");
1495 c->c_errors++;
1496 return;
1497 }
1498 n = CHILD(n, 0);
1499 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001500
Guido van Rossum50564e81996-01-12 01:13:16 +00001501 case power: /* atom trailer* ('**' power)* */
1502/* ('+'|'-'|'~') factor | atom trailer* */
1503 if (TYPE(CHILD(n, 0)) != atom) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001504 err_setstr(SyntaxError,
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001505 "can't assign to operator");
1506 c->c_errors++;
1507 return;
1508 }
Guido van Rossum50564e81996-01-12 01:13:16 +00001509 if (NCH(n) > 1) { /* trailer or exponent present */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001510 int i;
1511 com_node(c, CHILD(n, 0));
1512 for (i = 1; i+1 < NCH(n); i++) {
Guido van Rossum50564e81996-01-12 01:13:16 +00001513 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
1514 err_setstr(SyntaxError,
1515 "can't assign to operator");
1516 c->c_errors++;
1517 return;
1518 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001519 com_apply_trailer(c, CHILD(n, i));
1520 } /* NB i is still alive */
1521 com_assign_trailer(c,
1522 CHILD(n, i), assigning);
1523 return;
1524 }
1525 n = CHILD(n, 0);
1526 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001527
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001528 case atom:
1529 switch (TYPE(CHILD(n, 0))) {
1530 case LPAR:
1531 n = CHILD(n, 1);
1532 if (TYPE(n) == RPAR) {
1533 /* XXX Should allow () = () ??? */
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001534 err_setstr(SyntaxError,
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001535 "can't assign to ()");
1536 c->c_errors++;
1537 return;
1538 }
1539 break;
1540 case LSQB:
1541 n = CHILD(n, 1);
1542 if (TYPE(n) == RSQB) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001543 err_setstr(SyntaxError,
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001544 "can't assign to []");
1545 c->c_errors++;
1546 return;
1547 }
1548 com_assign_list(c, n, assigning);
1549 return;
1550 case NAME:
1551 com_assign_name(c, CHILD(n, 0), assigning);
1552 return;
1553 default:
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001554 err_setstr(SyntaxError,
Guido van Rossuma9df32a1991-12-31 13:13:35 +00001555 "can't assign to literal");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001556 c->c_errors++;
1557 return;
1558 }
1559 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001560
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001561 default:
1562 fprintf(stderr, "node type %d\n", TYPE(n));
1563 err_setstr(SystemError, "com_assign: bad node");
1564 c->c_errors++;
1565 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001566
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001567 }
1568 }
1569}
1570
1571static void
1572com_expr_stmt(c, n)
1573 struct compiling *c;
1574 node *n;
1575{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001576 REQ(n, expr_stmt); /* testlist ('=' testlist)* */
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001577 com_node(c, CHILD(n, NCH(n)-1));
1578 if (NCH(n) == 1) {
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001579 if (c->c_interactive)
1580 com_addbyte(c, PRINT_EXPR);
1581 else
1582 com_addbyte(c, POP_TOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001583 }
1584 else {
1585 int i;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001586 for (i = 0; i < NCH(n)-2; i+=2) {
1587 if (i+2 < NCH(n)-2)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001588 com_addbyte(c, DUP_TOP);
1589 com_assign(c, CHILD(n, i), 1/*assign*/);
1590 }
1591 }
1592}
1593
1594static void
1595com_print_stmt(c, n)
1596 struct compiling *c;
1597 node *n;
1598{
1599 int i;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001600 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
1601 for (i = 1; i < NCH(n); i += 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001602 com_node(c, CHILD(n, i));
1603 com_addbyte(c, PRINT_ITEM);
1604 }
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001605 if (TYPE(CHILD(n, NCH(n)-1)) != COMMA)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001606 com_addbyte(c, PRINT_NEWLINE);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001607 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001608}
1609
1610static void
1611com_return_stmt(c, n)
1612 struct compiling *c;
1613 node *n;
1614{
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001615 REQ(n, return_stmt); /* 'return' [testlist] */
Guido van Rossum3f5da241990-12-20 15:06:42 +00001616 if (!c->c_infunction) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00001617 err_setstr(SyntaxError, "'return' outside function");
Guido van Rossum3f5da241990-12-20 15:06:42 +00001618 c->c_errors++;
1619 }
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001620 if (NCH(n) < 2)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001621 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
1622 else
1623 com_node(c, CHILD(n, 1));
1624 com_addbyte(c, RETURN_VALUE);
1625}
1626
1627static void
1628com_raise_stmt(c, n)
1629 struct compiling *c;
1630 node *n;
1631{
Guido van Rossumf10570b1995-07-07 22:53:21 +00001632 REQ(n, raise_stmt); /* 'raise' test [',' test [',' test]] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001633 com_node(c, CHILD(n, 1));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001634 if (NCH(n) > 3) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001635 com_node(c, CHILD(n, 3));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001636 if (NCH(n) > 5)
1637 com_node(c, CHILD(n, 5));
Guido van Rossumf10570b1995-07-07 22:53:21 +00001638 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001639 com_addoparg(c, RAISE_VARARGS, NCH(n)/2);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001640}
1641
1642static void
1643com_import_stmt(c, n)
1644 struct compiling *c;
1645 node *n;
1646{
1647 int i;
1648 REQ(n, import_stmt);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001649 /* 'import' dotted_name (',' dotted_name)* |
1650 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001651 if (STR(CHILD(n, 0))[0] == 'f') {
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001652 /* 'from' dotted_name 'import' ... */
1653 REQ(CHILD(n, 1), dotted_name);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001654 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
1655 for (i = 3; i < NCH(n); i += 2)
1656 com_addopname(c, IMPORT_FROM, CHILD(n, i));
1657 com_addbyte(c, POP_TOP);
1658 }
1659 else {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001660 /* 'import' ... */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001661 for (i = 1; i < NCH(n); i += 2) {
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001662 REQ(CHILD(n, i), dotted_name);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001663 com_addopname(c, IMPORT_NAME, CHILD(n, i));
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001664 com_addopname(c, STORE_NAME, CHILD(CHILD(n, i), 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001665 }
1666 }
1667}
1668
1669static void
Guido van Rossumc5e96291991-12-10 13:53:51 +00001670com_global_stmt(c, n)
1671 struct compiling *c;
1672 node *n;
1673{
1674 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00001675 REQ(n, global_stmt);
1676 /* 'global' NAME (',' NAME)* */
1677 for (i = 1; i < NCH(n); i += 2) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00001678 char *s = STR(CHILD(n, i));
1679 if (dictlookup(c->c_locals, s) != NULL) {
1680 err_setstr(SyntaxError, "name is local and global");
1681 c->c_errors++;
1682 }
1683 else if (dictinsert(c->c_globals, s, None) != 0)
Guido van Rossumc5e96291991-12-10 13:53:51 +00001684 c->c_errors++;
1685 }
1686}
1687
Guido van Rossum681d79a1995-07-18 14:51:37 +00001688static int
1689com_newlocal_o(c, nameval)
1690 struct compiling *c;
1691 object *nameval;
1692{
1693 int i;
1694 object *ival;
1695 if (getlistsize(c->c_varnames) != c->c_nlocals) {
1696 /* This is usually caused by an error on a previous call */
1697 if (c->c_errors == 0) {
1698 err_setstr(SystemError, "mixed up var name/index");
1699 c->c_errors++;
1700 }
1701 return 0;
1702 }
1703 ival = newintobject(i = c->c_nlocals++);
1704 if (ival == NULL)
1705 c->c_errors++;
1706 else if (mappinginsert(c->c_locals, nameval, ival) != 0)
1707 c->c_errors++;
1708 else if (addlistitem(c->c_varnames, nameval) != 0)
1709 c->c_errors++;
1710 XDECREF(ival);
1711 return i;
1712}
1713
1714static int
1715com_addlocal_o(c, nameval)
1716 struct compiling *c;
1717 object *nameval;
1718{
1719 object *ival = mappinglookup(c->c_locals, nameval);
1720 if (ival != NULL)
1721 return getintvalue(ival);
1722 return com_newlocal_o(c, nameval);
1723}
1724
1725static int
1726com_newlocal(c, name)
1727 struct compiling *c;
1728 char *name;
1729{
1730 object *nameval = newstringobject(name);
1731 int i;
1732 if (nameval == NULL) {
1733 c->c_errors++;
1734 return 0;
1735 }
1736 i = com_newlocal_o(c, nameval);
1737 DECREF(nameval);
1738 return i;
1739}
1740
Guido van Rossum25831651993-05-19 14:50:45 +00001741#define strequ(a, b) (strcmp((a), (b)) == 0)
1742
1743static void
1744com_access_stmt(c, n)
1745 struct compiling *c;
1746 node *n;
1747{
Guido van Rossum53f45241995-10-08 00:42:46 +00001748#if 0
Guido van Rossum25831651993-05-19 14:50:45 +00001749 int i, j, k, mode, imode;
1750 object *vmode;
1751 REQ(n, access_stmt);
1752 /* 'access' NAME (',' NAME)* ':' accesstype (',' accesstype)*
1753 accesstype: NAME+ */
1754
1755 /* Find where the colon is */
1756 i = 1;
1757 while (TYPE(CHILD(n,i-1)) != COLON)
1758 i += 1;
1759
1760 /* Calculate the mode mask */
1761 mode = 0;
1762 for (j = i; j < NCH(n); j += 2) {
Guido van Rossum81daa321993-05-20 14:24:46 +00001763 int r = 0, w = 0, p = 0;
1764 for (k = 0; k < NCH(CHILD(n,j)); k++) {
Guido van Rossum25831651993-05-19 14:50:45 +00001765 if (strequ(STR(CHILD(CHILD(n,j),k)), "public"))
1766 p = 0;
1767 else if (strequ(STR(CHILD(CHILD(n,j),k)), "protected"))
1768 p = 1;
1769 else if (strequ(STR(CHILD(CHILD(n,j),k)), "private"))
1770 p = 2;
1771 else if (strequ(STR(CHILD(CHILD(n,j),k)), "read"))
1772 r = 1;
1773 else if (strequ(STR(CHILD(CHILD(n,j),k)), "write"))
1774 w = 1;
1775 else /* XXX should make this an exception */
1776 fprintf(stderr, "bad access type %s\n",
1777 STR(CHILD(CHILD(n,j),k)));
1778 }
1779 if (r == 0 && w == 0)
Guido van Rossum81daa321993-05-20 14:24:46 +00001780 r = w = 1;
Guido van Rossum25831651993-05-19 14:50:45 +00001781 if (p == 0) {
1782 if (r == 1) mode |= AC_R_PUBLIC;
1783 if (w == 1) mode |= AC_W_PUBLIC;
1784 } else if (p == 1) {
1785 if (r == 1) mode |= AC_R_PROTECTED;
1786 if (w == 1) mode |= AC_W_PROTECTED;
1787 } else {
1788 if (r == 1) mode |= AC_R_PRIVATE;
1789 if (w == 1) mode |= AC_W_PRIVATE;
1790 }
1791 }
1792 vmode = newintobject((long)mode);
1793 imode = com_addconst(c, vmode);
1794 XDECREF(vmode);
1795 for (i = 1; TYPE(CHILD(n,i-1)) != COLON; i+=2) {
1796 com_addoparg(c, LOAD_CONST, imode);
1797 com_addopname(c, ACCESS_MODE, CHILD(n, i));
1798 }
Guido van Rossum53f45241995-10-08 00:42:46 +00001799#endif
Guido van Rossum25831651993-05-19 14:50:45 +00001800}
1801
Guido van Rossumc5e96291991-12-10 13:53:51 +00001802static void
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001803com_exec_stmt(c, n)
1804 struct compiling *c;
1805 node *n;
1806{
1807 REQ(n, exec_stmt);
1808 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
1809 com_node(c, CHILD(n, 1));
1810 if (NCH(n) >= 4)
1811 com_node(c, CHILD(n, 3));
1812 else
1813 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
1814 if (NCH(n) >= 6)
1815 com_node(c, CHILD(n, 5));
1816 else
1817 com_addbyte(c, DUP_TOP);
1818 com_addbyte(c, EXEC_STMT);
1819}
1820
1821static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001822com_if_stmt(c, n)
1823 struct compiling *c;
1824 node *n;
1825{
1826 int i;
1827 int anchor = 0;
1828 REQ(n, if_stmt);
1829 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
1830 for (i = 0; i+3 < NCH(n); i+=4) {
1831 int a = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001832 node *ch = CHILD(n, i+1);
1833 if (i > 0)
1834 com_addoparg(c, SET_LINENO, ch->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001835 com_node(c, CHILD(n, i+1));
1836 com_addfwref(c, JUMP_IF_FALSE, &a);
1837 com_addbyte(c, POP_TOP);
1838 com_node(c, CHILD(n, i+3));
1839 com_addfwref(c, JUMP_FORWARD, &anchor);
1840 com_backpatch(c, a);
1841 com_addbyte(c, POP_TOP);
1842 }
1843 if (i+2 < NCH(n))
1844 com_node(c, CHILD(n, i+2));
1845 com_backpatch(c, anchor);
1846}
1847
1848static void
1849com_while_stmt(c, n)
1850 struct compiling *c;
1851 node *n;
1852{
1853 int break_anchor = 0;
1854 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001855 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001856 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
1857 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001858 block_push(c, SETUP_LOOP);
1859 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001860 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001861 com_node(c, CHILD(n, 1));
1862 com_addfwref(c, JUMP_IF_FALSE, &anchor);
1863 com_addbyte(c, POP_TOP);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001864 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001865 com_node(c, CHILD(n, 3));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001866 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001867 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1868 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001869 com_backpatch(c, anchor);
1870 com_addbyte(c, POP_TOP);
1871 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001872 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001873 if (NCH(n) > 4)
1874 com_node(c, CHILD(n, 6));
1875 com_backpatch(c, break_anchor);
1876}
1877
1878static void
1879com_for_stmt(c, n)
1880 struct compiling *c;
1881 node *n;
1882{
1883 object *v;
1884 int break_anchor = 0;
1885 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001886 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001887 REQ(n, for_stmt);
1888 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
1889 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001890 block_push(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001891 com_node(c, CHILD(n, 3));
1892 v = newintobject(0L);
1893 if (v == NULL)
1894 c->c_errors++;
1895 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
1896 XDECREF(v);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001897 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001898 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001899 com_addfwref(c, FOR_LOOP, &anchor);
1900 com_assign(c, CHILD(n, 1), 1/*assigning*/);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001901 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001902 com_node(c, CHILD(n, 5));
Guido van Rossum3f5da241990-12-20 15:06:42 +00001903 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001904 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1905 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001906 com_backpatch(c, anchor);
1907 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001908 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001909 if (NCH(n) > 8)
1910 com_node(c, CHILD(n, 8));
1911 com_backpatch(c, break_anchor);
1912}
1913
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001914/* Code generated for "try: S finally: Sf" is as follows:
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001915
1916 SETUP_FINALLY L
1917 <code for S>
1918 POP_BLOCK
1919 LOAD_CONST <nil>
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001920 L: <code for Sf>
1921 END_FINALLY
1922
1923 The special instructions use the block stack. Each block
1924 stack entry contains the instruction that created it (here
1925 SETUP_FINALLY), the level of the value stack at the time the
1926 block stack entry was created, and a label (here L).
1927
1928 SETUP_FINALLY:
1929 Pushes the current value stack level and the label
1930 onto the block stack.
1931 POP_BLOCK:
1932 Pops en entry from the block stack, and pops the value
1933 stack until its level is the same as indicated on the
1934 block stack. (The label is ignored.)
1935 END_FINALLY:
Guido van Rossum3f5da241990-12-20 15:06:42 +00001936 Pops a variable number of entries from the *value* stack
1937 and re-raises the exception they specify. The number of
1938 entries popped depends on the (pseudo) exception type.
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001939
1940 The block stack is unwound when an exception is raised:
1941 when a SETUP_FINALLY entry is found, the exception is pushed
1942 onto the value stack (and the exception condition is cleared),
1943 and the interpreter jumps to the label gotten from the block
1944 stack.
1945
1946 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
Guido van Rossum3f5da241990-12-20 15:06:42 +00001947 (The contents of the value stack is shown in [], with the top
1948 at the right; 'tb' is trace-back info, 'val' the exception's
1949 associated value, and 'exc' the exception.)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001950
1951 Value stack Label Instruction Argument
1952 [] SETUP_EXCEPT L1
1953 [] <code for S>
1954 [] POP_BLOCK
1955 [] JUMP_FORWARD L0
1956
Guido van Rossum3f5da241990-12-20 15:06:42 +00001957 [tb, val, exc] L1: DUP )
1958 [tb, val, exc, exc] <evaluate E1> )
1959 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1960 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1961 [tb, val, exc, 1] POP )
1962 [tb, val, exc] POP
1963 [tb, val] <assign to V1> (or POP if no V1)
1964 [tb] POP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001965 [] <code for S1>
1966 JUMP_FORWARD L0
1967
Guido van Rossum3f5da241990-12-20 15:06:42 +00001968 [tb, val, exc, 0] L2: POP
1969 [tb, val, exc] DUP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001970 .............................etc.......................
1971
Guido van Rossum3f5da241990-12-20 15:06:42 +00001972 [tb, val, exc, 0] Ln+1: POP
1973 [tb, val, exc] END_FINALLY # re-raise exception
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00001974
1975 [] L0: <next statement>
1976
1977 Of course, parts are not generated if Vi or Ei is not present.
1978*/
1979
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001980static void
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001981com_try_except(c, n)
1982 struct compiling *c;
1983 node *n;
1984{
1985 int except_anchor = 0;
1986 int end_anchor = 0;
1987 int else_anchor = 0;
1988 int i;
1989 node *ch;
1990
1991 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
1992 block_push(c, SETUP_EXCEPT);
1993 com_node(c, CHILD(n, 2));
1994 com_addbyte(c, POP_BLOCK);
1995 block_pop(c, SETUP_EXCEPT);
1996 com_addfwref(c, JUMP_FORWARD, &else_anchor);
1997 com_backpatch(c, except_anchor);
1998 for (i = 3;
1999 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
2000 i += 3) {
2001 /* except_clause: 'except' [expr [',' expr]] */
2002 if (except_anchor == 0) {
2003 err_setstr(SyntaxError,
2004 "default 'except:' must be last");
2005 c->c_errors++;
2006 break;
2007 }
2008 except_anchor = 0;
2009 com_addoparg(c, SET_LINENO, ch->n_lineno);
2010 if (NCH(ch) > 1) {
2011 com_addbyte(c, DUP_TOP);
2012 com_node(c, CHILD(ch, 1));
2013 com_addoparg(c, COMPARE_OP, EXC_MATCH);
2014 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
2015 com_addbyte(c, POP_TOP);
2016 }
2017 com_addbyte(c, POP_TOP);
2018 if (NCH(ch) > 3)
2019 com_assign(c, CHILD(ch, 3), 1/*assigning*/);
2020 else
2021 com_addbyte(c, POP_TOP);
2022 com_addbyte(c, POP_TOP);
2023 com_node(c, CHILD(n, i+2));
2024 com_addfwref(c, JUMP_FORWARD, &end_anchor);
2025 if (except_anchor) {
2026 com_backpatch(c, except_anchor);
2027 com_addbyte(c, POP_TOP);
2028 }
2029 }
2030 com_addbyte(c, END_FINALLY);
2031 com_backpatch(c, else_anchor);
2032 if (i < NCH(n))
2033 com_node(c, CHILD(n, i+2));
2034 com_backpatch(c, end_anchor);
2035}
2036
2037static void
2038com_try_finally(c, n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002039 struct compiling *c;
2040 node *n;
2041{
2042 int finally_anchor = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002043 node *ch;
Guido van Rossum94fb82e1992-04-05 14:24:50 +00002044
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002045 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
2046 block_push(c, SETUP_FINALLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002047 com_node(c, CHILD(n, 2));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002048 com_addbyte(c, POP_BLOCK);
2049 block_pop(c, SETUP_FINALLY);
2050 block_push(c, END_FINALLY);
2051 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
2052 com_backpatch(c, finally_anchor);
2053 ch = CHILD(n, NCH(n)-1);
2054 com_addoparg(c, SET_LINENO, ch->n_lineno);
2055 com_node(c, ch);
2056 com_addbyte(c, END_FINALLY);
2057 block_pop(c, END_FINALLY);
2058}
2059
2060static void
2061com_try_stmt(c, n)
2062 struct compiling *c;
2063 node *n;
2064{
2065 REQ(n, try_stmt);
2066 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
2067 | 'try' ':' suite 'finally' ':' suite */
2068 if (TYPE(CHILD(n, 3)) != except_clause)
2069 com_try_finally(c, n);
2070 else
2071 com_try_except(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002072}
2073
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002074static object *
2075get_docstring(n)
2076 node *n;
2077{
Guido van Rossum164d4ff1995-01-26 00:40:09 +00002078 int i;
2079
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002080 switch (TYPE(n)) {
2081
2082 case suite:
2083 if (NCH(n) == 1)
2084 return get_docstring(CHILD(n, 0));
2085 else {
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002086 for (i = 0; i < NCH(n); i++) {
2087 node *ch = CHILD(n, i);
2088 if (TYPE(ch) == stmt)
2089 return get_docstring(ch);
2090 }
2091 }
2092 break;
2093
Guido van Rossum164d4ff1995-01-26 00:40:09 +00002094 case file_input:
2095 for (i = 0; i < NCH(n); i++) {
2096 node *ch = CHILD(n, i);
2097 if (TYPE(ch) == stmt)
2098 return get_docstring(ch);
2099 }
2100 break;
2101
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002102 case stmt:
2103 case simple_stmt:
2104 case small_stmt:
2105 return get_docstring(CHILD(n, 0));
2106
2107 case expr_stmt:
2108 case testlist:
2109 case test:
2110 case and_test:
2111 case not_test:
2112 case comparison:
2113 case expr:
2114 case xor_expr:
2115 case and_expr:
2116 case shift_expr:
2117 case arith_expr:
2118 case term:
2119 case factor:
Guido van Rossum50564e81996-01-12 01:13:16 +00002120 case power:
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002121 if (NCH(n) == 1)
2122 return get_docstring(CHILD(n, 0));
2123 break;
2124
2125 case atom:
2126 if (TYPE(CHILD(n, 0)) == STRING)
2127 return parsestrplus(n);
2128 break;
2129
2130 }
2131 return NULL;
2132}
2133
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002134static void
2135com_suite(c, n)
2136 struct compiling *c;
2137 node *n;
2138{
2139 REQ(n, suite);
2140 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
2141 if (NCH(n) == 1) {
2142 com_node(c, CHILD(n, 0));
2143 }
2144 else {
2145 int i;
2146 for (i = 0; i < NCH(n); i++) {
2147 node *ch = CHILD(n, i);
2148 if (TYPE(ch) == stmt)
2149 com_node(c, ch);
2150 }
2151 }
2152}
2153
Guido van Rossumf1aeab71992-03-27 17:28:26 +00002154/* ARGSUSED */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002155static void
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002156com_continue_stmt(c, n)
2157 struct compiling *c;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00002158 node *n; /* Not used, but passed for consistency */
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002159{
2160 int i = c->c_nblocks;
2161 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
2162 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2163 }
2164 else {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00002165 err_setstr(SyntaxError, "'continue' not properly in loop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002166 c->c_errors++;
2167 }
2168 /* XXX Could allow it inside a 'finally' clause
2169 XXX if we could pop the exception still on the stack */
2170}
2171
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002172static int
Guido van Rossum681d79a1995-07-18 14:51:37 +00002173com_argdefs(c, n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002174 struct compiling *c;
2175 node *n;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002176{
Guido van Rossum681d79a1995-07-18 14:51:37 +00002177 int i, nch, nargs, ndefs;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002178 if (TYPE(n) == lambdef) {
2179 /* lambdef: 'lambda' [varargslist] ':' test */
2180 n = CHILD(n, 1);
2181 }
2182 else {
2183 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
2184 n = CHILD(n, 2);
2185 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
2186 n = CHILD(n, 1);
2187 }
2188 if (TYPE(n) != varargslist)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002189 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002190 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00002191 (fpdef ['=' test] ',')* '*' ....... |
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002192 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
2193 nch = NCH(n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002194 nargs = 0;
2195 ndefs = 0;
2196 for (i = 0; i < nch; i++) {
2197 int t;
Guido van Rossum50564e81996-01-12 01:13:16 +00002198 if (TYPE(CHILD(n, i)) == STAR || TYPE(CHILD(n, i)) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00002199 break;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002200 nargs++;
2201 i++;
2202 if (i >= nch)
Guido van Rossuma1e7e621995-09-18 21:44:04 +00002203 t = RPAR; /* Anything except EQUAL or COMMA */
2204 else
2205 t = TYPE(CHILD(n, i));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002206 if (t == EQUAL) {
2207 i++;
2208 ndefs++;
2209 com_node(c, CHILD(n, i));
2210 i++;
2211 if (i >= nch)
2212 break;
2213 t = TYPE(CHILD(n, i));
2214 }
2215 else {
2216 /* Treat "(a=1, b)" as "(a=1, b=None)" */
2217 if (ndefs) {
2218 com_addoparg(c, LOAD_CONST,
2219 com_addconst(c, None));
2220 ndefs++;
2221 }
2222 }
2223 if (t != COMMA)
2224 break;
2225 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002226 return ndefs;
2227}
2228
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002229static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002230com_funcdef(c, n)
2231 struct compiling *c;
2232 node *n;
2233{
2234 object *v;
2235 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum590baa41993-11-30 13:40:46 +00002236 v = (object *)compile(n, c->c_filename);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002237 if (v == NULL)
2238 c->c_errors++;
2239 else {
2240 int i = com_addconst(c, v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002241 int ndefs = com_argdefs(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002242 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002243 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002244 com_addopname(c, STORE_NAME, CHILD(n, 1));
2245 DECREF(v);
2246 }
2247}
2248
2249static void
Guido van Rossum25831651993-05-19 14:50:45 +00002250com_bases(c, n)
Guido van Rossumc5e96291991-12-10 13:53:51 +00002251 struct compiling *c;
2252 node *n;
2253{
Guido van Rossumf1aeab71992-03-27 17:28:26 +00002254 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00002255 REQ(n, testlist);
2256 /* testlist: test (',' test)* [','] */
2257 for (i = 0; i < NCH(n); i += 2)
2258 com_node(c, CHILD(n, i));
2259 com_addoparg(c, BUILD_TUPLE, (NCH(n)+1) / 2);
2260}
2261
2262static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002263com_classdef(c, n)
2264 struct compiling *c;
2265 node *n;
2266{
Guido van Rossum25831651993-05-19 14:50:45 +00002267 int i;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002268 object *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002269 REQ(n, classdef);
Guido van Rossum25831651993-05-19 14:50:45 +00002270 /* classdef: class NAME ['(' testlist ')'] ':' suite */
2271 if ((v = newstringobject(STR(CHILD(n, 1)))) == NULL) {
2272 c->c_errors++;
2273 return;
2274 }
2275 /* Push the class name on the stack */
2276 i = com_addconst(c, v);
2277 com_addoparg(c, LOAD_CONST, i);
2278 DECREF(v);
2279 /* Push the tuple of base classes on the stack */
2280 if (TYPE(CHILD(n, 2)) != LPAR)
Guido van Rossumc5e96291991-12-10 13:53:51 +00002281 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum25831651993-05-19 14:50:45 +00002282 else
2283 com_bases(c, CHILD(n, 3));
Guido van Rossum590baa41993-11-30 13:40:46 +00002284 v = (object *)compile(n, c->c_filename);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002285 if (v == NULL)
2286 c->c_errors++;
2287 else {
Guido van Rossum25831651993-05-19 14:50:45 +00002288 i = com_addconst(c, v);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002289 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002290 com_addoparg(c, MAKE_FUNCTION, 0);
2291 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002292 com_addbyte(c, BUILD_CLASS);
2293 com_addopname(c, STORE_NAME, CHILD(n, 1));
2294 DECREF(v);
2295 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002296}
2297
2298static void
2299com_node(c, n)
2300 struct compiling *c;
2301 node *n;
2302{
2303 switch (TYPE(n)) {
2304
2305 /* Definition nodes */
2306
2307 case funcdef:
2308 com_funcdef(c, n);
2309 break;
2310 case classdef:
2311 com_classdef(c, n);
2312 break;
2313
2314 /* Trivial parse tree nodes */
2315
2316 case stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002317 case small_stmt:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002318 case flow_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002319 com_node(c, CHILD(n, 0));
2320 break;
2321
2322 case simple_stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002323 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
2324 com_addoparg(c, SET_LINENO, n->n_lineno);
2325 {
2326 int i;
2327 for (i = 0; i < NCH(n)-1; i += 2)
2328 com_node(c, CHILD(n, i));
2329 }
2330 break;
2331
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002332 case compound_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002333 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002334 com_node(c, CHILD(n, 0));
2335 break;
2336
2337 /* Statement nodes */
2338
2339 case expr_stmt:
2340 com_expr_stmt(c, n);
2341 break;
2342 case print_stmt:
2343 com_print_stmt(c, n);
2344 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002345 case del_stmt: /* 'del' exprlist */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002346 com_assign(c, CHILD(n, 1), 0/*delete*/);
2347 break;
2348 case pass_stmt:
2349 break;
2350 case break_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002351 if (c->c_loops == 0) {
Guido van Rossum598fd8c1992-01-26 18:12:03 +00002352 err_setstr(SyntaxError, "'break' outside loop");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002353 c->c_errors++;
2354 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002355 com_addbyte(c, BREAK_LOOP);
2356 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002357 case continue_stmt:
2358 com_continue_stmt(c, n);
2359 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002360 case return_stmt:
2361 com_return_stmt(c, n);
2362 break;
2363 case raise_stmt:
2364 com_raise_stmt(c, n);
2365 break;
2366 case import_stmt:
2367 com_import_stmt(c, n);
2368 break;
Guido van Rossumc5e96291991-12-10 13:53:51 +00002369 case global_stmt:
2370 com_global_stmt(c, n);
2371 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002372 case access_stmt:
2373 com_access_stmt(c, n);
2374 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002375 case exec_stmt:
2376 com_exec_stmt(c, n);
2377 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002378 case if_stmt:
2379 com_if_stmt(c, n);
2380 break;
2381 case while_stmt:
2382 com_while_stmt(c, n);
2383 break;
2384 case for_stmt:
2385 com_for_stmt(c, n);
2386 break;
2387 case try_stmt:
2388 com_try_stmt(c, n);
2389 break;
2390 case suite:
2391 com_suite(c, n);
2392 break;
2393
2394 /* Expression nodes */
2395
2396 case testlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00002397 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002398 break;
2399 case test:
2400 com_test(c, n);
2401 break;
2402 case and_test:
2403 com_and_test(c, n);
2404 break;
2405 case not_test:
2406 com_not_test(c, n);
2407 break;
2408 case comparison:
2409 com_comparison(c, n);
2410 break;
2411 case exprlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00002412 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002413 break;
2414 case expr:
2415 com_expr(c, n);
2416 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +00002417 case xor_expr:
2418 com_xor_expr(c, n);
2419 break;
2420 case and_expr:
2421 com_and_expr(c, n);
2422 break;
2423 case shift_expr:
2424 com_shift_expr(c, n);
2425 break;
2426 case arith_expr:
2427 com_arith_expr(c, n);
2428 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002429 case term:
2430 com_term(c, n);
2431 break;
2432 case factor:
2433 com_factor(c, n);
2434 break;
Guido van Rossum50564e81996-01-12 01:13:16 +00002435 case power:
2436 com_power(c, n);
2437 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002438 case atom:
2439 com_atom(c, n);
2440 break;
2441
2442 default:
2443 fprintf(stderr, "node type %d\n", TYPE(n));
2444 err_setstr(SystemError, "com_node: unexpected node type");
2445 c->c_errors++;
2446 }
2447}
2448
2449static void com_fplist PROTO((struct compiling *, node *));
2450
2451static void
2452com_fpdef(c, n)
2453 struct compiling *c;
2454 node *n;
2455{
2456 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
2457 if (TYPE(CHILD(n, 0)) == LPAR)
2458 com_fplist(c, CHILD(n, 1));
2459 else
Guido van Rossum681d79a1995-07-18 14:51:37 +00002460 com_addoparg(c, STORE_FAST, com_newlocal(c, STR(CHILD(n, 0))));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002461}
2462
2463static void
2464com_fplist(c, n)
2465 struct compiling *c;
2466 node *n;
2467{
Guido van Rossuma9df32a1991-12-31 13:13:35 +00002468 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002469 if (NCH(n) == 1) {
2470 com_fpdef(c, CHILD(n, 0));
2471 }
2472 else {
2473 int i;
2474 com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2);
2475 for (i = 0; i < NCH(n); i += 2)
2476 com_fpdef(c, CHILD(n, i));
2477 }
2478}
2479
2480static void
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002481com_arglist(c, n)
2482 struct compiling *c;
2483 node *n;
2484{
Guido van Rossum681d79a1995-07-18 14:51:37 +00002485 int nch, i;
2486 int complex = 0;
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002487 REQ(n, varargslist);
Guido van Rossumacbefef1992-01-19 16:33:51 +00002488 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00002489 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002490 nch = NCH(n);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002491 /* Enter all arguments in table of locals */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002492 for (i = 0; i < nch; i++) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002493 node *ch = CHILD(n, i);
2494 node *fp;
2495 char *name;
Guido van Rossum50564e81996-01-12 01:13:16 +00002496 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00002497 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002498 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
2499 fp = CHILD(ch, 0);
2500 if (TYPE(fp) == NAME)
2501 name = STR(fp);
2502 else {
2503 name = "";
2504 complex= 1;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002505 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002506 com_newlocal(c, name);
2507 c->c_argcount++;
2508 if (++i >= nch)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002509 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002510 ch = CHILD(n, i);
2511 if (TYPE(ch) == EQUAL)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002512 i += 2;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002513 else
2514 REQ(ch, COMMA);
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002515 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002516 /* Handle *arguments */
2517 if (i < nch) {
2518 node *ch;
2519 ch = CHILD(n, i);
Guido van Rossum50564e81996-01-12 01:13:16 +00002520 if (TYPE(ch) != DOUBLESTAR) {
2521 REQ(ch, STAR);
2522 ch = CHILD(n, i+1);
2523 if (TYPE(ch) == NAME) {
2524 c->c_flags |= CO_VARARGS;
2525 i += 3;
2526 com_newlocal(c, STR(ch));
2527 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002528 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002529 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002530 /* Handle **keywords */
2531 if (i < nch) {
2532 node *ch;
2533 ch = CHILD(n, i);
Guido van Rossum50564e81996-01-12 01:13:16 +00002534 if (TYPE(ch) != DOUBLESTAR) {
2535 REQ(ch, STAR);
2536 ch = CHILD(n, i+1);
2537 REQ(ch, STAR);
2538 ch = CHILD(n, i+2);
2539 }
2540 else
2541 ch = CHILD(n, i+1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002542 REQ(ch, NAME);
2543 c->c_flags |= CO_VARKEYWORDS;
2544 com_newlocal(c, STR(ch));
2545 }
2546 if (complex) {
2547 /* Generate code for complex arguments only after
2548 having counted the simple arguments */
2549 int ilocal = 0;
2550 for (i = 0; i < nch; i++) {
2551 node *ch = CHILD(n, i);
2552 node *fp;
Guido van Rossum50564e81996-01-12 01:13:16 +00002553 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002554 break;
2555 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
2556 fp = CHILD(ch, 0);
2557 if (TYPE(fp) != NAME) {
2558 com_addoparg(c, LOAD_FAST, ilocal);
2559 com_fpdef(c, ch);
2560 }
2561 ilocal++;
2562 if (++i >= nch)
2563 break;
2564 ch = CHILD(n, i);
2565 if (TYPE(ch) == EQUAL)
2566 i += 2;
2567 else
2568 REQ(ch, COMMA);
2569 }
2570 }
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002571}
2572
2573static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002574com_file_input(c, n)
2575 struct compiling *c;
2576 node *n;
2577{
2578 int i;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002579 object *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002580 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002581 doc = get_docstring(n);
2582 if (doc != NULL) {
2583 int i = com_addconst(c, doc);
2584 DECREF(doc);
2585 com_addoparg(c, LOAD_CONST, i);
2586 com_addopnamestr(c, STORE_NAME, "__doc__");
2587 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002588 for (i = 0; i < NCH(n); i++) {
2589 node *ch = CHILD(n, i);
2590 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
2591 com_node(c, ch);
2592 }
2593}
2594
2595/* Top-level compile-node interface */
2596
2597static void
2598compile_funcdef(c, n)
2599 struct compiling *c;
2600 node *n;
2601{
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002602 object *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002603 node *ch;
2604 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum9bfef441993-03-29 10:43:31 +00002605 c->c_name = STR(CHILD(n, 1));
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002606 doc = get_docstring(CHILD(n, 4));
2607 if (doc != NULL) {
2608 (void) com_addconst(c, doc);
2609 DECREF(doc);
2610 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002611 else
2612 (void) com_addconst(c, None); /* No docstring */
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002613 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
2614 ch = CHILD(ch, 1); /* ')' | varargslist */
Guido van Rossum681d79a1995-07-18 14:51:37 +00002615 if (TYPE(ch) == varargslist)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00002616 com_arglist(c, ch);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002617 c->c_infunction = 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002618 com_node(c, CHILD(n, 4));
Guido van Rossum3f5da241990-12-20 15:06:42 +00002619 c->c_infunction = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002620 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
2621 com_addbyte(c, RETURN_VALUE);
2622}
2623
2624static void
Guido van Rossum590baa41993-11-30 13:40:46 +00002625compile_lambdef(c, n)
Guido van Rossum12d12c51993-10-26 17:58:25 +00002626 struct compiling *c;
2627 node *n;
2628{
Guido van Rossum590baa41993-11-30 13:40:46 +00002629 node *ch;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002630 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
Guido van Rossum363ac7d1994-11-10 22:40:34 +00002631 c->c_name = "<lambda>";
Guido van Rossum590baa41993-11-30 13:40:46 +00002632
2633 ch = CHILD(n, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002634 (void) com_addconst(c, None); /* No docstring */
2635 if (TYPE(ch) == varargslist) {
Guido van Rossum590baa41993-11-30 13:40:46 +00002636 com_arglist(c, ch);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002637 ch = CHILD(n, 3);
Guido van Rossum590baa41993-11-30 13:40:46 +00002638 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002639 else
2640 ch = CHILD(n, 2);
2641 com_node(c, ch);
Guido van Rossum12d12c51993-10-26 17:58:25 +00002642 com_addbyte(c, RETURN_VALUE);
2643}
2644
2645static void
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002646compile_classdef(c, n)
2647 struct compiling *c;
2648 node *n;
2649{
2650 node *ch;
2651 object *doc;
2652 REQ(n, classdef);
2653 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
2654 c->c_name = STR(CHILD(n, 1));
2655 ch = CHILD(n, NCH(n)-1); /* The suite */
2656 doc = get_docstring(ch);
2657 if (doc != NULL) {
2658 int i = com_addconst(c, doc);
2659 DECREF(doc);
2660 com_addoparg(c, LOAD_CONST, i);
2661 com_addopnamestr(c, STORE_NAME, "__doc__");
2662 }
2663 else
2664 (void) com_addconst(c, None);
2665 com_node(c, ch);
2666 com_addbyte(c, LOAD_LOCALS);
2667 com_addbyte(c, RETURN_VALUE);
2668}
2669
2670static void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002671compile_node(c, n)
2672 struct compiling *c;
2673 node *n;
2674{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002675 com_addoparg(c, SET_LINENO, n->n_lineno);
2676
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002677 switch (TYPE(n)) {
2678
Guido van Rossum4c417781991-01-21 16:09:22 +00002679 case single_input: /* One interactive command */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002680 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002681 c->c_interactive++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002682 n = CHILD(n, 0);
2683 if (TYPE(n) != NEWLINE)
2684 com_node(c, n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002685 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
2686 com_addbyte(c, RETURN_VALUE);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002687 c->c_interactive--;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002688 break;
2689
Guido van Rossum4c417781991-01-21 16:09:22 +00002690 case file_input: /* A whole file, or built-in function exec() */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002691 com_file_input(c, n);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002692 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
2693 com_addbyte(c, RETURN_VALUE);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002694 break;
2695
Guido van Rossum590baa41993-11-30 13:40:46 +00002696 case eval_input: /* Built-in function input() */
Guido van Rossum4c417781991-01-21 16:09:22 +00002697 com_node(c, CHILD(n, 0));
2698 com_addbyte(c, RETURN_VALUE);
2699 break;
2700
Guido van Rossum590baa41993-11-30 13:40:46 +00002701 case lambdef: /* anonymous function definition */
2702 compile_lambdef(c, n);
2703 break;
2704
Guido van Rossum4c417781991-01-21 16:09:22 +00002705 case funcdef: /* A function definition */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002706 compile_funcdef(c, n);
2707 break;
2708
Guido van Rossum4c417781991-01-21 16:09:22 +00002709 case classdef: /* A class definition */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002710 compile_classdef(c, n);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002711 break;
2712
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002713 default:
2714 fprintf(stderr, "node type %d\n", TYPE(n));
2715 err_setstr(SystemError, "compile_node: unexpected node type");
2716 c->c_errors++;
2717 }
2718}
2719
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002720/* Optimization for local variables in functions (and *only* functions).
Guido van Rossum282914b1991-04-04 10:42:56 +00002721
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002722 This replaces all LOAD_NAME, STORE_NAME and DELETE_NAME
2723 instructions that refer to local variables with LOAD_FAST etc.
2724 The latter instructions are much faster because they don't need to
2725 look up the variable name in a dictionary.
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002726
Guido van Rossum681d79a1995-07-18 14:51:37 +00002727 To find all local variables, we check all STORE_NAME, IMPORT_FROM
2728 and DELETE_NAME instructions. This yields all local variables,
2729 function definitions, class definitions and import statements.
2730 Argument names have already been entered into the list by the
2731 special processing for the argument list.
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002732
2733 All remaining LOAD_NAME instructions must refer to non-local (global
2734 or builtin) variables, so are replaced by LOAD_GLOBAL.
2735
2736 There are two problems: 'from foo import *' and 'exec' may introduce
2737 local variables that we can't know while compiling. If this is the
Guido van Rossum681d79a1995-07-18 14:51:37 +00002738 case, we can still optimize bona fide locals (since those
2739 statements will be surrounded by fast_2_locals() and
2740 locals_2_fast()), but we can't change LOAD_NAME to LOAD_GLOBAL.
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002741
Guido van Rossum681d79a1995-07-18 14:51:37 +00002742 NB: this modifies the string object c->c_code! */
Guido van Rossum282914b1991-04-04 10:42:56 +00002743
2744static void
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002745optimize(c)
2746 struct compiling *c;
Guido van Rossum282914b1991-04-04 10:42:56 +00002747{
Guido van Rossum0a697f61991-04-16 08:39:12 +00002748 unsigned char *next_instr, *cur_instr;
Guido van Rossum282914b1991-04-04 10:42:56 +00002749 int opcode;
2750 int oparg;
2751 object *name;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002752 object *error_type, *error_value, *error_traceback;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002753
Guido van Rossum282914b1991-04-04 10:42:56 +00002754#define NEXTOP() (*next_instr++)
2755#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
2756#define GETITEM(v, i) (getlistitem((v), (i)))
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002757#define GETNAMEOBJ(i) (GETITEM(c->c_names, (i)))
2758
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002759 err_fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002760
2761 c->c_flags |= CO_OPTIMIZED;
Guido van Rossum282914b1991-04-04 10:42:56 +00002762
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002763 next_instr = (unsigned char *) getstringvalue(c->c_code);
Guido van Rossum282914b1991-04-04 10:42:56 +00002764 for (;;) {
2765 opcode = NEXTOP();
2766 if (opcode == STOP_CODE)
2767 break;
2768 if (HAS_ARG(opcode))
2769 oparg = NEXTARG();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002770 switch (opcode) {
2771 case STORE_NAME:
2772 case DELETE_NAME:
2773 case IMPORT_FROM:
2774 com_addlocal_o(c, GETNAMEOBJ(oparg));
2775 break;
2776 case EXEC_STMT:
2777 c->c_flags &= ~CO_OPTIMIZED;
2778 break;
Guido van Rossum282914b1991-04-04 10:42:56 +00002779 }
2780 }
2781
Guido van Rossum681d79a1995-07-18 14:51:37 +00002782 if (dictlookup(c->c_locals, "*") != NULL)
2783 c->c_flags &= ~CO_OPTIMIZED;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002784
2785 next_instr = (unsigned char *) getstringvalue(c->c_code);
Guido van Rossum282914b1991-04-04 10:42:56 +00002786 for (;;) {
2787 cur_instr = next_instr;
2788 opcode = NEXTOP();
2789 if (opcode == STOP_CODE)
2790 break;
2791 if (HAS_ARG(opcode))
2792 oparg = NEXTARG();
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002793 if (opcode == LOAD_NAME ||
2794 opcode == STORE_NAME ||
2795 opcode == DELETE_NAME) {
2796 object *v;
2797 int i;
Guido van Rossum282914b1991-04-04 10:42:56 +00002798 name = GETNAMEOBJ(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002799 v = dict2lookup(c->c_locals, name);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002800 if (v == NULL) {
Guido van Rossum83163251991-08-16 08:58:43 +00002801 err_clear();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002802 if (opcode == LOAD_NAME &&
2803 (c->c_flags&CO_OPTIMIZED))
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002804 cur_instr[0] = LOAD_GLOBAL;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002805 continue;
Guido van Rossum83163251991-08-16 08:58:43 +00002806 }
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002807 i = getintvalue(v);
2808 switch (opcode) {
2809 case LOAD_NAME: cur_instr[0] = LOAD_FAST; break;
2810 case STORE_NAME: cur_instr[0] = STORE_FAST; break;
2811 case DELETE_NAME: cur_instr[0] = DELETE_FAST; break;
2812 }
2813 cur_instr[1] = i & 0xff;
2814 cur_instr[2] = (i>>8) & 0xff;
Guido van Rossum282914b1991-04-04 10:42:56 +00002815 }
2816 }
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002817
Guido van Rossum681d79a1995-07-18 14:51:37 +00002818 if (c->c_errors == 0)
2819 err_restore(error_type, error_value, error_traceback);
Guido van Rossum282914b1991-04-04 10:42:56 +00002820}
2821
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002822codeobject *
Guido van Rossum590baa41993-11-30 13:40:46 +00002823compile(n, filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002824 node *n;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002825 char *filename;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002826{
2827 struct compiling sc;
2828 codeobject *co;
Guido van Rossum590baa41993-11-30 13:40:46 +00002829 if (!com_init(&sc, filename))
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002830 return NULL;
2831 compile_node(&sc, n);
2832 com_done(&sc);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002833 if ((TYPE(n) == funcdef || TYPE(n) == lambdef) && sc.c_errors == 0) {
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00002834 optimize(&sc);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002835 sc.c_flags |= CO_NEWLOCALS;
2836 }
2837 else if (TYPE(n) == classdef)
2838 sc.c_flags |= CO_NEWLOCALS;
Guido van Rossum9bfef441993-03-29 10:43:31 +00002839 co = NULL;
2840 if (sc.c_errors == 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002841 object *consts, *names, *varnames, *filename, *name;
2842 consts = listtuple(sc.c_consts);
2843 names = listtuple(sc.c_names);
2844 varnames = listtuple(sc.c_varnames);
2845 filename = newstringobject(sc.c_filename);
2846 name = newstringobject(sc.c_name);
2847 if (!err_occurred())
2848 co = newcodeobject(sc.c_argcount,
2849 sc.c_nlocals,
2850 sc.c_flags,
2851 sc.c_code,
2852 consts,
2853 names,
2854 varnames,
2855 filename,
2856 name);
2857 XDECREF(consts);
2858 XDECREF(names);
2859 XDECREF(varnames);
2860 XDECREF(filename);
2861 XDECREF(name);
Guido van Rossuma082ce41991-06-04 19:41:56 +00002862 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002863 com_free(&sc);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002864 return co;
2865}