All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class JavaAgent.resource.fopl.Variable

java.lang.Object
   |
   +----JavaAgent.resource.fopl.Variable

public class Variable
extends Object
This class implements symbolic variables in Java. Like Symbols, Variables are memory-unique objects, i.e. they are equal if and only if the handles are the same. This allows the creation of multiple variables with the same name that are not equal. It also allows for efficient equality testing. The unique index could also be used to compare variables.

The class also maintains some aids that should facilitate parsing. A set of global and a set of local Variables are used by the function get(). The variable newLocal determines what get() will return. The function newVars() dismisses all local and global variables, i.e. future calls to get() cannot return them.


Variable Index

 o newLocal
This Java variable determines how get() looks for the symbolic Variable to be returned.
 o printWithIndex
This Java variable indicates how symbolic Variables are to be printed.
 o theName
the name of this Variable
 o theType
the Symbol that is the type of this Variable (by default null)

Constructor Index

 o Variable(String)
It is allowed to create Variables directly with this constructor.

Method Index

 o clone()
Cloning a Variable returns a new Variable with the same name and type but with a different index.
 o equals(Object)
This function tests for variable equality.
 o get(String)
This function must be used to get hold of Variables with the given name.
 o get(String, Vector)
This function returns the first Variable from the given Vector of Variables that has the given String as its name (ignoring the index).
 o getName()
This function returns the name of this Variable.
 o getType()
This function returns the Symbol that represents the type of this Variable.
 o hashCode()
The hashCode() of a Variable is the unique index of this variable.
 o newVars()
This function removes all Variables from the static memory.
 o removeLocals(int)
This function can be used to remove the last n local Variables from the stack.
 o setType(Symbol)
This function sets the type of the variable to the given Symbol (or null).
 o toString()
A Variable is printed as the String that is its name and its index if printWithIndex is true.
 o useVariable(Variable)
It is possible to reuse Variables with this function.

Variables

 o newLocal
 public static boolean newLocal
This Java variable determines how get() looks for the symbolic Variable to be returned. If false (the default), an existing Variable is searched for. If set to true, the Variable will always be created as a new local Variable.

 o printWithIndex
 public static boolean printWithIndex
This Java variable indicates how symbolic Variables are to be printed. If true, the default, symbolic Variables are printed with their index. Printing Variables without index is useful when the expression they occur in is to be parsed again. This will avoid multiple indices but may lead to ambiguities.

 o theName
 protected String theName
the name of this Variable

 o theType
 protected Symbol theType
the Symbol that is the type of this Variable (by default null)

Constructors

 o Variable
 protected Variable(String aName) throws IllegalArgumentException
It is allowed to create Variables directly with this constructor. However, the static function get() should normally be used. A Variable created with the constructor will not be stored as global or local.

Parameters:
aName - the name of the Variable
Throws: IllegalArgumentException
An exception will occur if the supplied String is empty or null.
Throws: OutOfMemoryError
An error can occur if the unique indices for Variables have run out.

Methods

 o get
 public static Variable get(String aName) throws IllegalArgumentException
This function must be used to get hold of Variables with the given name. It effectively replaces a public constructor. For example,

Variable myVar = Variable.get("?x");

will return a Variable with the given name. The '?' in the variable name is not necessary. If the Variable has been defined previously the same Variable will be returned (unless newVars() has been called). The function first searches the variable stack (local variables) and then the global variables. If the given name isn't found in either, the Variable is created as a new global. This behaviour can be changed by setting the static varibale newLocal to true. In this case a new local Variable will be created no matter whether one with this name existed.

Parameters:
aName - the name of the Variable
Returns:
a Variable with the given name (see above)
Throws: IllegalArgumentException
An exception will occur if the supplied String is empty or null.
Throws: OutOfMemoryError
An error can occur if the unique indices for Variables have run out.
 o clone
 public Object clone()
Cloning a Variable returns a new Variable with the same name and type but with a different index. The new Variable will not be equal to the original (using the equals() test).

Returns:
an (unequal) copy of this Variable
Throws: OutOfMemoryError
An error can occur if the unique indices for Variables have run out.
Overrides:
clone in class Object
 o newVars
 public static void newVars()
This function removes all Variables from the static memory. Future calls to get() (or the constructor) will return new Variables even if the given names have been used before.

 o removeLocals
 public static void removeLocals(int n) throws IllegalArgumentException
This function can be used to remove the last n local Variables from the stack. Subsequent calls to get() which could have otherwise returned one of these Variables will not find them anymore and thus, look for either previously defined local or global Variables that might have been hidden by the Variables now removed here.

Parameters:
n - the number of variables to be removed from the stack
Throws: IllegalArgumentException
An exception will occur if the number of local Variables currently in the static stack is less than the given number.
 o setType
 public void setType(Symbol aSy)
This function sets the type of the variable to the given Symbol (or null). If the Variable already had a different type then this will be overwritten.

Parameters:
aSy - the (new) type of this Variable
 o getType
 public Symbol getType()
This function returns the Symbol that represents the type of this Variable. It may be null if the type was undefined.

Returns:
the type of this Variable
 o equals
 public boolean equals(Object otherVar)
This function tests for variable equality. Two Variables are considered equal only if they are the same object in memory.

Parameters:
otherVar - the Object this one is compared to
Returns:
whether the other Object is the same as this
Overrides:
equals in class Object
 o getName
 public String getName()
This function returns the name of this Variable.

Returns:
the name of this Variable without the index
 o useVariable
 public static void useVariable(Variable var)
It is possible to reuse Variables with this function. The given Variable is stored as a global Variable and should a Variable of that name be seeked with get(String) then this Variable will be returned.

Parameters:
var - the Variable to be used as global
 o toString
 public String toString()
A Variable is printed as the String that is its name and its index if printWithIndex is true.

Returns:
the print-name of this Variable (depends on printWithIndex)
Overrides:
toString in class Object
 o hashCode
 public int hashCode()
The hashCode() of a Variable is the unique index of this variable.

Returns:
the index of this Variable
Overrides:
hashCode in class Object
 o get
 public static Variable get(String name,
                            Vector vars)
This function returns the first Variable from the given Vector of Variables that has the given String as its name (ignoring the index). If no such Variable exists the function returns null.

Parameters:
name - the name of the seeked Variable
vars - the Vector of Variables
Returns:
the first Variable from the given Vector with the given name

All Packages  Class Hierarchy  This Package  Previous  Next  Index