use of org.matheclipse.core.parser.ExprParser in project symja_android_library by axkr.
the class Functors method rules.
/**
* Create a functor from the given rules. All strings in
* <code>strRules</code> are parsed in internal rules form.
*
* @param strRules
* array of rules of the form "<code>x->y</code>"
* @return
* @throws WrongArgumentType
*/
public static Function<IExpr, IExpr> rules(@Nonnull String[] strRules) throws WrongArgumentType {
IAST astRules = F.ListAlloc(strRules.length);
ExprParser parser = new ExprParser(EvalEngine.get());
// final Parser parser = new Parser();
final EvalEngine engine = EvalEngine.get();
for (String str : strRules) {
IExpr expr = parser.parse(str);
// final ASTNode parsedAST = parser.parse(str);
// IExpr expr = AST2Expr.CONST.convert(parsedAST, engine);
expr = engine.evaluate(expr);
astRules.append(expr);
}
return rules(astRules);
}
use of org.matheclipse.core.parser.ExprParser in project symja_android_library by axkr.
the class HashedOrderlessMatcher method defineHashRule.
public void defineHashRule(final String lhs1Str, final String lhs2Str, final BinaryFunctorImpl<IExpr> function) {
ExprParser parser = new ExprParser(EvalEngine.get());
IExpr lhs1 = parser.parse(lhs1Str);
// final Parser parser = new Parser();
// ASTNode parsedAST = parser.parse(lhs1Str);
// final IExpr lhs1 = AST2Expr.CONST.convert(parsedAST);
IExpr lhs2 = parser.parse(lhs2Str);
// parsedAST = parser.parse(lhs2Str);
// final IExpr lhs2 = AST2Expr.CONST.convert(parsedAST);
defineHashRule(lhs1, lhs2, function);
}
use of org.matheclipse.core.parser.ExprParser in project symja_android_library by axkr.
the class EvalUtilities method eval.
/**
* Evaluate the <code>inputExpression</code> and return the resulting
* expression. <br/>
* The parser first tries (independently from the settings in the
* <code>evalEngine</code>) to parse the expression in the "relaxed
* mode" (i.e. "common math expression syntax" with
* parentheses for function arguments) and if that results in a
* <code>SyntaxError</code> exception it tries to parse in the
* "stronger mode" (i.e. with square brackets for function
* arguments). <br />
* <b>Note</B> that after the second parser step this method may also throw
* a <code>SyntaxError</code> exception.
*
* @param inputExpression
* the expression which should be evaluated.
* @param evalEngine
* the evaluation engine which should be used
* @return <code>F.NIL</code>, if the inputExpression is <code>null</code>
* @throw org.matheclipse.parser.client.SyntaxError
* @throw org.matheclipse.parser.client.math.MathException
*/
public static IExpr eval(final String inputExpression, final EvalEngine evalEngine) throws MathException {
if (inputExpression != null) {
EvalEngine.set(evalEngine);
boolean SIMPLE_SYNTAX = true;
IExpr parsedExpression;
try {
ExprParser parser = new ExprParser(evalEngine, SIMPLE_SYNTAX);
parsedExpression = parser.parse(inputExpression);
} catch (SyntaxError se1) {
try {
SIMPLE_SYNTAX = false;
ExprParser parser = new ExprParser(evalEngine, SIMPLE_SYNTAX);
parsedExpression = parser.parse(inputExpression);
} catch (SyntaxError se2) {
throw se1;
}
}
if (parsedExpression != null) {
F.join();
evalEngine.reset();
IExpr temp = evalEngine.evaluate(parsedExpression);
evalEngine.addOut(temp);
return temp;
}
}
return F.NIL;
}
use of org.matheclipse.core.parser.ExprParser in project symja_android_library by axkr.
the class EvalUtilities method toJavaForm.
/**
* Converts the inputExpression string into a MathML expression and writes
* the result to the given <code>Writer</code>
*
* @param inputExpression
* @param out
*/
public String toJavaForm(final String inputExpression) throws MathException {
if (inputExpression != null) {
EvalEngine.set(fEvalEngine);
fEvalEngine.reset();
ExprParser parser = new ExprParser(fEvalEngine);
IExpr parsedExpression = parser.parse(inputExpression);
// parsedExpression = AST2Expr.CONST.convert(node, fEvalEngine);
return parsedExpression.internalFormString(false, 0);
}
return "";
}
use of org.matheclipse.core.parser.ExprParser in project symja_android_library by axkr.
the class SymjaInterpreter method interpreter.
/**
* Parse the <code>codeString</code> into an <code>IExpr</code> and if
* <code>function</code> unequals <code>null</code>, replace all occurences
* of slot <code>#</code> in the function with the parsed expression. After
* that evaluate the given expression.
*
* @param function
* @return
*/
public String interpreter(IAST function) {
String evalStr = codeString;
IExpr expr;
EvalEngine engine = EvalEngine.get();
try {
ExprParser p = new ExprParser(engine, true);
// throws SyntaxError exception, if syntax isn't valid
expr = p.parse(evalStr);
} catch (SyntaxError e1) {
try {
ExprParser p = new ExprParser(engine);
// throws SyntaxError exception, if syntax isn't valid
expr = p.parse(evalStr);
} catch (Exception e2) {
outStream.println(e2.getMessage());
return "";
}
}
IExpr result;
final StringBuilder buf = new StringBuilder();
try {
if (function != null) {
expr = function.replaceAll(F.Rule(F.Slot1, expr));
}
if (expr.isPresent()) {
result = evaluate(expr);
if (result.isPresent()) {
if (result.equals(F.Null)) {
return buf.toString();
}
OutputFormFactory.get(true).convert(buf, result);
}
return buf.toString();
}
} catch (final RuntimeException re) {
Throwable me = re.getCause();
if (me instanceof MathException) {
Validate.printException(buf, me);
} else {
Validate.printException(buf, re);
}
} catch (final Exception e) {
Validate.printException(buf, e);
}
return buf.toString();
}
Aggregations