Changeset 42dcae7 for translator/examples/sum.c
- Timestamp:
- Nov 25, 2014, 9:16:10 AM (9 years ago)
- 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
- 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 5 1 extern "C" { 6 2 int printf( const char *, ... ); … … 9 5 context sumable( type T ) { 10 6 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 ); 14 10 }; 15 11 16 12 forall( type T | sumable( T ) ) 17 13 T 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 + 22 17 return total; 23 18 } 24 19 25 20 int 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; 29 27 } 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 ); 31 38 } 39 40 // Local Variables: // 41 // compile-command: "../../bin/cfa sum.c" // 42 // End: //
Note: See TracChangeset
for help on using the changeset viewer.