Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm writing an interpreter in C++.

C++
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
    vector<int> tape(30000, 0); // tape with 30000 cells
    int ptr = 0;                // pointer to the current cell (memory address)
    int code_ptr = 0;           // pointer to current code instruction
    
    string code;
    getline(cin, code);

    while (code_ptr < code.size())
    {
        char instruction = code[code_ptr];

        // instructions
        // https://en.wikipedia.org/wiki/[redacted]Language_design
        switch (instruction)
        {
        case '>':   // increment cell pointer
            ++ptr;
            break;

        case '<':   // decrement cell pointer
            --ptr;
            break;

        case '+':   // increment byte at ptr cell
            ++tape[ptr];
            break;

        case '-':   // decrement byte at ptr cell
            --tape[ptr];
            break;

        case '.':   // print byte at ptr cell
            cout << (char)tape[ptr];
            break;

        case ',':   // accept one byte of input, store it's value at ptr cell
            tape[ptr] = cin.get();
            break;

        case '[':   // start a loop
            if (tape[ptr] == 0)
            {
                int loop_count = 1;
                while (loop_count > 0)
                {
                    ++code_ptr;
                    if (code[code_ptr] == '[')
                        ++loop_count;
                    else if (code[code_ptr] == ']')
                        --loop_count;
                }
            }
            break;

        case ']':   // end a loop
            if (code[code_ptr] != 0)
            {
                int loop_count = 1;
                while (loop_count > 0)
                {
                    --code_ptr;
                    if (code[code_ptr] == '[')
                        --loop_count;
                    else if (code[code_ptr] == ']')
                        ++loop_count;
                }
            }
            break;
        
        default:
            cout << "\nUnrecognized character: " << code[code_ptr];
            break;
        }

        ++code_ptr;
    }

    return 0;
}


What I have tried:

When I run the program and type the following:
>++++++++[<+++++++++>-]<.

(which should print uppercase 'H')
it gets stuck in an infinite loop without outputting any characters or errors.
Posted
Updated 24-Aug-23 6:22am
v2
Comments
Richard MacCutchan 24-Aug-23 12:23pm    
Use the debugger to trace where it is going wrong.
Dave Kreskowiak 24-Aug-23 13:15pm    
This is where the debugger is going to show you what's happening. Your code is not work as you expect, but it's doing exactly what you told it to do. The debugger is there to debug YOUR understanding of the code, allows you to understand better, and make changes. Use it.

You're right, you have an infinite loop. When you get to a ']', you go back to the matching '[', and repeat, indefinitely. There's no exit condition, nor is it clear what the exit condition should be. Should the interpreter loop only once? Or 10 times? Or until some cell becomes zero or non zero?
 
Share this answer
 
Comments
ViperTheGuy 24-Aug-23 14:18pm    
Hi, I found what was the problem. yes indeed it was with ']', check my answer.
The problem was in line 64, here:
C++
if (code[code_ptr] != 0)
            {

instead of checking for code[code_ptr], it should check for the value of current cell, which is tape[ptr] not current instruction.
C++
if (tape[ptr] != 0)
            {
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900