Changeset e56cfdb0


Ignore:
Timestamp:
Nov 19, 2015, 6:06:12 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, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
Children:
05587c2
Parents:
d2ded3e7
Message:

allow nested routines to use type variables in containing scope, fix missing adapters, generalized iostream write

Location:
src
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/Box.cc

    rd2ded3e7 re56cfdb0  
    99// Author           : Richard C. Bilson
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Rob Schluntz
    12 // Last Modified On : Tue Aug 11 16:22:35 2015
    13 // Update Count     : 89
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Thu Nov 19 17:40:51 2015
     13// Update Count     : 133
    1414//
    1515
     
    4747        namespace {
    4848                const std::list<Label> noLabels;
     49
     50                FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars );
    4951
    5052                class Pass1 : public PolyMutator {
     
    7880                        ObjectDecl *makeTemporary( Type *type );
    7981
    80                         typedef std::map< std::string, FunctionDecl *> AdapterMap;
     82                        typedef std::map< std::string, DeclarationWithType *> AdapterMap;
    8183                        std::map< std::string, DeclarationWithType *> assignOps;
    8284                        std::stack< AdapterMap > adapters;
     
    144146                }
    145147
    146                 bool isPolyRet( FunctionType *function, std::string &name, const TyVarMap &otherTyVars ) {
    147                         bool doTransform = false;
    148                         if ( ! function->get_returnVals().empty() ) {
    149                                 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( function->get_returnVals().front()->get_type() ) ) {
    150        
    151                                         // figure out if the return type is specified by a type parameter
    152                                         for ( std::list< TypeDecl *>::const_iterator tyVar = function->get_forall().begin(); tyVar != function->get_forall().end(); ++tyVar ) {
    153                                                 if ( (*tyVar)->get_name() == typeInst->get_name() ) {
    154                                                         doTransform = true;
    155                                                         name = typeInst->get_name();
    156                                                         break;
    157                                                 } // if
    158                                         } // for
    159                                         if ( ! doTransform && otherTyVars.find( typeInst->get_name() ) != otherTyVars.end() ) {
    160                                                 doTransform = true;
    161                                         } // if
    162                                 } // if
    163                         } // if
    164                         return doTransform;
    165                 }
    166 
    167                 bool isPolyRet( FunctionType *function, std::string &name ) {
    168                         TyVarMap dummyTyVars;
    169                         return isPolyRet( function, name, dummyTyVars );
    170                 }
    171 
    172                 bool isPolyRet( FunctionType *function, const TyVarMap &otherTyVars ) {
    173                         std::string dummyString;
    174                         return isPolyRet( function, dummyString, otherTyVars );
    175                 }
    176 
    177148                Pass1::Pass1()
    178149                        : useRetval( false ), tempNamer( "_temp" ) {
     150                        adapters.push(AdapterMap());
    179151                }
    180152
     
    210182
    211183                DeclarationWithType *Pass1::mutate( FunctionDecl *functionDecl ) {
    212                         if ( functionDecl->get_statements() ) {
     184                        if ( functionDecl->get_statements() ) {         // empty routine body ?
     185                                doBeginScope();
    213186                                TyVarMap oldtyVars = scopeTyVars;
    214187                                DeclarationWithType *oldRetval = retval;
    215188                                bool oldUseRetval = useRetval;
    216        
     189
     190                                // process polymorphic return value
    217191                                retval = 0;
    218192                                std::string typeName;
     
    227201                                } // if
    228202       
    229                                 scopeTyVars.clear();
    230 ///     std::cerr << "clear\n";
     203                                FunctionType *functionType = functionDecl->get_functionType();
    231204                                makeTyVarMap( functionDecl->get_functionType(), scopeTyVars );
    232205                                findAssignOps( functionDecl->get_functionType()->get_forall() );
     206
     207                                std::list< DeclarationWithType *> &paramList = functionType->get_parameters();
     208                                std::list< FunctionType *> functions;
     209                                for ( std::list< TypeDecl *>::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) {
     210                                        for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) {
     211                                                findFunction( (*assert)->get_type(), functions, scopeTyVars, needsAdapter );
     212                                        } // for
     213                                } // for
     214                                for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) {
     215                                        findFunction( (*arg)->get_type(), functions, scopeTyVars, needsAdapter );
     216                                } // for
     217                                AdapterMap & adapters = Pass1::adapters.top();
     218                                for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
     219                                        std::string mangleName = SymTab::Mangler::mangle( *funType );
     220                                        if ( isPolyRet( *funType, scopeTyVars ) ) {
     221                                                // if the return type involved polymorphic types, then the adapter will need to take those
     222                                                // polymorphic types as pointers. Therefore, there can be two different functions with the same
     223                                                // mangled name, so we need the mangled names to be different.
     224                                                mangleName += "polyret_";
     225                                        } // if
     226                                        if ( adapters.find( mangleName ) == adapters.end() ) {
     227                                                std::string adapterName = makeAdapterName( mangleName );
     228                                                adapters.insert( std::pair< std::string, DeclarationWithType *>( mangleName, new ObjectDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) ) );
     229                                        } // if
     230                                } // for
     231
    233232                                functionDecl->set_statements( functionDecl->get_statements()->acceptMutator( *this ) );
    234233 
    235234                                scopeTyVars = oldtyVars;
    236 ///    std::cerr << "end FunctionDecl: ";
    237 ///    for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
    238 ///       std::cerr << i->first << " ";
    239 ///    }
    240 ///    std::cerr << "\n";
     235                                // std::cerr << "end FunctionDecl: ";
     236                                // for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
     237                                //      std::cerr << i->first << " ";
     238                                // }
     239                                // std::cerr << "\n";
    241240                                retval = oldRetval;
    242241                                useRetval = oldUseRetval;
    243                                 // doEndScope();
     242                                doEndScope();
    244243                        } // if
    245244                        return functionDecl;
     
    349348                        } // if
    350349                        std::string mangleName = SymTab::Mangler::mangle( function );
     350                        if ( isPolyRet( function, tyVars ) ) {
     351                                mangleName += "polyret_";
     352                        } // if
    351353                        std::string adapterName = makeAdapterName( mangleName );
    352354
     
    457459///       return new CastExpr( new VariableExpr( param ), arg->get_type()->clone() );
    458460///     } else {
    459                                 UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
    460                                 deref->get_args().push_back( new CastExpr( new VariableExpr( param ), new PointerType( Type::Qualifiers(), arg->get_type()->clone() ) ) );
    461                                 deref->get_results().push_back( arg->get_type()->clone() );
    462                                 return deref;
     461                                if ( dynamic_cast<TypeInstType *>(arg->get_type()) == NULL ) {
     462                                        UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
     463                                        deref->get_args().push_back( new CastExpr( new VariableExpr( param ), new PointerType( Type::Qualifiers(), arg->get_type()->clone() ) ) );
     464                                        deref->get_results().push_back( arg->get_type()->clone() );
     465                                        return deref;
     466                                } // if
    463467///     }
    464468                        } // if
     
    544548                        } // for
    545549
    546                         // parameter function types for which an appropriate adapter has been generated.
    547                         // we cannot use the types after applying substitutions, since two different
    548                         // parameter types may be unified to the same type
     550                        // parameter function types for which an appropriate adapter has been generated.  we cannot use the types
     551                        // after applying substitutions, since two different parameter types may be unified to the same type
    549552                        std::set< std::string > adaptersDone;
    550553
     
    554557                                std::string mangleName = SymTab::Mangler::mangle( realFunction );
    555558
    556                                 // only attempt to create an adapter or pass one as a parameter if we haven't
    557                                 // already done so for this pre-substitution parameter function type.
     559                                // only attempt to create an adapter or pass one as a parameter if we haven't already done so for this
     560                                // pre-substitution parameter function type.
    558561                                if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
    559                                         std::string mangleName = SymTab::Mangler::mangle( realFunction );
    560562                                        adaptersDone.insert( adaptersDone.begin(), mangleName );
    561563                                       
    562                                         // apply substitution to type variables to figure out what the
    563                                         // adapter's type should look like
     564                                        // apply substitution to type variables to figure out what the adapter's type should look like
    564565                                        assert( env );
    565566                                        env->apply( realFunction );
    566567                                        mangleName = SymTab::Mangler::mangle( realFunction );
    567568
    568                                         if ( needsAdapter( realFunction, exprTyVars, true ) ) {
    569                                                 // the function still contains type variables, which means we are in a polymorphic
    570                                                 // context and the adapter function is a parameter - call the parameter and don't
    571                                                 // create a new adapter.
    572                                                 appExpr->get_args().push_front( new NameExpr( makeAdapterName ( mangleName ) ) );
    573                                         } else {
    574                                                 if ( isPolyRet( originalFunction, exprTyVars ) ) {
    575                                                         // if the return type involved polymorphic types, then
    576                                                         // the adapter will need to take those polymorphic types
    577                                                         // as pointers. Therefore, there can be two different
    578                                                         // functions with the same mangled name, so we need two adapter map
    579                                                         // stacks and also we need the mangled names to be different.
    580                                                         mangleName += "polyret_";
    581                                                 }
    582 
    583                                                 AdapterMap & adapters = Pass1::adapters.top();
    584                                                 AdapterMap::iterator adapter = adapters.find( mangleName );
    585                                                 if ( adapter == adapters.end() ) {
    586                                                         // adapter has not been created yet in the current scope, so define it
    587                                                         FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars );
    588                                                         adapter = adapters.insert( adapters.begin(), std::pair< std::string, FunctionDecl *>( mangleName, newAdapter ) );
    589                                                         stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) );
    590                                                 } // if
    591                                                 assert( adapter != adapters.end() );
    592 
    593                                                 // add the appropriate adapter as a parameter
    594                                                 appExpr->get_args().push_front( new VariableExpr( adapter->second ) );
    595                                         } // if
     569                                        if ( isPolyRet( originalFunction, exprTyVars ) ) {
     570                                                mangleName += "polyret_";
     571                                        } // if
     572
     573                                        AdapterMap & adapters = Pass1::adapters.top();
     574                                        AdapterMap::iterator adapter = adapters.find( mangleName );
     575                                        if ( adapter == adapters.end() ) {
     576                                                // adapter has not been created yet in the current scope, so define it
     577                                                FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars );
     578                                                adapter = adapters.insert( adapters.begin(), std::pair< std::string, DeclarationWithType *>( mangleName, newAdapter ) );
     579                                                stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) );
     580                                        } // if
     581                                        assert( adapter != adapters.end() );
     582
     583                                        // add the appropriate adapter as a parameter
     584                                        appExpr->get_args().push_front( new VariableExpr( adapter->second ) );
    596585                                } // if
    597586                        } // for
    598                 }
     587                } // passAdapters
    599588
    600589                TypeInstType *isPolyPtr( Type *type, const TypeSubstitution *env, const TyVarMap &tyVars ) {
     
    769758
    770759                Expression *Pass1::mutate( ApplicationExpr *appExpr ) {
    771 ///    std::cerr << "mutate appExpr: ";
    772 ///    for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
    773 ///       std::cerr << i->first << " ";
    774 ///    }
    775 ///    std::cerr << "\n";
     760                        // std::cerr << "mutate appExpr: ";
     761                        // for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
     762                        //      std::cerr << i->first << " ";
     763                        // }
     764                        // std::cerr << "\n";
    776765                        bool oldUseRetval = useRetval;
    777766                        useRetval = false;
     
    799788                                ret = addPolyRetParam( appExpr, function, typeName, arg );
    800789                        } else if ( needsAdapter( function, scopeTyVars ) ) {
    801 ///    std::cerr << "needs adapter: ";
    802 ///    for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
    803 ///       std::cerr << i->first << " ";
    804 ///    }
    805 ///    std::cerr << "\n";
     790                                // std::cerr << "needs adapter: ";
     791                                // for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
     792                                //      std::cerr << i->first << " ";
     793                                // }
     794                                // std::cerr << "\n";
    806795                                // change the application so it calls the adapter rather than the passed function
    807796                                ret = applyAdapter( appExpr, function, arg, scopeTyVars );
     
    913902                        // actually, maybe this could (should?) push
    914903                        // a copy of the current map
    915                         adapters.push(AdapterMap());
     904                        adapters.push(adapters.top());
    916905                }
    917906
     
    935924                        for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
    936925                                std::string mangleName = SymTab::Mangler::mangle( *funType );
     926                                if ( isPolyRet( *funType, scopeTyVars ) ) {
     927                                        mangleName += "polyret_";
     928                                } // if
    937929                                if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
    938930                                        std::string adapterName = makeAdapterName( mangleName );
     
    1000992                        for ( std::list< TypeDecl *>::const_iterator tyParm = funcType->get_forall().begin(); tyParm != funcType->get_forall().end(); ++tyParm ) {
    1001993                                ObjectDecl *thisParm;
     994                                // add all size parameters to parameter list
    1002995                                if ( (*tyParm)->get_kind() == TypeDecl::Any ) {
    1003996                                        thisParm = newObj->clone();
     
    1006999                                        ++last;
    10071000                                }
     1001                                // move all assertions into parameter list
    10081002                                for ( std::list< DeclarationWithType *>::iterator assert = (*tyParm)->get_assertions().begin(); assert != (*tyParm)->get_assertions().end(); ++assert ) {
    10091003///      *assert = (*assert)->acceptMutator( *this );
  • src/GenPoly/Box.h

    rd2ded3e7 re56cfdb0  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue May 19 07:32:33 2015
    13 // Update Count     : 2
     12// Last Modified On : Thu Nov 19 17:24:01 2015
     13// Update Count     : 5
    1414//
    1515
  • src/GenPoly/GenPoly.cc

    rd2ded3e7 re56cfdb0  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue May 19 07:37:46 2015
    13 // Update Count     : 1
     12// Last Modified On : Thu Nov 19 17:23:44 2015
     13// Update Count     : 10
    1414//
    1515
     
    2121
    2222namespace GenPoly {
    23         // interface functions
    24         bool isPolyVal( Type *type, const TyVarMap &tyVars ) {
    25                 return isPolyVal( type, tyVars, false );
     23        // A function needs an adapter if it returns a polymorphic value or if any of its
     24        // parameters have polymorphic type
     25        bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) {
     26                if ( ! adaptee->get_returnVals().empty() && isPolyVal( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
     27                        return true;
     28                } // if
     29                for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); innerArg != adaptee->get_parameters().end(); ++innerArg ) {
     30                        if ( isPolyVal( (*innerArg)->get_type(), tyVars ) ) {
     31                                return true;
     32                        } // if
     33                } // for
     34                return false;
    2635        }
    2736
    28         bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) { 
    29                 return needsAdapter( adaptee, tyVars, false );
     37        bool isPolyRet( FunctionType *function, std::string &name, const TyVarMap &otherTyVars ) {
     38                bool doTransform = false;
     39                if ( ! function->get_returnVals().empty() ) {
     40                        if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( function->get_returnVals().front()->get_type() ) ) {
     41       
     42                                // figure out if the return type is specified by a type parameter
     43                                for ( std::list< TypeDecl *>::const_iterator tyVar = function->get_forall().begin(); tyVar != function->get_forall().end(); ++tyVar ) {
     44                                        if ( (*tyVar)->get_name() == typeInst->get_name() ) {
     45                                                doTransform = true;
     46                                                name = typeInst->get_name();
     47                                                break;
     48                                        } // if
     49                                } // for
     50                                if ( ! doTransform && otherTyVars.find( typeInst->get_name() ) != otherTyVars.end() ) {
     51                                        doTransform = true;
     52                                } // if
     53                        } // if
     54                } // if
     55                return doTransform;
    3056        }
    3157
    32         bool isPolyVal( Type *type, const TyVarMap &tyVars, bool considerAllTyVars ) {
    33                 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) {
     58        bool isPolyRet( FunctionType *function, std::string &name ) {
     59                TyVarMap dummyTyVars;
     60                return isPolyRet( function, name, dummyTyVars );
     61        }
     62
     63        bool isPolyRet( FunctionType *function, const TyVarMap &otherTyVars ) {
     64                std::string dummyString;
     65                return isPolyRet( function, dummyString, otherTyVars );
     66        }
     67
     68        bool isPolyVal( Type *type, const TyVarMap &tyVars ) {
     69                if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
    3470                        if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) {
    3571                                return true;
    3672                        } // if
    37                         return considerAllTyVars;
    3873                } // if
    3974                return false;
    40         }
    41 
    42         // A function needs an adapter if it returns a polymorphic value or if any of its
    43         // parameters have polymorphic type
    44         bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars, bool considerAllTyVars ) {
    45                 bool needsAdapter = false;
    46                 if ( ! adaptee->get_returnVals().empty() && isPolyVal( adaptee->get_returnVals().front()->get_type(), tyVars, considerAllTyVars ) ) {
    47                         needsAdapter = true;
    48                 } // if
    49                 for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); ! needsAdapter && innerArg != adaptee->get_parameters().end(); ++innerArg ) {
    50                         if ( isPolyVal( (*innerArg)->get_type(), tyVars, considerAllTyVars ) ) {
    51                                 needsAdapter = true;
    52                         } // if
    53                 } // for
    54                 return needsAdapter;
    5575        }
    5676
  • src/GenPoly/GenPoly.h

    rd2ded3e7 re56cfdb0  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue May 19 07:38:34 2015
    13 // Update Count     : 1
     12// Last Modified On : Thu Nov 19 17:24:03 2015
     13// Update Count     : 4
    1414//
    1515
     
    2525        typedef std::map< std::string, TypeDecl::Kind > TyVarMap;
    2626
    27         // considerAllTyVars allows ignoring the contents of the TyVarMap parameter, for the situations where
    28         // it is important only that a TypeInstType node exists.
    29 
    3027        bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVarr );
    31         bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars, bool considerAllTyVars );
    32         bool isPolyFun( FunctionType *fun, const TyVarMap &tyVars );
     28        bool isPolyRet( FunctionType *function, std::string &name, const TyVarMap &otherTyVars );
     29        bool isPolyRet( FunctionType *function, std::string &name );
     30        bool isPolyRet( FunctionType *function, const TyVarMap &otherTyVars );
     31//      bool isPolyFun( FunctionType *fun, const TyVarMap &tyVars );
    3332        bool isPolyVal( Type *type, const TyVarMap &tyVars );
    34         bool isPolyVal( Type *type, const TyVarMap &tyVars, bool considerAllTyVars );
    3533        void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap );
    3634} // namespace GenPoly
  • src/examples/Makefile.am

    rd2ded3e7 re56cfdb0  
    1111## Created On       : Sun May 31 09:08:15 2015
    1212## Last Modified By : Peter A. Buhr
    13 ## Last Modified On : Thu Jun  4 23:13:10 2015
    14 ## Update Count     : 22
     13## Last Modified On : Thu Nov 19 18:01:56 2015
     14## Update Count     : 23
    1515###############################################################################
    1616
     
    2020
    2121noinst_PROGRAMS = fstream_test vector_test
    22 fstream_test_SOURCES = iostream.c fstream.c fstream_test.c
     22fstream_test_SOURCES = iostream.c fstream.c fstream_test.c iterator.c
    2323vector_test_SOURCES = vector_int.c fstream.c iostream.c array.c iterator.c vector_test.c
  • src/examples/Makefile.in

    rd2ded3e7 re56cfdb0  
    4949PROGRAMS = $(noinst_PROGRAMS)
    5050am_fstream_test_OBJECTS = iostream.$(OBJEXT) fstream.$(OBJEXT) \
    51         fstream_test.$(OBJEXT)
     51        fstream_test.$(OBJEXT) iterator.$(OBJEXT)
    5252fstream_test_OBJECTS = $(am_fstream_test_OBJECTS)
    5353fstream_test_LDADD = $(LDADD)
     
    176176top_builddir = @top_builddir@
    177177top_srcdir = @top_srcdir@
    178 fstream_test_SOURCES = iostream.c fstream.c fstream_test.c
     178fstream_test_SOURCES = iostream.c fstream.c fstream_test.c iterator.c
    179179vector_test_SOURCES = vector_int.c fstream.c iostream.c array.c iterator.c vector_test.c
    180180all: all-am
  • src/examples/iostream.c

    rd2ded3e7 re56cfdb0  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed May 27 18:18:13 2015
    13 // Update Count     : 2
     12// Last Modified On : Thu Nov 19 17:54:38 2015
     13// Update Count     : 4
    1414//
    1515
     
    3030forall( dtype ostype | ostream( ostype ) )
    3131ostype * ?<<?( ostype *os, int i ) {
    32         char buffer[20];      // larger than the largest integer
     32        char buffer[32];                                                                        // larger than the largest integer
    3333        sprintf( buffer, "%d", i );
    3434        return write( os, buffer, strlen( buffer ) );
     
    3737forall( dtype ostype | ostream( ostype ) )
    3838ostype * ?<<?( ostype *os, double d ) {
    39         char buffer[32];      // larger than the largest double
     39        char buffer[32];                                                                        // larger than the largest double
    4040        sprintf( buffer, "%g", d );
    4141        return write( os, buffer, strlen( buffer ) );
     
    4646        return write( os, cp, strlen( cp ) );
    4747}
     48
     49forall( dtype ostype | ostream( ostype ) )
     50ostype * ?<<?( ostype *os, const void *p ) {
     51        char buffer[32];                                                                        // larger than the largest pointer
     52        sprintf( buffer, "%p", p );
     53        return write( os, buffer, strlen( buffer ) );
     54}
     55
     56forall( type elt_type | writeable( elt_type ),
     57                type iterator_type | iterator( iterator_type, elt_type ),
     58                dtype os_type | ostream( os_type ) )
     59void write( iterator_type begin, iterator_type end, os_type *os ) {
     60        void print( elt_type i ) {
     61                os << i << ' ';
     62        }
     63        for_each( begin, end, print );
     64}
     65
     66forall( type elt_type | writeable( elt_type ),
     67                type iterator_type | iterator( iterator_type, elt_type ),
     68                dtype os_type | ostream( os_type ) )
     69void write_reverse( iterator_type begin, iterator_type end, os_type *os ) {
     70        void print( elt_type i ) {
     71                os << i << ' ';
     72        }
     73        for_each_reverse( begin, end, print );
     74}
     75
    4876
    4977forall( dtype istype | istream( istype ) )
  • src/examples/iostream.h

    rd2ded3e7 re56cfdb0  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed May 27 18:18:46 2015
    13 // Update Count     : 1
     12// Last Modified On : Thu Nov 19 17:56:51 2015
     13// Update Count     : 5
    1414//
    1515
    1616#ifndef IOSTREAM_H
    1717#define IOSTREAM_H
     18
     19#include "iterator.h"
    1820
    1921typedef unsigned long streamsize_type;
     
    3436forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, double );
    3537forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, const char * );
     38forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, void * );
     39
     40// writes the range [begin, end) to the given stream
     41forall( type elt_type | writeable( elt_type ),
     42                type iterator_type | iterator( iterator_type, elt_type ),
     43                dtype os_type | ostream( os_type ) )
     44void write( iterator_type begin, iterator_type end, os_type *os );
     45
     46forall( type elt_type | writeable( elt_type ),
     47                type iterator_type | iterator( iterator_type, elt_type ),
     48                dtype os_type | ostream( os_type ) )
     49void write_reverse( iterator_type begin, iterator_type end, os_type *os );
    3650
    3751
  • src/examples/iterator.c

    rd2ded3e7 re56cfdb0  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed May 27 18:41:41 2015
    13 // Update Count     : 3
     12// Last Modified On : Thu Nov 19 17:54:37 2015
     13// Update Count     : 24
    1414//
    1515
    1616#include "iterator.h"
    1717
    18 /// forall( type iterator_type, type elt_type | iterator( iterator_type, elt_type ) )
    19 /// void
    20 /// for_each( iterator_type begin, iterator_type end, void (*func)( elt_type ) )
    21 /// {
    22 ///   iterator_type i;
    23 ///   for ( i = begin; i != end; ++i ) {
    24 ///      func( *i );
    25 ///   }
    26 /// }
    27 
    28 forall( type elt_type | writeable( elt_type ),
    29                 type iterator_type | iterator( iterator_type, elt_type ),
    30                 dtype os_type | ostream( os_type ) )
    31 void write_all( iterator_type begin, iterator_type end, os_type *os ) {
    32         iterator_type i;
    33         for ( i = begin; i != end; ++i ) {
    34                 os << *i << ' ';
     18forall( type iterator_type, type elt_type | iterator( iterator_type, elt_type ) )
     19void for_each( iterator_type begin, iterator_type end, void (*func)( elt_type ) ) {
     20        for ( iterator_type i = begin; i != end; ++i ) {
     21                func( *i );
    3522        }
    3623}
    3724
    38 forall( type elt_type | writeable( elt_type ),
    39                 type iterator_type | iterator( iterator_type, elt_type ),
    40                 dtype os_type | ostream( os_type ) )
    41 void write_reverse( iterator_type begin, iterator_type end, os_type *os ) {
    42         iterator_type i; // "= end;" does not work
    43         i = end;
    44         do {
     25forall( type iterator_type, type elt_type | iterator( iterator_type, elt_type ) )
     26void for_each_reverse( iterator_type begin, iterator_type end, void (*func)( elt_type ) ) {
     27        for ( iterator_type i = end; i != begin; ) {
    4528                --i;
    46                 os << *i << ' ';
    47         } while ( i != begin );
     29                func( *i );
     30        }
    4831}
    4932
  • src/examples/iterator.h

    rd2ded3e7 re56cfdb0  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed May 27 18:41:57 2015
    13 // Update Count     : 3
     12// Last Modified On : Thu Nov 19 17:58:28 2015
     13// Update Count     : 6
    1414//
    1515
    1616#ifndef ITERATOR_H
    1717#define ITERATOR_H
    18 
    19 #include "iostream.h"
    2018
    2119// An iterator can be used to traverse a data structure.
     
    3432};
    3533
    36 context iterator_for ( type iterator_type, type collection_type, type elt_type | iterator( iterator_type, elt_type ) ) {
     34context iterator_for( type iterator_type, type collection_type, type elt_type | iterator( iterator_type, elt_type ) ) {
    3735//      [ iterator_type begin, iterator_type end ] get_iterators( collection_type );
    3836        iterator_type begin( collection_type );
     
    4341void for_each( iterator_type begin, iterator_type end, void (*func)( elt_type ) );
    4442
    45 // writes the range [begin, end) to the given stream
    46 forall( type elt_type | writeable( elt_type ),
    47                 type iterator_type | iterator( iterator_type, elt_type ),
    48                 dtype os_type | ostream( os_type ) )
    49 void write_all( iterator_type begin, iterator_type end, os_type *os );
    50 
    51 forall( type elt_type | writeable( elt_type ),
    52                 type iterator_type | iterator( iterator_type, elt_type ),
    53                 dtype os_type | ostream( os_type ) )
    54 void write_reverse( iterator_type begin, iterator_type end, os_type *os );
     43forall( type iterator_type, type elt_type | iterator( iterator_type, elt_type ) )
     44void for_each_reverse( iterator_type begin, iterator_type end, void (*func)( elt_type ) );
    5545
    5646#endif // ITERATOR_H
  • src/examples/vector_test.c

    rd2ded3e7 re56cfdb0  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed May 27 18:42:55 2015
    13 // Update Count     : 2
     12// Last Modified On : Thu Nov 19 17:54:34 2015
     13// Update Count     : 9
    1414//
    1515
     
    2929        sout << "enter N elements and C-d on a separate line:\n";
    3030        for ( ;; ) {
    31         sin >> &num;
     31                sin >> &num;
    3232          if ( fail( sin ) || eof( sin ) ) break;
    33         append( &vec, num );
     33                append( &vec, num );
    3434        }
    3535        // write out the numbers
    3636
    3737        sout << "Array elements:\n";
    38 //      write_all( begin( vec ), end( vec ), sout );
    39 //      sout << "\n";
    40         for ( int index = 0; index <= last( vec ); index += 1 ) {
    41         sout << vec[ index ] << " ";
    42         }
     38        write( begin( vec ), end( vec ), sout );
    4339        sout << "\n";
    44 #if 1
     40
    4541        sout << "Array elements reversed:\n";
    4642        write_reverse( begin( vec ), end( vec ), sout );
    4743        sout << "\n";
    48 #endif
    4944}
    50 
    51 // ../bin/cfa vector_test.c fstream.o iostream.o vector_int.o iterator.o array.o
    5245
    5346// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.