Ignore:
Timestamp:
Nov 25, 2014, 9:16:10 AM (9 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:
3848e0e
Parents:
d11f789
Message:

re-inserted remove and reorder hoisted aggregate, fixed example programs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • translator/examples/sum.c

    rd11f789 r42dcae7  
    1 // "./cfa -g sum.c"
    2 // "./cfa -CFA sum.c > sum_out.c"
    3 // "gcc32 -g sum_out.c LibCfa/libcfa.a"
    4 
    51extern "C" {
    62    int printf( const char *, ... );
     
    95context sumable( type T ) {
    106    const T 0;
    11     T ?+?(T, T);
    12     T ?++(T*);
    13     [T] ?+=?(T*,T);
     7    T ?+?( T, T );
     8    T ?++( T * );
     9    [T] ?+=?( T *, T );
    1410};
    1511
    1612forall( type T | sumable( T ) )
    1713T sum( int n, T a[] ) {
    18     T total = 0;
    19     int i;
    20     for ( i = 0; i < n; i += 1 )
    21         total = total + a[i];
     14    T total = 0;                        // instantiate T, select 0
     15    for ( int i = 0; i < n; i += 1 )
     16        total = total + a[i];           // select +
    2217    return total;
    2318}
    2419
    2520int main() {
    26     int a[10];
    27     for ( int i = 0; i < 10; ++i ) {
    28         a[i] = i;
     21    const int size = 10, low = 0, High = 10;
     22    int si, ai[10]; // size
     23    int i;
     24    for ( i = low; i < High; i += 1 ) {
     25        si += i;
     26        ai[i] = i;
    2927    }
    30     printf( "the sum is %d\n", sum( 10, a ) );
     28    printf( "sum from %d to %d is %d, check %d\n",
     29            low, High, sum( size, ai ), si );
     30    double sd, ad[10]; // size
     31    for ( i = low; i < High; i += 1 ) {
     32        double d = i / (double)size;
     33        sd += d;
     34        ad[i] = d;
     35    }
     36    printf( "sum from %g to %g is %g, check %g\n",
     37            low / (double)size, High / (double)size, sum( size, ad ), sd );
    3138}
     39
     40// Local Variables: //
     41// compile-command: "../../bin/cfa sum.c" //
     42// End: //
Note: See TracChangeset for help on using the changeset viewer.