Commit 135e1a3a
135e1a3a3938Merged
Ryan Seys <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
feat(parse,codegen): support GlobalVariableOrWriteNode ($x ||= val)
Mirror of LocalVariableOrWriteNode. Emits if (!(cname)) cname = val;
where cname is sanitize_gvar(name).
Inherits LocalVariableOrWriteNode's pre-existing C-truthy vs Ruby-truthy divergence: numeric 0 tests falsy at the C level even though it is truthy in Ruby. This is documented in the test fixture; the nil-to-string type transition that exposes the gap most sharply ($x = nil; $x ||= "default") also doesn't compile today because Spinel's type inference keeps the variable typed as int across the ||= boundary. Both gaps exist for local variables too; fixing them is out of scope for this commit.
Test: test/global_var_or_write.rb (exercises only the agreement region). Bootstrap: gen2.c == gen3.c.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 135e1a3a3938
- Parent
- 887d2bcb138e
spinel_codegen.rbmodifiedView file
end return end if t == "GlobalVariableOrWriteNode" # `$x ||= val`. Short-circuit: only evaluate RHS when LHS is # falsy. compile_expr can emit prerequisite statements; running # it inside the C `if` block ensures those side effects only # fire on the assign branch. Note: in Ruby, an undefined $-var # reads as nil, which is falsy in C terms, so the `if (!cname)` # guard fires both for never-assigned and assigned-to-falsy. cname = sanitize_gvar(@nd_name[nid]) emit(" if (!(" + cname + ")) {") val = compile_expr(@nd_expression[nid]) emit(" " + cname + " = " + val + ";") emit(" }") return end if t == "LocalVariableWriteNode" lname = @nd_name[nid] # Check for method(:name) assignmentspinel_parse.cmodifiedView file
Loading diff…
test/global_var_or_write.rbaddedView file
Loading diff…
test/global_var_or_write.rb.expectedaddedView file
Loading diff…