64 lines
1.4 KiB
Python
64 lines
1.4 KiB
Python
class Type: pass
|
|
|
|
class UnknownType(Type):
|
|
def __repr__(self):
|
|
return 'UnknownType()'
|
|
|
|
def __str__(self):
|
|
return '?'
|
|
|
|
class Rational(Type):
|
|
def __repr__(self):
|
|
return 'Rational()'
|
|
|
|
def __str__(self):
|
|
return f'rational'
|
|
|
|
class Integer(Type):
|
|
def __init__(self, min = None, max = None):
|
|
self.min = min
|
|
self.max = max
|
|
|
|
def __repr__(self):
|
|
if self.min is None and self.max is None:
|
|
return 'Integer()'
|
|
else:
|
|
return f'Integer({repr(self.min)}, {repr(self.max)})'
|
|
|
|
def __str__(self):
|
|
if self.min is None and self.max is None:
|
|
return f'integer'
|
|
elif self.min is None:
|
|
return f'integer[..{self.max}]'
|
|
elif self.max is None:
|
|
return f'integer[{self.min}..]'
|
|
elif self.min == self.max:
|
|
return f'integer[{self.min}]'
|
|
else:
|
|
return f'integer'
|
|
|
|
class Function(Type):
|
|
def __init__(self, args, result):
|
|
self.args = args
|
|
self.result = result
|
|
|
|
def __repr__(self):
|
|
return f'Function({repr(self.args)}, {repr(self.result)})'
|
|
|
|
def __str__(self):
|
|
if len(self.args) == 1:
|
|
return f'{self.args[0]} → {self.result}'
|
|
else:
|
|
args = ', '.join(str(arg) for arg in self.args)
|
|
return f'({args}) → {self.result}'
|
|
|
|
class Or(Type):
|
|
def __init__(self, *possibilities):
|
|
assert len(possibilities) > 1
|
|
self.possibilities = possibilities
|
|
|
|
def __repr__(self):
|
|
return 'Possibilities(' + ', '.join(map(repr, self.possibilities)) +')'
|
|
|
|
def __str__(self):
|
|
return '(' + ')|('.join(map(str, self.possibilities)) + ')'
|