3

这是我的interpereter的代码:

program=list(raw_input('Program: '))
array = [0 for i in range(100)]
pointer=0
prgpointer=0
run = True
while run:
    try:
        command = program[prgpointer]
        if command == '>':
            pointer += 1
        if command == '<':
            pointer -= 1
        if command == '+':
            array[pointer]=array[pointer]+1
        if command == '-':
            array[pointer]=array[pointer]-1
        if command == '.':
            print(array[pointer])
        if command == '[' and array[pointer]==0:
            while program[prgpointer]!=']':
                prgpointer+=1
        if command == ']' and array[pointer]!=0:
            while program[prgpointer]!='[':
                prgpointer-=1        
    except:
        run = False  
    prgpointer+=1

当我运行这个程序时:

++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.

我得到的结果

-1
-3
4
4
7
0
-2
7
10
4
-4
1
1

这个 porgram 是任何其他 bf interpereter 中的功能性“hello world”程序。即使将输出转换为 ASCII,它也不是“Hello World”。关于我的 interpereter 是否有任何重大问题可以指出?命令是否正确?

4

1 回答 1

4
  1. 要在您的数据指针处打印 char 值,您需要sys.stdout.write(chr(array[pointer])),否则您将只打印数字——毕竟所有数字都是您列表中的内容。

  2. 语言规范说明了这一点,[]表示跳转到匹配的括号。您正在跳转到下一个/上一个括号。

于 2016-02-22T02:51:38.407 回答