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