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.ast;
017:
018: /**
019: * For precedence indices, see the Java Programming Language, 4th Edition, p.
020: * 750, Table 2. I just numbered the table top to bottom as 0 through 14. Lower
021: * number means higher precedence.
022: */
023: public class JBinaryOperator {
024:
025: public static final JBinaryOperator MUL = new JBinaryOperator("*",
026: 3);
027: public static final JBinaryOperator DIV = new JBinaryOperator("/",
028: 3);
029: public static final JBinaryOperator MOD = new JBinaryOperator("%",
030: 3);
031: public static final JBinaryOperator ADD = new JBinaryOperator("+",
032: 4);
033: public static final JBinaryOperator SUB = new JBinaryOperator("-",
034: 4);
035:
036: public static final JBinaryOperator SHL = new JBinaryOperator("<<",
037: 5);
038: public static final JBinaryOperator SHR = new JBinaryOperator(">>",
039: 5);
040: public static final JBinaryOperator SHRU = new JBinaryOperator(
041: ">>>", 5);
042:
043: public static final JBinaryOperator LT = new JBinaryOperator("<", 6);
044: public static final JBinaryOperator LTE = new JBinaryOperator("<=",
045: 6);
046: public static final JBinaryOperator GT = new JBinaryOperator(">", 6);
047: public static final JBinaryOperator GTE = new JBinaryOperator(">=",
048: 6);
049:
050: public static final JBinaryOperator EQ = new JBinaryOperator("==",
051: 7);
052: public static final JBinaryOperator NEQ = new JBinaryOperator("!=",
053: 7);
054:
055: public static final JBinaryOperator BIT_AND = new JBinaryOperator(
056: "&", 8);
057:
058: public static final JBinaryOperator BIT_XOR = new JBinaryOperator(
059: "^", 9);
060:
061: public static final JBinaryOperator BIT_OR = new JBinaryOperator(
062: "|", 10);
063:
064: public static final JBinaryOperator AND = new JBinaryOperator("&&",
065: 11);
066:
067: public static final JBinaryOperator OR = new JBinaryOperator("||",
068: 12);
069:
070: // Don't renumber ASG precs without checking implementation of isAssignment()
071: public static final JBinaryOperator ASG = new JBinaryOperator("=",
072: 14);
073: public static final JBinaryOperator ASG_ADD = new JBinaryOperator(
074: "+=", 14);
075: public static final JBinaryOperator ASG_SUB = new JBinaryOperator(
076: "-=", 14);
077: public static final JBinaryOperator ASG_MUL = new JBinaryOperator(
078: "*=", 14);
079: public static final JBinaryOperator ASG_DIV = new JBinaryOperator(
080: "/=", 14);
081: public static final JBinaryOperator ASG_MOD = new JBinaryOperator(
082: "%=", 14);
083: public static final JBinaryOperator ASG_SHL = new JBinaryOperator(
084: "<<=", 14);
085: public static final JBinaryOperator ASG_SHR = new JBinaryOperator(
086: ">>=", 14);
087: public static final JBinaryOperator ASG_SHRU = new JBinaryOperator(
088: ">>>=", 14);
089: public static final JBinaryOperator ASG_BIT_AND = new JBinaryOperator(
090: "&=", 14);
091: public static final JBinaryOperator ASG_BIT_OR = new JBinaryOperator(
092: "|=", 14);
093: public static final JBinaryOperator ASG_BIT_XOR = new JBinaryOperator(
094: "^=", 14);
095:
096: private final char[] symbol;
097: private final int precedence;
098:
099: private JBinaryOperator(String symbol, int precedence) {
100: this .symbol = symbol.toCharArray();
101: this .precedence = precedence;
102: }
103:
104: public int getPrecedence() {
105: return precedence;
106: }
107:
108: public char[] getSymbol() {
109: return symbol;
110: }
111:
112: public boolean isAssignment() {
113: /*
114: * Beware, flaky! Maybe I should have added Yet Another Field to
115: * BinaryOperator?
116: */
117: return (precedence == ASG.getPrecedence());
118: }
119:
120: public String toString() {
121: return new String(getSymbol());
122: }
123:
124: }
|