# Path: awake/operand.py
# class ComplexValue(Operand):
#     def __init__(self, hint='complex', deps=None):
#         self.hint = hint
#         if deps:
#             self.deps = deps
#         else:
#             self.deps = set()
# 
#     def isComplex(self):
#         return True
# 
#     def getDependencies(self):
#         return self.deps
# 
#     def __str__(self):
#         return '#'+self.hint+':'+(','.join(str(dep) for dep in self.deps if not isinstance(dep, address.Address)))+'#'
# 
# Path: awake/operator.py
# class HighByte(FuncOperator):
#     name = 'hi'
# 
#     @staticmethod
#     def calculate(arg):
#         return arg >> 8
# 
# class LowByte(FuncOperator):
#     name = 'lo'
# 
#     @staticmethod
#     def calculate(arg):
#         return arg & 0xFF
# 
# class Word(FuncOperator):
#     name = 'word'
#     bits = 16
# 
#     @staticmethod
#     def calculate(hi, lo):
#         return (hi << 8) | lo
# 
#     @classmethod
#     def make(cls, hi, lo):
#         # merge word
#         if isinstance(hi, HighByte) and isinstance(lo, LowByte):
#             if hi.childs[0] == lo.childs[0]:
#                 return hi.childs[0]
# 
#         # high zero
#         if hi.value == 0:
#             return lo
# 
#         if isinstance(hi, Shr) and isinstance(lo, Shl) and hi.left == lo.left and isConstant(hi.right) and isConstant(lo.right):
#             hi_sh = hi.right.value
#             lo_sh = lo.right.value
#             if hi_sh + lo_sh == 8:
#                 return Shl16.make(lo.left, Constant(lo_sh))
# 
#         return super(Word, cls).make(hi, lo)
# 
# class LogicalNot(Operator):
#     bits = 1
# 
#     def __str__(self):
#         return 'not '+str(self.childs[0])
# 
#     @classmethod
#     def make(cls, inner):
#         try:
#             return inner.logicalNot()
#         except AttributeError:
#             return super(LogicalNot, cls).make(inner)


from awake.operand import ComplexValue
from awake.operator import HighByte, LowByte, Word, LogicalNot
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


class Context:
    def __init__(self, values=None):
        if not values:
            self.values = dict()
        else:
            self.values = dict(values)

    def setValueComplex(self, register):
        if register in ('BC', 'DE', 'HL'):
            self.setValueComplex(register[0])
            self.setValueComplex(register[1])
        else:
            self.invalidate(register)
            self.values[register] = ComplexValue('ctx')

    def setValue(self, register, value):
        assert not isinstance(value, int)  # detect common errors

        if register in ('BC', 'DE', 'HL'):

# Path: awake/operand.py
# class ComplexValue(Operand):
#     def __init__(self, hint='complex', deps=None):
#         self.hint = hint
#         if deps:
#             self.deps = deps
#         else:
#             self.deps = set()
# 
#     def isComplex(self):
#         return True
# 
#     def getDependencies(self):
#         return self.deps
# 
#     def __str__(self):
#         return '#'+self.hint+':'+(','.join(str(dep) for dep in self.deps if not isinstance(dep, address.Address)))+'#'
# 
# Path: awake/operator.py
# class HighByte(FuncOperator):
#     name = 'hi'
# 
#     @staticmethod
#     def calculate(arg):
#         return arg >> 8
# 
# class LowByte(FuncOperator):
#     name = 'lo'
# 
#     @staticmethod
#     def calculate(arg):
#         return arg & 0xFF
# 
# class Word(FuncOperator):
#     name = 'word'
#     bits = 16
# 
#     @staticmethod
#     def calculate(hi, lo):
#         return (hi << 8) | lo
# 
#     @classmethod
#     def make(cls, hi, lo):
#         # merge word
#         if isinstance(hi, HighByte) and isinstance(lo, LowByte):
#             if hi.childs[0] == lo.childs[0]:
#                 return hi.childs[0]
# 
#         # high zero
#         if hi.value == 0:
#             return lo
# 
#         if isinstance(hi, Shr) and isinstance(lo, Shl) and hi.left == lo.left and isConstant(hi.right) and isConstant(lo.right):
#             hi_sh = hi.right.value
#             lo_sh = lo.right.value
#             if hi_sh + lo_sh == 8:
#                 return Shl16.make(lo.left, Constant(lo_sh))
# 
#         return super(Word, cls).make(hi, lo)
# 
# class LogicalNot(Operator):
#     bits = 1
# 
#     def __str__(self):
#         return 'not '+str(self.childs[0])
# 
#     @classmethod
#     def make(cls, inner):
#         try:
#             return inner.logicalNot()
#         except AttributeError:
#             return super(LogicalNot, cls).make(inner)


# Path: awake/context.py
from awake.operand import ComplexValue
from awake.operator import HighByte, LowByte, Word, LogicalNot

# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


class Context:
    def __init__(self, values=None):
        if not values:
            self.values = dict()
        else:
            self.values = dict(values)

    def setValueComplex(self, register):
        if register in ('BC', 'DE', 'HL'):
            self.setValueComplex(register[0])
            self.setValueComplex(register[1])
        else:
            self.invalidate(register)
            self.values[register] = ComplexValue('ctx')

    def setValue(self, register, value):
        assert not isinstance(value, int)  # detect common errors

        if register in ('BC', 'DE', 'HL'):

            self.setValue(register[0], HighByte(value))