Changeset e56cfdb0 for src/examples


Ignore:
Timestamp:
Nov 19, 2015, 6:06:12 PM (10 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/examples
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • 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.