#include #include #include // learned has to come afer stdlib, which uses the word tag // Usage: // ./a.out 5 // example does an unchecked reference to argv[1] 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; } // TODO: standardize argv int main( int argc, char * argv[] ) { const int @dim@ = ato( argv[1] ); $\C{// deduce conversion type}$ array( float, @dim@ ) x, y; $\C{// VLAs}$ for ( i; dim ) { $\C{// initialize arrays}$ x[i] = 3.14 / (i + 1); y[i] = x[i] + 0.005 ; } array( bool, @dim@ ) & result = @f( x, y )@; $\C{// call}$ sout | "result: " | nonl; $\C{// print result}$ for ( i; dim ) sout | result[i] | nonl; sout | nl; free( &result ); $\C{// free result storage}$ } /* $\$$ ./a.out 5 result: true true true false false $\$$ ./a.out 7 result: true true true false false false false */ #ifdef SHOWERR1 void fred() { array( float, @10@ ) x; array( float, @20@ ) y; f( x, x ); $\C[0.5in]{// ok}$ f( y, y ); $\C{// ok}$ f( x, y ); $\C{// error}\CRT$ } forall( [M], [N] ) void bad( array(float, M) &x, array(float, N) &y ) { f( x, x ); $\C[0.5in]{// ok}$ f( y, y ); $\C{// ok}$ f( x, y ); $\C{// error}\CRT$ } #endif forall( [M], [N] ) void bad_fixed( array( float, M ) & x, array( float, N ) & y ) { f( x, x ); $\C[0.5in]{// ok}$ f( y, y ); $\C{// ok}$ if ( M == N ) f( x, @(array( float, M ) &)@y ); $\C{// ok}$ } void fred_ok_only() { array( float, @10@ ) x; array( float, @20@ ) y; f( x, x ); f( y, y ); // f( x, y ); } forall( [M], [N] ) void bad_ok_only( array(float, M) &x, array(float, N) &y ) { f( x, x ); f( y, y ); // f( x, y ); } // Local Variables: // // compile-command: "sed -f sedcmd hello-array.cfa > ../build/tmp.cfa; cfa ../build/tmp.cfa -Wall -Wextra" // // End: //