在Python中解析Brainfuck括号时遇到问题

2024-06-26 19:35:54 发布

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

我正在用Python为Brainfuck做一个编译器,作为使用该语言的练习。大多数符号并没有太大的挑战性,但我完全被[]难住了。你知道吗

这是我到目前为止的情况

global cells
global pointer
cells = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
pointer = 0

#bfcode = list(input())
bfcode = list("+x+++,[>++++<-]>.")
for i in range(len(bfcode)):
    if(bfcode[i] == "."):
        print(cells[pointer])
    elif(bfcode[i] == ","):
        charin = list(input("Write one character: "))[0]
        cells[pointer] = ord(charin)
    elif(bfcode[i] == "+"):
        cells[pointer] += 1
    elif(bfcode[i] == "-"):
        cells[pointer] -= 1
    elif(bfcode[i] == ">"):
        pointer += 1
    elif(bfcode[i] == "<"):
        pointer -= 1
    elif(bfcode[i] == "["):
        print("PLACEHOLDER")
    elif(bfcode[i] == "]"):
        print("PLACEHOLDER")
    else:
        print("Non-brainfuck character detected")
    print(cells)

因此,作为一个Python新手,我的第一直觉是在for循环中以某种方式向后走,但从我所能看出的情况来看,这在Python中是不可能的。我在google上搜索了一个循环中倒退的解决方案,但目前所有这些都远远超出了我的理解范围。。。你知道吗

有没有一种简单的方法可以让我在for循环中倒转,而我却错过了?或者有没有一个更简单的方法一起做?你知道吗

我的代码的其他提示和建议也很感谢,因为我还在学习:)


Tags: 方法forinput情况globalplaceholderlistprint
1条回答
网友
1楼 · 发布于 2024-06-26 19:35:54

以下是我所做的:

elif ins == '[':
                # only does stuff if memory isnt zero
                if mem[ptr] == 0:
                    # initialistation
                    count = 0
                    ic += 1
                    # keeps going until the end of the program
                    while ic <= len(self.prg):
                        # sets instruction
                        ins = self.prg[ic]
                        # if has found the matching bracket, it ends
                        if ins == ']' and count == 0:
                            break
                        # deals with nested loops
                        elif ins == '[':
                            count += 1
                        elif ins == ']':
                            count -= 1
                        # moves forward one place in the program
                        ic += 1
            # its an algorithim, dont ask
            elif ins == ']':
                # only does stuff if memory is zero
                if mem[ptr] != 0:
                    # initialistation
                    count = 0
                    ic -= 1
                    # keeps going until the end of the program
                    while ic >= 0:
                        # sets instruction
                        ins = self.prg[ic]
                        # if has found the matching bracket, it ends
                        if ins == '[' and count == 0:
                            break
                        # deals with nested loops
                        elif ins == ']':
                            count += 1
                        elif ins == '[':
                            count -= 1
                        # moves backward one place in the program
                        ic -= 1

它可能不会使用与你相同的结构,但应该给你一些想法。你知道吗

简言之,循环遍历代码,找到一个开括号时加一,找到一个闭括号时减一,以找到匹配的闭括号。你知道吗

相关问题 更多 >