Skip to content

Commit f176d3d

Browse files
committed
cleanup hierarchy: remove NumberToken
1 parent 1152d3e commit f176d3d

File tree

3 files changed

+37
-61
lines changed

3 files changed

+37
-61
lines changed

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

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

3-
import io.polypen.parse.Parser.Token;
43
import io.polypen.parse.Parser.ListToken;
54
import io.polypen.parse.Parser.MinusToken;
6-
import io.polypen.parse.Parser.MultToken;
75
import io.polypen.parse.Parser.MultListToken;
8-
import io.polypen.parse.Parser.NumberToken;
9-
import io.polypen.parse.Parser.PlusToken;
6+
import io.polypen.parse.Parser.MultToken;
107
import io.polypen.parse.Parser.PlusListToken;
8+
import io.polypen.parse.Parser.PlusToken;
9+
import io.polypen.parse.Parser.Token;
10+
import io.polypen.parse.Parser.VarExp;
1111

1212
import java.util.List;
1313

@@ -42,7 +42,7 @@ public static Token applyStarMacro(List<Token> tokens) {
4242
int b = bound[i];
4343
if ((b & B_STRONG) != 0) {
4444
if ((b & B_MINUSBOUND) != 0) {
45-
region.add(MultListToken.of(NumberToken.of(-1), expandRecursively(token)));
45+
region.add(MultListToken.of(VarExp.constant(-1), expandRecursively(token)));
4646
} else {
4747
region.add(expandRecursively(token));
4848
}

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

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,13 @@ private static Token readWord(PushbackReader reader) throws IOException {
5656
}
5757
reader.unread(c);
5858
if (Character.isDigit(c)) {
59-
return readNumber(reader);
59+
int value = readNumber(reader);
60+
return VarExp.constant(value);
6061
}
6162
return readVarExp(reader);
6263
}
6364

64-
private static NumberToken readNumber(PushbackReader reader) throws IOException {
65+
private static int readNumber(PushbackReader reader) throws IOException {
6566
StringBuilder sb = new StringBuilder();
6667
int c;
6768
while (Character.isDigit(c = reader.read())) {
@@ -70,28 +71,24 @@ private static NumberToken readNumber(PushbackReader reader) throws IOException
7071
if (c != -1) {
7172
reader.unread(c);
7273
}
73-
return new NumberToken(Integer.parseInt(sb.toString()));
74+
return Integer.parseInt(sb.toString());
7475
}
7576

7677
private static VarExp readVarExp(PushbackReader reader) throws IOException {
77-
StringBuilder name = new StringBuilder();
7878
int c;
7979
while (true) {
8080
c = reader.read();
81-
if (Character.isAlphabetic(c) || Character.isDigit(c) || c == '_') {
82-
name.append((char) c);
83-
} else {
81+
if (!Character.isAlphabetic(c) && !Character.isDigit(c) && c != '_') {
8482
break;
8583
}
8684
}
8785
if (c == '^') {
88-
NumberToken expr = readNumber(reader);
89-
return new VarExp(name.toString(), expr.value);
86+
return VarExp.of(readNumber(reader));
9087
}
9188
if (c != -1) {
9289
reader.unread(c);
9390
}
94-
return new VarExp(name.toString(), 1);
91+
return VarExp.of(1);
9592
}
9693

9794
private static void consumeWhitespace(PushbackReader reader) throws IOException {
@@ -114,7 +111,7 @@ public static ListToken parse(String s) {
114111
}
115112
}
116113

117-
public sealed interface Token permits PlusToken, MinusToken, MultToken, ListToken, NumberToken, VarExp, PlusListToken, MultListToken {
114+
public sealed interface Token permits PlusToken, MinusToken, MultToken, ListToken, VarExp, PlusListToken, MultListToken {
118115
int size();
119116

120117
Token getFirst();
@@ -194,7 +191,7 @@ public List<Token> getExprs() {
194191

195192
public static final Token MULT = new MultToken();
196193

197-
public static Polynomial eval(Token token) {
194+
public static Polynomial eval(ListToken token) {
198195
Token exprs = Macro.applyStarMacro(token.getExprs());
199196
return _eval(exprs);
200197
}
@@ -239,8 +236,7 @@ private static Polynomial _eval(Token exprs) {
239236
}
240237
yield result;
241238
}
242-
case NumberToken numberExpr -> new Monomial(numberExpr.value, 0).polynomial();
243-
case VarExp varExp -> new Monomial(1, varExp.exp).polynomial();
239+
case VarExp varExp -> new Monomial(varExp.factor, varExp.exp).polynomial();
244240
default -> throw new IllegalStateException(exprs.toString());
245241
};
246242
}
@@ -277,7 +273,7 @@ public String toString() {
277273
}
278274

279275
public static MultListToken of(int... value) {
280-
List<Token> list = IntStream.of(value).mapToObj(NumberToken::of).map(s -> (Token) s).toList();
276+
List<Token> list = IntStream.of(value).mapToObj(value1 -> VarExp.constant(value1)).map(s -> (Token) s).toList();
281277
return new MultListToken(list);
282278
}
283279

@@ -378,43 +374,24 @@ public List<Token> getExprs() {
378374
}
379375
}
380376

381-
public record NumberToken(int value) implements Token {
382-
public static NumberToken of(int value) {
383-
return new NumberToken(value);
377+
public record VarExp(int factor, int exp) implements Token {
378+
public static VarExp constant(int factor) {
379+
return new VarExp(factor, 0);
384380
}
385381

386-
@Override
387-
public String toString() {
388-
return Integer.toString(value);
389-
}
390-
391-
@Override
392-
public int size() {
393-
return 1;
394-
}
395-
396-
@Override
397-
public Token getFirst() {
398-
return this;
399-
}
400-
401-
@Override
402-
public List<Token> getExprs() {
403-
return List.of(this);
404-
}
405-
}
406-
407-
public record VarExp(String var, int exp) implements Token {
408-
public static VarExp of(String var, int exp) {
409-
return new VarExp(var, exp);
382+
public static VarExp of(int exp) {
383+
return new VarExp(1, exp);
410384
}
411385

412386
@Override
413387
public String toString() {
388+
if (exp == 0) {
389+
return Integer.toString(factor);
390+
}
414391
if (exp == 1) {
415-
return var;
392+
return "x";
416393
}
417-
return var + "^" + exp;
394+
return "x^" + exp;
418395
}
419396

420397
@Override

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

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

77
import static io.polypen.parse.Macro.applyStarMacro;
8-
import static io.polypen.parse.Parser.Token;
98
import static io.polypen.parse.Parser.ListToken;
109
import static io.polypen.parse.Parser.MULT;
1110
import static io.polypen.parse.Parser.MultListToken;
12-
import static io.polypen.parse.Parser.NumberToken;
1311
import static io.polypen.parse.Parser.PLUS;
1412
import static io.polypen.parse.Parser.PlusListToken;
13+
import static io.polypen.parse.Parser.Token;
1514
import static io.polypen.parse.Parser.VarExp;
1615
import static io.polypen.parse.Parser.eval;
1716
import static io.polypen.parse.Parser.parse;
@@ -23,7 +22,7 @@ class ParserTest {
2322
void testParse() {
2423
ListToken result = parse("(a_1^12 + b12^2) * 2");
2524
assertEquals(ListToken.of(
26-
ListToken.of(VarExp.of("a_1", 12), PLUS, VarExp.of("b12", 2)), MULT, NumberToken.of(2)),
25+
ListToken.of(VarExp.of(12), PLUS, VarExp.of(2)), MULT, VarExp.constant(2)),
2726
result);
2827
}
2928

@@ -32,7 +31,7 @@ void starMacro1() {
3231
ListToken result = parse("1 + 2 * 3");
3332
Token expanded = applyStarMacro(result.value());
3433
assertEquals(PlusListToken.of(
35-
NumberToken.of(1), MultListToken.of(NumberToken.of(2), NumberToken.of(3))),
34+
VarExp.constant(1), MultListToken.of(VarExp.constant(2), VarExp.constant(3))),
3635
expanded);
3736
}
3837

@@ -41,7 +40,7 @@ void starMacro5() {
4140
ListToken result = parse("1 + 2 * 3 * 4");
4241
Token expanded = applyStarMacro(result.value());
4342
assertEquals(PlusListToken.of(
44-
NumberToken.of(1), MultListToken.of(NumberToken.of(2), NumberToken.of(3), NumberToken.of(4))),
43+
VarExp.constant(1), MultListToken.of(VarExp.constant(2), VarExp.constant(3), VarExp.constant(4))),
4544
expanded);
4645
}
4746

@@ -59,7 +58,7 @@ void starMacro2() {
5958
ListToken result = parse("1 + 2 * 3 + 4");
6059
Token expanded = applyStarMacro(result.value());
6160
assertEquals(PlusListToken.of(
62-
NumberToken.of(1), MultListToken.of(2, 3), NumberToken.of(4)),
61+
VarExp.constant(1), MultListToken.of(2, 3), VarExp.constant(4)),
6362
expanded);
6463
}
6564

@@ -69,7 +68,7 @@ void starMacro3() {
6968
Token expanded = applyStarMacro(result.value());
7069
assertEquals(
7170
MultListToken.of(
72-
PlusListToken.of(NumberToken.of(1), NumberToken.of(2)), NumberToken.of(3)),
71+
PlusListToken.of(VarExp.constant(1), VarExp.constant(2)), VarExp.constant(3)),
7372
expanded);
7473
}
7574

@@ -79,7 +78,7 @@ void starMacro7() {
7978
Token expanded = applyStarMacro(result.value());
8079
assertEquals(
8180
MultListToken.of(
82-
NumberToken.of(1), NumberToken.of(2)),
81+
VarExp.constant(1), VarExp.constant(2)),
8382
expanded);
8483
}
8584

@@ -89,8 +88,8 @@ void starMacro8() {
8988
Token expanded = applyStarMacro(result.getExprs());
9089
assertEquals(
9190
MultListToken.of(
92-
NumberToken.of(-1),
93-
PlusListToken.of(VarExp.of("x", 1),
91+
VarExp.constant(-1),
92+
PlusListToken.of(VarExp.of(1),
9493
MultListToken.of(-1, 1))),
9594
expanded);
9695
}
@@ -101,8 +100,8 @@ void starMacro4() {
101100
Token expanded = applyStarMacro(result.value());
102101
assertEquals(
103102
MultListToken.of(
104-
NumberToken.of(1),
105-
PlusListToken.of(NumberToken.of(2), NumberToken.of(3))),
103+
VarExp.constant(1),
104+
PlusListToken.of(VarExp.constant(2), VarExp.constant(3))),
106105
expanded);
107106
Polynomial polynomial = eval(result);
108107
assertEquals(Monomial.constant(5).polynomial(), polynomial);

0 commit comments

Comments
 (0)