Class Expression


  • public class Expression
    extends Object
    An expression object for evaluating dynamic calculations. Expressions, at best, should do no memory allocations during evaluation - they can be called multiple times per gametic.
    Author:
    Matthew Tropiano
    • Field Detail

      • TRUE

        public static final Expression TRUE
        Expression: literal true.
      • FALSE

        public static final Expression FALSE
        Expression: literal false.
      • FLOAT_0

        public static final Expression FLOAT_0
        Expression: literal 0.0.
      • FLOAT_1

        public static final Expression FLOAT_1
        Expression: literal 1.0.
      • INTEGER_0

        public static final Expression INTEGER_0
        Expression: literal 0.
      • INTEGER_1

        public static final Expression INTEGER_1
        Expression: literal 1.
      • INFINITY

        public static final Expression INFINITY
        Expression: Infinity.
      • NAN

        public static final Expression NAN
        Expression: NaN.
    • Method Detail

      • create

        public static Expression create​(boolean value)
        Creates an expression that is just one value.
        Parameters:
        value - the value.
        Returns:
        a new expression.
      • create

        public static Expression create​(long value)
        Creates an expression that is just one value.
        Parameters:
        value - the value.
        Returns:
        a new expression.
      • create

        public static Expression create​(double value)
        Creates an expression that is just one value.
        Parameters:
        value - the value.
        Returns:
        a new expression.
      • create

        public static Expression create​(ExpressionValue value)
        Creates an expression that is just one value.
        Parameters:
        value - the value.
        Returns:
        a new expression.
      • create

        public static Expression create​(ExpressionNode[] nodes)
        Creates an expression that is a full expression.
        Parameters:
        nodes - the list of expression nodes.
        Returns:
        a new expression.
      • evaluate

        public void evaluate​(ExpressionStack stack,
                             ExpressionVariableContext context,
                             ExpressionValue out)
        Evaluates this expression.
        Parameters:
        stack - the expression stack to use.
        context - the mutable variable context to use.
        out - the output value (returned value, top of stack, or literal value encapsulated).
      • evaluate

        public void evaluate​(ExpressionVariableContext context,
                             ExpressionValue out)
        Evaluates this expression. Creates a new stack.
        Parameters:
        context - the mutable variable context to use.
        out - the output value (returned value, top of stack, or literal value encapsulated).
      • evaluate

        public void evaluate​(ExpressionValue out)
        Evaluates this expression. Creates a new stack, and an empty context.
        Parameters:
        out - the output value (returned value, top of stack, or literal value encapsulated).
      • evaluate

        public ExpressionValue evaluate()
        Evaluates this expression. Creates a new stack, an empty context, and a value to put the result in.
        Returns:
        the output value (returned value, top of stack, or literal value encapsulated).
      • isCollapsible

        public boolean isCollapsible()
        Checks if this expression is collapsable.
        Returns:
        true if so, false if not.
      • isValue

        public boolean isValue()
        Checks if this is just a literal value (not complex).
        Returns:
        true if so, false if not.
      • getSource

        public String getSource()
        Gets the source code that was used to create this expression.
        Returns:
        the source or null if not created from a string.
      • getDigest

        public String getDigest()
        Gets the SHA-1 digest of this expression, in order to find duplicates. Within the Universe of all expressions, this digest should be sufficient enough to find equal expressions. Under the covers, this calculates the digest if it hasn't been calculated, yet. If it has, it returns the previously calculated digest.
        Returns:
        the SHA-1 digest.
      • getDigest

        public String getDigest​(boolean forceRecalc)
        Gets the SHA1 digest of this expression, in order to find duplicates.
        Parameters:
        forceRecalc - if true, forces a recalculation before return.
        Returns:
        a byte array of the digest.
        Throws:
        ExpressionException - if the digest could not be calculated for some reason.
      • intern

        public Expression intern()
        Internalizes an expression: adds this expression to an internal bank and returns it, or returns an existing reference for the same expression already stored.
        Returns:
        this expression, or a reference to an equal one.
      • equals

        public boolean equals​(Expression other)
        Tests if two expressions are the same. Only checks the digests - SHA1 hashes should be good enough.
        Parameters:
        other - the other expression.
        Returns:
        true if so, false if not.