Commit 22367b98

22367b9826c4Merged

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

Description

feat(analyze,codegen): Array#group_by + Hash#each chain on poly_array

<arr>.group_by(blk).each |k, rows| was the named gap: the call silently emitted 0 with a "cannot resolve" warning. The natural codegen would have produced a sp_PolyPolyHash with poly-boxed sp_PolyArray values, but that leaves rows typed as poly in the each-body, and spinel's poly-recv dispatch doesn't carry block-form methods (.map / .sum) for poly_array. Without those, the inner rows.map { _1[:score] }.sum.to_f / rows.size still fails downstream.

Approach: fuse the two-call chain into one emit so the per-key accumulator is exposed to the each-block as a typed sp_PolyArray * directly. The phase-1 loop walks the input array, runs the group-by block to produce a key (boxed as poly), and appends the element to the per-key accumulator. The phase-2 loop iterates the recorded key order, unboxes the accumulator back to a typed pointer, and runs the each-block body with rows as poly_array. Downstream calls then reach existing poly_array dispatch arms without poly-recv branches.

Three downstream gaps the fusion forced fixing:

  1. Array#size was missing for poly_array (only .length existed). Added the alias.

  2. <poly_array>.map { _1[:k] } infers as int_array via the default poly-recv hash-index fallback, but the actual emit returns sp_RbVal — an sp_IntArray_push mismatch. Both the analyzer (infer_type for the .map call) and codegen (compile_map_expr) now recognize the _1[] on the bp param pattern and switch the return / accumulator to poly_array. The analyzer rule fires only when recv_t is poly_array so unrelated int_array .map sites stay typed.

  3. <poly_array>#sum had no dispatch. Added a sp_PolyArray_sum_int helper that sums the int-tagged elements (matching the shape .map { _1[:int_key] } produces) and routed it from the dispatch.

Block-param typing on the each-side: block_param_type_at now has a <arr>.group_by(blk).each arm that returns poly for pi=0 (key) and poly_array for pi=1 (rows), matching what the fused emit binds.

Float interpolation: "#{x}" for float x emitted via printf %g, dropping trailing zeros so 4.0 rendered as 4. CRuby's Float#to_s always preserves the decimal point. Routed interpolated floats through sp_float_to_s for parity.

Test: test/array_group_by_poly_each_chain.rb is the canonical 11-LOC repro (shop / score average). spinel and CRuby outputs now match byte-for-byte. make test 581 pass / 0 fail / 0 error; make bootstrap fixpoint clean.

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

Details

Commit
22367b9826c4
Parent
493e29785175

lib/sp_runtime.hmodifiedView file

@@ -1715,6 +1715,11 @@
17151715 return r;
17161716 }
17171717 static sp_PolyArray *sp_PolyArray_dup(sp_PolyArray *a) { sp_PolyArray *b = sp_PolyArray_new(); for (mrb_int i = 0; i < a->len; i++) sp_PolyArray_push(b, a->data[i]); return b; }
1718+/* Sum the integer-tagged elements of a poly_array. Used by
1719+ `Array#sum` on a poly_array whose runtime tags are uniform int
1720+ (e.g. the result of `arr.map { _1[:int_key] }`). Non-int tags
1721+ contribute zero. */
1722+static mrb_int sp_PolyArray_sum_int(sp_PolyArray *a) { if (!a) return 0; mrb_int s = 0; for (mrb_int i = 0; i < a->len; i++) { if (a->data[i].tag == SP_TAG_INT) s += a->data[i].v.i; } return s; }
17181723 static void sp_PolyArray_shuffle_bang(sp_PolyArray *a) { for (mrb_int i = a->len - 1; i > 0; i--) { mrb_int j = (mrb_int)(rand() % (i + 1)); sp_RbVal t = a->data[i]; a->data[i] = a->data[j]; a->data[j] = t; } }
17191724 static void sp_PolyArray_rotate_bang(sp_PolyArray*a,mrb_int n){if(a->len<=0)return;n=((n%a->len)+a->len)%a->len;if(n==0)return;sp_RbVal*tmp=(sp_RbVal*)malloc(sizeof(sp_RbVal)*a->len);for(mrb_int i=0;i<a->len;i++)tmp[i]=a->data[(i+n)%a->len];for(mrb_int i=0;i<a->len;i++)a->data[i]=tmp[i];free(tmp);}
17201725 static sp_PolyArray *sp_PolyArray_shuffle(sp_PolyArray *a) { sp_PolyArray *b = sp_PolyArray_dup(a); sp_PolyArray_shuffle_bang(b); return b; }