Changeset 5189888 for src/examples
- Timestamp:
- Nov 20, 2015, 4:44:27 PM (10 years ago)
- 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:
- 7e23d0a, bdf1954
- Parents:
- 66a2a61 (diff), 839ccbb (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- src/examples
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
src/examples/Makefile.am
r66a2a61 r5189888 11 11 ## Created On : Sun May 31 09:08:15 2015 12 12 ## Last Modified By : Peter A. Buhr 13 ## Last Modified On : Thu Jun 4 23:13:10201514 ## Update Count : 2 213 ## Last Modified On : Fri Nov 20 16:03:46 2015 14 ## Update Count : 24 15 15 ############################################################################### 16 16 … … 19 19 CC = @CFA_BINDIR@/cfa 20 20 21 noinst_PROGRAMS = fstream_test vector_test 22 fstream_test_SOURCES = iostream.c fstream.c fstream_test.c 21 noinst_PROGRAMS = fstream_test vector_test # build but do not install 22 fstream_test_SOURCES = iostream.c fstream.c fstream_test.c iterator.c 23 23 vector_test_SOURCES = vector_int.c fstream.c iostream.c array.c iterator.c vector_test.c -
src/examples/Makefile.in
r66a2a61 r5189888 49 49 PROGRAMS = $(noinst_PROGRAMS) 50 50 am_fstream_test_OBJECTS = iostream.$(OBJEXT) fstream.$(OBJEXT) \ 51 fstream_test.$(OBJEXT) 51 fstream_test.$(OBJEXT) iterator.$(OBJEXT) 52 52 fstream_test_OBJECTS = $(am_fstream_test_OBJECTS) 53 53 fstream_test_LDADD = $(LDADD) … … 176 176 top_builddir = @top_builddir@ 177 177 top_srcdir = @top_srcdir@ 178 fstream_test_SOURCES = iostream.c fstream.c fstream_test.c 178 fstream_test_SOURCES = iostream.c fstream.c fstream_test.c iterator.c 179 179 vector_test_SOURCES = vector_int.c fstream.c iostream.c array.c iterator.c vector_test.c 180 180 all: all-am -
src/examples/fstream.c
r66a2a61 r5189888 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:12:33201513 // Update Count : 212 // Last Modified On : Thu Nov 19 22:43:31 2015 13 // Update Count : 4 14 14 // 15 15 … … 30 30 fwrite( data, size, 1, os->file ); 31 31 os->fail = ferror( os->file ); 32 } 32 } // if 33 33 return os; 34 } 34 } // write 35 35 36 36 int fail( ofstream *os ) { 37 37 return os->fail; 38 } 38 } // fail 39 39 40 40 static ofstream *make_ofstream() { … … 42 42 new_stream->fail = 0; 43 43 return new_stream; 44 } 44 } // make_ofstream 45 45 46 46 ofstream *ofstream_stdout() { … … 48 48 stdout_stream->file = stdout; 49 49 return stdout_stream; 50 } 50 } // ofstream_stdout 51 51 52 52 ofstream *ofstream_stderr() { … … 54 54 stderr_stream->file = stderr; 55 55 return stderr_stream; 56 } 56 } // ofstream_stderr 57 57 58 58 ofstream *ofstream_fromfile( const char *name ) { -
src/examples/hello.c
r66a2a61 r5189888 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:14:58201513 // Update Count : 112 // Last Modified On : Fri Nov 20 16:02:50 2015 13 // Update Count : 3 14 14 // 15 15 … … 28 28 // Local Variables: // 29 29 // tab-width: 4 // 30 // compile-command: "cfa hello.c fstream.o iostream.o " //30 // compile-command: "cfa hello.c fstream.o iostream.o iterator.o" // 31 31 // End: // -
src/examples/iostream.c
r66a2a61 r5189888 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:18:13201513 // Update Count : 212 // Last Modified On : Fri Nov 20 13:19:19 2015 13 // Update Count : 9 14 14 // 15 15 … … 17 17 extern "C" { 18 18 #include <stdio.h> 19 //#include <string.h> 20 //#include <ctype.h> 21 typedef long unsigned int size_t; 22 size_t strlen(const char *s); 19 #include <string.h> // strlen 23 20 } 24 21 … … 26 23 ostype * ?<<?( ostype *os, char c ) { 27 24 return write( os, &c, 1 ); 28 } 25 } // ?<<? 29 26 30 27 forall( dtype ostype | ostream( ostype ) ) 31 28 ostype * ?<<?( ostype *os, int i ) { 32 char buffer[ 20];// larger than the largest integer29 char buffer[32]; // larger than the largest integer 33 30 sprintf( buffer, "%d", i ); 34 31 return write( os, buffer, strlen( buffer ) ); 35 } 32 } // ?<<? 36 33 37 34 forall( dtype ostype | ostream( ostype ) ) 38 35 ostype * ?<<?( ostype *os, double d ) { 39 char buffer[32]; 36 char buffer[32]; // larger than the largest double 40 37 sprintf( buffer, "%g", d ); 41 38 return write( os, buffer, strlen( buffer ) ); 42 } 39 } // ?<<? 43 40 44 41 forall( dtype ostype | ostream( ostype ) ) 45 42 ostype * ?<<?( ostype *os, const char *cp ) { 46 43 return write( os, cp, strlen( cp ) ); 47 } 44 } // ?<<? 45 46 forall( dtype ostype | ostream( ostype ) ) 47 ostype * ?<<?( ostype *os, const void *p ) { 48 char buffer[32]; // larger than the largest pointer 49 sprintf( buffer, "%p", p ); 50 return write( os, buffer, strlen( buffer ) ); 51 } // ?<<? 52 53 forall( type elt_type | writeable( elt_type ), 54 type iterator_type | iterator( iterator_type, elt_type ), 55 dtype os_type | ostream( os_type ) ) 56 void write( iterator_type begin, iterator_type end, os_type *os ) { 57 void print( elt_type i ) { 58 os << i << ' '; 59 } 60 for_each( begin, end, print ); 61 } // ?<<? 62 63 forall( type elt_type | writeable( elt_type ), 64 type iterator_type | iterator( iterator_type, elt_type ), 65 dtype os_type | ostream( os_type ) ) 66 void write_reverse( iterator_type begin, iterator_type end, os_type *os ) { 67 void print( elt_type i ) { 68 os << i << ' '; 69 } 70 for_each_reverse( begin, end, print ); 71 } // ?<<? 72 48 73 49 74 forall( dtype istype | istream( istype ) ) 50 75 istype * ?>>?( istype *is, char *cp ) { 51 76 return read( is, cp, 1 ); 52 } 77 } // ?>>? 53 78 54 79 forall( dtype istype | istream( istype ) ) … … 72 97 unread( is, cur ); 73 98 return is; 74 } 99 } // ?>>? 75 100 76 101 // Local Variables: // -
src/examples/iostream.h
r66a2a61 r5189888 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:18:46201513 // Update Count : 112 // Last Modified On : Thu Nov 19 17:56:51 2015 13 // Update Count : 5 14 14 // 15 15 16 16 #ifndef IOSTREAM_H 17 17 #define IOSTREAM_H 18 19 #include "iterator.h" 18 20 19 21 typedef unsigned long streamsize_type; … … 34 36 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, double ); 35 37 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, const char * ); 38 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, void * ); 39 40 // writes the range [begin, end) to the given stream 41 forall( type elt_type | writeable( elt_type ), 42 type iterator_type | iterator( iterator_type, elt_type ), 43 dtype os_type | ostream( os_type ) ) 44 void write( iterator_type begin, iterator_type end, os_type *os ); 45 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_reverse( iterator_type begin, iterator_type end, os_type *os ); 36 50 37 51 -
src/examples/iterator.c
r66a2a61 r5189888 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:41:41201513 // Update Count : 312 // Last Modified On : Thu Nov 19 17:54:37 2015 13 // Update Count : 24 14 14 // 15 15 16 16 #include "iterator.h" 17 17 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 << ' '; 18 forall( type iterator_type, type elt_type | iterator( iterator_type, elt_type ) ) 19 void for_each( iterator_type begin, iterator_type end, void (*func)( elt_type ) ) { 20 for ( iterator_type i = begin; i != end; ++i ) { 21 func( *i ); 35 22 } 36 23 } 37 24 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 { 25 forall( type iterator_type, type elt_type | iterator( iterator_type, elt_type ) ) 26 void for_each_reverse( iterator_type begin, iterator_type end, void (*func)( elt_type ) ) { 27 for ( iterator_type i = end; i != begin; ) { 45 28 --i; 46 os << *i << ' ';47 } while ( i != begin );29 func( *i ); 30 } 48 31 } 49 32 -
src/examples/iterator.h
r66a2a61 r5189888 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:41:57201513 // Update Count : 312 // Last Modified On : Thu Nov 19 17:58:28 2015 13 // Update Count : 6 14 14 // 15 15 16 16 #ifndef ITERATOR_H 17 17 #define ITERATOR_H 18 19 #include "iostream.h"20 18 21 19 // An iterator can be used to traverse a data structure. … … 34 32 }; 35 33 36 context iterator_for 34 context iterator_for( type iterator_type, type collection_type, type elt_type | iterator( iterator_type, elt_type ) ) { 37 35 // [ iterator_type begin, iterator_type end ] get_iterators( collection_type ); 38 36 iterator_type begin( collection_type ); … … 43 41 void for_each( iterator_type begin, iterator_type end, void (*func)( elt_type ) ); 44 42 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 ); 43 forall( type iterator_type, type elt_type | iterator( iterator_type, elt_type ) ) 44 void for_each_reverse( iterator_type begin, iterator_type end, void (*func)( elt_type ) ); 55 45 56 46 #endif // ITERATOR_H -
src/examples/vector_test.c
r66a2a61 r5189888 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed May 27 18:42:55201513 // Update Count : 212 // Last Modified On : Thu Nov 19 17:54:34 2015 13 // Update Count : 9 14 14 // 15 15 … … 29 29 sout << "enter N elements and C-d on a separate line:\n"; 30 30 for ( ;; ) { 31 sin >> #31 sin >> # 32 32 if ( fail( sin ) || eof( sin ) ) break; 33 append( &vec, num );33 append( &vec, num ); 34 34 } 35 35 // write out the numbers 36 36 37 37 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 ); 43 39 sout << "\n"; 44 #if 1 40 45 41 sout << "Array elements reversed:\n"; 46 42 write_reverse( begin( vec ), end( vec ), sout ); 47 43 sout << "\n"; 48 #endif49 44 } 50 51 // ../bin/cfa vector_test.c fstream.o iostream.o vector_int.o iterator.o array.o52 45 53 46 // Local Variables: //
Note:
See TracChangeset
for help on using the changeset viewer.