blob: 5959a8cbc92f5c02137f639b0a043ee6d3322170 [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])) {
89 int arg0 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
90 int arg1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[2]);
91 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])) {
108 machine_int_t arg0 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
109 machine_int_t arg1 = MP_PARSE_NODE_LEAF_ARG(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 }
Damiend99b0522013-12-21 18:17:45 +0000120 if (MP_FIT_SMALL_INT(res)) {
121 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])) {
128 int arg0 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
129 int arg1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[2]);
130 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])) {
152 machine_int_t arg = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
153 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])) {
172 int power = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100173 if (power >= 0) {
174 int ans = 1;
Damiend99b0522013-12-21 18:17:45 +0000175 int base = MP_PARSE_NODE_LEAF_ARG(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
Damiend99b0522013-12-21 18:17:45 +0000191void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100192
Damienb05d7072013-10-05 13:37:10 +0100193static int comp_next_label(compiler_t *comp) {
194 return comp->next_label++;
195}
196
Damiend99b0522013-12-21 18:17:45 +0000197static 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 +0000198 scope_t *scope = scope_new(kind, pn, comp->source_file, rt_get_unique_code_id(), emit_options);
Damien429d7192013-10-04 19:53:11 +0100199 scope->parent = comp->scope_cur;
200 scope->next = NULL;
201 if (comp->scope_head == NULL) {
202 comp->scope_head = scope;
203 } else {
204 scope_t *s = comp->scope_head;
205 while (s->next != NULL) {
206 s = s->next;
207 }
208 s->next = scope;
209 }
210 return scope;
211}
212
Damiend99b0522013-12-21 18:17:45 +0000213static int list_len(mp_parse_node_t pn, int pn_kind) {
214 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100215 return 0;
Damiend99b0522013-12-21 18:17:45 +0000216 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100217 return 1;
218 } else {
Damiend99b0522013-12-21 18:17:45 +0000219 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
220 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100221 return 1;
222 } else {
Damiend99b0522013-12-21 18:17:45 +0000223 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100224 }
225 }
226}
227
Damiend99b0522013-12-21 18:17:45 +0000228static 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)) {
229 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
230 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
231 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100232 for (int i = 0; i < num_nodes; i++) {
233 f(comp, pns->nodes[i]);
234 }
Damiend99b0522013-12-21 18:17:45 +0000235 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100236 f(comp, pn);
237 }
238}
239
Damiend99b0522013-12-21 18:17:45 +0000240static int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
241 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100242 *nodes = NULL;
243 return 0;
Damiend99b0522013-12-21 18:17:45 +0000244 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100245 *nodes = pn;
246 return 1;
247 } else {
Damiend99b0522013-12-21 18:17:45 +0000248 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
249 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100250 *nodes = pn;
251 return 1;
252 } else {
253 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000254 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100255 }
256 }
257}
258
Damiend99b0522013-12-21 18:17:45 +0000259void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100260}
261
Damiend99b0522013-12-21 18:17:45 +0000262void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
263 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100264 for (int i = 0; i < num_nodes; i++) {
265 compile_node(comp, pns->nodes[i]);
266 }
267}
268
Damien3ef4abb2013-10-12 16:53:13 +0100269#if MICROPY_EMIT_CPYTHON
Damiend99b0522013-12-21 18:17:45 +0000270static bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
271 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100272 return false;
273 }
Damiend99b0522013-12-21 18:17:45 +0000274 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100275 return false;
276 }
277 return true;
278}
279
Damien02f89412013-12-12 15:13:36 +0000280static void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
Damien George55baff42014-01-21 21:40:13 +0000281 uint len;
282 const byte *str = qstr_data(qstr, &len);
Damien02f89412013-12-12 15:13:36 +0000283 bool has_single_quote = false;
284 bool has_double_quote = false;
285 for (int i = 0; i < len; i++) {
286 if (str[i] == '\'') {
287 has_single_quote = true;
288 } else if (str[i] == '"') {
289 has_double_quote = true;
290 }
291 }
292 if (bytes) {
293 vstr_printf(vstr, "b");
294 }
295 bool quote_single = false;
296 if (has_single_quote && !has_double_quote) {
297 vstr_printf(vstr, "\"");
298 } else {
299 quote_single = true;
300 vstr_printf(vstr, "'");
301 }
302 for (int i = 0; i < len; i++) {
303 if (str[i] == '\n') {
304 vstr_printf(vstr, "\\n");
305 } else if (str[i] == '\\') {
306 vstr_printf(vstr, "\\\\");
307 } else if (str[i] == '\'' && quote_single) {
308 vstr_printf(vstr, "\\'");
309 } else {
310 vstr_printf(vstr, "%c", str[i]);
311 }
312 }
313 if (has_single_quote && !has_double_quote) {
314 vstr_printf(vstr, "\"");
315 } else {
316 vstr_printf(vstr, "'");
317 }
318}
319
Damiend99b0522013-12-21 18:17:45 +0000320static void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
321 assert(MP_PARSE_NODE_IS_LEAF(pn));
322 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
323 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
324 case MP_PARSE_NODE_ID: assert(0);
325 case MP_PARSE_NODE_SMALL_INT: vstr_printf(vstr, "%d", arg); break;
326 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
327 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
328 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
329 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
330 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100331 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000332 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
333 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
334 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien429d7192013-10-04 19:53:11 +0100335 default: assert(0);
336 }
337 break;
338 default: assert(0);
339 }
340}
341
Damiend99b0522013-12-21 18:17:45 +0000342static 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 +0100343 int n = 0;
344 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000345 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100346 }
347 int total = n;
348 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000349 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100350 total += 1;
Damien3a205172013-10-12 15:01:56 +0100351 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100352 is_const = false;
353 }
354 }
355 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100356 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100357 is_const = false;
358 break;
359 }
360 }
361 if (total > 0 && is_const) {
362 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000363 vstr_t *vstr = vstr_new();
364 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000365 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000366 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100367 need_comma = true;
368 }
369 for (int i = 0; i < n; i++) {
370 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000371 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100372 }
Damien02f89412013-12-12 15:13:36 +0000373 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100374 need_comma = true;
375 }
376 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000377 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100378 } else {
Damien02f89412013-12-12 15:13:36 +0000379 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100380 }
Damien Georgeb9791222014-01-23 00:34:21 +0000381 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000382 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100383 } else {
Damiend99b0522013-12-21 18:17:45 +0000384 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100385 compile_node(comp, pn);
386 }
387 for (int i = 0; i < n; i++) {
388 compile_node(comp, pns_list->nodes[i]);
389 }
Damien Georgeb9791222014-01-23 00:34:21 +0000390 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100391 }
392}
Damien3a205172013-10-12 15:01:56 +0100393#endif
394
395// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damiend99b0522013-12-21 18:17:45 +0000396void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100397#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100398 cpython_c_tuple(comp, pn, pns_list);
399#else
400 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000401 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100402 compile_node(comp, pn);
403 total += 1;
404 }
405 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000406 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100407 for (int i = 0; i < n; i++) {
408 compile_node(comp, pns_list->nodes[i]);
409 }
410 total += n;
411 }
Damien Georgeb9791222014-01-23 00:34:21 +0000412 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100413#endif
414}
Damien429d7192013-10-04 19:53:11 +0100415
Damiend99b0522013-12-21 18:17:45 +0000416void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100417 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000418 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100419}
420
Damiend99b0522013-12-21 18:17:45 +0000421static bool node_is_const_false(mp_parse_node_t pn) {
422 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
423 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_ARG(pn) == 1);
Damien429d7192013-10-04 19:53:11 +0100424}
425
Damiend99b0522013-12-21 18:17:45 +0000426static bool node_is_const_true(mp_parse_node_t pn) {
427 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE) || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_ARG(pn) == 1);
Damien429d7192013-10-04 19:53:11 +0100428}
429
Damien3ef4abb2013-10-12 16:53:13 +0100430#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100431// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Damiend99b0522013-12-21 18:17:45 +0000432static 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 +0100433 if (node_is_const_false(pn)) {
434 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000435 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100436 }
437 return;
438 } else if (node_is_const_true(pn)) {
439 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000440 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100441 }
442 return;
Damiend99b0522013-12-21 18:17:45 +0000443 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
444 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
445 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
446 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100447 if (jump_if == false) {
Damienb05d7072013-10-05 13:37:10 +0100448 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100449 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100450 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100451 }
Damien3a205172013-10-12 15:01:56 +0100452 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000453 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100454 } else {
455 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100456 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100457 }
458 }
459 return;
Damiend99b0522013-12-21 18:17:45 +0000460 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100461 if (jump_if == false) {
462 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100463 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100464 }
465 } else {
Damienb05d7072013-10-05 13:37:10 +0100466 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100467 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100468 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100469 }
Damien3a205172013-10-12 15:01:56 +0100470 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000471 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100472 }
473 return;
Damiend99b0522013-12-21 18:17:45 +0000474 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100475 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100476 return;
477 }
478 }
479
480 // nothing special, fall back to default compiling for node and jump
481 compile_node(comp, pn);
482 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000483 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100484 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000485 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100486 }
487}
Damien3a205172013-10-12 15:01:56 +0100488#endif
Damien429d7192013-10-04 19:53:11 +0100489
Damiend99b0522013-12-21 18:17:45 +0000490static void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100491#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100492 cpython_c_if_cond(comp, pn, jump_if, label, false);
493#else
494 if (node_is_const_false(pn)) {
495 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000496 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100497 }
498 return;
499 } else if (node_is_const_true(pn)) {
500 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000501 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100502 }
503 return;
Damiend99b0522013-12-21 18:17:45 +0000504 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
505 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
506 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
507 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100508 if (jump_if == false) {
509 int label2 = comp_next_label(comp);
510 for (int i = 0; i < n - 1; i++) {
511 c_if_cond(comp, pns->nodes[i], true, label2);
512 }
513 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000514 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100515 } else {
516 for (int i = 0; i < n; i++) {
517 c_if_cond(comp, pns->nodes[i], true, label);
518 }
519 }
520 return;
Damiend99b0522013-12-21 18:17:45 +0000521 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100522 if (jump_if == false) {
523 for (int i = 0; i < n; i++) {
524 c_if_cond(comp, pns->nodes[i], false, label);
525 }
526 } else {
527 int label2 = comp_next_label(comp);
528 for (int i = 0; i < n - 1; i++) {
529 c_if_cond(comp, pns->nodes[i], false, label2);
530 }
531 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000532 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100533 }
534 return;
Damiend99b0522013-12-21 18:17:45 +0000535 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100536 c_if_cond(comp, pns->nodes[0], !jump_if, label);
537 return;
538 }
539 }
540
541 // nothing special, fall back to default compiling for node and jump
542 compile_node(comp, pn);
543 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000544 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100545 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000546 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100547 }
548#endif
Damien429d7192013-10-04 19:53:11 +0100549}
550
551typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000552void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100553
Damiend99b0522013-12-21 18:17:45 +0000554void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100555 if (assign_kind != ASSIGN_AUG_STORE) {
556 compile_node(comp, pns->nodes[0]);
557 }
558
Damiend99b0522013-12-21 18:17:45 +0000559 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
560 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
561 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
562 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100563 if (assign_kind != ASSIGN_AUG_STORE) {
564 for (int i = 0; i < n - 1; i++) {
565 compile_node(comp, pns1->nodes[i]);
566 }
567 }
Damiend99b0522013-12-21 18:17:45 +0000568 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
569 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100570 }
Damiend99b0522013-12-21 18:17:45 +0000571 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien429d7192013-10-04 19:53:11 +0100572 printf("SyntaxError: can't assign to function call\n");
573 return;
Damiend99b0522013-12-21 18:17:45 +0000574 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100575 if (assign_kind == ASSIGN_AUG_STORE) {
576 EMIT(rot_three);
577 EMIT(store_subscr);
578 } else {
579 compile_node(comp, pns1->nodes[0]);
580 if (assign_kind == ASSIGN_AUG_LOAD) {
581 EMIT(dup_top_two);
Damien Georgeb9791222014-01-23 00:34:21 +0000582 EMIT_ARG(binary_op, RT_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100583 } else {
584 EMIT(store_subscr);
585 }
586 }
Damiend99b0522013-12-21 18:17:45 +0000587 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
588 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100589 if (assign_kind == ASSIGN_AUG_LOAD) {
590 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000591 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100592 } else {
593 if (assign_kind == ASSIGN_AUG_STORE) {
594 EMIT(rot_two);
595 }
Damien Georgeb9791222014-01-23 00:34:21 +0000596 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100597 }
598 } else {
599 // shouldn't happen
600 assert(0);
601 }
602 } else {
603 // shouldn't happen
604 assert(0);
605 }
606
Damiend99b0522013-12-21 18:17:45 +0000607 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +0100608 // SyntaxError, cannot assign
609 assert(0);
610 }
611}
612
Damiend99b0522013-12-21 18:17:45 +0000613void c_assign_tuple(compiler_t *comp, int n, mp_parse_node_t *nodes) {
Damien429d7192013-10-04 19:53:11 +0100614 assert(n >= 0);
615 int have_star_index = -1;
616 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000617 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100618 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000619 EMIT_ARG(unpack_ex, i, n - i - 1);
Damien429d7192013-10-04 19:53:11 +0100620 have_star_index = i;
621 } else {
622 printf("SyntaxError: two starred expressions in assignment\n");
623 return;
624 }
625 }
626 }
627 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000628 EMIT_ARG(unpack_sequence, n);
Damien429d7192013-10-04 19:53:11 +0100629 }
630 for (int i = 0; i < n; i++) {
631 if (i == have_star_index) {
Damiend99b0522013-12-21 18:17:45 +0000632 c_assign(comp, ((mp_parse_node_struct_t*)nodes[i])->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100633 } else {
634 c_assign(comp, nodes[i], ASSIGN_STORE);
635 }
636 }
637}
638
639// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000640void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100641 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000642 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100643 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000644 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
645 if (MP_PARSE_NODE_IS_ID(pn)) {
646 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100647 switch (assign_kind) {
648 case ASSIGN_STORE:
649 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000650 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100651 break;
652 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000653 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100654 break;
655 }
656 } else {
657 printf("SyntaxError: can't assign to literal\n");
658 return;
659 }
660 } else {
Damiend99b0522013-12-21 18:17:45 +0000661 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
662 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100663 case PN_power:
664 // lhs is an index or attribute
665 c_assign_power(comp, pns, assign_kind);
666 break;
667
668 case PN_testlist_star_expr:
669 case PN_exprlist:
670 // lhs is a tuple
671 if (assign_kind != ASSIGN_STORE) {
672 goto bad_aug;
673 }
Damiend99b0522013-12-21 18:17:45 +0000674 c_assign_tuple(comp, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100675 break;
676
677 case PN_atom_paren:
678 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000679 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100680 // empty tuple
681 printf("SyntaxError: can't assign to ()\n");
682 return;
Damiend99b0522013-12-21 18:17:45 +0000683 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
684 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100685 goto testlist_comp;
686 } else {
687 // parenthesis around 1 item, is just that item
688 pn = pns->nodes[0];
689 goto tail_recursion;
690 }
691 break;
692
693 case PN_atom_bracket:
694 // lhs is something in brackets
695 if (assign_kind != ASSIGN_STORE) {
696 goto bad_aug;
697 }
Damiend99b0522013-12-21 18:17:45 +0000698 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100699 // empty list, assignment allowed
700 c_assign_tuple(comp, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000701 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
702 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100703 goto testlist_comp;
704 } else {
705 // brackets around 1 item
706 c_assign_tuple(comp, 1, &pns->nodes[0]);
707 }
708 break;
709
710 default:
Damiend99b0522013-12-21 18:17:45 +0000711 printf("unknown assign, %u\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien429d7192013-10-04 19:53:11 +0100712 assert(0);
713 }
714 return;
715
716 testlist_comp:
717 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000718 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
719 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
720 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100721 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000722 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100723 c_assign_tuple(comp, 1, &pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000724 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100725 // sequence of many items
726 // TODO call c_assign_tuple instead
Damiend99b0522013-12-21 18:17:45 +0000727 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
Damien Georgeb9791222014-01-23 00:34:21 +0000728 EMIT_ARG(unpack_sequence, 1 + n);
Damien429d7192013-10-04 19:53:11 +0100729 c_assign(comp, pns->nodes[0], ASSIGN_STORE);
730 for (int i = 0; i < n; i++) {
731 c_assign(comp, pns2->nodes[i], ASSIGN_STORE);
732 }
Damiend99b0522013-12-21 18:17:45 +0000733 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +0100734 // TODO not implemented
735 assert(0);
736 } else {
737 // sequence with 2 items
738 goto sequence_with_2_items;
739 }
740 } else {
741 // sequence with 2 items
742 sequence_with_2_items:
743 c_assign_tuple(comp, 2, pns->nodes);
744 }
745 return;
746 }
747 return;
748
749 bad_aug:
750 printf("SyntaxError: illegal expression for augmented assignment\n");
751}
752
753// stuff for lambda and comprehensions and generators
754void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_dict_params, int n_default_params) {
755 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000756 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100757 int nfree = 0;
758 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000759 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
760 id_info_t *id = &comp->scope_cur->id_info[i];
761 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
762 for (int j = 0; j < this_scope->id_info_len; j++) {
763 id_info_t *id2 = &this_scope->id_info[j];
764 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000765#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000766 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000767#else
768 // in Micro Python we load closures using LOAD_FAST
Damien Georgeb9791222014-01-23 00:34:21 +0000769 EMIT_ARG(load_fast, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000770#endif
Damien318aec62013-12-10 18:28:17 +0000771 nfree += 1;
772 }
773 }
Damien429d7192013-10-04 19:53:11 +0100774 }
775 }
776 }
777 if (nfree > 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000778 EMIT_ARG(build_tuple, nfree);
Damien429d7192013-10-04 19:53:11 +0100779 }
780
781 // make the function/closure
782 if (nfree == 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000783 EMIT_ARG(make_function, this_scope, n_dict_params, n_default_params);
Damien429d7192013-10-04 19:53:11 +0100784 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000785 EMIT_ARG(make_closure, this_scope, n_dict_params, n_default_params);
Damien429d7192013-10-04 19:53:11 +0100786 }
787}
788
Damiend99b0522013-12-21 18:17:45 +0000789void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
790 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
791 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
792 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +0100793 // this parameter has a default value
794 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
795 if (comp->have_bare_star) {
796 comp->param_pass_num_dict_params += 1;
797 if (comp->param_pass == 1) {
Damien Georgeb9791222014-01-23 00:34:21 +0000798 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100799 compile_node(comp, pns->nodes[2]);
800 }
801 } else {
802 comp->param_pass_num_default_params += 1;
803 if (comp->param_pass == 2) {
804 compile_node(comp, pns->nodes[2]);
805 }
806 }
807 }
Damiend99b0522013-12-21 18:17:45 +0000808 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
809 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
810 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100811 // bare star
812 comp->have_bare_star = true;
813 }
814 }
815}
816
817// leaves function object on stack
818// returns function name
Damiend99b0522013-12-21 18:17:45 +0000819qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100820 if (comp->pass == PASS_1) {
821 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000822 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100823 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000824 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100825 }
826
827 // save variables (probably don't need to do this, since we can't have nested definitions..?)
828 bool old_have_bare_star = comp->have_bare_star;
829 int old_param_pass = comp->param_pass;
830 int old_param_pass_num_dict_params = comp->param_pass_num_dict_params;
831 int old_param_pass_num_default_params = comp->param_pass_num_default_params;
832
833 // compile default parameters
834 comp->have_bare_star = false;
835 comp->param_pass = 1; // pass 1 does any default parameters after bare star
836 comp->param_pass_num_dict_params = 0;
837 comp->param_pass_num_default_params = 0;
838 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
839 comp->have_bare_star = false;
840 comp->param_pass = 2; // pass 2 does any default parameters before 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
845 // get the scope for this function
846 scope_t *fscope = (scope_t*)pns->nodes[4];
847
848 // make the function
849 close_over_variables_etc(comp, fscope, comp->param_pass_num_dict_params, comp->param_pass_num_default_params);
850
851 // restore variables
852 comp->have_bare_star = old_have_bare_star;
853 comp->param_pass = old_param_pass;
854 comp->param_pass_num_dict_params = old_param_pass_num_dict_params;
855 comp->param_pass_num_default_params = old_param_pass_num_default_params;
856
857 // return its name (the 'f' in "def f(...):")
858 return fscope->simple_name;
859}
860
861// leaves class object on stack
862// returns class name
Damiend99b0522013-12-21 18:17:45 +0000863qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100864 if (comp->pass == PASS_1) {
865 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000866 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100867 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000868 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100869 }
870
871 EMIT(load_build_class);
872
873 // scope for this class
874 scope_t *cscope = (scope_t*)pns->nodes[3];
875
876 // compile the class
877 close_over_variables_etc(comp, cscope, 0, 0);
878
879 // get its name
Damien Georgeb9791222014-01-23 00:34:21 +0000880 EMIT_ARG(load_const_id, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100881
882 // nodes[1] has parent classes, if any
Damiend99b0522013-12-21 18:17:45 +0000883 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +0100884 // no parent classes
Damien Georgeb9791222014-01-23 00:34:21 +0000885 EMIT_ARG(call_function, 2, 0, false, false);
Damien429d7192013-10-04 19:53:11 +0100886 } else {
887 // have a parent class or classes
888 // TODO what if we have, eg, *a or **a in the parent list?
889 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +0000890 EMIT_ARG(call_function, 2 + list_len(pns->nodes[1], PN_arglist), 0, false, false);
Damien429d7192013-10-04 19:53:11 +0100891 }
892
893 // return its name (the 'C' in class C(...):")
894 return cscope->simple_name;
895}
896
Damien6cdd3af2013-10-05 18:08:26 +0100897// returns true if it was a built-in decorator (even if the built-in had an error)
Damiend99b0522013-12-21 18:17:45 +0000898static 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 +0000899 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100900 return false;
901 }
902
903 if (name_len != 2) {
904 printf("SyntaxError: invalid micropython decorator\n");
905 return true;
906 }
907
Damiend99b0522013-12-21 18:17:45 +0000908 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000909 if (attr == MP_QSTR_byte_code) {
Damien5ac1b2e2013-10-18 19:58:12 +0100910 *emit_options = EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +0100911#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000912 } else if (attr == MP_QSTR_native) {
Damien6cdd3af2013-10-05 18:08:26 +0100913 *emit_options = EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000914 } else if (attr == MP_QSTR_viper) {
Damien7af3d192013-10-07 00:02:49 +0100915 *emit_options = EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100916#endif
Damien3ef4abb2013-10-12 16:53:13 +0100917#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000918 } else if (attr == MP_QSTR_asm_thumb) {
Damien5bfb7592013-10-05 18:41:24 +0100919 *emit_options = EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +0100920#endif
Damien6cdd3af2013-10-05 18:08:26 +0100921 } else {
Damience89a212013-10-15 22:25:17 +0100922 printf("SyntaxError: invalid micropython decorator '%s'\n", qstr_str(attr));
Damien6cdd3af2013-10-05 18:08:26 +0100923 }
924
925 return true;
926}
927
Damiend99b0522013-12-21 18:17:45 +0000928void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100929 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +0000930 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +0100931 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
932
Damien6cdd3af2013-10-05 18:08:26 +0100933 // inherit emit options for this function/class definition
934 uint emit_options = comp->scope_cur->emit_options;
935
936 // compile each decorator
937 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +0100938 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000939 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
940 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +0100941
942 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +0000943 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +0100944 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
945
946 // check for built-in decorators
947 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
948 // this was a built-in
949 num_built_in_decorators += 1;
950
951 } else {
952 // not a built-in, compile normally
953
954 // compile the decorator function
955 compile_node(comp, name_nodes[0]);
956 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +0000957 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +0000958 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +0100959 }
960
961 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +0000962 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +0100963 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +0000964 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +0100965 compile_node(comp, pns_decorator->nodes[1]);
966 }
Damien429d7192013-10-04 19:53:11 +0100967 }
968 }
969
970 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +0000971 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +0100972 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +0000973 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +0100974 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +0000975 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +0100976 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +0100977 } else {
978 // shouldn't happen
979 assert(0);
980 }
981
982 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +0100983 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +0000984 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +0100985 }
986
987 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +0000988 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +0100989}
990
Damiend99b0522013-12-21 18:17:45 +0000991void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +0100992 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +0100993 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +0000994 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +0100995}
996
Damiend99b0522013-12-21 18:17:45 +0000997void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
998 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +0000999 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001000 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1001 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001002
1003 compile_node(comp, pns->nodes[0]); // base of the power node
1004
Damiend99b0522013-12-21 18:17:45 +00001005 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1006 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1007 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1008 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001009 for (int i = 0; i < n - 1; i++) {
1010 compile_node(comp, pns1->nodes[i]);
1011 }
Damiend99b0522013-12-21 18:17:45 +00001012 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1013 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001014 }
Damiend99b0522013-12-21 18:17:45 +00001015 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien429d7192013-10-04 19:53:11 +01001016 // SyntaxError: can't delete a function call
1017 assert(0);
Damiend99b0522013-12-21 18:17:45 +00001018 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001019 compile_node(comp, pns1->nodes[0]);
1020 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001021 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1022 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001023 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001024 } else {
1025 // shouldn't happen
1026 assert(0);
1027 }
1028 } else {
1029 // shouldn't happen
1030 assert(0);
1031 }
1032
Damiend99b0522013-12-21 18:17:45 +00001033 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001034 // SyntaxError, cannot delete
1035 assert(0);
1036 }
Damiend99b0522013-12-21 18:17:45 +00001037 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1038 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1039 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1040 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001041 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1042
Damiend99b0522013-12-21 18:17:45 +00001043 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1044 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1045 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001046 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001047 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001048 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001049 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001050 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001051 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001052 c_del_stmt(comp, pns->nodes[0]);
1053 for (int i = 0; i < n; i++) {
1054 c_del_stmt(comp, pns1->nodes[i]);
1055 }
Damiend99b0522013-12-21 18:17:45 +00001056 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001057 // TODO not implemented; can't del comprehension?
1058 assert(0);
1059 } else {
1060 // sequence with 2 items
1061 goto sequence_with_2_items;
1062 }
1063 } else {
1064 // sequence with 2 items
1065 sequence_with_2_items:
1066 c_del_stmt(comp, pns->nodes[0]);
1067 c_del_stmt(comp, pns->nodes[1]);
1068 }
1069 } else {
1070 // tuple with 1 element
1071 c_del_stmt(comp, pn);
1072 }
1073 } else {
1074 // not implemented
1075 assert(0);
1076 }
1077}
1078
Damiend99b0522013-12-21 18:17:45 +00001079void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001080 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1081}
1082
Damiend99b0522013-12-21 18:17:45 +00001083void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001084 if (comp->break_label == 0) {
1085 printf("ERROR: cannot break from here\n");
1086 }
Damien Georgecbddb272014-02-01 20:08:18 +00001087 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001088}
1089
Damiend99b0522013-12-21 18:17:45 +00001090void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001091 if (comp->continue_label == 0) {
1092 printf("ERROR: cannot continue from here\n");
1093 }
Damien Georgecbddb272014-02-01 20:08:18 +00001094 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001095}
1096
Damiend99b0522013-12-21 18:17:45 +00001097void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001098 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
1099 printf("SyntaxError: 'return' outside function\n");
1100 comp->had_error = true;
1101 return;
1102 }
Damiend99b0522013-12-21 18:17:45 +00001103 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001104 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001105 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001106 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001107 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001108 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1109 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 +01001110
Damienb05d7072013-10-05 13:37:10 +01001111 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001112 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1113 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1114 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001115 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001116 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1117 } else {
1118 compile_node(comp, pns->nodes[0]);
1119 }
1120 EMIT(return_value);
1121}
1122
Damiend99b0522013-12-21 18:17:45 +00001123void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001124 compile_node(comp, pns->nodes[0]);
1125 EMIT(pop_top);
1126}
1127
Damiend99b0522013-12-21 18:17:45 +00001128void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1129 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001130 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001131 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001132 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001133 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001134 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001135 compile_node(comp, pns->nodes[0]);
1136 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001137 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001138 } else {
1139 // raise x
1140 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001141 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001142 }
1143}
1144
1145// q1 holds the base, q2 the full name
1146// eg a -> q1=q2=a
1147// a.b.c -> q1=a, q2=a.b.c
Damiend99b0522013-12-21 18:17:45 +00001148void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q1, qstr *q2) {
Damien429d7192013-10-04 19:53:11 +01001149 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001150 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1151 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001152 // a name of the form x as y; unwrap it
Damiend99b0522013-12-21 18:17:45 +00001153 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001154 pn = pns->nodes[0];
1155 is_as = true;
1156 }
Damiend99b0522013-12-21 18:17:45 +00001157 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001158 // just a simple name
Damiend99b0522013-12-21 18:17:45 +00001159 *q2 = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001160 if (!is_as) {
1161 *q1 = *q2;
1162 }
Damien Georgeb9791222014-01-23 00:34:21 +00001163 EMIT_ARG(import_name, *q2);
Damiend99b0522013-12-21 18:17:45 +00001164 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1165 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1166 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001167 // a name of the form a.b.c
1168 if (!is_as) {
Damiend99b0522013-12-21 18:17:45 +00001169 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001170 }
Damiend99b0522013-12-21 18:17:45 +00001171 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001172 int len = n - 1;
1173 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001174 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001175 }
Damien George55baff42014-01-21 21:40:13 +00001176 byte *q_ptr;
1177 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001178 for (int i = 0; i < n; i++) {
1179 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001180 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001181 }
Damien George55baff42014-01-21 21:40:13 +00001182 uint str_src_len;
1183 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001184 memcpy(str_dest, str_src, str_src_len);
1185 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001186 }
Damien George55baff42014-01-21 21:40:13 +00001187 *q2 = qstr_build_end(q_ptr);
Damien Georgeb9791222014-01-23 00:34:21 +00001188 EMIT_ARG(import_name, *q2);
Damien429d7192013-10-04 19:53:11 +01001189 if (is_as) {
1190 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001191 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001192 }
1193 }
1194 } else {
1195 // TODO not implemented
1196 assert(0);
1197 }
1198 } else {
1199 // TODO not implemented
1200 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 Sokolovsky899c69f2014-01-10 20:38:57 +02001471 if (MP_PARSE_NODE_LEAF_ARG(pn_step) >= 0) {
Damien George9aa2a522014-02-01 23:04:09 +00001472 EMIT_ARG(binary_op, RT_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001473 } else {
Damien George9aa2a522014-02-01 23:04:09 +00001474 EMIT_ARG(binary_op, RT_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001475 }
Damien Georgeb9791222014-01-23 00:34:21 +00001476 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001477
1478 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001479 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001480
1481 compile_node(comp, pn_else);
1482
Damien Georgeb9791222014-01-23 00:34:21 +00001483 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001484}
1485
Damiend99b0522013-12-21 18:17:45 +00001486void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001487#if !MICROPY_EMIT_CPYTHON
1488 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1489 // this is actually slower, but uses no heap memory
1490 // for viper it will be much, much faster
Damiend99b0522013-12-21 18:17:45 +00001491 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)) {
1492 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001493 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1494 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1495 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1496 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001497 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1498 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001499 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001500 mp_parse_node_t pn_range_start;
1501 mp_parse_node_t pn_range_end;
1502 mp_parse_node_t pn_range_step;
1503 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001504 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001505 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001506 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001507 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001508 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001509 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001510 } else if (n_args == 2) {
1511 pn_range_start = args[0];
1512 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001513 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001514 } else {
1515 pn_range_start = args[0];
1516 pn_range_end = args[1];
1517 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001518 // We need to know sign of step. This is possible only if it's constant
1519 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1520 optimize = false;
1521 }
Damienf72fd0e2013-11-06 20:20:49 +00001522 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001523 }
1524 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001525 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1526 return;
1527 }
1528 }
1529 }
1530#endif
1531
Damien Georgecbddb272014-02-01 20:08:18 +00001532 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001533
Damienb05d7072013-10-05 13:37:10 +01001534 int pop_label = comp_next_label(comp);
1535 int end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001536
Damience89a212013-10-15 22:25:17 +01001537 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1538#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001539 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001540#endif
1541
Damien429d7192013-10-04 19:53:11 +01001542 compile_node(comp, pns->nodes[1]); // iterator
1543 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001544 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001545 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001546 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1547 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001548 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001549 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001550 }
Damien Georgeb9791222014-01-23 00:34:21 +00001551 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001552 EMIT(for_iter_end);
1553
1554 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001555 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001556
Damience89a212013-10-15 22:25:17 +01001557#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001558 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001559#endif
Damien429d7192013-10-04 19:53:11 +01001560
1561 compile_node(comp, pns->nodes[3]); // else (not tested)
1562
Damien Georgeb9791222014-01-23 00:34:21 +00001563 EMIT_ARG(label_assign, break_label);
1564 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001565}
1566
Damiend99b0522013-12-21 18:17:45 +00001567void 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 +01001568 // this function is a bit of a hack at the moment
1569 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1570
1571 // setup code
1572 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001573 int l1 = comp_next_label(comp);
1574 int success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001575
Damien Georgeb9791222014-01-23 00:34:21 +00001576 EMIT_ARG(setup_except, l1);
Damien Georgecbddb272014-02-01 20:08:18 +00001577 comp->cur_except_level += 1;
1578
Damien429d7192013-10-04 19:53:11 +01001579 compile_node(comp, pn_body); // body
1580 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001581 EMIT_ARG(jump, success_label);
1582 EMIT_ARG(label_assign, l1);
Damienb05d7072013-10-05 13:37:10 +01001583 int l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001584
1585 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001586 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1587 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001588
1589 qstr qstr_exception_local = 0;
Damienb05d7072013-10-05 13:37:10 +01001590 int end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001591
Damiend99b0522013-12-21 18:17:45 +00001592 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001593 // this is a catch all exception handler
1594 if (i + 1 != n_except) {
1595 printf("SyntaxError: default 'except:' must be last\n");
1596 return;
1597 }
1598 } else {
1599 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001600 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1601 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1602 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1603 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001604 // handler binds the exception to a local
1605 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001606 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001607 }
1608 }
1609 EMIT(dup_top);
1610 compile_node(comp, pns_exception_expr);
Damien George9aa2a522014-02-01 23:04:09 +00001611 EMIT_ARG(binary_op, RT_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001612 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001613 }
1614
1615 EMIT(pop_top);
1616
1617 if (qstr_exception_local == 0) {
1618 EMIT(pop_top);
1619 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001620 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001621 }
1622
1623 EMIT(pop_top);
1624
Damiene2880aa2013-12-20 14:22:59 +00001625 int l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001626 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001627 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001628 EMIT_ARG(setup_finally, l3);
Damien Georgecbddb272014-02-01 20:08:18 +00001629 comp->cur_except_level += 1;
Damien429d7192013-10-04 19:53:11 +01001630 }
1631 compile_node(comp, pns_except->nodes[1]);
1632 if (qstr_exception_local != 0) {
1633 EMIT(pop_block);
1634 }
1635 EMIT(pop_except);
1636 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001637 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1638 EMIT_ARG(label_assign, l3);
1639 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1640 EMIT_ARG(store_id, qstr_exception_local);
1641 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001642
1643 comp->cur_except_level -= 1;
Damien429d7192013-10-04 19:53:11 +01001644 EMIT(end_finally);
1645 }
Damien Georgeb9791222014-01-23 00:34:21 +00001646 EMIT_ARG(jump, l2);
1647 EMIT_ARG(label_assign, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001648 }
1649
Damien Georgecbddb272014-02-01 20:08:18 +00001650 comp->cur_except_level -= 1;
Damien429d7192013-10-04 19:53:11 +01001651 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001652
Damien Georgeb9791222014-01-23 00:34:21 +00001653 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001654 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001655 EMIT_ARG(label_assign, l2);
1656 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001657}
1658
Damiend99b0522013-12-21 18:17:45 +00001659void 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 +01001660 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1661 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001662 int l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001663
Damien Georgeb9791222014-01-23 00:34:21 +00001664 EMIT_ARG(setup_finally, l_finally_block);
Damien Georgecbddb272014-02-01 20:08:18 +00001665 comp->cur_except_level += 1;
1666
Damien429d7192013-10-04 19:53:11 +01001667 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001668 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien429d7192013-10-04 19:53:11 +01001669 compile_node(comp, pn_body);
1670 } else {
1671 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1672 }
1673 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001674 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1675 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001676 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001677
1678 comp->cur_except_level -= 1;
Damien429d7192013-10-04 19:53:11 +01001679 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001680
Damien Georgeb9791222014-01-23 00:34:21 +00001681 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001682}
1683
Damiend99b0522013-12-21 18:17:45 +00001684void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1685 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1686 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1687 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001688 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001689 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1690 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001691 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001692 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001693 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001694 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001695 // no finally
1696 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1697 } else {
1698 // have finally
Damiend99b0522013-12-21 18:17:45 +00001699 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 +01001700 }
1701 } else {
1702 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001703 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001704 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001705 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001706 }
1707 } else {
1708 // shouldn't happen
1709 assert(0);
1710 }
1711}
1712
Damiend99b0522013-12-21 18:17:45 +00001713void 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 +01001714 if (n == 0) {
1715 // no more pre-bits, compile the body of the with
1716 compile_node(comp, body);
1717 } else {
Damienb05d7072013-10-05 13:37:10 +01001718 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001719 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001720 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001721 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001722 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001723 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001724 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1725 } else {
1726 // this pre-bit is just an expression
1727 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001728 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001729 EMIT(pop_top);
1730 }
1731 // compile additional pre-bits and the body
1732 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1733 // finish this with block
1734 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001735 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1736 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001737 EMIT(with_cleanup);
1738 EMIT(end_finally);
1739 }
1740}
1741
Damiend99b0522013-12-21 18:17:45 +00001742void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001743 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001744 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001745 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1746 assert(n > 0);
1747
1748 // compile in a nested fashion
1749 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1750}
1751
Damiend99b0522013-12-21 18:17:45 +00001752void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1753 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001754 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1755 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001756 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001757 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001758 EMIT_ARG(call_function, 1, 0, false, false);
Damien5ac1b2e2013-10-18 19:58:12 +01001759 EMIT(pop_top);
1760
Damien429d7192013-10-04 19:53:11 +01001761 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001762 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001763 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001764 // do nothing with a lonely constant
1765 } else {
1766 compile_node(comp, pns->nodes[0]); // just an expression
1767 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1768 }
Damien429d7192013-10-04 19:53:11 +01001769 }
1770 } else {
Damiend99b0522013-12-21 18:17:45 +00001771 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1772 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001773 if (kind == PN_expr_stmt_augassign) {
1774 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1775 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001776 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien George7e5fb242014-02-01 22:18:47 +00001777 rt_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001778 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien George7e5fb242014-02-01 22:18:47 +00001779 case MP_TOKEN_DEL_PIPE_EQUAL: op = RT_BINARY_OP_INPLACE_OR; break;
1780 case MP_TOKEN_DEL_CARET_EQUAL: op = RT_BINARY_OP_INPLACE_XOR; break;
1781 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = RT_BINARY_OP_INPLACE_AND; break;
1782 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = RT_BINARY_OP_INPLACE_LSHIFT; break;
1783 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = RT_BINARY_OP_INPLACE_RSHIFT; break;
1784 case MP_TOKEN_DEL_PLUS_EQUAL: op = RT_BINARY_OP_INPLACE_ADD; break;
1785 case MP_TOKEN_DEL_MINUS_EQUAL: op = RT_BINARY_OP_INPLACE_SUBTRACT; break;
1786 case MP_TOKEN_DEL_STAR_EQUAL: op = RT_BINARY_OP_INPLACE_MULTIPLY; break;
1787 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = RT_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1788 case MP_TOKEN_DEL_SLASH_EQUAL: op = RT_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1789 case MP_TOKEN_DEL_PERCENT_EQUAL: op = RT_BINARY_OP_INPLACE_MODULO; break;
1790 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = RT_BINARY_OP_INPLACE_POWER; break;
1791 default: assert(0); op = RT_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01001792 }
Damien George7e5fb242014-02-01 22:18:47 +00001793 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001794 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1795 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001796 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1797 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001798 // following CPython, we store left-most first
1799 if (rhs > 0) {
1800 EMIT(dup_top);
1801 }
1802 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1803 for (int i = 0; i < rhs; i++) {
1804 if (i + 1 < rhs) {
1805 EMIT(dup_top);
1806 }
Damiend99b0522013-12-21 18:17:45 +00001807 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001808 }
1809 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001810 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1811 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1812 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1813 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01001814 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001815 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1816 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001817 compile_node(comp, pns10->nodes[0]); // rhs
1818 compile_node(comp, pns10->nodes[1]); // rhs
1819 EMIT(rot_two);
1820 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1821 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00001822 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1823 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1824 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1825 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01001826 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001827 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1828 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001829 compile_node(comp, pns10->nodes[0]); // rhs
1830 compile_node(comp, pns10->nodes[1]); // rhs
1831 compile_node(comp, pns10->nodes[2]); // rhs
1832 EMIT(rot_three);
1833 EMIT(rot_two);
1834 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1835 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1836 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1837 } else {
1838 compile_node(comp, pns1->nodes[0]); // rhs
1839 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1840 }
1841 } else {
1842 // shouldn't happen
1843 assert(0);
1844 }
1845 }
1846}
1847
Damiend99b0522013-12-21 18:17:45 +00001848void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, rt_binary_op_t binary_op) {
1849 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001850 compile_node(comp, pns->nodes[0]);
1851 for (int i = 1; i < num_nodes; i += 1) {
1852 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001853 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001854 }
1855}
1856
Damiend99b0522013-12-21 18:17:45 +00001857void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1858 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1859 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001860
1861 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001862 int l_fail = comp_next_label(comp);
1863 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001864 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1865 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001866 EMIT_ARG(jump, l_end);
1867 EMIT_ARG(label_assign, l_fail);
1868 EMIT_ARG(set_stack_size, stack_size); // force stack size reset
Damien429d7192013-10-04 19:53:11 +01001869 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001870 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001871}
1872
Damiend99b0522013-12-21 18:17:45 +00001873void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001874 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00001875 //mp_parse_node_t pn_params = pns->nodes[0];
1876 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001877
1878 if (comp->pass == PASS_1) {
1879 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00001880 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 +01001881 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001882 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001883 }
1884
1885 // get the scope for this lambda
1886 scope_t *this_scope = (scope_t*)pns->nodes[2];
1887
1888 // make the lambda
1889 close_over_variables_etc(comp, this_scope, 0, 0);
1890}
1891
Damiend99b0522013-12-21 18:17:45 +00001892void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001893 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001894 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001895 for (int i = 0; i < n; i += 1) {
1896 compile_node(comp, pns->nodes[i]);
1897 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00001898 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01001899 }
1900 }
Damien Georgeb9791222014-01-23 00:34:21 +00001901 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001902}
1903
Damiend99b0522013-12-21 18:17:45 +00001904void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001905 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001906 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001907 for (int i = 0; i < n; i += 1) {
1908 compile_node(comp, pns->nodes[i]);
1909 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00001910 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01001911 }
1912 }
Damien Georgeb9791222014-01-23 00:34:21 +00001913 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001914}
1915
Damiend99b0522013-12-21 18:17:45 +00001916void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001917 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001918 EMIT_ARG(unary_op, RT_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01001919}
1920
Damiend99b0522013-12-21 18:17:45 +00001921void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001922 int stack_size = EMIT(get_stack_size);
Damiend99b0522013-12-21 18:17:45 +00001923 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001924 compile_node(comp, pns->nodes[0]);
1925 bool multi = (num_nodes > 3);
1926 int l_fail = 0;
1927 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001928 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001929 }
1930 for (int i = 1; i + 1 < num_nodes; i += 2) {
1931 compile_node(comp, pns->nodes[i + 1]);
1932 if (i + 2 < num_nodes) {
1933 EMIT(dup_top);
1934 EMIT(rot_three);
1935 }
Damien George7e5fb242014-02-01 22:18:47 +00001936 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
1937 rt_binary_op_t op;
1938 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien George9aa2a522014-02-01 23:04:09 +00001939 case MP_TOKEN_OP_LESS: op = RT_BINARY_OP_LESS; break;
1940 case MP_TOKEN_OP_MORE: op = RT_BINARY_OP_MORE; break;
1941 case MP_TOKEN_OP_DBL_EQUAL: op = RT_BINARY_OP_EQUAL; break;
1942 case MP_TOKEN_OP_LESS_EQUAL: op = RT_BINARY_OP_LESS_EQUAL; break;
1943 case MP_TOKEN_OP_MORE_EQUAL: op = RT_BINARY_OP_MORE_EQUAL; break;
1944 case MP_TOKEN_OP_NOT_EQUAL: op = RT_BINARY_OP_NOT_EQUAL; break;
1945 case MP_TOKEN_KW_IN: op = RT_BINARY_OP_IN; break;
1946 default: assert(0); op = RT_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00001947 }
1948 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00001949 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
1950 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
1951 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01001952 if (kind == PN_comp_op_not_in) {
Damien George9aa2a522014-02-01 23:04:09 +00001953 EMIT_ARG(binary_op, RT_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01001954 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00001955 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien George9aa2a522014-02-01 23:04:09 +00001956 EMIT_ARG(binary_op, RT_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01001957 } else {
Damien George9aa2a522014-02-01 23:04:09 +00001958 EMIT_ARG(binary_op, RT_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01001959 }
1960 } else {
1961 // shouldn't happen
1962 assert(0);
1963 }
1964 } else {
1965 // shouldn't happen
1966 assert(0);
1967 }
1968 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00001969 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01001970 }
1971 }
1972 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001973 int l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001974 EMIT_ARG(jump, l_end);
1975 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001976 EMIT(rot_two);
1977 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00001978 EMIT_ARG(label_assign, l_end);
1979 EMIT_ARG(set_stack_size, stack_size + 1); // force stack size
Damien429d7192013-10-04 19:53:11 +01001980 }
1981}
1982
Damiend99b0522013-12-21 18:17:45 +00001983void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001984 // TODO
1985 assert(0);
1986 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001987 //EMIT_ARG(unary_op, "UNARY_STAR");
Damien429d7192013-10-04 19:53:11 +01001988}
1989
Damiend99b0522013-12-21 18:17:45 +00001990void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001991 c_binary_op(comp, pns, RT_BINARY_OP_OR);
1992}
1993
Damiend99b0522013-12-21 18:17:45 +00001994void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001995 c_binary_op(comp, pns, RT_BINARY_OP_XOR);
1996}
1997
Damiend99b0522013-12-21 18:17:45 +00001998void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001999 c_binary_op(comp, pns, RT_BINARY_OP_AND);
2000}
2001
Damiend99b0522013-12-21 18:17:45 +00002002void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2003 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002004 compile_node(comp, pns->nodes[0]);
2005 for (int i = 1; i + 1 < num_nodes; i += 2) {
2006 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002007 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002008 EMIT_ARG(binary_op, RT_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002009 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002010 EMIT_ARG(binary_op, RT_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002011 } else {
2012 // shouldn't happen
2013 assert(0);
2014 }
2015 }
2016}
2017
Damiend99b0522013-12-21 18:17:45 +00002018void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2019 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002020 compile_node(comp, pns->nodes[0]);
2021 for (int i = 1; i + 1 < num_nodes; i += 2) {
2022 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002023 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002024 EMIT_ARG(binary_op, RT_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002025 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002026 EMIT_ARG(binary_op, RT_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002027 } else {
2028 // shouldn't happen
2029 assert(0);
2030 }
2031 }
2032}
2033
Damiend99b0522013-12-21 18:17:45 +00002034void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2035 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002036 compile_node(comp, pns->nodes[0]);
2037 for (int i = 1; i + 1 < num_nodes; i += 2) {
2038 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002039 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002040 EMIT_ARG(binary_op, RT_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002041 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002042 EMIT_ARG(binary_op, RT_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002043 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002044 EMIT_ARG(binary_op, RT_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002045 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002046 EMIT_ARG(binary_op, RT_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002047 } else {
2048 // shouldn't happen
2049 assert(0);
2050 }
2051 }
2052}
2053
Damiend99b0522013-12-21 18:17:45 +00002054void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002055 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002056 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002057 EMIT_ARG(unary_op, RT_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002058 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002059 EMIT_ARG(unary_op, RT_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002060 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002061 EMIT_ARG(unary_op, RT_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002062 } else {
2063 // shouldn't happen
2064 assert(0);
2065 }
2066}
2067
Damien George35e2a4e2014-02-05 00:51:47 +00002068void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2069 // this is to handle special super() call
2070 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2071
2072 compile_generic_all_nodes(comp, pns);
2073}
2074
Damiend99b0522013-12-21 18:17:45 +00002075void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_struct_t *pns, bool is_method_call) {
Damien429d7192013-10-04 19:53:11 +01002076 // function to call is on top of stack
2077
Damien George35e2a4e2014-02-05 00:51:47 +00002078#if !MICROPY_EMIT_CPYTHON
2079 // this is to handle special super() call
2080 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0]) && comp->func_arg_is_super && comp->scope_cur->kind == SCOPE_FUNCTION) {
2081 EMIT_ARG(load_id, MP_QSTR___class__);
2082 // get first argument to function
2083 bool found = false;
2084 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
2085 if (comp->scope_cur->id_info[i].param) {
2086 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
2087 found = true;
2088 break;
2089 }
2090 }
2091 if (!found) {
2092 printf("TypeError: super() call cannot find self\n");
2093 return;
2094 }
2095 EMIT_ARG(call_function, 2, 0, false, false);
2096 return;
2097 }
2098#endif
2099
Damien429d7192013-10-04 19:53:11 +01002100 int old_n_arg_keyword = comp->n_arg_keyword;
2101 bool old_have_star_arg = comp->have_star_arg;
2102 bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
2103 comp->n_arg_keyword = 0;
2104 comp->have_star_arg = false;
2105 comp->have_dbl_star_arg = false;
2106
2107 compile_node(comp, pns->nodes[0]); // arguments to function call; can be null
2108
2109 // compute number of positional arguments
2110 int n_positional = list_len(pns->nodes[0], PN_arglist) - comp->n_arg_keyword;
2111 if (comp->have_star_arg) {
2112 n_positional -= 1;
2113 }
2114 if (comp->have_dbl_star_arg) {
2115 n_positional -= 1;
2116 }
2117
2118 if (is_method_call) {
Damien Georgeb9791222014-01-23 00:34:21 +00002119 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 +01002120 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002121 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 +01002122 }
2123
2124 comp->n_arg_keyword = old_n_arg_keyword;
2125 comp->have_star_arg = old_have_star_arg;
2126 comp->have_dbl_star_arg = old_have_dbl_star_arg;
2127}
2128
Damiend99b0522013-12-21 18:17:45 +00002129void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2130 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002131 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002132 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 +01002133 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002134 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2135 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002136 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien429d7192013-10-04 19:53:11 +01002137 compile_trailer_paren_helper(comp, pns_paren, true);
2138 i += 1;
2139 } else {
2140 compile_node(comp, pns->nodes[i]);
2141 }
Damien George35e2a4e2014-02-05 00:51:47 +00002142 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002143 }
2144}
2145
Damiend99b0522013-12-21 18:17:45 +00002146void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002147 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002148 EMIT_ARG(binary_op, RT_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002149}
2150
Damiend99b0522013-12-21 18:17:45 +00002151void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002152 // a list of strings
Damien63321742013-12-10 17:41:49 +00002153
2154 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002155 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002156 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002157 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002158 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002159 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2160 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2161 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002162 if (i == 0) {
2163 string_kind = pn_kind;
2164 } else if (pn_kind != string_kind) {
2165 printf("SyntaxError: cannot mix bytes and nonbytes literals\n");
2166 return;
2167 }
Damien George55baff42014-01-21 21:40:13 +00002168 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002169 }
Damien63321742013-12-10 17:41:49 +00002170
Damien63321742013-12-10 17:41:49 +00002171 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002172 byte *q_ptr;
2173 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002174 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002175 uint s_len;
2176 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002177 memcpy(s_dest, s, s_len);
2178 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002179 }
Damien George55baff42014-01-21 21:40:13 +00002180 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002181
Damien Georgeb9791222014-01-23 00:34:21 +00002182 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002183}
2184
2185// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002186void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2187 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2188 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2189 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002190
2191 if (comp->pass == PASS_1) {
2192 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002193 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 +01002194 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002195 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002196 }
2197
2198 // get the scope for this comprehension
2199 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2200
2201 // compile the comprehension
2202 close_over_variables_etc(comp, this_scope, 0, 0);
2203
2204 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2205 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002206 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01002207}
2208
Damiend99b0522013-12-21 18:17:45 +00002209void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2210 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002211 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002212 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2213 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2214 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2215 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2216 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2217 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2218 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002219 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002220 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002221 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002222 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002223 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002224 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002225 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002226 // generator expression
2227 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2228 } else {
2229 // tuple with 2 items
2230 goto tuple_with_2_items;
2231 }
2232 } else {
2233 // tuple with 2 items
2234 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002235 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002236 }
2237 } else {
2238 // parenthesis around a single item, is just that item
2239 compile_node(comp, pns->nodes[0]);
2240 }
2241}
2242
Damiend99b0522013-12-21 18:17:45 +00002243void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2244 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002245 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002246 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002247 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2248 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2249 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2250 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2251 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002252 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002253 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002254 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002255 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002256 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002257 // list of many items
2258 compile_node(comp, pns2->nodes[0]);
2259 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002260 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002261 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002262 // list comprehension
2263 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2264 } else {
2265 // list with 2 items
2266 goto list_with_2_items;
2267 }
2268 } else {
2269 // list with 2 items
2270 list_with_2_items:
2271 compile_node(comp, pns2->nodes[0]);
2272 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002273 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002274 }
2275 } else {
2276 // list with 1 item
2277 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002278 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002279 }
2280}
2281
Damiend99b0522013-12-21 18:17:45 +00002282void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2283 mp_parse_node_t pn = pns->nodes[0];
2284 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002285 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002286 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002287 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2288 pns = (mp_parse_node_struct_t*)pn;
2289 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002290 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002291 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002292 compile_node(comp, pn);
2293 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002294 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2295 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2296 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2297 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002298 // dict/set with multiple elements
2299
2300 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002301 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002302 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2303
2304 // first element sets whether it's a dict or set
2305 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002306 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002307 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002308 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002309 compile_node(comp, pns->nodes[0]);
2310 EMIT(store_map);
2311 is_dict = true;
2312 } else {
2313 // a set
2314 compile_node(comp, pns->nodes[0]); // 1st value of set
2315 is_dict = false;
2316 }
2317
2318 // process rest of elements
2319 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002320 mp_parse_node_t pn = nodes[i];
2321 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002322 compile_node(comp, pn);
2323 if (is_dict) {
2324 if (!is_key_value) {
2325 printf("SyntaxError?: expecting key:value for dictionary");
2326 return;
2327 }
2328 EMIT(store_map);
2329 } else {
2330 if (is_key_value) {
2331 printf("SyntaxError?: expecting just a value for set");
2332 return;
2333 }
2334 }
2335 }
2336
2337 // if it's a set, build it
2338 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002339 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002340 }
Damiend99b0522013-12-21 18:17:45 +00002341 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002342 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002343 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002344 // a dictionary comprehension
2345 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2346 } else {
2347 // a set comprehension
2348 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2349 }
2350 } else {
2351 // shouldn't happen
2352 assert(0);
2353 }
2354 } else {
2355 // set with one element
2356 goto set_with_one_element;
2357 }
2358 } else {
2359 // set with one element
2360 set_with_one_element:
2361 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002362 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002363 }
2364}
2365
Damiend99b0522013-12-21 18:17:45 +00002366void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002367 compile_trailer_paren_helper(comp, pns, false);
2368}
2369
Damiend99b0522013-12-21 18:17:45 +00002370void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002371 // object who's index we want is on top of stack
2372 compile_node(comp, pns->nodes[0]); // the index
Damien Georgeb9791222014-01-23 00:34:21 +00002373 EMIT_ARG(binary_op, RT_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002374}
2375
Damiend99b0522013-12-21 18:17:45 +00002376void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002377 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002378 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002379}
2380
Damiend99b0522013-12-21 18:17:45 +00002381void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2382 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2383 mp_parse_node_t pn = pns->nodes[0];
2384 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002385 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002386 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2387 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002388 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2389 pns = (mp_parse_node_struct_t*)pn;
2390 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002391 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002392 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002393 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002394 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002395 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002396 } else {
2397 // [?::x]
2398 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002399 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002400 }
Damiend99b0522013-12-21 18:17:45 +00002401 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002402 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002403 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2404 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2405 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2406 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002407 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002408 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002409 } else {
2410 // [?:x:x]
2411 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002412 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002413 }
2414 } else {
2415 // [?:x]
2416 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002417 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002418 }
2419 } else {
2420 // [?:x]
2421 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002422 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002423 }
2424}
2425
Damiend99b0522013-12-21 18:17:45 +00002426void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002427 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002428 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2429 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002430}
2431
Damiend99b0522013-12-21 18:17:45 +00002432void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002433 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002434 compile_subscript_3_helper(comp, pns);
2435}
2436
Damiend99b0522013-12-21 18:17:45 +00002437void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002438 // if this is called then we are compiling a dict key:value pair
2439 compile_node(comp, pns->nodes[1]); // value
2440 compile_node(comp, pns->nodes[0]); // key
2441}
2442
Damiend99b0522013-12-21 18:17:45 +00002443void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002444 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002445 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002446 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002447}
2448
Damiend99b0522013-12-21 18:17:45 +00002449void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002450 if (comp->have_star_arg) {
2451 printf("SyntaxError?: can't have multiple *x\n");
2452 return;
2453 }
2454 comp->have_star_arg = true;
2455 compile_node(comp, pns->nodes[0]);
2456}
2457
Damiend99b0522013-12-21 18:17:45 +00002458void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002459 if (comp->have_dbl_star_arg) {
2460 printf("SyntaxError?: can't have multiple **x\n");
2461 return;
2462 }
2463 comp->have_dbl_star_arg = true;
2464 compile_node(comp, pns->nodes[0]);
2465}
2466
Damiend99b0522013-12-21 18:17:45 +00002467void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2468 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2469 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2470 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2471 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002472 printf("SyntaxError?: lhs of keyword argument must be an id\n");
2473 return;
2474 }
Damien Georgeb9791222014-01-23 00:34:21 +00002475 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002476 compile_node(comp, pns2->nodes[0]);
2477 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002478 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002479 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2480 } else {
2481 // shouldn't happen
2482 assert(0);
2483 }
2484}
2485
Damiend99b0522013-12-21 18:17:45 +00002486void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002487 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
2488 printf("SyntaxError: 'yield' outside function\n");
2489 return;
2490 }
Damiend99b0522013-12-21 18:17:45 +00002491 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002492 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002493 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002494 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2495 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002496 compile_node(comp, pns->nodes[0]);
2497 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002498 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002499 EMIT(yield_from);
2500 } else {
2501 compile_node(comp, pns->nodes[0]);
2502 EMIT(yield_value);
2503 }
2504}
2505
Damiend99b0522013-12-21 18:17:45 +00002506typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Damien429d7192013-10-04 19:53:11 +01002507static compile_function_t compile_function[] = {
2508 NULL,
2509#define nc NULL
2510#define c(f) compile_##f
2511#define DEF_RULE(rule, comp, kind, arg...) comp,
2512#include "grammar.h"
2513#undef nc
2514#undef c
2515#undef DEF_RULE
2516};
2517
Damiend99b0522013-12-21 18:17:45 +00002518void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2519 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002520 // pass
Damiend99b0522013-12-21 18:17:45 +00002521 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George08d07552014-01-29 18:58:52 +00002522 machine_int_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002523 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002524 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
2525 case MP_PARSE_NODE_SMALL_INT: EMIT_ARG(load_const_small_int, arg); break;
2526 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2527 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2528 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2529 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002530 case MP_PARSE_NODE_TOKEN:
2531 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002532 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002533 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002534 // do nothing
2535 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002536 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002537 }
2538 break;
Damien429d7192013-10-04 19:53:11 +01002539 default: assert(0);
2540 }
2541 } else {
Damiend99b0522013-12-21 18:17:45 +00002542 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002543 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002544 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002545 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002546 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002547#if MICROPY_DEBUG_PRINTERS
2548 mp_parse_node_print(pn, 0);
2549#endif
Damien429d7192013-10-04 19:53:11 +01002550 assert(0);
2551 } else {
2552 f(comp, pns);
2553 }
2554 }
2555}
2556
Damiend99b0522013-12-21 18:17:45 +00002557void 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 +01002558 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002559 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002560 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2561 if (MP_PARSE_NODE_IS_ID(pn)) {
2562 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002563 if (comp->have_bare_star) {
2564 // comes after a bare star, so doesn't count as a parameter
2565 } else {
2566 comp->scope_cur->num_params += 1;
2567 }
Damienb14de212013-10-06 00:28:28 +01002568 } else {
Damiend99b0522013-12-21 18:17:45 +00002569 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2570 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2571 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2572 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002573 //int node_index = 1; unused
2574 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002575 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002576 // this parameter has an annotation
2577 pn_annotation = pns->nodes[1];
2578 }
2579 //node_index = 2; unused
2580 }
2581 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002582 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002583 // this parameter has a default value
2584 if (comp->have_bare_star) {
2585 comp->scope_cur->num_dict_params += 1;
2586 } else {
2587 comp->scope_cur->num_default_params += 1;
2588 }
2589 }
2590 */
2591 if (comp->have_bare_star) {
2592 // comes after a bare star, so doesn't count as a parameter
2593 } else {
2594 comp->scope_cur->num_params += 1;
2595 }
Damiend99b0522013-12-21 18:17:45 +00002596 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2597 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002598 // bare star
2599 // TODO see http://www.python.org/dev/peps/pep-3102/
2600 comp->have_bare_star = true;
2601 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002602 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002603 // named star
2604 comp->scope_cur->flags |= SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002605 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2606 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002607 // named star with annotation
2608 comp->scope_cur->flags |= SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002609 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2610 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002611 pn_annotation = pns->nodes[1];
2612 } else {
2613 // shouldn't happen
2614 assert(0);
2615 }
Damiend99b0522013-12-21 18:17:45 +00002616 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2617 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2618 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002619 // this parameter has an annotation
2620 pn_annotation = pns->nodes[1];
2621 }
2622 comp->scope_cur->flags |= SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002623 } else {
Damienb14de212013-10-06 00:28:28 +01002624 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002625 assert(0);
2626 }
Damien429d7192013-10-04 19:53:11 +01002627 }
2628
2629 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002630 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002631 // TODO this parameter has an annotation
2632 }
2633 bool added;
2634 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2635 if (!added) {
2636 printf("SyntaxError?: same name used for parameter; %s\n", qstr_str(param_name));
2637 return;
2638 }
2639 id_info->param = true;
2640 id_info->kind = ID_INFO_KIND_LOCAL;
2641 }
2642}
2643
Damiend99b0522013-12-21 18:17:45 +00002644void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002645 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2646}
2647
Damiend99b0522013-12-21 18:17:45 +00002648void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002649 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2650}
2651
Damiend99b0522013-12-21 18:17:45 +00002652void 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 +01002653 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002654 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002655 // no more nested if/for; compile inner expression
2656 compile_node(comp, pn_inner_expr);
2657 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002658 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002659 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002660 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002661 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002662 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002663 } else {
2664 EMIT(yield_value);
2665 EMIT(pop_top);
2666 }
Damiend99b0522013-12-21 18:17:45 +00002667 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002668 // if condition
Damiend99b0522013-12-21 18:17:45 +00002669 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002670 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2671 pn_iter = pns_comp_if->nodes[1];
2672 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002673 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002674 // for loop
Damiend99b0522013-12-21 18:17:45 +00002675 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002676 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002677 int l_end2 = comp_next_label(comp);
2678 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002679 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002680 EMIT_ARG(label_assign, l_top2);
2681 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002682 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2683 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 +00002684 EMIT_ARG(jump, l_top2);
2685 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002686 EMIT(for_iter_end);
2687 } else {
2688 // shouldn't happen
2689 assert(0);
2690 }
2691}
2692
Damiend99b0522013-12-21 18:17:45 +00002693void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002694 // see http://www.python.org/dev/peps/pep-0257/
2695
2696 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002697 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002698 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002699 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002700 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002701 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2702 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002703 for (int i = 0; i < num_nodes; i++) {
2704 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002705 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 +00002706 // not a newline, so this is the first statement; finish search
2707 break;
2708 }
2709 }
2710 // 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 +00002711 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002712 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002713 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002714 } else {
2715 return;
2716 }
2717
2718 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002719 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2720 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2721 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2722 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2723 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002724 compile_node(comp, pns->nodes[0]); // a doc string
2725 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002726 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002727 }
2728 }
2729 }
2730}
2731
2732void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2733 comp->pass = pass;
2734 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002735 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002736 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002737
2738 if (comp->pass == PASS_1) {
2739 scope->stack_size = 0;
2740 }
2741
Damien5ac1b2e2013-10-18 19:58:12 +01002742#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002743 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002744 scope_print_info(scope);
2745 }
Damien5ac1b2e2013-10-18 19:58:12 +01002746#endif
Damien429d7192013-10-04 19:53:11 +01002747
2748 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002749 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2750 assert(scope->kind == SCOPE_MODULE);
2751 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2752 compile_node(comp, pns->nodes[0]); // compile the expression
2753 EMIT(return_value);
2754 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002755 if (!comp->is_repl) {
2756 check_for_doc_string(comp, scope->pn);
2757 }
Damien429d7192013-10-04 19:53:11 +01002758 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002759 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002760 EMIT(return_value);
2761 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002762 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2763 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2764 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002765
2766 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002767 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002768 if (comp->pass == PASS_1) {
2769 comp->have_bare_star = false;
2770 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2771 }
2772
Damiend99b0522013-12-21 18:17:45 +00002773 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // 2 is something...
Damien429d7192013-10-04 19:53:11 +01002774
2775 compile_node(comp, pns->nodes[3]); // 3 is function body
2776 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002777 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002778 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002779 EMIT(return_value);
2780 }
2781 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002782 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2783 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2784 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002785
2786 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002787 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002788 if (comp->pass == PASS_1) {
2789 comp->have_bare_star = false;
2790 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2791 }
2792
2793 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2794 EMIT(return_value);
2795 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2796 // a bit of a hack at the moment
2797
Damiend99b0522013-12-21 18:17:45 +00002798 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2799 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2800 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2801 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2802 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002803
Damien George55baff42014-01-21 21:40:13 +00002804 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01002805 if (comp->pass == PASS_1) {
2806 bool added;
2807 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2808 assert(added);
2809 id_info->kind = ID_INFO_KIND_LOCAL;
2810 scope->num_params = 1;
2811 }
2812
2813 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002814 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01002815 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002816 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01002817 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002818 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01002819 }
2820
Damienb05d7072013-10-05 13:37:10 +01002821 int l_end = comp_next_label(comp);
2822 int l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002823 EMIT_ARG(load_id, qstr_arg);
2824 EMIT_ARG(label_assign, l_top);
2825 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01002826 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2827 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00002828 EMIT_ARG(jump, l_top);
2829 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002830 EMIT(for_iter_end);
2831
2832 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00002833 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002834 }
2835 EMIT(return_value);
2836 } else {
2837 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002838 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2839 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2840 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01002841
2842 if (comp->pass == PASS_1) {
2843 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002844 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01002845 assert(added);
2846 id_info->kind = ID_INFO_KIND_LOCAL;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002847 id_info = scope_find_or_add_id(scope, MP_QSTR___locals__, &added);
Damien429d7192013-10-04 19:53:11 +01002848 assert(added);
2849 id_info->kind = ID_INFO_KIND_LOCAL;
2850 id_info->param = true;
2851 scope->num_params = 1; // __locals__ is the parameter
2852 }
2853
Damien Georgeb9791222014-01-23 00:34:21 +00002854 EMIT_ARG(load_id, MP_QSTR___locals__);
Damien429d7192013-10-04 19:53:11 +01002855 EMIT(store_locals);
Damien Georgeb9791222014-01-23 00:34:21 +00002856 EMIT_ARG(load_id, MP_QSTR___name__);
2857 EMIT_ARG(store_id, MP_QSTR___module__);
2858 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
2859 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01002860
2861 check_for_doc_string(comp, pns->nodes[2]);
2862 compile_node(comp, pns->nodes[2]); // 2 is class body
2863
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002864 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01002865 assert(id != NULL);
2866 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00002867 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002868 } else {
Damien George6baf76e2013-12-30 22:32:17 +00002869#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00002870 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002871#else
Damien George35e2a4e2014-02-05 00:51:47 +00002872 EMIT_ARG(load_fast, MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00002873#endif
Damien429d7192013-10-04 19:53:11 +01002874 }
2875 EMIT(return_value);
2876 }
2877
Damien415eb6f2013-10-05 12:19:06 +01002878 EMIT(end_pass);
Damien826005c2013-10-05 23:17:28 +01002879}
2880
2881void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2882 comp->pass = pass;
2883 comp->scope_cur = scope;
2884 comp->next_label = 1;
2885
2886 if (scope->kind != SCOPE_FUNCTION) {
2887 printf("Error: inline assembler must be a function\n");
2888 return;
2889 }
2890
Damiena2f2f7d2013-10-06 00:14:13 +01002891 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00002892 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01002893 }
2894
Damien826005c2013-10-05 23:17:28 +01002895 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00002896 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2897 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2898 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01002899
Damiend99b0522013-12-21 18:17:45 +00002900 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01002901
Damiena2f2f7d2013-10-06 00:14:13 +01002902 // parameters are in pns->nodes[1]
2903 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00002904 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01002905 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00002906 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01002907 }
2908
Damiend99b0522013-12-21 18:17:45 +00002909 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01002910
Damiend99b0522013-12-21 18:17:45 +00002911 mp_parse_node_t pn_body = pns->nodes[3]; // body
2912 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01002913 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
2914
Damien Georgecbd2f742014-01-19 11:48:48 +00002915 /*
Damien826005c2013-10-05 23:17:28 +01002916 if (comp->pass == PASS_3) {
2917 //printf("----\n");
2918 scope_print_info(scope);
2919 }
Damien Georgecbd2f742014-01-19 11:48:48 +00002920 */
Damien826005c2013-10-05 23:17:28 +01002921
2922 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00002923 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
2924 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
2925 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
2926 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
2927 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
2928 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
2929 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
2930 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
2931 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
2932 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
2933 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
2934 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
2935 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01002936 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
2937
2938 // emit instructions
2939 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00002940 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien826005c2013-10-05 23:17:28 +01002941 printf("SyntaxError: inline assembler 'label' requires 1 argument\n");
2942 return;
2943 }
2944 int lab = comp_next_label(comp);
2945 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00002946 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01002947 }
2948 } else {
2949 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00002950 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01002951 }
2952 }
2953 }
2954
2955 if (comp->pass > PASS_1) {
2956 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01002957 }
Damien429d7192013-10-04 19:53:11 +01002958}
2959
2960void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
2961 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00002962 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01002963 scope->num_locals = 0;
2964 for (int i = 0; i < scope->id_info_len; i++) {
2965 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002966 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01002967 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
2968 continue;
2969 }
2970 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
2971 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
2972 }
Damien9ecbcff2013-12-11 00:41:43 +00002973 // note: params always count for 1 local, even if they are a cell
Damien429d7192013-10-04 19:53:11 +01002974 if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
2975 id->local_num = scope->num_locals;
2976 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00002977 }
2978 }
2979
2980 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00002981#if MICROPY_EMIT_CPYTHON
2982 int num_cell = 0;
2983#endif
Damien9ecbcff2013-12-11 00:41:43 +00002984 for (int i = 0; i < scope->id_info_len; i++) {
2985 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00002986#if MICROPY_EMIT_CPYTHON
2987 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00002988 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00002989 id->local_num = num_cell;
2990 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00002991 }
Damien George6baf76e2013-12-30 22:32:17 +00002992#else
2993 // in Micro Python the cells come right after the fast locals
2994 // parameters are not counted here, since they remain at the start
2995 // of the locals, even if they are cell vars
2996 if (!id->param && id->kind == ID_INFO_KIND_CELL) {
2997 id->local_num = scope->num_locals;
2998 scope->num_locals += 1;
2999 }
3000#endif
Damien9ecbcff2013-12-11 00:41:43 +00003001 }
Damien9ecbcff2013-12-11 00:41:43 +00003002
3003 // compute the index of free vars (freevars[idx] in CPython)
3004 // make sure they are in the order of the parent scope
3005 if (scope->parent != NULL) {
3006 int num_free = 0;
3007 for (int i = 0; i < scope->parent->id_info_len; i++) {
3008 id_info_t *id = &scope->parent->id_info[i];
3009 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3010 for (int j = 0; j < scope->id_info_len; j++) {
3011 id_info_t *id2 = &scope->id_info[j];
3012 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +00003013 assert(!id2->param); // free vars should not be params
3014#if MICROPY_EMIT_CPYTHON
3015 // in CPython the frees are numbered after the cells
3016 id2->local_num = num_cell + num_free;
3017#else
3018 // in Micro Python the frees come first, before the params
3019 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003020#endif
3021 num_free += 1;
3022 }
3023 }
3024 }
Damien429d7192013-10-04 19:53:11 +01003025 }
Damien George6baf76e2013-12-30 22:32:17 +00003026#if !MICROPY_EMIT_CPYTHON
3027 // in Micro Python shift all other locals after the free locals
3028 if (num_free > 0) {
3029 for (int i = 0; i < scope->id_info_len; i++) {
3030 id_info_t *id = &scope->id_info[i];
3031 if (id->param || id->kind != ID_INFO_KIND_FREE) {
3032 id->local_num += num_free;
3033 }
3034 }
3035 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3036 scope->num_locals += num_free;
3037 }
3038#endif
Damien429d7192013-10-04 19:53:11 +01003039 }
3040
3041 // compute flags
3042 //scope->flags = 0; since we set some things in parameters
3043 if (scope->kind != SCOPE_MODULE) {
3044 scope->flags |= SCOPE_FLAG_NEWLOCALS;
3045 }
3046 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) {
3047 assert(scope->parent != NULL);
3048 scope->flags |= SCOPE_FLAG_OPTIMISED;
3049
3050 // TODO possibly other ways it can be nested
Damien George08d07552014-01-29 18:58:52 +00003051 // Note that we don't actually use this information at the moment (for CPython compat only)
3052 if ((SCOPE_FUNCTION <= scope->parent->kind && scope->parent->kind <= SCOPE_SET_COMP) || (scope->parent->kind == SCOPE_CLASS && scope->parent->parent->kind == SCOPE_FUNCTION)) {
Damien429d7192013-10-04 19:53:11 +01003053 scope->flags |= SCOPE_FLAG_NESTED;
3054 }
3055 }
3056 int num_free = 0;
3057 for (int i = 0; i < scope->id_info_len; i++) {
3058 id_info_t *id = &scope->id_info[i];
3059 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3060 num_free += 1;
3061 }
3062 }
3063 if (num_free == 0) {
3064 scope->flags |= SCOPE_FLAG_NOFREE;
3065 }
3066}
3067
Damien George08335002014-01-18 23:24:36 +00003068mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
Damien429d7192013-10-04 19:53:11 +01003069 compiler_t *comp = m_new(compiler_t, 1);
3070
Damien Georgecbd2f742014-01-19 11:48:48 +00003071 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003072 comp->is_repl = is_repl;
3073 comp->had_error = false;
3074
Damien429d7192013-10-04 19:53:11 +01003075 comp->break_label = 0;
3076 comp->continue_label = 0;
Damien Georgecbddb272014-02-01 20:08:18 +00003077 comp->break_continue_except_level = 0;
3078 comp->cur_except_level = 0;
3079
Damien George35e2a4e2014-02-05 00:51:47 +00003080 comp->func_arg_is_super = false;
3081
Damien429d7192013-10-04 19:53:11 +01003082 comp->scope_head = NULL;
3083 comp->scope_cur = NULL;
3084
Damien826005c2013-10-05 23:17:28 +01003085 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003086 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003087
3088 // set the outer scope
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003089 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE);
Damien429d7192013-10-04 19:53:11 +01003090
Damien826005c2013-10-05 23:17:28 +01003091 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003092 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003093 comp->emit_method_table = &emit_pass1_method_table;
3094 comp->emit_inline_asm = NULL;
3095 comp->emit_inline_asm_method_table = NULL;
3096 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003097 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003098 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003099#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003100 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003101 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003102#endif
Damien826005c2013-10-05 23:17:28 +01003103 } else {
3104 compile_scope(comp, s, PASS_1);
3105 }
3106
3107 // update maximim number of labels needed
3108 if (comp->next_label > max_num_labels) {
3109 max_num_labels = comp->next_label;
3110 }
Damien429d7192013-10-04 19:53:11 +01003111 }
3112
Damien826005c2013-10-05 23:17:28 +01003113 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003114 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003115 compile_scope_compute_things(comp, s);
3116 }
3117
Damien826005c2013-10-05 23:17:28 +01003118 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003119 emit_pass1_free(comp->emit);
3120
Damien826005c2013-10-05 23:17:28 +01003121 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003122#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003123 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003124#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003125 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003126#endif
Damien3ef4abb2013-10-12 16:53:13 +01003127#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003128 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003129#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003130#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003131 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003132 if (false) {
3133 // dummy
3134
Damien3ef4abb2013-10-12 16:53:13 +01003135#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003136 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
3137 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003138 if (emit_inline_thumb == NULL) {
3139 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3140 }
3141 comp->emit = NULL;
3142 comp->emit_method_table = NULL;
3143 comp->emit_inline_asm = emit_inline_thumb;
3144 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3145 compile_scope_inline_asm(comp, s, PASS_2);
3146 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003147#endif
3148
Damien826005c2013-10-05 23:17:28 +01003149 } else {
Damienc025ebb2013-10-12 14:30:21 +01003150
3151 // choose the emit type
3152
Damien3ef4abb2013-10-12 16:53:13 +01003153#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003154 comp->emit = emit_cpython_new(max_num_labels);
3155 comp->emit_method_table = &emit_cpython_method_table;
3156#else
Damien826005c2013-10-05 23:17:28 +01003157 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003158
3159#if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003160 case EMIT_OPT_NATIVE_PYTHON:
Damien3410be82013-10-07 23:09:10 +01003161 case EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003162#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003163 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003164 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003165 }
Damien13ed3a62013-10-08 09:05:10 +01003166 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003167#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003168 if (emit_native == NULL) {
3169 emit_native = emit_native_thumb_new(max_num_labels);
3170 }
3171 comp->emit_method_table = &emit_native_thumb_method_table;
3172#endif
3173 comp->emit = emit_native;
Damien3410be82013-10-07 23:09:10 +01003174 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003175 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003176#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003177
Damien826005c2013-10-05 23:17:28 +01003178 default:
3179 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003180 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003181 }
3182 comp->emit = emit_bc;
3183 comp->emit_method_table = &emit_bc_method_table;
3184 break;
3185 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003186#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003187
3188 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003189 compile_scope(comp, s, PASS_2);
3190 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003191 }
Damien429d7192013-10-04 19:53:11 +01003192 }
3193
Damien George41d02b62014-01-24 22:42:28 +00003194 // free the emitters
3195#if !MICROPY_EMIT_CPYTHON
3196 if (emit_bc != NULL) {
3197 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003198 }
Damien George41d02b62014-01-24 22:42:28 +00003199#if MICROPY_EMIT_NATIVE
3200 if (emit_native != NULL) {
3201#if MICROPY_EMIT_X64
3202 emit_native_x64_free(emit_native);
3203#elif MICROPY_EMIT_THUMB
3204 emit_native_thumb_free(emit_native);
3205#endif
3206 }
3207#endif
3208#if MICROPY_EMIT_INLINE_THUMB
3209 if (emit_inline_thumb != NULL) {
3210 emit_inline_thumb_free(emit_inline_thumb);
3211 }
3212#endif
3213#endif // !MICROPY_EMIT_CPYTHON
3214
3215 // free the scopes
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003216 uint unique_code_id = module_scope->unique_code_id;
3217 for (scope_t *s = module_scope; s;) {
3218 scope_t *next = s->next;
3219 scope_free(s);
3220 s = next;
3221 }
Damien5ac1b2e2013-10-18 19:58:12 +01003222
Damien George41d02b62014-01-24 22:42:28 +00003223 // free the compiler
3224 bool had_error = comp->had_error;
3225 m_del_obj(compiler_t, comp);
3226
Damien George1fb03172014-01-03 14:22:03 +00003227 if (had_error) {
3228 // TODO return a proper error message
3229 return mp_const_none;
3230 } else {
3231#if MICROPY_EMIT_CPYTHON
3232 // can't create code, so just return true
Damien George41d02b62014-01-24 22:42:28 +00003233 (void)unique_code_id; // to suppress warning that unique_code_id is unused
Damien George1fb03172014-01-03 14:22:03 +00003234 return mp_const_true;
3235#else
3236 // return function that executes the outer module
Paul Sokolovsky90750022014-02-01 15:05:04 +02003237 return rt_make_function_from_id(unique_code_id, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003238#endif
3239 }
Damien429d7192013-10-04 19:53:11 +01003240}