Changeset 134b86a
- Timestamp:
- Nov 3, 2014, 4:38:08 PM (9 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:
- 8c17ab0
- Parents:
- 93885663
- Files:
-
- 20 edited
Legend:
- Unmodified
- Added
- Removed
-
driver/cfa.cc
r93885663 r134b86a 8 8 // Created On : Tue Aug 20 13:44:49 2002 9 9 // Last Modified By : Peter A. Buhr 10 // Last Modified On : Fri Oct 17 18:03:15201411 // Update Count : 9 510 // Last Modified On : Sat Oct 18 13:52:43 2014 11 // Update Count : 97 12 12 // 13 13 … … 139 139 } else if ( arg == "-nohelp" ) { 140 140 help = false; // strip the nohelp flag 141 } else if ( arg == "-compiler" ) { 142 // use the user specified compiler 143 i += 1; 144 if ( i == argc ) continue; // next argument available ? 145 compiler_path = argv[i]; 146 if ( putenv( (char *)( *new string( string( "__U_COMPILER__=" ) + argv[i]) ).c_str() ) != 0 ) { 147 cerr << argv[0] << " error, cannot set environment variable." << endl; 148 exit( EXIT_FAILURE ); 149 } // if 141 150 142 151 // C++ specific arguments -
libcfa/prelude.cf
r93885663 r134b86a 325 325 forall(dtype DT) const volatile void * ?=?(const volatile void * volatile *, const volatile DT*); 326 326 327 void * 327 void * ?=?( void * *, void*); 328 328 void * ?=?( void * volatile *, void*); 329 const void * 329 const void * ?=?(const void * *, void*); 330 330 const void * ?=?(const void * volatile *, void*); 331 331 const void * ?=?(const void * *, const void*); -
translator/Parser/cfa.y
r93885663 r134b86a 10 10 * Created On : Sat Sep 1 20:22:55 2001 11 11 * Last Modified By : Peter A. Buhr 12 * Last Modified On : Mon Nov 3 1 1:21:43 200313 * Update Count : 8 5612 * Last Modified On : Mon Nov 3 16:24:24 2014 13 * Update Count : 880 14 14 */ 15 15 … … 2743 2743 /* Local Variables: */ 2744 2744 /* fill-column: 110 */ 2745 /* compile-command: " gmake" */2745 /* compile-command: "make install" */ 2746 2746 /* End: */ -
translator/SymTab/Validate.cc
r93885663 r134b86a 553 553 makeScalarAssignment( ObjectDecl *srcParam, ObjectDecl *dstParam, DeclarationWithType *member, OutputIterator out ) 554 554 { 555 ObjectDecl *obj = dynamic_cast<ObjectDecl *>( member ); // PAB: unnamed bit fields are not copied 556 if ( obj != NULL && obj->get_name() == "" && obj->get_bitfieldWidth() != NULL ) return; 557 555 558 UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) ); 556 559 -
translator/examples/Makefile
r93885663 r134b86a 1 CC=cfa 2 CFLAGS=-g 1 CC=../../bin/cfa 2 CFLAGS = -g -Wunused-function -MD 3 MAKEFILE_NAME = ${firstword ${MAKEFILE_LIST}} # makefile name 3 4 4 %.i: %.c 5 -$(CC) $(CFLAGS) -CFA $< > $@ 5 OBJECTS1 = iostream.o fstream.o fstream_test.o 6 EXEC1 = fstream_test 6 7 7 %.o: %.i 8 $(CC) $(CFLAGS) -c -o $@ $< 8 OBJECTS2 = vector_int.o fstream.o iostream.o array.o iterator.o vector_test.o 9 EXEC2 = vector_test 9 10 10 all: vector_test 11 OBJECTS = ${OBJECTS1} ${OBJECTS2} # all object files 12 DEPENDS = ${OBJECTS:.o=.d} # substitute ".o" with ".d" 13 EXECS = ${EXEC1} ${EXEC2} # all executables 11 14 12 vector_test: vector_test.o vector_int.o fstream.o iostream.o array.o iterator.o 13 fstream_test: fstream_test.o fstream.o iostream.o 15 ########## Targets ########## 14 16 15 array.o: array.i array.h iterator.h 16 iterator.o: iterator.i iterator.h iostream.h 17 vector_test.o: vector_test.i vector_int.h iostream.h fstream.h 18 vector_int.o: vector_int.i vector_int.h 19 fstream_test.o: fstream_test.i iostream.h fstream.h 20 fstream.o: fstream.i iostream.h fstream.h 21 iostream.o: iostream.i iostream.h 17 .PHONY : all clean # not file names 22 18 23 clean: 24 rm -f fstream_test vector_test *.i *.o 19 all : ${EXECS} # build all executables 20 21 ${EXEC1} : ${OBJECTS1} # link step 1st executable 22 ${CC} ${CCFLAGS} $^ -o $@ # additional object files before $^ 23 24 ${EXEC2} : ${OBJECTS2} # link step 2nd executable 25 ${CC} ${CCFLAGS} $^ -o $@ # additional object files before $^ 26 27 ${OBJECTS} : ${MAKEFILE_NAME} # OPTIONAL : changes to this file => recompile 28 29 -include ${DEPENDS} # include *.d files containing program dependences 30 31 clean : # remove files that can be regenerated 32 rm -f ${DEPENDS} ${OBJECTS} ${EXECS} *.class -
translator/examples/array.c
r93885663 r134b86a 10 10 /// { 11 11 /// begin = 0; 12 /// end = array_last( array );12 /// end = last( array ); 13 13 /// } 14 14 15 // The first element is always at index 0. 15 16 forall( type array_type, type elt_type | bounded_array( array_type, elt_type ) ) 16 elt_type * 17 get_begin( array_type array ) 18 { 19 return &array[ 0 ]; 17 elt_type * begin( array_type array ) { 18 return &array[ 0 ]; 20 19 } 21 20 21 // The end iterator should point one past the last element. 22 22 forall( type array_type, type elt_type | bounded_array( array_type, elt_type ) ) 23 elt_type * 24 get_end( array_type array ) 25 { 26 return &array[ array_last( array ) ] + 1; 23 elt_type * end( array_type array ) { 24 return &array[ last( array ) ] + 1; 27 25 } 28 -
translator/examples/array.h
r93885663 r134b86a 2 2 #define ARRAY_H 3 3 4 #include "iterator.h"4 //#include "iterator.h" 5 5 6 context array( type array_type, type elt_type ) 7 { 8 lvalue elt_type ?[?]( array_type, int ); 6 // An array has contiguous elements accessible in any order using integer indicies. The first 7 // element has index 0. 8 context array( type array_type, type elt_type ) { 9 lvalue elt_type ?[?]( array_type, int ); 9 10 }; 10 11 11 context bounded_array( type array_type, type elt_type | array( array_type, elt_type ) ) 12 {13 int array_last( array_type );12 // A bounded array is an array that carries its maximum index with it. 13 context bounded_array( type array_type, type elt_type | array( array_type, elt_type ) ) { 14 int last( array_type ); 14 15 }; 15 16 … … 17 18 18 19 typedef int array_iterator; 20 19 21 /// forall( type array_type, elt_type | bounded_array( array_type, elt_type ) ) 20 22 /// [ array_iterator begin, array_iterator end ] get_iterators( array_type ); 23 24 25 // A bounded array can be iterated over by using a pointer to the element type. These functions 26 // return iterators corresponding to the first element and the one-past-the-end element, STL-style. 21 27 forall( type array_type, type elt_type | bounded_array( array_type, elt_type ) ) 22 elt_type *get_begin( array_type ); 28 elt_type *begin( array_type ); 29 23 30 forall( type array_type, type elt_type | bounded_array( array_type, elt_type ) ) 24 elt_type * get_end( array_type );31 elt_type *end( array_type ); 25 32 26 #endif / * #ifndef ARRAY_H */33 #endif // ARRAY_H -
translator/examples/fstream.c
r93885663 r134b86a 1 1 // "cfa -E fstream.c > fstream_out.c" 2 2 // "cfa -c -o fstream.o fstream.c" 3 3 4 #include "fstream.h" 4 5 5 #undef __cplusplus6 6 extern "C" { 7 7 #include <stdio.h> … … 9 9 } 10 10 11 struct ofstream 12 { 13 FILE *file; 14 int fail; 11 struct ofstream { 12 FILE *file; 13 int fail; 15 14 }; 16 15 17 ofstream * 18 write( ofstream *os, const char *data, streamsize_type size ) 19 { 20 if( !os->fail ) { 21 fwrite( data, size, 1, os->file ); 22 os->fail = ferror( os->file ); 23 } 24 return os; 16 ofstream * write( ofstream *os, const char *data, streamsize_type size ) { 17 if( !os->fail ) { 18 fwrite( data, size, 1, os->file ); 19 os->fail = ferror( os->file ); 20 } 21 return os; 25 22 } 26 23 27 int 28 fail( ofstream *os ) 29 { 30 return os->fail; 24 int fail( ofstream *os ) { 25 return os->fail; 31 26 } 32 27 33 static ofstream* 34 make_ofstream() 35 { 36 ofstream *new_stream = malloc( sizeof( ofstream ) ); 37 new_stream->fail = 0; 38 return new_stream; 28 static ofstream * make_ofstream() { 29 ofstream *new_stream = malloc( sizeof( ofstream ) ); 30 new_stream->fail = 0; 31 return new_stream; 39 32 } 40 33 41 ofstream * 42 ofstream_stdout() 43 { 44 ofstream *stdout_stream = make_ofstream(); 45 stdout_stream->file = stdout; 46 return stdout_stream; 34 ofstream * ofstream_stdout() { 35 ofstream *stdout_stream = make_ofstream(); 36 stdout_stream->file = stdout; 37 return stdout_stream; 47 38 } 48 39 49 ofstream * 50 ofstream_stderr() 51 { 52 ofstream *stderr_stream = make_ofstream(); 53 stderr_stream->file = stderr; 54 return stderr_stream; 40 ofstream * ofstream_stderr() { 41 ofstream *stderr_stream = make_ofstream(); 42 stderr_stream->file = stderr; 43 return stderr_stream; 55 44 } 56 45 57 ofstream * 58 ofstream_fromfile( const char *name ) 59 { 60 ofstream *file_stream = make_ofstream(); 61 file_stream->file = fopen( name, "w" ); 62 file_stream->fail = file_stream->file == 0; 63 return file_stream; 46 ofstream * ofstream_fromfile( const char *name ) { 47 ofstream *file_stream = make_ofstream(); 48 file_stream->file = fopen( name, "w" ); 49 file_stream->fail = file_stream->file == 0; 50 return file_stream; 64 51 } 65 52 66 void 67 ofstream_close( ofstream *os ) 68 { 69 if( os->file != stdout && os->file != stderr ) { 70 os->fail = fclose( os->file ); 71 } 72 free( os ); 53 void ofstream_close( ofstream *os ) { 54 if( os->file != stdout && os->file != stderr ) { 55 os->fail = fclose( os->file ); 56 } 57 free( os ); 73 58 } 74 59 75 struct ifstream 76 { 77 FILE *file; 78 int fail; 79 int eof; 60 struct ifstream { 61 FILE *file; 62 int fail; 63 int eof; 80 64 }; 81 65 82 ifstream * 83 read( ifstream *is, char *data, streamsize_type size ) 84 { 85 if( !is->fail && !is->eof ) { 86 fread( data, size, 1, is->file ); 87 is->fail = ferror( is->file ); 88 is->eof = feof( is->file ); 89 } 90 return is; 66 ifstream * read( ifstream *is, char *data, streamsize_type size ) { 67 if( !is->fail && !is->eof ) { 68 fread( data, size, 1, is->file ); 69 is->fail = ferror( is->file ); 70 is->eof = feof( is->file ); 71 } 72 return is; 91 73 } 92 74 93 ifstream *unread( ifstream *is, char c ) 94 {95 if( !is->fail) {96 if( EOF == ungetc( c, is->file ) ) { 97 is->fail = 1; 75 ifstream *unread( ifstream *is, char c ) { 76 if( !is->fail ) { 77 if( EOF == ungetc( c, is->file ) ) { 78 is->fail = 1; 79 } 98 80 } 99 } 100 return is; 81 return is; 101 82 } 102 83 103 int fail( ifstream *is ) 104 { 105 return is->fail; 84 int fail( ifstream *is ) { 85 return is->fail; 106 86 } 107 87 108 int eof( ifstream *is ) 109 { 110 return is->eof; 88 int eof( ifstream *is ) { 89 return is->eof; 111 90 } 112 91 113 static ifstream* 114 make_ifstream() 115 { 116 ifstream *new_stream = malloc( sizeof( ifstream ) ); 117 new_stream->fail = 0; 118 new_stream->eof = 0; 119 return new_stream; 92 static ifstream * make_ifstream() { 93 ifstream *new_stream = malloc( sizeof( ifstream ) ); 94 new_stream->fail = 0; 95 new_stream->eof = 0; 96 return new_stream; 120 97 } 121 98 122 ifstream *ifstream_stdin() 123 { 124 ifstream *stdin_stream = make_ifstream(); 125 stdin_stream->file = stdin; 126 return stdin_stream; 99 ifstream * ifstream_stdin() { 100 ifstream *stdin_stream = make_ifstream(); 101 stdin_stream->file = stdin; 102 return stdin_stream; 127 103 } 128 104 129 ifstream * 130 ifstream_fromfile( const char *name ) 131 { 132 ifstream *file_stream = make_ifstream(); 133 file_stream->file = fopen( name, "r" ); 134 file_stream->fail = file_stream->file == 0; 135 return file_stream; 105 ifstream * ifstream_fromfile( const char *name ) { 106 ifstream *file_stream = make_ifstream(); 107 file_stream->file = fopen( name, "r" ); 108 file_stream->fail = file_stream->file == 0; 109 return file_stream; 136 110 } 137 -
translator/examples/fstream.h
r93885663 r134b86a 6 6 typedef struct ofstream ofstream; 7 7 8 / * implement context ostream */8 // implement context ostream 9 9 ofstream *write( ofstream *, const char *, streamsize_type ); 10 10 int fail( ofstream * ); … … 17 17 typedef struct ifstream ifstream; 18 18 19 / * implement context istream */19 // implement context istream 20 20 ifstream *read( ifstream *, char *, streamsize_type ); 21 21 ifstream *unread( ifstream *, char ); … … 26 26 ifstream *ifstream_fromfile( const char *name ); 27 27 28 #endif / * #ifndef FSTREAM_H */28 #endif // FSTREAM_H -
translator/examples/fstream_test.c
r93885663 r134b86a 5 5 #include "fstream.h" 6 6 7 int 8 main() 9 { 10 ofstream *sout = ofstream_stdout(); 11 ifstream *sin = ifstream_stdin(); 12 int nombre; 13 sout << "Appuyez un nombre, s'il vous plâit:\n"; 14 sin >> &nombre; 15 sout << "Vous avez appuyé: " << nombre << "\n"; 16 return 0; 7 int main() { 8 ofstream *sout = ofstream_stdout(); 9 ifstream *sin = ifstream_stdin(); 10 int nombre; 11 sout << "Appuyez un nombre, s'il vous plâit:\n"; 12 sin >> &nombre; 13 sout << "Vous avez appuyé: " << nombre << "\n"; 17 14 } -
translator/examples/fwrite.c
r93885663 r134b86a 2 2 // "cfa fwrite.i" 3 3 4 extern "C" { 4 5 #include <stdio.h> 6 } 5 7 6 int 7 main() 8 { 9 fwrite( "test\n", 5, 1, stdout ); 10 return 0; 8 int main() { 9 fwrite( "test\n", 5, 1, stdout ); 11 10 } -
translator/examples/hello.c
r93885663 r134b86a 3 3 // "cfa hello.i fstream.i iostream.o" 4 4 5 extern "C" {6 int printf( const char*, ... );7 }8 9 5 #include "fstream.h" 10 6 11 7 int main() { 12 ofstream *sout = ofstream_stdout(); 13 write( sout, "test\n", 5 ); 14 sout << "Bonjour au monde!\n"; 15 return 0; 8 ofstream *sout = ofstream_stdout(); 9 ifstream *sin = ifstream_stdin(); 10 sout << "Bonjour au monde!\n"; 11 sout << 3 << " " << 3.5 << " " << 'a' << " " << "abc" << "\n"; 12 int i, j, k; 13 sin >> &i >> &j >> &k; 14 sout << "i:" << i << " j:" << j << " k:" << k << "\n"; 16 15 } -
translator/examples/index.h
r93885663 r134b86a 1 context index( type T ) 2 { 3 T ?+?( T, T ); 4 T ?-?( T, T ); 5 const T 0, 1; 1 context index( type T ) { 2 T ?+?( T, T ); 3 T ?-?( T, T ); 4 const T 0, 1; 6 5 }; -
translator/examples/iostream.c
r93885663 r134b86a 6 6 7 7 #include "iostream.h" 8 #undef __cplusplus9 8 extern "C" { 10 #include <string.h>11 #include <ctype.h>12 9 #include <stdio.h> 10 //#include <string.h> 11 //#include <ctype.h> 12 typedef long unsigned int size_t; 13 size_t strlen(const char *s); 13 14 } 14 15 15 16 forall( dtype ostype | ostream( ostype ) ) 16 ostype * 17 ?<<?( ostype *os, char c ) 18 { 19 return write( os, &c, 1 ); 17 ostype * ?<<?( ostype *os, char c ) { 18 return write( os, &c, 1 ); 20 19 } 21 20 22 21 forall( dtype ostype | ostream( ostype ) ) 23 ostype * 24 ?<<?( ostype *os, int i ) 25 { 26 char buffer[20]; // larger than the largest integer 27 sprintf( buffer, "%d", i ); 28 return write( os, buffer, strlen( buffer ) ); 22 ostype * ?<<?( ostype *os, int i ) { 23 char buffer[20]; // larger than the largest integer 24 sprintf( buffer, "%d", i ); 25 return write( os, buffer, strlen( buffer ) ); 29 26 } 30 27 31 28 forall( dtype ostype | ostream( ostype ) ) 32 ostype * 33 ?<<?( ostype *os, const char *cp ) 34 { 35 return write( os, cp, strlen( cp ) ); 29 ostype * ?<<?( ostype *os, double d ) { 30 char buffer[32]; // larger than the largest double 31 sprintf( buffer, "%g", d ); 32 return write( os, buffer, strlen( buffer ) ); 33 } 34 35 forall( dtype ostype | ostream( ostype ) ) 36 ostype * ?<<?( ostype *os, const char *cp ) { 37 return write( os, cp, strlen( cp ) ); 36 38 } 37 39 38 40 forall( dtype istype | istream( istype ) ) 39 istype * 40 ?>>?( istype *is, char *cp ) 41 { 42 return read( is, cp, 1 ); 41 istype * ?>>?( istype *is, char *cp ) { 42 return read( is, cp, 1 ); 43 43 } 44 44 45 45 forall( dtype istype | istream( istype ) ) 46 istype * 47 ?>>?( istype *is, int *ip ) 48 { 49 char cur; 46 istype * ?>>?( istype *is, int *ip ) { 47 char cur; 50 48 51 // skip some whitespace52 do {53 54 55 } while( !( cur >= '0' && cur <= '9' ) );49 // skip some whitespace 50 do { 51 is >> &cur; 52 if( fail( is ) || eof( is ) ) return is; 53 } while( !( cur >= '0' && cur <= '9' ) ); 56 54 57 // accumulate digits58 *ip = 0;59 while( cur >= '0' && cur <= '9' ) {60 61 62 63 }55 // accumulate digits 56 *ip = 0; 57 while( cur >= '0' && cur <= '9' ) { 58 *ip = *ip * 10 + ( cur - '0' ); 59 is >> &cur; 60 if( fail( is ) || eof( is ) ) return is; 61 } 64 62 65 unread( is, cur );66 return is;63 unread( is, cur ); 64 return is; 67 65 } -
translator/examples/iostream.h
r93885663 r134b86a 4 4 typedef unsigned long streamsize_type; 5 5 6 context ostream( dtype ostype ) 7 { 8 ostype *write( ostype *, const char *, streamsize_type ); 9 int fail( ostype * ); 6 context ostream( dtype ostype ) { 7 ostype *write( ostype *, const char *, streamsize_type ); 8 int fail( ostype * ); 10 9 }; 11 10 12 context writeable( type T ) 13 { 14 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, T ); 11 context writeable( type T ) { 12 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, T ); 15 13 }; 16 14 … … 18 16 19 17 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, char ); 20 21 18 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, int ); 22 19 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, double ); 23 20 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, const char * ); 24 21 25 22 26 context istream( dtype istype ) 27 { 28 istype *read( istype *, char *, streamsize_type ); 29 istype *unread( istype *, char ); 30 int fail( istype * ); 31 int eof( istype * ); 23 context istream( dtype istype ) { 24 istype *read( istype *, char *, streamsize_type ); 25 istype *unread( istype *, char ); 26 int fail( istype * ); 27 int eof( istype * ); 32 28 }; 33 29 34 context readable( type T ) 35 { 36 forall( dtype istype | istream( istype ) ) istype * ?<<?( istype *, T ); 30 context readable( type T ) { 31 forall( dtype istype | istream( istype ) ) istype * ?<<?( istype *, T ); 37 32 }; 38 33 39 34 forall( dtype istype | istream( istype ) ) 40 istype * ?>>?( istype *, char * );35 istype * ?>>?( istype *, char * ); 41 36 42 37 forall( dtype istype | istream( istype ) ) 43 istype * ?>>?( istype *, int * );38 istype * ?>>?( istype *, int * ); 44 39 45 #endif / * #ifndef IOSTREAM_H */40 #endif // IOSTREAM_H -
translator/examples/iterator.c
r93885663 r134b86a 18 18 type iterator_type | iterator( iterator_type, elt_type ), 19 19 dtype os_type | ostream( os_type ) ) 20 void 21 write_all( iterator_type begin, iterator_type end, os_type *os ) 22 { 23 iterator_type i; 24 for( i = begin; i != end; ++i ) { 25 os << *i << ' '; 26 } 20 void write_all( iterator_type begin, iterator_type end, os_type *os ) { 21 iterator_type i; 22 for ( i = begin; i != end; ++i ) { 23 os << *i << ' '; 24 } 27 25 } 28 26 27 forall( type elt_type | writeable( elt_type ), 28 type iterator_type | iterator( iterator_type, elt_type ), 29 dtype os_type | ostream( os_type ) ) 30 void write_reverse( iterator_type begin, iterator_type end, os_type *os ) { 31 iterator_type i; /* = end; do not work */ 32 i = end; 33 do { 34 --i; 35 os << *i << ' '; 36 } while ( i != begin ); 37 } -
translator/examples/iterator.h
r93885663 r134b86a 4 4 #include "iostream.h" 5 5 6 context iterator( type iterator_type, type elt_type ) 7 { 8 iterator_type ?++( iterator_type* ); 9 iterator_type ++?( iterator_type* ); 10 int ?==?( iterator_type, iterator_type ); 11 int ?!=?( iterator_type, iterator_type ); 12 lvalue elt_type *?( iterator_type ); 6 // An iterator can be used to traverse a data structure. 7 context iterator( type iterator_type, type elt_type ) { 8 // point to the next element 9 // iterator_type ?++( iterator_type * ); 10 iterator_type ++?( iterator_type * ); 11 iterator_type --?( iterator_type * ); 12 13 // can be tested for equality with other iterators 14 int ?==?( iterator_type, iterator_type ); 15 int ?!=?( iterator_type, iterator_type ); 16 17 // dereference to get the pointed-at element 18 lvalue elt_type *?( iterator_type ); 13 19 }; 14 20 15 context iterator_for( type iterator_type, type collection_type, type elt_type | iterator( iterator_type, elt_type ) ) 16 { 17 /// [ iterator_type begin, iterator_type end ] get_iterators( collection_type ); 18 iterator_type get_begin( collection_type ); 19 iterator_type get_end( collection_type ); 21 context iterator_for( type iterator_type, type collection_type, type elt_type | iterator( iterator_type, elt_type ) ) { 22 // [ iterator_type begin, iterator_type end ] get_iterators( collection_type ); 23 iterator_type begin( collection_type ); 24 iterator_type end( collection_type ); 20 25 }; 21 26 … … 23 28 void for_each( iterator_type begin, iterator_type end, void (*func)( elt_type ) ); 24 29 30 // writes the range [begin, end) to the given stream 25 31 forall( type elt_type | writeable( elt_type ), 26 32 type iterator_type | iterator( iterator_type, elt_type ), … … 28 34 void write_all( iterator_type begin, iterator_type end, os_type *os ); 29 35 30 #endif /* #ifndef ITERATOR_H */ 36 forall( type elt_type | writeable( elt_type ), 37 type iterator_type | iterator( iterator_type, elt_type ), 38 dtype os_type | ostream( os_type ) ) 39 void write_reverse( iterator_type begin, iterator_type end, os_type *os ); 40 41 #endif // ITERATOR_H -
translator/examples/vector_int.c
r93885663 r134b86a 2 2 3 3 #include "vector_int.h" 4 5 #undef __cplusplus6 4 extern "C" { 7 5 #include <stdlib.h> … … 11 9 #define DEFAULT_CAPACITY 20 12 10 13 vector_int 14 vector_int_allocate() 15 { 16 return vector_int_allocate( DEFAULT_CAPACITY ); 11 vector_int vector_int_allocate() { 12 return vector_int_allocate( DEFAULT_CAPACITY ); 17 13 } 18 14 19 vector_int 20 vector_int_allocate( int reserve ) 21 { 22 vector_int new_vector; 23 new_vector.last = -1; 24 new_vector.capacity = reserve; 25 new_vector.data = malloc( sizeof( int ) * reserve ); 26 return new_vector; 15 vector_int vector_int_allocate( int reserve ) { 16 vector_int new_vector; 17 new_vector.last = -1; 18 new_vector.capacity = reserve; 19 new_vector.data = malloc( sizeof( int ) * reserve ); 20 return new_vector; 27 21 } 28 22 29 void 30 vector_int_deallocate( vector_int vec ) 31 { 32 free( vec.data ); 23 void vector_int_deallocate( vector_int vec ) { 24 free( vec.data ); 33 25 } 34 26 35 void 36 reserve( vector_int *vec, int reserve ) 37 { 38 if( reserve > vec->capacity ) { 39 vec->data = realloc( vec->data, sizeof( int ) * reserve ); 40 vec->capacity = reserve; 41 } 27 void reserve( vector_int *vec, int reserve ) { 28 if ( reserve > vec->capacity ) { 29 vec->data = realloc( vec->data, sizeof( int ) * reserve ); 30 vec->capacity = reserve; 31 } 42 32 } 43 33 44 void append( vector_int *vec, int element ) 45 { 46 vec->last++; 47 if( vec->last == vec->capacity ) { 48 vec->capacity *= 2; 49 vec->data = realloc( vec->data, sizeof( int ) * vec->capacity ); 50 } 51 vec->data[ vec->last ] = element; 34 void append( vector_int *vec, int element ) { 35 vec->last++; 36 if ( vec->last == vec->capacity ) { 37 vec->capacity *= 2; 38 vec->data = realloc( vec->data, sizeof( int ) * vec->capacity ); 39 } 40 vec->data[ vec->last ] = element; 52 41 } 53 42 54 43 // implement bounded_array 55 44 56 lvalue int 57 ?[?]( vector_int vec, int index ) 58 { 59 return vec.data[ index ]; 45 lvalue int ?[?]( vector_int vec, int index ) { 46 return vec.data[ index ]; 60 47 } 61 48 62 int 63 array_last( vector_int vec ) 64 { 65 return vec.last; 49 int last( vector_int vec ) { 50 return vec.last; 66 51 } 67 52 -
translator/examples/vector_int.h
r93885663 r134b86a 2 2 #define VECTOR_INT_H 3 3 4 typedef struct vector_int 5 { 6 int last; 7 int capacity; 8 int *data; 4 // A flexible array, similar to a C++ vector, that holds integers and can be resized dynamically 5 6 typedef struct vector_int { 7 int last; // last used index 8 int capacity; // last possible index before reallocation 9 int *data; // array 9 10 } vector_int; 10 11 12 vector_int vector_int_allocate(); // allocate vector with default capacity 13 vector_int vector_int_allocate( int reserve ); // allocate vector with specified capacity 14 void vector_int_deallocate( vector_int ); // deallocate vector's storage 11 15 12 vector_int vector_int_allocate(); 13 vector_int vector_int_allocate( int reserve ); 14 void vector_int_deallocate( vector_int ); 15 16 void reserve( vector_int *vec, int reserve ); 17 void append( vector_int *vec, int element ); 16 void reserve( vector_int *vec, int reserve ); // reserve more capacity 17 void append( vector_int *vec, int element ); // add element to end of vector, resizing as necessary 18 18 19 19 // implement bounded_array 20 20 21 lvalue int ?[?]( vector_int vec, int index ); 22 int array_last( vector_int vec );21 lvalue int ?[?]( vector_int vec, int index ); // access to arbitrary element (does not resize) 22 int last( vector_int vec ); // return last element 23 23 24 #endif / * #ifndef VECTOR_INT_H */24 #endif // VECTOR_INT_H -
translator/examples/vector_test.c
r93885663 r134b86a 7 7 #include "vector_int.h" 8 8 #include "array.h" 9 #include "iterator.h" 9 10 10 extern "C" { 11 int printf( const char *, ... ); 11 int main() { 12 ofstream *sout = ofstream_stdout(); 13 ifstream *sin = ifstream_stdin(); 14 vector_int vec = vector_int_allocate(); 15 16 // read in numbers until EOF or error 17 int num; 18 19 for ( ;; ) { 20 sin >> # 21 if ( fail( sin ) || eof( sin ) ) break; 22 append( &vec, num ); 23 } 24 25 // write out the numbers 26 27 sout << "Array elements:\n"; 28 // write_all( begin( vec ), end( vec ), sout ); 29 sout << "\n"; 30 write_reverse( begin( vec ), end( vec ), sout ); 31 sout << "\n"; 32 33 for ( int index = 0; index <= last( vec ); index += 1 ) { 34 sout << vec[ index ] << " "; 35 } 36 sout << "\n"; 12 37 } 13 38 14 int 15 main() 16 { 17 ofstream *sout = ofstream_stdout(); 18 ifstream *sin = ifstream_stdin(); 19 vector_int vec = vector_int_allocate(); 20 int nombre; 21 for(;;) { 22 sin >> &nombre; 23 if( fail( sin ) || eof( sin ) ) break; 24 append( &vec, nombre ); 25 } 26 sout << "Array elements: "; 27 write_all( get_begin( vec ), get_end( vec ), sout ); 28 /// int index; 29 /// for( index = 0; index <= array_last( vec ); ++index ) { 30 /// sout << vec[ index ] << " "; 31 /// } 32 sout << "\n"; 33 return 0; 34 } 39 // ../bin/cfa vector_test.c fstream.o iostream.o vector_int.o iterator.o array.o
Note: See TracChangeset
for help on using the changeset viewer.