Commit 35ab107f
35ab107f4c5bMerged
Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
feat(codegen): nested array literals (3D+) via PolyArray-of-PolyArray
3D array literals [[[a, b], [c, d]], [[e, f], [g, h]]]
infer the second-level [[a, b], [c, d]] as
int_array_ptr_array, but the outer level used to box each
element via sp_box_ptr_array — losing the elem-type info,
so the next [] read returned an unknown-cls_id obj that
fell through every dispatch arm to sp_box_nil().
Fix: when an array literal's element is itself a typed
<X>_ptr_array (or already poly_array), recompile that
inner literal as poly_array instead. Each level boxes via
sp_box_poly_array / sp_box_int_array / etc., so the
cls_id chain stays tagged and arr[i][j][k] recurses
through three SP_BUILTIN_POLY_ARRAY / SP_BUILTIN_INT_ARRAY
dispatch arms.
Adds compile_array_literal_as_poly (recursive helper) and
wires it into all three IVW-with-ArrayNode-RHS paths
(compile_array_literal's poly_array branch,
emit_constructor's IVW path, and compile_stmt's general
IVW path).
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 35ab107f4c5b
- Parent
- 0fc98c358452
spinel_codegen.rbmodifiedView file
end return "poly_array" end # Nested-deeper case: elements are themselves a typed # ptr_array (`int_array_ptr_array`, etc.) or already # poly_array. Spinel doesn't have a typed # `<X>_ptr_array_ptr_array` slot, so box each level via # poly_array — sp_box_*_array on each push, and the # poly-builtin dispatch on `[]` recurses into the next # level. if is_ptr_array_type(et) == 1 || et == "poly_array" @needs_gc = 1 @needs_rb_value = 1 return "poly_array" end # Check if elements have mixed types k = 1 while k < elems.length while ek < elems_iv.length eid = elems_iv[ek] et = infer_type(eid) ev = compile_expr(eid) ebox = et == "poly" ? ev : box_value_to_poly(et, ev) emit_raw(" sp_PolyArray_push(" + tmp_iv + ", " + ebox + ");") # 3D+ shape: nested ArrayNode with ptr_array elem # type → recompile as poly_array so cls_id chain # stays tagged through every level. if @nd_type[eid] == "ArrayNode" && (is_ptr_array_type(et) == 1 || et == "poly_array") inner_iv = compile_array_literal_as_poly(eid) emit_raw(" sp_PolyArray_push(" + tmp_iv + ", sp_box_poly_array(" + inner_iv + "));") else ev = compile_expr(eid) ebox = et == "poly" ? ev : box_value_to_poly(et, ev) emit_raw(" sp_PolyArray_push(" + tmp_iv + ", " + ebox + ");") end ek = ek + 1 end emit_raw(" " + self_arrow + ivar + " = " + tmp_iv + ";") "(!" + cond + " ? " + then_val + " : 0)" end # Compile an `ArrayNode` literal as `sp_PolyArray *`, regardless # of the inferred elem type. Used by the nested-array path in # the poly_array branch so 3D-and-deeper arrays preserve cls_id # tags through every level (e.g. an inner `[1,2,3]` ends up # boxed via `sp_box_int_array` so the dispatch can still # `sp_IntArray_get` on it). def compile_array_literal_as_poly(nid) @needs_gc = 1 @needs_rb_value = 1 elems = parse_id_list(@nd_elements[nid]) tmp = new_temp emit(" sp_PolyArray *" + tmp + " = sp_PolyArray_new();") k = 0 while k < elems.length eid = elems[k] et = infer_type(eid) if @nd_type[eid] == "ArrayNode" && (is_ptr_array_type(et) == 1 || et == "poly_array") inner = compile_array_literal_as_poly(eid) emit(" sp_PolyArray_push(" + tmp + ", sp_box_poly_array(" + inner + "));") else val = compile_expr(eid) emit(" sp_PolyArray_push(" + tmp + ", " + box_value_to_poly(et, val) + ");") end k = k + 1 end tmp end def compile_array_literal(nid) @needs_gc = 1 elems = parse_id_list(@nd_elements[nid]) k = 0 while k < elems.length et = infer_type(elems[k]) val = compile_expr(elems[k]) emit(" sp_PolyArray_push(" + tmp + ", " + box_value_to_poly(et, val) + ");") # 3D+ shape: when the element is itself a typed # `<X>_ptr_array` that came from a nested ArrayNode, the # default `compile_array_literal` for that element would # emit a typed sp_PtrArray and `sp_box_ptr_array` would # erase the elem type (cls_id PTR_ARRAY). Recompile the # nested literal as poly_array so the next-level dispatch # sees `cls_id == POLY_ARRAY` and recurses with each # innermost typed array still tagged correctly. if @nd_type[elems[k]] == "ArrayNode" && (is_ptr_array_type(et) == 1 || et == "poly_array") val = compile_array_literal_as_poly(elems[k]) emit(" sp_PolyArray_push(" + tmp + ", sp_box_poly_array(" + val + "));") else val = compile_expr(elems[k]) emit(" sp_PolyArray_push(" + tmp + ", " + box_value_to_poly(et, val) + ");") end k = k + 1 end return tmp while ek < elems_pa.length eid = elems_pa[ek] et = infer_type(eid) ev = compile_expr(eid) ebox = et == "poly" ? ev : box_value_to_poly(et, ev) emit(" sp_PolyArray_push(" + tmp_arr + ", " + ebox + ");") # 3D+ shape: nested ArrayNode with ptr_array element # type — recompile as poly_array so cls_id chain stays # tagged through every level. if @nd_type[eid] == "ArrayNode" && (is_ptr_array_type(et) == 1 || et == "poly_array") inner = compile_array_literal_as_poly(eid) emit(" sp_PolyArray_push(" + tmp_arr + ", sp_box_poly_array(" + inner + "));") else ev = compile_expr(eid) ebox = et == "poly" ? ev : box_value_to_poly(et, ev) emit(" sp_PolyArray_push(" + tmp_arr + ", " + ebox + ");") end ek = ek + 1 end emit(" " + self_arrow + sanitize_ivar(iname) + " = " + tmp_arr + ";")test/array_3d_nested.rbaddedView file
Loading diff…