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