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 end
62106210 end
62116211 end
6212+ # `grep`/`grep_v` (no block) return a new array of the source type
6213+ # holding the matching elements. Only the forms codegen supports are
6214+ # typed here (Range over int, Regexp over str, primitive Class over
6215+ # poly); other combinations fall through.
6216+ if (mname == "grep" || mname == "grep_v") && @nd_block[nid] < 0 && recv >= 0
6217+ rt_g = infer_type(recv)
6218+ args_g = @nd_arguments[nid] >= 0 ? get_args(@nd_arguments[nid]) : "".split(",", -1)
6219+ if args_g.length == 1
6220+ pt_g = base_type(infer_type(args_g[0]))
6221+ if rt_g == "int_array" && pt_g == "range"
6222+ return rt_g
6223+ end
6224+ if rt_g == "str_array" && pt_g == "regexp"
6225+ return rt_g
6226+ end
6227+ 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_g
6231+ end
6232+ end
6233+ end
6234+ end
62126235 if mname == "map"
62136236 if recv >= 0
62146237 # Declare bp inside a scope so infer_type sees the inner element type, not a shadowed outer local.