Commit b598846f

b598846f7840Merged

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

Description

feat(codegen): Hash#transform_keys block form for str_int_hash

Mirror image of the existing Hash#transform_values block form. Runs the block once per key, uses the return value as the new key, preserves the original value, and builds a fresh hash of the same type. Common usage shape: h.transform_keys { |k| k.upcase }.

Dispatch arm in compile_call_expr's str_int_hash block:

sp_StrIntHash *t = sp_StrIntHash_new();
for (mrb_int _i = 0; _i < rc->len; _i++) {
  const char *lv_<bp> = rc->order[_i];
  sp_StrIntHash_set(t, <block-body>,
                    sp_StrIntHash_get(rc, rc->order[_i]));
}

infer_method_name_type gets a transform_keys arm that returns the receiver type unchanged — symmetric with transform_values. The result type assumes the block returns the same key C-type as the input (string -> string for str_int_hash). Other key-type combinations (e.g. block returns symbol, switching the result to sym_int_hash) are out of scope and would need infer_block_return_type plus matching code paths.

Test exercises:

  • identity transform (lookup against original keys works)
  • upcase transform (lookup against new keys works; old keys gone)
  • key concatenation with a suffix
  • empty hash transform yields empty
  • length preserved across transform

Out of scope: int_str_hash#transform_keys (would need a separate arm at line ~17688), str_str_hash, sym_int_hash, sym_str_hash — all mechanical mirrors. Block returning a different key type (string -> symbol => sym_int_hash result) is also deferred.

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

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

Details

Commit
b598846f7840
Parent
02005f26deba

spinel_codegen.rbmodifiedView file

@@ -2758,6 +2758,12 @@
27582758 end
27592759 return "str_int_hash"
27602760 end
2761+ if mname == "transform_keys"
2762+ if recv >= 0
2763+ return infer_type(recv)
2764+ end
2765+ return "str_int_hash"
2766+ end
27612767 if mname == "zip"
27622768 if recv >= 0
27632769 rt = infer_type(recv)
@@ -17597,6 +17603,34 @@
1759717603 return tmp
1759817604 end
1759917605 end
17606+ # transform_keys: apply the block to each key, keep the matching
17607+ # value, build a new hash. Result type matches the input hash
17608+ # type when the block returns the same key C-type — the common
17609+ # case for `transform_keys { |k| k.upcase }` etc.
17610+ if mname == "transform_keys"
17611+ if @nd_block[nid] >= 0
17612+ blk = @nd_block[nid]
17613+ bp = get_block_param(nid, 0)
17614+ tmp = new_temp
17615+ emit(" sp_StrIntHash *" + tmp + " = sp_StrIntHash_new();")
17616+ emit(" for (mrb_int _i = 0; _i < " + rc + "->len; _i++) {")
17617+ emit(" const char *lv_" + bp + " = " + rc + "->order[_i];")
17618+ push_scope
17619+ declare_var(bp, "string")
17620+ bbody = @nd_body[blk]
17621+ bexpr = "lv_" + bp
17622+ if bbody >= 0
17623+ bs = get_stmts(bbody)
17624+ if bs.length > 0
17625+ bexpr = compile_expr(bs.last)
17626+ end
17627+ end
17628+ emit(" sp_StrIntHash_set(" + tmp + ", " + bexpr + ", sp_StrIntHash_get(" + rc + ", " + rc + "->order[_i]));")
17629+ pop_scope
17630+ emit(" }")
17631+ return tmp
17632+ end
17633+ end
1760017634 end
1760117635 if recv_type == "int_str_hash"
1760217636 @needs_int_str_hash = 1