Skip to content

Instantly share code, notes, and snippets.

@taiypeo
Created August 21, 2015 19:26
Show Gist options
  • Save taiypeo/f3fdd6d30e90ec6bcdbc to your computer and use it in GitHub Desktop.
Save taiypeo/f3fdd6d30e90ec6bcdbc to your computer and use it in GitHub Desktop.
Simple Brainfuck interpreter written in C++
#include <iostream>
#include <stack>
int main()
{
std::string program;
unsigned char tape[30000] = {0};
unsigned char* ptr = tape;
std::stack<int> loopStack;
std::cin>>program;
for(unsigned int commandIndex = 0; commandIndex < program.size(); commandIndex++)
{
switch(program[commandIndex])
{
case '+':
++*ptr;
break;
case '-':
--*ptr;
break;
case '>':
++ptr;
break;
case '<':
--ptr;
break;
case '.':
std::cout<<*ptr<<std::endl;
break;
case ',':
std::cin>>ptr;
break;
case '[':
loopStack.push(commandIndex);
break;
case ']':
if(*ptr == 0)
loopStack.pop();
else
commandIndex = loopStack.top();
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment