2015-10-16 09:40:38 +00:00
|
|
|
"""Data/storage model for SixtyPical."""
|
|
|
|
|
2015-10-18 16:23:01 +00:00
|
|
|
class Type(object):
|
2015-10-16 09:40:38 +00:00
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def __repr__(self):
|
2015-10-18 16:23:01 +00:00
|
|
|
return 'Type(%r)' % self.name
|
2015-10-16 09:40:38 +00:00
|
|
|
|
2015-10-16 22:12:52 +00:00
|
|
|
def __eq__(self, other):
|
2015-10-18 16:23:01 +00:00
|
|
|
return isinstance(other, Type) and other.name == self.name
|
2015-10-16 22:12:52 +00:00
|
|
|
|
2015-10-18 16:23:01 +00:00
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.name)
|
2015-10-16 09:40:38 +00:00
|
|
|
|
2015-10-18 16:23:01 +00:00
|
|
|
|
|
|
|
TYPE_BIT = Type('bit')
|
|
|
|
TYPE_BYTE = Type('byte')
|
|
|
|
TYPE_BYTE_TABLE = Type('byte table')
|
|
|
|
|
|
|
|
|
|
|
|
class Ref(object):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class LocationRef(Ref):
|
|
|
|
def __init__(self, type, name):
|
|
|
|
self.type = type
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
# Ordinarily there will only be one ref with a given name,
|
|
|
|
# but because we store the type in here and we want to treat
|
|
|
|
# these objects as immutable, we compare the types, too.
|
|
|
|
# Not sure if very wise.
|
|
|
|
return isinstance(other, LocationRef) and (
|
|
|
|
other.name == self.name and other.type == self.type
|
|
|
|
)
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.name + str(self.type))
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '%s(%r, %r)' % (self.__class__.__name__, self.type, self.name)
|
|
|
|
|
|
|
|
|
|
|
|
class ConstantRef(Ref):
|
|
|
|
def __init__(self, type, value):
|
|
|
|
self.type = type
|
2015-10-16 09:40:38 +00:00
|
|
|
self.value = value
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return 'ConstantRef(%r)' % self.value
|
|
|
|
|
2015-10-16 22:12:52 +00:00
|
|
|
def __eq__(self, other):
|
2015-10-18 16:23:01 +00:00
|
|
|
return isinstance(other, ConstantRef) and (
|
|
|
|
other.type == self.type and other.value == self.value
|
|
|
|
)
|
2015-10-16 22:12:52 +00:00
|
|
|
|
2015-10-18 16:23:01 +00:00
|
|
|
def __hash__(self):
|
|
|
|
return hash(str(self.value) + str(self.type))
|
2015-10-16 09:40:38 +00:00
|
|
|
|
2015-10-18 16:23:01 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return '%s(%r, %r)' % (self.__class__.__name__, self.type, self.value)
|
2015-10-16 09:40:38 +00:00
|
|
|
|
|
|
|
|
2015-10-18 16:23:01 +00:00
|
|
|
REG_A = LocationRef(TYPE_BYTE, 'a')
|
|
|
|
REG_X = LocationRef(TYPE_BYTE, 'x')
|
|
|
|
REG_Y = LocationRef(TYPE_BYTE, 'y')
|
2015-10-16 09:40:38 +00:00
|
|
|
|
2015-10-18 16:23:01 +00:00
|
|
|
FLAG_Z = LocationRef(TYPE_BIT, 'z')
|
|
|
|
FLAG_C = LocationRef(TYPE_BIT, 'c')
|
|
|
|
FLAG_N = LocationRef(TYPE_BIT, 'n')
|
|
|
|
FLAG_V = LocationRef(TYPE_BIT, 'v')
|