1 | #include <fstream.hfa>
|
---|
2 | #include <stdlib.hfa>
|
---|
3 | #include <array.hfa> // learned has to come afer stdlib, which uses the word tag
|
---|
4 |
|
---|
5 | // Usage:
|
---|
6 | // ./a.out 5
|
---|
7 | // example does an unchecked reference to argv[1]
|
---|
8 |
|
---|
9 |
|
---|
10 | forall( [@N@] ) $\C{// array dimension}$
|
---|
11 | array( bool, @N@ ) & f( array( float, @N@ ) & x, array( float, @N@ ) & y ) {
|
---|
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] ));
|
---|
15 | }
|
---|
16 | return ret;
|
---|
17 | }
|
---|
18 |
|
---|
19 |
|
---|
20 |
|
---|
21 |
|
---|
22 |
|
---|
23 |
|
---|
24 |
|
---|
25 |
|
---|
26 |
|
---|
27 |
|
---|
28 | // TODO: standardize argv
|
---|
29 |
|
---|
30 | int main( int argc, char * argv[] ) {
|
---|
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}$
|
---|
34 | x[i] = 3.14 / (i + 1);
|
---|
35 | y[i] = x[i] + 0.005 ;
|
---|
36 | }
|
---|
37 | array( bool, @dim@ ) & result = @f( x, y )@; $\C{// call}$
|
---|
38 | sout | "result: " | nonl; $\C{// print result}$
|
---|
39 | for ( i; dim )
|
---|
40 | sout | result[i] | nonl;
|
---|
41 | sout | nl;
|
---|
42 | free( &result ); $\C{// free result storage}$
|
---|
43 | }
|
---|
44 | /*
|
---|
45 | $\$$ ./a.out 5
|
---|
46 | result: true true true false false
|
---|
47 | $\$$ ./a.out 7
|
---|
48 | result: true true true false false false false
|
---|
49 | */
|
---|
50 |
|
---|
51 |
|
---|
52 |
|
---|
53 |
|
---|
54 |
|
---|
55 |
|
---|
56 |
|
---|
57 |
|
---|
58 |
|
---|
59 |
|
---|
60 |
|
---|
61 |
|
---|
62 |
|
---|
63 |
|
---|
64 | #ifdef SHOWERR1
|
---|
65 |
|
---|
66 | void fred() {
|
---|
67 |
|
---|
68 |
|
---|
69 |
|
---|
70 | array( float, @10@ ) x;
|
---|
71 | array( float, @20@ ) y;
|
---|
72 | f( x, x ); $\C[0.5in]{// ok}$
|
---|
73 | f( y, y ); $\C{// ok}$
|
---|
74 | f( x, y ); $\C{// error}\CRT$
|
---|
75 |
|
---|
76 |
|
---|
77 |
|
---|
78 |
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 |
|
---|
83 |
|
---|
84 |
|
---|
85 |
|
---|
86 |
|
---|
87 |
|
---|
88 |
|
---|
89 |
|
---|
90 | forall( [M], [N] )
|
---|
91 | void bad( array(float, M) &x,
|
---|
92 | array(float, N) &y ) {
|
---|
93 | f( x, x ); $\C[0.5in]{// ok}$
|
---|
94 | f( y, y ); $\C{// ok}$
|
---|
95 |
|
---|
96 | f( x, y ); $\C{// error}\CRT$
|
---|
97 | }
|
---|
98 |
|
---|
99 | #endif
|
---|
100 |
|
---|
101 |
|
---|
102 |
|
---|
103 |
|
---|
104 |
|
---|
105 |
|
---|
106 |
|
---|
107 |
|
---|
108 |
|
---|
109 |
|
---|
110 | forall( [M], [N] )
|
---|
111 | void bad_fixed( array( float, M ) & x,
|
---|
112 | array( float, N ) & y ) {
|
---|
113 | f( x, x ); $\C[0.5in]{// ok}$
|
---|
114 | f( y, y ); $\C{// ok}$
|
---|
115 | if ( M == N )
|
---|
116 | f( x, @(array( float, M ) &)@y ); $\C{// ok}\CRT$
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 |
|
---|
121 |
|
---|
122 |
|
---|
123 |
|
---|
124 | void fred_ok_only() {
|
---|
125 | array( float, @10@ ) x;
|
---|
126 | array( float, @20@ ) y;
|
---|
127 | f( x, x );
|
---|
128 | f( y, y );
|
---|
129 | // f( x, y );
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | forall( [M], [N] )
|
---|
134 | void bad_ok_only( array(float, M) &x,
|
---|
135 | array(float, N) &y ) {
|
---|
136 | f( x, x );
|
---|
137 | f( y, y );
|
---|
138 |
|
---|
139 | // f( x, y );
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | // Local Variables: //
|
---|
144 | // compile-command: "sed -f sedcmd hello-array.cfa > ../build/tmp.cfa; cfa ../build/tmp.cfa -Wall -Wextra" //
|
---|
145 | // End: //
|
---|