nac3-spec/toy-impl/type_def.py

102 lines
2.4 KiB
Python
Raw Normal View History

2020-12-17 14:50:51 +08:00
class Type:
2020-12-17 17:00:43 +08:00
def __eq__(self, other):
return False
2020-12-17 14:50:51 +08:00
class BotType:
2020-12-17 17:00:43 +08:00
def __eq__(self, other):
return isinstance(other, BotType)
2020-12-17 14:50:51 +08:00
class PrimitiveType(Type):
name: str
def __init__(self, name: str):
self.name = name
def __str__(self):
return self.name
2020-12-17 17:00:43 +08:00
def __eq__(self, other):
return isinstance(other, PrimitiveType) and self.name == other.name
2020-12-17 14:50:51 +08:00
class TypeVariable(Type):
name: str
2020-12-17 17:00:43 +08:00
# this may be a list of str, Type may not be determined when type variables
# are instantiated...
# and they cannot contain other type variables
2020-12-17 14:50:51 +08:00
constraints: list[Type]
def __init__(self, name: str, constraints: list[Type]):
self.name = name
self.constraints = constraints
def __str__(self):
return self.name
2020-12-17 17:00:43 +08:00
def __eq__(self, other):
return isinstance(other, TypeVariable) and self.name == other.name
2020-12-17 14:50:51 +08:00
class ClassType(Type):
name: str
parents: list['ClassType']
methods: dict[str, tuple[list[Type], Type, set[str]]]
fields: dict[str, Type]
def __init__(self, name: str):
self.name = name
self.parents = []
self.methods = {}
self.fields = {}
def __str__(self):
return self.name
2020-12-17 17:00:43 +08:00
def __eq__(self, other):
return isinstance(other, ClassType) and self.name == other.name
2020-12-17 14:50:51 +08:00
class VirtualClassType(Type):
base: ClassType
def __init__(self, base: ClassType):
self.base = base
def __str__(self):
return f"virtual[{self.base}]"
2020-12-17 17:00:43 +08:00
def __eq__(self, other):
return isinstance(other, VirtualClassType) and self.base== other.base
class ParametricType(Type):
params: list[Type]
def __init__(self, params: list[Type]):
self.params = params
def __eq__(self, other):
if type(self) != type(other) or len(self.params) != len(other.params):
return False
for x, y in zip(self.params, other.params):
if x != y:
return False
return True
2020-12-17 14:50:51 +08:00
2020-12-17 17:00:43 +08:00
class ListType(ParametricType):
def __init__(self, param: Type):
super().__init__([param])
2020-12-17 14:50:51 +08:00
def __str__(self):
2020-12-17 17:00:43 +08:00
return f"list[{self.params[0]}]"
2020-12-17 14:50:51 +08:00
2020-12-17 17:00:43 +08:00
class TupleType(ParametricType):
def __init__(self, params: list[Type]):
super().__init__(params)
2020-12-17 14:50:51 +08:00
def __str__(self):
2020-12-17 17:00:43 +08:00
return f"tuple[{', '.join([str(v) for v in self.params])}]"
2020-12-17 14:50:51 +08:00