Commit d30ff38b
d30ff38bc04bMerged
Ryan Seys <[email protected]> · 5 months ago · 7 files · +2 added · ~5 modified
Description
feat(iter): with_index collecting iterators (map/each/select + offset)
Details
- Commit
- d30ff38bc04b
- Parent
- 5a7a9672f859
src/analyze_infer.cmodifiedView file
@@ -1451,6 +1451,30 @@
14511451
}14521452
}14531453
1454+
/* array.{map,each,select,...}.with_index(off) { |x, i| } result: map collects1455+
the block value (array of body type); each yields the receiver; select/reject1456+
filter, preserving the receiver's array type. */1457+
if (recv >= 0 && !strcmp(name, "with_index") &&1458+
nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "CallNode") &&1459+
nt_ref(nt, recv, "block") < 0) {1460+
const char *inner = nt_str(nt, recv, "name");1461+
int arr_recv = nt_ref(nt, recv, "receiver");1462+
TyKind arr_t = arr_recv >= 0 ? infer_type(c, arr_recv) : TY_UNKNOWN;1463+
if (inner && ty_is_array(arr_t)) {1464+
if (!strcmp(inner, "map") || !strcmp(inner, "collect")) {1465+
int blk = nt_ref(nt, id, "block");1466+
if (blk >= 0) {1467+
int body = nt_ref(nt, blk, "body");1468+
int bn = 0; const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;1469+
return ty_array_of(bn > 0 ? infer_type(c, bb[bn - 1]) : TY_UNKNOWN);1470+
}1471+
}1472+
else if (!strcmp(inner, "each") || !strcmp(inner, "select") ||1473+
!strcmp(inner, "filter") || !strcmp(inner, "reject"))1474+
return arr_t;1475+
}1476+
}1477+
14541478
/* array receiver methods */14551479
if (recv >= 0 && ty_is_array(rt)) {14561480
int block = nt_ref(nt, id, "block");src/analyze_pass.cmodifiedView file
Loading diff…
src/codegen_call.cmodifiedView file
Loading diff…
src/codegen_fold.cmodifiedView file
Loading diff…
src/codegen_internal.hmodifiedView file
Loading diff…
test/with_index.rbaddedView file
Loading diff…
test/with_index.rb.expectedaddedView file
Loading diff…