Index: driver/cfa.cc
===================================================================
--- driver/cfa.cc	(revision d4778a6e9b648e3e3f98c72c55e110ea16df1cc9)
+++ driver/cfa.cc	(revision b1a6d6b5ecf5925aa65742a37eb5caaa0b59cb43)
@@ -294,4 +294,6 @@
 	args[nargs] = "-Wno-deprecated"; 
 	nargs += 1;
+	args[nargs] = "--std=c99";
+	nargs += 1;
 	args[nargs] = ( *new string( string("-B") + Bprefix + "/" ) ).c_str();
 	nargs += 1;
Index: translator/ControlStruct/Mutate.cc
===================================================================
--- translator/ControlStruct/Mutate.cc	(revision d4778a6e9b648e3e3f98c72c55e110ea16df1cc9)
+++ translator/ControlStruct/Mutate.cc	(revision b1a6d6b5ecf5925aa65742a37eb5caaa0b59cb43)
@@ -21,12 +21,12 @@
 namespace ControlStruct {
     void mutate( std::list< Declaration * > translationUnit ) {
+	// ForExprMutator formut;
+	LabelFixer lfix;
 	ChooseMutator chmut;
-	ForExprMutator formut;
 	CaseRangeMutator ranges;  // has to run after ChooseMutator
-	LabelFixer lfix;
 	//ExceptMutator exc;
-	LabelTypeChecker lbl;
+	// LabelTypeChecker lbl;
 
-	mutateAll( translationUnit, formut );
+	// mutateAll( translationUnit, formut );
 	acceptAll( translationUnit, lfix );
 	mutateAll( translationUnit, chmut );
Index: translator/GenPoly/Box.cc
===================================================================
--- translator/GenPoly/Box.cc	(revision d4778a6e9b648e3e3f98c72c55e110ea16df1cc9)
+++ translator/GenPoly/Box.cc	(revision b1a6d6b5ecf5925aa65742a37eb5caaa0b59cb43)
@@ -7,4 +7,5 @@
 
 #include <set>
+#include <stack>
 #include <string>
 #include <iterator>
@@ -50,4 +51,5 @@
 	    virtual Type *mutate( FunctionType *pointerType );
   
+	    virtual void doBeginScope();
 	    virtual void doEndScope();
 	  private:
@@ -66,5 +68,6 @@
   
 	    std::map< std::string, DeclarationWithType *> assignOps;
-	    std::map< std::string, FunctionDecl *> adapters;
+	    typedef std::map< std::string, FunctionDecl *> AdapterMap;
+	    std::stack< AdapterMap > adapters;
 	    DeclarationWithType *retval;
 	    bool useRetval;
@@ -126,4 +129,8 @@
 
     namespace {
+	std::string makeAdapterName( const std::string &mangleName ) {
+	    return "_adapter" + mangleName;
+	}
+
 	bool isPolyRet( FunctionType *function, std::string &name, const TyVarMap &otherTyVars ) {
 	    bool doTransform = false;
@@ -219,5 +226,5 @@
 		retval = oldRetval;
 		useRetval = oldUseRetval;
-		doEndScope();
+		// doEndScope();
 	    } // if
 	    return functionDecl;
@@ -316,5 +323,5 @@
 	    Type *concrete = env->lookup( typeName );
 	    if ( concrete == 0 ) {
-		throw SemanticError( "Unbound type variable " + typeName + " in", appExpr );
+		throw SemanticError( "Unbound type variable " + typeName + " in ", appExpr );
 	    } // if
 	    return addRetParam( appExpr, function, concrete, arg );
@@ -326,6 +333,9 @@
 		ret = addRetParam( appExpr, function, function->get_returnVals().front()->get_type(), arg );
 	    } // if
+	    std::string mangleName = SymTab::Mangler::mangle( function );
+	    std::string adapterName = makeAdapterName( mangleName );
+
 	    appExpr->get_args().push_front( appExpr->get_function() );
-	    appExpr->set_function( new NameExpr( "_adapter" + SymTab::Mangler::mangle( function ) ) );
+	    appExpr->set_function( new NameExpr( adapterName ) );
   
 	    return ret;
@@ -337,5 +347,9 @@
 	    TypeInstType *typeInst = dynamic_cast< TypeInstType *>( param );
 	    if ( typeInst && exprTyVars.find( typeInst->get_name() ) != exprTyVars.end() ) {
-		if ( arg->get_results().front()->get_isLvalue() ) {
+	        if ( dynamic_cast< TypeInstType *>( arg->get_results().front() ) ) {
+	            // if the argument's type is a type parameter, we don't need to box again!
+	            return;
+	        } else if ( arg->get_results().front()->get_isLvalue() ) {
+	            // VariableExpr and MemberExpr are lvalues
 		    arg = new AddressExpr( arg );
 		} else {
@@ -371,5 +385,5 @@
 	    for ( std::list< DeclarationWithType *>::const_iterator param = function->get_parameters().begin(); param != function->get_parameters().end(); ++param, ++arg ) {
 ///     std::cout << "parameter is ";
-///     (*param)->print( std::cout );
+///     (*param)->print( std::fcout );
 ///     std::cout << std::endl << "argument is ";
 ///     (*arg)->print( std::cout );
@@ -448,4 +462,6 @@
 	}
 
+
+
 	FunctionDecl *Pass1::makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ) {
 	    FunctionType *adapterType = makeAdapterType( adaptee, tyVars );
@@ -496,5 +512,6 @@
 	    CompoundStmt *adapterBody = new CompoundStmt( noLabels );
 	    adapterBody->get_kids().push_back( bodyStmt );
-	    return new FunctionDecl( "_adapter" + mangleName, Declaration::NoStorageClass, LinkageSpec::C, adapterType, adapterBody, false );
+	    std::string adapterName = makeAdapterName( mangleName );
+	    return new FunctionDecl( adapterName, Declaration::NoStorageClass, LinkageSpec::C, adapterType, adapterBody, false );
 	}
 
@@ -515,9 +532,18 @@
 		assert( env );
 		env->apply( realFunction );
+
 		std::string mangleName = SymTab::Mangler::mangle( realFunction );
 		if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
-		    std::map< std::string, FunctionDecl *>::iterator adapter = adapters.find( mangleName );
-		    if ( adapter == adapters.end() ) {
-			FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars );
+		    AdapterMap & adapters = Pass1::adapters.top();
+		    AdapterMap::iterator adapter = adapters.find( mangleName );
+
+		    if ( needsAdapter( realFunction, exprTyVars, true ) ) {
+		    	// the function still contains type variables, which means we are in a polymorphic
+		    	// context and the adapter function is a parameter - call the parameter and don't 
+		    	// create a new adapter.
+		        appExpr->get_args().push_front( new NameExpr( makeAdapterName ( mangleName ) ) );
+		        continue;
+		    } else if ( adapter == adapters.end() ) {
+		    	FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars );
 			adapter = adapters.insert( adapters.begin(), std::pair< std::string, FunctionDecl *>( mangleName, newAdapter ) );
 			stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) );
@@ -525,4 +551,5 @@
 		    assert( adapter != adapters.end() );
 		    appExpr->get_args().push_front( new VariableExpr( adapter->second ) );
+		    // appExpr->get_args().push_front( new NameExpr( makeAdapterName ( mangleName ) ) );
 		    adaptersDone.insert( adaptersDone.begin(), mangleName );
 		} // if
@@ -845,6 +872,10 @@
 	}
 
+	void Pass1::doBeginScope() {
+	    adapters.push(AdapterMap());
+	}
+
 	void Pass1::doEndScope() {
-	    adapters.clear();
+	    adapters.pop();
 	}
 
@@ -865,5 +896,6 @@
 		std::string mangleName = SymTab::Mangler::mangle( *funType );
 		if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
-		    paramList.push_front( new ObjectDecl( "_adapter" + mangleName, Declaration::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) );
+		    std::string adapterName = makeAdapterName( mangleName );
+		    paramList.push_front( new ObjectDecl( adapterName, Declaration::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) );
 		    adaptersDone.insert( adaptersDone.begin(), mangleName );
 		}
Index: translator/GenPoly/GenPoly.cc
===================================================================
--- translator/GenPoly/GenPoly.cc	(revision d4778a6e9b648e3e3f98c72c55e110ea16df1cc9)
+++ translator/GenPoly/GenPoly.cc	(revision b1a6d6b5ecf5925aa65742a37eb5caaa0b59cb43)
@@ -9,9 +9,20 @@
 #include "SynTree/Type.h"
 
+#include <iostream>
+using namespace std;
 
 namespace GenPoly {
 
+// interface functions
+bool isPolyVal( Type *type, const TyVarMap &tyVars ) {
+  return isPolyVal( type, tyVars, false );
+}
+
+bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) {  
+  return needsAdapter( adaptee, tyVars, false );
+}
+
 bool
-isPolyVal( Type *type, const TyVarMap &tyVars )
+isPolyVal( Type *type, const TyVarMap &tyVars, bool considerAllTyVars )
 {
   if( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) {
@@ -19,4 +30,5 @@
       return true;
     }
+    return considerAllTyVars;
   }
   return false;
@@ -26,12 +38,12 @@
 // parameters have polymorphic type
 bool
-needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars )
+needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars, bool considerAllTyVars )
 {
   bool needsAdapter = false;
-  if( !adaptee->get_returnVals().empty() && isPolyVal( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
+  if( !adaptee->get_returnVals().empty() && isPolyVal( adaptee->get_returnVals().front()->get_type(), tyVars, considerAllTyVars ) ) {
     needsAdapter = true;
   }
   for( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); !needsAdapter && innerArg != adaptee->get_parameters().end(); ++innerArg ) {
-    if( isPolyVal( (*innerArg)->get_type(), tyVars ) ) {
+    if( isPolyVal( (*innerArg)->get_type(), tyVars, considerAllTyVars ) ) {
       needsAdapter = true;
     }
Index: translator/GenPoly/GenPoly.h
===================================================================
--- translator/GenPoly/GenPoly.h	(revision d4778a6e9b648e3e3f98c72c55e110ea16df1cc9)
+++ translator/GenPoly/GenPoly.h	(revision b1a6d6b5ecf5925aa65742a37eb5caaa0b59cb43)
@@ -5,4 +5,7 @@
  *
  */
+
+#ifndef GENPOLY_H
+#define GENPOLY_H
 
 #include <map>
@@ -15,8 +18,15 @@
 typedef std::map< std::string, TypeDecl::Kind > TyVarMap;
 
-bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars );
+// considerAllTyVars allows ignoring the contents of the TyVarMap parameter, for the situations where
+// it is important only that a TypeInstType node exists.
+
+bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVarr );
+bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars, bool considerAllTyVars );
 bool isPolyFun( FunctionType *fun, const TyVarMap &tyVars );
 bool isPolyVal( Type *type, const TyVarMap &tyVars );
+bool isPolyVal( Type *type, const TyVarMap &tyVars, bool considerAllTyVars );
 void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap );
 
 } // namespace GenPoly
+
+#endif
Index: translator/GenPoly/PolyMutator.cc
===================================================================
--- translator/GenPoly/PolyMutator.cc	(revision d4778a6e9b648e3e3f98c72c55e110ea16df1cc9)
+++ translator/GenPoly/PolyMutator.cc	(revision b1a6d6b5ecf5925aa65742a37eb5caaa0b59cb43)
@@ -51,5 +51,5 @@
     compound->get_kids().push_back( newStmt );
     compound->get_kids().splice( compound->get_kids().end(), stmtsToAddAfter );
-    doEndScope();
+    // doEndScope();
     return compound;
   } else {
@@ -74,4 +74,5 @@
 PolyMutator::mutate(CompoundStmt *compoundStmt)
 {
+  doBeginScope();
   mutateStatementList( compoundStmt->get_kids() );
   doEndScope();
Index: translator/GenPoly/PolyMutator.h
===================================================================
--- translator/GenPoly/PolyMutator.h	(revision d4778a6e9b648e3e3f98c72c55e110ea16df1cc9)
+++ translator/GenPoly/PolyMutator.h	(revision b1a6d6b5ecf5925aa65742a37eb5caaa0b59cb43)
@@ -41,4 +41,5 @@
   
   // template method
+  virtual void doBeginScope() {}
   virtual void doEndScope() {}
 
Index: translator/ResolvExpr/Resolver.cc
===================================================================
--- translator/ResolvExpr/Resolver.cc	(revision d4778a6e9b648e3e3f98c72c55e110ea16df1cc9)
+++ translator/ResolvExpr/Resolver.cc	(revision b1a6d6b5ecf5925aa65742a37eb5caaa0b59cb43)
@@ -192,5 +192,9 @@
 
     void Resolver::visit( ForStmt *forStmt ) {
+        // SymTab::Indexer::visit( forStmt );
 	Expression *newExpr;
+        // for statements introduce a level of scope
+        enterScope();
+        maybeAccept( forStmt->get_initialization(), *this );
 	if ( forStmt->get_condition() ) {
 	    newExpr = findSingleExpression( forStmt->get_condition(), *this );
@@ -204,6 +208,9 @@
 	    forStmt->set_increment( newExpr );
 	} // if
-  
-	Visitor::visit( forStmt );
+
+        maybeAccept( forStmt->get_condition(), *this );
+        maybeAccept( forStmt->get_increment(), *this );
+        maybeAccept( forStmt->get_body(), *this );
+        leaveScope();
     }
 
Index: translator/SymTab/Validate.cc
===================================================================
--- translator/SymTab/Validate.cc	(revision d4778a6e9b648e3e3f98c72c55e110ea16df1cc9)
+++ translator/SymTab/Validate.cc	(revision b1a6d6b5ecf5925aa65742a37eb5caaa0b59cb43)
@@ -155,8 +155,8 @@
     };
 
-    void validate( std::list< Declaration * > &translationUnit, bool doDebug, const Indexer *indexer ) {
+    void validate( std::list< Declaration * > &translationUnit, bool doDebug ) {
 	Pass1 pass1;
-	Pass2 pass2( doDebug, indexer );
-	Pass3 pass3( indexer );
+	Pass2 pass2( doDebug, 0 );
+	Pass3 pass3( 0 );
 	EliminateTypedef::eliminateTypedef( translationUnit );
 	HoistStruct::hoistStruct( translationUnit );
@@ -166,5 +166,5 @@
 	acceptAll( translationUnit, pass3 );
     }
-
+    
     void validateType( Type *type, const Indexer *indexer ) {
 	Pass1 pass1;
Index: translator/SynTree/CompoundStmt.cc
===================================================================
--- translator/SynTree/CompoundStmt.cc	(revision d4778a6e9b648e3e3f98c72c55e110ea16df1cc9)
+++ translator/SynTree/CompoundStmt.cc	(revision b1a6d6b5ecf5925aa65742a37eb5caaa0b59cb43)
@@ -11,4 +11,6 @@
 #include <functional>
 
+#include <iostream>
+using namespace std;
 
 CompoundStmt::CompoundStmt( std::list<Label> labels )
@@ -31,5 +33,6 @@
 CompoundStmt::print( std::ostream &os, int indent )
 {
-    printAll( kids, os, indent );
+    os << "\r" << string(indent, ' ') << "CompoundStmt" << endl ;
+    printAll( kids, os, indent+2 );
 }
 
Index: translator/main.cc
===================================================================
--- translator/main.cc	(revision d4778a6e9b648e3e3f98c72c55e110ea16df1cc9)
+++ translator/main.cc	(revision b1a6d6b5ecf5925aa65742a37eb5caaa0b59cb43)
@@ -49,5 +49,5 @@
     bool debugp = false, treep = false, astp = false, manglep = false, symtabp = false, validp = false;
     bool preludep = true, protop = false, libp = false;
-    bool exprp = false, codegenp = false;
+    bool exprp = false, codegenp = false, errorp = false;
     int c;
     FILE *input, *prelude, *builtins;
@@ -56,36 +56,38 @@
     opterr = 0;
 
-    while ( (c = getopt( argc, argv, "dtsgmvxcenprlDz:" )) != -1 ) {
+    std::list< Declaration* > translationUnit;
+    
+    while ( (c = getopt( argc, argv, "dtsgmvxcenprlDyz:" )) != -1 ) {
 	switch (c) {
 	  case 'd':
-	    /* bison debugging info */
+	    // bison debugging info
 	    debugp = true;
 	    break;
 	  case 't':
-	    /* dump parse tree */
+	    // dump parse tree
 	    treep = true;
 	    break;
 	  case 's':
-	    /* dump AST */
+	    // dump AST
 	    astp = true;
 	    break;
 	  case 'g':
-	    /* print alternatives for expressions */
+	    // print alternatives for expressions
 	    manglep = true;
 	    break;
 	  case 'm':
-	    /* print symbol table events */
+	    // print symbol table events
 	    symtabp = true;
 	    break;
 	  case 'r':
-	    /* print resolver steps */
+	    // print resolver steps
 	    resolveVerbose = true;
 	    break;
 	  case 'x':
-	    /* dump AST after decl validation pass */
+	    // dump AST after decl validation pass
 	    validp = true;
 	    break;
 	  case 'e':
-	    /* dump AST after expression analysis */
+	    // dump AST after expression analysis
 	    exprp = true;
 	    break;
@@ -93,22 +95,25 @@
             codegenp = true;
             break;
+          case 'y':
+            errorp = true;
+            break;
 	  case 'n':
-	    /* don't read preamble */
+	    // don't read preamble
 	    preludep = false;
 	    break;
 	  case 'p':
-	    /* generate prototypes for preamble functions */
+	    // generate prototypes for preamble functions
 	    protop = true;
 	    break;
 	  case 'l':
-	    /* generate libcfa.c */
+	    // generate libcfa.c
 	    libp = true;
 	    break;
 	  case 'v':
-	    /* verbose */
+	    // verbose
 	    beVerbose = true;
 	    break;
 	  case 'D':
-	    /* ignore -Dxxx */
+	    // ignore -Dxxx
 	    break;
 	  case '?':
@@ -205,5 +210,4 @@
 	} // if
 
-	std::list< Declaration* > translationUnit;
 	buildList( Parser::get_parser().get_parseTree(), translationUnit );
 
@@ -233,26 +237,36 @@
 
 	if ( exprp ) {
+ 	    InitTweak::tweak( translationUnit );
 	    SymTab::validate( translationUnit, false );
-	    ResolvExpr::resolve( translationUnit );
-	    printAll( translationUnit, std::cout );
-	    return 0;
-	} // if
-
-	if ( codegenp ) {
-            // print the tree right before code generation...
-                        // InitTweak::mutate( translationUnit );
-            //            InitTweak::tweak( translationUnit );
-            //printAll( translationUnit, std::cout );
-
-	    // std::cerr << "finished tweaking" << std::endl;
-            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 );
-            printAll( translationUnit, std::cout );
+            if ( errorp ) {
+                printAll( translationUnit, std::cout );
+            }
             return 0;
         } // if
@@ -293,4 +307,7 @@
 
     } catch ( SemanticError &e ) {
+    	if ( errorp ) {
+           printAll( translationUnit, std::cout );
+    	}
 	e.print( cout );
 	if ( output != &std::cout ) {
