Commit 61164457
6116445752a8Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~1 modified · -2 deleted
Description
Merge pull request #324 from ryanseys/fix/hash-transform-values
fix(codegen): Hash#transform_values for sym_int_hash + inspect
Details
- Commit
- 6116445752a8
- Parent
- be8666c66f8d
- Merge parent
- 374c7b6f58b0
spinel_codegen.rbmodifiedView file
@@ -14568,6 +14568,11 @@
1456814568
emit_raw("static void sp_SymIntHash_delete(sp_SymIntHash*h,sp_sym k){mrb_int idx=(mrb_int)(((mrb_int)k)&h->mask);while(h->keys[idx]>=0){if(h->keys[idx]==k){h->keys[idx]=-1;h->vals[idx]=0;h->len--;mrb_int j=(idx+1)&h->mask;while(h->keys[j]>=0){mrb_int nj=(mrb_int)(((mrb_int)h->keys[j])&h->mask);if((j>idx&&(nj<=idx||nj>j))||(j<idx&&nj<=idx&&nj>j)){h->keys[idx]=h->keys[j];h->vals[idx]=h->vals[j];h->keys[j]=-1;h->vals[j]=0;idx=j;}j=(j+1)&h->mask;}{mrb_int oi=0;while(oi<=h->len){if(h->order[oi]==k){while(oi<h->len){h->order[oi]=h->order[oi+1];oi++;}break;}oi++;}}return;}idx=(idx+1)&h->mask;}}")1456914569
emit_raw("static sp_IntArray*sp_SymIntHash_keys(sp_SymIntHash*h){sp_IntArray*a=sp_IntArray_new();for(mrb_int i=0;i<h->len;i++)sp_IntArray_push(a,(mrb_int)h->order[i]);return a;}")1457014570
emit_raw("static sp_IntArray*sp_SymIntHash_values(sp_SymIntHash*h){sp_IntArray*a=sp_IntArray_new();for(mrb_int i=0;i<h->len;i++)sp_IntArray_push(a,sp_SymIntHash_get(h,h->order[i]));return a;}")14571+
# Hash inspect — Ruby's modern shorthand `{k: v, ...}`. All keys14572+
# in a sym_int_hash are valid identifier symbols (the parser only14573+
# routes literal symbol keys here), so the bare-name form always14574+
# round-trips correctly.14575+
emit_raw("static const char*sp_SymIntHash_inspect(sp_SymIntHash*h){sp_String*s=sp_String_new(\"{\");for(mrb_int i=0;i<h->len;i++){if(i>0)sp_String_append(s,\", \");sp_String_append(s,sp_sym_to_s(h->order[i]));sp_String_append(s,\": \");sp_String_append(s,sp_int_to_s(sp_SymIntHash_get(h,h->order[i])));}sp_String_append(s,\"}\");return s->data;}")1457114576
# sym_array.tally — emitted alongside sp_SymIntHash because the1457214577
# helper depends on the typedef. sym_array storage is sp_IntArray1457314578
# (sym ids stored as mrb_int); cast each element to sp_sym for the@@ -24312,6 +24317,34 @@
2431224317
@needs_int_array = 12431324318
return "sp_SymIntHash_values(" + rc + ")"2431424319
end24320+
# transform_values { |v| ... } — same key set, block-transformed24321+
# int values. Keeps the result a sym_int_hash; the block's last24322+
# expression is set as the new value (truncated to mrb_int by24323+
# sp_SymIntHash_set's signature when the block returns int).24324+
if mname == "transform_values"24325+
if @nd_block[nid] >= 024326+
blk = @nd_block[nid]24327+
bp = get_block_param(nid, 0)24328+
tmp = new_temp24329+
emit(" sp_SymIntHash *" + tmp + " = sp_SymIntHash_new();")24330+
emit(" for (mrb_int _i = 0; _i < " + rc + "->len; _i++) {")24331+
emit(" mrb_int lv_" + bp + " = sp_SymIntHash_get(" + rc + ", " + rc + "->order[_i]);")24332+
push_scope24333+
declare_var(bp, "int")24334+
bbody = @nd_body[blk]24335+
bexpr = "0"24336+
if bbody >= 024337+
bs = get_stmts(bbody)24338+
if bs.length > 024339+
bexpr = compile_expr(bs.last)24340+
end24341+
end24342+
emit(" sp_SymIntHash_set(" + tmp + ", " + rc + "->order[_i], " + bexpr + ");")24343+
pop_scope24344+
emit(" }")24345+
return tmp24346+
end24347+
end2431524348
end2431624349
if recv_type == "sym_str_hash"2431724350
if mname == "[]"@@ -32050,6 +32083,10 @@
3205032083
if at == "tuple:float,float"3205132084
return "sp_sprintf(\"[%s, %s]\", sp_float_inspect(" + val + "->_0), sp_float_inspect(" + val + "->_1))"3205232085
end32086+
if at == "sym_int_hash"32087+
@needs_sym_int_hash = 132088+
return "sp_SymIntHash_inspect(" + val + ")"32089+
end3205332090
""3205432091
end3205532092
test/hash_transform_values.rbaddedView file
Loading diff…
test/hash_transform_values.rb.expectedaddedView file
Loading diff…
test/hash_transform_values_int_str.rbdeleted
Loading diff…
test/hash_transform_values_int_str.rb.expecteddeleted
Loading diff…