Commit ddc868e2
ddc868e21a25Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +1 added · ~3 modified
Description
Track exclude_end in the runtime sp_Range so non-literal ... ranges work
The sp_Range value type held only {first, last}, so once a Range was
stored in a variable its exclude_end flag was lost: r = 1...5; r.max
returned 5, r.include?(5) returned true, r.to_a ran to 5, and each /
map / size / count / step all over-ran by one. The codegen could only
honour ... when the receiver was a literal RangeNode it could inspect
at compile time.
Worse, max and the numeric include? were wrong even for literals:
the last || end || max arm returned .last and shadowed the
exclude_end-aware max handler below it (dead code), and the include?
bound was an unconditional <= last.
Add an excl field (0 or 1) to sp_Range, populate it at the single
construction site from range_excl_end, and have every consumer compute
the inclusive upper bound as last - excl:
- sp_range_include / sp_Range_inspect (runtime)
- max, include?/cover?/===, overlap?, to_a, length/size/count, step
- the .each / .map iteration loops and emit_iter_open
last/end still return the raw endpoint ((1...5).last == 5), only
max subtracts the exclusion. Literal and non-literal receivers now go
through the same runtime flag, so both forms agree with CRuby.
Closes #1086
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- ddc868e21a25
- Parent
- 0bc1d675ac21
lib/sp_runtime.hmodifiedView file
/* String -> number parsers now live in libspinel_rt.a (lib/sp_core.c). */#include "sp_core.h"static sp_Range sp_range_new(mrb_int f,mrb_int l){sp_Range r;r.first=f;r.last=l;return r;}/* Inclusive-form `Range#include?`/`#cover?` on the boxed (SP_TAG_OBJ cls_id SP_BUILTIN_RANGE) Range value. The direct sp_Range typed path inlines this same check via compile_range_method_expr; poly-recv dispatch needs the wrapper so the cls_id arm in emit_poly_builtin_dispatch can land on a single C expression. The sp_Range struct doesn't track exclude_end, so behaviour matches the inclusive form -- consistent with the direct-typed emit. */static mrb_bool sp_range_include(sp_Range *r, mrb_int x){return r->first<=x && x<=r->last;}static sp_Range sp_range_new(mrb_int f,mrb_int l,mrb_int e){sp_Range r;r.first=f;r.last=l;r.excl=e;return r;}/* `Range#include?`/`#cover?` on the boxed (SP_TAG_OBJ cls_id SP_BUILTIN_RANGE) Range value. The direct sp_Range typed path inlines this same check via compile_range_method_expr; poly-recv dispatch needs the wrapper so the cls_id arm in emit_poly_builtin_dispatch can land on a single C expression. An exclusive range stops one short of `last`, so the upper bound is `last - excl` (excl is 0 or 1). */static mrb_bool sp_range_include(sp_Range *r, mrb_int x){return r->first<=x && x<=r->last-r->excl;}/* ---- Class object ---- Value-type Class reference: a single class id that indexes into return sp_box_obj(p, SP_BUILTIN_RANGE);}static const char *sp_Range_inspect(sp_Range *r) { /* "first..last" form. Buffer sized for two int64s plus the dots. */ /* "first..last" / "first...last" form. Buffer sized for two int64s plus the dots. */ char *buf = sp_str_alloc_raw(48); snprintf(buf, 48, "%lld..%lld", (long long)r->first, (long long)r->last); snprintf(buf, 48, r->excl ? "%lld...%lld" : "%lld..%lld", (long long)r->first, (long long)r->last); return buf;}/* Same heap-box rationale as sp_Range: sp_Time is 12+ bytes (tv_sec +lib/sp_types.hmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/range_exclusive_nonliteral.rbaddedView file
Loading diff…