Commit ad55a8c0
ad55a8c08b81Merged
Ryan Seys <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
feat(parse,codegen): support GlobalVariableAndWriteNode ($x &&= val)
Mirror of GlobalVariableOrWriteNode and LocalVariableAndWriteNode,
condition inverted. Emits if (cname) cname = val; where cname is
sanitize_gvar(name). Inherits the same C-truthy vs Ruby-truthy
divergence; documented and avoided in the test fixture.
Test: test/global_var_and_write.rb. Bootstrap: gen2.c == gen3.c.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- ad55a8c08b81
- Parent
- 59ff8d7a4f30
spinel_codegen.rbmodifiedView file
@@ -25360,6 +25360,18 @@
2536025360
emit(" }")2536125361
return2536225362
end25363+
if t == "GlobalVariableAndWriteNode"25364+
# `$x &&= val`. Short-circuit: only evaluate RHS when LHS is25365+
# truthy. compile_expr can emit prerequisite statements; running25366+
# it inside the C `if` block keeps those side effects on the25367+
# assign branch only. Mirror of OrWrite, condition inverted.25368+
cname = sanitize_gvar(@nd_name[nid])25369+
emit(" if (" + cname + ") {")25370+
val = compile_expr(@nd_expression[nid])25371+
emit(" " + cname + " = " + val + ";")25372+
emit(" }")25373+
return25374+
end2536325375
if t == "ClassVariableWriteNode"2536425376
# `@@x = val` inside class body or method. Storage is the2536525377
# per-(class,name) C global cvar_<ClassName>_<var> registeredspinel_parse.cmodifiedView file
Loading diff…
test/global_var_and_write.rbaddedView file
Loading diff…
test/global_var_and_write.rb.expectedaddedView file
Loading diff…