Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
4e8dc8c4
Commit
4e8dc8c4
authored
Jan 27, 2014
by
Damien George
Browse files
py: Add unary op not for NoneType, bool, tuple, list, dict; fix for int.
parent
addf60b2
Changes
6
Hide whitespace changes
Inline
Side-by-side
py/objdict.c
View file @
4e8dc8c4
...
...
@@ -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
,
...
...
py/objlist.c
View file @
4e8dc8c4
...
...
@@ -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
,
...
...
py/objnone.c
View file @
4e8dc8c4
...
...
@@ -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
}};
...
...
py/objtuple.c
View file @
4e8dc8c4
...
...
@@ -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
,
};
...
...
py/runtime.c
View file @
4e8dc8c4
...
...
@@ -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
;
...
...
tests/basics/unary_op.py
0 → 100644
View file @
4e8dc8c4
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
})
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment