package edu.princeton.toy.lang; /** * TExceptionType describes the types of TException that can be thrown. Like TWords, instances of * TExceptionType are immutable and are to be reused. * * @author btsang * @version 7.1 */ public class TExceptionType { /** * A string array containing the names of the families. */ public static final String FAMILIES[] = { "Uninitialized Exceptions", "Overflow Exceptions", "Out of Bounds Exceptions" }; /** * The identifier of the the family of uninitialized TExceptionTypes. This includes * REGISTER_UNINITIALIZED, MEMORY_UNINITIALIZED, and COMMAND_UNINITIALIZED. * * @see #REGISTER_UNINITIALIZED * @see #MEMORY_UNINITIALIZED * @see #COMMAND_UNINITIALIZED */ public static final int UNINITIALIZED_FAMILY = 0; /** * The identifier of the the family of overflow TExceptionTypes. This includes only OVERFLOW. * * @see #OVERFLOW */ public static final int OVERFLOW_FAMILY = 1; /** * The identifier of the the family of overflow TExceptionTypes. This includes * SHIFT_OUT_OF_BOUNDS, PC_OUT_OF_BOUNDS, REGISTER_OUT_OF_BOUNDS, and MEM_OUT_OF_BOUNDS. * * @see #SHIFT_OUT_OF_BOUNDS * @see #PC_OUT_OF_BOUNDS * @see #REGISTER_OUT_OF_BOUNDS * @see #MEM_OUT_OF_BOUNDS */ public static final int OUT_OF_BOUNDS_FAMILY = 2; /** * The type of exception that occurs when the value of an uninitialized register * is accessed by an operation. */ public static final TExceptionType REGISTER_UNINITIALIZED = new TExceptionType( 0, "Register Uninitialized Exception", UNINITIALIZED_FAMILY, "The register that an operation referenced has not been defined." ); /** * The type of exception that occurs when the value of an uninitialized memory memory * is accessed by an operation. */ public static final TExceptionType MEMORY_UNINITIALIZED = new TExceptionType( 1, "Memory Uninitialized Exception", UNINITIALIZED_FAMILY, "The memory memory that a load operation referenced has not been defined." ); /** * The type of exception that occurs when the fetch-execute loop attempts to execute the command * in an uninitialized memory memory. */ public static final TExceptionType COMMAND_UNINITIALIZED = new TExceptionType( 2, "Command Uninitialized Exception", UNINITIALIZED_FAMILY, "The memory memory to which the program counter points has not been defined." ); /** * The type of exception that occurs when the result of the operation is not representable as a * 16-bit two's complement signed integers. */ public static final TExceptionType OVERFLOW = new TExceptionType( 3, "Overflow Exception", OVERFLOW_FAMILY, "The result of an arithmatic operation was not between between -32768 and 32767." ); /** * The type of exception that occurs when a shift operation is commanded to shift further than * 000F positions. */ public static final TExceptionType SHIFT_OUT_OF_BOUNDS = new TExceptionType( 4, "Shift Magnitude Out Of Bounds Exception", OUT_OF_BOUNDS_FAMILY, "The shift operator was used with an invalid shift magnitude; shift magnitudes must be " + "in the closed interval [0000, 000F]." ); /** * The type of exception that occurs when the program counter exceeds FF. */ public static final TExceptionType PC_OUT_OF_BOUNDS = new TExceptionType( 5, "Program Counter Out Of Bounds Exception", OUT_OF_BOUNDS_FAMILY, "The execution or increment phase attempted to assign an invalid value to the " + "program counter; memory locations range from 00 to FF (inclusive)." ); /** * The type of exception that occurs when an operator attempts to access a register that is not * valid (attempting to assign a value to R[0]). */ public static final TExceptionType REGISTER_OUT_OF_BOUNDS = new TExceptionType( 6, "Register Index Out Of Bounds Exception", OUT_OF_BOUNDS_FAMILY, "The execution cycle attempted to change R[0], which is a constant." ); /** * The type of exception that occurs when an operator attempts to access a memory memory that is * not between 0000 and 00FF. */ public static final TExceptionType MEM_OUT_OF_BOUNDS = new TExceptionType( 7, "Memory Address Out Of Bounds Exception", OUT_OF_BOUNDS_FAMILY, "The execution cycle attempted to load from or store to an invalid memory location; " + "memory locations range from 00 to FF (inclusive)." ); /** * An array of all the TExceptionTypes, in order of hash-code. */ public static final TExceptionType[] TYPES = { REGISTER_UNINITIALIZED, MEMORY_UNINITIALIZED, COMMAND_UNINITIALIZED, OVERFLOW, SHIFT_OUT_OF_BOUNDS, PC_OUT_OF_BOUNDS, REGISTER_OUT_OF_BOUNDS, MEM_OUT_OF_BOUNDS }; private int hashCode; private String name; private int family; private String description; /** * Constructs a new instance of TExceptionType. */ private TExceptionType(int hashCode, String name, int family, String description) { this.hashCode = hashCode; this.name = name; this.family = family; this.description = description; } /** * Returns the hash-code of this TExceptionType. * * @return The hash-code of this TExceptionType. Hash codes are guaranteed to be different * for different TExceptionTypes. Moreover, the hash codes are gauranteed to have the property * that TYPES[n].hashCode() == n. */ public int hashCode() { return hashCode; } /** * Returns the name of this TExceptionType. * * @return The name of this TExceptionType. */ public String getName() { return name; } /** * Returns the family of this TExceptionType. * * @return The family of this TExceptionType. This should be one of UNINITIALIZED_FAMILY, * OVERFLOW_FAMILY, and OUT_OF_BOUNDS_FAMILY. */ public int getFamily() { return family; } /** * Returns the description of this TExceptionType. * * @return The description of this TExceptionType. */ public String getDescription() { return description; } /** * Returns the name of this TExceptionType. * * @return The name of this TExceptionType. */ public String toString() { return name; } }