@@ -24,48 +24,28 @@ public class ExpressionSimplifier {
2424 * expanding aliases Returns a derivation node representing the tree of simplifications applied
2525 */
2626 public static ValDerivationNode simplify (Expression exp , Map <String , AliasDTO > aliases ) {
27- int [] pass = { 0 };
28- // String, not Expression: the simplification passes mutate the AST in place, so storing an Expression
29- // reference would always compare equal to itself after the next pass runs. Snapshot the printed form instead.
30- String [] prev = { null };
31- DebugLog .simplificationPass (0 , "initial expression" , exp );
32- prev [0 ] = exp .toString ();
33- ValDerivationNode fixedPoint = simplifyToFixedPoint (null , exp , pass , prev );
34- logStep (pass , prev , "fixed-point reached" , fixedPoint .getValue ());
27+ DebugLog .simplificationStart (exp );
28+ ValDerivationNode fixedPoint = simplifyToFixedPoint (null , exp );
29+ DebugLog .simplificationPass ("fixed-point reached" , fixedPoint .getValue ());
3530 ValDerivationNode simplified = simplifyValDerivationNode (fixedPoint );
36- logStep ( pass , prev , "remove redundant &&" , simplified .getValue ());
31+ DebugLog . simplificationPass ( "remove redundant &&" , simplified .getValue ());
3732 ValDerivationNode unwrapped = unwrapBooleanLiterals (simplified );
38- logStep ( pass , prev , "unwrap boolean literals" , unwrapped .getValue ());
33+ DebugLog . simplificationPass ( "unwrap boolean literals" , unwrapped .getValue ());
3934 ValDerivationNode expanded = AliasExpansion .expand (unwrapped , aliases );
40- logStep ( pass , prev , "expand aliases" , expanded .getValue ());
35+ DebugLog . simplificationPass ( "expand aliases" , expanded .getValue ());
4136 return expanded ;
4237 }
4338
4439 public static ValDerivationNode simplify (Expression exp ) {
4540 return simplify (exp , Map .of ());
4641 }
4742
48- private static void logStep (int [] pass , String [] prev , String name , Expression result ) {
49- String resultStr = result .toString ();
50- DebugLog .simplificationPass (++pass [0 ], name , prev [0 ], resultStr );
51- prev [0 ] = resultStr ;
52- }
53-
5443 /**
5544 * Recursively applies propagation and folding until the expression stops changing (fixed point) Stops early if the
56- * expression simplifies to a boolean literal, which means we've simplified too much. {@code pass} and {@code prev}
57- * are running counters shared with {@link #simplify} so debug output keeps a single monotonic numbering and can
58- * detect no-op steps.
45+ * expression simplifies to a boolean literal, which means we've simplified too much.
5946 */
60- private static ValDerivationNode simplifyToFixedPoint (ValDerivationNode current , Expression prevExp , int [] pass ,
61- String [] prev ) {
62- // apply propagation and folding
63- ValDerivationNode prop = VariablePropagation .propagate (prevExp , current );
64- logStep (pass , prev , "variable propagation" , prop .getValue ());
65- ValDerivationNode fold = ExpressionFolding .fold (prop );
66- logStep (pass , prev , "expression folding" , fold .getValue ());
67- ValDerivationNode simplified = simplifyValDerivationNode (fold );
68- logStep (pass , prev , "remove redundant && (loop)" , simplified .getValue ());
47+ private static ValDerivationNode simplifyToFixedPoint (ValDerivationNode current , Expression prevExp ) {
48+ ValDerivationNode simplified = simplifyOnce (current , prevExp );
6949 Expression currExp = simplified .getValue ();
7050
7151 // fixed point reached — compare on toString() because propagate/fold/reduce mutate the AST in place, so a
@@ -80,7 +60,17 @@ private static ValDerivationNode simplifyToFixedPoint(ValDerivationNode current,
8060 }
8161
8262 // continue simplifying
83- return simplifyToFixedPoint (simplified , simplified .getValue (), pass , prev );
63+ return simplifyToFixedPoint (simplified , simplified .getValue ());
64+ }
65+
66+ private static ValDerivationNode simplifyOnce (ValDerivationNode current , Expression prevExp ) {
67+ ValDerivationNode prop = VariablePropagation .propagate (prevExp , current );
68+ DebugLog .simplificationPass ("variable propagation" , prop .getValue ());
69+ ValDerivationNode fold = ExpressionFolding .fold (prop );
70+ DebugLog .simplificationPass ("expression folding" , fold .getValue ());
71+ ValDerivationNode simplified = simplifyValDerivationNode (fold );
72+ DebugLog .simplificationPass ("remove redundant && (loop)" , simplified .getValue ());
73+ return simplified ;
8474 }
8575
8676 /**
0 commit comments