scphEditor
Class Expression

java.lang.Object
  extended by java.util.Observable
      extended by scphEditor.CloneableObservable
          extended by scphEditor.OperandVector
              extended by scphEditor.Operand
                  extended by scphEditor.Expression
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable
Direct Known Subclasses:
ExpressionNary

public class Expression
extends Operand

This class represents compound expressions. A compound expression has an operator and a list of subexpressions, i.e., children, each child being an Operand object. E.g., -x is modelled as an Expression object, with the unary - operator and the child x.

The arity of an expression is the same as that of its operator; in case the operator is n-ary, though, the expression becomes binary. Use the subclass ExpressionNary instead if you want true n-ary services. For example, 1 + 2 + 3 is better modelled by an ExpressionNary object.

The update() method of an Observer receives an Expression.Replacement object as a parameter.

See Also:
IOperator, ExpressionNary, Serialized Form

Nested Class Summary
 class Expression.Replacement
          A Replacement object represents a substitutional change made to an Expression object.
 
Field Summary
protected  java.util.Vector children
          List of child operands.
protected  IOperator op
          [Rooting] Operator in this expression.
protected  boolean paren
          True iff this operand is parenthesized.
protected  boolean splitted
          True iff the view should split this expression across lines.
 
Constructor Summary
Expression(IOperator opOf)
          Constructs an Expression object with the given operator.
 
Method Summary
 void acceptVisitor(OperandVisitor v)
          Calls v.visitExpression(this).
 void addChild(Operand newChild, int atIndex)
          This implementation does not do anything.
 void addParen()
          Parenthesizes me.
 java.lang.Object clone()
          Clones this expression.
 int countChildren()
          Gives the actual, present number of my children.
 Operand delChild(int atIndex)
          This implementation returns null and does not do anything else.
protected  void deParen()
          De-parenthesizes me.
 Operand getChild(int atIndex)
          Gives my child at the given index.
 java.util.Enumeration getChildren()
          Enumerates all of my children, in ascending order of indexes.
 IOperator getOperator()
          Gives the operator of this expression.
 boolean getParen()
          Tells if I am already parenthesized.
 boolean getSplit()
          Gets my split status.
 void setChild(Operand newChild, int atIndex)
          Adopts the given new child as my child at the given index, replacing the previous child at the same index.
 void setSplit(boolean split)
          Sets my split status.
 
Methods inherited from class scphEditor.Operand
ensureParen, needParen, removeParen
 
Methods inherited from class scphEditor.CloneableObservable
addObserver, cloneObserverList, cloneObserverList, countObservers, deleteObserver, deleteObservers, notifyObservers, notifyObservers
 
Methods inherited from class java.util.Observable
clearChanged, hasChanged, setChanged
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

op

protected IOperator op
[Rooting] Operator in this expression.


children

protected java.util.Vector children
List of child operands.


paren

protected boolean paren
True iff this operand is parenthesized.


splitted

protected boolean splitted
True iff the view should split this expression across lines.

Constructor Detail

Expression

public Expression(IOperator opOf)
Constructs an Expression object with the given operator. The given operator will become the operator of this compound expression permanently. As many children as the operator arity specifies are set up, and initialized to null. In the case of an n-ary operator, 2 children are set up.

Parameters:
opOf - the operator object for this Expression object.
Method Detail

clone

public java.lang.Object clone()
Clones this expression.

Overrides:
clone in class Operand
Returns:
a clone of this expression.

getOperator

public IOperator getOperator()
Gives the operator of this expression.

Specified by:
getOperator in class Operand
Returns:
the Operator object of this expression.
See Also:
IOperator

setChild

public void setChild(Operand newChild,
                     int atIndex)
Description copied from class: OperandVector
Adopts the given new child as my child at the given index, replacing the previous child at the same index.

This default implementation is for a leaf node in which there must be no child, so it always throws an exception. A compound expression must re-implement this method sensibly.

Overrides:
setChild in class OperandVector
Parameters:
newChild - the new child
atIndex - the index to where newChild goes

getChild

public Operand getChild(int atIndex)
Gives my child at the given index.

Overrides:
getChild in class OperandVector
Parameters:
atIndex - the index at where a child will be sought.
Returns:
the child at the index.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if atIndex is an invalid index, i.e., atIndex violates my arity.

addChild

public void addChild(Operand newChild,
                     int atIndex)
This implementation does not do anything.

Overrides:
addChild in class OperandVector

delChild

public Operand delChild(int atIndex)
This implementation returns null and does not do anything else.

Overrides:
delChild in class OperandVector

getChildren

public java.util.Enumeration getChildren()
Enumerates all of my children, in ascending order of indexes.

Overrides:
getChildren in class OperandVector
Returns:
an Enumeration that iterates over my children in ascending order of indexes.

countChildren

public int countChildren()
Description copied from class: OperandVector
Gives the actual, present number of my children.

This default implementation returns zero, as a leaf node has no child.

Overrides:
countChildren in class OperandVector
Returns:
the present number of my children. (Zero for leaf nodes.)

addParen

public void addParen()
Description copied from class: Operand
Parenthesizes me.

The default implementation is for leaf nodes and does nothing. Compound expressions should override this properly.

Overrides:
addParen in class Operand

deParen

protected void deParen()
Description copied from class: Operand
De-parenthesizes me. This method should only be called by internal code such as removeParen().

The default implementation is for leaf nodes and does nothing. Compound expressions should override this properly.

Overrides:
deParen in class Operand
See Also:
Operand.removeParen(scphEditor.OperandVector)

getParen

public boolean getParen()
Description copied from class: Operand
Tells if I am already parenthesized.

This default implementation is for leaf nodes. Compound expressions should override this properly.

Overrides:
getParen in class Operand
Returns:
true iff I am parenthesized. (False for leaf nodes.)

getSplit

public boolean getSplit()
Description copied from class: Operand
Gets my split status. In other words, whether or not I should be splitted across the line when displayed by a view, like, b = a becomes:
   b
 = a
 
Of course, this is just a hint to the view.

This default implementation is for leaf nodes, which never splits. Compound expressions should override this properly.

Overrides:
getSplit in class Operand
Returns:
true iff I should be displayed as splitted. (False for leaf nodes.)

setSplit

public void setSplit(boolean split)
Description copied from class: Operand
Sets my split status.

This default implementation is for leaf nodes, which never splits. Compound expressions should override this properly.

Overrides:
setSplit in class Operand
Parameters:
split - true if you want me to split, false otherwise.

acceptVisitor

public void acceptVisitor(OperandVisitor v)
Calls v.visitExpression(this).

Specified by:
acceptVisitor in class Operand
Parameters:
v - the visitor object that wants to visit this operand.