blob: 9e6c4e5fe42e7a3cad9cd99a048913248097e5e4 [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 Georgeb9791222014-01-23 00:34:21 +0000605 EMIT_ARG(binary_op, RT_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
777void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_dict_params, int n_default_params) {
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000778#if !MICROPY_EMIT_CPYTHON
779 // in Micro Python we put the default params into a tuple using the bytecode
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200780 if (n_default_params) {
781 EMIT_ARG(build_tuple, n_default_params);
782 }
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000783#endif
784
Damien429d7192013-10-04 19:53:11 +0100785 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000786 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100787 int nfree = 0;
788 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000789 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
790 id_info_t *id = &comp->scope_cur->id_info[i];
791 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
792 for (int j = 0; j < this_scope->id_info_len; j++) {
793 id_info_t *id2 = &this_scope->id_info[j];
794 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000795#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000796 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000797#else
798 // in Micro Python we load closures using LOAD_FAST
Damien Georgeb9791222014-01-23 00:34:21 +0000799 EMIT_ARG(load_fast, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000800#endif
Damien318aec62013-12-10 18:28:17 +0000801 nfree += 1;
802 }
803 }
Damien429d7192013-10-04 19:53:11 +0100804 }
805 }
806 }
Damien429d7192013-10-04 19:53:11 +0100807
808 // make the function/closure
809 if (nfree == 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000810 EMIT_ARG(make_function, this_scope, n_dict_params, n_default_params);
Damien429d7192013-10-04 19:53:11 +0100811 } else {
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000812 EMIT_ARG(build_tuple, nfree);
Damien Georgeb9791222014-01-23 00:34:21 +0000813 EMIT_ARG(make_closure, this_scope, n_dict_params, n_default_params);
Damien429d7192013-10-04 19:53:11 +0100814 }
815}
816
Damiend99b0522013-12-21 18:17:45 +0000817void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000818 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damiend99b0522013-12-21 18:17:45 +0000819 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
820 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100821 // bare star
822 comp->have_bare_star = true;
823 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000824
825 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
826 // TODO do we need to do anything with this?
827
828 } else {
829 mp_parse_node_t pn_id;
830 mp_parse_node_t pn_colon;
831 mp_parse_node_t pn_equal;
832 if (MP_PARSE_NODE_IS_ID(pn)) {
833 // this parameter is just an id
834
835 pn_id = pn;
836 pn_colon = MP_PARSE_NODE_NULL;
837 pn_equal = MP_PARSE_NODE_NULL;
838
839 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
840 // this parameter has a colon and/or equal specifier
841
842 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
843 pn_id = pns->nodes[0];
844 pn_colon = pns->nodes[1];
845 pn_equal = pns->nodes[2];
846
847 } else {
848 assert(0);
849 return;
850 }
851
852 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
853 // this parameter does not have a default value
854
855 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
856 if (!comp->have_bare_star && comp->param_pass_num_default_params != 0) {
857 compile_syntax_error(comp, "non-default argument follows default argument");
858 return;
859 }
860
861 } else {
862 // this parameter has a default value
863 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
864
865 if (comp->have_bare_star) {
866 comp->param_pass_num_dict_params += 1;
867 if (comp->param_pass == 1) {
868 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pn_id));
869 compile_node(comp, pn_equal);
870 }
871 } else {
872 comp->param_pass_num_default_params += 1;
873 if (comp->param_pass == 2) {
874 compile_node(comp, pn_equal);
875 }
876 }
877 }
878
879 // TODO pn_colon not implemented
880 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100881 }
882}
883
884// leaves function object on stack
885// returns function name
Damiend99b0522013-12-21 18:17:45 +0000886qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100887 if (comp->pass == PASS_1) {
888 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000889 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100890 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000891 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100892 }
893
894 // save variables (probably don't need to do this, since we can't have nested definitions..?)
895 bool old_have_bare_star = comp->have_bare_star;
896 int old_param_pass = comp->param_pass;
897 int old_param_pass_num_dict_params = comp->param_pass_num_dict_params;
898 int old_param_pass_num_default_params = comp->param_pass_num_default_params;
899
900 // compile default parameters
Damien Georgef41fdd02014-03-03 23:19:11 +0000901
902 // pass 1 does any default parameters after bare star
Damien429d7192013-10-04 19:53:11 +0100903 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000904 comp->param_pass = 1;
Damien429d7192013-10-04 19:53:11 +0100905 comp->param_pass_num_dict_params = 0;
906 comp->param_pass_num_default_params = 0;
907 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +0000908
909 if (comp->had_error) {
910 return MP_QSTR_NULL;
911 }
912
913 // pass 2 does any default parameters before bare star
Damien429d7192013-10-04 19:53:11 +0100914 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000915 comp->param_pass = 2;
Damien429d7192013-10-04 19:53:11 +0100916 comp->param_pass_num_dict_params = 0;
917 comp->param_pass_num_default_params = 0;
918 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
919
920 // get the scope for this function
921 scope_t *fscope = (scope_t*)pns->nodes[4];
922
923 // make the function
924 close_over_variables_etc(comp, fscope, comp->param_pass_num_dict_params, comp->param_pass_num_default_params);
925
926 // restore variables
927 comp->have_bare_star = old_have_bare_star;
928 comp->param_pass = old_param_pass;
929 comp->param_pass_num_dict_params = old_param_pass_num_dict_params;
930 comp->param_pass_num_default_params = old_param_pass_num_default_params;
931
932 // return its name (the 'f' in "def f(...):")
933 return fscope->simple_name;
934}
935
936// leaves class object on stack
937// returns class name
Damiend99b0522013-12-21 18:17:45 +0000938qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100939 if (comp->pass == PASS_1) {
940 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000941 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100942 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000943 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100944 }
945
946 EMIT(load_build_class);
947
948 // scope for this class
949 scope_t *cscope = (scope_t*)pns->nodes[3];
950
951 // compile the class
952 close_over_variables_etc(comp, cscope, 0, 0);
953
954 // get its name
Damien Georgeb9791222014-01-23 00:34:21 +0000955 EMIT_ARG(load_const_id, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100956
957 // nodes[1] has parent classes, if any
Damien Georgebbcd49a2014-02-06 20:30:16 +0000958 comp->func_arg_is_super = false;
959 compile_trailer_paren_helper(comp, pns->nodes[1], false, 2);
Damien429d7192013-10-04 19:53:11 +0100960
961 // return its name (the 'C' in class C(...):")
962 return cscope->simple_name;
963}
964
Damien6cdd3af2013-10-05 18:08:26 +0100965// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200966STATIC 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 +0000967 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100968 return false;
969 }
970
971 if (name_len != 2) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000972 compile_syntax_error(comp, "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100973 return true;
974 }
975
Damiend99b0522013-12-21 18:17:45 +0000976 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000977 if (attr == MP_QSTR_byte_code) {
Damien5ac1b2e2013-10-18 19:58:12 +0100978 *emit_options = EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +0100979#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000980 } else if (attr == MP_QSTR_native) {
Damien6cdd3af2013-10-05 18:08:26 +0100981 *emit_options = EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000982 } else if (attr == MP_QSTR_viper) {
Damien7af3d192013-10-07 00:02:49 +0100983 *emit_options = EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100984#endif
Damien3ef4abb2013-10-12 16:53:13 +0100985#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000986 } else if (attr == MP_QSTR_asm_thumb) {
Damien5bfb7592013-10-05 18:41:24 +0100987 *emit_options = EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +0100988#endif
Damien6cdd3af2013-10-05 18:08:26 +0100989 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000990 compile_syntax_error(comp, "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100991 }
992
993 return true;
994}
995
Damiend99b0522013-12-21 18:17:45 +0000996void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100997 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +0000998 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +0100999 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
1000
Damien6cdd3af2013-10-05 18:08:26 +01001001 // inherit emit options for this function/class definition
1002 uint emit_options = comp->scope_cur->emit_options;
1003
1004 // compile each decorator
1005 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001006 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001007 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1008 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001009
1010 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001011 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001012 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1013
1014 // check for built-in decorators
1015 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1016 // this was a built-in
1017 num_built_in_decorators += 1;
1018
1019 } else {
1020 // not a built-in, compile normally
1021
1022 // compile the decorator function
1023 compile_node(comp, name_nodes[0]);
1024 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001025 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001026 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001027 }
1028
1029 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001030 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001031 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001032 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001033 compile_node(comp, pns_decorator->nodes[1]);
1034 }
Damien429d7192013-10-04 19:53:11 +01001035 }
1036 }
1037
1038 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001039 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001040 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001041 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001042 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001043 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001044 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001045 } else {
1046 // shouldn't happen
1047 assert(0);
1048 }
1049
1050 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001051 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001052 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01001053 }
1054
1055 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001056 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001057}
1058
Damiend99b0522013-12-21 18:17:45 +00001059void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001060 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001061 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001062 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001063}
1064
Damiend99b0522013-12-21 18:17:45 +00001065void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1066 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001067 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001068 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1069 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001070
1071 compile_node(comp, pns->nodes[0]); // base of the power node
1072
Damiend99b0522013-12-21 18:17:45 +00001073 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1074 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1075 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1076 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001077 for (int i = 0; i < n - 1; i++) {
1078 compile_node(comp, pns1->nodes[i]);
1079 }
Damiend99b0522013-12-21 18:17:45 +00001080 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1081 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001082 }
Damiend99b0522013-12-21 18:17:45 +00001083 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien429d7192013-10-04 19:53:11 +01001084 // SyntaxError: can't delete a function call
1085 assert(0);
Damiend99b0522013-12-21 18:17:45 +00001086 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001087 compile_node(comp, pns1->nodes[0]);
1088 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001089 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1090 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001091 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001092 } else {
1093 // shouldn't happen
1094 assert(0);
1095 }
1096 } else {
1097 // shouldn't happen
1098 assert(0);
1099 }
1100
Damiend99b0522013-12-21 18:17:45 +00001101 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001102 // SyntaxError, cannot delete
1103 assert(0);
1104 }
Damiend99b0522013-12-21 18:17:45 +00001105 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1106 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1107 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1108 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001109 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1110
Damiend99b0522013-12-21 18:17:45 +00001111 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1112 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1113 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001114 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001115 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001116 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001117 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001118 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001119 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001120 c_del_stmt(comp, pns->nodes[0]);
1121 for (int i = 0; i < n; i++) {
1122 c_del_stmt(comp, pns1->nodes[i]);
1123 }
Damiend99b0522013-12-21 18:17:45 +00001124 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001125 // TODO not implemented; can't del comprehension?
1126 assert(0);
1127 } else {
1128 // sequence with 2 items
1129 goto sequence_with_2_items;
1130 }
1131 } else {
1132 // sequence with 2 items
1133 sequence_with_2_items:
1134 c_del_stmt(comp, pns->nodes[0]);
1135 c_del_stmt(comp, pns->nodes[1]);
1136 }
1137 } else {
1138 // tuple with 1 element
1139 c_del_stmt(comp, pn);
1140 }
1141 } else {
1142 // not implemented
1143 assert(0);
1144 }
1145}
1146
Damiend99b0522013-12-21 18:17:45 +00001147void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001148 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1149}
1150
Damiend99b0522013-12-21 18:17:45 +00001151void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001152 if (comp->break_label == 0) {
Damien George2326d522014-03-27 23:26:35 +00001153 compile_syntax_error(comp, "'break' outside loop");
Damien429d7192013-10-04 19:53:11 +01001154 }
Damien Georgecbddb272014-02-01 20:08:18 +00001155 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001156}
1157
Damiend99b0522013-12-21 18:17:45 +00001158void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001159 if (comp->continue_label == 0) {
Damien George2326d522014-03-27 23:26:35 +00001160 compile_syntax_error(comp, "'continue' outside loop");
Damien429d7192013-10-04 19:53:11 +01001161 }
Damien Georgecbddb272014-02-01 20:08:18 +00001162 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001163}
1164
Damiend99b0522013-12-21 18:17:45 +00001165void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001166 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001167 compile_syntax_error(comp, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001168 return;
1169 }
Damiend99b0522013-12-21 18:17:45 +00001170 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001171 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001172 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001173 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001174 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001175 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1176 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 +01001177
Damienb05d7072013-10-05 13:37:10 +01001178 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001179 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1180 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1181 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001182 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001183 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1184 } else {
1185 compile_node(comp, pns->nodes[0]);
1186 }
1187 EMIT(return_value);
1188}
1189
Damiend99b0522013-12-21 18:17:45 +00001190void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001191 compile_node(comp, pns->nodes[0]);
1192 EMIT(pop_top);
1193}
1194
Damiend99b0522013-12-21 18:17:45 +00001195void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1196 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001197 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001198 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001199 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001200 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001201 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001202 compile_node(comp, pns->nodes[0]);
1203 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001204 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001205 } else {
1206 // raise x
1207 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001208 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001209 }
1210}
1211
1212// q1 holds the base, q2 the full name
1213// eg a -> q1=q2=a
1214// a.b.c -> q1=a, q2=a.b.c
Damiend99b0522013-12-21 18:17:45 +00001215void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q1, qstr *q2) {
Damien429d7192013-10-04 19:53:11 +01001216 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001217 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1218 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001219 // a name of the form x as y; unwrap it
Damiend99b0522013-12-21 18:17:45 +00001220 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001221 pn = pns->nodes[0];
1222 is_as = true;
1223 }
Damiend99b0522013-12-21 18:17:45 +00001224 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001225 // just a simple name
Damiend99b0522013-12-21 18:17:45 +00001226 *q2 = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001227 if (!is_as) {
1228 *q1 = *q2;
1229 }
Damien Georgeb9791222014-01-23 00:34:21 +00001230 EMIT_ARG(import_name, *q2);
Damiend99b0522013-12-21 18:17:45 +00001231 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1232 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1233 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001234 // a name of the form a.b.c
1235 if (!is_as) {
Damiend99b0522013-12-21 18:17:45 +00001236 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001237 }
Damiend99b0522013-12-21 18:17:45 +00001238 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001239 int len = n - 1;
1240 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001241 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001242 }
Damien George55baff42014-01-21 21:40:13 +00001243 byte *q_ptr;
1244 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001245 for (int i = 0; i < n; i++) {
1246 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001247 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001248 }
Damien George55baff42014-01-21 21:40:13 +00001249 uint str_src_len;
1250 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001251 memcpy(str_dest, str_src, str_src_len);
1252 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001253 }
Damien George55baff42014-01-21 21:40:13 +00001254 *q2 = qstr_build_end(q_ptr);
Damien Georgeb9791222014-01-23 00:34:21 +00001255 EMIT_ARG(import_name, *q2);
Damien429d7192013-10-04 19:53:11 +01001256 if (is_as) {
1257 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001258 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001259 }
1260 }
1261 } else {
1262 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001263 // This covers relative imports starting with dot(s) like "from .foo import"
Damien429d7192013-10-04 19:53:11 +01001264 assert(0);
1265 }
1266 } else {
1267 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001268 // This covers relative imports with dots only like "from .. import"
Damien429d7192013-10-04 19:53:11 +01001269 assert(0);
1270 }
1271}
1272
Damiend99b0522013-12-21 18:17:45 +00001273void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgeb9791222014-01-23 00:34:21 +00001274 EMIT_ARG(load_const_small_int, 0); // ??
1275 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001276 qstr q1, q2;
1277 do_import_name(comp, pn, &q1, &q2);
Damien Georgeb9791222014-01-23 00:34:21 +00001278 EMIT_ARG(store_id, q1);
Damien429d7192013-10-04 19:53:11 +01001279}
1280
Damiend99b0522013-12-21 18:17:45 +00001281void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001282 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1283}
1284
Damiend99b0522013-12-21 18:17:45 +00001285void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
1286 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001287 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001288
1289 // build the "fromlist" tuple
1290#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001291 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001292#else
Damien Georgeb9791222014-01-23 00:34:21 +00001293 EMIT_ARG(load_const_str, QSTR_FROM_STR_STATIC("*"), false);
1294 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001295#endif
1296
1297 // do the import
Damien429d7192013-10-04 19:53:11 +01001298 qstr dummy_q, id1;
1299 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1300 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001301
Damien429d7192013-10-04 19:53:11 +01001302 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001303 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001304
1305 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001306 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001307 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001308#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001309 {
1310 vstr_t *vstr = vstr_new();
1311 vstr_printf(vstr, "(");
1312 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001313 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1314 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1315 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001316 if (i > 0) {
1317 vstr_printf(vstr, ", ");
1318 }
1319 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001320 uint len;
1321 const byte *str = qstr_data(id2, &len);
1322 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001323 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001324 }
Damien02f89412013-12-12 15:13:36 +00001325 if (n == 1) {
1326 vstr_printf(vstr, ",");
1327 }
1328 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001329 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001330 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001331 }
Damiendb4c3612013-12-10 17:27:24 +00001332#else
1333 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001334 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1335 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1336 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001337 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001338 }
Damien Georgeb9791222014-01-23 00:34:21 +00001339 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001340#endif
1341
1342 // do the import
Damien429d7192013-10-04 19:53:11 +01001343 qstr dummy_q, id1;
1344 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1345 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001346 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1347 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1348 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001349 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001350 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001351 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001352 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001353 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001354 }
1355 }
1356 EMIT(pop_top);
1357 }
1358}
1359
Damiend99b0522013-12-21 18:17:45 +00001360void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001361 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001362 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1363 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001364 } else {
Damiend99b0522013-12-21 18:17:45 +00001365 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1366 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001367 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001368 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001369 }
Damien429d7192013-10-04 19:53:11 +01001370 }
1371 }
1372}
1373
Damiend99b0522013-12-21 18:17:45 +00001374void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001375 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001376 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1377 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001378 } else {
Damiend99b0522013-12-21 18:17:45 +00001379 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1380 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001381 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001382 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001383 }
Damien429d7192013-10-04 19:53:11 +01001384 }
1385 }
1386}
1387
Damiend99b0522013-12-21 18:17:45 +00001388void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001389 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001390 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001391 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 +00001392 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001393 // assertion message
1394 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001395 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01001396 }
Damien Georgeb9791222014-01-23 00:34:21 +00001397 EMIT_ARG(raise_varargs, 1);
1398 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001399}
1400
Damiend99b0522013-12-21 18:17:45 +00001401void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001402 // TODO proper and/or short circuiting
1403
Damienb05d7072013-10-05 13:37:10 +01001404 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001405
Damienb05d7072013-10-05 13:37:10 +01001406 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001407 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1408
1409 compile_node(comp, pns->nodes[1]); // if block
Damiend99b0522013-12-21 18:17:45 +00001410 //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 +01001411 // jump over elif/else blocks if they exist
Damien415eb6f2013-10-05 12:19:06 +01001412 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001413 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001414 }
1415 //}
Damien Georgeb9791222014-01-23 00:34:21 +00001416 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001417
Damiend99b0522013-12-21 18:17:45 +00001418 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001419 // compile elif blocks
1420
Damiend99b0522013-12-21 18:17:45 +00001421 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001422
Damiend99b0522013-12-21 18:17:45 +00001423 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001424 // multiple elif blocks
1425
Damiend99b0522013-12-21 18:17:45 +00001426 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001427 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001428 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001429 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001430 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1431
1432 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001433 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001434 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001435 }
Damien Georgeb9791222014-01-23 00:34:21 +00001436 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001437 }
1438
1439 } else {
1440 // a single elif block
1441
Damienb05d7072013-10-05 13:37:10 +01001442 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001443 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1444
1445 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001446 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001447 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001448 }
Damien Georgeb9791222014-01-23 00:34:21 +00001449 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001450 }
1451 }
1452
1453 // compile else block
1454 compile_node(comp, pns->nodes[3]); // can be null
1455
Damien Georgeb9791222014-01-23 00:34:21 +00001456 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001457}
1458
Damien Georgecbddb272014-02-01 20:08:18 +00001459#define START_BREAK_CONTINUE_BLOCK \
1460 int old_break_label = comp->break_label; \
1461 int old_continue_label = comp->continue_label; \
1462 int break_label = comp_next_label(comp); \
1463 int continue_label = comp_next_label(comp); \
1464 comp->break_label = break_label; \
1465 comp->continue_label = continue_label; \
1466 comp->break_continue_except_level = comp->cur_except_level;
1467
1468#define END_BREAK_CONTINUE_BLOCK \
1469 comp->break_label = old_break_label; \
1470 comp->continue_label = old_continue_label; \
1471 comp->break_continue_except_level = comp->cur_except_level;
1472
Damiend99b0522013-12-21 18:17:45 +00001473void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001474 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001475
Damience89a212013-10-15 22:25:17 +01001476 // compared to CPython, we have an optimised version of while loops
1477#if MICROPY_EMIT_CPYTHON
1478 int done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001479 EMIT_ARG(setup_loop, break_label);
1480 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001481 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1482 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001483 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001484 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001485 }
Damien Georgeb9791222014-01-23 00:34:21 +00001486 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001487 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1488 // this is a small hack to agree with CPython
1489 if (!node_is_const_true(pns->nodes[0])) {
1490 EMIT(pop_block);
1491 }
Damience89a212013-10-15 22:25:17 +01001492#else
1493 int top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001494 EMIT_ARG(jump, continue_label);
1495 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001496 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001497 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001498 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1499#endif
1500
1501 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001502 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001503
1504 compile_node(comp, pns->nodes[2]); // else
1505
Damien Georgeb9791222014-01-23 00:34:21 +00001506 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001507}
1508
Damienf72fd0e2013-11-06 20:20:49 +00001509// TODO preload end and step onto stack if they are not constants
1510// TODO check if step is negative and do opposite test
Damiend99b0522013-12-21 18:17:45 +00001511void 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 +00001512 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001513
1514 int top_label = comp_next_label(comp);
Damien George600ae732014-01-21 23:48:04 +00001515 int entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001516
1517 // compile: var = start
1518 compile_node(comp, pn_start);
1519 c_assign(comp, pn_var, ASSIGN_STORE);
1520
Damien Georgeb9791222014-01-23 00:34:21 +00001521 EMIT_ARG(jump, entry_label);
1522 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001523
Damienf3822fc2013-11-09 20:12:03 +00001524 // compile body
1525 compile_node(comp, pn_body);
1526
Damien Georgeb9791222014-01-23 00:34:21 +00001527 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001528
Damienf72fd0e2013-11-06 20:20:49 +00001529 // compile: var += step
1530 c_assign(comp, pn_var, ASSIGN_AUG_LOAD);
1531 compile_node(comp, pn_step);
Damien Georgeb9791222014-01-23 00:34:21 +00001532 EMIT_ARG(binary_op, RT_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001533 c_assign(comp, pn_var, ASSIGN_AUG_STORE);
1534
Damien Georgeb9791222014-01-23 00:34:21 +00001535 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001536
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001537 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001538 compile_node(comp, pn_var);
1539 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001540 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1541 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien George9aa2a522014-02-01 23:04:09 +00001542 EMIT_ARG(binary_op, RT_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001543 } else {
Damien George9aa2a522014-02-01 23:04:09 +00001544 EMIT_ARG(binary_op, RT_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001545 }
Damien Georgeb9791222014-01-23 00:34:21 +00001546 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001547
1548 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001549 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001550
1551 compile_node(comp, pn_else);
1552
Damien Georgeb9791222014-01-23 00:34:21 +00001553 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001554}
1555
Damiend99b0522013-12-21 18:17:45 +00001556void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001557#if !MICROPY_EMIT_CPYTHON
1558 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1559 // this is actually slower, but uses no heap memory
1560 // for viper it will be much, much faster
Damiend99b0522013-12-21 18:17:45 +00001561 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)) {
1562 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001563 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1564 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1565 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1566 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001567 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1568 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001569 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001570 mp_parse_node_t pn_range_start;
1571 mp_parse_node_t pn_range_end;
1572 mp_parse_node_t pn_range_step;
1573 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001574 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001575 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001576 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001577 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001578 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001579 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001580 } else if (n_args == 2) {
1581 pn_range_start = args[0];
1582 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001583 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001584 } else {
1585 pn_range_start = args[0];
1586 pn_range_end = args[1];
1587 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001588 // We need to know sign of step. This is possible only if it's constant
1589 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1590 optimize = false;
1591 }
Damienf72fd0e2013-11-06 20:20:49 +00001592 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001593 }
1594 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001595 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1596 return;
1597 }
1598 }
1599 }
1600#endif
1601
Damien Georgecbddb272014-02-01 20:08:18 +00001602 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001603
Damienb05d7072013-10-05 13:37:10 +01001604 int pop_label = comp_next_label(comp);
1605 int end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001606
Damience89a212013-10-15 22:25:17 +01001607 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1608#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001609 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001610#endif
1611
Damien429d7192013-10-04 19:53:11 +01001612 compile_node(comp, pns->nodes[1]); // iterator
1613 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001614 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001615 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001616 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1617 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001618 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001619 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001620 }
Damien Georgeb9791222014-01-23 00:34:21 +00001621 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001622 EMIT(for_iter_end);
1623
1624 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001625 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001626
Damience89a212013-10-15 22:25:17 +01001627#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001628 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001629#endif
Damien429d7192013-10-04 19:53:11 +01001630
1631 compile_node(comp, pns->nodes[3]); // else (not tested)
1632
Damien Georgeb9791222014-01-23 00:34:21 +00001633 EMIT_ARG(label_assign, break_label);
1634 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001635}
1636
Damiend99b0522013-12-21 18:17:45 +00001637void 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 +01001638 // this function is a bit of a hack at the moment
1639 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1640
1641 // setup code
1642 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001643 int l1 = comp_next_label(comp);
1644 int success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001645
Damien Georgeb9791222014-01-23 00:34:21 +00001646 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001647 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001648
Damien429d7192013-10-04 19:53:11 +01001649 compile_node(comp, pn_body); // body
1650 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001651 EMIT_ARG(jump, success_label);
1652 EMIT_ARG(label_assign, l1);
Damienb05d7072013-10-05 13:37:10 +01001653 int l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001654
1655 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001656 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1657 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001658
1659 qstr qstr_exception_local = 0;
Damienb05d7072013-10-05 13:37:10 +01001660 int end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001661
Damiend99b0522013-12-21 18:17:45 +00001662 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001663 // this is a catch all exception handler
1664 if (i + 1 != n_except) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001665 compile_syntax_error(comp, "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001666 return;
1667 }
1668 } else {
1669 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001670 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1671 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1672 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1673 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001674 // handler binds the exception to a local
1675 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001676 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001677 }
1678 }
1679 EMIT(dup_top);
1680 compile_node(comp, pns_exception_expr);
Damien George9aa2a522014-02-01 23:04:09 +00001681 EMIT_ARG(binary_op, RT_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001682 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001683 }
1684
1685 EMIT(pop_top);
1686
1687 if (qstr_exception_local == 0) {
1688 EMIT(pop_top);
1689 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001690 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001691 }
1692
1693 EMIT(pop_top);
1694
Damiene2880aa2013-12-20 14:22:59 +00001695 int l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001696 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001697 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001698 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001699 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001700 }
1701 compile_node(comp, pns_except->nodes[1]);
1702 if (qstr_exception_local != 0) {
1703 EMIT(pop_block);
1704 }
1705 EMIT(pop_except);
1706 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001707 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1708 EMIT_ARG(label_assign, l3);
1709 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1710 EMIT_ARG(store_id, qstr_exception_local);
1711 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001712
Damien George8dcc0c72014-03-27 10:55:21 +00001713 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001714 EMIT(end_finally);
1715 }
Damien Georgeb9791222014-01-23 00:34:21 +00001716 EMIT_ARG(jump, l2);
1717 EMIT_ARG(label_assign, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001718 }
1719
Damien George8dcc0c72014-03-27 10:55:21 +00001720 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001721 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001722
Damien Georgeb9791222014-01-23 00:34:21 +00001723 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001724 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001725 EMIT_ARG(label_assign, l2);
1726 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001727}
1728
Damiend99b0522013-12-21 18:17:45 +00001729void 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 +01001730 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1731 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001732 int l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001733
Damien Georgeb9791222014-01-23 00:34:21 +00001734 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001735 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001736
Damien429d7192013-10-04 19:53:11 +01001737 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001738 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien429d7192013-10-04 19:53:11 +01001739 compile_node(comp, pn_body);
1740 } else {
1741 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1742 }
1743 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001744 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1745 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001746 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001747
Damien George8dcc0c72014-03-27 10:55:21 +00001748 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001749 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001750
Damien Georgeb9791222014-01-23 00:34:21 +00001751 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001752}
1753
Damiend99b0522013-12-21 18:17:45 +00001754void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1755 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1756 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1757 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001758 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001759 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1760 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001761 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001762 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001763 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001764 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001765 // no finally
1766 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1767 } else {
1768 // have finally
Damiend99b0522013-12-21 18:17:45 +00001769 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 +01001770 }
1771 } else {
1772 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001773 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001774 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001775 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001776 }
1777 } else {
1778 // shouldn't happen
1779 assert(0);
1780 }
1781}
1782
Damiend99b0522013-12-21 18:17:45 +00001783void 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 +01001784 if (n == 0) {
1785 // no more pre-bits, compile the body of the with
1786 compile_node(comp, body);
1787 } else {
Damienb05d7072013-10-05 13:37:10 +01001788 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001789 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001790 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001791 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001792 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001793 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001794 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1795 } else {
1796 // this pre-bit is just an expression
1797 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001798 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001799 EMIT(pop_top);
1800 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001801 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001802 // compile additional pre-bits and the body
1803 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1804 // finish this with block
1805 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001806 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1807 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001808 EMIT(with_cleanup);
Paul Sokolovsky44307d52014-03-29 04:10:11 +02001809 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001810 EMIT(end_finally);
1811 }
1812}
1813
Damiend99b0522013-12-21 18:17:45 +00001814void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001815 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001816 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001817 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1818 assert(n > 0);
1819
1820 // compile in a nested fashion
1821 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1822}
1823
Damiend99b0522013-12-21 18:17:45 +00001824void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1825 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001826 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1827 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001828 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001829 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001830 EMIT_ARG(call_function, 1, 0, false, false);
Damien5ac1b2e2013-10-18 19:58:12 +01001831 EMIT(pop_top);
1832
Damien429d7192013-10-04 19:53:11 +01001833 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001834 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001835 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001836 // do nothing with a lonely constant
1837 } else {
1838 compile_node(comp, pns->nodes[0]); // just an expression
1839 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1840 }
Damien429d7192013-10-04 19:53:11 +01001841 }
1842 } else {
Damiend99b0522013-12-21 18:17:45 +00001843 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1844 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001845 if (kind == PN_expr_stmt_augassign) {
1846 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1847 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001848 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien George7e5fb242014-02-01 22:18:47 +00001849 rt_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001850 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien George7e5fb242014-02-01 22:18:47 +00001851 case MP_TOKEN_DEL_PIPE_EQUAL: op = RT_BINARY_OP_INPLACE_OR; break;
1852 case MP_TOKEN_DEL_CARET_EQUAL: op = RT_BINARY_OP_INPLACE_XOR; break;
1853 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = RT_BINARY_OP_INPLACE_AND; break;
1854 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = RT_BINARY_OP_INPLACE_LSHIFT; break;
1855 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = RT_BINARY_OP_INPLACE_RSHIFT; break;
1856 case MP_TOKEN_DEL_PLUS_EQUAL: op = RT_BINARY_OP_INPLACE_ADD; break;
1857 case MP_TOKEN_DEL_MINUS_EQUAL: op = RT_BINARY_OP_INPLACE_SUBTRACT; break;
1858 case MP_TOKEN_DEL_STAR_EQUAL: op = RT_BINARY_OP_INPLACE_MULTIPLY; break;
1859 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = RT_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1860 case MP_TOKEN_DEL_SLASH_EQUAL: op = RT_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1861 case MP_TOKEN_DEL_PERCENT_EQUAL: op = RT_BINARY_OP_INPLACE_MODULO; break;
1862 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = RT_BINARY_OP_INPLACE_POWER; break;
1863 default: assert(0); op = RT_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01001864 }
Damien George7e5fb242014-02-01 22:18:47 +00001865 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001866 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1867 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001868 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1869 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001870 // following CPython, we store left-most first
1871 if (rhs > 0) {
1872 EMIT(dup_top);
1873 }
1874 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1875 for (int i = 0; i < rhs; i++) {
1876 if (i + 1 < rhs) {
1877 EMIT(dup_top);
1878 }
Damiend99b0522013-12-21 18:17:45 +00001879 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001880 }
1881 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001882 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1883 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1884 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1885 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01001886 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001887 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1888 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001889 compile_node(comp, pns10->nodes[0]); // rhs
1890 compile_node(comp, pns10->nodes[1]); // rhs
1891 EMIT(rot_two);
1892 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1893 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00001894 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1895 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1896 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1897 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01001898 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001899 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1900 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001901 compile_node(comp, pns10->nodes[0]); // rhs
1902 compile_node(comp, pns10->nodes[1]); // rhs
1903 compile_node(comp, pns10->nodes[2]); // rhs
1904 EMIT(rot_three);
1905 EMIT(rot_two);
1906 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1907 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1908 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1909 } else {
1910 compile_node(comp, pns1->nodes[0]); // rhs
1911 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1912 }
1913 } else {
1914 // shouldn't happen
1915 assert(0);
1916 }
1917 }
1918}
1919
Damiend99b0522013-12-21 18:17:45 +00001920void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, rt_binary_op_t binary_op) {
1921 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001922 compile_node(comp, pns->nodes[0]);
1923 for (int i = 1; i < num_nodes; i += 1) {
1924 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001925 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001926 }
1927}
1928
Damiend99b0522013-12-21 18:17:45 +00001929void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1930 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1931 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001932
1933 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001934 int l_fail = comp_next_label(comp);
1935 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001936 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1937 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001938 EMIT_ARG(jump, l_end);
1939 EMIT_ARG(label_assign, l_fail);
1940 EMIT_ARG(set_stack_size, stack_size); // force stack size reset
Damien429d7192013-10-04 19:53:11 +01001941 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001942 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001943}
1944
Damiend99b0522013-12-21 18:17:45 +00001945void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001946 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00001947 //mp_parse_node_t pn_params = pns->nodes[0];
1948 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001949
1950 if (comp->pass == PASS_1) {
1951 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00001952 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 +01001953 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001954 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001955 }
1956
1957 // get the scope for this lambda
1958 scope_t *this_scope = (scope_t*)pns->nodes[2];
1959
1960 // make the lambda
1961 close_over_variables_etc(comp, this_scope, 0, 0);
1962}
1963
Damiend99b0522013-12-21 18:17:45 +00001964void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001965 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001966 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001967 for (int i = 0; i < n; i += 1) {
1968 compile_node(comp, pns->nodes[i]);
1969 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00001970 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01001971 }
1972 }
Damien Georgeb9791222014-01-23 00:34:21 +00001973 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001974}
1975
Damiend99b0522013-12-21 18:17:45 +00001976void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001977 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001978 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001979 for (int i = 0; i < n; i += 1) {
1980 compile_node(comp, pns->nodes[i]);
1981 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00001982 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01001983 }
1984 }
Damien Georgeb9791222014-01-23 00:34:21 +00001985 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001986}
1987
Damiend99b0522013-12-21 18:17:45 +00001988void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001989 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001990 EMIT_ARG(unary_op, RT_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01001991}
1992
Damiend99b0522013-12-21 18:17:45 +00001993void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001994 int stack_size = EMIT(get_stack_size);
Damiend99b0522013-12-21 18:17:45 +00001995 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001996 compile_node(comp, pns->nodes[0]);
1997 bool multi = (num_nodes > 3);
1998 int l_fail = 0;
1999 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002000 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002001 }
2002 for (int i = 1; i + 1 < num_nodes; i += 2) {
2003 compile_node(comp, pns->nodes[i + 1]);
2004 if (i + 2 < num_nodes) {
2005 EMIT(dup_top);
2006 EMIT(rot_three);
2007 }
Damien George7e5fb242014-02-01 22:18:47 +00002008 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
2009 rt_binary_op_t op;
2010 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien George9aa2a522014-02-01 23:04:09 +00002011 case MP_TOKEN_OP_LESS: op = RT_BINARY_OP_LESS; break;
2012 case MP_TOKEN_OP_MORE: op = RT_BINARY_OP_MORE; break;
2013 case MP_TOKEN_OP_DBL_EQUAL: op = RT_BINARY_OP_EQUAL; break;
2014 case MP_TOKEN_OP_LESS_EQUAL: op = RT_BINARY_OP_LESS_EQUAL; break;
2015 case MP_TOKEN_OP_MORE_EQUAL: op = RT_BINARY_OP_MORE_EQUAL; break;
2016 case MP_TOKEN_OP_NOT_EQUAL: op = RT_BINARY_OP_NOT_EQUAL; break;
2017 case MP_TOKEN_KW_IN: op = RT_BINARY_OP_IN; break;
2018 default: assert(0); op = RT_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002019 }
2020 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002021 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2022 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2023 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002024 if (kind == PN_comp_op_not_in) {
Damien George9aa2a522014-02-01 23:04:09 +00002025 EMIT_ARG(binary_op, RT_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002026 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002027 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien George9aa2a522014-02-01 23:04:09 +00002028 EMIT_ARG(binary_op, RT_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002029 } else {
Damien George9aa2a522014-02-01 23:04:09 +00002030 EMIT_ARG(binary_op, RT_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002031 }
2032 } else {
2033 // shouldn't happen
2034 assert(0);
2035 }
2036 } else {
2037 // shouldn't happen
2038 assert(0);
2039 }
2040 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002041 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002042 }
2043 }
2044 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002045 int l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002046 EMIT_ARG(jump, l_end);
2047 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01002048 EMIT(rot_two);
2049 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002050 EMIT_ARG(label_assign, l_end);
2051 EMIT_ARG(set_stack_size, stack_size + 1); // force stack size
Damien429d7192013-10-04 19:53:11 +01002052 }
2053}
2054
Damiend99b0522013-12-21 18:17:45 +00002055void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002056 // TODO
2057 assert(0);
2058 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002059 //EMIT_ARG(unary_op, "UNARY_STAR");
Damien429d7192013-10-04 19:53:11 +01002060}
2061
Damiend99b0522013-12-21 18:17:45 +00002062void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002063 c_binary_op(comp, pns, RT_BINARY_OP_OR);
2064}
2065
Damiend99b0522013-12-21 18:17:45 +00002066void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002067 c_binary_op(comp, pns, RT_BINARY_OP_XOR);
2068}
2069
Damiend99b0522013-12-21 18:17:45 +00002070void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002071 c_binary_op(comp, pns, RT_BINARY_OP_AND);
2072}
2073
Damiend99b0522013-12-21 18:17:45 +00002074void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2075 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002076 compile_node(comp, pns->nodes[0]);
2077 for (int i = 1; i + 1 < num_nodes; i += 2) {
2078 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002079 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002080 EMIT_ARG(binary_op, RT_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002081 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002082 EMIT_ARG(binary_op, RT_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002083 } else {
2084 // shouldn't happen
2085 assert(0);
2086 }
2087 }
2088}
2089
Damiend99b0522013-12-21 18:17:45 +00002090void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2091 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002092 compile_node(comp, pns->nodes[0]);
2093 for (int i = 1; i + 1 < num_nodes; i += 2) {
2094 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002095 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002096 EMIT_ARG(binary_op, RT_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002097 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002098 EMIT_ARG(binary_op, RT_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002099 } else {
2100 // shouldn't happen
2101 assert(0);
2102 }
2103 }
2104}
2105
Damiend99b0522013-12-21 18:17:45 +00002106void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2107 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002108 compile_node(comp, pns->nodes[0]);
2109 for (int i = 1; i + 1 < num_nodes; i += 2) {
2110 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002111 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002112 EMIT_ARG(binary_op, RT_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002113 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002114 EMIT_ARG(binary_op, RT_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002115 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002116 EMIT_ARG(binary_op, RT_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002117 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002118 EMIT_ARG(binary_op, RT_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002119 } else {
2120 // shouldn't happen
2121 assert(0);
2122 }
2123 }
2124}
2125
Damiend99b0522013-12-21 18:17:45 +00002126void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002127 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002128 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002129 EMIT_ARG(unary_op, RT_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002130 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002131 EMIT_ARG(unary_op, RT_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002132 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002133 EMIT_ARG(unary_op, RT_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002134 } else {
2135 // shouldn't happen
2136 assert(0);
2137 }
2138}
2139
Damien George35e2a4e2014-02-05 00:51:47 +00002140void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2141 // this is to handle special super() call
2142 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2143
2144 compile_generic_all_nodes(comp, pns);
2145}
2146
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002147STATIC 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 +01002148 // function to call is on top of stack
2149
Damien George35e2a4e2014-02-05 00:51:47 +00002150#if !MICROPY_EMIT_CPYTHON
2151 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002152 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 +00002153 EMIT_ARG(load_id, MP_QSTR___class__);
2154 // get first argument to function
2155 bool found = false;
2156 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
2157 if (comp->scope_cur->id_info[i].param) {
2158 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
2159 found = true;
2160 break;
2161 }
2162 }
2163 if (!found) {
2164 printf("TypeError: super() call cannot find self\n");
2165 return;
2166 }
2167 EMIT_ARG(call_function, 2, 0, false, false);
2168 return;
2169 }
2170#endif
2171
Damien429d7192013-10-04 19:53:11 +01002172 int old_n_arg_keyword = comp->n_arg_keyword;
2173 bool old_have_star_arg = comp->have_star_arg;
2174 bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
2175 comp->n_arg_keyword = 0;
2176 comp->have_star_arg = false;
2177 comp->have_dbl_star_arg = false;
2178
Damien Georgebbcd49a2014-02-06 20:30:16 +00002179 compile_node(comp, pn_arglist); // arguments to function call; can be null
Damien429d7192013-10-04 19:53:11 +01002180
2181 // compute number of positional arguments
Damien Georgebbcd49a2014-02-06 20:30:16 +00002182 int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
Damien429d7192013-10-04 19:53:11 +01002183 if (comp->have_star_arg) {
2184 n_positional -= 1;
2185 }
2186 if (comp->have_dbl_star_arg) {
2187 n_positional -= 1;
2188 }
2189
2190 if (is_method_call) {
Damien Georgeb9791222014-01-23 00:34:21 +00002191 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 +01002192 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002193 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 +01002194 }
2195
2196 comp->n_arg_keyword = old_n_arg_keyword;
2197 comp->have_star_arg = old_have_star_arg;
2198 comp->have_dbl_star_arg = old_have_dbl_star_arg;
2199}
2200
Damiend99b0522013-12-21 18:17:45 +00002201void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2202 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002203 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002204 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 +01002205 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002206 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2207 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002208 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002209 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002210 i += 1;
2211 } else {
2212 compile_node(comp, pns->nodes[i]);
2213 }
Damien George35e2a4e2014-02-05 00:51:47 +00002214 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002215 }
2216}
2217
Damiend99b0522013-12-21 18:17:45 +00002218void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002219 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002220 EMIT_ARG(binary_op, RT_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002221}
2222
Damiend99b0522013-12-21 18:17:45 +00002223void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002224 // a list of strings
Damien63321742013-12-10 17:41:49 +00002225
2226 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002227 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002228 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002229 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002230 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002231 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2232 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2233 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002234 if (i == 0) {
2235 string_kind = pn_kind;
2236 } else if (pn_kind != string_kind) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002237 compile_syntax_error(comp, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002238 return;
2239 }
Damien George55baff42014-01-21 21:40:13 +00002240 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002241 }
Damien63321742013-12-10 17:41:49 +00002242
Damien63321742013-12-10 17:41:49 +00002243 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002244 byte *q_ptr;
2245 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002246 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002247 uint s_len;
2248 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002249 memcpy(s_dest, s, s_len);
2250 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002251 }
Damien George55baff42014-01-21 21:40:13 +00002252 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002253
Damien Georgeb9791222014-01-23 00:34:21 +00002254 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002255}
2256
2257// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002258void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2259 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2260 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2261 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002262
2263 if (comp->pass == PASS_1) {
2264 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002265 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 +01002266 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002267 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002268 }
2269
2270 // get the scope for this comprehension
2271 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2272
2273 // compile the comprehension
2274 close_over_variables_etc(comp, this_scope, 0, 0);
2275
2276 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2277 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002278 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01002279}
2280
Damiend99b0522013-12-21 18:17:45 +00002281void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2282 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002283 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002284 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2285 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2286 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2287 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2288 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2289 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2290 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002291 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002292 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002293 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002294 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002295 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002296 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002297 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002298 // generator expression
2299 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2300 } else {
2301 // tuple with 2 items
2302 goto tuple_with_2_items;
2303 }
2304 } else {
2305 // tuple with 2 items
2306 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002307 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002308 }
2309 } else {
2310 // parenthesis around a single item, is just that item
2311 compile_node(comp, pns->nodes[0]);
2312 }
2313}
2314
Damiend99b0522013-12-21 18:17:45 +00002315void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2316 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002317 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002318 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002319 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2320 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2321 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2322 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2323 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002324 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002325 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002326 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002327 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002328 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002329 // list of many items
2330 compile_node(comp, pns2->nodes[0]);
2331 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002332 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002333 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002334 // list comprehension
2335 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2336 } else {
2337 // list with 2 items
2338 goto list_with_2_items;
2339 }
2340 } else {
2341 // list with 2 items
2342 list_with_2_items:
2343 compile_node(comp, pns2->nodes[0]);
2344 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002345 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002346 }
2347 } else {
2348 // list with 1 item
2349 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002350 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002351 }
2352}
2353
Damiend99b0522013-12-21 18:17:45 +00002354void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2355 mp_parse_node_t pn = pns->nodes[0];
2356 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002357 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002358 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002359 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2360 pns = (mp_parse_node_struct_t*)pn;
2361 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002362 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002363 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002364 compile_node(comp, pn);
2365 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002366 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2367 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2368 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2369 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002370 // dict/set with multiple elements
2371
2372 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002373 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002374 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2375
2376 // first element sets whether it's a dict or set
2377 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002378 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002379 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002380 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002381 compile_node(comp, pns->nodes[0]);
2382 EMIT(store_map);
2383 is_dict = true;
2384 } else {
2385 // a set
2386 compile_node(comp, pns->nodes[0]); // 1st value of set
2387 is_dict = false;
2388 }
2389
2390 // process rest of elements
2391 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002392 mp_parse_node_t pn = nodes[i];
2393 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002394 compile_node(comp, pn);
2395 if (is_dict) {
2396 if (!is_key_value) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002397 compile_syntax_error(comp, "?expecting key:value for dictiona");
Damien429d7192013-10-04 19:53:11 +01002398 return;
2399 }
2400 EMIT(store_map);
2401 } else {
2402 if (is_key_value) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002403 compile_syntax_error(comp, "?expecting just a value for s");
Damien429d7192013-10-04 19:53:11 +01002404 return;
2405 }
2406 }
2407 }
2408
2409 // if it's a set, build it
2410 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002411 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002412 }
Damiend99b0522013-12-21 18:17:45 +00002413 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002414 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002415 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002416 // a dictionary comprehension
2417 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2418 } else {
2419 // a set comprehension
2420 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2421 }
2422 } else {
2423 // shouldn't happen
2424 assert(0);
2425 }
2426 } else {
2427 // set with one element
2428 goto set_with_one_element;
2429 }
2430 } else {
2431 // set with one element
2432 set_with_one_element:
2433 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002434 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002435 }
2436}
2437
Damiend99b0522013-12-21 18:17:45 +00002438void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002439 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002440}
2441
Damiend99b0522013-12-21 18:17:45 +00002442void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002443 // object who's index we want is on top of stack
2444 compile_node(comp, pns->nodes[0]); // the index
Damien Georgeb9791222014-01-23 00:34:21 +00002445 EMIT_ARG(binary_op, RT_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002446}
2447
Damiend99b0522013-12-21 18:17:45 +00002448void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002449 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002450 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002451}
2452
Damiend99b0522013-12-21 18:17:45 +00002453void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2454 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2455 mp_parse_node_t pn = pns->nodes[0];
2456 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002457 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002458 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2459 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002460 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2461 pns = (mp_parse_node_struct_t*)pn;
2462 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002463 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002464 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002465 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002466 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002467 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002468 } else {
2469 // [?::x]
2470 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002471 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002472 }
Damiend99b0522013-12-21 18:17:45 +00002473 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002474 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002475 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2476 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2477 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2478 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002479 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002480 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002481 } else {
2482 // [?:x:x]
2483 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002484 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002485 }
2486 } else {
2487 // [?:x]
2488 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002489 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002490 }
2491 } else {
2492 // [?:x]
2493 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002494 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002495 }
2496}
2497
Damiend99b0522013-12-21 18:17:45 +00002498void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002499 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002500 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2501 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002502}
2503
Damiend99b0522013-12-21 18:17:45 +00002504void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002505 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002506 compile_subscript_3_helper(comp, pns);
2507}
2508
Damiend99b0522013-12-21 18:17:45 +00002509void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002510 // if this is called then we are compiling a dict key:value pair
2511 compile_node(comp, pns->nodes[1]); // value
2512 compile_node(comp, pns->nodes[0]); // key
2513}
2514
Damiend99b0522013-12-21 18:17:45 +00002515void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002516 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002517 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002518 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002519}
2520
Damiend99b0522013-12-21 18:17:45 +00002521void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002522 if (comp->have_star_arg) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002523 compile_syntax_error(comp, "?can't have multiple *x");
Damien429d7192013-10-04 19:53:11 +01002524 return;
2525 }
2526 comp->have_star_arg = true;
2527 compile_node(comp, pns->nodes[0]);
2528}
2529
Damiend99b0522013-12-21 18:17:45 +00002530void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002531 if (comp->have_dbl_star_arg) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002532 compile_syntax_error(comp, "?can't have multiple **x");
Damien429d7192013-10-04 19:53:11 +01002533 return;
2534 }
2535 comp->have_dbl_star_arg = true;
2536 compile_node(comp, pns->nodes[0]);
2537}
2538
Damiend99b0522013-12-21 18:17:45 +00002539void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2540 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2541 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2542 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2543 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002544 compile_syntax_error(comp, "?lhs of keyword argument must be an id");
Damien429d7192013-10-04 19:53:11 +01002545 return;
2546 }
Damien Georgeb9791222014-01-23 00:34:21 +00002547 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002548 compile_node(comp, pns2->nodes[0]);
2549 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002550 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002551 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2552 } else {
2553 // shouldn't happen
2554 assert(0);
2555 }
2556}
2557
Damiend99b0522013-12-21 18:17:45 +00002558void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002559 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002560 compile_syntax_error(comp, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002561 return;
2562 }
Damiend99b0522013-12-21 18:17:45 +00002563 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002564 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002565 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002566 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2567 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002568 compile_node(comp, pns->nodes[0]);
2569 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002570 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002571 EMIT(yield_from);
2572 } else {
2573 compile_node(comp, pns->nodes[0]);
2574 EMIT(yield_value);
2575 }
2576}
2577
Damiend99b0522013-12-21 18:17:45 +00002578typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002579STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002580 NULL,
2581#define nc NULL
2582#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002583#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002584#include "grammar.h"
2585#undef nc
2586#undef c
2587#undef DEF_RULE
2588};
2589
Damiend99b0522013-12-21 18:17:45 +00002590void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2591 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002592 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002593 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2594 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2595 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002596 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002597 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002598 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002599 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002600 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2601 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2602 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2603 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002604 case MP_PARSE_NODE_TOKEN:
2605 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002606 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002607 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002608 // do nothing
2609 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002610 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002611 }
2612 break;
Damien429d7192013-10-04 19:53:11 +01002613 default: assert(0);
2614 }
2615 } else {
Damiend99b0522013-12-21 18:17:45 +00002616 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002617 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002618 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002619 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002620 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002621#if MICROPY_DEBUG_PRINTERS
2622 mp_parse_node_print(pn, 0);
2623#endif
Damien429d7192013-10-04 19:53:11 +01002624 assert(0);
2625 } else {
2626 f(comp, pns);
2627 }
2628 }
2629}
2630
Damiend99b0522013-12-21 18:17:45 +00002631void 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 +01002632 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002633 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002634 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2635 if (MP_PARSE_NODE_IS_ID(pn)) {
2636 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002637 if (comp->have_bare_star) {
2638 // comes after a bare star, so doesn't count as a parameter
2639 } else {
2640 comp->scope_cur->num_params += 1;
2641 }
Damienb14de212013-10-06 00:28:28 +01002642 } else {
Damiend99b0522013-12-21 18:17:45 +00002643 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2644 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2645 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2646 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002647 //int node_index = 1; unused
2648 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002649 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002650 // this parameter has an annotation
2651 pn_annotation = pns->nodes[1];
2652 }
2653 //node_index = 2; unused
2654 }
2655 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002656 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002657 // this parameter has a default value
2658 if (comp->have_bare_star) {
2659 comp->scope_cur->num_dict_params += 1;
2660 } else {
2661 comp->scope_cur->num_default_params += 1;
2662 }
2663 }
2664 */
2665 if (comp->have_bare_star) {
2666 // comes after a bare star, so doesn't count as a parameter
2667 } else {
2668 comp->scope_cur->num_params += 1;
2669 }
Damiend99b0522013-12-21 18:17:45 +00002670 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2671 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002672 // bare star
2673 // TODO see http://www.python.org/dev/peps/pep-3102/
2674 comp->have_bare_star = true;
2675 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002676 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002677 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002678 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002679 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2680 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002681 // named star with annotation
Damien George8725f8f2014-02-15 19:33:11 +00002682 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002683 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2684 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002685 pn_annotation = pns->nodes[1];
2686 } else {
2687 // shouldn't happen
2688 assert(0);
2689 }
Damiend99b0522013-12-21 18:17:45 +00002690 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2691 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2692 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002693 // this parameter has an annotation
2694 pn_annotation = pns->nodes[1];
2695 }
Damien George8725f8f2014-02-15 19:33:11 +00002696 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002697 } else {
Damienb14de212013-10-06 00:28:28 +01002698 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002699 assert(0);
2700 }
Damien429d7192013-10-04 19:53:11 +01002701 }
2702
2703 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002704 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002705 // TODO this parameter has an annotation
2706 }
2707 bool added;
2708 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2709 if (!added) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002710 compile_syntax_error(comp, "?same name used for parameter");
Damien429d7192013-10-04 19:53:11 +01002711 return;
2712 }
2713 id_info->param = true;
2714 id_info->kind = ID_INFO_KIND_LOCAL;
2715 }
2716}
2717
Damiend99b0522013-12-21 18:17:45 +00002718void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002719 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2720}
2721
Damiend99b0522013-12-21 18:17:45 +00002722void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002723 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2724}
2725
Damiend99b0522013-12-21 18:17:45 +00002726void 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 +01002727 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002728 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002729 // no more nested if/for; compile inner expression
2730 compile_node(comp, pn_inner_expr);
2731 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002732 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002733 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002734 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002735 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002736 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002737 } else {
2738 EMIT(yield_value);
2739 EMIT(pop_top);
2740 }
Damiend99b0522013-12-21 18:17:45 +00002741 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002742 // if condition
Damiend99b0522013-12-21 18:17:45 +00002743 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002744 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2745 pn_iter = pns_comp_if->nodes[1];
2746 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002747 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002748 // for loop
Damiend99b0522013-12-21 18:17:45 +00002749 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002750 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002751 int l_end2 = comp_next_label(comp);
2752 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002753 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002754 EMIT_ARG(label_assign, l_top2);
2755 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002756 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2757 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 +00002758 EMIT_ARG(jump, l_top2);
2759 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002760 EMIT(for_iter_end);
2761 } else {
2762 // shouldn't happen
2763 assert(0);
2764 }
2765}
2766
Damiend99b0522013-12-21 18:17:45 +00002767void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002768 // see http://www.python.org/dev/peps/pep-0257/
2769
2770 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002771 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002772 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002773 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002774 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002775 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2776 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002777 for (int i = 0; i < num_nodes; i++) {
2778 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002779 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 +00002780 // not a newline, so this is the first statement; finish search
2781 break;
2782 }
2783 }
2784 // 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 +00002785 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002786 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002787 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002788 } else {
2789 return;
2790 }
2791
2792 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002793 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2794 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2795 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2796 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2797 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002798 compile_node(comp, pns->nodes[0]); // a doc string
2799 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002800 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002801 }
2802 }
2803 }
2804}
2805
2806void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2807 comp->pass = pass;
2808 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002809 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002810 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002811
2812 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002813 // reset maximum stack sizes in scope
2814 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002815 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002816 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002817 }
2818
Damien5ac1b2e2013-10-18 19:58:12 +01002819#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002820 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002821 scope_print_info(scope);
2822 }
Damien5ac1b2e2013-10-18 19:58:12 +01002823#endif
Damien429d7192013-10-04 19:53:11 +01002824
2825 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002826 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2827 assert(scope->kind == SCOPE_MODULE);
2828 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2829 compile_node(comp, pns->nodes[0]); // compile the expression
2830 EMIT(return_value);
2831 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002832 if (!comp->is_repl) {
2833 check_for_doc_string(comp, scope->pn);
2834 }
Damien429d7192013-10-04 19:53:11 +01002835 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002836 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002837 EMIT(return_value);
2838 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002839 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2840 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2841 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002842
2843 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002844 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002845 if (comp->pass == PASS_1) {
2846 comp->have_bare_star = false;
2847 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2848 }
2849
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02002850 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01002851
2852 compile_node(comp, pns->nodes[3]); // 3 is function body
2853 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002854 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002855 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002856 EMIT(return_value);
2857 }
2858 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002859 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2860 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2861 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002862
2863 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002864 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002865 if (comp->pass == PASS_1) {
2866 comp->have_bare_star = false;
2867 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2868 }
2869
2870 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2871 EMIT(return_value);
2872 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2873 // a bit of a hack at the moment
2874
Damiend99b0522013-12-21 18:17:45 +00002875 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2876 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2877 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2878 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2879 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002880
Damien George55baff42014-01-21 21:40:13 +00002881 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01002882 if (comp->pass == PASS_1) {
2883 bool added;
2884 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2885 assert(added);
2886 id_info->kind = ID_INFO_KIND_LOCAL;
2887 scope->num_params = 1;
2888 }
2889
2890 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002891 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01002892 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002893 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01002894 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002895 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01002896 }
2897
Damienb05d7072013-10-05 13:37:10 +01002898 int l_end = comp_next_label(comp);
2899 int l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002900 EMIT_ARG(load_id, qstr_arg);
2901 EMIT_ARG(label_assign, l_top);
2902 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01002903 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2904 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00002905 EMIT_ARG(jump, l_top);
2906 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002907 EMIT(for_iter_end);
2908
2909 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00002910 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002911 }
2912 EMIT(return_value);
2913 } else {
2914 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002915 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2916 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2917 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01002918
2919 if (comp->pass == PASS_1) {
2920 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002921 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01002922 assert(added);
2923 id_info->kind = ID_INFO_KIND_LOCAL;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002924 id_info = scope_find_or_add_id(scope, MP_QSTR___locals__, &added);
Damien429d7192013-10-04 19:53:11 +01002925 assert(added);
2926 id_info->kind = ID_INFO_KIND_LOCAL;
2927 id_info->param = true;
2928 scope->num_params = 1; // __locals__ is the parameter
2929 }
2930
Damien Georgeb9791222014-01-23 00:34:21 +00002931 EMIT_ARG(load_id, MP_QSTR___locals__);
Damien429d7192013-10-04 19:53:11 +01002932 EMIT(store_locals);
Damien Georgeb9791222014-01-23 00:34:21 +00002933 EMIT_ARG(load_id, MP_QSTR___name__);
2934 EMIT_ARG(store_id, MP_QSTR___module__);
2935 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
2936 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01002937
2938 check_for_doc_string(comp, pns->nodes[2]);
2939 compile_node(comp, pns->nodes[2]); // 2 is class body
2940
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002941 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01002942 assert(id != NULL);
2943 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00002944 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002945 } else {
Damien George6baf76e2013-12-30 22:32:17 +00002946#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00002947 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002948#else
Damien George35e2a4e2014-02-05 00:51:47 +00002949 EMIT_ARG(load_fast, MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00002950#endif
Damien429d7192013-10-04 19:53:11 +01002951 }
2952 EMIT(return_value);
2953 }
2954
Damien415eb6f2013-10-05 12:19:06 +01002955 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00002956
2957 // make sure we match all the exception levels
2958 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01002959}
2960
2961void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2962 comp->pass = pass;
2963 comp->scope_cur = scope;
2964 comp->next_label = 1;
2965
2966 if (scope->kind != SCOPE_FUNCTION) {
2967 printf("Error: inline assembler must be a function\n");
2968 return;
2969 }
2970
Damiena2f2f7d2013-10-06 00:14:13 +01002971 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00002972 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01002973 }
2974
Damien826005c2013-10-05 23:17:28 +01002975 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00002976 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2977 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2978 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01002979
Damiend99b0522013-12-21 18:17:45 +00002980 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01002981
Damiena2f2f7d2013-10-06 00:14:13 +01002982 // parameters are in pns->nodes[1]
2983 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00002984 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01002985 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00002986 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01002987 }
2988
Damiend99b0522013-12-21 18:17:45 +00002989 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01002990
Damiend99b0522013-12-21 18:17:45 +00002991 mp_parse_node_t pn_body = pns->nodes[3]; // body
2992 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01002993 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
2994
Damien Georgecbd2f742014-01-19 11:48:48 +00002995 /*
Damien826005c2013-10-05 23:17:28 +01002996 if (comp->pass == PASS_3) {
2997 //printf("----\n");
2998 scope_print_info(scope);
2999 }
Damien Georgecbd2f742014-01-19 11:48:48 +00003000 */
Damien826005c2013-10-05 23:17:28 +01003001
3002 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003003 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3004 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
3005 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
3006 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3007 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3008 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3009 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3010 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3011 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3012 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3013 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3014 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3015 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003016 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3017
3018 // emit instructions
3019 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00003020 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgef41fdd02014-03-03 23:19:11 +00003021 compile_syntax_error(comp, "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003022 return;
3023 }
3024 int lab = comp_next_label(comp);
3025 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003026 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003027 }
3028 } else {
3029 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003030 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003031 }
3032 }
3033 }
3034
3035 if (comp->pass > PASS_1) {
3036 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01003037 }
Damien429d7192013-10-04 19:53:11 +01003038}
3039
3040void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3041 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003042 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003043 scope->num_locals = 0;
3044 for (int i = 0; i < scope->id_info_len; i++) {
3045 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003046 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003047 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3048 continue;
3049 }
3050 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3051 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3052 }
Damien9ecbcff2013-12-11 00:41:43 +00003053 // note: params always count for 1 local, even if they are a cell
Damien429d7192013-10-04 19:53:11 +01003054 if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
3055 id->local_num = scope->num_locals;
3056 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003057 }
3058 }
3059
3060 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003061#if MICROPY_EMIT_CPYTHON
3062 int num_cell = 0;
3063#endif
Damien9ecbcff2013-12-11 00:41:43 +00003064 for (int i = 0; i < scope->id_info_len; i++) {
3065 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003066#if MICROPY_EMIT_CPYTHON
3067 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003068 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003069 id->local_num = num_cell;
3070 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003071 }
Damien George6baf76e2013-12-30 22:32:17 +00003072#else
3073 // in Micro Python the cells come right after the fast locals
3074 // parameters are not counted here, since they remain at the start
3075 // of the locals, even if they are cell vars
3076 if (!id->param && id->kind == ID_INFO_KIND_CELL) {
3077 id->local_num = scope->num_locals;
3078 scope->num_locals += 1;
3079 }
3080#endif
Damien9ecbcff2013-12-11 00:41:43 +00003081 }
Damien9ecbcff2013-12-11 00:41:43 +00003082
3083 // compute the index of free vars (freevars[idx] in CPython)
3084 // make sure they are in the order of the parent scope
3085 if (scope->parent != NULL) {
3086 int num_free = 0;
3087 for (int i = 0; i < scope->parent->id_info_len; i++) {
3088 id_info_t *id = &scope->parent->id_info[i];
3089 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3090 for (int j = 0; j < scope->id_info_len; j++) {
3091 id_info_t *id2 = &scope->id_info[j];
3092 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +00003093 assert(!id2->param); // free vars should not be params
3094#if MICROPY_EMIT_CPYTHON
3095 // in CPython the frees are numbered after the cells
3096 id2->local_num = num_cell + num_free;
3097#else
3098 // in Micro Python the frees come first, before the params
3099 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003100#endif
3101 num_free += 1;
3102 }
3103 }
3104 }
Damien429d7192013-10-04 19:53:11 +01003105 }
Damien George6baf76e2013-12-30 22:32:17 +00003106#if !MICROPY_EMIT_CPYTHON
3107 // in Micro Python shift all other locals after the free locals
3108 if (num_free > 0) {
3109 for (int i = 0; i < scope->id_info_len; i++) {
3110 id_info_t *id = &scope->id_info[i];
3111 if (id->param || id->kind != ID_INFO_KIND_FREE) {
3112 id->local_num += num_free;
3113 }
3114 }
3115 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3116 scope->num_locals += num_free;
3117 }
3118#endif
Damien429d7192013-10-04 19:53:11 +01003119 }
3120
Damien George8725f8f2014-02-15 19:33:11 +00003121 // compute scope_flags
3122 //scope->scope_flags = 0; since we set some things in parameters
Damien429d7192013-10-04 19:53:11 +01003123 if (scope->kind != SCOPE_MODULE) {
Damien George8725f8f2014-02-15 19:33:11 +00003124 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien429d7192013-10-04 19:53:11 +01003125 }
3126 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) {
3127 assert(scope->parent != NULL);
Damien George8725f8f2014-02-15 19:33:11 +00003128 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien429d7192013-10-04 19:53:11 +01003129
3130 // TODO possibly other ways it can be nested
Damien George08d07552014-01-29 18:58:52 +00003131 // Note that we don't actually use this information at the moment (for CPython compat only)
3132 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 +00003133 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003134 }
3135 }
3136 int num_free = 0;
3137 for (int i = 0; i < scope->id_info_len; i++) {
3138 id_info_t *id = &scope->id_info[i];
3139 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3140 num_free += 1;
3141 }
3142 }
3143 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003144 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003145 }
3146}
3147
Damien George08335002014-01-18 23:24:36 +00003148mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
Damien429d7192013-10-04 19:53:11 +01003149 compiler_t *comp = m_new(compiler_t, 1);
3150
Damien Georgecbd2f742014-01-19 11:48:48 +00003151 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003152 comp->is_repl = is_repl;
3153 comp->had_error = false;
3154
Damien429d7192013-10-04 19:53:11 +01003155 comp->break_label = 0;
3156 comp->continue_label = 0;
Damien Georgecbddb272014-02-01 20:08:18 +00003157 comp->break_continue_except_level = 0;
3158 comp->cur_except_level = 0;
3159
Damien George35e2a4e2014-02-05 00:51:47 +00003160 comp->func_arg_is_super = false;
3161
Damien429d7192013-10-04 19:53:11 +01003162 comp->scope_head = NULL;
3163 comp->scope_cur = NULL;
3164
Damien826005c2013-10-05 23:17:28 +01003165 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003166 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003167
3168 // set the outer scope
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003169 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE);
Damien429d7192013-10-04 19:53:11 +01003170
Damien826005c2013-10-05 23:17:28 +01003171 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003172 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003173 comp->emit_method_table = &emit_pass1_method_table;
3174 comp->emit_inline_asm = NULL;
3175 comp->emit_inline_asm_method_table = NULL;
3176 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003177 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003178 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003179#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003180 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003181 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003182#endif
Damien826005c2013-10-05 23:17:28 +01003183 } else {
3184 compile_scope(comp, s, PASS_1);
3185 }
3186
3187 // update maximim number of labels needed
3188 if (comp->next_label > max_num_labels) {
3189 max_num_labels = comp->next_label;
3190 }
Damien429d7192013-10-04 19:53:11 +01003191 }
3192
Damien826005c2013-10-05 23:17:28 +01003193 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003194 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003195 compile_scope_compute_things(comp, s);
3196 }
3197
Damien826005c2013-10-05 23:17:28 +01003198 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003199 emit_pass1_free(comp->emit);
3200
Damien826005c2013-10-05 23:17:28 +01003201 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003202#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003203 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003204#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003205 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003206#endif
Damien3ef4abb2013-10-12 16:53:13 +01003207#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003208 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003209#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003210#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003211 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003212 if (false) {
3213 // dummy
3214
Damien3ef4abb2013-10-12 16:53:13 +01003215#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003216 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
3217 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003218 if (emit_inline_thumb == NULL) {
3219 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3220 }
3221 comp->emit = NULL;
3222 comp->emit_method_table = NULL;
3223 comp->emit_inline_asm = emit_inline_thumb;
3224 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3225 compile_scope_inline_asm(comp, s, PASS_2);
3226 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003227#endif
3228
Damien826005c2013-10-05 23:17:28 +01003229 } else {
Damienc025ebb2013-10-12 14:30:21 +01003230
3231 // choose the emit type
3232
Damien3ef4abb2013-10-12 16:53:13 +01003233#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003234 comp->emit = emit_cpython_new(max_num_labels);
3235 comp->emit_method_table = &emit_cpython_method_table;
3236#else
Damien826005c2013-10-05 23:17:28 +01003237 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003238
3239#if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003240 case EMIT_OPT_NATIVE_PYTHON:
Damien3410be82013-10-07 23:09:10 +01003241 case EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003242#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003243 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003244 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003245 }
Damien13ed3a62013-10-08 09:05:10 +01003246 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003247#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003248 if (emit_native == NULL) {
3249 emit_native = emit_native_thumb_new(max_num_labels);
3250 }
3251 comp->emit_method_table = &emit_native_thumb_method_table;
3252#endif
3253 comp->emit = emit_native;
Damien3410be82013-10-07 23:09:10 +01003254 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003255 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003256#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003257
Damien826005c2013-10-05 23:17:28 +01003258 default:
3259 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003260 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003261 }
3262 comp->emit = emit_bc;
3263 comp->emit_method_table = &emit_bc_method_table;
3264 break;
3265 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003266#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003267
3268 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003269 compile_scope(comp, s, PASS_2);
3270 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003271 }
Damien429d7192013-10-04 19:53:11 +01003272 }
3273
Damien George41d02b62014-01-24 22:42:28 +00003274 // free the emitters
3275#if !MICROPY_EMIT_CPYTHON
3276 if (emit_bc != NULL) {
3277 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003278 }
Damien George41d02b62014-01-24 22:42:28 +00003279#if MICROPY_EMIT_NATIVE
3280 if (emit_native != NULL) {
3281#if MICROPY_EMIT_X64
3282 emit_native_x64_free(emit_native);
3283#elif MICROPY_EMIT_THUMB
3284 emit_native_thumb_free(emit_native);
3285#endif
3286 }
3287#endif
3288#if MICROPY_EMIT_INLINE_THUMB
3289 if (emit_inline_thumb != NULL) {
3290 emit_inline_thumb_free(emit_inline_thumb);
3291 }
3292#endif
3293#endif // !MICROPY_EMIT_CPYTHON
3294
3295 // free the scopes
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003296 uint unique_code_id = module_scope->unique_code_id;
3297 for (scope_t *s = module_scope; s;) {
3298 scope_t *next = s->next;
3299 scope_free(s);
3300 s = next;
3301 }
Damien5ac1b2e2013-10-18 19:58:12 +01003302
Damien George41d02b62014-01-24 22:42:28 +00003303 // free the compiler
3304 bool had_error = comp->had_error;
3305 m_del_obj(compiler_t, comp);
3306
Damien George1fb03172014-01-03 14:22:03 +00003307 if (had_error) {
3308 // TODO return a proper error message
3309 return mp_const_none;
3310 } else {
3311#if MICROPY_EMIT_CPYTHON
3312 // can't create code, so just return true
Damien George41d02b62014-01-24 22:42:28 +00003313 (void)unique_code_id; // to suppress warning that unique_code_id is unused
Damien George1fb03172014-01-03 14:22:03 +00003314 return mp_const_true;
3315#else
3316 // return function that executes the outer module
Paul Sokolovsky90750022014-02-01 15:05:04 +02003317 return rt_make_function_from_id(unique_code_id, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003318#endif
3319 }
Damien429d7192013-10-04 19:53:11 +01003320}