Commit 216aa554

216aa5542143Merged

Ryan Seys <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

feat(codegen): Hash#transform_values block form for int_str_hash

Mirror image of the existing str_int_hash#transform_values arm. The str_int_hash form already shipped (and the matching str_int_hash# transform_keys arm landed earlier in this series); this commit extends transform_values to int-keyed-string hashes so int_str_hash gets parity.

Dispatch arm in compile_call_expr's int_str_hash block:

sp_IntStrHash *t = sp_IntStrHash_new();
for (mrb_int _i = 0; _i < rc->len; _i++) {
  const char *lv_<bp> = sp_IntStrHash_get(rc, rc->order[_i]);
  sp_IntStrHash_set(t, rc->order[_i], <block-body>);
}

Block param is typed const char * to match the value type. Same "return type matches receiver type" inference rule (infer_method_name_type's transform_values arm already returns infer_type(recv) when recv >= 0, so int_str_hash receivers infer int_str_hash result without needing a code change there).

Test exercises:

  • identity transform (values unchanged on lookup)
  • upcase transform (values changed; lookup against new values)
  • string concat suffix
  • length preserved across transform

Out of scope: empty-hash transform (empty = {}; empty[1] = "x"; empty.transform_values { ... }) — hits a separate empty-hash type-promotion bug, which is deferred. The non-empty shapes covered here all pass.

Bootstrap: gen2.c == gen3.c. Tests: 218 pass.

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

Details

Commit
216aa5542143
Parent
02005f26deba

spinel_codegen.rbmodifiedView file

@@ -17635,6 +17635,34 @@
1763517635 return "sp_IntStrHash_get(" + rc + ", " + key + ")"
1763617636 end
1763717637 end
17638+ # transform_values block form. Mirrors str_int_hash's arm: walk
17639+ # self, run the block on each value, build a new hash with the
17640+ # same keys and the block's returns. Result type matches the
17641+ # input hash type.
17642+ if mname == "transform_values"
17643+ if @nd_block[nid] >= 0
17644+ blk = @nd_block[nid]
17645+ bp = get_block_param(nid, 0)
17646+ tmp = new_temp
17647+ emit(" sp_IntStrHash *" + tmp + " = sp_IntStrHash_new();")
17648+ emit(" for (mrb_int _i = 0; _i < " + rc + "->len; _i++) {")
17649+ emit(" const char *lv_" + bp + " = sp_IntStrHash_get(" + rc + ", " + rc + "->order[_i]);")
17650+ push_scope
17651+ declare_var(bp, "string")
17652+ bbody = @nd_body[blk]
17653+ bexpr = "0"
17654+ if bbody >= 0
17655+ bs = get_stmts(bbody)
17656+ if bs.length > 0
17657+ bexpr = compile_expr(bs.last)
17658+ end
17659+ end
17660+ emit(" sp_IntStrHash_set(" + tmp + ", " + rc + "->order[_i], " + bexpr + ");")
17661+ pop_scope
17662+ emit(" }")
17663+ return tmp
17664+ end
17665+ end
1763817666 end
1763917667 if recv_type == "str_str_hash"
1764017668 if mname == "[]"