use of org.matheclipse.parser.client.operator.PostfixOperator in project symja_android_library by axkr.
the class Parser method parseLookaheadOperator.
private ASTNode parseLookaheadOperator(final int min_precedence) {
ASTNode rhs = parsePrimary();
while (true) {
final int lookahead = fToken;
if (fToken == TT_NEWLINE) {
return rhs;
}
if ((fToken == TT_LIST_OPEN) || (fToken == TT_PRECEDENCE_OPEN) || (fToken == TT_IDENTIFIER) || (fToken == TT_STRING) || (fToken == TT_DIGIT) || (fToken == TT_SLOT)) {
// lazy evaluation of multiplication
InfixOperator timesOperator = (InfixOperator) fFactory.get("Times");
if (timesOperator.getPrecedence() > min_precedence) {
rhs = parseExpression(rhs, timesOperator.getPrecedence());
continue;
} else if ((timesOperator.getPrecedence() == min_precedence) && (timesOperator.getGrouping() == InfixOperator.RIGHT_ASSOCIATIVE)) {
rhs = parseExpression(rhs, timesOperator.getPrecedence());
continue;
}
} else {
if (fToken == TT_DERIVATIVE) {
rhs = parseDerivative(rhs);
}
if (lookahead != TT_OPERATOR) {
break;
}
InfixOperator infixOperator = determineBinaryOperator();
if (infixOperator != null) {
if (infixOperator.getPrecedence() > min_precedence || ((infixOperator.getPrecedence() == min_precedence) && (infixOperator.getGrouping() == InfixOperator.RIGHT_ASSOCIATIVE))) {
if (";".equals(infixOperator.getOperatorString())) {
if (fPackageMode && fRecursionDepth < 1) {
return infixOperator.createFunction(fFactory, rhs, fFactory.createSymbol("Null"));
}
}
rhs = parseExpression(rhs, infixOperator.getPrecedence());
continue;
}
} else {
PostfixOperator postfixOperator = determinePostfixOperator();
if (postfixOperator != null) {
if (postfixOperator.getPrecedence() >= min_precedence) {
getNextToken();
rhs = postfixOperator.createFunction(fFactory, rhs);
continue;
}
}
}
}
break;
}
return rhs;
}
use of org.matheclipse.parser.client.operator.PostfixOperator in project symja_android_library by axkr.
the class OutputFormFactory method convertOperator.
private boolean convertOperator(final Operator operator, final IAST list, final Appendable buf, final int precedence, ISymbol head) throws IOException {
if ((operator instanceof PrefixOperator) && (list.isAST1())) {
convertPrefixOperator(buf, list, (PrefixOperator) operator, precedence);
return true;
}
if ((operator instanceof InfixOperator) && (list.size() > 2)) {
InfixOperator infixOperator = (InfixOperator) operator;
if (head.equals(S.Plus)) {
if (fPlusReversed) {
convertPlusOperatorReversed(buf, list, infixOperator, precedence);
} else {
convertPlusOperator(buf, list, infixOperator, precedence);
}
return true;
} else if (head.equals(S.Times)) {
convertTimesFraction(buf, list, infixOperator, precedence, NO_PLUS_CALL);
return true;
} else if (list.isPower()) {
convertPowerOperator(buf, list, infixOperator, precedence);
return true;
} else if (list.isAST(S.Apply)) {
if (list.size() == 3) {
convertInfixOperator(buf, list, ASTNodeFactory.APPLY_OPERATOR, precedence);
return true;
}
if (list.size() == 4 && list.arg2().equals(F.CListC1)) {
convertInfixOperator(buf, list, ASTNodeFactory.APPLY_LEVEL_OPERATOR, precedence);
return true;
}
return false;
} else if (list.size() != 3 && infixOperator.getGrouping() != InfixOperator.NONE) {
return false;
}
convertInfixOperator(buf, list, (InfixOperator) operator, precedence);
return true;
}
if ((operator instanceof PostfixOperator) && (list.isAST1())) {
convertPostfixOperator(buf, list, (PostfixOperator) operator, precedence);
return true;
}
return false;
}
use of org.matheclipse.parser.client.operator.PostfixOperator in project symja_android_library by axkr.
the class MathMLFormFactory method convertAST.
private void convertAST(final StringBuilder buf, final IAST ast, final int precedence) {
final IAST list = ast;
IExpr header = list.head();
if (!header.isSymbol()) {
// print expressions like: f(#1, y)& [x]
IAST[] derivStruct = list.isDerivativeAST1();
if (derivStruct != null) {
IAST a1Head = derivStruct[0];
IAST headAST = derivStruct[1];
if (a1Head.isAST1() && headAST.isAST1() && (headAST.arg1().isSymbol() || headAST.arg1().isAST())) {
try {
int n = a1Head.arg1().toIntDefault();
if (n == 1 || n == 2) {
tagStart(buf, "mrow");
tagStart(buf, "msup");
IExpr symbolOrAST = headAST.arg1();
convertInternal(buf, symbolOrAST, Integer.MAX_VALUE, false);
if (n == 1) {
tag(buf, "mo", "′");
} else if (n == 2) {
tag(buf, "mo", "′′");
}
tagEnd(buf, "msup");
if (derivStruct[2] != null) {
convertArgs(buf, symbolOrAST, list);
}
tagEnd(buf, "mrow");
return;
}
tagStart(buf, "mrow");
IExpr symbolOrAST = headAST.arg1();
tagStart(buf, "msup");
convertInternal(buf, symbolOrAST, Integer.MAX_VALUE, false);
tagStart(buf, "mrow");
tag(buf, "mo", "(");
// supscript "(n)"
convertInternal(buf, a1Head.arg1(), Integer.MIN_VALUE, false);
tag(buf, "mo", ")");
tagEnd(buf, "mrow");
tagEnd(buf, "msup");
if (derivStruct[2] != null) {
convertArgs(buf, symbolOrAST, list);
}
tagEnd(buf, "mrow");
return;
} catch (ArithmeticException ae) {
}
}
}
convertInternal(buf, header, Integer.MIN_VALUE, false);
convertFunctionArgs(buf, list);
return;
}
ISymbol head = list.topHead();
final org.matheclipse.parser.client.operator.Operator operator = OutputFormFactory.getOperator(head);
if (operator != null) {
if (operator instanceof PostfixOperator) {
if (list.isAST1()) {
convertPostfixOperator(buf, list, (PostfixOperator) operator, operator.getPrecedence());
return;
}
} else {
if (convertOperator(operator, list, buf, operator.getPrecedence(), head)) {
return;
}
}
}
if (list instanceof ASTSeriesData) {
if (convertSeriesData(buf, (ASTSeriesData) list, precedence)) {
return;
}
}
if (list.isList() || list instanceof ASTRealVector || list instanceof ASTRealMatrix) {
convertList(buf, list);
return;
}
if (list.isAST(S.Parenthesis)) {
convertArgs(buf, S.Parenthesis, list);
return;
}
if (list.isInterval() && convertInterval(buf, list)) {
return;
}
if (list.isAssociation()) {
convertAssociation(buf, (IAssociation) list);
return;
}
int functionID = ((ISymbol) list.head()).ordinal();
if (functionID > ID.UNKNOWN) {
switch(functionID) {
case ID.Inequality:
if (list.size() > 3 && convertInequality(buf, list, precedence)) {
return;
}
break;
case ID.Part:
if ((list.size() >= 3)) {
convertPart(buf, list);
return;
}
break;
case ID.Slot:
if ((list.isAST1()) && (list.arg1() instanceof IInteger)) {
convertSlot(buf, list);
return;
}
break;
case ID.SlotSequence:
if ((list.isAST1()) && (list.arg1() instanceof IInteger)) {
convertSlotSequence(buf, list);
return;
}
break;
case ID.SparseArray:
if (list.isSparseArray()) {
tagStart(buf, "mtext");
buf.append(list.toString());
tagEnd(buf, "mtext");
return;
}
break;
case ID.Defer:
case ID.HoldForm:
if ((list.isAST1())) {
convertInternal(buf, list.arg1(), precedence, false);
return;
}
break;
case ID.DirectedInfinity:
if (list.isDirectedInfinity()) {
// head.equals(F.DirectedInfinity))
if (list.isAST0()) {
convertSymbol(buf, S.ComplexInfinity);
return;
}
if (list.isAST1()) {
if (list.arg1().isOne()) {
convertSymbol(buf, S.Infinity);
return;
} else if (list.arg1().isMinusOne()) {
convertInternal(buf, F.Times(F.CN1, S.Infinity), precedence, false);
return;
} else if (list.arg1().isImaginaryUnit()) {
convertInternal(buf, F.Times(F.CI, S.Infinity), precedence, false);
return;
} else if (list.arg1().isNegativeImaginaryUnit()) {
convertInternal(buf, F.Times(F.CNI, S.Infinity), precedence, false);
return;
}
}
}
break;
}
}
// if (head.equals(F.SeriesData) && (list.size() == 7)) {
// if (convertSeriesData(buf, list, precedence)) {
// return;
// }
tagStart(buf, "mrow");
convertHead(buf, ast.head());
// ⁡ ⁡
// tag(buf, "mo", "⁡");
tagStart(buf, "mrow");
if (fRelaxedSyntax) {
tag(buf, "mo", "(");
} else {
tag(buf, "mo", "[");
}
tagStart(buf, "mrow");
for (int i = 1; i < ast.size(); i++) {
convertInternal(buf, ast.get(i), Integer.MIN_VALUE, false);
if (i < ast.argSize()) {
tag(buf, "mo", ",");
}
}
tagEnd(buf, "mrow");
if (fRelaxedSyntax) {
tag(buf, "mo", ")");
} else {
tag(buf, "mo", "]");
}
tagEnd(buf, "mrow");
tagEnd(buf, "mrow");
}
use of org.matheclipse.parser.client.operator.PostfixOperator in project symja_android_library by axkr.
the class DoubleFormFactory method convertInternal.
private void convertInternal(final StringBuilder buf, final IExpr o, final int precedence, boolean isASTHead) {
if (o instanceof IAST) {
final IAST list = (IAST) o;
// }
if (list.head().isSymbol()) {
ISymbol head = (ISymbol) list.head();
final Operator operator = getOperator(head);
if (operator != null) {
if (operator instanceof PostfixOperator) {
if (list.isAST1()) {
convertPostfixOperator(buf, list, (PostfixOperator) operator, precedence);
return;
}
} else {
if (convertOperator(operator, list, buf, isASTHead ? Integer.MAX_VALUE : precedence, head)) {
return;
}
}
}
// int functionID = head.ordinal();
// if (functionID > ID.UNKNOWN) {
// switch (functionID) {
// case ID.Quantity:
// // if (head.equals(F.SeriesData) && (list.size() == 7)) {
// if (list instanceof IQuantity) {
// if (convertQuantityData(buf, (IQuantity) list, precedence)) {
// return;
// }
// }
// break;
// case ID.SeriesData:
// // if (head.equals(F.SeriesData) && (list.size() == 7)) {
// if (list instanceof ASTSeriesData) {
// if (convertSeriesData(buf, (ASTSeriesData) list, precedence)) {
// return;
// }
// }
// break;
// case ID.List:
// convertList(buf, list);
// return;
// case ID.Part:
// if (list.size() >= 3) {
// convertPart(buf, list);
// return;
// }
// break;
// case ID.Slot:
// if (list.isAST1() && list.arg1().isInteger()) {
// convertSlot(buf, list);
// return;
// }
// break;
// case ID.SlotSequence:
// if (list.isAST1() && list.arg1().isInteger()) {
// convertSlotSequence(buf, list);
// return;
// }
// break;
// case ID.Defer:
// case ID.HoldForm:
// if (list.isAST1()) {
// convertInternal(buf, list.arg1());
// return;
// }
// break;
// case ID.DirectedInfinity:
// if (list.isDirectedInfinity()) { // head.equals(F.DirectedInfinity))
// if (list.isAST0()) {
// append(buf, "ComplexInfinity");
// return;
// }
// if (list.isAST1()) {
// if (list.arg1().isOne()) {
// append(buf, "Infinity");
// return;
// } else if (list.arg1().isMinusOne()) {
// if (ASTNodeFactory.PLUS_PRECEDENCE < precedence) {
// append(buf, "(");
// }
// append(buf, "-Infinity");
// if (ASTNodeFactory.PLUS_PRECEDENCE < precedence) {
// append(buf, ")");
// }
// return;
// } else if (list.arg1().isImaginaryUnit()) {
// append(buf, "I*Infinity");
// return;
// } else if (list.arg1().isNegativeImaginaryUnit()) {
// append(buf, "-I*Infinity");
// return;
// }
// }
// }
// break;
// case ID.Optional:
// if (list.isAST2() && (list.arg1().isBlank() || list.arg1().isPattern())) {
// convertInternal(buf, list.arg1());
// buf.append(":");
// convertInternal(buf, list.arg2());
// return;
// }
// break;
// }
// } else {
// if (list instanceof ASTRealVector || list instanceof ASTRealMatrix) {
// convertList(buf, list);
// return;
// }
// }
}
convertAST(buf, list);
return;
}
if (o instanceof ISignedNumber) {
convertNumber(buf, (ISignedNumber) o, precedence, NO_PLUS_CALL);
return;
}
if (o instanceof IComplexNum) {
convertDoubleComplex(buf, (IComplexNum) o, precedence, NO_PLUS_CALL);
return;
}
if (o instanceof IComplex) {
convertComplex(buf, (IComplex) o, precedence, NO_PLUS_CALL);
return;
}
if (o instanceof ISymbol) {
convertSymbol(buf, (ISymbol) o);
return;
}
if (o instanceof IPatternObject) {
convertPattern(buf, (IPatternObject) o);
return;
}
convertString(buf, o.toString());
}
use of org.matheclipse.parser.client.operator.PostfixOperator in project symja_android_library by axkr.
the class DoubleFormFactory method convertInfixOperator.
public void convertInfixOperator(ISymbol head, final StringBuilder buf, final IAST list, final InfixOperator oper, final int precedence) {
if (list.isAST2()) {
if (oper.getPrecedence() < precedence) {
append(buf, "(");
}
if (oper.getGrouping() == InfixOperator.RIGHT_ASSOCIATIVE && list.arg1().head().equals(list.head())) {
append(buf, "(");
} else {
if (oper.getOperatorString().equals("^")) {
final Operator operator = getOperator(list.arg1().topHead());
if (operator instanceof PostfixOperator) {
append(buf, "(");
}
}
}
convertInternal(buf, list.arg1(), oper.getPrecedence(), false);
if (oper.getGrouping() == InfixOperator.RIGHT_ASSOCIATIVE && list.arg1().head().equals(list.head())) {
append(buf, ")");
} else {
if (oper.getOperatorString().equals("^")) {
final Operator operator = getOperator(list.arg1().topHead());
if (operator instanceof PostfixOperator) {
append(buf, ")");
}
}
}
append(buf, oper.getOperatorString());
if (oper.getGrouping() == InfixOperator.LEFT_ASSOCIATIVE && list.arg2().head().equals(list.head())) {
append(buf, "(");
}
convertInternal(buf, list.arg2(), oper.getPrecedence(), false);
if (oper.getGrouping() == InfixOperator.LEFT_ASSOCIATIVE && list.arg2().head().equals(list.head())) {
append(buf, ")");
}
if (oper.getPrecedence() < precedence) {
append(buf, ")");
}
return;
}
if (oper.getPrecedence() < precedence) {
append(buf, "(");
}
if (//
list.size() > 3 && (head.equals(S.Equal) || head.equals(S.Unequal) || head.equals(S.Greater) || head.equals(S.GreaterEqual) || head.equals(S.Less) || head.equals(S.LessEqual))) {
convertInternal(buf, list.arg1(), oper.getPrecedence(), false);
for (int i = 2; i < list.size(); i++) {
append(buf, oper.getOperatorString());
convertInternal(buf, list.get(i), oper.getPrecedence(), false);
if (i < list.size() - 1) {
buf.append(" && ");
convertInternal(buf, list.get(i), oper.getPrecedence(), false);
}
}
} else {
if (list.size() > 1) {
convertInternal(buf, list.arg1(), oper.getPrecedence(), false);
}
for (int i = 2; i < list.size(); i++) {
append(buf, oper.getOperatorString());
convertInternal(buf, list.get(i), oper.getPrecedence(), false);
}
}
if (oper.getPrecedence() < precedence) {
append(buf, ")");
}
}
Aggregations