Brainfuck翻译问题

时间:2016-09-22 12:11:13

标签: c interpreter brainfuck

我是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);
}

它只能在没有循环的情况下工作(&#34; [&#34;和&#34;]&#34;)。当我尝试时:

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

它给我输出

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

预期产出:

Hello World!

1 个答案:

答案 0 :(得分:1)

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

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

程序正在寻找另一个括号(并找到它),但你的代码指针在while - 语句中增加了(第17行)。因此,在搜索循环后,您必须按a递减1。第二个问题是,如果curr_poscom[a] == '['以及{line[curr_pos] != 0,请增加数据 -pointer(com[a] == ']')。 1}}和line[curr_pos] == 0
您必须递增代码 -pointer(a)仍在while - 语句中自动递增的内容。所以实际上你必须通过这种情况。最后,如果当前单元格值不等于零,您实际上不必检查两个括号。我建议的代码如下所示:

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程序更有趣;)