Search in sources :

Example 11 with SymbolNode

use of org.matheclipse.parser.client.ast.SymbolNode in project symja_android_library by axkr.

the class Parser method parseInequality.

/**
 * Rewrite a chain of different comparator operators to an <code>Inequality(...)</code>
 * expression.
 *
 * @param ast the ast which should be rewritten
 * @param infixOperator
 * @return
 */
private ASTNode parseInequality(final FunctionNode ast, final InfixOperator infixOperator) {
    // rewrite to Inequality
    SymbolNode head = (SymbolNode) ast.get(0);
    final FunctionNode result = fFactory.createFunction(fFactory.createSymbol(IConstantOperators.Inequality));
    for (int i = 1; i < ast.size(); i++) {
        result.add(ast.get(i));
        result.add(head);
    }
    InfixOperator compareOperator = determineBinaryOperator();
    result.set(result.size() - 1, fFactory.createSymbol(compareOperator.getFunctionName()));
    getNextToken();
    while (fToken == TT_NEWLINE) {
        getNextToken();
    }
    int precedence = infixOperator.getPrecedence();
    result.add(parseLookaheadOperator(precedence));
    while (fToken == TT_OPERATOR && isComparatorOperator(fOperatorString)) {
        compareOperator = determineBinaryOperator();
        result.add(fFactory.createSymbol(compareOperator.getFunctionName()));
        getNextToken();
        while (fToken == TT_NEWLINE) {
            getNextToken();
        }
        result.add(parseLookaheadOperator(precedence));
    }
    return result;
}
Also used : SymbolNode(org.matheclipse.parser.client.ast.SymbolNode) FunctionNode(org.matheclipse.parser.client.ast.FunctionNode) InfixOperator(org.matheclipse.parser.client.operator.InfixOperator)

Example 12 with SymbolNode

use of org.matheclipse.parser.client.ast.SymbolNode in project symja_android_library by axkr.

the class Parser method getSymbol.

/**
 * Read the current identifier from the expression factories table
 *
 * @return
 * @see
 */
private SymbolNode getSymbol() throws SyntaxError {
    String[] identifierContext = getIdentifier();
    if (!fFactory.isValidIdentifier(identifierContext[0])) {
        throwSyntaxError("Invalid identifier: " + identifierContext[0] + " detected.");
    }
    final SymbolNode symbol = fFactory.createSymbol(identifierContext[0], identifierContext[1]);
    getNextToken();
    return symbol;
}
Also used : SymbolNode(org.matheclipse.parser.client.ast.SymbolNode)

Example 13 with SymbolNode

use of org.matheclipse.parser.client.ast.SymbolNode in project symja_android_library by axkr.

the class Parser method getNumber.

/**
 * Method Declaration.
 *
 * @return
 * @see
 */
private ASTNode getNumber(final boolean negative) throws SyntaxError {
    ASTNode temp = null;
    String numberStr = "";
    try {
        final Object[] result = getNumberString();
        numberStr = (String) result[0];
        final int numFormat = ((Integer) result[1]);
        String exponentStr = (String) result[2];
        if (negative) {
            numberStr = '-' + numberStr;
        }
        if (numFormat < 0) {
            if (fCurrentChar == '`' && isValidPosition()) {
                fCurrentPosition++;
                if (isValidPosition() && fInputString[fCurrentPosition] == '*') {
                    fCurrentPosition++;
                    if (isValidPosition() && fInputString[fCurrentPosition] == '^') {
                        fCurrentPosition += 2;
                        long exponent = getJavaLong();
                        // Double d = Double.valueOf(number + "E" + exponent);
                        return fFactory.createDouble(numberStr + "E" + exponent);
                    }
                } else if (isValidPosition() && fInputString[fCurrentPosition] == '`') {
                    fCurrentPosition += 2;
                    long precision = getJavaLong();
                    if (precision < ParserConfig.MACHINE_PRECISION) {
                        precision = ParserConfig.MACHINE_PRECISION;
                    }
                    return fFactory.createDouble(numberStr);
                } else {
                    if (isValidPosition() && Character.isDigit(fInputString[fCurrentPosition])) {
                        fCurrentPosition++;
                        long precision = getJavaLong();
                        if (precision < ParserConfig.MACHINE_PRECISION) {
                            precision = ParserConfig.MACHINE_PRECISION;
                        }
                        return fFactory.createDouble(numberStr);
                    } else {
                        getNextToken();
                        return fFactory.createDouble(numberStr);
                    }
                }
                throwSyntaxError("Number format error: " + numberStr, numberStr.length());
            }
            temp = fFactory.createDouble(numberStr);
        } else {
            if (exponentStr == null || exponentStr.equals("1")) {
                temp = fFactory.createInteger(numberStr, numFormat);
            } else {
                if (numFormat == 10) {
                    try {
                        int exponent = Integer.parseInt(exponentStr, 10);
                        if (exponent < 0) {
                            exponent = -exponent;
                            StringBuilder buf = createPowersOf10(exponent);
                            temp = fFactory.createFunction(new SymbolNode("Times"), fFactory.createInteger(numberStr, numFormat), fFactory.createFunction(new SymbolNode("Power"), fFactory.createInteger(buf.toString(), numFormat), IntegerNode.CN1));
                        } else {
                            StringBuilder buf = createPowersOf10(exponent);
                            temp = fFactory.createFunction(new SymbolNode("Times"), fFactory.createInteger(numberStr, numFormat), fFactory.createInteger(buf.toString(), numFormat));
                        }
                    } catch (final NumberFormatException e) {
                        throwSyntaxError("Number format error (not an int type): " + exponentStr, exponentStr.length());
                    }
                } else {
                    throwSyntaxError("Number format error: " + numberStr, numberStr.length());
                }
            }
        }
    } catch (final SyntaxError e) {
        throwSyntaxError("Number format error: " + numberStr, numberStr.length());
    }
    getNextToken();
    return temp;
}
Also used : SymbolNode(org.matheclipse.parser.client.ast.SymbolNode) ASTNode(org.matheclipse.parser.client.ast.ASTNode)

Aggregations

SymbolNode (org.matheclipse.parser.client.ast.SymbolNode)13 FunctionNode (org.matheclipse.parser.client.ast.FunctionNode)11 ASTNode (org.matheclipse.parser.client.ast.ASTNode)9 IExpr (org.matheclipse.core.interfaces.IExpr)3 InfixOperator (org.matheclipse.parser.client.operator.InfixOperator)3 Apfloat (org.apfloat.Apfloat)2 Apint (org.apfloat.Apint)2 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)2 IInteger (org.matheclipse.core.interfaces.IInteger)2 ISymbol (org.matheclipse.core.interfaces.ISymbol)2 FloatNode (org.matheclipse.parser.client.ast.FloatNode)2 FractionNode (org.matheclipse.parser.client.ast.FractionNode)2 IntegerNode (org.matheclipse.parser.client.ast.IntegerNode)2 NumberNode (org.matheclipse.parser.client.ast.NumberNode)2 Pattern2Node (org.matheclipse.parser.client.ast.Pattern2Node)2 Pattern3Node (org.matheclipse.parser.client.ast.Pattern3Node)2 PatternNode (org.matheclipse.parser.client.ast.PatternNode)2 StringNode (org.matheclipse.parser.client.ast.StringNode)2 DoubleNode (org.matheclipse.parser.client.eval.DoubleNode)2 ArithmeticMathException (org.matheclipse.parser.client.math.ArithmeticMathException)2