Changeset 3b8e52c for src/main.cc


Ignore:
Timestamp:
Aug 17, 2016, 11:09:11 PM (8 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
1cb2282, 7a5d773
Parents:
926af74
Message:

more refactoring of parser code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main.cc

    r926af74 r3b8e52c  
    1010// Created On       : Fri May 15 23:12:02 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Aug 17 09:08:43 2016
    13 // Update Count     : 274
     12// Last Modified On : Wed Aug 17 22:13:38 2016
     13// Update Count     : 341
    1414//
    1515
     
    7070static void dump( std::list< Declaration * > & translationUnit, std::ostream & out = std::cout );
    7171
     72//************************************************
     73
     74#define __STRINGIFY__(str) #str
     75#define __VSTRINGIFY__(str) __STRINGIFY__(str)
     76#define assertf(expr, fmt, ...) ((expr) ? static_cast<void>(0) : __assert_fail_f(__VSTRINGIFY__(expr), __FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, ## __VA_ARGS__ ))
     77#define CFA_ASSERT_FMT "*CFA assertion error* from program \"%s\" in \"%s\" at line %d in file \"%s\": "
     78
     79extern const char * __progname;                                                 // global name of running executable (argv[0])
     80// called by macro assert in assert.h
     81void __assert_fail( const char *assertion, const char *file, unsigned int line, const char *function ) {
     82        fprintf( stderr, CFA_ASSERT_FMT, __progname, function, line, file );
     83        exit( EXIT_FAILURE );
     84}
     85
     86#include <cstdarg>
     87// called by macro assertf
     88void __assert_fail_f( const char *assertion, const char *file, unsigned int line, const char *function, const char *fmt, ... ) {
     89        fprintf( stderr, CFA_ASSERT_FMT, __progname, function, line, file );
     90        va_list args;
     91        va_start( args, fmt );
     92        vfprintf( stderr, fmt, args );
     93        exit( EXIT_FAILURE );
     94}
     95
     96//************************************************
    7297
    7398int main( int argc, char * argv[] ) {
    74         FILE * input;
     99        FILE * input;                                                                           // use FILE rather than istream because yyin is FILE
    75100        std::ostream *output = & std::cout;
    76101        std::list< Declaration * > translationUnit;
     
    81106        try {
    82107                // choose to read the program from a file or stdin
    83                 if ( optind < argc ) {
     108                if ( optind < argc ) {                                                  // any commands after the flags ? => input file name
    84109                        input = fopen( argv[ optind ], "r" );
    85                         if ( ! input ) {
    86                                 std::cout << "Error: cannot open " << argv[ optind ] << std::endl;
    87                                 exit( EXIT_FAILURE );
    88                         } // if
     110                        assertf( input, "cannot open %s\n", argv[ optind ] );
    89111                        // if running cfa-cpp directly, might forget to pass -F option (and really shouldn't have to)
    90112                        if ( filename == nullptr ) filename = argv[ optind ];
     
    92114                        if ( libcfap ) filename = "prelude.cf";
    93115                        optind += 1;
    94                 } else {
     116                } else {                                                                                // no input file name
    95117                        input = stdin;
    96118                        // if running cfa-cpp directly, might forget to pass -F option. Since this takes from stdin, pass
     
    99121                } // if
    100122
    101                 if ( optind < argc ) {
     123                if ( optind < argc ) {                                                  // any commands after the flags and input file ? => output file name
    102124                        output = new ofstream( argv[ optind ] );
    103125                } // if
     
    107129                        // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here.
    108130                        FILE * builtins = fopen( libcfap | treep ? "builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" );
    109                         if ( builtins == nullptr ) {
    110                                 std::cerr << "Error: cannot open builtins.cf" << std::endl;
    111                                 exit( EXIT_FAILURE );
    112                         } // if
     131                        assertf( builtins, "cannot open builtins.cf\n" );
    113132                        parse( builtins, LinkageSpec::Compiler );
    114133
    115134                        // read the extra prelude in, if not generating the cfa library
    116135                        FILE * extras = fopen( libcfap | treep ? "extras.cf" : CFA_LIBDIR "/extras.cf", "r" );
    117                         if ( extras == nullptr ) {
    118                                 std::cerr << "Error: cannot open extras.cf" << std::endl;
    119                                 exit( EXIT_FAILURE );
    120                         } // if
     136                        assertf( extras, "cannot open extras.cf\n" );
    121137                        parse( extras, LinkageSpec::C );
    122138
     
    124140                                // read the prelude in, if not generating the cfa library
    125141                                FILE * prelude = fopen( treep ? "prelude.cf" : CFA_LIBDIR "/prelude.cf", "r" );
    126                                 if ( prelude == nullptr ) {
    127                                         std::cerr << "Error: cannot open prelude.cf" << std::endl;
    128                                         exit( EXIT_FAILURE );
    129                                 } // if
    130 
     142                                assertf( prelude, "cannot open prelude.cf\n" );
    131143                                parse( prelude, LinkageSpec::Intrinsic );
    132144                        } // if
     
    355367                        break;
    356368                  case '?':
    357                         cout << "Unknown option: '" << (char)optopt << "'" << endl;
    358                         exit( EXIT_FAILURE );
     369                        assertf( false, "Unknown option: '%c'\n", (char)optopt );
    359370                  default:
    360371                        abort();
     
    397408} // dump
    398409
     410
     411
    399412// Local Variables: //
    400413// tab-width: 4 //
Note: See TracChangeset for help on using the changeset viewer.