source: translator/examples/sum.c @ 42dcae7

aaron-thesisarm-ehcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newstringwith_gc
Last change on this file since 42dcae7 was 42dcae7, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

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

  • Property mode set to 100644
File size: 943 bytes
RevLine 
[a0d9f94]1extern "C" {
2    int printf( const char *, ... );
3}
4
5context sumable( type T ) {
6    const T 0;
[42dcae7]7    T ?+?( T, T );
8    T ?++( T * );
9    [T] ?+=?( T *, T );
[a0d9f94]10};
11
12forall( type T | sumable( T ) )
13T sum( int n, T a[] ) {
[42dcae7]14    T total = 0;                        // instantiate T, select 0
15    for ( int i = 0; i < n; i += 1 )
16        total = total + a[i];           // select +
[a0d9f94]17    return total;
18}
19
20int main() {
[42dcae7]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;
[a0d9f94]27    }
[42dcae7]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 );
[a0d9f94]38}
[42dcae7]39
40// Local Variables: //
41// compile-command: "../../bin/cfa sum.c" //
42// End: //
Note: See TracBrowser for help on using the repository browser.