Search in sources :

Example 1 with Operator

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

the class OutputFormFactory method getOperator.

private Operator getOperator(ISymbol head) {
    String headerStr = head.getSymbolName();
    if (Config.PARSER_USE_LOWERCASE_SYMBOLS) {
        String str = AST2Expr.PREDEFINED_SYMBOLS_MAP.get(headerStr);
        if (str != null) {
            headerStr = str;
        }
    }
    final Operator operator = ASTNodeFactory.MMA_STYLE_FACTORY.get(headerStr);
    return operator;
}
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)

Example 2 with Operator

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

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

the class OutputFormFactory method convert.

public void convert(final Appendable buf, final IExpr o, final int precedence, boolean isASTHead) throws IOException {
    if (o instanceof IAST) {
        final IAST list = (IAST) o;
        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() && a1Head.arg1().isInteger() && headAST.isAST1() && headAST.arg1().isSymbol() && derivStruct[2] != null) {
                    try {
                        int n = ((IInteger) a1Head.arg1()).toInt();
                        // IExpr arg1 = listArg1.arg1();
                        if (n == 1 || n == 2) {
                            ISymbol f = (ISymbol) headAST.arg1();
                            convertSymbol(buf, f);
                            if (n == 1) {
                                append(buf, "'");
                            } else if (n == 2) {
                                append(buf, "''");
                            }
                            convertArgs(buf, f, list);
                            return;
                        }
                    } catch (ArithmeticException ae) {
                    }
                }
            }
            convert(buf, header, Integer.MIN_VALUE, true);
            convertFunctionArgs(buf, list);
            return;
        }
        ISymbol head = list.topHead();
        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;
                }
            }
        }
        if (list.isList() || list instanceof ASTRealVector || list instanceof ASTRealMatrix) {
            convertList(buf, list);
            return;
        }
        if (head.equals(F.Part) && (list.size() >= 3)) {
            convertPart(buf, list);
            return;
        }
        if (head.equals(F.Slot) && (list.isAST1()) && (list.arg1() instanceof IInteger)) {
            convertSlot(buf, list);
            return;
        }
        if (head.equals(F.SlotSequence) && (list.isAST1()) && (list.arg1() instanceof IInteger)) {
            convertSlotSequence(buf, list);
            return;
        }
        if ((head.equals(F.HoldForm) || head.equals(F.Defer)) && (list.isAST1())) {
            convert(buf, list.arg1());
            return;
        }
        if (head.equals(F.SeriesData) && (list.size() == 7)) {
            if (convertSeriesData(buf, list, precedence)) {
                return;
            }
        }
        if (list.isDirectedInfinity()) {
            // {
            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;
                }
            }
        }
        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) ISymbol(org.matheclipse.core.interfaces.ISymbol) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) ASTRealMatrix(org.matheclipse.core.expression.ASTRealMatrix) IComplex(org.matheclipse.core.interfaces.IComplex) PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) ISignedNumber(org.matheclipse.core.interfaces.ISignedNumber) IInteger(org.matheclipse.core.interfaces.IInteger) IComplexNum(org.matheclipse.core.interfaces.IComplexNum) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr) ASTRealVector(org.matheclipse.core.expression.ASTRealVector)

Example 4 with Operator

use of org.matheclipse.parser.client.operator.Operator 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)4 Operator (org.matheclipse.parser.client.operator.Operator)4 PostfixOperator (org.matheclipse.parser.client.operator.PostfixOperator)4 PrefixOperator (org.matheclipse.parser.client.operator.PrefixOperator)4 ASTRealMatrix (org.matheclipse.core.expression.ASTRealMatrix)1 ASTRealVector (org.matheclipse.core.expression.ASTRealVector)1 IAST (org.matheclipse.core.interfaces.IAST)1 IComplex (org.matheclipse.core.interfaces.IComplex)1 IComplexNum (org.matheclipse.core.interfaces.IComplexNum)1 IExpr (org.matheclipse.core.interfaces.IExpr)1 IInteger (org.matheclipse.core.interfaces.IInteger)1 IPatternObject (org.matheclipse.core.interfaces.IPatternObject)1 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)1 ISymbol (org.matheclipse.core.interfaces.ISymbol)1 ASTNode (org.matheclipse.parser.client.ast.ASTNode)1 FunctionNode (org.matheclipse.parser.client.ast.FunctionNode)1