@Override protected SoyData visitNotOpNode(NotOpNode node) { SoyData operand = visit(node.getChild(0)); return convertResult( ! operand.toBoolean() ); }
@Override protected void visitOrOpNode(OrOpNode node) { // Recurse. visitChildren(node); // Can simplify if either child is constant. We assume no side-effects. SoyData operand0 = getConstantOrNull(node.getChild(0)); SoyData operand1 = getConstantOrNull(node.getChild(1)); if (operand0 == null && operand1 == null) { return; // cannot simplify } ExprNode replacementNode; if (operand0 != null && operand1 != null) { replacementNode = new BooleanNode(operand0.toBoolean() || operand1.toBoolean()); } else if (operand0 != null) { replacementNode = operand0.toBoolean() ? new BooleanNode(true) : node.getChild(1); } else /*(operand1 != null)*/ { replacementNode = operand1.toBoolean() ? new BooleanNode(true) : node.getChild(0); } node.getParent().replaceChild(node, replacementNode); }
@Override protected void visitAndOpNode(AndOpNode node) { // Recurse. visitChildren(node); // Can simplify if either child is constant. We assume no side-effects. SoyData operand0 = getConstantOrNull(node.getChild(0)); SoyData operand1 = getConstantOrNull(node.getChild(1)); if (operand0 == null && operand1 == null) { return; // cannot simplify } ExprNode replacementNode; if (operand0 != null && operand1 != null) { replacementNode = new BooleanNode(operand0.toBoolean() && operand1.toBoolean()); } else if (operand0 != null) { replacementNode = operand0.toBoolean() ? node.getChild(1) : new BooleanNode(false); } else /*(operand1 != null)*/ { replacementNode = operand1.toBoolean() ? node.getChild(0) : new BooleanNode(false); } node.getParent().replaceChild(node, replacementNode); }
@Override protected void visitIfNode(IfNode node) { for (SoyNode child : node.getChildren()) { if (child instanceof IfCondNode) { IfCondNode icn = (IfCondNode) child; if (eval(icn.getExprUnion().getExpr()).toBoolean()) { visit(icn); return; } } else if (child instanceof IfElseNode) { visit(child); return; } else { throw new AssertionError(); } } }
@Override protected SoyData visitConditionalOpNode(ConditionalOpNode node) { // Note: We only evaluate the part that we need. SoyData operand0 = visit(node.getChild(0)); if (operand0.toBoolean()) { return visit(node.getChild(1)); } else { return visit(node.getChild(2)); } }
@Override protected void visitConditionalOpNode(ConditionalOpNode node) { // Recurse. visitChildren(node); // Can simplify if operand0 is constant. We assume no side-effects. SoyData operand0 = getConstantOrNull(node.getChild(0)); if (operand0 == null) { return; // cannot simplify } ExprNode replacementNode = operand0.toBoolean() ? node.getChild(1) : node.getChild(2); node.getParent().replaceChild(node, replacementNode); }
if (getConstantOrNull(condExpr).toBoolean()) {