Search in sources :

Example 1 with InfixOperator

use of org.matheclipse.parser.client.operator.InfixOperator in project symja_android_library by axkr.

the class OutputFormFactory method convertOperator.

private boolean convertOperator(final Operator operator, final IAST list, final Appendable buf, final int precedence, ISymbol head) throws IOException {
    if ((operator instanceof PrefixOperator) && (list.isAST1())) {
        convertPrefixOperator(buf, list, (PrefixOperator) operator, precedence);
        return true;
    }
    if ((operator instanceof InfixOperator) && (list.size() > 2)) {
        InfixOperator infixOperator = (InfixOperator) operator;
        if (head.equals(F.Plus)) {
            if (fPlusReversed) {
                convertPlusOperatorReversed(buf, list, infixOperator, precedence);
            } else {
                convertPlusOperator(buf, list, infixOperator, precedence);
            }
            return true;
        } else if (head.equals(F.Times)) {
            convertTimesFraction(buf, list, infixOperator, precedence, NO_PLUS_CALL);
            return true;
        } else if (list.isPower()) {
            convertPowerOperator(buf, list, infixOperator, precedence);
            return true;
        } else if (list.isAST(F.Apply)) {
            if (list.size() == 3) {
                convertInfixOperator(buf, list, ASTNodeFactory.APPLY_OPERATOR, precedence);
                return true;
            }
            if (list.size() == 4 && list.get(2).equals(F.List(F.C1))) {
                convertInfixOperator(buf, list, ASTNodeFactory.APPLY_LEVEL_OPERATOR, precedence);
                return true;
            }
            return false;
        } else if (list.size() != 3 && infixOperator.getGrouping() != InfixOperator.NONE) {
            return false;
        }
        convertInfixOperator(buf, list, (InfixOperator) operator, precedence);
        return true;
    }
    if ((operator instanceof PostfixOperator) && (list.isAST1())) {
        convertPostfixOperator(buf, list, (PostfixOperator) operator, precedence);
        return true;
    }
    return false;
}
Also used : PrefixOperator(org.matheclipse.parser.client.operator.PrefixOperator) PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) InfixOperator(org.matheclipse.parser.client.operator.InfixOperator)

Example 2 with InfixOperator

use of org.matheclipse.parser.client.operator.InfixOperator in project symja_android_library by axkr.

the class OutputFormFactory method convertPlusArgument.

public void convertPlusArgument(final Appendable buf, IExpr plusArg, boolean caller) throws IOException {
    if (plusArg.isTimes()) {
        final IAST timesAST = (IAST) plusArg;
        // IExpr arg1 = timesAST.arg1();
        final InfixOperator TIMES_OPERATOR = (InfixOperator) ASTNodeFactory.MMA_STYLE_FACTORY.get("Times");
        convertTimesFraction(buf, timesAST, TIMES_OPERATOR, ASTNodeFactory.TIMES_PRECEDENCE, caller);
    } else {
        if (plusArg.isNegativeSigned()) {
            // special case negative number or -Infinity...
            convert(buf, plusArg);
        } else {
            if (caller == PLUS_CALL) {
                append(buf, "+");
            }
            convert(buf, plusArg, ASTNodeFactory.PLUS_PRECEDENCE, false);
        }
    }
}
Also used : IAST(org.matheclipse.core.interfaces.IAST) InfixOperator(org.matheclipse.parser.client.operator.InfixOperator)

Example 3 with InfixOperator

use of org.matheclipse.parser.client.operator.InfixOperator in project symja_android_library by axkr.

the class Parser method parseLookaheadOperator.

private ASTNode parseLookaheadOperator(final int min_precedence) {
    ASTNode rhs = parsePrimary();
    while (true) {
        final int lookahead = fToken;
        if (fToken == TT_NEWLINE) {
            return rhs;
        }
        if ((fToken == TT_LIST_OPEN) || (fToken == TT_PRECEDENCE_OPEN) || (fToken == TT_IDENTIFIER) || (fToken == TT_STRING) || (fToken == TT_DIGIT) || (fToken == TT_SLOT)) {
            // lazy evaluation of multiplication
            InfixOperator timesOperator = (InfixOperator) fFactory.get("Times");
            if (timesOperator.getPrecedence() > min_precedence) {
                rhs = parseExpression(rhs, timesOperator.getPrecedence());
                continue;
            } else if ((timesOperator.getPrecedence() == min_precedence) && (timesOperator.getGrouping() == InfixOperator.RIGHT_ASSOCIATIVE)) {
                rhs = parseExpression(rhs, timesOperator.getPrecedence());
                continue;
            }
        } else {
            if (fToken == TT_DERIVATIVE) {
                rhs = parseDerivative(rhs);
            }
            if (lookahead != TT_OPERATOR) {
                break;
            }
            InfixOperator infixOperator = determineBinaryOperator();
            if (infixOperator != null) {
                if (infixOperator.getPrecedence() > min_precedence || ((infixOperator.getPrecedence() == min_precedence) && (infixOperator.getGrouping() == InfixOperator.RIGHT_ASSOCIATIVE))) {
                    if (";".equals(infixOperator.getOperatorString())) {
                        if (fPackageMode && fRecursionDepth < 1) {
                            return infixOperator.createFunction(fFactory, rhs, fFactory.createSymbol("Null"));
                        }
                    }
                    rhs = parseExpression(rhs, infixOperator.getPrecedence());
                    continue;
                }
            } else {
                PostfixOperator postfixOperator = determinePostfixOperator();
                if (postfixOperator != null) {
                    if (postfixOperator.getPrecedence() >= min_precedence) {
                        getNextToken();
                        rhs = postfixOperator.createFunction(fFactory, rhs);
                        continue;
                    }
                }
            }
        }
        break;
    }
    return rhs;
}
Also used : PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) ASTNode(org.matheclipse.parser.client.ast.ASTNode) InfixOperator(org.matheclipse.parser.client.operator.InfixOperator)

Example 4 with InfixOperator

use of org.matheclipse.parser.client.operator.InfixOperator in project symja_android_library by axkr.

the class OutputFormFactory method convertInfixOperator.

public void convertInfixOperator(final Appendable buf, final IAST list, final InfixOperator oper, final int precedence) throws IOException {
    if (list.isAST2()) {
        if (oper.getPrecedence() < precedence) {
            append(buf, "(");
        }
        if (oper.getGrouping() == InfixOperator.RIGHT_ASSOCIATIVE && list.arg1().head().equals(list.head())) {
            append(buf, "(");
        } else {
            if (oper.getOperatorString() == "^") {
                final Operator operator = getOperator(list.arg1().topHead());
                if (operator instanceof PostfixOperator) {
                    append(buf, "(");
                }
            }
        }
        convert(buf, list.arg1(), oper.getPrecedence(), false);
        if (oper.getGrouping() == InfixOperator.RIGHT_ASSOCIATIVE && list.arg1().head().equals(list.head())) {
            append(buf, ")");
        } else {
            if (oper.getOperatorString() == "^") {
                final Operator operator = getOperator(list.arg1().topHead());
                if (operator instanceof PostfixOperator) {
                    append(buf, ")");
                }
            }
        }
        append(buf, oper.getOperatorString());
        if (oper.getGrouping() == InfixOperator.LEFT_ASSOCIATIVE && list.arg2().head().equals(list.head())) {
            append(buf, "(");
        }
        convert(buf, list.arg2(), oper.getPrecedence(), false);
        if (oper.getGrouping() == InfixOperator.LEFT_ASSOCIATIVE && list.arg2().head().equals(list.head())) {
            append(buf, ")");
        }
        if (oper.getPrecedence() < precedence) {
            append(buf, ")");
        }
        return;
    }
    if (oper.getPrecedence() < precedence) {
        append(buf, "(");
    }
    if (list.size() > 1) {
        convert(buf, list.arg1(), oper.getPrecedence(), false);
    }
    for (int i = 2; i < list.size(); i++) {
        append(buf, oper.getOperatorString());
        convert(buf, list.get(i), oper.getPrecedence(), false);
    }
    if (oper.getPrecedence() < precedence) {
        append(buf, ")");
    }
}
Also used : PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) Operator(org.matheclipse.parser.client.operator.Operator) InfixOperator(org.matheclipse.parser.client.operator.InfixOperator) PrefixOperator(org.matheclipse.parser.client.operator.PrefixOperator) PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator)

Example 5 with InfixOperator

use of org.matheclipse.parser.client.operator.InfixOperator 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)

Aggregations

InfixOperator (org.matheclipse.parser.client.operator.InfixOperator)5 PostfixOperator (org.matheclipse.parser.client.operator.PostfixOperator)4 PrefixOperator (org.matheclipse.parser.client.operator.PrefixOperator)3 ASTNode (org.matheclipse.parser.client.ast.ASTNode)2 Operator (org.matheclipse.parser.client.operator.Operator)2 IAST (org.matheclipse.core.interfaces.IAST)1 FunctionNode (org.matheclipse.parser.client.ast.FunctionNode)1