diff --git a/parser/src/parser/parser.rs b/parser/src/parser/parser.rs index 9788592c..2c943ce5 100644 --- a/parser/src/parser/parser.rs +++ b/parser/src/parser/parser.rs @@ -323,7 +323,9 @@ impl Parser { Kind::Global => self.parse_global_statement(), Kind::Nonlocal => self.parse_nonlocal_statement(), _ => { - if self.cur_kind() == Kind::Indent { + if self.cur_kind() == Kind::Identifier && self.cur_token().value.to_string() == "type" { + self.parse_type_alias_statement() + } else if self.cur_kind() == Kind::Indent { let node = self.start_node(); let kind = self.cur_kind(); return Err(self.unexpected_token_new( @@ -3145,6 +3147,28 @@ impl Parser { } Ok(type_params) } + + fn parse_type_alias_statement(&mut self) -> std::result::Result { + let node = self.start_node(); + self.expect(Kind::Identifier)?; + let name = self.cur_token().value.to_string(); + self.expect(Kind::Identifier)?; + let type_params = if self.eat(Kind::LeftBrace) { + let type_params = self.parse_type_parameters()?; + self.expect(Kind::RightBrace)?; + type_params + } else { + vec![] + }; + self.expect(Kind::Assign)?; + let value = self.parse_expression_2()?; + Ok(Statement::TypeAlias(TypeAlias { + node: self.finish_node(node), + name, + type_params, + value: Box::new(value), + })) + } } #[cfg(test)] diff --git a/typechecker/src/semantic_analyzer.rs b/typechecker/src/semantic_analyzer.rs index 57aa1ca1..d5f228d2 100644 --- a/typechecker/src/semantic_analyzer.rs +++ b/typechecker/src/semantic_analyzer.rs @@ -465,6 +465,10 @@ impl TraversalVisitor for SemanticAnalyzer { self.create_symbol(f.name.clone(), function_declaration); } + fn visit_type_alias(&mut self, _t: &parser::ast::TypeAlias) { + todo!() + } + fn visit_async_function_def(&mut self, _f: &parser::ast::AsyncFunctionDef) {} fn visit_class_def(&mut self, c: &parser::ast::ClassDef) { diff --git a/typechecker/test_data/inputs/symbol_table/type_alias.py b/typechecker/test_data/inputs/symbol_table/type_alias.py new file mode 100644 index 00000000..b3afe658 --- /dev/null +++ b/typechecker/test_data/inputs/symbol_table/type_alias.py @@ -0,0 +1,24 @@ +# Show me some examples of type aliasing in Python try to give examples for all + +# 1. Type aliasing for built-in types +# 2. Type aliasing for user-defined types +# 3. Type aliasing for generic types +# 4. Type aliasing for generic types with type parameters +# 5. Type aliasing for generic types with type parameters and constraints +# 6. Type aliasing for generic types with type parameters and constraints and default values +# 7. Type aliasing for generic types with type parameters and constraints and default values and type parameters +# 8. Type aliasing for generic types with type parameters and constraints and default values and type parameters and constraints +# 9. Type aliasing for generic types with type parameters and constraints and default values and type parameters and constraints and default values +# 10. Type aliasing for generic types with type parameters and constraints and default values and type parameters and constraints and default values and type parameters +# 11. Type aliasing for generic types with type parameters and constraints and default values and type parameters and constraints and default values and type parameters and constraints +# 12. Type aliasing for generic types with type parameters and constraints and default values and type parameters and constraints and default values and type parameters and constraints and default values +# 13. Type aliasing for generic types with type parameters and constraints and default values and type parameters and constraints and default values and type parameters and constraints and default values and type parameters +# 14. Type aliasing for generic types with type parameters and constraints and default values and type parameters and constraints and default values and type parameters and constraints and default values and type parameters and constraints + +type Alias1 = int +type Alias2 = str +type Alias3 = float + +type AliasWithParams1[T] = T +type AliasWithParams2[T] = T +