Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
Damien George | 9d5e5c0 | 2015-09-24 13:15:57 +0100 | [diff] [blame] | 6 | * Copyright (c) 2013-2015 Damien P. George |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
| 26 | |
xbe | efe3422 | 2014-03-16 00:14:26 -0700 | [diff] [blame] | 27 | #include <stdbool.h> |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 28 | #include <stdint.h> |
| 29 | #include <stdio.h> |
| 30 | #include <string.h> |
| 31 | #include <assert.h> |
| 32 | |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 33 | #include "py/scope.h" |
| 34 | #include "py/emit.h" |
| 35 | #include "py/compile.h" |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 36 | #include "py/runtime.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 37 | |
Damien George | dd5353a | 2015-12-18 12:35:44 +0000 | [diff] [blame] | 38 | #if MICROPY_ENABLE_COMPILER |
| 39 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 40 | // TODO need to mangle __attr names |
| 41 | |
| 42 | typedef enum { |
Damien George | 00208ce | 2014-01-23 00:00:53 +0000 | [diff] [blame] | 43 | #define DEF_RULE(rule, comp, kind, ...) PN_##rule, |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 44 | #include "py/grammar.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 45 | #undef DEF_RULE |
| 46 | PN_maximum_number_of, |
Damien George | 5042bce | 2014-05-25 22:06:06 +0100 | [diff] [blame] | 47 | PN_string, // special node for non-interned string |
Damien George | 4c81ba8 | 2015-01-13 16:21:23 +0000 | [diff] [blame] | 48 | PN_bytes, // special node for non-interned bytes |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 49 | PN_const_object, // special node for a constant, generic Python object |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 50 | } pn_kind_t; |
| 51 | |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 52 | #define NEED_METHOD_TABLE MICROPY_EMIT_NATIVE |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 53 | |
| 54 | #if NEED_METHOD_TABLE |
| 55 | |
| 56 | // we need a method table to do the lookup for the emitter functions |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 57 | #define EMIT(fun) (comp->emit_method_table->fun(comp->emit)) |
| 58 | #define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__)) |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 59 | #define EMIT_LOAD_FAST(qst, local_num) (comp->emit_method_table->load_id.fast(comp->emit, qst, local_num)) |
| 60 | #define EMIT_LOAD_GLOBAL(qst) (comp->emit_method_table->load_id.global(comp->emit, qst)) |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 61 | |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 62 | #else |
| 63 | |
| 64 | // if we only have the bytecode emitter enabled then we can do a direct call to the functions |
| 65 | #define EMIT(fun) (mp_emit_bc_##fun(comp->emit)) |
| 66 | #define EMIT_ARG(fun, ...) (mp_emit_bc_##fun(comp->emit, __VA_ARGS__)) |
| 67 | #define EMIT_LOAD_FAST(qst, local_num) (mp_emit_bc_load_fast(comp->emit, qst, local_num)) |
| 68 | #define EMIT_LOAD_GLOBAL(qst) (mp_emit_bc_load_global(comp->emit, qst)) |
| 69 | |
| 70 | #endif |
| 71 | |
Damien George | 0496de2 | 2015-10-03 17:07:54 +0100 | [diff] [blame] | 72 | #define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm)) |
| 73 | #define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__)) |
| 74 | |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 75 | // elements in this struct are ordered to make it compact |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 76 | typedef struct _compiler_t { |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 77 | qstr source_file; |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 78 | |
Damien George | 78035b9 | 2014-04-09 12:27:39 +0100 | [diff] [blame] | 79 | uint8_t is_repl; |
| 80 | uint8_t pass; // holds enum type pass_kind_t |
Damien George | 78035b9 | 2014-04-09 12:27:39 +0100 | [diff] [blame] | 81 | uint8_t func_arg_is_super; // used to compile special case of super() function call |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 82 | uint8_t have_star; |
| 83 | |
| 84 | // try to keep compiler clean from nlr |
Damien George | cfc4c33 | 2015-07-29 22:16:01 +0000 | [diff] [blame] | 85 | mp_obj_t compile_error; // set to an exception object if there's an error |
Damien George | 831137b | 2015-12-17 13:13:18 +0000 | [diff] [blame] | 86 | size_t compile_error_line; // set to best guess of line of error |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 87 | |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 88 | uint next_label; |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 89 | |
Damien George | 69b89d2 | 2014-04-11 13:38:30 +0000 | [diff] [blame] | 90 | uint16_t num_dict_params; |
| 91 | uint16_t num_default_params; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 92 | |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 93 | uint16_t break_label; // highest bit set indicates we are breaking out of a for loop |
| 94 | uint16_t continue_label; |
| 95 | uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT |
Damien George | 090c923 | 2014-10-17 14:08:49 +0000 | [diff] [blame] | 96 | uint16_t break_continue_except_level; |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 97 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 98 | scope_t *scope_head; |
| 99 | scope_t *scope_cur; |
| 100 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 101 | emit_t *emit; // current emitter |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 102 | #if NEED_METHOD_TABLE |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 103 | const emit_method_table_t *emit_method_table; // current emit method table |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 104 | #endif |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 105 | |
Damien George | a77ffe6 | 2015-03-14 12:59:31 +0000 | [diff] [blame] | 106 | #if MICROPY_EMIT_INLINE_THUMB |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 107 | emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm |
| 108 | const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm |
Damien George | a77ffe6 | 2015-03-14 12:59:31 +0000 | [diff] [blame] | 109 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 110 | } compiler_t; |
| 111 | |
Damien George | cfc4c33 | 2015-07-29 22:16:01 +0000 | [diff] [blame] | 112 | STATIC void compile_error_set_line(compiler_t *comp, mp_parse_node_t pn) { |
| 113 | // if the line of the error is unknown then try to update it from the pn |
| 114 | if (comp->compile_error_line == 0 && MP_PARSE_NODE_IS_STRUCT(pn)) { |
Damien George | 831137b | 2015-12-17 13:13:18 +0000 | [diff] [blame] | 115 | comp->compile_error_line = ((mp_parse_node_struct_t*)pn)->source_line; |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 116 | } |
Damien George | 84d59c2 | 2015-07-27 22:20:00 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) { |
Damien George | cfc4c33 | 2015-07-29 22:16:01 +0000 | [diff] [blame] | 120 | // only register the error if there has been no other error |
| 121 | if (comp->compile_error == MP_OBJ_NULL) { |
| 122 | comp->compile_error = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg); |
| 123 | compile_error_set_line(comp, pn); |
| 124 | } |
Damien George | f41fdd0 | 2014-03-03 23:19:11 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 127 | STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra); |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 128 | STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind); |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 129 | STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 130 | |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 131 | STATIC uint comp_next_label(compiler_t *comp) { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 132 | return comp->next_label++; |
| 133 | } |
| 134 | |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 135 | STATIC void compile_increase_except_level(compiler_t *comp) { |
| 136 | comp->cur_except_level += 1; |
| 137 | if (comp->cur_except_level > comp->scope_cur->exc_stack_size) { |
| 138 | comp->scope_cur->exc_stack_size = comp->cur_except_level; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | STATIC void compile_decrease_except_level(compiler_t *comp) { |
| 143 | assert(comp->cur_except_level > 0); |
| 144 | comp->cur_except_level -= 1; |
| 145 | } |
| 146 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 147 | STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) { |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 148 | scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 149 | scope->parent = comp->scope_cur; |
| 150 | scope->next = NULL; |
| 151 | if (comp->scope_head == NULL) { |
| 152 | comp->scope_head = scope; |
| 153 | } else { |
| 154 | scope_t *s = comp->scope_head; |
| 155 | while (s->next != NULL) { |
| 156 | s = s->next; |
| 157 | } |
| 158 | s->next = scope; |
| 159 | } |
| 160 | return scope; |
| 161 | } |
| 162 | |
Damien George | 91bc32d | 2015-04-09 15:31:53 +0000 | [diff] [blame] | 163 | typedef void (*apply_list_fun_t)(compiler_t *comp, mp_parse_node_t pn); |
| 164 | |
| 165 | STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, pn_kind_t pn_list_kind, apply_list_fun_t f) { |
Damien George | 963a5a3 | 2015-01-16 17:47:07 +0000 | [diff] [blame] | 166 | if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, pn_list_kind)) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 167 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
| 168 | int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 169 | for (int i = 0; i < num_nodes; i++) { |
| 170 | f(comp, pns->nodes[i]); |
| 171 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 172 | } else if (!MP_PARSE_NODE_IS_NULL(pn)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 173 | f(comp, pn); |
| 174 | } |
| 175 | } |
| 176 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 177 | STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 178 | int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 179 | for (int i = 0; i < num_nodes; i++) { |
| 180 | compile_node(comp, pns->nodes[i]); |
Damien George | cfc4c33 | 2015-07-29 22:16:01 +0000 | [diff] [blame] | 181 | if (comp->compile_error != MP_OBJ_NULL) { |
| 182 | // add line info for the error in case it didn't have a line number |
| 183 | compile_error_set_line(comp, pns->nodes[i]); |
| 184 | return; |
| 185 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 189 | STATIC void compile_load_id(compiler_t *comp, qstr qst) { |
| 190 | if (comp->pass == MP_PASS_SCOPE) { |
| 191 | mp_emit_common_get_id_for_load(comp->scope_cur, qst); |
| 192 | } else { |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 193 | #if NEED_METHOD_TABLE |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 194 | mp_emit_common_id_op(comp->emit, &comp->emit_method_table->load_id, comp->scope_cur, qst); |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 195 | #else |
| 196 | mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_load_id_ops, comp->scope_cur, qst); |
| 197 | #endif |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | STATIC void compile_store_id(compiler_t *comp, qstr qst) { |
| 202 | if (comp->pass == MP_PASS_SCOPE) { |
| 203 | mp_emit_common_get_id_for_modification(comp->scope_cur, qst); |
| 204 | } else { |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 205 | #if NEED_METHOD_TABLE |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 206 | mp_emit_common_id_op(comp->emit, &comp->emit_method_table->store_id, comp->scope_cur, qst); |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 207 | #else |
| 208 | mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_store_id_ops, comp->scope_cur, qst); |
| 209 | #endif |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
| 213 | STATIC void compile_delete_id(compiler_t *comp, qstr qst) { |
| 214 | if (comp->pass == MP_PASS_SCOPE) { |
| 215 | mp_emit_common_get_id_for_modification(comp->scope_cur, qst); |
| 216 | } else { |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 217 | #if NEED_METHOD_TABLE |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 218 | mp_emit_common_id_op(comp->emit, &comp->emit_method_table->delete_id, comp->scope_cur, qst); |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 219 | #else |
| 220 | mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_delete_id_ops, comp->scope_cur, qst); |
| 221 | #endif |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 225 | STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) { |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 226 | int total = 0; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 227 | if (!MP_PARSE_NODE_IS_NULL(pn)) { |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 228 | compile_node(comp, pn); |
| 229 | total += 1; |
| 230 | } |
| 231 | if (pns_list != NULL) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 232 | int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list); |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 233 | for (int i = 0; i < n; i++) { |
| 234 | compile_node(comp, pns_list->nodes[i]); |
| 235 | } |
| 236 | total += n; |
| 237 | } |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 238 | EMIT_ARG(build_tuple, total); |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 239 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 240 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 241 | STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 242 | // a simple tuple expression |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 243 | c_tuple(comp, MP_PARSE_NODE_NULL, pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 244 | } |
| 245 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 246 | STATIC bool node_is_const_false(mp_parse_node_t pn) { |
Damien George | 391db86 | 2014-10-17 17:57:33 +0000 | [diff] [blame] | 247 | return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE) |
| 248 | || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 249 | } |
| 250 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 251 | STATIC bool node_is_const_true(mp_parse_node_t pn) { |
Damien George | 391db86 | 2014-10-17 17:57:33 +0000 | [diff] [blame] | 252 | return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE) |
| 253 | || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) != 0); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 254 | } |
| 255 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 256 | STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) { |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 257 | if (node_is_const_false(pn)) { |
| 258 | if (jump_if == false) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 259 | EMIT_ARG(jump, label); |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 260 | } |
| 261 | return; |
| 262 | } else if (node_is_const_true(pn)) { |
| 263 | if (jump_if == true) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 264 | EMIT_ARG(jump, label); |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 265 | } |
| 266 | return; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 267 | } else if (MP_PARSE_NODE_IS_STRUCT(pn)) { |
| 268 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
| 269 | int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 270 | if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) { |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 271 | if (jump_if == false) { |
Damien George | 0b2fd91 | 2015-02-28 14:37:54 +0000 | [diff] [blame] | 272 | and_or_logic1:; |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 273 | uint label2 = comp_next_label(comp); |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 274 | for (int i = 0; i < n - 1; i++) { |
Damien George | 0b2fd91 | 2015-02-28 14:37:54 +0000 | [diff] [blame] | 275 | c_if_cond(comp, pns->nodes[i], !jump_if, label2); |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 276 | } |
Damien George | 0b2fd91 | 2015-02-28 14:37:54 +0000 | [diff] [blame] | 277 | c_if_cond(comp, pns->nodes[n - 1], jump_if, label); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 278 | EMIT_ARG(label_assign, label2); |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 279 | } else { |
Damien George | 0b2fd91 | 2015-02-28 14:37:54 +0000 | [diff] [blame] | 280 | and_or_logic2: |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 281 | for (int i = 0; i < n; i++) { |
Damien George | 0b2fd91 | 2015-02-28 14:37:54 +0000 | [diff] [blame] | 282 | c_if_cond(comp, pns->nodes[i], jump_if, label); |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | return; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 286 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) { |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 287 | if (jump_if == false) { |
Damien George | 0b2fd91 | 2015-02-28 14:37:54 +0000 | [diff] [blame] | 288 | goto and_or_logic2; |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 289 | } else { |
Damien George | 0b2fd91 | 2015-02-28 14:37:54 +0000 | [diff] [blame] | 290 | goto and_or_logic1; |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 291 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 292 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) { |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 293 | c_if_cond(comp, pns->nodes[0], !jump_if, label); |
| 294 | return; |
Damien George | eb4e18f | 2014-08-29 20:04:01 +0100 | [diff] [blame] | 295 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) { |
| 296 | // cond is something in parenthesis |
| 297 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
| 298 | // empty tuple, acts as false for the condition |
| 299 | if (jump_if == false) { |
| 300 | EMIT_ARG(jump, label); |
| 301 | } |
Damien George | 93b3726 | 2016-01-07 13:07:52 +0000 | [diff] [blame] | 302 | } else { |
| 303 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)); |
Damien George | eb4e18f | 2014-08-29 20:04:01 +0100 | [diff] [blame] | 304 | // non-empty tuple, acts as true for the condition |
| 305 | if (jump_if == true) { |
| 306 | EMIT_ARG(jump, label); |
| 307 | } |
Damien George | eb4e18f | 2014-08-29 20:04:01 +0100 | [diff] [blame] | 308 | } |
| 309 | return; |
Damien | 3a20517 | 2013-10-12 15:01:56 +0100 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
| 313 | // nothing special, fall back to default compiling for node and jump |
| 314 | compile_node(comp, pn); |
Damien George | 63f3832 | 2015-02-28 15:04:06 +0000 | [diff] [blame] | 315 | EMIT_ARG(pop_jump_if, jump_if, label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t; |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 319 | STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 320 | |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 321 | STATIC void c_assign_atom_expr(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 322 | if (assign_kind != ASSIGN_AUG_STORE) { |
| 323 | compile_node(comp, pns->nodes[0]); |
| 324 | } |
| 325 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 326 | if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
| 327 | mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1]; |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 328 | if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_atom_expr_trailers) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 329 | int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 330 | if (assign_kind != ASSIGN_AUG_STORE) { |
| 331 | for (int i = 0; i < n - 1; i++) { |
| 332 | compile_node(comp, pns1->nodes[i]); |
| 333 | } |
| 334 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 335 | assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1])); |
| 336 | pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 337 | } |
Damien George | aedf583 | 2015-03-25 22:06:47 +0000 | [diff] [blame] | 338 | if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 339 | if (assign_kind == ASSIGN_AUG_STORE) { |
| 340 | EMIT(rot_three); |
| 341 | EMIT(store_subscr); |
| 342 | } else { |
| 343 | compile_node(comp, pns1->nodes[0]); |
| 344 | if (assign_kind == ASSIGN_AUG_LOAD) { |
| 345 | EMIT(dup_top_two); |
Damien George | 729f7b4 | 2014-04-17 22:10:53 +0100 | [diff] [blame] | 346 | EMIT(load_subscr); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 347 | } else { |
| 348 | EMIT(store_subscr); |
| 349 | } |
| 350 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 351 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) { |
| 352 | assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 353 | if (assign_kind == ASSIGN_AUG_LOAD) { |
| 354 | EMIT(dup_top); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 355 | EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 356 | } else { |
| 357 | if (assign_kind == ASSIGN_AUG_STORE) { |
| 358 | EMIT(rot_two); |
| 359 | } |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 360 | EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 361 | } |
| 362 | } else { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 363 | goto cannot_assign; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 364 | } |
| 365 | } else { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 366 | goto cannot_assign; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 367 | } |
| 368 | |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 369 | return; |
| 370 | |
| 371 | cannot_assign: |
| 372 | compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 373 | } |
| 374 | |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 375 | // we need to allow for a caller passing in 1 initial node (node_head) followed by an array of nodes (nodes_tail) |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 376 | STATIC void c_assign_tuple(compiler_t *comp, mp_parse_node_t node_head, uint num_tail, mp_parse_node_t *nodes_tail) { |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 377 | uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1; |
| 378 | |
| 379 | // look for star expression |
Damien George | 963a5a3 | 2015-01-16 17:47:07 +0000 | [diff] [blame] | 380 | uint have_star_index = -1; |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 381 | if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) { |
| 382 | EMIT_ARG(unpack_ex, 0, num_tail); |
| 383 | have_star_index = 0; |
| 384 | } |
Damien George | 963a5a3 | 2015-01-16 17:47:07 +0000 | [diff] [blame] | 385 | for (uint i = 0; i < num_tail; i++) { |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 386 | if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) { |
Damien George | 963a5a3 | 2015-01-16 17:47:07 +0000 | [diff] [blame] | 387 | if (have_star_index == (uint)-1) { |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 388 | EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1); |
| 389 | have_star_index = num_head + i; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 390 | } else { |
Damien George | 9d181f6 | 2014-04-27 16:55:27 +0100 | [diff] [blame] | 391 | compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 392 | return; |
| 393 | } |
| 394 | } |
| 395 | } |
Damien George | 963a5a3 | 2015-01-16 17:47:07 +0000 | [diff] [blame] | 396 | if (have_star_index == (uint)-1) { |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 397 | EMIT_ARG(unpack_sequence, num_head + num_tail); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 398 | } |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 399 | if (num_head != 0) { |
| 400 | if (0 == have_star_index) { |
| 401 | c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 402 | } else { |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 403 | c_assign(comp, node_head, ASSIGN_STORE); |
| 404 | } |
| 405 | } |
Damien George | 963a5a3 | 2015-01-16 17:47:07 +0000 | [diff] [blame] | 406 | for (uint i = 0; i < num_tail; i++) { |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 407 | if (num_head + i == have_star_index) { |
| 408 | c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE); |
| 409 | } else { |
| 410 | c_assign(comp, nodes_tail[i], ASSIGN_STORE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | // assigns top of stack to pn |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 416 | STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) { |
Damien George | aedf583 | 2015-03-25 22:06:47 +0000 | [diff] [blame] | 417 | assert(!MP_PARSE_NODE_IS_NULL(pn)); |
| 418 | if (MP_PARSE_NODE_IS_LEAF(pn)) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 419 | if (MP_PARSE_NODE_IS_ID(pn)) { |
Damien George | 42f3de9 | 2014-10-03 17:44:14 +0000 | [diff] [blame] | 420 | qstr arg = MP_PARSE_NODE_LEAF_ARG(pn); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 421 | switch (assign_kind) { |
| 422 | case ASSIGN_STORE: |
| 423 | case ASSIGN_AUG_STORE: |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 424 | compile_store_id(comp, arg); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 425 | break; |
| 426 | case ASSIGN_AUG_LOAD: |
Damien George | d2d64f0 | 2015-01-14 21:32:42 +0000 | [diff] [blame] | 427 | default: |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 428 | compile_load_id(comp, arg); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 429 | break; |
| 430 | } |
| 431 | } else { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 432 | compile_syntax_error(comp, pn, "can't assign to literal"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 433 | return; |
| 434 | } |
| 435 | } else { |
Damien George | aedf583 | 2015-03-25 22:06:47 +0000 | [diff] [blame] | 436 | // pn must be a struct |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 437 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
| 438 | switch (MP_PARSE_NODE_STRUCT_KIND(pns)) { |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 439 | case PN_atom_expr_normal: |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 440 | // lhs is an index or attribute |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 441 | c_assign_atom_expr(comp, pns, assign_kind); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 442 | break; |
| 443 | |
| 444 | case PN_testlist_star_expr: |
| 445 | case PN_exprlist: |
| 446 | // lhs is a tuple |
| 447 | if (assign_kind != ASSIGN_STORE) { |
| 448 | goto bad_aug; |
| 449 | } |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 450 | c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 451 | break; |
| 452 | |
| 453 | case PN_atom_paren: |
| 454 | // lhs is something in parenthesis |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 455 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 456 | // empty tuple |
Damien George | 9d181f6 | 2014-04-27 16:55:27 +0100 | [diff] [blame] | 457 | goto cannot_assign; |
Damien George | 93b3726 | 2016-01-07 13:07:52 +0000 | [diff] [blame] | 458 | } else { |
| 459 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)); |
Damien George | 44f65c0 | 2015-03-25 23:06:48 +0000 | [diff] [blame] | 460 | if (assign_kind != ASSIGN_STORE) { |
| 461 | goto bad_aug; |
| 462 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 463 | pns = (mp_parse_node_struct_t*)pns->nodes[0]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 464 | goto testlist_comp; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 465 | } |
| 466 | break; |
| 467 | |
| 468 | case PN_atom_bracket: |
| 469 | // lhs is something in brackets |
| 470 | if (assign_kind != ASSIGN_STORE) { |
| 471 | goto bad_aug; |
| 472 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 473 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 474 | // empty list, assignment allowed |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 475 | c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 476 | } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) { |
| 477 | pns = (mp_parse_node_struct_t*)pns->nodes[0]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 478 | goto testlist_comp; |
| 479 | } else { |
| 480 | // brackets around 1 item |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 481 | c_assign_tuple(comp, pns->nodes[0], 0, NULL); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 482 | } |
| 483 | break; |
| 484 | |
| 485 | default: |
Damien George | 9d181f6 | 2014-04-27 16:55:27 +0100 | [diff] [blame] | 486 | goto cannot_assign; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 487 | } |
| 488 | return; |
| 489 | |
| 490 | testlist_comp: |
| 491 | // lhs is a sequence |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 492 | if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
| 493 | mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1]; |
| 494 | if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 495 | // sequence of one item, with trailing comma |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 496 | assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0])); |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 497 | c_assign_tuple(comp, pns->nodes[0], 0, NULL); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 498 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 499 | // sequence of many items |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 500 | uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2); |
| 501 | c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 502 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 503 | // TODO can we ever get here? can it be compiled? |
Damien George | 9d181f6 | 2014-04-27 16:55:27 +0100 | [diff] [blame] | 504 | goto cannot_assign; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 505 | } else { |
| 506 | // sequence with 2 items |
| 507 | goto sequence_with_2_items; |
| 508 | } |
| 509 | } else { |
| 510 | // sequence with 2 items |
| 511 | sequence_with_2_items: |
Damien George | 0288cf0 | 2014-04-11 11:53:00 +0000 | [diff] [blame] | 512 | c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 513 | } |
| 514 | return; |
| 515 | } |
| 516 | return; |
| 517 | |
Damien George | 9d181f6 | 2014-04-27 16:55:27 +0100 | [diff] [blame] | 518 | cannot_assign: |
| 519 | compile_syntax_error(comp, pn, "can't assign to expression"); |
| 520 | return; |
| 521 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 522 | bad_aug: |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 523 | compile_syntax_error(comp, pn, "illegal expression for augmented assignment"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 524 | } |
| 525 | |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 526 | // stuff for lambda and comprehensions and generators: |
Damien George | e337f1e | 2014-03-31 15:18:37 +0100 | [diff] [blame] | 527 | // if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults |
| 528 | // if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults |
| 529 | // if both exist, the tuple is above the dictionary (ie the first pop gets the tuple) |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 530 | STATIC void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) { |
Damien George | 3056509 | 2014-03-31 11:30:17 +0100 | [diff] [blame] | 531 | assert(n_pos_defaults >= 0); |
| 532 | assert(n_kw_defaults >= 0); |
| 533 | |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 534 | // set flags |
| 535 | if (n_kw_defaults > 0) { |
| 536 | this_scope->scope_flags |= MP_SCOPE_FLAG_DEFKWARGS; |
| 537 | } |
| 538 | this_scope->num_def_pos_args = n_pos_defaults; |
| 539 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 540 | // make closed over variables, if any |
Damien | 318aec6 | 2013-12-10 18:28:17 +0000 | [diff] [blame] | 541 | // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython) |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 542 | int nfree = 0; |
| 543 | if (comp->scope_cur->kind != SCOPE_MODULE) { |
Damien | 318aec6 | 2013-12-10 18:28:17 +0000 | [diff] [blame] | 544 | for (int i = 0; i < comp->scope_cur->id_info_len; i++) { |
| 545 | id_info_t *id = &comp->scope_cur->id_info[i]; |
| 546 | if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) { |
| 547 | for (int j = 0; j < this_scope->id_info_len; j++) { |
| 548 | id_info_t *id2 = &this_scope->id_info[j]; |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 549 | if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) { |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 550 | // in Micro Python we load closures using LOAD_FAST |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 551 | EMIT_LOAD_FAST(id->qst, id->local_num); |
Damien | 318aec6 | 2013-12-10 18:28:17 +0000 | [diff] [blame] | 552 | nfree += 1; |
| 553 | } |
| 554 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 558 | |
| 559 | // make the function/closure |
| 560 | if (nfree == 0) { |
Damien George | 3056509 | 2014-03-31 11:30:17 +0100 | [diff] [blame] | 561 | EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 562 | } else { |
Damien George | 3558f62 | 2014-04-20 17:50:40 +0100 | [diff] [blame] | 563 | EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
Damien George | 2c83894 | 2015-11-17 14:00:14 +0000 | [diff] [blame] | 567 | STATIC void compile_funcdef_lambdef_param(compiler_t *comp, mp_parse_node_t pn) { |
| 568 | if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star) |
| 569 | || MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_star)) { |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 570 | comp->have_star = true; |
| 571 | /* don't need to distinguish bare from named star |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 572 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
| 573 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 574 | // bare star |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 575 | } else { |
| 576 | // named star |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 577 | } |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 578 | */ |
Damien George | f41fdd0 | 2014-03-03 23:19:11 +0000 | [diff] [blame] | 579 | |
Damien George | 2c83894 | 2015-11-17 14:00:14 +0000 | [diff] [blame] | 580 | } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star) |
| 581 | || MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_dbl_star)) { |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 582 | // named double star |
Damien George | f41fdd0 | 2014-03-03 23:19:11 +0000 | [diff] [blame] | 583 | // TODO do we need to do anything with this? |
| 584 | |
| 585 | } else { |
| 586 | mp_parse_node_t pn_id; |
| 587 | mp_parse_node_t pn_colon; |
| 588 | mp_parse_node_t pn_equal; |
| 589 | if (MP_PARSE_NODE_IS_ID(pn)) { |
| 590 | // this parameter is just an id |
| 591 | |
| 592 | pn_id = pn; |
| 593 | pn_colon = MP_PARSE_NODE_NULL; |
| 594 | pn_equal = MP_PARSE_NODE_NULL; |
| 595 | |
Damien George | 2c83894 | 2015-11-17 14:00:14 +0000 | [diff] [blame] | 596 | } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) { |
Damien George | f41fdd0 | 2014-03-03 23:19:11 +0000 | [diff] [blame] | 597 | // this parameter has a colon and/or equal specifier |
| 598 | |
| 599 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
| 600 | pn_id = pns->nodes[0]; |
| 601 | pn_colon = pns->nodes[1]; |
| 602 | pn_equal = pns->nodes[2]; |
Damien George | 2c83894 | 2015-11-17 14:00:14 +0000 | [diff] [blame] | 603 | |
| 604 | } else { |
| 605 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_name)); // should be |
| 606 | // this parameter has an equal specifier |
| 607 | |
| 608 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
| 609 | pn_id = pns->nodes[0]; |
| 610 | pn_equal = pns->nodes[1]; |
Damien George | f41fdd0 | 2014-03-03 23:19:11 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | if (MP_PARSE_NODE_IS_NULL(pn_equal)) { |
| 614 | // this parameter does not have a default value |
| 615 | |
| 616 | // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid) |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 617 | if (!comp->have_star && comp->num_default_params != 0) { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 618 | compile_syntax_error(comp, pn, "non-default argument follows default argument"); |
Damien George | f41fdd0 | 2014-03-03 23:19:11 +0000 | [diff] [blame] | 619 | return; |
| 620 | } |
| 621 | |
| 622 | } else { |
| 623 | // this parameter has a default value |
| 624 | // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why |
| 625 | |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 626 | if (comp->have_star) { |
Damien George | 69b89d2 | 2014-04-11 13:38:30 +0000 | [diff] [blame] | 627 | comp->num_dict_params += 1; |
Damien George | 69b89d2 | 2014-04-11 13:38:30 +0000 | [diff] [blame] | 628 | // in Micro Python we put the default dict parameters into a dictionary using the bytecode |
| 629 | if (comp->num_dict_params == 1) { |
Damien George | 7b43301 | 2014-04-12 00:05:49 +0100 | [diff] [blame] | 630 | // in Micro Python we put the default positional parameters into a tuple using the bytecode |
| 631 | // we need to do this here before we start building the map for the default keywords |
| 632 | if (comp->num_default_params > 0) { |
| 633 | EMIT_ARG(build_tuple, comp->num_default_params); |
Damien George | 3558f62 | 2014-04-20 17:50:40 +0100 | [diff] [blame] | 634 | } else { |
| 635 | EMIT(load_null); // sentinel indicating empty default positional args |
Damien George | 7b43301 | 2014-04-12 00:05:49 +0100 | [diff] [blame] | 636 | } |
Damien George | 69b89d2 | 2014-04-11 13:38:30 +0000 | [diff] [blame] | 637 | // first default dict param, so make the map |
| 638 | EMIT_ARG(build_map, 0); |
Damien George | f41fdd0 | 2014-03-03 23:19:11 +0000 | [diff] [blame] | 639 | } |
Damien George | f0778a7 | 2014-06-07 22:01:00 +0100 | [diff] [blame] | 640 | |
| 641 | // compile value then key, then store it to the dict |
Damien George | 69b89d2 | 2014-04-11 13:38:30 +0000 | [diff] [blame] | 642 | compile_node(comp, pn_equal); |
Damien George | 59fba2d | 2015-06-25 14:42:13 +0000 | [diff] [blame] | 643 | EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id)); |
Damien George | 69b89d2 | 2014-04-11 13:38:30 +0000 | [diff] [blame] | 644 | EMIT(store_map); |
Damien George | f41fdd0 | 2014-03-03 23:19:11 +0000 | [diff] [blame] | 645 | } else { |
Damien George | 69b89d2 | 2014-04-11 13:38:30 +0000 | [diff] [blame] | 646 | comp->num_default_params += 1; |
| 647 | compile_node(comp, pn_equal); |
Damien George | f41fdd0 | 2014-03-03 23:19:11 +0000 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | |
| 651 | // TODO pn_colon not implemented |
| 652 | (void)pn_colon; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
Damien George | 2c83894 | 2015-11-17 14:00:14 +0000 | [diff] [blame] | 656 | STATIC void compile_funcdef_lambdef(compiler_t *comp, scope_t *scope, mp_parse_node_t pn_params, pn_kind_t pn_list_kind) { |
Damien George | 29e9db0 | 2015-12-12 13:42:51 +0000 | [diff] [blame] | 657 | // When we call compile_funcdef_lambdef_param below it can compile an arbitrary |
| 658 | // expression for default arguments, which may contain a lambda. The lambda will |
| 659 | // call here in a nested way, so we must save and restore the relevant state. |
| 660 | bool orig_have_star = comp->have_star; |
| 661 | uint16_t orig_num_dict_params = comp->num_dict_params; |
| 662 | uint16_t orig_num_default_params = comp->num_default_params; |
| 663 | |
Damien George | 2c83894 | 2015-11-17 14:00:14 +0000 | [diff] [blame] | 664 | // compile default parameters |
| 665 | comp->have_star = false; |
| 666 | comp->num_dict_params = 0; |
| 667 | comp->num_default_params = 0; |
| 668 | apply_to_single_or_list(comp, pn_params, pn_list_kind, compile_funcdef_lambdef_param); |
| 669 | |
| 670 | if (comp->compile_error != MP_OBJ_NULL) { |
| 671 | return; |
| 672 | } |
| 673 | |
| 674 | // in Micro Python we put the default positional parameters into a tuple using the bytecode |
| 675 | // the default keywords args may have already made the tuple; if not, do it now |
| 676 | if (comp->num_default_params > 0 && comp->num_dict_params == 0) { |
| 677 | EMIT_ARG(build_tuple, comp->num_default_params); |
| 678 | EMIT(load_null); // sentinel indicating empty default keyword args |
| 679 | } |
| 680 | |
| 681 | // make the function |
| 682 | close_over_variables_etc(comp, scope, comp->num_default_params, comp->num_dict_params); |
Damien George | 29e9db0 | 2015-12-12 13:42:51 +0000 | [diff] [blame] | 683 | |
| 684 | // restore state |
| 685 | comp->have_star = orig_have_star; |
| 686 | comp->num_dict_params = orig_num_dict_params; |
| 687 | comp->num_default_params = orig_num_default_params; |
Damien George | 2c83894 | 2015-11-17 14:00:14 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 690 | // leaves function object on stack |
| 691 | // returns function name |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 692 | STATIC qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) { |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 693 | if (comp->pass == MP_PASS_SCOPE) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 694 | // create a new scope for this function |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 695 | scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 696 | // store the function scope so the compiling function can use it at each pass |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 697 | pns->nodes[4] = (mp_parse_node_t)s; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 698 | } |
| 699 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 700 | // get the scope for this function |
| 701 | scope_t *fscope = (scope_t*)pns->nodes[4]; |
| 702 | |
Damien George | 2c83894 | 2015-11-17 14:00:14 +0000 | [diff] [blame] | 703 | // compile the function definition |
| 704 | compile_funcdef_lambdef(comp, fscope, pns->nodes[1], PN_typedargslist); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 705 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 706 | // return its name (the 'f' in "def f(...):") |
| 707 | return fscope->simple_name; |
| 708 | } |
| 709 | |
| 710 | // leaves class object on stack |
| 711 | // returns class name |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 712 | STATIC qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) { |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 713 | if (comp->pass == MP_PASS_SCOPE) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 714 | // create a new scope for this class |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 715 | scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 716 | // store the class scope so the compiling function can use it at each pass |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 717 | pns->nodes[3] = (mp_parse_node_t)s; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | EMIT(load_build_class); |
| 721 | |
| 722 | // scope for this class |
| 723 | scope_t *cscope = (scope_t*)pns->nodes[3]; |
| 724 | |
| 725 | // compile the class |
| 726 | close_over_variables_etc(comp, cscope, 0, 0); |
| 727 | |
| 728 | // get its name |
Damien George | 59fba2d | 2015-06-25 14:42:13 +0000 | [diff] [blame] | 729 | EMIT_ARG(load_const_str, cscope->simple_name); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 730 | |
| 731 | // nodes[1] has parent classes, if any |
Damien George | 804760b | 2014-03-30 23:06:37 +0100 | [diff] [blame] | 732 | // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling |
| 733 | mp_parse_node_t parents = pns->nodes[1]; |
| 734 | if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) { |
| 735 | parents = MP_PARSE_NODE_NULL; |
| 736 | } |
Damien George | bbcd49a | 2014-02-06 20:30:16 +0000 | [diff] [blame] | 737 | comp->func_arg_is_super = false; |
Damien George | 804760b | 2014-03-30 23:06:37 +0100 | [diff] [blame] | 738 | compile_trailer_paren_helper(comp, parents, false, 2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 739 | |
| 740 | // return its name (the 'C' in class C(...):") |
| 741 | return cscope->simple_name; |
| 742 | } |
| 743 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 744 | // returns true if it was a built-in decorator (even if the built-in had an error) |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 745 | STATIC bool compile_built_in_decorator(compiler_t *comp, int name_len, mp_parse_node_t *name_nodes, uint *emit_options) { |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 746 | if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) { |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 747 | return false; |
| 748 | } |
| 749 | |
| 750 | if (name_len != 2) { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 751 | compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator"); |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 752 | return true; |
| 753 | } |
| 754 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 755 | qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]); |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame] | 756 | if (attr == MP_QSTR_bytecode) { |
Damien George | 96f137b | 2014-05-12 22:35:37 +0100 | [diff] [blame] | 757 | *emit_options = MP_EMIT_OPT_BYTECODE; |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 758 | #if MICROPY_EMIT_NATIVE |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 759 | } else if (attr == MP_QSTR_native) { |
Damien George | 65cad12 | 2014-04-06 11:48:15 +0100 | [diff] [blame] | 760 | *emit_options = MP_EMIT_OPT_NATIVE_PYTHON; |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 761 | } else if (attr == MP_QSTR_viper) { |
Damien George | 65cad12 | 2014-04-06 11:48:15 +0100 | [diff] [blame] | 762 | *emit_options = MP_EMIT_OPT_VIPER; |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 763 | #endif |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 764 | #if MICROPY_EMIT_INLINE_THUMB |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 765 | } else if (attr == MP_QSTR_asm_thumb) { |
Damien George | 65cad12 | 2014-04-06 11:48:15 +0100 | [diff] [blame] | 766 | *emit_options = MP_EMIT_OPT_ASM_THUMB; |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 767 | #endif |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 768 | } else { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 769 | compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator"); |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | return true; |
| 773 | } |
| 774 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 775 | STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 776 | // get the list of decorators |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 777 | mp_parse_node_t *nodes; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 778 | int n = mp_parse_node_extract_list(&pns->nodes[0], PN_decorators, &nodes); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 779 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 780 | // inherit emit options for this function/class definition |
| 781 | uint emit_options = comp->scope_cur->emit_options; |
| 782 | |
| 783 | // compile each decorator |
| 784 | int num_built_in_decorators = 0; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 785 | for (int i = 0; i < n; i++) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 786 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be |
| 787 | mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i]; |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 788 | |
| 789 | // nodes[0] contains the decorator function, which is a dotted name |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 790 | mp_parse_node_t *name_nodes; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 791 | int name_len = mp_parse_node_extract_list(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes); |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 792 | |
| 793 | // check for built-in decorators |
| 794 | if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) { |
| 795 | // this was a built-in |
| 796 | num_built_in_decorators += 1; |
| 797 | |
| 798 | } else { |
| 799 | // not a built-in, compile normally |
| 800 | |
| 801 | // compile the decorator function |
| 802 | compile_node(comp, name_nodes[0]); |
Damien George | 50912e7 | 2015-01-20 11:55:10 +0000 | [diff] [blame] | 803 | for (int j = 1; j < name_len; j++) { |
| 804 | assert(MP_PARSE_NODE_IS_ID(name_nodes[j])); // should be |
| 805 | EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[j])); |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | // nodes[1] contains arguments to the decorator function, if any |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 809 | if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) { |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 810 | // call the decorator function with the arguments in nodes[1] |
Damien George | 35e2a4e | 2014-02-05 00:51:47 +0000 | [diff] [blame] | 811 | comp->func_arg_is_super = false; |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 812 | compile_node(comp, pns_decorator->nodes[1]); |
| 813 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 814 | } |
| 815 | } |
| 816 | |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 817 | // compile the body (funcdef, async funcdef or classdef) and get its name |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 818 | mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 819 | qstr body_name = 0; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 820 | if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) { |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 821 | body_name = compile_funcdef_helper(comp, pns_body, emit_options); |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 822 | #if MICROPY_PY_ASYNC_AWAIT |
| 823 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_async_funcdef) { |
| 824 | assert(MP_PARSE_NODE_IS_STRUCT(pns_body->nodes[0])); |
| 825 | mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns_body->nodes[0]; |
| 826 | body_name = compile_funcdef_helper(comp, pns0, emit_options); |
| 827 | scope_t *fscope = (scope_t*)pns0->nodes[4]; |
| 828 | fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR; |
| 829 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 830 | } else { |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 831 | assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be |
| 832 | body_name = compile_classdef_helper(comp, pns_body, emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | // call each decorator |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 836 | for (int i = 0; i < n - num_built_in_decorators; i++) { |
Damien George | 922ddd6 | 2014-04-09 12:43:17 +0100 | [diff] [blame] | 837 | EMIT_ARG(call_function, 1, 0, 0); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | // store func/class object into name |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 841 | compile_store_id(comp, body_name); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 842 | } |
| 843 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 844 | STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 845 | qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 846 | // store function object into function name |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 847 | compile_store_id(comp, fname); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 848 | } |
| 849 | |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 850 | STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 851 | if (MP_PARSE_NODE_IS_ID(pn)) { |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 852 | compile_delete_id(comp, MP_PARSE_NODE_LEAF_ARG(pn)); |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 853 | } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_expr_normal)) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 854 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 855 | |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 856 | compile_node(comp, pns->nodes[0]); // base of the atom_expr_normal node |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 857 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 858 | if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
| 859 | mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1]; |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 860 | if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_atom_expr_trailers) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 861 | int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 862 | for (int i = 0; i < n - 1; i++) { |
| 863 | compile_node(comp, pns1->nodes[i]); |
| 864 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 865 | assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1])); |
| 866 | pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 867 | } |
Damien George | aedf583 | 2015-03-25 22:06:47 +0000 | [diff] [blame] | 868 | if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 869 | compile_node(comp, pns1->nodes[0]); |
| 870 | EMIT(delete_subscr); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 871 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) { |
| 872 | assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0])); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 873 | EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 874 | } else { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 875 | goto cannot_delete; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 876 | } |
| 877 | } else { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 878 | goto cannot_delete; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 879 | } |
| 880 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 881 | } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) { |
| 882 | pn = ((mp_parse_node_struct_t*)pn)->nodes[0]; |
Damien George | 93b3726 | 2016-01-07 13:07:52 +0000 | [diff] [blame] | 883 | if (MP_PARSE_NODE_IS_NULL(pn)) { |
| 884 | goto cannot_delete; |
| 885 | } else { |
| 886 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 887 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 888 | // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp |
| 889 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 890 | if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
| 891 | mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1]; |
| 892 | if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 893 | // sequence of one item, with trailing comma |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 894 | assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 895 | c_del_stmt(comp, pns->nodes[0]); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 896 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 897 | // sequence of many items |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 898 | int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 899 | c_del_stmt(comp, pns->nodes[0]); |
| 900 | for (int i = 0; i < n; i++) { |
| 901 | c_del_stmt(comp, pns1->nodes[i]); |
| 902 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 903 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) { |
Damien George | aedf583 | 2015-03-25 22:06:47 +0000 | [diff] [blame] | 904 | // TODO not implemented; can't del comprehension? can we get here? |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 905 | goto cannot_delete; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 906 | } else { |
| 907 | // sequence with 2 items |
| 908 | goto sequence_with_2_items; |
| 909 | } |
| 910 | } else { |
| 911 | // sequence with 2 items |
| 912 | sequence_with_2_items: |
| 913 | c_del_stmt(comp, pns->nodes[0]); |
| 914 | c_del_stmt(comp, pns->nodes[1]); |
| 915 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 916 | } |
| 917 | } else { |
Damien George | 93b3726 | 2016-01-07 13:07:52 +0000 | [diff] [blame] | 918 | // some arbitrary statment that we can't delete (eg del 1) |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 919 | goto cannot_delete; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 920 | } |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 921 | |
| 922 | return; |
| 923 | |
| 924 | cannot_delete: |
| 925 | compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 926 | } |
| 927 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 928 | STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 929 | apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt); |
| 930 | } |
| 931 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 932 | STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 933 | if (comp->break_label == 0) { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 934 | compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 935 | } |
Damien George | 090c923 | 2014-10-17 14:08:49 +0000 | [diff] [blame] | 936 | assert(comp->cur_except_level >= comp->break_continue_except_level); |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 937 | EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 938 | } |
| 939 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 940 | STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 941 | if (comp->continue_label == 0) { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 942 | compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 943 | } |
Damien George | 090c923 | 2014-10-17 14:08:49 +0000 | [diff] [blame] | 944 | assert(comp->cur_except_level >= comp->break_continue_except_level); |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 945 | EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 946 | } |
| 947 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 948 | STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 949 | if (comp->scope_cur->kind != SCOPE_FUNCTION) { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 950 | compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function"); |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 951 | return; |
| 952 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 953 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 954 | // no argument to 'return', so return None |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 955 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 956 | } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 957 | // special case when returning an if-expression; to match CPython optimisation |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 958 | mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0]; |
| 959 | mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns_test_if_expr->nodes[1]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 960 | |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 961 | uint l_fail = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 962 | c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition |
| 963 | compile_node(comp, pns_test_if_expr->nodes[0]); // success value |
| 964 | EMIT(return_value); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 965 | EMIT_ARG(label_assign, l_fail); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 966 | compile_node(comp, pns_test_if_else->nodes[1]); // failure value |
| 967 | } else { |
| 968 | compile_node(comp, pns->nodes[0]); |
| 969 | } |
| 970 | EMIT(return_value); |
| 971 | } |
| 972 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 973 | STATIC void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 974 | compile_node(comp, pns->nodes[0]); |
| 975 | EMIT(pop_top); |
| 976 | } |
| 977 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 978 | STATIC void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 979 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 980 | // raise |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 981 | EMIT_ARG(raise_varargs, 0); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 982 | } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 983 | // raise x from y |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 984 | pns = (mp_parse_node_struct_t*)pns->nodes[0]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 985 | compile_node(comp, pns->nodes[0]); |
| 986 | compile_node(comp, pns->nodes[1]); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 987 | EMIT_ARG(raise_varargs, 2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 988 | } else { |
| 989 | // raise x |
| 990 | compile_node(comp, pns->nodes[0]); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 991 | EMIT_ARG(raise_varargs, 1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 995 | // q_base holds the base of the name |
| 996 | // eg a -> q_base=a |
| 997 | // a.b.c -> q_base=a |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 998 | STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 999 | bool is_as = false; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1000 | if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) { |
| 1001 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1002 | // a name of the form x as y; unwrap it |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1003 | *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1004 | pn = pns->nodes[0]; |
| 1005 | is_as = true; |
| 1006 | } |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1007 | if (MP_PARSE_NODE_IS_NULL(pn)) { |
| 1008 | // empty name (eg, from . import x) |
| 1009 | *q_base = MP_QSTR_; |
| 1010 | EMIT_ARG(import_name, MP_QSTR_); // import the empty string |
| 1011 | } else if (MP_PARSE_NODE_IS_ID(pn)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1012 | // just a simple name |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1013 | qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1014 | if (!is_as) { |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1015 | *q_base = q_full; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1016 | } |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1017 | EMIT_ARG(import_name, q_full); |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 1018 | } else { |
| 1019 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1020 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 1021 | { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1022 | // a name of the form a.b.c |
| 1023 | if (!is_as) { |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1024 | *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1025 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1026 | int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1027 | int len = n - 1; |
| 1028 | for (int i = 0; i < n; i++) { |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 1029 | len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1030 | } |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 1031 | byte *q_ptr; |
| 1032 | byte *str_dest = qstr_build_start(len, &q_ptr); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1033 | for (int i = 0; i < n; i++) { |
| 1034 | if (i > 0) { |
Damien George | fe8fb91 | 2014-01-02 16:36:09 +0000 | [diff] [blame] | 1035 | *str_dest++ = '.'; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1036 | } |
Damien George | c3f64d9 | 2015-11-27 12:23:18 +0000 | [diff] [blame] | 1037 | size_t str_src_len; |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 1038 | const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len); |
Damien George | fe8fb91 | 2014-01-02 16:36:09 +0000 | [diff] [blame] | 1039 | memcpy(str_dest, str_src, str_src_len); |
| 1040 | str_dest += str_src_len; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1041 | } |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1042 | qstr q_full = qstr_build_end(q_ptr); |
| 1043 | EMIT_ARG(import_name, q_full); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1044 | if (is_as) { |
| 1045 | for (int i = 1; i < n; i++) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1046 | EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1047 | } |
| 1048 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1049 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1050 | } |
| 1051 | } |
| 1052 | |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 1053 | STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) { |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1054 | EMIT_ARG(load_const_small_int, 0); // level 0 import |
| 1055 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything |
| 1056 | qstr q_base; |
| 1057 | do_import_name(comp, pn, &q_base); |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 1058 | compile_store_id(comp, q_base); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1059 | } |
| 1060 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1061 | STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1062 | apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name); |
| 1063 | } |
| 1064 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1065 | STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1066 | mp_parse_node_t pn_import_source = pns->nodes[0]; |
| 1067 | |
| 1068 | // extract the preceeding .'s (if any) for a relative import, to compute the import level |
| 1069 | uint import_level = 0; |
| 1070 | do { |
| 1071 | mp_parse_node_t pn_rel; |
| 1072 | if (MP_PARSE_NODE_IS_TOKEN(pn_import_source) || MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_one_or_more_period_or_ellipsis)) { |
| 1073 | // This covers relative imports with dots only like "from .. import" |
| 1074 | pn_rel = pn_import_source; |
| 1075 | pn_import_source = MP_PARSE_NODE_NULL; |
| 1076 | } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) { |
| 1077 | // This covers relative imports starting with dot(s) like "from .foo import" |
| 1078 | mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source; |
| 1079 | pn_rel = pns_2b->nodes[0]; |
| 1080 | pn_import_source = pns_2b->nodes[1]; |
| 1081 | assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be |
| 1082 | } else { |
| 1083 | // Not a relative import |
| 1084 | break; |
| 1085 | } |
| 1086 | |
| 1087 | // get the list of . and/or ...'s |
| 1088 | mp_parse_node_t *nodes; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 1089 | int n = mp_parse_node_extract_list(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes); |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1090 | |
| 1091 | // count the total number of .'s |
| 1092 | for (int i = 0; i < n; i++) { |
| 1093 | if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) { |
| 1094 | import_level++; |
| 1095 | } else { |
| 1096 | // should be an MP_TOKEN_ELLIPSIS |
| 1097 | import_level += 3; |
| 1098 | } |
| 1099 | } |
| 1100 | } while (0); |
| 1101 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1102 | if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) { |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1103 | EMIT_ARG(load_const_small_int, import_level); |
Damien | db4c361 | 2013-12-10 17:27:24 +0000 | [diff] [blame] | 1104 | |
| 1105 | // build the "fromlist" tuple |
Damien George | 59fba2d | 2015-06-25 14:42:13 +0000 | [diff] [blame] | 1106 | EMIT_ARG(load_const_str, MP_QSTR__star_); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1107 | EMIT_ARG(build_tuple, 1); |
Damien | db4c361 | 2013-12-10 17:27:24 +0000 | [diff] [blame] | 1108 | |
| 1109 | // do the import |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1110 | qstr dummy_q; |
| 1111 | do_import_name(comp, pn_import_source, &dummy_q); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1112 | EMIT(import_star); |
Damien | db4c361 | 2013-12-10 17:27:24 +0000 | [diff] [blame] | 1113 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1114 | } else { |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1115 | EMIT_ARG(load_const_small_int, import_level); |
Damien | db4c361 | 2013-12-10 17:27:24 +0000 | [diff] [blame] | 1116 | |
| 1117 | // build the "fromlist" tuple |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1118 | mp_parse_node_t *pn_nodes; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 1119 | int n = mp_parse_node_extract_list(&pns->nodes[1], PN_import_as_names, &pn_nodes); |
Damien | db4c361 | 2013-12-10 17:27:24 +0000 | [diff] [blame] | 1120 | for (int i = 0; i < n; i++) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1121 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name)); |
| 1122 | mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i]; |
| 1123 | qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id |
Damien George | 59fba2d | 2015-06-25 14:42:13 +0000 | [diff] [blame] | 1124 | EMIT_ARG(load_const_str, id2); |
Damien | db4c361 | 2013-12-10 17:27:24 +0000 | [diff] [blame] | 1125 | } |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1126 | EMIT_ARG(build_tuple, n); |
Damien | db4c361 | 2013-12-10 17:27:24 +0000 | [diff] [blame] | 1127 | |
| 1128 | // do the import |
Damien George | 635543c | 2014-04-10 12:56:52 +0100 | [diff] [blame] | 1129 | qstr dummy_q; |
| 1130 | do_import_name(comp, pn_import_source, &dummy_q); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1131 | for (int i = 0; i < n; i++) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1132 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name)); |
| 1133 | mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i]; |
| 1134 | qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1135 | EMIT_ARG(import_from, id2); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1136 | if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) { |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 1137 | compile_store_id(comp, id2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1138 | } else { |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 1139 | compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1140 | } |
| 1141 | } |
| 1142 | EMIT(pop_top); |
| 1143 | } |
| 1144 | } |
| 1145 | |
Damien George | 584ba67 | 2014-12-21 17:26:45 +0000 | [diff] [blame] | 1146 | STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qst) { |
| 1147 | bool added; |
| 1148 | id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added); |
Damien George | 558a016 | 2015-09-07 16:55:02 +0100 | [diff] [blame] | 1149 | if (!added && id_info->kind != ID_INFO_KIND_GLOBAL_EXPLICIT) { |
| 1150 | compile_syntax_error(comp, pn, "identifier redefined as global"); |
Damien George | 584ba67 | 2014-12-21 17:26:45 +0000 | [diff] [blame] | 1151 | return; |
| 1152 | } |
| 1153 | id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT; |
| 1154 | |
| 1155 | // if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL |
| 1156 | id_info = scope_find_global(comp->scope_cur, qst); |
| 1157 | if (id_info != NULL) { |
| 1158 | id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT; |
| 1159 | } |
| 1160 | } |
| 1161 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1162 | STATIC void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 1163 | if (comp->pass == MP_PASS_SCOPE) { |
Damien George | 584ba67 | 2014-12-21 17:26:45 +0000 | [diff] [blame] | 1164 | mp_parse_node_t *nodes; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 1165 | int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes); |
Damien George | 584ba67 | 2014-12-21 17:26:45 +0000 | [diff] [blame] | 1166 | for (int i = 0; i < n; i++) { |
| 1167 | compile_declare_global(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | } |
| 1171 | |
Damien George | 584ba67 | 2014-12-21 17:26:45 +0000 | [diff] [blame] | 1172 | STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst) { |
| 1173 | bool added; |
| 1174 | id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added); |
Damien George | 558a016 | 2015-09-07 16:55:02 +0100 | [diff] [blame] | 1175 | if (!added && id_info->kind != ID_INFO_KIND_FREE) { |
| 1176 | compile_syntax_error(comp, pn, "identifier redefined as nonlocal"); |
Damien George | 584ba67 | 2014-12-21 17:26:45 +0000 | [diff] [blame] | 1177 | return; |
| 1178 | } |
| 1179 | id_info_t *id_info2 = scope_find_local_in_parent(comp->scope_cur, qst); |
| 1180 | if (id_info2 == NULL || !(id_info2->kind == ID_INFO_KIND_LOCAL || id_info2->kind == ID_INFO_KIND_CELL || id_info2->kind == ID_INFO_KIND_FREE)) { |
| 1181 | compile_syntax_error(comp, pn, "no binding for nonlocal found"); |
| 1182 | return; |
| 1183 | } |
| 1184 | id_info->kind = ID_INFO_KIND_FREE; |
| 1185 | scope_close_over_in_parents(comp->scope_cur, qst); |
| 1186 | } |
| 1187 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1188 | STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 1189 | if (comp->pass == MP_PASS_SCOPE) { |
Damien George | 584ba67 | 2014-12-21 17:26:45 +0000 | [diff] [blame] | 1190 | if (comp->scope_cur->kind == SCOPE_MODULE) { |
| 1191 | compile_syntax_error(comp, (mp_parse_node_t)pns, "can't declare nonlocal in outer code"); |
| 1192 | return; |
| 1193 | } |
| 1194 | mp_parse_node_t *nodes; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 1195 | int n = mp_parse_node_extract_list(&pns->nodes[0], PN_name_list, &nodes); |
Damien George | 584ba67 | 2014-12-21 17:26:45 +0000 | [diff] [blame] | 1196 | for (int i = 0; i < n; i++) { |
| 1197 | compile_declare_nonlocal(comp, (mp_parse_node_t)pns, MP_PARSE_NODE_LEAF_ARG(nodes[i])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1198 | } |
| 1199 | } |
| 1200 | } |
| 1201 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1202 | STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | 24df30c | 2016-08-26 22:28:22 +1000 | [diff] [blame] | 1203 | // with optimisations enabled we don't compile assertions |
| 1204 | if (MP_STATE_VM(mp_optimise_value) != 0) { |
| 1205 | return; |
| 1206 | } |
| 1207 | |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 1208 | uint l_end = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1209 | c_if_cond(comp, pns->nodes[0], true, l_end); |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 1210 | EMIT_LOAD_GLOBAL(MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1211 | if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1212 | // assertion message |
| 1213 | compile_node(comp, pns->nodes[1]); |
Damien George | 922ddd6 | 2014-04-09 12:43:17 +0100 | [diff] [blame] | 1214 | EMIT_ARG(call_function, 1, 0, 0); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1215 | } |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1216 | EMIT_ARG(raise_varargs, 1); |
| 1217 | EMIT_ARG(label_assign, l_end); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1218 | } |
| 1219 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1220 | STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1221 | // TODO proper and/or short circuiting |
| 1222 | |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 1223 | uint l_end = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1224 | |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 1225 | // optimisation: don't emit anything when "if False" |
| 1226 | if (!node_is_const_false(pns->nodes[0])) { |
Damien George | 391db86 | 2014-10-17 17:57:33 +0000 | [diff] [blame] | 1227 | uint l_fail = comp_next_label(comp); |
| 1228 | c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1229 | |
Damien George | 391db86 | 2014-10-17 17:57:33 +0000 | [diff] [blame] | 1230 | compile_node(comp, pns->nodes[1]); // if block |
Damien George | af6edc6 | 2014-04-02 16:12:28 +0100 | [diff] [blame] | 1231 | |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 1232 | // optimisation: skip everything else when "if True" |
| 1233 | if (node_is_const_true(pns->nodes[0])) { |
Damien George | 391db86 | 2014-10-17 17:57:33 +0000 | [diff] [blame] | 1234 | goto done; |
| 1235 | } |
| 1236 | |
| 1237 | if ( |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 1238 | // optimisation: don't jump over non-existent elif/else blocks |
| 1239 | !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) |
Damien George | 391db86 | 2014-10-17 17:57:33 +0000 | [diff] [blame] | 1240 | // optimisation: don't jump if last instruction was return |
| 1241 | && !EMIT(last_emit_was_return_value) |
| 1242 | ) { |
| 1243 | // jump over elif/else blocks |
| 1244 | EMIT_ARG(jump, l_end); |
| 1245 | } |
| 1246 | |
| 1247 | EMIT_ARG(label_assign, l_fail); |
Damien George | af6edc6 | 2014-04-02 16:12:28 +0100 | [diff] [blame] | 1248 | } |
| 1249 | |
Damien George | 235f9b3 | 2014-10-17 17:30:16 +0000 | [diff] [blame] | 1250 | // compile elif blocks (if any) |
| 1251 | mp_parse_node_t *pn_elif; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 1252 | int n_elif = mp_parse_node_extract_list(&pns->nodes[2], PN_if_stmt_elif_list, &pn_elif); |
Damien George | 235f9b3 | 2014-10-17 17:30:16 +0000 | [diff] [blame] | 1253 | for (int i = 0; i < n_elif; i++) { |
| 1254 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_elif[i], PN_if_stmt_elif)); // should be |
| 1255 | mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pn_elif[i]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1256 | |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 1257 | // optimisation: don't emit anything when "if False" |
| 1258 | if (!node_is_const_false(pns_elif->nodes[0])) { |
Damien George | 391db86 | 2014-10-17 17:57:33 +0000 | [diff] [blame] | 1259 | uint l_fail = comp_next_label(comp); |
| 1260 | c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1261 | |
Damien George | 391db86 | 2014-10-17 17:57:33 +0000 | [diff] [blame] | 1262 | compile_node(comp, pns_elif->nodes[1]); // elif block |
| 1263 | |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 1264 | // optimisation: skip everything else when "elif True" |
| 1265 | if (node_is_const_true(pns_elif->nodes[0])) { |
Damien George | 391db86 | 2014-10-17 17:57:33 +0000 | [diff] [blame] | 1266 | goto done; |
| 1267 | } |
| 1268 | |
| 1269 | // optimisation: don't jump if last instruction was return |
| 1270 | if (!EMIT(last_emit_was_return_value)) { |
| 1271 | EMIT_ARG(jump, l_end); |
| 1272 | } |
| 1273 | EMIT_ARG(label_assign, l_fail); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | // compile else block |
| 1278 | compile_node(comp, pns->nodes[3]); // can be null |
| 1279 | |
Damien George | 391db86 | 2014-10-17 17:57:33 +0000 | [diff] [blame] | 1280 | done: |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1281 | EMIT_ARG(label_assign, l_end); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1282 | } |
| 1283 | |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1284 | #define START_BREAK_CONTINUE_BLOCK \ |
Damien George | 090c923 | 2014-10-17 14:08:49 +0000 | [diff] [blame] | 1285 | uint16_t old_break_label = comp->break_label; \ |
| 1286 | uint16_t old_continue_label = comp->continue_label; \ |
| 1287 | uint16_t old_break_continue_except_level = comp->break_continue_except_level; \ |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 1288 | uint break_label = comp_next_label(comp); \ |
| 1289 | uint continue_label = comp_next_label(comp); \ |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1290 | comp->break_label = break_label; \ |
| 1291 | comp->continue_label = continue_label; \ |
| 1292 | comp->break_continue_except_level = comp->cur_except_level; |
| 1293 | |
| 1294 | #define END_BREAK_CONTINUE_BLOCK \ |
| 1295 | comp->break_label = old_break_label; \ |
| 1296 | comp->continue_label = old_continue_label; \ |
Damien George | 090c923 | 2014-10-17 14:08:49 +0000 | [diff] [blame] | 1297 | comp->break_continue_except_level = old_break_continue_except_level; |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1298 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1299 | STATIC void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1300 | START_BREAK_CONTINUE_BLOCK |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1301 | |
Damien George | 391db86 | 2014-10-17 17:57:33 +0000 | [diff] [blame] | 1302 | if (!node_is_const_false(pns->nodes[0])) { // optimisation: don't emit anything for "while False" |
| 1303 | uint top_label = comp_next_label(comp); |
| 1304 | if (!node_is_const_true(pns->nodes[0])) { // optimisation: don't jump to cond for "while True" |
| 1305 | EMIT_ARG(jump, continue_label); |
| 1306 | } |
| 1307 | EMIT_ARG(label_assign, top_label); |
| 1308 | compile_node(comp, pns->nodes[1]); // body |
| 1309 | EMIT_ARG(label_assign, continue_label); |
| 1310 | c_if_cond(comp, pns->nodes[0], true, top_label); // condition |
| 1311 | } |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 1312 | |
| 1313 | // break/continue apply to outer loop (if any) in the else block |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1314 | END_BREAK_CONTINUE_BLOCK |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1315 | |
| 1316 | compile_node(comp, pns->nodes[2]); // else |
| 1317 | |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1318 | EMIT_ARG(label_assign, break_label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1319 | } |
| 1320 | |
Damien George | e181c0d | 2014-12-12 17:19:56 +0000 | [diff] [blame] | 1321 | // This function compiles an optimised for-loop of the form: |
| 1322 | // for <var> in range(<start>, <end>, <step>): |
| 1323 | // <body> |
| 1324 | // else: |
| 1325 | // <else> |
| 1326 | // <var> must be an identifier and <step> must be a small-int. |
| 1327 | // |
| 1328 | // Semantics of for-loop require: |
| 1329 | // - final failing value should not be stored in the loop variable |
| 1330 | // - if the loop never runs, the loop variable should never be assigned |
| 1331 | // - assignments to <var>, <end> or <step> in the body do not alter the loop |
| 1332 | // (<step> is a constant for us, so no need to worry about it changing) |
| 1333 | // |
| 1334 | // If <end> is a small-int, then the stack during the for-loop contains just |
| 1335 | // the current value of <var>. Otherwise, the stack contains <end> then the |
| 1336 | // current value of <var>. |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 1337 | STATIC void 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 George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1338 | START_BREAK_CONTINUE_BLOCK |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1339 | |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 1340 | uint top_label = comp_next_label(comp); |
| 1341 | uint entry_label = comp_next_label(comp); |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1342 | |
Damien George | e181c0d | 2014-12-12 17:19:56 +0000 | [diff] [blame] | 1343 | // put the end value on the stack if it's not a small-int constant |
| 1344 | bool end_on_stack = !MP_PARSE_NODE_IS_SMALL_INT(pn_end); |
| 1345 | if (end_on_stack) { |
| 1346 | compile_node(comp, pn_end); |
| 1347 | } |
| 1348 | |
| 1349 | // compile: start |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1350 | compile_node(comp, pn_start); |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1351 | |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1352 | EMIT_ARG(jump, entry_label); |
| 1353 | EMIT_ARG(label_assign, top_label); |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1354 | |
Damien George | c33ce60 | 2014-12-11 17:35:23 +0000 | [diff] [blame] | 1355 | // duplicate next value and store it to var |
| 1356 | EMIT(dup_top); |
Damien George | 3ff2d03 | 2014-03-31 18:02:22 +0100 | [diff] [blame] | 1357 | c_assign(comp, pn_var, ASSIGN_STORE); |
| 1358 | |
Damien | f3822fc | 2013-11-09 20:12:03 +0000 | [diff] [blame] | 1359 | // compile body |
| 1360 | compile_node(comp, pn_body); |
| 1361 | |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1362 | EMIT_ARG(label_assign, continue_label); |
Damien George | 600ae73 | 2014-01-21 23:48:04 +0000 | [diff] [blame] | 1363 | |
Damien George | e181c0d | 2014-12-12 17:19:56 +0000 | [diff] [blame] | 1364 | // compile: var + step |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1365 | compile_node(comp, pn_step); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 1366 | EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD); |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1367 | |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1368 | EMIT_ARG(label_assign, entry_label); |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1369 | |
Paul Sokolovsky | 899c69f | 2014-01-10 20:38:57 +0200 | [diff] [blame] | 1370 | // compile: if var <cond> end: goto top |
Damien George | e181c0d | 2014-12-12 17:19:56 +0000 | [diff] [blame] | 1371 | if (end_on_stack) { |
| 1372 | EMIT(dup_top_two); |
| 1373 | EMIT(rot_two); |
| 1374 | } else { |
| 1375 | EMIT(dup_top); |
| 1376 | compile_node(comp, pn_end); |
| 1377 | } |
Paul Sokolovsky | 56e5ef2 | 2014-02-22 16:39:45 +0200 | [diff] [blame] | 1378 | assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step)); |
| 1379 | if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 1380 | EMIT_ARG(binary_op, MP_BINARY_OP_LESS); |
Paul Sokolovsky | 899c69f | 2014-01-10 20:38:57 +0200 | [diff] [blame] | 1381 | } else { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 1382 | EMIT_ARG(binary_op, MP_BINARY_OP_MORE); |
Paul Sokolovsky | 899c69f | 2014-01-10 20:38:57 +0200 | [diff] [blame] | 1383 | } |
Damien George | 63f3832 | 2015-02-28 15:04:06 +0000 | [diff] [blame] | 1384 | EMIT_ARG(pop_jump_if, true, top_label); |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1385 | |
| 1386 | // break/continue apply to outer loop (if any) in the else block |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1387 | END_BREAK_CONTINUE_BLOCK |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1388 | |
| 1389 | compile_node(comp, pn_else); |
| 1390 | |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1391 | EMIT_ARG(label_assign, break_label); |
Damien George | e181c0d | 2014-12-12 17:19:56 +0000 | [diff] [blame] | 1392 | |
| 1393 | // discard final value of var that failed the loop condition |
| 1394 | EMIT(pop_top); |
| 1395 | |
| 1396 | // discard <end> value if it's on the stack |
| 1397 | if (end_on_stack) { |
| 1398 | EMIT(pop_top); |
| 1399 | } |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1402 | STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1403 | // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable |
| 1404 | // this is actually slower, but uses no heap memory |
| 1405 | // for viper it will be much, much faster |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 1406 | if (/*comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER &&*/ MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_atom_expr_normal)) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1407 | mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1]; |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 1408 | if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0]) |
Paul Sokolovsky | 899c69f | 2014-01-10 20:38:57 +0200 | [diff] [blame] | 1409 | && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range |
Damien George | eacbd7a | 2016-04-13 15:21:47 +0100 | [diff] [blame] | 1410 | && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1411 | mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0]; |
| 1412 | mp_parse_node_t *args; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 1413 | int n_args = mp_parse_node_extract_list(&pn_range_args, PN_arglist, &args); |
Paul Sokolovsky | 899c69f | 2014-01-10 20:38:57 +0200 | [diff] [blame] | 1414 | mp_parse_node_t pn_range_start; |
| 1415 | mp_parse_node_t pn_range_end; |
| 1416 | mp_parse_node_t pn_range_step; |
| 1417 | bool optimize = false; |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1418 | if (1 <= n_args && n_args <= 3) { |
Paul Sokolovsky | 899c69f | 2014-01-10 20:38:57 +0200 | [diff] [blame] | 1419 | optimize = true; |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1420 | if (n_args == 1) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1421 | pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0); |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1422 | pn_range_end = args[0]; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1423 | pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1); |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1424 | } else if (n_args == 2) { |
| 1425 | pn_range_start = args[0]; |
| 1426 | pn_range_end = args[1]; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1427 | pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1); |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1428 | } else { |
| 1429 | pn_range_start = args[0]; |
| 1430 | pn_range_end = args[1]; |
| 1431 | pn_range_step = args[2]; |
Paul Sokolovsky | 899c69f | 2014-01-10 20:38:57 +0200 | [diff] [blame] | 1432 | // We need to know sign of step. This is possible only if it's constant |
| 1433 | if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) { |
| 1434 | optimize = false; |
| 1435 | } |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1436 | } |
Damien George | 33ac0fd | 2015-12-08 21:05:14 +0000 | [diff] [blame] | 1437 | // arguments must be able to be compiled as standard expressions |
| 1438 | if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_start)) { |
| 1439 | int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_range_start); |
| 1440 | if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) { |
| 1441 | optimize = false; |
| 1442 | } |
| 1443 | } |
| 1444 | if (optimize && MP_PARSE_NODE_IS_STRUCT(pn_range_end)) { |
| 1445 | int k = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn_range_end); |
| 1446 | if (k == PN_arglist_star || k == PN_arglist_dbl_star || k == PN_argument) { |
| 1447 | optimize = false; |
| 1448 | } |
| 1449 | } |
Paul Sokolovsky | 899c69f | 2014-01-10 20:38:57 +0200 | [diff] [blame] | 1450 | } |
| 1451 | if (optimize) { |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1452 | compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]); |
| 1453 | return; |
| 1454 | } |
| 1455 | } |
| 1456 | } |
Damien | f72fd0e | 2013-11-06 20:20:49 +0000 | [diff] [blame] | 1457 | |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1458 | START_BREAK_CONTINUE_BLOCK |
Damien George | 25c8464 | 2014-05-30 15:20:41 +0100 | [diff] [blame] | 1459 | comp->break_label |= MP_EMIT_BREAK_FROM_FOR; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1460 | |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 1461 | uint pop_label = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1462 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1463 | compile_node(comp, pns->nodes[1]); // iterator |
| 1464 | EMIT(get_iter); |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1465 | EMIT_ARG(label_assign, continue_label); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1466 | EMIT_ARG(for_iter, pop_label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1467 | c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable |
| 1468 | compile_node(comp, pns->nodes[2]); // body |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 1469 | if (!EMIT(last_emit_was_return_value)) { |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1470 | EMIT_ARG(jump, continue_label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1471 | } |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1472 | EMIT_ARG(label_assign, pop_label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1473 | EMIT(for_iter_end); |
| 1474 | |
| 1475 | // break/continue apply to outer loop (if any) in the else block |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1476 | END_BREAK_CONTINUE_BLOCK |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1477 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1478 | compile_node(comp, pns->nodes[3]); // else (not tested) |
| 1479 | |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1480 | EMIT_ARG(label_assign, break_label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1481 | } |
| 1482 | |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 1483 | STATIC void 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) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1484 | // setup code |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 1485 | uint l1 = comp_next_label(comp); |
| 1486 | uint success_label = comp_next_label(comp); |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1487 | |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1488 | EMIT_ARG(setup_except, l1); |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 1489 | compile_increase_except_level(comp); |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1490 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1491 | compile_node(comp, pn_body); // body |
| 1492 | EMIT(pop_block); |
Damien George | 069a35e | 2014-04-10 17:22:19 +0000 | [diff] [blame] | 1493 | EMIT_ARG(jump, success_label); // jump over exception handler |
| 1494 | |
| 1495 | EMIT_ARG(label_assign, l1); // start of exception handler |
Damien George | b601d95 | 2014-06-30 05:17:25 +0100 | [diff] [blame] | 1496 | EMIT(start_except_handler); |
Damien George | 069a35e | 2014-04-10 17:22:19 +0000 | [diff] [blame] | 1497 | |
Damien George | f040685 | 2016-09-27 12:37:21 +1000 | [diff] [blame] | 1498 | // at this point the top of the stack contains the exception instance that was raised |
| 1499 | |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 1500 | uint l2 = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1501 | |
| 1502 | for (int i = 0; i < n_except; i++) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1503 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be |
| 1504 | mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1505 | |
| 1506 | qstr qstr_exception_local = 0; |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 1507 | uint end_finally_label = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1508 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1509 | if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1510 | // this is a catch all exception handler |
| 1511 | if (i + 1 != n_except) { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 1512 | compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last"); |
Damien George | c935d69 | 2015-01-13 23:33:16 +0000 | [diff] [blame] | 1513 | compile_decrease_except_level(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1514 | return; |
| 1515 | } |
| 1516 | } else { |
| 1517 | // this exception handler requires a match to a certain type of exception |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1518 | mp_parse_node_t pns_exception_expr = pns_except->nodes[0]; |
| 1519 | if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) { |
| 1520 | mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr; |
| 1521 | if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1522 | // handler binds the exception to a local |
| 1523 | pns_exception_expr = pns3->nodes[0]; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1524 | qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1525 | } |
| 1526 | } |
| 1527 | EMIT(dup_top); |
| 1528 | compile_node(comp, pns_exception_expr); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 1529 | EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH); |
Damien George | 63f3832 | 2015-02-28 15:04:06 +0000 | [diff] [blame] | 1530 | EMIT_ARG(pop_jump_if, false, end_finally_label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1531 | } |
| 1532 | |
Damien George | f040685 | 2016-09-27 12:37:21 +1000 | [diff] [blame] | 1533 | // either discard or store the exception instance |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1534 | if (qstr_exception_local == 0) { |
| 1535 | EMIT(pop_top); |
| 1536 | } else { |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 1537 | compile_store_id(comp, qstr_exception_local); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1538 | } |
| 1539 | |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 1540 | uint l3 = 0; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1541 | if (qstr_exception_local != 0) { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1542 | l3 = comp_next_label(comp); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1543 | EMIT_ARG(setup_finally, l3); |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 1544 | compile_increase_except_level(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1545 | } |
| 1546 | compile_node(comp, pns_except->nodes[1]); |
| 1547 | if (qstr_exception_local != 0) { |
| 1548 | EMIT(pop_block); |
| 1549 | } |
| 1550 | EMIT(pop_except); |
| 1551 | if (qstr_exception_local != 0) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1552 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
| 1553 | EMIT_ARG(label_assign, l3); |
| 1554 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 1555 | compile_store_id(comp, qstr_exception_local); |
| 1556 | compile_delete_id(comp, qstr_exception_local); |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1557 | |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 1558 | compile_decrease_except_level(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1559 | EMIT(end_finally); |
| 1560 | } |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1561 | EMIT_ARG(jump, l2); |
| 1562 | EMIT_ARG(label_assign, end_finally_label); |
Damien George | f040685 | 2016-09-27 12:37:21 +1000 | [diff] [blame] | 1563 | EMIT_ARG(adjust_stack_size, 1); // stack adjust for the exception instance |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1564 | } |
| 1565 | |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 1566 | compile_decrease_except_level(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1567 | EMIT(end_finally); |
Damien George | b601d95 | 2014-06-30 05:17:25 +0100 | [diff] [blame] | 1568 | EMIT(end_except_handler); |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1569 | |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1570 | EMIT_ARG(label_assign, success_label); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1571 | compile_node(comp, pn_else); // else block, can be null |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1572 | EMIT_ARG(label_assign, l2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1573 | } |
| 1574 | |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 1575 | STATIC void 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) { |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 1576 | uint l_finally_block = comp_next_label(comp); |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1577 | |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1578 | EMIT_ARG(setup_finally, l_finally_block); |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 1579 | compile_increase_except_level(comp); |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1580 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1581 | if (n_except == 0) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1582 | assert(MP_PARSE_NODE_IS_NULL(pn_else)); |
Damien George | d66ae18 | 2014-04-10 17:28:54 +0000 | [diff] [blame] | 1583 | EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1584 | compile_node(comp, pn_body); |
Damien George | d66ae18 | 2014-04-10 17:28:54 +0000 | [diff] [blame] | 1585 | EMIT_ARG(adjust_stack_size, -3); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1586 | } else { |
| 1587 | compile_try_except(comp, pn_body, n_except, pn_except, pn_else); |
| 1588 | } |
| 1589 | EMIT(pop_block); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1590 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
| 1591 | EMIT_ARG(label_assign, l_finally_block); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1592 | compile_node(comp, pn_finally); |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 1593 | |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 1594 | compile_decrease_except_level(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1595 | EMIT(end_finally); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1596 | } |
| 1597 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1598 | STATIC void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 1599 | assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should be |
| 1600 | { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1601 | mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1]; |
| 1602 | if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1603 | // just try-finally |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1604 | compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]); |
| 1605 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1606 | // try-except and possibly else and/or finally |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1607 | mp_parse_node_t *pn_excepts; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 1608 | int n_except = mp_parse_node_extract_list(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1609 | if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1610 | // no finally |
| 1611 | compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]); |
| 1612 | } else { |
| 1613 | // have finally |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1614 | compile_try_finally(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1], ((mp_parse_node_struct_t*)pns2->nodes[2])->nodes[0]); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1615 | } |
| 1616 | } else { |
| 1617 | // just try-except |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1618 | mp_parse_node_t *pn_excepts; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 1619 | int n_except = mp_parse_node_extract_list(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1620 | compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1621 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1622 | } |
| 1623 | } |
| 1624 | |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 1625 | STATIC void compile_with_stmt_helper(compiler_t *comp, int n, mp_parse_node_t *nodes, mp_parse_node_t body) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1626 | if (n == 0) { |
| 1627 | // no more pre-bits, compile the body of the with |
| 1628 | compile_node(comp, body); |
| 1629 | } else { |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 1630 | uint l_end = comp_next_label(comp); |
Damien George | 2c915e1 | 2016-04-07 08:53:24 +0100 | [diff] [blame] | 1631 | if (MICROPY_EMIT_NATIVE && comp->scope_cur->emit_options != MP_EMIT_OPT_BYTECODE) { |
| 1632 | // we need to allocate an extra label for the native emitter |
| 1633 | // it will use l_end+1 as an auxiliary label |
| 1634 | comp_next_label(comp); |
| 1635 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1636 | if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1637 | // this pre-bit is of the form "a as b" |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1638 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1639 | compile_node(comp, pns->nodes[0]); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1640 | EMIT_ARG(setup_with, l_end); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1641 | c_assign(comp, pns->nodes[1], ASSIGN_STORE); |
| 1642 | } else { |
| 1643 | // this pre-bit is just an expression |
| 1644 | compile_node(comp, nodes[0]); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1645 | EMIT_ARG(setup_with, l_end); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1646 | EMIT(pop_top); |
| 1647 | } |
Paul Sokolovsky | 44307d5 | 2014-03-29 04:10:11 +0200 | [diff] [blame] | 1648 | compile_increase_except_level(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1649 | // compile additional pre-bits and the body |
| 1650 | compile_with_stmt_helper(comp, n - 1, nodes + 1, body); |
| 1651 | // finish this with block |
Damien George | ce8b4e8 | 2016-04-07 08:50:38 +0100 | [diff] [blame] | 1652 | EMIT_ARG(with_cleanup, l_end); |
Paul Sokolovsky | 44307d5 | 2014-03-29 04:10:11 +0200 | [diff] [blame] | 1653 | compile_decrease_except_level(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1654 | EMIT(end_finally); |
| 1655 | } |
| 1656 | } |
| 1657 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1658 | STATIC void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1659 | // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit) |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1660 | mp_parse_node_t *nodes; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 1661 | int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1662 | assert(n > 0); |
| 1663 | |
| 1664 | // compile in a nested fashion |
| 1665 | compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]); |
| 1666 | } |
| 1667 | |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 1668 | STATIC void compile_yield_from(compiler_t *comp) { |
| 1669 | EMIT(get_iter); |
| 1670 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
| 1671 | EMIT(yield_from); |
| 1672 | } |
| 1673 | |
| 1674 | #if MICROPY_PY_ASYNC_AWAIT |
| 1675 | STATIC void compile_await_object_method(compiler_t *comp, qstr method) { |
| 1676 | EMIT_ARG(load_method, method); |
| 1677 | EMIT_ARG(call_method, 0, 0, 0); |
| 1678 | compile_yield_from(comp); |
| 1679 | } |
| 1680 | |
| 1681 | STATIC void compile_async_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
| 1682 | // comp->break_label |= MP_EMIT_BREAK_FROM_FOR; |
| 1683 | |
| 1684 | qstr context = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]); |
| 1685 | uint while_else_label = comp_next_label(comp); |
| 1686 | uint try_exception_label = comp_next_label(comp); |
| 1687 | uint try_else_label = comp_next_label(comp); |
| 1688 | uint try_finally_label = comp_next_label(comp); |
| 1689 | |
| 1690 | compile_node(comp, pns->nodes[1]); // iterator |
| 1691 | compile_await_object_method(comp, MP_QSTR___aiter__); |
| 1692 | compile_store_id(comp, context); |
| 1693 | |
| 1694 | START_BREAK_CONTINUE_BLOCK |
| 1695 | |
| 1696 | EMIT_ARG(label_assign, continue_label); |
| 1697 | |
| 1698 | EMIT_ARG(setup_except, try_exception_label); |
| 1699 | compile_increase_except_level(comp); |
| 1700 | |
| 1701 | compile_load_id(comp, context); |
| 1702 | compile_await_object_method(comp, MP_QSTR___anext__); |
| 1703 | c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable |
| 1704 | EMIT(pop_block); |
| 1705 | EMIT_ARG(jump, try_else_label); |
| 1706 | |
| 1707 | EMIT_ARG(label_assign, try_exception_label); |
| 1708 | EMIT(start_except_handler); |
| 1709 | EMIT(dup_top); |
| 1710 | EMIT_LOAD_GLOBAL(MP_QSTR_StopAsyncIteration); |
| 1711 | EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH); |
| 1712 | EMIT_ARG(pop_jump_if, false, try_finally_label); |
Damien George | b32c01b | 2016-09-28 11:52:13 +1000 | [diff] [blame] | 1713 | EMIT(pop_top); // pop exception instance |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 1714 | EMIT(pop_except); |
| 1715 | EMIT_ARG(jump, while_else_label); |
| 1716 | |
| 1717 | EMIT_ARG(label_assign, try_finally_label); |
Damien George | b32c01b | 2016-09-28 11:52:13 +1000 | [diff] [blame] | 1718 | EMIT_ARG(adjust_stack_size, 1); // if we jump here, the exc is on the stack |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 1719 | compile_decrease_except_level(comp); |
| 1720 | EMIT(end_finally); |
| 1721 | EMIT(end_except_handler); |
| 1722 | |
| 1723 | EMIT_ARG(label_assign, try_else_label); |
| 1724 | compile_node(comp, pns->nodes[2]); // body |
| 1725 | |
| 1726 | EMIT_ARG(jump, continue_label); |
| 1727 | // break/continue apply to outer loop (if any) in the else block |
| 1728 | END_BREAK_CONTINUE_BLOCK |
| 1729 | |
| 1730 | EMIT_ARG(label_assign, while_else_label); |
| 1731 | compile_node(comp, pns->nodes[3]); // else |
| 1732 | |
| 1733 | EMIT_ARG(label_assign, break_label); |
| 1734 | } |
| 1735 | |
| 1736 | STATIC void compile_async_with_stmt_helper(compiler_t *comp, int n, mp_parse_node_t *nodes, mp_parse_node_t body) { |
| 1737 | if (n == 0) { |
| 1738 | // no more pre-bits, compile the body of the with |
| 1739 | compile_node(comp, body); |
| 1740 | } else { |
| 1741 | uint try_exception_label = comp_next_label(comp); |
| 1742 | uint no_reraise_label = comp_next_label(comp); |
| 1743 | uint try_else_label = comp_next_label(comp); |
| 1744 | uint end_label = comp_next_label(comp); |
| 1745 | qstr context; |
| 1746 | |
| 1747 | if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) { |
| 1748 | // this pre-bit is of the form "a as b" |
| 1749 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0]; |
| 1750 | compile_node(comp, pns->nodes[0]); |
| 1751 | context = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
| 1752 | compile_store_id(comp, context); |
| 1753 | compile_load_id(comp, context); |
| 1754 | compile_await_object_method(comp, MP_QSTR___aenter__); |
| 1755 | c_assign(comp, pns->nodes[1], ASSIGN_STORE); |
| 1756 | } else { |
| 1757 | // this pre-bit is just an expression |
| 1758 | compile_node(comp, nodes[0]); |
| 1759 | context = MP_PARSE_NODE_LEAF_ARG(nodes[0]); |
| 1760 | compile_store_id(comp, context); |
| 1761 | compile_load_id(comp, context); |
| 1762 | compile_await_object_method(comp, MP_QSTR___aenter__); |
| 1763 | EMIT(pop_top); |
| 1764 | } |
| 1765 | |
| 1766 | compile_load_id(comp, context); |
| 1767 | EMIT_ARG(load_method, MP_QSTR___aexit__); |
| 1768 | |
| 1769 | EMIT_ARG(setup_except, try_exception_label); |
| 1770 | compile_increase_except_level(comp); |
| 1771 | // compile additional pre-bits and the body |
| 1772 | compile_async_with_stmt_helper(comp, n - 1, nodes + 1, body); |
| 1773 | // finish this with block |
| 1774 | EMIT(pop_block); |
| 1775 | EMIT_ARG(jump, try_else_label); // jump over exception handler |
| 1776 | |
| 1777 | EMIT_ARG(label_assign, try_exception_label); // start of exception handler |
| 1778 | EMIT(start_except_handler); |
Damien George | b32c01b | 2016-09-28 11:52:13 +1000 | [diff] [blame] | 1779 | |
| 1780 | // at this point the stack contains: ..., __aexit__, self, exc |
| 1781 | EMIT(dup_top); |
| 1782 | #if MICROPY_CPYTHON_COMPAT |
| 1783 | EMIT_ARG(load_attr, MP_QSTR___class__); // get type(exc) |
| 1784 | #else |
| 1785 | compile_load_id(comp, MP_QSTR_type); |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 1786 | EMIT(rot_two); |
Damien George | b32c01b | 2016-09-28 11:52:13 +1000 | [diff] [blame] | 1787 | EMIT_ARG(call_function, 1, 0, 0); // get type(exc) |
| 1788 | #endif |
| 1789 | EMIT(rot_two); |
| 1790 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // dummy traceback value |
| 1791 | // at this point the stack contains: ..., __aexit__, self, type(exc), exc, None |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 1792 | EMIT_ARG(call_method, 3, 0, 0); |
Damien George | b32c01b | 2016-09-28 11:52:13 +1000 | [diff] [blame] | 1793 | |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 1794 | compile_yield_from(comp); |
| 1795 | EMIT_ARG(pop_jump_if, true, no_reraise_label); |
| 1796 | EMIT_ARG(raise_varargs, 0); |
| 1797 | |
| 1798 | EMIT_ARG(label_assign, no_reraise_label); |
| 1799 | EMIT(pop_except); |
| 1800 | EMIT_ARG(jump, end_label); |
| 1801 | |
Damien George | b32c01b | 2016-09-28 11:52:13 +1000 | [diff] [blame] | 1802 | EMIT_ARG(adjust_stack_size, 3); // adjust for __aexit__, self, exc |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 1803 | compile_decrease_except_level(comp); |
| 1804 | EMIT(end_finally); |
| 1805 | EMIT(end_except_handler); |
| 1806 | |
| 1807 | EMIT_ARG(label_assign, try_else_label); // start of try-else handler |
| 1808 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
| 1809 | EMIT(dup_top); |
| 1810 | EMIT(dup_top); |
| 1811 | EMIT_ARG(call_method, 3, 0, 0); |
| 1812 | compile_yield_from(comp); |
| 1813 | EMIT(pop_top); |
| 1814 | |
| 1815 | EMIT_ARG(label_assign, end_label); |
| 1816 | |
| 1817 | } |
| 1818 | } |
| 1819 | |
| 1820 | STATIC void compile_async_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
| 1821 | // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit) |
| 1822 | mp_parse_node_t *nodes; |
| 1823 | int n = mp_parse_node_extract_list(&pns->nodes[0], PN_with_stmt_list, &nodes); |
| 1824 | assert(n > 0); |
| 1825 | |
| 1826 | // compile in a nested fashion |
| 1827 | compile_async_with_stmt_helper(comp, n, nodes, pns->nodes[1]); |
| 1828 | } |
| 1829 | |
| 1830 | STATIC void compile_async_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
| 1831 | assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[0])); |
| 1832 | mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0]; |
| 1833 | if (MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_funcdef) { |
| 1834 | // async def |
| 1835 | compile_funcdef(comp, pns0); |
| 1836 | scope_t *fscope = (scope_t*)pns0->nodes[4]; |
| 1837 | fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR; |
| 1838 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_for_stmt) { |
| 1839 | // async for |
| 1840 | compile_async_for_stmt(comp, pns0); |
| 1841 | } else { |
| 1842 | // async with |
| 1843 | assert(MP_PARSE_NODE_STRUCT_KIND(pns0) == PN_with_stmt); |
| 1844 | compile_async_with_stmt(comp, pns0); |
| 1845 | } |
| 1846 | } |
| 1847 | #endif |
| 1848 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1849 | STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1850 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 1851 | if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) { |
| 1852 | // for REPL, evaluate then print the expression |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 1853 | compile_load_id(comp, MP_QSTR___repl_print__); |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 1854 | compile_node(comp, pns->nodes[0]); |
Damien George | 922ddd6 | 2014-04-09 12:43:17 +0100 | [diff] [blame] | 1855 | EMIT_ARG(call_function, 1, 0, 0); |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 1856 | EMIT(pop_top); |
| 1857 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1858 | } else { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 1859 | // for non-REPL, evaluate then discard the expression |
Damien George | 5042bce | 2014-05-25 22:06:06 +0100 | [diff] [blame] | 1860 | if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0])) |
Damien George | 4c81ba8 | 2015-01-13 16:21:23 +0000 | [diff] [blame] | 1861 | || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string) |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 1862 | || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes) |
| 1863 | || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 1864 | // do nothing with a lonely constant |
| 1865 | } else { |
| 1866 | compile_node(comp, pns->nodes[0]); // just an expression |
| 1867 | EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack |
| 1868 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1869 | } |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1870 | } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1871 | mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1]; |
| 1872 | int kind = MP_PARSE_NODE_STRUCT_KIND(pns1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1873 | if (kind == PN_expr_stmt_augassign) { |
| 1874 | c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign |
| 1875 | compile_node(comp, pns1->nodes[1]); // rhs |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1876 | assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0])); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 1877 | mp_binary_op_t op; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1878 | switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 1879 | case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break; |
| 1880 | case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break; |
| 1881 | case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break; |
| 1882 | case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break; |
| 1883 | case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break; |
| 1884 | case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break; |
| 1885 | case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break; |
| 1886 | case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break; |
| 1887 | case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break; |
| 1888 | case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break; |
| 1889 | case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break; |
Damien George | d2d64f0 | 2015-01-14 21:32:42 +0000 | [diff] [blame] | 1890 | case MP_TOKEN_DEL_DBL_STAR_EQUAL: default: op = MP_BINARY_OP_INPLACE_POWER; break; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1891 | } |
Damien George | 7e5fb24 | 2014-02-01 22:18:47 +0000 | [diff] [blame] | 1892 | EMIT_ARG(binary_op, op); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1893 | c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign |
| 1894 | } else if (kind == PN_expr_stmt_assign_list) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1895 | int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1; |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1896 | compile_node(comp, pns1->nodes[rhs]); // rhs |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1897 | // following CPython, we store left-most first |
| 1898 | if (rhs > 0) { |
| 1899 | EMIT(dup_top); |
| 1900 | } |
| 1901 | c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store |
| 1902 | for (int i = 0; i < rhs; i++) { |
| 1903 | if (i + 1 < rhs) { |
| 1904 | EMIT(dup_top); |
| 1905 | } |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1906 | c_assign(comp, pns1->nodes[i], ASSIGN_STORE); // middle store |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1907 | } |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 1908 | } else { |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1909 | plain_assign: |
Damien George | 42e0c59 | 2015-03-14 13:11:35 +0000 | [diff] [blame] | 1910 | if (MICROPY_COMP_DOUBLE_TUPLE_ASSIGN |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1911 | && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr) |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1912 | && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr) |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1913 | && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[1]) == 2 |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1914 | && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) { |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 1915 | // optimisation for a, b = c, d |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1916 | mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns->nodes[1]; |
Damien George | 4dea922 | 2015-04-09 15:29:54 +0000 | [diff] [blame] | 1917 | mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0]; |
Damien George | 495d781 | 2014-04-08 17:51:47 +0100 | [diff] [blame] | 1918 | if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr) |
| 1919 | || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) { |
| 1920 | // can't optimise when it's a star expression on the lhs |
| 1921 | goto no_optimisation; |
| 1922 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1923 | compile_node(comp, pns10->nodes[0]); // rhs |
| 1924 | compile_node(comp, pns10->nodes[1]); // rhs |
| 1925 | EMIT(rot_two); |
| 1926 | c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store |
| 1927 | c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store |
Damien George | 42e0c59 | 2015-03-14 13:11:35 +0000 | [diff] [blame] | 1928 | } else if (MICROPY_COMP_TRIPLE_TUPLE_ASSIGN |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1929 | && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr) |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1930 | && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr) |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1931 | && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[1]) == 3 |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1932 | && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) { |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 1933 | // optimisation for a, b, c = d, e, f |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1934 | mp_parse_node_struct_t *pns10 = (mp_parse_node_struct_t*)pns->nodes[1]; |
Damien George | 4dea922 | 2015-04-09 15:29:54 +0000 | [diff] [blame] | 1935 | mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns->nodes[0]; |
Damien George | 495d781 | 2014-04-08 17:51:47 +0100 | [diff] [blame] | 1936 | if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr) |
| 1937 | || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr) |
| 1938 | || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) { |
| 1939 | // can't optimise when it's a star expression on the lhs |
| 1940 | goto no_optimisation; |
| 1941 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1942 | compile_node(comp, pns10->nodes[0]); // rhs |
| 1943 | compile_node(comp, pns10->nodes[1]); // rhs |
| 1944 | compile_node(comp, pns10->nodes[2]); // rhs |
| 1945 | EMIT(rot_three); |
| 1946 | EMIT(rot_two); |
| 1947 | c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store |
| 1948 | c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store |
| 1949 | c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store |
| 1950 | } else { |
Damien George | 495d781 | 2014-04-08 17:51:47 +0100 | [diff] [blame] | 1951 | no_optimisation: |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1952 | compile_node(comp, pns->nodes[1]); // rhs |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1953 | c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store |
| 1954 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1955 | } |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 1956 | } else { |
| 1957 | goto plain_assign; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1958 | } |
| 1959 | } |
| 1960 | |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 1961 | STATIC void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1962 | int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1963 | compile_node(comp, pns->nodes[0]); |
| 1964 | for (int i = 1; i < num_nodes; i += 1) { |
| 1965 | compile_node(comp, pns->nodes[i]); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1966 | EMIT_ARG(binary_op, binary_op); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1967 | } |
| 1968 | } |
| 1969 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1970 | STATIC void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1971 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else)); |
| 1972 | mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1973 | |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 1974 | uint l_fail = comp_next_label(comp); |
| 1975 | uint l_end = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1976 | c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition |
| 1977 | compile_node(comp, pns->nodes[0]); // success value |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1978 | EMIT_ARG(jump, l_end); |
| 1979 | EMIT_ARG(label_assign, l_fail); |
Damien George | d66ae18 | 2014-04-10 17:28:54 +0000 | [diff] [blame] | 1980 | EMIT_ARG(adjust_stack_size, -1); // adjust stack size |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1981 | compile_node(comp, pns_test_if_else->nodes[1]); // failure value |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 1982 | EMIT_ARG(label_assign, l_end); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1983 | } |
| 1984 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 1985 | STATIC void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 1986 | if (comp->pass == MP_PASS_SCOPE) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1987 | // create a new scope for this lambda |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1988 | scope_t *s = scope_new_and_link(comp, SCOPE_LAMBDA, (mp_parse_node_t)pns, comp->scope_cur->emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1989 | // store the lambda scope so the compiling function (this one) can use it at each pass |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1990 | pns->nodes[2] = (mp_parse_node_t)s; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1991 | } |
| 1992 | |
| 1993 | // get the scope for this lambda |
| 1994 | scope_t *this_scope = (scope_t*)pns->nodes[2]; |
| 1995 | |
Damien George | 2c83894 | 2015-11-17 14:00:14 +0000 | [diff] [blame] | 1996 | // compile the lambda definition |
| 1997 | compile_funcdef_lambdef(comp, this_scope, pns->nodes[0], PN_varargslist); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1998 | } |
| 1999 | |
Damien George | 7711afb | 2015-02-28 15:10:18 +0000 | [diff] [blame] | 2000 | STATIC void compile_or_and_test(compiler_t *comp, mp_parse_node_struct_t *pns, bool cond) { |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 2001 | uint l_end = comp_next_label(comp); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2002 | int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2003 | for (int i = 0; i < n; i += 1) { |
| 2004 | compile_node(comp, pns->nodes[i]); |
| 2005 | if (i + 1 < n) { |
Damien George | 7711afb | 2015-02-28 15:10:18 +0000 | [diff] [blame] | 2006 | EMIT_ARG(jump_if_or_pop, cond, l_end); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2007 | } |
| 2008 | } |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2009 | EMIT_ARG(label_assign, l_end); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2010 | } |
| 2011 | |
Damien George | 7711afb | 2015-02-28 15:10:18 +0000 | [diff] [blame] | 2012 | STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) { |
| 2013 | compile_or_and_test(comp, pns, true); |
| 2014 | } |
| 2015 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2016 | STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | 7711afb | 2015-02-28 15:10:18 +0000 | [diff] [blame] | 2017 | compile_or_and_test(comp, pns, false); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2018 | } |
| 2019 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2020 | STATIC void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2021 | compile_node(comp, pns->nodes[0]); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2022 | EMIT_ARG(unary_op, MP_UNARY_OP_NOT); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2023 | } |
| 2024 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2025 | STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2026 | int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2027 | compile_node(comp, pns->nodes[0]); |
| 2028 | bool multi = (num_nodes > 3); |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 2029 | uint l_fail = 0; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2030 | if (multi) { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 2031 | l_fail = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2032 | } |
| 2033 | for (int i = 1; i + 1 < num_nodes; i += 2) { |
| 2034 | compile_node(comp, pns->nodes[i + 1]); |
| 2035 | if (i + 2 < num_nodes) { |
| 2036 | EMIT(dup_top); |
| 2037 | EMIT(rot_three); |
| 2038 | } |
Damien George | 7e5fb24 | 2014-02-01 22:18:47 +0000 | [diff] [blame] | 2039 | if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2040 | mp_binary_op_t op; |
Damien George | 7e5fb24 | 2014-02-01 22:18:47 +0000 | [diff] [blame] | 2041 | switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2042 | case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break; |
| 2043 | case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break; |
| 2044 | case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break; |
| 2045 | case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break; |
| 2046 | case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break; |
| 2047 | case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break; |
Damien George | d2d64f0 | 2015-01-14 21:32:42 +0000 | [diff] [blame] | 2048 | case MP_TOKEN_KW_IN: default: op = MP_BINARY_OP_IN; break; |
Damien George | 7e5fb24 | 2014-02-01 22:18:47 +0000 | [diff] [blame] | 2049 | } |
| 2050 | EMIT_ARG(binary_op, op); |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 2051 | } else { |
| 2052 | assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); // should be |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2053 | mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i]; |
| 2054 | int kind = MP_PARSE_NODE_STRUCT_KIND(pns2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2055 | if (kind == PN_comp_op_not_in) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2056 | EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN); |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 2057 | } else { |
| 2058 | assert(kind == PN_comp_op_is); // should be |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2059 | if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2060 | EMIT_ARG(binary_op, MP_BINARY_OP_IS); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2061 | } else { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2062 | EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2063 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2064 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2065 | } |
| 2066 | if (i + 2 < num_nodes) { |
Damien George | 63f3832 | 2015-02-28 15:04:06 +0000 | [diff] [blame] | 2067 | EMIT_ARG(jump_if_or_pop, false, l_fail); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2068 | } |
| 2069 | } |
| 2070 | if (multi) { |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 2071 | uint l_end = comp_next_label(comp); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2072 | EMIT_ARG(jump, l_end); |
| 2073 | EMIT_ARG(label_assign, l_fail); |
Damien George | d66ae18 | 2014-04-10 17:28:54 +0000 | [diff] [blame] | 2074 | EMIT_ARG(adjust_stack_size, 1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2075 | EMIT(rot_two); |
| 2076 | EMIT(pop_top); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2077 | EMIT_ARG(label_assign, l_end); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2078 | } |
| 2079 | } |
| 2080 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2081 | STATIC void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | 9d181f6 | 2014-04-27 16:55:27 +0100 | [diff] [blame] | 2082 | compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2083 | } |
| 2084 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2085 | STATIC void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2086 | c_binary_op(comp, pns, MP_BINARY_OP_OR); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2087 | } |
| 2088 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2089 | STATIC void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2090 | c_binary_op(comp, pns, MP_BINARY_OP_XOR); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2091 | } |
| 2092 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2093 | STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2094 | c_binary_op(comp, pns, MP_BINARY_OP_AND); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2095 | } |
| 2096 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2097 | STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2098 | int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2099 | compile_node(comp, pns->nodes[0]); |
| 2100 | for (int i = 1; i + 1 < num_nodes; i += 2) { |
| 2101 | compile_node(comp, pns->nodes[i + 1]); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2102 | if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2103 | EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2104 | } else { |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 2105 | assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be |
| 2106 | EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2107 | } |
| 2108 | } |
| 2109 | } |
| 2110 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2111 | STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2112 | int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2113 | compile_node(comp, pns->nodes[0]); |
| 2114 | for (int i = 1; i + 1 < num_nodes; i += 2) { |
| 2115 | compile_node(comp, pns->nodes[i + 1]); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2116 | if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2117 | EMIT_ARG(binary_op, MP_BINARY_OP_ADD); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2118 | } else { |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 2119 | assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be |
| 2120 | EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2121 | } |
| 2122 | } |
| 2123 | } |
| 2124 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2125 | STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2126 | int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2127 | compile_node(comp, pns->nodes[0]); |
| 2128 | for (int i = 1; i + 1 < num_nodes; i += 2) { |
| 2129 | compile_node(comp, pns->nodes[i + 1]); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2130 | if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2131 | EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2132 | } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2133 | EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2134 | } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2135 | EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2136 | } else { |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 2137 | assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be |
| 2138 | EMIT_ARG(binary_op, MP_BINARY_OP_MODULO); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2139 | } |
| 2140 | } |
| 2141 | } |
| 2142 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2143 | STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2144 | compile_node(comp, pns->nodes[1]); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2145 | if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2146 | EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2147 | } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 2148 | EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2149 | } else { |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 2150 | assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be |
| 2151 | EMIT_ARG(unary_op, MP_UNARY_OP_INVERT); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2152 | } |
| 2153 | } |
| 2154 | |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 2155 | STATIC void compile_atom_expr_normal(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | 35e2a4e | 2014-02-05 00:51:47 +0000 | [diff] [blame] | 2156 | // this is to handle special super() call |
| 2157 | comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super; |
| 2158 | |
| 2159 | compile_generic_all_nodes(comp, pns); |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 2160 | } |
Damien George | 3acaa28 | 2016-03-16 13:04:51 +0000 | [diff] [blame] | 2161 | |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 2162 | STATIC void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) { |
| 2163 | compile_generic_all_nodes(comp, pns); // 2 nodes, arguments of power |
| 2164 | EMIT_ARG(binary_op, MP_BINARY_OP_POWER); |
Damien George | 35e2a4e | 2014-02-05 00:51:47 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 2167 | STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2168 | // function to call is on top of stack |
| 2169 | |
Damien George | 35e2a4e | 2014-02-05 00:51:47 +0000 | [diff] [blame] | 2170 | // this is to handle special super() call |
Damien George | bbcd49a | 2014-02-06 20:30:16 +0000 | [diff] [blame] | 2171 | if (MP_PARSE_NODE_IS_NULL(pn_arglist) && comp->func_arg_is_super && comp->scope_cur->kind == SCOPE_FUNCTION) { |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 2172 | compile_load_id(comp, MP_QSTR___class__); |
Damien George | 01039b5 | 2014-12-21 17:44:27 +0000 | [diff] [blame] | 2173 | // look for first argument to function (assumes it's "self") |
Damien George | 35e2a4e | 2014-02-05 00:51:47 +0000 | [diff] [blame] | 2174 | for (int i = 0; i < comp->scope_cur->id_info_len; i++) { |
Damien George | 2bf7c09 | 2014-04-09 15:26:46 +0100 | [diff] [blame] | 2175 | if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) { |
Damien George | 01039b5 | 2014-12-21 17:44:27 +0000 | [diff] [blame] | 2176 | // first argument found; load it and call super |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 2177 | EMIT_LOAD_FAST(MP_QSTR_, comp->scope_cur->id_info[i].local_num); |
Damien George | 01039b5 | 2014-12-21 17:44:27 +0000 | [diff] [blame] | 2178 | EMIT_ARG(call_function, 2, 0, 0); |
| 2179 | return; |
Damien George | 35e2a4e | 2014-02-05 00:51:47 +0000 | [diff] [blame] | 2180 | } |
| 2181 | } |
Damien George | 01039b5 | 2014-12-21 17:44:27 +0000 | [diff] [blame] | 2182 | compile_syntax_error(comp, MP_PARSE_NODE_NULL, "super() call cannot find self"); // really a TypeError |
Damien George | 35e2a4e | 2014-02-05 00:51:47 +0000 | [diff] [blame] | 2183 | return; |
| 2184 | } |
Damien George | 35e2a4e | 2014-02-05 00:51:47 +0000 | [diff] [blame] | 2185 | |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 2186 | // get the list of arguments |
| 2187 | mp_parse_node_t *args; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 2188 | int n_args = mp_parse_node_extract_list(&pn_arglist, PN_arglist, &args); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2189 | |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 2190 | // compile the arguments |
| 2191 | // Rather than calling compile_node on the list, we go through the list of args |
| 2192 | // explicitly here so that we can count the number of arguments and give sensible |
| 2193 | // error messages. |
| 2194 | int n_positional = n_positional_extra; |
| 2195 | uint n_keyword = 0; |
| 2196 | uint star_flags = 0; |
Delio Brignoli | e6978a4 | 2015-09-17 12:07:06 +0200 | [diff] [blame] | 2197 | mp_parse_node_struct_t *star_args_node = NULL, *dblstar_args_node = NULL; |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 2198 | for (int i = 0; i < n_args; i++) { |
| 2199 | if (MP_PARSE_NODE_IS_STRUCT(args[i])) { |
| 2200 | mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i]; |
| 2201 | if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) { |
| 2202 | if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) { |
| 2203 | compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x"); |
| 2204 | return; |
| 2205 | } |
| 2206 | star_flags |= MP_EMIT_STAR_FLAG_SINGLE; |
Delio Brignoli | e6978a4 | 2015-09-17 12:07:06 +0200 | [diff] [blame] | 2207 | star_args_node = pns_arg; |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 2208 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) { |
| 2209 | if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) { |
| 2210 | compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x"); |
| 2211 | return; |
| 2212 | } |
| 2213 | star_flags |= MP_EMIT_STAR_FLAG_DOUBLE; |
Delio Brignoli | e6978a4 | 2015-09-17 12:07:06 +0200 | [diff] [blame] | 2214 | dblstar_args_node = pns_arg; |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 2215 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) { |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 2216 | if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns_arg->nodes[1], PN_comp_for)) { |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 2217 | if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) { |
| 2218 | compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id"); |
| 2219 | return; |
| 2220 | } |
Damien George | 59fba2d | 2015-06-25 14:42:13 +0000 | [diff] [blame] | 2221 | EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0])); |
Damien George | b948de3 | 2015-10-08 14:26:01 +0100 | [diff] [blame] | 2222 | compile_node(comp, pns_arg->nodes[1]); |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 2223 | n_keyword += 1; |
| 2224 | } else { |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 2225 | compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR); |
| 2226 | n_positional++; |
| 2227 | } |
| 2228 | } else { |
| 2229 | goto normal_argument; |
| 2230 | } |
| 2231 | } else { |
| 2232 | normal_argument: |
| 2233 | if (n_keyword > 0) { |
| 2234 | compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg"); |
| 2235 | return; |
| 2236 | } |
| 2237 | compile_node(comp, args[i]); |
| 2238 | n_positional++; |
| 2239 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2240 | } |
| 2241 | |
Delio Brignoli | e6978a4 | 2015-09-17 12:07:06 +0200 | [diff] [blame] | 2242 | // compile the star/double-star arguments if we had them |
Damien George | fbcaf0e | 2015-09-23 11:47:01 +0100 | [diff] [blame] | 2243 | // if we had one but not the other then we load "null" as a place holder |
| 2244 | if (star_flags != 0) { |
| 2245 | if (star_args_node == NULL) { |
| 2246 | EMIT(load_null); |
| 2247 | } else { |
| 2248 | compile_node(comp, star_args_node->nodes[0]); |
| 2249 | } |
| 2250 | if (dblstar_args_node == NULL) { |
| 2251 | EMIT(load_null); |
| 2252 | } else { |
| 2253 | compile_node(comp, dblstar_args_node->nodes[0]); |
| 2254 | } |
Delio Brignoli | e6978a4 | 2015-09-17 12:07:06 +0200 | [diff] [blame] | 2255 | } |
| 2256 | |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 2257 | // emit the function/method call |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2258 | if (is_method_call) { |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 2259 | EMIT_ARG(call_method, n_positional, n_keyword, star_flags); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2260 | } else { |
Damien George | 2c0842b | 2014-04-27 16:46:51 +0100 | [diff] [blame] | 2261 | EMIT_ARG(call_function, n_positional, n_keyword, star_flags); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2262 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2263 | } |
| 2264 | |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 2265 | STATIC void compile_atom_expr_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2266 | int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2267 | for (int i = 0; i < num_nodes; i++) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2268 | 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)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2269 | // optimisation for method calls a.f(...), following PyPy |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2270 | mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i]; |
| 2271 | mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1]; |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2272 | EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method |
Damien George | bbcd49a | 2014-02-06 20:30:16 +0000 | [diff] [blame] | 2273 | compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2274 | i += 1; |
| 2275 | } else { |
| 2276 | compile_node(comp, pns->nodes[i]); |
| 2277 | } |
Damien George | 35e2a4e | 2014-02-05 00:51:47 +0000 | [diff] [blame] | 2278 | comp->func_arg_is_super = false; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2279 | } |
| 2280 | } |
| 2281 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2282 | STATIC void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2283 | // a list of strings |
Damien | 6332174 | 2013-12-10 17:41:49 +0000 | [diff] [blame] | 2284 | |
| 2285 | // check type of list (string or bytes) and count total number of bytes |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2286 | int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
Damien George | 831137b | 2015-12-17 13:13:18 +0000 | [diff] [blame] | 2287 | size_t n_bytes = 0; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2288 | int string_kind = MP_PARSE_NODE_NULL; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2289 | for (int i = 0; i < n; i++) { |
Damien George | 5042bce | 2014-05-25 22:06:06 +0100 | [diff] [blame] | 2290 | int pn_kind; |
| 2291 | if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) { |
| 2292 | pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]); |
| 2293 | assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES); |
| 2294 | n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])); |
| 2295 | } else { |
| 2296 | assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])); |
| 2297 | mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i]; |
Damien George | 4c81ba8 | 2015-01-13 16:21:23 +0000 | [diff] [blame] | 2298 | if (MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string) { |
| 2299 | pn_kind = MP_PARSE_NODE_STRING; |
| 2300 | } else { |
| 2301 | assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_bytes); |
| 2302 | pn_kind = MP_PARSE_NODE_BYTES; |
| 2303 | } |
Damien George | 831137b | 2015-12-17 13:13:18 +0000 | [diff] [blame] | 2304 | n_bytes += pns_string->nodes[1]; |
Damien George | 5042bce | 2014-05-25 22:06:06 +0100 | [diff] [blame] | 2305 | } |
Damien | 6332174 | 2013-12-10 17:41:49 +0000 | [diff] [blame] | 2306 | if (i == 0) { |
| 2307 | string_kind = pn_kind; |
| 2308 | } else if (pn_kind != string_kind) { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 2309 | compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals"); |
Damien | 6332174 | 2013-12-10 17:41:49 +0000 | [diff] [blame] | 2310 | return; |
| 2311 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2312 | } |
Damien | 6332174 | 2013-12-10 17:41:49 +0000 | [diff] [blame] | 2313 | |
Damien George | 65ef6b7 | 2015-01-14 21:17:27 +0000 | [diff] [blame] | 2314 | // if we are not in the last pass, just load a dummy object |
| 2315 | if (comp->pass != MP_PASS_EMIT) { |
| 2316 | EMIT_ARG(load_const_obj, mp_const_none); |
| 2317 | return; |
| 2318 | } |
| 2319 | |
Damien | 6332174 | 2013-12-10 17:41:49 +0000 | [diff] [blame] | 2320 | // concatenate string/bytes |
Damien George | 05005f6 | 2015-01-21 22:48:37 +0000 | [diff] [blame] | 2321 | vstr_t vstr; |
| 2322 | vstr_init_len(&vstr, n_bytes); |
| 2323 | byte *s_dest = (byte*)vstr.buf; |
Damien | 6332174 | 2013-12-10 17:41:49 +0000 | [diff] [blame] | 2324 | for (int i = 0; i < n; i++) { |
Damien George | 5042bce | 2014-05-25 22:06:06 +0100 | [diff] [blame] | 2325 | if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) { |
Damien George | c3f64d9 | 2015-11-27 12:23:18 +0000 | [diff] [blame] | 2326 | size_t s_len; |
Damien George | 5042bce | 2014-05-25 22:06:06 +0100 | [diff] [blame] | 2327 | const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len); |
| 2328 | memcpy(s_dest, s, s_len); |
| 2329 | s_dest += s_len; |
| 2330 | } else { |
| 2331 | mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i]; |
Damien George | 831137b | 2015-12-17 13:13:18 +0000 | [diff] [blame] | 2332 | memcpy(s_dest, (const char*)pns_string->nodes[0], pns_string->nodes[1]); |
| 2333 | s_dest += pns_string->nodes[1]; |
Damien George | 5042bce | 2014-05-25 22:06:06 +0100 | [diff] [blame] | 2334 | } |
Damien | 6332174 | 2013-12-10 17:41:49 +0000 | [diff] [blame] | 2335 | } |
Damien George | 65ef6b7 | 2015-01-14 21:17:27 +0000 | [diff] [blame] | 2336 | |
| 2337 | // load the object |
Damien George | 05005f6 | 2015-01-21 22:48:37 +0000 | [diff] [blame] | 2338 | EMIT_ARG(load_const_obj, mp_obj_new_str_from_vstr(string_kind == MP_PARSE_NODE_STRING ? &mp_type_str : &mp_type_bytes, &vstr)); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2339 | } |
| 2340 | |
| 2341 | // pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 2342 | STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2343 | assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2); |
| 2344 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for)); |
| 2345 | mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2346 | |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 2347 | if (comp->pass == MP_PASS_SCOPE) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2348 | // create a new scope for this comprehension |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2349 | scope_t *s = scope_new_and_link(comp, kind, (mp_parse_node_t)pns, comp->scope_cur->emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2350 | // store the comprehension scope so the compiling function (this one) can use it at each pass |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2351 | pns_comp_for->nodes[3] = (mp_parse_node_t)s; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2352 | } |
| 2353 | |
| 2354 | // get the scope for this comprehension |
| 2355 | scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3]; |
| 2356 | |
| 2357 | // compile the comprehension |
| 2358 | close_over_variables_etc(comp, this_scope, 0, 0); |
| 2359 | |
| 2360 | compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator |
| 2361 | EMIT(get_iter); |
Damien George | 922ddd6 | 2014-04-09 12:43:17 +0100 | [diff] [blame] | 2362 | EMIT_ARG(call_function, 1, 0, 0); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2363 | } |
| 2364 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2365 | STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2366 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2367 | // an empty tuple |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2368 | c_tuple(comp, MP_PARSE_NODE_NULL, NULL); |
Damien George | 93b3726 | 2016-01-07 13:07:52 +0000 | [diff] [blame] | 2369 | } else { |
| 2370 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2371 | pns = (mp_parse_node_struct_t*)pns->nodes[0]; |
| 2372 | assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1])); |
| 2373 | if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
| 2374 | mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1]; |
| 2375 | if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2376 | // tuple of one item, with trailing comma |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2377 | assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2378 | c_tuple(comp, pns->nodes[0], NULL); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2379 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2380 | // tuple of many items |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2381 | c_tuple(comp, pns->nodes[0], pns2); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2382 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2383 | // generator expression |
| 2384 | compile_comprehension(comp, pns, SCOPE_GEN_EXPR); |
| 2385 | } else { |
| 2386 | // tuple with 2 items |
| 2387 | goto tuple_with_2_items; |
| 2388 | } |
| 2389 | } else { |
| 2390 | // tuple with 2 items |
| 2391 | tuple_with_2_items: |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2392 | c_tuple(comp, MP_PARSE_NODE_NULL, pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2393 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2394 | } |
| 2395 | } |
| 2396 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2397 | STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2398 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2399 | // empty list |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2400 | EMIT_ARG(build_list, 0); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2401 | } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) { |
| 2402 | mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0]; |
| 2403 | if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) { |
| 2404 | mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1]; |
| 2405 | if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2406 | // list of one item, with trailing comma |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2407 | assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2408 | compile_node(comp, pns2->nodes[0]); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2409 | EMIT_ARG(build_list, 1); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2410 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2411 | // list of many items |
| 2412 | compile_node(comp, pns2->nodes[0]); |
| 2413 | compile_generic_all_nodes(comp, pns3); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2414 | EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3)); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2415 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2416 | // list comprehension |
| 2417 | compile_comprehension(comp, pns2, SCOPE_LIST_COMP); |
| 2418 | } else { |
| 2419 | // list with 2 items |
| 2420 | goto list_with_2_items; |
| 2421 | } |
| 2422 | } else { |
| 2423 | // list with 2 items |
| 2424 | list_with_2_items: |
| 2425 | compile_node(comp, pns2->nodes[0]); |
| 2426 | compile_node(comp, pns2->nodes[1]); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2427 | EMIT_ARG(build_list, 2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2428 | } |
| 2429 | } else { |
| 2430 | // list with 1 item |
| 2431 | compile_node(comp, pns->nodes[0]); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2432 | EMIT_ARG(build_list, 1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2433 | } |
| 2434 | } |
| 2435 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2436 | STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2437 | mp_parse_node_t pn = pns->nodes[0]; |
| 2438 | if (MP_PARSE_NODE_IS_NULL(pn)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2439 | // empty dict |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2440 | EMIT_ARG(build_map, 0); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2441 | } else if (MP_PARSE_NODE_IS_STRUCT(pn)) { |
| 2442 | pns = (mp_parse_node_struct_t*)pn; |
| 2443 | if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2444 | // dict with one element |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2445 | EMIT_ARG(build_map, 1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2446 | compile_node(comp, pn); |
| 2447 | EMIT(store_map); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2448 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) { |
| 2449 | assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed |
| 2450 | mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1]; |
| 2451 | if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2452 | // dict/set with multiple elements |
| 2453 | |
| 2454 | // get tail elements (2nd, 3rd, ...) |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2455 | mp_parse_node_t *nodes; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 2456 | int n = mp_parse_node_extract_list(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2457 | |
| 2458 | // first element sets whether it's a dict or set |
| 2459 | bool is_dict; |
Damien George | e37dcaa | 2014-12-27 17:07:16 +0000 | [diff] [blame] | 2460 | if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2461 | // a dictionary |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2462 | EMIT_ARG(build_map, 1 + n); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2463 | compile_node(comp, pns->nodes[0]); |
| 2464 | EMIT(store_map); |
| 2465 | is_dict = true; |
| 2466 | } else { |
| 2467 | // a set |
| 2468 | compile_node(comp, pns->nodes[0]); // 1st value of set |
| 2469 | is_dict = false; |
| 2470 | } |
| 2471 | |
| 2472 | // process rest of elements |
| 2473 | for (int i = 0; i < n; i++) { |
Damien George | 50912e7 | 2015-01-20 11:55:10 +0000 | [diff] [blame] | 2474 | mp_parse_node_t pn_i = nodes[i]; |
| 2475 | bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn_i, PN_dictorsetmaker_item); |
| 2476 | compile_node(comp, pn_i); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2477 | if (is_dict) { |
| 2478 | if (!is_key_value) { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 2479 | compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2480 | return; |
| 2481 | } |
| 2482 | EMIT(store_map); |
| 2483 | } else { |
| 2484 | if (is_key_value) { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 2485 | compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2486 | return; |
| 2487 | } |
| 2488 | } |
| 2489 | } |
| 2490 | |
Damien George | e37dcaa | 2014-12-27 17:07:16 +0000 | [diff] [blame] | 2491 | #if MICROPY_PY_BUILTINS_SET |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2492 | // if it's a set, build it |
| 2493 | if (!is_dict) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2494 | EMIT_ARG(build_set, 1 + n); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2495 | } |
Damien George | e37dcaa | 2014-12-27 17:07:16 +0000 | [diff] [blame] | 2496 | #endif |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 2497 | } else { |
| 2498 | assert(MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for); // should be |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2499 | // dict/set comprehension |
Damien George | e37dcaa | 2014-12-27 17:07:16 +0000 | [diff] [blame] | 2500 | if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2501 | // a dictionary comprehension |
| 2502 | compile_comprehension(comp, pns, SCOPE_DICT_COMP); |
| 2503 | } else { |
| 2504 | // a set comprehension |
| 2505 | compile_comprehension(comp, pns, SCOPE_SET_COMP); |
| 2506 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2507 | } |
| 2508 | } else { |
| 2509 | // set with one element |
| 2510 | goto set_with_one_element; |
| 2511 | } |
| 2512 | } else { |
| 2513 | // set with one element |
| 2514 | set_with_one_element: |
Damien George | e37dcaa | 2014-12-27 17:07:16 +0000 | [diff] [blame] | 2515 | #if MICROPY_PY_BUILTINS_SET |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2516 | compile_node(comp, pn); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2517 | EMIT_ARG(build_set, 1); |
Damien George | e37dcaa | 2014-12-27 17:07:16 +0000 | [diff] [blame] | 2518 | #else |
| 2519 | assert(0); |
| 2520 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2521 | } |
| 2522 | } |
| 2523 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2524 | STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | bbcd49a | 2014-02-06 20:30:16 +0000 | [diff] [blame] | 2525 | compile_trailer_paren_helper(comp, pns->nodes[0], false, 0); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2526 | } |
| 2527 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2528 | STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2529 | // object who's index we want is on top of stack |
| 2530 | compile_node(comp, pns->nodes[0]); // the index |
Damien George | 729f7b4 | 2014-04-17 22:10:53 +0100 | [diff] [blame] | 2531 | EMIT(load_subscr); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2532 | } |
| 2533 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2534 | STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2535 | // object who's attribute we want is on top of stack |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2536 | EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2537 | } |
| 2538 | |
Damien George | 83204f3 | 2014-12-27 17:20:41 +0000 | [diff] [blame] | 2539 | #if MICROPY_PY_BUILTINS_SLICE |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2540 | STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2541 | assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be |
| 2542 | mp_parse_node_t pn = pns->nodes[0]; |
| 2543 | if (MP_PARSE_NODE_IS_NULL(pn)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2544 | // [?:] |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2545 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
| 2546 | EMIT_ARG(build_slice, 2); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2547 | } else if (MP_PARSE_NODE_IS_STRUCT(pn)) { |
| 2548 | pns = (mp_parse_node_struct_t*)pn; |
| 2549 | if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2550 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2551 | pn = pns->nodes[0]; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2552 | if (MP_PARSE_NODE_IS_NULL(pn)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2553 | // [?::] |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2554 | EMIT_ARG(build_slice, 2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2555 | } else { |
| 2556 | // [?::x] |
| 2557 | compile_node(comp, pn); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2558 | EMIT_ARG(build_slice, 3); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2559 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2560 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2561 | compile_node(comp, pns->nodes[0]); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2562 | assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be |
| 2563 | pns = (mp_parse_node_struct_t*)pns->nodes[1]; |
| 2564 | assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be |
| 2565 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2566 | // [?:x:] |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2567 | EMIT_ARG(build_slice, 2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2568 | } else { |
| 2569 | // [?:x:x] |
| 2570 | compile_node(comp, pns->nodes[0]); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2571 | EMIT_ARG(build_slice, 3); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2572 | } |
| 2573 | } else { |
| 2574 | // [?:x] |
| 2575 | compile_node(comp, pn); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2576 | EMIT_ARG(build_slice, 2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2577 | } |
| 2578 | } else { |
| 2579 | // [?:x] |
| 2580 | compile_node(comp, pn); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2581 | EMIT_ARG(build_slice, 2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2582 | } |
| 2583 | } |
| 2584 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2585 | STATIC void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2586 | compile_node(comp, pns->nodes[0]); // start of slice |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2587 | assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be |
| 2588 | compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2589 | } |
| 2590 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2591 | STATIC void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2592 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2593 | compile_subscript_3_helper(comp, pns); |
| 2594 | } |
Damien George | 83204f3 | 2014-12-27 17:20:41 +0000 | [diff] [blame] | 2595 | #endif // MICROPY_PY_BUILTINS_SLICE |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2596 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2597 | STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2598 | // if this is called then we are compiling a dict key:value pair |
| 2599 | compile_node(comp, pns->nodes[1]); // value |
| 2600 | compile_node(comp, pns->nodes[0]); // key |
| 2601 | } |
| 2602 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2603 | STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 2604 | qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2605 | // store class object into class name |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 2606 | compile_store_id(comp, cname); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2607 | } |
| 2608 | |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 2609 | STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | 0e3329a | 2014-04-11 13:10:21 +0000 | [diff] [blame] | 2610 | if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) { |
Damien George | b7ffdcc | 2014-04-08 16:41:02 +0100 | [diff] [blame] | 2611 | compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2612 | return; |
| 2613 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2614 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2615 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2616 | EMIT(yield_value); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2617 | } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) { |
| 2618 | pns = (mp_parse_node_struct_t*)pns->nodes[0]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2619 | compile_node(comp, pns->nodes[0]); |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 2620 | compile_yield_from(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2621 | } else { |
| 2622 | compile_node(comp, pns->nodes[0]); |
| 2623 | EMIT(yield_value); |
| 2624 | } |
| 2625 | } |
| 2626 | |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 2627 | #if MICROPY_PY_ASYNC_AWAIT |
| 2628 | STATIC void compile_atom_expr_await(compiler_t *comp, mp_parse_node_struct_t *pns) { |
| 2629 | if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) { |
| 2630 | compile_syntax_error(comp, (mp_parse_node_t)pns, "'await' outside function"); |
| 2631 | return; |
| 2632 | } |
| 2633 | compile_atom_expr_normal(comp, pns); |
| 2634 | compile_yield_from(comp); |
| 2635 | } |
| 2636 | #endif |
| 2637 | |
Damien George | 65ef6b7 | 2015-01-14 21:17:27 +0000 | [diff] [blame] | 2638 | STATIC void compile_string(compiler_t *comp, mp_parse_node_struct_t *pns) { |
| 2639 | // only create and load the actual str object on the last pass |
| 2640 | if (comp->pass != MP_PASS_EMIT) { |
| 2641 | EMIT_ARG(load_const_obj, mp_const_none); |
| 2642 | } else { |
Damien George | 831137b | 2015-12-17 13:13:18 +0000 | [diff] [blame] | 2643 | EMIT_ARG(load_const_obj, mp_obj_new_str((const char*)pns->nodes[0], pns->nodes[1], false)); |
Damien George | 65ef6b7 | 2015-01-14 21:17:27 +0000 | [diff] [blame] | 2644 | } |
| 2645 | } |
| 2646 | |
| 2647 | STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) { |
| 2648 | // only create and load the actual bytes object on the last pass |
| 2649 | if (comp->pass != MP_PASS_EMIT) { |
| 2650 | EMIT_ARG(load_const_obj, mp_const_none); |
| 2651 | } else { |
Damien George | 831137b | 2015-12-17 13:13:18 +0000 | [diff] [blame] | 2652 | EMIT_ARG(load_const_obj, mp_obj_new_bytes((const byte*)pns->nodes[0], pns->nodes[1])); |
Damien George | 65ef6b7 | 2015-01-14 21:17:27 +0000 | [diff] [blame] | 2653 | } |
| 2654 | } |
| 2655 | |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 2656 | STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) { |
Damien George | b8cfb0d | 2015-11-27 17:09:11 +0000 | [diff] [blame] | 2657 | #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D |
| 2658 | // nodes are 32-bit pointers, but need to extract 64-bit object |
| 2659 | EMIT_ARG(load_const_obj, (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32)); |
| 2660 | #else |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 2661 | EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]); |
Damien George | b8cfb0d | 2015-11-27 17:09:11 +0000 | [diff] [blame] | 2662 | #endif |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 2663 | } |
| 2664 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2665 | typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*); |
Damien George | 3ff16ff | 2016-05-20 12:38:15 +0100 | [diff] [blame] | 2666 | STATIC const compile_function_t compile_function[] = { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2667 | #define nc NULL |
| 2668 | #define c(f) compile_##f |
Damien George | 1dc76af | 2014-02-26 16:57:08 +0000 | [diff] [blame] | 2669 | #define DEF_RULE(rule, comp, kind, ...) comp, |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 2670 | #include "py/grammar.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2671 | #undef nc |
| 2672 | #undef c |
| 2673 | #undef DEF_RULE |
Damien George | 65ef6b7 | 2015-01-14 21:17:27 +0000 | [diff] [blame] | 2674 | NULL, |
| 2675 | compile_string, |
| 2676 | compile_bytes, |
Damien George | 7d414a1 | 2015-02-08 01:57:40 +0000 | [diff] [blame] | 2677 | compile_const_object, |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2678 | }; |
| 2679 | |
Damien George | 6be0b0a | 2014-08-15 14:30:52 +0100 | [diff] [blame] | 2680 | STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2681 | if (MP_PARSE_NODE_IS_NULL(pn)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2682 | // pass |
Paul Sokolovsky | 56e5ef2 | 2014-02-22 16:39:45 +0200 | [diff] [blame] | 2683 | } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 2684 | mp_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn); |
Damien George | ea23520 | 2016-02-11 22:30:53 +0000 | [diff] [blame] | 2685 | #if MICROPY_DYNAMIC_COMPILER |
| 2686 | mp_uint_t sign_mask = -(1 << (mp_dynamic_compiler.small_int_bits - 1)); |
| 2687 | if ((arg & sign_mask) == 0 || (arg & sign_mask) == sign_mask) { |
| 2688 | // integer fits in target runtime's small-int |
| 2689 | EMIT_ARG(load_const_small_int, arg); |
| 2690 | } else { |
| 2691 | // integer doesn't fit, so create a multi-precision int object |
| 2692 | // (but only create the actual object on the last pass) |
| 2693 | if (comp->pass != MP_PASS_EMIT) { |
| 2694 | EMIT_ARG(load_const_obj, mp_const_none); |
| 2695 | } else { |
| 2696 | EMIT_ARG(load_const_obj, mp_obj_new_int_from_ll(arg)); |
| 2697 | } |
| 2698 | } |
| 2699 | #else |
Paul Sokolovsky | 56e5ef2 | 2014-02-22 16:39:45 +0200 | [diff] [blame] | 2700 | EMIT_ARG(load_const_small_int, arg); |
Damien George | ea23520 | 2016-02-11 22:30:53 +0000 | [diff] [blame] | 2701 | #endif |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2702 | } else if (MP_PARSE_NODE_IS_LEAF(pn)) { |
Damien George | 831137b | 2015-12-17 13:13:18 +0000 | [diff] [blame] | 2703 | uintptr_t arg = MP_PARSE_NODE_LEAF_ARG(pn); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2704 | switch (MP_PARSE_NODE_LEAF_KIND(pn)) { |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 2705 | case MP_PARSE_NODE_ID: compile_load_id(comp, arg); break; |
Damien George | 59fba2d | 2015-06-25 14:42:13 +0000 | [diff] [blame] | 2706 | case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg); break; |
| 2707 | case MP_PARSE_NODE_BYTES: |
| 2708 | // only create and load the actual bytes object on the last pass |
| 2709 | if (comp->pass != MP_PASS_EMIT) { |
| 2710 | EMIT_ARG(load_const_obj, mp_const_none); |
| 2711 | } else { |
Damien George | c3f64d9 | 2015-11-27 12:23:18 +0000 | [diff] [blame] | 2712 | size_t len; |
Damien George | 59fba2d | 2015-06-25 14:42:13 +0000 | [diff] [blame] | 2713 | const byte *data = qstr_data(arg, &len); |
| 2714 | EMIT_ARG(load_const_obj, mp_obj_new_bytes(data, len)); |
| 2715 | } |
| 2716 | break; |
Damien George | d2d64f0 | 2015-01-14 21:32:42 +0000 | [diff] [blame] | 2717 | case MP_PARSE_NODE_TOKEN: default: |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2718 | if (arg == MP_TOKEN_NEWLINE) { |
Damien | 91d387d | 2013-10-09 15:09:52 +0100 | [diff] [blame] | 2719 | // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline) |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 2720 | // or when single_input lets through a NEWLINE (user enters a blank line) |
Damien | 91d387d | 2013-10-09 15:09:52 +0100 | [diff] [blame] | 2721 | // do nothing |
| 2722 | } else { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2723 | EMIT_ARG(load_const_tok, arg); |
Damien | 91d387d | 2013-10-09 15:09:52 +0100 | [diff] [blame] | 2724 | } |
| 2725 | break; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2726 | } |
| 2727 | } else { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2728 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 2729 | EMIT_ARG(set_source_line, pns->source_line); |
Damien George | 65ef6b7 | 2015-01-14 21:17:27 +0000 | [diff] [blame] | 2730 | compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)]; |
| 2731 | if (f == NULL) { |
Damien George | 5042bce | 2014-05-25 22:06:06 +0100 | [diff] [blame] | 2732 | #if MICROPY_DEBUG_PRINTERS |
Damien George | 65ef6b7 | 2015-01-14 21:17:27 +0000 | [diff] [blame] | 2733 | printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns)); |
| 2734 | mp_parse_node_print(pn, 0); |
Damien George | 5042bce | 2014-05-25 22:06:06 +0100 | [diff] [blame] | 2735 | #endif |
Damien George | 65ef6b7 | 2015-01-14 21:17:27 +0000 | [diff] [blame] | 2736 | compile_syntax_error(comp, pn, "internal compiler error"); |
| 2737 | } else { |
| 2738 | f(comp, pns); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2739 | } |
| 2740 | } |
| 2741 | } |
| 2742 | |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 2743 | STATIC void 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) { |
Damien George | 9a56912 | 2015-11-23 16:50:42 +0000 | [diff] [blame] | 2744 | // check that **kw is last |
| 2745 | if ((comp->scope_cur->scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) { |
| 2746 | compile_syntax_error(comp, pn, "invalid syntax"); |
| 2747 | return; |
| 2748 | } |
| 2749 | |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 2750 | qstr param_name = MP_QSTR_NULL; |
| 2751 | uint param_flag = ID_FLAG_IS_PARAM; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2752 | if (MP_PARSE_NODE_IS_ID(pn)) { |
| 2753 | param_name = MP_PARSE_NODE_LEAF_ARG(pn); |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 2754 | if (comp->have_star) { |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 2755 | // comes after a star, so counts as a keyword-only parameter |
| 2756 | comp->scope_cur->num_kwonly_args += 1; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2757 | } else { |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 2758 | // comes before a star, so counts as a positional parameter |
| 2759 | comp->scope_cur->num_pos_args += 1; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2760 | } |
Damien | b14de21 | 2013-10-06 00:28:28 +0100 | [diff] [blame] | 2761 | } else { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2762 | assert(MP_PARSE_NODE_IS_STRUCT(pn)); |
| 2763 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
| 2764 | if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) { |
| 2765 | param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 2766 | if (comp->have_star) { |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 2767 | // comes after a star, so counts as a keyword-only parameter |
| 2768 | comp->scope_cur->num_kwonly_args += 1; |
Damien | b14de21 | 2013-10-06 00:28:28 +0100 | [diff] [blame] | 2769 | } else { |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 2770 | // comes before a star, so counts as a positional parameter |
| 2771 | comp->scope_cur->num_pos_args += 1; |
Damien | b14de21 | 2013-10-06 00:28:28 +0100 | [diff] [blame] | 2772 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2773 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) { |
Damien George | 9a56912 | 2015-11-23 16:50:42 +0000 | [diff] [blame] | 2774 | if (comp->have_star) { |
| 2775 | // more than one star |
| 2776 | compile_syntax_error(comp, pn, "invalid syntax"); |
| 2777 | return; |
| 2778 | } |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 2779 | comp->have_star = true; |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 2780 | param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2781 | if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
Damien | b14de21 | 2013-10-06 00:28:28 +0100 | [diff] [blame] | 2782 | // bare star |
| 2783 | // TODO see http://www.python.org/dev/peps/pep-3102/ |
Damien | b14de21 | 2013-10-06 00:28:28 +0100 | [diff] [blame] | 2784 | //assert(comp->scope_cur->num_dict_params == 0); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2785 | } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) { |
Damien | b14de21 | 2013-10-06 00:28:28 +0100 | [diff] [blame] | 2786 | // named star |
Damien George | 8725f8f | 2014-02-15 19:33:11 +0000 | [diff] [blame] | 2787 | comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2788 | param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 2789 | } else { |
| 2790 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)); // should be |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 2791 | // named star with possible annotation |
Damien George | 8725f8f | 2014-02-15 19:33:11 +0000 | [diff] [blame] | 2792 | comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2793 | pns = (mp_parse_node_struct_t*)pns->nodes[0]; |
| 2794 | param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
Damien | b14de21 | 2013-10-06 00:28:28 +0100 | [diff] [blame] | 2795 | } |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 2796 | } else { |
| 2797 | assert(MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star); // should be |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2798 | param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 2799 | param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM; |
Damien George | 8725f8f | 2014-02-15 19:33:11 +0000 | [diff] [blame] | 2800 | comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2801 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2802 | } |
| 2803 | |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 2804 | if (param_name != MP_QSTR_NULL) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2805 | bool added; |
| 2806 | id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added); |
| 2807 | if (!added) { |
Damien George | 9d181f6 | 2014-04-27 16:55:27 +0100 | [diff] [blame] | 2808 | compile_syntax_error(comp, pn, "name reused for argument"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2809 | return; |
| 2810 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2811 | id_info->kind = ID_INFO_KIND_LOCAL; |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 2812 | id_info->flags = param_flag; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2813 | } |
| 2814 | } |
| 2815 | |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 2816 | STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) { |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 2817 | compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2818 | } |
| 2819 | |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 2820 | STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) { |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 2821 | compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star); |
| 2822 | } |
| 2823 | |
Damien George | ea5b59b | 2015-09-04 16:44:14 +0100 | [diff] [blame] | 2824 | #if MICROPY_EMIT_NATIVE |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 2825 | STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn) { |
| 2826 | if (!MP_PARSE_NODE_IS_STRUCT(pn)) { |
| 2827 | // no annotation |
| 2828 | return; |
| 2829 | } |
| 2830 | |
| 2831 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
| 2832 | if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) { |
| 2833 | // named parameter with possible annotation |
| 2834 | // fallthrough |
| 2835 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) { |
| 2836 | if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) { |
| 2837 | // named star with possible annotation |
| 2838 | pns = (mp_parse_node_struct_t*)pns->nodes[0]; |
| 2839 | // fallthrough |
| 2840 | } else { |
| 2841 | // no annotation |
| 2842 | return; |
| 2843 | } |
| 2844 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_dbl_star) { |
| 2845 | // double star with possible annotation |
| 2846 | // fallthrough |
| 2847 | } else { |
| 2848 | // no annotation |
| 2849 | return; |
| 2850 | } |
| 2851 | |
| 2852 | mp_parse_node_t pn_annotation = pns->nodes[1]; |
| 2853 | |
| 2854 | if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) { |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 2855 | qstr param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
| 2856 | id_info_t *id_info = scope_find(comp->scope_cur, param_name); |
| 2857 | assert(id_info != NULL); |
| 2858 | |
Damien George | ea5b59b | 2015-09-04 16:44:14 +0100 | [diff] [blame] | 2859 | if (MP_PARSE_NODE_IS_ID(pn_annotation)) { |
| 2860 | qstr arg_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation); |
| 2861 | EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ARG, id_info->local_num, arg_type); |
| 2862 | } else { |
| 2863 | compile_syntax_error(comp, pn_annotation, "parameter annotation must be an identifier"); |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 2864 | } |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 2865 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2866 | } |
Damien George | ea5b59b | 2015-09-04 16:44:14 +0100 | [diff] [blame] | 2867 | #endif // MICROPY_EMIT_NATIVE |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2868 | |
Damien George | a831243 | 2015-12-18 01:37:55 +0000 | [diff] [blame] | 2869 | STATIC void compile_scope_comp_iter(compiler_t *comp, mp_parse_node_struct_t *pns_comp_for, mp_parse_node_t pn_inner_expr, int for_depth) { |
| 2870 | uint l_top = comp_next_label(comp); |
| 2871 | uint l_end = comp_next_label(comp); |
| 2872 | EMIT_ARG(label_assign, l_top); |
| 2873 | EMIT_ARG(for_iter, l_end); |
| 2874 | c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE); |
| 2875 | mp_parse_node_t pn_iter = pns_comp_for->nodes[2]; |
| 2876 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2877 | tail_recursion: |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2878 | if (MP_PARSE_NODE_IS_NULL(pn_iter)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2879 | // no more nested if/for; compile inner expression |
| 2880 | compile_node(comp, pn_inner_expr); |
Damien George | a5624bf | 2016-09-18 23:59:47 +1000 | [diff] [blame] | 2881 | if (comp->scope_cur->kind == SCOPE_GEN_EXPR) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2882 | EMIT(yield_value); |
| 2883 | EMIT(pop_top); |
Damien George | a5624bf | 2016-09-18 23:59:47 +1000 | [diff] [blame] | 2884 | } else { |
| 2885 | EMIT_ARG(store_comp, comp->scope_cur->kind, for_depth + 2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2886 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2887 | } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2888 | // if condition |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2889 | mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2890 | c_if_cond(comp, pns_comp_if->nodes[0], false, l_top); |
| 2891 | pn_iter = pns_comp_if->nodes[1]; |
| 2892 | goto tail_recursion; |
Damien George | 0bb9713 | 2015-02-27 14:25:47 +0000 | [diff] [blame] | 2893 | } else { |
| 2894 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)); // should be |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2895 | // for loop |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2896 | mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2897 | compile_node(comp, pns_comp_for2->nodes[1]); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2898 | EMIT(get_iter); |
Damien George | a831243 | 2015-12-18 01:37:55 +0000 | [diff] [blame] | 2899 | compile_scope_comp_iter(comp, pns_comp_for2, pn_inner_expr, for_depth + 1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2900 | } |
Damien George | a831243 | 2015-12-18 01:37:55 +0000 | [diff] [blame] | 2901 | |
| 2902 | EMIT_ARG(jump, l_top); |
| 2903 | EMIT_ARG(label_assign, l_end); |
| 2904 | EMIT(for_iter_end); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2905 | } |
| 2906 | |
Damien George | 1463c1f | 2014-04-25 23:52:57 +0100 | [diff] [blame] | 2907 | STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) { |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 2908 | #if MICROPY_ENABLE_DOC_STRING |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2909 | // see http://www.python.org/dev/peps/pep-0257/ |
| 2910 | |
| 2911 | // look for the first statement |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2912 | if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) { |
Damien | e388f10 | 2013-12-12 15:24:38 +0000 | [diff] [blame] | 2913 | // a statement; fall through |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2914 | } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) { |
Damien | e388f10 | 2013-12-12 15:24:38 +0000 | [diff] [blame] | 2915 | // file input; find the first non-newline node |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2916 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
| 2917 | int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns); |
Damien | e388f10 | 2013-12-12 15:24:38 +0000 | [diff] [blame] | 2918 | for (int i = 0; i < num_nodes; i++) { |
| 2919 | pn = pns->nodes[i]; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2920 | 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)) { |
Damien | e388f10 | 2013-12-12 15:24:38 +0000 | [diff] [blame] | 2921 | // not a newline, so this is the first statement; finish search |
| 2922 | break; |
| 2923 | } |
| 2924 | } |
| 2925 | // 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 |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2926 | } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) { |
Damien | e388f10 | 2013-12-12 15:24:38 +0000 | [diff] [blame] | 2927 | // a list of statements; get the first one |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2928 | pn = ((mp_parse_node_struct_t*)pn)->nodes[0]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2929 | } else { |
| 2930 | return; |
| 2931 | } |
| 2932 | |
| 2933 | // check the first statement for a doc string |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2934 | if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) { |
Damien George | 4dea922 | 2015-04-09 15:29:54 +0000 | [diff] [blame] | 2935 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; |
Damien George | 5042bce | 2014-05-25 22:06:06 +0100 | [diff] [blame] | 2936 | if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) |
| 2937 | && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING) |
| 2938 | || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) { |
| 2939 | // compile the doc string |
| 2940 | compile_node(comp, pns->nodes[0]); |
| 2941 | // store the doc string |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 2942 | compile_store_id(comp, MP_QSTR___doc__); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2943 | } |
| 2944 | } |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 2945 | #else |
| 2946 | (void)comp; |
| 2947 | (void)pn; |
Damien George | 1463c1f | 2014-04-25 23:52:57 +0100 | [diff] [blame] | 2948 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2949 | } |
| 2950 | |
Damien George | 1463c1f | 2014-04-25 23:52:57 +0100 | [diff] [blame] | 2951 | STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2952 | comp->pass = pass; |
| 2953 | comp->scope_cur = scope; |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 2954 | comp->next_label = 1; |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2955 | EMIT_ARG(start_pass, pass, scope); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2956 | |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 2957 | if (comp->pass == MP_PASS_SCOPE) { |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 2958 | // reset maximum stack sizes in scope |
| 2959 | // they will be computed in this first pass |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2960 | scope->stack_size = 0; |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 2961 | scope->exc_stack_size = 0; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2962 | } |
| 2963 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2964 | // compile |
Damien George | d02c6d8 | 2014-01-15 22:14:03 +0000 | [diff] [blame] | 2965 | if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) { |
| 2966 | assert(scope->kind == SCOPE_MODULE); |
| 2967 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn; |
| 2968 | compile_node(comp, pns->nodes[0]); // compile the expression |
| 2969 | EMIT(return_value); |
| 2970 | } else if (scope->kind == SCOPE_MODULE) { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 2971 | if (!comp->is_repl) { |
| 2972 | check_for_doc_string(comp, scope->pn); |
| 2973 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2974 | compile_node(comp, scope->pn); |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 2975 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2976 | EMIT(return_value); |
| 2977 | } else if (scope->kind == SCOPE_FUNCTION) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 2978 | assert(MP_PARSE_NODE_IS_STRUCT(scope->pn)); |
| 2979 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn; |
| 2980 | assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2981 | |
| 2982 | // work out number of parameters, keywords and default parameters, and add them to the id_info array |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 2983 | // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc) |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 2984 | if (comp->pass == MP_PASS_SCOPE) { |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 2985 | comp->have_star = false; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2986 | apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param); |
Damien George | ea5b59b | 2015-09-04 16:44:14 +0100 | [diff] [blame] | 2987 | } |
| 2988 | #if MICROPY_EMIT_NATIVE |
| 2989 | else if (scope->emit_options == MP_EMIT_OPT_VIPER) { |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 2990 | // compile annotations; only needed on latter compiler passes |
Damien George | ea5b59b | 2015-09-04 16:44:14 +0100 | [diff] [blame] | 2991 | // only needed for viper emitter |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2992 | |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 2993 | // argument annotations |
| 2994 | apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_annotations); |
| 2995 | |
| 2996 | // pns->nodes[2] is return/whole function annotation |
Damien George | a5190a7 | 2014-08-15 22:39:08 +0100 | [diff] [blame] | 2997 | mp_parse_node_t pn_annotation = pns->nodes[2]; |
| 2998 | if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) { |
Damien George | ea5b59b | 2015-09-04 16:44:14 +0100 | [diff] [blame] | 2999 | // nodes[2] can be null or a test-expr |
| 3000 | if (MP_PARSE_NODE_IS_ID(pn_annotation)) { |
| 3001 | qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation); |
| 3002 | EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_RETURN, 0, ret_type); |
| 3003 | } else { |
| 3004 | compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier"); |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 3005 | } |
| 3006 | } |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 3007 | } |
Damien George | ea5b59b | 2015-09-04 16:44:14 +0100 | [diff] [blame] | 3008 | #endif // MICROPY_EMIT_NATIVE |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3009 | |
| 3010 | compile_node(comp, pns->nodes[3]); // 3 is function body |
| 3011 | // emit return if it wasn't the last opcode |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 3012 | if (!EMIT(last_emit_was_return_value)) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 3013 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3014 | EMIT(return_value); |
| 3015 | } |
| 3016 | } else if (scope->kind == SCOPE_LAMBDA) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3017 | assert(MP_PARSE_NODE_IS_STRUCT(scope->pn)); |
| 3018 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn; |
| 3019 | assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3020 | |
| 3021 | // work out number of parameters, keywords and default parameters, and add them to the id_info array |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 3022 | // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc) |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3023 | if (comp->pass == MP_PASS_SCOPE) { |
Damien George | 8b19db0 | 2014-04-11 23:25:34 +0100 | [diff] [blame] | 3024 | comp->have_star = false; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3025 | apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param); |
| 3026 | } |
| 3027 | |
| 3028 | compile_node(comp, pns->nodes[1]); // 1 is lambda body |
Damien George | 0e3329a | 2014-04-11 13:10:21 +0000 | [diff] [blame] | 3029 | |
| 3030 | // if the lambda is a generator, then we return None, not the result of the expression of the lambda |
| 3031 | if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) { |
| 3032 | EMIT(pop_top); |
| 3033 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
| 3034 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3035 | EMIT(return_value); |
| 3036 | } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) { |
| 3037 | // a bit of a hack at the moment |
| 3038 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3039 | assert(MP_PARSE_NODE_IS_STRUCT(scope->pn)); |
| 3040 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn; |
| 3041 | assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2); |
| 3042 | assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for)); |
| 3043 | mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3044 | |
Damien George | 708c073 | 2014-04-27 19:23:46 +0100 | [diff] [blame] | 3045 | // We need a unique name for the comprehension argument (the iterator). |
| 3046 | // CPython uses .0, but we should be able to use anything that won't |
| 3047 | // clash with a user defined variable. Best to use an existing qstr, |
| 3048 | // so we use the blank qstr. |
Damien George | 708c073 | 2014-04-27 19:23:46 +0100 | [diff] [blame] | 3049 | qstr qstr_arg = MP_QSTR_; |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3050 | if (comp->pass == MP_PASS_SCOPE) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3051 | bool added; |
| 3052 | id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added); |
| 3053 | assert(added); |
| 3054 | id_info->kind = ID_INFO_KIND_LOCAL; |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 3055 | scope->num_pos_args = 1; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3056 | } |
| 3057 | |
| 3058 | if (scope->kind == SCOPE_LIST_COMP) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 3059 | EMIT_ARG(build_list, 0); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3060 | } else if (scope->kind == SCOPE_DICT_COMP) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 3061 | EMIT_ARG(build_map, 0); |
Damien George | e37dcaa | 2014-12-27 17:07:16 +0000 | [diff] [blame] | 3062 | #if MICROPY_PY_BUILTINS_SET |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3063 | } else if (scope->kind == SCOPE_SET_COMP) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 3064 | EMIT_ARG(build_set, 0); |
Damien George | e37dcaa | 2014-12-27 17:07:16 +0000 | [diff] [blame] | 3065 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3066 | } |
| 3067 | |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 3068 | compile_load_id(comp, qstr_arg); |
Damien George | a831243 | 2015-12-18 01:37:55 +0000 | [diff] [blame] | 3069 | compile_scope_comp_iter(comp, pns_comp_for, pns->nodes[0], 0); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3070 | |
| 3071 | if (scope->kind == SCOPE_GEN_EXPR) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 3072 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3073 | } |
| 3074 | EMIT(return_value); |
| 3075 | } else { |
| 3076 | assert(scope->kind == SCOPE_CLASS); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3077 | assert(MP_PARSE_NODE_IS_STRUCT(scope->pn)); |
| 3078 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn; |
| 3079 | assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3080 | |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3081 | if (comp->pass == MP_PASS_SCOPE) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3082 | bool added; |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 3083 | id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3084 | assert(added); |
| 3085 | id_info->kind = ID_INFO_KIND_LOCAL; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3086 | } |
| 3087 | |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 3088 | compile_load_id(comp, MP_QSTR___name__); |
| 3089 | compile_store_id(comp, MP_QSTR___module__); |
Damien George | 59fba2d | 2015-06-25 14:42:13 +0000 | [diff] [blame] | 3090 | EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 3091 | compile_store_id(comp, MP_QSTR___qualname__); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3092 | |
| 3093 | check_for_doc_string(comp, pns->nodes[2]); |
| 3094 | compile_node(comp, pns->nodes[2]); // 2 is class body |
| 3095 | |
Damien George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame] | 3096 | id_info_t *id = scope_find(scope, MP_QSTR___class__); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3097 | assert(id != NULL); |
| 3098 | if (id->kind == ID_INFO_KIND_LOCAL) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 3099 | EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3100 | } else { |
Damien George | 542bd6b | 2015-03-26 14:42:40 +0000 | [diff] [blame] | 3101 | EMIT_LOAD_FAST(MP_QSTR___class__, id->local_num); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3102 | } |
| 3103 | EMIT(return_value); |
| 3104 | } |
| 3105 | |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 3106 | EMIT(end_pass); |
Damien George | 8dcc0c7 | 2014-03-27 10:55:21 +0000 | [diff] [blame] | 3107 | |
| 3108 | // make sure we match all the exception levels |
| 3109 | assert(comp->cur_except_level == 0); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3110 | } |
| 3111 | |
Damien George | 094d450 | 2014-04-02 17:31:27 +0100 | [diff] [blame] | 3112 | #if MICROPY_EMIT_INLINE_THUMB |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3113 | // requires 3 passes: SCOPE, CODE_SIZE, EMIT |
Damien George | 1463c1f | 2014-04-25 23:52:57 +0100 | [diff] [blame] | 3114 | STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) { |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3115 | comp->pass = pass; |
| 3116 | comp->scope_cur = scope; |
| 3117 | comp->next_label = 1; |
| 3118 | |
| 3119 | if (scope->kind != SCOPE_FUNCTION) { |
Damien George | 01039b5 | 2014-12-21 17:44:27 +0000 | [diff] [blame] | 3120 | compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function"); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3121 | return; |
| 3122 | } |
| 3123 | |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3124 | if (comp->pass > MP_PASS_SCOPE) { |
Damien George | 8dfbd2d | 2015-02-13 01:00:51 +0000 | [diff] [blame] | 3125 | EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur, &comp->compile_error); |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 3126 | } |
| 3127 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3128 | // get the function definition parse node |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3129 | assert(MP_PARSE_NODE_IS_STRUCT(scope->pn)); |
| 3130 | mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn; |
| 3131 | assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3132 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3133 | //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3134 | |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 3135 | // parameters are in pns->nodes[1] |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3136 | if (comp->pass == MP_PASS_CODE_SIZE) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3137 | mp_parse_node_t *pn_params; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 3138 | int n_params = mp_parse_node_extract_list(&pns->nodes[1], PN_typedargslist, &pn_params); |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 3139 | scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params); |
Damien George | 8dfbd2d | 2015-02-13 01:00:51 +0000 | [diff] [blame] | 3140 | if (comp->compile_error != MP_OBJ_NULL) { |
| 3141 | goto inline_asm_error; |
| 3142 | } |
Damien | a2f2f7d | 2013-10-06 00:14:13 +0100 | [diff] [blame] | 3143 | } |
| 3144 | |
Damien George | 8f54c08 | 2016-01-15 15:20:43 +0000 | [diff] [blame] | 3145 | // pns->nodes[2] is function return annotation |
| 3146 | mp_uint_t type_sig = MP_NATIVE_TYPE_INT; |
| 3147 | mp_parse_node_t pn_annotation = pns->nodes[2]; |
| 3148 | if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) { |
| 3149 | // nodes[2] can be null or a test-expr |
| 3150 | if (MP_PARSE_NODE_IS_ID(pn_annotation)) { |
| 3151 | qstr ret_type = MP_PARSE_NODE_LEAF_ARG(pn_annotation); |
| 3152 | switch (ret_type) { |
| 3153 | case MP_QSTR_object: type_sig = MP_NATIVE_TYPE_OBJ; break; |
| 3154 | case MP_QSTR_bool: type_sig = MP_NATIVE_TYPE_BOOL; break; |
| 3155 | case MP_QSTR_int: type_sig = MP_NATIVE_TYPE_INT; break; |
| 3156 | case MP_QSTR_uint: type_sig = MP_NATIVE_TYPE_UINT; break; |
| 3157 | default: compile_syntax_error(comp, pn_annotation, "unknown type"); return; |
| 3158 | } |
| 3159 | } else { |
| 3160 | compile_syntax_error(comp, pn_annotation, "return annotation must be an identifier"); |
| 3161 | } |
| 3162 | } |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3163 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3164 | mp_parse_node_t pn_body = pns->nodes[3]; // body |
| 3165 | mp_parse_node_t *nodes; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 3166 | int num = mp_parse_node_extract_list(&pn_body, PN_suite_block_stmts, &nodes); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3167 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3168 | for (int i = 0; i < num; i++) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3169 | assert(MP_PARSE_NODE_IS_STRUCT(nodes[i])); |
| 3170 | mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i]; |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 3171 | if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) { |
| 3172 | // no instructions |
| 3173 | continue; |
Damien George | 3d7bf5d | 2015-02-16 17:46:28 +0000 | [diff] [blame] | 3174 | } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_expr_stmt) { |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 3175 | // not an instruction; error |
Damien George | 3d7bf5d | 2015-02-16 17:46:28 +0000 | [diff] [blame] | 3176 | not_an_instruction: |
Damien George | 3665d0b | 2015-03-03 17:11:18 +0000 | [diff] [blame] | 3177 | compile_syntax_error(comp, nodes[i], "expecting an assembler instruction"); |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 3178 | return; |
| 3179 | } |
Damien George | 3d7bf5d | 2015-02-16 17:46:28 +0000 | [diff] [blame] | 3180 | |
| 3181 | // check structure of parse node |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3182 | assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0])); |
Damien George | 3d7bf5d | 2015-02-16 17:46:28 +0000 | [diff] [blame] | 3183 | if (!MP_PARSE_NODE_IS_NULL(pns2->nodes[1])) { |
| 3184 | goto not_an_instruction; |
| 3185 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3186 | pns2 = (mp_parse_node_struct_t*)pns2->nodes[0]; |
pohmelie | 81ebba7 | 2016-01-27 23:23:11 +0300 | [diff] [blame] | 3187 | if (MP_PARSE_NODE_STRUCT_KIND(pns2) != PN_atom_expr_normal) { |
Damien George | 3d7bf5d | 2015-02-16 17:46:28 +0000 | [diff] [blame] | 3188 | goto not_an_instruction; |
| 3189 | } |
| 3190 | if (!MP_PARSE_NODE_IS_ID(pns2->nodes[0])) { |
| 3191 | goto not_an_instruction; |
| 3192 | } |
| 3193 | if (!MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren)) { |
| 3194 | goto not_an_instruction; |
| 3195 | } |
Damien George | 3d7bf5d | 2015-02-16 17:46:28 +0000 | [diff] [blame] | 3196 | |
| 3197 | // parse node looks like an instruction |
| 3198 | // get instruction name and args |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3199 | qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]); |
| 3200 | pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren |
| 3201 | mp_parse_node_t *pn_arg; |
Damien George | dfe944c | 2015-02-13 02:29:46 +0000 | [diff] [blame] | 3202 | int n_args = mp_parse_node_extract_list(&pns2->nodes[0], PN_arglist, &pn_arg); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3203 | |
| 3204 | // emit instructions |
Damien George | e5f8a77 | 2014-04-21 13:33:15 +0100 | [diff] [blame] | 3205 | if (op == MP_QSTR_label) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 3206 | if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) { |
Damien George | 3665d0b | 2015-03-03 17:11:18 +0000 | [diff] [blame] | 3207 | compile_syntax_error(comp, nodes[i], "'label' requires 1 argument"); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3208 | return; |
| 3209 | } |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 3210 | uint lab = comp_next_label(comp); |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3211 | if (pass > MP_PASS_SCOPE) { |
Damien George | 9c5cabb | 2015-03-03 17:08:02 +0000 | [diff] [blame] | 3212 | if (!EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]))) { |
| 3213 | compile_syntax_error(comp, nodes[i], "label redefined"); |
| 3214 | return; |
| 3215 | } |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3216 | } |
Damien George | e5f8a77 | 2014-04-21 13:33:15 +0100 | [diff] [blame] | 3217 | } else if (op == MP_QSTR_align) { |
| 3218 | if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) { |
Damien George | 3665d0b | 2015-03-03 17:11:18 +0000 | [diff] [blame] | 3219 | compile_syntax_error(comp, nodes[i], "'align' requires 1 argument"); |
Damien George | e5f8a77 | 2014-04-21 13:33:15 +0100 | [diff] [blame] | 3220 | return; |
| 3221 | } |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3222 | if (pass > MP_PASS_SCOPE) { |
Damien George | e5f8a77 | 2014-04-21 13:33:15 +0100 | [diff] [blame] | 3223 | EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0])); |
| 3224 | } |
| 3225 | } else if (op == MP_QSTR_data) { |
| 3226 | if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) { |
Damien George | 3665d0b | 2015-03-03 17:11:18 +0000 | [diff] [blame] | 3227 | compile_syntax_error(comp, nodes[i], "'data' requires at least 2 arguments"); |
Damien George | e5f8a77 | 2014-04-21 13:33:15 +0100 | [diff] [blame] | 3228 | return; |
| 3229 | } |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3230 | if (pass > MP_PASS_SCOPE) { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 3231 | mp_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]); |
Damien George | 50912e7 | 2015-01-20 11:55:10 +0000 | [diff] [blame] | 3232 | for (uint j = 1; j < n_args; j++) { |
| 3233 | if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[j])) { |
Damien George | 3665d0b | 2015-03-03 17:11:18 +0000 | [diff] [blame] | 3234 | compile_syntax_error(comp, nodes[i], "'data' requires integer arguments"); |
Damien George | e5f8a77 | 2014-04-21 13:33:15 +0100 | [diff] [blame] | 3235 | return; |
| 3236 | } |
Damien George | 50912e7 | 2015-01-20 11:55:10 +0000 | [diff] [blame] | 3237 | EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[j])); |
Damien George | e5f8a77 | 2014-04-21 13:33:15 +0100 | [diff] [blame] | 3238 | } |
| 3239 | } |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3240 | } else { |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3241 | if (pass > MP_PASS_SCOPE) { |
Damien George | b979122 | 2014-01-23 00:34:21 +0000 | [diff] [blame] | 3242 | EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3243 | } |
| 3244 | } |
Damien George | 8dfbd2d | 2015-02-13 01:00:51 +0000 | [diff] [blame] | 3245 | |
| 3246 | if (comp->compile_error != MP_OBJ_NULL) { |
| 3247 | pns = pns2; // this is the parse node that had the error |
| 3248 | goto inline_asm_error; |
| 3249 | } |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3250 | } |
| 3251 | |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3252 | if (comp->pass > MP_PASS_SCOPE) { |
Damien George | 8f54c08 | 2016-01-15 15:20:43 +0000 | [diff] [blame] | 3253 | EMIT_INLINE_ASM_ARG(end_pass, type_sig); |
Damien George | 8dfbd2d | 2015-02-13 01:00:51 +0000 | [diff] [blame] | 3254 | } |
| 3255 | |
| 3256 | if (comp->compile_error != MP_OBJ_NULL) { |
Damien George | cfc4c33 | 2015-07-29 22:16:01 +0000 | [diff] [blame] | 3257 | // inline assembler had an error; set line for its exception |
Damien George | 8dfbd2d | 2015-02-13 01:00:51 +0000 | [diff] [blame] | 3258 | inline_asm_error: |
Damien George | cfc4c33 | 2015-07-29 22:16:01 +0000 | [diff] [blame] | 3259 | comp->compile_error_line = pns->source_line; |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 3260 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3261 | } |
Damien George | 094d450 | 2014-04-02 17:31:27 +0100 | [diff] [blame] | 3262 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3263 | |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 3264 | STATIC void scope_compute_things(scope_t *scope) { |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 3265 | // in Micro Python we put the *x parameter after all other parameters (except **y) |
| 3266 | if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) { |
| 3267 | id_info_t *id_param = NULL; |
| 3268 | for (int i = scope->id_info_len - 1; i >= 0; i--) { |
| 3269 | id_info_t *id = &scope->id_info[i]; |
| 3270 | if (id->flags & ID_FLAG_IS_STAR_PARAM) { |
| 3271 | if (id_param != NULL) { |
| 3272 | // swap star param with last param |
| 3273 | id_info_t temp = *id_param; *id_param = *id; *id = temp; |
| 3274 | } |
| 3275 | break; |
| 3276 | } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) { |
| 3277 | id_param = id; |
| 3278 | } |
| 3279 | } |
| 3280 | } |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 3281 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3282 | // in functions, turn implicit globals into explicit globals |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 3283 | // compute the index of each local |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3284 | scope->num_locals = 0; |
| 3285 | for (int i = 0; i < scope->id_info_len; i++) { |
| 3286 | id_info_t *id = &scope->id_info[i]; |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 3287 | if (scope->kind == SCOPE_CLASS && id->qst == MP_QSTR___class__) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3288 | // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL |
| 3289 | continue; |
| 3290 | } |
Damien George | 3dea8c9 | 2016-09-30 12:34:05 +1000 | [diff] [blame^] | 3291 | if (SCOPE_IS_FUNC_LIKE(scope->kind) && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3292 | id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT; |
| 3293 | } |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 3294 | // params always count for 1 local, even if they are a cell |
Damien George | 11d8cd5 | 2014-04-09 14:42:51 +0100 | [diff] [blame] | 3295 | if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) { |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 3296 | id->local_num = scope->num_locals++; |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 3297 | } |
| 3298 | } |
| 3299 | |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 3300 | // compute the index of cell vars |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 3301 | for (int i = 0; i < scope->id_info_len; i++) { |
| 3302 | id_info_t *id = &scope->id_info[i]; |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 3303 | // in Micro Python the cells come right after the fast locals |
| 3304 | // parameters are not counted here, since they remain at the start |
| 3305 | // of the locals, even if they are cell vars |
Damien George | 11d8cd5 | 2014-04-09 14:42:51 +0100 | [diff] [blame] | 3306 | if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) { |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 3307 | id->local_num = scope->num_locals; |
| 3308 | scope->num_locals += 1; |
| 3309 | } |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 3310 | } |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 3311 | |
Damien George | 65dc960 | 2015-08-14 12:24:11 +0100 | [diff] [blame] | 3312 | // compute the index of free vars |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 3313 | // make sure they are in the order of the parent scope |
| 3314 | if (scope->parent != NULL) { |
| 3315 | int num_free = 0; |
| 3316 | for (int i = 0; i < scope->parent->id_info_len; i++) { |
| 3317 | id_info_t *id = &scope->parent->id_info[i]; |
| 3318 | if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) { |
| 3319 | for (int j = 0; j < scope->id_info_len; j++) { |
| 3320 | id_info_t *id2 = &scope->id_info[j]; |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 3321 | if (id2->kind == ID_INFO_KIND_FREE && id->qst == id2->qst) { |
Damien George | 11d8cd5 | 2014-04-09 14:42:51 +0100 | [diff] [blame] | 3322 | assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 3323 | // in Micro Python the frees come first, before the params |
| 3324 | id2->local_num = num_free; |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 3325 | num_free += 1; |
| 3326 | } |
| 3327 | } |
| 3328 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3329 | } |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 3330 | // in Micro Python shift all other locals after the free locals |
| 3331 | if (num_free > 0) { |
| 3332 | for (int i = 0; i < scope->id_info_len; i++) { |
| 3333 | id_info_t *id = &scope->id_info[i]; |
Damien George | 2bf7c09 | 2014-04-09 15:26:46 +0100 | [diff] [blame] | 3334 | if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) { |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 3335 | id->local_num += num_free; |
| 3336 | } |
| 3337 | } |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 3338 | scope->num_pos_args += num_free; // free vars are counted as params for passing them into the function |
Damien George | 6baf76e | 2013-12-30 22:32:17 +0000 | [diff] [blame] | 3339 | scope->num_locals += num_free; |
| 3340 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3341 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3342 | } |
| 3343 | |
Damien George | d4dba88 | 2015-11-13 13:38:28 +0000 | [diff] [blame] | 3344 | #if !MICROPY_PERSISTENT_CODE_SAVE |
| 3345 | STATIC |
| 3346 | #endif |
| 3347 | mp_raw_code_t *mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl) { |
Damien George | 9d5e5c0 | 2015-09-24 13:15:57 +0100 | [diff] [blame] | 3348 | // put compiler state on the stack, it's relatively small |
| 3349 | compiler_t comp_state = {0}; |
| 3350 | compiler_t *comp = &comp_state; |
| 3351 | |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 3352 | comp->source_file = source_file; |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 3353 | comp->is_repl = is_repl; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3354 | |
Damien George | 62a3a28 | 2015-03-01 12:04:05 +0000 | [diff] [blame] | 3355 | // create the module scope |
Damien George | 58e0f4a | 2015-09-23 10:50:43 +0100 | [diff] [blame] | 3356 | scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, parse_tree->root, emit_opt); |
Damien George | 62a3a28 | 2015-03-01 12:04:05 +0000 | [diff] [blame] | 3357 | |
Damien George | a210c77 | 2015-03-26 15:49:53 +0000 | [diff] [blame] | 3358 | // create standard emitter; it's used at least for MP_PASS_SCOPE |
Damien George | a210c77 | 2015-03-26 15:49:53 +0000 | [diff] [blame] | 3359 | emit_t *emit_bc = emit_bc_new(); |
Damien George | a210c77 | 2015-03-26 15:49:53 +0000 | [diff] [blame] | 3360 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3361 | // compile pass 1 |
Damien George | a210c77 | 2015-03-26 15:49:53 +0000 | [diff] [blame] | 3362 | comp->emit = emit_bc; |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 3363 | #if MICROPY_EMIT_NATIVE |
Damien George | a210c77 | 2015-03-26 15:49:53 +0000 | [diff] [blame] | 3364 | comp->emit_method_table = &emit_bc_method_table; |
| 3365 | #endif |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3366 | uint max_num_labels = 0; |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 3367 | for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) { |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 3368 | if (false) { |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 3369 | #if MICROPY_EMIT_INLINE_THUMB |
Damien George | 65cad12 | 2014-04-06 11:48:15 +0100 | [diff] [blame] | 3370 | } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) { |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3371 | compile_scope_inline_asm(comp, s, MP_PASS_SCOPE); |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 3372 | #endif |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3373 | } else { |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3374 | compile_scope(comp, s, MP_PASS_SCOPE); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3375 | } |
| 3376 | |
| 3377 | // update maximim number of labels needed |
| 3378 | if (comp->next_label > max_num_labels) { |
| 3379 | max_num_labels = comp->next_label; |
| 3380 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3381 | } |
| 3382 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3383 | // compute some things related to scope and identifiers |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 3384 | for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) { |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 3385 | scope_compute_things(s); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3386 | } |
| 3387 | |
Damien George | a210c77 | 2015-03-26 15:49:53 +0000 | [diff] [blame] | 3388 | // set max number of labels now that it's calculated |
Damien George | a210c77 | 2015-03-26 15:49:53 +0000 | [diff] [blame] | 3389 | emit_bc_set_max_num_labels(emit_bc, max_num_labels); |
Damien George | a210c77 | 2015-03-26 15:49:53 +0000 | [diff] [blame] | 3390 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3391 | // compile pass 2 and 3 |
Damien George | e67ed5d | 2014-01-04 13:55:24 +0000 | [diff] [blame] | 3392 | #if MICROPY_EMIT_NATIVE |
Damien | dc83382 | 2013-10-06 01:01:01 +0100 | [diff] [blame] | 3393 | emit_t *emit_native = NULL; |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 3394 | #endif |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 3395 | #if MICROPY_EMIT_INLINE_THUMB |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3396 | emit_inline_asm_t *emit_inline_thumb = NULL; |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 3397 | #endif |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 3398 | for (scope_t *s = comp->scope_head; s != NULL && comp->compile_error == MP_OBJ_NULL; s = s->next) { |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 3399 | if (false) { |
| 3400 | // dummy |
| 3401 | |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 3402 | #if MICROPY_EMIT_INLINE_THUMB |
Damien George | 65cad12 | 2014-04-06 11:48:15 +0100 | [diff] [blame] | 3403 | } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) { |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 3404 | // inline assembly for thumb |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3405 | if (emit_inline_thumb == NULL) { |
| 3406 | emit_inline_thumb = emit_inline_thumb_new(max_num_labels); |
| 3407 | } |
| 3408 | comp->emit = NULL; |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3409 | comp->emit_inline_asm = emit_inline_thumb; |
| 3410 | comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table; |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3411 | compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE); |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 3412 | if (comp->compile_error == MP_OBJ_NULL) { |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3413 | compile_scope_inline_asm(comp, s, MP_PASS_EMIT); |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 3414 | } |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 3415 | #endif |
| 3416 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3417 | } else { |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 3418 | |
| 3419 | // choose the emit type |
| 3420 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3421 | switch (s->emit_options) { |
Damien George | e67ed5d | 2014-01-04 13:55:24 +0000 | [diff] [blame] | 3422 | |
| 3423 | #if MICROPY_EMIT_NATIVE |
Damien George | 65cad12 | 2014-04-06 11:48:15 +0100 | [diff] [blame] | 3424 | case MP_EMIT_OPT_NATIVE_PYTHON: |
| 3425 | case MP_EMIT_OPT_VIPER: |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 3426 | #if MICROPY_EMIT_X64 |
Damien | dc83382 | 2013-10-06 01:01:01 +0100 | [diff] [blame] | 3427 | if (emit_native == NULL) { |
Damien George | c8b60f0 | 2015-04-20 13:29:31 +0000 | [diff] [blame] | 3428 | emit_native = emit_native_x64_new(&comp->compile_error, max_num_labels); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3429 | } |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 3430 | comp->emit_method_table = &emit_native_x64_method_table; |
Damien George | c90f59e | 2014-09-06 23:06:36 +0100 | [diff] [blame] | 3431 | #elif MICROPY_EMIT_X86 |
| 3432 | if (emit_native == NULL) { |
Damien George | c8b60f0 | 2015-04-20 13:29:31 +0000 | [diff] [blame] | 3433 | emit_native = emit_native_x86_new(&comp->compile_error, max_num_labels); |
Damien George | c90f59e | 2014-09-06 23:06:36 +0100 | [diff] [blame] | 3434 | } |
| 3435 | comp->emit_method_table = &emit_native_x86_method_table; |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 3436 | #elif MICROPY_EMIT_THUMB |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 3437 | if (emit_native == NULL) { |
Damien George | c8b60f0 | 2015-04-20 13:29:31 +0000 | [diff] [blame] | 3438 | emit_native = emit_native_thumb_new(&comp->compile_error, max_num_labels); |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 3439 | } |
| 3440 | comp->emit_method_table = &emit_native_thumb_method_table; |
Fabian Vogt | fe3d16e | 2014-08-16 22:55:53 +0200 | [diff] [blame] | 3441 | #elif MICROPY_EMIT_ARM |
| 3442 | if (emit_native == NULL) { |
Damien George | c8b60f0 | 2015-04-20 13:29:31 +0000 | [diff] [blame] | 3443 | emit_native = emit_native_arm_new(&comp->compile_error, max_num_labels); |
Fabian Vogt | fe3d16e | 2014-08-16 22:55:53 +0200 | [diff] [blame] | 3444 | } |
| 3445 | comp->emit_method_table = &emit_native_arm_method_table; |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 3446 | #endif |
| 3447 | comp->emit = emit_native; |
Damien George | a5190a7 | 2014-08-15 22:39:08 +0100 | [diff] [blame] | 3448 | EMIT_ARG(set_native_type, MP_EMIT_NATIVE_TYPE_ENABLE, s->emit_options == MP_EMIT_OPT_VIPER, 0); |
Damien | 7af3d19 | 2013-10-07 00:02:49 +0100 | [diff] [blame] | 3449 | break; |
Damien George | e67ed5d | 2014-01-04 13:55:24 +0000 | [diff] [blame] | 3450 | #endif // MICROPY_EMIT_NATIVE |
Damien | 7af3d19 | 2013-10-07 00:02:49 +0100 | [diff] [blame] | 3451 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3452 | default: |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3453 | comp->emit = emit_bc; |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 3454 | #if MICROPY_EMIT_NATIVE |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3455 | comp->emit_method_table = &emit_bc_method_table; |
Damien George | 4112590 | 2015-03-26 16:44:14 +0000 | [diff] [blame] | 3456 | #endif |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 3457 | break; |
| 3458 | } |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 3459 | |
Damien George | 1e1779e | 2015-01-14 00:20:28 +0000 | [diff] [blame] | 3460 | // need a pass to compute stack size |
| 3461 | compile_scope(comp, s, MP_PASS_STACK_SIZE); |
| 3462 | |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3463 | // second last pass: compute code size |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 3464 | if (comp->compile_error == MP_OBJ_NULL) { |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3465 | compile_scope(comp, s, MP_PASS_CODE_SIZE); |
| 3466 | } |
| 3467 | |
| 3468 | // final pass: emit code |
Damien George | a91ac20 | 2014-10-05 19:01:34 +0100 | [diff] [blame] | 3469 | if (comp->compile_error == MP_OBJ_NULL) { |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 3470 | compile_scope(comp, s, MP_PASS_EMIT); |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 3471 | } |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 3472 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3473 | } |
| 3474 | |
Damien George | cfc4c33 | 2015-07-29 22:16:01 +0000 | [diff] [blame] | 3475 | if (comp->compile_error != MP_OBJ_NULL) { |
| 3476 | // if there is no line number for the error then use the line |
| 3477 | // number for the start of this scope |
| 3478 | compile_error_set_line(comp, comp->scope_cur->pn); |
| 3479 | // add a traceback to the exception using relevant source info |
| 3480 | mp_obj_exception_add_traceback(comp->compile_error, comp->source_file, |
| 3481 | comp->compile_error_line, comp->scope_cur->simple_name); |
| 3482 | } |
| 3483 | |
Damien George | 41d02b6 | 2014-01-24 22:42:28 +0000 | [diff] [blame] | 3484 | // free the emitters |
Damien George | a210c77 | 2015-03-26 15:49:53 +0000 | [diff] [blame] | 3485 | |
Damien George | a210c77 | 2015-03-26 15:49:53 +0000 | [diff] [blame] | 3486 | emit_bc_free(emit_bc); |
Damien George | 41d02b6 | 2014-01-24 22:42:28 +0000 | [diff] [blame] | 3487 | #if MICROPY_EMIT_NATIVE |
| 3488 | if (emit_native != NULL) { |
| 3489 | #if MICROPY_EMIT_X64 |
| 3490 | emit_native_x64_free(emit_native); |
Damien George | c90f59e | 2014-09-06 23:06:36 +0100 | [diff] [blame] | 3491 | #elif MICROPY_EMIT_X86 |
| 3492 | emit_native_x86_free(emit_native); |
Damien George | 41d02b6 | 2014-01-24 22:42:28 +0000 | [diff] [blame] | 3493 | #elif MICROPY_EMIT_THUMB |
| 3494 | emit_native_thumb_free(emit_native); |
Fabian Vogt | fe3d16e | 2014-08-16 22:55:53 +0200 | [diff] [blame] | 3495 | #elif MICROPY_EMIT_ARM |
Damien George | dda4646 | 2014-09-03 22:47:23 +0100 | [diff] [blame] | 3496 | emit_native_arm_free(emit_native); |
Damien George | 41d02b6 | 2014-01-24 22:42:28 +0000 | [diff] [blame] | 3497 | #endif |
| 3498 | } |
| 3499 | #endif |
| 3500 | #if MICROPY_EMIT_INLINE_THUMB |
| 3501 | if (emit_inline_thumb != NULL) { |
| 3502 | emit_inline_thumb_free(emit_inline_thumb); |
| 3503 | } |
| 3504 | #endif |
Damien George | 41d02b6 | 2014-01-24 22:42:28 +0000 | [diff] [blame] | 3505 | |
Damien George | 52b5d76 | 2014-09-23 15:31:56 +0000 | [diff] [blame] | 3506 | // free the parse tree |
Damien George | 58e0f4a | 2015-09-23 10:50:43 +0100 | [diff] [blame] | 3507 | mp_parse_tree_clear(parse_tree); |
Damien George | 52b5d76 | 2014-09-23 15:31:56 +0000 | [diff] [blame] | 3508 | |
Damien George | 41d02b6 | 2014-01-24 22:42:28 +0000 | [diff] [blame] | 3509 | // free the scopes |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 3510 | mp_raw_code_t *outer_raw_code = module_scope->raw_code; |
Paul Sokolovsky | fd31358 | 2014-01-23 23:05:47 +0200 | [diff] [blame] | 3511 | for (scope_t *s = module_scope; s;) { |
| 3512 | scope_t *next = s->next; |
| 3513 | scope_free(s); |
| 3514 | s = next; |
| 3515 | } |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 3516 | |
Damien George | 9d5e5c0 | 2015-09-24 13:15:57 +0100 | [diff] [blame] | 3517 | if (comp->compile_error != MP_OBJ_NULL) { |
| 3518 | nlr_raise(comp->compile_error); |
Damien George | 1fb0317 | 2014-01-03 14:22:03 +0000 | [diff] [blame] | 3519 | } else { |
Damien George | d4dba88 | 2015-11-13 13:38:28 +0000 | [diff] [blame] | 3520 | return outer_raw_code; |
Damien George | 1fb0317 | 2014-01-03 14:22:03 +0000 | [diff] [blame] | 3521 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 3522 | } |
Damien George | d4dba88 | 2015-11-13 13:38:28 +0000 | [diff] [blame] | 3523 | |
| 3524 | mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl) { |
| 3525 | mp_raw_code_t *rc = mp_compile_to_raw_code(parse_tree, source_file, emit_opt, is_repl); |
| 3526 | // return function that executes the outer module |
| 3527 | return mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL); |
| 3528 | } |
Damien George | dd5353a | 2015-12-18 12:35:44 +0000 | [diff] [blame] | 3529 | |
| 3530 | #endif // MICROPY_ENABLE_COMPILER |