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();
}
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> → <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);
}
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;
}
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;
}
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;
}
Aggregations