コード例 #1
0
ファイル: virtualmachine.py プロジェクト: ShadenSmith/Mirza
    def run(self, program):
        '''Execute list of instructions'''
        end = program[0] # First opcode is length
        self.memory = [0] * program[1] # Second opcode is memory size requirement
        self.pc = 2

        while self.pc < end and program[self.pc] != opset.HALT:
            instruction = program[self.pc]
            self.pc += 1
            if opset.nullary(instruction):
                self.debug("%02d %s", self.pc, opset.byte2name[instruction])
                self.nullary[instruction]()
            elif opset.unary(instruction):
                argument = program[self.pc]
                self.pc += 1
                self.debug("%02d %s %s", self.pc, opset.byte2name[instruction], argument)
                self.unary[instruction - opset.LIT](argument)
コード例 #2
0
ファイル: assembler.py プロジェクト: ShadenSmith/Mirza
    def findsymbols(self, program):
        """Find labels and variables and build symboltable"""

        symbols = {opset.LABEL: {}, opset.MEM: {}}
        pc = addr = 0
        end = len(program)
        newprogram = [0, 0]

        self.debug("Finding labels and variables")

        while pc < end:
            instruction = program[pc]
            pc += 1
            if opset.nullary(instruction):
                newprogram.append(instruction)
            elif opset.unary(instruction):
                argument = program[pc]
                pc += 1
                if instruction in (opset.LABEL, opset.MEM):
                    if argument in symbols[instruction]:
                        raise Exception(
                            "Cannot declare symbol more than once: %s %s" % (opset.byte2name[instruction], argumet)
                        )
                    if instruction == opset.LABEL:
                        value = len(newprogram)
                    else:
                        value = addr
                        addr += 1
                    self.debug("Symbol - %s %s -> %s", opset.byte2name[instruction], argument, value)
                    symbols[instruction][argument] = value
                else:
                    newprogram.append(instruction)
                    newprogram.append(argument)
        # First instruction is length
        newprogram[0] = len(newprogram)
        # Second instruction is memory requirement
        newprogram[1] = addr
        return newprogram, symbols