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());
}
}
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();
}
Aggregations