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
return "sp_IntStrHash_get(" + rc + ", " + key + ")" end end # transform_values block form. Mirrors str_int_hash's arm: walk # self, run the block on each value, build a new hash with the # same keys and the block's returns. Result type matches the # input hash type. if mname == "transform_values" if @nd_block[nid] >= 0 blk = @nd_block[nid] bp = get_block_param(nid, 0) tmp = new_temp emit(" sp_IntStrHash *" + tmp + " = sp_IntStrHash_new();") emit(" for (mrb_int _i = 0; _i < " + rc + "->len; _i++) {") emit(" const char *lv_" + bp + " = sp_IntStrHash_get(" + rc + ", " + rc + "->order[_i]);") push_scope declare_var(bp, "string") bbody = @nd_body[blk] bexpr = "0" if bbody >= 0 bs = get_stmts(bbody) if bs.length > 0 bexpr = compile_expr(bs.last) end end emit(" sp_IntStrHash_set(" + tmp + ", " + rc + "->order[_i], " + bexpr + ");") pop_scope emit(" }") return tmp end end end if recv_type == "str_str_hash" if mname == "[]"test/hash_transform_values_int_str.rbaddedView file
Loading diff…