我的程序不做任何事情,如果我停止它,它会给我键盘中断traceb

2024-06-26 19:41:53 发布

您现在位置:Python中文网/ 问答频道 /正文

对不起,我的英语不好。。。你知道吗

我正在尝试用python制作brainfuck解释器-我有这个:

defaulttyp, defaultval = False, ""

def split_word(word):
    word_split = []
    word2 = word.split()
    for x in word2:
        word_split.extend(x)
    return word_split

def Brainfuck(inputlist, is_it_loop, value):
    in_loop = []
    output = []
    values = []
    pos = 0
    selected = 0
    for char in inputlist:

        if char == "+":
            if (len(values) - 1) < selected:
                values.append(0)
            values[selected] += 1

        elif char == "-":
            if (len(values) - 1) < selected:
                values.append(0)
            values[selected] -= 1

        elif char == ">":
            selected += 1
            if len(values) < selected:
                values.append(0)

        elif char == "<":
            selected -= 1

        elif char == ".":
            print(''.join(chr(values[selected])))

        elif char == "[":
            while char != "]":
                if char == "+":
                    in_loop.append("+")
                elif char == "-":
                    in_loop.append("-")
                elif char == ">":
                    in_loop.append(">")
                elif char == "<":
                    in_loop.append("<")
                elif char == ".":
                    in_loop.append(".")
                elif char == "[":
                    in_loop.append("[")
                elif char == "]":
                    in_loop.append("]")
                elif char == ",":
                    in_loop.append(",")
            while value != 0:
                Brainfuck(in_loop, True, value)

        elif char == ",":
            inp = input("")
            input1 = split_word(inp)
            if len(input1) > 1:
                continue
            else:
                values[selected] = ord(inp)

        if ((is_it_loop == True) and (values[selected] == 0)):
            value = 0
        pos += 1

#################################################################################################################

code = split_word('''
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
''')


Brainfuck(code, defaulttyp, defaultval)

但是有一些错误或者idk和程序让我写我想要的东西(作为输入),它什么都不做-如果我停止它,它会给我这个:

Traceback (most recent call last):
  File "/Users/jan/PycharmProjects/BrainfuckInPython/Brainfuck.py", line 79, in <module>
    Brainfuck(code, defaulttyp, defaultval)
  File "/Users/jan/PycharmProjects/BrainfuckInPython/Brainfuck.py", line 52, in Brainfuck
    in_loop.append("[")
KeyboardInterrupt

Process finished with exit code 1

有谁能告诉我,出了什么问题,怎么解决?谢谢-希望这不是以前那个愚蠢的问题。。。你知道吗

  • 我使用的是python3.6

好的,这是这个程序的工作版本:

def UBF(code):
    SBF(code, 0, 0, [0])

def SBF(code, start, memoryStart, memory):
    position_in_memory = memoryStart
    position_in_code = start
    while position_in_code < len(code):
        char = code[position_in_code]
        if char == '.':
            print(chr(memory[position_in_memory]), end='')
        elif char == ',':
            inp = input('Zadejte znak: ')
            memory[position_in_memory] = ord(inp)
        elif char == '+':
            memory[position_in_memory] += 1
        elif char == '-':
            memory[position_in_memory] -= 1
        elif char == '>':
            position_in_memory += 1
            if len(memory) <= position_in_memory:
                memory.append(0)
        elif char == '<':
            position_in_memory -= 1
        elif char == '[':
            position_in_code = SBF(code, position_in_code + 1, position_in_memory, memory)
        elif char == ']':
            if memory[position_in_memory] == 0:
                return position_in_code
            else:
                position_in_code = start
                continue

        position_in_code += 1

Tags: inlooplenifpositioncodewordsplit