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

@@ -2751,6 +2751,18 @@
27512751 end
27522752 return "int_array"
27532753 end
2754+ if mname == "transpose"
2755+ # Transposing a matrix preserves its shape type — `[[Int]]` stays
2756+ # `[[Int]]` (issue #156). Only ptr_array-of-T_array receivers are
2757+ # currently supported by codegen; other shapes fall through to
2758+ # the unresolved-call warning at emit time.
2759+ if recv >= 0
2760+ rt = infer_type(recv)
2761+ if is_ptr_array_type(rt) == 1
2762+ return rt
2763+ end
2764+ end
2765+ end
27542766 if mname == "flat_map"
27552767 if recv >= 0
27562768 # Block returns an array; result type matches block return type
@@ -17961,6 +17973,29 @@
1796117973 if mname == "empty?"
1796217974 return "sp_PtrArray_empty(" + rc + ")"
1796317975 end
17976+ # Issue #156: transpose for [[Int]] (int_array_ptr_array). Other
17977+ # nested-array element shapes are not yet supported and fall
17978+ # through to the unresolved-call warning.
17979+ if mname == "transpose" && elem_type == "int_array"
17980+ @needs_int_array = 1
17981+ tmp = new_temp
17982+ col = new_temp
17983+ itmp = new_temp
17984+ jtmp = new_temp
17985+ cols = new_temp
17986+ emit(" sp_PtrArray *" + tmp + " = sp_PtrArray_new();")
17987+ emit(" if (sp_PtrArray_length(" + rc + ") > 0) {")
17988+ emit(" mrb_int " + cols + " = sp_IntArray_length((sp_IntArray *)sp_PtrArray_get(" + rc + ", 0));")
17989+ emit(" for (mrb_int " + jtmp + " = 0; " + jtmp + " < " + cols + "; " + jtmp + "++) {")
17990+ emit(" sp_IntArray *" + col + " = sp_IntArray_new();")
17991+ emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_PtrArray_length(" + rc + "); " + itmp + "++) {")
17992+ emit(" sp_IntArray_push(" + col + ", sp_IntArray_get((sp_IntArray *)sp_PtrArray_get(" + rc + ", " + itmp + "), " + jtmp + "));")
17993+ emit(" }")
17994+ emit(" sp_PtrArray_push(" + tmp + ", " + col + ");")
17995+ emit(" }")
17996+ emit(" }")
17997+ return tmp
17998+ end
1796417999 end
1796518000 if recv_type == "str_array"
1796618001 if mname == "length"