block.py 1.03 KB
class Block:
    def __init__(self, type, line=''):
        self.blocks = list()
        self.code = line
        self.blockType = type
        self.indent = -1

    def setIndent(self, indent):
        self.indent = indent

    def addLine(self, line):
        if len(self.code) > 0:
            self.code += '\n'
        self.code += line

    def addBlock(self, block):
        self.blocks.append(block)

    def debug(self):
        if self.blockType != 'TYPE_NORMAL':
            print("Block Info:", self.blockType, self.indent)
        print(self.code)

        for block in self.blocks:
            if block.indent <= self.indent:
                raise ValueError("Invalid Indent Error Occurred: {}, INDENT {} included in {}, INDENT {}".format(block.code, block.indent, self.code, self.indent))
            block.debug()

    def __str__(self):
        if len(self.code) > 0:
            result = self.code + '\n'
        else:
            result = ''

        for block in self.blocks:
            result += block.__str__()

        return result