py: Implement bool unary op; tidy up unary op dispatch.
This commit is contained in:
parent
b051e7d167
commit
1e708fed18
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};
|
||||
|
@ -447,7 +447,7 @@ bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2) {
|
||||
if (l1 != l2) {
|
||||
return false;
|
||||
}
|
||||
return strncmp((const char*)d1, (const char*)d2, l1) == 0;
|
||||
return memcmp(d1, d2, l1) == 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -106,7 +106,7 @@ qstr qstr_find_strn(const byte *str, uint str_len) {
|
||||
// search pools for the data
|
||||
for (qstr_pool_t *pool = last_pool; pool != NULL; pool = pool->prev) {
|
||||
for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
|
||||
if (Q_GET_HASH(*q) == str_hash && Q_GET_LENGTH(*q) == str_len && strncmp((const char*)Q_GET_DATA(*q), (const char*)str, str_len) == 0) {
|
||||
if (Q_GET_HASH(*q) == str_hash && Q_GET_LENGTH(*q) == str_len && memcmp(Q_GET_DATA(*q), str, str_len) == 0) {
|
||||
return pool->total_prev_len + (q - pool->qstrs);
|
||||
}
|
||||
}
|
||||
|
10
py/runtime.c
10
py/runtime.c
@ -479,16 +479,16 @@ mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
|
||||
return MP_OBJ_NEW_SMALL_INT(val);
|
||||
}
|
||||
return mp_obj_new_int(val);
|
||||
} else { // will be an object (small ints are caught in previous if)
|
||||
mp_obj_base_t *o = arg;
|
||||
if (o->type->unary_op != NULL) {
|
||||
mp_obj_t result = o->type->unary_op(op, arg);
|
||||
} else {
|
||||
mp_obj_type_t *type = mp_obj_get_type(arg);
|
||||
if (type->unary_op != NULL) {
|
||||
mp_obj_t result = type->unary_op(op, arg);
|
||||
if (result != NULL) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
// TODO specify in error message what the operator is
|
||||
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "bad operand type for unary operator: '%s'", o->type->name));
|
||||
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "bad operand type for unary operator: '%s'", type->name));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user