Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <stdint.h> |
| 4 | #include <stdio.h> |
| 5 | #include <string.h> |
| 6 | #include <assert.h> |
| 7 | |
| 8 | #include "misc.h" |
| 9 | #include "lexer.h" |
| 10 | #include "machine.h" |
| 11 | #include "parse.h" |
| 12 | #include "scope.h" |
| 13 | #include "compile.h" |
| 14 | #include "runtime.h" |
| 15 | #include "emit.h" |
| 16 | |
| 17 | // TODO need to mangle __attr names |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 18 | // TODO add #define to enable/disable CPython compatibility |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 19 | |
| 20 | typedef enum { |
| 21 | PN_none = 0, |
| 22 | #define DEF_RULE(rule, comp, kind, arg...) PN_##rule, |
| 23 | #include "grammar.h" |
| 24 | #undef DEF_RULE |
| 25 | PN_maximum_number_of, |
| 26 | } pn_kind_t; |
| 27 | |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 28 | #define EMIT(fun, arg...) (comp->emit_method_table->fun(comp->emit, ##arg)) |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 29 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 30 | #define EMIT_OPT_NONE (0) |
| 31 | #define EMIT_OPT_BYTE_CODE (1) |
| 32 | #define EMIT_OPT_NATIVE_PYTHON (2) |
Damien | 5bfb759 | 2013-10-05 18:41:24 +0100 | [diff] [blame^] | 33 | #define EMIT_OPT_ASM_THUMB (3) |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 34 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 35 | typedef struct _compiler_t { |
| 36 | qstr qstr___class__; |
| 37 | qstr qstr___locals__; |
| 38 | qstr qstr___name__; |
| 39 | qstr qstr___module__; |
| 40 | qstr qstr___qualname__; |
| 41 | qstr qstr___doc__; |
| 42 | qstr qstr_assertion_error; |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 43 | qstr qstr_micropython; |
| 44 | qstr qstr_native; |
Damien | 5bfb759 | 2013-10-05 18:41:24 +0100 | [diff] [blame^] | 45 | qstr qstr_asm_thumb; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 46 | |
| 47 | pass_kind_t pass; |
| 48 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 49 | int next_label; |
| 50 | int max_num_labels; |
| 51 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 52 | int break_label; |
| 53 | int continue_label; |
| 54 | int except_nest_level; |
| 55 | |
| 56 | int n_arg_keyword; |
| 57 | bool have_star_arg; |
| 58 | bool have_dbl_star_arg; |
| 59 | bool have_bare_star; |
| 60 | int param_pass; |
| 61 | int param_pass_num_dict_params; |
| 62 | int param_pass_num_default_params; |
| 63 | |
| 64 | scope_t *scope_head; |
| 65 | scope_t *scope_cur; |
| 66 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 67 | emit_t *emit; // current emitter |
| 68 | const emit_method_table_t *emit_method_table; // current emit method table |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 69 | } compiler_t; |
| 70 | |
| 71 | py_parse_node_t fold_constants(py_parse_node_t pn) { |
| 72 | if (PY_PARSE_NODE_IS_STRUCT(pn)) { |
| 73 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 74 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 75 | |
| 76 | // fold arguments first |
| 77 | for (int i = 0; i < n; i++) { |
| 78 | pns->nodes[i] = fold_constants(pns->nodes[i]); |
| 79 | } |
| 80 | |
| 81 | switch (PY_PARSE_NODE_STRUCT_KIND(pns)) { |
| 82 | case PN_shift_expr: |
| 83 | if (n == 3 && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) { |
| 84 | int arg0 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
| 85 | int arg1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[2]); |
| 86 | if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_DBL_LESS)) { |
| 87 | pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 << arg1); // XXX can overflow; enabled only to compare with CPython |
| 88 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_DBL_MORE)) { |
| 89 | pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 >> arg1); |
| 90 | } else { |
| 91 | // shouldn't happen |
| 92 | assert(0); |
| 93 | } |
| 94 | } |
| 95 | break; |
| 96 | |
| 97 | case PN_arith_expr: |
| 98 | // XXX can overflow; enabled only to compare with CPython |
| 99 | if (n == 3 && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) { |
| 100 | int arg0 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
| 101 | int arg1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[2]); |
| 102 | if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_PLUS)) { |
| 103 | pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 + arg1); |
| 104 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_MINUS)) { |
| 105 | pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 - arg1); |
| 106 | } else { |
| 107 | // shouldn't happen |
| 108 | assert(0); |
| 109 | } |
| 110 | } |
| 111 | break; |
| 112 | |
| 113 | case PN_term: |
| 114 | // XXX can overflow; enabled only to compare with CPython |
| 115 | if (n == 3 && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) { |
| 116 | int arg0 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
| 117 | int arg1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[2]); |
| 118 | if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_STAR)) { |
| 119 | pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 * arg1); |
| 120 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_SLASH)) { |
| 121 | ; // pass |
| 122 | //} else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_)) { |
| 123 | //pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg0 - arg1); |
| 124 | } else { |
| 125 | // shouldn't happen |
| 126 | assert(0); |
| 127 | } |
| 128 | } |
| 129 | break; |
| 130 | |
| 131 | case PN_factor_2: |
| 132 | if (PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) { |
| 133 | machine_int_t arg = PY_PARSE_NODE_LEAF_ARG(pns->nodes[1]); |
| 134 | if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_PLUS)) { |
| 135 | pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, arg); |
| 136 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_MINUS)) { |
| 137 | pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, -arg); |
| 138 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_TILDE)) { |
| 139 | pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, ~arg); |
| 140 | } else { |
| 141 | // shouldn't happen |
| 142 | assert(0); |
| 143 | } |
| 144 | } |
| 145 | break; |
| 146 | |
| 147 | case PN_power: |
| 148 | // XXX can overflow; enabled only to compare with CPython |
| 149 | if (PY_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && PY_PARSE_NODE_IS_NULL(pns->nodes[1]) && !PY_PARSE_NODE_IS_NULL(pns->nodes[2])) { |
| 150 | py_parse_node_struct_t* pns2 = (py_parse_node_struct_t*)pns->nodes[2]; |
| 151 | if (PY_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) { |
| 152 | int power = PY_PARSE_NODE_LEAF_ARG(pns2->nodes[0]); |
| 153 | if (power >= 0) { |
| 154 | int ans = 1; |
| 155 | int base = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
| 156 | for (; power > 0; power--) { |
| 157 | ans *= base; |
| 158 | } |
| 159 | pn = py_parse_node_new_leaf(PY_PARSE_NODE_SMALL_INT, ans); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | return pn; |
| 168 | } |
| 169 | |
| 170 | void compile_node(compiler_t *comp, py_parse_node_t pn); |
| 171 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 172 | static int comp_next_label(compiler_t *comp) { |
| 173 | return comp->next_label++; |
| 174 | } |
| 175 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 176 | static scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, py_parse_node_t pn, uint emit_options) { |
| 177 | scope_t *scope = scope_new(kind, pn, rt_get_new_unique_code_id(), emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 178 | scope->parent = comp->scope_cur; |
| 179 | scope->next = NULL; |
| 180 | if (comp->scope_head == NULL) { |
| 181 | comp->scope_head = scope; |
| 182 | } else { |
| 183 | scope_t *s = comp->scope_head; |
| 184 | while (s->next != NULL) { |
| 185 | s = s->next; |
| 186 | } |
| 187 | s->next = scope; |
| 188 | } |
| 189 | return scope; |
| 190 | } |
| 191 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 192 | static int list_len(py_parse_node_t pn, int pn_kind) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 193 | if (PY_PARSE_NODE_IS_NULL(pn)) { |
| 194 | return 0; |
| 195 | } else if (PY_PARSE_NODE_IS_LEAF(pn)) { |
| 196 | return 1; |
| 197 | } else { |
| 198 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 199 | if (PY_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) { |
| 200 | return 1; |
| 201 | } else { |
| 202 | return PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 207 | static void apply_to_single_or_list(compiler_t *comp, py_parse_node_t pn, int pn_list_kind, void (*f)(compiler_t*, py_parse_node_t)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 208 | if (PY_PARSE_NODE_IS_STRUCT(pn) && PY_PARSE_NODE_STRUCT_KIND((py_parse_node_struct_t*)pn) == pn_list_kind) { |
| 209 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 210 | int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 211 | for (int i = 0; i < num_nodes; i++) { |
| 212 | f(comp, pns->nodes[i]); |
| 213 | } |
| 214 | } else if (!PY_PARSE_NODE_IS_NULL(pn)) { |
| 215 | f(comp, pn); |
| 216 | } |
| 217 | } |
| 218 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 219 | static int list_get(py_parse_node_t *pn, int pn_kind, py_parse_node_t **nodes) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 220 | if (PY_PARSE_NODE_IS_NULL(*pn)) { |
| 221 | *nodes = NULL; |
| 222 | return 0; |
| 223 | } else if (PY_PARSE_NODE_IS_LEAF(*pn)) { |
| 224 | *nodes = pn; |
| 225 | return 1; |
| 226 | } else { |
| 227 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)(*pn); |
| 228 | if (PY_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) { |
| 229 | *nodes = pn; |
| 230 | return 1; |
| 231 | } else { |
| 232 | *nodes = pns->nodes; |
| 233 | return PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | void compile_do_nothing(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 239 | } |
| 240 | |
| 241 | void compile_generic_all_nodes(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 242 | int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 243 | for (int i = 0; i < num_nodes; i++) { |
| 244 | compile_node(comp, pns->nodes[i]); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | bool c_tuple_is_const(py_parse_node_t pn) { |
| 249 | if (!PY_PARSE_NODE_IS_LEAF(pn)) { |
| 250 | return false; |
| 251 | } |
| 252 | if (PY_PARSE_NODE_IS_ID(pn)) { |
| 253 | return false; |
| 254 | } |
| 255 | return true; |
| 256 | } |
| 257 | |
| 258 | void c_tuple_emit_const(compiler_t *comp, py_parse_node_t pn) { |
| 259 | assert(PY_PARSE_NODE_IS_LEAF(pn)); |
| 260 | int arg = PY_PARSE_NODE_LEAF_ARG(pn); |
| 261 | switch (PY_PARSE_NODE_LEAF_KIND(pn)) { |
| 262 | case PY_PARSE_NODE_ID: assert(0); |
| 263 | case PY_PARSE_NODE_SMALL_INT: EMIT(load_const_verbatim_int, arg); break; |
| 264 | case PY_PARSE_NODE_INTEGER: EMIT(load_const_verbatim_str, qstr_str(arg)); break; |
| 265 | case PY_PARSE_NODE_DECIMAL: EMIT(load_const_verbatim_str, qstr_str(arg)); break; |
| 266 | case PY_PARSE_NODE_STRING: EMIT(load_const_verbatim_quoted_str, arg, false); break; |
| 267 | case PY_PARSE_NODE_BYTES: EMIT(load_const_verbatim_quoted_str, arg, true); break; |
| 268 | case PY_PARSE_NODE_TOKEN: |
| 269 | switch (arg) { |
| 270 | case PY_TOKEN_KW_FALSE: EMIT(load_const_verbatim_str, "False"); break; |
| 271 | case PY_TOKEN_KW_NONE: EMIT(load_const_verbatim_str, "None"); break; |
| 272 | case PY_TOKEN_KW_TRUE: EMIT(load_const_verbatim_str, "True"); break; |
| 273 | default: assert(0); |
| 274 | } |
| 275 | break; |
| 276 | default: assert(0); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | // funnelling all tuple creations through this function and all this constant stuff is purely to agree with CPython |
| 281 | void c_tuple(compiler_t *comp, py_parse_node_t pn, py_parse_node_struct_t *pns_list) { |
| 282 | int n = 0; |
| 283 | if (pns_list != NULL) { |
| 284 | n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns_list); |
| 285 | } |
| 286 | int total = n; |
| 287 | bool is_const = true; |
| 288 | if (!PY_PARSE_NODE_IS_NULL(pn)) { |
| 289 | total += 1; |
| 290 | if (!c_tuple_is_const(pn)) { |
| 291 | is_const = false; |
| 292 | } |
| 293 | } |
| 294 | for (int i = 0; i < n; i++) { |
| 295 | if (!c_tuple_is_const(pns_list->nodes[i])) { |
| 296 | is_const = false; |
| 297 | break; |
| 298 | } |
| 299 | } |
| 300 | if (total > 0 && is_const) { |
| 301 | bool need_comma = false; |
| 302 | EMIT(load_const_verbatim_start); |
| 303 | EMIT(load_const_verbatim_str, "("); |
| 304 | if (!PY_PARSE_NODE_IS_NULL(pn)) { |
| 305 | c_tuple_emit_const(comp, pn); |
| 306 | need_comma = true; |
| 307 | } |
| 308 | for (int i = 0; i < n; i++) { |
| 309 | if (need_comma) { |
| 310 | EMIT(load_const_verbatim_str, ", "); |
| 311 | } |
| 312 | c_tuple_emit_const(comp, pns_list->nodes[i]); |
| 313 | need_comma = true; |
| 314 | } |
| 315 | if (total == 1) { |
| 316 | EMIT(load_const_verbatim_str, ",)"); |
| 317 | } else { |
| 318 | EMIT(load_const_verbatim_str, ")"); |
| 319 | } |
| 320 | EMIT(load_const_verbatim_end); |
| 321 | } else { |
| 322 | if (!PY_PARSE_NODE_IS_NULL(pn)) { |
| 323 | compile_node(comp, pn); |
| 324 | } |
| 325 | for (int i = 0; i < n; i++) { |
| 326 | compile_node(comp, pns_list->nodes[i]); |
| 327 | } |
| 328 | EMIT(build_tuple, total); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | void compile_generic_tuple(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 333 | // a simple tuple expression |
| 334 | /* |
| 335 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 336 | for (int i = 0; i < n; i++) { |
| 337 | compile_node(comp, pns->nodes[i]); |
| 338 | } |
| 339 | EMIT(build_tuple, n); |
| 340 | */ |
| 341 | c_tuple(comp, PY_PARSE_NODE_NULL, pns); |
| 342 | } |
| 343 | |
| 344 | bool node_is_const_false(py_parse_node_t pn) { |
| 345 | return PY_PARSE_NODE_IS_TOKEN_KIND(pn, PY_TOKEN_KW_FALSE); |
| 346 | // untested: || (PY_PARSE_NODE_IS_SMALL_INT(pn) && PY_PARSE_NODE_LEAF_ARG(pn) == 1); |
| 347 | } |
| 348 | |
| 349 | bool node_is_const_true(py_parse_node_t pn) { |
| 350 | return PY_PARSE_NODE_IS_TOKEN_KIND(pn, PY_TOKEN_KW_TRUE) || (PY_PARSE_NODE_IS_SMALL_INT(pn) && PY_PARSE_NODE_LEAF_ARG(pn) == 1); |
| 351 | } |
| 352 | |
| 353 | // having c_if_cond_2 and the is_nested variable is purely to match with CPython, which doesn't fully optimise not's |
| 354 | void c_if_cond_2(compiler_t *comp, py_parse_node_t pn, bool jump_if, int label, bool is_nested) { |
| 355 | if (node_is_const_false(pn)) { |
| 356 | if (jump_if == false) { |
| 357 | EMIT(jump, label); |
| 358 | } |
| 359 | return; |
| 360 | } else if (node_is_const_true(pn)) { |
| 361 | if (jump_if == true) { |
| 362 | EMIT(jump, label); |
| 363 | } |
| 364 | return; |
| 365 | } else if (PY_PARSE_NODE_IS_STRUCT(pn)) { |
| 366 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 367 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 368 | if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) { |
| 369 | if (jump_if == false) { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 370 | int label2 = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 371 | for (int i = 0; i < n - 1; i++) { |
| 372 | c_if_cond_2(comp, pns->nodes[i], true, label2, true); |
| 373 | } |
| 374 | c_if_cond_2(comp, pns->nodes[n - 1], false, label, true); |
| 375 | EMIT(label_assign, label2); |
| 376 | } else { |
| 377 | for (int i = 0; i < n; i++) { |
| 378 | c_if_cond_2(comp, pns->nodes[i], true, label, true); |
| 379 | } |
| 380 | } |
| 381 | return; |
| 382 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) { |
| 383 | if (jump_if == false) { |
| 384 | for (int i = 0; i < n; i++) { |
| 385 | c_if_cond_2(comp, pns->nodes[i], false, label, true); |
| 386 | } |
| 387 | } else { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 388 | int label2 = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 389 | for (int i = 0; i < n - 1; i++) { |
| 390 | c_if_cond_2(comp, pns->nodes[i], false, label2, true); |
| 391 | } |
| 392 | c_if_cond_2(comp, pns->nodes[n - 1], true, label, true); |
| 393 | EMIT(label_assign, label2); |
| 394 | } |
| 395 | return; |
| 396 | } else if (!is_nested && PY_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) { |
| 397 | c_if_cond_2(comp, pns->nodes[0], !jump_if, label, true); |
| 398 | return; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | // nothing special, fall back to default compiling for node and jump |
| 403 | compile_node(comp, pn); |
| 404 | if (jump_if == false) { |
| 405 | EMIT(pop_jump_if_false, label); |
| 406 | } else { |
| 407 | EMIT(pop_jump_if_true, label); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | void c_if_cond(compiler_t *comp, py_parse_node_t pn, bool jump_if, int label) { |
| 412 | c_if_cond_2(comp, pn, jump_if, label, false); |
| 413 | } |
| 414 | |
| 415 | typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t; |
| 416 | void c_assign(compiler_t *comp, py_parse_node_t pn, assign_kind_t kind); |
| 417 | |
| 418 | void c_assign_power(compiler_t *comp, py_parse_node_struct_t *pns, assign_kind_t assign_kind) { |
| 419 | if (assign_kind != ASSIGN_AUG_STORE) { |
| 420 | compile_node(comp, pns->nodes[0]); |
| 421 | } |
| 422 | |
| 423 | if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
| 424 | py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1]; |
| 425 | if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) { |
| 426 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns1); |
| 427 | if (assign_kind != ASSIGN_AUG_STORE) { |
| 428 | for (int i = 0; i < n - 1; i++) { |
| 429 | compile_node(comp, pns1->nodes[i]); |
| 430 | } |
| 431 | } |
| 432 | assert(PY_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1])); |
| 433 | pns1 = (py_parse_node_struct_t*)pns1->nodes[n - 1]; |
| 434 | } |
| 435 | if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) { |
| 436 | printf("SyntaxError: can't assign to function call\n"); |
| 437 | return; |
| 438 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) { |
| 439 | if (assign_kind == ASSIGN_AUG_STORE) { |
| 440 | EMIT(rot_three); |
| 441 | EMIT(store_subscr); |
| 442 | } else { |
| 443 | compile_node(comp, pns1->nodes[0]); |
| 444 | if (assign_kind == ASSIGN_AUG_LOAD) { |
| 445 | EMIT(dup_top_two); |
| 446 | EMIT(binary_op, RT_BINARY_OP_SUBSCR); |
| 447 | } else { |
| 448 | EMIT(store_subscr); |
| 449 | } |
| 450 | } |
| 451 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) { |
| 452 | assert(PY_PARSE_NODE_IS_ID(pns1->nodes[0])); |
| 453 | if (assign_kind == ASSIGN_AUG_LOAD) { |
| 454 | EMIT(dup_top); |
| 455 | EMIT(load_attr, PY_PARSE_NODE_LEAF_ARG(pns1->nodes[0])); |
| 456 | } else { |
| 457 | if (assign_kind == ASSIGN_AUG_STORE) { |
| 458 | EMIT(rot_two); |
| 459 | } |
| 460 | EMIT(store_attr, PY_PARSE_NODE_LEAF_ARG(pns1->nodes[0])); |
| 461 | } |
| 462 | } else { |
| 463 | // shouldn't happen |
| 464 | assert(0); |
| 465 | } |
| 466 | } else { |
| 467 | // shouldn't happen |
| 468 | assert(0); |
| 469 | } |
| 470 | |
| 471 | if (!PY_PARSE_NODE_IS_NULL(pns->nodes[2])) { |
| 472 | // SyntaxError, cannot assign |
| 473 | assert(0); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | void c_assign_tuple(compiler_t *comp, int n, py_parse_node_t *nodes) { |
| 478 | assert(n >= 0); |
| 479 | int have_star_index = -1; |
| 480 | for (int i = 0; i < n; i++) { |
| 481 | if (PY_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_star_expr)) { |
| 482 | if (have_star_index < 0) { |
| 483 | EMIT(unpack_ex, i, n - i - 1); |
| 484 | have_star_index = i; |
| 485 | } else { |
| 486 | printf("SyntaxError: two starred expressions in assignment\n"); |
| 487 | return; |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | if (have_star_index < 0) { |
| 492 | EMIT(unpack_sequence, n); |
| 493 | } |
| 494 | for (int i = 0; i < n; i++) { |
| 495 | if (i == have_star_index) { |
| 496 | c_assign(comp, ((py_parse_node_struct_t*)nodes[i])->nodes[0], ASSIGN_STORE); |
| 497 | } else { |
| 498 | c_assign(comp, nodes[i], ASSIGN_STORE); |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | // assigns top of stack to pn |
| 504 | void c_assign(compiler_t *comp, py_parse_node_t pn, assign_kind_t assign_kind) { |
| 505 | tail_recursion: |
| 506 | if (PY_PARSE_NODE_IS_NULL(pn)) { |
| 507 | assert(0); |
| 508 | } else if (PY_PARSE_NODE_IS_LEAF(pn)) { |
| 509 | if (PY_PARSE_NODE_IS_ID(pn)) { |
| 510 | int arg = PY_PARSE_NODE_LEAF_ARG(pn); |
| 511 | switch (assign_kind) { |
| 512 | case ASSIGN_STORE: |
| 513 | case ASSIGN_AUG_STORE: |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 514 | EMIT(store_id, arg); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 515 | break; |
| 516 | case ASSIGN_AUG_LOAD: |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 517 | EMIT(load_id, arg); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 518 | break; |
| 519 | } |
| 520 | } else { |
| 521 | printf("SyntaxError: can't assign to literal\n"); |
| 522 | return; |
| 523 | } |
| 524 | } else { |
| 525 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 526 | switch (PY_PARSE_NODE_STRUCT_KIND(pns)) { |
| 527 | case PN_power: |
| 528 | // lhs is an index or attribute |
| 529 | c_assign_power(comp, pns, assign_kind); |
| 530 | break; |
| 531 | |
| 532 | case PN_testlist_star_expr: |
| 533 | case PN_exprlist: |
| 534 | // lhs is a tuple |
| 535 | if (assign_kind != ASSIGN_STORE) { |
| 536 | goto bad_aug; |
| 537 | } |
| 538 | c_assign_tuple(comp, PY_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes); |
| 539 | break; |
| 540 | |
| 541 | case PN_atom_paren: |
| 542 | // lhs is something in parenthesis |
| 543 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
| 544 | // empty tuple |
| 545 | printf("SyntaxError: can't assign to ()\n"); |
| 546 | return; |
| 547 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) { |
| 548 | pns = (py_parse_node_struct_t*)pns->nodes[0]; |
| 549 | goto testlist_comp; |
| 550 | } else { |
| 551 | // parenthesis around 1 item, is just that item |
| 552 | pn = pns->nodes[0]; |
| 553 | goto tail_recursion; |
| 554 | } |
| 555 | break; |
| 556 | |
| 557 | case PN_atom_bracket: |
| 558 | // lhs is something in brackets |
| 559 | if (assign_kind != ASSIGN_STORE) { |
| 560 | goto bad_aug; |
| 561 | } |
| 562 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
| 563 | // empty list, assignment allowed |
| 564 | c_assign_tuple(comp, 0, NULL); |
| 565 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) { |
| 566 | pns = (py_parse_node_struct_t*)pns->nodes[0]; |
| 567 | goto testlist_comp; |
| 568 | } else { |
| 569 | // brackets around 1 item |
| 570 | c_assign_tuple(comp, 1, &pns->nodes[0]); |
| 571 | } |
| 572 | break; |
| 573 | |
| 574 | default: |
| 575 | printf("unknown assign, %u\n", (uint)PY_PARSE_NODE_STRUCT_KIND(pns)); |
| 576 | assert(0); |
| 577 | } |
| 578 | return; |
| 579 | |
| 580 | testlist_comp: |
| 581 | // lhs is a sequence |
| 582 | if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
| 583 | py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[1]; |
| 584 | if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) { |
| 585 | // sequence of one item, with trailing comma |
| 586 | assert(PY_PARSE_NODE_IS_NULL(pns2->nodes[0])); |
| 587 | c_assign_tuple(comp, 1, &pns->nodes[0]); |
| 588 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) { |
| 589 | // sequence of many items |
| 590 | // TODO call c_assign_tuple instead |
| 591 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns2); |
| 592 | EMIT(unpack_sequence, 1 + n); |
| 593 | c_assign(comp, pns->nodes[0], ASSIGN_STORE); |
| 594 | for (int i = 0; i < n; i++) { |
| 595 | c_assign(comp, pns2->nodes[i], ASSIGN_STORE); |
| 596 | } |
| 597 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) { |
| 598 | // TODO not implemented |
| 599 | assert(0); |
| 600 | } else { |
| 601 | // sequence with 2 items |
| 602 | goto sequence_with_2_items; |
| 603 | } |
| 604 | } else { |
| 605 | // sequence with 2 items |
| 606 | sequence_with_2_items: |
| 607 | c_assign_tuple(comp, 2, pns->nodes); |
| 608 | } |
| 609 | return; |
| 610 | } |
| 611 | return; |
| 612 | |
| 613 | bad_aug: |
| 614 | printf("SyntaxError: illegal expression for augmented assignment\n"); |
| 615 | } |
| 616 | |
| 617 | // stuff for lambda and comprehensions and generators |
| 618 | void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_dict_params, int n_default_params) { |
| 619 | // make closed over variables, if any |
| 620 | int nfree = 0; |
| 621 | if (comp->scope_cur->kind != SCOPE_MODULE) { |
| 622 | for (int i = 0; i < this_scope->id_info_len; i++) { |
| 623 | id_info_t *id_info = &this_scope->id_info[i]; |
| 624 | if (id_info->kind == ID_INFO_KIND_FREE) { |
| 625 | EMIT(load_closure, id_info->qstr); |
| 626 | nfree += 1; |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | if (nfree > 0) { |
| 631 | EMIT(build_tuple, nfree); |
| 632 | } |
| 633 | |
| 634 | // make the function/closure |
| 635 | if (nfree == 0) { |
| 636 | EMIT(make_function, this_scope, n_dict_params, n_default_params); |
| 637 | } else { |
| 638 | EMIT(make_closure, this_scope, n_dict_params, n_default_params); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | void compile_funcdef_param(compiler_t *comp, py_parse_node_t pn) { |
| 643 | assert(PY_PARSE_NODE_IS_STRUCT(pn)); |
| 644 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 645 | if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_name) { |
| 646 | if (!PY_PARSE_NODE_IS_NULL(pns->nodes[2])) { |
| 647 | // this parameter has a default value |
| 648 | // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why |
| 649 | if (comp->have_bare_star) { |
| 650 | comp->param_pass_num_dict_params += 1; |
| 651 | if (comp->param_pass == 1) { |
| 652 | EMIT(load_const_id, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0])); |
| 653 | compile_node(comp, pns->nodes[2]); |
| 654 | } |
| 655 | } else { |
| 656 | comp->param_pass_num_default_params += 1; |
| 657 | if (comp->param_pass == 2) { |
| 658 | compile_node(comp, pns->nodes[2]); |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_typedargslist_star) { |
| 663 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
| 664 | // bare star |
| 665 | comp->have_bare_star = true; |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | // leaves function object on stack |
| 671 | // returns function name |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 672 | qstr compile_funcdef_helper(compiler_t *comp, py_parse_node_struct_t *pns, uint emit_options) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 673 | if (comp->pass == PASS_1) { |
| 674 | // create a new scope for this function |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 675 | scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (py_parse_node_t)pns, emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 676 | // store the function scope so the compiling function can use it at each pass |
| 677 | pns->nodes[4] = (py_parse_node_t)s; |
| 678 | } |
| 679 | |
| 680 | // save variables (probably don't need to do this, since we can't have nested definitions..?) |
| 681 | bool old_have_bare_star = comp->have_bare_star; |
| 682 | int old_param_pass = comp->param_pass; |
| 683 | int old_param_pass_num_dict_params = comp->param_pass_num_dict_params; |
| 684 | int old_param_pass_num_default_params = comp->param_pass_num_default_params; |
| 685 | |
| 686 | // compile default parameters |
| 687 | comp->have_bare_star = false; |
| 688 | comp->param_pass = 1; // pass 1 does any default parameters after bare star |
| 689 | comp->param_pass_num_dict_params = 0; |
| 690 | comp->param_pass_num_default_params = 0; |
| 691 | apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param); |
| 692 | comp->have_bare_star = false; |
| 693 | comp->param_pass = 2; // pass 2 does any default parameters before bare star |
| 694 | comp->param_pass_num_dict_params = 0; |
| 695 | comp->param_pass_num_default_params = 0; |
| 696 | apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param); |
| 697 | |
| 698 | // get the scope for this function |
| 699 | scope_t *fscope = (scope_t*)pns->nodes[4]; |
| 700 | |
| 701 | // make the function |
| 702 | close_over_variables_etc(comp, fscope, comp->param_pass_num_dict_params, comp->param_pass_num_default_params); |
| 703 | |
| 704 | // restore variables |
| 705 | comp->have_bare_star = old_have_bare_star; |
| 706 | comp->param_pass = old_param_pass; |
| 707 | comp->param_pass_num_dict_params = old_param_pass_num_dict_params; |
| 708 | comp->param_pass_num_default_params = old_param_pass_num_default_params; |
| 709 | |
| 710 | // return its name (the 'f' in "def f(...):") |
| 711 | return fscope->simple_name; |
| 712 | } |
| 713 | |
| 714 | // leaves class object on stack |
| 715 | // returns class name |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 716 | qstr compile_classdef_helper(compiler_t *comp, py_parse_node_struct_t *pns, uint emit_options) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 717 | if (comp->pass == PASS_1) { |
| 718 | // create a new scope for this class |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 719 | scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (py_parse_node_t)pns, emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 720 | // store the class scope so the compiling function can use it at each pass |
| 721 | pns->nodes[3] = (py_parse_node_t)s; |
| 722 | } |
| 723 | |
| 724 | EMIT(load_build_class); |
| 725 | |
| 726 | // scope for this class |
| 727 | scope_t *cscope = (scope_t*)pns->nodes[3]; |
| 728 | |
| 729 | // compile the class |
| 730 | close_over_variables_etc(comp, cscope, 0, 0); |
| 731 | |
| 732 | // get its name |
| 733 | EMIT(load_const_id, cscope->simple_name); |
| 734 | |
| 735 | // nodes[1] has parent classes, if any |
| 736 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[1])) { |
| 737 | // no parent classes |
| 738 | EMIT(call_function, 2, 0, false, false); |
| 739 | } else { |
| 740 | // have a parent class or classes |
| 741 | // TODO what if we have, eg, *a or **a in the parent list? |
| 742 | compile_node(comp, pns->nodes[1]); |
| 743 | EMIT(call_function, 2 + list_len(pns->nodes[1], PN_arglist), 0, false, false); |
| 744 | } |
| 745 | |
| 746 | // return its name (the 'C' in class C(...):") |
| 747 | return cscope->simple_name; |
| 748 | } |
| 749 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 750 | // returns true if it was a built-in decorator (even if the built-in had an error) |
| 751 | static bool compile_built_in_decorator(compiler_t *comp, int name_len, py_parse_node_t *name_nodes, uint *emit_options) { |
| 752 | if (PY_PARSE_NODE_LEAF_ARG(name_nodes[0]) != comp->qstr_micropython) { |
| 753 | return false; |
| 754 | } |
| 755 | |
| 756 | if (name_len != 2) { |
| 757 | printf("SyntaxError: invalid micropython decorator\n"); |
| 758 | return true; |
| 759 | } |
| 760 | |
| 761 | qstr attr = PY_PARSE_NODE_LEAF_ARG(name_nodes[1]); |
| 762 | if (attr == comp->qstr_native) { |
| 763 | *emit_options = EMIT_OPT_NATIVE_PYTHON; |
Damien | 5bfb759 | 2013-10-05 18:41:24 +0100 | [diff] [blame^] | 764 | } else if (attr == comp->qstr_asm_thumb) { |
| 765 | *emit_options = EMIT_OPT_ASM_THUMB; |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 766 | } else { |
| 767 | printf("SyntaxError: invalid micropython decorator\n"); |
| 768 | } |
| 769 | |
| 770 | return true; |
| 771 | } |
| 772 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 773 | void compile_decorated(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 774 | // get the list of decorators |
| 775 | py_parse_node_t *nodes; |
| 776 | int n = list_get(&pns->nodes[0], PN_decorators, &nodes); |
| 777 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 778 | // inherit emit options for this function/class definition |
| 779 | uint emit_options = comp->scope_cur->emit_options; |
| 780 | |
| 781 | // compile each decorator |
| 782 | int num_built_in_decorators = 0; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 783 | for (int i = 0; i < n; i++) { |
| 784 | assert(PY_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be |
| 785 | py_parse_node_struct_t *pns_decorator = (py_parse_node_struct_t*)nodes[i]; |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 786 | |
| 787 | // nodes[0] contains the decorator function, which is a dotted name |
| 788 | py_parse_node_t *name_nodes; |
| 789 | int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes); |
| 790 | |
| 791 | // check for built-in decorators |
| 792 | if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) { |
| 793 | // this was a built-in |
| 794 | num_built_in_decorators += 1; |
| 795 | |
| 796 | } else { |
| 797 | // not a built-in, compile normally |
| 798 | |
| 799 | // compile the decorator function |
| 800 | compile_node(comp, name_nodes[0]); |
| 801 | for (int i = 1; i < name_len; i++) { |
| 802 | assert(PY_PARSE_NODE_IS_ID(name_nodes[i])); // should be |
| 803 | EMIT(load_attr, PY_PARSE_NODE_LEAF_ARG(name_nodes[i])); |
| 804 | } |
| 805 | |
| 806 | // nodes[1] contains arguments to the decorator function, if any |
| 807 | if (!PY_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) { |
| 808 | // call the decorator function with the arguments in nodes[1] |
| 809 | compile_node(comp, pns_decorator->nodes[1]); |
| 810 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | |
| 814 | // compile the body (funcdef or classdef) and get its name |
| 815 | py_parse_node_struct_t *pns_body = (py_parse_node_struct_t*)pns->nodes[1]; |
| 816 | qstr body_name = 0; |
| 817 | if (PY_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) { |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 818 | body_name = compile_funcdef_helper(comp, pns_body, emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 819 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) { |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 820 | body_name = compile_classdef_helper(comp, pns_body, emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 821 | } else { |
| 822 | // shouldn't happen |
| 823 | assert(0); |
| 824 | } |
| 825 | |
| 826 | // call each decorator |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 827 | for (int i = 0; i < n - num_built_in_decorators; i++) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 828 | EMIT(call_function, 1, 0, false, false); |
| 829 | } |
| 830 | |
| 831 | // store func/class object into name |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 832 | EMIT(store_id, body_name); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | void compile_funcdef(compiler_t *comp, py_parse_node_struct_t *pns) { |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 836 | qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 837 | // store function object into function name |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 838 | EMIT(store_id, fname); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | void c_del_stmt(compiler_t *comp, py_parse_node_t pn) { |
| 842 | if (PY_PARSE_NODE_IS_ID(pn)) { |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 843 | EMIT(delete_id, PY_PARSE_NODE_LEAF_ARG(pn)); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 844 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) { |
| 845 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 846 | |
| 847 | compile_node(comp, pns->nodes[0]); // base of the power node |
| 848 | |
| 849 | if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
| 850 | py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1]; |
| 851 | if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) { |
| 852 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns1); |
| 853 | for (int i = 0; i < n - 1; i++) { |
| 854 | compile_node(comp, pns1->nodes[i]); |
| 855 | } |
| 856 | assert(PY_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1])); |
| 857 | pns1 = (py_parse_node_struct_t*)pns1->nodes[n - 1]; |
| 858 | } |
| 859 | if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) { |
| 860 | // SyntaxError: can't delete a function call |
| 861 | assert(0); |
| 862 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) { |
| 863 | compile_node(comp, pns1->nodes[0]); |
| 864 | EMIT(delete_subscr); |
| 865 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) { |
| 866 | assert(PY_PARSE_NODE_IS_ID(pns1->nodes[0])); |
| 867 | EMIT(delete_attr, PY_PARSE_NODE_LEAF_ARG(pns1->nodes[0])); |
| 868 | } else { |
| 869 | // shouldn't happen |
| 870 | assert(0); |
| 871 | } |
| 872 | } else { |
| 873 | // shouldn't happen |
| 874 | assert(0); |
| 875 | } |
| 876 | |
| 877 | if (!PY_PARSE_NODE_IS_NULL(pns->nodes[2])) { |
| 878 | // SyntaxError, cannot delete |
| 879 | assert(0); |
| 880 | } |
| 881 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) { |
| 882 | pn = ((py_parse_node_struct_t*)pn)->nodes[0]; |
| 883 | if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) { |
| 884 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 885 | // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp |
| 886 | |
| 887 | if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
| 888 | py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1]; |
| 889 | if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) { |
| 890 | // sequence of one item, with trailing comma |
| 891 | assert(PY_PARSE_NODE_IS_NULL(pns1->nodes[0])); |
| 892 | c_del_stmt(comp, pns->nodes[0]); |
| 893 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) { |
| 894 | // sequence of many items |
| 895 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns1); |
| 896 | c_del_stmt(comp, pns->nodes[0]); |
| 897 | for (int i = 0; i < n; i++) { |
| 898 | c_del_stmt(comp, pns1->nodes[i]); |
| 899 | } |
| 900 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) { |
| 901 | // TODO not implemented; can't del comprehension? |
| 902 | assert(0); |
| 903 | } else { |
| 904 | // sequence with 2 items |
| 905 | goto sequence_with_2_items; |
| 906 | } |
| 907 | } else { |
| 908 | // sequence with 2 items |
| 909 | sequence_with_2_items: |
| 910 | c_del_stmt(comp, pns->nodes[0]); |
| 911 | c_del_stmt(comp, pns->nodes[1]); |
| 912 | } |
| 913 | } else { |
| 914 | // tuple with 1 element |
| 915 | c_del_stmt(comp, pn); |
| 916 | } |
| 917 | } else { |
| 918 | // not implemented |
| 919 | assert(0); |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | void compile_del_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 924 | apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt); |
| 925 | } |
| 926 | |
| 927 | void compile_break_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 928 | if (comp->break_label == 0) { |
| 929 | printf("ERROR: cannot break from here\n"); |
| 930 | } |
| 931 | EMIT(break_loop, comp->break_label); |
| 932 | } |
| 933 | |
| 934 | void compile_continue_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 935 | if (comp->continue_label == 0) { |
| 936 | printf("ERROR: cannot continue from here\n"); |
| 937 | } |
| 938 | if (comp->except_nest_level > 0) { |
| 939 | EMIT(continue_loop, comp->continue_label); |
| 940 | } else { |
| 941 | EMIT(jump, comp->continue_label); |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | void compile_return_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 946 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
| 947 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 948 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) { |
| 949 | // special case when returning an if-expression; to match CPython optimisation |
| 950 | py_parse_node_struct_t *pns_test_if_expr = (py_parse_node_struct_t*)pns->nodes[0]; |
| 951 | py_parse_node_struct_t *pns_test_if_else = (py_parse_node_struct_t*)pns_test_if_expr->nodes[1]; |
| 952 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 953 | int l_fail = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 954 | c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition |
| 955 | compile_node(comp, pns_test_if_expr->nodes[0]); // success value |
| 956 | EMIT(return_value); |
| 957 | EMIT(label_assign, l_fail); |
| 958 | compile_node(comp, pns_test_if_else->nodes[1]); // failure value |
| 959 | } else { |
| 960 | compile_node(comp, pns->nodes[0]); |
| 961 | } |
| 962 | EMIT(return_value); |
| 963 | } |
| 964 | |
| 965 | void compile_yield_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 966 | compile_node(comp, pns->nodes[0]); |
| 967 | EMIT(pop_top); |
| 968 | } |
| 969 | |
| 970 | void compile_raise_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 971 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
| 972 | // raise |
| 973 | EMIT(raise_varargs, 0); |
| 974 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) { |
| 975 | // raise x from y |
| 976 | pns = (py_parse_node_struct_t*)pns->nodes[0]; |
| 977 | compile_node(comp, pns->nodes[0]); |
| 978 | compile_node(comp, pns->nodes[1]); |
| 979 | EMIT(raise_varargs, 2); |
| 980 | } else { |
| 981 | // raise x |
| 982 | compile_node(comp, pns->nodes[0]); |
| 983 | EMIT(raise_varargs, 1); |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | // q1 holds the base, q2 the full name |
| 988 | // eg a -> q1=q2=a |
| 989 | // a.b.c -> q1=a, q2=a.b.c |
| 990 | void do_import_name(compiler_t *comp, py_parse_node_t pn, qstr *q1, qstr *q2) { |
| 991 | bool is_as = false; |
| 992 | if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) { |
| 993 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 994 | // a name of the form x as y; unwrap it |
| 995 | *q1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[1]); |
| 996 | pn = pns->nodes[0]; |
| 997 | is_as = true; |
| 998 | } |
| 999 | if (PY_PARSE_NODE_IS_ID(pn)) { |
| 1000 | // just a simple name |
| 1001 | *q2 = PY_PARSE_NODE_LEAF_ARG(pn); |
| 1002 | if (!is_as) { |
| 1003 | *q1 = *q2; |
| 1004 | } |
| 1005 | EMIT(import_name, *q2); |
| 1006 | } else if (PY_PARSE_NODE_IS_STRUCT(pn)) { |
| 1007 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 1008 | if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) { |
| 1009 | // a name of the form a.b.c |
| 1010 | if (!is_as) { |
| 1011 | *q1 = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
| 1012 | } |
| 1013 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 1014 | int len = n - 1; |
| 1015 | for (int i = 0; i < n; i++) { |
| 1016 | len += strlen(qstr_str(PY_PARSE_NODE_LEAF_ARG(pns->nodes[i]))); |
| 1017 | } |
| 1018 | char *str = m_new(char, len + 1); |
| 1019 | str[0] = 0; |
| 1020 | for (int i = 0; i < n; i++) { |
| 1021 | if (i > 0) { |
| 1022 | strcat(str, "."); |
| 1023 | } |
| 1024 | strcat(str, qstr_str(PY_PARSE_NODE_LEAF_ARG(pns->nodes[i]))); |
| 1025 | } |
| 1026 | *q2 = qstr_from_str_take(str); |
| 1027 | EMIT(import_name, *q2); |
| 1028 | if (is_as) { |
| 1029 | for (int i = 1; i < n; i++) { |
| 1030 | EMIT(load_attr, PY_PARSE_NODE_LEAF_ARG(pns->nodes[i])); |
| 1031 | } |
| 1032 | } |
| 1033 | } else { |
| 1034 | // TODO not implemented |
| 1035 | assert(0); |
| 1036 | } |
| 1037 | } else { |
| 1038 | // TODO not implemented |
| 1039 | assert(0); |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | void compile_dotted_as_name(compiler_t *comp, py_parse_node_t pn) { |
| 1044 | EMIT(load_const_small_int, 0); // ?? |
| 1045 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 1046 | qstr q1, q2; |
| 1047 | do_import_name(comp, pn, &q1, &q2); |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 1048 | EMIT(store_id, q1); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | void compile_import_name(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1052 | apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name); |
| 1053 | } |
| 1054 | |
| 1055 | void compile_import_from(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1056 | if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], PY_TOKEN_OP_STAR)) { |
| 1057 | EMIT(load_const_small_int, 0); // what's this for?? |
| 1058 | EMIT(load_const_verbatim_start); |
| 1059 | EMIT(load_const_verbatim_str, "('*',)"); |
| 1060 | EMIT(load_const_verbatim_end); |
| 1061 | qstr dummy_q, id1; |
| 1062 | do_import_name(comp, pns->nodes[0], &dummy_q, &id1); |
| 1063 | EMIT(import_star); |
| 1064 | } else { |
| 1065 | py_parse_node_t *pn_nodes; |
| 1066 | int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes); |
| 1067 | |
| 1068 | EMIT(load_const_small_int, 0); // what's this for?? |
| 1069 | EMIT(load_const_verbatim_start); |
| 1070 | EMIT(load_const_verbatim_str, "("); |
| 1071 | for (int i = 0; i < n; i++) { |
| 1072 | assert(PY_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name)); |
| 1073 | py_parse_node_struct_t *pns3 = (py_parse_node_struct_t*)pn_nodes[i]; |
| 1074 | qstr id2 = PY_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id |
| 1075 | if (i > 0) { |
| 1076 | EMIT(load_const_verbatim_str, ", "); |
| 1077 | } |
| 1078 | EMIT(load_const_verbatim_str, "'"); |
| 1079 | EMIT(load_const_verbatim_str, qstr_str(id2)); |
| 1080 | EMIT(load_const_verbatim_str, "'"); |
| 1081 | } |
| 1082 | if (n == 1) { |
| 1083 | EMIT(load_const_verbatim_str, ","); |
| 1084 | } |
| 1085 | EMIT(load_const_verbatim_str, ")"); |
| 1086 | EMIT(load_const_verbatim_end); |
| 1087 | qstr dummy_q, id1; |
| 1088 | do_import_name(comp, pns->nodes[0], &dummy_q, &id1); |
| 1089 | for (int i = 0; i < n; i++) { |
| 1090 | assert(PY_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name)); |
| 1091 | py_parse_node_struct_t *pns3 = (py_parse_node_struct_t*)pn_nodes[i]; |
| 1092 | qstr id2 = PY_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id |
| 1093 | EMIT(import_from, id2); |
| 1094 | if (PY_PARSE_NODE_IS_NULL(pns3->nodes[1])) { |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 1095 | EMIT(store_id, id2); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1096 | } else { |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 1097 | EMIT(store_id, PY_PARSE_NODE_LEAF_ARG(pns3->nodes[1])); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1098 | } |
| 1099 | } |
| 1100 | EMIT(pop_top); |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | void compile_global_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 1105 | if (comp->pass == PASS_1) { |
| 1106 | if (PY_PARSE_NODE_IS_LEAF(pns->nodes[0])) { |
| 1107 | scope_declare_global(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0])); |
| 1108 | } else { |
| 1109 | pns = (py_parse_node_struct_t*)pns->nodes[0]; |
| 1110 | int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 1111 | for (int i = 0; i < num_nodes; i++) { |
| 1112 | scope_declare_global(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[i])); |
| 1113 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | void compile_nonlocal_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 1119 | if (comp->pass == PASS_1) { |
| 1120 | if (PY_PARSE_NODE_IS_LEAF(pns->nodes[0])) { |
| 1121 | scope_declare_nonlocal(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0])); |
| 1122 | } else { |
| 1123 | pns = (py_parse_node_struct_t*)pns->nodes[0]; |
| 1124 | int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 1125 | for (int i = 0; i < num_nodes; i++) { |
| 1126 | scope_declare_nonlocal(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[i])); |
| 1127 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1128 | } |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | void compile_assert_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1133 | int l_end = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1134 | c_if_cond(comp, pns->nodes[0], true, l_end); |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 1135 | EMIT(load_id, comp->qstr_assertion_error); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1136 | if (!PY_PARSE_NODE_IS_NULL(pns->nodes[1])) { |
| 1137 | // assertion message |
| 1138 | compile_node(comp, pns->nodes[1]); |
| 1139 | EMIT(call_function, 1, 0, false, false); |
| 1140 | } |
| 1141 | EMIT(raise_varargs, 1); |
| 1142 | EMIT(label_assign, l_end); |
| 1143 | } |
| 1144 | |
| 1145 | void compile_if_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1146 | // TODO proper and/or short circuiting |
| 1147 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1148 | int l_end = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1149 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1150 | int l_fail = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1151 | c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition |
| 1152 | |
| 1153 | compile_node(comp, pns->nodes[1]); // if block |
| 1154 | //if (!(PY_PARSE_NODE_IS_NULL(pns->nodes[2]) && PY_PARSE_NODE_IS_NULL(pns->nodes[3]))) { // optimisation; doesn't align with CPython |
| 1155 | // jump over elif/else blocks if they exist |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 1156 | if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1157 | EMIT(jump, l_end); |
| 1158 | } |
| 1159 | //} |
| 1160 | EMIT(label_assign, l_fail); |
| 1161 | |
| 1162 | if (!PY_PARSE_NODE_IS_NULL(pns->nodes[2])) { |
| 1163 | // compile elif blocks |
| 1164 | |
| 1165 | py_parse_node_struct_t *pns_elif = (py_parse_node_struct_t*)pns->nodes[2]; |
| 1166 | |
| 1167 | if (PY_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) { |
| 1168 | // multiple elif blocks |
| 1169 | |
| 1170 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns_elif); |
| 1171 | for (int i = 0; i < n; i++) { |
| 1172 | py_parse_node_struct_t *pns_elif2 = (py_parse_node_struct_t*)pns_elif->nodes[i]; |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1173 | l_fail = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1174 | c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition |
| 1175 | |
| 1176 | compile_node(comp, pns_elif2->nodes[1]); // elif block |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 1177 | if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1178 | EMIT(jump, l_end); |
| 1179 | } |
| 1180 | EMIT(label_assign, l_fail); |
| 1181 | } |
| 1182 | |
| 1183 | } else { |
| 1184 | // a single elif block |
| 1185 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1186 | l_fail = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1187 | c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition |
| 1188 | |
| 1189 | compile_node(comp, pns_elif->nodes[1]); // elif block |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 1190 | if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1191 | EMIT(jump, l_end); |
| 1192 | } |
| 1193 | EMIT(label_assign, l_fail); |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | // compile else block |
| 1198 | compile_node(comp, pns->nodes[3]); // can be null |
| 1199 | |
| 1200 | EMIT(label_assign, l_end); |
| 1201 | } |
| 1202 | |
| 1203 | void compile_while_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1204 | int old_break_label = comp->break_label; |
| 1205 | int old_continue_label = comp->continue_label; |
| 1206 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1207 | int done_label = comp_next_label(comp); |
| 1208 | int end_label = comp_next_label(comp); |
| 1209 | int break_label = comp_next_label(comp); |
| 1210 | int continue_label = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1211 | |
| 1212 | comp->break_label = break_label; |
| 1213 | comp->continue_label = continue_label; |
| 1214 | |
| 1215 | EMIT(setup_loop, end_label); |
| 1216 | EMIT(label_assign, continue_label); |
| 1217 | c_if_cond(comp, pns->nodes[0], false, done_label); // condition |
| 1218 | compile_node(comp, pns->nodes[1]); // body |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 1219 | if (!EMIT(last_emit_was_return_value)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1220 | EMIT(jump, continue_label); |
| 1221 | } |
| 1222 | EMIT(label_assign, done_label); |
| 1223 | |
| 1224 | // break/continue apply to outer loop (if any) in the else block |
| 1225 | comp->break_label = old_break_label; |
| 1226 | comp->continue_label = old_continue_label; |
| 1227 | |
| 1228 | // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why |
| 1229 | // this is a small hack to agree with CPython |
| 1230 | if (!node_is_const_true(pns->nodes[0])) { |
| 1231 | EMIT(pop_block); |
| 1232 | } |
| 1233 | |
| 1234 | compile_node(comp, pns->nodes[2]); // else |
| 1235 | |
| 1236 | EMIT(label_assign, break_label); |
| 1237 | EMIT(label_assign, end_label); |
| 1238 | } |
| 1239 | |
| 1240 | void compile_for_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1241 | int old_break_label = comp->break_label; |
| 1242 | int old_continue_label = comp->continue_label; |
| 1243 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1244 | int for_label = comp_next_label(comp); |
| 1245 | int pop_label = comp_next_label(comp); |
| 1246 | int end_label = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1247 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1248 | int break_label = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1249 | |
| 1250 | comp->continue_label = for_label; |
| 1251 | comp->break_label = break_label; |
| 1252 | |
| 1253 | EMIT(setup_loop, end_label); |
| 1254 | compile_node(comp, pns->nodes[1]); // iterator |
| 1255 | EMIT(get_iter); |
| 1256 | EMIT(label_assign, for_label); |
| 1257 | EMIT(for_iter, pop_label); |
| 1258 | c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable |
| 1259 | compile_node(comp, pns->nodes[2]); // body |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 1260 | if (!EMIT(last_emit_was_return_value)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1261 | EMIT(jump, for_label); |
| 1262 | } |
| 1263 | EMIT(label_assign, pop_label); |
| 1264 | EMIT(for_iter_end); |
| 1265 | |
| 1266 | // break/continue apply to outer loop (if any) in the else block |
| 1267 | comp->break_label = old_break_label; |
| 1268 | comp->continue_label = old_continue_label; |
| 1269 | |
| 1270 | EMIT(pop_block); |
| 1271 | |
| 1272 | compile_node(comp, pns->nodes[3]); // else (not tested) |
| 1273 | |
| 1274 | EMIT(label_assign, break_label); |
| 1275 | EMIT(label_assign, end_label); |
| 1276 | } |
| 1277 | |
| 1278 | void compile_try_except(compiler_t *comp, py_parse_node_t pn_body, int n_except, py_parse_node_t *pn_excepts, py_parse_node_t pn_else) { |
| 1279 | // this function is a bit of a hack at the moment |
| 1280 | // don't understand how the stack works with exceptions, so we force it to return to the correct value |
| 1281 | |
| 1282 | // setup code |
| 1283 | int stack_size = EMIT(get_stack_size); |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1284 | int l1 = comp_next_label(comp); |
| 1285 | int success_label = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1286 | comp->except_nest_level += 1; // for correct handling of continue |
| 1287 | EMIT(setup_except, l1); |
| 1288 | compile_node(comp, pn_body); // body |
| 1289 | EMIT(pop_block); |
| 1290 | EMIT(jump, success_label); |
| 1291 | EMIT(label_assign, l1); |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1292 | int l2 = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1293 | |
| 1294 | for (int i = 0; i < n_except; i++) { |
| 1295 | assert(PY_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be |
| 1296 | py_parse_node_struct_t *pns_except = (py_parse_node_struct_t*)pn_excepts[i]; |
| 1297 | |
| 1298 | qstr qstr_exception_local = 0; |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1299 | int end_finally_label = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1300 | |
| 1301 | if (PY_PARSE_NODE_IS_NULL(pns_except->nodes[0])) { |
| 1302 | // this is a catch all exception handler |
| 1303 | if (i + 1 != n_except) { |
| 1304 | printf("SyntaxError: default 'except:' must be last\n"); |
| 1305 | return; |
| 1306 | } |
| 1307 | } else { |
| 1308 | // this exception handler requires a match to a certain type of exception |
| 1309 | py_parse_node_t pns_exception_expr = pns_except->nodes[0]; |
| 1310 | if (PY_PARSE_NODE_IS_STRUCT(pns_exception_expr)) { |
| 1311 | py_parse_node_struct_t *pns3 = (py_parse_node_struct_t*)pns_exception_expr; |
| 1312 | if (PY_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) { |
| 1313 | // handler binds the exception to a local |
| 1314 | pns_exception_expr = pns3->nodes[0]; |
| 1315 | qstr_exception_local = PY_PARSE_NODE_LEAF_ARG(pns3->nodes[1]); |
| 1316 | } |
| 1317 | } |
| 1318 | EMIT(dup_top); |
| 1319 | compile_node(comp, pns_exception_expr); |
| 1320 | EMIT(compare_op, RT_COMPARE_OP_EXCEPTION_MATCH); |
| 1321 | EMIT(pop_jump_if_false, end_finally_label); |
| 1322 | } |
| 1323 | |
| 1324 | EMIT(pop_top); |
| 1325 | |
| 1326 | if (qstr_exception_local == 0) { |
| 1327 | EMIT(pop_top); |
| 1328 | } else { |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 1329 | EMIT(store_id, qstr_exception_local); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | EMIT(pop_top); |
| 1333 | |
| 1334 | int l3; |
| 1335 | if (qstr_exception_local != 0) { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1336 | l3 = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1337 | EMIT(setup_finally, l3); |
| 1338 | } |
| 1339 | compile_node(comp, pns_except->nodes[1]); |
| 1340 | if (qstr_exception_local != 0) { |
| 1341 | EMIT(pop_block); |
| 1342 | } |
| 1343 | EMIT(pop_except); |
| 1344 | if (qstr_exception_local != 0) { |
| 1345 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 1346 | EMIT(label_assign, l3); |
| 1347 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 1348 | EMIT(store_id, qstr_exception_local); |
| 1349 | EMIT(delete_id, qstr_exception_local); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1350 | EMIT(end_finally); |
| 1351 | } |
| 1352 | EMIT(jump, l2); |
| 1353 | EMIT(label_assign, end_finally_label); |
| 1354 | } |
| 1355 | |
| 1356 | EMIT(end_finally); |
| 1357 | EMIT(label_assign, success_label); |
| 1358 | comp->except_nest_level -= 1; |
| 1359 | compile_node(comp, pn_else); // else block, can be null |
| 1360 | EMIT(label_assign, l2); |
| 1361 | EMIT(set_stack_size, stack_size); |
| 1362 | } |
| 1363 | |
| 1364 | void compile_try_finally(compiler_t *comp, py_parse_node_t pn_body, int n_except, py_parse_node_t *pn_except, py_parse_node_t pn_else, py_parse_node_t pn_finally) { |
| 1365 | // don't understand how the stack works with exceptions, so we force it to return to the correct value |
| 1366 | int stack_size = EMIT(get_stack_size); |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1367 | int l_finally_block = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1368 | EMIT(setup_finally, l_finally_block); |
| 1369 | if (n_except == 0) { |
| 1370 | assert(PY_PARSE_NODE_IS_NULL(pn_else)); |
| 1371 | compile_node(comp, pn_body); |
| 1372 | } else { |
| 1373 | compile_try_except(comp, pn_body, n_except, pn_except, pn_else); |
| 1374 | } |
| 1375 | EMIT(pop_block); |
| 1376 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 1377 | EMIT(label_assign, l_finally_block); |
| 1378 | compile_node(comp, pn_finally); |
| 1379 | EMIT(end_finally); |
| 1380 | EMIT(set_stack_size, stack_size); |
| 1381 | } |
| 1382 | |
| 1383 | void compile_try_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1384 | if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
| 1385 | py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[1]; |
| 1386 | if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) { |
| 1387 | // just try-finally |
| 1388 | compile_try_finally(comp, pns->nodes[0], 0, NULL, PY_PARSE_NODE_NULL, pns2->nodes[0]); |
| 1389 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) { |
| 1390 | // try-except and possibly else and/or finally |
| 1391 | py_parse_node_t *pn_excepts; |
| 1392 | int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts); |
| 1393 | if (PY_PARSE_NODE_IS_NULL(pns2->nodes[2])) { |
| 1394 | // no finally |
| 1395 | compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]); |
| 1396 | } else { |
| 1397 | // have finally |
| 1398 | compile_try_finally(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1], ((py_parse_node_struct_t*)pns2->nodes[2])->nodes[0]); |
| 1399 | } |
| 1400 | } else { |
| 1401 | // just try-except |
| 1402 | py_parse_node_t *pn_excepts; |
| 1403 | int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts); |
| 1404 | compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, PY_PARSE_NODE_NULL); |
| 1405 | } |
| 1406 | } else { |
| 1407 | // shouldn't happen |
| 1408 | assert(0); |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | void compile_with_stmt_helper(compiler_t *comp, int n, py_parse_node_t *nodes, py_parse_node_t body) { |
| 1413 | if (n == 0) { |
| 1414 | // no more pre-bits, compile the body of the with |
| 1415 | compile_node(comp, body); |
| 1416 | } else { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1417 | int l_end = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1418 | if (PY_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) { |
| 1419 | // this pre-bit is of the form "a as b" |
| 1420 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)nodes[0]; |
| 1421 | compile_node(comp, pns->nodes[0]); |
| 1422 | EMIT(setup_with, l_end); |
| 1423 | c_assign(comp, pns->nodes[1], ASSIGN_STORE); |
| 1424 | } else { |
| 1425 | // this pre-bit is just an expression |
| 1426 | compile_node(comp, nodes[0]); |
| 1427 | EMIT(setup_with, l_end); |
| 1428 | EMIT(pop_top); |
| 1429 | } |
| 1430 | // compile additional pre-bits and the body |
| 1431 | compile_with_stmt_helper(comp, n - 1, nodes + 1, body); |
| 1432 | // finish this with block |
| 1433 | EMIT(pop_block); |
| 1434 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 1435 | EMIT(label_assign, l_end); |
| 1436 | EMIT(with_cleanup); |
| 1437 | EMIT(end_finally); |
| 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | void compile_with_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1442 | // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit) |
| 1443 | py_parse_node_t *nodes; |
| 1444 | int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes); |
| 1445 | assert(n > 0); |
| 1446 | |
| 1447 | // compile in a nested fashion |
| 1448 | compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]); |
| 1449 | } |
| 1450 | |
| 1451 | void compile_expr_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1452 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[1])) { |
| 1453 | if (PY_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !PY_PARSE_NODE_IS_ID(pns->nodes[0])) { |
| 1454 | // do nothing with a lonely constant |
| 1455 | } else { |
| 1456 | compile_node(comp, pns->nodes[0]); // just an expression |
| 1457 | EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack |
| 1458 | } |
| 1459 | } else { |
| 1460 | py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1]; |
| 1461 | int kind = PY_PARSE_NODE_STRUCT_KIND(pns1); |
| 1462 | if (kind == PN_expr_stmt_augassign) { |
| 1463 | c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign |
| 1464 | compile_node(comp, pns1->nodes[1]); // rhs |
| 1465 | assert(PY_PARSE_NODE_IS_TOKEN(pns1->nodes[0])); |
| 1466 | // note that we don't really need to implement separate inplace ops, just normal binary ops will suffice |
| 1467 | switch (PY_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) { |
| 1468 | case PY_TOKEN_DEL_PIPE_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_OR); break; |
| 1469 | case PY_TOKEN_DEL_CARET_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_XOR); break; |
| 1470 | case PY_TOKEN_DEL_AMPERSAND_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_AND); break; |
| 1471 | case PY_TOKEN_DEL_DBL_LESS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_LSHIFT); break; |
| 1472 | case PY_TOKEN_DEL_DBL_MORE_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_RSHIFT); break; |
| 1473 | case PY_TOKEN_DEL_PLUS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_ADD); break; |
| 1474 | case PY_TOKEN_DEL_MINUS_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_SUBTRACT); break; |
| 1475 | case PY_TOKEN_DEL_STAR_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_MULTIPLY); break; |
| 1476 | case PY_TOKEN_DEL_DBL_SLASH_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_FLOOR_DIVIDE); break; |
| 1477 | case PY_TOKEN_DEL_SLASH_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_TRUE_DIVIDE); break; |
| 1478 | case PY_TOKEN_DEL_PERCENT_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_MODULO); break; |
| 1479 | case PY_TOKEN_DEL_DBL_STAR_EQUAL: EMIT(binary_op, RT_BINARY_OP_INPLACE_POWER); break; |
| 1480 | default: assert(0); // shouldn't happen |
| 1481 | } |
| 1482 | c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign |
| 1483 | } else if (kind == PN_expr_stmt_assign_list) { |
| 1484 | int rhs = PY_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1; |
| 1485 | compile_node(comp, ((py_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs |
| 1486 | // following CPython, we store left-most first |
| 1487 | if (rhs > 0) { |
| 1488 | EMIT(dup_top); |
| 1489 | } |
| 1490 | c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store |
| 1491 | for (int i = 0; i < rhs; i++) { |
| 1492 | if (i + 1 < rhs) { |
| 1493 | EMIT(dup_top); |
| 1494 | } |
| 1495 | c_assign(comp, ((py_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store |
| 1496 | } |
| 1497 | } else if (kind == PN_expr_stmt_assign) { |
| 1498 | if (PY_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr) |
| 1499 | && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr) |
| 1500 | && PY_PARSE_NODE_STRUCT_NUM_NODES((py_parse_node_struct_t*)pns1->nodes[0]) == 2 |
| 1501 | && PY_PARSE_NODE_STRUCT_NUM_NODES((py_parse_node_struct_t*)pns->nodes[0]) == 2) { |
| 1502 | // optimisation for a, b = c, d; to match CPython's optimisation |
| 1503 | py_parse_node_struct_t* pns10 = (py_parse_node_struct_t*)pns1->nodes[0]; |
| 1504 | py_parse_node_struct_t* pns0 = (py_parse_node_struct_t*)pns->nodes[0]; |
| 1505 | compile_node(comp, pns10->nodes[0]); // rhs |
| 1506 | compile_node(comp, pns10->nodes[1]); // rhs |
| 1507 | EMIT(rot_two); |
| 1508 | c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store |
| 1509 | c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store |
| 1510 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr) |
| 1511 | && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr) |
| 1512 | && PY_PARSE_NODE_STRUCT_NUM_NODES((py_parse_node_struct_t*)pns1->nodes[0]) == 3 |
| 1513 | && PY_PARSE_NODE_STRUCT_NUM_NODES((py_parse_node_struct_t*)pns->nodes[0]) == 3) { |
| 1514 | // optimisation for a, b, c = d, e, f; to match CPython's optimisation |
| 1515 | py_parse_node_struct_t* pns10 = (py_parse_node_struct_t*)pns1->nodes[0]; |
| 1516 | py_parse_node_struct_t* pns0 = (py_parse_node_struct_t*)pns->nodes[0]; |
| 1517 | compile_node(comp, pns10->nodes[0]); // rhs |
| 1518 | compile_node(comp, pns10->nodes[1]); // rhs |
| 1519 | compile_node(comp, pns10->nodes[2]); // rhs |
| 1520 | EMIT(rot_three); |
| 1521 | EMIT(rot_two); |
| 1522 | c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store |
| 1523 | c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store |
| 1524 | c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store |
| 1525 | } else { |
| 1526 | compile_node(comp, pns1->nodes[0]); // rhs |
| 1527 | c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store |
| 1528 | } |
| 1529 | } else { |
| 1530 | // shouldn't happen |
| 1531 | assert(0); |
| 1532 | } |
| 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | void c_binary_op(compiler_t *comp, py_parse_node_struct_t *pns, rt_binary_op_t binary_op) { |
| 1537 | int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 1538 | compile_node(comp, pns->nodes[0]); |
| 1539 | for (int i = 1; i < num_nodes; i += 1) { |
| 1540 | compile_node(comp, pns->nodes[i]); |
| 1541 | EMIT(binary_op, binary_op); |
| 1542 | } |
| 1543 | } |
| 1544 | |
| 1545 | void compile_test_if_expr(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1546 | assert(PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else)); |
| 1547 | py_parse_node_struct_t *pns_test_if_else = (py_parse_node_struct_t*)pns->nodes[1]; |
| 1548 | |
| 1549 | int stack_size = EMIT(get_stack_size); |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1550 | int l_fail = comp_next_label(comp); |
| 1551 | int l_end = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1552 | c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition |
| 1553 | compile_node(comp, pns->nodes[0]); // success value |
| 1554 | EMIT(jump, l_end); |
| 1555 | EMIT(label_assign, l_fail); |
| 1556 | EMIT(set_stack_size, stack_size); // force stack size reset |
| 1557 | compile_node(comp, pns_test_if_else->nodes[1]); // failure value |
| 1558 | EMIT(label_assign, l_end); |
| 1559 | } |
| 1560 | |
| 1561 | void compile_lambdef(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1562 | // TODO default params etc for lambda; possibly just use funcdef code |
| 1563 | //py_parse_node_t pn_params = pns->nodes[0]; |
| 1564 | //py_parse_node_t pn_body = pns->nodes[1]; |
| 1565 | |
| 1566 | if (comp->pass == PASS_1) { |
| 1567 | // create a new scope for this lambda |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 1568 | scope_t *s = scope_new_and_link(comp, SCOPE_LAMBDA, (py_parse_node_t)pns, comp->scope_cur->emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1569 | // store the lambda scope so the compiling function (this one) can use it at each pass |
| 1570 | pns->nodes[2] = (py_parse_node_t)s; |
| 1571 | } |
| 1572 | |
| 1573 | // get the scope for this lambda |
| 1574 | scope_t *this_scope = (scope_t*)pns->nodes[2]; |
| 1575 | |
| 1576 | // make the lambda |
| 1577 | close_over_variables_etc(comp, this_scope, 0, 0); |
| 1578 | } |
| 1579 | |
| 1580 | void compile_or_test(compiler_t *comp, py_parse_node_struct_t *pns) { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1581 | int l_end = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1582 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 1583 | for (int i = 0; i < n; i += 1) { |
| 1584 | compile_node(comp, pns->nodes[i]); |
| 1585 | if (i + 1 < n) { |
| 1586 | EMIT(jump_if_true_or_pop, l_end); |
| 1587 | } |
| 1588 | } |
| 1589 | EMIT(label_assign, l_end); |
| 1590 | } |
| 1591 | |
| 1592 | void compile_and_test(compiler_t *comp, py_parse_node_struct_t *pns) { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1593 | int l_end = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1594 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 1595 | for (int i = 0; i < n; i += 1) { |
| 1596 | compile_node(comp, pns->nodes[i]); |
| 1597 | if (i + 1 < n) { |
| 1598 | EMIT(jump_if_false_or_pop, l_end); |
| 1599 | } |
| 1600 | } |
| 1601 | EMIT(label_assign, l_end); |
| 1602 | } |
| 1603 | |
| 1604 | void compile_not_test_2(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1605 | compile_node(comp, pns->nodes[0]); |
| 1606 | EMIT(unary_op, RT_UNARY_OP_NOT); |
| 1607 | } |
| 1608 | |
| 1609 | void compile_comparison(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1610 | int stack_size = EMIT(get_stack_size); |
| 1611 | int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 1612 | compile_node(comp, pns->nodes[0]); |
| 1613 | bool multi = (num_nodes > 3); |
| 1614 | int l_fail = 0; |
| 1615 | if (multi) { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1616 | l_fail = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1617 | } |
| 1618 | for (int i = 1; i + 1 < num_nodes; i += 2) { |
| 1619 | compile_node(comp, pns->nodes[i + 1]); |
| 1620 | if (i + 2 < num_nodes) { |
| 1621 | EMIT(dup_top); |
| 1622 | EMIT(rot_three); |
| 1623 | } |
| 1624 | if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_LESS)) { |
| 1625 | EMIT(compare_op, RT_COMPARE_OP_LESS); |
| 1626 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_MORE)) { |
| 1627 | EMIT(compare_op, RT_COMPARE_OP_MORE); |
| 1628 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_DBL_EQUAL)) { |
| 1629 | EMIT(compare_op, RT_COMPARE_OP_EQUAL); |
| 1630 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_LESS_EQUAL)) { |
| 1631 | EMIT(compare_op, RT_COMPARE_OP_LESS_EQUAL); |
| 1632 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_MORE_EQUAL)) { |
| 1633 | EMIT(compare_op, RT_COMPARE_OP_MORE_EQUAL); |
| 1634 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_NOT_EQUAL)) { |
| 1635 | EMIT(compare_op, RT_COMPARE_OP_NOT_EQUAL); |
| 1636 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_KW_IN)) { |
| 1637 | EMIT(compare_op, RT_COMPARE_OP_IN); |
| 1638 | } else if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[i])) { |
| 1639 | py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[i]; |
| 1640 | int kind = PY_PARSE_NODE_STRUCT_KIND(pns2); |
| 1641 | if (kind == PN_comp_op_not_in) { |
| 1642 | EMIT(compare_op, RT_COMPARE_OP_NOT_IN); |
| 1643 | } else if (kind == PN_comp_op_is) { |
| 1644 | if (PY_PARSE_NODE_IS_NULL(pns2->nodes[0])) { |
| 1645 | EMIT(compare_op, RT_COMPARE_OP_IS); |
| 1646 | } else { |
| 1647 | EMIT(compare_op, RT_COMPARE_OP_IS_NOT); |
| 1648 | } |
| 1649 | } else { |
| 1650 | // shouldn't happen |
| 1651 | assert(0); |
| 1652 | } |
| 1653 | } else { |
| 1654 | // shouldn't happen |
| 1655 | assert(0); |
| 1656 | } |
| 1657 | if (i + 2 < num_nodes) { |
| 1658 | EMIT(jump_if_false_or_pop, l_fail); |
| 1659 | } |
| 1660 | } |
| 1661 | if (multi) { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1662 | int l_end = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1663 | EMIT(jump, l_end); |
| 1664 | EMIT(label_assign, l_fail); |
| 1665 | EMIT(rot_two); |
| 1666 | EMIT(pop_top); |
| 1667 | EMIT(label_assign, l_end); |
| 1668 | EMIT(set_stack_size, stack_size + 1); // force stack size |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | void compile_star_expr(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1673 | // TODO |
| 1674 | assert(0); |
| 1675 | compile_node(comp, pns->nodes[0]); |
| 1676 | //EMIT(unary_op, "UNARY_STAR"); |
| 1677 | } |
| 1678 | |
| 1679 | void compile_expr(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1680 | c_binary_op(comp, pns, RT_BINARY_OP_OR); |
| 1681 | } |
| 1682 | |
| 1683 | void compile_xor_expr(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1684 | c_binary_op(comp, pns, RT_BINARY_OP_XOR); |
| 1685 | } |
| 1686 | |
| 1687 | void compile_and_expr(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1688 | c_binary_op(comp, pns, RT_BINARY_OP_AND); |
| 1689 | } |
| 1690 | |
| 1691 | void compile_shift_expr(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1692 | int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 1693 | compile_node(comp, pns->nodes[0]); |
| 1694 | for (int i = 1; i + 1 < num_nodes; i += 2) { |
| 1695 | compile_node(comp, pns->nodes[i + 1]); |
| 1696 | if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_DBL_LESS)) { |
| 1697 | EMIT(binary_op, RT_BINARY_OP_LSHIFT); |
| 1698 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_DBL_MORE)) { |
| 1699 | EMIT(binary_op, RT_BINARY_OP_RSHIFT); |
| 1700 | } else { |
| 1701 | // shouldn't happen |
| 1702 | assert(0); |
| 1703 | } |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | void compile_arith_expr(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1708 | int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 1709 | compile_node(comp, pns->nodes[0]); |
| 1710 | for (int i = 1; i + 1 < num_nodes; i += 2) { |
| 1711 | compile_node(comp, pns->nodes[i + 1]); |
| 1712 | if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_PLUS)) { |
| 1713 | EMIT(binary_op, RT_BINARY_OP_ADD); |
| 1714 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_MINUS)) { |
| 1715 | EMIT(binary_op, RT_BINARY_OP_SUBTRACT); |
| 1716 | } else { |
| 1717 | // shouldn't happen |
| 1718 | assert(0); |
| 1719 | } |
| 1720 | } |
| 1721 | } |
| 1722 | |
| 1723 | void compile_term(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1724 | int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 1725 | compile_node(comp, pns->nodes[0]); |
| 1726 | for (int i = 1; i + 1 < num_nodes; i += 2) { |
| 1727 | compile_node(comp, pns->nodes[i + 1]); |
| 1728 | if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_STAR)) { |
| 1729 | EMIT(binary_op, RT_BINARY_OP_MULTIPLY); |
| 1730 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_DBL_SLASH)) { |
| 1731 | EMIT(binary_op, RT_BINARY_OP_FLOOR_DIVIDE); |
| 1732 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_SLASH)) { |
| 1733 | EMIT(binary_op, RT_BINARY_OP_TRUE_DIVIDE); |
| 1734 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], PY_TOKEN_OP_PERCENT)) { |
| 1735 | EMIT(binary_op, RT_BINARY_OP_MODULO); |
| 1736 | } else { |
| 1737 | // shouldn't happen |
| 1738 | assert(0); |
| 1739 | } |
| 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | void compile_factor_2(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1744 | compile_node(comp, pns->nodes[1]); |
| 1745 | if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_PLUS)) { |
| 1746 | EMIT(unary_op, RT_UNARY_OP_POSITIVE); |
| 1747 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_MINUS)) { |
| 1748 | EMIT(unary_op, RT_UNARY_OP_NEGATIVE); |
| 1749 | } else if (PY_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], PY_TOKEN_OP_TILDE)) { |
| 1750 | EMIT(unary_op, RT_UNARY_OP_INVERT); |
| 1751 | } else { |
| 1752 | // shouldn't happen |
| 1753 | assert(0); |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | void compile_trailer_paren_helper(compiler_t *comp, py_parse_node_struct_t *pns, bool is_method_call) { |
| 1758 | // function to call is on top of stack |
| 1759 | |
| 1760 | int old_n_arg_keyword = comp->n_arg_keyword; |
| 1761 | bool old_have_star_arg = comp->have_star_arg; |
| 1762 | bool old_have_dbl_star_arg = comp->have_dbl_star_arg; |
| 1763 | comp->n_arg_keyword = 0; |
| 1764 | comp->have_star_arg = false; |
| 1765 | comp->have_dbl_star_arg = false; |
| 1766 | |
| 1767 | compile_node(comp, pns->nodes[0]); // arguments to function call; can be null |
| 1768 | |
| 1769 | // compute number of positional arguments |
| 1770 | int n_positional = list_len(pns->nodes[0], PN_arglist) - comp->n_arg_keyword; |
| 1771 | if (comp->have_star_arg) { |
| 1772 | n_positional -= 1; |
| 1773 | } |
| 1774 | if (comp->have_dbl_star_arg) { |
| 1775 | n_positional -= 1; |
| 1776 | } |
| 1777 | |
| 1778 | if (is_method_call) { |
| 1779 | EMIT(call_method, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg); |
| 1780 | } else { |
| 1781 | EMIT(call_function, n_positional, comp->n_arg_keyword, comp->have_star_arg, comp->have_dbl_star_arg); |
| 1782 | } |
| 1783 | |
| 1784 | comp->n_arg_keyword = old_n_arg_keyword; |
| 1785 | comp->have_star_arg = old_have_star_arg; |
| 1786 | comp->have_dbl_star_arg = old_have_dbl_star_arg; |
| 1787 | } |
| 1788 | |
| 1789 | void compile_power_trailers(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1790 | int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 1791 | for (int i = 0; i < num_nodes; i++) { |
| 1792 | if (i + 1 < num_nodes && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[i], PN_trailer_period) && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[i + 1], PN_trailer_paren)) { |
| 1793 | // optimisation for method calls a.f(...), following PyPy |
| 1794 | py_parse_node_struct_t *pns_period = (py_parse_node_struct_t*)pns->nodes[i]; |
| 1795 | py_parse_node_struct_t *pns_paren = (py_parse_node_struct_t*)pns->nodes[i + 1]; |
| 1796 | EMIT(load_method, PY_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method |
| 1797 | compile_trailer_paren_helper(comp, pns_paren, true); |
| 1798 | i += 1; |
| 1799 | } else { |
| 1800 | compile_node(comp, pns->nodes[i]); |
| 1801 | } |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | void compile_power_dbl_star(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1806 | compile_node(comp, pns->nodes[0]); |
| 1807 | EMIT(binary_op, RT_BINARY_OP_POWER); |
| 1808 | } |
| 1809 | |
| 1810 | void compile_atom_string(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1811 | // a list of strings |
| 1812 | EMIT(load_const_verbatim_start); |
| 1813 | EMIT(load_const_verbatim_str, "'"); |
| 1814 | int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); |
| 1815 | for (int i = 0; i < n; i++) { |
| 1816 | // TODO allow concatenation of either strings or bytes, but not mixed |
| 1817 | assert(PY_PARSE_NODE_IS_LEAF(pns->nodes[i])); |
| 1818 | assert(PY_PARSE_NODE_LEAF_KIND(pns->nodes[i]) == PY_PARSE_NODE_STRING); |
| 1819 | const char *str = qstr_str(PY_PARSE_NODE_LEAF_ARG(pns->nodes[i])); |
| 1820 | EMIT(load_const_verbatim_strn, str, strlen(str)); |
| 1821 | } |
| 1822 | EMIT(load_const_verbatim_str, "'"); |
| 1823 | EMIT(load_const_verbatim_end); |
| 1824 | } |
| 1825 | |
| 1826 | // pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node |
| 1827 | void compile_comprehension(compiler_t *comp, py_parse_node_struct_t *pns, scope_kind_t kind) { |
| 1828 | assert(PY_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2); |
| 1829 | assert(PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for)); |
| 1830 | py_parse_node_struct_t *pns_comp_for = (py_parse_node_struct_t*)pns->nodes[1]; |
| 1831 | |
| 1832 | if (comp->pass == PASS_1) { |
| 1833 | // create a new scope for this comprehension |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 1834 | scope_t *s = scope_new_and_link(comp, kind, (py_parse_node_t)pns, comp->scope_cur->emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1835 | // store the comprehension scope so the compiling function (this one) can use it at each pass |
| 1836 | pns_comp_for->nodes[3] = (py_parse_node_t)s; |
| 1837 | } |
| 1838 | |
| 1839 | // get the scope for this comprehension |
| 1840 | scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3]; |
| 1841 | |
| 1842 | // compile the comprehension |
| 1843 | close_over_variables_etc(comp, this_scope, 0, 0); |
| 1844 | |
| 1845 | compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator |
| 1846 | EMIT(get_iter); |
| 1847 | EMIT(call_function, 1, 0, false, false); |
| 1848 | } |
| 1849 | |
| 1850 | void compile_atom_paren(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1851 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
| 1852 | // an empty tuple |
| 1853 | /* |
| 1854 | EMIT(build_tuple, 0); |
| 1855 | */ |
| 1856 | c_tuple(comp, PY_PARSE_NODE_NULL, NULL); |
| 1857 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) { |
| 1858 | pns = (py_parse_node_struct_t*)pns->nodes[0]; |
| 1859 | assert(!PY_PARSE_NODE_IS_NULL(pns->nodes[1])); |
| 1860 | if (PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])) { |
| 1861 | py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[1]; |
| 1862 | if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) { |
| 1863 | // tuple of one item, with trailing comma |
| 1864 | assert(PY_PARSE_NODE_IS_NULL(pns2->nodes[0])); |
| 1865 | /* |
| 1866 | compile_node(comp, pns->nodes[0]); |
| 1867 | EMIT(build_tuple, 1); |
| 1868 | */ |
| 1869 | c_tuple(comp, pns->nodes[0], NULL); |
| 1870 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) { |
| 1871 | // tuple of many items |
| 1872 | /* |
| 1873 | compile_node(comp, pns->nodes[0]); |
| 1874 | compile_generic_all_nodes(comp, pns2); |
| 1875 | EMIT(build_tuple, 1 + PY_PARSE_NODE_STRUCT_NUM_NODES(pns2)); |
| 1876 | */ |
| 1877 | c_tuple(comp, pns->nodes[0], pns2); |
| 1878 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) { |
| 1879 | // generator expression |
| 1880 | compile_comprehension(comp, pns, SCOPE_GEN_EXPR); |
| 1881 | } else { |
| 1882 | // tuple with 2 items |
| 1883 | goto tuple_with_2_items; |
| 1884 | } |
| 1885 | } else { |
| 1886 | // tuple with 2 items |
| 1887 | tuple_with_2_items: |
| 1888 | /* |
| 1889 | compile_node(comp, pns->nodes[0]); |
| 1890 | compile_node(comp, pns->nodes[1]); |
| 1891 | EMIT(build_tuple, 2); |
| 1892 | */ |
| 1893 | c_tuple(comp, PY_PARSE_NODE_NULL, pns); |
| 1894 | } |
| 1895 | } else { |
| 1896 | // parenthesis around a single item, is just that item |
| 1897 | compile_node(comp, pns->nodes[0]); |
| 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | void compile_atom_bracket(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1902 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
| 1903 | // empty list |
| 1904 | EMIT(build_list, 0); |
| 1905 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) { |
| 1906 | py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[0]; |
| 1907 | if (PY_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) { |
| 1908 | py_parse_node_struct_t *pns3 = (py_parse_node_struct_t*)pns2->nodes[1]; |
| 1909 | if (PY_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) { |
| 1910 | // list of one item, with trailing comma |
| 1911 | assert(PY_PARSE_NODE_IS_NULL(pns3->nodes[0])); |
| 1912 | compile_node(comp, pns2->nodes[0]); |
| 1913 | EMIT(build_list, 1); |
| 1914 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) { |
| 1915 | // list of many items |
| 1916 | compile_node(comp, pns2->nodes[0]); |
| 1917 | compile_generic_all_nodes(comp, pns3); |
| 1918 | EMIT(build_list, 1 + PY_PARSE_NODE_STRUCT_NUM_NODES(pns3)); |
| 1919 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) { |
| 1920 | // list comprehension |
| 1921 | compile_comprehension(comp, pns2, SCOPE_LIST_COMP); |
| 1922 | } else { |
| 1923 | // list with 2 items |
| 1924 | goto list_with_2_items; |
| 1925 | } |
| 1926 | } else { |
| 1927 | // list with 2 items |
| 1928 | list_with_2_items: |
| 1929 | compile_node(comp, pns2->nodes[0]); |
| 1930 | compile_node(comp, pns2->nodes[1]); |
| 1931 | EMIT(build_list, 2); |
| 1932 | } |
| 1933 | } else { |
| 1934 | // list with 1 item |
| 1935 | compile_node(comp, pns->nodes[0]); |
| 1936 | EMIT(build_list, 1); |
| 1937 | } |
| 1938 | } |
| 1939 | |
| 1940 | void compile_atom_brace(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 1941 | py_parse_node_t pn = pns->nodes[0]; |
| 1942 | if (PY_PARSE_NODE_IS_NULL(pn)) { |
| 1943 | // empty dict |
| 1944 | EMIT(build_map, 0); |
| 1945 | } else if (PY_PARSE_NODE_IS_STRUCT(pn)) { |
| 1946 | pns = (py_parse_node_struct_t*)pn; |
| 1947 | if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) { |
| 1948 | // dict with one element |
| 1949 | EMIT(build_map, 1); |
| 1950 | compile_node(comp, pn); |
| 1951 | EMIT(store_map); |
| 1952 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) { |
| 1953 | assert(PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed |
| 1954 | py_parse_node_struct_t *pns1 = (py_parse_node_struct_t*)pns->nodes[1]; |
| 1955 | if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) { |
| 1956 | // dict/set with multiple elements |
| 1957 | |
| 1958 | // get tail elements (2nd, 3rd, ...) |
| 1959 | py_parse_node_t *nodes; |
| 1960 | int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes); |
| 1961 | |
| 1962 | // first element sets whether it's a dict or set |
| 1963 | bool is_dict; |
| 1964 | if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) { |
| 1965 | // a dictionary |
| 1966 | EMIT(build_map, 1 + n); |
| 1967 | compile_node(comp, pns->nodes[0]); |
| 1968 | EMIT(store_map); |
| 1969 | is_dict = true; |
| 1970 | } else { |
| 1971 | // a set |
| 1972 | compile_node(comp, pns->nodes[0]); // 1st value of set |
| 1973 | is_dict = false; |
| 1974 | } |
| 1975 | |
| 1976 | // process rest of elements |
| 1977 | for (int i = 0; i < n; i++) { |
| 1978 | py_parse_node_t pn = nodes[i]; |
| 1979 | bool is_key_value = PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item); |
| 1980 | compile_node(comp, pn); |
| 1981 | if (is_dict) { |
| 1982 | if (!is_key_value) { |
| 1983 | printf("SyntaxError?: expecting key:value for dictionary"); |
| 1984 | return; |
| 1985 | } |
| 1986 | EMIT(store_map); |
| 1987 | } else { |
| 1988 | if (is_key_value) { |
| 1989 | printf("SyntaxError?: expecting just a value for set"); |
| 1990 | return; |
| 1991 | } |
| 1992 | } |
| 1993 | } |
| 1994 | |
| 1995 | // if it's a set, build it |
| 1996 | if (!is_dict) { |
| 1997 | EMIT(build_set, 1 + n); |
| 1998 | } |
| 1999 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) { |
| 2000 | // dict/set comprehension |
| 2001 | if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) { |
| 2002 | // a dictionary comprehension |
| 2003 | compile_comprehension(comp, pns, SCOPE_DICT_COMP); |
| 2004 | } else { |
| 2005 | // a set comprehension |
| 2006 | compile_comprehension(comp, pns, SCOPE_SET_COMP); |
| 2007 | } |
| 2008 | } else { |
| 2009 | // shouldn't happen |
| 2010 | assert(0); |
| 2011 | } |
| 2012 | } else { |
| 2013 | // set with one element |
| 2014 | goto set_with_one_element; |
| 2015 | } |
| 2016 | } else { |
| 2017 | // set with one element |
| 2018 | set_with_one_element: |
| 2019 | compile_node(comp, pn); |
| 2020 | EMIT(build_set, 1); |
| 2021 | } |
| 2022 | } |
| 2023 | |
| 2024 | void compile_trailer_paren(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2025 | compile_trailer_paren_helper(comp, pns, false); |
| 2026 | } |
| 2027 | |
| 2028 | void compile_trailer_bracket(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2029 | // object who's index we want is on top of stack |
| 2030 | compile_node(comp, pns->nodes[0]); // the index |
| 2031 | EMIT(binary_op, RT_BINARY_OP_SUBSCR); |
| 2032 | } |
| 2033 | |
| 2034 | void compile_trailer_period(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2035 | // object who's attribute we want is on top of stack |
| 2036 | EMIT(load_attr, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get |
| 2037 | } |
| 2038 | |
| 2039 | void compile_subscript_3_helper(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2040 | assert(PY_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be |
| 2041 | py_parse_node_t pn = pns->nodes[0]; |
| 2042 | if (PY_PARSE_NODE_IS_NULL(pn)) { |
| 2043 | // [?:] |
| 2044 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 2045 | EMIT(build_slice, 2); |
| 2046 | } else if (PY_PARSE_NODE_IS_STRUCT(pn)) { |
| 2047 | pns = (py_parse_node_struct_t*)pn; |
| 2048 | if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) { |
| 2049 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 2050 | pn = pns->nodes[0]; |
| 2051 | if (PY_PARSE_NODE_IS_NULL(pn)) { |
| 2052 | // [?::] |
| 2053 | EMIT(build_slice, 2); |
| 2054 | } else { |
| 2055 | // [?::x] |
| 2056 | compile_node(comp, pn); |
| 2057 | EMIT(build_slice, 3); |
| 2058 | } |
| 2059 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) { |
| 2060 | compile_node(comp, pns->nodes[0]); |
| 2061 | assert(PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be |
| 2062 | pns = (py_parse_node_struct_t*)pns->nodes[1]; |
| 2063 | assert(PY_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be |
| 2064 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
| 2065 | // [?:x:] |
| 2066 | EMIT(build_slice, 2); |
| 2067 | } else { |
| 2068 | // [?:x:x] |
| 2069 | compile_node(comp, pns->nodes[0]); |
| 2070 | EMIT(build_slice, 3); |
| 2071 | } |
| 2072 | } else { |
| 2073 | // [?:x] |
| 2074 | compile_node(comp, pn); |
| 2075 | EMIT(build_slice, 2); |
| 2076 | } |
| 2077 | } else { |
| 2078 | // [?:x] |
| 2079 | compile_node(comp, pn); |
| 2080 | EMIT(build_slice, 2); |
| 2081 | } |
| 2082 | } |
| 2083 | |
| 2084 | void compile_subscript_2(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2085 | compile_node(comp, pns->nodes[0]); // start of slice |
| 2086 | assert(PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be |
| 2087 | compile_subscript_3_helper(comp, (py_parse_node_struct_t*)pns->nodes[1]); |
| 2088 | } |
| 2089 | |
| 2090 | void compile_subscript_3(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2091 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 2092 | compile_subscript_3_helper(comp, pns); |
| 2093 | } |
| 2094 | |
| 2095 | void compile_dictorsetmaker_item(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2096 | // if this is called then we are compiling a dict key:value pair |
| 2097 | compile_node(comp, pns->nodes[1]); // value |
| 2098 | compile_node(comp, pns->nodes[0]); // key |
| 2099 | } |
| 2100 | |
| 2101 | void compile_classdef(compiler_t *comp, py_parse_node_struct_t *pns) { |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 2102 | qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2103 | // store class object into class name |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 2104 | EMIT(store_id, cname); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2105 | } |
| 2106 | |
| 2107 | void compile_arglist_star(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2108 | if (comp->have_star_arg) { |
| 2109 | printf("SyntaxError?: can't have multiple *x\n"); |
| 2110 | return; |
| 2111 | } |
| 2112 | comp->have_star_arg = true; |
| 2113 | compile_node(comp, pns->nodes[0]); |
| 2114 | } |
| 2115 | |
| 2116 | void compile_arglist_dbl_star(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2117 | if (comp->have_dbl_star_arg) { |
| 2118 | printf("SyntaxError?: can't have multiple **x\n"); |
| 2119 | return; |
| 2120 | } |
| 2121 | comp->have_dbl_star_arg = true; |
| 2122 | compile_node(comp, pns->nodes[0]); |
| 2123 | } |
| 2124 | |
| 2125 | void compile_argument(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2126 | assert(PY_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be |
| 2127 | py_parse_node_struct_t *pns2 = (py_parse_node_struct_t*)pns->nodes[1]; |
| 2128 | if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) { |
| 2129 | if (!PY_PARSE_NODE_IS_ID(pns->nodes[0])) { |
| 2130 | printf("SyntaxError?: lhs of keyword argument must be an id\n"); |
| 2131 | return; |
| 2132 | } |
| 2133 | EMIT(load_const_id, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0])); |
| 2134 | compile_node(comp, pns2->nodes[0]); |
| 2135 | comp->n_arg_keyword += 1; |
| 2136 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) { |
| 2137 | compile_comprehension(comp, pns, SCOPE_GEN_EXPR); |
| 2138 | } else { |
| 2139 | // shouldn't happen |
| 2140 | assert(0); |
| 2141 | } |
| 2142 | } |
| 2143 | |
| 2144 | void compile_yield_expr(compiler_t *comp, py_parse_node_struct_t *pns) { |
| 2145 | if (comp->scope_cur->kind != SCOPE_FUNCTION) { |
| 2146 | printf("SyntaxError: 'yield' outside function\n"); |
| 2147 | return; |
| 2148 | } |
| 2149 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
| 2150 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 2151 | EMIT(yield_value); |
| 2152 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) { |
| 2153 | pns = (py_parse_node_struct_t*)pns->nodes[0]; |
| 2154 | compile_node(comp, pns->nodes[0]); |
| 2155 | EMIT(get_iter); |
| 2156 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 2157 | EMIT(yield_from); |
| 2158 | } else { |
| 2159 | compile_node(comp, pns->nodes[0]); |
| 2160 | EMIT(yield_value); |
| 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | typedef void (*compile_function_t)(compiler_t*, py_parse_node_struct_t*); |
| 2165 | static compile_function_t compile_function[] = { |
| 2166 | NULL, |
| 2167 | #define nc NULL |
| 2168 | #define c(f) compile_##f |
| 2169 | #define DEF_RULE(rule, comp, kind, arg...) comp, |
| 2170 | #include "grammar.h" |
| 2171 | #undef nc |
| 2172 | #undef c |
| 2173 | #undef DEF_RULE |
| 2174 | }; |
| 2175 | |
| 2176 | void compile_node(compiler_t *comp, py_parse_node_t pn) { |
| 2177 | if (PY_PARSE_NODE_IS_NULL(pn)) { |
| 2178 | // pass |
| 2179 | } else if (PY_PARSE_NODE_IS_LEAF(pn)) { |
| 2180 | int arg = PY_PARSE_NODE_LEAF_ARG(pn); |
| 2181 | switch (PY_PARSE_NODE_LEAF_KIND(pn)) { |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 2182 | case PY_PARSE_NODE_ID: EMIT(load_id, arg); break; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2183 | case PY_PARSE_NODE_SMALL_INT: EMIT(load_const_small_int, arg); break; |
| 2184 | case PY_PARSE_NODE_INTEGER: EMIT(load_const_int, arg); break; |
| 2185 | case PY_PARSE_NODE_DECIMAL: EMIT(load_const_dec, arg); break; |
| 2186 | case PY_PARSE_NODE_STRING: EMIT(load_const_str, arg, false); break; |
| 2187 | case PY_PARSE_NODE_BYTES: EMIT(load_const_str, arg, true); break; |
| 2188 | case PY_PARSE_NODE_TOKEN: EMIT(load_const_tok, arg); break; |
| 2189 | default: assert(0); |
| 2190 | } |
| 2191 | } else { |
| 2192 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 2193 | compile_function_t f = compile_function[PY_PARSE_NODE_STRUCT_KIND(pns)]; |
| 2194 | if (f == NULL) { |
| 2195 | printf("node %u cannot be compiled\n", (uint)PY_PARSE_NODE_STRUCT_KIND(pns)); |
| 2196 | parse_node_show(pn, 0); |
| 2197 | assert(0); |
| 2198 | } else { |
| 2199 | f(comp, pns); |
| 2200 | } |
| 2201 | } |
| 2202 | } |
| 2203 | |
| 2204 | void compile_scope_func_lambda_param(compiler_t *comp, py_parse_node_t pn, pn_kind_t pn_name, pn_kind_t pn_star, pn_kind_t pn_dbl_star, bool allow_annotations) { |
| 2205 | // TODO verify that *k and **k are last etc |
| 2206 | assert(PY_PARSE_NODE_IS_STRUCT(pn)); |
| 2207 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; |
| 2208 | qstr param_name = 0; |
| 2209 | py_parse_node_t pn_annotation = PY_PARSE_NODE_NULL; |
| 2210 | if (PY_PARSE_NODE_STRUCT_KIND(pns) == pn_name) { |
| 2211 | param_name = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
| 2212 | //int node_index = 1; unused |
| 2213 | if (allow_annotations) { |
| 2214 | if (!PY_PARSE_NODE_IS_NULL(pns->nodes[1])) { |
| 2215 | // this parameter has an annotation |
| 2216 | pn_annotation = pns->nodes[1]; |
| 2217 | } |
| 2218 | //node_index = 2; unused |
| 2219 | } |
| 2220 | /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param |
| 2221 | if (!PY_PARSE_NODE_IS_NULL(pns->nodes[node_index])) { |
| 2222 | // this parameter has a default value |
| 2223 | if (comp->have_bare_star) { |
| 2224 | comp->scope_cur->num_dict_params += 1; |
| 2225 | } else { |
| 2226 | comp->scope_cur->num_default_params += 1; |
| 2227 | } |
| 2228 | } |
| 2229 | */ |
| 2230 | if (comp->have_bare_star) { |
| 2231 | // comes after a bare star, so doesn't count as a parameter |
| 2232 | } else { |
| 2233 | comp->scope_cur->num_params += 1; |
| 2234 | } |
| 2235 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == pn_star) { |
| 2236 | if (PY_PARSE_NODE_IS_NULL(pns->nodes[0])) { |
| 2237 | // bare star |
| 2238 | // TODO see http://www.python.org/dev/peps/pep-3102/ |
| 2239 | comp->have_bare_star = true; |
| 2240 | //assert(comp->scope_cur->num_dict_params == 0); |
| 2241 | } else if (PY_PARSE_NODE_IS_ID(pns->nodes[0])) { |
| 2242 | // named star |
| 2243 | comp->scope_cur->flags |= SCOPE_FLAG_VARARGS; |
| 2244 | param_name = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
| 2245 | } else if (allow_annotations && PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) { |
| 2246 | // named star with annotation |
| 2247 | comp->scope_cur->flags |= SCOPE_FLAG_VARARGS; |
| 2248 | pns = (py_parse_node_struct_t*)pns->nodes[0]; |
| 2249 | param_name = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
| 2250 | pn_annotation = pns->nodes[1]; |
| 2251 | } else { |
| 2252 | // shouldn't happen |
| 2253 | assert(0); |
| 2254 | } |
| 2255 | } else if (PY_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) { |
| 2256 | param_name = PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]); |
| 2257 | if (allow_annotations && !PY_PARSE_NODE_IS_NULL(pns->nodes[1])) { |
| 2258 | // this parameter has an annotation |
| 2259 | pn_annotation = pns->nodes[1]; |
| 2260 | } |
| 2261 | comp->scope_cur->flags |= SCOPE_FLAG_VARKEYWORDS; |
| 2262 | } else { |
| 2263 | // TODO anything to implement? |
| 2264 | assert(0); |
| 2265 | } |
| 2266 | |
| 2267 | if (param_name != 0) { |
| 2268 | if (!PY_PARSE_NODE_IS_NULL(pn_annotation)) { |
| 2269 | // TODO this parameter has an annotation |
| 2270 | } |
| 2271 | bool added; |
| 2272 | id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added); |
| 2273 | if (!added) { |
| 2274 | printf("SyntaxError?: same name used for parameter; %s\n", qstr_str(param_name)); |
| 2275 | return; |
| 2276 | } |
| 2277 | id_info->param = true; |
| 2278 | id_info->kind = ID_INFO_KIND_LOCAL; |
| 2279 | } |
| 2280 | } |
| 2281 | |
| 2282 | void compile_scope_func_param(compiler_t *comp, py_parse_node_t pn) { |
| 2283 | compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true); |
| 2284 | } |
| 2285 | |
| 2286 | void compile_scope_lambda_param(compiler_t *comp, py_parse_node_t pn) { |
| 2287 | compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false); |
| 2288 | } |
| 2289 | |
| 2290 | void compile_scope_comp_iter(compiler_t *comp, py_parse_node_t pn_iter, py_parse_node_t pn_inner_expr, int l_top, int for_depth) { |
| 2291 | tail_recursion: |
| 2292 | if (PY_PARSE_NODE_IS_NULL(pn_iter)) { |
| 2293 | // no more nested if/for; compile inner expression |
| 2294 | compile_node(comp, pn_inner_expr); |
| 2295 | if (comp->scope_cur->kind == SCOPE_LIST_COMP) { |
| 2296 | EMIT(list_append, for_depth + 2); |
| 2297 | } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) { |
| 2298 | EMIT(map_add, for_depth + 2); |
| 2299 | } else if (comp->scope_cur->kind == SCOPE_SET_COMP) { |
| 2300 | EMIT(set_add, for_depth + 2); |
| 2301 | } else { |
| 2302 | EMIT(yield_value); |
| 2303 | EMIT(pop_top); |
| 2304 | } |
| 2305 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) { |
| 2306 | // if condition |
| 2307 | py_parse_node_struct_t *pns_comp_if = (py_parse_node_struct_t*)pn_iter; |
| 2308 | c_if_cond(comp, pns_comp_if->nodes[0], false, l_top); |
| 2309 | pn_iter = pns_comp_if->nodes[1]; |
| 2310 | goto tail_recursion; |
| 2311 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) { |
| 2312 | // for loop |
| 2313 | py_parse_node_struct_t *pns_comp_for2 = (py_parse_node_struct_t*)pn_iter; |
| 2314 | compile_node(comp, pns_comp_for2->nodes[1]); |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 2315 | int l_end2 = comp_next_label(comp); |
| 2316 | int l_top2 = comp_next_label(comp); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2317 | EMIT(get_iter); |
| 2318 | EMIT(label_assign, l_top2); |
| 2319 | EMIT(for_iter, l_end2); |
| 2320 | c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE); |
| 2321 | compile_scope_comp_iter(comp, pns_comp_for2->nodes[2], pn_inner_expr, l_top2, for_depth + 1); |
| 2322 | EMIT(jump, l_top2); |
| 2323 | EMIT(label_assign, l_end2); |
| 2324 | EMIT(for_iter_end); |
| 2325 | } else { |
| 2326 | // shouldn't happen |
| 2327 | assert(0); |
| 2328 | } |
| 2329 | } |
| 2330 | |
| 2331 | void check_for_doc_string(compiler_t *comp, py_parse_node_t pn) { |
| 2332 | // see http://www.python.org/dev/peps/pep-0257/ |
| 2333 | |
| 2334 | // look for the first statement |
| 2335 | if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) { |
| 2336 | // fall through |
| 2337 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) { |
| 2338 | pn = ((py_parse_node_struct_t*)pn)->nodes[0]; |
| 2339 | } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) { |
| 2340 | pn = ((py_parse_node_struct_t*)pn)->nodes[0]; |
| 2341 | } else { |
| 2342 | return; |
| 2343 | } |
| 2344 | |
| 2345 | // check the first statement for a doc string |
| 2346 | if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) { |
| 2347 | py_parse_node_struct_t* pns = (py_parse_node_struct_t*)pn; |
| 2348 | if (PY_PARSE_NODE_IS_LEAF(pns->nodes[0])) { |
| 2349 | int kind = PY_PARSE_NODE_LEAF_KIND(pns->nodes[0]); |
| 2350 | if (kind == PY_PARSE_NODE_STRING) { |
| 2351 | compile_node(comp, pns->nodes[0]); // a doc string |
| 2352 | // store doc string |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 2353 | EMIT(store_id, comp->qstr___doc__); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2354 | } |
| 2355 | } |
| 2356 | } |
| 2357 | } |
| 2358 | |
| 2359 | void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) { |
| 2360 | comp->pass = pass; |
| 2361 | comp->scope_cur = scope; |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 2362 | comp->next_label = 1; |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 2363 | EMIT(start_pass, pass, scope); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2364 | |
| 2365 | if (comp->pass == PASS_1) { |
| 2366 | scope->stack_size = 0; |
| 2367 | } |
| 2368 | |
| 2369 | if (comp->pass == PASS_3) { |
| 2370 | //printf("----\n"); |
| 2371 | scope_print_info(scope); |
| 2372 | } |
| 2373 | |
| 2374 | // compile |
| 2375 | if (scope->kind == SCOPE_MODULE) { |
| 2376 | check_for_doc_string(comp, scope->pn); |
| 2377 | compile_node(comp, scope->pn); |
| 2378 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 2379 | EMIT(return_value); |
| 2380 | } else if (scope->kind == SCOPE_FUNCTION) { |
| 2381 | assert(PY_PARSE_NODE_IS_STRUCT(scope->pn)); |
| 2382 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)scope->pn; |
| 2383 | assert(PY_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef); |
| 2384 | |
| 2385 | // 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] | 2386 | // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc) |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2387 | if (comp->pass == PASS_1) { |
| 2388 | comp->have_bare_star = false; |
| 2389 | apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param); |
| 2390 | } |
| 2391 | |
| 2392 | assert(pns->nodes[2] == 0); // 2 is something... |
| 2393 | |
| 2394 | compile_node(comp, pns->nodes[3]); // 3 is function body |
| 2395 | // emit return if it wasn't the last opcode |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 2396 | if (!EMIT(last_emit_was_return_value)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2397 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 2398 | EMIT(return_value); |
| 2399 | } |
| 2400 | } else if (scope->kind == SCOPE_LAMBDA) { |
| 2401 | assert(PY_PARSE_NODE_IS_STRUCT(scope->pn)); |
| 2402 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)scope->pn; |
| 2403 | assert(PY_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3); |
| 2404 | |
| 2405 | // 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] | 2406 | // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc) |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2407 | if (comp->pass == PASS_1) { |
| 2408 | comp->have_bare_star = false; |
| 2409 | apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param); |
| 2410 | } |
| 2411 | |
| 2412 | compile_node(comp, pns->nodes[1]); // 1 is lambda body |
| 2413 | EMIT(return_value); |
| 2414 | } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) { |
| 2415 | // a bit of a hack at the moment |
| 2416 | |
| 2417 | assert(PY_PARSE_NODE_IS_STRUCT(scope->pn)); |
| 2418 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)scope->pn; |
| 2419 | assert(PY_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2); |
| 2420 | assert(PY_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for)); |
| 2421 | py_parse_node_struct_t *pns_comp_for = (py_parse_node_struct_t*)pns->nodes[1]; |
| 2422 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 2423 | qstr qstr_arg = qstr_from_str_static(".0"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2424 | if (comp->pass == PASS_1) { |
| 2425 | bool added; |
| 2426 | id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added); |
| 2427 | assert(added); |
| 2428 | id_info->kind = ID_INFO_KIND_LOCAL; |
| 2429 | scope->num_params = 1; |
| 2430 | } |
| 2431 | |
| 2432 | if (scope->kind == SCOPE_LIST_COMP) { |
| 2433 | EMIT(build_list, 0); |
| 2434 | } else if (scope->kind == SCOPE_DICT_COMP) { |
| 2435 | EMIT(build_map, 0); |
| 2436 | } else if (scope->kind == SCOPE_SET_COMP) { |
| 2437 | EMIT(build_set, 0); |
| 2438 | } |
| 2439 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 2440 | int l_end = comp_next_label(comp); |
| 2441 | int l_top = comp_next_label(comp); |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 2442 | EMIT(load_id, qstr_arg); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2443 | EMIT(label_assign, l_top); |
| 2444 | EMIT(for_iter, l_end); |
| 2445 | c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE); |
| 2446 | compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0); |
| 2447 | EMIT(jump, l_top); |
| 2448 | EMIT(label_assign, l_end); |
| 2449 | EMIT(for_iter_end); |
| 2450 | |
| 2451 | if (scope->kind == SCOPE_GEN_EXPR) { |
| 2452 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 2453 | } |
| 2454 | EMIT(return_value); |
| 2455 | } else { |
| 2456 | assert(scope->kind == SCOPE_CLASS); |
| 2457 | assert(PY_PARSE_NODE_IS_STRUCT(scope->pn)); |
| 2458 | py_parse_node_struct_t *pns = (py_parse_node_struct_t*)scope->pn; |
| 2459 | assert(PY_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef); |
| 2460 | |
| 2461 | if (comp->pass == PASS_1) { |
| 2462 | bool added; |
| 2463 | id_info_t *id_info = scope_find_or_add_id(scope, comp->qstr___class__, &added); |
| 2464 | assert(added); |
| 2465 | id_info->kind = ID_INFO_KIND_LOCAL; |
| 2466 | id_info = scope_find_or_add_id(scope, comp->qstr___locals__, &added); |
| 2467 | assert(added); |
| 2468 | id_info->kind = ID_INFO_KIND_LOCAL; |
| 2469 | id_info->param = true; |
| 2470 | scope->num_params = 1; // __locals__ is the parameter |
| 2471 | } |
| 2472 | |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 2473 | EMIT(load_id, comp->qstr___locals__); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2474 | EMIT(store_locals); |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 2475 | EMIT(load_id, comp->qstr___name__); |
| 2476 | EMIT(store_id, comp->qstr___module__); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2477 | EMIT(load_const_id, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 2478 | EMIT(store_id, comp->qstr___qualname__); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2479 | |
| 2480 | check_for_doc_string(comp, pns->nodes[2]); |
| 2481 | compile_node(comp, pns->nodes[2]); // 2 is class body |
| 2482 | |
| 2483 | id_info_t *id = scope_find(scope, comp->qstr___class__); |
| 2484 | assert(id != NULL); |
| 2485 | if (id->kind == ID_INFO_KIND_LOCAL) { |
| 2486 | EMIT(load_const_tok, PY_TOKEN_KW_NONE); |
| 2487 | } else { |
| 2488 | EMIT(load_closure, comp->qstr___class__); |
| 2489 | } |
| 2490 | EMIT(return_value); |
| 2491 | } |
| 2492 | |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 2493 | EMIT(end_pass); |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 2494 | |
| 2495 | // update maximim number of labels needed |
| 2496 | if (comp->next_label > comp->max_num_labels) { |
| 2497 | comp->max_num_labels = comp->next_label; |
| 2498 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2499 | } |
| 2500 | |
| 2501 | void compile_scope_compute_things(compiler_t *comp, scope_t *scope) { |
| 2502 | // in functions, turn implicit globals into explicit globals |
| 2503 | // compute num_locals, and the index of each local |
| 2504 | scope->num_locals = 0; |
| 2505 | for (int i = 0; i < scope->id_info_len; i++) { |
| 2506 | id_info_t *id = &scope->id_info[i]; |
| 2507 | if (scope->kind == SCOPE_CLASS && id->qstr == comp->qstr___class__) { |
| 2508 | // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL |
| 2509 | continue; |
| 2510 | } |
| 2511 | if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) { |
| 2512 | id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT; |
| 2513 | } |
| 2514 | if (id->param || id->kind == ID_INFO_KIND_LOCAL) { |
| 2515 | id->local_num = scope->num_locals; |
| 2516 | scope->num_locals += 1; |
| 2517 | } |
| 2518 | } |
| 2519 | |
| 2520 | // compute flags |
| 2521 | //scope->flags = 0; since we set some things in parameters |
| 2522 | if (scope->kind != SCOPE_MODULE) { |
| 2523 | scope->flags |= SCOPE_FLAG_NEWLOCALS; |
| 2524 | } |
| 2525 | if (scope->kind == SCOPE_FUNCTION || scope->kind == SCOPE_LAMBDA || scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) { |
| 2526 | assert(scope->parent != NULL); |
| 2527 | scope->flags |= SCOPE_FLAG_OPTIMISED; |
| 2528 | |
| 2529 | // TODO possibly other ways it can be nested |
| 2530 | if (scope->parent->kind == SCOPE_FUNCTION || (scope->parent->kind == SCOPE_CLASS && scope->parent->parent->kind == SCOPE_FUNCTION)) { |
| 2531 | scope->flags |= SCOPE_FLAG_NESTED; |
| 2532 | } |
| 2533 | } |
| 2534 | int num_free = 0; |
| 2535 | for (int i = 0; i < scope->id_info_len; i++) { |
| 2536 | id_info_t *id = &scope->id_info[i]; |
| 2537 | if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) { |
| 2538 | num_free += 1; |
| 2539 | } |
| 2540 | } |
| 2541 | if (num_free == 0) { |
| 2542 | scope->flags |= SCOPE_FLAG_NOFREE; |
| 2543 | } |
| 2544 | } |
| 2545 | |
| 2546 | void py_compile(py_parse_node_t pn) { |
| 2547 | compiler_t *comp = m_new(compiler_t, 1); |
| 2548 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 2549 | comp->qstr___class__ = qstr_from_str_static("__class__"); |
| 2550 | comp->qstr___locals__ = qstr_from_str_static("__locals__"); |
| 2551 | comp->qstr___name__ = qstr_from_str_static("__name__"); |
| 2552 | comp->qstr___module__ = qstr_from_str_static("__module__"); |
| 2553 | comp->qstr___qualname__ = qstr_from_str_static("__qualname__"); |
| 2554 | comp->qstr___doc__ = qstr_from_str_static("__doc__"); |
| 2555 | comp->qstr_assertion_error = qstr_from_str_static("AssertionError"); |
| 2556 | comp->qstr_micropython = qstr_from_str_static("micropython"); |
| 2557 | comp->qstr_native = qstr_from_str_static("native"); |
Damien | 5bfb759 | 2013-10-05 18:41:24 +0100 | [diff] [blame^] | 2558 | comp->qstr_asm_thumb = qstr_from_str_static("asm_thumb"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2559 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 2560 | comp->max_num_labels = 0; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2561 | comp->break_label = 0; |
| 2562 | comp->continue_label = 0; |
| 2563 | comp->except_nest_level = 0; |
| 2564 | comp->scope_head = NULL; |
| 2565 | comp->scope_cur = NULL; |
| 2566 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 2567 | comp->emit = emit_pass1_new(comp->qstr___class__); |
| 2568 | comp->emit_method_table = &emit_pass1_method_table; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2569 | |
| 2570 | pn = fold_constants(pn); |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 2571 | scope_new_and_link(comp, SCOPE_MODULE, pn, EMIT_OPT_NONE); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2572 | |
| 2573 | for (scope_t *s = comp->scope_head; s != NULL; s = s->next) { |
| 2574 | compile_scope(comp, s, PASS_1); |
| 2575 | } |
| 2576 | |
| 2577 | for (scope_t *s = comp->scope_head; s != NULL; s = s->next) { |
| 2578 | compile_scope_compute_things(comp, s); |
| 2579 | } |
| 2580 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 2581 | emit_pass1_free(comp->emit); |
| 2582 | |
| 2583 | emit_t *emit_bc = NULL; |
| 2584 | emit_t *emit_x64 = NULL; |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 2585 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2586 | for (scope_t *s = comp->scope_head; s != NULL; s = s->next) { |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 2587 | switch (s->emit_options) { |
| 2588 | case EMIT_OPT_NATIVE_PYTHON: |
| 2589 | if (emit_x64 == NULL) { |
| 2590 | emit_x64 = emit_x64_new(comp->max_num_labels); |
| 2591 | } |
| 2592 | comp->emit = emit_x64; |
| 2593 | comp->emit_method_table = &emit_x64_method_table; |
| 2594 | break; |
| 2595 | |
Damien | 5bfb759 | 2013-10-05 18:41:24 +0100 | [diff] [blame^] | 2596 | //case EMIT_OPT_ASM_THUMB: |
| 2597 | //if (em |
| 2598 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 2599 | default: |
| 2600 | if (emit_bc == NULL) { |
| 2601 | emit_bc = emit_bc_new(comp->max_num_labels); |
| 2602 | } |
| 2603 | comp->emit = emit_bc; |
| 2604 | comp->emit_method_table = &emit_bc_method_table; |
| 2605 | break; |
| 2606 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 2607 | compile_scope(comp, s, PASS_2); |
| 2608 | compile_scope(comp, s, PASS_3); |
| 2609 | } |
| 2610 | |
| 2611 | m_free(comp); |
| 2612 | } |