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.expression.foreach;
018
019 import com.google.common.collect.Lists;
020 import com.google.dart.compiler.backend.js.ast.*;
021 import org.jetbrains.annotations.NotNull;
022 import org.jetbrains.jet.lang.psi.JetBinaryExpression;
023 import org.jetbrains.jet.lang.psi.JetExpression;
024 import org.jetbrains.jet.lang.psi.JetForExpression;
025 import org.jetbrains.jet.lexer.JetTokens;
026 import org.jetbrains.k2js.translate.context.TemporaryVariable;
027 import org.jetbrains.k2js.translate.context.TranslationContext;
028
029 import java.util.List;
030
031 import static org.jetbrains.k2js.translate.utils.JsAstUtils.*;
032 import static org.jetbrains.k2js.translate.utils.PsiUtils.getLoopRange;
033 import static org.jetbrains.k2js.translate.utils.TemporariesUtils.temporariesInitialization;
034 import static org.jetbrains.k2js.translate.utils.TranslationUtils.translateLeftExpression;
035 import static org.jetbrains.k2js.translate.utils.TranslationUtils.translateRightExpression;
036
037
038 // TODO: implement reverse semantics
039 public final class RangeLiteralForTranslator extends ForTranslator {
040
041 @NotNull
042 public static JsStatement doTranslate(@NotNull JetForExpression expression,
043 @NotNull TranslationContext context) {
044 return (new RangeLiteralForTranslator(expression, context).translate());
045 }
046
047 public static boolean isApplicable(@NotNull JetForExpression expression,
048 @NotNull TranslationContext context) {
049 JetExpression loopRange = getLoopRange(expression);
050 if (!(loopRange instanceof JetBinaryExpression)) {
051 return false;
052 }
053 boolean isRangeToOperation = ((JetBinaryExpression) loopRange).getOperationToken() == JetTokens.RANGE;
054 return isRangeToOperation && RangeForTranslator.isApplicable(expression, context);
055 }
056
057 @NotNull
058 private final JsExpression rangeStart;
059
060 @NotNull
061 private final TemporaryVariable rangeEnd;
062
063 private RangeLiteralForTranslator(@NotNull JetForExpression forExpression, @NotNull TranslationContext context) {
064 super(forExpression, context);
065 JetExpression loopRange = getLoopRange(expression);
066 assert loopRange instanceof JetBinaryExpression;
067 JetBinaryExpression loopRangeAsBinary = ((JetBinaryExpression) loopRange);
068 rangeStart = translateLeftExpression(context, loopRangeAsBinary);
069 rangeEnd = context.declareTemporary(getRangeEnd(loopRangeAsBinary));
070 }
071
072 @NotNull
073 private JsExpression getRangeEnd(@NotNull JetBinaryExpression loopRangeAsBinary) {
074 JsExpression rightExpression = translateRightExpression(context(), loopRangeAsBinary);
075 return new JsBinaryOperation(JsBinaryOperator.ADD, rightExpression, program().getNumberLiteral(1));
076 }
077
078 @NotNull
079 private JsBlock translate() {
080 List<JsStatement> blockStatements = Lists.newArrayList();
081 blockStatements.add(temporariesInitialization(rangeEnd).makeStmt());
082 blockStatements.add(generateForExpression(initExpression(),
083 getCondition(),
084 getIncrExpression(),
085 translateOriginalBodyExpression()));
086 return new JsBlock(blockStatements);
087 }
088
089 @NotNull
090 private JsVars initExpression() {
091 return newVar(parameterName, rangeStart);
092 }
093
094 @NotNull
095 private JsExpression getCondition() {
096 return inequality(parameterName.makeRef(), rangeEnd.reference());
097 }
098
099 @NotNull
100 private JsExpression getIncrExpression() {
101 return new JsPostfixOperation(JsUnaryOperator.INC, parameterName.makeRef());
102 }
103 }