001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.dev.jjs.impl;
017:
018: import com.google.gwt.dev.jjs.InternalCompilerException;
019: import com.google.gwt.dev.jjs.ast.Context;
020: import com.google.gwt.dev.jjs.ast.JAbsentArrayDimension;
021: import com.google.gwt.dev.jjs.ast.JArrayRef;
022: import com.google.gwt.dev.jjs.ast.JBinaryOperation;
023: import com.google.gwt.dev.jjs.ast.JBooleanLiteral;
024: import com.google.gwt.dev.jjs.ast.JCastOperation;
025: import com.google.gwt.dev.jjs.ast.JCharLiteral;
026: import com.google.gwt.dev.jjs.ast.JClassLiteral;
027: import com.google.gwt.dev.jjs.ast.JConditional;
028: import com.google.gwt.dev.jjs.ast.JDoubleLiteral;
029: import com.google.gwt.dev.jjs.ast.JExpression;
030: import com.google.gwt.dev.jjs.ast.JFieldRef;
031: import com.google.gwt.dev.jjs.ast.JFloatLiteral;
032: import com.google.gwt.dev.jjs.ast.JInstanceOf;
033: import com.google.gwt.dev.jjs.ast.JIntLiteral;
034: import com.google.gwt.dev.jjs.ast.JLocalRef;
035: import com.google.gwt.dev.jjs.ast.JLongLiteral;
036: import com.google.gwt.dev.jjs.ast.JMethodCall;
037: import com.google.gwt.dev.jjs.ast.JNewArray;
038: import com.google.gwt.dev.jjs.ast.JNewInstance;
039: import com.google.gwt.dev.jjs.ast.JNullLiteral;
040: import com.google.gwt.dev.jjs.ast.JParameterRef;
041: import com.google.gwt.dev.jjs.ast.JPostfixOperation;
042: import com.google.gwt.dev.jjs.ast.JPrefixOperation;
043: import com.google.gwt.dev.jjs.ast.JStringLiteral;
044: import com.google.gwt.dev.jjs.ast.JThisRef;
045: import com.google.gwt.dev.jjs.ast.JVisitor;
046:
047: /**
048: * See the Java Programming Language, 4th Edition, p. 750, Table 2. I just
049: * numbered the table top to bottom as 0 through 14. Lower number means higher
050: * precedence. I also gave primaries a precedence of 0; maybe I should have
051: * started operators at 1, but in practice it won't matter since primaries can't
052: * have children.
053: */
054: class JavaPrecedenceVisitor extends JVisitor {
055:
056: public static int exec(JExpression expression) {
057: JavaPrecedenceVisitor visitor = new JavaPrecedenceVisitor();
058: visitor.accept(expression);
059: if (visitor.answer < 0) {
060: throw new InternalCompilerException(
061: "Precedence must be >= 0!");
062: }
063: return visitor.answer;
064: }
065:
066: private int answer = -1;
067:
068: private JavaPrecedenceVisitor() {
069: }
070:
071: // @Override
072: public boolean visit(JAbsentArrayDimension x, Context ctx) {
073: answer = 0;
074: return false;
075: }
076:
077: // @Override
078: public boolean visit(JArrayRef x, Context ctx) {
079: answer = 0;
080: return false;
081: }
082:
083: // @Override
084: public boolean visit(JBinaryOperation operation, Context ctx) {
085: answer = operation.getOp().getPrecedence();
086: return false;
087: }
088:
089: // @Override
090: public boolean visit(JBooleanLiteral x, Context ctx) {
091: answer = 0;
092: return false;
093: }
094:
095: // @Override
096: public boolean visit(JCastOperation operation, Context ctx) {
097: answer = 2;
098: return false;
099: }
100:
101: // @Override
102: public boolean visit(JCharLiteral x, Context ctx) {
103: answer = 0;
104: return false;
105: }
106:
107: // @Override
108: public boolean visit(JClassLiteral x, Context ctx) {
109: answer = 0;
110: return false;
111: }
112:
113: // @Override
114: public boolean visit(JConditional conditional, Context ctx) {
115: answer = 13;
116: return false;
117: }
118:
119: // @Override
120: public boolean visit(JDoubleLiteral x, Context ctx) {
121: answer = 0;
122: return false;
123: }
124:
125: // @Override
126: public boolean visit(JFieldRef x, Context ctx) {
127: answer = 0;
128: return false;
129: }
130:
131: // @Override
132: public boolean visit(JFloatLiteral x, Context ctx) {
133: answer = 0;
134: return false;
135: }
136:
137: // @Override
138: public boolean visit(JInstanceOf of, Context ctx) {
139: answer = 6;
140: return false;
141: }
142:
143: // @Override
144: public boolean visit(JIntLiteral x, Context ctx) {
145: answer = 0;
146: return false;
147: }
148:
149: // @Override
150: public boolean visit(JLocalRef x, Context ctx) {
151: answer = 0;
152: return false;
153: }
154:
155: // @Override
156: public boolean visit(JLongLiteral x, Context ctx) {
157: answer = 0;
158: return false;
159: }
160:
161: // @Override
162: public boolean visit(JMethodCall x, Context ctx) {
163: answer = 0;
164: return false;
165: }
166:
167: // @Override
168: public boolean visit(JNewArray array, Context ctx) {
169: answer = 2;
170: return false;
171: }
172:
173: // @Override
174: public boolean visit(JNewInstance instance, Context ctx) {
175: answer = 2;
176: return false;
177: }
178:
179: // @Override
180: public boolean visit(JNullLiteral x, Context ctx) {
181: answer = 0;
182: return false;
183: }
184:
185: // @Override
186: public boolean visit(JParameterRef x, Context ctx) {
187: answer = 0;
188: return false;
189: }
190:
191: // @Override
192: public boolean visit(JPostfixOperation operation, Context ctx) {
193: answer = 0;
194: return false;
195: }
196:
197: // @Override
198: public boolean visit(JPrefixOperation operation, Context ctx) {
199: answer = 1;
200: return false;
201: }
202:
203: // @Override
204: public boolean visit(JStringLiteral x, Context ctx) {
205: answer = 0;
206: return false;
207: }
208:
209: // @Override
210: public boolean visit(JThisRef x, Context ctx) {
211: answer = 0;
212: return false;
213: }
214:
215: }
|