aboutsummaryrefslogtreecommitdiff
path: root/py/compile.c
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2020-06-16 22:49:25 +1000
committerDamien George <damien@micropython.org>2020-06-16 23:18:01 +1000
commitf2e267da68bf85f3f12fa77155e469352bdc6c09 (patch)
tree1387eb6b017d621fe04b75f5ab576bd3b7268d23 /py/compile.c
parent131b0de70a47861c266d2ac7c26ae643f679d2f7 (diff)
py/compile: Implement PEP 526, syntax for variable annotations.
This addition to the grammar was introduced in Python 3.6. It allows annotating the type of a varilable, like: x: int = 123 s: str The implementation in this commit is quite simple and just ignores the annotation (the int and str bits above). The reason to implement this is to allow Python 3.6+ code that uses this feature to compile under MicroPython without change, and for users to use type checkers. In the future viper could use this syntax as a way to give types to variables, which is currently done in a bit of an ad-hoc way, eg x = int(123). And this syntax could potentially be used in the inline assembler to define labels in an way that's easier to read.
Diffstat (limited to 'py/compile.c')
-rw-r--r--py/compile.c31
1 files changed, 24 insertions, 7 deletions
diff --git a/py/compile.c b/py/compile.c
index 614e851a3..53108b706 100644
--- a/py/compile.c
+++ b/py/compile.c
@@ -1981,7 +1981,8 @@ STATIC void compile_async_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
#endif
STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
- if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
+ mp_parse_node_t pn_rhs = pns->nodes[1];
+ if (MP_PARSE_NODE_IS_NULL(pn_rhs)) {
if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
// for REPL, evaluate then print the expression
compile_load_id(comp, MP_QSTR___repl_print__);
@@ -1999,10 +2000,26 @@ STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
}
}
- } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
- mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t *)pns->nodes[1];
+ } else if (MP_PARSE_NODE_IS_STRUCT(pn_rhs)) {
+ mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t *)pn_rhs;
int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
- if (kind == PN_expr_stmt_augassign) {
+ if (kind == PN_annassign) {
+ // the annotation is in pns1->nodes[0] and is ignored
+ if (MP_PARSE_NODE_IS_NULL(pns1->nodes[1])) {
+ // an annotation of the form "x: y"
+ // inside a function this declares "x" as a local
+ if (comp->scope_cur->kind == SCOPE_FUNCTION) {
+ if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
+ qstr lhs = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
+ scope_find_or_add_id(comp->scope_cur, lhs, ID_INFO_KIND_LOCAL);
+ }
+ }
+ } else {
+ // an assigned annotation of the form "x: y = z"
+ pn_rhs = pns1->nodes[1];
+ goto plain_assign;
+ }
+ } else if (kind == PN_expr_stmt_augassign) {
c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
compile_node(comp, pns1->nodes[1]); // rhs
assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
@@ -2027,10 +2044,10 @@ STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
} else {
plain_assign:
#if MICROPY_COMP_DOUBLE_TUPLE_ASSIGN
- if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_testlist_star_expr)
+ if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_rhs, PN_testlist_star_expr)
&& MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)) {
mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t *)pns->nodes[0];
- pns1 = (mp_parse_node_struct_t *)pns->nodes[1];
+ pns1 = (mp_parse_node_struct_t *)pn_rhs;
uint32_t n_pns0 = MP_PARSE_NODE_STRUCT_NUM_NODES(pns0);
// Can only optimise a tuple-to-tuple assignment when all of the following hold:
// - equal number of items in LHS and RHS tuples
@@ -2070,7 +2087,7 @@ STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
}
#endif
- compile_node(comp, pns->nodes[1]); // rhs
+ compile_node(comp, pn_rhs); // rhs
c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
}
} else {