Commit a2504c56
a2504c560496Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
feat(codegen): Array#transpose for [[Int]] (int_array_ptr_array)
[[1,2],[3,4]].transpose previously hit the unresolved-call path
and emitted 0 (issue #156). Implements transpose for receivers of
type int_array_ptr_array — allocates a new outer sp_PtrArray and
column-walks the input, building one sp_IntArray per column.
infer_type returns the receiver type unchanged (a [[Int]] transposes to a [[Int]]); other ptr_array element shapes (str_array_ptr_array, sym_array_ptr_array, ...) still fall through to the warn-and-emit-0 fallback and can be added incrementally.
Note: p [[1,3],[2,4]] still prints a pointer address rather than
[[1, 3], [2, 4]] because compile_inspect_for doesn't yet handle
ptr_array types — a separate pre-existing bug, not introduced or
fixed here. The repro in #156 now produces a correctly-transposed
array; printing it via p is the follow-up.
Refs #156
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- a2504c560496
- Parent
- 78de785dc7bf
spinel_codegen.rbmodifiedView file
end return "int_array" end if mname == "transpose" # Transposing a matrix preserves its shape type — `[[Int]]` stays # `[[Int]]` (issue #156). Only ptr_array-of-T_array receivers are # currently supported by codegen; other shapes fall through to # the unresolved-call warning at emit time. if recv >= 0 rt = infer_type(recv) if is_ptr_array_type(rt) == 1 return rt end end end if mname == "flat_map" if recv >= 0 # Block returns an array; result type matches block return type if mname == "empty?" return "sp_PtrArray_empty(" + rc + ")" end # Issue #156: transpose for [[Int]] (int_array_ptr_array). Other # nested-array element shapes are not yet supported and fall # through to the unresolved-call warning. if mname == "transpose" && elem_type == "int_array" @needs_int_array = 1 tmp = new_temp col = new_temp itmp = new_temp jtmp = new_temp cols = new_temp emit(" sp_PtrArray *" + tmp + " = sp_PtrArray_new();") emit(" if (sp_PtrArray_length(" + rc + ") > 0) {") emit(" mrb_int " + cols + " = sp_IntArray_length((sp_IntArray *)sp_PtrArray_get(" + rc + ", 0));") emit(" for (mrb_int " + jtmp + " = 0; " + jtmp + " < " + cols + "; " + jtmp + "++) {") emit(" sp_IntArray *" + col + " = sp_IntArray_new();") emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_PtrArray_length(" + rc + "); " + itmp + "++) {") emit(" sp_IntArray_push(" + col + ", sp_IntArray_get((sp_IntArray *)sp_PtrArray_get(" + rc + ", " + itmp + "), " + jtmp + "));") emit(" }") emit(" sp_PtrArray_push(" + tmp + ", " + col + ");") emit(" }") emit(" }") return tmp end end if recv_type == "str_array" if mname == "length"test/array_transpose.rbaddedView file
Loading diff…