Ook?Ook!

CDW

0
Mitarbeiter
Eingereicht von Ook! ;) :
Schreibe einen Ook!-Parser!
Wikipedia dürfte die nötigen Erklärungen geben. Ook!-Code kann in Brainfuck-Code geparsed und dann in Plaintext. Ein weiteres Beispiel zum Thema "Teile und Herrsche" ...
Wer mag, kann natürlich direkt in Plaintext parsen, keine Frage!
http://de.wikipedia.org/wiki/Ook!
 
hi

edit: fehler ausgebessert, jetzt geht alles

Code:
/*
* Ook/Brainfuck Interpreter
* Usage:
* ookbf.exe -ook <ookfile> // Ook Interpreter(Ook to BF und dann interpretieren)
* ookbf.exe -bf <bffile> // BF Interpreter
* ookbf.exe -ooktobf <ookfile> <bffile/outputfile> // Ook to BF Converter
*/

#include <iostream>
#include <fstream>

extern "C" int getch();
extern "C" int putch(int);

using namespace std;

string loadfile(string FileName)
{
string filecontent="";
ifstream in;
in.open(FileName.c_str(),ios_base::in);
if(in.is_open())
{
do
{
filecontent+=in.get();
}
while(!in.eof());
}
in.close();
return filecontent;
}

string ooktobf(string ook)
{
string bf="";
string pre="";
string current="";
int i=0;
while(ook.length()>0)
{
if(ook.find("Ook?",0)==0)
{
ook.erase(0,((string)"Ook?").length());
pre=current;
current="Ook?";
i++;
}
else if(ook.find("Ook!",0)==0)
{
ook.erase(0,((string)"Ook!").length());
pre=current;
current="Ook!";
i++;
}
else if(ook.find("Ook.",0)==0)
{
ook.erase(0,((string)"Ook.").length());
pre=current;
current="Ook.";
i++;
}
else
{
ook.erase(0,1);
}
if(i==2)
{
i=0;
if((pre=="Ook?")&&(current=="Ook?"))
{
cout << "Ook to Brainfuck converter: Error: Ook? Ook? is not a valid command" << endl;
}
else if((pre=="Ook.")&&(current=="Ook."))
{
bf+='+';
}
else if((pre=="Ook!")&&(current=="Ook!"))
{
bf+='-';
}
else if((pre=="Ook.")&&(current=="Ook?"))
{
bf+='>';
}
else if((pre=="Ook?")&&(current=="Ook."))
{
bf+='<';
}
else if((pre=="Ook!")&&(current=="Ook?"))
{
bf+='[';
}
else if((pre=="Ook?")&&(current=="Ook!"))
{
bf+=']';
}
else if((pre=="Ook!")&&(current=="Ook."))
{
bf+='.';
}
else if((pre=="Ook.")&&(current=="Ook!"))
{
bf+=',';
}
}
}
return bf;
}

int countchar(string str,char c)
{
int i=0;
for(int i=0;i<str.length();i++)
{
if(str[i]==c)
{
i++;
}
}
return i;
}

void bf(string commands)
{
char c=0;
long long int *p=new long long int[65536];
int count=0;
int line=1, column=1;
if(countchar(commands,'[')!=countchar(commands,']'))
{
cout << "Brainfuck Interpreter: Error: Every [/] requires a matching ]/[" << endl;
return;
}
for(int i=0;i<65536;i++)
{
p[i]=0;
}
for(int i=0;i<commands.length();i++)
{
count++;
c=commands[i];
column++;
if(c=='.')
{
putch(*p);
}
else if(c==',')
{
(*p)=getch();
}
else if(c=='+')
{
(*p)++;
}
else if(c=='-')
{
(*p)--;
}
else if(c=='>')
{
p++;
}
else if(c=='<')
{
p--;
}
else if(c=='[')
{
if((*p)==0)
{
bool found=false;
int j=0;
for(int k=i+1;k<commands.length();k++)
{
if((commands[k]==']')&&(j==0))
{
i=k;
found=true;
break;
}
else if(commands[k]=='[')
{
j++;
}
else if(commands[k]==']')
{
j--;
}
}
if(!found)
{
cout << "Brainfuck Interpreter: Error: Matching ] not found. Line: " << line << " Column: " << column << " Character Number: " << i << endl;
return;
}
}
}
else if(c==']')
{
if((*p)!=0)
{
bool found=false;
int j=0;
for(int k=i-1;k>=0;k--)
{
if((commands[k]=='[')&&(j==0))
{
i=k;
found=true;
break;
}
else if(commands[k]==']')
{
j++;
}
else if(commands[k]=='[')
{
j--;
}
}
if(!found)
{
cout << "Brainfuck Interpreter: Error: Matching [ not found. Line: " << line << " Column: " << column << " Character Number: " << i << endl;
return;
}
}
}
else if(c=='n')
{
line++;
column=1;
}
}
}

int main(int argc, char *argv[])
{
bool usage=true;
for(int i=1;i<argc;i++)
{
if((string)argv[i]=="-bf")
{
bf(loadfile(argv[i+1]));
usage=false;
i++;
}
else if((string)argv[i]=="-ook")
{
bf(ooktobf(loadfile(argv[i+1])));
usage=false;
i++;
}
else if((string)argv[i]=="-ooktobf")
{
ofstream out;
out.open(argv[i+2],ios_base::out);
if(out.is_open())
{
out << ooktobf(loadfile(argv[i+1]));
}
out.close();
usage=false;
i+=2;
}
}
if(usage)
{
cout << "Usage:" << endl;
cout << argv[0] << " -ook <ookfile>" << endl;
cout << argv[0] << " -bf <bffile>" << endl;
cout << argv[0] << " -ooktobf <ookfile> <bffile/outputfile>" << endl;
}
return 0;
}

MfG Liar
 
Zurück
Oben