py/compile: Use switch-case to match token and operator.
Reduces code size.
This commit is contained in:
parent
a040fb89e7
commit
91a385db98
50
py/compile.c
50
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) {
|
||||
|
|
Loading…
Reference in New Issue