Index: src/main.cc
===================================================================
--- src/main.cc	(revision 5f2f2d75adcd9f673ef8bda86f25d409347089be)
+++ src/main.cc	(revision 81419b56599b36f14d08bf2cbd8c25893ee2f52b)
@@ -9,7 +9,7 @@
 // Author           : Richard C. Bilson
 // Created On       : Fri May 15 23:12:02 2015
-// Last Modified By : Peter A. Buhr
-// Last Modified On : Mon Jun  8 16:50:31 2015
-// Update Count     : 11
+// Last Modified By : Rob Schluntz
+// Last Modified On : Tue Jun 09 15:10:05 2015
+// Update Count     : 68
 //
 
@@ -51,4 +51,9 @@
 using namespace std;
 
+#define OPTPRINT(x) \
+	if ( errorp ) std::cerr << x << std::endl;
+
+void parse(FILE * input, LinkageSpec::Type t, bool shouldExit = false );
+
 bool
 	astp = false,
@@ -153,4 +158,5 @@
 
 	try {
+		// choose to read the program from a file or stdin
 		if ( optind < argc ) {
 			input = fopen( argv[ optind ], "r" );
@@ -169,64 +175,32 @@
 	
 		Parser::get_parser().set_debug( grammarp );
-	
+
+		// read in the builtins and the prelude
 		if ( preludep ) {								// include gcc builtins
-			FILE *builtins = fopen( CFA_LIBDIR "/builtins.cf", "r" );
+			FILE * builtins = fopen( CFA_LIBDIR "/builtins.cf", "r" );
 			if ( builtins == NULL ) {
-				std::cout << "Error: can't open builtins" << std::endl;
+				std::cerr << "Error: can't open builtins" << std::endl;
 				exit( 1 );
 			} // if
-	  
-			Parser::get_parser().set_linkage( LinkageSpec::Compiler );
-			Parser::get_parser().parse( builtins );
-	
-			if ( Parser::get_parser().get_parseStatus() != 0 ) {
-				return Parser::get_parser().get_parseStatus();
+
+			parse( builtins, LinkageSpec::Compiler );
+
+			if ( ! libcfap ) {
+				// read the prelude in, if we're not generating the cfa library
+				FILE * prelude = fopen( CFA_LIBDIR "/prelude.cf", "r" );
+				if ( prelude == NULL ) {
+					std::cerr << "Error: can't open prelude" << std::endl;
+					exit( 1 );
+				} // if
+		    
+		    parse( prelude, LinkageSpec::Intrinsic );
 			} // if
-			fclose( builtins );
-
-			FILE *prelude;
-			if ( libcfap ) {							// include cfa prelude
-				prelude = input;
-			} else {
-				prelude = fopen( CFA_LIBDIR "/prelude.cf", "r" );
-			} // if
-			if ( prelude == NULL ) {
-				std::cout << "Error: can't open prelude" << std::endl;
-				exit( 1 );
-			} // if
-	  
-			Parser::get_parser().set_linkage( LinkageSpec::Intrinsic );
-			Parser::get_parser().parse( prelude );
-	
-			if ( Parser::get_parser().get_parseStatus() != 0 ) {
-				return Parser::get_parser().get_parseStatus();
-			} // if
-			fclose( prelude );
-		} // if
-	
+		} // if
+
 		if ( libcfap ) {
-			std::list< Declaration* > translationUnit;
-			buildList( Parser::get_parser().get_parseTree(), translationUnit );
-			Parser::get_parser().freeTree();
-			SymTab::validate( translationUnit, false );
-			CodeGen::fixNames( translationUnit );
-			LibCfa::makeLibCfa( translationUnit );
-			ResolvExpr::resolve( translationUnit );
-			GenPoly::convertLvalue( translationUnit );
-			GenPoly::box( translationUnit );
-			CodeGen::generate( translationUnit, *output, true );
-			if ( output != &std::cout ) {
-				delete output;
-			} // if
-			return 0;
-		} // if
-	
-		Parser::get_parser().set_linkage( LinkageSpec::Cforall );
-  
-		Parser::get_parser().parse( input );
-		if ( grammarp || Parser::get_parser().get_parseStatus() != 0 ) {
-			return Parser::get_parser().get_parseStatus();
-		} // if
-		fclose( input );
+			parse( input, LinkageSpec::Intrinsic );	
+		} else {
+			parse( input, LinkageSpec::Cforall, grammarp );	
+		}
   
 		if ( parsep ) {
@@ -244,6 +218,15 @@
 		} // if
 
+		// add the assignment statement after the 
+		// initialization of a type parameter
+		OPTPRINT( "tweak" )
+		InitTweak::tweak( translationUnit );
+		OPTPRINT( "validate" )
+		SymTab::validate( translationUnit, symtabp );
+		if ( symtabp ) {
+			return 0;
+		} // if
+
 		if ( expraltp ) {
-			SymTab::validate( translationUnit, false );
 			ResolvExpr::AlternativePrinter printer( std::cout );
 			acceptAll( translationUnit, printer );
@@ -251,78 +234,40 @@
 		} // if
 
-		if ( symtabp ) {
-			SymTab::validate( translationUnit, true );
-			return 0;
-		} // if
-
 		if ( validp ) {
-			SymTab::validate( translationUnit, false );
 			printAll( translationUnit, std::cout );
 			return 0;
 		} // if
 
+		OPTPRINT( "mutate" )
+		ControlStruct::mutate( translationUnit );
+		OPTPRINT( "fixNames" ) 
+		CodeGen::fixNames( translationUnit );
+
+		if ( libcfap ) {
+			protop = true;
+			// generate the bodies of cfa library functions
+			LibCfa::makeLibCfa( translationUnit );
+		} // if
+
+		OPTPRINT( "resolve" )
+		ResolvExpr::resolve( translationUnit );
 		if ( exprp ) {
-			InitTweak::tweak( translationUnit );
-			SymTab::validate( translationUnit, false );
-			ControlStruct::mutate( translationUnit );
-			CodeGen::fixNames( translationUnit );
-			ResolvExpr::resolve( translationUnit );
 			printAll( translationUnit, std::cout );
-			return 0;
-		} // if
-
+		}
+
+		OPTPRINT( "copyParams" );
+		GenPoly::copyParams( translationUnit );
+		OPTPRINT( "convertSpecializations" )
+		GenPoly::convertSpecializations( translationUnit );		
+		OPTPRINT( "convertLvalue" )
+		GenPoly::convertLvalue( translationUnit );
+		OPTPRINT( "box" )
+		GenPoly::box( translationUnit );
+
+    // print the tree right before code generation
 		if ( codegenp ) {
-			// print the tree right before code generation
-			cerr << "tweak" << endl;
-			InitTweak::tweak( translationUnit );
-			cerr << "validate" << endl;
-			SymTab::validate( translationUnit, false );
-			cerr << "mutate" << endl;
-			ControlStruct::mutate( translationUnit );
-			cerr << "fixNames" << endl;
-			CodeGen::fixNames( translationUnit );
-			cerr << "resolve" << endl;
-			ResolvExpr::resolve( translationUnit );
-			cerr << "copyParams" << endl;
-			GenPoly::copyParams( translationUnit );
-			cerr << "convertSpecializations" << endl;
-			GenPoly::convertSpecializations( translationUnit );
-			cerr << "convertLvalue" << endl;
-			GenPoly::convertLvalue( translationUnit );
-			cerr << "box" << endl;
-			GenPoly::box( translationUnit );
-			if ( errorp ) {
-				printAll( translationUnit, std::cout );
-			}
-			return 0;
-		} // if
-
-		// add the assignment statement after the 
-		// initialization of a type parameter
-		InitTweak::tweak( translationUnit );
-
-		//std::cerr << "before validate" << std::endl;
-		SymTab::validate( translationUnit, false );
-		//Try::visit( translationUnit );
-		//Tuples::mutate( translationUnit );
-		//InitTweak::mutate( translationUnit );
-		//std::cerr << "before mutate" << std::endl;
-		ControlStruct::mutate( translationUnit );
-		//std::cerr << "before fixNames" << std::endl;
-		CodeGen::fixNames( translationUnit );
-		//std::cerr << "before resolve" << std::endl;
-		ResolvExpr::resolve( translationUnit );
-		//Tuples::checkFunctions( translationUnit );
-		//	  std::cerr << "Finished tuple checkfunctions" << std::endl;
-		//printAll( translationUnit, std::cerr );
-		//std::cerr << "before copyParams" << std::endl;
-		GenPoly::copyParams( translationUnit );
-		//std::cerr << "before convertSpecializations" << std::endl;
-		GenPoly::convertSpecializations( translationUnit );
-		//std::cerr << "before convertLvalue" << std::endl;
-		GenPoly::convertLvalue( translationUnit );
-		//std::cerr << "before box" << std::endl;
-		GenPoly::box( translationUnit );
-		//Tuples::mutate( translationUnit );
+			printAll( translationUnit, std::cout );
+			return 0;
+		} // if
 
 		CodeGen::generate( translationUnit, *output, protop );
@@ -334,7 +279,7 @@
 	} catch ( SemanticError &e ) {
 		if ( errorp ) {
-			printAll( translationUnit, std::cout );
+			printAll( translationUnit, std::cerr );
 		}
-		e.print( cout );
+		e.print( std::cerr );
 		if ( output != &std::cout ) {
 			delete output;
@@ -359,4 +304,14 @@
 } // main
 
+void parse(FILE * input, LinkageSpec::Type linkage, bool shouldExit) {
+	Parser::get_parser().set_linkage( linkage );
+	Parser::get_parser().parse( input );
+
+	fclose( input );
+	if ( shouldExit || Parser::get_parser().get_parseStatus() != 0 ) {
+		exit( Parser::get_parser().get_parseStatus() );
+	} // if
+}
+
 // Local Variables: //
 // tab-width: 4 //
