# Path: pif_ir/bir/utils/exceptions.py
# class BIRError(Exception):
#     """ a base class for all bir exceptions
#     """
#     def __init__(self, msg="BIR Error!"):
#         self.value = msg


from pif_ir.bir.utils.exceptions import BIRError

class ControlState(object):
    def __init__(self, control_state_attr, header, bir_parser):
        # control_state format:
        #   - [[cond, new_offset], [,], [,], ..., default_offset]
        #   - [[cond, new_bb], [,], [,], ..., default_bb]
        self.offset = control_state_attr[0]
        self.basic_block = control_state_attr[1]
        self.header = header
        self.bir_parser = bir_parser

    def _get_offset(self, packet, bit_offset):
        for cond in self.offset:
            if isinstance(cond, str) or isinstance(cond, int):
                return self.bir_parser.eval_inst(cond, self.header, packet,
                                                 bit_offset)
            elif self.bir_parser.eval_cond(cond[0], self.header, packet,
                                           bit_offset):
                return self.bir_parser.eval_inst(cond[1], self.header, packet,
                                                 bit_offset)
        raise BIRError("didn't find offset!")

    def _get_basic_block(self, packet, bit_offset):
        for cond in self.basic_block:
            if isinstance(cond, str):
                return cond
            elif self.bir_parser.eval_cond(cond[0], self.header, packet,
                                           bit_offset):
                return cond[1]

# Path: pif_ir/bir/utils/exceptions.py
# class BIRError(Exception):
#     """ a base class for all bir exceptions
#     """
#     def __init__(self, msg="BIR Error!"):
#         self.value = msg


# Path: pif_ir/bir/objects/control_state.py
from pif_ir.bir.utils.exceptions import BIRError


class ControlState(object):
    def __init__(self, control_state_attr, header, bir_parser):
        # control_state format:
        #   - [[cond, new_offset], [,], [,], ..., default_offset]
        #   - [[cond, new_bb], [,], [,], ..., default_bb]
        self.offset = control_state_attr[0]
        self.basic_block = control_state_attr[1]
        self.header = header
        self.bir_parser = bir_parser

    def _get_offset(self, packet, bit_offset):
        for cond in self.offset:
            if isinstance(cond, str) or isinstance(cond, int):
                return self.bir_parser.eval_inst(cond, self.header, packet,
                                                 bit_offset)
            elif self.bir_parser.eval_cond(cond[0], self.header, packet,
                                           bit_offset):
                return self.bir_parser.eval_inst(cond[1], self.header, packet,
                                                 bit_offset)
        raise BIRError("didn't find offset!")

    def _get_basic_block(self, packet, bit_offset):
        for cond in self.basic_block:
            if isinstance(cond, str):
                return cond
            elif self.bir_parser.eval_cond(cond[0], self.header, packet,
                                           bit_offset):
                return cond[1]

        raise BIRError("didn't find basic_block!")