Search in sources :

Example 1 with PostfixOperator

use of org.matheclipse.parser.client.operator.PostfixOperator 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 2 with PostfixOperator

use of org.matheclipse.parser.client.operator.PostfixOperator 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(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(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 3 with PostfixOperator

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

the class MathMLFormFactory method convertAST.

private void convertAST(final StringBuilder buf, final IAST ast, final int precedence) {
    final IAST list = ast;
    IExpr header = list.head();
    if (!header.isSymbol()) {
        // print expressions like: f(#1, y)& [x]
        IAST[] derivStruct = list.isDerivativeAST1();
        if (derivStruct != null) {
            IAST a1Head = derivStruct[0];
            IAST headAST = derivStruct[1];
            if (a1Head.isAST1() && headAST.isAST1() && (headAST.arg1().isSymbol() || headAST.arg1().isAST())) {
                try {
                    int n = a1Head.arg1().toIntDefault();
                    if (n == 1 || n == 2) {
                        tagStart(buf, "mrow");
                        tagStart(buf, "msup");
                        IExpr symbolOrAST = headAST.arg1();
                        convertInternal(buf, symbolOrAST, Integer.MAX_VALUE, false);
                        if (n == 1) {
                            tag(buf, "mo", "&#8242;");
                        } else if (n == 2) {
                            tag(buf, "mo", "&#8242;&#8242;");
                        }
                        tagEnd(buf, "msup");
                        if (derivStruct[2] != null) {
                            convertArgs(buf, symbolOrAST, list);
                        }
                        tagEnd(buf, "mrow");
                        return;
                    }
                    tagStart(buf, "mrow");
                    IExpr symbolOrAST = headAST.arg1();
                    tagStart(buf, "msup");
                    convertInternal(buf, symbolOrAST, Integer.MAX_VALUE, false);
                    tagStart(buf, "mrow");
                    tag(buf, "mo", "(");
                    // supscript "(n)"
                    convertInternal(buf, a1Head.arg1(), Integer.MIN_VALUE, false);
                    tag(buf, "mo", ")");
                    tagEnd(buf, "mrow");
                    tagEnd(buf, "msup");
                    if (derivStruct[2] != null) {
                        convertArgs(buf, symbolOrAST, list);
                    }
                    tagEnd(buf, "mrow");
                    return;
                } catch (ArithmeticException ae) {
                }
            }
        }
        convertInternal(buf, header, Integer.MIN_VALUE, false);
        convertFunctionArgs(buf, list);
        return;
    }
    ISymbol head = list.topHead();
    final org.matheclipse.parser.client.operator.Operator operator = OutputFormFactory.getOperator(head);
    if (operator != null) {
        if (operator instanceof PostfixOperator) {
            if (list.isAST1()) {
                convertPostfixOperator(buf, list, (PostfixOperator) operator, operator.getPrecedence());
                return;
            }
        } else {
            if (convertOperator(operator, list, buf, operator.getPrecedence(), head)) {
                return;
            }
        }
    }
    if (list instanceof ASTSeriesData) {
        if (convertSeriesData(buf, (ASTSeriesData) list, precedence)) {
            return;
        }
    }
    if (list.isList() || list instanceof ASTRealVector || list instanceof ASTRealMatrix) {
        convertList(buf, list);
        return;
    }
    if (list.isAST(S.Parenthesis)) {
        convertArgs(buf, S.Parenthesis, list);
        return;
    }
    if (list.isInterval() && convertInterval(buf, list)) {
        return;
    }
    if (list.isAssociation()) {
        convertAssociation(buf, (IAssociation) list);
        return;
    }
    int functionID = ((ISymbol) list.head()).ordinal();
    if (functionID > ID.UNKNOWN) {
        switch(functionID) {
            case ID.Inequality:
                if (list.size() > 3 && convertInequality(buf, list, precedence)) {
                    return;
                }
                break;
            case ID.Part:
                if ((list.size() >= 3)) {
                    convertPart(buf, list);
                    return;
                }
                break;
            case ID.Slot:
                if ((list.isAST1()) && (list.arg1() instanceof IInteger)) {
                    convertSlot(buf, list);
                    return;
                }
                break;
            case ID.SlotSequence:
                if ((list.isAST1()) && (list.arg1() instanceof IInteger)) {
                    convertSlotSequence(buf, list);
                    return;
                }
                break;
            case ID.SparseArray:
                if (list.isSparseArray()) {
                    tagStart(buf, "mtext");
                    buf.append(list.toString());
                    tagEnd(buf, "mtext");
                    return;
                }
                break;
            case ID.Defer:
            case ID.HoldForm:
                if ((list.isAST1())) {
                    convertInternal(buf, list.arg1(), precedence, false);
                    return;
                }
                break;
            case ID.DirectedInfinity:
                if (list.isDirectedInfinity()) {
                    // head.equals(F.DirectedInfinity))
                    if (list.isAST0()) {
                        convertSymbol(buf, S.ComplexInfinity);
                        return;
                    }
                    if (list.isAST1()) {
                        if (list.arg1().isOne()) {
                            convertSymbol(buf, S.Infinity);
                            return;
                        } else if (list.arg1().isMinusOne()) {
                            convertInternal(buf, F.Times(F.CN1, S.Infinity), precedence, false);
                            return;
                        } else if (list.arg1().isImaginaryUnit()) {
                            convertInternal(buf, F.Times(F.CI, S.Infinity), precedence, false);
                            return;
                        } else if (list.arg1().isNegativeImaginaryUnit()) {
                            convertInternal(buf, F.Times(F.CNI, S.Infinity), precedence, false);
                            return;
                        }
                    }
                }
                break;
        }
    }
    // if (head.equals(F.SeriesData) && (list.size() == 7)) {
    // if (convertSeriesData(buf, list, precedence)) {
    // return;
    // }
    tagStart(buf, "mrow");
    convertHead(buf, ast.head());
    // &af; &#x2061;
    // tag(buf, "mo", "&#x2061;");
    tagStart(buf, "mrow");
    if (fRelaxedSyntax) {
        tag(buf, "mo", "(");
    } else {
        tag(buf, "mo", "[");
    }
    tagStart(buf, "mrow");
    for (int i = 1; i < ast.size(); i++) {
        convertInternal(buf, ast.get(i), Integer.MIN_VALUE, false);
        if (i < ast.argSize()) {
            tag(buf, "mo", ",");
        }
    }
    tagEnd(buf, "mrow");
    if (fRelaxedSyntax) {
        tag(buf, "mo", ")");
    } else {
        tag(buf, "mo", "]");
    }
    tagEnd(buf, "mrow");
    tagEnd(buf, "mrow");
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) ASTRealMatrix(org.matheclipse.core.expression.ASTRealMatrix) PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) IInteger(org.matheclipse.core.interfaces.IInteger) ASTSeriesData(org.matheclipse.core.expression.ASTSeriesData) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) ASTRealVector(org.matheclipse.core.expression.ASTRealVector)

Example 4 with PostfixOperator

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

the class DoubleFormFactory method convertInternal.

private void convertInternal(final StringBuilder buf, final IExpr o, final int precedence, boolean isASTHead) {
    if (o instanceof IAST) {
        final IAST list = (IAST) o;
        // }
        if (list.head().isSymbol()) {
            ISymbol head = (ISymbol) list.head();
            final Operator operator = getOperator(head);
            if (operator != null) {
                if (operator instanceof PostfixOperator) {
                    if (list.isAST1()) {
                        convertPostfixOperator(buf, list, (PostfixOperator) operator, precedence);
                        return;
                    }
                } else {
                    if (convertOperator(operator, list, buf, isASTHead ? Integer.MAX_VALUE : precedence, head)) {
                        return;
                    }
                }
            }
        // int functionID = head.ordinal();
        // if (functionID > ID.UNKNOWN) {
        // switch (functionID) {
        // case ID.Quantity:
        // // if (head.equals(F.SeriesData) && (list.size() == 7)) {
        // if (list instanceof IQuantity) {
        // if (convertQuantityData(buf, (IQuantity) list, precedence)) {
        // return;
        // }
        // }
        // break;
        // case ID.SeriesData:
        // // if (head.equals(F.SeriesData) && (list.size() == 7)) {
        // if (list instanceof ASTSeriesData) {
        // if (convertSeriesData(buf, (ASTSeriesData) list, precedence)) {
        // return;
        // }
        // }
        // break;
        // case ID.List:
        // convertList(buf, list);
        // return;
        // case ID.Part:
        // if (list.size() >= 3) {
        // convertPart(buf, list);
        // return;
        // }
        // break;
        // case ID.Slot:
        // if (list.isAST1() && list.arg1().isInteger()) {
        // convertSlot(buf, list);
        // return;
        // }
        // break;
        // case ID.SlotSequence:
        // if (list.isAST1() && list.arg1().isInteger()) {
        // convertSlotSequence(buf, list);
        // return;
        // }
        // break;
        // case ID.Defer:
        // case ID.HoldForm:
        // if (list.isAST1()) {
        // convertInternal(buf, list.arg1());
        // return;
        // }
        // break;
        // case ID.DirectedInfinity:
        // if (list.isDirectedInfinity()) { // head.equals(F.DirectedInfinity))
        // if (list.isAST0()) {
        // append(buf, "ComplexInfinity");
        // return;
        // }
        // if (list.isAST1()) {
        // if (list.arg1().isOne()) {
        // append(buf, "Infinity");
        // return;
        // } else if (list.arg1().isMinusOne()) {
        // if (ASTNodeFactory.PLUS_PRECEDENCE < precedence) {
        // append(buf, "(");
        // }
        // append(buf, "-Infinity");
        // if (ASTNodeFactory.PLUS_PRECEDENCE < precedence) {
        // append(buf, ")");
        // }
        // return;
        // } else if (list.arg1().isImaginaryUnit()) {
        // append(buf, "I*Infinity");
        // return;
        // } else if (list.arg1().isNegativeImaginaryUnit()) {
        // append(buf, "-I*Infinity");
        // return;
        // }
        // }
        // }
        // break;
        // case ID.Optional:
        // if (list.isAST2() && (list.arg1().isBlank() || list.arg1().isPattern())) {
        // convertInternal(buf, list.arg1());
        // buf.append(":");
        // convertInternal(buf, list.arg2());
        // return;
        // }
        // break;
        // }
        // } else {
        // if (list instanceof ASTRealVector || list instanceof ASTRealMatrix) {
        // convertList(buf, list);
        // return;
        // }
        // }
        }
        convertAST(buf, list);
        return;
    }
    if (o instanceof ISignedNumber) {
        convertNumber(buf, (ISignedNumber) o, precedence, NO_PLUS_CALL);
        return;
    }
    if (o instanceof IComplexNum) {
        convertDoubleComplex(buf, (IComplexNum) o, precedence, NO_PLUS_CALL);
        return;
    }
    if (o instanceof IComplex) {
        convertComplex(buf, (IComplex) o, precedence, NO_PLUS_CALL);
        return;
    }
    if (o instanceof ISymbol) {
        convertSymbol(buf, (ISymbol) o);
        return;
    }
    if (o instanceof IPatternObject) {
        convertPattern(buf, (IPatternObject) o);
        return;
    }
    convertString(buf, o.toString());
}
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) IComplex(org.matheclipse.core.interfaces.IComplex) ISymbol(org.matheclipse.core.interfaces.ISymbol) PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IAST(org.matheclipse.core.interfaces.IAST)

Example 5 with PostfixOperator

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

the class DoubleFormFactory method convertInfixOperator.

public void convertInfixOperator(ISymbol head, final StringBuilder buf, final IAST list, final InfixOperator oper, final int precedence) {
    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().equals("^")) {
                final Operator operator = getOperator(list.arg1().topHead());
                if (operator instanceof PostfixOperator) {
                    append(buf, "(");
                }
            }
        }
        convertInternal(buf, list.arg1(), oper.getPrecedence(), false);
        if (oper.getGrouping() == InfixOperator.RIGHT_ASSOCIATIVE && list.arg1().head().equals(list.head())) {
            append(buf, ")");
        } else {
            if (oper.getOperatorString().equals("^")) {
                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, "(");
        }
        convertInternal(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() > 3 && (head.equals(S.Equal) || head.equals(S.Unequal) || head.equals(S.Greater) || head.equals(S.GreaterEqual) || head.equals(S.Less) || head.equals(S.LessEqual))) {
        convertInternal(buf, list.arg1(), oper.getPrecedence(), false);
        for (int i = 2; i < list.size(); i++) {
            append(buf, oper.getOperatorString());
            convertInternal(buf, list.get(i), oper.getPrecedence(), false);
            if (i < list.size() - 1) {
                buf.append(" && ");
                convertInternal(buf, list.get(i), oper.getPrecedence(), false);
            }
        }
    } else {
        if (list.size() > 1) {
            convertInternal(buf, list.arg1(), oper.getPrecedence(), false);
        }
        for (int i = 2; i < list.size(); i++) {
            append(buf, oper.getOperatorString());
            convertInternal(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)

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