Skip to content

Instantly share code, notes, and snippets.

@njlr
Created August 29, 2016 17:12
Show Gist options
  • Save njlr/bb05a21d5194a2061c1cae8a348261e8 to your computer and use it in GitHub Desktop.
Save njlr/bb05a21d5194a2061c1cae8a348261e8 to your computer and use it in GitHub Desktop.
Gist created by fiddle.jyt.io
#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