use of org.matheclipse.core.eval.exception.ReturnException in project symja_android_library by axkr.
the class PatternMatcherAndEvaluator method replacePatternMatch.
/**
* A match which contains a pattern was found.
*
* <p>
* Assumption <code>
* matchExpr(fLhsPatternExpr, leftHandSide, engine, new StackMatcher(engine)) == true</code>.
*
* @param leftHandSide
* @param patternMap
* @param engine
* @param evaluate
* @return
*/
private IExpr replacePatternMatch(final IExpr leftHandSide, IPatternMap patternMap, EvalEngine engine, boolean evaluate) {
if (RulesData.showSteps) {
if (fLhsPatternExpr.head().equals(S.Integrate)) {
IExpr rhs = fRightHandSide.orElse(S.Null);
LOGGER.info("COMPLEX: {} := {}", fLhsPatternExpr, rhs);
LOGGER.info(">>>>> {}", this);
}
}
if (fReturnResult.isPresent()) {
return fReturnResult;
}
engine.pushOptionsStack();
try {
engine.setOptionsPattern(fLhsPatternExpr.topHead(), patternMap);
IExpr result = patternMap.substituteSymbols(fRightHandSide, F.CEmptySequence);
if (evaluate) {
if (Config.TRACE_REWRITE_RULE) {
return engine.addEvaluatedTraceStep(leftHandSide, result, leftHandSide.topHead(), F.$str("RewriteRule"));
}
return engine.evaluate(result);
} else {
return result;
}
} catch (final ConditionException e) {
if (LOGGER.isDebugEnabled()) {
logConditionFalse(leftHandSide, fLhsPatternExpr, fRightHandSide);
}
return F.NIL;
} catch (final ReturnException e) {
IExpr result = e.getValue();
if (evaluate) {
if (Config.TRACE_REWRITE_RULE) {
return engine.addEvaluatedTraceStep(leftHandSide, result, leftHandSide.topHead(), F.$str("RewriteRule"));
}
return engine.evaluate(result);
}
return result;
} finally {
engine.popOptionsStack();
}
}
use of org.matheclipse.core.eval.exception.ReturnException in project symja_android_library by axkr.
the class PatternMatcher method replaceSubExpressionOrderlessFlat.
/**
* Replace subexpressions for <code>Rule</code> or <code>RuleDelayed</code> in Flat or Orderless
* expressions.
*
* <pre>
* >> f(a, b, c) /. f(a, b) -> d
* f(d,c)
* </pre>
*
* @param lhsPatternAST
* @param lhsEvalAST
* @param rhsExpr
* @param engine
* @return <code>F.NIL</code> if no match was found.
*/
protected IExpr replaceSubExpressionOrderlessFlat(final IAST lhsPatternAST, final IAST lhsEvalAST, final IExpr rhsExpr, EvalEngine engine) {
if (lhsPatternAST.size() < lhsEvalAST.size()) {
if (lhsPatternAST.isOrderlessAST() && lhsPatternAST.isFlatAST()) {
if (!matchHeads(lhsPatternAST, lhsEvalAST, engine)) {
return F.NIL;
}
final OrderlessMatcher foMatcher = new OrderlessMatcher(lhsPatternAST, lhsEvalAST);
boolean matched = foMatcher.matchOrderlessAST(1, new StackMatcher(engine), engine);
if (matched) {
IASTAppendable lhsResultAST = (lhsEvalAST).copyAppendable();
foMatcher.filterResult(lhsResultAST);
IExpr result = fPatternMap.substituteSymbols(rhsExpr, F.NIL);
try {
result = engine.evaluate(result);
lhsResultAST.append(result);
return lhsResultAST;
} catch (final ConditionException e) {
if (LOGGER.isDebugEnabled()) {
logConditionFalse(lhsEvalAST, lhsPatternAST, rhsExpr);
}
// fall through
} catch (final ReturnException e) {
lhsResultAST.append(e.getValue());
return lhsResultAST;
}
}
return F.NIL;
}
if (lhsPatternAST.isFlatAST()) {
if (!matchHeads(lhsPatternAST, lhsEvalAST, engine)) {
return F.NIL;
}
return matchFlatSequenceFromIndex(lhsPatternAST, lhsEvalAST, rhsExpr, engine);
}
}
return F.NIL;
}
use of org.matheclipse.core.eval.exception.ReturnException in project symja_android_library by axkr.
the class MMAConsole method main.
public static void main(final String[] args) {
Locale.setDefault(Locale.US);
// distinguish between lower- and uppercase identifiers
ParserConfig.PARSER_USE_LOWERCASE_SYMBOLS = false;
ToggleFeature.COMPILE = true;
Config.BUILTIN_PROTECTED = ISymbol.NOATTRIBUTE;
Config.JAVA_UNSAFE = true;
Config.SHORTEN_STRING_LENGTH = 1024;
Config.USE_VISJS = true;
Config.FILESYSTEM_ENABLED = true;
IOInit.init();
MMAConsole console;
try {
console = new MMAConsole();
Config.PRINT_OUT = console::printOut;
} catch (final SyntaxError e1) {
e1.printStackTrace();
return;
}
String trimmedInput = null;
try {
console.setArgs(args);
} catch (ReturnException re) {
return;
}
while (true) {
try {
String inputExpression = console.readString(stdout, ">> ");
if (inputExpression != null) {
trimmedInput = inputExpression.trim();
if (inputExpression.length() > 1 && inputExpression.charAt(inputExpression.length() - 1) == '\t' && Scanner.isIdentifier(trimmedInput)) {
String docInput = "?" + trimmedInput + "*";
IExpr doc = Documentation.findDocumentation(docInput);
stdout.println(doc.toString());
continue;
}
if (trimmedInput.length() >= 4 && trimmedInput.charAt(0) == '/') {
String command = trimmedInput.substring(1).toLowerCase(Locale.ENGLISH);
if (command.equals("exit")) {
stdout.println("Closing Symja console... bye.");
System.exit(0);
} else if (command.equals("java")) {
stdout.println("Enabling output for JavaForm");
console.fUsedForm = JAVAFORM;
continue;
} else if (command.equals("traditional")) {
stdout.println("Enabling output for TraditionalForm");
console.fUsedForm = TRADITIONALFORM;
continue;
} else if (command.equals("output")) {
stdout.println("Enabling output for OutputForm");
console.fUsedForm = OUTPUTFORM;
continue;
} else if (command.equals("pretty")) {
stdout.println("Enabling output for PrettyPrinterForm");
console.fUsedForm = PRETTYFORM;
continue;
} else if (command.equals("input")) {
stdout.println("Enabling output for InputForm");
console.fUsedForm = INPUTFORM;
continue;
} else if (command.equals("timeoutoff")) {
stdout.println("Disabling timeout for evaluation");
console.fSeconds = -1;
continue;
} else if (command.equals("timeouton")) {
stdout.println("Enabling timeout for evaluation to 60 seconds.");
console.fSeconds = 60;
continue;
}
}
// if (trimmedInput.length() > 1 && trimmedInput.charAt(0) == '?') {
// IAST list = Names.getNamesByPrefix(trimmedInput.substring(1));
// for (int i = 1; i < list.size(); i++) {
// stdout.print(list.get(i).toString());
// if (i != list.argSize()) {
// stdout.print(", ");
// }
// }
// stdout.println();
// if (list.size() == 2) {
// printDocumentation(list.get(1).toString());
// }
// continue;
// }
// console.interpreter(trimmedInput);
String postfix = Scanner.balanceCode(trimmedInput);
if (postfix != null && postfix.length() > 0) {
stderr.println("Automatically closing brackets: " + postfix);
trimmedInput = trimmedInput + postfix;
}
stdout.println("In[" + COUNTER + "]:= " + trimmedInput);
stdout.flush();
// if (outputExpression.length() > 0) {
// stdout.println("Out[" + COUNTER + "]: " + outputExpression);
// }
// if (console.fPrettyPrinter) {
// console.prettyPrinter(trimmedInput);
// } else {
console.resultPrinter(trimmedInput);
// }
COUNTER++;
}
// } catch (final MathRuntimeException mre) {
// Throwable me = mre.getCause();
// stdout.println(me.getMessage());
} catch (final Exception e) {
stderr.println(e.getMessage());
stderr.flush();
}
}
}
use of org.matheclipse.core.eval.exception.ReturnException in project symja_android_library by axkr.
the class Console method main.
public static void main(final String[] args) {
Locale.setDefault(Locale.US);
ParserConfig.PARSER_USE_LOWERCASE_SYMBOLS = true;
ToggleFeature.COMPILE = true;
Config.JAVA_UNSAFE = true;
Config.SHORTEN_STRING_LENGTH = 1024;
Config.USE_VISJS = true;
Config.FILESYSTEM_ENABLED = true;
IOInit.init();
Console console;
try {
console = new Console();
Config.PRINT_OUT = console::printOut;
} catch (final SyntaxError e1) {
e1.printStackTrace();
return;
}
String trimmedInput = null;
try {
console.setArgs(args);
} catch (ReturnException re) {
return;
}
while (true) {
try {
String inputExpression = console.readString(stdout, ">> ");
if (inputExpression != null) {
trimmedInput = inputExpression.trim();
if (inputExpression.length() > 1 && inputExpression.charAt(inputExpression.length() - 1) == '\t' && Scanner.isIdentifier(trimmedInput)) {
String docInput = "?" + trimmedInput + "*";
IExpr doc = Documentation.findDocumentation(docInput);
stdout.println(doc.toString());
continue;
}
if (trimmedInput.length() >= 4 && trimmedInput.charAt(0) == '/') {
String command = trimmedInput.substring(1).toLowerCase(Locale.ENGLISH);
if (command.equals("exit")) {
stdout.println("Closing Symja console... bye.");
System.exit(0);
} else if (command.equals("java")) {
stdout.println("Enabling output for JavaForm");
console.fUsedForm = JAVAFORM;
continue;
} else if (command.equals("traditional")) {
stdout.println("Enabling output for TraditionalForm");
console.fUsedForm = TRADITIONALFORM;
continue;
} else if (command.equals("output")) {
stdout.println("Enabling output for OutputForm");
console.fUsedForm = OUTPUTFORM;
continue;
} else if (command.equals("pretty")) {
stdout.println("Enabling output for PrettyPrinterForm");
console.fUsedForm = PRETTYFORM;
continue;
} else if (command.equals("input")) {
stdout.println("Enabling output for InputForm");
console.fUsedForm = INPUTFORM;
continue;
} else if (command.equals("timeoutoff")) {
stdout.println("Disabling timeout for evaluation");
console.fSeconds = -1;
continue;
} else if (command.equals("timeouton")) {
stdout.println("Enabling timeout for evaluation to 60 seconds.");
console.fSeconds = 60;
continue;
}
}
// if ((trimmedInput.length() >= 4)
// && trimmedInput.toLowerCase(Locale.ENGLISH).substring(0, 4).equals("exit")) {
// stdout.println("Closing Symja console... bye.");
// System.exit(0);
// } else if ((trimmedInput.length() >= 7)
// && trimmedInput.toLowerCase(Locale.ENGLISH).substring(0, 7).equals("javaoff")) {
// stdout.println("Disabling output for JavaForm");
// console.fUseJavaForm = false;
// continue;
// } else if ((trimmedInput.length() >= 6)
// && trimmedInput.toLowerCase(Locale.ENGLISH).substring(0, 6).equals("javaon")) {
// stdout.println("Enabling output for JavaForm");
// console.fUseJavaForm = true;
// continue;
// } else if ((trimmedInput.length() >= 10)
// && trimmedInput.toLowerCase(Locale.ENGLISH).substring(0, 10).equals("timeoutoff")) {
// stdout.println("Disabling timeout for evaluation");
// console.fSeconds = -1;
// continue;
// } else if ((trimmedInput.length() >= 9)
// && trimmedInput.toLowerCase(Locale.ENGLISH).substring(0, 9).equals("timeouton")) {
// stdout.println("Enabling timeout for evaluation to 60 seconds.");
// console.fSeconds = 60;
// continue;
// } else
// if (trimmedInput.length() > 1 && trimmedInput.charAt(0) == '?') {
// Documentation.findDocumentation(stdout, trimmedInput);
// continue;
// }
String postfix = Scanner.balanceCode(trimmedInput);
if (postfix != null && postfix.length() > 0) {
stderr.println("Automatically closing brackets: " + postfix);
trimmedInput = trimmedInput + postfix;
}
stdout.println("In[" + COUNTER + "]:= " + trimmedInput);
stdout.flush();
// if (console.fPrettyPrinter) {
// console.prettyPrinter(inputExpression);
// } else {
console.resultPrinter(trimmedInput);
// }
COUNTER++;
}
// } catch (final MathRuntimeException mre) {
// Throwable me = mre.getCause();
// stdout.println(me.getMessage());
} catch (final Exception e) {
stderr.println(e.getMessage());
stderr.flush();
}
}
}
use of org.matheclipse.core.eval.exception.ReturnException in project symja_android_library by axkr.
the class VisitorReplacePart method initPatternMatcher.
private void initPatternMatcher(IAST rule, IExpr.COMPARE_TERNARY heads) {
IExpr fromPositions = rule.arg1();
try {
// try extracting an int[] array of expressions
if (fromPositions.isList()) {
IAST list = (IAST) fromPositions;
if (list.isListOfLists()) {
for (int j = 1; j < list.size(); j++) {
IAST subList = list.getAST(j);
int[] positions = new int[subList.argSize()];
for (int k = 1; k < subList.size(); k++) {
positions[k - 1] = subList.get(k).toIntDefault();
if (positions[k - 1] == Integer.MIN_VALUE) {
throw ReturnException.RETURN_FALSE;
}
if (positions[k - 1] == 0) {
offset = 0;
}
}
IPatternMatcher evalPatternMatcher = engine.evalPatternMatcher(F.Sequence(positions), rule.arg2());
this.patternMatcherList.add(evalPatternMatcher);
}
} else {
if (list.argSize() > 0) {
int[] positions = new int[list.argSize()];
for (int j = 1; j < list.size(); j++) {
positions[j - 1] = list.get(j).toIntDefault();
if (positions[j - 1] == Integer.MIN_VALUE) {
throw ReturnException.RETURN_FALSE;
}
if (positions[j - 1] == 0) {
offset = 0;
}
}
IPatternMatcher evalPatternMatcher = engine.evalPatternMatcher(F.Sequence(positions), rule.arg2());
this.patternMatcherList.add(evalPatternMatcher);
}
}
} else {
int[] positions = new int[] { rule.arg1().toIntDefault() };
if (positions[0] == Integer.MIN_VALUE) {
throw ReturnException.RETURN_FALSE;
}
if (positions[0] == 0) {
offset = 0;
}
IPatternMatcher evalPatternMatcher = engine.evalPatternMatcher(F.Sequence(positions), rule.arg2());
this.patternMatcherList.add(evalPatternMatcher);
}
} catch (ReturnException rex) {
if (fromPositions.isList()) {
IAST list = ((IAST) fromPositions).apply(S.Sequence, 1);
IPatternMatcher evalPatternMatcher = engine.evalPatternMatcher(list, rule.arg2());
this.patternMatcherList.add(evalPatternMatcher);
} else {
IPatternMatcher evalPatternMatcher = engine.evalPatternMatcher(fromPositions, rule.arg2());
this.patternMatcherList.add(evalPatternMatcher);
}
}
}
Aggregations