Search in sources :

Example 1 with BuiltInDummy

use of org.matheclipse.core.expression.BuiltInDummy in project symja_android_library by axkr.

the class TeXParser method convert.

private IExpr convert(NodeList list, int[] position, int end, IExpr lhs, int precedence) {
    final int listSize = list.getLength();
    if (end > 1) {
        if (lhs == null) {
            Node lhsNode = list.item(position[0]++);
            String name = lhsNode.getNodeName();
            if (name.equals("mo")) {
                String text = lhsNode.getTextContent();
                PrefixOperator operator = PREFIX_OPERATOR_MAP.get(text);
                if (operator != null) {
                    int currPrec = operator.getPrecedence();
                    IExpr x = convert(list, position, end, null, currPrec);
                    lhs = operator.createFunction(x);
                }
            }
            if (lhs == null) {
                lhs = toHeadExpr(lhsNode, list, position, precedence);
                if (position[0] >= listSize) {
                    return lhs;
                }
            }
            int attribute = ISymbol.NOATTRIBUTE;
            if (lhs.isSymbol()) {
                attribute = ((ISymbol) lhs).getAttributes();
            }
            if ((attribute & ISymbol.CONSTANT) != ISymbol.CONSTANT) {
                if (// 
                (lhs.isFunction() || lhs.isSymbol() || lhs.isDerivative() != null) && position[0] < listSize) {
                    boolean isNumericFunction = ((attribute & ISymbol.NUMERICFUNCTION) == ISymbol.NUMERICFUNCTION);
                    Node arg2 = list.item(position[0]);
                    if (arg2.getNodeName().equals("mfenced")) {
                        position[0]++;
                        int[] position2 = new int[] { 0 };
                        NodeList childNodes = arg2.getChildNodes();
                        IExpr args = convertArgs(childNodes, position2);
                        if (args.isSequence()) {
                            ((IASTMutable) args).set(0, lhs);
                            return args;
                        }
                        lhs = F.unaryAST1(lhs, args);
                        if (position[0] == listSize) {
                            return lhs;
                        }
                    } else if (isNumericFunction || (lhs.isBuiltInSymbol() && !(lhs instanceof BuiltInDummy)) || lhs.isFunction()) {
                        if (lhs.equals(S.Integrate)) {
                            ISymbol test = F.Dummy("test");
                            return integrate(list, position, test, test);
                        }
                        IExpr args = convert(list, position, end, null, 0);
                        if (args.isSequence()) {
                            ((IASTMutable) args).set(0, lhs);
                            return args;
                        }
                        if (lhs.isFunction() && lhs.size() == 2) {
                            IExpr temp = Lambda.replaceSlots(lhs.first(), F.list(args));
                            if (temp.isPresent()) {
                                lhs = temp;
                            }
                        } else {
                            lhs = F.unaryAST1(lhs, args);
                        }
                        if (position[0] == listSize) {
                            return lhs;
                        }
                    }
                }
            }
        }
        IExpr result = lhs;
        int currPrec = 0;
        while (position[0] < end) {
            Node op = list.item(position[0]);
            String name = op.getNodeName();
            if (name.equals("mo")) {
                String text = op.getTextContent();
                if (SHOW_UNICODE) {
                    LOGGER.info("mo: {} - {}", () -> text, () -> toUnicodeString(text, "UTF-8"));
                }
                BinaryOperator binaryOperator = BINARY_OPERATOR_MAP.get(text);
                if (binaryOperator != null) {
                    currPrec = binaryOperator.getPrecedence();
                    if (precedence >= currPrec) {
                        return result;
                    }
                    position[0]++;
                    IExpr rhs = convert(list, position, end, null, currPrec);
                    result = binaryOperator.createFunction(result, rhs);
                    continue;
                } else {
                    PostfixOperator postfixOperator = POSTFIX_OPERATOR_MAP.get(text);
                    if (postfixOperator != null) {
                        currPrec = postfixOperator.getPrecedence();
                        if (precedence >= currPrec) {
                            return result;
                        }
                        result = postfixOperator.createFunction(lhs);
                        position[0]++;
                        continue;
                    }
                }
                throw new AbortException();
            } else if (name.equals("mspace")) {
                position[0]++;
                continue;
            }
            // try to build a Times(...) expression
            currPrec = Precedence.TIMES;
            IExpr rhs = convert(list, position, end, null, currPrec);
            // invisible times?
            result = F.Times(lhs, rhs);
        }
        if (result.isPresent() && position[0] >= end) {
            return result;
        }
    }
    return convertArgs(list, position);
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) BuiltInDummy(org.matheclipse.core.expression.BuiltInDummy) IExpr(org.matheclipse.core.interfaces.IExpr) AbortException(org.matheclipse.core.eval.exception.AbortException)

Aggregations

AbortException (org.matheclipse.core.eval.exception.AbortException)1 BuiltInDummy (org.matheclipse.core.expression.BuiltInDummy)1 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)1 IExpr (org.matheclipse.core.interfaces.IExpr)1 ISymbol (org.matheclipse.core.interfaces.ISymbol)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1