Skip to content

Commit

Permalink
Add type alias test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack committed Oct 29, 2023
1 parent dbacb79 commit b87b23b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
26 changes: 25 additions & 1 deletion parser/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -3145,6 +3147,28 @@ impl Parser {
}
Ok(type_params)
}

fn parse_type_alias_statement(&mut self) -> std::result::Result<Statement, ParsingError> {
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)]
Expand Down
4 changes: 4 additions & 0 deletions typechecker/src/semantic_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions typechecker/test_data/inputs/symbol_table/type_alias.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b87b23b

Please sign in to comment.