Skip to content

Commit 20f1bd0

Browse files
committed
py/compile2: Combine arith and bit-shift ops into 1 compile routine.
A port of a040fb8
1 parent 4ddb838 commit 20f1bd0

File tree

1 file changed

+14
-39
lines changed

1 file changed

+14
-39
lines changed

py/compile2.c

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,52 +2035,27 @@ STATIC void compile_and_expr(compiler_t *comp, const byte *p, const byte *ptop)
20352035
c_binary_op(comp, p, ptop, MP_BINARY_OP_AND);
20362036
}
20372037

2038-
STATIC void compile_shift_expr(compiler_t *comp, const byte *p, const byte *ptop) {
2039-
p = compile_node(comp, p);
2040-
while (p != ptop) {
2041-
byte tok;
2042-
p = pt_tok_extract(p, &tok);
2043-
p = compile_node(comp, p);
2044-
if (tok == MP_TOKEN_OP_DBL_LESS) {
2045-
EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
2046-
} else {
2047-
assert(tok == MP_TOKEN_OP_DBL_MORE); // should be
2048-
EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
2049-
}
2050-
}
2051-
}
2052-
2053-
STATIC void compile_arith_expr(compiler_t *comp, const byte *p, const byte *ptop) {
2054-
p = compile_node(comp, p);
2055-
while (p != ptop) {
2056-
byte tok;
2057-
p = pt_tok_extract(p, &tok);
2058-
p = compile_node(comp, p);
2059-
if (tok == MP_TOKEN_OP_PLUS) {
2060-
EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
2061-
} else {
2062-
assert(tok == MP_TOKEN_OP_MINUS); // should be
2063-
EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
2064-
}
2065-
}
2066-
}
2067-
20682038
STATIC void compile_term(compiler_t *comp, const byte *p, const byte *ptop) {
20692039
p = compile_node(comp, p);
20702040
while (p != ptop) {
20712041
byte tok;
20722042
p = pt_tok_extract(p, &tok);
20732043
p = compile_node(comp, p);
2074-
if (tok == MP_TOKEN_OP_STAR) {
2075-
EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
2076-
} else if (tok == MP_TOKEN_OP_DBL_SLASH) {
2077-
EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
2078-
} else if (tok == MP_TOKEN_OP_SLASH) {
2079-
EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
2080-
} else {
2081-
assert(tok == MP_TOKEN_OP_PERCENT); // should be
2082-
EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
2044+
mp_binary_op_t op;
2045+
switch (tok) {
2046+
case MP_TOKEN_OP_PLUS: op = MP_BINARY_OP_ADD; break;
2047+
case MP_TOKEN_OP_MINUS: op = MP_BINARY_OP_SUBTRACT; break;
2048+
case MP_TOKEN_OP_STAR: op = MP_BINARY_OP_MULTIPLY; break;
2049+
case MP_TOKEN_OP_DBL_SLASH: op = MP_BINARY_OP_FLOOR_DIVIDE; break;
2050+
case MP_TOKEN_OP_SLASH: op = MP_BINARY_OP_TRUE_DIVIDE; break;
2051+
case MP_TOKEN_OP_PERCENT: op = MP_BINARY_OP_MODULO; break;
2052+
case MP_TOKEN_OP_DBL_LESS: op = MP_BINARY_OP_LSHIFT; break;
2053+
default:
2054+
assert(tok == MP_TOKEN_OP_DBL_MORE);
2055+
op = MP_BINARY_OP_RSHIFT;
2056+
break;
20832057
}
2058+
EMIT_ARG(binary_op, op);
20842059
}
20852060
}
20862061

0 commit comments

Comments
 (0)