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 soputs (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
if mname == "include?" return "bool" end if mname == "cover?" return "bool" end if mname == "match?" return "bool" end "" end # Resolve the literal RangeNode behind a method receiver, peeking # through a single ParenthesesNode wrap. Returns -1 when the receiver # isn't a literal range — in which case the runtime sp_Range struct is # used and exclude_end isn't tracked. def resolve_literal_range_recv(nid) recv = @nd_receiver[nid] if recv < 0 return -1 end if @nd_type[recv] == "RangeNode" return recv end if @nd_type[recv] == "ParenthesesNode" pb = @nd_body[recv] if pb >= 0 ps = get_stmts(pb) if ps.length > 0 && @nd_type[ps.first] == "RangeNode" return ps.first end end end -1 end def compile_range_method_expr(nid, mname, rc) if mname == "first" return rc + ".first" if mname == "last" return rc + ".last" end if mname == "include?" # include? / cover? on a numeric range reduce to first <= x <= last # (inclusive form). The two methods are identical for numeric ranges # so they share the same emission. Exclude_end isn't tracked in the # runtime sp_Range struct; non-literal receivers fall back to the # inclusive form (matches length/size). if mname == "include?" || mname == "cover?" tmp = new_temp emit(" sp_Range " + tmp + " = " + rc + ";") return "(" + compile_arg0(nid) + " >= " + tmp + ".first && " + compile_arg0(nid) + " <= " + tmp + ".last)" arg = compile_arg0(nid) return "(" + arg + " >= " + tmp + ".first && " + arg + " <= " + tmp + ".last)" end if mname == "to_a" @needs_int_array = 1 # RangeNode (or wrapped in parens). For non-literal Range values # held in sp_Range structs, exclude_end is not tracked at runtime # and the inclusive form is used. recv = @nd_receiver[nid] range_nid = -1 if recv >= 0 && @nd_type[recv] == "RangeNode" range_nid = recv end if recv >= 0 && @nd_type[recv] == "ParenthesesNode" pb = @nd_body[recv] if pb >= 0 ps = get_stmts(pb) if ps.length > 0 && @nd_type[ps.first] == "RangeNode" range_nid = ps.first end end end range_nid = resolve_literal_range_recv(nid) if range_nid >= 0 rright = compile_expr(@nd_right[range_nid]) if range_excl_end(range_nid) == 1 if mname == "size" return "(" + rc + ".last - " + rc + ".first + 1)" end if mname == "min" return rc + ".first" end if mname == "max" range_nid = resolve_literal_range_recv(nid) if range_nid >= 0 && range_excl_end(range_nid) == 1 return "(" + rc + ".last - 1)" end return rc + ".last" end if mname == "count" # Inclusive: last - first + 1. Exclusive (literal): last - first. range_nid = resolve_literal_range_recv(nid) if range_nid >= 0 && range_excl_end(range_nid) == 1 return "(" + rc + ".last - " + rc + ".first)" end return "(" + rc + ".last - " + rc + ".first + 1)" end "" endtest/range_completeness.rbaddedView file
Loading diff…