blob: f18c2284505315d04c1a2a0455325cd20a2317ee [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 George1fb03172014-01-03 14:22:03 +000016#include "obj.h"
17#include "compile.h"
18#include "runtime.h"
Rachel Dowdallcde86312014-03-22 17:29:27 +000019#include "intdivmod.h"
Damien429d7192013-10-04 19:53:11 +010020
21// TODO need to mangle __attr names
22
Damience89a212013-10-15 22:25:17 +010023#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_THUMB)
24
Damien429d7192013-10-04 19:53:11 +010025typedef enum {
26 PN_none = 0,
Damien George00208ce2014-01-23 00:00:53 +000027#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien429d7192013-10-04 19:53:11 +010028#include "grammar.h"
29#undef DEF_RULE
30 PN_maximum_number_of,
31} pn_kind_t;
32
Damien Georgeb9791222014-01-23 00:34:21 +000033#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
34#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
35#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
36#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 +010037
Damien6cdd3af2013-10-05 18:08:26 +010038#define EMIT_OPT_NONE (0)
39#define EMIT_OPT_BYTE_CODE (1)
40#define EMIT_OPT_NATIVE_PYTHON (2)
Damien7af3d192013-10-07 00:02:49 +010041#define EMIT_OPT_VIPER (3)
42#define EMIT_OPT_ASM_THUMB (4)
Damien6cdd3af2013-10-05 18:08:26 +010043
Damien429d7192013-10-04 19:53:11 +010044typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000045 qstr source_file;
Damien5ac1b2e2013-10-18 19:58:12 +010046 bool is_repl;
Damien429d7192013-10-04 19:53:11 +010047 pass_kind_t pass;
Damien5ac1b2e2013-10-18 19:58:12 +010048 bool had_error; // try to keep compiler clean from nlr
Damien429d7192013-10-04 19:53:11 +010049
Damienb05d7072013-10-05 13:37:10 +010050 int next_label;
Damienb05d7072013-10-05 13:37:10 +010051
Damien429d7192013-10-04 19:53:11 +010052 int break_label;
53 int continue_label;
Damien Georgecbddb272014-02-01 20:08:18 +000054 int break_continue_except_level;
55 int cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien429d7192013-10-04 19:53:11 +010056
57 int n_arg_keyword;
58 bool have_star_arg;
59 bool have_dbl_star_arg;
60 bool have_bare_star;
61 int param_pass;
62 int param_pass_num_dict_params;
63 int param_pass_num_default_params;
64
Damien George35e2a4e2014-02-05 00:51:47 +000065 bool func_arg_is_super; // used to compile special case of super() function call
66
Damien429d7192013-10-04 19:53:11 +010067 scope_t *scope_head;
68 scope_t *scope_cur;
69
Damien6cdd3af2013-10-05 18:08:26 +010070 emit_t *emit; // current emitter
71 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010072
73 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
74 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 +010075} compiler_t;
76
Damien Georgef41fdd02014-03-03 23:19:11 +000077STATIC void compile_syntax_error(compiler_t *comp, const char *msg) {
78 // TODO store the error message to a variable in compiler_t instead of printing it
79 printf("SyntaxError: %s\n", msg);
80 comp->had_error = true;
81}
82
Damiend99b0522013-12-21 18:17:45 +000083mp_parse_node_t fold_constants(mp_parse_node_t pn) {
84 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
85 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
86 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +010087
88 // fold arguments first
89 for (int i = 0; i < n; i++) {
90 pns->nodes[i] = fold_constants(pns->nodes[i]);
91 }
92
Damiend99b0522013-12-21 18:17:45 +000093 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +010094 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +000095 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 +020096 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
97 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +000098 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien3ef4abb2013-10-12 16:53:13 +010099#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100100 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000101 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
Damien0efb3a12013-10-12 16:16:56 +0100102#endif
Damiend99b0522013-12-21 18:17:45 +0000103 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
104 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100105 } else {
106 // shouldn't happen
107 assert(0);
108 }
109 }
110 break;
111
112 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100113 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000114 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 +0200115 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
116 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damien0efb3a12013-10-12 16:16:56 +0100117 machine_int_t res;
Damiend99b0522013-12-21 18:17:45 +0000118 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100119 res = arg0 + arg1;
Damiend99b0522013-12-21 18:17:45 +0000120 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100121 res = arg0 - arg1;
Damien429d7192013-10-04 19:53:11 +0100122 } else {
123 // shouldn't happen
124 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100125 res = 0;
126 }
Paul Sokolovskybbf0e2f2014-02-21 02:04:32 +0200127 if (MP_PARSE_FITS_SMALL_INT(res)) {
Damiend99b0522013-12-21 18:17:45 +0000128 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, res);
Damien429d7192013-10-04 19:53:11 +0100129 }
130 }
131 break;
132
133 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000134 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 +0200135 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
136 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000137 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien3ef4abb2013-10-12 16:53:13 +0100138#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100139 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000140 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 * arg1);
Damien0efb3a12013-10-12 16:16:56 +0100141#endif
Damiend99b0522013-12-21 18:17:45 +0000142 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien429d7192013-10-04 19:53:11 +0100143 ; // pass
Damiend99b0522013-12-21 18:17:45 +0000144 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Rachel Dowdallcde86312014-03-22 17:29:27 +0000145 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, python_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000146 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Rachel Dowdall56402792014-03-22 20:19:24 +0000147 //pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT,
148 // floor((mp_float_t)arg0 / arg1));
149 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT,
150 python_floor_divide(arg0, arg1));
151
Damien429d7192013-10-04 19:53:11 +0100152 } else {
153 // shouldn't happen
154 assert(0);
155 }
156 }
157 break;
158
159 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000160 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200161 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000162 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
163 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
164 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
165 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
166 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
167 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100168 } else {
169 // shouldn't happen
170 assert(0);
171 }
172 }
173 break;
174
Damien3ef4abb2013-10-12 16:53:13 +0100175#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +0100176 case PN_power:
Damien0efb3a12013-10-12 16:16:56 +0100177 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000178 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])) {
179 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
180 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200181 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100182 if (power >= 0) {
183 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200184 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100185 for (; power > 0; power--) {
186 ans *= base;
187 }
Damiend99b0522013-12-21 18:17:45 +0000188 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100189 }
190 }
191 }
192 break;
Damien0efb3a12013-10-12 16:16:56 +0100193#endif
Damien429d7192013-10-04 19:53:11 +0100194 }
195 }
196
197 return pn;
198}
199
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200200STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra);
Damiend99b0522013-12-21 18:17:45 +0000201void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100202
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200203STATIC int comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100204 return comp->next_label++;
205}
206
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200207STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) {
Damien Georgecbd2f742014-01-19 11:48:48 +0000208 scope_t *scope = scope_new(kind, pn, comp->source_file, rt_get_unique_code_id(), emit_options);
Damien429d7192013-10-04 19:53:11 +0100209 scope->parent = comp->scope_cur;
210 scope->next = NULL;
211 if (comp->scope_head == NULL) {
212 comp->scope_head = scope;
213 } else {
214 scope_t *s = comp->scope_head;
215 while (s->next != NULL) {
216 s = s->next;
217 }
218 s->next = scope;
219 }
220 return scope;
221}
222
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200223STATIC int list_len(mp_parse_node_t pn, int pn_kind) {
Damiend99b0522013-12-21 18:17:45 +0000224 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100225 return 0;
Damiend99b0522013-12-21 18:17:45 +0000226 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100227 return 1;
228 } else {
Damiend99b0522013-12-21 18:17:45 +0000229 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
230 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100231 return 1;
232 } else {
Damiend99b0522013-12-21 18:17:45 +0000233 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100234 }
235 }
236}
237
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200238STATIC 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 +0000239 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
240 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
241 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100242 for (int i = 0; i < num_nodes; i++) {
243 f(comp, pns->nodes[i]);
244 }
Damiend99b0522013-12-21 18:17:45 +0000245 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100246 f(comp, pn);
247 }
248}
249
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200250STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000251 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100252 *nodes = NULL;
253 return 0;
Damiend99b0522013-12-21 18:17:45 +0000254 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100255 *nodes = pn;
256 return 1;
257 } else {
Damiend99b0522013-12-21 18:17:45 +0000258 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
259 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100260 *nodes = pn;
261 return 1;
262 } else {
263 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000264 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100265 }
266 }
267}
268
Damiend99b0522013-12-21 18:17:45 +0000269void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100270}
271
Damiend99b0522013-12-21 18:17:45 +0000272void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
273 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100274 for (int i = 0; i < num_nodes; i++) {
275 compile_node(comp, pns->nodes[i]);
276 }
277}
278
Damien3ef4abb2013-10-12 16:53:13 +0100279#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200280STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000281 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100282 return false;
283 }
Damiend99b0522013-12-21 18:17:45 +0000284 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100285 return false;
286 }
287 return true;
288}
289
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200290STATIC void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
Damien George55baff42014-01-21 21:40:13 +0000291 uint len;
292 const byte *str = qstr_data(qstr, &len);
Damien02f89412013-12-12 15:13:36 +0000293 bool has_single_quote = false;
294 bool has_double_quote = false;
295 for (int i = 0; i < len; i++) {
296 if (str[i] == '\'') {
297 has_single_quote = true;
298 } else if (str[i] == '"') {
299 has_double_quote = true;
300 }
301 }
302 if (bytes) {
303 vstr_printf(vstr, "b");
304 }
305 bool quote_single = false;
306 if (has_single_quote && !has_double_quote) {
307 vstr_printf(vstr, "\"");
308 } else {
309 quote_single = true;
310 vstr_printf(vstr, "'");
311 }
312 for (int i = 0; i < len; i++) {
313 if (str[i] == '\n') {
314 vstr_printf(vstr, "\\n");
315 } else if (str[i] == '\\') {
316 vstr_printf(vstr, "\\\\");
317 } else if (str[i] == '\'' && quote_single) {
318 vstr_printf(vstr, "\\'");
319 } else {
320 vstr_printf(vstr, "%c", str[i]);
321 }
322 }
323 if (has_single_quote && !has_double_quote) {
324 vstr_printf(vstr, "\"");
325 } else {
326 vstr_printf(vstr, "'");
327 }
328}
329
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200330STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damiend99b0522013-12-21 18:17:45 +0000331 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200332 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
333 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
334 return;
335 }
336
Damiend99b0522013-12-21 18:17:45 +0000337 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
338 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
339 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000340 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
341 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
342 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
343 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
344 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100345 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000346 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
347 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
348 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien429d7192013-10-04 19:53:11 +0100349 default: assert(0);
350 }
351 break;
352 default: assert(0);
353 }
354}
355
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200356STATIC 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 +0100357 int n = 0;
358 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000359 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100360 }
361 int total = n;
362 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000363 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100364 total += 1;
Damien3a205172013-10-12 15:01:56 +0100365 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100366 is_const = false;
367 }
368 }
369 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100370 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100371 is_const = false;
372 break;
373 }
374 }
375 if (total > 0 && is_const) {
376 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000377 vstr_t *vstr = vstr_new();
378 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000379 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000380 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100381 need_comma = true;
382 }
383 for (int i = 0; i < n; i++) {
384 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000385 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100386 }
Damien02f89412013-12-12 15:13:36 +0000387 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100388 need_comma = true;
389 }
390 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000391 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100392 } else {
Damien02f89412013-12-12 15:13:36 +0000393 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100394 }
Damien Georgeb9791222014-01-23 00:34:21 +0000395 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000396 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100397 } else {
Damiend99b0522013-12-21 18:17:45 +0000398 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100399 compile_node(comp, pn);
400 }
401 for (int i = 0; i < n; i++) {
402 compile_node(comp, pns_list->nodes[i]);
403 }
Damien Georgeb9791222014-01-23 00:34:21 +0000404 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100405 }
406}
Damien3a205172013-10-12 15:01:56 +0100407#endif
408
409// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damiend99b0522013-12-21 18:17:45 +0000410void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100411#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100412 cpython_c_tuple(comp, pn, pns_list);
413#else
414 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000415 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100416 compile_node(comp, pn);
417 total += 1;
418 }
419 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000420 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100421 for (int i = 0; i < n; i++) {
422 compile_node(comp, pns_list->nodes[i]);
423 }
424 total += n;
425 }
Damien Georgeb9791222014-01-23 00:34:21 +0000426 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100427#endif
428}
Damien429d7192013-10-04 19:53:11 +0100429
Damiend99b0522013-12-21 18:17:45 +0000430void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100431 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000432 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100433}
434
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200435STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000436 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200437 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100438}
439
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200440STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200441 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 +0100442}
443
Damien3ef4abb2013-10-12 16:53:13 +0100444#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100445// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200446STATIC 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 +0100447 if (node_is_const_false(pn)) {
448 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000449 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100450 }
451 return;
452 } else if (node_is_const_true(pn)) {
453 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000454 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100455 }
456 return;
Damiend99b0522013-12-21 18:17:45 +0000457 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
458 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
459 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
460 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100461 if (jump_if == false) {
Damienb05d7072013-10-05 13:37:10 +0100462 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100463 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100464 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100465 }
Damien3a205172013-10-12 15:01:56 +0100466 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000467 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100468 } else {
469 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100470 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100471 }
472 }
473 return;
Damiend99b0522013-12-21 18:17:45 +0000474 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100475 if (jump_if == false) {
476 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100477 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100478 }
479 } else {
Damienb05d7072013-10-05 13:37:10 +0100480 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100481 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100482 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100483 }
Damien3a205172013-10-12 15:01:56 +0100484 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000485 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100486 }
487 return;
Damiend99b0522013-12-21 18:17:45 +0000488 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100489 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100490 return;
491 }
492 }
493
494 // nothing special, fall back to default compiling for node and jump
495 compile_node(comp, pn);
496 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000497 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100498 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000499 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100500 }
501}
Damien3a205172013-10-12 15:01:56 +0100502#endif
Damien429d7192013-10-04 19:53:11 +0100503
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200504STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100505#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100506 cpython_c_if_cond(comp, pn, jump_if, label, false);
507#else
508 if (node_is_const_false(pn)) {
509 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000510 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100511 }
512 return;
513 } else if (node_is_const_true(pn)) {
514 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000515 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100516 }
517 return;
Damiend99b0522013-12-21 18:17:45 +0000518 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
519 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
520 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
521 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100522 if (jump_if == false) {
523 int label2 = comp_next_label(comp);
524 for (int i = 0; i < n - 1; i++) {
525 c_if_cond(comp, pns->nodes[i], true, label2);
526 }
527 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000528 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100529 } else {
530 for (int i = 0; i < n; i++) {
531 c_if_cond(comp, pns->nodes[i], true, label);
532 }
533 }
534 return;
Damiend99b0522013-12-21 18:17:45 +0000535 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100536 if (jump_if == false) {
537 for (int i = 0; i < n; i++) {
538 c_if_cond(comp, pns->nodes[i], false, label);
539 }
540 } else {
541 int label2 = comp_next_label(comp);
542 for (int i = 0; i < n - 1; i++) {
543 c_if_cond(comp, pns->nodes[i], false, label2);
544 }
545 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000546 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100547 }
548 return;
Damiend99b0522013-12-21 18:17:45 +0000549 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100550 c_if_cond(comp, pns->nodes[0], !jump_if, label);
551 return;
552 }
553 }
554
555 // nothing special, fall back to default compiling for node and jump
556 compile_node(comp, pn);
557 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000558 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100559 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000560 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100561 }
562#endif
Damien429d7192013-10-04 19:53:11 +0100563}
564
565typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000566void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100567
Damiend99b0522013-12-21 18:17:45 +0000568void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100569 if (assign_kind != ASSIGN_AUG_STORE) {
570 compile_node(comp, pns->nodes[0]);
571 }
572
Damiend99b0522013-12-21 18:17:45 +0000573 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
574 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
575 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
576 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100577 if (assign_kind != ASSIGN_AUG_STORE) {
578 for (int i = 0; i < n - 1; i++) {
579 compile_node(comp, pns1->nodes[i]);
580 }
581 }
Damiend99b0522013-12-21 18:17:45 +0000582 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
583 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100584 }
Damiend99b0522013-12-21 18:17:45 +0000585 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000586 compile_syntax_error(comp, "can't assign to function call");
Damien429d7192013-10-04 19:53:11 +0100587 return;
Damiend99b0522013-12-21 18:17:45 +0000588 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100589 if (assign_kind == ASSIGN_AUG_STORE) {
590 EMIT(rot_three);
591 EMIT(store_subscr);
592 } else {
593 compile_node(comp, pns1->nodes[0]);
594 if (assign_kind == ASSIGN_AUG_LOAD) {
595 EMIT(dup_top_two);
Damien Georgeb9791222014-01-23 00:34:21 +0000596 EMIT_ARG(binary_op, RT_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100597 } else {
598 EMIT(store_subscr);
599 }
600 }
Damiend99b0522013-12-21 18:17:45 +0000601 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
602 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100603 if (assign_kind == ASSIGN_AUG_LOAD) {
604 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000605 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100606 } else {
607 if (assign_kind == ASSIGN_AUG_STORE) {
608 EMIT(rot_two);
609 }
Damien Georgeb9791222014-01-23 00:34:21 +0000610 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100611 }
612 } else {
613 // shouldn't happen
614 assert(0);
615 }
616 } else {
617 // shouldn't happen
618 assert(0);
619 }
620
Damiend99b0522013-12-21 18:17:45 +0000621 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +0100622 // SyntaxError, cannot assign
623 assert(0);
624 }
625}
626
Damiend99b0522013-12-21 18:17:45 +0000627void c_assign_tuple(compiler_t *comp, int n, mp_parse_node_t *nodes) {
Damien429d7192013-10-04 19:53:11 +0100628 assert(n >= 0);
629 int have_star_index = -1;
630 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000631 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100632 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000633 EMIT_ARG(unpack_ex, i, n - i - 1);
Damien429d7192013-10-04 19:53:11 +0100634 have_star_index = i;
635 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000636 compile_syntax_error(comp, "two starred expressions in assignment");
Damien429d7192013-10-04 19:53:11 +0100637 return;
638 }
639 }
640 }
641 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000642 EMIT_ARG(unpack_sequence, n);
Damien429d7192013-10-04 19:53:11 +0100643 }
644 for (int i = 0; i < n; i++) {
645 if (i == have_star_index) {
Damiend99b0522013-12-21 18:17:45 +0000646 c_assign(comp, ((mp_parse_node_struct_t*)nodes[i])->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100647 } else {
648 c_assign(comp, nodes[i], ASSIGN_STORE);
649 }
650 }
651}
652
653// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000654void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100655 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000656 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100657 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000658 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
659 if (MP_PARSE_NODE_IS_ID(pn)) {
660 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100661 switch (assign_kind) {
662 case ASSIGN_STORE:
663 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000664 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100665 break;
666 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000667 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100668 break;
669 }
670 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000671 compile_syntax_error(comp, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100672 return;
673 }
674 } else {
Damiend99b0522013-12-21 18:17:45 +0000675 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
676 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100677 case PN_power:
678 // lhs is an index or attribute
679 c_assign_power(comp, pns, assign_kind);
680 break;
681
682 case PN_testlist_star_expr:
683 case PN_exprlist:
684 // lhs is a tuple
685 if (assign_kind != ASSIGN_STORE) {
686 goto bad_aug;
687 }
Damiend99b0522013-12-21 18:17:45 +0000688 c_assign_tuple(comp, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100689 break;
690
691 case PN_atom_paren:
692 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000693 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100694 // empty tuple
Damien Georgef41fdd02014-03-03 23:19:11 +0000695 compile_syntax_error(comp, "can't assign to ()");
Damien429d7192013-10-04 19:53:11 +0100696 return;
Damiend99b0522013-12-21 18:17:45 +0000697 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
698 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100699 goto testlist_comp;
700 } else {
701 // parenthesis around 1 item, is just that item
702 pn = pns->nodes[0];
703 goto tail_recursion;
704 }
705 break;
706
707 case PN_atom_bracket:
708 // lhs is something in brackets
709 if (assign_kind != ASSIGN_STORE) {
710 goto bad_aug;
711 }
Damiend99b0522013-12-21 18:17:45 +0000712 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100713 // empty list, assignment allowed
714 c_assign_tuple(comp, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000715 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
716 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100717 goto testlist_comp;
718 } else {
719 // brackets around 1 item
720 c_assign_tuple(comp, 1, &pns->nodes[0]);
721 }
722 break;
723
724 default:
Damiend99b0522013-12-21 18:17:45 +0000725 printf("unknown assign, %u\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien429d7192013-10-04 19:53:11 +0100726 assert(0);
727 }
728 return;
729
730 testlist_comp:
731 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000732 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
733 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
734 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100735 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000736 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100737 c_assign_tuple(comp, 1, &pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000738 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100739 // sequence of many items
740 // TODO call c_assign_tuple instead
Damiend99b0522013-12-21 18:17:45 +0000741 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
Damien Georgeb9791222014-01-23 00:34:21 +0000742 EMIT_ARG(unpack_sequence, 1 + n);
Damien429d7192013-10-04 19:53:11 +0100743 c_assign(comp, pns->nodes[0], ASSIGN_STORE);
744 for (int i = 0; i < n; i++) {
745 c_assign(comp, pns2->nodes[i], ASSIGN_STORE);
746 }
Damiend99b0522013-12-21 18:17:45 +0000747 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +0100748 // TODO not implemented
749 assert(0);
750 } else {
751 // sequence with 2 items
752 goto sequence_with_2_items;
753 }
754 } else {
755 // sequence with 2 items
756 sequence_with_2_items:
757 c_assign_tuple(comp, 2, pns->nodes);
758 }
759 return;
760 }
761 return;
762
763 bad_aug:
Damien Georgef41fdd02014-03-03 23:19:11 +0000764 compile_syntax_error(comp, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100765}
766
767// stuff for lambda and comprehensions and generators
768void 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 +0000769#if !MICROPY_EMIT_CPYTHON
770 // in Micro Python we put the default params into a tuple using the bytecode
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200771 if (n_default_params) {
772 EMIT_ARG(build_tuple, n_default_params);
773 }
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000774#endif
775
Damien429d7192013-10-04 19:53:11 +0100776 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000777 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100778 int nfree = 0;
779 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000780 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
781 id_info_t *id = &comp->scope_cur->id_info[i];
782 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
783 for (int j = 0; j < this_scope->id_info_len; j++) {
784 id_info_t *id2 = &this_scope->id_info[j];
785 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000786#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000787 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000788#else
789 // in Micro Python we load closures using LOAD_FAST
Damien Georgeb9791222014-01-23 00:34:21 +0000790 EMIT_ARG(load_fast, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000791#endif
Damien318aec62013-12-10 18:28:17 +0000792 nfree += 1;
793 }
794 }
Damien429d7192013-10-04 19:53:11 +0100795 }
796 }
797 }
Damien429d7192013-10-04 19:53:11 +0100798
799 // make the function/closure
800 if (nfree == 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000801 EMIT_ARG(make_function, this_scope, n_dict_params, n_default_params);
Damien429d7192013-10-04 19:53:11 +0100802 } else {
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000803 EMIT_ARG(build_tuple, nfree);
Damien Georgeb9791222014-01-23 00:34:21 +0000804 EMIT_ARG(make_closure, this_scope, n_dict_params, n_default_params);
Damien429d7192013-10-04 19:53:11 +0100805 }
806}
807
Damiend99b0522013-12-21 18:17:45 +0000808void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000809 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damiend99b0522013-12-21 18:17:45 +0000810 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
811 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100812 // bare star
813 comp->have_bare_star = true;
814 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000815
816 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
817 // TODO do we need to do anything with this?
818
819 } else {
820 mp_parse_node_t pn_id;
821 mp_parse_node_t pn_colon;
822 mp_parse_node_t pn_equal;
823 if (MP_PARSE_NODE_IS_ID(pn)) {
824 // this parameter is just an id
825
826 pn_id = pn;
827 pn_colon = MP_PARSE_NODE_NULL;
828 pn_equal = MP_PARSE_NODE_NULL;
829
830 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
831 // this parameter has a colon and/or equal specifier
832
833 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
834 pn_id = pns->nodes[0];
835 pn_colon = pns->nodes[1];
836 pn_equal = pns->nodes[2];
837
838 } else {
839 assert(0);
840 return;
841 }
842
843 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
844 // this parameter does not have a default value
845
846 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
847 if (!comp->have_bare_star && comp->param_pass_num_default_params != 0) {
848 compile_syntax_error(comp, "non-default argument follows default argument");
849 return;
850 }
851
852 } else {
853 // this parameter has a default value
854 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
855
856 if (comp->have_bare_star) {
857 comp->param_pass_num_dict_params += 1;
858 if (comp->param_pass == 1) {
859 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pn_id));
860 compile_node(comp, pn_equal);
861 }
862 } else {
863 comp->param_pass_num_default_params += 1;
864 if (comp->param_pass == 2) {
865 compile_node(comp, pn_equal);
866 }
867 }
868 }
869
870 // TODO pn_colon not implemented
871 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100872 }
873}
874
875// leaves function object on stack
876// returns function name
Damiend99b0522013-12-21 18:17:45 +0000877qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100878 if (comp->pass == PASS_1) {
879 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000880 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100881 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000882 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100883 }
884
885 // save variables (probably don't need to do this, since we can't have nested definitions..?)
886 bool old_have_bare_star = comp->have_bare_star;
887 int old_param_pass = comp->param_pass;
888 int old_param_pass_num_dict_params = comp->param_pass_num_dict_params;
889 int old_param_pass_num_default_params = comp->param_pass_num_default_params;
890
891 // compile default parameters
Damien Georgef41fdd02014-03-03 23:19:11 +0000892
893 // pass 1 does any default parameters after bare star
Damien429d7192013-10-04 19:53:11 +0100894 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000895 comp->param_pass = 1;
Damien429d7192013-10-04 19:53:11 +0100896 comp->param_pass_num_dict_params = 0;
897 comp->param_pass_num_default_params = 0;
898 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +0000899
900 if (comp->had_error) {
901 return MP_QSTR_NULL;
902 }
903
904 // pass 2 does any default parameters before bare star
Damien429d7192013-10-04 19:53:11 +0100905 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000906 comp->param_pass = 2;
Damien429d7192013-10-04 19:53:11 +0100907 comp->param_pass_num_dict_params = 0;
908 comp->param_pass_num_default_params = 0;
909 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
910
911 // get the scope for this function
912 scope_t *fscope = (scope_t*)pns->nodes[4];
913
914 // make the function
915 close_over_variables_etc(comp, fscope, comp->param_pass_num_dict_params, comp->param_pass_num_default_params);
916
917 // restore variables
918 comp->have_bare_star = old_have_bare_star;
919 comp->param_pass = old_param_pass;
920 comp->param_pass_num_dict_params = old_param_pass_num_dict_params;
921 comp->param_pass_num_default_params = old_param_pass_num_default_params;
922
923 // return its name (the 'f' in "def f(...):")
924 return fscope->simple_name;
925}
926
927// leaves class object on stack
928// returns class name
Damiend99b0522013-12-21 18:17:45 +0000929qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100930 if (comp->pass == PASS_1) {
931 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000932 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100933 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000934 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100935 }
936
937 EMIT(load_build_class);
938
939 // scope for this class
940 scope_t *cscope = (scope_t*)pns->nodes[3];
941
942 // compile the class
943 close_over_variables_etc(comp, cscope, 0, 0);
944
945 // get its name
Damien Georgeb9791222014-01-23 00:34:21 +0000946 EMIT_ARG(load_const_id, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100947
948 // nodes[1] has parent classes, if any
Damien Georgebbcd49a2014-02-06 20:30:16 +0000949 comp->func_arg_is_super = false;
950 compile_trailer_paren_helper(comp, pns->nodes[1], false, 2);
Damien429d7192013-10-04 19:53:11 +0100951
952 // return its name (the 'C' in class C(...):")
953 return cscope->simple_name;
954}
955
Damien6cdd3af2013-10-05 18:08:26 +0100956// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200957STATIC 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 +0000958 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100959 return false;
960 }
961
962 if (name_len != 2) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000963 compile_syntax_error(comp, "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100964 return true;
965 }
966
Damiend99b0522013-12-21 18:17:45 +0000967 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000968 if (attr == MP_QSTR_byte_code) {
Damien5ac1b2e2013-10-18 19:58:12 +0100969 *emit_options = EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +0100970#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000971 } else if (attr == MP_QSTR_native) {
Damien6cdd3af2013-10-05 18:08:26 +0100972 *emit_options = EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000973 } else if (attr == MP_QSTR_viper) {
Damien7af3d192013-10-07 00:02:49 +0100974 *emit_options = EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100975#endif
Damien3ef4abb2013-10-12 16:53:13 +0100976#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000977 } else if (attr == MP_QSTR_asm_thumb) {
Damien5bfb7592013-10-05 18:41:24 +0100978 *emit_options = EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +0100979#endif
Damien6cdd3af2013-10-05 18:08:26 +0100980 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000981 compile_syntax_error(comp, "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100982 }
983
984 return true;
985}
986
Damiend99b0522013-12-21 18:17:45 +0000987void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100988 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +0000989 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +0100990 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
991
Damien6cdd3af2013-10-05 18:08:26 +0100992 // inherit emit options for this function/class definition
993 uint emit_options = comp->scope_cur->emit_options;
994
995 // compile each decorator
996 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +0100997 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000998 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
999 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001000
1001 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001002 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001003 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1004
1005 // check for built-in decorators
1006 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1007 // this was a built-in
1008 num_built_in_decorators += 1;
1009
1010 } else {
1011 // not a built-in, compile normally
1012
1013 // compile the decorator function
1014 compile_node(comp, name_nodes[0]);
1015 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001016 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001017 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001018 }
1019
1020 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001021 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001022 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001023 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001024 compile_node(comp, pns_decorator->nodes[1]);
1025 }
Damien429d7192013-10-04 19:53:11 +01001026 }
1027 }
1028
1029 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001030 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001031 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001032 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001033 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001034 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001035 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001036 } else {
1037 // shouldn't happen
1038 assert(0);
1039 }
1040
1041 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001042 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001043 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01001044 }
1045
1046 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001047 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001048}
1049
Damiend99b0522013-12-21 18:17:45 +00001050void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001051 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001052 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001053 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001054}
1055
Damiend99b0522013-12-21 18:17:45 +00001056void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1057 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001058 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001059 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1060 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001061
1062 compile_node(comp, pns->nodes[0]); // base of the power node
1063
Damiend99b0522013-12-21 18:17:45 +00001064 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1065 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1066 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1067 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001068 for (int i = 0; i < n - 1; i++) {
1069 compile_node(comp, pns1->nodes[i]);
1070 }
Damiend99b0522013-12-21 18:17:45 +00001071 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1072 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001073 }
Damiend99b0522013-12-21 18:17:45 +00001074 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien429d7192013-10-04 19:53:11 +01001075 // SyntaxError: can't delete a function call
1076 assert(0);
Damiend99b0522013-12-21 18:17:45 +00001077 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001078 compile_node(comp, pns1->nodes[0]);
1079 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001080 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1081 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001082 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001083 } else {
1084 // shouldn't happen
1085 assert(0);
1086 }
1087 } else {
1088 // shouldn't happen
1089 assert(0);
1090 }
1091
Damiend99b0522013-12-21 18:17:45 +00001092 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001093 // SyntaxError, cannot delete
1094 assert(0);
1095 }
Damiend99b0522013-12-21 18:17:45 +00001096 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1097 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1098 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1099 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001100 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1101
Damiend99b0522013-12-21 18:17:45 +00001102 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1103 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1104 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001105 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001106 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001107 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001108 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001109 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001110 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001111 c_del_stmt(comp, pns->nodes[0]);
1112 for (int i = 0; i < n; i++) {
1113 c_del_stmt(comp, pns1->nodes[i]);
1114 }
Damiend99b0522013-12-21 18:17:45 +00001115 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001116 // TODO not implemented; can't del comprehension?
1117 assert(0);
1118 } else {
1119 // sequence with 2 items
1120 goto sequence_with_2_items;
1121 }
1122 } else {
1123 // sequence with 2 items
1124 sequence_with_2_items:
1125 c_del_stmt(comp, pns->nodes[0]);
1126 c_del_stmt(comp, pns->nodes[1]);
1127 }
1128 } else {
1129 // tuple with 1 element
1130 c_del_stmt(comp, pn);
1131 }
1132 } else {
1133 // not implemented
1134 assert(0);
1135 }
1136}
1137
Damiend99b0522013-12-21 18:17:45 +00001138void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001139 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1140}
1141
Damiend99b0522013-12-21 18:17:45 +00001142void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001143 if (comp->break_label == 0) {
1144 printf("ERROR: cannot break from here\n");
1145 }
Damien Georgecbddb272014-02-01 20:08:18 +00001146 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001147}
1148
Damiend99b0522013-12-21 18:17:45 +00001149void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001150 if (comp->continue_label == 0) {
1151 printf("ERROR: cannot continue from here\n");
1152 }
Damien Georgecbddb272014-02-01 20:08:18 +00001153 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001154}
1155
Damiend99b0522013-12-21 18:17:45 +00001156void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001157 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001158 compile_syntax_error(comp, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001159 return;
1160 }
Damiend99b0522013-12-21 18:17:45 +00001161 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001162 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001163 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001164 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001165 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001166 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1167 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 +01001168
Damienb05d7072013-10-05 13:37:10 +01001169 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001170 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1171 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1172 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001173 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001174 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1175 } else {
1176 compile_node(comp, pns->nodes[0]);
1177 }
1178 EMIT(return_value);
1179}
1180
Damiend99b0522013-12-21 18:17:45 +00001181void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001182 compile_node(comp, pns->nodes[0]);
1183 EMIT(pop_top);
1184}
1185
Damiend99b0522013-12-21 18:17:45 +00001186void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1187 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001188 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001189 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001190 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001191 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001192 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001193 compile_node(comp, pns->nodes[0]);
1194 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001195 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001196 } else {
1197 // raise x
1198 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001199 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001200 }
1201}
1202
1203// q1 holds the base, q2 the full name
1204// eg a -> q1=q2=a
1205// a.b.c -> q1=a, q2=a.b.c
Damiend99b0522013-12-21 18:17:45 +00001206void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q1, qstr *q2) {
Damien429d7192013-10-04 19:53:11 +01001207 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001208 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1209 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001210 // a name of the form x as y; unwrap it
Damiend99b0522013-12-21 18:17:45 +00001211 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001212 pn = pns->nodes[0];
1213 is_as = true;
1214 }
Damiend99b0522013-12-21 18:17:45 +00001215 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001216 // just a simple name
Damiend99b0522013-12-21 18:17:45 +00001217 *q2 = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001218 if (!is_as) {
1219 *q1 = *q2;
1220 }
Damien Georgeb9791222014-01-23 00:34:21 +00001221 EMIT_ARG(import_name, *q2);
Damiend99b0522013-12-21 18:17:45 +00001222 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1223 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1224 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001225 // a name of the form a.b.c
1226 if (!is_as) {
Damiend99b0522013-12-21 18:17:45 +00001227 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001228 }
Damiend99b0522013-12-21 18:17:45 +00001229 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001230 int len = n - 1;
1231 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001232 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001233 }
Damien George55baff42014-01-21 21:40:13 +00001234 byte *q_ptr;
1235 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001236 for (int i = 0; i < n; i++) {
1237 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001238 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001239 }
Damien George55baff42014-01-21 21:40:13 +00001240 uint str_src_len;
1241 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001242 memcpy(str_dest, str_src, str_src_len);
1243 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001244 }
Damien George55baff42014-01-21 21:40:13 +00001245 *q2 = qstr_build_end(q_ptr);
Damien Georgeb9791222014-01-23 00:34:21 +00001246 EMIT_ARG(import_name, *q2);
Damien429d7192013-10-04 19:53:11 +01001247 if (is_as) {
1248 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001249 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001250 }
1251 }
1252 } else {
1253 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001254 // This covers relative imports starting with dot(s) like "from .foo import"
Damien429d7192013-10-04 19:53:11 +01001255 assert(0);
1256 }
1257 } else {
1258 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001259 // This covers relative imports with dots only like "from .. import"
Damien429d7192013-10-04 19:53:11 +01001260 assert(0);
1261 }
1262}
1263
Damiend99b0522013-12-21 18:17:45 +00001264void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgeb9791222014-01-23 00:34:21 +00001265 EMIT_ARG(load_const_small_int, 0); // ??
1266 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001267 qstr q1, q2;
1268 do_import_name(comp, pn, &q1, &q2);
Damien Georgeb9791222014-01-23 00:34:21 +00001269 EMIT_ARG(store_id, q1);
Damien429d7192013-10-04 19:53:11 +01001270}
1271
Damiend99b0522013-12-21 18:17:45 +00001272void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001273 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1274}
1275
Damiend99b0522013-12-21 18:17:45 +00001276void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
1277 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001278 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001279
1280 // build the "fromlist" tuple
1281#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001282 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001283#else
Damien Georgeb9791222014-01-23 00:34:21 +00001284 EMIT_ARG(load_const_str, QSTR_FROM_STR_STATIC("*"), false);
1285 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001286#endif
1287
1288 // do the import
Damien429d7192013-10-04 19:53:11 +01001289 qstr dummy_q, id1;
1290 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1291 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001292
Damien429d7192013-10-04 19:53:11 +01001293 } else {
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
Damiend99b0522013-12-21 18:17:45 +00001297 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001298 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001299#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001300 {
1301 vstr_t *vstr = vstr_new();
1302 vstr_printf(vstr, "(");
1303 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001304 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1305 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1306 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001307 if (i > 0) {
1308 vstr_printf(vstr, ", ");
1309 }
1310 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001311 uint len;
1312 const byte *str = qstr_data(id2, &len);
1313 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001314 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001315 }
Damien02f89412013-12-12 15:13:36 +00001316 if (n == 1) {
1317 vstr_printf(vstr, ",");
1318 }
1319 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001320 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001321 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001322 }
Damiendb4c3612013-12-10 17:27:24 +00001323#else
1324 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001325 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1326 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1327 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001328 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001329 }
Damien Georgeb9791222014-01-23 00:34:21 +00001330 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001331#endif
1332
1333 // do the import
Damien429d7192013-10-04 19:53:11 +01001334 qstr dummy_q, id1;
1335 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1336 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001337 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1338 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1339 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001340 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001341 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001342 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001343 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001344 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001345 }
1346 }
1347 EMIT(pop_top);
1348 }
1349}
1350
Damiend99b0522013-12-21 18:17:45 +00001351void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001352 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001353 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1354 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001355 } else {
Damiend99b0522013-12-21 18:17:45 +00001356 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1357 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001358 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001359 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001360 }
Damien429d7192013-10-04 19:53:11 +01001361 }
1362 }
1363}
1364
Damiend99b0522013-12-21 18:17:45 +00001365void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001366 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001367 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1368 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001369 } else {
Damiend99b0522013-12-21 18:17:45 +00001370 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1371 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001372 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001373 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001374 }
Damien429d7192013-10-04 19:53:11 +01001375 }
1376 }
1377}
1378
Damiend99b0522013-12-21 18:17:45 +00001379void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001380 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001381 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001382 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 +00001383 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001384 // assertion message
1385 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001386 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01001387 }
Damien Georgeb9791222014-01-23 00:34:21 +00001388 EMIT_ARG(raise_varargs, 1);
1389 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001390}
1391
Damiend99b0522013-12-21 18:17:45 +00001392void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001393 // TODO proper and/or short circuiting
1394
Damienb05d7072013-10-05 13:37:10 +01001395 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001396
Damienb05d7072013-10-05 13:37:10 +01001397 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001398 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1399
1400 compile_node(comp, pns->nodes[1]); // if block
Damiend99b0522013-12-21 18:17:45 +00001401 //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 +01001402 // jump over elif/else blocks if they exist
Damien415eb6f2013-10-05 12:19:06 +01001403 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001404 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001405 }
1406 //}
Damien Georgeb9791222014-01-23 00:34:21 +00001407 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001408
Damiend99b0522013-12-21 18:17:45 +00001409 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001410 // compile elif blocks
1411
Damiend99b0522013-12-21 18:17:45 +00001412 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001413
Damiend99b0522013-12-21 18:17:45 +00001414 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001415 // multiple elif blocks
1416
Damiend99b0522013-12-21 18:17:45 +00001417 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001418 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001419 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001420 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001421 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1422
1423 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001424 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001425 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001426 }
Damien Georgeb9791222014-01-23 00:34:21 +00001427 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001428 }
1429
1430 } else {
1431 // a single elif block
1432
Damienb05d7072013-10-05 13:37:10 +01001433 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001434 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1435
1436 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001437 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001438 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001439 }
Damien Georgeb9791222014-01-23 00:34:21 +00001440 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001441 }
1442 }
1443
1444 // compile else block
1445 compile_node(comp, pns->nodes[3]); // can be null
1446
Damien Georgeb9791222014-01-23 00:34:21 +00001447 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001448}
1449
Damien Georgecbddb272014-02-01 20:08:18 +00001450#define START_BREAK_CONTINUE_BLOCK \
1451 int old_break_label = comp->break_label; \
1452 int old_continue_label = comp->continue_label; \
1453 int break_label = comp_next_label(comp); \
1454 int continue_label = comp_next_label(comp); \
1455 comp->break_label = break_label; \
1456 comp->continue_label = continue_label; \
1457 comp->break_continue_except_level = comp->cur_except_level;
1458
1459#define END_BREAK_CONTINUE_BLOCK \
1460 comp->break_label = old_break_label; \
1461 comp->continue_label = old_continue_label; \
1462 comp->break_continue_except_level = comp->cur_except_level;
1463
Damiend99b0522013-12-21 18:17:45 +00001464void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001465 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001466
Damience89a212013-10-15 22:25:17 +01001467 // compared to CPython, we have an optimised version of while loops
1468#if MICROPY_EMIT_CPYTHON
1469 int done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001470 EMIT_ARG(setup_loop, break_label);
1471 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001472 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1473 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001474 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001475 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001476 }
Damien Georgeb9791222014-01-23 00:34:21 +00001477 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001478 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1479 // this is a small hack to agree with CPython
1480 if (!node_is_const_true(pns->nodes[0])) {
1481 EMIT(pop_block);
1482 }
Damience89a212013-10-15 22:25:17 +01001483#else
1484 int top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001485 EMIT_ARG(jump, continue_label);
1486 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001487 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001488 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001489 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1490#endif
1491
1492 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001493 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001494
1495 compile_node(comp, pns->nodes[2]); // else
1496
Damien Georgeb9791222014-01-23 00:34:21 +00001497 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001498}
1499
Damienf72fd0e2013-11-06 20:20:49 +00001500// TODO preload end and step onto stack if they are not constants
1501// TODO check if step is negative and do opposite test
Damiend99b0522013-12-21 18:17:45 +00001502void 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 +00001503 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001504
1505 int top_label = comp_next_label(comp);
Damien George600ae732014-01-21 23:48:04 +00001506 int entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001507
1508 // compile: var = start
1509 compile_node(comp, pn_start);
1510 c_assign(comp, pn_var, ASSIGN_STORE);
1511
Damien Georgeb9791222014-01-23 00:34:21 +00001512 EMIT_ARG(jump, entry_label);
1513 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001514
Damienf3822fc2013-11-09 20:12:03 +00001515 // compile body
1516 compile_node(comp, pn_body);
1517
Damien Georgeb9791222014-01-23 00:34:21 +00001518 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001519
Damienf72fd0e2013-11-06 20:20:49 +00001520 // compile: var += step
1521 c_assign(comp, pn_var, ASSIGN_AUG_LOAD);
1522 compile_node(comp, pn_step);
Damien Georgeb9791222014-01-23 00:34:21 +00001523 EMIT_ARG(binary_op, RT_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001524 c_assign(comp, pn_var, ASSIGN_AUG_STORE);
1525
Damien Georgeb9791222014-01-23 00:34:21 +00001526 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001527
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001528 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001529 compile_node(comp, pn_var);
1530 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001531 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1532 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien George9aa2a522014-02-01 23:04:09 +00001533 EMIT_ARG(binary_op, RT_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001534 } else {
Damien George9aa2a522014-02-01 23:04:09 +00001535 EMIT_ARG(binary_op, RT_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001536 }
Damien Georgeb9791222014-01-23 00:34:21 +00001537 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001538
1539 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001540 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001541
1542 compile_node(comp, pn_else);
1543
Damien Georgeb9791222014-01-23 00:34:21 +00001544 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001545}
1546
Damiend99b0522013-12-21 18:17:45 +00001547void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001548#if !MICROPY_EMIT_CPYTHON
1549 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1550 // this is actually slower, but uses no heap memory
1551 // for viper it will be much, much faster
Damiend99b0522013-12-21 18:17:45 +00001552 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)) {
1553 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001554 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1555 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1556 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1557 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001558 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1559 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001560 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001561 mp_parse_node_t pn_range_start;
1562 mp_parse_node_t pn_range_end;
1563 mp_parse_node_t pn_range_step;
1564 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001565 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001566 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001567 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001568 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001569 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001570 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001571 } else if (n_args == 2) {
1572 pn_range_start = args[0];
1573 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001574 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001575 } else {
1576 pn_range_start = args[0];
1577 pn_range_end = args[1];
1578 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001579 // We need to know sign of step. This is possible only if it's constant
1580 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1581 optimize = false;
1582 }
Damienf72fd0e2013-11-06 20:20:49 +00001583 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001584 }
1585 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001586 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1587 return;
1588 }
1589 }
1590 }
1591#endif
1592
Damien Georgecbddb272014-02-01 20:08:18 +00001593 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001594
Damienb05d7072013-10-05 13:37:10 +01001595 int pop_label = comp_next_label(comp);
1596 int end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001597
Damience89a212013-10-15 22:25:17 +01001598 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1599#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001600 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001601#endif
1602
Damien429d7192013-10-04 19:53:11 +01001603 compile_node(comp, pns->nodes[1]); // iterator
1604 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001605 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001606 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001607 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1608 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001609 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001610 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001611 }
Damien Georgeb9791222014-01-23 00:34:21 +00001612 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001613 EMIT(for_iter_end);
1614
1615 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001616 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001617
Damience89a212013-10-15 22:25:17 +01001618#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001619 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001620#endif
Damien429d7192013-10-04 19:53:11 +01001621
1622 compile_node(comp, pns->nodes[3]); // else (not tested)
1623
Damien Georgeb9791222014-01-23 00:34:21 +00001624 EMIT_ARG(label_assign, break_label);
1625 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001626}
1627
Damiend99b0522013-12-21 18:17:45 +00001628void 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 +01001629 // this function is a bit of a hack at the moment
1630 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1631
1632 // setup code
1633 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001634 int l1 = comp_next_label(comp);
1635 int success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001636
Damien Georgeb9791222014-01-23 00:34:21 +00001637 EMIT_ARG(setup_except, l1);
Damien Georgecbddb272014-02-01 20:08:18 +00001638 comp->cur_except_level += 1;
1639
Damien429d7192013-10-04 19:53:11 +01001640 compile_node(comp, pn_body); // body
1641 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001642 EMIT_ARG(jump, success_label);
1643 EMIT_ARG(label_assign, l1);
Damienb05d7072013-10-05 13:37:10 +01001644 int l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001645
1646 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001647 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1648 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001649
1650 qstr qstr_exception_local = 0;
Damienb05d7072013-10-05 13:37:10 +01001651 int end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001652
Damiend99b0522013-12-21 18:17:45 +00001653 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001654 // this is a catch all exception handler
1655 if (i + 1 != n_except) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001656 compile_syntax_error(comp, "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001657 return;
1658 }
1659 } else {
1660 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001661 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1662 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1663 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1664 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001665 // handler binds the exception to a local
1666 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001667 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001668 }
1669 }
1670 EMIT(dup_top);
1671 compile_node(comp, pns_exception_expr);
Damien George9aa2a522014-02-01 23:04:09 +00001672 EMIT_ARG(binary_op, RT_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001673 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001674 }
1675
1676 EMIT(pop_top);
1677
1678 if (qstr_exception_local == 0) {
1679 EMIT(pop_top);
1680 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001681 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001682 }
1683
1684 EMIT(pop_top);
1685
Damiene2880aa2013-12-20 14:22:59 +00001686 int l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001687 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001688 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001689 EMIT_ARG(setup_finally, l3);
Damien Georgecbddb272014-02-01 20:08:18 +00001690 comp->cur_except_level += 1;
Damien429d7192013-10-04 19:53:11 +01001691 }
1692 compile_node(comp, pns_except->nodes[1]);
1693 if (qstr_exception_local != 0) {
1694 EMIT(pop_block);
1695 }
1696 EMIT(pop_except);
1697 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001698 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1699 EMIT_ARG(label_assign, l3);
1700 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1701 EMIT_ARG(store_id, qstr_exception_local);
1702 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001703
1704 comp->cur_except_level -= 1;
Damien429d7192013-10-04 19:53:11 +01001705 EMIT(end_finally);
1706 }
Damien Georgeb9791222014-01-23 00:34:21 +00001707 EMIT_ARG(jump, l2);
1708 EMIT_ARG(label_assign, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001709 }
1710
Damien Georgecbddb272014-02-01 20:08:18 +00001711 comp->cur_except_level -= 1;
Damien429d7192013-10-04 19:53:11 +01001712 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001713
Damien Georgeb9791222014-01-23 00:34:21 +00001714 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001715 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001716 EMIT_ARG(label_assign, l2);
1717 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001718}
1719
Damiend99b0522013-12-21 18:17:45 +00001720void 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 +01001721 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1722 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001723 int l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001724
Damien Georgeb9791222014-01-23 00:34:21 +00001725 EMIT_ARG(setup_finally, l_finally_block);
Damien Georgecbddb272014-02-01 20:08:18 +00001726 comp->cur_except_level += 1;
1727
Damien429d7192013-10-04 19:53:11 +01001728 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001729 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien429d7192013-10-04 19:53:11 +01001730 compile_node(comp, pn_body);
1731 } else {
1732 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1733 }
1734 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001735 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1736 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001737 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001738
1739 comp->cur_except_level -= 1;
Damien429d7192013-10-04 19:53:11 +01001740 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001741
Damien Georgeb9791222014-01-23 00:34:21 +00001742 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001743}
1744
Damiend99b0522013-12-21 18:17:45 +00001745void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1746 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1747 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1748 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001749 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001750 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1751 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001752 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001753 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001754 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001755 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001756 // no finally
1757 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1758 } else {
1759 // have finally
Damiend99b0522013-12-21 18:17:45 +00001760 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 +01001761 }
1762 } else {
1763 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001764 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001765 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001766 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001767 }
1768 } else {
1769 // shouldn't happen
1770 assert(0);
1771 }
1772}
1773
Damiend99b0522013-12-21 18:17:45 +00001774void 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 +01001775 if (n == 0) {
1776 // no more pre-bits, compile the body of the with
1777 compile_node(comp, body);
1778 } else {
Damienb05d7072013-10-05 13:37:10 +01001779 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001780 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001781 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001782 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001783 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001784 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001785 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1786 } else {
1787 // this pre-bit is just an expression
1788 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001789 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001790 EMIT(pop_top);
1791 }
1792 // compile additional pre-bits and the body
1793 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1794 // finish this with block
1795 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001796 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1797 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001798 EMIT(with_cleanup);
1799 EMIT(end_finally);
1800 }
1801}
1802
Damiend99b0522013-12-21 18:17:45 +00001803void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001804 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001805 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001806 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1807 assert(n > 0);
1808
1809 // compile in a nested fashion
1810 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1811}
1812
Damiend99b0522013-12-21 18:17:45 +00001813void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1814 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001815 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1816 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001817 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001818 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001819 EMIT_ARG(call_function, 1, 0, false, false);
Damien5ac1b2e2013-10-18 19:58:12 +01001820 EMIT(pop_top);
1821
Damien429d7192013-10-04 19:53:11 +01001822 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001823 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001824 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001825 // do nothing with a lonely constant
1826 } else {
1827 compile_node(comp, pns->nodes[0]); // just an expression
1828 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1829 }
Damien429d7192013-10-04 19:53:11 +01001830 }
1831 } else {
Damiend99b0522013-12-21 18:17:45 +00001832 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1833 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001834 if (kind == PN_expr_stmt_augassign) {
1835 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1836 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001837 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien George7e5fb242014-02-01 22:18:47 +00001838 rt_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001839 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien George7e5fb242014-02-01 22:18:47 +00001840 case MP_TOKEN_DEL_PIPE_EQUAL: op = RT_BINARY_OP_INPLACE_OR; break;
1841 case MP_TOKEN_DEL_CARET_EQUAL: op = RT_BINARY_OP_INPLACE_XOR; break;
1842 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = RT_BINARY_OP_INPLACE_AND; break;
1843 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = RT_BINARY_OP_INPLACE_LSHIFT; break;
1844 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = RT_BINARY_OP_INPLACE_RSHIFT; break;
1845 case MP_TOKEN_DEL_PLUS_EQUAL: op = RT_BINARY_OP_INPLACE_ADD; break;
1846 case MP_TOKEN_DEL_MINUS_EQUAL: op = RT_BINARY_OP_INPLACE_SUBTRACT; break;
1847 case MP_TOKEN_DEL_STAR_EQUAL: op = RT_BINARY_OP_INPLACE_MULTIPLY; break;
1848 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = RT_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1849 case MP_TOKEN_DEL_SLASH_EQUAL: op = RT_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1850 case MP_TOKEN_DEL_PERCENT_EQUAL: op = RT_BINARY_OP_INPLACE_MODULO; break;
1851 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = RT_BINARY_OP_INPLACE_POWER; break;
1852 default: assert(0); op = RT_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01001853 }
Damien George7e5fb242014-02-01 22:18:47 +00001854 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001855 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1856 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001857 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1858 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001859 // following CPython, we store left-most first
1860 if (rhs > 0) {
1861 EMIT(dup_top);
1862 }
1863 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1864 for (int i = 0; i < rhs; i++) {
1865 if (i + 1 < rhs) {
1866 EMIT(dup_top);
1867 }
Damiend99b0522013-12-21 18:17:45 +00001868 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001869 }
1870 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001871 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1872 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1873 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1874 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01001875 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001876 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1877 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001878 compile_node(comp, pns10->nodes[0]); // rhs
1879 compile_node(comp, pns10->nodes[1]); // rhs
1880 EMIT(rot_two);
1881 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1882 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00001883 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1884 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1885 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1886 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01001887 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001888 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1889 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001890 compile_node(comp, pns10->nodes[0]); // rhs
1891 compile_node(comp, pns10->nodes[1]); // rhs
1892 compile_node(comp, pns10->nodes[2]); // rhs
1893 EMIT(rot_three);
1894 EMIT(rot_two);
1895 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1896 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1897 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1898 } else {
1899 compile_node(comp, pns1->nodes[0]); // rhs
1900 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1901 }
1902 } else {
1903 // shouldn't happen
1904 assert(0);
1905 }
1906 }
1907}
1908
Damiend99b0522013-12-21 18:17:45 +00001909void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, rt_binary_op_t binary_op) {
1910 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001911 compile_node(comp, pns->nodes[0]);
1912 for (int i = 1; i < num_nodes; i += 1) {
1913 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001914 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001915 }
1916}
1917
Damiend99b0522013-12-21 18:17:45 +00001918void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1919 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1920 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001921
1922 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001923 int l_fail = comp_next_label(comp);
1924 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001925 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1926 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001927 EMIT_ARG(jump, l_end);
1928 EMIT_ARG(label_assign, l_fail);
1929 EMIT_ARG(set_stack_size, stack_size); // force stack size reset
Damien429d7192013-10-04 19:53:11 +01001930 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001931 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001932}
1933
Damiend99b0522013-12-21 18:17:45 +00001934void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001935 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00001936 //mp_parse_node_t pn_params = pns->nodes[0];
1937 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001938
1939 if (comp->pass == PASS_1) {
1940 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00001941 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 +01001942 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001943 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001944 }
1945
1946 // get the scope for this lambda
1947 scope_t *this_scope = (scope_t*)pns->nodes[2];
1948
1949 // make the lambda
1950 close_over_variables_etc(comp, this_scope, 0, 0);
1951}
1952
Damiend99b0522013-12-21 18:17:45 +00001953void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001954 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001955 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001956 for (int i = 0; i < n; i += 1) {
1957 compile_node(comp, pns->nodes[i]);
1958 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00001959 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01001960 }
1961 }
Damien Georgeb9791222014-01-23 00:34:21 +00001962 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001963}
1964
Damiend99b0522013-12-21 18:17:45 +00001965void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001966 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001967 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001968 for (int i = 0; i < n; i += 1) {
1969 compile_node(comp, pns->nodes[i]);
1970 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00001971 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01001972 }
1973 }
Damien Georgeb9791222014-01-23 00:34:21 +00001974 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001975}
1976
Damiend99b0522013-12-21 18:17:45 +00001977void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001978 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001979 EMIT_ARG(unary_op, RT_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01001980}
1981
Damiend99b0522013-12-21 18:17:45 +00001982void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001983 int stack_size = EMIT(get_stack_size);
Damiend99b0522013-12-21 18:17:45 +00001984 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001985 compile_node(comp, pns->nodes[0]);
1986 bool multi = (num_nodes > 3);
1987 int l_fail = 0;
1988 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001989 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001990 }
1991 for (int i = 1; i + 1 < num_nodes; i += 2) {
1992 compile_node(comp, pns->nodes[i + 1]);
1993 if (i + 2 < num_nodes) {
1994 EMIT(dup_top);
1995 EMIT(rot_three);
1996 }
Damien George7e5fb242014-02-01 22:18:47 +00001997 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
1998 rt_binary_op_t op;
1999 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien George9aa2a522014-02-01 23:04:09 +00002000 case MP_TOKEN_OP_LESS: op = RT_BINARY_OP_LESS; break;
2001 case MP_TOKEN_OP_MORE: op = RT_BINARY_OP_MORE; break;
2002 case MP_TOKEN_OP_DBL_EQUAL: op = RT_BINARY_OP_EQUAL; break;
2003 case MP_TOKEN_OP_LESS_EQUAL: op = RT_BINARY_OP_LESS_EQUAL; break;
2004 case MP_TOKEN_OP_MORE_EQUAL: op = RT_BINARY_OP_MORE_EQUAL; break;
2005 case MP_TOKEN_OP_NOT_EQUAL: op = RT_BINARY_OP_NOT_EQUAL; break;
2006 case MP_TOKEN_KW_IN: op = RT_BINARY_OP_IN; break;
2007 default: assert(0); op = RT_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002008 }
2009 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002010 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2011 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2012 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002013 if (kind == PN_comp_op_not_in) {
Damien George9aa2a522014-02-01 23:04:09 +00002014 EMIT_ARG(binary_op, RT_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002015 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002016 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien George9aa2a522014-02-01 23:04:09 +00002017 EMIT_ARG(binary_op, RT_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002018 } else {
Damien George9aa2a522014-02-01 23:04:09 +00002019 EMIT_ARG(binary_op, RT_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002020 }
2021 } else {
2022 // shouldn't happen
2023 assert(0);
2024 }
2025 } else {
2026 // shouldn't happen
2027 assert(0);
2028 }
2029 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002030 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002031 }
2032 }
2033 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002034 int l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002035 EMIT_ARG(jump, l_end);
2036 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01002037 EMIT(rot_two);
2038 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002039 EMIT_ARG(label_assign, l_end);
2040 EMIT_ARG(set_stack_size, stack_size + 1); // force stack size
Damien429d7192013-10-04 19:53:11 +01002041 }
2042}
2043
Damiend99b0522013-12-21 18:17:45 +00002044void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002045 // TODO
2046 assert(0);
2047 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002048 //EMIT_ARG(unary_op, "UNARY_STAR");
Damien429d7192013-10-04 19:53:11 +01002049}
2050
Damiend99b0522013-12-21 18:17:45 +00002051void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002052 c_binary_op(comp, pns, RT_BINARY_OP_OR);
2053}
2054
Damiend99b0522013-12-21 18:17:45 +00002055void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002056 c_binary_op(comp, pns, RT_BINARY_OP_XOR);
2057}
2058
Damiend99b0522013-12-21 18:17:45 +00002059void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002060 c_binary_op(comp, pns, RT_BINARY_OP_AND);
2061}
2062
Damiend99b0522013-12-21 18:17:45 +00002063void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2064 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002065 compile_node(comp, pns->nodes[0]);
2066 for (int i = 1; i + 1 < num_nodes; i += 2) {
2067 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002068 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002069 EMIT_ARG(binary_op, RT_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002070 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002071 EMIT_ARG(binary_op, RT_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002072 } else {
2073 // shouldn't happen
2074 assert(0);
2075 }
2076 }
2077}
2078
Damiend99b0522013-12-21 18:17:45 +00002079void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2080 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002081 compile_node(comp, pns->nodes[0]);
2082 for (int i = 1; i + 1 < num_nodes; i += 2) {
2083 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002084 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002085 EMIT_ARG(binary_op, RT_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002086 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002087 EMIT_ARG(binary_op, RT_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002088 } else {
2089 // shouldn't happen
2090 assert(0);
2091 }
2092 }
2093}
2094
Damiend99b0522013-12-21 18:17:45 +00002095void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2096 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002097 compile_node(comp, pns->nodes[0]);
2098 for (int i = 1; i + 1 < num_nodes; i += 2) {
2099 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002100 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002101 EMIT_ARG(binary_op, RT_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002102 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002103 EMIT_ARG(binary_op, RT_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002104 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002105 EMIT_ARG(binary_op, RT_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002106 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002107 EMIT_ARG(binary_op, RT_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002108 } else {
2109 // shouldn't happen
2110 assert(0);
2111 }
2112 }
2113}
2114
Damiend99b0522013-12-21 18:17:45 +00002115void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002116 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002117 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002118 EMIT_ARG(unary_op, RT_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002119 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002120 EMIT_ARG(unary_op, RT_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002121 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002122 EMIT_ARG(unary_op, RT_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002123 } else {
2124 // shouldn't happen
2125 assert(0);
2126 }
2127}
2128
Damien George35e2a4e2014-02-05 00:51:47 +00002129void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2130 // this is to handle special super() call
2131 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2132
2133 compile_generic_all_nodes(comp, pns);
2134}
2135
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002136STATIC 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 +01002137 // function to call is on top of stack
2138
Damien George35e2a4e2014-02-05 00:51:47 +00002139#if !MICROPY_EMIT_CPYTHON
2140 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002141 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 +00002142 EMIT_ARG(load_id, MP_QSTR___class__);
2143 // get first argument to function
2144 bool found = false;
2145 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
2146 if (comp->scope_cur->id_info[i].param) {
2147 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
2148 found = true;
2149 break;
2150 }
2151 }
2152 if (!found) {
2153 printf("TypeError: super() call cannot find self\n");
2154 return;
2155 }
2156 EMIT_ARG(call_function, 2, 0, false, false);
2157 return;
2158 }
2159#endif
2160
Damien429d7192013-10-04 19:53:11 +01002161 int old_n_arg_keyword = comp->n_arg_keyword;
2162 bool old_have_star_arg = comp->have_star_arg;
2163 bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
2164 comp->n_arg_keyword = 0;
2165 comp->have_star_arg = false;
2166 comp->have_dbl_star_arg = false;
2167
Damien Georgebbcd49a2014-02-06 20:30:16 +00002168 compile_node(comp, pn_arglist); // arguments to function call; can be null
Damien429d7192013-10-04 19:53:11 +01002169
2170 // compute number of positional arguments
Damien Georgebbcd49a2014-02-06 20:30:16 +00002171 int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
Damien429d7192013-10-04 19:53:11 +01002172 if (comp->have_star_arg) {
2173 n_positional -= 1;
2174 }
2175 if (comp->have_dbl_star_arg) {
2176 n_positional -= 1;
2177 }
2178
2179 if (is_method_call) {
Damien Georgeb9791222014-01-23 00:34:21 +00002180 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 +01002181 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002182 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 +01002183 }
2184
2185 comp->n_arg_keyword = old_n_arg_keyword;
2186 comp->have_star_arg = old_have_star_arg;
2187 comp->have_dbl_star_arg = old_have_dbl_star_arg;
2188}
2189
Damiend99b0522013-12-21 18:17:45 +00002190void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2191 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002192 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002193 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 +01002194 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002195 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2196 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002197 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002198 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002199 i += 1;
2200 } else {
2201 compile_node(comp, pns->nodes[i]);
2202 }
Damien George35e2a4e2014-02-05 00:51:47 +00002203 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002204 }
2205}
2206
Damiend99b0522013-12-21 18:17:45 +00002207void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002208 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002209 EMIT_ARG(binary_op, RT_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002210}
2211
Damiend99b0522013-12-21 18:17:45 +00002212void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002213 // a list of strings
Damien63321742013-12-10 17:41:49 +00002214
2215 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002216 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002217 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002218 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002219 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002220 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2221 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2222 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002223 if (i == 0) {
2224 string_kind = pn_kind;
2225 } else if (pn_kind != string_kind) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002226 compile_syntax_error(comp, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002227 return;
2228 }
Damien George55baff42014-01-21 21:40:13 +00002229 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002230 }
Damien63321742013-12-10 17:41:49 +00002231
Damien63321742013-12-10 17:41:49 +00002232 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002233 byte *q_ptr;
2234 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002235 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002236 uint s_len;
2237 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002238 memcpy(s_dest, s, s_len);
2239 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002240 }
Damien George55baff42014-01-21 21:40:13 +00002241 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002242
Damien Georgeb9791222014-01-23 00:34:21 +00002243 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002244}
2245
2246// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002247void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2248 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2249 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2250 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002251
2252 if (comp->pass == PASS_1) {
2253 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002254 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 +01002255 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002256 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002257 }
2258
2259 // get the scope for this comprehension
2260 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2261
2262 // compile the comprehension
2263 close_over_variables_etc(comp, this_scope, 0, 0);
2264
2265 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2266 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002267 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01002268}
2269
Damiend99b0522013-12-21 18:17:45 +00002270void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2271 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002272 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002273 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2274 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2275 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2276 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2277 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2278 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2279 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002280 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002281 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002282 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002283 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002284 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002285 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002286 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002287 // generator expression
2288 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2289 } else {
2290 // tuple with 2 items
2291 goto tuple_with_2_items;
2292 }
2293 } else {
2294 // tuple with 2 items
2295 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002296 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002297 }
2298 } else {
2299 // parenthesis around a single item, is just that item
2300 compile_node(comp, pns->nodes[0]);
2301 }
2302}
2303
Damiend99b0522013-12-21 18:17:45 +00002304void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2305 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002306 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002307 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002308 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2309 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2310 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2311 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2312 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002313 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002314 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002315 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002316 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002317 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002318 // list of many items
2319 compile_node(comp, pns2->nodes[0]);
2320 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002321 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002322 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002323 // list comprehension
2324 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2325 } else {
2326 // list with 2 items
2327 goto list_with_2_items;
2328 }
2329 } else {
2330 // list with 2 items
2331 list_with_2_items:
2332 compile_node(comp, pns2->nodes[0]);
2333 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002334 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002335 }
2336 } else {
2337 // list with 1 item
2338 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002339 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002340 }
2341}
2342
Damiend99b0522013-12-21 18:17:45 +00002343void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2344 mp_parse_node_t pn = pns->nodes[0];
2345 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002346 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002347 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002348 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2349 pns = (mp_parse_node_struct_t*)pn;
2350 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002351 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002352 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002353 compile_node(comp, pn);
2354 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002355 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2356 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2357 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2358 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002359 // dict/set with multiple elements
2360
2361 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002362 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002363 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2364
2365 // first element sets whether it's a dict or set
2366 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002367 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002368 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002369 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002370 compile_node(comp, pns->nodes[0]);
2371 EMIT(store_map);
2372 is_dict = true;
2373 } else {
2374 // a set
2375 compile_node(comp, pns->nodes[0]); // 1st value of set
2376 is_dict = false;
2377 }
2378
2379 // process rest of elements
2380 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002381 mp_parse_node_t pn = nodes[i];
2382 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002383 compile_node(comp, pn);
2384 if (is_dict) {
2385 if (!is_key_value) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002386 compile_syntax_error(comp, "?expecting key:value for dictiona");
Damien429d7192013-10-04 19:53:11 +01002387 return;
2388 }
2389 EMIT(store_map);
2390 } else {
2391 if (is_key_value) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002392 compile_syntax_error(comp, "?expecting just a value for s");
Damien429d7192013-10-04 19:53:11 +01002393 return;
2394 }
2395 }
2396 }
2397
2398 // if it's a set, build it
2399 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002400 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002401 }
Damiend99b0522013-12-21 18:17:45 +00002402 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002403 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002404 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002405 // a dictionary comprehension
2406 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2407 } else {
2408 // a set comprehension
2409 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2410 }
2411 } else {
2412 // shouldn't happen
2413 assert(0);
2414 }
2415 } else {
2416 // set with one element
2417 goto set_with_one_element;
2418 }
2419 } else {
2420 // set with one element
2421 set_with_one_element:
2422 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002423 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002424 }
2425}
2426
Damiend99b0522013-12-21 18:17:45 +00002427void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002428 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002429}
2430
Damiend99b0522013-12-21 18:17:45 +00002431void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002432 // object who's index we want is on top of stack
2433 compile_node(comp, pns->nodes[0]); // the index
Damien Georgeb9791222014-01-23 00:34:21 +00002434 EMIT_ARG(binary_op, RT_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002435}
2436
Damiend99b0522013-12-21 18:17:45 +00002437void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002438 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002439 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002440}
2441
Damiend99b0522013-12-21 18:17:45 +00002442void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2443 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2444 mp_parse_node_t pn = pns->nodes[0];
2445 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002446 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002447 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2448 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002449 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2450 pns = (mp_parse_node_struct_t*)pn;
2451 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002452 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002453 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002454 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002455 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002456 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002457 } else {
2458 // [?::x]
2459 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002460 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002461 }
Damiend99b0522013-12-21 18:17:45 +00002462 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002463 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002464 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2465 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2466 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2467 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002468 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002469 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002470 } else {
2471 // [?:x:x]
2472 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002473 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002474 }
2475 } else {
2476 // [?:x]
2477 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002478 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002479 }
2480 } else {
2481 // [?:x]
2482 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002483 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002484 }
2485}
2486
Damiend99b0522013-12-21 18:17:45 +00002487void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002488 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002489 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2490 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002491}
2492
Damiend99b0522013-12-21 18:17:45 +00002493void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002494 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002495 compile_subscript_3_helper(comp, pns);
2496}
2497
Damiend99b0522013-12-21 18:17:45 +00002498void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002499 // if this is called then we are compiling a dict key:value pair
2500 compile_node(comp, pns->nodes[1]); // value
2501 compile_node(comp, pns->nodes[0]); // key
2502}
2503
Damiend99b0522013-12-21 18:17:45 +00002504void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002505 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002506 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002507 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002508}
2509
Damiend99b0522013-12-21 18:17:45 +00002510void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002511 if (comp->have_star_arg) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002512 compile_syntax_error(comp, "?can't have multiple *x");
Damien429d7192013-10-04 19:53:11 +01002513 return;
2514 }
2515 comp->have_star_arg = true;
2516 compile_node(comp, pns->nodes[0]);
2517}
2518
Damiend99b0522013-12-21 18:17:45 +00002519void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002520 if (comp->have_dbl_star_arg) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002521 compile_syntax_error(comp, "?can't have multiple **x");
Damien429d7192013-10-04 19:53:11 +01002522 return;
2523 }
2524 comp->have_dbl_star_arg = true;
2525 compile_node(comp, pns->nodes[0]);
2526}
2527
Damiend99b0522013-12-21 18:17:45 +00002528void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2529 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2530 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2531 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2532 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002533 compile_syntax_error(comp, "?lhs of keyword argument must be an id");
Damien429d7192013-10-04 19:53:11 +01002534 return;
2535 }
Damien Georgeb9791222014-01-23 00:34:21 +00002536 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002537 compile_node(comp, pns2->nodes[0]);
2538 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002539 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002540 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2541 } else {
2542 // shouldn't happen
2543 assert(0);
2544 }
2545}
2546
Damiend99b0522013-12-21 18:17:45 +00002547void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002548 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002549 compile_syntax_error(comp, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002550 return;
2551 }
Damiend99b0522013-12-21 18:17:45 +00002552 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002553 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002554 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002555 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2556 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002557 compile_node(comp, pns->nodes[0]);
2558 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002559 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002560 EMIT(yield_from);
2561 } else {
2562 compile_node(comp, pns->nodes[0]);
2563 EMIT(yield_value);
2564 }
2565}
2566
Damiend99b0522013-12-21 18:17:45 +00002567typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002568STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002569 NULL,
2570#define nc NULL
2571#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002572#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002573#include "grammar.h"
2574#undef nc
2575#undef c
2576#undef DEF_RULE
2577};
2578
Damiend99b0522013-12-21 18:17:45 +00002579void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2580 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002581 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002582 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2583 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2584 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002585 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002586 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002587 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002588 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002589 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2590 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2591 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2592 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002593 case MP_PARSE_NODE_TOKEN:
2594 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002595 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002596 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002597 // do nothing
2598 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002599 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002600 }
2601 break;
Damien429d7192013-10-04 19:53:11 +01002602 default: assert(0);
2603 }
2604 } else {
Damiend99b0522013-12-21 18:17:45 +00002605 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002606 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002607 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002608 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002609 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002610#if MICROPY_DEBUG_PRINTERS
2611 mp_parse_node_print(pn, 0);
2612#endif
Damien429d7192013-10-04 19:53:11 +01002613 assert(0);
2614 } else {
2615 f(comp, pns);
2616 }
2617 }
2618}
2619
Damiend99b0522013-12-21 18:17:45 +00002620void 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 +01002621 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002622 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002623 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2624 if (MP_PARSE_NODE_IS_ID(pn)) {
2625 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002626 if (comp->have_bare_star) {
2627 // comes after a bare star, so doesn't count as a parameter
2628 } else {
2629 comp->scope_cur->num_params += 1;
2630 }
Damienb14de212013-10-06 00:28:28 +01002631 } else {
Damiend99b0522013-12-21 18:17:45 +00002632 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2633 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2634 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2635 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002636 //int node_index = 1; unused
2637 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002638 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002639 // this parameter has an annotation
2640 pn_annotation = pns->nodes[1];
2641 }
2642 //node_index = 2; unused
2643 }
2644 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002645 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002646 // this parameter has a default value
2647 if (comp->have_bare_star) {
2648 comp->scope_cur->num_dict_params += 1;
2649 } else {
2650 comp->scope_cur->num_default_params += 1;
2651 }
2652 }
2653 */
2654 if (comp->have_bare_star) {
2655 // comes after a bare star, so doesn't count as a parameter
2656 } else {
2657 comp->scope_cur->num_params += 1;
2658 }
Damiend99b0522013-12-21 18:17:45 +00002659 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2660 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002661 // bare star
2662 // TODO see http://www.python.org/dev/peps/pep-3102/
2663 comp->have_bare_star = true;
2664 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002665 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002666 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002667 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002668 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2669 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002670 // named star with annotation
Damien George8725f8f2014-02-15 19:33:11 +00002671 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002672 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2673 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002674 pn_annotation = pns->nodes[1];
2675 } else {
2676 // shouldn't happen
2677 assert(0);
2678 }
Damiend99b0522013-12-21 18:17:45 +00002679 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2680 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2681 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002682 // this parameter has an annotation
2683 pn_annotation = pns->nodes[1];
2684 }
Damien George8725f8f2014-02-15 19:33:11 +00002685 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002686 } else {
Damienb14de212013-10-06 00:28:28 +01002687 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002688 assert(0);
2689 }
Damien429d7192013-10-04 19:53:11 +01002690 }
2691
2692 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002693 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002694 // TODO this parameter has an annotation
2695 }
2696 bool added;
2697 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2698 if (!added) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002699 compile_syntax_error(comp, "?same name used for parameter");
Damien429d7192013-10-04 19:53:11 +01002700 return;
2701 }
2702 id_info->param = true;
2703 id_info->kind = ID_INFO_KIND_LOCAL;
2704 }
2705}
2706
Damiend99b0522013-12-21 18:17:45 +00002707void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002708 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2709}
2710
Damiend99b0522013-12-21 18:17:45 +00002711void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002712 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2713}
2714
Damiend99b0522013-12-21 18:17:45 +00002715void 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 +01002716 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002717 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002718 // no more nested if/for; compile inner expression
2719 compile_node(comp, pn_inner_expr);
2720 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002721 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002722 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002723 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002724 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002725 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002726 } else {
2727 EMIT(yield_value);
2728 EMIT(pop_top);
2729 }
Damiend99b0522013-12-21 18:17:45 +00002730 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002731 // if condition
Damiend99b0522013-12-21 18:17:45 +00002732 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002733 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2734 pn_iter = pns_comp_if->nodes[1];
2735 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002736 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002737 // for loop
Damiend99b0522013-12-21 18:17:45 +00002738 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002739 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002740 int l_end2 = comp_next_label(comp);
2741 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002742 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002743 EMIT_ARG(label_assign, l_top2);
2744 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002745 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2746 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 +00002747 EMIT_ARG(jump, l_top2);
2748 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002749 EMIT(for_iter_end);
2750 } else {
2751 // shouldn't happen
2752 assert(0);
2753 }
2754}
2755
Damiend99b0522013-12-21 18:17:45 +00002756void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002757 // see http://www.python.org/dev/peps/pep-0257/
2758
2759 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002760 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002761 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002762 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002763 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002764 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2765 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002766 for (int i = 0; i < num_nodes; i++) {
2767 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002768 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 +00002769 // not a newline, so this is the first statement; finish search
2770 break;
2771 }
2772 }
2773 // 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 +00002774 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002775 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002776 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002777 } else {
2778 return;
2779 }
2780
2781 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002782 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2783 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2784 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2785 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2786 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002787 compile_node(comp, pns->nodes[0]); // a doc string
2788 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002789 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002790 }
2791 }
2792 }
2793}
2794
2795void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2796 comp->pass = pass;
2797 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002798 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002799 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002800
2801 if (comp->pass == PASS_1) {
2802 scope->stack_size = 0;
2803 }
2804
Damien5ac1b2e2013-10-18 19:58:12 +01002805#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002806 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002807 scope_print_info(scope);
2808 }
Damien5ac1b2e2013-10-18 19:58:12 +01002809#endif
Damien429d7192013-10-04 19:53:11 +01002810
2811 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002812 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2813 assert(scope->kind == SCOPE_MODULE);
2814 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2815 compile_node(comp, pns->nodes[0]); // compile the expression
2816 EMIT(return_value);
2817 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002818 if (!comp->is_repl) {
2819 check_for_doc_string(comp, scope->pn);
2820 }
Damien429d7192013-10-04 19:53:11 +01002821 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002822 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002823 EMIT(return_value);
2824 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002825 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2826 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2827 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002828
2829 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002830 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002831 if (comp->pass == PASS_1) {
2832 comp->have_bare_star = false;
2833 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2834 }
2835
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02002836 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01002837
2838 compile_node(comp, pns->nodes[3]); // 3 is function body
2839 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002840 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002841 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002842 EMIT(return_value);
2843 }
2844 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002845 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2846 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2847 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002848
2849 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002850 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002851 if (comp->pass == PASS_1) {
2852 comp->have_bare_star = false;
2853 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2854 }
2855
2856 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2857 EMIT(return_value);
2858 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2859 // a bit of a hack at the moment
2860
Damiend99b0522013-12-21 18:17:45 +00002861 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2862 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2863 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2864 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2865 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002866
Damien George55baff42014-01-21 21:40:13 +00002867 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01002868 if (comp->pass == PASS_1) {
2869 bool added;
2870 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2871 assert(added);
2872 id_info->kind = ID_INFO_KIND_LOCAL;
2873 scope->num_params = 1;
2874 }
2875
2876 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002877 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01002878 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002879 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01002880 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002881 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01002882 }
2883
Damienb05d7072013-10-05 13:37:10 +01002884 int l_end = comp_next_label(comp);
2885 int l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002886 EMIT_ARG(load_id, qstr_arg);
2887 EMIT_ARG(label_assign, l_top);
2888 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01002889 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2890 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00002891 EMIT_ARG(jump, l_top);
2892 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002893 EMIT(for_iter_end);
2894
2895 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00002896 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002897 }
2898 EMIT(return_value);
2899 } else {
2900 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002901 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2902 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2903 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01002904
2905 if (comp->pass == PASS_1) {
2906 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002907 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01002908 assert(added);
2909 id_info->kind = ID_INFO_KIND_LOCAL;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002910 id_info = scope_find_or_add_id(scope, MP_QSTR___locals__, &added);
Damien429d7192013-10-04 19:53:11 +01002911 assert(added);
2912 id_info->kind = ID_INFO_KIND_LOCAL;
2913 id_info->param = true;
2914 scope->num_params = 1; // __locals__ is the parameter
2915 }
2916
Damien Georgeb9791222014-01-23 00:34:21 +00002917 EMIT_ARG(load_id, MP_QSTR___locals__);
Damien429d7192013-10-04 19:53:11 +01002918 EMIT(store_locals);
Damien Georgeb9791222014-01-23 00:34:21 +00002919 EMIT_ARG(load_id, MP_QSTR___name__);
2920 EMIT_ARG(store_id, MP_QSTR___module__);
2921 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
2922 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01002923
2924 check_for_doc_string(comp, pns->nodes[2]);
2925 compile_node(comp, pns->nodes[2]); // 2 is class body
2926
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002927 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01002928 assert(id != NULL);
2929 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00002930 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002931 } else {
Damien George6baf76e2013-12-30 22:32:17 +00002932#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00002933 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002934#else
Damien George35e2a4e2014-02-05 00:51:47 +00002935 EMIT_ARG(load_fast, MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00002936#endif
Damien429d7192013-10-04 19:53:11 +01002937 }
2938 EMIT(return_value);
2939 }
2940
Damien415eb6f2013-10-05 12:19:06 +01002941 EMIT(end_pass);
Damien826005c2013-10-05 23:17:28 +01002942}
2943
2944void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2945 comp->pass = pass;
2946 comp->scope_cur = scope;
2947 comp->next_label = 1;
2948
2949 if (scope->kind != SCOPE_FUNCTION) {
2950 printf("Error: inline assembler must be a function\n");
2951 return;
2952 }
2953
Damiena2f2f7d2013-10-06 00:14:13 +01002954 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00002955 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01002956 }
2957
Damien826005c2013-10-05 23:17:28 +01002958 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00002959 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2960 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2961 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01002962
Damiend99b0522013-12-21 18:17:45 +00002963 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01002964
Damiena2f2f7d2013-10-06 00:14:13 +01002965 // parameters are in pns->nodes[1]
2966 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00002967 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01002968 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00002969 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01002970 }
2971
Damiend99b0522013-12-21 18:17:45 +00002972 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01002973
Damiend99b0522013-12-21 18:17:45 +00002974 mp_parse_node_t pn_body = pns->nodes[3]; // body
2975 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01002976 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
2977
Damien Georgecbd2f742014-01-19 11:48:48 +00002978 /*
Damien826005c2013-10-05 23:17:28 +01002979 if (comp->pass == PASS_3) {
2980 //printf("----\n");
2981 scope_print_info(scope);
2982 }
Damien Georgecbd2f742014-01-19 11:48:48 +00002983 */
Damien826005c2013-10-05 23:17:28 +01002984
2985 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00002986 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
2987 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
2988 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
2989 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
2990 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
2991 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
2992 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
2993 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
2994 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
2995 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
2996 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
2997 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
2998 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01002999 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3000
3001 // emit instructions
3002 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00003003 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgef41fdd02014-03-03 23:19:11 +00003004 compile_syntax_error(comp, "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003005 return;
3006 }
3007 int lab = comp_next_label(comp);
3008 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003009 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003010 }
3011 } else {
3012 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003013 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003014 }
3015 }
3016 }
3017
3018 if (comp->pass > PASS_1) {
3019 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01003020 }
Damien429d7192013-10-04 19:53:11 +01003021}
3022
3023void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3024 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003025 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003026 scope->num_locals = 0;
3027 for (int i = 0; i < scope->id_info_len; i++) {
3028 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003029 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003030 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3031 continue;
3032 }
3033 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3034 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3035 }
Damien9ecbcff2013-12-11 00:41:43 +00003036 // note: params always count for 1 local, even if they are a cell
Damien429d7192013-10-04 19:53:11 +01003037 if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
3038 id->local_num = scope->num_locals;
3039 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003040 }
3041 }
3042
3043 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003044#if MICROPY_EMIT_CPYTHON
3045 int num_cell = 0;
3046#endif
Damien9ecbcff2013-12-11 00:41:43 +00003047 for (int i = 0; i < scope->id_info_len; i++) {
3048 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003049#if MICROPY_EMIT_CPYTHON
3050 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003051 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003052 id->local_num = num_cell;
3053 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003054 }
Damien George6baf76e2013-12-30 22:32:17 +00003055#else
3056 // in Micro Python the cells come right after the fast locals
3057 // parameters are not counted here, since they remain at the start
3058 // of the locals, even if they are cell vars
3059 if (!id->param && id->kind == ID_INFO_KIND_CELL) {
3060 id->local_num = scope->num_locals;
3061 scope->num_locals += 1;
3062 }
3063#endif
Damien9ecbcff2013-12-11 00:41:43 +00003064 }
Damien9ecbcff2013-12-11 00:41:43 +00003065
3066 // compute the index of free vars (freevars[idx] in CPython)
3067 // make sure they are in the order of the parent scope
3068 if (scope->parent != NULL) {
3069 int num_free = 0;
3070 for (int i = 0; i < scope->parent->id_info_len; i++) {
3071 id_info_t *id = &scope->parent->id_info[i];
3072 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3073 for (int j = 0; j < scope->id_info_len; j++) {
3074 id_info_t *id2 = &scope->id_info[j];
3075 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +00003076 assert(!id2->param); // free vars should not be params
3077#if MICROPY_EMIT_CPYTHON
3078 // in CPython the frees are numbered after the cells
3079 id2->local_num = num_cell + num_free;
3080#else
3081 // in Micro Python the frees come first, before the params
3082 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003083#endif
3084 num_free += 1;
3085 }
3086 }
3087 }
Damien429d7192013-10-04 19:53:11 +01003088 }
Damien George6baf76e2013-12-30 22:32:17 +00003089#if !MICROPY_EMIT_CPYTHON
3090 // in Micro Python shift all other locals after the free locals
3091 if (num_free > 0) {
3092 for (int i = 0; i < scope->id_info_len; i++) {
3093 id_info_t *id = &scope->id_info[i];
3094 if (id->param || id->kind != ID_INFO_KIND_FREE) {
3095 id->local_num += num_free;
3096 }
3097 }
3098 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3099 scope->num_locals += num_free;
3100 }
3101#endif
Damien429d7192013-10-04 19:53:11 +01003102 }
3103
Damien George8725f8f2014-02-15 19:33:11 +00003104 // compute scope_flags
3105 //scope->scope_flags = 0; since we set some things in parameters
Damien429d7192013-10-04 19:53:11 +01003106 if (scope->kind != SCOPE_MODULE) {
Damien George8725f8f2014-02-15 19:33:11 +00003107 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien429d7192013-10-04 19:53:11 +01003108 }
3109 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) {
3110 assert(scope->parent != NULL);
Damien George8725f8f2014-02-15 19:33:11 +00003111 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien429d7192013-10-04 19:53:11 +01003112
3113 // TODO possibly other ways it can be nested
Damien George08d07552014-01-29 18:58:52 +00003114 // Note that we don't actually use this information at the moment (for CPython compat only)
3115 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 +00003116 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003117 }
3118 }
3119 int num_free = 0;
3120 for (int i = 0; i < scope->id_info_len; i++) {
3121 id_info_t *id = &scope->id_info[i];
3122 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3123 num_free += 1;
3124 }
3125 }
3126 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003127 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003128 }
3129}
3130
Damien George08335002014-01-18 23:24:36 +00003131mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
Damien429d7192013-10-04 19:53:11 +01003132 compiler_t *comp = m_new(compiler_t, 1);
3133
Damien Georgecbd2f742014-01-19 11:48:48 +00003134 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003135 comp->is_repl = is_repl;
3136 comp->had_error = false;
3137
Damien429d7192013-10-04 19:53:11 +01003138 comp->break_label = 0;
3139 comp->continue_label = 0;
Damien Georgecbddb272014-02-01 20:08:18 +00003140 comp->break_continue_except_level = 0;
3141 comp->cur_except_level = 0;
3142
Damien George35e2a4e2014-02-05 00:51:47 +00003143 comp->func_arg_is_super = false;
3144
Damien429d7192013-10-04 19:53:11 +01003145 comp->scope_head = NULL;
3146 comp->scope_cur = NULL;
3147
Damien826005c2013-10-05 23:17:28 +01003148 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003149 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003150
3151 // set the outer scope
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003152 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE);
Damien429d7192013-10-04 19:53:11 +01003153
Damien826005c2013-10-05 23:17:28 +01003154 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003155 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003156 comp->emit_method_table = &emit_pass1_method_table;
3157 comp->emit_inline_asm = NULL;
3158 comp->emit_inline_asm_method_table = NULL;
3159 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003160 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003161 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003162#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003163 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003164 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003165#endif
Damien826005c2013-10-05 23:17:28 +01003166 } else {
3167 compile_scope(comp, s, PASS_1);
3168 }
3169
3170 // update maximim number of labels needed
3171 if (comp->next_label > max_num_labels) {
3172 max_num_labels = comp->next_label;
3173 }
Damien429d7192013-10-04 19:53:11 +01003174 }
3175
Damien826005c2013-10-05 23:17:28 +01003176 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003177 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003178 compile_scope_compute_things(comp, s);
3179 }
3180
Damien826005c2013-10-05 23:17:28 +01003181 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003182 emit_pass1_free(comp->emit);
3183
Damien826005c2013-10-05 23:17:28 +01003184 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003185#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003186 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003187#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003188 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003189#endif
Damien3ef4abb2013-10-12 16:53:13 +01003190#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003191 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003192#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003193#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003194 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003195 if (false) {
3196 // dummy
3197
Damien3ef4abb2013-10-12 16:53:13 +01003198#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003199 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
3200 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003201 if (emit_inline_thumb == NULL) {
3202 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3203 }
3204 comp->emit = NULL;
3205 comp->emit_method_table = NULL;
3206 comp->emit_inline_asm = emit_inline_thumb;
3207 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3208 compile_scope_inline_asm(comp, s, PASS_2);
3209 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003210#endif
3211
Damien826005c2013-10-05 23:17:28 +01003212 } else {
Damienc025ebb2013-10-12 14:30:21 +01003213
3214 // choose the emit type
3215
Damien3ef4abb2013-10-12 16:53:13 +01003216#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003217 comp->emit = emit_cpython_new(max_num_labels);
3218 comp->emit_method_table = &emit_cpython_method_table;
3219#else
Damien826005c2013-10-05 23:17:28 +01003220 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003221
3222#if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003223 case EMIT_OPT_NATIVE_PYTHON:
Damien3410be82013-10-07 23:09:10 +01003224 case EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003225#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003226 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003227 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003228 }
Damien13ed3a62013-10-08 09:05:10 +01003229 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003230#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003231 if (emit_native == NULL) {
3232 emit_native = emit_native_thumb_new(max_num_labels);
3233 }
3234 comp->emit_method_table = &emit_native_thumb_method_table;
3235#endif
3236 comp->emit = emit_native;
Damien3410be82013-10-07 23:09:10 +01003237 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003238 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003239#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003240
Damien826005c2013-10-05 23:17:28 +01003241 default:
3242 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003243 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003244 }
3245 comp->emit = emit_bc;
3246 comp->emit_method_table = &emit_bc_method_table;
3247 break;
3248 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003249#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003250
3251 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003252 compile_scope(comp, s, PASS_2);
3253 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003254 }
Damien429d7192013-10-04 19:53:11 +01003255 }
3256
Damien George41d02b62014-01-24 22:42:28 +00003257 // free the emitters
3258#if !MICROPY_EMIT_CPYTHON
3259 if (emit_bc != NULL) {
3260 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003261 }
Damien George41d02b62014-01-24 22:42:28 +00003262#if MICROPY_EMIT_NATIVE
3263 if (emit_native != NULL) {
3264#if MICROPY_EMIT_X64
3265 emit_native_x64_free(emit_native);
3266#elif MICROPY_EMIT_THUMB
3267 emit_native_thumb_free(emit_native);
3268#endif
3269 }
3270#endif
3271#if MICROPY_EMIT_INLINE_THUMB
3272 if (emit_inline_thumb != NULL) {
3273 emit_inline_thumb_free(emit_inline_thumb);
3274 }
3275#endif
3276#endif // !MICROPY_EMIT_CPYTHON
3277
3278 // free the scopes
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003279 uint unique_code_id = module_scope->unique_code_id;
3280 for (scope_t *s = module_scope; s;) {
3281 scope_t *next = s->next;
3282 scope_free(s);
3283 s = next;
3284 }
Damien5ac1b2e2013-10-18 19:58:12 +01003285
Damien George41d02b62014-01-24 22:42:28 +00003286 // free the compiler
3287 bool had_error = comp->had_error;
3288 m_del_obj(compiler_t, comp);
3289
Damien George1fb03172014-01-03 14:22:03 +00003290 if (had_error) {
3291 // TODO return a proper error message
3292 return mp_const_none;
3293 } else {
3294#if MICROPY_EMIT_CPYTHON
3295 // can't create code, so just return true
Damien George41d02b62014-01-24 22:42:28 +00003296 (void)unique_code_id; // to suppress warning that unique_code_id is unused
Damien George1fb03172014-01-03 14:22:03 +00003297 return mp_const_true;
3298#else
3299 // return function that executes the outer module
Paul Sokolovsky90750022014-02-01 15:05:04 +02003300 return rt_make_function_from_id(unique_code_id, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003301#endif
3302 }
Damien429d7192013-10-04 19:53:11 +01003303}