blob: fe22a90174cb26e1b29ac3b0774309effd0fba70 [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"
Damiend99b0522013-12-21 18:17:45 +00009#include "mpconfig.h"
Damien429d7192013-10-04 19:53:11 +010010#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010011#include "parse.h"
12#include "scope.h"
13#include "compile.h"
Damiend99b0522013-12-21 18:17:45 +000014#include "runtime0.h"
Damien429d7192013-10-04 19:53:11 +010015#include "emit.h"
16
17// TODO need to mangle __attr names
18
Damience89a212013-10-15 22:25:17 +010019#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_THUMB)
20
Damien429d7192013-10-04 19:53:11 +010021typedef enum {
22 PN_none = 0,
23#define DEF_RULE(rule, comp, kind, arg...) PN_##rule,
24#include "grammar.h"
25#undef DEF_RULE
26 PN_maximum_number_of,
27} pn_kind_t;
28
Damien415eb6f2013-10-05 12:19:06 +010029#define EMIT(fun, arg...) (comp->emit_method_table->fun(comp->emit, ##arg))
Damien826005c2013-10-05 23:17:28 +010030#define EMIT_INLINE_ASM(fun, arg...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, ##arg))
Damien429d7192013-10-04 19:53:11 +010031
Damien6cdd3af2013-10-05 18:08:26 +010032#define EMIT_OPT_NONE (0)
33#define EMIT_OPT_BYTE_CODE (1)
34#define EMIT_OPT_NATIVE_PYTHON (2)
Damien7af3d192013-10-07 00:02:49 +010035#define EMIT_OPT_VIPER (3)
36#define EMIT_OPT_ASM_THUMB (4)
Damien6cdd3af2013-10-05 18:08:26 +010037
Damien429d7192013-10-04 19:53:11 +010038typedef struct _compiler_t {
39 qstr qstr___class__;
40 qstr qstr___locals__;
41 qstr qstr___name__;
42 qstr qstr___module__;
43 qstr qstr___qualname__;
44 qstr qstr___doc__;
45 qstr qstr_assertion_error;
Damien6cdd3af2013-10-05 18:08:26 +010046 qstr qstr_micropython;
Damien5ac1b2e2013-10-18 19:58:12 +010047 qstr qstr_byte_code;
Damien6cdd3af2013-10-05 18:08:26 +010048 qstr qstr_native;
Damien7af3d192013-10-07 00:02:49 +010049 qstr qstr_viper;
Damien5bfb7592013-10-05 18:41:24 +010050 qstr qstr_asm_thumb;
Damiend7933892013-11-28 19:12:18 +000051 qstr qstr_range;
Damien429d7192013-10-04 19:53:11 +010052
Damien5ac1b2e2013-10-18 19:58:12 +010053 bool is_repl;
Damien429d7192013-10-04 19:53:11 +010054 pass_kind_t pass;
Damien5ac1b2e2013-10-18 19:58:12 +010055 bool had_error; // try to keep compiler clean from nlr
Damien429d7192013-10-04 19:53:11 +010056
Damienb05d7072013-10-05 13:37:10 +010057 int next_label;
Damienb05d7072013-10-05 13:37:10 +010058
Damien429d7192013-10-04 19:53:11 +010059 int break_label;
60 int continue_label;
61 int except_nest_level;
62
63 int n_arg_keyword;
64 bool have_star_arg;
65 bool have_dbl_star_arg;
66 bool have_bare_star;
67 int param_pass;
68 int param_pass_num_dict_params;
69 int param_pass_num_default_params;
70
71 scope_t *scope_head;
72 scope_t *scope_cur;
73
Damien6cdd3af2013-10-05 18:08:26 +010074 emit_t *emit; // current emitter
75 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010076
77 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
78 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien429d7192013-10-04 19:53:11 +010079} compiler_t;
80
Damiend99b0522013-12-21 18:17:45 +000081mp_parse_node_t fold_constants(mp_parse_node_t pn) {
82 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
83 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
84 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +010085
86 // fold arguments first
87 for (int i = 0; i < n; i++) {
88 pns->nodes[i] = fold_constants(pns->nodes[i]);
89 }
90
Damiend99b0522013-12-21 18:17:45 +000091 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +010092 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +000093 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
94 int arg0 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
95 int arg1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[2]);
96 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien3ef4abb2013-10-12 16:53:13 +010097#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +010098 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +000099 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
Damien0efb3a12013-10-12 16:16:56 +0100100#endif
Damiend99b0522013-12-21 18:17:45 +0000101 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
102 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100103 } else {
104 // shouldn't happen
105 assert(0);
106 }
107 }
108 break;
109
110 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100111 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000112 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
113 machine_int_t arg0 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
114 machine_int_t arg1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[2]);
Damien0efb3a12013-10-12 16:16:56 +0100115 machine_int_t res;
Damiend99b0522013-12-21 18:17:45 +0000116 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100117 res = arg0 + arg1;
Damiend99b0522013-12-21 18:17:45 +0000118 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100119 res = arg0 - arg1;
Damien429d7192013-10-04 19:53:11 +0100120 } else {
121 // shouldn't happen
122 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100123 res = 0;
124 }
Damiend99b0522013-12-21 18:17:45 +0000125 if (MP_FIT_SMALL_INT(res)) {
126 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, res);
Damien429d7192013-10-04 19:53:11 +0100127 }
128 }
129 break;
130
131 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000132 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
133 int arg0 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
134 int arg1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[2]);
135 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien3ef4abb2013-10-12 16:53:13 +0100136#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100137 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000138 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 * arg1);
Damien0efb3a12013-10-12 16:16:56 +0100139#endif
Damiend99b0522013-12-21 18:17:45 +0000140 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien429d7192013-10-04 19:53:11 +0100141 ; // pass
Damiend99b0522013-12-21 18:17:45 +0000142 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien0efb3a12013-10-12 16:16:56 +0100143 // XXX implement this properly as Python's % operator acts differently to C's
Damiend99b0522013-12-21 18:17:45 +0000144 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 % arg1);
145 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Damien0efb3a12013-10-12 16:16:56 +0100146 // XXX implement this properly as Python's // operator acts differently to C's
Damiend99b0522013-12-21 18:17:45 +0000147 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 / arg1);
Damien429d7192013-10-04 19:53:11 +0100148 } else {
149 // shouldn't happen
150 assert(0);
151 }
152 }
153 break;
154
155 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000156 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
157 machine_int_t arg = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
158 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
159 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
160 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
161 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
162 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
163 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100164 } else {
165 // shouldn't happen
166 assert(0);
167 }
168 }
169 break;
170
Damien3ef4abb2013-10-12 16:53:13 +0100171#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +0100172 case PN_power:
Damien0efb3a12013-10-12 16:16:56 +0100173 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000174 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_NULL(pns->nodes[1]) && !MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
175 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
176 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
177 int power = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100178 if (power >= 0) {
179 int ans = 1;
Damiend99b0522013-12-21 18:17:45 +0000180 int base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100181 for (; power > 0; power--) {
182 ans *= base;
183 }
Damiend99b0522013-12-21 18:17:45 +0000184 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100185 }
186 }
187 }
188 break;
Damien0efb3a12013-10-12 16:16:56 +0100189#endif
Damien429d7192013-10-04 19:53:11 +0100190 }
191 }
192
193 return pn;
194}
195
Damiend99b0522013-12-21 18:17:45 +0000196void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100197
Damienb05d7072013-10-05 13:37:10 +0100198static int comp_next_label(compiler_t *comp) {
199 return comp->next_label++;
200}
201
Damiend99b0522013-12-21 18:17:45 +0000202static scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) {
Damien5ac1b2e2013-10-18 19:58:12 +0100203 scope_t *scope = scope_new(kind, pn, rt_get_unique_code_id(kind == SCOPE_MODULE), emit_options);
Damien429d7192013-10-04 19:53:11 +0100204 scope->parent = comp->scope_cur;
205 scope->next = NULL;
206 if (comp->scope_head == NULL) {
207 comp->scope_head = scope;
208 } else {
209 scope_t *s = comp->scope_head;
210 while (s->next != NULL) {
211 s = s->next;
212 }
213 s->next = scope;
214 }
215 return scope;
216}
217
Damiend99b0522013-12-21 18:17:45 +0000218static int list_len(mp_parse_node_t pn, int pn_kind) {
219 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100220 return 0;
Damiend99b0522013-12-21 18:17:45 +0000221 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100222 return 1;
223 } else {
Damiend99b0522013-12-21 18:17:45 +0000224 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
225 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100226 return 1;
227 } else {
Damiend99b0522013-12-21 18:17:45 +0000228 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100229 }
230 }
231}
232
Damiend99b0522013-12-21 18:17:45 +0000233static void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, int pn_list_kind, void (*f)(compiler_t*, mp_parse_node_t)) {
234 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
235 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
236 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100237 for (int i = 0; i < num_nodes; i++) {
238 f(comp, pns->nodes[i]);
239 }
Damiend99b0522013-12-21 18:17:45 +0000240 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100241 f(comp, pn);
242 }
243}
244
Damiend99b0522013-12-21 18:17:45 +0000245static int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
246 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100247 *nodes = NULL;
248 return 0;
Damiend99b0522013-12-21 18:17:45 +0000249 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100250 *nodes = pn;
251 return 1;
252 } else {
Damiend99b0522013-12-21 18:17:45 +0000253 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
254 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100255 *nodes = pn;
256 return 1;
257 } else {
258 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000259 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100260 }
261 }
262}
263
Damiend99b0522013-12-21 18:17:45 +0000264void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100265}
266
Damiend99b0522013-12-21 18:17:45 +0000267void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
268 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100269 for (int i = 0; i < num_nodes; i++) {
270 compile_node(comp, pns->nodes[i]);
271 }
272}
273
Damien3ef4abb2013-10-12 16:53:13 +0100274#if MICROPY_EMIT_CPYTHON
Damiend99b0522013-12-21 18:17:45 +0000275static bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
276 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100277 return false;
278 }
Damiend99b0522013-12-21 18:17:45 +0000279 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100280 return false;
281 }
282 return true;
283}
284
Damien02f89412013-12-12 15:13:36 +0000285static void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
286 const char *str = qstr_str(qstr);
287 int len = strlen(str);
288 bool has_single_quote = false;
289 bool has_double_quote = false;
290 for (int i = 0; i < len; i++) {
291 if (str[i] == '\'') {
292 has_single_quote = true;
293 } else if (str[i] == '"') {
294 has_double_quote = true;
295 }
296 }
297 if (bytes) {
298 vstr_printf(vstr, "b");
299 }
300 bool quote_single = false;
301 if (has_single_quote && !has_double_quote) {
302 vstr_printf(vstr, "\"");
303 } else {
304 quote_single = true;
305 vstr_printf(vstr, "'");
306 }
307 for (int i = 0; i < len; i++) {
308 if (str[i] == '\n') {
309 vstr_printf(vstr, "\\n");
310 } else if (str[i] == '\\') {
311 vstr_printf(vstr, "\\\\");
312 } else if (str[i] == '\'' && quote_single) {
313 vstr_printf(vstr, "\\'");
314 } else {
315 vstr_printf(vstr, "%c", str[i]);
316 }
317 }
318 if (has_single_quote && !has_double_quote) {
319 vstr_printf(vstr, "\"");
320 } else {
321 vstr_printf(vstr, "'");
322 }
323}
324
Damiend99b0522013-12-21 18:17:45 +0000325static void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
326 assert(MP_PARSE_NODE_IS_LEAF(pn));
327 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
328 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
329 case MP_PARSE_NODE_ID: assert(0);
330 case MP_PARSE_NODE_SMALL_INT: vstr_printf(vstr, "%d", arg); break;
331 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
332 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
333 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
334 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
335 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100336 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000337 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
338 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
339 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien429d7192013-10-04 19:53:11 +0100340 default: assert(0);
341 }
342 break;
343 default: assert(0);
344 }
345}
346
Damiend99b0522013-12-21 18:17:45 +0000347static void cpython_c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien429d7192013-10-04 19:53:11 +0100348 int n = 0;
349 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000350 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100351 }
352 int total = n;
353 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000354 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100355 total += 1;
Damien3a205172013-10-12 15:01:56 +0100356 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100357 is_const = false;
358 }
359 }
360 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100361 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100362 is_const = false;
363 break;
364 }
365 }
366 if (total > 0 && is_const) {
367 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000368 vstr_t *vstr = vstr_new();
369 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000370 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000371 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100372 need_comma = true;
373 }
374 for (int i = 0; i < n; i++) {
375 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000376 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100377 }
Damien02f89412013-12-12 15:13:36 +0000378 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100379 need_comma = true;
380 }
381 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000382 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100383 } else {
Damien02f89412013-12-12 15:13:36 +0000384 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100385 }
Damien02f89412013-12-12 15:13:36 +0000386 EMIT(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000387 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100388 } else {
Damiend99b0522013-12-21 18:17:45 +0000389 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100390 compile_node(comp, pn);
391 }
392 for (int i = 0; i < n; i++) {
393 compile_node(comp, pns_list->nodes[i]);
394 }
395 EMIT(build_tuple, total);
396 }
397}
Damien3a205172013-10-12 15:01:56 +0100398#endif
399
400// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damiend99b0522013-12-21 18:17:45 +0000401void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100402#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100403 cpython_c_tuple(comp, pn, pns_list);
404#else
405 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000406 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100407 compile_node(comp, pn);
408 total += 1;
409 }
410 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000411 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100412 for (int i = 0; i < n; i++) {
413 compile_node(comp, pns_list->nodes[i]);
414 }
415 total += n;
416 }
417 EMIT(build_tuple, total);
418#endif
419}
Damien429d7192013-10-04 19:53:11 +0100420
Damiend99b0522013-12-21 18:17:45 +0000421void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100422 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000423 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100424}
425
Damiend99b0522013-12-21 18:17:45 +0000426static bool node_is_const_false(mp_parse_node_t pn) {
427 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
428 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_ARG(pn) == 1);
Damien429d7192013-10-04 19:53:11 +0100429}
430
Damiend99b0522013-12-21 18:17:45 +0000431static bool node_is_const_true(mp_parse_node_t pn) {
432 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE) || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_ARG(pn) == 1);
Damien429d7192013-10-04 19:53:11 +0100433}
434
Damien3ef4abb2013-10-12 16:53:13 +0100435#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100436// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Damiend99b0522013-12-21 18:17:45 +0000437static void cpython_c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label, bool is_nested) {
Damien429d7192013-10-04 19:53:11 +0100438 if (node_is_const_false(pn)) {
439 if (jump_if == false) {
440 EMIT(jump, label);
441 }
442 return;
443 } else if (node_is_const_true(pn)) {
444 if (jump_if == true) {
445 EMIT(jump, label);
446 }
447 return;
Damiend99b0522013-12-21 18:17:45 +0000448 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
449 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
450 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
451 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100452 if (jump_if == false) {
Damienb05d7072013-10-05 13:37:10 +0100453 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100454 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100455 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100456 }
Damien3a205172013-10-12 15:01:56 +0100457 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100458 EMIT(label_assign, label2);
459 } else {
460 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100461 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100462 }
463 }
464 return;
Damiend99b0522013-12-21 18:17:45 +0000465 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100466 if (jump_if == false) {
467 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100468 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100469 }
470 } else {
Damienb05d7072013-10-05 13:37:10 +0100471 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100472 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100473 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100474 }
Damien3a205172013-10-12 15:01:56 +0100475 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100476 EMIT(label_assign, label2);
477 }
478 return;
Damiend99b0522013-12-21 18:17:45 +0000479 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100480 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100481 return;
482 }
483 }
484
485 // nothing special, fall back to default compiling for node and jump
486 compile_node(comp, pn);
487 if (jump_if == false) {
488 EMIT(pop_jump_if_false, label);
489 } else {
490 EMIT(pop_jump_if_true, label);
491 }
492}
Damien3a205172013-10-12 15:01:56 +0100493#endif
Damien429d7192013-10-04 19:53:11 +0100494
Damiend99b0522013-12-21 18:17:45 +0000495static void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100496#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100497 cpython_c_if_cond(comp, pn, jump_if, label, false);
498#else
499 if (node_is_const_false(pn)) {
500 if (jump_if == false) {
501 EMIT(jump, label);
502 }
503 return;
504 } else if (node_is_const_true(pn)) {
505 if (jump_if == true) {
506 EMIT(jump, label);
507 }
508 return;
Damiend99b0522013-12-21 18:17:45 +0000509 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
510 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
511 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
512 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100513 if (jump_if == false) {
514 int label2 = comp_next_label(comp);
515 for (int i = 0; i < n - 1; i++) {
516 c_if_cond(comp, pns->nodes[i], true, label2);
517 }
518 c_if_cond(comp, pns->nodes[n - 1], false, label);
519 EMIT(label_assign, label2);
520 } else {
521 for (int i = 0; i < n; i++) {
522 c_if_cond(comp, pns->nodes[i], true, label);
523 }
524 }
525 return;
Damiend99b0522013-12-21 18:17:45 +0000526 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100527 if (jump_if == false) {
528 for (int i = 0; i < n; i++) {
529 c_if_cond(comp, pns->nodes[i], false, label);
530 }
531 } else {
532 int label2 = comp_next_label(comp);
533 for (int i = 0; i < n - 1; i++) {
534 c_if_cond(comp, pns->nodes[i], false, label2);
535 }
536 c_if_cond(comp, pns->nodes[n - 1], true, label);
537 EMIT(label_assign, label2);
538 }
539 return;
Damiend99b0522013-12-21 18:17:45 +0000540 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100541 c_if_cond(comp, pns->nodes[0], !jump_if, label);
542 return;
543 }
544 }
545
546 // nothing special, fall back to default compiling for node and jump
547 compile_node(comp, pn);
548 if (jump_if == false) {
549 EMIT(pop_jump_if_false, label);
550 } else {
551 EMIT(pop_jump_if_true, label);
552 }
553#endif
Damien429d7192013-10-04 19:53:11 +0100554}
555
556typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000557void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100558
Damiend99b0522013-12-21 18:17:45 +0000559void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100560 if (assign_kind != ASSIGN_AUG_STORE) {
561 compile_node(comp, pns->nodes[0]);
562 }
563
Damiend99b0522013-12-21 18:17:45 +0000564 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
565 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
566 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
567 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100568 if (assign_kind != ASSIGN_AUG_STORE) {
569 for (int i = 0; i < n - 1; i++) {
570 compile_node(comp, pns1->nodes[i]);
571 }
572 }
Damiend99b0522013-12-21 18:17:45 +0000573 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
574 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100575 }
Damiend99b0522013-12-21 18:17:45 +0000576 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien429d7192013-10-04 19:53:11 +0100577 printf("SyntaxError: can't assign to function call\n");
578 return;
Damiend99b0522013-12-21 18:17:45 +0000579 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100580 if (assign_kind == ASSIGN_AUG_STORE) {
581 EMIT(rot_three);
582 EMIT(store_subscr);
583 } else {
584 compile_node(comp, pns1->nodes[0]);
585 if (assign_kind == ASSIGN_AUG_LOAD) {
586 EMIT(dup_top_two);
587 EMIT(binary_op, RT_BINARY_OP_SUBSCR);
588 } else {
589 EMIT(store_subscr);
590 }
591 }
Damiend99b0522013-12-21 18:17:45 +0000592 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
593 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100594 if (assign_kind == ASSIGN_AUG_LOAD) {
595 EMIT(dup_top);
Damiend99b0522013-12-21 18:17:45 +0000596 EMIT(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100597 } else {
598 if (assign_kind == ASSIGN_AUG_STORE) {
599 EMIT(rot_two);
600 }
Damiend99b0522013-12-21 18:17:45 +0000601 EMIT(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100602 }
603 } else {
604 // shouldn't happen
605 assert(0);
606 }
607 } else {
608 // shouldn't happen
609 assert(0);
610 }
611
Damiend99b0522013-12-21 18:17:45 +0000612 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +0100613 // SyntaxError, cannot assign
614 assert(0);
615 }
616}
617
Damiend99b0522013-12-21 18:17:45 +0000618void c_assign_tuple(compiler_t *comp, int n, mp_parse_node_t *nodes) {
Damien429d7192013-10-04 19:53:11 +0100619 assert(n >= 0);
620 int have_star_index = -1;
621 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000622 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100623 if (have_star_index < 0) {
624 EMIT(unpack_ex, i, n - i - 1);
625 have_star_index = i;
626 } else {
627 printf("SyntaxError: two starred expressions in assignment\n");
628 return;
629 }
630 }
631 }
632 if (have_star_index < 0) {
633 EMIT(unpack_sequence, n);
634 }
635 for (int i = 0; i < n; i++) {
636 if (i == have_star_index) {
Damiend99b0522013-12-21 18:17:45 +0000637 c_assign(comp, ((mp_parse_node_struct_t*)nodes[i])->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100638 } else {
639 c_assign(comp, nodes[i], ASSIGN_STORE);
640 }
641 }
642}
643
644// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000645void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100646 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000647 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100648 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000649 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
650 if (MP_PARSE_NODE_IS_ID(pn)) {
651 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100652 switch (assign_kind) {
653 case ASSIGN_STORE:
654 case ASSIGN_AUG_STORE:
Damien4b03e772013-10-05 14:17:09 +0100655 EMIT(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100656 break;
657 case ASSIGN_AUG_LOAD:
Damien4b03e772013-10-05 14:17:09 +0100658 EMIT(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100659 break;
660 }
661 } else {
662 printf("SyntaxError: can't assign to literal\n");
663 return;
664 }
665 } else {
Damiend99b0522013-12-21 18:17:45 +0000666 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
667 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100668 case PN_power:
669 // lhs is an index or attribute
670 c_assign_power(comp, pns, assign_kind);
671 break;
672
673 case PN_testlist_star_expr:
674 case PN_exprlist:
675 // lhs is a tuple
676 if (assign_kind != ASSIGN_STORE) {
677 goto bad_aug;
678 }
Damiend99b0522013-12-21 18:17:45 +0000679 c_assign_tuple(comp, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100680 break;
681
682 case PN_atom_paren:
683 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000684 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100685 // empty tuple
686 printf("SyntaxError: can't assign to ()\n");
687 return;
Damiend99b0522013-12-21 18:17:45 +0000688 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
689 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100690 goto testlist_comp;
691 } else {
692 // parenthesis around 1 item, is just that item
693 pn = pns->nodes[0];
694 goto tail_recursion;
695 }
696 break;
697
698 case PN_atom_bracket:
699 // lhs is something in brackets
700 if (assign_kind != ASSIGN_STORE) {
701 goto bad_aug;
702 }
Damiend99b0522013-12-21 18:17:45 +0000703 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100704 // empty list, assignment allowed
705 c_assign_tuple(comp, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000706 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
707 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100708 goto testlist_comp;
709 } else {
710 // brackets around 1 item
711 c_assign_tuple(comp, 1, &pns->nodes[0]);
712 }
713 break;
714
715 default:
Damiend99b0522013-12-21 18:17:45 +0000716 printf("unknown assign, %u\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien429d7192013-10-04 19:53:11 +0100717 assert(0);
718 }
719 return;
720
721 testlist_comp:
722 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000723 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
724 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
725 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100726 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000727 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100728 c_assign_tuple(comp, 1, &pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000729 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100730 // sequence of many items
731 // TODO call c_assign_tuple instead
Damiend99b0522013-12-21 18:17:45 +0000732 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
Damien429d7192013-10-04 19:53:11 +0100733 EMIT(unpack_sequence, 1 + n);
734 c_assign(comp, pns->nodes[0], ASSIGN_STORE);
735 for (int i = 0; i < n; i++) {
736 c_assign(comp, pns2->nodes[i], ASSIGN_STORE);
737 }
Damiend99b0522013-12-21 18:17:45 +0000738 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +0100739 // TODO not implemented
740 assert(0);
741 } else {
742 // sequence with 2 items
743 goto sequence_with_2_items;
744 }
745 } else {
746 // sequence with 2 items
747 sequence_with_2_items:
748 c_assign_tuple(comp, 2, pns->nodes);
749 }
750 return;
751 }
752 return;
753
754 bad_aug:
755 printf("SyntaxError: illegal expression for augmented assignment\n");
756}
757
758// stuff for lambda and comprehensions and generators
759void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_dict_params, int n_default_params) {
760 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000761 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100762 int nfree = 0;
763 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000764 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
765 id_info_t *id = &comp->scope_cur->id_info[i];
766 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
767 for (int j = 0; j < this_scope->id_info_len; j++) {
768 id_info_t *id2 = &this_scope->id_info[j];
769 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000770#if MICROPY_EMIT_CPYTHON
Damien318aec62013-12-10 18:28:17 +0000771 EMIT(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000772#else
773 // in Micro Python we load closures using LOAD_FAST
774 EMIT(load_fast, id->qstr, id->local_num);
775#endif
Damien318aec62013-12-10 18:28:17 +0000776 nfree += 1;
777 }
778 }
Damien429d7192013-10-04 19:53:11 +0100779 }
780 }
781 }
782 if (nfree > 0) {
783 EMIT(build_tuple, nfree);
784 }
785
786 // make the function/closure
787 if (nfree == 0) {
788 EMIT(make_function, this_scope, n_dict_params, n_default_params);
789 } else {
790 EMIT(make_closure, this_scope, n_dict_params, n_default_params);
791 }
792}
793
Damiend99b0522013-12-21 18:17:45 +0000794void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
795 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
796 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
797 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +0100798 // this parameter has a default value
799 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
800 if (comp->have_bare_star) {
801 comp->param_pass_num_dict_params += 1;
802 if (comp->param_pass == 1) {
Damiend99b0522013-12-21 18:17:45 +0000803 EMIT(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100804 compile_node(comp, pns->nodes[2]);
805 }
806 } else {
807 comp->param_pass_num_default_params += 1;
808 if (comp->param_pass == 2) {
809 compile_node(comp, pns->nodes[2]);
810 }
811 }
812 }
Damiend99b0522013-12-21 18:17:45 +0000813 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
814 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
815 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100816 // bare star
817 comp->have_bare_star = true;
818 }
819 }
820}
821
822// leaves function object on stack
823// returns function name
Damiend99b0522013-12-21 18:17:45 +0000824qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100825 if (comp->pass == PASS_1) {
826 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000827 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100828 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000829 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100830 }
831
832 // save variables (probably don't need to do this, since we can't have nested definitions..?)
833 bool old_have_bare_star = comp->have_bare_star;
834 int old_param_pass = comp->param_pass;
835 int old_param_pass_num_dict_params = comp->param_pass_num_dict_params;
836 int old_param_pass_num_default_params = comp->param_pass_num_default_params;
837
838 // compile default parameters
839 comp->have_bare_star = false;
840 comp->param_pass = 1; // pass 1 does any default parameters after bare star
841 comp->param_pass_num_dict_params = 0;
842 comp->param_pass_num_default_params = 0;
843 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
844 comp->have_bare_star = false;
845 comp->param_pass = 2; // pass 2 does any default parameters before bare star
846 comp->param_pass_num_dict_params = 0;
847 comp->param_pass_num_default_params = 0;
848 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
849
850 // get the scope for this function
851 scope_t *fscope = (scope_t*)pns->nodes[4];
852
853 // make the function
854 close_over_variables_etc(comp, fscope, comp->param_pass_num_dict_params, comp->param_pass_num_default_params);
855
856 // restore variables
857 comp->have_bare_star = old_have_bare_star;
858 comp->param_pass = old_param_pass;
859 comp->param_pass_num_dict_params = old_param_pass_num_dict_params;
860 comp->param_pass_num_default_params = old_param_pass_num_default_params;
861
862 // return its name (the 'f' in "def f(...):")
863 return fscope->simple_name;
864}
865
866// leaves class object on stack
867// returns class name
Damiend99b0522013-12-21 18:17:45 +0000868qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100869 if (comp->pass == PASS_1) {
870 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000871 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100872 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000873 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100874 }
875
876 EMIT(load_build_class);
877
878 // scope for this class
879 scope_t *cscope = (scope_t*)pns->nodes[3];
880
881 // compile the class
882 close_over_variables_etc(comp, cscope, 0, 0);
883
884 // get its name
885 EMIT(load_const_id, cscope->simple_name);
886
887 // nodes[1] has parent classes, if any
Damiend99b0522013-12-21 18:17:45 +0000888 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +0100889 // no parent classes
890 EMIT(call_function, 2, 0, false, false);
891 } else {
892 // have a parent class or classes
893 // TODO what if we have, eg, *a or **a in the parent list?
894 compile_node(comp, pns->nodes[1]);
895 EMIT(call_function, 2 + list_len(pns->nodes[1], PN_arglist), 0, false, false);
896 }
897
898 // return its name (the 'C' in class C(...):")
899 return cscope->simple_name;
900}
901
Damien6cdd3af2013-10-05 18:08:26 +0100902// returns true if it was a built-in decorator (even if the built-in had an error)
Damiend99b0522013-12-21 18:17:45 +0000903static bool compile_built_in_decorator(compiler_t *comp, int name_len, mp_parse_node_t *name_nodes, uint *emit_options) {
904 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != comp->qstr_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100905 return false;
906 }
907
908 if (name_len != 2) {
909 printf("SyntaxError: invalid micropython decorator\n");
910 return true;
911 }
912
Damiend99b0522013-12-21 18:17:45 +0000913 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien5ac1b2e2013-10-18 19:58:12 +0100914 if (attr == comp->qstr_byte_code) {
915 *emit_options = EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +0100916#if MICROPY_EMIT_NATIVE
917 } else if (attr == comp->qstr_native) {
Damien6cdd3af2013-10-05 18:08:26 +0100918 *emit_options = EMIT_OPT_NATIVE_PYTHON;
Damien7af3d192013-10-07 00:02:49 +0100919 } else if (attr == comp->qstr_viper) {
920 *emit_options = EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100921#endif
Damien3ef4abb2013-10-12 16:53:13 +0100922#if MICROPY_EMIT_INLINE_THUMB
Damien5bfb7592013-10-05 18:41:24 +0100923 } else if (attr == comp->qstr_asm_thumb) {
924 *emit_options = EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +0100925#endif
Damien6cdd3af2013-10-05 18:08:26 +0100926 } else {
Damience89a212013-10-15 22:25:17 +0100927 printf("SyntaxError: invalid micropython decorator '%s'\n", qstr_str(attr));
Damien6cdd3af2013-10-05 18:08:26 +0100928 }
929
930 return true;
931}
932
Damiend99b0522013-12-21 18:17:45 +0000933void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100934 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +0000935 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +0100936 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
937
Damien6cdd3af2013-10-05 18:08:26 +0100938 // inherit emit options for this function/class definition
939 uint emit_options = comp->scope_cur->emit_options;
940
941 // compile each decorator
942 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +0100943 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000944 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
945 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +0100946
947 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +0000948 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +0100949 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
950
951 // check for built-in decorators
952 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
953 // this was a built-in
954 num_built_in_decorators += 1;
955
956 } else {
957 // not a built-in, compile normally
958
959 // compile the decorator function
960 compile_node(comp, name_nodes[0]);
961 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +0000962 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
963 EMIT(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +0100964 }
965
966 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +0000967 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +0100968 // call the decorator function with the arguments in nodes[1]
969 compile_node(comp, pns_decorator->nodes[1]);
970 }
Damien429d7192013-10-04 19:53:11 +0100971 }
972 }
973
974 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +0000975 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +0100976 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +0000977 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +0100978 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +0000979 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +0100980 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +0100981 } else {
982 // shouldn't happen
983 assert(0);
984 }
985
986 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +0100987 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien429d7192013-10-04 19:53:11 +0100988 EMIT(call_function, 1, 0, false, false);
989 }
990
991 // store func/class object into name
Damien4b03e772013-10-05 14:17:09 +0100992 EMIT(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +0100993}
994
Damiend99b0522013-12-21 18:17:45 +0000995void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +0100996 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +0100997 // store function object into function name
Damien4b03e772013-10-05 14:17:09 +0100998 EMIT(store_id, fname);
Damien429d7192013-10-04 19:53:11 +0100999}
1000
Damiend99b0522013-12-21 18:17:45 +00001001void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1002 if (MP_PARSE_NODE_IS_ID(pn)) {
1003 EMIT(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
1004 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1005 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001006
1007 compile_node(comp, pns->nodes[0]); // base of the power node
1008
Damiend99b0522013-12-21 18:17:45 +00001009 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1010 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1011 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1012 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001013 for (int i = 0; i < n - 1; i++) {
1014 compile_node(comp, pns1->nodes[i]);
1015 }
Damiend99b0522013-12-21 18:17:45 +00001016 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1017 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001018 }
Damiend99b0522013-12-21 18:17:45 +00001019 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien429d7192013-10-04 19:53:11 +01001020 // SyntaxError: can't delete a function call
1021 assert(0);
Damiend99b0522013-12-21 18:17:45 +00001022 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001023 compile_node(comp, pns1->nodes[0]);
1024 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001025 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1026 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
1027 EMIT(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001028 } else {
1029 // shouldn't happen
1030 assert(0);
1031 }
1032 } else {
1033 // shouldn't happen
1034 assert(0);
1035 }
1036
Damiend99b0522013-12-21 18:17:45 +00001037 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001038 // SyntaxError, cannot delete
1039 assert(0);
1040 }
Damiend99b0522013-12-21 18:17:45 +00001041 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1042 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1043 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1044 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001045 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1046
Damiend99b0522013-12-21 18:17:45 +00001047 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1048 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1049 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001050 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001051 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001052 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001053 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001054 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001055 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001056 c_del_stmt(comp, pns->nodes[0]);
1057 for (int i = 0; i < n; i++) {
1058 c_del_stmt(comp, pns1->nodes[i]);
1059 }
Damiend99b0522013-12-21 18:17:45 +00001060 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001061 // TODO not implemented; can't del comprehension?
1062 assert(0);
1063 } else {
1064 // sequence with 2 items
1065 goto sequence_with_2_items;
1066 }
1067 } else {
1068 // sequence with 2 items
1069 sequence_with_2_items:
1070 c_del_stmt(comp, pns->nodes[0]);
1071 c_del_stmt(comp, pns->nodes[1]);
1072 }
1073 } else {
1074 // tuple with 1 element
1075 c_del_stmt(comp, pn);
1076 }
1077 } else {
1078 // not implemented
1079 assert(0);
1080 }
1081}
1082
Damiend99b0522013-12-21 18:17:45 +00001083void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001084 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1085}
1086
Damiend99b0522013-12-21 18:17:45 +00001087void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001088 if (comp->break_label == 0) {
1089 printf("ERROR: cannot break from here\n");
1090 }
1091 EMIT(break_loop, comp->break_label);
1092}
1093
Damiend99b0522013-12-21 18:17:45 +00001094void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001095 if (comp->continue_label == 0) {
1096 printf("ERROR: cannot continue from here\n");
1097 }
1098 if (comp->except_nest_level > 0) {
1099 EMIT(continue_loop, comp->continue_label);
1100 } else {
1101 EMIT(jump, comp->continue_label);
1102 }
1103}
1104
Damiend99b0522013-12-21 18:17:45 +00001105void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001106 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
1107 printf("SyntaxError: 'return' outside function\n");
1108 comp->had_error = true;
1109 return;
1110 }
Damiend99b0522013-12-21 18:17:45 +00001111 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001112 // no argument to 'return', so return None
Damiend99b0522013-12-21 18:17:45 +00001113 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
1114 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001115 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001116 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1117 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns_test_if_expr->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001118
Damienb05d7072013-10-05 13:37:10 +01001119 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001120 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1121 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1122 EMIT(return_value);
1123 EMIT(label_assign, l_fail);
1124 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1125 } else {
1126 compile_node(comp, pns->nodes[0]);
1127 }
1128 EMIT(return_value);
1129}
1130
Damiend99b0522013-12-21 18:17:45 +00001131void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001132 compile_node(comp, pns->nodes[0]);
1133 EMIT(pop_top);
1134}
1135
Damiend99b0522013-12-21 18:17:45 +00001136void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1137 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001138 // raise
1139 EMIT(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001140 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001141 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001142 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001143 compile_node(comp, pns->nodes[0]);
1144 compile_node(comp, pns->nodes[1]);
1145 EMIT(raise_varargs, 2);
1146 } else {
1147 // raise x
1148 compile_node(comp, pns->nodes[0]);
1149 EMIT(raise_varargs, 1);
1150 }
1151}
1152
1153// q1 holds the base, q2 the full name
1154// eg a -> q1=q2=a
1155// a.b.c -> q1=a, q2=a.b.c
Damiend99b0522013-12-21 18:17:45 +00001156void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q1, qstr *q2) {
Damien429d7192013-10-04 19:53:11 +01001157 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001158 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1159 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001160 // a name of the form x as y; unwrap it
Damiend99b0522013-12-21 18:17:45 +00001161 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001162 pn = pns->nodes[0];
1163 is_as = true;
1164 }
Damiend99b0522013-12-21 18:17:45 +00001165 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001166 // just a simple name
Damiend99b0522013-12-21 18:17:45 +00001167 *q2 = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001168 if (!is_as) {
1169 *q1 = *q2;
1170 }
1171 EMIT(import_name, *q2);
Damiend99b0522013-12-21 18:17:45 +00001172 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1173 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1174 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001175 // a name of the form a.b.c
1176 if (!is_as) {
Damiend99b0522013-12-21 18:17:45 +00001177 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001178 }
Damiend99b0522013-12-21 18:17:45 +00001179 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001180 int len = n - 1;
1181 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001182 len += strlen(qstr_str(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])));
Damien429d7192013-10-04 19:53:11 +01001183 }
1184 char *str = m_new(char, len + 1);
1185 str[0] = 0;
1186 for (int i = 0; i < n; i++) {
1187 if (i > 0) {
1188 strcat(str, ".");
1189 }
Damiend99b0522013-12-21 18:17:45 +00001190 strcat(str, qstr_str(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])));
Damien429d7192013-10-04 19:53:11 +01001191 }
Damien732407f2013-12-29 19:33:23 +00001192 *q2 = qstr_from_str_take(str, len + 1);
Damien429d7192013-10-04 19:53:11 +01001193 EMIT(import_name, *q2);
1194 if (is_as) {
1195 for (int i = 1; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001196 EMIT(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001197 }
1198 }
1199 } else {
1200 // TODO not implemented
1201 assert(0);
1202 }
1203 } else {
1204 // TODO not implemented
1205 assert(0);
1206 }
1207}
1208
Damiend99b0522013-12-21 18:17:45 +00001209void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01001210 EMIT(load_const_small_int, 0); // ??
Damiend99b0522013-12-21 18:17:45 +00001211 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001212 qstr q1, q2;
1213 do_import_name(comp, pn, &q1, &q2);
Damien4b03e772013-10-05 14:17:09 +01001214 EMIT(store_id, q1);
Damien429d7192013-10-04 19:53:11 +01001215}
1216
Damiend99b0522013-12-21 18:17:45 +00001217void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001218 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1219}
1220
Damiend99b0522013-12-21 18:17:45 +00001221void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
1222 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damiendb4c3612013-12-10 17:27:24 +00001223 EMIT(load_const_small_int, 0); // level 0 for __import__
1224
1225 // build the "fromlist" tuple
1226#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001227 EMIT(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001228#else
1229 EMIT(load_const_str, qstr_from_str_static("*"), false);
1230 EMIT(build_tuple, 1);
1231#endif
1232
1233 // do the import
Damien429d7192013-10-04 19:53:11 +01001234 qstr dummy_q, id1;
1235 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1236 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001237
Damien429d7192013-10-04 19:53:11 +01001238 } else {
Damiendb4c3612013-12-10 17:27:24 +00001239 EMIT(load_const_small_int, 0); // level 0 for __import__
1240
1241 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001242 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001243 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001244#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001245 {
1246 vstr_t *vstr = vstr_new();
1247 vstr_printf(vstr, "(");
1248 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001249 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1250 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1251 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001252 if (i > 0) {
1253 vstr_printf(vstr, ", ");
1254 }
1255 vstr_printf(vstr, "'");
1256 vstr_printf(vstr, qstr_str(id2));
1257 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001258 }
Damien02f89412013-12-12 15:13:36 +00001259 if (n == 1) {
1260 vstr_printf(vstr, ",");
1261 }
1262 vstr_printf(vstr, ")");
Damien02f89412013-12-12 15:13:36 +00001263 EMIT(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001264 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001265 }
Damiendb4c3612013-12-10 17:27:24 +00001266#else
1267 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001268 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1269 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1270 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damiendb4c3612013-12-10 17:27:24 +00001271 EMIT(load_const_str, id2, false);
1272 }
1273 EMIT(build_tuple, n);
1274#endif
1275
1276 // do the import
Damien429d7192013-10-04 19:53:11 +01001277 qstr dummy_q, id1;
1278 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1279 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001280 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1281 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1282 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien429d7192013-10-04 19:53:11 +01001283 EMIT(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001284 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien4b03e772013-10-05 14:17:09 +01001285 EMIT(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001286 } else {
Damiend99b0522013-12-21 18:17:45 +00001287 EMIT(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001288 }
1289 }
1290 EMIT(pop_top);
1291 }
1292}
1293
Damiend99b0522013-12-21 18:17:45 +00001294void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001295 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001296 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1297 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001298 } else {
Damiend99b0522013-12-21 18:17:45 +00001299 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1300 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001301 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001302 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001303 }
Damien429d7192013-10-04 19:53:11 +01001304 }
1305 }
1306}
1307
Damiend99b0522013-12-21 18:17:45 +00001308void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001309 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001310 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1311 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001312 } else {
Damiend99b0522013-12-21 18:17:45 +00001313 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1314 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001315 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001316 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001317 }
Damien429d7192013-10-04 19:53:11 +01001318 }
1319 }
1320}
1321
Damiend99b0522013-12-21 18:17:45 +00001322void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001323 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001324 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien4b03e772013-10-05 14:17:09 +01001325 EMIT(load_id, comp->qstr_assertion_error);
Damiend99b0522013-12-21 18:17:45 +00001326 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001327 // assertion message
1328 compile_node(comp, pns->nodes[1]);
1329 EMIT(call_function, 1, 0, false, false);
1330 }
1331 EMIT(raise_varargs, 1);
1332 EMIT(label_assign, l_end);
1333}
1334
Damiend99b0522013-12-21 18:17:45 +00001335void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001336 // TODO proper and/or short circuiting
1337
Damienb05d7072013-10-05 13:37:10 +01001338 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001339
Damienb05d7072013-10-05 13:37:10 +01001340 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001341 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1342
1343 compile_node(comp, pns->nodes[1]); // if block
Damiend99b0522013-12-21 18:17:45 +00001344 //if (!(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3]))) { // optimisation; doesn't align with CPython
Damien429d7192013-10-04 19:53:11 +01001345 // jump over elif/else blocks if they exist
Damien415eb6f2013-10-05 12:19:06 +01001346 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien429d7192013-10-04 19:53:11 +01001347 EMIT(jump, l_end);
1348 }
1349 //}
1350 EMIT(label_assign, l_fail);
1351
Damiend99b0522013-12-21 18:17:45 +00001352 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001353 // compile elif blocks
1354
Damiend99b0522013-12-21 18:17:45 +00001355 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001356
Damiend99b0522013-12-21 18:17:45 +00001357 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001358 // multiple elif blocks
1359
Damiend99b0522013-12-21 18:17:45 +00001360 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001361 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001362 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001363 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001364 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1365
1366 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001367 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien429d7192013-10-04 19:53:11 +01001368 EMIT(jump, l_end);
1369 }
1370 EMIT(label_assign, l_fail);
1371 }
1372
1373 } else {
1374 // a single elif block
1375
Damienb05d7072013-10-05 13:37:10 +01001376 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001377 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1378
1379 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001380 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien429d7192013-10-04 19:53:11 +01001381 EMIT(jump, l_end);
1382 }
1383 EMIT(label_assign, l_fail);
1384 }
1385 }
1386
1387 // compile else block
1388 compile_node(comp, pns->nodes[3]); // can be null
1389
1390 EMIT(label_assign, l_end);
1391}
1392
Damiend99b0522013-12-21 18:17:45 +00001393void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001394 int old_break_label = comp->break_label;
1395 int old_continue_label = comp->continue_label;
1396
Damienb05d7072013-10-05 13:37:10 +01001397 int break_label = comp_next_label(comp);
1398 int continue_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001399
1400 comp->break_label = break_label;
1401 comp->continue_label = continue_label;
1402
Damience89a212013-10-15 22:25:17 +01001403 // compared to CPython, we have an optimised version of while loops
1404#if MICROPY_EMIT_CPYTHON
1405 int done_label = comp_next_label(comp);
1406 EMIT(setup_loop, break_label);
Damien429d7192013-10-04 19:53:11 +01001407 EMIT(label_assign, continue_label);
1408 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1409 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001410 if (!EMIT(last_emit_was_return_value)) {
Damien429d7192013-10-04 19:53:11 +01001411 EMIT(jump, continue_label);
1412 }
1413 EMIT(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001414 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1415 // this is a small hack to agree with CPython
1416 if (!node_is_const_true(pns->nodes[0])) {
1417 EMIT(pop_block);
1418 }
Damience89a212013-10-15 22:25:17 +01001419#else
1420 int top_label = comp_next_label(comp);
1421 EMIT(jump, continue_label);
1422 EMIT(label_assign, top_label);
1423 compile_node(comp, pns->nodes[1]); // body
1424 EMIT(label_assign, continue_label);
1425 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1426#endif
1427
1428 // break/continue apply to outer loop (if any) in the else block
1429 comp->break_label = old_break_label;
1430 comp->continue_label = old_continue_label;
Damien429d7192013-10-04 19:53:11 +01001431
1432 compile_node(comp, pns->nodes[2]); // else
1433
1434 EMIT(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001435}
1436
Damienf72fd0e2013-11-06 20:20:49 +00001437// TODO preload end and step onto stack if they are not constants
1438// TODO check if step is negative and do opposite test
Damiend99b0522013-12-21 18:17:45 +00001439void compile_for_stmt_optimised_range(compiler_t *comp, mp_parse_node_t pn_var, mp_parse_node_t pn_start, mp_parse_node_t pn_end, mp_parse_node_t pn_step, mp_parse_node_t pn_body, mp_parse_node_t pn_else) {
Damienf72fd0e2013-11-06 20:20:49 +00001440 int old_break_label = comp->break_label;
1441 int old_continue_label = comp->continue_label;
1442
1443 int break_label = comp_next_label(comp);
1444 int continue_label = comp_next_label(comp);
1445
1446 comp->break_label = break_label;
1447 comp->continue_label = continue_label;
1448
1449 int top_label = comp_next_label(comp);
1450
1451 // compile: var = start
1452 compile_node(comp, pn_start);
1453 c_assign(comp, pn_var, ASSIGN_STORE);
1454
1455 EMIT(jump, continue_label);
1456 EMIT(label_assign, top_label);
1457
Damienf3822fc2013-11-09 20:12:03 +00001458 // compile body
1459 compile_node(comp, pn_body);
1460
Damienf72fd0e2013-11-06 20:20:49 +00001461 // compile: var += step
1462 c_assign(comp, pn_var, ASSIGN_AUG_LOAD);
1463 compile_node(comp, pn_step);
1464 EMIT(binary_op, RT_BINARY_OP_INPLACE_ADD);
1465 c_assign(comp, pn_var, ASSIGN_AUG_STORE);
1466
Damienf72fd0e2013-11-06 20:20:49 +00001467 EMIT(label_assign, continue_label);
1468
1469 // compile: if var < end: goto top
1470 compile_node(comp, pn_var);
1471 compile_node(comp, pn_end);
1472 EMIT(compare_op, RT_COMPARE_OP_LESS);
1473 EMIT(pop_jump_if_true, top_label);
1474
1475 // break/continue apply to outer loop (if any) in the else block
1476 comp->break_label = old_break_label;
1477 comp->continue_label = old_continue_label;
1478
1479 compile_node(comp, pn_else);
1480
1481 EMIT(label_assign, break_label);
1482}
1483
Damiend99b0522013-12-21 18:17:45 +00001484void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001485#if !MICROPY_EMIT_CPYTHON
1486 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1487 // this is actually slower, but uses no heap memory
1488 // for viper it will be much, much faster
Damiend99b0522013-12-21 18:17:45 +00001489 if (/*comp->scope_cur->emit_options == EMIT_OPT_VIPER &&*/ MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_power)) {
1490 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
1491 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == comp->qstr_range && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren) && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
1492 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1493 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001494 int n_args = list_get(&pn_range_args, PN_arglist, &args);
1495 if (1 <= n_args && n_args <= 3) {
Damiend99b0522013-12-21 18:17:45 +00001496 mp_parse_node_t pn_range_start;
1497 mp_parse_node_t pn_range_end;
1498 mp_parse_node_t pn_range_step;
Damienf72fd0e2013-11-06 20:20:49 +00001499 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001500 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001501 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001502 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001503 } else if (n_args == 2) {
1504 pn_range_start = args[0];
1505 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001506 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001507 } else {
1508 pn_range_start = args[0];
1509 pn_range_end = args[1];
1510 pn_range_step = args[2];
1511 }
1512 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1513 return;
1514 }
1515 }
1516 }
1517#endif
1518
Damien429d7192013-10-04 19:53:11 +01001519 int old_break_label = comp->break_label;
1520 int old_continue_label = comp->continue_label;
1521
Damienb05d7072013-10-05 13:37:10 +01001522 int for_label = comp_next_label(comp);
1523 int pop_label = comp_next_label(comp);
1524 int end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001525
Damienb05d7072013-10-05 13:37:10 +01001526 int break_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001527
1528 comp->continue_label = for_label;
1529 comp->break_label = break_label;
1530
Damience89a212013-10-15 22:25:17 +01001531 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1532#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001533 EMIT(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001534#endif
1535
Damien429d7192013-10-04 19:53:11 +01001536 compile_node(comp, pns->nodes[1]); // iterator
1537 EMIT(get_iter);
1538 EMIT(label_assign, for_label);
1539 EMIT(for_iter, pop_label);
1540 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1541 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001542 if (!EMIT(last_emit_was_return_value)) {
Damien429d7192013-10-04 19:53:11 +01001543 EMIT(jump, for_label);
1544 }
1545 EMIT(label_assign, pop_label);
1546 EMIT(for_iter_end);
1547
1548 // break/continue apply to outer loop (if any) in the else block
1549 comp->break_label = old_break_label;
1550 comp->continue_label = old_continue_label;
1551
Damience89a212013-10-15 22:25:17 +01001552#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001553 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001554#endif
Damien429d7192013-10-04 19:53:11 +01001555
1556 compile_node(comp, pns->nodes[3]); // else (not tested)
1557
1558 EMIT(label_assign, break_label);
1559 EMIT(label_assign, end_label);
1560}
1561
Damiend99b0522013-12-21 18:17:45 +00001562void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_except, mp_parse_node_t *pn_excepts, mp_parse_node_t pn_else) {
Damien429d7192013-10-04 19:53:11 +01001563 // this function is a bit of a hack at the moment
1564 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1565
1566 // setup code
1567 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001568 int l1 = comp_next_label(comp);
1569 int success_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001570 comp->except_nest_level += 1; // for correct handling of continue
1571 EMIT(setup_except, l1);
1572 compile_node(comp, pn_body); // body
1573 EMIT(pop_block);
1574 EMIT(jump, success_label);
1575 EMIT(label_assign, l1);
Damienb05d7072013-10-05 13:37:10 +01001576 int l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001577
1578 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001579 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1580 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001581
1582 qstr qstr_exception_local = 0;
Damienb05d7072013-10-05 13:37:10 +01001583 int end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001584
Damiend99b0522013-12-21 18:17:45 +00001585 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001586 // this is a catch all exception handler
1587 if (i + 1 != n_except) {
1588 printf("SyntaxError: default 'except:' must be last\n");
1589 return;
1590 }
1591 } else {
1592 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001593 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1594 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1595 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1596 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001597 // handler binds the exception to a local
1598 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001599 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001600 }
1601 }
1602 EMIT(dup_top);
1603 compile_node(comp, pns_exception_expr);
1604 EMIT(compare_op, RT_COMPARE_OP_EXCEPTION_MATCH);
1605 EMIT(pop_jump_if_false, end_finally_label);
1606 }
1607
1608 EMIT(pop_top);
1609
1610 if (qstr_exception_local == 0) {
1611 EMIT(pop_top);
1612 } else {
Damien4b03e772013-10-05 14:17:09 +01001613 EMIT(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001614 }
1615
1616 EMIT(pop_top);
1617
Damiene2880aa2013-12-20 14:22:59 +00001618 int l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001619 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001620 l3 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001621 EMIT(setup_finally, l3);
1622 }
1623 compile_node(comp, pns_except->nodes[1]);
1624 if (qstr_exception_local != 0) {
1625 EMIT(pop_block);
1626 }
1627 EMIT(pop_except);
1628 if (qstr_exception_local != 0) {
Damiend99b0522013-12-21 18:17:45 +00001629 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001630 EMIT(label_assign, l3);
Damiend99b0522013-12-21 18:17:45 +00001631 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien4b03e772013-10-05 14:17:09 +01001632 EMIT(store_id, qstr_exception_local);
1633 EMIT(delete_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001634 EMIT(end_finally);
1635 }
1636 EMIT(jump, l2);
1637 EMIT(label_assign, end_finally_label);
1638 }
1639
1640 EMIT(end_finally);
1641 EMIT(label_assign, success_label);
1642 comp->except_nest_level -= 1;
1643 compile_node(comp, pn_else); // else block, can be null
1644 EMIT(label_assign, l2);
1645 EMIT(set_stack_size, stack_size);
1646}
1647
Damiend99b0522013-12-21 18:17:45 +00001648void compile_try_finally(compiler_t *comp, mp_parse_node_t pn_body, int n_except, mp_parse_node_t *pn_except, mp_parse_node_t pn_else, mp_parse_node_t pn_finally) {
Damien429d7192013-10-04 19:53:11 +01001649 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1650 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001651 int l_finally_block = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001652 EMIT(setup_finally, l_finally_block);
1653 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001654 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien429d7192013-10-04 19:53:11 +01001655 compile_node(comp, pn_body);
1656 } else {
1657 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1658 }
1659 EMIT(pop_block);
Damiend99b0522013-12-21 18:17:45 +00001660 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001661 EMIT(label_assign, l_finally_block);
1662 compile_node(comp, pn_finally);
1663 EMIT(end_finally);
1664 EMIT(set_stack_size, stack_size);
1665}
1666
Damiend99b0522013-12-21 18:17:45 +00001667void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1668 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1669 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1670 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001671 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001672 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1673 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001674 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001675 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001676 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001677 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001678 // no finally
1679 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1680 } else {
1681 // have finally
Damiend99b0522013-12-21 18:17:45 +00001682 compile_try_finally(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1], ((mp_parse_node_struct_t*)pns2->nodes[2])->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001683 }
1684 } else {
1685 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001686 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001687 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001688 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001689 }
1690 } else {
1691 // shouldn't happen
1692 assert(0);
1693 }
1694}
1695
Damiend99b0522013-12-21 18:17:45 +00001696void compile_with_stmt_helper(compiler_t *comp, int n, mp_parse_node_t *nodes, mp_parse_node_t body) {
Damien429d7192013-10-04 19:53:11 +01001697 if (n == 0) {
1698 // no more pre-bits, compile the body of the with
1699 compile_node(comp, body);
1700 } else {
Damienb05d7072013-10-05 13:37:10 +01001701 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001702 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001703 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001704 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001705 compile_node(comp, pns->nodes[0]);
1706 EMIT(setup_with, l_end);
1707 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1708 } else {
1709 // this pre-bit is just an expression
1710 compile_node(comp, nodes[0]);
1711 EMIT(setup_with, l_end);
1712 EMIT(pop_top);
1713 }
1714 // compile additional pre-bits and the body
1715 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1716 // finish this with block
1717 EMIT(pop_block);
Damiend99b0522013-12-21 18:17:45 +00001718 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001719 EMIT(label_assign, l_end);
1720 EMIT(with_cleanup);
1721 EMIT(end_finally);
1722 }
1723}
1724
Damiend99b0522013-12-21 18:17:45 +00001725void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001726 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001727 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001728 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1729 assert(n > 0);
1730
1731 // compile in a nested fashion
1732 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1733}
1734
Damiend99b0522013-12-21 18:17:45 +00001735void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1736 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001737 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1738 // for REPL, evaluate then print the expression
1739 EMIT(load_id, qstr_from_str_static("__repl_print__"));
1740 compile_node(comp, pns->nodes[0]);
1741 EMIT(call_function, 1, 0, false, false);
1742 EMIT(pop_top);
1743
Damien429d7192013-10-04 19:53:11 +01001744 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001745 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001746 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001747 // do nothing with a lonely constant
1748 } else {
1749 compile_node(comp, pns->nodes[0]); // just an expression
1750 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1751 }
Damien429d7192013-10-04 19:53:11 +01001752 }
1753 } else {
Damiend99b0522013-12-21 18:17:45 +00001754 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1755 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001756 if (kind == PN_expr_stmt_augassign) {
1757 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1758 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001759 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001760 // note that we don't really need to implement separate inplace ops, just normal binary ops will suffice
Damiend99b0522013-12-21 18:17:45 +00001761 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
1762 case MP_TOKEN_DEL_PIPE_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_OR); break;
1763 case MP_TOKEN_DEL_CARET_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_XOR); break;
1764 case MP_TOKEN_DEL_AMPERSAND_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_AND); break;
1765 case MP_TOKEN_DEL_DBL_LESS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_LSHIFT); break;
1766 case MP_TOKEN_DEL_DBL_MORE_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_RSHIFT); break;
1767 case MP_TOKEN_DEL_PLUS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_ADD); break;
1768 case MP_TOKEN_DEL_MINUS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_SUBTRACT); break;
1769 case MP_TOKEN_DEL_STAR_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_MULTIPLY); break;
1770 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_FLOOR_DIVIDE); break;
1771 case MP_TOKEN_DEL_SLASH_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_TRUE_DIVIDE); break;
1772 case MP_TOKEN_DEL_PERCENT_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_MODULO); break;
1773 case MP_TOKEN_DEL_DBL_STAR_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_POWER); break;
Damien429d7192013-10-04 19:53:11 +01001774 default: assert(0); // shouldn't happen
1775 }
1776 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1777 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001778 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1779 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001780 // following CPython, we store left-most first
1781 if (rhs > 0) {
1782 EMIT(dup_top);
1783 }
1784 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1785 for (int i = 0; i < rhs; i++) {
1786 if (i + 1 < rhs) {
1787 EMIT(dup_top);
1788 }
Damiend99b0522013-12-21 18:17:45 +00001789 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001790 }
1791 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001792 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1793 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1794 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1795 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01001796 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001797 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1798 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001799 compile_node(comp, pns10->nodes[0]); // rhs
1800 compile_node(comp, pns10->nodes[1]); // rhs
1801 EMIT(rot_two);
1802 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1803 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00001804 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1805 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1806 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1807 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01001808 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001809 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1810 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001811 compile_node(comp, pns10->nodes[0]); // rhs
1812 compile_node(comp, pns10->nodes[1]); // rhs
1813 compile_node(comp, pns10->nodes[2]); // rhs
1814 EMIT(rot_three);
1815 EMIT(rot_two);
1816 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1817 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1818 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1819 } else {
1820 compile_node(comp, pns1->nodes[0]); // rhs
1821 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1822 }
1823 } else {
1824 // shouldn't happen
1825 assert(0);
1826 }
1827 }
1828}
1829
Damiend99b0522013-12-21 18:17:45 +00001830void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, rt_binary_op_t binary_op) {
1831 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001832 compile_node(comp, pns->nodes[0]);
1833 for (int i = 1; i < num_nodes; i += 1) {
1834 compile_node(comp, pns->nodes[i]);
1835 EMIT(binary_op, binary_op);
1836 }
1837}
1838
Damiend99b0522013-12-21 18:17:45 +00001839void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1840 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1841 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001842
1843 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001844 int l_fail = comp_next_label(comp);
1845 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001846 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1847 compile_node(comp, pns->nodes[0]); // success value
1848 EMIT(jump, l_end);
1849 EMIT(label_assign, l_fail);
1850 EMIT(set_stack_size, stack_size); // force stack size reset
1851 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1852 EMIT(label_assign, l_end);
1853}
1854
Damiend99b0522013-12-21 18:17:45 +00001855void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001856 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00001857 //mp_parse_node_t pn_params = pns->nodes[0];
1858 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001859
1860 if (comp->pass == PASS_1) {
1861 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00001862 scope_t *s = scope_new_and_link(comp, SCOPE_LAMBDA, (mp_parse_node_t)pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001863 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001864 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001865 }
1866
1867 // get the scope for this lambda
1868 scope_t *this_scope = (scope_t*)pns->nodes[2];
1869
1870 // make the lambda
1871 close_over_variables_etc(comp, this_scope, 0, 0);
1872}
1873
Damiend99b0522013-12-21 18:17:45 +00001874void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001875 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001876 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001877 for (int i = 0; i < n; i += 1) {
1878 compile_node(comp, pns->nodes[i]);
1879 if (i + 1 < n) {
1880 EMIT(jump_if_true_or_pop, l_end);
1881 }
1882 }
1883 EMIT(label_assign, l_end);
1884}
1885
Damiend99b0522013-12-21 18:17:45 +00001886void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001887 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001888 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001889 for (int i = 0; i < n; i += 1) {
1890 compile_node(comp, pns->nodes[i]);
1891 if (i + 1 < n) {
1892 EMIT(jump_if_false_or_pop, l_end);
1893 }
1894 }
1895 EMIT(label_assign, l_end);
1896}
1897
Damiend99b0522013-12-21 18:17:45 +00001898void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001899 compile_node(comp, pns->nodes[0]);
1900 EMIT(unary_op, RT_UNARY_OP_NOT);
1901}
1902
Damiend99b0522013-12-21 18:17:45 +00001903void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001904 int stack_size = EMIT(get_stack_size);
Damiend99b0522013-12-21 18:17:45 +00001905 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001906 compile_node(comp, pns->nodes[0]);
1907 bool multi = (num_nodes > 3);
1908 int l_fail = 0;
1909 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001910 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001911 }
1912 for (int i = 1; i + 1 < num_nodes; i += 2) {
1913 compile_node(comp, pns->nodes[i + 1]);
1914 if (i + 2 < num_nodes) {
1915 EMIT(dup_top);
1916 EMIT(rot_three);
1917 }
Damiend99b0522013-12-21 18:17:45 +00001918 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_LESS)) {
Damien429d7192013-10-04 19:53:11 +01001919 EMIT(compare_op, RT_COMPARE_OP_LESS);
Damiend99b0522013-12-21 18:17:45 +00001920 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MORE)) {
Damien429d7192013-10-04 19:53:11 +01001921 EMIT(compare_op, RT_COMPARE_OP_MORE);
Damiend99b0522013-12-21 18:17:45 +00001922 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_EQUAL)) {
Damien429d7192013-10-04 19:53:11 +01001923 EMIT(compare_op, RT_COMPARE_OP_EQUAL);
Damiend99b0522013-12-21 18:17:45 +00001924 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_LESS_EQUAL)) {
Damien429d7192013-10-04 19:53:11 +01001925 EMIT(compare_op, RT_COMPARE_OP_LESS_EQUAL);
Damiend99b0522013-12-21 18:17:45 +00001926 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MORE_EQUAL)) {
Damien429d7192013-10-04 19:53:11 +01001927 EMIT(compare_op, RT_COMPARE_OP_MORE_EQUAL);
Damiend99b0522013-12-21 18:17:45 +00001928 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_NOT_EQUAL)) {
Damien429d7192013-10-04 19:53:11 +01001929 EMIT(compare_op, RT_COMPARE_OP_NOT_EQUAL);
Damiend99b0522013-12-21 18:17:45 +00001930 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_KW_IN)) {
Damien429d7192013-10-04 19:53:11 +01001931 EMIT(compare_op, RT_COMPARE_OP_IN);
Damiend99b0522013-12-21 18:17:45 +00001932 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
1933 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
1934 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01001935 if (kind == PN_comp_op_not_in) {
1936 EMIT(compare_op, RT_COMPARE_OP_NOT_IN);
1937 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00001938 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001939 EMIT(compare_op, RT_COMPARE_OP_IS);
1940 } else {
1941 EMIT(compare_op, RT_COMPARE_OP_IS_NOT);
1942 }
1943 } else {
1944 // shouldn't happen
1945 assert(0);
1946 }
1947 } else {
1948 // shouldn't happen
1949 assert(0);
1950 }
1951 if (i + 2 < num_nodes) {
1952 EMIT(jump_if_false_or_pop, l_fail);
1953 }
1954 }
1955 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001956 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001957 EMIT(jump, l_end);
1958 EMIT(label_assign, l_fail);
1959 EMIT(rot_two);
1960 EMIT(pop_top);
1961 EMIT(label_assign, l_end);
1962 EMIT(set_stack_size, stack_size + 1); // force stack size
1963 }
1964}
1965
Damiend99b0522013-12-21 18:17:45 +00001966void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001967 // TODO
1968 assert(0);
1969 compile_node(comp, pns->nodes[0]);
1970 //EMIT(unary_op, "UNARY_STAR");
1971}
1972
Damiend99b0522013-12-21 18:17:45 +00001973void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001974 c_binary_op(comp, pns, RT_BINARY_OP_OR);
1975}
1976
Damiend99b0522013-12-21 18:17:45 +00001977void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001978 c_binary_op(comp, pns, RT_BINARY_OP_XOR);
1979}
1980
Damiend99b0522013-12-21 18:17:45 +00001981void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001982 c_binary_op(comp, pns, RT_BINARY_OP_AND);
1983}
1984
Damiend99b0522013-12-21 18:17:45 +00001985void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1986 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001987 compile_node(comp, pns->nodes[0]);
1988 for (int i = 1; i + 1 < num_nodes; i += 2) {
1989 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00001990 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien429d7192013-10-04 19:53:11 +01001991 EMIT(binary_op, RT_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00001992 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien429d7192013-10-04 19:53:11 +01001993 EMIT(binary_op, RT_BINARY_OP_RSHIFT);
1994 } else {
1995 // shouldn't happen
1996 assert(0);
1997 }
1998 }
1999}
2000
Damiend99b0522013-12-21 18:17:45 +00002001void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2002 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002003 compile_node(comp, pns->nodes[0]);
2004 for (int i = 1; i + 1 < num_nodes; i += 2) {
2005 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002006 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien429d7192013-10-04 19:53:11 +01002007 EMIT(binary_op, RT_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002008 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien429d7192013-10-04 19:53:11 +01002009 EMIT(binary_op, RT_BINARY_OP_SUBTRACT);
2010 } else {
2011 // shouldn't happen
2012 assert(0);
2013 }
2014 }
2015}
2016
Damiend99b0522013-12-21 18:17:45 +00002017void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2018 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002019 compile_node(comp, pns->nodes[0]);
2020 for (int i = 1; i + 1 < num_nodes; i += 2) {
2021 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002022 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien429d7192013-10-04 19:53:11 +01002023 EMIT(binary_op, RT_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002024 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien429d7192013-10-04 19:53:11 +01002025 EMIT(binary_op, RT_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002026 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien429d7192013-10-04 19:53:11 +01002027 EMIT(binary_op, RT_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002028 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien429d7192013-10-04 19:53:11 +01002029 EMIT(binary_op, RT_BINARY_OP_MODULO);
2030 } else {
2031 // shouldn't happen
2032 assert(0);
2033 }
2034 }
2035}
2036
Damiend99b0522013-12-21 18:17:45 +00002037void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002038 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002039 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien429d7192013-10-04 19:53:11 +01002040 EMIT(unary_op, RT_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002041 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien429d7192013-10-04 19:53:11 +01002042 EMIT(unary_op, RT_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002043 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien429d7192013-10-04 19:53:11 +01002044 EMIT(unary_op, RT_UNARY_OP_INVERT);
2045 } else {
2046 // shouldn't happen
2047 assert(0);
2048 }
2049}
2050
Damiend99b0522013-12-21 18:17:45 +00002051void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_struct_t *pns, bool is_method_call) {
Damien429d7192013-10-04 19:53:11 +01002052 // function to call is on top of stack
2053
2054 int old_n_arg_keyword = comp->n_arg_keyword;
2055 bool old_have_star_arg = comp->have_star_arg;
2056 bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
2057 comp->n_arg_keyword = 0;
2058 comp->have_star_arg = false;
2059 comp->have_dbl_star_arg = false;
2060
2061 compile_node(comp, pns->nodes[0]); // arguments to function call; can be null
2062
2063 // compute number of positional arguments
2064 int n_positional = list_len(pns->nodes[0], PN_arglist) - comp->n_arg_keyword;
2065 if (comp->have_star_arg) {
2066 n_positional -= 1;
2067 }
2068 if (comp->have_dbl_star_arg) {
2069 n_positional -= 1;
2070 }
2071
2072 if (is_method_call) {
2073 EMIT(call_method, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg);
2074 } else {
2075 EMIT(call_function, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg);
2076 }
2077
2078 comp->n_arg_keyword = old_n_arg_keyword;
2079 comp->have_star_arg = old_have_star_arg;
2080 comp->have_dbl_star_arg = old_have_dbl_star_arg;
2081}
2082
Damiend99b0522013-12-21 18:17:45 +00002083void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2084 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002085 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002086 if (i + 1 < num_nodes && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[i], PN_trailer_period) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[i + 1], PN_trailer_paren)) {
Damien429d7192013-10-04 19:53:11 +01002087 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002088 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2089 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
2090 EMIT(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien429d7192013-10-04 19:53:11 +01002091 compile_trailer_paren_helper(comp, pns_paren, true);
2092 i += 1;
2093 } else {
2094 compile_node(comp, pns->nodes[i]);
2095 }
2096 }
2097}
2098
Damiend99b0522013-12-21 18:17:45 +00002099void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002100 compile_node(comp, pns->nodes[0]);
2101 EMIT(binary_op, RT_BINARY_OP_POWER);
2102}
2103
Damiend99b0522013-12-21 18:17:45 +00002104void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002105 // a list of strings
Damien63321742013-12-10 17:41:49 +00002106
2107 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002108 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002109 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002110 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002111 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002112 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2113 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2114 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002115 if (i == 0) {
2116 string_kind = pn_kind;
2117 } else if (pn_kind != string_kind) {
2118 printf("SyntaxError: cannot mix bytes and nonbytes literals\n");
2119 return;
2120 }
Damiend99b0522013-12-21 18:17:45 +00002121 const char *str = qstr_str(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien63321742013-12-10 17:41:49 +00002122 n_bytes += strlen(str);
Damien429d7192013-10-04 19:53:11 +01002123 }
Damien63321742013-12-10 17:41:49 +00002124
2125 // allocate memory for concatenated string/bytes
2126 char *cat_str = m_new(char, n_bytes + 1);
2127 cat_str[0] = '\0';
2128
2129 // concatenate string/bytes
2130 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002131 const char *str = qstr_str(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien63321742013-12-10 17:41:49 +00002132 strcat(cat_str, str);
2133 }
2134
Damien732407f2013-12-29 19:33:23 +00002135 EMIT(load_const_str, qstr_from_str_take(cat_str, n_bytes + 1), string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002136}
2137
2138// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002139void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2140 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2141 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2142 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002143
2144 if (comp->pass == PASS_1) {
2145 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002146 scope_t *s = scope_new_and_link(comp, kind, (mp_parse_node_t)pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002147 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002148 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002149 }
2150
2151 // get the scope for this comprehension
2152 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2153
2154 // compile the comprehension
2155 close_over_variables_etc(comp, this_scope, 0, 0);
2156
2157 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2158 EMIT(get_iter);
2159 EMIT(call_function, 1, 0, false, false);
2160}
2161
Damiend99b0522013-12-21 18:17:45 +00002162void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2163 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002164 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002165 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2166 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2167 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2168 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2169 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2170 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2171 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002172 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002173 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002174 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002175 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002176 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002177 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002178 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002179 // generator expression
2180 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2181 } else {
2182 // tuple with 2 items
2183 goto tuple_with_2_items;
2184 }
2185 } else {
2186 // tuple with 2 items
2187 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002188 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002189 }
2190 } else {
2191 // parenthesis around a single item, is just that item
2192 compile_node(comp, pns->nodes[0]);
2193 }
2194}
2195
Damiend99b0522013-12-21 18:17:45 +00002196void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2197 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002198 // empty list
2199 EMIT(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002200 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2201 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2202 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2203 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2204 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002205 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002206 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002207 compile_node(comp, pns2->nodes[0]);
2208 EMIT(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002209 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002210 // list of many items
2211 compile_node(comp, pns2->nodes[0]);
2212 compile_generic_all_nodes(comp, pns3);
Damiend99b0522013-12-21 18:17:45 +00002213 EMIT(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
2214 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002215 // list comprehension
2216 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2217 } else {
2218 // list with 2 items
2219 goto list_with_2_items;
2220 }
2221 } else {
2222 // list with 2 items
2223 list_with_2_items:
2224 compile_node(comp, pns2->nodes[0]);
2225 compile_node(comp, pns2->nodes[1]);
2226 EMIT(build_list, 2);
2227 }
2228 } else {
2229 // list with 1 item
2230 compile_node(comp, pns->nodes[0]);
2231 EMIT(build_list, 1);
2232 }
2233}
2234
Damiend99b0522013-12-21 18:17:45 +00002235void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2236 mp_parse_node_t pn = pns->nodes[0];
2237 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002238 // empty dict
2239 EMIT(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002240 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2241 pns = (mp_parse_node_struct_t*)pn;
2242 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002243 // dict with one element
2244 EMIT(build_map, 1);
2245 compile_node(comp, pn);
2246 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002247 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2248 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2249 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2250 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002251 // dict/set with multiple elements
2252
2253 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002254 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002255 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2256
2257 // first element sets whether it's a dict or set
2258 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002259 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002260 // a dictionary
2261 EMIT(build_map, 1 + n);
2262 compile_node(comp, pns->nodes[0]);
2263 EMIT(store_map);
2264 is_dict = true;
2265 } else {
2266 // a set
2267 compile_node(comp, pns->nodes[0]); // 1st value of set
2268 is_dict = false;
2269 }
2270
2271 // process rest of elements
2272 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002273 mp_parse_node_t pn = nodes[i];
2274 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002275 compile_node(comp, pn);
2276 if (is_dict) {
2277 if (!is_key_value) {
2278 printf("SyntaxError?: expecting key:value for dictionary");
2279 return;
2280 }
2281 EMIT(store_map);
2282 } else {
2283 if (is_key_value) {
2284 printf("SyntaxError?: expecting just a value for set");
2285 return;
2286 }
2287 }
2288 }
2289
2290 // if it's a set, build it
2291 if (!is_dict) {
2292 EMIT(build_set, 1 + n);
2293 }
Damiend99b0522013-12-21 18:17:45 +00002294 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002295 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002296 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002297 // a dictionary comprehension
2298 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2299 } else {
2300 // a set comprehension
2301 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2302 }
2303 } else {
2304 // shouldn't happen
2305 assert(0);
2306 }
2307 } else {
2308 // set with one element
2309 goto set_with_one_element;
2310 }
2311 } else {
2312 // set with one element
2313 set_with_one_element:
2314 compile_node(comp, pn);
2315 EMIT(build_set, 1);
2316 }
2317}
2318
Damiend99b0522013-12-21 18:17:45 +00002319void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002320 compile_trailer_paren_helper(comp, pns, false);
2321}
2322
Damiend99b0522013-12-21 18:17:45 +00002323void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002324 // object who's index we want is on top of stack
2325 compile_node(comp, pns->nodes[0]); // the index
2326 EMIT(binary_op, RT_BINARY_OP_SUBSCR);
2327}
2328
Damiend99b0522013-12-21 18:17:45 +00002329void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002330 // object who's attribute we want is on top of stack
Damiend99b0522013-12-21 18:17:45 +00002331 EMIT(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002332}
2333
Damiend99b0522013-12-21 18:17:45 +00002334void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2335 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2336 mp_parse_node_t pn = pns->nodes[0];
2337 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002338 // [?:]
Damiend99b0522013-12-21 18:17:45 +00002339 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002340 EMIT(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002341 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2342 pns = (mp_parse_node_struct_t*)pn;
2343 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
2344 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002345 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002346 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002347 // [?::]
2348 EMIT(build_slice, 2);
2349 } else {
2350 // [?::x]
2351 compile_node(comp, pn);
2352 EMIT(build_slice, 3);
2353 }
Damiend99b0522013-12-21 18:17:45 +00002354 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002355 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002356 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2357 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2358 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2359 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002360 // [?:x:]
2361 EMIT(build_slice, 2);
2362 } else {
2363 // [?:x:x]
2364 compile_node(comp, pns->nodes[0]);
2365 EMIT(build_slice, 3);
2366 }
2367 } else {
2368 // [?:x]
2369 compile_node(comp, pn);
2370 EMIT(build_slice, 2);
2371 }
2372 } else {
2373 // [?:x]
2374 compile_node(comp, pn);
2375 EMIT(build_slice, 2);
2376 }
2377}
2378
Damiend99b0522013-12-21 18:17:45 +00002379void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002380 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002381 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2382 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002383}
2384
Damiend99b0522013-12-21 18:17:45 +00002385void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
2386 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002387 compile_subscript_3_helper(comp, pns);
2388}
2389
Damiend99b0522013-12-21 18:17:45 +00002390void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002391 // if this is called then we are compiling a dict key:value pair
2392 compile_node(comp, pns->nodes[1]); // value
2393 compile_node(comp, pns->nodes[0]); // key
2394}
2395
Damiend99b0522013-12-21 18:17:45 +00002396void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002397 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002398 // store class object into class name
Damien4b03e772013-10-05 14:17:09 +01002399 EMIT(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002400}
2401
Damiend99b0522013-12-21 18:17:45 +00002402void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002403 if (comp->have_star_arg) {
2404 printf("SyntaxError?: can't have multiple *x\n");
2405 return;
2406 }
2407 comp->have_star_arg = true;
2408 compile_node(comp, pns->nodes[0]);
2409}
2410
Damiend99b0522013-12-21 18:17:45 +00002411void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002412 if (comp->have_dbl_star_arg) {
2413 printf("SyntaxError?: can't have multiple **x\n");
2414 return;
2415 }
2416 comp->have_dbl_star_arg = true;
2417 compile_node(comp, pns->nodes[0]);
2418}
2419
Damiend99b0522013-12-21 18:17:45 +00002420void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2421 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2422 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2423 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2424 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002425 printf("SyntaxError?: lhs of keyword argument must be an id\n");
2426 return;
2427 }
Damiend99b0522013-12-21 18:17:45 +00002428 EMIT(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002429 compile_node(comp, pns2->nodes[0]);
2430 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002431 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002432 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2433 } else {
2434 // shouldn't happen
2435 assert(0);
2436 }
2437}
2438
Damiend99b0522013-12-21 18:17:45 +00002439void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002440 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
2441 printf("SyntaxError: 'yield' outside function\n");
2442 return;
2443 }
Damiend99b0522013-12-21 18:17:45 +00002444 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
2445 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002446 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002447 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2448 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002449 compile_node(comp, pns->nodes[0]);
2450 EMIT(get_iter);
Damiend99b0522013-12-21 18:17:45 +00002451 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002452 EMIT(yield_from);
2453 } else {
2454 compile_node(comp, pns->nodes[0]);
2455 EMIT(yield_value);
2456 }
2457}
2458
Damiend99b0522013-12-21 18:17:45 +00002459typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Damien429d7192013-10-04 19:53:11 +01002460static compile_function_t compile_function[] = {
2461 NULL,
2462#define nc NULL
2463#define c(f) compile_##f
2464#define DEF_RULE(rule, comp, kind, arg...) comp,
2465#include "grammar.h"
2466#undef nc
2467#undef c
2468#undef DEF_RULE
2469};
2470
Damiend99b0522013-12-21 18:17:45 +00002471void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2472 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002473 // pass
Damiend99b0522013-12-21 18:17:45 +00002474 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
2475 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
2476 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
2477 case MP_PARSE_NODE_ID: EMIT(load_id, arg); break;
2478 case MP_PARSE_NODE_SMALL_INT: EMIT(load_const_small_int, arg); break;
2479 case MP_PARSE_NODE_INTEGER: EMIT(load_const_int, arg); break;
2480 case MP_PARSE_NODE_DECIMAL: EMIT(load_const_dec, arg); break;
2481 case MP_PARSE_NODE_STRING: EMIT(load_const_str, arg, false); break;
2482 case MP_PARSE_NODE_BYTES: EMIT(load_const_str, arg, true); break;
2483 case MP_PARSE_NODE_TOKEN:
2484 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002485 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002486 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002487 // do nothing
2488 } else {
2489 EMIT(load_const_tok, arg);
2490 }
2491 break;
Damien429d7192013-10-04 19:53:11 +01002492 default: assert(0);
2493 }
2494 } else {
Damiend99b0522013-12-21 18:17:45 +00002495 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2496 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002497 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002498 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2499 mp_parse_node_show(pn, 0);
Damien429d7192013-10-04 19:53:11 +01002500 assert(0);
2501 } else {
2502 f(comp, pns);
2503 }
2504 }
2505}
2506
Damiend99b0522013-12-21 18:17:45 +00002507void compile_scope_func_lambda_param(compiler_t *comp, mp_parse_node_t pn, pn_kind_t pn_name, pn_kind_t pn_star, pn_kind_t pn_dbl_star, bool allow_annotations) {
Damien429d7192013-10-04 19:53:11 +01002508 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002509 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002510 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2511 if (MP_PARSE_NODE_IS_ID(pn)) {
2512 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002513 if (comp->have_bare_star) {
2514 // comes after a bare star, so doesn't count as a parameter
2515 } else {
2516 comp->scope_cur->num_params += 1;
2517 }
Damienb14de212013-10-06 00:28:28 +01002518 } else {
Damiend99b0522013-12-21 18:17:45 +00002519 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2520 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2521 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2522 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002523 //int node_index = 1; unused
2524 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002525 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002526 // this parameter has an annotation
2527 pn_annotation = pns->nodes[1];
2528 }
2529 //node_index = 2; unused
2530 }
2531 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002532 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002533 // this parameter has a default value
2534 if (comp->have_bare_star) {
2535 comp->scope_cur->num_dict_params += 1;
2536 } else {
2537 comp->scope_cur->num_default_params += 1;
2538 }
2539 }
2540 */
2541 if (comp->have_bare_star) {
2542 // comes after a bare star, so doesn't count as a parameter
2543 } else {
2544 comp->scope_cur->num_params += 1;
2545 }
Damiend99b0522013-12-21 18:17:45 +00002546 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2547 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002548 // bare star
2549 // TODO see http://www.python.org/dev/peps/pep-3102/
2550 comp->have_bare_star = true;
2551 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002552 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002553 // named star
2554 comp->scope_cur->flags |= SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002555 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2556 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002557 // named star with annotation
2558 comp->scope_cur->flags |= SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002559 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2560 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002561 pn_annotation = pns->nodes[1];
2562 } else {
2563 // shouldn't happen
2564 assert(0);
2565 }
Damiend99b0522013-12-21 18:17:45 +00002566 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2567 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2568 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002569 // this parameter has an annotation
2570 pn_annotation = pns->nodes[1];
2571 }
2572 comp->scope_cur->flags |= SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002573 } else {
Damienb14de212013-10-06 00:28:28 +01002574 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002575 assert(0);
2576 }
Damien429d7192013-10-04 19:53:11 +01002577 }
2578
2579 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002580 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002581 // TODO this parameter has an annotation
2582 }
2583 bool added;
2584 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2585 if (!added) {
2586 printf("SyntaxError?: same name used for parameter; %s\n", qstr_str(param_name));
2587 return;
2588 }
2589 id_info->param = true;
2590 id_info->kind = ID_INFO_KIND_LOCAL;
2591 }
2592}
2593
Damiend99b0522013-12-21 18:17:45 +00002594void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002595 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2596}
2597
Damiend99b0522013-12-21 18:17:45 +00002598void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002599 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2600}
2601
Damiend99b0522013-12-21 18:17:45 +00002602void compile_scope_comp_iter(compiler_t *comp, mp_parse_node_t pn_iter, mp_parse_node_t pn_inner_expr, int l_top, int for_depth) {
Damien429d7192013-10-04 19:53:11 +01002603 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002604 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002605 // no more nested if/for; compile inner expression
2606 compile_node(comp, pn_inner_expr);
2607 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
2608 EMIT(list_append, for_depth + 2);
2609 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
2610 EMIT(map_add, for_depth + 2);
2611 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
2612 EMIT(set_add, for_depth + 2);
2613 } else {
2614 EMIT(yield_value);
2615 EMIT(pop_top);
2616 }
Damiend99b0522013-12-21 18:17:45 +00002617 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002618 // if condition
Damiend99b0522013-12-21 18:17:45 +00002619 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002620 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2621 pn_iter = pns_comp_if->nodes[1];
2622 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002623 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002624 // for loop
Damiend99b0522013-12-21 18:17:45 +00002625 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002626 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002627 int l_end2 = comp_next_label(comp);
2628 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002629 EMIT(get_iter);
2630 EMIT(label_assign, l_top2);
2631 EMIT(for_iter, l_end2);
2632 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2633 compile_scope_comp_iter(comp, pns_comp_for2->nodes[2], pn_inner_expr, l_top2, for_depth + 1);
2634 EMIT(jump, l_top2);
2635 EMIT(label_assign, l_end2);
2636 EMIT(for_iter_end);
2637 } else {
2638 // shouldn't happen
2639 assert(0);
2640 }
2641}
2642
Damiend99b0522013-12-21 18:17:45 +00002643void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002644 // see http://www.python.org/dev/peps/pep-0257/
2645
2646 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002647 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002648 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002649 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002650 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002651 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2652 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002653 for (int i = 0; i < num_nodes; i++) {
2654 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002655 if (!(MP_PARSE_NODE_IS_LEAF(pn) && MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_TOKEN && MP_PARSE_NODE_LEAF_ARG(pn) == MP_TOKEN_NEWLINE)) {
Damiene388f102013-12-12 15:24:38 +00002656 // not a newline, so this is the first statement; finish search
2657 break;
2658 }
2659 }
2660 // if we didn't find a non-newline then it's okay to fall through; pn will be a newline and so doc-string test below will fail gracefully
Damiend99b0522013-12-21 18:17:45 +00002661 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002662 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002663 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002664 } else {
2665 return;
2666 }
2667
2668 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002669 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2670 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2671 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2672 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2673 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002674 compile_node(comp, pns->nodes[0]); // a doc string
2675 // store doc string
Damien4b03e772013-10-05 14:17:09 +01002676 EMIT(store_id, comp->qstr___doc__);
Damien429d7192013-10-04 19:53:11 +01002677 }
2678 }
2679 }
2680}
2681
2682void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2683 comp->pass = pass;
2684 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002685 comp->next_label = 1;
Damien415eb6f2013-10-05 12:19:06 +01002686 EMIT(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002687
2688 if (comp->pass == PASS_1) {
2689 scope->stack_size = 0;
2690 }
2691
Damien5ac1b2e2013-10-18 19:58:12 +01002692#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002693 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002694 scope_print_info(scope);
2695 }
Damien5ac1b2e2013-10-18 19:58:12 +01002696#endif
Damien429d7192013-10-04 19:53:11 +01002697
2698 // compile
2699 if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002700 if (!comp->is_repl) {
2701 check_for_doc_string(comp, scope->pn);
2702 }
Damien429d7192013-10-04 19:53:11 +01002703 compile_node(comp, scope->pn);
Damiend99b0522013-12-21 18:17:45 +00002704 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002705 EMIT(return_value);
2706 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002707 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2708 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2709 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002710
2711 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002712 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002713 if (comp->pass == PASS_1) {
2714 comp->have_bare_star = false;
2715 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2716 }
2717
Damiend99b0522013-12-21 18:17:45 +00002718 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // 2 is something...
Damien429d7192013-10-04 19:53:11 +01002719
2720 compile_node(comp, pns->nodes[3]); // 3 is function body
2721 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002722 if (!EMIT(last_emit_was_return_value)) {
Damiend99b0522013-12-21 18:17:45 +00002723 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002724 EMIT(return_value);
2725 }
2726 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002727 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2728 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2729 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002730
2731 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002732 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002733 if (comp->pass == PASS_1) {
2734 comp->have_bare_star = false;
2735 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2736 }
2737
2738 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2739 EMIT(return_value);
2740 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2741 // a bit of a hack at the moment
2742
Damiend99b0522013-12-21 18:17:45 +00002743 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2744 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2745 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2746 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2747 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002748
Damien6cdd3af2013-10-05 18:08:26 +01002749 qstr qstr_arg = qstr_from_str_static(".0");
Damien429d7192013-10-04 19:53:11 +01002750 if (comp->pass == PASS_1) {
2751 bool added;
2752 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2753 assert(added);
2754 id_info->kind = ID_INFO_KIND_LOCAL;
2755 scope->num_params = 1;
2756 }
2757
2758 if (scope->kind == SCOPE_LIST_COMP) {
2759 EMIT(build_list, 0);
2760 } else if (scope->kind == SCOPE_DICT_COMP) {
2761 EMIT(build_map, 0);
2762 } else if (scope->kind == SCOPE_SET_COMP) {
2763 EMIT(build_set, 0);
2764 }
2765
Damienb05d7072013-10-05 13:37:10 +01002766 int l_end = comp_next_label(comp);
2767 int l_top = comp_next_label(comp);
Damien4b03e772013-10-05 14:17:09 +01002768 EMIT(load_id, qstr_arg);
Damien429d7192013-10-04 19:53:11 +01002769 EMIT(label_assign, l_top);
2770 EMIT(for_iter, l_end);
2771 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2772 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
2773 EMIT(jump, l_top);
2774 EMIT(label_assign, l_end);
2775 EMIT(for_iter_end);
2776
2777 if (scope->kind == SCOPE_GEN_EXPR) {
Damiend99b0522013-12-21 18:17:45 +00002778 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002779 }
2780 EMIT(return_value);
2781 } else {
2782 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002783 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2784 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2785 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01002786
2787 if (comp->pass == PASS_1) {
2788 bool added;
2789 id_info_t *id_info = scope_find_or_add_id(scope, comp->qstr___class__, &added);
2790 assert(added);
2791 id_info->kind = ID_INFO_KIND_LOCAL;
2792 id_info = scope_find_or_add_id(scope, comp->qstr___locals__, &added);
2793 assert(added);
2794 id_info->kind = ID_INFO_KIND_LOCAL;
2795 id_info->param = true;
2796 scope->num_params = 1; // __locals__ is the parameter
2797 }
2798
Damien4b03e772013-10-05 14:17:09 +01002799 EMIT(load_id, comp->qstr___locals__);
Damien429d7192013-10-04 19:53:11 +01002800 EMIT(store_locals);
Damien4b03e772013-10-05 14:17:09 +01002801 EMIT(load_id, comp->qstr___name__);
2802 EMIT(store_id, comp->qstr___module__);
Damiend99b0522013-12-21 18:17:45 +00002803 EMIT(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
Damien4b03e772013-10-05 14:17:09 +01002804 EMIT(store_id, comp->qstr___qualname__);
Damien429d7192013-10-04 19:53:11 +01002805
2806 check_for_doc_string(comp, pns->nodes[2]);
2807 compile_node(comp, pns->nodes[2]); // 2 is class body
2808
2809 id_info_t *id = scope_find(scope, comp->qstr___class__);
2810 assert(id != NULL);
2811 if (id->kind == ID_INFO_KIND_LOCAL) {
Damiend99b0522013-12-21 18:17:45 +00002812 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002813 } else {
Damien George6baf76e2013-12-30 22:32:17 +00002814#if MICROPY_EMIT_CPYTHON
Damien27fb45e2013-10-20 15:07:49 +01002815 EMIT(load_closure, comp->qstr___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002816#else
2817 EMIT(load_fast, comp->qstr___class__, 0); // XXX check this is the correct local num
2818#endif
Damien429d7192013-10-04 19:53:11 +01002819 }
2820 EMIT(return_value);
2821 }
2822
Damien415eb6f2013-10-05 12:19:06 +01002823 EMIT(end_pass);
Damienb05d7072013-10-05 13:37:10 +01002824
Damien826005c2013-10-05 23:17:28 +01002825}
2826
2827void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2828 comp->pass = pass;
2829 comp->scope_cur = scope;
2830 comp->next_label = 1;
2831
2832 if (scope->kind != SCOPE_FUNCTION) {
2833 printf("Error: inline assembler must be a function\n");
2834 return;
2835 }
2836
Damiena2f2f7d2013-10-06 00:14:13 +01002837 if (comp->pass > PASS_1) {
2838 EMIT_INLINE_ASM(start_pass, comp->pass, comp->scope_cur);
2839 }
2840
Damien826005c2013-10-05 23:17:28 +01002841 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00002842 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2843 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2844 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01002845
Damiend99b0522013-12-21 18:17:45 +00002846 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01002847
Damiena2f2f7d2013-10-06 00:14:13 +01002848 // parameters are in pns->nodes[1]
2849 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00002850 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01002851 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
2852 scope->num_params = EMIT_INLINE_ASM(count_params, n_params, pn_params);
2853 }
2854
Damiend99b0522013-12-21 18:17:45 +00002855 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01002856
Damiend99b0522013-12-21 18:17:45 +00002857 mp_parse_node_t pn_body = pns->nodes[3]; // body
2858 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01002859 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
2860
Damien826005c2013-10-05 23:17:28 +01002861 if (comp->pass == PASS_3) {
2862 //printf("----\n");
2863 scope_print_info(scope);
2864 }
2865
2866 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00002867 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
2868 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
2869 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
2870 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
2871 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
2872 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
2873 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
2874 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
2875 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
2876 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
2877 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
2878 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
2879 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01002880 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
2881
2882 // emit instructions
2883 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00002884 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien826005c2013-10-05 23:17:28 +01002885 printf("SyntaxError: inline assembler 'label' requires 1 argument\n");
2886 return;
2887 }
2888 int lab = comp_next_label(comp);
2889 if (pass > PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00002890 EMIT_INLINE_ASM(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01002891 }
2892 } else {
2893 if (pass > PASS_1) {
2894 EMIT_INLINE_ASM(op, op, n_args, pn_arg);
2895 }
2896 }
2897 }
2898
2899 if (comp->pass > PASS_1) {
2900 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01002901 }
Damien429d7192013-10-04 19:53:11 +01002902}
2903
2904void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
2905 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00002906 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01002907 scope->num_locals = 0;
2908 for (int i = 0; i < scope->id_info_len; i++) {
2909 id_info_t *id = &scope->id_info[i];
2910 if (scope->kind == SCOPE_CLASS && id->qstr == comp->qstr___class__) {
2911 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
2912 continue;
2913 }
2914 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
2915 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
2916 }
Damien9ecbcff2013-12-11 00:41:43 +00002917 // note: params always count for 1 local, even if they are a cell
Damien429d7192013-10-04 19:53:11 +01002918 if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
2919 id->local_num = scope->num_locals;
2920 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00002921 }
2922 }
2923
2924 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00002925#if MICROPY_EMIT_CPYTHON
2926 int num_cell = 0;
2927#endif
Damien9ecbcff2013-12-11 00:41:43 +00002928 for (int i = 0; i < scope->id_info_len; i++) {
2929 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00002930#if MICROPY_EMIT_CPYTHON
2931 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00002932 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00002933 id->local_num = num_cell;
2934 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00002935 }
Damien George6baf76e2013-12-30 22:32:17 +00002936#else
2937 // in Micro Python the cells come right after the fast locals
2938 // parameters are not counted here, since they remain at the start
2939 // of the locals, even if they are cell vars
2940 if (!id->param && id->kind == ID_INFO_KIND_CELL) {
2941 id->local_num = scope->num_locals;
2942 scope->num_locals += 1;
2943 }
2944#endif
Damien9ecbcff2013-12-11 00:41:43 +00002945 }
Damien9ecbcff2013-12-11 00:41:43 +00002946
2947 // compute the index of free vars (freevars[idx] in CPython)
2948 // make sure they are in the order of the parent scope
2949 if (scope->parent != NULL) {
2950 int num_free = 0;
2951 for (int i = 0; i < scope->parent->id_info_len; i++) {
2952 id_info_t *id = &scope->parent->id_info[i];
2953 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
2954 for (int j = 0; j < scope->id_info_len; j++) {
2955 id_info_t *id2 = &scope->id_info[j];
2956 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +00002957 assert(!id2->param); // free vars should not be params
2958#if MICROPY_EMIT_CPYTHON
2959 // in CPython the frees are numbered after the cells
2960 id2->local_num = num_cell + num_free;
2961#else
2962 // in Micro Python the frees come first, before the params
2963 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00002964#endif
2965 num_free += 1;
2966 }
2967 }
2968 }
Damien429d7192013-10-04 19:53:11 +01002969 }
Damien George6baf76e2013-12-30 22:32:17 +00002970#if !MICROPY_EMIT_CPYTHON
2971 // in Micro Python shift all other locals after the free locals
2972 if (num_free > 0) {
2973 for (int i = 0; i < scope->id_info_len; i++) {
2974 id_info_t *id = &scope->id_info[i];
2975 if (id->param || id->kind != ID_INFO_KIND_FREE) {
2976 id->local_num += num_free;
2977 }
2978 }
2979 scope->num_params += num_free; // free vars are counted as params for passing them into the function
2980 scope->num_locals += num_free;
2981 }
2982#endif
Damien429d7192013-10-04 19:53:11 +01002983 }
2984
2985 // compute flags
2986 //scope->flags = 0; since we set some things in parameters
2987 if (scope->kind != SCOPE_MODULE) {
2988 scope->flags |= SCOPE_FLAG_NEWLOCALS;
2989 }
2990 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) {
2991 assert(scope->parent != NULL);
2992 scope->flags |= SCOPE_FLAG_OPTIMISED;
2993
2994 // TODO possibly other ways it can be nested
2995 if (scope->parent->kind == SCOPE_FUNCTION || (scope->parent->kind == SCOPE_CLASS && scope->parent->parent->kind == SCOPE_FUNCTION)) {
2996 scope->flags |= SCOPE_FLAG_NESTED;
2997 }
2998 }
2999 int num_free = 0;
3000 for (int i = 0; i < scope->id_info_len; i++) {
3001 id_info_t *id = &scope->id_info[i];
3002 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3003 num_free += 1;
3004 }
3005 }
3006 if (num_free == 0) {
3007 scope->flags |= SCOPE_FLAG_NOFREE;
3008 }
3009}
3010
Damiend99b0522013-12-21 18:17:45 +00003011bool mp_compile(mp_parse_node_t pn, bool is_repl) {
Damien429d7192013-10-04 19:53:11 +01003012 compiler_t *comp = m_new(compiler_t, 1);
3013
Damien6cdd3af2013-10-05 18:08:26 +01003014 comp->qstr___class__ = qstr_from_str_static("__class__");
3015 comp->qstr___locals__ = qstr_from_str_static("__locals__");
3016 comp->qstr___name__ = qstr_from_str_static("__name__");
3017 comp->qstr___module__ = qstr_from_str_static("__module__");
3018 comp->qstr___qualname__ = qstr_from_str_static("__qualname__");
3019 comp->qstr___doc__ = qstr_from_str_static("__doc__");
3020 comp->qstr_assertion_error = qstr_from_str_static("AssertionError");
3021 comp->qstr_micropython = qstr_from_str_static("micropython");
Damien5ac1b2e2013-10-18 19:58:12 +01003022 comp->qstr_byte_code = qstr_from_str_static("byte_code");
Damien6cdd3af2013-10-05 18:08:26 +01003023 comp->qstr_native = qstr_from_str_static("native");
Damien7af3d192013-10-07 00:02:49 +01003024 comp->qstr_viper = qstr_from_str_static("viper");
Damien5bfb7592013-10-05 18:41:24 +01003025 comp->qstr_asm_thumb = qstr_from_str_static("asm_thumb");
Damiend7933892013-11-28 19:12:18 +00003026 comp->qstr_range = qstr_from_str_static("range");
Damien429d7192013-10-04 19:53:11 +01003027
Damien5ac1b2e2013-10-18 19:58:12 +01003028 comp->is_repl = is_repl;
3029 comp->had_error = false;
3030
Damien429d7192013-10-04 19:53:11 +01003031 comp->break_label = 0;
3032 comp->continue_label = 0;
3033 comp->except_nest_level = 0;
3034 comp->scope_head = NULL;
3035 comp->scope_cur = NULL;
3036
Damien826005c2013-10-05 23:17:28 +01003037 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003038 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003039
3040 // set the outer scope
Damien6cdd3af2013-10-05 18:08:26 +01003041 scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE);
Damien429d7192013-10-04 19:53:11 +01003042
Damien826005c2013-10-05 23:17:28 +01003043 // compile pass 1
3044 comp->emit = emit_pass1_new(comp->qstr___class__);
3045 comp->emit_method_table = &emit_pass1_method_table;
3046 comp->emit_inline_asm = NULL;
3047 comp->emit_inline_asm_method_table = NULL;
3048 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003049 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003050 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003051#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003052 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003053 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003054#endif
Damien826005c2013-10-05 23:17:28 +01003055 } else {
3056 compile_scope(comp, s, PASS_1);
3057 }
3058
3059 // update maximim number of labels needed
3060 if (comp->next_label > max_num_labels) {
3061 max_num_labels = comp->next_label;
3062 }
Damien429d7192013-10-04 19:53:11 +01003063 }
3064
Damien826005c2013-10-05 23:17:28 +01003065 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003066 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003067 compile_scope_compute_things(comp, s);
3068 }
3069
Damien826005c2013-10-05 23:17:28 +01003070 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003071 emit_pass1_free(comp->emit);
3072
Damien826005c2013-10-05 23:17:28 +01003073 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003074#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003075 emit_t *emit_bc = NULL;
Damiendc833822013-10-06 01:01:01 +01003076 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003077#endif
Damien3ef4abb2013-10-12 16:53:13 +01003078#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003079 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003080#endif
Damien5ac1b2e2013-10-18 19:58:12 +01003081 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003082 if (false) {
3083 // dummy
3084
Damien3ef4abb2013-10-12 16:53:13 +01003085#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003086 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
3087 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003088 if (emit_inline_thumb == NULL) {
3089 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3090 }
3091 comp->emit = NULL;
3092 comp->emit_method_table = NULL;
3093 comp->emit_inline_asm = emit_inline_thumb;
3094 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3095 compile_scope_inline_asm(comp, s, PASS_2);
3096 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003097#endif
3098
Damien826005c2013-10-05 23:17:28 +01003099 } else {
Damienc025ebb2013-10-12 14:30:21 +01003100
3101 // choose the emit type
3102
Damien3ef4abb2013-10-12 16:53:13 +01003103#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003104 comp->emit = emit_cpython_new(max_num_labels);
3105 comp->emit_method_table = &emit_cpython_method_table;
3106#else
Damien826005c2013-10-05 23:17:28 +01003107 switch (s->emit_options) {
3108 case EMIT_OPT_NATIVE_PYTHON:
Damien3410be82013-10-07 23:09:10 +01003109 case EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003110#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003111 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003112 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003113 }
Damien13ed3a62013-10-08 09:05:10 +01003114 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003115#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003116 if (emit_native == NULL) {
3117 emit_native = emit_native_thumb_new(max_num_labels);
3118 }
3119 comp->emit_method_table = &emit_native_thumb_method_table;
3120#endif
3121 comp->emit = emit_native;
Damien3410be82013-10-07 23:09:10 +01003122 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003123 break;
3124
Damien826005c2013-10-05 23:17:28 +01003125 default:
3126 if (emit_bc == NULL) {
3127 emit_bc = emit_bc_new(max_num_labels);
3128 }
3129 comp->emit = emit_bc;
3130 comp->emit_method_table = &emit_bc_method_table;
3131 break;
3132 }
Damienc025ebb2013-10-12 14:30:21 +01003133#endif
3134
3135 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003136 compile_scope(comp, s, PASS_2);
3137 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003138 }
Damien429d7192013-10-04 19:53:11 +01003139 }
3140
Damien732407f2013-12-29 19:33:23 +00003141 m_del_obj(compiler_t, comp);
Damien5ac1b2e2013-10-18 19:58:12 +01003142
3143 return !comp->had_error;
Damien429d7192013-10-04 19:53:11 +01003144}