Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
1e708fed
Commit
1e708fed
authored
11 years ago
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
py: Implement bool unary op; tidy up unary op dispatch.
parent
b051e7d1
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
py/objbool.c
+14
-0
14 additions, 0 deletions
py/objbool.c
py/objstr.c
+1
-1
1 addition, 1 deletion
py/objstr.c
py/qstr.c
+1
-1
1 addition, 1 deletion
py/qstr.c
py/runtime.c
+5
-5
5 additions, 5 deletions
py/runtime.c
with
21 additions
and
7 deletions
py/objbool.c
+
14
−
0
View file @
1e708fed
...
...
@@ -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
};
...
...
This diff is collapsed.
Click to expand it.
py/objstr.c
+
1
−
1
View file @
1e708fed
...
...
@@ -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
;
}
}
...
...
This diff is collapsed.
Click to expand it.
py/qstr.c
+
1
−
1
View file @
1e708fed
...
...
@@ -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
);
}
}
...
...
This diff is collapsed.
Click to expand it.
py/runtime.c
+
5
−
5
View file @
1e708fed
...
...
@@ -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_
bas
e_t
*
o
=
arg
;
if
(
o
->
type
->
unary_op
!=
NULL
)
{
mp_obj_t
result
=
o
->
type
->
unary_op
(
op
,
arg
);
}
else
{
mp_obj_
typ
e_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
));
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment