Commit c82ee96b

c82ee96be469Merged

Ryan Seys <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

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

Bundle of four sibling methods, all reducing to trivial reads or arithmetic on sp_Range's first/last fields and ship the same restriction: exclude_end is honoured for literal RangeNodes (the codegen has the parse-time flag) but treated as inclusive for non-literal sp_Range values, since the runtime struct doesn't carry exclude_end. Mirrors the existing to_a literal-vs-runtime split.

  • cover?: numeric reduces to first <= x <= last. Mirrors include? for inclusive ranges. Added "bool" return-type entry alongside include?'s so puts (1..10).cover?(5) formats as "true" instead of "1".
  • min: rc.first.
  • max: rc.last for inclusive; (rc.last - 1) for exclusive literal.
  • count: (last - first + 1) for inclusive; (last - first) for exclusive literal.

Test exercises:

  • cover? at midpoint, both endpoints, just outside, with negative bounds (8 cases)
  • min and max for three ranges including a negative-bound case
  • count for inclusive ranges including single-element (1..1)
  • count for exclusive literal ranges including empty (1...1) and one with negative start

Out of scope: Float ranges (sp_Range is mrb_int-only); endless (1..) and beginless (..10) ranges; cover? against a sub-range argument (Ruby allows (1..10).cover?(2..5) — symbol/string-typed ranges are also out).

Bootstrap: gen2.c == gen3.c. Tests: 206 pass.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
c82ee96be469
Parent
02005f26deba

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
@@ -16384,6 +16387,30 @@
1638416387 ""
1638516388 end
1638616389
16390+ # Resolve the literal RangeNode behind a method receiver, peeking
16391+ # through a single ParenthesesNode wrap. Returns -1 when the receiver
16392+ # isn't a literal range — in which case the runtime sp_Range struct is
16393+ # used and exclude_end isn't tracked.
16394+ def resolve_literal_range_recv(nid)
16395+ recv = @nd_receiver[nid]
16396+ if recv < 0
16397+ return -1
16398+ end
16399+ if @nd_type[recv] == "RangeNode"
16400+ return recv
16401+ end
16402+ if @nd_type[recv] == "ParenthesesNode"
16403+ pb = @nd_body[recv]
16404+ if pb >= 0
16405+ ps = get_stmts(pb)
16406+ if ps.length > 0 && @nd_type[ps.first] == "RangeNode"
16407+ return ps.first
16408+ end
16409+ end
16410+ end
16411+ -1
16412+ end
16413+
1638716414 def compile_range_method_expr(nid, mname, rc)
1638816415 if mname == "first"
1638916416 return rc + ".first"
@@ -16391,10 +16418,16 @@
1639116418 if mname == "last"
1639216419 return rc + ".last"
1639316420 end
16394- if mname == "include?"
16421+ # include? / cover? on a numeric range reduce to first <= x <= last
16422+ # (inclusive form). The two methods are identical for numeric ranges
16423+ # so they share the same emission. Exclude_end isn't tracked in the
16424+ # runtime sp_Range struct; non-literal receivers fall back to the
16425+ # inclusive form (matches length/size).
16426+ if mname == "include?" || mname == "cover?"
1639516427 tmp = new_temp
1639616428 emit(" sp_Range " + tmp + " = " + rc + ";")
16397- return "(" + compile_arg0(nid) + " >= " + tmp + ".first && " + compile_arg0(nid) + " <= " + tmp + ".last)"
16429+ arg = compile_arg0(nid)
16430+ return "(" + arg + " >= " + tmp + ".first && " + arg + " <= " + tmp + ".last)"
1639816431 end
1639916432 if mname == "to_a"
1640016433 @needs_int_array = 1
@@ -16403,20 +16436,7 @@
1640316436 # RangeNode (or wrapped in parens). For non-literal Range values
1640416437 # held in sp_Range structs, exclude_end is not tracked at runtime
1640516438 # and the inclusive form is used.
16406- recv = @nd_receiver[nid]
16407- range_nid = -1
16408- if recv >= 0 && @nd_type[recv] == "RangeNode"
16409- range_nid = recv
16410- end
16411- if recv >= 0 && @nd_type[recv] == "ParenthesesNode"
16412- pb = @nd_body[recv]
16413- if pb >= 0
16414- ps = get_stmts(pb)
16415- if ps.length > 0 && @nd_type[ps.first] == "RangeNode"
16416- range_nid = ps.first
16417- end
16418- end
16419- end
16439+ range_nid = resolve_literal_range_recv(nid)
1642016440 if range_nid >= 0
1642116441 rright = compile_expr(@nd_right[range_nid])
1642216442 if range_excl_end(range_nid) == 1
@@ -16432,6 +16452,24 @@
1643216452 if mname == "size"
1643316453 return "(" + rc + ".last - " + rc + ".first + 1)"
1643416454 end
16455+ if mname == "min"
16456+ return rc + ".first"
16457+ end
16458+ if mname == "max"
16459+ range_nid = resolve_literal_range_recv(nid)
16460+ if range_nid >= 0 && range_excl_end(range_nid) == 1
16461+ return "(" + rc + ".last - 1)"
16462+ end
16463+ return rc + ".last"
16464+ end
16465+ if mname == "count"
16466+ # Inclusive: last - first + 1. Exclusive (literal): last - first.
16467+ range_nid = resolve_literal_range_recv(nid)
16468+ if range_nid >= 0 && range_excl_end(range_nid) == 1
16469+ return "(" + rc + ".last - " + rc + ".first)"
16470+ end
16471+ return "(" + rc + ".last - " + rc + ".first + 1)"
16472+ end
1643516473 ""
1643616474 end
1643716475