Commit 7131a900
7131a9003e7bMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +1 added · ~2 modified
Description
Merge pull request #184 from mame/pr/local-or-and-write
feat(parser+codegen): support a ||= b and a &&= b for locals
Details
- Commit
- 7131a9003e7b
- Parent
- b6007f86940c
- Merge parent
- 322c7870a8f7
spinel_codegen.rbmodifiedView file
@@ -14488,6 +14488,20 @@
1448814488
end1448914489
return "(" + self_arrow + sanitize_ivar(iname_w) + " = " + val + ")"1449014490
end14491+
if t == "LocalVariableOrWriteNode"14492+
# `local ||= expr` in expression context. Lower as14493+
# `(local = local ? local : expr)` so the side effect runs14494+
# only on a falsy current value.14495+
vref = fiber_var_ref(@nd_name[nid])14496+
val = compile_expr(@nd_expression[nid])14497+
return "(" + vref + " = " + vref + " ? " + vref + " : (" + val + "))"14498+
end14499+
if t == "LocalVariableAndWriteNode"14500+
# `local &&= expr` in expression context.14501+
vref = fiber_var_ref(@nd_name[nid])14502+
val = compile_expr(@nd_expression[nid])14503+
return "(" + vref + " = " + vref + " ? (" + val + ") : " + vref + ")"14504+
end1449114505
if t == "ConstantReadNode"1449214506
if @nd_name[nid] == "ARGV"1449314507
return "sp_argv"@@ -21436,6 +21450,18 @@
2143621450
end2143721451
return2143821452
end21453+
if t == "LocalVariableOrWriteNode"21454+
vref = fiber_var_ref(@nd_name[nid])21455+
val = compile_expr(@nd_expression[nid])21456+
emit(" if (!(" + vref + ")) " + vref + " = " + val + ";")21457+
return21458+
end21459+
if t == "LocalVariableAndWriteNode"21460+
vref = fiber_var_ref(@nd_name[nid])21461+
val = compile_expr(@nd_expression[nid])21462+
emit(" if (" + vref + ") " + vref + " = " + val + ";")21463+
return21464+
end2143921465
if t == "InstanceVariableWriteNode"2144021466
iname = @nd_name[nid]2144121467
expr_id = @nd_expression[nid]spinel_parse.cmodifiedView file
Loading diff…
test/local_or_and_write.rbaddedView file
Loading diff…