Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main.cc

    rde62360d r843054c2  
    1010// Created On       : Fri May 15 23:12:02 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jun 22 17:02:11 2015
    13 // Update Count     : 73
     12// Last Modified On : Thu May 21 21:15:54 2015
     13// Update Count     : 9
    1414//
    1515
     
    5151using namespace std;
    5252
    53 #define OPTPRINT(x) \
    54         if ( errorp ) std::cerr << x << std::endl;
    55 
    56 void parse( FILE * input, LinkageSpec::Type t, bool shouldExit = false );
    57 
    5853bool
    5954        astp = false,
    60         bresolvep = false,
    6155        exprp = false,
    6256        expraltp = false,
    6357        grammarp = false,
    6458        libcfap = false,
    65         nopreludep = false,
    66         protop = false,
    67         parsep = false,
    6859        resolvep = false,                                                                       // used in AlternativeFinder
    6960        symtabp = false,
     61        parsep = false,
    7062        validp = false,
    71         errorp = false,
    72         codegenp = false;
    73 
    74 enum { Ast, Bresolver, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Validate, };
     63        preludep = true,
     64        protop = false,
     65        codegenp = false,
     66        errorp = false;
     67
     68enum { Ast, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Prototypes, Resolver, Symbol, Parse, };
    7569
    7670static struct option long_opts[] = {
    7771        { "ast", no_argument, 0, Ast },
    78         { "before-resolver", no_argument, 0, Bresolver },
    7972        { "expr", no_argument, 0, Expr },
    8073        { "expralt", no_argument, 0, ExprAlt },
     
    8275        { "libcfa", no_argument, 0, LibCFA },
    8376        { "nopreamble", no_argument, 0, Nopreamble },
    84         { "parse", no_argument, 0, Parse },
    8577        { "prototypes", no_argument, 0, Prototypes },
    8678        { "resolver", no_argument, 0, Resolver },
    8779        { "symbol", no_argument, 0, Symbol },
    88         { "validate", no_argument, 0, Validate },
     80        { "parse", no_argument, 0, Parse },
    8981        { 0, 0, 0, 0 }
    9082};
     
    9991       
    10092        int c;
    101         while ( (c = getopt_long( argc, argv, "abefglnpqrsvyzD:", long_opts, &long_index )) != -1 ) {
     93        while ( (c = getopt_long( argc, argv, "aefglnpqrsxyzD:", long_opts, &long_index )) != -1 ) {
    10294                switch ( c ) {
    10395                  case Ast:
     
    10597                        astp = true;
    10698                        break;
    107                   case Bresolver:
    108                   case 'b':                                                                             // print before resolver steps
    109                         bresolvep = true;
    110                         break;
    11199                  case Expr:
    112100                  case 'e':                                                                             // dump AST after expression analysis
     
    127115                  case Nopreamble:
    128116                  case 'n':                                                                             // do not read preamble
    129                         nopreludep = true;
     117                        preludep = false;
    130118                        break;
    131119                  case Prototypes:
     
    145133                        symtabp = true;
    146134                        break;
    147                   case 'v':                                                                             // dump AST after decl validation pass
     135                  case 'x':                                                                             // dump AST after decl validation pass
    148136                        validp = true;
    149137                        break;
     
    165153
    166154        try {
    167                 // choose to read the program from a file or stdin
    168155                if ( optind < argc ) {
    169156                        input = fopen( argv[ optind ], "r" );
     
    182169       
    183170                Parser::get_parser().set_debug( grammarp );
    184 
    185                 // read in the builtins and the prelude
    186                 if ( ! nopreludep ) {                                                   // include gcc builtins
    187                         FILE * builtins = fopen( CFA_LIBDIR "/builtins.cf", "r" );
     171       
     172                if ( preludep ) {                                                               // include gcc builtins
     173                        FILE *builtins = fopen( CFA_LIBDIR "/builtins.cf", "r" );
    188174                        if ( builtins == NULL ) {
    189                                 std::cerr << "Error: can't open builtins" << std::endl;
     175                                std::cout << "Error: can't open builtins" << std::endl;
    190176                                exit( 1 );
    191177                        } // if
    192 
    193                         parse( builtins, LinkageSpec::Compiler );
    194 
    195                         if ( ! libcfap ) {
    196                                 // read the prelude in, if we're not generating the cfa library
    197                                 FILE * prelude = fopen( CFA_LIBDIR "/prelude.cf", "r" );
    198                                 if ( prelude == NULL ) {
    199                                         std::cerr << "Error: can't open prelude" << std::endl;
    200                                         exit( 1 );
    201                                 } // if
    202                    
    203                     parse( prelude, LinkageSpec::Intrinsic );
    204                         } // if
    205                 } // if
    206 
     178         
     179                        Parser::get_parser().set_linkage( LinkageSpec::Compiler );
     180                        Parser::get_parser().parse( builtins );
     181       
     182                        if ( Parser::get_parser().get_parseStatus() != 0 ) {
     183                                return Parser::get_parser().get_parseStatus();
     184                        } // if
     185                        fclose( builtins );
     186
     187                        FILE *prelude;
     188                        if ( libcfap ) {                                                        // include cfa prelude
     189                                prelude = input;
     190                        } else {
     191                                prelude = fopen( CFA_LIBDIR "/prelude.cf", "r" );
     192                        } // if
     193                        if ( prelude == NULL ) {
     194                                std::cout << "Error: can't open prelude" << std::endl;
     195                                exit( 1 );
     196                        } // if
     197         
     198                        Parser::get_parser().set_linkage( LinkageSpec::Intrinsic );
     199                        Parser::get_parser().parse( prelude );
     200       
     201                        if ( Parser::get_parser().get_parseStatus() != 0 ) {
     202                                return Parser::get_parser().get_parseStatus();
     203                        } // if
     204                        fclose( prelude );
     205                } // if
     206       
    207207                if ( libcfap ) {
    208                         parse( input, LinkageSpec::Intrinsic );
    209                 } else {
    210                         parse( input, LinkageSpec::Cforall, grammarp );
    211                 }
     208                        std::list< Declaration* > translationUnit;
     209                        buildList( Parser::get_parser().get_parseTree(), translationUnit );
     210                        Parser::get_parser().freeTree();
     211                        SymTab::validate( translationUnit, false );
     212                        CodeGen::fixNames( translationUnit );
     213                        LibCfa::makeLibCfa( translationUnit );
     214                        ResolvExpr::resolve( translationUnit );
     215                        GenPoly::convertLvalue( translationUnit );
     216                        GenPoly::box( translationUnit );
     217                        CodeGen::generate( translationUnit, *output, true );
     218                        if ( output != &std::cout ) {
     219                                delete output;
     220                        } // if
     221                        return 0;
     222                } // if
     223       
     224                Parser::get_parser().set_linkage( LinkageSpec::Cforall );
     225 
     226                Parser::get_parser().parse( input );
     227                if ( grammarp || Parser::get_parser().get_parseStatus() != 0 ) {
     228                        return Parser::get_parser().get_parseStatus();
     229                } // if
     230                fclose( input );
    212231 
    213232                if ( parsep ) {
     
    225244                } // if
    226245
     246                if ( expraltp ) {
     247                        SymTab::validate( translationUnit, false );
     248                        ResolvExpr::AlternativePrinter printer( std::cout );
     249                        acceptAll( translationUnit, printer );
     250                        return 0;
     251                } // if
     252
     253                if ( symtabp ) {
     254                        SymTab::validate( translationUnit, true );
     255                        return 0;
     256                } // if
     257
     258                if ( validp ) {
     259                        SymTab::validate( translationUnit, false );
     260                        printAll( translationUnit, std::cout );
     261                        return 0;
     262                } // if
     263
     264                if ( exprp ) {
     265                        InitTweak::tweak( translationUnit );
     266                        SymTab::validate( translationUnit, false );
     267                        ControlStruct::mutate( translationUnit );
     268                        CodeGen::fixNames( translationUnit );
     269                        ResolvExpr::resolve( translationUnit );
     270                        printAll( translationUnit, std::cout );
     271                        return 0;
     272                } // if
     273
     274                if ( codegenp ) {
     275                        // print the tree right before code generation
     276                        cerr << "tweak" << endl;
     277                        InitTweak::tweak( translationUnit );
     278                        cerr << "validate" << endl;
     279                        SymTab::validate( translationUnit, false );
     280                        cerr << "mutate" << endl;
     281                        ControlStruct::mutate( translationUnit );
     282                        cerr << "fixNames" << endl;
     283                        CodeGen::fixNames( translationUnit );
     284                        cerr << "resolve" << endl;
     285                        ResolvExpr::resolve( translationUnit );
     286                        cerr << "copyParams" << endl;
     287                        GenPoly::copyParams( translationUnit );
     288                        cerr << "convertSpecializations" << endl;
     289                        GenPoly::convertSpecializations( translationUnit );
     290                        cerr << "convertLvalue" << endl;
     291                        GenPoly::convertLvalue( translationUnit );
     292                        cerr << "box" << endl;
     293                        GenPoly::box( translationUnit );
     294                        if ( errorp ) {
     295                                printAll( translationUnit, std::cout );
     296                        }
     297                        return 0;
     298                } // if
     299
    227300                // add the assignment statement after the
    228301                // initialization of a type parameter
    229                 OPTPRINT( "tweak" )
    230302                InitTweak::tweak( translationUnit );
    231                 OPTPRINT( "validate" )
    232                 SymTab::validate( translationUnit, symtabp );
    233                 if ( symtabp ) {
    234                         return 0;
    235                 } // if
    236 
    237                 if ( expraltp ) {
    238                         ResolvExpr::AlternativePrinter printer( std::cout );
    239                         acceptAll( translationUnit, printer );
    240                         return 0;
    241                 } // if
    242 
    243                 if ( validp ) {
    244                         printAll( translationUnit, std::cout );
    245                         return 0;
    246                 } // if
    247 
    248                 OPTPRINT( "mutate" )
     303
     304                //std::cerr << "before validate" << std::endl;
     305                SymTab::validate( translationUnit, false );
     306                //Try::visit( translationUnit );
     307                //Tuples::mutate( translationUnit );
     308                //InitTweak::mutate( translationUnit );
     309                //std::cerr << "before mutate" << std::endl;
    249310                ControlStruct::mutate( translationUnit );
    250                 OPTPRINT( "fixNames" )
     311                //std::cerr << "before fixNames" << std::endl;
    251312                CodeGen::fixNames( translationUnit );
    252 
    253                 if ( libcfap ) {
    254                         protop = true;
    255                         // generate the bodies of cfa library functions
    256                         LibCfa::makeLibCfa( translationUnit );
    257                 } // if
    258 
    259                 if ( bresolvep ) {
    260                         printAll( translationUnit, std::cout );
    261                         return 0;
    262                 } // if
    263 
    264                 OPTPRINT( "resolve" )
     313                //std::cerr << "before resolve" << std::endl;
    265314                ResolvExpr::resolve( translationUnit );
    266                 if ( exprp ) {
     315                //Tuples::checkFunctions( translationUnit );
     316                //        std::cerr << "Finished tuple checkfunctions" << std::endl;
     317                //printAll( translationUnit, std::cerr );
     318                //std::cerr << "before copyParams" << std::endl;
     319                GenPoly::copyParams( translationUnit );
     320                //std::cerr << "before convertSpecializations" << std::endl;
     321                GenPoly::convertSpecializations( translationUnit );
     322                //std::cerr << "before convertLvalue" << std::endl;
     323                GenPoly::convertLvalue( translationUnit );
     324                //std::cerr << "before box" << std::endl;
     325                GenPoly::box( translationUnit );
     326                //Tuples::mutate( translationUnit );
     327
     328                CodeGen::generate( translationUnit, *output, protop );
     329
     330                if ( output != &std::cout ) {
     331                        delete output;
     332                } // if
     333
     334        } catch ( SemanticError &e ) {
     335                if ( errorp ) {
    267336                        printAll( translationUnit, std::cout );
    268337                }
    269 
    270                 OPTPRINT( "copyParams" );
    271                 GenPoly::copyParams( translationUnit );
    272                 OPTPRINT( "convertSpecializations" )
    273                 GenPoly::convertSpecializations( translationUnit );             
    274                 OPTPRINT( "convertLvalue" )
    275                 GenPoly::convertLvalue( translationUnit );
    276                 OPTPRINT( "box" )
    277                 GenPoly::box( translationUnit );
    278 
    279     // print the tree right before code generation
    280                 if ( codegenp ) {
    281                         printAll( translationUnit, std::cout );
    282                         return 0;
    283                 } // if
    284 
    285                 CodeGen::generate( translationUnit, *output, protop );
    286 
    287                 if ( output != &std::cout ) {
    288                         delete output;
    289                 } // if
    290         } catch ( SemanticError &e ) {
    291                 if ( errorp ) {
    292                         printAll( translationUnit, std::cerr );
    293                 }
    294                 e.print( std::cerr );
     338                e.print( cout );
    295339                if ( output != &std::cout ) {
    296340                        delete output;
     
    315359} // main
    316360
    317 void parse( FILE * input, LinkageSpec::Type linkage, bool shouldExit ) {
    318         Parser::get_parser().set_linkage( linkage );
    319         Parser::get_parser().parse( input );
    320 
    321         fclose( input );
    322         if ( shouldExit || Parser::get_parser().get_parseStatus() != 0 ) {
    323                 exit( Parser::get_parser().get_parseStatus() );
    324         } // if
    325 }
    326 
    327361// Local Variables: //
    328362// tab-width: 4 //
Note: See TracChangeset for help on using the changeset viewer.