Search in sources :

Example 6 with FunctionNode

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

the class Parser method parsePrimary.

private ASTNode parsePrimary() {
    if (fToken == TT_OPERATOR) {
        if (";;".equals(fOperatorString)) {
            FunctionNode function = fFactory.createFunction(fFactory.createSymbol(IConstantOperators.Span));
            function.add(fFactory.createInteger(1));
            getNextToken();
            if (fToken == TT_COMMA || fToken == TT_ARGUMENTS_CLOSE || fToken == TT_PRECEDENCE_CLOSE) {
                function.add(fFactory.createSymbol(IConstantOperators.All));
                return function;
            }
            function.add(parsePrimary());
            if (fToken == TT_OPERATOR && ";;".equals(fOperatorString)) {
                function.add(fFactory.createSymbol(IConstantOperators.All));
                getNextToken();
            }
            return function;
        }
        if (".".equals(fOperatorString)) {
            fCurrentChar = '.';
            return getNumber(false);
        }
        final PrefixOperator prefixOperator = determinePrefixOperator();
        if (prefixOperator != null) {
            getNextToken();
            final ASTNode temp = parseLookaheadOperator(prefixOperator.getPrecedence());
            if ("PreMinus".equals(prefixOperator.getFunctionName()) && temp instanceof NumberNode) {
                // special cases for negative numbers
                ((NumberNode) temp).toggleSign();
                return temp;
            }
            return prefixOperator.createFunction(fFactory, temp);
        }
        throwSyntaxError("Operator: " + fOperatorString + " is no prefix operator.");
    }
    return getPart();
}
Also used : PrefixOperator(org.matheclipse.parser.client.operator.PrefixOperator) NumberNode(org.matheclipse.parser.client.ast.NumberNode) FunctionNode(org.matheclipse.parser.client.ast.FunctionNode) ASTNode(org.matheclipse.parser.client.ast.ASTNode)

Example 7 with FunctionNode

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

the class Parser method getPart.

/**
	 * Get a <i>part [[..]]</i> of an expression <code>{a,b,c}[[2]]</code> &rarr; <code>b</code>
	 * 
	 */
private ASTNode getPart() throws SyntaxError {
    ASTNode temp = getFactor();
    if (fToken != TT_PARTOPEN) {
        return temp;
    }
    FunctionNode function = null;
    do {
        if (function == null) {
            function = fFactory.createFunction(fFactory.createSymbol(IConstantOperators.Part), temp);
        } else {
            function = fFactory.createFunction(fFactory.createSymbol(IConstantOperators.Part), function);
        }
        fRecursionDepth++;
        try {
            do {
                getNextToken();
                if (fToken == TT_ARGUMENTS_CLOSE) {
                    if (fInputString.length() > fCurrentPosition && fInputString.charAt(fCurrentPosition) == ']') {
                        throwSyntaxError("Statement (i.e. index) expected in [[ ]].");
                    }
                }
                function.add(parseExpression());
            } while (fToken == TT_COMMA);
            if (fToken == TT_ARGUMENTS_CLOSE) {
                // scanner-step begin: (instead of getNextToken() call):
                if (fInputString.length() > fCurrentPosition) {
                    if (fInputString.charAt(fCurrentPosition) == ']') {
                        fCurrentPosition++;
                        fToken = TT_PARTCLOSE;
                    }
                }
            // scanner-step end
            }
            if (fToken != TT_PARTCLOSE) {
                throwSyntaxError("']]' expected.");
            }
        } finally {
            fRecursionDepth--;
        }
        getNextToken();
    } while (fToken == TT_PARTOPEN);
    return parseArguments(function);
}
Also used : ASTNode(org.matheclipse.parser.client.ast.ASTNode) FunctionNode(org.matheclipse.parser.client.ast.FunctionNode)

Example 8 with FunctionNode

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

the class DoubleEvaluator method optimizeFunction.

/**
	 * Optimize an already parsed in <code>functionNode</code> into an
	 * <code>ASTNode</code>.
	 * 
	 * @param functionNode
	 * @return
	 * 
	 */
public ASTNode optimizeFunction(final FunctionNode functionNode) {
    if (functionNode.size() > 0) {
        boolean doubleOnly = true;
        ASTNode node;
        for (int i = 1; i < functionNode.size(); i++) {
            node = functionNode.getNode(i);
            if (node instanceof NumberNode) {
                functionNode.set(i, new DoubleNode(((NumberNode) functionNode.getNode(i)).doubleValue()));
            } else if (functionNode.getNode(i) instanceof FunctionNode) {
                ASTNode optNode = optimizeFunction((FunctionNode) functionNode.getNode(i));
                if (!(optNode instanceof DoubleNode)) {
                    doubleOnly = false;
                }
                functionNode.set(i, optNode);
            } else if (node instanceof SymbolNode) {
                Double dbl = SYMBOL_DOUBLE_MAP.get(node.toString());
                if (dbl != null) {
                    functionNode.set(i, new DoubleNode(dbl.doubleValue()));
                } else {
                    doubleOnly = false;
                }
            } else {
                doubleOnly = false;
            }
        }
        if (doubleOnly) {
            try {
                return new DoubleNode(evaluateFunction(functionNode));
            } catch (Exception e) {
            }
        }
    }
    return functionNode;
}
Also used : SymbolNode(org.matheclipse.parser.client.ast.SymbolNode) NumberNode(org.matheclipse.parser.client.ast.NumberNode) ASTNode(org.matheclipse.parser.client.ast.ASTNode) FunctionNode(org.matheclipse.parser.client.ast.FunctionNode) ArithmeticMathException(org.matheclipse.parser.client.math.ArithmeticMathException)

Example 9 with FunctionNode

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

the class DoubleEvaluator method parse.

/**
	 * Parse the given <code>expression String</code> and store the resulting
	 * ASTNode in this DoubleEvaluator
	 * 
	 * @param expression
	 * @return
	 * @throws SyntaxError
	 */
public ASTNode parse(String expression) {
    Parser p;
    if (fRelaxedSyntax) {
        p = new Parser(ASTNodeFactory.RELAXED_STYLE_FACTORY, true);
    } else {
        p = new Parser(ASTNodeFactory.MMA_STYLE_FACTORY, false);
    }
    fNode = p.parse(expression);
    if (fNode instanceof FunctionNode) {
        fNode = optimizeFunction((FunctionNode) fNode);
    }
    return fNode;
}
Also used : FunctionNode(org.matheclipse.parser.client.ast.FunctionNode) Parser(org.matheclipse.parser.client.Parser)

Example 10 with FunctionNode

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

the class ApplyOperator method createFunction.

public ASTNode createFunction(final IParserFactory factory, final ASTNode lhs, final ASTNode rhs) {
    FunctionNode fn = factory.createFunction(factory.createSymbol("Apply"), lhs, rhs);
    if (fOperatorString.equals("@@")) {
        return fn;
    }
    fn.add(factory.createFunction(factory.createSymbol("List"), factory.createInteger(1)));
    return fn;
}
Also used : FunctionNode(org.matheclipse.parser.client.ast.FunctionNode)

Aggregations

FunctionNode (org.matheclipse.parser.client.ast.FunctionNode)18 ASTNode (org.matheclipse.parser.client.ast.ASTNode)8 SymbolNode (org.matheclipse.parser.client.ast.SymbolNode)6 NumberNode (org.matheclipse.parser.client.ast.NumberNode)3 IAST (org.matheclipse.core.interfaces.IAST)2 IExpr (org.matheclipse.core.interfaces.IExpr)2 Parser (org.matheclipse.parser.client.Parser)2 IntegerNode (org.matheclipse.parser.client.ast.IntegerNode)2 ArithmeticMathException (org.matheclipse.parser.client.math.ArithmeticMathException)2 PrefixOperator (org.matheclipse.parser.client.operator.PrefixOperator)2 Apfloat (org.apfloat.Apfloat)1 Apint (org.apfloat.Apint)1 AST2Expr (org.matheclipse.core.convert.AST2Expr)1 WrongArgumentType (org.matheclipse.core.eval.exception.WrongArgumentType)1 IInteger (org.matheclipse.core.interfaces.IInteger)1 INumber (org.matheclipse.core.interfaces.INumber)1 ISignedNumber (org.matheclipse.core.interfaces.ISignedNumber)1 ISymbol (org.matheclipse.core.interfaces.ISymbol)1 FloatNode (org.matheclipse.parser.client.ast.FloatNode)1 FractionNode (org.matheclipse.parser.client.ast.FractionNode)1