blob: 62c07cfd8c7d0661ea7a457bab7a7779f6a20cc8 [file] [log] [blame]
xbeefe34222014-03-16 00:14:26 -07001#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +01002#include <stdint.h>
3#include <stdio.h>
4#include <string.h>
5#include <assert.h>
Rachel Dowdall56402792014-03-22 20:19:24 +00006#include <math.h>
Damien429d7192013-10-04 19:53:11 +01007
8#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00009#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000010#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010011#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010012#include "parse.h"
13#include "scope.h"
Damiend99b0522013-12-21 18:17:45 +000014#include "runtime0.h"
Damien429d7192013-10-04 19:53:11 +010015#include "emit.h"
Damien George2326d522014-03-27 23:26:35 +000016#include "emitglue.h"
Damien George1fb03172014-01-03 14:22:03 +000017#include "obj.h"
18#include "compile.h"
19#include "runtime.h"
Damien Georgeecf5b772014-04-04 11:13:51 +000020#include "smallint.h"
Damien429d7192013-10-04 19:53:11 +010021
22// TODO need to mangle __attr names
23
Damience89a212013-10-15 22:25:17 +010024#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_THUMB)
25
Damien429d7192013-10-04 19:53:11 +010026typedef enum {
27 PN_none = 0,
Damien George00208ce2014-01-23 00:00:53 +000028#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien429d7192013-10-04 19:53:11 +010029#include "grammar.h"
30#undef DEF_RULE
31 PN_maximum_number_of,
32} pn_kind_t;
33
Damien Georgeb9791222014-01-23 00:34:21 +000034#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
35#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
36#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
37#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010038
39typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000040 qstr source_file;
Damien5ac1b2e2013-10-18 19:58:12 +010041 bool is_repl;
Damien429d7192013-10-04 19:53:11 +010042 pass_kind_t pass;
Damien5ac1b2e2013-10-18 19:58:12 +010043 bool had_error; // try to keep compiler clean from nlr
Damien429d7192013-10-04 19:53:11 +010044
Damienb05d7072013-10-05 13:37:10 +010045 int next_label;
Damienb05d7072013-10-05 13:37:10 +010046
Damien429d7192013-10-04 19:53:11 +010047 int break_label;
48 int continue_label;
Damien Georgecbddb272014-02-01 20:08:18 +000049 int break_continue_except_level;
50 int cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien429d7192013-10-04 19:53:11 +010051
52 int n_arg_keyword;
53 bool have_star_arg;
54 bool have_dbl_star_arg;
55 bool have_bare_star;
56 int param_pass;
57 int param_pass_num_dict_params;
58 int param_pass_num_default_params;
59
Damien George35e2a4e2014-02-05 00:51:47 +000060 bool func_arg_is_super; // used to compile special case of super() function call
61
Damien429d7192013-10-04 19:53:11 +010062 scope_t *scope_head;
63 scope_t *scope_cur;
64
Damien6cdd3af2013-10-05 18:08:26 +010065 emit_t *emit; // current emitter
66 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010067
68 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
69 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 +010070} compiler_t;
71
Damien Georgef41fdd02014-03-03 23:19:11 +000072STATIC void compile_syntax_error(compiler_t *comp, const char *msg) {
73 // TODO store the error message to a variable in compiler_t instead of printing it
74 printf("SyntaxError: %s\n", msg);
75 comp->had_error = true;
76}
77
Damiend99b0522013-12-21 18:17:45 +000078mp_parse_node_t fold_constants(mp_parse_node_t pn) {
79 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
80 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
81 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +010082
83 // fold arguments first
84 for (int i = 0; i < n; i++) {
85 pns->nodes[i] = fold_constants(pns->nodes[i]);
86 }
87
Damiend99b0522013-12-21 18:17:45 +000088 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +010089 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +000090 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +020091 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
92 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +000093 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien3ef4abb2013-10-12 16:53:13 +010094#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +010095 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +000096 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
Damien0efb3a12013-10-12 16:16:56 +010097#endif
Damiend99b0522013-12-21 18:17:45 +000098 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
99 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100100 } else {
101 // shouldn't happen
102 assert(0);
103 }
104 }
105 break;
106
107 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100108 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000109 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200110 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
111 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000112 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000113 arg0 += arg1;
Damiend99b0522013-12-21 18:17:45 +0000114 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000115 arg0 -= arg1;
Damien429d7192013-10-04 19:53:11 +0100116 } else {
117 // shouldn't happen
118 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100119 }
Damien Georgeaf272592014-04-04 11:21:58 +0000120 if (MP_PARSE_FITS_SMALL_INT(arg0)) {
121 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
Damien429d7192013-10-04 19:53:11 +0100122 }
123 }
124 break;
125
126 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000127 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Damien Georgeaf272592014-04-04 11:21:58 +0000128 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
129 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000130 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgeaf272592014-04-04 11:21:58 +0000131 if (!mp_small_int_mul_overflow(arg0, arg1)) {
132 arg0 *= arg1;
133 if (MP_PARSE_FITS_SMALL_INT(arg0)) {
134 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
135 }
136 }
Damiend99b0522013-12-21 18:17:45 +0000137 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien429d7192013-10-04 19:53:11 +0100138 ; // pass
Damiend99b0522013-12-21 18:17:45 +0000139 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien Georgeecf5b772014-04-04 11:13:51 +0000140 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000141 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300142 if (arg1 != 0) {
Damien Georgeecf5b772014-04-04 11:13:51 +0000143 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_floor_divide(arg0, arg1));
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300144 }
Damien429d7192013-10-04 19:53:11 +0100145 } else {
146 // shouldn't happen
147 assert(0);
148 }
149 }
150 break;
151
152 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000153 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200154 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000155 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
156 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
157 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
158 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
159 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
160 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100161 } else {
162 // shouldn't happen
163 assert(0);
164 }
165 }
166 break;
167
Damien3ef4abb2013-10-12 16:53:13 +0100168#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +0100169 case PN_power:
Damien0efb3a12013-10-12 16:16:56 +0100170 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000171 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])) {
172 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
173 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200174 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100175 if (power >= 0) {
176 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200177 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100178 for (; power > 0; power--) {
179 ans *= base;
180 }
Damiend99b0522013-12-21 18:17:45 +0000181 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100182 }
183 }
184 }
185 break;
Damien0efb3a12013-10-12 16:16:56 +0100186#endif
Damien429d7192013-10-04 19:53:11 +0100187 }
188 }
189
190 return pn;
191}
192
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200193STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra);
Damien George8dcc0c72014-03-27 10:55:21 +0000194STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100195
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200196STATIC int comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100197 return comp->next_label++;
198}
199
Damien George8dcc0c72014-03-27 10:55:21 +0000200STATIC void compile_increase_except_level(compiler_t *comp) {
201 comp->cur_except_level += 1;
202 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
203 comp->scope_cur->exc_stack_size = comp->cur_except_level;
204 }
205}
206
207STATIC void compile_decrease_except_level(compiler_t *comp) {
208 assert(comp->cur_except_level > 0);
209 comp->cur_except_level -= 1;
210}
211
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200212STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) {
Damien George2326d522014-03-27 23:26:35 +0000213 scope_t *scope = scope_new(kind, pn, comp->source_file, mp_emit_glue_get_unique_code_id(), emit_options);
Damien429d7192013-10-04 19:53:11 +0100214 scope->parent = comp->scope_cur;
215 scope->next = NULL;
216 if (comp->scope_head == NULL) {
217 comp->scope_head = scope;
218 } else {
219 scope_t *s = comp->scope_head;
220 while (s->next != NULL) {
221 s = s->next;
222 }
223 s->next = scope;
224 }
225 return scope;
226}
227
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200228STATIC int list_len(mp_parse_node_t pn, int pn_kind) {
Damiend99b0522013-12-21 18:17:45 +0000229 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100230 return 0;
Damiend99b0522013-12-21 18:17:45 +0000231 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100232 return 1;
233 } else {
Damiend99b0522013-12-21 18:17:45 +0000234 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
235 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100236 return 1;
237 } else {
Damiend99b0522013-12-21 18:17:45 +0000238 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100239 }
240 }
241}
242
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200243STATIC 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)) {
Damiend99b0522013-12-21 18:17:45 +0000244 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
245 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
246 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100247 for (int i = 0; i < num_nodes; i++) {
248 f(comp, pns->nodes[i]);
249 }
Damiend99b0522013-12-21 18:17:45 +0000250 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100251 f(comp, pn);
252 }
253}
254
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200255STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000256 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100257 *nodes = NULL;
258 return 0;
Damiend99b0522013-12-21 18:17:45 +0000259 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100260 *nodes = pn;
261 return 1;
262 } else {
Damiend99b0522013-12-21 18:17:45 +0000263 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
264 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100265 *nodes = pn;
266 return 1;
267 } else {
268 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000269 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100270 }
271 }
272}
273
Damiend99b0522013-12-21 18:17:45 +0000274void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100275}
276
Damiend99b0522013-12-21 18:17:45 +0000277void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
278 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100279 for (int i = 0; i < num_nodes; i++) {
280 compile_node(comp, pns->nodes[i]);
281 }
282}
283
Damien3ef4abb2013-10-12 16:53:13 +0100284#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200285STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000286 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100287 return false;
288 }
Damiend99b0522013-12-21 18:17:45 +0000289 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100290 return false;
291 }
292 return true;
293}
294
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200295STATIC void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
Damien George55baff42014-01-21 21:40:13 +0000296 uint len;
297 const byte *str = qstr_data(qstr, &len);
Damien02f89412013-12-12 15:13:36 +0000298 bool has_single_quote = false;
299 bool has_double_quote = false;
300 for (int i = 0; i < len; i++) {
301 if (str[i] == '\'') {
302 has_single_quote = true;
303 } else if (str[i] == '"') {
304 has_double_quote = true;
305 }
306 }
307 if (bytes) {
308 vstr_printf(vstr, "b");
309 }
310 bool quote_single = false;
311 if (has_single_quote && !has_double_quote) {
312 vstr_printf(vstr, "\"");
313 } else {
314 quote_single = true;
315 vstr_printf(vstr, "'");
316 }
317 for (int i = 0; i < len; i++) {
318 if (str[i] == '\n') {
319 vstr_printf(vstr, "\\n");
320 } else if (str[i] == '\\') {
321 vstr_printf(vstr, "\\\\");
322 } else if (str[i] == '\'' && quote_single) {
323 vstr_printf(vstr, "\\'");
324 } else {
325 vstr_printf(vstr, "%c", str[i]);
326 }
327 }
328 if (has_single_quote && !has_double_quote) {
329 vstr_printf(vstr, "\"");
330 } else {
331 vstr_printf(vstr, "'");
332 }
333}
334
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200335STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damiend99b0522013-12-21 18:17:45 +0000336 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200337 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
338 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
339 return;
340 }
341
Damiend99b0522013-12-21 18:17:45 +0000342 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
343 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
344 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000345 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
346 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
347 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
348 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
349 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100350 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000351 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
352 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
353 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien429d7192013-10-04 19:53:11 +0100354 default: assert(0);
355 }
356 break;
357 default: assert(0);
358 }
359}
360
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200361STATIC 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 +0100362 int n = 0;
363 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000364 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100365 }
366 int total = n;
367 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000368 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100369 total += 1;
Damien3a205172013-10-12 15:01:56 +0100370 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100371 is_const = false;
372 }
373 }
374 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100375 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100376 is_const = false;
377 break;
378 }
379 }
380 if (total > 0 && is_const) {
381 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000382 vstr_t *vstr = vstr_new();
383 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000384 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000385 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100386 need_comma = true;
387 }
388 for (int i = 0; i < n; i++) {
389 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000390 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100391 }
Damien02f89412013-12-12 15:13:36 +0000392 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100393 need_comma = true;
394 }
395 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000396 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100397 } else {
Damien02f89412013-12-12 15:13:36 +0000398 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100399 }
Damien Georgeb9791222014-01-23 00:34:21 +0000400 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000401 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100402 } else {
Damiend99b0522013-12-21 18:17:45 +0000403 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100404 compile_node(comp, pn);
405 }
406 for (int i = 0; i < n; i++) {
407 compile_node(comp, pns_list->nodes[i]);
408 }
Damien Georgeb9791222014-01-23 00:34:21 +0000409 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100410 }
411}
Damien3a205172013-10-12 15:01:56 +0100412#endif
413
414// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damiend99b0522013-12-21 18:17:45 +0000415void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100416#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100417 cpython_c_tuple(comp, pn, pns_list);
418#else
419 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000420 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100421 compile_node(comp, pn);
422 total += 1;
423 }
424 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000425 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100426 for (int i = 0; i < n; i++) {
427 compile_node(comp, pns_list->nodes[i]);
428 }
429 total += n;
430 }
Damien Georgeb9791222014-01-23 00:34:21 +0000431 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100432#endif
433}
Damien429d7192013-10-04 19:53:11 +0100434
Damiend99b0522013-12-21 18:17:45 +0000435void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100436 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000437 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100438}
439
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200440STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000441 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200442 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100443}
444
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200445STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200446 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE) || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 1);
Damien429d7192013-10-04 19:53:11 +0100447}
448
Damien3ef4abb2013-10-12 16:53:13 +0100449#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100450// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200451STATIC 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 +0100452 if (node_is_const_false(pn)) {
453 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000454 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100455 }
456 return;
457 } else if (node_is_const_true(pn)) {
458 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000459 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100460 }
461 return;
Damiend99b0522013-12-21 18:17:45 +0000462 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
463 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
464 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
465 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100466 if (jump_if == false) {
Damienb05d7072013-10-05 13:37:10 +0100467 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100468 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100469 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100470 }
Damien3a205172013-10-12 15:01:56 +0100471 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000472 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100473 } else {
474 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100475 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100476 }
477 }
478 return;
Damiend99b0522013-12-21 18:17:45 +0000479 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100480 if (jump_if == false) {
481 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100482 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100483 }
484 } else {
Damienb05d7072013-10-05 13:37:10 +0100485 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100486 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100487 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100488 }
Damien3a205172013-10-12 15:01:56 +0100489 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000490 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100491 }
492 return;
Damiend99b0522013-12-21 18:17:45 +0000493 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100494 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100495 return;
496 }
497 }
498
499 // nothing special, fall back to default compiling for node and jump
500 compile_node(comp, pn);
501 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000502 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100503 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000504 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100505 }
506}
Damien3a205172013-10-12 15:01:56 +0100507#endif
Damien429d7192013-10-04 19:53:11 +0100508
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200509STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100510#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100511 cpython_c_if_cond(comp, pn, jump_if, label, false);
512#else
513 if (node_is_const_false(pn)) {
514 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000515 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100516 }
517 return;
518 } else if (node_is_const_true(pn)) {
519 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000520 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100521 }
522 return;
Damiend99b0522013-12-21 18:17:45 +0000523 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
524 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
525 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
526 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100527 if (jump_if == false) {
528 int label2 = comp_next_label(comp);
529 for (int i = 0; i < n - 1; i++) {
530 c_if_cond(comp, pns->nodes[i], true, label2);
531 }
532 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000533 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100534 } else {
535 for (int i = 0; i < n; i++) {
536 c_if_cond(comp, pns->nodes[i], true, label);
537 }
538 }
539 return;
Damiend99b0522013-12-21 18:17:45 +0000540 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100541 if (jump_if == false) {
542 for (int i = 0; i < n; i++) {
543 c_if_cond(comp, pns->nodes[i], false, label);
544 }
545 } else {
546 int label2 = comp_next_label(comp);
547 for (int i = 0; i < n - 1; i++) {
548 c_if_cond(comp, pns->nodes[i], false, label2);
549 }
550 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000551 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100552 }
553 return;
Damiend99b0522013-12-21 18:17:45 +0000554 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100555 c_if_cond(comp, pns->nodes[0], !jump_if, label);
556 return;
557 }
558 }
559
560 // nothing special, fall back to default compiling for node and jump
561 compile_node(comp, pn);
562 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000563 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100564 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000565 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100566 }
567#endif
Damien429d7192013-10-04 19:53:11 +0100568}
569
570typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000571void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100572
Damiend99b0522013-12-21 18:17:45 +0000573void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100574 if (assign_kind != ASSIGN_AUG_STORE) {
575 compile_node(comp, pns->nodes[0]);
576 }
577
Damiend99b0522013-12-21 18:17:45 +0000578 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
579 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
580 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
581 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100582 if (assign_kind != ASSIGN_AUG_STORE) {
583 for (int i = 0; i < n - 1; i++) {
584 compile_node(comp, pns1->nodes[i]);
585 }
586 }
Damiend99b0522013-12-21 18:17:45 +0000587 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
588 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100589 }
Damiend99b0522013-12-21 18:17:45 +0000590 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000591 compile_syntax_error(comp, "can't assign to function call");
Damien429d7192013-10-04 19:53:11 +0100592 return;
Damiend99b0522013-12-21 18:17:45 +0000593 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100594 if (assign_kind == ASSIGN_AUG_STORE) {
595 EMIT(rot_three);
596 EMIT(store_subscr);
597 } else {
598 compile_node(comp, pns1->nodes[0]);
599 if (assign_kind == ASSIGN_AUG_LOAD) {
600 EMIT(dup_top_two);
Damien Georged17926d2014-03-30 13:35:08 +0100601 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100602 } else {
603 EMIT(store_subscr);
604 }
605 }
Damiend99b0522013-12-21 18:17:45 +0000606 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
607 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100608 if (assign_kind == ASSIGN_AUG_LOAD) {
609 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000610 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100611 } else {
612 if (assign_kind == ASSIGN_AUG_STORE) {
613 EMIT(rot_two);
614 }
Damien Georgeb9791222014-01-23 00:34:21 +0000615 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100616 }
617 } else {
618 // shouldn't happen
619 assert(0);
620 }
621 } else {
622 // shouldn't happen
623 assert(0);
624 }
625
Damiend99b0522013-12-21 18:17:45 +0000626 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +0100627 // SyntaxError, cannot assign
628 assert(0);
629 }
630}
631
Damiend99b0522013-12-21 18:17:45 +0000632void c_assign_tuple(compiler_t *comp, int n, mp_parse_node_t *nodes) {
Damien429d7192013-10-04 19:53:11 +0100633 assert(n >= 0);
634 int have_star_index = -1;
635 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000636 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100637 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000638 EMIT_ARG(unpack_ex, i, n - i - 1);
Damien429d7192013-10-04 19:53:11 +0100639 have_star_index = i;
640 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000641 compile_syntax_error(comp, "two starred expressions in assignment");
Damien429d7192013-10-04 19:53:11 +0100642 return;
643 }
644 }
645 }
646 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000647 EMIT_ARG(unpack_sequence, n);
Damien429d7192013-10-04 19:53:11 +0100648 }
649 for (int i = 0; i < n; i++) {
650 if (i == have_star_index) {
Damiend99b0522013-12-21 18:17:45 +0000651 c_assign(comp, ((mp_parse_node_struct_t*)nodes[i])->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100652 } else {
653 c_assign(comp, nodes[i], ASSIGN_STORE);
654 }
655 }
656}
657
658// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000659void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100660 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000661 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100662 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000663 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
664 if (MP_PARSE_NODE_IS_ID(pn)) {
665 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100666 switch (assign_kind) {
667 case ASSIGN_STORE:
668 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000669 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100670 break;
671 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000672 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100673 break;
674 }
675 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000676 compile_syntax_error(comp, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100677 return;
678 }
679 } else {
Damiend99b0522013-12-21 18:17:45 +0000680 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
681 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100682 case PN_power:
683 // lhs is an index or attribute
684 c_assign_power(comp, pns, assign_kind);
685 break;
686
687 case PN_testlist_star_expr:
688 case PN_exprlist:
689 // lhs is a tuple
690 if (assign_kind != ASSIGN_STORE) {
691 goto bad_aug;
692 }
Damiend99b0522013-12-21 18:17:45 +0000693 c_assign_tuple(comp, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100694 break;
695
696 case PN_atom_paren:
697 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000698 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100699 // empty tuple
Damien Georgef41fdd02014-03-03 23:19:11 +0000700 compile_syntax_error(comp, "can't assign to ()");
Damien429d7192013-10-04 19:53:11 +0100701 return;
Damiend99b0522013-12-21 18:17:45 +0000702 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
703 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100704 goto testlist_comp;
705 } else {
706 // parenthesis around 1 item, is just that item
707 pn = pns->nodes[0];
708 goto tail_recursion;
709 }
710 break;
711
712 case PN_atom_bracket:
713 // lhs is something in brackets
714 if (assign_kind != ASSIGN_STORE) {
715 goto bad_aug;
716 }
Damiend99b0522013-12-21 18:17:45 +0000717 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100718 // empty list, assignment allowed
719 c_assign_tuple(comp, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000720 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
721 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100722 goto testlist_comp;
723 } else {
724 // brackets around 1 item
725 c_assign_tuple(comp, 1, &pns->nodes[0]);
726 }
727 break;
728
729 default:
Damiend99b0522013-12-21 18:17:45 +0000730 printf("unknown assign, %u\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien429d7192013-10-04 19:53:11 +0100731 assert(0);
732 }
733 return;
734
735 testlist_comp:
736 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000737 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
738 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
739 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100740 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000741 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100742 c_assign_tuple(comp, 1, &pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000743 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100744 // sequence of many items
745 // TODO call c_assign_tuple instead
Damiend99b0522013-12-21 18:17:45 +0000746 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
Damien Georgeb9791222014-01-23 00:34:21 +0000747 EMIT_ARG(unpack_sequence, 1 + n);
Damien429d7192013-10-04 19:53:11 +0100748 c_assign(comp, pns->nodes[0], ASSIGN_STORE);
749 for (int i = 0; i < n; i++) {
750 c_assign(comp, pns2->nodes[i], ASSIGN_STORE);
751 }
Damiend99b0522013-12-21 18:17:45 +0000752 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +0100753 // TODO not implemented
754 assert(0);
755 } else {
756 // sequence with 2 items
757 goto sequence_with_2_items;
758 }
759 } else {
760 // sequence with 2 items
761 sequence_with_2_items:
762 c_assign_tuple(comp, 2, pns->nodes);
763 }
764 return;
765 }
766 return;
767
768 bad_aug:
Damien Georgef41fdd02014-03-03 23:19:11 +0000769 compile_syntax_error(comp, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100770}
771
772// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100773// if we are not in CPython compatibility mode then:
774// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
775// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
776// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George30565092014-03-31 11:30:17 +0100777void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
778 assert(n_pos_defaults >= 0);
779 assert(n_kw_defaults >= 0);
780
Damien429d7192013-10-04 19:53:11 +0100781 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000782 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100783 int nfree = 0;
784 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000785 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
786 id_info_t *id = &comp->scope_cur->id_info[i];
787 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
788 for (int j = 0; j < this_scope->id_info_len; j++) {
789 id_info_t *id2 = &this_scope->id_info[j];
790 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000791#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000792 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000793#else
794 // in Micro Python we load closures using LOAD_FAST
Damien Georgeb9791222014-01-23 00:34:21 +0000795 EMIT_ARG(load_fast, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000796#endif
Damien318aec62013-12-10 18:28:17 +0000797 nfree += 1;
798 }
799 }
Damien429d7192013-10-04 19:53:11 +0100800 }
801 }
802 }
Damien429d7192013-10-04 19:53:11 +0100803
804 // make the function/closure
805 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100806 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100807 } else {
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000808 EMIT_ARG(build_tuple, nfree);
Damien George30565092014-03-31 11:30:17 +0100809 EMIT_ARG(make_closure, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100810 }
811}
812
Damiend99b0522013-12-21 18:17:45 +0000813void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000814 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damiend99b0522013-12-21 18:17:45 +0000815 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
816 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100817 // bare star
818 comp->have_bare_star = true;
819 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000820
821 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
822 // TODO do we need to do anything with this?
823
824 } else {
825 mp_parse_node_t pn_id;
826 mp_parse_node_t pn_colon;
827 mp_parse_node_t pn_equal;
828 if (MP_PARSE_NODE_IS_ID(pn)) {
829 // this parameter is just an id
830
831 pn_id = pn;
832 pn_colon = MP_PARSE_NODE_NULL;
833 pn_equal = MP_PARSE_NODE_NULL;
834
835 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
836 // this parameter has a colon and/or equal specifier
837
838 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
839 pn_id = pns->nodes[0];
840 pn_colon = pns->nodes[1];
841 pn_equal = pns->nodes[2];
842
843 } else {
844 assert(0);
845 return;
846 }
847
848 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
849 // this parameter does not have a default value
850
851 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
852 if (!comp->have_bare_star && comp->param_pass_num_default_params != 0) {
853 compile_syntax_error(comp, "non-default argument follows default argument");
854 return;
855 }
856
857 } else {
858 // this parameter has a default value
859 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
860
861 if (comp->have_bare_star) {
862 comp->param_pass_num_dict_params += 1;
863 if (comp->param_pass == 1) {
Damien Georgee337f1e2014-03-31 15:18:37 +0100864#if !MICROPY_EMIT_CPYTHON
865 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
866 if (comp->param_pass_num_dict_params == 1) {
867 // first default dict param, so make the map
868 EMIT_ARG(build_map, 0);
869 }
870#endif
Damien Georgef41fdd02014-03-03 23:19:11 +0000871 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pn_id));
872 compile_node(comp, pn_equal);
Damien Georgee337f1e2014-03-31 15:18:37 +0100873#if !MICROPY_EMIT_CPYTHON
874 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
875 EMIT(store_map);
876#endif
Damien Georgef41fdd02014-03-03 23:19:11 +0000877 }
878 } else {
879 comp->param_pass_num_default_params += 1;
880 if (comp->param_pass == 2) {
881 compile_node(comp, pn_equal);
882 }
883 }
884 }
885
886 // TODO pn_colon not implemented
887 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100888 }
889}
890
891// leaves function object on stack
892// returns function name
Damiend99b0522013-12-21 18:17:45 +0000893qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100894 if (comp->pass == PASS_1) {
895 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000896 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100897 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000898 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100899 }
900
901 // save variables (probably don't need to do this, since we can't have nested definitions..?)
902 bool old_have_bare_star = comp->have_bare_star;
903 int old_param_pass = comp->param_pass;
904 int old_param_pass_num_dict_params = comp->param_pass_num_dict_params;
905 int old_param_pass_num_default_params = comp->param_pass_num_default_params;
906
907 // compile default parameters
Damien Georgef41fdd02014-03-03 23:19:11 +0000908
909 // pass 1 does any default parameters after bare star
Damien429d7192013-10-04 19:53:11 +0100910 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000911 comp->param_pass = 1;
Damien429d7192013-10-04 19:53:11 +0100912 comp->param_pass_num_dict_params = 0;
913 comp->param_pass_num_default_params = 0;
914 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +0000915
916 if (comp->had_error) {
917 return MP_QSTR_NULL;
918 }
919
920 // pass 2 does any default parameters before bare star
Damien429d7192013-10-04 19:53:11 +0100921 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000922 comp->param_pass = 2;
Damien429d7192013-10-04 19:53:11 +0100923 comp->param_pass_num_dict_params = 0;
924 comp->param_pass_num_default_params = 0;
925 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
926
Damien Georgee337f1e2014-03-31 15:18:37 +0100927#if !MICROPY_EMIT_CPYTHON
928 // in Micro Python we put the default positional parameters into a tuple using the bytecode
929 if (comp->param_pass_num_default_params > 0) {
930 EMIT_ARG(build_tuple, comp->param_pass_num_default_params);
931 }
932#endif
933
Damien429d7192013-10-04 19:53:11 +0100934 // get the scope for this function
935 scope_t *fscope = (scope_t*)pns->nodes[4];
936
937 // make the function
Damien George30565092014-03-31 11:30:17 +0100938 close_over_variables_etc(comp, fscope, comp->param_pass_num_default_params, comp->param_pass_num_dict_params);
Damien429d7192013-10-04 19:53:11 +0100939
940 // restore variables
941 comp->have_bare_star = old_have_bare_star;
942 comp->param_pass = old_param_pass;
943 comp->param_pass_num_dict_params = old_param_pass_num_dict_params;
944 comp->param_pass_num_default_params = old_param_pass_num_default_params;
945
946 // return its name (the 'f' in "def f(...):")
947 return fscope->simple_name;
948}
949
950// leaves class object on stack
951// returns class name
Damiend99b0522013-12-21 18:17:45 +0000952qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100953 if (comp->pass == PASS_1) {
954 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000955 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100956 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000957 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100958 }
959
960 EMIT(load_build_class);
961
962 // scope for this class
963 scope_t *cscope = (scope_t*)pns->nodes[3];
964
965 // compile the class
966 close_over_variables_etc(comp, cscope, 0, 0);
967
968 // get its name
Damien Georgeb9791222014-01-23 00:34:21 +0000969 EMIT_ARG(load_const_id, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100970
971 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +0100972 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
973 mp_parse_node_t parents = pns->nodes[1];
974 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
975 parents = MP_PARSE_NODE_NULL;
976 }
Damien Georgebbcd49a2014-02-06 20:30:16 +0000977 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +0100978 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +0100979
980 // return its name (the 'C' in class C(...):")
981 return cscope->simple_name;
982}
983
Damien6cdd3af2013-10-05 18:08:26 +0100984// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200985STATIC bool compile_built_in_decorator(compiler_t *comp, int name_len, mp_parse_node_t *name_nodes, uint *emit_options) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000986 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100987 return false;
988 }
989
990 if (name_len != 2) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000991 compile_syntax_error(comp, "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100992 return true;
993 }
994
Damiend99b0522013-12-21 18:17:45 +0000995 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000996 if (attr == MP_QSTR_byte_code) {
Damien George65cad122014-04-06 11:48:15 +0100997 *emit_options = MP_EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +0100998#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000999 } else if (attr == MP_QSTR_native) {
Damien George65cad122014-04-06 11:48:15 +01001000 *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001001 } else if (attr == MP_QSTR_viper) {
Damien George65cad122014-04-06 11:48:15 +01001002 *emit_options = MP_EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001003#endif
Damien3ef4abb2013-10-12 16:53:13 +01001004#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001005 } else if (attr == MP_QSTR_asm_thumb) {
Damien George65cad122014-04-06 11:48:15 +01001006 *emit_options = MP_EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001007#endif
Damien6cdd3af2013-10-05 18:08:26 +01001008 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +00001009 compile_syntax_error(comp, "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001010 }
1011
1012 return true;
1013}
1014
Damiend99b0522013-12-21 18:17:45 +00001015void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001016 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001017 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001018 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1019
Damien6cdd3af2013-10-05 18:08:26 +01001020 // inherit emit options for this function/class definition
1021 uint emit_options = comp->scope_cur->emit_options;
1022
1023 // compile each decorator
1024 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001025 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001026 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1027 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001028
1029 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001030 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001031 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1032
1033 // check for built-in decorators
1034 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1035 // this was a built-in
1036 num_built_in_decorators += 1;
1037
1038 } else {
1039 // not a built-in, compile normally
1040
1041 // compile the decorator function
1042 compile_node(comp, name_nodes[0]);
1043 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001044 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001045 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001046 }
1047
1048 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001049 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001050 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001051 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001052 compile_node(comp, pns_decorator->nodes[1]);
1053 }
Damien429d7192013-10-04 19:53:11 +01001054 }
1055 }
1056
1057 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001058 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001059 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001060 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001061 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001062 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001063 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001064 } else {
1065 // shouldn't happen
1066 assert(0);
1067 }
1068
1069 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001070 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001071 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01001072 }
1073
1074 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001075 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001076}
1077
Damiend99b0522013-12-21 18:17:45 +00001078void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001079 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001080 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001081 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001082}
1083
Damiend99b0522013-12-21 18:17:45 +00001084void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1085 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001086 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001087 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1088 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001089
1090 compile_node(comp, pns->nodes[0]); // base of the power node
1091
Damiend99b0522013-12-21 18:17:45 +00001092 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1093 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1094 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1095 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001096 for (int i = 0; i < n - 1; i++) {
1097 compile_node(comp, pns1->nodes[i]);
1098 }
Damiend99b0522013-12-21 18:17:45 +00001099 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1100 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001101 }
Damiend99b0522013-12-21 18:17:45 +00001102 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien429d7192013-10-04 19:53:11 +01001103 // SyntaxError: can't delete a function call
1104 assert(0);
Damiend99b0522013-12-21 18:17:45 +00001105 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001106 compile_node(comp, pns1->nodes[0]);
1107 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001108 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1109 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001110 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001111 } else {
1112 // shouldn't happen
1113 assert(0);
1114 }
1115 } else {
1116 // shouldn't happen
1117 assert(0);
1118 }
1119
Damiend99b0522013-12-21 18:17:45 +00001120 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001121 // SyntaxError, cannot delete
1122 assert(0);
1123 }
Damiend99b0522013-12-21 18:17:45 +00001124 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1125 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1126 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1127 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001128 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1129
Damiend99b0522013-12-21 18:17:45 +00001130 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1131 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1132 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001133 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001134 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001135 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001136 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001137 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001138 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001139 c_del_stmt(comp, pns->nodes[0]);
1140 for (int i = 0; i < n; i++) {
1141 c_del_stmt(comp, pns1->nodes[i]);
1142 }
Damiend99b0522013-12-21 18:17:45 +00001143 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001144 // TODO not implemented; can't del comprehension?
1145 assert(0);
1146 } else {
1147 // sequence with 2 items
1148 goto sequence_with_2_items;
1149 }
1150 } else {
1151 // sequence with 2 items
1152 sequence_with_2_items:
1153 c_del_stmt(comp, pns->nodes[0]);
1154 c_del_stmt(comp, pns->nodes[1]);
1155 }
1156 } else {
1157 // tuple with 1 element
1158 c_del_stmt(comp, pn);
1159 }
1160 } else {
1161 // not implemented
1162 assert(0);
1163 }
1164}
1165
Damiend99b0522013-12-21 18:17:45 +00001166void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001167 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1168}
1169
Damiend99b0522013-12-21 18:17:45 +00001170void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001171 if (comp->break_label == 0) {
Damien George2326d522014-03-27 23:26:35 +00001172 compile_syntax_error(comp, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001173 }
Damien Georgecbddb272014-02-01 20:08:18 +00001174 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001175}
1176
Damiend99b0522013-12-21 18:17:45 +00001177void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001178 if (comp->continue_label == 0) {
Damien George2326d522014-03-27 23:26:35 +00001179 compile_syntax_error(comp, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001180 }
Damien Georgecbddb272014-02-01 20:08:18 +00001181 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001182}
1183
Damiend99b0522013-12-21 18:17:45 +00001184void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001185 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001186 compile_syntax_error(comp, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001187 return;
1188 }
Damiend99b0522013-12-21 18:17:45 +00001189 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001190 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001191 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001192 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001193 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001194 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1195 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 +01001196
Damienb05d7072013-10-05 13:37:10 +01001197 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001198 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1199 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1200 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001201 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001202 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1203 } else {
1204 compile_node(comp, pns->nodes[0]);
1205 }
1206 EMIT(return_value);
1207}
1208
Damiend99b0522013-12-21 18:17:45 +00001209void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001210 compile_node(comp, pns->nodes[0]);
1211 EMIT(pop_top);
1212}
1213
Damiend99b0522013-12-21 18:17:45 +00001214void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1215 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001216 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001217 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001218 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001219 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001220 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001221 compile_node(comp, pns->nodes[0]);
1222 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001223 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001224 } else {
1225 // raise x
1226 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001227 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001228 }
1229}
1230
1231// q1 holds the base, q2 the full name
1232// eg a -> q1=q2=a
1233// a.b.c -> q1=a, q2=a.b.c
Damiend99b0522013-12-21 18:17:45 +00001234void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q1, qstr *q2) {
Damien429d7192013-10-04 19:53:11 +01001235 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001236 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1237 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001238 // a name of the form x as y; unwrap it
Damiend99b0522013-12-21 18:17:45 +00001239 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001240 pn = pns->nodes[0];
1241 is_as = true;
1242 }
Damiend99b0522013-12-21 18:17:45 +00001243 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001244 // just a simple name
Damiend99b0522013-12-21 18:17:45 +00001245 *q2 = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001246 if (!is_as) {
1247 *q1 = *q2;
1248 }
Damien Georgeb9791222014-01-23 00:34:21 +00001249 EMIT_ARG(import_name, *q2);
Damiend99b0522013-12-21 18:17:45 +00001250 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1251 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1252 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001253 // a name of the form a.b.c
1254 if (!is_as) {
Damiend99b0522013-12-21 18:17:45 +00001255 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001256 }
Damiend99b0522013-12-21 18:17:45 +00001257 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001258 int len = n - 1;
1259 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001260 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001261 }
Damien George55baff42014-01-21 21:40:13 +00001262 byte *q_ptr;
1263 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001264 for (int i = 0; i < n; i++) {
1265 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001266 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001267 }
Damien George55baff42014-01-21 21:40:13 +00001268 uint str_src_len;
1269 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001270 memcpy(str_dest, str_src, str_src_len);
1271 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001272 }
Damien George55baff42014-01-21 21:40:13 +00001273 *q2 = qstr_build_end(q_ptr);
Damien Georgeb9791222014-01-23 00:34:21 +00001274 EMIT_ARG(import_name, *q2);
Damien429d7192013-10-04 19:53:11 +01001275 if (is_as) {
1276 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001277 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001278 }
1279 }
1280 } else {
1281 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001282 // This covers relative imports starting with dot(s) like "from .foo import"
Paul Sokolovsky8d9cc2e2014-03-30 02:31:08 +02001283 compile_syntax_error(comp, "Relative imports not implemented");
Damien429d7192013-10-04 19:53:11 +01001284 assert(0);
1285 }
1286 } else {
1287 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001288 // This covers relative imports with dots only like "from .. import"
Paul Sokolovsky8d9cc2e2014-03-30 02:31:08 +02001289 compile_syntax_error(comp, "Relative imports not implemented");
Damien429d7192013-10-04 19:53:11 +01001290 assert(0);
1291 }
1292}
1293
Damiend99b0522013-12-21 18:17:45 +00001294void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgeb9791222014-01-23 00:34:21 +00001295 EMIT_ARG(load_const_small_int, 0); // ??
1296 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001297 qstr q1, q2;
1298 do_import_name(comp, pn, &q1, &q2);
Damien Georgeb9791222014-01-23 00:34:21 +00001299 EMIT_ARG(store_id, q1);
Damien429d7192013-10-04 19:53:11 +01001300}
1301
Damiend99b0522013-12-21 18:17:45 +00001302void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001303 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1304}
1305
Damiend99b0522013-12-21 18:17:45 +00001306void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
1307 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001308 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001309
1310 // build the "fromlist" tuple
1311#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001312 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001313#else
Damien Georgeb9791222014-01-23 00:34:21 +00001314 EMIT_ARG(load_const_str, QSTR_FROM_STR_STATIC("*"), false);
1315 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001316#endif
1317
1318 // do the import
Damien429d7192013-10-04 19:53:11 +01001319 qstr dummy_q, id1;
1320 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1321 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001322
Damien429d7192013-10-04 19:53:11 +01001323 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001324 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001325
1326 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001327 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001328 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001329#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001330 {
1331 vstr_t *vstr = vstr_new();
1332 vstr_printf(vstr, "(");
1333 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001334 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1335 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1336 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001337 if (i > 0) {
1338 vstr_printf(vstr, ", ");
1339 }
1340 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001341 uint len;
1342 const byte *str = qstr_data(id2, &len);
1343 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001344 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001345 }
Damien02f89412013-12-12 15:13:36 +00001346 if (n == 1) {
1347 vstr_printf(vstr, ",");
1348 }
1349 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001350 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001351 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001352 }
Damiendb4c3612013-12-10 17:27:24 +00001353#else
1354 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001355 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1356 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1357 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001358 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001359 }
Damien Georgeb9791222014-01-23 00:34:21 +00001360 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001361#endif
1362
1363 // do the import
Damien429d7192013-10-04 19:53:11 +01001364 qstr dummy_q, id1;
1365 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1366 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001367 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1368 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1369 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001370 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001371 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001372 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001373 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001374 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001375 }
1376 }
1377 EMIT(pop_top);
1378 }
1379}
1380
Damiend99b0522013-12-21 18:17:45 +00001381void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001382 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001383 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1384 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001385 } else {
Damiend99b0522013-12-21 18:17:45 +00001386 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1387 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001388 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001389 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001390 }
Damien429d7192013-10-04 19:53:11 +01001391 }
1392 }
1393}
1394
Damiend99b0522013-12-21 18:17:45 +00001395void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001396 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001397 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1398 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001399 } else {
Damiend99b0522013-12-21 18:17:45 +00001400 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1401 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001402 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001403 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001404 }
Damien429d7192013-10-04 19:53:11 +01001405 }
1406 }
1407}
1408
Damiend99b0522013-12-21 18:17:45 +00001409void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001410 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001411 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001412 EMIT_ARG(load_global, MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001413 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001414 // assertion message
1415 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001416 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01001417 }
Damien Georgeb9791222014-01-23 00:34:21 +00001418 EMIT_ARG(raise_varargs, 1);
1419 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001420}
1421
Damiend99b0522013-12-21 18:17:45 +00001422void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001423 // TODO proper and/or short circuiting
1424
Damienb05d7072013-10-05 13:37:10 +01001425 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001426
Damienb05d7072013-10-05 13:37:10 +01001427 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001428 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1429
1430 compile_node(comp, pns->nodes[1]); // if block
Damien Georgeaf6edc62014-04-02 16:12:28 +01001431
1432 if (
1433#if !MICROPY_EMIT_CPYTHON
1434 // optimisation to not jump over non-existent elif/else blocks (this optimisation is not in CPython)
1435 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) &&
1436#endif
1437 // optimisation to not jump if last instruction was return
1438 !EMIT(last_emit_was_return_value)
1439 ) {
1440 // jump over elif/else blocks
1441 EMIT_ARG(jump, l_end);
1442 }
1443
Damien Georgeb9791222014-01-23 00:34:21 +00001444 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001445
Damiend99b0522013-12-21 18:17:45 +00001446 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001447 // compile elif blocks
1448
Damiend99b0522013-12-21 18:17:45 +00001449 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001450
Damiend99b0522013-12-21 18:17:45 +00001451 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001452 // multiple elif blocks
1453
Damiend99b0522013-12-21 18:17:45 +00001454 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001455 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001456 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001457 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001458 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1459
1460 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001461 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001462 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001463 }
Damien Georgeb9791222014-01-23 00:34:21 +00001464 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001465 }
1466
1467 } else {
1468 // a single elif block
1469
Damienb05d7072013-10-05 13:37:10 +01001470 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001471 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1472
1473 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001474 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001475 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001476 }
Damien Georgeb9791222014-01-23 00:34:21 +00001477 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001478 }
1479 }
1480
1481 // compile else block
1482 compile_node(comp, pns->nodes[3]); // can be null
1483
Damien Georgeb9791222014-01-23 00:34:21 +00001484 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001485}
1486
Damien Georgecbddb272014-02-01 20:08:18 +00001487#define START_BREAK_CONTINUE_BLOCK \
1488 int old_break_label = comp->break_label; \
1489 int old_continue_label = comp->continue_label; \
1490 int break_label = comp_next_label(comp); \
1491 int continue_label = comp_next_label(comp); \
1492 comp->break_label = break_label; \
1493 comp->continue_label = continue_label; \
1494 comp->break_continue_except_level = comp->cur_except_level;
1495
1496#define END_BREAK_CONTINUE_BLOCK \
1497 comp->break_label = old_break_label; \
1498 comp->continue_label = old_continue_label; \
1499 comp->break_continue_except_level = comp->cur_except_level;
1500
Damiend99b0522013-12-21 18:17:45 +00001501void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001502 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001503
Damience89a212013-10-15 22:25:17 +01001504 // compared to CPython, we have an optimised version of while loops
1505#if MICROPY_EMIT_CPYTHON
1506 int done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001507 EMIT_ARG(setup_loop, break_label);
1508 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001509 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1510 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001511 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001512 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001513 }
Damien Georgeb9791222014-01-23 00:34:21 +00001514 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001515 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1516 // this is a small hack to agree with CPython
1517 if (!node_is_const_true(pns->nodes[0])) {
1518 EMIT(pop_block);
1519 }
Damience89a212013-10-15 22:25:17 +01001520#else
1521 int top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001522 EMIT_ARG(jump, continue_label);
1523 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001524 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001525 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001526 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1527#endif
1528
1529 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001530 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001531
1532 compile_node(comp, pns->nodes[2]); // else
1533
Damien Georgeb9791222014-01-23 00:34:21 +00001534 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001535}
1536
Damienf72fd0e2013-11-06 20:20:49 +00001537// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001538// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1539// And, if the loop never runs, the loop variable should never be assigned
Damiend99b0522013-12-21 18:17:45 +00001540void 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) {
Damien Georgecbddb272014-02-01 20:08:18 +00001541 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001542
1543 int top_label = comp_next_label(comp);
Damien George600ae732014-01-21 23:48:04 +00001544 int entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001545
Damien George3ff2d032014-03-31 18:02:22 +01001546 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001547 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001548 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001549
Damien Georgeb9791222014-01-23 00:34:21 +00001550 EMIT_ARG(jump, entry_label);
1551 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001552
Damien George3ff2d032014-03-31 18:02:22 +01001553 // at this point we actually have 1 less element on the stack
1554 EMIT_ARG(set_stack_size, EMIT(get_stack_size) - 1);
1555
1556 // store next value to var
1557 c_assign(comp, pn_var, ASSIGN_STORE);
1558
Damienf3822fc2013-11-09 20:12:03 +00001559 // compile body
1560 compile_node(comp, pn_body);
1561
Damien Georgeb9791222014-01-23 00:34:21 +00001562 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001563
Damien George3ff2d032014-03-31 18:02:22 +01001564 // compile: var + step, duplicated on stack
1565 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001566 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001567 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001568 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001569
Damien Georgeb9791222014-01-23 00:34:21 +00001570 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001571
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001572 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001573 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001574 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1575 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001576 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001577 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001578 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001579 }
Damien Georgeb9791222014-01-23 00:34:21 +00001580 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001581
Damien George3ff2d032014-03-31 18:02:22 +01001582 // discard final value of var that failed the loop condition
1583 EMIT(pop_top);
1584
Damienf72fd0e2013-11-06 20:20:49 +00001585 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001586 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001587
1588 compile_node(comp, pn_else);
1589
Damien Georgeb9791222014-01-23 00:34:21 +00001590 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001591}
1592
Damiend99b0522013-12-21 18:17:45 +00001593void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001594#if !MICROPY_EMIT_CPYTHON
1595 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1596 // this is actually slower, but uses no heap memory
1597 // for viper it will be much, much faster
Damien George65cad122014-04-06 11:48:15 +01001598 if (/*comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER &&*/ MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_power)) {
Damiend99b0522013-12-21 18:17:45 +00001599 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001600 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1601 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1602 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1603 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001604 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1605 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001606 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001607 mp_parse_node_t pn_range_start;
1608 mp_parse_node_t pn_range_end;
1609 mp_parse_node_t pn_range_step;
1610 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001611 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001612 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001613 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001614 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001615 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001616 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001617 } else if (n_args == 2) {
1618 pn_range_start = args[0];
1619 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001620 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001621 } else {
1622 pn_range_start = args[0];
1623 pn_range_end = args[1];
1624 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001625 // We need to know sign of step. This is possible only if it's constant
1626 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1627 optimize = false;
1628 }
Damienf72fd0e2013-11-06 20:20:49 +00001629 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001630 }
1631 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001632 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1633 return;
1634 }
1635 }
1636 }
1637#endif
1638
Damien Georgecbddb272014-02-01 20:08:18 +00001639 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001640
Damienb05d7072013-10-05 13:37:10 +01001641 int pop_label = comp_next_label(comp);
1642 int end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001643
Damience89a212013-10-15 22:25:17 +01001644 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1645#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001646 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001647#endif
1648
Damien429d7192013-10-04 19:53:11 +01001649 compile_node(comp, pns->nodes[1]); // iterator
1650 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001651 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001652 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001653 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1654 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001655 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001656 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001657 }
Damien Georgeb9791222014-01-23 00:34:21 +00001658 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001659 EMIT(for_iter_end);
1660
1661 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001662 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001663
Damience89a212013-10-15 22:25:17 +01001664#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001665 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001666#endif
Damien429d7192013-10-04 19:53:11 +01001667
1668 compile_node(comp, pns->nodes[3]); // else (not tested)
1669
Damien Georgeb9791222014-01-23 00:34:21 +00001670 EMIT_ARG(label_assign, break_label);
1671 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001672}
1673
Damiend99b0522013-12-21 18:17:45 +00001674void 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 +01001675 // this function is a bit of a hack at the moment
1676 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1677
1678 // setup code
1679 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001680 int l1 = comp_next_label(comp);
1681 int success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001682
Damien Georgeb9791222014-01-23 00:34:21 +00001683 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001684 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001685
Damien429d7192013-10-04 19:53:11 +01001686 compile_node(comp, pn_body); // body
1687 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001688 EMIT_ARG(jump, success_label);
1689 EMIT_ARG(label_assign, l1);
Damienb05d7072013-10-05 13:37:10 +01001690 int l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001691
1692 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001693 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1694 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001695
1696 qstr qstr_exception_local = 0;
Damienb05d7072013-10-05 13:37:10 +01001697 int end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001698
Damiend99b0522013-12-21 18:17:45 +00001699 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001700 // this is a catch all exception handler
1701 if (i + 1 != n_except) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001702 compile_syntax_error(comp, "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001703 return;
1704 }
1705 } else {
1706 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001707 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1708 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1709 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1710 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001711 // handler binds the exception to a local
1712 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001713 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001714 }
1715 }
1716 EMIT(dup_top);
1717 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001718 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001719 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001720 }
1721
1722 EMIT(pop_top);
1723
1724 if (qstr_exception_local == 0) {
1725 EMIT(pop_top);
1726 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001727 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001728 }
1729
1730 EMIT(pop_top);
1731
Damiene2880aa2013-12-20 14:22:59 +00001732 int l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001733 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001734 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001735 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001736 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001737 }
1738 compile_node(comp, pns_except->nodes[1]);
1739 if (qstr_exception_local != 0) {
1740 EMIT(pop_block);
1741 }
1742 EMIT(pop_except);
1743 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001744 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1745 EMIT_ARG(label_assign, l3);
1746 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1747 EMIT_ARG(store_id, qstr_exception_local);
1748 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001749
Damien George8dcc0c72014-03-27 10:55:21 +00001750 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001751 EMIT(end_finally);
1752 }
Damien Georgeb9791222014-01-23 00:34:21 +00001753 EMIT_ARG(jump, l2);
1754 EMIT_ARG(label_assign, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001755 }
1756
Damien George8dcc0c72014-03-27 10:55:21 +00001757 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001758 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001759
Damien Georgeb9791222014-01-23 00:34:21 +00001760 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001761 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001762 EMIT_ARG(label_assign, l2);
1763 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001764}
1765
Damiend99b0522013-12-21 18:17:45 +00001766void 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 +01001767 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1768 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001769 int l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001770
Damien Georgeb9791222014-01-23 00:34:21 +00001771 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001772 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001773
Damien429d7192013-10-04 19:53:11 +01001774 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001775 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien429d7192013-10-04 19:53:11 +01001776 compile_node(comp, pn_body);
1777 } else {
1778 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1779 }
1780 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001781 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1782 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001783 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001784
Damien George8dcc0c72014-03-27 10:55:21 +00001785 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001786 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001787
Damien Georgeb9791222014-01-23 00:34:21 +00001788 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001789}
1790
Damiend99b0522013-12-21 18:17:45 +00001791void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1792 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1793 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1794 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001795 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001796 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1797 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001798 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001799 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001800 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001801 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001802 // no finally
1803 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1804 } else {
1805 // have finally
Damiend99b0522013-12-21 18:17:45 +00001806 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 +01001807 }
1808 } else {
1809 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001810 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001811 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001812 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001813 }
1814 } else {
1815 // shouldn't happen
1816 assert(0);
1817 }
1818}
1819
Damiend99b0522013-12-21 18:17:45 +00001820void 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 +01001821 if (n == 0) {
1822 // no more pre-bits, compile the body of the with
1823 compile_node(comp, body);
1824 } else {
Damienb05d7072013-10-05 13:37:10 +01001825 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001826 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001827 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001828 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001829 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001830 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001831 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1832 } else {
1833 // this pre-bit is just an expression
1834 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001835 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001836 EMIT(pop_top);
1837 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001838 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001839 // compile additional pre-bits and the body
1840 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1841 // finish this with block
1842 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001843 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1844 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001845 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001846 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001847 EMIT(end_finally);
1848 }
1849}
1850
Damiend99b0522013-12-21 18:17:45 +00001851void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001852 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001853 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001854 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1855 assert(n > 0);
1856
1857 // compile in a nested fashion
1858 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1859}
1860
Damiend99b0522013-12-21 18:17:45 +00001861void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1862 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001863 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1864 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001865 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001866 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001867 EMIT_ARG(call_function, 1, 0, false, false);
Damien5ac1b2e2013-10-18 19:58:12 +01001868 EMIT(pop_top);
1869
Damien429d7192013-10-04 19:53:11 +01001870 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001871 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001872 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001873 // do nothing with a lonely constant
1874 } else {
1875 compile_node(comp, pns->nodes[0]); // just an expression
1876 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1877 }
Damien429d7192013-10-04 19:53:11 +01001878 }
1879 } else {
Damiend99b0522013-12-21 18:17:45 +00001880 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1881 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001882 if (kind == PN_expr_stmt_augassign) {
1883 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1884 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001885 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001886 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001887 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001888 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1889 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1890 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1891 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1892 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1893 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1894 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1895 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1896 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1897 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1898 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
1899 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
1900 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01001901 }
Damien George7e5fb242014-02-01 22:18:47 +00001902 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001903 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1904 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001905 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1906 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001907 // following CPython, we store left-most first
1908 if (rhs > 0) {
1909 EMIT(dup_top);
1910 }
1911 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1912 for (int i = 0; i < rhs; i++) {
1913 if (i + 1 < rhs) {
1914 EMIT(dup_top);
1915 }
Damiend99b0522013-12-21 18:17:45 +00001916 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001917 }
1918 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001919 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1920 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1921 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1922 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01001923 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001924 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1925 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001926 compile_node(comp, pns10->nodes[0]); // rhs
1927 compile_node(comp, pns10->nodes[1]); // rhs
1928 EMIT(rot_two);
1929 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1930 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00001931 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1932 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1933 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1934 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01001935 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001936 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1937 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001938 compile_node(comp, pns10->nodes[0]); // rhs
1939 compile_node(comp, pns10->nodes[1]); // rhs
1940 compile_node(comp, pns10->nodes[2]); // rhs
1941 EMIT(rot_three);
1942 EMIT(rot_two);
1943 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1944 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1945 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1946 } else {
1947 compile_node(comp, pns1->nodes[0]); // rhs
1948 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1949 }
1950 } else {
1951 // shouldn't happen
1952 assert(0);
1953 }
1954 }
1955}
1956
Damien Georged17926d2014-03-30 13:35:08 +01001957void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00001958 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001959 compile_node(comp, pns->nodes[0]);
1960 for (int i = 1; i < num_nodes; i += 1) {
1961 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001962 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001963 }
1964}
1965
Damiend99b0522013-12-21 18:17:45 +00001966void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1967 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1968 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001969
1970 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001971 int l_fail = comp_next_label(comp);
1972 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001973 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1974 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001975 EMIT_ARG(jump, l_end);
1976 EMIT_ARG(label_assign, l_fail);
1977 EMIT_ARG(set_stack_size, stack_size); // force stack size reset
Damien429d7192013-10-04 19:53:11 +01001978 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001979 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001980}
1981
Damiend99b0522013-12-21 18:17:45 +00001982void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001983 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00001984 //mp_parse_node_t pn_params = pns->nodes[0];
1985 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001986
1987 if (comp->pass == PASS_1) {
1988 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00001989 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 +01001990 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001991 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001992 }
1993
1994 // get the scope for this lambda
1995 scope_t *this_scope = (scope_t*)pns->nodes[2];
1996
1997 // make the lambda
1998 close_over_variables_etc(comp, this_scope, 0, 0);
1999}
2000
Damiend99b0522013-12-21 18:17:45 +00002001void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01002002 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002003 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002004 for (int i = 0; i < n; i += 1) {
2005 compile_node(comp, pns->nodes[i]);
2006 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002007 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002008 }
2009 }
Damien Georgeb9791222014-01-23 00:34:21 +00002010 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002011}
2012
Damiend99b0522013-12-21 18:17:45 +00002013void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01002014 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002015 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002016 for (int i = 0; i < n; i += 1) {
2017 compile_node(comp, pns->nodes[i]);
2018 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002019 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002020 }
2021 }
Damien Georgeb9791222014-01-23 00:34:21 +00002022 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002023}
2024
Damiend99b0522013-12-21 18:17:45 +00002025void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002026 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002027 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002028}
2029
Damiend99b0522013-12-21 18:17:45 +00002030void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002031 int stack_size = EMIT(get_stack_size);
Damiend99b0522013-12-21 18:17:45 +00002032 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002033 compile_node(comp, pns->nodes[0]);
2034 bool multi = (num_nodes > 3);
2035 int l_fail = 0;
2036 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002037 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002038 }
2039 for (int i = 1; i + 1 < num_nodes; i += 2) {
2040 compile_node(comp, pns->nodes[i + 1]);
2041 if (i + 2 < num_nodes) {
2042 EMIT(dup_top);
2043 EMIT(rot_three);
2044 }
Damien George7e5fb242014-02-01 22:18:47 +00002045 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002046 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002047 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002048 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2049 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2050 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2051 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2052 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2053 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2054 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2055 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002056 }
2057 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002058 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2059 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2060 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002061 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002062 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002063 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002064 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002065 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002066 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002067 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002068 }
2069 } else {
2070 // shouldn't happen
2071 assert(0);
2072 }
2073 } else {
2074 // shouldn't happen
2075 assert(0);
2076 }
2077 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002078 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002079 }
2080 }
2081 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002082 int l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002083 EMIT_ARG(jump, l_end);
2084 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01002085 EMIT(rot_two);
2086 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002087 EMIT_ARG(label_assign, l_end);
2088 EMIT_ARG(set_stack_size, stack_size + 1); // force stack size
Damien429d7192013-10-04 19:53:11 +01002089 }
2090}
2091
Damiend99b0522013-12-21 18:17:45 +00002092void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002093 // TODO
2094 assert(0);
2095 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002096 //EMIT_ARG(unary_op, "UNARY_STAR");
Damien429d7192013-10-04 19:53:11 +01002097}
2098
Damiend99b0522013-12-21 18:17:45 +00002099void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002100 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002101}
2102
Damiend99b0522013-12-21 18:17:45 +00002103void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002104 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002105}
2106
Damiend99b0522013-12-21 18:17:45 +00002107void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002108 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002109}
2110
Damiend99b0522013-12-21 18:17:45 +00002111void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2112 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002113 compile_node(comp, pns->nodes[0]);
2114 for (int i = 1; i + 1 < num_nodes; i += 2) {
2115 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002116 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002117 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002118 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002119 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002120 } else {
2121 // shouldn't happen
2122 assert(0);
2123 }
2124 }
2125}
2126
Damiend99b0522013-12-21 18:17:45 +00002127void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2128 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002129 compile_node(comp, pns->nodes[0]);
2130 for (int i = 1; i + 1 < num_nodes; i += 2) {
2131 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002132 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002133 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002134 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002135 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002136 } else {
2137 // shouldn't happen
2138 assert(0);
2139 }
2140 }
2141}
2142
Damiend99b0522013-12-21 18:17:45 +00002143void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2144 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002145 compile_node(comp, pns->nodes[0]);
2146 for (int i = 1; i + 1 < num_nodes; i += 2) {
2147 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002148 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002149 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002150 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002151 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002152 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002153 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002154 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002155 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002156 } else {
2157 // shouldn't happen
2158 assert(0);
2159 }
2160 }
2161}
2162
Damiend99b0522013-12-21 18:17:45 +00002163void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002164 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002165 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002166 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002167 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002168 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002169 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002170 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002171 } else {
2172 // shouldn't happen
2173 assert(0);
2174 }
2175}
2176
Damien George35e2a4e2014-02-05 00:51:47 +00002177void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2178 // this is to handle special super() call
2179 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2180
2181 compile_generic_all_nodes(comp, pns);
2182}
2183
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002184STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra) {
Damien429d7192013-10-04 19:53:11 +01002185 // function to call is on top of stack
2186
Damien George35e2a4e2014-02-05 00:51:47 +00002187#if !MICROPY_EMIT_CPYTHON
2188 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002189 if (MP_PARSE_NODE_IS_NULL(pn_arglist) && comp->func_arg_is_super && comp->scope_cur->kind == SCOPE_FUNCTION) {
Damien George35e2a4e2014-02-05 00:51:47 +00002190 EMIT_ARG(load_id, MP_QSTR___class__);
2191 // get first argument to function
2192 bool found = false;
2193 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
2194 if (comp->scope_cur->id_info[i].param) {
2195 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
2196 found = true;
2197 break;
2198 }
2199 }
2200 if (!found) {
2201 printf("TypeError: super() call cannot find self\n");
2202 return;
2203 }
2204 EMIT_ARG(call_function, 2, 0, false, false);
2205 return;
2206 }
2207#endif
2208
Damien429d7192013-10-04 19:53:11 +01002209 int old_n_arg_keyword = comp->n_arg_keyword;
2210 bool old_have_star_arg = comp->have_star_arg;
2211 bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
2212 comp->n_arg_keyword = 0;
2213 comp->have_star_arg = false;
2214 comp->have_dbl_star_arg = false;
2215
Damien Georgebbcd49a2014-02-06 20:30:16 +00002216 compile_node(comp, pn_arglist); // arguments to function call; can be null
Damien429d7192013-10-04 19:53:11 +01002217
2218 // compute number of positional arguments
Damien Georgebbcd49a2014-02-06 20:30:16 +00002219 int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
Damien429d7192013-10-04 19:53:11 +01002220 if (comp->have_star_arg) {
2221 n_positional -= 1;
2222 }
2223 if (comp->have_dbl_star_arg) {
2224 n_positional -= 1;
2225 }
2226
2227 if (is_method_call) {
Damien Georgeb9791222014-01-23 00:34:21 +00002228 EMIT_ARG(call_method, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg);
Damien429d7192013-10-04 19:53:11 +01002229 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002230 EMIT_ARG(call_function, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg);
Damien429d7192013-10-04 19:53:11 +01002231 }
2232
2233 comp->n_arg_keyword = old_n_arg_keyword;
2234 comp->have_star_arg = old_have_star_arg;
2235 comp->have_dbl_star_arg = old_have_dbl_star_arg;
2236}
2237
Damiend99b0522013-12-21 18:17:45 +00002238void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2239 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002240 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002241 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 +01002242 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002243 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2244 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002245 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002246 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002247 i += 1;
2248 } else {
2249 compile_node(comp, pns->nodes[i]);
2250 }
Damien George35e2a4e2014-02-05 00:51:47 +00002251 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002252 }
2253}
2254
Damiend99b0522013-12-21 18:17:45 +00002255void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002256 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002257 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002258}
2259
Damiend99b0522013-12-21 18:17:45 +00002260void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002261 // a list of strings
Damien63321742013-12-10 17:41:49 +00002262
2263 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002264 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002265 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002266 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002267 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002268 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2269 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2270 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002271 if (i == 0) {
2272 string_kind = pn_kind;
2273 } else if (pn_kind != string_kind) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002274 compile_syntax_error(comp, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002275 return;
2276 }
Damien George55baff42014-01-21 21:40:13 +00002277 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002278 }
Damien63321742013-12-10 17:41:49 +00002279
Damien63321742013-12-10 17:41:49 +00002280 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002281 byte *q_ptr;
2282 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002283 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002284 uint s_len;
2285 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002286 memcpy(s_dest, s, s_len);
2287 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002288 }
Damien George55baff42014-01-21 21:40:13 +00002289 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002290
Damien Georgeb9791222014-01-23 00:34:21 +00002291 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002292}
2293
2294// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002295void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2296 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2297 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2298 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002299
2300 if (comp->pass == PASS_1) {
2301 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002302 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 +01002303 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002304 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002305 }
2306
2307 // get the scope for this comprehension
2308 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2309
2310 // compile the comprehension
2311 close_over_variables_etc(comp, this_scope, 0, 0);
2312
2313 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2314 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002315 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01002316}
2317
Damiend99b0522013-12-21 18:17:45 +00002318void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2319 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002320 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002321 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2322 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2323 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2324 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2325 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2326 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2327 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002328 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002329 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002330 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002331 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002332 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002333 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002334 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002335 // generator expression
2336 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2337 } else {
2338 // tuple with 2 items
2339 goto tuple_with_2_items;
2340 }
2341 } else {
2342 // tuple with 2 items
2343 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002344 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002345 }
2346 } else {
2347 // parenthesis around a single item, is just that item
2348 compile_node(comp, pns->nodes[0]);
2349 }
2350}
2351
Damiend99b0522013-12-21 18:17:45 +00002352void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2353 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002354 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002355 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002356 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2357 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2358 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2359 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2360 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002361 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002362 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002363 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002364 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002365 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002366 // list of many items
2367 compile_node(comp, pns2->nodes[0]);
2368 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002369 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002370 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002371 // list comprehension
2372 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2373 } else {
2374 // list with 2 items
2375 goto list_with_2_items;
2376 }
2377 } else {
2378 // list with 2 items
2379 list_with_2_items:
2380 compile_node(comp, pns2->nodes[0]);
2381 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002382 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002383 }
2384 } else {
2385 // list with 1 item
2386 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002387 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002388 }
2389}
2390
Damiend99b0522013-12-21 18:17:45 +00002391void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2392 mp_parse_node_t pn = pns->nodes[0];
2393 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002394 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002395 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002396 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2397 pns = (mp_parse_node_struct_t*)pn;
2398 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002399 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002400 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002401 compile_node(comp, pn);
2402 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002403 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2404 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2405 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2406 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002407 // dict/set with multiple elements
2408
2409 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002410 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002411 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2412
2413 // first element sets whether it's a dict or set
2414 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002415 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002416 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002417 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002418 compile_node(comp, pns->nodes[0]);
2419 EMIT(store_map);
2420 is_dict = true;
2421 } else {
2422 // a set
2423 compile_node(comp, pns->nodes[0]); // 1st value of set
2424 is_dict = false;
2425 }
2426
2427 // process rest of elements
2428 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002429 mp_parse_node_t pn = nodes[i];
2430 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002431 compile_node(comp, pn);
2432 if (is_dict) {
2433 if (!is_key_value) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002434 compile_syntax_error(comp, "?expecting key:value for dictiona");
Damien429d7192013-10-04 19:53:11 +01002435 return;
2436 }
2437 EMIT(store_map);
2438 } else {
2439 if (is_key_value) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002440 compile_syntax_error(comp, "?expecting just a value for s");
Damien429d7192013-10-04 19:53:11 +01002441 return;
2442 }
2443 }
2444 }
2445
2446 // if it's a set, build it
2447 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002448 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002449 }
Damiend99b0522013-12-21 18:17:45 +00002450 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002451 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002452 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002453 // a dictionary comprehension
2454 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2455 } else {
2456 // a set comprehension
2457 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2458 }
2459 } else {
2460 // shouldn't happen
2461 assert(0);
2462 }
2463 } else {
2464 // set with one element
2465 goto set_with_one_element;
2466 }
2467 } else {
2468 // set with one element
2469 set_with_one_element:
2470 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002471 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002472 }
2473}
2474
Damiend99b0522013-12-21 18:17:45 +00002475void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002476 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002477}
2478
Damiend99b0522013-12-21 18:17:45 +00002479void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002480 // object who's index we want is on top of stack
2481 compile_node(comp, pns->nodes[0]); // the index
Damien Georged17926d2014-03-30 13:35:08 +01002482 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002483}
2484
Damiend99b0522013-12-21 18:17:45 +00002485void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002486 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002487 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002488}
2489
Damiend99b0522013-12-21 18:17:45 +00002490void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2491 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2492 mp_parse_node_t pn = pns->nodes[0];
2493 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002494 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002495 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2496 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002497 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2498 pns = (mp_parse_node_struct_t*)pn;
2499 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002500 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002501 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002502 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002503 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002504 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002505 } else {
2506 // [?::x]
2507 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002508 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002509 }
Damiend99b0522013-12-21 18:17:45 +00002510 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002511 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002512 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2513 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2514 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2515 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002516 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002517 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002518 } else {
2519 // [?:x:x]
2520 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002521 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002522 }
2523 } else {
2524 // [?:x]
2525 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002526 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002527 }
2528 } else {
2529 // [?:x]
2530 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002531 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002532 }
2533}
2534
Damiend99b0522013-12-21 18:17:45 +00002535void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002536 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002537 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2538 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002539}
2540
Damiend99b0522013-12-21 18:17:45 +00002541void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002542 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002543 compile_subscript_3_helper(comp, pns);
2544}
2545
Damiend99b0522013-12-21 18:17:45 +00002546void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002547 // if this is called then we are compiling a dict key:value pair
2548 compile_node(comp, pns->nodes[1]); // value
2549 compile_node(comp, pns->nodes[0]); // key
2550}
2551
Damiend99b0522013-12-21 18:17:45 +00002552void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002553 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002554 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002555 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002556}
2557
Damiend99b0522013-12-21 18:17:45 +00002558void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002559 if (comp->have_star_arg) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002560 compile_syntax_error(comp, "?can't have multiple *x");
Damien429d7192013-10-04 19:53:11 +01002561 return;
2562 }
2563 comp->have_star_arg = true;
2564 compile_node(comp, pns->nodes[0]);
2565}
2566
Damiend99b0522013-12-21 18:17:45 +00002567void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002568 if (comp->have_dbl_star_arg) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002569 compile_syntax_error(comp, "?can't have multiple **x");
Damien429d7192013-10-04 19:53:11 +01002570 return;
2571 }
2572 comp->have_dbl_star_arg = true;
2573 compile_node(comp, pns->nodes[0]);
2574}
2575
Damiend99b0522013-12-21 18:17:45 +00002576void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2577 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2578 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2579 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2580 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002581 compile_syntax_error(comp, "?lhs of keyword argument must be an id");
Damien429d7192013-10-04 19:53:11 +01002582 return;
2583 }
Damien Georgeb9791222014-01-23 00:34:21 +00002584 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002585 compile_node(comp, pns2->nodes[0]);
2586 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002587 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002588 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2589 } else {
2590 // shouldn't happen
2591 assert(0);
2592 }
2593}
2594
Damiend99b0522013-12-21 18:17:45 +00002595void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002596 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002597 compile_syntax_error(comp, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002598 return;
2599 }
Damiend99b0522013-12-21 18:17:45 +00002600 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002601 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002602 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002603 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2604 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002605 compile_node(comp, pns->nodes[0]);
2606 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002607 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002608 EMIT(yield_from);
2609 } else {
2610 compile_node(comp, pns->nodes[0]);
2611 EMIT(yield_value);
2612 }
2613}
2614
Damiend99b0522013-12-21 18:17:45 +00002615typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002616STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002617 NULL,
2618#define nc NULL
2619#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002620#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002621#include "grammar.h"
2622#undef nc
2623#undef c
2624#undef DEF_RULE
2625};
2626
Damiend99b0522013-12-21 18:17:45 +00002627void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2628 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002629 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002630 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2631 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2632 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002633 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002634 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002635 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002636 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002637 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2638 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2639 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2640 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002641 case MP_PARSE_NODE_TOKEN:
2642 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002643 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002644 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002645 // do nothing
2646 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002647 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002648 }
2649 break;
Damien429d7192013-10-04 19:53:11 +01002650 default: assert(0);
2651 }
2652 } else {
Damiend99b0522013-12-21 18:17:45 +00002653 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002654 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002655 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002656 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002657 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002658#if MICROPY_DEBUG_PRINTERS
2659 mp_parse_node_print(pn, 0);
2660#endif
Damien429d7192013-10-04 19:53:11 +01002661 assert(0);
2662 } else {
2663 f(comp, pns);
2664 }
2665 }
2666}
2667
Damiend99b0522013-12-21 18:17:45 +00002668void 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 +01002669 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002670 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002671 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2672 if (MP_PARSE_NODE_IS_ID(pn)) {
2673 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002674 if (comp->have_bare_star) {
2675 // comes after a bare star, so doesn't count as a parameter
2676 } else {
2677 comp->scope_cur->num_params += 1;
2678 }
Damienb14de212013-10-06 00:28:28 +01002679 } else {
Damiend99b0522013-12-21 18:17:45 +00002680 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2681 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2682 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2683 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002684 //int node_index = 1; unused
2685 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002686 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002687 // this parameter has an annotation
2688 pn_annotation = pns->nodes[1];
2689 }
2690 //node_index = 2; unused
2691 }
2692 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002693 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002694 // this parameter has a default value
2695 if (comp->have_bare_star) {
2696 comp->scope_cur->num_dict_params += 1;
2697 } else {
2698 comp->scope_cur->num_default_params += 1;
2699 }
2700 }
2701 */
2702 if (comp->have_bare_star) {
2703 // comes after a bare star, so doesn't count as a parameter
2704 } else {
2705 comp->scope_cur->num_params += 1;
2706 }
Damiend99b0522013-12-21 18:17:45 +00002707 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2708 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002709 // bare star
2710 // TODO see http://www.python.org/dev/peps/pep-3102/
2711 comp->have_bare_star = true;
2712 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002713 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002714 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002715 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002716 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2717 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002718 // named star with annotation
Damien George8725f8f2014-02-15 19:33:11 +00002719 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002720 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2721 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002722 pn_annotation = pns->nodes[1];
2723 } else {
2724 // shouldn't happen
2725 assert(0);
2726 }
Damiend99b0522013-12-21 18:17:45 +00002727 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2728 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2729 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002730 // this parameter has an annotation
2731 pn_annotation = pns->nodes[1];
2732 }
Damien George8725f8f2014-02-15 19:33:11 +00002733 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002734 } else {
Damienb14de212013-10-06 00:28:28 +01002735 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002736 assert(0);
2737 }
Damien429d7192013-10-04 19:53:11 +01002738 }
2739
2740 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002741 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002742 // TODO this parameter has an annotation
2743 }
2744 bool added;
2745 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2746 if (!added) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002747 compile_syntax_error(comp, "?same name used for parameter");
Damien429d7192013-10-04 19:53:11 +01002748 return;
2749 }
2750 id_info->param = true;
2751 id_info->kind = ID_INFO_KIND_LOCAL;
2752 }
2753}
2754
Damiend99b0522013-12-21 18:17:45 +00002755void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002756 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2757}
2758
Damiend99b0522013-12-21 18:17:45 +00002759void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002760 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2761}
2762
Damiend99b0522013-12-21 18:17:45 +00002763void 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 +01002764 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002765 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002766 // no more nested if/for; compile inner expression
2767 compile_node(comp, pn_inner_expr);
2768 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002769 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002770 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002771 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002772 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002773 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002774 } else {
2775 EMIT(yield_value);
2776 EMIT(pop_top);
2777 }
Damiend99b0522013-12-21 18:17:45 +00002778 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002779 // if condition
Damiend99b0522013-12-21 18:17:45 +00002780 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002781 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2782 pn_iter = pns_comp_if->nodes[1];
2783 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002784 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002785 // for loop
Damiend99b0522013-12-21 18:17:45 +00002786 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002787 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002788 int l_end2 = comp_next_label(comp);
2789 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002790 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002791 EMIT_ARG(label_assign, l_top2);
2792 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002793 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2794 compile_scope_comp_iter(comp, pns_comp_for2->nodes[2], pn_inner_expr, l_top2, for_depth + 1);
Damien Georgeb9791222014-01-23 00:34:21 +00002795 EMIT_ARG(jump, l_top2);
2796 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002797 EMIT(for_iter_end);
2798 } else {
2799 // shouldn't happen
2800 assert(0);
2801 }
2802}
2803
Damiend99b0522013-12-21 18:17:45 +00002804void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002805 // see http://www.python.org/dev/peps/pep-0257/
2806
2807 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002808 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002809 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002810 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002811 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002812 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2813 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002814 for (int i = 0; i < num_nodes; i++) {
2815 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002816 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 +00002817 // not a newline, so this is the first statement; finish search
2818 break;
2819 }
2820 }
2821 // 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 +00002822 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002823 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002824 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002825 } else {
2826 return;
2827 }
2828
2829 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002830 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2831 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2832 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2833 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2834 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002835 compile_node(comp, pns->nodes[0]); // a doc string
2836 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002837 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002838 }
2839 }
2840 }
2841}
2842
2843void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2844 comp->pass = pass;
2845 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002846 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002847 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002848
2849 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002850 // reset maximum stack sizes in scope
2851 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002852 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002853 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002854 }
2855
Damien5ac1b2e2013-10-18 19:58:12 +01002856#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002857 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002858 scope_print_info(scope);
2859 }
Damien5ac1b2e2013-10-18 19:58:12 +01002860#endif
Damien429d7192013-10-04 19:53:11 +01002861
2862 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002863 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2864 assert(scope->kind == SCOPE_MODULE);
2865 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2866 compile_node(comp, pns->nodes[0]); // compile the expression
2867 EMIT(return_value);
2868 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002869 if (!comp->is_repl) {
2870 check_for_doc_string(comp, scope->pn);
2871 }
Damien429d7192013-10-04 19:53:11 +01002872 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002873 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002874 EMIT(return_value);
2875 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002876 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2877 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2878 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002879
2880 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002881 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002882 if (comp->pass == PASS_1) {
2883 comp->have_bare_star = false;
2884 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2885 }
2886
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02002887 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01002888
2889 compile_node(comp, pns->nodes[3]); // 3 is function body
2890 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002891 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002892 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002893 EMIT(return_value);
2894 }
2895 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002896 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2897 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2898 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002899
2900 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002901 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002902 if (comp->pass == PASS_1) {
2903 comp->have_bare_star = false;
2904 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2905 }
2906
2907 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2908 EMIT(return_value);
2909 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2910 // a bit of a hack at the moment
2911
Damiend99b0522013-12-21 18:17:45 +00002912 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2913 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2914 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2915 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2916 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002917
Damien George55baff42014-01-21 21:40:13 +00002918 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01002919 if (comp->pass == PASS_1) {
2920 bool added;
2921 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2922 assert(added);
2923 id_info->kind = ID_INFO_KIND_LOCAL;
2924 scope->num_params = 1;
2925 }
2926
2927 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002928 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01002929 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002930 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01002931 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002932 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01002933 }
2934
Damienb05d7072013-10-05 13:37:10 +01002935 int l_end = comp_next_label(comp);
2936 int l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002937 EMIT_ARG(load_id, qstr_arg);
2938 EMIT_ARG(label_assign, l_top);
2939 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01002940 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2941 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00002942 EMIT_ARG(jump, l_top);
2943 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002944 EMIT(for_iter_end);
2945
2946 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00002947 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002948 }
2949 EMIT(return_value);
2950 } else {
2951 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002952 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2953 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2954 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01002955
2956 if (comp->pass == PASS_1) {
2957 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002958 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01002959 assert(added);
2960 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01002961 }
2962
Damien Georgeb9791222014-01-23 00:34:21 +00002963 EMIT_ARG(load_id, MP_QSTR___name__);
2964 EMIT_ARG(store_id, MP_QSTR___module__);
2965 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
2966 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01002967
2968 check_for_doc_string(comp, pns->nodes[2]);
2969 compile_node(comp, pns->nodes[2]); // 2 is class body
2970
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002971 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01002972 assert(id != NULL);
2973 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00002974 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002975 } else {
Damien George6baf76e2013-12-30 22:32:17 +00002976#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00002977 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002978#else
Damien George35e2a4e2014-02-05 00:51:47 +00002979 EMIT_ARG(load_fast, MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00002980#endif
Damien429d7192013-10-04 19:53:11 +01002981 }
2982 EMIT(return_value);
2983 }
2984
Damien415eb6f2013-10-05 12:19:06 +01002985 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00002986
2987 // make sure we match all the exception levels
2988 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01002989}
2990
Damien George094d4502014-04-02 17:31:27 +01002991#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01002992void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2993 comp->pass = pass;
2994 comp->scope_cur = scope;
2995 comp->next_label = 1;
2996
2997 if (scope->kind != SCOPE_FUNCTION) {
2998 printf("Error: inline assembler must be a function\n");
2999 return;
3000 }
3001
Damiena2f2f7d2013-10-06 00:14:13 +01003002 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003003 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003004 }
3005
Damien826005c2013-10-05 23:17:28 +01003006 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003007 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3008 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3009 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003010
Damiend99b0522013-12-21 18:17:45 +00003011 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003012
Damiena2f2f7d2013-10-06 00:14:13 +01003013 // parameters are in pns->nodes[1]
3014 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00003015 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003016 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00003017 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003018 }
3019
Damiend99b0522013-12-21 18:17:45 +00003020 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003021
Damiend99b0522013-12-21 18:17:45 +00003022 mp_parse_node_t pn_body = pns->nodes[3]; // body
3023 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003024 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3025
Damien Georgecbd2f742014-01-19 11:48:48 +00003026 /*
Damien826005c2013-10-05 23:17:28 +01003027 if (comp->pass == PASS_3) {
3028 //printf("----\n");
3029 scope_print_info(scope);
3030 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003031 */
Damien826005c2013-10-05 23:17:28 +01003032
3033 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003034 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3035 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
3036 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
3037 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3038 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3039 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3040 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3041 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3042 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3043 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3044 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3045 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3046 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003047 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3048
3049 // emit instructions
3050 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00003051 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgef41fdd02014-03-03 23:19:11 +00003052 compile_syntax_error(comp, "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003053 return;
3054 }
3055 int lab = comp_next_label(comp);
3056 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003057 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003058 }
3059 } else {
3060 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003061 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003062 }
3063 }
3064 }
3065
3066 if (comp->pass > PASS_1) {
3067 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01003068 }
Damien429d7192013-10-04 19:53:11 +01003069}
Damien George094d4502014-04-02 17:31:27 +01003070#endif
Damien429d7192013-10-04 19:53:11 +01003071
3072void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3073 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003074 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003075 scope->num_locals = 0;
3076 for (int i = 0; i < scope->id_info_len; i++) {
3077 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003078 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003079 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3080 continue;
3081 }
3082 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3083 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3084 }
Damien9ecbcff2013-12-11 00:41:43 +00003085 // note: params always count for 1 local, even if they are a cell
Damien429d7192013-10-04 19:53:11 +01003086 if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
3087 id->local_num = scope->num_locals;
3088 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003089 }
3090 }
3091
3092 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003093#if MICROPY_EMIT_CPYTHON
3094 int num_cell = 0;
3095#endif
Damien9ecbcff2013-12-11 00:41:43 +00003096 for (int i = 0; i < scope->id_info_len; i++) {
3097 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003098#if MICROPY_EMIT_CPYTHON
3099 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003100 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003101 id->local_num = num_cell;
3102 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003103 }
Damien George6baf76e2013-12-30 22:32:17 +00003104#else
3105 // in Micro Python the cells come right after the fast locals
3106 // parameters are not counted here, since they remain at the start
3107 // of the locals, even if they are cell vars
3108 if (!id->param && id->kind == ID_INFO_KIND_CELL) {
3109 id->local_num = scope->num_locals;
3110 scope->num_locals += 1;
3111 }
3112#endif
Damien9ecbcff2013-12-11 00:41:43 +00003113 }
Damien9ecbcff2013-12-11 00:41:43 +00003114
3115 // compute the index of free vars (freevars[idx] in CPython)
3116 // make sure they are in the order of the parent scope
3117 if (scope->parent != NULL) {
3118 int num_free = 0;
3119 for (int i = 0; i < scope->parent->id_info_len; i++) {
3120 id_info_t *id = &scope->parent->id_info[i];
3121 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3122 for (int j = 0; j < scope->id_info_len; j++) {
3123 id_info_t *id2 = &scope->id_info[j];
3124 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +00003125 assert(!id2->param); // free vars should not be params
3126#if MICROPY_EMIT_CPYTHON
3127 // in CPython the frees are numbered after the cells
3128 id2->local_num = num_cell + num_free;
3129#else
3130 // in Micro Python the frees come first, before the params
3131 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003132#endif
3133 num_free += 1;
3134 }
3135 }
3136 }
Damien429d7192013-10-04 19:53:11 +01003137 }
Damien George6baf76e2013-12-30 22:32:17 +00003138#if !MICROPY_EMIT_CPYTHON
3139 // in Micro Python shift all other locals after the free locals
3140 if (num_free > 0) {
3141 for (int i = 0; i < scope->id_info_len; i++) {
3142 id_info_t *id = &scope->id_info[i];
3143 if (id->param || id->kind != ID_INFO_KIND_FREE) {
3144 id->local_num += num_free;
3145 }
3146 }
3147 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3148 scope->num_locals += num_free;
3149 }
3150#endif
Damien429d7192013-10-04 19:53:11 +01003151 }
3152
Damien George8725f8f2014-02-15 19:33:11 +00003153 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003154
3155#if MICROPY_EMIT_CPYTHON
3156 // these flags computed here are for CPython compatibility only
3157 if (scope->kind == SCOPE_FUNCTION) {
Damien George8725f8f2014-02-15 19:33:11 +00003158 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien429d7192013-10-04 19:53:11 +01003159 }
3160 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) {
3161 assert(scope->parent != NULL);
Damien George8725f8f2014-02-15 19:33:11 +00003162 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien429d7192013-10-04 19:53:11 +01003163
3164 // TODO possibly other ways it can be nested
Damien George08d07552014-01-29 18:58:52 +00003165 // Note that we don't actually use this information at the moment (for CPython compat only)
3166 if ((SCOPE_FUNCTION <= scope->parent->kind && scope->parent->kind <= SCOPE_SET_COMP) || (scope->parent->kind == SCOPE_CLASS && scope->parent->parent->kind == SCOPE_FUNCTION)) {
Damien George8725f8f2014-02-15 19:33:11 +00003167 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003168 }
3169 }
Damien George882b3632014-04-02 15:56:31 +01003170#endif
3171
Damien429d7192013-10-04 19:53:11 +01003172 int num_free = 0;
3173 for (int i = 0; i < scope->id_info_len; i++) {
3174 id_info_t *id = &scope->id_info[i];
3175 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3176 num_free += 1;
3177 }
3178 }
3179 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003180 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003181 }
3182}
3183
Damien George65cad122014-04-06 11:48:15 +01003184mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is_repl) {
Damien429d7192013-10-04 19:53:11 +01003185 compiler_t *comp = m_new(compiler_t, 1);
3186
Damien Georgecbd2f742014-01-19 11:48:48 +00003187 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003188 comp->is_repl = is_repl;
3189 comp->had_error = false;
3190
Damien429d7192013-10-04 19:53:11 +01003191 comp->break_label = 0;
3192 comp->continue_label = 0;
Damien Georgecbddb272014-02-01 20:08:18 +00003193 comp->break_continue_except_level = 0;
3194 comp->cur_except_level = 0;
3195
Damien George35e2a4e2014-02-05 00:51:47 +00003196 comp->func_arg_is_super = false;
3197
Damien429d7192013-10-04 19:53:11 +01003198 comp->scope_head = NULL;
3199 comp->scope_cur = NULL;
3200
Damien826005c2013-10-05 23:17:28 +01003201 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003202 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003203
3204 // set the outer scope
Damien George65cad122014-04-06 11:48:15 +01003205 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
Damien429d7192013-10-04 19:53:11 +01003206
Damien826005c2013-10-05 23:17:28 +01003207 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003208 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003209 comp->emit_method_table = &emit_pass1_method_table;
3210 comp->emit_inline_asm = NULL;
3211 comp->emit_inline_asm_method_table = NULL;
3212 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003213 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003214 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003215#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003216 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003217 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003218#endif
Damien826005c2013-10-05 23:17:28 +01003219 } else {
3220 compile_scope(comp, s, PASS_1);
3221 }
3222
3223 // update maximim number of labels needed
3224 if (comp->next_label > max_num_labels) {
3225 max_num_labels = comp->next_label;
3226 }
Damien429d7192013-10-04 19:53:11 +01003227 }
3228
Damien826005c2013-10-05 23:17:28 +01003229 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003230 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003231 compile_scope_compute_things(comp, s);
3232 }
3233
Damien826005c2013-10-05 23:17:28 +01003234 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003235 emit_pass1_free(comp->emit);
3236
Damien826005c2013-10-05 23:17:28 +01003237 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003238#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003239 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003240#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003241 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003242#endif
Damien3ef4abb2013-10-12 16:53:13 +01003243#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003244 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003245#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003246#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003247 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003248 if (false) {
3249 // dummy
3250
Damien3ef4abb2013-10-12 16:53:13 +01003251#if MICROPY_EMIT_INLINE_THUMB
Damien George65cad122014-04-06 11:48:15 +01003252 } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
Damienc025ebb2013-10-12 14:30:21 +01003253 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003254 if (emit_inline_thumb == NULL) {
3255 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3256 }
3257 comp->emit = NULL;
3258 comp->emit_method_table = NULL;
3259 comp->emit_inline_asm = emit_inline_thumb;
3260 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3261 compile_scope_inline_asm(comp, s, PASS_2);
3262 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003263#endif
3264
Damien826005c2013-10-05 23:17:28 +01003265 } else {
Damienc025ebb2013-10-12 14:30:21 +01003266
3267 // choose the emit type
3268
Damien3ef4abb2013-10-12 16:53:13 +01003269#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003270 comp->emit = emit_cpython_new(max_num_labels);
3271 comp->emit_method_table = &emit_cpython_method_table;
3272#else
Damien826005c2013-10-05 23:17:28 +01003273 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003274
3275#if MICROPY_EMIT_NATIVE
Damien George65cad122014-04-06 11:48:15 +01003276 case MP_EMIT_OPT_NATIVE_PYTHON:
3277 case MP_EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003278#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003279 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003280 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003281 }
Damien13ed3a62013-10-08 09:05:10 +01003282 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003283#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003284 if (emit_native == NULL) {
3285 emit_native = emit_native_thumb_new(max_num_labels);
3286 }
3287 comp->emit_method_table = &emit_native_thumb_method_table;
3288#endif
3289 comp->emit = emit_native;
Damien George65cad122014-04-06 11:48:15 +01003290 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003291 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003292#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003293
Damien826005c2013-10-05 23:17:28 +01003294 default:
3295 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003296 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003297 }
3298 comp->emit = emit_bc;
3299 comp->emit_method_table = &emit_bc_method_table;
3300 break;
3301 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003302#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003303
3304 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003305 compile_scope(comp, s, PASS_2);
3306 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003307 }
Damien429d7192013-10-04 19:53:11 +01003308 }
3309
Damien George41d02b62014-01-24 22:42:28 +00003310 // free the emitters
3311#if !MICROPY_EMIT_CPYTHON
3312 if (emit_bc != NULL) {
3313 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003314 }
Damien George41d02b62014-01-24 22:42:28 +00003315#if MICROPY_EMIT_NATIVE
3316 if (emit_native != NULL) {
3317#if MICROPY_EMIT_X64
3318 emit_native_x64_free(emit_native);
3319#elif MICROPY_EMIT_THUMB
3320 emit_native_thumb_free(emit_native);
3321#endif
3322 }
3323#endif
3324#if MICROPY_EMIT_INLINE_THUMB
3325 if (emit_inline_thumb != NULL) {
3326 emit_inline_thumb_free(emit_inline_thumb);
3327 }
3328#endif
3329#endif // !MICROPY_EMIT_CPYTHON
3330
3331 // free the scopes
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003332 uint unique_code_id = module_scope->unique_code_id;
3333 for (scope_t *s = module_scope; s;) {
3334 scope_t *next = s->next;
3335 scope_free(s);
3336 s = next;
3337 }
Damien5ac1b2e2013-10-18 19:58:12 +01003338
Damien George41d02b62014-01-24 22:42:28 +00003339 // free the compiler
3340 bool had_error = comp->had_error;
3341 m_del_obj(compiler_t, comp);
3342
Damien George1fb03172014-01-03 14:22:03 +00003343 if (had_error) {
3344 // TODO return a proper error message
3345 return mp_const_none;
3346 } else {
3347#if MICROPY_EMIT_CPYTHON
3348 // can't create code, so just return true
Damien George41d02b62014-01-24 22:42:28 +00003349 (void)unique_code_id; // to suppress warning that unique_code_id is unused
Damien George1fb03172014-01-03 14:22:03 +00003350 return mp_const_true;
3351#else
3352 // return function that executes the outer module
Damien Georged1e443d2014-03-29 11:39:36 +00003353 // we can free the unique_code slot because no-one has reference to this unique_code_id anymore
Damien Georgee337f1e2014-03-31 15:18:37 +01003354 return mp_make_function_from_id(unique_code_id, true, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003355#endif
3356 }
Damien429d7192013-10-04 19:53:11 +01003357}