Changeset cf16f94 for src/examples


Ignore:
Timestamp:
Dec 15, 2015, 4:09:13 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:
4389966
Parents:
b0b958a
Message:

create temporary return variable for return expressions, remove unnecessary code after temporary-return-variable change, fix missing lvalue qualifiers, change stream operator to |

Location:
src/examples
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • src/examples/fstream_test.c

    rb0b958a rcf16f94  
    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:13:43 2015
    13 // Update Count     : 2
     12// Last Modified On : Mon Nov 23 14:43:32 2015
     13// Update Count     : 40
    1414//
    1515
     
    2020        ifstream *sin = ifstream_stdin();
    2121        int nombre;
    22         sout << "Appuyez un nombre, s'il vous plâit:\n";
    23         sin >> &nombre;
    24         sout << "Vous avez appuyé: " << nombre << "\n";
     22        sout | "Entrez un nombre, s'il vous plaît:\n";
     23        sin  | &nombre;
     24        sout | "Vous avez entré " | nombre | " stocké à l'adresse " | &nombre | endl;
     25        sout | "nombre " | nombre | " est "
     26                 | (nombre > 0 ? "plus grand que" :
     27                   nombre == 0 ? "égal à" : "moins de")
     28                 | " zéro" | endl;
     29
     30        sout | "Entrez trois nombres, s'il vous plaît:\n";
     31        int i, j, k;
     32        sin  | &i | &j | &k;
     33        sout | "Vous avez entré " | "i:" | i | " j:" | j | " k:" | k | endl;
     34
     35        sout | 3 | ' ' | 3.5 | ' ' | 'a' | ' ' | "abc" | endl;
    2536}
    2637
  • src/examples/hello.c

    rb0b958a rcf16f94  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Nov 20 16:02:50 2015
    13 // Update Count     : 3
     12// Last Modified On : Sun Nov 22 17:40:37 2015
     13// Update Count     : 5
    1414//
    1515
     
    1919        ofstream *sout = ofstream_stdout();
    2020        ifstream *sin = ifstream_stdin();
    21         sout << "Bonjour au monde!\n";
    22         sout << 3 << " " << 3.5 << " " << 'a' << " " << "abc" << "\n";
    23         int i, j, k;
    24         sin >> &i >> &j >> &k;
    25         sout << "i:" << i << " j:" << j << " k:" << k << "\n";
     21        sout | "Bonjour au monde!\n";
    2622}
    2723
  • src/examples/iostream.c

    rb0b958a rcf16f94  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Nov 20 13:19:19 2015
    13 // Update Count     : 9
     12// Last Modified On : Mon Dec  7 23:08:02 2015
     13// Update Count     : 24
    1414//
    1515
     
    2121
    2222forall( dtype ostype | ostream( ostype ) )
    23 ostype * ?<<?( ostype *os, char c ) {
     23ostype * ?|?( ostype *os, char c ) {
    2424        return write( os, &c, 1 );
    25 } // ?<<?
     25} // ?|?
    2626
    2727forall( dtype ostype | ostream( ostype ) )
    28 ostype * ?<<?( ostype *os, int i ) {
     28ostype * ?|?( ostype *os, int i ) {
    2929        char buffer[32];                                                                        // larger than the largest integer
    3030        sprintf( buffer, "%d", i );
    3131        return write( os, buffer, strlen( buffer ) );
    32 } // ?<<?
     32} // ?|?
    3333
    3434forall( dtype ostype | ostream( ostype ) )
    35 ostype * ?<<?( ostype *os, double d ) {
     35ostype * ?|?( ostype *os, double d ) {
    3636        char buffer[32];                                                                        // larger than the largest double
    3737        sprintf( buffer, "%g", d );
    3838        return write( os, buffer, strlen( buffer ) );
    39 } // ?<<?
     39} // ?|?
    4040
    4141forall( dtype ostype | ostream( ostype ) )
    42 ostype * ?<<?( ostype *os, const char *cp ) {
     42ostype * ?|?( ostype *os, const char *cp ) {
    4343        return write( os, cp, strlen( cp ) );
    44 } // ?<<?
     44} // ?|?
    4545
    4646forall( dtype ostype | ostream( ostype ) )
    47 ostype * ?<<?( ostype *os, const void *p ) {
     47ostype * ?|?( ostype *os, const void *p ) {
    4848        char buffer[32];                                                                        // larger than the largest pointer
    4949        sprintf( buffer, "%p", p );
    5050        return write( os, buffer, strlen( buffer ) );
    51 } // ?<<?
     51} // ?|?
     52
     53
     54forall( dtype ostype, dtype retostype | ostream( ostype ) | ostream( retostype ) )
     55retostype * ?|?( ostype *os, retostype * (*manip)(ostype*) ) {
     56  return manip(os);
     57}
     58
     59forall( dtype ostype | ostream( ostype ) )
     60ostype * endl( ostype * os ) {
     61  os | "\n";
     62  // flush
     63  return os;
     64} // endl
    5265
    5366forall( type elt_type | writeable( elt_type ),
     
    5669void write( iterator_type begin, iterator_type end, os_type *os ) {
    5770        void print( elt_type i ) {
    58                 os << i << ' ';
     71                os | i | ' ';
    5972        }
    6073        for_each( begin, end, print );
    61 } // ?<<?
     74} // ?|?
    6275
    6376forall( type elt_type | writeable( elt_type ),
     
    6578                dtype os_type | ostream( os_type ) )
    6679void write_reverse( iterator_type begin, iterator_type end, os_type *os ) {
    67         void print( elt_type i ) {
    68                 os << i << ' ';
    69         }
     80        void print( elt_type i ) { os | i | ' '; }
    7081        for_each_reverse( begin, end, print );
    71 } // ?<<?
     82} // ?|?
    7283
    7384
    7485forall( dtype istype | istream( istype ) )
    75 istype * ?>>?( istype *is, char *cp ) {
     86istype * ?|?( istype *is, char *cp ) {
    7687        return read( is, cp, 1 );
    77 } // ?>>?
     88} // ?|?
    7889
    7990forall( dtype istype | istream( istype ) )
    80 istype * ?>>?( istype *is, int *ip ) {
     91istype * ?|?( istype *is, int *ip ) {
    8192        char cur;
    8293 
    8394        // skip some whitespace
    8495        do {
    85                 is >> &cur;
     96                is | &cur;
    8697                if ( fail( is ) || eof( is ) ) return is;
    8798        } while ( !( cur >= '0' && cur <= '9' ) );
     
    91102        while ( cur >= '0' && cur <= '9' ) {
    92103                *ip = *ip * 10 + ( cur - '0' );
    93                 is >> &cur;
     104                is | &cur;
    94105                if ( fail( is ) || eof( is ) ) return is;
    95106        }
     
    97108        unread( is, cur );
    98109        return is;
    99 } // ?>>?
     110} // ?|?
    100111
    101112// Local Variables: //
  • src/examples/iostream.h

    rb0b958a rcf16f94  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Nov 19 17:56:51 2015
    13 // Update Count     : 5
     12// Last Modified On : Mon Nov 23 14:15:25 2015
     13// Update Count     : 17
    1414//
    1515
     
    2727
    2828context writeable( type T ) {
    29         forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, T );
     29        forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, T );
    3030};
    3131
    3232// implement writable for some intrinsic types
    3333
    34 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, char );
    35 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, int );
    36 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, double );
    37 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, const char * );
    38 forall( dtype ostype | ostream( ostype ) ) ostype * ?<<?( ostype *, void * );
     34forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, char );
     35forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, int );
     36forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, double );
     37forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, const char * );
     38forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, const void * );
     39
     40forall( dtype ostype, dtype retostype | ostream( ostype ) | ostream( retostype ) ) retostype * ?|?( ostype *os, retostype * (* manip)(ostype*) );
     41forall( dtype ostype | ostream( ostype ) ) ostype * endl( ostype * );
    3942
    4043// writes the range [begin, end) to the given stream
     
    4952void write_reverse( iterator_type begin, iterator_type end, os_type *os );
    5053
     54//******************************************************************************
    5155
    5256context istream( dtype istype ) {
     
    5862
    5963context readable( type T ) {
    60         forall( dtype istype | istream( istype ) ) istype * ?<<?( istype *, T );
     64        forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, T );
    6165};
    6266
    6367forall( dtype istype | istream( istype ) )
    64 istype * ?>>?( istype *, char * );
     68istype * ?|?( istype *, char * );
    6569
    6670forall( dtype istype | istream( istype ) )
    67 istype * ?>>?( istype *, int * );
     71istype * ?|?( istype *, int * );
    6872
    6973#endif // IOSTREAM_H
  • src/examples/sum.c

    rb0b958a rcf16f94  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Sep 28 15:09:55 2015
    13 // Update Count     : 118
     12// Last Modified On : Sat Nov 21 18:08:18 2015
     13// Update Count     : 119
    1414//
    1515
     
    5353        }
    5454        sout << "sum from " << low << " to " << High << " is "
    55                  << (int)sum( size, a ) << ", check " << (int)s << "\n";
     55                 << (int)sum( size, a ) << ", check " << (int)s << endl;
    5656
    5757        int s = 0, a[size];
     
    6262        }
    6363        sout << "sum from " << low << " to " << High << " is "
    64                  << sum( size, (int *)a ) << ", check " << (int)s << "\n";
     64                 << sum( size, (int *)a ) << ", check " << (int)s << endl;
    6565
    6666        double s = 0.0, a[size];
     
    7272        printf( "%g\n", sum( size, (double *)a ) );
    7373//      sout << "sum from " << low / 10.0 << " to " << High / 10.0 << " is "
    74 //               << sum( size, (double *)a ) << ", check " << (double)s << "\n";
     74//               << sum( size, (double *)a ) << ", check " << (double)s << endl;
    7575
    7676        float s = 0.0, a[size];
     
    8282        printf( "%g\n", sum( size, (float *)a ) );
    8383//      sout << "sum from " << low / 10.0 << " to " << High / 10.0 << " is "
    84 //               << sum( size, (float *)a ) << ", check " << (float)s << "\n";
     84//               << sum( size, (float *)a ) << ", check " << (float)s << endl;
    8585}
    8686
  • src/examples/vector_test.c

    rb0b958a rcf16f94  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Nov 19 17:54:34 2015
    13 // Update Count     : 9
     12// Last Modified On : Tue Dec 15 16:02:56 2015
     13// Update Count     : 13
    1414//
    1515
     
    2727        int num;
    2828
    29         sout << "enter N elements and C-d on a separate line:\n";
     29        sout | "enter N elements and C-d on a separate line:" | endl;
    3030        for ( ;; ) {
    31                 sin >> &num;
     31                sin | &num;
    3232          if ( fail( sin ) || eof( sin ) ) break;
    3333                append( &vec, num );
     
    3535        // write out the numbers
    3636
    37         sout << "Array elements:\n";
     37        sout | "Array elements:" | endl;
    3838        write( begin( vec ), end( vec ), sout );
    39         sout << "\n";
     39        sout | endl;
    4040
    41         sout << "Array elements reversed:\n";
     41        sout | "Array elements reversed:" | endl;
    4242        write_reverse( begin( vec ), end( vec ), sout );
    43         sout << "\n";
     43        sout | endl;
    4444}
    4545
Note: See TracChangeset for help on using the changeset viewer.