Skip to content

Commit 1e708fe

Browse files
committed
py: Implement bool unary op; tidy up unary op dispatch.
1 parent b051e7d commit 1e708fe

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

py/objbool.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "mpconfig.h"
77
#include "qstr.h"
88
#include "obj.h"
9+
#include "runtime0.h"
910
#include "runtime.h"
1011

1112
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
3233
}
3334
}
3435

36+
static mp_obj_t bool_unary_op(int op, mp_obj_t o_in) {
37+
machine_int_t value = ((mp_obj_bool_t*)o_in)->value;
38+
switch (op) {
39+
case RT_UNARY_OP_NOT: if (value) { return mp_const_false; } else { return mp_const_true; }
40+
case RT_UNARY_OP_POSITIVE: return MP_OBJ_NEW_SMALL_INT(value);
41+
case RT_UNARY_OP_NEGATIVE: return MP_OBJ_NEW_SMALL_INT(-value);
42+
case RT_UNARY_OP_INVERT:
43+
default: // no other cases
44+
return MP_OBJ_NEW_SMALL_INT(~value);
45+
}
46+
}
47+
3548
const mp_obj_type_t bool_type = {
3649
{ &mp_const_type },
3750
"bool",
3851
.print = bool_print,
3952
.make_new = bool_make_new,
53+
.unary_op = bool_unary_op,
4054
};
4155

4256
static const mp_obj_bool_t false_obj = {{&bool_type}, false};

py/objstr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2) {
447447
if (l1 != l2) {
448448
return false;
449449
}
450-
return strncmp((const char*)d1, (const char*)d2, l1) == 0;
450+
return memcmp(d1, d2, l1) == 0;
451451
}
452452
}
453453

py/qstr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ qstr qstr_find_strn(const byte *str, uint str_len) {
106106
// search pools for the data
107107
for (qstr_pool_t *pool = last_pool; pool != NULL; pool = pool->prev) {
108108
for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
109-
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) {
109+
if (Q_GET_HASH(*q) == str_hash && Q_GET_LENGTH(*q) == str_len && memcmp(Q_GET_DATA(*q), str, str_len) == 0) {
110110
return pool->total_prev_len + (q - pool->qstrs);
111111
}
112112
}

py/runtime.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -479,16 +479,16 @@ mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
479479
return MP_OBJ_NEW_SMALL_INT(val);
480480
}
481481
return mp_obj_new_int(val);
482-
} else { // will be an object (small ints are caught in previous if)
483-
mp_obj_base_t *o = arg;
484-
if (o->type->unary_op != NULL) {
485-
mp_obj_t result = o->type->unary_op(op, arg);
482+
} else {
483+
mp_obj_type_t *type = mp_obj_get_type(arg);
484+
if (type->unary_op != NULL) {
485+
mp_obj_t result = type->unary_op(op, arg);
486486
if (result != NULL) {
487487
return result;
488488
}
489489
}
490490
// TODO specify in error message what the operator is
491-
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "bad operand type for unary operator: '%s'", o->type->name));
491+
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "bad operand type for unary operator: '%s'", type->name));
492492
}
493493
}
494494

0 commit comments

Comments
 (0)