add full typehints support

This commit is contained in:
blueloveTH 2023-05-27 21:47:52 +08:00
parent db283d4ba6
commit 492c3fda00
2 changed files with 23 additions and 6 deletions

View File

@ -814,6 +814,8 @@ __SUBSCR_END:
default: { default: {
advance(-1); // do revert since we have pre-called advance() at the beginning advance(-1); // do revert since we have pre-called advance() at the beginning
EXPR_TUPLE(); EXPR_TUPLE();
// eat variable's type hint
if(match(TK(":"))) consume_type_hints();
if(!try_compile_assignment()){ if(!try_compile_assignment()){
ctx()->emit_expr(); ctx()->emit_expr();
if((mode()==CELL_MODE || mode()==REPL_MODE) && name_scope()==NAME_GLOBAL){ 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(){ void compile_class(){
consume(TK("@id")); consume(TK("@id"));
int namei = StrName(prev().str()).index; int namei = StrName(prev().str()).index;
@ -876,7 +882,7 @@ __SUBSCR_END:
} }
// eat type hints // 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; if(state == 0 && curr().type == TK("=")) state = 2;
int index = ctx()->add_varname(name); int index = ctx()->add_varname(name);
switch (state) switch (state)
@ -917,9 +923,7 @@ __SUBSCR_END:
_compile_f_args(decl, true); _compile_f_args(decl, true);
consume(TK(")")); consume(TK(")"));
} }
if(match(TK("->"))){ if(match(TK("->"))) consume_type_hints();
if(!match(TK("None"))) consume(TK("@id"));
}
compile_block_body(); compile_block_body();
pop_context(); pop_context();

View File

@ -41,4 +41,17 @@ def i(x, y: int, *args):
return x + y + len(args) return x + y + len(args)
def j(x, y: int, *args: str) -> int: 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'