Commit d161a0db

d161a0dbbf31Merged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

feat: Array#* (repeat) for every typed array kind

arr * n previously fell through to the default * handler and emitted ptr * int at the C level — a type error. Add codegen and inference for the repeat form. Every typed array Spinel knows about (int / float / str / sym / poly) exposes the same _new / _length / _get / _push shape, so the loop is a single template parameterized by array_c_prefix(lt).

Reuses the existing is_array_type and array_c_prefix helpers; adds mark_array_runtime_needs(t) to centralize the per-type @needs_* runtime flag — there were already several open-coded copies of the same 4-way switch elsewhere in the file (zip, +, concat, ...).

*_ptr_array is intentionally outside the scope of is_array_type upstream (sp_PtrArray lacks _dup/_shuffle and several other helpers), so it inherits that exclusion.

Enables patterns like @prg_ref = [nil] * 0x10000 and mem = [[0] * 8] * 16 for optcarrot-style preallocation.

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

Details

Commit
d161a0dbbf31
Parent
0c3c5d918fc5

spinel_codegen.rbmodifiedView file

@@ -1726,6 +1726,10 @@
17261726 if lt == "poly"
17271727 return "poly"
17281728 end
1729+ if is_array_type(lt) == 1
1730+ # Array#* (repeat) yields another array of the same element type.
1731+ return lt
1732+ end
17291733 # Check RHS for float promotion
17301734 args_id = @nd_arguments[nid]
17311735 if args_id >= 0
@@ -3553,6 +3557,19 @@
35533557 0
35543558 end
35553559
3560+ # Set the right @needs_<runtime> flag for the given array type.
3561+ def mark_array_runtime_needs(t)
3562+ if t == "float_array"
3563+ @needs_float_array = 1
3564+ elsif t == "str_array"
3565+ @needs_str_array = 1
3566+ elsif t == "poly_array"
3567+ @needs_rb_value = 1
3568+ else
3569+ @needs_int_array = 1
3570+ end
3571+ end
3572+
35563573 # ---- Collection pass ----
35573574 def collect_all
35583575 root = @root_id
@@ -13863,6 +13880,22 @@
1386313880 @needs_rb_value = 1
1386413881 return "sp_poly_mul(" + compile_expr(recv) + ", " + box_expr_to_poly(get_args(@nd_arguments[nid])[0]) + ")"
1386513882 end
13883+ if is_array_type(lt) == 1
13884+ # All array kinds expose `_new` / `_length` / `_get` / `_push` with
13885+ # the same shape, so the repeat loop is a single template
13886+ # parameterized by the C prefix.
13887+ mark_array_runtime_needs(lt)
13888+ @needs_gc = 1
13889+ pfx = array_c_prefix(lt)
13890+ tmp = new_temp
13891+ src = new_temp
13892+ cnt = new_temp
13893+ emit(" sp_" + pfx + " *" + src + " = " + compile_expr(recv) + ";")
13894+ emit(" mrb_int " + cnt + " = " + compile_arg0(nid) + ";")
13895+ emit(" sp_" + pfx + " *" + tmp + " = sp_" + pfx + "_new();")
13896+ emit(" { mrb_int _mi; mrb_int _sl = sp_" + pfx + "_length(" + src + "); for (_mi = 0; _mi < " + cnt + "; _mi++) { mrb_int _mj; for (_mj = 0; _mj < _sl; _mj++) sp_" + pfx + "_push(" + tmp + ", sp_" + pfx + "_get(" + src + ", _mj)); } }")
13897+ return tmp
13898+ end
1386613899 return "(" + compile_expr(recv) + " * " + compile_arg0(nid) + ")"
1386713900 end
1386813901 if mname == "/"