Skip to content

Commit ca9a5ec

Browse files
committed
refactoring
1 parent 855d462 commit ca9a5ec

File tree

3 files changed

+85
-111
lines changed

3 files changed

+85
-111
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package io.polypen.parse;
22

3+
import io.polypen.parse.Parser.HeadToken;
34
import io.polypen.parse.Parser.ListToken;
45
import io.polypen.parse.Parser.MinusToken;
5-
import io.polypen.parse.Parser.MultListToken;
66
import io.polypen.parse.Parser.MultToken;
7-
import io.polypen.parse.Parser.PlusListToken;
87
import io.polypen.parse.Parser.PlusToken;
98
import io.polypen.parse.Parser.Token;
109
import io.polypen.parse.Parser.VarExp;
1110

1211
import java.util.List;
1312

13+
import static io.polypen.parse.Parser.HeadToken.createMult;
14+
import static io.polypen.parse.Parser.HeadToken.createPlus;
15+
1416
public class Macro {
1517

1618
public static final int B_STRONG = 4;
@@ -25,8 +27,8 @@ public static Token applyStarMacro(Token input) {
2527
if (tokens.size() == 1) {
2628
return applyStarMacro(tokens.getFirst());
2729
}
28-
PlusListToken exprsCopy = PlusListToken.create(tokens.size());
29-
MultListToken region = MultListToken.create(tokens.size());
30+
HeadToken exprsCopy = createPlus(tokens.size());
31+
HeadToken region = createMult(tokens.size());
3032
int[] bound = new int[tokens.size()];
3133
for (int i = 0; i < tokens.size() - 1; i++) {
3234
Token left = tokens.get(i);
@@ -46,7 +48,7 @@ public static Token applyStarMacro(Token input) {
4648
int b = bound[i];
4749
if ((b & B_STRONG) != 0) {
4850
if ((b & B_MINUSBOUND) != 0) {
49-
region.add(MultListToken.of(VarExp.constant(-1), applyStarMacro(token)));
51+
region.add(HeadToken.ofMult(VarExp.constant(-1), applyStarMacro(token)));
5052
} else {
5153
region.add(applyStarMacro(token));
5254
}
@@ -71,7 +73,7 @@ public static Token applyStarMacro(Token input) {
7173
return exprsCopy;
7274
}
7375

74-
private static Token unwrap(MultListToken expr) {
76+
private static Token unwrap(HeadToken expr) {
7577
return expr.size() == 1 ? expr.getFirst() : expr;
7678
}
7779

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

Lines changed: 55 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@
88
import java.io.InputStreamReader;
99
import java.io.PushbackReader;
1010
import java.util.ArrayList;
11+
import java.util.Arrays;
1112
import java.util.List;
1213
import java.util.Objects;
1314
import java.util.stream.Collectors;
14-
import java.util.stream.IntStream;
15+
16+
import static io.polypen.Polynomial.ONE;
17+
import static io.polypen.Polynomial.ZERO;
18+
import static io.polypen.parse.Parser.Symbol.M;
19+
import static io.polypen.parse.Parser.Symbol.P;
1520

1621
public final class Parser {
1722

@@ -111,7 +116,7 @@ public static ListToken parse(String s) {
111116
}
112117
}
113118

114-
public sealed interface Token permits PlusToken, MinusToken, MultToken, ListToken, VarExp, PlusListToken, MultListToken {
119+
public sealed interface Token permits PlusToken, MinusToken, MultToken, ListToken, VarExp, HeadToken {
115120
int size();
116121

117122
Token getFirst();
@@ -198,43 +203,26 @@ public static Polynomial eval(ListToken token) {
198203

199204
private static Polynomial _eval(Token exprs) {
200205
return switch (exprs) {
201-
case PlusListToken listExpr -> {
206+
case HeadToken listExpr -> {
202207
if (listExpr.value.size() == 1) {
203208
yield _eval(listExpr.value().getFirst());
204209
}
205-
if (exprs.size() == 1) {
206-
yield _eval(exprs.getFirst());
207-
}
208-
Polynomial result = Polynomial.ZERO;
209-
for (Token exp : exprs.getExprs()) {
210-
if (isMinus(exp)) {
211-
continue;
210+
yield switch (listExpr.head) {
211+
case P -> {
212+
Polynomial result = ZERO;
213+
for (Token exp : exprs.getExprs()) {
214+
result = result.add(_eval(exp));
215+
}
216+
yield result;
212217
}
213-
if (isPlus(exp)) {
214-
continue;
215-
}
216-
Polynomial p = _eval(exp);
217-
result = result.add(p);
218-
}
219-
yield result;
220-
}
221-
case MultListToken listExpr -> {
222-
if (listExpr.value.size() == 1) {
223-
yield _eval(listExpr.value().getFirst());
224-
}
225-
if (exprs.size() == 1) {
226-
yield _eval(exprs.getFirst());
227-
}
228-
Polynomial result;
229-
result = Polynomial.ONE;
230-
for (Token exp : exprs.getExprs()) {
231-
if (isOperator(exp)) {
232-
continue;
218+
case M -> {
219+
Polynomial result = ONE;
220+
for (Token exp : exprs.getExprs()) {
221+
result = result.multiply(_eval(exp));
222+
}
223+
yield result;
233224
}
234-
Polynomial p = _eval(exp);
235-
result = result.multiply(p);
236-
}
237-
yield result;
225+
};
238226
}
239227
case VarExp varExp -> new Monomial(varExp.factor, varExp.exp).polynomial();
240228
default -> throw new IllegalStateException(exprs.toString());
@@ -258,75 +246,50 @@ private static boolean isMinus(Token token) {
258246
return token instanceof MinusToken;
259247
}
260248

261-
public record MultListToken(List<Token> value) implements Token {
262-
public static MultListToken create(int capacity) {
263-
return new MultListToken(new ArrayList<>(capacity));
249+
private static void addIfNotOperator(List<Token> tokens, Token token) {
250+
if (!isOperator(token)) {
251+
tokens.add(token);
264252
}
253+
}
265254

266-
public static MultListToken of(Token... value) {
267-
return new MultListToken(List.of(value));
255+
enum Symbol {
256+
P('+'), M('*');
257+
final char c;
258+
259+
Symbol(char c) {
260+
this.c = c;
268261
}
269262

270263
@Override
271264
public String toString() {
272-
return value.stream().map(Objects::toString).collect(Collectors.joining(" ", "(* ", ")"));
273-
}
274-
275-
public static MultListToken of(int... value) {
276-
List<Token> list = IntStream.of(value).mapToObj(value1 -> VarExp.constant(value1)).map(s -> (Token) s).toList();
277-
return new MultListToken(list);
278-
}
279-
280-
public void add(Token token) {
281-
addIfNotOperator(value, token);
282-
}
283-
284-
public MultListToken copy() {
285-
return new MultListToken(List.copyOf(value));
286-
}
287-
288-
public void clear() {
289-
value.clear();
290-
}
291-
292-
public boolean isEmpty() {
293-
return value.isEmpty();
265+
return Character.toString(c);
294266
}
267+
}
295268

296-
@Override
297-
public int size() {
298-
return value().size();
269+
public record HeadToken(Symbol head, List<Token> value) implements Token {
270+
public static HeadToken createPlus(int capacity) {
271+
return new HeadToken(P, new ArrayList<>(capacity));
299272
}
300273

301-
@Override
302-
public Token getFirst() {
303-
return value.getFirst();
274+
public static HeadToken createMult(int capacity) {
275+
return new HeadToken(M, new ArrayList<>(capacity));
304276
}
305277

306278
@Override
307-
public List<Token> getExprs() {
308-
return value;
309-
}
310-
}
311-
312-
private static void addIfNotOperator(List<Token> tokens, Token token) {
313-
if (!isOperator(token)) {
314-
tokens.add(token);
279+
public String toString() {
280+
return value.stream().map(Objects::toString).collect(Collectors.joining(" ", "(" + head + " ", ")"));
315281
}
316-
}
317282

318-
public record PlusListToken(List<Token> value) implements Token {
319-
public static PlusListToken create(int capacity) {
320-
return new PlusListToken(new ArrayList<>(capacity));
283+
public static HeadToken ofPlus(Token... value) {
284+
return new HeadToken(P, List.of(value));
321285
}
322286

323-
@Override
324-
public String toString() {
325-
return value.stream().map(Objects::toString).collect(Collectors.joining(" ", "(+ ", ")"));
287+
public static HeadToken ofMult(Token... value) {
288+
return new HeadToken(M, List.of(value));
326289
}
327290

328-
public static PlusListToken of(Token... value) {
329-
return new PlusListToken(List.of(value));
291+
public static HeadToken ofMult(int... value) {
292+
return new HeadToken(M, Arrays.stream(value).mapToObj(VarExp::constant).map(t -> (Token) t).toList());
330293
}
331294

332295
public void add(Token token) {
@@ -351,6 +314,14 @@ public Token getFirst() {
351314
public List<Token> getExprs() {
352315
return value;
353316
}
317+
318+
void clear() {
319+
value.clear();
320+
}
321+
322+
HeadToken copy() {
323+
return new HeadToken(head, List.copyOf(value));
324+
}
354325
}
355326

356327
public record ListToken(List<Token> value) implements Token {

src/test/java/io/polypen/parse/ParserTest.java

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
import org.junit.jupiter.api.Test;
66

77
import static io.polypen.parse.Macro.applyStarMacro;
8+
import static io.polypen.parse.Parser.HeadToken.ofMult;
9+
import static io.polypen.parse.Parser.HeadToken.ofPlus;
810
import static io.polypen.parse.Parser.ListToken;
911
import static io.polypen.parse.Parser.MULT;
10-
import static io.polypen.parse.Parser.MultListToken;
1112
import static io.polypen.parse.Parser.PLUS;
12-
import static io.polypen.parse.Parser.PlusListToken;
1313
import static io.polypen.parse.Parser.Token;
1414
import static io.polypen.parse.Parser.VarExp;
15+
import static io.polypen.parse.Parser.VarExp.constant;
1516
import static io.polypen.parse.Parser.eval;
1617
import static io.polypen.parse.Parser.parse;
1718
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -22,25 +23,25 @@ class ParserTest {
2223
void testParse() {
2324
ListToken result = parse("(a_1^12 + b12^2) * 2");
2425
assertEquals(ListToken.of(
25-
ListToken.of(VarExp.of(12), PLUS, VarExp.of(2)), MULT, VarExp.constant(2)),
26+
ListToken.of(VarExp.of(12), PLUS, VarExp.of(2)), MULT, constant(2)),
2627
result);
2728
}
2829

2930
@Test
3031
void starMacro1() {
3132
ListToken result = parse("1 + 2 * 3");
3233
Token expanded = applyStarMacro(result);
33-
assertEquals(PlusListToken.of(
34-
VarExp.constant(1), MultListToken.of(VarExp.constant(2), VarExp.constant(3))),
34+
assertEquals(ofPlus(
35+
constant(1), ofMult(constant(2), constant(3))),
3536
expanded);
3637
}
3738

3839
@Test
3940
void starMacro5() {
4041
ListToken result = parse("1 + 2 * 3 * 4");
4142
Token expanded = applyStarMacro(result);
42-
assertEquals(PlusListToken.of(
43-
VarExp.constant(1), MultListToken.of(VarExp.constant(2), VarExp.constant(3), VarExp.constant(4))),
43+
assertEquals(ofPlus(
44+
constant(1), ofMult(constant(2), constant(3), constant(4))),
4445
expanded);
4546
}
4647

@@ -49,16 +50,16 @@ void starMacro6() {
4950
ListToken result = parse("2 * 3 * 4");
5051
Token expanded = applyStarMacro(result);
5152
assertEquals(
52-
MultListToken.of(2, 3, 4),
53+
ofMult(2, 3, 4),
5354
expanded);
5455
}
5556

5657
@Test
5758
void starMacro2() {
5859
ListToken result = parse("1 + 2 * 3 + 4");
5960
Token expanded = applyStarMacro(result);
60-
assertEquals(PlusListToken.of(
61-
VarExp.constant(1), MultListToken.of(2, 3), VarExp.constant(4)),
61+
assertEquals(ofPlus(
62+
constant(1), ofMult(2, 3), constant(4)),
6263
expanded);
6364
}
6465

@@ -67,8 +68,8 @@ void starMacro3() {
6768
ListToken result = parse("(1 + 2) * 3");
6869
Token expanded = applyStarMacro(result);
6970
assertEquals(
70-
MultListToken.of(
71-
PlusListToken.of(VarExp.constant(1), VarExp.constant(2)), VarExp.constant(3)),
71+
ofMult(
72+
ofPlus(constant(1), constant(2)), constant(3)),
7273
expanded);
7374
}
7475

@@ -77,8 +78,8 @@ void starMacro7() {
7778
ListToken result = parse("1 * 2");
7879
Token expanded = applyStarMacro(result);
7980
assertEquals(
80-
MultListToken.of(
81-
VarExp.constant(1), VarExp.constant(2)),
81+
ofMult(
82+
constant(1), constant(2)),
8283
expanded);
8384
}
8485

@@ -87,10 +88,10 @@ void starMacro8() {
8788
ListToken result = parse("-(x - 1)");
8889
Token expanded = applyStarMacro(result);
8990
assertEquals(
90-
MultListToken.of(
91-
VarExp.constant(-1),
92-
PlusListToken.of(VarExp.of(1),
93-
MultListToken.of(-1, 1))),
91+
ofMult(
92+
constant(-1),
93+
ofPlus(VarExp.of(1),
94+
ofMult(-1, 1))),
9495
expanded);
9596
}
9697

@@ -99,9 +100,9 @@ void starMacro4() {
99100
ListToken result = parse("1 * (2 + 3)");
100101
Token expanded = applyStarMacro(result);
101102
assertEquals(
102-
MultListToken.of(
103-
VarExp.constant(1),
104-
PlusListToken.of(VarExp.constant(2), VarExp.constant(3))),
103+
ofMult(
104+
constant(1),
105+
ofPlus(constant(2), constant(3))),
105106
expanded);
106107
Polynomial polynomial = eval(result);
107108
assertEquals(Monomial.constant(5).polynomial(), polynomial);

0 commit comments

Comments
 (0)