diff options
author | Damien George <damien.p.george@gmail.com> | 2014-04-04 15:08:23 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-04-04 15:08:23 +0100 |
commit | e8208a7f0246e584b09455e82cc7f27cf7f0fe95 (patch) | |
tree | c1dd95f882e9f187172a4d2a3cfc3ee07020664e /py/objbool.c | |
parent | d7aadcfe1b35a54ea873c3b052abbeeb268a7202 (diff) |
py: Make False and True act like 0 and 1 for integer arithmetic.
Diffstat (limited to 'py/objbool.c')
-rw-r--r-- | py/objbool.c | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/py/objbool.c b/py/objbool.c index 6afb6e950..84aa5a781 100644 --- a/py/objbool.c +++ b/py/objbool.c @@ -1,3 +1,4 @@ +#include <stdlib.h> #include "nlr.h" #include "misc.h" @@ -43,12 +44,21 @@ STATIC mp_obj_t bool_unary_op(int op, mp_obj_t o_in) { } } +STATIC mp_obj_t bool_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { + if (MP_BINARY_OP_OR <= op && op <= MP_BINARY_OP_NOT_EQUAL) { + return mp_binary_op(op, MP_OBJ_NEW_SMALL_INT((machine_int_t)mp_obj_is_true(lhs_in)), rhs_in); + } + // operation not supported + return MP_OBJ_NULL; +} + const mp_obj_type_t mp_type_bool = { { &mp_type_type }, .name = MP_QSTR_bool, .print = bool_print, .make_new = bool_make_new, .unary_op = bool_unary_op, + .binary_op = bool_binary_op, }; const mp_obj_bool_t mp_const_false_obj = {{&mp_type_bool}, false}; |