Commit 322c7870

322c7870a8f7Merged

Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +1 added · ~2 modified

Description

feat(parser+codegen): support a ||= b and a &&= b for locals

LocalVariableOrWriteNode and LocalVariableAndWriteNode parse as distinct prism AST nodes — separate from LocalVariableOperatorWriteNode (which carries +, -, etc. via a binary_operator field). Without dedicated cases the prism nodes were dropped on the floor, so a ||= b produced no C output.

Add parser cases (spinel_parse.c) and codegen handlers in both compile_expr and compile_stmt. The expression form lowers as (local = local ? local : (val)) (or its && counterpart); the statement form lowers as if (cond) local = val;.

Note: this depends on the prior PR (#XXX) adding LocalVariableWriteNode to compile_expr — chained @a = local ||= b writes use the LocalVariableWriteNode expr path for the outer write.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
322c7870a8f7
Parent
78de785dc7bf

spinel_codegen.rbmodifiedView file

@@ -13978,6 +13978,20 @@
1397813978 end
1397913979 return "(" + self_arrow + sanitize_ivar(iname_w) + " = " + val + ")"
1398013980 end
13981+ if t == "LocalVariableOrWriteNode"
13982+ # `local ||= expr` in expression context. Lower as
13983+ # `(local = local ? local : expr)` so the side effect runs
13984+ # only on a falsy current value.
13985+ vref = fiber_var_ref(@nd_name[nid])
13986+ val = compile_expr(@nd_expression[nid])
13987+ return "(" + vref + " = " + vref + " ? " + vref + " : (" + val + "))"
13988+ end
13989+ if t == "LocalVariableAndWriteNode"
13990+ # `local &&= expr` in expression context.
13991+ vref = fiber_var_ref(@nd_name[nid])
13992+ val = compile_expr(@nd_expression[nid])
13993+ return "(" + vref + " = " + vref + " ? (" + val + ") : " + vref + ")"
13994+ end
1398113995 if t == "ConstantReadNode"
1398213996 if @nd_name[nid] == "ARGV"
1398313997 return "sp_argv"
@@ -20749,6 +20763,18 @@
2074920763 end
2075020764 return
2075120765 end
20766+ if t == "LocalVariableOrWriteNode"
20767+ vref = fiber_var_ref(@nd_name[nid])
20768+ val = compile_expr(@nd_expression[nid])
20769+ emit(" if (!(" + vref + ")) " + vref + " = " + val + ";")
20770+ return
20771+ end
20772+ if t == "LocalVariableAndWriteNode"
20773+ vref = fiber_var_ref(@nd_name[nid])
20774+ val = compile_expr(@nd_expression[nid])
20775+ emit(" if (" + vref + ") " + vref + " = " + val + ";")
20776+ return
20777+ end
2075220778 if t == "InstanceVariableWriteNode"
2075320779 iname = @nd_name[nid]
2075420780 expr_id = @nd_expression[nid]