Class LiteralExpr

  • All Implemented Interfaces:
    ASTNode, Expression

    public final class LiteralExpr
    extends Object
    implements Expression
    A literal value: integers, floats, strings, booleans, chars, or null.
    • Constructor Detail

      • LiteralExpr

        public LiteralExpr​(Object value,
                           SourceType type,
                           SourceLocation location)
        Creates a literal.
        Parameters:
        value - the literal value, or null for the null literal
        type - the literal's source type
        location - the source location, or null for unknown
      • LiteralExpr

        public LiteralExpr​(Object value,
                           SourceType type)
        Creates a literal with an unknown source location.
        Parameters:
        value - the literal value, or null for the null literal
        type - the literal's source type
    • Method Detail

      • getValue

        public Object getValue()
        Returns:
        the value
      • setValue

        public void setValue​(Object value)
        Parameters:
        value - the new literal 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 type
      • setType

        public void setType​(SourceType type)
        Parameters:
        type - the new source type
      • 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
      • ofInt

        public static LiteralExpr ofInt​(int value)
        Creates an int literal.
        Parameters:
        value - the literal value
        Returns:
        the new literal
      • ofLong

        public static LiteralExpr ofLong​(long value)
        Creates a long literal.
        Parameters:
        value - the literal value
        Returns:
        the new literal
      • ofFloat

        public static LiteralExpr ofFloat​(float value)
        Creates a float literal.
        Parameters:
        value - the literal value
        Returns:
        the new literal
      • ofDouble

        public static LiteralExpr ofDouble​(double value)
        Creates a double literal.
        Parameters:
        value - the literal value
        Returns:
        the new literal
      • ofBoolean

        public static LiteralExpr ofBoolean​(boolean value)
        Creates a boolean literal.
        Parameters:
        value - the literal value
        Returns:
        the new literal
      • ofChar

        public static LiteralExpr ofChar​(char value)
        Creates a char literal.
        Parameters:
        value - the literal value
        Returns:
        the new literal
      • ofString

        public static LiteralExpr ofString​(String value)
        Creates a String literal.
        Parameters:
        value - the literal value
        Returns:
        the new literal
      • ofNull

        public static LiteralExpr ofNull()
        Creates a null literal typed as Object.
        Returns:
        the new literal
      • withValue

        public LiteralExpr withValue​(Object value)
        Replaces the literal value.
        Parameters:
        value - the new literal value
        Returns:
        this expression
      • withType

        public LiteralExpr withType​(SourceType type)
        Replaces the source type.
        Parameters:
        type - the new source type
        Returns:
        this expression
      • isNull

        public boolean isNull()
        Returns:
        true if the value is null
      • isString

        public boolean isString()
        Returns:
        true if the value is a String
      • isNumeric

        public boolean isNumeric()
        Returns:
        true if the value is a Number
      • 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