-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionParser.java
More file actions
125 lines (106 loc) · 3.44 KB
/
ExpressionParser.java
File metadata and controls
125 lines (106 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package expression.parser;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import expression.*;
public class ExpressionParser extends BaseParser implements Parser {
private static Set<Character> VARIABLE_NAMES = Set.of('x', 'y', 'z');
private final Supplier<BothExpressions> minMax = () -> minMax();
private final Supplier<BothExpressions> addSubtract = () -> addSubtract();
private final Supplier<BothExpressions> multiplyDivide = () -> multiplyDivide();
private final Supplier<BothExpressions> varConst = () -> varConst();
private final Map<Supplier<BothExpressions>, Supplier<BothExpressions>> nextFunc = Map.of(
minMax, addSubtract,
addSubtract, multiplyDivide,
multiplyDivide, varConst,
varConst, minMax);
public BothExpressions parse(final String s) {
setSource(s);
nextChar();
return minMax();
}
private BothExpressions minMax() {
Supplier<BothExpressions> lower = nextFunc.get(minMax);
BothExpressions first = lower.get();
while (!eof()) {
if (test("m")) {
if (test("in")) {
first = new Min(first, lower.get());
} else if (test("ax")) {
first = new Max(first, lower.get());
}
} else {
break;
}
}
return first;
}
private BothExpressions addSubtract() {
Supplier<BothExpressions> lower = nextFunc.get(addSubtract);
BothExpressions first = lower.get();
while (!eof()) {
if (test('+')) {
first = new Add(first, lower.get());
} else if (test('-')) {
first = new Subtract(first, lower.get());
} else {
break;
}
}
return first;
}
private BothExpressions multiplyDivide() {
Supplier<BothExpressions> lower = nextFunc.get(multiplyDivide);
BothExpressions first = lower.get();
while (!eof()) {
if (test('*')) {
first = new Multiply(first, lower.get());
} else if (test('/')) {
first = new Divide(first, lower.get());
} else {
break;
}
}
return first;
}
private BothExpressions varConst() {
eof();
if (isDigit()) {
return parseConst(1);
}
if (test('(')) {
BothExpressions result = nextFunc.get(varConst).get();
test(')');
return result;
}
if (test('-')) {
return parseMinus();
}
if (isLetter()) {
return parseVariable();
}
return new Const(0);
}
private BothExpressions parseMinus() {
if (isDigit()) {
return parseConst(-1);
}
return new Minus(varConst());
}
private BothExpressions parseConst(int sign) {
int num = 0;
while (!eof() && isDigit()) {
num = num * 10 + sign * (int) (getChar() - '0');
nextChar();
}
return new Const(num);
}
private BothExpressions parseVariable() {
StringBuilder sb = new StringBuilder();
while (!eof() && isLetter() && VARIABLE_NAMES.contains(getChar())) {
sb.append(getChar());
nextChar();
}
return new Variable(sb.toString());
}
}