-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAST.java
241 lines (229 loc) · 4.39 KB
/
AST.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import java.util.HashMap;
import java.util.Map.Entry;
/*
* State Class
*/
class State {
HashMap<String,Integer> variableValues = new HashMap<String,Integer>();
public State() {}
public void setVariable(String name, int value) {
variableValues.put(name, value);
}
public int getVariable(String name) throws NotDefinedException {
Integer value = variableValues.get(name);
if (value == null) throw new NotDefinedException(name);
return value.intValue();
}
public String toString() {
String table = "";
for (Entry<String,Integer> entry : variableValues.entrySet()) {
table += entry.getKey() + " " + entry.getValue() + "\n";
}
return table;
}
}
/*
* Exception handling class
*/
class NotDefinedException extends Exception {
static final long serialVersionUID = 1L;
String variableName;
NotDefinedException(String name) {
this.variableName = name;
}
public String toString() {
return "The variable " + variableName + " is not defined.";
}
}
/*
* Arithmetic Class for all the Arithmetic Operations
*/
abstract class ArithExp{
abstract int eval(State S) throws NotDefinedException;
}
abstract class BoolExp{
abstract boolean eval(State S) throws NotDefinedException;
}
abstract class ComExp{
abstract State eval(State S) throws NotDefinedException;
}
/*
* Number type
*/
class Number extends ArithExp{
int n;
Number(int n){
this.n=n;
}
int eval(State S) throws NotDefinedException{
return n;
}
}
class Bool extends BoolExp{
boolean b;
Bool(boolean b){
this.b=b;
}
boolean eval(State S) throws NotDefinedException{
return b;
}
}
/*
* Arithmetic Operations
*/
class variable extends ArithExp{
String x;
variable(String x){
this.x=x;
}
public int eval(State s) throws NotDefinedException{
s.setVariable(x, 0);
return 0;
}
}
class ArithMul extends ArithExp{
ArithExp e1,e2;
ArithMul(ArithExp e1,ArithExp e2){
this.e1=e1;
this.e2=e2;
}
public int eval(State s) throws NotDefinedException{
return e1.eval(s)*e2.eval(s);
}
}
class ArithAdd extends ArithExp{
ArithExp e1,e2;
ArithAdd(ArithExp e1,ArithExp e2){
this.e1=e1;
this.e2=e2;
}
int eval(State s) throws NotDefinedException{
return e1.eval(s)+e2.eval(s);
}
}
class ArithSub extends ArithExp{
ArithExp e1,e2;
ArithSub(ArithExp e1,ArithExp e2){
this.e1=e1;
this.e2=e2;
}
int eval(State s) throws NotDefinedException{
return e1.eval(s)-e2.eval(s);
}
}
/*
* Boolean Operations
*/
class Negation extends BoolExp{
BoolExp b;
Negation(BoolExp b){
this.b=b;
}
boolean eval(State s) throws NotDefinedException{
return !b.eval(s);
}
}
class Equal extends BoolExp{
ArithExp e1,e2;
Equal(ArithExp e1,ArithExp e2){
this.e1=e1;
this.e2=e2;
}
boolean eval(State s) throws NotDefinedException{
return (e1.eval(s)==e2.eval(s));
}
}
class LessThan extends BoolExp{
ArithExp e1,e2;
LessThan(ArithExp e1,ArithExp e2){
this.e1=e1;
this.e2=e2;
}
boolean eval(State s) throws NotDefinedException{
return e1.eval(s)<e2.eval(s);
}
}
class And extends BoolExp{
BoolExp b1,b2;
And(BoolExp b1,BoolExp b2){
this.b1=b1;
this.b2=b2;
}
boolean eval(State s) throws NotDefinedException{
return b1.eval(s)&&b2.eval(s);
}
}
class Or extends BoolExp{
BoolExp b1,b2;
Or(BoolExp b1,BoolExp b2){
this.b1=b1;
this.b2=b2;
}
boolean eval(State s) throws NotDefinedException{
return b1.eval(s)||b2.eval(s);
}
}
/*
* Commands Operations
*/
class skip extends ComExp{
State eval(State s) throws NotDefinedException{
return s;
}
}
class Assign extends ComExp{
String x;
ArithExp e;
Assign(String x,ArithExp e){
this.x=x;
this.e=e;
}
State eval(State s) throws NotDefinedException{
int value=e.eval(s);
State s1=new State();
s1.setVariable(x, value);
return s1;
}
}
class Sequential extends ComExp{
ComExp c1,c2;
Sequential(ComExp c1,ComExp c2){
this.c1=c1;
this.c2=c2;
}
State eval(State s) throws NotDefinedException{
State s1= c1.eval(s);
return c2.eval(s1);
}
}
class If extends ComExp{
BoolExp b;
ComExp c1,c2;
If(BoolExp b,ComExp c1,ComExp c2){
this.b=b;
this.c1=c1;
this.c2=c2;
}
public State eval(State s) throws NotDefinedException{
if(b.eval(s)) return c1.eval(s);
else return c2.eval(s);
}
}
class While extends ComExp{
BoolExp b;
ComExp c;
While(BoolExp b,ComExp c){
this.b=b;
this.c=c;
}
State eval(State s) throws NotDefinedException{
if(b.eval(s)){
do {
return c.eval(s);
} while (b.eval(s));
}
else {
return s;
}
}
}