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

A port of a040fb89e7
This commit is contained in:
Damien George 2017-08-30 11:26:29 +10:00
parent 4ddb838f8b
commit 20f1bd0a14
1 changed files with 14 additions and 39 deletions

View File

@ -2035,52 +2035,27 @@ STATIC void compile_and_expr(compiler_t *comp, const byte *p, const byte *ptop)
c_binary_op(comp, p, ptop, MP_BINARY_OP_AND); c_binary_op(comp, p, ptop, MP_BINARY_OP_AND);
} }
STATIC void compile_shift_expr(compiler_t *comp, const byte *p, const byte *ptop) {
p = compile_node(comp, p);
while (p != ptop) {
byte tok;
p = pt_tok_extract(p, &tok);
p = compile_node(comp, p);
if (tok == MP_TOKEN_OP_DBL_LESS) {
EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
} else {
assert(tok == MP_TOKEN_OP_DBL_MORE); // should be
EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
}
}
}
STATIC void compile_arith_expr(compiler_t *comp, const byte *p, const byte *ptop) {
p = compile_node(comp, p);
while (p != ptop) {
byte tok;
p = pt_tok_extract(p, &tok);
p = compile_node(comp, p);
if (tok == MP_TOKEN_OP_PLUS) {
EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
} else {
assert(tok == MP_TOKEN_OP_MINUS); // should be
EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
}
}
}
STATIC void compile_term(compiler_t *comp, const byte *p, const byte *ptop) { STATIC void compile_term(compiler_t *comp, const byte *p, const byte *ptop) {
p = compile_node(comp, p); p = compile_node(comp, p);
while (p != ptop) { while (p != ptop) {
byte tok; byte tok;
p = pt_tok_extract(p, &tok); p = pt_tok_extract(p, &tok);
p = compile_node(comp, p); p = compile_node(comp, p);
if (tok == MP_TOKEN_OP_STAR) { mp_binary_op_t op;
EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY); switch (tok) {
} else if (tok == MP_TOKEN_OP_DBL_SLASH) { case MP_TOKEN_OP_PLUS: op = MP_BINARY_OP_ADD; break;
EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE); case MP_TOKEN_OP_MINUS: op = MP_BINARY_OP_SUBTRACT; break;
} else if (tok == MP_TOKEN_OP_SLASH) { case MP_TOKEN_OP_STAR: op = MP_BINARY_OP_MULTIPLY; break;
EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE); case MP_TOKEN_OP_DBL_SLASH: op = MP_BINARY_OP_FLOOR_DIVIDE; break;
} else { case MP_TOKEN_OP_SLASH: op = MP_BINARY_OP_TRUE_DIVIDE; break;
assert(tok == MP_TOKEN_OP_PERCENT); // should be case MP_TOKEN_OP_PERCENT: op = MP_BINARY_OP_MODULO; break;
EMIT_ARG(binary_op, MP_BINARY_OP_MODULO); 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);
} }
} }