blob: b32bc7e69c2ee7b12f8daa0689236052cd6781a9 [file] [log] [blame]
xbeefe34222014-03-16 00:14:26 -07001#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +01002#include <stdint.h>
3#include <stdio.h>
4#include <string.h>
5#include <assert.h>
Rachel Dowdall56402792014-03-22 20:19:24 +00006#include <math.h>
Damien429d7192013-10-04 19:53:11 +01007
8#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00009#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000010#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010011#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010012#include "parse.h"
13#include "scope.h"
Damiend99b0522013-12-21 18:17:45 +000014#include "runtime0.h"
Damien429d7192013-10-04 19:53:11 +010015#include "emit.h"
Damien George2326d522014-03-27 23:26:35 +000016#include "emitglue.h"
Damien George1fb03172014-01-03 14:22:03 +000017#include "obj.h"
18#include "compile.h"
19#include "runtime.h"
Rachel Dowdallcde86312014-03-22 17:29:27 +000020#include "intdivmod.h"
Damien429d7192013-10-04 19:53:11 +010021
22// TODO need to mangle __attr names
23
Damience89a212013-10-15 22:25:17 +010024#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_THUMB)
25
Damien429d7192013-10-04 19:53:11 +010026typedef enum {
27 PN_none = 0,
Damien George00208ce2014-01-23 00:00:53 +000028#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien429d7192013-10-04 19:53:11 +010029#include "grammar.h"
30#undef DEF_RULE
31 PN_maximum_number_of,
32} pn_kind_t;
33
Damien Georgeb9791222014-01-23 00:34:21 +000034#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
35#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
36#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
37#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010038
Damien6cdd3af2013-10-05 18:08:26 +010039#define EMIT_OPT_NONE (0)
40#define EMIT_OPT_BYTE_CODE (1)
41#define EMIT_OPT_NATIVE_PYTHON (2)
Damien7af3d192013-10-07 00:02:49 +010042#define EMIT_OPT_VIPER (3)
43#define EMIT_OPT_ASM_THUMB (4)
Damien6cdd3af2013-10-05 18:08:26 +010044
Damien429d7192013-10-04 19:53:11 +010045typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000046 qstr source_file;
Damien5ac1b2e2013-10-18 19:58:12 +010047 bool is_repl;
Damien429d7192013-10-04 19:53:11 +010048 pass_kind_t pass;
Damien5ac1b2e2013-10-18 19:58:12 +010049 bool had_error; // try to keep compiler clean from nlr
Damien429d7192013-10-04 19:53:11 +010050
Damienb05d7072013-10-05 13:37:10 +010051 int next_label;
Damienb05d7072013-10-05 13:37:10 +010052
Damien429d7192013-10-04 19:53:11 +010053 int break_label;
54 int continue_label;
Damien Georgecbddb272014-02-01 20:08:18 +000055 int break_continue_except_level;
56 int cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien429d7192013-10-04 19:53:11 +010057
58 int n_arg_keyword;
59 bool have_star_arg;
60 bool have_dbl_star_arg;
61 bool have_bare_star;
62 int param_pass;
63 int param_pass_num_dict_params;
64 int param_pass_num_default_params;
65
Damien George35e2a4e2014-02-05 00:51:47 +000066 bool func_arg_is_super; // used to compile special case of super() function call
67
Damien429d7192013-10-04 19:53:11 +010068 scope_t *scope_head;
69 scope_t *scope_cur;
70
Damien6cdd3af2013-10-05 18:08:26 +010071 emit_t *emit; // current emitter
72 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010073
74 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
75 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien429d7192013-10-04 19:53:11 +010076} compiler_t;
77
Damien Georgef41fdd02014-03-03 23:19:11 +000078STATIC void compile_syntax_error(compiler_t *comp, const char *msg) {
79 // TODO store the error message to a variable in compiler_t instead of printing it
80 printf("SyntaxError: %s\n", msg);
81 comp->had_error = true;
82}
83
Damiend99b0522013-12-21 18:17:45 +000084mp_parse_node_t fold_constants(mp_parse_node_t pn) {
85 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
86 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
87 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +010088
89 // fold arguments first
90 for (int i = 0; i < n; i++) {
91 pns->nodes[i] = fold_constants(pns->nodes[i]);
92 }
93
Damiend99b0522013-12-21 18:17:45 +000094 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +010095 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +000096 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +020097 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
98 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +000099 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien3ef4abb2013-10-12 16:53:13 +0100100#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100101 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000102 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
Damien0efb3a12013-10-12 16:16:56 +0100103#endif
Damiend99b0522013-12-21 18:17:45 +0000104 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
105 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100106 } else {
107 // shouldn't happen
108 assert(0);
109 }
110 }
111 break;
112
113 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100114 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000115 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200116 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
117 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damien0efb3a12013-10-12 16:16:56 +0100118 machine_int_t res;
Damiend99b0522013-12-21 18:17:45 +0000119 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100120 res = arg0 + arg1;
Damiend99b0522013-12-21 18:17:45 +0000121 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100122 res = arg0 - arg1;
Damien429d7192013-10-04 19:53:11 +0100123 } else {
124 // shouldn't happen
125 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100126 res = 0;
127 }
Paul Sokolovskybbf0e2f2014-02-21 02:04:32 +0200128 if (MP_PARSE_FITS_SMALL_INT(res)) {
Damiend99b0522013-12-21 18:17:45 +0000129 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, res);
Damien429d7192013-10-04 19:53:11 +0100130 }
131 }
132 break;
133
134 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000135 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200136 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
137 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000138 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien3ef4abb2013-10-12 16:53:13 +0100139#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100140 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000141 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 * arg1);
Damien0efb3a12013-10-12 16:16:56 +0100142#endif
Damiend99b0522013-12-21 18:17:45 +0000143 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien429d7192013-10-04 19:53:11 +0100144 ; // pass
Damiend99b0522013-12-21 18:17:45 +0000145 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Rachel Dowdallcde86312014-03-22 17:29:27 +0000146 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, python_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000147 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Damien George8dcc0c72014-03-27 10:55:21 +0000148 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, python_floor_divide(arg0, arg1));
Damien429d7192013-10-04 19:53:11 +0100149 } else {
150 // shouldn't happen
151 assert(0);
152 }
153 }
154 break;
155
156 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000157 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200158 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000159 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
160 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
161 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
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_TILDE)) {
164 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100165 } else {
166 // shouldn't happen
167 assert(0);
168 }
169 }
170 break;
171
Damien3ef4abb2013-10-12 16:53:13 +0100172#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +0100173 case PN_power:
Damien0efb3a12013-10-12 16:16:56 +0100174 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000175 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])) {
176 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
177 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200178 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100179 if (power >= 0) {
180 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200181 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100182 for (; power > 0; power--) {
183 ans *= base;
184 }
Damiend99b0522013-12-21 18:17:45 +0000185 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100186 }
187 }
188 }
189 break;
Damien0efb3a12013-10-12 16:16:56 +0100190#endif
Damien429d7192013-10-04 19:53:11 +0100191 }
192 }
193
194 return pn;
195}
196
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200197STATIC 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 +0000198STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100199
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200200STATIC int comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100201 return comp->next_label++;
202}
203
Damien George8dcc0c72014-03-27 10:55:21 +0000204STATIC void compile_increase_except_level(compiler_t *comp) {
205 comp->cur_except_level += 1;
206 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
207 comp->scope_cur->exc_stack_size = comp->cur_except_level;
208 }
209}
210
211STATIC void compile_decrease_except_level(compiler_t *comp) {
212 assert(comp->cur_except_level > 0);
213 comp->cur_except_level -= 1;
214}
215
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200216STATIC 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 +0000217 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 +0100218 scope->parent = comp->scope_cur;
219 scope->next = NULL;
220 if (comp->scope_head == NULL) {
221 comp->scope_head = scope;
222 } else {
223 scope_t *s = comp->scope_head;
224 while (s->next != NULL) {
225 s = s->next;
226 }
227 s->next = scope;
228 }
229 return scope;
230}
231
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200232STATIC int list_len(mp_parse_node_t pn, int pn_kind) {
Damiend99b0522013-12-21 18:17:45 +0000233 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100234 return 0;
Damiend99b0522013-12-21 18:17:45 +0000235 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100236 return 1;
237 } else {
Damiend99b0522013-12-21 18:17:45 +0000238 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
239 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100240 return 1;
241 } else {
Damiend99b0522013-12-21 18:17:45 +0000242 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100243 }
244 }
245}
246
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200247STATIC 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 +0000248 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
249 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
250 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100251 for (int i = 0; i < num_nodes; i++) {
252 f(comp, pns->nodes[i]);
253 }
Damiend99b0522013-12-21 18:17:45 +0000254 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100255 f(comp, pn);
256 }
257}
258
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200259STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000260 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100261 *nodes = NULL;
262 return 0;
Damiend99b0522013-12-21 18:17:45 +0000263 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100264 *nodes = pn;
265 return 1;
266 } else {
Damiend99b0522013-12-21 18:17:45 +0000267 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
268 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100269 *nodes = pn;
270 return 1;
271 } else {
272 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000273 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100274 }
275 }
276}
277
Damiend99b0522013-12-21 18:17:45 +0000278void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100279}
280
Damiend99b0522013-12-21 18:17:45 +0000281void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
282 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100283 for (int i = 0; i < num_nodes; i++) {
284 compile_node(comp, pns->nodes[i]);
285 }
286}
287
Damien3ef4abb2013-10-12 16:53:13 +0100288#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200289STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000290 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100291 return false;
292 }
Damiend99b0522013-12-21 18:17:45 +0000293 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100294 return false;
295 }
296 return true;
297}
298
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200299STATIC void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
Damien George55baff42014-01-21 21:40:13 +0000300 uint len;
301 const byte *str = qstr_data(qstr, &len);
Damien02f89412013-12-12 15:13:36 +0000302 bool has_single_quote = false;
303 bool has_double_quote = false;
304 for (int i = 0; i < len; i++) {
305 if (str[i] == '\'') {
306 has_single_quote = true;
307 } else if (str[i] == '"') {
308 has_double_quote = true;
309 }
310 }
311 if (bytes) {
312 vstr_printf(vstr, "b");
313 }
314 bool quote_single = false;
315 if (has_single_quote && !has_double_quote) {
316 vstr_printf(vstr, "\"");
317 } else {
318 quote_single = true;
319 vstr_printf(vstr, "'");
320 }
321 for (int i = 0; i < len; i++) {
322 if (str[i] == '\n') {
323 vstr_printf(vstr, "\\n");
324 } else if (str[i] == '\\') {
325 vstr_printf(vstr, "\\\\");
326 } else if (str[i] == '\'' && quote_single) {
327 vstr_printf(vstr, "\\'");
328 } else {
329 vstr_printf(vstr, "%c", str[i]);
330 }
331 }
332 if (has_single_quote && !has_double_quote) {
333 vstr_printf(vstr, "\"");
334 } else {
335 vstr_printf(vstr, "'");
336 }
337}
338
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200339STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damiend99b0522013-12-21 18:17:45 +0000340 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200341 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
342 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
343 return;
344 }
345
Damiend99b0522013-12-21 18:17:45 +0000346 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
347 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
348 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000349 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
350 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
351 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
352 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
353 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100354 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000355 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
356 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
357 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien429d7192013-10-04 19:53:11 +0100358 default: assert(0);
359 }
360 break;
361 default: assert(0);
362 }
363}
364
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200365STATIC 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 +0100366 int n = 0;
367 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000368 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100369 }
370 int total = n;
371 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000372 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100373 total += 1;
Damien3a205172013-10-12 15:01:56 +0100374 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100375 is_const = false;
376 }
377 }
378 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100379 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100380 is_const = false;
381 break;
382 }
383 }
384 if (total > 0 && is_const) {
385 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000386 vstr_t *vstr = vstr_new();
387 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000388 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000389 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100390 need_comma = true;
391 }
392 for (int i = 0; i < n; i++) {
393 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000394 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100395 }
Damien02f89412013-12-12 15:13:36 +0000396 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100397 need_comma = true;
398 }
399 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000400 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100401 } else {
Damien02f89412013-12-12 15:13:36 +0000402 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100403 }
Damien Georgeb9791222014-01-23 00:34:21 +0000404 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000405 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100406 } else {
Damiend99b0522013-12-21 18:17:45 +0000407 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100408 compile_node(comp, pn);
409 }
410 for (int i = 0; i < n; i++) {
411 compile_node(comp, pns_list->nodes[i]);
412 }
Damien Georgeb9791222014-01-23 00:34:21 +0000413 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100414 }
415}
Damien3a205172013-10-12 15:01:56 +0100416#endif
417
418// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damiend99b0522013-12-21 18:17:45 +0000419void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100420#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100421 cpython_c_tuple(comp, pn, pns_list);
422#else
423 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000424 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100425 compile_node(comp, pn);
426 total += 1;
427 }
428 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000429 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100430 for (int i = 0; i < n; i++) {
431 compile_node(comp, pns_list->nodes[i]);
432 }
433 total += n;
434 }
Damien Georgeb9791222014-01-23 00:34:21 +0000435 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100436#endif
437}
Damien429d7192013-10-04 19:53:11 +0100438
Damiend99b0522013-12-21 18:17:45 +0000439void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100440 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000441 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100442}
443
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200444STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000445 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200446 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100447}
448
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200449STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200450 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 +0100451}
452
Damien3ef4abb2013-10-12 16:53:13 +0100453#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100454// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200455STATIC 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 +0100456 if (node_is_const_false(pn)) {
457 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000458 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100459 }
460 return;
461 } else if (node_is_const_true(pn)) {
462 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000463 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100464 }
465 return;
Damiend99b0522013-12-21 18:17:45 +0000466 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
467 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
468 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
469 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100470 if (jump_if == false) {
Damienb05d7072013-10-05 13:37:10 +0100471 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100472 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100473 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100474 }
Damien3a205172013-10-12 15:01:56 +0100475 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000476 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100477 } else {
478 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100479 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100480 }
481 }
482 return;
Damiend99b0522013-12-21 18:17:45 +0000483 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100484 if (jump_if == false) {
485 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100486 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100487 }
488 } else {
Damienb05d7072013-10-05 13:37:10 +0100489 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100490 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100491 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100492 }
Damien3a205172013-10-12 15:01:56 +0100493 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000494 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100495 }
496 return;
Damiend99b0522013-12-21 18:17:45 +0000497 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100498 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100499 return;
500 }
501 }
502
503 // nothing special, fall back to default compiling for node and jump
504 compile_node(comp, pn);
505 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000506 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100507 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000508 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100509 }
510}
Damien3a205172013-10-12 15:01:56 +0100511#endif
Damien429d7192013-10-04 19:53:11 +0100512
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200513STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100514#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100515 cpython_c_if_cond(comp, pn, jump_if, label, false);
516#else
517 if (node_is_const_false(pn)) {
518 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000519 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100520 }
521 return;
522 } else if (node_is_const_true(pn)) {
523 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000524 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100525 }
526 return;
Damiend99b0522013-12-21 18:17:45 +0000527 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
528 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
529 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
530 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100531 if (jump_if == false) {
532 int label2 = comp_next_label(comp);
533 for (int i = 0; i < n - 1; i++) {
534 c_if_cond(comp, pns->nodes[i], true, label2);
535 }
536 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000537 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100538 } else {
539 for (int i = 0; i < n; i++) {
540 c_if_cond(comp, pns->nodes[i], true, label);
541 }
542 }
543 return;
Damiend99b0522013-12-21 18:17:45 +0000544 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100545 if (jump_if == false) {
546 for (int i = 0; i < n; i++) {
547 c_if_cond(comp, pns->nodes[i], false, label);
548 }
549 } else {
550 int label2 = comp_next_label(comp);
551 for (int i = 0; i < n - 1; i++) {
552 c_if_cond(comp, pns->nodes[i], false, label2);
553 }
554 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000555 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100556 }
557 return;
Damiend99b0522013-12-21 18:17:45 +0000558 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100559 c_if_cond(comp, pns->nodes[0], !jump_if, label);
560 return;
561 }
562 }
563
564 // nothing special, fall back to default compiling for node and jump
565 compile_node(comp, pn);
566 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000567 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100568 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000569 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100570 }
571#endif
Damien429d7192013-10-04 19:53:11 +0100572}
573
574typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000575void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100576
Damiend99b0522013-12-21 18:17:45 +0000577void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100578 if (assign_kind != ASSIGN_AUG_STORE) {
579 compile_node(comp, pns->nodes[0]);
580 }
581
Damiend99b0522013-12-21 18:17:45 +0000582 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
583 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
584 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
585 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100586 if (assign_kind != ASSIGN_AUG_STORE) {
587 for (int i = 0; i < n - 1; i++) {
588 compile_node(comp, pns1->nodes[i]);
589 }
590 }
Damiend99b0522013-12-21 18:17:45 +0000591 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
592 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100593 }
Damiend99b0522013-12-21 18:17:45 +0000594 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000595 compile_syntax_error(comp, "can't assign to function call");
Damien429d7192013-10-04 19:53:11 +0100596 return;
Damiend99b0522013-12-21 18:17:45 +0000597 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100598 if (assign_kind == ASSIGN_AUG_STORE) {
599 EMIT(rot_three);
600 EMIT(store_subscr);
601 } else {
602 compile_node(comp, pns1->nodes[0]);
603 if (assign_kind == ASSIGN_AUG_LOAD) {
604 EMIT(dup_top_two);
Damien Georged17926d2014-03-30 13:35:08 +0100605 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100606 } else {
607 EMIT(store_subscr);
608 }
609 }
Damiend99b0522013-12-21 18:17:45 +0000610 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
611 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100612 if (assign_kind == ASSIGN_AUG_LOAD) {
613 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000614 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100615 } else {
616 if (assign_kind == ASSIGN_AUG_STORE) {
617 EMIT(rot_two);
618 }
Damien Georgeb9791222014-01-23 00:34:21 +0000619 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100620 }
621 } else {
622 // shouldn't happen
623 assert(0);
624 }
625 } else {
626 // shouldn't happen
627 assert(0);
628 }
629
Damiend99b0522013-12-21 18:17:45 +0000630 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +0100631 // SyntaxError, cannot assign
632 assert(0);
633 }
634}
635
Damiend99b0522013-12-21 18:17:45 +0000636void c_assign_tuple(compiler_t *comp, int n, mp_parse_node_t *nodes) {
Damien429d7192013-10-04 19:53:11 +0100637 assert(n >= 0);
638 int have_star_index = -1;
639 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000640 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100641 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000642 EMIT_ARG(unpack_ex, i, n - i - 1);
Damien429d7192013-10-04 19:53:11 +0100643 have_star_index = i;
644 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000645 compile_syntax_error(comp, "two starred expressions in assignment");
Damien429d7192013-10-04 19:53:11 +0100646 return;
647 }
648 }
649 }
650 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000651 EMIT_ARG(unpack_sequence, n);
Damien429d7192013-10-04 19:53:11 +0100652 }
653 for (int i = 0; i < n; i++) {
654 if (i == have_star_index) {
Damiend99b0522013-12-21 18:17:45 +0000655 c_assign(comp, ((mp_parse_node_struct_t*)nodes[i])->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100656 } else {
657 c_assign(comp, nodes[i], ASSIGN_STORE);
658 }
659 }
660}
661
662// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000663void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100664 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000665 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100666 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000667 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
668 if (MP_PARSE_NODE_IS_ID(pn)) {
669 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100670 switch (assign_kind) {
671 case ASSIGN_STORE:
672 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000673 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100674 break;
675 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000676 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100677 break;
678 }
679 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000680 compile_syntax_error(comp, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100681 return;
682 }
683 } else {
Damiend99b0522013-12-21 18:17:45 +0000684 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
685 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100686 case PN_power:
687 // lhs is an index or attribute
688 c_assign_power(comp, pns, assign_kind);
689 break;
690
691 case PN_testlist_star_expr:
692 case PN_exprlist:
693 // lhs is a tuple
694 if (assign_kind != ASSIGN_STORE) {
695 goto bad_aug;
696 }
Damiend99b0522013-12-21 18:17:45 +0000697 c_assign_tuple(comp, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100698 break;
699
700 case PN_atom_paren:
701 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000702 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100703 // empty tuple
Damien Georgef41fdd02014-03-03 23:19:11 +0000704 compile_syntax_error(comp, "can't assign to ()");
Damien429d7192013-10-04 19:53:11 +0100705 return;
Damiend99b0522013-12-21 18:17:45 +0000706 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
707 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100708 goto testlist_comp;
709 } else {
710 // parenthesis around 1 item, is just that item
711 pn = pns->nodes[0];
712 goto tail_recursion;
713 }
714 break;
715
716 case PN_atom_bracket:
717 // lhs is something in brackets
718 if (assign_kind != ASSIGN_STORE) {
719 goto bad_aug;
720 }
Damiend99b0522013-12-21 18:17:45 +0000721 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100722 // empty list, assignment allowed
723 c_assign_tuple(comp, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000724 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
725 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100726 goto testlist_comp;
727 } else {
728 // brackets around 1 item
729 c_assign_tuple(comp, 1, &pns->nodes[0]);
730 }
731 break;
732
733 default:
Damiend99b0522013-12-21 18:17:45 +0000734 printf("unknown assign, %u\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien429d7192013-10-04 19:53:11 +0100735 assert(0);
736 }
737 return;
738
739 testlist_comp:
740 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000741 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
742 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
743 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100744 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000745 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100746 c_assign_tuple(comp, 1, &pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000747 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100748 // sequence of many items
749 // TODO call c_assign_tuple instead
Damiend99b0522013-12-21 18:17:45 +0000750 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
Damien Georgeb9791222014-01-23 00:34:21 +0000751 EMIT_ARG(unpack_sequence, 1 + n);
Damien429d7192013-10-04 19:53:11 +0100752 c_assign(comp, pns->nodes[0], ASSIGN_STORE);
753 for (int i = 0; i < n; i++) {
754 c_assign(comp, pns2->nodes[i], ASSIGN_STORE);
755 }
Damiend99b0522013-12-21 18:17:45 +0000756 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +0100757 // TODO not implemented
758 assert(0);
759 } else {
760 // sequence with 2 items
761 goto sequence_with_2_items;
762 }
763 } else {
764 // sequence with 2 items
765 sequence_with_2_items:
766 c_assign_tuple(comp, 2, pns->nodes);
767 }
768 return;
769 }
770 return;
771
772 bad_aug:
Damien Georgef41fdd02014-03-03 23:19:11 +0000773 compile_syntax_error(comp, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100774}
775
776// stuff for lambda and comprehensions and generators
777void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_dict_params, int n_default_params) {
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000778#if !MICROPY_EMIT_CPYTHON
779 // in Micro Python we put the default params into a tuple using the bytecode
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200780 if (n_default_params) {
781 EMIT_ARG(build_tuple, n_default_params);
782 }
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000783#endif
784
Damien429d7192013-10-04 19:53:11 +0100785 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000786 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100787 int nfree = 0;
788 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000789 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
790 id_info_t *id = &comp->scope_cur->id_info[i];
791 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
792 for (int j = 0; j < this_scope->id_info_len; j++) {
793 id_info_t *id2 = &this_scope->id_info[j];
794 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000795#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000796 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000797#else
798 // in Micro Python we load closures using LOAD_FAST
Damien Georgeb9791222014-01-23 00:34:21 +0000799 EMIT_ARG(load_fast, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000800#endif
Damien318aec62013-12-10 18:28:17 +0000801 nfree += 1;
802 }
803 }
Damien429d7192013-10-04 19:53:11 +0100804 }
805 }
806 }
Damien429d7192013-10-04 19:53:11 +0100807
808 // make the function/closure
809 if (nfree == 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000810 EMIT_ARG(make_function, this_scope, n_dict_params, n_default_params);
Damien429d7192013-10-04 19:53:11 +0100811 } else {
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000812 EMIT_ARG(build_tuple, nfree);
Damien Georgeb9791222014-01-23 00:34:21 +0000813 EMIT_ARG(make_closure, this_scope, n_dict_params, n_default_params);
Damien429d7192013-10-04 19:53:11 +0100814 }
815}
816
Damiend99b0522013-12-21 18:17:45 +0000817void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000818 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damiend99b0522013-12-21 18:17:45 +0000819 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
820 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100821 // bare star
822 comp->have_bare_star = true;
823 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000824
825 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
826 // TODO do we need to do anything with this?
827
828 } else {
829 mp_parse_node_t pn_id;
830 mp_parse_node_t pn_colon;
831 mp_parse_node_t pn_equal;
832 if (MP_PARSE_NODE_IS_ID(pn)) {
833 // this parameter is just an id
834
835 pn_id = pn;
836 pn_colon = MP_PARSE_NODE_NULL;
837 pn_equal = MP_PARSE_NODE_NULL;
838
839 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
840 // this parameter has a colon and/or equal specifier
841
842 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
843 pn_id = pns->nodes[0];
844 pn_colon = pns->nodes[1];
845 pn_equal = pns->nodes[2];
846
847 } else {
848 assert(0);
849 return;
850 }
851
852 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
853 // this parameter does not have a default value
854
855 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
856 if (!comp->have_bare_star && comp->param_pass_num_default_params != 0) {
857 compile_syntax_error(comp, "non-default argument follows default argument");
858 return;
859 }
860
861 } else {
862 // this parameter has a default value
863 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
864
865 if (comp->have_bare_star) {
866 comp->param_pass_num_dict_params += 1;
867 if (comp->param_pass == 1) {
868 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pn_id));
869 compile_node(comp, pn_equal);
870 }
871 } else {
872 comp->param_pass_num_default_params += 1;
873 if (comp->param_pass == 2) {
874 compile_node(comp, pn_equal);
875 }
876 }
877 }
878
879 // TODO pn_colon not implemented
880 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100881 }
882}
883
884// leaves function object on stack
885// returns function name
Damiend99b0522013-12-21 18:17:45 +0000886qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100887 if (comp->pass == PASS_1) {
888 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000889 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100890 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000891 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100892 }
893
894 // save variables (probably don't need to do this, since we can't have nested definitions..?)
895 bool old_have_bare_star = comp->have_bare_star;
896 int old_param_pass = comp->param_pass;
897 int old_param_pass_num_dict_params = comp->param_pass_num_dict_params;
898 int old_param_pass_num_default_params = comp->param_pass_num_default_params;
899
900 // compile default parameters
Damien Georgef41fdd02014-03-03 23:19:11 +0000901
902 // pass 1 does any default parameters after bare star
Damien429d7192013-10-04 19:53:11 +0100903 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000904 comp->param_pass = 1;
Damien429d7192013-10-04 19:53:11 +0100905 comp->param_pass_num_dict_params = 0;
906 comp->param_pass_num_default_params = 0;
907 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +0000908
909 if (comp->had_error) {
910 return MP_QSTR_NULL;
911 }
912
913 // pass 2 does any default parameters before bare star
Damien429d7192013-10-04 19:53:11 +0100914 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000915 comp->param_pass = 2;
Damien429d7192013-10-04 19:53:11 +0100916 comp->param_pass_num_dict_params = 0;
917 comp->param_pass_num_default_params = 0;
918 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
919
920 // get the scope for this function
921 scope_t *fscope = (scope_t*)pns->nodes[4];
922
923 // make the function
924 close_over_variables_etc(comp, fscope, comp->param_pass_num_dict_params, comp->param_pass_num_default_params);
925
926 // restore variables
927 comp->have_bare_star = old_have_bare_star;
928 comp->param_pass = old_param_pass;
929 comp->param_pass_num_dict_params = old_param_pass_num_dict_params;
930 comp->param_pass_num_default_params = old_param_pass_num_default_params;
931
932 // return its name (the 'f' in "def f(...):")
933 return fscope->simple_name;
934}
935
936// leaves class object on stack
937// returns class name
Damiend99b0522013-12-21 18:17:45 +0000938qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100939 if (comp->pass == PASS_1) {
940 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000941 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100942 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000943 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100944 }
945
946 EMIT(load_build_class);
947
948 // scope for this class
949 scope_t *cscope = (scope_t*)pns->nodes[3];
950
951 // compile the class
952 close_over_variables_etc(comp, cscope, 0, 0);
953
954 // get its name
Damien Georgeb9791222014-01-23 00:34:21 +0000955 EMIT_ARG(load_const_id, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100956
957 // nodes[1] has parent classes, if any
Damien Georgebbcd49a2014-02-06 20:30:16 +0000958 comp->func_arg_is_super = false;
959 compile_trailer_paren_helper(comp, pns->nodes[1], false, 2);
Damien429d7192013-10-04 19:53:11 +0100960
961 // return its name (the 'C' in class C(...):")
962 return cscope->simple_name;
963}
964
Damien6cdd3af2013-10-05 18:08:26 +0100965// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200966STATIC 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 +0000967 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100968 return false;
969 }
970
971 if (name_len != 2) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000972 compile_syntax_error(comp, "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100973 return true;
974 }
975
Damiend99b0522013-12-21 18:17:45 +0000976 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000977 if (attr == MP_QSTR_byte_code) {
Damien5ac1b2e2013-10-18 19:58:12 +0100978 *emit_options = EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +0100979#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000980 } else if (attr == MP_QSTR_native) {
Damien6cdd3af2013-10-05 18:08:26 +0100981 *emit_options = EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000982 } else if (attr == MP_QSTR_viper) {
Damien7af3d192013-10-07 00:02:49 +0100983 *emit_options = EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100984#endif
Damien3ef4abb2013-10-12 16:53:13 +0100985#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000986 } else if (attr == MP_QSTR_asm_thumb) {
Damien5bfb7592013-10-05 18:41:24 +0100987 *emit_options = EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +0100988#endif
Damien6cdd3af2013-10-05 18:08:26 +0100989 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000990 compile_syntax_error(comp, "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100991 }
992
993 return true;
994}
995
Damiend99b0522013-12-21 18:17:45 +0000996void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100997 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +0000998 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +0100999 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1000
Damien6cdd3af2013-10-05 18:08:26 +01001001 // inherit emit options for this function/class definition
1002 uint emit_options = comp->scope_cur->emit_options;
1003
1004 // compile each decorator
1005 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001006 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001007 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1008 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001009
1010 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001011 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001012 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1013
1014 // check for built-in decorators
1015 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1016 // this was a built-in
1017 num_built_in_decorators += 1;
1018
1019 } else {
1020 // not a built-in, compile normally
1021
1022 // compile the decorator function
1023 compile_node(comp, name_nodes[0]);
1024 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001025 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001026 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001027 }
1028
1029 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001030 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001031 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001032 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001033 compile_node(comp, pns_decorator->nodes[1]);
1034 }
Damien429d7192013-10-04 19:53:11 +01001035 }
1036 }
1037
1038 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001039 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001040 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001041 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001042 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001043 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001044 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001045 } else {
1046 // shouldn't happen
1047 assert(0);
1048 }
1049
1050 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001051 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001052 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01001053 }
1054
1055 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001056 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001057}
1058
Damiend99b0522013-12-21 18:17:45 +00001059void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001060 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001061 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001062 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001063}
1064
Damiend99b0522013-12-21 18:17:45 +00001065void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1066 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001067 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001068 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1069 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001070
1071 compile_node(comp, pns->nodes[0]); // base of the power node
1072
Damiend99b0522013-12-21 18:17:45 +00001073 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1074 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1075 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1076 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001077 for (int i = 0; i < n - 1; i++) {
1078 compile_node(comp, pns1->nodes[i]);
1079 }
Damiend99b0522013-12-21 18:17:45 +00001080 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1081 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001082 }
Damiend99b0522013-12-21 18:17:45 +00001083 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien429d7192013-10-04 19:53:11 +01001084 // SyntaxError: can't delete a function call
1085 assert(0);
Damiend99b0522013-12-21 18:17:45 +00001086 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001087 compile_node(comp, pns1->nodes[0]);
1088 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001089 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1090 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001091 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001092 } else {
1093 // shouldn't happen
1094 assert(0);
1095 }
1096 } else {
1097 // shouldn't happen
1098 assert(0);
1099 }
1100
Damiend99b0522013-12-21 18:17:45 +00001101 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001102 // SyntaxError, cannot delete
1103 assert(0);
1104 }
Damiend99b0522013-12-21 18:17:45 +00001105 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1106 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1107 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1108 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001109 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1110
Damiend99b0522013-12-21 18:17:45 +00001111 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1112 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1113 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001114 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001115 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001116 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001117 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001118 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001119 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001120 c_del_stmt(comp, pns->nodes[0]);
1121 for (int i = 0; i < n; i++) {
1122 c_del_stmt(comp, pns1->nodes[i]);
1123 }
Damiend99b0522013-12-21 18:17:45 +00001124 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001125 // TODO not implemented; can't del comprehension?
1126 assert(0);
1127 } else {
1128 // sequence with 2 items
1129 goto sequence_with_2_items;
1130 }
1131 } else {
1132 // sequence with 2 items
1133 sequence_with_2_items:
1134 c_del_stmt(comp, pns->nodes[0]);
1135 c_del_stmt(comp, pns->nodes[1]);
1136 }
1137 } else {
1138 // tuple with 1 element
1139 c_del_stmt(comp, pn);
1140 }
1141 } else {
1142 // not implemented
1143 assert(0);
1144 }
1145}
1146
Damiend99b0522013-12-21 18:17:45 +00001147void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001148 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1149}
1150
Damiend99b0522013-12-21 18:17:45 +00001151void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001152 if (comp->break_label == 0) {
Damien George2326d522014-03-27 23:26:35 +00001153 compile_syntax_error(comp, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001154 }
Damien Georgecbddb272014-02-01 20:08:18 +00001155 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001156}
1157
Damiend99b0522013-12-21 18:17:45 +00001158void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001159 if (comp->continue_label == 0) {
Damien George2326d522014-03-27 23:26:35 +00001160 compile_syntax_error(comp, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001161 }
Damien Georgecbddb272014-02-01 20:08:18 +00001162 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001163}
1164
Damiend99b0522013-12-21 18:17:45 +00001165void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001166 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001167 compile_syntax_error(comp, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001168 return;
1169 }
Damiend99b0522013-12-21 18:17:45 +00001170 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001171 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001172 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001173 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001174 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001175 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1176 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 +01001177
Damienb05d7072013-10-05 13:37:10 +01001178 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001179 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1180 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1181 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001182 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001183 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1184 } else {
1185 compile_node(comp, pns->nodes[0]);
1186 }
1187 EMIT(return_value);
1188}
1189
Damiend99b0522013-12-21 18:17:45 +00001190void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001191 compile_node(comp, pns->nodes[0]);
1192 EMIT(pop_top);
1193}
1194
Damiend99b0522013-12-21 18:17:45 +00001195void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1196 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001197 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001198 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001199 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001200 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001201 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001202 compile_node(comp, pns->nodes[0]);
1203 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001204 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001205 } else {
1206 // raise x
1207 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001208 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001209 }
1210}
1211
1212// q1 holds the base, q2 the full name
1213// eg a -> q1=q2=a
1214// a.b.c -> q1=a, q2=a.b.c
Damiend99b0522013-12-21 18:17:45 +00001215void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q1, qstr *q2) {
Damien429d7192013-10-04 19:53:11 +01001216 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001217 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1218 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001219 // a name of the form x as y; unwrap it
Damiend99b0522013-12-21 18:17:45 +00001220 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001221 pn = pns->nodes[0];
1222 is_as = true;
1223 }
Damiend99b0522013-12-21 18:17:45 +00001224 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001225 // just a simple name
Damiend99b0522013-12-21 18:17:45 +00001226 *q2 = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001227 if (!is_as) {
1228 *q1 = *q2;
1229 }
Damien Georgeb9791222014-01-23 00:34:21 +00001230 EMIT_ARG(import_name, *q2);
Damiend99b0522013-12-21 18:17:45 +00001231 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1232 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1233 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001234 // a name of the form a.b.c
1235 if (!is_as) {
Damiend99b0522013-12-21 18:17:45 +00001236 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001237 }
Damiend99b0522013-12-21 18:17:45 +00001238 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001239 int len = n - 1;
1240 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001241 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001242 }
Damien George55baff42014-01-21 21:40:13 +00001243 byte *q_ptr;
1244 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001245 for (int i = 0; i < n; i++) {
1246 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001247 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001248 }
Damien George55baff42014-01-21 21:40:13 +00001249 uint str_src_len;
1250 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001251 memcpy(str_dest, str_src, str_src_len);
1252 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001253 }
Damien George55baff42014-01-21 21:40:13 +00001254 *q2 = qstr_build_end(q_ptr);
Damien Georgeb9791222014-01-23 00:34:21 +00001255 EMIT_ARG(import_name, *q2);
Damien429d7192013-10-04 19:53:11 +01001256 if (is_as) {
1257 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001258 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001259 }
1260 }
1261 } else {
1262 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001263 // This covers relative imports starting with dot(s) like "from .foo import"
Paul Sokolovsky8d9cc2e2014-03-30 02:31:08 +02001264 compile_syntax_error(comp, "Relative imports not implemented");
Damien429d7192013-10-04 19:53:11 +01001265 assert(0);
1266 }
1267 } else {
1268 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001269 // This covers relative imports with dots only like "from .. import"
Paul Sokolovsky8d9cc2e2014-03-30 02:31:08 +02001270 compile_syntax_error(comp, "Relative imports not implemented");
Damien429d7192013-10-04 19:53:11 +01001271 assert(0);
1272 }
1273}
1274
Damiend99b0522013-12-21 18:17:45 +00001275void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgeb9791222014-01-23 00:34:21 +00001276 EMIT_ARG(load_const_small_int, 0); // ??
1277 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001278 qstr q1, q2;
1279 do_import_name(comp, pn, &q1, &q2);
Damien Georgeb9791222014-01-23 00:34:21 +00001280 EMIT_ARG(store_id, q1);
Damien429d7192013-10-04 19:53:11 +01001281}
1282
Damiend99b0522013-12-21 18:17:45 +00001283void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001284 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1285}
1286
Damiend99b0522013-12-21 18:17:45 +00001287void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
1288 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001289 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001290
1291 // build the "fromlist" tuple
1292#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001293 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001294#else
Damien Georgeb9791222014-01-23 00:34:21 +00001295 EMIT_ARG(load_const_str, QSTR_FROM_STR_STATIC("*"), false);
1296 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001297#endif
1298
1299 // do the import
Damien429d7192013-10-04 19:53:11 +01001300 qstr dummy_q, id1;
1301 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1302 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001303
Damien429d7192013-10-04 19:53:11 +01001304 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001305 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001306
1307 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001308 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001309 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001310#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001311 {
1312 vstr_t *vstr = vstr_new();
1313 vstr_printf(vstr, "(");
1314 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001315 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1316 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1317 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001318 if (i > 0) {
1319 vstr_printf(vstr, ", ");
1320 }
1321 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001322 uint len;
1323 const byte *str = qstr_data(id2, &len);
1324 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001325 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001326 }
Damien02f89412013-12-12 15:13:36 +00001327 if (n == 1) {
1328 vstr_printf(vstr, ",");
1329 }
1330 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001331 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001332 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001333 }
Damiendb4c3612013-12-10 17:27:24 +00001334#else
1335 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001336 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1337 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1338 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001339 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001340 }
Damien Georgeb9791222014-01-23 00:34:21 +00001341 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001342#endif
1343
1344 // do the import
Damien429d7192013-10-04 19:53:11 +01001345 qstr dummy_q, id1;
1346 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1347 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001348 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1349 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1350 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001351 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001352 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001353 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001354 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001355 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001356 }
1357 }
1358 EMIT(pop_top);
1359 }
1360}
1361
Damiend99b0522013-12-21 18:17:45 +00001362void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001363 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001364 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1365 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001366 } else {
Damiend99b0522013-12-21 18:17:45 +00001367 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1368 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001369 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001370 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001371 }
Damien429d7192013-10-04 19:53:11 +01001372 }
1373 }
1374}
1375
Damiend99b0522013-12-21 18:17:45 +00001376void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001377 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001378 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1379 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001380 } else {
Damiend99b0522013-12-21 18:17:45 +00001381 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1382 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001383 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001384 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001385 }
Damien429d7192013-10-04 19:53:11 +01001386 }
1387 }
1388}
1389
Damiend99b0522013-12-21 18:17:45 +00001390void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001391 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001392 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001393 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 +00001394 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001395 // assertion message
1396 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001397 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01001398 }
Damien Georgeb9791222014-01-23 00:34:21 +00001399 EMIT_ARG(raise_varargs, 1);
1400 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001401}
1402
Damiend99b0522013-12-21 18:17:45 +00001403void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001404 // TODO proper and/or short circuiting
1405
Damienb05d7072013-10-05 13:37:10 +01001406 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001407
Damienb05d7072013-10-05 13:37:10 +01001408 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001409 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1410
1411 compile_node(comp, pns->nodes[1]); // if block
Damiend99b0522013-12-21 18:17:45 +00001412 //if (!(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3]))) { // optimisation; doesn't align with CPython
Damien429d7192013-10-04 19:53:11 +01001413 // jump over elif/else blocks if they exist
Damien415eb6f2013-10-05 12:19:06 +01001414 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001415 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001416 }
1417 //}
Damien Georgeb9791222014-01-23 00:34:21 +00001418 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001419
Damiend99b0522013-12-21 18:17:45 +00001420 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001421 // compile elif blocks
1422
Damiend99b0522013-12-21 18:17:45 +00001423 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001424
Damiend99b0522013-12-21 18:17:45 +00001425 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001426 // multiple elif blocks
1427
Damiend99b0522013-12-21 18:17:45 +00001428 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001429 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001430 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001431 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001432 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1433
1434 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001435 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001436 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001437 }
Damien Georgeb9791222014-01-23 00:34:21 +00001438 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001439 }
1440
1441 } else {
1442 // a single elif block
1443
Damienb05d7072013-10-05 13:37:10 +01001444 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001445 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1446
1447 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001448 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001449 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001450 }
Damien Georgeb9791222014-01-23 00:34:21 +00001451 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001452 }
1453 }
1454
1455 // compile else block
1456 compile_node(comp, pns->nodes[3]); // can be null
1457
Damien Georgeb9791222014-01-23 00:34:21 +00001458 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001459}
1460
Damien Georgecbddb272014-02-01 20:08:18 +00001461#define START_BREAK_CONTINUE_BLOCK \
1462 int old_break_label = comp->break_label; \
1463 int old_continue_label = comp->continue_label; \
1464 int break_label = comp_next_label(comp); \
1465 int continue_label = comp_next_label(comp); \
1466 comp->break_label = break_label; \
1467 comp->continue_label = continue_label; \
1468 comp->break_continue_except_level = comp->cur_except_level;
1469
1470#define END_BREAK_CONTINUE_BLOCK \
1471 comp->break_label = old_break_label; \
1472 comp->continue_label = old_continue_label; \
1473 comp->break_continue_except_level = comp->cur_except_level;
1474
Damiend99b0522013-12-21 18:17:45 +00001475void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001476 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001477
Damience89a212013-10-15 22:25:17 +01001478 // compared to CPython, we have an optimised version of while loops
1479#if MICROPY_EMIT_CPYTHON
1480 int done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001481 EMIT_ARG(setup_loop, break_label);
1482 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001483 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1484 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001485 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001486 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001487 }
Damien Georgeb9791222014-01-23 00:34:21 +00001488 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001489 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1490 // this is a small hack to agree with CPython
1491 if (!node_is_const_true(pns->nodes[0])) {
1492 EMIT(pop_block);
1493 }
Damience89a212013-10-15 22:25:17 +01001494#else
1495 int top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001496 EMIT_ARG(jump, continue_label);
1497 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001498 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001499 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001500 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1501#endif
1502
1503 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001504 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001505
1506 compile_node(comp, pns->nodes[2]); // else
1507
Damien Georgeb9791222014-01-23 00:34:21 +00001508 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001509}
1510
Damienf72fd0e2013-11-06 20:20:49 +00001511// TODO preload end and step onto stack if they are not constants
1512// TODO check if step is negative and do opposite test
Damiend99b0522013-12-21 18:17:45 +00001513void 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 +00001514 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001515
1516 int top_label = comp_next_label(comp);
Damien George600ae732014-01-21 23:48:04 +00001517 int entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001518
1519 // compile: var = start
1520 compile_node(comp, pn_start);
1521 c_assign(comp, pn_var, ASSIGN_STORE);
1522
Damien Georgeb9791222014-01-23 00:34:21 +00001523 EMIT_ARG(jump, entry_label);
1524 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001525
Damienf3822fc2013-11-09 20:12:03 +00001526 // compile body
1527 compile_node(comp, pn_body);
1528
Damien Georgeb9791222014-01-23 00:34:21 +00001529 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001530
Damienf72fd0e2013-11-06 20:20:49 +00001531 // compile: var += step
1532 c_assign(comp, pn_var, ASSIGN_AUG_LOAD);
1533 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001534 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001535 c_assign(comp, pn_var, ASSIGN_AUG_STORE);
1536
Damien Georgeb9791222014-01-23 00:34:21 +00001537 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001538
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001539 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001540 compile_node(comp, pn_var);
1541 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001542 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1543 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001544 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001545 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001546 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001547 }
Damien Georgeb9791222014-01-23 00:34:21 +00001548 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001549
1550 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001551 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001552
1553 compile_node(comp, pn_else);
1554
Damien Georgeb9791222014-01-23 00:34:21 +00001555 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001556}
1557
Damiend99b0522013-12-21 18:17:45 +00001558void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001559#if !MICROPY_EMIT_CPYTHON
1560 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1561 // this is actually slower, but uses no heap memory
1562 // for viper it will be much, much faster
Damiend99b0522013-12-21 18:17:45 +00001563 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)) {
1564 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001565 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1566 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1567 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1568 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001569 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1570 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001571 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001572 mp_parse_node_t pn_range_start;
1573 mp_parse_node_t pn_range_end;
1574 mp_parse_node_t pn_range_step;
1575 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001576 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001577 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001578 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001579 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001580 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001581 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001582 } else if (n_args == 2) {
1583 pn_range_start = args[0];
1584 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001585 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001586 } else {
1587 pn_range_start = args[0];
1588 pn_range_end = args[1];
1589 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001590 // We need to know sign of step. This is possible only if it's constant
1591 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1592 optimize = false;
1593 }
Damienf72fd0e2013-11-06 20:20:49 +00001594 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001595 }
1596 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001597 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1598 return;
1599 }
1600 }
1601 }
1602#endif
1603
Damien Georgecbddb272014-02-01 20:08:18 +00001604 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001605
Damienb05d7072013-10-05 13:37:10 +01001606 int pop_label = comp_next_label(comp);
1607 int end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001608
Damience89a212013-10-15 22:25:17 +01001609 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1610#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001611 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001612#endif
1613
Damien429d7192013-10-04 19:53:11 +01001614 compile_node(comp, pns->nodes[1]); // iterator
1615 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001616 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001617 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001618 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1619 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001620 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001621 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001622 }
Damien Georgeb9791222014-01-23 00:34:21 +00001623 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001624 EMIT(for_iter_end);
1625
1626 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001627 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001628
Damience89a212013-10-15 22:25:17 +01001629#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001630 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001631#endif
Damien429d7192013-10-04 19:53:11 +01001632
1633 compile_node(comp, pns->nodes[3]); // else (not tested)
1634
Damien Georgeb9791222014-01-23 00:34:21 +00001635 EMIT_ARG(label_assign, break_label);
1636 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001637}
1638
Damiend99b0522013-12-21 18:17:45 +00001639void 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 +01001640 // this function is a bit of a hack at the moment
1641 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1642
1643 // setup code
1644 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001645 int l1 = comp_next_label(comp);
1646 int success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001647
Damien Georgeb9791222014-01-23 00:34:21 +00001648 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001649 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001650
Damien429d7192013-10-04 19:53:11 +01001651 compile_node(comp, pn_body); // body
1652 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001653 EMIT_ARG(jump, success_label);
1654 EMIT_ARG(label_assign, l1);
Damienb05d7072013-10-05 13:37:10 +01001655 int l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001656
1657 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001658 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1659 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001660
1661 qstr qstr_exception_local = 0;
Damienb05d7072013-10-05 13:37:10 +01001662 int end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001663
Damiend99b0522013-12-21 18:17:45 +00001664 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001665 // this is a catch all exception handler
1666 if (i + 1 != n_except) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001667 compile_syntax_error(comp, "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001668 return;
1669 }
1670 } else {
1671 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001672 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1673 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1674 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1675 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001676 // handler binds the exception to a local
1677 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001678 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001679 }
1680 }
1681 EMIT(dup_top);
1682 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001683 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001684 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001685 }
1686
1687 EMIT(pop_top);
1688
1689 if (qstr_exception_local == 0) {
1690 EMIT(pop_top);
1691 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001692 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001693 }
1694
1695 EMIT(pop_top);
1696
Damiene2880aa2013-12-20 14:22:59 +00001697 int l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001698 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001699 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001700 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001701 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001702 }
1703 compile_node(comp, pns_except->nodes[1]);
1704 if (qstr_exception_local != 0) {
1705 EMIT(pop_block);
1706 }
1707 EMIT(pop_except);
1708 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001709 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1710 EMIT_ARG(label_assign, l3);
1711 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1712 EMIT_ARG(store_id, qstr_exception_local);
1713 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001714
Damien George8dcc0c72014-03-27 10:55:21 +00001715 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001716 EMIT(end_finally);
1717 }
Damien Georgeb9791222014-01-23 00:34:21 +00001718 EMIT_ARG(jump, l2);
1719 EMIT_ARG(label_assign, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001720 }
1721
Damien George8dcc0c72014-03-27 10:55:21 +00001722 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001723 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001724
Damien Georgeb9791222014-01-23 00:34:21 +00001725 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001726 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001727 EMIT_ARG(label_assign, l2);
1728 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001729}
1730
Damiend99b0522013-12-21 18:17:45 +00001731void 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 +01001732 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1733 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001734 int l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001735
Damien Georgeb9791222014-01-23 00:34:21 +00001736 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001737 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001738
Damien429d7192013-10-04 19:53:11 +01001739 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001740 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien429d7192013-10-04 19:53:11 +01001741 compile_node(comp, pn_body);
1742 } else {
1743 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1744 }
1745 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001746 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1747 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001748 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001749
Damien George8dcc0c72014-03-27 10:55:21 +00001750 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001751 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001752
Damien Georgeb9791222014-01-23 00:34:21 +00001753 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001754}
1755
Damiend99b0522013-12-21 18:17:45 +00001756void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1757 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1758 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1759 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001760 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001761 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1762 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001763 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001764 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001765 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001766 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001767 // no finally
1768 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1769 } else {
1770 // have finally
Damiend99b0522013-12-21 18:17:45 +00001771 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 +01001772 }
1773 } else {
1774 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001775 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001776 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001777 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001778 }
1779 } else {
1780 // shouldn't happen
1781 assert(0);
1782 }
1783}
1784
Damiend99b0522013-12-21 18:17:45 +00001785void 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 +01001786 if (n == 0) {
1787 // no more pre-bits, compile the body of the with
1788 compile_node(comp, body);
1789 } else {
Damienb05d7072013-10-05 13:37:10 +01001790 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001791 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001792 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001793 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001794 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001795 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001796 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1797 } else {
1798 // this pre-bit is just an expression
1799 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001800 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001801 EMIT(pop_top);
1802 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001803 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001804 // compile additional pre-bits and the body
1805 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1806 // finish this with block
1807 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001808 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1809 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001810 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001811 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001812 EMIT(end_finally);
1813 }
1814}
1815
Damiend99b0522013-12-21 18:17:45 +00001816void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001817 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001818 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001819 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1820 assert(n > 0);
1821
1822 // compile in a nested fashion
1823 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1824}
1825
Damiend99b0522013-12-21 18:17:45 +00001826void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1827 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001828 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1829 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001830 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001831 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001832 EMIT_ARG(call_function, 1, 0, false, false);
Damien5ac1b2e2013-10-18 19:58:12 +01001833 EMIT(pop_top);
1834
Damien429d7192013-10-04 19:53:11 +01001835 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001836 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001837 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001838 // do nothing with a lonely constant
1839 } else {
1840 compile_node(comp, pns->nodes[0]); // just an expression
1841 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1842 }
Damien429d7192013-10-04 19:53:11 +01001843 }
1844 } else {
Damiend99b0522013-12-21 18:17:45 +00001845 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1846 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001847 if (kind == PN_expr_stmt_augassign) {
1848 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1849 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001850 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001851 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001852 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001853 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1854 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1855 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1856 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1857 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1858 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1859 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1860 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1861 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1862 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1863 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
1864 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
1865 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01001866 }
Damien George7e5fb242014-02-01 22:18:47 +00001867 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001868 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1869 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001870 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1871 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001872 // following CPython, we store left-most first
1873 if (rhs > 0) {
1874 EMIT(dup_top);
1875 }
1876 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1877 for (int i = 0; i < rhs; i++) {
1878 if (i + 1 < rhs) {
1879 EMIT(dup_top);
1880 }
Damiend99b0522013-12-21 18:17:45 +00001881 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001882 }
1883 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001884 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1885 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1886 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1887 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01001888 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001889 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1890 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001891 compile_node(comp, pns10->nodes[0]); // rhs
1892 compile_node(comp, pns10->nodes[1]); // rhs
1893 EMIT(rot_two);
1894 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1895 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00001896 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1897 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1898 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1899 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01001900 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001901 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1902 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001903 compile_node(comp, pns10->nodes[0]); // rhs
1904 compile_node(comp, pns10->nodes[1]); // rhs
1905 compile_node(comp, pns10->nodes[2]); // rhs
1906 EMIT(rot_three);
1907 EMIT(rot_two);
1908 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1909 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1910 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1911 } else {
1912 compile_node(comp, pns1->nodes[0]); // rhs
1913 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1914 }
1915 } else {
1916 // shouldn't happen
1917 assert(0);
1918 }
1919 }
1920}
1921
Damien Georged17926d2014-03-30 13:35:08 +01001922void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00001923 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001924 compile_node(comp, pns->nodes[0]);
1925 for (int i = 1; i < num_nodes; i += 1) {
1926 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001927 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001928 }
1929}
1930
Damiend99b0522013-12-21 18:17:45 +00001931void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1932 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1933 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001934
1935 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001936 int l_fail = comp_next_label(comp);
1937 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001938 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1939 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001940 EMIT_ARG(jump, l_end);
1941 EMIT_ARG(label_assign, l_fail);
1942 EMIT_ARG(set_stack_size, stack_size); // force stack size reset
Damien429d7192013-10-04 19:53:11 +01001943 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001944 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001945}
1946
Damiend99b0522013-12-21 18:17:45 +00001947void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001948 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00001949 //mp_parse_node_t pn_params = pns->nodes[0];
1950 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001951
1952 if (comp->pass == PASS_1) {
1953 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00001954 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 +01001955 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001956 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001957 }
1958
1959 // get the scope for this lambda
1960 scope_t *this_scope = (scope_t*)pns->nodes[2];
1961
1962 // make the lambda
1963 close_over_variables_etc(comp, this_scope, 0, 0);
1964}
1965
Damiend99b0522013-12-21 18:17:45 +00001966void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001967 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001968 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001969 for (int i = 0; i < n; i += 1) {
1970 compile_node(comp, pns->nodes[i]);
1971 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00001972 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01001973 }
1974 }
Damien Georgeb9791222014-01-23 00:34:21 +00001975 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001976}
1977
Damiend99b0522013-12-21 18:17:45 +00001978void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001979 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001980 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001981 for (int i = 0; i < n; i += 1) {
1982 compile_node(comp, pns->nodes[i]);
1983 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00001984 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01001985 }
1986 }
Damien Georgeb9791222014-01-23 00:34:21 +00001987 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001988}
1989
Damiend99b0522013-12-21 18:17:45 +00001990void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001991 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01001992 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01001993}
1994
Damiend99b0522013-12-21 18:17:45 +00001995void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001996 int stack_size = EMIT(get_stack_size);
Damiend99b0522013-12-21 18:17:45 +00001997 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001998 compile_node(comp, pns->nodes[0]);
1999 bool multi = (num_nodes > 3);
2000 int l_fail = 0;
2001 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002002 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002003 }
2004 for (int i = 1; i + 1 < num_nodes; i += 2) {
2005 compile_node(comp, pns->nodes[i + 1]);
2006 if (i + 2 < num_nodes) {
2007 EMIT(dup_top);
2008 EMIT(rot_three);
2009 }
Damien George7e5fb242014-02-01 22:18:47 +00002010 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002011 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002012 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002013 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2014 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2015 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2016 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2017 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2018 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2019 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2020 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002021 }
2022 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002023 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2024 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2025 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002026 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002027 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002028 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002029 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002030 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002031 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002032 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002033 }
2034 } else {
2035 // shouldn't happen
2036 assert(0);
2037 }
2038 } else {
2039 // shouldn't happen
2040 assert(0);
2041 }
2042 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002043 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002044 }
2045 }
2046 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002047 int l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002048 EMIT_ARG(jump, l_end);
2049 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01002050 EMIT(rot_two);
2051 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002052 EMIT_ARG(label_assign, l_end);
2053 EMIT_ARG(set_stack_size, stack_size + 1); // force stack size
Damien429d7192013-10-04 19:53:11 +01002054 }
2055}
2056
Damiend99b0522013-12-21 18:17:45 +00002057void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002058 // TODO
2059 assert(0);
2060 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002061 //EMIT_ARG(unary_op, "UNARY_STAR");
Damien429d7192013-10-04 19:53:11 +01002062}
2063
Damiend99b0522013-12-21 18:17:45 +00002064void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002065 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002066}
2067
Damiend99b0522013-12-21 18:17:45 +00002068void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002069 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002070}
2071
Damiend99b0522013-12-21 18:17:45 +00002072void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002073 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002074}
2075
Damiend99b0522013-12-21 18:17:45 +00002076void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2077 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002078 compile_node(comp, pns->nodes[0]);
2079 for (int i = 1; i + 1 < num_nodes; i += 2) {
2080 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002081 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002082 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002083 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002084 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002085 } else {
2086 // shouldn't happen
2087 assert(0);
2088 }
2089 }
2090}
2091
Damiend99b0522013-12-21 18:17:45 +00002092void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2093 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002094 compile_node(comp, pns->nodes[0]);
2095 for (int i = 1; i + 1 < num_nodes; i += 2) {
2096 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002097 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002098 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002099 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002100 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002101 } else {
2102 // shouldn't happen
2103 assert(0);
2104 }
2105 }
2106}
2107
Damiend99b0522013-12-21 18:17:45 +00002108void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2109 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002110 compile_node(comp, pns->nodes[0]);
2111 for (int i = 1; i + 1 < num_nodes; i += 2) {
2112 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002113 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002114 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002115 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002116 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002117 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002118 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002119 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002120 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002121 } else {
2122 // shouldn't happen
2123 assert(0);
2124 }
2125 }
2126}
2127
Damiend99b0522013-12-21 18:17:45 +00002128void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002129 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002130 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002131 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002132 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002133 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002134 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002135 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002136 } else {
2137 // shouldn't happen
2138 assert(0);
2139 }
2140}
2141
Damien George35e2a4e2014-02-05 00:51:47 +00002142void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2143 // this is to handle special super() call
2144 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2145
2146 compile_generic_all_nodes(comp, pns);
2147}
2148
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002149STATIC 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 +01002150 // function to call is on top of stack
2151
Damien George35e2a4e2014-02-05 00:51:47 +00002152#if !MICROPY_EMIT_CPYTHON
2153 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002154 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 +00002155 EMIT_ARG(load_id, MP_QSTR___class__);
2156 // get first argument to function
2157 bool found = false;
2158 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
2159 if (comp->scope_cur->id_info[i].param) {
2160 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
2161 found = true;
2162 break;
2163 }
2164 }
2165 if (!found) {
2166 printf("TypeError: super() call cannot find self\n");
2167 return;
2168 }
2169 EMIT_ARG(call_function, 2, 0, false, false);
2170 return;
2171 }
2172#endif
2173
Damien429d7192013-10-04 19:53:11 +01002174 int old_n_arg_keyword = comp->n_arg_keyword;
2175 bool old_have_star_arg = comp->have_star_arg;
2176 bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
2177 comp->n_arg_keyword = 0;
2178 comp->have_star_arg = false;
2179 comp->have_dbl_star_arg = false;
2180
Damien Georgebbcd49a2014-02-06 20:30:16 +00002181 compile_node(comp, pn_arglist); // arguments to function call; can be null
Damien429d7192013-10-04 19:53:11 +01002182
2183 // compute number of positional arguments
Damien Georgebbcd49a2014-02-06 20:30:16 +00002184 int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
Damien429d7192013-10-04 19:53:11 +01002185 if (comp->have_star_arg) {
2186 n_positional -= 1;
2187 }
2188 if (comp->have_dbl_star_arg) {
2189 n_positional -= 1;
2190 }
2191
2192 if (is_method_call) {
Damien Georgeb9791222014-01-23 00:34:21 +00002193 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 +01002194 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002195 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 +01002196 }
2197
2198 comp->n_arg_keyword = old_n_arg_keyword;
2199 comp->have_star_arg = old_have_star_arg;
2200 comp->have_dbl_star_arg = old_have_dbl_star_arg;
2201}
2202
Damiend99b0522013-12-21 18:17:45 +00002203void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2204 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002205 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002206 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 +01002207 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002208 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2209 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002210 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002211 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002212 i += 1;
2213 } else {
2214 compile_node(comp, pns->nodes[i]);
2215 }
Damien George35e2a4e2014-02-05 00:51:47 +00002216 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002217 }
2218}
2219
Damiend99b0522013-12-21 18:17:45 +00002220void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002221 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002222 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002223}
2224
Damiend99b0522013-12-21 18:17:45 +00002225void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002226 // a list of strings
Damien63321742013-12-10 17:41:49 +00002227
2228 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002229 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002230 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002231 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002232 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002233 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2234 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2235 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002236 if (i == 0) {
2237 string_kind = pn_kind;
2238 } else if (pn_kind != string_kind) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002239 compile_syntax_error(comp, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002240 return;
2241 }
Damien George55baff42014-01-21 21:40:13 +00002242 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002243 }
Damien63321742013-12-10 17:41:49 +00002244
Damien63321742013-12-10 17:41:49 +00002245 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002246 byte *q_ptr;
2247 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002248 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002249 uint s_len;
2250 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002251 memcpy(s_dest, s, s_len);
2252 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002253 }
Damien George55baff42014-01-21 21:40:13 +00002254 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002255
Damien Georgeb9791222014-01-23 00:34:21 +00002256 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002257}
2258
2259// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002260void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2261 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2262 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2263 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002264
2265 if (comp->pass == PASS_1) {
2266 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002267 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 +01002268 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002269 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002270 }
2271
2272 // get the scope for this comprehension
2273 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2274
2275 // compile the comprehension
2276 close_over_variables_etc(comp, this_scope, 0, 0);
2277
2278 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2279 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002280 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01002281}
2282
Damiend99b0522013-12-21 18:17:45 +00002283void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2284 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002285 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002286 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2287 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2288 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2289 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2290 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2291 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2292 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002293 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002294 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002295 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002296 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002297 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002298 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002299 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002300 // generator expression
2301 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2302 } else {
2303 // tuple with 2 items
2304 goto tuple_with_2_items;
2305 }
2306 } else {
2307 // tuple with 2 items
2308 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002309 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002310 }
2311 } else {
2312 // parenthesis around a single item, is just that item
2313 compile_node(comp, pns->nodes[0]);
2314 }
2315}
2316
Damiend99b0522013-12-21 18:17:45 +00002317void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2318 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002319 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002320 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002321 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2322 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2323 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2324 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2325 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002326 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002327 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002328 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002329 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002330 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002331 // list of many items
2332 compile_node(comp, pns2->nodes[0]);
2333 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002334 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002335 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002336 // list comprehension
2337 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2338 } else {
2339 // list with 2 items
2340 goto list_with_2_items;
2341 }
2342 } else {
2343 // list with 2 items
2344 list_with_2_items:
2345 compile_node(comp, pns2->nodes[0]);
2346 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002347 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002348 }
2349 } else {
2350 // list with 1 item
2351 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002352 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002353 }
2354}
2355
Damiend99b0522013-12-21 18:17:45 +00002356void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2357 mp_parse_node_t pn = pns->nodes[0];
2358 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002359 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002360 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002361 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2362 pns = (mp_parse_node_struct_t*)pn;
2363 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002364 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002365 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002366 compile_node(comp, pn);
2367 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002368 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2369 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2370 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2371 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002372 // dict/set with multiple elements
2373
2374 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002375 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002376 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2377
2378 // first element sets whether it's a dict or set
2379 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002380 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002381 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002382 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002383 compile_node(comp, pns->nodes[0]);
2384 EMIT(store_map);
2385 is_dict = true;
2386 } else {
2387 // a set
2388 compile_node(comp, pns->nodes[0]); // 1st value of set
2389 is_dict = false;
2390 }
2391
2392 // process rest of elements
2393 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002394 mp_parse_node_t pn = nodes[i];
2395 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002396 compile_node(comp, pn);
2397 if (is_dict) {
2398 if (!is_key_value) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002399 compile_syntax_error(comp, "?expecting key:value for dictiona");
Damien429d7192013-10-04 19:53:11 +01002400 return;
2401 }
2402 EMIT(store_map);
2403 } else {
2404 if (is_key_value) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002405 compile_syntax_error(comp, "?expecting just a value for s");
Damien429d7192013-10-04 19:53:11 +01002406 return;
2407 }
2408 }
2409 }
2410
2411 // if it's a set, build it
2412 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002413 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002414 }
Damiend99b0522013-12-21 18:17:45 +00002415 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002416 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002417 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002418 // a dictionary comprehension
2419 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2420 } else {
2421 // a set comprehension
2422 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2423 }
2424 } else {
2425 // shouldn't happen
2426 assert(0);
2427 }
2428 } else {
2429 // set with one element
2430 goto set_with_one_element;
2431 }
2432 } else {
2433 // set with one element
2434 set_with_one_element:
2435 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002436 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002437 }
2438}
2439
Damiend99b0522013-12-21 18:17:45 +00002440void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002441 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002442}
2443
Damiend99b0522013-12-21 18:17:45 +00002444void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002445 // object who's index we want is on top of stack
2446 compile_node(comp, pns->nodes[0]); // the index
Damien Georged17926d2014-03-30 13:35:08 +01002447 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002448}
2449
Damiend99b0522013-12-21 18:17:45 +00002450void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002451 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002452 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002453}
2454
Damiend99b0522013-12-21 18:17:45 +00002455void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2456 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2457 mp_parse_node_t pn = pns->nodes[0];
2458 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002459 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002460 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2461 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002462 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2463 pns = (mp_parse_node_struct_t*)pn;
2464 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002465 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002466 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002467 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002468 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002469 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002470 } else {
2471 // [?::x]
2472 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002473 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002474 }
Damiend99b0522013-12-21 18:17:45 +00002475 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002476 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002477 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2478 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2479 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2480 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002481 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002482 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002483 } else {
2484 // [?:x:x]
2485 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002486 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002487 }
2488 } else {
2489 // [?:x]
2490 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002491 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002492 }
2493 } else {
2494 // [?:x]
2495 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002496 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002497 }
2498}
2499
Damiend99b0522013-12-21 18:17:45 +00002500void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002501 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002502 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2503 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002504}
2505
Damiend99b0522013-12-21 18:17:45 +00002506void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002507 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002508 compile_subscript_3_helper(comp, pns);
2509}
2510
Damiend99b0522013-12-21 18:17:45 +00002511void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002512 // if this is called then we are compiling a dict key:value pair
2513 compile_node(comp, pns->nodes[1]); // value
2514 compile_node(comp, pns->nodes[0]); // key
2515}
2516
Damiend99b0522013-12-21 18:17:45 +00002517void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002518 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002519 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002520 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002521}
2522
Damiend99b0522013-12-21 18:17:45 +00002523void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002524 if (comp->have_star_arg) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002525 compile_syntax_error(comp, "?can't have multiple *x");
Damien429d7192013-10-04 19:53:11 +01002526 return;
2527 }
2528 comp->have_star_arg = true;
2529 compile_node(comp, pns->nodes[0]);
2530}
2531
Damiend99b0522013-12-21 18:17:45 +00002532void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002533 if (comp->have_dbl_star_arg) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002534 compile_syntax_error(comp, "?can't have multiple **x");
Damien429d7192013-10-04 19:53:11 +01002535 return;
2536 }
2537 comp->have_dbl_star_arg = true;
2538 compile_node(comp, pns->nodes[0]);
2539}
2540
Damiend99b0522013-12-21 18:17:45 +00002541void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2542 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2543 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2544 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2545 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002546 compile_syntax_error(comp, "?lhs of keyword argument must be an id");
Damien429d7192013-10-04 19:53:11 +01002547 return;
2548 }
Damien Georgeb9791222014-01-23 00:34:21 +00002549 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002550 compile_node(comp, pns2->nodes[0]);
2551 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002552 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002553 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2554 } else {
2555 // shouldn't happen
2556 assert(0);
2557 }
2558}
2559
Damiend99b0522013-12-21 18:17:45 +00002560void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002561 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002562 compile_syntax_error(comp, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002563 return;
2564 }
Damiend99b0522013-12-21 18:17:45 +00002565 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002566 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002567 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002568 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2569 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002570 compile_node(comp, pns->nodes[0]);
2571 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002572 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002573 EMIT(yield_from);
2574 } else {
2575 compile_node(comp, pns->nodes[0]);
2576 EMIT(yield_value);
2577 }
2578}
2579
Damiend99b0522013-12-21 18:17:45 +00002580typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002581STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002582 NULL,
2583#define nc NULL
2584#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002585#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002586#include "grammar.h"
2587#undef nc
2588#undef c
2589#undef DEF_RULE
2590};
2591
Damiend99b0522013-12-21 18:17:45 +00002592void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2593 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002594 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002595 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2596 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2597 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002598 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002599 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002600 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002601 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002602 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2603 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2604 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2605 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002606 case MP_PARSE_NODE_TOKEN:
2607 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002608 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002609 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002610 // do nothing
2611 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002612 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002613 }
2614 break;
Damien429d7192013-10-04 19:53:11 +01002615 default: assert(0);
2616 }
2617 } else {
Damiend99b0522013-12-21 18:17:45 +00002618 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002619 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002620 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002621 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002622 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002623#if MICROPY_DEBUG_PRINTERS
2624 mp_parse_node_print(pn, 0);
2625#endif
Damien429d7192013-10-04 19:53:11 +01002626 assert(0);
2627 } else {
2628 f(comp, pns);
2629 }
2630 }
2631}
2632
Damiend99b0522013-12-21 18:17:45 +00002633void 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 +01002634 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002635 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002636 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2637 if (MP_PARSE_NODE_IS_ID(pn)) {
2638 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002639 if (comp->have_bare_star) {
2640 // comes after a bare star, so doesn't count as a parameter
2641 } else {
2642 comp->scope_cur->num_params += 1;
2643 }
Damienb14de212013-10-06 00:28:28 +01002644 } else {
Damiend99b0522013-12-21 18:17:45 +00002645 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2646 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2647 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2648 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002649 //int node_index = 1; unused
2650 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002651 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002652 // this parameter has an annotation
2653 pn_annotation = pns->nodes[1];
2654 }
2655 //node_index = 2; unused
2656 }
2657 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002658 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002659 // this parameter has a default value
2660 if (comp->have_bare_star) {
2661 comp->scope_cur->num_dict_params += 1;
2662 } else {
2663 comp->scope_cur->num_default_params += 1;
2664 }
2665 }
2666 */
2667 if (comp->have_bare_star) {
2668 // comes after a bare star, so doesn't count as a parameter
2669 } else {
2670 comp->scope_cur->num_params += 1;
2671 }
Damiend99b0522013-12-21 18:17:45 +00002672 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2673 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002674 // bare star
2675 // TODO see http://www.python.org/dev/peps/pep-3102/
2676 comp->have_bare_star = true;
2677 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002678 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002679 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002680 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002681 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2682 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002683 // named star with annotation
Damien George8725f8f2014-02-15 19:33:11 +00002684 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002685 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2686 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002687 pn_annotation = pns->nodes[1];
2688 } else {
2689 // shouldn't happen
2690 assert(0);
2691 }
Damiend99b0522013-12-21 18:17:45 +00002692 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2693 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2694 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002695 // this parameter has an annotation
2696 pn_annotation = pns->nodes[1];
2697 }
Damien George8725f8f2014-02-15 19:33:11 +00002698 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002699 } else {
Damienb14de212013-10-06 00:28:28 +01002700 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002701 assert(0);
2702 }
Damien429d7192013-10-04 19:53:11 +01002703 }
2704
2705 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002706 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002707 // TODO this parameter has an annotation
2708 }
2709 bool added;
2710 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2711 if (!added) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002712 compile_syntax_error(comp, "?same name used for parameter");
Damien429d7192013-10-04 19:53:11 +01002713 return;
2714 }
2715 id_info->param = true;
2716 id_info->kind = ID_INFO_KIND_LOCAL;
2717 }
2718}
2719
Damiend99b0522013-12-21 18:17:45 +00002720void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002721 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2722}
2723
Damiend99b0522013-12-21 18:17:45 +00002724void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002725 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2726}
2727
Damiend99b0522013-12-21 18:17:45 +00002728void 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 +01002729 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002730 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002731 // no more nested if/for; compile inner expression
2732 compile_node(comp, pn_inner_expr);
2733 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002734 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002735 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002736 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002737 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002738 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002739 } else {
2740 EMIT(yield_value);
2741 EMIT(pop_top);
2742 }
Damiend99b0522013-12-21 18:17:45 +00002743 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002744 // if condition
Damiend99b0522013-12-21 18:17:45 +00002745 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002746 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2747 pn_iter = pns_comp_if->nodes[1];
2748 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002749 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002750 // for loop
Damiend99b0522013-12-21 18:17:45 +00002751 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002752 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002753 int l_end2 = comp_next_label(comp);
2754 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002755 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002756 EMIT_ARG(label_assign, l_top2);
2757 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002758 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2759 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 +00002760 EMIT_ARG(jump, l_top2);
2761 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002762 EMIT(for_iter_end);
2763 } else {
2764 // shouldn't happen
2765 assert(0);
2766 }
2767}
2768
Damiend99b0522013-12-21 18:17:45 +00002769void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002770 // see http://www.python.org/dev/peps/pep-0257/
2771
2772 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002773 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002774 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002775 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002776 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002777 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2778 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002779 for (int i = 0; i < num_nodes; i++) {
2780 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002781 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 +00002782 // not a newline, so this is the first statement; finish search
2783 break;
2784 }
2785 }
2786 // 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 +00002787 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002788 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002789 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002790 } else {
2791 return;
2792 }
2793
2794 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002795 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2796 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2797 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2798 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2799 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002800 compile_node(comp, pns->nodes[0]); // a doc string
2801 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002802 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002803 }
2804 }
2805 }
2806}
2807
2808void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2809 comp->pass = pass;
2810 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002811 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002812 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002813
2814 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002815 // reset maximum stack sizes in scope
2816 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002817 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002818 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002819 }
2820
Damien5ac1b2e2013-10-18 19:58:12 +01002821#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002822 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002823 scope_print_info(scope);
2824 }
Damien5ac1b2e2013-10-18 19:58:12 +01002825#endif
Damien429d7192013-10-04 19:53:11 +01002826
2827 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002828 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2829 assert(scope->kind == SCOPE_MODULE);
2830 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2831 compile_node(comp, pns->nodes[0]); // compile the expression
2832 EMIT(return_value);
2833 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002834 if (!comp->is_repl) {
2835 check_for_doc_string(comp, scope->pn);
2836 }
Damien429d7192013-10-04 19:53:11 +01002837 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002838 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002839 EMIT(return_value);
2840 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002841 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2842 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2843 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002844
2845 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002846 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002847 if (comp->pass == PASS_1) {
2848 comp->have_bare_star = false;
2849 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2850 }
2851
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02002852 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01002853
2854 compile_node(comp, pns->nodes[3]); // 3 is function body
2855 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002856 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002857 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002858 EMIT(return_value);
2859 }
2860 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002861 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2862 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2863 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002864
2865 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002866 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002867 if (comp->pass == PASS_1) {
2868 comp->have_bare_star = false;
2869 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2870 }
2871
2872 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2873 EMIT(return_value);
2874 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2875 // a bit of a hack at the moment
2876
Damiend99b0522013-12-21 18:17:45 +00002877 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2878 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2879 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2880 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2881 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002882
Damien George55baff42014-01-21 21:40:13 +00002883 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01002884 if (comp->pass == PASS_1) {
2885 bool added;
2886 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2887 assert(added);
2888 id_info->kind = ID_INFO_KIND_LOCAL;
2889 scope->num_params = 1;
2890 }
2891
2892 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002893 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01002894 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002895 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01002896 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002897 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01002898 }
2899
Damienb05d7072013-10-05 13:37:10 +01002900 int l_end = comp_next_label(comp);
2901 int l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002902 EMIT_ARG(load_id, qstr_arg);
2903 EMIT_ARG(label_assign, l_top);
2904 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01002905 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2906 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00002907 EMIT_ARG(jump, l_top);
2908 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002909 EMIT(for_iter_end);
2910
2911 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00002912 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002913 }
2914 EMIT(return_value);
2915 } else {
2916 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002917 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2918 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2919 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01002920
2921 if (comp->pass == PASS_1) {
2922 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002923 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01002924 assert(added);
2925 id_info->kind = ID_INFO_KIND_LOCAL;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002926 id_info = scope_find_or_add_id(scope, MP_QSTR___locals__, &added);
Damien429d7192013-10-04 19:53:11 +01002927 assert(added);
2928 id_info->kind = ID_INFO_KIND_LOCAL;
2929 id_info->param = true;
2930 scope->num_params = 1; // __locals__ is the parameter
2931 }
2932
Damien Georgeb9791222014-01-23 00:34:21 +00002933 EMIT_ARG(load_id, MP_QSTR___locals__);
Damien429d7192013-10-04 19:53:11 +01002934 EMIT(store_locals);
Damien Georgeb9791222014-01-23 00:34:21 +00002935 EMIT_ARG(load_id, MP_QSTR___name__);
2936 EMIT_ARG(store_id, MP_QSTR___module__);
2937 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
2938 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01002939
2940 check_for_doc_string(comp, pns->nodes[2]);
2941 compile_node(comp, pns->nodes[2]); // 2 is class body
2942
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002943 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01002944 assert(id != NULL);
2945 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00002946 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002947 } else {
Damien George6baf76e2013-12-30 22:32:17 +00002948#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00002949 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002950#else
Damien George35e2a4e2014-02-05 00:51:47 +00002951 EMIT_ARG(load_fast, MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00002952#endif
Damien429d7192013-10-04 19:53:11 +01002953 }
2954 EMIT(return_value);
2955 }
2956
Damien415eb6f2013-10-05 12:19:06 +01002957 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00002958
2959 // make sure we match all the exception levels
2960 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01002961}
2962
2963void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2964 comp->pass = pass;
2965 comp->scope_cur = scope;
2966 comp->next_label = 1;
2967
2968 if (scope->kind != SCOPE_FUNCTION) {
2969 printf("Error: inline assembler must be a function\n");
2970 return;
2971 }
2972
Damiena2f2f7d2013-10-06 00:14:13 +01002973 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00002974 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01002975 }
2976
Damien826005c2013-10-05 23:17:28 +01002977 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00002978 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2979 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2980 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01002981
Damiend99b0522013-12-21 18:17:45 +00002982 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01002983
Damiena2f2f7d2013-10-06 00:14:13 +01002984 // parameters are in pns->nodes[1]
2985 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00002986 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01002987 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00002988 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01002989 }
2990
Damiend99b0522013-12-21 18:17:45 +00002991 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01002992
Damiend99b0522013-12-21 18:17:45 +00002993 mp_parse_node_t pn_body = pns->nodes[3]; // body
2994 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01002995 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
2996
Damien Georgecbd2f742014-01-19 11:48:48 +00002997 /*
Damien826005c2013-10-05 23:17:28 +01002998 if (comp->pass == PASS_3) {
2999 //printf("----\n");
3000 scope_print_info(scope);
3001 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003002 */
Damien826005c2013-10-05 23:17:28 +01003003
3004 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003005 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3006 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
3007 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
3008 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3009 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3010 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3011 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3012 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3013 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3014 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3015 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3016 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3017 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003018 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3019
3020 // emit instructions
3021 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00003022 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgef41fdd02014-03-03 23:19:11 +00003023 compile_syntax_error(comp, "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003024 return;
3025 }
3026 int lab = comp_next_label(comp);
3027 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003028 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003029 }
3030 } else {
3031 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003032 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003033 }
3034 }
3035 }
3036
3037 if (comp->pass > PASS_1) {
3038 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01003039 }
Damien429d7192013-10-04 19:53:11 +01003040}
3041
3042void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3043 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003044 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003045 scope->num_locals = 0;
3046 for (int i = 0; i < scope->id_info_len; i++) {
3047 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003048 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003049 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3050 continue;
3051 }
3052 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3053 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3054 }
Damien9ecbcff2013-12-11 00:41:43 +00003055 // note: params always count for 1 local, even if they are a cell
Damien429d7192013-10-04 19:53:11 +01003056 if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
3057 id->local_num = scope->num_locals;
3058 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003059 }
3060 }
3061
3062 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003063#if MICROPY_EMIT_CPYTHON
3064 int num_cell = 0;
3065#endif
Damien9ecbcff2013-12-11 00:41:43 +00003066 for (int i = 0; i < scope->id_info_len; i++) {
3067 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003068#if MICROPY_EMIT_CPYTHON
3069 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003070 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003071 id->local_num = num_cell;
3072 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003073 }
Damien George6baf76e2013-12-30 22:32:17 +00003074#else
3075 // in Micro Python the cells come right after the fast locals
3076 // parameters are not counted here, since they remain at the start
3077 // of the locals, even if they are cell vars
3078 if (!id->param && id->kind == ID_INFO_KIND_CELL) {
3079 id->local_num = scope->num_locals;
3080 scope->num_locals += 1;
3081 }
3082#endif
Damien9ecbcff2013-12-11 00:41:43 +00003083 }
Damien9ecbcff2013-12-11 00:41:43 +00003084
3085 // compute the index of free vars (freevars[idx] in CPython)
3086 // make sure they are in the order of the parent scope
3087 if (scope->parent != NULL) {
3088 int num_free = 0;
3089 for (int i = 0; i < scope->parent->id_info_len; i++) {
3090 id_info_t *id = &scope->parent->id_info[i];
3091 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3092 for (int j = 0; j < scope->id_info_len; j++) {
3093 id_info_t *id2 = &scope->id_info[j];
3094 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +00003095 assert(!id2->param); // free vars should not be params
3096#if MICROPY_EMIT_CPYTHON
3097 // in CPython the frees are numbered after the cells
3098 id2->local_num = num_cell + num_free;
3099#else
3100 // in Micro Python the frees come first, before the params
3101 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003102#endif
3103 num_free += 1;
3104 }
3105 }
3106 }
Damien429d7192013-10-04 19:53:11 +01003107 }
Damien George6baf76e2013-12-30 22:32:17 +00003108#if !MICROPY_EMIT_CPYTHON
3109 // in Micro Python shift all other locals after the free locals
3110 if (num_free > 0) {
3111 for (int i = 0; i < scope->id_info_len; i++) {
3112 id_info_t *id = &scope->id_info[i];
3113 if (id->param || id->kind != ID_INFO_KIND_FREE) {
3114 id->local_num += num_free;
3115 }
3116 }
3117 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3118 scope->num_locals += num_free;
3119 }
3120#endif
Damien429d7192013-10-04 19:53:11 +01003121 }
3122
Damien George8725f8f2014-02-15 19:33:11 +00003123 // compute scope_flags
3124 //scope->scope_flags = 0; since we set some things in parameters
Damien429d7192013-10-04 19:53:11 +01003125 if (scope->kind != SCOPE_MODULE) {
Damien George8725f8f2014-02-15 19:33:11 +00003126 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien429d7192013-10-04 19:53:11 +01003127 }
3128 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) {
3129 assert(scope->parent != NULL);
Damien George8725f8f2014-02-15 19:33:11 +00003130 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien429d7192013-10-04 19:53:11 +01003131
3132 // TODO possibly other ways it can be nested
Damien George08d07552014-01-29 18:58:52 +00003133 // Note that we don't actually use this information at the moment (for CPython compat only)
3134 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 +00003135 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003136 }
3137 }
3138 int num_free = 0;
3139 for (int i = 0; i < scope->id_info_len; i++) {
3140 id_info_t *id = &scope->id_info[i];
3141 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3142 num_free += 1;
3143 }
3144 }
3145 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003146 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003147 }
3148}
3149
Damien George08335002014-01-18 23:24:36 +00003150mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
Damien429d7192013-10-04 19:53:11 +01003151 compiler_t *comp = m_new(compiler_t, 1);
3152
Damien Georgecbd2f742014-01-19 11:48:48 +00003153 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003154 comp->is_repl = is_repl;
3155 comp->had_error = false;
3156
Damien429d7192013-10-04 19:53:11 +01003157 comp->break_label = 0;
3158 comp->continue_label = 0;
Damien Georgecbddb272014-02-01 20:08:18 +00003159 comp->break_continue_except_level = 0;
3160 comp->cur_except_level = 0;
3161
Damien George35e2a4e2014-02-05 00:51:47 +00003162 comp->func_arg_is_super = false;
3163
Damien429d7192013-10-04 19:53:11 +01003164 comp->scope_head = NULL;
3165 comp->scope_cur = NULL;
3166
Damien826005c2013-10-05 23:17:28 +01003167 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003168 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003169
3170 // set the outer scope
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003171 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE);
Damien429d7192013-10-04 19:53:11 +01003172
Damien826005c2013-10-05 23:17:28 +01003173 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003174 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003175 comp->emit_method_table = &emit_pass1_method_table;
3176 comp->emit_inline_asm = NULL;
3177 comp->emit_inline_asm_method_table = NULL;
3178 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003179 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003180 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003181#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003182 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003183 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003184#endif
Damien826005c2013-10-05 23:17:28 +01003185 } else {
3186 compile_scope(comp, s, PASS_1);
3187 }
3188
3189 // update maximim number of labels needed
3190 if (comp->next_label > max_num_labels) {
3191 max_num_labels = comp->next_label;
3192 }
Damien429d7192013-10-04 19:53:11 +01003193 }
3194
Damien826005c2013-10-05 23:17:28 +01003195 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003196 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003197 compile_scope_compute_things(comp, s);
3198 }
3199
Damien826005c2013-10-05 23:17:28 +01003200 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003201 emit_pass1_free(comp->emit);
3202
Damien826005c2013-10-05 23:17:28 +01003203 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003204#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003205 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003206#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003207 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003208#endif
Damien3ef4abb2013-10-12 16:53:13 +01003209#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003210 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003211#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003212#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003213 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003214 if (false) {
3215 // dummy
3216
Damien3ef4abb2013-10-12 16:53:13 +01003217#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003218 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
3219 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003220 if (emit_inline_thumb == NULL) {
3221 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3222 }
3223 comp->emit = NULL;
3224 comp->emit_method_table = NULL;
3225 comp->emit_inline_asm = emit_inline_thumb;
3226 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3227 compile_scope_inline_asm(comp, s, PASS_2);
3228 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003229#endif
3230
Damien826005c2013-10-05 23:17:28 +01003231 } else {
Damienc025ebb2013-10-12 14:30:21 +01003232
3233 // choose the emit type
3234
Damien3ef4abb2013-10-12 16:53:13 +01003235#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003236 comp->emit = emit_cpython_new(max_num_labels);
3237 comp->emit_method_table = &emit_cpython_method_table;
3238#else
Damien826005c2013-10-05 23:17:28 +01003239 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003240
3241#if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003242 case EMIT_OPT_NATIVE_PYTHON:
Damien3410be82013-10-07 23:09:10 +01003243 case EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003244#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003245 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003246 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003247 }
Damien13ed3a62013-10-08 09:05:10 +01003248 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003249#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003250 if (emit_native == NULL) {
3251 emit_native = emit_native_thumb_new(max_num_labels);
3252 }
3253 comp->emit_method_table = &emit_native_thumb_method_table;
3254#endif
3255 comp->emit = emit_native;
Damien3410be82013-10-07 23:09:10 +01003256 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003257 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003258#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003259
Damien826005c2013-10-05 23:17:28 +01003260 default:
3261 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003262 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003263 }
3264 comp->emit = emit_bc;
3265 comp->emit_method_table = &emit_bc_method_table;
3266 break;
3267 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003268#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003269
3270 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003271 compile_scope(comp, s, PASS_2);
3272 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003273 }
Damien429d7192013-10-04 19:53:11 +01003274 }
3275
Damien George41d02b62014-01-24 22:42:28 +00003276 // free the emitters
3277#if !MICROPY_EMIT_CPYTHON
3278 if (emit_bc != NULL) {
3279 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003280 }
Damien George41d02b62014-01-24 22:42:28 +00003281#if MICROPY_EMIT_NATIVE
3282 if (emit_native != NULL) {
3283#if MICROPY_EMIT_X64
3284 emit_native_x64_free(emit_native);
3285#elif MICROPY_EMIT_THUMB
3286 emit_native_thumb_free(emit_native);
3287#endif
3288 }
3289#endif
3290#if MICROPY_EMIT_INLINE_THUMB
3291 if (emit_inline_thumb != NULL) {
3292 emit_inline_thumb_free(emit_inline_thumb);
3293 }
3294#endif
3295#endif // !MICROPY_EMIT_CPYTHON
3296
3297 // free the scopes
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003298 uint unique_code_id = module_scope->unique_code_id;
3299 for (scope_t *s = module_scope; s;) {
3300 scope_t *next = s->next;
3301 scope_free(s);
3302 s = next;
3303 }
Damien5ac1b2e2013-10-18 19:58:12 +01003304
Damien George41d02b62014-01-24 22:42:28 +00003305 // free the compiler
3306 bool had_error = comp->had_error;
3307 m_del_obj(compiler_t, comp);
3308
Damien George1fb03172014-01-03 14:22:03 +00003309 if (had_error) {
3310 // TODO return a proper error message
3311 return mp_const_none;
3312 } else {
3313#if MICROPY_EMIT_CPYTHON
3314 // can't create code, so just return true
Damien George41d02b62014-01-24 22:42:28 +00003315 (void)unique_code_id; // to suppress warning that unique_code_id is unused
Damien George1fb03172014-01-03 14:22:03 +00003316 return mp_const_true;
3317#else
3318 // return function that executes the outer module
Damien Georged1e443d2014-03-29 11:39:36 +00003319 // we can free the unique_code slot because no-one has reference to this unique_code_id anymore
Damien Georged17926d2014-03-30 13:35:08 +01003320 return mp_make_function_from_id(unique_code_id, true, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003321#endif
3322 }
Damien429d7192013-10-04 19:53:11 +01003323}