-1

I am trying to make a brainf*** interpreter in c++. when I test it with the Esolang hello world example:

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

is is here: https://esolangs.org/wiki/Brainfuck

it does not print anything for whatever reason. the period operator works, Ive tested it alone, but programs that work on proper brainf*** interpreters don't work on mine. Can anyone find the problem here?

PS: file_str::read(file); is a custom method I implemented which works. just to let you know.

this is my code so far.

    // Read File
    std::string file_data;
    file_data = file_str::read(file);

    // Array
    int array[30000];

    bool running = true;

    // pos
    int pos = 0;
    int pos_in_stack = 0;

    // Handle brackets
    std::vector<int> brackets;


    // Main Loop
    while(running)
    {

        if(pos > file_data.size()-1)
        {
            break;
        }

        // check what the curren command is and execute
        if(file_data.at(pos) == '>')
        {
            pos_in_stack++;
            pos++;
            continue;
        }

        if(file_data.at(pos) == '<')
        {
            pos_in_stack--;
            pos++;
            continue;
        }

        if(file_data.at(pos) == '+')
        {
            array[pos_in_stack]++;
            pos++;
            continue;
        }

        if(file_data.at(pos) == '-')
        {
            array[pos_in_stack]--;
            pos++;
            continue;
        }

        if(file_data.at(pos) == '.')
        {
            char a = array[pos_in_stack];
            std::cout << a;
            pos++;
            continue;
        }

        if(file_data.at(pos) == ',')
        {
            std::string a;
            std::cin >> a;
            char b = a.at(0);
            array[pos_in_stack] = b;
            pos++;
            continue;
        }

        if(file_data.at(pos) == '[')
        {
            if(array[pos_in_stack] != 0)
            {
                brackets.push_back(pos);
                pos++;
                continue;
            }


            // find coresponding bracket
            else{
                int brackets_looking_for = 1;
                while (brackets_looking_for != 0)
                {
                    pos++;


                    if(file_data.at(pos) == '[')
                    {
                        brackets_looking_for++;
                    }

                    if(file_data.at(pos) == ']')
                    {
                        brackets_looking_for--;
                    }

               }
               continue;
           }
        } 

        if(file_data.at(pos) == ']')
        {
            if(array[pos_in_stack] != 0)
            {
                pos = brackets.at(brackets.size()-1);
                continue;
            }


            brackets.pop_back();
            pos++;
            continue;
        } 

    }

    return 0;

}
Ed May
  • 33
  • 1
  • 7
  • And yes, above this snippet of code I use all of the necessary includes. – Ed May Oct 09 '21 at 16:55
  • And im relatively new to c++, so any advice is welcome! (my code is pretty bad btw) – Ed May Oct 09 '21 at 17:04
  • your `array` seems uninitialized – numzero Oct 09 '21 at 17:32
  • @EdMay Have a look at how to make a [mcve]. – super Oct 09 '21 at 17:51
  • The code accesses `array` out of bounds. – Eljay Oct 09 '21 at 17:52
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Oct 12 '21 at 13:14
  • The ',' command needs to read one character, not read a string and throw out all but the first character. – Daniel Cristofani Oct 15 '21 at 12:55
  • 1
    If I have this right, your '[' jumps to the matching ']' and not past it, on a 0? Small time waste. And anyway, the best thing is to match brackets before execution, store all the matches, and jump directly to matching bracket during execution. Faster and cleaner, and it lets you recognize mismatched brackets and report them as a syntax error right away. An example in C: http://brainfuck.org/sbi.c – Daniel Cristofani Oct 15 '21 at 13:10
  • 1
    One more thing, you mean pos_in_array, right? The array isn't a stack. – Daniel Cristofani Oct 15 '21 at 13:12

1 Answers1

1

Thanks for your help everyone, but I found the solution.

I need to change this:

pos = brackets.at(brackets.size()-1);

to this:

pos = brackets.at(brackets.size()-1)+1;
Ed May
  • 33
  • 1
  • 7