use of org.matheclipse.core.eval.exception.FailedException 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();
}
}
use of org.matheclipse.core.eval.exception.FailedException in project symja_android_library by axkr.
the class MMAConsole 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();
}
use of org.matheclipse.core.eval.exception.FailedException 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();
}
use of org.matheclipse.core.eval.exception.FailedException in project symja_android_library by axkr.
the class PatternMatching method setDelayedDownRule.
private static void setDelayedDownRule(IExpr leftHandSide, int flags, IExpr rightHandSide, boolean packageMode) {
ISymbol lhsSymbol = null;
if (leftHandSide instanceof PatternNested) {
PatternNested pn = (PatternNested) leftHandSide;
IExpr pattern = pn.getPatternExpr();
lhsSymbol = determineRuleTag(pattern);
}
if (leftHandSide.isAST()) {
lhsSymbol = determineRuleTag(leftHandSide);
}
if (lhsSymbol != null) {
if (lhsSymbol.isProtected()) {
// Symbol `1` is Protected.
IOFunctions.printMessage(S.SetDelayed, "wrsym", F.list(lhsSymbol), EvalEngine.get());
throw new FailedException();
}
lhsSymbol.putDownRule(flags | IPatternMatcher.SET_DELAYED, false, leftHandSide, rightHandSide, packageMode);
return;
}
if (leftHandSide.isSymbol()) {
lhsSymbol = (ISymbol) leftHandSide;
if (lhsSymbol.isProtected()) {
// Symbol `1` is Protected.
IOFunctions.printMessage(S.SetDelayed, "wrsym", F.list(lhsSymbol), EvalEngine.get());
throw new FailedException();
}
((ISymbol) leftHandSide).assignValue(rightHandSide, true);
return;
}
throw new RuleCreationError(leftHandSide);
}
Aggregations