Class TernaryExpr

  • All Implemented Interfaces:
    ASTNode, Expression

    public final class TernaryExpr
    extends Object
    implements Expression
    A conditional expression - condition ? thenExpr : elseExpr - whose three operands are children of this node.
    • Constructor Detail

      • TernaryExpr

        public TernaryExpr​(Expression condition,
                           Expression thenExpr,
                           Expression elseExpr,
                           SourceType type,
                           SourceLocation location)
        Creates a conditional expression and adopts the three operands as children.
        Parameters:
        condition - the test
        thenExpr - the value when the test holds
        elseExpr - the value when it does not
        type - the expression's static type
        location - source position, or null for an unknown one
        Throws:
        NullPointerException - if condition, thenExpr, elseExpr or type is null
      • TernaryExpr

        public TernaryExpr​(Expression condition,
                           Expression thenExpr,
                           Expression elseExpr,
                           SourceType type)
        Creates a conditional expression with an unknown source position.
        Parameters:
        condition - the test
        thenExpr - the value when the test holds
        elseExpr - the value when it does not
        type - the expression's static type
        Throws:
        NullPointerException - if condition, thenExpr, elseExpr or type is null
    • Method Detail

      • getCondition

        public Expression getCondition()
        Returns:
        the test expression
      • setCondition

        public void setCondition​(Expression condition)
        Replaces the test, reparenting it and releasing the old one.
        Parameters:
        condition - the new test
      • getThenExpr

        public Expression getThenExpr()
        Returns:
        the value taken when the test holds
      • setThenExpr

        public void setThenExpr​(Expression thenExpr)
        Replaces the then-value, reparenting it and releasing the old one.
        Parameters:
        thenExpr - the new then-value
      • getElseExpr

        public Expression getElseExpr()
        Returns:
        the else-value
      • setElseExpr

        public void setElseExpr​(Expression elseExpr)
        Replaces the else-value, reparenting it and releasing the old one.
        Parameters:
        elseExpr - the new else-value
      • getType

        public SourceType getType()
        Description copied from interface: Expression
        Gets the inferred type of this expression.
        Specified by:
        getType in interface Expression
        Returns:
        the static type of this expression
      • getLocation

        public SourceLocation getLocation()
        Description copied from interface: ASTNode
        Gets the source location of this node.
        Specified by:
        getLocation in interface ASTNode
        Returns:
        the location
      • getParent

        public ASTNode getParent()
        Description copied from interface: ASTNode
        Gets the parent node in the AST tree.
        Specified by:
        getParent in interface ASTNode
        Returns:
        the parent
      • setParent

        public void setParent​(ASTNode parent)
        Sets the node this expression hangs under.
        Specified by:
        setParent in interface ASTNode
        Parameters:
        parent - the owning node
      • withCondition

        public TernaryExpr withCondition​(Expression condition)
        Replaces the test, reparenting it and releasing the old one.
        Parameters:
        condition - the new test, or null to clear it
        Returns:
        this expression
      • withThenExpr

        public TernaryExpr withThenExpr​(Expression thenExpr)
        Replaces the then-value, reparenting it and releasing the old one.
        Parameters:
        thenExpr - the new then-value, or null to clear it
        Returns:
        this expression
      • withElseExpr

        public TernaryExpr withElseExpr​(Expression elseExpr)
        Replaces the else-value, reparenting it and releasing the old one.
        Parameters:
        elseExpr - the new else-value, or null to clear it
        Returns:
        this expression
      • getChildren

        public List<ASTNode> getChildren()
        Description copied from interface: ASTNode
        Lists the direct children a node holds, in source order.
        Specified by:
        getChildren in interface ASTNode
        Returns:
        the direct children, empty for a leaf
      • accept

        public <T> T accept​(SourceVisitor<T> visitor)
        Description copied from interface: ASTNode
        Accepts a visitor for this node.
        Specified by:
        accept in interface ASTNode
        Type Parameters:
        T - the return type of the visitor
        Parameters:
        visitor - the visitor to accept
        Returns:
        the result from the visitor