py/compile: Combine arith and bit-shift ops into 1 compile routine.

This refactoring saves code space.
This commit is contained in:
Krzysztof Blazewicz
2017-04-27 21:32:50 +02:00
committed by Damien George
parent f110dbd795
commit a040fb89e7
2 changed files with 12 additions and 31 deletions

View File

@@ -2132,48 +2132,29 @@ STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
c_binary_op(comp, pns, MP_BINARY_OP_AND);
}
STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(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_DBL_LESS)) {
EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
} else {
assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be
EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
}
}
}
STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(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 {
assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
}
}
}
STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(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_STAR)) {
} 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 {
assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be
} 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);
}
}
}