Search in sources :

Example 16 with FunctionNode

use of org.matheclipse.parser.client.ast.FunctionNode in project symja_android_library by axkr.

the class Parser 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 ASTNode parseExpression(ASTNode lhs, final int min_precedence) {
    ASTNode rhs;
    Operator oper;
    InfixOperator infixOperator;
    PostfixOperator 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)) {
            // lazy evaluation of multiplication
            oper = fFactory.get("Times");
            if (oper.getPrecedence() >= min_precedence) {
                rhs = parseLookaheadOperator(oper.getPrecedence());
                lhs = fFactory.createFunction(fFactory.createSymbol(oper.getFunctionName()), 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();
                    ASTNode compoundExpressionNull = parseCompoundExpressionNull(infixOperator, lhs);
                    if (compoundExpressionNull != null) {
                        return compoundExpressionNull;
                    }
                    while (fToken == TT_NEWLINE) {
                        getNextToken();
                    }
                    rhs = parseLookaheadOperator(infixOperator.getPrecedence());
                    lhs = infixOperator.createFunction(fFactory, lhs, rhs);
                    while (fToken == TT_OPERATOR && infixOperator.getGrouping() == InfixOperator.NONE && infixOperator.getOperatorString().equals(fOperatorString)) {
                        getNextToken();
                        rhs = parseLookaheadOperator(infixOperator.getPrecedence());
                        ((FunctionNode) lhs).add(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 : InfixOperator(org.matheclipse.parser.client.operator.InfixOperator) PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) PrefixOperator(org.matheclipse.parser.client.operator.PrefixOperator) Operator(org.matheclipse.parser.client.operator.Operator) PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) ASTNode(org.matheclipse.parser.client.ast.ASTNode) FunctionNode(org.matheclipse.parser.client.ast.FunctionNode) InfixOperator(org.matheclipse.parser.client.operator.InfixOperator)

Example 17 with FunctionNode

use of org.matheclipse.parser.client.ast.FunctionNode in project symja_android_library by axkr.

the class Parser method getFunction.

/**
	 * Get a function f[...][...]
	 * 
	 */
FunctionNode getFunction(final ASTNode head) throws SyntaxError {
    final FunctionNode function = fFactory.createAST(head);
    getNextToken();
    if (fRelaxedSyntax) {
        if (fToken == TT_PRECEDENCE_CLOSE) {
            getNextToken();
            if (fToken == TT_PRECEDENCE_OPEN) {
                return function;
            }
            if (fToken == TT_ARGUMENTS_OPEN) {
                return getFunctionArguments(function);
            }
            return function;
        }
    } else {
        if (fToken == TT_ARGUMENTS_CLOSE) {
            getNextToken();
            if (fToken == TT_ARGUMENTS_OPEN) {
                return getFunctionArguments(function);
            }
            return function;
        }
    }
    fRecursionDepth++;
    try {
        getArguments(function);
    } finally {
        fRecursionDepth--;
    }
    if (fRelaxedSyntax) {
        if (fToken == TT_PRECEDENCE_CLOSE) {
            getNextToken();
            if (fToken == TT_PRECEDENCE_OPEN) {
                return function;
            }
            if (fToken == TT_ARGUMENTS_OPEN) {
                return getFunctionArguments(function);
            }
            return function;
        }
    } else {
        if (fToken == TT_ARGUMENTS_CLOSE) {
            getNextToken();
            if (fToken == TT_ARGUMENTS_OPEN) {
                return getFunctionArguments(function);
            }
            return function;
        }
    }
    if (fRelaxedSyntax) {
        throwSyntaxError("')' expected.");
    } else {
        throwSyntaxError("']' expected.");
    }
    return null;
}
Also used : FunctionNode(org.matheclipse.parser.client.ast.FunctionNode)

Example 18 with FunctionNode

use of org.matheclipse.parser.client.ast.FunctionNode in project symja_android_library by axkr.

the class Parser method getTimes.

private ASTNode getTimes(ASTNode temp) throws SyntaxError {
    FunctionNode func = fFactory.createAST(new SymbolNode("Times"));
    func.add(temp);
    do {
        getNextToken();
        temp = parseExpression();
        func.add(temp);
        if (fToken != TT_PRECEDENCE_CLOSE) {
            throwSyntaxError("\')\' expected.");
        }
        getNextToken();
    } while (fToken == TT_PRECEDENCE_OPEN);
    return func;
}
Also used : SymbolNode(org.matheclipse.parser.client.ast.SymbolNode) FunctionNode(org.matheclipse.parser.client.ast.FunctionNode)

Aggregations

FunctionNode (org.matheclipse.parser.client.ast.FunctionNode)18 ASTNode (org.matheclipse.parser.client.ast.ASTNode)8 SymbolNode (org.matheclipse.parser.client.ast.SymbolNode)6 NumberNode (org.matheclipse.parser.client.ast.NumberNode)3 IAST (org.matheclipse.core.interfaces.IAST)2 IExpr (org.matheclipse.core.interfaces.IExpr)2 Parser (org.matheclipse.parser.client.Parser)2 IntegerNode (org.matheclipse.parser.client.ast.IntegerNode)2 ArithmeticMathException (org.matheclipse.parser.client.math.ArithmeticMathException)2 PrefixOperator (org.matheclipse.parser.client.operator.PrefixOperator)2 Apfloat (org.apfloat.Apfloat)1 Apint (org.apfloat.Apint)1 AST2Expr (org.matheclipse.core.convert.AST2Expr)1 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)1 IInteger (org.matheclipse.core.interfaces.IInteger)1 INumber (org.matheclipse.core.interfaces.INumber)1 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)1 ISymbol (org.matheclipse.core.interfaces.ISymbol)1 FloatNode (org.matheclipse.parser.client.ast.FloatNode)1 FractionNode (org.matheclipse.parser.client.ast.FractionNode)1