Changes in src/main.cc [de62360d:843054c2]
- File:
-
- 1 edited
-
src/main.cc (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/main.cc
rde62360d r843054c2 10 10 // Created On : Fri May 15 23:12:02 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Jun 22 17:02:11201513 // Update Count : 7312 // Last Modified On : Thu May 21 21:15:54 2015 13 // Update Count : 9 14 14 // 15 15 … … 51 51 using namespace std; 52 52 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 58 53 bool 59 54 astp = false, 60 bresolvep = false,61 55 exprp = false, 62 56 expraltp = false, 63 57 grammarp = false, 64 58 libcfap = false, 65 nopreludep = false,66 protop = false,67 parsep = false,68 59 resolvep = false, // used in AlternativeFinder 69 60 symtabp = false, 61 parsep = false, 70 62 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 68 enum { Ast, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Prototypes, Resolver, Symbol, Parse, }; 75 69 76 70 static struct option long_opts[] = { 77 71 { "ast", no_argument, 0, Ast }, 78 { "before-resolver", no_argument, 0, Bresolver },79 72 { "expr", no_argument, 0, Expr }, 80 73 { "expralt", no_argument, 0, ExprAlt }, … … 82 75 { "libcfa", no_argument, 0, LibCFA }, 83 76 { "nopreamble", no_argument, 0, Nopreamble }, 84 { "parse", no_argument, 0, Parse },85 77 { "prototypes", no_argument, 0, Prototypes }, 86 78 { "resolver", no_argument, 0, Resolver }, 87 79 { "symbol", no_argument, 0, Symbol }, 88 { " validate", no_argument, 0, Validate },80 { "parse", no_argument, 0, Parse }, 89 81 { 0, 0, 0, 0 } 90 82 }; … … 99 91 100 92 int c; 101 while ( (c = getopt_long( argc, argv, "a befglnpqrsvyzD:", long_opts, &long_index )) != -1 ) {93 while ( (c = getopt_long( argc, argv, "aefglnpqrsxyzD:", long_opts, &long_index )) != -1 ) { 102 94 switch ( c ) { 103 95 case Ast: … … 105 97 astp = true; 106 98 break; 107 case Bresolver:108 case 'b': // print before resolver steps109 bresolvep = true;110 break;111 99 case Expr: 112 100 case 'e': // dump AST after expression analysis … … 127 115 case Nopreamble: 128 116 case 'n': // do not read preamble 129 nopreludep = true;117 preludep = false; 130 118 break; 131 119 case Prototypes: … … 145 133 symtabp = true; 146 134 break; 147 case ' v': // dump AST after decl validation pass135 case 'x': // dump AST after decl validation pass 148 136 validp = true; 149 137 break; … … 165 153 166 154 try { 167 // choose to read the program from a file or stdin168 155 if ( optind < argc ) { 169 156 input = fopen( argv[ optind ], "r" ); … … 182 169 183 170 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" ); 188 174 if ( builtins == NULL ) { 189 std::c err<< "Error: can't open builtins" << std::endl;175 std::cout << "Error: can't open builtins" << std::endl; 190 176 exit( 1 ); 191 177 } // 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 207 207 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 ); 212 231 213 232 if ( parsep ) { … … 225 244 } // if 226 245 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 227 300 // add the assignment statement after the 228 301 // initialization of a type parameter 229 OPTPRINT( "tweak" )230 302 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; 249 310 ControlStruct::mutate( translationUnit ); 250 OPTPRINT( "fixNames" )311 //std::cerr << "before fixNames" << std::endl; 251 312 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; 265 314 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 ) { 267 336 printAll( translationUnit, std::cout ); 268 337 } 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 ); 295 339 if ( output != &std::cout ) { 296 340 delete output; … … 315 359 } // main 316 360 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 } // if325 }326 327 361 // Local Variables: // 328 362 // tab-width: 4 //
Note:
See TracChangeset
for help on using the changeset viewer.