Commit bee7e89d
bee7e89d5b64Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
Merge pull request #149 from ryanseys/rs-array-take-drop-while
feat(codegen): Array#take_while and #drop_while for int/sym arrays
Details
- Commit
- bee7e89d5b64
- Parent
- 43a4ea1acfb0
- Merge parent
- d4cecbe4a28c
spinel_codegen.rbmodifiedView file
@@ -3029,6 +3029,13 @@
30293029
end30303030
return ""30313031
end3032+
if mname == "take_while" || mname == "drop_while"3033+
if recv >= 03034+
rt = infer_type(recv)3035+
return rt if rt == "int_array" || rt == "sym_array"3036+
end3037+
return ""3038+
end30323039
""30333040
end30343041
@@ -17355,6 +17362,58 @@
1735517362
end1735617363
return "sp_IntArray_sort(" + rc + ")"1735717364
end17365+
# take_while / drop_while: block-driven prefix scan. take_while17366+
# collects elements from the front while the block stays truthy;17367+
# drop_while skips them and returns the rest. Mirrors the17368+
# existing take/drop arms but with per-element block evaluation.17369+
# `bp`'s C type and the metadata `declare_var` records both come17370+
# from `elem_type_of_array(recv_type)` so sym_array gets `sp_sym`17371+
# rather than a hardcoded `mrb_int`. Multi-stmt blocks compile17372+
# preceding statements before extracting the predicate expr from17373+
# the last — same shape as the partition arm.17374+
if (mname == "take_while" || mname == "drop_while") && @nd_block[nid] >= 017375+
blk = @nd_block[nid]17376+
bp = get_block_param(nid, 0)17377+
if bp == ""17378+
bp = "_x"17379+
end17380+
elem_t = elem_type_of_array(recv_type)17381+
tmp = new_temp17382+
itmp = new_temp17383+
ktmp = new_temp17384+
emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();")17385+
emit(" { mrb_int " + ktmp + " = sp_IntArray_length(" + rc + ");")17386+
emit(" mrb_int " + itmp + " = 0;")17387+
emit(" while (" + itmp + " < " + ktmp + ") {")17388+
emit(" " + c_type(elem_t) + " lv_" + bp + " = sp_IntArray_get(" + rc + ", " + itmp + ");")17389+
push_scope17390+
declare_var(bp, elem_t)17391+
bbody = @nd_body[blk]17392+
bexpr = "0"17393+
if bbody >= 017394+
bs = get_stmts(bbody)17395+
if bs.length > 017396+
k = 017397+
while k < bs.length - 117398+
compile_stmt(bs[k])17399+
k = k + 117400+
end17401+
bexpr = compile_expr(bs.last)17402+
end17403+
end17404+
emit(" if (!(" + bexpr + ")) break;")17405+
if mname == "take_while"17406+
emit(" sp_IntArray_push(" + tmp + ", lv_" + bp + ");")17407+
end17408+
emit(" " + itmp + "++;")17409+
emit(" }")17410+
if mname == "drop_while"17411+
emit(" while (" + itmp + " < " + ktmp + ") { sp_IntArray_push(" + tmp + ", sp_IntArray_get(" + rc + ", " + itmp + ")); " + itmp + "++; }")17412+
end17413+
emit(" }")17414+
pop_scope17415+
return tmp17416+
end1735817417
if mname == "first"1735917418
return "sp_IntArray_get(" + rc + ", 0)"1736017419
endtest/array_take_drop_while.rbaddedView file
Loading diff…