Skip to content

Commit 4466bd6

Browse files
Move precedence up by one to prepare for **
1 parent df2396d commit 4466bd6

4 files changed

Lines changed: 28 additions & 23 deletions

File tree

src/main/java/net/marcellperger/mathexpr/SymbolInfo.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
public enum SymbolInfo {
2020
// Let's say that precedence 0 is for (parens) OR literals - TODO add a class?? but it wouldn't actually be used !
2121
// POW(PowOperation.class, 1, GroupingDirection.RightToLeft, "**"),
22-
MUL(MulOperation.class, 1, GroupingDirection.LeftToRight, "*", MulOperation::new),
23-
DIV(DivOperation.class, 1, GroupingDirection.LeftToRight, "/", DivOperation::new),
22+
MUL(MulOperation.class, 2, GroupingDirection.LeftToRight, "*", MulOperation::new),
23+
DIV(DivOperation.class, 2, GroupingDirection.LeftToRight, "/", DivOperation::new),
2424

25-
ADD(AddOperation.class, 2, GroupingDirection.LeftToRight, "+", AddOperation::new),
26-
SUB(SubOperation.class, 2, GroupingDirection.LeftToRight, "-", SubOperation::new),
25+
ADD(AddOperation.class, 3, GroupingDirection.LeftToRight, "+", AddOperation::new),
26+
SUB(SubOperation.class, 3, GroupingDirection.LeftToRight, "-", SubOperation::new),
2727
;
2828

2929
public static final Map<Class<? extends MathSymbol>, SymbolInfo> CLS_TO_INFO_MAP;

src/main/java/net/marcellperger/mathexpr/parser/Parser.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public Parser(String src_) {
3030
}
3131

3232
public MathSymbol parse() throws ExprParseException {
33-
MathSymbol sym = parseInfixPrecedenceLevel(2); // TODO compute max prec level
33+
MathSymbol sym = parseInfixPrecedenceLevel(3); // TODO compute max prec level
3434
discardWhitespace();
3535
if(notEof()) throw new ExprParseException("Syntax error: didn't reach end of input");
3636
return sym;
@@ -69,9 +69,10 @@ public MathSymbol parse() throws ExprParseException {
6969
return parseDoubleLiteral_null();
7070
}
7171

72+
private static final int PREC_IN_PARENS = 3; // TODO compute max prec level
7273
public @Nullable MathSymbol parseParens() throws ExprParseException {
7374
advanceExpectNext('(');
74-
MathSymbol sym = parseInfixPrecedenceLevel(2); // TODO use .parse() when it is completed
75+
MathSymbol sym = parseInfixPrecedenceLevel(PREC_IN_PARENS); // TODO use .parse() when it is completed
7576
advanceExpectNext(')');
7677
return sym;
7778
}
@@ -81,11 +82,12 @@ public MathSymbol parseInfixPrecedenceLevel(int level) throws ExprParseException
8182
if(level == 0) {
8283
return parseParensOrLiteral();
8384
}
84-
85+
// TODO remove this next-line:
86+
if(SymbolInfo.PREC_TO_INFO_MAP.get(level) == null) { return parseInfixPrecedenceLevel(level - 1); }
8587
Set<SymbolInfo> symbols = Util.requireNonEmptyNonNull(SymbolInfo.PREC_TO_INFO_MAP.get(level));
8688
@Nullable GroupingDirection dirn = symbols.stream()
8789
.map(sm -> sm.groupingDirection).distinct().collect(UtilCollectors.singleItem());
88-
assert dirn == GroupingDirection.LeftToRight: "RTL/unknown operators not implemented yet";
90+
assert dirn == GroupingDirection.LeftToRight: "RTL/unknown operators not implemented yet"; // TODO
8991
Map<String, SymbolInfo> infixToSymbolInfo = symbols.stream().collect( // TODO pre-compute/cache these
9092
Collectors.toUnmodifiableMap(
9193
si -> Objects.requireNonNull(si.infix, "null infix not allowed for parseInfixPrecedenceLevel"),

src/main/java/net/marcellperger/mathexpr/util/Util.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ protected Util() {}
5656
return collection;
5757
}
5858
@SuppressWarnings("UnusedReturnValue")
59-
@Contract("_ -> param1")
60-
public static <C extends Collection<?>> @NotNull C requireNonEmptyNonNull(@NotNull C collection) {
59+
@Contract("null -> fail; _ -> param1")
60+
public static <C extends Collection<?>> @NotNull C requireNonEmptyNonNull(C collection) {
6161
Objects.requireNonNull(collection);
6262
// IllegalArgumentException might not always be appropriate
6363
if(collection.isEmpty()) throw new IllegalArgumentException("Argument must not be empty");

src/test/java/net/marcellperger/mathexpr/parser/ParserTest.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import static org.junit.jupiter.api.Assertions.*;
1616

1717
class ParserTest {
18+
public static final int MUL_PREC = 2;
19+
public static final int ADD_PREC = 3;
20+
1821
boolean nocache = false;
1922

2023
void assertInfixParsesTo(String src, int level, MathSymbol expected) {
@@ -60,27 +63,27 @@ void assertInfixParsesToInner(String src, int level, MathSymbol expected) {
6063

6164
@Test
6265
void parseInfixPrecedenceLevel() {
63-
assertInfixParsesTo("1.0/2.0", 1,
66+
assertInfixParsesTo("1.0/2.0", MUL_PREC,
6467
new DivOperation(new BasicDoubleSymbol(1.0), new BasicDoubleSymbol(2.0)));
65-
assertInfixParsesTo(".3*6.", 1,
68+
assertInfixParsesTo(".3*6.", MUL_PREC,
6669
new MulOperation(new BasicDoubleSymbol(.3), new BasicDoubleSymbol(6.)));
67-
assertInfixParsesTo("2.1*5.3+1.1", 2,
70+
assertInfixParsesTo("2.1*5.3+1.1", ADD_PREC,
6871
new AddOperation(new MulOperation(new BasicDoubleSymbol(2.1), new BasicDoubleSymbol(5.3)), new BasicDoubleSymbol(1.1)));
69-
assertInfixParsesTo("0.9-2.1/.3", 2,
72+
assertInfixParsesTo("0.9-2.1/.3", ADD_PREC,
7073
new SubOperation(new BasicDoubleSymbol(0.9), new DivOperation(new BasicDoubleSymbol(2.1), new BasicDoubleSymbol(.3))));
71-
assertInfixParsesTo("(2.2+1.1)+3.7", 2,
74+
assertInfixParsesTo("(2.2+1.1)+3.7", ADD_PREC,
7275
new AddOperation(new AddOperation(new BasicDoubleSymbol(2.2), new BasicDoubleSymbol(1.1)), new BasicDoubleSymbol(3.7)));
73-
assertInfixParsesTo(CommonData.getBigData1_groupingParens(), 2);
74-
assertInfixParsesTo(CommonData.getBigData2_groupingParens(), 2);
75-
assertInfixParsesTo("2.2+1.1+3.7", 2,
76+
assertInfixParsesTo(CommonData.getBigData1_groupingParens(), ADD_PREC);
77+
assertInfixParsesTo(CommonData.getBigData2_groupingParens(), ADD_PREC);
78+
assertInfixParsesTo("2.2+1.1+3.7", ADD_PREC,
7679
new AddOperation(new AddOperation(new BasicDoubleSymbol(2.2), new BasicDoubleSymbol(1.1)), new BasicDoubleSymbol(3.7)));
77-
assertInfixParsesTo("2.2+1.1+3.7+0.2", 2,
80+
assertInfixParsesTo("2.2+1.1+3.7+0.2", ADD_PREC,
7881
new AddOperation(new AddOperation(new AddOperation(new BasicDoubleSymbol(2.2), new BasicDoubleSymbol(1.1)), new BasicDoubleSymbol(3.7)), new BasicDoubleSymbol(0.2)));
79-
assertInfixParsesTo(CommonData.getBigData1_minimumParens(), 2);
80-
assertInfixParsesTo(CommonData.getBigData2_minimumParens(), 2);
81-
assertInfixParsesTo(".9/2./3.3", 1,
82+
assertInfixParsesTo(CommonData.getBigData1_minimumParens(), ADD_PREC);
83+
assertInfixParsesTo(CommonData.getBigData2_minimumParens(), ADD_PREC);
84+
assertInfixParsesTo(".9/2./3.3", MUL_PREC,
8285
new DivOperation(new DivOperation(new BasicDoubleSymbol(.9), new BasicDoubleSymbol(2.)), new BasicDoubleSymbol(3.3)));
83-
assertInfixParsesTo(".9/2./3.3", 2,
86+
assertInfixParsesTo(".9/2./3.3", ADD_PREC,
8487
new DivOperation(new DivOperation(new BasicDoubleSymbol(.9), new BasicDoubleSymbol(2.)), new BasicDoubleSymbol(3.3)));
8588
}
8689

0 commit comments

Comments
 (0)