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                .build();
050    
051        private static final ImmutableBiMap<JetToken, JsUnaryOperator> unaryOperatorsMap = ImmutableBiMap.<JetToken, JsUnaryOperator>builder()
052                .put(JetTokens.PLUSPLUS, JsUnaryOperator.INC)
053                .put(JetTokens.MINUSMINUS, JsUnaryOperator.DEC)
054                .put(JetTokens.EXCL, JsUnaryOperator.NOT)
055                .put(JetTokens.MINUS, JsUnaryOperator.NEG)
056                .put(JetTokens.PLUS, JsUnaryOperator.POS)
057                .build();
058    
059        private OperatorTable() {
060        }
061    
062    
063        public static boolean hasCorrespondingBinaryOperator(@NotNull JetToken token) {
064            return binaryOperatorsMap.containsKey(token);
065        }
066    
067        @NotNull
068        static public JsBinaryOperator getBinaryOperator(@NotNull JetToken token) {
069            assert JetTokens.OPERATIONS.contains(token) : "Token should represent an operation!";
070            return binaryOperatorsMap.get(token);
071        }
072    
073        @NotNull
074        static public JsUnaryOperator getUnaryOperator(@NotNull JetToken token) {
075            assert JetTokens.OPERATIONS.contains(token) : "Token should represent an operation!";
076            return unaryOperatorsMap.get(token);
077        }
078    }