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