package rebelsky.compiler.lexer;

/**
 * Unsigned reals for a simple tokenizer.
 *
 * @author Samuel A. Rebelsky
 * @version 1.0 of January 2004
 */
public class RealToken
  extends Token
{
  // +--------+------------------------------------------------------------
  // | Fields |
  // +--------+

  /** The value of the real. */
  float value;

  /** 
   * The string representation. Included because the value may be
   * slightly different in practice due to approximation that happens
   * in conversion to reals.
   */
  String original;


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

  /** 
   * Create a new real token with specified value.
   */
  public RealToken(float value) {
    super(Token.REAL);
    this.value = value;
    this.original = Float.toString(value);
  } // RealToken

  /**
   * Create a new real token based on a string.
   *
   * @exception NumberFormatException
   *   If original cannot be parsed as a real.
   */
  public RealToken(String original) 
    throws NumberFormatException
  {
    super(Token.REAL);
    this.value = Float.parseFloat(original);
     this.original = original;
  } // RealToken(int)

  
  // +-----------+---------------------------------------------------------
  // | Observers |
  // +-----------+

  /**
   * Get the value of this real.
   */
  public float getValue() {
    return this.value;
  } // getValue()

  /**
   * Convert to a string for printing.
   */
  public String toString() {
    return "REAL[" + this.original + "]";
  } // toString()

  /**
   * Compare to another real token.  Warning!  Uses float comparison
   * which is notably unreliable.
   */
  public boolean equals(RealToken other) {
    return this.value == other.value;
  } // equals(RealToken)

} // class RealToken

