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
6e48f7fa
Commit
6e48f7fa
authored
11 years ago
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
py: Allow 'complex()' to take a string as first argument.
parent
c06ea7ab
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/objcomplex.c
+10
-4
10 additions, 4 deletions
py/objcomplex.c
py/objfloat.c
+3
-1
3 additions, 1 deletion
py/objfloat.c
py/parsenum.c
+4
-2
4 additions, 2 deletions
py/parsenum.c
py/parsenum.h
+1
-1
1 addition, 1 deletion
py/parsenum.h
py/runtime.c
+1
-1
1 addition, 1 deletion
py/runtime.c
with
19 additions
and
9 deletions
py/objcomplex.c
+
10
−
4
View file @
6e48f7fa
...
...
@@ -6,6 +6,7 @@
#include
"mpconfig.h"
#include
"qstr.h"
#include
"obj.h"
#include
"parsenum.h"
#include
"runtime0.h"
#include
"map.h"
...
...
@@ -36,15 +37,20 @@ STATIC mp_obj_t complex_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const
return
mp_obj_new_complex
(
0
,
0
);
case
1
:
// TODO allow string as first arg and parse it
if
(
MP_OBJ_IS_TYPE
(
args
[
0
],
&
mp_type_complex
))
{
if
(
MP_OBJ_IS_STR
(
args
[
0
]))
{
// a string, parse it
uint
l
;
const
char
*
s
=
mp_obj_str_get_data
(
args
[
0
],
&
l
);
return
mp_parse_num_decimal
(
s
,
l
,
true
,
true
);
}
else
if
(
MP_OBJ_IS_TYPE
(
args
[
0
],
&
mp_type_complex
))
{
// a complex, just return it
return
args
[
0
];
}
else
{
// something else, try to cast it to a complex
return
mp_obj_new_complex
(
mp_obj_get_float
(
args
[
0
]),
0
);
}
case
2
:
{
case
2
:
{
mp_float_t
real
,
imag
;
if
(
MP_OBJ_IS_TYPE
(
args
[
0
],
&
mp_type_complex
))
{
mp_obj_complex_get
(
args
[
0
],
&
real
,
&
imag
);
...
...
This diff is collapsed.
Click to expand it.
py/objfloat.c
+
3
−
1
View file @
6e48f7fa
...
...
@@ -38,10 +38,12 @@ STATIC mp_obj_t float_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m
// a string, parse it
uint
l
;
const
char
*
s
=
mp_obj_str_get_data
(
args
[
0
],
&
l
);
return
mp_parse_num_decimal
(
s
,
l
,
false
);
return
mp_parse_num_decimal
(
s
,
l
,
false
,
false
);
}
else
if
(
MP_OBJ_IS_TYPE
(
args
[
0
],
&
mp_type_float
))
{
// a float, just return it
return
args
[
0
];
}
else
{
// something else, try to cast it to a float
return
mp_obj_new_float
(
mp_obj_get_float
(
args
[
0
]));
}
...
...
This diff is collapsed.
Click to expand it.
py/parsenum.c
+
4
−
2
View file @
6e48f7fa
...
...
@@ -88,7 +88,7 @@ mp_obj_t mp_parse_num_integer(const char *restrict str, uint len, int base) {
#define PARSE_DEC_IN_FRAC (2)
#define PARSE_DEC_IN_EXP (3)
mp_obj_t
mp_parse_num_decimal
(
const
char
*
str
,
uint
len
,
bool
allow_imag
)
{
mp_obj_t
mp_parse_num_decimal
(
const
char
*
str
,
uint
len
,
bool
allow_imag
,
bool
force_complex
)
{
#if MICROPY_ENABLE_FLOAT
const
char
*
top
=
str
+
len
;
mp_float_t
dec_val
=
0
;
...
...
@@ -129,7 +129,7 @@ mp_obj_t mp_parse_num_decimal(const char *str, uint len, bool allow_imag) {
dec_val
=
MICROPY_FLOAT_C_FUN
(
nan
)(
""
);
}
}
else
{
//
parse the digits
//
string should be a decimal number
int
in
=
PARSE_DEC_IN_INTG
;
bool
exp_neg
=
false
;
int
exp_val
=
0
;
...
...
@@ -198,6 +198,8 @@ mp_obj_t mp_parse_num_decimal(const char *str, uint len, bool allow_imag) {
// return the object
if
(
imag
)
{
return
mp_obj_new_complex
(
0
,
dec_val
);
}
else
if
(
force_complex
)
{
return
mp_obj_new_complex
(
dec_val
,
0
);
}
else
{
return
mp_obj_new_float
(
dec_val
);
}
...
...
This diff is collapsed.
Click to expand it.
py/parsenum.h
+
1
−
1
View file @
6e48f7fa
mp_obj_t
mp_parse_num_integer
(
const
char
*
restrict
str
,
uint
len
,
int
base
);
mp_obj_t
mp_parse_num_decimal
(
const
char
*
str
,
uint
len
,
bool
allow_imag
);
mp_obj_t
mp_parse_num_decimal
(
const
char
*
str
,
uint
len
,
bool
allow_imag
,
bool
force_complex
);
This diff is collapsed.
Click to expand it.
py/runtime.c
+
1
−
1
View file @
6e48f7fa
...
...
@@ -375,7 +375,7 @@ mp_obj_t rt_load_const_dec(qstr qstr) {
DEBUG_OP_printf
(
"load '%s'
\n
"
,
qstr_str
(
qstr
));
uint
len
;
const
byte
*
data
=
qstr_data
(
qstr
,
&
len
);
return
mp_parse_num_decimal
((
const
char
*
)
data
,
len
,
true
);
return
mp_parse_num_decimal
((
const
char
*
)
data
,
len
,
true
,
false
);
}
mp_obj_t
rt_load_const_str
(
qstr
qstr
)
{
...
...
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