Skip to content

Commit

Permalink
Merge pull request #213 from nbuilding/generic-fix
Browse files Browse the repository at this point in the history
Fix up Generics
  • Loading branch information
SheepTester authored Jul 10, 2021
2 parents 34bd5b6 + 19787c1 commit c07208b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
8 changes: 4 additions & 4 deletions python/syntax.lark
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ anonymous_func: arguments "->" NAME ":" (instruction (";" instruction)*)
char: "\\" ( escape_code | "{" /./ "}" | "u{" hex_pattern "}")
hex_pattern: /[0-9a-fA-F]+/
escape_code: /[nrtv0fb]/
tupledef: "(" types ("," types)* ")"
tupledef: "(" types ("," types)+ (",")? ")"
recorddef: "{" (record_entry_lit (";" | "\n"))* record_entry_lit (";" | "\n")? "}"
spread: ".." NAME
record_entry_lit: CNAME ":" types
with_typevars: module_type "[" types ("," types)* (",")? "]"
declaration_with_typevars: NAME ("[" NAME ("," NAME)* (",")? "]")?
tupleval: "(" expression ("," expression)* (",")? ")"
tupleval: "(" expression ("," expression)+ (",")? ")"
listval: "[" (list_expression ("," list_expression)* (",")?)? "]"
recordval: "{" (record_entry (";" | "\n"))* record_entry (";" | "\n")? "}"
enum_constructors: ("|")? enum_constructor ("|" enum_constructor)*
Expand Down Expand Up @@ -122,8 +122,8 @@ enum_pattern: "<" NAME pattern_value* ">"
// Square brackets mean that the stuff inside it can appear 0 or 1 time. Same as
// (whatever)?

?types: not_func_type
| func_type
?types: ( "(" not_func_type ")" | not_func_type )
| ( "(" func_type ")" | func_type )
?not_func_type: module_type
| tupledef
| recorddef
Expand Down
26 changes: 18 additions & 8 deletions python/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,8 @@ def apply_generics_to(return_type, generics):
- resolve_equal_types(None, int) -> None, False
"""


def resolve_equal_types(type_a, type_b):
if type_a is None or type_b is None:
return None, False
elif isinstance(type_a, NTypeVars):
def resolve_equal_special_types(type_a, type_b):
if isinstance(type_a, NTypeVars):
if (
not isinstance(type_b, NTypeVars)
or type_a.base_type is not type_b.base_type
Expand All @@ -227,7 +224,9 @@ def resolve_equal_types(type_a, type_b):
base_type = type_a.base_type
resolved_typevars = []
for typevar_a, typevar_b in zip(type_a.typevars, type_b.typevars):
if isinstance(typevar_a, NGenericType) and typevar_a in base_type.typevars:
if isinstance(typevar_a, NGenericType) and isinstance(typevar_b, NGenericType):
resolved_typevars.append(typevar_a)
elif isinstance(typevar_a, NGenericType):
resolved_typevars.append(typevar_b)
elif (
isinstance(typevar_b, NGenericType) and typevar_b in base_type.typevars
Expand All @@ -239,8 +238,8 @@ def resolve_equal_types(type_a, type_b):
return None, True
resolved_typevars.append(resolved)
return type_a.with_typevars(resolved_typevars), False
elif isinstance(type_a, list):
if not isinstance(type_b, list) or len(type_a) != len(type_b):
elif isinstance(type_a, list) or isinstance(type_b, tuple):
if (not isinstance(type_b, list) and not isinstance(type_b, tuple)) or len(type_a) != len(type_b):
return None, True
resolved_types = []
for item_a, item_b in zip(type_a, type_b):
Expand All @@ -263,6 +262,17 @@ def resolve_equal_types(type_a, type_b):
return None, True
resolved_types[key] = resolved
return resolved_types, False
return None

def resolve_equal_types(type_a, type_b):
type_a_special_resolved = resolve_equal_special_types(type_a, type_b)
type_b_special_resolved = resolve_equal_special_types(type_b, type_a)
if type_a is None or type_b is None:
return None, False
elif type_a_special_resolved:
return type_a_special_resolved
elif type_b_special_resolved:
return type_b_special_resolved
elif type_a == type_b:
return type_a, False
else:
Expand Down

0 comments on commit c07208b

Please sign in to comment.