-
Notifications
You must be signed in to change notification settings - Fork 2
/
Interpreter.java
183 lines (161 loc) · 4.82 KB
/
Interpreter.java
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
// Interpreter pattern
package behavioral.interpreter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
interface Element
{
int eval();
}
class Integer implements Element
{
private int value;
public Integer(int value) {
this.value = value;
}
@Override
public int eval() {
return value;
}
}
// Will use binary tree approach
class BinaryOperation implements Element
{
public enum Type
{
ADDITION,
SUBTRACTION
}
public Type type;
public Element left, right; // Binary tree approach
@Override
public int eval() {
switch (type) {
case ADDITION:
return left.eval() + right.eval();
case SUBTRACTION:
return left.eval() - right.eval();
default:
return 0;
}
}
}
class Token
{
public enum Type
{
INTEGER,
PLUS,
MINUS,
LPAREN,
RPAREN
}
public Type type; // the type of token we are evaluating
public String text; // the textual value of the token
public Token(Type type, String text) {
this.type = type;
this.text = text;
}
@Override
public String toString() {
return "`" + text + "`";
}
}
class InterpreterDemo
{
// Lexing operation
static List<Token> lex(String input)
{
ArrayList<Token> result = new ArrayList<>();
for (int i = 0; i < input.length(); ++i)
{
switch (input.charAt(i))
{
case '+':
result.add(new Token(Token.Type.PLUS, "+"));
break;
case '-':
result.add(new Token(Token.Type.MINUS, "-"));
break;
case '(':
result.add(new Token(Token.Type.LPAREN, "("));
break;
case ')':
result.add(new Token(Token.Type.RPAREN, ")"));
break;
default:
StringBuilder sb = new StringBuilder("" + input.charAt(i));
for (int j = i+1; j < input.length(); ++j)
{
if (Character.isDigit(input.charAt(j)))
{
sb.append(input.charAt(j));
i++;
}
else {
result.add(new Token(
Token.Type.INTEGER, sb.toString()
));
break;
}
}
break;
}
}
return result;
}
// Parsing operation
static Element parse(List<Token> tokens)
{
BinaryOperation result = new BinaryOperation();
boolean haveLHS = false; // 'have left hand side'
for (int i = 0; i < tokens.size(); ++i)
{
Token token = tokens.get(i);
switch (token.type) {
case INTEGER:
Integer integer = new Integer(java.lang.Integer.parseInt(token.text));
if(!haveLHS)
{
result.left = integer;
haveLHS = true;
} else result.right = integer;
break;
case PLUS:
result.type = BinaryOperation.Type.ADDITION;
break;
case MINUS:
result.type = BinaryOperation.Type.SUBTRACTION;
break;
case LPAREN:
int j = i; // points to location of rparen
for (; j < tokens.size(); ++j)
if(tokens.get(j).type == Token.Type.RPAREN)
break;
List<Token> subexpression = tokens.stream()
.skip(i + 1)
.limit(j - i - 1)
.collect(Collectors.toList());
Element element = parse(subexpression); // recursively parse elements
if (!haveLHS)
{
result.left = element;
haveLHS = true;
} else result.right = element;
i = j;
break;
}
}
return result;
}
public static void main(String[] args) {
String input = "(13+4)-(12+1)";
List<Token> tokens = lex(input);
// Testing out lexing
System.out.println(tokens.stream()
.map(t -> t.toString())
.collect(Collectors.joining("\t")));
Element parsed = parse(tokens);
System.out.println(input + " = " + parsed.eval());
}
}