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
017package org.jetbrains.k2js.translate.utils;
018
019import com.google.common.collect.Lists;
020import com.google.dart.compiler.backend.js.ast.JsExpression;
021import org.jetbrains.annotations.NotNull;
022import org.jetbrains.k2js.translate.context.TemporaryVariable;
023import org.jetbrains.k2js.translate.context.TranslationContext;
024
025import java.util.List;
026
027import static org.jetbrains.k2js.translate.utils.JsAstUtils.newSequence;
028
029//TODO: look for what should go there
030public final class TemporariesUtils {
031    private TemporariesUtils() {
032    }
033
034    @NotNull
035    public static List<TemporaryVariable> fromExpressionList(@NotNull List<JsExpression> expressions,
036                                                             @NotNull TranslationContext context) {
037        List<TemporaryVariable> result = Lists.newArrayList();
038        for (JsExpression expression : expressions) {
039            result.add(context.declareTemporary(expression));
040        }
041        return result;
042    }
043
044    @NotNull
045    public static List<JsExpression> toExpressionList(@NotNull List<TemporaryVariable> temporaries) {
046        List<JsExpression> result = Lists.newArrayList();
047        for (TemporaryVariable temp : temporaries) {
048            result.add(temp.reference());
049        }
050        return result;
051    }
052
053    @NotNull
054    public static JsExpression temporariesInitialization(@NotNull TemporaryVariable... temporaries) {
055        List<JsExpression> result = Lists.newArrayList();
056        for (TemporaryVariable temporary : temporaries) {
057            result.add(temporary.assignmentExpression());
058        }
059        return newSequence(result);
060    }
061
062    @NotNull
063    public static List<JsExpression> temporariesInitialization(@NotNull List<TemporaryVariable> temporaries) {
064        List<JsExpression> result = Lists.newArrayList();
065        for (TemporaryVariable temporary : temporaries) {
066            result.add(temporary.assignmentExpression());
067        }
068        return result;
069    }
070}