Commit 168b6c93
168b6c9336f7Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 8 files · +6 added · ~2 modified
Description
Merge pull request #278 from ryanseys/rs-node-index-writes
feat(parse,codegen): support Index{And,Or,Target}WriteNode (3 nodes)
Details
- Commit
- 168b6c9336f7
- Parent
- 538a8eeedd5c
- Merge parent
- cebd3e604997
spinel_codegen.rbmodifiedView file
@@ -25010,6 +25010,14 @@
2501025010
compile_index_op_assign(nid)2501125011
return2501225012
end25013+
if t == "IndexAndWriteNode"25014+
compile_index_and_assign(nid)25015+
return25016+
end25017+
if t == "IndexOrWriteNode"25018+
compile_index_or_assign(nid)25019+
return25020+
end2501325021
if t == "IfNode"2501425022
compile_if_stmt(nid)2501525023
return@@ -25166,6 +25174,49 @@
2516625174
end2516725175
end2516825176
end25177+
if @nd_type[tid] == "IndexTargetNode"25178+
# Multi-assign LHS: `a[0], b[1] = 1, 2`. Each LHS is an25179+
# IndexTargetNode with a receiver and an arguments slot.25180+
# Routes through the same per-receiver-type sp_<TYPE>_set25181+
# dispatch as compile_index_op_assign, but without the25182+
# read-then-modify dance -- the value is supplied by the25183+
# caller. The temp-receiver pattern keeps `a` and the index25184+
# evaluated exactly once.25185+
tgt_recv = @nd_receiver[tid]25186+
tgt_args_id = @nd_arguments[tid]25187+
tgt_arg_ids = tgt_args_id >= 0 ? get_args(tgt_args_id) : []25188+
if tgt_arg_ids.length >= 125189+
rt = infer_type(tgt_recv)25190+
rc = compile_expr_gc_rooted(tgt_recv)25191+
idx = compile_expr(tgt_arg_ids[0])25192+
if rt == "float_array" || rt == "int_array"25193+
pfx = array_c_prefix(rt)25194+
tt = new_temp25195+
ti = new_temp25196+
emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx +25197+
"; sp_" + pfx + "_set(" + tt + ", " + ti + ", (" + value_expr + ")); }")25198+
end25199+
if rt == "str_int_hash"25200+
tt = new_temp25201+
ti = new_temp25202+
idx_s = compile_expr_as_string(tgt_arg_ids[0])25203+
emit(" { sp_StrIntHash *" + tt + " = " + rc + "; const char *" + ti + " = " + idx_s +25204+
"; sp_StrIntHash_set(" + tt + ", " + ti + ", (" + value_expr + ")); }")25205+
end25206+
if rt == "sym_int_hash"25207+
tt = new_temp25208+
ti = new_temp25209+
emit(" { sp_SymIntHash *" + tt + " = " + rc + "; sp_sym " + ti + " = " + idx +25210+
"; sp_SymIntHash_set(" + tt + ", " + ti + ", (" + value_expr + ")); }")25211+
end25212+
if rt == "int_str_hash"25213+
tt = new_temp25214+
ti = new_temp25215+
emit(" { sp_IntStrHash *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx +25216+
"; sp_IntStrHash_set(" + tt + ", " + ti + ", (" + value_expr + ")); }")25217+
end25218+
end25219+
end2516925220
end2517025221
2517125222
# Handle `a, *b = rhs` / `*a, b = rhs` / `a, *b, c = rhs`.@@ -28550,6 +28601,137 @@
2855028601
end2855128602
end2855228603
28604+
# `a[i] &&= val` -- read once, conditionally write once. Mirrors28605+
# compile_index_op_assign's per-receiver-type dispatch but routes28606+
# the new value through a C `if (cur)` guard instead of an28607+
# arithmetic op. The temp pattern keeps `a` and `i` evaluated28608+
# exactly once, matching CRuby's `a[i] = a[i] && val` evaluation28609+
# order.28610+
def compile_index_and_assign(nid)28611+
recv = @nd_receiver[nid]28612+
args_id = @nd_arguments[nid]28613+
arg_ids = args_id >= 0 ? get_args(args_id) : []28614+
return if arg_ids.length < 128615+
rt = infer_type(recv)28616+
rc = compile_expr_gc_rooted(recv)28617+
idx = compile_expr(arg_ids[0])28618+
# Note: defer compile_expr(@nd_expression[nid]) to inside each28619+
# if-branch so RHS side effects only fire when the get is truthy28620+
# (Ruby `&&=` short-circuit semantics).28621+
28622+
if rt == "float_array" || rt == "int_array"28623+
pfx = array_c_prefix(rt)28624+
tt = new_temp28625+
ti = new_temp28626+
emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx + ";")28627+
emit(" if (sp_" + pfx + "_get(" + tt + ", " + ti + ")) {")28628+
val = compile_expr(@nd_expression[nid])28629+
emit(" sp_" + pfx + "_set(" + tt + ", " + ti + ", (" + val + "));")28630+
emit(" }")28631+
emit(" }")28632+
return28633+
end28634+
if rt == "str_int_hash"28635+
tt = new_temp28636+
ti = new_temp28637+
idx_s = compile_expr_as_string(arg_ids[0])28638+
emit(" { sp_StrIntHash *" + tt + " = " + rc + "; const char *" + ti + " = " + idx_s + ";")28639+
emit(" if (sp_StrIntHash_get(" + tt + ", " + ti + ")) {")28640+
val = compile_expr(@nd_expression[nid])28641+
emit(" sp_StrIntHash_set(" + tt + ", " + ti + ", (" + val + "));")28642+
emit(" }")28643+
emit(" }")28644+
return28645+
end28646+
if rt == "int_str_hash"28647+
tt = new_temp28648+
ti = new_temp28649+
emit(" { sp_IntStrHash *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx + ";")28650+
emit(" if (sp_IntStrHash_get(" + tt + ", " + ti + ")) {")28651+
val = compile_expr(@nd_expression[nid])28652+
emit(" sp_IntStrHash_set(" + tt + ", " + ti + ", (" + val + "));")28653+
emit(" }")28654+
emit(" }")28655+
return28656+
end28657+
if rt == "sym_int_hash"28658+
tt = new_temp28659+
ti = new_temp28660+
emit(" { sp_SymIntHash *" + tt + " = " + rc + "; sp_sym " + ti + " = " + idx + ";")28661+
emit(" if (sp_SymIntHash_get(" + tt + ", " + ti + ")) {")28662+
val = compile_expr(@nd_expression[nid])28663+
emit(" sp_SymIntHash_set(" + tt + ", " + ti + ", (" + val + "));")28664+
emit(" }")28665+
emit(" }")28666+
return28667+
end28668+
end28669+
28670+
# `a[i] ||= val` -- read once, write only if current is falsy.28671+
# Same C-truthy vs Ruby-truthy gap as the LocalVariable/Global/28672+
# Class compound forms (numeric 0 is C-falsy; documented in28673+
# test/global_var_or_write.rb). For string-valued slots (NULL28674+
# falsy, anything else truthy) the C and Ruby semantics agree.28675+
def compile_index_or_assign(nid)28676+
recv = @nd_receiver[nid]28677+
args_id = @nd_arguments[nid]28678+
arg_ids = args_id >= 0 ? get_args(args_id) : []28679+
return if arg_ids.length < 128680+
rt = infer_type(recv)28681+
rc = compile_expr_gc_rooted(recv)28682+
idx = compile_expr(arg_ids[0])28683+
# Note: defer compile_expr(@nd_expression[nid]) to inside each28684+
# if-branch so RHS side effects only fire when the get is falsy28685+
# (Ruby `||=` short-circuit semantics).28686+
28687+
if rt == "float_array" || rt == "int_array"28688+
pfx = array_c_prefix(rt)28689+
tt = new_temp28690+
ti = new_temp28691+
emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx + ";")28692+
emit(" if (!sp_" + pfx + "_get(" + tt + ", " + ti + ")) {")28693+
val = compile_expr(@nd_expression[nid])28694+
emit(" sp_" + pfx + "_set(" + tt + ", " + ti + ", (" + val + "));")28695+
emit(" }")28696+
emit(" }")28697+
return28698+
end28699+
if rt == "str_int_hash"28700+
tt = new_temp28701+
ti = new_temp28702+
idx_s = compile_expr_as_string(arg_ids[0])28703+
emit(" { sp_StrIntHash *" + tt + " = " + rc + "; const char *" + ti + " = " + idx_s + ";")28704+
emit(" if (!sp_StrIntHash_get(" + tt + ", " + ti + ")) {")28705+
val = compile_expr(@nd_expression[nid])28706+
emit(" sp_StrIntHash_set(" + tt + ", " + ti + ", (" + val + "));")28707+
emit(" }")28708+
emit(" }")28709+
return28710+
end28711+
if rt == "int_str_hash"28712+
tt = new_temp28713+
ti = new_temp28714+
emit(" { sp_IntStrHash *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx + ";")28715+
emit(" if (!sp_IntStrHash_get(" + tt + ", " + ti + ")) {")28716+
val = compile_expr(@nd_expression[nid])28717+
emit(" sp_IntStrHash_set(" + tt + ", " + ti + ", (" + val + "));")28718+
emit(" }")28719+
emit(" }")28720+
return28721+
end28722+
if rt == "sym_int_hash"28723+
tt = new_temp28724+
ti = new_temp28725+
emit(" { sp_SymIntHash *" + tt + " = " + rc + "; sp_sym " + ti + " = " + idx + ";")28726+
emit(" if (!sp_SymIntHash_get(" + tt + ", " + ti + ")) {")28727+
val = compile_expr(@nd_expression[nid])28728+
emit(" sp_SymIntHash_set(" + tt + ", " + ti + ", (" + val + "));")28729+
emit(" }")28730+
emit(" }")28731+
return28732+
end28733+
end28734+
2855328735
# Return a C expression that evaluates to the inspected form of `val`2855428736
# (a value of inferred Ruby type `at`), following Ruby's Object#inspect2855528737
# contract. Returns "" when `at` has no inspect implementation yet, sospinel_parse.cmodifiedView file
Loading diff…
test/index_and_write.rbaddedView file
Loading diff…
test/index_and_write.rb.expectedaddedView file
Loading diff…
test/index_or_write.rbaddedView file
Loading diff…
test/index_or_write.rb.expectedaddedView file
Loading diff…
test/index_target.rbaddedView file
Loading diff…
test/index_target.rb.expectedaddedView file
Loading diff…