# Changeset e26a842

Ignore:
Timestamp:
Jul 8, 2024, 9:03:12 AM (2 months ago)
Branches:
master
Children:
061b001
Parents:
5a553e2
Message:

update example programs

Location:
doc/theses/mike_brooks_MMath/programs
Files:
6 edited

Unmodified
Removed
• ## doc/theses/mike_brooks_MMath/programs/bkgd-carray-arrty.c

 r5a553e2 void stx2() { const T x[10]; //            x[5] = 3.14; // bad } } void stx3() { T const x[10]; //            x[5] = 3.14; // bad } } // Local Variables: //
• ## doc/theses/mike_brooks_MMath/programs/hello-accordion.cfa

 r5a553e2 #include #include #include                                                      // setlocale forall( T, [Nclients], [Ncosts] ) struct request { unsigned int requestor_id; array( T, Nclients ) impacted_client_ids; // nested VLA array( float, Ncosts ) cost_contribs; // nested VLA float total_cost; forall( T, @[NprovTerty]@, @[Nmunicipalities]@ ) struct CanadaPop { array( T, @NprovTerty@ ) provTerty; \$\C{// nested VLA}\$ array( T, @Nmunicipalities@ ) municipalities; \$\C{// nested VLA}\$ int total_pt, total_mun; }; // TODO: understand (fix?) why these are needed (autogen seems to be failing ... is typeof as struct member nayok?) forall( T, [Nclients], [Ncosts] ) void ?{}( T &, request( T, Nclients, Ncosts ) & this ) {} forall( T, [NprovTerty], [Nmunicipalities] ) void ?{}( T &, CanadaPop( T, NprovTerty, Nmunicipalities ) & this ) {} forall( T &, [Nclients], [Ncosts] ) void ^?{}( request( T, Nclients, Ncosts ) & this ) {} forall( T &, [NprovTerty], [Nmunicipalities] ) void ^?{}( CanadaPop( T, NprovTerty, Nmunicipalities ) & this ) {} forall( T, [Nclients], [Ncosts] ) void summarize( request( T, Nclients, Ncosts ) & r ) { r.total_cost = 0; for( i; Ncosts ) r.total_cost += r.cost_contribs[i]; // say the cost is per-client, to make output vary r.total_cost *= Nclients; forall( T, [NprovTerty], [Nmunicipalities] ) void check( CanadaPop( T, NprovTerty, Nmunicipalities ) & pop ) with( pop ) { total_pt = total_mun = 0; for ( i; NprovTerty ) total_pt += provTerty[i]; for ( i; Nmunicipalities ) total_mun += municipalities[i]; } int main( int argc, char * argv[] ) { const int ncl = ato( argv[1] ); const int nco = 2; request( int, ncl, nco ) r; r.cost_contribs[0] = 100; r.cost_contribs[1] = 0.1; summarize(r); sout | "Total cost:" | r.total_cost; const int npt = ato( argv[1] ), nmun = ato( argv[2] ); @CanadaPop( int, npt, nmun ) pop;@ // read in population numbers @check( pop );@ sout | setlocale( LC_NUMERIC, getenv( "LANG" ) ); sout | "Total province/territory:" | pop.total_pt; sout | "Total municipalities:" | pop.total_mun; } /* \$\\$\$ ./a.out 5 Total cost: 500.5 \$\\$\$ ./a.out 6 Total cost: 600.6 \$\\$\$ ./a.out  13  3573 Total province/territory: 36,991,981 Total municipalities: 36,991,981 */ // Local Variables: // // compile-command: "sed -f sedcmd hello-accordion.cfa > ../build/tmp.cfa; cfa ../build/tmp.cfa -Wall -Wextra" // // End: //
• ## doc/theses/mike_brooks_MMath/programs/hello-array.cfa

 r5a553e2 forall( [N] ) // array bound array(bool, N) & f( array(float, N) & a, array(float, N) & b ) { array(bool, N) & ret = *alloc(); // sizeof used by alloc for( i; N ) { ret[i] = 0.005 > 2 * (abs(a[i] - b[i])) / (abs(a[i]) + abs(b[i])); forall( [@N@] )                                                         \$\C{// array dimension}\$ array( bool, @N@) & f( array( float, @N@ ) & x, array( float, @N@ ) & y ) { array( bool, @N@ ) & ret = *@alloc@();  \$\C{// sizeof ret  used by alloc}\$ for ( i; @N@ ) { ret[i] = 0.005 > 2 * (abs( x[i] - y[i] )) / (abs( x[i]) + abs(y[i] )); } return ret; int main( int argc, char * argv[] ) { int n = ato( argv[1] ); array(float, n) a, b; // VLA for ( i; n ) { a[i] = 3.14 / (i + 1); b[i] = a[i] + 0.005 ; const int @n@ = ato( argv[1] );                 \$\C{// deduce conversion type}\$ array( float, @n@ ) x, y;                               \$\C{// VLAs}\$ for ( i; n ) {                                                  \$\C{// initialize arrays}\$ x[i] = 3.14 / (i + 1); y[i] = x[i] + 0.005 ; } array(bool, n) & result = f( a, b ); // call sout | "result: " | nonl; array( bool, @n@ ) & result = @f( x, y )@; \$\C{// call}\$ sout | "result: " | nonl;                               \$\C{// print result}\$ for ( i; n ) sout | result[i] | nonl; sout | nl; free( &result ); // free returned storage free( &result );                                                \$\C{// free result storage}\$ } /* void fred() { array(float, 10) a; array(float, 20) b; f( a, a ); f( b, b ); f( a, b ); array( float, @10@ ) x; array( float, @20@ ) y; f( x, x ); f( y, y ); //      f( x, y ); } #ifdef SHOWERR1 forall( [M], [N] ) void bad( array(float, M) &a, array(float, N) &b ) { f( a, a ); // ok f( b, b ); // ok f( a, b ); // error void bad( array(float, M) &x, array(float, N) &y ) { f( x, x );              \$\C[1.5in]{// ok}\$ f( y, y );              \$\C{// ok}\$ f( x, y );              \$\C{// error}\CRT\$ } #endif forall( [M], [N] ) void bad_fixed( array(float, M) & a, array(float, N) & b ) { if ( M == N ) { f( a, (array(float, M) &)b ); // cast b to matching type } void bad_fixed( array( float, M ) & x, array( float, N ) & y ) { if ( M == N ) f( x, @(array( float, M ) &)@y ); \$\C{// cast y to matching type}\$ } // Local Variables: // // compile-command: "sed -f sedcmd hello-array.cfa > ../build/tmp.cfa; cfa ../build/tmp.cfa -Wall -Wextra" // // End: //
• ## doc/theses/mike_brooks_MMath/programs/hello-md.cfa

 r5a553e2 forall( [N] ) void print1d_cstyle( array(float, N) & c ); void print1d_cstyle( array( float, N ) & r ); \$\C{// C style}\$ forall( [N], C & | ar( C, float, N ) ) forall( [N], C & @| ar( C, float, N )@ ) \$\C{// add trait}\$ void print1d( C & c ); forall( [N] ) void print1d_cstyle( array(float, N) & c ) { for ( i; N ) { sout | c[i] | nonl; } void print1d_cstyle( array( float, N ) & r ) { \$\C{// C style}\$ for ( i; N ) sout | r[i] | nonl; sout | nl; } void fill( array(float, 5, 7) & a ) { void fill( array( float, 5, 7 ) & a ) { for ( i; (ptrdiff_t) 5 ) { for ( j; 7 ) { array( float, 5, 7 ) a; fill(a); array( float, 5, 7 ) m; fill( m ); /* 0.0  0.1  0.2  0.3  0.4  0.5  0.6 1.0  1.1  1.2  1.3  1.4  1.5  1.6 2.0  2.1  2.2  2.3  2.4  2.5  2.6 3.0  3.1  3.2  3.3  3.4  3.5  3.6 4.0  4.1  4.2  4.3  4.4  4.5  4.6 r/c   0     1     2     3     4     5     6 0  0.0  0.1  0.2  @0.3@  0.4  0.5  0.6 1  1.0  1.1  1.2  @1.3@  1.4  1.5  1.6 2  @2.0  2.1  2.2  2.3  2.4  2.5  2.6@ 3  3.0  3.1  3.2  @3.3@  3.4  3.5  3.6 4  4.0  4.1  4.2  @4.3@  4.4  4.5  4.6 */ print1d_cstyle( a[ 2 ] );  // 2.0  2.1  2.2  2.3  2.4  2.5  2.6 print1d_cstyle( m[ 2 ] );  \$\C{// row 2:  2.0  2.1  2.2  2.3  2.4  2.5  2.6}\$ print1d( a[ 2 ] );  // 2.0  2.1  2.2  2.3  2.4  2.5  2.6 print1d( m[ 2 ] );  \$\C{// row:  2.0  2.1  2.2  2.3  2.4  2.5  2.6}\$ print1d( a[ 2, all ] );  // 2.0  2.1  2.2  2.3  2.4  2.5  2.6 print1d( a[ all, 3 ] );  // 0.3  1.3  2.3  3.3  4.3 print1d( m[ 2, all ] );  \$\C{// row 2:  2.0  2.1  2.2  2.3  2.4  2.5  2.6}\$ print1d( m[ all, 3 ] );  \$\C{// column 3:  0.3  1.3  2.3  3.3  4.3}\$ print1d_cstyle( a[ 2, all ] ); print1d_cstyle( m[ 2, all ] ); #ifdef SHOW_ERROR_1 print1d_cstyle( a[ all, 2 ] );  // bad print1d_cstyle( m[ all, 2 ] );  \$\C{// bad}\$ #endif
• ## doc/theses/mike_brooks_MMath/programs/lst-issues-attach-reduction.hpp

 r5a553e2 class list { struct node { @LIST_ENTRY(node) links;@ @El elem;@ LIST_ENTRY(node) links; El elem; }; LIST_HEAD(Impl, node); } void push_front( const El & src ) { node * n = @new node()@; node * n = new node(); n->elem = src; LIST_INSERT_HEAD(&impl, n, links);
• ## doc/theses/mike_brooks_MMath/programs/lst-issues-wrapped-byref.run.cpp

 r5a553e2 for (auto const& cur : reqs) printf("{%d %d} ", cur->pri, cur->rqr); printf("\n"); printf("\n"); }
Note: See TracChangeset for help on using the changeset viewer.