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