package rebelsky.pal;

/**
 * The PAL instruction to push a value.
 *
 * @author Samuel A. Rebelsky
 * @version 1.1 of December 2002.
 */
public class Push
  implements Instruction
{
  // +--------+------------------------------------------------------------
  // | Fields |
  // +--------+

  /** The container to push. */
  Variable location;


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

  /** 
   * Create a new instruction to push a location.
   */
  public Push(Variable location)
  {
    this.location = location;
  } // Push(Variable)


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

  /** 
   * Execute the instruction on a machine. 
   * 
   * @exception Exception
   *   If the memory location is invalid.
   *   If the stack is full or empty.
   */
  public void execute(Computer hal)
    throws Exception
  {
    if (hal.sp <= 0)
      throw new Exception("Stack Overflow");
    if (hal.sp > hal.memory.length)
      throw new Exception("Stack Underflow");
    hal.memory[--hal.sp] = location.iget(hal);
  } // execute(Computer)

  /** Convert the instruction to a string (usually for printing). */
  public String toString()
  {
    return "  PUSH <- " + location.toString();
  } // toString()

} // class Push

