KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > ext > jl > ast > FloatLit_c


1 package polyglot.ext.jl.ast;
2
3 import polyglot.ast.*;
4 import polyglot.types.*;
5 import polyglot.visit.*;
6 import polyglot.util.*;
7
8 /**
9  * A <code>FloatLit</code> represents a literal in java of type
10  * <code>float</code> or <code>double</code>.
11  */

12 public class FloatLit_c extends Lit_c implements FloatLit
13 {
14     protected FloatLit.Kind kind;
15     protected double value;
16
17     public FloatLit_c(Position pos, FloatLit.Kind kind, double value) {
18     super(pos);
19     this.kind = kind;
20     this.value = value;
21     }
22
23     /** Get the kind of the literal. */
24     public FloatLit.Kind kind() {
25     return this.kind;
26     }
27
28     /** Set the kind of the literal. */
29     public FloatLit kind(FloatLit.Kind kind) {
30     FloatLit_c n = (FloatLit_c) copy();
31     n.kind = kind;
32     return n;
33     }
34
35     /** Get the value of the expression. */
36     public double value() {
37     return this.value;
38     }
39
40     /** Set the value of the expression. */
41     public FloatLit value(double value) {
42     FloatLit_c n = (FloatLit_c) copy();
43     n.value = value;
44     return n;
45     }
46
47     /** Type check the expression. */
48     public Node typeCheck(TypeChecker tc) throws SemanticException {
49     if (kind == FLOAT) {
50         return type(tc.typeSystem().Float());
51     }
52     else if (kind == DOUBLE) {
53         return type(tc.typeSystem().Double());
54     }
55     else {
56         throw new InternalCompilerError("Unrecognized FloatLit kind " +
57         kind);
58     }
59     }
60
61     public String JavaDoc toString() {
62     return Double.toString(value);
63     }
64
65     /** Write the expression to an output file. */
66     public void prettyPrint(CodeWriter w, PrettyPrinter tr) {
67         if (kind == FLOAT) {
68         w.write(Float.toString((float) value) + "F");
69     }
70     else if (kind == DOUBLE) {
71         w.write(Double.toString(value));
72     }
73     else {
74         throw new InternalCompilerError("Unrecognized FloatLit kind " +
75         kind);
76     }
77     }
78
79     public Object JavaDoc constantValue() {
80       if (kind == FLOAT) {
81         return new Float JavaDoc(value);
82       }
83       else {
84         return new Double JavaDoc(value);
85       }
86     }
87
88     public Precedence precedence() {
89         if (value < 0) {
90             return Precedence.UNARY;
91         }
92         else {
93             return Precedence.LITERAL;
94         }
95     }
96 }
97
Popular Tags