package rebelsky.compiler.pascal; import rebelsky.compiler.lexer.Token; /** * Unsigned reals for a simple Pascal tokenizer. * * @author Samuel A. Rebelsky * @version 1.1 of December 2002 */ public class PascalReal 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 PascalReal(float value) { super(PascalTokens.PREAL); this.value = value; this.original = Float.toString(value); } // PascalReal /** * Create a new real token based on a string. * * @exception NumberFormatException * If original cannot be parsed as a real. */ public PascalReal(String original) throws NumberFormatException { super(PascalTokens.PREAL); this.value = Float.parseFloat(original); this.original = original; } // PascalReal(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 Pascal real. Warning! Uses float comparison * which is notably unreliable. */ public boolean equals(PascalReal other) { return this.value == other.value; } // equals(PascalReal) } // class PascalReal