blob: f8fa2cb2c2ad12e3807494af66b74647e4f954ee [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 Georgeeb7bfcb2014-01-04 15:57:35 +00001319 EMIT(load_id, MP_QSTR_assertion_error);
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
1463 // compile: if var < end: goto top
1464 compile_node(comp, pn_var);
1465 compile_node(comp, pn_end);
1466 EMIT(compare_op, RT_COMPARE_OP_LESS);
1467 EMIT(pop_jump_if_true, top_label);
1468
1469 // break/continue apply to outer loop (if any) in the else block
1470 comp->break_label = old_break_label;
1471 comp->continue_label = old_continue_label;
1472
1473 compile_node(comp, pn_else);
1474
1475 EMIT(label_assign, break_label);
1476}
1477
Damiend99b0522013-12-21 18:17:45 +00001478void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001479#if !MICROPY_EMIT_CPYTHON
1480 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1481 // this is actually slower, but uses no heap memory
1482 // for viper it will be much, much faster
Damiend99b0522013-12-21 18:17:45 +00001483 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)) {
1484 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001485 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren) && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001486 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1487 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001488 int n_args = list_get(&pn_range_args, PN_arglist, &args);
1489 if (1 <= n_args && n_args <= 3) {
Damiend99b0522013-12-21 18:17:45 +00001490 mp_parse_node_t pn_range_start;
1491 mp_parse_node_t pn_range_end;
1492 mp_parse_node_t pn_range_step;
Damienf72fd0e2013-11-06 20:20:49 +00001493 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001494 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001495 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001496 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001497 } else if (n_args == 2) {
1498 pn_range_start = args[0];
1499 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001500 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001501 } else {
1502 pn_range_start = args[0];
1503 pn_range_end = args[1];
1504 pn_range_step = args[2];
1505 }
1506 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1507 return;
1508 }
1509 }
1510 }
1511#endif
1512
Damien429d7192013-10-04 19:53:11 +01001513 int old_break_label = comp->break_label;
1514 int old_continue_label = comp->continue_label;
1515
Damienb05d7072013-10-05 13:37:10 +01001516 int for_label = comp_next_label(comp);
1517 int pop_label = comp_next_label(comp);
1518 int end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001519
Damienb05d7072013-10-05 13:37:10 +01001520 int break_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001521
1522 comp->continue_label = for_label;
1523 comp->break_label = break_label;
1524
Damience89a212013-10-15 22:25:17 +01001525 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1526#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001527 EMIT(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001528#endif
1529
Damien429d7192013-10-04 19:53:11 +01001530 compile_node(comp, pns->nodes[1]); // iterator
1531 EMIT(get_iter);
1532 EMIT(label_assign, for_label);
1533 EMIT(for_iter, pop_label);
1534 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1535 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001536 if (!EMIT(last_emit_was_return_value)) {
Damien429d7192013-10-04 19:53:11 +01001537 EMIT(jump, for_label);
1538 }
1539 EMIT(label_assign, pop_label);
1540 EMIT(for_iter_end);
1541
1542 // break/continue apply to outer loop (if any) in the else block
1543 comp->break_label = old_break_label;
1544 comp->continue_label = old_continue_label;
1545
Damience89a212013-10-15 22:25:17 +01001546#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001547 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001548#endif
Damien429d7192013-10-04 19:53:11 +01001549
1550 compile_node(comp, pns->nodes[3]); // else (not tested)
1551
1552 EMIT(label_assign, break_label);
1553 EMIT(label_assign, end_label);
1554}
1555
Damiend99b0522013-12-21 18:17:45 +00001556void 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 +01001557 // this function is a bit of a hack at the moment
1558 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1559
1560 // setup code
1561 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001562 int l1 = comp_next_label(comp);
1563 int success_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001564 comp->except_nest_level += 1; // for correct handling of continue
1565 EMIT(setup_except, l1);
1566 compile_node(comp, pn_body); // body
1567 EMIT(pop_block);
1568 EMIT(jump, success_label);
1569 EMIT(label_assign, l1);
Damienb05d7072013-10-05 13:37:10 +01001570 int l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001571
1572 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001573 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1574 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001575
1576 qstr qstr_exception_local = 0;
Damienb05d7072013-10-05 13:37:10 +01001577 int end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001578
Damiend99b0522013-12-21 18:17:45 +00001579 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001580 // this is a catch all exception handler
1581 if (i + 1 != n_except) {
1582 printf("SyntaxError: default 'except:' must be last\n");
1583 return;
1584 }
1585 } else {
1586 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001587 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1588 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1589 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1590 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001591 // handler binds the exception to a local
1592 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001593 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001594 }
1595 }
1596 EMIT(dup_top);
1597 compile_node(comp, pns_exception_expr);
1598 EMIT(compare_op, RT_COMPARE_OP_EXCEPTION_MATCH);
1599 EMIT(pop_jump_if_false, end_finally_label);
1600 }
1601
1602 EMIT(pop_top);
1603
1604 if (qstr_exception_local == 0) {
1605 EMIT(pop_top);
1606 } else {
Damien4b03e772013-10-05 14:17:09 +01001607 EMIT(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001608 }
1609
1610 EMIT(pop_top);
1611
Damiene2880aa2013-12-20 14:22:59 +00001612 int l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001613 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001614 l3 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001615 EMIT(setup_finally, l3);
1616 }
1617 compile_node(comp, pns_except->nodes[1]);
1618 if (qstr_exception_local != 0) {
1619 EMIT(pop_block);
1620 }
1621 EMIT(pop_except);
1622 if (qstr_exception_local != 0) {
Damiend99b0522013-12-21 18:17:45 +00001623 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001624 EMIT(label_assign, l3);
Damiend99b0522013-12-21 18:17:45 +00001625 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien4b03e772013-10-05 14:17:09 +01001626 EMIT(store_id, qstr_exception_local);
1627 EMIT(delete_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001628 EMIT(end_finally);
1629 }
1630 EMIT(jump, l2);
1631 EMIT(label_assign, end_finally_label);
1632 }
1633
1634 EMIT(end_finally);
1635 EMIT(label_assign, success_label);
1636 comp->except_nest_level -= 1;
1637 compile_node(comp, pn_else); // else block, can be null
1638 EMIT(label_assign, l2);
1639 EMIT(set_stack_size, stack_size);
1640}
1641
Damiend99b0522013-12-21 18:17:45 +00001642void 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 +01001643 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1644 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001645 int l_finally_block = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001646 EMIT(setup_finally, l_finally_block);
1647 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001648 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien429d7192013-10-04 19:53:11 +01001649 compile_node(comp, pn_body);
1650 } else {
1651 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1652 }
1653 EMIT(pop_block);
Damiend99b0522013-12-21 18:17:45 +00001654 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001655 EMIT(label_assign, l_finally_block);
1656 compile_node(comp, pn_finally);
1657 EMIT(end_finally);
1658 EMIT(set_stack_size, stack_size);
1659}
1660
Damiend99b0522013-12-21 18:17:45 +00001661void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1662 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1663 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1664 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001665 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001666 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1667 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001668 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001669 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001670 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001671 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001672 // no finally
1673 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1674 } else {
1675 // have finally
Damiend99b0522013-12-21 18:17:45 +00001676 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 +01001677 }
1678 } else {
1679 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001680 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001681 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001682 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001683 }
1684 } else {
1685 // shouldn't happen
1686 assert(0);
1687 }
1688}
1689
Damiend99b0522013-12-21 18:17:45 +00001690void 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 +01001691 if (n == 0) {
1692 // no more pre-bits, compile the body of the with
1693 compile_node(comp, body);
1694 } else {
Damienb05d7072013-10-05 13:37:10 +01001695 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001696 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001697 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001698 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001699 compile_node(comp, pns->nodes[0]);
1700 EMIT(setup_with, l_end);
1701 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1702 } else {
1703 // this pre-bit is just an expression
1704 compile_node(comp, nodes[0]);
1705 EMIT(setup_with, l_end);
1706 EMIT(pop_top);
1707 }
1708 // compile additional pre-bits and the body
1709 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1710 // finish this with block
1711 EMIT(pop_block);
Damiend99b0522013-12-21 18:17:45 +00001712 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001713 EMIT(label_assign, l_end);
1714 EMIT(with_cleanup);
1715 EMIT(end_finally);
1716 }
1717}
1718
Damiend99b0522013-12-21 18:17:45 +00001719void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001720 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001721 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001722 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1723 assert(n > 0);
1724
1725 // compile in a nested fashion
1726 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1727}
1728
Damiend99b0522013-12-21 18:17:45 +00001729void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1730 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001731 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1732 // for REPL, evaluate then print the expression
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00001733 EMIT(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001734 compile_node(comp, pns->nodes[0]);
1735 EMIT(call_function, 1, 0, false, false);
1736 EMIT(pop_top);
1737
Damien429d7192013-10-04 19:53:11 +01001738 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001739 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001740 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001741 // do nothing with a lonely constant
1742 } else {
1743 compile_node(comp, pns->nodes[0]); // just an expression
1744 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1745 }
Damien429d7192013-10-04 19:53:11 +01001746 }
1747 } else {
Damiend99b0522013-12-21 18:17:45 +00001748 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1749 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001750 if (kind == PN_expr_stmt_augassign) {
1751 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1752 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001753 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001754 // note that we don't really need to implement separate inplace ops, just normal binary ops will suffice
Damiend99b0522013-12-21 18:17:45 +00001755 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
1756 case MP_TOKEN_DEL_PIPE_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_OR); break;
1757 case MP_TOKEN_DEL_CARET_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_XOR); break;
1758 case MP_TOKEN_DEL_AMPERSAND_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_AND); break;
1759 case MP_TOKEN_DEL_DBL_LESS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_LSHIFT); break;
1760 case MP_TOKEN_DEL_DBL_MORE_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_RSHIFT); break;
1761 case MP_TOKEN_DEL_PLUS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_ADD); break;
1762 case MP_TOKEN_DEL_MINUS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_SUBTRACT); break;
1763 case MP_TOKEN_DEL_STAR_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_MULTIPLY); break;
1764 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_FLOOR_DIVIDE); break;
1765 case MP_TOKEN_DEL_SLASH_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_TRUE_DIVIDE); break;
1766 case MP_TOKEN_DEL_PERCENT_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_MODULO); break;
1767 case MP_TOKEN_DEL_DBL_STAR_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_POWER); break;
Damien429d7192013-10-04 19:53:11 +01001768 default: assert(0); // shouldn't happen
1769 }
1770 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1771 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001772 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1773 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001774 // following CPython, we store left-most first
1775 if (rhs > 0) {
1776 EMIT(dup_top);
1777 }
1778 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1779 for (int i = 0; i < rhs; i++) {
1780 if (i + 1 < rhs) {
1781 EMIT(dup_top);
1782 }
Damiend99b0522013-12-21 18:17:45 +00001783 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001784 }
1785 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001786 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1787 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1788 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1789 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01001790 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001791 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1792 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001793 compile_node(comp, pns10->nodes[0]); // rhs
1794 compile_node(comp, pns10->nodes[1]); // rhs
1795 EMIT(rot_two);
1796 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1797 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00001798 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1799 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1800 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1801 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01001802 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001803 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1804 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001805 compile_node(comp, pns10->nodes[0]); // rhs
1806 compile_node(comp, pns10->nodes[1]); // rhs
1807 compile_node(comp, pns10->nodes[2]); // rhs
1808 EMIT(rot_three);
1809 EMIT(rot_two);
1810 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1811 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1812 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1813 } else {
1814 compile_node(comp, pns1->nodes[0]); // rhs
1815 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1816 }
1817 } else {
1818 // shouldn't happen
1819 assert(0);
1820 }
1821 }
1822}
1823
Damiend99b0522013-12-21 18:17:45 +00001824void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, rt_binary_op_t binary_op) {
1825 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001826 compile_node(comp, pns->nodes[0]);
1827 for (int i = 1; i < num_nodes; i += 1) {
1828 compile_node(comp, pns->nodes[i]);
1829 EMIT(binary_op, binary_op);
1830 }
1831}
1832
Damiend99b0522013-12-21 18:17:45 +00001833void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1834 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1835 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001836
1837 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001838 int l_fail = comp_next_label(comp);
1839 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001840 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1841 compile_node(comp, pns->nodes[0]); // success value
1842 EMIT(jump, l_end);
1843 EMIT(label_assign, l_fail);
1844 EMIT(set_stack_size, stack_size); // force stack size reset
1845 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1846 EMIT(label_assign, l_end);
1847}
1848
Damiend99b0522013-12-21 18:17:45 +00001849void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001850 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00001851 //mp_parse_node_t pn_params = pns->nodes[0];
1852 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001853
1854 if (comp->pass == PASS_1) {
1855 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00001856 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 +01001857 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001858 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001859 }
1860
1861 // get the scope for this lambda
1862 scope_t *this_scope = (scope_t*)pns->nodes[2];
1863
1864 // make the lambda
1865 close_over_variables_etc(comp, this_scope, 0, 0);
1866}
1867
Damiend99b0522013-12-21 18:17:45 +00001868void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001869 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001870 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001871 for (int i = 0; i < n; i += 1) {
1872 compile_node(comp, pns->nodes[i]);
1873 if (i + 1 < n) {
1874 EMIT(jump_if_true_or_pop, l_end);
1875 }
1876 }
1877 EMIT(label_assign, l_end);
1878}
1879
Damiend99b0522013-12-21 18:17:45 +00001880void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001881 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001882 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001883 for (int i = 0; i < n; i += 1) {
1884 compile_node(comp, pns->nodes[i]);
1885 if (i + 1 < n) {
1886 EMIT(jump_if_false_or_pop, l_end);
1887 }
1888 }
1889 EMIT(label_assign, l_end);
1890}
1891
Damiend99b0522013-12-21 18:17:45 +00001892void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001893 compile_node(comp, pns->nodes[0]);
1894 EMIT(unary_op, RT_UNARY_OP_NOT);
1895}
1896
Damiend99b0522013-12-21 18:17:45 +00001897void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001898 int stack_size = EMIT(get_stack_size);
Damiend99b0522013-12-21 18:17:45 +00001899 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001900 compile_node(comp, pns->nodes[0]);
1901 bool multi = (num_nodes > 3);
1902 int l_fail = 0;
1903 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001904 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001905 }
1906 for (int i = 1; i + 1 < num_nodes; i += 2) {
1907 compile_node(comp, pns->nodes[i + 1]);
1908 if (i + 2 < num_nodes) {
1909 EMIT(dup_top);
1910 EMIT(rot_three);
1911 }
Damiend99b0522013-12-21 18:17:45 +00001912 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_LESS)) {
Damien429d7192013-10-04 19:53:11 +01001913 EMIT(compare_op, RT_COMPARE_OP_LESS);
Damiend99b0522013-12-21 18:17:45 +00001914 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MORE)) {
Damien429d7192013-10-04 19:53:11 +01001915 EMIT(compare_op, RT_COMPARE_OP_MORE);
Damiend99b0522013-12-21 18:17:45 +00001916 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_EQUAL)) {
Damien429d7192013-10-04 19:53:11 +01001917 EMIT(compare_op, RT_COMPARE_OP_EQUAL);
Damiend99b0522013-12-21 18:17:45 +00001918 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_LESS_EQUAL)) {
Damien429d7192013-10-04 19:53:11 +01001919 EMIT(compare_op, RT_COMPARE_OP_LESS_EQUAL);
Damiend99b0522013-12-21 18:17:45 +00001920 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MORE_EQUAL)) {
Damien429d7192013-10-04 19:53:11 +01001921 EMIT(compare_op, RT_COMPARE_OP_MORE_EQUAL);
Damiend99b0522013-12-21 18:17:45 +00001922 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_NOT_EQUAL)) {
Damien429d7192013-10-04 19:53:11 +01001923 EMIT(compare_op, RT_COMPARE_OP_NOT_EQUAL);
Damiend99b0522013-12-21 18:17:45 +00001924 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_KW_IN)) {
Damien429d7192013-10-04 19:53:11 +01001925 EMIT(compare_op, RT_COMPARE_OP_IN);
Damiend99b0522013-12-21 18:17:45 +00001926 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
1927 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
1928 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01001929 if (kind == PN_comp_op_not_in) {
1930 EMIT(compare_op, RT_COMPARE_OP_NOT_IN);
1931 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00001932 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001933 EMIT(compare_op, RT_COMPARE_OP_IS);
1934 } else {
1935 EMIT(compare_op, RT_COMPARE_OP_IS_NOT);
1936 }
1937 } else {
1938 // shouldn't happen
1939 assert(0);
1940 }
1941 } else {
1942 // shouldn't happen
1943 assert(0);
1944 }
1945 if (i + 2 < num_nodes) {
1946 EMIT(jump_if_false_or_pop, l_fail);
1947 }
1948 }
1949 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001950 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001951 EMIT(jump, l_end);
1952 EMIT(label_assign, l_fail);
1953 EMIT(rot_two);
1954 EMIT(pop_top);
1955 EMIT(label_assign, l_end);
1956 EMIT(set_stack_size, stack_size + 1); // force stack size
1957 }
1958}
1959
Damiend99b0522013-12-21 18:17:45 +00001960void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001961 // TODO
1962 assert(0);
1963 compile_node(comp, pns->nodes[0]);
1964 //EMIT(unary_op, "UNARY_STAR");
1965}
1966
Damiend99b0522013-12-21 18:17:45 +00001967void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001968 c_binary_op(comp, pns, RT_BINARY_OP_OR);
1969}
1970
Damiend99b0522013-12-21 18:17:45 +00001971void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001972 c_binary_op(comp, pns, RT_BINARY_OP_XOR);
1973}
1974
Damiend99b0522013-12-21 18:17:45 +00001975void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001976 c_binary_op(comp, pns, RT_BINARY_OP_AND);
1977}
1978
Damiend99b0522013-12-21 18:17:45 +00001979void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1980 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001981 compile_node(comp, pns->nodes[0]);
1982 for (int i = 1; i + 1 < num_nodes; i += 2) {
1983 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00001984 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien429d7192013-10-04 19:53:11 +01001985 EMIT(binary_op, RT_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00001986 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien429d7192013-10-04 19:53:11 +01001987 EMIT(binary_op, RT_BINARY_OP_RSHIFT);
1988 } else {
1989 // shouldn't happen
1990 assert(0);
1991 }
1992 }
1993}
1994
Damiend99b0522013-12-21 18:17:45 +00001995void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1996 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001997 compile_node(comp, pns->nodes[0]);
1998 for (int i = 1; i + 1 < num_nodes; i += 2) {
1999 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002000 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien429d7192013-10-04 19:53:11 +01002001 EMIT(binary_op, RT_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002002 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien429d7192013-10-04 19:53:11 +01002003 EMIT(binary_op, RT_BINARY_OP_SUBTRACT);
2004 } else {
2005 // shouldn't happen
2006 assert(0);
2007 }
2008 }
2009}
2010
Damiend99b0522013-12-21 18:17:45 +00002011void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2012 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002013 compile_node(comp, pns->nodes[0]);
2014 for (int i = 1; i + 1 < num_nodes; i += 2) {
2015 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002016 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien429d7192013-10-04 19:53:11 +01002017 EMIT(binary_op, RT_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002018 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien429d7192013-10-04 19:53:11 +01002019 EMIT(binary_op, RT_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002020 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien429d7192013-10-04 19:53:11 +01002021 EMIT(binary_op, RT_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002022 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien429d7192013-10-04 19:53:11 +01002023 EMIT(binary_op, RT_BINARY_OP_MODULO);
2024 } else {
2025 // shouldn't happen
2026 assert(0);
2027 }
2028 }
2029}
2030
Damiend99b0522013-12-21 18:17:45 +00002031void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002032 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002033 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien429d7192013-10-04 19:53:11 +01002034 EMIT(unary_op, RT_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002035 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien429d7192013-10-04 19:53:11 +01002036 EMIT(unary_op, RT_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002037 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien429d7192013-10-04 19:53:11 +01002038 EMIT(unary_op, RT_UNARY_OP_INVERT);
2039 } else {
2040 // shouldn't happen
2041 assert(0);
2042 }
2043}
2044
Damiend99b0522013-12-21 18:17:45 +00002045void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_struct_t *pns, bool is_method_call) {
Damien429d7192013-10-04 19:53:11 +01002046 // function to call is on top of stack
2047
2048 int old_n_arg_keyword = comp->n_arg_keyword;
2049 bool old_have_star_arg = comp->have_star_arg;
2050 bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
2051 comp->n_arg_keyword = 0;
2052 comp->have_star_arg = false;
2053 comp->have_dbl_star_arg = false;
2054
2055 compile_node(comp, pns->nodes[0]); // arguments to function call; can be null
2056
2057 // compute number of positional arguments
2058 int n_positional = list_len(pns->nodes[0], PN_arglist) - comp->n_arg_keyword;
2059 if (comp->have_star_arg) {
2060 n_positional -= 1;
2061 }
2062 if (comp->have_dbl_star_arg) {
2063 n_positional -= 1;
2064 }
2065
2066 if (is_method_call) {
2067 EMIT(call_method, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg);
2068 } else {
2069 EMIT(call_function, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg);
2070 }
2071
2072 comp->n_arg_keyword = old_n_arg_keyword;
2073 comp->have_star_arg = old_have_star_arg;
2074 comp->have_dbl_star_arg = old_have_dbl_star_arg;
2075}
2076
Damiend99b0522013-12-21 18:17:45 +00002077void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2078 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002079 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002080 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 +01002081 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002082 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2083 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
2084 EMIT(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien429d7192013-10-04 19:53:11 +01002085 compile_trailer_paren_helper(comp, pns_paren, true);
2086 i += 1;
2087 } else {
2088 compile_node(comp, pns->nodes[i]);
2089 }
2090 }
2091}
2092
Damiend99b0522013-12-21 18:17:45 +00002093void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002094 compile_node(comp, pns->nodes[0]);
2095 EMIT(binary_op, RT_BINARY_OP_POWER);
2096}
2097
Damiend99b0522013-12-21 18:17:45 +00002098void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002099 // a list of strings
Damien63321742013-12-10 17:41:49 +00002100
2101 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002102 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002103 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002104 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002105 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002106 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2107 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2108 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002109 if (i == 0) {
2110 string_kind = pn_kind;
2111 } else if (pn_kind != string_kind) {
2112 printf("SyntaxError: cannot mix bytes and nonbytes literals\n");
2113 return;
2114 }
Damiend99b0522013-12-21 18:17:45 +00002115 const char *str = qstr_str(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien63321742013-12-10 17:41:49 +00002116 n_bytes += strlen(str);
Damien429d7192013-10-04 19:53:11 +01002117 }
Damien63321742013-12-10 17:41:49 +00002118
2119 // allocate memory for concatenated string/bytes
2120 char *cat_str = m_new(char, n_bytes + 1);
Damien63321742013-12-10 17:41:49 +00002121
2122 // concatenate string/bytes
Damien Georgefe8fb912014-01-02 16:36:09 +00002123 char *s_dest = cat_str;
Damien63321742013-12-10 17:41:49 +00002124 for (int i = 0; i < n; i++) {
Damien Georgefe8fb912014-01-02 16:36:09 +00002125 const char *s = qstr_str(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
2126 size_t s_len = strlen(s);
2127 memcpy(s_dest, s, s_len);
2128 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002129 }
Damien Georgefe8fb912014-01-02 16:36:09 +00002130 *s_dest = '\0';
Damien63321742013-12-10 17:41:49 +00002131
Damien732407f2013-12-29 19:33:23 +00002132 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 +01002133}
2134
2135// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002136void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2137 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2138 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2139 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002140
2141 if (comp->pass == PASS_1) {
2142 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002143 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 +01002144 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002145 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002146 }
2147
2148 // get the scope for this comprehension
2149 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2150
2151 // compile the comprehension
2152 close_over_variables_etc(comp, this_scope, 0, 0);
2153
2154 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2155 EMIT(get_iter);
2156 EMIT(call_function, 1, 0, false, false);
2157}
2158
Damiend99b0522013-12-21 18:17:45 +00002159void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2160 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002161 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002162 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2163 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2164 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2165 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2166 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2167 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2168 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002169 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002170 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002171 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002172 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002173 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002174 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002175 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002176 // generator expression
2177 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2178 } else {
2179 // tuple with 2 items
2180 goto tuple_with_2_items;
2181 }
2182 } else {
2183 // tuple with 2 items
2184 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002185 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002186 }
2187 } else {
2188 // parenthesis around a single item, is just that item
2189 compile_node(comp, pns->nodes[0]);
2190 }
2191}
2192
Damiend99b0522013-12-21 18:17:45 +00002193void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2194 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002195 // empty list
2196 EMIT(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002197 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2198 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2199 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2200 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2201 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002202 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002203 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002204 compile_node(comp, pns2->nodes[0]);
2205 EMIT(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002206 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002207 // list of many items
2208 compile_node(comp, pns2->nodes[0]);
2209 compile_generic_all_nodes(comp, pns3);
Damiend99b0522013-12-21 18:17:45 +00002210 EMIT(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
2211 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002212 // list comprehension
2213 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2214 } else {
2215 // list with 2 items
2216 goto list_with_2_items;
2217 }
2218 } else {
2219 // list with 2 items
2220 list_with_2_items:
2221 compile_node(comp, pns2->nodes[0]);
2222 compile_node(comp, pns2->nodes[1]);
2223 EMIT(build_list, 2);
2224 }
2225 } else {
2226 // list with 1 item
2227 compile_node(comp, pns->nodes[0]);
2228 EMIT(build_list, 1);
2229 }
2230}
2231
Damiend99b0522013-12-21 18:17:45 +00002232void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2233 mp_parse_node_t pn = pns->nodes[0];
2234 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002235 // empty dict
2236 EMIT(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002237 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2238 pns = (mp_parse_node_struct_t*)pn;
2239 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002240 // dict with one element
2241 EMIT(build_map, 1);
2242 compile_node(comp, pn);
2243 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002244 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2245 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2246 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2247 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002248 // dict/set with multiple elements
2249
2250 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002251 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002252 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2253
2254 // first element sets whether it's a dict or set
2255 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002256 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002257 // a dictionary
2258 EMIT(build_map, 1 + n);
2259 compile_node(comp, pns->nodes[0]);
2260 EMIT(store_map);
2261 is_dict = true;
2262 } else {
2263 // a set
2264 compile_node(comp, pns->nodes[0]); // 1st value of set
2265 is_dict = false;
2266 }
2267
2268 // process rest of elements
2269 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002270 mp_parse_node_t pn = nodes[i];
2271 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002272 compile_node(comp, pn);
2273 if (is_dict) {
2274 if (!is_key_value) {
2275 printf("SyntaxError?: expecting key:value for dictionary");
2276 return;
2277 }
2278 EMIT(store_map);
2279 } else {
2280 if (is_key_value) {
2281 printf("SyntaxError?: expecting just a value for set");
2282 return;
2283 }
2284 }
2285 }
2286
2287 // if it's a set, build it
2288 if (!is_dict) {
2289 EMIT(build_set, 1 + n);
2290 }
Damiend99b0522013-12-21 18:17:45 +00002291 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002292 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002293 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002294 // a dictionary comprehension
2295 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2296 } else {
2297 // a set comprehension
2298 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2299 }
2300 } else {
2301 // shouldn't happen
2302 assert(0);
2303 }
2304 } else {
2305 // set with one element
2306 goto set_with_one_element;
2307 }
2308 } else {
2309 // set with one element
2310 set_with_one_element:
2311 compile_node(comp, pn);
2312 EMIT(build_set, 1);
2313 }
2314}
2315
Damiend99b0522013-12-21 18:17:45 +00002316void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002317 compile_trailer_paren_helper(comp, pns, false);
2318}
2319
Damiend99b0522013-12-21 18:17:45 +00002320void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002321 // object who's index we want is on top of stack
2322 compile_node(comp, pns->nodes[0]); // the index
2323 EMIT(binary_op, RT_BINARY_OP_SUBSCR);
2324}
2325
Damiend99b0522013-12-21 18:17:45 +00002326void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002327 // object who's attribute we want is on top of stack
Damiend99b0522013-12-21 18:17:45 +00002328 EMIT(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002329}
2330
Damiend99b0522013-12-21 18:17:45 +00002331void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2332 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2333 mp_parse_node_t pn = pns->nodes[0];
2334 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002335 // [?:]
Damiend99b0522013-12-21 18:17:45 +00002336 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002337 EMIT(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002338 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2339 pns = (mp_parse_node_struct_t*)pn;
2340 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
2341 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002342 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002343 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002344 // [?::]
2345 EMIT(build_slice, 2);
2346 } else {
2347 // [?::x]
2348 compile_node(comp, pn);
2349 EMIT(build_slice, 3);
2350 }
Damiend99b0522013-12-21 18:17:45 +00002351 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002352 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002353 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2354 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2355 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2356 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002357 // [?:x:]
2358 EMIT(build_slice, 2);
2359 } else {
2360 // [?:x:x]
2361 compile_node(comp, pns->nodes[0]);
2362 EMIT(build_slice, 3);
2363 }
2364 } else {
2365 // [?:x]
2366 compile_node(comp, pn);
2367 EMIT(build_slice, 2);
2368 }
2369 } else {
2370 // [?:x]
2371 compile_node(comp, pn);
2372 EMIT(build_slice, 2);
2373 }
2374}
2375
Damiend99b0522013-12-21 18:17:45 +00002376void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002377 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002378 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2379 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002380}
2381
Damiend99b0522013-12-21 18:17:45 +00002382void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
2383 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002384 compile_subscript_3_helper(comp, pns);
2385}
2386
Damiend99b0522013-12-21 18:17:45 +00002387void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002388 // if this is called then we are compiling a dict key:value pair
2389 compile_node(comp, pns->nodes[1]); // value
2390 compile_node(comp, pns->nodes[0]); // key
2391}
2392
Damiend99b0522013-12-21 18:17:45 +00002393void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002394 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002395 // store class object into class name
Damien4b03e772013-10-05 14:17:09 +01002396 EMIT(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002397}
2398
Damiend99b0522013-12-21 18:17:45 +00002399void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002400 if (comp->have_star_arg) {
2401 printf("SyntaxError?: can't have multiple *x\n");
2402 return;
2403 }
2404 comp->have_star_arg = true;
2405 compile_node(comp, pns->nodes[0]);
2406}
2407
Damiend99b0522013-12-21 18:17:45 +00002408void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002409 if (comp->have_dbl_star_arg) {
2410 printf("SyntaxError?: can't have multiple **x\n");
2411 return;
2412 }
2413 comp->have_dbl_star_arg = true;
2414 compile_node(comp, pns->nodes[0]);
2415}
2416
Damiend99b0522013-12-21 18:17:45 +00002417void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2418 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2419 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2420 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2421 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002422 printf("SyntaxError?: lhs of keyword argument must be an id\n");
2423 return;
2424 }
Damiend99b0522013-12-21 18:17:45 +00002425 EMIT(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002426 compile_node(comp, pns2->nodes[0]);
2427 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002428 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002429 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2430 } else {
2431 // shouldn't happen
2432 assert(0);
2433 }
2434}
2435
Damiend99b0522013-12-21 18:17:45 +00002436void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002437 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
2438 printf("SyntaxError: 'yield' outside function\n");
2439 return;
2440 }
Damiend99b0522013-12-21 18:17:45 +00002441 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
2442 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002443 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002444 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2445 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002446 compile_node(comp, pns->nodes[0]);
2447 EMIT(get_iter);
Damiend99b0522013-12-21 18:17:45 +00002448 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002449 EMIT(yield_from);
2450 } else {
2451 compile_node(comp, pns->nodes[0]);
2452 EMIT(yield_value);
2453 }
2454}
2455
Damiend99b0522013-12-21 18:17:45 +00002456typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Damien429d7192013-10-04 19:53:11 +01002457static compile_function_t compile_function[] = {
2458 NULL,
2459#define nc NULL
2460#define c(f) compile_##f
2461#define DEF_RULE(rule, comp, kind, arg...) comp,
2462#include "grammar.h"
2463#undef nc
2464#undef c
2465#undef DEF_RULE
2466};
2467
Damiend99b0522013-12-21 18:17:45 +00002468void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2469 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002470 // pass
Damiend99b0522013-12-21 18:17:45 +00002471 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
2472 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
2473 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
2474 case MP_PARSE_NODE_ID: EMIT(load_id, arg); break;
2475 case MP_PARSE_NODE_SMALL_INT: EMIT(load_const_small_int, arg); break;
2476 case MP_PARSE_NODE_INTEGER: EMIT(load_const_int, arg); break;
2477 case MP_PARSE_NODE_DECIMAL: EMIT(load_const_dec, arg); break;
2478 case MP_PARSE_NODE_STRING: EMIT(load_const_str, arg, false); break;
2479 case MP_PARSE_NODE_BYTES: EMIT(load_const_str, arg, true); break;
2480 case MP_PARSE_NODE_TOKEN:
2481 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002482 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002483 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002484 // do nothing
2485 } else {
2486 EMIT(load_const_tok, arg);
2487 }
2488 break;
Damien429d7192013-10-04 19:53:11 +01002489 default: assert(0);
2490 }
2491 } else {
Damiend99b0522013-12-21 18:17:45 +00002492 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2493 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002494 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002495 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
2496 mp_parse_node_show(pn, 0);
Damien429d7192013-10-04 19:53:11 +01002497 assert(0);
2498 } else {
2499 f(comp, pns);
2500 }
2501 }
2502}
2503
Damiend99b0522013-12-21 18:17:45 +00002504void 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 +01002505 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002506 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002507 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2508 if (MP_PARSE_NODE_IS_ID(pn)) {
2509 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002510 if (comp->have_bare_star) {
2511 // comes after a bare star, so doesn't count as a parameter
2512 } else {
2513 comp->scope_cur->num_params += 1;
2514 }
Damienb14de212013-10-06 00:28:28 +01002515 } else {
Damiend99b0522013-12-21 18:17:45 +00002516 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2517 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2518 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2519 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002520 //int node_index = 1; unused
2521 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002522 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002523 // this parameter has an annotation
2524 pn_annotation = pns->nodes[1];
2525 }
2526 //node_index = 2; unused
2527 }
2528 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002529 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002530 // this parameter has a default value
2531 if (comp->have_bare_star) {
2532 comp->scope_cur->num_dict_params += 1;
2533 } else {
2534 comp->scope_cur->num_default_params += 1;
2535 }
2536 }
2537 */
2538 if (comp->have_bare_star) {
2539 // comes after a bare star, so doesn't count as a parameter
2540 } else {
2541 comp->scope_cur->num_params += 1;
2542 }
Damiend99b0522013-12-21 18:17:45 +00002543 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2544 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002545 // bare star
2546 // TODO see http://www.python.org/dev/peps/pep-3102/
2547 comp->have_bare_star = true;
2548 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002549 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002550 // named star
2551 comp->scope_cur->flags |= SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002552 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2553 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002554 // named star with annotation
2555 comp->scope_cur->flags |= SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002556 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2557 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002558 pn_annotation = pns->nodes[1];
2559 } else {
2560 // shouldn't happen
2561 assert(0);
2562 }
Damiend99b0522013-12-21 18:17:45 +00002563 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2564 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2565 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002566 // this parameter has an annotation
2567 pn_annotation = pns->nodes[1];
2568 }
2569 comp->scope_cur->flags |= SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002570 } else {
Damienb14de212013-10-06 00:28:28 +01002571 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002572 assert(0);
2573 }
Damien429d7192013-10-04 19:53:11 +01002574 }
2575
2576 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002577 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002578 // TODO this parameter has an annotation
2579 }
2580 bool added;
2581 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2582 if (!added) {
2583 printf("SyntaxError?: same name used for parameter; %s\n", qstr_str(param_name));
2584 return;
2585 }
2586 id_info->param = true;
2587 id_info->kind = ID_INFO_KIND_LOCAL;
2588 }
2589}
2590
Damiend99b0522013-12-21 18:17:45 +00002591void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002592 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2593}
2594
Damiend99b0522013-12-21 18:17:45 +00002595void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002596 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2597}
2598
Damiend99b0522013-12-21 18:17:45 +00002599void 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 +01002600 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002601 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002602 // no more nested if/for; compile inner expression
2603 compile_node(comp, pn_inner_expr);
2604 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
2605 EMIT(list_append, for_depth + 2);
2606 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
2607 EMIT(map_add, for_depth + 2);
2608 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
2609 EMIT(set_add, for_depth + 2);
2610 } else {
2611 EMIT(yield_value);
2612 EMIT(pop_top);
2613 }
Damiend99b0522013-12-21 18:17:45 +00002614 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002615 // if condition
Damiend99b0522013-12-21 18:17:45 +00002616 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002617 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2618 pn_iter = pns_comp_if->nodes[1];
2619 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002620 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002621 // for loop
Damiend99b0522013-12-21 18:17:45 +00002622 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002623 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002624 int l_end2 = comp_next_label(comp);
2625 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002626 EMIT(get_iter);
2627 EMIT(label_assign, l_top2);
2628 EMIT(for_iter, l_end2);
2629 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2630 compile_scope_comp_iter(comp, pns_comp_for2->nodes[2], pn_inner_expr, l_top2, for_depth + 1);
2631 EMIT(jump, l_top2);
2632 EMIT(label_assign, l_end2);
2633 EMIT(for_iter_end);
2634 } else {
2635 // shouldn't happen
2636 assert(0);
2637 }
2638}
2639
Damiend99b0522013-12-21 18:17:45 +00002640void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002641 // see http://www.python.org/dev/peps/pep-0257/
2642
2643 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002644 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002645 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002646 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002647 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002648 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2649 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002650 for (int i = 0; i < num_nodes; i++) {
2651 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002652 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 +00002653 // not a newline, so this is the first statement; finish search
2654 break;
2655 }
2656 }
2657 // 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 +00002658 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002659 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002660 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002661 } else {
2662 return;
2663 }
2664
2665 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002666 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2667 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2668 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2669 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2670 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002671 compile_node(comp, pns->nodes[0]); // a doc string
2672 // store doc string
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002673 EMIT(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002674 }
2675 }
2676 }
2677}
2678
2679void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2680 comp->pass = pass;
2681 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002682 comp->next_label = 1;
Damien415eb6f2013-10-05 12:19:06 +01002683 EMIT(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002684
2685 if (comp->pass == PASS_1) {
2686 scope->stack_size = 0;
2687 }
2688
Damien5ac1b2e2013-10-18 19:58:12 +01002689#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002690 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002691 scope_print_info(scope);
2692 }
Damien5ac1b2e2013-10-18 19:58:12 +01002693#endif
Damien429d7192013-10-04 19:53:11 +01002694
2695 // compile
2696 if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002697 if (!comp->is_repl) {
2698 check_for_doc_string(comp, scope->pn);
2699 }
Damien429d7192013-10-04 19:53:11 +01002700 compile_node(comp, scope->pn);
Damiend99b0522013-12-21 18:17:45 +00002701 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002702 EMIT(return_value);
2703 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002704 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2705 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2706 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002707
2708 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002709 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002710 if (comp->pass == PASS_1) {
2711 comp->have_bare_star = false;
2712 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2713 }
2714
Damiend99b0522013-12-21 18:17:45 +00002715 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // 2 is something...
Damien429d7192013-10-04 19:53:11 +01002716
2717 compile_node(comp, pns->nodes[3]); // 3 is function body
2718 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002719 if (!EMIT(last_emit_was_return_value)) {
Damiend99b0522013-12-21 18:17:45 +00002720 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002721 EMIT(return_value);
2722 }
2723 } else if (scope->kind == SCOPE_LAMBDA) {
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_NUM_NODES(pns) == 3);
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[0], PN_varargslist, compile_scope_lambda_param);
2733 }
2734
2735 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2736 EMIT(return_value);
2737 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2738 // a bit of a hack at the moment
2739
Damiend99b0522013-12-21 18:17:45 +00002740 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2741 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2742 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2743 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2744 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002745
Damien6cdd3af2013-10-05 18:08:26 +01002746 qstr qstr_arg = qstr_from_str_static(".0");
Damien429d7192013-10-04 19:53:11 +01002747 if (comp->pass == PASS_1) {
2748 bool added;
2749 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2750 assert(added);
2751 id_info->kind = ID_INFO_KIND_LOCAL;
2752 scope->num_params = 1;
2753 }
2754
2755 if (scope->kind == SCOPE_LIST_COMP) {
2756 EMIT(build_list, 0);
2757 } else if (scope->kind == SCOPE_DICT_COMP) {
2758 EMIT(build_map, 0);
2759 } else if (scope->kind == SCOPE_SET_COMP) {
2760 EMIT(build_set, 0);
2761 }
2762
Damienb05d7072013-10-05 13:37:10 +01002763 int l_end = comp_next_label(comp);
2764 int l_top = comp_next_label(comp);
Damien4b03e772013-10-05 14:17:09 +01002765 EMIT(load_id, qstr_arg);
Damien429d7192013-10-04 19:53:11 +01002766 EMIT(label_assign, l_top);
2767 EMIT(for_iter, l_end);
2768 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2769 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
2770 EMIT(jump, l_top);
2771 EMIT(label_assign, l_end);
2772 EMIT(for_iter_end);
2773
2774 if (scope->kind == SCOPE_GEN_EXPR) {
Damiend99b0522013-12-21 18:17:45 +00002775 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002776 }
2777 EMIT(return_value);
2778 } else {
2779 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002780 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2781 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2782 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01002783
2784 if (comp->pass == PASS_1) {
2785 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002786 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01002787 assert(added);
2788 id_info->kind = ID_INFO_KIND_LOCAL;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002789 id_info = scope_find_or_add_id(scope, MP_QSTR___locals__, &added);
Damien429d7192013-10-04 19:53:11 +01002790 assert(added);
2791 id_info->kind = ID_INFO_KIND_LOCAL;
2792 id_info->param = true;
2793 scope->num_params = 1; // __locals__ is the parameter
2794 }
2795
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002796 EMIT(load_id, MP_QSTR___locals__);
Damien429d7192013-10-04 19:53:11 +01002797 EMIT(store_locals);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002798 EMIT(load_id, MP_QSTR___name__);
2799 EMIT(store_id, MP_QSTR___module__);
Damiend99b0522013-12-21 18:17:45 +00002800 EMIT(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002801 EMIT(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01002802
2803 check_for_doc_string(comp, pns->nodes[2]);
2804 compile_node(comp, pns->nodes[2]); // 2 is class body
2805
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002806 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01002807 assert(id != NULL);
2808 if (id->kind == ID_INFO_KIND_LOCAL) {
Damiend99b0522013-12-21 18:17:45 +00002809 EMIT(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002810 } else {
Damien George6baf76e2013-12-30 22:32:17 +00002811#if MICROPY_EMIT_CPYTHON
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002812 EMIT(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002813#else
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002814 EMIT(load_fast, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002815#endif
Damien429d7192013-10-04 19:53:11 +01002816 }
2817 EMIT(return_value);
2818 }
2819
Damien415eb6f2013-10-05 12:19:06 +01002820 EMIT(end_pass);
Damienb05d7072013-10-05 13:37:10 +01002821
Damien826005c2013-10-05 23:17:28 +01002822}
2823
2824void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2825 comp->pass = pass;
2826 comp->scope_cur = scope;
2827 comp->next_label = 1;
2828
2829 if (scope->kind != SCOPE_FUNCTION) {
2830 printf("Error: inline assembler must be a function\n");
2831 return;
2832 }
2833
Damiena2f2f7d2013-10-06 00:14:13 +01002834 if (comp->pass > PASS_1) {
2835 EMIT_INLINE_ASM(start_pass, comp->pass, comp->scope_cur);
2836 }
2837
Damien826005c2013-10-05 23:17:28 +01002838 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00002839 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2840 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2841 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01002842
Damiend99b0522013-12-21 18:17:45 +00002843 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01002844
Damiena2f2f7d2013-10-06 00:14:13 +01002845 // parameters are in pns->nodes[1]
2846 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00002847 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01002848 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
2849 scope->num_params = EMIT_INLINE_ASM(count_params, n_params, pn_params);
2850 }
2851
Damiend99b0522013-12-21 18:17:45 +00002852 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01002853
Damiend99b0522013-12-21 18:17:45 +00002854 mp_parse_node_t pn_body = pns->nodes[3]; // body
2855 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01002856 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
2857
Damien826005c2013-10-05 23:17:28 +01002858 if (comp->pass == PASS_3) {
2859 //printf("----\n");
2860 scope_print_info(scope);
2861 }
2862
2863 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00002864 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
2865 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
2866 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
2867 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
2868 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
2869 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
2870 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
2871 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
2872 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
2873 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
2874 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
2875 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
2876 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01002877 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
2878
2879 // emit instructions
2880 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00002881 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien826005c2013-10-05 23:17:28 +01002882 printf("SyntaxError: inline assembler 'label' requires 1 argument\n");
2883 return;
2884 }
2885 int lab = comp_next_label(comp);
2886 if (pass > PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00002887 EMIT_INLINE_ASM(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01002888 }
2889 } else {
2890 if (pass > PASS_1) {
2891 EMIT_INLINE_ASM(op, op, n_args, pn_arg);
2892 }
2893 }
2894 }
2895
2896 if (comp->pass > PASS_1) {
2897 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01002898 }
Damien429d7192013-10-04 19:53:11 +01002899}
2900
2901void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
2902 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00002903 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01002904 scope->num_locals = 0;
2905 for (int i = 0; i < scope->id_info_len; i++) {
2906 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002907 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01002908 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
2909 continue;
2910 }
2911 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
2912 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
2913 }
Damien9ecbcff2013-12-11 00:41:43 +00002914 // note: params always count for 1 local, even if they are a cell
Damien429d7192013-10-04 19:53:11 +01002915 if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
2916 id->local_num = scope->num_locals;
2917 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00002918 }
2919 }
2920
2921 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00002922#if MICROPY_EMIT_CPYTHON
2923 int num_cell = 0;
2924#endif
Damien9ecbcff2013-12-11 00:41:43 +00002925 for (int i = 0; i < scope->id_info_len; i++) {
2926 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00002927#if MICROPY_EMIT_CPYTHON
2928 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00002929 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00002930 id->local_num = num_cell;
2931 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00002932 }
Damien George6baf76e2013-12-30 22:32:17 +00002933#else
2934 // in Micro Python the cells come right after the fast locals
2935 // parameters are not counted here, since they remain at the start
2936 // of the locals, even if they are cell vars
2937 if (!id->param && id->kind == ID_INFO_KIND_CELL) {
2938 id->local_num = scope->num_locals;
2939 scope->num_locals += 1;
2940 }
2941#endif
Damien9ecbcff2013-12-11 00:41:43 +00002942 }
Damien9ecbcff2013-12-11 00:41:43 +00002943
2944 // compute the index of free vars (freevars[idx] in CPython)
2945 // make sure they are in the order of the parent scope
2946 if (scope->parent != NULL) {
2947 int num_free = 0;
2948 for (int i = 0; i < scope->parent->id_info_len; i++) {
2949 id_info_t *id = &scope->parent->id_info[i];
2950 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
2951 for (int j = 0; j < scope->id_info_len; j++) {
2952 id_info_t *id2 = &scope->id_info[j];
2953 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +00002954 assert(!id2->param); // free vars should not be params
2955#if MICROPY_EMIT_CPYTHON
2956 // in CPython the frees are numbered after the cells
2957 id2->local_num = num_cell + num_free;
2958#else
2959 // in Micro Python the frees come first, before the params
2960 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00002961#endif
2962 num_free += 1;
2963 }
2964 }
2965 }
Damien429d7192013-10-04 19:53:11 +01002966 }
Damien George6baf76e2013-12-30 22:32:17 +00002967#if !MICROPY_EMIT_CPYTHON
2968 // in Micro Python shift all other locals after the free locals
2969 if (num_free > 0) {
2970 for (int i = 0; i < scope->id_info_len; i++) {
2971 id_info_t *id = &scope->id_info[i];
2972 if (id->param || id->kind != ID_INFO_KIND_FREE) {
2973 id->local_num += num_free;
2974 }
2975 }
2976 scope->num_params += num_free; // free vars are counted as params for passing them into the function
2977 scope->num_locals += num_free;
2978 }
2979#endif
Damien429d7192013-10-04 19:53:11 +01002980 }
2981
2982 // compute flags
2983 //scope->flags = 0; since we set some things in parameters
2984 if (scope->kind != SCOPE_MODULE) {
2985 scope->flags |= SCOPE_FLAG_NEWLOCALS;
2986 }
2987 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) {
2988 assert(scope->parent != NULL);
2989 scope->flags |= SCOPE_FLAG_OPTIMISED;
2990
2991 // TODO possibly other ways it can be nested
2992 if (scope->parent->kind == SCOPE_FUNCTION || (scope->parent->kind == SCOPE_CLASS && scope->parent->parent->kind == SCOPE_FUNCTION)) {
2993 scope->flags |= SCOPE_FLAG_NESTED;
2994 }
2995 }
2996 int num_free = 0;
2997 for (int i = 0; i < scope->id_info_len; i++) {
2998 id_info_t *id = &scope->id_info[i];
2999 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3000 num_free += 1;
3001 }
3002 }
3003 if (num_free == 0) {
3004 scope->flags |= SCOPE_FLAG_NOFREE;
3005 }
3006}
3007
Damien George1fb03172014-01-03 14:22:03 +00003008mp_obj_t mp_compile(mp_parse_node_t pn, bool is_repl) {
Damien429d7192013-10-04 19:53:11 +01003009 compiler_t *comp = m_new(compiler_t, 1);
3010
Damien5ac1b2e2013-10-18 19:58:12 +01003011 comp->is_repl = is_repl;
3012 comp->had_error = false;
3013
Damien429d7192013-10-04 19:53:11 +01003014 comp->break_label = 0;
3015 comp->continue_label = 0;
3016 comp->except_nest_level = 0;
3017 comp->scope_head = NULL;
3018 comp->scope_cur = NULL;
3019
Damien826005c2013-10-05 23:17:28 +01003020 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003021 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003022
3023 // set the outer scope
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003024 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE);
Damien429d7192013-10-04 19:53:11 +01003025
Damien826005c2013-10-05 23:17:28 +01003026 // compile pass 1
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003027 comp->emit = emit_pass1_new(MP_QSTR___class__);
Damien826005c2013-10-05 23:17:28 +01003028 comp->emit_method_table = &emit_pass1_method_table;
3029 comp->emit_inline_asm = NULL;
3030 comp->emit_inline_asm_method_table = NULL;
3031 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003032 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003033 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003034#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003035 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003036 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003037#endif
Damien826005c2013-10-05 23:17:28 +01003038 } else {
3039 compile_scope(comp, s, PASS_1);
3040 }
3041
3042 // update maximim number of labels needed
3043 if (comp->next_label > max_num_labels) {
3044 max_num_labels = comp->next_label;
3045 }
Damien429d7192013-10-04 19:53:11 +01003046 }
3047
Damien826005c2013-10-05 23:17:28 +01003048 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003049 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003050 compile_scope_compute_things(comp, s);
3051 }
3052
Damien826005c2013-10-05 23:17:28 +01003053 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003054 emit_pass1_free(comp->emit);
3055
Damien826005c2013-10-05 23:17:28 +01003056 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003057#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003058 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003059#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003060 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003061#endif
Damien3ef4abb2013-10-12 16:53:13 +01003062#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003063 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003064#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003065#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003066 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003067 if (false) {
3068 // dummy
3069
Damien3ef4abb2013-10-12 16:53:13 +01003070#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003071 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
3072 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003073 if (emit_inline_thumb == NULL) {
3074 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3075 }
3076 comp->emit = NULL;
3077 comp->emit_method_table = NULL;
3078 comp->emit_inline_asm = emit_inline_thumb;
3079 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3080 compile_scope_inline_asm(comp, s, PASS_2);
3081 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003082#endif
3083
Damien826005c2013-10-05 23:17:28 +01003084 } else {
Damienc025ebb2013-10-12 14:30:21 +01003085
3086 // choose the emit type
3087
Damien3ef4abb2013-10-12 16:53:13 +01003088#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003089 comp->emit = emit_cpython_new(max_num_labels);
3090 comp->emit_method_table = &emit_cpython_method_table;
3091#else
Damien826005c2013-10-05 23:17:28 +01003092 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003093
3094#if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003095 case EMIT_OPT_NATIVE_PYTHON:
Damien3410be82013-10-07 23:09:10 +01003096 case EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003097#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003098 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003099 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003100 }
Damien13ed3a62013-10-08 09:05:10 +01003101 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003102#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003103 if (emit_native == NULL) {
3104 emit_native = emit_native_thumb_new(max_num_labels);
3105 }
3106 comp->emit_method_table = &emit_native_thumb_method_table;
3107#endif
3108 comp->emit = emit_native;
Damien3410be82013-10-07 23:09:10 +01003109 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003110 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003111#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003112
Damien826005c2013-10-05 23:17:28 +01003113 default:
3114 if (emit_bc == NULL) {
3115 emit_bc = emit_bc_new(max_num_labels);
3116 }
3117 comp->emit = emit_bc;
3118 comp->emit_method_table = &emit_bc_method_table;
3119 break;
3120 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003121#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003122
3123 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003124 compile_scope(comp, s, PASS_2);
3125 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003126 }
Damien429d7192013-10-04 19:53:11 +01003127 }
3128
Damien George1fb03172014-01-03 14:22:03 +00003129 bool had_error = comp->had_error;
Damien732407f2013-12-29 19:33:23 +00003130 m_del_obj(compiler_t, comp);
Damien5ac1b2e2013-10-18 19:58:12 +01003131
Damien George1fb03172014-01-03 14:22:03 +00003132 if (had_error) {
3133 // TODO return a proper error message
3134 return mp_const_none;
3135 } else {
3136#if MICROPY_EMIT_CPYTHON
3137 // can't create code, so just return true
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003138 (void)module_scope; // to suppress warning that module_scope is unused
Damien George1fb03172014-01-03 14:22:03 +00003139 return mp_const_true;
3140#else
3141 // return function that executes the outer module
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003142 return rt_make_function_from_id(module_scope->unique_code_id);
Damien George1fb03172014-01-03 14:22:03 +00003143#endif
3144 }
Damien429d7192013-10-04 19:53:11 +01003145}