Changeset 134b86a


Ignore:
Timestamp:
Nov 3, 2014, 4:38:08 PM (8 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
aaron-thesis, arm-eh, 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, resolv-new, string, with_gc
Children:
8c17ab0
Parents:
93885663
Message:

add compiler flag to driver, update examples, fix unnamed bit fields

Files:
20 edited

Legend:

Unmodified
Added
Removed
  • driver/cfa.cc

    r93885663 r134b86a  
    88// Created On       : Tue Aug 20 13:44:49 2002
    99// Last Modified By : Peter A. Buhr
    10 // Last Modified On : Fri Oct 17 18:03:15 2014
    11 // Update Count     : 95
     10// Last Modified On : Sat Oct 18 13:52:43 2014
     11// Update Count     : 97
    1212//
    1313
     
    139139            } else if ( arg == "-nohelp" ) {
    140140                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
    141150
    142151            // C++ specific arguments
  • libcfa/prelude.cf

    r93885663 r134b86a  
    325325forall(dtype DT) const volatile void *   ?=?(const volatile void * volatile *, const volatile DT*);
    326326
    327 void *             ?=?(               void *          *,                void*);
     327void *            ?=?(               void *          *,                void*);
    328328void *            ?=?(               void * volatile *,                void*);
    329 const void *       ?=?(const          void *          *,                void*);
     329const void *      ?=?(const          void *          *,                void*);
    330330const void *      ?=?(const          void * volatile *,                void*);
    331331const void *      ?=?(const          void *          *, const          void*);
  • translator/Parser/cfa.y

    r93885663 r134b86a  
    1010 * Created On       : Sat Sep  1 20:22:55 2001
    1111 * Last Modified By : Peter A. Buhr
    12  * Last Modified On : Mon Nov  3 11:21:43 2003
    13  * Update Count     : 856
     12 * Last Modified On : Mon Nov  3 16:24:24 2014
     13 * Update Count     : 880
    1414 */
    1515
     
    27432743/* Local Variables: */
    27442744/* fill-column: 110 */
    2745 /* compile-command: "gmake" */
     2745/* compile-command: "make install" */
    27462746/* End: */
  • translator/SymTab/Validate.cc

    r93885663 r134b86a  
    553553makeScalarAssignment( ObjectDecl *srcParam, ObjectDecl *dstParam, DeclarationWithType *member, OutputIterator out )
    554554{
     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
    555558  UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) );
    556559 
  • translator/examples/Makefile

    r93885663 r134b86a  
    1 CC=cfa
    2 CFLAGS=-g
     1CC=../../bin/cfa
     2CFLAGS = -g -Wunused-function -MD
     3MAKEFILE_NAME = ${firstword ${MAKEFILE_LIST}}   # makefile name
    34
    4 %.i: %.c
    5         -$(CC) $(CFLAGS) -CFA $< > $@
     5OBJECTS1 = iostream.o fstream.o fstream_test.o
     6EXEC1 = fstream_test
    67
    7 %.o: %.i
    8         $(CC) $(CFLAGS) -c -o $@ $<
     8OBJECTS2 = vector_int.o fstream.o iostream.o array.o iterator.o vector_test.o
     9EXEC2 = vector_test
    910
    10 all: vector_test
     11OBJECTS = ${OBJECTS1} ${OBJECTS2}               # all object files
     12DEPENDS = ${OBJECTS:.o=.d}                      # substitute ".o" with ".d"
     13EXECS = ${EXEC1} ${EXEC2}                       # all executables
    1114
    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 ##########
    1416
    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
    2218
    23 clean:
    24         rm -f fstream_test vector_test *.i *.o
     19all : ${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
     31clean :                                         # remove files that can be regenerated
     32        rm -f ${DEPENDS} ${OBJECTS} ${EXECS} *.class
  • translator/examples/array.c

    r93885663 r134b86a  
    1010/// {
    1111///   begin = 0;
    12 ///   end = array_last( array );
     12///   end = last( array );
    1313/// }
    1414
     15// The first element is always at index 0.
    1516forall( 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 ];
     17elt_type * begin( array_type array ) {
     18    return &array[ 0 ];
    2019}
    2120
     21// The end iterator should point one past the last element.
    2222forall( 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;
     23elt_type * end( array_type array ) {
     24    return &array[ last( array ) ] + 1;
    2725}
    28 
  • translator/examples/array.h

    r93885663 r134b86a  
    22#define ARRAY_H
    33
    4 #include "iterator.h"
     4//#include "iterator.h"
    55
    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.
     8context array( type array_type, type elt_type ) {
     9    lvalue elt_type ?[?]( array_type, int );
    910};
    1011
    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.
     13context bounded_array( type array_type, type elt_type | array( array_type, elt_type ) ) {
     14    int last( array_type );
    1415};
    1516
     
    1718
    1819typedef int array_iterator;
     20
    1921/// forall( type array_type, elt_type | bounded_array( array_type, elt_type ) )
    2022/// [ 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.
    2127forall( type array_type, type elt_type | bounded_array( array_type, elt_type ) )
    22 elt_type *get_begin( array_type );
     28elt_type *begin( array_type );
     29
    2330forall( type array_type, type elt_type | bounded_array( array_type, elt_type ) )
    24 elt_type *get_end( array_type );
     31elt_type *end( array_type );
    2532
    26 #endif /* #ifndef ARRAY_H */
     33#endif // ARRAY_H
  • translator/examples/fstream.c

    r93885663 r134b86a  
    11// "cfa -E fstream.c > fstream_out.c"
    22// "cfa -c -o fstream.o fstream.c"
     3
    34#include "fstream.h"
    45
    5 #undef __cplusplus
    66extern "C" {
    77#include <stdio.h>
     
    99}
    1010
    11 struct ofstream
    12 {
    13   FILE *file;
    14   int fail;
     11struct ofstream {
     12    FILE *file;
     13    int fail;
    1514};
    1615
    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;
     16ofstream * 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;
    2522}
    2623
    27 int
    28 fail( ofstream *os )
    29 {
    30   return os->fail;
     24int fail( ofstream *os ) {
     25    return os->fail;
    3126}
    3227
    33 static ofstream*
    34 make_ofstream()
    35 {
    36   ofstream *new_stream = malloc( sizeof( ofstream ) );
    37   new_stream->fail = 0;
    38   return new_stream;
     28static ofstream * make_ofstream() {
     29    ofstream *new_stream = malloc( sizeof( ofstream ) );
     30    new_stream->fail = 0;
     31    return new_stream;
    3932}
    4033
    41 ofstream *
    42 ofstream_stdout()
    43 {
    44   ofstream *stdout_stream = make_ofstream();
    45   stdout_stream->file = stdout;
    46   return stdout_stream;
     34ofstream * ofstream_stdout() {
     35    ofstream *stdout_stream = make_ofstream();
     36    stdout_stream->file = stdout;
     37    return stdout_stream;
    4738}
    4839
    49 ofstream *
    50 ofstream_stderr()
    51 {
    52   ofstream *stderr_stream = make_ofstream();
    53   stderr_stream->file = stderr;
    54   return stderr_stream;
     40ofstream * ofstream_stderr() {
     41    ofstream *stderr_stream = make_ofstream();
     42    stderr_stream->file = stderr;
     43    return stderr_stream;
    5544}
    5645
    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;
     46ofstream * 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;
    6451}
    6552
    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 );
     53void ofstream_close( ofstream *os ) {
     54    if( os->file != stdout && os->file != stderr ) {
     55        os->fail = fclose( os->file );
     56    }
     57    free( os );
    7358}
    7459
    75 struct ifstream
    76 {
    77   FILE *file;
    78   int fail;
    79   int eof;
     60struct ifstream {
     61    FILE *file;
     62    int fail;
     63    int eof;
    8064};
    8165
    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;
     66ifstream * 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;
    9173}
    9274 
    93 ifstream *unread( ifstream *is, char c )
    94 {
    95   if( !is->fail ) {
    96     if( EOF == ungetc( c, is->file ) ) {
    97       is->fail = 1;
     75ifstream *unread( ifstream *is, char c ) {
     76    if( !is->fail ) {
     77        if( EOF == ungetc( c, is->file ) ) {
     78            is->fail = 1;
     79        }
    9880    }
    99   }
    100   return is;
     81    return is;
    10182}
    10283
    103 int fail( ifstream *is )
    104 {
    105   return is->fail;
     84int fail( ifstream *is ) {
     85    return is->fail;
    10686}
    10787
    108 int eof( ifstream *is )
    109 {
    110   return is->eof;
     88int eof( ifstream *is ) {
     89    return is->eof;
    11190}
    11291
    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;
     92static ifstream * make_ifstream() {
     93    ifstream *new_stream = malloc( sizeof( ifstream ) );
     94    new_stream->fail = 0;
     95    new_stream->eof = 0;
     96    return new_stream;
    12097}
    12198
    122 ifstream *ifstream_stdin()
    123 {
    124   ifstream *stdin_stream = make_ifstream();
    125   stdin_stream->file = stdin;
    126   return stdin_stream;
     99ifstream * ifstream_stdin() {
     100    ifstream *stdin_stream = make_ifstream();
     101    stdin_stream->file = stdin;
     102    return stdin_stream;
    127103}
    128104
    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;
     105ifstream * 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;
    136110}
    137 
  • translator/examples/fstream.h

    r93885663 r134b86a  
    66typedef struct ofstream ofstream;
    77
    8 /* implement context ostream */
     8// implement context ostream
    99ofstream *write( ofstream *, const char *, streamsize_type );
    1010int fail( ofstream * );
     
    1717typedef struct ifstream ifstream;
    1818
    19 /* implement context istream */
     19// implement context istream
    2020ifstream *read( ifstream *, char *, streamsize_type );
    2121ifstream *unread( ifstream *, char );
     
    2626ifstream *ifstream_fromfile( const char *name );
    2727
    28 #endif /* #ifndef FSTREAM_H */
     28#endif // FSTREAM_H
  • translator/examples/fstream_test.c

    r93885663 r134b86a  
    55#include "fstream.h"
    66
    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;
     7int 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";
    1714}
  • translator/examples/fwrite.c

    r93885663 r134b86a  
    22// "cfa fwrite.i"
    33
     4extern "C" {
    45#include <stdio.h>
     6}
    57
    6 int
    7 main()
    8 {
    9   fwrite( "test\n", 5, 1, stdout );
    10   return 0;
     8int main() {
     9    fwrite( "test\n", 5, 1, stdout );
    1110}
  • translator/examples/hello.c

    r93885663 r134b86a  
    33// "cfa hello.i fstream.i iostream.o"
    44
    5 extern "C" {
    6 int printf( const char*, ... );
    7 }
    8 
    95#include "fstream.h"
    106
    117int 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";
    1615}
  • 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;
     1context index( type T ) {
     2    T ?+?( T, T );
     3    T ?-?( T, T );
     4    const T 0, 1;
    65};
  • translator/examples/iostream.c

    r93885663 r134b86a  
    66
    77#include "iostream.h"
    8 #undef __cplusplus
    98extern "C" {
    10 #include <string.h>
    11 #include <ctype.h>
    129#include <stdio.h>
     10//#include <string.h>
     11//#include <ctype.h>
     12typedef long unsigned int size_t;
     13size_t strlen(const char *s);
    1314}
    1415
    1516forall( dtype ostype | ostream( ostype ) )
    16 ostype *
    17 ?<<?( ostype *os, char c )
    18 {
    19   return write( os, &c, 1 );
     17ostype * ?<<?( ostype *os, char c ) {
     18    return write( os, &c, 1 );
    2019}
    2120
    2221forall( 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 ) );
     22ostype * ?<<?( 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 ) );
    2926}
    3027
    3128forall( dtype ostype | ostream( ostype ) )
    32 ostype *
    33 ?<<?( ostype *os, const char *cp )
    34 {
    35   return write( os, cp, strlen( cp ) );
     29ostype * ?<<?( 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
     35forall( dtype ostype | ostream( ostype ) )
     36ostype * ?<<?( ostype *os, const char *cp ) {
     37    return write( os, cp, strlen( cp ) );
    3638}
    3739
    3840forall( dtype istype | istream( istype ) )
    39 istype *
    40 ?>>?( istype *is, char *cp )
    41 {
    42   return read( is, cp, 1 );
     41istype * ?>>?( istype *is, char *cp ) {
     42    return read( is, cp, 1 );
    4343}
    4444
    4545forall( dtype istype | istream( istype ) )
    46 istype *
    47 ?>>?( istype *is, int *ip )
    48 {
    49   char cur;
     46istype * ?>>?( istype *is, int *ip ) {
     47    char cur;
    5048 
    51   // skip some whitespace
    52   do {
    53     is >> &cur;
    54     if( fail( is ) || eof( is ) ) return is;
    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' ) );
    5654 
    57   // accumulate digits
    58   *ip = 0;
    59   while( cur >= '0' && cur <= '9' ) {
    60     *ip = *ip * 10 + ( cur - '0' );
    61     is >> &cur;
    62     if( fail( is ) || eof( is ) ) return is;
    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    }
    6462 
    65   unread( is, cur );
    66   return is;
     63    unread( is, cur );
     64    return is;
    6765}
  • translator/examples/iostream.h

    r93885663 r134b86a  
    44typedef unsigned long streamsize_type;
    55
    6 context ostream( dtype ostype )
    7 {
    8   ostype *write( ostype *, const char *, streamsize_type );
    9   int fail( ostype * );
     6context ostream( dtype ostype ) {
     7    ostype *write( ostype *, const char *, streamsize_type );
     8    int fail( ostype * );
    109};
    1110
    12 context writeable( type T )
    13 {
    14   forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, T );
     11context writeable( type T ) {
     12    forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, T );
    1513};
    1614
     
    1816
    1917forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, char );
    20 
    2118forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, int );
    22 
     19forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, double );
    2320forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, const char * );
    2421
    2522
    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 * );
     23context istream( dtype istype ) {
     24    istype *read( istype *, char *, streamsize_type );
     25    istype *unread( istype *, char );
     26    int fail( istype * );
     27    int eof( istype * );
    3228};
    3329
    34 context readable( type T )
    35 {
    36   forall( dtype istype | istream( istype ) ) istype * ?<<?( istype *, T );
     30context readable( type T ) {
     31    forall( dtype istype | istream( istype ) ) istype * ?<<?( istype *, T );
    3732};
    3833
    3934forall( dtype istype | istream( istype ) )
    40 istype * ?>>?( istype *, char* );
     35istype * ?>>?( istype *, char * );
    4136
    4237forall( dtype istype | istream( istype ) )
    43 istype * ?>>?( istype *, int* );
     38istype * ?>>?( istype *, int * );
    4439
    45 #endif /* #ifndef IOSTREAM_H */
     40#endif // IOSTREAM_H
  • translator/examples/iterator.c

    r93885663 r134b86a  
    1818        type iterator_type | iterator( iterator_type, elt_type ),
    1919        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   }
     20void 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    }
    2725}
    2826
     27forall( type elt_type | writeable( elt_type ),
     28        type iterator_type | iterator( iterator_type, elt_type ),
     29        dtype os_type | ostream( os_type ) )
     30void 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  
    44#include "iostream.h"
    55
    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.
     7context 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 );
    1319};
    1420
    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 );
     21context 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 );
    2025};
    2126
     
    2328void for_each( iterator_type begin, iterator_type end, void (*func)( elt_type ) );
    2429
     30// writes the range [begin, end) to the given stream
    2531forall( type elt_type | writeable( elt_type ),
    2632        type iterator_type | iterator( iterator_type, elt_type ),
     
    2834void write_all( iterator_type begin, iterator_type end, os_type *os );
    2935
    30 #endif /* #ifndef ITERATOR_H */
     36forall( type elt_type | writeable( elt_type ),
     37        type iterator_type | iterator( iterator_type, elt_type ),
     38        dtype os_type | ostream( os_type ) )
     39void write_reverse( iterator_type begin, iterator_type end, os_type *os );
     40
     41#endif // ITERATOR_H
  • translator/examples/vector_int.c

    r93885663 r134b86a  
    22
    33#include "vector_int.h"
    4 
    5 #undef __cplusplus
    64extern "C" {
    75#include <stdlib.h>
     
    119#define DEFAULT_CAPACITY 20
    1210
    13 vector_int
    14 vector_int_allocate()
    15 {
    16   return vector_int_allocate( DEFAULT_CAPACITY );
     11vector_int vector_int_allocate() {
     12    return vector_int_allocate( DEFAULT_CAPACITY );
    1713}
    1814
    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;
     15vector_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;
    2721}
    2822
    29 void
    30 vector_int_deallocate( vector_int vec )
    31 {
    32   free( vec.data );
     23void vector_int_deallocate( vector_int vec ) {
     24    free( vec.data );
    3325}
    3426
    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   }
     27void 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    }
    4232}
    4333
    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;
     34void 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;
    5241}
    5342
    5443// implement bounded_array
    5544
    56 lvalue int
    57 ?[?]( vector_int vec, int index )
    58 {
    59   return vec.data[ index ];
     45lvalue int ?[?]( vector_int vec, int index ) {
     46    return vec.data[ index ];
    6047}
    6148
    62 int
    63 array_last( vector_int vec )
    64 {
    65   return vec.last;
     49int last( vector_int vec ) {
     50    return vec.last;
    6651}
    6752
  • translator/examples/vector_int.h

    r93885663 r134b86a  
    22#define VECTOR_INT_H
    33
    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
     6typedef struct vector_int {
     7    int last;                                           // last used index
     8    int capacity;                                       // last possible index before reallocation
     9    int *data;                                          // array
    910} vector_int;
    1011
     12vector_int vector_int_allocate();                       // allocate vector with default capacity
     13vector_int vector_int_allocate( int reserve );          // allocate vector with specified capacity
     14void vector_int_deallocate( vector_int );               // deallocate vector's storage
    1115
    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 );
     16void reserve( vector_int *vec, int reserve );           // reserve more capacity
     17void append( vector_int *vec, int element );            // add element to end of vector, resizing as necessary
    1818
    1919// implement bounded_array
    2020
    21 lvalue int ?[?]( vector_int vec, int index );
    22 int array_last( vector_int vec );
     21lvalue int ?[?]( vector_int vec, int index );           // access to arbitrary element (does not resize)
     22int last( vector_int vec );                             // return last element
    2323
    24 #endif /* #ifndef VECTOR_INT_H */
     24#endif // VECTOR_INT_H
  • translator/examples/vector_test.c

    r93885663 r134b86a  
    77#include "vector_int.h"
    88#include "array.h"
     9#include "iterator.h"
    910
    10 extern "C" {
    11 int printf( const char *, ... );
     11int 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 >> &num;
     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";
    1237}
    1338
    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.