|
69
|
1 #define DEBUG
|
|
|
2 /* Compile an expression node to intermediate code */
|
|
|
3
|
|
|
4 #include <stdio.h>
|
|
|
5 #include <ctype.h>
|
|
|
6 #include "string.h"
|
|
|
7
|
|
|
8 #include "PROTO.h"
|
|
|
9 #include "object.h"
|
|
|
10 #include "objimpl.h"
|
|
|
11 #include "intobject.h"
|
|
|
12 #include "floatobject.h"
|
|
|
13 #include "stringobject.h"
|
|
|
14 #include "listobject.h"
|
|
|
15 #include "node.h"
|
|
|
16 #include "token.h"
|
|
|
17 #include "graminit.h"
|
|
|
18 #include "errors.h"
|
|
|
19 #include "compile.h"
|
|
|
20 #include "opcode.h"
|
|
|
21
|
|
|
22 static void
|
|
|
23 code_dealloc(c)
|
|
|
24 codeobject *c;
|
|
|
25 {
|
|
|
26 XDECREF(c->co_code);
|
|
|
27 XDECREF(c->co_consts);
|
|
|
28 XDECREF(c->co_names);
|
|
|
29 DEL(c);
|
|
|
30 }
|
|
|
31
|
|
|
32 typeobject Codetype = {
|
|
|
33 OB_HEAD_INIT(&Typetype)
|
|
|
34 0,
|
|
|
35 "code",
|
|
|
36 sizeof(codeobject),
|
|
|
37 0,
|
|
|
38 code_dealloc, /*tp_dealloc*/
|
|
|
39 0, /*tp_print*/
|
|
|
40 0, /*tp_getattr*/
|
|
|
41 0, /*tp_setattr*/
|
|
|
42 0, /*tp_compare*/
|
|
|
43 0, /*tp_repr*/
|
|
|
44 0, /*tp_as_number*/
|
|
|
45 0, /*tp_as_sequence*/
|
|
|
46 0, /*tp_as_mapping*/
|
|
|
47 };
|
|
|
48
|
|
|
49 static codeobject *newcodeobject PROTO((object *, object *, object *));
|
|
|
50
|
|
|
51 static codeobject *
|
|
|
52 newcodeobject(code, consts, names)
|
|
|
53 object *code;
|
|
|
54 object *consts;
|
|
|
55 object *names;
|
|
|
56 {
|
|
|
57 codeobject *co;
|
|
|
58 int i;
|
|
|
59 /* Check argument types */
|
|
|
60 if (code == NULL || !is_stringobject(code) ||
|
|
|
61 consts == NULL || !is_listobject(consts) ||
|
|
|
62 names == NULL || !is_listobject(names)) {
|
|
|
63 err_badcall();
|
|
|
64 return NULL;
|
|
|
65 }
|
|
|
66 /* Make sure the list of names contains only strings */
|
|
|
67 for (i = getlistsize(names); --i >= 0; ) {
|
|
|
68 object *v = getlistitem(names, i);
|
|
|
69 if (v == NULL || !is_stringobject(v)) {
|
|
|
70 err_badcall();
|
|
|
71 return NULL;
|
|
|
72 }
|
|
|
73 }
|
|
|
74 co = NEWOBJ(codeobject, &Codetype);
|
|
|
75 if (co != NULL) {
|
|
|
76 INCREF(code);
|
|
|
77 co->co_code = (stringobject *)code;
|
|
|
78 INCREF(consts);
|
|
|
79 co->co_consts = consts;
|
|
|
80 INCREF(names);
|
|
|
81 co->co_names = names;
|
|
|
82 }
|
|
|
83 return co;
|
|
|
84 }
|
|
|
85
|
|
|
86
|
|
|
87 /* Data structure used internally */
|
|
|
88 struct compiling {
|
|
|
89 object *c_code; /* string */
|
|
|
90 object *c_consts; /* list of objects */
|
|
|
91 object *c_names; /* list of strings (names) */
|
|
|
92 int c_nexti; /* index into c_code */
|
|
|
93 int c_errors; /* counts errors occurred */
|
|
|
94 };
|
|
|
95
|
|
|
96 /* Prototypes */
|
|
|
97 static int com_init PROTO((struct compiling *));
|
|
|
98 static void com_free PROTO((struct compiling *));
|
|
|
99 static void com_done PROTO((struct compiling *));
|
|
|
100 static void com_node PROTO((struct compiling *, struct _node *));
|
|
|
101 static void com_addbyte PROTO((struct compiling *, int));
|
|
|
102 static void com_addint PROTO((struct compiling *, int));
|
|
|
103 static void com_addoparg PROTO((struct compiling *, int, int));
|
|
|
104 static void com_addfwref PROTO((struct compiling *, int, int *));
|
|
|
105 static void com_backpatch PROTO((struct compiling *, int));
|
|
|
106 static int com_add PROTO((struct compiling *, object *, object *));
|
|
|
107 static int com_addconst PROTO((struct compiling *, object *));
|
|
|
108 static int com_addname PROTO((struct compiling *, object *));
|
|
|
109 static void com_addopname PROTO((struct compiling *, int, node *));
|
|
|
110
|
|
|
111 static int
|
|
|
112 com_init(c)
|
|
|
113 struct compiling *c;
|
|
|
114 {
|
|
|
115 if ((c->c_code = newsizedstringobject((char *)NULL, 0)) == NULL)
|
|
|
116 goto fail_3;
|
|
|
117 if ((c->c_consts = newlistobject(0)) == NULL)
|
|
|
118 goto fail_2;
|
|
|
119 if ((c->c_names = newlistobject(0)) == NULL)
|
|
|
120 goto fail_1;
|
|
|
121 c->c_nexti = 0;
|
|
|
122 c->c_errors = 0;
|
|
|
123 return 1;
|
|
|
124
|
|
|
125 fail_1:
|
|
|
126 DECREF(c->c_consts);
|
|
|
127 fail_2:
|
|
|
128 DECREF(c->c_code);
|
|
|
129 fail_3:
|
|
|
130 return 0;
|
|
|
131 }
|
|
|
132
|
|
|
133 static void
|
|
|
134 com_free(c)
|
|
|
135 struct compiling *c;
|
|
|
136 {
|
|
|
137 XDECREF(c->c_code);
|
|
|
138 XDECREF(c->c_consts);
|
|
|
139 XDECREF(c->c_names);
|
|
|
140 }
|
|
|
141
|
|
|
142 static void
|
|
|
143 com_done(c)
|
|
|
144 struct compiling *c;
|
|
|
145 {
|
|
|
146 if (c->c_code != NULL)
|
|
|
147 resizestring(&c->c_code, c->c_nexti);
|
|
|
148 }
|
|
|
149
|
|
|
150 static void
|
|
|
151 com_addbyte(c, byte)
|
|
|
152 struct compiling *c;
|
|
|
153 int byte;
|
|
|
154 {
|
|
|
155 int len;
|
|
|
156 if (byte < 0 || byte > 255) {
|
|
|
157 err_setstr(SystemError, "com_addbyte: byte out of range");
|
|
|
158 c->c_errors++;
|
|
|
159 }
|
|
|
160 if (c->c_code == NULL)
|
|
|
161 return;
|
|
|
162 len = getstringsize(c->c_code);
|
|
|
163 if (c->c_nexti >= len) {
|
|
|
164 if (resizestring(&c->c_code, len+1000) != 0) {
|
|
|
165 c->c_errors++;
|
|
|
166 return;
|
|
|
167 }
|
|
|
168 }
|
|
|
169 getstringvalue(c->c_code)[c->c_nexti++] = byte;
|
|
|
170 }
|
|
|
171
|
|
|
172 static void
|
|
|
173 com_addint(c, x)
|
|
|
174 struct compiling *c;
|
|
|
175 int x;
|
|
|
176 {
|
|
|
177 com_addbyte(c, x);
|
|
|
178 }
|
|
|
179
|
|
|
180 static void
|
|
|
181 com_addoparg(c, op, arg)
|
|
|
182 struct compiling *c;
|
|
|
183 int op;
|
|
|
184 int arg;
|
|
|
185 {
|
|
|
186 com_addbyte(c, op);
|
|
|
187 com_addint(c, arg);
|
|
|
188 }
|
|
|
189
|
|
|
190 static void
|
|
|
191 com_addfwref(c, op, p_anchor)
|
|
|
192 struct compiling *c;
|
|
|
193 int op;
|
|
|
194 int *p_anchor;
|
|
|
195 {
|
|
|
196 /* Compile a forward reference for backpatching */
|
|
|
197 int here;
|
|
|
198 int anchor;
|
|
|
199 com_addbyte(c, op);
|
|
|
200 here = c->c_nexti;
|
|
|
201 anchor = *p_anchor;
|
|
|
202 *p_anchor = here;
|
|
|
203 com_addint(c, anchor == 0 ? 0 : here - anchor);
|
|
|
204 }
|
|
|
205
|
|
|
206 static void
|
|
|
207 com_backpatch(c, anchor)
|
|
|
208 struct compiling *c;
|
|
|
209 int anchor; /* Must be nonzero */
|
|
|
210 {
|
|
|
211 unsigned char *code = (unsigned char *) getstringvalue(c->c_code);
|
|
|
212 int target = c->c_nexti;
|
|
|
213 int lastanchor = 0;
|
|
|
214 int dist;
|
|
|
215 int prev;
|
|
|
216 for (;;) {
|
|
|
217 /* Make the JUMP instruction at anchor point to target */
|
|
|
218 prev = code[anchor];
|
|
|
219 dist = target - (anchor+1);
|
|
|
220 if (dist > 255 && lastanchor &&
|
|
|
221 code[lastanchor-1] == code[anchor-1]) {
|
|
|
222 /* Attempt to jump to a similar jump closer by */
|
|
|
223 /* XXX Show that it works */
|
|
|
224 fprintf(stderr, "com_backpatch: huge jump rescued (?)\n");
|
|
|
225 target = lastanchor-1;
|
|
|
226 dist = target - (anchor+1);
|
|
|
227 lastanchor = 0;
|
|
|
228 }
|
|
|
229 if (dist > 255) {
|
|
|
230 err_setstr(SystemError, "relative jump > 255 bytes");
|
|
|
231 c->c_errors++;
|
|
|
232 }
|
|
|
233 code[anchor] = dist;
|
|
|
234 if (!prev)
|
|
|
235 break;
|
|
|
236 lastanchor = anchor;
|
|
|
237 anchor -= prev;
|
|
|
238 }
|
|
|
239 }
|
|
|
240
|
|
|
241 /* Handle constants and names uniformly */
|
|
|
242
|
|
|
243 static int
|
|
|
244 com_add(c, list, v)
|
|
|
245 struct compiling *c;
|
|
|
246 object *list;
|
|
|
247 object *v;
|
|
|
248 {
|
|
|
249 /* XXX Should look through list for object with same value */
|
|
|
250 int i = getlistsize(list);
|
|
|
251 if (addlistitem(list, v) != 0)
|
|
|
252 c->c_errors++;
|
|
|
253 return i;
|
|
|
254 }
|
|
|
255
|
|
|
256 static int
|
|
|
257 com_addconst(c, v)
|
|
|
258 struct compiling *c;
|
|
|
259 object *v;
|
|
|
260 {
|
|
|
261 return com_add(c, c->c_consts, v);
|
|
|
262 }
|
|
|
263
|
|
|
264 static int
|
|
|
265 com_addname(c, v)
|
|
|
266 struct compiling *c;
|
|
|
267 object *v;
|
|
|
268 {
|
|
|
269 return com_add(c, c->c_names, v);
|
|
|
270 }
|
|
|
271
|
|
|
272 static void
|
|
|
273 com_addopname(c, op, n)
|
|
|
274 struct compiling *c;
|
|
|
275 int op;
|
|
|
276 node *n;
|
|
|
277 {
|
|
|
278 object *v;
|
|
|
279 int i;
|
|
|
280 char *name;
|
|
|
281 if (TYPE(n) == STAR)
|
|
|
282 name = "*";
|
|
|
283 else {
|
|
|
284 REQ(n, NAME);
|
|
|
285 name = STR(n);
|
|
|
286 }
|
|
|
287 if ((v = newstringobject(name)) == NULL) {
|
|
|
288 c->c_errors++;
|
|
|
289 i = 255;
|
|
|
290 }
|
|
|
291 else {
|
|
|
292 i = com_addname(c, v);
|
|
|
293 DECREF(v);
|
|
|
294 }
|
|
|
295 com_addoparg(c, op, i);
|
|
|
296 }
|
|
|
297
|
|
|
298 static object *
|
|
|
299 parsenumber(s)
|
|
|
300 char *s;
|
|
|
301 {
|
|
|
302 extern long strtol();
|
|
|
303 extern double atof();
|
|
|
304 char *end = s;
|
|
|
305 long x;
|
|
|
306 x = strtol(s, &end, 0);
|
|
|
307 if (*end == '\0')
|
|
|
308 return newintobject(x);
|
|
|
309 if (*end == '.' || *end == 'e' || *end == 'E')
|
|
|
310 return newfloatobject(atof(s));
|
|
|
311 err_setstr(RuntimeError, "bad number syntax");
|
|
|
312 return NULL;
|
|
|
313 }
|
|
|
314
|
|
|
315 static object *
|
|
|
316 parsestr(s)
|
|
|
317 char *s;
|
|
|
318 {
|
|
|
319 object *v;
|
|
|
320 int len;
|
|
|
321 char *buf;
|
|
|
322 char *p;
|
|
|
323 int c;
|
|
|
324 if (*s != '\'') {
|
|
|
325 err_badcall();
|
|
|
326 return NULL;
|
|
|
327 }
|
|
|
328 s++;
|
|
|
329 len = strlen(s);
|
|
|
330 if (s[--len] != '\'') {
|
|
|
331 err_badcall();
|
|
|
332 return NULL;
|
|
|
333 }
|
|
|
334 if (strchr(s, '\\') == NULL)
|
|
|
335 return newsizedstringobject(s, len);
|
|
|
336 v = newsizedstringobject((char *)NULL, len);
|
|
|
337 p = buf = getstringvalue(v);
|
|
|
338 while (*s != '\0' && *s != '\'') {
|
|
|
339 if (*s != '\\') {
|
|
|
340 *p++ = *s++;
|
|
|
341 continue;
|
|
|
342 }
|
|
|
343 s++;
|
|
|
344 switch (*s++) {
|
|
|
345 /* XXX This assumes ASCII! */
|
|
|
346 case '\\': *p++ = '\\'; break;
|
|
|
347 case '\'': *p++ = '\''; break;
|
|
|
348 case 'b': *p++ = '\b'; break;
|
|
|
349 case 'f': *p++ = '\014'; break; /* FF */
|
|
|
350 case 't': *p++ = '\t'; break;
|
|
|
351 case 'n': *p++ = '\n'; break;
|
|
|
352 case 'r': *p++ = '\r'; break;
|
|
|
353 case 'v': *p++ = '\013'; break; /* VT */
|
|
|
354 case 'E': *p++ = '\033'; break; /* ESC, not C */
|
|
|
355 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
|
|
|
356 case '0': case '1': case '2': case '3':
|
|
|
357 case '4': case '5': case '6': case '7':
|
|
|
358 c = s[-1] - '0';
|
|
|
359 if ('0' <= *s && *s <= '7') {
|
|
|
360 c = (c<<3) + *s++ - '0';
|
|
|
361 if ('0' <= *s && *s <= '7')
|
|
|
362 c = (c<<3) + *s++ - '0';
|
|
|
363 }
|
|
|
364 *p++ = c;
|
|
|
365 break;
|
|
|
366 case 'x':
|
|
|
367 if (isxdigit(*s)) {
|
|
|
368 sscanf(s, "%x", &c);
|
|
|
369 *p++ = c;
|
|
|
370 do {
|
|
|
371 s++;
|
|
|
372 } while (isxdigit(*s));
|
|
|
373 break;
|
|
|
374 }
|
|
|
375 /* FALLTHROUGH */
|
|
|
376 default: *p++ = '\\'; *p++ = s[-1]; break;
|
|
|
377 }
|
|
|
378 }
|
|
|
379 resizestring(&v, (int)(p - buf));
|
|
|
380 return v;
|
|
|
381 }
|
|
|
382
|
|
|
383 static void
|
|
|
384 com_list_constructor(c, n)
|
|
|
385 struct compiling *c;
|
|
|
386 node *n;
|
|
|
387 {
|
|
|
388 int len;
|
|
|
389 int i;
|
|
|
390 object *v, *w;
|
|
|
391 if (TYPE(n) != testlist)
|
|
|
392 REQ(n, exprlist);
|
|
|
393 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
|
|
|
394 len = (NCH(n) + 1) / 2;
|
|
|
395 for (i = 0; i < NCH(n); i += 2)
|
|
|
396 com_node(c, CHILD(n, i));
|
|
|
397 com_addoparg(c, BUILD_LIST, len);
|
|
|
398 }
|
|
|
399
|
|
|
400 static void
|
|
|
401 com_atom(c, n)
|
|
|
402 struct compiling *c;
|
|
|
403 node *n;
|
|
|
404 {
|
|
|
405 node *ch;
|
|
|
406 object *v;
|
|
|
407 int i;
|
|
|
408 REQ(n, atom);
|
|
|
409 ch = CHILD(n, 0);
|
|
|
410 switch (TYPE(ch)) {
|
|
|
411 case LPAR:
|
|
|
412 if (TYPE(CHILD(n, 1)) == RPAR)
|
|
|
413 com_addoparg(c, BUILD_TUPLE, 0);
|
|
|
414 else
|
|
|
415 com_node(c, CHILD(n, 1));
|
|
|
416 break;
|
|
|
417 case LSQB:
|
|
|
418 if (TYPE(CHILD(n, 1)) == RSQB)
|
|
|
419 com_addoparg(c, BUILD_LIST, 0);
|
|
|
420 else
|
|
|
421 com_list_constructor(c, CHILD(n, 1));
|
|
|
422 break;
|
|
|
423 case LBRACE:
|
|
|
424 com_addoparg(c, BUILD_MAP, 0);
|
|
|
425 break;
|
|
|
426 case BACKQUOTE:
|
|
|
427 com_node(c, CHILD(n, 1));
|
|
|
428 com_addbyte(c, UNARY_CONVERT);
|
|
|
429 break;
|
|
|
430 case NUMBER:
|
|
|
431 if ((v = parsenumber(STR(ch))) == NULL) {
|
|
|
432 c->c_errors++;
|
|
|
433 i = 255;
|
|
|
434 }
|
|
|
435 else {
|
|
|
436 i = com_addconst(c, v);
|
|
|
437 DECREF(v);
|
|
|
438 }
|
|
|
439 com_addoparg(c, LOAD_CONST, i);
|
|
|
440 break;
|
|
|
441 case STRING:
|
|
|
442 if ((v = parsestr(STR(ch))) == NULL) {
|
|
|
443 c->c_errors++;
|
|
|
444 i = 255;
|
|
|
445 }
|
|
|
446 else {
|
|
|
447 i = com_addconst(c, v);
|
|
|
448 DECREF(v);
|
|
|
449 }
|
|
|
450 com_addoparg(c, LOAD_CONST, i);
|
|
|
451 break;
|
|
|
452 case NAME:
|
|
|
453 com_addopname(c, LOAD_NAME, ch);
|
|
|
454 break;
|
|
|
455 default:
|
|
|
456 fprintf(stderr, "node type %d\n", TYPE(ch));
|
|
|
457 err_setstr(SystemError, "com_atom: unexpected node type");
|
|
|
458 c->c_errors++;
|
|
|
459 }
|
|
|
460 }
|
|
|
461
|
|
|
462 static void
|
|
|
463 com_slice(c, n, op)
|
|
|
464 struct compiling *c;
|
|
|
465 node *n;
|
|
|
466 int op;
|
|
|
467 {
|
|
|
468 if (NCH(n) == 1) {
|
|
|
469 com_addbyte(c, op);
|
|
|
470 }
|
|
|
471 else if (NCH(n) == 2) {
|
|
|
472 if (TYPE(CHILD(n, 0)) != COLON) {
|
|
|
473 com_node(c, CHILD(n, 0));
|
|
|
474 com_addbyte(c, op+1);
|
|
|
475 }
|
|
|
476 else {
|
|
|
477 com_node(c, CHILD(n, 1));
|
|
|
478 com_addbyte(c, op+2);
|
|
|
479 }
|
|
|
480 }
|
|
|
481 else {
|
|
|
482 com_node(c, CHILD(n, 0));
|
|
|
483 com_node(c, CHILD(n, 2));
|
|
|
484 com_addbyte(c, op+3);
|
|
|
485 }
|
|
|
486 }
|
|
|
487
|
|
|
488 static void
|
|
|
489 com_apply_subscript(c, n)
|
|
|
490 struct compiling *c;
|
|
|
491 node *n;
|
|
|
492 {
|
|
|
493 REQ(n, subscript);
|
|
|
494 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) != COLON) {
|
|
|
495 /* It's a single subscript */
|
|
|
496 com_node(c, CHILD(n, 0));
|
|
|
497 com_addbyte(c, BINARY_SUBSCR);
|
|
|
498 }
|
|
|
499 else {
|
|
|
500 /* It's a slice: [expr] ':' [expr] */
|
|
|
501 com_slice(c, n, SLICE);
|
|
|
502 }
|
|
|
503 }
|
|
|
504
|
|
|
505 static void
|
|
|
506 com_call_function(c, n)
|
|
|
507 struct compiling *c;
|
|
|
508 node *n; /* EITHER testlist OR ')' */
|
|
|
509 {
|
|
|
510 if (TYPE(n) == RPAR) {
|
|
|
511 com_addbyte(c, UNARY_CALL);
|
|
|
512 }
|
|
|
513 else {
|
|
|
514 com_node(c, n);
|
|
|
515 com_addbyte(c, BINARY_CALL);
|
|
|
516 }
|
|
|
517 }
|
|
|
518
|
|
|
519 static void
|
|
|
520 com_select_member(c, n)
|
|
|
521 struct compiling *c;
|
|
|
522 node *n;
|
|
|
523 {
|
|
|
524 com_addopname(c, LOAD_ATTR, n);
|
|
|
525 }
|
|
|
526
|
|
|
527 static void
|
|
|
528 com_apply_trailer(c, n)
|
|
|
529 struct compiling *c;
|
|
|
530 node *n;
|
|
|
531 {
|
|
|
532 REQ(n, trailer);
|
|
|
533 switch (TYPE(CHILD(n, 0))) {
|
|
|
534 case LPAR:
|
|
|
535 com_call_function(c, CHILD(n, 1));
|
|
|
536 break;
|
|
|
537 case DOT:
|
|
|
538 com_select_member(c, CHILD(n, 1));
|
|
|
539 break;
|
|
|
540 case LSQB:
|
|
|
541 com_apply_subscript(c, CHILD(n, 1));
|
|
|
542 break;
|
|
|
543 default:
|
|
|
544 err_setstr(SystemError,
|
|
|
545 "com_apply_trailer: unknown trailer type");
|
|
|
546 c->c_errors++;
|
|
|
547 }
|
|
|
548 }
|
|
|
549
|
|
|
550 static void
|
|
|
551 com_factor(c, n)
|
|
|
552 struct compiling *c;
|
|
|
553 node *n;
|
|
|
554 {
|
|
|
555 int i;
|
|
|
556 REQ(n, factor);
|
|
|
557 if (TYPE(CHILD(n, 0)) == PLUS) {
|
|
|
558 com_factor(c, CHILD(n, 1));
|
|
|
559 com_addbyte(c, UNARY_POSITIVE);
|
|
|
560 }
|
|
|
561 else if (TYPE(CHILD(n, 0)) == MINUS) {
|
|
|
562 com_factor(c, CHILD(n, 1));
|
|
|
563 com_addbyte(c, UNARY_NEGATIVE);
|
|
|
564 }
|
|
|
565 else {
|
|
|
566 com_atom(c, CHILD(n, 0));
|
|
|
567 for (i = 1; i < NCH(n); i++)
|
|
|
568 com_apply_trailer(c, CHILD(n, i));
|
|
|
569 }
|
|
|
570 }
|
|
|
571
|
|
|
572 static void
|
|
|
573 com_term(c, n)
|
|
|
574 struct compiling *c;
|
|
|
575 node *n;
|
|
|
576 {
|
|
|
577 int i;
|
|
|
578 int op;
|
|
|
579 REQ(n, term);
|
|
|
580 com_factor(c, CHILD(n, 0));
|
|
|
581 for (i = 2; i < NCH(n); i += 2) {
|
|
|
582 com_factor(c, CHILD(n, i));
|
|
|
583 switch (TYPE(CHILD(n, i-1))) {
|
|
|
584 case STAR:
|
|
|
585 op = BINARY_MULTIPLY;
|
|
|
586 break;
|
|
|
587 case SLASH:
|
|
|
588 op = BINARY_DIVIDE;
|
|
|
589 break;
|
|
|
590 case PERCENT:
|
|
|
591 op = BINARY_MODULO;
|
|
|
592 break;
|
|
|
593 default:
|
|
|
594 err_setstr(SystemError,
|
|
|
595 "com_term: term operator not *, / or %");
|
|
|
596 c->c_errors++;
|
|
|
597 op = 255;
|
|
|
598 }
|
|
|
599 com_addbyte(c, op);
|
|
|
600 }
|
|
|
601 }
|
|
|
602
|
|
|
603 static void
|
|
|
604 com_expr(c, n)
|
|
|
605 struct compiling *c;
|
|
|
606 node *n;
|
|
|
607 {
|
|
|
608 int i;
|
|
|
609 int op;
|
|
|
610 REQ(n, expr);
|
|
|
611 com_term(c, CHILD(n, 0));
|
|
|
612 for (i = 2; i < NCH(n); i += 2) {
|
|
|
613 com_term(c, CHILD(n, i));
|
|
|
614 switch (TYPE(CHILD(n, i-1))) {
|
|
|
615 case PLUS:
|
|
|
616 op = BINARY_ADD;
|
|
|
617 break;
|
|
|
618 case MINUS:
|
|
|
619 op = BINARY_SUBTRACT;
|
|
|
620 break;
|
|
|
621 default:
|
|
|
622 err_setstr(SystemError,
|
|
|
623 "com_expr: expr operator not + or -");
|
|
|
624 c->c_errors++;
|
|
|
625 op = 255;
|
|
|
626 }
|
|
|
627 com_addbyte(c, op);
|
|
|
628 }
|
|
|
629 }
|
|
|
630
|
|
|
631 static enum cmp_op
|
|
|
632 cmp_type(n)
|
|
|
633 node *n;
|
|
|
634 {
|
|
|
635 REQ(n, comp_op);
|
|
|
636 /* comp_op: '<' | '>' | '=' | '>' '=' | '<' '=' | '<' '>'
|
|
|
637 | 'in' | 'not' 'in' | 'is' | 'is' not' */
|
|
|
638 if (NCH(n) == 1) {
|
|
|
639 n = CHILD(n, 0);
|
|
|
640 switch (TYPE(n)) {
|
|
|
641 case LESS: return LT;
|
|
|
642 case GREATER: return GT;
|
|
|
643 case EQUAL: return EQ;
|
|
|
644 case NAME: if (strcmp(STR(n), "in") == 0) return IN;
|
|
|
645 if (strcmp(STR(n), "is") == 0) return IS;
|
|
|
646 }
|
|
|
647 }
|
|
|
648 else if (NCH(n) == 2) {
|
|
|
649 int t2 = TYPE(CHILD(n, 1));
|
|
|
650 switch (TYPE(CHILD(n, 0))) {
|
|
|
651 case LESS: if (t2 == EQUAL) return LE;
|
|
|
652 if (t2 == GREATER) return NE;
|
|
|
653 break;
|
|
|
654 case GREATER: if (t2 == EQUAL) return GE;
|
|
|
655 break;
|
|
|
656 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
|
|
|
657 return NOT_IN;
|
|
|
658 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
|
|
|
659 return IS_NOT;
|
|
|
660 }
|
|
|
661 }
|
|
|
662 return BAD;
|
|
|
663 }
|
|
|
664
|
|
|
665 static void
|
|
|
666 com_comparison(c, n)
|
|
|
667 struct compiling *c;
|
|
|
668 node *n;
|
|
|
669 {
|
|
|
670 int i;
|
|
|
671 enum cmp_op op;
|
|
|
672 int anchor;
|
|
|
673 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
|
|
|
674 com_expr(c, CHILD(n, 0));
|
|
|
675 if (NCH(n) == 1)
|
|
|
676 return;
|
|
|
677
|
|
|
678 /****************************************************************
|
|
|
679 The following code is generated for all but the last
|
|
|
680 comparison in a chain:
|
|
|
681
|
|
|
682 label: on stack: opcode: jump to:
|
|
|
683
|
|
|
684 a <code to load b>
|
|
|
685 a, b DUP_TOP
|
|
|
686 a, b, b ROT_THREE
|
|
|
687 b, a, b COMPARE_OP
|
|
|
688 b, 0-or-1 JUMP_IF_FALSE L1
|
|
|
689 b, 1 POP_TOP
|
|
|
690 b
|
|
|
691
|
|
|
692 We are now ready to repeat this sequence for the next
|
|
|
693 comparison in the chain.
|
|
|
694
|
|
|
695 For the last we generate:
|
|
|
696
|
|
|
697 b <code to load c>
|
|
|
698 b, c COMPARE_OP
|
|
|
699 0-or-1
|
|
|
700
|
|
|
701 If there were any jumps to L1 (i.e., there was more than one
|
|
|
702 comparison), we generate:
|
|
|
703
|
|
|
704 0-or-1 JUMP_FORWARD L2
|
|
|
705 L1: b, 0 ROT_TWO
|
|
|
706 0, b POP_TOP
|
|
|
707 0
|
|
|
708 L2:
|
|
|
709 ****************************************************************/
|
|
|
710
|
|
|
711 anchor = 0;
|
|
|
712
|
|
|
713 for (i = 2; i < NCH(n); i += 2) {
|
|
|
714 com_expr(c, CHILD(n, i));
|
|
|
715 if (i+2 < NCH(n)) {
|
|
|
716 com_addbyte(c, DUP_TOP);
|
|
|
717 com_addbyte(c, ROT_THREE);
|
|
|
718 }
|
|
|
719 op = cmp_type(CHILD(n, i-1));
|
|
|
720 if (op == BAD) {
|
|
|
721 err_setstr(SystemError,
|
|
|
722 "com_comparison: unknown comparison op");
|
|
|
723 c->c_errors++;
|
|
|
724 }
|
|
|
725 com_addoparg(c, COMPARE_OP, op);
|
|
|
726 if (i+2 < NCH(n)) {
|
|
|
727 com_addfwref(c, JUMP_IF_FALSE, &anchor);
|
|
|
728 com_addbyte(c, POP_TOP);
|
|
|
729 }
|
|
|
730 }
|
|
|
731
|
|
|
732 if (anchor) {
|
|
|
733 int anchor2 = 0;
|
|
|
734 com_addfwref(c, JUMP_FORWARD, &anchor2);
|
|
|
735 com_backpatch(c, anchor);
|
|
|
736 com_addbyte(c, ROT_TWO);
|
|
|
737 com_addbyte(c, POP_TOP);
|
|
|
738 com_backpatch(c, anchor2);
|
|
|
739 }
|
|
|
740 }
|
|
|
741
|
|
|
742 static void
|
|
|
743 com_not_test(c, n)
|
|
|
744 struct compiling *c;
|
|
|
745 node *n;
|
|
|
746 {
|
|
|
747 REQ(n, not_test); /* 'not' not_test | comparison */
|
|
|
748 if (NCH(n) == 1) {
|
|
|
749 com_comparison(c, CHILD(n, 0));
|
|
|
750 }
|
|
|
751 else {
|
|
|
752 com_not_test(c, CHILD(n, 1));
|
|
|
753 com_addbyte(c, UNARY_NOT);
|
|
|
754 }
|
|
|
755 }
|
|
|
756
|
|
|
757 static void
|
|
|
758 com_and_test(c, n)
|
|
|
759 struct compiling *c;
|
|
|
760 node *n;
|
|
|
761 {
|
|
|
762 int i;
|
|
|
763 int anchor;
|
|
|
764 REQ(n, and_test); /* not_test ('and' not_test)* */
|
|
|
765 anchor = 0;
|
|
|
766 i = 0;
|
|
|
767 for (;;) {
|
|
|
768 com_not_test(c, CHILD(n, i));
|
|
|
769 if ((i += 2) >= NCH(n))
|
|
|
770 break;
|
|
|
771 com_addfwref(c, JUMP_IF_FALSE, &anchor);
|
|
|
772 com_addbyte(c, POP_TOP);
|
|
|
773 }
|
|
|
774 if (anchor)
|
|
|
775 com_backpatch(c, anchor);
|
|
|
776 }
|
|
|
777
|
|
|
778 static void
|
|
|
779 com_test(c, n)
|
|
|
780 struct compiling *c;
|
|
|
781 node *n;
|
|
|
782 {
|
|
|
783 int i;
|
|
|
784 int anchor;
|
|
|
785 REQ(n, test); /* and_test ('and' and_test)* */
|
|
|
786 anchor = 0;
|
|
|
787 i = 0;
|
|
|
788 for (;;) {
|
|
|
789 com_and_test(c, CHILD(n, i));
|
|
|
790 if ((i += 2) >= NCH(n))
|
|
|
791 break;
|
|
|
792 com_addfwref(c, JUMP_IF_TRUE, &anchor);
|
|
|
793 com_addbyte(c, POP_TOP);
|
|
|
794 }
|
|
|
795 if (anchor)
|
|
|
796 com_backpatch(c, anchor);
|
|
|
797 }
|
|
|
798
|
|
|
799 static void
|
|
|
800 com_list(c, n)
|
|
|
801 struct compiling *c;
|
|
|
802 node *n;
|
|
|
803 {
|
|
|
804 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
|
|
|
805 if (NCH(n) == 1) {
|
|
|
806 com_node(c, CHILD(n, 0));
|
|
|
807 }
|
|
|
808 else {
|
|
|
809 int i;
|
|
|
810 int len;
|
|
|
811 len = (NCH(n) + 1) / 2;
|
|
|
812 for (i = 0; i < NCH(n); i += 2)
|
|
|
813 com_node(c, CHILD(n, i));
|
|
|
814 com_addoparg(c, BUILD_TUPLE, len);
|
|
|
815 }
|
|
|
816 }
|
|
|
817
|
|
|
818
|
|
|
819 /* Begin of assignment compilation */
|
|
|
820
|
|
|
821 static void com_assign_name PROTO((struct compiling *, node *, int));
|
|
|
822 static void com_assign PROTO((struct compiling *, node *, int));
|
|
|
823
|
|
|
824 static void
|
|
|
825 com_assign_attr(c, n, assigning)
|
|
|
826 struct compiling *c;
|
|
|
827 node *n;
|
|
|
828 int assigning;
|
|
|
829 {
|
|
|
830 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
|
|
|
831 }
|
|
|
832
|
|
|
833 static void
|
|
|
834 com_assign_slice(c, n, assigning)
|
|
|
835 struct compiling *c;
|
|
|
836 node *n;
|
|
|
837 int assigning;
|
|
|
838 {
|
|
|
839 com_slice(c, n, assigning ? STORE_SLICE : DELETE_SLICE);
|
|
|
840 }
|
|
|
841
|
|
|
842 static void
|
|
|
843 com_assign_subscript(c, n, assigning)
|
|
|
844 struct compiling *c;
|
|
|
845 node *n;
|
|
|
846 int assigning;
|
|
|
847 {
|
|
|
848 com_node(c, n);
|
|
|
849 com_addbyte(c, assigning ? STORE_SUBSCR : DELETE_SUBSCR);
|
|
|
850 }
|
|
|
851
|
|
|
852 static void
|
|
|
853 com_assign_trailer(c, n, assigning)
|
|
|
854 struct compiling *c;
|
|
|
855 node *n;
|
|
|
856 int assigning;
|
|
|
857 {
|
|
|
858 char *name;
|
|
|
859 REQ(n, trailer);
|
|
|
860 switch (TYPE(CHILD(n, 0))) {
|
|
|
861 case LPAR: /* '(' [exprlist] ')' */
|
|
|
862 err_setstr(TypeError, "can't assign to function call");
|
|
|
863 c->c_errors++;
|
|
|
864 break;
|
|
|
865 case DOT: /* '.' NAME */
|
|
|
866 com_assign_attr(c, CHILD(n, 1), assigning);
|
|
|
867 break;
|
|
|
868 case LSQB: /* '[' subscript ']' */
|
|
|
869 n = CHILD(n, 1);
|
|
|
870 REQ(n, subscript); /* subscript: expr | [expr] ':' [expr] */
|
|
|
871 if (NCH(n) > 1 || TYPE(CHILD(n, 0)) == COLON)
|
|
|
872 com_assign_slice(c, n, assigning);
|
|
|
873 else
|
|
|
874 com_assign_subscript(c, CHILD(n, 0), assigning);
|
|
|
875 break;
|
|
|
876 default:
|
|
|
877 err_setstr(TypeError, "unknown trailer type");
|
|
|
878 c->c_errors++;
|
|
|
879 }
|
|
|
880 }
|
|
|
881
|
|
|
882 static void
|
|
|
883 com_assign_tuple(c, n, assigning)
|
|
|
884 struct compiling *c;
|
|
|
885 node *n;
|
|
|
886 int assigning;
|
|
|
887 {
|
|
|
888 int i;
|
|
|
889 if (TYPE(n) != testlist)
|
|
|
890 REQ(n, exprlist);
|
|
|
891 if (assigning)
|
|
|
892 com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2);
|
|
|
893 for (i = 0; i < NCH(n); i += 2)
|
|
|
894 com_assign(c, CHILD(n, i), assigning);
|
|
|
895 }
|
|
|
896
|
|
|
897 static void
|
|
|
898 com_assign_list(c, n, assigning)
|
|
|
899 struct compiling *c;
|
|
|
900 node *n;
|
|
|
901 int assigning;
|
|
|
902 {
|
|
|
903 int i;
|
|
|
904 if (assigning)
|
|
|
905 com_addoparg(c, UNPACK_LIST, (NCH(n)+1)/2);
|
|
|
906 for (i = 0; i < NCH(n); i += 2)
|
|
|
907 com_assign(c, CHILD(n, i), assigning);
|
|
|
908 }
|
|
|
909
|
|
|
910 static void
|
|
|
911 com_assign_name(c, n, assigning)
|
|
|
912 struct compiling *c;
|
|
|
913 node *n;
|
|
|
914 int assigning;
|
|
|
915 {
|
|
|
916 REQ(n, NAME);
|
|
|
917 com_addopname(c, assigning ? STORE_NAME : DELETE_NAME, n);
|
|
|
918 }
|
|
|
919
|
|
|
920 static void
|
|
|
921 com_assign(c, n, assigning)
|
|
|
922 struct compiling *c;
|
|
|
923 node *n;
|
|
|
924 int assigning;
|
|
|
925 {
|
|
|
926 /* Loop to avoid trivial recursion */
|
|
|
927 for (;;) {
|
|
|
928 switch (TYPE(n)) {
|
|
|
929 case exprlist:
|
|
|
930 case testlist:
|
|
|
931 if (NCH(n) > 1) {
|
|
|
932 com_assign_tuple(c, n, assigning);
|
|
|
933 return;
|
|
|
934 }
|
|
|
935 n = CHILD(n, 0);
|
|
|
936 break;
|
|
|
937 case test:
|
|
|
938 case and_test:
|
|
|
939 case not_test:
|
|
|
940 if (NCH(n) > 1) {
|
|
|
941 err_setstr(TypeError,
|
|
|
942 "can't assign to operator");
|
|
|
943 c->c_errors++;
|
|
|
944 return;
|
|
|
945 }
|
|
|
946 n = CHILD(n, 0);
|
|
|
947 break;
|
|
|
948 case comparison:
|
|
|
949 if (NCH(n) > 1) {
|
|
|
950 err_setstr(TypeError,
|
|
|
951 "can't assign to operator");
|
|
|
952 c->c_errors++;
|
|
|
953 return;
|
|
|
954 }
|
|
|
955 n = CHILD(n, 0);
|
|
|
956 break;
|
|
|
957 case expr:
|
|
|
958 if (NCH(n) > 1) {
|
|
|
959 err_setstr(TypeError,
|
|
|
960 "can't assign to operator");
|
|
|
961 c->c_errors++;
|
|
|
962 return;
|
|
|
963 }
|
|
|
964 n = CHILD(n, 0);
|
|
|
965 break;
|
|
|
966 case term:
|
|
|
967 if (NCH(n) > 1) {
|
|
|
968 err_setstr(TypeError,
|
|
|
969 "can't assign to operator");
|
|
|
970 c->c_errors++;
|
|
|
971 return;
|
|
|
972 }
|
|
|
973 n = CHILD(n, 0);
|
|
|
974 break;
|
|
|
975 case factor: /* ('+'|'-') factor | atom trailer* */
|
|
|
976 if (TYPE(CHILD(n, 0)) != atom) { /* '+' | '-' */
|
|
|
977 err_setstr(TypeError,
|
|
|
978 "can't assign to operator");
|
|
|
979 c->c_errors++;
|
|
|
980 return;
|
|
|
981 }
|
|
|
982 if (NCH(n) > 1) { /* trailer present */
|
|
|
983 int i;
|
|
|
984 com_node(c, CHILD(n, 0));
|
|
|
985 for (i = 1; i+1 < NCH(n); i++) {
|
|
|
986 com_apply_trailer(c, CHILD(n, i));
|
|
|
987 } /* NB i is still alive */
|
|
|
988 com_assign_trailer(c,
|
|
|
989 CHILD(n, i), assigning);
|
|
|
990 return;
|
|
|
991 }
|
|
|
992 n = CHILD(n, 0);
|
|
|
993 break;
|
|
|
994 case atom:
|
|
|
995 switch (TYPE(CHILD(n, 0))) {
|
|
|
996 case LPAR:
|
|
|
997 n = CHILD(n, 1);
|
|
|
998 if (TYPE(n) == RPAR) {
|
|
|
999 /* XXX Should allow () = () ??? */
|
|
|
1000 err_setstr(TypeError,
|
|
|
1001 "can't assign to ()");
|
|
|
1002 c->c_errors++;
|
|
|
1003 return;
|
|
|
1004 }
|
|
|
1005 break;
|
|
|
1006 case LSQB:
|
|
|
1007 n = CHILD(n, 1);
|
|
|
1008 if (TYPE(n) == RSQB) {
|
|
|
1009 err_setstr(TypeError,
|
|
|
1010 "can't assign to []");
|
|
|
1011 c->c_errors++;
|
|
|
1012 return;
|
|
|
1013 }
|
|
|
1014 com_assign_list(c, n, assigning);
|
|
|
1015 return;
|
|
|
1016 case NAME:
|
|
|
1017 com_assign_name(c, CHILD(n, 0), assigning);
|
|
|
1018 return;
|
|
|
1019 default:
|
|
|
1020 err_setstr(TypeError,
|
|
|
1021 "can't assign to constant");
|
|
|
1022 c->c_errors++;
|
|
|
1023 return;
|
|
|
1024 }
|
|
|
1025 break;
|
|
|
1026 default:
|
|
|
1027 fprintf(stderr, "node type %d\n", TYPE(n));
|
|
|
1028 err_setstr(SystemError, "com_assign: bad node");
|
|
|
1029 c->c_errors++;
|
|
|
1030 return;
|
|
|
1031 }
|
|
|
1032 }
|
|
|
1033 }
|
|
|
1034
|
|
|
1035 static void
|
|
|
1036 com_expr_stmt(c, n)
|
|
|
1037 struct compiling *c;
|
|
|
1038 node *n;
|
|
|
1039 {
|
|
|
1040 REQ(n, expr_stmt); /* exprlist ('=' exprlist)* NEWLINE */
|
|
|
1041 com_node(c, CHILD(n, NCH(n)-2));
|
|
|
1042 if (NCH(n) == 2) {
|
|
|
1043 com_addbyte(c, PRINT_EXPR);
|
|
|
1044 }
|
|
|
1045 else {
|
|
|
1046 int i;
|
|
|
1047 for (i = 0; i < NCH(n)-3; i+=2) {
|
|
|
1048 if (i+2 < NCH(n)-3)
|
|
|
1049 com_addbyte(c, DUP_TOP);
|
|
|
1050 com_assign(c, CHILD(n, i), 1/*assign*/);
|
|
|
1051 }
|
|
|
1052 }
|
|
|
1053 }
|
|
|
1054
|
|
|
1055 static void
|
|
|
1056 com_print_stmt(c, n)
|
|
|
1057 struct compiling *c;
|
|
|
1058 node *n;
|
|
|
1059 {
|
|
|
1060 int i;
|
|
|
1061 REQ(n, print_stmt); /* 'print' (test ',')* [test] NEWLINE */
|
|
|
1062 for (i = 1; i+1 < NCH(n); i += 2) {
|
|
|
1063 com_node(c, CHILD(n, i));
|
|
|
1064 com_addbyte(c, PRINT_ITEM);
|
|
|
1065 }
|
|
|
1066 if (TYPE(CHILD(n, NCH(n)-2)) != COMMA)
|
|
|
1067 com_addbyte(c, PRINT_NEWLINE);
|
|
|
1068 }
|
|
|
1069
|
|
|
1070 static void
|
|
|
1071 com_return_stmt(c, n)
|
|
|
1072 struct compiling *c;
|
|
|
1073 node *n;
|
|
|
1074 {
|
|
|
1075 REQ(n, return_stmt); /* 'return' [testlist] NEWLINE */
|
|
|
1076 if (NCH(n) == 2)
|
|
|
1077 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
|
|
|
1078 else
|
|
|
1079 com_node(c, CHILD(n, 1));
|
|
|
1080 com_addbyte(c, RETURN_VALUE);
|
|
|
1081 }
|
|
|
1082
|
|
|
1083 static void
|
|
|
1084 com_raise_stmt(c, n)
|
|
|
1085 struct compiling *c;
|
|
|
1086 node *n;
|
|
|
1087 {
|
|
|
1088 REQ(n, raise_stmt); /* 'raise' expr [',' expr] NEWLINE */
|
|
|
1089 com_node(c, CHILD(n, 1));
|
|
|
1090 if (NCH(n) > 3)
|
|
|
1091 com_node(c, CHILD(n, 3));
|
|
|
1092 else
|
|
|
1093 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
|
|
|
1094 com_addbyte(c, RAISE_EXCEPTION);
|
|
|
1095 }
|
|
|
1096
|
|
|
1097 static void
|
|
|
1098 com_import_stmt(c, n)
|
|
|
1099 struct compiling *c;
|
|
|
1100 node *n;
|
|
|
1101 {
|
|
|
1102 int i;
|
|
|
1103 REQ(n, import_stmt);
|
|
|
1104 /* 'import' NAME (',' NAME)* NEWLINE |
|
|
|
1105 'from' NAME 'import' ('*' | NAME (',' NAME)*) NEWLINE */
|
|
|
1106 if (STR(CHILD(n, 0))[0] == 'f') {
|
|
|
1107 /* 'from' NAME 'import' ... */
|
|
|
1108 REQ(CHILD(n, 1), NAME);
|
|
|
1109 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
|
|
|
1110 for (i = 3; i < NCH(n); i += 2)
|
|
|
1111 com_addopname(c, IMPORT_FROM, CHILD(n, i));
|
|
|
1112 com_addbyte(c, POP_TOP);
|
|
|
1113 }
|
|
|
1114 else {
|
|
|
1115 for (i = 1; i < NCH(n); i += 2) {
|
|
|
1116 com_addopname(c, IMPORT_NAME, CHILD(n, i));
|
|
|
1117 com_addopname(c, STORE_NAME, CHILD(n, i));
|
|
|
1118 }
|
|
|
1119 }
|
|
|
1120 }
|
|
|
1121
|
|
|
1122 static void
|
|
|
1123 com_if_stmt(c, n)
|
|
|
1124 struct compiling *c;
|
|
|
1125 node *n;
|
|
|
1126 {
|
|
|
1127 int i;
|
|
|
1128 int anchor = 0;
|
|
|
1129 REQ(n, if_stmt);
|
|
|
1130 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
|
|
|
1131 for (i = 0; i+3 < NCH(n); i+=4) {
|
|
|
1132 int a = 0;
|
|
|
1133 com_node(c, CHILD(n, i+1));
|
|
|
1134 com_addfwref(c, JUMP_IF_FALSE, &a);
|
|
|
1135 com_addbyte(c, POP_TOP);
|
|
|
1136 com_node(c, CHILD(n, i+3));
|
|
|
1137 com_addfwref(c, JUMP_FORWARD, &anchor);
|
|
|
1138 com_backpatch(c, a);
|
|
|
1139 com_addbyte(c, POP_TOP);
|
|
|
1140 }
|
|
|
1141 if (i+2 < NCH(n))
|
|
|
1142 com_node(c, CHILD(n, i+2));
|
|
|
1143 com_backpatch(c, anchor);
|
|
|
1144 }
|
|
|
1145
|
|
|
1146 static void
|
|
|
1147 com_while_stmt(c, n)
|
|
|
1148 struct compiling *c;
|
|
|
1149 node *n;
|
|
|
1150 {
|
|
|
1151 int break_anchor = 0;
|
|
|
1152 int anchor = 0;
|
|
|
1153 int begin;
|
|
|
1154 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
|
|
|
1155 com_addfwref(c, SETUP_LOOP, &break_anchor);
|
|
|
1156 begin = c->c_nexti;
|
|
|
1157 com_node(c, CHILD(n, 1));
|
|
|
1158 com_addfwref(c, JUMP_IF_FALSE, &anchor);
|
|
|
1159 com_addbyte(c, POP_TOP);
|
|
|
1160 com_node(c, CHILD(n, 3));
|
|
|
1161 com_addoparg(c, JUMP_ABSOLUTE, begin);
|
|
|
1162 com_backpatch(c, anchor);
|
|
|
1163 com_addbyte(c, POP_TOP);
|
|
|
1164 com_addbyte(c, POP_BLOCK);
|
|
|
1165 if (NCH(n) > 4)
|
|
|
1166 com_node(c, CHILD(n, 6));
|
|
|
1167 com_backpatch(c, break_anchor);
|
|
|
1168 }
|
|
|
1169
|
|
|
1170 static void
|
|
|
1171 com_for_stmt(c, n)
|
|
|
1172 struct compiling *c;
|
|
|
1173 node *n;
|
|
|
1174 {
|
|
|
1175 object *v;
|
|
|
1176 int break_anchor = 0;
|
|
|
1177 int anchor = 0;
|
|
|
1178 int begin;
|
|
|
1179 REQ(n, for_stmt);
|
|
|
1180 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
|
|
|
1181 com_addfwref(c, SETUP_LOOP, &break_anchor);
|
|
|
1182 com_node(c, CHILD(n, 3));
|
|
|
1183 v = newintobject(0L);
|
|
|
1184 if (v == NULL)
|
|
|
1185 c->c_errors++;
|
|
|
1186 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
|
|
|
1187 XDECREF(v);
|
|
|
1188 begin = c->c_nexti;
|
|
|
1189 com_addfwref(c, FOR_LOOP, &anchor);
|
|
|
1190 com_assign(c, CHILD(n, 1), 1/*assigning*/);
|
|
|
1191 com_node(c, CHILD(n, 5));
|
|
|
1192 com_addoparg(c, JUMP_ABSOLUTE, begin);
|
|
|
1193 com_backpatch(c, anchor);
|
|
|
1194 com_addbyte(c, POP_BLOCK);
|
|
|
1195 if (NCH(n) > 8)
|
|
|
1196 com_node(c, CHILD(n, 8));
|
|
|
1197 com_backpatch(c, break_anchor);
|
|
|
1198 }
|
|
|
1199
|
|
|
1200 static void
|
|
|
1201 com_try_stmt(c, n)
|
|
|
1202 struct compiling *c;
|
|
|
1203 node *n;
|
|
|
1204 {
|
|
|
1205 int finally_anchor = 0;
|
|
|
1206 int except_anchor = 0;
|
|
|
1207 REQ(n, try_stmt);
|
|
|
1208 /* 'try' ':' suite (except_clause ':' suite)* ['finally' ':' suite] */
|
|
|
1209 if (NCH(n) > 3 && TYPE(CHILD(n, NCH(n)-3)) != except_clause) {
|
|
|
1210 /* Have a 'finally' clause */
|
|
|
1211 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
|
|
|
1212 }
|
|
|
1213 if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == except_clause) {
|
|
|
1214 /* Have an 'except' clause */
|
|
|
1215 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
|
|
|
1216 }
|
|
|
1217 com_node(c, CHILD(n, 2));
|
|
|
1218 if (except_anchor) {
|
|
|
1219 int end_anchor = 0;
|
|
|
1220 int i;
|
|
|
1221 node *ch;
|
|
|
1222 com_addbyte(c, POP_BLOCK);
|
|
|
1223 com_addfwref(c, JUMP_FORWARD, &end_anchor);
|
|
|
1224 com_backpatch(c, except_anchor);
|
|
|
1225 for (i = 3;
|
|
|
1226 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
|
|
|
1227 i += 3) {
|
|
|
1228 /* except_clause: 'except' [expr [',' expr]] */
|
|
|
1229 int next_anchor = 0;
|
|
|
1230 if (NCH(ch) > 1) {
|
|
|
1231 com_addbyte(c, DUP_TOP);
|
|
|
1232 com_node(c, CHILD(ch, 1));
|
|
|
1233 com_addoparg(c, COMPARE_OP, EXC_MATCH);
|
|
|
1234 com_addfwref(c, JUMP_IF_FALSE, &next_anchor);
|
|
|
1235 com_addbyte(c, POP_TOP);
|
|
|
1236 }
|
|
|
1237 com_addbyte(c, POP_TOP);
|
|
|
1238 if (NCH(ch) > 3)
|
|
|
1239 com_assign(c, CHILD(ch, 3), 1/*assigning*/);
|
|
|
1240 else
|
|
|
1241 com_addbyte(c, POP_TOP);
|
|
|
1242 com_node(c, CHILD(n, i+2));
|
|
|
1243 com_addfwref(c, JUMP_FORWARD, &end_anchor);
|
|
|
1244 if (next_anchor)
|
|
|
1245 com_backpatch(c, next_anchor);
|
|
|
1246 }
|
|
|
1247 com_addbyte(c, END_FINALLY);
|
|
|
1248 com_backpatch(c, end_anchor);
|
|
|
1249 }
|
|
|
1250 if (finally_anchor) {
|
|
|
1251 com_addbyte(c, POP_BLOCK);
|
|
|
1252 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
|
|
|
1253 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
|
|
|
1254 com_backpatch(c, finally_anchor);
|
|
|
1255 com_node(c, CHILD(n, NCH(n)-1));
|
|
|
1256 com_addbyte(c, END_FINALLY);
|
|
|
1257 }
|
|
|
1258 }
|
|
|
1259
|
|
|
1260 static void
|
|
|
1261 com_suite(c, n)
|
|
|
1262 struct compiling *c;
|
|
|
1263 node *n;
|
|
|
1264 {
|
|
|
1265 REQ(n, suite);
|
|
|
1266 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
|
|
|
1267 if (NCH(n) == 1) {
|
|
|
1268 com_node(c, CHILD(n, 0));
|
|
|
1269 }
|
|
|
1270 else {
|
|
|
1271 int i;
|
|
|
1272 for (i = 0; i < NCH(n); i++) {
|
|
|
1273 node *ch = CHILD(n, i);
|
|
|
1274 if (TYPE(ch) == stmt)
|
|
|
1275 com_node(c, ch);
|
|
|
1276 }
|
|
|
1277 }
|
|
|
1278 }
|
|
|
1279
|
|
|
1280 static void
|
|
|
1281 com_funcdef(c, n)
|
|
|
1282 struct compiling *c;
|
|
|
1283 node *n;
|
|
|
1284 {
|
|
|
1285 object *v;
|
|
|
1286 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
|
|
|
1287 v = (object *)compile(n);
|
|
|
1288 if (v == NULL)
|
|
|
1289 c->c_errors++;
|
|
|
1290 else {
|
|
|
1291 int i = com_addconst(c, v);
|
|
|
1292 com_addoparg(c, LOAD_CONST, i);
|
|
|
1293 com_addbyte(c, BUILD_FUNCTION);
|
|
|
1294 com_addopname(c, STORE_NAME, CHILD(n, 1));
|
|
|
1295 DECREF(v);
|
|
|
1296 }
|
|
|
1297 }
|
|
|
1298
|
|
|
1299 static void
|
|
|
1300 com_classdef(c, n)
|
|
|
1301 struct compiling *c;
|
|
|
1302 node *n;
|
|
|
1303 {
|
|
|
1304 REQ(n, classdef);
|
|
|
1305 /*
|
|
|
1306 classdef: 'class' NAME parameters ['=' baselist] ':' suite
|
|
|
1307 baselist: atom arguments (',' atom arguments)*
|
|
|
1308 arguments: '(' [testlist] ')'
|
|
|
1309 */
|
|
|
1310 err_setstr(SystemError, "can't compile 'class' yet");
|
|
|
1311 c->c_errors++;
|
|
|
1312 }
|
|
|
1313
|
|
|
1314 static void
|
|
|
1315 com_node(c, n)
|
|
|
1316 struct compiling *c;
|
|
|
1317 node *n;
|
|
|
1318 {
|
|
|
1319 switch (TYPE(n)) {
|
|
|
1320
|
|
|
1321 /* Definition nodes */
|
|
|
1322
|
|
|
1323 case funcdef:
|
|
|
1324 com_funcdef(c, n);
|
|
|
1325 break;
|
|
|
1326 case classdef:
|
|
|
1327 com_classdef(c, n);
|
|
|
1328 break;
|
|
|
1329
|
|
|
1330 /* Trivial parse tree nodes */
|
|
|
1331
|
|
|
1332 case stmt:
|
|
|
1333 case simple_stmt:
|
|
|
1334 case flow_stmt:
|
|
|
1335 case compound_stmt:
|
|
|
1336 com_node(c, CHILD(n, 0));
|
|
|
1337 break;
|
|
|
1338
|
|
|
1339 /* Statement nodes */
|
|
|
1340
|
|
|
1341 case expr_stmt:
|
|
|
1342 com_expr_stmt(c, n);
|
|
|
1343 break;
|
|
|
1344 case print_stmt:
|
|
|
1345 com_print_stmt(c, n);
|
|
|
1346 break;
|
|
|
1347 case del_stmt: /* 'del' exprlist NEWLINE */
|
|
|
1348 com_assign(c, CHILD(n, 1), 0/*delete*/);
|
|
|
1349 break;
|
|
|
1350 case pass_stmt:
|
|
|
1351 break;
|
|
|
1352 case break_stmt:
|
|
|
1353 com_addbyte(c, BREAK_LOOP);
|
|
|
1354 break;
|
|
|
1355 case return_stmt:
|
|
|
1356 com_return_stmt(c, n);
|
|
|
1357 break;
|
|
|
1358 case raise_stmt:
|
|
|
1359 com_raise_stmt(c, n);
|
|
|
1360 break;
|
|
|
1361 case import_stmt:
|
|
|
1362 com_import_stmt(c, n);
|
|
|
1363 break;
|
|
|
1364 case if_stmt:
|
|
|
1365 com_if_stmt(c, n);
|
|
|
1366 break;
|
|
|
1367 case while_stmt:
|
|
|
1368 com_while_stmt(c, n);
|
|
|
1369 break;
|
|
|
1370 case for_stmt:
|
|
|
1371 com_for_stmt(c, n);
|
|
|
1372 break;
|
|
|
1373 case try_stmt:
|
|
|
1374 com_try_stmt(c, n);
|
|
|
1375 break;
|
|
|
1376 case suite:
|
|
|
1377 com_suite(c, n);
|
|
|
1378 break;
|
|
|
1379
|
|
|
1380 /* Expression nodes */
|
|
|
1381
|
|
|
1382 case testlist:
|
|
|
1383 com_list(c, n);
|
|
|
1384 break;
|
|
|
1385 case test:
|
|
|
1386 com_test(c, n);
|
|
|
1387 break;
|
|
|
1388 case and_test:
|
|
|
1389 com_and_test(c, n);
|
|
|
1390 break;
|
|
|
1391 case not_test:
|
|
|
1392 com_not_test(c, n);
|
|
|
1393 break;
|
|
|
1394 case comparison:
|
|
|
1395 com_comparison(c, n);
|
|
|
1396 break;
|
|
|
1397 case exprlist:
|
|
|
1398 com_list(c, n);
|
|
|
1399 break;
|
|
|
1400 case expr:
|
|
|
1401 com_expr(c, n);
|
|
|
1402 break;
|
|
|
1403 case term:
|
|
|
1404 com_term(c, n);
|
|
|
1405 break;
|
|
|
1406 case factor:
|
|
|
1407 com_factor(c, n);
|
|
|
1408 break;
|
|
|
1409 case atom:
|
|
|
1410 com_atom(c, n);
|
|
|
1411 break;
|
|
|
1412
|
|
|
1413 default:
|
|
|
1414 fprintf(stderr, "node type %d\n", TYPE(n));
|
|
|
1415 err_setstr(SystemError, "com_node: unexpected node type");
|
|
|
1416 c->c_errors++;
|
|
|
1417 }
|
|
|
1418 }
|
|
|
1419
|
|
|
1420 static void com_fplist PROTO((struct compiling *, node *));
|
|
|
1421
|
|
|
1422 static void
|
|
|
1423 com_fpdef(c, n)
|
|
|
1424 struct compiling *c;
|
|
|
1425 node *n;
|
|
|
1426 {
|
|
|
1427 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
|
|
|
1428 if (TYPE(CHILD(n, 0)) == LPAR)
|
|
|
1429 com_fplist(c, CHILD(n, 1));
|
|
|
1430 else
|
|
|
1431 com_addopname(c, STORE_NAME, CHILD(n, 0));
|
|
|
1432 }
|
|
|
1433
|
|
|
1434 static void
|
|
|
1435 com_fplist(c, n)
|
|
|
1436 struct compiling *c;
|
|
|
1437 node *n;
|
|
|
1438 {
|
|
|
1439 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* */
|
|
|
1440 if (NCH(n) == 1) {
|
|
|
1441 com_fpdef(c, CHILD(n, 0));
|
|
|
1442 }
|
|
|
1443 else {
|
|
|
1444 int i;
|
|
|
1445 com_addoparg(c, UNPACK_TUPLE, (NCH(n)+1)/2);
|
|
|
1446 for (i = 0; i < NCH(n); i += 2)
|
|
|
1447 com_fpdef(c, CHILD(n, i));
|
|
|
1448 }
|
|
|
1449 }
|
|
|
1450
|
|
|
1451 static void
|
|
|
1452 com_file_input(c, n)
|
|
|
1453 struct compiling *c;
|
|
|
1454 node *n;
|
|
|
1455 {
|
|
|
1456 int i;
|
|
|
1457 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
|
|
|
1458 for (i = 0; i < NCH(n); i++) {
|
|
|
1459 node *ch = CHILD(n, i);
|
|
|
1460 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
|
|
|
1461 com_node(c, ch);
|
|
|
1462 }
|
|
|
1463 }
|
|
|
1464
|
|
|
1465 /* Top-level compile-node interface */
|
|
|
1466
|
|
|
1467 static void
|
|
|
1468 compile_funcdef(c, n)
|
|
|
1469 struct compiling *c;
|
|
|
1470 node *n;
|
|
|
1471 {
|
|
|
1472 node *ch;
|
|
|
1473 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
|
|
|
1474 ch = CHILD(n, 2); /* parameters: '(' [fplist] ')' */
|
|
|
1475 ch = CHILD(ch, 1); /* ')' | fplist */
|
|
|
1476 if (TYPE(ch) == RPAR)
|
|
|
1477 com_addbyte(c, REFUSE_ARGS);
|
|
|
1478 else {
|
|
|
1479 com_addbyte(c, REQUIRE_ARGS);
|
|
|
1480 com_fplist(c, ch);
|
|
|
1481 }
|
|
|
1482 com_node(c, CHILD(n, 4));
|
|
|
1483 com_addoparg(c, LOAD_CONST, com_addconst(c, None));
|
|
|
1484 com_addbyte(c, RETURN_VALUE);
|
|
|
1485 }
|
|
|
1486
|
|
|
1487 static void
|
|
|
1488 compile_node(c, n)
|
|
|
1489 struct compiling *c;
|
|
|
1490 node *n;
|
|
|
1491 {
|
|
|
1492 switch (TYPE(n)) {
|
|
|
1493
|
|
|
1494 case single_input:
|
|
|
1495 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
|
|
|
1496 n = CHILD(n, 0);
|
|
|
1497 if (TYPE(n) != NEWLINE)
|
|
|
1498 com_node(c, n);
|
|
|
1499 break;
|
|
|
1500
|
|
|
1501 case file_input:
|
|
|
1502 com_file_input(c, n);
|
|
|
1503 break;
|
|
|
1504
|
|
|
1505 case expr_input:
|
|
|
1506 case eval_input:
|
|
|
1507 com_node(c, CHILD(n, 0));
|
|
|
1508 break;
|
|
|
1509
|
|
|
1510 case funcdef:
|
|
|
1511 compile_funcdef(c, n);
|
|
|
1512 break;
|
|
|
1513
|
|
|
1514 default:
|
|
|
1515 fprintf(stderr, "node type %d\n", TYPE(n));
|
|
|
1516 err_setstr(SystemError, "compile_node: unexpected node type");
|
|
|
1517 c->c_errors++;
|
|
|
1518 }
|
|
|
1519 }
|
|
|
1520
|
|
|
1521 codeobject *
|
|
|
1522 compile(n)
|
|
|
1523 node *n;
|
|
|
1524 {
|
|
|
1525 struct compiling sc;
|
|
|
1526 codeobject *co;
|
|
|
1527 if (!com_init(&sc))
|
|
|
1528 return NULL;
|
|
|
1529 compile_node(&sc, n);
|
|
|
1530 com_done(&sc);
|
|
|
1531 if (sc.c_errors == 0)
|
|
|
1532 co = newcodeobject(sc.c_code, sc.c_consts, sc.c_names);
|
|
|
1533 else
|
|
|
1534 co = NULL;
|
|
|
1535 com_free(&sc);
|
|
|
1536 return co;
|
|
|
1537 }
|