diff options
Diffstat (limited to 'py/objbool.c')
-rw-r--r-- | py/objbool.c | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/py/objbool.c b/py/objbool.c index 729ffb4e6d..53b2bf8ed7 100644 --- a/py/objbool.c +++ b/py/objbool.c @@ -6,6 +6,7 @@ #include "mpconfig.h" #include "qstr.h" #include "obj.h" +#include "runtime0.h" #include "runtime.h" typedef struct _mp_obj_bool_t { @@ -32,11 +33,24 @@ static mp_obj_t bool_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp } } +static mp_obj_t bool_unary_op(int op, mp_obj_t o_in) { + machine_int_t value = ((mp_obj_bool_t*)o_in)->value; + switch (op) { + case RT_UNARY_OP_NOT: if (value) { return mp_const_false; } else { return mp_const_true; } + case RT_UNARY_OP_POSITIVE: return MP_OBJ_NEW_SMALL_INT(value); + case RT_UNARY_OP_NEGATIVE: return MP_OBJ_NEW_SMALL_INT(-value); + case RT_UNARY_OP_INVERT: + default: // no other cases + return MP_OBJ_NEW_SMALL_INT(~value); + } +} + const mp_obj_type_t bool_type = { { &mp_const_type }, "bool", .print = bool_print, .make_new = bool_make_new, + .unary_op = bool_unary_op, }; static const mp_obj_bool_t false_obj = {{&bool_type}, false}; |