blob: 44b37b934252ccec58b39241a8872ec0c4253856 [file] [log] [blame]
xbeefe34222014-03-16 00:14:26 -07001#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +01002#include <stdint.h>
3#include <stdio.h>
4#include <string.h>
5#include <assert.h>
Rachel Dowdall56402792014-03-22 20:19:24 +00006#include <math.h>
Damien429d7192013-10-04 19:53:11 +01007
8#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00009#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000010#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010011#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010012#include "parse.h"
13#include "scope.h"
Damiend99b0522013-12-21 18:17:45 +000014#include "runtime0.h"
Damien429d7192013-10-04 19:53:11 +010015#include "emit.h"
Damien George2326d522014-03-27 23:26:35 +000016#include "emitglue.h"
Damien George1fb03172014-01-03 14:22:03 +000017#include "obj.h"
18#include "compile.h"
19#include "runtime.h"
Damien Georgeecf5b772014-04-04 11:13:51 +000020#include "smallint.h"
Damien429d7192013-10-04 19:53:11 +010021
22// TODO need to mangle __attr names
23
Damience89a212013-10-15 22:25:17 +010024#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_THUMB)
25
Damien429d7192013-10-04 19:53:11 +010026typedef enum {
27 PN_none = 0,
Damien George00208ce2014-01-23 00:00:53 +000028#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien429d7192013-10-04 19:53:11 +010029#include "grammar.h"
30#undef DEF_RULE
31 PN_maximum_number_of,
32} pn_kind_t;
33
Damien Georgeb9791222014-01-23 00:34:21 +000034#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
35#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
36#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
37#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010038
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)) {
Damien Georgeecf5b772014-04-04 11:13:51 +0000146 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_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) {
Damien Georgeecf5b772014-04-04 11:13:51 +0000149 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_floor_divide(arg0, arg1));
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300150 }
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
Damien Georgeaf6edc62014-04-02 16:12:28 +01001437
1438 if (
1439#if !MICROPY_EMIT_CPYTHON
1440 // optimisation to not jump over non-existent elif/else blocks (this optimisation is not in CPython)
1441 !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) &&
1442#endif
1443 // optimisation to not jump if last instruction was return
1444 !EMIT(last_emit_was_return_value)
1445 ) {
1446 // jump over elif/else blocks
1447 EMIT_ARG(jump, l_end);
1448 }
1449
Damien Georgeb9791222014-01-23 00:34:21 +00001450 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001451
Damiend99b0522013-12-21 18:17:45 +00001452 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001453 // compile elif blocks
1454
Damiend99b0522013-12-21 18:17:45 +00001455 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001456
Damiend99b0522013-12-21 18:17:45 +00001457 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001458 // multiple elif blocks
1459
Damiend99b0522013-12-21 18:17:45 +00001460 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001461 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001462 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001463 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001464 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1465
1466 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001467 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001468 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001469 }
Damien Georgeb9791222014-01-23 00:34:21 +00001470 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001471 }
1472
1473 } else {
1474 // a single elif block
1475
Damienb05d7072013-10-05 13:37:10 +01001476 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001477 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1478
1479 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001480 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001481 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001482 }
Damien Georgeb9791222014-01-23 00:34:21 +00001483 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001484 }
1485 }
1486
1487 // compile else block
1488 compile_node(comp, pns->nodes[3]); // can be null
1489
Damien Georgeb9791222014-01-23 00:34:21 +00001490 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001491}
1492
Damien Georgecbddb272014-02-01 20:08:18 +00001493#define START_BREAK_CONTINUE_BLOCK \
1494 int old_break_label = comp->break_label; \
1495 int old_continue_label = comp->continue_label; \
1496 int break_label = comp_next_label(comp); \
1497 int continue_label = comp_next_label(comp); \
1498 comp->break_label = break_label; \
1499 comp->continue_label = continue_label; \
1500 comp->break_continue_except_level = comp->cur_except_level;
1501
1502#define END_BREAK_CONTINUE_BLOCK \
1503 comp->break_label = old_break_label; \
1504 comp->continue_label = old_continue_label; \
1505 comp->break_continue_except_level = comp->cur_except_level;
1506
Damiend99b0522013-12-21 18:17:45 +00001507void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001508 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001509
Damience89a212013-10-15 22:25:17 +01001510 // compared to CPython, we have an optimised version of while loops
1511#if MICROPY_EMIT_CPYTHON
1512 int done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001513 EMIT_ARG(setup_loop, break_label);
1514 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001515 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1516 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001517 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001518 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001519 }
Damien Georgeb9791222014-01-23 00:34:21 +00001520 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001521 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1522 // this is a small hack to agree with CPython
1523 if (!node_is_const_true(pns->nodes[0])) {
1524 EMIT(pop_block);
1525 }
Damience89a212013-10-15 22:25:17 +01001526#else
1527 int top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001528 EMIT_ARG(jump, continue_label);
1529 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001530 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001531 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001532 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1533#endif
1534
1535 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001536 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001537
1538 compile_node(comp, pns->nodes[2]); // else
1539
Damien Georgeb9791222014-01-23 00:34:21 +00001540 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001541}
1542
Damienf72fd0e2013-11-06 20:20:49 +00001543// TODO preload end and step onto stack if they are not constants
Damien George3ff2d032014-03-31 18:02:22 +01001544// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
1545// And, if the loop never runs, the loop variable should never be assigned
Damiend99b0522013-12-21 18:17:45 +00001546void 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 +00001547 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001548
1549 int top_label = comp_next_label(comp);
Damien George600ae732014-01-21 23:48:04 +00001550 int entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001551
Damien George3ff2d032014-03-31 18:02:22 +01001552 // compile: start, duplicated on stack
Damienf72fd0e2013-11-06 20:20:49 +00001553 compile_node(comp, pn_start);
Damien George3ff2d032014-03-31 18:02:22 +01001554 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001555
Damien Georgeb9791222014-01-23 00:34:21 +00001556 EMIT_ARG(jump, entry_label);
1557 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001558
Damien George3ff2d032014-03-31 18:02:22 +01001559 // at this point we actually have 1 less element on the stack
1560 EMIT_ARG(set_stack_size, EMIT(get_stack_size) - 1);
1561
1562 // store next value to var
1563 c_assign(comp, pn_var, ASSIGN_STORE);
1564
Damienf3822fc2013-11-09 20:12:03 +00001565 // compile body
1566 compile_node(comp, pn_body);
1567
Damien Georgeb9791222014-01-23 00:34:21 +00001568 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001569
Damien George3ff2d032014-03-31 18:02:22 +01001570 // compile: var + step, duplicated on stack
1571 compile_node(comp, pn_var);
Damienf72fd0e2013-11-06 20:20:49 +00001572 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001573 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damien George3ff2d032014-03-31 18:02:22 +01001574 EMIT(dup_top);
Damienf72fd0e2013-11-06 20:20:49 +00001575
Damien Georgeb9791222014-01-23 00:34:21 +00001576 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001577
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001578 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001579 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001580 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1581 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001582 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001583 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001584 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001585 }
Damien Georgeb9791222014-01-23 00:34:21 +00001586 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001587
Damien George3ff2d032014-03-31 18:02:22 +01001588 // discard final value of var that failed the loop condition
1589 EMIT(pop_top);
1590
Damienf72fd0e2013-11-06 20:20:49 +00001591 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001592 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001593
1594 compile_node(comp, pn_else);
1595
Damien Georgeb9791222014-01-23 00:34:21 +00001596 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001597}
1598
Damiend99b0522013-12-21 18:17:45 +00001599void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001600#if !MICROPY_EMIT_CPYTHON
1601 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1602 // this is actually slower, but uses no heap memory
1603 // for viper it will be much, much faster
Damiend99b0522013-12-21 18:17:45 +00001604 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)) {
1605 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001606 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1607 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1608 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1609 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001610 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1611 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001612 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001613 mp_parse_node_t pn_range_start;
1614 mp_parse_node_t pn_range_end;
1615 mp_parse_node_t pn_range_step;
1616 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001617 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001618 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001619 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001620 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001621 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001622 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001623 } else if (n_args == 2) {
1624 pn_range_start = args[0];
1625 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001626 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001627 } else {
1628 pn_range_start = args[0];
1629 pn_range_end = args[1];
1630 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001631 // We need to know sign of step. This is possible only if it's constant
1632 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1633 optimize = false;
1634 }
Damienf72fd0e2013-11-06 20:20:49 +00001635 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001636 }
1637 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001638 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1639 return;
1640 }
1641 }
1642 }
1643#endif
1644
Damien Georgecbddb272014-02-01 20:08:18 +00001645 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001646
Damienb05d7072013-10-05 13:37:10 +01001647 int pop_label = comp_next_label(comp);
1648 int end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001649
Damience89a212013-10-15 22:25:17 +01001650 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1651#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001652 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001653#endif
1654
Damien429d7192013-10-04 19:53:11 +01001655 compile_node(comp, pns->nodes[1]); // iterator
1656 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001657 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001658 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001659 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1660 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001661 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001662 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001663 }
Damien Georgeb9791222014-01-23 00:34:21 +00001664 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001665 EMIT(for_iter_end);
1666
1667 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001668 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001669
Damience89a212013-10-15 22:25:17 +01001670#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001671 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001672#endif
Damien429d7192013-10-04 19:53:11 +01001673
1674 compile_node(comp, pns->nodes[3]); // else (not tested)
1675
Damien Georgeb9791222014-01-23 00:34:21 +00001676 EMIT_ARG(label_assign, break_label);
1677 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001678}
1679
Damiend99b0522013-12-21 18:17:45 +00001680void 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 +01001681 // this function is a bit of a hack at the moment
1682 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1683
1684 // setup code
1685 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001686 int l1 = comp_next_label(comp);
1687 int success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001688
Damien Georgeb9791222014-01-23 00:34:21 +00001689 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001690 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001691
Damien429d7192013-10-04 19:53:11 +01001692 compile_node(comp, pn_body); // body
1693 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001694 EMIT_ARG(jump, success_label);
1695 EMIT_ARG(label_assign, l1);
Damienb05d7072013-10-05 13:37:10 +01001696 int l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001697
1698 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001699 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1700 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001701
1702 qstr qstr_exception_local = 0;
Damienb05d7072013-10-05 13:37:10 +01001703 int end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001704
Damiend99b0522013-12-21 18:17:45 +00001705 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001706 // this is a catch all exception handler
1707 if (i + 1 != n_except) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001708 compile_syntax_error(comp, "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001709 return;
1710 }
1711 } else {
1712 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001713 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1714 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1715 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1716 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001717 // handler binds the exception to a local
1718 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001719 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001720 }
1721 }
1722 EMIT(dup_top);
1723 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001724 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001725 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001726 }
1727
1728 EMIT(pop_top);
1729
1730 if (qstr_exception_local == 0) {
1731 EMIT(pop_top);
1732 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001733 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001734 }
1735
1736 EMIT(pop_top);
1737
Damiene2880aa2013-12-20 14:22:59 +00001738 int l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001739 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001740 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001741 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001742 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001743 }
1744 compile_node(comp, pns_except->nodes[1]);
1745 if (qstr_exception_local != 0) {
1746 EMIT(pop_block);
1747 }
1748 EMIT(pop_except);
1749 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001750 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1751 EMIT_ARG(label_assign, l3);
1752 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1753 EMIT_ARG(store_id, qstr_exception_local);
1754 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001755
Damien George8dcc0c72014-03-27 10:55:21 +00001756 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001757 EMIT(end_finally);
1758 }
Damien Georgeb9791222014-01-23 00:34:21 +00001759 EMIT_ARG(jump, l2);
1760 EMIT_ARG(label_assign, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001761 }
1762
Damien George8dcc0c72014-03-27 10:55:21 +00001763 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001764 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001765
Damien Georgeb9791222014-01-23 00:34:21 +00001766 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001767 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001768 EMIT_ARG(label_assign, l2);
1769 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001770}
1771
Damiend99b0522013-12-21 18:17:45 +00001772void 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 +01001773 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1774 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001775 int l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001776
Damien Georgeb9791222014-01-23 00:34:21 +00001777 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001778 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001779
Damien429d7192013-10-04 19:53:11 +01001780 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001781 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien429d7192013-10-04 19:53:11 +01001782 compile_node(comp, pn_body);
1783 } else {
1784 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1785 }
1786 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001787 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1788 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001789 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001790
Damien George8dcc0c72014-03-27 10:55:21 +00001791 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001792 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001793
Damien Georgeb9791222014-01-23 00:34:21 +00001794 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001795}
1796
Damiend99b0522013-12-21 18:17:45 +00001797void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1798 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1799 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1800 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001801 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001802 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1803 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001804 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001805 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001806 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001807 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001808 // no finally
1809 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1810 } else {
1811 // have finally
Damiend99b0522013-12-21 18:17:45 +00001812 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 +01001813 }
1814 } else {
1815 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001816 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001817 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001818 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001819 }
1820 } else {
1821 // shouldn't happen
1822 assert(0);
1823 }
1824}
1825
Damiend99b0522013-12-21 18:17:45 +00001826void 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 +01001827 if (n == 0) {
1828 // no more pre-bits, compile the body of the with
1829 compile_node(comp, body);
1830 } else {
Damienb05d7072013-10-05 13:37:10 +01001831 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001832 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001833 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001834 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001835 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001836 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001837 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1838 } else {
1839 // this pre-bit is just an expression
1840 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001841 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001842 EMIT(pop_top);
1843 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001844 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001845 // compile additional pre-bits and the body
1846 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1847 // finish this with block
1848 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001849 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1850 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001851 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001852 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001853 EMIT(end_finally);
1854 }
1855}
1856
Damiend99b0522013-12-21 18:17:45 +00001857void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001858 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001859 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001860 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1861 assert(n > 0);
1862
1863 // compile in a nested fashion
1864 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1865}
1866
Damiend99b0522013-12-21 18:17:45 +00001867void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1868 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001869 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1870 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001871 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001872 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001873 EMIT_ARG(call_function, 1, 0, false, false);
Damien5ac1b2e2013-10-18 19:58:12 +01001874 EMIT(pop_top);
1875
Damien429d7192013-10-04 19:53:11 +01001876 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001877 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001878 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001879 // do nothing with a lonely constant
1880 } else {
1881 compile_node(comp, pns->nodes[0]); // just an expression
1882 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1883 }
Damien429d7192013-10-04 19:53:11 +01001884 }
1885 } else {
Damiend99b0522013-12-21 18:17:45 +00001886 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1887 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001888 if (kind == PN_expr_stmt_augassign) {
1889 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1890 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001891 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001892 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001893 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001894 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1895 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1896 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1897 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1898 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1899 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1900 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1901 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1902 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1903 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1904 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
1905 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
1906 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01001907 }
Damien George7e5fb242014-02-01 22:18:47 +00001908 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001909 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1910 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001911 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1912 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001913 // following CPython, we store left-most first
1914 if (rhs > 0) {
1915 EMIT(dup_top);
1916 }
1917 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1918 for (int i = 0; i < rhs; i++) {
1919 if (i + 1 < rhs) {
1920 EMIT(dup_top);
1921 }
Damiend99b0522013-12-21 18:17:45 +00001922 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001923 }
1924 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001925 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1926 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1927 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1928 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01001929 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001930 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1931 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001932 compile_node(comp, pns10->nodes[0]); // rhs
1933 compile_node(comp, pns10->nodes[1]); // rhs
1934 EMIT(rot_two);
1935 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1936 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00001937 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1938 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1939 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1940 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01001941 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001942 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1943 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001944 compile_node(comp, pns10->nodes[0]); // rhs
1945 compile_node(comp, pns10->nodes[1]); // rhs
1946 compile_node(comp, pns10->nodes[2]); // rhs
1947 EMIT(rot_three);
1948 EMIT(rot_two);
1949 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1950 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1951 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1952 } else {
1953 compile_node(comp, pns1->nodes[0]); // rhs
1954 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1955 }
1956 } else {
1957 // shouldn't happen
1958 assert(0);
1959 }
1960 }
1961}
1962
Damien Georged17926d2014-03-30 13:35:08 +01001963void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00001964 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001965 compile_node(comp, pns->nodes[0]);
1966 for (int i = 1; i < num_nodes; i += 1) {
1967 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001968 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001969 }
1970}
1971
Damiend99b0522013-12-21 18:17:45 +00001972void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1973 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1974 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001975
1976 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001977 int l_fail = comp_next_label(comp);
1978 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001979 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1980 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001981 EMIT_ARG(jump, l_end);
1982 EMIT_ARG(label_assign, l_fail);
1983 EMIT_ARG(set_stack_size, stack_size); // force stack size reset
Damien429d7192013-10-04 19:53:11 +01001984 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001985 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001986}
1987
Damiend99b0522013-12-21 18:17:45 +00001988void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001989 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00001990 //mp_parse_node_t pn_params = pns->nodes[0];
1991 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001992
1993 if (comp->pass == PASS_1) {
1994 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00001995 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 +01001996 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001997 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001998 }
1999
2000 // get the scope for this lambda
2001 scope_t *this_scope = (scope_t*)pns->nodes[2];
2002
2003 // make the lambda
2004 close_over_variables_etc(comp, this_scope, 0, 0);
2005}
2006
Damiend99b0522013-12-21 18:17:45 +00002007void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01002008 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002009 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002010 for (int i = 0; i < n; i += 1) {
2011 compile_node(comp, pns->nodes[i]);
2012 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002013 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002014 }
2015 }
Damien Georgeb9791222014-01-23 00:34:21 +00002016 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002017}
2018
Damiend99b0522013-12-21 18:17:45 +00002019void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01002020 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002021 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002022 for (int i = 0; i < n; i += 1) {
2023 compile_node(comp, pns->nodes[i]);
2024 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002025 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002026 }
2027 }
Damien Georgeb9791222014-01-23 00:34:21 +00002028 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002029}
2030
Damiend99b0522013-12-21 18:17:45 +00002031void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002032 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002033 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002034}
2035
Damiend99b0522013-12-21 18:17:45 +00002036void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002037 int stack_size = EMIT(get_stack_size);
Damiend99b0522013-12-21 18:17:45 +00002038 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002039 compile_node(comp, pns->nodes[0]);
2040 bool multi = (num_nodes > 3);
2041 int l_fail = 0;
2042 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002043 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002044 }
2045 for (int i = 1; i + 1 < num_nodes; i += 2) {
2046 compile_node(comp, pns->nodes[i + 1]);
2047 if (i + 2 < num_nodes) {
2048 EMIT(dup_top);
2049 EMIT(rot_three);
2050 }
Damien George7e5fb242014-02-01 22:18:47 +00002051 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002052 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002053 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002054 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2055 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2056 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2057 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2058 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2059 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2060 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2061 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002062 }
2063 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002064 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2065 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2066 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002067 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002068 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002069 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002070 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002071 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002072 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002073 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002074 }
2075 } else {
2076 // shouldn't happen
2077 assert(0);
2078 }
2079 } else {
2080 // shouldn't happen
2081 assert(0);
2082 }
2083 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002084 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002085 }
2086 }
2087 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002088 int l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002089 EMIT_ARG(jump, l_end);
2090 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01002091 EMIT(rot_two);
2092 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002093 EMIT_ARG(label_assign, l_end);
2094 EMIT_ARG(set_stack_size, stack_size + 1); // force stack size
Damien429d7192013-10-04 19:53:11 +01002095 }
2096}
2097
Damiend99b0522013-12-21 18:17:45 +00002098void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002099 // TODO
2100 assert(0);
2101 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002102 //EMIT_ARG(unary_op, "UNARY_STAR");
Damien429d7192013-10-04 19:53:11 +01002103}
2104
Damiend99b0522013-12-21 18:17:45 +00002105void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002106 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002107}
2108
Damiend99b0522013-12-21 18:17:45 +00002109void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002110 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002111}
2112
Damiend99b0522013-12-21 18:17:45 +00002113void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002114 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002115}
2116
Damiend99b0522013-12-21 18:17:45 +00002117void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2118 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002119 compile_node(comp, pns->nodes[0]);
2120 for (int i = 1; i + 1 < num_nodes; i += 2) {
2121 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002122 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002123 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002124 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002125 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002126 } else {
2127 // shouldn't happen
2128 assert(0);
2129 }
2130 }
2131}
2132
Damiend99b0522013-12-21 18:17:45 +00002133void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2134 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002135 compile_node(comp, pns->nodes[0]);
2136 for (int i = 1; i + 1 < num_nodes; i += 2) {
2137 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002138 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002139 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002140 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002141 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002142 } else {
2143 // shouldn't happen
2144 assert(0);
2145 }
2146 }
2147}
2148
Damiend99b0522013-12-21 18:17:45 +00002149void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2150 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002151 compile_node(comp, pns->nodes[0]);
2152 for (int i = 1; i + 1 < num_nodes; i += 2) {
2153 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002154 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002155 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002156 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002157 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002158 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002159 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002160 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002161 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002162 } else {
2163 // shouldn't happen
2164 assert(0);
2165 }
2166 }
2167}
2168
Damiend99b0522013-12-21 18:17:45 +00002169void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002170 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002171 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002172 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002173 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002174 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002175 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002176 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002177 } else {
2178 // shouldn't happen
2179 assert(0);
2180 }
2181}
2182
Damien George35e2a4e2014-02-05 00:51:47 +00002183void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2184 // this is to handle special super() call
2185 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2186
2187 compile_generic_all_nodes(comp, pns);
2188}
2189
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002190STATIC 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 +01002191 // function to call is on top of stack
2192
Damien George35e2a4e2014-02-05 00:51:47 +00002193#if !MICROPY_EMIT_CPYTHON
2194 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002195 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 +00002196 EMIT_ARG(load_id, MP_QSTR___class__);
2197 // get first argument to function
2198 bool found = false;
2199 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
2200 if (comp->scope_cur->id_info[i].param) {
2201 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
2202 found = true;
2203 break;
2204 }
2205 }
2206 if (!found) {
2207 printf("TypeError: super() call cannot find self\n");
2208 return;
2209 }
2210 EMIT_ARG(call_function, 2, 0, false, false);
2211 return;
2212 }
2213#endif
2214
Damien429d7192013-10-04 19:53:11 +01002215 int old_n_arg_keyword = comp->n_arg_keyword;
2216 bool old_have_star_arg = comp->have_star_arg;
2217 bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
2218 comp->n_arg_keyword = 0;
2219 comp->have_star_arg = false;
2220 comp->have_dbl_star_arg = false;
2221
Damien Georgebbcd49a2014-02-06 20:30:16 +00002222 compile_node(comp, pn_arglist); // arguments to function call; can be null
Damien429d7192013-10-04 19:53:11 +01002223
2224 // compute number of positional arguments
Damien Georgebbcd49a2014-02-06 20:30:16 +00002225 int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
Damien429d7192013-10-04 19:53:11 +01002226 if (comp->have_star_arg) {
2227 n_positional -= 1;
2228 }
2229 if (comp->have_dbl_star_arg) {
2230 n_positional -= 1;
2231 }
2232
2233 if (is_method_call) {
Damien Georgeb9791222014-01-23 00:34:21 +00002234 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 +01002235 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002236 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 +01002237 }
2238
2239 comp->n_arg_keyword = old_n_arg_keyword;
2240 comp->have_star_arg = old_have_star_arg;
2241 comp->have_dbl_star_arg = old_have_dbl_star_arg;
2242}
2243
Damiend99b0522013-12-21 18:17:45 +00002244void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2245 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002246 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002247 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 +01002248 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002249 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2250 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002251 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002252 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002253 i += 1;
2254 } else {
2255 compile_node(comp, pns->nodes[i]);
2256 }
Damien George35e2a4e2014-02-05 00:51:47 +00002257 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002258 }
2259}
2260
Damiend99b0522013-12-21 18:17:45 +00002261void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002262 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002263 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002264}
2265
Damiend99b0522013-12-21 18:17:45 +00002266void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002267 // a list of strings
Damien63321742013-12-10 17:41:49 +00002268
2269 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002270 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002271 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002272 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002273 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002274 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2275 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2276 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002277 if (i == 0) {
2278 string_kind = pn_kind;
2279 } else if (pn_kind != string_kind) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002280 compile_syntax_error(comp, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002281 return;
2282 }
Damien George55baff42014-01-21 21:40:13 +00002283 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002284 }
Damien63321742013-12-10 17:41:49 +00002285
Damien63321742013-12-10 17:41:49 +00002286 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002287 byte *q_ptr;
2288 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002289 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002290 uint s_len;
2291 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002292 memcpy(s_dest, s, s_len);
2293 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002294 }
Damien George55baff42014-01-21 21:40:13 +00002295 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002296
Damien Georgeb9791222014-01-23 00:34:21 +00002297 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002298}
2299
2300// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002301void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2302 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2303 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2304 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002305
2306 if (comp->pass == PASS_1) {
2307 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002308 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 +01002309 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002310 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002311 }
2312
2313 // get the scope for this comprehension
2314 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2315
2316 // compile the comprehension
2317 close_over_variables_etc(comp, this_scope, 0, 0);
2318
2319 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2320 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002321 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01002322}
2323
Damiend99b0522013-12-21 18:17:45 +00002324void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2325 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002326 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002327 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2328 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2329 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2330 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2331 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2332 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2333 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002334 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002335 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002336 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002337 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002338 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002339 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002340 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002341 // generator expression
2342 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2343 } else {
2344 // tuple with 2 items
2345 goto tuple_with_2_items;
2346 }
2347 } else {
2348 // tuple with 2 items
2349 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002350 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002351 }
2352 } else {
2353 // parenthesis around a single item, is just that item
2354 compile_node(comp, pns->nodes[0]);
2355 }
2356}
2357
Damiend99b0522013-12-21 18:17:45 +00002358void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2359 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002360 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002361 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002362 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2363 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2364 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2365 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2366 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002367 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002368 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002369 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002370 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002371 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002372 // list of many items
2373 compile_node(comp, pns2->nodes[0]);
2374 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002375 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002376 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002377 // list comprehension
2378 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2379 } else {
2380 // list with 2 items
2381 goto list_with_2_items;
2382 }
2383 } else {
2384 // list with 2 items
2385 list_with_2_items:
2386 compile_node(comp, pns2->nodes[0]);
2387 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002388 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002389 }
2390 } else {
2391 // list with 1 item
2392 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002393 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002394 }
2395}
2396
Damiend99b0522013-12-21 18:17:45 +00002397void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2398 mp_parse_node_t pn = pns->nodes[0];
2399 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002400 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002401 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002402 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2403 pns = (mp_parse_node_struct_t*)pn;
2404 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002405 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002406 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002407 compile_node(comp, pn);
2408 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002409 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2410 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2411 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2412 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002413 // dict/set with multiple elements
2414
2415 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002416 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002417 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2418
2419 // first element sets whether it's a dict or set
2420 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002421 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002422 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002423 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002424 compile_node(comp, pns->nodes[0]);
2425 EMIT(store_map);
2426 is_dict = true;
2427 } else {
2428 // a set
2429 compile_node(comp, pns->nodes[0]); // 1st value of set
2430 is_dict = false;
2431 }
2432
2433 // process rest of elements
2434 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002435 mp_parse_node_t pn = nodes[i];
2436 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002437 compile_node(comp, pn);
2438 if (is_dict) {
2439 if (!is_key_value) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002440 compile_syntax_error(comp, "?expecting key:value for dictiona");
Damien429d7192013-10-04 19:53:11 +01002441 return;
2442 }
2443 EMIT(store_map);
2444 } else {
2445 if (is_key_value) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002446 compile_syntax_error(comp, "?expecting just a value for s");
Damien429d7192013-10-04 19:53:11 +01002447 return;
2448 }
2449 }
2450 }
2451
2452 // if it's a set, build it
2453 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002454 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002455 }
Damiend99b0522013-12-21 18:17:45 +00002456 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002457 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002458 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002459 // a dictionary comprehension
2460 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2461 } else {
2462 // a set comprehension
2463 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2464 }
2465 } else {
2466 // shouldn't happen
2467 assert(0);
2468 }
2469 } else {
2470 // set with one element
2471 goto set_with_one_element;
2472 }
2473 } else {
2474 // set with one element
2475 set_with_one_element:
2476 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002477 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002478 }
2479}
2480
Damiend99b0522013-12-21 18:17:45 +00002481void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002482 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002483}
2484
Damiend99b0522013-12-21 18:17:45 +00002485void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002486 // object who's index we want is on top of stack
2487 compile_node(comp, pns->nodes[0]); // the index
Damien Georged17926d2014-03-30 13:35:08 +01002488 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002489}
2490
Damiend99b0522013-12-21 18:17:45 +00002491void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002492 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002493 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002494}
2495
Damiend99b0522013-12-21 18:17:45 +00002496void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2497 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2498 mp_parse_node_t pn = pns->nodes[0];
2499 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002500 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002501 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2502 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002503 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2504 pns = (mp_parse_node_struct_t*)pn;
2505 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002506 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002507 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002508 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002509 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002510 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002511 } else {
2512 // [?::x]
2513 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002514 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002515 }
Damiend99b0522013-12-21 18:17:45 +00002516 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002517 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002518 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2519 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2520 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2521 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002522 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002523 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002524 } else {
2525 // [?:x:x]
2526 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002527 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002528 }
2529 } else {
2530 // [?:x]
2531 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002532 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002533 }
2534 } else {
2535 // [?:x]
2536 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002537 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002538 }
2539}
2540
Damiend99b0522013-12-21 18:17:45 +00002541void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002542 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002543 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2544 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002545}
2546
Damiend99b0522013-12-21 18:17:45 +00002547void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002548 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002549 compile_subscript_3_helper(comp, pns);
2550}
2551
Damiend99b0522013-12-21 18:17:45 +00002552void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002553 // if this is called then we are compiling a dict key:value pair
2554 compile_node(comp, pns->nodes[1]); // value
2555 compile_node(comp, pns->nodes[0]); // key
2556}
2557
Damiend99b0522013-12-21 18:17:45 +00002558void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002559 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002560 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002561 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002562}
2563
Damiend99b0522013-12-21 18:17:45 +00002564void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002565 if (comp->have_star_arg) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002566 compile_syntax_error(comp, "?can't have multiple *x");
Damien429d7192013-10-04 19:53:11 +01002567 return;
2568 }
2569 comp->have_star_arg = true;
2570 compile_node(comp, pns->nodes[0]);
2571}
2572
Damiend99b0522013-12-21 18:17:45 +00002573void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002574 if (comp->have_dbl_star_arg) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002575 compile_syntax_error(comp, "?can't have multiple **x");
Damien429d7192013-10-04 19:53:11 +01002576 return;
2577 }
2578 comp->have_dbl_star_arg = true;
2579 compile_node(comp, pns->nodes[0]);
2580}
2581
Damiend99b0522013-12-21 18:17:45 +00002582void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2583 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2584 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2585 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2586 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002587 compile_syntax_error(comp, "?lhs of keyword argument must be an id");
Damien429d7192013-10-04 19:53:11 +01002588 return;
2589 }
Damien Georgeb9791222014-01-23 00:34:21 +00002590 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002591 compile_node(comp, pns2->nodes[0]);
2592 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002593 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002594 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2595 } else {
2596 // shouldn't happen
2597 assert(0);
2598 }
2599}
2600
Damiend99b0522013-12-21 18:17:45 +00002601void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002602 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002603 compile_syntax_error(comp, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002604 return;
2605 }
Damiend99b0522013-12-21 18:17:45 +00002606 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002607 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002608 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002609 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2610 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002611 compile_node(comp, pns->nodes[0]);
2612 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002613 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002614 EMIT(yield_from);
2615 } else {
2616 compile_node(comp, pns->nodes[0]);
2617 EMIT(yield_value);
2618 }
2619}
2620
Damiend99b0522013-12-21 18:17:45 +00002621typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002622STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002623 NULL,
2624#define nc NULL
2625#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002626#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002627#include "grammar.h"
2628#undef nc
2629#undef c
2630#undef DEF_RULE
2631};
2632
Damiend99b0522013-12-21 18:17:45 +00002633void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2634 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002635 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002636 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2637 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2638 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002639 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002640 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002641 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002642 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002643 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2644 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2645 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2646 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002647 case MP_PARSE_NODE_TOKEN:
2648 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002649 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002650 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002651 // do nothing
2652 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002653 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002654 }
2655 break;
Damien429d7192013-10-04 19:53:11 +01002656 default: assert(0);
2657 }
2658 } else {
Damiend99b0522013-12-21 18:17:45 +00002659 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002660 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002661 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002662 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002663 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002664#if MICROPY_DEBUG_PRINTERS
2665 mp_parse_node_print(pn, 0);
2666#endif
Damien429d7192013-10-04 19:53:11 +01002667 assert(0);
2668 } else {
2669 f(comp, pns);
2670 }
2671 }
2672}
2673
Damiend99b0522013-12-21 18:17:45 +00002674void 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 +01002675 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002676 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002677 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2678 if (MP_PARSE_NODE_IS_ID(pn)) {
2679 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002680 if (comp->have_bare_star) {
2681 // comes after a bare star, so doesn't count as a parameter
2682 } else {
2683 comp->scope_cur->num_params += 1;
2684 }
Damienb14de212013-10-06 00:28:28 +01002685 } else {
Damiend99b0522013-12-21 18:17:45 +00002686 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2687 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2688 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2689 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002690 //int node_index = 1; unused
2691 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002692 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002693 // this parameter has an annotation
2694 pn_annotation = pns->nodes[1];
2695 }
2696 //node_index = 2; unused
2697 }
2698 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002699 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002700 // this parameter has a default value
2701 if (comp->have_bare_star) {
2702 comp->scope_cur->num_dict_params += 1;
2703 } else {
2704 comp->scope_cur->num_default_params += 1;
2705 }
2706 }
2707 */
2708 if (comp->have_bare_star) {
2709 // comes after a bare star, so doesn't count as a parameter
2710 } else {
2711 comp->scope_cur->num_params += 1;
2712 }
Damiend99b0522013-12-21 18:17:45 +00002713 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2714 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002715 // bare star
2716 // TODO see http://www.python.org/dev/peps/pep-3102/
2717 comp->have_bare_star = true;
2718 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002719 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002720 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002721 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002722 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2723 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002724 // named star with annotation
Damien George8725f8f2014-02-15 19:33:11 +00002725 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002726 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2727 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002728 pn_annotation = pns->nodes[1];
2729 } else {
2730 // shouldn't happen
2731 assert(0);
2732 }
Damiend99b0522013-12-21 18:17:45 +00002733 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2734 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2735 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002736 // this parameter has an annotation
2737 pn_annotation = pns->nodes[1];
2738 }
Damien George8725f8f2014-02-15 19:33:11 +00002739 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002740 } else {
Damienb14de212013-10-06 00:28:28 +01002741 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002742 assert(0);
2743 }
Damien429d7192013-10-04 19:53:11 +01002744 }
2745
2746 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002747 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002748 // TODO this parameter has an annotation
2749 }
2750 bool added;
2751 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2752 if (!added) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002753 compile_syntax_error(comp, "?same name used for parameter");
Damien429d7192013-10-04 19:53:11 +01002754 return;
2755 }
2756 id_info->param = true;
2757 id_info->kind = ID_INFO_KIND_LOCAL;
2758 }
2759}
2760
Damiend99b0522013-12-21 18:17:45 +00002761void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002762 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2763}
2764
Damiend99b0522013-12-21 18:17:45 +00002765void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002766 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2767}
2768
Damiend99b0522013-12-21 18:17:45 +00002769void 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 +01002770 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002771 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002772 // no more nested if/for; compile inner expression
2773 compile_node(comp, pn_inner_expr);
2774 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002775 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002776 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002777 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002778 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002779 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002780 } else {
2781 EMIT(yield_value);
2782 EMIT(pop_top);
2783 }
Damiend99b0522013-12-21 18:17:45 +00002784 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002785 // if condition
Damiend99b0522013-12-21 18:17:45 +00002786 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002787 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2788 pn_iter = pns_comp_if->nodes[1];
2789 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002790 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002791 // for loop
Damiend99b0522013-12-21 18:17:45 +00002792 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002793 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002794 int l_end2 = comp_next_label(comp);
2795 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002796 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002797 EMIT_ARG(label_assign, l_top2);
2798 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002799 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2800 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 +00002801 EMIT_ARG(jump, l_top2);
2802 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002803 EMIT(for_iter_end);
2804 } else {
2805 // shouldn't happen
2806 assert(0);
2807 }
2808}
2809
Damiend99b0522013-12-21 18:17:45 +00002810void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002811 // see http://www.python.org/dev/peps/pep-0257/
2812
2813 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002814 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002815 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002816 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002817 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002818 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2819 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002820 for (int i = 0; i < num_nodes; i++) {
2821 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002822 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 +00002823 // not a newline, so this is the first statement; finish search
2824 break;
2825 }
2826 }
2827 // 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 +00002828 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002829 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002830 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002831 } else {
2832 return;
2833 }
2834
2835 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002836 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2837 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2838 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2839 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2840 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002841 compile_node(comp, pns->nodes[0]); // a doc string
2842 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002843 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002844 }
2845 }
2846 }
2847}
2848
2849void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2850 comp->pass = pass;
2851 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002852 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002853 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002854
2855 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002856 // reset maximum stack sizes in scope
2857 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002858 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002859 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002860 }
2861
Damien5ac1b2e2013-10-18 19:58:12 +01002862#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002863 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002864 scope_print_info(scope);
2865 }
Damien5ac1b2e2013-10-18 19:58:12 +01002866#endif
Damien429d7192013-10-04 19:53:11 +01002867
2868 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002869 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2870 assert(scope->kind == SCOPE_MODULE);
2871 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2872 compile_node(comp, pns->nodes[0]); // compile the expression
2873 EMIT(return_value);
2874 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002875 if (!comp->is_repl) {
2876 check_for_doc_string(comp, scope->pn);
2877 }
Damien429d7192013-10-04 19:53:11 +01002878 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002879 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002880 EMIT(return_value);
2881 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002882 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2883 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2884 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002885
2886 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002887 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002888 if (comp->pass == PASS_1) {
2889 comp->have_bare_star = false;
2890 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2891 }
2892
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02002893 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01002894
2895 compile_node(comp, pns->nodes[3]); // 3 is function body
2896 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002897 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002898 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002899 EMIT(return_value);
2900 }
2901 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002902 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2903 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2904 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002905
2906 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002907 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002908 if (comp->pass == PASS_1) {
2909 comp->have_bare_star = false;
2910 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2911 }
2912
2913 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2914 EMIT(return_value);
2915 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2916 // a bit of a hack at the moment
2917
Damiend99b0522013-12-21 18:17:45 +00002918 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2919 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2920 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2921 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2922 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002923
Damien George55baff42014-01-21 21:40:13 +00002924 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01002925 if (comp->pass == PASS_1) {
2926 bool added;
2927 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2928 assert(added);
2929 id_info->kind = ID_INFO_KIND_LOCAL;
2930 scope->num_params = 1;
2931 }
2932
2933 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002934 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01002935 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002936 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01002937 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002938 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01002939 }
2940
Damienb05d7072013-10-05 13:37:10 +01002941 int l_end = comp_next_label(comp);
2942 int l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002943 EMIT_ARG(load_id, qstr_arg);
2944 EMIT_ARG(label_assign, l_top);
2945 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01002946 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2947 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00002948 EMIT_ARG(jump, l_top);
2949 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002950 EMIT(for_iter_end);
2951
2952 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00002953 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002954 }
2955 EMIT(return_value);
2956 } else {
2957 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002958 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2959 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2960 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01002961
2962 if (comp->pass == PASS_1) {
2963 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002964 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01002965 assert(added);
2966 id_info->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +01002967 }
2968
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
Damien George094d4502014-04-02 17:31:27 +01002997#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01002998void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2999 comp->pass = pass;
3000 comp->scope_cur = scope;
3001 comp->next_label = 1;
3002
3003 if (scope->kind != SCOPE_FUNCTION) {
3004 printf("Error: inline assembler must be a function\n");
3005 return;
3006 }
3007
Damiena2f2f7d2013-10-06 00:14:13 +01003008 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003009 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003010 }
3011
Damien826005c2013-10-05 23:17:28 +01003012 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003013 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3014 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3015 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003016
Damiend99b0522013-12-21 18:17:45 +00003017 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003018
Damiena2f2f7d2013-10-06 00:14:13 +01003019 // parameters are in pns->nodes[1]
3020 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00003021 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003022 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00003023 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003024 }
3025
Damiend99b0522013-12-21 18:17:45 +00003026 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003027
Damiend99b0522013-12-21 18:17:45 +00003028 mp_parse_node_t pn_body = pns->nodes[3]; // body
3029 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003030 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3031
Damien Georgecbd2f742014-01-19 11:48:48 +00003032 /*
Damien826005c2013-10-05 23:17:28 +01003033 if (comp->pass == PASS_3) {
3034 //printf("----\n");
3035 scope_print_info(scope);
3036 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003037 */
Damien826005c2013-10-05 23:17:28 +01003038
3039 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003040 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3041 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
3042 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
3043 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3044 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3045 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3046 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3047 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3048 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3049 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3050 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3051 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3052 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003053 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3054
3055 // emit instructions
3056 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00003057 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgef41fdd02014-03-03 23:19:11 +00003058 compile_syntax_error(comp, "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003059 return;
3060 }
3061 int lab = comp_next_label(comp);
3062 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003063 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003064 }
3065 } else {
3066 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003067 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003068 }
3069 }
3070 }
3071
3072 if (comp->pass > PASS_1) {
3073 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01003074 }
Damien429d7192013-10-04 19:53:11 +01003075}
Damien George094d4502014-04-02 17:31:27 +01003076#endif
Damien429d7192013-10-04 19:53:11 +01003077
3078void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3079 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003080 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003081 scope->num_locals = 0;
3082 for (int i = 0; i < scope->id_info_len; i++) {
3083 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003084 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003085 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3086 continue;
3087 }
3088 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3089 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3090 }
Damien9ecbcff2013-12-11 00:41:43 +00003091 // note: params always count for 1 local, even if they are a cell
Damien429d7192013-10-04 19:53:11 +01003092 if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
3093 id->local_num = scope->num_locals;
3094 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003095 }
3096 }
3097
3098 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003099#if MICROPY_EMIT_CPYTHON
3100 int num_cell = 0;
3101#endif
Damien9ecbcff2013-12-11 00:41:43 +00003102 for (int i = 0; i < scope->id_info_len; i++) {
3103 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003104#if MICROPY_EMIT_CPYTHON
3105 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003106 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003107 id->local_num = num_cell;
3108 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003109 }
Damien George6baf76e2013-12-30 22:32:17 +00003110#else
3111 // in Micro Python the cells come right after the fast locals
3112 // parameters are not counted here, since they remain at the start
3113 // of the locals, even if they are cell vars
3114 if (!id->param && id->kind == ID_INFO_KIND_CELL) {
3115 id->local_num = scope->num_locals;
3116 scope->num_locals += 1;
3117 }
3118#endif
Damien9ecbcff2013-12-11 00:41:43 +00003119 }
Damien9ecbcff2013-12-11 00:41:43 +00003120
3121 // compute the index of free vars (freevars[idx] in CPython)
3122 // make sure they are in the order of the parent scope
3123 if (scope->parent != NULL) {
3124 int num_free = 0;
3125 for (int i = 0; i < scope->parent->id_info_len; i++) {
3126 id_info_t *id = &scope->parent->id_info[i];
3127 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3128 for (int j = 0; j < scope->id_info_len; j++) {
3129 id_info_t *id2 = &scope->id_info[j];
3130 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +00003131 assert(!id2->param); // free vars should not be params
3132#if MICROPY_EMIT_CPYTHON
3133 // in CPython the frees are numbered after the cells
3134 id2->local_num = num_cell + num_free;
3135#else
3136 // in Micro Python the frees come first, before the params
3137 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003138#endif
3139 num_free += 1;
3140 }
3141 }
3142 }
Damien429d7192013-10-04 19:53:11 +01003143 }
Damien George6baf76e2013-12-30 22:32:17 +00003144#if !MICROPY_EMIT_CPYTHON
3145 // in Micro Python shift all other locals after the free locals
3146 if (num_free > 0) {
3147 for (int i = 0; i < scope->id_info_len; i++) {
3148 id_info_t *id = &scope->id_info[i];
3149 if (id->param || id->kind != ID_INFO_KIND_FREE) {
3150 id->local_num += num_free;
3151 }
3152 }
3153 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3154 scope->num_locals += num_free;
3155 }
3156#endif
Damien429d7192013-10-04 19:53:11 +01003157 }
3158
Damien George8725f8f2014-02-15 19:33:11 +00003159 // compute scope_flags
Damien George882b3632014-04-02 15:56:31 +01003160
3161#if MICROPY_EMIT_CPYTHON
3162 // these flags computed here are for CPython compatibility only
3163 if (scope->kind == SCOPE_FUNCTION) {
Damien George8725f8f2014-02-15 19:33:11 +00003164 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien429d7192013-10-04 19:53:11 +01003165 }
3166 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) {
3167 assert(scope->parent != NULL);
Damien George8725f8f2014-02-15 19:33:11 +00003168 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien429d7192013-10-04 19:53:11 +01003169
3170 // TODO possibly other ways it can be nested
Damien George08d07552014-01-29 18:58:52 +00003171 // Note that we don't actually use this information at the moment (for CPython compat only)
3172 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 +00003173 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003174 }
3175 }
Damien George882b3632014-04-02 15:56:31 +01003176#endif
3177
Damien429d7192013-10-04 19:53:11 +01003178 int num_free = 0;
3179 for (int i = 0; i < scope->id_info_len; i++) {
3180 id_info_t *id = &scope->id_info[i];
3181 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3182 num_free += 1;
3183 }
3184 }
3185 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003186 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003187 }
3188}
3189
Damien George08335002014-01-18 23:24:36 +00003190mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
Damien429d7192013-10-04 19:53:11 +01003191 compiler_t *comp = m_new(compiler_t, 1);
3192
Damien Georgecbd2f742014-01-19 11:48:48 +00003193 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003194 comp->is_repl = is_repl;
3195 comp->had_error = false;
3196
Damien429d7192013-10-04 19:53:11 +01003197 comp->break_label = 0;
3198 comp->continue_label = 0;
Damien Georgecbddb272014-02-01 20:08:18 +00003199 comp->break_continue_except_level = 0;
3200 comp->cur_except_level = 0;
3201
Damien George35e2a4e2014-02-05 00:51:47 +00003202 comp->func_arg_is_super = false;
3203
Damien429d7192013-10-04 19:53:11 +01003204 comp->scope_head = NULL;
3205 comp->scope_cur = NULL;
3206
Damien826005c2013-10-05 23:17:28 +01003207 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003208 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003209
3210 // set the outer scope
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003211 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE);
Damien429d7192013-10-04 19:53:11 +01003212
Damien826005c2013-10-05 23:17:28 +01003213 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003214 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003215 comp->emit_method_table = &emit_pass1_method_table;
3216 comp->emit_inline_asm = NULL;
3217 comp->emit_inline_asm_method_table = NULL;
3218 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003219 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003220 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003221#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003222 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003223 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003224#endif
Damien826005c2013-10-05 23:17:28 +01003225 } else {
3226 compile_scope(comp, s, PASS_1);
3227 }
3228
3229 // update maximim number of labels needed
3230 if (comp->next_label > max_num_labels) {
3231 max_num_labels = comp->next_label;
3232 }
Damien429d7192013-10-04 19:53:11 +01003233 }
3234
Damien826005c2013-10-05 23:17:28 +01003235 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003236 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003237 compile_scope_compute_things(comp, s);
3238 }
3239
Damien826005c2013-10-05 23:17:28 +01003240 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003241 emit_pass1_free(comp->emit);
3242
Damien826005c2013-10-05 23:17:28 +01003243 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003244#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003245 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003246#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003247 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003248#endif
Damien3ef4abb2013-10-12 16:53:13 +01003249#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003250 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003251#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003252#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003253 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003254 if (false) {
3255 // dummy
3256
Damien3ef4abb2013-10-12 16:53:13 +01003257#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003258 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
3259 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003260 if (emit_inline_thumb == NULL) {
3261 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3262 }
3263 comp->emit = NULL;
3264 comp->emit_method_table = NULL;
3265 comp->emit_inline_asm = emit_inline_thumb;
3266 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3267 compile_scope_inline_asm(comp, s, PASS_2);
3268 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003269#endif
3270
Damien826005c2013-10-05 23:17:28 +01003271 } else {
Damienc025ebb2013-10-12 14:30:21 +01003272
3273 // choose the emit type
3274
Damien3ef4abb2013-10-12 16:53:13 +01003275#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003276 comp->emit = emit_cpython_new(max_num_labels);
3277 comp->emit_method_table = &emit_cpython_method_table;
3278#else
Damien826005c2013-10-05 23:17:28 +01003279 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003280
3281#if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003282 case EMIT_OPT_NATIVE_PYTHON:
Damien3410be82013-10-07 23:09:10 +01003283 case EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003284#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003285 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003286 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003287 }
Damien13ed3a62013-10-08 09:05:10 +01003288 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003289#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003290 if (emit_native == NULL) {
3291 emit_native = emit_native_thumb_new(max_num_labels);
3292 }
3293 comp->emit_method_table = &emit_native_thumb_method_table;
3294#endif
3295 comp->emit = emit_native;
Damien3410be82013-10-07 23:09:10 +01003296 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003297 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003298#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003299
Damien826005c2013-10-05 23:17:28 +01003300 default:
3301 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003302 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003303 }
3304 comp->emit = emit_bc;
3305 comp->emit_method_table = &emit_bc_method_table;
3306 break;
3307 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003308#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003309
3310 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003311 compile_scope(comp, s, PASS_2);
3312 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003313 }
Damien429d7192013-10-04 19:53:11 +01003314 }
3315
Damien George41d02b62014-01-24 22:42:28 +00003316 // free the emitters
3317#if !MICROPY_EMIT_CPYTHON
3318 if (emit_bc != NULL) {
3319 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003320 }
Damien George41d02b62014-01-24 22:42:28 +00003321#if MICROPY_EMIT_NATIVE
3322 if (emit_native != NULL) {
3323#if MICROPY_EMIT_X64
3324 emit_native_x64_free(emit_native);
3325#elif MICROPY_EMIT_THUMB
3326 emit_native_thumb_free(emit_native);
3327#endif
3328 }
3329#endif
3330#if MICROPY_EMIT_INLINE_THUMB
3331 if (emit_inline_thumb != NULL) {
3332 emit_inline_thumb_free(emit_inline_thumb);
3333 }
3334#endif
3335#endif // !MICROPY_EMIT_CPYTHON
3336
3337 // free the scopes
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003338 uint unique_code_id = module_scope->unique_code_id;
3339 for (scope_t *s = module_scope; s;) {
3340 scope_t *next = s->next;
3341 scope_free(s);
3342 s = next;
3343 }
Damien5ac1b2e2013-10-18 19:58:12 +01003344
Damien George41d02b62014-01-24 22:42:28 +00003345 // free the compiler
3346 bool had_error = comp->had_error;
3347 m_del_obj(compiler_t, comp);
3348
Damien George1fb03172014-01-03 14:22:03 +00003349 if (had_error) {
3350 // TODO return a proper error message
3351 return mp_const_none;
3352 } else {
3353#if MICROPY_EMIT_CPYTHON
3354 // can't create code, so just return true
Damien George41d02b62014-01-24 22:42:28 +00003355 (void)unique_code_id; // to suppress warning that unique_code_id is unused
Damien George1fb03172014-01-03 14:22:03 +00003356 return mp_const_true;
3357#else
3358 // return function that executes the outer module
Damien Georged1e443d2014-03-29 11:39:36 +00003359 // 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 +01003360 return mp_make_function_from_id(unique_code_id, true, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003361#endif
3362 }
Damien429d7192013-10-04 19:53:11 +01003363}