Commit fead3d12

fead3d12c946Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

feat: add Array#concat (mutating) and Array#+ (non-mutating)

concat appends all elements from another array in-place.

  • creates a new array by duplicating then appending. Both work for int_array, str_array, and float_array.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
fead3d12c946
Parent
0fe35a10a06c

spinel_codegen.rbmodifiedView file

@@ -1322,6 +1322,9 @@
13221322 if lt == "poly"
13231323 return "poly"
13241324 end
1325+ if lt == "int_array" || lt == "str_array" || lt == "float_array"
1326+ return lt
1327+ end
13251328 if lt == "float"
13261329 return "float"
13271330 end
@@ -11214,6 +11217,17 @@
1121411217 @needs_string_helpers = 1
1121511218 return "sp_poly_add(" + compile_expr(recv) + ", " + box_expr_to_poly(@nd_arguments[nid] >= 0 ? get_args(@nd_arguments[nid])[0] : -1) + ")"
1121611219 end
11220+ if lt == "int_array" || lt == "str_array" || lt == "float_array"
11221+ rc = compile_expr(recv)
11222+ arg = compile_arg0(nid)
11223+ pfx = array_c_prefix(lt)
11224+ tmp = new_temp
11225+ itmp = new_temp
11226+ emit(" " + c_type(lt) + tmp + " = sp_" + pfx + "_dup(" + rc + ");")
11227+ emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_" + pfx + "_length(" + arg + "); " + itmp + "++)")
11228+ emit(" sp_" + pfx + "_push(" + tmp + ", sp_" + pfx + "_get(" + arg + ", " + itmp + "));")
11229+ return tmp
11230+ end
1121711231 return "(" + compile_expr(recv) + " + " + compile_arg0(nid) + ")"
1121811232 end
1121911233 if mname == "-"
@@ -14391,6 +14405,22 @@
1439114405 end
1439214406 end
1439314407
14408+ # concat on array (mutating append)
14409+ if mname == "concat"
14410+ if recv >= 0
14411+ rt = infer_type(recv)
14412+ if rt == "int_array" || rt == "str_array" || rt == "float_array"
14413+ rc = compile_expr(recv)
14414+ arg = compile_arg0(nid)
14415+ pfx = array_c_prefix(rt)
14416+ tmp = new_temp
14417+ emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_" + pfx + "_length(" + arg + "); " + tmp + "++)")
14418+ emit(" sp_" + pfx + "_push(" + rc + ", sp_" + pfx + "_get(" + arg + ", " + tmp + "));")
14419+ return 1
14420+ end
14421+ end
14422+ end
14423+
1439414424 # replace on string (mutating reassign)
1439514425 if mname == "replace"
1439614426 if recv >= 0
@@ -17316,7 +17346,7 @@
1731617346 lt = @nd_type[last]
1731717347 if lt == "CallNode"
1731817348 lm = @nd_name[last]
17319- if lm == "[]=" || lm == "push" || lm == "pop" || lm == "emit" || lm == "emit_raw" || lm == "puts" || lm == "print" || lm == "p" || lm == "printf" || lm == "warn" || lm == "raise" || lm == "exit" || lm == "delete" || lm == "clear" || lm == "reverse!" || lm == "sort!" || lm == "each" || lm == "times" || lm == "upto" || lm == "downto"
17349+ if lm == "[]=" || lm == "push" || lm == "pop" || lm == "emit" || lm == "emit_raw" || lm == "puts" || lm == "print" || lm == "p" || lm == "printf" || lm == "warn" || lm == "raise" || lm == "exit" || lm == "delete" || lm == "clear" || lm == "concat" || lm == "reverse!" || lm == "sort!" || lm == "each" || lm == "times" || lm == "upto" || lm == "downto"
1732017350 compile_stmt(last)
1732117351 if return_type != "void"
1732217352 emit(" return " + c_return_default(return_type) + ";")