blob: 43253a926b1586e94d789f5124b7f43ec3c296db [file] [log] [blame]
xbeefe34222014-03-16 00:14:26 -07001#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +01002#include <stdint.h>
3#include <stdio.h>
4#include <string.h>
5#include <assert.h>
Rachel Dowdall56402792014-03-22 20:19:24 +00006#include <math.h>
Damien429d7192013-10-04 19:53:11 +01007
8#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00009#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000010#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010011#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010012#include "parse.h"
13#include "scope.h"
Damiend99b0522013-12-21 18:17:45 +000014#include "runtime0.h"
Damien429d7192013-10-04 19:53:11 +010015#include "emit.h"
Damien George2326d522014-03-27 23:26:35 +000016#include "emitglue.h"
Damien George1fb03172014-01-03 14:22:03 +000017#include "obj.h"
18#include "compile.h"
19#include "runtime.h"
Rachel Dowdallcde86312014-03-22 17:29:27 +000020#include "intdivmod.h"
Damien429d7192013-10-04 19:53:11 +010021
22// TODO need to mangle __attr names
23
Damience89a212013-10-15 22:25:17 +010024#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_THUMB)
25
Damien429d7192013-10-04 19:53:11 +010026typedef enum {
27 PN_none = 0,
Damien George00208ce2014-01-23 00:00:53 +000028#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien429d7192013-10-04 19:53:11 +010029#include "grammar.h"
30#undef DEF_RULE
31 PN_maximum_number_of,
32} pn_kind_t;
33
Damien Georgeb9791222014-01-23 00:34:21 +000034#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
35#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
36#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
37#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010038
Damien6cdd3af2013-10-05 18:08:26 +010039#define EMIT_OPT_NONE (0)
40#define EMIT_OPT_BYTE_CODE (1)
41#define EMIT_OPT_NATIVE_PYTHON (2)
Damien7af3d192013-10-07 00:02:49 +010042#define EMIT_OPT_VIPER (3)
43#define EMIT_OPT_ASM_THUMB (4)
Damien6cdd3af2013-10-05 18:08:26 +010044
Damien429d7192013-10-04 19:53:11 +010045typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000046 qstr source_file;
Damien5ac1b2e2013-10-18 19:58:12 +010047 bool is_repl;
Damien429d7192013-10-04 19:53:11 +010048 pass_kind_t pass;
Damien5ac1b2e2013-10-18 19:58:12 +010049 bool had_error; // try to keep compiler clean from nlr
Damien429d7192013-10-04 19:53:11 +010050
Damienb05d7072013-10-05 13:37:10 +010051 int next_label;
Damienb05d7072013-10-05 13:37:10 +010052
Damien429d7192013-10-04 19:53:11 +010053 int break_label;
54 int continue_label;
Damien Georgecbddb272014-02-01 20:08:18 +000055 int break_continue_except_level;
56 int cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien429d7192013-10-04 19:53:11 +010057
58 int n_arg_keyword;
59 bool have_star_arg;
60 bool have_dbl_star_arg;
61 bool have_bare_star;
62 int param_pass;
63 int param_pass_num_dict_params;
64 int param_pass_num_default_params;
65
Damien George35e2a4e2014-02-05 00:51:47 +000066 bool func_arg_is_super; // used to compile special case of super() function call
67
Damien429d7192013-10-04 19:53:11 +010068 scope_t *scope_head;
69 scope_t *scope_cur;
70
Damien6cdd3af2013-10-05 18:08:26 +010071 emit_t *emit; // current emitter
72 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010073
74 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
75 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien429d7192013-10-04 19:53:11 +010076} compiler_t;
77
Damien Georgef41fdd02014-03-03 23:19:11 +000078STATIC void compile_syntax_error(compiler_t *comp, const char *msg) {
79 // TODO store the error message to a variable in compiler_t instead of printing it
80 printf("SyntaxError: %s\n", msg);
81 comp->had_error = true;
82}
83
Damiend99b0522013-12-21 18:17:45 +000084mp_parse_node_t fold_constants(mp_parse_node_t pn) {
85 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
86 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
87 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +010088
89 // fold arguments first
90 for (int i = 0; i < n; i++) {
91 pns->nodes[i] = fold_constants(pns->nodes[i]);
92 }
93
Damiend99b0522013-12-21 18:17:45 +000094 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +010095 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +000096 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +020097 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
98 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +000099 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien3ef4abb2013-10-12 16:53:13 +0100100#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100101 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000102 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
Damien0efb3a12013-10-12 16:16:56 +0100103#endif
Damiend99b0522013-12-21 18:17:45 +0000104 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
105 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100106 } else {
107 // shouldn't happen
108 assert(0);
109 }
110 }
111 break;
112
113 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100114 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000115 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200116 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
117 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damien0efb3a12013-10-12 16:16:56 +0100118 machine_int_t res;
Damiend99b0522013-12-21 18:17:45 +0000119 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100120 res = arg0 + arg1;
Damiend99b0522013-12-21 18:17:45 +0000121 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100122 res = arg0 - arg1;
Damien429d7192013-10-04 19:53:11 +0100123 } else {
124 // shouldn't happen
125 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100126 res = 0;
127 }
Paul Sokolovskybbf0e2f2014-02-21 02:04:32 +0200128 if (MP_PARSE_FITS_SMALL_INT(res)) {
Damiend99b0522013-12-21 18:17:45 +0000129 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, res);
Damien429d7192013-10-04 19:53:11 +0100130 }
131 }
132 break;
133
134 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000135 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200136 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
137 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000138 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien3ef4abb2013-10-12 16:53:13 +0100139#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100140 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000141 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 * arg1);
Damien0efb3a12013-10-12 16:16:56 +0100142#endif
Damiend99b0522013-12-21 18:17:45 +0000143 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien429d7192013-10-04 19:53:11 +0100144 ; // pass
Damiend99b0522013-12-21 18:17:45 +0000145 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Rachel Dowdallcde86312014-03-22 17:29:27 +0000146 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, python_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000147 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Paul Sokolovsky96eec4f2014-03-31 02:16:25 +0300148 if (arg1 != 0) {
149 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, python_floor_divide(arg0, arg1));
150 }
Damien429d7192013-10-04 19:53:11 +0100151 } else {
152 // shouldn't happen
153 assert(0);
154 }
155 }
156 break;
157
158 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000159 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200160 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000161 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
162 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
163 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
164 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
165 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
166 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100167 } else {
168 // shouldn't happen
169 assert(0);
170 }
171 }
172 break;
173
Damien3ef4abb2013-10-12 16:53:13 +0100174#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +0100175 case PN_power:
Damien0efb3a12013-10-12 16:16:56 +0100176 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000177 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_NULL(pns->nodes[1]) && !MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
178 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
179 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200180 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100181 if (power >= 0) {
182 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200183 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100184 for (; power > 0; power--) {
185 ans *= base;
186 }
Damiend99b0522013-12-21 18:17:45 +0000187 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100188 }
189 }
190 }
191 break;
Damien0efb3a12013-10-12 16:16:56 +0100192#endif
Damien429d7192013-10-04 19:53:11 +0100193 }
194 }
195
196 return pn;
197}
198
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200199STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra);
Damien George8dcc0c72014-03-27 10:55:21 +0000200STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100201
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200202STATIC int comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100203 return comp->next_label++;
204}
205
Damien George8dcc0c72014-03-27 10:55:21 +0000206STATIC void compile_increase_except_level(compiler_t *comp) {
207 comp->cur_except_level += 1;
208 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
209 comp->scope_cur->exc_stack_size = comp->cur_except_level;
210 }
211}
212
213STATIC void compile_decrease_except_level(compiler_t *comp) {
214 assert(comp->cur_except_level > 0);
215 comp->cur_except_level -= 1;
216}
217
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200218STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) {
Damien George2326d522014-03-27 23:26:35 +0000219 scope_t *scope = scope_new(kind, pn, comp->source_file, mp_emit_glue_get_unique_code_id(), emit_options);
Damien429d7192013-10-04 19:53:11 +0100220 scope->parent = comp->scope_cur;
221 scope->next = NULL;
222 if (comp->scope_head == NULL) {
223 comp->scope_head = scope;
224 } else {
225 scope_t *s = comp->scope_head;
226 while (s->next != NULL) {
227 s = s->next;
228 }
229 s->next = scope;
230 }
231 return scope;
232}
233
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200234STATIC int list_len(mp_parse_node_t pn, int pn_kind) {
Damiend99b0522013-12-21 18:17:45 +0000235 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100236 return 0;
Damiend99b0522013-12-21 18:17:45 +0000237 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100238 return 1;
239 } else {
Damiend99b0522013-12-21 18:17:45 +0000240 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
241 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100242 return 1;
243 } else {
Damiend99b0522013-12-21 18:17:45 +0000244 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100245 }
246 }
247}
248
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200249STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, int pn_list_kind, void (*f)(compiler_t*, mp_parse_node_t)) {
Damiend99b0522013-12-21 18:17:45 +0000250 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
251 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
252 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100253 for (int i = 0; i < num_nodes; i++) {
254 f(comp, pns->nodes[i]);
255 }
Damiend99b0522013-12-21 18:17:45 +0000256 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100257 f(comp, pn);
258 }
259}
260
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200261STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000262 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100263 *nodes = NULL;
264 return 0;
Damiend99b0522013-12-21 18:17:45 +0000265 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100266 *nodes = pn;
267 return 1;
268 } else {
Damiend99b0522013-12-21 18:17:45 +0000269 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
270 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100271 *nodes = pn;
272 return 1;
273 } else {
274 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000275 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100276 }
277 }
278}
279
Damiend99b0522013-12-21 18:17:45 +0000280void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100281}
282
Damiend99b0522013-12-21 18:17:45 +0000283void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
284 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100285 for (int i = 0; i < num_nodes; i++) {
286 compile_node(comp, pns->nodes[i]);
287 }
288}
289
Damien3ef4abb2013-10-12 16:53:13 +0100290#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200291STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000292 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100293 return false;
294 }
Damiend99b0522013-12-21 18:17:45 +0000295 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100296 return false;
297 }
298 return true;
299}
300
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200301STATIC void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
Damien George55baff42014-01-21 21:40:13 +0000302 uint len;
303 const byte *str = qstr_data(qstr, &len);
Damien02f89412013-12-12 15:13:36 +0000304 bool has_single_quote = false;
305 bool has_double_quote = false;
306 for (int i = 0; i < len; i++) {
307 if (str[i] == '\'') {
308 has_single_quote = true;
309 } else if (str[i] == '"') {
310 has_double_quote = true;
311 }
312 }
313 if (bytes) {
314 vstr_printf(vstr, "b");
315 }
316 bool quote_single = false;
317 if (has_single_quote && !has_double_quote) {
318 vstr_printf(vstr, "\"");
319 } else {
320 quote_single = true;
321 vstr_printf(vstr, "'");
322 }
323 for (int i = 0; i < len; i++) {
324 if (str[i] == '\n') {
325 vstr_printf(vstr, "\\n");
326 } else if (str[i] == '\\') {
327 vstr_printf(vstr, "\\\\");
328 } else if (str[i] == '\'' && quote_single) {
329 vstr_printf(vstr, "\\'");
330 } else {
331 vstr_printf(vstr, "%c", str[i]);
332 }
333 }
334 if (has_single_quote && !has_double_quote) {
335 vstr_printf(vstr, "\"");
336 } else {
337 vstr_printf(vstr, "'");
338 }
339}
340
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200341STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damiend99b0522013-12-21 18:17:45 +0000342 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200343 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
344 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
345 return;
346 }
347
Damiend99b0522013-12-21 18:17:45 +0000348 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
349 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
350 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000351 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
352 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
353 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
354 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
355 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100356 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000357 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
358 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
359 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien429d7192013-10-04 19:53:11 +0100360 default: assert(0);
361 }
362 break;
363 default: assert(0);
364 }
365}
366
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200367STATIC void cpython_c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien429d7192013-10-04 19:53:11 +0100368 int n = 0;
369 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000370 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100371 }
372 int total = n;
373 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000374 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100375 total += 1;
Damien3a205172013-10-12 15:01:56 +0100376 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100377 is_const = false;
378 }
379 }
380 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100381 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100382 is_const = false;
383 break;
384 }
385 }
386 if (total > 0 && is_const) {
387 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000388 vstr_t *vstr = vstr_new();
389 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000390 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000391 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100392 need_comma = true;
393 }
394 for (int i = 0; i < n; i++) {
395 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000396 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100397 }
Damien02f89412013-12-12 15:13:36 +0000398 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100399 need_comma = true;
400 }
401 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000402 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100403 } else {
Damien02f89412013-12-12 15:13:36 +0000404 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100405 }
Damien Georgeb9791222014-01-23 00:34:21 +0000406 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000407 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100408 } else {
Damiend99b0522013-12-21 18:17:45 +0000409 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100410 compile_node(comp, pn);
411 }
412 for (int i = 0; i < n; i++) {
413 compile_node(comp, pns_list->nodes[i]);
414 }
Damien Georgeb9791222014-01-23 00:34:21 +0000415 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100416 }
417}
Damien3a205172013-10-12 15:01:56 +0100418#endif
419
420// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damiend99b0522013-12-21 18:17:45 +0000421void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100422#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100423 cpython_c_tuple(comp, pn, pns_list);
424#else
425 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000426 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100427 compile_node(comp, pn);
428 total += 1;
429 }
430 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000431 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100432 for (int i = 0; i < n; i++) {
433 compile_node(comp, pns_list->nodes[i]);
434 }
435 total += n;
436 }
Damien Georgeb9791222014-01-23 00:34:21 +0000437 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100438#endif
439}
Damien429d7192013-10-04 19:53:11 +0100440
Damiend99b0522013-12-21 18:17:45 +0000441void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100442 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000443 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100444}
445
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200446STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000447 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200448 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100449}
450
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200451STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200452 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE) || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 1);
Damien429d7192013-10-04 19:53:11 +0100453}
454
Damien3ef4abb2013-10-12 16:53:13 +0100455#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100456// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200457STATIC void cpython_c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label, bool is_nested) {
Damien429d7192013-10-04 19:53:11 +0100458 if (node_is_const_false(pn)) {
459 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000460 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100461 }
462 return;
463 } else if (node_is_const_true(pn)) {
464 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000465 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100466 }
467 return;
Damiend99b0522013-12-21 18:17:45 +0000468 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
469 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
470 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
471 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100472 if (jump_if == false) {
Damienb05d7072013-10-05 13:37:10 +0100473 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100474 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100475 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100476 }
Damien3a205172013-10-12 15:01:56 +0100477 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000478 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100479 } else {
480 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100481 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100482 }
483 }
484 return;
Damiend99b0522013-12-21 18:17:45 +0000485 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100486 if (jump_if == false) {
487 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100488 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100489 }
490 } else {
Damienb05d7072013-10-05 13:37:10 +0100491 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100492 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100493 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100494 }
Damien3a205172013-10-12 15:01:56 +0100495 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000496 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100497 }
498 return;
Damiend99b0522013-12-21 18:17:45 +0000499 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100500 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100501 return;
502 }
503 }
504
505 // nothing special, fall back to default compiling for node and jump
506 compile_node(comp, pn);
507 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000508 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100509 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000510 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100511 }
512}
Damien3a205172013-10-12 15:01:56 +0100513#endif
Damien429d7192013-10-04 19:53:11 +0100514
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200515STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100516#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100517 cpython_c_if_cond(comp, pn, jump_if, label, false);
518#else
519 if (node_is_const_false(pn)) {
520 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000521 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100522 }
523 return;
524 } else if (node_is_const_true(pn)) {
525 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000526 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100527 }
528 return;
Damiend99b0522013-12-21 18:17:45 +0000529 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
530 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
531 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
532 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100533 if (jump_if == false) {
534 int label2 = comp_next_label(comp);
535 for (int i = 0; i < n - 1; i++) {
536 c_if_cond(comp, pns->nodes[i], true, label2);
537 }
538 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000539 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100540 } else {
541 for (int i = 0; i < n; i++) {
542 c_if_cond(comp, pns->nodes[i], true, label);
543 }
544 }
545 return;
Damiend99b0522013-12-21 18:17:45 +0000546 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100547 if (jump_if == false) {
548 for (int i = 0; i < n; i++) {
549 c_if_cond(comp, pns->nodes[i], false, label);
550 }
551 } else {
552 int label2 = comp_next_label(comp);
553 for (int i = 0; i < n - 1; i++) {
554 c_if_cond(comp, pns->nodes[i], false, label2);
555 }
556 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000557 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100558 }
559 return;
Damiend99b0522013-12-21 18:17:45 +0000560 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100561 c_if_cond(comp, pns->nodes[0], !jump_if, label);
562 return;
563 }
564 }
565
566 // nothing special, fall back to default compiling for node and jump
567 compile_node(comp, pn);
568 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000569 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100570 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000571 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100572 }
573#endif
Damien429d7192013-10-04 19:53:11 +0100574}
575
576typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000577void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100578
Damiend99b0522013-12-21 18:17:45 +0000579void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100580 if (assign_kind != ASSIGN_AUG_STORE) {
581 compile_node(comp, pns->nodes[0]);
582 }
583
Damiend99b0522013-12-21 18:17:45 +0000584 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
585 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
586 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
587 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100588 if (assign_kind != ASSIGN_AUG_STORE) {
589 for (int i = 0; i < n - 1; i++) {
590 compile_node(comp, pns1->nodes[i]);
591 }
592 }
Damiend99b0522013-12-21 18:17:45 +0000593 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
594 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100595 }
Damiend99b0522013-12-21 18:17:45 +0000596 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000597 compile_syntax_error(comp, "can't assign to function call");
Damien429d7192013-10-04 19:53:11 +0100598 return;
Damiend99b0522013-12-21 18:17:45 +0000599 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100600 if (assign_kind == ASSIGN_AUG_STORE) {
601 EMIT(rot_three);
602 EMIT(store_subscr);
603 } else {
604 compile_node(comp, pns1->nodes[0]);
605 if (assign_kind == ASSIGN_AUG_LOAD) {
606 EMIT(dup_top_two);
Damien Georged17926d2014-03-30 13:35:08 +0100607 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100608 } else {
609 EMIT(store_subscr);
610 }
611 }
Damiend99b0522013-12-21 18:17:45 +0000612 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
613 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100614 if (assign_kind == ASSIGN_AUG_LOAD) {
615 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000616 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100617 } else {
618 if (assign_kind == ASSIGN_AUG_STORE) {
619 EMIT(rot_two);
620 }
Damien Georgeb9791222014-01-23 00:34:21 +0000621 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100622 }
623 } else {
624 // shouldn't happen
625 assert(0);
626 }
627 } else {
628 // shouldn't happen
629 assert(0);
630 }
631
Damiend99b0522013-12-21 18:17:45 +0000632 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +0100633 // SyntaxError, cannot assign
634 assert(0);
635 }
636}
637
Damiend99b0522013-12-21 18:17:45 +0000638void c_assign_tuple(compiler_t *comp, int n, mp_parse_node_t *nodes) {
Damien429d7192013-10-04 19:53:11 +0100639 assert(n >= 0);
640 int have_star_index = -1;
641 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000642 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100643 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000644 EMIT_ARG(unpack_ex, i, n - i - 1);
Damien429d7192013-10-04 19:53:11 +0100645 have_star_index = i;
646 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000647 compile_syntax_error(comp, "two starred expressions in assignment");
Damien429d7192013-10-04 19:53:11 +0100648 return;
649 }
650 }
651 }
652 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000653 EMIT_ARG(unpack_sequence, n);
Damien429d7192013-10-04 19:53:11 +0100654 }
655 for (int i = 0; i < n; i++) {
656 if (i == have_star_index) {
Damiend99b0522013-12-21 18:17:45 +0000657 c_assign(comp, ((mp_parse_node_struct_t*)nodes[i])->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100658 } else {
659 c_assign(comp, nodes[i], ASSIGN_STORE);
660 }
661 }
662}
663
664// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000665void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100666 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000667 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100668 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000669 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
670 if (MP_PARSE_NODE_IS_ID(pn)) {
671 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100672 switch (assign_kind) {
673 case ASSIGN_STORE:
674 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000675 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100676 break;
677 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000678 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100679 break;
680 }
681 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000682 compile_syntax_error(comp, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100683 return;
684 }
685 } else {
Damiend99b0522013-12-21 18:17:45 +0000686 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
687 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100688 case PN_power:
689 // lhs is an index or attribute
690 c_assign_power(comp, pns, assign_kind);
691 break;
692
693 case PN_testlist_star_expr:
694 case PN_exprlist:
695 // lhs is a tuple
696 if (assign_kind != ASSIGN_STORE) {
697 goto bad_aug;
698 }
Damiend99b0522013-12-21 18:17:45 +0000699 c_assign_tuple(comp, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100700 break;
701
702 case PN_atom_paren:
703 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000704 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100705 // empty tuple
Damien Georgef41fdd02014-03-03 23:19:11 +0000706 compile_syntax_error(comp, "can't assign to ()");
Damien429d7192013-10-04 19:53:11 +0100707 return;
Damiend99b0522013-12-21 18:17:45 +0000708 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
709 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100710 goto testlist_comp;
711 } else {
712 // parenthesis around 1 item, is just that item
713 pn = pns->nodes[0];
714 goto tail_recursion;
715 }
716 break;
717
718 case PN_atom_bracket:
719 // lhs is something in brackets
720 if (assign_kind != ASSIGN_STORE) {
721 goto bad_aug;
722 }
Damiend99b0522013-12-21 18:17:45 +0000723 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100724 // empty list, assignment allowed
725 c_assign_tuple(comp, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000726 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
727 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100728 goto testlist_comp;
729 } else {
730 // brackets around 1 item
731 c_assign_tuple(comp, 1, &pns->nodes[0]);
732 }
733 break;
734
735 default:
Damiend99b0522013-12-21 18:17:45 +0000736 printf("unknown assign, %u\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien429d7192013-10-04 19:53:11 +0100737 assert(0);
738 }
739 return;
740
741 testlist_comp:
742 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000743 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
744 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
745 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100746 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000747 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100748 c_assign_tuple(comp, 1, &pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000749 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100750 // sequence of many items
751 // TODO call c_assign_tuple instead
Damiend99b0522013-12-21 18:17:45 +0000752 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
Damien Georgeb9791222014-01-23 00:34:21 +0000753 EMIT_ARG(unpack_sequence, 1 + n);
Damien429d7192013-10-04 19:53:11 +0100754 c_assign(comp, pns->nodes[0], ASSIGN_STORE);
755 for (int i = 0; i < n; i++) {
756 c_assign(comp, pns2->nodes[i], ASSIGN_STORE);
757 }
Damiend99b0522013-12-21 18:17:45 +0000758 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +0100759 // TODO not implemented
760 assert(0);
761 } else {
762 // sequence with 2 items
763 goto sequence_with_2_items;
764 }
765 } else {
766 // sequence with 2 items
767 sequence_with_2_items:
768 c_assign_tuple(comp, 2, pns->nodes);
769 }
770 return;
771 }
772 return;
773
774 bad_aug:
Damien Georgef41fdd02014-03-03 23:19:11 +0000775 compile_syntax_error(comp, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100776}
777
778// stuff for lambda and comprehensions and generators
Damien Georgee337f1e2014-03-31 15:18:37 +0100779// if we are not in CPython compatibility mode then:
780// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
781// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
782// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
Damien George30565092014-03-31 11:30:17 +0100783void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
784 assert(n_pos_defaults >= 0);
785 assert(n_kw_defaults >= 0);
786
Damien429d7192013-10-04 19:53:11 +0100787 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000788 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100789 int nfree = 0;
790 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000791 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
792 id_info_t *id = &comp->scope_cur->id_info[i];
793 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
794 for (int j = 0; j < this_scope->id_info_len; j++) {
795 id_info_t *id2 = &this_scope->id_info[j];
796 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000797#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000798 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000799#else
800 // in Micro Python we load closures using LOAD_FAST
Damien Georgeb9791222014-01-23 00:34:21 +0000801 EMIT_ARG(load_fast, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000802#endif
Damien318aec62013-12-10 18:28:17 +0000803 nfree += 1;
804 }
805 }
Damien429d7192013-10-04 19:53:11 +0100806 }
807 }
808 }
Damien429d7192013-10-04 19:53:11 +0100809
810 // make the function/closure
811 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100812 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100813 } else {
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000814 EMIT_ARG(build_tuple, nfree);
Damien George30565092014-03-31 11:30:17 +0100815 EMIT_ARG(make_closure, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100816 }
817}
818
Damiend99b0522013-12-21 18:17:45 +0000819void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000820 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damiend99b0522013-12-21 18:17:45 +0000821 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
822 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100823 // bare star
824 comp->have_bare_star = true;
825 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000826
827 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
828 // TODO do we need to do anything with this?
829
830 } else {
831 mp_parse_node_t pn_id;
832 mp_parse_node_t pn_colon;
833 mp_parse_node_t pn_equal;
834 if (MP_PARSE_NODE_IS_ID(pn)) {
835 // this parameter is just an id
836
837 pn_id = pn;
838 pn_colon = MP_PARSE_NODE_NULL;
839 pn_equal = MP_PARSE_NODE_NULL;
840
841 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
842 // this parameter has a colon and/or equal specifier
843
844 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
845 pn_id = pns->nodes[0];
846 pn_colon = pns->nodes[1];
847 pn_equal = pns->nodes[2];
848
849 } else {
850 assert(0);
851 return;
852 }
853
854 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
855 // this parameter does not have a default value
856
857 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
858 if (!comp->have_bare_star && comp->param_pass_num_default_params != 0) {
859 compile_syntax_error(comp, "non-default argument follows default argument");
860 return;
861 }
862
863 } else {
864 // this parameter has a default value
865 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
866
867 if (comp->have_bare_star) {
868 comp->param_pass_num_dict_params += 1;
869 if (comp->param_pass == 1) {
Damien Georgee337f1e2014-03-31 15:18:37 +0100870#if !MICROPY_EMIT_CPYTHON
871 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
872 if (comp->param_pass_num_dict_params == 1) {
873 // first default dict param, so make the map
874 EMIT_ARG(build_map, 0);
875 }
876#endif
Damien Georgef41fdd02014-03-03 23:19:11 +0000877 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pn_id));
878 compile_node(comp, pn_equal);
Damien Georgee337f1e2014-03-31 15:18:37 +0100879#if !MICROPY_EMIT_CPYTHON
880 // in Micro Python we put the default dict parameters into a dictionary using the bytecode
881 EMIT(store_map);
882#endif
Damien Georgef41fdd02014-03-03 23:19:11 +0000883 }
884 } else {
885 comp->param_pass_num_default_params += 1;
886 if (comp->param_pass == 2) {
887 compile_node(comp, pn_equal);
888 }
889 }
890 }
891
892 // TODO pn_colon not implemented
893 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100894 }
895}
896
897// leaves function object on stack
898// returns function name
Damiend99b0522013-12-21 18:17:45 +0000899qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100900 if (comp->pass == PASS_1) {
901 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000902 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100903 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000904 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100905 }
906
907 // save variables (probably don't need to do this, since we can't have nested definitions..?)
908 bool old_have_bare_star = comp->have_bare_star;
909 int old_param_pass = comp->param_pass;
910 int old_param_pass_num_dict_params = comp->param_pass_num_dict_params;
911 int old_param_pass_num_default_params = comp->param_pass_num_default_params;
912
913 // compile default parameters
Damien Georgef41fdd02014-03-03 23:19:11 +0000914
915 // pass 1 does any default parameters after bare star
Damien429d7192013-10-04 19:53:11 +0100916 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000917 comp->param_pass = 1;
Damien429d7192013-10-04 19:53:11 +0100918 comp->param_pass_num_dict_params = 0;
919 comp->param_pass_num_default_params = 0;
920 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +0000921
922 if (comp->had_error) {
923 return MP_QSTR_NULL;
924 }
925
926 // pass 2 does any default parameters before bare star
Damien429d7192013-10-04 19:53:11 +0100927 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000928 comp->param_pass = 2;
Damien429d7192013-10-04 19:53:11 +0100929 comp->param_pass_num_dict_params = 0;
930 comp->param_pass_num_default_params = 0;
931 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
932
Damien Georgee337f1e2014-03-31 15:18:37 +0100933#if !MICROPY_EMIT_CPYTHON
934 // in Micro Python we put the default positional parameters into a tuple using the bytecode
935 if (comp->param_pass_num_default_params > 0) {
936 EMIT_ARG(build_tuple, comp->param_pass_num_default_params);
937 }
938#endif
939
Damien429d7192013-10-04 19:53:11 +0100940 // get the scope for this function
941 scope_t *fscope = (scope_t*)pns->nodes[4];
942
943 // make the function
Damien George30565092014-03-31 11:30:17 +0100944 close_over_variables_etc(comp, fscope, comp->param_pass_num_default_params, comp->param_pass_num_dict_params);
Damien429d7192013-10-04 19:53:11 +0100945
946 // restore variables
947 comp->have_bare_star = old_have_bare_star;
948 comp->param_pass = old_param_pass;
949 comp->param_pass_num_dict_params = old_param_pass_num_dict_params;
950 comp->param_pass_num_default_params = old_param_pass_num_default_params;
951
952 // return its name (the 'f' in "def f(...):")
953 return fscope->simple_name;
954}
955
956// leaves class object on stack
957// returns class name
Damiend99b0522013-12-21 18:17:45 +0000958qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100959 if (comp->pass == PASS_1) {
960 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000961 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100962 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000963 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100964 }
965
966 EMIT(load_build_class);
967
968 // scope for this class
969 scope_t *cscope = (scope_t*)pns->nodes[3];
970
971 // compile the class
972 close_over_variables_etc(comp, cscope, 0, 0);
973
974 // get its name
Damien Georgeb9791222014-01-23 00:34:21 +0000975 EMIT_ARG(load_const_id, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100976
977 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +0100978 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
979 mp_parse_node_t parents = pns->nodes[1];
980 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
981 parents = MP_PARSE_NODE_NULL;
982 }
Damien Georgebbcd49a2014-02-06 20:30:16 +0000983 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +0100984 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +0100985
986 // return its name (the 'C' in class C(...):")
987 return cscope->simple_name;
988}
989
Damien6cdd3af2013-10-05 18:08:26 +0100990// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200991STATIC bool compile_built_in_decorator(compiler_t *comp, int name_len, mp_parse_node_t *name_nodes, uint *emit_options) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000992 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100993 return false;
994 }
995
996 if (name_len != 2) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000997 compile_syntax_error(comp, "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100998 return true;
999 }
1000
Damiend99b0522013-12-21 18:17:45 +00001001 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001002 if (attr == MP_QSTR_byte_code) {
Damien5ac1b2e2013-10-18 19:58:12 +01001003 *emit_options = EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +01001004#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001005 } else if (attr == MP_QSTR_native) {
Damien6cdd3af2013-10-05 18:08:26 +01001006 *emit_options = EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001007 } else if (attr == MP_QSTR_viper) {
Damien7af3d192013-10-07 00:02:49 +01001008 *emit_options = EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +01001009#endif
Damien3ef4abb2013-10-12 16:53:13 +01001010#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001011 } else if (attr == MP_QSTR_asm_thumb) {
Damien5bfb7592013-10-05 18:41:24 +01001012 *emit_options = EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +01001013#endif
Damien6cdd3af2013-10-05 18:08:26 +01001014 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +00001015 compile_syntax_error(comp, "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +01001016 }
1017
1018 return true;
1019}
1020
Damiend99b0522013-12-21 18:17:45 +00001021void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001022 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001023 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001024 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1025
Damien6cdd3af2013-10-05 18:08:26 +01001026 // inherit emit options for this function/class definition
1027 uint emit_options = comp->scope_cur->emit_options;
1028
1029 // compile each decorator
1030 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001031 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001032 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1033 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001034
1035 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001036 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001037 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1038
1039 // check for built-in decorators
1040 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1041 // this was a built-in
1042 num_built_in_decorators += 1;
1043
1044 } else {
1045 // not a built-in, compile normally
1046
1047 // compile the decorator function
1048 compile_node(comp, name_nodes[0]);
1049 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001050 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001051 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001052 }
1053
1054 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001055 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001056 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001057 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001058 compile_node(comp, pns_decorator->nodes[1]);
1059 }
Damien429d7192013-10-04 19:53:11 +01001060 }
1061 }
1062
1063 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001064 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001065 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001066 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001067 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001068 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001069 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001070 } else {
1071 // shouldn't happen
1072 assert(0);
1073 }
1074
1075 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001076 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001077 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01001078 }
1079
1080 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001081 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001082}
1083
Damiend99b0522013-12-21 18:17:45 +00001084void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001085 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001086 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001087 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001088}
1089
Damiend99b0522013-12-21 18:17:45 +00001090void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1091 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001092 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001093 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1094 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001095
1096 compile_node(comp, pns->nodes[0]); // base of the power node
1097
Damiend99b0522013-12-21 18:17:45 +00001098 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1099 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1100 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1101 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001102 for (int i = 0; i < n - 1; i++) {
1103 compile_node(comp, pns1->nodes[i]);
1104 }
Damiend99b0522013-12-21 18:17:45 +00001105 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1106 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001107 }
Damiend99b0522013-12-21 18:17:45 +00001108 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien429d7192013-10-04 19:53:11 +01001109 // SyntaxError: can't delete a function call
1110 assert(0);
Damiend99b0522013-12-21 18:17:45 +00001111 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001112 compile_node(comp, pns1->nodes[0]);
1113 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001114 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1115 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001116 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001117 } else {
1118 // shouldn't happen
1119 assert(0);
1120 }
1121 } else {
1122 // shouldn't happen
1123 assert(0);
1124 }
1125
Damiend99b0522013-12-21 18:17:45 +00001126 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001127 // SyntaxError, cannot delete
1128 assert(0);
1129 }
Damiend99b0522013-12-21 18:17:45 +00001130 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1131 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1132 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1133 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001134 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1135
Damiend99b0522013-12-21 18:17:45 +00001136 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1137 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1138 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001139 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001140 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001141 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001142 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001143 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001144 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001145 c_del_stmt(comp, pns->nodes[0]);
1146 for (int i = 0; i < n; i++) {
1147 c_del_stmt(comp, pns1->nodes[i]);
1148 }
Damiend99b0522013-12-21 18:17:45 +00001149 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001150 // TODO not implemented; can't del comprehension?
1151 assert(0);
1152 } else {
1153 // sequence with 2 items
1154 goto sequence_with_2_items;
1155 }
1156 } else {
1157 // sequence with 2 items
1158 sequence_with_2_items:
1159 c_del_stmt(comp, pns->nodes[0]);
1160 c_del_stmt(comp, pns->nodes[1]);
1161 }
1162 } else {
1163 // tuple with 1 element
1164 c_del_stmt(comp, pn);
1165 }
1166 } else {
1167 // not implemented
1168 assert(0);
1169 }
1170}
1171
Damiend99b0522013-12-21 18:17:45 +00001172void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001173 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1174}
1175
Damiend99b0522013-12-21 18:17:45 +00001176void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001177 if (comp->break_label == 0) {
Damien George2326d522014-03-27 23:26:35 +00001178 compile_syntax_error(comp, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001179 }
Damien Georgecbddb272014-02-01 20:08:18 +00001180 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001181}
1182
Damiend99b0522013-12-21 18:17:45 +00001183void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001184 if (comp->continue_label == 0) {
Damien George2326d522014-03-27 23:26:35 +00001185 compile_syntax_error(comp, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001186 }
Damien Georgecbddb272014-02-01 20:08:18 +00001187 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001188}
1189
Damiend99b0522013-12-21 18:17:45 +00001190void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001191 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001192 compile_syntax_error(comp, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001193 return;
1194 }
Damiend99b0522013-12-21 18:17:45 +00001195 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001196 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001197 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001198 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001199 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001200 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1201 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns_test_if_expr->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001202
Damienb05d7072013-10-05 13:37:10 +01001203 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001204 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1205 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1206 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001207 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001208 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1209 } else {
1210 compile_node(comp, pns->nodes[0]);
1211 }
1212 EMIT(return_value);
1213}
1214
Damiend99b0522013-12-21 18:17:45 +00001215void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001216 compile_node(comp, pns->nodes[0]);
1217 EMIT(pop_top);
1218}
1219
Damiend99b0522013-12-21 18:17:45 +00001220void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1221 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001222 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001223 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001224 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001225 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001226 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001227 compile_node(comp, pns->nodes[0]);
1228 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001229 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001230 } else {
1231 // raise x
1232 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001233 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001234 }
1235}
1236
1237// q1 holds the base, q2 the full name
1238// eg a -> q1=q2=a
1239// a.b.c -> q1=a, q2=a.b.c
Damiend99b0522013-12-21 18:17:45 +00001240void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q1, qstr *q2) {
Damien429d7192013-10-04 19:53:11 +01001241 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001242 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1243 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001244 // a name of the form x as y; unwrap it
Damiend99b0522013-12-21 18:17:45 +00001245 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001246 pn = pns->nodes[0];
1247 is_as = true;
1248 }
Damiend99b0522013-12-21 18:17:45 +00001249 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001250 // just a simple name
Damiend99b0522013-12-21 18:17:45 +00001251 *q2 = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001252 if (!is_as) {
1253 *q1 = *q2;
1254 }
Damien Georgeb9791222014-01-23 00:34:21 +00001255 EMIT_ARG(import_name, *q2);
Damiend99b0522013-12-21 18:17:45 +00001256 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1257 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1258 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001259 // a name of the form a.b.c
1260 if (!is_as) {
Damiend99b0522013-12-21 18:17:45 +00001261 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001262 }
Damiend99b0522013-12-21 18:17:45 +00001263 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001264 int len = n - 1;
1265 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001266 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001267 }
Damien George55baff42014-01-21 21:40:13 +00001268 byte *q_ptr;
1269 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001270 for (int i = 0; i < n; i++) {
1271 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001272 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001273 }
Damien George55baff42014-01-21 21:40:13 +00001274 uint str_src_len;
1275 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001276 memcpy(str_dest, str_src, str_src_len);
1277 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001278 }
Damien George55baff42014-01-21 21:40:13 +00001279 *q2 = qstr_build_end(q_ptr);
Damien Georgeb9791222014-01-23 00:34:21 +00001280 EMIT_ARG(import_name, *q2);
Damien429d7192013-10-04 19:53:11 +01001281 if (is_as) {
1282 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001283 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001284 }
1285 }
1286 } else {
1287 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001288 // This covers relative imports starting with dot(s) like "from .foo import"
Paul Sokolovsky8d9cc2e2014-03-30 02:31:08 +02001289 compile_syntax_error(comp, "Relative imports not implemented");
Damien429d7192013-10-04 19:53:11 +01001290 assert(0);
1291 }
1292 } else {
1293 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001294 // This covers relative imports with dots only like "from .. import"
Paul Sokolovsky8d9cc2e2014-03-30 02:31:08 +02001295 compile_syntax_error(comp, "Relative imports not implemented");
Damien429d7192013-10-04 19:53:11 +01001296 assert(0);
1297 }
1298}
1299
Damiend99b0522013-12-21 18:17:45 +00001300void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgeb9791222014-01-23 00:34:21 +00001301 EMIT_ARG(load_const_small_int, 0); // ??
1302 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001303 qstr q1, q2;
1304 do_import_name(comp, pn, &q1, &q2);
Damien Georgeb9791222014-01-23 00:34:21 +00001305 EMIT_ARG(store_id, q1);
Damien429d7192013-10-04 19:53:11 +01001306}
1307
Damiend99b0522013-12-21 18:17:45 +00001308void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001309 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1310}
1311
Damiend99b0522013-12-21 18:17:45 +00001312void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
1313 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001314 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001315
1316 // build the "fromlist" tuple
1317#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001318 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001319#else
Damien Georgeb9791222014-01-23 00:34:21 +00001320 EMIT_ARG(load_const_str, QSTR_FROM_STR_STATIC("*"), false);
1321 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001322#endif
1323
1324 // do the import
Damien429d7192013-10-04 19:53:11 +01001325 qstr dummy_q, id1;
1326 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1327 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001328
Damien429d7192013-10-04 19:53:11 +01001329 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001330 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001331
1332 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001333 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001334 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001335#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001336 {
1337 vstr_t *vstr = vstr_new();
1338 vstr_printf(vstr, "(");
1339 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001340 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1341 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1342 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001343 if (i > 0) {
1344 vstr_printf(vstr, ", ");
1345 }
1346 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001347 uint len;
1348 const byte *str = qstr_data(id2, &len);
1349 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001350 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001351 }
Damien02f89412013-12-12 15:13:36 +00001352 if (n == 1) {
1353 vstr_printf(vstr, ",");
1354 }
1355 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001356 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001357 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001358 }
Damiendb4c3612013-12-10 17:27:24 +00001359#else
1360 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001361 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1362 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1363 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001364 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001365 }
Damien Georgeb9791222014-01-23 00:34:21 +00001366 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001367#endif
1368
1369 // do the import
Damien429d7192013-10-04 19:53:11 +01001370 qstr dummy_q, id1;
1371 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1372 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001373 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1374 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1375 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001376 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001377 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001378 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001379 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001380 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001381 }
1382 }
1383 EMIT(pop_top);
1384 }
1385}
1386
Damiend99b0522013-12-21 18:17:45 +00001387void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001388 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001389 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1390 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001391 } else {
Damiend99b0522013-12-21 18:17:45 +00001392 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1393 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001394 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001395 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001396 }
Damien429d7192013-10-04 19:53:11 +01001397 }
1398 }
1399}
1400
Damiend99b0522013-12-21 18:17:45 +00001401void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001402 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001403 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1404 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001405 } else {
Damiend99b0522013-12-21 18:17:45 +00001406 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1407 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001408 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001409 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001410 }
Damien429d7192013-10-04 19:53:11 +01001411 }
1412 }
1413}
1414
Damiend99b0522013-12-21 18:17:45 +00001415void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001416 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001417 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001418 EMIT_ARG(load_global, MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001419 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001420 // assertion message
1421 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001422 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01001423 }
Damien Georgeb9791222014-01-23 00:34:21 +00001424 EMIT_ARG(raise_varargs, 1);
1425 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001426}
1427
Damiend99b0522013-12-21 18:17:45 +00001428void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001429 // TODO proper and/or short circuiting
1430
Damienb05d7072013-10-05 13:37:10 +01001431 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001432
Damienb05d7072013-10-05 13:37:10 +01001433 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001434 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1435
1436 compile_node(comp, pns->nodes[1]); // if block
Damiend99b0522013-12-21 18:17:45 +00001437 //if (!(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3]))) { // optimisation; doesn't align with CPython
Damien429d7192013-10-04 19:53:11 +01001438 // jump over elif/else blocks if they exist
Damien415eb6f2013-10-05 12:19:06 +01001439 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001440 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001441 }
1442 //}
Damien Georgeb9791222014-01-23 00:34:21 +00001443 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001444
Damiend99b0522013-12-21 18:17:45 +00001445 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001446 // compile elif blocks
1447
Damiend99b0522013-12-21 18:17:45 +00001448 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001449
Damiend99b0522013-12-21 18:17:45 +00001450 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001451 // multiple elif blocks
1452
Damiend99b0522013-12-21 18:17:45 +00001453 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001454 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001455 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001456 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001457 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1458
1459 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001460 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001461 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001462 }
Damien Georgeb9791222014-01-23 00:34:21 +00001463 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001464 }
1465
1466 } else {
1467 // a single elif block
1468
Damienb05d7072013-10-05 13:37:10 +01001469 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001470 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1471
1472 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001473 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001474 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001475 }
Damien Georgeb9791222014-01-23 00:34:21 +00001476 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001477 }
1478 }
1479
1480 // compile else block
1481 compile_node(comp, pns->nodes[3]); // can be null
1482
Damien Georgeb9791222014-01-23 00:34:21 +00001483 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001484}
1485
Damien Georgecbddb272014-02-01 20:08:18 +00001486#define START_BREAK_CONTINUE_BLOCK \
1487 int old_break_label = comp->break_label; \
1488 int old_continue_label = comp->continue_label; \
1489 int break_label = comp_next_label(comp); \
1490 int continue_label = comp_next_label(comp); \
1491 comp->break_label = break_label; \
1492 comp->continue_label = continue_label; \
1493 comp->break_continue_except_level = comp->cur_except_level;
1494
1495#define END_BREAK_CONTINUE_BLOCK \
1496 comp->break_label = old_break_label; \
1497 comp->continue_label = old_continue_label; \
1498 comp->break_continue_except_level = comp->cur_except_level;
1499
Damiend99b0522013-12-21 18:17:45 +00001500void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001501 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001502
Damience89a212013-10-15 22:25:17 +01001503 // compared to CPython, we have an optimised version of while loops
1504#if MICROPY_EMIT_CPYTHON
1505 int done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001506 EMIT_ARG(setup_loop, break_label);
1507 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001508 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1509 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001510 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001511 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001512 }
Damien Georgeb9791222014-01-23 00:34:21 +00001513 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001514 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1515 // this is a small hack to agree with CPython
1516 if (!node_is_const_true(pns->nodes[0])) {
1517 EMIT(pop_block);
1518 }
Damience89a212013-10-15 22:25:17 +01001519#else
1520 int top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001521 EMIT_ARG(jump, continue_label);
1522 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001523 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001524 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001525 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1526#endif
1527
1528 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001529 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001530
1531 compile_node(comp, pns->nodes[2]); // else
1532
Damien Georgeb9791222014-01-23 00:34:21 +00001533 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001534}
1535
Damienf72fd0e2013-11-06 20:20:49 +00001536// TODO preload end and step onto stack if they are not constants
1537// TODO check if step is negative and do opposite test
Damiend99b0522013-12-21 18:17:45 +00001538void 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 +00001539 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001540
1541 int top_label = comp_next_label(comp);
Damien George600ae732014-01-21 23:48:04 +00001542 int entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001543
1544 // compile: var = start
1545 compile_node(comp, pn_start);
1546 c_assign(comp, pn_var, ASSIGN_STORE);
1547
Damien Georgeb9791222014-01-23 00:34:21 +00001548 EMIT_ARG(jump, entry_label);
1549 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001550
Damienf3822fc2013-11-09 20:12:03 +00001551 // compile body
1552 compile_node(comp, pn_body);
1553
Damien Georgeb9791222014-01-23 00:34:21 +00001554 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001555
Damienf72fd0e2013-11-06 20:20:49 +00001556 // compile: var += step
1557 c_assign(comp, pn_var, ASSIGN_AUG_LOAD);
1558 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001559 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001560 c_assign(comp, pn_var, ASSIGN_AUG_STORE);
1561
Damien Georgeb9791222014-01-23 00:34:21 +00001562 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001563
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001564 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001565 compile_node(comp, pn_var);
1566 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001567 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1568 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001569 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001570 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001571 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001572 }
Damien Georgeb9791222014-01-23 00:34:21 +00001573 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001574
1575 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001576 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001577
1578 compile_node(comp, pn_else);
1579
Damien Georgeb9791222014-01-23 00:34:21 +00001580 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001581}
1582
Damiend99b0522013-12-21 18:17:45 +00001583void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001584#if !MICROPY_EMIT_CPYTHON
1585 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1586 // this is actually slower, but uses no heap memory
1587 // for viper it will be much, much faster
Damiend99b0522013-12-21 18:17:45 +00001588 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)) {
1589 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001590 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1591 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1592 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1593 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001594 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1595 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001596 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001597 mp_parse_node_t pn_range_start;
1598 mp_parse_node_t pn_range_end;
1599 mp_parse_node_t pn_range_step;
1600 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001601 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001602 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001603 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001604 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001605 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001606 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001607 } else if (n_args == 2) {
1608 pn_range_start = args[0];
1609 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001610 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001611 } else {
1612 pn_range_start = args[0];
1613 pn_range_end = args[1];
1614 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001615 // We need to know sign of step. This is possible only if it's constant
1616 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1617 optimize = false;
1618 }
Damienf72fd0e2013-11-06 20:20:49 +00001619 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001620 }
1621 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001622 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1623 return;
1624 }
1625 }
1626 }
1627#endif
1628
Damien Georgecbddb272014-02-01 20:08:18 +00001629 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001630
Damienb05d7072013-10-05 13:37:10 +01001631 int pop_label = comp_next_label(comp);
1632 int end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001633
Damience89a212013-10-15 22:25:17 +01001634 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1635#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001636 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001637#endif
1638
Damien429d7192013-10-04 19:53:11 +01001639 compile_node(comp, pns->nodes[1]); // iterator
1640 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001641 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001642 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001643 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1644 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001645 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001646 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001647 }
Damien Georgeb9791222014-01-23 00:34:21 +00001648 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001649 EMIT(for_iter_end);
1650
1651 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001652 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001653
Damience89a212013-10-15 22:25:17 +01001654#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001655 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001656#endif
Damien429d7192013-10-04 19:53:11 +01001657
1658 compile_node(comp, pns->nodes[3]); // else (not tested)
1659
Damien Georgeb9791222014-01-23 00:34:21 +00001660 EMIT_ARG(label_assign, break_label);
1661 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001662}
1663
Damiend99b0522013-12-21 18:17:45 +00001664void 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 +01001665 // this function is a bit of a hack at the moment
1666 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1667
1668 // setup code
1669 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001670 int l1 = comp_next_label(comp);
1671 int success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001672
Damien Georgeb9791222014-01-23 00:34:21 +00001673 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001674 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001675
Damien429d7192013-10-04 19:53:11 +01001676 compile_node(comp, pn_body); // body
1677 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001678 EMIT_ARG(jump, success_label);
1679 EMIT_ARG(label_assign, l1);
Damienb05d7072013-10-05 13:37:10 +01001680 int l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001681
1682 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001683 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1684 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001685
1686 qstr qstr_exception_local = 0;
Damienb05d7072013-10-05 13:37:10 +01001687 int end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001688
Damiend99b0522013-12-21 18:17:45 +00001689 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001690 // this is a catch all exception handler
1691 if (i + 1 != n_except) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001692 compile_syntax_error(comp, "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001693 return;
1694 }
1695 } else {
1696 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001697 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1698 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1699 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1700 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001701 // handler binds the exception to a local
1702 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001703 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001704 }
1705 }
1706 EMIT(dup_top);
1707 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001708 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001709 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001710 }
1711
1712 EMIT(pop_top);
1713
1714 if (qstr_exception_local == 0) {
1715 EMIT(pop_top);
1716 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001717 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001718 }
1719
1720 EMIT(pop_top);
1721
Damiene2880aa2013-12-20 14:22:59 +00001722 int l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001723 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001724 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001725 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001726 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001727 }
1728 compile_node(comp, pns_except->nodes[1]);
1729 if (qstr_exception_local != 0) {
1730 EMIT(pop_block);
1731 }
1732 EMIT(pop_except);
1733 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001734 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1735 EMIT_ARG(label_assign, l3);
1736 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1737 EMIT_ARG(store_id, qstr_exception_local);
1738 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001739
Damien George8dcc0c72014-03-27 10:55:21 +00001740 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001741 EMIT(end_finally);
1742 }
Damien Georgeb9791222014-01-23 00:34:21 +00001743 EMIT_ARG(jump, l2);
1744 EMIT_ARG(label_assign, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001745 }
1746
Damien George8dcc0c72014-03-27 10:55:21 +00001747 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001748 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001749
Damien Georgeb9791222014-01-23 00:34:21 +00001750 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001751 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001752 EMIT_ARG(label_assign, l2);
1753 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001754}
1755
Damiend99b0522013-12-21 18:17:45 +00001756void 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 +01001757 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1758 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001759 int l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001760
Damien Georgeb9791222014-01-23 00:34:21 +00001761 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001762 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001763
Damien429d7192013-10-04 19:53:11 +01001764 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001765 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien429d7192013-10-04 19:53:11 +01001766 compile_node(comp, pn_body);
1767 } else {
1768 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1769 }
1770 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001771 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1772 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001773 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001774
Damien George8dcc0c72014-03-27 10:55:21 +00001775 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001776 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001777
Damien Georgeb9791222014-01-23 00:34:21 +00001778 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001779}
1780
Damiend99b0522013-12-21 18:17:45 +00001781void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1782 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1783 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1784 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001785 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001786 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1787 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001788 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001789 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001790 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001791 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001792 // no finally
1793 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1794 } else {
1795 // have finally
Damiend99b0522013-12-21 18:17:45 +00001796 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 +01001797 }
1798 } else {
1799 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001800 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001801 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001802 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001803 }
1804 } else {
1805 // shouldn't happen
1806 assert(0);
1807 }
1808}
1809
Damiend99b0522013-12-21 18:17:45 +00001810void 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 +01001811 if (n == 0) {
1812 // no more pre-bits, compile the body of the with
1813 compile_node(comp, body);
1814 } else {
Damienb05d7072013-10-05 13:37:10 +01001815 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001816 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001817 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001818 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001819 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001820 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001821 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1822 } else {
1823 // this pre-bit is just an expression
1824 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001825 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001826 EMIT(pop_top);
1827 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001828 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001829 // compile additional pre-bits and the body
1830 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1831 // finish this with block
1832 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001833 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1834 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001835 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001836 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001837 EMIT(end_finally);
1838 }
1839}
1840
Damiend99b0522013-12-21 18:17:45 +00001841void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001842 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001843 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001844 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1845 assert(n > 0);
1846
1847 // compile in a nested fashion
1848 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1849}
1850
Damiend99b0522013-12-21 18:17:45 +00001851void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1852 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001853 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1854 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001855 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001856 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001857 EMIT_ARG(call_function, 1, 0, false, false);
Damien5ac1b2e2013-10-18 19:58:12 +01001858 EMIT(pop_top);
1859
Damien429d7192013-10-04 19:53:11 +01001860 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001861 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001862 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001863 // do nothing with a lonely constant
1864 } else {
1865 compile_node(comp, pns->nodes[0]); // just an expression
1866 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1867 }
Damien429d7192013-10-04 19:53:11 +01001868 }
1869 } else {
Damiend99b0522013-12-21 18:17:45 +00001870 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1871 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001872 if (kind == PN_expr_stmt_augassign) {
1873 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1874 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001875 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001876 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001877 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001878 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1879 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1880 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1881 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1882 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1883 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1884 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1885 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1886 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1887 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1888 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
1889 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
1890 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01001891 }
Damien George7e5fb242014-02-01 22:18:47 +00001892 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001893 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1894 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001895 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1896 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001897 // following CPython, we store left-most first
1898 if (rhs > 0) {
1899 EMIT(dup_top);
1900 }
1901 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1902 for (int i = 0; i < rhs; i++) {
1903 if (i + 1 < rhs) {
1904 EMIT(dup_top);
1905 }
Damiend99b0522013-12-21 18:17:45 +00001906 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001907 }
1908 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001909 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1910 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1911 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1912 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01001913 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001914 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1915 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001916 compile_node(comp, pns10->nodes[0]); // rhs
1917 compile_node(comp, pns10->nodes[1]); // rhs
1918 EMIT(rot_two);
1919 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1920 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00001921 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1922 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1923 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1924 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01001925 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001926 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1927 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001928 compile_node(comp, pns10->nodes[0]); // rhs
1929 compile_node(comp, pns10->nodes[1]); // rhs
1930 compile_node(comp, pns10->nodes[2]); // rhs
1931 EMIT(rot_three);
1932 EMIT(rot_two);
1933 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1934 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1935 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1936 } else {
1937 compile_node(comp, pns1->nodes[0]); // rhs
1938 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1939 }
1940 } else {
1941 // shouldn't happen
1942 assert(0);
1943 }
1944 }
1945}
1946
Damien Georged17926d2014-03-30 13:35:08 +01001947void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00001948 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001949 compile_node(comp, pns->nodes[0]);
1950 for (int i = 1; i < num_nodes; i += 1) {
1951 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001952 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001953 }
1954}
1955
Damiend99b0522013-12-21 18:17:45 +00001956void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1957 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1958 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001959
1960 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001961 int l_fail = comp_next_label(comp);
1962 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001963 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1964 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001965 EMIT_ARG(jump, l_end);
1966 EMIT_ARG(label_assign, l_fail);
1967 EMIT_ARG(set_stack_size, stack_size); // force stack size reset
Damien429d7192013-10-04 19:53:11 +01001968 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001969 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001970}
1971
Damiend99b0522013-12-21 18:17:45 +00001972void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001973 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00001974 //mp_parse_node_t pn_params = pns->nodes[0];
1975 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001976
1977 if (comp->pass == PASS_1) {
1978 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00001979 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 +01001980 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001981 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001982 }
1983
1984 // get the scope for this lambda
1985 scope_t *this_scope = (scope_t*)pns->nodes[2];
1986
1987 // make the lambda
1988 close_over_variables_etc(comp, this_scope, 0, 0);
1989}
1990
Damiend99b0522013-12-21 18:17:45 +00001991void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001992 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001993 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001994 for (int i = 0; i < n; i += 1) {
1995 compile_node(comp, pns->nodes[i]);
1996 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00001997 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01001998 }
1999 }
Damien Georgeb9791222014-01-23 00:34:21 +00002000 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002001}
2002
Damiend99b0522013-12-21 18:17:45 +00002003void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01002004 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00002005 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002006 for (int i = 0; i < n; i += 1) {
2007 compile_node(comp, pns->nodes[i]);
2008 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00002009 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01002010 }
2011 }
Damien Georgeb9791222014-01-23 00:34:21 +00002012 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002013}
2014
Damiend99b0522013-12-21 18:17:45 +00002015void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002016 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002017 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002018}
2019
Damiend99b0522013-12-21 18:17:45 +00002020void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002021 int stack_size = EMIT(get_stack_size);
Damiend99b0522013-12-21 18:17:45 +00002022 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002023 compile_node(comp, pns->nodes[0]);
2024 bool multi = (num_nodes > 3);
2025 int l_fail = 0;
2026 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002027 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002028 }
2029 for (int i = 1; i + 1 < num_nodes; i += 2) {
2030 compile_node(comp, pns->nodes[i + 1]);
2031 if (i + 2 < num_nodes) {
2032 EMIT(dup_top);
2033 EMIT(rot_three);
2034 }
Damien George7e5fb242014-02-01 22:18:47 +00002035 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002036 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002037 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002038 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2039 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2040 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2041 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2042 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2043 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2044 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2045 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002046 }
2047 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002048 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2049 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2050 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002051 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002052 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002053 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002054 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002055 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002056 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002057 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002058 }
2059 } else {
2060 // shouldn't happen
2061 assert(0);
2062 }
2063 } else {
2064 // shouldn't happen
2065 assert(0);
2066 }
2067 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002068 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002069 }
2070 }
2071 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002072 int l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002073 EMIT_ARG(jump, l_end);
2074 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01002075 EMIT(rot_two);
2076 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002077 EMIT_ARG(label_assign, l_end);
2078 EMIT_ARG(set_stack_size, stack_size + 1); // force stack size
Damien429d7192013-10-04 19:53:11 +01002079 }
2080}
2081
Damiend99b0522013-12-21 18:17:45 +00002082void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002083 // TODO
2084 assert(0);
2085 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002086 //EMIT_ARG(unary_op, "UNARY_STAR");
Damien429d7192013-10-04 19:53:11 +01002087}
2088
Damiend99b0522013-12-21 18:17:45 +00002089void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002090 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002091}
2092
Damiend99b0522013-12-21 18:17:45 +00002093void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002094 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002095}
2096
Damiend99b0522013-12-21 18:17:45 +00002097void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002098 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002099}
2100
Damiend99b0522013-12-21 18:17:45 +00002101void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2102 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002103 compile_node(comp, pns->nodes[0]);
2104 for (int i = 1; i + 1 < num_nodes; i += 2) {
2105 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002106 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002107 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002108 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002109 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002110 } else {
2111 // shouldn't happen
2112 assert(0);
2113 }
2114 }
2115}
2116
Damiend99b0522013-12-21 18:17:45 +00002117void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2118 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002119 compile_node(comp, pns->nodes[0]);
2120 for (int i = 1; i + 1 < num_nodes; i += 2) {
2121 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002122 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002123 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002124 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002125 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002126 } else {
2127 // shouldn't happen
2128 assert(0);
2129 }
2130 }
2131}
2132
Damiend99b0522013-12-21 18:17:45 +00002133void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2134 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002135 compile_node(comp, pns->nodes[0]);
2136 for (int i = 1; i + 1 < num_nodes; i += 2) {
2137 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002138 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002139 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002140 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002141 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002142 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002143 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002144 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002145 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002146 } else {
2147 // shouldn't happen
2148 assert(0);
2149 }
2150 }
2151}
2152
Damiend99b0522013-12-21 18:17:45 +00002153void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002154 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002155 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002156 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002157 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002158 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002159 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002160 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002161 } else {
2162 // shouldn't happen
2163 assert(0);
2164 }
2165}
2166
Damien George35e2a4e2014-02-05 00:51:47 +00002167void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2168 // this is to handle special super() call
2169 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2170
2171 compile_generic_all_nodes(comp, pns);
2172}
2173
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002174STATIC 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 +01002175 // function to call is on top of stack
2176
Damien George35e2a4e2014-02-05 00:51:47 +00002177#if !MICROPY_EMIT_CPYTHON
2178 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002179 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 +00002180 EMIT_ARG(load_id, MP_QSTR___class__);
2181 // get first argument to function
2182 bool found = false;
2183 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
2184 if (comp->scope_cur->id_info[i].param) {
2185 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
2186 found = true;
2187 break;
2188 }
2189 }
2190 if (!found) {
2191 printf("TypeError: super() call cannot find self\n");
2192 return;
2193 }
2194 EMIT_ARG(call_function, 2, 0, false, false);
2195 return;
2196 }
2197#endif
2198
Damien429d7192013-10-04 19:53:11 +01002199 int old_n_arg_keyword = comp->n_arg_keyword;
2200 bool old_have_star_arg = comp->have_star_arg;
2201 bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
2202 comp->n_arg_keyword = 0;
2203 comp->have_star_arg = false;
2204 comp->have_dbl_star_arg = false;
2205
Damien Georgebbcd49a2014-02-06 20:30:16 +00002206 compile_node(comp, pn_arglist); // arguments to function call; can be null
Damien429d7192013-10-04 19:53:11 +01002207
2208 // compute number of positional arguments
Damien Georgebbcd49a2014-02-06 20:30:16 +00002209 int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
Damien429d7192013-10-04 19:53:11 +01002210 if (comp->have_star_arg) {
2211 n_positional -= 1;
2212 }
2213 if (comp->have_dbl_star_arg) {
2214 n_positional -= 1;
2215 }
2216
2217 if (is_method_call) {
Damien Georgeb9791222014-01-23 00:34:21 +00002218 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 +01002219 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002220 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 +01002221 }
2222
2223 comp->n_arg_keyword = old_n_arg_keyword;
2224 comp->have_star_arg = old_have_star_arg;
2225 comp->have_dbl_star_arg = old_have_dbl_star_arg;
2226}
2227
Damiend99b0522013-12-21 18:17:45 +00002228void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2229 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002230 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002231 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 +01002232 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002233 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2234 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002235 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002236 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002237 i += 1;
2238 } else {
2239 compile_node(comp, pns->nodes[i]);
2240 }
Damien George35e2a4e2014-02-05 00:51:47 +00002241 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002242 }
2243}
2244
Damiend99b0522013-12-21 18:17:45 +00002245void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002246 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002247 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002248}
2249
Damiend99b0522013-12-21 18:17:45 +00002250void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002251 // a list of strings
Damien63321742013-12-10 17:41:49 +00002252
2253 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002254 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002255 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002256 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002257 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002258 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2259 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2260 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002261 if (i == 0) {
2262 string_kind = pn_kind;
2263 } else if (pn_kind != string_kind) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002264 compile_syntax_error(comp, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002265 return;
2266 }
Damien George55baff42014-01-21 21:40:13 +00002267 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002268 }
Damien63321742013-12-10 17:41:49 +00002269
Damien63321742013-12-10 17:41:49 +00002270 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002271 byte *q_ptr;
2272 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002273 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002274 uint s_len;
2275 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002276 memcpy(s_dest, s, s_len);
2277 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002278 }
Damien George55baff42014-01-21 21:40:13 +00002279 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002280
Damien Georgeb9791222014-01-23 00:34:21 +00002281 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002282}
2283
2284// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002285void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2286 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2287 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2288 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002289
2290 if (comp->pass == PASS_1) {
2291 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002292 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 +01002293 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002294 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002295 }
2296
2297 // get the scope for this comprehension
2298 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2299
2300 // compile the comprehension
2301 close_over_variables_etc(comp, this_scope, 0, 0);
2302
2303 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2304 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002305 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01002306}
2307
Damiend99b0522013-12-21 18:17:45 +00002308void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2309 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002310 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002311 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2312 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2313 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2314 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2315 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2316 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2317 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002318 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002319 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002320 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002321 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002322 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002323 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002324 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002325 // generator expression
2326 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2327 } else {
2328 // tuple with 2 items
2329 goto tuple_with_2_items;
2330 }
2331 } else {
2332 // tuple with 2 items
2333 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002334 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002335 }
2336 } else {
2337 // parenthesis around a single item, is just that item
2338 compile_node(comp, pns->nodes[0]);
2339 }
2340}
2341
Damiend99b0522013-12-21 18:17:45 +00002342void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2343 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002344 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002345 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002346 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2347 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2348 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2349 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2350 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002351 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002352 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002353 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002354 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002355 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002356 // list of many items
2357 compile_node(comp, pns2->nodes[0]);
2358 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002359 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002360 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002361 // list comprehension
2362 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2363 } else {
2364 // list with 2 items
2365 goto list_with_2_items;
2366 }
2367 } else {
2368 // list with 2 items
2369 list_with_2_items:
2370 compile_node(comp, pns2->nodes[0]);
2371 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002372 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002373 }
2374 } else {
2375 // list with 1 item
2376 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002377 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002378 }
2379}
2380
Damiend99b0522013-12-21 18:17:45 +00002381void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2382 mp_parse_node_t pn = pns->nodes[0];
2383 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002384 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002385 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002386 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2387 pns = (mp_parse_node_struct_t*)pn;
2388 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002389 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002390 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002391 compile_node(comp, pn);
2392 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002393 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2394 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2395 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2396 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002397 // dict/set with multiple elements
2398
2399 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002400 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002401 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2402
2403 // first element sets whether it's a dict or set
2404 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002405 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002406 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002407 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002408 compile_node(comp, pns->nodes[0]);
2409 EMIT(store_map);
2410 is_dict = true;
2411 } else {
2412 // a set
2413 compile_node(comp, pns->nodes[0]); // 1st value of set
2414 is_dict = false;
2415 }
2416
2417 // process rest of elements
2418 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002419 mp_parse_node_t pn = nodes[i];
2420 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002421 compile_node(comp, pn);
2422 if (is_dict) {
2423 if (!is_key_value) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002424 compile_syntax_error(comp, "?expecting key:value for dictiona");
Damien429d7192013-10-04 19:53:11 +01002425 return;
2426 }
2427 EMIT(store_map);
2428 } else {
2429 if (is_key_value) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002430 compile_syntax_error(comp, "?expecting just a value for s");
Damien429d7192013-10-04 19:53:11 +01002431 return;
2432 }
2433 }
2434 }
2435
2436 // if it's a set, build it
2437 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002438 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002439 }
Damiend99b0522013-12-21 18:17:45 +00002440 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002441 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002442 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002443 // a dictionary comprehension
2444 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2445 } else {
2446 // a set comprehension
2447 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2448 }
2449 } else {
2450 // shouldn't happen
2451 assert(0);
2452 }
2453 } else {
2454 // set with one element
2455 goto set_with_one_element;
2456 }
2457 } else {
2458 // set with one element
2459 set_with_one_element:
2460 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002461 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002462 }
2463}
2464
Damiend99b0522013-12-21 18:17:45 +00002465void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002466 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002467}
2468
Damiend99b0522013-12-21 18:17:45 +00002469void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002470 // object who's index we want is on top of stack
2471 compile_node(comp, pns->nodes[0]); // the index
Damien Georged17926d2014-03-30 13:35:08 +01002472 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002473}
2474
Damiend99b0522013-12-21 18:17:45 +00002475void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002476 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002477 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002478}
2479
Damiend99b0522013-12-21 18:17:45 +00002480void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2481 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2482 mp_parse_node_t pn = pns->nodes[0];
2483 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002484 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002485 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2486 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002487 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2488 pns = (mp_parse_node_struct_t*)pn;
2489 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002490 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002491 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002492 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002493 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002494 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002495 } else {
2496 // [?::x]
2497 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002498 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002499 }
Damiend99b0522013-12-21 18:17:45 +00002500 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002501 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002502 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2503 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2504 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2505 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002506 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002507 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002508 } else {
2509 // [?:x:x]
2510 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002511 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002512 }
2513 } else {
2514 // [?:x]
2515 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002516 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002517 }
2518 } else {
2519 // [?:x]
2520 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002521 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002522 }
2523}
2524
Damiend99b0522013-12-21 18:17:45 +00002525void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002526 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002527 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2528 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002529}
2530
Damiend99b0522013-12-21 18:17:45 +00002531void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002532 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002533 compile_subscript_3_helper(comp, pns);
2534}
2535
Damiend99b0522013-12-21 18:17:45 +00002536void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002537 // if this is called then we are compiling a dict key:value pair
2538 compile_node(comp, pns->nodes[1]); // value
2539 compile_node(comp, pns->nodes[0]); // key
2540}
2541
Damiend99b0522013-12-21 18:17:45 +00002542void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002543 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002544 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002545 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002546}
2547
Damiend99b0522013-12-21 18:17:45 +00002548void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002549 if (comp->have_star_arg) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002550 compile_syntax_error(comp, "?can't have multiple *x");
Damien429d7192013-10-04 19:53:11 +01002551 return;
2552 }
2553 comp->have_star_arg = true;
2554 compile_node(comp, pns->nodes[0]);
2555}
2556
Damiend99b0522013-12-21 18:17:45 +00002557void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002558 if (comp->have_dbl_star_arg) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002559 compile_syntax_error(comp, "?can't have multiple **x");
Damien429d7192013-10-04 19:53:11 +01002560 return;
2561 }
2562 comp->have_dbl_star_arg = true;
2563 compile_node(comp, pns->nodes[0]);
2564}
2565
Damiend99b0522013-12-21 18:17:45 +00002566void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2567 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2568 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2569 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2570 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002571 compile_syntax_error(comp, "?lhs of keyword argument must be an id");
Damien429d7192013-10-04 19:53:11 +01002572 return;
2573 }
Damien Georgeb9791222014-01-23 00:34:21 +00002574 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002575 compile_node(comp, pns2->nodes[0]);
2576 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002577 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002578 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2579 } else {
2580 // shouldn't happen
2581 assert(0);
2582 }
2583}
2584
Damiend99b0522013-12-21 18:17:45 +00002585void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002586 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002587 compile_syntax_error(comp, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002588 return;
2589 }
Damiend99b0522013-12-21 18:17:45 +00002590 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002591 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002592 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002593 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2594 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002595 compile_node(comp, pns->nodes[0]);
2596 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002597 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002598 EMIT(yield_from);
2599 } else {
2600 compile_node(comp, pns->nodes[0]);
2601 EMIT(yield_value);
2602 }
2603}
2604
Damiend99b0522013-12-21 18:17:45 +00002605typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002606STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002607 NULL,
2608#define nc NULL
2609#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002610#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002611#include "grammar.h"
2612#undef nc
2613#undef c
2614#undef DEF_RULE
2615};
2616
Damiend99b0522013-12-21 18:17:45 +00002617void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2618 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002619 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002620 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2621 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2622 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002623 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002624 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002625 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002626 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002627 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2628 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2629 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2630 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002631 case MP_PARSE_NODE_TOKEN:
2632 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002633 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002634 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002635 // do nothing
2636 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002637 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002638 }
2639 break;
Damien429d7192013-10-04 19:53:11 +01002640 default: assert(0);
2641 }
2642 } else {
Damiend99b0522013-12-21 18:17:45 +00002643 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002644 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002645 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002646 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002647 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002648#if MICROPY_DEBUG_PRINTERS
2649 mp_parse_node_print(pn, 0);
2650#endif
Damien429d7192013-10-04 19:53:11 +01002651 assert(0);
2652 } else {
2653 f(comp, pns);
2654 }
2655 }
2656}
2657
Damiend99b0522013-12-21 18:17:45 +00002658void 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 +01002659 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002660 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002661 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2662 if (MP_PARSE_NODE_IS_ID(pn)) {
2663 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002664 if (comp->have_bare_star) {
2665 // comes after a bare star, so doesn't count as a parameter
2666 } else {
2667 comp->scope_cur->num_params += 1;
2668 }
Damienb14de212013-10-06 00:28:28 +01002669 } else {
Damiend99b0522013-12-21 18:17:45 +00002670 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2671 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2672 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2673 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002674 //int node_index = 1; unused
2675 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002676 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002677 // this parameter has an annotation
2678 pn_annotation = pns->nodes[1];
2679 }
2680 //node_index = 2; unused
2681 }
2682 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002683 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002684 // this parameter has a default value
2685 if (comp->have_bare_star) {
2686 comp->scope_cur->num_dict_params += 1;
2687 } else {
2688 comp->scope_cur->num_default_params += 1;
2689 }
2690 }
2691 */
2692 if (comp->have_bare_star) {
2693 // comes after a bare star, so doesn't count as a parameter
2694 } else {
2695 comp->scope_cur->num_params += 1;
2696 }
Damiend99b0522013-12-21 18:17:45 +00002697 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2698 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002699 // bare star
2700 // TODO see http://www.python.org/dev/peps/pep-3102/
2701 comp->have_bare_star = true;
2702 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002703 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002704 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002705 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002706 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2707 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002708 // named star with annotation
Damien George8725f8f2014-02-15 19:33:11 +00002709 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002710 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2711 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002712 pn_annotation = pns->nodes[1];
2713 } else {
2714 // shouldn't happen
2715 assert(0);
2716 }
Damiend99b0522013-12-21 18:17:45 +00002717 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2718 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2719 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002720 // this parameter has an annotation
2721 pn_annotation = pns->nodes[1];
2722 }
Damien George8725f8f2014-02-15 19:33:11 +00002723 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002724 } else {
Damienb14de212013-10-06 00:28:28 +01002725 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002726 assert(0);
2727 }
Damien429d7192013-10-04 19:53:11 +01002728 }
2729
2730 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002731 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002732 // TODO this parameter has an annotation
2733 }
2734 bool added;
2735 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2736 if (!added) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002737 compile_syntax_error(comp, "?same name used for parameter");
Damien429d7192013-10-04 19:53:11 +01002738 return;
2739 }
2740 id_info->param = true;
2741 id_info->kind = ID_INFO_KIND_LOCAL;
2742 }
2743}
2744
Damiend99b0522013-12-21 18:17:45 +00002745void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002746 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2747}
2748
Damiend99b0522013-12-21 18:17:45 +00002749void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002750 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2751}
2752
Damiend99b0522013-12-21 18:17:45 +00002753void 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 +01002754 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002755 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002756 // no more nested if/for; compile inner expression
2757 compile_node(comp, pn_inner_expr);
2758 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002759 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002760 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002761 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002762 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002763 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002764 } else {
2765 EMIT(yield_value);
2766 EMIT(pop_top);
2767 }
Damiend99b0522013-12-21 18:17:45 +00002768 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002769 // if condition
Damiend99b0522013-12-21 18:17:45 +00002770 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002771 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2772 pn_iter = pns_comp_if->nodes[1];
2773 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002774 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002775 // for loop
Damiend99b0522013-12-21 18:17:45 +00002776 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002777 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002778 int l_end2 = comp_next_label(comp);
2779 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002780 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002781 EMIT_ARG(label_assign, l_top2);
2782 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002783 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2784 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 +00002785 EMIT_ARG(jump, l_top2);
2786 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002787 EMIT(for_iter_end);
2788 } else {
2789 // shouldn't happen
2790 assert(0);
2791 }
2792}
2793
Damiend99b0522013-12-21 18:17:45 +00002794void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002795 // see http://www.python.org/dev/peps/pep-0257/
2796
2797 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002798 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002799 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002800 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002801 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002802 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2803 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002804 for (int i = 0; i < num_nodes; i++) {
2805 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002806 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 +00002807 // not a newline, so this is the first statement; finish search
2808 break;
2809 }
2810 }
2811 // 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 +00002812 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002813 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002814 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002815 } else {
2816 return;
2817 }
2818
2819 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002820 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2821 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2822 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2823 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2824 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002825 compile_node(comp, pns->nodes[0]); // a doc string
2826 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002827 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002828 }
2829 }
2830 }
2831}
2832
2833void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2834 comp->pass = pass;
2835 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002836 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002837 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002838
2839 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002840 // reset maximum stack sizes in scope
2841 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002842 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002843 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002844 }
2845
Damien5ac1b2e2013-10-18 19:58:12 +01002846#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002847 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002848 scope_print_info(scope);
2849 }
Damien5ac1b2e2013-10-18 19:58:12 +01002850#endif
Damien429d7192013-10-04 19:53:11 +01002851
2852 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002853 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2854 assert(scope->kind == SCOPE_MODULE);
2855 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2856 compile_node(comp, pns->nodes[0]); // compile the expression
2857 EMIT(return_value);
2858 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002859 if (!comp->is_repl) {
2860 check_for_doc_string(comp, scope->pn);
2861 }
Damien429d7192013-10-04 19:53:11 +01002862 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002863 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002864 EMIT(return_value);
2865 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002866 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2867 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2868 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002869
2870 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002871 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002872 if (comp->pass == PASS_1) {
2873 comp->have_bare_star = false;
2874 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2875 }
2876
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02002877 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01002878
2879 compile_node(comp, pns->nodes[3]); // 3 is function body
2880 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002881 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002882 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002883 EMIT(return_value);
2884 }
2885 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002886 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2887 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2888 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002889
2890 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002891 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002892 if (comp->pass == PASS_1) {
2893 comp->have_bare_star = false;
2894 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2895 }
2896
2897 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2898 EMIT(return_value);
2899 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2900 // a bit of a hack at the moment
2901
Damiend99b0522013-12-21 18:17:45 +00002902 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2903 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2904 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2905 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2906 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002907
Damien George55baff42014-01-21 21:40:13 +00002908 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01002909 if (comp->pass == PASS_1) {
2910 bool added;
2911 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2912 assert(added);
2913 id_info->kind = ID_INFO_KIND_LOCAL;
2914 scope->num_params = 1;
2915 }
2916
2917 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002918 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01002919 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002920 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01002921 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002922 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01002923 }
2924
Damienb05d7072013-10-05 13:37:10 +01002925 int l_end = comp_next_label(comp);
2926 int l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002927 EMIT_ARG(load_id, qstr_arg);
2928 EMIT_ARG(label_assign, l_top);
2929 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01002930 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2931 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00002932 EMIT_ARG(jump, l_top);
2933 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002934 EMIT(for_iter_end);
2935
2936 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00002937 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002938 }
2939 EMIT(return_value);
2940 } else {
2941 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002942 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2943 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2944 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01002945
2946 if (comp->pass == PASS_1) {
2947 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002948 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01002949 assert(added);
2950 id_info->kind = ID_INFO_KIND_LOCAL;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002951 id_info = scope_find_or_add_id(scope, MP_QSTR___locals__, &added);
Damien429d7192013-10-04 19:53:11 +01002952 assert(added);
2953 id_info->kind = ID_INFO_KIND_LOCAL;
2954 id_info->param = true;
2955 scope->num_params = 1; // __locals__ is the parameter
2956 }
2957
Damien Georgeb9791222014-01-23 00:34:21 +00002958 EMIT_ARG(load_id, MP_QSTR___locals__);
Damien429d7192013-10-04 19:53:11 +01002959 EMIT(store_locals);
Damien Georgeb9791222014-01-23 00:34:21 +00002960 EMIT_ARG(load_id, MP_QSTR___name__);
2961 EMIT_ARG(store_id, MP_QSTR___module__);
2962 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
2963 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01002964
2965 check_for_doc_string(comp, pns->nodes[2]);
2966 compile_node(comp, pns->nodes[2]); // 2 is class body
2967
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002968 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01002969 assert(id != NULL);
2970 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00002971 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002972 } else {
Damien George6baf76e2013-12-30 22:32:17 +00002973#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00002974 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002975#else
Damien George35e2a4e2014-02-05 00:51:47 +00002976 EMIT_ARG(load_fast, MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00002977#endif
Damien429d7192013-10-04 19:53:11 +01002978 }
2979 EMIT(return_value);
2980 }
2981
Damien415eb6f2013-10-05 12:19:06 +01002982 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00002983
2984 // make sure we match all the exception levels
2985 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01002986}
2987
2988void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2989 comp->pass = pass;
2990 comp->scope_cur = scope;
2991 comp->next_label = 1;
2992
2993 if (scope->kind != SCOPE_FUNCTION) {
2994 printf("Error: inline assembler must be a function\n");
2995 return;
2996 }
2997
Damiena2f2f7d2013-10-06 00:14:13 +01002998 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00002999 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01003000 }
3001
Damien826005c2013-10-05 23:17:28 +01003002 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00003003 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
3004 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
3005 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01003006
Damiend99b0522013-12-21 18:17:45 +00003007 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01003008
Damiena2f2f7d2013-10-06 00:14:13 +01003009 // parameters are in pns->nodes[1]
3010 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00003011 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01003012 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00003013 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01003014 }
3015
Damiend99b0522013-12-21 18:17:45 +00003016 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003017
Damiend99b0522013-12-21 18:17:45 +00003018 mp_parse_node_t pn_body = pns->nodes[3]; // body
3019 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003020 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3021
Damien Georgecbd2f742014-01-19 11:48:48 +00003022 /*
Damien826005c2013-10-05 23:17:28 +01003023 if (comp->pass == PASS_3) {
3024 //printf("----\n");
3025 scope_print_info(scope);
3026 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003027 */
Damien826005c2013-10-05 23:17:28 +01003028
3029 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003030 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3031 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
3032 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
3033 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3034 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3035 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3036 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3037 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3038 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3039 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3040 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3041 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3042 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003043 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3044
3045 // emit instructions
3046 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00003047 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgef41fdd02014-03-03 23:19:11 +00003048 compile_syntax_error(comp, "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003049 return;
3050 }
3051 int lab = comp_next_label(comp);
3052 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003053 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003054 }
3055 } else {
3056 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003057 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003058 }
3059 }
3060 }
3061
3062 if (comp->pass > PASS_1) {
3063 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01003064 }
Damien429d7192013-10-04 19:53:11 +01003065}
3066
3067void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3068 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003069 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003070 scope->num_locals = 0;
3071 for (int i = 0; i < scope->id_info_len; i++) {
3072 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003073 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003074 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3075 continue;
3076 }
3077 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3078 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3079 }
Damien9ecbcff2013-12-11 00:41:43 +00003080 // note: params always count for 1 local, even if they are a cell
Damien429d7192013-10-04 19:53:11 +01003081 if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
3082 id->local_num = scope->num_locals;
3083 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003084 }
3085 }
3086
3087 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003088#if MICROPY_EMIT_CPYTHON
3089 int num_cell = 0;
3090#endif
Damien9ecbcff2013-12-11 00:41:43 +00003091 for (int i = 0; i < scope->id_info_len; i++) {
3092 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003093#if MICROPY_EMIT_CPYTHON
3094 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003095 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003096 id->local_num = num_cell;
3097 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003098 }
Damien George6baf76e2013-12-30 22:32:17 +00003099#else
3100 // in Micro Python the cells come right after the fast locals
3101 // parameters are not counted here, since they remain at the start
3102 // of the locals, even if they are cell vars
3103 if (!id->param && id->kind == ID_INFO_KIND_CELL) {
3104 id->local_num = scope->num_locals;
3105 scope->num_locals += 1;
3106 }
3107#endif
Damien9ecbcff2013-12-11 00:41:43 +00003108 }
Damien9ecbcff2013-12-11 00:41:43 +00003109
3110 // compute the index of free vars (freevars[idx] in CPython)
3111 // make sure they are in the order of the parent scope
3112 if (scope->parent != NULL) {
3113 int num_free = 0;
3114 for (int i = 0; i < scope->parent->id_info_len; i++) {
3115 id_info_t *id = &scope->parent->id_info[i];
3116 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3117 for (int j = 0; j < scope->id_info_len; j++) {
3118 id_info_t *id2 = &scope->id_info[j];
3119 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +00003120 assert(!id2->param); // free vars should not be params
3121#if MICROPY_EMIT_CPYTHON
3122 // in CPython the frees are numbered after the cells
3123 id2->local_num = num_cell + num_free;
3124#else
3125 // in Micro Python the frees come first, before the params
3126 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003127#endif
3128 num_free += 1;
3129 }
3130 }
3131 }
Damien429d7192013-10-04 19:53:11 +01003132 }
Damien George6baf76e2013-12-30 22:32:17 +00003133#if !MICROPY_EMIT_CPYTHON
3134 // in Micro Python shift all other locals after the free locals
3135 if (num_free > 0) {
3136 for (int i = 0; i < scope->id_info_len; i++) {
3137 id_info_t *id = &scope->id_info[i];
3138 if (id->param || id->kind != ID_INFO_KIND_FREE) {
3139 id->local_num += num_free;
3140 }
3141 }
3142 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3143 scope->num_locals += num_free;
3144 }
3145#endif
Damien429d7192013-10-04 19:53:11 +01003146 }
3147
Damien George8725f8f2014-02-15 19:33:11 +00003148 // compute scope_flags
3149 //scope->scope_flags = 0; since we set some things in parameters
Damien429d7192013-10-04 19:53:11 +01003150 if (scope->kind != SCOPE_MODULE) {
Damien George8725f8f2014-02-15 19:33:11 +00003151 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien429d7192013-10-04 19:53:11 +01003152 }
3153 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) {
3154 assert(scope->parent != NULL);
Damien George8725f8f2014-02-15 19:33:11 +00003155 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien429d7192013-10-04 19:53:11 +01003156
3157 // TODO possibly other ways it can be nested
Damien George08d07552014-01-29 18:58:52 +00003158 // Note that we don't actually use this information at the moment (for CPython compat only)
3159 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 +00003160 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003161 }
3162 }
3163 int num_free = 0;
3164 for (int i = 0; i < scope->id_info_len; i++) {
3165 id_info_t *id = &scope->id_info[i];
3166 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3167 num_free += 1;
3168 }
3169 }
3170 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003171 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003172 }
3173}
3174
Damien George08335002014-01-18 23:24:36 +00003175mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
Damien429d7192013-10-04 19:53:11 +01003176 compiler_t *comp = m_new(compiler_t, 1);
3177
Damien Georgecbd2f742014-01-19 11:48:48 +00003178 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003179 comp->is_repl = is_repl;
3180 comp->had_error = false;
3181
Damien429d7192013-10-04 19:53:11 +01003182 comp->break_label = 0;
3183 comp->continue_label = 0;
Damien Georgecbddb272014-02-01 20:08:18 +00003184 comp->break_continue_except_level = 0;
3185 comp->cur_except_level = 0;
3186
Damien George35e2a4e2014-02-05 00:51:47 +00003187 comp->func_arg_is_super = false;
3188
Damien429d7192013-10-04 19:53:11 +01003189 comp->scope_head = NULL;
3190 comp->scope_cur = NULL;
3191
Damien826005c2013-10-05 23:17:28 +01003192 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003193 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003194
3195 // set the outer scope
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003196 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE);
Damien429d7192013-10-04 19:53:11 +01003197
Damien826005c2013-10-05 23:17:28 +01003198 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003199 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003200 comp->emit_method_table = &emit_pass1_method_table;
3201 comp->emit_inline_asm = NULL;
3202 comp->emit_inline_asm_method_table = NULL;
3203 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003204 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003205 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003206#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003207 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003208 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003209#endif
Damien826005c2013-10-05 23:17:28 +01003210 } else {
3211 compile_scope(comp, s, PASS_1);
3212 }
3213
3214 // update maximim number of labels needed
3215 if (comp->next_label > max_num_labels) {
3216 max_num_labels = comp->next_label;
3217 }
Damien429d7192013-10-04 19:53:11 +01003218 }
3219
Damien826005c2013-10-05 23:17:28 +01003220 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003221 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003222 compile_scope_compute_things(comp, s);
3223 }
3224
Damien826005c2013-10-05 23:17:28 +01003225 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003226 emit_pass1_free(comp->emit);
3227
Damien826005c2013-10-05 23:17:28 +01003228 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003229#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003230 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003231#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003232 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003233#endif
Damien3ef4abb2013-10-12 16:53:13 +01003234#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003235 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003236#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003237#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003238 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003239 if (false) {
3240 // dummy
3241
Damien3ef4abb2013-10-12 16:53:13 +01003242#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003243 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
3244 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003245 if (emit_inline_thumb == NULL) {
3246 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3247 }
3248 comp->emit = NULL;
3249 comp->emit_method_table = NULL;
3250 comp->emit_inline_asm = emit_inline_thumb;
3251 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3252 compile_scope_inline_asm(comp, s, PASS_2);
3253 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003254#endif
3255
Damien826005c2013-10-05 23:17:28 +01003256 } else {
Damienc025ebb2013-10-12 14:30:21 +01003257
3258 // choose the emit type
3259
Damien3ef4abb2013-10-12 16:53:13 +01003260#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003261 comp->emit = emit_cpython_new(max_num_labels);
3262 comp->emit_method_table = &emit_cpython_method_table;
3263#else
Damien826005c2013-10-05 23:17:28 +01003264 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003265
3266#if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003267 case EMIT_OPT_NATIVE_PYTHON:
Damien3410be82013-10-07 23:09:10 +01003268 case EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003269#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003270 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003271 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003272 }
Damien13ed3a62013-10-08 09:05:10 +01003273 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003274#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003275 if (emit_native == NULL) {
3276 emit_native = emit_native_thumb_new(max_num_labels);
3277 }
3278 comp->emit_method_table = &emit_native_thumb_method_table;
3279#endif
3280 comp->emit = emit_native;
Damien3410be82013-10-07 23:09:10 +01003281 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003282 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003283#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003284
Damien826005c2013-10-05 23:17:28 +01003285 default:
3286 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003287 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003288 }
3289 comp->emit = emit_bc;
3290 comp->emit_method_table = &emit_bc_method_table;
3291 break;
3292 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003293#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003294
3295 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003296 compile_scope(comp, s, PASS_2);
3297 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003298 }
Damien429d7192013-10-04 19:53:11 +01003299 }
3300
Damien George41d02b62014-01-24 22:42:28 +00003301 // free the emitters
3302#if !MICROPY_EMIT_CPYTHON
3303 if (emit_bc != NULL) {
3304 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003305 }
Damien George41d02b62014-01-24 22:42:28 +00003306#if MICROPY_EMIT_NATIVE
3307 if (emit_native != NULL) {
3308#if MICROPY_EMIT_X64
3309 emit_native_x64_free(emit_native);
3310#elif MICROPY_EMIT_THUMB
3311 emit_native_thumb_free(emit_native);
3312#endif
3313 }
3314#endif
3315#if MICROPY_EMIT_INLINE_THUMB
3316 if (emit_inline_thumb != NULL) {
3317 emit_inline_thumb_free(emit_inline_thumb);
3318 }
3319#endif
3320#endif // !MICROPY_EMIT_CPYTHON
3321
3322 // free the scopes
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003323 uint unique_code_id = module_scope->unique_code_id;
3324 for (scope_t *s = module_scope; s;) {
3325 scope_t *next = s->next;
3326 scope_free(s);
3327 s = next;
3328 }
Damien5ac1b2e2013-10-18 19:58:12 +01003329
Damien George41d02b62014-01-24 22:42:28 +00003330 // free the compiler
3331 bool had_error = comp->had_error;
3332 m_del_obj(compiler_t, comp);
3333
Damien George1fb03172014-01-03 14:22:03 +00003334 if (had_error) {
3335 // TODO return a proper error message
3336 return mp_const_none;
3337 } else {
3338#if MICROPY_EMIT_CPYTHON
3339 // can't create code, so just return true
Damien George41d02b62014-01-24 22:42:28 +00003340 (void)unique_code_id; // to suppress warning that unique_code_id is unused
Damien George1fb03172014-01-03 14:22:03 +00003341 return mp_const_true;
3342#else
3343 // return function that executes the outer module
Damien Georged1e443d2014-03-29 11:39:36 +00003344 // we can free the unique_code slot because no-one has reference to this unique_code_id anymore
Damien Georgee337f1e2014-03-31 15:18:37 +01003345 return mp_make_function_from_id(unique_code_id, true, MP_OBJ_NULL, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003346#endif
3347 }
Damien429d7192013-10-04 19:53:11 +01003348}