60 lines
1.6 KiB
Python
60 lines
1.6 KiB
Python
from typing import Dict, Iterable, List, Mapping, Set
|
|
|
|
|
|
class Type3ClassMethod:
|
|
__slots__ = ('type3_class', 'name', 'signature', )
|
|
|
|
type3_class: 'Type3Class'
|
|
name: str
|
|
signature: str
|
|
|
|
def __init__(self, type3_class: 'Type3Class', name: str, signature: str) -> None:
|
|
self.type3_class = type3_class
|
|
self.name = name
|
|
self.signature = signature
|
|
|
|
@property
|
|
def signature_parts(self) -> List[str]:
|
|
return self.signature.split(' -> ')
|
|
|
|
@property
|
|
def type_vars(self) -> Set[str]:
|
|
return {
|
|
x
|
|
for x in self.signature_parts
|
|
if 1 == len(x)
|
|
and x == x.lower()
|
|
}
|
|
|
|
def __repr__(self) -> str:
|
|
return f'Type3ClassMethod({repr(self.type3_class)}, {repr(self.name)}, {repr(self.signature)})'
|
|
|
|
class Type3Class:
|
|
__slots__ = ('name', 'args', 'methods', 'operators', )
|
|
|
|
name: str
|
|
args: List[str]
|
|
methods: Dict[str, Type3ClassMethod]
|
|
operators: Dict[str, Type3ClassMethod]
|
|
|
|
def __init__(self, name: str, args: Iterable[str], methods: Mapping[str, str], operators: Mapping[str, str]) -> None:
|
|
self.name = name
|
|
self.args = [*args]
|
|
self.methods = {
|
|
k: Type3ClassMethod(self, k, v)
|
|
for k, v in methods.items()
|
|
}
|
|
self.operators = {
|
|
k: Type3ClassMethod(self, k, v)
|
|
for k, v in operators.items()
|
|
}
|
|
|
|
def __repr__(self) -> str:
|
|
return self.name
|
|
|
|
Num = Type3Class('Num', ['a'], methods={}, operators={
|
|
'+': 'a -> a -> a',
|
|
'-': 'a -> a -> a',
|
|
'*': 'a -> a -> a',
|
|
})
|