int main( int argc, char **argv ){

    //Will hold return value of compiler components.
    int ret;

    //initialize directives ds.
    d.l = d.a = d.v = 0;

    //Parse directives.
    parseDirectives( argc, argv );

    /*
     * Run scanner, parser, code generator, and VM components.
     * For each:
     * If the component didn't finish successfully, delete associated files based on
     * directives, and exit.
     */

    //Scanner.
    ret = system( "./LexicalAnalyzer" );

    if( ret < 0 || WEXITSTATUS(ret) != EXIT_SUCCESS ){

        if( d.l == 0 ){
            remove( "source_program.txt" );
            remove( "lexeme_table.txt" );
            remove( "lexeme_list.txt" );
        }

        exit(EXIT_FAILURE);
    }

    //Parser / Code Generator.
    ret = system( "./Parser-Code_Generator" );

    if( ret < 0 || WEXITSTATUS(ret) != EXIT_SUCCESS ){

        if( d.a == 0 ){
            remove( "assembly.txt" );
        }

        exit(EXIT_FAILURE);

    }

    //VM.
    ret = system( "./pmachine" );

    if( ret < 0 || WEXITSTATUS(ret) != EXIT_SUCCESS ){

        if( d.v == 0 ){
            remove( "output1.txt" );
            remove( "output2.txt" );
            remove( "output3.txt" );
        }

        exit(EXIT_FAILURE);

    }

    //Handle directives.
    if( d.l == 0 ){
        remove( "source_program.txt" );
        remove( "lexeme_table.txt" );
        remove( "lexeme_list.txt" );
    }

    if( d.a == 0 )
        remove( "assembly.txt" );

    if( d.v == 0 ){
        remove( "output1.txt" );
        remove( "output2.txt" );
    }

    //Exit successfully!
    exit( EXIT_SUCCESS );

    return 0;
}
Exemplo n.º 2
0
std::shared_ptr<PrototypeAST> Parser::parsePrototype() {

	lexer->getNextToken(); //Eat tokenn `def`

	if (lexer->currentToken != tok_id) {

		puts ("Expected identifier");
		return nullptr;

	}

	std::string name = lexer->identifier;
	lexer->getNextToken();

	puts (name.c_str());

	if (lexer->currentToken != '(') {

		puts ("Expected '('");
		return nullptr;

	}

	// Arguments

	auto args = parseArgs();

	if (!args)
		return nullptr;

	if (lexer->currentToken != ')') {

		puts ("Expected ')'");
		return nullptr;

	}

	// Type

	lexer->getNextToken();
	auto type = compiler->findType ("void");

	if (lexer->currentToken == tok_ra) {

		lexer->getNextToken();
		type = parseType();

		if (!type) {

			puts ("cant find type");
			puts (lexer->identifier.c_str());

			return nullptr;

		}

		lexer->getNextToken();

	}

	// Directives

	std::unique_ptr<DirectivesAST> directives = nullptr;

	if (lexer->currentToken == tok_dd)
		directives = parseDirectives();

	return std::make_shared<PrototypeAST> (name, std::move(args), std::move(directives), type);

}