package rebelsky.compiler.types;

/**
 * Representations of one-dimensional arrays with lower and upper bounds.
 *
 * These objects are intended to be static.
 *
 * @author Samuel A. Rebelsky
 * @version 1.0 of April 2004
 */
public class BoundedArray
  extends Type
{
  // +--------+------------------------------------------------------------
  // | Fields |
  // +--------+

  /** 
   * The index type. In simplified pascal, this will always be 
   * BasicTypes.INTEGER.  However, this class is designed to support
   * a more general one-dimensional array.
   */
  Type index;

  /**
   * The lower bound of the indices, represented as an integer.  (Every
   * value in an enumerated type has a corresponding integer, so we
   * use that one.)
   */
  int lb;

  /**
   * The upper bound of the indices, represented as an integer.
   */
  int ub;

  /**
   * The type of values stored in the array.
   */
  Type contents;


  // +--------------+------------------------------------------------------
  // | Constructors |
  // +--------------+

  /** 
   * Build a new type with particular index type, lower bound, upper bound,
   * and contents type.
   */
  public BoundedArray(Type index, int lb, int ub, Type contents)
  {
    super("array[" + lb + ".." + ub + "] of " + contents);
    this.index = index;
    this.lb = lb;
    this.ub = ub;
    this.contents = contents;
  } // BoundedArray(String)


  // +---------+-----------------------------------------------------------
  // | Methods |
  // +---------+

  /**
   * Determine the index type.
   */
  public Type indexType()
  {
    return index;
  } // indexType()

  /**
   * Determine  the contents type.
   */
  public Type contentsType()
  {
    return contents;
  } // contentsType()

  /**
   * Determine if an index is valid.
   */
  public boolean validIndex(int i)
  {
    return (lb <= i) && (i <= ub);
  } // validIndex(int)

} // class BoundedArray

