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
a5190a7d
Commit
a5190a7d
authored
10 years ago
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
py: Fix typing of viper locals; allow default types in annotation.
parent
2ac4af69
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/compile.c
+14
-11
14 additions, 11 deletions
py/compile.c
py/emitnative.c
+11
-5
11 additions, 5 deletions
py/emitnative.c
tests/micropython/viper.py
+22
-1
22 additions, 1 deletion
tests/micropython/viper.py
tests/micropython/viper.py.exp
+3
-0
3 additions, 0 deletions
tests/micropython/viper.py.exp
with
50 additions
and
17 deletions
py/compile.c
+
14
−
11
View file @
a5190a7d
...
...
@@ -3017,7 +3017,7 @@ STATIC void compile_scope_func_annotations(compiler_t *comp, mp_parse_node_t pn)
qstr
arg_type
=
MP_PARSE_NODE_LEAF_ARG
(
pn_annotation
);
EMIT_ARG
(
set_native_type
,
MP_EMIT_NATIVE_TYPE_ARG
,
id_info
->
local_num
,
arg_type
);
}
else
{
compile_syntax_error
(
comp
,
pn_annotation
,
"annotation must be an identifier"
);
compile_syntax_error
(
comp
,
pn_annotation
,
"
parameter
annotation must be an identifier"
);
}
}
#endif // MICROPY_EMIT_NATIVE
...
...
@@ -3155,17 +3155,20 @@ STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
apply_to_single_or_list
(
comp
,
pns
->
nodes
[
1
],
PN_typedargslist
,
compile_scope_func_annotations
);
// pns->nodes[2] is return/whole function annotation
#if MICROPY_EMIT_NATIVE
if
(
scope
->
emit_options
==
MP_EMIT_OPT_VIPER
)
{
// nodes[2] can be null or a test-expr
if
(
MP_PARSE_NODE_IS_ID
(
pns
->
nodes
[
2
]))
{
qstr
ret_type
=
MP_PARSE_NODE_LEAF_ARG
(
pns
->
nodes
[
2
]);
EMIT_ARG
(
set_native_type
,
MP_EMIT_NATIVE_TYPE_RETURN
,
0
,
ret_type
);
}
else
{
compile_syntax_error
(
comp
,
pns
->
nodes
[
2
],
"annotation must be an identifier"
);
mp_parse_node_t
pn_annotation
=
pns
->
nodes
[
2
];
if
(
!
MP_PARSE_NODE_IS_NULL
(
pn_annotation
))
{
#if MICROPY_EMIT_NATIVE
if
(
scope
->
emit_options
==
MP_EMIT_OPT_VIPER
)
{
// nodes[2] can be null or a test-expr
if
(
MP_PARSE_NODE_IS_ID
(
pn_annotation
))
{
qstr
ret_type
=
MP_PARSE_NODE_LEAF_ARG
(
pn_annotation
);
EMIT_ARG
(
set_native_type
,
MP_EMIT_NATIVE_TYPE_RETURN
,
0
,
ret_type
);
}
else
{
compile_syntax_error
(
comp
,
pn_annotation
,
"return annotation must be an identifier"
);
}
}
#endif // MICROPY_EMIT_NATIVE
}
#endif // MICROPY_EMIT_NATIVE
}
compile_node
(
comp
,
pns
->
nodes
[
3
]);
// 3 is function body
...
...
@@ -3625,7 +3628,7 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
comp
->
emit_method_table
=
&
emit_native_thumb_method_table
;
#endif
comp
->
emit
=
emit_native
;
comp
->
emit_method_table
->
set_native_type
(
comp
->
emit
,
MP_EMIT_NATIVE_TYPE_ENABLE
,
s
->
emit_options
==
MP_EMIT_OPT_VIPER
,
0
);
EMIT_ARG
(
set_native_type
,
MP_EMIT_NATIVE_TYPE_ENABLE
,
s
->
emit_options
==
MP_EMIT_OPT_VIPER
,
0
);
// native emitters need an extra pass to compute stack size
compile_scope
(
comp
,
s
,
MP_PASS_STACK_SIZE
);
...
...
This diff is collapsed.
Click to expand it.
py/emitnative.c
+
11
−
5
View file @
a5190a7d
...
...
@@ -241,12 +241,19 @@ STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop
// set default type for return and arguments
emit
->
return_vtype
=
VTYPE_PYOBJ
;
for
(
in
t
i
=
0
;
i
<
emit
->
local_vtype_alloc
;
i
++
)
{
for
(
mp_uint_
t
i
=
0
;
i
<
emit
->
scope
->
num_pos_args
;
i
++
)
{
emit
->
local_vtype
[
i
]
=
VTYPE_PYOBJ
;
}
for
(
int
i
=
0
;
i
<
emit
->
stack_info_alloc
;
i
++
)
{
// local variables begin unbound, and have unknown type
for
(
mp_uint_t
i
=
emit
->
scope
->
num_pos_args
;
i
<
emit
->
local_vtype_alloc
;
i
++
)
{
emit
->
local_vtype
[
i
]
=
VTYPE_UNBOUND
;
}
// values on stack begin unbound
for
(
mp_uint_t
i
=
0
;
i
<
emit
->
stack_info_alloc
;
i
++
)
{
emit
->
stack_info
[
i
].
kind
=
STACK_VALUE
;
emit
->
stack_info
[
i
].
vtype
=
VTYPE_
PYOBJ
;
emit
->
stack_info
[
i
].
vtype
=
VTYPE_
UNBOUND
;
}
#if N_X64
...
...
@@ -844,7 +851,6 @@ STATIC void emit_native_load_subscr(emit_t *emit) {
emit_post_push_reg
(
emit
,
VTYPE_PYOBJ
,
REG_RET
);
}
else
{
printf
(
"ViperTypeError: can't do subscr of types %d and %d
\n
"
,
vtype_lhs
,
vtype_rhs
);
assert
(
0
);
}
}
...
...
@@ -1212,7 +1218,7 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
emit_post_push_reg
(
emit
,
VTYPE_PYOBJ
,
REG_RET
);
}
else
{
printf
(
"ViperTypeError: can't do binary op between types %d and %d
\n
"
,
vtype_lhs
,
vtype_rhs
);
assert
(
0
);
emit_post_push_reg
(
emit
,
VTYPE_PYOBJ
,
REG_RET
);
}
}
...
...
This diff is collapsed.
Click to expand it.
tests/micropython/viper.py
+
22
−
1
View file @
a5190a7d
...
...
@@ -10,6 +10,25 @@ def f(x:int, y:int) -> int:
def
g
(
x
:
object
,
y
:
object
)
->
object
:
return
x
+
y
# a local (should have automatic type int)
@micropython.viper
def
h
(
x
:
int
)
->
int
:
y
=
4
return
x
+
y
# without type annotation, types should default to object
@micropython.viper
def
i
(
x
,
y
):
return
x
*
y
# a for loop
@micropython.viper
def
viper_sum
(
a
:
int
,
b
:
int
)
->
int
:
total
=
0
for
x
in
range
(
a
,
b
):
total
+=
x
return
total
# this doesn't work at the moment
#@micropython.viper
#def g() -> uint:
...
...
@@ -17,4 +36,6 @@ def g(x:object, y:object) -> object:
print
(
f
(
1
,
2
))
print
(
g
(
1
,
2
))
#print(h())
print
(
h
(
3
))
print
(
i
(
4
,
5
))
print
(
viper_sum
(
10
,
10000
))
This diff is collapsed.
Click to expand it.
tests/micropython/viper.py.exp
+
3
−
0
View file @
a5190a7d
6
3
7
20
49994955
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