Venice 0.751beta

nz.org.venice.parser.expression
Class AbstractExpression

java.lang.Object
  extended bynz.org.venice.parser.expression.AbstractExpression
All Implemented Interfaces:
java.lang.Cloneable, Expression
Direct Known Subclasses:
BinaryExpression, ClauseExpression, QuaternaryExpression, TerminalExpression, TernaryExpression, UnaryExpression

public abstract class AbstractExpression
extends java.lang.Object
implements Expression

The abstract base class for all expressions in the Gondola language. This class implements the Expression interface and provides functions for managing an expression's child nodes. E.g. the expression 4+5 would consist of three nodes. The plus being the root node, which would have two child nodes of 4 and 5.

See Also:
Expression

Field Summary
 
Fields inherited from interface nz.org.venice.parser.Expression
BOOLEAN_TYPE, FALSE, FLOAT_QUOTE_TYPE, FLOAT_TYPE, INTEGER_QUOTE_TYPE, INTEGER_SHORT_TYPE, INTEGER_TYPE, NUMERIC_TYPE, STRING_TYPE, TRUE, TRUE_LEVEL, UNDEFINED_TYPE
 
Constructor Summary
AbstractExpression()
          Create a new expression with no children.
AbstractExpression(Expression[] children)
          Create a new expression with the given number of children.
AbstractExpression(java.util.List children)
          Create a new expression with the given number of children.
 
Method Summary
abstract  java.lang.Object clone()
           
 boolean equals(java.lang.Object object)
          Returns whether this expression tree and the given expression tree are equivalent.
 Expression getChild(int index)
          Return the child of this node at the given index.
 java.lang.String getId()
           
 int getIndex(Expression child)
          Return the index of the given argument in the expression.
static java.text.NumberFormat getNumberFormat()
           
 Expression getParent()
          Get the parent of this node.
 ParseMetadata getParseMetadata()
           
 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 over the node's children.
static double parseDouble(java.lang.String doubleText)
          Parses doubleText from a string to produce a double.
static int parseInt(java.lang.String intText)
          Parses intText from a string to produce an integer.
 java.lang.String printParents()
           
 Expression setChild(Expression child, int index)
          Set the argument.
 void setChildMutableVersion(Expression child, int index)
          Set this expression's child to the given child.
 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.
 Expression simplifyMutableVersion()
          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()
          Return true if none of the children of this expression are null.
static java.lang.Double valueOfDouble(java.lang.String doubleText)
          Parses doubleText from a string to produce a Double Object.
static java.lang.Integer valueOfInt(java.lang.String intText)
          Parses intText from a string to produce an Integer Object.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface nz.org.venice.parser.Expression
checkType, evaluate, getChildCount, getType
 

Constructor Detail

AbstractExpression

public AbstractExpression()
Create a new expression with no children.


AbstractExpression

public AbstractExpression(Expression[] children)
Create a new expression with the given number of children.

Parameters:
children - An array of the children of the expression.

AbstractExpression

public AbstractExpression(java.util.List children)
Create a new expression with the given number of children.

Parameters:
children - A List of the children of the expression.
Method Detail

getParent

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

Specified by:
getParent in interface Expression
Returns:
the parent.

setParent

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

Specified by:
setParent in interface Expression
Parameters:
parent - the new parent.

getChild

public Expression getChild(int index)
Return the child of this node at the given index.

Specified by:
getChild in interface Expression
Parameters:
index - the argument index
Returns:
child at given index.

isRoot

public boolean isRoot()
Return whether this node is the root node. The root node has no parent.

Specified by:
isRoot in interface Expression
Returns:
TRUE iff this node is the root node.

setChild

public Expression setChild(Expression child,
                           int index)
Description copied from interface: Expression
Set the argument.

Specified by:
setChild in interface Expression
Parameters:
child - new argument expression
index - index of the argument expression

setChildMutableVersion

public void setChildMutableVersion(Expression child,
                                   int index)
Set this expression's child to the given child. The new child will be removed from its parent.

Parameters:
child - the new child.
index - the index of the new child.

validTree

public boolean validTree()
Return true if none of the children of this expression are null. This means the expression can be simplified safely.

Specified by:
validTree in interface Expression
Returns:
true if all children are not null

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.

Specified by:
simplify in interface Expression

simplifyMutableVersion

public Expression simplifyMutableVersion()
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 child)
Return the index of the given argument in the expression. This method uses reference equality rather than using the equals method.

Specified by:
getIndex in interface Expression
Parameters:
child - the child expression to locate
Returns:
index of the child expression or -1 if it could not be found

parseDouble

public static double parseDouble(java.lang.String doubleText)
                          throws java.lang.NumberFormatException
Parses doubleText from a string to produce a double.

Parameters:
doubleText - the string to be parsed
Returns:
the parsed value
Throws:
java.lang.NumberFormatException

parseInt

public static int parseInt(java.lang.String intText)
                    throws java.lang.NumberFormatException
Parses intText from a string to produce an integer.

Parameters:
intText - the string to be parsed
Returns:
the parsed value
Throws:
java.lang.NumberFormatException

valueOfDouble

public static java.lang.Double valueOfDouble(java.lang.String doubleText)
                                      throws java.lang.NumberFormatException
Parses doubleText from a string to produce a Double Object.

Parameters:
doubleText - the string to be parsed
Returns:
the parsed value as Double Object
Throws:
java.lang.NumberFormatException

valueOfInt

public static java.lang.Integer valueOfInt(java.lang.String intText)
                                    throws java.lang.NumberFormatException
Parses intText from a string to produce an Integer Object.

Parameters:
intText - the string to be parsed
Returns:
the parsed value as Integer Object
Throws:
java.lang.NumberFormatException

getNumberFormat

public static java.text.NumberFormat getNumberFormat()

equals

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

Specified by:
equals in interface Expression
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.

Specified by:
hashCode in interface Expression
Returns:
a poor hash code of the tree

size

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

Specified by:
size in interface Expression
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.

Specified by:
size in interface Expression
Returns:
number of nodes in the tree with the given type.

iterator

public java.util.Iterator iterator()
Return an iterator over the node's children.

Specified by:
iterator in interface Expression
Returns:
iterator.

toString

public java.lang.String toString()
Description copied from interface: Expression
Convert the given expression to a string.

Specified by:
toString in interface Expression

clone

public abstract java.lang.Object clone()
Specified by:
clone in interface Expression

printParents

public java.lang.String printParents()

setParseMetadata

public void setParseMetadata(java.util.HashMap parseTree,
                             java.util.HashMap tokenLineMap)
Description copied from interface: Expression
Store the metadata about how the expressing parsing.

Specified by:
setParseMetadata in interface Expression
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()
Specified by:
getParseMetadata in interface Expression
Returns:
metadata information about the parse. (e.g. line numbers)

getId

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

Venice 0.751beta