Search in sources :

Example 11 with PostfixOperator

use of org.matheclipse.parser.client.operator.PostfixOperator 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 {
    final boolean isOr = list.isOr();
    String operatorString = oper.getOperatorString();
    if (list.isAST2()) {
        IExpr arg1 = list.arg1();
        IExpr arg2 = list.arg2();
        if (oper.getPrecedence() < precedence) {
            append(buf, "(");
        }
        if (oper.getGrouping() == InfixOperator.RIGHT_ASSOCIATIVE && arg1.head().equals(list.head())) {
            append(buf, "(");
        } else {
            if (operatorString.equals("^")) {
                final Operator operator = getOperator(arg1.topHead());
                if (operator instanceof PostfixOperator) {
                    append(buf, "(");
                }
            }
        }
        if (isOr && arg1.isAnd()) {
            append(buf, "(");
        }
        convert(buf, arg1, oper.getPrecedence(), false);
        if (isOr && arg1.isAnd()) {
            append(buf, ")");
        }
        if (oper.getGrouping() == InfixOperator.RIGHT_ASSOCIATIVE && arg1.head().equals(list.head())) {
            append(buf, ")");
        } else {
            if (operatorString.equals("^")) {
                final Operator operator = getOperator(arg1.topHead());
                if (operator instanceof PostfixOperator) {
                    append(buf, ")");
                }
            }
        }
        appendUnicodeMapped(buf, operatorString);
        if (oper.getGrouping() == InfixOperator.LEFT_ASSOCIATIVE && arg2.head().equals(list.head())) {
            append(buf, "(");
        }
        if (isOr && arg2.isAnd()) {
            append(buf, "(");
        }
        convert(buf, arg2, oper.getPrecedence(), false);
        if (isOr && arg2.isAnd()) {
            append(buf, ")");
        }
        if (oper.getGrouping() == InfixOperator.LEFT_ASSOCIATIVE && arg2.head().equals(list.head())) {
            append(buf, ")");
        }
        if (oper.getPrecedence() < precedence) {
            append(buf, ")");
        }
        return;
    }
    if (oper.getPrecedence() < precedence) {
        append(buf, "(");
    }
    if (list.size() > 1) {
        if (isOr && list.arg1().isAnd()) {
            append(buf, "(");
        }
        convert(buf, list.arg1(), oper.getPrecedence(), false);
        if (isOr && list.arg1().isAnd()) {
            append(buf, ")");
        }
    }
    for (int i = 2; i < list.size(); i++) {
        appendUnicodeMapped(buf, operatorString);
        if (isOr && list.get(i).isAnd()) {
            append(buf, "(");
        }
        convert(buf, list.get(i), oper.getPrecedence(), false);
        if (isOr && list.get(i).isAnd()) {
            append(buf, ")");
        }
    }
    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) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 12 with PostfixOperator

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

the class MathMLFormFactory method convertOperator.

private boolean convertOperator(final org.matheclipse.parser.client.operator.Operator operator, final IAST list, final StringBuilder buf, final int precedence, ISymbol head) {
    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;
        // } else
        if (list.isAST(S.Apply)) {
            if (list.size() == 3) {
                convertInfixOperator(buf, list, ASTNodeFactory.APPLY_OPERATOR, precedence);
                return true;
            }
            if (list.size() == 4 && list.arg2().equals(F.CListC1)) {
                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 13 with PostfixOperator

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

the class DoubleFormFactory method convertOperator.

protected boolean convertOperator(final Operator operator, final IAST list, final StringBuilder buf, final int precedence, ISymbol head) {
    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(S.Plus)) {
            if (fPlusReversed) {
                convertPlusOperatorReversed(buf, list, infixOperator, precedence);
            } else {
                convertPlusOperator(buf, list, infixOperator, precedence);
            }
            return true;
        } else if (head.equals(S.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(S.Apply)) {
            if (list.size() == 3) {
                convertInfixOperator(head, buf, list, ASTNodeFactory.APPLY_OPERATOR, precedence);
                return true;
            }
            if (list.size() == 4 && list.arg2().equals(F.CListC1)) {
                convertInfixOperator(head, buf, list, ASTNodeFactory.APPLY_LEVEL_OPERATOR, precedence);
                return true;
            }
            return false;
        } else if (list.size() != 3 && infixOperator.getGrouping() != InfixOperator.NONE) {
            return false;
        }
        convertInfixOperator(head, 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)

Aggregations

PostfixOperator (org.matheclipse.parser.client.operator.PostfixOperator)13 InfixOperator (org.matheclipse.parser.client.operator.InfixOperator)12 PrefixOperator (org.matheclipse.parser.client.operator.PrefixOperator)11 Operator (org.matheclipse.parser.client.operator.Operator)7 IAST (org.matheclipse.core.interfaces.IAST)4 ISymbol (org.matheclipse.core.interfaces.ISymbol)4 IExpr (org.matheclipse.core.interfaces.IExpr)3 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)3 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)3 ASTRealMatrix (org.matheclipse.core.expression.ASTRealMatrix)2 ASTRealVector (org.matheclipse.core.expression.ASTRealVector)2 ASTSeriesData (org.matheclipse.core.expression.ASTSeriesData)2 IComplex (org.matheclipse.core.interfaces.IComplex)2 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)2 IInteger (org.matheclipse.core.interfaces.IInteger)2 ASTNode (org.matheclipse.parser.client.ast.ASTNode)2 IntList (it.unimi.dsi.fastutil.ints.IntList)1 Complex (org.hipparchus.complex.Complex)1 IAssociation (org.matheclipse.core.interfaces.IAssociation)1 INumber (org.matheclipse.core.interfaces.INumber)1