-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInterpreter.java
More file actions
227 lines (181 loc) · 5.44 KB
/
Interpreter.java
File metadata and controls
227 lines (181 loc) · 5.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
interface Element {
int evaluate();
}
class BadVariableException extends RuntimeException {
}
class IntegerElement implements Element {
private int value;
IntegerElement(LexToken t) {
this.value = Integer.parseInt(t.text);
}
public int evaluate() {
return value;
}
}
abstract class BinaryOperationElement implements Element {
Element lhs;
Element rhs;
BinaryOperationElement(Element lhs) {
this.lhs = lhs;
}
}
class AdditionElement extends BinaryOperationElement {
AdditionElement(Element lhs) {
super(lhs);
}
public int evaluate() {
return lhs.evaluate() + rhs.evaluate();
}
}
class SubtractionElement extends BinaryOperationElement {
SubtractionElement(Element lhs) {
super(lhs);
}
public int evaluate() {
return lhs.evaluate() - rhs.evaluate();
}
}
class VariableElement implements Element {
private String name;
private ExpressionProcessor p;
VariableElement(LexToken t, ExpressionProcessor p) {
this.name = t.text;
this.p = p;
}
public int evaluate() {
if (name.length() > 1) {
throw new BadVariableException();
}
return p.variables.get(name.charAt(0));
}
}
class LexToken {
enum Type {
NUMBER, PLUS, MINUS, VARIABLE, DONE
}
Type type;
String text;
LexToken(Type type, char c) {
this.type = type;
this.text = Character.toString(c);
}
void append(char c) {
text = text + c;
}
@Override
public String toString() {
return String.format("{%s, %s}", type, text);
}
}
class Lexer {
LexToken current = null;
Stream<LexToken> tokenize(Character c) {
if (c == '+') {
return addToken(LexToken.Type.PLUS, c);
}
if (c == '-') {
return addToken(LexToken.Type.MINUS, c);
}
if (Character.isLetter(c)) {
return addToken(LexToken.Type.VARIABLE, c);
}
if (Character.isDigit(c)) {
return addToken(LexToken.Type.NUMBER, c);
}
if (c == '\n') {
return Stream.of(current, new LexToken(LexToken.Type.DONE, c));
}
throw new RuntimeException("Illegal expression character " + c);
}
Stream<LexToken> addToken(LexToken.Type type, char c) {
if (current != null && current.type == type) {
current.append(c);
return Stream.empty();
}
LexToken lastToken = current;
current = new LexToken(type, c);
return lastToken == null ? Stream.empty() : Stream.of(lastToken);
}
Optional<LexToken> flush() {
return Optional.empty();
}
}
class Parser {
Element current = null;
private ExpressionProcessor p;
Parser(ExpressionProcessor p) {
this.p = p;
}
void parse(LexToken token) {
switch (token.type) {
case NUMBER:
Element t = new IntegerElement(token);
if (current == null) {
current = t;
} else if (current instanceof BinaryOperationElement) {
((BinaryOperationElement) current).rhs = t;
} else {
throw new RuntimeException();
}
break;
case VARIABLE:
Element vt = new VariableElement(token, p);
if (current == null) {
current = vt;
} else if (current instanceof BinaryOperationElement) {
((BinaryOperationElement) current).rhs = vt;
} else {
throw new RuntimeException();
}
break;
case PLUS:
current = new AdditionElement(current);
break;
case MINUS:
current = new SubtractionElement(current);
break;
case DONE:
break;
default:
throw new RuntimeException();
}
}
}
class ExpressionProcessor {
public Map<Character, Integer> variables = new HashMap<>();
public int calculate(String expression) {
Lexer lexer = new Lexer();
Parser parser = new Parser(this);
int ret;
try {
ret = evaluate(expression + "\n", lexer, parser);
} catch(BadVariableException e) {
ret = 0;
}
System.out.println("Evaluate " + expression + " = " + ret);
return ret;
}
private int evaluate(String expression, Lexer lexer, Parser parser) {
// @formatter:off
expression
.chars()
.mapToObj(c -> (char) c)
.flatMap(lexer::tokenize)
.forEach(t -> parser.parse(t));
// @formatter:on
return parser.current.evaluate();
}
}
class DemoInterpreter {
public static void main(String[] args) {
ExpressionProcessor p = new ExpressionProcessor();
p.variables.put('x', 3);
p.calculate("1+2+3");
p.calculate("1+2+xy");
p.calculate("10-2-x");
}
}