Commit cebd3e60
cebd3e604997Merged
Ryan Seys <[email protected]> · 6 months ago · 8 files · +6 added · ~2 modified
Description
feat(parse,codegen): support Index{And,Or,Target}WriteNode (3 nodes)
Bundle of the three remaining Index* compound forms; the Operator form (`a[i] += v`) is already supported via compile_index_op_assign. The new helpers compile_index_and_assign and compile_index_or_assign mirror the existing op-assign helper's per-receiver-type dispatch (int_array, float_array, str_int_hash, sym_int_hash, int_str_hash for OrWrite) but route the new value through a C `if (cur)` / `if (!cur)` guard instead of an arithmetic op.
Critical for correctness: the temp pattern keeps the receiver and index evaluated exactly ONCE, even though source like `a[i] ||= v` conceptually expands to `a[i] = a[i] || v`. Each helper emits:
{ sp_<TYPE> *_t = recv; mrb_int _i = idx;
if (sp_<TYPE>_get(_t, _i)) sp_<TYPE>_set(_t, _i, val); }
so a side-effecting receiver (e.g. `grow_array(...)[i] &&= ...`) fires only once.
IndexAndWriteNode (`a[i] &&= val`): Sets a[i] only if the current value is truthy. Inherits the C-truthy vs Ruby-truthy gap (numeric 0 is C-falsy but Ruby-truthy); test fixture stays in the agreement region.
IndexOrWriteNode (`a[i] ||= val`): Sets a[i] only if the current value is falsy. Includes the int_str_hash arm for the common `hash[key] ||= "default"` idiom -- NULL is falsy in both C and Ruby for string-valued slots, so no divergence there.
IndexTargetNode (`a[i], b[j] = 1, 2`): Multi-assign LHS. New arm in emit_multi_write_target alongside the existing LocalVariable/InstanceVariable/GlobalVariable/ ClassVariable cases. Same per-receiver-type dispatch but no read-then-modify dance -- the value comes from the caller.
Test exercises:
- index_or_write: str_int_hash with mix of pre-existing, pre-existing-zero, and never-set keys; ||= initializes the never-set key without overwriting the existing values.
- index_and_write: int_array with non-zero slots, demonstrating truthy-fires; str_int_hash with the never-set-key case exercising the don't-fire path. Also confirms receiver/index evaluated once via `xs[4] &&= xs[4] * 100`.
- index_target: cross-receiver swap (a[0], b[2] = ...); same-array swap (zs[0], zs[4] = zs[4], zs[0]) confirms RHS evaluation precedes any write; mixed array+hash multi-assign.
Out of scope: str_str_hash and other hash variants the existing compile_index_op_assign also doesn't cover; safe-navigation on the receiver (e.g. `obj&.attr ||= v`) -- the PM_CALL_NODE_FLAGS_SAFE_NAVIGATION flag exists in Prism's struct but Spinel's index-write helpers don't yet branch on it. Bigint arithmetic for IndexOperatorWriteNode also remains an existing gap (not introduced here).
Tests: 266 pass. Bootstrap: gen2.c == gen3.c.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- cebd3e604997
- Parent
- fea5369dddb0
spinel_codegen.rbmodifiedView file
compile_index_op_assign(nid) return end if t == "IndexAndWriteNode" compile_index_and_assign(nid) return end if t == "IndexOrWriteNode" compile_index_or_assign(nid) return end if t == "IfNode" compile_if_stmt(nid) return end end end if @nd_type[tid] == "IndexTargetNode" # Multi-assign LHS: `a[0], b[1] = 1, 2`. Each LHS is an # IndexTargetNode with a receiver and an arguments slot. # Routes through the same per-receiver-type sp_<TYPE>_set # dispatch as compile_index_op_assign, but without the # read-then-modify dance -- the value is supplied by the # caller. The temp-receiver pattern keeps `a` and the index # evaluated exactly once. tgt_recv = @nd_receiver[tid] tgt_args_id = @nd_arguments[tid] tgt_arg_ids = tgt_args_id >= 0 ? get_args(tgt_args_id) : [] if tgt_arg_ids.length >= 1 rt = infer_type(tgt_recv) rc = compile_expr_gc_rooted(tgt_recv) idx = compile_expr(tgt_arg_ids[0]) if rt == "float_array" || rt == "int_array" pfx = array_c_prefix(rt) tt = new_temp ti = new_temp emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx + "; sp_" + pfx + "_set(" + tt + ", " + ti + ", (" + value_expr + ")); }") end if rt == "str_int_hash" tt = new_temp ti = new_temp idx_s = compile_expr_as_string(tgt_arg_ids[0]) emit(" { sp_StrIntHash *" + tt + " = " + rc + "; const char *" + ti + " = " + idx_s + "; sp_StrIntHash_set(" + tt + ", " + ti + ", (" + value_expr + ")); }") end if rt == "sym_int_hash" tt = new_temp ti = new_temp emit(" { sp_SymIntHash *" + tt + " = " + rc + "; sp_sym " + ti + " = " + idx + "; sp_SymIntHash_set(" + tt + ", " + ti + ", (" + value_expr + ")); }") end if rt == "int_str_hash" tt = new_temp ti = new_temp emit(" { sp_IntStrHash *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx + "; sp_IntStrHash_set(" + tt + ", " + ti + ", (" + value_expr + ")); }") end end end end # Handle `a, *b = rhs` / `*a, b = rhs` / `a, *b, c = rhs`. end end # `a[i] &&= val` -- read once, conditionally write once. Mirrors # compile_index_op_assign's per-receiver-type dispatch but routes # the new value through a C `if (cur)` guard instead of an # arithmetic op. The temp pattern keeps `a` and `i` evaluated # exactly once, matching CRuby's `a[i] = a[i] && val` evaluation # order. def compile_index_and_assign(nid) recv = @nd_receiver[nid] args_id = @nd_arguments[nid] arg_ids = args_id >= 0 ? get_args(args_id) : [] return if arg_ids.length < 1 rt = infer_type(recv) rc = compile_expr_gc_rooted(recv) idx = compile_expr(arg_ids[0]) # Note: defer compile_expr(@nd_expression[nid]) to inside each # if-branch so RHS side effects only fire when the get is truthy # (Ruby `&&=` short-circuit semantics). if rt == "float_array" || rt == "int_array" pfx = array_c_prefix(rt) tt = new_temp ti = new_temp emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx + ";") emit(" if (sp_" + pfx + "_get(" + tt + ", " + ti + ")) {") val = compile_expr(@nd_expression[nid]) emit(" sp_" + pfx + "_set(" + tt + ", " + ti + ", (" + val + "));") emit(" }") emit(" }") return end if rt == "str_int_hash" tt = new_temp ti = new_temp idx_s = compile_expr_as_string(arg_ids[0]) emit(" { sp_StrIntHash *" + tt + " = " + rc + "; const char *" + ti + " = " + idx_s + ";") emit(" if (sp_StrIntHash_get(" + tt + ", " + ti + ")) {") val = compile_expr(@nd_expression[nid]) emit(" sp_StrIntHash_set(" + tt + ", " + ti + ", (" + val + "));") emit(" }") emit(" }") return end if rt == "int_str_hash" tt = new_temp ti = new_temp emit(" { sp_IntStrHash *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx + ";") emit(" if (sp_IntStrHash_get(" + tt + ", " + ti + ")) {") val = compile_expr(@nd_expression[nid]) emit(" sp_IntStrHash_set(" + tt + ", " + ti + ", (" + val + "));") emit(" }") emit(" }") return end if rt == "sym_int_hash" tt = new_temp ti = new_temp emit(" { sp_SymIntHash *" + tt + " = " + rc + "; sp_sym " + ti + " = " + idx + ";") emit(" if (sp_SymIntHash_get(" + tt + ", " + ti + ")) {") val = compile_expr(@nd_expression[nid]) emit(" sp_SymIntHash_set(" + tt + ", " + ti + ", (" + val + "));") emit(" }") emit(" }") return end end # `a[i] ||= val` -- read once, write only if current is falsy. # Same C-truthy vs Ruby-truthy gap as the LocalVariable/Global/ # Class compound forms (numeric 0 is C-falsy; documented in # test/global_var_or_write.rb). For string-valued slots (NULL # falsy, anything else truthy) the C and Ruby semantics agree. def compile_index_or_assign(nid) recv = @nd_receiver[nid] args_id = @nd_arguments[nid] arg_ids = args_id >= 0 ? get_args(args_id) : [] return if arg_ids.length < 1 rt = infer_type(recv) rc = compile_expr_gc_rooted(recv) idx = compile_expr(arg_ids[0]) # Note: defer compile_expr(@nd_expression[nid]) to inside each # if-branch so RHS side effects only fire when the get is falsy # (Ruby `||=` short-circuit semantics). if rt == "float_array" || rt == "int_array" pfx = array_c_prefix(rt) tt = new_temp ti = new_temp emit(" { sp_" + pfx + " *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx + ";") emit(" if (!sp_" + pfx + "_get(" + tt + ", " + ti + ")) {") val = compile_expr(@nd_expression[nid]) emit(" sp_" + pfx + "_set(" + tt + ", " + ti + ", (" + val + "));") emit(" }") emit(" }") return end if rt == "str_int_hash" tt = new_temp ti = new_temp idx_s = compile_expr_as_string(arg_ids[0]) emit(" { sp_StrIntHash *" + tt + " = " + rc + "; const char *" + ti + " = " + idx_s + ";") emit(" if (!sp_StrIntHash_get(" + tt + ", " + ti + ")) {") val = compile_expr(@nd_expression[nid]) emit(" sp_StrIntHash_set(" + tt + ", " + ti + ", (" + val + "));") emit(" }") emit(" }") return end if rt == "int_str_hash" tt = new_temp ti = new_temp emit(" { sp_IntStrHash *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx + ";") emit(" if (!sp_IntStrHash_get(" + tt + ", " + ti + ")) {") val = compile_expr(@nd_expression[nid]) emit(" sp_IntStrHash_set(" + tt + ", " + ti + ", (" + val + "));") emit(" }") emit(" }") return end if rt == "sym_int_hash" tt = new_temp ti = new_temp emit(" { sp_SymIntHash *" + tt + " = " + rc + "; sp_sym " + ti + " = " + idx + ";") emit(" if (!sp_SymIntHash_get(" + tt + ", " + ti + ")) {") val = compile_expr(@nd_expression[nid]) emit(" sp_SymIntHash_set(" + tt + ", " + ti + ", (" + val + "));") emit(" }") emit(" }") return end end # Return a C expression that evaluates to the inspected form of `val` # (a value of inferred Ruby type `at`), following Ruby's Object#inspect # 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…