Package com.blackrook.expression
Class Expression
- java.lang.Object
-
- com.blackrook.expression.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 Summary
Fields Modifier and Type Field Description static Expression
FALSE
Expression: literal false.static Expression
FLOAT_0
Expression: literal 0.0.static Expression
FLOAT_1
Expression: literal 1.0.static Expression
INFINITY
Expression: Infinity.static Expression
INTEGER_0
Expression: literal 0.static Expression
INTEGER_1
Expression: literal 1.static Expression
NAN
Expression: NaN.static String
RETURN_VARIABLE
Return variable name.static Expression
TRUE
Expression: literal true.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
collapse()
Collapses this expression.static Expression
create(boolean value)
Creates an expression that is just one value.static Expression
create(double value)
Creates an expression that is just one value.static Expression
create(long value)
Creates an expression that is just one value.static Expression
create(ExpressionNode[] nodes)
Creates an expression that is a full expression.static Expression
create(ExpressionValue value)
Creates an expression that is just one value.boolean
equals(Expression other)
Tests if two expressions are the same.boolean
equals(Object obj)
ExpressionValue
evaluate()
Evaluates this expression.void
evaluate(ExpressionStack stack, ExpressionVariableContext context, ExpressionValue out)
Evaluates this expression.void
evaluate(ExpressionValue out)
Evaluates this expression.void
evaluate(ExpressionVariableContext context, ExpressionValue out)
Evaluates this expression.String
getDigest()
Gets the SHA-1 digest of this expression, in order to find duplicates.String
getDigest(boolean forceRecalc)
Gets the SHA1 digest of this expression, in order to find duplicates.String
getSource()
Gets the source code that was used to create this expression.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.boolean
isCollapsible()
Checks if this expression is collapsable.boolean
isValue()
Checks if this is just a literal value (not complex).String
toString()
-
-
-
Field Detail
-
RETURN_VARIABLE
public static final String RETURN_VARIABLE
Return variable name.- See Also:
- Constant Field Values
-
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).
-
collapse
public void collapse()
Collapses this expression.- See Also:
evaluate(ExpressionValue)
,isCollapsible()
-
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.
-
-