Commit 7ff4f4a8

7ff4f4a8ddcaMerged

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

Description

feat(codegen): Array#take_while and #drop_while for int/sym arrays

Bundle the two block-driven prefix-scan siblings. Both share the same shape: walk self until the block first returns falsy, then for take_while keep what came before, for drop_while keep what came after. Mirrors the existing #take and #drop branches in the int_array/sym_array dispatch arm but with per-element block evaluation in place of a counted limit.

Single dispatch arm in compile_call_expr's int_array/sym_array block emits:

sp_IntArray *t = sp_IntArray_new();
{ mrb_int n = sp_IntArray_length(rc);
  mrb_int i = 0;
  while (i < n) {
    mrb_int lv_<bp> = sp_IntArray_get(rc, i);
    if (!(<block-body>)) break;
    sp_IntArray_push(t, lv_<bp>);   // take_while only
    i++;
  }
  while (i < n) { sp_IntArray_push(t, sp_IntArray_get(rc, i)); i++; }
    // drop_while only — copy the suffix verbatim
}

The block body is the trailing expression of the block's StatementsNode, compiled inside push_scope so a shadowed block param doesn't leak. Type inference returns the receiver's array type unchanged.

Test exercises both methods over four shapes:

  • prefix predicate fires mid-array ([1,2,3,1].take_while { x < 3 } yields [1, 2])
  • always-true predicate (returns full / empty for take/drop)
  • always-false predicate (returns empty / full for take/drop)
  • empty input
  • length round-trip — take_while.length + drop_while.length == self.length

Out of scope: str_array, float_array, sym_array variants. The shape copy-pastes mechanically across types but each adds another arm — ship as a follow-up bundle if needed.

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

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

Details

Commit
7ff4f4a8ddca
Parent
02005f26deba

spinel_codegen.rbmodifiedView file

@@ -2984,6 +2984,13 @@
29842984 end
29852985 return ""
29862986 end
2987+ if mname == "take_while" || mname == "drop_while"
2988+ if recv >= 0
2989+ rt = infer_type(recv)
2990+ return rt if rt == "int_array" || rt == "sym_array"
2991+ end
2992+ return ""
2993+ end
29872994 ""
29882995 end
29892996
@@ -17040,6 +17047,58 @@
1704017047 end
1704117048 return "sp_IntArray_sort(" + rc + ")"
1704217049 end
17050+ # take_while / drop_while: block-driven prefix scan. take_while
17051+ # collects elements from the front while the block stays truthy;
17052+ # drop_while skips them and returns the rest. Mirrors the
17053+ # existing take/drop arms but with per-element block evaluation.
17054+ # `bp`'s C type and the metadata `declare_var` records both come
17055+ # from `elem_type_of_array(recv_type)` so sym_array gets `sp_sym`
17056+ # rather than a hardcoded `mrb_int`. Multi-stmt blocks compile
17057+ # preceding statements before extracting the predicate expr from
17058+ # the last — same shape as the partition arm.
17059+ if (mname == "take_while" || mname == "drop_while") && @nd_block[nid] >= 0
17060+ blk = @nd_block[nid]
17061+ bp = get_block_param(nid, 0)
17062+ if bp == ""
17063+ bp = "_x"
17064+ end
17065+ elem_t = elem_type_of_array(recv_type)
17066+ tmp = new_temp
17067+ itmp = new_temp
17068+ ktmp = new_temp
17069+ emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();")
17070+ emit(" { mrb_int " + ktmp + " = sp_IntArray_length(" + rc + ");")
17071+ emit(" mrb_int " + itmp + " = 0;")
17072+ emit(" while (" + itmp + " < " + ktmp + ") {")
17073+ emit(" " + c_type(elem_t) + " lv_" + bp + " = sp_IntArray_get(" + rc + ", " + itmp + ");")
17074+ push_scope
17075+ declare_var(bp, elem_t)
17076+ bbody = @nd_body[blk]
17077+ bexpr = "0"
17078+ if bbody >= 0
17079+ bs = get_stmts(bbody)
17080+ if bs.length > 0
17081+ k = 0
17082+ while k < bs.length - 1
17083+ compile_stmt(bs[k])
17084+ k = k + 1
17085+ end
17086+ bexpr = compile_expr(bs.last)
17087+ end
17088+ end
17089+ emit(" if (!(" + bexpr + ")) break;")
17090+ if mname == "take_while"
17091+ emit(" sp_IntArray_push(" + tmp + ", lv_" + bp + ");")
17092+ end
17093+ emit(" " + itmp + "++;")
17094+ emit(" }")
17095+ if mname == "drop_while"
17096+ emit(" while (" + itmp + " < " + ktmp + ") { sp_IntArray_push(" + tmp + ", sp_IntArray_get(" + rc + ", " + itmp + ")); " + itmp + "++; }")
17097+ end
17098+ emit(" }")
17099+ pop_scope
17100+ return tmp
17101+ end
1704317102 if mname == "first"
1704417103 return "sp_IntArray_get(" + rc + ", 0)"
1704517104 end