blob: b24e94a8dc4eee9682d01bb9c60300cb9b43e7e2 [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 Georgeeb7bfcb2014-01-04 15:57:35 +000010#include "mpqstr.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,
26#define DEF_RULE(rule, comp, kind, arg...) PN_##rule,
27#include "grammar.h"
28#undef DEF_RULE
29 PN_maximum_number_of,
30} pn_kind_t;
31
Damien415eb6f2013-10-05 12:19:06 +010032#define EMIT(fun, arg...) (comp->emit_method_table->fun(comp->emit, ##arg))
Damien826005c2013-10-05 23:17:28 +010033#define EMIT_INLINE_ASM(fun, arg...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, ##arg))
Damien429d7192013-10-04 19:53:11 +010034
Damien6cdd3af2013-10-05 18:08:26 +010035#define EMIT_OPT_NONE (0)
36#define EMIT_OPT_BYTE_CODE (1)
37#define EMIT_OPT_NATIVE_PYTHON (2)
Damien7af3d192013-10-07 00:02:49 +010038#define EMIT_OPT_VIPER (3)
39#define EMIT_OPT_ASM_THUMB (4)
Damien6cdd3af2013-10-05 18:08:26 +010040
Damien429d7192013-10-04 19:53:11 +010041typedef struct _compiler_t {
Damien5ac1b2e2013-10-18 19:58:12 +010042 bool is_repl;
Damien429d7192013-10-04 19:53:11 +010043 pass_kind_t pass;
Damien5ac1b2e2013-10-18 19:58:12 +010044 bool had_error; // try to keep compiler clean from nlr
Damien429d7192013-10-04 19:53:11 +010045
Damienb05d7072013-10-05 13:37:10 +010046 int next_label;
Damienb05d7072013-10-05 13:37:10 +010047
Damien429d7192013-10-04 19:53:11 +010048 int break_label;
49 int continue_label;
50 int except_nest_level;
51
52 int n_arg_keyword;
53 bool have_star_arg;
54 bool have_dbl_star_arg;
55 bool have_bare_star;
56 int param_pass;
57 int param_pass_num_dict_params;
58 int param_pass_num_default_params;
59
60 scope_t *scope_head;
61 scope_t *scope_cur;
62
Damien6cdd3af2013-10-05 18:08:26 +010063 emit_t *emit; // current emitter
64 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010065
66 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
67 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 +010068} compiler_t;
69
Damiend99b0522013-12-21 18:17:45 +000070mp_parse_node_t fold_constants(mp_parse_node_t pn) {
71 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
72 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
73 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +010074
75 // fold arguments first
76 for (int i = 0; i < n; i++) {
77 pns->nodes[i] = fold_constants(pns->nodes[i]);
78 }
79
Damiend99b0522013-12-21 18:17:45 +000080 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +010081 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +000082 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
83 int arg0 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
84 int arg1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[2]);
85 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien3ef4abb2013-10-12 16:53:13 +010086#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +010087 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +000088 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
Damien0efb3a12013-10-12 16:16:56 +010089#endif
Damiend99b0522013-12-21 18:17:45 +000090 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
91 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +010092 } else {
93 // shouldn't happen
94 assert(0);
95 }
96 }
97 break;
98
99 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100100 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000101 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
102 machine_int_t arg0 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
103 machine_int_t arg1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[2]);
Damien0efb3a12013-10-12 16:16:56 +0100104 machine_int_t res;
Damiend99b0522013-12-21 18:17:45 +0000105 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100106 res = arg0 + arg1;
Damiend99b0522013-12-21 18:17:45 +0000107 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100108 res = arg0 - arg1;
Damien429d7192013-10-04 19:53:11 +0100109 } else {
110 // shouldn't happen
111 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100112 res = 0;
113 }
Damiend99b0522013-12-21 18:17:45 +0000114 if (MP_FIT_SMALL_INT(res)) {
115 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, res);
Damien429d7192013-10-04 19:53:11 +0100116 }
117 }
118 break;
119
120 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000121 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
122 int arg0 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
123 int arg1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[2]);
124 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien3ef4abb2013-10-12 16:53:13 +0100125#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100126 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000127 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 * arg1);
Damien0efb3a12013-10-12 16:16:56 +0100128#endif
Damiend99b0522013-12-21 18:17:45 +0000129 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien429d7192013-10-04 19:53:11 +0100130 ; // pass
Damiend99b0522013-12-21 18:17:45 +0000131 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Damien0efb3a12013-10-12 16:16:56 +0100132 // XXX implement this properly as Python's % operator acts differently to C's
Damiend99b0522013-12-21 18:17:45 +0000133 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 % arg1);
134 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
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);
Damien429d7192013-10-04 19:53:11 +0100137 } else {
138 // shouldn't happen
139 assert(0);
140 }
141 }
142 break;
143
144 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000145 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
146 machine_int_t arg = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
147 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
148 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
149 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
150 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
151 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
152 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100153 } else {
154 // shouldn't happen
155 assert(0);
156 }
157 }
158 break;
159
Damien3ef4abb2013-10-12 16:53:13 +0100160#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +0100161 case PN_power:
Damien0efb3a12013-10-12 16:16:56 +0100162 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000163 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])) {
164 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
165 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
166 int power = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100167 if (power >= 0) {
168 int ans = 1;
Damiend99b0522013-12-21 18:17:45 +0000169 int base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100170 for (; power > 0; power--) {
171 ans *= base;
172 }
Damiend99b0522013-12-21 18:17:45 +0000173 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100174 }
175 }
176 }
177 break;
Damien0efb3a12013-10-12 16:16:56 +0100178#endif
Damien429d7192013-10-04 19:53:11 +0100179 }
180 }
181
182 return pn;
183}
184
Damiend99b0522013-12-21 18:17:45 +0000185void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100186
Damienb05d7072013-10-05 13:37:10 +0100187static int comp_next_label(compiler_t *comp) {
188 return comp->next_label++;
189}
190
Damiend99b0522013-12-21 18:17:45 +0000191static scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000192 scope_t *scope = scope_new(kind, pn, rt_get_unique_code_id(), emit_options);
Damien429d7192013-10-04 19:53:11 +0100193 scope->parent = comp->scope_cur;
194 scope->next = NULL;
195 if (comp->scope_head == NULL) {
196 comp->scope_head = scope;
197 } else {
198 scope_t *s = comp->scope_head;
199 while (s->next != NULL) {
200 s = s->next;
201 }
202 s->next = scope;
203 }
204 return scope;
205}
206
Damiend99b0522013-12-21 18:17:45 +0000207static int list_len(mp_parse_node_t pn, int pn_kind) {
208 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100209 return 0;
Damiend99b0522013-12-21 18:17:45 +0000210 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100211 return 1;
212 } else {
Damiend99b0522013-12-21 18:17:45 +0000213 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
214 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100215 return 1;
216 } else {
Damiend99b0522013-12-21 18:17:45 +0000217 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100218 }
219 }
220}
221
Damiend99b0522013-12-21 18:17:45 +0000222static 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)) {
223 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
224 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
225 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100226 for (int i = 0; i < num_nodes; i++) {
227 f(comp, pns->nodes[i]);
228 }
Damiend99b0522013-12-21 18:17:45 +0000229 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100230 f(comp, pn);
231 }
232}
233
Damiend99b0522013-12-21 18:17:45 +0000234static int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
235 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100236 *nodes = NULL;
237 return 0;
Damiend99b0522013-12-21 18:17:45 +0000238 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100239 *nodes = pn;
240 return 1;
241 } else {
Damiend99b0522013-12-21 18:17:45 +0000242 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
243 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100244 *nodes = pn;
245 return 1;
246 } else {
247 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000248 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100249 }
250 }
251}
252
Damiend99b0522013-12-21 18:17:45 +0000253void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100254}
255
Damiend99b0522013-12-21 18:17:45 +0000256void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
257 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100258 for (int i = 0; i < num_nodes; i++) {
259 compile_node(comp, pns->nodes[i]);
260 }
261}
262
Damien3ef4abb2013-10-12 16:53:13 +0100263#if MICROPY_EMIT_CPYTHON
Damiend99b0522013-12-21 18:17:45 +0000264static bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
265 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100266 return false;
267 }
Damiend99b0522013-12-21 18:17:45 +0000268 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100269 return false;
270 }
271 return true;
272}
273
Damien02f89412013-12-12 15:13:36 +0000274static void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
275 const char *str = qstr_str(qstr);
276 int len = strlen(str);
277 bool has_single_quote = false;
278 bool has_double_quote = false;
279 for (int i = 0; i < len; i++) {
280 if (str[i] == '\'') {
281 has_single_quote = true;
282 } else if (str[i] == '"') {
283 has_double_quote = true;
284 }
285 }
286 if (bytes) {
287 vstr_printf(vstr, "b");
288 }
289 bool quote_single = false;
290 if (has_single_quote && !has_double_quote) {
291 vstr_printf(vstr, "\"");
292 } else {
293 quote_single = true;
294 vstr_printf(vstr, "'");
295 }
296 for (int i = 0; i < len; i++) {
297 if (str[i] == '\n') {
298 vstr_printf(vstr, "\\n");
299 } else if (str[i] == '\\') {
300 vstr_printf(vstr, "\\\\");
301 } else if (str[i] == '\'' && quote_single) {
302 vstr_printf(vstr, "\\'");
303 } else {
304 vstr_printf(vstr, "%c", str[i]);
305 }
306 }
307 if (has_single_quote && !has_double_quote) {
308 vstr_printf(vstr, "\"");
309 } else {
310 vstr_printf(vstr, "'");
311 }
312}
313
Damiend99b0522013-12-21 18:17:45 +0000314static void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
315 assert(MP_PARSE_NODE_IS_LEAF(pn));
316 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
317 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
318 case MP_PARSE_NODE_ID: assert(0);
319 case MP_PARSE_NODE_SMALL_INT: vstr_printf(vstr, "%d", arg); break;
320 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
321 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
322 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
323 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
324 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100325 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000326 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
327 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
328 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien429d7192013-10-04 19:53:11 +0100329 default: assert(0);
330 }
331 break;
332 default: assert(0);
333 }
334}
335
Damiend99b0522013-12-21 18:17:45 +0000336static 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 +0100337 int n = 0;
338 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000339 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100340 }
341 int total = n;
342 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000343 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100344 total += 1;
Damien3a205172013-10-12 15:01:56 +0100345 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100346 is_const = false;
347 }
348 }
349 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100350 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100351 is_const = false;
352 break;
353 }
354 }
355 if (total > 0 && is_const) {
356 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000357 vstr_t *vstr = vstr_new();
358 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000359 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000360 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100361 need_comma = true;
362 }
363 for (int i = 0; i < n; i++) {
364 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000365 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100366 }
Damien02f89412013-12-12 15:13:36 +0000367 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100368 need_comma = true;
369 }
370 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000371 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100372 } else {
Damien02f89412013-12-12 15:13:36 +0000373 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100374 }
Damien02f89412013-12-12 15:13:36 +0000375 EMIT(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000376 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100377 } else {
Damiend99b0522013-12-21 18:17:45 +0000378 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100379 compile_node(comp, pn);
380 }
381 for (int i = 0; i < n; i++) {
382 compile_node(comp, pns_list->nodes[i]);
383 }
384 EMIT(build_tuple, total);
385 }
386}
Damien3a205172013-10-12 15:01:56 +0100387#endif
388
389// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damiend99b0522013-12-21 18:17:45 +0000390void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100391#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100392 cpython_c_tuple(comp, pn, pns_list);
393#else
394 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000395 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100396 compile_node(comp, pn);
397 total += 1;
398 }
399 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000400 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100401 for (int i = 0; i < n; i++) {
402 compile_node(comp, pns_list->nodes[i]);
403 }
404 total += n;
405 }
406 EMIT(build_tuple, total);
407#endif
408}
Damien429d7192013-10-04 19:53:11 +0100409
Damiend99b0522013-12-21 18:17:45 +0000410void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100411 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000412 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100413}
414
Damiend99b0522013-12-21 18:17:45 +0000415static bool node_is_const_false(mp_parse_node_t pn) {
416 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
417 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_ARG(pn) == 1);
Damien429d7192013-10-04 19:53:11 +0100418}
419
Damiend99b0522013-12-21 18:17:45 +0000420static bool node_is_const_true(mp_parse_node_t pn) {
421 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 +0100422}
423
Damien3ef4abb2013-10-12 16:53:13 +0100424#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100425// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Damiend99b0522013-12-21 18:17:45 +0000426static 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 +0100427 if (node_is_const_false(pn)) {
428 if (jump_if == false) {
429 EMIT(jump, label);
430 }
431 return;
432 } else if (node_is_const_true(pn)) {
433 if (jump_if == true) {
434 EMIT(jump, label);
435 }
436 return;
Damiend99b0522013-12-21 18:17:45 +0000437 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
438 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
439 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
440 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100441 if (jump_if == false) {
Damienb05d7072013-10-05 13:37:10 +0100442 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100443 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100444 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100445 }
Damien3a205172013-10-12 15:01:56 +0100446 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100447 EMIT(label_assign, label2);
448 } else {
449 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100450 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100451 }
452 }
453 return;
Damiend99b0522013-12-21 18:17:45 +0000454 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100455 if (jump_if == false) {
456 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100457 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100458 }
459 } else {
Damienb05d7072013-10-05 13:37:10 +0100460 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100461 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100462 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100463 }
Damien3a205172013-10-12 15:01:56 +0100464 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100465 EMIT(label_assign, label2);
466 }
467 return;
Damiend99b0522013-12-21 18:17:45 +0000468 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100469 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100470 return;
471 }
472 }
473
474 // nothing special, fall back to default compiling for node and jump
475 compile_node(comp, pn);
476 if (jump_if == false) {
477 EMIT(pop_jump_if_false, label);
478 } else {
479 EMIT(pop_jump_if_true, label);
480 }
481}
Damien3a205172013-10-12 15:01:56 +0100482#endif
Damien429d7192013-10-04 19:53:11 +0100483
Damiend99b0522013-12-21 18:17:45 +0000484static void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100485#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100486 cpython_c_if_cond(comp, pn, jump_if, label, false);
487#else
488 if (node_is_const_false(pn)) {
489 if (jump_if == false) {
490 EMIT(jump, label);
491 }
492 return;
493 } else if (node_is_const_true(pn)) {
494 if (jump_if == true) {
495 EMIT(jump, label);
496 }
497 return;
Damiend99b0522013-12-21 18:17:45 +0000498 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
499 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
500 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
501 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100502 if (jump_if == false) {
503 int label2 = comp_next_label(comp);
504 for (int i = 0; i < n - 1; i++) {
505 c_if_cond(comp, pns->nodes[i], true, label2);
506 }
507 c_if_cond(comp, pns->nodes[n - 1], false, label);
508 EMIT(label_assign, label2);
509 } else {
510 for (int i = 0; i < n; i++) {
511 c_if_cond(comp, pns->nodes[i], true, label);
512 }
513 }
514 return;
Damiend99b0522013-12-21 18:17:45 +0000515 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100516 if (jump_if == false) {
517 for (int i = 0; i < n; i++) {
518 c_if_cond(comp, pns->nodes[i], false, label);
519 }
520 } else {
521 int label2 = comp_next_label(comp);
522 for (int i = 0; i < n - 1; i++) {
523 c_if_cond(comp, pns->nodes[i], false, label2);
524 }
525 c_if_cond(comp, pns->nodes[n - 1], true, label);
526 EMIT(label_assign, label2);
527 }
528 return;
Damiend99b0522013-12-21 18:17:45 +0000529 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100530 c_if_cond(comp, pns->nodes[0], !jump_if, label);
531 return;
532 }
533 }
534
535 // nothing special, fall back to default compiling for node and jump
536 compile_node(comp, pn);
537 if (jump_if == false) {
538 EMIT(pop_jump_if_false, label);
539 } else {
540 EMIT(pop_jump_if_true, label);
541 }
542#endif
Damien429d7192013-10-04 19:53:11 +0100543}
544
545typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000546void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100547
Damiend99b0522013-12-21 18:17:45 +0000548void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100549 if (assign_kind != ASSIGN_AUG_STORE) {
550 compile_node(comp, pns->nodes[0]);
551 }
552
Damiend99b0522013-12-21 18:17:45 +0000553 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
554 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
555 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
556 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100557 if (assign_kind != ASSIGN_AUG_STORE) {
558 for (int i = 0; i < n - 1; i++) {
559 compile_node(comp, pns1->nodes[i]);
560 }
561 }
Damiend99b0522013-12-21 18:17:45 +0000562 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
563 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100564 }
Damiend99b0522013-12-21 18:17:45 +0000565 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien429d7192013-10-04 19:53:11 +0100566 printf("SyntaxError: can't assign to function call\n");
567 return;
Damiend99b0522013-12-21 18:17:45 +0000568 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100569 if (assign_kind == ASSIGN_AUG_STORE) {
570 EMIT(rot_three);
571 EMIT(store_subscr);
572 } else {
573 compile_node(comp, pns1->nodes[0]);
574 if (assign_kind == ASSIGN_AUG_LOAD) {
575 EMIT(dup_top_two);
576 EMIT(binary_op, RT_BINARY_OP_SUBSCR);
577 } else {
578 EMIT(store_subscr);
579 }
580 }
Damiend99b0522013-12-21 18:17:45 +0000581 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
582 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100583 if (assign_kind == ASSIGN_AUG_LOAD) {
584 EMIT(dup_top);
Damiend99b0522013-12-21 18:17:45 +0000585 EMIT(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100586 } else {
587 if (assign_kind == ASSIGN_AUG_STORE) {
588 EMIT(rot_two);
589 }
Damiend99b0522013-12-21 18:17:45 +0000590 EMIT(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100591 }
592 } else {
593 // shouldn't happen
594 assert(0);
595 }
596 } else {
597 // shouldn't happen
598 assert(0);
599 }
600
Damiend99b0522013-12-21 18:17:45 +0000601 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +0100602 // SyntaxError, cannot assign
603 assert(0);
604 }
605}
606
Damiend99b0522013-12-21 18:17:45 +0000607void c_assign_tuple(compiler_t *comp, int n, mp_parse_node_t *nodes) {
Damien429d7192013-10-04 19:53:11 +0100608 assert(n >= 0);
609 int have_star_index = -1;
610 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000611 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100612 if (have_star_index < 0) {
613 EMIT(unpack_ex, i, n - i - 1);
614 have_star_index = i;
615 } else {
616 printf("SyntaxError: two starred expressions in assignment\n");
617 return;
618 }
619 }
620 }
621 if (have_star_index < 0) {
622 EMIT(unpack_sequence, n);
623 }
624 for (int i = 0; i < n; i++) {
625 if (i == have_star_index) {
Damiend99b0522013-12-21 18:17:45 +0000626 c_assign(comp, ((mp_parse_node_struct_t*)nodes[i])->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100627 } else {
628 c_assign(comp, nodes[i], ASSIGN_STORE);
629 }
630 }
631}
632
633// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000634void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100635 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000636 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100637 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000638 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
639 if (MP_PARSE_NODE_IS_ID(pn)) {
640 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100641 switch (assign_kind) {
642 case ASSIGN_STORE:
643 case ASSIGN_AUG_STORE:
Damien4b03e772013-10-05 14:17:09 +0100644 EMIT(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100645 break;
646 case ASSIGN_AUG_LOAD:
Damien4b03e772013-10-05 14:17:09 +0100647 EMIT(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100648 break;
649 }
650 } else {
651 printf("SyntaxError: can't assign to literal\n");
652 return;
653 }
654 } else {
Damiend99b0522013-12-21 18:17:45 +0000655 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
656 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100657 case PN_power:
658 // lhs is an index or attribute
659 c_assign_power(comp, pns, assign_kind);
660 break;
661
662 case PN_testlist_star_expr:
663 case PN_exprlist:
664 // lhs is a tuple
665 if (assign_kind != ASSIGN_STORE) {
666 goto bad_aug;
667 }
Damiend99b0522013-12-21 18:17:45 +0000668 c_assign_tuple(comp, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100669 break;
670
671 case PN_atom_paren:
672 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000673 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100674 // empty tuple
675 printf("SyntaxError: can't assign to ()\n");
676 return;
Damiend99b0522013-12-21 18:17:45 +0000677 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
678 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100679 goto testlist_comp;
680 } else {
681 // parenthesis around 1 item, is just that item
682 pn = pns->nodes[0];
683 goto tail_recursion;
684 }
685 break;
686
687 case PN_atom_bracket:
688 // lhs is something in brackets
689 if (assign_kind != ASSIGN_STORE) {
690 goto bad_aug;
691 }
Damiend99b0522013-12-21 18:17:45 +0000692 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100693 // empty list, assignment allowed
694 c_assign_tuple(comp, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000695 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
696 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100697 goto testlist_comp;
698 } else {
699 // brackets around 1 item
700 c_assign_tuple(comp, 1, &pns->nodes[0]);
701 }
702 break;
703
704 default:
Damiend99b0522013-12-21 18:17:45 +0000705 printf("unknown assign, %u\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien429d7192013-10-04 19:53:11 +0100706 assert(0);
707 }
708 return;
709
710 testlist_comp:
711 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000712 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
713 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
714 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100715 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000716 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100717 c_assign_tuple(comp, 1, &pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000718 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100719 // sequence of many items
720 // TODO call c_assign_tuple instead
Damiend99b0522013-12-21 18:17:45 +0000721 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
Damien429d7192013-10-04 19:53:11 +0100722 EMIT(unpack_sequence, 1 + n);
723 c_assign(comp, pns->nodes[0], ASSIGN_STORE);
724 for (int i = 0; i < n; i++) {
725 c_assign(comp, pns2->nodes[i], ASSIGN_STORE);
726 }
Damiend99b0522013-12-21 18:17:45 +0000727 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +0100728 // TODO not implemented
729 assert(0);
730 } else {
731 // sequence with 2 items
732 goto sequence_with_2_items;
733 }
734 } else {
735 // sequence with 2 items
736 sequence_with_2_items:
737 c_assign_tuple(comp, 2, pns->nodes);
738 }
739 return;
740 }
741 return;
742
743 bad_aug:
744 printf("SyntaxError: illegal expression for augmented assignment\n");
745}
746
747// stuff for lambda and comprehensions and generators
748void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_dict_params, int n_default_params) {
749 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000750 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100751 int nfree = 0;
752 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000753 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
754 id_info_t *id = &comp->scope_cur->id_info[i];
755 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
756 for (int j = 0; j < this_scope->id_info_len; j++) {
757 id_info_t *id2 = &this_scope->id_info[j];
758 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000759#if MICROPY_EMIT_CPYTHON
Damien318aec62013-12-10 18:28:17 +0000760 EMIT(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000761#else
762 // in Micro Python we load closures using LOAD_FAST
763 EMIT(load_fast, id->qstr, id->local_num);
764#endif
Damien318aec62013-12-10 18:28:17 +0000765 nfree += 1;
766 }
767 }
Damien429d7192013-10-04 19:53:11 +0100768 }
769 }
770 }
771 if (nfree > 0) {
772 EMIT(build_tuple, nfree);
773 }
774
775 // make the function/closure
776 if (nfree == 0) {
777 EMIT(make_function, this_scope, n_dict_params, n_default_params);
778 } else {
779 EMIT(make_closure, this_scope, n_dict_params, n_default_params);
780 }
781}
782
Damiend99b0522013-12-21 18:17:45 +0000783void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
784 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
785 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
786 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +0100787 // this parameter has a default value
788 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
789 if (comp->have_bare_star) {
790 comp->param_pass_num_dict_params += 1;
791 if (comp->param_pass == 1) {
Damiend99b0522013-12-21 18:17:45 +0000792 EMIT(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100793 compile_node(comp, pns->nodes[2]);
794 }
795 } else {
796 comp->param_pass_num_default_params += 1;
797 if (comp->param_pass == 2) {
798 compile_node(comp, pns->nodes[2]);
799 }
800 }
801 }
Damiend99b0522013-12-21 18:17:45 +0000802 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
803 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
804 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100805 // bare star
806 comp->have_bare_star = true;
807 }
808 }
809}
810
811// leaves function object on stack
812// returns function name
Damiend99b0522013-12-21 18:17:45 +0000813qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100814 if (comp->pass == PASS_1) {
815 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000816 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100817 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000818 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100819 }
820
821 // save variables (probably don't need to do this, since we can't have nested definitions..?)
822 bool old_have_bare_star = comp->have_bare_star;
823 int old_param_pass = comp->param_pass;
824 int old_param_pass_num_dict_params = comp->param_pass_num_dict_params;
825 int old_param_pass_num_default_params = comp->param_pass_num_default_params;
826
827 // compile default parameters
828 comp->have_bare_star = false;
829 comp->param_pass = 1; // pass 1 does any default parameters after bare star
830 comp->param_pass_num_dict_params = 0;
831 comp->param_pass_num_default_params = 0;
832 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
833 comp->have_bare_star = false;
834 comp->param_pass = 2; // pass 2 does any default parameters before bare star
835 comp->param_pass_num_dict_params = 0;
836 comp->param_pass_num_default_params = 0;
837 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
838
839 // get the scope for this function
840 scope_t *fscope = (scope_t*)pns->nodes[4];
841
842 // make the function
843 close_over_variables_etc(comp, fscope, comp->param_pass_num_dict_params, comp->param_pass_num_default_params);
844
845 // restore variables
846 comp->have_bare_star = old_have_bare_star;
847 comp->param_pass = old_param_pass;
848 comp->param_pass_num_dict_params = old_param_pass_num_dict_params;
849 comp->param_pass_num_default_params = old_param_pass_num_default_params;
850
851 // return its name (the 'f' in "def f(...):")
852 return fscope->simple_name;
853}
854
855// leaves class object on stack
856// returns class name
Damiend99b0522013-12-21 18:17:45 +0000857qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100858 if (comp->pass == PASS_1) {
859 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000860 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100861 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000862 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100863 }
864
865 EMIT(load_build_class);
866
867 // scope for this class
868 scope_t *cscope = (scope_t*)pns->nodes[3];
869
870 // compile the class
871 close_over_variables_etc(comp, cscope, 0, 0);
872
873 // get its name
874 EMIT(load_const_id, cscope->simple_name);
875
876 // nodes[1] has parent classes, if any
Damiend99b0522013-12-21 18:17:45 +0000877 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +0100878 // no parent classes
879 EMIT(call_function, 2, 0, false, false);
880 } else {
881 // have a parent class or classes
882 // TODO what if we have, eg, *a or **a in the parent list?
883 compile_node(comp, pns->nodes[1]);
884 EMIT(call_function, 2 + list_len(pns->nodes[1], PN_arglist), 0, false, false);
885 }
886
887 // return its name (the 'C' in class C(...):")
888 return cscope->simple_name;
889}
890
Damien6cdd3af2013-10-05 18:08:26 +0100891// returns true if it was a built-in decorator (even if the built-in had an error)
Damiend99b0522013-12-21 18:17:45 +0000892static 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 +0000893 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100894 return false;
895 }
896
897 if (name_len != 2) {
898 printf("SyntaxError: invalid micropython decorator\n");
899 return true;
900 }
901
Damiend99b0522013-12-21 18:17:45 +0000902 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000903 if (attr == MP_QSTR_byte_code) {
Damien5ac1b2e2013-10-18 19:58:12 +0100904 *emit_options = EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +0100905#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000906 } else if (attr == MP_QSTR_native) {
Damien6cdd3af2013-10-05 18:08:26 +0100907 *emit_options = EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000908 } else if (attr == MP_QSTR_viper) {
Damien7af3d192013-10-07 00:02:49 +0100909 *emit_options = EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100910#endif
Damien3ef4abb2013-10-12 16:53:13 +0100911#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000912 } else if (attr == MP_QSTR_asm_thumb) {
Damien5bfb7592013-10-05 18:41:24 +0100913 *emit_options = EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +0100914#endif
Damien6cdd3af2013-10-05 18:08:26 +0100915 } else {
Damience89a212013-10-15 22:25:17 +0100916 printf("SyntaxError: invalid micropython decorator '%s'\n", qstr_str(attr));
Damien6cdd3af2013-10-05 18:08:26 +0100917 }
918
919 return true;
920}
921
Damiend99b0522013-12-21 18:17:45 +0000922void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100923 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +0000924 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +0100925 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
926
Damien6cdd3af2013-10-05 18:08:26 +0100927 // inherit emit options for this function/class definition
928 uint emit_options = comp->scope_cur->emit_options;
929
930 // compile each decorator
931 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +0100932 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000933 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
934 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +0100935
936 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +0000937 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +0100938 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
939
940 // check for built-in decorators
941 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
942 // this was a built-in
943 num_built_in_decorators += 1;
944
945 } else {
946 // not a built-in, compile normally
947
948 // compile the decorator function
949 compile_node(comp, name_nodes[0]);
950 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +0000951 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
952 EMIT(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +0100953 }
954
955 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +0000956 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +0100957 // call the decorator function with the arguments in nodes[1]
958 compile_node(comp, pns_decorator->nodes[1]);
959 }
Damien429d7192013-10-04 19:53:11 +0100960 }
961 }
962
963 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +0000964 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +0100965 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +0000966 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +0100967 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +0000968 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +0100969 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +0100970 } else {
971 // shouldn't happen
972 assert(0);
973 }
974
975 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +0100976 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien429d7192013-10-04 19:53:11 +0100977 EMIT(call_function, 1, 0, false, false);
978 }
979
980 // store func/class object into name
Damien4b03e772013-10-05 14:17:09 +0100981 EMIT(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +0100982}
983
Damiend99b0522013-12-21 18:17:45 +0000984void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +0100985 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +0100986 // store function object into function name
Damien4b03e772013-10-05 14:17:09 +0100987 EMIT(store_id, fname);
Damien429d7192013-10-04 19:53:11 +0100988}
989
Damiend99b0522013-12-21 18:17:45 +0000990void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
991 if (MP_PARSE_NODE_IS_ID(pn)) {
992 EMIT(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
993 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
994 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +0100995
996 compile_node(comp, pns->nodes[0]); // base of the power node
997
Damiend99b0522013-12-21 18:17:45 +0000998 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
999 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1000 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1001 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001002 for (int i = 0; i < n - 1; i++) {
1003 compile_node(comp, pns1->nodes[i]);
1004 }
Damiend99b0522013-12-21 18:17:45 +00001005 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1006 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001007 }
Damiend99b0522013-12-21 18:17:45 +00001008 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien429d7192013-10-04 19:53:11 +01001009 // SyntaxError: can't delete a function call
1010 assert(0);
Damiend99b0522013-12-21 18:17:45 +00001011 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001012 compile_node(comp, pns1->nodes[0]);
1013 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001014 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1015 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
1016 EMIT(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001017 } else {
1018 // shouldn't happen
1019 assert(0);
1020 }
1021 } else {
1022 // shouldn't happen
1023 assert(0);
1024 }
1025
Damiend99b0522013-12-21 18:17:45 +00001026 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001027 // SyntaxError, cannot delete
1028 assert(0);
1029 }
Damiend99b0522013-12-21 18:17:45 +00001030 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1031 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1032 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1033 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001034 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1035
Damiend99b0522013-12-21 18:17:45 +00001036 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1037 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1038 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001039 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001040 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001041 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001042 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001043 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001044 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001045 c_del_stmt(comp, pns->nodes[0]);
1046 for (int i = 0; i < n; i++) {
1047 c_del_stmt(comp, pns1->nodes[i]);
1048 }
Damiend99b0522013-12-21 18:17:45 +00001049 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001050 // TODO not implemented; can't del comprehension?
1051 assert(0);
1052 } else {
1053 // sequence with 2 items
1054 goto sequence_with_2_items;
1055 }
1056 } else {
1057 // sequence with 2 items
1058 sequence_with_2_items:
1059 c_del_stmt(comp, pns->nodes[0]);
1060 c_del_stmt(comp, pns->nodes[1]);
1061 }
1062 } else {
1063 // tuple with 1 element
1064 c_del_stmt(comp, pn);
1065 }
1066 } else {
1067 // not implemented
1068 assert(0);
1069 }
1070}
1071
Damiend99b0522013-12-21 18:17:45 +00001072void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001073 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1074}
1075
Damiend99b0522013-12-21 18:17:45 +00001076void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001077 if (comp->break_label == 0) {
1078 printf("ERROR: cannot break from here\n");
1079 }
1080 EMIT(break_loop, comp->break_label);
1081}
1082
Damiend99b0522013-12-21 18:17:45 +00001083void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001084 if (comp->continue_label == 0) {
1085 printf("ERROR: cannot continue from here\n");
1086 }
1087 if (comp->except_nest_level > 0) {
1088 EMIT(continue_loop, comp->continue_label);
1089 } else {
1090 EMIT(jump, comp->continue_label);
1091 }
1092}
1093
Damiend99b0522013-12-21 18:17:45 +00001094void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001095 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
1096 printf("SyntaxError: 'return' outside function\n");
1097 comp->had_error = true;
1098 return;
1099 }
Damiend99b0522013-12-21 18:17:45 +00001100 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001101 // no argument to 'return', so return None
Damiend99b0522013-12-21 18:17:45 +00001102 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
1103 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001104 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001105 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1106 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 +01001107
Damienb05d7072013-10-05 13:37:10 +01001108 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001109 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1110 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1111 EMIT(return_value);
1112 EMIT(label_assign, l_fail);
1113 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1114 } else {
1115 compile_node(comp, pns->nodes[0]);
1116 }
1117 EMIT(return_value);
1118}
1119
Damiend99b0522013-12-21 18:17:45 +00001120void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001121 compile_node(comp, pns->nodes[0]);
1122 EMIT(pop_top);
1123}
1124
Damiend99b0522013-12-21 18:17:45 +00001125void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1126 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001127 // raise
1128 EMIT(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001129 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001130 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001131 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001132 compile_node(comp, pns->nodes[0]);
1133 compile_node(comp, pns->nodes[1]);
1134 EMIT(raise_varargs, 2);
1135 } else {
1136 // raise x
1137 compile_node(comp, pns->nodes[0]);
1138 EMIT(raise_varargs, 1);
1139 }
1140}
1141
1142// q1 holds the base, q2 the full name
1143// eg a -> q1=q2=a
1144// a.b.c -> q1=a, q2=a.b.c
Damiend99b0522013-12-21 18:17:45 +00001145void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q1, qstr *q2) {
Damien429d7192013-10-04 19:53:11 +01001146 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001147 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1148 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001149 // a name of the form x as y; unwrap it
Damiend99b0522013-12-21 18:17:45 +00001150 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001151 pn = pns->nodes[0];
1152 is_as = true;
1153 }
Damiend99b0522013-12-21 18:17:45 +00001154 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001155 // just a simple name
Damiend99b0522013-12-21 18:17:45 +00001156 *q2 = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001157 if (!is_as) {
1158 *q1 = *q2;
1159 }
1160 EMIT(import_name, *q2);
Damiend99b0522013-12-21 18:17:45 +00001161 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1162 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1163 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001164 // a name of the form a.b.c
1165 if (!is_as) {
Damiend99b0522013-12-21 18:17:45 +00001166 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001167 }
Damiend99b0522013-12-21 18:17:45 +00001168 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001169 int len = n - 1;
1170 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001171 len += strlen(qstr_str(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])));
Damien429d7192013-10-04 19:53:11 +01001172 }
1173 char *str = m_new(char, len + 1);
Damien Georgefe8fb912014-01-02 16:36:09 +00001174 char *str_dest = str;
Damien429d7192013-10-04 19:53:11 +01001175 str[0] = 0;
1176 for (int i = 0; i < n; i++) {
1177 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001178 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001179 }
Damien Georgefe8fb912014-01-02 16:36:09 +00001180 const char *str_src = qstr_str(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
1181 size_t str_src_len = strlen(str_src);
1182 memcpy(str_dest, str_src, str_src_len);
1183 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001184 }
Damien Georgefe8fb912014-01-02 16:36:09 +00001185 *str_dest = '\0';
Damien732407f2013-12-29 19:33:23 +00001186 *q2 = qstr_from_str_take(str, len + 1);
Damien429d7192013-10-04 19:53:11 +01001187 EMIT(import_name, *q2);
1188 if (is_as) {
1189 for (int i = 1; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001190 EMIT(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001191 }
1192 }
1193 } else {
1194 // TODO not implemented
1195 assert(0);
1196 }
1197 } else {
1198 // TODO not implemented
1199 assert(0);
1200 }
1201}
1202
Damiend99b0522013-12-21 18:17:45 +00001203void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01001204 EMIT(load_const_small_int, 0); // ??
Damiend99b0522013-12-21 18:17:45 +00001205 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001206 qstr q1, q2;
1207 do_import_name(comp, pn, &q1, &q2);
Damien4b03e772013-10-05 14:17:09 +01001208 EMIT(store_id, q1);
Damien429d7192013-10-04 19:53:11 +01001209}
1210
Damiend99b0522013-12-21 18:17:45 +00001211void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001212 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1213}
1214
Damiend99b0522013-12-21 18:17:45 +00001215void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
1216 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damiendb4c3612013-12-10 17:27:24 +00001217 EMIT(load_const_small_int, 0); // level 0 for __import__
1218
1219 // build the "fromlist" tuple
1220#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001221 EMIT(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001222#else
1223 EMIT(load_const_str, qstr_from_str_static("*"), false);
1224 EMIT(build_tuple, 1);
1225#endif
1226
1227 // do the import
Damien429d7192013-10-04 19:53:11 +01001228 qstr dummy_q, id1;
1229 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1230 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001231
Damien429d7192013-10-04 19:53:11 +01001232 } else {
Damiendb4c3612013-12-10 17:27:24 +00001233 EMIT(load_const_small_int, 0); // level 0 for __import__
1234
1235 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001236 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001237 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001238#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001239 {
1240 vstr_t *vstr = vstr_new();
1241 vstr_printf(vstr, "(");
1242 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001243 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1244 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1245 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001246 if (i > 0) {
1247 vstr_printf(vstr, ", ");
1248 }
1249 vstr_printf(vstr, "'");
1250 vstr_printf(vstr, qstr_str(id2));
1251 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001252 }
Damien02f89412013-12-12 15:13:36 +00001253 if (n == 1) {
1254 vstr_printf(vstr, ",");
1255 }
1256 vstr_printf(vstr, ")");
Damien02f89412013-12-12 15:13:36 +00001257 EMIT(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001258 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001259 }
Damiendb4c3612013-12-10 17:27:24 +00001260#else
1261 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001262 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1263 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1264 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damiendb4c3612013-12-10 17:27:24 +00001265 EMIT(load_const_str, id2, false);
1266 }
1267 EMIT(build_tuple, n);
1268#endif
1269
1270 // do the import
Damien429d7192013-10-04 19:53:11 +01001271 qstr dummy_q, id1;
1272 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1273 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001274 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1275 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1276 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien429d7192013-10-04 19:53:11 +01001277 EMIT(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001278 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien4b03e772013-10-05 14:17:09 +01001279 EMIT(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001280 } else {
Damiend99b0522013-12-21 18:17:45 +00001281 EMIT(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001282 }
1283 }
1284 EMIT(pop_top);
1285 }
1286}
1287
Damiend99b0522013-12-21 18:17:45 +00001288void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001289 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001290 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1291 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001292 } else {
Damiend99b0522013-12-21 18:17:45 +00001293 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1294 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001295 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001296 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001297 }
Damien429d7192013-10-04 19:53:11 +01001298 }
1299 }
1300}
1301
Damiend99b0522013-12-21 18:17:45 +00001302void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001303 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001304 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1305 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001306 } else {
Damiend99b0522013-12-21 18:17:45 +00001307 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1308 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001309 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001310 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001311 }
Damien429d7192013-10-04 19:53:11 +01001312 }
1313 }
1314}
1315
Damiend99b0522013-12-21 18:17:45 +00001316void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001317 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001318 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgee5863d92014-01-12 12:35:08 +00001319 EMIT(load_global, MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001320 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001321 // assertion message
1322 compile_node(comp, pns->nodes[1]);
1323 EMIT(call_function, 1, 0, false, false);
1324 }
1325 EMIT(raise_varargs, 1);
1326 EMIT(label_assign, l_end);
1327}
1328
Damiend99b0522013-12-21 18:17:45 +00001329void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001330 // TODO proper and/or short circuiting
1331
Damienb05d7072013-10-05 13:37:10 +01001332 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001333
Damienb05d7072013-10-05 13:37:10 +01001334 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001335 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1336
1337 compile_node(comp, pns->nodes[1]); // if block
Damiend99b0522013-12-21 18:17:45 +00001338 //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 +01001339 // jump over elif/else blocks if they exist
Damien415eb6f2013-10-05 12:19:06 +01001340 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien429d7192013-10-04 19:53:11 +01001341 EMIT(jump, l_end);
1342 }
1343 //}
1344 EMIT(label_assign, l_fail);
1345
Damiend99b0522013-12-21 18:17:45 +00001346 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001347 // compile elif blocks
1348
Damiend99b0522013-12-21 18:17:45 +00001349 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001350
Damiend99b0522013-12-21 18:17:45 +00001351 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001352 // multiple elif blocks
1353
Damiend99b0522013-12-21 18:17:45 +00001354 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001355 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001356 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001357 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001358 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1359
1360 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001361 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien429d7192013-10-04 19:53:11 +01001362 EMIT(jump, l_end);
1363 }
1364 EMIT(label_assign, l_fail);
1365 }
1366
1367 } else {
1368 // a single elif block
1369
Damienb05d7072013-10-05 13:37:10 +01001370 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001371 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1372
1373 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001374 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien429d7192013-10-04 19:53:11 +01001375 EMIT(jump, l_end);
1376 }
1377 EMIT(label_assign, l_fail);
1378 }
1379 }
1380
1381 // compile else block
1382 compile_node(comp, pns->nodes[3]); // can be null
1383
1384 EMIT(label_assign, l_end);
1385}
1386
Damiend99b0522013-12-21 18:17:45 +00001387void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001388 int old_break_label = comp->break_label;
1389 int old_continue_label = comp->continue_label;
1390
Damienb05d7072013-10-05 13:37:10 +01001391 int break_label = comp_next_label(comp);
1392 int continue_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001393
1394 comp->break_label = break_label;
1395 comp->continue_label = continue_label;
1396
Damience89a212013-10-15 22:25:17 +01001397 // compared to CPython, we have an optimised version of while loops
1398#if MICROPY_EMIT_CPYTHON
1399 int done_label = comp_next_label(comp);
1400 EMIT(setup_loop, break_label);
Damien429d7192013-10-04 19:53:11 +01001401 EMIT(label_assign, continue_label);
1402 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1403 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001404 if (!EMIT(last_emit_was_return_value)) {
Damien429d7192013-10-04 19:53:11 +01001405 EMIT(jump, continue_label);
1406 }
1407 EMIT(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001408 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1409 // this is a small hack to agree with CPython
1410 if (!node_is_const_true(pns->nodes[0])) {
1411 EMIT(pop_block);
1412 }
Damience89a212013-10-15 22:25:17 +01001413#else
1414 int top_label = comp_next_label(comp);
1415 EMIT(jump, continue_label);
1416 EMIT(label_assign, top_label);
1417 compile_node(comp, pns->nodes[1]); // body
1418 EMIT(label_assign, continue_label);
1419 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1420#endif
1421
1422 // break/continue apply to outer loop (if any) in the else block
1423 comp->break_label = old_break_label;
1424 comp->continue_label = old_continue_label;
Damien429d7192013-10-04 19:53:11 +01001425
1426 compile_node(comp, pns->nodes[2]); // else
1427
1428 EMIT(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001429}
1430
Damienf72fd0e2013-11-06 20:20:49 +00001431// TODO preload end and step onto stack if they are not constants
1432// TODO check if step is negative and do opposite test
Damiend99b0522013-12-21 18:17:45 +00001433void 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 +00001434 int old_break_label = comp->break_label;
1435 int old_continue_label = comp->continue_label;
1436
1437 int break_label = comp_next_label(comp);
1438 int continue_label = comp_next_label(comp);
1439
1440 comp->break_label = break_label;
1441 comp->continue_label = continue_label;
1442
1443 int top_label = comp_next_label(comp);
1444
1445 // compile: var = start
1446 compile_node(comp, pn_start);
1447 c_assign(comp, pn_var, ASSIGN_STORE);
1448
1449 EMIT(jump, continue_label);
1450 EMIT(label_assign, top_label);
1451
Damienf3822fc2013-11-09 20:12:03 +00001452 // compile body
1453 compile_node(comp, pn_body);
1454
Damienf72fd0e2013-11-06 20:20:49 +00001455 // compile: var += step
1456 c_assign(comp, pn_var, ASSIGN_AUG_LOAD);
1457 compile_node(comp, pn_step);
1458 EMIT(binary_op, RT_BINARY_OP_INPLACE_ADD);
1459 c_assign(comp, pn_var, ASSIGN_AUG_STORE);
1460
Damienf72fd0e2013-11-06 20:20:49 +00001461 EMIT(label_assign, continue_label);
1462
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001463 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001464 compile_node(comp, pn_var);
1465 compile_node(comp, pn_end);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001466 if (MP_PARSE_NODE_LEAF_ARG(pn_step) >= 0) {
John R. Lentonb8698fc2014-01-11 00:58:59 +00001467 EMIT(binary_op, RT_COMPARE_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001468 } else {
John R. Lentonb8698fc2014-01-11 00:58:59 +00001469 EMIT(binary_op, RT_COMPARE_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001470 }
Damienf72fd0e2013-11-06 20:20:49 +00001471 EMIT(pop_jump_if_true, top_label);
1472
1473 // break/continue apply to outer loop (if any) in the else block
1474 comp->break_label = old_break_label;
1475 comp->continue_label = old_continue_label;
1476
1477 compile_node(comp, pn_else);
1478
1479 EMIT(label_assign, break_label);
1480}
1481
Damiend99b0522013-12-21 18:17:45 +00001482void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001483#if !MICROPY_EMIT_CPYTHON
1484 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1485 // this is actually slower, but uses no heap memory
1486 // for viper it will be much, much faster
Damiend99b0522013-12-21 18:17:45 +00001487 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)) {
1488 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001489 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1490 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1491 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1492 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001493 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1494 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001495 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001496 mp_parse_node_t pn_range_start;
1497 mp_parse_node_t pn_range_end;
1498 mp_parse_node_t pn_range_step;
1499 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001500 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001501 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001502 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001503 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001504 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001505 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001506 } else if (n_args == 2) {
1507 pn_range_start = args[0];
1508 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001509 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001510 } else {
1511 pn_range_start = args[0];
1512 pn_range_end = args[1];
1513 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001514 // We need to know sign of step. This is possible only if it's constant
1515 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1516 optimize = false;
1517 }
Damienf72fd0e2013-11-06 20:20:49 +00001518 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001519 }
1520 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001521 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1522 return;
1523 }
1524 }
1525 }
1526#endif
1527
Damien429d7192013-10-04 19:53:11 +01001528 int old_break_label = comp->break_label;
1529 int old_continue_label = comp->continue_label;
1530
Damienb05d7072013-10-05 13:37:10 +01001531 int for_label = comp_next_label(comp);
1532 int pop_label = comp_next_label(comp);
1533 int end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001534
Damienb05d7072013-10-05 13:37:10 +01001535 int break_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001536
1537 comp->continue_label = for_label;
1538 comp->break_label = break_label;
1539
Damience89a212013-10-15 22:25:17 +01001540 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1541#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001542 EMIT(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001543#endif
1544
Damien429d7192013-10-04 19:53:11 +01001545 compile_node(comp, pns->nodes[1]); // iterator
1546 EMIT(get_iter);
1547 EMIT(label_assign, for_label);
1548 EMIT(for_iter, pop_label);
1549 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1550 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001551 if (!EMIT(last_emit_was_return_value)) {
Damien429d7192013-10-04 19:53:11 +01001552 EMIT(jump, for_label);
1553 }
1554 EMIT(label_assign, pop_label);
1555 EMIT(for_iter_end);
1556
1557 // break/continue apply to outer loop (if any) in the else block
1558 comp->break_label = old_break_label;
1559 comp->continue_label = old_continue_label;
1560
Damience89a212013-10-15 22:25:17 +01001561#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001562 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001563#endif
Damien429d7192013-10-04 19:53:11 +01001564
1565 compile_node(comp, pns->nodes[3]); // else (not tested)
1566
1567 EMIT(label_assign, break_label);
1568 EMIT(label_assign, end_label);
1569}
1570
Damiend99b0522013-12-21 18:17:45 +00001571void 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 +01001572 // this function is a bit of a hack at the moment
1573 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1574
1575 // setup code
1576 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001577 int l1 = comp_next_label(comp);
1578 int success_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001579 comp->except_nest_level += 1; // for correct handling of continue
1580 EMIT(setup_except, l1);
1581 compile_node(comp, pn_body); // body
1582 EMIT(pop_block);
1583 EMIT(jump, success_label);
1584 EMIT(label_assign, l1);
Damienb05d7072013-10-05 13:37:10 +01001585 int l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001586
1587 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001588 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1589 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001590
1591 qstr qstr_exception_local = 0;
Damienb05d7072013-10-05 13:37:10 +01001592 int end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001593
Damiend99b0522013-12-21 18:17:45 +00001594 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001595 // this is a catch all exception handler
1596 if (i + 1 != n_except) {
1597 printf("SyntaxError: default 'except:' must be last\n");
1598 return;
1599 }
1600 } else {
1601 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001602 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1603 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1604 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1605 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001606 // handler binds the exception to a local
1607 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001608 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001609 }
1610 }
1611 EMIT(dup_top);
1612 compile_node(comp, pns_exception_expr);
John R. Lentonb8698fc2014-01-11 00:58:59 +00001613 EMIT(binary_op, RT_COMPARE_OP_EXCEPTION_MATCH);
Damien429d7192013-10-04 19:53:11 +01001614 EMIT(pop_jump_if_false, end_finally_label);
1615 }
1616
1617 EMIT(pop_top);
1618
1619 if (qstr_exception_local == 0) {
1620 EMIT(pop_top);
1621 } else {
Damien4b03e772013-10-05 14:17:09 +01001622 EMIT(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001623 }
1624
1625 EMIT(pop_top);
1626
Damiene2880aa2013-12-20 14:22:59 +00001627 int l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001628 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001629 l3 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001630 EMIT(setup_finally, l3);
1631 }
1632 compile_node(comp, pns_except->nodes[1]);
1633 if (qstr_exception_local != 0) {
1634 EMIT(pop_block);
1635 }
1636 EMIT(pop_except);
1637 if (qstr_exception_local != 0) {
Damiend99b0522013-12-21 18:17:45 +00001638 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001639 EMIT(label_assign, l3);
Damiend99b0522013-12-21 18:17:45 +00001640 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien4b03e772013-10-05 14:17:09 +01001641 EMIT(store_id, qstr_exception_local);
1642 EMIT(delete_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001643 EMIT(end_finally);
1644 }
1645 EMIT(jump, l2);
1646 EMIT(label_assign, end_finally_label);
1647 }
1648
1649 EMIT(end_finally);
1650 EMIT(label_assign, success_label);
1651 comp->except_nest_level -= 1;
1652 compile_node(comp, pn_else); // else block, can be null
1653 EMIT(label_assign, l2);
1654 EMIT(set_stack_size, stack_size);
1655}
1656
Damiend99b0522013-12-21 18:17:45 +00001657void 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 +01001658 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1659 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001660 int l_finally_block = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001661 EMIT(setup_finally, l_finally_block);
1662 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001663 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien429d7192013-10-04 19:53:11 +01001664 compile_node(comp, pn_body);
1665 } else {
1666 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1667 }
1668 EMIT(pop_block);
Damiend99b0522013-12-21 18:17:45 +00001669 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001670 EMIT(label_assign, l_finally_block);
1671 compile_node(comp, pn_finally);
1672 EMIT(end_finally);
1673 EMIT(set_stack_size, stack_size);
1674}
1675
Damiend99b0522013-12-21 18:17:45 +00001676void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1677 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1678 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1679 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001680 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001681 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1682 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001683 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001684 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001685 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001686 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001687 // no finally
1688 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1689 } else {
1690 // have finally
Damiend99b0522013-12-21 18:17:45 +00001691 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 +01001692 }
1693 } else {
1694 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001695 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001696 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001697 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001698 }
1699 } else {
1700 // shouldn't happen
1701 assert(0);
1702 }
1703}
1704
Damiend99b0522013-12-21 18:17:45 +00001705void 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 +01001706 if (n == 0) {
1707 // no more pre-bits, compile the body of the with
1708 compile_node(comp, body);
1709 } else {
Damienb05d7072013-10-05 13:37:10 +01001710 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001711 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001712 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001713 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001714 compile_node(comp, pns->nodes[0]);
1715 EMIT(setup_with, l_end);
1716 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1717 } else {
1718 // this pre-bit is just an expression
1719 compile_node(comp, nodes[0]);
1720 EMIT(setup_with, l_end);
1721 EMIT(pop_top);
1722 }
1723 // compile additional pre-bits and the body
1724 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1725 // finish this with block
1726 EMIT(pop_block);
Damiend99b0522013-12-21 18:17:45 +00001727 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001728 EMIT(label_assign, l_end);
1729 EMIT(with_cleanup);
1730 EMIT(end_finally);
1731 }
1732}
1733
Damiend99b0522013-12-21 18:17:45 +00001734void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001735 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001736 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001737 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1738 assert(n > 0);
1739
1740 // compile in a nested fashion
1741 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1742}
1743
Damiend99b0522013-12-21 18:17:45 +00001744void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1745 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001746 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1747 // for REPL, evaluate then print the expression
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001748 EMIT(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001749 compile_node(comp, pns->nodes[0]);
1750 EMIT(call_function, 1, 0, false, false);
1751 EMIT(pop_top);
1752
Damien429d7192013-10-04 19:53:11 +01001753 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001754 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001755 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001756 // do nothing with a lonely constant
1757 } else {
1758 compile_node(comp, pns->nodes[0]); // just an expression
1759 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1760 }
Damien429d7192013-10-04 19:53:11 +01001761 }
1762 } else {
Damiend99b0522013-12-21 18:17:45 +00001763 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1764 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001765 if (kind == PN_expr_stmt_augassign) {
1766 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1767 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001768 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001769 // note that we don't really need to implement separate inplace ops, just normal binary ops will suffice
Damiend99b0522013-12-21 18:17:45 +00001770 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
1771 case MP_TOKEN_DEL_PIPE_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_OR); break;
1772 case MP_TOKEN_DEL_CARET_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_XOR); break;
1773 case MP_TOKEN_DEL_AMPERSAND_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_AND); break;
1774 case MP_TOKEN_DEL_DBL_LESS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_LSHIFT); break;
1775 case MP_TOKEN_DEL_DBL_MORE_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_RSHIFT); break;
1776 case MP_TOKEN_DEL_PLUS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_ADD); break;
1777 case MP_TOKEN_DEL_MINUS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_SUBTRACT); break;
1778 case MP_TOKEN_DEL_STAR_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_MULTIPLY); break;
1779 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_FLOOR_DIVIDE); break;
1780 case MP_TOKEN_DEL_SLASH_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_TRUE_DIVIDE); break;
1781 case MP_TOKEN_DEL_PERCENT_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_MODULO); break;
1782 case MP_TOKEN_DEL_DBL_STAR_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_POWER); break;
Damien429d7192013-10-04 19:53:11 +01001783 default: assert(0); // shouldn't happen
1784 }
1785 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1786 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001787 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1788 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001789 // following CPython, we store left-most first
1790 if (rhs > 0) {
1791 EMIT(dup_top);
1792 }
1793 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1794 for (int i = 0; i < rhs; i++) {
1795 if (i + 1 < rhs) {
1796 EMIT(dup_top);
1797 }
Damiend99b0522013-12-21 18:17:45 +00001798 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001799 }
1800 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001801 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1802 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1803 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1804 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01001805 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001806 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1807 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001808 compile_node(comp, pns10->nodes[0]); // rhs
1809 compile_node(comp, pns10->nodes[1]); // rhs
1810 EMIT(rot_two);
1811 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1812 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00001813 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1814 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1815 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1816 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01001817 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001818 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1819 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001820 compile_node(comp, pns10->nodes[0]); // rhs
1821 compile_node(comp, pns10->nodes[1]); // rhs
1822 compile_node(comp, pns10->nodes[2]); // rhs
1823 EMIT(rot_three);
1824 EMIT(rot_two);
1825 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1826 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1827 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1828 } else {
1829 compile_node(comp, pns1->nodes[0]); // rhs
1830 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1831 }
1832 } else {
1833 // shouldn't happen
1834 assert(0);
1835 }
1836 }
1837}
1838
Damiend99b0522013-12-21 18:17:45 +00001839void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, rt_binary_op_t binary_op) {
1840 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001841 compile_node(comp, pns->nodes[0]);
1842 for (int i = 1; i < num_nodes; i += 1) {
1843 compile_node(comp, pns->nodes[i]);
1844 EMIT(binary_op, binary_op);
1845 }
1846}
1847
Damiend99b0522013-12-21 18:17:45 +00001848void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1849 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1850 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001851
1852 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001853 int l_fail = comp_next_label(comp);
1854 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001855 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1856 compile_node(comp, pns->nodes[0]); // success value
1857 EMIT(jump, l_end);
1858 EMIT(label_assign, l_fail);
1859 EMIT(set_stack_size, stack_size); // force stack size reset
1860 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1861 EMIT(label_assign, l_end);
1862}
1863
Damiend99b0522013-12-21 18:17:45 +00001864void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001865 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00001866 //mp_parse_node_t pn_params = pns->nodes[0];
1867 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001868
1869 if (comp->pass == PASS_1) {
1870 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00001871 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 +01001872 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001873 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001874 }
1875
1876 // get the scope for this lambda
1877 scope_t *this_scope = (scope_t*)pns->nodes[2];
1878
1879 // make the lambda
1880 close_over_variables_etc(comp, this_scope, 0, 0);
1881}
1882
Damiend99b0522013-12-21 18:17:45 +00001883void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001884 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001885 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001886 for (int i = 0; i < n; i += 1) {
1887 compile_node(comp, pns->nodes[i]);
1888 if (i + 1 < n) {
1889 EMIT(jump_if_true_or_pop, l_end);
1890 }
1891 }
1892 EMIT(label_assign, l_end);
1893}
1894
Damiend99b0522013-12-21 18:17:45 +00001895void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001896 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001897 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001898 for (int i = 0; i < n; i += 1) {
1899 compile_node(comp, pns->nodes[i]);
1900 if (i + 1 < n) {
1901 EMIT(jump_if_false_or_pop, l_end);
1902 }
1903 }
1904 EMIT(label_assign, l_end);
1905}
1906
Damiend99b0522013-12-21 18:17:45 +00001907void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001908 compile_node(comp, pns->nodes[0]);
1909 EMIT(unary_op, RT_UNARY_OP_NOT);
1910}
1911
Damiend99b0522013-12-21 18:17:45 +00001912void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001913 int stack_size = EMIT(get_stack_size);
Damiend99b0522013-12-21 18:17:45 +00001914 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001915 compile_node(comp, pns->nodes[0]);
1916 bool multi = (num_nodes > 3);
1917 int l_fail = 0;
1918 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001919 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001920 }
1921 for (int i = 1; i + 1 < num_nodes; i += 2) {
1922 compile_node(comp, pns->nodes[i + 1]);
1923 if (i + 2 < num_nodes) {
1924 EMIT(dup_top);
1925 EMIT(rot_three);
1926 }
Damiend99b0522013-12-21 18:17:45 +00001927 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_LESS)) {
John R. Lentonb8698fc2014-01-11 00:58:59 +00001928 EMIT(binary_op, RT_COMPARE_OP_LESS);
Damiend99b0522013-12-21 18:17:45 +00001929 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MORE)) {
John R. Lentonb8698fc2014-01-11 00:58:59 +00001930 EMIT(binary_op, RT_COMPARE_OP_MORE);
Damiend99b0522013-12-21 18:17:45 +00001931 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_EQUAL)) {
John R. Lentonb8698fc2014-01-11 00:58:59 +00001932 EMIT(binary_op, RT_COMPARE_OP_EQUAL);
Damiend99b0522013-12-21 18:17:45 +00001933 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_LESS_EQUAL)) {
John R. Lentonb8698fc2014-01-11 00:58:59 +00001934 EMIT(binary_op, RT_COMPARE_OP_LESS_EQUAL);
Damiend99b0522013-12-21 18:17:45 +00001935 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MORE_EQUAL)) {
John R. Lentonb8698fc2014-01-11 00:58:59 +00001936 EMIT(binary_op, RT_COMPARE_OP_MORE_EQUAL);
Damiend99b0522013-12-21 18:17:45 +00001937 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_NOT_EQUAL)) {
John R. Lentonb8698fc2014-01-11 00:58:59 +00001938 EMIT(binary_op, RT_COMPARE_OP_NOT_EQUAL);
Damiend99b0522013-12-21 18:17:45 +00001939 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_KW_IN)) {
John R. Lentonb8698fc2014-01-11 00:58:59 +00001940 EMIT(binary_op, RT_COMPARE_OP_IN);
Damiend99b0522013-12-21 18:17:45 +00001941 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
1942 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
1943 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01001944 if (kind == PN_comp_op_not_in) {
John R. Lentonb8698fc2014-01-11 00:58:59 +00001945 EMIT(binary_op, RT_COMPARE_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01001946 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00001947 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
John R. Lentonb8698fc2014-01-11 00:58:59 +00001948 EMIT(binary_op, RT_COMPARE_OP_IS);
Damien429d7192013-10-04 19:53:11 +01001949 } else {
John R. Lentonb8698fc2014-01-11 00:58:59 +00001950 EMIT(binary_op, RT_COMPARE_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01001951 }
1952 } else {
1953 // shouldn't happen
1954 assert(0);
1955 }
1956 } else {
1957 // shouldn't happen
1958 assert(0);
1959 }
1960 if (i + 2 < num_nodes) {
1961 EMIT(jump_if_false_or_pop, l_fail);
1962 }
1963 }
1964 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001965 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001966 EMIT(jump, l_end);
1967 EMIT(label_assign, l_fail);
1968 EMIT(rot_two);
1969 EMIT(pop_top);
1970 EMIT(label_assign, l_end);
1971 EMIT(set_stack_size, stack_size + 1); // force stack size
1972 }
1973}
1974
Damiend99b0522013-12-21 18:17:45 +00001975void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001976 // TODO
1977 assert(0);
1978 compile_node(comp, pns->nodes[0]);
1979 //EMIT(unary_op, "UNARY_STAR");
1980}
1981
Damiend99b0522013-12-21 18:17:45 +00001982void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001983 c_binary_op(comp, pns, RT_BINARY_OP_OR);
1984}
1985
Damiend99b0522013-12-21 18:17:45 +00001986void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001987 c_binary_op(comp, pns, RT_BINARY_OP_XOR);
1988}
1989
Damiend99b0522013-12-21 18:17:45 +00001990void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001991 c_binary_op(comp, pns, RT_BINARY_OP_AND);
1992}
1993
Damiend99b0522013-12-21 18:17:45 +00001994void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1995 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001996 compile_node(comp, pns->nodes[0]);
1997 for (int i = 1; i + 1 < num_nodes; i += 2) {
1998 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00001999 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien429d7192013-10-04 19:53:11 +01002000 EMIT(binary_op, RT_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002001 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien429d7192013-10-04 19:53:11 +01002002 EMIT(binary_op, RT_BINARY_OP_RSHIFT);
2003 } else {
2004 // shouldn't happen
2005 assert(0);
2006 }
2007 }
2008}
2009
Damiend99b0522013-12-21 18:17:45 +00002010void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2011 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002012 compile_node(comp, pns->nodes[0]);
2013 for (int i = 1; i + 1 < num_nodes; i += 2) {
2014 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002015 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien429d7192013-10-04 19:53:11 +01002016 EMIT(binary_op, RT_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002017 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien429d7192013-10-04 19:53:11 +01002018 EMIT(binary_op, RT_BINARY_OP_SUBTRACT);
2019 } else {
2020 // shouldn't happen
2021 assert(0);
2022 }
2023 }
2024}
2025
Damiend99b0522013-12-21 18:17:45 +00002026void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2027 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002028 compile_node(comp, pns->nodes[0]);
2029 for (int i = 1; i + 1 < num_nodes; i += 2) {
2030 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002031 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien429d7192013-10-04 19:53:11 +01002032 EMIT(binary_op, RT_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002033 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien429d7192013-10-04 19:53:11 +01002034 EMIT(binary_op, RT_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002035 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien429d7192013-10-04 19:53:11 +01002036 EMIT(binary_op, RT_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002037 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien429d7192013-10-04 19:53:11 +01002038 EMIT(binary_op, RT_BINARY_OP_MODULO);
2039 } else {
2040 // shouldn't happen
2041 assert(0);
2042 }
2043 }
2044}
2045
Damiend99b0522013-12-21 18:17:45 +00002046void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002047 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002048 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien429d7192013-10-04 19:53:11 +01002049 EMIT(unary_op, RT_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002050 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien429d7192013-10-04 19:53:11 +01002051 EMIT(unary_op, RT_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002052 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien429d7192013-10-04 19:53:11 +01002053 EMIT(unary_op, RT_UNARY_OP_INVERT);
2054 } else {
2055 // shouldn't happen
2056 assert(0);
2057 }
2058}
2059
Damiend99b0522013-12-21 18:17:45 +00002060void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_struct_t *pns, bool is_method_call) {
Damien429d7192013-10-04 19:53:11 +01002061 // function to call is on top of stack
2062
2063 int old_n_arg_keyword = comp->n_arg_keyword;
2064 bool old_have_star_arg = comp->have_star_arg;
2065 bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
2066 comp->n_arg_keyword = 0;
2067 comp->have_star_arg = false;
2068 comp->have_dbl_star_arg = false;
2069
2070 compile_node(comp, pns->nodes[0]); // arguments to function call; can be null
2071
2072 // compute number of positional arguments
2073 int n_positional = list_len(pns->nodes[0], PN_arglist) - comp->n_arg_keyword;
2074 if (comp->have_star_arg) {
2075 n_positional -= 1;
2076 }
2077 if (comp->have_dbl_star_arg) {
2078 n_positional -= 1;
2079 }
2080
2081 if (is_method_call) {
2082 EMIT(call_method, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg);
2083 } else {
2084 EMIT(call_function, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg);
2085 }
2086
2087 comp->n_arg_keyword = old_n_arg_keyword;
2088 comp->have_star_arg = old_have_star_arg;
2089 comp->have_dbl_star_arg = old_have_dbl_star_arg;
2090}
2091
Damiend99b0522013-12-21 18:17:45 +00002092void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2093 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002094 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002095 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 +01002096 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002097 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2098 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
2099 EMIT(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien429d7192013-10-04 19:53:11 +01002100 compile_trailer_paren_helper(comp, pns_paren, true);
2101 i += 1;
2102 } else {
2103 compile_node(comp, pns->nodes[i]);
2104 }
2105 }
2106}
2107
Damiend99b0522013-12-21 18:17:45 +00002108void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002109 compile_node(comp, pns->nodes[0]);
2110 EMIT(binary_op, RT_BINARY_OP_POWER);
2111}
2112
Damiend99b0522013-12-21 18:17:45 +00002113void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002114 // a list of strings
Damien63321742013-12-10 17:41:49 +00002115
2116 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002117 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002118 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002119 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002120 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002121 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2122 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2123 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002124 if (i == 0) {
2125 string_kind = pn_kind;
2126 } else if (pn_kind != string_kind) {
2127 printf("SyntaxError: cannot mix bytes and nonbytes literals\n");
2128 return;
2129 }
Damiend99b0522013-12-21 18:17:45 +00002130 const char *str = qstr_str(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien63321742013-12-10 17:41:49 +00002131 n_bytes += strlen(str);
Damien429d7192013-10-04 19:53:11 +01002132 }
Damien63321742013-12-10 17:41:49 +00002133
2134 // allocate memory for concatenated string/bytes
2135 char *cat_str = m_new(char, n_bytes + 1);
Damien63321742013-12-10 17:41:49 +00002136
2137 // concatenate string/bytes
Damien Georgefe8fb912014-01-02 16:36:09 +00002138 char *s_dest = cat_str;
Damien63321742013-12-10 17:41:49 +00002139 for (int i = 0; i < n; i++) {
Damien Georgefe8fb912014-01-02 16:36:09 +00002140 const char *s = qstr_str(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2141 size_t s_len = strlen(s);
2142 memcpy(s_dest, s, s_len);
2143 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002144 }
Damien Georgefe8fb912014-01-02 16:36:09 +00002145 *s_dest = '\0';
Damien63321742013-12-10 17:41:49 +00002146
Damien732407f2013-12-29 19:33:23 +00002147 EMIT(load_const_str, qstr_from_str_take(cat_str, n_bytes + 1), string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002148}
2149
2150// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002151void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2152 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2153 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2154 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002155
2156 if (comp->pass == PASS_1) {
2157 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002158 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 +01002159 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002160 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002161 }
2162
2163 // get the scope for this comprehension
2164 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2165
2166 // compile the comprehension
2167 close_over_variables_etc(comp, this_scope, 0, 0);
2168
2169 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2170 EMIT(get_iter);
2171 EMIT(call_function, 1, 0, false, false);
2172}
2173
Damiend99b0522013-12-21 18:17:45 +00002174void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2175 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002176 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002177 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2178 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2179 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2180 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2181 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2182 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2183 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002184 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002185 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002186 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002187 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002188 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002189 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002190 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002191 // generator expression
2192 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2193 } else {
2194 // tuple with 2 items
2195 goto tuple_with_2_items;
2196 }
2197 } else {
2198 // tuple with 2 items
2199 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002200 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002201 }
2202 } else {
2203 // parenthesis around a single item, is just that item
2204 compile_node(comp, pns->nodes[0]);
2205 }
2206}
2207
Damiend99b0522013-12-21 18:17:45 +00002208void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2209 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002210 // empty list
2211 EMIT(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002212 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2213 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2214 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2215 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2216 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002217 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002218 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002219 compile_node(comp, pns2->nodes[0]);
2220 EMIT(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002221 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002222 // list of many items
2223 compile_node(comp, pns2->nodes[0]);
2224 compile_generic_all_nodes(comp, pns3);
Damiend99b0522013-12-21 18:17:45 +00002225 EMIT(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
2226 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002227 // list comprehension
2228 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2229 } else {
2230 // list with 2 items
2231 goto list_with_2_items;
2232 }
2233 } else {
2234 // list with 2 items
2235 list_with_2_items:
2236 compile_node(comp, pns2->nodes[0]);
2237 compile_node(comp, pns2->nodes[1]);
2238 EMIT(build_list, 2);
2239 }
2240 } else {
2241 // list with 1 item
2242 compile_node(comp, pns->nodes[0]);
2243 EMIT(build_list, 1);
2244 }
2245}
2246
Damiend99b0522013-12-21 18:17:45 +00002247void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2248 mp_parse_node_t pn = pns->nodes[0];
2249 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002250 // empty dict
2251 EMIT(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002252 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2253 pns = (mp_parse_node_struct_t*)pn;
2254 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002255 // dict with one element
2256 EMIT(build_map, 1);
2257 compile_node(comp, pn);
2258 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002259 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2260 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2261 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2262 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002263 // dict/set with multiple elements
2264
2265 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002266 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002267 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2268
2269 // first element sets whether it's a dict or set
2270 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002271 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002272 // a dictionary
2273 EMIT(build_map, 1 + n);
2274 compile_node(comp, pns->nodes[0]);
2275 EMIT(store_map);
2276 is_dict = true;
2277 } else {
2278 // a set
2279 compile_node(comp, pns->nodes[0]); // 1st value of set
2280 is_dict = false;
2281 }
2282
2283 // process rest of elements
2284 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002285 mp_parse_node_t pn = nodes[i];
2286 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002287 compile_node(comp, pn);
2288 if (is_dict) {
2289 if (!is_key_value) {
2290 printf("SyntaxError?: expecting key:value for dictionary");
2291 return;
2292 }
2293 EMIT(store_map);
2294 } else {
2295 if (is_key_value) {
2296 printf("SyntaxError?: expecting just a value for set");
2297 return;
2298 }
2299 }
2300 }
2301
2302 // if it's a set, build it
2303 if (!is_dict) {
2304 EMIT(build_set, 1 + n);
2305 }
Damiend99b0522013-12-21 18:17:45 +00002306 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002307 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002308 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002309 // a dictionary comprehension
2310 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2311 } else {
2312 // a set comprehension
2313 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2314 }
2315 } else {
2316 // shouldn't happen
2317 assert(0);
2318 }
2319 } else {
2320 // set with one element
2321 goto set_with_one_element;
2322 }
2323 } else {
2324 // set with one element
2325 set_with_one_element:
2326 compile_node(comp, pn);
2327 EMIT(build_set, 1);
2328 }
2329}
2330
Damiend99b0522013-12-21 18:17:45 +00002331void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002332 compile_trailer_paren_helper(comp, pns, false);
2333}
2334
Damiend99b0522013-12-21 18:17:45 +00002335void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002336 // object who's index we want is on top of stack
2337 compile_node(comp, pns->nodes[0]); // the index
2338 EMIT(binary_op, RT_BINARY_OP_SUBSCR);
2339}
2340
Damiend99b0522013-12-21 18:17:45 +00002341void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002342 // object who's attribute we want is on top of stack
Damiend99b0522013-12-21 18:17:45 +00002343 EMIT(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002344}
2345
Damiend99b0522013-12-21 18:17:45 +00002346void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2347 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2348 mp_parse_node_t pn = pns->nodes[0];
2349 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002350 // [?:]
Damiend99b0522013-12-21 18:17:45 +00002351 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002352 EMIT(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002353 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2354 pns = (mp_parse_node_struct_t*)pn;
2355 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
2356 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002357 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002358 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002359 // [?::]
2360 EMIT(build_slice, 2);
2361 } else {
2362 // [?::x]
2363 compile_node(comp, pn);
2364 EMIT(build_slice, 3);
2365 }
Damiend99b0522013-12-21 18:17:45 +00002366 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002367 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002368 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2369 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2370 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2371 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002372 // [?:x:]
2373 EMIT(build_slice, 2);
2374 } else {
2375 // [?:x:x]
2376 compile_node(comp, pns->nodes[0]);
2377 EMIT(build_slice, 3);
2378 }
2379 } else {
2380 // [?:x]
2381 compile_node(comp, pn);
2382 EMIT(build_slice, 2);
2383 }
2384 } else {
2385 // [?:x]
2386 compile_node(comp, pn);
2387 EMIT(build_slice, 2);
2388 }
2389}
2390
Damiend99b0522013-12-21 18:17:45 +00002391void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002392 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002393 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2394 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002395}
2396
Damiend99b0522013-12-21 18:17:45 +00002397void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
2398 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002399 compile_subscript_3_helper(comp, pns);
2400}
2401
Damiend99b0522013-12-21 18:17:45 +00002402void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002403 // if this is called then we are compiling a dict key:value pair
2404 compile_node(comp, pns->nodes[1]); // value
2405 compile_node(comp, pns->nodes[0]); // key
2406}
2407
Damiend99b0522013-12-21 18:17:45 +00002408void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002409 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002410 // store class object into class name
Damien4b03e772013-10-05 14:17:09 +01002411 EMIT(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002412}
2413
Damiend99b0522013-12-21 18:17:45 +00002414void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002415 if (comp->have_star_arg) {
2416 printf("SyntaxError?: can't have multiple *x\n");
2417 return;
2418 }
2419 comp->have_star_arg = true;
2420 compile_node(comp, pns->nodes[0]);
2421}
2422
Damiend99b0522013-12-21 18:17:45 +00002423void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002424 if (comp->have_dbl_star_arg) {
2425 printf("SyntaxError?: can't have multiple **x\n");
2426 return;
2427 }
2428 comp->have_dbl_star_arg = true;
2429 compile_node(comp, pns->nodes[0]);
2430}
2431
Damiend99b0522013-12-21 18:17:45 +00002432void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2433 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2434 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2435 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2436 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002437 printf("SyntaxError?: lhs of keyword argument must be an id\n");
2438 return;
2439 }
Damiend99b0522013-12-21 18:17:45 +00002440 EMIT(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002441 compile_node(comp, pns2->nodes[0]);
2442 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002443 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002444 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2445 } else {
2446 // shouldn't happen
2447 assert(0);
2448 }
2449}
2450
Damiend99b0522013-12-21 18:17:45 +00002451void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002452 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
2453 printf("SyntaxError: 'yield' outside function\n");
2454 return;
2455 }
Damiend99b0522013-12-21 18:17:45 +00002456 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
2457 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002458 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002459 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2460 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002461 compile_node(comp, pns->nodes[0]);
2462 EMIT(get_iter);
Damiend99b0522013-12-21 18:17:45 +00002463 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002464 EMIT(yield_from);
2465 } else {
2466 compile_node(comp, pns->nodes[0]);
2467 EMIT(yield_value);
2468 }
2469}
2470
Damiend99b0522013-12-21 18:17:45 +00002471typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Damien429d7192013-10-04 19:53:11 +01002472static compile_function_t compile_function[] = {
2473 NULL,
2474#define nc NULL
2475#define c(f) compile_##f
2476#define DEF_RULE(rule, comp, kind, arg...) comp,
2477#include "grammar.h"
2478#undef nc
2479#undef c
2480#undef DEF_RULE
2481};
2482
Damiend99b0522013-12-21 18:17:45 +00002483void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2484 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002485 // pass
Damiend99b0522013-12-21 18:17:45 +00002486 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
2487 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
2488 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
2489 case MP_PARSE_NODE_ID: EMIT(load_id, arg); break;
2490 case MP_PARSE_NODE_SMALL_INT: EMIT(load_const_small_int, arg); break;
2491 case MP_PARSE_NODE_INTEGER: EMIT(load_const_int, arg); break;
2492 case MP_PARSE_NODE_DECIMAL: EMIT(load_const_dec, arg); break;
2493 case MP_PARSE_NODE_STRING: EMIT(load_const_str, arg, false); break;
2494 case MP_PARSE_NODE_BYTES: EMIT(load_const_str, arg, true); break;
2495 case MP_PARSE_NODE_TOKEN:
2496 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002497 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002498 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002499 // do nothing
2500 } else {
2501 EMIT(load_const_tok, arg);
2502 }
2503 break;
Damien429d7192013-10-04 19:53:11 +01002504 default: assert(0);
2505 }
2506 } else {
Damiend99b0522013-12-21 18:17:45 +00002507 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2508 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002509 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002510 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2511 mp_parse_node_show(pn, 0);
Damien429d7192013-10-04 19:53:11 +01002512 assert(0);
2513 } else {
2514 f(comp, pns);
2515 }
2516 }
2517}
2518
Damiend99b0522013-12-21 18:17:45 +00002519void 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 +01002520 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002521 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002522 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2523 if (MP_PARSE_NODE_IS_ID(pn)) {
2524 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002525 if (comp->have_bare_star) {
2526 // comes after a bare star, so doesn't count as a parameter
2527 } else {
2528 comp->scope_cur->num_params += 1;
2529 }
Damienb14de212013-10-06 00:28:28 +01002530 } else {
Damiend99b0522013-12-21 18:17:45 +00002531 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2532 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2533 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2534 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002535 //int node_index = 1; unused
2536 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002537 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002538 // this parameter has an annotation
2539 pn_annotation = pns->nodes[1];
2540 }
2541 //node_index = 2; unused
2542 }
2543 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002544 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002545 // this parameter has a default value
2546 if (comp->have_bare_star) {
2547 comp->scope_cur->num_dict_params += 1;
2548 } else {
2549 comp->scope_cur->num_default_params += 1;
2550 }
2551 }
2552 */
2553 if (comp->have_bare_star) {
2554 // comes after a bare star, so doesn't count as a parameter
2555 } else {
2556 comp->scope_cur->num_params += 1;
2557 }
Damiend99b0522013-12-21 18:17:45 +00002558 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2559 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002560 // bare star
2561 // TODO see http://www.python.org/dev/peps/pep-3102/
2562 comp->have_bare_star = true;
2563 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002564 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002565 // named star
2566 comp->scope_cur->flags |= SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002567 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2568 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002569 // named star with annotation
2570 comp->scope_cur->flags |= SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002571 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2572 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002573 pn_annotation = pns->nodes[1];
2574 } else {
2575 // shouldn't happen
2576 assert(0);
2577 }
Damiend99b0522013-12-21 18:17:45 +00002578 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2579 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2580 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002581 // this parameter has an annotation
2582 pn_annotation = pns->nodes[1];
2583 }
2584 comp->scope_cur->flags |= SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002585 } else {
Damienb14de212013-10-06 00:28:28 +01002586 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002587 assert(0);
2588 }
Damien429d7192013-10-04 19:53:11 +01002589 }
2590
2591 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002592 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002593 // TODO this parameter has an annotation
2594 }
2595 bool added;
2596 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2597 if (!added) {
2598 printf("SyntaxError?: same name used for parameter; %s\n", qstr_str(param_name));
2599 return;
2600 }
2601 id_info->param = true;
2602 id_info->kind = ID_INFO_KIND_LOCAL;
2603 }
2604}
2605
Damiend99b0522013-12-21 18:17:45 +00002606void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002607 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2608}
2609
Damiend99b0522013-12-21 18:17:45 +00002610void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002611 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2612}
2613
Damiend99b0522013-12-21 18:17:45 +00002614void 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 +01002615 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002616 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002617 // no more nested if/for; compile inner expression
2618 compile_node(comp, pn_inner_expr);
2619 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
2620 EMIT(list_append, for_depth + 2);
2621 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
2622 EMIT(map_add, for_depth + 2);
2623 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
2624 EMIT(set_add, for_depth + 2);
2625 } else {
2626 EMIT(yield_value);
2627 EMIT(pop_top);
2628 }
Damiend99b0522013-12-21 18:17:45 +00002629 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002630 // if condition
Damiend99b0522013-12-21 18:17:45 +00002631 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002632 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2633 pn_iter = pns_comp_if->nodes[1];
2634 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002635 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002636 // for loop
Damiend99b0522013-12-21 18:17:45 +00002637 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002638 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002639 int l_end2 = comp_next_label(comp);
2640 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002641 EMIT(get_iter);
2642 EMIT(label_assign, l_top2);
2643 EMIT(for_iter, l_end2);
2644 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2645 compile_scope_comp_iter(comp, pns_comp_for2->nodes[2], pn_inner_expr, l_top2, for_depth + 1);
2646 EMIT(jump, l_top2);
2647 EMIT(label_assign, l_end2);
2648 EMIT(for_iter_end);
2649 } else {
2650 // shouldn't happen
2651 assert(0);
2652 }
2653}
2654
Damiend99b0522013-12-21 18:17:45 +00002655void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002656 // see http://www.python.org/dev/peps/pep-0257/
2657
2658 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002659 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002660 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002661 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002662 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002663 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2664 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002665 for (int i = 0; i < num_nodes; i++) {
2666 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002667 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 +00002668 // not a newline, so this is the first statement; finish search
2669 break;
2670 }
2671 }
2672 // 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 +00002673 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002674 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002675 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002676 } else {
2677 return;
2678 }
2679
2680 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002681 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2682 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2683 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2684 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2685 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002686 compile_node(comp, pns->nodes[0]); // a doc string
2687 // store doc string
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002688 EMIT(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002689 }
2690 }
2691 }
2692}
2693
2694void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2695 comp->pass = pass;
2696 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002697 comp->next_label = 1;
Damien415eb6f2013-10-05 12:19:06 +01002698 EMIT(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002699
2700 if (comp->pass == PASS_1) {
2701 scope->stack_size = 0;
2702 }
2703
Damien5ac1b2e2013-10-18 19:58:12 +01002704#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002705 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002706 scope_print_info(scope);
2707 }
Damien5ac1b2e2013-10-18 19:58:12 +01002708#endif
Damien429d7192013-10-04 19:53:11 +01002709
2710 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002711 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2712 assert(scope->kind == SCOPE_MODULE);
2713 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2714 compile_node(comp, pns->nodes[0]); // compile the expression
2715 EMIT(return_value);
2716 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002717 if (!comp->is_repl) {
2718 check_for_doc_string(comp, scope->pn);
2719 }
Damien429d7192013-10-04 19:53:11 +01002720 compile_node(comp, scope->pn);
Damiend99b0522013-12-21 18:17:45 +00002721 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002722 EMIT(return_value);
2723 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002724 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2725 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2726 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002727
2728 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002729 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002730 if (comp->pass == PASS_1) {
2731 comp->have_bare_star = false;
2732 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2733 }
2734
Damiend99b0522013-12-21 18:17:45 +00002735 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // 2 is something...
Damien429d7192013-10-04 19:53:11 +01002736
2737 compile_node(comp, pns->nodes[3]); // 3 is function body
2738 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002739 if (!EMIT(last_emit_was_return_value)) {
Damiend99b0522013-12-21 18:17:45 +00002740 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002741 EMIT(return_value);
2742 }
2743 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002744 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2745 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2746 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002747
2748 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002749 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002750 if (comp->pass == PASS_1) {
2751 comp->have_bare_star = false;
2752 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2753 }
2754
2755 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2756 EMIT(return_value);
2757 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2758 // a bit of a hack at the moment
2759
Damiend99b0522013-12-21 18:17:45 +00002760 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2761 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2762 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2763 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2764 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002765
Damien6cdd3af2013-10-05 18:08:26 +01002766 qstr qstr_arg = qstr_from_str_static(".0");
Damien429d7192013-10-04 19:53:11 +01002767 if (comp->pass == PASS_1) {
2768 bool added;
2769 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2770 assert(added);
2771 id_info->kind = ID_INFO_KIND_LOCAL;
2772 scope->num_params = 1;
2773 }
2774
2775 if (scope->kind == SCOPE_LIST_COMP) {
2776 EMIT(build_list, 0);
2777 } else if (scope->kind == SCOPE_DICT_COMP) {
2778 EMIT(build_map, 0);
2779 } else if (scope->kind == SCOPE_SET_COMP) {
2780 EMIT(build_set, 0);
2781 }
2782
Damienb05d7072013-10-05 13:37:10 +01002783 int l_end = comp_next_label(comp);
2784 int l_top = comp_next_label(comp);
Damien4b03e772013-10-05 14:17:09 +01002785 EMIT(load_id, qstr_arg);
Damien429d7192013-10-04 19:53:11 +01002786 EMIT(label_assign, l_top);
2787 EMIT(for_iter, l_end);
2788 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2789 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
2790 EMIT(jump, l_top);
2791 EMIT(label_assign, l_end);
2792 EMIT(for_iter_end);
2793
2794 if (scope->kind == SCOPE_GEN_EXPR) {
Damiend99b0522013-12-21 18:17:45 +00002795 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002796 }
2797 EMIT(return_value);
2798 } else {
2799 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002800 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2801 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2802 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01002803
2804 if (comp->pass == PASS_1) {
2805 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002806 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01002807 assert(added);
2808 id_info->kind = ID_INFO_KIND_LOCAL;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002809 id_info = scope_find_or_add_id(scope, MP_QSTR___locals__, &added);
Damien429d7192013-10-04 19:53:11 +01002810 assert(added);
2811 id_info->kind = ID_INFO_KIND_LOCAL;
2812 id_info->param = true;
2813 scope->num_params = 1; // __locals__ is the parameter
2814 }
2815
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002816 EMIT(load_id, MP_QSTR___locals__);
Damien429d7192013-10-04 19:53:11 +01002817 EMIT(store_locals);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002818 EMIT(load_id, MP_QSTR___name__);
2819 EMIT(store_id, MP_QSTR___module__);
Damiend99b0522013-12-21 18:17:45 +00002820 EMIT(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002821 EMIT(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01002822
2823 check_for_doc_string(comp, pns->nodes[2]);
2824 compile_node(comp, pns->nodes[2]); // 2 is class body
2825
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002826 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01002827 assert(id != NULL);
2828 if (id->kind == ID_INFO_KIND_LOCAL) {
Damiend99b0522013-12-21 18:17:45 +00002829 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002830 } else {
Damien George6baf76e2013-12-30 22:32:17 +00002831#if MICROPY_EMIT_CPYTHON
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002832 EMIT(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002833#else
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002834 EMIT(load_fast, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002835#endif
Damien429d7192013-10-04 19:53:11 +01002836 }
2837 EMIT(return_value);
2838 }
2839
Damien415eb6f2013-10-05 12:19:06 +01002840 EMIT(end_pass);
Damien826005c2013-10-05 23:17:28 +01002841}
2842
2843void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2844 comp->pass = pass;
2845 comp->scope_cur = scope;
2846 comp->next_label = 1;
2847
2848 if (scope->kind != SCOPE_FUNCTION) {
2849 printf("Error: inline assembler must be a function\n");
2850 return;
2851 }
2852
Damiena2f2f7d2013-10-06 00:14:13 +01002853 if (comp->pass > PASS_1) {
2854 EMIT_INLINE_ASM(start_pass, comp->pass, comp->scope_cur);
2855 }
2856
Damien826005c2013-10-05 23:17:28 +01002857 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00002858 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2859 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2860 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01002861
Damiend99b0522013-12-21 18:17:45 +00002862 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01002863
Damiena2f2f7d2013-10-06 00:14:13 +01002864 // parameters are in pns->nodes[1]
2865 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00002866 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01002867 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
2868 scope->num_params = EMIT_INLINE_ASM(count_params, n_params, pn_params);
2869 }
2870
Damiend99b0522013-12-21 18:17:45 +00002871 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01002872
Damiend99b0522013-12-21 18:17:45 +00002873 mp_parse_node_t pn_body = pns->nodes[3]; // body
2874 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01002875 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
2876
Damien826005c2013-10-05 23:17:28 +01002877 if (comp->pass == PASS_3) {
2878 //printf("----\n");
2879 scope_print_info(scope);
2880 }
2881
2882 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00002883 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
2884 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
2885 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
2886 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
2887 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
2888 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
2889 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
2890 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
2891 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
2892 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
2893 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
2894 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
2895 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01002896 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
2897
2898 // emit instructions
2899 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00002900 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien826005c2013-10-05 23:17:28 +01002901 printf("SyntaxError: inline assembler 'label' requires 1 argument\n");
2902 return;
2903 }
2904 int lab = comp_next_label(comp);
2905 if (pass > PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00002906 EMIT_INLINE_ASM(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01002907 }
2908 } else {
2909 if (pass > PASS_1) {
2910 EMIT_INLINE_ASM(op, op, n_args, pn_arg);
2911 }
2912 }
2913 }
2914
2915 if (comp->pass > PASS_1) {
2916 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01002917 }
Damien429d7192013-10-04 19:53:11 +01002918}
2919
2920void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
2921 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00002922 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01002923 scope->num_locals = 0;
2924 for (int i = 0; i < scope->id_info_len; i++) {
2925 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002926 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01002927 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
2928 continue;
2929 }
2930 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
2931 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
2932 }
Damien9ecbcff2013-12-11 00:41:43 +00002933 // note: params always count for 1 local, even if they are a cell
Damien429d7192013-10-04 19:53:11 +01002934 if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
2935 id->local_num = scope->num_locals;
2936 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00002937 }
2938 }
2939
2940 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00002941#if MICROPY_EMIT_CPYTHON
2942 int num_cell = 0;
2943#endif
Damien9ecbcff2013-12-11 00:41:43 +00002944 for (int i = 0; i < scope->id_info_len; i++) {
2945 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00002946#if MICROPY_EMIT_CPYTHON
2947 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00002948 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00002949 id->local_num = num_cell;
2950 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00002951 }
Damien George6baf76e2013-12-30 22:32:17 +00002952#else
2953 // in Micro Python the cells come right after the fast locals
2954 // parameters are not counted here, since they remain at the start
2955 // of the locals, even if they are cell vars
2956 if (!id->param && id->kind == ID_INFO_KIND_CELL) {
2957 id->local_num = scope->num_locals;
2958 scope->num_locals += 1;
2959 }
2960#endif
Damien9ecbcff2013-12-11 00:41:43 +00002961 }
Damien9ecbcff2013-12-11 00:41:43 +00002962
2963 // compute the index of free vars (freevars[idx] in CPython)
2964 // make sure they are in the order of the parent scope
2965 if (scope->parent != NULL) {
2966 int num_free = 0;
2967 for (int i = 0; i < scope->parent->id_info_len; i++) {
2968 id_info_t *id = &scope->parent->id_info[i];
2969 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
2970 for (int j = 0; j < scope->id_info_len; j++) {
2971 id_info_t *id2 = &scope->id_info[j];
2972 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +00002973 assert(!id2->param); // free vars should not be params
2974#if MICROPY_EMIT_CPYTHON
2975 // in CPython the frees are numbered after the cells
2976 id2->local_num = num_cell + num_free;
2977#else
2978 // in Micro Python the frees come first, before the params
2979 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00002980#endif
2981 num_free += 1;
2982 }
2983 }
2984 }
Damien429d7192013-10-04 19:53:11 +01002985 }
Damien George6baf76e2013-12-30 22:32:17 +00002986#if !MICROPY_EMIT_CPYTHON
2987 // in Micro Python shift all other locals after the free locals
2988 if (num_free > 0) {
2989 for (int i = 0; i < scope->id_info_len; i++) {
2990 id_info_t *id = &scope->id_info[i];
2991 if (id->param || id->kind != ID_INFO_KIND_FREE) {
2992 id->local_num += num_free;
2993 }
2994 }
2995 scope->num_params += num_free; // free vars are counted as params for passing them into the function
2996 scope->num_locals += num_free;
2997 }
2998#endif
Damien429d7192013-10-04 19:53:11 +01002999 }
3000
3001 // compute flags
3002 //scope->flags = 0; since we set some things in parameters
3003 if (scope->kind != SCOPE_MODULE) {
3004 scope->flags |= SCOPE_FLAG_NEWLOCALS;
3005 }
3006 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) {
3007 assert(scope->parent != NULL);
3008 scope->flags |= SCOPE_FLAG_OPTIMISED;
3009
3010 // TODO possibly other ways it can be nested
3011 if (scope->parent->kind == SCOPE_FUNCTION || (scope->parent->kind == SCOPE_CLASS && scope->parent->parent->kind == SCOPE_FUNCTION)) {
3012 scope->flags |= SCOPE_FLAG_NESTED;
3013 }
3014 }
3015 int num_free = 0;
3016 for (int i = 0; i < scope->id_info_len; i++) {
3017 id_info_t *id = &scope->id_info[i];
3018 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3019 num_free += 1;
3020 }
3021 }
3022 if (num_free == 0) {
3023 scope->flags |= SCOPE_FLAG_NOFREE;
3024 }
3025}
3026
Damien George1fb03172014-01-03 14:22:03 +00003027mp_obj_t mp_compile(mp_parse_node_t pn, bool is_repl) {
Damien429d7192013-10-04 19:53:11 +01003028 compiler_t *comp = m_new(compiler_t, 1);
3029
Damien5ac1b2e2013-10-18 19:58:12 +01003030 comp->is_repl = is_repl;
3031 comp->had_error = false;
3032
Damien429d7192013-10-04 19:53:11 +01003033 comp->break_label = 0;
3034 comp->continue_label = 0;
3035 comp->except_nest_level = 0;
3036 comp->scope_head = NULL;
3037 comp->scope_cur = NULL;
3038
Damien826005c2013-10-05 23:17:28 +01003039 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003040 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003041
3042 // set the outer scope
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003043 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE);
Damien429d7192013-10-04 19:53:11 +01003044
Damien826005c2013-10-05 23:17:28 +01003045 // compile pass 1
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003046 comp->emit = emit_pass1_new(MP_QSTR___class__);
Damien826005c2013-10-05 23:17:28 +01003047 comp->emit_method_table = &emit_pass1_method_table;
3048 comp->emit_inline_asm = NULL;
3049 comp->emit_inline_asm_method_table = NULL;
3050 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003051 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003052 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003053#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003054 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003055 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003056#endif
Damien826005c2013-10-05 23:17:28 +01003057 } else {
3058 compile_scope(comp, s, PASS_1);
3059 }
3060
3061 // update maximim number of labels needed
3062 if (comp->next_label > max_num_labels) {
3063 max_num_labels = comp->next_label;
3064 }
Damien429d7192013-10-04 19:53:11 +01003065 }
3066
Damien826005c2013-10-05 23:17:28 +01003067 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003068 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003069 compile_scope_compute_things(comp, s);
3070 }
3071
Damien826005c2013-10-05 23:17:28 +01003072 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003073 emit_pass1_free(comp->emit);
3074
Damien826005c2013-10-05 23:17:28 +01003075 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003076#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003077 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003078#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003079 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003080#endif
Damien3ef4abb2013-10-12 16:53:13 +01003081#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003082 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003083#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003084#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003085 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003086 if (false) {
3087 // dummy
3088
Damien3ef4abb2013-10-12 16:53:13 +01003089#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003090 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
3091 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003092 if (emit_inline_thumb == NULL) {
3093 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3094 }
3095 comp->emit = NULL;
3096 comp->emit_method_table = NULL;
3097 comp->emit_inline_asm = emit_inline_thumb;
3098 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3099 compile_scope_inline_asm(comp, s, PASS_2);
3100 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003101#endif
3102
Damien826005c2013-10-05 23:17:28 +01003103 } else {
Damienc025ebb2013-10-12 14:30:21 +01003104
3105 // choose the emit type
3106
Damien3ef4abb2013-10-12 16:53:13 +01003107#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003108 comp->emit = emit_cpython_new(max_num_labels);
3109 comp->emit_method_table = &emit_cpython_method_table;
3110#else
Damien826005c2013-10-05 23:17:28 +01003111 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003112
3113#if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003114 case EMIT_OPT_NATIVE_PYTHON:
Damien3410be82013-10-07 23:09:10 +01003115 case EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003116#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003117 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003118 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003119 }
Damien13ed3a62013-10-08 09:05:10 +01003120 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003121#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003122 if (emit_native == NULL) {
3123 emit_native = emit_native_thumb_new(max_num_labels);
3124 }
3125 comp->emit_method_table = &emit_native_thumb_method_table;
3126#endif
3127 comp->emit = emit_native;
Damien3410be82013-10-07 23:09:10 +01003128 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003129 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003130#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003131
Damien826005c2013-10-05 23:17:28 +01003132 default:
3133 if (emit_bc == NULL) {
3134 emit_bc = emit_bc_new(max_num_labels);
3135 }
3136 comp->emit = emit_bc;
3137 comp->emit_method_table = &emit_bc_method_table;
3138 break;
3139 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003140#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003141
3142 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003143 compile_scope(comp, s, PASS_2);
3144 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003145 }
Damien429d7192013-10-04 19:53:11 +01003146 }
3147
Damien George1fb03172014-01-03 14:22:03 +00003148 bool had_error = comp->had_error;
Damien732407f2013-12-29 19:33:23 +00003149 m_del_obj(compiler_t, comp);
Damien5ac1b2e2013-10-18 19:58:12 +01003150
Damien George1fb03172014-01-03 14:22:03 +00003151 if (had_error) {
3152 // TODO return a proper error message
3153 return mp_const_none;
3154 } else {
3155#if MICROPY_EMIT_CPYTHON
3156 // can't create code, so just return true
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003157 (void)module_scope; // to suppress warning that module_scope is unused
Damien George1fb03172014-01-03 14:22:03 +00003158 return mp_const_true;
3159#else
3160 // return function that executes the outer module
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003161 return rt_make_function_from_id(module_scope->unique_code_id);
Damien George1fb03172014-01-03 14:22:03 +00003162#endif
3163 }
Damien429d7192013-10-04 19:53:11 +01003164}