forked from M-Labs/artiq
1
0
Fork 0

compiler.types: make all hashable.

This commit is contained in:
whitequark 2015-08-10 13:15:42 +03:00
parent 8f510a4407
commit 4f02f6e667
1 changed files with 20 additions and 3 deletions

View File

@ -6,6 +6,12 @@ in :mod:`asttyped`.
import string import string
from collections import OrderedDict from collections import OrderedDict
class UnificationError(Exception):
def __init__(self, typea, typeb):
self.typea, self.typeb = typea, typeb
def genalnum(): def genalnum():
ident = ["a"] ident = ["a"]
while True: while True:
@ -22,9 +28,8 @@ def genalnum():
if pos < 0: if pos < 0:
ident = ["a"] + ident ident = ["a"] + ident
class UnificationError(Exception): def _freeze(dict_):
def __init__(self, typea, typeb): return tuple((key, dict_[key]) for key in dict_)
self.typea, self.typeb = typea, typeb
def _map_find(elts): def _map_find(elts):
if isinstance(elts, list): if isinstance(elts, list):
@ -34,6 +39,7 @@ def _map_find(elts):
else: else:
assert False assert False
class Type(object): class Type(object):
pass pass
@ -93,6 +99,7 @@ class TMono(Type):
attributes = OrderedDict() attributes = OrderedDict()
def __init__(self, name, params={}): def __init__(self, name, params={}):
assert isinstance(params, dict)
self.name, self.params = name, params self.name, self.params = name, params
def find(self): def find(self):
@ -127,6 +134,9 @@ class TMono(Type):
def __ne__(self, other): def __ne__(self, other):
return not (self == other) return not (self == other)
def __hash__(self):
return hash((self.name, _freeze(self.params)))
class TTuple(Type): class TTuple(Type):
""" """
A tuple type. A tuple type.
@ -181,6 +191,9 @@ class TFunction(Type):
attributes = OrderedDict() attributes = OrderedDict()
def __init__(self, args, optargs, ret): def __init__(self, args, optargs, ret):
assert isinstance(args, OrderedDict)
assert isinstance(optargs, OrderedDict)
assert isinstance(ret, Type)
self.args, self.optargs, self.ret = args, optargs, ret self.args, self.optargs, self.ret = args, optargs, ret
def arity(self): def arity(self):
@ -222,6 +235,9 @@ class TFunction(Type):
def __ne__(self, other): def __ne__(self, other):
return not (self == other) return not (self == other)
def __hash__(self):
return hash((_freeze(self.args), _freeze(self.optargs), self.ret))
class TRPCFunction(TFunction): class TRPCFunction(TFunction):
""" """
A function type of a remote function. A function type of a remote function.
@ -249,6 +265,7 @@ class TBuiltin(Type):
""" """
def __init__(self, name): def __init__(self, name):
assert isinstance(name, str)
self.name = name self.name = name
self.attributes = OrderedDict() self.attributes = OrderedDict()