use of org.cafebabepy.evaluter.Interpret.InterpretEvaluator in project cafebabepy by cafebabepy.
the class InteractiveConsole method interact.
public void interact() {
InteractiveParser parser = new InteractiveParser(this.runtime);
InterpretEvaluator evaluator = new InterpretEvaluator(this.runtime);
Terminal terminal = TerminalFactory.get();
terminal.setEchoEnabled(false);
try {
terminal.init();
} catch (Exception e) {
e.printStackTrace();
return;
}
try (ConsoleReader consoleReader = new ConsoleReader(System.in, System.out, terminal)) {
this.consoleReader = consoleReader;
printBanner();
StringBuilder buffer = new StringBuilder();
while (status != Status.QUIT) {
consoleReader.flush();
String line = readLine();
if (line == null) {
break;
}
if (buffer.length() > 0) {
buffer.append(System.lineSeparator());
}
buffer.append(line);
PyObject ast;
try {
ast = parser.parse(buffer.toString());
if (ast.isNone()) {
this.status = Status.INCOMPLETE;
continue;
}
} catch (RaiseException e) {
e.printStackTrace();
this.consoleReader.println(e.getException().toJava(String.class));
this.status = Status.READLINE;
buffer.setLength(0);
continue;
}
this.status = Status.READLINE;
buffer.setLength(0);
PyObject result;
try {
result = evaluator.evalMainModule(ast);
if (!result.isNone()) {
if (result instanceof PyStrObject) {
this.consoleReader.println(result.toString());
} else {
this.consoleReader.println(result.toJava(String.class));
}
}
} catch (RaiseException e) {
PyObject exception = e.getException();
this.consoleReader.println(exception.toJava(String.class));
this.consoleReader.println(e.getMessage());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.cafebabepy.evaluter.Interpret.InterpretEvaluator in project cafebabepy by cafebabepy.
the class Python method eval.
public static PyObject eval(String input) {
Python runtime = Python.createRuntime();
NormalParser parser = new NormalParser(runtime);
PyObject ast = parser.parse(input);
InterpretEvaluator evaluter = new InterpretEvaluator(runtime);
return evaluter.evalMainModule(ast);
}
Aggregations