# Path: pym2149/dosound.py
# def issleepcommand(ctrl):
#     return ctrl >= 0x82


from .dosound import issleepcommand
import itertools, re
    @staticmethod
    def number(s):
        if s[0] == '%':
            return int(s[1:], 2)
        elif s[0] == '$':
            return int(s[1:], 16)
        else:
            return int(s) # XXX: Could it be octal?

    def __init__(self, aligned):
        self.bytes = []
        self.aligned = aligned

    def startingnow(self):
        return Label(self.bytes, len(self.bytes))

    def process(self, line):
        key = line.directive.lower()
        if self.aligned and 'even' == key:
            if len(self.bytes) & 1:
                self.bytes.append(0) # May be used as value by accident.
        elif 'dc.w' == key:
            self.bytes.extend([None, None]) # Unknown endianness.
        elif 'dc.b' == key:
            self.bytes.extend(map(self.number, line.argstext.split(',')))
        else:
            raise UnsupportedDirectiveException(line.directive)

    def hasterminator(self):
        # TODO LATER: Support termination not just at end of line.

# Path: pym2149/dosound.py
# def issleepcommand(ctrl):
#     return ctrl >= 0x82


# Path: pym2149/budgie.py
from .dosound import issleepcommand
import itertools, re

    @staticmethod
    def number(s):
        if s[0] == '%':
            return int(s[1:], 2)
        elif s[0] == '$':
            return int(s[1:], 16)
        else:
            return int(s) # XXX: Could it be octal?

    def __init__(self, aligned):
        self.bytes = []
        self.aligned = aligned

    def startingnow(self):
        return Label(self.bytes, len(self.bytes))

    def process(self, line):
        key = line.directive.lower()
        if self.aligned and 'even' == key:
            if len(self.bytes) & 1:
                self.bytes.append(0) # May be used as value by accident.
        elif 'dc.w' == key:
            self.bytes.extend([None, None]) # Unknown endianness.
        elif 'dc.b' == key:
            self.bytes.extend(map(self.number, line.argstext.split(',')))
        else:
            raise UnsupportedDirectiveException(line.directive)

    def hasterminator(self):
        # TODO LATER: Support termination not just at end of line.

        return len(self.bytes) >= 2 and not self.bytes[-1] and issleepcommand(self.bytes[-2])