Skip to content

Commit 91a385d

Browse files
blazewiczdpgeorge
authored andcommitted
py/compile: Use switch-case to match token and operator.
Reduces code size.
1 parent a040fb8 commit 91a385d

1 file changed

Lines changed: 25 additions & 25 deletions

File tree

py/compile.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,38 +2137,38 @@ STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
21372137
compile_node(comp, pns->nodes[0]);
21382138
for (int i = 1; i + 1 < num_nodes; i += 2) {
21392139
compile_node(comp, pns->nodes[i + 1]);
2140-
if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
2141-
EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
2142-
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
2143-
EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
2144-
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
2145-
EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
2146-
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
2147-
EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
2148-
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
2149-
EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
2150-
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
2151-
EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
2152-
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
2153-
EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
2154-
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
2155-
EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
2156-
} else {
2157-
assert(false);
2140+
mp_binary_op_t op;
2141+
mp_token_kind_t tok = MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]);
2142+
switch (tok) {
2143+
case MP_TOKEN_OP_PLUS: op = MP_BINARY_OP_ADD; break;
2144+
case MP_TOKEN_OP_MINUS: op = MP_BINARY_OP_SUBTRACT; break;
2145+
case MP_TOKEN_OP_STAR: op = MP_BINARY_OP_MULTIPLY; break;
2146+
case MP_TOKEN_OP_DBL_SLASH: op = MP_BINARY_OP_FLOOR_DIVIDE; break;
2147+
case MP_TOKEN_OP_SLASH: op = MP_BINARY_OP_TRUE_DIVIDE; break;
2148+
case MP_TOKEN_OP_PERCENT: op = MP_BINARY_OP_MODULO; break;
2149+
case MP_TOKEN_OP_DBL_LESS: op = MP_BINARY_OP_LSHIFT; break;
2150+
default:
2151+
assert(tok == MP_TOKEN_OP_DBL_MORE);
2152+
op = MP_BINARY_OP_RSHIFT;
2153+
break;
21582154
}
2155+
EMIT_ARG(binary_op, op);
21592156
}
21602157
}
21612158

21622159
STATIC void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
21632160
compile_node(comp, pns->nodes[1]);
2164-
if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
2165-
EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
2166-
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
2167-
EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
2168-
} else {
2169-
assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
2170-
EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
2161+
mp_binary_op_t op;
2162+
mp_token_kind_t tok = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
2163+
switch (tok) {
2164+
case MP_TOKEN_OP_PLUS: op = MP_UNARY_OP_POSITIVE; break;
2165+
case MP_TOKEN_OP_MINUS: op = MP_UNARY_OP_NEGATIVE; break;
2166+
default:
2167+
assert(tok == MP_TOKEN_OP_TILDE);
2168+
op = MP_UNARY_OP_INVERT;
2169+
break;
21712170
}
2171+
EMIT_ARG(unary_op, op);
21722172
}
21732173

21742174
STATIC void compile_atom_expr_normal(compiler_t *comp, mp_parse_node_struct_t *pns) {

0 commit comments

Comments
 (0)