Skip to content

Commit a040fb8

Browse files
blazewiczdpgeorge
authored andcommitted
py/compile: Combine arith and bit-shift ops into 1 compile routine.
This refactoring saves code space.
1 parent f110dbd commit a040fb8

2 files changed

Lines changed: 12 additions & 31 deletions

File tree

py/compile.c

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2132,48 +2132,29 @@ STATIC void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
21322132
c_binary_op(comp, pns, MP_BINARY_OP_AND);
21332133
}
21342134

2135-
STATIC void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2136-
int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
2137-
compile_node(comp, pns->nodes[0]);
2138-
for (int i = 1; i + 1 < num_nodes; i += 2) {
2139-
compile_node(comp, pns->nodes[i + 1]);
2140-
if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
2141-
EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
2142-
} else {
2143-
assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)); // should be
2144-
EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
2145-
}
2146-
}
2147-
}
2148-
2149-
STATIC void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
2135+
STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
21502136
int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
21512137
compile_node(comp, pns->nodes[0]);
21522138
for (int i = 1; i + 1 < num_nodes; i += 2) {
21532139
compile_node(comp, pns->nodes[i + 1]);
21542140
if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
21552141
EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
2156-
} else {
2157-
assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)); // should be
2142+
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
21582143
EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
2159-
}
2160-
}
2161-
}
2162-
2163-
STATIC void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
2164-
int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
2165-
compile_node(comp, pns->nodes[0]);
2166-
for (int i = 1; i + 1 < num_nodes; i += 2) {
2167-
compile_node(comp, pns->nodes[i + 1]);
2168-
if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
2144+
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
21692145
EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
21702146
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
21712147
EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
21722148
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
21732149
EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
2174-
} else {
2175-
assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)); // should be
2150+
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
21762151
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);
21772158
}
21782159
}
21792160
}

py/grammar.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,9 @@ DEF_RULE(star_expr, c(star_expr), and(2), tok(OP_STAR), rule(expr))
244244
DEF_RULE(expr, c(expr), list, rule(xor_expr), tok(OP_PIPE))
245245
DEF_RULE(xor_expr, c(xor_expr), list, rule(and_expr), tok(OP_CARET))
246246
DEF_RULE(and_expr, c(and_expr), list, rule(shift_expr), tok(OP_AMPERSAND))
247-
DEF_RULE(shift_expr, c(shift_expr), list, rule(arith_expr), rule(shift_op))
247+
DEF_RULE(shift_expr, c(term), list, rule(arith_expr), rule(shift_op))
248248
DEF_RULE_NC(shift_op, or(2), tok(OP_DBL_LESS), tok(OP_DBL_MORE))
249-
DEF_RULE(arith_expr, c(arith_expr), list, rule(term), rule(arith_op))
249+
DEF_RULE(arith_expr, c(term), list, rule(term), rule(arith_op))
250250
DEF_RULE_NC(arith_op, or(2), tok(OP_PLUS), tok(OP_MINUS))
251251
DEF_RULE(term, c(term), list, rule(factor), rule(term_op))
252252
DEF_RULE_NC(term_op, or(4), tok(OP_STAR), tok(OP_SLASH), tok(OP_PERCENT), tok(OP_DBL_SLASH))

0 commit comments

Comments
 (0)