008
14.06.2004, 14:09 Uhr
virtual
Sexiest Bit alive (Operator)
|
@FloSoft Ein Compiler kann da aber Abhilfe schaffen: Folgendes Mittagspausenprogramm generiert aus der BF Datei eine C Datei, welche man dann durch einen C Compiler jagen kann. Sollte Deine Performancebedenken zumindest teilweise ausräumen....
C++: |
#include <fstream> #include <iostream> #include <string> #include <cstdlib> #include <iterator> #include <list> #include <algorithm> #include <cctype>
const unsigned DATA_SPACE = 32768; const unsigned PRG_SPACE = 32768;
int main(int argc, char** argv) { std::string strPrgname = *argv++; std::string::size_type delimit = strPrgname.find_last_of("/\\"); if (delimit!=std::string::npos) strPrgname = strPrgname.substr(delimit+1);
if (argc!=3) { std::cerr<<"error: "<<strPrgname<<": illegal command line."<<std::endl <<"usage: "<<strPrgname<<" <infile> <outfile>"<<std::endl; std::exit(1); } std::string inFilename = *argv++; std::string outFilename = *argv;
std::list<char> bfSource; std::ifstream inFile(inFilename.c_str()); std::copy(std::istream_iterator<char>(inFile), std::istream_iterator<char>(), std::back_inserter(bfSource)); bfSource.remove_if(std::isspace); if (bfSource.size()>PRG_SPACE) { std::cerr<<"error: "<<strPrgname<<": input too long."<<std::endl; std::exit(2); }
std::ofstream outFile(outFilename.c_str()); outFile<<"#include <stdio.h>"<<std::endl <<std::endl <<"char data["<<DATA_SPACE<<"];"<<std::endl <<std::endl <<"int main(void)"<<std::endl <<"{"<<std::endl <<"\tchar* p = data;"<<std::endl;
std::list<char>::const_iterator i = bfSource.begin(); std::string indent = "\t"; int level = 0; unsigned pos = 1; while (i != bfSource.end()) { int count = 1; char token = *i++; while (i!=bfSource.end() && *i==token) { ++count; ++i; } switch(token) { case '+': if (count>1) outFile<<indent<<"*p += "<<count<<";"<<std::endl; else outFile<<indent<<"++*p;"<<std::endl; break; case '-': if (count>1) outFile<<indent<<"*p -= "<<count<<";"<<std::endl; else outFile<<indent<<"--*p;"<<std::endl; break; case '<': if (count>1) outFile<<indent<<"p -= "<<count<<";"<<std::endl; else outFile<<indent<<"--p;"<<std::endl; break; case '>': if (count>1) outFile<<indent<<"p += "<<count<<";"<<std::endl; else outFile<<indent<<"++p;"<<std::endl; break; case '.': for(int j=0; j<count; ++j) outFile<<indent<<"putchar(*p);"<<std::endl; break; case ',': if (count>1) std::cerr<<"warning: "<<strPrgname<<": "<<inFilename<<":"<<":"<<(pos+1)<<": dead code."<<std::endl; outFile<<indent<<"*p = getchar();"<<std::endl; break; case '[': for(int j=0; j<count; ++j) { outFile<<indent<<"while(*p)"<<std::endl; outFile<<indent<<"{"<<std::endl; ++level; indent += "\t"; } break; case ']': for(int j=0; j<count; ++j) { if (level-- == 0) { std::cerr<<"error: "<<strPrgname<<": "<<inFilename<<":"<<":"<<(pos+1)<<": unbalanced []."<<std::endl; std::exit(3); } indent = indent.substr(0, indent.length()-1); outFile<<indent<<"}"<<std::endl; } break; default: std::cerr<<"error: "<<strPrgname<<": "<<inFilename<<":"<<":"<<(pos+1)<<": unrecognized token \""<<token<<"\"."<<std::endl; std::exit(4);
} pos += count; }
if (level!=0) { std::cerr<<"error: "<<strPrgname<<": "<<inFilename<<":"<<(pos+1)<<": unexpected end of file."<<std::endl; std::exit(5); } outFile<<indent<<"}"<<std::endl; outFile.close();
std::exit(0); }
|
Brainfuck rulez -- Gruß, virtual Quote of the Month Ich eß' nur was ein Gesicht hat (Creme 21) Dieser Post wurde am 14.06.2004 um 14:13 Uhr von virtual editiert. |