blob: 2d2f800d4235ec231a4517366b967bdf44053a01 [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <unistd.h>
2#include <stdlib.h>
3#include <stdint.h>
4#include <stdio.h>
5#include <string.h>
6#include <assert.h>
7
8#include "misc.h"
9#include "lexer.h"
10#include "machine.h"
11#include "parse.h"
12#include "scope.h"
13#include "compile.h"
14#include "runtime.h"
15#include "emit.h"
16
17// TODO need to mangle __attr names
Damien415eb6f2013-10-05 12:19:06 +010018// TODO add #define to enable/disable CPython compatibility
Damien429d7192013-10-04 19:53:11 +010019
20typedef enum {
21 PN_none = 0,
22#define DEF_RULE(rule, comp, kind, arg...) PN_##rule,
23#include "grammar.h"
24#undef DEF_RULE
25 PN_maximum_number_of,
26} pn_kind_t;
27
Damien415eb6f2013-10-05 12:19:06 +010028#define EMIT(fun, arg...) (comp->emit_method_table->fun(comp->emit, ##arg))
Damien429d7192013-10-04 19:53:11 +010029
30typedef struct _compiler_t {
31 qstr qstr___class__;
32 qstr qstr___locals__;
33 qstr qstr___name__;
34 qstr qstr___module__;
35 qstr qstr___qualname__;
36 qstr qstr___doc__;
37 qstr qstr_assertion_error;
38
39 pass_kind_t pass;
40
Damienb05d7072013-10-05 13:37:10 +010041 int next_label;
42 int max_num_labels;
43
Damien429d7192013-10-04 19:53:11 +010044 int break_label;
45 int continue_label;
46 int except_nest_level;
47
48 int n_arg_keyword;
49 bool have_star_arg;
50 bool have_dbl_star_arg;
51 bool have_bare_star;
52 int param_pass;
53 int param_pass_num_dict_params;
54 int param_pass_num_default_params;
55
56 scope_t *scope_head;
57 scope_t *scope_cur;
58
Damien415eb6f2013-10-05 12:19:06 +010059 emit_t *emit;
60 const emit_method_table_t *emit_method_table;
Damien429d7192013-10-04 19:53:11 +010061} compiler_t;
62
63py_parse_node_t fold_constants(py_parse_node_t pn) {
64 if (PY_PARSE_NODE_IS_STRUCT(pn)) {
65 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
66 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
67
68 // fold arguments first
69 for (int i = 0; i < n; i++) {
70 pns->nodes[i] = fold_constants(pns->nodes[i]);
71 }
72
73 switch (PY_PARSE_NODE_STRUCT_KIND(pns)) {
74 case PN_shift_expr:
75 if (n == 3 && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
76 int arg0 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
77 int arg1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[2]);
78 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_DBL_LESS)) {
79 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 << arg1); // XXX can overflow; enabled only to compare with CPython
80 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_DBL_MORE)) {
81 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 >> arg1);
82 } else {
83 // shouldn't happen
84 assert(0);
85 }
86 }
87 break;
88
89 case PN_arith_expr:
90 // XXX can overflow; enabled only to compare with CPython
91 if (n == 3 && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
92 int arg0 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
93 int arg1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[2]);
94 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_PLUS)) {
95 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 + arg1);
96 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_MINUS)) {
97 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 - arg1);
98 } else {
99 // shouldn't happen
100 assert(0);
101 }
102 }
103 break;
104
105 case PN_term:
106 // XXX can overflow; enabled only to compare with CPython
107 if (n == 3 && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
108 int arg0 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
109 int arg1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[2]);
110 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_STAR)) {
111 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 * arg1);
112 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_SLASH)) {
113 ; // pass
114 //} else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_)) {
115 //pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 - arg1);
116 } else {
117 // shouldn't happen
118 assert(0);
119 }
120 }
121 break;
122
123 case PN_factor_2:
124 if (PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
125 machine_int_t arg = PY_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
126 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_PLUS)) {
127 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg);
128 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_MINUS)) {
129 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, -arg);
130 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_TILDE)) {
131 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, ~arg);
132 } else {
133 // shouldn't happen
134 assert(0);
135 }
136 }
137 break;
138
139 case PN_power:
140 // XXX can overflow; enabled only to compare with CPython
141 if (PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && PY_PARSE_NODE_IS_NULL(pns->nodes[1]) && !PY_PARSE_NODE_IS_NULL(pns->nodes[2])) {
142 py_parse_node_struct_t* pns2 = (py_parse_node_struct_t*)pns->nodes[2];
143 if (PY_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
144 int power = PY_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
145 if (power >= 0) {
146 int ans = 1;
147 int base = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
148 for (; power > 0; power--) {
149 ans *= base;
150 }
151 pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, ans);
152 }
153 }
154 }
155 break;
156 }
157 }
158
159 return pn;
160}
161
162void compile_node(compiler_t *comp, py_parse_node_t pn);
163
Damienb05d7072013-10-05 13:37:10 +0100164static int comp_next_label(compiler_t *comp) {
165 return comp->next_label++;
166}
167
168static scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, py_parse_node_t pn) {
169 scope_t *scope = scope_new(kind, pn, rt_get_new_unique_code_id());
Damien429d7192013-10-04 19:53:11 +0100170 scope->parent = comp->scope_cur;
171 scope->next = NULL;
172 if (comp->scope_head == NULL) {
173 comp->scope_head = scope;
174 } else {
175 scope_t *s = comp->scope_head;
176 while (s->next != NULL) {
177 s = s->next;
178 }
179 s->next = scope;
180 }
181 return scope;
182}
183
Damienb05d7072013-10-05 13:37:10 +0100184static int list_len(py_parse_node_t pn, int pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100185 if (PY_PARSE_NODE_IS_NULL(pn)) {
186 return 0;
187 } else if (PY_PARSE_NODE_IS_LEAF(pn)) {
188 return 1;
189 } else {
190 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
191 if (PY_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
192 return 1;
193 } else {
194 return PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
195 }
196 }
197}
198
Damienb05d7072013-10-05 13:37:10 +0100199static void apply_to_single_or_list(compiler_t *comp, py_parse_node_t pn, int pn_list_kind, void (*f)(compiler_t*, py_parse_node_t)) {
Damien429d7192013-10-04 19:53:11 +0100200 if (PY_PARSE_NODE_IS_STRUCT(pn) && PY_PARSE_NODE_STRUCT_KIND((py_parse_node_struct_t*)pn) == pn_list_kind) {
201 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
202 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
203 for (int i = 0; i < num_nodes; i++) {
204 f(comp, pns->nodes[i]);
205 }
206 } else if (!PY_PARSE_NODE_IS_NULL(pn)) {
207 f(comp, pn);
208 }
209}
210
Damienb05d7072013-10-05 13:37:10 +0100211static int list_get(py_parse_node_t *pn, int pn_kind, py_parse_node_t **nodes) {
Damien429d7192013-10-04 19:53:11 +0100212 if (PY_PARSE_NODE_IS_NULL(*pn)) {
213 *nodes = NULL;
214 return 0;
215 } else if (PY_PARSE_NODE_IS_LEAF(*pn)) {
216 *nodes = pn;
217 return 1;
218 } else {
219 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)(*pn);
220 if (PY_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
221 *nodes = pn;
222 return 1;
223 } else {
224 *nodes = pns->nodes;
225 return PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
226 }
227 }
228}
229
230void compile_do_nothing(compiler_t *comp, py_parse_node_struct_t *pns) {
231}
232
233void compile_generic_all_nodes(compiler_t *comp, py_parse_node_struct_t *pns) {
234 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
235 for (int i = 0; i < num_nodes; i++) {
236 compile_node(comp, pns->nodes[i]);
237 }
238}
239
240bool c_tuple_is_const(py_parse_node_t pn) {
241 if (!PY_PARSE_NODE_IS_LEAF(pn)) {
242 return false;
243 }
244 if (PY_PARSE_NODE_IS_ID(pn)) {
245 return false;
246 }
247 return true;
248}
249
250void c_tuple_emit_const(compiler_t *comp, py_parse_node_t pn) {
251 assert(PY_PARSE_NODE_IS_LEAF(pn));
252 int arg = PY_PARSE_NODE_LEAF_ARG(pn);
253 switch (PY_PARSE_NODE_LEAF_KIND(pn)) {
254 case PY_PARSE_NODE_ID: assert(0);
255 case PY_PARSE_NODE_SMALL_INT: EMIT(load_const_verbatim_int, arg); break;
256 case PY_PARSE_NODE_INTEGER: EMIT(load_const_verbatim_str, qstr_str(arg)); break;
257 case PY_PARSE_NODE_DECIMAL: EMIT(load_const_verbatim_str, qstr_str(arg)); break;
258 case PY_PARSE_NODE_STRING: EMIT(load_const_verbatim_quoted_str, arg, false); break;
259 case PY_PARSE_NODE_BYTES: EMIT(load_const_verbatim_quoted_str, arg, true); break;
260 case PY_PARSE_NODE_TOKEN:
261 switch (arg) {
262 case PY_TOKEN_KW_FALSE: EMIT(load_const_verbatim_str, "False"); break;
263 case PY_TOKEN_KW_NONE: EMIT(load_const_verbatim_str, "None"); break;
264 case PY_TOKEN_KW_TRUE: EMIT(load_const_verbatim_str, "True"); break;
265 default: assert(0);
266 }
267 break;
268 default: assert(0);
269 }
270}
271
272// funnelling all tuple creations through this function and all this constant stuff is purely to agree with CPython
273void c_tuple(compiler_t *comp, py_parse_node_t pn, py_parse_node_struct_t *pns_list) {
274 int n = 0;
275 if (pns_list != NULL) {
276 n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
277 }
278 int total = n;
279 bool is_const = true;
280 if (!PY_PARSE_NODE_IS_NULL(pn)) {
281 total += 1;
282 if (!c_tuple_is_const(pn)) {
283 is_const = false;
284 }
285 }
286 for (int i = 0; i < n; i++) {
287 if (!c_tuple_is_const(pns_list->nodes[i])) {
288 is_const = false;
289 break;
290 }
291 }
292 if (total > 0 && is_const) {
293 bool need_comma = false;
294 EMIT(load_const_verbatim_start);
295 EMIT(load_const_verbatim_str, "(");
296 if (!PY_PARSE_NODE_IS_NULL(pn)) {
297 c_tuple_emit_const(comp, pn);
298 need_comma = true;
299 }
300 for (int i = 0; i < n; i++) {
301 if (need_comma) {
302 EMIT(load_const_verbatim_str, ", ");
303 }
304 c_tuple_emit_const(comp, pns_list->nodes[i]);
305 need_comma = true;
306 }
307 if (total == 1) {
308 EMIT(load_const_verbatim_str, ",)");
309 } else {
310 EMIT(load_const_verbatim_str, ")");
311 }
312 EMIT(load_const_verbatim_end);
313 } else {
314 if (!PY_PARSE_NODE_IS_NULL(pn)) {
315 compile_node(comp, pn);
316 }
317 for (int i = 0; i < n; i++) {
318 compile_node(comp, pns_list->nodes[i]);
319 }
320 EMIT(build_tuple, total);
321 }
322}
323
324void compile_generic_tuple(compiler_t *comp, py_parse_node_struct_t *pns) {
325 // a simple tuple expression
326 /*
327 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
328 for (int i = 0; i < n; i++) {
329 compile_node(comp, pns->nodes[i]);
330 }
331 EMIT(build_tuple, n);
332 */
333 c_tuple(comp, PY_PARSE_NODE_NULL, pns);
334}
335
336bool node_is_const_false(py_parse_node_t pn) {
337 return PY_PARSE_NODE_IS_TOKEN_KIND(pn, PY_TOKEN_KW_FALSE);
338 // untested: || (PY_PARSE_NODE_IS_SMALL_INT(pn) && PY_PARSE_NODE_LEAF_ARG(pn) == 1);
339}
340
341bool node_is_const_true(py_parse_node_t pn) {
342 return PY_PARSE_NODE_IS_TOKEN_KIND(pn, PY_TOKEN_KW_TRUE) || (PY_PARSE_NODE_IS_SMALL_INT(pn) && PY_PARSE_NODE_LEAF_ARG(pn) == 1);
343}
344
345// having c_if_cond_2 and the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
346void c_if_cond_2(compiler_t *comp, py_parse_node_t pn, bool jump_if, int label, bool is_nested) {
347 if (node_is_const_false(pn)) {
348 if (jump_if == false) {
349 EMIT(jump, label);
350 }
351 return;
352 } else if (node_is_const_true(pn)) {
353 if (jump_if == true) {
354 EMIT(jump, label);
355 }
356 return;
357 } else if (PY_PARSE_NODE_IS_STRUCT(pn)) {
358 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
359 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
360 if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
361 if (jump_if == false) {
Damienb05d7072013-10-05 13:37:10 +0100362 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100363 for (int i = 0; i < n - 1; i++) {
364 c_if_cond_2(comp, pns->nodes[i], true, label2, true);
365 }
366 c_if_cond_2(comp, pns->nodes[n - 1], false, label, true);
367 EMIT(label_assign, label2);
368 } else {
369 for (int i = 0; i < n; i++) {
370 c_if_cond_2(comp, pns->nodes[i], true, label, true);
371 }
372 }
373 return;
374 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
375 if (jump_if == false) {
376 for (int i = 0; i < n; i++) {
377 c_if_cond_2(comp, pns->nodes[i], false, label, true);
378 }
379 } else {
Damienb05d7072013-10-05 13:37:10 +0100380 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100381 for (int i = 0; i < n - 1; i++) {
382 c_if_cond_2(comp, pns->nodes[i], false, label2, true);
383 }
384 c_if_cond_2(comp, pns->nodes[n - 1], true, label, true);
385 EMIT(label_assign, label2);
386 }
387 return;
388 } else if (!is_nested && PY_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
389 c_if_cond_2(comp, pns->nodes[0], !jump_if, label, true);
390 return;
391 }
392 }
393
394 // nothing special, fall back to default compiling for node and jump
395 compile_node(comp, pn);
396 if (jump_if == false) {
397 EMIT(pop_jump_if_false, label);
398 } else {
399 EMIT(pop_jump_if_true, label);
400 }
401}
402
403void c_if_cond(compiler_t *comp, py_parse_node_t pn, bool jump_if, int label) {
404 c_if_cond_2(comp, pn, jump_if, label, false);
405}
406
407typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
408void c_assign(compiler_t *comp, py_parse_node_t pn, assign_kind_t kind);
409
410void c_assign_power(compiler_t *comp, py_parse_node_struct_t *pns, assign_kind_t assign_kind) {
411 if (assign_kind != ASSIGN_AUG_STORE) {
412 compile_node(comp, pns->nodes[0]);
413 }
414
415 if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
416 py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1];
417 if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
418 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns1);
419 if (assign_kind != ASSIGN_AUG_STORE) {
420 for (int i = 0; i < n - 1; i++) {
421 compile_node(comp, pns1->nodes[i]);
422 }
423 }
424 assert(PY_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
425 pns1 = (py_parse_node_struct_t*)pns1->nodes[n - 1];
426 }
427 if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
428 printf("SyntaxError: can't assign to function call\n");
429 return;
430 } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
431 if (assign_kind == ASSIGN_AUG_STORE) {
432 EMIT(rot_three);
433 EMIT(store_subscr);
434 } else {
435 compile_node(comp, pns1->nodes[0]);
436 if (assign_kind == ASSIGN_AUG_LOAD) {
437 EMIT(dup_top_two);
438 EMIT(binary_op, RT_BINARY_OP_SUBSCR);
439 } else {
440 EMIT(store_subscr);
441 }
442 }
443 } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
444 assert(PY_PARSE_NODE_IS_ID(pns1->nodes[0]));
445 if (assign_kind == ASSIGN_AUG_LOAD) {
446 EMIT(dup_top);
447 EMIT(load_attr, PY_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
448 } else {
449 if (assign_kind == ASSIGN_AUG_STORE) {
450 EMIT(rot_two);
451 }
452 EMIT(store_attr, PY_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
453 }
454 } else {
455 // shouldn't happen
456 assert(0);
457 }
458 } else {
459 // shouldn't happen
460 assert(0);
461 }
462
463 if (!PY_PARSE_NODE_IS_NULL(pns->nodes[2])) {
464 // SyntaxError, cannot assign
465 assert(0);
466 }
467}
468
469void c_assign_tuple(compiler_t *comp, int n, py_parse_node_t *nodes) {
470 assert(n >= 0);
471 int have_star_index = -1;
472 for (int i = 0; i < n; i++) {
473 if (PY_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_star_expr)) {
474 if (have_star_index < 0) {
475 EMIT(unpack_ex, i, n - i - 1);
476 have_star_index = i;
477 } else {
478 printf("SyntaxError: two starred expressions in assignment\n");
479 return;
480 }
481 }
482 }
483 if (have_star_index < 0) {
484 EMIT(unpack_sequence, n);
485 }
486 for (int i = 0; i < n; i++) {
487 if (i == have_star_index) {
488 c_assign(comp, ((py_parse_node_struct_t*)nodes[i])->nodes[0], ASSIGN_STORE);
489 } else {
490 c_assign(comp, nodes[i], ASSIGN_STORE);
491 }
492 }
493}
494
495// assigns top of stack to pn
496void c_assign(compiler_t *comp, py_parse_node_t pn, assign_kind_t assign_kind) {
497 tail_recursion:
498 if (PY_PARSE_NODE_IS_NULL(pn)) {
499 assert(0);
500 } else if (PY_PARSE_NODE_IS_LEAF(pn)) {
501 if (PY_PARSE_NODE_IS_ID(pn)) {
502 int arg = PY_PARSE_NODE_LEAF_ARG(pn);
503 switch (assign_kind) {
504 case ASSIGN_STORE:
505 case ASSIGN_AUG_STORE:
Damien4b03e772013-10-05 14:17:09 +0100506 EMIT(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100507 break;
508 case ASSIGN_AUG_LOAD:
Damien4b03e772013-10-05 14:17:09 +0100509 EMIT(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100510 break;
511 }
512 } else {
513 printf("SyntaxError: can't assign to literal\n");
514 return;
515 }
516 } else {
517 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
518 switch (PY_PARSE_NODE_STRUCT_KIND(pns)) {
519 case PN_power:
520 // lhs is an index or attribute
521 c_assign_power(comp, pns, assign_kind);
522 break;
523
524 case PN_testlist_star_expr:
525 case PN_exprlist:
526 // lhs is a tuple
527 if (assign_kind != ASSIGN_STORE) {
528 goto bad_aug;
529 }
530 c_assign_tuple(comp, PY_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
531 break;
532
533 case PN_atom_paren:
534 // lhs is something in parenthesis
535 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
536 // empty tuple
537 printf("SyntaxError: can't assign to ()\n");
538 return;
539 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
540 pns = (py_parse_node_struct_t*)pns->nodes[0];
541 goto testlist_comp;
542 } else {
543 // parenthesis around 1 item, is just that item
544 pn = pns->nodes[0];
545 goto tail_recursion;
546 }
547 break;
548
549 case PN_atom_bracket:
550 // lhs is something in brackets
551 if (assign_kind != ASSIGN_STORE) {
552 goto bad_aug;
553 }
554 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
555 // empty list, assignment allowed
556 c_assign_tuple(comp, 0, NULL);
557 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
558 pns = (py_parse_node_struct_t*)pns->nodes[0];
559 goto testlist_comp;
560 } else {
561 // brackets around 1 item
562 c_assign_tuple(comp, 1, &pns->nodes[0]);
563 }
564 break;
565
566 default:
567 printf("unknown assign, %u\n", (uint)PY_PARSE_NODE_STRUCT_KIND(pns));
568 assert(0);
569 }
570 return;
571
572 testlist_comp:
573 // lhs is a sequence
574 if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
575 py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[1];
576 if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
577 // sequence of one item, with trailing comma
578 assert(PY_PARSE_NODE_IS_NULL(pns2->nodes[0]));
579 c_assign_tuple(comp, 1, &pns->nodes[0]);
580 } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
581 // sequence of many items
582 // TODO call c_assign_tuple instead
583 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns2);
584 EMIT(unpack_sequence, 1 + n);
585 c_assign(comp, pns->nodes[0], ASSIGN_STORE);
586 for (int i = 0; i < n; i++) {
587 c_assign(comp, pns2->nodes[i], ASSIGN_STORE);
588 }
589 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
590 // TODO not implemented
591 assert(0);
592 } else {
593 // sequence with 2 items
594 goto sequence_with_2_items;
595 }
596 } else {
597 // sequence with 2 items
598 sequence_with_2_items:
599 c_assign_tuple(comp, 2, pns->nodes);
600 }
601 return;
602 }
603 return;
604
605 bad_aug:
606 printf("SyntaxError: illegal expression for augmented assignment\n");
607}
608
609// stuff for lambda and comprehensions and generators
610void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_dict_params, int n_default_params) {
611 // make closed over variables, if any
612 int nfree = 0;
613 if (comp->scope_cur->kind != SCOPE_MODULE) {
614 for (int i = 0; i < this_scope->id_info_len; i++) {
615 id_info_t *id_info = &this_scope->id_info[i];
616 if (id_info->kind == ID_INFO_KIND_FREE) {
617 EMIT(load_closure, id_info->qstr);
618 nfree += 1;
619 }
620 }
621 }
622 if (nfree > 0) {
623 EMIT(build_tuple, nfree);
624 }
625
626 // make the function/closure
627 if (nfree == 0) {
628 EMIT(make_function, this_scope, n_dict_params, n_default_params);
629 } else {
630 EMIT(make_closure, this_scope, n_dict_params, n_default_params);
631 }
632}
633
634void compile_funcdef_param(compiler_t *comp, py_parse_node_t pn) {
635 assert(PY_PARSE_NODE_IS_STRUCT(pn));
636 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
637 if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) {
638 if (!PY_PARSE_NODE_IS_NULL(pns->nodes[2])) {
639 // this parameter has a default value
640 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
641 if (comp->have_bare_star) {
642 comp->param_pass_num_dict_params += 1;
643 if (comp->param_pass == 1) {
644 EMIT(load_const_id, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
645 compile_node(comp, pns->nodes[2]);
646 }
647 } else {
648 comp->param_pass_num_default_params += 1;
649 if (comp->param_pass == 2) {
650 compile_node(comp, pns->nodes[2]);
651 }
652 }
653 }
654 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) {
655 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
656 // bare star
657 comp->have_bare_star = true;
658 }
659 }
660}
661
662// leaves function object on stack
663// returns function name
664qstr compile_funcdef_helper(compiler_t *comp, py_parse_node_struct_t *pns) {
665 if (comp->pass == PASS_1) {
666 // create a new scope for this function
667 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (py_parse_node_t)pns);
668 // store the function scope so the compiling function can use it at each pass
669 pns->nodes[4] = (py_parse_node_t)s;
670 }
671
672 // save variables (probably don't need to do this, since we can't have nested definitions..?)
673 bool old_have_bare_star = comp->have_bare_star;
674 int old_param_pass = comp->param_pass;
675 int old_param_pass_num_dict_params = comp->param_pass_num_dict_params;
676 int old_param_pass_num_default_params = comp->param_pass_num_default_params;
677
678 // compile default parameters
679 comp->have_bare_star = false;
680 comp->param_pass = 1; // pass 1 does any default parameters after bare star
681 comp->param_pass_num_dict_params = 0;
682 comp->param_pass_num_default_params = 0;
683 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
684 comp->have_bare_star = false;
685 comp->param_pass = 2; // pass 2 does any default parameters before bare star
686 comp->param_pass_num_dict_params = 0;
687 comp->param_pass_num_default_params = 0;
688 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
689
690 // get the scope for this function
691 scope_t *fscope = (scope_t*)pns->nodes[4];
692
693 // make the function
694 close_over_variables_etc(comp, fscope, comp->param_pass_num_dict_params, comp->param_pass_num_default_params);
695
696 // restore variables
697 comp->have_bare_star = old_have_bare_star;
698 comp->param_pass = old_param_pass;
699 comp->param_pass_num_dict_params = old_param_pass_num_dict_params;
700 comp->param_pass_num_default_params = old_param_pass_num_default_params;
701
702 // return its name (the 'f' in "def f(...):")
703 return fscope->simple_name;
704}
705
706// leaves class object on stack
707// returns class name
708qstr compile_classdef_helper(compiler_t *comp, py_parse_node_struct_t *pns) {
709 if (comp->pass == PASS_1) {
710 // create a new scope for this class
711 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (py_parse_node_t)pns);
712 // store the class scope so the compiling function can use it at each pass
713 pns->nodes[3] = (py_parse_node_t)s;
714 }
715
716 EMIT(load_build_class);
717
718 // scope for this class
719 scope_t *cscope = (scope_t*)pns->nodes[3];
720
721 // compile the class
722 close_over_variables_etc(comp, cscope, 0, 0);
723
724 // get its name
725 EMIT(load_const_id, cscope->simple_name);
726
727 // nodes[1] has parent classes, if any
728 if (PY_PARSE_NODE_IS_NULL(pns->nodes[1])) {
729 // no parent classes
730 EMIT(call_function, 2, 0, false, false);
731 } else {
732 // have a parent class or classes
733 // TODO what if we have, eg, *a or **a in the parent list?
734 compile_node(comp, pns->nodes[1]);
735 EMIT(call_function, 2 + list_len(pns->nodes[1], PN_arglist), 0, false, false);
736 }
737
738 // return its name (the 'C' in class C(...):")
739 return cscope->simple_name;
740}
741
742void compile_decorated(compiler_t *comp, py_parse_node_struct_t *pns) {
743 // get the list of decorators
744 py_parse_node_t *nodes;
745 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
746
747 // load each decorator
748 for (int i = 0; i < n; i++) {
749 assert(PY_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
750 py_parse_node_struct_t *pns_decorator = (py_parse_node_struct_t*)nodes[i];
751 py_parse_node_t *nodes2;
752 int n2 = list_get(&pns_decorator->nodes[0], PN_dotted_name, &nodes2);
753 compile_node(comp, nodes2[0]);
754 for (int i = 1; i < n2; i++) {
755 EMIT(load_attr, PY_PARSE_NODE_LEAF_ARG(nodes2[i]));
756 }
757 if (!PY_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
758 // first call the function with these arguments
759 compile_node(comp, pns_decorator->nodes[1]);
760 }
761 }
762
763 // compile the body (funcdef or classdef) and get its name
764 py_parse_node_struct_t *pns_body = (py_parse_node_struct_t*)pns->nodes[1];
765 qstr body_name = 0;
766 if (PY_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
767 body_name = compile_funcdef_helper(comp, pns_body);
768 } else if (PY_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
769 body_name = compile_classdef_helper(comp, pns_body);
770 } else {
771 // shouldn't happen
772 assert(0);
773 }
774
775 // call each decorator
776 for (int i = 0; i < n; i++) {
777 EMIT(call_function, 1, 0, false, false);
778 }
779
780 // store func/class object into name
Damien4b03e772013-10-05 14:17:09 +0100781 EMIT(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +0100782}
783
784void compile_funcdef(compiler_t *comp, py_parse_node_struct_t *pns) {
785 qstr fname = compile_funcdef_helper(comp, pns);
786 // store function object into function name
Damien4b03e772013-10-05 14:17:09 +0100787 EMIT(store_id, fname);
Damien429d7192013-10-04 19:53:11 +0100788}
789
790void c_del_stmt(compiler_t *comp, py_parse_node_t pn) {
791 if (PY_PARSE_NODE_IS_ID(pn)) {
Damien4b03e772013-10-05 14:17:09 +0100792 EMIT(delete_id, PY_PARSE_NODE_LEAF_ARG(pn));
Damien429d7192013-10-04 19:53:11 +0100793 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
794 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
795
796 compile_node(comp, pns->nodes[0]); // base of the power node
797
798 if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
799 py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1];
800 if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
801 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns1);
802 for (int i = 0; i < n - 1; i++) {
803 compile_node(comp, pns1->nodes[i]);
804 }
805 assert(PY_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
806 pns1 = (py_parse_node_struct_t*)pns1->nodes[n - 1];
807 }
808 if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
809 // SyntaxError: can't delete a function call
810 assert(0);
811 } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
812 compile_node(comp, pns1->nodes[0]);
813 EMIT(delete_subscr);
814 } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
815 assert(PY_PARSE_NODE_IS_ID(pns1->nodes[0]));
816 EMIT(delete_attr, PY_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
817 } else {
818 // shouldn't happen
819 assert(0);
820 }
821 } else {
822 // shouldn't happen
823 assert(0);
824 }
825
826 if (!PY_PARSE_NODE_IS_NULL(pns->nodes[2])) {
827 // SyntaxError, cannot delete
828 assert(0);
829 }
830 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
831 pn = ((py_parse_node_struct_t*)pn)->nodes[0];
832 if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
833 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
834 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
835
836 if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
837 py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1];
838 if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
839 // sequence of one item, with trailing comma
840 assert(PY_PARSE_NODE_IS_NULL(pns1->nodes[0]));
841 c_del_stmt(comp, pns->nodes[0]);
842 } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
843 // sequence of many items
844 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns1);
845 c_del_stmt(comp, pns->nodes[0]);
846 for (int i = 0; i < n; i++) {
847 c_del_stmt(comp, pns1->nodes[i]);
848 }
849 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
850 // TODO not implemented; can't del comprehension?
851 assert(0);
852 } else {
853 // sequence with 2 items
854 goto sequence_with_2_items;
855 }
856 } else {
857 // sequence with 2 items
858 sequence_with_2_items:
859 c_del_stmt(comp, pns->nodes[0]);
860 c_del_stmt(comp, pns->nodes[1]);
861 }
862 } else {
863 // tuple with 1 element
864 c_del_stmt(comp, pn);
865 }
866 } else {
867 // not implemented
868 assert(0);
869 }
870}
871
872void compile_del_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
873 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
874}
875
876void compile_break_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
877 if (comp->break_label == 0) {
878 printf("ERROR: cannot break from here\n");
879 }
880 EMIT(break_loop, comp->break_label);
881}
882
883void compile_continue_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
884 if (comp->continue_label == 0) {
885 printf("ERROR: cannot continue from here\n");
886 }
887 if (comp->except_nest_level > 0) {
888 EMIT(continue_loop, comp->continue_label);
889 } else {
890 EMIT(jump, comp->continue_label);
891 }
892}
893
894void compile_return_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
895 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
896 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
897 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
898 // special case when returning an if-expression; to match CPython optimisation
899 py_parse_node_struct_t *pns_test_if_expr = (py_parse_node_struct_t*)pns->nodes[0];
900 py_parse_node_struct_t *pns_test_if_else = (py_parse_node_struct_t*)pns_test_if_expr->nodes[1];
901
Damienb05d7072013-10-05 13:37:10 +0100902 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100903 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
904 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
905 EMIT(return_value);
906 EMIT(label_assign, l_fail);
907 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
908 } else {
909 compile_node(comp, pns->nodes[0]);
910 }
911 EMIT(return_value);
912}
913
914void compile_yield_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
915 compile_node(comp, pns->nodes[0]);
916 EMIT(pop_top);
917}
918
919void compile_raise_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
920 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
921 // raise
922 EMIT(raise_varargs, 0);
923 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
924 // raise x from y
925 pns = (py_parse_node_struct_t*)pns->nodes[0];
926 compile_node(comp, pns->nodes[0]);
927 compile_node(comp, pns->nodes[1]);
928 EMIT(raise_varargs, 2);
929 } else {
930 // raise x
931 compile_node(comp, pns->nodes[0]);
932 EMIT(raise_varargs, 1);
933 }
934}
935
936// q1 holds the base, q2 the full name
937// eg a -> q1=q2=a
938// a.b.c -> q1=a, q2=a.b.c
939void do_import_name(compiler_t *comp, py_parse_node_t pn, qstr *q1, qstr *q2) {
940 bool is_as = false;
941 if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
942 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
943 // a name of the form x as y; unwrap it
944 *q1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
945 pn = pns->nodes[0];
946 is_as = true;
947 }
948 if (PY_PARSE_NODE_IS_ID(pn)) {
949 // just a simple name
950 *q2 = PY_PARSE_NODE_LEAF_ARG(pn);
951 if (!is_as) {
952 *q1 = *q2;
953 }
954 EMIT(import_name, *q2);
955 } else if (PY_PARSE_NODE_IS_STRUCT(pn)) {
956 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
957 if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
958 // a name of the form a.b.c
959 if (!is_as) {
960 *q1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
961 }
962 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
963 int len = n - 1;
964 for (int i = 0; i < n; i++) {
965 len += strlen(qstr_str(PY_PARSE_NODE_LEAF_ARG(pns->nodes[i])));
966 }
967 char *str = m_new(char, len + 1);
968 str[0] = 0;
969 for (int i = 0; i < n; i++) {
970 if (i > 0) {
971 strcat(str, ".");
972 }
973 strcat(str, qstr_str(PY_PARSE_NODE_LEAF_ARG(pns->nodes[i])));
974 }
975 *q2 = qstr_from_str_take(str);
976 EMIT(import_name, *q2);
977 if (is_as) {
978 for (int i = 1; i < n; i++) {
979 EMIT(load_attr, PY_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
980 }
981 }
982 } else {
983 // TODO not implemented
984 assert(0);
985 }
986 } else {
987 // TODO not implemented
988 assert(0);
989 }
990}
991
992void compile_dotted_as_name(compiler_t *comp, py_parse_node_t pn) {
993 EMIT(load_const_small_int, 0); // ??
994 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
995 qstr q1, q2;
996 do_import_name(comp, pn, &q1, &q2);
Damien4b03e772013-10-05 14:17:09 +0100997 EMIT(store_id, q1);
Damien429d7192013-10-04 19:53:11 +0100998}
999
1000void compile_import_name(compiler_t *comp, py_parse_node_struct_t *pns) {
1001 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1002}
1003
1004void compile_import_from(compiler_t *comp, py_parse_node_struct_t *pns) {
1005 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_STAR)) {
1006 EMIT(load_const_small_int, 0); // what's this for??
1007 EMIT(load_const_verbatim_start);
1008 EMIT(load_const_verbatim_str, "('*',)");
1009 EMIT(load_const_verbatim_end);
1010 qstr dummy_q, id1;
1011 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1012 EMIT(import_star);
1013 } else {
1014 py_parse_node_t *pn_nodes;
1015 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
1016
1017 EMIT(load_const_small_int, 0); // what's this for??
1018 EMIT(load_const_verbatim_start);
1019 EMIT(load_const_verbatim_str, "(");
1020 for (int i = 0; i < n; i++) {
1021 assert(PY_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1022 py_parse_node_struct_t *pns3 = (py_parse_node_struct_t*)pn_nodes[i];
1023 qstr id2 = PY_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
1024 if (i > 0) {
1025 EMIT(load_const_verbatim_str, ", ");
1026 }
1027 EMIT(load_const_verbatim_str, "'");
1028 EMIT(load_const_verbatim_str, qstr_str(id2));
1029 EMIT(load_const_verbatim_str, "'");
1030 }
1031 if (n == 1) {
1032 EMIT(load_const_verbatim_str, ",");
1033 }
1034 EMIT(load_const_verbatim_str, ")");
1035 EMIT(load_const_verbatim_end);
1036 qstr dummy_q, id1;
1037 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1038 for (int i = 0; i < n; i++) {
1039 assert(PY_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1040 py_parse_node_struct_t *pns3 = (py_parse_node_struct_t*)pn_nodes[i];
1041 qstr id2 = PY_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
1042 EMIT(import_from, id2);
1043 if (PY_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien4b03e772013-10-05 14:17:09 +01001044 EMIT(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001045 } else {
Damien4b03e772013-10-05 14:17:09 +01001046 EMIT(store_id, PY_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001047 }
1048 }
1049 EMIT(pop_top);
1050 }
1051}
1052
1053void compile_global_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001054 if (comp->pass == PASS_1) {
1055 if (PY_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1056 scope_declare_global(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
1057 } else {
1058 pns = (py_parse_node_struct_t*)pns->nodes[0];
1059 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1060 for (int i = 0; i < num_nodes; i++) {
1061 scope_declare_global(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
1062 }
Damien429d7192013-10-04 19:53:11 +01001063 }
1064 }
1065}
1066
1067void compile_nonlocal_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001068 if (comp->pass == PASS_1) {
1069 if (PY_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1070 scope_declare_nonlocal(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
1071 } else {
1072 pns = (py_parse_node_struct_t*)pns->nodes[0];
1073 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1074 for (int i = 0; i < num_nodes; i++) {
1075 scope_declare_nonlocal(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
1076 }
Damien429d7192013-10-04 19:53:11 +01001077 }
1078 }
1079}
1080
1081void compile_assert_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001082 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001083 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien4b03e772013-10-05 14:17:09 +01001084 EMIT(load_id, comp->qstr_assertion_error);
Damien429d7192013-10-04 19:53:11 +01001085 if (!PY_PARSE_NODE_IS_NULL(pns->nodes[1])) {
1086 // assertion message
1087 compile_node(comp, pns->nodes[1]);
1088 EMIT(call_function, 1, 0, false, false);
1089 }
1090 EMIT(raise_varargs, 1);
1091 EMIT(label_assign, l_end);
1092}
1093
1094void compile_if_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
1095 // TODO proper and/or short circuiting
1096
Damienb05d7072013-10-05 13:37:10 +01001097 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001098
Damienb05d7072013-10-05 13:37:10 +01001099 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001100 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1101
1102 compile_node(comp, pns->nodes[1]); // if block
1103 //if (!(PY_PARSE_NODE_IS_NULL(pns->nodes[2]) && PY_PARSE_NODE_IS_NULL(pns->nodes[3]))) { // optimisation; doesn't align with CPython
1104 // jump over elif/else blocks if they exist
Damien415eb6f2013-10-05 12:19:06 +01001105 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien429d7192013-10-04 19:53:11 +01001106 EMIT(jump, l_end);
1107 }
1108 //}
1109 EMIT(label_assign, l_fail);
1110
1111 if (!PY_PARSE_NODE_IS_NULL(pns->nodes[2])) {
1112 // compile elif blocks
1113
1114 py_parse_node_struct_t *pns_elif = (py_parse_node_struct_t*)pns->nodes[2];
1115
1116 if (PY_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
1117 // multiple elif blocks
1118
1119 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
1120 for (int i = 0; i < n; i++) {
1121 py_parse_node_struct_t *pns_elif2 = (py_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001122 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001123 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1124
1125 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001126 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien429d7192013-10-04 19:53:11 +01001127 EMIT(jump, l_end);
1128 }
1129 EMIT(label_assign, l_fail);
1130 }
1131
1132 } else {
1133 // a single elif block
1134
Damienb05d7072013-10-05 13:37:10 +01001135 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001136 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1137
1138 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001139 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien429d7192013-10-04 19:53:11 +01001140 EMIT(jump, l_end);
1141 }
1142 EMIT(label_assign, l_fail);
1143 }
1144 }
1145
1146 // compile else block
1147 compile_node(comp, pns->nodes[3]); // can be null
1148
1149 EMIT(label_assign, l_end);
1150}
1151
1152void compile_while_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
1153 int old_break_label = comp->break_label;
1154 int old_continue_label = comp->continue_label;
1155
Damienb05d7072013-10-05 13:37:10 +01001156 int done_label = comp_next_label(comp);
1157 int end_label = comp_next_label(comp);
1158 int break_label = comp_next_label(comp);
1159 int continue_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001160
1161 comp->break_label = break_label;
1162 comp->continue_label = continue_label;
1163
1164 EMIT(setup_loop, end_label);
1165 EMIT(label_assign, continue_label);
1166 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1167 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001168 if (!EMIT(last_emit_was_return_value)) {
Damien429d7192013-10-04 19:53:11 +01001169 EMIT(jump, continue_label);
1170 }
1171 EMIT(label_assign, done_label);
1172
1173 // break/continue apply to outer loop (if any) in the else block
1174 comp->break_label = old_break_label;
1175 comp->continue_label = old_continue_label;
1176
1177 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1178 // this is a small hack to agree with CPython
1179 if (!node_is_const_true(pns->nodes[0])) {
1180 EMIT(pop_block);
1181 }
1182
1183 compile_node(comp, pns->nodes[2]); // else
1184
1185 EMIT(label_assign, break_label);
1186 EMIT(label_assign, end_label);
1187}
1188
1189void compile_for_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
1190 int old_break_label = comp->break_label;
1191 int old_continue_label = comp->continue_label;
1192
Damienb05d7072013-10-05 13:37:10 +01001193 int for_label = comp_next_label(comp);
1194 int pop_label = comp_next_label(comp);
1195 int end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001196
Damienb05d7072013-10-05 13:37:10 +01001197 int break_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001198
1199 comp->continue_label = for_label;
1200 comp->break_label = break_label;
1201
1202 EMIT(setup_loop, end_label);
1203 compile_node(comp, pns->nodes[1]); // iterator
1204 EMIT(get_iter);
1205 EMIT(label_assign, for_label);
1206 EMIT(for_iter, pop_label);
1207 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1208 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001209 if (!EMIT(last_emit_was_return_value)) {
Damien429d7192013-10-04 19:53:11 +01001210 EMIT(jump, for_label);
1211 }
1212 EMIT(label_assign, pop_label);
1213 EMIT(for_iter_end);
1214
1215 // break/continue apply to outer loop (if any) in the else block
1216 comp->break_label = old_break_label;
1217 comp->continue_label = old_continue_label;
1218
1219 EMIT(pop_block);
1220
1221 compile_node(comp, pns->nodes[3]); // else (not tested)
1222
1223 EMIT(label_assign, break_label);
1224 EMIT(label_assign, end_label);
1225}
1226
1227void compile_try_except(compiler_t *comp, py_parse_node_t pn_body, int n_except, py_parse_node_t *pn_excepts, py_parse_node_t pn_else) {
1228 // this function is a bit of a hack at the moment
1229 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1230
1231 // setup code
1232 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001233 int l1 = comp_next_label(comp);
1234 int success_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001235 comp->except_nest_level += 1; // for correct handling of continue
1236 EMIT(setup_except, l1);
1237 compile_node(comp, pn_body); // body
1238 EMIT(pop_block);
1239 EMIT(jump, success_label);
1240 EMIT(label_assign, l1);
Damienb05d7072013-10-05 13:37:10 +01001241 int l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001242
1243 for (int i = 0; i < n_except; i++) {
1244 assert(PY_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1245 py_parse_node_struct_t *pns_except = (py_parse_node_struct_t*)pn_excepts[i];
1246
1247 qstr qstr_exception_local = 0;
Damienb05d7072013-10-05 13:37:10 +01001248 int end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001249
1250 if (PY_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
1251 // this is a catch all exception handler
1252 if (i + 1 != n_except) {
1253 printf("SyntaxError: default 'except:' must be last\n");
1254 return;
1255 }
1256 } else {
1257 // this exception handler requires a match to a certain type of exception
1258 py_parse_node_t pns_exception_expr = pns_except->nodes[0];
1259 if (PY_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1260 py_parse_node_struct_t *pns3 = (py_parse_node_struct_t*)pns_exception_expr;
1261 if (PY_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
1262 // handler binds the exception to a local
1263 pns_exception_expr = pns3->nodes[0];
1264 qstr_exception_local = PY_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
1265 }
1266 }
1267 EMIT(dup_top);
1268 compile_node(comp, pns_exception_expr);
1269 EMIT(compare_op, RT_COMPARE_OP_EXCEPTION_MATCH);
1270 EMIT(pop_jump_if_false, end_finally_label);
1271 }
1272
1273 EMIT(pop_top);
1274
1275 if (qstr_exception_local == 0) {
1276 EMIT(pop_top);
1277 } else {
Damien4b03e772013-10-05 14:17:09 +01001278 EMIT(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001279 }
1280
1281 EMIT(pop_top);
1282
1283 int l3;
1284 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001285 l3 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001286 EMIT(setup_finally, l3);
1287 }
1288 compile_node(comp, pns_except->nodes[1]);
1289 if (qstr_exception_local != 0) {
1290 EMIT(pop_block);
1291 }
1292 EMIT(pop_except);
1293 if (qstr_exception_local != 0) {
1294 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
1295 EMIT(label_assign, l3);
1296 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
Damien4b03e772013-10-05 14:17:09 +01001297 EMIT(store_id, qstr_exception_local);
1298 EMIT(delete_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001299 EMIT(end_finally);
1300 }
1301 EMIT(jump, l2);
1302 EMIT(label_assign, end_finally_label);
1303 }
1304
1305 EMIT(end_finally);
1306 EMIT(label_assign, success_label);
1307 comp->except_nest_level -= 1;
1308 compile_node(comp, pn_else); // else block, can be null
1309 EMIT(label_assign, l2);
1310 EMIT(set_stack_size, stack_size);
1311}
1312
1313void compile_try_finally(compiler_t *comp, py_parse_node_t pn_body, int n_except, py_parse_node_t *pn_except, py_parse_node_t pn_else, py_parse_node_t pn_finally) {
1314 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1315 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001316 int l_finally_block = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001317 EMIT(setup_finally, l_finally_block);
1318 if (n_except == 0) {
1319 assert(PY_PARSE_NODE_IS_NULL(pn_else));
1320 compile_node(comp, pn_body);
1321 } else {
1322 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1323 }
1324 EMIT(pop_block);
1325 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
1326 EMIT(label_assign, l_finally_block);
1327 compile_node(comp, pn_finally);
1328 EMIT(end_finally);
1329 EMIT(set_stack_size, stack_size);
1330}
1331
1332void compile_try_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
1333 if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1334 py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[1];
1335 if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
1336 // just try-finally
1337 compile_try_finally(comp, pns->nodes[0], 0, NULL, PY_PARSE_NODE_NULL, pns2->nodes[0]);
1338 } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
1339 // try-except and possibly else and/or finally
1340 py_parse_node_t *pn_excepts;
1341 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
1342 if (PY_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
1343 // no finally
1344 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1345 } else {
1346 // have finally
1347 compile_try_finally(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1], ((py_parse_node_struct_t*)pns2->nodes[2])->nodes[0]);
1348 }
1349 } else {
1350 // just try-except
1351 py_parse_node_t *pn_excepts;
1352 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
1353 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, PY_PARSE_NODE_NULL);
1354 }
1355 } else {
1356 // shouldn't happen
1357 assert(0);
1358 }
1359}
1360
1361void compile_with_stmt_helper(compiler_t *comp, int n, py_parse_node_t *nodes, py_parse_node_t body) {
1362 if (n == 0) {
1363 // no more pre-bits, compile the body of the with
1364 compile_node(comp, body);
1365 } else {
Damienb05d7072013-10-05 13:37:10 +01001366 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001367 if (PY_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
1368 // this pre-bit is of the form "a as b"
1369 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)nodes[0];
1370 compile_node(comp, pns->nodes[0]);
1371 EMIT(setup_with, l_end);
1372 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1373 } else {
1374 // this pre-bit is just an expression
1375 compile_node(comp, nodes[0]);
1376 EMIT(setup_with, l_end);
1377 EMIT(pop_top);
1378 }
1379 // compile additional pre-bits and the body
1380 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1381 // finish this with block
1382 EMIT(pop_block);
1383 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
1384 EMIT(label_assign, l_end);
1385 EMIT(with_cleanup);
1386 EMIT(end_finally);
1387 }
1388}
1389
1390void compile_with_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
1391 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
1392 py_parse_node_t *nodes;
1393 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1394 assert(n > 0);
1395
1396 // compile in a nested fashion
1397 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1398}
1399
1400void compile_expr_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
1401 if (PY_PARSE_NODE_IS_NULL(pns->nodes[1])) {
1402 if (PY_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !PY_PARSE_NODE_IS_ID(pns->nodes[0])) {
1403 // do nothing with a lonely constant
1404 } else {
1405 compile_node(comp, pns->nodes[0]); // just an expression
1406 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1407 }
1408 } else {
1409 py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1];
1410 int kind = PY_PARSE_NODE_STRUCT_KIND(pns1);
1411 if (kind == PN_expr_stmt_augassign) {
1412 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1413 compile_node(comp, pns1->nodes[1]); // rhs
1414 assert(PY_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
1415 // note that we don't really need to implement separate inplace ops, just normal binary ops will suffice
1416 switch (PY_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
1417 case PY_TOKEN_DEL_PIPE_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_OR); break;
1418 case PY_TOKEN_DEL_CARET_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_XOR); break;
1419 case PY_TOKEN_DEL_AMPERSAND_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_AND); break;
1420 case PY_TOKEN_DEL_DBL_LESS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_LSHIFT); break;
1421 case PY_TOKEN_DEL_DBL_MORE_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_RSHIFT); break;
1422 case PY_TOKEN_DEL_PLUS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_ADD); break;
1423 case PY_TOKEN_DEL_MINUS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_SUBTRACT); break;
1424 case PY_TOKEN_DEL_STAR_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_MULTIPLY); break;
1425 case PY_TOKEN_DEL_DBL_SLASH_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_FLOOR_DIVIDE); break;
1426 case PY_TOKEN_DEL_SLASH_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_TRUE_DIVIDE); break;
1427 case PY_TOKEN_DEL_PERCENT_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_MODULO); break;
1428 case PY_TOKEN_DEL_DBL_STAR_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_POWER); break;
1429 default: assert(0); // shouldn't happen
1430 }
1431 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1432 } else if (kind == PN_expr_stmt_assign_list) {
1433 int rhs = PY_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1434 compile_node(comp, ((py_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
1435 // following CPython, we store left-most first
1436 if (rhs > 0) {
1437 EMIT(dup_top);
1438 }
1439 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1440 for (int i = 0; i < rhs; i++) {
1441 if (i + 1 < rhs) {
1442 EMIT(dup_top);
1443 }
1444 c_assign(comp, ((py_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
1445 }
1446 } else if (kind == PN_expr_stmt_assign) {
1447 if (PY_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1448 && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1449 && PY_PARSE_NODE_STRUCT_NUM_NODES((py_parse_node_struct_t*)pns1->nodes[0]) == 2
1450 && PY_PARSE_NODE_STRUCT_NUM_NODES((py_parse_node_struct_t*)pns->nodes[0]) == 2) {
1451 // optimisation for a, b = c, d; to match CPython's optimisation
1452 py_parse_node_struct_t* pns10 = (py_parse_node_struct_t*)pns1->nodes[0];
1453 py_parse_node_struct_t* pns0 = (py_parse_node_struct_t*)pns->nodes[0];
1454 compile_node(comp, pns10->nodes[0]); // rhs
1455 compile_node(comp, pns10->nodes[1]); // rhs
1456 EMIT(rot_two);
1457 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1458 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1459 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1460 && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1461 && PY_PARSE_NODE_STRUCT_NUM_NODES((py_parse_node_struct_t*)pns1->nodes[0]) == 3
1462 && PY_PARSE_NODE_STRUCT_NUM_NODES((py_parse_node_struct_t*)pns->nodes[0]) == 3) {
1463 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
1464 py_parse_node_struct_t* pns10 = (py_parse_node_struct_t*)pns1->nodes[0];
1465 py_parse_node_struct_t* pns0 = (py_parse_node_struct_t*)pns->nodes[0];
1466 compile_node(comp, pns10->nodes[0]); // rhs
1467 compile_node(comp, pns10->nodes[1]); // rhs
1468 compile_node(comp, pns10->nodes[2]); // rhs
1469 EMIT(rot_three);
1470 EMIT(rot_two);
1471 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1472 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1473 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1474 } else {
1475 compile_node(comp, pns1->nodes[0]); // rhs
1476 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1477 }
1478 } else {
1479 // shouldn't happen
1480 assert(0);
1481 }
1482 }
1483}
1484
1485void c_binary_op(compiler_t *comp, py_parse_node_struct_t *pns, rt_binary_op_t binary_op) {
1486 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1487 compile_node(comp, pns->nodes[0]);
1488 for (int i = 1; i < num_nodes; i += 1) {
1489 compile_node(comp, pns->nodes[i]);
1490 EMIT(binary_op, binary_op);
1491 }
1492}
1493
1494void compile_test_if_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
1495 assert(PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1496 py_parse_node_struct_t *pns_test_if_else = (py_parse_node_struct_t*)pns->nodes[1];
1497
1498 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001499 int l_fail = comp_next_label(comp);
1500 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001501 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1502 compile_node(comp, pns->nodes[0]); // success value
1503 EMIT(jump, l_end);
1504 EMIT(label_assign, l_fail);
1505 EMIT(set_stack_size, stack_size); // force stack size reset
1506 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1507 EMIT(label_assign, l_end);
1508}
1509
1510void compile_lambdef(compiler_t *comp, py_parse_node_struct_t *pns) {
1511 // TODO default params etc for lambda; possibly just use funcdef code
1512 //py_parse_node_t pn_params = pns->nodes[0];
1513 //py_parse_node_t pn_body = pns->nodes[1];
1514
1515 if (comp->pass == PASS_1) {
1516 // create a new scope for this lambda
1517 scope_t *s = scope_new_and_link(comp, SCOPE_LAMBDA, (py_parse_node_t)pns);
1518 // store the lambda scope so the compiling function (this one) can use it at each pass
1519 pns->nodes[2] = (py_parse_node_t)s;
1520 }
1521
1522 // get the scope for this lambda
1523 scope_t *this_scope = (scope_t*)pns->nodes[2];
1524
1525 // make the lambda
1526 close_over_variables_etc(comp, this_scope, 0, 0);
1527}
1528
1529void compile_or_test(compiler_t *comp, py_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001530 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001531 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1532 for (int i = 0; i < n; i += 1) {
1533 compile_node(comp, pns->nodes[i]);
1534 if (i + 1 < n) {
1535 EMIT(jump_if_true_or_pop, l_end);
1536 }
1537 }
1538 EMIT(label_assign, l_end);
1539}
1540
1541void compile_and_test(compiler_t *comp, py_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001542 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001543 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1544 for (int i = 0; i < n; i += 1) {
1545 compile_node(comp, pns->nodes[i]);
1546 if (i + 1 < n) {
1547 EMIT(jump_if_false_or_pop, l_end);
1548 }
1549 }
1550 EMIT(label_assign, l_end);
1551}
1552
1553void compile_not_test_2(compiler_t *comp, py_parse_node_struct_t *pns) {
1554 compile_node(comp, pns->nodes[0]);
1555 EMIT(unary_op, RT_UNARY_OP_NOT);
1556}
1557
1558void compile_comparison(compiler_t *comp, py_parse_node_struct_t *pns) {
1559 int stack_size = EMIT(get_stack_size);
1560 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1561 compile_node(comp, pns->nodes[0]);
1562 bool multi = (num_nodes > 3);
1563 int l_fail = 0;
1564 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001565 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001566 }
1567 for (int i = 1; i + 1 < num_nodes; i += 2) {
1568 compile_node(comp, pns->nodes[i + 1]);
1569 if (i + 2 < num_nodes) {
1570 EMIT(dup_top);
1571 EMIT(rot_three);
1572 }
1573 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_LESS)) {
1574 EMIT(compare_op, RT_COMPARE_OP_LESS);
1575 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_MORE)) {
1576 EMIT(compare_op, RT_COMPARE_OP_MORE);
1577 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_DBL_EQUAL)) {
1578 EMIT(compare_op, RT_COMPARE_OP_EQUAL);
1579 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_LESS_EQUAL)) {
1580 EMIT(compare_op, RT_COMPARE_OP_LESS_EQUAL);
1581 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_MORE_EQUAL)) {
1582 EMIT(compare_op, RT_COMPARE_OP_MORE_EQUAL);
1583 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_NOT_EQUAL)) {
1584 EMIT(compare_op, RT_COMPARE_OP_NOT_EQUAL);
1585 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_KW_IN)) {
1586 EMIT(compare_op, RT_COMPARE_OP_IN);
1587 } else if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
1588 py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[i];
1589 int kind = PY_PARSE_NODE_STRUCT_KIND(pns2);
1590 if (kind == PN_comp_op_not_in) {
1591 EMIT(compare_op, RT_COMPARE_OP_NOT_IN);
1592 } else if (kind == PN_comp_op_is) {
1593 if (PY_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
1594 EMIT(compare_op, RT_COMPARE_OP_IS);
1595 } else {
1596 EMIT(compare_op, RT_COMPARE_OP_IS_NOT);
1597 }
1598 } else {
1599 // shouldn't happen
1600 assert(0);
1601 }
1602 } else {
1603 // shouldn't happen
1604 assert(0);
1605 }
1606 if (i + 2 < num_nodes) {
1607 EMIT(jump_if_false_or_pop, l_fail);
1608 }
1609 }
1610 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001611 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001612 EMIT(jump, l_end);
1613 EMIT(label_assign, l_fail);
1614 EMIT(rot_two);
1615 EMIT(pop_top);
1616 EMIT(label_assign, l_end);
1617 EMIT(set_stack_size, stack_size + 1); // force stack size
1618 }
1619}
1620
1621void compile_star_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
1622 // TODO
1623 assert(0);
1624 compile_node(comp, pns->nodes[0]);
1625 //EMIT(unary_op, "UNARY_STAR");
1626}
1627
1628void compile_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
1629 c_binary_op(comp, pns, RT_BINARY_OP_OR);
1630}
1631
1632void compile_xor_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
1633 c_binary_op(comp, pns, RT_BINARY_OP_XOR);
1634}
1635
1636void compile_and_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
1637 c_binary_op(comp, pns, RT_BINARY_OP_AND);
1638}
1639
1640void compile_shift_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
1641 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1642 compile_node(comp, pns->nodes[0]);
1643 for (int i = 1; i + 1 < num_nodes; i += 2) {
1644 compile_node(comp, pns->nodes[i + 1]);
1645 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_DBL_LESS)) {
1646 EMIT(binary_op, RT_BINARY_OP_LSHIFT);
1647 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_DBL_MORE)) {
1648 EMIT(binary_op, RT_BINARY_OP_RSHIFT);
1649 } else {
1650 // shouldn't happen
1651 assert(0);
1652 }
1653 }
1654}
1655
1656void compile_arith_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
1657 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1658 compile_node(comp, pns->nodes[0]);
1659 for (int i = 1; i + 1 < num_nodes; i += 2) {
1660 compile_node(comp, pns->nodes[i + 1]);
1661 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_PLUS)) {
1662 EMIT(binary_op, RT_BINARY_OP_ADD);
1663 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_MINUS)) {
1664 EMIT(binary_op, RT_BINARY_OP_SUBTRACT);
1665 } else {
1666 // shouldn't happen
1667 assert(0);
1668 }
1669 }
1670}
1671
1672void compile_term(compiler_t *comp, py_parse_node_struct_t *pns) {
1673 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1674 compile_node(comp, pns->nodes[0]);
1675 for (int i = 1; i + 1 < num_nodes; i += 2) {
1676 compile_node(comp, pns->nodes[i + 1]);
1677 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_STAR)) {
1678 EMIT(binary_op, RT_BINARY_OP_MULTIPLY);
1679 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_DBL_SLASH)) {
1680 EMIT(binary_op, RT_BINARY_OP_FLOOR_DIVIDE);
1681 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_SLASH)) {
1682 EMIT(binary_op, RT_BINARY_OP_TRUE_DIVIDE);
1683 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_PERCENT)) {
1684 EMIT(binary_op, RT_BINARY_OP_MODULO);
1685 } else {
1686 // shouldn't happen
1687 assert(0);
1688 }
1689 }
1690}
1691
1692void compile_factor_2(compiler_t *comp, py_parse_node_struct_t *pns) {
1693 compile_node(comp, pns->nodes[1]);
1694 if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_PLUS)) {
1695 EMIT(unary_op, RT_UNARY_OP_POSITIVE);
1696 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_MINUS)) {
1697 EMIT(unary_op, RT_UNARY_OP_NEGATIVE);
1698 } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_TILDE)) {
1699 EMIT(unary_op, RT_UNARY_OP_INVERT);
1700 } else {
1701 // shouldn't happen
1702 assert(0);
1703 }
1704}
1705
1706void compile_trailer_paren_helper(compiler_t *comp, py_parse_node_struct_t *pns, bool is_method_call) {
1707 // function to call is on top of stack
1708
1709 int old_n_arg_keyword = comp->n_arg_keyword;
1710 bool old_have_star_arg = comp->have_star_arg;
1711 bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
1712 comp->n_arg_keyword = 0;
1713 comp->have_star_arg = false;
1714 comp->have_dbl_star_arg = false;
1715
1716 compile_node(comp, pns->nodes[0]); // arguments to function call; can be null
1717
1718 // compute number of positional arguments
1719 int n_positional = list_len(pns->nodes[0], PN_arglist) - comp->n_arg_keyword;
1720 if (comp->have_star_arg) {
1721 n_positional -= 1;
1722 }
1723 if (comp->have_dbl_star_arg) {
1724 n_positional -= 1;
1725 }
1726
1727 if (is_method_call) {
1728 EMIT(call_method, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg);
1729 } else {
1730 EMIT(call_function, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg);
1731 }
1732
1733 comp->n_arg_keyword = old_n_arg_keyword;
1734 comp->have_star_arg = old_have_star_arg;
1735 comp->have_dbl_star_arg = old_have_dbl_star_arg;
1736}
1737
1738void compile_power_trailers(compiler_t *comp, py_parse_node_struct_t *pns) {
1739 int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1740 for (int i = 0; i < num_nodes; i++) {
1741 if (i + 1 < num_nodes && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[i], PN_trailer_period) && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[i + 1], PN_trailer_paren)) {
1742 // optimisation for method calls a.f(...), following PyPy
1743 py_parse_node_struct_t *pns_period = (py_parse_node_struct_t*)pns->nodes[i];
1744 py_parse_node_struct_t *pns_paren = (py_parse_node_struct_t*)pns->nodes[i + 1];
1745 EMIT(load_method, PY_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
1746 compile_trailer_paren_helper(comp, pns_paren, true);
1747 i += 1;
1748 } else {
1749 compile_node(comp, pns->nodes[i]);
1750 }
1751 }
1752}
1753
1754void compile_power_dbl_star(compiler_t *comp, py_parse_node_struct_t *pns) {
1755 compile_node(comp, pns->nodes[0]);
1756 EMIT(binary_op, RT_BINARY_OP_POWER);
1757}
1758
1759void compile_atom_string(compiler_t *comp, py_parse_node_struct_t *pns) {
1760 // a list of strings
1761 EMIT(load_const_verbatim_start);
1762 EMIT(load_const_verbatim_str, "'");
1763 int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
1764 for (int i = 0; i < n; i++) {
1765 // TODO allow concatenation of either strings or bytes, but not mixed
1766 assert(PY_PARSE_NODE_IS_LEAF(pns->nodes[i]));
1767 assert(PY_PARSE_NODE_LEAF_KIND(pns->nodes[i]) == PY_PARSE_NODE_STRING);
1768 const char *str = qstr_str(PY_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
1769 EMIT(load_const_verbatim_strn, str, strlen(str));
1770 }
1771 EMIT(load_const_verbatim_str, "'");
1772 EMIT(load_const_verbatim_end);
1773}
1774
1775// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
1776void compile_comprehension(compiler_t *comp, py_parse_node_struct_t *pns, scope_kind_t kind) {
1777 assert(PY_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
1778 assert(PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
1779 py_parse_node_struct_t *pns_comp_for = (py_parse_node_struct_t*)pns->nodes[1];
1780
1781 if (comp->pass == PASS_1) {
1782 // create a new scope for this comprehension
1783 scope_t *s = scope_new_and_link(comp, kind, (py_parse_node_t)pns);
1784 // store the comprehension scope so the compiling function (this one) can use it at each pass
1785 pns_comp_for->nodes[3] = (py_parse_node_t)s;
1786 }
1787
1788 // get the scope for this comprehension
1789 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
1790
1791 // compile the comprehension
1792 close_over_variables_etc(comp, this_scope, 0, 0);
1793
1794 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
1795 EMIT(get_iter);
1796 EMIT(call_function, 1, 0, false, false);
1797}
1798
1799void compile_atom_paren(compiler_t *comp, py_parse_node_struct_t *pns) {
1800 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
1801 // an empty tuple
1802 /*
1803 EMIT(build_tuple, 0);
1804 */
1805 c_tuple(comp, PY_PARSE_NODE_NULL, NULL);
1806 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
1807 pns = (py_parse_node_struct_t*)pns->nodes[0];
1808 assert(!PY_PARSE_NODE_IS_NULL(pns->nodes[1]));
1809 if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1810 py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[1];
1811 if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
1812 // tuple of one item, with trailing comma
1813 assert(PY_PARSE_NODE_IS_NULL(pns2->nodes[0]));
1814 /*
1815 compile_node(comp, pns->nodes[0]);
1816 EMIT(build_tuple, 1);
1817 */
1818 c_tuple(comp, pns->nodes[0], NULL);
1819 } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
1820 // tuple of many items
1821 /*
1822 compile_node(comp, pns->nodes[0]);
1823 compile_generic_all_nodes(comp, pns2);
1824 EMIT(build_tuple, 1 + PY_PARSE_NODE_STRUCT_NUM_NODES(pns2));
1825 */
1826 c_tuple(comp, pns->nodes[0], pns2);
1827 } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
1828 // generator expression
1829 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
1830 } else {
1831 // tuple with 2 items
1832 goto tuple_with_2_items;
1833 }
1834 } else {
1835 // tuple with 2 items
1836 tuple_with_2_items:
1837 /*
1838 compile_node(comp, pns->nodes[0]);
1839 compile_node(comp, pns->nodes[1]);
1840 EMIT(build_tuple, 2);
1841 */
1842 c_tuple(comp, PY_PARSE_NODE_NULL, pns);
1843 }
1844 } else {
1845 // parenthesis around a single item, is just that item
1846 compile_node(comp, pns->nodes[0]);
1847 }
1848}
1849
1850void compile_atom_bracket(compiler_t *comp, py_parse_node_struct_t *pns) {
1851 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
1852 // empty list
1853 EMIT(build_list, 0);
1854 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
1855 py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[0];
1856 if (PY_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
1857 py_parse_node_struct_t *pns3 = (py_parse_node_struct_t*)pns2->nodes[1];
1858 if (PY_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
1859 // list of one item, with trailing comma
1860 assert(PY_PARSE_NODE_IS_NULL(pns3->nodes[0]));
1861 compile_node(comp, pns2->nodes[0]);
1862 EMIT(build_list, 1);
1863 } else if (PY_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
1864 // list of many items
1865 compile_node(comp, pns2->nodes[0]);
1866 compile_generic_all_nodes(comp, pns3);
1867 EMIT(build_list, 1 + PY_PARSE_NODE_STRUCT_NUM_NODES(pns3));
1868 } else if (PY_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
1869 // list comprehension
1870 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
1871 } else {
1872 // list with 2 items
1873 goto list_with_2_items;
1874 }
1875 } else {
1876 // list with 2 items
1877 list_with_2_items:
1878 compile_node(comp, pns2->nodes[0]);
1879 compile_node(comp, pns2->nodes[1]);
1880 EMIT(build_list, 2);
1881 }
1882 } else {
1883 // list with 1 item
1884 compile_node(comp, pns->nodes[0]);
1885 EMIT(build_list, 1);
1886 }
1887}
1888
1889void compile_atom_brace(compiler_t *comp, py_parse_node_struct_t *pns) {
1890 py_parse_node_t pn = pns->nodes[0];
1891 if (PY_PARSE_NODE_IS_NULL(pn)) {
1892 // empty dict
1893 EMIT(build_map, 0);
1894 } else if (PY_PARSE_NODE_IS_STRUCT(pn)) {
1895 pns = (py_parse_node_struct_t*)pn;
1896 if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
1897 // dict with one element
1898 EMIT(build_map, 1);
1899 compile_node(comp, pn);
1900 EMIT(store_map);
1901 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
1902 assert(PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
1903 py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1];
1904 if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
1905 // dict/set with multiple elements
1906
1907 // get tail elements (2nd, 3rd, ...)
1908 py_parse_node_t *nodes;
1909 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
1910
1911 // first element sets whether it's a dict or set
1912 bool is_dict;
1913 if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
1914 // a dictionary
1915 EMIT(build_map, 1 + n);
1916 compile_node(comp, pns->nodes[0]);
1917 EMIT(store_map);
1918 is_dict = true;
1919 } else {
1920 // a set
1921 compile_node(comp, pns->nodes[0]); // 1st value of set
1922 is_dict = false;
1923 }
1924
1925 // process rest of elements
1926 for (int i = 0; i < n; i++) {
1927 py_parse_node_t pn = nodes[i];
1928 bool is_key_value = PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
1929 compile_node(comp, pn);
1930 if (is_dict) {
1931 if (!is_key_value) {
1932 printf("SyntaxError?: expecting key:value for dictionary");
1933 return;
1934 }
1935 EMIT(store_map);
1936 } else {
1937 if (is_key_value) {
1938 printf("SyntaxError?: expecting just a value for set");
1939 return;
1940 }
1941 }
1942 }
1943
1944 // if it's a set, build it
1945 if (!is_dict) {
1946 EMIT(build_set, 1 + n);
1947 }
1948 } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
1949 // dict/set comprehension
1950 if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
1951 // a dictionary comprehension
1952 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
1953 } else {
1954 // a set comprehension
1955 compile_comprehension(comp, pns, SCOPE_SET_COMP);
1956 }
1957 } else {
1958 // shouldn't happen
1959 assert(0);
1960 }
1961 } else {
1962 // set with one element
1963 goto set_with_one_element;
1964 }
1965 } else {
1966 // set with one element
1967 set_with_one_element:
1968 compile_node(comp, pn);
1969 EMIT(build_set, 1);
1970 }
1971}
1972
1973void compile_trailer_paren(compiler_t *comp, py_parse_node_struct_t *pns) {
1974 compile_trailer_paren_helper(comp, pns, false);
1975}
1976
1977void compile_trailer_bracket(compiler_t *comp, py_parse_node_struct_t *pns) {
1978 // object who's index we want is on top of stack
1979 compile_node(comp, pns->nodes[0]); // the index
1980 EMIT(binary_op, RT_BINARY_OP_SUBSCR);
1981}
1982
1983void compile_trailer_period(compiler_t *comp, py_parse_node_struct_t *pns) {
1984 // object who's attribute we want is on top of stack
1985 EMIT(load_attr, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
1986}
1987
1988void compile_subscript_3_helper(compiler_t *comp, py_parse_node_struct_t *pns) {
1989 assert(PY_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
1990 py_parse_node_t pn = pns->nodes[0];
1991 if (PY_PARSE_NODE_IS_NULL(pn)) {
1992 // [?:]
1993 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
1994 EMIT(build_slice, 2);
1995 } else if (PY_PARSE_NODE_IS_STRUCT(pn)) {
1996 pns = (py_parse_node_struct_t*)pn;
1997 if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
1998 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
1999 pn = pns->nodes[0];
2000 if (PY_PARSE_NODE_IS_NULL(pn)) {
2001 // [?::]
2002 EMIT(build_slice, 2);
2003 } else {
2004 // [?::x]
2005 compile_node(comp, pn);
2006 EMIT(build_slice, 3);
2007 }
2008 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
2009 compile_node(comp, pns->nodes[0]);
2010 assert(PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2011 pns = (py_parse_node_struct_t*)pns->nodes[1];
2012 assert(PY_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2013 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
2014 // [?:x:]
2015 EMIT(build_slice, 2);
2016 } else {
2017 // [?:x:x]
2018 compile_node(comp, pns->nodes[0]);
2019 EMIT(build_slice, 3);
2020 }
2021 } else {
2022 // [?:x]
2023 compile_node(comp, pn);
2024 EMIT(build_slice, 2);
2025 }
2026 } else {
2027 // [?:x]
2028 compile_node(comp, pn);
2029 EMIT(build_slice, 2);
2030 }
2031}
2032
2033void compile_subscript_2(compiler_t *comp, py_parse_node_struct_t *pns) {
2034 compile_node(comp, pns->nodes[0]); // start of slice
2035 assert(PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2036 compile_subscript_3_helper(comp, (py_parse_node_struct_t*)pns->nodes[1]);
2037}
2038
2039void compile_subscript_3(compiler_t *comp, py_parse_node_struct_t *pns) {
2040 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2041 compile_subscript_3_helper(comp, pns);
2042}
2043
2044void compile_dictorsetmaker_item(compiler_t *comp, py_parse_node_struct_t *pns) {
2045 // if this is called then we are compiling a dict key:value pair
2046 compile_node(comp, pns->nodes[1]); // value
2047 compile_node(comp, pns->nodes[0]); // key
2048}
2049
2050void compile_classdef(compiler_t *comp, py_parse_node_struct_t *pns) {
2051 qstr cname = compile_classdef_helper(comp, pns);
2052 // store class object into class name
Damien4b03e772013-10-05 14:17:09 +01002053 EMIT(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002054}
2055
2056void compile_arglist_star(compiler_t *comp, py_parse_node_struct_t *pns) {
2057 if (comp->have_star_arg) {
2058 printf("SyntaxError?: can't have multiple *x\n");
2059 return;
2060 }
2061 comp->have_star_arg = true;
2062 compile_node(comp, pns->nodes[0]);
2063}
2064
2065void compile_arglist_dbl_star(compiler_t *comp, py_parse_node_struct_t *pns) {
2066 if (comp->have_dbl_star_arg) {
2067 printf("SyntaxError?: can't have multiple **x\n");
2068 return;
2069 }
2070 comp->have_dbl_star_arg = true;
2071 compile_node(comp, pns->nodes[0]);
2072}
2073
2074void compile_argument(compiler_t *comp, py_parse_node_struct_t *pns) {
2075 assert(PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2076 py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[1];
2077 if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2078 if (!PY_PARSE_NODE_IS_ID(pns->nodes[0])) {
2079 printf("SyntaxError?: lhs of keyword argument must be an id\n");
2080 return;
2081 }
2082 EMIT(load_const_id, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
2083 compile_node(comp, pns2->nodes[0]);
2084 comp->n_arg_keyword += 1;
2085 } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
2086 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2087 } else {
2088 // shouldn't happen
2089 assert(0);
2090 }
2091}
2092
2093void compile_yield_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
2094 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
2095 printf("SyntaxError: 'yield' outside function\n");
2096 return;
2097 }
2098 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
2099 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2100 EMIT(yield_value);
2101 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2102 pns = (py_parse_node_struct_t*)pns->nodes[0];
2103 compile_node(comp, pns->nodes[0]);
2104 EMIT(get_iter);
2105 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2106 EMIT(yield_from);
2107 } else {
2108 compile_node(comp, pns->nodes[0]);
2109 EMIT(yield_value);
2110 }
2111}
2112
2113typedef void (*compile_function_t)(compiler_t*, py_parse_node_struct_t*);
2114static compile_function_t compile_function[] = {
2115 NULL,
2116#define nc NULL
2117#define c(f) compile_##f
2118#define DEF_RULE(rule, comp, kind, arg...) comp,
2119#include "grammar.h"
2120#undef nc
2121#undef c
2122#undef DEF_RULE
2123};
2124
2125void compile_node(compiler_t *comp, py_parse_node_t pn) {
2126 if (PY_PARSE_NODE_IS_NULL(pn)) {
2127 // pass
2128 } else if (PY_PARSE_NODE_IS_LEAF(pn)) {
2129 int arg = PY_PARSE_NODE_LEAF_ARG(pn);
2130 switch (PY_PARSE_NODE_LEAF_KIND(pn)) {
Damien4b03e772013-10-05 14:17:09 +01002131 case PY_PARSE_NODE_ID: EMIT(load_id, arg); break;
Damien429d7192013-10-04 19:53:11 +01002132 case PY_PARSE_NODE_SMALL_INT: EMIT(load_const_small_int, arg); break;
2133 case PY_PARSE_NODE_INTEGER: EMIT(load_const_int, arg); break;
2134 case PY_PARSE_NODE_DECIMAL: EMIT(load_const_dec, arg); break;
2135 case PY_PARSE_NODE_STRING: EMIT(load_const_str, arg, false); break;
2136 case PY_PARSE_NODE_BYTES: EMIT(load_const_str, arg, true); break;
2137 case PY_PARSE_NODE_TOKEN: EMIT(load_const_tok, arg); break;
2138 default: assert(0);
2139 }
2140 } else {
2141 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
2142 compile_function_t f = compile_function[PY_PARSE_NODE_STRUCT_KIND(pns)];
2143 if (f == NULL) {
2144 printf("node %u cannot be compiled\n", (uint)PY_PARSE_NODE_STRUCT_KIND(pns));
2145 parse_node_show(pn, 0);
2146 assert(0);
2147 } else {
2148 f(comp, pns);
2149 }
2150 }
2151}
2152
2153void compile_scope_func_lambda_param(compiler_t *comp, py_parse_node_t pn, pn_kind_t pn_name, pn_kind_t pn_star, pn_kind_t pn_dbl_star, bool allow_annotations) {
2154 // TODO verify that *k and **k are last etc
2155 assert(PY_PARSE_NODE_IS_STRUCT(pn));
2156 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
2157 qstr param_name = 0;
2158 py_parse_node_t pn_annotation = PY_PARSE_NODE_NULL;
2159 if (PY_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2160 param_name = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2161 //int node_index = 1; unused
2162 if (allow_annotations) {
2163 if (!PY_PARSE_NODE_IS_NULL(pns->nodes[1])) {
2164 // this parameter has an annotation
2165 pn_annotation = pns->nodes[1];
2166 }
2167 //node_index = 2; unused
2168 }
2169 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
2170 if (!PY_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
2171 // this parameter has a default value
2172 if (comp->have_bare_star) {
2173 comp->scope_cur->num_dict_params += 1;
2174 } else {
2175 comp->scope_cur->num_default_params += 1;
2176 }
2177 }
2178 */
2179 if (comp->have_bare_star) {
2180 // comes after a bare star, so doesn't count as a parameter
2181 } else {
2182 comp->scope_cur->num_params += 1;
2183 }
2184 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2185 if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) {
2186 // bare star
2187 // TODO see http://www.python.org/dev/peps/pep-3102/
2188 comp->have_bare_star = true;
2189 //assert(comp->scope_cur->num_dict_params == 0);
2190 } else if (PY_PARSE_NODE_IS_ID(pns->nodes[0])) {
2191 // named star
2192 comp->scope_cur->flags |= SCOPE_FLAG_VARARGS;
2193 param_name = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2194 } else if (allow_annotations && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
2195 // named star with annotation
2196 comp->scope_cur->flags |= SCOPE_FLAG_VARARGS;
2197 pns = (py_parse_node_struct_t*)pns->nodes[0];
2198 param_name = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2199 pn_annotation = pns->nodes[1];
2200 } else {
2201 // shouldn't happen
2202 assert(0);
2203 }
2204 } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2205 param_name = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2206 if (allow_annotations && !PY_PARSE_NODE_IS_NULL(pns->nodes[1])) {
2207 // this parameter has an annotation
2208 pn_annotation = pns->nodes[1];
2209 }
2210 comp->scope_cur->flags |= SCOPE_FLAG_VARKEYWORDS;
2211 } else {
2212 // TODO anything to implement?
2213 assert(0);
2214 }
2215
2216 if (param_name != 0) {
2217 if (!PY_PARSE_NODE_IS_NULL(pn_annotation)) {
2218 // TODO this parameter has an annotation
2219 }
2220 bool added;
2221 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2222 if (!added) {
2223 printf("SyntaxError?: same name used for parameter; %s\n", qstr_str(param_name));
2224 return;
2225 }
2226 id_info->param = true;
2227 id_info->kind = ID_INFO_KIND_LOCAL;
2228 }
2229}
2230
2231void compile_scope_func_param(compiler_t *comp, py_parse_node_t pn) {
2232 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2233}
2234
2235void compile_scope_lambda_param(compiler_t *comp, py_parse_node_t pn) {
2236 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2237}
2238
2239void compile_scope_comp_iter(compiler_t *comp, py_parse_node_t pn_iter, py_parse_node_t pn_inner_expr, int l_top, int for_depth) {
2240 tail_recursion:
2241 if (PY_PARSE_NODE_IS_NULL(pn_iter)) {
2242 // no more nested if/for; compile inner expression
2243 compile_node(comp, pn_inner_expr);
2244 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
2245 EMIT(list_append, for_depth + 2);
2246 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
2247 EMIT(map_add, for_depth + 2);
2248 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
2249 EMIT(set_add, for_depth + 2);
2250 } else {
2251 EMIT(yield_value);
2252 EMIT(pop_top);
2253 }
2254 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
2255 // if condition
2256 py_parse_node_struct_t *pns_comp_if = (py_parse_node_struct_t*)pn_iter;
2257 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2258 pn_iter = pns_comp_if->nodes[1];
2259 goto tail_recursion;
2260 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
2261 // for loop
2262 py_parse_node_struct_t *pns_comp_for2 = (py_parse_node_struct_t*)pn_iter;
2263 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002264 int l_end2 = comp_next_label(comp);
2265 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002266 EMIT(get_iter);
2267 EMIT(label_assign, l_top2);
2268 EMIT(for_iter, l_end2);
2269 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2270 compile_scope_comp_iter(comp, pns_comp_for2->nodes[2], pn_inner_expr, l_top2, for_depth + 1);
2271 EMIT(jump, l_top2);
2272 EMIT(label_assign, l_end2);
2273 EMIT(for_iter_end);
2274 } else {
2275 // shouldn't happen
2276 assert(0);
2277 }
2278}
2279
2280void check_for_doc_string(compiler_t *comp, py_parse_node_t pn) {
2281 // see http://www.python.org/dev/peps/pep-0257/
2282
2283 // look for the first statement
2284 if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2285 // fall through
2286 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
2287 pn = ((py_parse_node_struct_t*)pn)->nodes[0];
2288 } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
2289 pn = ((py_parse_node_struct_t*)pn)->nodes[0];
2290 } else {
2291 return;
2292 }
2293
2294 // check the first statement for a doc string
2295 if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2296 py_parse_node_struct_t* pns = (py_parse_node_struct_t*)pn;
2297 if (PY_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2298 int kind = PY_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2299 if (kind == PY_PARSE_NODE_STRING) {
2300 compile_node(comp, pns->nodes[0]); // a doc string
2301 // store doc string
Damien4b03e772013-10-05 14:17:09 +01002302 EMIT(store_id, comp->qstr___doc__);
Damien429d7192013-10-04 19:53:11 +01002303 }
2304 }
2305 }
2306}
2307
2308void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2309 comp->pass = pass;
2310 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002311 comp->next_label = 1;
Damien415eb6f2013-10-05 12:19:06 +01002312 EMIT(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002313
2314 if (comp->pass == PASS_1) {
2315 scope->stack_size = 0;
2316 }
2317
2318 if (comp->pass == PASS_3) {
2319 //printf("----\n");
2320 scope_print_info(scope);
2321 }
2322
2323 // compile
2324 if (scope->kind == SCOPE_MODULE) {
2325 check_for_doc_string(comp, scope->pn);
2326 compile_node(comp, scope->pn);
2327 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2328 EMIT(return_value);
2329 } else if (scope->kind == SCOPE_FUNCTION) {
2330 assert(PY_PARSE_NODE_IS_STRUCT(scope->pn));
2331 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)scope->pn;
2332 assert(PY_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
2333
2334 // work out number of parameters, keywords and default parameters, and add them to the id_info array
2335 if (comp->pass == PASS_1) {
2336 comp->have_bare_star = false;
2337 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2338 }
2339
2340 assert(pns->nodes[2] == 0); // 2 is something...
2341
2342 compile_node(comp, pns->nodes[3]); // 3 is function body
2343 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002344 if (!EMIT(last_emit_was_return_value)) {
Damien429d7192013-10-04 19:53:11 +01002345 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2346 EMIT(return_value);
2347 }
2348 } else if (scope->kind == SCOPE_LAMBDA) {
2349 assert(PY_PARSE_NODE_IS_STRUCT(scope->pn));
2350 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)scope->pn;
2351 assert(PY_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
2352
2353 // work out number of parameters, keywords and default parameters, and add them to the id_info array
2354 if (comp->pass == PASS_1) {
2355 comp->have_bare_star = false;
2356 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2357 }
2358
2359 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2360 EMIT(return_value);
2361 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2362 // a bit of a hack at the moment
2363
2364 assert(PY_PARSE_NODE_IS_STRUCT(scope->pn));
2365 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)scope->pn;
2366 assert(PY_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2367 assert(PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2368 py_parse_node_struct_t *pns_comp_for = (py_parse_node_struct_t*)pns->nodes[1];
2369
2370 qstr qstr_arg = qstr_from_strn_copy(".0", 2);
2371 if (comp->pass == PASS_1) {
2372 bool added;
2373 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2374 assert(added);
2375 id_info->kind = ID_INFO_KIND_LOCAL;
2376 scope->num_params = 1;
2377 }
2378
2379 if (scope->kind == SCOPE_LIST_COMP) {
2380 EMIT(build_list, 0);
2381 } else if (scope->kind == SCOPE_DICT_COMP) {
2382 EMIT(build_map, 0);
2383 } else if (scope->kind == SCOPE_SET_COMP) {
2384 EMIT(build_set, 0);
2385 }
2386
Damienb05d7072013-10-05 13:37:10 +01002387 int l_end = comp_next_label(comp);
2388 int l_top = comp_next_label(comp);
Damien4b03e772013-10-05 14:17:09 +01002389 EMIT(load_id, qstr_arg);
Damien429d7192013-10-04 19:53:11 +01002390 EMIT(label_assign, l_top);
2391 EMIT(for_iter, l_end);
2392 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2393 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
2394 EMIT(jump, l_top);
2395 EMIT(label_assign, l_end);
2396 EMIT(for_iter_end);
2397
2398 if (scope->kind == SCOPE_GEN_EXPR) {
2399 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2400 }
2401 EMIT(return_value);
2402 } else {
2403 assert(scope->kind == SCOPE_CLASS);
2404 assert(PY_PARSE_NODE_IS_STRUCT(scope->pn));
2405 py_parse_node_struct_t *pns = (py_parse_node_struct_t*)scope->pn;
2406 assert(PY_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
2407
2408 if (comp->pass == PASS_1) {
2409 bool added;
2410 id_info_t *id_info = scope_find_or_add_id(scope, comp->qstr___class__, &added);
2411 assert(added);
2412 id_info->kind = ID_INFO_KIND_LOCAL;
2413 id_info = scope_find_or_add_id(scope, comp->qstr___locals__, &added);
2414 assert(added);
2415 id_info->kind = ID_INFO_KIND_LOCAL;
2416 id_info->param = true;
2417 scope->num_params = 1; // __locals__ is the parameter
2418 }
2419
Damien4b03e772013-10-05 14:17:09 +01002420 EMIT(load_id, comp->qstr___locals__);
Damien429d7192013-10-04 19:53:11 +01002421 EMIT(store_locals);
Damien4b03e772013-10-05 14:17:09 +01002422 EMIT(load_id, comp->qstr___name__);
2423 EMIT(store_id, comp->qstr___module__);
Damien429d7192013-10-04 19:53:11 +01002424 EMIT(load_const_id, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
Damien4b03e772013-10-05 14:17:09 +01002425 EMIT(store_id, comp->qstr___qualname__);
Damien429d7192013-10-04 19:53:11 +01002426
2427 check_for_doc_string(comp, pns->nodes[2]);
2428 compile_node(comp, pns->nodes[2]); // 2 is class body
2429
2430 id_info_t *id = scope_find(scope, comp->qstr___class__);
2431 assert(id != NULL);
2432 if (id->kind == ID_INFO_KIND_LOCAL) {
2433 EMIT(load_const_tok, PY_TOKEN_KW_NONE);
2434 } else {
2435 EMIT(load_closure, comp->qstr___class__);
2436 }
2437 EMIT(return_value);
2438 }
2439
Damien415eb6f2013-10-05 12:19:06 +01002440 EMIT(end_pass);
Damienb05d7072013-10-05 13:37:10 +01002441
2442 // update maximim number of labels needed
2443 if (comp->next_label > comp->max_num_labels) {
2444 comp->max_num_labels = comp->next_label;
2445 }
Damien429d7192013-10-04 19:53:11 +01002446}
2447
2448void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
2449 // in functions, turn implicit globals into explicit globals
2450 // compute num_locals, and the index of each local
2451 scope->num_locals = 0;
2452 for (int i = 0; i < scope->id_info_len; i++) {
2453 id_info_t *id = &scope->id_info[i];
2454 if (scope->kind == SCOPE_CLASS && id->qstr == comp->qstr___class__) {
2455 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
2456 continue;
2457 }
2458 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
2459 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
2460 }
2461 if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
2462 id->local_num = scope->num_locals;
2463 scope->num_locals += 1;
2464 }
2465 }
2466
2467 // compute flags
2468 //scope->flags = 0; since we set some things in parameters
2469 if (scope->kind != SCOPE_MODULE) {
2470 scope->flags |= SCOPE_FLAG_NEWLOCALS;
2471 }
2472 if (scope->kind == SCOPE_FUNCTION || scope->kind == SCOPE_LAMBDA || scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2473 assert(scope->parent != NULL);
2474 scope->flags |= SCOPE_FLAG_OPTIMISED;
2475
2476 // TODO possibly other ways it can be nested
2477 if (scope->parent->kind == SCOPE_FUNCTION || (scope->parent->kind == SCOPE_CLASS && scope->parent->parent->kind == SCOPE_FUNCTION)) {
2478 scope->flags |= SCOPE_FLAG_NESTED;
2479 }
2480 }
2481 int num_free = 0;
2482 for (int i = 0; i < scope->id_info_len; i++) {
2483 id_info_t *id = &scope->id_info[i];
2484 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
2485 num_free += 1;
2486 }
2487 }
2488 if (num_free == 0) {
2489 scope->flags |= SCOPE_FLAG_NOFREE;
2490 }
2491}
2492
2493void py_compile(py_parse_node_t pn) {
2494 compiler_t *comp = m_new(compiler_t, 1);
2495
2496 comp->qstr___class__ = qstr_from_strn_copy("__class__", 9);
2497 comp->qstr___locals__ = qstr_from_strn_copy("__locals__", 10);
2498 comp->qstr___name__ = qstr_from_strn_copy("__name__", 8);
2499 comp->qstr___module__ = qstr_from_strn_copy("__module__", 10);
2500 comp->qstr___qualname__ = qstr_from_strn_copy("__qualname__", 12);
2501 comp->qstr___doc__ = qstr_from_strn_copy("__doc__", 7);
2502 comp->qstr_assertion_error = qstr_from_strn_copy("AssertionError", 14);
2503
Damienb05d7072013-10-05 13:37:10 +01002504 comp->max_num_labels = 0;
Damien429d7192013-10-04 19:53:11 +01002505 comp->break_label = 0;
2506 comp->continue_label = 0;
2507 comp->except_nest_level = 0;
2508 comp->scope_head = NULL;
2509 comp->scope_cur = NULL;
2510
Damien4b03e772013-10-05 14:17:09 +01002511 emit_pass1_new(&comp->emit, &comp->emit_method_table, comp->qstr___class__);
Damien429d7192013-10-04 19:53:11 +01002512
2513 pn = fold_constants(pn);
2514 scope_new_and_link(comp, SCOPE_MODULE, pn);
2515
2516 for (scope_t *s = comp->scope_head; s != NULL; s = s->next) {
2517 compile_scope(comp, s, PASS_1);
2518 }
2519
2520 for (scope_t *s = comp->scope_head; s != NULL; s = s->next) {
2521 compile_scope_compute_things(comp, s);
2522 }
2523
Damien4b03e772013-10-05 14:17:09 +01002524 emit_cpython_new(&comp->emit, &comp->emit_method_table, comp->max_num_labels);
Damien054848a2013-10-05 13:44:41 +01002525 //emit_bc_new(&comp->emit, &comp->emit_method_table, comp->max_num_labels);
Damien4b03e772013-10-05 14:17:09 +01002526 //emit_x64_new(&comp->emit, &comp->emit_method_table, comp->max_num_labels);
Damienb05d7072013-10-05 13:37:10 +01002527
Damien429d7192013-10-04 19:53:11 +01002528 for (scope_t *s = comp->scope_head; s != NULL; s = s->next) {
2529 compile_scope(comp, s, PASS_2);
2530 compile_scope(comp, s, PASS_3);
2531 }
2532
2533 m_free(comp);
2534}