Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#include<iostream>
#include<vector>
#include<string>
int main(){
std::vector<char> memory(10000, 0);
int pointer = 0;
int loop = 0;
while(true){
int o_pointer = 0;
std::string brain_code;
std::cout << ">>";
std::cin >> brain_code;
while(o_pointer < brain_code.length()){
switch(brain_code[o_pointer]){
case '>':
pointer++;
break;
case '<':
pointer--;
break;
case '+':
memory[pointer]++;
break;
case '-':
memory[pointer]--;
break;
case '.':
std::cout << memory[pointer];
break;
case ',':
memory[pointer] = std::cin.get();
break;
case '[':
if(memory[pointer] == 0){
o_pointer++;
while(loop > 0 || brain_code[o_pointer] != ']'){
if(brain_code[o_pointer] == '[') loop++;
if(brain_code[o_pointer] == ']') loop--;
o_pointer++;
}
}
break;
case ']':
if(memory[pointer] != 0){
o_pointer--;
while(loop > 0 || brain_code[o_pointer] != '['){
if(brain_code[o_pointer] == '[') loop--;
if(brain_code[o_pointer] == ']') loop++;
o_pointer--;
}
o_pointer--;
}
break;
}
o_pointer++;
}
std::cout << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment