Search in sources :

Example 1 with RaiseException

use of org.cafebabepy.runtime.RaiseException 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();
    }
}
Also used : ConsoleReader(jline.console.ConsoleReader) InterpretEvaluator(org.cafebabepy.evaluter.Interpret.InterpretEvaluator) PyStrObject(org.cafebabepy.runtime.object.java.PyStrObject) RaiseException(org.cafebabepy.runtime.RaiseException) InteractiveParser(org.cafebabepy.parser.InteractiveParser) Terminal(jline.Terminal) PyObject(org.cafebabepy.runtime.PyObject) IOException(java.io.IOException) RaiseException(org.cafebabepy.runtime.RaiseException)

Aggregations

IOException (java.io.IOException)1 Terminal (jline.Terminal)1 ConsoleReader (jline.console.ConsoleReader)1 InterpretEvaluator (org.cafebabepy.evaluter.Interpret.InterpretEvaluator)1 InteractiveParser (org.cafebabepy.parser.InteractiveParser)1 PyObject (org.cafebabepy.runtime.PyObject)1 RaiseException (org.cafebabepy.runtime.RaiseException)1 PyStrObject (org.cafebabepy.runtime.object.java.PyStrObject)1