tenua.symbol
Class Expression

java.lang.Object
  extended by tenua.symbol.Expression

public class Expression
extends java.lang.Object

A sequence of Symbols that are evaluated in order, like an RPN calculator


Constructor Summary
Expression()
          create a new empty Expression
Expression(Symbol s)
          create a new Expression that contains a single Symbol
 
Method Summary
 Expression add(Expression e)
          add all the contents of an existing Expression to the end of this
 Expression add(Symbol s)
          add a Symbol to the end of this
 Vec eval(VariableMemento v)
          evaluate this
 Vec eval(Vec d, VariableMemento v)
          evaluate this
 void eval(Vec d, Vec result, VariableMemento v)
          evaluate this
 Assignment getAssignment(SymbolTable st)
          returns an Assignment representing an assignment operation to this.
 boolean isConstant()
          tests whether an expression is constant (no variables)
 Expression prepend(Expression e)
          insert all the contents of an existing Expression to the start of this
 Expression prepend(Symbol s)
          insert a Symbol to the start of this
 java.lang.String toString()
          produces an RPN string, with variables listed as Variable[n].
 java.lang.String toString(SymbolTable st)
          produces a String representation of this expression.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Expression

public Expression()
create a new empty Expression


Expression

public Expression(Symbol s)
create a new Expression that contains a single Symbol

Method Detail

add

public Expression add(Symbol s)
add a Symbol to the end of this

Parameters:
s - the Symbol to add
Returns:
this

add

public Expression add(Expression e)
add all the contents of an existing Expression to the end of this

Parameters:
e - the Expression to add to this
Returns:
this

prepend

public Expression prepend(Symbol s)
insert a Symbol to the start of this

Parameters:
s - the Symbol to add
Returns:
this

prepend

public Expression prepend(Expression e)
insert all the contents of an existing Expression to the start of this

Parameters:
e - the Expression to add
Returns:
this

getAssignment

public Assignment getAssignment(SymbolTable st)
returns an Assignment representing an assignment operation to this. valid only if this is a single Symbol which is a variable

Parameters:
st - the SymbolTable to use for error messages (using null will use cryptic names (like "Variable[0]") but thhe method will work
Returns:
the Assignment created
Throws:
UnexpectedSymbolException - if this cannot be assigned to

eval

public Vec eval(VariableMemento v)
evaluate this

Parameters:
v - the memento representing the values of the variables. if v == null, use the default values
Returns:
a copy of the values from the stack

eval

public Vec eval(Vec d,
                VariableMemento v)
evaluate this

Parameters:
d - the array of values to push onto the stack before evaluating. Pushed in reverse order (the stack is created with DoubleStack.DoubleStack(nr.Vec).
v - the memento representing the values of the variables. if v == null, use the default values
Returns:
a copy of the values from the stack

eval

public void eval(Vec d,
                 Vec result,
                 VariableMemento v)
evaluate this

Parameters:
d - the array of values to push onto the stack before evaluating. Pushed in reverse order (the stack is created with DoubleStack.DoubleStack(nr.Vec).
result - a copy of the values from the stack after evaluation
v - the memento representing the values of the variables. if v == null, use the default values

toString

public java.lang.String toString()
produces an RPN string, with variables listed as Variable[n]. for debugging; not for showing to users

Overrides:
toString in class java.lang.Object
Returns:
the String produced

toString

public java.lang.String toString(SymbolTable st)
produces a String representation of this expression. Uses the names from a SymbolTable. parenthesizes only what is necessary by the rules of precedence. This is rather kludgy, but it seems to work

Parameters:
st - the SymbolTable to use. If null, uses toString()
Returns:
the String produced

isConstant

public boolean isConstant()
tests whether an expression is constant (no variables)

Returns:
true if this is constant, false if not.