Search in sources :

Example 1 with IterationLimitExceeded

use of org.matheclipse.core.eval.exception.IterationLimitExceeded in project symja_android_library by axkr.

the class EvalControlledCallable method call.

@Override
public IExpr call() {
    EvalEngine.remove();
    EvalEngine.setReset(fEngine);
    final StringWriter buf = new StringWriter();
    try {
        // fEngine.reset();<>
        IExpr preRead = S.$PreRead.assignedValue();
        IExpr temp;
        try {
            if (preRead != null && preRead.isPresent()) {
                temp = fEngine.evaluate(F.unaryAST1(preRead, fExpr));
            } else {
                temp = fEngine.evaluate(fExpr);
            }
        } catch (final IterationLimitExceeded e) {
            // Iteration limit of `1` exceeded.
            int iterationLimit = fEngine.getIterationLimit();
            IOFunctions.printMessage(S.$IterationLimit, "itlim", F.list(iterationLimit < 0 ? F.CInfinity : F.ZZ(iterationLimit), fExpr), fEngine);
            temp = F.Hold(fExpr);
        } catch (final RecursionLimitExceeded e) {
            // Recursion depth of `1` exceeded during evaluation of `2`.
            int recursionLimit = fEngine.getRecursionLimit();
            IOFunctions.printMessage(S.$RecursionLimit, "reclim2", F.list(recursionLimit < 0 ? F.CInfinity : F.ZZ(recursionLimit), fExpr), fEngine);
            temp = F.Hold(fExpr);
        }
        // IExpr temp = fEngine.evaluate(fExpr);
        if (!fEngine.isOutListDisabled()) {
            fEngine.addInOut(fExpr, temp);
        }
        return temp;
    } catch (org.matheclipse.core.eval.exception.TimeoutException e) {
        return S.$Aborted;
    } catch (final SyntaxError se) {
        LOGGER.error("EvalControlledCallable.call() failed", se);
    } catch (final RuntimeException re) {
        Throwable me = re.getCause();
        if (me instanceof MathException) {
            Validate.printException(buf, me);
        } else {
            Validate.printException(buf, re);
        }
        LOGGER.error(buf);
    } catch (final Exception | OutOfMemoryError | StackOverflowError e) {
        LOGGER.debug("EvalControlledCallable.call() failed", e);
        Validate.printException(buf, e);
        LOGGER.error(buf);
    }
    return S.$Aborted;
}
Also used : IterationLimitExceeded(org.matheclipse.core.eval.exception.IterationLimitExceeded) MathException(org.matheclipse.parser.client.math.MathException) RecursionLimitExceeded(org.matheclipse.core.eval.exception.RecursionLimitExceeded) StringWriter(java.io.StringWriter) SyntaxError(org.matheclipse.parser.client.SyntaxError) MathException(org.matheclipse.parser.client.math.MathException) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 2 with IterationLimitExceeded

use of org.matheclipse.core.eval.exception.IterationLimitExceeded in project symja_android_library by axkr.

the class ExprEvaluator method evalTryCatch.

public static IExpr evalTryCatch(final IExpr expr, EvalEngine[] engineRef) {
    EvalEngine engine = engineRef[0];
    EvalEngine.set(engine);
    // engine.reset() must be done before parsing step
    IExpr preRead = S.$PreRead.assignedValue();
    IExpr temp;
    try {
        if (preRead != null && preRead.isPresent()) {
            temp = engine.evaluate(F.unaryAST1(preRead, expr));
        } else {
            temp = engine.evaluate(expr);
        }
    } catch (ReturnException rex) {
        LOGGER.debug("ExprEvaluator.evalTryCatch() failed", rex);
        return rex.getValue();
    } catch (BreakException | ContinueException conex) {
        LOGGER.debug("ExprEvaluator.evalTryCatch() failed", conex);
        IAST ast = F.Continue();
        if (conex instanceof BreakException) {
            ast = F.Break();
        }
        // No enclosing For, While or Do found for `1`.
        IOFunctions.printMessage(S.Continue, "nofwd", F.list(ast), engine);
        temp = F.Hold(ast);
    } catch (ThrowException e) {
        LOGGER.debug("ExprEvaluator.evalTryCatch() failed", e);
        // Uncaught `1` returned to top level.
        IAST ast = F.Throw(e.getValue());
        IOFunctions.printMessage(S.Throw, "nocatch", F.list(ast), engine);
        temp = F.Hold(ast);
    } catch (IterationLimitExceeded e) {
        LOGGER.debug("ExprEvaluator.evalTryCatch() failed", e);
        // Iteration limit of `1` exceeded.
        int iterationLimit = engine.getIterationLimit();
        IOFunctions.printMessage(S.$IterationLimit, "itlim", F.list(iterationLimit < 0 ? F.CInfinity : F.ZZ(iterationLimit), expr), engine);
        temp = F.Hold(expr);
    } catch (RecursionLimitExceeded e) {
        LOGGER.debug("ExprEvaluator.evalTryCatch() failed", e);
        // Recursion depth of `1` exceeded during evaluation of `2`.
        int recursionLimit = engine.getRecursionLimit();
        IOFunctions.printMessage(S.$RecursionLimit, "reclim2", F.list(recursionLimit < 0 ? F.CInfinity : F.ZZ(recursionLimit), expr), engine);
        temp = F.Hold(expr);
    }
    if (!engine.isOutListDisabled()) {
        engine.addInOut(expr, temp);
    }
    return temp;
}
Also used : RecursionLimitExceeded(org.matheclipse.core.eval.exception.RecursionLimitExceeded) ThrowException(org.matheclipse.core.eval.exception.ThrowException) ContinueException(org.matheclipse.core.eval.exception.ContinueException) BreakException(org.matheclipse.core.eval.exception.BreakException) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) IterationLimitExceeded(org.matheclipse.core.eval.exception.IterationLimitExceeded) ReturnException(org.matheclipse.core.eval.exception.ReturnException)

Aggregations

IterationLimitExceeded (org.matheclipse.core.eval.exception.IterationLimitExceeded)2 RecursionLimitExceeded (org.matheclipse.core.eval.exception.RecursionLimitExceeded)2 IExpr (org.matheclipse.core.interfaces.IExpr)2 StringWriter (java.io.StringWriter)1 BreakException (org.matheclipse.core.eval.exception.BreakException)1 ContinueException (org.matheclipse.core.eval.exception.ContinueException)1 ReturnException (org.matheclipse.core.eval.exception.ReturnException)1 ThrowException (org.matheclipse.core.eval.exception.ThrowException)1 IAST (org.matheclipse.core.interfaces.IAST)1 SyntaxError (org.matheclipse.parser.client.SyntaxError)1 MathException (org.matheclipse.parser.client.math.MathException)1