Skip to content

Commit e39dd59

Browse files
committed
new parser
1 parent 114db0c commit e39dd59

File tree

11 files changed

+164
-322
lines changed

11 files changed

+164
-322
lines changed

build.gradle

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ group = 'io.github.jbock-java'
88

99
compileJava {
1010
options.encoding = 'UTF-8'
11-
sourceCompatibility = '17'
12-
targetCompatibility = '17'
13-
options.compilerArgs << '--release'
14-
options.compilerArgs << '17'
11+
sourceCompatibility = '21'
12+
targetCompatibility = '21'
1513
}
1614

1715
jar {

src/main/java/io/polypen/Expressions.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/main/java/io/polypen/Main.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.polypen;
22

3-
import io.polypen.Expressions.Expression;
3+
import io.polypen.parse.Parser;
4+
import io.polypen.parse.Parser.ListExpr;
45

56
import java.util.Scanner;
67

@@ -13,7 +14,7 @@ public static void main(String[] args) {
1314
String line = in.nextLine();
1415
sb.append(line);
1516
}
16-
Expression expression = Parser.parse(sb.toString());
17-
System.out.println(expression.eval());
17+
ListExpr expression = Parser.parse(sb.toString());
18+
System.out.println(Parser.eval(expression));
1819
}
1920
}

src/main/java/io/polypen/Monomial.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,31 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
public record Monomial(int coefficient, int exponent) {
6+
public record Monomial(int coefficient, int degree) {
77

88
public static final Monomial ZERO = new Monomial(0, 0);
99

10+
public static Monomial constant(int n) {
11+
return new Monomial(n, 0);
12+
}
13+
1014
public Polynomial multiply(Polynomial p) {
11-
List<Integer> result = new ArrayList<>(p.degree() + exponent + 1);
12-
for (int i = 0; i < exponent; i++) {
15+
List<Integer> result = new ArrayList<>(p.degree() + degree + 1);
16+
for (int i = 0; i < degree; i++) {
1317
result.add(0);
1418
}
1519
for (int i = 0; i <= p.degree(); i++) {
1620
result.add(coefficient * (p.coefficient(i)));
1721
}
1822
return new Polynomial(result);
1923
}
24+
25+
public Polynomial polynomial() {
26+
List<Integer> coefficients = new ArrayList<>(degree + 1);
27+
for (int i = 0; i < degree; i++) {
28+
coefficients.add(0);
29+
}
30+
coefficients.add(coefficient);
31+
return new Polynomial(coefficients);
32+
}
2033
}

src/main/java/io/polypen/Parser.java

Lines changed: 0 additions & 142 deletions
This file was deleted.

src/main/java/io/polypen/Polynomial.java

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.polypen;
22

3+
import io.polypen.parse.Parser;
4+
35
import java.util.ArrayList;
46
import java.util.List;
57

@@ -15,55 +17,7 @@ public final class Polynomial {
1517
}
1618

1719
public static Polynomial parse(String s) {
18-
NestingInfo nestingInfo = unnest(s.trim());
19-
return new Polynomial(Parser.parsePolynomial(nestingInfo.term)).multiply(nestingInfo.sign);
20-
}
21-
22-
private static NestingInfo unnest(String s) {
23-
int nestingLevel = -1;
24-
int sign = 1;
25-
int start = 0;
26-
int end = 0;
27-
for (int i = 0; i < s.length(); i++) {
28-
char c = s.charAt(i);
29-
switch (c) {
30-
case '(' -> {
31-
nestingLevel = Math.max(nestingLevel, 0) + 1;
32-
}
33-
case ')' -> {
34-
if (end == 0) {
35-
end = i;
36-
}
37-
nestingLevel--;
38-
if (nestingLevel < 0) {
39-
throw new IllegalStateException("Illegal nesting");
40-
}
41-
}
42-
case '-' -> {
43-
if (start == 0) {
44-
sign *= -1;
45-
}
46-
}
47-
case '+', ' ' -> {
48-
//ignore
49-
}
50-
default -> {
51-
if (start == 0) {
52-
start = i;
53-
}
54-
}
55-
}
56-
}
57-
if (nestingLevel > 0) {
58-
throw new IllegalStateException("Illegal nesting");
59-
}
60-
if (nestingLevel == -1) {
61-
return new NestingInfo(1, s);
62-
}
63-
return new NestingInfo(sign, s.substring(start, end));
64-
}
65-
66-
private record NestingInfo(int sign, String term) {
20+
return Parser.eval(Parser.parse(s));
6721
}
6822

6923
public Polynomial add(Polynomial other) {

src/main/java/io/polypen/Util.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/main/java/io/polypen/parse/Macro.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,21 @@
22

33
import io.polypen.parse.Parser.Expr;
44
import io.polypen.parse.Parser.ListExpr;
5-
import io.polypen.parse.Parser.MinusExpr;
65
import io.polypen.parse.Parser.MultExpr;
76
import io.polypen.parse.Parser.NumberExpr;
8-
import io.polypen.parse.Parser.PlusExpr;
97
import io.polypen.parse.Parser.VarExp;
108

119
import java.util.ArrayList;
1210
import java.util.List;
1311

1412
public class Macro {
1513

16-
static ListExpr applyStarMacro(ListExpr listExpr) {
14+
static List<Expr> applyStarMacro(ListExpr listExpr) {
1715
List<Expr> exprs = listExpr.value();
1816
List<Expr> exprsCopy = new ArrayList<>(exprs.size());
1917
List<Expr> region = new ArrayList<>(exprs.size());
2018
Expr previous = null;
21-
for (int i = 0; i < exprs.size(); i++) {
22-
Expr expr = exprs.get(i);
19+
for (Expr expr : exprs) {
2320
if (isStrongBind(previous) && (isStrongBind(expr) || !region.isEmpty())) {
2421
region.add(previous);
2522
} else {
@@ -33,23 +30,28 @@ static ListExpr applyStarMacro(ListExpr listExpr) {
3330
}
3431
previous = expr;
3532
}
33+
if (exprsCopy.isEmpty()) {
34+
return listExpr.value();
35+
}
3636
if (region.isEmpty()) {
3737
exprsCopy.add(previous);
3838
} else {
3939
region.add(previous);
4040
exprsCopy.add(new ListExpr(region));
4141
}
42-
return new ListExpr(exprsCopy);
42+
return exprsCopy;
4343
}
4444

4545
public static boolean isStrongBind(Expr expr) {
4646
if (expr == null) {
4747
return false;
4848
}
49-
return expr instanceof MultExpr || expr instanceof VarExp || expr instanceof NumberExpr || expr instanceof ListExpr;
50-
}
51-
52-
public static boolean isPlus(Expr expr) {
53-
return expr instanceof PlusExpr || expr instanceof MinusExpr;
49+
return switch (expr) {
50+
case ListExpr ignored -> true;
51+
case MultExpr ignored -> true;
52+
case NumberExpr ignored -> true;
53+
case VarExp ignored -> true;
54+
default -> false;
55+
};
5456
}
5557
}

0 commit comments

Comments
 (0)