Commit e139e3a8

e139e3a8b94cMerged

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

Description

Merge pull request #151 from ryanseys/rs-hash-transform-keys-block

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

Details

Commit
e139e3a8b94c
Parent
8111db75af2f
Merge parent
b598846f7840

spinel_codegen.rbmodifiedView file

@@ -2759,6 +2759,12 @@
27592759 end
27602760 return "str_int_hash"
27612761 end
2762+ if mname == "transform_keys"
2763+ if recv >= 0
2764+ return infer_type(recv)
2765+ end
2766+ return "str_int_hash"
2767+ end
27622768 if mname == "zip"
27632769 if recv >= 0
27642770 rt = infer_type(recv)
@@ -17931,6 +17937,34 @@
1793117937 return tmp
1793217938 end
1793317939 end
17940+ # transform_keys: apply the block to each key, keep the matching
17941+ # value, build a new hash. Result type matches the input hash
17942+ # type when the block returns the same key C-type — the common
17943+ # case for `transform_keys { |k| k.upcase }` etc.
17944+ if mname == "transform_keys"
17945+ if @nd_block[nid] >= 0
17946+ blk = @nd_block[nid]
17947+ bp = get_block_param(nid, 0)
17948+ tmp = new_temp
17949+ emit(" sp_StrIntHash *" + tmp + " = sp_StrIntHash_new();")
17950+ emit(" for (mrb_int _i = 0; _i < " + rc + "->len; _i++) {")
17951+ emit(" const char *lv_" + bp + " = " + rc + "->order[_i];")
17952+ push_scope
17953+ declare_var(bp, "string")
17954+ bbody = @nd_body[blk]
17955+ bexpr = "lv_" + bp
17956+ if bbody >= 0
17957+ bs = get_stmts(bbody)
17958+ if bs.length > 0
17959+ bexpr = compile_expr(bs.last)
17960+ end
17961+ end
17962+ emit(" sp_StrIntHash_set(" + tmp + ", " + bexpr + ", sp_StrIntHash_get(" + rc + ", " + rc + "->order[_i]));")
17963+ pop_scope
17964+ emit(" }")
17965+ return tmp
17966+ end
17967+ end
1793417968 end
1793517969 if recv_type == "int_str_hash"
1793617970 @needs_int_str_hash = 1