source: translator/examples/sum.c @ 9df2dd5

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 9df2dd5 was 42dcae7, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

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

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