1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // loopctrl.cfa -- |
---|
8 | // |
---|
9 | // Author : Peter A. Buhr |
---|
10 | // Created On : Wed Aug 8 18:32:59 2018 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Thu Jun 27 11:00:09 2024 |
---|
13 | // Update Count : 161 |
---|
14 | // |
---|
15 | |
---|
16 | #include <fstream.hfa> |
---|
17 | |
---|
18 | void fred() { |
---|
19 | // Test all possible loop syntax. |
---|
20 | |
---|
21 | int s = 0, c = 10, i = 2; |
---|
22 | |
---|
23 | for ( c ) { sout | "A"; } sout | nl; |
---|
24 | for ( ~= c ) { sout | "B"; } sout | nl; |
---|
25 | for ( -~ c ) { sout | "C"; } sout | nl; |
---|
26 | for ( -~= c ) { sout | "D"; } sout | nl; |
---|
27 | |
---|
28 | for ( s ~ c ) { sout | "A"; } sout | nl; |
---|
29 | for ( s ~= c ) { sout | "B"; } sout | nl; |
---|
30 | for ( s -~ c ) { sout | "C"; } sout | nl; |
---|
31 | for ( s -~= c ) { sout | "D"; } sout | nl; |
---|
32 | |
---|
33 | for ( s ~ c ~ i ) { sout | "A"; } sout | nl; |
---|
34 | for ( s ~= c ~ i ) { sout | "B"; } sout | nl; |
---|
35 | for ( s -~ c ~ i ) { sout | "C"; } sout | nl; |
---|
36 | for ( s -~= c ~ i ) { sout | "D"; } sout | nl; |
---|
37 | |
---|
38 | for ( j; c ) { sout | j; } sout | nl; |
---|
39 | for ( j; ~= c ) { sout | j; } sout | nl; |
---|
40 | for ( j; -~ c ) { sout | j; } sout | nl; |
---|
41 | for ( j; -~= c ) { sout | j; } sout | nl; |
---|
42 | |
---|
43 | for ( j; s ~ c ) { sout | j; } sout | nl; |
---|
44 | for ( j; s ~= c ) { sout | j; } sout | nl; |
---|
45 | for ( j; s -~ c ) { sout | j; } sout | nl; |
---|
46 | for ( j; s -~= c ) { sout | j; } sout | nl; |
---|
47 | |
---|
48 | for ( j; s ~ c ~ i ) { sout | j; } sout | nl; |
---|
49 | for ( j; s ~= c ~ i ) { sout | j; } sout | nl; |
---|
50 | for ( j; s -~ c ~ i ) { sout | j; } sout | nl; |
---|
51 | for ( j; s -~= c ~ i ) { sout | j; } sout | nl; |
---|
52 | |
---|
53 | // CANNOT DIRECTLY INITIALIZE INDEX VARIABLE, ONLY SINGLE LOOP INDEX VARIABLE IN DECLARATION |
---|
54 | |
---|
55 | for ( j; c ) { sout | j; } sout | nl; |
---|
56 | for ( int j; c ) { sout | j; } sout | nl; |
---|
57 | for ( int j; ~= c ) { sout | j; } sout | nl; |
---|
58 | for ( int j; -~ c ) { sout | j; } sout | nl; |
---|
59 | for ( int j; -~= c ) { sout | j; } sout | nl; |
---|
60 | |
---|
61 | for ( int j; s ~ c ) { sout | j; } sout | nl; |
---|
62 | for ( int j; s ~= c ) { sout | j; } sout | nl; |
---|
63 | for ( int j; s -~ c ) { sout | j; } sout | nl; |
---|
64 | for ( int j; s -~= c ) { sout | j; } sout | nl; |
---|
65 | |
---|
66 | for ( int j; s ~ c ~ i ) { sout | j; } sout | nl; |
---|
67 | for ( int j; s ~= c ~ i ) { sout | j; } sout | nl; |
---|
68 | for ( int j; s -~ c ~ i ) { sout | j; } sout | nl; |
---|
69 | for ( int j; s -~= c ~ i ) { sout | j; } sout | nl; |
---|
70 | |
---|
71 | for ( j; s ~ @ ) { if ( j == 10 ) break; sout | j; } sout | nl; |
---|
72 | for ( j; @ -~ s ) { if ( j == -10 ) break; sout | j; } sout | nl; |
---|
73 | for ( j; s ~ @ ~ i ) { if ( j == 10 ) break; sout | j; } sout | nl; |
---|
74 | for ( j; @ -~ s ~ i ) { if ( j == -10 ) break; sout | j; } sout | nl; |
---|
75 | for ( j; s ~ @ ~ @ ) { if ( j == 10 ) break; sout | j; j += 1; } sout | nl; |
---|
76 | |
---|
77 | for ( int j; s ~ @ ) { if ( j == 10 ) break; sout | j; j += 1; } sout | nl; |
---|
78 | for ( int j; @ -~ s ) { if ( j == -10 ) break; sout | j; j -= 1; } sout | nl; |
---|
79 | for ( int j; s ~ @ ~ i ) { if ( j == 10 ) break; sout | j; } sout | nl; |
---|
80 | for ( int j; @ -~ s ~ i ) { if ( j == -10 ) break; sout | j; } sout | nl; |
---|
81 | for ( int j; s ~ @ ~ @ ) { if ( j == 10 ) break; sout | j; j += 1; } sout | nl; |
---|
82 | |
---|
83 | // enum E { A, B, C, D }; |
---|
84 | // for ( e; A ~= C ) { sout | j; } |
---|
85 | // for ( e; A ~= D ) { sout | j; } |
---|
86 | // for ( e; A -~= D ~ 2 ) { sout | j; } |
---|
87 | // for ( e; E ) { sout | j; } |
---|
88 | // for ( e; -~ E ) { sout | j; } |
---|
89 | } |
---|
90 | |
---|
91 | struct S { int i, j; }; |
---|
92 | void ?{}( S & s ) { s.[i, j] = 0; } |
---|
93 | void ?{}( S & s, int i ) { s.[i, j] = [i, 0]; } |
---|
94 | void ?{}( S & s, int i, int j ) { s.[i, j] = [i, j]; } |
---|
95 | void ?{}( S & s, zero_t ) { s.[i, j] = 0; } |
---|
96 | void ?{}( S & s, one_t ) { s.[i, j] = 1; } |
---|
97 | int ?<?( S t1, S t2 ) { return t1.i < t2.i && t1.j < t2.j; } |
---|
98 | int ?<=?( S t1, S t2 ) { return t1.i <= t2.i && t1.j <= t2.j; } |
---|
99 | int ?>?( S t1, S t2 ) { return t1.i > t2.i && t1.j > t2.j; } |
---|
100 | int ?>=?( S t1, S t2 ) { return t1.i >= t2.i && t1.j >= t2.j; } |
---|
101 | S ?+=?( S & t1, S t2 ) { t1.i += t2.i; t1.j += t2.j; return t1; } |
---|
102 | S ?+=?( S & t, one_t ) { t.i += 1; t.j += 1; return t; } |
---|
103 | S ?-=?( S & t1, S t2 ) { t1.i -= t2.i; t1.j -= t2.j; return t1; } |
---|
104 | S ?-=?( S & t, one_t ) { t.i -= 1; t.j -= 1; return t; } |
---|
105 | ofstream & ?|?( ofstream & os, S v ) { return os | '(' | v.i | v.j | ')'; } |
---|
106 | void & ?|?( ofstream & os, S v ) { (ofstream &)(os | v); ends( os ); } |
---|
107 | |
---|
108 | int main() { |
---|
109 | // Test some loop options. |
---|
110 | |
---|
111 | sout | nlOff; // turn off auto newline |
---|
112 | while () { sout | "empty"; break; } sout | nl; |
---|
113 | do { sout | "empty"; break; } while (); sout | nl; |
---|
114 | for () { sout | "empty"; break; } sout | nl | nl; |
---|
115 | |
---|
116 | for ( 0 ) { sout | "A"; } sout | "zero" | nl; |
---|
117 | for ( 1 ) { sout | "A"; } sout | nl; |
---|
118 | for ( 10 ) { sout | "A"; } sout | nl; |
---|
119 | for ( ~= 10 ) { sout | "A"; } sout | nl; |
---|
120 | for ( 1 ~= 10 ~ 2 ) { sout | "B"; } sout | nl; |
---|
121 | for ( 1 -~= 10 ~ 2 ) { sout | "C"; } sout | nl; |
---|
122 | for ( 0.5 ~ 5.5 ) { sout | "D"; } sout | nl; |
---|
123 | for ( 0.5 -~ 5.5 ) { sout | "E"; } sout | nl | nl; |
---|
124 | |
---|
125 | for ( i; 10 ) { sout | i; } sout | nl; |
---|
126 | for ( i; ~= 10 ) { sout | i; } sout | nl; |
---|
127 | for ( i; 1 ~= 10 ~ 2 ) { sout | i; } sout | nl; |
---|
128 | for ( i; 1 -~= 10 ~ 2 ) { sout | i; } sout | nl; |
---|
129 | for ( i; 0.5 ~ 5.5 ) { sout | i; } sout | nl; |
---|
130 | for ( i; 0.5 -~ 5.5 ) { sout | i; } sout | nl; |
---|
131 | for ( ui; 2u ~= 10u ~ 2u ) { sout | ui; } sout | nl; |
---|
132 | for ( ui; 2u -~= 10u ~ 2u ) { sout | ui; } sout | nl | nl; |
---|
133 | |
---|
134 | // @ means do nothing |
---|
135 | for ( i; 1 ~ @ ) { |
---|
136 | if ( i > 10 ) break; |
---|
137 | sout | i; |
---|
138 | } sout | nl; |
---|
139 | for ( i; @ -~ 10 ) { |
---|
140 | if ( i < 0 ) break; |
---|
141 | sout | i; |
---|
142 | } sout | nl; |
---|
143 | for ( i; 2 ~ @ ~ 2 ) { |
---|
144 | if ( i > 10 ) break; |
---|
145 | sout | i; |
---|
146 | } sout | nl; |
---|
147 | for ( i; 2.1 ~ @ ~ @ ) { |
---|
148 | if ( i > 10.5 ) break; |
---|
149 | sout | i; |
---|
150 | i += 1.7; |
---|
151 | } sout | nl; |
---|
152 | for ( i; @ -~ 10 ~ 2 ) { |
---|
153 | if ( i < 0 ) break; |
---|
154 | sout | i; |
---|
155 | } sout | nl; |
---|
156 | for ( i; 12.1 ~ @ ~ @ ) { |
---|
157 | if ( i < 2.5 ) break; |
---|
158 | sout | i; |
---|
159 | i -= 1.7; |
---|
160 | } sout | nl | nl; |
---|
161 | |
---|
162 | enum { N = 10 }; |
---|
163 | for ( N ) { sout | "N"; } sout | nl; |
---|
164 | for ( i; N ) { sout | i; } sout | nl; |
---|
165 | for ( i; ~= N ) { sout | i; } sout | nl; |
---|
166 | for ( i; -~= N ) { sout | i; } sout | nl | nl; |
---|
167 | |
---|
168 | const int start = 3, comp = 10, inc = 2; |
---|
169 | for ( i; start ~ comp ~ inc + 1 ) { sout | i; } sout | nl | nl; |
---|
170 | |
---|
171 | for ( i; 10 : j; -5 ~ @ ) { sout | i | j; } sout | nl; |
---|
172 | for ( i; 10 : j; @ -~ -5 ) { sout | i | j; } sout | nl; |
---|
173 | for ( i; 10 : j; -5 ~ @ ~ 2 ) { sout | i | j; } sout | nl; |
---|
174 | for ( i; 10 : j; @ -~ -5 ~ 2 ) { sout | i | j; } sout | nl | nl; |
---|
175 | |
---|
176 | for ( j; -5 ~ @ : i; 10 ) { sout | i | j; } sout | nl; |
---|
177 | for ( j; @ -~ -5 : i; 10 ) { sout | i | j; } sout | nl; |
---|
178 | for ( j; -5 ~ @ ~ 2 : i; 10 ) { sout | i | j; } sout | nl; |
---|
179 | for ( j; @ -~ -5 ~ 2 : i; 10 ) { sout | i | j; } sout | nl | nl; |
---|
180 | |
---|
181 | for ( j; @ -~ -5 ~ 2 : i; 10 : k; 1.5 ~ @ ) { sout | i | j | k; } sout | nl; |
---|
182 | for ( j; @ -~ -5 ~ 2 : k; 1.5 ~ @ : i; 10 ) { sout | i | j | k; } sout | nl; |
---|
183 | for ( k; 1.5 ~ @ : j; @ -~ -5 ~ 2 : i; 10 ) { sout | i | j | k; } sout | nl; |
---|
184 | |
---|
185 | for ( S s = (S){0}; s < (S){10,10}; s += (S){1} ) { sout | s; } sout | nl; |
---|
186 | for ( s; (S){10,10} ) { sout | s; } sout | nl; |
---|
187 | sout | nl; |
---|
188 | for ( s; (S){0} ~ (S){10,10} ) { sout | s; } sout | nl; |
---|
189 | for ( s; (S){0} ~ (S){10,10} ~ (S){1} ) { sout | s; } sout | nl; |
---|
190 | for ( s; (S){0} ~= (S){10,10} ) { sout | s; } sout | nl; |
---|
191 | for ( s; (S){0} ~= (S){10,10} ~ (S){1} ) { sout | s; } sout | nl; |
---|
192 | sout | nl; |
---|
193 | for ( s; (S){0} -~ (S){10,10} ) { sout | s; } sout | nl; |
---|
194 | for ( s; (S){0} -~ (S){10,10} ~ (S){1} ) { sout | s; } sout | nl; |
---|
195 | for ( s; (S){0} -~= (S){10,10} ) { sout | s; } sout | nl; |
---|
196 | for ( s; (S){0} -~= (S){10,10} ~ (S){1} ) { sout | s; } sout | nl | nl; |
---|
197 | |
---|
198 | fred(); |
---|
199 | } |
---|
200 | |
---|
201 | // Local Variables: // |
---|
202 | // tab-width: 4 // |
---|
203 | // compile-command: "cfa loopctrl.cfa" // |
---|
204 | // End: // |
---|