Skip to content

Instantly share code, notes, and snippets.

@mb6ockatf
Created December 2, 2023 07:56
Show Gist options
  • Save mb6ockatf/ab374623a3b3d2011438dd2c491d1db7 to your computer and use it in GitHub Desktop.
Save mb6ockatf/ab374623a3b3d2011438dd2c491d1db7 to your computer and use it in GitHub Desktop.
cpp brainfuck interpreter
#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>
int main(int argc, char **argv)
{
std::fstream file(argv[1], std::ios::in);
std::istreambuf_iterator<char> fstart(file), fend;
std::vector<unsigned char> itape(fstart, fend);
file.close();
std::vector<unsigned char> mtape(30000, 0);
std::vector<unsigned char>::iterator m = mtape.begin();
std::vector<unsigned char>::iterator i = itape.begin();
int b = 0;
for (; i != itape.end(); ++i) {
switch (*i) {
case '>':
if (++m == mtape.end()) {
mtape.push_back(0);
m = --mtape.end();
}
break;
case '<':
--m;
break;
case '+':
++*m;
break;
case '-':
--*m;
break;
case '.':
std::cout << *m;
break;
case ',':
std::cin >> *m;
break;
case '[':
if (*m)
continue;
++b;
while (b)
switch (*++i) {
case '[':
++b;
break;
case ']':
--b;
break;
}
break;
case ']':
if (!*m)
continue;
++b;
while (b)
switch (*--i) {
case '[':
--b;
break;
case ']':
++b;
break;
}
--i;
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment