KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > jjs > impl > JavaPrecedenceVisitor


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.jjs.impl;
17
18 import com.google.gwt.dev.jjs.InternalCompilerException;
19 import com.google.gwt.dev.jjs.ast.Context;
20 import com.google.gwt.dev.jjs.ast.JAbsentArrayDimension;
21 import com.google.gwt.dev.jjs.ast.JArrayRef;
22 import com.google.gwt.dev.jjs.ast.JBinaryOperation;
23 import com.google.gwt.dev.jjs.ast.JBooleanLiteral;
24 import com.google.gwt.dev.jjs.ast.JCastOperation;
25 import com.google.gwt.dev.jjs.ast.JCharLiteral;
26 import com.google.gwt.dev.jjs.ast.JClassLiteral;
27 import com.google.gwt.dev.jjs.ast.JConditional;
28 import com.google.gwt.dev.jjs.ast.JDoubleLiteral;
29 import com.google.gwt.dev.jjs.ast.JExpression;
30 import com.google.gwt.dev.jjs.ast.JFieldRef;
31 import com.google.gwt.dev.jjs.ast.JFloatLiteral;
32 import com.google.gwt.dev.jjs.ast.JInstanceOf;
33 import com.google.gwt.dev.jjs.ast.JIntLiteral;
34 import com.google.gwt.dev.jjs.ast.JLocalRef;
35 import com.google.gwt.dev.jjs.ast.JLongLiteral;
36 import com.google.gwt.dev.jjs.ast.JMethodCall;
37 import com.google.gwt.dev.jjs.ast.JNewArray;
38 import com.google.gwt.dev.jjs.ast.JNewInstance;
39 import com.google.gwt.dev.jjs.ast.JNullLiteral;
40 import com.google.gwt.dev.jjs.ast.JParameterRef;
41 import com.google.gwt.dev.jjs.ast.JPostfixOperation;
42 import com.google.gwt.dev.jjs.ast.JPrefixOperation;
43 import com.google.gwt.dev.jjs.ast.JStringLiteral;
44 import com.google.gwt.dev.jjs.ast.JThisRef;
45 import com.google.gwt.dev.jjs.ast.JVisitor;
46
47 /**
48  * See the Java Programming Language, 4th Edition, p. 750, Table 2. I just
49  * numbered the table top to bottom as 0 through 14. Lower number means higher
50  * precedence. I also gave primaries a precedence of 0; maybe I should have
51  * started operators at 1, but in practice it won't matter since primaries can't
52  * have children.
53  */

54 class JavaPrecedenceVisitor extends JVisitor {
55
56   public static int exec(JExpression expression) {
57     JavaPrecedenceVisitor visitor = new JavaPrecedenceVisitor();
58     visitor.accept(expression);
59     if (visitor.answer < 0) {
60       throw new InternalCompilerException("Precedence must be >= 0!");
61     }
62     return visitor.answer;
63   }
64
65   private int answer = -1;
66
67   private JavaPrecedenceVisitor() {
68   }
69
70   // @Override
71
public boolean visit(JAbsentArrayDimension x, Context ctx) {
72     answer = 0;
73     return false;
74   }
75
76   // @Override
77
public boolean visit(JArrayRef x, Context ctx) {
78     answer = 0;
79     return false;
80   }
81
82   // @Override
83
public boolean visit(JBinaryOperation operation, Context ctx) {
84     answer = operation.getOp().getPrecedence();
85     return false;
86   }
87
88   // @Override
89
public boolean visit(JBooleanLiteral x, Context ctx) {
90     answer = 0;
91     return false;
92   }
93
94   // @Override
95
public boolean visit(JCastOperation operation, Context ctx) {
96     answer = 2;
97     return false;
98   }
99
100   // @Override
101
public boolean visit(JCharLiteral x, Context ctx) {
102     answer = 0;
103     return false;
104   }
105
106   // @Override
107
public boolean visit(JClassLiteral x, Context ctx) {
108     answer = 0;
109     return false;
110   }
111
112   // @Override
113
public boolean visit(JConditional conditional, Context ctx) {
114     answer = 13;
115     return false;
116   }
117
118   // @Override
119
public boolean visit(JDoubleLiteral x, Context ctx) {
120     answer = 0;
121     return false;
122   }
123
124   // @Override
125
public boolean visit(JFieldRef x, Context ctx) {
126     answer = 0;
127     return false;
128   }
129
130   // @Override
131
public boolean visit(JFloatLiteral x, Context ctx) {
132     answer = 0;
133     return false;
134   }
135
136   // @Override
137
public boolean visit(JInstanceOf of, Context ctx) {
138     answer = 6;
139     return false;
140   }
141
142   // @Override
143
public boolean visit(JIntLiteral x, Context ctx) {
144     answer = 0;
145     return false;
146   }
147
148   // @Override
149
public boolean visit(JLocalRef x, Context ctx) {
150     answer = 0;
151     return false;
152   }
153
154   // @Override
155
public boolean visit(JLongLiteral x, Context ctx) {
156     answer = 0;
157     return false;
158   }
159
160   // @Override
161
public boolean visit(JMethodCall x, Context ctx) {
162     answer = 0;
163     return false;
164   }
165
166   // @Override
167
public boolean visit(JNewArray array, Context ctx) {
168     answer = 2;
169     return false;
170   }
171
172   // @Override
173
public boolean visit(JNewInstance instance, Context ctx) {
174     answer = 2;
175     return false;
176   }
177
178   // @Override
179
public boolean visit(JNullLiteral x, Context ctx) {
180     answer = 0;
181     return false;
182   }
183
184   // @Override
185
public boolean visit(JParameterRef x, Context ctx) {
186     answer = 0;
187     return false;
188   }
189
190   // @Override
191
public boolean visit(JPostfixOperation operation, Context ctx) {
192     answer = 0;
193     return false;
194   }
195
196   // @Override
197
public boolean visit(JPrefixOperation operation, Context ctx) {
198     answer = 1;
199     return false;
200   }
201
202   // @Override
203
public boolean visit(JStringLiteral x, Context ctx) {
204     answer = 0;
205     return false;
206   }
207
208   // @Override
209
public boolean visit(JThisRef x, Context ctx) {
210     answer = 0;
211     return false;
212   }
213
214 }
Popular Tags