001    /*
002     * Copyright 2010-2013 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of 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,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.k2js.translate.operation;
018    
019    import com.google.common.collect.ImmutableBiMap;
020    import com.google.dart.compiler.backend.js.ast.JsBinaryOperator;
021    import com.google.dart.compiler.backend.js.ast.JsUnaryOperator;
022    import org.jetbrains.annotations.NotNull;
023    import org.jetbrains.jet.lexer.JetToken;
024    import org.jetbrains.jet.lexer.JetTokens;
025    
026    import java.util.Map;
027    
028    public final class OperatorTable {
029    
030        //TODO : not all operators , add and test bit operators
031        private static final Map<JetToken, JsBinaryOperator> binaryOperatorsMap = ImmutableBiMap.<JetToken, JsBinaryOperator>builder()
032                .put(JetTokens.PLUS, JsBinaryOperator.ADD)
033                .put(JetTokens.MINUS, JsBinaryOperator.SUB)
034                .put(JetTokens.MUL, JsBinaryOperator.MUL)
035                .put(JetTokens.DIV, JsBinaryOperator.DIV)
036                .put(JetTokens.EQ, JsBinaryOperator.ASG)
037                .put(JetTokens.GT, JsBinaryOperator.GT)
038                .put(JetTokens.GTEQ, JsBinaryOperator.GTE)
039                .put(JetTokens.LT, JsBinaryOperator.LT)
040                .put(JetTokens.LTEQ, JsBinaryOperator.LTE)
041                .put(JetTokens.ANDAND, JsBinaryOperator.AND)
042                .put(JetTokens.OROR, JsBinaryOperator.OR)
043                .put(JetTokens.PERC, JsBinaryOperator.MOD)
044                .put(JetTokens.PLUSEQ, JsBinaryOperator.ASG_ADD)
045                .put(JetTokens.MINUSEQ, JsBinaryOperator.ASG_SUB)
046                .put(JetTokens.DIVEQ, JsBinaryOperator.ASG_DIV)
047                .put(JetTokens.MULTEQ, JsBinaryOperator.ASG_MUL)
048                .put(JetTokens.PERCEQ, JsBinaryOperator.ASG_MOD)
049                .put(JetTokens.IN_KEYWORD, JsBinaryOperator.INOP)
050                .put(JetTokens.EQEQEQ, JsBinaryOperator.REF_EQ)
051                .put(JetTokens.EXCLEQEQEQ, JsBinaryOperator.REF_NEQ)
052                .build();
053    
054        private static final ImmutableBiMap<JetToken, JsUnaryOperator> unaryOperatorsMap = ImmutableBiMap.<JetToken, JsUnaryOperator>builder()
055                .put(JetTokens.PLUSPLUS, JsUnaryOperator.INC)
056                .put(JetTokens.MINUSMINUS, JsUnaryOperator.DEC)
057                .put(JetTokens.EXCL, JsUnaryOperator.NOT)
058                .put(JetTokens.MINUS, JsUnaryOperator.NEG)
059                .put(JetTokens.PLUS, JsUnaryOperator.POS)
060                .build();
061    
062        private OperatorTable() {
063        }
064    
065        public static boolean hasCorrespondingOperator(@NotNull JetToken token) {
066            return binaryOperatorsMap.containsKey(token) || unaryOperatorsMap.containsKey(token);
067        }
068    
069        public static boolean hasCorrespondingBinaryOperator(@NotNull JetToken token) {
070            return binaryOperatorsMap.containsKey(token);
071        }
072    
073        @NotNull
074        static public JsBinaryOperator getBinaryOperator(@NotNull JetToken token) {
075            assert JetTokens.OPERATIONS.contains(token) : "Token should represent an operation!";
076            return binaryOperatorsMap.get(token);
077        }
078    
079        @NotNull
080        static public JsUnaryOperator getUnaryOperator(@NotNull JetToken token) {
081            assert JetTokens.OPERATIONS.contains(token) : "Token should represent an operation!";
082            return unaryOperatorsMap.get(token);
083        }
084    }