Skip to content

Commit

Permalink
Issue #18: Setting for disabling lazy evaluation of multiplication
Browse files Browse the repository at this point in the history
see especially MainTestCase#testGithub18() test case
  • Loading branch information
axkr committed Mar 2, 2018
1 parent bc3e6df commit b63e5a1
Show file tree
Hide file tree
Showing 13 changed files with 454 additions and 357 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,9 @@ public class Config {
public static boolean isFileSystemEnabled(EvalEngine engine) {
return FILESYSTEM_ENABLED || engine.isFileSystemEnabled();
}

/**
* If <code>true</code> the <code>*</code> operator must be written for a <code>Times()</code> expression.
*/
public static boolean EXPLICIT_TIMES_OPERATOR = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ private IExpr getFactor() throws SyntaxError {
}
getNextToken();
if (fToken == TT_PRECEDENCE_OPEN) {
return getTimes(temp);
if (!Config.EXPLICIT_TIMES_OPERATOR) {
return getTimes(temp);
}
}
if (fToken == TT_ARGUMENTS_OPEN) {
return getFunctionArguments(temp);
Expand Down Expand Up @@ -1068,15 +1070,15 @@ private IExpr parseExpression(IExpr lhs, final int min_precedence) {
// '\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 =
// fFactory.createFunction(fFactory.createSymbol(oper.getFunctionName()),
// lhs, rhs);
continue;

if (!Config.EXPLICIT_TIMES_OPERATOR) {
// 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);
continue;
}
}
} else {
if (fToken == TT_DERIVATIVE) {
Expand Down Expand Up @@ -1150,18 +1152,17 @@ private IExpr parseLookaheadOperator(final int min_precedence) {
}
if ((fToken == TT_LIST_OPEN) || (fToken == TT_PRECEDENCE_OPEN) || (fToken == TT_IDENTIFIER)
|| (fToken == TT_STRING) || (fToken == TT_DIGIT) || (fToken == TT_SLOT)) {
// if (fPackageMode && fRecursionDepth < 1) {
// return rhs;
// }
// lazy evaluation of multiplication
InfixExprOperator timesOperator = (InfixExprOperator) fFactory.get("Times");
if (timesOperator.getPrecedence() > min_precedence) {
rhs = parseExpression(rhs, timesOperator.getPrecedence());
continue;
} else if ((timesOperator.getPrecedence() == min_precedence)
&& (timesOperator.getGrouping() == InfixExprOperator.RIGHT_ASSOCIATIVE)) {
rhs = parseExpression(rhs, timesOperator.getPrecedence());
continue;
if (!Config.EXPLICIT_TIMES_OPERATOR) {
// lazy evaluation of multiplication
InfixExprOperator timesOperator = (InfixExprOperator) fFactory.get("Times");
if (timesOperator.getPrecedence() > min_precedence) {
rhs = parseExpression(rhs, timesOperator.getPrecedence());
continue;
} else if ((timesOperator.getPrecedence() == min_precedence)
&& (timesOperator.getGrouping() == InfixExprOperator.RIGHT_ASSOCIATIVE)) {
rhs = parseExpression(rhs, timesOperator.getPrecedence());
continue;
}
}
} else {
if (fToken == TT_DERIVATIVE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.HashMap;
import java.util.List;

import org.matheclipse.core.basic.Config;
import org.matheclipse.parser.client.Characters;
import org.matheclipse.parser.client.SyntaxError;

Expand Down Expand Up @@ -162,7 +163,7 @@ public class ExprScanner {
* Token type: pattern placeholder '_'
*/
final static public int TT_BLANK = 142;

final static public int TT_BLANK_BLANK = 143;

final static public int TT_BLANK_BLANK_BLANK = 144;
Expand Down Expand Up @@ -689,38 +690,40 @@ protected Object[] getNumberString() {
dFlag = fCurrentChar;
}
getChar();
if (firstCh == '0') {
switch (fCurrentChar) {
case 'b': // binary format
numFormat = 2;
startPosition = fCurrentPosition;
getChar();
break;
case 'B': // binary format
numFormat = 2;
startPosition = fCurrentPosition;
getChar();
break;
case 'o': // octal format
numFormat = 8;
startPosition = fCurrentPosition;
getChar();
break;
case 'O': // octal format
numFormat = 8;
startPosition = fCurrentPosition;
getChar();
break;
case 'x': // hexadecimal format
numFormat = 16;
startPosition = fCurrentPosition;
getChar();
break;
case 'X': // hexadecimal format
numFormat = 16;
startPosition = fCurrentPosition;
getChar();
break;
if (Config.EXPLICIT_TIMES_OPERATOR) {
if (firstCh == '0') {
switch (fCurrentChar) {
case 'b': // binary format
numFormat = 2;
startPosition = fCurrentPosition;
getChar();
break;
case 'B': // binary format
numFormat = 2;
startPosition = fCurrentPosition;
getChar();
break;
case 'o': // octal format
numFormat = 8;
startPosition = fCurrentPosition;
getChar();
break;
case 'O': // octal format
numFormat = 8;
startPosition = fCurrentPosition;
getChar();
break;
case 'x': // hexadecimal format
numFormat = 16;
startPosition = fCurrentPosition;
getChar();
break;
case 'X': // hexadecimal format
numFormat = 16;
startPosition = fCurrentPosition;
getChar();
break;
}
}
}

Expand All @@ -744,14 +747,8 @@ protected Object[] getNumberString() {
if ((fCurrentChar == '.') && (dFlag != ' ')) {
break;
}
// if ((dFlag == 'E') || (dFlag == 'e')) {
// break;
// }
dFlag = fCurrentChar;
getChar();
// if ((ch == '-') || (ch == '+')) {
// getChar();
// }
} else {
getChar();
}
Expand All @@ -760,16 +757,20 @@ protected Object[] getNumberString() {
numFormat = -1;
}
}
if (numFormat < 0) {
if ((fCurrentChar == 'E') || (fCurrentChar == 'e')) {

if ((fCurrentChar == 'E') || (fCurrentChar == 'e')) {
if (Config.EXPLICIT_TIMES_OPERATOR) {
numFormat = -1;
getChar();
if ((fCurrentChar == '+') || (fCurrentChar == '-')) {
getChar();
}
while (((fCurrentChar >= '0') && (fCurrentChar <= '9'))) {
getChar();
}
} else {
}
} else {
if (numFormat < 0) {
if (fCurrentChar == '*') {
int lastPosition = fCurrentPosition;
getChar();
Expand All @@ -791,6 +792,7 @@ protected Object[] getNumberString() {
}
}
}

// }
int endPosition = fCurrentPosition--;
result[0] = fInputString.substring(startPosition, --endPosition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ private ASTNode getFactor() throws SyntaxError {
}
getNextToken();
if (fToken == TT_PRECEDENCE_OPEN) {
return getTimes(temp);
if (!Config.EXPLICIT_TIMES_OPERATOR) {
return getTimes(temp);
}
}
if (fToken == TT_ARGUMENTS_OPEN) {
return getFunctionArguments(temp);
Expand Down Expand Up @@ -801,12 +803,14 @@ private ASTNode parseExpression(ASTNode lhs, final int min_precedence) {
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;
if (!Config.EXPLICIT_TIMES_OPERATOR) {
// 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) {
Expand Down Expand Up @@ -881,15 +885,17 @@ private ASTNode parseLookaheadOperator(final int min_precedence) {
}
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;
if (!Config.EXPLICIT_TIMES_OPERATOR) {
// 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.List;
import java.util.Stack;

import org.matheclipse.core.basic.Config;
import org.matheclipse.parser.client.ast.IParserFactory;
import org.matheclipse.parser.client.operator.Operator;

Expand Down Expand Up @@ -549,39 +550,41 @@ protected Object[] getNumberString() {
dFlag = fCurrentChar;
}
getChar();
if (firstCh == '0') {
switch (fCurrentChar) {
case 'b': // binary format
numFormat = 2;
startPosition = fCurrentPosition;
getChar();
break;
case 'B': // binary format
numFormat = 2;
startPosition = fCurrentPosition;
getChar();
break;
case 'o': // octal format
numFormat = 8;
startPosition = fCurrentPosition;
getChar();
break;
case 'O': // octal format
numFormat = 8;
startPosition = fCurrentPosition;
getChar();
break;
case 'x': // hexadecimal format
numFormat = 16;
startPosition = fCurrentPosition;
getChar();
break;
case 'X': // hexadecimal format
numFormat = 16;
startPosition = fCurrentPosition;
getChar();
break;
default:
if (Config.EXPLICIT_TIMES_OPERATOR) {
if (firstCh == '0') {
switch (fCurrentChar) {
case 'b': // binary format
numFormat = 2;
startPosition = fCurrentPosition;
getChar();
break;
case 'B': // binary format
numFormat = 2;
startPosition = fCurrentPosition;
getChar();
break;
case 'o': // octal format
numFormat = 8;
startPosition = fCurrentPosition;
getChar();
break;
case 'O': // octal format
numFormat = 8;
startPosition = fCurrentPosition;
getChar();
break;
case 'x': // hexadecimal format
numFormat = 16;
startPosition = fCurrentPosition;
getChar();
break;
case 'X': // hexadecimal format
numFormat = 16;
startPosition = fCurrentPosition;
getChar();
break;
default:
}
}
}

Expand Down Expand Up @@ -614,16 +617,19 @@ protected Object[] getNumberString() {
numFormat = -1;
}
}
if (numFormat < 0) {
if ((fCurrentChar == 'E') || (fCurrentChar == 'e')) {

if ((fCurrentChar == 'E') || (fCurrentChar == 'e')) {
if (Config.EXPLICIT_TIMES_OPERATOR) {
getChar();
if ((fCurrentChar == '+') || (fCurrentChar == '-')) {
getChar();
}
while ((fCurrentChar >= '0') && (fCurrentChar <= '9')) {
getChar();
}
} else {
}
} else {
if (numFormat < 0) {
if (fCurrentChar == '*') {
int lastPosition = fCurrentPosition;
getChar();
Expand All @@ -645,6 +651,7 @@ protected Object[] getNumberString() {
}
}
}

int endPosition = fCurrentPosition--;
result[0] = fInputString.substring(startPosition, --endPosition);
result[1] = Integer.valueOf(numFormat);
Expand Down
Loading

0 comments on commit b63e5a1

Please sign in to comment.