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
end return "(" + self_arrow + sanitize_ivar(iname_w) + " = " + val + ")" end if t == "LocalVariableOrWriteNode" # `local ||= expr` in expression context. Lower as # `(local = local ? local : expr)` so the side effect runs # only on a falsy current value. vref = fiber_var_ref(@nd_name[nid]) val = compile_expr(@nd_expression[nid]) return "(" + vref + " = " + vref + " ? " + vref + " : (" + val + "))" end if t == "LocalVariableAndWriteNode" # `local &&= expr` in expression context. vref = fiber_var_ref(@nd_name[nid]) val = compile_expr(@nd_expression[nid]) return "(" + vref + " = " + vref + " ? (" + val + ") : " + vref + ")" end if t == "ConstantReadNode" if @nd_name[nid] == "ARGV" return "sp_argv" end return end if t == "LocalVariableOrWriteNode" vref = fiber_var_ref(@nd_name[nid]) val = compile_expr(@nd_expression[nid]) emit(" if (!(" + vref + ")) " + vref + " = " + val + ";") return end if t == "LocalVariableAndWriteNode" vref = fiber_var_ref(@nd_name[nid]) val = compile_expr(@nd_expression[nid]) emit(" if (" + vref + ") " + vref + " = " + val + ";") return end if t == "InstanceVariableWriteNode" iname = @nd_name[nid] expr_id = @nd_expression[nid]spinel_parse.cmodifiedView file
Loading diff…
test/local_or_and_write.rbaddedView file
Loading diff…