py: Add unary op not for NoneType, bool, tuple, list, dict; fix for int.
This commit is contained in:
parent
addf60b2e6
commit
4e8dc8c41b
@ -43,6 +43,14 @@ static mp_obj_t dict_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp
|
||||
return rt_build_map(0);
|
||||
}
|
||||
|
||||
static mp_obj_t dict_unary_op(int op, mp_obj_t self_in) {
|
||||
mp_obj_dict_t *self = self_in;
|
||||
switch (op) {
|
||||
case RT_UNARY_OP_NOT: if (self->map.used == 0) { return mp_const_true; } else { return mp_const_false; }
|
||||
default: return MP_OBJ_NULL; // op not supported for None
|
||||
}
|
||||
}
|
||||
|
||||
static mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
||||
mp_obj_dict_t *o = lhs_in;
|
||||
switch (op) {
|
||||
@ -436,6 +444,7 @@ const mp_obj_type_t dict_type = {
|
||||
"dict",
|
||||
.print = dict_print,
|
||||
.make_new = dict_make_new,
|
||||
.unary_op = dict_unary_op,
|
||||
.binary_op = dict_binary_op,
|
||||
.getiter = dict_getiter,
|
||||
.methods = dict_type_methods,
|
||||
|
@ -119,6 +119,14 @@ static bool list_cmp_helper(int op, mp_obj_t self_in, mp_obj_t another_in) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static mp_obj_t list_unary_op(int op, mp_obj_t self_in) {
|
||||
mp_obj_list_t *self = self_in;
|
||||
switch (op) {
|
||||
case RT_UNARY_OP_NOT: if (self->len == 0) { return mp_const_true; } else { return mp_const_false; }
|
||||
default: return MP_OBJ_NULL; // op not supported for None
|
||||
}
|
||||
}
|
||||
|
||||
static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
|
||||
mp_obj_list_t *o = lhs;
|
||||
switch (op) {
|
||||
@ -395,6 +403,7 @@ const mp_obj_type_t list_type = {
|
||||
"list",
|
||||
.print = list_print,
|
||||
.make_new = list_make_new,
|
||||
.unary_op = list_unary_op,
|
||||
.binary_op = list_binary_op,
|
||||
.getiter = list_getiter,
|
||||
.methods = list_type_methods,
|
||||
|
11
py/objnone.c
11
py/objnone.c
@ -6,19 +6,28 @@
|
||||
#include "mpconfig.h"
|
||||
#include "qstr.h"
|
||||
#include "obj.h"
|
||||
#include "runtime0.h"
|
||||
|
||||
typedef struct _mp_obj_none_t {
|
||||
mp_obj_base_t base;
|
||||
} mp_obj_none_t;
|
||||
|
||||
void none_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
static void none_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
print(env, "None");
|
||||
}
|
||||
|
||||
static mp_obj_t none_unary_op(int op, mp_obj_t o_in) {
|
||||
switch (op) {
|
||||
case RT_UNARY_OP_NOT: return mp_const_true;
|
||||
default: return MP_OBJ_NULL; // op not supported for None
|
||||
}
|
||||
}
|
||||
|
||||
const mp_obj_type_t none_type = {
|
||||
{ &mp_const_type },
|
||||
"NoneType",
|
||||
.print = none_print,
|
||||
.unary_op = none_unary_op,
|
||||
};
|
||||
|
||||
static const mp_obj_none_t none_obj = {{&none_type}};
|
||||
|
@ -73,6 +73,14 @@ static mp_obj_t tuple_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m
|
||||
}
|
||||
}
|
||||
|
||||
static mp_obj_t tuple_unary_op(int op, mp_obj_t self_in) {
|
||||
mp_obj_tuple_t *self = self_in;
|
||||
switch (op) {
|
||||
case RT_UNARY_OP_NOT: if (self->len == 0) { return mp_const_true; } else { return mp_const_false; }
|
||||
default: return MP_OBJ_NULL; // op not supported for None
|
||||
}
|
||||
}
|
||||
|
||||
static mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
|
||||
mp_obj_tuple_t *o = lhs;
|
||||
switch (op) {
|
||||
@ -97,6 +105,7 @@ const mp_obj_type_t tuple_type = {
|
||||
"tuple",
|
||||
.print = tuple_print,
|
||||
.make_new = tuple_make_new,
|
||||
.unary_op = tuple_unary_op,
|
||||
.binary_op = tuple_binary_op,
|
||||
.getiter = tuple_getiter,
|
||||
};
|
||||
|
@ -481,7 +481,7 @@ mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
|
||||
if (MP_OBJ_IS_SMALL_INT(arg)) {
|
||||
mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
|
||||
switch (op) {
|
||||
case RT_UNARY_OP_NOT: if (val != 0) { return mp_const_true;} else { return mp_const_false; }
|
||||
case RT_UNARY_OP_NOT: if (val == 0) { return mp_const_true;} else { return mp_const_false; }
|
||||
case RT_UNARY_OP_POSITIVE: break;
|
||||
case RT_UNARY_OP_NEGATIVE: val = -val; break;
|
||||
case RT_UNARY_OP_INVERT: val = ~val; break;
|
||||
|
12
tests/basics/unary_op.py
Normal file
12
tests/basics/unary_op.py
Normal file
@ -0,0 +1,12 @@
|
||||
print(not None)
|
||||
print(not False)
|
||||
print(not True)
|
||||
print(not 0)
|
||||
print(not 1)
|
||||
print(not -1)
|
||||
print(not ())
|
||||
print(not (1,))
|
||||
print(not [])
|
||||
print(not [1,])
|
||||
print(not {})
|
||||
print(not {1:1})
|
Loading…
Reference in New Issue
Block a user