Venice 0.751beta

nz.org.venice.parser
Interface Expression

All Superinterfaces:
java.lang.Cloneable
All Known Implementing Classes:
AbstractExpression

public interface Expression
extends java.lang.Cloneable

Representation of a composite executable parse tree. Any expression in the Gondola language is parsed into a composite structure built upon this class. This class therefore represents an executable expression.

Any single object of this type could represent a terminal expression that is a number such as "5", a unary expression such as "not(X)", a binary expression such as "X and Y" or a ternary expression. The arguments labelled above as X and Y would be represented by separate Expression classes. Those classes would however be contained by this class.


Field Summary
static int BOOLEAN_TYPE
          A boolean type that can be either TRUE or FALSE.
static double FALSE
          Value of FALSE
static int FLOAT_QUOTE_TYPE
          Represents a stock float quote type: open, close, low, high
static int FLOAT_TYPE
          A float type that can contain any number.
static int INTEGER_QUOTE_TYPE
          Represents a stock integer quote type: volume
static int INTEGER_SHORT_TYPE
          Represents a type than can contain any short number.
static int INTEGER_TYPE
          An integer type that can contain any integer number.
static int NUMERIC_TYPE
          Represents either a float or an integer.
static int STRING_TYPE
          Represents a string type that can contain any string.
static double TRUE
          Value of TRUE
static double TRUE_LEVEL
          Threshold level where a number is registered as TRUE
static int UNDEFINED_TYPE
          Type is undefined.
 
Method Summary
 int checkType()
          Perform type checking on the expression.
 java.lang.Object clone()
           
 boolean equals(java.lang.Object object)
          Returns whether this expression tree and the given expression tree are equivalent.
 double evaluate(Variables variables, QuoteBundle quoteBundle, Symbol symbol, int day)
          Evaluates the given expression and returns the result.
 Expression getChild(int index)
          Return the given argument.
 int getChildCount()
          Return the number of arguments of this expression.
 java.lang.String getId()
           
 int getIndex(Expression expression)
          Return the index of the given argument in the expression.
 Expression getParent()
          Return the parent of this node.
 ParseMetadata getParseMetadata()
           
 int getType()
          Get the type of the expression.
 int hashCode()
          If you override the equals(java.lang.Object) method then you should override this method.
 boolean isRoot()
          Return whether this node is the root node.
 java.util.Iterator iterator()
          Return an iterator to iterate over all the nodes in this expression's tree.
 Expression setChild(Expression child, int index)
          Set the argument.
 void setParent(Expression parent)
          Set the parent of this node.
 void setParseMetadata(java.util.HashMap parseTree, java.util.HashMap tokenLineMap)
          Store the metadata about how the expressing parsing.
 Expression simplify()
          Perform simplifications and optimisations on the expression tree.
 int size()
          Count the number of nodes in the tree.
 int size(int type)
          Count the number of nodes in the tree with the given type.
 java.lang.String toString()
          Convert the given expression to a string.
 boolean validTree()
           
 

Field Detail

UNDEFINED_TYPE

public static final int UNDEFINED_TYPE
Type is undefined. Initial or default value only. Shouldn't be returned unless there's a bug.

See Also:
Constant Field Values

BOOLEAN_TYPE

public static final int BOOLEAN_TYPE
A boolean type that can be either TRUE or FALSE.

See Also:
Constant Field Values

FLOAT_TYPE

public static final int FLOAT_TYPE
A float type that can contain any number.

See Also:
Constant Field Values

INTEGER_TYPE

public static final int INTEGER_TYPE
An integer type that can contain any integer number.

See Also:
Constant Field Values

FLOAT_QUOTE_TYPE

public static final int FLOAT_QUOTE_TYPE
Represents a stock float quote type: open, close, low, high

See Also:
Constant Field Values

INTEGER_QUOTE_TYPE

public static final int INTEGER_QUOTE_TYPE
Represents a stock integer quote type: volume

See Also:
Constant Field Values

STRING_TYPE

public static final int STRING_TYPE
Represents a string type that can contain any string.

See Also:
Constant Field Values

TRUE_LEVEL

public static final double TRUE_LEVEL
Threshold level where a number is registered as TRUE

See Also:
Constant Field Values

INTEGER_SHORT_TYPE

public static final int INTEGER_SHORT_TYPE
Represents a type than can contain any short number.

See Also:
Constant Field Values

NUMERIC_TYPE

public static final int NUMERIC_TYPE
Represents either a float or an integer.

See Also:
Constant Field Values

TRUE

public static final double TRUE
Value of TRUE

See Also:
Constant Field Values

FALSE

public static final double FALSE
Value of FALSE

See Also:
Constant Field Values
Method Detail

evaluate

public double evaluate(Variables variables,
                       QuoteBundle quoteBundle,
                       Symbol symbol,
                       int day)
                throws EvaluationException
Evaluates the given expression and returns the result.

Parameters:
variables - variable storage area for expression
quoteBundle - the quote bundle containing quote data to use
symbol - the current symbol
day - current date in cache fast access format
Returns:
the result of the expression
Throws:
EvaluationException - if the expression performs an illegal operation such as divide by zero.

toString

public java.lang.String toString()
Convert the given expression to a string.

Returns:
the string representation of the expression

checkType

public int checkType()
              throws TypeMismatchException
Perform type checking on the expression.

Returns:
the return type of the expression
Throws:
TypeMismatchException - if the expression has incorrect types

getType

public int getType()
Get the type of the expression.

Returns:
one of BOOLEAN_TYPE, FLOAT_TYPE, INTEGER_TYPE, FLOAT_QUOTE_TYPE or INTEGER_QUOTE_TYPE.

getChildCount

public int getChildCount()
Return the number of arguments of this expression.

Returns:
the number of arguments

getParent

public Expression getParent()
Return the parent of this node.

Returns:
the parent.

setParent

public void setParent(Expression parent)
Set the parent of this node.

Parameters:
parent - the parent.

clone

public java.lang.Object clone()

isRoot

public boolean isRoot()
Return whether this node is the root node.

Returns:
TRUE if this node has no parent.

iterator

public java.util.Iterator iterator()
Return an iterator to iterate over all the nodes in this expression's tree.

Returns:
iterator.

getChild

public Expression getChild(int index)
Return the given argument.

Parameters:
index - the argument index
Returns:
the argument

setChild

public Expression setChild(Expression child,
                           int index)
Set the argument.

Parameters:
child - new argument expression
index - index of the argument expression

simplify

public Expression simplify()
Perform simplifications and optimisations on the expression tree. For example, if the expression tree was a and true then the expression tree would be simplified to a.


getIndex

public int getIndex(Expression expression)
Return the index of the given argument in the expression. We override this method because we use "==" to denote equality, not "equals" as the former would return the first argument with the same expression not necessarily the actual expression instance desired.

Parameters:
expression - the child expression to locate
Returns:
index of the child expression or -1 if it could not be found

equals

public boolean equals(java.lang.Object object)
Returns whether this expression tree and the given expression tree are equivalent.

Parameters:
object - the other expression

hashCode

public int hashCode()
If you override the equals(java.lang.Object) method then you should override this method. It provides a very basic hash code function.

Returns:
a poor hash code of the tree

size

public int size()
Count the number of nodes in the tree.

Returns:
number of nodes or 1 if this is a terminal node

size

public int size(int type)
Count the number of nodes in the tree with the given type.

Returns:
number of nodes in the tree with the given type.

validTree

public boolean validTree()
Returns:
true if none of the children of the expression are null (slightly deprecated since 7.21 - shouldn't be anything but true because children are now immutable and consequently not allowed to be null)

setParseMetadata

public void setParseMetadata(java.util.HashMap parseTree,
                             java.util.HashMap tokenLineMap)
Store the metadata about how the expressing parsing.

Parameters:
parseTree - A map which relates a constructed expression with it's associated token.
tokenLineMap - A map which associates a token to which line number of the rule text it appears.

getParseMetadata

public ParseMetadata getParseMetadata()
Returns:
metadata information about the parse. (e.g. line numbers)

getId

public java.lang.String getId()
Returns:
the unique id of the expression. (Not all expressions implement a proper hashcode())

Venice 0.751beta