Index: src/main.cc
===================================================================
--- src/main.cc	(revision 721f17ac6221a75230b4b1228917e6a163b53ff6)
+++ src/main.cc	(revision 09d789c493c816483fb88c49a900967b7addbd2c)
@@ -10,6 +10,6 @@
 // Created On       : Fri May 15 23:12:02 2015
 // Last Modified By : Peter A. Buhr
-// Last Modified On : Mon Jun 22 17:02:11 2015
-// Update Count     : 73
+// Last Modified On : Thu May 21 21:15:54 2015
+// Update Count     : 9
 //
 
@@ -51,30 +51,23 @@
 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,
-	bresolvep = false,
 	exprp = false,
 	expraltp = false,
 	grammarp = false,
 	libcfap = false,
-	nopreludep = false,
-	protop = false,
-	parsep = false,
 	resolvep = false,									// used in AlternativeFinder
 	symtabp = false,
+	parsep = false,
 	validp = false,
-	errorp = false,
-	codegenp = false;
-
-enum { Ast, Bresolver, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Validate, };
+	preludep = true,
+	protop = false,
+	codegenp = false,
+	errorp = false;
+
+enum { Ast, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Prototypes, Resolver, Symbol, Parse, };
 
 static struct option long_opts[] = {
 	{ "ast", no_argument, 0, Ast },
-	{ "before-resolver", no_argument, 0, Bresolver },
 	{ "expr", no_argument, 0, Expr },
 	{ "expralt", no_argument, 0, ExprAlt },
@@ -82,9 +75,8 @@
 	{ "libcfa", no_argument, 0, LibCFA },
 	{ "nopreamble", no_argument, 0, Nopreamble },
-	{ "parse", no_argument, 0, Parse },
 	{ "prototypes", no_argument, 0, Prototypes },
 	{ "resolver", no_argument, 0, Resolver },
 	{ "symbol", no_argument, 0, Symbol },
-	{ "validate", no_argument, 0, Validate },
+	{ "parse", no_argument, 0, Parse },
 	{ 0, 0, 0, 0 }
 };
@@ -99,5 +91,5 @@
 	
 	int c;
-	while ( (c = getopt_long( argc, argv, "abefglnpqrsvyzD:", long_opts, &long_index )) != -1 ) {
+	while ( (c = getopt_long( argc, argv, "aefglnpqrsxyzD:", long_opts, &long_index )) != -1 ) {
 		switch ( c ) {
 		  case Ast:
@@ -105,8 +97,4 @@
 			astp = true;
 			break;
-		  case Bresolver:
-		  case 'b':										// print before resolver steps
-			bresolvep = true;
-			break;
 		  case Expr:
 		  case 'e':										// dump AST after expression analysis
@@ -127,5 +115,5 @@
 		  case Nopreamble:
 		  case 'n':										// do not read preamble
-			nopreludep = true;
+			preludep = false;
 			break;
 		  case Prototypes:
@@ -145,5 +133,5 @@
 			symtabp = true;
 			break;
-		  case 'v':										// dump AST after decl validation pass
+		  case 'x':										// dump AST after decl validation pass
 			validp = true;
 			break;
@@ -165,5 +153,4 @@
 
 	try {
-		// choose to read the program from a file or stdin
 		if ( optind < argc ) {
 			input = fopen( argv[ optind ], "r" );
@@ -182,32 +169,64 @@
 	
 		Parser::get_parser().set_debug( grammarp );
-
-		// read in the builtins and the prelude
-		if ( ! nopreludep ) {							// include gcc builtins
-			FILE * builtins = fopen( CFA_LIBDIR "/builtins.cf", "r" );
+	
+		if ( preludep ) {								// include gcc builtins
+			FILE *builtins = fopen( CFA_LIBDIR "/builtins.cf", "r" );
 			if ( builtins == NULL ) {
-				std::cerr << "Error: can't open builtins" << std::endl;
+				std::cout << "Error: can't open builtins" << std::endl;
 				exit( 1 );
 			} // if
-
-			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
-		} // 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();
+			} // 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 ( libcfap ) {
-			parse( input, LinkageSpec::Intrinsic );	
-		} else {
-			parse( input, LinkageSpec::Cforall, grammarp );	
-		}
+			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 );
   
 		if ( parsep ) {
@@ -225,72 +244,97 @@
 		} // if
 
+		if ( expraltp ) {
+			SymTab::validate( translationUnit, false );
+			ResolvExpr::AlternativePrinter printer( std::cout );
+			acceptAll( translationUnit, printer );
+			return 0;
+		} // if
+
+		if ( symtabp ) {
+			SymTab::validate( translationUnit, true );
+			return 0;
+		} // if
+
+		if ( validp ) {
+			SymTab::validate( translationUnit, false );
+			printAll( translationUnit, std::cout );
+			return 0;
+		} // if
+
+		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
+
+		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
-		OPTPRINT( "tweak" )
 		InitTweak::tweak( translationUnit );
-		OPTPRINT( "validate" )
-		SymTab::validate( translationUnit, symtabp );
-		if ( symtabp ) {
-			return 0;
-		} // if
-
-		if ( expraltp ) {
-			ResolvExpr::AlternativePrinter printer( std::cout );
-			acceptAll( translationUnit, printer );
-			return 0;
-		} // if
-
-		if ( validp ) {
-			printAll( translationUnit, std::cout );
-			return 0;
-		} // if
-
-		OPTPRINT( "mutate" )
+
+		//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 );
-		OPTPRINT( "fixNames" ) 
+		//std::cerr << "before fixNames" << std::endl;
 		CodeGen::fixNames( translationUnit );
-
-		if ( libcfap ) {
-			protop = true;
-			// generate the bodies of cfa library functions
-			LibCfa::makeLibCfa( translationUnit );
-		} // if
-
-		if ( bresolvep ) {
-			printAll( translationUnit, std::cout );
-			return 0;
-		} // if
-
-		OPTPRINT( "resolve" )
+		//std::cerr << "before resolve" << std::endl;
 		ResolvExpr::resolve( translationUnit );
-		if ( exprp ) {
+		//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 );
+
+		CodeGen::generate( translationUnit, *output, protop );
+
+		if ( output != &std::cout ) {
+			delete output;
+		} // if
+
+	} catch ( SemanticError &e ) {
+		if ( errorp ) {
 			printAll( translationUnit, std::cout );
 		}
-
-		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 ) {
-			printAll( translationUnit, std::cout );
-			return 0;
-		} // if
-
-		CodeGen::generate( translationUnit, *output, protop );
-
-		if ( output != &std::cout ) {
-			delete output;
-		} // if
-	} catch ( SemanticError &e ) {
-		if ( errorp ) {
-			printAll( translationUnit, std::cerr );
-		}
-		e.print( std::cerr );
+		e.print( cout );
 		if ( output != &std::cout ) {
 			delete output;
@@ -315,14 +359,4 @@
 } // 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 //
