Search in sources :

Example 51 with MathException

use of org.matheclipse.parser.client.math.MathException in project symja_android_library by axkr.

the class Console method interpreter.

/**
 * Evaluates the given string-expression and returns the result in <code>OutputForm</code>
 *
 * @param trimmedInput a trimmed input string
 * @return
 */
/* package private */
String interpreter(final String trimmedInput) {
    IExpr result;
    final StringWriter buf = new StringWriter();
    try {
        if (trimmedInput.length() > 1 && trimmedInput.charAt(0) == '?') {
            IExpr doc = Documentation.findDocumentation(trimmedInput);
            return printResult(doc);
        }
        if (fSeconds <= 0) {
            result = fEvaluator.eval(trimmedInput);
        } else {
            result = fEvaluator.evaluateWithTimeout(trimmedInput, fSeconds, TimeUnit.SECONDS, true, new EvalControlledCallable(fEvaluator.getEvalEngine()));
        }
        if (result != null) {
            return printResult(result);
        }
    } catch (final AbortException re) {
        return printResult(S.$Aborted);
    } catch (final FailedException re) {
        return printResult(S.$Failed);
    } catch (final SyntaxError se) {
        String msg = se.getMessage();
        stderr.println(msg);
        // stderr.println();
        stderr.flush();
        return "";
    } catch (final RuntimeException re) {
        Throwable me = re.getCause();
        if (me instanceof MathException) {
            Validate.printException(buf, me);
        } else {
            Validate.printException(buf, re);
        }
        stderr.println(buf.toString());
        stderr.flush();
        return "";
    } catch (final Exception | OutOfMemoryError | StackOverflowError e) {
        Validate.printException(buf, e);
        stderr.println(buf.toString());
        stderr.flush();
        return "";
    }
    return buf.toString();
}
Also used : MathException(org.matheclipse.parser.client.math.MathException) AbortException(org.matheclipse.core.eval.exception.AbortException) FailedException(org.matheclipse.core.eval.exception.FailedException) ReturnException(org.matheclipse.core.eval.exception.ReturnException) IOException(java.io.IOException) StringWriter(java.io.StringWriter) SyntaxError(org.matheclipse.parser.client.SyntaxError) FailedException(org.matheclipse.core.eval.exception.FailedException) MathException(org.matheclipse.parser.client.math.MathException) EvalControlledCallable(org.matheclipse.core.eval.EvalControlledCallable) IExpr(org.matheclipse.core.interfaces.IExpr) AbortException(org.matheclipse.core.eval.exception.AbortException)

Aggregations

MathException (org.matheclipse.parser.client.math.MathException)51 IExpr (org.matheclipse.core.interfaces.IExpr)41 SyntaxError (org.matheclipse.parser.client.SyntaxError)37 ExprEvaluator (org.matheclipse.core.eval.ExprEvaluator)24 IAST (org.matheclipse.core.interfaces.IAST)15 StringWriter (java.io.StringWriter)12 SymjaMathException (org.matheclipse.core.eval.exception.SymjaMathException)8 IOException (java.io.IOException)7 AbortException (org.matheclipse.core.eval.exception.AbortException)7 FailedException (org.matheclipse.core.eval.exception.FailedException)7 ASTNode (org.matheclipse.parser.client.ast.ASTNode)7 EvalControlledCallable (org.matheclipse.core.eval.EvalControlledCallable)5 EvalEngine (org.matheclipse.core.eval.EvalEngine)5 DoubleEvaluator (org.matheclipse.parser.client.eval.DoubleEvaluator)5 ISymbol (org.matheclipse.core.interfaces.ISymbol)4 FlowControlException (org.matheclipse.core.eval.exception.FlowControlException)3 OutputFormFactory (org.matheclipse.core.form.output.OutputFormFactory)3 ExprParser (org.matheclipse.core.parser.ExprParser)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 AST2Expr (org.matheclipse.core.convert.AST2Expr)2