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