Commit ccffc61c
ccffc61c679fMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
feat: add Array#min, #max, #sum with block
min/max with block uses block result as comparison key, returning the element (not the key). sum with block accumulates block results. Also fixed min/max return type for str_array (returns string, not int) and expanded scan_locals block param type list.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- ccffc61c679f
- Parent
- 887c1e30ce20
spinel_codegen.rbmodifiedView file
@@ -1892,10 +1892,16 @@
18921892
end18931893
return "int"18941894
end1895-
if mname == "min"1896-
return "int"1897-
end1898-
if mname == "max"1895+
if mname == "min" || mname == "max"1896+
if recv >= 01897+
rt = infer_type(recv)1898+
if rt == "str_array"1899+
return "string"1900+
end1901+
if rt == "float_array"1902+
return "float"1903+
end1904+
end18991905
return "int"19001906
end19011907
if mname == "sum"@@ -9435,7 +9441,7 @@
94359441
end94369442
if mname == "times" || mname == "upto" || mname == "downto"94379443
types.push("int")9438-
elsif mname == "each" || mname == "each_pair" || mname == "map" || mname == "select" || mname == "filter" || mname == "reject" || mname == "find" || mname == "detect" || mname == "any?" || mname == "all?" || mname == "none?" || mname == "count" || mname == "min_by" || mname == "max_by" || mname == "sort_by" || mname == "flat_map" || mname == "filter_map"9444+
elsif mname == "each" || mname == "each_pair" || mname == "map" || mname == "select" || mname == "filter" || mname == "reject" || mname == "find" || mname == "detect" || mname == "any?" || mname == "all?" || mname == "none?" || mname == "one?" || mname == "count" || mname == "min" || mname == "max" || mname == "sum" || mname == "min_by" || mname == "max_by" || mname == "sort_by" || mname == "flat_map" || mname == "filter_map" || mname == "cycle"94399445
# Element iteration: infer block param from collection type94409446
if recv_type == "str_array"94419447
types.push("string")@@ -12357,6 +12363,12 @@
1235712363
if mname == "filter_map" && @nd_block[nid] >= 01235812364
return compile_array_filter_map(nid, rc, recv_type)1235912365
end12366+
if (mname == "sum") && @nd_block[nid] >= 012367+
return compile_array_sum_block(nid, rc, recv_type)12368+
end12369+
if (mname == "min" || mname == "max") && @nd_block[nid] >= 012370+
return compile_array_min_max_block(nid, rc, recv_type, mname)12371+
end1236012372
# Array methods1236112373
if recv_type == "int_array"1236212374
if mname == "length"@@ -17008,6 +17020,87 @@
1700817020
"0"1700917021
end1701017022
17023+
def compile_array_sum_block(nid, rc, recv_type)17024+
bp1 = get_block_param(nid, 0)17025+
if bp1 == ""17026+
bp1 = "_x"17027+
end17028+
elem_type = "int"17029+
if recv_type == "str_array"17030+
elem_type = "string"17031+
elsif recv_type == "float_array"17032+
elem_type = "float"17033+
end17034+
declare_var(bp1, elem_type)17035+
pfx = array_c_prefix(recv_type)17036+
tmp_sum = new_temp17037+
tmp_i = new_temp17038+
emit(" mrb_int " + tmp_sum + " = 0;")17039+
emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_" + pfx + "_length(" + rc + "); " + tmp_i + "++) {")17040+
emit(" lv_" + bp1 + " = sp_" + pfx + "_get(" + rc + ", " + tmp_i + ");")17041+
blk = @nd_block[nid]17042+
bexpr = "0"17043+
if @nd_body[blk] >= 017044+
bs = get_stmts(@nd_body[blk])17045+
if bs.length > 017046+
k = 017047+
while k < bs.length - 117048+
compile_stmt(bs[k])17049+
k = k + 117050+
end17051+
bexpr = compile_expr(bs.last)17052+
end17053+
end17054+
emit(" " + tmp_sum + " += " + bexpr + ";")17055+
emit(" }")17056+
tmp_sum17057+
end17058+
17059+
def compile_array_min_max_block(nid, rc, recv_type, mname)17060+
bp1 = get_block_param(nid, 0)17061+
if bp1 == ""17062+
bp1 = "_x"17063+
end17064+
elem_type = "int"17065+
if recv_type == "str_array"17066+
elem_type = "string"17067+
elsif recv_type == "float_array"17068+
elem_type = "float"17069+
end17070+
set_var_type(bp1, elem_type)17071+
pfx = array_c_prefix(recv_type)17072+
tmp_res = new_temp17073+
tmp_key = new_temp17074+
tmp_i = new_temp17075+
bp_tmp = new_temp17076+
emit(" " + c_type(elem_type) + " " + tmp_res + " = " + c_default_val(elem_type) + ";")17077+
emit(" mrb_int " + tmp_key + " = 0;")17078+
emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_" + pfx + "_length(" + rc + "); " + tmp_i + "++) {")17079+
emit(" " + c_type(elem_type) + " " + bp_tmp + " = sp_" + pfx + "_get(" + rc + ", " + tmp_i + ");")17080+
emit(" lv_" + bp1 + " = " + bp_tmp + ";")17081+
blk = @nd_block[nid]17082+
bexpr = "0"17083+
if @nd_body[blk] >= 017084+
bs = get_stmts(@nd_body[blk])17085+
if bs.length > 017086+
k = 017087+
while k < bs.length - 117088+
compile_stmt(bs[k])17089+
k = k + 117090+
end17091+
bexpr = compile_expr(bs.last)17092+
end17093+
end17094+
cmp = ">"17095+
if mname == "min"17096+
cmp = "<"17097+
end17098+
emit(" mrb_int _k = " + bexpr + ";")17099+
emit(" if (" + tmp_i + " == 0 || _k " + cmp + " " + tmp_key + ") { " + tmp_res + " = " + bp_tmp + "; " + tmp_key + " = _k; }")17100+
emit(" }")17101+
tmp_res17102+
end17103+
1701117104
def compile_array_filter_map(nid, rc, recv_type)1701217105
bp1 = get_block_param(nid, 0)1701317106
if bp1 == ""