0

我为一种名为Self-modifying Brainf*** (SMBF)的语言编写了这个 Python 解释器。今天我发现了一个错误,如果程序在初始单元格处或之后在磁带上动态创建代码,它将不会被执行。我编写这个解释器是为了尽可能接近链接页面上的 Ruby 解释器。请注意,这个错误也可能存在于原始的 Ruby 解释器中。不知道,我没用过。

SMBF 与普通 BF 的不同之处在于源代码放在磁带上指针开始所在单元格的左侧。因此程序<.将打印源的最后一个字符(句点)。这行得通。

请注意,我删除了一些代码,因此它仍然可以运行,但在这篇文章中占用的空间更少。

口译员:

from __future__ import print_function
import os, sys

class Tape(bytearray):
    def __init__(self):
        self.data = bytearray(b'\0' * 1000)
        self.center = len(self.data) // 2

    def __len__(self):
        return len(self.data)

    def __getitem__(self, index):
        try:
            return self.data[index + self.center]
        except:
            return 0

    def __setitem__(self, index, val):

        i = index + self.center

        if i < 0 or i >= len(self.data):
            # resize the data array to be large enough

            new_size = len(self.data)

            while True:
                new_size *= 2
                test_index = index + (new_size // 2)
                if test_index >= 0 and test_index < new_size:
                    # array is big enough now
                    break

            # generate the new array
            new_data = bytearray(b'\0' * new_size)
            new_center = new_size // 2

            # copy old data into new array
            for j in range(0, len(self.data)):
                new_data[j - self.center + new_center] = self.data[j]

            self.data = new_data
            self.center = new_center

        self.data[index + self.center] = val & 0xff

class Interpreter():
    def __init__(self, data):
        self.tape = Tape()

        # copy the data into the tape
        for i in range(0, len(data)):
            self.tape[i - len(data)] = data[i]

        # program start point

        self.entrypoint = -len(data)

    def call(self):
        pc = self.entrypoint
        ptr = 0

        # same as -len(self.tape) // 2 <= pc + self.tape.center < len(self.tape) // 2
        while -len(self.tape) <= pc < 0: # used to be "while pc < 0:"
            c = chr(self.tape[pc])
            if   c == '>':
                ptr += 1
            elif c == '<':
                ptr -= 1
            elif c == '+':
                self.tape[ptr] += 1
            elif c == '-':
                self.tape[ptr] -= 1
            elif c == '.':
                print(chr(self.tape[ptr]), end="")
            elif c == ',':
                sys.stdin.read(1)
            elif c == '[':
                if self.tape[ptr] == 0:
                    # advance to end of loop
                    loop_level = 1
                    while loop_level > 0:
                        pc += 1
                        if   chr(self.tape[pc]) == '[': loop_level += 1
                        elif chr(self.tape[pc]) == ']': loop_level -= 1
            elif c == ']':
                # rewind to the start of the loop
                loop_level = 1
                while loop_level > 0:
                    pc -= 1
                    if   chr(self.tape[pc]) == '[': loop_level -= 1
                    elif chr(self.tape[pc]) == ']': loop_level += 1
                pc -= 1
            pc += 1

            # DEBUG
            #print(pc, self.tape.data.find(b'.'))

def main():
    # Working "Hello, World!" program.
    #data = bytearray(b'<[.<]>>>>>>>>+\x00!dlroW ,olleH')

    # Should print a period, but doesn't.
    data = bytearray(b'>++++++++++++++++++++++++++++++++++++++++++++++')

    intr = Interpreter(data)
    intr.call()
    #print(intr.tape.data.decode('ascii').strip('\0'))

if __name__ == "__main__":
    main()

问题:

这一行是我设置程序的方式(所以我可以在 Ideone.com 上运行它):

data = bytearray(b'++++++++++++++++++++++++++++++++++++++++++++++')

程序添加到单元格直到它是 46,这是 ASCII 的十进制值.,它应该打印当前单元格(句点)。但是由于某种原因,程序计数器pc永远不会到达那个单元格。我希望程序运行它找到的所有代码,直到它到达磁带的末尾,但是我很难让程序计数器考虑磁带的中心,并确保它仍然正确,如果磁带中调整大小__setitem__

相关行是(我正在尝试):

while -len(self.tape) <= pc < 0:

原来是这样的:

while pc < 0:

所以我认为这while条线要么需要调整,要么我需要将其更改为while True:并使用一段try/except时间chr(self.tape[pc])来确定我是否已经到达磁带的末尾。

有谁知道出了什么问题或如何解决?

4

1 回答 1

0

借助 Sp3000 找到了解决方案。

self.end = 0in Tape.__init__, self.end = max(self.end, index+1)inTape.__setitem__并将whileinInterpreter.call替换为while pc < self.tape.end:.

于 2015-10-09T15:46:43.417 回答