Brainfuck 解释器问题

Brainfuck interpreter problems

我是 C 的新手。目前我正在尝试编写一个 Brainfuck 解释器。到目前为止我已经试过了。

#include <unistd.h>
#include <stdlib.h>

char    *line;
int     curr_pos;

void    interprete(char *coms)
{
    int a;
    int curr_loop;

    a = -1;
    curr_loop = 0;
    while (line[++a])
        line[a] = 0;
    a = -1;
    while (coms[++a])
    {
        if (coms[a] == '+')
            line[curr_pos]++;
        else if (coms[a] == '-')
            line[curr_pos]--;
        else if (coms[a] == '>')
            curr_pos++;
        else if (coms[a] == '<')
            curr_pos--;
        else if (coms[a] == '.')
            write(1, &line[curr_pos], 1);
        else if (coms[a] == '[')
        {
            if (line[curr_pos])
                curr_pos++;
            else
            {
                curr_loop = 1;
                while (curr_loop)
                {
                    ++a;
                    if (coms[a] == '[')
                        curr_loop++;
                    else if (coms[a] == ']')
                        curr_loop--;
                }
            }
        }
        else if (coms[a] == ']')
        {
            if (line[curr_pos])
            {
                curr_loop = 1;
                while (curr_loop)
                {
                    --a;
                    if (coms[a] == '[')
                        curr_loop--;
                    else if (coms[a] == ']')
                        curr_loop++;
                }
            }
            else
                curr_pos++;
        }
    }
}

int main(int ac, char **av)
{
    if (ac == 2)
    {
        curr_pos = 0;
        line = malloc(sizeof(char) * 4096);
        interprete(av[1]);
    }
    write(1, "\n", 1);
}

它只在没有循环(“[”和“]”)的情况下工作。当我尝试时:

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

它给了我输出

^B^A^H^H^K^B^Q^K^N^H^@^C^@

预期输出:

Hello World!

我猜问题出在以下代码块中:

else if (coms[a] == '[')
{
    ...
}
else if (coms[a] == ']')
{
    ...
}

程序正在寻找另一个括号(并找到它),但是您的代码指针在您的 while 语句(第 17 行)中另外递增。因此,您必须在搜索循环后将 a 减去 1。第二个问题是,你增加 data-pointer (curr_pos)
if com[a] == '[' and line[curr_pos] != 0
如果 com[a] == ']'line[curr_pos] == 0
你必须递增 code-pointer (a) 仍然会自动递增你的 while-声明。所以实际上你必须在这种情况下通过。最后,如果当前单元格值不等于零,您实际上不必检查 both 括号。我建议的代码如下所示:

else if (coms[a] == '[')
{
    if (!line[curr_pos])
    {
        curr_loop = 1;
        while (curr_loop)
        {
            ++a;
            if (coms[a] == '[')
                cur_loop++;
            else if (coms[a] == ']')
                cur_loop--;
        }
        a--;
    }
}
else if (coms[a] == ']')
{
    // You can always jump back to the opening bracket '['
    // because then the program checks again and jumps behind
    // the closing bracket if line[a] != 0
    curr_loop = 1;
    while (curr_loop)
    {
        --a;
        if (coms[a] == '[')
            curr_loop--;
        else if (coms[a] == ']')
            curr_loop++;
    }
    a--;
}

顺便说一句:尝试执行“,”命令。它让 brainfuck 程序变得更有趣 ;)