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

@@ -3081,10 +3081,87 @@
30813081 ""
30823082 end
30833083
3084+ # True when `n` is the `Float::INFINITY` constant-path node.
3085+ def is_float_infinity(n)
3086+ if n < 0
3087+ return 0
3088+ end
3089+ if @nd_name[n] != "INFINITY"
3090+ return 0
3091+ end
3092+ r = @nd_receiver[n]
3093+ if r >= 0 && resolve_const_ref_name(r) == "Float"
3094+ return 1
3095+ end
3096+ 0
3097+ end
3098+
3099+ # Recognise `<literal-range>.lazy[.select/.reject/.filter{blk}].first`
3100+ # where `nid` is the terminal `.first` CallNode. Returns the source
3101+ # RangeNode nid, or -1 when the chain is not a supported lazy chain.
3102+ # Mirror of codegen's lazy_chain_range.
3103+ def lazy_chain_range(nid)
3104+ recv = @nd_receiver[nid]
3105+ if recv < 0 || @nd_type[recv] != "CallNode"
3106+ return -1
3107+ end
3108+ rn = @nd_name[recv]
3109+ lazy_nid = -1
3110+ if rn == "lazy" && @nd_block[recv] < 0
3111+ lazy_nid = recv
3112+ elsif (rn == "select" || rn == "filter" || rn == "reject") && @nd_block[recv] >= 0
3113+ inner = @nd_receiver[recv]
3114+ if inner >= 0 && @nd_type[inner] == "CallNode" && @nd_name[inner] == "lazy" && @nd_block[inner] < 0
3115+ lazy_nid = inner
3116+ end
3117+ end
3118+ if lazy_nid < 0
3119+ return -1
3120+ end
3121+ src = @nd_receiver[lazy_nid]
3122+ if src < 0
3123+ return -1
3124+ end
3125+ rng = -1
3126+ if @nd_type[src] == "RangeNode"
3127+ rng = src
3128+ elsif @nd_type[src] == "ParenthesesNode"
3129+ pb = @nd_body[src]
3130+ if pb >= 0
3131+ ps = get_stmts(pb)
3132+ if ps.length > 0 && @nd_type[ps.first] == "RangeNode"
3133+ rng = ps.first
3134+ end
3135+ end
3136+ end
3137+ if rng < 0
3138+ return -1
3139+ end
3140+ if @nd_left[rng] < 0 || @nd_type[@nd_left[rng]] == "NilNode"
3141+ return -1
3142+ end
3143+ rng
3144+ end
3145+
30843146 def infer_call_type(nid)
30853147 mname = @nd_name[nid]
30863148 recv = @nd_receiver[nid]
30873149
3150+ # Lazy range chain `(a..b).lazy[.select/.reject/.filter{blk}].first`.
3151+ # first(n) materialises an int_array, bare first an int. codegen
3152+ # lowers the whole chain to a bounded pull-loop; here we only need
3153+ # the terminal's type so a following `.inspect` dispatches as array.
3154+ if mname == "first" && lazy_chain_range(nid) >= 0
3155+ args_id_lz = @nd_arguments[nid]
3156+ if args_id_lz >= 0
3157+ aa_lz = get_args(args_id_lz)
3158+ if aa_lz.length > 0
3159+ return "int_array"
3160+ end
3161+ end
3162+ return "int"
3163+ end
3164+
30883165 if mname == "block_given?"
30893166 if recv < 0 || (recv >= 0 && @nd_type[recv] == "SelfNode")
30903167 return "bool"