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.facade;
018
019import com.google.dart.compiler.backend.js.ast.JsProgram;
020import com.intellij.openapi.project.Project;
021import com.intellij.openapi.util.io.FileUtil;
022import org.jetbrains.annotations.NotNull;
023import org.jetbrains.jet.lang.psi.JetFile;
024import org.jetbrains.jet.lang.resolve.BindingContext;
025import org.jetbrains.k2js.analyze.AnalyzerFacadeForJS;
026import org.jetbrains.k2js.config.Config;
027import org.jetbrains.k2js.facade.exceptions.TranslationException;
028import org.jetbrains.k2js.translate.general.Translation;
029import org.jetbrains.k2js.utils.JetFileUtils;
030
031import java.io.File;
032import java.io.IOException;
033import java.util.Arrays;
034import java.util.List;
035
036import static org.jetbrains.k2js.facade.FacadeUtils.parseString;
037import static org.jetbrains.k2js.generate.CodeGenerator.generateProgramToString;
038
039/**
040 * An entry point of translator.
041 */
042public final class K2JSTranslator {
043
044    public static final String FLUSH_SYSTEM_OUT = "Kotlin.System.flush();\n";
045    public static final String GET_SYSTEM_OUT = "Kotlin.System.output();\n";
046
047    public static void translateWithMainCallParametersAndSaveToFile(@NotNull MainCallParameters mainCall,
048            @NotNull List<JetFile> files,
049            @NotNull String outputPath,
050            @NotNull Config config) throws TranslationException, IOException {
051        K2JSTranslator translator = new K2JSTranslator(config);
052        String programCode = translator.generateProgramCode(files, mainCall);
053        FileUtil.writeToFile(new File(outputPath), programCode);
054    }
055
056    @NotNull
057    private final Config config;
058
059
060    public K2JSTranslator(@NotNull Config config) {
061        this.config = config;
062    }
063
064    //NOTE: web demo related method
065    @SuppressWarnings("UnusedDeclaration")
066    @NotNull
067    public String translateStringWithCallToMain(@NotNull String programText, @NotNull String argumentsString) throws TranslationException {
068        JetFile file = JetFileUtils.createPsiFile("test", programText, getProject());
069        String programCode = generateProgramCode(file, MainCallParameters.mainWithArguments(parseString(argumentsString))) + "\n";
070        return FLUSH_SYSTEM_OUT + programCode + GET_SYSTEM_OUT;
071    }
072
073    @NotNull
074    public String generateProgramCode(@NotNull JetFile file, @NotNull MainCallParameters mainCallParameters) throws TranslationException {
075        JsProgram program = generateProgram(Arrays.asList(file), mainCallParameters);
076        return generateProgramToString(program);
077    }
078
079    @NotNull
080    public String generateProgramCode(@NotNull List<JetFile> files, @NotNull MainCallParameters mainCallParameters)
081            throws TranslationException {
082        JsProgram program = generateProgram(files, mainCallParameters);
083        return generateProgramToString(program);
084    }
085
086    @NotNull
087    public JsProgram generateProgram(@NotNull List<JetFile> filesToTranslate,
088            @NotNull MainCallParameters mainCallParameters)
089            throws TranslationException {
090        BindingContext bindingContext = AnalyzerFacadeForJS.analyzeFilesAndCheckErrors(filesToTranslate, config);
091        return Translation.generateAst(bindingContext, filesToTranslate, mainCallParameters, config);
092    }
093
094    @NotNull
095    private Project getProject() {
096        return config.getProject();
097    }
098}