From 48b1ab417b7fc07a5b5608d805bc770a7906823a Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Sat, 9 Jun 2012 00:28:17 +0400 Subject: [PATCH] invoke repl from command line --- .../jetbrains/jet/cli/jvm/K2JVMCompiler.java | 7 ++ .../jet/cli/jvm/repl/ReplFromTerminal.java | 101 ++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 compiler/cli/src/org/jetbrains/jet/cli/jvm/repl/ReplFromTerminal.java diff --git a/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java index cf158292b9e..18e6a2eac76 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java @@ -29,6 +29,7 @@ import org.jetbrains.jet.cli.jvm.compiler.K2JVMCompileEnvironmentConfiguration; import org.jetbrains.jet.cli.jvm.compiler.CompileEnvironmentUtil; import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment; import org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler; +import org.jetbrains.jet.cli.jvm.repl.ReplFromTerminal; import org.jetbrains.jet.codegen.CompilationException; import org.jetbrains.jet.lang.resolve.java.CompilerDependencies; import org.jetbrains.jet.lang.resolve.java.CompilerSpecialMode; @@ -84,6 +85,12 @@ public class K2JVMCompiler extends CLICompiler scriptArgs = arguments.script ? arguments.freeArgs.subList(1, arguments.freeArgs.size()) : Collections.emptyList(); K2JVMCompileEnvironmentConfiguration configuration = new K2JVMCompileEnvironmentConfiguration(environment, messageCollector, arguments.script, scriptArgs); diff --git a/compiler/cli/src/org/jetbrains/jet/cli/jvm/repl/ReplFromTerminal.java b/compiler/cli/src/org/jetbrains/jet/cli/jvm/repl/ReplFromTerminal.java new file mode 100644 index 00000000000..b3f417ee94d --- /dev/null +++ b/compiler/cli/src/org/jetbrains/jet/cli/jvm/repl/ReplFromTerminal.java @@ -0,0 +1,101 @@ +/* + * Copyright 2010-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.cli.jvm.repl; + +import com.intellij.openapi.Disposable; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.resolve.java.CompilerDependencies; +import org.jetbrains.jet.lang.resolve.java.CompilerSpecialMode; +import org.jetbrains.jet.utils.ExceptionUtils; + +import javax.sound.midi.SysexMessage; +import java.io.Console; +import java.io.File; +import java.util.Collections; +import java.util.List; + +/** + * @author Stepan Koltsov + */ +public class ReplFromTerminal { + + private ReplInterpreter replInterpreter; + private Throwable replInitializationFailed; + private final Object waitRepl = new Object(); + + public ReplFromTerminal(@NotNull final Disposable disposable, @NotNull final CompilerDependencies compilerDependencies) { + final List extraClasspath = Collections.emptyList(); + new Thread("initialize-repl") { + @Override + public void run() { + try { + replInterpreter = new ReplInterpreter(disposable, compilerDependencies, extraClasspath); + } catch (Throwable e) { + replInitializationFailed = e; + } + synchronized (waitRepl) { + waitRepl.notifyAll(); + } + } + }.start(); + } + + private ReplInterpreter getReplInterpreter() { + if (replInterpreter != null) { + return replInterpreter; + } + synchronized (waitRepl) { + while (replInterpreter == null && replInitializationFailed == null) { + try { + waitRepl.wait(); + } catch (Throwable e) { + throw ExceptionUtils.rethrow(e); + } + } + if (replInterpreter != null) { + return replInterpreter; + } + throw ExceptionUtils.rethrow(replInitializationFailed); + } + } + + private void doRun() { + System.out.println("Kotlin"); + while (true) { + boolean next = one(); + if (!next) { + break; + } + } + } + + private boolean one() { + System.out.print(">>> "); + String line = System.console().readLine(); + if (line == null) { + return false; + } + Object value = getReplInterpreter().eval(line); + System.out.println(value); + return true; + } + + public static void run(@NotNull Disposable disposable, @NotNull CompilerDependencies compilerDependencies) { + new ReplFromTerminal(disposable, compilerDependencies).doRun(); + } + +}