patterncppMinor
BrainFuck Interpreter in C++
Viewed 0 times
brainfuckinterpreterstackoverflow
Problem
Generic Headers
BF Interpreter
```
class BFInterpretor
{
static const std::size_t MemorySize = 10000;
std::vector memory;
std::vector openLoops;
BFProgram& program;
std::istream& input;
std::ostream& output;
std::size_t dataPointer;
std::size_t progPointer;
std::size_t scanToEndOfLoop()
{
std::size_t nestedLoops = 1;
std::size_t scanPos = progPointer;
for(;nestedLoops != 0;++scanPos)
{
switch(program[scanPos])
{
case '[': ++nestedLoops; break;
case ']': --nestedLoops; break;
default: break;
}
}
return scanPos;
}
public:
BFInterpretor(BFProgram& program, std::istream& input = std::cin, std::ostream& output = std::cout)
: memory(MemorySize)
, program(program)
, input(input)
, output(output)
, dataPointer(0)
, progPointer(0)
{}
static bool bfComment(char c)
{
switch(c)
{
case '>': case '': dataPointer = (dataPointer + 1) % MemorySize;break;
case '<': dataPointer = (dataPointer - 1) % MemorySize;break;
case '+': ++(memory[dataPointer]); break;
case '-': --(memory[dataPointer]); break;
case '.': output.put(memory[dataPointer]); break;
case ',': memory[dataPointer] = input.get(); break;
case '[': if (memory[dataPointer]) {
openLoops.push_back(progPointer);
}
#include
#include
#include
#include
typedef std::string const BFProgram;BF Interpreter
```
class BFInterpretor
{
static const std::size_t MemorySize = 10000;
std::vector memory;
std::vector openLoops;
BFProgram& program;
std::istream& input;
std::ostream& output;
std::size_t dataPointer;
std::size_t progPointer;
std::size_t scanToEndOfLoop()
{
std::size_t nestedLoops = 1;
std::size_t scanPos = progPointer;
for(;nestedLoops != 0;++scanPos)
{
switch(program[scanPos])
{
case '[': ++nestedLoops; break;
case ']': --nestedLoops; break;
default: break;
}
}
return scanPos;
}
public:
BFInterpretor(BFProgram& program, std::istream& input = std::cin, std::ostream& output = std::cout)
: memory(MemorySize)
, program(program)
, input(input)
, output(output)
, dataPointer(0)
, progPointer(0)
{}
static bool bfComment(char c)
{
switch(c)
{
case '>': case '': dataPointer = (dataPointer + 1) % MemorySize;break;
case '<': dataPointer = (dataPointer - 1) % MemorySize;break;
case '+': ++(memory[dataPointer]); break;
case '-': --(memory[dataPointer]); break;
case '.': output.put(memory[dataPointer]); break;
case ',': memory[dataPointer] = input.get(); break;
case '[': if (memory[dataPointer]) {
openLoops.push_back(progPointer);
}
Solution
-
An malformed program (with misbalanced brackets) may cause an UB (e.g. popping back from an empty vector; or running out of program space in
-
I recommend implementing
with an intention to reuse it in a debugger.
An malformed program (with misbalanced brackets) may cause an UB (e.g. popping back from an empty vector; or running out of program space in
scanToEndOfLoops).-
I recommend implementing
step method:void run() {
while (progPointer < program.size()) {
step();
}
}with an intention to reuse it in a debugger.
Code Snippets
void run() {
while (progPointer < program.size()) {
step();
}
}Context
StackExchange Code Review Q#84393, answer score: 4
Revisions (0)
No revisions yet.