blob: 0704cc457ab63ca769e2a0f700db3fe2e2b598e0 [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 Georgeb9791222014-01-23 00:34:21 +00001915 EMIT_ARG(unary_op, RT_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01001916}
1917
Damiend99b0522013-12-21 18:17:45 +00001918void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001919 int stack_size = EMIT(get_stack_size);
Damiend99b0522013-12-21 18:17:45 +00001920 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001921 compile_node(comp, pns->nodes[0]);
1922 bool multi = (num_nodes > 3);
1923 int l_fail = 0;
1924 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001925 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001926 }
1927 for (int i = 1; i + 1 < num_nodes; i += 2) {
1928 compile_node(comp, pns->nodes[i + 1]);
1929 if (i + 2 < num_nodes) {
1930 EMIT(dup_top);
1931 EMIT(rot_three);
1932 }
Damiend99b0522013-12-21 18:17:45 +00001933 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_LESS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001934 EMIT_ARG(binary_op, RT_COMPARE_OP_LESS);
Damiend99b0522013-12-21 18:17:45 +00001935 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MORE)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001936 EMIT_ARG(binary_op, RT_COMPARE_OP_MORE);
Damiend99b0522013-12-21 18:17:45 +00001937 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_EQUAL)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001938 EMIT_ARG(binary_op, RT_COMPARE_OP_EQUAL);
Damiend99b0522013-12-21 18:17:45 +00001939 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_LESS_EQUAL)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001940 EMIT_ARG(binary_op, RT_COMPARE_OP_LESS_EQUAL);
Damiend99b0522013-12-21 18:17:45 +00001941 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MORE_EQUAL)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001942 EMIT_ARG(binary_op, RT_COMPARE_OP_MORE_EQUAL);
Damiend99b0522013-12-21 18:17:45 +00001943 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_NOT_EQUAL)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001944 EMIT_ARG(binary_op, RT_COMPARE_OP_NOT_EQUAL);
Damiend99b0522013-12-21 18:17:45 +00001945 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_KW_IN)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001946 EMIT_ARG(binary_op, RT_COMPARE_OP_IN);
Damiend99b0522013-12-21 18:17:45 +00001947 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
1948 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
1949 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01001950 if (kind == PN_comp_op_not_in) {
Damien Georgeb9791222014-01-23 00:34:21 +00001951 EMIT_ARG(binary_op, RT_COMPARE_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01001952 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00001953 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001954 EMIT_ARG(binary_op, RT_COMPARE_OP_IS);
Damien429d7192013-10-04 19:53:11 +01001955 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001956 EMIT_ARG(binary_op, RT_COMPARE_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01001957 }
1958 } else {
1959 // shouldn't happen
1960 assert(0);
1961 }
1962 } else {
1963 // shouldn't happen
1964 assert(0);
1965 }
1966 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00001967 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01001968 }
1969 }
1970 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001971 int l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001972 EMIT_ARG(jump, l_end);
1973 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001974 EMIT(rot_two);
1975 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00001976 EMIT_ARG(label_assign, l_end);
1977 EMIT_ARG(set_stack_size, stack_size + 1); // force stack size
Damien429d7192013-10-04 19:53:11 +01001978 }
1979}
1980
Damiend99b0522013-12-21 18:17:45 +00001981void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001982 // TODO
1983 assert(0);
1984 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001985 //EMIT_ARG(unary_op, "UNARY_STAR");
Damien429d7192013-10-04 19:53:11 +01001986}
1987
Damiend99b0522013-12-21 18:17:45 +00001988void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001989 c_binary_op(comp, pns, RT_BINARY_OP_OR);
1990}
1991
Damiend99b0522013-12-21 18:17:45 +00001992void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001993 c_binary_op(comp, pns, RT_BINARY_OP_XOR);
1994}
1995
Damiend99b0522013-12-21 18:17:45 +00001996void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001997 c_binary_op(comp, pns, RT_BINARY_OP_AND);
1998}
1999
Damiend99b0522013-12-21 18:17:45 +00002000void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2001 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002002 compile_node(comp, pns->nodes[0]);
2003 for (int i = 1; i + 1 < num_nodes; i += 2) {
2004 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002005 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002006 EMIT_ARG(binary_op, RT_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002007 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002008 EMIT_ARG(binary_op, RT_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002009 } else {
2010 // shouldn't happen
2011 assert(0);
2012 }
2013 }
2014}
2015
Damiend99b0522013-12-21 18:17:45 +00002016void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2017 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002018 compile_node(comp, pns->nodes[0]);
2019 for (int i = 1; i + 1 < num_nodes; i += 2) {
2020 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002021 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002022 EMIT_ARG(binary_op, RT_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002023 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002024 EMIT_ARG(binary_op, RT_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002025 } else {
2026 // shouldn't happen
2027 assert(0);
2028 }
2029 }
2030}
2031
Damiend99b0522013-12-21 18:17:45 +00002032void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2033 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002034 compile_node(comp, pns->nodes[0]);
2035 for (int i = 1; i + 1 < num_nodes; i += 2) {
2036 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002037 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002038 EMIT_ARG(binary_op, RT_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002039 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002040 EMIT_ARG(binary_op, RT_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002041 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002042 EMIT_ARG(binary_op, RT_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002043 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002044 EMIT_ARG(binary_op, RT_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002045 } else {
2046 // shouldn't happen
2047 assert(0);
2048 }
2049 }
2050}
2051
Damiend99b0522013-12-21 18:17:45 +00002052void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002053 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002054 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002055 EMIT_ARG(unary_op, RT_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002056 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002057 EMIT_ARG(unary_op, RT_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002058 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002059 EMIT_ARG(unary_op, RT_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002060 } else {
2061 // shouldn't happen
2062 assert(0);
2063 }
2064}
2065
Damiend99b0522013-12-21 18:17:45 +00002066void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_struct_t *pns, bool is_method_call) {
Damien429d7192013-10-04 19:53:11 +01002067 // function to call is on top of stack
2068
2069 int old_n_arg_keyword = comp->n_arg_keyword;
2070 bool old_have_star_arg = comp->have_star_arg;
2071 bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
2072 comp->n_arg_keyword = 0;
2073 comp->have_star_arg = false;
2074 comp->have_dbl_star_arg = false;
2075
2076 compile_node(comp, pns->nodes[0]); // arguments to function call; can be null
2077
2078 // compute number of positional arguments
2079 int n_positional = list_len(pns->nodes[0], PN_arglist) - comp->n_arg_keyword;
2080 if (comp->have_star_arg) {
2081 n_positional -= 1;
2082 }
2083 if (comp->have_dbl_star_arg) {
2084 n_positional -= 1;
2085 }
2086
2087 if (is_method_call) {
Damien Georgeb9791222014-01-23 00:34:21 +00002088 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 +01002089 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002090 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 +01002091 }
2092
2093 comp->n_arg_keyword = old_n_arg_keyword;
2094 comp->have_star_arg = old_have_star_arg;
2095 comp->have_dbl_star_arg = old_have_dbl_star_arg;
2096}
2097
Damiend99b0522013-12-21 18:17:45 +00002098void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2099 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002100 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002101 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 +01002102 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002103 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2104 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002105 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien429d7192013-10-04 19:53:11 +01002106 compile_trailer_paren_helper(comp, pns_paren, true);
2107 i += 1;
2108 } else {
2109 compile_node(comp, pns->nodes[i]);
2110 }
2111 }
2112}
2113
Damiend99b0522013-12-21 18:17:45 +00002114void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002115 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002116 EMIT_ARG(binary_op, RT_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002117}
2118
Damiend99b0522013-12-21 18:17:45 +00002119void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002120 // a list of strings
Damien63321742013-12-10 17:41:49 +00002121
2122 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002123 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002124 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002125 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002126 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002127 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2128 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2129 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002130 if (i == 0) {
2131 string_kind = pn_kind;
2132 } else if (pn_kind != string_kind) {
2133 printf("SyntaxError: cannot mix bytes and nonbytes literals\n");
2134 return;
2135 }
Damien George55baff42014-01-21 21:40:13 +00002136 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002137 }
Damien63321742013-12-10 17:41:49 +00002138
Damien63321742013-12-10 17:41:49 +00002139 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002140 byte *q_ptr;
2141 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002142 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002143 uint s_len;
2144 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002145 memcpy(s_dest, s, s_len);
2146 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002147 }
Damien George55baff42014-01-21 21:40:13 +00002148 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002149
Damien Georgeb9791222014-01-23 00:34:21 +00002150 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002151}
2152
2153// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002154void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2155 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2156 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2157 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002158
2159 if (comp->pass == PASS_1) {
2160 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002161 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 +01002162 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002163 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002164 }
2165
2166 // get the scope for this comprehension
2167 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2168
2169 // compile the comprehension
2170 close_over_variables_etc(comp, this_scope, 0, 0);
2171
2172 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2173 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002174 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01002175}
2176
Damiend99b0522013-12-21 18:17:45 +00002177void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2178 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002179 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002180 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2181 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2182 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2183 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2184 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2185 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2186 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002187 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002188 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002189 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002190 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002191 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002192 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002193 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002194 // generator expression
2195 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2196 } else {
2197 // tuple with 2 items
2198 goto tuple_with_2_items;
2199 }
2200 } else {
2201 // tuple with 2 items
2202 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002203 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002204 }
2205 } else {
2206 // parenthesis around a single item, is just that item
2207 compile_node(comp, pns->nodes[0]);
2208 }
2209}
2210
Damiend99b0522013-12-21 18:17:45 +00002211void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2212 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002213 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002214 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002215 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2216 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2217 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2218 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2219 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002220 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002221 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002222 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002223 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002224 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002225 // list of many items
2226 compile_node(comp, pns2->nodes[0]);
2227 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002228 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002229 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002230 // list comprehension
2231 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2232 } else {
2233 // list with 2 items
2234 goto list_with_2_items;
2235 }
2236 } else {
2237 // list with 2 items
2238 list_with_2_items:
2239 compile_node(comp, pns2->nodes[0]);
2240 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002241 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002242 }
2243 } else {
2244 // list with 1 item
2245 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002246 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002247 }
2248}
2249
Damiend99b0522013-12-21 18:17:45 +00002250void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2251 mp_parse_node_t pn = pns->nodes[0];
2252 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002253 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002254 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002255 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2256 pns = (mp_parse_node_struct_t*)pn;
2257 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002258 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002259 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002260 compile_node(comp, pn);
2261 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002262 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2263 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2264 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2265 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002266 // dict/set with multiple elements
2267
2268 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002269 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002270 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2271
2272 // first element sets whether it's a dict or set
2273 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002274 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002275 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002276 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002277 compile_node(comp, pns->nodes[0]);
2278 EMIT(store_map);
2279 is_dict = true;
2280 } else {
2281 // a set
2282 compile_node(comp, pns->nodes[0]); // 1st value of set
2283 is_dict = false;
2284 }
2285
2286 // process rest of elements
2287 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002288 mp_parse_node_t pn = nodes[i];
2289 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002290 compile_node(comp, pn);
2291 if (is_dict) {
2292 if (!is_key_value) {
2293 printf("SyntaxError?: expecting key:value for dictionary");
2294 return;
2295 }
2296 EMIT(store_map);
2297 } else {
2298 if (is_key_value) {
2299 printf("SyntaxError?: expecting just a value for set");
2300 return;
2301 }
2302 }
2303 }
2304
2305 // if it's a set, build it
2306 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002307 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002308 }
Damiend99b0522013-12-21 18:17:45 +00002309 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002310 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002311 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002312 // a dictionary comprehension
2313 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2314 } else {
2315 // a set comprehension
2316 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2317 }
2318 } else {
2319 // shouldn't happen
2320 assert(0);
2321 }
2322 } else {
2323 // set with one element
2324 goto set_with_one_element;
2325 }
2326 } else {
2327 // set with one element
2328 set_with_one_element:
2329 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002330 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002331 }
2332}
2333
Damiend99b0522013-12-21 18:17:45 +00002334void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002335 compile_trailer_paren_helper(comp, pns, false);
2336}
2337
Damiend99b0522013-12-21 18:17:45 +00002338void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002339 // object who's index we want is on top of stack
2340 compile_node(comp, pns->nodes[0]); // the index
Damien Georgeb9791222014-01-23 00:34:21 +00002341 EMIT_ARG(binary_op, RT_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002342}
2343
Damiend99b0522013-12-21 18:17:45 +00002344void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002345 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002346 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002347}
2348
Damiend99b0522013-12-21 18:17:45 +00002349void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2350 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2351 mp_parse_node_t pn = pns->nodes[0];
2352 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002353 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002354 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2355 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002356 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2357 pns = (mp_parse_node_struct_t*)pn;
2358 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002359 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002360 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002361 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002362 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002363 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002364 } else {
2365 // [?::x]
2366 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002367 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002368 }
Damiend99b0522013-12-21 18:17:45 +00002369 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002370 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002371 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2372 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2373 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2374 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002375 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002376 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002377 } else {
2378 // [?:x:x]
2379 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002380 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002381 }
2382 } else {
2383 // [?:x]
2384 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002385 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002386 }
2387 } else {
2388 // [?:x]
2389 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002390 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002391 }
2392}
2393
Damiend99b0522013-12-21 18:17:45 +00002394void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002395 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002396 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2397 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002398}
2399
Damiend99b0522013-12-21 18:17:45 +00002400void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002401 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002402 compile_subscript_3_helper(comp, pns);
2403}
2404
Damiend99b0522013-12-21 18:17:45 +00002405void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002406 // if this is called then we are compiling a dict key:value pair
2407 compile_node(comp, pns->nodes[1]); // value
2408 compile_node(comp, pns->nodes[0]); // key
2409}
2410
Damiend99b0522013-12-21 18:17:45 +00002411void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002412 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002413 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002414 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002415}
2416
Damiend99b0522013-12-21 18:17:45 +00002417void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002418 if (comp->have_star_arg) {
2419 printf("SyntaxError?: can't have multiple *x\n");
2420 return;
2421 }
2422 comp->have_star_arg = true;
2423 compile_node(comp, pns->nodes[0]);
2424}
2425
Damiend99b0522013-12-21 18:17:45 +00002426void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002427 if (comp->have_dbl_star_arg) {
2428 printf("SyntaxError?: can't have multiple **x\n");
2429 return;
2430 }
2431 comp->have_dbl_star_arg = true;
2432 compile_node(comp, pns->nodes[0]);
2433}
2434
Damiend99b0522013-12-21 18:17:45 +00002435void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2436 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2437 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2438 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2439 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002440 printf("SyntaxError?: lhs of keyword argument must be an id\n");
2441 return;
2442 }
Damien Georgeb9791222014-01-23 00:34:21 +00002443 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002444 compile_node(comp, pns2->nodes[0]);
2445 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002446 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002447 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2448 } else {
2449 // shouldn't happen
2450 assert(0);
2451 }
2452}
2453
Damiend99b0522013-12-21 18:17:45 +00002454void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002455 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
2456 printf("SyntaxError: 'yield' outside function\n");
2457 return;
2458 }
Damiend99b0522013-12-21 18:17:45 +00002459 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002460 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002461 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002462 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2463 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002464 compile_node(comp, pns->nodes[0]);
2465 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002466 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002467 EMIT(yield_from);
2468 } else {
2469 compile_node(comp, pns->nodes[0]);
2470 EMIT(yield_value);
2471 }
2472}
2473
Damiend99b0522013-12-21 18:17:45 +00002474typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Damien429d7192013-10-04 19:53:11 +01002475static compile_function_t compile_function[] = {
2476 NULL,
2477#define nc NULL
2478#define c(f) compile_##f
2479#define DEF_RULE(rule, comp, kind, arg...) comp,
2480#include "grammar.h"
2481#undef nc
2482#undef c
2483#undef DEF_RULE
2484};
2485
Damiend99b0522013-12-21 18:17:45 +00002486void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2487 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002488 // pass
Damiend99b0522013-12-21 18:17:45 +00002489 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
2490 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
2491 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002492 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
2493 case MP_PARSE_NODE_SMALL_INT: EMIT_ARG(load_const_small_int, arg); break;
2494 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2495 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2496 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2497 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002498 case MP_PARSE_NODE_TOKEN:
2499 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002500 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002501 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002502 // do nothing
2503 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002504 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002505 }
2506 break;
Damien429d7192013-10-04 19:53:11 +01002507 default: assert(0);
2508 }
2509 } else {
Damiend99b0522013-12-21 18:17:45 +00002510 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002511 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002512 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002513 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002514 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002515#if MICROPY_DEBUG_PRINTERS
2516 mp_parse_node_print(pn, 0);
2517#endif
Damien429d7192013-10-04 19:53:11 +01002518 assert(0);
2519 } else {
2520 f(comp, pns);
2521 }
2522 }
2523}
2524
Damiend99b0522013-12-21 18:17:45 +00002525void 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 +01002526 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002527 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002528 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2529 if (MP_PARSE_NODE_IS_ID(pn)) {
2530 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002531 if (comp->have_bare_star) {
2532 // comes after a bare star, so doesn't count as a parameter
2533 } else {
2534 comp->scope_cur->num_params += 1;
2535 }
Damienb14de212013-10-06 00:28:28 +01002536 } else {
Damiend99b0522013-12-21 18:17:45 +00002537 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2538 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2539 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2540 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002541 //int node_index = 1; unused
2542 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002543 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002544 // this parameter has an annotation
2545 pn_annotation = pns->nodes[1];
2546 }
2547 //node_index = 2; unused
2548 }
2549 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002550 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002551 // this parameter has a default value
2552 if (comp->have_bare_star) {
2553 comp->scope_cur->num_dict_params += 1;
2554 } else {
2555 comp->scope_cur->num_default_params += 1;
2556 }
2557 }
2558 */
2559 if (comp->have_bare_star) {
2560 // comes after a bare star, so doesn't count as a parameter
2561 } else {
2562 comp->scope_cur->num_params += 1;
2563 }
Damiend99b0522013-12-21 18:17:45 +00002564 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2565 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002566 // bare star
2567 // TODO see http://www.python.org/dev/peps/pep-3102/
2568 comp->have_bare_star = true;
2569 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002570 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002571 // named star
2572 comp->scope_cur->flags |= SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002573 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2574 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002575 // named star with annotation
2576 comp->scope_cur->flags |= SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002577 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2578 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002579 pn_annotation = pns->nodes[1];
2580 } else {
2581 // shouldn't happen
2582 assert(0);
2583 }
Damiend99b0522013-12-21 18:17:45 +00002584 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2585 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2586 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002587 // this parameter has an annotation
2588 pn_annotation = pns->nodes[1];
2589 }
2590 comp->scope_cur->flags |= SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002591 } else {
Damienb14de212013-10-06 00:28:28 +01002592 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002593 assert(0);
2594 }
Damien429d7192013-10-04 19:53:11 +01002595 }
2596
2597 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002598 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002599 // TODO this parameter has an annotation
2600 }
2601 bool added;
2602 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2603 if (!added) {
2604 printf("SyntaxError?: same name used for parameter; %s\n", qstr_str(param_name));
2605 return;
2606 }
2607 id_info->param = true;
2608 id_info->kind = ID_INFO_KIND_LOCAL;
2609 }
2610}
2611
Damiend99b0522013-12-21 18:17:45 +00002612void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002613 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2614}
2615
Damiend99b0522013-12-21 18:17:45 +00002616void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002617 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2618}
2619
Damiend99b0522013-12-21 18:17:45 +00002620void 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 +01002621 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002622 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002623 // no more nested if/for; compile inner expression
2624 compile_node(comp, pn_inner_expr);
2625 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002626 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002627 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002628 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002629 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002630 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002631 } else {
2632 EMIT(yield_value);
2633 EMIT(pop_top);
2634 }
Damiend99b0522013-12-21 18:17:45 +00002635 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002636 // if condition
Damiend99b0522013-12-21 18:17:45 +00002637 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002638 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2639 pn_iter = pns_comp_if->nodes[1];
2640 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002641 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002642 // for loop
Damiend99b0522013-12-21 18:17:45 +00002643 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002644 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002645 int l_end2 = comp_next_label(comp);
2646 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002647 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002648 EMIT_ARG(label_assign, l_top2);
2649 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002650 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2651 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 +00002652 EMIT_ARG(jump, l_top2);
2653 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002654 EMIT(for_iter_end);
2655 } else {
2656 // shouldn't happen
2657 assert(0);
2658 }
2659}
2660
Damiend99b0522013-12-21 18:17:45 +00002661void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002662 // see http://www.python.org/dev/peps/pep-0257/
2663
2664 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002665 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002666 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002667 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002668 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002669 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2670 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002671 for (int i = 0; i < num_nodes; i++) {
2672 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002673 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 +00002674 // not a newline, so this is the first statement; finish search
2675 break;
2676 }
2677 }
2678 // 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 +00002679 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002680 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002681 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002682 } else {
2683 return;
2684 }
2685
2686 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002687 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2688 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2689 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2690 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2691 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002692 compile_node(comp, pns->nodes[0]); // a doc string
2693 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002694 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002695 }
2696 }
2697 }
2698}
2699
2700void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2701 comp->pass = pass;
2702 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002703 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002704 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002705
2706 if (comp->pass == PASS_1) {
2707 scope->stack_size = 0;
2708 }
2709
Damien5ac1b2e2013-10-18 19:58:12 +01002710#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002711 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002712 scope_print_info(scope);
2713 }
Damien5ac1b2e2013-10-18 19:58:12 +01002714#endif
Damien429d7192013-10-04 19:53:11 +01002715
2716 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002717 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2718 assert(scope->kind == SCOPE_MODULE);
2719 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2720 compile_node(comp, pns->nodes[0]); // compile the expression
2721 EMIT(return_value);
2722 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002723 if (!comp->is_repl) {
2724 check_for_doc_string(comp, scope->pn);
2725 }
Damien429d7192013-10-04 19:53:11 +01002726 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002727 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002728 EMIT(return_value);
2729 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002730 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2731 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2732 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002733
2734 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002735 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002736 if (comp->pass == PASS_1) {
2737 comp->have_bare_star = false;
2738 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2739 }
2740
Damiend99b0522013-12-21 18:17:45 +00002741 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // 2 is something...
Damien429d7192013-10-04 19:53:11 +01002742
2743 compile_node(comp, pns->nodes[3]); // 3 is function body
2744 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002745 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002746 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002747 EMIT(return_value);
2748 }
2749 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002750 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2751 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2752 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002753
2754 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002755 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002756 if (comp->pass == PASS_1) {
2757 comp->have_bare_star = false;
2758 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2759 }
2760
2761 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2762 EMIT(return_value);
2763 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2764 // a bit of a hack at the moment
2765
Damiend99b0522013-12-21 18:17:45 +00002766 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2767 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2768 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2769 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2770 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002771
Damien George55baff42014-01-21 21:40:13 +00002772 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01002773 if (comp->pass == PASS_1) {
2774 bool added;
2775 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2776 assert(added);
2777 id_info->kind = ID_INFO_KIND_LOCAL;
2778 scope->num_params = 1;
2779 }
2780
2781 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002782 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01002783 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002784 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01002785 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002786 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01002787 }
2788
Damienb05d7072013-10-05 13:37:10 +01002789 int l_end = comp_next_label(comp);
2790 int l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002791 EMIT_ARG(load_id, qstr_arg);
2792 EMIT_ARG(label_assign, l_top);
2793 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01002794 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2795 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00002796 EMIT_ARG(jump, l_top);
2797 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002798 EMIT(for_iter_end);
2799
2800 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00002801 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002802 }
2803 EMIT(return_value);
2804 } else {
2805 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002806 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2807 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2808 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01002809
2810 if (comp->pass == PASS_1) {
2811 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002812 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01002813 assert(added);
2814 id_info->kind = ID_INFO_KIND_LOCAL;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002815 id_info = scope_find_or_add_id(scope, MP_QSTR___locals__, &added);
Damien429d7192013-10-04 19:53:11 +01002816 assert(added);
2817 id_info->kind = ID_INFO_KIND_LOCAL;
2818 id_info->param = true;
2819 scope->num_params = 1; // __locals__ is the parameter
2820 }
2821
Damien Georgeb9791222014-01-23 00:34:21 +00002822 EMIT_ARG(load_id, MP_QSTR___locals__);
Damien429d7192013-10-04 19:53:11 +01002823 EMIT(store_locals);
Damien Georgeb9791222014-01-23 00:34:21 +00002824 EMIT_ARG(load_id, MP_QSTR___name__);
2825 EMIT_ARG(store_id, MP_QSTR___module__);
2826 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
2827 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01002828
2829 check_for_doc_string(comp, pns->nodes[2]);
2830 compile_node(comp, pns->nodes[2]); // 2 is class body
2831
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002832 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01002833 assert(id != NULL);
2834 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00002835 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002836 } else {
Damien George6baf76e2013-12-30 22:32:17 +00002837#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00002838 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002839#else
Damien Georgeb9791222014-01-23 00:34:21 +00002840 EMIT_ARG(load_fast, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002841#endif
Damien429d7192013-10-04 19:53:11 +01002842 }
2843 EMIT(return_value);
2844 }
2845
Damien415eb6f2013-10-05 12:19:06 +01002846 EMIT(end_pass);
Damien826005c2013-10-05 23:17:28 +01002847}
2848
2849void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2850 comp->pass = pass;
2851 comp->scope_cur = scope;
2852 comp->next_label = 1;
2853
2854 if (scope->kind != SCOPE_FUNCTION) {
2855 printf("Error: inline assembler must be a function\n");
2856 return;
2857 }
2858
Damiena2f2f7d2013-10-06 00:14:13 +01002859 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00002860 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01002861 }
2862
Damien826005c2013-10-05 23:17:28 +01002863 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00002864 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2865 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2866 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01002867
Damiend99b0522013-12-21 18:17:45 +00002868 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01002869
Damiena2f2f7d2013-10-06 00:14:13 +01002870 // parameters are in pns->nodes[1]
2871 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00002872 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01002873 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00002874 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01002875 }
2876
Damiend99b0522013-12-21 18:17:45 +00002877 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01002878
Damiend99b0522013-12-21 18:17:45 +00002879 mp_parse_node_t pn_body = pns->nodes[3]; // body
2880 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01002881 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
2882
Damien Georgecbd2f742014-01-19 11:48:48 +00002883 /*
Damien826005c2013-10-05 23:17:28 +01002884 if (comp->pass == PASS_3) {
2885 //printf("----\n");
2886 scope_print_info(scope);
2887 }
Damien Georgecbd2f742014-01-19 11:48:48 +00002888 */
Damien826005c2013-10-05 23:17:28 +01002889
2890 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00002891 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
2892 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
2893 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
2894 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
2895 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
2896 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
2897 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
2898 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
2899 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
2900 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
2901 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
2902 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
2903 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01002904 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
2905
2906 // emit instructions
2907 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00002908 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien826005c2013-10-05 23:17:28 +01002909 printf("SyntaxError: inline assembler 'label' requires 1 argument\n");
2910 return;
2911 }
2912 int lab = comp_next_label(comp);
2913 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00002914 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01002915 }
2916 } else {
2917 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00002918 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01002919 }
2920 }
2921 }
2922
2923 if (comp->pass > PASS_1) {
2924 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01002925 }
Damien429d7192013-10-04 19:53:11 +01002926}
2927
2928void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
2929 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00002930 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01002931 scope->num_locals = 0;
2932 for (int i = 0; i < scope->id_info_len; i++) {
2933 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002934 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01002935 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
2936 continue;
2937 }
2938 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
2939 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
2940 }
Damien9ecbcff2013-12-11 00:41:43 +00002941 // note: params always count for 1 local, even if they are a cell
Damien429d7192013-10-04 19:53:11 +01002942 if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
2943 id->local_num = scope->num_locals;
2944 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00002945 }
2946 }
2947
2948 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00002949#if MICROPY_EMIT_CPYTHON
2950 int num_cell = 0;
2951#endif
Damien9ecbcff2013-12-11 00:41:43 +00002952 for (int i = 0; i < scope->id_info_len; i++) {
2953 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00002954#if MICROPY_EMIT_CPYTHON
2955 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00002956 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00002957 id->local_num = num_cell;
2958 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00002959 }
Damien George6baf76e2013-12-30 22:32:17 +00002960#else
2961 // in Micro Python the cells come right after the fast locals
2962 // parameters are not counted here, since they remain at the start
2963 // of the locals, even if they are cell vars
2964 if (!id->param && id->kind == ID_INFO_KIND_CELL) {
2965 id->local_num = scope->num_locals;
2966 scope->num_locals += 1;
2967 }
2968#endif
Damien9ecbcff2013-12-11 00:41:43 +00002969 }
Damien9ecbcff2013-12-11 00:41:43 +00002970
2971 // compute the index of free vars (freevars[idx] in CPython)
2972 // make sure they are in the order of the parent scope
2973 if (scope->parent != NULL) {
2974 int num_free = 0;
2975 for (int i = 0; i < scope->parent->id_info_len; i++) {
2976 id_info_t *id = &scope->parent->id_info[i];
2977 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
2978 for (int j = 0; j < scope->id_info_len; j++) {
2979 id_info_t *id2 = &scope->id_info[j];
2980 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +00002981 assert(!id2->param); // free vars should not be params
2982#if MICROPY_EMIT_CPYTHON
2983 // in CPython the frees are numbered after the cells
2984 id2->local_num = num_cell + num_free;
2985#else
2986 // in Micro Python the frees come first, before the params
2987 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00002988#endif
2989 num_free += 1;
2990 }
2991 }
2992 }
Damien429d7192013-10-04 19:53:11 +01002993 }
Damien George6baf76e2013-12-30 22:32:17 +00002994#if !MICROPY_EMIT_CPYTHON
2995 // in Micro Python shift all other locals after the free locals
2996 if (num_free > 0) {
2997 for (int i = 0; i < scope->id_info_len; i++) {
2998 id_info_t *id = &scope->id_info[i];
2999 if (id->param || id->kind != ID_INFO_KIND_FREE) {
3000 id->local_num += num_free;
3001 }
3002 }
3003 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3004 scope->num_locals += num_free;
3005 }
3006#endif
Damien429d7192013-10-04 19:53:11 +01003007 }
3008
3009 // compute flags
3010 //scope->flags = 0; since we set some things in parameters
3011 if (scope->kind != SCOPE_MODULE) {
3012 scope->flags |= SCOPE_FLAG_NEWLOCALS;
3013 }
3014 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) {
3015 assert(scope->parent != NULL);
3016 scope->flags |= SCOPE_FLAG_OPTIMISED;
3017
3018 // TODO possibly other ways it can be nested
3019 if (scope->parent->kind == SCOPE_FUNCTION || (scope->parent->kind == SCOPE_CLASS && scope->parent->parent->kind == SCOPE_FUNCTION)) {
3020 scope->flags |= SCOPE_FLAG_NESTED;
3021 }
3022 }
3023 int num_free = 0;
3024 for (int i = 0; i < scope->id_info_len; i++) {
3025 id_info_t *id = &scope->id_info[i];
3026 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3027 num_free += 1;
3028 }
3029 }
3030 if (num_free == 0) {
3031 scope->flags |= SCOPE_FLAG_NOFREE;
3032 }
3033}
3034
Damien George08335002014-01-18 23:24:36 +00003035mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
Damien429d7192013-10-04 19:53:11 +01003036 compiler_t *comp = m_new(compiler_t, 1);
3037
Damien Georgecbd2f742014-01-19 11:48:48 +00003038 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003039 comp->is_repl = is_repl;
3040 comp->had_error = false;
3041
Damien429d7192013-10-04 19:53:11 +01003042 comp->break_label = 0;
3043 comp->continue_label = 0;
3044 comp->except_nest_level = 0;
3045 comp->scope_head = NULL;
3046 comp->scope_cur = NULL;
3047
Damien826005c2013-10-05 23:17:28 +01003048 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003049 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003050
3051 // set the outer scope
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003052 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE);
Damien429d7192013-10-04 19:53:11 +01003053
Damien826005c2013-10-05 23:17:28 +01003054 // compile pass 1
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003055 comp->emit = emit_pass1_new(MP_QSTR___class__);
Damien826005c2013-10-05 23:17:28 +01003056 comp->emit_method_table = &emit_pass1_method_table;
3057 comp->emit_inline_asm = NULL;
3058 comp->emit_inline_asm_method_table = NULL;
3059 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003060 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003061 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003062#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003063 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003064 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003065#endif
Damien826005c2013-10-05 23:17:28 +01003066 } else {
3067 compile_scope(comp, s, PASS_1);
3068 }
3069
3070 // update maximim number of labels needed
3071 if (comp->next_label > max_num_labels) {
3072 max_num_labels = comp->next_label;
3073 }
Damien429d7192013-10-04 19:53:11 +01003074 }
3075
Damien826005c2013-10-05 23:17:28 +01003076 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003077 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003078 compile_scope_compute_things(comp, s);
3079 }
3080
Damien826005c2013-10-05 23:17:28 +01003081 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003082 emit_pass1_free(comp->emit);
3083
Damien826005c2013-10-05 23:17:28 +01003084 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003085#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003086 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003087#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003088 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003089#endif
Damien3ef4abb2013-10-12 16:53:13 +01003090#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003091 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003092#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003093#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003094 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003095 if (false) {
3096 // dummy
3097
Damien3ef4abb2013-10-12 16:53:13 +01003098#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003099 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
3100 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003101 if (emit_inline_thumb == NULL) {
3102 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3103 }
3104 comp->emit = NULL;
3105 comp->emit_method_table = NULL;
3106 comp->emit_inline_asm = emit_inline_thumb;
3107 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3108 compile_scope_inline_asm(comp, s, PASS_2);
3109 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003110#endif
3111
Damien826005c2013-10-05 23:17:28 +01003112 } else {
Damienc025ebb2013-10-12 14:30:21 +01003113
3114 // choose the emit type
3115
Damien3ef4abb2013-10-12 16:53:13 +01003116#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003117 comp->emit = emit_cpython_new(max_num_labels);
3118 comp->emit_method_table = &emit_cpython_method_table;
3119#else
Damien826005c2013-10-05 23:17:28 +01003120 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003121
3122#if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003123 case EMIT_OPT_NATIVE_PYTHON:
Damien3410be82013-10-07 23:09:10 +01003124 case EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003125#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003126 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003127 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003128 }
Damien13ed3a62013-10-08 09:05:10 +01003129 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003130#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003131 if (emit_native == NULL) {
3132 emit_native = emit_native_thumb_new(max_num_labels);
3133 }
3134 comp->emit_method_table = &emit_native_thumb_method_table;
3135#endif
3136 comp->emit = emit_native;
Damien3410be82013-10-07 23:09:10 +01003137 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003138 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003139#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003140
Damien826005c2013-10-05 23:17:28 +01003141 default:
3142 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003143 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003144 }
3145 comp->emit = emit_bc;
3146 comp->emit_method_table = &emit_bc_method_table;
3147 break;
3148 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003149#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003150
3151 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003152 compile_scope(comp, s, PASS_2);
3153 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003154 }
Damien429d7192013-10-04 19:53:11 +01003155 }
3156
Damien George1fb03172014-01-03 14:22:03 +00003157 bool had_error = comp->had_error;
Damien732407f2013-12-29 19:33:23 +00003158 m_del_obj(compiler_t, comp);
Damien5ac1b2e2013-10-18 19:58:12 +01003159
Damien George1fb03172014-01-03 14:22:03 +00003160 if (had_error) {
3161 // TODO return a proper error message
3162 return mp_const_none;
3163 } else {
3164#if MICROPY_EMIT_CPYTHON
3165 // can't create code, so just return true
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003166 (void)module_scope; // to suppress warning that module_scope is unused
Damien George1fb03172014-01-03 14:22:03 +00003167 return mp_const_true;
3168#else
3169 // return function that executes the outer module
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003170 return rt_make_function_from_id(module_scope->unique_code_id);
Damien George1fb03172014-01-03 14:22:03 +00003171#endif
3172 }
Damien429d7192013-10-04 19:53:11 +01003173}