Commit 6bfb01fe
6bfb01fe0149Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
Implement Array#grep / #grep_v on typed arrays
The block-less forms were unresolved for typed arrays. Collect the
elements where pattern === element (grep) or where it does not
(grep_v) into a new array of the source type. Supported patterns: a
Range over an int array (membership), a primitive Class over a poly
array (runtime tag check), and a Regexp over a str array (match). The
analyze and codegen passes enumerate the same (variant, pattern) set;
other combinations and the block form fall through to the unresolved
path.
Refs #1216
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 6bfb01fe0149
- Parent
- 69b6a9a10249
spinel_analyze.rbmodifiedView file
@@ -6209,6 +6209,29 @@
62096209
end62106210
end62116211
end6212+
# `grep`/`grep_v` (no block) return a new array of the source type6213+
# holding the matching elements. Only the forms codegen supports are6214+
# typed here (Range over int, Regexp over str, primitive Class over6215+
# poly); other combinations fall through.6216+
if (mname == "grep" || mname == "grep_v") && @nd_block[nid] < 0 && recv >= 06217+
rt_g = infer_type(recv)6218+
args_g = @nd_arguments[nid] >= 0 ? get_args(@nd_arguments[nid]) : "".split(",", -1)6219+
if args_g.length == 16220+
pt_g = base_type(infer_type(args_g[0]))6221+
if rt_g == "int_array" && pt_g == "range"6222+
return rt_g6223+
end6224+
if rt_g == "str_array" && pt_g == "regexp"6225+
return rt_g6226+
end6227+
if rt_g == "poly_array" && @nd_type[args_g[0]] == "ConstantReadNode"6228+
cn_g = @nd_name[args_g[0]]6229+
if cn_g == "Integer" || cn_g == "String" || cn_g == "Float" || cn_g == "Symbol"6230+
return rt_g6231+
end6232+
end6233+
end6234+
end62126235
if mname == "map"62136236
if recv >= 062146237
# Declare bp inside a scope so infer_type sees the inner element type, not a shadowed outer local.spinel_codegen.rbmodifiedView file
Loading diff…
test/array_grep.rbaddedView file
Loading diff…
test/array_grep.rb.expectedaddedView file
Loading diff…