Commit aedf583a authored by Damien George's avatar Damien George
Browse files

py: Simplify some logic in compiler; add comments about CPython compat.

parent 7f41f650
...@@ -732,9 +732,7 @@ STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign ...@@ -732,9 +732,7 @@ STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign
assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1])); assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1]; pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
} }
if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) { if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
goto cannot_assign;
} else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
if (assign_kind == ASSIGN_AUG_STORE) { if (assign_kind == ASSIGN_AUG_STORE) {
EMIT(rot_three); EMIT(rot_three);
EMIT(store_subscr); EMIT(store_subscr);
...@@ -818,9 +816,8 @@ STATIC void c_assign_tuple(compiler_t *comp, mp_parse_node_t node_head, uint num ...@@ -818,9 +816,8 @@ STATIC void c_assign_tuple(compiler_t *comp, mp_parse_node_t node_head, uint num
// assigns top of stack to pn // assigns top of stack to pn
STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) { STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
tail_recursion: tail_recursion:
if (MP_PARSE_NODE_IS_NULL(pn)) { assert(!MP_PARSE_NODE_IS_NULL(pn));
assert(0); if (MP_PARSE_NODE_IS_LEAF(pn)) {
} else if (MP_PARSE_NODE_IS_LEAF(pn)) {
if (MP_PARSE_NODE_IS_ID(pn)) { if (MP_PARSE_NODE_IS_ID(pn)) {
qstr arg = MP_PARSE_NODE_LEAF_ARG(pn); qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
switch (assign_kind) { switch (assign_kind) {
...@@ -838,6 +835,7 @@ STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_ ...@@ -838,6 +835,7 @@ STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_
return; return;
} }
} else { } else {
// pn must be a struct
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
switch (MP_PARSE_NODE_STRUCT_KIND(pns)) { switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
case PN_power: case PN_power:
...@@ -1252,10 +1250,7 @@ STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) { ...@@ -1252,10 +1250,7 @@ STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1])); assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1]; pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
} }
if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) { if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
// can't delete function calls
goto cannot_delete;
} else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
compile_node(comp, pns1->nodes[0]); compile_node(comp, pns1->nodes[0]);
EMIT(delete_subscr); EMIT(delete_subscr);
} else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) { } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
...@@ -1291,7 +1286,7 @@ STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) { ...@@ -1291,7 +1286,7 @@ STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
c_del_stmt(comp, pns1->nodes[i]); c_del_stmt(comp, pns1->nodes[i]);
} }
} else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) { } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
// TODO not implemented; can't del comprehension? // TODO not implemented; can't del comprehension? can we get here?
goto cannot_delete; goto cannot_delete;
} else { } else {
// sequence with 2 items // sequence with 2 items
...@@ -1570,6 +1565,7 @@ STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qs ...@@ -1570,6 +1565,7 @@ STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qs
bool added; bool added;
id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added); id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
if (!added) { if (!added) {
// TODO this is not compliant with CPython
compile_syntax_error(comp, pn, "identifier already used"); compile_syntax_error(comp, pn, "identifier already used");
return; return;
} }
...@@ -1596,6 +1592,7 @@ STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr ...@@ -1596,6 +1592,7 @@ STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr
bool added; bool added;
id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added); id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qst, &added);
if (!added) { if (!added) {
// TODO this is not compliant with CPython
compile_syntax_error(comp, pn, "identifier already used"); compile_syntax_error(comp, pn, "identifier already used");
return; return;
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment