blob: 4d07f2e74b2f66b349e27703cf025cd8874155b6 [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 George1fb03172014-01-03 14:22:03 +000016#include "obj.h"
17#include "compile.h"
18#include "runtime.h"
Rachel Dowdallcde86312014-03-22 17:29:27 +000019#include "intdivmod.h"
Damien429d7192013-10-04 19:53:11 +010020
21// TODO need to mangle __attr names
22
Damience89a212013-10-15 22:25:17 +010023#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_THUMB)
24
Damien429d7192013-10-04 19:53:11 +010025typedef enum {
26 PN_none = 0,
Damien George00208ce2014-01-23 00:00:53 +000027#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
Damien429d7192013-10-04 19:53:11 +010028#include "grammar.h"
29#undef DEF_RULE
30 PN_maximum_number_of,
31} pn_kind_t;
32
Damien Georgeb9791222014-01-23 00:34:21 +000033#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
34#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
35#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
36#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 +010037
Damien6cdd3af2013-10-05 18:08:26 +010038#define EMIT_OPT_NONE (0)
39#define EMIT_OPT_BYTE_CODE (1)
40#define EMIT_OPT_NATIVE_PYTHON (2)
Damien7af3d192013-10-07 00:02:49 +010041#define EMIT_OPT_VIPER (3)
42#define EMIT_OPT_ASM_THUMB (4)
Damien6cdd3af2013-10-05 18:08:26 +010043
Damien429d7192013-10-04 19:53:11 +010044typedef struct _compiler_t {
Damien Georgecbd2f742014-01-19 11:48:48 +000045 qstr source_file;
Damien5ac1b2e2013-10-18 19:58:12 +010046 bool is_repl;
Damien429d7192013-10-04 19:53:11 +010047 pass_kind_t pass;
Damien5ac1b2e2013-10-18 19:58:12 +010048 bool had_error; // try to keep compiler clean from nlr
Damien429d7192013-10-04 19:53:11 +010049
Damienb05d7072013-10-05 13:37:10 +010050 int next_label;
Damienb05d7072013-10-05 13:37:10 +010051
Damien429d7192013-10-04 19:53:11 +010052 int break_label;
53 int continue_label;
Damien Georgecbddb272014-02-01 20:08:18 +000054 int break_continue_except_level;
55 int cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
Damien429d7192013-10-04 19:53:11 +010056
57 int n_arg_keyword;
58 bool have_star_arg;
59 bool have_dbl_star_arg;
60 bool have_bare_star;
61 int param_pass;
62 int param_pass_num_dict_params;
63 int param_pass_num_default_params;
64
Damien George35e2a4e2014-02-05 00:51:47 +000065 bool func_arg_is_super; // used to compile special case of super() function call
66
Damien429d7192013-10-04 19:53:11 +010067 scope_t *scope_head;
68 scope_t *scope_cur;
69
Damien6cdd3af2013-10-05 18:08:26 +010070 emit_t *emit; // current emitter
71 const emit_method_table_t *emit_method_table; // current emit method table
Damien826005c2013-10-05 23:17:28 +010072
73 emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
74 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 +010075} compiler_t;
76
Damien Georgef41fdd02014-03-03 23:19:11 +000077STATIC void compile_syntax_error(compiler_t *comp, const char *msg) {
78 // TODO store the error message to a variable in compiler_t instead of printing it
79 printf("SyntaxError: %s\n", msg);
80 comp->had_error = true;
81}
82
Damiend99b0522013-12-21 18:17:45 +000083mp_parse_node_t fold_constants(mp_parse_node_t pn) {
84 if (MP_PARSE_NODE_IS_STRUCT(pn)) {
85 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
86 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +010087
88 // fold arguments first
89 for (int i = 0; i < n; i++) {
90 pns->nodes[i] = fold_constants(pns->nodes[i]);
91 }
92
Damiend99b0522013-12-21 18:17:45 +000093 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +010094 case PN_shift_expr:
Damiend99b0522013-12-21 18:17:45 +000095 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 +020096 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
97 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +000098 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
Damien3ef4abb2013-10-12 16:53:13 +010099#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100100 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000101 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
Damien0efb3a12013-10-12 16:16:56 +0100102#endif
Damiend99b0522013-12-21 18:17:45 +0000103 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
104 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
Damien429d7192013-10-04 19:53:11 +0100105 } else {
106 // shouldn't happen
107 assert(0);
108 }
109 }
110 break;
111
112 case PN_arith_expr:
Damien0efb3a12013-10-12 16:16:56 +0100113 // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
Damiend99b0522013-12-21 18:17:45 +0000114 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 +0200115 machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
116 machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damien0efb3a12013-10-12 16:16:56 +0100117 machine_int_t res;
Damiend99b0522013-12-21 18:17:45 +0000118 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100119 res = arg0 + arg1;
Damiend99b0522013-12-21 18:17:45 +0000120 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
Damien0efb3a12013-10-12 16:16:56 +0100121 res = arg0 - arg1;
Damien429d7192013-10-04 19:53:11 +0100122 } else {
123 // shouldn't happen
124 assert(0);
Damien0efb3a12013-10-12 16:16:56 +0100125 res = 0;
126 }
Paul Sokolovskybbf0e2f2014-02-21 02:04:32 +0200127 if (MP_PARSE_FITS_SMALL_INT(res)) {
Damiend99b0522013-12-21 18:17:45 +0000128 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, res);
Damien429d7192013-10-04 19:53:11 +0100129 }
130 }
131 break;
132
133 case PN_term:
Damiend99b0522013-12-21 18:17:45 +0000134 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 +0200135 int arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
136 int arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
Damiend99b0522013-12-21 18:17:45 +0000137 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien3ef4abb2013-10-12 16:53:13 +0100138#if MICROPY_EMIT_CPYTHON
Damien0efb3a12013-10-12 16:16:56 +0100139 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000140 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 * arg1);
Damien0efb3a12013-10-12 16:16:56 +0100141#endif
Damiend99b0522013-12-21 18:17:45 +0000142 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
Damien429d7192013-10-04 19:53:11 +0100143 ; // pass
Damiend99b0522013-12-21 18:17:45 +0000144 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
Rachel Dowdallcde86312014-03-22 17:29:27 +0000145 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, python_modulo(arg0, arg1));
Damiend99b0522013-12-21 18:17:45 +0000146 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
Damien George8dcc0c72014-03-27 10:55:21 +0000147 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, python_floor_divide(arg0, arg1));
Damien429d7192013-10-04 19:53:11 +0100148 } else {
149 // shouldn't happen
150 assert(0);
151 }
152 }
153 break;
154
155 case PN_factor_2:
Damiend99b0522013-12-21 18:17:45 +0000156 if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200157 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +0000158 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
159 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
160 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
161 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
162 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
163 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
Damien429d7192013-10-04 19:53:11 +0100164 } else {
165 // shouldn't happen
166 assert(0);
167 }
168 }
169 break;
170
Damien3ef4abb2013-10-12 16:53:13 +0100171#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +0100172 case PN_power:
Damien0efb3a12013-10-12 16:16:56 +0100173 // can overflow; enabled only to compare with CPython
Damiend99b0522013-12-21 18:17:45 +0000174 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])) {
175 mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
176 if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200177 int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100178 if (power >= 0) {
179 int ans = 1;
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200180 int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +0100181 for (; power > 0; power--) {
182 ans *= base;
183 }
Damiend99b0522013-12-21 18:17:45 +0000184 pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
Damien429d7192013-10-04 19:53:11 +0100185 }
186 }
187 }
188 break;
Damien0efb3a12013-10-12 16:16:56 +0100189#endif
Damien429d7192013-10-04 19:53:11 +0100190 }
191 }
192
193 return pn;
194}
195
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200196STATIC 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 +0000197STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
Damien429d7192013-10-04 19:53:11 +0100198
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200199STATIC int comp_next_label(compiler_t *comp) {
Damienb05d7072013-10-05 13:37:10 +0100200 return comp->next_label++;
201}
202
Damien George8dcc0c72014-03-27 10:55:21 +0000203STATIC void compile_increase_except_level(compiler_t *comp) {
204 comp->cur_except_level += 1;
205 if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
206 comp->scope_cur->exc_stack_size = comp->cur_except_level;
207 }
208}
209
210STATIC void compile_decrease_except_level(compiler_t *comp) {
211 assert(comp->cur_except_level > 0);
212 comp->cur_except_level -= 1;
213}
214
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200215STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) {
Damien Georgecbd2f742014-01-19 11:48:48 +0000216 scope_t *scope = scope_new(kind, pn, comp->source_file, rt_get_unique_code_id(), emit_options);
Damien429d7192013-10-04 19:53:11 +0100217 scope->parent = comp->scope_cur;
218 scope->next = NULL;
219 if (comp->scope_head == NULL) {
220 comp->scope_head = scope;
221 } else {
222 scope_t *s = comp->scope_head;
223 while (s->next != NULL) {
224 s = s->next;
225 }
226 s->next = scope;
227 }
228 return scope;
229}
230
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200231STATIC int list_len(mp_parse_node_t pn, int pn_kind) {
Damiend99b0522013-12-21 18:17:45 +0000232 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100233 return 0;
Damiend99b0522013-12-21 18:17:45 +0000234 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100235 return 1;
236 } else {
Damiend99b0522013-12-21 18:17:45 +0000237 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
238 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100239 return 1;
240 } else {
Damiend99b0522013-12-21 18:17:45 +0000241 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100242 }
243 }
244}
245
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200246STATIC 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 +0000247 if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
248 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
249 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100250 for (int i = 0; i < num_nodes; i++) {
251 f(comp, pns->nodes[i]);
252 }
Damiend99b0522013-12-21 18:17:45 +0000253 } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100254 f(comp, pn);
255 }
256}
257
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200258STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
Damiend99b0522013-12-21 18:17:45 +0000259 if (MP_PARSE_NODE_IS_NULL(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100260 *nodes = NULL;
261 return 0;
Damiend99b0522013-12-21 18:17:45 +0000262 } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
Damien429d7192013-10-04 19:53:11 +0100263 *nodes = pn;
264 return 1;
265 } else {
Damiend99b0522013-12-21 18:17:45 +0000266 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
267 if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
Damien429d7192013-10-04 19:53:11 +0100268 *nodes = pn;
269 return 1;
270 } else {
271 *nodes = pns->nodes;
Damiend99b0522013-12-21 18:17:45 +0000272 return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100273 }
274 }
275}
276
Damiend99b0522013-12-21 18:17:45 +0000277void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100278}
279
Damiend99b0522013-12-21 18:17:45 +0000280void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
281 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +0100282 for (int i = 0; i < num_nodes; i++) {
283 compile_node(comp, pns->nodes[i]);
284 }
285}
286
Damien3ef4abb2013-10-12 16:53:13 +0100287#if MICROPY_EMIT_CPYTHON
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200288STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000289 if (!MP_PARSE_NODE_IS_LEAF(pn)) {
Damien429d7192013-10-04 19:53:11 +0100290 return false;
291 }
Damiend99b0522013-12-21 18:17:45 +0000292 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +0100293 return false;
294 }
295 return true;
296}
297
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200298STATIC void cpython_c_print_quoted_str(vstr_t *vstr, qstr qstr, bool bytes) {
Damien George55baff42014-01-21 21:40:13 +0000299 uint len;
300 const byte *str = qstr_data(qstr, &len);
Damien02f89412013-12-12 15:13:36 +0000301 bool has_single_quote = false;
302 bool has_double_quote = false;
303 for (int i = 0; i < len; i++) {
304 if (str[i] == '\'') {
305 has_single_quote = true;
306 } else if (str[i] == '"') {
307 has_double_quote = true;
308 }
309 }
310 if (bytes) {
311 vstr_printf(vstr, "b");
312 }
313 bool quote_single = false;
314 if (has_single_quote && !has_double_quote) {
315 vstr_printf(vstr, "\"");
316 } else {
317 quote_single = true;
318 vstr_printf(vstr, "'");
319 }
320 for (int i = 0; i < len; i++) {
321 if (str[i] == '\n') {
322 vstr_printf(vstr, "\\n");
323 } else if (str[i] == '\\') {
324 vstr_printf(vstr, "\\\\");
325 } else if (str[i] == '\'' && quote_single) {
326 vstr_printf(vstr, "\\'");
327 } else {
328 vstr_printf(vstr, "%c", str[i]);
329 }
330 }
331 if (has_single_quote && !has_double_quote) {
332 vstr_printf(vstr, "\"");
333 } else {
334 vstr_printf(vstr, "'");
335 }
336}
337
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200338STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
Damiend99b0522013-12-21 18:17:45 +0000339 assert(MP_PARSE_NODE_IS_LEAF(pn));
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200340 if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
341 vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
342 return;
343 }
344
Damiend99b0522013-12-21 18:17:45 +0000345 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
346 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
347 case MP_PARSE_NODE_ID: assert(0);
Damiend99b0522013-12-21 18:17:45 +0000348 case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
349 case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
350 case MP_PARSE_NODE_STRING: cpython_c_print_quoted_str(vstr, arg, false); break;
351 case MP_PARSE_NODE_BYTES: cpython_c_print_quoted_str(vstr, arg, true); break;
352 case MP_PARSE_NODE_TOKEN:
Damien429d7192013-10-04 19:53:11 +0100353 switch (arg) {
Damiend99b0522013-12-21 18:17:45 +0000354 case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
355 case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
356 case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
Damien429d7192013-10-04 19:53:11 +0100357 default: assert(0);
358 }
359 break;
360 default: assert(0);
361 }
362}
363
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200364STATIC 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 +0100365 int n = 0;
366 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000367 n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien429d7192013-10-04 19:53:11 +0100368 }
369 int total = n;
370 bool is_const = true;
Damiend99b0522013-12-21 18:17:45 +0000371 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100372 total += 1;
Damien3a205172013-10-12 15:01:56 +0100373 if (!cpython_c_tuple_is_const(pn)) {
Damien429d7192013-10-04 19:53:11 +0100374 is_const = false;
375 }
376 }
377 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100378 if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
Damien429d7192013-10-04 19:53:11 +0100379 is_const = false;
380 break;
381 }
382 }
383 if (total > 0 && is_const) {
384 bool need_comma = false;
Damien02f89412013-12-12 15:13:36 +0000385 vstr_t *vstr = vstr_new();
386 vstr_printf(vstr, "(");
Damiend99b0522013-12-21 18:17:45 +0000387 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien02f89412013-12-12 15:13:36 +0000388 cpython_c_tuple_emit_const(comp, pn, vstr);
Damien429d7192013-10-04 19:53:11 +0100389 need_comma = true;
390 }
391 for (int i = 0; i < n; i++) {
392 if (need_comma) {
Damien02f89412013-12-12 15:13:36 +0000393 vstr_printf(vstr, ", ");
Damien429d7192013-10-04 19:53:11 +0100394 }
Damien02f89412013-12-12 15:13:36 +0000395 cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
Damien429d7192013-10-04 19:53:11 +0100396 need_comma = true;
397 }
398 if (total == 1) {
Damien02f89412013-12-12 15:13:36 +0000399 vstr_printf(vstr, ",)");
Damien429d7192013-10-04 19:53:11 +0100400 } else {
Damien02f89412013-12-12 15:13:36 +0000401 vstr_printf(vstr, ")");
Damien429d7192013-10-04 19:53:11 +0100402 }
Damien Georgeb9791222014-01-23 00:34:21 +0000403 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +0000404 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +0100405 } else {
Damiend99b0522013-12-21 18:17:45 +0000406 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100407 compile_node(comp, pn);
408 }
409 for (int i = 0; i < n; i++) {
410 compile_node(comp, pns_list->nodes[i]);
411 }
Damien Georgeb9791222014-01-23 00:34:21 +0000412 EMIT_ARG(build_tuple, total);
Damien429d7192013-10-04 19:53:11 +0100413 }
414}
Damien3a205172013-10-12 15:01:56 +0100415#endif
416
417// funnelling all tuple creations through this function is purely so we can optionally agree with CPython
Damiend99b0522013-12-21 18:17:45 +0000418void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
Damien3ef4abb2013-10-12 16:53:13 +0100419#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100420 cpython_c_tuple(comp, pn, pns_list);
421#else
422 int total = 0;
Damiend99b0522013-12-21 18:17:45 +0000423 if (!MP_PARSE_NODE_IS_NULL(pn)) {
Damien3a205172013-10-12 15:01:56 +0100424 compile_node(comp, pn);
425 total += 1;
426 }
427 if (pns_list != NULL) {
Damiend99b0522013-12-21 18:17:45 +0000428 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
Damien3a205172013-10-12 15:01:56 +0100429 for (int i = 0; i < n; i++) {
430 compile_node(comp, pns_list->nodes[i]);
431 }
432 total += n;
433 }
Damien Georgeb9791222014-01-23 00:34:21 +0000434 EMIT_ARG(build_tuple, total);
Damien3a205172013-10-12 15:01:56 +0100435#endif
436}
Damien429d7192013-10-04 19:53:11 +0100437
Damiend99b0522013-12-21 18:17:45 +0000438void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100439 // a simple tuple expression
Damiend99b0522013-12-21 18:17:45 +0000440 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +0100441}
442
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200443STATIC bool node_is_const_false(mp_parse_node_t pn) {
Damiend99b0522013-12-21 18:17:45 +0000444 return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200445 // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
Damien429d7192013-10-04 19:53:11 +0100446}
447
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200448STATIC bool node_is_const_true(mp_parse_node_t pn) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +0200449 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 +0100450}
451
Damien3ef4abb2013-10-12 16:53:13 +0100452#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100453// the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200454STATIC 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 +0100455 if (node_is_const_false(pn)) {
456 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000457 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100458 }
459 return;
460 } else if (node_is_const_true(pn)) {
461 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000462 EMIT_ARG(jump, label);
Damien429d7192013-10-04 19:53:11 +0100463 }
464 return;
Damiend99b0522013-12-21 18:17:45 +0000465 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
466 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
467 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
468 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien429d7192013-10-04 19:53:11 +0100469 if (jump_if == false) {
Damienb05d7072013-10-05 13:37:10 +0100470 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100471 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100472 cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
Damien429d7192013-10-04 19:53:11 +0100473 }
Damien3a205172013-10-12 15:01:56 +0100474 cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000475 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100476 } else {
477 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100478 cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
Damien429d7192013-10-04 19:53:11 +0100479 }
480 }
481 return;
Damiend99b0522013-12-21 18:17:45 +0000482 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien429d7192013-10-04 19:53:11 +0100483 if (jump_if == false) {
484 for (int i = 0; i < n; i++) {
Damien3a205172013-10-12 15:01:56 +0100485 cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
Damien429d7192013-10-04 19:53:11 +0100486 }
487 } else {
Damienb05d7072013-10-05 13:37:10 +0100488 int label2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +0100489 for (int i = 0; i < n - 1; i++) {
Damien3a205172013-10-12 15:01:56 +0100490 cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
Damien429d7192013-10-04 19:53:11 +0100491 }
Damien3a205172013-10-12 15:01:56 +0100492 cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
Damien Georgeb9791222014-01-23 00:34:21 +0000493 EMIT_ARG(label_assign, label2);
Damien429d7192013-10-04 19:53:11 +0100494 }
495 return;
Damiend99b0522013-12-21 18:17:45 +0000496 } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100497 cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
Damien429d7192013-10-04 19:53:11 +0100498 return;
499 }
500 }
501
502 // nothing special, fall back to default compiling for node and jump
503 compile_node(comp, pn);
504 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000505 EMIT_ARG(pop_jump_if_false, label);
Damien429d7192013-10-04 19:53:11 +0100506 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000507 EMIT_ARG(pop_jump_if_true, label);
Damien429d7192013-10-04 19:53:11 +0100508 }
509}
Damien3a205172013-10-12 15:01:56 +0100510#endif
Damien429d7192013-10-04 19:53:11 +0100511
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200512STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
Damien3ef4abb2013-10-12 16:53:13 +0100513#if MICROPY_EMIT_CPYTHON
Damien3a205172013-10-12 15:01:56 +0100514 cpython_c_if_cond(comp, pn, jump_if, label, false);
515#else
516 if (node_is_const_false(pn)) {
517 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000518 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100519 }
520 return;
521 } else if (node_is_const_true(pn)) {
522 if (jump_if == true) {
Damien Georgeb9791222014-01-23 00:34:21 +0000523 EMIT_ARG(jump, label);
Damien3a205172013-10-12 15:01:56 +0100524 }
525 return;
Damiend99b0522013-12-21 18:17:45 +0000526 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
527 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
528 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
529 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
Damien3a205172013-10-12 15:01:56 +0100530 if (jump_if == false) {
531 int label2 = comp_next_label(comp);
532 for (int i = 0; i < n - 1; i++) {
533 c_if_cond(comp, pns->nodes[i], true, label2);
534 }
535 c_if_cond(comp, pns->nodes[n - 1], false, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000536 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100537 } else {
538 for (int i = 0; i < n; i++) {
539 c_if_cond(comp, pns->nodes[i], true, label);
540 }
541 }
542 return;
Damiend99b0522013-12-21 18:17:45 +0000543 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
Damien3a205172013-10-12 15:01:56 +0100544 if (jump_if == false) {
545 for (int i = 0; i < n; i++) {
546 c_if_cond(comp, pns->nodes[i], false, label);
547 }
548 } else {
549 int label2 = comp_next_label(comp);
550 for (int i = 0; i < n - 1; i++) {
551 c_if_cond(comp, pns->nodes[i], false, label2);
552 }
553 c_if_cond(comp, pns->nodes[n - 1], true, label);
Damien Georgeb9791222014-01-23 00:34:21 +0000554 EMIT_ARG(label_assign, label2);
Damien3a205172013-10-12 15:01:56 +0100555 }
556 return;
Damiend99b0522013-12-21 18:17:45 +0000557 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
Damien3a205172013-10-12 15:01:56 +0100558 c_if_cond(comp, pns->nodes[0], !jump_if, label);
559 return;
560 }
561 }
562
563 // nothing special, fall back to default compiling for node and jump
564 compile_node(comp, pn);
565 if (jump_if == false) {
Damien Georgeb9791222014-01-23 00:34:21 +0000566 EMIT_ARG(pop_jump_if_false, label);
Damien3a205172013-10-12 15:01:56 +0100567 } else {
Damien Georgeb9791222014-01-23 00:34:21 +0000568 EMIT_ARG(pop_jump_if_true, label);
Damien3a205172013-10-12 15:01:56 +0100569 }
570#endif
Damien429d7192013-10-04 19:53:11 +0100571}
572
573typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
Damiend99b0522013-12-21 18:17:45 +0000574void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
Damien429d7192013-10-04 19:53:11 +0100575
Damiend99b0522013-12-21 18:17:45 +0000576void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100577 if (assign_kind != ASSIGN_AUG_STORE) {
578 compile_node(comp, pns->nodes[0]);
579 }
580
Damiend99b0522013-12-21 18:17:45 +0000581 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
582 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
583 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
584 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +0100585 if (assign_kind != ASSIGN_AUG_STORE) {
586 for (int i = 0; i < n - 1; i++) {
587 compile_node(comp, pns1->nodes[i]);
588 }
589 }
Damiend99b0522013-12-21 18:17:45 +0000590 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
591 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +0100592 }
Damiend99b0522013-12-21 18:17:45 +0000593 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000594 compile_syntax_error(comp, "can't assign to function call");
Damien429d7192013-10-04 19:53:11 +0100595 return;
Damiend99b0522013-12-21 18:17:45 +0000596 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +0100597 if (assign_kind == ASSIGN_AUG_STORE) {
598 EMIT(rot_three);
599 EMIT(store_subscr);
600 } else {
601 compile_node(comp, pns1->nodes[0]);
602 if (assign_kind == ASSIGN_AUG_LOAD) {
603 EMIT(dup_top_two);
Damien Georgeb9791222014-01-23 00:34:21 +0000604 EMIT_ARG(binary_op, RT_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +0100605 } else {
606 EMIT(store_subscr);
607 }
608 }
Damiend99b0522013-12-21 18:17:45 +0000609 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
610 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100611 if (assign_kind == ASSIGN_AUG_LOAD) {
612 EMIT(dup_top);
Damien Georgeb9791222014-01-23 00:34:21 +0000613 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100614 } else {
615 if (assign_kind == ASSIGN_AUG_STORE) {
616 EMIT(rot_two);
617 }
Damien Georgeb9791222014-01-23 00:34:21 +0000618 EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100619 }
620 } else {
621 // shouldn't happen
622 assert(0);
623 }
624 } else {
625 // shouldn't happen
626 assert(0);
627 }
628
Damiend99b0522013-12-21 18:17:45 +0000629 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +0100630 // SyntaxError, cannot assign
631 assert(0);
632 }
633}
634
Damiend99b0522013-12-21 18:17:45 +0000635void c_assign_tuple(compiler_t *comp, int n, mp_parse_node_t *nodes) {
Damien429d7192013-10-04 19:53:11 +0100636 assert(n >= 0);
637 int have_star_index = -1;
638 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +0000639 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_star_expr)) {
Damien429d7192013-10-04 19:53:11 +0100640 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000641 EMIT_ARG(unpack_ex, i, n - i - 1);
Damien429d7192013-10-04 19:53:11 +0100642 have_star_index = i;
643 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000644 compile_syntax_error(comp, "two starred expressions in assignment");
Damien429d7192013-10-04 19:53:11 +0100645 return;
646 }
647 }
648 }
649 if (have_star_index < 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000650 EMIT_ARG(unpack_sequence, n);
Damien429d7192013-10-04 19:53:11 +0100651 }
652 for (int i = 0; i < n; i++) {
653 if (i == have_star_index) {
Damiend99b0522013-12-21 18:17:45 +0000654 c_assign(comp, ((mp_parse_node_struct_t*)nodes[i])->nodes[0], ASSIGN_STORE);
Damien429d7192013-10-04 19:53:11 +0100655 } else {
656 c_assign(comp, nodes[i], ASSIGN_STORE);
657 }
658 }
659}
660
661// assigns top of stack to pn
Damiend99b0522013-12-21 18:17:45 +0000662void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
Damien429d7192013-10-04 19:53:11 +0100663 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +0000664 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +0100665 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000666 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
667 if (MP_PARSE_NODE_IS_ID(pn)) {
668 int arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +0100669 switch (assign_kind) {
670 case ASSIGN_STORE:
671 case ASSIGN_AUG_STORE:
Damien Georgeb9791222014-01-23 00:34:21 +0000672 EMIT_ARG(store_id, arg);
Damien429d7192013-10-04 19:53:11 +0100673 break;
674 case ASSIGN_AUG_LOAD:
Damien Georgeb9791222014-01-23 00:34:21 +0000675 EMIT_ARG(load_id, arg);
Damien429d7192013-10-04 19:53:11 +0100676 break;
677 }
678 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000679 compile_syntax_error(comp, "can't assign to literal");
Damien429d7192013-10-04 19:53:11 +0100680 return;
681 }
682 } else {
Damiend99b0522013-12-21 18:17:45 +0000683 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
684 switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
Damien429d7192013-10-04 19:53:11 +0100685 case PN_power:
686 // lhs is an index or attribute
687 c_assign_power(comp, pns, assign_kind);
688 break;
689
690 case PN_testlist_star_expr:
691 case PN_exprlist:
692 // lhs is a tuple
693 if (assign_kind != ASSIGN_STORE) {
694 goto bad_aug;
695 }
Damiend99b0522013-12-21 18:17:45 +0000696 c_assign_tuple(comp, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
Damien429d7192013-10-04 19:53:11 +0100697 break;
698
699 case PN_atom_paren:
700 // lhs is something in parenthesis
Damiend99b0522013-12-21 18:17:45 +0000701 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100702 // empty tuple
Damien Georgef41fdd02014-03-03 23:19:11 +0000703 compile_syntax_error(comp, "can't assign to ()");
Damien429d7192013-10-04 19:53:11 +0100704 return;
Damiend99b0522013-12-21 18:17:45 +0000705 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
706 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100707 goto testlist_comp;
708 } else {
709 // parenthesis around 1 item, is just that item
710 pn = pns->nodes[0];
711 goto tail_recursion;
712 }
713 break;
714
715 case PN_atom_bracket:
716 // lhs is something in brackets
717 if (assign_kind != ASSIGN_STORE) {
718 goto bad_aug;
719 }
Damiend99b0522013-12-21 18:17:45 +0000720 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100721 // empty list, assignment allowed
722 c_assign_tuple(comp, 0, NULL);
Damiend99b0522013-12-21 18:17:45 +0000723 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
724 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +0100725 goto testlist_comp;
726 } else {
727 // brackets around 1 item
728 c_assign_tuple(comp, 1, &pns->nodes[0]);
729 }
730 break;
731
732 default:
Damiend99b0522013-12-21 18:17:45 +0000733 printf("unknown assign, %u\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien429d7192013-10-04 19:53:11 +0100734 assert(0);
735 }
736 return;
737
738 testlist_comp:
739 // lhs is a sequence
Damiend99b0522013-12-21 18:17:45 +0000740 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
741 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
742 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +0100743 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +0000744 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +0100745 c_assign_tuple(comp, 1, &pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +0000746 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +0100747 // sequence of many items
748 // TODO call c_assign_tuple instead
Damiend99b0522013-12-21 18:17:45 +0000749 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
Damien Georgeb9791222014-01-23 00:34:21 +0000750 EMIT_ARG(unpack_sequence, 1 + n);
Damien429d7192013-10-04 19:53:11 +0100751 c_assign(comp, pns->nodes[0], ASSIGN_STORE);
752 for (int i = 0; i < n; i++) {
753 c_assign(comp, pns2->nodes[i], ASSIGN_STORE);
754 }
Damiend99b0522013-12-21 18:17:45 +0000755 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +0100756 // TODO not implemented
757 assert(0);
758 } else {
759 // sequence with 2 items
760 goto sequence_with_2_items;
761 }
762 } else {
763 // sequence with 2 items
764 sequence_with_2_items:
765 c_assign_tuple(comp, 2, pns->nodes);
766 }
767 return;
768 }
769 return;
770
771 bad_aug:
Damien Georgef41fdd02014-03-03 23:19:11 +0000772 compile_syntax_error(comp, "illegal expression for augmented assignment");
Damien429d7192013-10-04 19:53:11 +0100773}
774
775// stuff for lambda and comprehensions and generators
776void 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 +0000777#if !MICROPY_EMIT_CPYTHON
778 // in Micro Python we put the default params into a tuple using the bytecode
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200779 if (n_default_params) {
780 EMIT_ARG(build_tuple, n_default_params);
781 }
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000782#endif
783
Damien429d7192013-10-04 19:53:11 +0100784 // make closed over variables, if any
Damien318aec62013-12-10 18:28:17 +0000785 // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
Damien429d7192013-10-04 19:53:11 +0100786 int nfree = 0;
787 if (comp->scope_cur->kind != SCOPE_MODULE) {
Damien318aec62013-12-10 18:28:17 +0000788 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
789 id_info_t *id = &comp->scope_cur->id_info[i];
790 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
791 for (int j = 0; j < this_scope->id_info_len; j++) {
792 id_info_t *id2 = &this_scope->id_info[j];
793 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +0000794#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +0000795 EMIT_ARG(load_closure, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000796#else
797 // in Micro Python we load closures using LOAD_FAST
Damien Georgeb9791222014-01-23 00:34:21 +0000798 EMIT_ARG(load_fast, id->qstr, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +0000799#endif
Damien318aec62013-12-10 18:28:17 +0000800 nfree += 1;
801 }
802 }
Damien429d7192013-10-04 19:53:11 +0100803 }
804 }
805 }
Damien429d7192013-10-04 19:53:11 +0100806
807 // make the function/closure
808 if (nfree == 0) {
Damien Georgeb9791222014-01-23 00:34:21 +0000809 EMIT_ARG(make_function, this_scope, n_dict_params, n_default_params);
Damien429d7192013-10-04 19:53:11 +0100810 } else {
Damien Georgebdcbf0f2014-03-26 23:15:35 +0000811 EMIT_ARG(build_tuple, nfree);
Damien Georgeb9791222014-01-23 00:34:21 +0000812 EMIT_ARG(make_closure, this_scope, n_dict_params, n_default_params);
Damien429d7192013-10-04 19:53:11 +0100813 }
814}
815
Damiend99b0522013-12-21 18:17:45 +0000816void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000817 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
Damiend99b0522013-12-21 18:17:45 +0000818 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
819 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +0100820 // bare star
821 comp->have_bare_star = true;
822 }
Damien Georgef41fdd02014-03-03 23:19:11 +0000823
824 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
825 // TODO do we need to do anything with this?
826
827 } else {
828 mp_parse_node_t pn_id;
829 mp_parse_node_t pn_colon;
830 mp_parse_node_t pn_equal;
831 if (MP_PARSE_NODE_IS_ID(pn)) {
832 // this parameter is just an id
833
834 pn_id = pn;
835 pn_colon = MP_PARSE_NODE_NULL;
836 pn_equal = MP_PARSE_NODE_NULL;
837
838 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
839 // this parameter has a colon and/or equal specifier
840
841 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
842 pn_id = pns->nodes[0];
843 pn_colon = pns->nodes[1];
844 pn_equal = pns->nodes[2];
845
846 } else {
847 assert(0);
848 return;
849 }
850
851 if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
852 // this parameter does not have a default value
853
854 // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
855 if (!comp->have_bare_star && comp->param_pass_num_default_params != 0) {
856 compile_syntax_error(comp, "non-default argument follows default argument");
857 return;
858 }
859
860 } else {
861 // this parameter has a default value
862 // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
863
864 if (comp->have_bare_star) {
865 comp->param_pass_num_dict_params += 1;
866 if (comp->param_pass == 1) {
867 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pn_id));
868 compile_node(comp, pn_equal);
869 }
870 } else {
871 comp->param_pass_num_default_params += 1;
872 if (comp->param_pass == 2) {
873 compile_node(comp, pn_equal);
874 }
875 }
876 }
877
878 // TODO pn_colon not implemented
879 (void)pn_colon;
Damien429d7192013-10-04 19:53:11 +0100880 }
881}
882
883// leaves function object on stack
884// returns function name
Damiend99b0522013-12-21 18:17:45 +0000885qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100886 if (comp->pass == PASS_1) {
887 // create a new scope for this function
Damiend99b0522013-12-21 18:17:45 +0000888 scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100889 // store the function scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000890 pns->nodes[4] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100891 }
892
893 // save variables (probably don't need to do this, since we can't have nested definitions..?)
894 bool old_have_bare_star = comp->have_bare_star;
895 int old_param_pass = comp->param_pass;
896 int old_param_pass_num_dict_params = comp->param_pass_num_dict_params;
897 int old_param_pass_num_default_params = comp->param_pass_num_default_params;
898
899 // compile default parameters
Damien Georgef41fdd02014-03-03 23:19:11 +0000900
901 // pass 1 does any default parameters after bare star
Damien429d7192013-10-04 19:53:11 +0100902 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000903 comp->param_pass = 1;
Damien429d7192013-10-04 19:53:11 +0100904 comp->param_pass_num_dict_params = 0;
905 comp->param_pass_num_default_params = 0;
906 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
Damien Georgef41fdd02014-03-03 23:19:11 +0000907
908 if (comp->had_error) {
909 return MP_QSTR_NULL;
910 }
911
912 // pass 2 does any default parameters before bare star
Damien429d7192013-10-04 19:53:11 +0100913 comp->have_bare_star = false;
Damien Georgef41fdd02014-03-03 23:19:11 +0000914 comp->param_pass = 2;
Damien429d7192013-10-04 19:53:11 +0100915 comp->param_pass_num_dict_params = 0;
916 comp->param_pass_num_default_params = 0;
917 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
918
919 // get the scope for this function
920 scope_t *fscope = (scope_t*)pns->nodes[4];
921
922 // make the function
923 close_over_variables_etc(comp, fscope, comp->param_pass_num_dict_params, comp->param_pass_num_default_params);
924
925 // restore variables
926 comp->have_bare_star = old_have_bare_star;
927 comp->param_pass = old_param_pass;
928 comp->param_pass_num_dict_params = old_param_pass_num_dict_params;
929 comp->param_pass_num_default_params = old_param_pass_num_default_params;
930
931 // return its name (the 'f' in "def f(...):")
932 return fscope->simple_name;
933}
934
935// leaves class object on stack
936// returns class name
Damiend99b0522013-12-21 18:17:45 +0000937qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
Damien429d7192013-10-04 19:53:11 +0100938 if (comp->pass == PASS_1) {
939 // create a new scope for this class
Damiend99b0522013-12-21 18:17:45 +0000940 scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
Damien429d7192013-10-04 19:53:11 +0100941 // store the class scope so the compiling function can use it at each pass
Damiend99b0522013-12-21 18:17:45 +0000942 pns->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +0100943 }
944
945 EMIT(load_build_class);
946
947 // scope for this class
948 scope_t *cscope = (scope_t*)pns->nodes[3];
949
950 // compile the class
951 close_over_variables_etc(comp, cscope, 0, 0);
952
953 // get its name
Damien Georgeb9791222014-01-23 00:34:21 +0000954 EMIT_ARG(load_const_id, cscope->simple_name);
Damien429d7192013-10-04 19:53:11 +0100955
956 // nodes[1] has parent classes, if any
Damien Georgebbcd49a2014-02-06 20:30:16 +0000957 comp->func_arg_is_super = false;
958 compile_trailer_paren_helper(comp, pns->nodes[1], false, 2);
Damien429d7192013-10-04 19:53:11 +0100959
960 // return its name (the 'C' in class C(...):")
961 return cscope->simple_name;
962}
963
Damien6cdd3af2013-10-05 18:08:26 +0100964// returns true if it was a built-in decorator (even if the built-in had an error)
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200965STATIC 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 +0000966 if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
Damien6cdd3af2013-10-05 18:08:26 +0100967 return false;
968 }
969
970 if (name_len != 2) {
Damien Georgef41fdd02014-03-03 23:19:11 +0000971 compile_syntax_error(comp, "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100972 return true;
973 }
974
Damiend99b0522013-12-21 18:17:45 +0000975 qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000976 if (attr == MP_QSTR_byte_code) {
Damien5ac1b2e2013-10-18 19:58:12 +0100977 *emit_options = EMIT_OPT_BYTE_CODE;
Damience89a212013-10-15 22:25:17 +0100978#if MICROPY_EMIT_NATIVE
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000979 } else if (attr == MP_QSTR_native) {
Damien6cdd3af2013-10-05 18:08:26 +0100980 *emit_options = EMIT_OPT_NATIVE_PYTHON;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000981 } else if (attr == MP_QSTR_viper) {
Damien7af3d192013-10-07 00:02:49 +0100982 *emit_options = EMIT_OPT_VIPER;
Damience89a212013-10-15 22:25:17 +0100983#endif
Damien3ef4abb2013-10-12 16:53:13 +0100984#if MICROPY_EMIT_INLINE_THUMB
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000985 } else if (attr == MP_QSTR_asm_thumb) {
Damien5bfb7592013-10-05 18:41:24 +0100986 *emit_options = EMIT_OPT_ASM_THUMB;
Damienc025ebb2013-10-12 14:30:21 +0100987#endif
Damien6cdd3af2013-10-05 18:08:26 +0100988 } else {
Damien Georgef41fdd02014-03-03 23:19:11 +0000989 compile_syntax_error(comp, "invalid micropython decorator");
Damien6cdd3af2013-10-05 18:08:26 +0100990 }
991
992 return true;
993}
994
Damiend99b0522013-12-21 18:17:45 +0000995void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +0100996 // get the list of decorators
Damiend99b0522013-12-21 18:17:45 +0000997 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +0100998 int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
999
Damien6cdd3af2013-10-05 18:08:26 +01001000 // inherit emit options for this function/class definition
1001 uint emit_options = comp->scope_cur->emit_options;
1002
1003 // compile each decorator
1004 int num_built_in_decorators = 0;
Damien429d7192013-10-04 19:53:11 +01001005 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001006 assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
1007 mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
Damien6cdd3af2013-10-05 18:08:26 +01001008
1009 // nodes[0] contains the decorator function, which is a dotted name
Damiend99b0522013-12-21 18:17:45 +00001010 mp_parse_node_t *name_nodes;
Damien6cdd3af2013-10-05 18:08:26 +01001011 int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
1012
1013 // check for built-in decorators
1014 if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
1015 // this was a built-in
1016 num_built_in_decorators += 1;
1017
1018 } else {
1019 // not a built-in, compile normally
1020
1021 // compile the decorator function
1022 compile_node(comp, name_nodes[0]);
1023 for (int i = 1; i < name_len; i++) {
Damiend99b0522013-12-21 18:17:45 +00001024 assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
Damien Georgeb9791222014-01-23 00:34:21 +00001025 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
Damien6cdd3af2013-10-05 18:08:26 +01001026 }
1027
1028 // nodes[1] contains arguments to the decorator function, if any
Damiend99b0522013-12-21 18:17:45 +00001029 if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
Damien6cdd3af2013-10-05 18:08:26 +01001030 // call the decorator function with the arguments in nodes[1]
Damien George35e2a4e2014-02-05 00:51:47 +00001031 comp->func_arg_is_super = false;
Damien6cdd3af2013-10-05 18:08:26 +01001032 compile_node(comp, pns_decorator->nodes[1]);
1033 }
Damien429d7192013-10-04 19:53:11 +01001034 }
1035 }
1036
1037 // compile the body (funcdef or classdef) and get its name
Damiend99b0522013-12-21 18:17:45 +00001038 mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001039 qstr body_name = 0;
Damiend99b0522013-12-21 18:17:45 +00001040 if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001041 body_name = compile_funcdef_helper(comp, pns_body, emit_options);
Damiend99b0522013-12-21 18:17:45 +00001042 } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
Damien6cdd3af2013-10-05 18:08:26 +01001043 body_name = compile_classdef_helper(comp, pns_body, emit_options);
Damien429d7192013-10-04 19:53:11 +01001044 } else {
1045 // shouldn't happen
1046 assert(0);
1047 }
1048
1049 // call each decorator
Damien6cdd3af2013-10-05 18:08:26 +01001050 for (int i = 0; i < n - num_built_in_decorators; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001051 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01001052 }
1053
1054 // store func/class object into name
Damien Georgeb9791222014-01-23 00:34:21 +00001055 EMIT_ARG(store_id, body_name);
Damien429d7192013-10-04 19:53:11 +01001056}
1057
Damiend99b0522013-12-21 18:17:45 +00001058void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01001059 qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01001060 // store function object into function name
Damien Georgeb9791222014-01-23 00:34:21 +00001061 EMIT_ARG(store_id, fname);
Damien429d7192013-10-04 19:53:11 +01001062}
1063
Damiend99b0522013-12-21 18:17:45 +00001064void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1065 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001066 EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
Damiend99b0522013-12-21 18:17:45 +00001067 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
1068 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001069
1070 compile_node(comp, pns->nodes[0]); // base of the power node
1071
Damiend99b0522013-12-21 18:17:45 +00001072 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1073 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1074 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
1075 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001076 for (int i = 0; i < n - 1; i++) {
1077 compile_node(comp, pns1->nodes[i]);
1078 }
Damiend99b0522013-12-21 18:17:45 +00001079 assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
1080 pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
Damien429d7192013-10-04 19:53:11 +01001081 }
Damiend99b0522013-12-21 18:17:45 +00001082 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
Damien429d7192013-10-04 19:53:11 +01001083 // SyntaxError: can't delete a function call
1084 assert(0);
Damiend99b0522013-12-21 18:17:45 +00001085 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
Damien429d7192013-10-04 19:53:11 +01001086 compile_node(comp, pns1->nodes[0]);
1087 EMIT(delete_subscr);
Damiend99b0522013-12-21 18:17:45 +00001088 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
1089 assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
Damien Georgeb9791222014-01-23 00:34:21 +00001090 EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001091 } else {
1092 // shouldn't happen
1093 assert(0);
1094 }
1095 } else {
1096 // shouldn't happen
1097 assert(0);
1098 }
1099
Damiend99b0522013-12-21 18:17:45 +00001100 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001101 // SyntaxError, cannot delete
1102 assert(0);
1103 }
Damiend99b0522013-12-21 18:17:45 +00001104 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
1105 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
1106 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
1107 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001108 // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
1109
Damiend99b0522013-12-21 18:17:45 +00001110 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1111 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1112 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01001113 // sequence of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00001114 assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01001115 c_del_stmt(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00001116 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01001117 // sequence of many items
Damiend99b0522013-12-21 18:17:45 +00001118 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
Damien429d7192013-10-04 19:53:11 +01001119 c_del_stmt(comp, pns->nodes[0]);
1120 for (int i = 0; i < n; i++) {
1121 c_del_stmt(comp, pns1->nodes[i]);
1122 }
Damiend99b0522013-12-21 18:17:45 +00001123 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01001124 // TODO not implemented; can't del comprehension?
1125 assert(0);
1126 } else {
1127 // sequence with 2 items
1128 goto sequence_with_2_items;
1129 }
1130 } else {
1131 // sequence with 2 items
1132 sequence_with_2_items:
1133 c_del_stmt(comp, pns->nodes[0]);
1134 c_del_stmt(comp, pns->nodes[1]);
1135 }
1136 } else {
1137 // tuple with 1 element
1138 c_del_stmt(comp, pn);
1139 }
1140 } else {
1141 // not implemented
1142 assert(0);
1143 }
1144}
1145
Damiend99b0522013-12-21 18:17:45 +00001146void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001147 apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
1148}
1149
Damiend99b0522013-12-21 18:17:45 +00001150void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001151 if (comp->break_label == 0) {
1152 printf("ERROR: cannot break from here\n");
1153 }
Damien Georgecbddb272014-02-01 20:08:18 +00001154 EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001155}
1156
Damiend99b0522013-12-21 18:17:45 +00001157void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001158 if (comp->continue_label == 0) {
1159 printf("ERROR: cannot continue from here\n");
1160 }
Damien Georgecbddb272014-02-01 20:08:18 +00001161 EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
Damien429d7192013-10-04 19:53:11 +01001162}
1163
Damiend99b0522013-12-21 18:17:45 +00001164void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien5ac1b2e2013-10-18 19:58:12 +01001165 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001166 compile_syntax_error(comp, "'return' outside function");
Damien5ac1b2e2013-10-18 19:58:12 +01001167 return;
1168 }
Damiend99b0522013-12-21 18:17:45 +00001169 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001170 // no argument to 'return', so return None
Damien Georgeb9791222014-01-23 00:34:21 +00001171 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damiend99b0522013-12-21 18:17:45 +00001172 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
Damien429d7192013-10-04 19:53:11 +01001173 // special case when returning an if-expression; to match CPython optimisation
Damiend99b0522013-12-21 18:17:45 +00001174 mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
1175 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 +01001176
Damienb05d7072013-10-05 13:37:10 +01001177 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001178 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1179 compile_node(comp, pns_test_if_expr->nodes[0]); // success value
1180 EMIT(return_value);
Damien Georgeb9791222014-01-23 00:34:21 +00001181 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001182 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
1183 } else {
1184 compile_node(comp, pns->nodes[0]);
1185 }
1186 EMIT(return_value);
1187}
1188
Damiend99b0522013-12-21 18:17:45 +00001189void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001190 compile_node(comp, pns->nodes[0]);
1191 EMIT(pop_top);
1192}
1193
Damiend99b0522013-12-21 18:17:45 +00001194void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1195 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001196 // raise
Damien Georgeb9791222014-01-23 00:34:21 +00001197 EMIT_ARG(raise_varargs, 0);
Damiend99b0522013-12-21 18:17:45 +00001198 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
Damien429d7192013-10-04 19:53:11 +01001199 // raise x from y
Damiend99b0522013-12-21 18:17:45 +00001200 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001201 compile_node(comp, pns->nodes[0]);
1202 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001203 EMIT_ARG(raise_varargs, 2);
Damien429d7192013-10-04 19:53:11 +01001204 } else {
1205 // raise x
1206 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001207 EMIT_ARG(raise_varargs, 1);
Damien429d7192013-10-04 19:53:11 +01001208 }
1209}
1210
1211// q1 holds the base, q2 the full name
1212// eg a -> q1=q2=a
1213// a.b.c -> q1=a, q2=a.b.c
Damiend99b0522013-12-21 18:17:45 +00001214void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q1, qstr *q2) {
Damien429d7192013-10-04 19:53:11 +01001215 bool is_as = false;
Damiend99b0522013-12-21 18:17:45 +00001216 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
1217 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien429d7192013-10-04 19:53:11 +01001218 // a name of the form x as y; unwrap it
Damiend99b0522013-12-21 18:17:45 +00001219 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001220 pn = pns->nodes[0];
1221 is_as = true;
1222 }
Damiend99b0522013-12-21 18:17:45 +00001223 if (MP_PARSE_NODE_IS_ID(pn)) {
Damien429d7192013-10-04 19:53:11 +01001224 // just a simple name
Damiend99b0522013-12-21 18:17:45 +00001225 *q2 = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01001226 if (!is_as) {
1227 *q1 = *q2;
1228 }
Damien Georgeb9791222014-01-23 00:34:21 +00001229 EMIT_ARG(import_name, *q2);
Damiend99b0522013-12-21 18:17:45 +00001230 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
1231 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
1232 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
Damien429d7192013-10-04 19:53:11 +01001233 // a name of the form a.b.c
1234 if (!is_as) {
Damiend99b0522013-12-21 18:17:45 +00001235 *q1 = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damien429d7192013-10-04 19:53:11 +01001236 }
Damiend99b0522013-12-21 18:17:45 +00001237 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001238 int len = n - 1;
1239 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00001240 len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001241 }
Damien George55baff42014-01-21 21:40:13 +00001242 byte *q_ptr;
1243 byte *str_dest = qstr_build_start(len, &q_ptr);
Damien429d7192013-10-04 19:53:11 +01001244 for (int i = 0; i < n; i++) {
1245 if (i > 0) {
Damien Georgefe8fb912014-01-02 16:36:09 +00001246 *str_dest++ = '.';
Damien429d7192013-10-04 19:53:11 +01001247 }
Damien George55baff42014-01-21 21:40:13 +00001248 uint str_src_len;
1249 const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00001250 memcpy(str_dest, str_src, str_src_len);
1251 str_dest += str_src_len;
Damien429d7192013-10-04 19:53:11 +01001252 }
Damien George55baff42014-01-21 21:40:13 +00001253 *q2 = qstr_build_end(q_ptr);
Damien Georgeb9791222014-01-23 00:34:21 +00001254 EMIT_ARG(import_name, *q2);
Damien429d7192013-10-04 19:53:11 +01001255 if (is_as) {
1256 for (int i = 1; i < n; i++) {
Damien Georgeb9791222014-01-23 00:34:21 +00001257 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01001258 }
1259 }
1260 } else {
1261 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001262 // This covers relative imports starting with dot(s) like "from .foo import"
Damien429d7192013-10-04 19:53:11 +01001263 assert(0);
1264 }
1265 } else {
1266 // TODO not implemented
Paul Sokolovskya1aba362014-02-20 13:21:31 +02001267 // This covers relative imports with dots only like "from .. import"
Damien429d7192013-10-04 19:53:11 +01001268 assert(0);
1269 }
1270}
1271
Damiend99b0522013-12-21 18:17:45 +00001272void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
Damien Georgeb9791222014-01-23 00:34:21 +00001273 EMIT_ARG(load_const_small_int, 0); // ??
1274 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01001275 qstr q1, q2;
1276 do_import_name(comp, pn, &q1, &q2);
Damien Georgeb9791222014-01-23 00:34:21 +00001277 EMIT_ARG(store_id, q1);
Damien429d7192013-10-04 19:53:11 +01001278}
1279
Damiend99b0522013-12-21 18:17:45 +00001280void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001281 apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
1282}
1283
Damiend99b0522013-12-21 18:17:45 +00001284void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
1285 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001286 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001287
1288 // build the "fromlist" tuple
1289#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001290 EMIT_ARG(load_const_verbatim_str, "('*',)");
Damiendb4c3612013-12-10 17:27:24 +00001291#else
Damien Georgeb9791222014-01-23 00:34:21 +00001292 EMIT_ARG(load_const_str, QSTR_FROM_STR_STATIC("*"), false);
1293 EMIT_ARG(build_tuple, 1);
Damiendb4c3612013-12-10 17:27:24 +00001294#endif
1295
1296 // do the import
Damien429d7192013-10-04 19:53:11 +01001297 qstr dummy_q, id1;
1298 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1299 EMIT(import_star);
Damiendb4c3612013-12-10 17:27:24 +00001300
Damien429d7192013-10-04 19:53:11 +01001301 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001302 EMIT_ARG(load_const_small_int, 0); // level 0 for __import__
Damiendb4c3612013-12-10 17:27:24 +00001303
1304 // build the "fromlist" tuple
Damiend99b0522013-12-21 18:17:45 +00001305 mp_parse_node_t *pn_nodes;
Damien429d7192013-10-04 19:53:11 +01001306 int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
Damiendb4c3612013-12-10 17:27:24 +00001307#if MICROPY_EMIT_CPYTHON
Damien02f89412013-12-12 15:13:36 +00001308 {
1309 vstr_t *vstr = vstr_new();
1310 vstr_printf(vstr, "(");
1311 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001312 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1313 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1314 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien02f89412013-12-12 15:13:36 +00001315 if (i > 0) {
1316 vstr_printf(vstr, ", ");
1317 }
1318 vstr_printf(vstr, "'");
Damien George55baff42014-01-21 21:40:13 +00001319 uint len;
1320 const byte *str = qstr_data(id2, &len);
1321 vstr_add_strn(vstr, (const char*)str, len);
Damien02f89412013-12-12 15:13:36 +00001322 vstr_printf(vstr, "'");
Damien429d7192013-10-04 19:53:11 +01001323 }
Damien02f89412013-12-12 15:13:36 +00001324 if (n == 1) {
1325 vstr_printf(vstr, ",");
1326 }
1327 vstr_printf(vstr, ")");
Damien Georgeb9791222014-01-23 00:34:21 +00001328 EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
Damien02f89412013-12-12 15:13:36 +00001329 vstr_free(vstr);
Damien429d7192013-10-04 19:53:11 +01001330 }
Damiendb4c3612013-12-10 17:27:24 +00001331#else
1332 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001333 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1334 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1335 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001336 EMIT_ARG(load_const_str, id2, false);
Damiendb4c3612013-12-10 17:27:24 +00001337 }
Damien Georgeb9791222014-01-23 00:34:21 +00001338 EMIT_ARG(build_tuple, n);
Damiendb4c3612013-12-10 17:27:24 +00001339#endif
1340
1341 // do the import
Damien429d7192013-10-04 19:53:11 +01001342 qstr dummy_q, id1;
1343 do_import_name(comp, pns->nodes[0], &dummy_q, &id1);
1344 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001345 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
1346 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
1347 qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
Damien Georgeb9791222014-01-23 00:34:21 +00001348 EMIT_ARG(import_from, id2);
Damiend99b0522013-12-21 18:17:45 +00001349 if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
Damien Georgeb9791222014-01-23 00:34:21 +00001350 EMIT_ARG(store_id, id2);
Damien429d7192013-10-04 19:53:11 +01001351 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001352 EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
Damien429d7192013-10-04 19:53:11 +01001353 }
1354 }
1355 EMIT(pop_top);
1356 }
1357}
1358
Damiend99b0522013-12-21 18:17:45 +00001359void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001360 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001361 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1362 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001363 } else {
Damiend99b0522013-12-21 18:17:45 +00001364 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1365 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001366 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001367 scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001368 }
Damien429d7192013-10-04 19:53:11 +01001369 }
1370 }
1371}
1372
Damiend99b0522013-12-21 18:17:45 +00001373void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien415eb6f2013-10-05 12:19:06 +01001374 if (comp->pass == PASS_1) {
Damiend99b0522013-12-21 18:17:45 +00001375 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
1376 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien415eb6f2013-10-05 12:19:06 +01001377 } else {
Damiend99b0522013-12-21 18:17:45 +00001378 pns = (mp_parse_node_struct_t*)pns->nodes[0];
1379 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien415eb6f2013-10-05 12:19:06 +01001380 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00001381 scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien415eb6f2013-10-05 12:19:06 +01001382 }
Damien429d7192013-10-04 19:53:11 +01001383 }
1384 }
1385}
1386
Damiend99b0522013-12-21 18:17:45 +00001387void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001388 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001389 c_if_cond(comp, pns->nodes[0], true, l_end);
Damien Georgeb9791222014-01-23 00:34:21 +00001390 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 +00001391 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien429d7192013-10-04 19:53:11 +01001392 // assertion message
1393 compile_node(comp, pns->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00001394 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01001395 }
Damien Georgeb9791222014-01-23 00:34:21 +00001396 EMIT_ARG(raise_varargs, 1);
1397 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001398}
1399
Damiend99b0522013-12-21 18:17:45 +00001400void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001401 // TODO proper and/or short circuiting
1402
Damienb05d7072013-10-05 13:37:10 +01001403 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001404
Damienb05d7072013-10-05 13:37:10 +01001405 int l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001406 c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
1407
1408 compile_node(comp, pns->nodes[1]); // if block
Damiend99b0522013-12-21 18:17:45 +00001409 //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 +01001410 // jump over elif/else blocks if they exist
Damien415eb6f2013-10-05 12:19:06 +01001411 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001412 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001413 }
1414 //}
Damien Georgeb9791222014-01-23 00:34:21 +00001415 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001416
Damiend99b0522013-12-21 18:17:45 +00001417 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001418 // compile elif blocks
1419
Damiend99b0522013-12-21 18:17:45 +00001420 mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
Damien429d7192013-10-04 19:53:11 +01001421
Damiend99b0522013-12-21 18:17:45 +00001422 if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
Damien429d7192013-10-04 19:53:11 +01001423 // multiple elif blocks
1424
Damiend99b0522013-12-21 18:17:45 +00001425 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
Damien429d7192013-10-04 19:53:11 +01001426 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00001427 mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
Damienb05d7072013-10-05 13:37:10 +01001428 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001429 c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
1430
1431 compile_node(comp, pns_elif2->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001432 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001433 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001434 }
Damien Georgeb9791222014-01-23 00:34:21 +00001435 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001436 }
1437
1438 } else {
1439 // a single elif block
1440
Damienb05d7072013-10-05 13:37:10 +01001441 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001442 c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
1443
1444 compile_node(comp, pns_elif->nodes[1]); // elif block
Damien415eb6f2013-10-05 12:19:06 +01001445 if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
Damien Georgeb9791222014-01-23 00:34:21 +00001446 EMIT_ARG(jump, l_end);
Damien429d7192013-10-04 19:53:11 +01001447 }
Damien Georgeb9791222014-01-23 00:34:21 +00001448 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01001449 }
1450 }
1451
1452 // compile else block
1453 compile_node(comp, pns->nodes[3]); // can be null
1454
Damien Georgeb9791222014-01-23 00:34:21 +00001455 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001456}
1457
Damien Georgecbddb272014-02-01 20:08:18 +00001458#define START_BREAK_CONTINUE_BLOCK \
1459 int old_break_label = comp->break_label; \
1460 int old_continue_label = comp->continue_label; \
1461 int break_label = comp_next_label(comp); \
1462 int continue_label = comp_next_label(comp); \
1463 comp->break_label = break_label; \
1464 comp->continue_label = continue_label; \
1465 comp->break_continue_except_level = comp->cur_except_level;
1466
1467#define END_BREAK_CONTINUE_BLOCK \
1468 comp->break_label = old_break_label; \
1469 comp->continue_label = old_continue_label; \
1470 comp->break_continue_except_level = comp->cur_except_level;
1471
Damiend99b0522013-12-21 18:17:45 +00001472void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgecbddb272014-02-01 20:08:18 +00001473 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001474
Damience89a212013-10-15 22:25:17 +01001475 // compared to CPython, we have an optimised version of while loops
1476#if MICROPY_EMIT_CPYTHON
1477 int done_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001478 EMIT_ARG(setup_loop, break_label);
1479 EMIT_ARG(label_assign, continue_label);
Damien429d7192013-10-04 19:53:11 +01001480 c_if_cond(comp, pns->nodes[0], false, done_label); // condition
1481 compile_node(comp, pns->nodes[1]); // body
Damien415eb6f2013-10-05 12:19:06 +01001482 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00001483 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001484 }
Damien Georgeb9791222014-01-23 00:34:21 +00001485 EMIT_ARG(label_assign, done_label);
Damien429d7192013-10-04 19:53:11 +01001486 // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
1487 // this is a small hack to agree with CPython
1488 if (!node_is_const_true(pns->nodes[0])) {
1489 EMIT(pop_block);
1490 }
Damience89a212013-10-15 22:25:17 +01001491#else
1492 int top_label = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001493 EMIT_ARG(jump, continue_label);
1494 EMIT_ARG(label_assign, top_label);
Damience89a212013-10-15 22:25:17 +01001495 compile_node(comp, pns->nodes[1]); // body
Damien Georgeb9791222014-01-23 00:34:21 +00001496 EMIT_ARG(label_assign, continue_label);
Damience89a212013-10-15 22:25:17 +01001497 c_if_cond(comp, pns->nodes[0], true, top_label); // condition
1498#endif
1499
1500 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001501 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001502
1503 compile_node(comp, pns->nodes[2]); // else
1504
Damien Georgeb9791222014-01-23 00:34:21 +00001505 EMIT_ARG(label_assign, break_label);
Damien429d7192013-10-04 19:53:11 +01001506}
1507
Damienf72fd0e2013-11-06 20:20:49 +00001508// TODO preload end and step onto stack if they are not constants
1509// TODO check if step is negative and do opposite test
Damiend99b0522013-12-21 18:17:45 +00001510void 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 +00001511 START_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001512
1513 int top_label = comp_next_label(comp);
Damien George600ae732014-01-21 23:48:04 +00001514 int entry_label = comp_next_label(comp);
Damienf72fd0e2013-11-06 20:20:49 +00001515
1516 // compile: var = start
1517 compile_node(comp, pn_start);
1518 c_assign(comp, pn_var, ASSIGN_STORE);
1519
Damien Georgeb9791222014-01-23 00:34:21 +00001520 EMIT_ARG(jump, entry_label);
1521 EMIT_ARG(label_assign, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001522
Damienf3822fc2013-11-09 20:12:03 +00001523 // compile body
1524 compile_node(comp, pn_body);
1525
Damien Georgeb9791222014-01-23 00:34:21 +00001526 EMIT_ARG(label_assign, continue_label);
Damien George600ae732014-01-21 23:48:04 +00001527
Damienf72fd0e2013-11-06 20:20:49 +00001528 // compile: var += step
1529 c_assign(comp, pn_var, ASSIGN_AUG_LOAD);
1530 compile_node(comp, pn_step);
Damien Georgeb9791222014-01-23 00:34:21 +00001531 EMIT_ARG(binary_op, RT_BINARY_OP_INPLACE_ADD);
Damienf72fd0e2013-11-06 20:20:49 +00001532 c_assign(comp, pn_var, ASSIGN_AUG_STORE);
1533
Damien Georgeb9791222014-01-23 00:34:21 +00001534 EMIT_ARG(label_assign, entry_label);
Damienf72fd0e2013-11-06 20:20:49 +00001535
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001536 // compile: if var <cond> end: goto top
Damienf72fd0e2013-11-06 20:20:49 +00001537 compile_node(comp, pn_var);
1538 compile_node(comp, pn_end);
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02001539 assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
1540 if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
Damien George9aa2a522014-02-01 23:04:09 +00001541 EMIT_ARG(binary_op, RT_BINARY_OP_LESS);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001542 } else {
Damien George9aa2a522014-02-01 23:04:09 +00001543 EMIT_ARG(binary_op, RT_BINARY_OP_MORE);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001544 }
Damien Georgeb9791222014-01-23 00:34:21 +00001545 EMIT_ARG(pop_jump_if_true, top_label);
Damienf72fd0e2013-11-06 20:20:49 +00001546
1547 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001548 END_BREAK_CONTINUE_BLOCK
Damienf72fd0e2013-11-06 20:20:49 +00001549
1550 compile_node(comp, pn_else);
1551
Damien Georgeb9791222014-01-23 00:34:21 +00001552 EMIT_ARG(label_assign, break_label);
Damienf72fd0e2013-11-06 20:20:49 +00001553}
1554
Damiend99b0522013-12-21 18:17:45 +00001555void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienf72fd0e2013-11-06 20:20:49 +00001556#if !MICROPY_EMIT_CPYTHON
1557 // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
1558 // this is actually slower, but uses no heap memory
1559 // for viper it will be much, much faster
Damiend99b0522013-12-21 18:17:45 +00001560 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)) {
1561 mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001562 if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
1563 && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
1564 && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
1565 && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
Damiend99b0522013-12-21 18:17:45 +00001566 mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
1567 mp_parse_node_t *args;
Damienf72fd0e2013-11-06 20:20:49 +00001568 int n_args = list_get(&pn_range_args, PN_arglist, &args);
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001569 mp_parse_node_t pn_range_start;
1570 mp_parse_node_t pn_range_end;
1571 mp_parse_node_t pn_range_step;
1572 bool optimize = false;
Damienf72fd0e2013-11-06 20:20:49 +00001573 if (1 <= n_args && n_args <= 3) {
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001574 optimize = true;
Damienf72fd0e2013-11-06 20:20:49 +00001575 if (n_args == 1) {
Damiend99b0522013-12-21 18:17:45 +00001576 pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
Damienf72fd0e2013-11-06 20:20:49 +00001577 pn_range_end = args[0];
Damiend99b0522013-12-21 18:17:45 +00001578 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001579 } else if (n_args == 2) {
1580 pn_range_start = args[0];
1581 pn_range_end = args[1];
Damiend99b0522013-12-21 18:17:45 +00001582 pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
Damienf72fd0e2013-11-06 20:20:49 +00001583 } else {
1584 pn_range_start = args[0];
1585 pn_range_end = args[1];
1586 pn_range_step = args[2];
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001587 // We need to know sign of step. This is possible only if it's constant
1588 if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
1589 optimize = false;
1590 }
Damienf72fd0e2013-11-06 20:20:49 +00001591 }
Paul Sokolovsky899c69f2014-01-10 20:38:57 +02001592 }
1593 if (optimize) {
Damienf72fd0e2013-11-06 20:20:49 +00001594 compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
1595 return;
1596 }
1597 }
1598 }
1599#endif
1600
Damien Georgecbddb272014-02-01 20:08:18 +00001601 START_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001602
Damienb05d7072013-10-05 13:37:10 +01001603 int pop_label = comp_next_label(comp);
1604 int end_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001605
Damience89a212013-10-15 22:25:17 +01001606 // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
1607#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00001608 EMIT_ARG(setup_loop, end_label);
Damience89a212013-10-15 22:25:17 +01001609#endif
1610
Damien429d7192013-10-04 19:53:11 +01001611 compile_node(comp, pns->nodes[1]); // iterator
1612 EMIT(get_iter);
Damien Georgecbddb272014-02-01 20:08:18 +00001613 EMIT_ARG(label_assign, continue_label);
Damien Georgeb9791222014-01-23 00:34:21 +00001614 EMIT_ARG(for_iter, pop_label);
Damien429d7192013-10-04 19:53:11 +01001615 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
1616 compile_node(comp, pns->nodes[2]); // body
Damien415eb6f2013-10-05 12:19:06 +01001617 if (!EMIT(last_emit_was_return_value)) {
Damien Georgecbddb272014-02-01 20:08:18 +00001618 EMIT_ARG(jump, continue_label);
Damien429d7192013-10-04 19:53:11 +01001619 }
Damien Georgeb9791222014-01-23 00:34:21 +00001620 EMIT_ARG(label_assign, pop_label);
Damien429d7192013-10-04 19:53:11 +01001621 EMIT(for_iter_end);
1622
1623 // break/continue apply to outer loop (if any) in the else block
Damien Georgecbddb272014-02-01 20:08:18 +00001624 END_BREAK_CONTINUE_BLOCK
Damien429d7192013-10-04 19:53:11 +01001625
Damience89a212013-10-15 22:25:17 +01001626#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01001627 EMIT(pop_block);
Damience89a212013-10-15 22:25:17 +01001628#endif
Damien429d7192013-10-04 19:53:11 +01001629
1630 compile_node(comp, pns->nodes[3]); // else (not tested)
1631
Damien Georgeb9791222014-01-23 00:34:21 +00001632 EMIT_ARG(label_assign, break_label);
1633 EMIT_ARG(label_assign, end_label);
Damien429d7192013-10-04 19:53:11 +01001634}
1635
Damiend99b0522013-12-21 18:17:45 +00001636void 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 +01001637 // this function is a bit of a hack at the moment
1638 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1639
1640 // setup code
1641 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001642 int l1 = comp_next_label(comp);
1643 int success_label = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001644
Damien Georgeb9791222014-01-23 00:34:21 +00001645 EMIT_ARG(setup_except, l1);
Damien George8dcc0c72014-03-27 10:55:21 +00001646 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001647
Damien429d7192013-10-04 19:53:11 +01001648 compile_node(comp, pn_body); // body
1649 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001650 EMIT_ARG(jump, success_label);
1651 EMIT_ARG(label_assign, l1);
Damienb05d7072013-10-05 13:37:10 +01001652 int l2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001653
1654 for (int i = 0; i < n_except; i++) {
Damiend99b0522013-12-21 18:17:45 +00001655 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
1656 mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
Damien429d7192013-10-04 19:53:11 +01001657
1658 qstr qstr_exception_local = 0;
Damienb05d7072013-10-05 13:37:10 +01001659 int end_finally_label = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001660
Damiend99b0522013-12-21 18:17:45 +00001661 if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01001662 // this is a catch all exception handler
1663 if (i + 1 != n_except) {
Damien Georgef41fdd02014-03-03 23:19:11 +00001664 compile_syntax_error(comp, "default 'except:' must be last");
Damien429d7192013-10-04 19:53:11 +01001665 return;
1666 }
1667 } else {
1668 // this exception handler requires a match to a certain type of exception
Damiend99b0522013-12-21 18:17:45 +00001669 mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
1670 if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
1671 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
1672 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
Damien429d7192013-10-04 19:53:11 +01001673 // handler binds the exception to a local
1674 pns_exception_expr = pns3->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00001675 qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01001676 }
1677 }
1678 EMIT(dup_top);
1679 compile_node(comp, pns_exception_expr);
Damien George9aa2a522014-02-01 23:04:09 +00001680 EMIT_ARG(binary_op, RT_BINARY_OP_EXCEPTION_MATCH);
Damien Georgeb9791222014-01-23 00:34:21 +00001681 EMIT_ARG(pop_jump_if_false, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001682 }
1683
1684 EMIT(pop_top);
1685
1686 if (qstr_exception_local == 0) {
1687 EMIT(pop_top);
1688 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00001689 EMIT_ARG(store_id, qstr_exception_local);
Damien429d7192013-10-04 19:53:11 +01001690 }
1691
1692 EMIT(pop_top);
1693
Damiene2880aa2013-12-20 14:22:59 +00001694 int l3 = 0;
Damien429d7192013-10-04 19:53:11 +01001695 if (qstr_exception_local != 0) {
Damienb05d7072013-10-05 13:37:10 +01001696 l3 = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00001697 EMIT_ARG(setup_finally, l3);
Damien George8dcc0c72014-03-27 10:55:21 +00001698 compile_increase_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001699 }
1700 compile_node(comp, pns_except->nodes[1]);
1701 if (qstr_exception_local != 0) {
1702 EMIT(pop_block);
1703 }
1704 EMIT(pop_except);
1705 if (qstr_exception_local != 0) {
Damien Georgeb9791222014-01-23 00:34:21 +00001706 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1707 EMIT_ARG(label_assign, l3);
1708 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1709 EMIT_ARG(store_id, qstr_exception_local);
1710 EMIT_ARG(delete_id, qstr_exception_local);
Damien Georgecbddb272014-02-01 20:08:18 +00001711
Damien George8dcc0c72014-03-27 10:55:21 +00001712 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001713 EMIT(end_finally);
1714 }
Damien Georgeb9791222014-01-23 00:34:21 +00001715 EMIT_ARG(jump, l2);
1716 EMIT_ARG(label_assign, end_finally_label);
Damien429d7192013-10-04 19:53:11 +01001717 }
1718
Damien George8dcc0c72014-03-27 10:55:21 +00001719 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001720 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001721
Damien Georgeb9791222014-01-23 00:34:21 +00001722 EMIT_ARG(label_assign, success_label);
Damien429d7192013-10-04 19:53:11 +01001723 compile_node(comp, pn_else); // else block, can be null
Damien Georgeb9791222014-01-23 00:34:21 +00001724 EMIT_ARG(label_assign, l2);
1725 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001726}
1727
Damiend99b0522013-12-21 18:17:45 +00001728void 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 +01001729 // don't understand how the stack works with exceptions, so we force it to return to the correct value
1730 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001731 int l_finally_block = comp_next_label(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001732
Damien Georgeb9791222014-01-23 00:34:21 +00001733 EMIT_ARG(setup_finally, l_finally_block);
Damien George8dcc0c72014-03-27 10:55:21 +00001734 compile_increase_except_level(comp);
Damien Georgecbddb272014-02-01 20:08:18 +00001735
Damien429d7192013-10-04 19:53:11 +01001736 if (n_except == 0) {
Damiend99b0522013-12-21 18:17:45 +00001737 assert(MP_PARSE_NODE_IS_NULL(pn_else));
Damien429d7192013-10-04 19:53:11 +01001738 compile_node(comp, pn_body);
1739 } else {
1740 compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
1741 }
1742 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001743 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1744 EMIT_ARG(label_assign, l_finally_block);
Damien429d7192013-10-04 19:53:11 +01001745 compile_node(comp, pn_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001746
Damien George8dcc0c72014-03-27 10:55:21 +00001747 compile_decrease_except_level(comp);
Damien429d7192013-10-04 19:53:11 +01001748 EMIT(end_finally);
Damien Georgecbddb272014-02-01 20:08:18 +00001749
Damien Georgeb9791222014-01-23 00:34:21 +00001750 EMIT_ARG(set_stack_size, stack_size);
Damien429d7192013-10-04 19:53:11 +01001751}
1752
Damiend99b0522013-12-21 18:17:45 +00001753void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1754 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
1755 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
1756 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
Damien429d7192013-10-04 19:53:11 +01001757 // just try-finally
Damiend99b0522013-12-21 18:17:45 +00001758 compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
1759 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
Damien429d7192013-10-04 19:53:11 +01001760 // try-except and possibly else and/or finally
Damiend99b0522013-12-21 18:17:45 +00001761 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001762 int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001763 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
Damien429d7192013-10-04 19:53:11 +01001764 // no finally
1765 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
1766 } else {
1767 // have finally
Damiend99b0522013-12-21 18:17:45 +00001768 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 +01001769 }
1770 } else {
1771 // just try-except
Damiend99b0522013-12-21 18:17:45 +00001772 mp_parse_node_t *pn_excepts;
Damien429d7192013-10-04 19:53:11 +01001773 int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
Damiend99b0522013-12-21 18:17:45 +00001774 compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
Damien429d7192013-10-04 19:53:11 +01001775 }
1776 } else {
1777 // shouldn't happen
1778 assert(0);
1779 }
1780}
1781
Damiend99b0522013-12-21 18:17:45 +00001782void 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 +01001783 if (n == 0) {
1784 // no more pre-bits, compile the body of the with
1785 compile_node(comp, body);
1786 } else {
Damienb05d7072013-10-05 13:37:10 +01001787 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001788 if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
Damien429d7192013-10-04 19:53:11 +01001789 // this pre-bit is of the form "a as b"
Damiend99b0522013-12-21 18:17:45 +00001790 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
Damien429d7192013-10-04 19:53:11 +01001791 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001792 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001793 c_assign(comp, pns->nodes[1], ASSIGN_STORE);
1794 } else {
1795 // this pre-bit is just an expression
1796 compile_node(comp, nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001797 EMIT_ARG(setup_with, l_end);
Damien429d7192013-10-04 19:53:11 +01001798 EMIT(pop_top);
1799 }
1800 // compile additional pre-bits and the body
1801 compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
1802 // finish this with block
1803 EMIT(pop_block);
Damien Georgeb9791222014-01-23 00:34:21 +00001804 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1805 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001806 EMIT(with_cleanup);
1807 EMIT(end_finally);
1808 }
1809}
1810
Damiend99b0522013-12-21 18:17:45 +00001811void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001812 // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
Damiend99b0522013-12-21 18:17:45 +00001813 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01001814 int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
1815 assert(n > 0);
1816
1817 // compile in a nested fashion
1818 compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
1819}
1820
Damiend99b0522013-12-21 18:17:45 +00001821void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
1822 if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001823 if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
1824 // for REPL, evaluate then print the expression
Damien Georgeb9791222014-01-23 00:34:21 +00001825 EMIT_ARG(load_id, MP_QSTR___repl_print__);
Damien5ac1b2e2013-10-18 19:58:12 +01001826 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001827 EMIT_ARG(call_function, 1, 0, false, false);
Damien5ac1b2e2013-10-18 19:58:12 +01001828 EMIT(pop_top);
1829
Damien429d7192013-10-04 19:53:11 +01001830 } else {
Damien5ac1b2e2013-10-18 19:58:12 +01001831 // for non-REPL, evaluate then discard the expression
Damiend99b0522013-12-21 18:17:45 +00001832 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien5ac1b2e2013-10-18 19:58:12 +01001833 // do nothing with a lonely constant
1834 } else {
1835 compile_node(comp, pns->nodes[0]); // just an expression
1836 EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
1837 }
Damien429d7192013-10-04 19:53:11 +01001838 }
1839 } else {
Damiend99b0522013-12-21 18:17:45 +00001840 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
1841 int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
Damien429d7192013-10-04 19:53:11 +01001842 if (kind == PN_expr_stmt_augassign) {
1843 c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
1844 compile_node(comp, pns1->nodes[1]); // rhs
Damiend99b0522013-12-21 18:17:45 +00001845 assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
Damien George7e5fb242014-02-01 22:18:47 +00001846 rt_binary_op_t op;
Damiend99b0522013-12-21 18:17:45 +00001847 switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
Damien George7e5fb242014-02-01 22:18:47 +00001848 case MP_TOKEN_DEL_PIPE_EQUAL: op = RT_BINARY_OP_INPLACE_OR; break;
1849 case MP_TOKEN_DEL_CARET_EQUAL: op = RT_BINARY_OP_INPLACE_XOR; break;
1850 case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = RT_BINARY_OP_INPLACE_AND; break;
1851 case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = RT_BINARY_OP_INPLACE_LSHIFT; break;
1852 case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = RT_BINARY_OP_INPLACE_RSHIFT; break;
1853 case MP_TOKEN_DEL_PLUS_EQUAL: op = RT_BINARY_OP_INPLACE_ADD; break;
1854 case MP_TOKEN_DEL_MINUS_EQUAL: op = RT_BINARY_OP_INPLACE_SUBTRACT; break;
1855 case MP_TOKEN_DEL_STAR_EQUAL: op = RT_BINARY_OP_INPLACE_MULTIPLY; break;
1856 case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = RT_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
1857 case MP_TOKEN_DEL_SLASH_EQUAL: op = RT_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
1858 case MP_TOKEN_DEL_PERCENT_EQUAL: op = RT_BINARY_OP_INPLACE_MODULO; break;
1859 case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = RT_BINARY_OP_INPLACE_POWER; break;
1860 default: assert(0); op = RT_BINARY_OP_INPLACE_OR; // shouldn't happen
Damien429d7192013-10-04 19:53:11 +01001861 }
Damien George7e5fb242014-02-01 22:18:47 +00001862 EMIT_ARG(binary_op, op);
Damien429d7192013-10-04 19:53:11 +01001863 c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
1864 } else if (kind == PN_expr_stmt_assign_list) {
Damiend99b0522013-12-21 18:17:45 +00001865 int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
1866 compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
Damien429d7192013-10-04 19:53:11 +01001867 // following CPython, we store left-most first
1868 if (rhs > 0) {
1869 EMIT(dup_top);
1870 }
1871 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1872 for (int i = 0; i < rhs; i++) {
1873 if (i + 1 < rhs) {
1874 EMIT(dup_top);
1875 }
Damiend99b0522013-12-21 18:17:45 +00001876 c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
Damien429d7192013-10-04 19:53:11 +01001877 }
1878 } else if (kind == PN_expr_stmt_assign) {
Damiend99b0522013-12-21 18:17:45 +00001879 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1880 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1881 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
1882 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
Damien429d7192013-10-04 19:53:11 +01001883 // optimisation for a, b = c, d; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001884 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1885 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001886 compile_node(comp, pns10->nodes[0]); // rhs
1887 compile_node(comp, pns10->nodes[1]); // rhs
1888 EMIT(rot_two);
1889 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1890 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
Damiend99b0522013-12-21 18:17:45 +00001891 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
1892 && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
1893 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
1894 && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
Damien429d7192013-10-04 19:53:11 +01001895 // optimisation for a, b, c = d, e, f; to match CPython's optimisation
Damiend99b0522013-12-21 18:17:45 +00001896 mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
1897 mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01001898 compile_node(comp, pns10->nodes[0]); // rhs
1899 compile_node(comp, pns10->nodes[1]); // rhs
1900 compile_node(comp, pns10->nodes[2]); // rhs
1901 EMIT(rot_three);
1902 EMIT(rot_two);
1903 c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
1904 c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
1905 c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
1906 } else {
1907 compile_node(comp, pns1->nodes[0]); // rhs
1908 c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
1909 }
1910 } else {
1911 // shouldn't happen
1912 assert(0);
1913 }
1914 }
1915}
1916
Damiend99b0522013-12-21 18:17:45 +00001917void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, rt_binary_op_t binary_op) {
1918 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001919 compile_node(comp, pns->nodes[0]);
1920 for (int i = 1; i < num_nodes; i += 1) {
1921 compile_node(comp, pns->nodes[i]);
Damien Georgeb9791222014-01-23 00:34:21 +00001922 EMIT_ARG(binary_op, binary_op);
Damien429d7192013-10-04 19:53:11 +01001923 }
1924}
1925
Damiend99b0522013-12-21 18:17:45 +00001926void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
1927 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
1928 mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001929
1930 int stack_size = EMIT(get_stack_size);
Damienb05d7072013-10-05 13:37:10 +01001931 int l_fail = comp_next_label(comp);
1932 int l_end = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001933 c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
1934 compile_node(comp, pns->nodes[0]); // success value
Damien Georgeb9791222014-01-23 00:34:21 +00001935 EMIT_ARG(jump, l_end);
1936 EMIT_ARG(label_assign, l_fail);
1937 EMIT_ARG(set_stack_size, stack_size); // force stack size reset
Damien429d7192013-10-04 19:53:11 +01001938 compile_node(comp, pns_test_if_else->nodes[1]); // failure value
Damien Georgeb9791222014-01-23 00:34:21 +00001939 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001940}
1941
Damiend99b0522013-12-21 18:17:45 +00001942void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001943 // TODO default params etc for lambda; possibly just use funcdef code
Damiend99b0522013-12-21 18:17:45 +00001944 //mp_parse_node_t pn_params = pns->nodes[0];
1945 //mp_parse_node_t pn_body = pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01001946
1947 if (comp->pass == PASS_1) {
1948 // create a new scope for this lambda
Damiend99b0522013-12-21 18:17:45 +00001949 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 +01001950 // store the lambda scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00001951 pns->nodes[2] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01001952 }
1953
1954 // get the scope for this lambda
1955 scope_t *this_scope = (scope_t*)pns->nodes[2];
1956
1957 // make the lambda
1958 close_over_variables_etc(comp, this_scope, 0, 0);
1959}
1960
Damiend99b0522013-12-21 18:17:45 +00001961void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001962 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001963 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001964 for (int i = 0; i < n; i += 1) {
1965 compile_node(comp, pns->nodes[i]);
1966 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00001967 EMIT_ARG(jump_if_true_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01001968 }
1969 }
Damien Georgeb9791222014-01-23 00:34:21 +00001970 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001971}
1972
Damiend99b0522013-12-21 18:17:45 +00001973void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damienb05d7072013-10-05 13:37:10 +01001974 int l_end = comp_next_label(comp);
Damiend99b0522013-12-21 18:17:45 +00001975 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001976 for (int i = 0; i < n; i += 1) {
1977 compile_node(comp, pns->nodes[i]);
1978 if (i + 1 < n) {
Damien Georgeb9791222014-01-23 00:34:21 +00001979 EMIT_ARG(jump_if_false_or_pop, l_end);
Damien429d7192013-10-04 19:53:11 +01001980 }
1981 }
Damien Georgeb9791222014-01-23 00:34:21 +00001982 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01001983}
1984
Damiend99b0522013-12-21 18:17:45 +00001985void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001986 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00001987 EMIT_ARG(unary_op, RT_UNARY_OP_NOT);
Damien429d7192013-10-04 19:53:11 +01001988}
1989
Damiend99b0522013-12-21 18:17:45 +00001990void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01001991 int stack_size = EMIT(get_stack_size);
Damiend99b0522013-12-21 18:17:45 +00001992 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01001993 compile_node(comp, pns->nodes[0]);
1994 bool multi = (num_nodes > 3);
1995 int l_fail = 0;
1996 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01001997 l_fail = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01001998 }
1999 for (int i = 1; i + 1 < num_nodes; i += 2) {
2000 compile_node(comp, pns->nodes[i + 1]);
2001 if (i + 2 < num_nodes) {
2002 EMIT(dup_top);
2003 EMIT(rot_three);
2004 }
Damien George7e5fb242014-02-01 22:18:47 +00002005 if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
2006 rt_binary_op_t op;
2007 switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
Damien George9aa2a522014-02-01 23:04:09 +00002008 case MP_TOKEN_OP_LESS: op = RT_BINARY_OP_LESS; break;
2009 case MP_TOKEN_OP_MORE: op = RT_BINARY_OP_MORE; break;
2010 case MP_TOKEN_OP_DBL_EQUAL: op = RT_BINARY_OP_EQUAL; break;
2011 case MP_TOKEN_OP_LESS_EQUAL: op = RT_BINARY_OP_LESS_EQUAL; break;
2012 case MP_TOKEN_OP_MORE_EQUAL: op = RT_BINARY_OP_MORE_EQUAL; break;
2013 case MP_TOKEN_OP_NOT_EQUAL: op = RT_BINARY_OP_NOT_EQUAL; break;
2014 case MP_TOKEN_KW_IN: op = RT_BINARY_OP_IN; break;
2015 default: assert(0); op = RT_BINARY_OP_LESS; // shouldn't happen
Damien George7e5fb242014-02-01 22:18:47 +00002016 }
2017 EMIT_ARG(binary_op, op);
Damiend99b0522013-12-21 18:17:45 +00002018 } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
2019 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
2020 int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
Damien429d7192013-10-04 19:53:11 +01002021 if (kind == PN_comp_op_not_in) {
Damien George9aa2a522014-02-01 23:04:09 +00002022 EMIT_ARG(binary_op, RT_BINARY_OP_NOT_IN);
Damien429d7192013-10-04 19:53:11 +01002023 } else if (kind == PN_comp_op_is) {
Damiend99b0522013-12-21 18:17:45 +00002024 if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
Damien George9aa2a522014-02-01 23:04:09 +00002025 EMIT_ARG(binary_op, RT_BINARY_OP_IS);
Damien429d7192013-10-04 19:53:11 +01002026 } else {
Damien George9aa2a522014-02-01 23:04:09 +00002027 EMIT_ARG(binary_op, RT_BINARY_OP_IS_NOT);
Damien429d7192013-10-04 19:53:11 +01002028 }
2029 } else {
2030 // shouldn't happen
2031 assert(0);
2032 }
2033 } else {
2034 // shouldn't happen
2035 assert(0);
2036 }
2037 if (i + 2 < num_nodes) {
Damien Georgeb9791222014-01-23 00:34:21 +00002038 EMIT_ARG(jump_if_false_or_pop, l_fail);
Damien429d7192013-10-04 19:53:11 +01002039 }
2040 }
2041 if (multi) {
Damienb05d7072013-10-05 13:37:10 +01002042 int l_end = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002043 EMIT_ARG(jump, l_end);
2044 EMIT_ARG(label_assign, l_fail);
Damien429d7192013-10-04 19:53:11 +01002045 EMIT(rot_two);
2046 EMIT(pop_top);
Damien Georgeb9791222014-01-23 00:34:21 +00002047 EMIT_ARG(label_assign, l_end);
2048 EMIT_ARG(set_stack_size, stack_size + 1); // force stack size
Damien429d7192013-10-04 19:53:11 +01002049 }
2050}
2051
Damiend99b0522013-12-21 18:17:45 +00002052void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002053 // TODO
2054 assert(0);
2055 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002056 //EMIT_ARG(unary_op, "UNARY_STAR");
Damien429d7192013-10-04 19:53:11 +01002057}
2058
Damiend99b0522013-12-21 18:17:45 +00002059void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002060 c_binary_op(comp, pns, RT_BINARY_OP_OR);
2061}
2062
Damiend99b0522013-12-21 18:17:45 +00002063void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002064 c_binary_op(comp, pns, RT_BINARY_OP_XOR);
2065}
2066
Damiend99b0522013-12-21 18:17:45 +00002067void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002068 c_binary_op(comp, pns, RT_BINARY_OP_AND);
2069}
2070
Damiend99b0522013-12-21 18:17:45 +00002071void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2072 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002073 compile_node(comp, pns->nodes[0]);
2074 for (int i = 1; i + 1 < num_nodes; i += 2) {
2075 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002076 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002077 EMIT_ARG(binary_op, RT_BINARY_OP_LSHIFT);
Damiend99b0522013-12-21 18:17:45 +00002078 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002079 EMIT_ARG(binary_op, RT_BINARY_OP_RSHIFT);
Damien429d7192013-10-04 19:53:11 +01002080 } else {
2081 // shouldn't happen
2082 assert(0);
2083 }
2084 }
2085}
2086
Damiend99b0522013-12-21 18:17:45 +00002087void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2088 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002089 compile_node(comp, pns->nodes[0]);
2090 for (int i = 1; i + 1 < num_nodes; i += 2) {
2091 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002092 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002093 EMIT_ARG(binary_op, RT_BINARY_OP_ADD);
Damiend99b0522013-12-21 18:17:45 +00002094 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002095 EMIT_ARG(binary_op, RT_BINARY_OP_SUBTRACT);
Damien429d7192013-10-04 19:53:11 +01002096 } else {
2097 // shouldn't happen
2098 assert(0);
2099 }
2100 }
2101}
2102
Damiend99b0522013-12-21 18:17:45 +00002103void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2104 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002105 compile_node(comp, pns->nodes[0]);
2106 for (int i = 1; i + 1 < num_nodes; i += 2) {
2107 compile_node(comp, pns->nodes[i + 1]);
Damiend99b0522013-12-21 18:17:45 +00002108 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002109 EMIT_ARG(binary_op, RT_BINARY_OP_MULTIPLY);
Damiend99b0522013-12-21 18:17:45 +00002110 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002111 EMIT_ARG(binary_op, RT_BINARY_OP_FLOOR_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002112 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002113 EMIT_ARG(binary_op, RT_BINARY_OP_TRUE_DIVIDE);
Damiend99b0522013-12-21 18:17:45 +00002114 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002115 EMIT_ARG(binary_op, RT_BINARY_OP_MODULO);
Damien429d7192013-10-04 19:53:11 +01002116 } else {
2117 // shouldn't happen
2118 assert(0);
2119 }
2120 }
2121}
2122
Damiend99b0522013-12-21 18:17:45 +00002123void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002124 compile_node(comp, pns->nodes[1]);
Damiend99b0522013-12-21 18:17:45 +00002125 if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002126 EMIT_ARG(unary_op, RT_UNARY_OP_POSITIVE);
Damiend99b0522013-12-21 18:17:45 +00002127 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002128 EMIT_ARG(unary_op, RT_UNARY_OP_NEGATIVE);
Damiend99b0522013-12-21 18:17:45 +00002129 } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002130 EMIT_ARG(unary_op, RT_UNARY_OP_INVERT);
Damien429d7192013-10-04 19:53:11 +01002131 } else {
2132 // shouldn't happen
2133 assert(0);
2134 }
2135}
2136
Damien George35e2a4e2014-02-05 00:51:47 +00002137void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
2138 // this is to handle special super() call
2139 comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
2140
2141 compile_generic_all_nodes(comp, pns);
2142}
2143
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002144STATIC 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 +01002145 // function to call is on top of stack
2146
Damien George35e2a4e2014-02-05 00:51:47 +00002147#if !MICROPY_EMIT_CPYTHON
2148 // this is to handle special super() call
Damien Georgebbcd49a2014-02-06 20:30:16 +00002149 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 +00002150 EMIT_ARG(load_id, MP_QSTR___class__);
2151 // get first argument to function
2152 bool found = false;
2153 for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
2154 if (comp->scope_cur->id_info[i].param) {
2155 EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
2156 found = true;
2157 break;
2158 }
2159 }
2160 if (!found) {
2161 printf("TypeError: super() call cannot find self\n");
2162 return;
2163 }
2164 EMIT_ARG(call_function, 2, 0, false, false);
2165 return;
2166 }
2167#endif
2168
Damien429d7192013-10-04 19:53:11 +01002169 int old_n_arg_keyword = comp->n_arg_keyword;
2170 bool old_have_star_arg = comp->have_star_arg;
2171 bool old_have_dbl_star_arg = comp->have_dbl_star_arg;
2172 comp->n_arg_keyword = 0;
2173 comp->have_star_arg = false;
2174 comp->have_dbl_star_arg = false;
2175
Damien Georgebbcd49a2014-02-06 20:30:16 +00002176 compile_node(comp, pn_arglist); // arguments to function call; can be null
Damien429d7192013-10-04 19:53:11 +01002177
2178 // compute number of positional arguments
Damien Georgebbcd49a2014-02-06 20:30:16 +00002179 int n_positional = n_positional_extra + list_len(pn_arglist, PN_arglist) - comp->n_arg_keyword;
Damien429d7192013-10-04 19:53:11 +01002180 if (comp->have_star_arg) {
2181 n_positional -= 1;
2182 }
2183 if (comp->have_dbl_star_arg) {
2184 n_positional -= 1;
2185 }
2186
2187 if (is_method_call) {
Damien Georgeb9791222014-01-23 00:34:21 +00002188 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 +01002189 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002190 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 +01002191 }
2192
2193 comp->n_arg_keyword = old_n_arg_keyword;
2194 comp->have_star_arg = old_have_star_arg;
2195 comp->have_dbl_star_arg = old_have_dbl_star_arg;
2196}
2197
Damiend99b0522013-12-21 18:17:45 +00002198void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
2199 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien429d7192013-10-04 19:53:11 +01002200 for (int i = 0; i < num_nodes; i++) {
Damiend99b0522013-12-21 18:17:45 +00002201 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 +01002202 // optimisation for method calls a.f(...), following PyPy
Damiend99b0522013-12-21 18:17:45 +00002203 mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
2204 mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
Damien Georgeb9791222014-01-23 00:34:21 +00002205 EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
Damien Georgebbcd49a2014-02-06 20:30:16 +00002206 compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
Damien429d7192013-10-04 19:53:11 +01002207 i += 1;
2208 } else {
2209 compile_node(comp, pns->nodes[i]);
2210 }
Damien George35e2a4e2014-02-05 00:51:47 +00002211 comp->func_arg_is_super = false;
Damien429d7192013-10-04 19:53:11 +01002212 }
2213}
2214
Damiend99b0522013-12-21 18:17:45 +00002215void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002216 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002217 EMIT_ARG(binary_op, RT_BINARY_OP_POWER);
Damien429d7192013-10-04 19:53:11 +01002218}
2219
Damiend99b0522013-12-21 18:17:45 +00002220void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002221 // a list of strings
Damien63321742013-12-10 17:41:49 +00002222
2223 // check type of list (string or bytes) and count total number of bytes
Damiend99b0522013-12-21 18:17:45 +00002224 int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damien63321742013-12-10 17:41:49 +00002225 int n_bytes = 0;
Damiend99b0522013-12-21 18:17:45 +00002226 int string_kind = MP_PARSE_NODE_NULL;
Damien429d7192013-10-04 19:53:11 +01002227 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002228 assert(MP_PARSE_NODE_IS_LEAF(pns->nodes[i]));
2229 int pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
2230 assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
Damien63321742013-12-10 17:41:49 +00002231 if (i == 0) {
2232 string_kind = pn_kind;
2233 } else if (pn_kind != string_kind) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002234 compile_syntax_error(comp, "cannot mix bytes and nonbytes literals");
Damien63321742013-12-10 17:41:49 +00002235 return;
2236 }
Damien George55baff42014-01-21 21:40:13 +00002237 n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
Damien429d7192013-10-04 19:53:11 +01002238 }
Damien63321742013-12-10 17:41:49 +00002239
Damien63321742013-12-10 17:41:49 +00002240 // concatenate string/bytes
Damien George55baff42014-01-21 21:40:13 +00002241 byte *q_ptr;
2242 byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
Damien63321742013-12-10 17:41:49 +00002243 for (int i = 0; i < n; i++) {
Damien George55baff42014-01-21 21:40:13 +00002244 uint s_len;
2245 const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
Damien Georgefe8fb912014-01-02 16:36:09 +00002246 memcpy(s_dest, s, s_len);
2247 s_dest += s_len;
Damien63321742013-12-10 17:41:49 +00002248 }
Damien George55baff42014-01-21 21:40:13 +00002249 qstr q = qstr_build_end(q_ptr);
Damien63321742013-12-10 17:41:49 +00002250
Damien Georgeb9791222014-01-23 00:34:21 +00002251 EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
Damien429d7192013-10-04 19:53:11 +01002252}
2253
2254// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
Damiend99b0522013-12-21 18:17:45 +00002255void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2256 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2257 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2258 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002259
2260 if (comp->pass == PASS_1) {
2261 // create a new scope for this comprehension
Damiend99b0522013-12-21 18:17:45 +00002262 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 +01002263 // store the comprehension scope so the compiling function (this one) can use it at each pass
Damiend99b0522013-12-21 18:17:45 +00002264 pns_comp_for->nodes[3] = (mp_parse_node_t)s;
Damien429d7192013-10-04 19:53:11 +01002265 }
2266
2267 // get the scope for this comprehension
2268 scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
2269
2270 // compile the comprehension
2271 close_over_variables_etc(comp, this_scope, 0, 0);
2272
2273 compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
2274 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002275 EMIT_ARG(call_function, 1, 0, false, false);
Damien429d7192013-10-04 19:53:11 +01002276}
2277
Damiend99b0522013-12-21 18:17:45 +00002278void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
2279 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002280 // an empty tuple
Damiend99b0522013-12-21 18:17:45 +00002281 c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
2282 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2283 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2284 assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
2285 if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
2286 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2287 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002288 // tuple of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002289 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002290 c_tuple(comp, pns->nodes[0], NULL);
Damiend99b0522013-12-21 18:17:45 +00002291 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002292 // tuple of many items
Damien429d7192013-10-04 19:53:11 +01002293 c_tuple(comp, pns->nodes[0], pns2);
Damiend99b0522013-12-21 18:17:45 +00002294 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002295 // generator expression
2296 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2297 } else {
2298 // tuple with 2 items
2299 goto tuple_with_2_items;
2300 }
2301 } else {
2302 // tuple with 2 items
2303 tuple_with_2_items:
Damiend99b0522013-12-21 18:17:45 +00002304 c_tuple(comp, MP_PARSE_NODE_NULL, pns);
Damien429d7192013-10-04 19:53:11 +01002305 }
2306 } else {
2307 // parenthesis around a single item, is just that item
2308 compile_node(comp, pns->nodes[0]);
2309 }
2310}
2311
Damiend99b0522013-12-21 18:17:45 +00002312void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
2313 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002314 // empty list
Damien Georgeb9791222014-01-23 00:34:21 +00002315 EMIT_ARG(build_list, 0);
Damiend99b0522013-12-21 18:17:45 +00002316 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
2317 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
2318 if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
2319 mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
2320 if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
Damien429d7192013-10-04 19:53:11 +01002321 // list of one item, with trailing comma
Damiend99b0522013-12-21 18:17:45 +00002322 assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002323 compile_node(comp, pns2->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002324 EMIT_ARG(build_list, 1);
Damiend99b0522013-12-21 18:17:45 +00002325 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
Damien429d7192013-10-04 19:53:11 +01002326 // list of many items
2327 compile_node(comp, pns2->nodes[0]);
2328 compile_generic_all_nodes(comp, pns3);
Damien Georgeb9791222014-01-23 00:34:21 +00002329 EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
Damiend99b0522013-12-21 18:17:45 +00002330 } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002331 // list comprehension
2332 compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
2333 } else {
2334 // list with 2 items
2335 goto list_with_2_items;
2336 }
2337 } else {
2338 // list with 2 items
2339 list_with_2_items:
2340 compile_node(comp, pns2->nodes[0]);
2341 compile_node(comp, pns2->nodes[1]);
Damien Georgeb9791222014-01-23 00:34:21 +00002342 EMIT_ARG(build_list, 2);
Damien429d7192013-10-04 19:53:11 +01002343 }
2344 } else {
2345 // list with 1 item
2346 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002347 EMIT_ARG(build_list, 1);
Damien429d7192013-10-04 19:53:11 +01002348 }
2349}
2350
Damiend99b0522013-12-21 18:17:45 +00002351void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
2352 mp_parse_node_t pn = pns->nodes[0];
2353 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002354 // empty dict
Damien Georgeb9791222014-01-23 00:34:21 +00002355 EMIT_ARG(build_map, 0);
Damiend99b0522013-12-21 18:17:45 +00002356 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2357 pns = (mp_parse_node_struct_t*)pn;
2358 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
Damien429d7192013-10-04 19:53:11 +01002359 // dict with one element
Damien Georgeb9791222014-01-23 00:34:21 +00002360 EMIT_ARG(build_map, 1);
Damien429d7192013-10-04 19:53:11 +01002361 compile_node(comp, pn);
2362 EMIT(store_map);
Damiend99b0522013-12-21 18:17:45 +00002363 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
2364 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
2365 mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
2366 if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
Damien429d7192013-10-04 19:53:11 +01002367 // dict/set with multiple elements
2368
2369 // get tail elements (2nd, 3rd, ...)
Damiend99b0522013-12-21 18:17:45 +00002370 mp_parse_node_t *nodes;
Damien429d7192013-10-04 19:53:11 +01002371 int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
2372
2373 // first element sets whether it's a dict or set
2374 bool is_dict;
Damiend99b0522013-12-21 18:17:45 +00002375 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002376 // a dictionary
Damien Georgeb9791222014-01-23 00:34:21 +00002377 EMIT_ARG(build_map, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002378 compile_node(comp, pns->nodes[0]);
2379 EMIT(store_map);
2380 is_dict = true;
2381 } else {
2382 // a set
2383 compile_node(comp, pns->nodes[0]); // 1st value of set
2384 is_dict = false;
2385 }
2386
2387 // process rest of elements
2388 for (int i = 0; i < n; i++) {
Damiend99b0522013-12-21 18:17:45 +00002389 mp_parse_node_t pn = nodes[i];
2390 bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
Damien429d7192013-10-04 19:53:11 +01002391 compile_node(comp, pn);
2392 if (is_dict) {
2393 if (!is_key_value) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002394 compile_syntax_error(comp, "?expecting key:value for dictiona");
Damien429d7192013-10-04 19:53:11 +01002395 return;
2396 }
2397 EMIT(store_map);
2398 } else {
2399 if (is_key_value) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002400 compile_syntax_error(comp, "?expecting just a value for s");
Damien429d7192013-10-04 19:53:11 +01002401 return;
2402 }
2403 }
2404 }
2405
2406 // if it's a set, build it
2407 if (!is_dict) {
Damien Georgeb9791222014-01-23 00:34:21 +00002408 EMIT_ARG(build_set, 1 + n);
Damien429d7192013-10-04 19:53:11 +01002409 }
Damiend99b0522013-12-21 18:17:45 +00002410 } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002411 // dict/set comprehension
Damiend99b0522013-12-21 18:17:45 +00002412 if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
Damien429d7192013-10-04 19:53:11 +01002413 // a dictionary comprehension
2414 compile_comprehension(comp, pns, SCOPE_DICT_COMP);
2415 } else {
2416 // a set comprehension
2417 compile_comprehension(comp, pns, SCOPE_SET_COMP);
2418 }
2419 } else {
2420 // shouldn't happen
2421 assert(0);
2422 }
2423 } else {
2424 // set with one element
2425 goto set_with_one_element;
2426 }
2427 } else {
2428 // set with one element
2429 set_with_one_element:
2430 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002431 EMIT_ARG(build_set, 1);
Damien429d7192013-10-04 19:53:11 +01002432 }
2433}
2434
Damiend99b0522013-12-21 18:17:45 +00002435void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgebbcd49a2014-02-06 20:30:16 +00002436 compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
Damien429d7192013-10-04 19:53:11 +01002437}
2438
Damiend99b0522013-12-21 18:17:45 +00002439void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002440 // object who's index we want is on top of stack
2441 compile_node(comp, pns->nodes[0]); // the index
Damien Georgeb9791222014-01-23 00:34:21 +00002442 EMIT_ARG(binary_op, RT_BINARY_OP_SUBSCR);
Damien429d7192013-10-04 19:53:11 +01002443}
2444
Damiend99b0522013-12-21 18:17:45 +00002445void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002446 // object who's attribute we want is on top of stack
Damien Georgeb9791222014-01-23 00:34:21 +00002447 EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
Damien429d7192013-10-04 19:53:11 +01002448}
2449
Damiend99b0522013-12-21 18:17:45 +00002450void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
2451 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
2452 mp_parse_node_t pn = pns->nodes[0];
2453 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002454 // [?:]
Damien Georgeb9791222014-01-23 00:34:21 +00002455 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
2456 EMIT_ARG(build_slice, 2);
Damiend99b0522013-12-21 18:17:45 +00002457 } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
2458 pns = (mp_parse_node_struct_t*)pn;
2459 if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
Damien Georgeb9791222014-01-23 00:34:21 +00002460 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002461 pn = pns->nodes[0];
Damiend99b0522013-12-21 18:17:45 +00002462 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002463 // [?::]
Damien Georgeb9791222014-01-23 00:34:21 +00002464 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002465 } else {
2466 // [?::x]
2467 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002468 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002469 }
Damiend99b0522013-12-21 18:17:45 +00002470 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
Damien429d7192013-10-04 19:53:11 +01002471 compile_node(comp, pns->nodes[0]);
Damiend99b0522013-12-21 18:17:45 +00002472 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2473 pns = (mp_parse_node_struct_t*)pns->nodes[1];
2474 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
2475 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien429d7192013-10-04 19:53:11 +01002476 // [?:x:]
Damien Georgeb9791222014-01-23 00:34:21 +00002477 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002478 } else {
2479 // [?:x:x]
2480 compile_node(comp, pns->nodes[0]);
Damien Georgeb9791222014-01-23 00:34:21 +00002481 EMIT_ARG(build_slice, 3);
Damien429d7192013-10-04 19:53:11 +01002482 }
2483 } else {
2484 // [?:x]
2485 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002486 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002487 }
2488 } else {
2489 // [?:x]
2490 compile_node(comp, pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002491 EMIT_ARG(build_slice, 2);
Damien429d7192013-10-04 19:53:11 +01002492 }
2493}
2494
Damiend99b0522013-12-21 18:17:45 +00002495void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002496 compile_node(comp, pns->nodes[0]); // start of slice
Damiend99b0522013-12-21 18:17:45 +00002497 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2498 compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
Damien429d7192013-10-04 19:53:11 +01002499}
2500
Damiend99b0522013-12-21 18:17:45 +00002501void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien Georgeb9791222014-01-23 00:34:21 +00002502 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002503 compile_subscript_3_helper(comp, pns);
2504}
2505
Damiend99b0522013-12-21 18:17:45 +00002506void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002507 // if this is called then we are compiling a dict key:value pair
2508 compile_node(comp, pns->nodes[1]); // value
2509 compile_node(comp, pns->nodes[0]); // key
2510}
2511
Damiend99b0522013-12-21 18:17:45 +00002512void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien6cdd3af2013-10-05 18:08:26 +01002513 qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
Damien429d7192013-10-04 19:53:11 +01002514 // store class object into class name
Damien Georgeb9791222014-01-23 00:34:21 +00002515 EMIT_ARG(store_id, cname);
Damien429d7192013-10-04 19:53:11 +01002516}
2517
Damiend99b0522013-12-21 18:17:45 +00002518void compile_arglist_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002519 if (comp->have_star_arg) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002520 compile_syntax_error(comp, "?can't have multiple *x");
Damien429d7192013-10-04 19:53:11 +01002521 return;
2522 }
2523 comp->have_star_arg = true;
2524 compile_node(comp, pns->nodes[0]);
2525}
2526
Damiend99b0522013-12-21 18:17:45 +00002527void compile_arglist_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002528 if (comp->have_dbl_star_arg) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002529 compile_syntax_error(comp, "?can't have multiple **x");
Damien429d7192013-10-04 19:53:11 +01002530 return;
2531 }
2532 comp->have_dbl_star_arg = true;
2533 compile_node(comp, pns->nodes[0]);
2534}
2535
Damiend99b0522013-12-21 18:17:45 +00002536void compile_argument(compiler_t *comp, mp_parse_node_struct_t *pns) {
2537 assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
2538 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
2539 if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
2540 if (!MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002541 compile_syntax_error(comp, "?lhs of keyword argument must be an id");
Damien429d7192013-10-04 19:53:11 +01002542 return;
2543 }
Damien Georgeb9791222014-01-23 00:34:21 +00002544 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
Damien429d7192013-10-04 19:53:11 +01002545 compile_node(comp, pns2->nodes[0]);
2546 comp->n_arg_keyword += 1;
Damiend99b0522013-12-21 18:17:45 +00002547 } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
Damien429d7192013-10-04 19:53:11 +01002548 compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
2549 } else {
2550 // shouldn't happen
2551 assert(0);
2552 }
2553}
2554
Damiend99b0522013-12-21 18:17:45 +00002555void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
Damien429d7192013-10-04 19:53:11 +01002556 if (comp->scope_cur->kind != SCOPE_FUNCTION) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002557 compile_syntax_error(comp, "'yield' outside function");
Damien429d7192013-10-04 19:53:11 +01002558 return;
2559 }
Damiend99b0522013-12-21 18:17:45 +00002560 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damien Georgeb9791222014-01-23 00:34:21 +00002561 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002562 EMIT(yield_value);
Damiend99b0522013-12-21 18:17:45 +00002563 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
2564 pns = (mp_parse_node_struct_t*)pns->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002565 compile_node(comp, pns->nodes[0]);
2566 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002567 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002568 EMIT(yield_from);
2569 } else {
2570 compile_node(comp, pns->nodes[0]);
2571 EMIT(yield_value);
2572 }
2573}
2574
Damiend99b0522013-12-21 18:17:45 +00002575typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002576STATIC compile_function_t compile_function[] = {
Damien429d7192013-10-04 19:53:11 +01002577 NULL,
2578#define nc NULL
2579#define c(f) compile_##f
Damien George1dc76af2014-02-26 16:57:08 +00002580#define DEF_RULE(rule, comp, kind, ...) comp,
Damien429d7192013-10-04 19:53:11 +01002581#include "grammar.h"
2582#undef nc
2583#undef c
2584#undef DEF_RULE
2585};
2586
Damiend99b0522013-12-21 18:17:45 +00002587void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2588 if (MP_PARSE_NODE_IS_NULL(pn)) {
Damien429d7192013-10-04 19:53:11 +01002589 // pass
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002590 } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
2591 machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
2592 EMIT_ARG(load_const_small_int, arg);
Damiend99b0522013-12-21 18:17:45 +00002593 } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
Paul Sokolovsky56e5ef22014-02-22 16:39:45 +02002594 machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
Damiend99b0522013-12-21 18:17:45 +00002595 switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002596 case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
Damien Georgeb9791222014-01-23 00:34:21 +00002597 case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
2598 case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
2599 case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
2600 case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
Damiend99b0522013-12-21 18:17:45 +00002601 case MP_PARSE_NODE_TOKEN:
2602 if (arg == MP_TOKEN_NEWLINE) {
Damien91d387d2013-10-09 15:09:52 +01002603 // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
Damien5ac1b2e2013-10-18 19:58:12 +01002604 // or when single_input lets through a NEWLINE (user enters a blank line)
Damien91d387d2013-10-09 15:09:52 +01002605 // do nothing
2606 } else {
Damien Georgeb9791222014-01-23 00:34:21 +00002607 EMIT_ARG(load_const_tok, arg);
Damien91d387d2013-10-09 15:09:52 +01002608 }
2609 break;
Damien429d7192013-10-04 19:53:11 +01002610 default: assert(0);
2611 }
2612 } else {
Damiend99b0522013-12-21 18:17:45 +00002613 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
Damien Georgeb9791222014-01-23 00:34:21 +00002614 EMIT_ARG(set_line_number, pns->source_line);
Damiend99b0522013-12-21 18:17:45 +00002615 compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
Damien429d7192013-10-04 19:53:11 +01002616 if (f == NULL) {
Damiend99b0522013-12-21 18:17:45 +00002617 printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
Damien Georgecbd2f742014-01-19 11:48:48 +00002618#if MICROPY_DEBUG_PRINTERS
2619 mp_parse_node_print(pn, 0);
2620#endif
Damien429d7192013-10-04 19:53:11 +01002621 assert(0);
2622 } else {
2623 f(comp, pns);
2624 }
2625 }
2626}
2627
Damiend99b0522013-12-21 18:17:45 +00002628void 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 +01002629 // TODO verify that *k and **k are last etc
Damien429d7192013-10-04 19:53:11 +01002630 qstr param_name = 0;
Damiend99b0522013-12-21 18:17:45 +00002631 mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
2632 if (MP_PARSE_NODE_IS_ID(pn)) {
2633 param_name = MP_PARSE_NODE_LEAF_ARG(pn);
Damien429d7192013-10-04 19:53:11 +01002634 if (comp->have_bare_star) {
2635 // comes after a bare star, so doesn't count as a parameter
2636 } else {
2637 comp->scope_cur->num_params += 1;
2638 }
Damienb14de212013-10-06 00:28:28 +01002639 } else {
Damiend99b0522013-12-21 18:17:45 +00002640 assert(MP_PARSE_NODE_IS_STRUCT(pn));
2641 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2642 if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
2643 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002644 //int node_index = 1; unused
2645 if (allow_annotations) {
Damiend99b0522013-12-21 18:17:45 +00002646 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002647 // this parameter has an annotation
2648 pn_annotation = pns->nodes[1];
2649 }
2650 //node_index = 2; unused
2651 }
2652 /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
Damiend99b0522013-12-21 18:17:45 +00002653 if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
Damienb14de212013-10-06 00:28:28 +01002654 // this parameter has a default value
2655 if (comp->have_bare_star) {
2656 comp->scope_cur->num_dict_params += 1;
2657 } else {
2658 comp->scope_cur->num_default_params += 1;
2659 }
2660 }
2661 */
2662 if (comp->have_bare_star) {
2663 // comes after a bare star, so doesn't count as a parameter
2664 } else {
2665 comp->scope_cur->num_params += 1;
2666 }
Damiend99b0522013-12-21 18:17:45 +00002667 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
2668 if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002669 // bare star
2670 // TODO see http://www.python.org/dev/peps/pep-3102/
2671 comp->have_bare_star = true;
2672 //assert(comp->scope_cur->num_dict_params == 0);
Damiend99b0522013-12-21 18:17:45 +00002673 } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
Damienb14de212013-10-06 00:28:28 +01002674 // named star
Damien George8725f8f2014-02-15 19:33:11 +00002675 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002676 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2677 } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
Damienb14de212013-10-06 00:28:28 +01002678 // named star with annotation
Damien George8725f8f2014-02-15 19:33:11 +00002679 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
Damiend99b0522013-12-21 18:17:45 +00002680 pns = (mp_parse_node_struct_t*)pns->nodes[0];
2681 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
Damienb14de212013-10-06 00:28:28 +01002682 pn_annotation = pns->nodes[1];
2683 } else {
2684 // shouldn't happen
2685 assert(0);
2686 }
Damiend99b0522013-12-21 18:17:45 +00002687 } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
2688 param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2689 if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
Damienb14de212013-10-06 00:28:28 +01002690 // this parameter has an annotation
2691 pn_annotation = pns->nodes[1];
2692 }
Damien George8725f8f2014-02-15 19:33:11 +00002693 comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
Damien429d7192013-10-04 19:53:11 +01002694 } else {
Damienb14de212013-10-06 00:28:28 +01002695 // TODO anything to implement?
Damien429d7192013-10-04 19:53:11 +01002696 assert(0);
2697 }
Damien429d7192013-10-04 19:53:11 +01002698 }
2699
2700 if (param_name != 0) {
Damiend99b0522013-12-21 18:17:45 +00002701 if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
Damien429d7192013-10-04 19:53:11 +01002702 // TODO this parameter has an annotation
2703 }
2704 bool added;
2705 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
2706 if (!added) {
Damien Georgef41fdd02014-03-03 23:19:11 +00002707 compile_syntax_error(comp, "?same name used for parameter");
Damien429d7192013-10-04 19:53:11 +01002708 return;
2709 }
2710 id_info->param = true;
2711 id_info->kind = ID_INFO_KIND_LOCAL;
2712 }
2713}
2714
Damiend99b0522013-12-21 18:17:45 +00002715void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002716 compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
2717}
2718
Damiend99b0522013-12-21 18:17:45 +00002719void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002720 compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
2721}
2722
Damiend99b0522013-12-21 18:17:45 +00002723void 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 +01002724 tail_recursion:
Damiend99b0522013-12-21 18:17:45 +00002725 if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
Damien429d7192013-10-04 19:53:11 +01002726 // no more nested if/for; compile inner expression
2727 compile_node(comp, pn_inner_expr);
2728 if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002729 EMIT_ARG(list_append, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002730 } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002731 EMIT_ARG(map_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002732 } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002733 EMIT_ARG(set_add, for_depth + 2);
Damien429d7192013-10-04 19:53:11 +01002734 } else {
2735 EMIT(yield_value);
2736 EMIT(pop_top);
2737 }
Damiend99b0522013-12-21 18:17:45 +00002738 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
Damien429d7192013-10-04 19:53:11 +01002739 // if condition
Damiend99b0522013-12-21 18:17:45 +00002740 mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002741 c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
2742 pn_iter = pns_comp_if->nodes[1];
2743 goto tail_recursion;
Damiend99b0522013-12-21 18:17:45 +00002744 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
Damien429d7192013-10-04 19:53:11 +01002745 // for loop
Damiend99b0522013-12-21 18:17:45 +00002746 mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
Damien429d7192013-10-04 19:53:11 +01002747 compile_node(comp, pns_comp_for2->nodes[1]);
Damienb05d7072013-10-05 13:37:10 +01002748 int l_end2 = comp_next_label(comp);
2749 int l_top2 = comp_next_label(comp);
Damien429d7192013-10-04 19:53:11 +01002750 EMIT(get_iter);
Damien Georgeb9791222014-01-23 00:34:21 +00002751 EMIT_ARG(label_assign, l_top2);
2752 EMIT_ARG(for_iter, l_end2);
Damien429d7192013-10-04 19:53:11 +01002753 c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
2754 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 +00002755 EMIT_ARG(jump, l_top2);
2756 EMIT_ARG(label_assign, l_end2);
Damien429d7192013-10-04 19:53:11 +01002757 EMIT(for_iter_end);
2758 } else {
2759 // shouldn't happen
2760 assert(0);
2761 }
2762}
2763
Damiend99b0522013-12-21 18:17:45 +00002764void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
Damien429d7192013-10-04 19:53:11 +01002765 // see http://www.python.org/dev/peps/pep-0257/
2766
2767 // look for the first statement
Damiend99b0522013-12-21 18:17:45 +00002768 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
Damiene388f102013-12-12 15:24:38 +00002769 // a statement; fall through
Damiend99b0522013-12-21 18:17:45 +00002770 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
Damiene388f102013-12-12 15:24:38 +00002771 // file input; find the first non-newline node
Damiend99b0522013-12-21 18:17:45 +00002772 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2773 int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
Damiene388f102013-12-12 15:24:38 +00002774 for (int i = 0; i < num_nodes; i++) {
2775 pn = pns->nodes[i];
Damiend99b0522013-12-21 18:17:45 +00002776 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 +00002777 // not a newline, so this is the first statement; finish search
2778 break;
2779 }
2780 }
2781 // 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 +00002782 } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
Damiene388f102013-12-12 15:24:38 +00002783 // a list of statements; get the first one
Damiend99b0522013-12-21 18:17:45 +00002784 pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
Damien429d7192013-10-04 19:53:11 +01002785 } else {
2786 return;
2787 }
2788
2789 // check the first statement for a doc string
Damiend99b0522013-12-21 18:17:45 +00002790 if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
2791 mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
2792 if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
2793 int kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]);
2794 if (kind == MP_PARSE_NODE_STRING) {
Damien429d7192013-10-04 19:53:11 +01002795 compile_node(comp, pns->nodes[0]); // a doc string
2796 // store doc string
Damien Georgeb9791222014-01-23 00:34:21 +00002797 EMIT_ARG(store_id, MP_QSTR___doc__);
Damien429d7192013-10-04 19:53:11 +01002798 }
2799 }
2800 }
2801}
2802
2803void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2804 comp->pass = pass;
2805 comp->scope_cur = scope;
Damienb05d7072013-10-05 13:37:10 +01002806 comp->next_label = 1;
Damien Georgeb9791222014-01-23 00:34:21 +00002807 EMIT_ARG(start_pass, pass, scope);
Damien429d7192013-10-04 19:53:11 +01002808
2809 if (comp->pass == PASS_1) {
Damien George8dcc0c72014-03-27 10:55:21 +00002810 // reset maximum stack sizes in scope
2811 // they will be computed in this first pass
Damien429d7192013-10-04 19:53:11 +01002812 scope->stack_size = 0;
Damien George8dcc0c72014-03-27 10:55:21 +00002813 scope->exc_stack_size = 0;
Damien429d7192013-10-04 19:53:11 +01002814 }
2815
Damien5ac1b2e2013-10-18 19:58:12 +01002816#if MICROPY_EMIT_CPYTHON
Damien429d7192013-10-04 19:53:11 +01002817 if (comp->pass == PASS_3) {
Damien429d7192013-10-04 19:53:11 +01002818 scope_print_info(scope);
2819 }
Damien5ac1b2e2013-10-18 19:58:12 +01002820#endif
Damien429d7192013-10-04 19:53:11 +01002821
2822 // compile
Damien Georged02c6d82014-01-15 22:14:03 +00002823 if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
2824 assert(scope->kind == SCOPE_MODULE);
2825 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2826 compile_node(comp, pns->nodes[0]); // compile the expression
2827 EMIT(return_value);
2828 } else if (scope->kind == SCOPE_MODULE) {
Damien5ac1b2e2013-10-18 19:58:12 +01002829 if (!comp->is_repl) {
2830 check_for_doc_string(comp, scope->pn);
2831 }
Damien429d7192013-10-04 19:53:11 +01002832 compile_node(comp, scope->pn);
Damien Georgeb9791222014-01-23 00:34:21 +00002833 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002834 EMIT(return_value);
2835 } else if (scope->kind == SCOPE_FUNCTION) {
Damiend99b0522013-12-21 18:17:45 +00002836 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2837 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2838 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien429d7192013-10-04 19:53:11 +01002839
2840 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002841 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002842 if (comp->pass == PASS_1) {
2843 comp->have_bare_star = false;
2844 apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
2845 }
2846
Paul Sokolovsky2f0b0262014-02-10 02:04:26 +02002847 // pns->nodes[2] is return/whole function annotation
Damien429d7192013-10-04 19:53:11 +01002848
2849 compile_node(comp, pns->nodes[3]); // 3 is function body
2850 // emit return if it wasn't the last opcode
Damien415eb6f2013-10-05 12:19:06 +01002851 if (!EMIT(last_emit_was_return_value)) {
Damien Georgeb9791222014-01-23 00:34:21 +00002852 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002853 EMIT(return_value);
2854 }
2855 } else if (scope->kind == SCOPE_LAMBDA) {
Damiend99b0522013-12-21 18:17:45 +00002856 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2857 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2858 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
Damien429d7192013-10-04 19:53:11 +01002859
2860 // work out number of parameters, keywords and default parameters, and add them to the id_info array
Damien6cdd3af2013-10-05 18:08:26 +01002861 // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
Damien429d7192013-10-04 19:53:11 +01002862 if (comp->pass == PASS_1) {
2863 comp->have_bare_star = false;
2864 apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
2865 }
2866
2867 compile_node(comp, pns->nodes[1]); // 1 is lambda body
2868 EMIT(return_value);
2869 } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
2870 // a bit of a hack at the moment
2871
Damiend99b0522013-12-21 18:17:45 +00002872 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2873 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2874 assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
2875 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
2876 mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
Damien429d7192013-10-04 19:53:11 +01002877
Damien George55baff42014-01-21 21:40:13 +00002878 qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
Damien429d7192013-10-04 19:53:11 +01002879 if (comp->pass == PASS_1) {
2880 bool added;
2881 id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
2882 assert(added);
2883 id_info->kind = ID_INFO_KIND_LOCAL;
2884 scope->num_params = 1;
2885 }
2886
2887 if (scope->kind == SCOPE_LIST_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002888 EMIT_ARG(build_list, 0);
Damien429d7192013-10-04 19:53:11 +01002889 } else if (scope->kind == SCOPE_DICT_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002890 EMIT_ARG(build_map, 0);
Damien429d7192013-10-04 19:53:11 +01002891 } else if (scope->kind == SCOPE_SET_COMP) {
Damien Georgeb9791222014-01-23 00:34:21 +00002892 EMIT_ARG(build_set, 0);
Damien429d7192013-10-04 19:53:11 +01002893 }
2894
Damienb05d7072013-10-05 13:37:10 +01002895 int l_end = comp_next_label(comp);
2896 int l_top = comp_next_label(comp);
Damien Georgeb9791222014-01-23 00:34:21 +00002897 EMIT_ARG(load_id, qstr_arg);
2898 EMIT_ARG(label_assign, l_top);
2899 EMIT_ARG(for_iter, l_end);
Damien429d7192013-10-04 19:53:11 +01002900 c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
2901 compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
Damien Georgeb9791222014-01-23 00:34:21 +00002902 EMIT_ARG(jump, l_top);
2903 EMIT_ARG(label_assign, l_end);
Damien429d7192013-10-04 19:53:11 +01002904 EMIT(for_iter_end);
2905
2906 if (scope->kind == SCOPE_GEN_EXPR) {
Damien Georgeb9791222014-01-23 00:34:21 +00002907 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002908 }
2909 EMIT(return_value);
2910 } else {
2911 assert(scope->kind == SCOPE_CLASS);
Damiend99b0522013-12-21 18:17:45 +00002912 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2913 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2914 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
Damien429d7192013-10-04 19:53:11 +01002915
2916 if (comp->pass == PASS_1) {
2917 bool added;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002918 id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
Damien429d7192013-10-04 19:53:11 +01002919 assert(added);
2920 id_info->kind = ID_INFO_KIND_LOCAL;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002921 id_info = scope_find_or_add_id(scope, MP_QSTR___locals__, &added);
Damien429d7192013-10-04 19:53:11 +01002922 assert(added);
2923 id_info->kind = ID_INFO_KIND_LOCAL;
2924 id_info->param = true;
2925 scope->num_params = 1; // __locals__ is the parameter
2926 }
2927
Damien Georgeb9791222014-01-23 00:34:21 +00002928 EMIT_ARG(load_id, MP_QSTR___locals__);
Damien429d7192013-10-04 19:53:11 +01002929 EMIT(store_locals);
Damien Georgeb9791222014-01-23 00:34:21 +00002930 EMIT_ARG(load_id, MP_QSTR___name__);
2931 EMIT_ARG(store_id, MP_QSTR___module__);
2932 EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
2933 EMIT_ARG(store_id, MP_QSTR___qualname__);
Damien429d7192013-10-04 19:53:11 +01002934
2935 check_for_doc_string(comp, pns->nodes[2]);
2936 compile_node(comp, pns->nodes[2]); // 2 is class body
2937
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00002938 id_info_t *id = scope_find(scope, MP_QSTR___class__);
Damien429d7192013-10-04 19:53:11 +01002939 assert(id != NULL);
2940 if (id->kind == ID_INFO_KIND_LOCAL) {
Damien Georgeb9791222014-01-23 00:34:21 +00002941 EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
Damien429d7192013-10-04 19:53:11 +01002942 } else {
Damien George6baf76e2013-12-30 22:32:17 +00002943#if MICROPY_EMIT_CPYTHON
Damien Georgeb9791222014-01-23 00:34:21 +00002944 EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
Damien George6baf76e2013-12-30 22:32:17 +00002945#else
Damien George35e2a4e2014-02-05 00:51:47 +00002946 EMIT_ARG(load_fast, MP_QSTR___class__, id->local_num);
Damien George6baf76e2013-12-30 22:32:17 +00002947#endif
Damien429d7192013-10-04 19:53:11 +01002948 }
2949 EMIT(return_value);
2950 }
2951
Damien415eb6f2013-10-05 12:19:06 +01002952 EMIT(end_pass);
Damien George8dcc0c72014-03-27 10:55:21 +00002953
2954 // make sure we match all the exception levels
2955 assert(comp->cur_except_level == 0);
Damien826005c2013-10-05 23:17:28 +01002956}
2957
2958void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
2959 comp->pass = pass;
2960 comp->scope_cur = scope;
2961 comp->next_label = 1;
2962
2963 if (scope->kind != SCOPE_FUNCTION) {
2964 printf("Error: inline assembler must be a function\n");
2965 return;
2966 }
2967
Damiena2f2f7d2013-10-06 00:14:13 +01002968 if (comp->pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00002969 EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
Damiena2f2f7d2013-10-06 00:14:13 +01002970 }
2971
Damien826005c2013-10-05 23:17:28 +01002972 // get the function definition parse node
Damiend99b0522013-12-21 18:17:45 +00002973 assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
2974 mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
2975 assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
Damien826005c2013-10-05 23:17:28 +01002976
Damiend99b0522013-12-21 18:17:45 +00002977 //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
Damien826005c2013-10-05 23:17:28 +01002978
Damiena2f2f7d2013-10-06 00:14:13 +01002979 // parameters are in pns->nodes[1]
2980 if (comp->pass == PASS_2) {
Damiend99b0522013-12-21 18:17:45 +00002981 mp_parse_node_t *pn_params;
Damiena2f2f7d2013-10-06 00:14:13 +01002982 int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
Damien Georgeb9791222014-01-23 00:34:21 +00002983 scope->num_params = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
Damiena2f2f7d2013-10-06 00:14:13 +01002984 }
2985
Damiend99b0522013-12-21 18:17:45 +00002986 assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
Damien826005c2013-10-05 23:17:28 +01002987
Damiend99b0522013-12-21 18:17:45 +00002988 mp_parse_node_t pn_body = pns->nodes[3]; // body
2989 mp_parse_node_t *nodes;
Damien826005c2013-10-05 23:17:28 +01002990 int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
2991
Damien Georgecbd2f742014-01-19 11:48:48 +00002992 /*
Damien826005c2013-10-05 23:17:28 +01002993 if (comp->pass == PASS_3) {
2994 //printf("----\n");
2995 scope_print_info(scope);
2996 }
Damien Georgecbd2f742014-01-19 11:48:48 +00002997 */
Damien826005c2013-10-05 23:17:28 +01002998
2999 for (int i = 0; i < num; i++) {
Damiend99b0522013-12-21 18:17:45 +00003000 assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
3001 mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
3002 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt);
3003 assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
3004 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
3005 pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
3006 assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
3007 assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
3008 assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
3009 assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
3010 qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
3011 pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
3012 mp_parse_node_t *pn_arg;
Damien826005c2013-10-05 23:17:28 +01003013 int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
3014
3015 // emit instructions
3016 if (strcmp(qstr_str(op), "label") == 0) {
Damiend99b0522013-12-21 18:17:45 +00003017 if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
Damien Georgef41fdd02014-03-03 23:19:11 +00003018 compile_syntax_error(comp, "inline assembler 'label' requires 1 argument");
Damien826005c2013-10-05 23:17:28 +01003019 return;
3020 }
3021 int lab = comp_next_label(comp);
3022 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003023 EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
Damien826005c2013-10-05 23:17:28 +01003024 }
3025 } else {
3026 if (pass > PASS_1) {
Damien Georgeb9791222014-01-23 00:34:21 +00003027 EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
Damien826005c2013-10-05 23:17:28 +01003028 }
3029 }
3030 }
3031
3032 if (comp->pass > PASS_1) {
3033 EMIT_INLINE_ASM(end_pass);
Damienb05d7072013-10-05 13:37:10 +01003034 }
Damien429d7192013-10-04 19:53:11 +01003035}
3036
3037void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
3038 // in functions, turn implicit globals into explicit globals
Damien George6baf76e2013-12-30 22:32:17 +00003039 // compute the index of each local
Damien429d7192013-10-04 19:53:11 +01003040 scope->num_locals = 0;
3041 for (int i = 0; i < scope->id_info_len; i++) {
3042 id_info_t *id = &scope->id_info[i];
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003043 if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
Damien429d7192013-10-04 19:53:11 +01003044 // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
3045 continue;
3046 }
3047 if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
3048 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
3049 }
Damien9ecbcff2013-12-11 00:41:43 +00003050 // note: params always count for 1 local, even if they are a cell
Damien429d7192013-10-04 19:53:11 +01003051 if (id->param || id->kind == ID_INFO_KIND_LOCAL) {
3052 id->local_num = scope->num_locals;
3053 scope->num_locals += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003054 }
3055 }
3056
3057 // compute the index of cell vars (freevars[idx] in CPython)
Damien George6baf76e2013-12-30 22:32:17 +00003058#if MICROPY_EMIT_CPYTHON
3059 int num_cell = 0;
3060#endif
Damien9ecbcff2013-12-11 00:41:43 +00003061 for (int i = 0; i < scope->id_info_len; i++) {
3062 id_info_t *id = &scope->id_info[i];
Damien George6baf76e2013-12-30 22:32:17 +00003063#if MICROPY_EMIT_CPYTHON
3064 // in CPython the cells are numbered starting from 0
Damien9ecbcff2013-12-11 00:41:43 +00003065 if (id->kind == ID_INFO_KIND_CELL) {
Damien George6baf76e2013-12-30 22:32:17 +00003066 id->local_num = num_cell;
3067 num_cell += 1;
Damien9ecbcff2013-12-11 00:41:43 +00003068 }
Damien George6baf76e2013-12-30 22:32:17 +00003069#else
3070 // in Micro Python the cells come right after the fast locals
3071 // parameters are not counted here, since they remain at the start
3072 // of the locals, even if they are cell vars
3073 if (!id->param && id->kind == ID_INFO_KIND_CELL) {
3074 id->local_num = scope->num_locals;
3075 scope->num_locals += 1;
3076 }
3077#endif
Damien9ecbcff2013-12-11 00:41:43 +00003078 }
Damien9ecbcff2013-12-11 00:41:43 +00003079
3080 // compute the index of free vars (freevars[idx] in CPython)
3081 // make sure they are in the order of the parent scope
3082 if (scope->parent != NULL) {
3083 int num_free = 0;
3084 for (int i = 0; i < scope->parent->id_info_len; i++) {
3085 id_info_t *id = &scope->parent->id_info[i];
3086 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3087 for (int j = 0; j < scope->id_info_len; j++) {
3088 id_info_t *id2 = &scope->id_info[j];
3089 if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
Damien George6baf76e2013-12-30 22:32:17 +00003090 assert(!id2->param); // free vars should not be params
3091#if MICROPY_EMIT_CPYTHON
3092 // in CPython the frees are numbered after the cells
3093 id2->local_num = num_cell + num_free;
3094#else
3095 // in Micro Python the frees come first, before the params
3096 id2->local_num = num_free;
Damien9ecbcff2013-12-11 00:41:43 +00003097#endif
3098 num_free += 1;
3099 }
3100 }
3101 }
Damien429d7192013-10-04 19:53:11 +01003102 }
Damien George6baf76e2013-12-30 22:32:17 +00003103#if !MICROPY_EMIT_CPYTHON
3104 // in Micro Python shift all other locals after the free locals
3105 if (num_free > 0) {
3106 for (int i = 0; i < scope->id_info_len; i++) {
3107 id_info_t *id = &scope->id_info[i];
3108 if (id->param || id->kind != ID_INFO_KIND_FREE) {
3109 id->local_num += num_free;
3110 }
3111 }
3112 scope->num_params += num_free; // free vars are counted as params for passing them into the function
3113 scope->num_locals += num_free;
3114 }
3115#endif
Damien429d7192013-10-04 19:53:11 +01003116 }
3117
Damien George8725f8f2014-02-15 19:33:11 +00003118 // compute scope_flags
3119 //scope->scope_flags = 0; since we set some things in parameters
Damien429d7192013-10-04 19:53:11 +01003120 if (scope->kind != SCOPE_MODULE) {
Damien George8725f8f2014-02-15 19:33:11 +00003121 scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
Damien429d7192013-10-04 19:53:11 +01003122 }
3123 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) {
3124 assert(scope->parent != NULL);
Damien George8725f8f2014-02-15 19:33:11 +00003125 scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
Damien429d7192013-10-04 19:53:11 +01003126
3127 // TODO possibly other ways it can be nested
Damien George08d07552014-01-29 18:58:52 +00003128 // Note that we don't actually use this information at the moment (for CPython compat only)
3129 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 +00003130 scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
Damien429d7192013-10-04 19:53:11 +01003131 }
3132 }
3133 int num_free = 0;
3134 for (int i = 0; i < scope->id_info_len; i++) {
3135 id_info_t *id = &scope->id_info[i];
3136 if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
3137 num_free += 1;
3138 }
3139 }
3140 if (num_free == 0) {
Damien George8725f8f2014-02-15 19:33:11 +00003141 scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
Damien429d7192013-10-04 19:53:11 +01003142 }
3143}
3144
Damien George08335002014-01-18 23:24:36 +00003145mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
Damien429d7192013-10-04 19:53:11 +01003146 compiler_t *comp = m_new(compiler_t, 1);
3147
Damien Georgecbd2f742014-01-19 11:48:48 +00003148 comp->source_file = source_file;
Damien5ac1b2e2013-10-18 19:58:12 +01003149 comp->is_repl = is_repl;
3150 comp->had_error = false;
3151
Damien429d7192013-10-04 19:53:11 +01003152 comp->break_label = 0;
3153 comp->continue_label = 0;
Damien Georgecbddb272014-02-01 20:08:18 +00003154 comp->break_continue_except_level = 0;
3155 comp->cur_except_level = 0;
3156
Damien George35e2a4e2014-02-05 00:51:47 +00003157 comp->func_arg_is_super = false;
3158
Damien429d7192013-10-04 19:53:11 +01003159 comp->scope_head = NULL;
3160 comp->scope_cur = NULL;
3161
Damien826005c2013-10-05 23:17:28 +01003162 // optimise constants
Damien429d7192013-10-04 19:53:11 +01003163 pn = fold_constants(pn);
Damien826005c2013-10-05 23:17:28 +01003164
3165 // set the outer scope
Damien Georgeeb7bfcb2014-01-04 15:57:35 +00003166 scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE);
Damien429d7192013-10-04 19:53:11 +01003167
Damien826005c2013-10-05 23:17:28 +01003168 // compile pass 1
Damien George35e2a4e2014-02-05 00:51:47 +00003169 comp->emit = emit_pass1_new();
Damien826005c2013-10-05 23:17:28 +01003170 comp->emit_method_table = &emit_pass1_method_table;
3171 comp->emit_inline_asm = NULL;
3172 comp->emit_inline_asm_method_table = NULL;
3173 uint max_num_labels = 0;
Damien5ac1b2e2013-10-18 19:58:12 +01003174 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003175 if (false) {
Damien3ef4abb2013-10-12 16:53:13 +01003176#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003177 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
Damien826005c2013-10-05 23:17:28 +01003178 compile_scope_inline_asm(comp, s, PASS_1);
Damienc025ebb2013-10-12 14:30:21 +01003179#endif
Damien826005c2013-10-05 23:17:28 +01003180 } else {
3181 compile_scope(comp, s, PASS_1);
3182 }
3183
3184 // update maximim number of labels needed
3185 if (comp->next_label > max_num_labels) {
3186 max_num_labels = comp->next_label;
3187 }
Damien429d7192013-10-04 19:53:11 +01003188 }
3189
Damien826005c2013-10-05 23:17:28 +01003190 // compute some things related to scope and identifiers
Damien5ac1b2e2013-10-18 19:58:12 +01003191 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damien429d7192013-10-04 19:53:11 +01003192 compile_scope_compute_things(comp, s);
3193 }
3194
Damien826005c2013-10-05 23:17:28 +01003195 // finish with pass 1
Damien6cdd3af2013-10-05 18:08:26 +01003196 emit_pass1_free(comp->emit);
3197
Damien826005c2013-10-05 23:17:28 +01003198 // compile pass 2 and 3
Damien3ef4abb2013-10-12 16:53:13 +01003199#if !MICROPY_EMIT_CPYTHON
Damien6cdd3af2013-10-05 18:08:26 +01003200 emit_t *emit_bc = NULL;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003201#if MICROPY_EMIT_NATIVE
Damiendc833822013-10-06 01:01:01 +01003202 emit_t *emit_native = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003203#endif
Damien3ef4abb2013-10-12 16:53:13 +01003204#if MICROPY_EMIT_INLINE_THUMB
Damien826005c2013-10-05 23:17:28 +01003205 emit_inline_asm_t *emit_inline_thumb = NULL;
Damienc025ebb2013-10-12 14:30:21 +01003206#endif
Damien Georgee67ed5d2014-01-04 13:55:24 +00003207#endif // !MICROPY_EMIT_CPYTHON
Damien5ac1b2e2013-10-18 19:58:12 +01003208 for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
Damienc025ebb2013-10-12 14:30:21 +01003209 if (false) {
3210 // dummy
3211
Damien3ef4abb2013-10-12 16:53:13 +01003212#if MICROPY_EMIT_INLINE_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003213 } else if (s->emit_options == EMIT_OPT_ASM_THUMB) {
3214 // inline assembly for thumb
Damien826005c2013-10-05 23:17:28 +01003215 if (emit_inline_thumb == NULL) {
3216 emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
3217 }
3218 comp->emit = NULL;
3219 comp->emit_method_table = NULL;
3220 comp->emit_inline_asm = emit_inline_thumb;
3221 comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
3222 compile_scope_inline_asm(comp, s, PASS_2);
3223 compile_scope_inline_asm(comp, s, PASS_3);
Damienc025ebb2013-10-12 14:30:21 +01003224#endif
3225
Damien826005c2013-10-05 23:17:28 +01003226 } else {
Damienc025ebb2013-10-12 14:30:21 +01003227
3228 // choose the emit type
3229
Damien3ef4abb2013-10-12 16:53:13 +01003230#if MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003231 comp->emit = emit_cpython_new(max_num_labels);
3232 comp->emit_method_table = &emit_cpython_method_table;
3233#else
Damien826005c2013-10-05 23:17:28 +01003234 switch (s->emit_options) {
Damien Georgee67ed5d2014-01-04 13:55:24 +00003235
3236#if MICROPY_EMIT_NATIVE
Damien826005c2013-10-05 23:17:28 +01003237 case EMIT_OPT_NATIVE_PYTHON:
Damien3410be82013-10-07 23:09:10 +01003238 case EMIT_OPT_VIPER:
Damien3ef4abb2013-10-12 16:53:13 +01003239#if MICROPY_EMIT_X64
Damiendc833822013-10-06 01:01:01 +01003240 if (emit_native == NULL) {
Damien13ed3a62013-10-08 09:05:10 +01003241 emit_native = emit_native_x64_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003242 }
Damien13ed3a62013-10-08 09:05:10 +01003243 comp->emit_method_table = &emit_native_x64_method_table;
Damien3ef4abb2013-10-12 16:53:13 +01003244#elif MICROPY_EMIT_THUMB
Damienc025ebb2013-10-12 14:30:21 +01003245 if (emit_native == NULL) {
3246 emit_native = emit_native_thumb_new(max_num_labels);
3247 }
3248 comp->emit_method_table = &emit_native_thumb_method_table;
3249#endif
3250 comp->emit = emit_native;
Damien3410be82013-10-07 23:09:10 +01003251 comp->emit_method_table->set_native_types(comp->emit, s->emit_options == EMIT_OPT_VIPER);
Damien7af3d192013-10-07 00:02:49 +01003252 break;
Damien Georgee67ed5d2014-01-04 13:55:24 +00003253#endif // MICROPY_EMIT_NATIVE
Damien7af3d192013-10-07 00:02:49 +01003254
Damien826005c2013-10-05 23:17:28 +01003255 default:
3256 if (emit_bc == NULL) {
Damien Georgecbd2f742014-01-19 11:48:48 +00003257 emit_bc = emit_bc_new(max_num_labels);
Damien826005c2013-10-05 23:17:28 +01003258 }
3259 comp->emit = emit_bc;
3260 comp->emit_method_table = &emit_bc_method_table;
3261 break;
3262 }
Damien Georgee67ed5d2014-01-04 13:55:24 +00003263#endif // !MICROPY_EMIT_CPYTHON
Damienc025ebb2013-10-12 14:30:21 +01003264
3265 // compile pass 2 and pass 3
Damien826005c2013-10-05 23:17:28 +01003266 compile_scope(comp, s, PASS_2);
3267 compile_scope(comp, s, PASS_3);
Damien6cdd3af2013-10-05 18:08:26 +01003268 }
Damien429d7192013-10-04 19:53:11 +01003269 }
3270
Damien George41d02b62014-01-24 22:42:28 +00003271 // free the emitters
3272#if !MICROPY_EMIT_CPYTHON
3273 if (emit_bc != NULL) {
3274 emit_bc_free(emit_bc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +02003275 }
Damien George41d02b62014-01-24 22:42:28 +00003276#if MICROPY_EMIT_NATIVE
3277 if (emit_native != NULL) {
3278#if MICROPY_EMIT_X64
3279 emit_native_x64_free(emit_native);
3280#elif MICROPY_EMIT_THUMB
3281 emit_native_thumb_free(emit_native);
3282#endif
3283 }
3284#endif
3285#if MICROPY_EMIT_INLINE_THUMB
3286 if (emit_inline_thumb != NULL) {
3287 emit_inline_thumb_free(emit_inline_thumb);
3288 }
3289#endif
3290#endif // !MICROPY_EMIT_CPYTHON
3291
3292 // free the scopes
Paul Sokolovskyfd313582014-01-23 23:05:47 +02003293 uint unique_code_id = module_scope->unique_code_id;
3294 for (scope_t *s = module_scope; s;) {
3295 scope_t *next = s->next;
3296 scope_free(s);
3297 s = next;
3298 }
Damien5ac1b2e2013-10-18 19:58:12 +01003299
Damien George41d02b62014-01-24 22:42:28 +00003300 // free the compiler
3301 bool had_error = comp->had_error;
3302 m_del_obj(compiler_t, comp);
3303
Damien George1fb03172014-01-03 14:22:03 +00003304 if (had_error) {
3305 // TODO return a proper error message
3306 return mp_const_none;
3307 } else {
3308#if MICROPY_EMIT_CPYTHON
3309 // can't create code, so just return true
Damien George41d02b62014-01-24 22:42:28 +00003310 (void)unique_code_id; // to suppress warning that unique_code_id is unused
Damien George1fb03172014-01-03 14:22:03 +00003311 return mp_const_true;
3312#else
3313 // return function that executes the outer module
Paul Sokolovsky90750022014-02-01 15:05:04 +02003314 return rt_make_function_from_id(unique_code_id, MP_OBJ_NULL);
Damien George1fb03172014-01-03 14:22:03 +00003315#endif
3316 }
Damien429d7192013-10-04 19:53:11 +01003317}