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 end
30303030 return ""
30313031 end
3032+ if mname == "take_while" || mname == "drop_while"
3033+ if recv >= 0
3034+ rt = infer_type(recv)
3035+ return rt if rt == "int_array" || rt == "sym_array"
3036+ end
3037+ return ""
3038+ end
30323039 ""
30333040 end
30343041
@@ -17355,6 +17362,58 @@
1735517362 end
1735617363 return "sp_IntArray_sort(" + rc + ")"
1735717364 end
17365+ # take_while / drop_while: block-driven prefix scan. take_while
17366+ # collects elements from the front while the block stays truthy;
17367+ # drop_while skips them and returns the rest. Mirrors the
17368+ # existing take/drop arms but with per-element block evaluation.
17369+ # `bp`'s C type and the metadata `declare_var` records both come
17370+ # from `elem_type_of_array(recv_type)` so sym_array gets `sp_sym`
17371+ # rather than a hardcoded `mrb_int`. Multi-stmt blocks compile
17372+ # preceding statements before extracting the predicate expr from
17373+ # the last — same shape as the partition arm.
17374+ if (mname == "take_while" || mname == "drop_while") && @nd_block[nid] >= 0
17375+ blk = @nd_block[nid]
17376+ bp = get_block_param(nid, 0)
17377+ if bp == ""
17378+ bp = "_x"
17379+ end
17380+ elem_t = elem_type_of_array(recv_type)
17381+ tmp = new_temp
17382+ itmp = new_temp
17383+ ktmp = new_temp
17384+ 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_scope
17390+ declare_var(bp, elem_t)
17391+ bbody = @nd_body[blk]
17392+ bexpr = "0"
17393+ if bbody >= 0
17394+ bs = get_stmts(bbody)
17395+ if bs.length > 0
17396+ k = 0
17397+ while k < bs.length - 1
17398+ compile_stmt(bs[k])
17399+ k = k + 1
17400+ end
17401+ bexpr = compile_expr(bs.last)
17402+ end
17403+ end
17404+ emit(" if (!(" + bexpr + ")) break;")
17405+ if mname == "take_while"
17406+ emit(" sp_IntArray_push(" + tmp + ", lv_" + bp + ");")
17407+ end
17408+ 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+ end
17413+ emit(" }")
17414+ pop_scope
17415+ return tmp
17416+ end
1735817417 if mname == "first"
1735917418 return "sp_IntArray_get(" + rc + ", 0)"
1736017419 end