authorgravatar for adambgoertz@gmail.comAdam Goertz <adambgoertz@gmail.com> 2023-11-07 01:24:19+00:00
committergravatar for adambgoertz@gmail.comAdam Goertz <adambgoertz@gmail.com> 2023-11-07 01:24:19+00:00
logdb785e25b9808168d6259de53ffb4151a422e307
treedef25fe78d9c7e726a22614fd7d7264842233473
parent91570cc42d4fe973d15ac05cdecff42051f154ad

Apply same reductions to while


1 files changed, 32 insertions(+), 2 deletions(-)

src/reduce/Walk.zig+32-2
...@@ -559,7 +559,7 @@ fn walkExpression(w: *Walk, node: Ast.Node.Index) Error!void {...@@ -559,7 +559,7 @@ fn walkExpression(w: *Walk, node: Ast.Node.Index) Error!void {
559 .while_simple,559 .while_simple,
560 .while_cont,560 .while_cont,
561 .@"while",561 .@"while",
562 => return walkWhile(w, ast.fullWhile(node).?),562 => return walkWhile(w, node, ast.fullWhile(node).?),
563563
564 .for_simple,564 .for_simple,
565 .@"for",565 .@"for",
...@@ -863,7 +863,37 @@ fn walkSwitchCase(w: *Walk, switch_case: Ast.full.SwitchCase) Error!void {...@@ -863,7 +863,37 @@ fn walkSwitchCase(w: *Walk, switch_case: Ast.full.SwitchCase) Error!void {
863 try walkExpression(w, switch_case.ast.target_expr);863 try walkExpression(w, switch_case.ast.target_expr);
864}864}
865865
866fn walkWhile(w: *Walk, while_node: Ast.full.While) Error!void {866fn walkWhile(w: *Walk, node_index: Ast.Node.Index, while_node: Ast.full.While) Error!void {
867 assert(while_node.ast.cond_expr != 0);
868 assert(while_node.ast.then_expr != 0);
869
870 // Perform these transformations in this priority order:
871 // 1. If the `else` expression is missing or an empty block, replace the condition with `if (true)` if it is not already.
872 // 2. If the `then` block is empty, replace the condition with `if (false)` if it is not already.
873 // 3. If the condition is `if (true)`, replace the `if` expression with the contents of the `then` expression.
874 // 4. If the condition is `if (false)`, replace the `if` expression with the contents of the `else` expression.
875 if (!isTrueIdent(w.ast, while_node.ast.cond_expr) and
876 (while_node.ast.else_expr == 0 or isEmptyBlock(w.ast, while_node.ast.else_expr)))
877 {
878 try w.transformations.ensureUnusedCapacity(1);
879 w.transformations.appendAssumeCapacity(.{ .replace_with_true = while_node.ast.cond_expr });
880 } else if (!isFalseIdent(w.ast, while_node.ast.cond_expr) and isEmptyBlock(w.ast, while_node.ast.then_expr)) {
881 try w.transformations.ensureUnusedCapacity(1);
882 w.transformations.appendAssumeCapacity(.{ .replace_with_false = while_node.ast.cond_expr });
883 } else if (isTrueIdent(w.ast, while_node.ast.cond_expr)) {
884 try w.transformations.ensureUnusedCapacity(1);
885 w.transformations.appendAssumeCapacity(.{ .replace_node = .{
886 .to_replace = node_index,
887 .replacement = while_node.ast.then_expr,
888 } });
889 } else if (isFalseIdent(w.ast, while_node.ast.cond_expr)) {
890 try w.transformations.ensureUnusedCapacity(1);
891 w.transformations.appendAssumeCapacity(.{ .replace_node = .{
892 .to_replace = node_index,
893 .replacement = while_node.ast.else_expr,
894 } });
895 }
896
867 try walkExpression(w, while_node.ast.cond_expr); // condition897 try walkExpression(w, while_node.ast.cond_expr); // condition
868898
869 if (while_node.ast.cont_expr != 0) {899 if (while_node.ast.cont_expr != 0) {