blob: c1a7955dcdf7ecd02aa90312306d20958695c80e [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;
53 int except_nest_level;
54
55 int n_arg_keyword;
56 bool have_star_arg;
57 bool have_dbl_star_arg;
58 bool have_bare_star;
59 int param_pass;
60 int param_pass_num_dict_params;
61 int param_pass_num_default_params;
62
63 scope_t *scope_head;
64 scope_t *scope_cur;
65
Damien6cdd3af2013-10-05 18:08:26 +010066 emit_t *emit; // current emitter
67 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010068
69 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
70 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 +010071} compiler_t;
72
Damiend99b0522013-12-21 18:17:45 +000073mp_parse_node_t fold_constants(mp_parse_node_t pn) {
74 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
75 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
76 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +010077
78 // fold arguments first
79 for (int i = 0; i < n; i++) {
80 pns->nodes[i] = fold_constants(pns->nodes[i]);
81 }
82
Damiend99b0522013-12-21 18:17:45 +000083 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +010084 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +000085 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
86 int arg0 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
87 int arg1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[2]);
88 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien3ef4abb2013-10-12 16:53:13 +010089#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +010090 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +000091 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
Damien0efb3a12013-10-12 16:16:56 +010092#endif
Damiend99b0522013-12-21 18:17:45 +000093 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
94 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +010095 } else {
96 // shouldn't happen
97 assert(0);
98 }
99 }
100 break;
101
102 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100103 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000104 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
105 machine_int_t arg0 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
106 machine_int_t arg1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[2]);
Damien0efb3a12013-10-12 16:16:56 +0100107 machine_int_t res;
Damiend99b0522013-12-21 18:17:45 +0000108 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100109 res = arg0 + arg1;
Damiend99b0522013-12-21 18:17:45 +0000110 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100111 res = arg0 - arg1;
Damien429d7192013-10-04 19:53:11 +0100112 } else {
113 // shouldn't happen
114 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100115 res = 0;
116 }
Damiend99b0522013-12-21 18:17:45 +0000117 if (MP_FIT_SMALL_INT(res)) {
118 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, res);
Damien429d7192013-10-04 19:53:11 +0100119 }
120 }
121 break;
122
123 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000124 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
125 int arg0 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
126 int arg1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[2]);
127 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien3ef4abb2013-10-12 16:53:13 +0100128#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100129 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000130 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 * arg1);
Damien0efb3a12013-10-12 16:16:56 +0100131#endif
Damiend99b0522013-12-21 18:17:45 +0000132 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien429d7192013-10-04 19:53:11 +0100133 ; // pass
Damiend99b0522013-12-21 18:17:45 +0000134 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien0efb3a12013-10-12 16:16:56 +0100135 // XXX implement this properly as Python's % operator acts differently to C's
Damiend99b0522013-12-21 18:17:45 +0000136 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 % arg1);
137 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
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);
Damien429d7192013-10-04 19:53:11 +0100140 } else {
141 // shouldn't happen
142 assert(0);
143 }
144 }
145 break;
146
147 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000148 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
149 machine_int_t arg = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
150 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
151 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
152 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
153 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
154 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
155 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100156 } else {
157 // shouldn't happen
158 assert(0);
159 }
160 }
161 break;
162
Damien3ef4abb2013-10-12 16:53:13 +0100163#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +0100164 case PN_power:
Damien0efb3a12013-10-12 16:16:56 +0100165 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000166 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])) {
167 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
168 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
169 int power = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100170 if (power >= 0) {
171 int ans = 1;
Damiend99b0522013-12-21 18:17:45 +0000172 int base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100173 for (; power > 0; power--) {
174 ans *= base;
175 }
Damiend99b0522013-12-21 18:17:45 +0000176 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100177 }
178 }
179 }
180 break;
Damien0efb3a12013-10-12 16:16:56 +0100181#endif
Damien429d7192013-10-04 19:53:11 +0100182 }
183 }
184
185 return pn;
186}
187
Damiend99b0522013-12-21 18:17:45 +0000188void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100189
Damienb05d7072013-10-05 13:37:10 +0100190static int comp_next_label(compiler_t *comp) {
191 return comp->next_label++;
192}
193
Damiend99b0522013-12-21 18:17:45 +0000194static 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 +0000195 scope_t *scope = scope_new(kind, pn, comp->source_file, rt_get_unique_code_id(), emit_options);
Damien429d7192013-10-04 19:53:11 +0100196 scope->parent = comp->scope_cur;
197 scope->next = NULL;
198 if (comp->scope_head == NULL) {
199 comp->scope_head = scope;
200 } else {
201 scope_t *s = comp->scope_head;
202 while (s->next != NULL) {
203 s = s->next;
204 }
205 s->next = scope;
206 }
207 return scope;
208}
209
Damiend99b0522013-12-21 18:17:45 +0000210static int list_len(mp_parse_node_t pn, int pn_kind) {
211 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100212 return 0;
Damiend99b0522013-12-21 18:17:45 +0000213 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100214 return 1;
215 } else {
Damiend99b0522013-12-21 18:17:45 +0000216 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
217 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100218 return 1;
219 } else {
Damiend99b0522013-12-21 18:17:45 +0000220 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100221 }
222 }
223}
224
Damiend99b0522013-12-21 18:17:45 +0000225static 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)) {
226 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
227 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
228 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100229 for (int i = 0; i < num_nodes; i++) {
230 f(comp, pns->nodes[i]);
231 }
Damiend99b0522013-12-21 18:17:45 +0000232 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100233 f(comp, pn);
234 }
235}
236
Damiend99b0522013-12-21 18:17:45 +0000237static int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
238 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100239 *nodes = NULL;
240 return 0;
Damiend99b0522013-12-21 18:17:45 +0000241 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100242 *nodes = pn;
243 return 1;
244 } else {
Damiend99b0522013-12-21 18:17:45 +0000245 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
246 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100247 *nodes = pn;
248 return 1;
249 } else {
250 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000251 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100252 }
253 }
254}
255
Damiend99b0522013-12-21 18:17:45 +0000256void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100257}
258
Damiend99b0522013-12-21 18:17:45 +0000259void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
260 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100261 for (int i = 0; i < num_nodes; i++) {
262 compile_node(comp, pns->nodes[i]);
263 }
264}
265
Damien3ef4abb2013-10-12 16:53:13 +0100266#if MICROPY_EMIT_CPYTHON
Damiend99b0522013-12-21 18:17:45 +0000267static bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
268 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100269 return false;
270 }
Damiend99b0522013-12-21 18:17:45 +0000271 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100272 return false;
273 }
274 return true;
275}
276
Damien02f89412013-12-12 15:13:36 +0000277static void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
Damien George55baff42014-01-21 21:40:13 +0000278 uint len;
279 const byte *str = qstr_data(qstr, &len);
Damien02f89412013-12-12 15:13:36 +0000280 bool has_single_quote = false;
281 bool has_double_quote = false;
282 for (int i = 0; i < len; i++) {
283 if (str[i] == '\'') {
284 has_single_quote = true;
285 } else if (str[i] == '"') {
286 has_double_quote = true;
287 }
288 }
289 if (bytes) {
290 vstr_printf(vstr, "b");
291 }
292 bool quote_single = false;
293 if (has_single_quote && !has_double_quote) {
294 vstr_printf(vstr, "\"");
295 } else {
296 quote_single = true;
297 vstr_printf(vstr, "'");
298 }
299 for (int i = 0; i < len; i++) {
300 if (str[i] == '\n') {
301 vstr_printf(vstr, "\\n");
302 } else if (str[i] == '\\') {
303 vstr_printf(vstr, "\\\\");
304 } else if (str[i] == '\'' && quote_single) {
305 vstr_printf(vstr, "\\'");
306 } else {
307 vstr_printf(vstr, "%c", str[i]);
308 }
309 }
310 if (has_single_quote && !has_double_quote) {
311 vstr_printf(vstr, "\"");
312 } else {
313 vstr_printf(vstr, "'");
314 }
315}
316
Damiend99b0522013-12-21 18:17:45 +0000317static void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
318 assert(MP_PARSE_NODE_IS_LEAF(pn));
319 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
320 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
321 case MP_PARSE_NODE_ID: assert(0);
322 case MP_PARSE_NODE_SMALL_INT: vstr_printf(vstr, "%d", arg); break;
323 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
324 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
325 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
326 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
327 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100328 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000329 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
330 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
331 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien429d7192013-10-04 19:53:11 +0100332 default: assert(0);
333 }
334 break;
335 default: assert(0);
336 }
337}
338
Damiend99b0522013-12-21 18:17:45 +0000339static 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 +0100340 int n = 0;
341 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000342 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100343 }
344 int total = n;
345 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000346 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100347 total += 1;
Damien3a205172013-10-12 15:01:56 +0100348 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100349 is_const = false;
350 }
351 }
352 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100353 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100354 is_const = false;
355 break;
356 }
357 }
358 if (total > 0 && is_const) {
359 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000360 vstr_t *vstr = vstr_new();
361 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000362 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000363 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100364 need_comma = true;
365 }
366 for (int i = 0; i < n; i++) {
367 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000368 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100369 }
Damien02f89412013-12-12 15:13:36 +0000370 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100371 need_comma = true;
372 }
373 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000374 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100375 } else {
Damien02f89412013-12-12 15:13:36 +0000376 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100377 }
Damien Georgeb9791222014-01-23 00:34:21 +0000378 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000379 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100380 } else {
Damiend99b0522013-12-21 18:17:45 +0000381 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100382 compile_node(comp, pn);
383 }
384 for (int i = 0; i < n; i++) {
385 compile_node(comp, pns_list->nodes[i]);
386 }
Damien Georgeb9791222014-01-23 00:34:21 +0000387 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100388 }
389}
Damien3a205172013-10-12 15:01:56 +0100390#endif
391
392// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damiend99b0522013-12-21 18:17:45 +0000393void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100394#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100395 cpython_c_tuple(comp, pn, pns_list);
396#else
397 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000398 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100399 compile_node(comp, pn);
400 total += 1;
401 }
402 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000403 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100404 for (int i = 0; i < n; i++) {
405 compile_node(comp, pns_list->nodes[i]);
406 }
407 total += n;
408 }
Damien Georgeb9791222014-01-23 00:34:21 +0000409 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100410#endif
411}
Damien429d7192013-10-04 19:53:11 +0100412
Damiend99b0522013-12-21 18:17:45 +0000413void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100414 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000415 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100416}
417
Damiend99b0522013-12-21 18:17:45 +0000418static bool node_is_const_false(mp_parse_node_t pn) {
419 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
420 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_ARG(pn) == 1);
Damien429d7192013-10-04 19:53:11 +0100421}
422
Damiend99b0522013-12-21 18:17:45 +0000423static bool node_is_const_true(mp_parse_node_t pn) {
424 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 +0100425}
426
Damien3ef4abb2013-10-12 16:53:13 +0100427#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100428// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Damiend99b0522013-12-21 18:17:45 +0000429static 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 +0100430 if (node_is_const_false(pn)) {
431 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000432 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100433 }
434 return;
435 } else if (node_is_const_true(pn)) {
436 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000437 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100438 }
439 return;
Damiend99b0522013-12-21 18:17:45 +0000440 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
441 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
442 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
443 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100444 if (jump_if == false) {
Damienb05d7072013-10-05 13:37:10 +0100445 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100446 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100447 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100448 }
Damien3a205172013-10-12 15:01:56 +0100449 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000450 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100451 } else {
452 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100453 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100454 }
455 }
456 return;
Damiend99b0522013-12-21 18:17:45 +0000457 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100458 if (jump_if == false) {
459 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100460 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100461 }
462 } else {
Damienb05d7072013-10-05 13:37:10 +0100463 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100464 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100465 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100466 }
Damien3a205172013-10-12 15:01:56 +0100467 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000468 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100469 }
470 return;
Damiend99b0522013-12-21 18:17:45 +0000471 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100472 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100473 return;
474 }
475 }
476
477 // nothing special, fall back to default compiling for node and jump
478 compile_node(comp, pn);
479 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000480 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100481 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000482 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100483 }
484}
Damien3a205172013-10-12 15:01:56 +0100485#endif
Damien429d7192013-10-04 19:53:11 +0100486
Damiend99b0522013-12-21 18:17:45 +0000487static void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100488#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100489 cpython_c_if_cond(comp, pn, jump_if, label, false);
490#else
491 if (node_is_const_false(pn)) {
492 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000493 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100494 }
495 return;
496 } else if (node_is_const_true(pn)) {
497 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000498 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100499 }
500 return;
Damiend99b0522013-12-21 18:17:45 +0000501 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
502 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
503 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
504 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100505 if (jump_if == false) {
506 int label2 = comp_next_label(comp);
507 for (int i = 0; i < n - 1; i++) {
508 c_if_cond(comp, pns->nodes[i], true, label2);
509 }
510 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000511 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100512 } else {
513 for (int i = 0; i < n; i++) {
514 c_if_cond(comp, pns->nodes[i], true, label);
515 }
516 }
517 return;
Damiend99b0522013-12-21 18:17:45 +0000518 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100519 if (jump_if == false) {
520 for (int i = 0; i < n; i++) {
521 c_if_cond(comp, pns->nodes[i], false, label);
522 }
523 } else {
524 int label2 = comp_next_label(comp);
525 for (int i = 0; i < n - 1; i++) {
526 c_if_cond(comp, pns->nodes[i], false, label2);
527 }
528 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000529 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100530 }
531 return;
Damiend99b0522013-12-21 18:17:45 +0000532 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100533 c_if_cond(comp, pns->nodes[0], !jump_if, label);
534 return;
535 }
536 }
537
538 // nothing special, fall back to default compiling for node and jump
539 compile_node(comp, pn);
540 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000541 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100542 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000543 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100544 }
545#endif
Damien429d7192013-10-04 19:53:11 +0100546}
547
548typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000549void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100550
Damiend99b0522013-12-21 18:17:45 +0000551void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100552 if (assign_kind != ASSIGN_AUG_STORE) {
553 compile_node(comp, pns->nodes[0]);
554 }
555
Damiend99b0522013-12-21 18:17:45 +0000556 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
557 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
558 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
559 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100560 if (assign_kind != ASSIGN_AUG_STORE) {
561 for (int i = 0; i < n - 1; i++) {
562 compile_node(comp, pns1->nodes[i]);
563 }
564 }
Damiend99b0522013-12-21 18:17:45 +0000565 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
566 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100567 }
Damiend99b0522013-12-21 18:17:45 +0000568 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien429d7192013-10-04 19:53:11 +0100569 printf("SyntaxError: can't assign to function call\n");
570 return;
Damiend99b0522013-12-21 18:17:45 +0000571 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100572 if (assign_kind == ASSIGN_AUG_STORE) {
573 EMIT(rot_three);
574 EMIT(store_subscr);
575 } else {
576 compile_node(comp, pns1->nodes[0]);
577 if (assign_kind == ASSIGN_AUG_LOAD) {
578 EMIT(dup_top_two);
Damien Georgeb9791222014-01-23 00:34:21 +0000579 EMIT_ARG(binary_op, RT_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100580 } else {
581 EMIT(store_subscr);
582 }
583 }
Damiend99b0522013-12-21 18:17:45 +0000584 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
585 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100586 if (assign_kind == ASSIGN_AUG_LOAD) {
587 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000588 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100589 } else {
590 if (assign_kind == ASSIGN_AUG_STORE) {
591 EMIT(rot_two);
592 }
Damien Georgeb9791222014-01-23 00:34:21 +0000593 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100594 }
595 } else {
596 // shouldn't happen
597 assert(0);
598 }
599 } else {
600 // shouldn't happen
601 assert(0);
602 }
603
Damiend99b0522013-12-21 18:17:45 +0000604 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +0100605 // SyntaxError, cannot assign
606 assert(0);
607 }
608}
609
Damiend99b0522013-12-21 18:17:45 +0000610void c_assign_tuple(compiler_t *comp, int n, mp_parse_node_t *nodes) {
Damien429d7192013-10-04 19:53:11 +0100611 assert(n >= 0);
612 int have_star_index = -1;
613 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000614 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100615 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000616 EMIT_ARG(unpack_ex, i, n - i - 1);
Damien429d7192013-10-04 19:53:11 +0100617 have_star_index = i;
618 } else {
619 printf("SyntaxError: two starred expressions in assignment\n");
620 return;
621 }
622 }
623 }
624 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000625 EMIT_ARG(unpack_sequence, n);
Damien429d7192013-10-04 19:53:11 +0100626 }
627 for (int i = 0; i < n; i++) {
628 if (i == have_star_index) {
Damiend99b0522013-12-21 18:17:45 +0000629 c_assign(comp, ((mp_parse_node_struct_t*)nodes[i])->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100630 } else {
631 c_assign(comp, nodes[i], ASSIGN_STORE);
632 }
633 }
634}
635
636// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000637void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100638 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000639 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100640 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000641 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
642 if (MP_PARSE_NODE_IS_ID(pn)) {
643 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100644 switch (assign_kind) {
645 case ASSIGN_STORE:
646 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000647 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100648 break;
649 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000650 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100651 break;
652 }
653 } else {
654 printf("SyntaxError: can't assign to literal\n");
655 return;
656 }
657 } else {
Damiend99b0522013-12-21 18:17:45 +0000658 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
659 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100660 case PN_power:
661 // lhs is an index or attribute
662 c_assign_power(comp, pns, assign_kind);
663 break;
664
665 case PN_testlist_star_expr:
666 case PN_exprlist:
667 // lhs is a tuple
668 if (assign_kind != ASSIGN_STORE) {
669 goto bad_aug;
670 }
Damiend99b0522013-12-21 18:17:45 +0000671 c_assign_tuple(comp, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100672 break;
673
674 case PN_atom_paren:
675 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000676 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100677 // empty tuple
678 printf("SyntaxError: can't assign to ()\n");
679 return;
Damiend99b0522013-12-21 18:17:45 +0000680 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
681 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100682 goto testlist_comp;
683 } else {
684 // parenthesis around 1 item, is just that item
685 pn = pns->nodes[0];
686 goto tail_recursion;
687 }
688 break;
689
690 case PN_atom_bracket:
691 // lhs is something in brackets
692 if (assign_kind != ASSIGN_STORE) {
693 goto bad_aug;
694 }
Damiend99b0522013-12-21 18:17:45 +0000695 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100696 // empty list, assignment allowed
697 c_assign_tuple(comp, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000698 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
699 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100700 goto testlist_comp;
701 } else {
702 // brackets around 1 item
703 c_assign_tuple(comp, 1, &pns->nodes[0]);
704 }
705 break;
706
707 default:
Damiend99b0522013-12-21 18:17:45 +0000708 printf("unknown assign, %u\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien429d7192013-10-04 19:53:11 +0100709 assert(0);
710 }
711 return;
712
713 testlist_comp:
714 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000715 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
716 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
717 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100718 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000719 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100720 c_assign_tuple(comp, 1, &pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000721 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100722 // sequence of many items
723 // TODO call c_assign_tuple instead
Damiend99b0522013-12-21 18:17:45 +0000724 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
Damien Georgeb9791222014-01-23 00:34:21 +0000725 EMIT_ARG(unpack_sequence, 1 + n);
Damien429d7192013-10-04 19:53:11 +0100726 c_assign(comp, pns->nodes[0], ASSIGN_STORE);
727 for (int i = 0; i < n; i++) {
728 c_assign(comp, pns2->nodes[i], ASSIGN_STORE);
729 }
Damiend99b0522013-12-21 18:17:45 +0000730 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +0100731 // TODO not implemented
732 assert(0);
733 } else {
734 // sequence with 2 items
735 goto sequence_with_2_items;
736 }
737 } else {
738 // sequence with 2 items
739 sequence_with_2_items:
740 c_assign_tuple(comp, 2, pns->nodes);
741 }
742 return;
743 }
744 return;
745
746 bad_aug:
747 printf("SyntaxError: illegal expression for augmented assignment\n");
748}
749
750// stuff for lambda and comprehensions and generators
751void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_dict_params, int n_default_params) {
752 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000753 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100754 int nfree = 0;
755 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000756 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
757 id_info_t *id = &comp->scope_cur->id_info[i];
758 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
759 for (int j = 0; j < this_scope->id_info_len; j++) {
760 id_info_t *id2 = &this_scope->id_info[j];
761 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000762#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000763 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000764#else
765 // in Micro Python we load closures using LOAD_FAST
Damien Georgeb9791222014-01-23 00:34:21 +0000766 EMIT_ARG(load_fast, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000767#endif
Damien318aec62013-12-10 18:28:17 +0000768 nfree += 1;
769 }
770 }
Damien429d7192013-10-04 19:53:11 +0100771 }
772 }
773 }
774 if (nfree > 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000775 EMIT_ARG(build_tuple, nfree);
Damien429d7192013-10-04 19:53:11 +0100776 }
777
778 // make the function/closure
779 if (nfree == 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000780 EMIT_ARG(make_function, this_scope, n_dict_params, n_default_params);
Damien429d7192013-10-04 19:53:11 +0100781 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000782 EMIT_ARG(make_closure, this_scope, n_dict_params, n_default_params);
Damien429d7192013-10-04 19:53:11 +0100783 }
784}
785
Damiend99b0522013-12-21 18:17:45 +0000786void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
787 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
788 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
789 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +0100790 // this parameter has a default value
791 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
792 if (comp->have_bare_star) {
793 comp->param_pass_num_dict_params += 1;
794 if (comp->param_pass == 1) {
Damien Georgeb9791222014-01-23 00:34:21 +0000795 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100796 compile_node(comp, pns->nodes[2]);
797 }
798 } else {
799 comp->param_pass_num_default_params += 1;
800 if (comp->param_pass == 2) {
801 compile_node(comp, pns->nodes[2]);
802 }
803 }
804 }
Damiend99b0522013-12-21 18:17:45 +0000805 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
806 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
807 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100808 // bare star
809 comp->have_bare_star = true;
810 }
811 }
812}
813
814// leaves function object on stack
815// returns function name
Damiend99b0522013-12-21 18:17:45 +0000816qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100817 if (comp->pass == PASS_1) {
818 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000819 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100820 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000821 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100822 }
823
824 // save variables (probably don't need to do this, since we can't have nested definitions..?)
825 bool old_have_bare_star = comp->have_bare_star;
826 int old_param_pass = comp->param_pass;
827 int old_param_pass_num_dict_params = comp->param_pass_num_dict_params;
828 int old_param_pass_num_default_params = comp->param_pass_num_default_params;
829
830 // compile default parameters
831 comp->have_bare_star = false;
832 comp->param_pass = 1; // pass 1 does any default parameters after bare star
833 comp->param_pass_num_dict_params = 0;
834 comp->param_pass_num_default_params = 0;
835 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
836 comp->have_bare_star = false;
837 comp->param_pass = 2; // pass 2 does any default parameters before bare star
838 comp->param_pass_num_dict_params = 0;
839 comp->param_pass_num_default_params = 0;
840 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
841
842 // get the scope for this function
843 scope_t *fscope = (scope_t*)pns->nodes[4];
844
845 // make the function
846 close_over_variables_etc(comp, fscope, comp->param_pass_num_dict_params, comp->param_pass_num_default_params);
847
848 // restore variables
849 comp->have_bare_star = old_have_bare_star;
850 comp->param_pass = old_param_pass;
851 comp->param_pass_num_dict_params = old_param_pass_num_dict_params;
852 comp->param_pass_num_default_params = old_param_pass_num_default_params;
853
854 // return its name (the 'f' in "def f(...):")
855 return fscope->simple_name;
856}
857
858// leaves class object on stack
859// returns class name
Damiend99b0522013-12-21 18:17:45 +0000860qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100861 if (comp->pass == PASS_1) {
862 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000863 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100864 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000865 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100866 }
867
868 EMIT(load_build_class);
869
870 // scope for this class
871 scope_t *cscope = (scope_t*)pns->nodes[3];
872
873 // compile the class
874 close_over_variables_etc(comp, cscope, 0, 0);
875
876 // get its name
Damien Georgeb9791222014-01-23 00:34:21 +0000877 EMIT_ARG(load_const_id, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100878
879 // nodes[1] has parent classes, if any
Damiend99b0522013-12-21 18:17:45 +0000880 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +0100881 // no parent classes
Damien Georgeb9791222014-01-23 00:34:21 +0000882 EMIT_ARG(call_function, 2, 0, false, false);
Damien429d7192013-10-04 19:53:11 +0100883 } else {
884 // have a parent class or classes
885 // TODO what if we have, eg, *a or **a in the parent list?
886 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +0000887 EMIT_ARG(call_function, 2 + list_len(pns->nodes[1], PN_arglist), 0, false, false);
Damien429d7192013-10-04 19:53:11 +0100888 }
889
890 // return its name (the 'C' in class C(...):")
891 return cscope->simple_name;
892}
893
Damien6cdd3af2013-10-05 18:08:26 +0100894// returns true if it was a built-in decorator (even if the built-in had an error)
Damiend99b0522013-12-21 18:17:45 +0000895static 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 +0000896 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100897 return false;
898 }
899
900 if (name_len != 2) {
901 printf("SyntaxError: invalid micropython decorator\n");
902 return true;
903 }
904
Damiend99b0522013-12-21 18:17:45 +0000905 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000906 if (attr == MP_QSTR_byte_code) {
Damien5ac1b2e2013-10-18 19:58:12 +0100907 *emit_options = EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +0100908#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000909 } else if (attr == MP_QSTR_native) {
Damien6cdd3af2013-10-05 18:08:26 +0100910 *emit_options = EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000911 } else if (attr == MP_QSTR_viper) {
Damien7af3d192013-10-07 00:02:49 +0100912 *emit_options = EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100913#endif
Damien3ef4abb2013-10-12 16:53:13 +0100914#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000915 } else if (attr == MP_QSTR_asm_thumb) {
Damien5bfb7592013-10-05 18:41:24 +0100916 *emit_options = EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +0100917#endif
Damien6cdd3af2013-10-05 18:08:26 +0100918 } else {
Damience89a212013-10-15 22:25:17 +0100919 printf("SyntaxError: invalid micropython decorator '%s'\n", qstr_str(attr));
Damien6cdd3af2013-10-05 18:08:26 +0100920 }
921
922 return true;
923}
924
Damiend99b0522013-12-21 18:17:45 +0000925void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100926 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +0000927 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +0100928 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
929
Damien6cdd3af2013-10-05 18:08:26 +0100930 // inherit emit options for this function/class definition
931 uint emit_options = comp->scope_cur->emit_options;
932
933 // compile each decorator
934 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +0100935 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000936 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
937 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +0100938
939 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +0000940 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +0100941 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
942
943 // check for built-in decorators
944 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
945 // this was a built-in
946 num_built_in_decorators += 1;
947
948 } else {
949 // not a built-in, compile normally
950
951 // compile the decorator function
952 compile_node(comp, name_nodes[0]);
953 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +0000954 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +0000955 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +0100956 }
957
958 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +0000959 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +0100960 // call the decorator function with the arguments in nodes[1]
961 compile_node(comp, pns_decorator->nodes[1]);
962 }
Damien429d7192013-10-04 19:53:11 +0100963 }
964 }
965
966 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +0000967 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +0100968 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +0000969 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +0100970 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +0000971 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +0100972 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +0100973 } else {
974 // shouldn't happen
975 assert(0);
976 }
977
978 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +0100979 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +0000980 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +0100981 }
982
983 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +0000984 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +0100985}
986
Damiend99b0522013-12-21 18:17:45 +0000987void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +0100988 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +0100989 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +0000990 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +0100991}
992
Damiend99b0522013-12-21 18:17:45 +0000993void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
994 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +0000995 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +0000996 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
997 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100998
999 compile_node(comp, pns->nodes[0]); // base of the power node
1000
Damiend99b0522013-12-21 18:17:45 +00001001 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1002 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1003 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1004 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001005 for (int i = 0; i < n - 1; i++) {
1006 compile_node(comp, pns1->nodes[i]);
1007 }
Damiend99b0522013-12-21 18:17:45 +00001008 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1009 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001010 }
Damiend99b0522013-12-21 18:17:45 +00001011 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien429d7192013-10-04 19:53:11 +01001012 // SyntaxError: can't delete a function call
1013 assert(0);
Damiend99b0522013-12-21 18:17:45 +00001014 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001015 compile_node(comp, pns1->nodes[0]);
1016 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001017 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1018 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001019 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001020 } else {
1021 // shouldn't happen
1022 assert(0);
1023 }
1024 } else {
1025 // shouldn't happen
1026 assert(0);
1027 }
1028
Damiend99b0522013-12-21 18:17:45 +00001029 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001030 // SyntaxError, cannot delete
1031 assert(0);
1032 }
Damiend99b0522013-12-21 18:17:45 +00001033 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1034 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1035 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1036 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001037 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1038
Damiend99b0522013-12-21 18:17:45 +00001039 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1040 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1041 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001042 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001043 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001044 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001045 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001046 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001047 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001048 c_del_stmt(comp, pns->nodes[0]);
1049 for (int i = 0; i < n; i++) {
1050 c_del_stmt(comp, pns1->nodes[i]);
1051 }
Damiend99b0522013-12-21 18:17:45 +00001052 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001053 // TODO not implemented; can't del comprehension?
1054 assert(0);
1055 } else {
1056 // sequence with 2 items
1057 goto sequence_with_2_items;
1058 }
1059 } else {
1060 // sequence with 2 items
1061 sequence_with_2_items:
1062 c_del_stmt(comp, pns->nodes[0]);
1063 c_del_stmt(comp, pns->nodes[1]);
1064 }
1065 } else {
1066 // tuple with 1 element
1067 c_del_stmt(comp, pn);
1068 }
1069 } else {
1070 // not implemented
1071 assert(0);
1072 }
1073}
1074
Damiend99b0522013-12-21 18:17:45 +00001075void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001076 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1077}
1078
Damiend99b0522013-12-21 18:17:45 +00001079void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001080 if (comp->break_label == 0) {
1081 printf("ERROR: cannot break from here\n");
1082 }
Damien Georgeb9791222014-01-23 00:34:21 +00001083 EMIT_ARG(break_loop, comp->break_label);
Damien429d7192013-10-04 19:53:11 +01001084}
1085
Damiend99b0522013-12-21 18:17:45 +00001086void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001087 if (comp->continue_label == 0) {
1088 printf("ERROR: cannot continue from here\n");
1089 }
1090 if (comp->except_nest_level > 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001091 EMIT_ARG(continue_loop, comp->continue_label);
Damien429d7192013-10-04 19:53:11 +01001092 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001093 EMIT_ARG(jump, comp->continue_label);
Damien429d7192013-10-04 19:53:11 +01001094 }
1095}
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
Damiend99b0522013-12-21 18:17:45 +00001390void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001391 int old_break_label = comp->break_label;
1392 int old_continue_label = comp->continue_label;
1393
Damienb05d7072013-10-05 13:37:10 +01001394 int break_label = comp_next_label(comp);
1395 int continue_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001396
1397 comp->break_label = break_label;
1398 comp->continue_label = continue_label;
1399
Damience89a212013-10-15 22:25:17 +01001400 // compared to CPython, we have an optimised version of while loops
1401#if MICROPY_EMIT_CPYTHON
1402 int done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001403 EMIT_ARG(setup_loop, break_label);
1404 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001405 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1406 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001407 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001408 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001409 }
Damien Georgeb9791222014-01-23 00:34:21 +00001410 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001411 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1412 // this is a small hack to agree with CPython
1413 if (!node_is_const_true(pns->nodes[0])) {
1414 EMIT(pop_block);
1415 }
Damience89a212013-10-15 22:25:17 +01001416#else
1417 int top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001418 EMIT_ARG(jump, continue_label);
1419 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001420 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001421 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001422 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1423#endif
1424
1425 // break/continue apply to outer loop (if any) in the else block
1426 comp->break_label = old_break_label;
1427 comp->continue_label = old_continue_label;
Damien429d7192013-10-04 19:53:11 +01001428
1429 compile_node(comp, pns->nodes[2]); // else
1430
Damien Georgeb9791222014-01-23 00:34:21 +00001431 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001432}
1433
Damienf72fd0e2013-11-06 20:20:49 +00001434// TODO preload end and step onto stack if they are not constants
1435// TODO check if step is negative and do opposite test
Damiend99b0522013-12-21 18:17:45 +00001436void 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) {
Damienf72fd0e2013-11-06 20:20:49 +00001437 int old_break_label = comp->break_label;
1438 int old_continue_label = comp->continue_label;
1439
1440 int break_label = comp_next_label(comp);
1441 int continue_label = comp_next_label(comp);
1442
1443 comp->break_label = break_label;
1444 comp->continue_label = continue_label;
1445
1446 int top_label = comp_next_label(comp);
Damien George600ae732014-01-21 23:48:04 +00001447 int entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001448
1449 // compile: var = start
1450 compile_node(comp, pn_start);
1451 c_assign(comp, pn_var, ASSIGN_STORE);
1452
Damien Georgeb9791222014-01-23 00:34:21 +00001453 EMIT_ARG(jump, entry_label);
1454 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001455
Damienf3822fc2013-11-09 20:12:03 +00001456 // compile body
1457 compile_node(comp, pn_body);
1458
Damien Georgeb9791222014-01-23 00:34:21 +00001459 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001460
Damienf72fd0e2013-11-06 20:20:49 +00001461 // compile: var += step
1462 c_assign(comp, pn_var, ASSIGN_AUG_LOAD);
1463 compile_node(comp, pn_step);
Damien Georgeb9791222014-01-23 00:34:21 +00001464 EMIT_ARG(binary_op, RT_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001465 c_assign(comp, pn_var, ASSIGN_AUG_STORE);
1466
Damien Georgeb9791222014-01-23 00:34:21 +00001467 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001468
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001469 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001470 compile_node(comp, pn_var);
1471 compile_node(comp, pn_end);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001472 if (MP_PARSE_NODE_LEAF_ARG(pn_step) >= 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001473 EMIT_ARG(binary_op, RT_COMPARE_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001474 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001475 EMIT_ARG(binary_op, RT_COMPARE_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001476 }
Damien Georgeb9791222014-01-23 00:34:21 +00001477 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001478
1479 // break/continue apply to outer loop (if any) in the else block
1480 comp->break_label = old_break_label;
1481 comp->continue_label = old_continue_label;
1482
1483 compile_node(comp, pn_else);
1484
Damien Georgeb9791222014-01-23 00:34:21 +00001485 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001486}
1487
Damiend99b0522013-12-21 18:17:45 +00001488void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001489#if !MICROPY_EMIT_CPYTHON
1490 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1491 // this is actually slower, but uses no heap memory
1492 // for viper it will be much, much faster
Damiend99b0522013-12-21 18:17:45 +00001493 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)) {
1494 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001495 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1496 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1497 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1498 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001499 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1500 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001501 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001502 mp_parse_node_t pn_range_start;
1503 mp_parse_node_t pn_range_end;
1504 mp_parse_node_t pn_range_step;
1505 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001506 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001507 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001508 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001509 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001510 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001511 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001512 } else if (n_args == 2) {
1513 pn_range_start = args[0];
1514 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001515 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001516 } else {
1517 pn_range_start = args[0];
1518 pn_range_end = args[1];
1519 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001520 // We need to know sign of step. This is possible only if it's constant
1521 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1522 optimize = false;
1523 }
Damienf72fd0e2013-11-06 20:20:49 +00001524 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001525 }
1526 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001527 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1528 return;
1529 }
1530 }
1531 }
1532#endif
1533
Damien429d7192013-10-04 19:53:11 +01001534 int old_break_label = comp->break_label;
1535 int old_continue_label = comp->continue_label;
1536
Damienb05d7072013-10-05 13:37:10 +01001537 int for_label = comp_next_label(comp);
1538 int pop_label = comp_next_label(comp);
1539 int end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001540
Damienb05d7072013-10-05 13:37:10 +01001541 int break_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001542
1543 comp->continue_label = for_label;
1544 comp->break_label = break_label;
1545
Damience89a212013-10-15 22:25:17 +01001546 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1547#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001548 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001549#endif
1550
Damien429d7192013-10-04 19:53:11 +01001551 compile_node(comp, pns->nodes[1]); // iterator
1552 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00001553 EMIT_ARG(label_assign, for_label);
1554 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001555 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1556 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001557 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001558 EMIT_ARG(jump, for_label);
Damien429d7192013-10-04 19:53:11 +01001559 }
Damien Georgeb9791222014-01-23 00:34:21 +00001560 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001561 EMIT(for_iter_end);
1562
1563 // break/continue apply to outer loop (if any) in the else block
1564 comp->break_label = old_break_label;
1565 comp->continue_label = old_continue_label;
1566
Damience89a212013-10-15 22:25:17 +01001567#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001568 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001569#endif
Damien429d7192013-10-04 19:53:11 +01001570
1571 compile_node(comp, pns->nodes[3]); // else (not tested)
1572
Damien Georgeb9791222014-01-23 00:34:21 +00001573 EMIT_ARG(label_assign, break_label);
1574 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001575}
1576
Damiend99b0522013-12-21 18:17:45 +00001577void 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 +01001578 // this function is a bit of a hack at the moment
1579 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1580
1581 // setup code
1582 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001583 int l1 = comp_next_label(comp);
1584 int success_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001585 comp->except_nest_level += 1; // for correct handling of continue
Damien Georgeb9791222014-01-23 00:34:21 +00001586 EMIT_ARG(setup_except, l1);
Damien429d7192013-10-04 19:53:11 +01001587 compile_node(comp, pn_body); // body
1588 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001589 EMIT_ARG(jump, success_label);
1590 EMIT_ARG(label_assign, l1);
Damienb05d7072013-10-05 13:37:10 +01001591 int l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001592
1593 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001594 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1595 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001596
1597 qstr qstr_exception_local = 0;
Damienb05d7072013-10-05 13:37:10 +01001598 int end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001599
Damiend99b0522013-12-21 18:17:45 +00001600 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001601 // this is a catch all exception handler
1602 if (i + 1 != n_except) {
1603 printf("SyntaxError: default 'except:' must be last\n");
1604 return;
1605 }
1606 } else {
1607 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001608 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1609 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1610 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1611 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001612 // handler binds the exception to a local
1613 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001614 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001615 }
1616 }
1617 EMIT(dup_top);
1618 compile_node(comp, pns_exception_expr);
Damien Georgeb9791222014-01-23 00:34:21 +00001619 EMIT_ARG(binary_op, RT_COMPARE_OP_EXCEPTION_MATCH);
1620 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001621 }
1622
1623 EMIT(pop_top);
1624
1625 if (qstr_exception_local == 0) {
1626 EMIT(pop_top);
1627 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001628 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001629 }
1630
1631 EMIT(pop_top);
1632
Damiene2880aa2013-12-20 14:22:59 +00001633 int l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001634 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001635 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001636 EMIT_ARG(setup_finally, l3);
Damien429d7192013-10-04 19:53:11 +01001637 }
1638 compile_node(comp, pns_except->nodes[1]);
1639 if (qstr_exception_local != 0) {
1640 EMIT(pop_block);
1641 }
1642 EMIT(pop_except);
1643 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001644 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1645 EMIT_ARG(label_assign, l3);
1646 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1647 EMIT_ARG(store_id, qstr_exception_local);
1648 EMIT_ARG(delete_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001649 EMIT(end_finally);
1650 }
Damien Georgeb9791222014-01-23 00:34:21 +00001651 EMIT_ARG(jump, l2);
1652 EMIT_ARG(label_assign, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001653 }
1654
1655 EMIT(end_finally);
Damien Georgeb9791222014-01-23 00:34:21 +00001656 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001657 comp->except_nest_level -= 1;
1658 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001659 EMIT_ARG(label_assign, l2);
1660 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001661}
1662
Damiend99b0522013-12-21 18:17:45 +00001663void 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 +01001664 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1665 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001666 int l_finally_block = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001667 EMIT_ARG(setup_finally, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001668 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001669 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien429d7192013-10-04 19:53:11 +01001670 compile_node(comp, pn_body);
1671 } else {
1672 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1673 }
1674 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001675 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1676 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001677 compile_node(comp, pn_finally);
1678 EMIT(end_finally);
Damien Georgeb9791222014-01-23 00:34:21 +00001679 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001680}
1681
Damiend99b0522013-12-21 18:17:45 +00001682void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1683 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1684 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1685 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001686 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001687 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1688 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001689 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001690 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001691 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001692 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001693 // no finally
1694 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1695 } else {
1696 // have finally
Damiend99b0522013-12-21 18:17:45 +00001697 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 +01001698 }
1699 } else {
1700 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001701 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001702 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001703 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001704 }
1705 } else {
1706 // shouldn't happen
1707 assert(0);
1708 }
1709}
1710
Damiend99b0522013-12-21 18:17:45 +00001711void 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 +01001712 if (n == 0) {
1713 // no more pre-bits, compile the body of the with
1714 compile_node(comp, body);
1715 } else {
Damienb05d7072013-10-05 13:37:10 +01001716 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001717 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001718 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001719 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001720 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001721 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001722 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1723 } else {
1724 // this pre-bit is just an expression
1725 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001726 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001727 EMIT(pop_top);
1728 }
1729 // compile additional pre-bits and the body
1730 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1731 // finish this with block
1732 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001733 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1734 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001735 EMIT(with_cleanup);
1736 EMIT(end_finally);
1737 }
1738}
1739
Damiend99b0522013-12-21 18:17:45 +00001740void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001741 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001742 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001743 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1744 assert(n > 0);
1745
1746 // compile in a nested fashion
1747 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1748}
1749
Damiend99b0522013-12-21 18:17:45 +00001750void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1751 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001752 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1753 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001754 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001755 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001756 EMIT_ARG(call_function, 1, 0, false, false);
Damien5ac1b2e2013-10-18 19:58:12 +01001757 EMIT(pop_top);
1758
Damien429d7192013-10-04 19:53:11 +01001759 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001760 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001761 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001762 // do nothing with a lonely constant
1763 } else {
1764 compile_node(comp, pns->nodes[0]); // just an expression
1765 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1766 }
Damien429d7192013-10-04 19:53:11 +01001767 }
1768 } else {
Damiend99b0522013-12-21 18:17:45 +00001769 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1770 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001771 if (kind == PN_expr_stmt_augassign) {
1772 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1773 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001774 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001775 // note that we don't really need to implement separate inplace ops, just normal binary ops will suffice
Damiend99b0522013-12-21 18:17:45 +00001776 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001777 case MP_TOKEN_DEL_PIPE_EQUAL: EMIT_ARG(binary_op, RT_BINARY_OP_INPLACE_OR); break;
1778 case MP_TOKEN_DEL_CARET_EQUAL: EMIT_ARG(binary_op, RT_BINARY_OP_INPLACE_XOR); break;
1779 case MP_TOKEN_DEL_AMPERSAND_EQUAL: EMIT_ARG(binary_op, RT_BINARY_OP_INPLACE_AND); break;
1780 case MP_TOKEN_DEL_DBL_LESS_EQUAL: EMIT_ARG(binary_op, RT_BINARY_OP_INPLACE_LSHIFT); break;
1781 case MP_TOKEN_DEL_DBL_MORE_EQUAL: EMIT_ARG(binary_op, RT_BINARY_OP_INPLACE_RSHIFT); break;
1782 case MP_TOKEN_DEL_PLUS_EQUAL: EMIT_ARG(binary_op, RT_BINARY_OP_INPLACE_ADD); break;
1783 case MP_TOKEN_DEL_MINUS_EQUAL: EMIT_ARG(binary_op, RT_BINARY_OP_INPLACE_SUBTRACT); break;
1784 case MP_TOKEN_DEL_STAR_EQUAL: EMIT_ARG(binary_op, RT_BINARY_OP_INPLACE_MULTIPLY); break;
1785 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: EMIT_ARG(binary_op, RT_BINARY_OP_INPLACE_FLOOR_DIVIDE); break;
1786 case MP_TOKEN_DEL_SLASH_EQUAL: EMIT_ARG(binary_op, RT_BINARY_OP_INPLACE_TRUE_DIVIDE); break;
1787 case MP_TOKEN_DEL_PERCENT_EQUAL: EMIT_ARG(binary_op, RT_BINARY_OP_INPLACE_MODULO); break;
1788 case MP_TOKEN_DEL_DBL_STAR_EQUAL: EMIT_ARG(binary_op, RT_BINARY_OP_INPLACE_POWER); break;
Damien429d7192013-10-04 19:53:11 +01001789 default: assert(0); // shouldn't happen
1790 }
1791 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1792 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001793 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1794 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001795 // following CPython, we store left-most first
1796 if (rhs > 0) {
1797 EMIT(dup_top);
1798 }
1799 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1800 for (int i = 0; i < rhs; i++) {
1801 if (i + 1 < rhs) {
1802 EMIT(dup_top);
1803 }
Damiend99b0522013-12-21 18:17:45 +00001804 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001805 }
1806 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001807 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1808 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1809 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1810 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01001811 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001812 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1813 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001814 compile_node(comp, pns10->nodes[0]); // rhs
1815 compile_node(comp, pns10->nodes[1]); // rhs
1816 EMIT(rot_two);
1817 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1818 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00001819 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1820 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1821 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1822 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01001823 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001824 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1825 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001826 compile_node(comp, pns10->nodes[0]); // rhs
1827 compile_node(comp, pns10->nodes[1]); // rhs
1828 compile_node(comp, pns10->nodes[2]); // rhs
1829 EMIT(rot_three);
1830 EMIT(rot_two);
1831 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1832 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1833 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1834 } else {
1835 compile_node(comp, pns1->nodes[0]); // rhs
1836 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1837 }
1838 } else {
1839 // shouldn't happen
1840 assert(0);
1841 }
1842 }
1843}
1844
Damiend99b0522013-12-21 18:17:45 +00001845void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, rt_binary_op_t binary_op) {
1846 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001847 compile_node(comp, pns->nodes[0]);
1848 for (int i = 1; i < num_nodes; i += 1) {
1849 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001850 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001851 }
1852}
1853
Damiend99b0522013-12-21 18:17:45 +00001854void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1855 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1856 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001857
1858 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001859 int l_fail = comp_next_label(comp);
1860 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001861 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1862 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001863 EMIT_ARG(jump, l_end);
1864 EMIT_ARG(label_assign, l_fail);
1865 EMIT_ARG(set_stack_size, stack_size); // force stack size reset
Damien429d7192013-10-04 19:53:11 +01001866 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001867 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001868}
1869
Damiend99b0522013-12-21 18:17:45 +00001870void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001871 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00001872 //mp_parse_node_t pn_params = pns->nodes[0];
1873 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001874
1875 if (comp->pass == PASS_1) {
1876 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00001877 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 +01001878 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001879 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001880 }
1881
1882 // get the scope for this lambda
1883 scope_t *this_scope = (scope_t*)pns->nodes[2];
1884
1885 // make the lambda
1886 close_over_variables_etc(comp, this_scope, 0, 0);
1887}
1888
Damiend99b0522013-12-21 18:17:45 +00001889void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001890 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001891 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001892 for (int i = 0; i < n; i += 1) {
1893 compile_node(comp, pns->nodes[i]);
1894 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00001895 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01001896 }
1897 }
Damien Georgeb9791222014-01-23 00:34:21 +00001898 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001899}
1900
Damiend99b0522013-12-21 18:17:45 +00001901void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001902 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001903 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001904 for (int i = 0; i < n; i += 1) {
1905 compile_node(comp, pns->nodes[i]);
1906 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00001907 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01001908 }
1909 }
Damien Georgeb9791222014-01-23 00:34:21 +00001910 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001911}
1912
Damiend99b0522013-12-21 18:17:45 +00001913void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001914 compile_node(comp, pns->nodes[0]);
Damien Georgee4b6a072014-01-28 23:27:35 +00001915#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001916 EMIT_ARG(unary_op, RT_UNARY_OP_NOT);
Damien Georgee4b6a072014-01-28 23:27:35 +00001917#else
1918 // eliminate use of NOT byte code
1919 int l_load_false = comp_next_label(comp);
1920 int l_done = comp_next_label(comp);
1921 int stack_size = EMIT(get_stack_size);
1922 EMIT_ARG(pop_jump_if_true, l_load_false);
1923 EMIT_ARG(load_const_tok, MP_TOKEN_KW_TRUE);
1924 EMIT_ARG(jump, l_done);
1925 EMIT_ARG(label_assign, l_load_false);
1926 EMIT_ARG(load_const_tok, MP_TOKEN_KW_FALSE);
1927 EMIT_ARG(label_assign, l_done);
1928 EMIT_ARG(set_stack_size, stack_size); // force stack size since it counts 1 pop and 2 pushes statically, but really it's 1 pop and 1 push dynamically
1929#endif
Damien429d7192013-10-04 19:53:11 +01001930}
1931
Damiend99b0522013-12-21 18:17:45 +00001932void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001933 int stack_size = EMIT(get_stack_size);
Damiend99b0522013-12-21 18:17:45 +00001934 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001935 compile_node(comp, pns->nodes[0]);
1936 bool multi = (num_nodes > 3);
1937 int l_fail = 0;
1938 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001939 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001940 }
1941 for (int i = 1; i + 1 < num_nodes; i += 2) {
1942 compile_node(comp, pns->nodes[i + 1]);
1943 if (i + 2 < num_nodes) {
1944 EMIT(dup_top);
1945 EMIT(rot_three);
1946 }
Damiend99b0522013-12-21 18:17:45 +00001947 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_LESS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001948 EMIT_ARG(binary_op, RT_COMPARE_OP_LESS);
Damiend99b0522013-12-21 18:17:45 +00001949 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MORE)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001950 EMIT_ARG(binary_op, RT_COMPARE_OP_MORE);
Damiend99b0522013-12-21 18:17:45 +00001951 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_EQUAL)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001952 EMIT_ARG(binary_op, RT_COMPARE_OP_EQUAL);
Damiend99b0522013-12-21 18:17:45 +00001953 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_LESS_EQUAL)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001954 EMIT_ARG(binary_op, RT_COMPARE_OP_LESS_EQUAL);
Damiend99b0522013-12-21 18:17:45 +00001955 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MORE_EQUAL)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001956 EMIT_ARG(binary_op, RT_COMPARE_OP_MORE_EQUAL);
Damiend99b0522013-12-21 18:17:45 +00001957 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_NOT_EQUAL)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001958 EMIT_ARG(binary_op, RT_COMPARE_OP_NOT_EQUAL);
Damiend99b0522013-12-21 18:17:45 +00001959 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_KW_IN)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001960 EMIT_ARG(binary_op, RT_COMPARE_OP_IN);
Damiend99b0522013-12-21 18:17:45 +00001961 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
1962 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
1963 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01001964 if (kind == PN_comp_op_not_in) {
Damien Georgeb9791222014-01-23 00:34:21 +00001965 EMIT_ARG(binary_op, RT_COMPARE_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01001966 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00001967 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001968 EMIT_ARG(binary_op, RT_COMPARE_OP_IS);
Damien429d7192013-10-04 19:53:11 +01001969 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001970 EMIT_ARG(binary_op, RT_COMPARE_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01001971 }
1972 } else {
1973 // shouldn't happen
1974 assert(0);
1975 }
1976 } else {
1977 // shouldn't happen
1978 assert(0);
1979 }
1980 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00001981 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01001982 }
1983 }
1984 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001985 int l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001986 EMIT_ARG(jump, l_end);
1987 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001988 EMIT(rot_two);
1989 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00001990 EMIT_ARG(label_assign, l_end);
1991 EMIT_ARG(set_stack_size, stack_size + 1); // force stack size
Damien429d7192013-10-04 19:53:11 +01001992 }
1993}
1994
Damiend99b0522013-12-21 18:17:45 +00001995void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001996 // TODO
1997 assert(0);
1998 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001999 //EMIT_ARG(unary_op, "UNARY_STAR");
Damien429d7192013-10-04 19:53:11 +01002000}
2001
Damiend99b0522013-12-21 18:17:45 +00002002void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002003 c_binary_op(comp, pns, RT_BINARY_OP_OR);
2004}
2005
Damiend99b0522013-12-21 18:17:45 +00002006void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002007 c_binary_op(comp, pns, RT_BINARY_OP_XOR);
2008}
2009
Damiend99b0522013-12-21 18:17:45 +00002010void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002011 c_binary_op(comp, pns, RT_BINARY_OP_AND);
2012}
2013
Damiend99b0522013-12-21 18:17:45 +00002014void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2015 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002016 compile_node(comp, pns->nodes[0]);
2017 for (int i = 1; i + 1 < num_nodes; i += 2) {
2018 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002019 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002020 EMIT_ARG(binary_op, RT_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002021 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002022 EMIT_ARG(binary_op, RT_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002023 } else {
2024 // shouldn't happen
2025 assert(0);
2026 }
2027 }
2028}
2029
Damiend99b0522013-12-21 18:17:45 +00002030void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2031 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002032 compile_node(comp, pns->nodes[0]);
2033 for (int i = 1; i + 1 < num_nodes; i += 2) {
2034 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002035 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002036 EMIT_ARG(binary_op, RT_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002037 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002038 EMIT_ARG(binary_op, RT_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002039 } else {
2040 // shouldn't happen
2041 assert(0);
2042 }
2043 }
2044}
2045
Damiend99b0522013-12-21 18:17:45 +00002046void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2047 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002048 compile_node(comp, pns->nodes[0]);
2049 for (int i = 1; i + 1 < num_nodes; i += 2) {
2050 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002051 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002052 EMIT_ARG(binary_op, RT_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002053 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002054 EMIT_ARG(binary_op, RT_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002055 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002056 EMIT_ARG(binary_op, RT_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002057 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002058 EMIT_ARG(binary_op, RT_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002059 } else {
2060 // shouldn't happen
2061 assert(0);
2062 }
2063 }
2064}
2065
Damiend99b0522013-12-21 18:17:45 +00002066void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002067 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002068 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002069 EMIT_ARG(unary_op, RT_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002070 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002071 EMIT_ARG(unary_op, RT_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002072 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002073 EMIT_ARG(unary_op, RT_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002074 } else {
2075 // shouldn't happen
2076 assert(0);
2077 }
2078}
2079
Damiend99b0522013-12-21 18:17:45 +00002080void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_struct_t *pns, bool is_method_call) {
Damien429d7192013-10-04 19:53:11 +01002081 // function to call is on top of stack
2082
2083 int old_n_arg_keyword = comp->n_arg_keyword;
2084 bool old_have_star_arg = comp->have_star_arg;
2085 bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
2086 comp->n_arg_keyword = 0;
2087 comp->have_star_arg = false;
2088 comp->have_dbl_star_arg = false;
2089
2090 compile_node(comp, pns->nodes[0]); // arguments to function call; can be null
2091
2092 // compute number of positional arguments
2093 int n_positional = list_len(pns->nodes[0], PN_arglist) - comp->n_arg_keyword;
2094 if (comp->have_star_arg) {
2095 n_positional -= 1;
2096 }
2097 if (comp->have_dbl_star_arg) {
2098 n_positional -= 1;
2099 }
2100
2101 if (is_method_call) {
Damien Georgeb9791222014-01-23 00:34:21 +00002102 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 +01002103 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002104 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 +01002105 }
2106
2107 comp->n_arg_keyword = old_n_arg_keyword;
2108 comp->have_star_arg = old_have_star_arg;
2109 comp->have_dbl_star_arg = old_have_dbl_star_arg;
2110}
2111
Damiend99b0522013-12-21 18:17:45 +00002112void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2113 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002114 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002115 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 +01002116 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002117 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2118 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002119 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien429d7192013-10-04 19:53:11 +01002120 compile_trailer_paren_helper(comp, pns_paren, true);
2121 i += 1;
2122 } else {
2123 compile_node(comp, pns->nodes[i]);
2124 }
2125 }
2126}
2127
Damiend99b0522013-12-21 18:17:45 +00002128void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002129 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002130 EMIT_ARG(binary_op, RT_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002131}
2132
Damiend99b0522013-12-21 18:17:45 +00002133void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002134 // a list of strings
Damien63321742013-12-10 17:41:49 +00002135
2136 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002137 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002138 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002139 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002140 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002141 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2142 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2143 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002144 if (i == 0) {
2145 string_kind = pn_kind;
2146 } else if (pn_kind != string_kind) {
2147 printf("SyntaxError: cannot mix bytes and nonbytes literals\n");
2148 return;
2149 }
Damien George55baff42014-01-21 21:40:13 +00002150 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002151 }
Damien63321742013-12-10 17:41:49 +00002152
Damien63321742013-12-10 17:41:49 +00002153 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002154 byte *q_ptr;
2155 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002156 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002157 uint s_len;
2158 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002159 memcpy(s_dest, s, s_len);
2160 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002161 }
Damien George55baff42014-01-21 21:40:13 +00002162 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002163
Damien Georgeb9791222014-01-23 00:34:21 +00002164 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002165}
2166
2167// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002168void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2169 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2170 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2171 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002172
2173 if (comp->pass == PASS_1) {
2174 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002175 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 +01002176 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002177 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002178 }
2179
2180 // get the scope for this comprehension
2181 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2182
2183 // compile the comprehension
2184 close_over_variables_etc(comp, this_scope, 0, 0);
2185
2186 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2187 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002188 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01002189}
2190
Damiend99b0522013-12-21 18:17:45 +00002191void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2192 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002193 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002194 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2195 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2196 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2197 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2198 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2199 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2200 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002201 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002202 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002203 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002204 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002205 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002206 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002207 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002208 // generator expression
2209 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2210 } else {
2211 // tuple with 2 items
2212 goto tuple_with_2_items;
2213 }
2214 } else {
2215 // tuple with 2 items
2216 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002217 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002218 }
2219 } else {
2220 // parenthesis around a single item, is just that item
2221 compile_node(comp, pns->nodes[0]);
2222 }
2223}
2224
Damiend99b0522013-12-21 18:17:45 +00002225void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2226 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002227 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002228 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002229 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2230 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2231 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2232 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2233 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002234 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002235 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002236 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002237 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002238 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002239 // list of many items
2240 compile_node(comp, pns2->nodes[0]);
2241 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002242 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002243 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002244 // list comprehension
2245 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2246 } else {
2247 // list with 2 items
2248 goto list_with_2_items;
2249 }
2250 } else {
2251 // list with 2 items
2252 list_with_2_items:
2253 compile_node(comp, pns2->nodes[0]);
2254 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002255 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002256 }
2257 } else {
2258 // list with 1 item
2259 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002260 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002261 }
2262}
2263
Damiend99b0522013-12-21 18:17:45 +00002264void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2265 mp_parse_node_t pn = pns->nodes[0];
2266 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002267 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002268 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002269 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2270 pns = (mp_parse_node_struct_t*)pn;
2271 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002272 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002273 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002274 compile_node(comp, pn);
2275 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002276 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2277 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2278 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2279 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002280 // dict/set with multiple elements
2281
2282 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002283 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002284 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2285
2286 // first element sets whether it's a dict or set
2287 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002288 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002289 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002290 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002291 compile_node(comp, pns->nodes[0]);
2292 EMIT(store_map);
2293 is_dict = true;
2294 } else {
2295 // a set
2296 compile_node(comp, pns->nodes[0]); // 1st value of set
2297 is_dict = false;
2298 }
2299
2300 // process rest of elements
2301 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002302 mp_parse_node_t pn = nodes[i];
2303 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002304 compile_node(comp, pn);
2305 if (is_dict) {
2306 if (!is_key_value) {
2307 printf("SyntaxError?: expecting key:value for dictionary");
2308 return;
2309 }
2310 EMIT(store_map);
2311 } else {
2312 if (is_key_value) {
2313 printf("SyntaxError?: expecting just a value for set");
2314 return;
2315 }
2316 }
2317 }
2318
2319 // if it's a set, build it
2320 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002321 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002322 }
Damiend99b0522013-12-21 18:17:45 +00002323 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002324 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002325 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002326 // a dictionary comprehension
2327 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2328 } else {
2329 // a set comprehension
2330 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2331 }
2332 } else {
2333 // shouldn't happen
2334 assert(0);
2335 }
2336 } else {
2337 // set with one element
2338 goto set_with_one_element;
2339 }
2340 } else {
2341 // set with one element
2342 set_with_one_element:
2343 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002344 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002345 }
2346}
2347
Damiend99b0522013-12-21 18:17:45 +00002348void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002349 compile_trailer_paren_helper(comp, pns, false);
2350}
2351
Damiend99b0522013-12-21 18:17:45 +00002352void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002353 // object who's index we want is on top of stack
2354 compile_node(comp, pns->nodes[0]); // the index
Damien Georgeb9791222014-01-23 00:34:21 +00002355 EMIT_ARG(binary_op, RT_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002356}
2357
Damiend99b0522013-12-21 18:17:45 +00002358void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002359 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002360 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002361}
2362
Damiend99b0522013-12-21 18:17:45 +00002363void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2364 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2365 mp_parse_node_t pn = pns->nodes[0];
2366 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002367 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002368 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2369 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002370 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2371 pns = (mp_parse_node_struct_t*)pn;
2372 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002373 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002374 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002375 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002376 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002377 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002378 } else {
2379 // [?::x]
2380 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002381 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002382 }
Damiend99b0522013-12-21 18:17:45 +00002383 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002384 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002385 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2386 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2387 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2388 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002389 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002390 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002391 } else {
2392 // [?:x:x]
2393 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002394 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002395 }
2396 } else {
2397 // [?:x]
2398 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002399 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002400 }
2401 } else {
2402 // [?:x]
2403 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002404 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002405 }
2406}
2407
Damiend99b0522013-12-21 18:17:45 +00002408void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002409 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002410 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2411 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002412}
2413
Damiend99b0522013-12-21 18:17:45 +00002414void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002415 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002416 compile_subscript_3_helper(comp, pns);
2417}
2418
Damiend99b0522013-12-21 18:17:45 +00002419void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002420 // if this is called then we are compiling a dict key:value pair
2421 compile_node(comp, pns->nodes[1]); // value
2422 compile_node(comp, pns->nodes[0]); // key
2423}
2424
Damiend99b0522013-12-21 18:17:45 +00002425void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002426 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002427 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002428 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002429}
2430
Damiend99b0522013-12-21 18:17:45 +00002431void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002432 if (comp->have_star_arg) {
2433 printf("SyntaxError?: can't have multiple *x\n");
2434 return;
2435 }
2436 comp->have_star_arg = true;
2437 compile_node(comp, pns->nodes[0]);
2438}
2439
Damiend99b0522013-12-21 18:17:45 +00002440void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002441 if (comp->have_dbl_star_arg) {
2442 printf("SyntaxError?: can't have multiple **x\n");
2443 return;
2444 }
2445 comp->have_dbl_star_arg = true;
2446 compile_node(comp, pns->nodes[0]);
2447}
2448
Damiend99b0522013-12-21 18:17:45 +00002449void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2450 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2451 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2452 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2453 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002454 printf("SyntaxError?: lhs of keyword argument must be an id\n");
2455 return;
2456 }
Damien Georgeb9791222014-01-23 00:34:21 +00002457 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002458 compile_node(comp, pns2->nodes[0]);
2459 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002460 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002461 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2462 } else {
2463 // shouldn't happen
2464 assert(0);
2465 }
2466}
2467
Damiend99b0522013-12-21 18:17:45 +00002468void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002469 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
2470 printf("SyntaxError: 'yield' outside function\n");
2471 return;
2472 }
Damiend99b0522013-12-21 18:17:45 +00002473 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002474 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002475 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002476 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2477 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002478 compile_node(comp, pns->nodes[0]);
2479 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002480 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002481 EMIT(yield_from);
2482 } else {
2483 compile_node(comp, pns->nodes[0]);
2484 EMIT(yield_value);
2485 }
2486}
2487
Damiend99b0522013-12-21 18:17:45 +00002488typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Damien429d7192013-10-04 19:53:11 +01002489static compile_function_t compile_function[] = {
2490 NULL,
2491#define nc NULL
2492#define c(f) compile_##f
2493#define DEF_RULE(rule, comp, kind, arg...) comp,
2494#include "grammar.h"
2495#undef nc
2496#undef c
2497#undef DEF_RULE
2498};
2499
Damiend99b0522013-12-21 18:17:45 +00002500void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2501 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002502 // pass
Damiend99b0522013-12-21 18:17:45 +00002503 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien George08d07552014-01-29 18:58:52 +00002504 machine_int_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002505 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002506 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
2507 case MP_PARSE_NODE_SMALL_INT: EMIT_ARG(load_const_small_int, arg); break;
2508 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2509 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2510 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2511 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002512 case MP_PARSE_NODE_TOKEN:
2513 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002514 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002515 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002516 // do nothing
2517 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002518 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002519 }
2520 break;
Damien429d7192013-10-04 19:53:11 +01002521 default: assert(0);
2522 }
2523 } else {
Damiend99b0522013-12-21 18:17:45 +00002524 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002525 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002526 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002527 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002528 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002529#if MICROPY_DEBUG_PRINTERS
2530 mp_parse_node_print(pn, 0);
2531#endif
Damien429d7192013-10-04 19:53:11 +01002532 assert(0);
2533 } else {
2534 f(comp, pns);
2535 }
2536 }
2537}
2538
Damiend99b0522013-12-21 18:17:45 +00002539void 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 +01002540 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002541 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002542 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2543 if (MP_PARSE_NODE_IS_ID(pn)) {
2544 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002545 if (comp->have_bare_star) {
2546 // comes after a bare star, so doesn't count as a parameter
2547 } else {
2548 comp->scope_cur->num_params += 1;
2549 }
Damienb14de212013-10-06 00:28:28 +01002550 } else {
Damiend99b0522013-12-21 18:17:45 +00002551 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2552 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2553 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2554 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002555 //int node_index = 1; unused
2556 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002557 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002558 // this parameter has an annotation
2559 pn_annotation = pns->nodes[1];
2560 }
2561 //node_index = 2; unused
2562 }
2563 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002564 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002565 // this parameter has a default value
2566 if (comp->have_bare_star) {
2567 comp->scope_cur->num_dict_params += 1;
2568 } else {
2569 comp->scope_cur->num_default_params += 1;
2570 }
2571 }
2572 */
2573 if (comp->have_bare_star) {
2574 // comes after a bare star, so doesn't count as a parameter
2575 } else {
2576 comp->scope_cur->num_params += 1;
2577 }
Damiend99b0522013-12-21 18:17:45 +00002578 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2579 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002580 // bare star
2581 // TODO see http://www.python.org/dev/peps/pep-3102/
2582 comp->have_bare_star = true;
2583 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002584 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002585 // named star
2586 comp->scope_cur->flags |= SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002587 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2588 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002589 // named star with annotation
2590 comp->scope_cur->flags |= SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002591 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2592 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002593 pn_annotation = pns->nodes[1];
2594 } else {
2595 // shouldn't happen
2596 assert(0);
2597 }
Damiend99b0522013-12-21 18:17:45 +00002598 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2599 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2600 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002601 // this parameter has an annotation
2602 pn_annotation = pns->nodes[1];
2603 }
2604 comp->scope_cur->flags |= SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002605 } else {
Damienb14de212013-10-06 00:28:28 +01002606 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002607 assert(0);
2608 }
Damien429d7192013-10-04 19:53:11 +01002609 }
2610
2611 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002612 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002613 // TODO this parameter has an annotation
2614 }
2615 bool added;
2616 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2617 if (!added) {
2618 printf("SyntaxError?: same name used for parameter; %s\n", qstr_str(param_name));
2619 return;
2620 }
2621 id_info->param = true;
2622 id_info->kind = ID_INFO_KIND_LOCAL;
2623 }
2624}
2625
Damiend99b0522013-12-21 18:17:45 +00002626void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002627 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2628}
2629
Damiend99b0522013-12-21 18:17:45 +00002630void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002631 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2632}
2633
Damiend99b0522013-12-21 18:17:45 +00002634void 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 +01002635 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002636 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002637 // no more nested if/for; compile inner expression
2638 compile_node(comp, pn_inner_expr);
2639 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002640 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002641 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002642 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002643 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002644 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002645 } else {
2646 EMIT(yield_value);
2647 EMIT(pop_top);
2648 }
Damiend99b0522013-12-21 18:17:45 +00002649 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002650 // if condition
Damiend99b0522013-12-21 18:17:45 +00002651 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002652 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2653 pn_iter = pns_comp_if->nodes[1];
2654 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002655 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002656 // for loop
Damiend99b0522013-12-21 18:17:45 +00002657 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002658 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002659 int l_end2 = comp_next_label(comp);
2660 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002661 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002662 EMIT_ARG(label_assign, l_top2);
2663 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002664 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2665 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 +00002666 EMIT_ARG(jump, l_top2);
2667 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002668 EMIT(for_iter_end);
2669 } else {
2670 // shouldn't happen
2671 assert(0);
2672 }
2673}
2674
Damiend99b0522013-12-21 18:17:45 +00002675void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002676 // see http://www.python.org/dev/peps/pep-0257/
2677
2678 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002679 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002680 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002681 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002682 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002683 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2684 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002685 for (int i = 0; i < num_nodes; i++) {
2686 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002687 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 +00002688 // not a newline, so this is the first statement; finish search
2689 break;
2690 }
2691 }
2692 // 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 +00002693 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002694 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002695 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002696 } else {
2697 return;
2698 }
2699
2700 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002701 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2702 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2703 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2704 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2705 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002706 compile_node(comp, pns->nodes[0]); // a doc string
2707 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002708 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002709 }
2710 }
2711 }
2712}
2713
2714void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2715 comp->pass = pass;
2716 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002717 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002718 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002719
2720 if (comp->pass == PASS_1) {
2721 scope->stack_size = 0;
2722 }
2723
Damien5ac1b2e2013-10-18 19:58:12 +01002724#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002725 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002726 scope_print_info(scope);
2727 }
Damien5ac1b2e2013-10-18 19:58:12 +01002728#endif
Damien429d7192013-10-04 19:53:11 +01002729
2730 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002731 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2732 assert(scope->kind == SCOPE_MODULE);
2733 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2734 compile_node(comp, pns->nodes[0]); // compile the expression
2735 EMIT(return_value);
2736 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002737 if (!comp->is_repl) {
2738 check_for_doc_string(comp, scope->pn);
2739 }
Damien429d7192013-10-04 19:53:11 +01002740 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002741 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002742 EMIT(return_value);
2743 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002744 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2745 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2746 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002747
2748 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002749 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002750 if (comp->pass == PASS_1) {
2751 comp->have_bare_star = false;
2752 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2753 }
2754
Damiend99b0522013-12-21 18:17:45 +00002755 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // 2 is something...
Damien429d7192013-10-04 19:53:11 +01002756
2757 compile_node(comp, pns->nodes[3]); // 3 is function body
2758 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002759 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002760 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002761 EMIT(return_value);
2762 }
2763 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002764 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2765 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2766 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002767
2768 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002769 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002770 if (comp->pass == PASS_1) {
2771 comp->have_bare_star = false;
2772 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2773 }
2774
2775 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2776 EMIT(return_value);
2777 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2778 // a bit of a hack at the moment
2779
Damiend99b0522013-12-21 18:17:45 +00002780 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2781 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2782 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2783 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2784 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002785
Damien George55baff42014-01-21 21:40:13 +00002786 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01002787 if (comp->pass == PASS_1) {
2788 bool added;
2789 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2790 assert(added);
2791 id_info->kind = ID_INFO_KIND_LOCAL;
2792 scope->num_params = 1;
2793 }
2794
2795 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002796 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01002797 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002798 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01002799 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002800 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01002801 }
2802
Damienb05d7072013-10-05 13:37:10 +01002803 int l_end = comp_next_label(comp);
2804 int l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002805 EMIT_ARG(load_id, qstr_arg);
2806 EMIT_ARG(label_assign, l_top);
2807 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01002808 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2809 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00002810 EMIT_ARG(jump, l_top);
2811 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002812 EMIT(for_iter_end);
2813
2814 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00002815 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002816 }
2817 EMIT(return_value);
2818 } else {
2819 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002820 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2821 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2822 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01002823
2824 if (comp->pass == PASS_1) {
2825 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002826 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01002827 assert(added);
2828 id_info->kind = ID_INFO_KIND_LOCAL;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002829 id_info = scope_find_or_add_id(scope, MP_QSTR___locals__, &added);
Damien429d7192013-10-04 19:53:11 +01002830 assert(added);
2831 id_info->kind = ID_INFO_KIND_LOCAL;
2832 id_info->param = true;
2833 scope->num_params = 1; // __locals__ is the parameter
2834 }
2835
Damien Georgeb9791222014-01-23 00:34:21 +00002836 EMIT_ARG(load_id, MP_QSTR___locals__);
Damien429d7192013-10-04 19:53:11 +01002837 EMIT(store_locals);
Damien Georgeb9791222014-01-23 00:34:21 +00002838 EMIT_ARG(load_id, MP_QSTR___name__);
2839 EMIT_ARG(store_id, MP_QSTR___module__);
2840 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
2841 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01002842
2843 check_for_doc_string(comp, pns->nodes[2]);
2844 compile_node(comp, pns->nodes[2]); // 2 is class body
2845
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002846 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01002847 assert(id != NULL);
2848 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00002849 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002850 } else {
Damien George6baf76e2013-12-30 22:32:17 +00002851#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00002852 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002853#else
Damien Georgeb9791222014-01-23 00:34:21 +00002854 EMIT_ARG(load_fast, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002855#endif
Damien429d7192013-10-04 19:53:11 +01002856 }
2857 EMIT(return_value);
2858 }
2859
Damien415eb6f2013-10-05 12:19:06 +01002860 EMIT(end_pass);
Damien826005c2013-10-05 23:17:28 +01002861}
2862
2863void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2864 comp->pass = pass;
2865 comp->scope_cur = scope;
2866 comp->next_label = 1;
2867
2868 if (scope->kind != SCOPE_FUNCTION) {
2869 printf("Error: inline assembler must be a function\n");
2870 return;
2871 }
2872
Damiena2f2f7d2013-10-06 00:14:13 +01002873 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00002874 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01002875 }
2876
Damien826005c2013-10-05 23:17:28 +01002877 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00002878 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2879 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2880 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01002881
Damiend99b0522013-12-21 18:17:45 +00002882 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01002883
Damiena2f2f7d2013-10-06 00:14:13 +01002884 // parameters are in pns->nodes[1]
2885 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00002886 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01002887 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00002888 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01002889 }
2890
Damiend99b0522013-12-21 18:17:45 +00002891 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01002892
Damiend99b0522013-12-21 18:17:45 +00002893 mp_parse_node_t pn_body = pns->nodes[3]; // body
2894 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01002895 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
2896
Damien Georgecbd2f742014-01-19 11:48:48 +00002897 /*
Damien826005c2013-10-05 23:17:28 +01002898 if (comp->pass == PASS_3) {
2899 //printf("----\n");
2900 scope_print_info(scope);
2901 }
Damien Georgecbd2f742014-01-19 11:48:48 +00002902 */
Damien826005c2013-10-05 23:17:28 +01002903
2904 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00002905 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
2906 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
2907 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
2908 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
2909 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
2910 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
2911 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
2912 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
2913 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
2914 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
2915 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
2916 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
2917 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01002918 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
2919
2920 // emit instructions
2921 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00002922 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien826005c2013-10-05 23:17:28 +01002923 printf("SyntaxError: inline assembler 'label' requires 1 argument\n");
2924 return;
2925 }
2926 int lab = comp_next_label(comp);
2927 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00002928 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01002929 }
2930 } else {
2931 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00002932 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01002933 }
2934 }
2935 }
2936
2937 if (comp->pass > PASS_1) {
2938 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01002939 }
Damien429d7192013-10-04 19:53:11 +01002940}
2941
2942void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
2943 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00002944 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01002945 scope->num_locals = 0;
2946 for (int i = 0; i < scope->id_info_len; i++) {
2947 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002948 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01002949 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
2950 continue;
2951 }
2952 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
2953 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
2954 }
Damien9ecbcff2013-12-11 00:41:43 +00002955 // note: params always count for 1 local, even if they are a cell
Damien429d7192013-10-04 19:53:11 +01002956 if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
2957 id->local_num = scope->num_locals;
2958 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00002959 }
2960 }
2961
2962 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00002963#if MICROPY_EMIT_CPYTHON
2964 int num_cell = 0;
2965#endif
Damien9ecbcff2013-12-11 00:41:43 +00002966 for (int i = 0; i < scope->id_info_len; i++) {
2967 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00002968#if MICROPY_EMIT_CPYTHON
2969 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00002970 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00002971 id->local_num = num_cell;
2972 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00002973 }
Damien George6baf76e2013-12-30 22:32:17 +00002974#else
2975 // in Micro Python the cells come right after the fast locals
2976 // parameters are not counted here, since they remain at the start
2977 // of the locals, even if they are cell vars
2978 if (!id->param && id->kind == ID_INFO_KIND_CELL) {
2979 id->local_num = scope->num_locals;
2980 scope->num_locals += 1;
2981 }
2982#endif
Damien9ecbcff2013-12-11 00:41:43 +00002983 }
Damien9ecbcff2013-12-11 00:41:43 +00002984
2985 // compute the index of free vars (freevars[idx] in CPython)
2986 // make sure they are in the order of the parent scope
2987 if (scope->parent != NULL) {
2988 int num_free = 0;
2989 for (int i = 0; i < scope->parent->id_info_len; i++) {
2990 id_info_t *id = &scope->parent->id_info[i];
2991 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
2992 for (int j = 0; j < scope->id_info_len; j++) {
2993 id_info_t *id2 = &scope->id_info[j];
2994 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +00002995 assert(!id2->param); // free vars should not be params
2996#if MICROPY_EMIT_CPYTHON
2997 // in CPython the frees are numbered after the cells
2998 id2->local_num = num_cell + num_free;
2999#else
3000 // in Micro Python the frees come first, before the params
3001 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003002#endif
3003 num_free += 1;
3004 }
3005 }
3006 }
Damien429d7192013-10-04 19:53:11 +01003007 }
Damien George6baf76e2013-12-30 22:32:17 +00003008#if !MICROPY_EMIT_CPYTHON
3009 // in Micro Python shift all other locals after the free locals
3010 if (num_free > 0) {
3011 for (int i = 0; i < scope->id_info_len; i++) {
3012 id_info_t *id = &scope->id_info[i];
3013 if (id->param || id->kind != ID_INFO_KIND_FREE) {
3014 id->local_num += num_free;
3015 }
3016 }
3017 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3018 scope->num_locals += num_free;
3019 }
3020#endif
Damien429d7192013-10-04 19:53:11 +01003021 }
3022
3023 // compute flags
3024 //scope->flags = 0; since we set some things in parameters
3025 if (scope->kind != SCOPE_MODULE) {
3026 scope->flags |= SCOPE_FLAG_NEWLOCALS;
3027 }
3028 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) {
3029 assert(scope->parent != NULL);
3030 scope->flags |= SCOPE_FLAG_OPTIMISED;
3031
3032 // TODO possibly other ways it can be nested
Damien George08d07552014-01-29 18:58:52 +00003033 // Note that we don't actually use this information at the moment (for CPython compat only)
3034 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 +01003035 scope->flags |= SCOPE_FLAG_NESTED;
3036 }
3037 }
3038 int num_free = 0;
3039 for (int i = 0; i < scope->id_info_len; i++) {
3040 id_info_t *id = &scope->id_info[i];
3041 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3042 num_free += 1;
3043 }
3044 }
3045 if (num_free == 0) {
3046 scope->flags |= SCOPE_FLAG_NOFREE;
3047 }
3048}
3049
Damien George08335002014-01-18 23:24:36 +00003050mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
Damien429d7192013-10-04 19:53:11 +01003051 compiler_t *comp = m_new(compiler_t, 1);
3052
Damien Georgecbd2f742014-01-19 11:48:48 +00003053 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003054 comp->is_repl = is_repl;
3055 comp->had_error = false;
3056
Damien429d7192013-10-04 19:53:11 +01003057 comp->break_label = 0;
3058 comp->continue_label = 0;
3059 comp->except_nest_level = 0;
3060 comp->scope_head = NULL;
3061 comp->scope_cur = NULL;
3062
Damien826005c2013-10-05 23:17:28 +01003063 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003064 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003065
3066 // set the outer scope
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003067 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE);
Damien429d7192013-10-04 19:53:11 +01003068
Damien826005c2013-10-05 23:17:28 +01003069 // compile pass 1
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003070 comp->emit = emit_pass1_new(MP_QSTR___class__);
Damien826005c2013-10-05 23:17:28 +01003071 comp->emit_method_table = &emit_pass1_method_table;
3072 comp->emit_inline_asm = NULL;
3073 comp->emit_inline_asm_method_table = NULL;
3074 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003075 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003076 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003077#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003078 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003079 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003080#endif
Damien826005c2013-10-05 23:17:28 +01003081 } else {
3082 compile_scope(comp, s, PASS_1);
3083 }
3084
3085 // update maximim number of labels needed
3086 if (comp->next_label > max_num_labels) {
3087 max_num_labels = comp->next_label;
3088 }
Damien429d7192013-10-04 19:53:11 +01003089 }
3090
Damien826005c2013-10-05 23:17:28 +01003091 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003092 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003093 compile_scope_compute_things(comp, s);
3094 }
3095
Damien826005c2013-10-05 23:17:28 +01003096 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003097 emit_pass1_free(comp->emit);
3098
Damien826005c2013-10-05 23:17:28 +01003099 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003100#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003101 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003102#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003103 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003104#endif
Damien3ef4abb2013-10-12 16:53:13 +01003105#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003106 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003107#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003108#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003109 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003110 if (false) {
3111 // dummy
3112
Damien3ef4abb2013-10-12 16:53:13 +01003113#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003114 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
3115 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003116 if (emit_inline_thumb == NULL) {
3117 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3118 }
3119 comp->emit = NULL;
3120 comp->emit_method_table = NULL;
3121 comp->emit_inline_asm = emit_inline_thumb;
3122 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3123 compile_scope_inline_asm(comp, s, PASS_2);
3124 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003125#endif
3126
Damien826005c2013-10-05 23:17:28 +01003127 } else {
Damienc025ebb2013-10-12 14:30:21 +01003128
3129 // choose the emit type
3130
Damien3ef4abb2013-10-12 16:53:13 +01003131#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003132 comp->emit = emit_cpython_new(max_num_labels);
3133 comp->emit_method_table = &emit_cpython_method_table;
3134#else
Damien826005c2013-10-05 23:17:28 +01003135 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003136
3137#if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003138 case EMIT_OPT_NATIVE_PYTHON:
Damien3410be82013-10-07 23:09:10 +01003139 case EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003140#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003141 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003142 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003143 }
Damien13ed3a62013-10-08 09:05:10 +01003144 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003145#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003146 if (emit_native == NULL) {
3147 emit_native = emit_native_thumb_new(max_num_labels);
3148 }
3149 comp->emit_method_table = &emit_native_thumb_method_table;
3150#endif
3151 comp->emit = emit_native;
Damien3410be82013-10-07 23:09:10 +01003152 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003153 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003154#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003155
Damien826005c2013-10-05 23:17:28 +01003156 default:
3157 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003158 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003159 }
3160 comp->emit = emit_bc;
3161 comp->emit_method_table = &emit_bc_method_table;
3162 break;
3163 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003164#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003165
3166 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003167 compile_scope(comp, s, PASS_2);
3168 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003169 }
Damien429d7192013-10-04 19:53:11 +01003170 }
3171
Damien George41d02b62014-01-24 22:42:28 +00003172 // free the emitters
3173#if !MICROPY_EMIT_CPYTHON
3174 if (emit_bc != NULL) {
3175 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003176 }
Damien George41d02b62014-01-24 22:42:28 +00003177#if MICROPY_EMIT_NATIVE
3178 if (emit_native != NULL) {
3179#if MICROPY_EMIT_X64
3180 emit_native_x64_free(emit_native);
3181#elif MICROPY_EMIT_THUMB
3182 emit_native_thumb_free(emit_native);
3183#endif
3184 }
3185#endif
3186#if MICROPY_EMIT_INLINE_THUMB
3187 if (emit_inline_thumb != NULL) {
3188 emit_inline_thumb_free(emit_inline_thumb);
3189 }
3190#endif
3191#endif // !MICROPY_EMIT_CPYTHON
3192
3193 // free the scopes
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003194 uint unique_code_id = module_scope->unique_code_id;
3195 for (scope_t *s = module_scope; s;) {
3196 scope_t *next = s->next;
3197 scope_free(s);
3198 s = next;
3199 }
Damien5ac1b2e2013-10-18 19:58:12 +01003200
Damien George41d02b62014-01-24 22:42:28 +00003201 // free the compiler
3202 bool had_error = comp->had_error;
3203 m_del_obj(compiler_t, comp);
3204
Damien George1fb03172014-01-03 14:22:03 +00003205 if (had_error) {
3206 // TODO return a proper error message
3207 return mp_const_none;
3208 } else {
3209#if MICROPY_EMIT_CPYTHON
3210 // can't create code, so just return true
Damien George41d02b62014-01-24 22:42:28 +00003211 (void)unique_code_id; // to suppress warning that unique_code_id is unused
Damien George1fb03172014-01-03 14:22:03 +00003212 return mp_const_true;
3213#else
3214 // return function that executes the outer module
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003215 return rt_make_function_from_id(unique_code_id);
Damien George1fb03172014-01-03 14:22:03 +00003216#endif
3217 }
Damien429d7192013-10-04 19:53:11 +01003218}