📄 environment.java
字号:
import java.util.*;
/** * This class implements a table containing all relevant data for parsing and
* execution. This includes declared variables, methods or constants which are
* used by the robot program. The table can both be used for representing the
* memory during execution and as a symbol table for type checking during
* parsing. <br>
* Valuenames are filed using a key which is of type String. Values are subtypes of
* EnvValue. <br>
*
* @see EnvValue */
public class Environment extends Hashtable
{
private Environment vSuperEnvironment;
Environment()
{
vSuperEnvironment= null;
}
Environment(Environment aEnvironment)
{
vSuperEnvironment= aEnvironment;
}
/**
* For the execution of a method a new environment is needed. This
* environment is not empty but contains the already known types, constants
* and user defined methods. The so called top level environment holds these
* values.
*
* @param env the top level environment
*/
void mSetSuperEnvironment(Environment env)
{
vSuperEnvironment = env;
}
/**
* Returns the top level environment.
*
* @return the top level environment
*
* @uml.property name="topLevelEnv"
*/
Environment mGetSuperEnvironment()
{
return vSuperEnvironment;
}
/**
* Checks, whether the given key exists.
*
* @param key
* to be checked
* @return true, if the key exists, false if not
*/
boolean exists(String key)
{
if(this.at(key).isNotFound())
if(vSuperEnvironment!= null)
return !vSuperEnvironment.at(key).isNotFound();
else return false;
else return true;
}
/**
* Returns the value for a given key.
*
* @param key
* the key to be searched for.
* @return the value for the given key or an instance of NotFoundValue
*/
EnvValue at(String key)
{
Object o = this.get(key);
if (o == null)
if(vSuperEnvironment!=null)return vSuperEnvironment.at(key);
else return new NotFoundValue();
return (EnvValue) o;
}
/**
* Updates the value of the given key with the given value.
*
* @param identifier
* key to be updated
* @param value
* new value for that key
* @throws InterpreterException
* is thrown when the key doesn't exist.
*/
void updateValue(String identifier, EnvValue value)
throws InterpreterException {
EnvValue currentValue = this.at(identifier);
if (currentValue.isNotFound())
AbstractSyntaxNode
.interpreterError("assignment attempt to undeclared variable "
+ identifier);
if (!currentValue.isValue())
AbstractSyntaxNode.interpreterError("can assign to variables only");
if (!value.isValue())
AbstractSyntaxNode
.interpreterError("can assign variable values only");
((Value) currentValue).assign((Value) value);
}
/**
* Creates a copy of the Environment, adds the given key-value pair and
* returns the new created and updated copy.
*
* @param key
* the key to be added
* @param value
* the associated value
* @return a new and updated Environment
*/
void add(String key, EnvValue value)
{
put(key, value);
}
/**
* Creates a copy of the Environment and adds the value-key pairs of the
* given Environment.
*
* @param env
* Environment containing the values to be added
* @return a new and updated Environment
*/
void addAll(Environment env)
{
Enumeration enumeration = env.keys();
while (enumeration.hasMoreElements())
{
Object key = enumeration.nextElement();
put(key, env.get(key));
}
}
/**
* Creates a shallow copy of the Environment. The reference
* to the top level environment is also copied. So the clone and
* the <code>this</code> environment have the same top level
* environment.
*
* @return a copy of the Environment
*/
public String toString()
{
Enumeration enumeration = keys();
String result = "[Environment-Variables:\n";
while (enumeration.hasMoreElements())
{
String key = (String) enumeration.nextElement();
result += key + " = " + at(key) + "\n";
}
return result + "]";
}
Environment mCreateNewEnvironment()
{
return new Environment(this);
}
Environment cloneEnvironment()
{
Environment result = new Environment();
result.addAll(this);
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -