diff --git a/src/compiler.h b/src/compiler.h index df21438e..0b4892e7 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -814,6 +814,8 @@ __SUBSCR_END: default: { advance(-1); // do revert since we have pre-called advance() at the beginning EXPR_TUPLE(); + // eat variable's type hint + if(match(TK(":"))) consume_type_hints(); if(!try_compile_assignment()){ ctx()->emit_expr(); if((mode()==CELL_MODE || mode()==REPL_MODE) && name_scope()==NAME_GLOBAL){ @@ -827,7 +829,11 @@ __SUBSCR_END: } } - + void consume_type_hints(){ + EXPR(); + ctx()->s_expr.pop(); + } + void compile_class(){ consume(TK("@id")); int namei = StrName(prev().str()).index; @@ -876,7 +882,7 @@ __SUBSCR_END: } // eat type hints - if(enable_type_hints && match(TK(":"))) consume(TK("@id")); + if(enable_type_hints && match(TK(":"))) consume_type_hints(); if(state == 0 && curr().type == TK("=")) state = 2; int index = ctx()->add_varname(name); switch (state) @@ -917,9 +923,7 @@ __SUBSCR_END: _compile_f_args(decl, true); consume(TK(")")); } - if(match(TK("->"))){ - if(!match(TK("None"))) consume(TK("@id")); - } + if(match(TK("->"))) consume_type_hints(); compile_block_body(); pop_context(); diff --git a/tests/22_typehints.py b/tests/22_typehints.py index 4e262317..8b0e72c8 100644 --- a/tests/22_typehints.py +++ b/tests/22_typehints.py @@ -41,4 +41,17 @@ def i(x, y: int, *args): return x + y + len(args) def j(x, y: int, *args: str) -> int: - return x + y + len(args) \ No newline at end of file + return x + y + len(args) + +x: int = 1 +y: 'str' = '2' + +x: 'list[int]' = [1, 2, 3] +y: 'list[str]' = ['1', '2', '3'] + +def g(x: 'list[int]', y: 'list[str]') -> 'list[int]': + return x + y + +def z(x: float): + x: int = 1 + y: 'str' = '2' \ No newline at end of file