Search in sources :

Example 1 with ApfloatRuntimeException

use of org.apfloat.ApfloatRuntimeException in project symja_android_library by axkr.

the class MathScriptEngine method eval.

@Override
public Object eval(final String script, final ScriptContext context) {
    boolean relaxedSyntax = false;
    final Object enableStackTraceBoolean = get("PRINT_STACKTRACE");
    Level stackLogLevel = Boolean.TRUE.equals(enableStackTraceBoolean) ? Level.ERROR : Level.DEBUG;
    try {
        // first assign the EvalEngine to the current thread:
        EvalEngine.setReset(fEngine);
        // final Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
        // ISymbol symbol;
        // for (Map.Entry<String, Object> currEntry : bindings.entrySet()) {
        // symbol = F.userSymbol(currEntry.getKey(), fEngine);
        // symbol.pushLocalVariable(Object2Expr.convert(currEntry.getValue()));
        // list.add(symbol);
        // }
        final Object decimalFormat = get("DECIMAL_FORMAT");
        if (decimalFormat instanceof String) {
            fDecimalFormat = (String) decimalFormat;
        }
        final Object relaxedSyntaxBoolean = get("RELAXED_SYNTAX");
        if (Boolean.TRUE.equals(relaxedSyntaxBoolean)) {
            relaxedSyntax = true;
            fEngine.setRelaxedSyntax(relaxedSyntax);
        }
        boolean disableHistory = true;
        final Object enableHistoryBoolean = get("ENABLE_HISTORY");
        if (Boolean.TRUE.equals(enableHistoryBoolean)) {
            disableHistory = false;
            fEngine.setOutListDisabled(disableHistory, (short) 100);
        }
        // evaluate an expression
        final Object stepwise = get("STEPWISE");
        IExpr result;
        String trimmedScript = script.trim();
        if (trimmedScript.length() == 0) {
            return "";
        }
        if (Boolean.TRUE.equals(stepwise)) {
            result = fUtility.evalTrace(script, null);
        } else {
            result = fUtility.evaluate(script);
        }
        final Object returnType = context.getAttribute("RETURN_OBJECT");
        if ((returnType != null) && returnType.equals(Boolean.TRUE)) {
            // return the object "as is"
            return result;
        } else {
            // return the object as String representation
            if (result != null) {
                return printResult(result, relaxedSyntax);
            }
            return "";
        }
    } catch (final AbortException e) {
        LOGGER.log(stackLogLevel, "Aborted", e);
        return printResult(S.$Aborted, relaxedSyntax);
    } catch (final FailedException e) {
        LOGGER.log(stackLogLevel, "Failed", e);
        return printResult(S.$Failed, relaxedSyntax);
    } catch (final MathException e) {
        // catches parser errors as well
        LOGGER.log(stackLogLevel, "evaluation failed", e);
        return e.getMessage();
    } catch (final ApfloatRuntimeException e) {
        LOGGER.log(stackLogLevel, "ApFloat error", e);
        // catch parser errors here
        return "Apfloat: " + e.getMessage();
    } catch (final Exception e) {
        LOGGER.log(stackLogLevel, "Exception", e);
        return "Exception: " + e.getMessage();
    } catch (final OutOfMemoryError e) {
        LOGGER.log(stackLogLevel, "Out of memory", e);
        return "OutOfMemoryError";
    } catch (final StackOverflowError e) {
        LOGGER.log(stackLogLevel, "Stack overflow", e);
        return "StackOverflowError";
    } finally {
        // if (list.size() > 0) {
        // for (int i = 0; i < list.size(); i++) {
        // list.get(i).popLocalVariable();
        // }
        // }
        EvalEngine.remove();
    }
}
Also used : ApfloatRuntimeException(org.apfloat.ApfloatRuntimeException) FailedException(org.matheclipse.core.eval.exception.FailedException) MathException(org.matheclipse.parser.client.math.MathException) Level(org.apache.logging.log4j.Level) IExpr(org.matheclipse.core.interfaces.IExpr) ApfloatRuntimeException(org.apfloat.ApfloatRuntimeException) MathException(org.matheclipse.parser.client.math.MathException) IOException(java.io.IOException) AbortException(org.matheclipse.core.eval.exception.AbortException) FailedException(org.matheclipse.core.eval.exception.FailedException) ScriptException(javax.script.ScriptException) AbortException(org.matheclipse.core.eval.exception.AbortException)

Aggregations

IOException (java.io.IOException)1 ScriptException (javax.script.ScriptException)1 Level (org.apache.logging.log4j.Level)1 ApfloatRuntimeException (org.apfloat.ApfloatRuntimeException)1 AbortException (org.matheclipse.core.eval.exception.AbortException)1 FailedException (org.matheclipse.core.eval.exception.FailedException)1 IExpr (org.matheclipse.core.interfaces.IExpr)1 MathException (org.matheclipse.parser.client.math.MathException)1