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
e8208a7f
Commit
e8208a7f
authored
10 years ago
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
py: Make False and True act like 0 and 1 for integer arithmetic.
parent
d7aadcfe
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
py/objbool.c
+10
-0
10 additions, 0 deletions
py/objbool.c
py/objint.c
+28
-17
28 additions, 17 deletions
py/objint.c
py/objint.h
+4
-3
4 additions, 3 deletions
py/objint.h
py/objint_longlong.c
+5
-11
5 additions, 11 deletions
py/objint_longlong.c
py/objint_mpz.c
+5
-11
5 additions, 11 deletions
py/objint_mpz.c
with
52 additions
and
42 deletions
py/objbool.c
+
10
−
0
View file @
e8208a7f
#include
<stdlib.h>
#include
"nlr.h"
#include
"misc.h"
...
...
@@ -43,12 +44,21 @@ STATIC mp_obj_t bool_unary_op(int op, mp_obj_t o_in) {
}
}
STATIC
mp_obj_t
bool_binary_op
(
int
op
,
mp_obj_t
lhs_in
,
mp_obj_t
rhs_in
)
{
if
(
MP_BINARY_OP_OR
<=
op
&&
op
<=
MP_BINARY_OP_NOT_EQUAL
)
{
return
mp_binary_op
(
op
,
MP_OBJ_NEW_SMALL_INT
((
machine_int_t
)
mp_obj_is_true
(
lhs_in
)),
rhs_in
);
}
// operation not supported
return
MP_OBJ_NULL
;
}
const
mp_obj_type_t
mp_type_bool
=
{
{
&
mp_type_type
},
.
name
=
MP_QSTR_bool
,
.
print
=
bool_print
,
.
make_new
=
bool_make_new
,
.
unary_op
=
bool_unary_op
,
.
binary_op
=
bool_binary_op
,
};
const
mp_obj_bool_t
mp_const_false_obj
=
{{
&
mp_type_bool
},
false
};
...
...
This diff is collapsed.
Click to expand it.
py/objint.c
+
28
−
17
View file @
e8208a7f
...
...
@@ -17,9 +17,8 @@
#include
<math.h>
#endif
// This dispatcher function is expected to be independent of the implementation
// of long int
STATIC
mp_obj_t
int_make_new
(
mp_obj_t
type_in
,
uint
n_args
,
uint
n_kw
,
const
mp_obj_t
*
args
)
{
// This dispatcher function is expected to be independent of the implementation of long int
STATIC
mp_obj_t
mp_obj_int_make_new
(
mp_obj_t
type_in
,
uint
n_args
,
uint
n_kw
,
const
mp_obj_t
*
args
)
{
// TODO check n_kw == 0
switch
(
n_args
)
{
...
...
@@ -56,26 +55,20 @@ STATIC mp_obj_t int_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
void
int_print
(
void
(
*
print
)(
void
*
env
,
const
char
*
fmt
,
...),
void
*
env
,
mp_obj_t
self_in
,
mp_print_kind_t
kind
)
{
void
mp_obj_
int_print
(
void
(
*
print
)(
void
*
env
,
const
char
*
fmt
,
...),
void
*
env
,
mp_obj_t
self_in
,
mp_print_kind_t
kind
)
{
if
(
MP_OBJ_IS_SMALL_INT
(
self_in
))
{
print
(
env
,
INT_FMT
,
MP_OBJ_SMALL_INT_VALUE
(
self_in
));
}
}
// This is called for operations on SMALL_INT that are not handled by mp_unary_op
mp_obj_t
int_unary_op
(
int
op
,
mp_obj_t
o_in
)
{
mp_obj_t
mp_obj_
int_unary_op
(
int
op
,
mp_obj_t
o_in
)
{
return
MP_OBJ_NULL
;
}
// This is called for operations on SMALL_INT that are not handled by mp_binary_op
mp_obj_t
int_binary_op
(
int
op
,
mp_obj_t
lhs_in
,
mp_obj_t
rhs_in
)
{
if
(
op
==
MP_BINARY_OP_MULTIPLY
)
{
if
(
MP_OBJ_IS_STR
(
rhs_in
)
||
MP_OBJ_IS_TYPE
(
rhs_in
,
&
mp_type_tuple
)
||
MP_OBJ_IS_TYPE
(
rhs_in
,
&
mp_type_list
))
{
// multiply is commutative for these types, so delegate to them
return
mp_binary_op
(
op
,
rhs_in
,
lhs_in
);
}
}
return
MP_OBJ_NULL
;
mp_obj_t
mp_obj_int_binary_op
(
int
op
,
mp_obj_t
lhs_in
,
mp_obj_t
rhs_in
)
{
return
mp_obj_int_binary_op_extra_cases
(
op
,
lhs_in
,
rhs_in
);
}
// This is called only with strings whose value doesn't fit in SMALL_INT
...
...
@@ -124,11 +117,29 @@ mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
#endif // MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
// This dispatcher function is expected to be independent of the implementation of long int
// It handles the extra cases for integer-like arithmetic
mp_obj_t
mp_obj_int_binary_op_extra_cases
(
int
op
,
mp_obj_t
lhs_in
,
mp_obj_t
rhs_in
)
{
if
(
rhs_in
==
mp_const_false
)
{
// false acts as 0
return
mp_binary_op
(
op
,
lhs_in
,
MP_OBJ_NEW_SMALL_INT
(
0
));
}
else
if
(
rhs_in
==
mp_const_true
)
{
// true acts as 0
return
mp_binary_op
(
op
,
lhs_in
,
MP_OBJ_NEW_SMALL_INT
(
1
));
}
else
if
(
op
==
MP_BINARY_OP_MULTIPLY
)
{
if
(
MP_OBJ_IS_STR
(
rhs_in
)
||
MP_OBJ_IS_TYPE
(
rhs_in
,
&
mp_type_tuple
)
||
MP_OBJ_IS_TYPE
(
rhs_in
,
&
mp_type_list
))
{
// multiply is commutative for these types, so delegate to them
return
mp_binary_op
(
op
,
rhs_in
,
lhs_in
);
}
}
return
MP_OBJ_NULL
;
}
const
mp_obj_type_t
mp_type_int
=
{
{
&
mp_type_type
},
.
name
=
MP_QSTR_int
,
.
print
=
int_print
,
.
make_new
=
int_make_new
,
.
unary_op
=
int_unary_op
,
.
binary_op
=
int_binary_op
,
.
print
=
mp_obj_
int_print
,
.
make_new
=
mp_obj_
int_make_new
,
.
unary_op
=
mp_obj_
int_unary_op
,
.
binary_op
=
mp_obj_
int_binary_op
,
};
This diff is collapsed.
Click to expand it.
py/objint.h
+
4
−
3
View file @
e8208a7f
...
...
@@ -7,6 +7,7 @@ typedef struct _mp_obj_int_t {
#endif
}
mp_obj_int_t
;
void
int_print
(
void
(
*
print
)(
void
*
env
,
const
char
*
fmt
,
...),
void
*
env
,
mp_obj_t
self_in
,
mp_print_kind_t
kind
);
mp_obj_t
int_unary_op
(
int
op
,
mp_obj_t
o_in
);
mp_obj_t
int_binary_op
(
int
op
,
mp_obj_t
lhs_in
,
mp_obj_t
rhs_in
);
void
mp_obj_int_print
(
void
(
*
print
)(
void
*
env
,
const
char
*
fmt
,
...),
void
*
env
,
mp_obj_t
self_in
,
mp_print_kind_t
kind
);
mp_obj_t
mp_obj_int_unary_op
(
int
op
,
mp_obj_t
o_in
);
mp_obj_t
mp_obj_int_binary_op
(
int
op
,
mp_obj_t
lhs_in
,
mp_obj_t
rhs_in
);
mp_obj_t
mp_obj_int_binary_op_extra_cases
(
int
op
,
mp_obj_t
lhs_in
,
mp_obj_t
rhs_in
);
This diff is collapsed.
Click to expand it.
py/objint_longlong.c
+
5
−
11
View file @
e8208a7f
...
...
@@ -21,7 +21,7 @@
#define SUFFIX ""
#endif
void
int_print
(
void
(
*
print
)(
void
*
env
,
const
char
*
fmt
,
...),
void
*
env
,
mp_obj_t
self_in
,
mp_print_kind_t
kind
)
{
void
mp_obj_
int_print
(
void
(
*
print
)(
void
*
env
,
const
char
*
fmt
,
...),
void
*
env
,
mp_obj_t
self_in
,
mp_print_kind_t
kind
)
{
if
(
MP_OBJ_IS_SMALL_INT
(
self_in
))
{
print
(
env
,
INT_FMT
,
MP_OBJ_SMALL_INT_VALUE
(
self_in
));
}
else
{
...
...
@@ -30,7 +30,7 @@ void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj
}
}
mp_obj_t
int_unary_op
(
int
op
,
mp_obj_t
o_in
)
{
mp_obj_t
mp_obj_
int_unary_op
(
int
op
,
mp_obj_t
o_in
)
{
mp_obj_int_t
*
o
=
o_in
;
switch
(
op
)
{
case
MP_UNARY_OP_BOOL
:
return
MP_BOOL
(
o
->
val
!=
0
);
...
...
@@ -41,7 +41,7 @@ mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
}
}
mp_obj_t
int_binary_op
(
int
op
,
mp_obj_t
lhs_in
,
mp_obj_t
rhs_in
)
{
mp_obj_t
mp_obj_
int_binary_op
(
int
op
,
mp_obj_t
lhs_in
,
mp_obj_t
rhs_in
)
{
long
long
lhs_val
;
long
long
rhs_val
;
...
...
@@ -58,14 +58,8 @@ mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
}
else
if
(
MP_OBJ_IS_TYPE
(
rhs_in
,
&
mp_type_int
))
{
rhs_val
=
((
mp_obj_int_t
*
)
rhs_in
)
->
val
;
}
else
{
if
(
op
==
MP_BINARY_OP_MULTIPLY
)
{
if
(
MP_OBJ_IS_STR
(
rhs_in
)
||
MP_OBJ_IS_TYPE
(
rhs_in
,
&
mp_type_tuple
)
||
MP_OBJ_IS_TYPE
(
rhs_in
,
&
mp_type_list
))
{
// multiply is commutative for these types, so delegate to them
return
mp_binary_op
(
op
,
rhs_in
,
lhs_in
);
}
}
// unsupported operation/type
return
MP_OBJ_NULL
;
// delegate to generic function to check for extra cases
return
mp_obj_int_binary_op_extra_cases
(
op
,
lhs_in
,
rhs_in
);
}
switch
(
op
)
{
...
...
This diff is collapsed.
Click to expand it.
py/objint_mpz.c
+
5
−
11
View file @
e8208a7f
...
...
@@ -22,7 +22,7 @@ STATIC mp_obj_int_t *mp_obj_int_new_mpz(void) {
return
o
;
}
void
int_print
(
void
(
*
print
)(
void
*
env
,
const
char
*
fmt
,
...),
void
*
env
,
mp_obj_t
self_in
,
mp_print_kind_t
kind
)
{
void
mp_obj_
int_print
(
void
(
*
print
)(
void
*
env
,
const
char
*
fmt
,
...),
void
*
env
,
mp_obj_t
self_in
,
mp_print_kind_t
kind
)
{
if
(
MP_OBJ_IS_SMALL_INT
(
self_in
))
{
print
(
env
,
INT_FMT
,
MP_OBJ_SMALL_INT_VALUE
(
self_in
));
}
else
{
...
...
@@ -34,7 +34,7 @@ void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj
}
}
mp_obj_t
int_unary_op
(
int
op
,
mp_obj_t
o_in
)
{
mp_obj_t
mp_obj_
int_unary_op
(
int
op
,
mp_obj_t
o_in
)
{
mp_obj_int_t
*
o
=
o_in
;
switch
(
op
)
{
case
MP_UNARY_OP_BOOL
:
return
MP_BOOL
(
!
mpz_is_zero
(
&
o
->
mpz
));
...
...
@@ -45,7 +45,7 @@ mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
}
}
mp_obj_t
int_binary_op
(
int
op
,
mp_obj_t
lhs_in
,
mp_obj_t
rhs_in
)
{
mp_obj_t
mp_obj_
int_binary_op
(
int
op
,
mp_obj_t
lhs_in
,
mp_obj_t
rhs_in
)
{
const
mpz_t
*
zlhs
;
const
mpz_t
*
zrhs
;
mpz_t
z_int
;
...
...
@@ -75,14 +75,8 @@ mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
return
mp_obj_complex_binary_op
(
op
,
mpz_as_float
(
zlhs
),
0
,
rhs_in
);
#endif
}
else
{
if
(
op
==
MP_BINARY_OP_MULTIPLY
)
{
if
(
MP_OBJ_IS_STR
(
rhs_in
)
||
MP_OBJ_IS_TYPE
(
rhs_in
,
&
mp_type_tuple
)
||
MP_OBJ_IS_TYPE
(
rhs_in
,
&
mp_type_list
))
{
// multiply is commutative for these types, so delegate to them
return
mp_binary_op
(
op
,
rhs_in
,
lhs_in
);
}
}
// unsupported operation/type
return
MP_OBJ_NULL
;
// delegate to generic function to check for extra cases
return
mp_obj_int_binary_op_extra_cases
(
op
,
lhs_in
,
rhs_in
);
}
if
(
0
)
{
...
...
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