use of org.matheclipse.parser.client.ast.FunctionNode 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;
}
use of org.matheclipse.parser.client.ast.FunctionNode in project symja_android_library by axkr.
the class Parser method getFunction.
/**
* Get a function f[...][...]
*
*/
FunctionNode getFunction(final ASTNode head) throws SyntaxError {
final FunctionNode function = fFactory.createAST(head);
getNextToken();
if (fRelaxedSyntax) {
if (fToken == TT_PRECEDENCE_CLOSE) {
getNextToken();
if (fToken == TT_PRECEDENCE_OPEN) {
return function;
}
if (fToken == TT_ARGUMENTS_OPEN) {
return getFunctionArguments(function);
}
return function;
}
} else {
if (fToken == TT_ARGUMENTS_CLOSE) {
getNextToken();
if (fToken == TT_ARGUMENTS_OPEN) {
return getFunctionArguments(function);
}
return function;
}
}
fRecursionDepth++;
try {
getArguments(function);
} finally {
fRecursionDepth--;
}
if (fRelaxedSyntax) {
if (fToken == TT_PRECEDENCE_CLOSE) {
getNextToken();
if (fToken == TT_PRECEDENCE_OPEN) {
return function;
}
if (fToken == TT_ARGUMENTS_OPEN) {
return getFunctionArguments(function);
}
return function;
}
} else {
if (fToken == TT_ARGUMENTS_CLOSE) {
getNextToken();
if (fToken == TT_ARGUMENTS_OPEN) {
return getFunctionArguments(function);
}
return function;
}
}
if (fRelaxedSyntax) {
throwSyntaxError("')' expected.");
} else {
throwSyntaxError("']' expected.");
}
return null;
}
use of org.matheclipse.parser.client.ast.FunctionNode in project symja_android_library by axkr.
the class Parser method getTimes.
private ASTNode getTimes(ASTNode temp) throws SyntaxError {
FunctionNode func = fFactory.createAST(new SymbolNode("Times"));
func.add(temp);
do {
getNextToken();
temp = parseExpression();
func.add(temp);
if (fToken != TT_PRECEDENCE_CLOSE) {
throwSyntaxError("\')\' expected.");
}
getNextToken();
} while (fToken == TT_PRECEDENCE_OPEN);
return func;
}
Aggregations