Class IfStmt

  • All Implemented Interfaces:
    ASTNode, Statement

    public final class IfStmt
    extends Object
    implements Statement
    An if statement with an optional else branch.
    • Constructor Detail

      • IfStmt

        public IfStmt​(Expression condition,
                      Statement thenBranch,
                      Statement elseBranch,
                      SourceLocation location)
        Creates an if statement and parents its children to it.
        Parameters:
        condition - the branch condition
        thenBranch - statement executed when the condition holds
        elseBranch - statement executed otherwise, or null for none
        location - source location, or null for unknown
        Throws:
        NullPointerException - if condition or thenBranch is null
      • IfStmt

        public IfStmt​(Expression condition,
                      Statement thenBranch,
                      Statement elseBranch)
        Creates an if statement with an unknown location.
        Parameters:
        condition - the branch condition
        thenBranch - statement executed when the condition holds
        elseBranch - statement executed otherwise, or null for none
        Throws:
        NullPointerException - if condition or thenBranch is null
      • IfStmt

        public IfStmt​(Expression condition,
                      Statement thenBranch)
        Creates an if statement without an else branch.
        Parameters:
        condition - the branch condition
        thenBranch - statement executed when the condition holds
        Throws:
        NullPointerException - if condition or thenBranch is null
    • Method Detail

      • getCondition

        public Expression getCondition()
        Returns:
        the condition
      • setCondition

        public void setCondition​(Expression condition)
        Replaces the branch condition, reparenting old and new nodes.
        Parameters:
        condition - the new condition
      • getThenBranch

        public Statement getThenBranch()
        Returns:
        the then branch
      • setThenBranch

        public void setThenBranch​(Statement thenBranch)
        Replaces the then branch, reparenting old and new nodes.
        Parameters:
        thenBranch - the new then branch
      • getElseBranch

        public Statement getElseBranch()
        Returns:
        the else branch
      • setElseBranch

        public void setElseBranch​(Statement elseBranch)
        Replaces the else branch, reparenting old and new nodes.
        Parameters:
        elseBranch - the new else branch, or null to remove it
      • 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 enclosing AST node.
        Specified by:
        setParent in interface ASTNode
        Parameters:
        parent - the new parent node
      • hasElse

        public boolean hasElse()
        Returns:
        true if this if statement has an else branch
      • isElseIf

        public boolean isElseIf()
        Returns:
        true if the else branch is itself an if statement
      • withCondition

        public IfStmt withCondition​(Expression condition)
        Replaces the branch condition, reparenting old and new nodes.
        Parameters:
        condition - the new condition
        Returns:
        this statement
      • withThenBranch

        public IfStmt withThenBranch​(Statement thenBranch)
        Replaces the then branch, reparenting old and new nodes.
        Parameters:
        thenBranch - the new then branch
        Returns:
        this statement
      • withElseBranch

        public IfStmt withElseBranch​(Statement elseBranch)
        Replaces the else branch, reparenting old and new nodes.
        Parameters:
        elseBranch - the new else branch, or null to remove it
        Returns:
        this statement
      • 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