Skip to content

Commit 93d07ba

Browse files
Initial commit
0 parents  commit 93d07ba

33 files changed

Lines changed: 1624 additions & 0 deletions

.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea/modules.xml
8+
.idea/jarRepositories.xml
9+
.idea/compiler.xml
10+
.idea/libraries/
11+
*.iws
12+
*.iml
13+
*.ipr
14+
15+
### Eclipse ###
16+
.apt_generated
17+
.classpath
18+
.factorypath
19+
.project
20+
.settings
21+
.springBeans
22+
.sts4-cache
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
build/
31+
!**/src/main/**/build/
32+
!**/src/test/**/build/
33+
34+
### VS Code ###
35+
.vscode/
36+
37+
### Mac OS ###
38+
.DS_Store

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>First</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>21</maven.compiler.source>
13+
<maven.compiler.target>21</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.jetbrains</groupId>
19+
<artifactId>annotations</artifactId>
20+
<version>24.1.0</version>
21+
<scope>compile</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.junit.jupiter</groupId>
25+
<artifactId>junit-jupiter</artifactId>
26+
<version>[5.10.2,6.0)</version>
27+
<scope>test</scope>
28+
</dependency>
29+
</dependencies>
30+
31+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package net.marcellperger.first;
2+
3+
4+
public class AddOperation extends LTRBinaryOperationLeftRight {
5+
public AddOperation(MathSymbol left_, MathSymbol right_) {
6+
super(left_, right_);
7+
}
8+
9+
@Override
10+
public double calculateValue() {
11+
return left.calculateValue() + right.calculateValue();
12+
}
13+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package net.marcellperger.first;
2+
3+
import java.util.Objects;
4+
5+
public class BasicDoubleSymbol implements LeafNode {
6+
double value;
7+
8+
@Override
9+
public String toString() {
10+
return "BasicDoubleSymbol{value=" + value + '}';
11+
}
12+
13+
public BasicDoubleSymbol(double value) {
14+
this.value = value;
15+
}
16+
17+
@Override
18+
public double calculateValue() {
19+
return value;
20+
}
21+
22+
@Override
23+
public String fmt() {
24+
return String.valueOf(value);
25+
}
26+
27+
@Override
28+
public boolean equals(Object o) {
29+
if (this == o) return true;
30+
if (o == null || getClass() != o.getClass()) return false;
31+
BasicDoubleSymbol that = (BasicDoubleSymbol) o;
32+
return Double.compare(value, that.value) == 0;
33+
}
34+
35+
@Override
36+
public int hashCode() {
37+
return Objects.hash(value);
38+
}
39+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package net.marcellperger.first;
2+
3+
4+
@FunctionalInterface
5+
public interface BinOpBiConstructor<R extends MathSymbol> {
6+
R construct(MathSymbol left, MathSymbol right);
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package net.marcellperger.first;
2+
3+
public interface BinaryOperation extends MathSymbol {
4+
MathSymbol getLeft();
5+
MathSymbol getRight();
6+
7+
@Override
8+
default String fmt() {
9+
return "%s(%s, %s)".formatted(this.getClass().getSimpleName(), getLeft().fmt(), getRight().fmt());
10+
}
11+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package net.marcellperger.first;
2+
3+
// I wish I could put these on 1 line like in Rust like
4+
// import org.jetbrains.annotations.{Contract, NotNull, Nullable}
5+
// This would be especially nice as the import paths are ENORMOUS
6+
import org.jetbrains.annotations.Contract;
7+
import org.jetbrains.annotations.NotNull;
8+
import org.jetbrains.annotations.Nullable;
9+
10+
import java.util.Objects;
11+
12+
/**
13+
* Adds the {@link #left} and {@link #right} fields along with getters (TODO setters?)
14+
* and a better {@link #fmt()} (using {@link #getInfixInst()} and {@link #getGroupingDirectionInst()})
15+
*/
16+
public abstract class BinaryOperationLeftRight implements BinaryOperation {
17+
MathSymbol left;
18+
MathSymbol right;
19+
20+
public BinaryOperationLeftRight(MathSymbol left_, MathSymbol right_) {
21+
left = left_;
22+
right = right_;
23+
}
24+
25+
@Override
26+
public MathSymbol getLeft() {
27+
return left;
28+
}
29+
30+
@Override
31+
public MathSymbol getRight() {
32+
return right;
33+
}
34+
35+
@Contract(pure = true)
36+
public @Nullable String getInfixInst() {
37+
return SymbolInfo.infixFromClass(getClass());
38+
}
39+
40+
@Contract(pure = true)
41+
public @Nullable GroupingDirection getGroupingDirectionInst() {
42+
return SymbolInfo.groupingDirectionFromClass(getClass());
43+
}
44+
45+
@Override
46+
public String fmt() {
47+
String infix = getInfixInst();
48+
if(infix == null) return BinaryOperation.super.fmt(); // fallback
49+
return "%s %s %s".formatted(
50+
left.fmtWithParensIfRequired(instPrecedenceInt(), _parensRequiredIfEqual(LeftRight.LEFT)),
51+
infix,
52+
right.fmtWithParensIfRequired(instPrecedenceInt(), _parensRequiredIfEqual(LeftRight.RIGHT)));
53+
}
54+
55+
protected boolean _parensRequiredIfEqual(@NotNull LeftRight side) {
56+
// return true;
57+
// This code be lookin' proper Rust-y
58+
return switch (getGroupingDirectionInst()) {
59+
case null -> true;
60+
case LeftToRight -> side == LeftRight.RIGHT; // e.g. 1 - 2 + 3 == (1 - 2) + 3 != 1 - (2 + 3)
61+
case RightToLeft -> side == LeftRight.LEFT; // e.g. 2**3**4 == 2**(3**4) != (2**3)**4
62+
};
63+
}
64+
65+
@Override
66+
public boolean equals(Object o) {
67+
if (this == o) return true;
68+
if (o == null || getClass() != o.getClass()) return false;
69+
BinaryOperationLeftRight that = (BinaryOperationLeftRight) o;
70+
return Objects.equals(left, that.left) && Objects.equals(right, that.right);
71+
}
72+
73+
@Override
74+
public int hashCode() {
75+
return Objects.hash(left, right);
76+
}
77+
78+
@Override
79+
public String toString() {
80+
return getClass().getSimpleName() + "{left=" + left + ", right=" + right + '}';
81+
}
82+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package net.marcellperger.first;
2+
3+
4+
public class DivOperation extends LTRBinaryOperationLeftRight {
5+
public DivOperation(MathSymbol left_, MathSymbol right_) {
6+
super(left_, right_);
7+
}
8+
9+
@Override
10+
public double calculateValue() {
11+
return left.calculateValue() / right.calculateValue();
12+
}
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package net.marcellperger.first;
2+
3+
public enum GroupingDirection {
4+
LeftToRight,
5+
RightToLeft,
6+
}

0 commit comments

Comments
 (0)