use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class ExprParser 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 IExpr parseExpression(IExpr lhs, final int min_precedence) {
IExpr rhs = null;
AbstractExprOperator oper;
InfixExprOperator infixOperator;
PostfixExprOperator 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)) {
// if (fPackageMode && fRecursionDepth < 1) {
// return lhs;
// }
// if (fPackageMode && fToken == TT_IDENTIFIER && fLastChar ==
// '\n') {
// return lhs;
// }
// lazy evaluation of multiplication
oper = fFactory.get("Times");
if (oper.getPrecedence() >= min_precedence) {
rhs = parseLookaheadOperator(oper.getPrecedence());
lhs = F.$(F.$s(oper.getFunctionName()), lhs, rhs);
// 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();
IExpr compoundExpressionNull = parseCompoundExpressionNull(infixOperator, lhs);
if (compoundExpressionNull != null) {
return compoundExpressionNull;
}
while (fToken == TT_NEWLINE) {
getNextToken();
}
rhs = parseLookaheadOperator(infixOperator.getPrecedence());
lhs = createInfixFunction(infixOperator, lhs, rhs);
while (fToken == TT_OPERATOR && infixOperator.getGrouping() == InfixOperator.NONE && infixOperator.getOperatorString().equals(fOperatorString)) {
getNextToken();
rhs = parseLookaheadOperator(infixOperator.getPrecedence());
((IAST) lhs).append(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.core.interfaces.IAST in project symja_android_library by axkr.
the class ExprParser method parsePrimary.
private IExpr parsePrimary() {
if (fToken == TT_OPERATOR) {
if (";;".equals(fOperatorString)) {
IAST span = F.ast(F.Span);
span.append(F.C1);
getNextToken();
if (fToken == TT_COMMA || fToken == TT_ARGUMENTS_CLOSE || fToken == TT_PRECEDENCE_CLOSE) {
span.append(F.All);
return span;
}
if (fToken == TT_OPERATOR && ";;".equals(fOperatorString)) {
span.append(F.All);
getNextToken();
}
span.append(parsePrimary());
return span;
}
if (fOperatorString.equals(".")) {
fCurrentChar = '.';
// return getPart();
return getNumber(false);
}
final PrefixExprOperator prefixOperator = determinePrefixOperator();
if (prefixOperator != null) {
getNextToken();
final IExpr temp = parseLookaheadOperator(prefixOperator.getPrecedence());
if (prefixOperator.getFunctionName().equals("PreMinus")) {
// special cases for negative numbers
if (temp.isNumber()) {
return temp.negate();
}
}
return prefixOperator.createFunction(fFactory, temp);
}
throwSyntaxError("Operator: " + fOperatorString + " is no prefix operator.");
}
return getPart();
}
use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class ExprParser method parseDerivative.
/**
* Parse expressions like <code>expr''[x]</code>
*
* @param expr
* @return
*/
private IExpr parseDerivative(IExpr expr) {
int derivativeCounter = 1;
getNextToken();
while (fToken == TT_DERIVATIVE) {
derivativeCounter++;
getNextToken();
}
IAST deriv = F.$(DERIVATIVE, F.integer(derivativeCounter));
expr = F.$(deriv, expr);
expr = parseArguments(expr);
return expr;
}
use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class ExprParser method getFunctionArguments.
/**
* Get a function f[...][...]
*
*/
IAST getFunctionArguments(final IExpr head) throws SyntaxError {
final IAST function = F.ast(head);
fRecursionDepth++;
try {
getNextToken();
if (fToken == TT_ARGUMENTS_CLOSE) {
getNextToken();
if (fToken == TT_ARGUMENTS_OPEN) {
return getFunctionArguments(function);
}
return function;
}
getArguments(function);
} finally {
fRecursionDepth--;
}
if (fToken == TT_ARGUMENTS_CLOSE) {
getNextToken();
if (fToken == TT_ARGUMENTS_OPEN) {
return getFunctionArguments(function);
}
return function;
}
throwSyntaxError("']' expected.");
return null;
}
use of org.matheclipse.core.interfaces.IAST in project symja_android_library by axkr.
the class ExprParser method getList.
/**
* Get a list {...}
*
*/
private IExpr getList() throws SyntaxError {
// fFactory.createFunction(fFactory.createSymbol(IConstantOperators.List));
final IAST function = F.ListAlloc(10);
getNextToken();
if (fToken == TT_LIST_CLOSE) {
getNextToken();
return function;
}
fRecursionDepth++;
try {
getArguments(function);
} finally {
fRecursionDepth--;
}
if (fToken == TT_LIST_CLOSE) {
getNextToken();
return function;
}
throwSyntaxError("'}' expected.");
return null;
}
Aggregations