Commit 37a993fa
37a993fa0d85Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
codegen: lower lazy range chains to a bounded pull-loop
Support (a..b).lazy[.select/.reject/.filter{blk}].first(n) and the
bare-first form. The whole chain compiles to one loop that pulls
elements from the range begin, applies the optional filter inline,
and stops at the first count, so an unbounded source such as
(1..Float::INFINITY) terminates. The terminal types as int_array
for first(n) and int for first, in both analyze and codegen, so a
trailing .inspect dispatches as an array.
Scope is the documented case: a literal range source, zero or one select/reject/filter stage, and a first terminal. map/collect, non-literal ranges, multi-stage chains and other terminals fall through to the existing unresolved-call path.
Fixes #897
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 37a993fa0d85
- Parent
- c07eac2cfdd5
spinel_analyze.rbmodifiedView file
"" end # True when `n` is the `Float::INFINITY` constant-path node. def is_float_infinity(n) if n < 0 return 0 end if @nd_name[n] != "INFINITY" return 0 end r = @nd_receiver[n] if r >= 0 && resolve_const_ref_name(r) == "Float" return 1 end 0 end # Recognise `<literal-range>.lazy[.select/.reject/.filter{blk}].first` # where `nid` is the terminal `.first` CallNode. Returns the source # RangeNode nid, or -1 when the chain is not a supported lazy chain. # Mirror of codegen's lazy_chain_range. def lazy_chain_range(nid) recv = @nd_receiver[nid] if recv < 0 || @nd_type[recv] != "CallNode" return -1 end rn = @nd_name[recv] lazy_nid = -1 if rn == "lazy" && @nd_block[recv] < 0 lazy_nid = recv elsif (rn == "select" || rn == "filter" || rn == "reject") && @nd_block[recv] >= 0 inner = @nd_receiver[recv] if inner >= 0 && @nd_type[inner] == "CallNode" && @nd_name[inner] == "lazy" && @nd_block[inner] < 0 lazy_nid = inner end end if lazy_nid < 0 return -1 end src = @nd_receiver[lazy_nid] if src < 0 return -1 end rng = -1 if @nd_type[src] == "RangeNode" rng = src elsif @nd_type[src] == "ParenthesesNode" pb = @nd_body[src] if pb >= 0 ps = get_stmts(pb) if ps.length > 0 && @nd_type[ps.first] == "RangeNode" rng = ps.first end end end if rng < 0 return -1 end if @nd_left[rng] < 0 || @nd_type[@nd_left[rng]] == "NilNode" return -1 end rng end def infer_call_type(nid) mname = @nd_name[nid] recv = @nd_receiver[nid] # Lazy range chain `(a..b).lazy[.select/.reject/.filter{blk}].first`. # first(n) materialises an int_array, bare first an int. codegen # lowers the whole chain to a bounded pull-loop; here we only need # the terminal's type so a following `.inspect` dispatches as array. if mname == "first" && lazy_chain_range(nid) >= 0 args_id_lz = @nd_arguments[nid] if args_id_lz >= 0 aa_lz = get_args(args_id_lz) if aa_lz.length > 0 return "int_array" end end return "int" end if mname == "block_given?" if recv < 0 || (recv >= 0 && @nd_type[recv] == "SelfNode") return "bool"spinel_codegen.rbmodifiedView file
Loading diff…
test/i897.rbaddedView file
Loading diff…
test/i897.rb.expectedaddedView file
Loading diff…