py: Implement bool unary op; tidy up unary op dispatch.

This commit is contained in:
Damien George
2014-01-23 18:27:51 +00:00
parent b051e7d167
commit 1e708fed18
4 changed files with 21 additions and 7 deletions

View File

@@ -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};