[dab9fb93] | 1 | #include <fstream.hfa> |
---|
| 2 | #include <stdlib.hfa> |
---|
| 3 | #include <array.hfa> // learned has to come afer stdlib, which uses the word tag |
---|
[bbf6a180] | 4 | |
---|
| 5 | // Usage: |
---|
| 6 | // ./a.out 5 |
---|
| 7 | // example does an unchecked reference to argv[1] |
---|
| 8 | |
---|
| 9 | |
---|
[e26a842] | 10 | forall( [@N@] ) $\C{// array dimension}$ |
---|
[caa3e2c] | 11 | array( bool, @N@ ) & f( array( float, @N@ ) & x, array( float, @N@ ) & y ) { |
---|
[e26a842] | 12 | array( bool, @N@ ) & ret = *@alloc@(); $\C{// sizeof ret used by alloc}$ |
---|
| 13 | for ( i; @N@ ) { |
---|
| 14 | ret[i] = 0.005 > 2 * (abs( x[i] - y[i] )) / (abs( x[i]) + abs(y[i] )); |
---|
[5546f50b] | 15 | } |
---|
| 16 | return ret; |
---|
[bbf6a180] | 17 | } |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | // TODO: standardize argv |
---|
| 29 | |
---|
[dab9fb93] | 30 | int main( int argc, char * argv[] ) { |
---|
[caa3e2c] | 31 | const int @dim@ = ato( argv[1] ); $\C{// deduce conversion type}$ |
---|
| 32 | array( float, @dim@ ) x, y; $\C{// VLAs}$ |
---|
| 33 | for ( i; dim ) { $\C{// initialize arrays}$ |
---|
[e26a842] | 34 | x[i] = 3.14 / (i + 1); |
---|
| 35 | y[i] = x[i] + 0.005 ; |
---|
[5546f50b] | 36 | } |
---|
[caa3e2c] | 37 | array( bool, @dim@ ) & result = @f( x, y )@; $\C{// call}$ |
---|
[e26a842] | 38 | sout | "result: " | nonl; $\C{// print result}$ |
---|
[caa3e2c] | 39 | for ( i; dim ) |
---|
[5546f50b] | 40 | sout | result[i] | nonl; |
---|
| 41 | sout | nl; |
---|
[e26a842] | 42 | free( &result ); $\C{// free result storage}$ |
---|
[bbf6a180] | 43 | } |
---|
| 44 | /* |
---|
[dab9fb93] | 45 | $\$$ ./a.out 5 |
---|
| 46 | result: true true true false false |
---|
| 47 | $\$$ ./a.out 7 |
---|
| 48 | result: true true true false false false false |
---|
[bbf6a180] | 49 | */ |
---|
| 50 | |
---|
[dab9fb93] | 51 | void fred() { |
---|
[e26a842] | 52 | array( float, @10@ ) x; |
---|
| 53 | array( float, @20@ ) y; |
---|
| 54 | f( x, x ); |
---|
| 55 | f( y, y ); |
---|
| 56 | // f( x, y ); |
---|
[bbf6a180] | 57 | } |
---|
| 58 | |
---|
| 59 | #ifdef SHOWERR1 |
---|
[dab9fb93] | 60 | forall( [M], [N] ) |
---|
[e26a842] | 61 | void bad( array(float, M) &x, array(float, N) &y ) { |
---|
| 62 | f( x, x ); $\C[1.5in]{// ok}$ |
---|
| 63 | f( y, y ); $\C{// ok}$ |
---|
| 64 | f( x, y ); $\C{// error}\CRT$ |
---|
[bbf6a180] | 65 | } |
---|
| 66 | #endif |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | |
---|
[dab9fb93] | 70 | forall( [M], [N] ) |
---|
[e26a842] | 71 | void bad_fixed( array( float, M ) & x, array( float, N ) & y ) { |
---|
| 72 | if ( M == N ) |
---|
| 73 | f( x, @(array( float, M ) &)@y ); $\C{// cast y to matching type}$ |
---|
[dab9fb93] | 74 | } |
---|
[e26a842] | 75 | |
---|
| 76 | // Local Variables: // |
---|
| 77 | // compile-command: "sed -f sedcmd hello-array.cfa > ../build/tmp.cfa; cfa ../build/tmp.cfa -Wall -Wextra" // |
---|
| 78 | // End: // |
---|