Skip to content

Commit e03c984

Browse files
Make it put no spaces around **
1 parent 07e63c0 commit e03c984

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public MathSymbol getRight() {
3737
return SymbolInfo.infixFromClass(getClass());
3838
}
3939

40+
@Contract(pure = true)
41+
public @Nullable String getSpacesAroundInfixInst() {
42+
return SymbolInfo.spacesAroundInfixFromClass(getClass());
43+
}
44+
4045
@Contract(pure = true)
4146
public @Nullable GroupingDirection getGroupingDirectionInst() {
4247
return SymbolInfo.groupingDirectionFromClass(getClass());
@@ -46,9 +51,11 @@ public MathSymbol getRight() {
4651
public String fmt() {
4752
String infix = getInfixInst();
4853
if(infix == null) return BinaryOperation.super.fmt(); // fallback
49-
return "%s %s %s".formatted(
54+
return "%s%s%s%s%s".formatted(
5055
left.fmtWithParensIfRequired(instPrecedenceInt(), _parensRequiredIfEqual(LeftRight.LEFT)),
56+
getSpacesAroundInfixInst(),
5157
infix,
58+
getSpacesAroundInfixInst(),
5259
right.fmtWithParensIfRequired(instPrecedenceInt(), _parensRequiredIfEqual(LeftRight.RIGHT)));
5360
}
5461

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919

2020
public enum SymbolInfo {
2121
// Let's say that precedence 0 is for (parens) OR literals - TODO add a class?? but it wouldn't actually be used !
22-
POW(PowOperation.class, 1, GroupingDirection.RightToLeft, "**", PowOperation::new),
23-
MUL(MulOperation.class, 2, GroupingDirection.LeftToRight, "*", MulOperation::new),
24-
DIV(DivOperation.class, 2, GroupingDirection.LeftToRight, "/", DivOperation::new),
22+
POW(PowOperation.class, 1, GroupingDirection.RightToLeft, "**", "", PowOperation::new),
2523

26-
ADD(AddOperation.class, 3, GroupingDirection.LeftToRight, "+", AddOperation::new),
27-
SUB(SubOperation.class, 3, GroupingDirection.LeftToRight, "-", SubOperation::new),
24+
MUL(MulOperation.class, 2, GroupingDirection.LeftToRight, "*", " ", MulOperation::new),
25+
DIV(DivOperation.class, 2, GroupingDirection.LeftToRight, "/", " ", DivOperation::new),
26+
27+
ADD(AddOperation.class, 3, GroupingDirection.LeftToRight, "+", " ", AddOperation::new),
28+
SUB(SubOperation.class, 3, GroupingDirection.LeftToRight, "-", " ", SubOperation::new),
2829
;
2930

3031
public static final Map<Class<? extends MathSymbol>, SymbolInfo> CLS_TO_INFO_MAP;
@@ -37,22 +38,25 @@ public enum SymbolInfo {
3738
public final Class<? extends MathSymbol> cls;
3839
public final GroupingDirection groupingDirection;
3940
public final String infix;
41+
public final @Nullable String spacesAroundInfix;
4042

4143
@SuppressWarnings("unused") // useful later
4244
SymbolInfo(Class<? extends MathSymbol> cls, int precedence,
43-
GroupingDirection groupingDirection, @Nullable String infix) {
45+
GroupingDirection groupingDirection, @Nullable String spacesAroundInfix, @Nullable String infix) {
4446
this.precedence = precedence;
45-
this.cls = cls; // TODO: private + getters?
47+
this.cls = cls;
4648
this.groupingDirection = groupingDirection;
4749
this.infix = infix;
50+
this.spacesAroundInfix = spacesAroundInfix;
4851
}
4952
SymbolInfo(Class<? extends BinaryOperationLeftRight> cls, int precedence,
50-
GroupingDirection groupingDirection, @Nullable String infix,
53+
GroupingDirection groupingDirection, @Nullable String infix, @Nullable String spacesAroundInfix,
5154
BinOpBiConstructor biConstructor) {
5255
this.precedence = precedence;
5356
this.cls = cls; // TODO: private + getters?
5457
this.groupingDirection = groupingDirection;
5558
this.infix = infix;
59+
this.spacesAroundInfix = spacesAroundInfix;
5660
this.biConstructorCache = biConstructor;
5761
}
5862

@@ -70,6 +74,9 @@ public enum SymbolInfo {
7074
public static @Nullable String infixFromClass(Class<? extends MathSymbol> cls) {
7175
return Util.chainNulls(fromClass(cls), x -> x.infix);
7276
}
77+
public static @Nullable String spacesAroundInfixFromClass(Class<? extends MathSymbol> cls) {
78+
return Util.chainNulls(fromClass(cls), s -> s.spacesAroundInfix);
79+
}
7380

7481
private BinOpBiConstructor biConstructorCache = null;
7582
public @NotNull BinOpBiConstructor getBiConstructor() {

src/test/java/net/marcellperger/mathexpr/CommonData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,10 @@ public static ObjStringPair getBigData2_groupingParens() {
356356
}
357357

358358
public static ObjStringPair getBigData3Pow_minimumParens() {
359-
return new ObjStringPair(getBigData3Pow_obj(), "((-19.7 - 17.1) * (-99.6 * -49.2) * (43.6 ** (-29.1 + -68.3) - (40.6 - 58.4) / ((86.8 + 71.8) * 51.8)) / (72.4 * (-91.1 - -3.2)) - ((-27.1 ** -28.2 / -99.8 / -54.3) ** -50.2 / (-69.8 - 64.5 + 63.9 ** -92.9 + (-24.9 - (58.1 + 13.3))) + (78.0 + -34.5))) ** (12.5 - 52.3 + (32.0 / 43.5 / (37.4 - -11.4)) ** 12.2 ** 33.0) ** (-19.7 ** (11.0 / 67.9) * (46.2 / 97.6 * -13.9))");
359+
return new ObjStringPair(getBigData3Pow_obj(), "((-19.7 - 17.1) * (-99.6 * -49.2) * (43.6**(-29.1 + -68.3) - (40.6 - 58.4) / ((86.8 + 71.8) * 51.8)) / (72.4 * (-91.1 - -3.2)) - ((-27.1**-28.2 / -99.8 / -54.3)**-50.2 / (-69.8 - 64.5 + 63.9**-92.9 + (-24.9 - (58.1 + 13.3))) + (78.0 + -34.5)))**(12.5 - 52.3 + (32.0 / 43.5 / (37.4 - -11.4))**12.2**33.0)**(-19.7**(11.0 / 67.9) * (46.2 / 97.6 * -13.9))");
360360
}
361361

362362
public static ObjStringPair getBigData3Pow_groupingParens() {
363-
return new ObjStringPair(getBigData3Pow_obj(), "(((((-19.7 - 17.1) * (-99.6 * -49.2)) * ((43.6 ** (-29.1 + -68.3)) - ((40.6 - 58.4) / ((86.8 + 71.8) * 51.8)))) / (72.4 * (-91.1 - -3.2))) - ((((((-27.1 ** -28.2) / -99.8) / -54.3) ** -50.2) / (((-69.8 - 64.5) + (63.9 ** -92.9)) + (-24.9 - (58.1 + 13.3)))) + (78.0 + -34.5))) ** (((12.5 - 52.3) + (((32.0 / 43.5) / (37.4 - -11.4)) ** (12.2 ** 33.0))) ** ((-19.7 ** (11.0 / 67.9)) * ((46.2 / 97.6) * -13.9)))");
363+
return new ObjStringPair(getBigData3Pow_obj(), "(((((-19.7 - 17.1) * (-99.6 * -49.2)) * ((43.6**(-29.1 + -68.3)) - ((40.6 - 58.4) / ((86.8 + 71.8) * 51.8)))) / (72.4 * (-91.1 - -3.2))) - ((((((-27.1**-28.2) / -99.8) / -54.3)**-50.2) / (((-69.8 - 64.5) + (63.9**-92.9)) + (-24.9 - (58.1 + 13.3)))) + (78.0 + -34.5)))**(((12.5 - 52.3) + (((32.0 / 43.5) / (37.4 - -11.4))**(12.2**33.0)))**((-19.7**(11.0 / 67.9)) * ((46.2 / 97.6) * -13.9)))");
364364
}
365365
}

src/test/java/net/marcellperger/mathexpr/MathSymbolTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ void test_fmt() {
2121
@Test
2222
void test_fmt__pow() {
2323
// TODO decide spacing: `2.2**3.1` or `2.2 ** 3.1`
24-
assertExprFmt(new ObjStringPair(new PowOperation(new BasicDoubleSymbol(0.2), new BasicDoubleSymbol(5.5)), "0.2 ** 5.5"));
24+
assertExprFmt(new ObjStringPair(new PowOperation(new BasicDoubleSymbol(0.2), new BasicDoubleSymbol(5.5)), "0.2**5.5"));
2525
assertExprFmt(new ObjStringPair(new PowOperation(new PowOperation(new BasicDoubleSymbol(0.2), new BasicDoubleSymbol(5.5)), new BasicDoubleSymbol(3.3)),
26-
"(0.2 ** 5.5) ** 3.3"));
26+
"(0.2**5.5)**3.3"));
2727
assertExprFmt(new ObjStringPair(new PowOperation(new BasicDoubleSymbol(0.2), new PowOperation(new BasicDoubleSymbol(5.5), new BasicDoubleSymbol(3.3))),
28-
"0.2 ** 5.5 ** 3.3"));
28+
"0.2**5.5**3.3"));
2929
assertExprFmt(CommonData.getBigData3Pow_minimumParens());
3030
}
3131

0 commit comments

Comments
 (0)