Skip to content

Instantly share code, notes, and snippets.

@enedil
Created February 16, 2015 18:09
Show Gist options
  • Save enedil/2ba663d3613cb6379b72 to your computer and use it in GitHub Desktop.
Save enedil/2ba663d3613cb6379b72 to your computer and use it in GitHub Desktop.
#include <fstream>
#include <iostream>
#include <string>
const std::string head = "#include <cstdio>\n#include <vector>\nstd::vector<short> v(30000);\nshort* p = &(v[0]);\nint main() {\n";
const std::string end = "}";
int main(int argc, char* argv[])
{
std::string line;
std::ifstream bfsourcef(argv[1]);
std::string bfsource = "";
std::string bfCleanedUp = "";
std::string cppsource = head;
if (bfsourcef.is_open()) {
while (std::getline(bfsourcef, line)) {
bfsource.append(line);
}
bfsourcef.close();
}
for (int i = 0; i < bfsource.size(); ++i) {
switch(bfsource[i]) {
case '<':
case '>':
case '+':
case '-':
case '[':
case ']':
case ',':
case '.':
bfCleanedUp.append(bfsource.substr(i, 1));
}
}
int brace = 0;
for (int i = 0; i < bfCleanedUp.size(); ++i) {
if (bfCleanedUp[i] == '[') {
++brace;
}
else if (bfCleanedUp[i] == ']') {
--brace;
}
}
if (brace != 0) {
if (brace > 0) {
std::cerr << "Missing closing brackets! ";
}
else {
std::cerr << "Missing opening brackets! ";
}
std::cerr << "Terminating compilation!\n";
return 1;
}
for (int i = 0; i < bfCleanedUp.size(); ++i) {
switch (bfCleanedUp[i]) {
case '<':
cppsource.append("--p;");
break;
case '>':
cppsource.append("++p;");
break;
case '+':
cppsource.append("++(*p);");
break;
case '-':
cppsource.append("--(*p);");
break;
case '[':
cppsource.append("while(*p) {");
break;
case ']':
cppsource.append("}");
break;
case ',':
cppsource.append("*p = std::getchar();");
break;
case '.':
cppsource.append("putchar(*p);");
break;
}
//cppsource.append("\n");
}
cppsource.append(end);
std::cout << cppsource << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment