Commit b09cac74

b09cac74596fMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

Merge pull request #146 from ryanseys/rs-range-completeness

feat(codegen): Range#cover?, #min, #max, #count for numeric ranges

Details

Commit
b09cac74596f
Parent
9643e12b65ba
Merge parent
c82ee96be469

spinel_codegen.rbmodifiedView file

@@ -2209,6 +2209,9 @@
22092209 if mname == "include?"
22102210 return "bool"
22112211 end
2212+ if mname == "cover?"
2213+ return "bool"
2214+ end
22122215 if mname == "match?"
22132216 return "bool"
22142217 end
@@ -16576,6 +16579,30 @@
1657616579 ""
1657716580 end
1657816581
16582+ # Resolve the literal RangeNode behind a method receiver, peeking
16583+ # through a single ParenthesesNode wrap. Returns -1 when the receiver
16584+ # isn't a literal range — in which case the runtime sp_Range struct is
16585+ # used and exclude_end isn't tracked.
16586+ def resolve_literal_range_recv(nid)
16587+ recv = @nd_receiver[nid]
16588+ if recv < 0
16589+ return -1
16590+ end
16591+ if @nd_type[recv] == "RangeNode"
16592+ return recv
16593+ end
16594+ if @nd_type[recv] == "ParenthesesNode"
16595+ pb = @nd_body[recv]
16596+ if pb >= 0
16597+ ps = get_stmts(pb)
16598+ if ps.length > 0 && @nd_type[ps.first] == "RangeNode"
16599+ return ps.first
16600+ end
16601+ end
16602+ end
16603+ -1
16604+ end
16605+
1657916606 def compile_range_method_expr(nid, mname, rc)
1658016607 if mname == "first"
1658116608 return rc + ".first"
@@ -16583,10 +16610,16 @@
1658316610 if mname == "last"
1658416611 return rc + ".last"
1658516612 end
16586- if mname == "include?"
16613+ # include? / cover? on a numeric range reduce to first <= x <= last
16614+ # (inclusive form). The two methods are identical for numeric ranges
16615+ # so they share the same emission. Exclude_end isn't tracked in the
16616+ # runtime sp_Range struct; non-literal receivers fall back to the
16617+ # inclusive form (matches length/size).
16618+ if mname == "include?" || mname == "cover?"
1658716619 tmp = new_temp
1658816620 emit(" sp_Range " + tmp + " = " + rc + ";")
16589- return "(" + compile_arg0(nid) + " >= " + tmp + ".first && " + compile_arg0(nid) + " <= " + tmp + ".last)"
16621+ arg = compile_arg0(nid)
16622+ return "(" + arg + " >= " + tmp + ".first && " + arg + " <= " + tmp + ".last)"
1659016623 end
1659116624 if mname == "to_a"
1659216625 @needs_int_array = 1
@@ -16595,20 +16628,7 @@
1659516628 # RangeNode (or wrapped in parens). For non-literal Range values
1659616629 # held in sp_Range structs, exclude_end is not tracked at runtime
1659716630 # and the inclusive form is used.
16598- recv = @nd_receiver[nid]
16599- range_nid = -1
16600- if recv >= 0 && @nd_type[recv] == "RangeNode"
16601- range_nid = recv
16602- end
16603- if recv >= 0 && @nd_type[recv] == "ParenthesesNode"
16604- pb = @nd_body[recv]
16605- if pb >= 0
16606- ps = get_stmts(pb)
16607- if ps.length > 0 && @nd_type[ps.first] == "RangeNode"
16608- range_nid = ps.first
16609- end
16610- end
16611- end
16631+ range_nid = resolve_literal_range_recv(nid)
1661216632 if range_nid >= 0
1661316633 rright = compile_expr(@nd_right[range_nid])
1661416634 if range_excl_end(range_nid) == 1
@@ -16624,6 +16644,24 @@
1662416644 if mname == "size"
1662516645 return "(" + rc + ".last - " + rc + ".first + 1)"
1662616646 end
16647+ if mname == "min"
16648+ return rc + ".first"
16649+ end
16650+ if mname == "max"
16651+ range_nid = resolve_literal_range_recv(nid)
16652+ if range_nid >= 0 && range_excl_end(range_nid) == 1
16653+ return "(" + rc + ".last - 1)"
16654+ end
16655+ return rc + ".last"
16656+ end
16657+ if mname == "count"
16658+ # Inclusive: last - first + 1. Exclusive (literal): last - first.
16659+ range_nid = resolve_literal_range_recv(nid)
16660+ if range_nid >= 0 && range_excl_end(range_nid) == 1
16661+ return "(" + rc + ".last - " + rc + ".first)"
16662+ end
16663+ return "(" + rc + ".last - " + rc + ".first + 1)"
16664+ end
1662716665 ""
1662816666 end
1662916667