Class ForStmt

  • All Implemented Interfaces:
    ASTNode, Statement

    public final class ForStmt
    extends Object
    implements Statement
    A basic for loop with init statements, an optional condition, and update expressions.
    • Constructor Detail

      • ForStmt

        public ForStmt​(List<Statement> init,
                       Expression condition,
                       List<Expression> update,
                       Statement body,
                       String label,
                       SourceLocation location)
        Creates a for loop, skipping null init and update entries and parenting children to it.
        Parameters:
        init - initializer statements, or null for none
        condition - loop condition, or null for an infinite loop
        update - update expressions, or null for none
        body - the loop body
        label - loop label, or null for none
        location - source location, or null for unknown
        Throws:
        NullPointerException - if body is null
      • ForStmt

        public ForStmt​(List<Statement> init,
                       Expression condition,
                       List<Expression> update,
                       Statement body)
        Creates an unlabeled for loop with an unknown location.
        Parameters:
        init - initializer statements, or null for none
        condition - loop condition, or null for an infinite loop
        update - update expressions, or null for none
        body - the loop body
        Throws:
        NullPointerException - if body is null
    • Method Detail

      • getCondition

        public Expression getCondition()
        Returns:
        the condition
      • setCondition

        public void setCondition​(Expression condition)
        Replaces the loop condition, reparenting old and new nodes.
        Parameters:
        condition - the new condition, or null for an infinite loop
      • getBody

        public Statement getBody()
        Returns:
        the body
      • setBody

        public void setBody​(Statement body)
        Replaces the loop body, reparenting old and new nodes.
        Parameters:
        body - the new body
      • setLabel

        public void setLabel​(String label)
        Sets the loop label.
        Parameters:
        label - the new label, or null for none
      • 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
      • infinite

        public static ForStmt infinite​(Statement body)
        Creates a conditionless for loop.
        Parameters:
        body - the loop body
        Returns:
        a new infinite for loop
      • isInfinite

        public boolean isInfinite()
        Returns:
        true if this loop has no condition
      • addInit

        public void addInit​(Statement stmt)
        Appends an initializer statement, ignoring null.
        Parameters:
        stmt - the statement to append
      • addUpdate

        public void addUpdate​(Expression expr)
        Appends an update expression, ignoring null.
        Parameters:
        expr - the expression to append
      • getLabel

        public String getLabel()
        Description copied from interface: Statement
        Gets the label for this statement, if any.
        Specified by:
        getLabel in interface Statement
        Returns:
        the label, or null if not labeled
      • withCondition

        public ForStmt withCondition​(Expression condition)
        Replaces the loop condition, reparenting old and new nodes.
        Parameters:
        condition - the new condition, or null for an infinite loop
        Returns:
        this statement
      • withBody

        public ForStmt withBody​(Statement body)
        Replaces the loop body, reparenting old and new nodes.
        Parameters:
        body - the new body
        Returns:
        this statement
      • withLabel

        public ForStmt withLabel​(String label)
        Sets the loop label.
        Parameters:
        label - the new label, or null for none
        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