|
17 | 17 | #include <math.h> |
18 | 18 | #endif |
19 | 19 |
|
20 | | -// This dispatcher function is expected to be independent of the implementation |
21 | | -// of long int |
22 | | -STATIC mp_obj_t int_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { |
| 20 | +// This dispatcher function is expected to be independent of the implementation of long int |
| 21 | +STATIC mp_obj_t mp_obj_int_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { |
23 | 22 | // TODO check n_kw == 0 |
24 | 23 |
|
25 | 24 | switch (n_args) { |
@@ -56,26 +55,20 @@ STATIC mp_obj_t int_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_ |
56 | 55 |
|
57 | 56 | #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE |
58 | 57 |
|
59 | | -void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { |
| 58 | +void mp_obj_int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { |
60 | 59 | if (MP_OBJ_IS_SMALL_INT(self_in)) { |
61 | 60 | print(env, INT_FMT, MP_OBJ_SMALL_INT_VALUE(self_in)); |
62 | 61 | } |
63 | 62 | } |
64 | 63 |
|
65 | 64 | // This is called for operations on SMALL_INT that are not handled by mp_unary_op |
66 | | -mp_obj_t int_unary_op(int op, mp_obj_t o_in) { |
| 65 | +mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) { |
67 | 66 | return MP_OBJ_NULL; |
68 | 67 | } |
69 | 68 |
|
70 | 69 | // This is called for operations on SMALL_INT that are not handled by mp_binary_op |
71 | | -mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { |
72 | | - if (op == MP_BINARY_OP_MULTIPLY) { |
73 | | - if (MP_OBJ_IS_STR(rhs_in) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_list)) { |
74 | | - // multiply is commutative for these types, so delegate to them |
75 | | - return mp_binary_op(op, rhs_in, lhs_in); |
76 | | - } |
77 | | - } |
78 | | - return MP_OBJ_NULL; |
| 70 | +mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { |
| 71 | + return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in); |
79 | 72 | } |
80 | 73 |
|
81 | 74 | // This is called only with strings whose value doesn't fit in SMALL_INT |
@@ -124,11 +117,29 @@ mp_float_t mp_obj_int_as_float(mp_obj_t self_in) { |
124 | 117 |
|
125 | 118 | #endif // MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE |
126 | 119 |
|
| 120 | +// This dispatcher function is expected to be independent of the implementation of long int |
| 121 | +// It handles the extra cases for integer-like arithmetic |
| 122 | +mp_obj_t mp_obj_int_binary_op_extra_cases(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { |
| 123 | + if (rhs_in == mp_const_false) { |
| 124 | + // false acts as 0 |
| 125 | + return mp_binary_op(op, lhs_in, MP_OBJ_NEW_SMALL_INT(0)); |
| 126 | + } else if (rhs_in == mp_const_true) { |
| 127 | + // true acts as 0 |
| 128 | + return mp_binary_op(op, lhs_in, MP_OBJ_NEW_SMALL_INT(1)); |
| 129 | + } else if (op == MP_BINARY_OP_MULTIPLY) { |
| 130 | + if (MP_OBJ_IS_STR(rhs_in) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_list)) { |
| 131 | + // multiply is commutative for these types, so delegate to them |
| 132 | + return mp_binary_op(op, rhs_in, lhs_in); |
| 133 | + } |
| 134 | + } |
| 135 | + return MP_OBJ_NULL; |
| 136 | +} |
| 137 | + |
127 | 138 | const mp_obj_type_t mp_type_int = { |
128 | 139 | { &mp_type_type }, |
129 | 140 | .name = MP_QSTR_int, |
130 | | - .print = int_print, |
131 | | - .make_new = int_make_new, |
132 | | - .unary_op = int_unary_op, |
133 | | - .binary_op = int_binary_op, |
| 141 | + .print = mp_obj_int_print, |
| 142 | + .make_new = mp_obj_int_make_new, |
| 143 | + .unary_op = mp_obj_int_unary_op, |
| 144 | + .binary_op = mp_obj_int_binary_op, |
134 | 145 | }; |
0 commit comments