Created
August 21, 2015 19:26
-
-
Save taiypeo/f3fdd6d30e90ec6bcdbc to your computer and use it in GitHub Desktop.
Simple Brainfuck interpreter written in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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