py: Implement bool unary op; tidy up unary op dispatch.
This commit is contained in:
14
py/objbool.c
14
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};
|
||||
|
||||
Reference in New Issue
Block a user