Commit a09a6b8b

a09a6b8b339bMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +1 added · ~2 modified

Description

feat(codegen): inspect for nested T_array_ptr_array (p / inspect)

p [[1, 2], [3, 4]] previously printed a pointer address because compile_inspect_for had no branch for ptr_array types and fell through to compile_puts_single's printf("%lld", ...) default.

Adds four runtime helpers — sp_{Int,Float,Str,Sym}ArrayPtrArray_inspect — that recurse into the matching primitive inspect for each element, and a compile_inspect_for branch dispatching on the four T_array_ptr_array shapes. Output now matches CRuby byte-for-byte for nested arrays of the four built-in T_array element types.

obj__ptr_array and poly_array_ptr_array still fall through; those need their own per-element formatting and aren't covered here.

Closes #169

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

Details

Commit
a09a6b8b339b
Parent
a2504c560496

lib/sp_runtime.hmodifiedView file

@@ -490,6 +490,13 @@
490490 /* PtrArray elements are object pointers without a per-element class
491491 tag, so we render them as `#<Object>` rather than recursing. */
492492 static const char*sp_PtrArray_inspect(sp_PtrArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,"#<Object>");}sp_String_append(s,"]");return s->data;}
493+/* Nested-array inspect: when codegen knows the ptr_array's element
494+ type is one of the four built-in T_array shapes, recurse into the
495+ matching primitive inspect (issue #169). */
496+static const char*sp_IntArrayPtrArray_inspect(sp_PtrArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_IntArray_inspect((sp_IntArray*)a->data[i]));}sp_String_append(s,"]");return s->data;}
497+static const char*sp_FloatArrayPtrArray_inspect(sp_PtrArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_FloatArray_inspect((sp_FloatArray*)a->data[i]));}sp_String_append(s,"]");return s->data;}
498+static const char*sp_StrArrayPtrArray_inspect(sp_PtrArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_StrArray_inspect((sp_StrArray*)a->data[i]));}sp_String_append(s,"]");return s->data;}
499+static const char*sp_SymArrayPtrArray_inspect(sp_PtrArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_SymArray_inspect((sp_IntArray*)a->data[i]));}sp_String_append(s,"]");return s->data;}
493500
494501 /* Regexp engine (link with libspre.a from lib/regexp/) */
495502 typedef struct mrb_regexp_pattern mrb_regexp_pattern;