Search in sources :

Example 1 with ExprParser

use of org.matheclipse.core.parser.ExprParser in project symja_android_library by axkr.

the class MathMLUtilities method toMathML.

/**
	 * Converts the inputExpression string into a MathML expression and writes
	 * the result to the given <code>Writer</code>
	 * 
	 * @param inputExpression
	 * @param out
	 */
public synchronized void toMathML(final String inputExpression, final Writer out) {
    IExpr parsedExpression = null;
    // ASTNode node;
    if (inputExpression != null) {
        try {
            ExprParser parser = new ExprParser(fEvalEngine);
            parsedExpression = parser.parse(inputExpression);
        // node = fEvalEngine.parseNode(inputExpression);
        // parsedExpression = AST2Expr.CONST.convert(node, fEvalEngine);
        } catch (final Throwable e) {
            return;
        // parsedExpression == null ==> fError occured
        }
    }
    toMathML(parsedExpression, out);
}
Also used : IExpr(org.matheclipse.core.interfaces.IExpr) ExprParser(org.matheclipse.core.parser.ExprParser)

Example 2 with ExprParser

use of org.matheclipse.core.parser.ExprParser in project symja_android_library by axkr.

the class MathMLUtilities method toJava.

public synchronized void toJava(final String inputExpression, final Writer out, boolean strictJava) {
    IExpr parsedExpression = null;
    // ASTNode node;
    if (inputExpression != null) {
        try {
            ExprParser parser = new ExprParser(fEvalEngine);
            parsedExpression = parser.parse(inputExpression);
            // node = fEvalEngine.parseNode(inputExpression);
            // parsedExpression = AST2Expr.CONST.convert(node, fEvalEngine);
            out.write(parsedExpression.internalFormString(strictJava, 0));
        } catch (final Throwable e) {
            return;
        // parsedExpression == null ==> fError occured
        }
    }
}
Also used : IExpr(org.matheclipse.core.interfaces.IExpr) ExprParser(org.matheclipse.core.parser.ExprParser)

Example 3 with ExprParser

use of org.matheclipse.core.parser.ExprParser in project symja_android_library by axkr.

the class SymjaInterpreter method interpreter.

/**
	 * Evaluate the expression assigned to this interpreter.
	 * 
	 * @param function
	 *            <code>null</code> if you like to evaluate in symbolic mode;
	 *            &quot;N&quot; if you like to evaluate in numeric mode
	 * @return
	 */
public String interpreter(String 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
        if (function != null) {
            evalStr = function + "(" + codeString + ")";
        }
        expr = p.parse(evalStr);
    } catch (SyntaxError e1) {
        try {
            ExprParser p = new ExprParser(engine);
            // throws SyntaxError exception, if syntax isn't valid
            if (function != null) {
                evalStr = function + "[" + codeString + "]";
            }
            expr = p.parse(evalStr);
        } catch (Exception e2) {
            outStream.println(e2.getMessage());
            return "";
        }
    }
    IExpr result;
    final StringBuilder buf = new StringBuilder();
    try {
        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 ex) {
        Validate.printException(buf, ex);
    } catch (final StackOverflowError soe) {
        Validate.printException(buf, soe);
    } catch (final OutOfMemoryError oome) {
        Validate.printException(buf, oome);
    }
    return buf.toString();
}
Also used : SyntaxError(org.matheclipse.parser.client.SyntaxError) MathException(org.matheclipse.parser.client.math.MathException) IExpr(org.matheclipse.core.interfaces.IExpr) ExprParser(org.matheclipse.core.parser.ExprParser) MathException(org.matheclipse.parser.client.math.MathException)

Example 4 with ExprParser

use of org.matheclipse.core.parser.ExprParser in project symja_android_library by axkr.

the class ExprEvaluator method toScalaForm.

/**
	 * Converts the inputExpression string into a Scala expression and writes the result to the given
	 * <code>Writer</code>string.
	 * 
	 * @param inputExpression
	 * @param out
	 */
public String toScalaForm(final String inputExpression) throws MathException {
    IExpr parsedExpression;
    if (inputExpression != null) {
        ExprParser parser = new ExprParser(engine);
        parsedExpression = parser.parse(inputExpression);
        return parsedExpression.internalScalaString(false, 0);
    }
    return "";
}
Also used : IExpr(org.matheclipse.core.interfaces.IExpr) ExprParser(org.matheclipse.core.parser.ExprParser)

Example 5 with ExprParser

use of org.matheclipse.core.parser.ExprParser in project symja_android_library by axkr.

the class ExprEvaluator method toJavaForm.

/**
	 * Converts the inputExpression string into a Java Symja expression string.
	 * 
	 * @param inputExpression
	 * @param out
	 */
public String toJavaForm(final String inputExpression) throws MathException {
    IExpr parsedExpression;
    if (inputExpression != null) {
        ExprParser parser = new ExprParser(engine);
        parsedExpression = parser.parse(inputExpression);
        return parsedExpression.internalFormString(false, 0);
    }
    return "";
}
Also used : IExpr(org.matheclipse.core.interfaces.IExpr) ExprParser(org.matheclipse.core.parser.ExprParser)

Aggregations

IExpr (org.matheclipse.core.interfaces.IExpr)11 ExprParser (org.matheclipse.core.parser.ExprParser)11 SyntaxError (org.matheclipse.parser.client.SyntaxError)3 MathException (org.matheclipse.parser.client.math.MathException)2 EvalEngine (org.matheclipse.core.eval.EvalEngine)1 IAST (org.matheclipse.core.interfaces.IAST)1