Commit bfe58c28
bfe58c281865Merged
Ryan Seys <[email protected]> · 6 months ago · 5 files · +4 added · ~1 modified
Description
fix(codegen): inspect tuple:int,int / tuple:int,float for p
Methods that return Ruby Arrays of fixed arity — Integer#divmod,
Array#minmax, etc. — internally lower to a tuple:int,int (or
tuple:int,float) struct rather than building a heap-allocated
sp_PtrArray. That's efficient for destructuring (q, r = …) and
arithmetic, but compile_inspect_for had no entry for these
tuple types, so p 100.divmod(7) and p arr.minmax fell through
to the puts-style printf("%lld", val) default and printed the
struct pointer instead of [14, 2] / [1, 9].
Adds two inspect branches that emit the Ruby array literal form via sp_sprintf:
tuple:int,int → "[%lld, %lld]" tuple:int,float → "[%lld, %s]" (second slot via sp_float_inspect)
The float-second variant covers Float#divmod, which returns
[int, float] per CRuby (e.g. 5.5.divmod(2) → [2, 1.5]).
Bootstrap fixed-point holds. Tests added: integer_divmod,
array_minmax, array_minmax_2 — each just p <expr> against the
matching CRuby reference output.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- bfe58c281865
- Parent
- 8ff9aa915a30
spinel_codegen.rbmodifiedView file
@needs_int_array = 1 return "sp_SymArrayPtrArray_inspect(" + val + ")" end # Tuples produced by methods that return Ruby Arrays of fixed # arity (Integer#divmod, Array#minmax, etc.). Without an inspect # path here, `p arr.minmax` would fall through to printf("%lld",...) # and emit the struct pointer. Format as Ruby's "[a, b]" so the # output round-trips with CRuby. if at == "tuple:int,int" return "sp_sprintf(\"[%lld, %lld]\", (long long)" + val + "->_0, (long long)" + val + "->_1)" end if at == "tuple:int,float" return "sp_sprintf(\"[%lld, %s]\", (long long)" + val + "->_0, sp_float_inspect(" + val + "->_1))" end if at == "tuple:float,float" return "sp_sprintf(\"[%s, %s]\", sp_float_inspect(" + val + "->_0), sp_float_inspect(" + val + "->_1))" end "" endtest/array_minmax.rbaddedView file
Loading diff…
test/array_minmax.rb.expectedaddedView file
Loading diff…
test/integer_divmod.rbaddedView file
Loading diff…
test/integer_divmod.rb.expectedaddedView file
Loading diff…