Created
August 29, 2016 17:12
-
-
Save njlr/bb05a21d5194a2061c1cae8a348261e8 to your computer and use it in GitHub Desktop.
Gist created by fiddle.jyt.io
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 <fstream> | |
#include <sys/stat.h> | |
#include <unistd.h> | |
using namespace std; | |
class Brainfuck { | |
public: | |
char data[30000]; | |
char *d; | |
const char *p; | |
Brainfuck(const char prog[]) { | |
d = data; | |
p = prog; | |
} | |
void pincr() { | |
d++; | |
} | |
void pdecr() { | |
d--; | |
} | |
void bincr() { | |
(*d)++; | |
} | |
void bdecr() { | |
(*d)--; | |
} | |
void puts() { | |
std::cout << *d; | |
} | |
void gets() { | |
std::cin >> *d; | |
} | |
void bropen() { | |
int bal = 1; | |
if (*d == '\0') { | |
do { | |
p++; | |
if (*p == '[') bal++; | |
else if (*p == ']') bal--; | |
} while ( bal != 0 ); | |
} | |
} | |
void brclose() { | |
int bal = 0; | |
do { | |
if (*p == '[') bal++; | |
else if (*p == ']') bal--; | |
p--; | |
} while ( bal != 0 ); | |
} | |
void evaluate() { | |
while (*p) { | |
switch (*p) { | |
case '>': | |
pincr(); | |
break; | |
case '<': | |
pdecr(); | |
break; | |
case '+': | |
bincr(); | |
break; | |
case '-': | |
bdecr(); | |
break; | |
case '.': | |
puts(); | |
break; | |
case ',': | |
gets(); | |
break; | |
case '[': | |
bropen(); | |
break; | |
case ']': | |
brclose(); | |
break; | |
} | |
p++; | |
} | |
} | |
}; | |
// auto helloWorld = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."; | |
// auto fibonacci = "+++++++++++>+>>>>++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>+<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-<-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<-]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<+>>[-]]<<<<<<<]>>>>>[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++++++++++++++++++++++++++++++++++++++++++++++.[-]<<<<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<[-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]"; | |
// Brainfuck(helloWorld).evaluate(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment