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
if lt == "poly" return "poly" end if is_array_type(lt) == 1 # Array#* (repeat) yields another array of the same element type. return lt end # Check RHS for float promotion args_id = @nd_arguments[nid] if args_id >= 0 0 end # Set the right @needs_<runtime> flag for the given array type. def mark_array_runtime_needs(t) if t == "float_array" @needs_float_array = 1 elsif t == "str_array" @needs_str_array = 1 elsif t == "poly_array" @needs_rb_value = 1 else @needs_int_array = 1 end end # ---- Collection pass ---- def collect_all root = @root_id @needs_rb_value = 1 return "sp_poly_mul(" + compile_expr(recv) + ", " + box_expr_to_poly(get_args(@nd_arguments[nid])[0]) + ")" end if is_array_type(lt) == 1 # All array kinds expose `_new` / `_length` / `_get` / `_push` with # the same shape, so the repeat loop is a single template # parameterized by the C prefix. mark_array_runtime_needs(lt) @needs_gc = 1 pfx = array_c_prefix(lt) tmp = new_temp src = new_temp cnt = new_temp emit(" sp_" + pfx + " *" + src + " = " + compile_expr(recv) + ";") emit(" mrb_int " + cnt + " = " + compile_arg0(nid) + ";") emit(" sp_" + pfx + " *" + tmp + " = sp_" + pfx + "_new();") 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)); } }") return tmp end return "(" + compile_expr(recv) + " * " + compile_arg0(nid) + ")" end if mname == "/"test/array_mul.rbaddedView file
Loading diff…