Search in sources :

Example 51 with IAST

use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.

the class ExprParser method parseExpression.

/**
	 * See <a href="http://en.wikipedia.org/wiki/Operator-precedence_parser"> Operator -precedence parser</a> for the
	 * idea, how to parse the operators depending on their precedence.
	 * 
	 * @param lhs
	 *            the already parsed left-hand-side of the operator
	 * @param min_precedence
	 * @return
	 */
private IExpr parseExpression(IExpr lhs, final int min_precedence) {
    IExpr rhs = null;
    AbstractExprOperator oper;
    InfixExprOperator infixOperator;
    PostfixExprOperator postfixOperator;
    while (true) {
        if (fToken == TT_NEWLINE) {
            return lhs;
        }
        if ((fToken == TT_LIST_OPEN) || (fToken == TT_PRECEDENCE_OPEN) || (fToken == TT_IDENTIFIER) || (fToken == TT_STRING) || (fToken == TT_DIGIT) || (fToken == TT_SLOT) || (fToken == TT_SLOTSEQUENCE)) {
            // if (fPackageMode && fRecursionDepth < 1) {
            // return lhs;
            // }
            // if (fPackageMode && fToken == TT_IDENTIFIER && fLastChar ==
            // '\n') {
            // return lhs;
            // }
            // lazy evaluation of multiplication
            oper = fFactory.get("Times");
            if (oper.getPrecedence() >= min_precedence) {
                rhs = parseLookaheadOperator(oper.getPrecedence());
                lhs = F.$(F.$s(oper.getFunctionName()), lhs, rhs);
                // lhs, rhs);
                continue;
            }
        } else {
            if (fToken == TT_DERIVATIVE) {
                lhs = parseDerivative(lhs);
            }
            if (fToken != TT_OPERATOR) {
                break;
            }
            infixOperator = determineBinaryOperator();
            if (infixOperator != null) {
                if (infixOperator.getPrecedence() >= min_precedence) {
                    getNextToken();
                    IExpr compoundExpressionNull = parseCompoundExpressionNull(infixOperator, lhs);
                    if (compoundExpressionNull != null) {
                        return compoundExpressionNull;
                    }
                    while (fToken == TT_NEWLINE) {
                        getNextToken();
                    }
                    rhs = parseLookaheadOperator(infixOperator.getPrecedence());
                    lhs = createInfixFunction(infixOperator, lhs, rhs);
                    while (fToken == TT_OPERATOR && infixOperator.getGrouping() == InfixOperator.NONE && infixOperator.getOperatorString().equals(fOperatorString)) {
                        getNextToken();
                        rhs = parseLookaheadOperator(infixOperator.getPrecedence());
                        ((IAST) lhs).append(rhs);
                    }
                    continue;
                }
            } else {
                postfixOperator = determinePostfixOperator();
                if (postfixOperator != null) {
                    if (postfixOperator.getPrecedence() >= min_precedence) {
                        getNextToken();
                        lhs = postfixOperator.createFunction(fFactory, lhs);
                        lhs = parseArguments(lhs);
                        continue;
                    }
                } else {
                    throwSyntaxError("Operator: " + fOperatorString + " is no infix or postfix operator.");
                }
            }
        }
        break;
    }
    return lhs;
}
Also used : IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 52 with IAST

use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.

the class ExprParser method parsePrimary.

private IExpr parsePrimary() {
    if (fToken == TT_OPERATOR) {
        if (";;".equals(fOperatorString)) {
            IAST span = F.ast(F.Span);
            span.append(F.C1);
            getNextToken();
            if (fToken == TT_COMMA || fToken == TT_ARGUMENTS_CLOSE || fToken == TT_PRECEDENCE_CLOSE) {
                span.append(F.All);
                return span;
            }
            if (fToken == TT_OPERATOR && ";;".equals(fOperatorString)) {
                span.append(F.All);
                getNextToken();
            }
            span.append(parsePrimary());
            return span;
        }
        if (fOperatorString.equals(".")) {
            fCurrentChar = '.';
            // return getPart();
            return getNumber(false);
        }
        final PrefixExprOperator prefixOperator = determinePrefixOperator();
        if (prefixOperator != null) {
            getNextToken();
            final IExpr temp = parseLookaheadOperator(prefixOperator.getPrecedence());
            if (prefixOperator.getFunctionName().equals("PreMinus")) {
                // special cases for negative numbers
                if (temp.isNumber()) {
                    return temp.negate();
                }
            }
            return prefixOperator.createFunction(fFactory, temp);
        }
        throwSyntaxError("Operator: " + fOperatorString + " is no prefix operator.");
    }
    return getPart();
}
Also used : IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 53 with IAST

use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.

the class ExprParser method parseDerivative.

/**
	 * Parse expressions like <code>expr''[x]</code>
	 * 
	 * @param expr
	 * @return
	 */
private IExpr parseDerivative(IExpr expr) {
    int derivativeCounter = 1;
    getNextToken();
    while (fToken == TT_DERIVATIVE) {
        derivativeCounter++;
        getNextToken();
    }
    IAST deriv = F.$(DERIVATIVE, F.integer(derivativeCounter));
    expr = F.$(deriv, expr);
    expr = parseArguments(expr);
    return expr;
}
Also used : IAST(org.matheclipse.core.interfaces.IAST) Apint(org.apfloat.Apint)

Example 54 with IAST

use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.

the class ExprParser method getFunctionArguments.

/**
	 * Get a function f[...][...]
	 * 
	 */
IAST getFunctionArguments(final IExpr head) throws SyntaxError {
    final IAST function = F.ast(head);
    fRecursionDepth++;
    try {
        getNextToken();
        if (fToken == TT_ARGUMENTS_CLOSE) {
            getNextToken();
            if (fToken == TT_ARGUMENTS_OPEN) {
                return getFunctionArguments(function);
            }
            return function;
        }
        getArguments(function);
    } finally {
        fRecursionDepth--;
    }
    if (fToken == TT_ARGUMENTS_CLOSE) {
        getNextToken();
        if (fToken == TT_ARGUMENTS_OPEN) {
            return getFunctionArguments(function);
        }
        return function;
    }
    throwSyntaxError("']' expected.");
    return null;
}
Also used : IAST(org.matheclipse.core.interfaces.IAST)

Example 55 with IAST

use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.

the class ExprParser method getList.

/**
	 * Get a list {...}
	 * 
	 */
private IExpr getList() throws SyntaxError {
    // fFactory.createFunction(fFactory.createSymbol(IConstantOperators.List));
    final IAST function = F.ListAlloc(10);
    getNextToken();
    if (fToken == TT_LIST_CLOSE) {
        getNextToken();
        return function;
    }
    fRecursionDepth++;
    try {
        getArguments(function);
    } finally {
        fRecursionDepth--;
    }
    if (fToken == TT_LIST_CLOSE) {
        getNextToken();
        return function;
    }
    throwSyntaxError("'}' expected.");
    return null;
}
Also used : IAST(org.matheclipse.core.interfaces.IAST)

Aggregations

IAST (org.matheclipse.core.interfaces.IAST)413 IExpr (org.matheclipse.core.interfaces.IExpr)248 ISymbol (org.matheclipse.core.interfaces.ISymbol)76 IInteger (org.matheclipse.core.interfaces.IInteger)34 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)30 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)22 ExpVector (edu.jas.poly.ExpVector)15 ArrayList (java.util.ArrayList)14 BigRational (edu.jas.arith.BigRational)13 JASIExpr (org.matheclipse.core.convert.JASIExpr)13 VariablesSet (org.matheclipse.core.convert.VariablesSet)13 INum (org.matheclipse.core.interfaces.INum)13 ExprPolynomial (org.matheclipse.core.polynomials.ExprPolynomial)12 GenPolynomial (edu.jas.poly.GenPolynomial)11 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)11 IFraction (org.matheclipse.core.interfaces.IFraction)11 INumber (org.matheclipse.core.interfaces.INumber)11 IComplex (org.matheclipse.core.interfaces.IComplex)10 ModLong (edu.jas.arith.ModLong)9 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)9