Commit 9ffad2a9

9ffad2a90bcdMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

Merge pull request #272 from ryanseys/rs-node-global-var-or-write

feat(parse,codegen): support GlobalVariableOrWriteNode ($x ||= val)

Details

Commit
9ffad2a90bcd
Parent
887d2bcb138e
Merge parent
135e1a3a3938

spinel_codegen.rbmodifiedView file

@@ -24744,6 +24744,20 @@
2474424744 end
2474524745 return
2474624746 end
24747+ if t == "GlobalVariableOrWriteNode"
24748+ # `$x ||= val`. Short-circuit: only evaluate RHS when LHS is
24749+ # falsy. compile_expr can emit prerequisite statements; running
24750+ # it inside the C `if` block ensures those side effects only
24751+ # fire on the assign branch. Note: in Ruby, an undefined $-var
24752+ # reads as nil, which is falsy in C terms, so the `if (!cname)`
24753+ # guard fires both for never-assigned and assigned-to-falsy.
24754+ cname = sanitize_gvar(@nd_name[nid])
24755+ emit(" if (!(" + cname + ")) {")
24756+ val = compile_expr(@nd_expression[nid])
24757+ emit(" " + cname + " = " + val + ";")
24758+ emit(" }")
24759+ return
24760+ end
2474724761 if t == "LocalVariableWriteNode"
2474824762 lname = @nd_name[nid]
2474924763 # Check for method(:name) assignment