我为一种叫做Self-modifying Brainf***(SMBF)的语言编写了这个Python解释器。今天我发现了一个bug,如果程序在磁带上的初始单元格或之后动态创建代码,它将不会被执行。我编写这个解释器是为了尽可能接近链接页面上的Ruby解释器。注意,这个bug可能也存在于原始的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:
,然后在得到chr(self.tape[pc])
时使用try/except
来确定我是否到达了磁带的末尾。你知道吗
有没有人知道哪里出了问题或者怎么解决?你知道吗
借助Sp3000找到了解决方案。你知道吗
self.end = 0
在Tape.__init__
中,self.end = max(self.end, index+1)
在Tape.__setitem__
中,并用while pc < self.tape.end:
替换Interpreter.call
中的while
。你知道吗相关问题 更多 >
编程相关推荐