Resultados 1 a 3 de 3
  1. #1
    Moderador Avatar de _Guga_
    Data de Ingresso
    Apr 2006
    Localização
    Salvador - BA
    Posts
    2.118

    [source] interpretador de Brainfuck

    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
    Có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
    Interpreter.h
    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
    Có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);
    	}
    
    }
    main.cpp

    Có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;
    }
    abrassao =P


    I must not fear. Fear is the mind killer.

  2. #2
    Hacker Avatar de acpguedes
    Data de Ingresso
    Sep 2011
    Localização
    #!/usr/bin/env perl
    Posts
    955
    Citação Postado originalmente por _Guga_ Ver Post
    caso eu encontre um erro pedirei a algum mod pra editar o topico
    vc n é mod???
    So respondo se a consiencia perguntar!!!
    Não Respondo MP's de perguntas, as faça em um tópico!

    Perl User, Bioinformatcs Programmer!

  3. #3
    Hacker Avatar de acpguedes
    Data de Ingresso
    Sep 2011
    Localização
    #!/usr/bin/env perl
    Posts
    955
    putz.... agora q vi d quando é o Tópico... hahaha

    Eu me declaro vencedor da pá de ouro!!

    --> devo a vitoria ao guga que me passa os links

    ps: mals pelo flood
    So respondo se a consiencia perguntar!!!
    Não Respondo MP's de perguntas, as faça em um tópico!

    Perl User, Bioinformatcs Programmer!

Tópicos Similares

  1. [source] BrainFuck Compiler
    Por _Guga_ no fórum Programação
    Respostas: 17
    Último Post: 21 Apr 2012, 19:02
  2. Respostas: 2
    Último Post: 04 Jan 2010, 18:36
  3. [source] Pote de Mel! em C
    Por Djack no fórum C,C++
    Respostas: 14
    Último Post: 21 Mar 2007, 13:17

Permissões de Postagem

  • Você não pode iniciar novos tópicos
  • Você não pode enviar respostas
  • Você não pode enviar anexos
  • Você não pode editar suas mensagens
  •