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
end return "" end if mname == "take_while" || mname == "drop_while" if recv >= 0 rt = infer_type(recv) return rt if rt == "int_array" || rt == "sym_array" end return "" end "" end end return "sp_IntArray_sort(" + rc + ")" end # take_while / drop_while: block-driven prefix scan. take_while # collects elements from the front while the block stays truthy; # drop_while skips them and returns the rest. Mirrors the # existing take/drop arms but with per-element block evaluation. # `bp`'s C type and the metadata `declare_var` records both come # from `elem_type_of_array(recv_type)` so sym_array gets `sp_sym` # rather than a hardcoded `mrb_int`. Multi-stmt blocks compile # preceding statements before extracting the predicate expr from # the last — same shape as the partition arm. if (mname == "take_while" || mname == "drop_while") && @nd_block[nid] >= 0 blk = @nd_block[nid] bp = get_block_param(nid, 0) if bp == "" bp = "_x" end elem_t = elem_type_of_array(recv_type) tmp = new_temp itmp = new_temp ktmp = new_temp emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();") emit(" { mrb_int " + ktmp + " = sp_IntArray_length(" + rc + ");") emit(" mrb_int " + itmp + " = 0;") emit(" while (" + itmp + " < " + ktmp + ") {") emit(" " + c_type(elem_t) + " lv_" + bp + " = sp_IntArray_get(" + rc + ", " + itmp + ");") push_scope declare_var(bp, elem_t) bbody = @nd_body[blk] bexpr = "0" if bbody >= 0 bs = get_stmts(bbody) if bs.length > 0 k = 0 while k < bs.length - 1 compile_stmt(bs[k]) k = k + 1 end bexpr = compile_expr(bs.last) end end emit(" if (!(" + bexpr + ")) break;") if mname == "take_while" emit(" sp_IntArray_push(" + tmp + ", lv_" + bp + ");") end emit(" " + itmp + "++;") emit(" }") if mname == "drop_while" emit(" while (" + itmp + " < " + ktmp + ") { sp_IntArray_push(" + tmp + ", sp_IntArray_get(" + rc + ", " + itmp + ")); " + itmp + "++; }") end emit(" }") pop_scope return tmp end if mname == "first" return "sp_IntArray_get(" + rc + ", 0)" endtest/array_take_drop_while.rbaddedView file
Loading diff…