blob: b2000d5b09334d974ec80a139a381a71efebbb8a [file] [log] [blame]
xbeefe34222014-03-16 00:14:26 -07001#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +01002#include <stdint.h>
3#include <stdio.h>
4#include <string.h>
5#include <assert.h>
Rachel Dowdall56402792014-03-22 20:19:24 +00006#include <math.h>
Damien429d7192013-10-04 19:53:11 +01007
8#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00009#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000010#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010011#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010012#include "parse.h"
13#include "scope.h"
Damiend99b0522013-12-21 18:17:45 +000014#include "runtime0.h"
Damien429d7192013-10-04 19:53:11 +010015#include "emit.h"
Damien George2326d522014-03-27 23:26:35 +000016#include "emitglue.h"
Damien George1fb03172014-01-03 14:22:03 +000017#include "obj.h"
18#include "compile.h"
19#include "runtime.h"
Rachel Dowdallcde86312014-03-22 17:29:27 +000020#include "intdivmod.h"
Damien429d7192013-10-04 19:53:11 +010021
22// TODO need to mangle __attr names
23
Damience89a212013-10-15 22:25:17 +010024#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_THUMB)
25
Damien429d7192013-10-04 19:53:11 +010026typedef enum {
27 PN_none = 0,
Damien George00208ce2014-01-23 00:00:53 +000028#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien429d7192013-10-04 19:53:11 +010029#include "grammar.h"
30#undef DEF_RULE
31 PN_maximum_number_of,
32} pn_kind_t;
33
Damien Georgeb9791222014-01-23 00:34:21 +000034#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
35#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
36#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
37#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010038
Damien6cdd3af2013-10-05 18:08:26 +010039#define EMIT_OPT_NONE (0)
40#define EMIT_OPT_BYTE_CODE (1)
41#define EMIT_OPT_NATIVE_PYTHON (2)
Damien7af3d192013-10-07 00:02:49 +010042#define EMIT_OPT_VIPER (3)
43#define EMIT_OPT_ASM_THUMB (4)
Damien6cdd3af2013-10-05 18:08:26 +010044
Damien429d7192013-10-04 19:53:11 +010045typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000046 qstr source_file;
Damien5ac1b2e2013-10-18 19:58:12 +010047 bool is_repl;
Damien429d7192013-10-04 19:53:11 +010048 pass_kind_t pass;
Damien5ac1b2e2013-10-18 19:58:12 +010049 bool had_error; // try to keep compiler clean from nlr
Damien429d7192013-10-04 19:53:11 +010050
Damienb05d7072013-10-05 13:37:10 +010051 int next_label;
Damienb05d7072013-10-05 13:37:10 +010052
Damien429d7192013-10-04 19:53:11 +010053 int break_label;
54 int continue_label;
Damien Georgecbddb272014-02-01 20:08:18 +000055 int break_continue_except_level;
56 int cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien429d7192013-10-04 19:53:11 +010057
58 int n_arg_keyword;
59 bool have_star_arg;
60 bool have_dbl_star_arg;
61 bool have_bare_star;
62 int param_pass;
63 int param_pass_num_dict_params;
64 int param_pass_num_default_params;
65
Damien George35e2a4e2014-02-05 00:51:47 +000066 bool func_arg_is_super; // used to compile special case of super() function call
67
Damien429d7192013-10-04 19:53:11 +010068 scope_t *scope_head;
69 scope_t *scope_cur;
70
Damien6cdd3af2013-10-05 18:08:26 +010071 emit_t *emit; // current emitter
72 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010073
74 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
75 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 +010076} compiler_t;
77
Damien Georgef41fdd02014-03-03 23:19:11 +000078STATIC void compile_syntax_error(compiler_t *comp, const char *msg) {
79 // TODO store the error message to a variable in compiler_t instead of printing it
80 printf("SyntaxError: %s\n", msg);
81 comp->had_error = true;
82}
83
Damiend99b0522013-12-21 18:17:45 +000084mp_parse_node_t fold_constants(mp_parse_node_t pn) {
85 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
86 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
87 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +010088
89 // fold arguments first
90 for (int i = 0; i < n; i++) {
91 pns->nodes[i] = fold_constants(pns->nodes[i]);
92 }
93
Damiend99b0522013-12-21 18:17:45 +000094 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +010095 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +000096 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +020097 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
98 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +000099 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien3ef4abb2013-10-12 16:53:13 +0100100#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100101 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000102 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
Damien0efb3a12013-10-12 16:16:56 +0100103#endif
Damiend99b0522013-12-21 18:17:45 +0000104 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
105 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100106 } else {
107 // shouldn't happen
108 assert(0);
109 }
110 }
111 break;
112
113 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100114 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000115 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200116 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
117 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damien0efb3a12013-10-12 16:16:56 +0100118 machine_int_t res;
Damiend99b0522013-12-21 18:17:45 +0000119 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100120 res = arg0 + arg1;
Damiend99b0522013-12-21 18:17:45 +0000121 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100122 res = arg0 - arg1;
Damien429d7192013-10-04 19:53:11 +0100123 } else {
124 // shouldn't happen
125 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100126 res = 0;
127 }
Paul Sokolovskybbf0e2f2014-02-21 02:04:32 +0200128 if (MP_PARSE_FITS_SMALL_INT(res)) {
Damiend99b0522013-12-21 18:17:45 +0000129 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, res);
Damien429d7192013-10-04 19:53:11 +0100130 }
131 }
132 break;
133
134 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000135 if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200136 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
137 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000138 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien3ef4abb2013-10-12 16:53:13 +0100139#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100140 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000141 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 * arg1);
Damien0efb3a12013-10-12 16:16:56 +0100142#endif
Damiend99b0522013-12-21 18:17:45 +0000143 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien429d7192013-10-04 19:53:11 +0100144 ; // pass
Damiend99b0522013-12-21 18:17:45 +0000145 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Rachel Dowdallcde86312014-03-22 17:29:27 +0000146 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, python_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000147 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Damien George8dcc0c72014-03-27 10:55:21 +0000148 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, python_floor_divide(arg0, arg1));
Damien429d7192013-10-04 19:53:11 +0100149 } else {
150 // shouldn't happen
151 assert(0);
152 }
153 }
154 break;
155
156 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000157 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200158 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000159 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
160 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
161 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
162 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
163 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
164 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100165 } else {
166 // shouldn't happen
167 assert(0);
168 }
169 }
170 break;
171
Damien3ef4abb2013-10-12 16:53:13 +0100172#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +0100173 case PN_power:
Damien0efb3a12013-10-12 16:16:56 +0100174 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000175 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])) {
176 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
177 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200178 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100179 if (power >= 0) {
180 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200181 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100182 for (; power > 0; power--) {
183 ans *= base;
184 }
Damiend99b0522013-12-21 18:17:45 +0000185 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100186 }
187 }
188 }
189 break;
Damien0efb3a12013-10-12 16:16:56 +0100190#endif
Damien429d7192013-10-04 19:53:11 +0100191 }
192 }
193
194 return pn;
195}
196
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200197STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra);
Damien George8dcc0c72014-03-27 10:55:21 +0000198STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100199
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200200STATIC int comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100201 return comp->next_label++;
202}
203
Damien George8dcc0c72014-03-27 10:55:21 +0000204STATIC void compile_increase_except_level(compiler_t *comp) {
205 comp->cur_except_level += 1;
206 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
207 comp->scope_cur->exc_stack_size = comp->cur_except_level;
208 }
209}
210
211STATIC void compile_decrease_except_level(compiler_t *comp) {
212 assert(comp->cur_except_level > 0);
213 comp->cur_except_level -= 1;
214}
215
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200216STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) {
Damien George2326d522014-03-27 23:26:35 +0000217 scope_t *scope = scope_new(kind, pn, comp->source_file, mp_emit_glue_get_unique_code_id(), emit_options);
Damien429d7192013-10-04 19:53:11 +0100218 scope->parent = comp->scope_cur;
219 scope->next = NULL;
220 if (comp->scope_head == NULL) {
221 comp->scope_head = scope;
222 } else {
223 scope_t *s = comp->scope_head;
224 while (s->next != NULL) {
225 s = s->next;
226 }
227 s->next = scope;
228 }
229 return scope;
230}
231
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200232STATIC int list_len(mp_parse_node_t pn, int pn_kind) {
Damiend99b0522013-12-21 18:17:45 +0000233 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100234 return 0;
Damiend99b0522013-12-21 18:17:45 +0000235 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100236 return 1;
237 } else {
Damiend99b0522013-12-21 18:17:45 +0000238 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
239 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100240 return 1;
241 } else {
Damiend99b0522013-12-21 18:17:45 +0000242 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100243 }
244 }
245}
246
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200247STATIC 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)) {
Damiend99b0522013-12-21 18:17:45 +0000248 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
249 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
250 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100251 for (int i = 0; i < num_nodes; i++) {
252 f(comp, pns->nodes[i]);
253 }
Damiend99b0522013-12-21 18:17:45 +0000254 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100255 f(comp, pn);
256 }
257}
258
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200259STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000260 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100261 *nodes = NULL;
262 return 0;
Damiend99b0522013-12-21 18:17:45 +0000263 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100264 *nodes = pn;
265 return 1;
266 } else {
Damiend99b0522013-12-21 18:17:45 +0000267 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
268 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100269 *nodes = pn;
270 return 1;
271 } else {
272 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000273 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100274 }
275 }
276}
277
Damiend99b0522013-12-21 18:17:45 +0000278void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100279}
280
Damiend99b0522013-12-21 18:17:45 +0000281void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
282 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100283 for (int i = 0; i < num_nodes; i++) {
284 compile_node(comp, pns->nodes[i]);
285 }
286}
287
Damien3ef4abb2013-10-12 16:53:13 +0100288#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200289STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000290 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100291 return false;
292 }
Damiend99b0522013-12-21 18:17:45 +0000293 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100294 return false;
295 }
296 return true;
297}
298
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200299STATIC void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
Damien George55baff42014-01-21 21:40:13 +0000300 uint len;
301 const byte *str = qstr_data(qstr, &len);
Damien02f89412013-12-12 15:13:36 +0000302 bool has_single_quote = false;
303 bool has_double_quote = false;
304 for (int i = 0; i < len; i++) {
305 if (str[i] == '\'') {
306 has_single_quote = true;
307 } else if (str[i] == '"') {
308 has_double_quote = true;
309 }
310 }
311 if (bytes) {
312 vstr_printf(vstr, "b");
313 }
314 bool quote_single = false;
315 if (has_single_quote && !has_double_quote) {
316 vstr_printf(vstr, "\"");
317 } else {
318 quote_single = true;
319 vstr_printf(vstr, "'");
320 }
321 for (int i = 0; i < len; i++) {
322 if (str[i] == '\n') {
323 vstr_printf(vstr, "\\n");
324 } else if (str[i] == '\\') {
325 vstr_printf(vstr, "\\\\");
326 } else if (str[i] == '\'' && quote_single) {
327 vstr_printf(vstr, "\\'");
328 } else {
329 vstr_printf(vstr, "%c", str[i]);
330 }
331 }
332 if (has_single_quote && !has_double_quote) {
333 vstr_printf(vstr, "\"");
334 } else {
335 vstr_printf(vstr, "'");
336 }
337}
338
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200339STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damiend99b0522013-12-21 18:17:45 +0000340 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200341 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
342 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
343 return;
344 }
345
Damiend99b0522013-12-21 18:17:45 +0000346 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
347 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
348 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000349 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
350 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
351 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
352 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
353 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100354 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000355 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
356 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
357 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien429d7192013-10-04 19:53:11 +0100358 default: assert(0);
359 }
360 break;
361 default: assert(0);
362 }
363}
364
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200365STATIC 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 +0100366 int n = 0;
367 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000368 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100369 }
370 int total = n;
371 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000372 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100373 total += 1;
Damien3a205172013-10-12 15:01:56 +0100374 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100375 is_const = false;
376 }
377 }
378 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100379 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100380 is_const = false;
381 break;
382 }
383 }
384 if (total > 0 && is_const) {
385 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000386 vstr_t *vstr = vstr_new();
387 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000388 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000389 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100390 need_comma = true;
391 }
392 for (int i = 0; i < n; i++) {
393 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000394 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100395 }
Damien02f89412013-12-12 15:13:36 +0000396 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100397 need_comma = true;
398 }
399 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000400 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100401 } else {
Damien02f89412013-12-12 15:13:36 +0000402 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100403 }
Damien Georgeb9791222014-01-23 00:34:21 +0000404 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000405 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100406 } else {
Damiend99b0522013-12-21 18:17:45 +0000407 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100408 compile_node(comp, pn);
409 }
410 for (int i = 0; i < n; i++) {
411 compile_node(comp, pns_list->nodes[i]);
412 }
Damien Georgeb9791222014-01-23 00:34:21 +0000413 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100414 }
415}
Damien3a205172013-10-12 15:01:56 +0100416#endif
417
418// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damiend99b0522013-12-21 18:17:45 +0000419void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100420#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100421 cpython_c_tuple(comp, pn, pns_list);
422#else
423 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000424 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100425 compile_node(comp, pn);
426 total += 1;
427 }
428 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000429 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100430 for (int i = 0; i < n; i++) {
431 compile_node(comp, pns_list->nodes[i]);
432 }
433 total += n;
434 }
Damien Georgeb9791222014-01-23 00:34:21 +0000435 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100436#endif
437}
Damien429d7192013-10-04 19:53:11 +0100438
Damiend99b0522013-12-21 18:17:45 +0000439void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100440 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000441 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100442}
443
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200444STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000445 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200446 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100447}
448
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200449STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200450 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE) || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 1);
Damien429d7192013-10-04 19:53:11 +0100451}
452
Damien3ef4abb2013-10-12 16:53:13 +0100453#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100454// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200455STATIC 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 +0100456 if (node_is_const_false(pn)) {
457 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000458 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100459 }
460 return;
461 } else if (node_is_const_true(pn)) {
462 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000463 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100464 }
465 return;
Damiend99b0522013-12-21 18:17:45 +0000466 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
467 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
468 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
469 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100470 if (jump_if == false) {
Damienb05d7072013-10-05 13:37:10 +0100471 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100472 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100473 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100474 }
Damien3a205172013-10-12 15:01:56 +0100475 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000476 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100477 } else {
478 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100479 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100480 }
481 }
482 return;
Damiend99b0522013-12-21 18:17:45 +0000483 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100484 if (jump_if == false) {
485 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100486 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100487 }
488 } else {
Damienb05d7072013-10-05 13:37:10 +0100489 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100490 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100491 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100492 }
Damien3a205172013-10-12 15:01:56 +0100493 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000494 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100495 }
496 return;
Damiend99b0522013-12-21 18:17:45 +0000497 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100498 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100499 return;
500 }
501 }
502
503 // nothing special, fall back to default compiling for node and jump
504 compile_node(comp, pn);
505 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000506 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100507 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000508 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100509 }
510}
Damien3a205172013-10-12 15:01:56 +0100511#endif
Damien429d7192013-10-04 19:53:11 +0100512
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200513STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100514#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100515 cpython_c_if_cond(comp, pn, jump_if, label, false);
516#else
517 if (node_is_const_false(pn)) {
518 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000519 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100520 }
521 return;
522 } else if (node_is_const_true(pn)) {
523 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000524 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100525 }
526 return;
Damiend99b0522013-12-21 18:17:45 +0000527 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
528 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
529 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
530 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100531 if (jump_if == false) {
532 int label2 = comp_next_label(comp);
533 for (int i = 0; i < n - 1; i++) {
534 c_if_cond(comp, pns->nodes[i], true, label2);
535 }
536 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000537 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100538 } else {
539 for (int i = 0; i < n; i++) {
540 c_if_cond(comp, pns->nodes[i], true, label);
541 }
542 }
543 return;
Damiend99b0522013-12-21 18:17:45 +0000544 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100545 if (jump_if == false) {
546 for (int i = 0; i < n; i++) {
547 c_if_cond(comp, pns->nodes[i], false, label);
548 }
549 } else {
550 int label2 = comp_next_label(comp);
551 for (int i = 0; i < n - 1; i++) {
552 c_if_cond(comp, pns->nodes[i], false, label2);
553 }
554 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000555 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100556 }
557 return;
Damiend99b0522013-12-21 18:17:45 +0000558 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100559 c_if_cond(comp, pns->nodes[0], !jump_if, label);
560 return;
561 }
562 }
563
564 // nothing special, fall back to default compiling for node and jump
565 compile_node(comp, pn);
566 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000567 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100568 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000569 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100570 }
571#endif
Damien429d7192013-10-04 19:53:11 +0100572}
573
574typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000575void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100576
Damiend99b0522013-12-21 18:17:45 +0000577void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100578 if (assign_kind != ASSIGN_AUG_STORE) {
579 compile_node(comp, pns->nodes[0]);
580 }
581
Damiend99b0522013-12-21 18:17:45 +0000582 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
583 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
584 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
585 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100586 if (assign_kind != ASSIGN_AUG_STORE) {
587 for (int i = 0; i < n - 1; i++) {
588 compile_node(comp, pns1->nodes[i]);
589 }
590 }
Damiend99b0522013-12-21 18:17:45 +0000591 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
592 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100593 }
Damiend99b0522013-12-21 18:17:45 +0000594 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000595 compile_syntax_error(comp, "can't assign to function call");
Damien429d7192013-10-04 19:53:11 +0100596 return;
Damiend99b0522013-12-21 18:17:45 +0000597 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100598 if (assign_kind == ASSIGN_AUG_STORE) {
599 EMIT(rot_three);
600 EMIT(store_subscr);
601 } else {
602 compile_node(comp, pns1->nodes[0]);
603 if (assign_kind == ASSIGN_AUG_LOAD) {
604 EMIT(dup_top_two);
Damien Georged17926d2014-03-30 13:35:08 +0100605 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100606 } else {
607 EMIT(store_subscr);
608 }
609 }
Damiend99b0522013-12-21 18:17:45 +0000610 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
611 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100612 if (assign_kind == ASSIGN_AUG_LOAD) {
613 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000614 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100615 } else {
616 if (assign_kind == ASSIGN_AUG_STORE) {
617 EMIT(rot_two);
618 }
Damien Georgeb9791222014-01-23 00:34:21 +0000619 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100620 }
621 } else {
622 // shouldn't happen
623 assert(0);
624 }
625 } else {
626 // shouldn't happen
627 assert(0);
628 }
629
Damiend99b0522013-12-21 18:17:45 +0000630 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +0100631 // SyntaxError, cannot assign
632 assert(0);
633 }
634}
635
Damiend99b0522013-12-21 18:17:45 +0000636void c_assign_tuple(compiler_t *comp, int n, mp_parse_node_t *nodes) {
Damien429d7192013-10-04 19:53:11 +0100637 assert(n >= 0);
638 int have_star_index = -1;
639 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000640 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100641 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000642 EMIT_ARG(unpack_ex, i, n - i - 1);
Damien429d7192013-10-04 19:53:11 +0100643 have_star_index = i;
644 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000645 compile_syntax_error(comp, "two starred expressions in assignment");
Damien429d7192013-10-04 19:53:11 +0100646 return;
647 }
648 }
649 }
650 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000651 EMIT_ARG(unpack_sequence, n);
Damien429d7192013-10-04 19:53:11 +0100652 }
653 for (int i = 0; i < n; i++) {
654 if (i == have_star_index) {
Damiend99b0522013-12-21 18:17:45 +0000655 c_assign(comp, ((mp_parse_node_struct_t*)nodes[i])->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100656 } else {
657 c_assign(comp, nodes[i], ASSIGN_STORE);
658 }
659 }
660}
661
662// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000663void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100664 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000665 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100666 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000667 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
668 if (MP_PARSE_NODE_IS_ID(pn)) {
669 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100670 switch (assign_kind) {
671 case ASSIGN_STORE:
672 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000673 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100674 break;
675 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000676 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100677 break;
678 }
679 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000680 compile_syntax_error(comp, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100681 return;
682 }
683 } else {
Damiend99b0522013-12-21 18:17:45 +0000684 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
685 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100686 case PN_power:
687 // lhs is an index or attribute
688 c_assign_power(comp, pns, assign_kind);
689 break;
690
691 case PN_testlist_star_expr:
692 case PN_exprlist:
693 // lhs is a tuple
694 if (assign_kind != ASSIGN_STORE) {
695 goto bad_aug;
696 }
Damiend99b0522013-12-21 18:17:45 +0000697 c_assign_tuple(comp, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100698 break;
699
700 case PN_atom_paren:
701 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000702 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100703 // empty tuple
Damien Georgef41fdd02014-03-03 23:19:11 +0000704 compile_syntax_error(comp, "can't assign to ()");
Damien429d7192013-10-04 19:53:11 +0100705 return;
Damiend99b0522013-12-21 18:17:45 +0000706 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
707 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100708 goto testlist_comp;
709 } else {
710 // parenthesis around 1 item, is just that item
711 pn = pns->nodes[0];
712 goto tail_recursion;
713 }
714 break;
715
716 case PN_atom_bracket:
717 // lhs is something in brackets
718 if (assign_kind != ASSIGN_STORE) {
719 goto bad_aug;
720 }
Damiend99b0522013-12-21 18:17:45 +0000721 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100722 // empty list, assignment allowed
723 c_assign_tuple(comp, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000724 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
725 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100726 goto testlist_comp;
727 } else {
728 // brackets around 1 item
729 c_assign_tuple(comp, 1, &pns->nodes[0]);
730 }
731 break;
732
733 default:
Damiend99b0522013-12-21 18:17:45 +0000734 printf("unknown assign, %u\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien429d7192013-10-04 19:53:11 +0100735 assert(0);
736 }
737 return;
738
739 testlist_comp:
740 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000741 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
742 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
743 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100744 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000745 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100746 c_assign_tuple(comp, 1, &pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000747 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100748 // sequence of many items
749 // TODO call c_assign_tuple instead
Damiend99b0522013-12-21 18:17:45 +0000750 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
Damien Georgeb9791222014-01-23 00:34:21 +0000751 EMIT_ARG(unpack_sequence, 1 + n);
Damien429d7192013-10-04 19:53:11 +0100752 c_assign(comp, pns->nodes[0], ASSIGN_STORE);
753 for (int i = 0; i < n; i++) {
754 c_assign(comp, pns2->nodes[i], ASSIGN_STORE);
755 }
Damiend99b0522013-12-21 18:17:45 +0000756 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +0100757 // TODO not implemented
758 assert(0);
759 } else {
760 // sequence with 2 items
761 goto sequence_with_2_items;
762 }
763 } else {
764 // sequence with 2 items
765 sequence_with_2_items:
766 c_assign_tuple(comp, 2, pns->nodes);
767 }
768 return;
769 }
770 return;
771
772 bad_aug:
Damien Georgef41fdd02014-03-03 23:19:11 +0000773 compile_syntax_error(comp, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100774}
775
776// stuff for lambda and comprehensions and generators
Damien George30565092014-03-31 11:30:17 +0100777void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
778 assert(n_pos_defaults >= 0);
779 assert(n_kw_defaults >= 0);
780
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000781#if !MICROPY_EMIT_CPYTHON
782 // in Micro Python we put the default params into a tuple using the bytecode
Damien George30565092014-03-31 11:30:17 +0100783 if (n_pos_defaults) {
784 EMIT_ARG(build_tuple, n_pos_defaults);
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200785 }
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000786#endif
787
Damien429d7192013-10-04 19:53:11 +0100788 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000789 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100790 int nfree = 0;
791 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000792 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
793 id_info_t *id = &comp->scope_cur->id_info[i];
794 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
795 for (int j = 0; j < this_scope->id_info_len; j++) {
796 id_info_t *id2 = &this_scope->id_info[j];
797 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000798#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000799 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000800#else
801 // in Micro Python we load closures using LOAD_FAST
Damien Georgeb9791222014-01-23 00:34:21 +0000802 EMIT_ARG(load_fast, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000803#endif
Damien318aec62013-12-10 18:28:17 +0000804 nfree += 1;
805 }
806 }
Damien429d7192013-10-04 19:53:11 +0100807 }
808 }
809 }
Damien429d7192013-10-04 19:53:11 +0100810
811 // make the function/closure
812 if (nfree == 0) {
Damien George30565092014-03-31 11:30:17 +0100813 EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100814 } else {
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000815 EMIT_ARG(build_tuple, nfree);
Damien George30565092014-03-31 11:30:17 +0100816 EMIT_ARG(make_closure, this_scope, n_pos_defaults, n_kw_defaults);
Damien429d7192013-10-04 19:53:11 +0100817 }
818}
819
Damiend99b0522013-12-21 18:17:45 +0000820void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000821 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damiend99b0522013-12-21 18:17:45 +0000822 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
823 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100824 // bare star
825 comp->have_bare_star = true;
826 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000827
828 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
829 // TODO do we need to do anything with this?
830
831 } else {
832 mp_parse_node_t pn_id;
833 mp_parse_node_t pn_colon;
834 mp_parse_node_t pn_equal;
835 if (MP_PARSE_NODE_IS_ID(pn)) {
836 // this parameter is just an id
837
838 pn_id = pn;
839 pn_colon = MP_PARSE_NODE_NULL;
840 pn_equal = MP_PARSE_NODE_NULL;
841
842 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
843 // this parameter has a colon and/or equal specifier
844
845 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
846 pn_id = pns->nodes[0];
847 pn_colon = pns->nodes[1];
848 pn_equal = pns->nodes[2];
849
850 } else {
851 assert(0);
852 return;
853 }
854
855 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
856 // this parameter does not have a default value
857
858 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
859 if (!comp->have_bare_star && comp->param_pass_num_default_params != 0) {
860 compile_syntax_error(comp, "non-default argument follows default argument");
861 return;
862 }
863
864 } else {
865 // this parameter has a default value
866 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
867
868 if (comp->have_bare_star) {
869 comp->param_pass_num_dict_params += 1;
870 if (comp->param_pass == 1) {
871 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pn_id));
872 compile_node(comp, pn_equal);
873 }
874 } else {
875 comp->param_pass_num_default_params += 1;
876 if (comp->param_pass == 2) {
877 compile_node(comp, pn_equal);
878 }
879 }
880 }
881
882 // TODO pn_colon not implemented
883 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100884 }
885}
886
887// leaves function object on stack
888// returns function name
Damiend99b0522013-12-21 18:17:45 +0000889qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100890 if (comp->pass == PASS_1) {
891 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000892 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100893 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000894 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100895 }
896
897 // save variables (probably don't need to do this, since we can't have nested definitions..?)
898 bool old_have_bare_star = comp->have_bare_star;
899 int old_param_pass = comp->param_pass;
900 int old_param_pass_num_dict_params = comp->param_pass_num_dict_params;
901 int old_param_pass_num_default_params = comp->param_pass_num_default_params;
902
903 // compile default parameters
Damien Georgef41fdd02014-03-03 23:19:11 +0000904
905 // pass 1 does any default parameters after bare star
Damien429d7192013-10-04 19:53:11 +0100906 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000907 comp->param_pass = 1;
Damien429d7192013-10-04 19:53:11 +0100908 comp->param_pass_num_dict_params = 0;
909 comp->param_pass_num_default_params = 0;
910 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +0000911
912 if (comp->had_error) {
913 return MP_QSTR_NULL;
914 }
915
916 // pass 2 does any default parameters before bare star
Damien429d7192013-10-04 19:53:11 +0100917 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000918 comp->param_pass = 2;
Damien429d7192013-10-04 19:53:11 +0100919 comp->param_pass_num_dict_params = 0;
920 comp->param_pass_num_default_params = 0;
921 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
922
923 // get the scope for this function
924 scope_t *fscope = (scope_t*)pns->nodes[4];
925
926 // make the function
Damien George30565092014-03-31 11:30:17 +0100927 close_over_variables_etc(comp, fscope, comp->param_pass_num_default_params, comp->param_pass_num_dict_params);
Damien429d7192013-10-04 19:53:11 +0100928
929 // restore variables
930 comp->have_bare_star = old_have_bare_star;
931 comp->param_pass = old_param_pass;
932 comp->param_pass_num_dict_params = old_param_pass_num_dict_params;
933 comp->param_pass_num_default_params = old_param_pass_num_default_params;
934
935 // return its name (the 'f' in "def f(...):")
936 return fscope->simple_name;
937}
938
939// leaves class object on stack
940// returns class name
Damiend99b0522013-12-21 18:17:45 +0000941qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100942 if (comp->pass == PASS_1) {
943 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000944 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100945 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000946 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100947 }
948
949 EMIT(load_build_class);
950
951 // scope for this class
952 scope_t *cscope = (scope_t*)pns->nodes[3];
953
954 // compile the class
955 close_over_variables_etc(comp, cscope, 0, 0);
956
957 // get its name
Damien Georgeb9791222014-01-23 00:34:21 +0000958 EMIT_ARG(load_const_id, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100959
960 // nodes[1] has parent classes, if any
Damien George804760b2014-03-30 23:06:37 +0100961 // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
962 mp_parse_node_t parents = pns->nodes[1];
963 if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
964 parents = MP_PARSE_NODE_NULL;
965 }
Damien Georgebbcd49a2014-02-06 20:30:16 +0000966 comp->func_arg_is_super = false;
Damien George804760b2014-03-30 23:06:37 +0100967 compile_trailer_paren_helper(comp, parents, false, 2);
Damien429d7192013-10-04 19:53:11 +0100968
969 // return its name (the 'C' in class C(...):")
970 return cscope->simple_name;
971}
972
Damien6cdd3af2013-10-05 18:08:26 +0100973// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200974STATIC 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 +0000975 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100976 return false;
977 }
978
979 if (name_len != 2) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000980 compile_syntax_error(comp, "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100981 return true;
982 }
983
Damiend99b0522013-12-21 18:17:45 +0000984 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000985 if (attr == MP_QSTR_byte_code) {
Damien5ac1b2e2013-10-18 19:58:12 +0100986 *emit_options = EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +0100987#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000988 } else if (attr == MP_QSTR_native) {
Damien6cdd3af2013-10-05 18:08:26 +0100989 *emit_options = EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000990 } else if (attr == MP_QSTR_viper) {
Damien7af3d192013-10-07 00:02:49 +0100991 *emit_options = EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100992#endif
Damien3ef4abb2013-10-12 16:53:13 +0100993#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000994 } else if (attr == MP_QSTR_asm_thumb) {
Damien5bfb7592013-10-05 18:41:24 +0100995 *emit_options = EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +0100996#endif
Damien6cdd3af2013-10-05 18:08:26 +0100997 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000998 compile_syntax_error(comp, "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100999 }
1000
1001 return true;
1002}
1003
Damiend99b0522013-12-21 18:17:45 +00001004void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001005 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +00001006 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001007 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1008
Damien6cdd3af2013-10-05 18:08:26 +01001009 // inherit emit options for this function/class definition
1010 uint emit_options = comp->scope_cur->emit_options;
1011
1012 // compile each decorator
1013 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001014 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001015 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1016 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001017
1018 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001019 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001020 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1021
1022 // check for built-in decorators
1023 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1024 // this was a built-in
1025 num_built_in_decorators += 1;
1026
1027 } else {
1028 // not a built-in, compile normally
1029
1030 // compile the decorator function
1031 compile_node(comp, name_nodes[0]);
1032 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001033 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001034 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001035 }
1036
1037 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001038 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001039 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001040 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001041 compile_node(comp, pns_decorator->nodes[1]);
1042 }
Damien429d7192013-10-04 19:53:11 +01001043 }
1044 }
1045
1046 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001047 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001048 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001049 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001050 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001051 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001052 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001053 } else {
1054 // shouldn't happen
1055 assert(0);
1056 }
1057
1058 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001059 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001060 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01001061 }
1062
1063 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001064 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001065}
1066
Damiend99b0522013-12-21 18:17:45 +00001067void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001068 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001069 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001070 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001071}
1072
Damiend99b0522013-12-21 18:17:45 +00001073void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1074 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001075 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001076 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1077 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001078
1079 compile_node(comp, pns->nodes[0]); // base of the power node
1080
Damiend99b0522013-12-21 18:17:45 +00001081 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1082 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1083 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1084 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001085 for (int i = 0; i < n - 1; i++) {
1086 compile_node(comp, pns1->nodes[i]);
1087 }
Damiend99b0522013-12-21 18:17:45 +00001088 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1089 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001090 }
Damiend99b0522013-12-21 18:17:45 +00001091 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien429d7192013-10-04 19:53:11 +01001092 // SyntaxError: can't delete a function call
1093 assert(0);
Damiend99b0522013-12-21 18:17:45 +00001094 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001095 compile_node(comp, pns1->nodes[0]);
1096 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001097 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1098 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001099 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001100 } else {
1101 // shouldn't happen
1102 assert(0);
1103 }
1104 } else {
1105 // shouldn't happen
1106 assert(0);
1107 }
1108
Damiend99b0522013-12-21 18:17:45 +00001109 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001110 // SyntaxError, cannot delete
1111 assert(0);
1112 }
Damiend99b0522013-12-21 18:17:45 +00001113 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1114 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1115 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1116 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001117 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1118
Damiend99b0522013-12-21 18:17:45 +00001119 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1120 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1121 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001122 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001123 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001124 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001125 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001126 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001127 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001128 c_del_stmt(comp, pns->nodes[0]);
1129 for (int i = 0; i < n; i++) {
1130 c_del_stmt(comp, pns1->nodes[i]);
1131 }
Damiend99b0522013-12-21 18:17:45 +00001132 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001133 // TODO not implemented; can't del comprehension?
1134 assert(0);
1135 } else {
1136 // sequence with 2 items
1137 goto sequence_with_2_items;
1138 }
1139 } else {
1140 // sequence with 2 items
1141 sequence_with_2_items:
1142 c_del_stmt(comp, pns->nodes[0]);
1143 c_del_stmt(comp, pns->nodes[1]);
1144 }
1145 } else {
1146 // tuple with 1 element
1147 c_del_stmt(comp, pn);
1148 }
1149 } else {
1150 // not implemented
1151 assert(0);
1152 }
1153}
1154
Damiend99b0522013-12-21 18:17:45 +00001155void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001156 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1157}
1158
Damiend99b0522013-12-21 18:17:45 +00001159void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001160 if (comp->break_label == 0) {
Damien George2326d522014-03-27 23:26:35 +00001161 compile_syntax_error(comp, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001162 }
Damien Georgecbddb272014-02-01 20:08:18 +00001163 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001164}
1165
Damiend99b0522013-12-21 18:17:45 +00001166void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001167 if (comp->continue_label == 0) {
Damien George2326d522014-03-27 23:26:35 +00001168 compile_syntax_error(comp, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001169 }
Damien Georgecbddb272014-02-01 20:08:18 +00001170 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001171}
1172
Damiend99b0522013-12-21 18:17:45 +00001173void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001174 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001175 compile_syntax_error(comp, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001176 return;
1177 }
Damiend99b0522013-12-21 18:17:45 +00001178 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001179 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001180 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001181 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001182 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001183 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1184 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 +01001185
Damienb05d7072013-10-05 13:37:10 +01001186 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001187 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1188 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1189 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001190 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001191 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1192 } else {
1193 compile_node(comp, pns->nodes[0]);
1194 }
1195 EMIT(return_value);
1196}
1197
Damiend99b0522013-12-21 18:17:45 +00001198void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001199 compile_node(comp, pns->nodes[0]);
1200 EMIT(pop_top);
1201}
1202
Damiend99b0522013-12-21 18:17:45 +00001203void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1204 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001205 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001206 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001207 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001208 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001209 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001210 compile_node(comp, pns->nodes[0]);
1211 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001212 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001213 } else {
1214 // raise x
1215 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001216 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001217 }
1218}
1219
1220// q1 holds the base, q2 the full name
1221// eg a -> q1=q2=a
1222// a.b.c -> q1=a, q2=a.b.c
Damiend99b0522013-12-21 18:17:45 +00001223void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q1, qstr *q2) {
Damien429d7192013-10-04 19:53:11 +01001224 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001225 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1226 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001227 // a name of the form x as y; unwrap it
Damiend99b0522013-12-21 18:17:45 +00001228 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001229 pn = pns->nodes[0];
1230 is_as = true;
1231 }
Damiend99b0522013-12-21 18:17:45 +00001232 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001233 // just a simple name
Damiend99b0522013-12-21 18:17:45 +00001234 *q2 = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001235 if (!is_as) {
1236 *q1 = *q2;
1237 }
Damien Georgeb9791222014-01-23 00:34:21 +00001238 EMIT_ARG(import_name, *q2);
Damiend99b0522013-12-21 18:17:45 +00001239 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1240 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1241 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001242 // a name of the form a.b.c
1243 if (!is_as) {
Damiend99b0522013-12-21 18:17:45 +00001244 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001245 }
Damiend99b0522013-12-21 18:17:45 +00001246 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001247 int len = n - 1;
1248 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001249 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001250 }
Damien George55baff42014-01-21 21:40:13 +00001251 byte *q_ptr;
1252 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001253 for (int i = 0; i < n; i++) {
1254 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001255 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001256 }
Damien George55baff42014-01-21 21:40:13 +00001257 uint str_src_len;
1258 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001259 memcpy(str_dest, str_src, str_src_len);
1260 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001261 }
Damien George55baff42014-01-21 21:40:13 +00001262 *q2 = qstr_build_end(q_ptr);
Damien Georgeb9791222014-01-23 00:34:21 +00001263 EMIT_ARG(import_name, *q2);
Damien429d7192013-10-04 19:53:11 +01001264 if (is_as) {
1265 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001266 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001267 }
1268 }
1269 } else {
1270 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001271 // This covers relative imports starting with dot(s) like "from .foo import"
Paul Sokolovsky8d9cc2e2014-03-30 02:31:08 +02001272 compile_syntax_error(comp, "Relative imports not implemented");
Damien429d7192013-10-04 19:53:11 +01001273 assert(0);
1274 }
1275 } else {
1276 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001277 // This covers relative imports with dots only like "from .. import"
Paul Sokolovsky8d9cc2e2014-03-30 02:31:08 +02001278 compile_syntax_error(comp, "Relative imports not implemented");
Damien429d7192013-10-04 19:53:11 +01001279 assert(0);
1280 }
1281}
1282
Damiend99b0522013-12-21 18:17:45 +00001283void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgeb9791222014-01-23 00:34:21 +00001284 EMIT_ARG(load_const_small_int, 0); // ??
1285 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001286 qstr q1, q2;
1287 do_import_name(comp, pn, &q1, &q2);
Damien Georgeb9791222014-01-23 00:34:21 +00001288 EMIT_ARG(store_id, q1);
Damien429d7192013-10-04 19:53:11 +01001289}
1290
Damiend99b0522013-12-21 18:17:45 +00001291void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001292 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1293}
1294
Damiend99b0522013-12-21 18:17:45 +00001295void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
1296 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001297 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001298
1299 // build the "fromlist" tuple
1300#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001301 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001302#else
Damien Georgeb9791222014-01-23 00:34:21 +00001303 EMIT_ARG(load_const_str, QSTR_FROM_STR_STATIC("*"), false);
1304 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001305#endif
1306
1307 // do the import
Damien429d7192013-10-04 19:53:11 +01001308 qstr dummy_q, id1;
1309 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1310 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001311
Damien429d7192013-10-04 19:53:11 +01001312 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001313 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001314
1315 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001316 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001317 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001318#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001319 {
1320 vstr_t *vstr = vstr_new();
1321 vstr_printf(vstr, "(");
1322 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001323 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1324 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1325 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001326 if (i > 0) {
1327 vstr_printf(vstr, ", ");
1328 }
1329 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001330 uint len;
1331 const byte *str = qstr_data(id2, &len);
1332 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001333 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001334 }
Damien02f89412013-12-12 15:13:36 +00001335 if (n == 1) {
1336 vstr_printf(vstr, ",");
1337 }
1338 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001339 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001340 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001341 }
Damiendb4c3612013-12-10 17:27:24 +00001342#else
1343 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001344 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1345 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1346 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001347 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001348 }
Damien Georgeb9791222014-01-23 00:34:21 +00001349 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001350#endif
1351
1352 // do the import
Damien429d7192013-10-04 19:53:11 +01001353 qstr dummy_q, id1;
1354 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1355 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001356 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1357 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1358 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001359 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001360 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001361 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001362 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001363 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001364 }
1365 }
1366 EMIT(pop_top);
1367 }
1368}
1369
Damiend99b0522013-12-21 18:17:45 +00001370void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001371 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001372 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1373 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001374 } else {
Damiend99b0522013-12-21 18:17:45 +00001375 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1376 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001377 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001378 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001379 }
Damien429d7192013-10-04 19:53:11 +01001380 }
1381 }
1382}
1383
Damiend99b0522013-12-21 18:17:45 +00001384void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001385 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001386 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1387 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001388 } else {
Damiend99b0522013-12-21 18:17:45 +00001389 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1390 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001391 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001392 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001393 }
Damien429d7192013-10-04 19:53:11 +01001394 }
1395 }
1396}
1397
Damiend99b0522013-12-21 18:17:45 +00001398void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001399 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001400 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001401 EMIT_ARG(load_global, MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
Damiend99b0522013-12-21 18:17:45 +00001402 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001403 // assertion message
1404 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001405 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01001406 }
Damien Georgeb9791222014-01-23 00:34:21 +00001407 EMIT_ARG(raise_varargs, 1);
1408 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001409}
1410
Damiend99b0522013-12-21 18:17:45 +00001411void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001412 // TODO proper and/or short circuiting
1413
Damienb05d7072013-10-05 13:37:10 +01001414 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001415
Damienb05d7072013-10-05 13:37:10 +01001416 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001417 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1418
1419 compile_node(comp, pns->nodes[1]); // if block
Damiend99b0522013-12-21 18:17:45 +00001420 //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 +01001421 // jump over elif/else blocks if they exist
Damien415eb6f2013-10-05 12:19:06 +01001422 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001423 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001424 }
1425 //}
Damien Georgeb9791222014-01-23 00:34:21 +00001426 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001427
Damiend99b0522013-12-21 18:17:45 +00001428 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001429 // compile elif blocks
1430
Damiend99b0522013-12-21 18:17:45 +00001431 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001432
Damiend99b0522013-12-21 18:17:45 +00001433 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001434 // multiple elif blocks
1435
Damiend99b0522013-12-21 18:17:45 +00001436 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001437 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001438 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001439 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001440 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1441
1442 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001443 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001444 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001445 }
Damien Georgeb9791222014-01-23 00:34:21 +00001446 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001447 }
1448
1449 } else {
1450 // a single elif block
1451
Damienb05d7072013-10-05 13:37:10 +01001452 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001453 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1454
1455 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001456 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001457 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001458 }
Damien Georgeb9791222014-01-23 00:34:21 +00001459 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001460 }
1461 }
1462
1463 // compile else block
1464 compile_node(comp, pns->nodes[3]); // can be null
1465
Damien Georgeb9791222014-01-23 00:34:21 +00001466 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001467}
1468
Damien Georgecbddb272014-02-01 20:08:18 +00001469#define START_BREAK_CONTINUE_BLOCK \
1470 int old_break_label = comp->break_label; \
1471 int old_continue_label = comp->continue_label; \
1472 int break_label = comp_next_label(comp); \
1473 int continue_label = comp_next_label(comp); \
1474 comp->break_label = break_label; \
1475 comp->continue_label = continue_label; \
1476 comp->break_continue_except_level = comp->cur_except_level;
1477
1478#define END_BREAK_CONTINUE_BLOCK \
1479 comp->break_label = old_break_label; \
1480 comp->continue_label = old_continue_label; \
1481 comp->break_continue_except_level = comp->cur_except_level;
1482
Damiend99b0522013-12-21 18:17:45 +00001483void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001484 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001485
Damience89a212013-10-15 22:25:17 +01001486 // compared to CPython, we have an optimised version of while loops
1487#if MICROPY_EMIT_CPYTHON
1488 int done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001489 EMIT_ARG(setup_loop, break_label);
1490 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001491 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1492 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001493 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001494 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001495 }
Damien Georgeb9791222014-01-23 00:34:21 +00001496 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001497 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1498 // this is a small hack to agree with CPython
1499 if (!node_is_const_true(pns->nodes[0])) {
1500 EMIT(pop_block);
1501 }
Damience89a212013-10-15 22:25:17 +01001502#else
1503 int top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001504 EMIT_ARG(jump, continue_label);
1505 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001506 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001507 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001508 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1509#endif
1510
1511 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001512 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001513
1514 compile_node(comp, pns->nodes[2]); // else
1515
Damien Georgeb9791222014-01-23 00:34:21 +00001516 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001517}
1518
Damienf72fd0e2013-11-06 20:20:49 +00001519// TODO preload end and step onto stack if they are not constants
1520// TODO check if step is negative and do opposite test
Damiend99b0522013-12-21 18:17:45 +00001521void compile_for_stmt_optimised_range(compiler_t *comp, mp_parse_node_t pn_var, mp_parse_node_t pn_start, mp_parse_node_t pn_end, mp_parse_node_t pn_step, mp_parse_node_t pn_body, mp_parse_node_t pn_else) {
Damien Georgecbddb272014-02-01 20:08:18 +00001522 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001523
1524 int top_label = comp_next_label(comp);
Damien George600ae732014-01-21 23:48:04 +00001525 int entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001526
1527 // compile: var = start
1528 compile_node(comp, pn_start);
1529 c_assign(comp, pn_var, ASSIGN_STORE);
1530
Damien Georgeb9791222014-01-23 00:34:21 +00001531 EMIT_ARG(jump, entry_label);
1532 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001533
Damienf3822fc2013-11-09 20:12:03 +00001534 // compile body
1535 compile_node(comp, pn_body);
1536
Damien Georgeb9791222014-01-23 00:34:21 +00001537 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001538
Damienf72fd0e2013-11-06 20:20:49 +00001539 // compile: var += step
1540 c_assign(comp, pn_var, ASSIGN_AUG_LOAD);
1541 compile_node(comp, pn_step);
Damien Georged17926d2014-03-30 13:35:08 +01001542 EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001543 c_assign(comp, pn_var, ASSIGN_AUG_STORE);
1544
Damien Georgeb9791222014-01-23 00:34:21 +00001545 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001546
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001547 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001548 compile_node(comp, pn_var);
1549 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001550 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1551 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien Georged17926d2014-03-30 13:35:08 +01001552 EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001553 } else {
Damien Georged17926d2014-03-30 13:35:08 +01001554 EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001555 }
Damien Georgeb9791222014-01-23 00:34:21 +00001556 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001557
1558 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001559 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001560
1561 compile_node(comp, pn_else);
1562
Damien Georgeb9791222014-01-23 00:34:21 +00001563 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001564}
1565
Damiend99b0522013-12-21 18:17:45 +00001566void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001567#if !MICROPY_EMIT_CPYTHON
1568 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1569 // this is actually slower, but uses no heap memory
1570 // for viper it will be much, much faster
Damiend99b0522013-12-21 18:17:45 +00001571 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)) {
1572 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001573 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1574 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1575 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1576 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001577 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1578 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001579 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001580 mp_parse_node_t pn_range_start;
1581 mp_parse_node_t pn_range_end;
1582 mp_parse_node_t pn_range_step;
1583 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001584 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001585 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001586 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001587 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001588 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001589 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001590 } else if (n_args == 2) {
1591 pn_range_start = args[0];
1592 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001593 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001594 } else {
1595 pn_range_start = args[0];
1596 pn_range_end = args[1];
1597 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001598 // We need to know sign of step. This is possible only if it's constant
1599 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1600 optimize = false;
1601 }
Damienf72fd0e2013-11-06 20:20:49 +00001602 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001603 }
1604 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001605 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1606 return;
1607 }
1608 }
1609 }
1610#endif
1611
Damien Georgecbddb272014-02-01 20:08:18 +00001612 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001613
Damienb05d7072013-10-05 13:37:10 +01001614 int pop_label = comp_next_label(comp);
1615 int end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001616
Damience89a212013-10-15 22:25:17 +01001617 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1618#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001619 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001620#endif
1621
Damien429d7192013-10-04 19:53:11 +01001622 compile_node(comp, pns->nodes[1]); // iterator
1623 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001624 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001625 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001626 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1627 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001628 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001629 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001630 }
Damien Georgeb9791222014-01-23 00:34:21 +00001631 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001632 EMIT(for_iter_end);
1633
1634 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001635 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001636
Damience89a212013-10-15 22:25:17 +01001637#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001638 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001639#endif
Damien429d7192013-10-04 19:53:11 +01001640
1641 compile_node(comp, pns->nodes[3]); // else (not tested)
1642
Damien Georgeb9791222014-01-23 00:34:21 +00001643 EMIT_ARG(label_assign, break_label);
1644 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001645}
1646
Damiend99b0522013-12-21 18:17:45 +00001647void 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 +01001648 // this function is a bit of a hack at the moment
1649 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1650
1651 // setup code
1652 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001653 int l1 = comp_next_label(comp);
1654 int success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001655
Damien Georgeb9791222014-01-23 00:34:21 +00001656 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001657 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001658
Damien429d7192013-10-04 19:53:11 +01001659 compile_node(comp, pn_body); // body
1660 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001661 EMIT_ARG(jump, success_label);
1662 EMIT_ARG(label_assign, l1);
Damienb05d7072013-10-05 13:37:10 +01001663 int l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001664
1665 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001666 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1667 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001668
1669 qstr qstr_exception_local = 0;
Damienb05d7072013-10-05 13:37:10 +01001670 int end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001671
Damiend99b0522013-12-21 18:17:45 +00001672 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001673 // this is a catch all exception handler
1674 if (i + 1 != n_except) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001675 compile_syntax_error(comp, "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001676 return;
1677 }
1678 } else {
1679 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001680 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1681 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1682 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1683 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001684 // handler binds the exception to a local
1685 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001686 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001687 }
1688 }
1689 EMIT(dup_top);
1690 compile_node(comp, pns_exception_expr);
Damien Georged17926d2014-03-30 13:35:08 +01001691 EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001692 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001693 }
1694
1695 EMIT(pop_top);
1696
1697 if (qstr_exception_local == 0) {
1698 EMIT(pop_top);
1699 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001700 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001701 }
1702
1703 EMIT(pop_top);
1704
Damiene2880aa2013-12-20 14:22:59 +00001705 int l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001706 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001707 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001708 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001709 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001710 }
1711 compile_node(comp, pns_except->nodes[1]);
1712 if (qstr_exception_local != 0) {
1713 EMIT(pop_block);
1714 }
1715 EMIT(pop_except);
1716 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001717 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1718 EMIT_ARG(label_assign, l3);
1719 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1720 EMIT_ARG(store_id, qstr_exception_local);
1721 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001722
Damien George8dcc0c72014-03-27 10:55:21 +00001723 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001724 EMIT(end_finally);
1725 }
Damien Georgeb9791222014-01-23 00:34:21 +00001726 EMIT_ARG(jump, l2);
1727 EMIT_ARG(label_assign, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001728 }
1729
Damien George8dcc0c72014-03-27 10:55:21 +00001730 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001731 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001732
Damien Georgeb9791222014-01-23 00:34:21 +00001733 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001734 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001735 EMIT_ARG(label_assign, l2);
1736 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001737}
1738
Damiend99b0522013-12-21 18:17:45 +00001739void 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 +01001740 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1741 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001742 int l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001743
Damien Georgeb9791222014-01-23 00:34:21 +00001744 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001745 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001746
Damien429d7192013-10-04 19:53:11 +01001747 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001748 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien429d7192013-10-04 19:53:11 +01001749 compile_node(comp, pn_body);
1750 } else {
1751 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1752 }
1753 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001754 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1755 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001756 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001757
Damien George8dcc0c72014-03-27 10:55:21 +00001758 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001759 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001760
Damien Georgeb9791222014-01-23 00:34:21 +00001761 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001762}
1763
Damiend99b0522013-12-21 18:17:45 +00001764void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1765 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1766 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1767 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001768 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001769 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1770 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001771 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001772 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001773 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001774 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001775 // no finally
1776 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1777 } else {
1778 // have finally
Damiend99b0522013-12-21 18:17:45 +00001779 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 +01001780 }
1781 } else {
1782 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001783 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001784 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001785 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001786 }
1787 } else {
1788 // shouldn't happen
1789 assert(0);
1790 }
1791}
1792
Damiend99b0522013-12-21 18:17:45 +00001793void 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 +01001794 if (n == 0) {
1795 // no more pre-bits, compile the body of the with
1796 compile_node(comp, body);
1797 } else {
Damienb05d7072013-10-05 13:37:10 +01001798 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001799 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001800 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001801 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001802 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001803 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001804 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1805 } else {
1806 // this pre-bit is just an expression
1807 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001808 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001809 EMIT(pop_top);
1810 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001811 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001812 // compile additional pre-bits and the body
1813 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1814 // finish this with block
1815 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001816 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1817 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001818 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001819 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001820 EMIT(end_finally);
1821 }
1822}
1823
Damiend99b0522013-12-21 18:17:45 +00001824void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001825 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001826 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001827 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1828 assert(n > 0);
1829
1830 // compile in a nested fashion
1831 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1832}
1833
Damiend99b0522013-12-21 18:17:45 +00001834void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1835 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001836 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1837 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001838 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001839 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001840 EMIT_ARG(call_function, 1, 0, false, false);
Damien5ac1b2e2013-10-18 19:58:12 +01001841 EMIT(pop_top);
1842
Damien429d7192013-10-04 19:53:11 +01001843 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001844 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001845 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001846 // do nothing with a lonely constant
1847 } else {
1848 compile_node(comp, pns->nodes[0]); // just an expression
1849 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1850 }
Damien429d7192013-10-04 19:53:11 +01001851 }
1852 } else {
Damiend99b0522013-12-21 18:17:45 +00001853 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1854 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001855 if (kind == PN_expr_stmt_augassign) {
1856 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1857 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001858 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien Georged17926d2014-03-30 13:35:08 +01001859 mp_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001860 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01001861 case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
1862 case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
1863 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
1864 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
1865 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
1866 case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
1867 case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
1868 case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
1869 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1870 case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1871 case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
1872 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
1873 default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01001874 }
Damien George7e5fb242014-02-01 22:18:47 +00001875 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001876 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1877 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001878 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1879 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001880 // following CPython, we store left-most first
1881 if (rhs > 0) {
1882 EMIT(dup_top);
1883 }
1884 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1885 for (int i = 0; i < rhs; i++) {
1886 if (i + 1 < rhs) {
1887 EMIT(dup_top);
1888 }
Damiend99b0522013-12-21 18:17:45 +00001889 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001890 }
1891 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001892 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1893 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1894 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1895 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01001896 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001897 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1898 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001899 compile_node(comp, pns10->nodes[0]); // rhs
1900 compile_node(comp, pns10->nodes[1]); // rhs
1901 EMIT(rot_two);
1902 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1903 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00001904 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1905 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1906 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1907 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01001908 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001909 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1910 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001911 compile_node(comp, pns10->nodes[0]); // rhs
1912 compile_node(comp, pns10->nodes[1]); // rhs
1913 compile_node(comp, pns10->nodes[2]); // rhs
1914 EMIT(rot_three);
1915 EMIT(rot_two);
1916 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1917 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1918 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1919 } else {
1920 compile_node(comp, pns1->nodes[0]); // rhs
1921 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1922 }
1923 } else {
1924 // shouldn't happen
1925 assert(0);
1926 }
1927 }
1928}
1929
Damien Georged17926d2014-03-30 13:35:08 +01001930void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
Damiend99b0522013-12-21 18:17:45 +00001931 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001932 compile_node(comp, pns->nodes[0]);
1933 for (int i = 1; i < num_nodes; i += 1) {
1934 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001935 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001936 }
1937}
1938
Damiend99b0522013-12-21 18:17:45 +00001939void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1940 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1941 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001942
1943 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001944 int l_fail = comp_next_label(comp);
1945 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001946 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1947 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001948 EMIT_ARG(jump, l_end);
1949 EMIT_ARG(label_assign, l_fail);
1950 EMIT_ARG(set_stack_size, stack_size); // force stack size reset
Damien429d7192013-10-04 19:53:11 +01001951 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001952 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001953}
1954
Damiend99b0522013-12-21 18:17:45 +00001955void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001956 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00001957 //mp_parse_node_t pn_params = pns->nodes[0];
1958 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001959
1960 if (comp->pass == PASS_1) {
1961 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00001962 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 +01001963 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001964 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001965 }
1966
1967 // get the scope for this lambda
1968 scope_t *this_scope = (scope_t*)pns->nodes[2];
1969
1970 // make the lambda
1971 close_over_variables_etc(comp, this_scope, 0, 0);
1972}
1973
Damiend99b0522013-12-21 18:17:45 +00001974void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001975 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001976 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001977 for (int i = 0; i < n; i += 1) {
1978 compile_node(comp, pns->nodes[i]);
1979 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00001980 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01001981 }
1982 }
Damien Georgeb9791222014-01-23 00:34:21 +00001983 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001984}
1985
Damiend99b0522013-12-21 18:17:45 +00001986void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001987 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001988 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001989 for (int i = 0; i < n; i += 1) {
1990 compile_node(comp, pns->nodes[i]);
1991 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00001992 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01001993 }
1994 }
Damien Georgeb9791222014-01-23 00:34:21 +00001995 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001996}
1997
Damiend99b0522013-12-21 18:17:45 +00001998void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001999 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002000 EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01002001}
2002
Damiend99b0522013-12-21 18:17:45 +00002003void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002004 int stack_size = EMIT(get_stack_size);
Damiend99b0522013-12-21 18:17:45 +00002005 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002006 compile_node(comp, pns->nodes[0]);
2007 bool multi = (num_nodes > 3);
2008 int l_fail = 0;
2009 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002010 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002011 }
2012 for (int i = 1; i + 1 < num_nodes; i += 2) {
2013 compile_node(comp, pns->nodes[i + 1]);
2014 if (i + 2 < num_nodes) {
2015 EMIT(dup_top);
2016 EMIT(rot_three);
2017 }
Damien George7e5fb242014-02-01 22:18:47 +00002018 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002019 mp_binary_op_t op;
Damien George7e5fb242014-02-01 22:18:47 +00002020 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien Georged17926d2014-03-30 13:35:08 +01002021 case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
2022 case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
2023 case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
2024 case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
2025 case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
2026 case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
2027 case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
2028 default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002029 }
2030 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002031 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2032 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2033 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002034 if (kind == PN_comp_op_not_in) {
Damien Georged17926d2014-03-30 13:35:08 +01002035 EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002036 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002037 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien Georged17926d2014-03-30 13:35:08 +01002038 EMIT_ARG(binary_op, MP_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002039 } else {
Damien Georged17926d2014-03-30 13:35:08 +01002040 EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002041 }
2042 } else {
2043 // shouldn't happen
2044 assert(0);
2045 }
2046 } else {
2047 // shouldn't happen
2048 assert(0);
2049 }
2050 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002051 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002052 }
2053 }
2054 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002055 int l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002056 EMIT_ARG(jump, l_end);
2057 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01002058 EMIT(rot_two);
2059 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002060 EMIT_ARG(label_assign, l_end);
2061 EMIT_ARG(set_stack_size, stack_size + 1); // force stack size
Damien429d7192013-10-04 19:53:11 +01002062 }
2063}
2064
Damiend99b0522013-12-21 18:17:45 +00002065void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002066 // TODO
2067 assert(0);
2068 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002069 //EMIT_ARG(unary_op, "UNARY_STAR");
Damien429d7192013-10-04 19:53:11 +01002070}
2071
Damiend99b0522013-12-21 18:17:45 +00002072void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002073 c_binary_op(comp, pns, MP_BINARY_OP_OR);
Damien429d7192013-10-04 19:53:11 +01002074}
2075
Damiend99b0522013-12-21 18:17:45 +00002076void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002077 c_binary_op(comp, pns, MP_BINARY_OP_XOR);
Damien429d7192013-10-04 19:53:11 +01002078}
2079
Damiend99b0522013-12-21 18:17:45 +00002080void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georged17926d2014-03-30 13:35:08 +01002081 c_binary_op(comp, pns, MP_BINARY_OP_AND);
Damien429d7192013-10-04 19:53:11 +01002082}
2083
Damiend99b0522013-12-21 18:17:45 +00002084void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2085 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002086 compile_node(comp, pns->nodes[0]);
2087 for (int i = 1; i + 1 < num_nodes; i += 2) {
2088 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002089 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002090 EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002091 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002092 EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002093 } else {
2094 // shouldn't happen
2095 assert(0);
2096 }
2097 }
2098}
2099
Damiend99b0522013-12-21 18:17:45 +00002100void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2101 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002102 compile_node(comp, pns->nodes[0]);
2103 for (int i = 1; i + 1 < num_nodes; i += 2) {
2104 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002105 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002106 EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002107 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002108 EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002109 } else {
2110 // shouldn't happen
2111 assert(0);
2112 }
2113 }
2114}
2115
Damiend99b0522013-12-21 18:17:45 +00002116void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2117 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002118 compile_node(comp, pns->nodes[0]);
2119 for (int i = 1; i + 1 < num_nodes; i += 2) {
2120 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002121 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georged17926d2014-03-30 13:35:08 +01002122 EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002123 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002124 EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002125 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georged17926d2014-03-30 13:35:08 +01002126 EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002127 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georged17926d2014-03-30 13:35:08 +01002128 EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002129 } else {
2130 // shouldn't happen
2131 assert(0);
2132 }
2133 }
2134}
2135
Damiend99b0522013-12-21 18:17:45 +00002136void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002137 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002138 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002139 EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002140 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georged17926d2014-03-30 13:35:08 +01002141 EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002142 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georged17926d2014-03-30 13:35:08 +01002143 EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002144 } else {
2145 // shouldn't happen
2146 assert(0);
2147 }
2148}
2149
Damien George35e2a4e2014-02-05 00:51:47 +00002150void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2151 // this is to handle special super() call
2152 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2153
2154 compile_generic_all_nodes(comp, pns);
2155}
2156
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002157STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra) {
Damien429d7192013-10-04 19:53:11 +01002158 // function to call is on top of stack
2159
Damien George35e2a4e2014-02-05 00:51:47 +00002160#if !MICROPY_EMIT_CPYTHON
2161 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002162 if (MP_PARSE_NODE_IS_NULL(pn_arglist) && comp->func_arg_is_super && comp->scope_cur->kind == SCOPE_FUNCTION) {
Damien George35e2a4e2014-02-05 00:51:47 +00002163 EMIT_ARG(load_id, MP_QSTR___class__);
2164 // get first argument to function
2165 bool found = false;
2166 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
2167 if (comp->scope_cur->id_info[i].param) {
2168 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
2169 found = true;
2170 break;
2171 }
2172 }
2173 if (!found) {
2174 printf("TypeError: super() call cannot find self\n");
2175 return;
2176 }
2177 EMIT_ARG(call_function, 2, 0, false, false);
2178 return;
2179 }
2180#endif
2181
Damien429d7192013-10-04 19:53:11 +01002182 int old_n_arg_keyword = comp->n_arg_keyword;
2183 bool old_have_star_arg = comp->have_star_arg;
2184 bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
2185 comp->n_arg_keyword = 0;
2186 comp->have_star_arg = false;
2187 comp->have_dbl_star_arg = false;
2188
Damien Georgebbcd49a2014-02-06 20:30:16 +00002189 compile_node(comp, pn_arglist); // arguments to function call; can be null
Damien429d7192013-10-04 19:53:11 +01002190
2191 // compute number of positional arguments
Damien Georgebbcd49a2014-02-06 20:30:16 +00002192 int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
Damien429d7192013-10-04 19:53:11 +01002193 if (comp->have_star_arg) {
2194 n_positional -= 1;
2195 }
2196 if (comp->have_dbl_star_arg) {
2197 n_positional -= 1;
2198 }
2199
2200 if (is_method_call) {
Damien Georgeb9791222014-01-23 00:34:21 +00002201 EMIT_ARG(call_method, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg);
Damien429d7192013-10-04 19:53:11 +01002202 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002203 EMIT_ARG(call_function, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg);
Damien429d7192013-10-04 19:53:11 +01002204 }
2205
2206 comp->n_arg_keyword = old_n_arg_keyword;
2207 comp->have_star_arg = old_have_star_arg;
2208 comp->have_dbl_star_arg = old_have_dbl_star_arg;
2209}
2210
Damiend99b0522013-12-21 18:17:45 +00002211void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2212 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002213 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002214 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 +01002215 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002216 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2217 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002218 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002219 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002220 i += 1;
2221 } else {
2222 compile_node(comp, pns->nodes[i]);
2223 }
Damien George35e2a4e2014-02-05 00:51:47 +00002224 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002225 }
2226}
2227
Damiend99b0522013-12-21 18:17:45 +00002228void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002229 compile_node(comp, pns->nodes[0]);
Damien Georged17926d2014-03-30 13:35:08 +01002230 EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002231}
2232
Damiend99b0522013-12-21 18:17:45 +00002233void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002234 // a list of strings
Damien63321742013-12-10 17:41:49 +00002235
2236 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002237 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002238 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002239 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002240 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002241 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2242 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2243 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002244 if (i == 0) {
2245 string_kind = pn_kind;
2246 } else if (pn_kind != string_kind) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002247 compile_syntax_error(comp, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002248 return;
2249 }
Damien George55baff42014-01-21 21:40:13 +00002250 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002251 }
Damien63321742013-12-10 17:41:49 +00002252
Damien63321742013-12-10 17:41:49 +00002253 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002254 byte *q_ptr;
2255 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002256 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002257 uint s_len;
2258 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002259 memcpy(s_dest, s, s_len);
2260 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002261 }
Damien George55baff42014-01-21 21:40:13 +00002262 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002263
Damien Georgeb9791222014-01-23 00:34:21 +00002264 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002265}
2266
2267// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002268void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2269 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2270 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2271 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002272
2273 if (comp->pass == PASS_1) {
2274 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002275 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 +01002276 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002277 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002278 }
2279
2280 // get the scope for this comprehension
2281 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2282
2283 // compile the comprehension
2284 close_over_variables_etc(comp, this_scope, 0, 0);
2285
2286 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2287 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002288 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01002289}
2290
Damiend99b0522013-12-21 18:17:45 +00002291void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2292 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002293 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002294 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2295 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2296 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2297 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2298 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2299 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2300 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002301 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002302 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002303 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002304 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002305 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002306 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002307 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002308 // generator expression
2309 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2310 } else {
2311 // tuple with 2 items
2312 goto tuple_with_2_items;
2313 }
2314 } else {
2315 // tuple with 2 items
2316 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002317 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002318 }
2319 } else {
2320 // parenthesis around a single item, is just that item
2321 compile_node(comp, pns->nodes[0]);
2322 }
2323}
2324
Damiend99b0522013-12-21 18:17:45 +00002325void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2326 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002327 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002328 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002329 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2330 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2331 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2332 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2333 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002334 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002335 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002336 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002337 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002338 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002339 // list of many items
2340 compile_node(comp, pns2->nodes[0]);
2341 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002342 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002343 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002344 // list comprehension
2345 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2346 } else {
2347 // list with 2 items
2348 goto list_with_2_items;
2349 }
2350 } else {
2351 // list with 2 items
2352 list_with_2_items:
2353 compile_node(comp, pns2->nodes[0]);
2354 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002355 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002356 }
2357 } else {
2358 // list with 1 item
2359 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002360 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002361 }
2362}
2363
Damiend99b0522013-12-21 18:17:45 +00002364void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2365 mp_parse_node_t pn = pns->nodes[0];
2366 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002367 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002368 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002369 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2370 pns = (mp_parse_node_struct_t*)pn;
2371 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002372 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002373 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002374 compile_node(comp, pn);
2375 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002376 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2377 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2378 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2379 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002380 // dict/set with multiple elements
2381
2382 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002383 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002384 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2385
2386 // first element sets whether it's a dict or set
2387 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002388 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002389 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002390 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002391 compile_node(comp, pns->nodes[0]);
2392 EMIT(store_map);
2393 is_dict = true;
2394 } else {
2395 // a set
2396 compile_node(comp, pns->nodes[0]); // 1st value of set
2397 is_dict = false;
2398 }
2399
2400 // process rest of elements
2401 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002402 mp_parse_node_t pn = nodes[i];
2403 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002404 compile_node(comp, pn);
2405 if (is_dict) {
2406 if (!is_key_value) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002407 compile_syntax_error(comp, "?expecting key:value for dictiona");
Damien429d7192013-10-04 19:53:11 +01002408 return;
2409 }
2410 EMIT(store_map);
2411 } else {
2412 if (is_key_value) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002413 compile_syntax_error(comp, "?expecting just a value for s");
Damien429d7192013-10-04 19:53:11 +01002414 return;
2415 }
2416 }
2417 }
2418
2419 // if it's a set, build it
2420 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002421 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002422 }
Damiend99b0522013-12-21 18:17:45 +00002423 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002424 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002425 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002426 // a dictionary comprehension
2427 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2428 } else {
2429 // a set comprehension
2430 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2431 }
2432 } else {
2433 // shouldn't happen
2434 assert(0);
2435 }
2436 } else {
2437 // set with one element
2438 goto set_with_one_element;
2439 }
2440 } else {
2441 // set with one element
2442 set_with_one_element:
2443 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002444 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002445 }
2446}
2447
Damiend99b0522013-12-21 18:17:45 +00002448void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002449 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002450}
2451
Damiend99b0522013-12-21 18:17:45 +00002452void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002453 // object who's index we want is on top of stack
2454 compile_node(comp, pns->nodes[0]); // the index
Damien Georged17926d2014-03-30 13:35:08 +01002455 EMIT_ARG(binary_op, MP_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002456}
2457
Damiend99b0522013-12-21 18:17:45 +00002458void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002459 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002460 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002461}
2462
Damiend99b0522013-12-21 18:17:45 +00002463void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2464 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2465 mp_parse_node_t pn = pns->nodes[0];
2466 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002467 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002468 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2469 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002470 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2471 pns = (mp_parse_node_struct_t*)pn;
2472 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002473 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002474 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002475 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002476 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002477 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002478 } else {
2479 // [?::x]
2480 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002481 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002482 }
Damiend99b0522013-12-21 18:17:45 +00002483 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002484 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002485 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2486 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2487 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2488 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002489 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002490 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002491 } else {
2492 // [?:x:x]
2493 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002494 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002495 }
2496 } else {
2497 // [?:x]
2498 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002499 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002500 }
2501 } else {
2502 // [?:x]
2503 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002504 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002505 }
2506}
2507
Damiend99b0522013-12-21 18:17:45 +00002508void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002509 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002510 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2511 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002512}
2513
Damiend99b0522013-12-21 18:17:45 +00002514void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002515 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002516 compile_subscript_3_helper(comp, pns);
2517}
2518
Damiend99b0522013-12-21 18:17:45 +00002519void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002520 // if this is called then we are compiling a dict key:value pair
2521 compile_node(comp, pns->nodes[1]); // value
2522 compile_node(comp, pns->nodes[0]); // key
2523}
2524
Damiend99b0522013-12-21 18:17:45 +00002525void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002526 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002527 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002528 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002529}
2530
Damiend99b0522013-12-21 18:17:45 +00002531void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002532 if (comp->have_star_arg) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002533 compile_syntax_error(comp, "?can't have multiple *x");
Damien429d7192013-10-04 19:53:11 +01002534 return;
2535 }
2536 comp->have_star_arg = true;
2537 compile_node(comp, pns->nodes[0]);
2538}
2539
Damiend99b0522013-12-21 18:17:45 +00002540void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002541 if (comp->have_dbl_star_arg) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002542 compile_syntax_error(comp, "?can't have multiple **x");
Damien429d7192013-10-04 19:53:11 +01002543 return;
2544 }
2545 comp->have_dbl_star_arg = true;
2546 compile_node(comp, pns->nodes[0]);
2547}
2548
Damiend99b0522013-12-21 18:17:45 +00002549void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2550 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2551 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2552 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2553 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002554 compile_syntax_error(comp, "?lhs of keyword argument must be an id");
Damien429d7192013-10-04 19:53:11 +01002555 return;
2556 }
Damien Georgeb9791222014-01-23 00:34:21 +00002557 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002558 compile_node(comp, pns2->nodes[0]);
2559 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002560 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002561 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2562 } else {
2563 // shouldn't happen
2564 assert(0);
2565 }
2566}
2567
Damiend99b0522013-12-21 18:17:45 +00002568void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002569 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002570 compile_syntax_error(comp, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002571 return;
2572 }
Damiend99b0522013-12-21 18:17:45 +00002573 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002574 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002575 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002576 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2577 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002578 compile_node(comp, pns->nodes[0]);
2579 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002580 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002581 EMIT(yield_from);
2582 } else {
2583 compile_node(comp, pns->nodes[0]);
2584 EMIT(yield_value);
2585 }
2586}
2587
Damiend99b0522013-12-21 18:17:45 +00002588typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002589STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002590 NULL,
2591#define nc NULL
2592#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002593#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002594#include "grammar.h"
2595#undef nc
2596#undef c
2597#undef DEF_RULE
2598};
2599
Damiend99b0522013-12-21 18:17:45 +00002600void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2601 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002602 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002603 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2604 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2605 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002606 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002607 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002608 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002609 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002610 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2611 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2612 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2613 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002614 case MP_PARSE_NODE_TOKEN:
2615 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002616 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002617 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002618 // do nothing
2619 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002620 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002621 }
2622 break;
Damien429d7192013-10-04 19:53:11 +01002623 default: assert(0);
2624 }
2625 } else {
Damiend99b0522013-12-21 18:17:45 +00002626 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002627 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002628 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002629 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002630 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002631#if MICROPY_DEBUG_PRINTERS
2632 mp_parse_node_print(pn, 0);
2633#endif
Damien429d7192013-10-04 19:53:11 +01002634 assert(0);
2635 } else {
2636 f(comp, pns);
2637 }
2638 }
2639}
2640
Damiend99b0522013-12-21 18:17:45 +00002641void 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 +01002642 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002643 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002644 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2645 if (MP_PARSE_NODE_IS_ID(pn)) {
2646 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002647 if (comp->have_bare_star) {
2648 // comes after a bare star, so doesn't count as a parameter
2649 } else {
2650 comp->scope_cur->num_params += 1;
2651 }
Damienb14de212013-10-06 00:28:28 +01002652 } else {
Damiend99b0522013-12-21 18:17:45 +00002653 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2654 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2655 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2656 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002657 //int node_index = 1; unused
2658 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002659 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002660 // this parameter has an annotation
2661 pn_annotation = pns->nodes[1];
2662 }
2663 //node_index = 2; unused
2664 }
2665 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002666 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002667 // this parameter has a default value
2668 if (comp->have_bare_star) {
2669 comp->scope_cur->num_dict_params += 1;
2670 } else {
2671 comp->scope_cur->num_default_params += 1;
2672 }
2673 }
2674 */
2675 if (comp->have_bare_star) {
2676 // comes after a bare star, so doesn't count as a parameter
2677 } else {
2678 comp->scope_cur->num_params += 1;
2679 }
Damiend99b0522013-12-21 18:17:45 +00002680 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2681 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002682 // bare star
2683 // TODO see http://www.python.org/dev/peps/pep-3102/
2684 comp->have_bare_star = true;
2685 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002686 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002687 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002688 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002689 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2690 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002691 // named star with annotation
Damien George8725f8f2014-02-15 19:33:11 +00002692 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002693 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2694 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002695 pn_annotation = pns->nodes[1];
2696 } else {
2697 // shouldn't happen
2698 assert(0);
2699 }
Damiend99b0522013-12-21 18:17:45 +00002700 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2701 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2702 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002703 // this parameter has an annotation
2704 pn_annotation = pns->nodes[1];
2705 }
Damien George8725f8f2014-02-15 19:33:11 +00002706 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002707 } else {
Damienb14de212013-10-06 00:28:28 +01002708 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002709 assert(0);
2710 }
Damien429d7192013-10-04 19:53:11 +01002711 }
2712
2713 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002714 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002715 // TODO this parameter has an annotation
2716 }
2717 bool added;
2718 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2719 if (!added) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002720 compile_syntax_error(comp, "?same name used for parameter");
Damien429d7192013-10-04 19:53:11 +01002721 return;
2722 }
2723 id_info->param = true;
2724 id_info->kind = ID_INFO_KIND_LOCAL;
2725 }
2726}
2727
Damiend99b0522013-12-21 18:17:45 +00002728void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002729 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2730}
2731
Damiend99b0522013-12-21 18:17:45 +00002732void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002733 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2734}
2735
Damiend99b0522013-12-21 18:17:45 +00002736void 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 +01002737 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002738 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002739 // no more nested if/for; compile inner expression
2740 compile_node(comp, pn_inner_expr);
2741 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002742 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002743 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002744 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002745 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002746 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002747 } else {
2748 EMIT(yield_value);
2749 EMIT(pop_top);
2750 }
Damiend99b0522013-12-21 18:17:45 +00002751 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002752 // if condition
Damiend99b0522013-12-21 18:17:45 +00002753 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002754 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2755 pn_iter = pns_comp_if->nodes[1];
2756 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002757 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002758 // for loop
Damiend99b0522013-12-21 18:17:45 +00002759 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002760 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002761 int l_end2 = comp_next_label(comp);
2762 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002763 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002764 EMIT_ARG(label_assign, l_top2);
2765 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002766 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2767 compile_scope_comp_iter(comp, pns_comp_for2->nodes[2], pn_inner_expr, l_top2, for_depth + 1);
Damien Georgeb9791222014-01-23 00:34:21 +00002768 EMIT_ARG(jump, l_top2);
2769 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002770 EMIT(for_iter_end);
2771 } else {
2772 // shouldn't happen
2773 assert(0);
2774 }
2775}
2776
Damiend99b0522013-12-21 18:17:45 +00002777void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002778 // see http://www.python.org/dev/peps/pep-0257/
2779
2780 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002781 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002782 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002783 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002784 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002785 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2786 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002787 for (int i = 0; i < num_nodes; i++) {
2788 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002789 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 +00002790 // not a newline, so this is the first statement; finish search
2791 break;
2792 }
2793 }
2794 // 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 +00002795 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002796 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002797 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002798 } else {
2799 return;
2800 }
2801
2802 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002803 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2804 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2805 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2806 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2807 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002808 compile_node(comp, pns->nodes[0]); // a doc string
2809 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002810 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002811 }
2812 }
2813 }
2814}
2815
2816void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2817 comp->pass = pass;
2818 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002819 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002820 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002821
2822 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002823 // reset maximum stack sizes in scope
2824 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002825 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002826 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002827 }
2828
Damien5ac1b2e2013-10-18 19:58:12 +01002829#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002830 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002831 scope_print_info(scope);
2832 }
Damien5ac1b2e2013-10-18 19:58:12 +01002833#endif
Damien429d7192013-10-04 19:53:11 +01002834
2835 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002836 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2837 assert(scope->kind == SCOPE_MODULE);
2838 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2839 compile_node(comp, pns->nodes[0]); // compile the expression
2840 EMIT(return_value);
2841 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002842 if (!comp->is_repl) {
2843 check_for_doc_string(comp, scope->pn);
2844 }
Damien429d7192013-10-04 19:53:11 +01002845 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002846 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002847 EMIT(return_value);
2848 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002849 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2850 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2851 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002852
2853 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002854 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002855 if (comp->pass == PASS_1) {
2856 comp->have_bare_star = false;
2857 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2858 }
2859
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02002860 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01002861
2862 compile_node(comp, pns->nodes[3]); // 3 is function body
2863 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002864 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002865 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002866 EMIT(return_value);
2867 }
2868 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002869 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2870 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2871 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002872
2873 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002874 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002875 if (comp->pass == PASS_1) {
2876 comp->have_bare_star = false;
2877 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2878 }
2879
2880 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2881 EMIT(return_value);
2882 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2883 // a bit of a hack at the moment
2884
Damiend99b0522013-12-21 18:17:45 +00002885 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2886 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2887 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2888 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2889 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002890
Damien George55baff42014-01-21 21:40:13 +00002891 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01002892 if (comp->pass == PASS_1) {
2893 bool added;
2894 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2895 assert(added);
2896 id_info->kind = ID_INFO_KIND_LOCAL;
2897 scope->num_params = 1;
2898 }
2899
2900 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002901 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01002902 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002903 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01002904 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002905 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01002906 }
2907
Damienb05d7072013-10-05 13:37:10 +01002908 int l_end = comp_next_label(comp);
2909 int l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002910 EMIT_ARG(load_id, qstr_arg);
2911 EMIT_ARG(label_assign, l_top);
2912 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01002913 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2914 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00002915 EMIT_ARG(jump, l_top);
2916 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002917 EMIT(for_iter_end);
2918
2919 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00002920 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002921 }
2922 EMIT(return_value);
2923 } else {
2924 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002925 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2926 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2927 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01002928
2929 if (comp->pass == PASS_1) {
2930 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002931 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01002932 assert(added);
2933 id_info->kind = ID_INFO_KIND_LOCAL;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002934 id_info = scope_find_or_add_id(scope, MP_QSTR___locals__, &added);
Damien429d7192013-10-04 19:53:11 +01002935 assert(added);
2936 id_info->kind = ID_INFO_KIND_LOCAL;
2937 id_info->param = true;
2938 scope->num_params = 1; // __locals__ is the parameter
2939 }
2940
Damien Georgeb9791222014-01-23 00:34:21 +00002941 EMIT_ARG(load_id, MP_QSTR___locals__);
Damien429d7192013-10-04 19:53:11 +01002942 EMIT(store_locals);
Damien Georgeb9791222014-01-23 00:34:21 +00002943 EMIT_ARG(load_id, MP_QSTR___name__);
2944 EMIT_ARG(store_id, MP_QSTR___module__);
2945 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
2946 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01002947
2948 check_for_doc_string(comp, pns->nodes[2]);
2949 compile_node(comp, pns->nodes[2]); // 2 is class body
2950
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002951 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01002952 assert(id != NULL);
2953 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00002954 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002955 } else {
Damien George6baf76e2013-12-30 22:32:17 +00002956#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00002957 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002958#else
Damien George35e2a4e2014-02-05 00:51:47 +00002959 EMIT_ARG(load_fast, MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00002960#endif
Damien429d7192013-10-04 19:53:11 +01002961 }
2962 EMIT(return_value);
2963 }
2964
Damien415eb6f2013-10-05 12:19:06 +01002965 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00002966
2967 // make sure we match all the exception levels
2968 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01002969}
2970
2971void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2972 comp->pass = pass;
2973 comp->scope_cur = scope;
2974 comp->next_label = 1;
2975
2976 if (scope->kind != SCOPE_FUNCTION) {
2977 printf("Error: inline assembler must be a function\n");
2978 return;
2979 }
2980
Damiena2f2f7d2013-10-06 00:14:13 +01002981 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00002982 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01002983 }
2984
Damien826005c2013-10-05 23:17:28 +01002985 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00002986 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2987 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2988 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01002989
Damiend99b0522013-12-21 18:17:45 +00002990 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01002991
Damiena2f2f7d2013-10-06 00:14:13 +01002992 // parameters are in pns->nodes[1]
2993 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00002994 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01002995 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00002996 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01002997 }
2998
Damiend99b0522013-12-21 18:17:45 +00002999 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01003000
Damiend99b0522013-12-21 18:17:45 +00003001 mp_parse_node_t pn_body = pns->nodes[3]; // body
3002 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01003003 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
3004
Damien Georgecbd2f742014-01-19 11:48:48 +00003005 /*
Damien826005c2013-10-05 23:17:28 +01003006 if (comp->pass == PASS_3) {
3007 //printf("----\n");
3008 scope_print_info(scope);
3009 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003010 */
Damien826005c2013-10-05 23:17:28 +01003011
3012 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003013 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3014 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
3015 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
3016 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3017 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3018 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3019 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3020 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3021 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3022 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3023 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3024 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3025 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003026 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3027
3028 // emit instructions
3029 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00003030 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgef41fdd02014-03-03 23:19:11 +00003031 compile_syntax_error(comp, "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003032 return;
3033 }
3034 int lab = comp_next_label(comp);
3035 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003036 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003037 }
3038 } else {
3039 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003040 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003041 }
3042 }
3043 }
3044
3045 if (comp->pass > PASS_1) {
3046 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01003047 }
Damien429d7192013-10-04 19:53:11 +01003048}
3049
3050void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3051 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003052 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003053 scope->num_locals = 0;
3054 for (int i = 0; i < scope->id_info_len; i++) {
3055 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003056 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003057 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3058 continue;
3059 }
3060 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3061 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3062 }
Damien9ecbcff2013-12-11 00:41:43 +00003063 // note: params always count for 1 local, even if they are a cell
Damien429d7192013-10-04 19:53:11 +01003064 if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
3065 id->local_num = scope->num_locals;
3066 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003067 }
3068 }
3069
3070 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003071#if MICROPY_EMIT_CPYTHON
3072 int num_cell = 0;
3073#endif
Damien9ecbcff2013-12-11 00:41:43 +00003074 for (int i = 0; i < scope->id_info_len; i++) {
3075 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003076#if MICROPY_EMIT_CPYTHON
3077 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003078 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003079 id->local_num = num_cell;
3080 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003081 }
Damien George6baf76e2013-12-30 22:32:17 +00003082#else
3083 // in Micro Python the cells come right after the fast locals
3084 // parameters are not counted here, since they remain at the start
3085 // of the locals, even if they are cell vars
3086 if (!id->param && id->kind == ID_INFO_KIND_CELL) {
3087 id->local_num = scope->num_locals;
3088 scope->num_locals += 1;
3089 }
3090#endif
Damien9ecbcff2013-12-11 00:41:43 +00003091 }
Damien9ecbcff2013-12-11 00:41:43 +00003092
3093 // compute the index of free vars (freevars[idx] in CPython)
3094 // make sure they are in the order of the parent scope
3095 if (scope->parent != NULL) {
3096 int num_free = 0;
3097 for (int i = 0; i < scope->parent->id_info_len; i++) {
3098 id_info_t *id = &scope->parent->id_info[i];
3099 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3100 for (int j = 0; j < scope->id_info_len; j++) {
3101 id_info_t *id2 = &scope->id_info[j];
3102 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +00003103 assert(!id2->param); // free vars should not be params
3104#if MICROPY_EMIT_CPYTHON
3105 // in CPython the frees are numbered after the cells
3106 id2->local_num = num_cell + num_free;
3107#else
3108 // in Micro Python the frees come first, before the params
3109 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003110#endif
3111 num_free += 1;
3112 }
3113 }
3114 }
Damien429d7192013-10-04 19:53:11 +01003115 }
Damien George6baf76e2013-12-30 22:32:17 +00003116#if !MICROPY_EMIT_CPYTHON
3117 // in Micro Python shift all other locals after the free locals
3118 if (num_free > 0) {
3119 for (int i = 0; i < scope->id_info_len; i++) {
3120 id_info_t *id = &scope->id_info[i];
3121 if (id->param || id->kind != ID_INFO_KIND_FREE) {
3122 id->local_num += num_free;
3123 }
3124 }
3125 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3126 scope->num_locals += num_free;
3127 }
3128#endif
Damien429d7192013-10-04 19:53:11 +01003129 }
3130
Damien George8725f8f2014-02-15 19:33:11 +00003131 // compute scope_flags
3132 //scope->scope_flags = 0; since we set some things in parameters
Damien429d7192013-10-04 19:53:11 +01003133 if (scope->kind != SCOPE_MODULE) {
Damien George8725f8f2014-02-15 19:33:11 +00003134 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien429d7192013-10-04 19:53:11 +01003135 }
3136 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) {
3137 assert(scope->parent != NULL);
Damien George8725f8f2014-02-15 19:33:11 +00003138 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien429d7192013-10-04 19:53:11 +01003139
3140 // TODO possibly other ways it can be nested
Damien George08d07552014-01-29 18:58:52 +00003141 // Note that we don't actually use this information at the moment (for CPython compat only)
3142 if ((SCOPE_FUNCTION <= scope->parent->kind && scope->parent->kind <= SCOPE_SET_COMP) || (scope->parent->kind == SCOPE_CLASS && scope->parent->parent->kind == SCOPE_FUNCTION)) {
Damien George8725f8f2014-02-15 19:33:11 +00003143 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003144 }
3145 }
3146 int num_free = 0;
3147 for (int i = 0; i < scope->id_info_len; i++) {
3148 id_info_t *id = &scope->id_info[i];
3149 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3150 num_free += 1;
3151 }
3152 }
3153 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003154 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003155 }
3156}
3157
Damien George08335002014-01-18 23:24:36 +00003158mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
Damien429d7192013-10-04 19:53:11 +01003159 compiler_t *comp = m_new(compiler_t, 1);
3160
Damien Georgecbd2f742014-01-19 11:48:48 +00003161 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003162 comp->is_repl = is_repl;
3163 comp->had_error = false;
3164
Damien429d7192013-10-04 19:53:11 +01003165 comp->break_label = 0;
3166 comp->continue_label = 0;
Damien Georgecbddb272014-02-01 20:08:18 +00003167 comp->break_continue_except_level = 0;
3168 comp->cur_except_level = 0;
3169
Damien George35e2a4e2014-02-05 00:51:47 +00003170 comp->func_arg_is_super = false;
3171
Damien429d7192013-10-04 19:53:11 +01003172 comp->scope_head = NULL;
3173 comp->scope_cur = NULL;
3174
Damien826005c2013-10-05 23:17:28 +01003175 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003176 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003177
3178 // set the outer scope
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003179 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE);
Damien429d7192013-10-04 19:53:11 +01003180
Damien826005c2013-10-05 23:17:28 +01003181 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003182 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003183 comp->emit_method_table = &emit_pass1_method_table;
3184 comp->emit_inline_asm = NULL;
3185 comp->emit_inline_asm_method_table = NULL;
3186 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003187 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003188 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003189#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003190 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003191 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003192#endif
Damien826005c2013-10-05 23:17:28 +01003193 } else {
3194 compile_scope(comp, s, PASS_1);
3195 }
3196
3197 // update maximim number of labels needed
3198 if (comp->next_label > max_num_labels) {
3199 max_num_labels = comp->next_label;
3200 }
Damien429d7192013-10-04 19:53:11 +01003201 }
3202
Damien826005c2013-10-05 23:17:28 +01003203 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003204 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003205 compile_scope_compute_things(comp, s);
3206 }
3207
Damien826005c2013-10-05 23:17:28 +01003208 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003209 emit_pass1_free(comp->emit);
3210
Damien826005c2013-10-05 23:17:28 +01003211 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003212#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003213 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003214#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003215 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003216#endif
Damien3ef4abb2013-10-12 16:53:13 +01003217#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003218 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003219#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003220#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003221 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003222 if (false) {
3223 // dummy
3224
Damien3ef4abb2013-10-12 16:53:13 +01003225#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003226 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
3227 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003228 if (emit_inline_thumb == NULL) {
3229 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3230 }
3231 comp->emit = NULL;
3232 comp->emit_method_table = NULL;
3233 comp->emit_inline_asm = emit_inline_thumb;
3234 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3235 compile_scope_inline_asm(comp, s, PASS_2);
3236 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003237#endif
3238
Damien826005c2013-10-05 23:17:28 +01003239 } else {
Damienc025ebb2013-10-12 14:30:21 +01003240
3241 // choose the emit type
3242
Damien3ef4abb2013-10-12 16:53:13 +01003243#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003244 comp->emit = emit_cpython_new(max_num_labels);
3245 comp->emit_method_table = &emit_cpython_method_table;
3246#else
Damien826005c2013-10-05 23:17:28 +01003247 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003248
3249#if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003250 case EMIT_OPT_NATIVE_PYTHON:
Damien3410be82013-10-07 23:09:10 +01003251 case EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003252#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003253 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003254 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003255 }
Damien13ed3a62013-10-08 09:05:10 +01003256 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003257#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003258 if (emit_native == NULL) {
3259 emit_native = emit_native_thumb_new(max_num_labels);
3260 }
3261 comp->emit_method_table = &emit_native_thumb_method_table;
3262#endif
3263 comp->emit = emit_native;
Damien3410be82013-10-07 23:09:10 +01003264 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003265 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003266#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003267
Damien826005c2013-10-05 23:17:28 +01003268 default:
3269 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003270 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003271 }
3272 comp->emit = emit_bc;
3273 comp->emit_method_table = &emit_bc_method_table;
3274 break;
3275 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003276#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003277
3278 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003279 compile_scope(comp, s, PASS_2);
3280 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003281 }
Damien429d7192013-10-04 19:53:11 +01003282 }
3283
Damien George41d02b62014-01-24 22:42:28 +00003284 // free the emitters
3285#if !MICROPY_EMIT_CPYTHON
3286 if (emit_bc != NULL) {
3287 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003288 }
Damien George41d02b62014-01-24 22:42:28 +00003289#if MICROPY_EMIT_NATIVE
3290 if (emit_native != NULL) {
3291#if MICROPY_EMIT_X64
3292 emit_native_x64_free(emit_native);
3293#elif MICROPY_EMIT_THUMB
3294 emit_native_thumb_free(emit_native);
3295#endif
3296 }
3297#endif
3298#if MICROPY_EMIT_INLINE_THUMB
3299 if (emit_inline_thumb != NULL) {
3300 emit_inline_thumb_free(emit_inline_thumb);
3301 }
3302#endif
3303#endif // !MICROPY_EMIT_CPYTHON
3304
3305 // free the scopes
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003306 uint unique_code_id = module_scope->unique_code_id;
3307 for (scope_t *s = module_scope; s;) {
3308 scope_t *next = s->next;
3309 scope_free(s);
3310 s = next;
3311 }
Damien5ac1b2e2013-10-18 19:58:12 +01003312
Damien George41d02b62014-01-24 22:42:28 +00003313 // free the compiler
3314 bool had_error = comp->had_error;
3315 m_del_obj(compiler_t, comp);
3316
Damien George1fb03172014-01-03 14:22:03 +00003317 if (had_error) {
3318 // TODO return a proper error message
3319 return mp_const_none;
3320 } else {
3321#if MICROPY_EMIT_CPYTHON
3322 // can't create code, so just return true
Damien George41d02b62014-01-24 22:42:28 +00003323 (void)unique_code_id; // to suppress warning that unique_code_id is unused
Damien George1fb03172014-01-03 14:22:03 +00003324 return mp_const_true;
3325#else
3326 // return function that executes the outer module
Damien Georged1e443d2014-03-29 11:39:36 +00003327 // we can free the unique_code slot because no-one has reference to this unique_code_id anymore
Damien Georged17926d2014-03-30 13:35:08 +01003328 return mp_make_function_from_id(unique_code_id, true, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003329#endif
3330 }
Damien429d7192013-10-04 19:53:11 +01003331}