命令在我制作的解释器中不起作用

Command not working in an interpreter I made

(请耐心等待,我的代码真的很草率,可能有点难以理解,主要是因为所有的变量,globals 和 if 语句...)

我创建了一种解释性的、深奥的、基于磁带的编程语言,专为名为 Tellurium 的代码高尔夫而设计(它是一个元素 fyi)。到目前为止,除了条件语句外,它一直运行良好。

条件语句的语法如下:

(integer[code]

这是 Python 的等价物:

if tape[selectedCell] == integer: code

所以,我的问题是您只能在代码中使用语句一次。用了不止一次,还是不行。它甚至不会抛出错误。它什么都不做。


下面是一些不起作用的命令示例,以及它们应该输出的内容。

(0[^](1[^]+(1[^] 应该输出 0 后跟 1。相反,只输出 0。我认为这与 first (1[^],解释器应该跳过它,因为所选单元格的值不是 1。

这是另一个例子:+++++(0[^](5[^]。单元格的值递增到 5。然后,(0[^] 检查单元格的值是否等于零。好吧,它不是,所以它应该被跳过。接下来,(5[^] 检查单元格的值是否等于 5。是的,它应该输出5。相反,它输出 5\n5.

又一个例子:(0[^](1[^](0[^](1[^] 指令应该被解释器完全忽略,因为单元格的值不是 1。它仍然是零。因此,所需的输出应该是 0\n0,因为有两个 0[^] 命令。

(0[^] 完美运行。它输出单元格的值,即 0。

我认为问题与应该跳过的语句有关,例如示例 2 中的 (1[^]


我不知道为什么这不起作用,我试过了我知道。非常感谢您的帮助,我真的很想重新使用这门语言!


所以,这是没有所有额外命令的解释器。

tape = [0] * 25500
readingNum = False
readingIf = False
num = []
code = []
selectedCell = 0

def prompt():
    userInput = input("> ")
    return userInput

def read(cmd):
    length = len(cmd)
    commands = list(cmd)
    for i in range(0, length):
        parse(commands[i])

def parse(cmd):
    global tape
    global readingNum
    global readingIf
    global num
    global code
    global selectedCell

    if readingNum == True:
        if cmd == "[":
            readingNum = False
            readingIf = True

        else:               
            num.append(cmd)

    elif readingIf == True:
        if cmd == "]":
            readingIf = False
            if tape[selectedCell] == int(''.join(num)):
                read(code)
                code = []
                num = []

            else:
                return

        else:
            code.append(cmd)

    elif cmd == "^":
        print(tape[selectedCell])

    elif cmd == "+":
        tape[selectedCell] += 1

    elif cmd == "(":
        readingNum = True

    else:
        print("Error")

while 1:
    read(prompt())

这不起作用,因为您使用的是全局变量。当您为第三个 if 语句点击 if tape[selectedCell] == int(''.join(num)): 时,nums 包含 ['1', '1'] 因为两个 if 语句都在 num 中添加了 1,所以当您执行 int(''.join(num)) 时,您最终 11 不等于 1.

您需要重构此代码以停止使用全局变量。有关如何不使用全局变量来表示堆栈的工作示例,请查看 esolang I wrote a couple months back。它没有像我想要的那样完成,但它有一个没有全局变量的工作堆栈。