Commit 2e5fed48
2e5fed4870eeMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · ~3 modified
Description
fix(runtime,codegen,analyze): Hash#invert returns poly_poly_hash (#738)
Hash#invert swaps keys and values. The result variant depends on the input shape:
- str_str_hash -> str_str_hash (existing helper preserves type).
- str_int_hash -> poly_poly_hash (int keys, str values).
- sym_int_hash -> poly_poly_hash (int keys, sym values).
- sym_str_hash -> poly_poly_hash.
- int_str_hash -> poly_poly_hash.
Implementation:
- lib/sp_runtime.h: sp_StrIntHash_invert_poly, sp_IntStrHash_invert
use poly_poly_hash. sp_PolyPolyHash_inspect added (was missing;
uses sp_poly_inspect on key + value, mirrors Ruby's
{k=>v}/{k: v}shorthand for sym keys). - spinel_codegen.rb: invert arm per hash variant. emit_raw for sym_int/sym_str variants. poly_poly_hash recv arm gets an inspect/to_s branch routed to sp_PolyPolyHash_inspect.
- spinel_analyze.rb: invert returns str_str_hash for str_str recv, poly_poly_hash for other hash variants.
Closes #738
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 2e5fed4870ee
- Parent
- c00129fb33c2
lib/sp_runtime.hmodifiedView file
@@ -2263,6 +2263,17 @@
22632263
static sp_PolyArray*sp_PolyPolyHash_keys(sp_PolyPolyHash*h){sp_PolyArray*a=sp_PolyArray_new();for(mrb_int i=0;i<h->len;i++)sp_PolyArray_push(a,h->keys[h->order[i]]);return a;}22642264
static sp_PolyArray*sp_PolyPolyHash_values(sp_PolyPolyHash*h){sp_PolyArray*a=sp_PolyArray_new();for(mrb_int i=0;i<h->len;i++)sp_PolyArray_push(a,h->vals[h->order[i]]);return a;}22652265
static sp_PolyPolyHash*sp_PolyPolyHash_dup(sp_PolyPolyHash*h){sp_PolyPolyHash*r=sp_PolyPolyHash_new();for(mrb_int i=0;i<h->len;i++)sp_PolyPolyHash_set(r,h->keys[h->order[i]],h->vals[h->order[i]]);return r;}2266+
/* Issue #738: poly_poly_hash inspect using sp_poly_inspect on each2267+
k,v. Output mirrors Ruby's `{k=>v, ...}` for non-symbol keys and2268+
`{k: v, ...}` shorthand for symbol keys. */2269+
static const char *sp_poly_inspect(sp_RbVal v);2270+
static const char*sp_PolyPolyHash_inspect(sp_PolyPolyHash*h){sp_String*s=sp_String_new("{");if(!h){sp_String_append(s,"}");return s->data;}for(mrb_int i=0;i<h->len;i++){if(i>0)sp_String_append(s,", ");sp_RbVal k=h->keys[h->order[i]];if(k.tag==SP_TAG_SYM){sp_String_append(s,sp_sym_to_s((sp_sym)k.v.i));sp_String_append(s,": ");}else{sp_String_append(s,sp_poly_inspect(k));sp_String_append(s,"=>");}sp_String_append(s,sp_poly_inspect(h->vals[h->order[i]]));}sp_String_append(s,"}");return s->data;}2271+
/* Issue #738: Hash#invert -- swap keys and values. Returns a2272+
poly_poly_hash so any (key, value) pair shape is uniformly2273+
representable. str_str_hash_invert lives above (line ~1132)2274+
and stays as a same-type round-trip. */2275+
static sp_PolyPolyHash*sp_StrIntHash_invert_poly(sp_StrIntHash*h){sp_PolyPolyHash*r=sp_PolyPolyHash_new();if(!h)return r;for(mrb_int i=0;i<h->len;i++)sp_PolyPolyHash_set(r,sp_box_int(sp_StrIntHash_get(h,h->order[i])),sp_box_str(h->order[i]));return r;}2276+
static sp_PolyPolyHash*sp_IntStrHash_invert(sp_IntStrHash*h){sp_PolyPolyHash*r=sp_PolyPolyHash_new();if(!h)return r;for(mrb_int i=0;i<h->len;i++)sp_PolyPolyHash_set(r,sp_box_str(sp_IntStrHash_get(h,h->order[i])),sp_box_int(h->order[i]));return r;}22662277
static mrb_bool sp_PolyPolyHash_eq(sp_PolyPolyHash*a,sp_PolyPolyHash*b){if(!a||!b)return a==b;if(a->len!=b->len)return FALSE;for(mrb_int i=0;i<a->len;i++){sp_RbVal k=a->keys[a->order[i]];if(!sp_PolyPolyHash_has_key(b,k))return FALSE;if(!sp_poly_eq(sp_PolyPolyHash_get(a,k),sp_PolyPolyHash_get(b,k)))return FALSE;}return TRUE;}22672278
22682279
#include <setjmp.h>spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…