Selaa lähdekoodia

add full typehints support

blueloveTH 2 vuotta sitten
vanhempi
commit
492c3fda00
2 muutettua tiedostoa jossa 23 lisäystä ja 6 poistoa
  1. 9 5
      src/compiler.h
  2. 14 1
      tests/22_typehints.py

+ 9 - 5
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();
 

+ 14 - 1
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)
+    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'