com.voytechs.jnetstream.npl
Class OpNode

java.lang.Object
  extended by com.voytechs.jnetstream.npl.Node
      extended by com.voytechs.jnetstream.npl.OpNode
All Implemented Interfaces:
BooleanNode, IntNode, LongNode, Visitable
Direct Known Subclasses:
AndOpNode, AssignOpNode, CommaOpNode, DivideOpNode, GreaterEqualsThanOpNode, GreaterThanOpNode, InverseOpNode, LessEqualsThanOpNode, LessThanOpNode, LogicalAndOpNode, LogicalEqualOpNode, LogicalNotEqualOpNode, LogicalNotOpNode, LogicalOrOpNode, MinusOpNode, ModOpNode, MultiplyOpNode, OrOpNode, PlusOpNode, PowOpNode

public abstract class OpNode
extends Node
implements IntNode, BooleanNode, Visitable, LongNode

Base class for all operator nodes. Operators in an expression make certain operations on the operands on both left and right handside of the operator. For example "1 + 2" expression breaks down into 3 components. Operator "+", and 2 operands "1" and "2". The "1" operand is set as the left operand in operator "+", the "2" operand on the right side. You specify the left and right operands through either the constructor or using the set(), setLeft() or setRight() methods. The set() method is provided to allow overriding of previously set operands without having to specificly know if the operand being replaced is on the left or right side of the operator. This is usefull if are working with the actual operand and you use its getParent() method to get the operator object, but then you wish to replace the operand. You might not specifically know if the operand you just went through is on the left or right of the operand in the expression. Each operator carries three properties which are used in parsing of the expression.

Author:
Mark Bednarczyk

Field Summary
static int BINARY
           
protected  int cardinality
           
static int FLAG_NO_OP
           
protected  int flags
           
static int HIGH
           
protected  Node left
           
static int LOW
           
static int MEDIUM
           
static int NORMAL
           
protected  java.lang.String op
           
protected  int presedence
           
protected  Node right
           
static int UNARY
           
 
Fields inherited from class com.voytechs.jnetstream.npl.Node
parent, token
 
Constructor Summary
OpNode(java.lang.String op, int presedence)
          Base class for all Op nodes (Operators).
OpNode(java.lang.String op, int presedence, int cardinality, Node right, Node left)
          Base class for all Op nodes (Operators).
OpNode(java.lang.String op, int presedence, Node right, Node left)
          Base class for all Op nodes (Operators).
 
Method Summary
 boolean canOptimize()
          Checks to see if both left and right operands can be optimized.
 java.lang.Object clone()
          Clones the current Operator node.
 OpNode createOpNode(Node left)
           
 OpNode createOpNode(Node right, Node left)
           
 OpNode createOpNode(java.util.Stack operands)
          Creates a new node using the factory pattern.
 boolean equals(java.lang.Object s)
          Compares the name of the Operation with the given string.
 int getCardinality()
           
 int getFlags()
           
 Node getLeft()
          Returns the left handside operand.
 java.lang.String getOpString()
           
 int getPresedence()
           
 Node getRight()
          Returns the right handside operand.
static void main(java.lang.String[] args)
          Test function for OpNode
 Node optimize()
          If possible optimizes away its branches.
 void set(Node newValue, Node oldValue)
          Sets a new object for either left or right side of the Operation.
 void setFlag(int flag)
           
 void setLeft(Node node)
          Sets the left handside operand of this operation.
 void setRight(Node node)
          Sets the right handside operand of this operation.
 java.lang.String toString()
           
 boolean visit(Visitor visitor, java.lang.Object user1, java.lang.Object user2)
           
 
Methods inherited from class com.voytechs.jnetstream.npl.Node
getParent, getToken, setParent, setToken
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.voytechs.jnetstream.npl.IntNode
getInt
 
Methods inherited from interface com.voytechs.jnetstream.npl.BooleanNode
getBoolean
 
Methods inherited from interface com.voytechs.jnetstream.npl.LongNode
getLong
 

Field Detail

right

protected Node right

left

protected Node left

op

protected java.lang.String op

LOW

public static final int LOW
See Also:
Constant Field Values

NORMAL

public static final int NORMAL
See Also:
Constant Field Values

MEDIUM

public static final int MEDIUM
See Also:
Constant Field Values

HIGH

public static final int HIGH
See Also:
Constant Field Values

presedence

protected int presedence

UNARY

public static final int UNARY
See Also:
Constant Field Values

BINARY

public static final int BINARY
See Also:
Constant Field Values

cardinality

protected int cardinality

FLAG_NO_OP

public static final int FLAG_NO_OP
See Also:
Constant Field Values

flags

protected int flags
Constructor Detail

OpNode

public OpNode(java.lang.String op,
              int presedence)
Base class for all Op nodes (Operators). You can specify the following protoperties for all OP nodes. This constructor assumes BINARY cardinality of the operator. Also the left and right operands are initialized to null. This form of constructor is used with factory pattern methods which initialize basic properties of an operator but actual operands are assigned at a later time to this object or possibly its clone.

Parameters:
op - The name for this operator. This is a string that represents its name.
presedence - This an integer value that determines the operators presedence when parsing expression. The lower the presedence the more important the operator. Thus a lower level operator will have presedence with higher level operator. i.e. '+' operator has presedence over over a '='. That is you want to do calculations first and then the assignment. Use the predefined constant values in OpNode, such as
  • OpNode.LOW
  • OpNode.NORMAL
  • OpNode.MEDIUM
  • OpNode.HIGH

OpNode

public OpNode(java.lang.String op,
              int presedence,
              Node right,
              Node left)
Base class for all Op nodes (Operators). You can specify the following protoperties for all OP nodes. This constructor assumes BINARY cardinality of the operator.

Parameters:
op - The name for this operator. This is a string that represents its name.
presedence - This an integer value that determines the operators presedence when parsing expression. The lower the presedence the more important the operator. Thus a lower level operator will have presedence with higher level operator. i.e. '+' operator has presedence over over a '='. That is you want to do calculations first and then the assignment. Use the predefined constant values in OpNode, such as
  • OpNode.LOW
  • OpNode.NORMAL
  • OpNode.MEDIUM
  • OpNode.HIGH
right - The operand on the right side of the operator.
left - The operand on the left side of the operator.

OpNode

public OpNode(java.lang.String op,
              int presedence,
              int cardinality,
              Node right,
              Node left)
Base class for all Op nodes (Operators). You can specify the following protoperties for all OP nodes.

Parameters:
op - The name for this operator. This is a string that represents its name.
presedence - This an integer value that determines the operators presedence when parsing expression. The lower the presedence the more important the operator. Thus a lower level operator will have presedence with higher level operator. i.e. '+' operator has presedence over over a '='. That is you want to do calculations first and then the assignment. Use the predefined constant values in OpNode, such as
  • OpNode.LOW
  • OpNode.NORMAL
  • OpNode.MEDIUM
  • OpNode.HIGH
cardinality - Determines if the operator is binary or unary. Binary operators have both left and right handside in an expression. Unary oparators only the left. Use the predefine constants to set the value correctly:
  • OpNode.UNARY
  • OpNode.BINARY
right - The operand on the right side of the operator.
left - The operand on the left side of the operator.
Method Detail

visit

public boolean visit(Visitor visitor,
                     java.lang.Object user1,
                     java.lang.Object user2)
              throws NodeException
Specified by:
visit in interface Visitable
Throws:
NodeException

createOpNode

public OpNode createOpNode(Node right,
                           Node left)

createOpNode

public OpNode createOpNode(Node left)

getFlags

public int getFlags()

setFlag

public void setFlag(int flag)

getPresedence

public int getPresedence()

getCardinality

public int getCardinality()

clone

public java.lang.Object clone()
Clones the current Operator node. It preserves its properties. Operands are copied as a reference to the original and are not cloned.

Overrides:
clone in class java.lang.Object
Returns:
New copy of the operator node.

canOptimize

public boolean canOptimize()
Checks to see if both left and right operands can be optimized. The canOptimize() call is propagated down the Node Tree.

Specified by:
canOptimize in class Node
Returns:
boolean True if both left and right can be optimized, otherwise false.

optimize

public Node optimize()
If possible optimizes away its branches. If values are constant then this method calculates the final values and returns then new Node object to replace this OP object. The new object will be equal in value with the result of the operation. If either left or right operands can not be optimized then optimization is not performed. The method call to optimize() is propagated to both left and rigth operands. Each of which has a chance to optimize its side of the branch. Thus portions of the Node Tree can be optimized independantly. If both left and right can be optimized, checked by use of canOptimize() call, then a new Node is created to replace the this one.

Specified by:
optimize in class Node
Returns:
Node that the parent should replace appropriate operand with. This can either be the original node or a new Node. The parent can perform a check if the returned node is the same or new if neccessary.

getOpString

public java.lang.String getOpString()

equals

public boolean equals(java.lang.Object s)
Compares the name of the Operation with the given string. Each Operator has a name and this operation checks if the name is equal.

Overrides:
equals in class java.lang.Object
Parameters:
s - Object to compare against.
Returns:
true if equal, false if not.

createOpNode

public OpNode createOpNode(java.util.Stack operands)
                    throws OperandException,
                           ExpInternalException
Creates a new node using the factory pattern. New node is initialized using the operands from the stack. OP node is either unary or binary operand node (takes 1 or 2 elements from the stack). This depends on the value of cardinality (see getCardinatlity()).

Parameters:
operands - Stack containing operands.
Returns:
Newly created OpNode initialized.
Throws:
OperandException - Throws OperandException if there aren't enough operands on the stack.
ExpInternalException

getLeft

public Node getLeft()
Returns the left handside operand.

Returns:
Left operand.

getRight

public Node getRight()
Returns the right handside operand.

Returns:
Right operand.

setLeft

public void setLeft(Node node)
Sets the left handside operand of this operation.

Parameters:
left - Left operand for the operation.

setRight

public void setRight(Node node)
Sets the right handside operand of this operation.

Parameters:
right - Right operand for the operation.

set

public void set(Node newValue,
                Node oldValue)
         throws NodeException
Sets a new object for either left or right side of the Operation. Which side is determined by the oldValue. The new value will replace the old value in either left or right operand reference.

Parameters:
newValue - New operand value
oldValue - Old operand value which also determines where the new value will be set, right or left.
Throws:
NodeException - If oldValue can not be found in either left or right operand an exception is thrown. "oldValue" is used to determine which operand, left or right, should the newValue replace.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

main

public static void main(java.lang.String[] args)
Test function for OpNode

Parameters:
args - command line arguments