diff options
author | Krzysztof Blazewicz <blazewicz.krzysztof@gmail.com> | 2017-04-27 21:33:11 +0200 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2017-07-05 15:50:36 +1000 |
commit | 91a385db98711c2a9115ad661cb4d637450d7ee2 (patch) | |
tree | b08bdc2f5d2b6c4451248b4fcc5f2831b80d0134 /py | |
parent | a040fb89e7b8507aa775b0620de1770642b0f5ee (diff) | |
download | micropython-91a385db98711c2a9115ad661cb4d637450d7ee2.tar.gz micropython-91a385db98711c2a9115ad661cb4d637450d7ee2.zip |
py/compile: Use switch-case to match token and operator.
Reduces code size.
Diffstat (limited to 'py')
-rw-r--r-- | py/compile.c | 50 |
1 files changed, 25 insertions, 25 deletions
diff --git a/py/compile.c b/py/compile.c index f284938f38..70e7b39312 100644 --- a/py/compile.c +++ b/py/compile.c @@ -2137,38 +2137,38 @@ STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) { compile_node(comp, pns->nodes[0]); for (int i = 1; i + 1 < num_nodes; i += 2) { compile_node(comp, pns->nodes[i + 1]); - if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) { - EMIT_ARG(binary_op, MP_BINARY_OP_ADD); - } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) { - EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT); - } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) { - EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY); - } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) { - EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE); - } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) { - EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE); - } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) { - EMIT_ARG(binary_op, MP_BINARY_OP_MODULO); - } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) { - EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT); - } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) { - EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT); - } else { - assert(false); + mp_binary_op_t op; + mp_token_kind_t tok = MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]); + switch (tok) { + case MP_TOKEN_OP_PLUS: op = MP_BINARY_OP_ADD; break; + case MP_TOKEN_OP_MINUS: op = MP_BINARY_OP_SUBTRACT; break; + case MP_TOKEN_OP_STAR: op = MP_BINARY_OP_MULTIPLY; break; + case MP_TOKEN_OP_DBL_SLASH: op = MP_BINARY_OP_FLOOR_DIVIDE; break; + case MP_TOKEN_OP_SLASH: op = MP_BINARY_OP_TRUE_DIVIDE; break; + case MP_TOKEN_OP_PERCENT: op = MP_BINARY_OP_MODULO; break; + case MP_TOKEN_OP_DBL_LESS: op = MP_BINARY_OP_LSHIFT; break; + default: + assert(tok == MP_TOKEN_OP_DBL_MORE); + op = MP_BINARY_OP_RSHIFT; + break; } + EMIT_ARG(binary_op, op); } } STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) { compile_node(comp, pns->nodes[1]); - if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) { - EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE); - } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) { - EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE); - } else { - assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be - EMIT_ARG(unary_op, MP_UNARY_OP_INVERT); + mp_binary_op_t op; + mp_token_kind_t tok = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); + switch (tok) { + case MP_TOKEN_OP_PLUS: op = MP_UNARY_OP_POSITIVE; break; + case MP_TOKEN_OP_MINUS: op = MP_UNARY_OP_NEGATIVE; break; + default: + assert(tok == MP_TOKEN_OP_TILDE); + op = MP_UNARY_OP_INVERT; + break; } + EMIT_ARG(unary_op, op); } STATIC void compile_atom_expr_normal(compiler_t *comp, mp_parse_node_struct_t *pns) { |