Skip to content

Commit 9744e8a

Browse files
committed
read varexp
1 parent 84f92f4 commit 9744e8a

File tree

2 files changed

+76
-12
lines changed

2 files changed

+76
-12
lines changed

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

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,67 @@ private static ListExpr parse(PushbackReader reader) throws IOException {
2525
}
2626
default -> {
2727
reader.unread(c);
28-
Expr atom = readAtom(reader);
29-
result.add(atom);
28+
Expr word = readWord(reader);
29+
result.add(word);
3030
}
3131
}
32+
consumeWhitespace(reader);
3233
}
3334
return new ListExpr(result);
3435
}
3536

36-
private static Expr readAtom(PushbackReader reader) throws IOException {
37-
consumeWhitespace(reader);
37+
private static Expr readWord(PushbackReader reader) throws IOException {
38+
int c = reader.read();
39+
if (c == -1) {
40+
return null;
41+
}
42+
if (c == '*') {
43+
return MULT;
44+
}
45+
if (c == '+') {
46+
return PLUS;
47+
}
48+
if (c == '-') {
49+
return MINUS;
50+
}
51+
reader.unread(c);
52+
if (Character.isDigit(c)) {
53+
return readNumber(reader);
54+
}
55+
return readVarExp(reader);
56+
}
57+
58+
private static NumberExpr readNumber(PushbackReader reader) throws IOException {
3859
StringBuilder sb = new StringBuilder();
3960
int c;
40-
while ((c = reader.read()) != -1) {
41-
if (c != ' ' && c != '(' && c != ')') {
42-
sb.append((char) c);
61+
while (Character.isDigit(c = reader.read())) {
62+
sb.append((char) c);
63+
}
64+
if (c != -1) {
65+
reader.unread(c);
66+
}
67+
return new NumberExpr(Integer.parseInt(sb.toString()));
68+
}
69+
70+
private static VarExp readVarExp(PushbackReader reader) throws IOException {
71+
StringBuilder name = new StringBuilder();
72+
int c;
73+
while (true) {
74+
c = reader.read();
75+
if (Character.isAlphabetic(c) || Character.isDigit(c) || c == '_') {
76+
name.append((char) c);
4377
} else {
44-
reader.unread(c);
4578
break;
4679
}
4780
}
48-
return new Atom(sb.toString());
81+
if (c == '^') {
82+
NumberExpr expr = readNumber(reader);
83+
return new VarExp(name.toString(), expr.value);
84+
}
85+
if (c != -1) {
86+
reader.unread(c);
87+
}
88+
return new VarExp(name.toString(), 1);
4989
}
5090

5191
private static void consumeWhitespace(PushbackReader reader) throws IOException {
@@ -71,10 +111,34 @@ public static ListExpr parse(String s) {
71111
public interface Expr {
72112
}
73113

74-
public record Atom(String value) implements Expr {
114+
public static final Expr PLUS = new Expr() {
115+
@Override
116+
public String toString() {
117+
return "+";
118+
}
119+
};
120+
121+
public static final Expr MINUS = new Expr() {
122+
@Override
123+
public String toString() {
124+
return "-";
125+
}
126+
};
127+
128+
public static final Expr MULT = new Expr() {
129+
@Override
130+
public String toString() {
131+
return "*";
132+
}
133+
};
134+
135+
public record ListExpr(List<Expr> value) implements Expr {
136+
}
137+
138+
public record NumberExpr(int value) implements Expr {
75139
}
76140

77-
public record ListExpr(List<Expr> values) implements Expr {
141+
public record VarExp(String var, int exp) implements Expr {
78142
}
79143

80144
private Parser() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class ParserTest {
66

77
@Test
88
void parse() {
9-
Parser.ListExpr result = Parser.parse("(a b) 2 3");
9+
Parser.ListExpr result = Parser.parse("(a_1^12 + b12^2) * 2");
1010
System.out.println(result);
1111
}
1212
}

0 commit comments

Comments
 (0)