KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > ext > jl > ast > Expr_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  * An <code>Expr</code> represents any Java expression. All expressions
10  * must be subtypes of Expr.
11  */

12 public abstract class Expr_c extends Term_c implements Expr
13 {
14     protected Type type;
15
16     public Expr_c(Position pos) {
17     super(pos);
18     }
19
20     /**
21      * Get the type of the expression. This may return an
22      * <code>UnknownType</code> before type-checking, but should return the
23      * correct type after type-checking.
24      */

25     public Type type() {
26     return this.type;
27     }
28
29     /** Set the type of the expression. */
30     public Expr type(Type type) {
31         if (type == this.type) return this;
32     Expr_c n = (Expr_c) copy();
33     n.type = type;
34     return n;
35     }
36
37     public void dump(CodeWriter w) {
38         super.dump(w);
39
40     if (type != null) {
41         w.allowBreak(4, " ");
42         w.begin(0);
43         w.write("(type " + type + ")");
44         w.end();
45     }
46     }
47
48     /** Get the precedence of the expression. */
49     public Precedence precedence() {
50     return Precedence.UNKNOWN;
51     }
52
53     public boolean isConstant() {
54         return false;
55     }
56
57     public Object JavaDoc constantValue() {
58         return null;
59     }
60
61     public String JavaDoc stringValue() {
62         return (String JavaDoc) constantValue();
63     }
64
65     public boolean booleanValue() {
66         return ((Boolean JavaDoc) constantValue()).booleanValue();
67     }
68
69     public byte byteValue() {
70         return ((Byte JavaDoc) constantValue()).byteValue();
71     }
72
73     public short shortValue() {
74         return ((Short JavaDoc) constantValue()).shortValue();
75     }
76
77     public char charValue() {
78         return ((Character JavaDoc) constantValue()).charValue();
79     }
80
81     public int intValue() {
82         return ((Integer JavaDoc) constantValue()).intValue();
83     }
84
85     public long longValue() {
86         return ((Long JavaDoc) constantValue()).longValue();
87     }
88
89     public float floatValue() {
90         return ((Float JavaDoc) constantValue()).floatValue();
91     }
92
93     public double doubleValue() {
94         return ((Double JavaDoc) constantValue()).doubleValue();
95     }
96
97     public Node buildTypes(TypeBuilder tb) throws SemanticException {
98         return type(tb.typeSystem().unknownType(position()));
99     }
100
101     /**
102      * Correctly parenthesize the subexpression <code>expr<code> given
103      * the its precendence and the precedence of the current expression.
104      *
105      * If the sub-expression has the same precedence as this expression
106      * we do not parenthesize.
107      *
108      * @param expr The subexpression.
109      * (right-) associative operator.
110      * @param w The output writer.
111      * @param pp The pretty printer.
112      */

113     public void printSubExpr(Expr expr, CodeWriter w, PrettyPrinter pp) {
114         printSubExpr(expr, true, w, pp);
115     }
116
117     /**
118      * Correctly parenthesize the subexpression <code>expr<code> given
119      * the its precendence and the precedence of the current expression.
120      *
121      * If the sub-expression has the same precedence as this expression
122      * we parenthesize if the sub-expression does not associate; e.g.,
123      * we parenthesis the right sub-expression of a left-associative
124      * operator.
125      *
126      * @param expr The subexpression.
127      * @param associative Whether expr is the left (right) child of a left-
128      * (right-) associative operator.
129      * @param w The output writer.
130      * @param pp The pretty printer.
131      */

132     public void printSubExpr(Expr expr, boolean associative,
133                              CodeWriter w, PrettyPrinter pp) {
134         if (! associative && precedence().equals(expr.precedence()) ||
135         precedence().isTighter(expr.precedence())) {
136         w.write("(");
137             printBlock(expr, w, pp);
138         w.write( ")");
139     }
140         else {
141             printBlock(expr, w, pp);
142         }
143     }
144 }
145
Popular Tags