blob: c6c6752d16c0cee10266a197dde7c0a6eec95a53 [file] [log] [blame]
xbeefe34222014-03-16 00:14:26 -07001#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +01002#include <stdint.h>
3#include <stdio.h>
4#include <string.h>
5#include <assert.h>
Rachel Dowdall56402792014-03-22 20:19:24 +00006#include <math.h>
Damien429d7192013-10-04 19:53:11 +01007
8#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00009#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000010#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010011#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010012#include "parse.h"
13#include "scope.h"
Damiend99b0522013-12-21 18:17:45 +000014#include "runtime0.h"
Damien429d7192013-10-04 19:53:11 +010015#include "emit.h"
Damien George2326d522014-03-27 23:26:35 +000016#include "emitglue.h"
Damien George1fb03172014-01-03 14:22:03 +000017#include "obj.h"
18#include "compile.h"
19#include "runtime.h"
Rachel Dowdallcde86312014-03-22 17:29:27 +000020#include "intdivmod.h"
Damien429d7192013-10-04 19:53:11 +010021
22// TODO need to mangle __attr names
23
Damience89a212013-10-15 22:25:17 +010024#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_THUMB)
25
Damien429d7192013-10-04 19:53:11 +010026typedef enum {
27 PN_none = 0,
Damien George00208ce2014-01-23 00:00:53 +000028#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien429d7192013-10-04 19:53:11 +010029#include "grammar.h"
30#undef DEF_RULE
31 PN_maximum_number_of,
32} pn_kind_t;
33
Damien Georgeb9791222014-01-23 00:34:21 +000034#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
35#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
36#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
37#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010038
Damien6cdd3af2013-10-05 18:08:26 +010039#define EMIT_OPT_NONE (0)
40#define EMIT_OPT_BYTE_CODE (1)
41#define EMIT_OPT_NATIVE_PYTHON (2)
Damien7af3d192013-10-07 00:02:49 +010042#define EMIT_OPT_VIPER (3)
43#define EMIT_OPT_ASM_THUMB (4)
Damien6cdd3af2013-10-05 18:08:26 +010044
Damien429d7192013-10-04 19:53:11 +010045typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000046 qstr source_file;
Damien5ac1b2e2013-10-18 19:58:12 +010047 bool is_repl;
Damien429d7192013-10-04 19:53:11 +010048 pass_kind_t pass;
Damien5ac1b2e2013-10-18 19:58:12 +010049 bool had_error; // try to keep compiler clean from nlr
Damien429d7192013-10-04 19:53:11 +010050
Damienb05d7072013-10-05 13:37:10 +010051 int next_label;
Damienb05d7072013-10-05 13:37:10 +010052
Damien429d7192013-10-04 19:53:11 +010053 int break_label;
54 int continue_label;
Damien Georgecbddb272014-02-01 20:08:18 +000055 int break_continue_except_level;
56 int cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien429d7192013-10-04 19:53:11 +010057
58 int n_arg_keyword;
59 bool have_star_arg;
60 bool have_dbl_star_arg;
61 bool have_bare_star;
62 int param_pass;
63 int param_pass_num_dict_params;
64 int param_pass_num_default_params;
65
Damien George35e2a4e2014-02-05 00:51:47 +000066 bool func_arg_is_super; // used to compile special case of super() function call
67
Damien429d7192013-10-04 19:53:11 +010068 scope_t *scope_head;
69 scope_t *scope_cur;
70
Damien6cdd3af2013-10-05 18:08:26 +010071 emit_t *emit; // current emitter
72 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010073
74 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
75 const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
Damien429d7192013-10-04 19:53:11 +010076} compiler_t;
77
Damien Georgef41fdd02014-03-03 23:19:11 +000078STATIC void compile_syntax_error(compiler_t *comp, const char *msg) {
79 // TODO store the error message to a variable in compiler_t instead of printing it
80 printf("SyntaxError: %s\n", msg);
81 comp->had_error = true;
82}
83
Damiend99b0522013-12-21 18:17:45 +000084mp_parse_node_t fold_constants(mp_parse_node_t pn) {
85 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
86 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
87 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +010088
89 // fold arguments first
90 for (int i = 0; i < n; i++) {
91 pns->nodes[i] = fold_constants(pns->nodes[i]);
92 }
93
Damiend99b0522013-12-21 18:17:45 +000094 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +010095 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +000096 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +020097 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
98 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +000099 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien3ef4abb2013-10-12 16:53:13 +0100100#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100101 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000102 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
Damien0efb3a12013-10-12 16:16:56 +0100103#endif
Damiend99b0522013-12-21 18:17:45 +0000104 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
105 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100106 } else {
107 // shouldn't happen
108 assert(0);
109 }
110 }
111 break;
112
113 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100114 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000115 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200116 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
117 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damien0efb3a12013-10-12 16:16:56 +0100118 machine_int_t res;
Damiend99b0522013-12-21 18:17:45 +0000119 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100120 res = arg0 + arg1;
Damiend99b0522013-12-21 18:17:45 +0000121 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100122 res = arg0 - arg1;
Damien429d7192013-10-04 19:53:11 +0100123 } else {
124 // shouldn't happen
125 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100126 res = 0;
127 }
Paul Sokolovskybbf0e2f2014-02-21 02:04:32 +0200128 if (MP_PARSE_FITS_SMALL_INT(res)) {
Damiend99b0522013-12-21 18:17:45 +0000129 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, res);
Damien429d7192013-10-04 19:53:11 +0100130 }
131 }
132 break;
133
134 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000135 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200136 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
137 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000138 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien3ef4abb2013-10-12 16:53:13 +0100139#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100140 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000141 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 * arg1);
Damien0efb3a12013-10-12 16:16:56 +0100142#endif
Damiend99b0522013-12-21 18:17:45 +0000143 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien429d7192013-10-04 19:53:11 +0100144 ; // pass
Damiend99b0522013-12-21 18:17:45 +0000145 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Rachel Dowdallcde86312014-03-22 17:29:27 +0000146 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, python_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000147 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Damien George8dcc0c72014-03-27 10:55:21 +0000148 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, python_floor_divide(arg0, arg1));
Damien429d7192013-10-04 19:53:11 +0100149 } else {
150 // shouldn't happen
151 assert(0);
152 }
153 }
154 break;
155
156 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000157 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200158 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000159 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
160 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
161 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
162 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
163 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
164 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100165 } else {
166 // shouldn't happen
167 assert(0);
168 }
169 }
170 break;
171
Damien3ef4abb2013-10-12 16:53:13 +0100172#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +0100173 case PN_power:
Damien0efb3a12013-10-12 16:16:56 +0100174 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000175 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_NULL(pns->nodes[1]) && !MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
176 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
177 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200178 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100179 if (power >= 0) {
180 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200181 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100182 for (; power > 0; power--) {
183 ans *= base;
184 }
Damiend99b0522013-12-21 18:17:45 +0000185 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100186 }
187 }
188 }
189 break;
Damien0efb3a12013-10-12 16:16:56 +0100190#endif
Damien429d7192013-10-04 19:53:11 +0100191 }
192 }
193
194 return pn;
195}
196
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200197STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra);
Damien George8dcc0c72014-03-27 10:55:21 +0000198STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100199
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200200STATIC int comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100201 return comp->next_label++;
202}
203
Damien George8dcc0c72014-03-27 10:55:21 +0000204STATIC void compile_increase_except_level(compiler_t *comp) {
205 comp->cur_except_level += 1;
206 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
207 comp->scope_cur->exc_stack_size = comp->cur_except_level;
208 }
209}
210
211STATIC void compile_decrease_except_level(compiler_t *comp) {
212 assert(comp->cur_except_level > 0);
213 comp->cur_except_level -= 1;
214}
215
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200216STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) {
Damien George2326d522014-03-27 23:26:35 +0000217 scope_t *scope = scope_new(kind, pn, comp->source_file, mp_emit_glue_get_unique_code_id(), emit_options);
Damien429d7192013-10-04 19:53:11 +0100218 scope->parent = comp->scope_cur;
219 scope->next = NULL;
220 if (comp->scope_head == NULL) {
221 comp->scope_head = scope;
222 } else {
223 scope_t *s = comp->scope_head;
224 while (s->next != NULL) {
225 s = s->next;
226 }
227 s->next = scope;
228 }
229 return scope;
230}
231
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200232STATIC int list_len(mp_parse_node_t pn, int pn_kind) {
Damiend99b0522013-12-21 18:17:45 +0000233 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100234 return 0;
Damiend99b0522013-12-21 18:17:45 +0000235 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100236 return 1;
237 } else {
Damiend99b0522013-12-21 18:17:45 +0000238 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
239 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100240 return 1;
241 } else {
Damiend99b0522013-12-21 18:17:45 +0000242 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100243 }
244 }
245}
246
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200247STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, int pn_list_kind, void (*f)(compiler_t*, mp_parse_node_t)) {
Damiend99b0522013-12-21 18:17:45 +0000248 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
249 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
250 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100251 for (int i = 0; i < num_nodes; i++) {
252 f(comp, pns->nodes[i]);
253 }
Damiend99b0522013-12-21 18:17:45 +0000254 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100255 f(comp, pn);
256 }
257}
258
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200259STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000260 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100261 *nodes = NULL;
262 return 0;
Damiend99b0522013-12-21 18:17:45 +0000263 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100264 *nodes = pn;
265 return 1;
266 } else {
Damiend99b0522013-12-21 18:17:45 +0000267 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
268 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100269 *nodes = pn;
270 return 1;
271 } else {
272 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000273 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100274 }
275 }
276}
277
Damiend99b0522013-12-21 18:17:45 +0000278void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100279}
280
Damiend99b0522013-12-21 18:17:45 +0000281void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
282 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100283 for (int i = 0; i < num_nodes; i++) {
284 compile_node(comp, pns->nodes[i]);
285 }
286}
287
Damien3ef4abb2013-10-12 16:53:13 +0100288#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200289STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000290 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100291 return false;
292 }
Damiend99b0522013-12-21 18:17:45 +0000293 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100294 return false;
295 }
296 return true;
297}
298
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200299STATIC void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
Damien George55baff42014-01-21 21:40:13 +0000300 uint len;
301 const byte *str = qstr_data(qstr, &len);
Damien02f89412013-12-12 15:13:36 +0000302 bool has_single_quote = false;
303 bool has_double_quote = false;
304 for (int i = 0; i < len; i++) {
305 if (str[i] == '\'') {
306 has_single_quote = true;
307 } else if (str[i] == '"') {
308 has_double_quote = true;
309 }
310 }
311 if (bytes) {
312 vstr_printf(vstr, "b");
313 }
314 bool quote_single = false;
315 if (has_single_quote && !has_double_quote) {
316 vstr_printf(vstr, "\"");
317 } else {
318 quote_single = true;
319 vstr_printf(vstr, "'");
320 }
321 for (int i = 0; i < len; i++) {
322 if (str[i] == '\n') {
323 vstr_printf(vstr, "\\n");
324 } else if (str[i] == '\\') {
325 vstr_printf(vstr, "\\\\");
326 } else if (str[i] == '\'' && quote_single) {
327 vstr_printf(vstr, "\\'");
328 } else {
329 vstr_printf(vstr, "%c", str[i]);
330 }
331 }
332 if (has_single_quote && !has_double_quote) {
333 vstr_printf(vstr, "\"");
334 } else {
335 vstr_printf(vstr, "'");
336 }
337}
338
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200339STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damiend99b0522013-12-21 18:17:45 +0000340 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200341 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
342 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
343 return;
344 }
345
Damiend99b0522013-12-21 18:17:45 +0000346 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
347 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
348 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000349 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
350 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
351 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
352 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
353 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100354 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000355 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
356 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
357 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien429d7192013-10-04 19:53:11 +0100358 default: assert(0);
359 }
360 break;
361 default: assert(0);
362 }
363}
364
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200365STATIC void cpython_c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien429d7192013-10-04 19:53:11 +0100366 int n = 0;
367 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000368 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100369 }
370 int total = n;
371 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000372 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100373 total += 1;
Damien3a205172013-10-12 15:01:56 +0100374 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100375 is_const = false;
376 }
377 }
378 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100379 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100380 is_const = false;
381 break;
382 }
383 }
384 if (total > 0 && is_const) {
385 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000386 vstr_t *vstr = vstr_new();
387 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000388 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000389 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100390 need_comma = true;
391 }
392 for (int i = 0; i < n; i++) {
393 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000394 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100395 }
Damien02f89412013-12-12 15:13:36 +0000396 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100397 need_comma = true;
398 }
399 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000400 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100401 } else {
Damien02f89412013-12-12 15:13:36 +0000402 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100403 }
Damien Georgeb9791222014-01-23 00:34:21 +0000404 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000405 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100406 } else {
Damiend99b0522013-12-21 18:17:45 +0000407 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100408 compile_node(comp, pn);
409 }
410 for (int i = 0; i < n; i++) {
411 compile_node(comp, pns_list->nodes[i]);
412 }
Damien Georgeb9791222014-01-23 00:34:21 +0000413 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100414 }
415}
Damien3a205172013-10-12 15:01:56 +0100416#endif
417
418// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damiend99b0522013-12-21 18:17:45 +0000419void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100420#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100421 cpython_c_tuple(comp, pn, pns_list);
422#else
423 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000424 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100425 compile_node(comp, pn);
426 total += 1;
427 }
428 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000429 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100430 for (int i = 0; i < n; i++) {
431 compile_node(comp, pns_list->nodes[i]);
432 }
433 total += n;
434 }
Damien Georgeb9791222014-01-23 00:34:21 +0000435 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100436#endif
437}
Damien429d7192013-10-04 19:53:11 +0100438
Damiend99b0522013-12-21 18:17:45 +0000439void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100440 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000441 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100442}
443
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200444STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000445 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200446 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100447}
448
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200449STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200450 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE) || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 1);
Damien429d7192013-10-04 19:53:11 +0100451}
452
Damien3ef4abb2013-10-12 16:53:13 +0100453#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100454// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200455STATIC void cpython_c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label, bool is_nested) {
Damien429d7192013-10-04 19:53:11 +0100456 if (node_is_const_false(pn)) {
457 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000458 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100459 }
460 return;
461 } else if (node_is_const_true(pn)) {
462 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000463 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100464 }
465 return;
Damiend99b0522013-12-21 18:17:45 +0000466 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
467 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
468 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
469 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100470 if (jump_if == false) {
Damienb05d7072013-10-05 13:37:10 +0100471 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100472 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100473 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100474 }
Damien3a205172013-10-12 15:01:56 +0100475 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000476 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100477 } else {
478 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100479 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100480 }
481 }
482 return;
Damiend99b0522013-12-21 18:17:45 +0000483 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100484 if (jump_if == false) {
485 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100486 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100487 }
488 } else {
Damienb05d7072013-10-05 13:37:10 +0100489 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100490 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100491 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100492 }
Damien3a205172013-10-12 15:01:56 +0100493 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000494 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100495 }
496 return;
Damiend99b0522013-12-21 18:17:45 +0000497 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100498 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100499 return;
500 }
501 }
502
503 // nothing special, fall back to default compiling for node and jump
504 compile_node(comp, pn);
505 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000506 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100507 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000508 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100509 }
510}
Damien3a205172013-10-12 15:01:56 +0100511#endif
Damien429d7192013-10-04 19:53:11 +0100512
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200513STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100514#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100515 cpython_c_if_cond(comp, pn, jump_if, label, false);
516#else
517 if (node_is_const_false(pn)) {
518 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000519 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100520 }
521 return;
522 } else if (node_is_const_true(pn)) {
523 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000524 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100525 }
526 return;
Damiend99b0522013-12-21 18:17:45 +0000527 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
528 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
529 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
530 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100531 if (jump_if == false) {
532 int label2 = comp_next_label(comp);
533 for (int i = 0; i < n - 1; i++) {
534 c_if_cond(comp, pns->nodes[i], true, label2);
535 }
536 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000537 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100538 } else {
539 for (int i = 0; i < n; i++) {
540 c_if_cond(comp, pns->nodes[i], true, label);
541 }
542 }
543 return;
Damiend99b0522013-12-21 18:17:45 +0000544 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100545 if (jump_if == false) {
546 for (int i = 0; i < n; i++) {
547 c_if_cond(comp, pns->nodes[i], false, label);
548 }
549 } else {
550 int label2 = comp_next_label(comp);
551 for (int i = 0; i < n - 1; i++) {
552 c_if_cond(comp, pns->nodes[i], false, label2);
553 }
554 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000555 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100556 }
557 return;
Damiend99b0522013-12-21 18:17:45 +0000558 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100559 c_if_cond(comp, pns->nodes[0], !jump_if, label);
560 return;
561 }
562 }
563
564 // nothing special, fall back to default compiling for node and jump
565 compile_node(comp, pn);
566 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000567 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100568 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000569 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100570 }
571#endif
Damien429d7192013-10-04 19:53:11 +0100572}
573
574typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000575void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100576
Damiend99b0522013-12-21 18:17:45 +0000577void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100578 if (assign_kind != ASSIGN_AUG_STORE) {
579 compile_node(comp, pns->nodes[0]);
580 }
581
Damiend99b0522013-12-21 18:17:45 +0000582 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
583 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
584 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
585 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100586 if (assign_kind != ASSIGN_AUG_STORE) {
587 for (int i = 0; i < n - 1; i++) {
588 compile_node(comp, pns1->nodes[i]);
589 }
590 }
Damiend99b0522013-12-21 18:17:45 +0000591 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
592 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100593 }
Damiend99b0522013-12-21 18:17:45 +0000594 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000595 compile_syntax_error(comp, "can't assign to function call");
Damien429d7192013-10-04 19:53:11 +0100596 return;
Damiend99b0522013-12-21 18:17:45 +0000597 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100598 if (assign_kind == ASSIGN_AUG_STORE) {
599 EMIT(rot_three);
600 EMIT(store_subscr);
601 } else {
602 compile_node(comp, pns1->nodes[0]);
603 if (assign_kind == ASSIGN_AUG_LOAD) {
604 EMIT(dup_top_two);
Damien Georged17926d2014-03-30 13:35:08 +0100605 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100606 } else {
607 EMIT(store_subscr);
608 }
609 }
Damiend99b0522013-12-21 18:17:45 +0000610 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
611 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100612 if (assign_kind == ASSIGN_AUG_LOAD) {
613 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000614 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100615 } else {
616 if (assign_kind == ASSIGN_AUG_STORE) {
617 EMIT(rot_two);
618 }
Damien Georgeb9791222014-01-23 00:34:21 +0000619 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100620 }
621 } else {
622 // shouldn't happen
623 assert(0);
624 }
625 } else {
626 // shouldn't happen
627 assert(0);
628 }
629
Damiend99b0522013-12-21 18:17:45 +0000630 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +0100631 // SyntaxError, cannot assign
632 assert(0);
633 }
634}
635
Damiend99b0522013-12-21 18:17:45 +0000636void c_assign_tuple(compiler_t *comp, int n, mp_parse_node_t *nodes) {
Damien429d7192013-10-04 19:53:11 +0100637 assert(n >= 0);
638 int have_star_index = -1;
639 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000640 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100641 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000642 EMIT_ARG(unpack_ex, i, n - i - 1);
Damien429d7192013-10-04 19:53:11 +0100643 have_star_index = i;
644 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000645 compile_syntax_error(comp, "two starred expressions in assignment");
Damien429d7192013-10-04 19:53:11 +0100646 return;
647 }
648 }
649 }
650 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000651 EMIT_ARG(unpack_sequence, n);
Damien429d7192013-10-04 19:53:11 +0100652 }
653 for (int i = 0; i < n; i++) {
654 if (i == have_star_index) {
Damiend99b0522013-12-21 18:17:45 +0000655 c_assign(comp, ((mp_parse_node_struct_t*)nodes[i])->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100656 } else {
657 c_assign(comp, nodes[i], ASSIGN_STORE);
658 }
659 }
660}
661
662// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000663void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100664 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000665 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100666 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000667 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
668 if (MP_PARSE_NODE_IS_ID(pn)) {
669 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100670 switch (assign_kind) {
671 case ASSIGN_STORE:
672 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000673 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100674 break;
675 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000676 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100677 break;
678 }
679 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000680 compile_syntax_error(comp, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100681 return;
682 }
683 } else {
Damiend99b0522013-12-21 18:17:45 +0000684 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
685 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100686 case PN_power:
687 // lhs is an index or attribute
688 c_assign_power(comp, pns, assign_kind);
689 break;
690
691 case PN_testlist_star_expr:
692 case PN_exprlist:
693 // lhs is a tuple
694 if (assign_kind != ASSIGN_STORE) {
695 goto bad_aug;
696 }
Damiend99b0522013-12-21 18:17:45 +0000697 c_assign_tuple(comp, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100698 break;
699
700 case PN_atom_paren:
701 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000702 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100703 // empty tuple
Damien Georgef41fdd02014-03-03 23:19:11 +0000704 compile_syntax_error(comp, "can't assign to ()");
Damien429d7192013-10-04 19:53:11 +0100705 return;
Damiend99b0522013-12-21 18:17:45 +0000706 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
707 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100708 goto testlist_comp;
709 } else {
710 // parenthesis around 1 item, is just that item
711 pn = pns->nodes[0];
712 goto tail_recursion;
713 }
714 break;
715
716 case PN_atom_bracket:
717 // lhs is something in brackets
718 if (assign_kind != ASSIGN_STORE) {
719 goto bad_aug;
720 }
Damiend99b0522013-12-21 18:17:45 +0000721 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100722 // empty list, assignment allowed
723 c_assign_tuple(comp, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000724 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
725 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100726 goto testlist_comp;
727 } else {
728 // brackets around 1 item
729 c_assign_tuple(comp, 1, &pns->nodes[0]);
730 }
731 break;
732
733 default:
Damiend99b0522013-12-21 18:17:45 +0000734 printf("unknown assign, %u\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien429d7192013-10-04 19:53:11 +0100735 assert(0);
736 }
737 return;
738
739 testlist_comp:
740 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000741 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
742 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
743 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100744 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000745 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100746 c_assign_tuple(comp, 1, &pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000747 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100748 // sequence of many items
749 // TODO call c_assign_tuple instead
Damiend99b0522013-12-21 18:17:45 +0000750 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
Damien Georgeb9791222014-01-23 00:34:21 +0000751 EMIT_ARG(unpack_sequence, 1 + n);
Damien429d7192013-10-04 19:53:11 +0100752 c_assign(comp, pns->nodes[0], ASSIGN_STORE);
753 for (int i = 0; i < n; i++) {
754 c_assign(comp, pns2->nodes[i], ASSIGN_STORE);
755 }
Damiend99b0522013-12-21 18:17:45 +0000756 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +0100757 // TODO not implemented
758 assert(0);
759 } else {
760 // sequence with 2 items
761 goto sequence_with_2_items;
762 }
763 } else {
764 // sequence with 2 items
765 sequence_with_2_items:
766 c_assign_tuple(comp, 2, pns->nodes);
767 }
768 return;
769 }
770 return;
771
772 bad_aug:
Damien Georgef41fdd02014-03-03 23:19:11 +0000773 compile_syntax_error(comp, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100774}
775
776// stuff for lambda and comprehensions and generators
777void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_dict_params, int n_default_params) {
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000778#if !MICROPY_EMIT_CPYTHON
779 // in Micro Python we put the default params into a tuple using the bytecode
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200780 if (n_default_params) {
781 EMIT_ARG(build_tuple, n_default_params);
782 }
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000783#endif
784
Damien429d7192013-10-04 19:53:11 +0100785 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000786 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100787 int nfree = 0;
788 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000789 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
790 id_info_t *id = &comp->scope_cur->id_info[i];
791 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
792 for (int j = 0; j < this_scope->id_info_len; j++) {
793 id_info_t *id2 = &this_scope->id_info[j];
794 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000795#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000796 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000797#else
798 // in Micro Python we load closures using LOAD_FAST
Damien Georgeb9791222014-01-23 00:34:21 +0000799 EMIT_ARG(load_fast, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000800#endif
Damien318aec62013-12-10 18:28:17 +0000801 nfree += 1;
802 }
803 }
Damien429d7192013-10-04 19:53:11 +0100804 }
805 }
806 }
Damien429d7192013-10-04 19:53:11 +0100807
808 // make the function/closure
809 if (nfree == 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000810 EMIT_ARG(make_function, this_scope, n_dict_params, n_default_params);
Damien429d7192013-10-04 19:53:11 +0100811 } else {
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000812 EMIT_ARG(build_tuple, nfree);
Damien Georgeb9791222014-01-23 00:34:21 +0000813 EMIT_ARG(make_closure, this_scope, n_dict_params, n_default_params);
Damien429d7192013-10-04 19:53:11 +0100814 }
815}
816
Damiend99b0522013-12-21 18:17:45 +0000817void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000818 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damiend99b0522013-12-21 18:17:45 +0000819 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
820 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100821 // bare star
822 comp->have_bare_star = true;
823 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000824
825 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
826 // TODO do we need to do anything with this?
827
828 } else {
829 mp_parse_node_t pn_id;
830 mp_parse_node_t pn_colon;
831 mp_parse_node_t pn_equal;
832 if (MP_PARSE_NODE_IS_ID(pn)) {
833 // this parameter is just an id
834
835 pn_id = pn;
836 pn_colon = MP_PARSE_NODE_NULL;
837 pn_equal = MP_PARSE_NODE_NULL;
838
839 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
840 // this parameter has a colon and/or equal specifier
841
842 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
843 pn_id = pns->nodes[0];
844 pn_colon = pns->nodes[1];
845 pn_equal = pns->nodes[2];
846
847 } else {
848 assert(0);
849 return;
850 }
851
852 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
853 // this parameter does not have a default value
854
855 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
856 if (!comp->have_bare_star && comp->param_pass_num_default_params != 0) {
857 compile_syntax_error(comp, "non-default argument follows default argument");
858 return;
859 }
860
861 } else {
862 // this parameter has a default value
863 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
864
865 if (comp->have_bare_star) {
866 comp->param_pass_num_dict_params += 1;
867 if (comp->param_pass == 1) {
868 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pn_id));
869 compile_node(comp, pn_equal);
870 }
871 } else {
872 comp->param_pass_num_default_params += 1;
873 if (comp->param_pass == 2) {
874 compile_node(comp, pn_equal);
875 }
876 }
877 }
878
879 // TODO pn_colon not implemented
880 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100881 }
882}
883
884// leaves function object on stack
885// returns function name
Damiend99b0522013-12-21 18:17:45 +0000886qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100887 if (comp->pass == PASS_1) {
888 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000889 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100890 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000891 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100892 }
893
894 // save variables (probably don't need to do this, since we can't have nested definitions..?)
895 bool old_have_bare_star = comp->have_bare_star;
896 int old_param_pass = comp->param_pass;
897 int old_param_pass_num_dict_params = comp->param_pass_num_dict_params;
898 int old_param_pass_num_default_params = comp->param_pass_num_default_params;
899
900 // compile default parameters
Damien Georgef41fdd02014-03-03 23:19:11 +0000901
902 // pass 1 does any default parameters after bare star
Damien429d7192013-10-04 19:53:11 +0100903 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000904 comp->param_pass = 1;
Damien429d7192013-10-04 19:53:11 +0100905 comp->param_pass_num_dict_params = 0;
906 comp->param_pass_num_default_params = 0;
907 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +0000908
909 if (comp->had_error) {
910 return MP_QSTR_NULL;
911 }
912
913 // pass 2 does any default parameters before bare star
Damien429d7192013-10-04 19:53:11 +0100914 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000915 comp->param_pass = 2;
Damien429d7192013-10-04 19:53:11 +0100916 comp->param_pass_num_dict_params = 0;
917 comp->param_pass_num_default_params = 0;
918 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
919
920 // get the scope for this function
921 scope_t *fscope = (scope_t*)pns->nodes[4];
922
923 // make the function
924 close_over_variables_etc(comp, fscope, comp->param_pass_num_dict_params, comp->param_pass_num_default_params);
925
926 // restore variables
927 comp->have_bare_star = old_have_bare_star;
928 comp->param_pass = old_param_pass;
929 comp->param_pass_num_dict_params = old_param_pass_num_dict_params;
930 comp->param_pass_num_default_params = old_param_pass_num_default_params;
931
932 // return its name (the 'f' in "def f(...):")
933 return fscope->simple_name;
934}
935
936// leaves class object on stack
937// returns class name
Damiend99b0522013-12-21 18:17:45 +0000938qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100939 if (comp->pass == PASS_1) {
940 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000941 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100942 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000943 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100944 }
945
946 EMIT(load_build_class);
947
948 // scope for this class
949 scope_t *cscope = (scope_t*)pns->nodes[3];
950
951 // compile the class
952 close_over_variables_etc(comp, cscope, 0, 0);
953
954 // get its name
Damien Georgeb9791222014-01-23 00:34:21 +0000955 EMIT_ARG(load_const_id, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100956
957 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +0100958 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
959 mp_parse_node_t parents = pns->nodes[1];
960 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
961 parents = MP_PARSE_NODE_NULL;
962 }
Damien Georgebbcd49a2014-02-06 20:30:16 +0000963 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +0100964 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +0100965
966 // return its name (the 'C' in class C(...):")
967 return cscope->simple_name;
968}
969
Damien6cdd3af2013-10-05 18:08:26 +0100970// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200971STATIC 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 +0000972 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100973 return false;
974 }
975
976 if (name_len != 2) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000977 compile_syntax_error(comp, "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100978 return true;
979 }
980
Damiend99b0522013-12-21 18:17:45 +0000981 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000982 if (attr == MP_QSTR_byte_code) {
Damien5ac1b2e2013-10-18 19:58:12 +0100983 *emit_options = EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +0100984#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000985 } else if (attr == MP_QSTR_native) {
Damien6cdd3af2013-10-05 18:08:26 +0100986 *emit_options = EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000987 } else if (attr == MP_QSTR_viper) {
Damien7af3d192013-10-07 00:02:49 +0100988 *emit_options = EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100989#endif
Damien3ef4abb2013-10-12 16:53:13 +0100990#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000991 } else if (attr == MP_QSTR_asm_thumb) {
Damien5bfb7592013-10-05 18:41:24 +0100992 *emit_options = EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +0100993#endif
Damien6cdd3af2013-10-05 18:08:26 +0100994 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000995 compile_syntax_error(comp, "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100996 }
997
998 return true;
999}
1000
Damiend99b0522013-12-21 18:17:45 +00001001void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001002 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001003 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001004 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1005
Damien6cdd3af2013-10-05 18:08:26 +01001006 // inherit emit options for this function/class definition
1007 uint emit_options = comp->scope_cur->emit_options;
1008
1009 // compile each decorator
1010 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001011 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001012 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1013 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001014
1015 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001016 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001017 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1018
1019 // check for built-in decorators
1020 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1021 // this was a built-in
1022 num_built_in_decorators += 1;
1023
1024 } else {
1025 // not a built-in, compile normally
1026
1027 // compile the decorator function
1028 compile_node(comp, name_nodes[0]);
1029 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001030 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001031 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001032 }
1033
1034 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001035 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001036 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001037 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001038 compile_node(comp, pns_decorator->nodes[1]);
1039 }
Damien429d7192013-10-04 19:53:11 +01001040 }
1041 }
1042
1043 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001044 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001045 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001046 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001047 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001048 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001049 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001050 } else {
1051 // shouldn't happen
1052 assert(0);
1053 }
1054
1055 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001056 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001057 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01001058 }
1059
1060 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001061 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001062}
1063
Damiend99b0522013-12-21 18:17:45 +00001064void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001065 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001066 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001067 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001068}
1069
Damiend99b0522013-12-21 18:17:45 +00001070void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1071 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001072 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001073 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1074 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001075
1076 compile_node(comp, pns->nodes[0]); // base of the power node
1077
Damiend99b0522013-12-21 18:17:45 +00001078 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1079 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1080 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1081 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001082 for (int i = 0; i < n - 1; i++) {
1083 compile_node(comp, pns1->nodes[i]);
1084 }
Damiend99b0522013-12-21 18:17:45 +00001085 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1086 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001087 }
Damiend99b0522013-12-21 18:17:45 +00001088 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien429d7192013-10-04 19:53:11 +01001089 // SyntaxError: can't delete a function call
1090 assert(0);
Damiend99b0522013-12-21 18:17:45 +00001091 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001092 compile_node(comp, pns1->nodes[0]);
1093 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001094 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1095 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001096 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001097 } else {
1098 // shouldn't happen
1099 assert(0);
1100 }
1101 } else {
1102 // shouldn't happen
1103 assert(0);
1104 }
1105
Damiend99b0522013-12-21 18:17:45 +00001106 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001107 // SyntaxError, cannot delete
1108 assert(0);
1109 }
Damiend99b0522013-12-21 18:17:45 +00001110 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1111 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1112 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1113 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001114 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1115
Damiend99b0522013-12-21 18:17:45 +00001116 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1117 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1118 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001119 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001120 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001121 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001122 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001123 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001124 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001125 c_del_stmt(comp, pns->nodes[0]);
1126 for (int i = 0; i < n; i++) {
1127 c_del_stmt(comp, pns1->nodes[i]);
1128 }
Damiend99b0522013-12-21 18:17:45 +00001129 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001130 // TODO not implemented; can't del comprehension?
1131 assert(0);
1132 } else {
1133 // sequence with 2 items
1134 goto sequence_with_2_items;
1135 }
1136 } else {
1137 // sequence with 2 items
1138 sequence_with_2_items:
1139 c_del_stmt(comp, pns->nodes[0]);
1140 c_del_stmt(comp, pns->nodes[1]);
1141 }
1142 } else {
1143 // tuple with 1 element
1144 c_del_stmt(comp, pn);
1145 }
1146 } else {
1147 // not implemented
1148 assert(0);
1149 }
1150}
1151
Damiend99b0522013-12-21 18:17:45 +00001152void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001153 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1154}
1155
Damiend99b0522013-12-21 18:17:45 +00001156void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001157 if (comp->break_label == 0) {
Damien George2326d522014-03-27 23:26:35 +00001158 compile_syntax_error(comp, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001159 }
Damien Georgecbddb272014-02-01 20:08:18 +00001160 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001161}
1162
Damiend99b0522013-12-21 18:17:45 +00001163void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001164 if (comp->continue_label == 0) {
Damien George2326d522014-03-27 23:26:35 +00001165 compile_syntax_error(comp, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001166 }
Damien Georgecbddb272014-02-01 20:08:18 +00001167 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001168}
1169
Damiend99b0522013-12-21 18:17:45 +00001170void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001171 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001172 compile_syntax_error(comp, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001173 return;
1174 }
Damiend99b0522013-12-21 18:17:45 +00001175 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001176 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001177 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001178 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001179 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001180 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1181 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 +01001182
Damienb05d7072013-10-05 13:37:10 +01001183 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001184 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1185 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1186 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001187 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001188 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1189 } else {
1190 compile_node(comp, pns->nodes[0]);
1191 }
1192 EMIT(return_value);
1193}
1194
Damiend99b0522013-12-21 18:17:45 +00001195void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001196 compile_node(comp, pns->nodes[0]);
1197 EMIT(pop_top);
1198}
1199
Damiend99b0522013-12-21 18:17:45 +00001200void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1201 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001202 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001203 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001204 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001205 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001206 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001207 compile_node(comp, pns->nodes[0]);
1208 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001209 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001210 } else {
1211 // raise x
1212 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001213 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001214 }
1215}
1216
1217// q1 holds the base, q2 the full name
1218// eg a -> q1=q2=a
1219// a.b.c -> q1=a, q2=a.b.c
Damiend99b0522013-12-21 18:17:45 +00001220void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q1, qstr *q2) {
Damien429d7192013-10-04 19:53:11 +01001221 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001222 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1223 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001224 // a name of the form x as y; unwrap it
Damiend99b0522013-12-21 18:17:45 +00001225 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001226 pn = pns->nodes[0];
1227 is_as = true;
1228 }
Damiend99b0522013-12-21 18:17:45 +00001229 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001230 // just a simple name
Damiend99b0522013-12-21 18:17:45 +00001231 *q2 = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001232 if (!is_as) {
1233 *q1 = *q2;
1234 }
Damien Georgeb9791222014-01-23 00:34:21 +00001235 EMIT_ARG(import_name, *q2);
Damiend99b0522013-12-21 18:17:45 +00001236 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1237 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1238 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001239 // a name of the form a.b.c
1240 if (!is_as) {
Damiend99b0522013-12-21 18:17:45 +00001241 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001242 }
Damiend99b0522013-12-21 18:17:45 +00001243 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001244 int len = n - 1;
1245 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001246 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001247 }
Damien George55baff42014-01-21 21:40:13 +00001248 byte *q_ptr;
1249 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001250 for (int i = 0; i < n; i++) {
1251 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001252 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001253 }
Damien George55baff42014-01-21 21:40:13 +00001254 uint str_src_len;
1255 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001256 memcpy(str_dest, str_src, str_src_len);
1257 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001258 }
Damien George55baff42014-01-21 21:40:13 +00001259 *q2 = qstr_build_end(q_ptr);
Damien Georgeb9791222014-01-23 00:34:21 +00001260 EMIT_ARG(import_name, *q2);
Damien429d7192013-10-04 19:53:11 +01001261 if (is_as) {
1262 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001263 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001264 }
1265 }
1266 } else {
1267 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001268 // This covers relative imports starting with dot(s) like "from .foo import"
Paul Sokolovsky8d9cc2e2014-03-30 02:31:08 +02001269 compile_syntax_error(comp, "Relative imports not implemented");
Damien429d7192013-10-04 19:53:11 +01001270 assert(0);
1271 }
1272 } else {
1273 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001274 // This covers relative imports with dots only like "from .. import"
Paul Sokolovsky8d9cc2e2014-03-30 02:31:08 +02001275 compile_syntax_error(comp, "Relative imports not implemented");
Damien429d7192013-10-04 19:53:11 +01001276 assert(0);
1277 }
1278}
1279
Damiend99b0522013-12-21 18:17:45 +00001280void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgeb9791222014-01-23 00:34:21 +00001281 EMIT_ARG(load_const_small_int, 0); // ??
1282 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001283 qstr q1, q2;
1284 do_import_name(comp, pn, &q1, &q2);
Damien Georgeb9791222014-01-23 00:34:21 +00001285 EMIT_ARG(store_id, q1);
Damien429d7192013-10-04 19:53:11 +01001286}
1287
Damiend99b0522013-12-21 18:17:45 +00001288void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001289 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1290}
1291
Damiend99b0522013-12-21 18:17:45 +00001292void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
1293 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001294 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001295
1296 // build the "fromlist" tuple
1297#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001298 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001299#else
Damien Georgeb9791222014-01-23 00:34:21 +00001300 EMIT_ARG(load_const_str, QSTR_FROM_STR_STATIC("*"), false);
1301 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001302#endif
1303
1304 // do the import
Damien429d7192013-10-04 19:53:11 +01001305 qstr dummy_q, id1;
1306 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1307 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001308
Damien429d7192013-10-04 19:53:11 +01001309 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001310 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001311
1312 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001313 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001314 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001315#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001316 {
1317 vstr_t *vstr = vstr_new();
1318 vstr_printf(vstr, "(");
1319 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001320 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1321 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1322 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001323 if (i > 0) {
1324 vstr_printf(vstr, ", ");
1325 }
1326 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001327 uint len;
1328 const byte *str = qstr_data(id2, &len);
1329 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001330 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001331 }
Damien02f89412013-12-12 15:13:36 +00001332 if (n == 1) {
1333 vstr_printf(vstr, ",");
1334 }
1335 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001336 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001337 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001338 }
Damiendb4c3612013-12-10 17:27:24 +00001339#else
1340 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001341 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1342 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1343 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001344 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001345 }
Damien Georgeb9791222014-01-23 00:34:21 +00001346 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001347#endif
1348
1349 // do the import
Damien429d7192013-10-04 19:53:11 +01001350 qstr dummy_q, id1;
1351 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1352 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001353 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1354 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1355 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001356 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001357 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001358 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001359 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001360 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001361 }
1362 }
1363 EMIT(pop_top);
1364 }
1365}
1366
Damiend99b0522013-12-21 18:17:45 +00001367void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001368 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001369 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1370 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001371 } else {
Damiend99b0522013-12-21 18:17:45 +00001372 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1373 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001374 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001375 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001376 }
Damien429d7192013-10-04 19:53:11 +01001377 }
1378 }
1379}
1380
Damiend99b0522013-12-21 18:17:45 +00001381void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001382 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001383 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1384 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001385 } else {
Damiend99b0522013-12-21 18:17:45 +00001386 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1387 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001388 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001389 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001390 }
Damien429d7192013-10-04 19:53:11 +01001391 }
1392 }
1393}
1394
Damiend99b0522013-12-21 18:17:45 +00001395void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001396 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001397 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001398 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 +00001399 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001400 // assertion message
1401 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001402 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01001403 }
Damien Georgeb9791222014-01-23 00:34:21 +00001404 EMIT_ARG(raise_varargs, 1);
1405 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001406}
1407
Damiend99b0522013-12-21 18:17:45 +00001408void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001409 // TODO proper and/or short circuiting
1410
Damienb05d7072013-10-05 13:37:10 +01001411 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001412
Damienb05d7072013-10-05 13:37:10 +01001413 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001414 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1415
1416 compile_node(comp, pns->nodes[1]); // if block
Damiend99b0522013-12-21 18:17:45 +00001417 //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 +01001418 // jump over elif/else blocks if they exist
Damien415eb6f2013-10-05 12:19:06 +01001419 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001420 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001421 }
1422 //}
Damien Georgeb9791222014-01-23 00:34:21 +00001423 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001424
Damiend99b0522013-12-21 18:17:45 +00001425 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001426 // compile elif blocks
1427
Damiend99b0522013-12-21 18:17:45 +00001428 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001429
Damiend99b0522013-12-21 18:17:45 +00001430 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001431 // multiple elif blocks
1432
Damiend99b0522013-12-21 18:17:45 +00001433 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001434 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001435 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001436 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001437 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1438
1439 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001440 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001441 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001442 }
Damien Georgeb9791222014-01-23 00:34:21 +00001443 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001444 }
1445
1446 } else {
1447 // a single elif block
1448
Damienb05d7072013-10-05 13:37:10 +01001449 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001450 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1451
1452 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001453 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001454 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001455 }
Damien Georgeb9791222014-01-23 00:34:21 +00001456 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001457 }
1458 }
1459
1460 // compile else block
1461 compile_node(comp, pns->nodes[3]); // can be null
1462
Damien Georgeb9791222014-01-23 00:34:21 +00001463 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001464}
1465
Damien Georgecbddb272014-02-01 20:08:18 +00001466#define START_BREAK_CONTINUE_BLOCK \
1467 int old_break_label = comp->break_label; \
1468 int old_continue_label = comp->continue_label; \
1469 int break_label = comp_next_label(comp); \
1470 int continue_label = comp_next_label(comp); \
1471 comp->break_label = break_label; \
1472 comp->continue_label = continue_label; \
1473 comp->break_continue_except_level = comp->cur_except_level;
1474
1475#define END_BREAK_CONTINUE_BLOCK \
1476 comp->break_label = old_break_label; \
1477 comp->continue_label = old_continue_label; \
1478 comp->break_continue_except_level = comp->cur_except_level;
1479
Damiend99b0522013-12-21 18:17:45 +00001480void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001481 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001482
Damience89a212013-10-15 22:25:17 +01001483 // compared to CPython, we have an optimised version of while loops
1484#if MICROPY_EMIT_CPYTHON
1485 int done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001486 EMIT_ARG(setup_loop, break_label);
1487 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001488 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1489 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001490 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001491 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001492 }
Damien Georgeb9791222014-01-23 00:34:21 +00001493 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001494 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1495 // this is a small hack to agree with CPython
1496 if (!node_is_const_true(pns->nodes[0])) {
1497 EMIT(pop_block);
1498 }
Damience89a212013-10-15 22:25:17 +01001499#else
1500 int top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001501 EMIT_ARG(jump, continue_label);
1502 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001503 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001504 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001505 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1506#endif
1507
1508 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001509 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001510
1511 compile_node(comp, pns->nodes[2]); // else
1512
Damien Georgeb9791222014-01-23 00:34:21 +00001513 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001514}
1515
Damienf72fd0e2013-11-06 20:20:49 +00001516// TODO preload end and step onto stack if they are not constants
1517// TODO check if step is negative and do opposite test
Damiend99b0522013-12-21 18:17:45 +00001518void 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 +00001519 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001520
1521 int top_label = comp_next_label(comp);
Damien George600ae732014-01-21 23:48:04 +00001522 int entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001523
1524 // compile: var = start
1525 compile_node(comp, pn_start);
1526 c_assign(comp, pn_var, ASSIGN_STORE);
1527
Damien Georgeb9791222014-01-23 00:34:21 +00001528 EMIT_ARG(jump, entry_label);
1529 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001530
Damienf3822fc2013-11-09 20:12:03 +00001531 // compile body
1532 compile_node(comp, pn_body);
1533
Damien Georgeb9791222014-01-23 00:34:21 +00001534 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001535
Damienf72fd0e2013-11-06 20:20:49 +00001536 // compile: var += step
1537 c_assign(comp, pn_var, ASSIGN_AUG_LOAD);
1538 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001539 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001540 c_assign(comp, pn_var, ASSIGN_AUG_STORE);
1541
Damien Georgeb9791222014-01-23 00:34:21 +00001542 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001543
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001544 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001545 compile_node(comp, pn_var);
1546 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001547 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1548 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001549 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001550 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001551 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001552 }
Damien Georgeb9791222014-01-23 00:34:21 +00001553 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001554
1555 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001556 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001557
1558 compile_node(comp, pn_else);
1559
Damien Georgeb9791222014-01-23 00:34:21 +00001560 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001561}
1562
Damiend99b0522013-12-21 18:17:45 +00001563void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001564#if !MICROPY_EMIT_CPYTHON
1565 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1566 // this is actually slower, but uses no heap memory
1567 // for viper it will be much, much faster
Damiend99b0522013-12-21 18:17:45 +00001568 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)) {
1569 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001570 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1571 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1572 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1573 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001574 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1575 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001576 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001577 mp_parse_node_t pn_range_start;
1578 mp_parse_node_t pn_range_end;
1579 mp_parse_node_t pn_range_step;
1580 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001581 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001582 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001583 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001584 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001585 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001586 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001587 } else if (n_args == 2) {
1588 pn_range_start = args[0];
1589 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001590 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001591 } else {
1592 pn_range_start = args[0];
1593 pn_range_end = args[1];
1594 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001595 // We need to know sign of step. This is possible only if it's constant
1596 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1597 optimize = false;
1598 }
Damienf72fd0e2013-11-06 20:20:49 +00001599 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001600 }
1601 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001602 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1603 return;
1604 }
1605 }
1606 }
1607#endif
1608
Damien Georgecbddb272014-02-01 20:08:18 +00001609 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001610
Damienb05d7072013-10-05 13:37:10 +01001611 int pop_label = comp_next_label(comp);
1612 int end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001613
Damience89a212013-10-15 22:25:17 +01001614 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1615#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001616 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001617#endif
1618
Damien429d7192013-10-04 19:53:11 +01001619 compile_node(comp, pns->nodes[1]); // iterator
1620 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001621 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001622 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001623 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1624 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001625 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001626 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001627 }
Damien Georgeb9791222014-01-23 00:34:21 +00001628 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001629 EMIT(for_iter_end);
1630
1631 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001632 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001633
Damience89a212013-10-15 22:25:17 +01001634#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001635 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001636#endif
Damien429d7192013-10-04 19:53:11 +01001637
1638 compile_node(comp, pns->nodes[3]); // else (not tested)
1639
Damien Georgeb9791222014-01-23 00:34:21 +00001640 EMIT_ARG(label_assign, break_label);
1641 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001642}
1643
Damiend99b0522013-12-21 18:17:45 +00001644void 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 +01001645 // this function is a bit of a hack at the moment
1646 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1647
1648 // setup code
1649 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001650 int l1 = comp_next_label(comp);
1651 int success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001652
Damien Georgeb9791222014-01-23 00:34:21 +00001653 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001654 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001655
Damien429d7192013-10-04 19:53:11 +01001656 compile_node(comp, pn_body); // body
1657 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001658 EMIT_ARG(jump, success_label);
1659 EMIT_ARG(label_assign, l1);
Damienb05d7072013-10-05 13:37:10 +01001660 int l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001661
1662 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001663 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1664 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001665
1666 qstr qstr_exception_local = 0;
Damienb05d7072013-10-05 13:37:10 +01001667 int end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001668
Damiend99b0522013-12-21 18:17:45 +00001669 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001670 // this is a catch all exception handler
1671 if (i + 1 != n_except) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001672 compile_syntax_error(comp, "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001673 return;
1674 }
1675 } else {
1676 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001677 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1678 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1679 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1680 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001681 // handler binds the exception to a local
1682 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001683 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001684 }
1685 }
1686 EMIT(dup_top);
1687 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001688 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001689 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001690 }
1691
1692 EMIT(pop_top);
1693
1694 if (qstr_exception_local == 0) {
1695 EMIT(pop_top);
1696 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001697 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001698 }
1699
1700 EMIT(pop_top);
1701
Damiene2880aa2013-12-20 14:22:59 +00001702 int l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001703 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001704 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001705 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001706 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001707 }
1708 compile_node(comp, pns_except->nodes[1]);
1709 if (qstr_exception_local != 0) {
1710 EMIT(pop_block);
1711 }
1712 EMIT(pop_except);
1713 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001714 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1715 EMIT_ARG(label_assign, l3);
1716 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1717 EMIT_ARG(store_id, qstr_exception_local);
1718 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001719
Damien George8dcc0c72014-03-27 10:55:21 +00001720 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001721 EMIT(end_finally);
1722 }
Damien Georgeb9791222014-01-23 00:34:21 +00001723 EMIT_ARG(jump, l2);
1724 EMIT_ARG(label_assign, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001725 }
1726
Damien George8dcc0c72014-03-27 10:55:21 +00001727 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001728 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001729
Damien Georgeb9791222014-01-23 00:34:21 +00001730 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001731 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001732 EMIT_ARG(label_assign, l2);
1733 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001734}
1735
Damiend99b0522013-12-21 18:17:45 +00001736void 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 +01001737 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1738 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001739 int l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001740
Damien Georgeb9791222014-01-23 00:34:21 +00001741 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001742 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001743
Damien429d7192013-10-04 19:53:11 +01001744 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001745 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien429d7192013-10-04 19:53:11 +01001746 compile_node(comp, pn_body);
1747 } else {
1748 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1749 }
1750 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001751 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1752 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001753 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001754
Damien George8dcc0c72014-03-27 10:55:21 +00001755 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001756 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001757
Damien Georgeb9791222014-01-23 00:34:21 +00001758 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001759}
1760
Damiend99b0522013-12-21 18:17:45 +00001761void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1762 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1763 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1764 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001765 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001766 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1767 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001768 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001769 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001770 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001771 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001772 // no finally
1773 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1774 } else {
1775 // have finally
Damiend99b0522013-12-21 18:17:45 +00001776 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 +01001777 }
1778 } else {
1779 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001780 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001781 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001782 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001783 }
1784 } else {
1785 // shouldn't happen
1786 assert(0);
1787 }
1788}
1789
Damiend99b0522013-12-21 18:17:45 +00001790void 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 +01001791 if (n == 0) {
1792 // no more pre-bits, compile the body of the with
1793 compile_node(comp, body);
1794 } else {
Damienb05d7072013-10-05 13:37:10 +01001795 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001796 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001797 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001798 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001799 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001800 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001801 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1802 } else {
1803 // this pre-bit is just an expression
1804 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001805 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001806 EMIT(pop_top);
1807 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001808 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001809 // compile additional pre-bits and the body
1810 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1811 // finish this with block
1812 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001813 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1814 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001815 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001816 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001817 EMIT(end_finally);
1818 }
1819}
1820
Damiend99b0522013-12-21 18:17:45 +00001821void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001822 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001823 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001824 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1825 assert(n > 0);
1826
1827 // compile in a nested fashion
1828 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1829}
1830
Damiend99b0522013-12-21 18:17:45 +00001831void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1832 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001833 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1834 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001835 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001836 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001837 EMIT_ARG(call_function, 1, 0, false, false);
Damien5ac1b2e2013-10-18 19:58:12 +01001838 EMIT(pop_top);
1839
Damien429d7192013-10-04 19:53:11 +01001840 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001841 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001842 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001843 // do nothing with a lonely constant
1844 } else {
1845 compile_node(comp, pns->nodes[0]); // just an expression
1846 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1847 }
Damien429d7192013-10-04 19:53:11 +01001848 }
1849 } else {
Damiend99b0522013-12-21 18:17:45 +00001850 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1851 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001852 if (kind == PN_expr_stmt_augassign) {
1853 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1854 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001855 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001856 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001857 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001858 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1859 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1860 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1861 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1862 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1863 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1864 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1865 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1866 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1867 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1868 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
1869 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
1870 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01001871 }
Damien George7e5fb242014-02-01 22:18:47 +00001872 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001873 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1874 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001875 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1876 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001877 // following CPython, we store left-most first
1878 if (rhs > 0) {
1879 EMIT(dup_top);
1880 }
1881 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1882 for (int i = 0; i < rhs; i++) {
1883 if (i + 1 < rhs) {
1884 EMIT(dup_top);
1885 }
Damiend99b0522013-12-21 18:17:45 +00001886 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001887 }
1888 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001889 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1890 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1891 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1892 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01001893 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001894 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1895 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001896 compile_node(comp, pns10->nodes[0]); // rhs
1897 compile_node(comp, pns10->nodes[1]); // rhs
1898 EMIT(rot_two);
1899 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1900 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00001901 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1902 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1903 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1904 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01001905 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001906 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1907 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001908 compile_node(comp, pns10->nodes[0]); // rhs
1909 compile_node(comp, pns10->nodes[1]); // rhs
1910 compile_node(comp, pns10->nodes[2]); // rhs
1911 EMIT(rot_three);
1912 EMIT(rot_two);
1913 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1914 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1915 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1916 } else {
1917 compile_node(comp, pns1->nodes[0]); // rhs
1918 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1919 }
1920 } else {
1921 // shouldn't happen
1922 assert(0);
1923 }
1924 }
1925}
1926
Damien Georged17926d2014-03-30 13:35:08 +01001927void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00001928 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001929 compile_node(comp, pns->nodes[0]);
1930 for (int i = 1; i < num_nodes; i += 1) {
1931 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001932 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001933 }
1934}
1935
Damiend99b0522013-12-21 18:17:45 +00001936void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1937 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1938 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001939
1940 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001941 int l_fail = comp_next_label(comp);
1942 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001943 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1944 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001945 EMIT_ARG(jump, l_end);
1946 EMIT_ARG(label_assign, l_fail);
1947 EMIT_ARG(set_stack_size, stack_size); // force stack size reset
Damien429d7192013-10-04 19:53:11 +01001948 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001949 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001950}
1951
Damiend99b0522013-12-21 18:17:45 +00001952void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001953 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00001954 //mp_parse_node_t pn_params = pns->nodes[0];
1955 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001956
1957 if (comp->pass == PASS_1) {
1958 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00001959 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 +01001960 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001961 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001962 }
1963
1964 // get the scope for this lambda
1965 scope_t *this_scope = (scope_t*)pns->nodes[2];
1966
1967 // make the lambda
1968 close_over_variables_etc(comp, this_scope, 0, 0);
1969}
1970
Damiend99b0522013-12-21 18:17:45 +00001971void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001972 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001973 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001974 for (int i = 0; i < n; i += 1) {
1975 compile_node(comp, pns->nodes[i]);
1976 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00001977 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01001978 }
1979 }
Damien Georgeb9791222014-01-23 00:34:21 +00001980 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001981}
1982
Damiend99b0522013-12-21 18:17:45 +00001983void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001984 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001985 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001986 for (int i = 0; i < n; i += 1) {
1987 compile_node(comp, pns->nodes[i]);
1988 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00001989 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01001990 }
1991 }
Damien Georgeb9791222014-01-23 00:34:21 +00001992 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001993}
1994
Damiend99b0522013-12-21 18:17:45 +00001995void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001996 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01001997 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01001998}
1999
Damiend99b0522013-12-21 18:17:45 +00002000void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002001 int stack_size = EMIT(get_stack_size);
Damiend99b0522013-12-21 18:17:45 +00002002 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002003 compile_node(comp, pns->nodes[0]);
2004 bool multi = (num_nodes > 3);
2005 int l_fail = 0;
2006 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002007 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002008 }
2009 for (int i = 1; i + 1 < num_nodes; i += 2) {
2010 compile_node(comp, pns->nodes[i + 1]);
2011 if (i + 2 < num_nodes) {
2012 EMIT(dup_top);
2013 EMIT(rot_three);
2014 }
Damien George7e5fb242014-02-01 22:18:47 +00002015 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002016 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002017 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002018 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2019 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2020 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2021 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2022 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2023 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2024 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2025 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002026 }
2027 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002028 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2029 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2030 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002031 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002032 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002033 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002034 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002035 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002036 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002037 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002038 }
2039 } else {
2040 // shouldn't happen
2041 assert(0);
2042 }
2043 } else {
2044 // shouldn't happen
2045 assert(0);
2046 }
2047 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002048 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002049 }
2050 }
2051 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002052 int l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002053 EMIT_ARG(jump, l_end);
2054 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01002055 EMIT(rot_two);
2056 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002057 EMIT_ARG(label_assign, l_end);
2058 EMIT_ARG(set_stack_size, stack_size + 1); // force stack size
Damien429d7192013-10-04 19:53:11 +01002059 }
2060}
2061
Damiend99b0522013-12-21 18:17:45 +00002062void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002063 // TODO
2064 assert(0);
2065 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002066 //EMIT_ARG(unary_op, "UNARY_STAR");
Damien429d7192013-10-04 19:53:11 +01002067}
2068
Damiend99b0522013-12-21 18:17:45 +00002069void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002070 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002071}
2072
Damiend99b0522013-12-21 18:17:45 +00002073void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002074 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002075}
2076
Damiend99b0522013-12-21 18:17:45 +00002077void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002078 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002079}
2080
Damiend99b0522013-12-21 18:17:45 +00002081void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2082 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002083 compile_node(comp, pns->nodes[0]);
2084 for (int i = 1; i + 1 < num_nodes; i += 2) {
2085 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002086 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002087 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002088 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002089 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002090 } else {
2091 // shouldn't happen
2092 assert(0);
2093 }
2094 }
2095}
2096
Damiend99b0522013-12-21 18:17:45 +00002097void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2098 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002099 compile_node(comp, pns->nodes[0]);
2100 for (int i = 1; i + 1 < num_nodes; i += 2) {
2101 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002102 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002103 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002104 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002105 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002106 } else {
2107 // shouldn't happen
2108 assert(0);
2109 }
2110 }
2111}
2112
Damiend99b0522013-12-21 18:17:45 +00002113void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2114 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002115 compile_node(comp, pns->nodes[0]);
2116 for (int i = 1; i + 1 < num_nodes; i += 2) {
2117 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002118 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002119 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002120 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002121 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002122 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002123 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002124 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002125 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002126 } else {
2127 // shouldn't happen
2128 assert(0);
2129 }
2130 }
2131}
2132
Damiend99b0522013-12-21 18:17:45 +00002133void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002134 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002135 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002136 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002137 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002138 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002139 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002140 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002141 } else {
2142 // shouldn't happen
2143 assert(0);
2144 }
2145}
2146
Damien George35e2a4e2014-02-05 00:51:47 +00002147void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2148 // this is to handle special super() call
2149 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2150
2151 compile_generic_all_nodes(comp, pns);
2152}
2153
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002154STATIC 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 +01002155 // function to call is on top of stack
2156
Damien George35e2a4e2014-02-05 00:51:47 +00002157#if !MICROPY_EMIT_CPYTHON
2158 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002159 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 +00002160 EMIT_ARG(load_id, MP_QSTR___class__);
2161 // get first argument to function
2162 bool found = false;
2163 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
2164 if (comp->scope_cur->id_info[i].param) {
2165 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
2166 found = true;
2167 break;
2168 }
2169 }
2170 if (!found) {
2171 printf("TypeError: super() call cannot find self\n");
2172 return;
2173 }
2174 EMIT_ARG(call_function, 2, 0, false, false);
2175 return;
2176 }
2177#endif
2178
Damien429d7192013-10-04 19:53:11 +01002179 int old_n_arg_keyword = comp->n_arg_keyword;
2180 bool old_have_star_arg = comp->have_star_arg;
2181 bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
2182 comp->n_arg_keyword = 0;
2183 comp->have_star_arg = false;
2184 comp->have_dbl_star_arg = false;
2185
Damien Georgebbcd49a2014-02-06 20:30:16 +00002186 compile_node(comp, pn_arglist); // arguments to function call; can be null
Damien429d7192013-10-04 19:53:11 +01002187
2188 // compute number of positional arguments
Damien Georgebbcd49a2014-02-06 20:30:16 +00002189 int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
Damien429d7192013-10-04 19:53:11 +01002190 if (comp->have_star_arg) {
2191 n_positional -= 1;
2192 }
2193 if (comp->have_dbl_star_arg) {
2194 n_positional -= 1;
2195 }
2196
2197 if (is_method_call) {
Damien Georgeb9791222014-01-23 00:34:21 +00002198 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 +01002199 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002200 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 +01002201 }
2202
2203 comp->n_arg_keyword = old_n_arg_keyword;
2204 comp->have_star_arg = old_have_star_arg;
2205 comp->have_dbl_star_arg = old_have_dbl_star_arg;
2206}
2207
Damiend99b0522013-12-21 18:17:45 +00002208void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2209 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002210 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002211 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 +01002212 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002213 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2214 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002215 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002216 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002217 i += 1;
2218 } else {
2219 compile_node(comp, pns->nodes[i]);
2220 }
Damien George35e2a4e2014-02-05 00:51:47 +00002221 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002222 }
2223}
2224
Damiend99b0522013-12-21 18:17:45 +00002225void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002226 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002227 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002228}
2229
Damiend99b0522013-12-21 18:17:45 +00002230void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002231 // a list of strings
Damien63321742013-12-10 17:41:49 +00002232
2233 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002234 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002235 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002236 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002237 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002238 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2239 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2240 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002241 if (i == 0) {
2242 string_kind = pn_kind;
2243 } else if (pn_kind != string_kind) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002244 compile_syntax_error(comp, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002245 return;
2246 }
Damien George55baff42014-01-21 21:40:13 +00002247 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002248 }
Damien63321742013-12-10 17:41:49 +00002249
Damien63321742013-12-10 17:41:49 +00002250 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002251 byte *q_ptr;
2252 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002253 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002254 uint s_len;
2255 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002256 memcpy(s_dest, s, s_len);
2257 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002258 }
Damien George55baff42014-01-21 21:40:13 +00002259 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002260
Damien Georgeb9791222014-01-23 00:34:21 +00002261 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002262}
2263
2264// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002265void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2266 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2267 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2268 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002269
2270 if (comp->pass == PASS_1) {
2271 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002272 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 +01002273 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002274 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002275 }
2276
2277 // get the scope for this comprehension
2278 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2279
2280 // compile the comprehension
2281 close_over_variables_etc(comp, this_scope, 0, 0);
2282
2283 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2284 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002285 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01002286}
2287
Damiend99b0522013-12-21 18:17:45 +00002288void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2289 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002290 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002291 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2292 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2293 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2294 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2295 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2296 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2297 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002298 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002299 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002300 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002301 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002302 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002303 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002304 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002305 // generator expression
2306 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2307 } else {
2308 // tuple with 2 items
2309 goto tuple_with_2_items;
2310 }
2311 } else {
2312 // tuple with 2 items
2313 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002314 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002315 }
2316 } else {
2317 // parenthesis around a single item, is just that item
2318 compile_node(comp, pns->nodes[0]);
2319 }
2320}
2321
Damiend99b0522013-12-21 18:17:45 +00002322void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2323 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002324 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002325 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002326 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2327 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2328 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2329 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2330 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002331 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002332 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002333 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002334 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002335 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002336 // list of many items
2337 compile_node(comp, pns2->nodes[0]);
2338 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002339 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002340 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002341 // list comprehension
2342 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2343 } else {
2344 // list with 2 items
2345 goto list_with_2_items;
2346 }
2347 } else {
2348 // list with 2 items
2349 list_with_2_items:
2350 compile_node(comp, pns2->nodes[0]);
2351 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002352 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002353 }
2354 } else {
2355 // list with 1 item
2356 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002357 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002358 }
2359}
2360
Damiend99b0522013-12-21 18:17:45 +00002361void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2362 mp_parse_node_t pn = pns->nodes[0];
2363 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002364 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002365 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002366 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2367 pns = (mp_parse_node_struct_t*)pn;
2368 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002369 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002370 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002371 compile_node(comp, pn);
2372 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002373 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2374 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2375 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2376 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002377 // dict/set with multiple elements
2378
2379 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002380 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002381 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2382
2383 // first element sets whether it's a dict or set
2384 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002385 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002386 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002387 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002388 compile_node(comp, pns->nodes[0]);
2389 EMIT(store_map);
2390 is_dict = true;
2391 } else {
2392 // a set
2393 compile_node(comp, pns->nodes[0]); // 1st value of set
2394 is_dict = false;
2395 }
2396
2397 // process rest of elements
2398 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002399 mp_parse_node_t pn = nodes[i];
2400 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002401 compile_node(comp, pn);
2402 if (is_dict) {
2403 if (!is_key_value) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002404 compile_syntax_error(comp, "?expecting key:value for dictiona");
Damien429d7192013-10-04 19:53:11 +01002405 return;
2406 }
2407 EMIT(store_map);
2408 } else {
2409 if (is_key_value) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002410 compile_syntax_error(comp, "?expecting just a value for s");
Damien429d7192013-10-04 19:53:11 +01002411 return;
2412 }
2413 }
2414 }
2415
2416 // if it's a set, build it
2417 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002418 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002419 }
Damiend99b0522013-12-21 18:17:45 +00002420 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002421 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002422 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002423 // a dictionary comprehension
2424 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2425 } else {
2426 // a set comprehension
2427 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2428 }
2429 } else {
2430 // shouldn't happen
2431 assert(0);
2432 }
2433 } else {
2434 // set with one element
2435 goto set_with_one_element;
2436 }
2437 } else {
2438 // set with one element
2439 set_with_one_element:
2440 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002441 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002442 }
2443}
2444
Damiend99b0522013-12-21 18:17:45 +00002445void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002446 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002447}
2448
Damiend99b0522013-12-21 18:17:45 +00002449void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002450 // object who's index we want is on top of stack
2451 compile_node(comp, pns->nodes[0]); // the index
Damien Georged17926d2014-03-30 13:35:08 +01002452 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002453}
2454
Damiend99b0522013-12-21 18:17:45 +00002455void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002456 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002457 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002458}
2459
Damiend99b0522013-12-21 18:17:45 +00002460void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2461 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2462 mp_parse_node_t pn = pns->nodes[0];
2463 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002464 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002465 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2466 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002467 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2468 pns = (mp_parse_node_struct_t*)pn;
2469 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002470 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002471 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002472 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002473 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002474 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002475 } else {
2476 // [?::x]
2477 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002478 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002479 }
Damiend99b0522013-12-21 18:17:45 +00002480 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002481 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002482 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2483 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2484 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2485 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002486 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002487 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002488 } else {
2489 // [?:x:x]
2490 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002491 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002492 }
2493 } else {
2494 // [?:x]
2495 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002496 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002497 }
2498 } else {
2499 // [?:x]
2500 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002501 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002502 }
2503}
2504
Damiend99b0522013-12-21 18:17:45 +00002505void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002506 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002507 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2508 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002509}
2510
Damiend99b0522013-12-21 18:17:45 +00002511void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002512 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002513 compile_subscript_3_helper(comp, pns);
2514}
2515
Damiend99b0522013-12-21 18:17:45 +00002516void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002517 // if this is called then we are compiling a dict key:value pair
2518 compile_node(comp, pns->nodes[1]); // value
2519 compile_node(comp, pns->nodes[0]); // key
2520}
2521
Damiend99b0522013-12-21 18:17:45 +00002522void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002523 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002524 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002525 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002526}
2527
Damiend99b0522013-12-21 18:17:45 +00002528void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002529 if (comp->have_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_star_arg = true;
2534 compile_node(comp, pns->nodes[0]);
2535}
2536
Damiend99b0522013-12-21 18:17:45 +00002537void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002538 if (comp->have_dbl_star_arg) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002539 compile_syntax_error(comp, "?can't have multiple **x");
Damien429d7192013-10-04 19:53:11 +01002540 return;
2541 }
2542 comp->have_dbl_star_arg = true;
2543 compile_node(comp, pns->nodes[0]);
2544}
2545
Damiend99b0522013-12-21 18:17:45 +00002546void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2547 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2548 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2549 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2550 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002551 compile_syntax_error(comp, "?lhs of keyword argument must be an id");
Damien429d7192013-10-04 19:53:11 +01002552 return;
2553 }
Damien Georgeb9791222014-01-23 00:34:21 +00002554 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002555 compile_node(comp, pns2->nodes[0]);
2556 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002557 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002558 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2559 } else {
2560 // shouldn't happen
2561 assert(0);
2562 }
2563}
2564
Damiend99b0522013-12-21 18:17:45 +00002565void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002566 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002567 compile_syntax_error(comp, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002568 return;
2569 }
Damiend99b0522013-12-21 18:17:45 +00002570 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002571 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002572 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002573 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2574 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002575 compile_node(comp, pns->nodes[0]);
2576 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002577 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002578 EMIT(yield_from);
2579 } else {
2580 compile_node(comp, pns->nodes[0]);
2581 EMIT(yield_value);
2582 }
2583}
2584
Damiend99b0522013-12-21 18:17:45 +00002585typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002586STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002587 NULL,
2588#define nc NULL
2589#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002590#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002591#include "grammar.h"
2592#undef nc
2593#undef c
2594#undef DEF_RULE
2595};
2596
Damiend99b0522013-12-21 18:17:45 +00002597void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2598 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002599 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002600 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2601 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2602 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002603 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002604 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002605 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002606 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002607 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2608 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2609 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2610 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002611 case MP_PARSE_NODE_TOKEN:
2612 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002613 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002614 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002615 // do nothing
2616 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002617 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002618 }
2619 break;
Damien429d7192013-10-04 19:53:11 +01002620 default: assert(0);
2621 }
2622 } else {
Damiend99b0522013-12-21 18:17:45 +00002623 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002624 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002625 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002626 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002627 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002628#if MICROPY_DEBUG_PRINTERS
2629 mp_parse_node_print(pn, 0);
2630#endif
Damien429d7192013-10-04 19:53:11 +01002631 assert(0);
2632 } else {
2633 f(comp, pns);
2634 }
2635 }
2636}
2637
Damiend99b0522013-12-21 18:17:45 +00002638void 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 +01002639 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002640 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002641 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2642 if (MP_PARSE_NODE_IS_ID(pn)) {
2643 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002644 if (comp->have_bare_star) {
2645 // comes after a bare star, so doesn't count as a parameter
2646 } else {
2647 comp->scope_cur->num_params += 1;
2648 }
Damienb14de212013-10-06 00:28:28 +01002649 } else {
Damiend99b0522013-12-21 18:17:45 +00002650 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2651 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2652 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2653 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002654 //int node_index = 1; unused
2655 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002656 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002657 // this parameter has an annotation
2658 pn_annotation = pns->nodes[1];
2659 }
2660 //node_index = 2; unused
2661 }
2662 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002663 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002664 // this parameter has a default value
2665 if (comp->have_bare_star) {
2666 comp->scope_cur->num_dict_params += 1;
2667 } else {
2668 comp->scope_cur->num_default_params += 1;
2669 }
2670 }
2671 */
2672 if (comp->have_bare_star) {
2673 // comes after a bare star, so doesn't count as a parameter
2674 } else {
2675 comp->scope_cur->num_params += 1;
2676 }
Damiend99b0522013-12-21 18:17:45 +00002677 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2678 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002679 // bare star
2680 // TODO see http://www.python.org/dev/peps/pep-3102/
2681 comp->have_bare_star = true;
2682 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002683 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002684 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002685 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002686 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2687 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002688 // named star with annotation
Damien George8725f8f2014-02-15 19:33:11 +00002689 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002690 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2691 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002692 pn_annotation = pns->nodes[1];
2693 } else {
2694 // shouldn't happen
2695 assert(0);
2696 }
Damiend99b0522013-12-21 18:17:45 +00002697 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2698 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2699 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002700 // this parameter has an annotation
2701 pn_annotation = pns->nodes[1];
2702 }
Damien George8725f8f2014-02-15 19:33:11 +00002703 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002704 } else {
Damienb14de212013-10-06 00:28:28 +01002705 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002706 assert(0);
2707 }
Damien429d7192013-10-04 19:53:11 +01002708 }
2709
2710 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002711 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002712 // TODO this parameter has an annotation
2713 }
2714 bool added;
2715 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2716 if (!added) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002717 compile_syntax_error(comp, "?same name used for parameter");
Damien429d7192013-10-04 19:53:11 +01002718 return;
2719 }
2720 id_info->param = true;
2721 id_info->kind = ID_INFO_KIND_LOCAL;
2722 }
2723}
2724
Damiend99b0522013-12-21 18:17:45 +00002725void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002726 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2727}
2728
Damiend99b0522013-12-21 18:17:45 +00002729void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002730 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2731}
2732
Damiend99b0522013-12-21 18:17:45 +00002733void 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 +01002734 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002735 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002736 // no more nested if/for; compile inner expression
2737 compile_node(comp, pn_inner_expr);
2738 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002739 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002740 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002741 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002742 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002743 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002744 } else {
2745 EMIT(yield_value);
2746 EMIT(pop_top);
2747 }
Damiend99b0522013-12-21 18:17:45 +00002748 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002749 // if condition
Damiend99b0522013-12-21 18:17:45 +00002750 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002751 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2752 pn_iter = pns_comp_if->nodes[1];
2753 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002754 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002755 // for loop
Damiend99b0522013-12-21 18:17:45 +00002756 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002757 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002758 int l_end2 = comp_next_label(comp);
2759 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002760 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002761 EMIT_ARG(label_assign, l_top2);
2762 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002763 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2764 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 +00002765 EMIT_ARG(jump, l_top2);
2766 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002767 EMIT(for_iter_end);
2768 } else {
2769 // shouldn't happen
2770 assert(0);
2771 }
2772}
2773
Damiend99b0522013-12-21 18:17:45 +00002774void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002775 // see http://www.python.org/dev/peps/pep-0257/
2776
2777 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002778 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002779 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002780 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002781 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002782 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2783 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002784 for (int i = 0; i < num_nodes; i++) {
2785 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002786 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 +00002787 // not a newline, so this is the first statement; finish search
2788 break;
2789 }
2790 }
2791 // 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 +00002792 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002793 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002794 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002795 } else {
2796 return;
2797 }
2798
2799 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002800 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2801 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2802 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2803 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2804 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002805 compile_node(comp, pns->nodes[0]); // a doc string
2806 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002807 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002808 }
2809 }
2810 }
2811}
2812
2813void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2814 comp->pass = pass;
2815 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002816 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002817 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002818
2819 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002820 // reset maximum stack sizes in scope
2821 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002822 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002823 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002824 }
2825
Damien5ac1b2e2013-10-18 19:58:12 +01002826#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002827 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002828 scope_print_info(scope);
2829 }
Damien5ac1b2e2013-10-18 19:58:12 +01002830#endif
Damien429d7192013-10-04 19:53:11 +01002831
2832 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002833 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2834 assert(scope->kind == SCOPE_MODULE);
2835 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2836 compile_node(comp, pns->nodes[0]); // compile the expression
2837 EMIT(return_value);
2838 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002839 if (!comp->is_repl) {
2840 check_for_doc_string(comp, scope->pn);
2841 }
Damien429d7192013-10-04 19:53:11 +01002842 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002843 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002844 EMIT(return_value);
2845 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002846 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2847 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2848 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002849
2850 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002851 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002852 if (comp->pass == PASS_1) {
2853 comp->have_bare_star = false;
2854 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2855 }
2856
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02002857 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01002858
2859 compile_node(comp, pns->nodes[3]); // 3 is function body
2860 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002861 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002862 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002863 EMIT(return_value);
2864 }
2865 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002866 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2867 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2868 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002869
2870 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002871 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002872 if (comp->pass == PASS_1) {
2873 comp->have_bare_star = false;
2874 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2875 }
2876
2877 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2878 EMIT(return_value);
2879 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2880 // a bit of a hack at the moment
2881
Damiend99b0522013-12-21 18:17:45 +00002882 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2883 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2884 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2885 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2886 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002887
Damien George55baff42014-01-21 21:40:13 +00002888 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01002889 if (comp->pass == PASS_1) {
2890 bool added;
2891 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2892 assert(added);
2893 id_info->kind = ID_INFO_KIND_LOCAL;
2894 scope->num_params = 1;
2895 }
2896
2897 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002898 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01002899 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002900 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01002901 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002902 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01002903 }
2904
Damienb05d7072013-10-05 13:37:10 +01002905 int l_end = comp_next_label(comp);
2906 int l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002907 EMIT_ARG(load_id, qstr_arg);
2908 EMIT_ARG(label_assign, l_top);
2909 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01002910 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2911 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00002912 EMIT_ARG(jump, l_top);
2913 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002914 EMIT(for_iter_end);
2915
2916 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00002917 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002918 }
2919 EMIT(return_value);
2920 } else {
2921 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002922 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2923 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2924 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01002925
2926 if (comp->pass == PASS_1) {
2927 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002928 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01002929 assert(added);
2930 id_info->kind = ID_INFO_KIND_LOCAL;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002931 id_info = scope_find_or_add_id(scope, MP_QSTR___locals__, &added);
Damien429d7192013-10-04 19:53:11 +01002932 assert(added);
2933 id_info->kind = ID_INFO_KIND_LOCAL;
2934 id_info->param = true;
2935 scope->num_params = 1; // __locals__ is the parameter
2936 }
2937
Damien Georgeb9791222014-01-23 00:34:21 +00002938 EMIT_ARG(load_id, MP_QSTR___locals__);
Damien429d7192013-10-04 19:53:11 +01002939 EMIT(store_locals);
Damien Georgeb9791222014-01-23 00:34:21 +00002940 EMIT_ARG(load_id, MP_QSTR___name__);
2941 EMIT_ARG(store_id, MP_QSTR___module__);
2942 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
2943 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01002944
2945 check_for_doc_string(comp, pns->nodes[2]);
2946 compile_node(comp, pns->nodes[2]); // 2 is class body
2947
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002948 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01002949 assert(id != NULL);
2950 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00002951 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002952 } else {
Damien George6baf76e2013-12-30 22:32:17 +00002953#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00002954 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002955#else
Damien George35e2a4e2014-02-05 00:51:47 +00002956 EMIT_ARG(load_fast, MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00002957#endif
Damien429d7192013-10-04 19:53:11 +01002958 }
2959 EMIT(return_value);
2960 }
2961
Damien415eb6f2013-10-05 12:19:06 +01002962 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00002963
2964 // make sure we match all the exception levels
2965 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01002966}
2967
2968void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2969 comp->pass = pass;
2970 comp->scope_cur = scope;
2971 comp->next_label = 1;
2972
2973 if (scope->kind != SCOPE_FUNCTION) {
2974 printf("Error: inline assembler must be a function\n");
2975 return;
2976 }
2977
Damiena2f2f7d2013-10-06 00:14:13 +01002978 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00002979 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01002980 }
2981
Damien826005c2013-10-05 23:17:28 +01002982 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00002983 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2984 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2985 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01002986
Damiend99b0522013-12-21 18:17:45 +00002987 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01002988
Damiena2f2f7d2013-10-06 00:14:13 +01002989 // parameters are in pns->nodes[1]
2990 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00002991 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01002992 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00002993 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01002994 }
2995
Damiend99b0522013-12-21 18:17:45 +00002996 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01002997
Damiend99b0522013-12-21 18:17:45 +00002998 mp_parse_node_t pn_body = pns->nodes[3]; // body
2999 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003000 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3001
Damien Georgecbd2f742014-01-19 11:48:48 +00003002 /*
Damien826005c2013-10-05 23:17:28 +01003003 if (comp->pass == PASS_3) {
3004 //printf("----\n");
3005 scope_print_info(scope);
3006 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003007 */
Damien826005c2013-10-05 23:17:28 +01003008
3009 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003010 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3011 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
3012 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
3013 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3014 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3015 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3016 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3017 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3018 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3019 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3020 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3021 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3022 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003023 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3024
3025 // emit instructions
3026 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00003027 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgef41fdd02014-03-03 23:19:11 +00003028 compile_syntax_error(comp, "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003029 return;
3030 }
3031 int lab = comp_next_label(comp);
3032 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003033 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003034 }
3035 } else {
3036 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003037 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003038 }
3039 }
3040 }
3041
3042 if (comp->pass > PASS_1) {
3043 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01003044 }
Damien429d7192013-10-04 19:53:11 +01003045}
3046
3047void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3048 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003049 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003050 scope->num_locals = 0;
3051 for (int i = 0; i < scope->id_info_len; i++) {
3052 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003053 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003054 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3055 continue;
3056 }
3057 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3058 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3059 }
Damien9ecbcff2013-12-11 00:41:43 +00003060 // note: params always count for 1 local, even if they are a cell
Damien429d7192013-10-04 19:53:11 +01003061 if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
3062 id->local_num = scope->num_locals;
3063 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003064 }
3065 }
3066
3067 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003068#if MICROPY_EMIT_CPYTHON
3069 int num_cell = 0;
3070#endif
Damien9ecbcff2013-12-11 00:41:43 +00003071 for (int i = 0; i < scope->id_info_len; i++) {
3072 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003073#if MICROPY_EMIT_CPYTHON
3074 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003075 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003076 id->local_num = num_cell;
3077 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003078 }
Damien George6baf76e2013-12-30 22:32:17 +00003079#else
3080 // in Micro Python the cells come right after the fast locals
3081 // parameters are not counted here, since they remain at the start
3082 // of the locals, even if they are cell vars
3083 if (!id->param && id->kind == ID_INFO_KIND_CELL) {
3084 id->local_num = scope->num_locals;
3085 scope->num_locals += 1;
3086 }
3087#endif
Damien9ecbcff2013-12-11 00:41:43 +00003088 }
Damien9ecbcff2013-12-11 00:41:43 +00003089
3090 // compute the index of free vars (freevars[idx] in CPython)
3091 // make sure they are in the order of the parent scope
3092 if (scope->parent != NULL) {
3093 int num_free = 0;
3094 for (int i = 0; i < scope->parent->id_info_len; i++) {
3095 id_info_t *id = &scope->parent->id_info[i];
3096 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3097 for (int j = 0; j < scope->id_info_len; j++) {
3098 id_info_t *id2 = &scope->id_info[j];
3099 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +00003100 assert(!id2->param); // free vars should not be params
3101#if MICROPY_EMIT_CPYTHON
3102 // in CPython the frees are numbered after the cells
3103 id2->local_num = num_cell + num_free;
3104#else
3105 // in Micro Python the frees come first, before the params
3106 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003107#endif
3108 num_free += 1;
3109 }
3110 }
3111 }
Damien429d7192013-10-04 19:53:11 +01003112 }
Damien George6baf76e2013-12-30 22:32:17 +00003113#if !MICROPY_EMIT_CPYTHON
3114 // in Micro Python shift all other locals after the free locals
3115 if (num_free > 0) {
3116 for (int i = 0; i < scope->id_info_len; i++) {
3117 id_info_t *id = &scope->id_info[i];
3118 if (id->param || id->kind != ID_INFO_KIND_FREE) {
3119 id->local_num += num_free;
3120 }
3121 }
3122 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3123 scope->num_locals += num_free;
3124 }
3125#endif
Damien429d7192013-10-04 19:53:11 +01003126 }
3127
Damien George8725f8f2014-02-15 19:33:11 +00003128 // compute scope_flags
3129 //scope->scope_flags = 0; since we set some things in parameters
Damien429d7192013-10-04 19:53:11 +01003130 if (scope->kind != SCOPE_MODULE) {
Damien George8725f8f2014-02-15 19:33:11 +00003131 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien429d7192013-10-04 19:53:11 +01003132 }
3133 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) {
3134 assert(scope->parent != NULL);
Damien George8725f8f2014-02-15 19:33:11 +00003135 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien429d7192013-10-04 19:53:11 +01003136
3137 // TODO possibly other ways it can be nested
Damien George08d07552014-01-29 18:58:52 +00003138 // Note that we don't actually use this information at the moment (for CPython compat only)
3139 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 +00003140 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003141 }
3142 }
3143 int num_free = 0;
3144 for (int i = 0; i < scope->id_info_len; i++) {
3145 id_info_t *id = &scope->id_info[i];
3146 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3147 num_free += 1;
3148 }
3149 }
3150 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003151 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003152 }
3153}
3154
Damien George08335002014-01-18 23:24:36 +00003155mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
Damien429d7192013-10-04 19:53:11 +01003156 compiler_t *comp = m_new(compiler_t, 1);
3157
Damien Georgecbd2f742014-01-19 11:48:48 +00003158 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003159 comp->is_repl = is_repl;
3160 comp->had_error = false;
3161
Damien429d7192013-10-04 19:53:11 +01003162 comp->break_label = 0;
3163 comp->continue_label = 0;
Damien Georgecbddb272014-02-01 20:08:18 +00003164 comp->break_continue_except_level = 0;
3165 comp->cur_except_level = 0;
3166
Damien George35e2a4e2014-02-05 00:51:47 +00003167 comp->func_arg_is_super = false;
3168
Damien429d7192013-10-04 19:53:11 +01003169 comp->scope_head = NULL;
3170 comp->scope_cur = NULL;
3171
Damien826005c2013-10-05 23:17:28 +01003172 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003173 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003174
3175 // set the outer scope
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003176 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE);
Damien429d7192013-10-04 19:53:11 +01003177
Damien826005c2013-10-05 23:17:28 +01003178 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003179 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003180 comp->emit_method_table = &emit_pass1_method_table;
3181 comp->emit_inline_asm = NULL;
3182 comp->emit_inline_asm_method_table = NULL;
3183 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003184 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003185 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003186#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003187 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003188 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003189#endif
Damien826005c2013-10-05 23:17:28 +01003190 } else {
3191 compile_scope(comp, s, PASS_1);
3192 }
3193
3194 // update maximim number of labels needed
3195 if (comp->next_label > max_num_labels) {
3196 max_num_labels = comp->next_label;
3197 }
Damien429d7192013-10-04 19:53:11 +01003198 }
3199
Damien826005c2013-10-05 23:17:28 +01003200 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003201 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003202 compile_scope_compute_things(comp, s);
3203 }
3204
Damien826005c2013-10-05 23:17:28 +01003205 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003206 emit_pass1_free(comp->emit);
3207
Damien826005c2013-10-05 23:17:28 +01003208 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003209#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003210 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003211#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003212 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003213#endif
Damien3ef4abb2013-10-12 16:53:13 +01003214#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003215 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003216#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003217#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003218 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003219 if (false) {
3220 // dummy
3221
Damien3ef4abb2013-10-12 16:53:13 +01003222#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003223 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
3224 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003225 if (emit_inline_thumb == NULL) {
3226 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3227 }
3228 comp->emit = NULL;
3229 comp->emit_method_table = NULL;
3230 comp->emit_inline_asm = emit_inline_thumb;
3231 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3232 compile_scope_inline_asm(comp, s, PASS_2);
3233 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003234#endif
3235
Damien826005c2013-10-05 23:17:28 +01003236 } else {
Damienc025ebb2013-10-12 14:30:21 +01003237
3238 // choose the emit type
3239
Damien3ef4abb2013-10-12 16:53:13 +01003240#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003241 comp->emit = emit_cpython_new(max_num_labels);
3242 comp->emit_method_table = &emit_cpython_method_table;
3243#else
Damien826005c2013-10-05 23:17:28 +01003244 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003245
3246#if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003247 case EMIT_OPT_NATIVE_PYTHON:
Damien3410be82013-10-07 23:09:10 +01003248 case EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003249#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003250 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003251 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003252 }
Damien13ed3a62013-10-08 09:05:10 +01003253 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003254#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003255 if (emit_native == NULL) {
3256 emit_native = emit_native_thumb_new(max_num_labels);
3257 }
3258 comp->emit_method_table = &emit_native_thumb_method_table;
3259#endif
3260 comp->emit = emit_native;
Damien3410be82013-10-07 23:09:10 +01003261 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003262 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003263#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003264
Damien826005c2013-10-05 23:17:28 +01003265 default:
3266 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003267 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003268 }
3269 comp->emit = emit_bc;
3270 comp->emit_method_table = &emit_bc_method_table;
3271 break;
3272 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003273#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003274
3275 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003276 compile_scope(comp, s, PASS_2);
3277 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003278 }
Damien429d7192013-10-04 19:53:11 +01003279 }
3280
Damien George41d02b62014-01-24 22:42:28 +00003281 // free the emitters
3282#if !MICROPY_EMIT_CPYTHON
3283 if (emit_bc != NULL) {
3284 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003285 }
Damien George41d02b62014-01-24 22:42:28 +00003286#if MICROPY_EMIT_NATIVE
3287 if (emit_native != NULL) {
3288#if MICROPY_EMIT_X64
3289 emit_native_x64_free(emit_native);
3290#elif MICROPY_EMIT_THUMB
3291 emit_native_thumb_free(emit_native);
3292#endif
3293 }
3294#endif
3295#if MICROPY_EMIT_INLINE_THUMB
3296 if (emit_inline_thumb != NULL) {
3297 emit_inline_thumb_free(emit_inline_thumb);
3298 }
3299#endif
3300#endif // !MICROPY_EMIT_CPYTHON
3301
3302 // free the scopes
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003303 uint unique_code_id = module_scope->unique_code_id;
3304 for (scope_t *s = module_scope; s;) {
3305 scope_t *next = s->next;
3306 scope_free(s);
3307 s = next;
3308 }
Damien5ac1b2e2013-10-18 19:58:12 +01003309
Damien George41d02b62014-01-24 22:42:28 +00003310 // free the compiler
3311 bool had_error = comp->had_error;
3312 m_del_obj(compiler_t, comp);
3313
Damien George1fb03172014-01-03 14:22:03 +00003314 if (had_error) {
3315 // TODO return a proper error message
3316 return mp_const_none;
3317 } else {
3318#if MICROPY_EMIT_CPYTHON
3319 // can't create code, so just return true
Damien George41d02b62014-01-24 22:42:28 +00003320 (void)unique_code_id; // to suppress warning that unique_code_id is unused
Damien George1fb03172014-01-03 14:22:03 +00003321 return mp_const_true;
3322#else
3323 // return function that executes the outer module
Damien Georged1e443d2014-03-29 11:39:36 +00003324 // we can free the unique_code slot because no-one has reference to this unique_code_id anymore
Damien Georged17926d2014-03-30 13:35:08 +01003325 return mp_make_function_from_id(unique_code_id, true, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003326#endif
3327 }
Damien429d7192013-10-04 19:53:11 +01003328}