Search in sources :

Example 1 with IMain

use of scala.tools.nsc.interpreter.IMain in project zeppelin by apache.

the class FlinkInterpreter method interpret.

public InterpreterResult interpret(String[] lines, InterpreterContext context) {
    final IMain imain = flinkIloop.intp();
    String[] linesToRun = new String[lines.length + 1];
    for (int i = 0; i < lines.length; i++) {
        linesToRun[i] = lines[i];
    }
    linesToRun[lines.length] = "print(\"\")";
    System.setOut(new PrintStream(out));
    out.reset();
    Code r = null;
    String incomplete = "";
    boolean inComment = false;
    for (int l = 0; l < linesToRun.length; l++) {
        final String s = linesToRun[l];
        // check if next line starts with "." (but not ".." or "./") it is treated as an invocation
        if (l + 1 < linesToRun.length) {
            String nextLine = linesToRun[l + 1].trim();
            boolean continuation = false;
            if (nextLine.isEmpty() || // skip empty line or comment
            nextLine.startsWith("//") || nextLine.startsWith("}") || nextLine.startsWith("object")) {
                // include "} object" for Scala companion object
                continuation = true;
            } else if (!inComment && nextLine.startsWith("/*")) {
                inComment = true;
                continuation = true;
            } else if (inComment && nextLine.lastIndexOf("*/") >= 0) {
                inComment = false;
                continuation = true;
            } else if (nextLine.length() > 1 && nextLine.charAt(0) == '.' && // ".."
            nextLine.charAt(1) != '.' && nextLine.charAt(1) != '/') {
                // "./"
                continuation = true;
            } else if (inComment) {
                continuation = true;
            }
            if (continuation) {
                incomplete += s + "\n";
                continue;
            }
        }
        final String currentCommand = incomplete;
        scala.tools.nsc.interpreter.Results.Result res = null;
        try {
            res = Console.withOut(System.out, new AbstractFunction0<Results.Result>() {

                @Override
                public Results.Result apply() {
                    return imain.interpret(currentCommand + s);
                }
            });
        } catch (Exception e) {
            logger.info("Interpreter exception", e);
            return new InterpreterResult(Code.ERROR, InterpreterUtils.getMostRelevantMessage(e));
        }
        r = getResultCode(res);
        if (r == Code.ERROR) {
            return new InterpreterResult(r, out.toString());
        } else if (r == Code.INCOMPLETE) {
            incomplete += s + "\n";
        } else {
            incomplete = "";
        }
    }
    if (r == Code.INCOMPLETE) {
        return new InterpreterResult(r, "Incomplete expression");
    } else {
        return new InterpreterResult(r, out.toString());
    }
}
Also used : PrintStream(java.io.PrintStream) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) IMain(scala.tools.nsc.interpreter.IMain) AbstractFunction0(scala.runtime.AbstractFunction0) Code(org.apache.zeppelin.interpreter.InterpreterResult.Code) InvocationTargetException(java.lang.reflect.InvocationTargetException) Results(scala.tools.nsc.interpreter.Results)

Example 2 with IMain

use of scala.tools.nsc.interpreter.IMain in project zeppelin by apache.

the class IgniteInterpreter method open.

@Override
public void open() {
    Settings settings = new Settings();
    URL[] urls = getClassloaderUrls();
    // set classpath
    PathSetting pathSettings = settings.classpath();
    StringBuilder sb = new StringBuilder();
    for (File f : currentClassPath()) {
        if (sb.length() > 0) {
            sb.append(File.pathSeparator);
        }
        sb.append(f.getAbsolutePath());
    }
    if (urls != null) {
        for (URL u : urls) {
            if (sb.length() > 0) {
                sb.append(File.pathSeparator);
            }
            sb.append(u.getFile());
        }
    }
    pathSettings.v_$eq(sb.toString());
    settings.scala$tools$nsc$settings$ScalaSettings$_setter_$classpath_$eq(pathSettings);
    settings.explicitParentLoader_$eq(new Some<>(Thread.currentThread().getContextClassLoader()));
    BooleanSetting b = (BooleanSetting) settings.usejavacp();
    b.v_$eq(true);
    settings.scala$tools$nsc$settings$StandardScalaSettings$_setter_$usejavacp_$eq(b);
    out = new ByteArrayOutputStream();
    imain = new IMain(settings, new PrintWriter(out));
    initIgnite();
}
Also used : PathSetting(scala.tools.nsc.settings.MutableSettings.PathSetting) BooleanSetting(scala.tools.nsc.settings.MutableSettings.BooleanSetting) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IMain(scala.tools.nsc.interpreter.IMain) File(java.io.File) Settings(scala.tools.nsc.Settings) URL(java.net.URL) PrintWriter(java.io.PrintWriter)

Aggregations

IMain (scala.tools.nsc.interpreter.IMain)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 PrintStream (java.io.PrintStream)1 PrintWriter (java.io.PrintWriter)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URL (java.net.URL)1 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)1 Code (org.apache.zeppelin.interpreter.InterpreterResult.Code)1 AbstractFunction0 (scala.runtime.AbstractFunction0)1 Settings (scala.tools.nsc.Settings)1 Results (scala.tools.nsc.interpreter.Results)1 BooleanSetting (scala.tools.nsc.settings.MutableSettings.BooleanSetting)1 PathSetting (scala.tools.nsc.settings.MutableSettings.PathSetting)1