Class BinaryExpr

  • All Implemented Interfaces:
    ASTNode, Expression

    public final class BinaryExpr
    extends Object
    implements Expression
    A binary expression: left op right, including assignments and logical operators.
    • Constructor Detail

      • BinaryExpr

        public BinaryExpr​(BinaryOperator operator,
                          Expression left,
                          Expression right,
                          SourceType type,
                          SourceLocation location)
        Creates a binary expression and reparents both operands.
        Parameters:
        operator - the binary operator
        left - the left operand
        right - the right operand
        type - the result type of the expression
        location - the source location, or null for unknown
        Throws:
        NullPointerException - if operator, left, right, or type is null
      • BinaryExpr

        public BinaryExpr​(BinaryOperator operator,
                          Expression left,
                          Expression right,
                          SourceType type)
        Creates a binary expression with an unknown source location.
        Parameters:
        operator - the binary operator
        left - the left operand
        right - the right operand
        type - the result type of the expression
        Throws:
        NullPointerException - if operator, left, right, or type is null
    • Method Detail

      • getOperator

        public BinaryOperator getOperator()
        Returns:
        the operator
      • setOperator

        public void setOperator​(BinaryOperator operator)
        Parameters:
        operator - the new binary operator
      • getLeft

        public Expression getLeft()
        Returns:
        the left
      • setLeft

        public void setLeft​(Expression left)
        Replaces the left operand, reparenting the new child.
        Parameters:
        left - the new left operand
      • getRight

        public Expression getRight()
        Returns:
        the right operand
      • setRight

        public void setRight​(Expression right)
        Replaces the right operand, reparenting the new child.
        Parameters:
        right - the new right operand
      • 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)
        Description copied from interface: ASTNode
        Sets the parent node in the AST tree.
        Specified by:
        setParent in interface ASTNode
        Parameters:
        parent - the enclosing AST node
      • getPrecedence

        public int getPrecedence()
        Returns:
        the precedence of the operator
      • isAssignment

        public boolean isAssignment()
        Returns:
        true if the operator is an assignment
      • isComparison

        public boolean isComparison()
        Returns:
        true if the operator is a comparison
      • isLogical

        public boolean isLogical()
        Returns:
        true if the operator is logical (&& or ||)
      • withOperator

        public BinaryExpr withOperator​(BinaryOperator operator)
        Replaces the operator.
        Parameters:
        operator - the new binary operator
        Returns:
        this expression
      • withLeft

        public BinaryExpr withLeft​(Expression left)
        Replaces the left operand, reparenting the new child and releasing the former one.
        Parameters:
        left - the new left operand
        Returns:
        this expression
      • withRight

        public BinaryExpr withRight​(Expression right)
        Replaces the right operand, reparenting the new child and releasing the former one.
        Parameters:
        right - the new right operand
        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