blob: 6a87236b9d65506ce82ac9fe2251724560f99946 [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <unistd.h>
2#include <stdlib.h>
3#include <stdint.h>
4#include <stdio.h>
5#include <string.h>
6#include <assert.h>
7
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"
Damien429d7192013-10-04 19:53:11 +010019
20// TODO need to mangle __attr names
21
Damience89a212013-10-15 22:25:17 +010022#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_THUMB)
23
Damien429d7192013-10-04 19:53:11 +010024typedef enum {
25 PN_none = 0,
Damien George00208ce2014-01-23 00:00:53 +000026#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien429d7192013-10-04 19:53:11 +010027#include "grammar.h"
28#undef DEF_RULE
29 PN_maximum_number_of,
30} pn_kind_t;
31
Damien Georgeb9791222014-01-23 00:34:21 +000032#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
33#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
34#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
35#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 +010036
Damien6cdd3af2013-10-05 18:08:26 +010037#define EMIT_OPT_NONE (0)
38#define EMIT_OPT_BYTE_CODE (1)
39#define EMIT_OPT_NATIVE_PYTHON (2)
Damien7af3d192013-10-07 00:02:49 +010040#define EMIT_OPT_VIPER (3)
41#define EMIT_OPT_ASM_THUMB (4)
Damien6cdd3af2013-10-05 18:08:26 +010042
Damien429d7192013-10-04 19:53:11 +010043typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000044 qstr source_file;
Damien5ac1b2e2013-10-18 19:58:12 +010045 bool is_repl;
Damien429d7192013-10-04 19:53:11 +010046 pass_kind_t pass;
Damien5ac1b2e2013-10-18 19:58:12 +010047 bool had_error; // try to keep compiler clean from nlr
Damien429d7192013-10-04 19:53:11 +010048
Damienb05d7072013-10-05 13:37:10 +010049 int next_label;
Damienb05d7072013-10-05 13:37:10 +010050
Damien429d7192013-10-04 19:53:11 +010051 int break_label;
52 int continue_label;
Damien Georgecbddb272014-02-01 20:08:18 +000053 int break_continue_except_level;
54 int cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien429d7192013-10-04 19:53:11 +010055
56 int n_arg_keyword;
57 bool have_star_arg;
58 bool have_dbl_star_arg;
59 bool have_bare_star;
60 int param_pass;
61 int param_pass_num_dict_params;
62 int param_pass_num_default_params;
63
Damien George35e2a4e2014-02-05 00:51:47 +000064 bool func_arg_is_super; // used to compile special case of super() function call
65
Damien429d7192013-10-04 19:53:11 +010066 scope_t *scope_head;
67 scope_t *scope_cur;
68
Damien6cdd3af2013-10-05 18:08:26 +010069 emit_t *emit; // current emitter
70 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010071
72 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
73 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 +010074} compiler_t;
75
Damiend99b0522013-12-21 18:17:45 +000076mp_parse_node_t fold_constants(mp_parse_node_t pn) {
77 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
78 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
79 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +010080
81 // fold arguments first
82 for (int i = 0; i < n; i++) {
83 pns->nodes[i] = fold_constants(pns->nodes[i]);
84 }
85
Damiend99b0522013-12-21 18:17:45 +000086 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +010087 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +000088 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 +020089 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
90 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +000091 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien3ef4abb2013-10-12 16:53:13 +010092#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +010093 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +000094 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
Damien0efb3a12013-10-12 16:16:56 +010095#endif
Damiend99b0522013-12-21 18:17:45 +000096 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
97 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +010098 } else {
99 // shouldn't happen
100 assert(0);
101 }
102 }
103 break;
104
105 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100106 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000107 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 +0200108 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
109 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damien0efb3a12013-10-12 16:16:56 +0100110 machine_int_t res;
Damiend99b0522013-12-21 18:17:45 +0000111 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100112 res = arg0 + arg1;
Damiend99b0522013-12-21 18:17:45 +0000113 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100114 res = arg0 - arg1;
Damien429d7192013-10-04 19:53:11 +0100115 } else {
116 // shouldn't happen
117 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100118 res = 0;
119 }
Paul Sokolovskybbf0e2f2014-02-21 02:04:32 +0200120 if (MP_PARSE_FITS_SMALL_INT(res)) {
Damiend99b0522013-12-21 18:17:45 +0000121 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, res);
Damien429d7192013-10-04 19:53:11 +0100122 }
123 }
124 break;
125
126 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000127 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 +0200128 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
129 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000130 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien3ef4abb2013-10-12 16:53:13 +0100131#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100132 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000133 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 * arg1);
Damien0efb3a12013-10-12 16:16:56 +0100134#endif
Damiend99b0522013-12-21 18:17:45 +0000135 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien429d7192013-10-04 19:53:11 +0100136 ; // pass
Damiend99b0522013-12-21 18:17:45 +0000137 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien0efb3a12013-10-12 16:16:56 +0100138 // XXX implement this properly as Python's % operator acts differently to C's
Damiend99b0522013-12-21 18:17:45 +0000139 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 % arg1);
140 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Damien0efb3a12013-10-12 16:16:56 +0100141 // XXX implement this properly as Python's // operator acts differently to C's
Damiend99b0522013-12-21 18:17:45 +0000142 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 / arg1);
Damien429d7192013-10-04 19:53:11 +0100143 } else {
144 // shouldn't happen
145 assert(0);
146 }
147 }
148 break;
149
150 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000151 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200152 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000153 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
154 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
155 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
156 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
157 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
158 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100159 } else {
160 // shouldn't happen
161 assert(0);
162 }
163 }
164 break;
165
Damien3ef4abb2013-10-12 16:53:13 +0100166#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +0100167 case PN_power:
Damien0efb3a12013-10-12 16:16:56 +0100168 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000169 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])) {
170 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
171 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200172 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100173 if (power >= 0) {
174 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200175 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100176 for (; power > 0; power--) {
177 ans *= base;
178 }
Damiend99b0522013-12-21 18:17:45 +0000179 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100180 }
181 }
182 }
183 break;
Damien0efb3a12013-10-12 16:16:56 +0100184#endif
Damien429d7192013-10-04 19:53:11 +0100185 }
186 }
187
188 return pn;
189}
190
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200191STATIC 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 +0000192void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100193
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200194STATIC int comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100195 return comp->next_label++;
196}
197
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200198STATIC 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 +0000199 scope_t *scope = scope_new(kind, pn, comp->source_file, rt_get_unique_code_id(), emit_options);
Damien429d7192013-10-04 19:53:11 +0100200 scope->parent = comp->scope_cur;
201 scope->next = NULL;
202 if (comp->scope_head == NULL) {
203 comp->scope_head = scope;
204 } else {
205 scope_t *s = comp->scope_head;
206 while (s->next != NULL) {
207 s = s->next;
208 }
209 s->next = scope;
210 }
211 return scope;
212}
213
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200214STATIC int list_len(mp_parse_node_t pn, int pn_kind) {
Damiend99b0522013-12-21 18:17:45 +0000215 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100216 return 0;
Damiend99b0522013-12-21 18:17:45 +0000217 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100218 return 1;
219 } else {
Damiend99b0522013-12-21 18:17:45 +0000220 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
221 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100222 return 1;
223 } else {
Damiend99b0522013-12-21 18:17:45 +0000224 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100225 }
226 }
227}
228
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200229STATIC 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 +0000230 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
231 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
232 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100233 for (int i = 0; i < num_nodes; i++) {
234 f(comp, pns->nodes[i]);
235 }
Damiend99b0522013-12-21 18:17:45 +0000236 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100237 f(comp, pn);
238 }
239}
240
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200241STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000242 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100243 *nodes = NULL;
244 return 0;
Damiend99b0522013-12-21 18:17:45 +0000245 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100246 *nodes = pn;
247 return 1;
248 } else {
Damiend99b0522013-12-21 18:17:45 +0000249 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
250 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100251 *nodes = pn;
252 return 1;
253 } else {
254 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000255 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100256 }
257 }
258}
259
Damiend99b0522013-12-21 18:17:45 +0000260void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100261}
262
Damiend99b0522013-12-21 18:17:45 +0000263void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
264 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100265 for (int i = 0; i < num_nodes; i++) {
266 compile_node(comp, pns->nodes[i]);
267 }
268}
269
Damien3ef4abb2013-10-12 16:53:13 +0100270#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200271STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000272 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100273 return false;
274 }
Damiend99b0522013-12-21 18:17:45 +0000275 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100276 return false;
277 }
278 return true;
279}
280
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200281STATIC void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
Damien George55baff42014-01-21 21:40:13 +0000282 uint len;
283 const byte *str = qstr_data(qstr, &len);
Damien02f89412013-12-12 15:13:36 +0000284 bool has_single_quote = false;
285 bool has_double_quote = false;
286 for (int i = 0; i < len; i++) {
287 if (str[i] == '\'') {
288 has_single_quote = true;
289 } else if (str[i] == '"') {
290 has_double_quote = true;
291 }
292 }
293 if (bytes) {
294 vstr_printf(vstr, "b");
295 }
296 bool quote_single = false;
297 if (has_single_quote && !has_double_quote) {
298 vstr_printf(vstr, "\"");
299 } else {
300 quote_single = true;
301 vstr_printf(vstr, "'");
302 }
303 for (int i = 0; i < len; i++) {
304 if (str[i] == '\n') {
305 vstr_printf(vstr, "\\n");
306 } else if (str[i] == '\\') {
307 vstr_printf(vstr, "\\\\");
308 } else if (str[i] == '\'' && quote_single) {
309 vstr_printf(vstr, "\\'");
310 } else {
311 vstr_printf(vstr, "%c", str[i]);
312 }
313 }
314 if (has_single_quote && !has_double_quote) {
315 vstr_printf(vstr, "\"");
316 } else {
317 vstr_printf(vstr, "'");
318 }
319}
320
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200321STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damiend99b0522013-12-21 18:17:45 +0000322 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200323 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
324 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
325 return;
326 }
327
Damiend99b0522013-12-21 18:17:45 +0000328 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
329 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
330 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000331 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
332 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
333 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
334 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
335 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100336 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000337 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
338 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
339 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien429d7192013-10-04 19:53:11 +0100340 default: assert(0);
341 }
342 break;
343 default: assert(0);
344 }
345}
346
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200347STATIC 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 +0100348 int n = 0;
349 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000350 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100351 }
352 int total = n;
353 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000354 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100355 total += 1;
Damien3a205172013-10-12 15:01:56 +0100356 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100357 is_const = false;
358 }
359 }
360 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100361 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100362 is_const = false;
363 break;
364 }
365 }
366 if (total > 0 && is_const) {
367 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000368 vstr_t *vstr = vstr_new();
369 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000370 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000371 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100372 need_comma = true;
373 }
374 for (int i = 0; i < n; i++) {
375 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000376 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100377 }
Damien02f89412013-12-12 15:13:36 +0000378 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100379 need_comma = true;
380 }
381 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000382 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100383 } else {
Damien02f89412013-12-12 15:13:36 +0000384 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100385 }
Damien Georgeb9791222014-01-23 00:34:21 +0000386 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000387 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100388 } else {
Damiend99b0522013-12-21 18:17:45 +0000389 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100390 compile_node(comp, pn);
391 }
392 for (int i = 0; i < n; i++) {
393 compile_node(comp, pns_list->nodes[i]);
394 }
Damien Georgeb9791222014-01-23 00:34:21 +0000395 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100396 }
397}
Damien3a205172013-10-12 15:01:56 +0100398#endif
399
400// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damiend99b0522013-12-21 18:17:45 +0000401void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100402#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100403 cpython_c_tuple(comp, pn, pns_list);
404#else
405 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000406 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100407 compile_node(comp, pn);
408 total += 1;
409 }
410 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000411 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100412 for (int i = 0; i < n; i++) {
413 compile_node(comp, pns_list->nodes[i]);
414 }
415 total += n;
416 }
Damien Georgeb9791222014-01-23 00:34:21 +0000417 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100418#endif
419}
Damien429d7192013-10-04 19:53:11 +0100420
Damiend99b0522013-12-21 18:17:45 +0000421void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100422 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000423 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100424}
425
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200426STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000427 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200428 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100429}
430
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200431STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200432 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 +0100433}
434
Damien3ef4abb2013-10-12 16:53:13 +0100435#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100436// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200437STATIC 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 +0100438 if (node_is_const_false(pn)) {
439 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000440 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100441 }
442 return;
443 } else if (node_is_const_true(pn)) {
444 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000445 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100446 }
447 return;
Damiend99b0522013-12-21 18:17:45 +0000448 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
449 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
450 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
451 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100452 if (jump_if == false) {
Damienb05d7072013-10-05 13:37:10 +0100453 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100454 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100455 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100456 }
Damien3a205172013-10-12 15:01:56 +0100457 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000458 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100459 } else {
460 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100461 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100462 }
463 }
464 return;
Damiend99b0522013-12-21 18:17:45 +0000465 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100466 if (jump_if == false) {
467 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100468 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100469 }
470 } else {
Damienb05d7072013-10-05 13:37:10 +0100471 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100472 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100473 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100474 }
Damien3a205172013-10-12 15:01:56 +0100475 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000476 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100477 }
478 return;
Damiend99b0522013-12-21 18:17:45 +0000479 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100480 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100481 return;
482 }
483 }
484
485 // nothing special, fall back to default compiling for node and jump
486 compile_node(comp, pn);
487 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000488 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100489 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000490 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100491 }
492}
Damien3a205172013-10-12 15:01:56 +0100493#endif
Damien429d7192013-10-04 19:53:11 +0100494
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200495STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100496#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100497 cpython_c_if_cond(comp, pn, jump_if, label, false);
498#else
499 if (node_is_const_false(pn)) {
500 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000501 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100502 }
503 return;
504 } else if (node_is_const_true(pn)) {
505 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000506 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100507 }
508 return;
Damiend99b0522013-12-21 18:17:45 +0000509 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
510 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
511 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
512 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100513 if (jump_if == false) {
514 int label2 = comp_next_label(comp);
515 for (int i = 0; i < n - 1; i++) {
516 c_if_cond(comp, pns->nodes[i], true, label2);
517 }
518 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000519 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100520 } else {
521 for (int i = 0; i < n; i++) {
522 c_if_cond(comp, pns->nodes[i], true, label);
523 }
524 }
525 return;
Damiend99b0522013-12-21 18:17:45 +0000526 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100527 if (jump_if == false) {
528 for (int i = 0; i < n; i++) {
529 c_if_cond(comp, pns->nodes[i], false, label);
530 }
531 } else {
532 int label2 = comp_next_label(comp);
533 for (int i = 0; i < n - 1; i++) {
534 c_if_cond(comp, pns->nodes[i], false, label2);
535 }
536 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000537 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100538 }
539 return;
Damiend99b0522013-12-21 18:17:45 +0000540 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100541 c_if_cond(comp, pns->nodes[0], !jump_if, label);
542 return;
543 }
544 }
545
546 // nothing special, fall back to default compiling for node and jump
547 compile_node(comp, pn);
548 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000549 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100550 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000551 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100552 }
553#endif
Damien429d7192013-10-04 19:53:11 +0100554}
555
556typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000557void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100558
Damiend99b0522013-12-21 18:17:45 +0000559void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100560 if (assign_kind != ASSIGN_AUG_STORE) {
561 compile_node(comp, pns->nodes[0]);
562 }
563
Damiend99b0522013-12-21 18:17:45 +0000564 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
565 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
566 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
567 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100568 if (assign_kind != ASSIGN_AUG_STORE) {
569 for (int i = 0; i < n - 1; i++) {
570 compile_node(comp, pns1->nodes[i]);
571 }
572 }
Damiend99b0522013-12-21 18:17:45 +0000573 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
574 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100575 }
Damiend99b0522013-12-21 18:17:45 +0000576 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien429d7192013-10-04 19:53:11 +0100577 printf("SyntaxError: can't assign to function call\n");
578 return;
Damiend99b0522013-12-21 18:17:45 +0000579 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100580 if (assign_kind == ASSIGN_AUG_STORE) {
581 EMIT(rot_three);
582 EMIT(store_subscr);
583 } else {
584 compile_node(comp, pns1->nodes[0]);
585 if (assign_kind == ASSIGN_AUG_LOAD) {
586 EMIT(dup_top_two);
Damien Georgeb9791222014-01-23 00:34:21 +0000587 EMIT_ARG(binary_op, RT_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100588 } else {
589 EMIT(store_subscr);
590 }
591 }
Damiend99b0522013-12-21 18:17:45 +0000592 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
593 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100594 if (assign_kind == ASSIGN_AUG_LOAD) {
595 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000596 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100597 } else {
598 if (assign_kind == ASSIGN_AUG_STORE) {
599 EMIT(rot_two);
600 }
Damien Georgeb9791222014-01-23 00:34:21 +0000601 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100602 }
603 } else {
604 // shouldn't happen
605 assert(0);
606 }
607 } else {
608 // shouldn't happen
609 assert(0);
610 }
611
Damiend99b0522013-12-21 18:17:45 +0000612 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +0100613 // SyntaxError, cannot assign
614 assert(0);
615 }
616}
617
Damiend99b0522013-12-21 18:17:45 +0000618void c_assign_tuple(compiler_t *comp, int n, mp_parse_node_t *nodes) {
Damien429d7192013-10-04 19:53:11 +0100619 assert(n >= 0);
620 int have_star_index = -1;
621 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000622 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100623 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000624 EMIT_ARG(unpack_ex, i, n - i - 1);
Damien429d7192013-10-04 19:53:11 +0100625 have_star_index = i;
626 } else {
627 printf("SyntaxError: two starred expressions in assignment\n");
628 return;
629 }
630 }
631 }
632 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000633 EMIT_ARG(unpack_sequence, n);
Damien429d7192013-10-04 19:53:11 +0100634 }
635 for (int i = 0; i < n; i++) {
636 if (i == have_star_index) {
Damiend99b0522013-12-21 18:17:45 +0000637 c_assign(comp, ((mp_parse_node_struct_t*)nodes[i])->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100638 } else {
639 c_assign(comp, nodes[i], ASSIGN_STORE);
640 }
641 }
642}
643
644// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000645void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100646 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000647 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100648 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000649 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
650 if (MP_PARSE_NODE_IS_ID(pn)) {
651 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100652 switch (assign_kind) {
653 case ASSIGN_STORE:
654 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000655 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100656 break;
657 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000658 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100659 break;
660 }
661 } else {
662 printf("SyntaxError: can't assign to literal\n");
663 return;
664 }
665 } else {
Damiend99b0522013-12-21 18:17:45 +0000666 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
667 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100668 case PN_power:
669 // lhs is an index or attribute
670 c_assign_power(comp, pns, assign_kind);
671 break;
672
673 case PN_testlist_star_expr:
674 case PN_exprlist:
675 // lhs is a tuple
676 if (assign_kind != ASSIGN_STORE) {
677 goto bad_aug;
678 }
Damiend99b0522013-12-21 18:17:45 +0000679 c_assign_tuple(comp, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100680 break;
681
682 case PN_atom_paren:
683 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000684 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100685 // empty tuple
686 printf("SyntaxError: can't assign to ()\n");
687 return;
Damiend99b0522013-12-21 18:17:45 +0000688 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
689 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100690 goto testlist_comp;
691 } else {
692 // parenthesis around 1 item, is just that item
693 pn = pns->nodes[0];
694 goto tail_recursion;
695 }
696 break;
697
698 case PN_atom_bracket:
699 // lhs is something in brackets
700 if (assign_kind != ASSIGN_STORE) {
701 goto bad_aug;
702 }
Damiend99b0522013-12-21 18:17:45 +0000703 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100704 // empty list, assignment allowed
705 c_assign_tuple(comp, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000706 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
707 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100708 goto testlist_comp;
709 } else {
710 // brackets around 1 item
711 c_assign_tuple(comp, 1, &pns->nodes[0]);
712 }
713 break;
714
715 default:
Damiend99b0522013-12-21 18:17:45 +0000716 printf("unknown assign, %u\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien429d7192013-10-04 19:53:11 +0100717 assert(0);
718 }
719 return;
720
721 testlist_comp:
722 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000723 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
724 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
725 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100726 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000727 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100728 c_assign_tuple(comp, 1, &pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000729 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100730 // sequence of many items
731 // TODO call c_assign_tuple instead
Damiend99b0522013-12-21 18:17:45 +0000732 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
Damien Georgeb9791222014-01-23 00:34:21 +0000733 EMIT_ARG(unpack_sequence, 1 + n);
Damien429d7192013-10-04 19:53:11 +0100734 c_assign(comp, pns->nodes[0], ASSIGN_STORE);
735 for (int i = 0; i < n; i++) {
736 c_assign(comp, pns2->nodes[i], ASSIGN_STORE);
737 }
Damiend99b0522013-12-21 18:17:45 +0000738 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +0100739 // TODO not implemented
740 assert(0);
741 } else {
742 // sequence with 2 items
743 goto sequence_with_2_items;
744 }
745 } else {
746 // sequence with 2 items
747 sequence_with_2_items:
748 c_assign_tuple(comp, 2, pns->nodes);
749 }
750 return;
751 }
752 return;
753
754 bad_aug:
755 printf("SyntaxError: illegal expression for augmented assignment\n");
756}
757
758// stuff for lambda and comprehensions and generators
759void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_dict_params, int n_default_params) {
760 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000761 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100762 int nfree = 0;
763 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000764 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
765 id_info_t *id = &comp->scope_cur->id_info[i];
766 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
767 for (int j = 0; j < this_scope->id_info_len; j++) {
768 id_info_t *id2 = &this_scope->id_info[j];
769 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000770#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000771 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000772#else
773 // in Micro Python we load closures using LOAD_FAST
Damien Georgeb9791222014-01-23 00:34:21 +0000774 EMIT_ARG(load_fast, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000775#endif
Damien318aec62013-12-10 18:28:17 +0000776 nfree += 1;
777 }
778 }
Damien429d7192013-10-04 19:53:11 +0100779 }
780 }
781 }
782 if (nfree > 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000783 EMIT_ARG(build_tuple, nfree);
Damien429d7192013-10-04 19:53:11 +0100784 }
785
786 // make the function/closure
787 if (nfree == 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000788 EMIT_ARG(make_function, this_scope, n_dict_params, n_default_params);
Damien429d7192013-10-04 19:53:11 +0100789 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000790 EMIT_ARG(make_closure, this_scope, n_dict_params, n_default_params);
Damien429d7192013-10-04 19:53:11 +0100791 }
792}
793
Damiend99b0522013-12-21 18:17:45 +0000794void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
795 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
796 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
797 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +0100798 // this parameter has a default value
799 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
800 if (comp->have_bare_star) {
801 comp->param_pass_num_dict_params += 1;
802 if (comp->param_pass == 1) {
Damien Georgeb9791222014-01-23 00:34:21 +0000803 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100804 compile_node(comp, pns->nodes[2]);
805 }
806 } else {
807 comp->param_pass_num_default_params += 1;
808 if (comp->param_pass == 2) {
809 compile_node(comp, pns->nodes[2]);
810 }
811 }
812 }
Damiend99b0522013-12-21 18:17:45 +0000813 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
814 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
815 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100816 // bare star
817 comp->have_bare_star = true;
818 }
819 }
820}
821
822// leaves function object on stack
823// returns function name
Damiend99b0522013-12-21 18:17:45 +0000824qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100825 if (comp->pass == PASS_1) {
826 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000827 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100828 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000829 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100830 }
831
832 // save variables (probably don't need to do this, since we can't have nested definitions..?)
833 bool old_have_bare_star = comp->have_bare_star;
834 int old_param_pass = comp->param_pass;
835 int old_param_pass_num_dict_params = comp->param_pass_num_dict_params;
836 int old_param_pass_num_default_params = comp->param_pass_num_default_params;
837
838 // compile default parameters
839 comp->have_bare_star = false;
840 comp->param_pass = 1; // pass 1 does any default parameters after bare star
841 comp->param_pass_num_dict_params = 0;
842 comp->param_pass_num_default_params = 0;
843 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
844 comp->have_bare_star = false;
845 comp->param_pass = 2; // pass 2 does any default parameters before bare star
846 comp->param_pass_num_dict_params = 0;
847 comp->param_pass_num_default_params = 0;
848 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
849
850 // get the scope for this function
851 scope_t *fscope = (scope_t*)pns->nodes[4];
852
853 // make the function
854 close_over_variables_etc(comp, fscope, comp->param_pass_num_dict_params, comp->param_pass_num_default_params);
855
856 // restore variables
857 comp->have_bare_star = old_have_bare_star;
858 comp->param_pass = old_param_pass;
859 comp->param_pass_num_dict_params = old_param_pass_num_dict_params;
860 comp->param_pass_num_default_params = old_param_pass_num_default_params;
861
862 // return its name (the 'f' in "def f(...):")
863 return fscope->simple_name;
864}
865
866// leaves class object on stack
867// returns class name
Damiend99b0522013-12-21 18:17:45 +0000868qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100869 if (comp->pass == PASS_1) {
870 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000871 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100872 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000873 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100874 }
875
876 EMIT(load_build_class);
877
878 // scope for this class
879 scope_t *cscope = (scope_t*)pns->nodes[3];
880
881 // compile the class
882 close_over_variables_etc(comp, cscope, 0, 0);
883
884 // get its name
Damien Georgeb9791222014-01-23 00:34:21 +0000885 EMIT_ARG(load_const_id, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100886
887 // nodes[1] has parent classes, if any
Damien Georgebbcd49a2014-02-06 20:30:16 +0000888 comp->func_arg_is_super = false;
889 compile_trailer_paren_helper(comp, pns->nodes[1], false, 2);
Damien429d7192013-10-04 19:53:11 +0100890
891 // return its name (the 'C' in class C(...):")
892 return cscope->simple_name;
893}
894
Damien6cdd3af2013-10-05 18:08:26 +0100895// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200896STATIC 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 +0000897 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100898 return false;
899 }
900
901 if (name_len != 2) {
902 printf("SyntaxError: invalid micropython decorator\n");
903 return true;
904 }
905
Damiend99b0522013-12-21 18:17:45 +0000906 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000907 if (attr == MP_QSTR_byte_code) {
Damien5ac1b2e2013-10-18 19:58:12 +0100908 *emit_options = EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +0100909#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000910 } else if (attr == MP_QSTR_native) {
Damien6cdd3af2013-10-05 18:08:26 +0100911 *emit_options = EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000912 } else if (attr == MP_QSTR_viper) {
Damien7af3d192013-10-07 00:02:49 +0100913 *emit_options = EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100914#endif
Damien3ef4abb2013-10-12 16:53:13 +0100915#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000916 } else if (attr == MP_QSTR_asm_thumb) {
Damien5bfb7592013-10-05 18:41:24 +0100917 *emit_options = EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +0100918#endif
Damien6cdd3af2013-10-05 18:08:26 +0100919 } else {
Damience89a212013-10-15 22:25:17 +0100920 printf("SyntaxError: invalid micropython decorator '%s'\n", qstr_str(attr));
Damien6cdd3af2013-10-05 18:08:26 +0100921 }
922
923 return true;
924}
925
Damiend99b0522013-12-21 18:17:45 +0000926void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100927 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +0000928 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +0100929 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
930
Damien6cdd3af2013-10-05 18:08:26 +0100931 // inherit emit options for this function/class definition
932 uint emit_options = comp->scope_cur->emit_options;
933
934 // compile each decorator
935 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +0100936 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000937 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
938 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +0100939
940 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +0000941 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +0100942 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
943
944 // check for built-in decorators
945 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
946 // this was a built-in
947 num_built_in_decorators += 1;
948
949 } else {
950 // not a built-in, compile normally
951
952 // compile the decorator function
953 compile_node(comp, name_nodes[0]);
954 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +0000955 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +0000956 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +0100957 }
958
959 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +0000960 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +0100961 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +0000962 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +0100963 compile_node(comp, pns_decorator->nodes[1]);
964 }
Damien429d7192013-10-04 19:53:11 +0100965 }
966 }
967
968 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +0000969 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +0100970 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +0000971 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +0100972 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +0000973 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +0100974 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +0100975 } else {
976 // shouldn't happen
977 assert(0);
978 }
979
980 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +0100981 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +0000982 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +0100983 }
984
985 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +0000986 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +0100987}
988
Damiend99b0522013-12-21 18:17:45 +0000989void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +0100990 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +0100991 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +0000992 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +0100993}
994
Damiend99b0522013-12-21 18:17:45 +0000995void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
996 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +0000997 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +0000998 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
999 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001000
1001 compile_node(comp, pns->nodes[0]); // base of the power node
1002
Damiend99b0522013-12-21 18:17:45 +00001003 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1004 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1005 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1006 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001007 for (int i = 0; i < n - 1; i++) {
1008 compile_node(comp, pns1->nodes[i]);
1009 }
Damiend99b0522013-12-21 18:17:45 +00001010 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1011 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001012 }
Damiend99b0522013-12-21 18:17:45 +00001013 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien429d7192013-10-04 19:53:11 +01001014 // SyntaxError: can't delete a function call
1015 assert(0);
Damiend99b0522013-12-21 18:17:45 +00001016 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001017 compile_node(comp, pns1->nodes[0]);
1018 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001019 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1020 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001021 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001022 } else {
1023 // shouldn't happen
1024 assert(0);
1025 }
1026 } else {
1027 // shouldn't happen
1028 assert(0);
1029 }
1030
Damiend99b0522013-12-21 18:17:45 +00001031 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001032 // SyntaxError, cannot delete
1033 assert(0);
1034 }
Damiend99b0522013-12-21 18:17:45 +00001035 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1036 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1037 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1038 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001039 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1040
Damiend99b0522013-12-21 18:17:45 +00001041 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1042 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1043 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001044 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001045 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001046 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001047 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001048 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001049 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001050 c_del_stmt(comp, pns->nodes[0]);
1051 for (int i = 0; i < n; i++) {
1052 c_del_stmt(comp, pns1->nodes[i]);
1053 }
Damiend99b0522013-12-21 18:17:45 +00001054 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001055 // TODO not implemented; can't del comprehension?
1056 assert(0);
1057 } else {
1058 // sequence with 2 items
1059 goto sequence_with_2_items;
1060 }
1061 } else {
1062 // sequence with 2 items
1063 sequence_with_2_items:
1064 c_del_stmt(comp, pns->nodes[0]);
1065 c_del_stmt(comp, pns->nodes[1]);
1066 }
1067 } else {
1068 // tuple with 1 element
1069 c_del_stmt(comp, pn);
1070 }
1071 } else {
1072 // not implemented
1073 assert(0);
1074 }
1075}
1076
Damiend99b0522013-12-21 18:17:45 +00001077void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001078 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1079}
1080
Damiend99b0522013-12-21 18:17:45 +00001081void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001082 if (comp->break_label == 0) {
1083 printf("ERROR: cannot break from here\n");
1084 }
Damien Georgecbddb272014-02-01 20:08:18 +00001085 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001086}
1087
Damiend99b0522013-12-21 18:17:45 +00001088void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001089 if (comp->continue_label == 0) {
1090 printf("ERROR: cannot continue from here\n");
1091 }
Damien Georgecbddb272014-02-01 20:08:18 +00001092 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001093}
1094
Damiend99b0522013-12-21 18:17:45 +00001095void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001096 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
1097 printf("SyntaxError: 'return' outside function\n");
1098 comp->had_error = true;
1099 return;
1100 }
Damiend99b0522013-12-21 18:17:45 +00001101 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001102 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001103 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001104 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001105 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001106 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1107 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 +01001108
Damienb05d7072013-10-05 13:37:10 +01001109 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001110 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1111 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1112 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001113 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001114 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1115 } else {
1116 compile_node(comp, pns->nodes[0]);
1117 }
1118 EMIT(return_value);
1119}
1120
Damiend99b0522013-12-21 18:17:45 +00001121void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001122 compile_node(comp, pns->nodes[0]);
1123 EMIT(pop_top);
1124}
1125
Damiend99b0522013-12-21 18:17:45 +00001126void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1127 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001128 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001129 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001130 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001131 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001132 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001133 compile_node(comp, pns->nodes[0]);
1134 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001135 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001136 } else {
1137 // raise x
1138 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001139 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001140 }
1141}
1142
1143// q1 holds the base, q2 the full name
1144// eg a -> q1=q2=a
1145// a.b.c -> q1=a, q2=a.b.c
Damiend99b0522013-12-21 18:17:45 +00001146void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q1, qstr *q2) {
Damien429d7192013-10-04 19:53:11 +01001147 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001148 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1149 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001150 // a name of the form x as y; unwrap it
Damiend99b0522013-12-21 18:17:45 +00001151 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001152 pn = pns->nodes[0];
1153 is_as = true;
1154 }
Damiend99b0522013-12-21 18:17:45 +00001155 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001156 // just a simple name
Damiend99b0522013-12-21 18:17:45 +00001157 *q2 = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001158 if (!is_as) {
1159 *q1 = *q2;
1160 }
Damien Georgeb9791222014-01-23 00:34:21 +00001161 EMIT_ARG(import_name, *q2);
Damiend99b0522013-12-21 18:17:45 +00001162 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1163 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1164 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001165 // a name of the form a.b.c
1166 if (!is_as) {
Damiend99b0522013-12-21 18:17:45 +00001167 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001168 }
Damiend99b0522013-12-21 18:17:45 +00001169 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001170 int len = n - 1;
1171 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001172 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001173 }
Damien George55baff42014-01-21 21:40:13 +00001174 byte *q_ptr;
1175 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001176 for (int i = 0; i < n; i++) {
1177 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001178 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001179 }
Damien George55baff42014-01-21 21:40:13 +00001180 uint str_src_len;
1181 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001182 memcpy(str_dest, str_src, str_src_len);
1183 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001184 }
Damien George55baff42014-01-21 21:40:13 +00001185 *q2 = qstr_build_end(q_ptr);
Damien Georgeb9791222014-01-23 00:34:21 +00001186 EMIT_ARG(import_name, *q2);
Damien429d7192013-10-04 19:53:11 +01001187 if (is_as) {
1188 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001189 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001190 }
1191 }
1192 } else {
1193 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001194 // This covers relative imports starting with dot(s) like "from .foo import"
Damien429d7192013-10-04 19:53:11 +01001195 assert(0);
1196 }
1197 } else {
1198 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001199 // This covers relative imports with dots only like "from .. import"
Damien429d7192013-10-04 19:53:11 +01001200 assert(0);
1201 }
1202}
1203
Damiend99b0522013-12-21 18:17:45 +00001204void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgeb9791222014-01-23 00:34:21 +00001205 EMIT_ARG(load_const_small_int, 0); // ??
1206 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001207 qstr q1, q2;
1208 do_import_name(comp, pn, &q1, &q2);
Damien Georgeb9791222014-01-23 00:34:21 +00001209 EMIT_ARG(store_id, q1);
Damien429d7192013-10-04 19:53:11 +01001210}
1211
Damiend99b0522013-12-21 18:17:45 +00001212void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001213 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1214}
1215
Damiend99b0522013-12-21 18:17:45 +00001216void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
1217 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001218 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001219
1220 // build the "fromlist" tuple
1221#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001222 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001223#else
Damien Georgeb9791222014-01-23 00:34:21 +00001224 EMIT_ARG(load_const_str, QSTR_FROM_STR_STATIC("*"), false);
1225 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001226#endif
1227
1228 // do the import
Damien429d7192013-10-04 19:53:11 +01001229 qstr dummy_q, id1;
1230 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1231 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001232
Damien429d7192013-10-04 19:53:11 +01001233 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001234 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001235
1236 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001237 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001238 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001239#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001240 {
1241 vstr_t *vstr = vstr_new();
1242 vstr_printf(vstr, "(");
1243 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001244 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1245 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1246 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001247 if (i > 0) {
1248 vstr_printf(vstr, ", ");
1249 }
1250 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001251 uint len;
1252 const byte *str = qstr_data(id2, &len);
1253 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001254 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001255 }
Damien02f89412013-12-12 15:13:36 +00001256 if (n == 1) {
1257 vstr_printf(vstr, ",");
1258 }
1259 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001260 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001261 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001262 }
Damiendb4c3612013-12-10 17:27:24 +00001263#else
1264 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001265 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1266 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1267 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001268 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001269 }
Damien Georgeb9791222014-01-23 00:34:21 +00001270 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001271#endif
1272
1273 // do the import
Damien429d7192013-10-04 19:53:11 +01001274 qstr dummy_q, id1;
1275 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1276 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001277 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1278 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1279 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001280 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001281 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001282 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001283 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001284 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001285 }
1286 }
1287 EMIT(pop_top);
1288 }
1289}
1290
Damiend99b0522013-12-21 18:17:45 +00001291void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001292 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001293 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1294 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001295 } else {
Damiend99b0522013-12-21 18:17:45 +00001296 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1297 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001298 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001299 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001300 }
Damien429d7192013-10-04 19:53:11 +01001301 }
1302 }
1303}
1304
Damiend99b0522013-12-21 18:17:45 +00001305void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001306 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001307 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1308 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001309 } else {
Damiend99b0522013-12-21 18:17:45 +00001310 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1311 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001312 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001313 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001314 }
Damien429d7192013-10-04 19:53:11 +01001315 }
1316 }
1317}
1318
Damiend99b0522013-12-21 18:17:45 +00001319void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001320 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001321 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001322 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 +00001323 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001324 // assertion message
1325 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001326 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01001327 }
Damien Georgeb9791222014-01-23 00:34:21 +00001328 EMIT_ARG(raise_varargs, 1);
1329 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001330}
1331
Damiend99b0522013-12-21 18:17:45 +00001332void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001333 // TODO proper and/or short circuiting
1334
Damienb05d7072013-10-05 13:37:10 +01001335 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001336
Damienb05d7072013-10-05 13:37:10 +01001337 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001338 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1339
1340 compile_node(comp, pns->nodes[1]); // if block
Damiend99b0522013-12-21 18:17:45 +00001341 //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 +01001342 // jump over elif/else blocks if they exist
Damien415eb6f2013-10-05 12:19:06 +01001343 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001344 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001345 }
1346 //}
Damien Georgeb9791222014-01-23 00:34:21 +00001347 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001348
Damiend99b0522013-12-21 18:17:45 +00001349 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001350 // compile elif blocks
1351
Damiend99b0522013-12-21 18:17:45 +00001352 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001353
Damiend99b0522013-12-21 18:17:45 +00001354 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001355 // multiple elif blocks
1356
Damiend99b0522013-12-21 18:17:45 +00001357 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001358 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001359 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001360 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001361 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1362
1363 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001364 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001365 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001366 }
Damien Georgeb9791222014-01-23 00:34:21 +00001367 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001368 }
1369
1370 } else {
1371 // a single elif block
1372
Damienb05d7072013-10-05 13:37:10 +01001373 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001374 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1375
1376 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001377 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001378 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001379 }
Damien Georgeb9791222014-01-23 00:34:21 +00001380 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001381 }
1382 }
1383
1384 // compile else block
1385 compile_node(comp, pns->nodes[3]); // can be null
1386
Damien Georgeb9791222014-01-23 00:34:21 +00001387 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001388}
1389
Damien Georgecbddb272014-02-01 20:08:18 +00001390#define START_BREAK_CONTINUE_BLOCK \
1391 int old_break_label = comp->break_label; \
1392 int old_continue_label = comp->continue_label; \
1393 int break_label = comp_next_label(comp); \
1394 int continue_label = comp_next_label(comp); \
1395 comp->break_label = break_label; \
1396 comp->continue_label = continue_label; \
1397 comp->break_continue_except_level = comp->cur_except_level;
1398
1399#define END_BREAK_CONTINUE_BLOCK \
1400 comp->break_label = old_break_label; \
1401 comp->continue_label = old_continue_label; \
1402 comp->break_continue_except_level = comp->cur_except_level;
1403
Damiend99b0522013-12-21 18:17:45 +00001404void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001405 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001406
Damience89a212013-10-15 22:25:17 +01001407 // compared to CPython, we have an optimised version of while loops
1408#if MICROPY_EMIT_CPYTHON
1409 int done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001410 EMIT_ARG(setup_loop, break_label);
1411 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001412 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1413 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001414 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001415 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001416 }
Damien Georgeb9791222014-01-23 00:34:21 +00001417 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001418 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1419 // this is a small hack to agree with CPython
1420 if (!node_is_const_true(pns->nodes[0])) {
1421 EMIT(pop_block);
1422 }
Damience89a212013-10-15 22:25:17 +01001423#else
1424 int top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001425 EMIT_ARG(jump, continue_label);
1426 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001427 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001428 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001429 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1430#endif
1431
1432 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001433 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001434
1435 compile_node(comp, pns->nodes[2]); // else
1436
Damien Georgeb9791222014-01-23 00:34:21 +00001437 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001438}
1439
Damienf72fd0e2013-11-06 20:20:49 +00001440// TODO preload end and step onto stack if they are not constants
1441// TODO check if step is negative and do opposite test
Damiend99b0522013-12-21 18:17:45 +00001442void 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 +00001443 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001444
1445 int top_label = comp_next_label(comp);
Damien George600ae732014-01-21 23:48:04 +00001446 int entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001447
1448 // compile: var = start
1449 compile_node(comp, pn_start);
1450 c_assign(comp, pn_var, ASSIGN_STORE);
1451
Damien Georgeb9791222014-01-23 00:34:21 +00001452 EMIT_ARG(jump, entry_label);
1453 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001454
Damienf3822fc2013-11-09 20:12:03 +00001455 // compile body
1456 compile_node(comp, pn_body);
1457
Damien Georgeb9791222014-01-23 00:34:21 +00001458 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001459
Damienf72fd0e2013-11-06 20:20:49 +00001460 // compile: var += step
1461 c_assign(comp, pn_var, ASSIGN_AUG_LOAD);
1462 compile_node(comp, pn_step);
Damien Georgeb9791222014-01-23 00:34:21 +00001463 EMIT_ARG(binary_op, RT_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001464 c_assign(comp, pn_var, ASSIGN_AUG_STORE);
1465
Damien Georgeb9791222014-01-23 00:34:21 +00001466 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001467
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001468 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001469 compile_node(comp, pn_var);
1470 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001471 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1472 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien George9aa2a522014-02-01 23:04:09 +00001473 EMIT_ARG(binary_op, RT_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001474 } else {
Damien George9aa2a522014-02-01 23:04:09 +00001475 EMIT_ARG(binary_op, RT_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001476 }
Damien Georgeb9791222014-01-23 00:34:21 +00001477 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001478
1479 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001480 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001481
1482 compile_node(comp, pn_else);
1483
Damien Georgeb9791222014-01-23 00:34:21 +00001484 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001485}
1486
Damiend99b0522013-12-21 18:17:45 +00001487void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001488#if !MICROPY_EMIT_CPYTHON
1489 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1490 // this is actually slower, but uses no heap memory
1491 // for viper it will be much, much faster
Damiend99b0522013-12-21 18:17:45 +00001492 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)) {
1493 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001494 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1495 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1496 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1497 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001498 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1499 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001500 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001501 mp_parse_node_t pn_range_start;
1502 mp_parse_node_t pn_range_end;
1503 mp_parse_node_t pn_range_step;
1504 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001505 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001506 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001507 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001508 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001509 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001510 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001511 } else if (n_args == 2) {
1512 pn_range_start = args[0];
1513 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001514 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001515 } else {
1516 pn_range_start = args[0];
1517 pn_range_end = args[1];
1518 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001519 // We need to know sign of step. This is possible only if it's constant
1520 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1521 optimize = false;
1522 }
Damienf72fd0e2013-11-06 20:20:49 +00001523 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001524 }
1525 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001526 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1527 return;
1528 }
1529 }
1530 }
1531#endif
1532
Damien Georgecbddb272014-02-01 20:08:18 +00001533 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001534
Damienb05d7072013-10-05 13:37:10 +01001535 int pop_label = comp_next_label(comp);
1536 int end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001537
Damience89a212013-10-15 22:25:17 +01001538 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1539#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001540 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001541#endif
1542
Damien429d7192013-10-04 19:53:11 +01001543 compile_node(comp, pns->nodes[1]); // iterator
1544 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001545 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001546 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001547 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1548 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001549 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001550 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001551 }
Damien Georgeb9791222014-01-23 00:34:21 +00001552 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001553 EMIT(for_iter_end);
1554
1555 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001556 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001557
Damience89a212013-10-15 22:25:17 +01001558#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001559 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001560#endif
Damien429d7192013-10-04 19:53:11 +01001561
1562 compile_node(comp, pns->nodes[3]); // else (not tested)
1563
Damien Georgeb9791222014-01-23 00:34:21 +00001564 EMIT_ARG(label_assign, break_label);
1565 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001566}
1567
Damiend99b0522013-12-21 18:17:45 +00001568void 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 +01001569 // this function is a bit of a hack at the moment
1570 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1571
1572 // setup code
1573 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001574 int l1 = comp_next_label(comp);
1575 int success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001576
Damien Georgeb9791222014-01-23 00:34:21 +00001577 EMIT_ARG(setup_except, l1);
Damien Georgecbddb272014-02-01 20:08:18 +00001578 comp->cur_except_level += 1;
1579
Damien429d7192013-10-04 19:53:11 +01001580 compile_node(comp, pn_body); // body
1581 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001582 EMIT_ARG(jump, success_label);
1583 EMIT_ARG(label_assign, l1);
Damienb05d7072013-10-05 13:37:10 +01001584 int l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001585
1586 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001587 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1588 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001589
1590 qstr qstr_exception_local = 0;
Damienb05d7072013-10-05 13:37:10 +01001591 int end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001592
Damiend99b0522013-12-21 18:17:45 +00001593 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001594 // this is a catch all exception handler
1595 if (i + 1 != n_except) {
1596 printf("SyntaxError: default 'except:' must be last\n");
1597 return;
1598 }
1599 } else {
1600 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001601 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1602 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1603 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1604 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001605 // handler binds the exception to a local
1606 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001607 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001608 }
1609 }
1610 EMIT(dup_top);
1611 compile_node(comp, pns_exception_expr);
Damien George9aa2a522014-02-01 23:04:09 +00001612 EMIT_ARG(binary_op, RT_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001613 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001614 }
1615
1616 EMIT(pop_top);
1617
1618 if (qstr_exception_local == 0) {
1619 EMIT(pop_top);
1620 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001621 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001622 }
1623
1624 EMIT(pop_top);
1625
Damiene2880aa2013-12-20 14:22:59 +00001626 int l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001627 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001628 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001629 EMIT_ARG(setup_finally, l3);
Damien Georgecbddb272014-02-01 20:08:18 +00001630 comp->cur_except_level += 1;
Damien429d7192013-10-04 19:53:11 +01001631 }
1632 compile_node(comp, pns_except->nodes[1]);
1633 if (qstr_exception_local != 0) {
1634 EMIT(pop_block);
1635 }
1636 EMIT(pop_except);
1637 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001638 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1639 EMIT_ARG(label_assign, l3);
1640 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1641 EMIT_ARG(store_id, qstr_exception_local);
1642 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001643
1644 comp->cur_except_level -= 1;
Damien429d7192013-10-04 19:53:11 +01001645 EMIT(end_finally);
1646 }
Damien Georgeb9791222014-01-23 00:34:21 +00001647 EMIT_ARG(jump, l2);
1648 EMIT_ARG(label_assign, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001649 }
1650
Damien Georgecbddb272014-02-01 20:08:18 +00001651 comp->cur_except_level -= 1;
Damien429d7192013-10-04 19:53:11 +01001652 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001653
Damien Georgeb9791222014-01-23 00:34:21 +00001654 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001655 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001656 EMIT_ARG(label_assign, l2);
1657 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001658}
1659
Damiend99b0522013-12-21 18:17:45 +00001660void 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 +01001661 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1662 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001663 int l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001664
Damien Georgeb9791222014-01-23 00:34:21 +00001665 EMIT_ARG(setup_finally, l_finally_block);
Damien Georgecbddb272014-02-01 20:08:18 +00001666 comp->cur_except_level += 1;
1667
Damien429d7192013-10-04 19:53:11 +01001668 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001669 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien429d7192013-10-04 19:53:11 +01001670 compile_node(comp, pn_body);
1671 } else {
1672 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1673 }
1674 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001675 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1676 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001677 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001678
1679 comp->cur_except_level -= 1;
Damien429d7192013-10-04 19:53:11 +01001680 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001681
Damien Georgeb9791222014-01-23 00:34:21 +00001682 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001683}
1684
Damiend99b0522013-12-21 18:17:45 +00001685void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1686 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1687 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1688 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001689 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001690 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1691 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001692 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001693 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001694 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001695 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001696 // no finally
1697 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1698 } else {
1699 // have finally
Damiend99b0522013-12-21 18:17:45 +00001700 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 +01001701 }
1702 } else {
1703 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001704 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001705 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001706 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001707 }
1708 } else {
1709 // shouldn't happen
1710 assert(0);
1711 }
1712}
1713
Damiend99b0522013-12-21 18:17:45 +00001714void 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 +01001715 if (n == 0) {
1716 // no more pre-bits, compile the body of the with
1717 compile_node(comp, body);
1718 } else {
Damienb05d7072013-10-05 13:37:10 +01001719 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001720 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001721 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001722 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001723 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001724 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001725 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1726 } else {
1727 // this pre-bit is just an expression
1728 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001729 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001730 EMIT(pop_top);
1731 }
1732 // compile additional pre-bits and the body
1733 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1734 // finish this with block
1735 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001736 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1737 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001738 EMIT(with_cleanup);
1739 EMIT(end_finally);
1740 }
1741}
1742
Damiend99b0522013-12-21 18:17:45 +00001743void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001744 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001745 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001746 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1747 assert(n > 0);
1748
1749 // compile in a nested fashion
1750 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1751}
1752
Damiend99b0522013-12-21 18:17:45 +00001753void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1754 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001755 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1756 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001757 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001758 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001759 EMIT_ARG(call_function, 1, 0, false, false);
Damien5ac1b2e2013-10-18 19:58:12 +01001760 EMIT(pop_top);
1761
Damien429d7192013-10-04 19:53:11 +01001762 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001763 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001764 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001765 // do nothing with a lonely constant
1766 } else {
1767 compile_node(comp, pns->nodes[0]); // just an expression
1768 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1769 }
Damien429d7192013-10-04 19:53:11 +01001770 }
1771 } else {
Damiend99b0522013-12-21 18:17:45 +00001772 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1773 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001774 if (kind == PN_expr_stmt_augassign) {
1775 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1776 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001777 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien George7e5fb242014-02-01 22:18:47 +00001778 rt_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001779 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien George7e5fb242014-02-01 22:18:47 +00001780 case MP_TOKEN_DEL_PIPE_EQUAL: op = RT_BINARY_OP_INPLACE_OR; break;
1781 case MP_TOKEN_DEL_CARET_EQUAL: op = RT_BINARY_OP_INPLACE_XOR; break;
1782 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = RT_BINARY_OP_INPLACE_AND; break;
1783 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = RT_BINARY_OP_INPLACE_LSHIFT; break;
1784 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = RT_BINARY_OP_INPLACE_RSHIFT; break;
1785 case MP_TOKEN_DEL_PLUS_EQUAL: op = RT_BINARY_OP_INPLACE_ADD; break;
1786 case MP_TOKEN_DEL_MINUS_EQUAL: op = RT_BINARY_OP_INPLACE_SUBTRACT; break;
1787 case MP_TOKEN_DEL_STAR_EQUAL: op = RT_BINARY_OP_INPLACE_MULTIPLY; break;
1788 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = RT_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1789 case MP_TOKEN_DEL_SLASH_EQUAL: op = RT_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1790 case MP_TOKEN_DEL_PERCENT_EQUAL: op = RT_BINARY_OP_INPLACE_MODULO; break;
1791 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = RT_BINARY_OP_INPLACE_POWER; break;
1792 default: assert(0); op = RT_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01001793 }
Damien George7e5fb242014-02-01 22:18:47 +00001794 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001795 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1796 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001797 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1798 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001799 // following CPython, we store left-most first
1800 if (rhs > 0) {
1801 EMIT(dup_top);
1802 }
1803 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1804 for (int i = 0; i < rhs; i++) {
1805 if (i + 1 < rhs) {
1806 EMIT(dup_top);
1807 }
Damiend99b0522013-12-21 18:17:45 +00001808 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001809 }
1810 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001811 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1812 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1813 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1814 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01001815 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001816 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1817 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001818 compile_node(comp, pns10->nodes[0]); // rhs
1819 compile_node(comp, pns10->nodes[1]); // rhs
1820 EMIT(rot_two);
1821 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1822 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00001823 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1824 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1825 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1826 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01001827 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001828 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1829 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001830 compile_node(comp, pns10->nodes[0]); // rhs
1831 compile_node(comp, pns10->nodes[1]); // rhs
1832 compile_node(comp, pns10->nodes[2]); // rhs
1833 EMIT(rot_three);
1834 EMIT(rot_two);
1835 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1836 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1837 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1838 } else {
1839 compile_node(comp, pns1->nodes[0]); // rhs
1840 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1841 }
1842 } else {
1843 // shouldn't happen
1844 assert(0);
1845 }
1846 }
1847}
1848
Damiend99b0522013-12-21 18:17:45 +00001849void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, rt_binary_op_t binary_op) {
1850 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001851 compile_node(comp, pns->nodes[0]);
1852 for (int i = 1; i < num_nodes; i += 1) {
1853 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001854 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001855 }
1856}
1857
Damiend99b0522013-12-21 18:17:45 +00001858void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1859 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1860 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001861
1862 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001863 int l_fail = comp_next_label(comp);
1864 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001865 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1866 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001867 EMIT_ARG(jump, l_end);
1868 EMIT_ARG(label_assign, l_fail);
1869 EMIT_ARG(set_stack_size, stack_size); // force stack size reset
Damien429d7192013-10-04 19:53:11 +01001870 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001871 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001872}
1873
Damiend99b0522013-12-21 18:17:45 +00001874void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001875 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00001876 //mp_parse_node_t pn_params = pns->nodes[0];
1877 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001878
1879 if (comp->pass == PASS_1) {
1880 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00001881 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 +01001882 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001883 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001884 }
1885
1886 // get the scope for this lambda
1887 scope_t *this_scope = (scope_t*)pns->nodes[2];
1888
1889 // make the lambda
1890 close_over_variables_etc(comp, this_scope, 0, 0);
1891}
1892
Damiend99b0522013-12-21 18:17:45 +00001893void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001894 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001895 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001896 for (int i = 0; i < n; i += 1) {
1897 compile_node(comp, pns->nodes[i]);
1898 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00001899 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01001900 }
1901 }
Damien Georgeb9791222014-01-23 00:34:21 +00001902 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001903}
1904
Damiend99b0522013-12-21 18:17:45 +00001905void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001906 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001907 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001908 for (int i = 0; i < n; i += 1) {
1909 compile_node(comp, pns->nodes[i]);
1910 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00001911 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01001912 }
1913 }
Damien Georgeb9791222014-01-23 00:34:21 +00001914 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001915}
1916
Damiend99b0522013-12-21 18:17:45 +00001917void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001918 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001919 EMIT_ARG(unary_op, RT_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01001920}
1921
Damiend99b0522013-12-21 18:17:45 +00001922void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001923 int stack_size = EMIT(get_stack_size);
Damiend99b0522013-12-21 18:17:45 +00001924 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001925 compile_node(comp, pns->nodes[0]);
1926 bool multi = (num_nodes > 3);
1927 int l_fail = 0;
1928 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001929 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001930 }
1931 for (int i = 1; i + 1 < num_nodes; i += 2) {
1932 compile_node(comp, pns->nodes[i + 1]);
1933 if (i + 2 < num_nodes) {
1934 EMIT(dup_top);
1935 EMIT(rot_three);
1936 }
Damien George7e5fb242014-02-01 22:18:47 +00001937 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
1938 rt_binary_op_t op;
1939 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien George9aa2a522014-02-01 23:04:09 +00001940 case MP_TOKEN_OP_LESS: op = RT_BINARY_OP_LESS; break;
1941 case MP_TOKEN_OP_MORE: op = RT_BINARY_OP_MORE; break;
1942 case MP_TOKEN_OP_DBL_EQUAL: op = RT_BINARY_OP_EQUAL; break;
1943 case MP_TOKEN_OP_LESS_EQUAL: op = RT_BINARY_OP_LESS_EQUAL; break;
1944 case MP_TOKEN_OP_MORE_EQUAL: op = RT_BINARY_OP_MORE_EQUAL; break;
1945 case MP_TOKEN_OP_NOT_EQUAL: op = RT_BINARY_OP_NOT_EQUAL; break;
1946 case MP_TOKEN_KW_IN: op = RT_BINARY_OP_IN; break;
1947 default: assert(0); op = RT_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00001948 }
1949 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00001950 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
1951 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
1952 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01001953 if (kind == PN_comp_op_not_in) {
Damien George9aa2a522014-02-01 23:04:09 +00001954 EMIT_ARG(binary_op, RT_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01001955 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00001956 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien George9aa2a522014-02-01 23:04:09 +00001957 EMIT_ARG(binary_op, RT_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01001958 } else {
Damien George9aa2a522014-02-01 23:04:09 +00001959 EMIT_ARG(binary_op, RT_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01001960 }
1961 } else {
1962 // shouldn't happen
1963 assert(0);
1964 }
1965 } else {
1966 // shouldn't happen
1967 assert(0);
1968 }
1969 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00001970 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01001971 }
1972 }
1973 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001974 int l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001975 EMIT_ARG(jump, l_end);
1976 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001977 EMIT(rot_two);
1978 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00001979 EMIT_ARG(label_assign, l_end);
1980 EMIT_ARG(set_stack_size, stack_size + 1); // force stack size
Damien429d7192013-10-04 19:53:11 +01001981 }
1982}
1983
Damiend99b0522013-12-21 18:17:45 +00001984void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001985 // TODO
1986 assert(0);
1987 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001988 //EMIT_ARG(unary_op, "UNARY_STAR");
Damien429d7192013-10-04 19:53:11 +01001989}
1990
Damiend99b0522013-12-21 18:17:45 +00001991void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001992 c_binary_op(comp, pns, RT_BINARY_OP_OR);
1993}
1994
Damiend99b0522013-12-21 18:17:45 +00001995void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001996 c_binary_op(comp, pns, RT_BINARY_OP_XOR);
1997}
1998
Damiend99b0522013-12-21 18:17:45 +00001999void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002000 c_binary_op(comp, pns, RT_BINARY_OP_AND);
2001}
2002
Damiend99b0522013-12-21 18:17:45 +00002003void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2004 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002005 compile_node(comp, pns->nodes[0]);
2006 for (int i = 1; i + 1 < num_nodes; i += 2) {
2007 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002008 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002009 EMIT_ARG(binary_op, RT_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002010 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002011 EMIT_ARG(binary_op, RT_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002012 } else {
2013 // shouldn't happen
2014 assert(0);
2015 }
2016 }
2017}
2018
Damiend99b0522013-12-21 18:17:45 +00002019void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2020 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002021 compile_node(comp, pns->nodes[0]);
2022 for (int i = 1; i + 1 < num_nodes; i += 2) {
2023 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002024 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002025 EMIT_ARG(binary_op, RT_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002026 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002027 EMIT_ARG(binary_op, RT_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002028 } else {
2029 // shouldn't happen
2030 assert(0);
2031 }
2032 }
2033}
2034
Damiend99b0522013-12-21 18:17:45 +00002035void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2036 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002037 compile_node(comp, pns->nodes[0]);
2038 for (int i = 1; i + 1 < num_nodes; i += 2) {
2039 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002040 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002041 EMIT_ARG(binary_op, RT_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002042 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002043 EMIT_ARG(binary_op, RT_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002044 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002045 EMIT_ARG(binary_op, RT_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002046 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002047 EMIT_ARG(binary_op, RT_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002048 } else {
2049 // shouldn't happen
2050 assert(0);
2051 }
2052 }
2053}
2054
Damiend99b0522013-12-21 18:17:45 +00002055void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002056 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002057 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002058 EMIT_ARG(unary_op, RT_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002059 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002060 EMIT_ARG(unary_op, RT_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002061 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002062 EMIT_ARG(unary_op, RT_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002063 } else {
2064 // shouldn't happen
2065 assert(0);
2066 }
2067}
2068
Damien George35e2a4e2014-02-05 00:51:47 +00002069void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2070 // this is to handle special super() call
2071 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2072
2073 compile_generic_all_nodes(comp, pns);
2074}
2075
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002076STATIC 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 +01002077 // function to call is on top of stack
2078
Damien George35e2a4e2014-02-05 00:51:47 +00002079#if !MICROPY_EMIT_CPYTHON
2080 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002081 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 +00002082 EMIT_ARG(load_id, MP_QSTR___class__);
2083 // get first argument to function
2084 bool found = false;
2085 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
2086 if (comp->scope_cur->id_info[i].param) {
2087 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
2088 found = true;
2089 break;
2090 }
2091 }
2092 if (!found) {
2093 printf("TypeError: super() call cannot find self\n");
2094 return;
2095 }
2096 EMIT_ARG(call_function, 2, 0, false, false);
2097 return;
2098 }
2099#endif
2100
Damien429d7192013-10-04 19:53:11 +01002101 int old_n_arg_keyword = comp->n_arg_keyword;
2102 bool old_have_star_arg = comp->have_star_arg;
2103 bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
2104 comp->n_arg_keyword = 0;
2105 comp->have_star_arg = false;
2106 comp->have_dbl_star_arg = false;
2107
Damien Georgebbcd49a2014-02-06 20:30:16 +00002108 compile_node(comp, pn_arglist); // arguments to function call; can be null
Damien429d7192013-10-04 19:53:11 +01002109
2110 // compute number of positional arguments
Damien Georgebbcd49a2014-02-06 20:30:16 +00002111 int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
Damien429d7192013-10-04 19:53:11 +01002112 if (comp->have_star_arg) {
2113 n_positional -= 1;
2114 }
2115 if (comp->have_dbl_star_arg) {
2116 n_positional -= 1;
2117 }
2118
2119 if (is_method_call) {
Damien Georgeb9791222014-01-23 00:34:21 +00002120 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 +01002121 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002122 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 +01002123 }
2124
2125 comp->n_arg_keyword = old_n_arg_keyword;
2126 comp->have_star_arg = old_have_star_arg;
2127 comp->have_dbl_star_arg = old_have_dbl_star_arg;
2128}
2129
Damiend99b0522013-12-21 18:17:45 +00002130void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2131 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002132 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002133 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 +01002134 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002135 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2136 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002137 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002138 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002139 i += 1;
2140 } else {
2141 compile_node(comp, pns->nodes[i]);
2142 }
Damien George35e2a4e2014-02-05 00:51:47 +00002143 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002144 }
2145}
2146
Damiend99b0522013-12-21 18:17:45 +00002147void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002148 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002149 EMIT_ARG(binary_op, RT_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002150}
2151
Damiend99b0522013-12-21 18:17:45 +00002152void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002153 // a list of strings
Damien63321742013-12-10 17:41:49 +00002154
2155 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002156 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002157 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002158 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002159 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002160 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2161 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2162 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002163 if (i == 0) {
2164 string_kind = pn_kind;
2165 } else if (pn_kind != string_kind) {
2166 printf("SyntaxError: cannot mix bytes and nonbytes literals\n");
2167 return;
2168 }
Damien George55baff42014-01-21 21:40:13 +00002169 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002170 }
Damien63321742013-12-10 17:41:49 +00002171
Damien63321742013-12-10 17:41:49 +00002172 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002173 byte *q_ptr;
2174 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002175 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002176 uint s_len;
2177 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002178 memcpy(s_dest, s, s_len);
2179 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002180 }
Damien George55baff42014-01-21 21:40:13 +00002181 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002182
Damien Georgeb9791222014-01-23 00:34:21 +00002183 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002184}
2185
2186// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002187void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2188 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2189 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2190 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002191
2192 if (comp->pass == PASS_1) {
2193 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002194 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 +01002195 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002196 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002197 }
2198
2199 // get the scope for this comprehension
2200 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2201
2202 // compile the comprehension
2203 close_over_variables_etc(comp, this_scope, 0, 0);
2204
2205 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2206 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002207 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01002208}
2209
Damiend99b0522013-12-21 18:17:45 +00002210void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2211 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002212 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002213 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2214 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2215 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2216 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2217 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2218 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2219 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002220 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002221 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002222 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002223 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002224 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002225 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002226 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002227 // generator expression
2228 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2229 } else {
2230 // tuple with 2 items
2231 goto tuple_with_2_items;
2232 }
2233 } else {
2234 // tuple with 2 items
2235 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002236 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002237 }
2238 } else {
2239 // parenthesis around a single item, is just that item
2240 compile_node(comp, pns->nodes[0]);
2241 }
2242}
2243
Damiend99b0522013-12-21 18:17:45 +00002244void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2245 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002246 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002247 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002248 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2249 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2250 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2251 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2252 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002253 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002254 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002255 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002256 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002257 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002258 // list of many items
2259 compile_node(comp, pns2->nodes[0]);
2260 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002261 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002262 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002263 // list comprehension
2264 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2265 } else {
2266 // list with 2 items
2267 goto list_with_2_items;
2268 }
2269 } else {
2270 // list with 2 items
2271 list_with_2_items:
2272 compile_node(comp, pns2->nodes[0]);
2273 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002274 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002275 }
2276 } else {
2277 // list with 1 item
2278 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002279 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002280 }
2281}
2282
Damiend99b0522013-12-21 18:17:45 +00002283void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2284 mp_parse_node_t pn = pns->nodes[0];
2285 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002286 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002287 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002288 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2289 pns = (mp_parse_node_struct_t*)pn;
2290 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002291 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002292 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002293 compile_node(comp, pn);
2294 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002295 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2296 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2297 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2298 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002299 // dict/set with multiple elements
2300
2301 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002302 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002303 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2304
2305 // first element sets whether it's a dict or set
2306 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002307 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002308 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002309 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002310 compile_node(comp, pns->nodes[0]);
2311 EMIT(store_map);
2312 is_dict = true;
2313 } else {
2314 // a set
2315 compile_node(comp, pns->nodes[0]); // 1st value of set
2316 is_dict = false;
2317 }
2318
2319 // process rest of elements
2320 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002321 mp_parse_node_t pn = nodes[i];
2322 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002323 compile_node(comp, pn);
2324 if (is_dict) {
2325 if (!is_key_value) {
2326 printf("SyntaxError?: expecting key:value for dictionary");
2327 return;
2328 }
2329 EMIT(store_map);
2330 } else {
2331 if (is_key_value) {
2332 printf("SyntaxError?: expecting just a value for set");
2333 return;
2334 }
2335 }
2336 }
2337
2338 // if it's a set, build it
2339 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002340 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002341 }
Damiend99b0522013-12-21 18:17:45 +00002342 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002343 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002344 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002345 // a dictionary comprehension
2346 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2347 } else {
2348 // a set comprehension
2349 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2350 }
2351 } else {
2352 // shouldn't happen
2353 assert(0);
2354 }
2355 } else {
2356 // set with one element
2357 goto set_with_one_element;
2358 }
2359 } else {
2360 // set with one element
2361 set_with_one_element:
2362 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002363 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002364 }
2365}
2366
Damiend99b0522013-12-21 18:17:45 +00002367void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002368 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002369}
2370
Damiend99b0522013-12-21 18:17:45 +00002371void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002372 // object who's index we want is on top of stack
2373 compile_node(comp, pns->nodes[0]); // the index
Damien Georgeb9791222014-01-23 00:34:21 +00002374 EMIT_ARG(binary_op, RT_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002375}
2376
Damiend99b0522013-12-21 18:17:45 +00002377void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002378 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002379 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002380}
2381
Damiend99b0522013-12-21 18:17:45 +00002382void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2383 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2384 mp_parse_node_t pn = pns->nodes[0];
2385 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002386 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002387 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2388 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002389 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2390 pns = (mp_parse_node_struct_t*)pn;
2391 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002392 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002393 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002394 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002395 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002396 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002397 } else {
2398 // [?::x]
2399 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002400 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002401 }
Damiend99b0522013-12-21 18:17:45 +00002402 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002403 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002404 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2405 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2406 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2407 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002408 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002409 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002410 } else {
2411 // [?:x:x]
2412 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002413 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002414 }
2415 } else {
2416 // [?:x]
2417 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002418 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002419 }
2420 } else {
2421 // [?:x]
2422 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002423 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002424 }
2425}
2426
Damiend99b0522013-12-21 18:17:45 +00002427void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002428 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002429 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2430 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002431}
2432
Damiend99b0522013-12-21 18:17:45 +00002433void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002434 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002435 compile_subscript_3_helper(comp, pns);
2436}
2437
Damiend99b0522013-12-21 18:17:45 +00002438void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002439 // if this is called then we are compiling a dict key:value pair
2440 compile_node(comp, pns->nodes[1]); // value
2441 compile_node(comp, pns->nodes[0]); // key
2442}
2443
Damiend99b0522013-12-21 18:17:45 +00002444void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002445 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002446 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002447 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002448}
2449
Damiend99b0522013-12-21 18:17:45 +00002450void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002451 if (comp->have_star_arg) {
2452 printf("SyntaxError?: can't have multiple *x\n");
2453 return;
2454 }
2455 comp->have_star_arg = true;
2456 compile_node(comp, pns->nodes[0]);
2457}
2458
Damiend99b0522013-12-21 18:17:45 +00002459void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002460 if (comp->have_dbl_star_arg) {
2461 printf("SyntaxError?: can't have multiple **x\n");
2462 return;
2463 }
2464 comp->have_dbl_star_arg = true;
2465 compile_node(comp, pns->nodes[0]);
2466}
2467
Damiend99b0522013-12-21 18:17:45 +00002468void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2469 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2470 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2471 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2472 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002473 printf("SyntaxError?: lhs of keyword argument must be an id\n");
2474 return;
2475 }
Damien Georgeb9791222014-01-23 00:34:21 +00002476 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002477 compile_node(comp, pns2->nodes[0]);
2478 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002479 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002480 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2481 } else {
2482 // shouldn't happen
2483 assert(0);
2484 }
2485}
2486
Damiend99b0522013-12-21 18:17:45 +00002487void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002488 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
2489 printf("SyntaxError: 'yield' outside function\n");
2490 return;
2491 }
Damiend99b0522013-12-21 18:17:45 +00002492 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002493 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002494 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002495 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2496 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002497 compile_node(comp, pns->nodes[0]);
2498 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002499 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002500 EMIT(yield_from);
2501 } else {
2502 compile_node(comp, pns->nodes[0]);
2503 EMIT(yield_value);
2504 }
2505}
2506
Damiend99b0522013-12-21 18:17:45 +00002507typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002508STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002509 NULL,
2510#define nc NULL
2511#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002512#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002513#include "grammar.h"
2514#undef nc
2515#undef c
2516#undef DEF_RULE
2517};
2518
Damiend99b0522013-12-21 18:17:45 +00002519void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2520 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002521 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002522 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2523 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2524 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002525 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002526 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002527 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002528 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002529 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2530 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2531 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2532 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002533 case MP_PARSE_NODE_TOKEN:
2534 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002535 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002536 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002537 // do nothing
2538 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002539 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002540 }
2541 break;
Damien429d7192013-10-04 19:53:11 +01002542 default: assert(0);
2543 }
2544 } else {
Damiend99b0522013-12-21 18:17:45 +00002545 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002546 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002547 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002548 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002549 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002550#if MICROPY_DEBUG_PRINTERS
2551 mp_parse_node_print(pn, 0);
2552#endif
Damien429d7192013-10-04 19:53:11 +01002553 assert(0);
2554 } else {
2555 f(comp, pns);
2556 }
2557 }
2558}
2559
Damiend99b0522013-12-21 18:17:45 +00002560void 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 +01002561 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002562 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002563 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2564 if (MP_PARSE_NODE_IS_ID(pn)) {
2565 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002566 if (comp->have_bare_star) {
2567 // comes after a bare star, so doesn't count as a parameter
2568 } else {
2569 comp->scope_cur->num_params += 1;
2570 }
Damienb14de212013-10-06 00:28:28 +01002571 } else {
Damiend99b0522013-12-21 18:17:45 +00002572 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2573 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2574 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2575 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002576 //int node_index = 1; unused
2577 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002578 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002579 // this parameter has an annotation
2580 pn_annotation = pns->nodes[1];
2581 }
2582 //node_index = 2; unused
2583 }
2584 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002585 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002586 // this parameter has a default value
2587 if (comp->have_bare_star) {
2588 comp->scope_cur->num_dict_params += 1;
2589 } else {
2590 comp->scope_cur->num_default_params += 1;
2591 }
2592 }
2593 */
2594 if (comp->have_bare_star) {
2595 // comes after a bare star, so doesn't count as a parameter
2596 } else {
2597 comp->scope_cur->num_params += 1;
2598 }
Damiend99b0522013-12-21 18:17:45 +00002599 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2600 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002601 // bare star
2602 // TODO see http://www.python.org/dev/peps/pep-3102/
2603 comp->have_bare_star = true;
2604 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002605 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002606 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002607 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002608 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2609 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002610 // named star with annotation
Damien George8725f8f2014-02-15 19:33:11 +00002611 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002612 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2613 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002614 pn_annotation = pns->nodes[1];
2615 } else {
2616 // shouldn't happen
2617 assert(0);
2618 }
Damiend99b0522013-12-21 18:17:45 +00002619 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2620 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2621 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002622 // this parameter has an annotation
2623 pn_annotation = pns->nodes[1];
2624 }
Damien George8725f8f2014-02-15 19:33:11 +00002625 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002626 } else {
Damienb14de212013-10-06 00:28:28 +01002627 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002628 assert(0);
2629 }
Damien429d7192013-10-04 19:53:11 +01002630 }
2631
2632 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002633 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002634 // TODO this parameter has an annotation
2635 }
2636 bool added;
2637 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2638 if (!added) {
2639 printf("SyntaxError?: same name used for parameter; %s\n", qstr_str(param_name));
2640 return;
2641 }
2642 id_info->param = true;
2643 id_info->kind = ID_INFO_KIND_LOCAL;
2644 }
2645}
2646
Damiend99b0522013-12-21 18:17:45 +00002647void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002648 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2649}
2650
Damiend99b0522013-12-21 18:17:45 +00002651void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002652 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2653}
2654
Damiend99b0522013-12-21 18:17:45 +00002655void 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 +01002656 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002657 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002658 // no more nested if/for; compile inner expression
2659 compile_node(comp, pn_inner_expr);
2660 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002661 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002662 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002663 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002664 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002665 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002666 } else {
2667 EMIT(yield_value);
2668 EMIT(pop_top);
2669 }
Damiend99b0522013-12-21 18:17:45 +00002670 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002671 // if condition
Damiend99b0522013-12-21 18:17:45 +00002672 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002673 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2674 pn_iter = pns_comp_if->nodes[1];
2675 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002676 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002677 // for loop
Damiend99b0522013-12-21 18:17:45 +00002678 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002679 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002680 int l_end2 = comp_next_label(comp);
2681 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002682 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002683 EMIT_ARG(label_assign, l_top2);
2684 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002685 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2686 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 +00002687 EMIT_ARG(jump, l_top2);
2688 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002689 EMIT(for_iter_end);
2690 } else {
2691 // shouldn't happen
2692 assert(0);
2693 }
2694}
2695
Damiend99b0522013-12-21 18:17:45 +00002696void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002697 // see http://www.python.org/dev/peps/pep-0257/
2698
2699 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002700 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002701 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002702 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002703 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002704 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2705 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002706 for (int i = 0; i < num_nodes; i++) {
2707 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002708 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 +00002709 // not a newline, so this is the first statement; finish search
2710 break;
2711 }
2712 }
2713 // 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 +00002714 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002715 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002716 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002717 } else {
2718 return;
2719 }
2720
2721 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002722 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2723 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2724 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2725 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2726 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002727 compile_node(comp, pns->nodes[0]); // a doc string
2728 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002729 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002730 }
2731 }
2732 }
2733}
2734
2735void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2736 comp->pass = pass;
2737 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002738 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002739 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002740
2741 if (comp->pass == PASS_1) {
2742 scope->stack_size = 0;
2743 }
2744
Damien5ac1b2e2013-10-18 19:58:12 +01002745#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002746 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002747 scope_print_info(scope);
2748 }
Damien5ac1b2e2013-10-18 19:58:12 +01002749#endif
Damien429d7192013-10-04 19:53:11 +01002750
2751 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002752 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2753 assert(scope->kind == SCOPE_MODULE);
2754 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2755 compile_node(comp, pns->nodes[0]); // compile the expression
2756 EMIT(return_value);
2757 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002758 if (!comp->is_repl) {
2759 check_for_doc_string(comp, scope->pn);
2760 }
Damien429d7192013-10-04 19:53:11 +01002761 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002762 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002763 EMIT(return_value);
2764 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002765 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2766 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2767 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002768
2769 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002770 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002771 if (comp->pass == PASS_1) {
2772 comp->have_bare_star = false;
2773 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2774 }
2775
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02002776 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01002777
2778 compile_node(comp, pns->nodes[3]); // 3 is function body
2779 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002780 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002781 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002782 EMIT(return_value);
2783 }
2784 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002785 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2786 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2787 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002788
2789 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002790 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002791 if (comp->pass == PASS_1) {
2792 comp->have_bare_star = false;
2793 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2794 }
2795
2796 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2797 EMIT(return_value);
2798 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2799 // a bit of a hack at the moment
2800
Damiend99b0522013-12-21 18:17:45 +00002801 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2802 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2803 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2804 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2805 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002806
Damien George55baff42014-01-21 21:40:13 +00002807 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01002808 if (comp->pass == PASS_1) {
2809 bool added;
2810 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2811 assert(added);
2812 id_info->kind = ID_INFO_KIND_LOCAL;
2813 scope->num_params = 1;
2814 }
2815
2816 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002817 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01002818 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002819 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01002820 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002821 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01002822 }
2823
Damienb05d7072013-10-05 13:37:10 +01002824 int l_end = comp_next_label(comp);
2825 int l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002826 EMIT_ARG(load_id, qstr_arg);
2827 EMIT_ARG(label_assign, l_top);
2828 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01002829 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2830 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00002831 EMIT_ARG(jump, l_top);
2832 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002833 EMIT(for_iter_end);
2834
2835 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00002836 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002837 }
2838 EMIT(return_value);
2839 } else {
2840 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002841 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2842 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2843 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01002844
2845 if (comp->pass == PASS_1) {
2846 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002847 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01002848 assert(added);
2849 id_info->kind = ID_INFO_KIND_LOCAL;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002850 id_info = scope_find_or_add_id(scope, MP_QSTR___locals__, &added);
Damien429d7192013-10-04 19:53:11 +01002851 assert(added);
2852 id_info->kind = ID_INFO_KIND_LOCAL;
2853 id_info->param = true;
2854 scope->num_params = 1; // __locals__ is the parameter
2855 }
2856
Damien Georgeb9791222014-01-23 00:34:21 +00002857 EMIT_ARG(load_id, MP_QSTR___locals__);
Damien429d7192013-10-04 19:53:11 +01002858 EMIT(store_locals);
Damien Georgeb9791222014-01-23 00:34:21 +00002859 EMIT_ARG(load_id, MP_QSTR___name__);
2860 EMIT_ARG(store_id, MP_QSTR___module__);
2861 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
2862 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01002863
2864 check_for_doc_string(comp, pns->nodes[2]);
2865 compile_node(comp, pns->nodes[2]); // 2 is class body
2866
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002867 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01002868 assert(id != NULL);
2869 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00002870 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002871 } else {
Damien George6baf76e2013-12-30 22:32:17 +00002872#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00002873 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002874#else
Damien George35e2a4e2014-02-05 00:51:47 +00002875 EMIT_ARG(load_fast, MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00002876#endif
Damien429d7192013-10-04 19:53:11 +01002877 }
2878 EMIT(return_value);
2879 }
2880
Damien415eb6f2013-10-05 12:19:06 +01002881 EMIT(end_pass);
Damien826005c2013-10-05 23:17:28 +01002882}
2883
2884void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2885 comp->pass = pass;
2886 comp->scope_cur = scope;
2887 comp->next_label = 1;
2888
2889 if (scope->kind != SCOPE_FUNCTION) {
2890 printf("Error: inline assembler must be a function\n");
2891 return;
2892 }
2893
Damiena2f2f7d2013-10-06 00:14:13 +01002894 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00002895 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01002896 }
2897
Damien826005c2013-10-05 23:17:28 +01002898 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00002899 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2900 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2901 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01002902
Damiend99b0522013-12-21 18:17:45 +00002903 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01002904
Damiena2f2f7d2013-10-06 00:14:13 +01002905 // parameters are in pns->nodes[1]
2906 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00002907 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01002908 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00002909 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01002910 }
2911
Damiend99b0522013-12-21 18:17:45 +00002912 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01002913
Damiend99b0522013-12-21 18:17:45 +00002914 mp_parse_node_t pn_body = pns->nodes[3]; // body
2915 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01002916 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
2917
Damien Georgecbd2f742014-01-19 11:48:48 +00002918 /*
Damien826005c2013-10-05 23:17:28 +01002919 if (comp->pass == PASS_3) {
2920 //printf("----\n");
2921 scope_print_info(scope);
2922 }
Damien Georgecbd2f742014-01-19 11:48:48 +00002923 */
Damien826005c2013-10-05 23:17:28 +01002924
2925 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00002926 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
2927 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
2928 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
2929 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
2930 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
2931 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
2932 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
2933 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
2934 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
2935 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
2936 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
2937 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
2938 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01002939 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
2940
2941 // emit instructions
2942 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00002943 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien826005c2013-10-05 23:17:28 +01002944 printf("SyntaxError: inline assembler 'label' requires 1 argument\n");
2945 return;
2946 }
2947 int lab = comp_next_label(comp);
2948 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00002949 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01002950 }
2951 } else {
2952 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00002953 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01002954 }
2955 }
2956 }
2957
2958 if (comp->pass > PASS_1) {
2959 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01002960 }
Damien429d7192013-10-04 19:53:11 +01002961}
2962
2963void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
2964 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00002965 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01002966 scope->num_locals = 0;
2967 for (int i = 0; i < scope->id_info_len; i++) {
2968 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002969 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01002970 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
2971 continue;
2972 }
2973 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
2974 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
2975 }
Damien9ecbcff2013-12-11 00:41:43 +00002976 // note: params always count for 1 local, even if they are a cell
Damien429d7192013-10-04 19:53:11 +01002977 if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
2978 id->local_num = scope->num_locals;
2979 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00002980 }
2981 }
2982
2983 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00002984#if MICROPY_EMIT_CPYTHON
2985 int num_cell = 0;
2986#endif
Damien9ecbcff2013-12-11 00:41:43 +00002987 for (int i = 0; i < scope->id_info_len; i++) {
2988 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00002989#if MICROPY_EMIT_CPYTHON
2990 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00002991 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00002992 id->local_num = num_cell;
2993 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00002994 }
Damien George6baf76e2013-12-30 22:32:17 +00002995#else
2996 // in Micro Python the cells come right after the fast locals
2997 // parameters are not counted here, since they remain at the start
2998 // of the locals, even if they are cell vars
2999 if (!id->param && id->kind == ID_INFO_KIND_CELL) {
3000 id->local_num = scope->num_locals;
3001 scope->num_locals += 1;
3002 }
3003#endif
Damien9ecbcff2013-12-11 00:41:43 +00003004 }
Damien9ecbcff2013-12-11 00:41:43 +00003005
3006 // compute the index of free vars (freevars[idx] in CPython)
3007 // make sure they are in the order of the parent scope
3008 if (scope->parent != NULL) {
3009 int num_free = 0;
3010 for (int i = 0; i < scope->parent->id_info_len; i++) {
3011 id_info_t *id = &scope->parent->id_info[i];
3012 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3013 for (int j = 0; j < scope->id_info_len; j++) {
3014 id_info_t *id2 = &scope->id_info[j];
3015 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +00003016 assert(!id2->param); // free vars should not be params
3017#if MICROPY_EMIT_CPYTHON
3018 // in CPython the frees are numbered after the cells
3019 id2->local_num = num_cell + num_free;
3020#else
3021 // in Micro Python the frees come first, before the params
3022 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003023#endif
3024 num_free += 1;
3025 }
3026 }
3027 }
Damien429d7192013-10-04 19:53:11 +01003028 }
Damien George6baf76e2013-12-30 22:32:17 +00003029#if !MICROPY_EMIT_CPYTHON
3030 // in Micro Python shift all other locals after the free locals
3031 if (num_free > 0) {
3032 for (int i = 0; i < scope->id_info_len; i++) {
3033 id_info_t *id = &scope->id_info[i];
3034 if (id->param || id->kind != ID_INFO_KIND_FREE) {
3035 id->local_num += num_free;
3036 }
3037 }
3038 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3039 scope->num_locals += num_free;
3040 }
3041#endif
Damien429d7192013-10-04 19:53:11 +01003042 }
3043
Damien George8725f8f2014-02-15 19:33:11 +00003044 // compute scope_flags
3045 //scope->scope_flags = 0; since we set some things in parameters
Damien429d7192013-10-04 19:53:11 +01003046 if (scope->kind != SCOPE_MODULE) {
Damien George8725f8f2014-02-15 19:33:11 +00003047 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien429d7192013-10-04 19:53:11 +01003048 }
3049 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) {
3050 assert(scope->parent != NULL);
Damien George8725f8f2014-02-15 19:33:11 +00003051 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien429d7192013-10-04 19:53:11 +01003052
3053 // TODO possibly other ways it can be nested
Damien George08d07552014-01-29 18:58:52 +00003054 // Note that we don't actually use this information at the moment (for CPython compat only)
3055 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 +00003056 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003057 }
3058 }
3059 int num_free = 0;
3060 for (int i = 0; i < scope->id_info_len; i++) {
3061 id_info_t *id = &scope->id_info[i];
3062 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3063 num_free += 1;
3064 }
3065 }
3066 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003067 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003068 }
3069}
3070
Damien George08335002014-01-18 23:24:36 +00003071mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
Damien429d7192013-10-04 19:53:11 +01003072 compiler_t *comp = m_new(compiler_t, 1);
3073
Damien Georgecbd2f742014-01-19 11:48:48 +00003074 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003075 comp->is_repl = is_repl;
3076 comp->had_error = false;
3077
Damien429d7192013-10-04 19:53:11 +01003078 comp->break_label = 0;
3079 comp->continue_label = 0;
Damien Georgecbddb272014-02-01 20:08:18 +00003080 comp->break_continue_except_level = 0;
3081 comp->cur_except_level = 0;
3082
Damien George35e2a4e2014-02-05 00:51:47 +00003083 comp->func_arg_is_super = false;
3084
Damien429d7192013-10-04 19:53:11 +01003085 comp->scope_head = NULL;
3086 comp->scope_cur = NULL;
3087
Damien826005c2013-10-05 23:17:28 +01003088 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003089 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003090
3091 // set the outer scope
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003092 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE);
Damien429d7192013-10-04 19:53:11 +01003093
Damien826005c2013-10-05 23:17:28 +01003094 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003095 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003096 comp->emit_method_table = &emit_pass1_method_table;
3097 comp->emit_inline_asm = NULL;
3098 comp->emit_inline_asm_method_table = NULL;
3099 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003100 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003101 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003102#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003103 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003104 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003105#endif
Damien826005c2013-10-05 23:17:28 +01003106 } else {
3107 compile_scope(comp, s, PASS_1);
3108 }
3109
3110 // update maximim number of labels needed
3111 if (comp->next_label > max_num_labels) {
3112 max_num_labels = comp->next_label;
3113 }
Damien429d7192013-10-04 19:53:11 +01003114 }
3115
Damien826005c2013-10-05 23:17:28 +01003116 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003117 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003118 compile_scope_compute_things(comp, s);
3119 }
3120
Damien826005c2013-10-05 23:17:28 +01003121 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003122 emit_pass1_free(comp->emit);
3123
Damien826005c2013-10-05 23:17:28 +01003124 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003125#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003126 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003127#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003128 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003129#endif
Damien3ef4abb2013-10-12 16:53:13 +01003130#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003131 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003132#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003133#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003134 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003135 if (false) {
3136 // dummy
3137
Damien3ef4abb2013-10-12 16:53:13 +01003138#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003139 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
3140 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003141 if (emit_inline_thumb == NULL) {
3142 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3143 }
3144 comp->emit = NULL;
3145 comp->emit_method_table = NULL;
3146 comp->emit_inline_asm = emit_inline_thumb;
3147 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3148 compile_scope_inline_asm(comp, s, PASS_2);
3149 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003150#endif
3151
Damien826005c2013-10-05 23:17:28 +01003152 } else {
Damienc025ebb2013-10-12 14:30:21 +01003153
3154 // choose the emit type
3155
Damien3ef4abb2013-10-12 16:53:13 +01003156#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003157 comp->emit = emit_cpython_new(max_num_labels);
3158 comp->emit_method_table = &emit_cpython_method_table;
3159#else
Damien826005c2013-10-05 23:17:28 +01003160 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003161
3162#if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003163 case EMIT_OPT_NATIVE_PYTHON:
Damien3410be82013-10-07 23:09:10 +01003164 case EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003165#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003166 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003167 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003168 }
Damien13ed3a62013-10-08 09:05:10 +01003169 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003170#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003171 if (emit_native == NULL) {
3172 emit_native = emit_native_thumb_new(max_num_labels);
3173 }
3174 comp->emit_method_table = &emit_native_thumb_method_table;
3175#endif
3176 comp->emit = emit_native;
Damien3410be82013-10-07 23:09:10 +01003177 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003178 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003179#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003180
Damien826005c2013-10-05 23:17:28 +01003181 default:
3182 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003183 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003184 }
3185 comp->emit = emit_bc;
3186 comp->emit_method_table = &emit_bc_method_table;
3187 break;
3188 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003189#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003190
3191 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003192 compile_scope(comp, s, PASS_2);
3193 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003194 }
Damien429d7192013-10-04 19:53:11 +01003195 }
3196
Damien George41d02b62014-01-24 22:42:28 +00003197 // free the emitters
3198#if !MICROPY_EMIT_CPYTHON
3199 if (emit_bc != NULL) {
3200 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003201 }
Damien George41d02b62014-01-24 22:42:28 +00003202#if MICROPY_EMIT_NATIVE
3203 if (emit_native != NULL) {
3204#if MICROPY_EMIT_X64
3205 emit_native_x64_free(emit_native);
3206#elif MICROPY_EMIT_THUMB
3207 emit_native_thumb_free(emit_native);
3208#endif
3209 }
3210#endif
3211#if MICROPY_EMIT_INLINE_THUMB
3212 if (emit_inline_thumb != NULL) {
3213 emit_inline_thumb_free(emit_inline_thumb);
3214 }
3215#endif
3216#endif // !MICROPY_EMIT_CPYTHON
3217
3218 // free the scopes
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003219 uint unique_code_id = module_scope->unique_code_id;
3220 for (scope_t *s = module_scope; s;) {
3221 scope_t *next = s->next;
3222 scope_free(s);
3223 s = next;
3224 }
Damien5ac1b2e2013-10-18 19:58:12 +01003225
Damien George41d02b62014-01-24 22:42:28 +00003226 // free the compiler
3227 bool had_error = comp->had_error;
3228 m_del_obj(compiler_t, comp);
3229
Damien George1fb03172014-01-03 14:22:03 +00003230 if (had_error) {
3231 // TODO return a proper error message
3232 return mp_const_none;
3233 } else {
3234#if MICROPY_EMIT_CPYTHON
3235 // can't create code, so just return true
Damien George41d02b62014-01-24 22:42:28 +00003236 (void)unique_code_id; // to suppress warning that unique_code_id is unused
Damien George1fb03172014-01-03 14:22:03 +00003237 return mp_const_true;
3238#else
3239 // return function that executes the outer module
Paul Sokolovsky90750022014-02-01 15:05:04 +02003240 return rt_make_function_from_id(unique_code_id, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003241#endif
3242 }
Damien429d7192013-10-04 19:53:11 +01003243}