blob: b3a83715e059d0df1b350f9a0ecd72d125fb4b63 [file] [log] [blame]
xbeefe34222014-03-16 00:14:26 -07001#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +01002#include <stdint.h>
3#include <stdio.h>
4#include <string.h>
5#include <assert.h>
Rachel Dowdall56402792014-03-22 20:19:24 +00006#include <math.h>
Damien429d7192013-10-04 19:53:11 +01007
8#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00009#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000010#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010011#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010012#include "parse.h"
13#include "scope.h"
Damiend99b0522013-12-21 18:17:45 +000014#include "runtime0.h"
Damien429d7192013-10-04 19:53:11 +010015#include "emit.h"
Damien George2326d522014-03-27 23:26:35 +000016#include "emitglue.h"
Damien George1fb03172014-01-03 14:22:03 +000017#include "obj.h"
18#include "compile.h"
19#include "runtime.h"
Rachel Dowdallcde86312014-03-22 17:29:27 +000020#include "intdivmod.h"
Damien429d7192013-10-04 19:53:11 +010021
22// TODO need to mangle __attr names
23
Damience89a212013-10-15 22:25:17 +010024#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_THUMB)
25
Damien429d7192013-10-04 19:53:11 +010026typedef enum {
27 PN_none = 0,
Damien George00208ce2014-01-23 00:00:53 +000028#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien429d7192013-10-04 19:53:11 +010029#include "grammar.h"
30#undef DEF_RULE
31 PN_maximum_number_of,
32} pn_kind_t;
33
Damien Georgeb9791222014-01-23 00:34:21 +000034#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
35#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
36#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
37#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010038
Damien6cdd3af2013-10-05 18:08:26 +010039#define EMIT_OPT_NONE (0)
40#define EMIT_OPT_BYTE_CODE (1)
41#define EMIT_OPT_NATIVE_PYTHON (2)
Damien7af3d192013-10-07 00:02:49 +010042#define EMIT_OPT_VIPER (3)
43#define EMIT_OPT_ASM_THUMB (4)
Damien6cdd3af2013-10-05 18:08:26 +010044
Damien429d7192013-10-04 19:53:11 +010045typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000046 qstr source_file;
Damien5ac1b2e2013-10-18 19:58:12 +010047 bool is_repl;
Damien429d7192013-10-04 19:53:11 +010048 pass_kind_t pass;
Damien5ac1b2e2013-10-18 19:58:12 +010049 bool had_error; // try to keep compiler clean from nlr
Damien429d7192013-10-04 19:53:11 +010050
Damienb05d7072013-10-05 13:37:10 +010051 int next_label;
Damienb05d7072013-10-05 13:37:10 +010052
Damien429d7192013-10-04 19:53:11 +010053 int break_label;
54 int continue_label;
Damien Georgecbddb272014-02-01 20:08:18 +000055 int break_continue_except_level;
56 int cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien429d7192013-10-04 19:53:11 +010057
58 int n_arg_keyword;
59 bool have_star_arg;
60 bool have_dbl_star_arg;
61 bool have_bare_star;
62 int param_pass;
63 int param_pass_num_dict_params;
64 int param_pass_num_default_params;
65
Damien George35e2a4e2014-02-05 00:51:47 +000066 bool func_arg_is_super; // used to compile special case of super() function call
67
Damien429d7192013-10-04 19:53:11 +010068 scope_t *scope_head;
69 scope_t *scope_cur;
70
Damien6cdd3af2013-10-05 18:08:26 +010071 emit_t *emit; // current emitter
72 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010073
74 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
75 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien429d7192013-10-04 19:53:11 +010076} compiler_t;
77
Damien Georgef41fdd02014-03-03 23:19:11 +000078STATIC void compile_syntax_error(compiler_t *comp, const char *msg) {
79 // TODO store the error message to a variable in compiler_t instead of printing it
80 printf("SyntaxError: %s\n", msg);
81 comp->had_error = true;
82}
83
Damiend99b0522013-12-21 18:17:45 +000084mp_parse_node_t fold_constants(mp_parse_node_t pn) {
85 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
86 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
87 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +010088
89 // fold arguments first
90 for (int i = 0; i < n; i++) {
91 pns->nodes[i] = fold_constants(pns->nodes[i]);
92 }
93
Damiend99b0522013-12-21 18:17:45 +000094 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +010095 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +000096 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +020097 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
98 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +000099 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien3ef4abb2013-10-12 16:53:13 +0100100#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100101 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000102 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
Damien0efb3a12013-10-12 16:16:56 +0100103#endif
Damiend99b0522013-12-21 18:17:45 +0000104 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
105 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100106 } else {
107 // shouldn't happen
108 assert(0);
109 }
110 }
111 break;
112
113 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100114 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000115 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200116 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
117 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damien0efb3a12013-10-12 16:16:56 +0100118 machine_int_t res;
Damiend99b0522013-12-21 18:17:45 +0000119 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100120 res = arg0 + arg1;
Damiend99b0522013-12-21 18:17:45 +0000121 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100122 res = arg0 - arg1;
Damien429d7192013-10-04 19:53:11 +0100123 } else {
124 // shouldn't happen
125 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100126 res = 0;
127 }
Paul Sokolovskybbf0e2f2014-02-21 02:04:32 +0200128 if (MP_PARSE_FITS_SMALL_INT(res)) {
Damiend99b0522013-12-21 18:17:45 +0000129 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, res);
Damien429d7192013-10-04 19:53:11 +0100130 }
131 }
132 break;
133
134 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000135 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200136 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
137 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000138 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien3ef4abb2013-10-12 16:53:13 +0100139#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100140 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000141 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 * arg1);
Damien0efb3a12013-10-12 16:16:56 +0100142#endif
Damiend99b0522013-12-21 18:17:45 +0000143 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien429d7192013-10-04 19:53:11 +0100144 ; // pass
Damiend99b0522013-12-21 18:17:45 +0000145 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Rachel Dowdallcde86312014-03-22 17:29:27 +0000146 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, python_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000147 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Damien George8dcc0c72014-03-27 10:55:21 +0000148 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, python_floor_divide(arg0, arg1));
Damien429d7192013-10-04 19:53:11 +0100149 } else {
150 // shouldn't happen
151 assert(0);
152 }
153 }
154 break;
155
156 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000157 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200158 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000159 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
160 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
161 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
162 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
163 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
164 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100165 } else {
166 // shouldn't happen
167 assert(0);
168 }
169 }
170 break;
171
Damien3ef4abb2013-10-12 16:53:13 +0100172#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +0100173 case PN_power:
Damien0efb3a12013-10-12 16:16:56 +0100174 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000175 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_NULL(pns->nodes[1]) && !MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
176 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
177 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200178 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100179 if (power >= 0) {
180 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200181 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100182 for (; power > 0; power--) {
183 ans *= base;
184 }
Damiend99b0522013-12-21 18:17:45 +0000185 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100186 }
187 }
188 }
189 break;
Damien0efb3a12013-10-12 16:16:56 +0100190#endif
Damien429d7192013-10-04 19:53:11 +0100191 }
192 }
193
194 return pn;
195}
196
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200197STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra);
Damien George8dcc0c72014-03-27 10:55:21 +0000198STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100199
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200200STATIC int comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100201 return comp->next_label++;
202}
203
Damien George8dcc0c72014-03-27 10:55:21 +0000204STATIC void compile_increase_except_level(compiler_t *comp) {
205 comp->cur_except_level += 1;
206 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
207 comp->scope_cur->exc_stack_size = comp->cur_except_level;
208 }
209}
210
211STATIC void compile_decrease_except_level(compiler_t *comp) {
212 assert(comp->cur_except_level > 0);
213 comp->cur_except_level -= 1;
214}
215
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200216STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) {
Damien George2326d522014-03-27 23:26:35 +0000217 scope_t *scope = scope_new(kind, pn, comp->source_file, mp_emit_glue_get_unique_code_id(), emit_options);
Damien429d7192013-10-04 19:53:11 +0100218 scope->parent = comp->scope_cur;
219 scope->next = NULL;
220 if (comp->scope_head == NULL) {
221 comp->scope_head = scope;
222 } else {
223 scope_t *s = comp->scope_head;
224 while (s->next != NULL) {
225 s = s->next;
226 }
227 s->next = scope;
228 }
229 return scope;
230}
231
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200232STATIC int list_len(mp_parse_node_t pn, int pn_kind) {
Damiend99b0522013-12-21 18:17:45 +0000233 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100234 return 0;
Damiend99b0522013-12-21 18:17:45 +0000235 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100236 return 1;
237 } else {
Damiend99b0522013-12-21 18:17:45 +0000238 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
239 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100240 return 1;
241 } else {
Damiend99b0522013-12-21 18:17:45 +0000242 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100243 }
244 }
245}
246
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200247STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, int pn_list_kind, void (*f)(compiler_t*, mp_parse_node_t)) {
Damiend99b0522013-12-21 18:17:45 +0000248 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
249 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
250 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100251 for (int i = 0; i < num_nodes; i++) {
252 f(comp, pns->nodes[i]);
253 }
Damiend99b0522013-12-21 18:17:45 +0000254 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100255 f(comp, pn);
256 }
257}
258
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200259STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000260 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100261 *nodes = NULL;
262 return 0;
Damiend99b0522013-12-21 18:17:45 +0000263 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100264 *nodes = pn;
265 return 1;
266 } else {
Damiend99b0522013-12-21 18:17:45 +0000267 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
268 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100269 *nodes = pn;
270 return 1;
271 } else {
272 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000273 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100274 }
275 }
276}
277
Damiend99b0522013-12-21 18:17:45 +0000278void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100279}
280
Damiend99b0522013-12-21 18:17:45 +0000281void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
282 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100283 for (int i = 0; i < num_nodes; i++) {
284 compile_node(comp, pns->nodes[i]);
285 }
286}
287
Damien3ef4abb2013-10-12 16:53:13 +0100288#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200289STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000290 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100291 return false;
292 }
Damiend99b0522013-12-21 18:17:45 +0000293 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100294 return false;
295 }
296 return true;
297}
298
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200299STATIC void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
Damien George55baff42014-01-21 21:40:13 +0000300 uint len;
301 const byte *str = qstr_data(qstr, &len);
Damien02f89412013-12-12 15:13:36 +0000302 bool has_single_quote = false;
303 bool has_double_quote = false;
304 for (int i = 0; i < len; i++) {
305 if (str[i] == '\'') {
306 has_single_quote = true;
307 } else if (str[i] == '"') {
308 has_double_quote = true;
309 }
310 }
311 if (bytes) {
312 vstr_printf(vstr, "b");
313 }
314 bool quote_single = false;
315 if (has_single_quote && !has_double_quote) {
316 vstr_printf(vstr, "\"");
317 } else {
318 quote_single = true;
319 vstr_printf(vstr, "'");
320 }
321 for (int i = 0; i < len; i++) {
322 if (str[i] == '\n') {
323 vstr_printf(vstr, "\\n");
324 } else if (str[i] == '\\') {
325 vstr_printf(vstr, "\\\\");
326 } else if (str[i] == '\'' && quote_single) {
327 vstr_printf(vstr, "\\'");
328 } else {
329 vstr_printf(vstr, "%c", str[i]);
330 }
331 }
332 if (has_single_quote && !has_double_quote) {
333 vstr_printf(vstr, "\"");
334 } else {
335 vstr_printf(vstr, "'");
336 }
337}
338
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200339STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damiend99b0522013-12-21 18:17:45 +0000340 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200341 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
342 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
343 return;
344 }
345
Damiend99b0522013-12-21 18:17:45 +0000346 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
347 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
348 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000349 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
350 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
351 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
352 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
353 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100354 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000355 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
356 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
357 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien429d7192013-10-04 19:53:11 +0100358 default: assert(0);
359 }
360 break;
361 default: assert(0);
362 }
363}
364
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200365STATIC void cpython_c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien429d7192013-10-04 19:53:11 +0100366 int n = 0;
367 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000368 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100369 }
370 int total = n;
371 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000372 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100373 total += 1;
Damien3a205172013-10-12 15:01:56 +0100374 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100375 is_const = false;
376 }
377 }
378 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100379 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100380 is_const = false;
381 break;
382 }
383 }
384 if (total > 0 && is_const) {
385 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000386 vstr_t *vstr = vstr_new();
387 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000388 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000389 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100390 need_comma = true;
391 }
392 for (int i = 0; i < n; i++) {
393 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000394 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100395 }
Damien02f89412013-12-12 15:13:36 +0000396 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100397 need_comma = true;
398 }
399 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000400 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100401 } else {
Damien02f89412013-12-12 15:13:36 +0000402 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100403 }
Damien Georgeb9791222014-01-23 00:34:21 +0000404 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000405 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100406 } else {
Damiend99b0522013-12-21 18:17:45 +0000407 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100408 compile_node(comp, pn);
409 }
410 for (int i = 0; i < n; i++) {
411 compile_node(comp, pns_list->nodes[i]);
412 }
Damien Georgeb9791222014-01-23 00:34:21 +0000413 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100414 }
415}
Damien3a205172013-10-12 15:01:56 +0100416#endif
417
418// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damiend99b0522013-12-21 18:17:45 +0000419void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100420#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100421 cpython_c_tuple(comp, pn, pns_list);
422#else
423 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000424 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100425 compile_node(comp, pn);
426 total += 1;
427 }
428 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000429 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100430 for (int i = 0; i < n; i++) {
431 compile_node(comp, pns_list->nodes[i]);
432 }
433 total += n;
434 }
Damien Georgeb9791222014-01-23 00:34:21 +0000435 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100436#endif
437}
Damien429d7192013-10-04 19:53:11 +0100438
Damiend99b0522013-12-21 18:17:45 +0000439void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100440 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000441 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100442}
443
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200444STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000445 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200446 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100447}
448
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200449STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200450 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE) || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 1);
Damien429d7192013-10-04 19:53:11 +0100451}
452
Damien3ef4abb2013-10-12 16:53:13 +0100453#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100454// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200455STATIC void cpython_c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label, bool is_nested) {
Damien429d7192013-10-04 19:53:11 +0100456 if (node_is_const_false(pn)) {
457 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000458 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100459 }
460 return;
461 } else if (node_is_const_true(pn)) {
462 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000463 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100464 }
465 return;
Damiend99b0522013-12-21 18:17:45 +0000466 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
467 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
468 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
469 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100470 if (jump_if == false) {
Damienb05d7072013-10-05 13:37:10 +0100471 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100472 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100473 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100474 }
Damien3a205172013-10-12 15:01:56 +0100475 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000476 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100477 } else {
478 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100479 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100480 }
481 }
482 return;
Damiend99b0522013-12-21 18:17:45 +0000483 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100484 if (jump_if == false) {
485 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100486 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100487 }
488 } else {
Damienb05d7072013-10-05 13:37:10 +0100489 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100490 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100491 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100492 }
Damien3a205172013-10-12 15:01:56 +0100493 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000494 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100495 }
496 return;
Damiend99b0522013-12-21 18:17:45 +0000497 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100498 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100499 return;
500 }
501 }
502
503 // nothing special, fall back to default compiling for node and jump
504 compile_node(comp, pn);
505 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000506 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100507 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000508 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100509 }
510}
Damien3a205172013-10-12 15:01:56 +0100511#endif
Damien429d7192013-10-04 19:53:11 +0100512
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200513STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100514#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100515 cpython_c_if_cond(comp, pn, jump_if, label, false);
516#else
517 if (node_is_const_false(pn)) {
518 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000519 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100520 }
521 return;
522 } else if (node_is_const_true(pn)) {
523 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000524 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100525 }
526 return;
Damiend99b0522013-12-21 18:17:45 +0000527 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
528 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
529 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
530 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100531 if (jump_if == false) {
532 int label2 = comp_next_label(comp);
533 for (int i = 0; i < n - 1; i++) {
534 c_if_cond(comp, pns->nodes[i], true, label2);
535 }
536 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000537 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100538 } else {
539 for (int i = 0; i < n; i++) {
540 c_if_cond(comp, pns->nodes[i], true, label);
541 }
542 }
543 return;
Damiend99b0522013-12-21 18:17:45 +0000544 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100545 if (jump_if == false) {
546 for (int i = 0; i < n; i++) {
547 c_if_cond(comp, pns->nodes[i], false, label);
548 }
549 } else {
550 int label2 = comp_next_label(comp);
551 for (int i = 0; i < n - 1; i++) {
552 c_if_cond(comp, pns->nodes[i], false, label2);
553 }
554 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000555 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100556 }
557 return;
Damiend99b0522013-12-21 18:17:45 +0000558 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100559 c_if_cond(comp, pns->nodes[0], !jump_if, label);
560 return;
561 }
562 }
563
564 // nothing special, fall back to default compiling for node and jump
565 compile_node(comp, pn);
566 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000567 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100568 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000569 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100570 }
571#endif
Damien429d7192013-10-04 19:53:11 +0100572}
573
574typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000575void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100576
Damiend99b0522013-12-21 18:17:45 +0000577void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100578 if (assign_kind != ASSIGN_AUG_STORE) {
579 compile_node(comp, pns->nodes[0]);
580 }
581
Damiend99b0522013-12-21 18:17:45 +0000582 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
583 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
584 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
585 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100586 if (assign_kind != ASSIGN_AUG_STORE) {
587 for (int i = 0; i < n - 1; i++) {
588 compile_node(comp, pns1->nodes[i]);
589 }
590 }
Damiend99b0522013-12-21 18:17:45 +0000591 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
592 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100593 }
Damiend99b0522013-12-21 18:17:45 +0000594 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000595 compile_syntax_error(comp, "can't assign to function call");
Damien429d7192013-10-04 19:53:11 +0100596 return;
Damiend99b0522013-12-21 18:17:45 +0000597 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100598 if (assign_kind == ASSIGN_AUG_STORE) {
599 EMIT(rot_three);
600 EMIT(store_subscr);
601 } else {
602 compile_node(comp, pns1->nodes[0]);
603 if (assign_kind == ASSIGN_AUG_LOAD) {
604 EMIT(dup_top_two);
Damien Georgeb9791222014-01-23 00:34:21 +0000605 EMIT_ARG(binary_op, RT_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100606 } else {
607 EMIT(store_subscr);
608 }
609 }
Damiend99b0522013-12-21 18:17:45 +0000610 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
611 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100612 if (assign_kind == ASSIGN_AUG_LOAD) {
613 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000614 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100615 } else {
616 if (assign_kind == ASSIGN_AUG_STORE) {
617 EMIT(rot_two);
618 }
Damien Georgeb9791222014-01-23 00:34:21 +0000619 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100620 }
621 } else {
622 // shouldn't happen
623 assert(0);
624 }
625 } else {
626 // shouldn't happen
627 assert(0);
628 }
629
Damiend99b0522013-12-21 18:17:45 +0000630 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +0100631 // SyntaxError, cannot assign
632 assert(0);
633 }
634}
635
Damiend99b0522013-12-21 18:17:45 +0000636void c_assign_tuple(compiler_t *comp, int n, mp_parse_node_t *nodes) {
Damien429d7192013-10-04 19:53:11 +0100637 assert(n >= 0);
638 int have_star_index = -1;
639 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000640 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100641 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000642 EMIT_ARG(unpack_ex, i, n - i - 1);
Damien429d7192013-10-04 19:53:11 +0100643 have_star_index = i;
644 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000645 compile_syntax_error(comp, "two starred expressions in assignment");
Damien429d7192013-10-04 19:53:11 +0100646 return;
647 }
648 }
649 }
650 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000651 EMIT_ARG(unpack_sequence, n);
Damien429d7192013-10-04 19:53:11 +0100652 }
653 for (int i = 0; i < n; i++) {
654 if (i == have_star_index) {
Damiend99b0522013-12-21 18:17:45 +0000655 c_assign(comp, ((mp_parse_node_struct_t*)nodes[i])->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100656 } else {
657 c_assign(comp, nodes[i], ASSIGN_STORE);
658 }
659 }
660}
661
662// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000663void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100664 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000665 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100666 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000667 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
668 if (MP_PARSE_NODE_IS_ID(pn)) {
669 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100670 switch (assign_kind) {
671 case ASSIGN_STORE:
672 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000673 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100674 break;
675 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000676 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100677 break;
678 }
679 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000680 compile_syntax_error(comp, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100681 return;
682 }
683 } else {
Damiend99b0522013-12-21 18:17:45 +0000684 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
685 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100686 case PN_power:
687 // lhs is an index or attribute
688 c_assign_power(comp, pns, assign_kind);
689 break;
690
691 case PN_testlist_star_expr:
692 case PN_exprlist:
693 // lhs is a tuple
694 if (assign_kind != ASSIGN_STORE) {
695 goto bad_aug;
696 }
Damiend99b0522013-12-21 18:17:45 +0000697 c_assign_tuple(comp, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100698 break;
699
700 case PN_atom_paren:
701 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000702 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100703 // empty tuple
Damien Georgef41fdd02014-03-03 23:19:11 +0000704 compile_syntax_error(comp, "can't assign to ()");
Damien429d7192013-10-04 19:53:11 +0100705 return;
Damiend99b0522013-12-21 18:17:45 +0000706 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
707 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100708 goto testlist_comp;
709 } else {
710 // parenthesis around 1 item, is just that item
711 pn = pns->nodes[0];
712 goto tail_recursion;
713 }
714 break;
715
716 case PN_atom_bracket:
717 // lhs is something in brackets
718 if (assign_kind != ASSIGN_STORE) {
719 goto bad_aug;
720 }
Damiend99b0522013-12-21 18:17:45 +0000721 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100722 // empty list, assignment allowed
723 c_assign_tuple(comp, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000724 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
725 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100726 goto testlist_comp;
727 } else {
728 // brackets around 1 item
729 c_assign_tuple(comp, 1, &pns->nodes[0]);
730 }
731 break;
732
733 default:
Damiend99b0522013-12-21 18:17:45 +0000734 printf("unknown assign, %u\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien429d7192013-10-04 19:53:11 +0100735 assert(0);
736 }
737 return;
738
739 testlist_comp:
740 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000741 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
742 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
743 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100744 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000745 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100746 c_assign_tuple(comp, 1, &pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000747 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100748 // sequence of many items
749 // TODO call c_assign_tuple instead
Damiend99b0522013-12-21 18:17:45 +0000750 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
Damien Georgeb9791222014-01-23 00:34:21 +0000751 EMIT_ARG(unpack_sequence, 1 + n);
Damien429d7192013-10-04 19:53:11 +0100752 c_assign(comp, pns->nodes[0], ASSIGN_STORE);
753 for (int i = 0; i < n; i++) {
754 c_assign(comp, pns2->nodes[i], ASSIGN_STORE);
755 }
Damiend99b0522013-12-21 18:17:45 +0000756 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +0100757 // TODO not implemented
758 assert(0);
759 } else {
760 // sequence with 2 items
761 goto sequence_with_2_items;
762 }
763 } else {
764 // sequence with 2 items
765 sequence_with_2_items:
766 c_assign_tuple(comp, 2, pns->nodes);
767 }
768 return;
769 }
770 return;
771
772 bad_aug:
Damien Georgef41fdd02014-03-03 23:19:11 +0000773 compile_syntax_error(comp, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100774}
775
776// stuff for lambda and comprehensions and generators
777void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_dict_params, int n_default_params) {
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000778#if !MICROPY_EMIT_CPYTHON
779 // in Micro Python we put the default params into a tuple using the bytecode
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200780 if (n_default_params) {
781 EMIT_ARG(build_tuple, n_default_params);
782 }
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000783#endif
784
Damien429d7192013-10-04 19:53:11 +0100785 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000786 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100787 int nfree = 0;
788 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000789 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
790 id_info_t *id = &comp->scope_cur->id_info[i];
791 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
792 for (int j = 0; j < this_scope->id_info_len; j++) {
793 id_info_t *id2 = &this_scope->id_info[j];
794 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000795#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000796 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000797#else
798 // in Micro Python we load closures using LOAD_FAST
Damien Georgeb9791222014-01-23 00:34:21 +0000799 EMIT_ARG(load_fast, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000800#endif
Damien318aec62013-12-10 18:28:17 +0000801 nfree += 1;
802 }
803 }
Damien429d7192013-10-04 19:53:11 +0100804 }
805 }
806 }
Damien429d7192013-10-04 19:53:11 +0100807
808 // make the function/closure
809 if (nfree == 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000810 EMIT_ARG(make_function, this_scope, n_dict_params, n_default_params);
Damien429d7192013-10-04 19:53:11 +0100811 } else {
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000812 EMIT_ARG(build_tuple, nfree);
Damien Georgeb9791222014-01-23 00:34:21 +0000813 EMIT_ARG(make_closure, this_scope, n_dict_params, n_default_params);
Damien429d7192013-10-04 19:53:11 +0100814 }
815}
816
Damiend99b0522013-12-21 18:17:45 +0000817void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000818 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damiend99b0522013-12-21 18:17:45 +0000819 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
820 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100821 // bare star
822 comp->have_bare_star = true;
823 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000824
825 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
826 // TODO do we need to do anything with this?
827
828 } else {
829 mp_parse_node_t pn_id;
830 mp_parse_node_t pn_colon;
831 mp_parse_node_t pn_equal;
832 if (MP_PARSE_NODE_IS_ID(pn)) {
833 // this parameter is just an id
834
835 pn_id = pn;
836 pn_colon = MP_PARSE_NODE_NULL;
837 pn_equal = MP_PARSE_NODE_NULL;
838
839 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
840 // this parameter has a colon and/or equal specifier
841
842 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
843 pn_id = pns->nodes[0];
844 pn_colon = pns->nodes[1];
845 pn_equal = pns->nodes[2];
846
847 } else {
848 assert(0);
849 return;
850 }
851
852 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
853 // this parameter does not have a default value
854
855 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
856 if (!comp->have_bare_star && comp->param_pass_num_default_params != 0) {
857 compile_syntax_error(comp, "non-default argument follows default argument");
858 return;
859 }
860
861 } else {
862 // this parameter has a default value
863 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
864
865 if (comp->have_bare_star) {
866 comp->param_pass_num_dict_params += 1;
867 if (comp->param_pass == 1) {
868 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pn_id));
869 compile_node(comp, pn_equal);
870 }
871 } else {
872 comp->param_pass_num_default_params += 1;
873 if (comp->param_pass == 2) {
874 compile_node(comp, pn_equal);
875 }
876 }
877 }
878
879 // TODO pn_colon not implemented
880 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100881 }
882}
883
884// leaves function object on stack
885// returns function name
Damiend99b0522013-12-21 18:17:45 +0000886qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100887 if (comp->pass == PASS_1) {
888 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000889 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100890 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000891 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100892 }
893
894 // save variables (probably don't need to do this, since we can't have nested definitions..?)
895 bool old_have_bare_star = comp->have_bare_star;
896 int old_param_pass = comp->param_pass;
897 int old_param_pass_num_dict_params = comp->param_pass_num_dict_params;
898 int old_param_pass_num_default_params = comp->param_pass_num_default_params;
899
900 // compile default parameters
Damien Georgef41fdd02014-03-03 23:19:11 +0000901
902 // pass 1 does any default parameters after bare star
Damien429d7192013-10-04 19:53:11 +0100903 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000904 comp->param_pass = 1;
Damien429d7192013-10-04 19:53:11 +0100905 comp->param_pass_num_dict_params = 0;
906 comp->param_pass_num_default_params = 0;
907 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +0000908
909 if (comp->had_error) {
910 return MP_QSTR_NULL;
911 }
912
913 // pass 2 does any default parameters before bare star
Damien429d7192013-10-04 19:53:11 +0100914 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000915 comp->param_pass = 2;
Damien429d7192013-10-04 19:53:11 +0100916 comp->param_pass_num_dict_params = 0;
917 comp->param_pass_num_default_params = 0;
918 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
919
920 // get the scope for this function
921 scope_t *fscope = (scope_t*)pns->nodes[4];
922
923 // make the function
924 close_over_variables_etc(comp, fscope, comp->param_pass_num_dict_params, comp->param_pass_num_default_params);
925
926 // restore variables
927 comp->have_bare_star = old_have_bare_star;
928 comp->param_pass = old_param_pass;
929 comp->param_pass_num_dict_params = old_param_pass_num_dict_params;
930 comp->param_pass_num_default_params = old_param_pass_num_default_params;
931
932 // return its name (the 'f' in "def f(...):")
933 return fscope->simple_name;
934}
935
936// leaves class object on stack
937// returns class name
Damiend99b0522013-12-21 18:17:45 +0000938qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100939 if (comp->pass == PASS_1) {
940 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000941 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100942 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000943 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100944 }
945
946 EMIT(load_build_class);
947
948 // scope for this class
949 scope_t *cscope = (scope_t*)pns->nodes[3];
950
951 // compile the class
952 close_over_variables_etc(comp, cscope, 0, 0);
953
954 // get its name
Damien Georgeb9791222014-01-23 00:34:21 +0000955 EMIT_ARG(load_const_id, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100956
957 // nodes[1] has parent classes, if any
Damien Georgebbcd49a2014-02-06 20:30:16 +0000958 comp->func_arg_is_super = false;
959 compile_trailer_paren_helper(comp, pns->nodes[1], false, 2);
Damien429d7192013-10-04 19:53:11 +0100960
961 // return its name (the 'C' in class C(...):")
962 return cscope->simple_name;
963}
964
Damien6cdd3af2013-10-05 18:08:26 +0100965// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200966STATIC bool compile_built_in_decorator(compiler_t *comp, int name_len, mp_parse_node_t *name_nodes, uint *emit_options) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000967 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100968 return false;
969 }
970
971 if (name_len != 2) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000972 compile_syntax_error(comp, "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100973 return true;
974 }
975
Damiend99b0522013-12-21 18:17:45 +0000976 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000977 if (attr == MP_QSTR_byte_code) {
Damien5ac1b2e2013-10-18 19:58:12 +0100978 *emit_options = EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +0100979#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000980 } else if (attr == MP_QSTR_native) {
Damien6cdd3af2013-10-05 18:08:26 +0100981 *emit_options = EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000982 } else if (attr == MP_QSTR_viper) {
Damien7af3d192013-10-07 00:02:49 +0100983 *emit_options = EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100984#endif
Damien3ef4abb2013-10-12 16:53:13 +0100985#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000986 } else if (attr == MP_QSTR_asm_thumb) {
Damien5bfb7592013-10-05 18:41:24 +0100987 *emit_options = EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +0100988#endif
Damien6cdd3af2013-10-05 18:08:26 +0100989 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000990 compile_syntax_error(comp, "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100991 }
992
993 return true;
994}
995
Damiend99b0522013-12-21 18:17:45 +0000996void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100997 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +0000998 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +0100999 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1000
Damien6cdd3af2013-10-05 18:08:26 +01001001 // inherit emit options for this function/class definition
1002 uint emit_options = comp->scope_cur->emit_options;
1003
1004 // compile each decorator
1005 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001006 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001007 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1008 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001009
1010 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001011 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001012 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1013
1014 // check for built-in decorators
1015 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1016 // this was a built-in
1017 num_built_in_decorators += 1;
1018
1019 } else {
1020 // not a built-in, compile normally
1021
1022 // compile the decorator function
1023 compile_node(comp, name_nodes[0]);
1024 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001025 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001026 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001027 }
1028
1029 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001030 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001031 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001032 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001033 compile_node(comp, pns_decorator->nodes[1]);
1034 }
Damien429d7192013-10-04 19:53:11 +01001035 }
1036 }
1037
1038 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001039 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001040 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001041 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001042 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001043 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001044 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001045 } else {
1046 // shouldn't happen
1047 assert(0);
1048 }
1049
1050 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001051 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001052 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01001053 }
1054
1055 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001056 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001057}
1058
Damiend99b0522013-12-21 18:17:45 +00001059void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001060 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001061 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001062 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001063}
1064
Damiend99b0522013-12-21 18:17:45 +00001065void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1066 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001067 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001068 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1069 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001070
1071 compile_node(comp, pns->nodes[0]); // base of the power node
1072
Damiend99b0522013-12-21 18:17:45 +00001073 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1074 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1075 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1076 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001077 for (int i = 0; i < n - 1; i++) {
1078 compile_node(comp, pns1->nodes[i]);
1079 }
Damiend99b0522013-12-21 18:17:45 +00001080 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1081 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001082 }
Damiend99b0522013-12-21 18:17:45 +00001083 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien429d7192013-10-04 19:53:11 +01001084 // SyntaxError: can't delete a function call
1085 assert(0);
Damiend99b0522013-12-21 18:17:45 +00001086 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001087 compile_node(comp, pns1->nodes[0]);
1088 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001089 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1090 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001091 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001092 } else {
1093 // shouldn't happen
1094 assert(0);
1095 }
1096 } else {
1097 // shouldn't happen
1098 assert(0);
1099 }
1100
Damiend99b0522013-12-21 18:17:45 +00001101 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001102 // SyntaxError, cannot delete
1103 assert(0);
1104 }
Damiend99b0522013-12-21 18:17:45 +00001105 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1106 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1107 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1108 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001109 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1110
Damiend99b0522013-12-21 18:17:45 +00001111 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1112 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1113 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001114 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001115 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001116 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001117 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001118 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001119 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001120 c_del_stmt(comp, pns->nodes[0]);
1121 for (int i = 0; i < n; i++) {
1122 c_del_stmt(comp, pns1->nodes[i]);
1123 }
Damiend99b0522013-12-21 18:17:45 +00001124 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001125 // TODO not implemented; can't del comprehension?
1126 assert(0);
1127 } else {
1128 // sequence with 2 items
1129 goto sequence_with_2_items;
1130 }
1131 } else {
1132 // sequence with 2 items
1133 sequence_with_2_items:
1134 c_del_stmt(comp, pns->nodes[0]);
1135 c_del_stmt(comp, pns->nodes[1]);
1136 }
1137 } else {
1138 // tuple with 1 element
1139 c_del_stmt(comp, pn);
1140 }
1141 } else {
1142 // not implemented
1143 assert(0);
1144 }
1145}
1146
Damiend99b0522013-12-21 18:17:45 +00001147void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001148 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1149}
1150
Damiend99b0522013-12-21 18:17:45 +00001151void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001152 if (comp->break_label == 0) {
Damien George2326d522014-03-27 23:26:35 +00001153 compile_syntax_error(comp, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001154 }
Damien Georgecbddb272014-02-01 20:08:18 +00001155 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001156}
1157
Damiend99b0522013-12-21 18:17:45 +00001158void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001159 if (comp->continue_label == 0) {
Damien George2326d522014-03-27 23:26:35 +00001160 compile_syntax_error(comp, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001161 }
Damien Georgecbddb272014-02-01 20:08:18 +00001162 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001163}
1164
Damiend99b0522013-12-21 18:17:45 +00001165void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001166 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001167 compile_syntax_error(comp, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001168 return;
1169 }
Damiend99b0522013-12-21 18:17:45 +00001170 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001171 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001172 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001173 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001174 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001175 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1176 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns_test_if_expr->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001177
Damienb05d7072013-10-05 13:37:10 +01001178 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001179 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1180 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1181 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001182 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001183 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1184 } else {
1185 compile_node(comp, pns->nodes[0]);
1186 }
1187 EMIT(return_value);
1188}
1189
Damiend99b0522013-12-21 18:17:45 +00001190void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001191 compile_node(comp, pns->nodes[0]);
1192 EMIT(pop_top);
1193}
1194
Damiend99b0522013-12-21 18:17:45 +00001195void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1196 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001197 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001198 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001199 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001200 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001201 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001202 compile_node(comp, pns->nodes[0]);
1203 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001204 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001205 } else {
1206 // raise x
1207 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001208 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001209 }
1210}
1211
1212// q1 holds the base, q2 the full name
1213// eg a -> q1=q2=a
1214// a.b.c -> q1=a, q2=a.b.c
Damiend99b0522013-12-21 18:17:45 +00001215void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q1, qstr *q2) {
Damien429d7192013-10-04 19:53:11 +01001216 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001217 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1218 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001219 // a name of the form x as y; unwrap it
Damiend99b0522013-12-21 18:17:45 +00001220 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001221 pn = pns->nodes[0];
1222 is_as = true;
1223 }
Damiend99b0522013-12-21 18:17:45 +00001224 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001225 // just a simple name
Damiend99b0522013-12-21 18:17:45 +00001226 *q2 = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001227 if (!is_as) {
1228 *q1 = *q2;
1229 }
Damien Georgeb9791222014-01-23 00:34:21 +00001230 EMIT_ARG(import_name, *q2);
Damiend99b0522013-12-21 18:17:45 +00001231 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1232 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1233 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001234 // a name of the form a.b.c
1235 if (!is_as) {
Damiend99b0522013-12-21 18:17:45 +00001236 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001237 }
Damiend99b0522013-12-21 18:17:45 +00001238 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001239 int len = n - 1;
1240 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001241 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001242 }
Damien George55baff42014-01-21 21:40:13 +00001243 byte *q_ptr;
1244 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001245 for (int i = 0; i < n; i++) {
1246 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001247 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001248 }
Damien George55baff42014-01-21 21:40:13 +00001249 uint str_src_len;
1250 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001251 memcpy(str_dest, str_src, str_src_len);
1252 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001253 }
Damien George55baff42014-01-21 21:40:13 +00001254 *q2 = qstr_build_end(q_ptr);
Damien Georgeb9791222014-01-23 00:34:21 +00001255 EMIT_ARG(import_name, *q2);
Damien429d7192013-10-04 19:53:11 +01001256 if (is_as) {
1257 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001258 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001259 }
1260 }
1261 } else {
1262 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001263 // This covers relative imports starting with dot(s) like "from .foo import"
Damien429d7192013-10-04 19:53:11 +01001264 assert(0);
1265 }
1266 } else {
1267 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001268 // This covers relative imports with dots only like "from .. import"
Damien429d7192013-10-04 19:53:11 +01001269 assert(0);
1270 }
1271}
1272
Damiend99b0522013-12-21 18:17:45 +00001273void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgeb9791222014-01-23 00:34:21 +00001274 EMIT_ARG(load_const_small_int, 0); // ??
1275 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001276 qstr q1, q2;
1277 do_import_name(comp, pn, &q1, &q2);
Damien Georgeb9791222014-01-23 00:34:21 +00001278 EMIT_ARG(store_id, q1);
Damien429d7192013-10-04 19:53:11 +01001279}
1280
Damiend99b0522013-12-21 18:17:45 +00001281void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001282 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1283}
1284
Damiend99b0522013-12-21 18:17:45 +00001285void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
1286 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001287 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001288
1289 // build the "fromlist" tuple
1290#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001291 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001292#else
Damien Georgeb9791222014-01-23 00:34:21 +00001293 EMIT_ARG(load_const_str, QSTR_FROM_STR_STATIC("*"), false);
1294 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001295#endif
1296
1297 // do the import
Damien429d7192013-10-04 19:53:11 +01001298 qstr dummy_q, id1;
1299 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1300 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001301
Damien429d7192013-10-04 19:53:11 +01001302 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001303 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001304
1305 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001306 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001307 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001308#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001309 {
1310 vstr_t *vstr = vstr_new();
1311 vstr_printf(vstr, "(");
1312 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001313 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1314 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1315 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001316 if (i > 0) {
1317 vstr_printf(vstr, ", ");
1318 }
1319 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001320 uint len;
1321 const byte *str = qstr_data(id2, &len);
1322 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001323 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001324 }
Damien02f89412013-12-12 15:13:36 +00001325 if (n == 1) {
1326 vstr_printf(vstr, ",");
1327 }
1328 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001329 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001330 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001331 }
Damiendb4c3612013-12-10 17:27:24 +00001332#else
1333 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001334 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1335 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1336 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001337 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001338 }
Damien Georgeb9791222014-01-23 00:34:21 +00001339 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001340#endif
1341
1342 // do the import
Damien429d7192013-10-04 19:53:11 +01001343 qstr dummy_q, id1;
1344 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1345 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001346 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1347 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1348 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001349 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001350 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001351 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001352 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001353 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001354 }
1355 }
1356 EMIT(pop_top);
1357 }
1358}
1359
Damiend99b0522013-12-21 18:17:45 +00001360void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001361 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001362 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1363 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001364 } else {
Damiend99b0522013-12-21 18:17:45 +00001365 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1366 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001367 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001368 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001369 }
Damien429d7192013-10-04 19:53:11 +01001370 }
1371 }
1372}
1373
Damiend99b0522013-12-21 18:17:45 +00001374void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001375 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001376 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1377 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001378 } else {
Damiend99b0522013-12-21 18:17:45 +00001379 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1380 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001381 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001382 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001383 }
Damien429d7192013-10-04 19:53:11 +01001384 }
1385 }
1386}
1387
Damiend99b0522013-12-21 18:17:45 +00001388void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001389 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001390 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001391 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 +00001392 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001393 // assertion message
1394 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001395 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01001396 }
Damien Georgeb9791222014-01-23 00:34:21 +00001397 EMIT_ARG(raise_varargs, 1);
1398 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001399}
1400
Damiend99b0522013-12-21 18:17:45 +00001401void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001402 // TODO proper and/or short circuiting
1403
Damienb05d7072013-10-05 13:37:10 +01001404 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001405
Damienb05d7072013-10-05 13:37:10 +01001406 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001407 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1408
1409 compile_node(comp, pns->nodes[1]); // if block
Damiend99b0522013-12-21 18:17:45 +00001410 //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 +01001411 // jump over elif/else blocks if they exist
Damien415eb6f2013-10-05 12:19:06 +01001412 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001413 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001414 }
1415 //}
Damien Georgeb9791222014-01-23 00:34:21 +00001416 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001417
Damiend99b0522013-12-21 18:17:45 +00001418 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001419 // compile elif blocks
1420
Damiend99b0522013-12-21 18:17:45 +00001421 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001422
Damiend99b0522013-12-21 18:17:45 +00001423 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001424 // multiple elif blocks
1425
Damiend99b0522013-12-21 18:17:45 +00001426 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001427 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001428 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001429 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001430 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1431
1432 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001433 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001434 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001435 }
Damien Georgeb9791222014-01-23 00:34:21 +00001436 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001437 }
1438
1439 } else {
1440 // a single elif block
1441
Damienb05d7072013-10-05 13:37:10 +01001442 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001443 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1444
1445 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001446 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001447 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001448 }
Damien Georgeb9791222014-01-23 00:34:21 +00001449 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001450 }
1451 }
1452
1453 // compile else block
1454 compile_node(comp, pns->nodes[3]); // can be null
1455
Damien Georgeb9791222014-01-23 00:34:21 +00001456 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001457}
1458
Damien Georgecbddb272014-02-01 20:08:18 +00001459#define START_BREAK_CONTINUE_BLOCK \
1460 int old_break_label = comp->break_label; \
1461 int old_continue_label = comp->continue_label; \
1462 int break_label = comp_next_label(comp); \
1463 int continue_label = comp_next_label(comp); \
1464 comp->break_label = break_label; \
1465 comp->continue_label = continue_label; \
1466 comp->break_continue_except_level = comp->cur_except_level;
1467
1468#define END_BREAK_CONTINUE_BLOCK \
1469 comp->break_label = old_break_label; \
1470 comp->continue_label = old_continue_label; \
1471 comp->break_continue_except_level = comp->cur_except_level;
1472
Damiend99b0522013-12-21 18:17:45 +00001473void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001474 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001475
Damience89a212013-10-15 22:25:17 +01001476 // compared to CPython, we have an optimised version of while loops
1477#if MICROPY_EMIT_CPYTHON
1478 int done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001479 EMIT_ARG(setup_loop, break_label);
1480 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001481 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1482 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001483 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001484 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001485 }
Damien Georgeb9791222014-01-23 00:34:21 +00001486 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001487 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1488 // this is a small hack to agree with CPython
1489 if (!node_is_const_true(pns->nodes[0])) {
1490 EMIT(pop_block);
1491 }
Damience89a212013-10-15 22:25:17 +01001492#else
1493 int top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001494 EMIT_ARG(jump, continue_label);
1495 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001496 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001497 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001498 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1499#endif
1500
1501 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001502 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001503
1504 compile_node(comp, pns->nodes[2]); // else
1505
Damien Georgeb9791222014-01-23 00:34:21 +00001506 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001507}
1508
Damienf72fd0e2013-11-06 20:20:49 +00001509// TODO preload end and step onto stack if they are not constants
1510// TODO check if step is negative and do opposite test
Damiend99b0522013-12-21 18:17:45 +00001511void 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 +00001512 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001513
1514 int top_label = comp_next_label(comp);
Damien George600ae732014-01-21 23:48:04 +00001515 int entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001516
1517 // compile: var = start
1518 compile_node(comp, pn_start);
1519 c_assign(comp, pn_var, ASSIGN_STORE);
1520
Damien Georgeb9791222014-01-23 00:34:21 +00001521 EMIT_ARG(jump, entry_label);
1522 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001523
Damienf3822fc2013-11-09 20:12:03 +00001524 // compile body
1525 compile_node(comp, pn_body);
1526
Damien Georgeb9791222014-01-23 00:34:21 +00001527 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001528
Damienf72fd0e2013-11-06 20:20:49 +00001529 // compile: var += step
1530 c_assign(comp, pn_var, ASSIGN_AUG_LOAD);
1531 compile_node(comp, pn_step);
Damien Georgeb9791222014-01-23 00:34:21 +00001532 EMIT_ARG(binary_op, RT_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001533 c_assign(comp, pn_var, ASSIGN_AUG_STORE);
1534
Damien Georgeb9791222014-01-23 00:34:21 +00001535 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001536
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001537 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001538 compile_node(comp, pn_var);
1539 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001540 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1541 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien George9aa2a522014-02-01 23:04:09 +00001542 EMIT_ARG(binary_op, RT_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001543 } else {
Damien George9aa2a522014-02-01 23:04:09 +00001544 EMIT_ARG(binary_op, RT_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001545 }
Damien Georgeb9791222014-01-23 00:34:21 +00001546 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001547
1548 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001549 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001550
1551 compile_node(comp, pn_else);
1552
Damien Georgeb9791222014-01-23 00:34:21 +00001553 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001554}
1555
Damiend99b0522013-12-21 18:17:45 +00001556void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001557#if !MICROPY_EMIT_CPYTHON
1558 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1559 // this is actually slower, but uses no heap memory
1560 // for viper it will be much, much faster
Damiend99b0522013-12-21 18:17:45 +00001561 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)) {
1562 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001563 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1564 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1565 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1566 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001567 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1568 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001569 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001570 mp_parse_node_t pn_range_start;
1571 mp_parse_node_t pn_range_end;
1572 mp_parse_node_t pn_range_step;
1573 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001574 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001575 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001576 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001577 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001578 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001579 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001580 } else if (n_args == 2) {
1581 pn_range_start = args[0];
1582 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001583 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001584 } else {
1585 pn_range_start = args[0];
1586 pn_range_end = args[1];
1587 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001588 // We need to know sign of step. This is possible only if it's constant
1589 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1590 optimize = false;
1591 }
Damienf72fd0e2013-11-06 20:20:49 +00001592 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001593 }
1594 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001595 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1596 return;
1597 }
1598 }
1599 }
1600#endif
1601
Damien Georgecbddb272014-02-01 20:08:18 +00001602 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001603
Damienb05d7072013-10-05 13:37:10 +01001604 int pop_label = comp_next_label(comp);
1605 int end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001606
Damience89a212013-10-15 22:25:17 +01001607 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1608#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001609 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001610#endif
1611
Damien429d7192013-10-04 19:53:11 +01001612 compile_node(comp, pns->nodes[1]); // iterator
1613 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001614 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001615 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001616 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1617 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001618 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001619 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001620 }
Damien Georgeb9791222014-01-23 00:34:21 +00001621 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001622 EMIT(for_iter_end);
1623
1624 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001625 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001626
Damience89a212013-10-15 22:25:17 +01001627#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001628 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001629#endif
Damien429d7192013-10-04 19:53:11 +01001630
1631 compile_node(comp, pns->nodes[3]); // else (not tested)
1632
Damien Georgeb9791222014-01-23 00:34:21 +00001633 EMIT_ARG(label_assign, break_label);
1634 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001635}
1636
Damiend99b0522013-12-21 18:17:45 +00001637void 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 +01001638 // this function is a bit of a hack at the moment
1639 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1640
1641 // setup code
1642 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001643 int l1 = comp_next_label(comp);
1644 int success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001645
Damien Georgeb9791222014-01-23 00:34:21 +00001646 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001647 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001648
Damien429d7192013-10-04 19:53:11 +01001649 compile_node(comp, pn_body); // body
1650 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001651 EMIT_ARG(jump, success_label);
1652 EMIT_ARG(label_assign, l1);
Damienb05d7072013-10-05 13:37:10 +01001653 int l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001654
1655 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001656 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1657 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001658
1659 qstr qstr_exception_local = 0;
Damienb05d7072013-10-05 13:37:10 +01001660 int end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001661
Damiend99b0522013-12-21 18:17:45 +00001662 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001663 // this is a catch all exception handler
1664 if (i + 1 != n_except) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001665 compile_syntax_error(comp, "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001666 return;
1667 }
1668 } else {
1669 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001670 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1671 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1672 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1673 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001674 // handler binds the exception to a local
1675 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001676 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001677 }
1678 }
1679 EMIT(dup_top);
1680 compile_node(comp, pns_exception_expr);
Damien George9aa2a522014-02-01 23:04:09 +00001681 EMIT_ARG(binary_op, RT_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001682 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001683 }
1684
1685 EMIT(pop_top);
1686
1687 if (qstr_exception_local == 0) {
1688 EMIT(pop_top);
1689 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001690 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001691 }
1692
1693 EMIT(pop_top);
1694
Damiene2880aa2013-12-20 14:22:59 +00001695 int l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001696 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001697 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001698 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001699 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001700 }
1701 compile_node(comp, pns_except->nodes[1]);
1702 if (qstr_exception_local != 0) {
1703 EMIT(pop_block);
1704 }
1705 EMIT(pop_except);
1706 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001707 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1708 EMIT_ARG(label_assign, l3);
1709 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1710 EMIT_ARG(store_id, qstr_exception_local);
1711 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001712
Damien George8dcc0c72014-03-27 10:55:21 +00001713 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001714 EMIT(end_finally);
1715 }
Damien Georgeb9791222014-01-23 00:34:21 +00001716 EMIT_ARG(jump, l2);
1717 EMIT_ARG(label_assign, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001718 }
1719
Damien George8dcc0c72014-03-27 10:55:21 +00001720 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001721 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001722
Damien Georgeb9791222014-01-23 00:34:21 +00001723 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001724 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001725 EMIT_ARG(label_assign, l2);
1726 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001727}
1728
Damiend99b0522013-12-21 18:17:45 +00001729void 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 +01001730 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1731 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001732 int l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001733
Damien Georgeb9791222014-01-23 00:34:21 +00001734 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001735 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001736
Damien429d7192013-10-04 19:53:11 +01001737 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001738 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien429d7192013-10-04 19:53:11 +01001739 compile_node(comp, pn_body);
1740 } else {
1741 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1742 }
1743 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001744 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1745 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001746 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001747
Damien George8dcc0c72014-03-27 10:55:21 +00001748 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001749 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001750
Damien Georgeb9791222014-01-23 00:34:21 +00001751 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001752}
1753
Damiend99b0522013-12-21 18:17:45 +00001754void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1755 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1756 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1757 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001758 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001759 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1760 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001761 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001762 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001763 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001764 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001765 // no finally
1766 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1767 } else {
1768 // have finally
Damiend99b0522013-12-21 18:17:45 +00001769 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 +01001770 }
1771 } else {
1772 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001773 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001774 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001775 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001776 }
1777 } else {
1778 // shouldn't happen
1779 assert(0);
1780 }
1781}
1782
Damiend99b0522013-12-21 18:17:45 +00001783void 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 +01001784 if (n == 0) {
1785 // no more pre-bits, compile the body of the with
1786 compile_node(comp, body);
1787 } else {
Damienb05d7072013-10-05 13:37:10 +01001788 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001789 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001790 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001791 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001792 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001793 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001794 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1795 } else {
1796 // this pre-bit is just an expression
1797 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001798 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001799 EMIT(pop_top);
1800 }
1801 // compile additional pre-bits and the body
1802 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1803 // finish this with block
1804 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001805 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1806 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001807 EMIT(with_cleanup);
1808 EMIT(end_finally);
1809 }
1810}
1811
Damiend99b0522013-12-21 18:17:45 +00001812void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001813 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001814 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001815 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1816 assert(n > 0);
1817
1818 // compile in a nested fashion
1819 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1820}
1821
Damiend99b0522013-12-21 18:17:45 +00001822void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1823 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001824 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1825 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001826 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001827 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001828 EMIT_ARG(call_function, 1, 0, false, false);
Damien5ac1b2e2013-10-18 19:58:12 +01001829 EMIT(pop_top);
1830
Damien429d7192013-10-04 19:53:11 +01001831 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001832 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001833 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001834 // do nothing with a lonely constant
1835 } else {
1836 compile_node(comp, pns->nodes[0]); // just an expression
1837 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1838 }
Damien429d7192013-10-04 19:53:11 +01001839 }
1840 } else {
Damiend99b0522013-12-21 18:17:45 +00001841 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1842 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001843 if (kind == PN_expr_stmt_augassign) {
1844 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1845 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001846 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien George7e5fb242014-02-01 22:18:47 +00001847 rt_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001848 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien George7e5fb242014-02-01 22:18:47 +00001849 case MP_TOKEN_DEL_PIPE_EQUAL: op = RT_BINARY_OP_INPLACE_OR; break;
1850 case MP_TOKEN_DEL_CARET_EQUAL: op = RT_BINARY_OP_INPLACE_XOR; break;
1851 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = RT_BINARY_OP_INPLACE_AND; break;
1852 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = RT_BINARY_OP_INPLACE_LSHIFT; break;
1853 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = RT_BINARY_OP_INPLACE_RSHIFT; break;
1854 case MP_TOKEN_DEL_PLUS_EQUAL: op = RT_BINARY_OP_INPLACE_ADD; break;
1855 case MP_TOKEN_DEL_MINUS_EQUAL: op = RT_BINARY_OP_INPLACE_SUBTRACT; break;
1856 case MP_TOKEN_DEL_STAR_EQUAL: op = RT_BINARY_OP_INPLACE_MULTIPLY; break;
1857 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = RT_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1858 case MP_TOKEN_DEL_SLASH_EQUAL: op = RT_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1859 case MP_TOKEN_DEL_PERCENT_EQUAL: op = RT_BINARY_OP_INPLACE_MODULO; break;
1860 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = RT_BINARY_OP_INPLACE_POWER; break;
1861 default: assert(0); op = RT_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01001862 }
Damien George7e5fb242014-02-01 22:18:47 +00001863 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001864 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1865 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001866 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1867 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001868 // following CPython, we store left-most first
1869 if (rhs > 0) {
1870 EMIT(dup_top);
1871 }
1872 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1873 for (int i = 0; i < rhs; i++) {
1874 if (i + 1 < rhs) {
1875 EMIT(dup_top);
1876 }
Damiend99b0522013-12-21 18:17:45 +00001877 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001878 }
1879 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001880 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1881 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1882 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1883 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01001884 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001885 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1886 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001887 compile_node(comp, pns10->nodes[0]); // rhs
1888 compile_node(comp, pns10->nodes[1]); // rhs
1889 EMIT(rot_two);
1890 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1891 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00001892 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1893 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1894 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1895 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01001896 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001897 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1898 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001899 compile_node(comp, pns10->nodes[0]); // rhs
1900 compile_node(comp, pns10->nodes[1]); // rhs
1901 compile_node(comp, pns10->nodes[2]); // rhs
1902 EMIT(rot_three);
1903 EMIT(rot_two);
1904 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1905 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1906 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1907 } else {
1908 compile_node(comp, pns1->nodes[0]); // rhs
1909 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1910 }
1911 } else {
1912 // shouldn't happen
1913 assert(0);
1914 }
1915 }
1916}
1917
Damiend99b0522013-12-21 18:17:45 +00001918void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, rt_binary_op_t binary_op) {
1919 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001920 compile_node(comp, pns->nodes[0]);
1921 for (int i = 1; i < num_nodes; i += 1) {
1922 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001923 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001924 }
1925}
1926
Damiend99b0522013-12-21 18:17:45 +00001927void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1928 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1929 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001930
1931 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001932 int l_fail = comp_next_label(comp);
1933 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001934 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1935 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001936 EMIT_ARG(jump, l_end);
1937 EMIT_ARG(label_assign, l_fail);
1938 EMIT_ARG(set_stack_size, stack_size); // force stack size reset
Damien429d7192013-10-04 19:53:11 +01001939 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001940 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001941}
1942
Damiend99b0522013-12-21 18:17:45 +00001943void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001944 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00001945 //mp_parse_node_t pn_params = pns->nodes[0];
1946 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001947
1948 if (comp->pass == PASS_1) {
1949 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00001950 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 +01001951 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001952 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001953 }
1954
1955 // get the scope for this lambda
1956 scope_t *this_scope = (scope_t*)pns->nodes[2];
1957
1958 // make the lambda
1959 close_over_variables_etc(comp, this_scope, 0, 0);
1960}
1961
Damiend99b0522013-12-21 18:17:45 +00001962void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001963 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001964 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001965 for (int i = 0; i < n; i += 1) {
1966 compile_node(comp, pns->nodes[i]);
1967 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00001968 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01001969 }
1970 }
Damien Georgeb9791222014-01-23 00:34:21 +00001971 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001972}
1973
Damiend99b0522013-12-21 18:17:45 +00001974void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001975 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001976 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001977 for (int i = 0; i < n; i += 1) {
1978 compile_node(comp, pns->nodes[i]);
1979 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00001980 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01001981 }
1982 }
Damien Georgeb9791222014-01-23 00:34:21 +00001983 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001984}
1985
Damiend99b0522013-12-21 18:17:45 +00001986void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001987 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001988 EMIT_ARG(unary_op, RT_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01001989}
1990
Damiend99b0522013-12-21 18:17:45 +00001991void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001992 int stack_size = EMIT(get_stack_size);
Damiend99b0522013-12-21 18:17:45 +00001993 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001994 compile_node(comp, pns->nodes[0]);
1995 bool multi = (num_nodes > 3);
1996 int l_fail = 0;
1997 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001998 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001999 }
2000 for (int i = 1; i + 1 < num_nodes; i += 2) {
2001 compile_node(comp, pns->nodes[i + 1]);
2002 if (i + 2 < num_nodes) {
2003 EMIT(dup_top);
2004 EMIT(rot_three);
2005 }
Damien George7e5fb242014-02-01 22:18:47 +00002006 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
2007 rt_binary_op_t op;
2008 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien George9aa2a522014-02-01 23:04:09 +00002009 case MP_TOKEN_OP_LESS: op = RT_BINARY_OP_LESS; break;
2010 case MP_TOKEN_OP_MORE: op = RT_BINARY_OP_MORE; break;
2011 case MP_TOKEN_OP_DBL_EQUAL: op = RT_BINARY_OP_EQUAL; break;
2012 case MP_TOKEN_OP_LESS_EQUAL: op = RT_BINARY_OP_LESS_EQUAL; break;
2013 case MP_TOKEN_OP_MORE_EQUAL: op = RT_BINARY_OP_MORE_EQUAL; break;
2014 case MP_TOKEN_OP_NOT_EQUAL: op = RT_BINARY_OP_NOT_EQUAL; break;
2015 case MP_TOKEN_KW_IN: op = RT_BINARY_OP_IN; break;
2016 default: assert(0); op = RT_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002017 }
2018 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002019 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2020 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2021 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002022 if (kind == PN_comp_op_not_in) {
Damien George9aa2a522014-02-01 23:04:09 +00002023 EMIT_ARG(binary_op, RT_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002024 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002025 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien George9aa2a522014-02-01 23:04:09 +00002026 EMIT_ARG(binary_op, RT_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002027 } else {
Damien George9aa2a522014-02-01 23:04:09 +00002028 EMIT_ARG(binary_op, RT_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002029 }
2030 } else {
2031 // shouldn't happen
2032 assert(0);
2033 }
2034 } else {
2035 // shouldn't happen
2036 assert(0);
2037 }
2038 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002039 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002040 }
2041 }
2042 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002043 int l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002044 EMIT_ARG(jump, l_end);
2045 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01002046 EMIT(rot_two);
2047 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002048 EMIT_ARG(label_assign, l_end);
2049 EMIT_ARG(set_stack_size, stack_size + 1); // force stack size
Damien429d7192013-10-04 19:53:11 +01002050 }
2051}
2052
Damiend99b0522013-12-21 18:17:45 +00002053void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002054 // TODO
2055 assert(0);
2056 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002057 //EMIT_ARG(unary_op, "UNARY_STAR");
Damien429d7192013-10-04 19:53:11 +01002058}
2059
Damiend99b0522013-12-21 18:17:45 +00002060void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002061 c_binary_op(comp, pns, RT_BINARY_OP_OR);
2062}
2063
Damiend99b0522013-12-21 18:17:45 +00002064void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002065 c_binary_op(comp, pns, RT_BINARY_OP_XOR);
2066}
2067
Damiend99b0522013-12-21 18:17:45 +00002068void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002069 c_binary_op(comp, pns, RT_BINARY_OP_AND);
2070}
2071
Damiend99b0522013-12-21 18:17:45 +00002072void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2073 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002074 compile_node(comp, pns->nodes[0]);
2075 for (int i = 1; i + 1 < num_nodes; i += 2) {
2076 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002077 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002078 EMIT_ARG(binary_op, RT_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002079 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002080 EMIT_ARG(binary_op, RT_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002081 } else {
2082 // shouldn't happen
2083 assert(0);
2084 }
2085 }
2086}
2087
Damiend99b0522013-12-21 18:17:45 +00002088void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2089 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002090 compile_node(comp, pns->nodes[0]);
2091 for (int i = 1; i + 1 < num_nodes; i += 2) {
2092 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002093 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002094 EMIT_ARG(binary_op, RT_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002095 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002096 EMIT_ARG(binary_op, RT_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002097 } else {
2098 // shouldn't happen
2099 assert(0);
2100 }
2101 }
2102}
2103
Damiend99b0522013-12-21 18:17:45 +00002104void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2105 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002106 compile_node(comp, pns->nodes[0]);
2107 for (int i = 1; i + 1 < num_nodes; i += 2) {
2108 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002109 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002110 EMIT_ARG(binary_op, RT_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002111 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002112 EMIT_ARG(binary_op, RT_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002113 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002114 EMIT_ARG(binary_op, RT_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002115 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002116 EMIT_ARG(binary_op, RT_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002117 } else {
2118 // shouldn't happen
2119 assert(0);
2120 }
2121 }
2122}
2123
Damiend99b0522013-12-21 18:17:45 +00002124void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002125 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002126 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002127 EMIT_ARG(unary_op, RT_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002128 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002129 EMIT_ARG(unary_op, RT_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002130 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002131 EMIT_ARG(unary_op, RT_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002132 } else {
2133 // shouldn't happen
2134 assert(0);
2135 }
2136}
2137
Damien George35e2a4e2014-02-05 00:51:47 +00002138void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2139 // this is to handle special super() call
2140 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2141
2142 compile_generic_all_nodes(comp, pns);
2143}
2144
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002145STATIC 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 +01002146 // function to call is on top of stack
2147
Damien George35e2a4e2014-02-05 00:51:47 +00002148#if !MICROPY_EMIT_CPYTHON
2149 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002150 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 +00002151 EMIT_ARG(load_id, MP_QSTR___class__);
2152 // get first argument to function
2153 bool found = false;
2154 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
2155 if (comp->scope_cur->id_info[i].param) {
2156 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
2157 found = true;
2158 break;
2159 }
2160 }
2161 if (!found) {
2162 printf("TypeError: super() call cannot find self\n");
2163 return;
2164 }
2165 EMIT_ARG(call_function, 2, 0, false, false);
2166 return;
2167 }
2168#endif
2169
Damien429d7192013-10-04 19:53:11 +01002170 int old_n_arg_keyword = comp->n_arg_keyword;
2171 bool old_have_star_arg = comp->have_star_arg;
2172 bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
2173 comp->n_arg_keyword = 0;
2174 comp->have_star_arg = false;
2175 comp->have_dbl_star_arg = false;
2176
Damien Georgebbcd49a2014-02-06 20:30:16 +00002177 compile_node(comp, pn_arglist); // arguments to function call; can be null
Damien429d7192013-10-04 19:53:11 +01002178
2179 // compute number of positional arguments
Damien Georgebbcd49a2014-02-06 20:30:16 +00002180 int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
Damien429d7192013-10-04 19:53:11 +01002181 if (comp->have_star_arg) {
2182 n_positional -= 1;
2183 }
2184 if (comp->have_dbl_star_arg) {
2185 n_positional -= 1;
2186 }
2187
2188 if (is_method_call) {
Damien Georgeb9791222014-01-23 00:34:21 +00002189 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 +01002190 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002191 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 +01002192 }
2193
2194 comp->n_arg_keyword = old_n_arg_keyword;
2195 comp->have_star_arg = old_have_star_arg;
2196 comp->have_dbl_star_arg = old_have_dbl_star_arg;
2197}
2198
Damiend99b0522013-12-21 18:17:45 +00002199void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2200 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002201 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002202 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 +01002203 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002204 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2205 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002206 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002207 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002208 i += 1;
2209 } else {
2210 compile_node(comp, pns->nodes[i]);
2211 }
Damien George35e2a4e2014-02-05 00:51:47 +00002212 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002213 }
2214}
2215
Damiend99b0522013-12-21 18:17:45 +00002216void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002217 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002218 EMIT_ARG(binary_op, RT_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002219}
2220
Damiend99b0522013-12-21 18:17:45 +00002221void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002222 // a list of strings
Damien63321742013-12-10 17:41:49 +00002223
2224 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002225 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002226 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002227 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002228 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002229 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2230 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2231 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002232 if (i == 0) {
2233 string_kind = pn_kind;
2234 } else if (pn_kind != string_kind) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002235 compile_syntax_error(comp, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002236 return;
2237 }
Damien George55baff42014-01-21 21:40:13 +00002238 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002239 }
Damien63321742013-12-10 17:41:49 +00002240
Damien63321742013-12-10 17:41:49 +00002241 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002242 byte *q_ptr;
2243 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002244 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002245 uint s_len;
2246 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002247 memcpy(s_dest, s, s_len);
2248 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002249 }
Damien George55baff42014-01-21 21:40:13 +00002250 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002251
Damien Georgeb9791222014-01-23 00:34:21 +00002252 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002253}
2254
2255// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002256void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2257 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2258 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2259 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002260
2261 if (comp->pass == PASS_1) {
2262 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002263 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 +01002264 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002265 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002266 }
2267
2268 // get the scope for this comprehension
2269 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2270
2271 // compile the comprehension
2272 close_over_variables_etc(comp, this_scope, 0, 0);
2273
2274 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2275 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002276 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01002277}
2278
Damiend99b0522013-12-21 18:17:45 +00002279void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2280 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002281 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002282 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2283 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2284 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2285 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2286 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2287 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2288 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002289 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002290 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002291 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002292 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002293 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002294 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002295 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002296 // generator expression
2297 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2298 } else {
2299 // tuple with 2 items
2300 goto tuple_with_2_items;
2301 }
2302 } else {
2303 // tuple with 2 items
2304 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002305 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002306 }
2307 } else {
2308 // parenthesis around a single item, is just that item
2309 compile_node(comp, pns->nodes[0]);
2310 }
2311}
2312
Damiend99b0522013-12-21 18:17:45 +00002313void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2314 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002315 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002316 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002317 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2318 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2319 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2320 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2321 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002322 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002323 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002324 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002325 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002326 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002327 // list of many items
2328 compile_node(comp, pns2->nodes[0]);
2329 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002330 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002331 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002332 // list comprehension
2333 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2334 } else {
2335 // list with 2 items
2336 goto list_with_2_items;
2337 }
2338 } else {
2339 // list with 2 items
2340 list_with_2_items:
2341 compile_node(comp, pns2->nodes[0]);
2342 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002343 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002344 }
2345 } else {
2346 // list with 1 item
2347 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002348 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002349 }
2350}
2351
Damiend99b0522013-12-21 18:17:45 +00002352void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2353 mp_parse_node_t pn = pns->nodes[0];
2354 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002355 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002356 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002357 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2358 pns = (mp_parse_node_struct_t*)pn;
2359 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002360 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002361 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002362 compile_node(comp, pn);
2363 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002364 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2365 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2366 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2367 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002368 // dict/set with multiple elements
2369
2370 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002371 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002372 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2373
2374 // first element sets whether it's a dict or set
2375 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002376 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002377 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002378 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002379 compile_node(comp, pns->nodes[0]);
2380 EMIT(store_map);
2381 is_dict = true;
2382 } else {
2383 // a set
2384 compile_node(comp, pns->nodes[0]); // 1st value of set
2385 is_dict = false;
2386 }
2387
2388 // process rest of elements
2389 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002390 mp_parse_node_t pn = nodes[i];
2391 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002392 compile_node(comp, pn);
2393 if (is_dict) {
2394 if (!is_key_value) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002395 compile_syntax_error(comp, "?expecting key:value for dictiona");
Damien429d7192013-10-04 19:53:11 +01002396 return;
2397 }
2398 EMIT(store_map);
2399 } else {
2400 if (is_key_value) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002401 compile_syntax_error(comp, "?expecting just a value for s");
Damien429d7192013-10-04 19:53:11 +01002402 return;
2403 }
2404 }
2405 }
2406
2407 // if it's a set, build it
2408 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002409 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002410 }
Damiend99b0522013-12-21 18:17:45 +00002411 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002412 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002413 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002414 // a dictionary comprehension
2415 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2416 } else {
2417 // a set comprehension
2418 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2419 }
2420 } else {
2421 // shouldn't happen
2422 assert(0);
2423 }
2424 } else {
2425 // set with one element
2426 goto set_with_one_element;
2427 }
2428 } else {
2429 // set with one element
2430 set_with_one_element:
2431 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002432 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002433 }
2434}
2435
Damiend99b0522013-12-21 18:17:45 +00002436void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002437 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002438}
2439
Damiend99b0522013-12-21 18:17:45 +00002440void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002441 // object who's index we want is on top of stack
2442 compile_node(comp, pns->nodes[0]); // the index
Damien Georgeb9791222014-01-23 00:34:21 +00002443 EMIT_ARG(binary_op, RT_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002444}
2445
Damiend99b0522013-12-21 18:17:45 +00002446void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002447 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002448 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002449}
2450
Damiend99b0522013-12-21 18:17:45 +00002451void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2452 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2453 mp_parse_node_t pn = pns->nodes[0];
2454 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002455 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002456 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2457 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002458 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2459 pns = (mp_parse_node_struct_t*)pn;
2460 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002461 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002462 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002463 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002464 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002465 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002466 } else {
2467 // [?::x]
2468 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002469 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002470 }
Damiend99b0522013-12-21 18:17:45 +00002471 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002472 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002473 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2474 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2475 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2476 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002477 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002478 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002479 } else {
2480 // [?:x:x]
2481 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002482 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002483 }
2484 } else {
2485 // [?:x]
2486 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002487 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002488 }
2489 } else {
2490 // [?:x]
2491 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002492 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002493 }
2494}
2495
Damiend99b0522013-12-21 18:17:45 +00002496void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002497 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002498 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2499 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002500}
2501
Damiend99b0522013-12-21 18:17:45 +00002502void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002503 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002504 compile_subscript_3_helper(comp, pns);
2505}
2506
Damiend99b0522013-12-21 18:17:45 +00002507void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002508 // if this is called then we are compiling a dict key:value pair
2509 compile_node(comp, pns->nodes[1]); // value
2510 compile_node(comp, pns->nodes[0]); // key
2511}
2512
Damiend99b0522013-12-21 18:17:45 +00002513void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002514 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002515 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002516 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002517}
2518
Damiend99b0522013-12-21 18:17:45 +00002519void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002520 if (comp->have_star_arg) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002521 compile_syntax_error(comp, "?can't have multiple *x");
Damien429d7192013-10-04 19:53:11 +01002522 return;
2523 }
2524 comp->have_star_arg = true;
2525 compile_node(comp, pns->nodes[0]);
2526}
2527
Damiend99b0522013-12-21 18:17:45 +00002528void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002529 if (comp->have_dbl_star_arg) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002530 compile_syntax_error(comp, "?can't have multiple **x");
Damien429d7192013-10-04 19:53:11 +01002531 return;
2532 }
2533 comp->have_dbl_star_arg = true;
2534 compile_node(comp, pns->nodes[0]);
2535}
2536
Damiend99b0522013-12-21 18:17:45 +00002537void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2538 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2539 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2540 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2541 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002542 compile_syntax_error(comp, "?lhs of keyword argument must be an id");
Damien429d7192013-10-04 19:53:11 +01002543 return;
2544 }
Damien Georgeb9791222014-01-23 00:34:21 +00002545 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002546 compile_node(comp, pns2->nodes[0]);
2547 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002548 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002549 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2550 } else {
2551 // shouldn't happen
2552 assert(0);
2553 }
2554}
2555
Damiend99b0522013-12-21 18:17:45 +00002556void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002557 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002558 compile_syntax_error(comp, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002559 return;
2560 }
Damiend99b0522013-12-21 18:17:45 +00002561 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002562 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002563 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002564 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2565 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002566 compile_node(comp, pns->nodes[0]);
2567 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002568 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002569 EMIT(yield_from);
2570 } else {
2571 compile_node(comp, pns->nodes[0]);
2572 EMIT(yield_value);
2573 }
2574}
2575
Damiend99b0522013-12-21 18:17:45 +00002576typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002577STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002578 NULL,
2579#define nc NULL
2580#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002581#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002582#include "grammar.h"
2583#undef nc
2584#undef c
2585#undef DEF_RULE
2586};
2587
Damiend99b0522013-12-21 18:17:45 +00002588void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2589 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002590 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002591 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2592 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2593 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002594 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002595 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002596 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002597 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002598 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2599 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2600 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2601 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002602 case MP_PARSE_NODE_TOKEN:
2603 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002604 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002605 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002606 // do nothing
2607 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002608 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002609 }
2610 break;
Damien429d7192013-10-04 19:53:11 +01002611 default: assert(0);
2612 }
2613 } else {
Damiend99b0522013-12-21 18:17:45 +00002614 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002615 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002616 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002617 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002618 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002619#if MICROPY_DEBUG_PRINTERS
2620 mp_parse_node_print(pn, 0);
2621#endif
Damien429d7192013-10-04 19:53:11 +01002622 assert(0);
2623 } else {
2624 f(comp, pns);
2625 }
2626 }
2627}
2628
Damiend99b0522013-12-21 18:17:45 +00002629void 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 +01002630 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002631 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002632 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2633 if (MP_PARSE_NODE_IS_ID(pn)) {
2634 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002635 if (comp->have_bare_star) {
2636 // comes after a bare star, so doesn't count as a parameter
2637 } else {
2638 comp->scope_cur->num_params += 1;
2639 }
Damienb14de212013-10-06 00:28:28 +01002640 } else {
Damiend99b0522013-12-21 18:17:45 +00002641 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2642 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2643 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2644 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002645 //int node_index = 1; unused
2646 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002647 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002648 // this parameter has an annotation
2649 pn_annotation = pns->nodes[1];
2650 }
2651 //node_index = 2; unused
2652 }
2653 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002654 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002655 // this parameter has a default value
2656 if (comp->have_bare_star) {
2657 comp->scope_cur->num_dict_params += 1;
2658 } else {
2659 comp->scope_cur->num_default_params += 1;
2660 }
2661 }
2662 */
2663 if (comp->have_bare_star) {
2664 // comes after a bare star, so doesn't count as a parameter
2665 } else {
2666 comp->scope_cur->num_params += 1;
2667 }
Damiend99b0522013-12-21 18:17:45 +00002668 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2669 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002670 // bare star
2671 // TODO see http://www.python.org/dev/peps/pep-3102/
2672 comp->have_bare_star = true;
2673 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002674 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002675 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002676 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002677 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2678 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002679 // named star with annotation
Damien George8725f8f2014-02-15 19:33:11 +00002680 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002681 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2682 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002683 pn_annotation = pns->nodes[1];
2684 } else {
2685 // shouldn't happen
2686 assert(0);
2687 }
Damiend99b0522013-12-21 18:17:45 +00002688 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2689 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2690 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002691 // this parameter has an annotation
2692 pn_annotation = pns->nodes[1];
2693 }
Damien George8725f8f2014-02-15 19:33:11 +00002694 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002695 } else {
Damienb14de212013-10-06 00:28:28 +01002696 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002697 assert(0);
2698 }
Damien429d7192013-10-04 19:53:11 +01002699 }
2700
2701 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002702 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002703 // TODO this parameter has an annotation
2704 }
2705 bool added;
2706 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2707 if (!added) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002708 compile_syntax_error(comp, "?same name used for parameter");
Damien429d7192013-10-04 19:53:11 +01002709 return;
2710 }
2711 id_info->param = true;
2712 id_info->kind = ID_INFO_KIND_LOCAL;
2713 }
2714}
2715
Damiend99b0522013-12-21 18:17:45 +00002716void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002717 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2718}
2719
Damiend99b0522013-12-21 18:17:45 +00002720void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002721 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2722}
2723
Damiend99b0522013-12-21 18:17:45 +00002724void 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 +01002725 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002726 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002727 // no more nested if/for; compile inner expression
2728 compile_node(comp, pn_inner_expr);
2729 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002730 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002731 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002732 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002733 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002734 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002735 } else {
2736 EMIT(yield_value);
2737 EMIT(pop_top);
2738 }
Damiend99b0522013-12-21 18:17:45 +00002739 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002740 // if condition
Damiend99b0522013-12-21 18:17:45 +00002741 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002742 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2743 pn_iter = pns_comp_if->nodes[1];
2744 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002745 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002746 // for loop
Damiend99b0522013-12-21 18:17:45 +00002747 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002748 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002749 int l_end2 = comp_next_label(comp);
2750 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002751 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002752 EMIT_ARG(label_assign, l_top2);
2753 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002754 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2755 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 +00002756 EMIT_ARG(jump, l_top2);
2757 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002758 EMIT(for_iter_end);
2759 } else {
2760 // shouldn't happen
2761 assert(0);
2762 }
2763}
2764
Damiend99b0522013-12-21 18:17:45 +00002765void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002766 // see http://www.python.org/dev/peps/pep-0257/
2767
2768 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002769 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002770 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002771 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002772 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002773 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2774 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002775 for (int i = 0; i < num_nodes; i++) {
2776 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002777 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 +00002778 // not a newline, so this is the first statement; finish search
2779 break;
2780 }
2781 }
2782 // 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 +00002783 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002784 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002785 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002786 } else {
2787 return;
2788 }
2789
2790 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002791 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2792 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2793 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2794 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2795 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002796 compile_node(comp, pns->nodes[0]); // a doc string
2797 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002798 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002799 }
2800 }
2801 }
2802}
2803
2804void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2805 comp->pass = pass;
2806 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002807 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002808 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002809
2810 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002811 // reset maximum stack sizes in scope
2812 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002813 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002814 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002815 }
2816
Damien5ac1b2e2013-10-18 19:58:12 +01002817#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002818 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002819 scope_print_info(scope);
2820 }
Damien5ac1b2e2013-10-18 19:58:12 +01002821#endif
Damien429d7192013-10-04 19:53:11 +01002822
2823 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002824 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2825 assert(scope->kind == SCOPE_MODULE);
2826 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2827 compile_node(comp, pns->nodes[0]); // compile the expression
2828 EMIT(return_value);
2829 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002830 if (!comp->is_repl) {
2831 check_for_doc_string(comp, scope->pn);
2832 }
Damien429d7192013-10-04 19:53:11 +01002833 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002834 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002835 EMIT(return_value);
2836 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002837 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2838 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2839 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002840
2841 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002842 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002843 if (comp->pass == PASS_1) {
2844 comp->have_bare_star = false;
2845 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2846 }
2847
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02002848 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01002849
2850 compile_node(comp, pns->nodes[3]); // 3 is function body
2851 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002852 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002853 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002854 EMIT(return_value);
2855 }
2856 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002857 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2858 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2859 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002860
2861 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002862 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002863 if (comp->pass == PASS_1) {
2864 comp->have_bare_star = false;
2865 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2866 }
2867
2868 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2869 EMIT(return_value);
2870 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2871 // a bit of a hack at the moment
2872
Damiend99b0522013-12-21 18:17:45 +00002873 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2874 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2875 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2876 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2877 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002878
Damien George55baff42014-01-21 21:40:13 +00002879 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01002880 if (comp->pass == PASS_1) {
2881 bool added;
2882 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2883 assert(added);
2884 id_info->kind = ID_INFO_KIND_LOCAL;
2885 scope->num_params = 1;
2886 }
2887
2888 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002889 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01002890 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002891 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01002892 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002893 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01002894 }
2895
Damienb05d7072013-10-05 13:37:10 +01002896 int l_end = comp_next_label(comp);
2897 int l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002898 EMIT_ARG(load_id, qstr_arg);
2899 EMIT_ARG(label_assign, l_top);
2900 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01002901 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2902 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00002903 EMIT_ARG(jump, l_top);
2904 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002905 EMIT(for_iter_end);
2906
2907 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00002908 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002909 }
2910 EMIT(return_value);
2911 } else {
2912 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002913 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2914 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2915 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01002916
2917 if (comp->pass == PASS_1) {
2918 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002919 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01002920 assert(added);
2921 id_info->kind = ID_INFO_KIND_LOCAL;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002922 id_info = scope_find_or_add_id(scope, MP_QSTR___locals__, &added);
Damien429d7192013-10-04 19:53:11 +01002923 assert(added);
2924 id_info->kind = ID_INFO_KIND_LOCAL;
2925 id_info->param = true;
2926 scope->num_params = 1; // __locals__ is the parameter
2927 }
2928
Damien Georgeb9791222014-01-23 00:34:21 +00002929 EMIT_ARG(load_id, MP_QSTR___locals__);
Damien429d7192013-10-04 19:53:11 +01002930 EMIT(store_locals);
Damien Georgeb9791222014-01-23 00:34:21 +00002931 EMIT_ARG(load_id, MP_QSTR___name__);
2932 EMIT_ARG(store_id, MP_QSTR___module__);
2933 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
2934 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01002935
2936 check_for_doc_string(comp, pns->nodes[2]);
2937 compile_node(comp, pns->nodes[2]); // 2 is class body
2938
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002939 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01002940 assert(id != NULL);
2941 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00002942 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002943 } else {
Damien George6baf76e2013-12-30 22:32:17 +00002944#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00002945 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002946#else
Damien George35e2a4e2014-02-05 00:51:47 +00002947 EMIT_ARG(load_fast, MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00002948#endif
Damien429d7192013-10-04 19:53:11 +01002949 }
2950 EMIT(return_value);
2951 }
2952
Damien415eb6f2013-10-05 12:19:06 +01002953 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00002954
2955 // make sure we match all the exception levels
2956 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01002957}
2958
2959void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2960 comp->pass = pass;
2961 comp->scope_cur = scope;
2962 comp->next_label = 1;
2963
2964 if (scope->kind != SCOPE_FUNCTION) {
2965 printf("Error: inline assembler must be a function\n");
2966 return;
2967 }
2968
Damiena2f2f7d2013-10-06 00:14:13 +01002969 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00002970 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01002971 }
2972
Damien826005c2013-10-05 23:17:28 +01002973 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00002974 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2975 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2976 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01002977
Damiend99b0522013-12-21 18:17:45 +00002978 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01002979
Damiena2f2f7d2013-10-06 00:14:13 +01002980 // parameters are in pns->nodes[1]
2981 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00002982 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01002983 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00002984 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01002985 }
2986
Damiend99b0522013-12-21 18:17:45 +00002987 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01002988
Damiend99b0522013-12-21 18:17:45 +00002989 mp_parse_node_t pn_body = pns->nodes[3]; // body
2990 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01002991 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
2992
Damien Georgecbd2f742014-01-19 11:48:48 +00002993 /*
Damien826005c2013-10-05 23:17:28 +01002994 if (comp->pass == PASS_3) {
2995 //printf("----\n");
2996 scope_print_info(scope);
2997 }
Damien Georgecbd2f742014-01-19 11:48:48 +00002998 */
Damien826005c2013-10-05 23:17:28 +01002999
3000 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003001 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3002 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
3003 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
3004 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3005 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3006 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3007 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3008 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3009 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3010 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3011 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3012 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3013 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003014 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3015
3016 // emit instructions
3017 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00003018 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgef41fdd02014-03-03 23:19:11 +00003019 compile_syntax_error(comp, "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003020 return;
3021 }
3022 int lab = comp_next_label(comp);
3023 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003024 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003025 }
3026 } else {
3027 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003028 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003029 }
3030 }
3031 }
3032
3033 if (comp->pass > PASS_1) {
3034 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01003035 }
Damien429d7192013-10-04 19:53:11 +01003036}
3037
3038void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3039 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003040 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003041 scope->num_locals = 0;
3042 for (int i = 0; i < scope->id_info_len; i++) {
3043 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003044 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003045 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3046 continue;
3047 }
3048 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3049 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3050 }
Damien9ecbcff2013-12-11 00:41:43 +00003051 // note: params always count for 1 local, even if they are a cell
Damien429d7192013-10-04 19:53:11 +01003052 if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
3053 id->local_num = scope->num_locals;
3054 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003055 }
3056 }
3057
3058 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003059#if MICROPY_EMIT_CPYTHON
3060 int num_cell = 0;
3061#endif
Damien9ecbcff2013-12-11 00:41:43 +00003062 for (int i = 0; i < scope->id_info_len; i++) {
3063 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003064#if MICROPY_EMIT_CPYTHON
3065 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003066 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003067 id->local_num = num_cell;
3068 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003069 }
Damien George6baf76e2013-12-30 22:32:17 +00003070#else
3071 // in Micro Python the cells come right after the fast locals
3072 // parameters are not counted here, since they remain at the start
3073 // of the locals, even if they are cell vars
3074 if (!id->param && id->kind == ID_INFO_KIND_CELL) {
3075 id->local_num = scope->num_locals;
3076 scope->num_locals += 1;
3077 }
3078#endif
Damien9ecbcff2013-12-11 00:41:43 +00003079 }
Damien9ecbcff2013-12-11 00:41:43 +00003080
3081 // compute the index of free vars (freevars[idx] in CPython)
3082 // make sure they are in the order of the parent scope
3083 if (scope->parent != NULL) {
3084 int num_free = 0;
3085 for (int i = 0; i < scope->parent->id_info_len; i++) {
3086 id_info_t *id = &scope->parent->id_info[i];
3087 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3088 for (int j = 0; j < scope->id_info_len; j++) {
3089 id_info_t *id2 = &scope->id_info[j];
3090 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +00003091 assert(!id2->param); // free vars should not be params
3092#if MICROPY_EMIT_CPYTHON
3093 // in CPython the frees are numbered after the cells
3094 id2->local_num = num_cell + num_free;
3095#else
3096 // in Micro Python the frees come first, before the params
3097 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003098#endif
3099 num_free += 1;
3100 }
3101 }
3102 }
Damien429d7192013-10-04 19:53:11 +01003103 }
Damien George6baf76e2013-12-30 22:32:17 +00003104#if !MICROPY_EMIT_CPYTHON
3105 // in Micro Python shift all other locals after the free locals
3106 if (num_free > 0) {
3107 for (int i = 0; i < scope->id_info_len; i++) {
3108 id_info_t *id = &scope->id_info[i];
3109 if (id->param || id->kind != ID_INFO_KIND_FREE) {
3110 id->local_num += num_free;
3111 }
3112 }
3113 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3114 scope->num_locals += num_free;
3115 }
3116#endif
Damien429d7192013-10-04 19:53:11 +01003117 }
3118
Damien George8725f8f2014-02-15 19:33:11 +00003119 // compute scope_flags
3120 //scope->scope_flags = 0; since we set some things in parameters
Damien429d7192013-10-04 19:53:11 +01003121 if (scope->kind != SCOPE_MODULE) {
Damien George8725f8f2014-02-15 19:33:11 +00003122 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien429d7192013-10-04 19:53:11 +01003123 }
3124 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) {
3125 assert(scope->parent != NULL);
Damien George8725f8f2014-02-15 19:33:11 +00003126 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien429d7192013-10-04 19:53:11 +01003127
3128 // TODO possibly other ways it can be nested
Damien George08d07552014-01-29 18:58:52 +00003129 // Note that we don't actually use this information at the moment (for CPython compat only)
3130 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 +00003131 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003132 }
3133 }
3134 int num_free = 0;
3135 for (int i = 0; i < scope->id_info_len; i++) {
3136 id_info_t *id = &scope->id_info[i];
3137 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3138 num_free += 1;
3139 }
3140 }
3141 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003142 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003143 }
3144}
3145
Damien George08335002014-01-18 23:24:36 +00003146mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
Damien429d7192013-10-04 19:53:11 +01003147 compiler_t *comp = m_new(compiler_t, 1);
3148
Damien Georgecbd2f742014-01-19 11:48:48 +00003149 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003150 comp->is_repl = is_repl;
3151 comp->had_error = false;
3152
Damien429d7192013-10-04 19:53:11 +01003153 comp->break_label = 0;
3154 comp->continue_label = 0;
Damien Georgecbddb272014-02-01 20:08:18 +00003155 comp->break_continue_except_level = 0;
3156 comp->cur_except_level = 0;
3157
Damien George35e2a4e2014-02-05 00:51:47 +00003158 comp->func_arg_is_super = false;
3159
Damien429d7192013-10-04 19:53:11 +01003160 comp->scope_head = NULL;
3161 comp->scope_cur = NULL;
3162
Damien826005c2013-10-05 23:17:28 +01003163 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003164 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003165
3166 // set the outer scope
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003167 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE);
Damien429d7192013-10-04 19:53:11 +01003168
Damien826005c2013-10-05 23:17:28 +01003169 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003170 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003171 comp->emit_method_table = &emit_pass1_method_table;
3172 comp->emit_inline_asm = NULL;
3173 comp->emit_inline_asm_method_table = NULL;
3174 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003175 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003176 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003177#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003178 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003179 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003180#endif
Damien826005c2013-10-05 23:17:28 +01003181 } else {
3182 compile_scope(comp, s, PASS_1);
3183 }
3184
3185 // update maximim number of labels needed
3186 if (comp->next_label > max_num_labels) {
3187 max_num_labels = comp->next_label;
3188 }
Damien429d7192013-10-04 19:53:11 +01003189 }
3190
Damien826005c2013-10-05 23:17:28 +01003191 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003192 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003193 compile_scope_compute_things(comp, s);
3194 }
3195
Damien826005c2013-10-05 23:17:28 +01003196 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003197 emit_pass1_free(comp->emit);
3198
Damien826005c2013-10-05 23:17:28 +01003199 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003200#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003201 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003202#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003203 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003204#endif
Damien3ef4abb2013-10-12 16:53:13 +01003205#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003206 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003207#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003208#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003209 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003210 if (false) {
3211 // dummy
3212
Damien3ef4abb2013-10-12 16:53:13 +01003213#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003214 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
3215 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003216 if (emit_inline_thumb == NULL) {
3217 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3218 }
3219 comp->emit = NULL;
3220 comp->emit_method_table = NULL;
3221 comp->emit_inline_asm = emit_inline_thumb;
3222 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3223 compile_scope_inline_asm(comp, s, PASS_2);
3224 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003225#endif
3226
Damien826005c2013-10-05 23:17:28 +01003227 } else {
Damienc025ebb2013-10-12 14:30:21 +01003228
3229 // choose the emit type
3230
Damien3ef4abb2013-10-12 16:53:13 +01003231#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003232 comp->emit = emit_cpython_new(max_num_labels);
3233 comp->emit_method_table = &emit_cpython_method_table;
3234#else
Damien826005c2013-10-05 23:17:28 +01003235 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003236
3237#if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003238 case EMIT_OPT_NATIVE_PYTHON:
Damien3410be82013-10-07 23:09:10 +01003239 case EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003240#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003241 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003242 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003243 }
Damien13ed3a62013-10-08 09:05:10 +01003244 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003245#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003246 if (emit_native == NULL) {
3247 emit_native = emit_native_thumb_new(max_num_labels);
3248 }
3249 comp->emit_method_table = &emit_native_thumb_method_table;
3250#endif
3251 comp->emit = emit_native;
Damien3410be82013-10-07 23:09:10 +01003252 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003253 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003254#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003255
Damien826005c2013-10-05 23:17:28 +01003256 default:
3257 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003258 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003259 }
3260 comp->emit = emit_bc;
3261 comp->emit_method_table = &emit_bc_method_table;
3262 break;
3263 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003264#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003265
3266 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003267 compile_scope(comp, s, PASS_2);
3268 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003269 }
Damien429d7192013-10-04 19:53:11 +01003270 }
3271
Damien George41d02b62014-01-24 22:42:28 +00003272 // free the emitters
3273#if !MICROPY_EMIT_CPYTHON
3274 if (emit_bc != NULL) {
3275 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003276 }
Damien George41d02b62014-01-24 22:42:28 +00003277#if MICROPY_EMIT_NATIVE
3278 if (emit_native != NULL) {
3279#if MICROPY_EMIT_X64
3280 emit_native_x64_free(emit_native);
3281#elif MICROPY_EMIT_THUMB
3282 emit_native_thumb_free(emit_native);
3283#endif
3284 }
3285#endif
3286#if MICROPY_EMIT_INLINE_THUMB
3287 if (emit_inline_thumb != NULL) {
3288 emit_inline_thumb_free(emit_inline_thumb);
3289 }
3290#endif
3291#endif // !MICROPY_EMIT_CPYTHON
3292
3293 // free the scopes
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003294 uint unique_code_id = module_scope->unique_code_id;
3295 for (scope_t *s = module_scope; s;) {
3296 scope_t *next = s->next;
3297 scope_free(s);
3298 s = next;
3299 }
Damien5ac1b2e2013-10-18 19:58:12 +01003300
Damien George41d02b62014-01-24 22:42:28 +00003301 // free the compiler
3302 bool had_error = comp->had_error;
3303 m_del_obj(compiler_t, comp);
3304
Damien George1fb03172014-01-03 14:22:03 +00003305 if (had_error) {
3306 // TODO return a proper error message
3307 return mp_const_none;
3308 } else {
3309#if MICROPY_EMIT_CPYTHON
3310 // can't create code, so just return true
Damien George41d02b62014-01-24 22:42:28 +00003311 (void)unique_code_id; // to suppress warning that unique_code_id is unused
Damien George1fb03172014-01-03 14:22:03 +00003312 return mp_const_true;
3313#else
3314 // return function that executes the outer module
Paul Sokolovsky90750022014-02-01 15:05:04 +02003315 return rt_make_function_from_id(unique_code_id, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003316#endif
3317 }
Damien429d7192013-10-04 19:53:11 +01003318}