c - Brainfuck 解释器不运行某些代码

标签 c interpreter brainfuck

我对 C 编程有点陌生,并且认为用 C 语言制作一个脑残的解释器将是学习这门语言的好方法。我可以用这些 bf 代码编写和测试:

这应该打印一个 hello world

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

这按预期工作,所以我认为我的解释器工作得很好,但是当我用 hello world 代码的几个变体进行测试时,奇怪的事情发生了。

这个 bf 代码还应该打印一个 hello world,但它却打印出 ²♣■■ÖFu ÖÖ■♦u

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

这个 bf 代码还应该打印一个 hello world,但程序却卡住了

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

这是我编写的用于解释 Brainfuck 的代码:

#include <stdio.h>

int main(int argc, char const *argv[])
{

    if (argc == 1)
    {
        printf("You must specify a file path\n");
        return -1;
    }

    //amount of memory locations available
    int mem = 30000;
    //creating an integer array with mem positions
    char arr[mem];
    //current memory position
    int index = 0;

    //setting everything to 0
    for (int i = 0; i < mem; i++)
    {
        arr[i] = 0;
    }

    FILE *file = fopen(argv[1], "r");

    if (file == NULL)
    {
        printf("ERROR, file couldn't be read\n");
        return -1;
    }

    //reading util the END OF THE FILE
    char c;
    while ((c = fgetc(file)) != EOF)
    {
        if (c == '+')
        {
            arr[index]++;
        }
        else if (c == '-')
        {
            arr[index]--;
        }
        else if (c == '>')
        {
            index++;
            index %= mem;
        }
        else if (c == '<')
        {
            index--;
            index %= mem;
        }
        else if (c == '.')
        {
            printf("%c", arr[index]);
        }
        else if (c == ',')
        {
            scanf("%c", &arr[index]);
        }
        else if (c == '[')
        {
            char temp = fgetc(file);
            int skip = 0;

            while (temp != ']' || skip != 0)
            {
                if (temp == '[')
                    skip++;
                if (temp == ']' && skip > 0)
                    skip--;

                temp = fgetc(file);
            }

            fseek(file, -1, SEEK_CUR);
        }
        else if (c == ']')
        {
            if (arr[index] != 0)
            {
                fseek(file, -2, SEEK_CUR);
                char temp = fgetc(file);

                int skip = 0;

                while (temp != '[' || skip != 0)
                {
                    if (temp == ']')
                        skip++;
                    if (temp == '[' && skip > 0)
                        skip--;

                    fseek(file, -2, SEEK_CUR);
                    temp = fgetc(file);
                }
            }
            else
            {
                continue;
            }
        }
    }

    fclose(file);
    return 0;
}

非常感谢您能帮我解决这个问题。

最佳答案

index 变为负数时,这段代码可能存在问题。

        index--;
        index %= mem;

% 运算符保留左侧参数的符号,因此 -1 % mem 是 –1,而不是您可能期望的 mem–1。

关于c - Brainfuck 解释器不运行某些代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59228814/

相关文章:

c - 在 C 中的循环/IF 结构外声明变量

python - 如何在 sublime text 2 中配置 python 解释器以使其充当 IDLE python shell

brainfuck - Brainfuck中数字的绝对值

c - 溢出与信息

c++ - gcc -O2 的奇怪整数行为

c++ - 将动态分配的二维字符数组传递给函数

python - 如何将制表符补全添加到 Python shell?

python - pycharm 看不到 python3.7 解释器

c - 这将更快地编译和/或计算斐波那契数列的前 100 个数字 : C or Brainfuck