Skip to content

Commit e8208a7

Browse files
committed
py: Make False and True act like 0 and 1 for integer arithmetic.
1 parent d7aadcf commit e8208a7

5 files changed

Lines changed: 52 additions & 42 deletions

File tree

py/objbool.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <stdlib.h>
12

23
#include "nlr.h"
34
#include "misc.h"
@@ -43,12 +44,21 @@ STATIC mp_obj_t bool_unary_op(int op, mp_obj_t o_in) {
4344
}
4445
}
4546

47+
STATIC mp_obj_t bool_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
48+
if (MP_BINARY_OP_OR <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
49+
return mp_binary_op(op, MP_OBJ_NEW_SMALL_INT((machine_int_t)mp_obj_is_true(lhs_in)), rhs_in);
50+
}
51+
// operation not supported
52+
return MP_OBJ_NULL;
53+
}
54+
4655
const mp_obj_type_t mp_type_bool = {
4756
{ &mp_type_type },
4857
.name = MP_QSTR_bool,
4958
.print = bool_print,
5059
.make_new = bool_make_new,
5160
.unary_op = bool_unary_op,
61+
.binary_op = bool_binary_op,
5262
};
5363

5464
const mp_obj_bool_t mp_const_false_obj = {{&mp_type_bool}, false};

py/objint.c

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
#include <math.h>
1818
#endif
1919

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) {
2322
// TODO check n_kw == 0
2423

2524
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_
5655

5756
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
5857

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) {
6059
if (MP_OBJ_IS_SMALL_INT(self_in)) {
6160
print(env, INT_FMT, MP_OBJ_SMALL_INT_VALUE(self_in));
6261
}
6362
}
6463

6564
// 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) {
6766
return MP_OBJ_NULL;
6867
}
6968

7069
// 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);
7972
}
8073

8174
// 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) {
124117

125118
#endif // MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
126119

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+
127138
const mp_obj_type_t mp_type_int = {
128139
{ &mp_type_type },
129140
.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,
134145
};

py/objint.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ typedef struct _mp_obj_int_t {
77
#endif
88
} mp_obj_int_t;
99

10-
void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind);
11-
mp_obj_t int_unary_op(int op, mp_obj_t o_in);
12-
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in);
10+
void mp_obj_int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind);
11+
mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in);
12+
mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in);
13+
mp_obj_t mp_obj_int_binary_op_extra_cases(int op, mp_obj_t lhs_in, mp_obj_t rhs_in);

py/objint_longlong.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#define SUFFIX ""
2222
#endif
2323

24-
void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
24+
void mp_obj_int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
2525
if (MP_OBJ_IS_SMALL_INT(self_in)) {
2626
print(env, INT_FMT, MP_OBJ_SMALL_INT_VALUE(self_in));
2727
} else {
@@ -30,7 +30,7 @@ void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj
3030
}
3131
}
3232

33-
mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
33+
mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) {
3434
mp_obj_int_t *o = o_in;
3535
switch (op) {
3636
case MP_UNARY_OP_BOOL: return MP_BOOL(o->val != 0);
@@ -41,7 +41,7 @@ mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
4141
}
4242
}
4343

44-
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
44+
mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
4545
long long lhs_val;
4646
long long rhs_val;
4747

@@ -58,14 +58,8 @@ mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
5858
} else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
5959
rhs_val = ((mp_obj_int_t*)rhs_in)->val;
6060
} else {
61-
if (op == MP_BINARY_OP_MULTIPLY) {
62-
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)) {
63-
// multiply is commutative for these types, so delegate to them
64-
return mp_binary_op(op, rhs_in, lhs_in);
65-
}
66-
}
67-
// unsupported operation/type
68-
return MP_OBJ_NULL;
61+
// delegate to generic function to check for extra cases
62+
return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
6963
}
7064

7165
switch (op) {

py/objint_mpz.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ STATIC mp_obj_int_t *mp_obj_int_new_mpz(void) {
2222
return o;
2323
}
2424

25-
void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
25+
void mp_obj_int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
2626
if (MP_OBJ_IS_SMALL_INT(self_in)) {
2727
print(env, INT_FMT, MP_OBJ_SMALL_INT_VALUE(self_in));
2828
} else {
@@ -34,7 +34,7 @@ void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj
3434
}
3535
}
3636

37-
mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
37+
mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) {
3838
mp_obj_int_t *o = o_in;
3939
switch (op) {
4040
case MP_UNARY_OP_BOOL: return MP_BOOL(!mpz_is_zero(&o->mpz));
@@ -45,7 +45,7 @@ mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
4545
}
4646
}
4747

48-
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
48+
mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
4949
const mpz_t *zlhs;
5050
const mpz_t *zrhs;
5151
mpz_t z_int;
@@ -75,14 +75,8 @@ mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
7575
return mp_obj_complex_binary_op(op, mpz_as_float(zlhs), 0, rhs_in);
7676
#endif
7777
} else {
78-
if (op == MP_BINARY_OP_MULTIPLY) {
79-
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)) {
80-
// multiply is commutative for these types, so delegate to them
81-
return mp_binary_op(op, rhs_in, lhs_in);
82-
}
83-
}
84-
// unsupported operation/type
85-
return MP_OBJ_NULL;
78+
// delegate to generic function to check for extra cases
79+
return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
8680
}
8781

8882
if (0) {

0 commit comments

Comments
 (0)