blob: 25830eb6f98be0886bd03dbbf65fa59306422b63 [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"
Rachel Dowdallcde86312014-03-22 17:29:27 +000020#include "intdivmod.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
Damien6cdd3af2013-10-05 18:08:26 +010039#define EMIT_OPT_NONE (0)
40#define EMIT_OPT_BYTE_CODE (1)
41#define EMIT_OPT_NATIVE_PYTHON (2)
Damien7af3d192013-10-07 00:02:49 +010042#define EMIT_OPT_VIPER (3)
43#define EMIT_OPT_ASM_THUMB (4)
Damien6cdd3af2013-10-05 18:08:26 +010044
Damien429d7192013-10-04 19:53:11 +010045typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000046 qstr source_file;
Damien5ac1b2e2013-10-18 19:58:12 +010047 bool is_repl;
Damien429d7192013-10-04 19:53:11 +010048 pass_kind_t pass;
Damien5ac1b2e2013-10-18 19:58:12 +010049 bool had_error; // try to keep compiler clean from nlr
Damien429d7192013-10-04 19:53:11 +010050
Damienb05d7072013-10-05 13:37:10 +010051 int next_label;
Damienb05d7072013-10-05 13:37:10 +010052
Damien429d7192013-10-04 19:53:11 +010053 int break_label;
54 int continue_label;
Damien Georgecbddb272014-02-01 20:08:18 +000055 int break_continue_except_level;
56 int cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien429d7192013-10-04 19:53:11 +010057
58 int n_arg_keyword;
59 bool have_star_arg;
60 bool have_dbl_star_arg;
61 bool have_bare_star;
62 int param_pass;
63 int param_pass_num_dict_params;
64 int param_pass_num_default_params;
65
Damien George35e2a4e2014-02-05 00:51:47 +000066 bool func_arg_is_super; // used to compile special case of super() function call
67
Damien429d7192013-10-04 19:53:11 +010068 scope_t *scope_head;
69 scope_t *scope_cur;
70
Damien6cdd3af2013-10-05 18:08:26 +010071 emit_t *emit; // current emitter
72 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010073
74 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
75 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 +010076} compiler_t;
77
Damien Georgef41fdd02014-03-03 23:19:11 +000078STATIC void compile_syntax_error(compiler_t *comp, const char *msg) {
79 // TODO store the error message to a variable in compiler_t instead of printing it
80 printf("SyntaxError: %s\n", msg);
81 comp->had_error = true;
82}
83
Damiend99b0522013-12-21 18:17:45 +000084mp_parse_node_t fold_constants(mp_parse_node_t pn) {
85 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
86 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
87 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +010088
89 // fold arguments first
90 for (int i = 0; i < n; i++) {
91 pns->nodes[i] = fold_constants(pns->nodes[i]);
92 }
93
Damiend99b0522013-12-21 18:17:45 +000094 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +010095 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +000096 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 +020097 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
98 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +000099 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien3ef4abb2013-10-12 16:53:13 +0100100#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100101 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000102 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
Damien0efb3a12013-10-12 16:16:56 +0100103#endif
Damiend99b0522013-12-21 18:17:45 +0000104 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
105 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100106 } else {
107 // shouldn't happen
108 assert(0);
109 }
110 }
111 break;
112
113 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100114 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000115 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 +0200116 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
117 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damien0efb3a12013-10-12 16:16:56 +0100118 machine_int_t res;
Damiend99b0522013-12-21 18:17:45 +0000119 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100120 res = arg0 + arg1;
Damiend99b0522013-12-21 18:17:45 +0000121 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100122 res = arg0 - arg1;
Damien429d7192013-10-04 19:53:11 +0100123 } else {
124 // shouldn't happen
125 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100126 res = 0;
127 }
Paul Sokolovskybbf0e2f2014-02-21 02:04:32 +0200128 if (MP_PARSE_FITS_SMALL_INT(res)) {
Damiend99b0522013-12-21 18:17:45 +0000129 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, res);
Damien429d7192013-10-04 19:53:11 +0100130 }
131 }
132 break;
133
134 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000135 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 +0200136 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
137 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000138 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien3ef4abb2013-10-12 16:53:13 +0100139#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100140 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000141 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 * arg1);
Damien0efb3a12013-10-12 16:16:56 +0100142#endif
Damiend99b0522013-12-21 18:17:45 +0000143 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien429d7192013-10-04 19:53:11 +0100144 ; // pass
Damiend99b0522013-12-21 18:17:45 +0000145 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Rachel Dowdallcde86312014-03-22 17:29:27 +0000146 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, python_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000147 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300148 if (arg1 != 0) {
149 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, python_floor_divide(arg0, arg1));
150 }
Damien429d7192013-10-04 19:53:11 +0100151 } else {
152 // shouldn't happen
153 assert(0);
154 }
155 }
156 break;
157
158 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000159 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200160 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000161 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
162 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
163 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
164 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
165 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
166 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100167 } else {
168 // shouldn't happen
169 assert(0);
170 }
171 }
172 break;
173
Damien3ef4abb2013-10-12 16:53:13 +0100174#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +0100175 case PN_power:
Damien0efb3a12013-10-12 16:16:56 +0100176 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000177 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])) {
178 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
179 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200180 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100181 if (power >= 0) {
182 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200183 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100184 for (; power > 0; power--) {
185 ans *= base;
186 }
Damiend99b0522013-12-21 18:17:45 +0000187 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100188 }
189 }
190 }
191 break;
Damien0efb3a12013-10-12 16:16:56 +0100192#endif
Damien429d7192013-10-04 19:53:11 +0100193 }
194 }
195
196 return pn;
197}
198
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200199STATIC 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 +0000200STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100201
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200202STATIC int comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100203 return comp->next_label++;
204}
205
Damien George8dcc0c72014-03-27 10:55:21 +0000206STATIC void compile_increase_except_level(compiler_t *comp) {
207 comp->cur_except_level += 1;
208 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
209 comp->scope_cur->exc_stack_size = comp->cur_except_level;
210 }
211}
212
213STATIC void compile_decrease_except_level(compiler_t *comp) {
214 assert(comp->cur_except_level > 0);
215 comp->cur_except_level -= 1;
216}
217
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200218STATIC 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 +0000219 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 +0100220 scope->parent = comp->scope_cur;
221 scope->next = NULL;
222 if (comp->scope_head == NULL) {
223 comp->scope_head = scope;
224 } else {
225 scope_t *s = comp->scope_head;
226 while (s->next != NULL) {
227 s = s->next;
228 }
229 s->next = scope;
230 }
231 return scope;
232}
233
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200234STATIC int list_len(mp_parse_node_t pn, int pn_kind) {
Damiend99b0522013-12-21 18:17:45 +0000235 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100236 return 0;
Damiend99b0522013-12-21 18:17:45 +0000237 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100238 return 1;
239 } else {
Damiend99b0522013-12-21 18:17:45 +0000240 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
241 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100242 return 1;
243 } else {
Damiend99b0522013-12-21 18:17:45 +0000244 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100245 }
246 }
247}
248
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200249STATIC 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 +0000250 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
251 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
252 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100253 for (int i = 0; i < num_nodes; i++) {
254 f(comp, pns->nodes[i]);
255 }
Damiend99b0522013-12-21 18:17:45 +0000256 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100257 f(comp, pn);
258 }
259}
260
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200261STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000262 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100263 *nodes = NULL;
264 return 0;
Damiend99b0522013-12-21 18:17:45 +0000265 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100266 *nodes = pn;
267 return 1;
268 } else {
Damiend99b0522013-12-21 18:17:45 +0000269 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
270 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100271 *nodes = pn;
272 return 1;
273 } else {
274 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000275 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100276 }
277 }
278}
279
Damiend99b0522013-12-21 18:17:45 +0000280void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100281}
282
Damiend99b0522013-12-21 18:17:45 +0000283void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
284 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100285 for (int i = 0; i < num_nodes; i++) {
286 compile_node(comp, pns->nodes[i]);
287 }
288}
289
Damien3ef4abb2013-10-12 16:53:13 +0100290#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200291STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000292 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100293 return false;
294 }
Damiend99b0522013-12-21 18:17:45 +0000295 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100296 return false;
297 }
298 return true;
299}
300
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200301STATIC void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
Damien George55baff42014-01-21 21:40:13 +0000302 uint len;
303 const byte *str = qstr_data(qstr, &len);
Damien02f89412013-12-12 15:13:36 +0000304 bool has_single_quote = false;
305 bool has_double_quote = false;
306 for (int i = 0; i < len; i++) {
307 if (str[i] == '\'') {
308 has_single_quote = true;
309 } else if (str[i] == '"') {
310 has_double_quote = true;
311 }
312 }
313 if (bytes) {
314 vstr_printf(vstr, "b");
315 }
316 bool quote_single = false;
317 if (has_single_quote && !has_double_quote) {
318 vstr_printf(vstr, "\"");
319 } else {
320 quote_single = true;
321 vstr_printf(vstr, "'");
322 }
323 for (int i = 0; i < len; i++) {
324 if (str[i] == '\n') {
325 vstr_printf(vstr, "\\n");
326 } else if (str[i] == '\\') {
327 vstr_printf(vstr, "\\\\");
328 } else if (str[i] == '\'' && quote_single) {
329 vstr_printf(vstr, "\\'");
330 } else {
331 vstr_printf(vstr, "%c", str[i]);
332 }
333 }
334 if (has_single_quote && !has_double_quote) {
335 vstr_printf(vstr, "\"");
336 } else {
337 vstr_printf(vstr, "'");
338 }
339}
340
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200341STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damiend99b0522013-12-21 18:17:45 +0000342 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200343 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
344 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
345 return;
346 }
347
Damiend99b0522013-12-21 18:17:45 +0000348 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
349 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
350 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000351 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
352 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
353 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
354 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
355 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100356 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000357 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
358 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
359 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien429d7192013-10-04 19:53:11 +0100360 default: assert(0);
361 }
362 break;
363 default: assert(0);
364 }
365}
366
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200367STATIC 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 +0100368 int n = 0;
369 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000370 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100371 }
372 int total = n;
373 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000374 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100375 total += 1;
Damien3a205172013-10-12 15:01:56 +0100376 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100377 is_const = false;
378 }
379 }
380 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100381 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100382 is_const = false;
383 break;
384 }
385 }
386 if (total > 0 && is_const) {
387 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000388 vstr_t *vstr = vstr_new();
389 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000390 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000391 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100392 need_comma = true;
393 }
394 for (int i = 0; i < n; i++) {
395 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000396 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100397 }
Damien02f89412013-12-12 15:13:36 +0000398 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100399 need_comma = true;
400 }
401 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000402 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100403 } else {
Damien02f89412013-12-12 15:13:36 +0000404 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100405 }
Damien Georgeb9791222014-01-23 00:34:21 +0000406 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000407 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100408 } else {
Damiend99b0522013-12-21 18:17:45 +0000409 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100410 compile_node(comp, pn);
411 }
412 for (int i = 0; i < n; i++) {
413 compile_node(comp, pns_list->nodes[i]);
414 }
Damien Georgeb9791222014-01-23 00:34:21 +0000415 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100416 }
417}
Damien3a205172013-10-12 15:01:56 +0100418#endif
419
420// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damiend99b0522013-12-21 18:17:45 +0000421void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100422#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100423 cpython_c_tuple(comp, pn, pns_list);
424#else
425 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000426 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100427 compile_node(comp, pn);
428 total += 1;
429 }
430 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000431 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100432 for (int i = 0; i < n; i++) {
433 compile_node(comp, pns_list->nodes[i]);
434 }
435 total += n;
436 }
Damien Georgeb9791222014-01-23 00:34:21 +0000437 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100438#endif
439}
Damien429d7192013-10-04 19:53:11 +0100440
Damiend99b0522013-12-21 18:17:45 +0000441void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100442 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000443 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100444}
445
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200446STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000447 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200448 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100449}
450
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200451STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200452 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 +0100453}
454
Damien3ef4abb2013-10-12 16:53:13 +0100455#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100456// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200457STATIC 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 +0100458 if (node_is_const_false(pn)) {
459 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000460 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100461 }
462 return;
463 } else if (node_is_const_true(pn)) {
464 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000465 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100466 }
467 return;
Damiend99b0522013-12-21 18:17:45 +0000468 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
469 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
470 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
471 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100472 if (jump_if == false) {
Damienb05d7072013-10-05 13:37:10 +0100473 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100474 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100475 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100476 }
Damien3a205172013-10-12 15:01:56 +0100477 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000478 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100479 } else {
480 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100481 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100482 }
483 }
484 return;
Damiend99b0522013-12-21 18:17:45 +0000485 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100486 if (jump_if == false) {
487 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100488 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100489 }
490 } else {
Damienb05d7072013-10-05 13:37:10 +0100491 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100492 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100493 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100494 }
Damien3a205172013-10-12 15:01:56 +0100495 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000496 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100497 }
498 return;
Damiend99b0522013-12-21 18:17:45 +0000499 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100500 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100501 return;
502 }
503 }
504
505 // nothing special, fall back to default compiling for node and jump
506 compile_node(comp, pn);
507 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000508 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100509 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000510 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100511 }
512}
Damien3a205172013-10-12 15:01:56 +0100513#endif
Damien429d7192013-10-04 19:53:11 +0100514
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200515STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100516#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100517 cpython_c_if_cond(comp, pn, jump_if, label, false);
518#else
519 if (node_is_const_false(pn)) {
520 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000521 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100522 }
523 return;
524 } else if (node_is_const_true(pn)) {
525 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000526 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100527 }
528 return;
Damiend99b0522013-12-21 18:17:45 +0000529 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
530 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
531 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
532 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100533 if (jump_if == false) {
534 int label2 = comp_next_label(comp);
535 for (int i = 0; i < n - 1; i++) {
536 c_if_cond(comp, pns->nodes[i], true, label2);
537 }
538 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000539 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100540 } else {
541 for (int i = 0; i < n; i++) {
542 c_if_cond(comp, pns->nodes[i], true, label);
543 }
544 }
545 return;
Damiend99b0522013-12-21 18:17:45 +0000546 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100547 if (jump_if == false) {
548 for (int i = 0; i < n; i++) {
549 c_if_cond(comp, pns->nodes[i], false, label);
550 }
551 } else {
552 int label2 = comp_next_label(comp);
553 for (int i = 0; i < n - 1; i++) {
554 c_if_cond(comp, pns->nodes[i], false, label2);
555 }
556 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000557 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100558 }
559 return;
Damiend99b0522013-12-21 18:17:45 +0000560 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100561 c_if_cond(comp, pns->nodes[0], !jump_if, label);
562 return;
563 }
564 }
565
566 // nothing special, fall back to default compiling for node and jump
567 compile_node(comp, pn);
568 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000569 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100570 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000571 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100572 }
573#endif
Damien429d7192013-10-04 19:53:11 +0100574}
575
576typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000577void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100578
Damiend99b0522013-12-21 18:17:45 +0000579void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100580 if (assign_kind != ASSIGN_AUG_STORE) {
581 compile_node(comp, pns->nodes[0]);
582 }
583
Damiend99b0522013-12-21 18:17:45 +0000584 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
585 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
586 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
587 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100588 if (assign_kind != ASSIGN_AUG_STORE) {
589 for (int i = 0; i < n - 1; i++) {
590 compile_node(comp, pns1->nodes[i]);
591 }
592 }
Damiend99b0522013-12-21 18:17:45 +0000593 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
594 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100595 }
Damiend99b0522013-12-21 18:17:45 +0000596 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000597 compile_syntax_error(comp, "can't assign to function call");
Damien429d7192013-10-04 19:53:11 +0100598 return;
Damiend99b0522013-12-21 18:17:45 +0000599 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100600 if (assign_kind == ASSIGN_AUG_STORE) {
601 EMIT(rot_three);
602 EMIT(store_subscr);
603 } else {
604 compile_node(comp, pns1->nodes[0]);
605 if (assign_kind == ASSIGN_AUG_LOAD) {
606 EMIT(dup_top_two);
Damien Georged17926d2014-03-30 13:35:08 +0100607 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100608 } else {
609 EMIT(store_subscr);
610 }
611 }
Damiend99b0522013-12-21 18:17:45 +0000612 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
613 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100614 if (assign_kind == ASSIGN_AUG_LOAD) {
615 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000616 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100617 } else {
618 if (assign_kind == ASSIGN_AUG_STORE) {
619 EMIT(rot_two);
620 }
Damien Georgeb9791222014-01-23 00:34:21 +0000621 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100622 }
623 } else {
624 // shouldn't happen
625 assert(0);
626 }
627 } else {
628 // shouldn't happen
629 assert(0);
630 }
631
Damiend99b0522013-12-21 18:17:45 +0000632 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +0100633 // SyntaxError, cannot assign
634 assert(0);
635 }
636}
637
Damiend99b0522013-12-21 18:17:45 +0000638void c_assign_tuple(compiler_t *comp, int n, mp_parse_node_t *nodes) {
Damien429d7192013-10-04 19:53:11 +0100639 assert(n >= 0);
640 int have_star_index = -1;
641 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000642 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100643 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000644 EMIT_ARG(unpack_ex, i, n - i - 1);
Damien429d7192013-10-04 19:53:11 +0100645 have_star_index = i;
646 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000647 compile_syntax_error(comp, "two starred expressions in assignment");
Damien429d7192013-10-04 19:53:11 +0100648 return;
649 }
650 }
651 }
652 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000653 EMIT_ARG(unpack_sequence, n);
Damien429d7192013-10-04 19:53:11 +0100654 }
655 for (int i = 0; i < n; i++) {
656 if (i == have_star_index) {
Damiend99b0522013-12-21 18:17:45 +0000657 c_assign(comp, ((mp_parse_node_struct_t*)nodes[i])->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100658 } else {
659 c_assign(comp, nodes[i], ASSIGN_STORE);
660 }
661 }
662}
663
664// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000665void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100666 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000667 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100668 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000669 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
670 if (MP_PARSE_NODE_IS_ID(pn)) {
671 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100672 switch (assign_kind) {
673 case ASSIGN_STORE:
674 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000675 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100676 break;
677 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000678 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100679 break;
680 }
681 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000682 compile_syntax_error(comp, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100683 return;
684 }
685 } else {
Damiend99b0522013-12-21 18:17:45 +0000686 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
687 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100688 case PN_power:
689 // lhs is an index or attribute
690 c_assign_power(comp, pns, assign_kind);
691 break;
692
693 case PN_testlist_star_expr:
694 case PN_exprlist:
695 // lhs is a tuple
696 if (assign_kind != ASSIGN_STORE) {
697 goto bad_aug;
698 }
Damiend99b0522013-12-21 18:17:45 +0000699 c_assign_tuple(comp, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100700 break;
701
702 case PN_atom_paren:
703 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000704 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100705 // empty tuple
Damien Georgef41fdd02014-03-03 23:19:11 +0000706 compile_syntax_error(comp, "can't assign to ()");
Damien429d7192013-10-04 19:53:11 +0100707 return;
Damiend99b0522013-12-21 18:17:45 +0000708 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
709 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100710 goto testlist_comp;
711 } else {
712 // parenthesis around 1 item, is just that item
713 pn = pns->nodes[0];
714 goto tail_recursion;
715 }
716 break;
717
718 case PN_atom_bracket:
719 // lhs is something in brackets
720 if (assign_kind != ASSIGN_STORE) {
721 goto bad_aug;
722 }
Damiend99b0522013-12-21 18:17:45 +0000723 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100724 // empty list, assignment allowed
725 c_assign_tuple(comp, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000726 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
727 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100728 goto testlist_comp;
729 } else {
730 // brackets around 1 item
731 c_assign_tuple(comp, 1, &pns->nodes[0]);
732 }
733 break;
734
735 default:
Damiend99b0522013-12-21 18:17:45 +0000736 printf("unknown assign, %u\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien429d7192013-10-04 19:53:11 +0100737 assert(0);
738 }
739 return;
740
741 testlist_comp:
742 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000743 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
744 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
745 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100746 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000747 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100748 c_assign_tuple(comp, 1, &pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000749 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100750 // sequence of many items
751 // TODO call c_assign_tuple instead
Damiend99b0522013-12-21 18:17:45 +0000752 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
Damien Georgeb9791222014-01-23 00:34:21 +0000753 EMIT_ARG(unpack_sequence, 1 + n);
Damien429d7192013-10-04 19:53:11 +0100754 c_assign(comp, pns->nodes[0], ASSIGN_STORE);
755 for (int i = 0; i < n; i++) {
756 c_assign(comp, pns2->nodes[i], ASSIGN_STORE);
757 }
Damiend99b0522013-12-21 18:17:45 +0000758 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +0100759 // TODO not implemented
760 assert(0);
761 } else {
762 // sequence with 2 items
763 goto sequence_with_2_items;
764 }
765 } else {
766 // sequence with 2 items
767 sequence_with_2_items:
768 c_assign_tuple(comp, 2, pns->nodes);
769 }
770 return;
771 }
772 return;
773
774 bad_aug:
Damien Georgef41fdd02014-03-03 23:19:11 +0000775 compile_syntax_error(comp, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100776}
777
778// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100779// if we are not in CPython compatibility mode then:
780// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
781// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
782// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George30565092014-03-31 11:30:17 +0100783void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
784 assert(n_pos_defaults >= 0);
785 assert(n_kw_defaults >= 0);
786
Damien429d7192013-10-04 19:53:11 +0100787 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000788 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100789 int nfree = 0;
790 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000791 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
792 id_info_t *id = &comp->scope_cur->id_info[i];
793 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
794 for (int j = 0; j < this_scope->id_info_len; j++) {
795 id_info_t *id2 = &this_scope->id_info[j];
796 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000797#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000798 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000799#else
800 // in Micro Python we load closures using LOAD_FAST
Damien Georgeb9791222014-01-23 00:34:21 +0000801 EMIT_ARG(load_fast, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000802#endif
Damien318aec62013-12-10 18:28:17 +0000803 nfree += 1;
804 }
805 }
Damien429d7192013-10-04 19:53:11 +0100806 }
807 }
808 }
Damien429d7192013-10-04 19:53:11 +0100809
810 // make the function/closure
811 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100812 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100813 } else {
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000814 EMIT_ARG(build_tuple, nfree);
Damien George30565092014-03-31 11:30:17 +0100815 EMIT_ARG(make_closure, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100816 }
817}
818
Damiend99b0522013-12-21 18:17:45 +0000819void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000820 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damiend99b0522013-12-21 18:17:45 +0000821 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
822 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100823 // bare star
824 comp->have_bare_star = true;
825 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000826
827 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
828 // TODO do we need to do anything with this?
829
830 } else {
831 mp_parse_node_t pn_id;
832 mp_parse_node_t pn_colon;
833 mp_parse_node_t pn_equal;
834 if (MP_PARSE_NODE_IS_ID(pn)) {
835 // this parameter is just an id
836
837 pn_id = pn;
838 pn_colon = MP_PARSE_NODE_NULL;
839 pn_equal = MP_PARSE_NODE_NULL;
840
841 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
842 // this parameter has a colon and/or equal specifier
843
844 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
845 pn_id = pns->nodes[0];
846 pn_colon = pns->nodes[1];
847 pn_equal = pns->nodes[2];
848
849 } else {
850 assert(0);
851 return;
852 }
853
854 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
855 // this parameter does not have a default value
856
857 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
858 if (!comp->have_bare_star && comp->param_pass_num_default_params != 0) {
859 compile_syntax_error(comp, "non-default argument follows default argument");
860 return;
861 }
862
863 } else {
864 // this parameter has a default value
865 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
866
867 if (comp->have_bare_star) {
868 comp->param_pass_num_dict_params += 1;
869 if (comp->param_pass == 1) {
Damien Georgee337f1e2014-03-31 15:18:37 +0100870#if !MICROPY_EMIT_CPYTHON
871 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
872 if (comp->param_pass_num_dict_params == 1) {
873 // first default dict param, so make the map
874 EMIT_ARG(build_map, 0);
875 }
876#endif
Damien Georgef41fdd02014-03-03 23:19:11 +0000877 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pn_id));
878 compile_node(comp, pn_equal);
Damien Georgee337f1e2014-03-31 15:18:37 +0100879#if !MICROPY_EMIT_CPYTHON
880 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
881 EMIT(store_map);
882#endif
Damien Georgef41fdd02014-03-03 23:19:11 +0000883 }
884 } else {
885 comp->param_pass_num_default_params += 1;
886 if (comp->param_pass == 2) {
887 compile_node(comp, pn_equal);
888 }
889 }
890 }
891
892 // TODO pn_colon not implemented
893 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100894 }
895}
896
897// leaves function object on stack
898// returns function name
Damiend99b0522013-12-21 18:17:45 +0000899qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100900 if (comp->pass == PASS_1) {
901 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000902 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100903 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000904 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100905 }
906
907 // save variables (probably don't need to do this, since we can't have nested definitions..?)
908 bool old_have_bare_star = comp->have_bare_star;
909 int old_param_pass = comp->param_pass;
910 int old_param_pass_num_dict_params = comp->param_pass_num_dict_params;
911 int old_param_pass_num_default_params = comp->param_pass_num_default_params;
912
913 // compile default parameters
Damien Georgef41fdd02014-03-03 23:19:11 +0000914
915 // pass 1 does any default parameters after bare star
Damien429d7192013-10-04 19:53:11 +0100916 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000917 comp->param_pass = 1;
Damien429d7192013-10-04 19:53:11 +0100918 comp->param_pass_num_dict_params = 0;
919 comp->param_pass_num_default_params = 0;
920 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +0000921
922 if (comp->had_error) {
923 return MP_QSTR_NULL;
924 }
925
926 // pass 2 does any default parameters before bare star
Damien429d7192013-10-04 19:53:11 +0100927 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000928 comp->param_pass = 2;
Damien429d7192013-10-04 19:53:11 +0100929 comp->param_pass_num_dict_params = 0;
930 comp->param_pass_num_default_params = 0;
931 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
932
Damien Georgee337f1e2014-03-31 15:18:37 +0100933#if !MICROPY_EMIT_CPYTHON
934 // in Micro Python we put the default positional parameters into a tuple using the bytecode
935 if (comp->param_pass_num_default_params > 0) {
936 EMIT_ARG(build_tuple, comp->param_pass_num_default_params);
937 }
938#endif
939
Damien429d7192013-10-04 19:53:11 +0100940 // get the scope for this function
941 scope_t *fscope = (scope_t*)pns->nodes[4];
942
943 // make the function
Damien George30565092014-03-31 11:30:17 +0100944 close_over_variables_etc(comp, fscope, comp->param_pass_num_default_params, comp->param_pass_num_dict_params);
Damien429d7192013-10-04 19:53:11 +0100945
946 // restore variables
947 comp->have_bare_star = old_have_bare_star;
948 comp->param_pass = old_param_pass;
949 comp->param_pass_num_dict_params = old_param_pass_num_dict_params;
950 comp->param_pass_num_default_params = old_param_pass_num_default_params;
951
952 // return its name (the 'f' in "def f(...):")
953 return fscope->simple_name;
954}
955
956// leaves class object on stack
957// returns class name
Damiend99b0522013-12-21 18:17:45 +0000958qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100959 if (comp->pass == PASS_1) {
960 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000961 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100962 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000963 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100964 }
965
966 EMIT(load_build_class);
967
968 // scope for this class
969 scope_t *cscope = (scope_t*)pns->nodes[3];
970
971 // compile the class
972 close_over_variables_etc(comp, cscope, 0, 0);
973
974 // get its name
Damien Georgeb9791222014-01-23 00:34:21 +0000975 EMIT_ARG(load_const_id, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100976
977 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +0100978 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
979 mp_parse_node_t parents = pns->nodes[1];
980 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
981 parents = MP_PARSE_NODE_NULL;
982 }
Damien Georgebbcd49a2014-02-06 20:30:16 +0000983 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +0100984 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +0100985
986 // return its name (the 'C' in class C(...):")
987 return cscope->simple_name;
988}
989
Damien6cdd3af2013-10-05 18:08:26 +0100990// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200991STATIC 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 +0000992 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100993 return false;
994 }
995
996 if (name_len != 2) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000997 compile_syntax_error(comp, "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100998 return true;
999 }
1000
Damiend99b0522013-12-21 18:17:45 +00001001 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001002 if (attr == MP_QSTR_byte_code) {
Damien5ac1b2e2013-10-18 19:58:12 +01001003 *emit_options = EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +01001004#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001005 } else if (attr == MP_QSTR_native) {
Damien6cdd3af2013-10-05 18:08:26 +01001006 *emit_options = EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001007 } else if (attr == MP_QSTR_viper) {
Damien7af3d192013-10-07 00:02:49 +01001008 *emit_options = EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001009#endif
Damien3ef4abb2013-10-12 16:53:13 +01001010#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001011 } else if (attr == MP_QSTR_asm_thumb) {
Damien5bfb7592013-10-05 18:41:24 +01001012 *emit_options = EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001013#endif
Damien6cdd3af2013-10-05 18:08:26 +01001014 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +00001015 compile_syntax_error(comp, "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001016 }
1017
1018 return true;
1019}
1020
Damiend99b0522013-12-21 18:17:45 +00001021void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001022 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001023 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001024 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1025
Damien6cdd3af2013-10-05 18:08:26 +01001026 // inherit emit options for this function/class definition
1027 uint emit_options = comp->scope_cur->emit_options;
1028
1029 // compile each decorator
1030 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001031 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001032 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1033 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001034
1035 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001036 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001037 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1038
1039 // check for built-in decorators
1040 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1041 // this was a built-in
1042 num_built_in_decorators += 1;
1043
1044 } else {
1045 // not a built-in, compile normally
1046
1047 // compile the decorator function
1048 compile_node(comp, name_nodes[0]);
1049 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001050 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001051 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001052 }
1053
1054 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001055 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001056 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001057 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001058 compile_node(comp, pns_decorator->nodes[1]);
1059 }
Damien429d7192013-10-04 19:53:11 +01001060 }
1061 }
1062
1063 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001064 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001065 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001066 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001067 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001068 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001069 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001070 } else {
1071 // shouldn't happen
1072 assert(0);
1073 }
1074
1075 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001076 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001077 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01001078 }
1079
1080 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001081 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001082}
1083
Damiend99b0522013-12-21 18:17:45 +00001084void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001085 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001086 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001087 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001088}
1089
Damiend99b0522013-12-21 18:17:45 +00001090void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1091 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001092 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001093 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1094 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001095
1096 compile_node(comp, pns->nodes[0]); // base of the power node
1097
Damiend99b0522013-12-21 18:17:45 +00001098 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1099 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1100 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1101 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001102 for (int i = 0; i < n - 1; i++) {
1103 compile_node(comp, pns1->nodes[i]);
1104 }
Damiend99b0522013-12-21 18:17:45 +00001105 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1106 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001107 }
Damiend99b0522013-12-21 18:17:45 +00001108 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien429d7192013-10-04 19:53:11 +01001109 // SyntaxError: can't delete a function call
1110 assert(0);
Damiend99b0522013-12-21 18:17:45 +00001111 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001112 compile_node(comp, pns1->nodes[0]);
1113 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001114 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1115 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001116 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001117 } else {
1118 // shouldn't happen
1119 assert(0);
1120 }
1121 } else {
1122 // shouldn't happen
1123 assert(0);
1124 }
1125
Damiend99b0522013-12-21 18:17:45 +00001126 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001127 // SyntaxError, cannot delete
1128 assert(0);
1129 }
Damiend99b0522013-12-21 18:17:45 +00001130 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1131 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1132 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1133 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001134 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1135
Damiend99b0522013-12-21 18:17:45 +00001136 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1137 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1138 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001139 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001140 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001141 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001142 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001143 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001144 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001145 c_del_stmt(comp, pns->nodes[0]);
1146 for (int i = 0; i < n; i++) {
1147 c_del_stmt(comp, pns1->nodes[i]);
1148 }
Damiend99b0522013-12-21 18:17:45 +00001149 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001150 // TODO not implemented; can't del comprehension?
1151 assert(0);
1152 } else {
1153 // sequence with 2 items
1154 goto sequence_with_2_items;
1155 }
1156 } else {
1157 // sequence with 2 items
1158 sequence_with_2_items:
1159 c_del_stmt(comp, pns->nodes[0]);
1160 c_del_stmt(comp, pns->nodes[1]);
1161 }
1162 } else {
1163 // tuple with 1 element
1164 c_del_stmt(comp, pn);
1165 }
1166 } else {
1167 // not implemented
1168 assert(0);
1169 }
1170}
1171
Damiend99b0522013-12-21 18:17:45 +00001172void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001173 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1174}
1175
Damiend99b0522013-12-21 18:17:45 +00001176void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001177 if (comp->break_label == 0) {
Damien George2326d522014-03-27 23:26:35 +00001178 compile_syntax_error(comp, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001179 }
Damien Georgecbddb272014-02-01 20:08:18 +00001180 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001181}
1182
Damiend99b0522013-12-21 18:17:45 +00001183void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001184 if (comp->continue_label == 0) {
Damien George2326d522014-03-27 23:26:35 +00001185 compile_syntax_error(comp, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001186 }
Damien Georgecbddb272014-02-01 20:08:18 +00001187 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001188}
1189
Damiend99b0522013-12-21 18:17:45 +00001190void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001191 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001192 compile_syntax_error(comp, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001193 return;
1194 }
Damiend99b0522013-12-21 18:17:45 +00001195 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001196 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001197 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001198 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001199 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001200 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1201 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 +01001202
Damienb05d7072013-10-05 13:37:10 +01001203 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001204 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1205 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1206 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001207 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001208 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1209 } else {
1210 compile_node(comp, pns->nodes[0]);
1211 }
1212 EMIT(return_value);
1213}
1214
Damiend99b0522013-12-21 18:17:45 +00001215void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001216 compile_node(comp, pns->nodes[0]);
1217 EMIT(pop_top);
1218}
1219
Damiend99b0522013-12-21 18:17:45 +00001220void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1221 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001222 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001223 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001224 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001225 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001226 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001227 compile_node(comp, pns->nodes[0]);
1228 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001229 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001230 } else {
1231 // raise x
1232 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001233 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001234 }
1235}
1236
1237// q1 holds the base, q2 the full name
1238// eg a -> q1=q2=a
1239// a.b.c -> q1=a, q2=a.b.c
Damiend99b0522013-12-21 18:17:45 +00001240void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q1, qstr *q2) {
Damien429d7192013-10-04 19:53:11 +01001241 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001242 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1243 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001244 // a name of the form x as y; unwrap it
Damiend99b0522013-12-21 18:17:45 +00001245 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001246 pn = pns->nodes[0];
1247 is_as = true;
1248 }
Damiend99b0522013-12-21 18:17:45 +00001249 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001250 // just a simple name
Damiend99b0522013-12-21 18:17:45 +00001251 *q2 = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001252 if (!is_as) {
1253 *q1 = *q2;
1254 }
Damien Georgeb9791222014-01-23 00:34:21 +00001255 EMIT_ARG(import_name, *q2);
Damiend99b0522013-12-21 18:17:45 +00001256 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1257 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1258 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001259 // a name of the form a.b.c
1260 if (!is_as) {
Damiend99b0522013-12-21 18:17:45 +00001261 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001262 }
Damiend99b0522013-12-21 18:17:45 +00001263 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001264 int len = n - 1;
1265 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001266 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001267 }
Damien George55baff42014-01-21 21:40:13 +00001268 byte *q_ptr;
1269 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001270 for (int i = 0; i < n; i++) {
1271 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001272 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001273 }
Damien George55baff42014-01-21 21:40:13 +00001274 uint str_src_len;
1275 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001276 memcpy(str_dest, str_src, str_src_len);
1277 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001278 }
Damien George55baff42014-01-21 21:40:13 +00001279 *q2 = qstr_build_end(q_ptr);
Damien Georgeb9791222014-01-23 00:34:21 +00001280 EMIT_ARG(import_name, *q2);
Damien429d7192013-10-04 19:53:11 +01001281 if (is_as) {
1282 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001283 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001284 }
1285 }
1286 } else {
1287 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001288 // This covers relative imports starting with dot(s) like "from .foo 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 } else {
1293 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001294 // This covers relative imports with dots only like "from .. import"
Paul Sokolovsky8d9cc2e2014-03-30 02:31:08 +02001295 compile_syntax_error(comp, "Relative imports not implemented");
Damien429d7192013-10-04 19:53:11 +01001296 assert(0);
1297 }
1298}
1299
Damiend99b0522013-12-21 18:17:45 +00001300void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgeb9791222014-01-23 00:34:21 +00001301 EMIT_ARG(load_const_small_int, 0); // ??
1302 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001303 qstr q1, q2;
1304 do_import_name(comp, pn, &q1, &q2);
Damien Georgeb9791222014-01-23 00:34:21 +00001305 EMIT_ARG(store_id, q1);
Damien429d7192013-10-04 19:53:11 +01001306}
1307
Damiend99b0522013-12-21 18:17:45 +00001308void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001309 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1310}
1311
Damiend99b0522013-12-21 18:17:45 +00001312void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
1313 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001314 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001315
1316 // build the "fromlist" tuple
1317#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001318 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001319#else
Damien Georgeb9791222014-01-23 00:34:21 +00001320 EMIT_ARG(load_const_str, QSTR_FROM_STR_STATIC("*"), false);
1321 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001322#endif
1323
1324 // do the import
Damien429d7192013-10-04 19:53:11 +01001325 qstr dummy_q, id1;
1326 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1327 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001328
Damien429d7192013-10-04 19:53:11 +01001329 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001330 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001331
1332 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001333 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001334 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001335#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001336 {
1337 vstr_t *vstr = vstr_new();
1338 vstr_printf(vstr, "(");
1339 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001340 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1341 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1342 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001343 if (i > 0) {
1344 vstr_printf(vstr, ", ");
1345 }
1346 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001347 uint len;
1348 const byte *str = qstr_data(id2, &len);
1349 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001350 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001351 }
Damien02f89412013-12-12 15:13:36 +00001352 if (n == 1) {
1353 vstr_printf(vstr, ",");
1354 }
1355 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001356 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001357 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001358 }
Damiendb4c3612013-12-10 17:27:24 +00001359#else
1360 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001361 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1362 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1363 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001364 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001365 }
Damien Georgeb9791222014-01-23 00:34:21 +00001366 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001367#endif
1368
1369 // do the import
Damien429d7192013-10-04 19:53:11 +01001370 qstr dummy_q, id1;
1371 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1372 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001373 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1374 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1375 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001376 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001377 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001378 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001379 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001380 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001381 }
1382 }
1383 EMIT(pop_top);
1384 }
1385}
1386
Damiend99b0522013-12-21 18:17:45 +00001387void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001388 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001389 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1390 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001391 } else {
Damiend99b0522013-12-21 18:17:45 +00001392 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1393 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001394 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001395 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001396 }
Damien429d7192013-10-04 19:53:11 +01001397 }
1398 }
1399}
1400
Damiend99b0522013-12-21 18:17:45 +00001401void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001402 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001403 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1404 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001405 } else {
Damiend99b0522013-12-21 18:17:45 +00001406 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1407 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001408 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001409 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001410 }
Damien429d7192013-10-04 19:53:11 +01001411 }
1412 }
1413}
1414
Damiend99b0522013-12-21 18:17:45 +00001415void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001416 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001417 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001418 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 +00001419 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001420 // assertion message
1421 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001422 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01001423 }
Damien Georgeb9791222014-01-23 00:34:21 +00001424 EMIT_ARG(raise_varargs, 1);
1425 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001426}
1427
Damiend99b0522013-12-21 18:17:45 +00001428void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001429 // TODO proper and/or short circuiting
1430
Damienb05d7072013-10-05 13:37:10 +01001431 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001432
Damienb05d7072013-10-05 13:37:10 +01001433 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001434 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1435
1436 compile_node(comp, pns->nodes[1]); // if block
Damiend99b0522013-12-21 18:17:45 +00001437 //if (!(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3]))) { // optimisation; doesn't align with CPython
Damien429d7192013-10-04 19:53:11 +01001438 // jump over elif/else blocks if they exist
Damien415eb6f2013-10-05 12:19:06 +01001439 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001440 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001441 }
1442 //}
Damien Georgeb9791222014-01-23 00:34:21 +00001443 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001444
Damiend99b0522013-12-21 18:17:45 +00001445 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001446 // compile elif blocks
1447
Damiend99b0522013-12-21 18:17:45 +00001448 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001449
Damiend99b0522013-12-21 18:17:45 +00001450 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001451 // multiple elif blocks
1452
Damiend99b0522013-12-21 18:17:45 +00001453 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001454 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001455 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001456 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001457 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1458
1459 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001460 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001461 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001462 }
Damien Georgeb9791222014-01-23 00:34:21 +00001463 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001464 }
1465
1466 } else {
1467 // a single elif block
1468
Damienb05d7072013-10-05 13:37:10 +01001469 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001470 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1471
1472 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001473 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001474 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001475 }
Damien Georgeb9791222014-01-23 00:34:21 +00001476 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001477 }
1478 }
1479
1480 // compile else block
1481 compile_node(comp, pns->nodes[3]); // can be null
1482
Damien Georgeb9791222014-01-23 00:34:21 +00001483 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001484}
1485
Damien Georgecbddb272014-02-01 20:08:18 +00001486#define START_BREAK_CONTINUE_BLOCK \
1487 int old_break_label = comp->break_label; \
1488 int old_continue_label = comp->continue_label; \
1489 int break_label = comp_next_label(comp); \
1490 int continue_label = comp_next_label(comp); \
1491 comp->break_label = break_label; \
1492 comp->continue_label = continue_label; \
1493 comp->break_continue_except_level = comp->cur_except_level;
1494
1495#define END_BREAK_CONTINUE_BLOCK \
1496 comp->break_label = old_break_label; \
1497 comp->continue_label = old_continue_label; \
1498 comp->break_continue_except_level = comp->cur_except_level;
1499
Damiend99b0522013-12-21 18:17:45 +00001500void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001501 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001502
Damience89a212013-10-15 22:25:17 +01001503 // compared to CPython, we have an optimised version of while loops
1504#if MICROPY_EMIT_CPYTHON
1505 int done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001506 EMIT_ARG(setup_loop, break_label);
1507 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001508 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1509 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001510 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001511 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001512 }
Damien Georgeb9791222014-01-23 00:34:21 +00001513 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001514 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1515 // this is a small hack to agree with CPython
1516 if (!node_is_const_true(pns->nodes[0])) {
1517 EMIT(pop_block);
1518 }
Damience89a212013-10-15 22:25:17 +01001519#else
1520 int top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001521 EMIT_ARG(jump, continue_label);
1522 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001523 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001524 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001525 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1526#endif
1527
1528 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001529 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001530
1531 compile_node(comp, pns->nodes[2]); // else
1532
Damien Georgeb9791222014-01-23 00:34:21 +00001533 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001534}
1535
Damienf72fd0e2013-11-06 20:20:49 +00001536// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001537// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1538// And, if the loop never runs, the loop variable should never be assigned
Damiend99b0522013-12-21 18:17:45 +00001539void 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 +00001540 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001541
1542 int top_label = comp_next_label(comp);
Damien George600ae732014-01-21 23:48:04 +00001543 int entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001544
Damien George3ff2d032014-03-31 18:02:22 +01001545 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001546 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001547 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001548
Damien Georgeb9791222014-01-23 00:34:21 +00001549 EMIT_ARG(jump, entry_label);
1550 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001551
Damien George3ff2d032014-03-31 18:02:22 +01001552 // at this point we actually have 1 less element on the stack
1553 EMIT_ARG(set_stack_size, EMIT(get_stack_size) - 1);
1554
1555 // store next value to var
1556 c_assign(comp, pn_var, ASSIGN_STORE);
1557
Damienf3822fc2013-11-09 20:12:03 +00001558 // compile body
1559 compile_node(comp, pn_body);
1560
Damien Georgeb9791222014-01-23 00:34:21 +00001561 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001562
Damien George3ff2d032014-03-31 18:02:22 +01001563 // compile: var + step, duplicated on stack
1564 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001565 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001566 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001567 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001568
Damien Georgeb9791222014-01-23 00:34:21 +00001569 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001570
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001571 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001572 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001573 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1574 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001575 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001576 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001577 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001578 }
Damien Georgeb9791222014-01-23 00:34:21 +00001579 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001580
Damien George3ff2d032014-03-31 18:02:22 +01001581 // discard final value of var that failed the loop condition
1582 EMIT(pop_top);
1583
Damienf72fd0e2013-11-06 20:20:49 +00001584 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001585 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001586
1587 compile_node(comp, pn_else);
1588
Damien Georgeb9791222014-01-23 00:34:21 +00001589 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001590}
1591
Damiend99b0522013-12-21 18:17:45 +00001592void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001593#if !MICROPY_EMIT_CPYTHON
1594 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1595 // this is actually slower, but uses no heap memory
1596 // for viper it will be much, much faster
Damiend99b0522013-12-21 18:17:45 +00001597 if (/*comp->scope_cur->emit_options == EMIT_OPT_VIPER &&*/ MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_power)) {
1598 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001599 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1600 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1601 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1602 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001603 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1604 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001605 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001606 mp_parse_node_t pn_range_start;
1607 mp_parse_node_t pn_range_end;
1608 mp_parse_node_t pn_range_step;
1609 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001610 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001611 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001612 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001613 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001614 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001615 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001616 } else if (n_args == 2) {
1617 pn_range_start = args[0];
1618 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001619 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001620 } else {
1621 pn_range_start = args[0];
1622 pn_range_end = args[1];
1623 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001624 // We need to know sign of step. This is possible only if it's constant
1625 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1626 optimize = false;
1627 }
Damienf72fd0e2013-11-06 20:20:49 +00001628 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001629 }
1630 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001631 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1632 return;
1633 }
1634 }
1635 }
1636#endif
1637
Damien Georgecbddb272014-02-01 20:08:18 +00001638 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001639
Damienb05d7072013-10-05 13:37:10 +01001640 int pop_label = comp_next_label(comp);
1641 int end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001642
Damience89a212013-10-15 22:25:17 +01001643 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1644#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001645 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001646#endif
1647
Damien429d7192013-10-04 19:53:11 +01001648 compile_node(comp, pns->nodes[1]); // iterator
1649 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001650 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001651 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001652 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1653 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001654 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001655 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001656 }
Damien Georgeb9791222014-01-23 00:34:21 +00001657 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001658 EMIT(for_iter_end);
1659
1660 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001661 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001662
Damience89a212013-10-15 22:25:17 +01001663#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001664 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001665#endif
Damien429d7192013-10-04 19:53:11 +01001666
1667 compile_node(comp, pns->nodes[3]); // else (not tested)
1668
Damien Georgeb9791222014-01-23 00:34:21 +00001669 EMIT_ARG(label_assign, break_label);
1670 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001671}
1672
Damiend99b0522013-12-21 18:17:45 +00001673void 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 +01001674 // this function is a bit of a hack at the moment
1675 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1676
1677 // setup code
1678 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001679 int l1 = comp_next_label(comp);
1680 int success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001681
Damien Georgeb9791222014-01-23 00:34:21 +00001682 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001683 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001684
Damien429d7192013-10-04 19:53:11 +01001685 compile_node(comp, pn_body); // body
1686 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001687 EMIT_ARG(jump, success_label);
1688 EMIT_ARG(label_assign, l1);
Damienb05d7072013-10-05 13:37:10 +01001689 int l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001690
1691 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001692 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1693 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001694
1695 qstr qstr_exception_local = 0;
Damienb05d7072013-10-05 13:37:10 +01001696 int end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001697
Damiend99b0522013-12-21 18:17:45 +00001698 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001699 // this is a catch all exception handler
1700 if (i + 1 != n_except) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001701 compile_syntax_error(comp, "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001702 return;
1703 }
1704 } else {
1705 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001706 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1707 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1708 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1709 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001710 // handler binds the exception to a local
1711 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001712 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001713 }
1714 }
1715 EMIT(dup_top);
1716 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001717 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001718 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001719 }
1720
1721 EMIT(pop_top);
1722
1723 if (qstr_exception_local == 0) {
1724 EMIT(pop_top);
1725 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001726 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001727 }
1728
1729 EMIT(pop_top);
1730
Damiene2880aa2013-12-20 14:22:59 +00001731 int l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001732 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001733 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001734 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001735 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001736 }
1737 compile_node(comp, pns_except->nodes[1]);
1738 if (qstr_exception_local != 0) {
1739 EMIT(pop_block);
1740 }
1741 EMIT(pop_except);
1742 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001743 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1744 EMIT_ARG(label_assign, l3);
1745 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1746 EMIT_ARG(store_id, qstr_exception_local);
1747 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001748
Damien George8dcc0c72014-03-27 10:55:21 +00001749 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001750 EMIT(end_finally);
1751 }
Damien Georgeb9791222014-01-23 00:34:21 +00001752 EMIT_ARG(jump, l2);
1753 EMIT_ARG(label_assign, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001754 }
1755
Damien George8dcc0c72014-03-27 10:55:21 +00001756 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001757 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001758
Damien Georgeb9791222014-01-23 00:34:21 +00001759 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001760 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001761 EMIT_ARG(label_assign, l2);
1762 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001763}
1764
Damiend99b0522013-12-21 18:17:45 +00001765void 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 +01001766 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1767 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001768 int l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001769
Damien Georgeb9791222014-01-23 00:34:21 +00001770 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001771 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001772
Damien429d7192013-10-04 19:53:11 +01001773 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001774 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien429d7192013-10-04 19:53:11 +01001775 compile_node(comp, pn_body);
1776 } else {
1777 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1778 }
1779 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001780 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1781 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001782 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001783
Damien George8dcc0c72014-03-27 10:55:21 +00001784 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001785 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001786
Damien Georgeb9791222014-01-23 00:34:21 +00001787 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001788}
1789
Damiend99b0522013-12-21 18:17:45 +00001790void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1791 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1792 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1793 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001794 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001795 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1796 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001797 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001798 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001799 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001800 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001801 // no finally
1802 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1803 } else {
1804 // have finally
Damiend99b0522013-12-21 18:17:45 +00001805 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 +01001806 }
1807 } else {
1808 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001809 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001810 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001811 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001812 }
1813 } else {
1814 // shouldn't happen
1815 assert(0);
1816 }
1817}
1818
Damiend99b0522013-12-21 18:17:45 +00001819void 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 +01001820 if (n == 0) {
1821 // no more pre-bits, compile the body of the with
1822 compile_node(comp, body);
1823 } else {
Damienb05d7072013-10-05 13:37:10 +01001824 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001825 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001826 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001827 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001828 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001829 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001830 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1831 } else {
1832 // this pre-bit is just an expression
1833 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001834 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001835 EMIT(pop_top);
1836 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001837 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001838 // compile additional pre-bits and the body
1839 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1840 // finish this with block
1841 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001842 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1843 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001844 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001845 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001846 EMIT(end_finally);
1847 }
1848}
1849
Damiend99b0522013-12-21 18:17:45 +00001850void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001851 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001852 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001853 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1854 assert(n > 0);
1855
1856 // compile in a nested fashion
1857 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1858}
1859
Damiend99b0522013-12-21 18:17:45 +00001860void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1861 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001862 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1863 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001864 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001865 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001866 EMIT_ARG(call_function, 1, 0, false, false);
Damien5ac1b2e2013-10-18 19:58:12 +01001867 EMIT(pop_top);
1868
Damien429d7192013-10-04 19:53:11 +01001869 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001870 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001871 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001872 // do nothing with a lonely constant
1873 } else {
1874 compile_node(comp, pns->nodes[0]); // just an expression
1875 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1876 }
Damien429d7192013-10-04 19:53:11 +01001877 }
1878 } else {
Damiend99b0522013-12-21 18:17:45 +00001879 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1880 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001881 if (kind == PN_expr_stmt_augassign) {
1882 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1883 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001884 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001885 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001886 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001887 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1888 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1889 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1890 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1891 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1892 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1893 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1894 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1895 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1896 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1897 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
1898 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
1899 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01001900 }
Damien George7e5fb242014-02-01 22:18:47 +00001901 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001902 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1903 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001904 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1905 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001906 // following CPython, we store left-most first
1907 if (rhs > 0) {
1908 EMIT(dup_top);
1909 }
1910 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1911 for (int i = 0; i < rhs; i++) {
1912 if (i + 1 < rhs) {
1913 EMIT(dup_top);
1914 }
Damiend99b0522013-12-21 18:17:45 +00001915 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001916 }
1917 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001918 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1919 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1920 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1921 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01001922 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001923 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1924 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001925 compile_node(comp, pns10->nodes[0]); // rhs
1926 compile_node(comp, pns10->nodes[1]); // rhs
1927 EMIT(rot_two);
1928 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1929 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00001930 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1931 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1932 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1933 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01001934 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001935 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1936 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001937 compile_node(comp, pns10->nodes[0]); // rhs
1938 compile_node(comp, pns10->nodes[1]); // rhs
1939 compile_node(comp, pns10->nodes[2]); // rhs
1940 EMIT(rot_three);
1941 EMIT(rot_two);
1942 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1943 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1944 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1945 } else {
1946 compile_node(comp, pns1->nodes[0]); // rhs
1947 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1948 }
1949 } else {
1950 // shouldn't happen
1951 assert(0);
1952 }
1953 }
1954}
1955
Damien Georged17926d2014-03-30 13:35:08 +01001956void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00001957 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001958 compile_node(comp, pns->nodes[0]);
1959 for (int i = 1; i < num_nodes; i += 1) {
1960 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001961 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001962 }
1963}
1964
Damiend99b0522013-12-21 18:17:45 +00001965void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1966 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1967 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001968
1969 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001970 int l_fail = comp_next_label(comp);
1971 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001972 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1973 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001974 EMIT_ARG(jump, l_end);
1975 EMIT_ARG(label_assign, l_fail);
1976 EMIT_ARG(set_stack_size, stack_size); // force stack size reset
Damien429d7192013-10-04 19:53:11 +01001977 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001978 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001979}
1980
Damiend99b0522013-12-21 18:17:45 +00001981void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001982 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00001983 //mp_parse_node_t pn_params = pns->nodes[0];
1984 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001985
1986 if (comp->pass == PASS_1) {
1987 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00001988 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 +01001989 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001990 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001991 }
1992
1993 // get the scope for this lambda
1994 scope_t *this_scope = (scope_t*)pns->nodes[2];
1995
1996 // make the lambda
1997 close_over_variables_etc(comp, this_scope, 0, 0);
1998}
1999
Damiend99b0522013-12-21 18:17:45 +00002000void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01002001 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002002 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002003 for (int i = 0; i < n; i += 1) {
2004 compile_node(comp, pns->nodes[i]);
2005 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002006 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002007 }
2008 }
Damien Georgeb9791222014-01-23 00:34:21 +00002009 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002010}
2011
Damiend99b0522013-12-21 18:17:45 +00002012void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01002013 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002014 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002015 for (int i = 0; i < n; i += 1) {
2016 compile_node(comp, pns->nodes[i]);
2017 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002018 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002019 }
2020 }
Damien Georgeb9791222014-01-23 00:34:21 +00002021 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002022}
2023
Damiend99b0522013-12-21 18:17:45 +00002024void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002025 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002026 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002027}
2028
Damiend99b0522013-12-21 18:17:45 +00002029void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002030 int stack_size = EMIT(get_stack_size);
Damiend99b0522013-12-21 18:17:45 +00002031 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002032 compile_node(comp, pns->nodes[0]);
2033 bool multi = (num_nodes > 3);
2034 int l_fail = 0;
2035 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002036 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002037 }
2038 for (int i = 1; i + 1 < num_nodes; i += 2) {
2039 compile_node(comp, pns->nodes[i + 1]);
2040 if (i + 2 < num_nodes) {
2041 EMIT(dup_top);
2042 EMIT(rot_three);
2043 }
Damien George7e5fb242014-02-01 22:18:47 +00002044 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002045 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002046 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002047 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2048 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2049 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2050 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2051 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2052 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2053 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2054 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002055 }
2056 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002057 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2058 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2059 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002060 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002061 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002062 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002063 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002064 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002065 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002066 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002067 }
2068 } else {
2069 // shouldn't happen
2070 assert(0);
2071 }
2072 } else {
2073 // shouldn't happen
2074 assert(0);
2075 }
2076 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002077 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002078 }
2079 }
2080 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002081 int l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002082 EMIT_ARG(jump, l_end);
2083 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01002084 EMIT(rot_two);
2085 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002086 EMIT_ARG(label_assign, l_end);
2087 EMIT_ARG(set_stack_size, stack_size + 1); // force stack size
Damien429d7192013-10-04 19:53:11 +01002088 }
2089}
2090
Damiend99b0522013-12-21 18:17:45 +00002091void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002092 // TODO
2093 assert(0);
2094 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002095 //EMIT_ARG(unary_op, "UNARY_STAR");
Damien429d7192013-10-04 19:53:11 +01002096}
2097
Damiend99b0522013-12-21 18:17:45 +00002098void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002099 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002100}
2101
Damiend99b0522013-12-21 18:17:45 +00002102void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002103 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002104}
2105
Damiend99b0522013-12-21 18:17:45 +00002106void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002107 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002108}
2109
Damiend99b0522013-12-21 18:17:45 +00002110void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2111 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002112 compile_node(comp, pns->nodes[0]);
2113 for (int i = 1; i + 1 < num_nodes; i += 2) {
2114 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002115 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002116 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002117 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002118 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002119 } else {
2120 // shouldn't happen
2121 assert(0);
2122 }
2123 }
2124}
2125
Damiend99b0522013-12-21 18:17:45 +00002126void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2127 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002128 compile_node(comp, pns->nodes[0]);
2129 for (int i = 1; i + 1 < num_nodes; i += 2) {
2130 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002131 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002132 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002133 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002134 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002135 } else {
2136 // shouldn't happen
2137 assert(0);
2138 }
2139 }
2140}
2141
Damiend99b0522013-12-21 18:17:45 +00002142void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2143 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002144 compile_node(comp, pns->nodes[0]);
2145 for (int i = 1; i + 1 < num_nodes; i += 2) {
2146 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002147 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002148 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002149 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002150 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002151 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002152 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002153 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002154 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002155 } else {
2156 // shouldn't happen
2157 assert(0);
2158 }
2159 }
2160}
2161
Damiend99b0522013-12-21 18:17:45 +00002162void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002163 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002164 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002165 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002166 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002167 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002168 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002169 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002170 } else {
2171 // shouldn't happen
2172 assert(0);
2173 }
2174}
2175
Damien George35e2a4e2014-02-05 00:51:47 +00002176void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2177 // this is to handle special super() call
2178 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2179
2180 compile_generic_all_nodes(comp, pns);
2181}
2182
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002183STATIC 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 +01002184 // function to call is on top of stack
2185
Damien George35e2a4e2014-02-05 00:51:47 +00002186#if !MICROPY_EMIT_CPYTHON
2187 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002188 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 +00002189 EMIT_ARG(load_id, MP_QSTR___class__);
2190 // get first argument to function
2191 bool found = false;
2192 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
2193 if (comp->scope_cur->id_info[i].param) {
2194 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
2195 found = true;
2196 break;
2197 }
2198 }
2199 if (!found) {
2200 printf("TypeError: super() call cannot find self\n");
2201 return;
2202 }
2203 EMIT_ARG(call_function, 2, 0, false, false);
2204 return;
2205 }
2206#endif
2207
Damien429d7192013-10-04 19:53:11 +01002208 int old_n_arg_keyword = comp->n_arg_keyword;
2209 bool old_have_star_arg = comp->have_star_arg;
2210 bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
2211 comp->n_arg_keyword = 0;
2212 comp->have_star_arg = false;
2213 comp->have_dbl_star_arg = false;
2214
Damien Georgebbcd49a2014-02-06 20:30:16 +00002215 compile_node(comp, pn_arglist); // arguments to function call; can be null
Damien429d7192013-10-04 19:53:11 +01002216
2217 // compute number of positional arguments
Damien Georgebbcd49a2014-02-06 20:30:16 +00002218 int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
Damien429d7192013-10-04 19:53:11 +01002219 if (comp->have_star_arg) {
2220 n_positional -= 1;
2221 }
2222 if (comp->have_dbl_star_arg) {
2223 n_positional -= 1;
2224 }
2225
2226 if (is_method_call) {
Damien Georgeb9791222014-01-23 00:34:21 +00002227 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 +01002228 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002229 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 +01002230 }
2231
2232 comp->n_arg_keyword = old_n_arg_keyword;
2233 comp->have_star_arg = old_have_star_arg;
2234 comp->have_dbl_star_arg = old_have_dbl_star_arg;
2235}
2236
Damiend99b0522013-12-21 18:17:45 +00002237void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2238 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002239 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002240 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 +01002241 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002242 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2243 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002244 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002245 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002246 i += 1;
2247 } else {
2248 compile_node(comp, pns->nodes[i]);
2249 }
Damien George35e2a4e2014-02-05 00:51:47 +00002250 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002251 }
2252}
2253
Damiend99b0522013-12-21 18:17:45 +00002254void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002255 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002256 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002257}
2258
Damiend99b0522013-12-21 18:17:45 +00002259void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002260 // a list of strings
Damien63321742013-12-10 17:41:49 +00002261
2262 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002263 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002264 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002265 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002266 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002267 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2268 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2269 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002270 if (i == 0) {
2271 string_kind = pn_kind;
2272 } else if (pn_kind != string_kind) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002273 compile_syntax_error(comp, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002274 return;
2275 }
Damien George55baff42014-01-21 21:40:13 +00002276 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002277 }
Damien63321742013-12-10 17:41:49 +00002278
Damien63321742013-12-10 17:41:49 +00002279 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002280 byte *q_ptr;
2281 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002282 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002283 uint s_len;
2284 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002285 memcpy(s_dest, s, s_len);
2286 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002287 }
Damien George55baff42014-01-21 21:40:13 +00002288 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002289
Damien Georgeb9791222014-01-23 00:34:21 +00002290 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002291}
2292
2293// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002294void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2295 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2296 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2297 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002298
2299 if (comp->pass == PASS_1) {
2300 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002301 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 +01002302 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002303 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002304 }
2305
2306 // get the scope for this comprehension
2307 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2308
2309 // compile the comprehension
2310 close_over_variables_etc(comp, this_scope, 0, 0);
2311
2312 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2313 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002314 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01002315}
2316
Damiend99b0522013-12-21 18:17:45 +00002317void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2318 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002319 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002320 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2321 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2322 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2323 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2324 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2325 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2326 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002327 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002328 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002329 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002330 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002331 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002332 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002333 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002334 // generator expression
2335 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2336 } else {
2337 // tuple with 2 items
2338 goto tuple_with_2_items;
2339 }
2340 } else {
2341 // tuple with 2 items
2342 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002343 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002344 }
2345 } else {
2346 // parenthesis around a single item, is just that item
2347 compile_node(comp, pns->nodes[0]);
2348 }
2349}
2350
Damiend99b0522013-12-21 18:17:45 +00002351void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2352 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002353 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002354 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002355 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2356 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2357 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2358 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2359 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002360 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002361 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002362 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002363 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002364 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002365 // list of many items
2366 compile_node(comp, pns2->nodes[0]);
2367 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002368 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002369 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002370 // list comprehension
2371 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2372 } else {
2373 // list with 2 items
2374 goto list_with_2_items;
2375 }
2376 } else {
2377 // list with 2 items
2378 list_with_2_items:
2379 compile_node(comp, pns2->nodes[0]);
2380 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002381 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002382 }
2383 } else {
2384 // list with 1 item
2385 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002386 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002387 }
2388}
2389
Damiend99b0522013-12-21 18:17:45 +00002390void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2391 mp_parse_node_t pn = pns->nodes[0];
2392 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002393 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002394 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002395 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2396 pns = (mp_parse_node_struct_t*)pn;
2397 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002398 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002399 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002400 compile_node(comp, pn);
2401 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002402 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2403 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2404 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2405 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002406 // dict/set with multiple elements
2407
2408 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002409 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002410 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2411
2412 // first element sets whether it's a dict or set
2413 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002414 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002415 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002416 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002417 compile_node(comp, pns->nodes[0]);
2418 EMIT(store_map);
2419 is_dict = true;
2420 } else {
2421 // a set
2422 compile_node(comp, pns->nodes[0]); // 1st value of set
2423 is_dict = false;
2424 }
2425
2426 // process rest of elements
2427 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002428 mp_parse_node_t pn = nodes[i];
2429 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002430 compile_node(comp, pn);
2431 if (is_dict) {
2432 if (!is_key_value) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002433 compile_syntax_error(comp, "?expecting key:value for dictiona");
Damien429d7192013-10-04 19:53:11 +01002434 return;
2435 }
2436 EMIT(store_map);
2437 } else {
2438 if (is_key_value) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002439 compile_syntax_error(comp, "?expecting just a value for s");
Damien429d7192013-10-04 19:53:11 +01002440 return;
2441 }
2442 }
2443 }
2444
2445 // if it's a set, build it
2446 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002447 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002448 }
Damiend99b0522013-12-21 18:17:45 +00002449 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002450 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002451 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002452 // a dictionary comprehension
2453 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2454 } else {
2455 // a set comprehension
2456 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2457 }
2458 } else {
2459 // shouldn't happen
2460 assert(0);
2461 }
2462 } else {
2463 // set with one element
2464 goto set_with_one_element;
2465 }
2466 } else {
2467 // set with one element
2468 set_with_one_element:
2469 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002470 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002471 }
2472}
2473
Damiend99b0522013-12-21 18:17:45 +00002474void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002475 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002476}
2477
Damiend99b0522013-12-21 18:17:45 +00002478void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002479 // object who's index we want is on top of stack
2480 compile_node(comp, pns->nodes[0]); // the index
Damien Georged17926d2014-03-30 13:35:08 +01002481 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002482}
2483
Damiend99b0522013-12-21 18:17:45 +00002484void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002485 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002486 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002487}
2488
Damiend99b0522013-12-21 18:17:45 +00002489void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2490 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2491 mp_parse_node_t pn = pns->nodes[0];
2492 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002493 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002494 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2495 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002496 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2497 pns = (mp_parse_node_struct_t*)pn;
2498 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002499 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002500 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002501 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002502 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002503 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002504 } else {
2505 // [?::x]
2506 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002507 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002508 }
Damiend99b0522013-12-21 18:17:45 +00002509 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002510 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002511 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2512 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2513 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2514 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002515 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002516 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002517 } else {
2518 // [?:x:x]
2519 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002520 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002521 }
2522 } else {
2523 // [?:x]
2524 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002525 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002526 }
2527 } else {
2528 // [?:x]
2529 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002530 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002531 }
2532}
2533
Damiend99b0522013-12-21 18:17:45 +00002534void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002535 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002536 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2537 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002538}
2539
Damiend99b0522013-12-21 18:17:45 +00002540void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002541 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002542 compile_subscript_3_helper(comp, pns);
2543}
2544
Damiend99b0522013-12-21 18:17:45 +00002545void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002546 // if this is called then we are compiling a dict key:value pair
2547 compile_node(comp, pns->nodes[1]); // value
2548 compile_node(comp, pns->nodes[0]); // key
2549}
2550
Damiend99b0522013-12-21 18:17:45 +00002551void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002552 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002553 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002554 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002555}
2556
Damiend99b0522013-12-21 18:17:45 +00002557void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002558 if (comp->have_star_arg) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002559 compile_syntax_error(comp, "?can't have multiple *x");
Damien429d7192013-10-04 19:53:11 +01002560 return;
2561 }
2562 comp->have_star_arg = true;
2563 compile_node(comp, pns->nodes[0]);
2564}
2565
Damiend99b0522013-12-21 18:17:45 +00002566void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002567 if (comp->have_dbl_star_arg) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002568 compile_syntax_error(comp, "?can't have multiple **x");
Damien429d7192013-10-04 19:53:11 +01002569 return;
2570 }
2571 comp->have_dbl_star_arg = true;
2572 compile_node(comp, pns->nodes[0]);
2573}
2574
Damiend99b0522013-12-21 18:17:45 +00002575void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2576 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2577 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2578 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2579 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002580 compile_syntax_error(comp, "?lhs of keyword argument must be an id");
Damien429d7192013-10-04 19:53:11 +01002581 return;
2582 }
Damien Georgeb9791222014-01-23 00:34:21 +00002583 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002584 compile_node(comp, pns2->nodes[0]);
2585 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002586 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002587 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2588 } else {
2589 // shouldn't happen
2590 assert(0);
2591 }
2592}
2593
Damiend99b0522013-12-21 18:17:45 +00002594void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002595 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002596 compile_syntax_error(comp, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002597 return;
2598 }
Damiend99b0522013-12-21 18:17:45 +00002599 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002600 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002601 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002602 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2603 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002604 compile_node(comp, pns->nodes[0]);
2605 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002606 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002607 EMIT(yield_from);
2608 } else {
2609 compile_node(comp, pns->nodes[0]);
2610 EMIT(yield_value);
2611 }
2612}
2613
Damiend99b0522013-12-21 18:17:45 +00002614typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002615STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002616 NULL,
2617#define nc NULL
2618#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002619#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002620#include "grammar.h"
2621#undef nc
2622#undef c
2623#undef DEF_RULE
2624};
2625
Damiend99b0522013-12-21 18:17:45 +00002626void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2627 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002628 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002629 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2630 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2631 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002632 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002633 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002634 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002635 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002636 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2637 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2638 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2639 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002640 case MP_PARSE_NODE_TOKEN:
2641 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002642 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002643 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002644 // do nothing
2645 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002646 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002647 }
2648 break;
Damien429d7192013-10-04 19:53:11 +01002649 default: assert(0);
2650 }
2651 } else {
Damiend99b0522013-12-21 18:17:45 +00002652 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002653 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002654 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002655 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002656 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002657#if MICROPY_DEBUG_PRINTERS
2658 mp_parse_node_print(pn, 0);
2659#endif
Damien429d7192013-10-04 19:53:11 +01002660 assert(0);
2661 } else {
2662 f(comp, pns);
2663 }
2664 }
2665}
2666
Damiend99b0522013-12-21 18:17:45 +00002667void 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 +01002668 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002669 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002670 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2671 if (MP_PARSE_NODE_IS_ID(pn)) {
2672 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002673 if (comp->have_bare_star) {
2674 // comes after a bare star, so doesn't count as a parameter
2675 } else {
2676 comp->scope_cur->num_params += 1;
2677 }
Damienb14de212013-10-06 00:28:28 +01002678 } else {
Damiend99b0522013-12-21 18:17:45 +00002679 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2680 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2681 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2682 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002683 //int node_index = 1; unused
2684 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002685 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002686 // this parameter has an annotation
2687 pn_annotation = pns->nodes[1];
2688 }
2689 //node_index = 2; unused
2690 }
2691 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002692 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002693 // this parameter has a default value
2694 if (comp->have_bare_star) {
2695 comp->scope_cur->num_dict_params += 1;
2696 } else {
2697 comp->scope_cur->num_default_params += 1;
2698 }
2699 }
2700 */
2701 if (comp->have_bare_star) {
2702 // comes after a bare star, so doesn't count as a parameter
2703 } else {
2704 comp->scope_cur->num_params += 1;
2705 }
Damiend99b0522013-12-21 18:17:45 +00002706 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2707 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002708 // bare star
2709 // TODO see http://www.python.org/dev/peps/pep-3102/
2710 comp->have_bare_star = true;
2711 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002712 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002713 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002714 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002715 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2716 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002717 // named star with annotation
Damien George8725f8f2014-02-15 19:33:11 +00002718 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002719 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2720 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002721 pn_annotation = pns->nodes[1];
2722 } else {
2723 // shouldn't happen
2724 assert(0);
2725 }
Damiend99b0522013-12-21 18:17:45 +00002726 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2727 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2728 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002729 // this parameter has an annotation
2730 pn_annotation = pns->nodes[1];
2731 }
Damien George8725f8f2014-02-15 19:33:11 +00002732 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002733 } else {
Damienb14de212013-10-06 00:28:28 +01002734 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002735 assert(0);
2736 }
Damien429d7192013-10-04 19:53:11 +01002737 }
2738
2739 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002740 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002741 // TODO this parameter has an annotation
2742 }
2743 bool added;
2744 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2745 if (!added) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002746 compile_syntax_error(comp, "?same name used for parameter");
Damien429d7192013-10-04 19:53:11 +01002747 return;
2748 }
2749 id_info->param = true;
2750 id_info->kind = ID_INFO_KIND_LOCAL;
2751 }
2752}
2753
Damiend99b0522013-12-21 18:17:45 +00002754void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002755 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2756}
2757
Damiend99b0522013-12-21 18:17:45 +00002758void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002759 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2760}
2761
Damiend99b0522013-12-21 18:17:45 +00002762void 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 +01002763 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002764 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002765 // no more nested if/for; compile inner expression
2766 compile_node(comp, pn_inner_expr);
2767 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002768 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002769 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002770 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002771 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002772 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002773 } else {
2774 EMIT(yield_value);
2775 EMIT(pop_top);
2776 }
Damiend99b0522013-12-21 18:17:45 +00002777 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002778 // if condition
Damiend99b0522013-12-21 18:17:45 +00002779 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002780 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2781 pn_iter = pns_comp_if->nodes[1];
2782 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002783 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002784 // for loop
Damiend99b0522013-12-21 18:17:45 +00002785 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002786 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002787 int l_end2 = comp_next_label(comp);
2788 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002789 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002790 EMIT_ARG(label_assign, l_top2);
2791 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002792 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2793 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 +00002794 EMIT_ARG(jump, l_top2);
2795 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002796 EMIT(for_iter_end);
2797 } else {
2798 // shouldn't happen
2799 assert(0);
2800 }
2801}
2802
Damiend99b0522013-12-21 18:17:45 +00002803void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002804 // see http://www.python.org/dev/peps/pep-0257/
2805
2806 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002807 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002808 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002809 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002810 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002811 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2812 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002813 for (int i = 0; i < num_nodes; i++) {
2814 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002815 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 +00002816 // not a newline, so this is the first statement; finish search
2817 break;
2818 }
2819 }
2820 // 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 +00002821 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002822 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002823 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002824 } else {
2825 return;
2826 }
2827
2828 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002829 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2830 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2831 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2832 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2833 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002834 compile_node(comp, pns->nodes[0]); // a doc string
2835 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002836 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002837 }
2838 }
2839 }
2840}
2841
2842void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2843 comp->pass = pass;
2844 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002845 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002846 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002847
2848 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002849 // reset maximum stack sizes in scope
2850 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002851 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002852 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002853 }
2854
Damien5ac1b2e2013-10-18 19:58:12 +01002855#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002856 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002857 scope_print_info(scope);
2858 }
Damien5ac1b2e2013-10-18 19:58:12 +01002859#endif
Damien429d7192013-10-04 19:53:11 +01002860
2861 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002862 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2863 assert(scope->kind == SCOPE_MODULE);
2864 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2865 compile_node(comp, pns->nodes[0]); // compile the expression
2866 EMIT(return_value);
2867 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002868 if (!comp->is_repl) {
2869 check_for_doc_string(comp, scope->pn);
2870 }
Damien429d7192013-10-04 19:53:11 +01002871 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002872 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002873 EMIT(return_value);
2874 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002875 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2876 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2877 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002878
2879 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002880 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002881 if (comp->pass == PASS_1) {
2882 comp->have_bare_star = false;
2883 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2884 }
2885
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02002886 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01002887
2888 compile_node(comp, pns->nodes[3]); // 3 is function body
2889 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002890 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002891 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002892 EMIT(return_value);
2893 }
2894 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002895 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2896 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2897 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002898
2899 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002900 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002901 if (comp->pass == PASS_1) {
2902 comp->have_bare_star = false;
2903 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2904 }
2905
2906 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2907 EMIT(return_value);
2908 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2909 // a bit of a hack at the moment
2910
Damiend99b0522013-12-21 18:17:45 +00002911 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2912 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2913 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2914 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2915 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002916
Damien George55baff42014-01-21 21:40:13 +00002917 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01002918 if (comp->pass == PASS_1) {
2919 bool added;
2920 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2921 assert(added);
2922 id_info->kind = ID_INFO_KIND_LOCAL;
2923 scope->num_params = 1;
2924 }
2925
2926 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002927 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01002928 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002929 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01002930 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002931 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01002932 }
2933
Damienb05d7072013-10-05 13:37:10 +01002934 int l_end = comp_next_label(comp);
2935 int l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002936 EMIT_ARG(load_id, qstr_arg);
2937 EMIT_ARG(label_assign, l_top);
2938 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01002939 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2940 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00002941 EMIT_ARG(jump, l_top);
2942 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002943 EMIT(for_iter_end);
2944
2945 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00002946 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002947 }
2948 EMIT(return_value);
2949 } else {
2950 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002951 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2952 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2953 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01002954
2955 if (comp->pass == PASS_1) {
2956 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002957 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01002958 assert(added);
2959 id_info->kind = ID_INFO_KIND_LOCAL;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002960 id_info = scope_find_or_add_id(scope, MP_QSTR___locals__, &added);
Damien429d7192013-10-04 19:53:11 +01002961 assert(added);
2962 id_info->kind = ID_INFO_KIND_LOCAL;
2963 id_info->param = true;
2964 scope->num_params = 1; // __locals__ is the parameter
2965 }
2966
Damien Georgeb9791222014-01-23 00:34:21 +00002967 EMIT_ARG(load_id, MP_QSTR___locals__);
Damien429d7192013-10-04 19:53:11 +01002968 EMIT(store_locals);
Damien Georgeb9791222014-01-23 00:34:21 +00002969 EMIT_ARG(load_id, MP_QSTR___name__);
2970 EMIT_ARG(store_id, MP_QSTR___module__);
2971 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
2972 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01002973
2974 check_for_doc_string(comp, pns->nodes[2]);
2975 compile_node(comp, pns->nodes[2]); // 2 is class body
2976
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002977 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01002978 assert(id != NULL);
2979 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00002980 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002981 } else {
Damien George6baf76e2013-12-30 22:32:17 +00002982#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00002983 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002984#else
Damien George35e2a4e2014-02-05 00:51:47 +00002985 EMIT_ARG(load_fast, MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00002986#endif
Damien429d7192013-10-04 19:53:11 +01002987 }
2988 EMIT(return_value);
2989 }
2990
Damien415eb6f2013-10-05 12:19:06 +01002991 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00002992
2993 // make sure we match all the exception levels
2994 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01002995}
2996
2997void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2998 comp->pass = pass;
2999 comp->scope_cur = scope;
3000 comp->next_label = 1;
3001
3002 if (scope->kind != SCOPE_FUNCTION) {
3003 printf("Error: inline assembler must be a function\n");
3004 return;
3005 }
3006
Damiena2f2f7d2013-10-06 00:14:13 +01003007 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003008 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003009 }
3010
Damien826005c2013-10-05 23:17:28 +01003011 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003012 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3013 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3014 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003015
Damiend99b0522013-12-21 18:17:45 +00003016 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003017
Damiena2f2f7d2013-10-06 00:14:13 +01003018 // parameters are in pns->nodes[1]
3019 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00003020 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003021 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00003022 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003023 }
3024
Damiend99b0522013-12-21 18:17:45 +00003025 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003026
Damiend99b0522013-12-21 18:17:45 +00003027 mp_parse_node_t pn_body = pns->nodes[3]; // body
3028 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003029 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3030
Damien Georgecbd2f742014-01-19 11:48:48 +00003031 /*
Damien826005c2013-10-05 23:17:28 +01003032 if (comp->pass == PASS_3) {
3033 //printf("----\n");
3034 scope_print_info(scope);
3035 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003036 */
Damien826005c2013-10-05 23:17:28 +01003037
3038 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003039 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3040 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
3041 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
3042 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3043 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3044 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3045 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3046 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3047 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3048 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3049 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3050 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3051 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003052 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3053
3054 // emit instructions
3055 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00003056 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgef41fdd02014-03-03 23:19:11 +00003057 compile_syntax_error(comp, "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003058 return;
3059 }
3060 int lab = comp_next_label(comp);
3061 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003062 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003063 }
3064 } else {
3065 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003066 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003067 }
3068 }
3069 }
3070
3071 if (comp->pass > PASS_1) {
3072 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01003073 }
Damien429d7192013-10-04 19:53:11 +01003074}
3075
3076void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3077 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003078 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003079 scope->num_locals = 0;
3080 for (int i = 0; i < scope->id_info_len; i++) {
3081 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003082 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003083 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3084 continue;
3085 }
3086 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3087 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3088 }
Damien9ecbcff2013-12-11 00:41:43 +00003089 // note: params always count for 1 local, even if they are a cell
Damien429d7192013-10-04 19:53:11 +01003090 if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
3091 id->local_num = scope->num_locals;
3092 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003093 }
3094 }
3095
3096 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003097#if MICROPY_EMIT_CPYTHON
3098 int num_cell = 0;
3099#endif
Damien9ecbcff2013-12-11 00:41:43 +00003100 for (int i = 0; i < scope->id_info_len; i++) {
3101 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003102#if MICROPY_EMIT_CPYTHON
3103 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003104 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003105 id->local_num = num_cell;
3106 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003107 }
Damien George6baf76e2013-12-30 22:32:17 +00003108#else
3109 // in Micro Python the cells come right after the fast locals
3110 // parameters are not counted here, since they remain at the start
3111 // of the locals, even if they are cell vars
3112 if (!id->param && id->kind == ID_INFO_KIND_CELL) {
3113 id->local_num = scope->num_locals;
3114 scope->num_locals += 1;
3115 }
3116#endif
Damien9ecbcff2013-12-11 00:41:43 +00003117 }
Damien9ecbcff2013-12-11 00:41:43 +00003118
3119 // compute the index of free vars (freevars[idx] in CPython)
3120 // make sure they are in the order of the parent scope
3121 if (scope->parent != NULL) {
3122 int num_free = 0;
3123 for (int i = 0; i < scope->parent->id_info_len; i++) {
3124 id_info_t *id = &scope->parent->id_info[i];
3125 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3126 for (int j = 0; j < scope->id_info_len; j++) {
3127 id_info_t *id2 = &scope->id_info[j];
3128 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +00003129 assert(!id2->param); // free vars should not be params
3130#if MICROPY_EMIT_CPYTHON
3131 // in CPython the frees are numbered after the cells
3132 id2->local_num = num_cell + num_free;
3133#else
3134 // in Micro Python the frees come first, before the params
3135 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003136#endif
3137 num_free += 1;
3138 }
3139 }
3140 }
Damien429d7192013-10-04 19:53:11 +01003141 }
Damien George6baf76e2013-12-30 22:32:17 +00003142#if !MICROPY_EMIT_CPYTHON
3143 // in Micro Python shift all other locals after the free locals
3144 if (num_free > 0) {
3145 for (int i = 0; i < scope->id_info_len; i++) {
3146 id_info_t *id = &scope->id_info[i];
3147 if (id->param || id->kind != ID_INFO_KIND_FREE) {
3148 id->local_num += num_free;
3149 }
3150 }
3151 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3152 scope->num_locals += num_free;
3153 }
3154#endif
Damien429d7192013-10-04 19:53:11 +01003155 }
3156
Damien George8725f8f2014-02-15 19:33:11 +00003157 // compute scope_flags
3158 //scope->scope_flags = 0; since we set some things in parameters
Damien429d7192013-10-04 19:53:11 +01003159 if (scope->kind != SCOPE_MODULE) {
Damien George8725f8f2014-02-15 19:33:11 +00003160 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien429d7192013-10-04 19:53:11 +01003161 }
3162 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) {
3163 assert(scope->parent != NULL);
Damien George8725f8f2014-02-15 19:33:11 +00003164 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien429d7192013-10-04 19:53:11 +01003165
3166 // TODO possibly other ways it can be nested
Damien George08d07552014-01-29 18:58:52 +00003167 // Note that we don't actually use this information at the moment (for CPython compat only)
3168 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 +00003169 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003170 }
3171 }
3172 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 George08335002014-01-18 23:24:36 +00003184mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, 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 Georgeeb7bfcb2014-01-04 15:57:35 +00003205 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE);
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
Damienc025ebb2013-10-12 14:30:21 +01003216 } else if (s->emit_options == 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
Damienc025ebb2013-10-12 14:30:21 +01003252 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
3253 // 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
Damien826005c2013-10-05 23:17:28 +01003276 case EMIT_OPT_NATIVE_PYTHON:
Damien3410be82013-10-07 23:09:10 +01003277 case 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;
Damien3410be82013-10-07 23:09:10 +01003290 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == 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}