28
28
#include " chpl/uast/Record.h"
29
29
#include " chpl/uast/Variable.h"
30
30
31
- // TODO:
32
- // - Slices
33
-
34
31
static void testArray (std::string domainType,
35
32
std::string eltType) {
36
33
std::string arrayText;
@@ -169,6 +166,41 @@ static void testArrayLiteral(std::string arrayLiteral, std::string domainType,
169
166
assert (guard.realizeErrors () == 0 );
170
167
}
171
168
169
+ static void testArraySlicing (std::string arrayLiteral, std::string sliceArgs,
170
+ std::string resultType) {
171
+ printf (" Testing slicing array %s with %s\n " , arrayLiteral.c_str (),
172
+ sliceArgs.c_str ());
173
+
174
+ Context* context = buildStdContext ();
175
+ ErrorGuard guard (context);
176
+
177
+ auto vars = resolveTypesOfVariables (context,
178
+ R"""(
179
+ module M {
180
+ var A = )""" + arrayLiteral + R"""( ;
181
+
182
+ var mySlice = A[)""" + sliceArgs + R"""( ];
183
+
184
+ type expectedSliceTy = )""" + resultType + R"""( ;
185
+ param correctSliceType = mySlice.type == expectedSliceTy;
186
+ }
187
+ )""" ,
188
+ {" A" , " mySlice" , " correctSliceType" });
189
+ auto arrType = vars.at (" A" );
190
+ assert (arrType.type ());
191
+ assert (!arrType.type ()->isUnknownType ());
192
+ assert (arrType.type ()->isArrayType ());
193
+
194
+ auto sliceType = vars.at (" mySlice" );
195
+ assert (sliceType.type ());
196
+ assert (!sliceType.type ()->isUnknownType ());
197
+ assert (sliceType.type ()->isArrayType ());
198
+
199
+ ensureParamBool (vars.at (" correctSliceType" ), true );
200
+
201
+ assert (guard.realizeErrors () == 0 );
202
+ }
203
+
172
204
int main () {
173
205
// rectangular
174
206
testArray (" domain(1)" , " int" );
@@ -189,6 +221,11 @@ int main() {
189
221
testArrayLiteral (" [1;]" , " domain(2)" , " int" );
190
222
testArrayLiteral (" [1, 2; 3, 4;; 5, 6; 7, 8]" , " domain(3)" , " int" );
191
223
224
+ // slices
225
+ testArraySlicing (" [1, 2, 3]" , " 0..1" , " [1..2] int" );
226
+ testArraySlicing (" [1, 2; 3, 4]" , " 0, 1" , " int" ); // multi-dim indexing, not really a slice
227
+ testArraySlicing (" [1, 2; 3, 4;; 5, 6; 7, 8]" , " 0..1, 0..1, 1" , " [0..1, 0..1] int" );
228
+
192
229
// associative
193
230
testArray (" domain(int)" , " int" );
194
231
testArray (" domain(int, true)" , " int" );
0 commit comments