ae pessoal, como prometi em alguns posts, desenvolvi um interpretador de brainfuck em C++, esta em fase de testes, por isso caso eu encontre um erro pedirei a algum mod pra editar o topico, por favor, testem e me reportem erros =P
download do .exe para testes (esta incluido uns .bf para testarem ok?)
http://rapidshare.com/files/376219028/bf.zip.html
ah, e claro, o source dele disponivel e livre pra all modificarem xD
Commands.h
Interpreter.hCódigo:#ifndef COMMANDS_H #define COMMANDS_H #include <new> #include <cstring> using namespace std; enum Token_e { _COMMENT_TOKEN, // a comment like this one _EOF, // End Of File _Plus, // The '+' Token _Minus, // The '-' Token _Great, // The '>' Token _Less, // The '<' Token _Comma, // The ',' Token _Dot, // The '.' Token _OpenBracket, // The '[' Token _CloseBracket // The ']' Token }; namespace { int* _Cell; } inline void initialize_internal() { _Cell = new(nothrow) int[256]; memset(_Cell, 0, 256); } #endif
Código:#ifndef INTERPRETER_H #define INTERPRETER_H #include "Commands.h" #include <string> #include <fstream> #include <stdexcept> #include <iostream> #include <deque> class Interpreter { private: ifstream inputfile; deque<Token_e> Runtime; string cmdLine; signed int cx; deque<Token_e>::iterator It; public: Interpreter() : cx(0) { } ~Interpreter() { Runtime.clear(); delete[] ::_Cell; } Token_e inferTokenType(char ch); void lexical(char** argv); protected: void ParseLine(string&); void Execute(); void instruction(Token_e inst); }; #endif
Interpreter.cpp
main.cppCódigo:#include "Interpreter.h" #include <conio.h> #include <cstdio> #include <algorithm> Token_e Interpreter::inferTokenType(char ch) { if( ch == '+' ) return _Plus; if( ch == '-' ) return _Minus; if( ch == '>' ) return _Great; if( ch == '<' ) return _Less; if( ch == ',' ) return _Comma; if( ch == '.' ) return _Dot; if( ch == '[' ) return _OpenBracket; if( ch == ']' ) return _CloseBracket; return _COMMENT_TOKEN; } void Interpreter::lexical(char** argv) { this->inputfile.open(argv[1], ios_base::in); if(inputfile.fail()) { inputfile.close(); throw runtime_error( "Could not open the file =\\" ); } while(!inputfile.eof()) { this->cmdLine.push_back(this->inputfile.get()); } inputfile.close(); initialize_internal(); this->ParseLine(cmdLine); It = Runtime.begin(); this->Execute(); } void Interpreter::ParseLine(string& cmdLine) { string::size_type count = 0; while( count < cmdLine.size() ) { char ch = cmdLine.at( count ); if( isspace( ch ) ) { count++; continue; } switch( this->inferTokenType( ch ) ) { case _Plus: { Runtime.push_back( _Plus ); break; } case _Minus: { Runtime.push_back( _Minus ); break; } case _Great: { Runtime.push_back( _Great ); break; } case _Less: { Runtime.push_back( _Less ); break; } case _Comma: { Runtime.push_back( _Comma ); break; } case _Dot: { Runtime.push_back( _Dot ); break; } case _OpenBracket: { Runtime.push_back( _OpenBracket ); break; } case _CloseBracket: { Runtime.push_back( _CloseBracket ); break; } case _COMMENT_TOKEN: { break; } } count++; } Runtime.push_back( _EOF ); } void Interpreter::instruction(Token_e inst) { switch( inst ) { case _Plus: { _Cell[this->cx] += 1; break; } case _Minus: { _Cell[this->cx] -= 1; break; } case _Great: { if( ++this->cx > 255 ) cx = 0; break; } case _Less: { if( --this->cx < 0 ) cx = 255; break; } case _Comma: { _Cell[this->cx] = static_cast<char>( getch() ); break; } case _Dot: { putchar( ::_Cell[this->cx] ); break; } case _OpenBracket: { It++; if( *It == _EOF ) throw runtime_error("End of file unexpected"); if( *It == _CloseBracket ) if( ::_Cell[this->cx] ) for(;;) {} else break; deque<Token_e>::iterator Ptr = It; while( ::_Cell[this->cx] ) { It = Ptr; while( *It != _CloseBracket) { this->instruction( *It ); } } break; } case _CloseBracket: { throw runtime_error("\']\' Token unexpected"); break; } case _EOF: { break; } } ++It; } void Interpreter::Execute() { for(;It != Runtime.end();) { this->instruction(*It); } }
abrassao =PCódigo:#include "Interpreter.h" #include <iostream> #include <stdexcept> using namespace std; int main(int argc, char* argv[]) { if( argc < 2 || argc > 2 ) { cerr << "BrainFuck Language Interpreter by Guga =D\n"; cerr << "Usage: " << argv[0] << " file.b"; exit(EXIT_FAILURE); } try { Interpreter obj; obj.lexical(argv); } catch(runtime_error& e) { cerr << "An Error Occurred: " << e.what() << flush; } return 0; }



Responder com Citação
