Commit 4af1b4df

4af1b4df8b8fMerged

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

Description

codegen: Array#sort / min / max with 2-param comparator block

#979 / #980. The block form of sort previously ignored the comparator and always sorted ascending (no arm at all for str_array, sym_array sort ignored the block). min/max with a 2-arg block was mistyped as a min_by-style scoring function — the second param was never declared so the block body failed to compile (e.g. `b <=> a` saw `b` as default-int).

Both fixes share the comparator shape: block returns -1/0/1; for sort, swap when cmp > 0; for min/max, update the running best when cmp <,> 0 against the current best.

Variants wired:

  • int_array sort with block (bubble sort, comparator)
  • str_array sort with and without block (plus const-char swap)
  • min/max comparator path declares both bp1/bp2 with the element type; 1-param block keeps the existing min_by-style fallback

Fixes #979 Fixes #980

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
4af1b4df8b8f
Parent
45329ccadc94

spinel_codegen.rbmodifiedView file

@@ -22336,6 +22336,47 @@
2233622336 end
2233722337 end
2233822338 if mname == "sort"
22339+ # Block-form sort: bubble-sort using the block's <=> return as
22340+ # the comparator. Block params get the array element type
22341+ # declared so `b <=> a` inside infers correctly.
22342+ if @nd_block[nid] >= 0
22343+ blk_so = @nd_block[nid]
22344+ bp_a = get_block_param(nid, 0)
22345+ bp_a = "_a" if bp_a == ""
22346+ bp_b = get_block_param(nid, 1)
22347+ bp_b = "_b" if bp_b == ""
22348+ tmp_so = new_temp
22349+ emit(" sp_IntArray *" + tmp_so + " = sp_IntArray_dup(" + rc + ");")
22350+ emit(" { mrb_int _n = " + tmp_so + "->len;")
22351+ emit(" for (mrb_int _i = 0; _i < _n - 1; _i++)")
22352+ emit(" for (mrb_int _j = 0; _j < _n - 1 - _i; _j++) {")
22353+ emit(" mrb_int lv_" + bp_a + " = " + tmp_so + "->data[" + tmp_so + "->start + _j];")
22354+ emit(" mrb_int lv_" + bp_b + " = " + tmp_so + "->data[" + tmp_so + "->start + _j + 1];")
22355+ @indent = @indent + 1
22356+ push_scope
22357+ declare_var(bp_a, "int")
22358+ declare_var(bp_b, "int")
22359+ bbody_so = @nd_body[blk_so]
22360+ bexpr_so = "0"
22361+ if bbody_so >= 0
22362+ bs_so = get_stmts(bbody_so)
22363+ if bs_so.length > 0
22364+ k_so = 0
22365+ while k_so < bs_so.length - 1
22366+ compile_stmt(bs_so[k_so])
22367+ k_so = k_so + 1
22368+ end
22369+ bexpr_so = compile_expr(bs_so.last)
22370+ end
22371+ end
22372+ pop_scope
22373+ @indent = @indent - 1
22374+ emit(" mrb_int _cmp = " + bexpr_so + ";")
22375+ emit(" if (_cmp > 0) { mrb_int _t = " + tmp_so + "->data[" + tmp_so + "->start + _j]; " + tmp_so + "->data[" + tmp_so + "->start + _j] = " + tmp_so + "->data[" + tmp_so + "->start + _j + 1]; " + tmp_so + "->data[" + tmp_so + "->start + _j + 1] = _t; }")
22376+ emit(" }")
22377+ emit(" }")
22378+ return tmp_so
22379+ end
2233922380 if recv_type == "sym_array"
2234022381 tmp = new_temp
2234122382 emit(" sp_IntArray *" + tmp + " = sp_IntArray_dup(" + rc + "); sp_sym_array_sort(" + tmp + ");")
@@ -23090,6 +23131,55 @@
2309023131 emit(" }")
2309123132 return tmp_sb_s
2309223133 end
23134+ # str_array.sort with block — comparator returns -1/0/1.
23135+ if mname == "sort" && @nd_block[nid] >= 0
23136+ @needs_str_array = 1
23137+ @needs_gc = 1
23138+ blk_ss = @nd_block[nid]
23139+ bp_a_ss = get_block_param(nid, 0)
23140+ bp_a_ss = "_a" if bp_a_ss == ""
23141+ bp_b_ss = get_block_param(nid, 1)
23142+ bp_b_ss = "_b" if bp_b_ss == ""
23143+ tmp_ss = new_temp
23144+ emit(" sp_StrArray *" + tmp_ss + " = sp_StrArray_new(); sp_StrArray_replace(" + tmp_ss + ", " + rc + ");")
23145+ emit(" { mrb_int _n = " + tmp_ss + "->len;")
23146+ emit(" for (mrb_int _i = 0; _i < _n - 1; _i++)")
23147+ emit(" for (mrb_int _j = 0; _j < _n - 1 - _i; _j++) {")
23148+ emit(" const char *lv_" + bp_a_ss + " = " + tmp_ss + "->data[_j];")
23149+ emit(" const char *lv_" + bp_b_ss + " = " + tmp_ss + "->data[_j + 1];")
23150+ @indent = @indent + 1
23151+ push_scope
23152+ declare_var(bp_a_ss, "string")
23153+ declare_var(bp_b_ss, "string")
23154+ bbody_ss = @nd_body[blk_ss]
23155+ bexpr_ss = "0"
23156+ if bbody_ss >= 0
23157+ bs_ss = get_stmts(bbody_ss)
23158+ if bs_ss.length > 0
23159+ k_ss = 0
23160+ while k_ss < bs_ss.length - 1
23161+ compile_stmt(bs_ss[k_ss])
23162+ k_ss = k_ss + 1
23163+ end
23164+ bexpr_ss = compile_expr(bs_ss.last)
23165+ end
23166+ end
23167+ pop_scope
23168+ @indent = @indent - 1
23169+ emit(" mrb_int _cmp = " + bexpr_ss + ";")
23170+ emit(" if (_cmp > 0) { const char *_tt = " + tmp_ss + "->data[_j]; " + tmp_ss + "->data[_j] = " + tmp_ss + "->data[_j + 1]; " + tmp_ss + "->data[_j + 1] = _tt; }")
23171+ emit(" }")
23172+ emit(" }")
23173+ return tmp_ss
23174+ end
23175+ # str_array.sort without block — lex sort via runtime helper.
23176+ if mname == "sort"
23177+ @needs_str_array = 1
23178+ @needs_gc = 1
23179+ tmp_sn = new_temp
23180+ emit(" sp_StrArray *" + tmp_sn + " = sp_StrArray_new(); sp_StrArray_replace(" + tmp_sn + ", " + rc + "); sp_StrArray_sort_bang(" + tmp_sn + ");")
23181+ return tmp_sn
23182+ end
2309323183 end
2309423184
2309523185 # PolyArray methods
@@ -40416,9 +40506,8 @@
4041640506
4041740507 def compile_array_min_max_block(nid, rc, recv_type, mname)
4041840508 bp1 = get_block_param(nid, 0)
40419- if bp1 == ""
40420- bp1 = "_x"
40421- end
40509+ bp1 = "_a" if bp1 == ""
40510+ bp2 = get_block_param(nid, 1)
4042240511 elem_type = "int"
4042340512 if recv_type == "str_array"
4042440513 elem_type = "string"
@@ -40426,8 +40515,45 @@
4042640515 elem_type = "float"
4042740516 end
4042840517 tmp_res = new_temp
40429- tmp_key = new_temp
4043040518 tmp_i = new_temp
40519+ # 2-param block is a comparator (CRuby's min/max-with-block).
40520+ # The block returns -1/0/1; we keep `tmp_res` as the running
40521+ # best and update when the comparator says the new element wins.
40522+ # 1-param block is the legacy min_by-style scoring function.
40523+ if bp2 != ""
40524+ emit(" " + c_type(elem_type) + " " + tmp_res + " = " + c_default_val(elem_type) + ";")
40525+ emit_iter_open(rc, recv_type, "lv_" + bp1, tmp_i)
40526+ push_scope
40527+ declare_var(bp1, elem_type)
40528+ declare_var(bp2, elem_type)
40529+ emit(" if (" + tmp_i + " == 0) { " + tmp_res + " = lv_" + bp1 + "; } else {")
40530+ emit(" " + c_type(elem_type) + " lv_" + bp2 + " = " + tmp_res + ";")
40531+ blk_mm = @nd_block[nid]
40532+ bexpr_mm = "0"
40533+ if @nd_body[blk_mm] >= 0
40534+ bs_mm = get_stmts(@nd_body[blk_mm])
40535+ if bs_mm.length > 0
40536+ k_mm = 0
40537+ while k_mm < bs_mm.length - 1
40538+ compile_stmt(bs_mm[k_mm])
40539+ k_mm = k_mm + 1
40540+ end
40541+ bexpr_mm = compile_expr(bs_mm.last)
40542+ end
40543+ end
40544+ # `a <=> b` returning < 0 means a comes first. For min we want
40545+ # the element that comes first; for max the element that comes
40546+ # last. CRuby: min returns the elem whose cmp(elem, best) < 0
40547+ # picks it; max picks when cmp(elem, best) > 0.
40548+ pick_cmp_mm = mname == "min" ? "< 0" : "> 0"
40549+ emit(" mrb_int _cmp = " + bexpr_mm + ";")
40550+ emit(" if (_cmp " + pick_cmp_mm + ") " + tmp_res + " = lv_" + bp1 + ";")
40551+ emit(" }")
40552+ pop_scope
40553+ emit(" }")
40554+ return tmp_res
40555+ end
40556+ tmp_key = new_temp
4043140557 bp_tmp = new_temp
4043240558 emit(" " + c_type(elem_type) + " " + tmp_res + " = " + c_default_val(elem_type) + ";")
4043340559 emit(" mrb_int " + tmp_key + " = 0;")