Commit 43613f6e
43613f6e305eMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
feat: add Integer#divmod, Array#minmax, Array#partition using tuples
divmod returns tuple:int,int with quotient and remainder. minmax returns tuple:T,T with min and max elements. partition returns tuple:arr,arr splitting by block predicate. All use the tuple type infrastructure for type-safe access.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 43613f6e305e
- Parent
- dd941226816b
spinel_codegen.rbmodifiedView file
@@ -1831,6 +1831,24 @@
18311831
if mname == "bit_length"18321832
return "int"18331833
end1834+
if mname == "divmod"1835+
return "tuple:int,int"1836+
end1837+
if mname == "minmax"1838+
if recv >= 01839+
rt = infer_type(recv)1840+
et = elem_type_of_array(rt)1841+
return "tuple:" + et + "," + et1842+
end1843+
return "tuple:int,int"1844+
end1845+
if mname == "partition"1846+
if recv >= 01847+
rt = infer_type(recv)1848+
return "tuple:" + rt + "," + rt1849+
end1850+
return "tuple:int_array,int_array"1851+
end18341852
if mname == "fdiv"18351853
return "float"18361854
end@@ -9620,7 +9638,7 @@
96209638
end96219639
if mname == "times" || mname == "upto" || mname == "downto"96229640
types.push("int")9623-
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"9641+
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" || mname == "partition"96249642
# Element iteration: infer block param from collection type96259643
if recv_type == "str_array"96269644
types.push("string")@@ -12452,6 +12470,18 @@
1245212470
if mname == "fdiv"1245312471
return "((mrb_float)(" + rc + ") / (mrb_float)" + compile_arg0(nid) + ")"1245412472
end12473+
if mname == "divmod"12474+
tt = "tuple:int,int"12475+
register_tuple_type(tt)12476+
@needs_gc = 112477+
name = tuple_c_name(tt)12478+
arg = compile_arg0(nid)12479+
tmp = new_temp12480+
emit(" " + name + " *" + tmp + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, NULL);")12481+
emit(" " + tmp + "->_0 = " + rc + " / " + arg + ";")12482+
emit(" " + tmp + "->_1 = sp_imod(" + rc + ", " + arg + ");")12483+
return tmp12484+
end1245512485
if mname == "to_i"1245612486
return rc1245712487
end@@ -12725,6 +12755,74 @@
1272512755
if (mname == "sum") && @nd_block[nid] >= 01272612756
return compile_array_sum_block(nid, rc, recv_type)1272712757
end12758+
if mname == "partition" && @nd_block[nid] >= 012759+
pfx = array_c_prefix(recv_type)12760+
tt = "tuple:" + recv_type + "," + recv_type12761+
register_tuple_type(tt)12762+
@needs_gc = 112763+
name = tuple_c_name(tt)12764+
bp1 = get_block_param(nid, 0)12765+
if bp1 == ""12766+
bp1 = "_x"12767+
end12768+
et = elem_type_of_array(recv_type)12769+
declare_var(bp1, et)12770+
tmp_t = new_temp12771+
tmp_f = new_temp12772+
tmp_res = new_temp12773+
itmp = new_temp12774+
emit(" " + c_type(recv_type) + " " + tmp_t + " = sp_" + pfx + "_new();")12775+
emit(" " + c_type(recv_type) + " " + tmp_f + " = sp_" + pfx + "_new();")12776+
emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_" + pfx + "_length(" + rc + "); " + itmp + "++) {")12777+
emit(" lv_" + bp1 + " = sp_" + pfx + "_get(" + rc + ", " + itmp + ");")12778+
blk = @nd_block[nid]12779+
bexpr = "0"12780+
if @nd_body[blk] >= 012781+
bs = get_stmts(@nd_body[blk])12782+
if bs.length > 012783+
k = 012784+
while k < bs.length - 112785+
compile_stmt(bs[k])12786+
k = k + 112787+
end12788+
bexpr = compile_expr(bs.last)12789+
end12790+
end12791+
emit(" if (" + bexpr + ") sp_" + pfx + "_push(" + tmp_t + ", lv_" + bp1 + "); else sp_" + pfx + "_push(" + tmp_f + ", lv_" + bp1 + ");")12792+
emit(" }")12793+
emit(" " + name + " *" + tmp_res + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, NULL);")12794+
emit(" " + tmp_res + "->_0 = " + tmp_t + ";")12795+
emit(" " + tmp_res + "->_1 = " + tmp_f + ";")12796+
return tmp_res12797+
end12798+
if mname == "minmax"12799+
pfx = array_c_prefix(recv_type)12800+
et = elem_type_of_array(recv_type)12801+
tt = "tuple:" + et + "," + et12802+
register_tuple_type(tt)12803+
@needs_gc = 112804+
name = tuple_c_name(tt)12805+
tmp = new_temp12806+
tmp_min = new_temp12807+
tmp_max = new_temp12808+
itmp = new_temp12809+
emit(" " + c_type(et) + " " + tmp_min + " = sp_" + pfx + "_get(" + rc + ", 0);")12810+
emit(" " + c_type(et) + " " + tmp_max + " = " + tmp_min + ";")12811+
emit(" for (mrb_int " + itmp + " = 1; " + itmp + " < sp_" + pfx + "_length(" + rc + "); " + itmp + "++) {")12812+
emit(" " + c_type(et) + " _v = sp_" + pfx + "_get(" + rc + ", " + itmp + ");")12813+
if et == "string"12814+
emit(" if (strcmp(_v, " + tmp_min + ") < 0) " + tmp_min + " = _v;")12815+
emit(" if (strcmp(_v, " + tmp_max + ") > 0) " + tmp_max + " = _v;")12816+
else12817+
emit(" if (_v < " + tmp_min + ") " + tmp_min + " = _v;")12818+
emit(" if (_v > " + tmp_max + ") " + tmp_max + " = _v;")12819+
end12820+
emit(" }")12821+
emit(" " + name + " *" + tmp + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, NULL);")12822+
emit(" " + tmp + "->_0 = " + tmp_min + ";")12823+
emit(" " + tmp + "->_1 = " + tmp_max + ";")12824+
return tmp12825+
end1272812826
if (mname == "min" || mname == "max") && @nd_block[nid] >= 01272912827
return compile_array_min_max_block(nid, rc, recv_type, mname)1273012828
end