Witam.
Piszę interpreter brainfucka:
#include <iostream>
#include <fstream>
std::string read( std::fstream && file ) {
std::string temp;
std::string ret;
while( file >> temp ) ret += temp;
return ret;
}
int main( int argc, char ** argv ) {
if( argc == 1 ) {
std::cerr << "Enter path to the *.by file\nPress any key to exit...";
std::cin.get();
return 1;
}
std::fstream file( argv[ 1 ] );
if( !file ) {
std::cerr << "Unable to open file\nPress any key to exit...";
std::cin.get();
return 2;
}
char data[ 1000 ];
char * dp = data; //data pointer
std::string code = read( std::move( file ) );
size_t it = 0;
size_t nor = 0;
while( it < code.size() ) {
switch( code[ it ] ) {
case '>':
++dp;
break;
case '<':
--dp;
break;
case '+':
++( * dp );
break;
case '-':
++( * dp );
break;
case '.':
std::cout <<* dp;
break;
case ',':
std::cin >>* dp;
break;
case '[':
if( * dp == 0 ) {
int lc = 1; //loop counter
while( lc != 0 ) {
++it;
if( code[ it ] == '[' ) ++lc;
if( code[ it ] == ']' ) --lc;
}
}
else
nor = it;
break;
case ']':
it = nor - 1;
break;
default:
break;
}
++it;
}
std::cout << "\nExecution time: TODO\nPress any key to exit...";
std::cin.get();
}
Testuję na tym kodzie:
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
Ale wypisuje śmieci za każdym razem(i za każdym razem inne). Na przykład:
╝ÍŢŢÓń╦ÓŃÚ˝ňÍ
Proszę o pomoc i z góry dzięki.