py/compile: Combine arith and bit-shift ops into 1 compile routine.
This refactoring saves code space.
This commit is contained in:
parent
f110dbd795
commit
a040fb89e7
39
py/compile.c
39
py/compile.c
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -244,9 +244,9 @@ DEF_RULE(star_expr, c(star_expr), and(2), tok(OP_STAR), rule(expr))
|
|||
DEF_RULE(expr, c(expr), list, rule(xor_expr), tok(OP_PIPE))
|
||||
DEF_RULE(xor_expr, c(xor_expr), list, rule(and_expr), tok(OP_CARET))
|
||||
DEF_RULE(and_expr, c(and_expr), list, rule(shift_expr), tok(OP_AMPERSAND))
|
||||
DEF_RULE(shift_expr, c(shift_expr), list, rule(arith_expr), rule(shift_op))
|
||||
DEF_RULE(shift_expr, c(term), list, rule(arith_expr), rule(shift_op))
|
||||
DEF_RULE_NC(shift_op, or(2), tok(OP_DBL_LESS), tok(OP_DBL_MORE))
|
||||
DEF_RULE(arith_expr, c(arith_expr), list, rule(term), rule(arith_op))
|
||||
DEF_RULE(arith_expr, c(term), list, rule(term), rule(arith_op))
|
||||
DEF_RULE_NC(arith_op, or(2), tok(OP_PLUS), tok(OP_MINUS))
|
||||
DEF_RULE(term, c(term), list, rule(factor), rule(term_op))
|
||||
DEF_RULE_NC(term_op, or(4), tok(OP_STAR), tok(OP_SLASH), tok(OP_PERCENT), tok(OP_DBL_SLASH))
|
||||
|
|
Loading…
Reference in New Issue