Commit 83536b81
83536b81e8feMerged
Ryan Seys <[email protected]> · 6 months ago · 8 files · +6 added · ~2 modified
Description
feat(parse,codegen): support Call{Operator,And,Or}WriteNode (3 nodes)
Bundle of three Call* compound-write forms. Together with the Index{Operator,And,Or,Target}WriteNode set and the existing IndexOperatorWriteNode, this closes out the compound-write family across all variable kinds (locals, ivars, globals, cvars, indexed slots, and now method-style accessors).
Critical for correctness: obj.bar += val evaluates the receiver
exactly ONCE -- the source is shorthand for
tmp = obj; tmp.bar = tmp.bar + val, NOT
obj.bar = obj.bar + val. The single-evaluation matters when
the receiver expression has side effects (the test exercises
get_cfg(c).name &&= "second" and verifies the helper fires
exactly once via a global counter). compile_call_assign_typed
emits a temp-receiver block:
{ sp_<Cls> *_t = recv; _t->iv_<bname> = _t->iv_<bname> <op> (val); }
so the read and write share one temp.
Implementation restriction: only typed instance receivers backed
by attr_accessor (or struct field) are supported. The codegen
checks rt[0,4] == "obj_" to detect a typed instance, then
cls_has_attr_writer(ci, bname) to confirm the attr exists. For
non-attr receivers (untyped, polymorphic, or method-without-
backing-ivar), we exit(1) with a precise message rather than
emit silently-wrong C -- matching Phase 0's loud-failure spirit
even though the parser succeeded.
CallOperatorWriteNode (obj.attr += val):
Full operator dispatch: +, -, *, /, %, <<, >>, &, |, ^.
String-valued attrs with += route to sp_str_concat instead
of raw + (mirroring the LocalVariableOperatorWriteNode arm).
CallAndWriteNode (obj.attr &&= val):
Emits if (_t->iv_<bname>) _t->iv_<bname> = (val);.
CallOrWriteNode (obj.attr ||= val):
Emits if (!_t->iv_<bname>) _t->iv_<bname> = (val);.
Test fixture documents the Spinel-specific gap: nil-typed
string slots get sentineled to "" by the constructor's default
arg path, and "" is C-truthy, so the "fire on nil" branch
doesn't exercise cleanly. The don't-fire-on-truthy branch
agrees in C and Ruby, which the test covers.
CallTargetNode (the 4th compound form, obj.x, p.y = 1, 2 for
multi-assign LHS) is intentionally NOT in this commit -- it landed
upstream via mame's #216 (CallTargetNode in multi-write targets),
so it is already present.
Test exercises (3 fixtures):
- call_op_write: Counter#n with all 10 numeric operators; Greeting#msg += " world" exercising the string-concat path.
- call_or_write: don't-fire-on-truthy + receiver-once via side-effecting helper.
- call_and_write: fire-on-truthy + receiver-once verification.
Out of scope (each documented inline): non-attr_accessor receivers (method-style getters with custom logic); polymorphic receivers (untyped or union-type); the nil-vs-empty-string conversion gap that limits the ||= "fire on nil" branch for string slots.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 83536b81e8fe
- Parent
- 40403f434501
spinel_codegen.rbmodifiedView file
compile_index_or_assign(nid) return end if t == "CallOperatorWriteNode" compile_call_assign_typed(nid, "Operator") return end if t == "CallAndWriteNode" compile_call_assign_typed(nid, "And") return end if t == "CallOrWriteNode" compile_call_assign_typed(nid, "Or") return end if t == "IfNode" compile_if_stmt(nid) return end end # `obj.attr <op>= val` family helper -- builds the # temp-receiver-once C block for typed-instance receivers backed by # an attr_accessor (or struct field). For non-attr receivers, we # exit with a precise error rather than fall through to an # incorrect emission. The caller passes a fragment that takes the # temp variable name and returns the C body to execute. # # Spec note: even for the typed-attr-accessor case the receiver is # evaluated exactly ONCE -- the source `obj.bar += val` is # `tmp = obj; tmp.bar = tmp.bar + val`, NOT `obj.bar = obj.bar + val`. # The temp pattern matters when the receiver expression has side # effects (e.g. `next_holder().attr += 1`). def compile_call_assign_typed(nid, kind) recv = @nd_receiver[nid] rt = infer_type(recv) bname = @nd_name[nid] # parser emits read_name as "name" if rt.length <= 4 || rt[0, 4] != "obj_" $stderr.puts "Spinel: Call" + kind + "WriteNode requires a typed instance receiver with attr_accessor; got receiver type \"" + rt + "\"" exit(1) end cname = rt[4, rt.length - 4] ci = find_class_idx(cname) if ci < 0 || cls_has_attr_writer(ci, bname) == 0 $stderr.puts "Spinel: Call" + kind + "WriteNode for `." + bname + "` requires attr_accessor (or struct field) on class " + cname exit(1) end rc = compile_expr_gc_rooted(recv) val = compile_expr(@nd_expression[nid]) tt = new_temp field = "iv_" + bname if kind == "Operator" op = @nd_binop[nid] ivar_t = cls_ivar_type(ci, "@" + bname) if op == "+" && ivar_t == "string" && infer_type(@nd_expression[nid]) == "string" # String-valued attr with `+=` becomes sp_str_concat, mirroring # the LocalVariableOperatorWriteNode arm at line 21786. emit(" { sp_" + cname + " *" + tt + " = " + rc + "; " + tt + "->" + field + " = sp_str_concat(" + tt + "->" + field + ", (" + val + ")); }") return end if op == "%" emit(" { sp_" + cname + " *" + tt + " = " + rc + "; " + tt + "->" + field + " = sp_imod(" + tt + "->" + field + ", (" + val + ")); }") else emit(" { sp_" + cname + " *" + tt + " = " + rc + "; " + tt + "->" + field + " = " + tt + "->" + field + " " + op + " (" + val + "); }") end return end if kind == "And" emit(" { sp_" + cname + " *" + tt + " = " + rc + "; " + "if (" + tt + "->" + field + ") " + tt + "->" + field + " = (" + val + "); }") return end if kind == "Or" emit(" { sp_" + cname + " *" + tt + " = " + rc + "; " + "if (!" + tt + "->" + field + ") " + tt + "->" + field + " = (" + val + "); }") return 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 anspinel_parse.cmodifiedView file
Loading diff…
test/call_and_write.rbaddedView file
Loading diff…
test/call_and_write.rb.expectedaddedView file
Loading diff…
test/call_op_write.rbaddedView file
Loading diff…
test/call_op_write.rb.expectedaddedView file
Loading diff…
test/call_or_write.rbaddedView file
Loading diff…
test/call_or_write.rb.expectedaddedView file
Loading diff…