-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interpreter.java
479 lines (407 loc) · 13.8 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
package dev.alephpt.Dis;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
class Interpreter implements Express.Visitor<Object>, Statement.Visitor<Void> {
final Field globals = new Field();
private Field fields = globals;
private final Map<Express, Integer> relatives = new HashMap<>();
Interpreter() {
globals.define("clock", new DisCaller() {
@Override
public int arity() { return 0; }
@Override
public Object call(Interpreter interpret,
List<Object> args) {
return (double)System.currentTimeMillis();
}
@Override
public String toString() { return "<Native Operation>"; }
});
}
void interpret(List<Statement> statements) {
try {
for (Statement statement : statements) {
execute(statement);
}
} catch (RuntimeError error) {
DisC.runtimeError(error);
} catch (RuntimeArgsError error) {
DisC.runtimeArgsError(error);
}
}
@Override
public Object visitLiteralExpress(Express.Literal express) {
return express.value;
}
@Override
public Object visitLogicalExpress(Express.Logical express) {
Object left = evaluate(express.left);
if (express.operator.type == TokenType.OR_OP) {
if (isTruthful(left)) {
return left;
}
} else {
if (!isTruthful(left)) {
return left;
}
}
return evaluate(express.right);
}
@Override
public Object visitGroupingExpress(Express.Grouping express) {
return evaluate(express.expression);
}
@Override
public Object visitCallingExpress(Express.Calling express) {
Object called = evaluate(express.called);
List<Object> args = new ArrayList<>();
for (Express argument : express.args) {
args.add(evaluate(argument));
}
if (!(called instanceof DisCaller)) {
throw new RuntimeArgsError(args, "Only Classes and Functions are Callable.");
}
DisCaller operation = (DisCaller)called;
if(args.size() != operation.arity()) {
throw new RuntimeArgsError(args, "Expected " + operation.arity() + " arguments.");
}
return operation.call(this, args);
}
@Override
public Object visitUnaryExpress(Express.Unary express) {
Object right = evaluate(express.right);
switch(express.operator.type) {
case NOT:
return !isTruthful(right);
case MINUS:
checkNumberOperand(express.operator, right);
if(right instanceof Double) { return -((Number)right).doubleValue(); }
if(right instanceof Integer) { return -(Integer)right; }
}
// unreachable
return null;
}
@Override
public Object visitCountExpress(Express.Count express) {
Object identity = evaluate(express.identifier);
Number value = 0;
switch(express.operator.type){
case PLUSPLUS:
if(identity instanceof Integer) {
value = (Integer)identity + 1;
break;
} else
if (identity instanceof Double) {
value = (Double)identity + 1.0;
break;
} else {
throw new RuntimeError(express.operator, "must only be used with Numbers.");
}
case MINUSMINUS:
if(identity instanceof Integer) {
value = (Integer)identity - 1;
break;
} else
if (identity instanceof Double) {
value = (Double)identity - 1.0;
break;
} else {
throw new RuntimeError(express.operator, "must only be used with Numbers.");
}
}
fields.assign(express.name, value);
return value;
}
@Override
public Object visitBinaryExpress(Express.Binary express) {
Object left = evaluate(express.left);
Object right = evaluate(express.right);
switch(express.operator.type){
case INEQ:
return !isEqual(left, right);
case EQEQ:
return isEqual(left, right);
case GREATER:
checkNumberOperands(express.operator, left, right);
if(left instanceof Integer && right instanceof Integer) {
return (Integer)left > (Integer)right;
}
return ((Number)left).doubleValue() > ((Number)right).doubleValue();
case GREAT_EQ:
checkNumberOperands(express.operator, left, right);
if(left instanceof Integer && right instanceof Integer) {
return (Integer)left >= (Integer)right;
}
return ((Number)left).doubleValue() >= ((Number)right).doubleValue();
case LESSER:
checkNumberOperands(express.operator, left, right);
if(left instanceof Integer && right instanceof Integer) {
return (Integer)left < (Integer)right;
}
return ((Number)left).doubleValue() < ((Number)right).doubleValue();
case LESS_EQ:
checkNumberOperands(express.operator, left, right);
if(left instanceof Integer && right instanceof Integer) {
return (Integer)left <= (Integer)right;
}
return ((Number)left).doubleValue() <= ((Number)right).doubleValue();
case MINUS:
checkNumberOperands(express.operator, left, right);
if(left instanceof Integer && right instanceof Integer) {
return (Integer)left - (Integer)right;
}
return ((Number)left).doubleValue() - ((Number)right).doubleValue();
case PLUS:
if (left instanceof Integer && right instanceof Integer) {
return (Integer)left + (Integer)right;
}
if (left instanceof String || right instanceof String) {
return asString(left) + asString(right);
}
if (left instanceof Double || right instanceof Double ||
left instanceof Integer || right instanceof Integer) {
return ((Number)left).doubleValue() + ((Number)right).doubleValue();
}
throw new RuntimeError(express.operator, "Operands must be of Numbers or Strings.");
case WHACK:
checkNumberOperands(express.operator, left, right);
if(left instanceof Integer && right instanceof Integer) {
return (Integer)left / (Integer)right;
}
return ((Number)left).doubleValue() / ((Number)right).doubleValue();
case STAR:
checkNumberOperands(express.operator, left, right);
if(left instanceof Integer && right instanceof Integer) {
return (Integer)left * (Integer)right;
}
return ((Number)left).doubleValue() * ((Number)right).doubleValue();
}
// unreachable
return null;
}
@Override
public Object visitVariableExpress(Express.Variable express) {
return findVar(express.name, express);
}
@Override
public Object visitParentVariableExpress(Express.ParentVariable express) {
return fields.parentGet(express.name);
}
@Override
public Object visitGlobalVariableExpress(Express.GlobalVariable express) {
return fields.globalGet(express.name);
// globals.get(express.name);
}
@Override
public Object visitAssignExpress(Express.Assign express) {
Object value = evaluate(express.value);
Integer distance = relatives.get(express);
if (distance != null) {
fields.assignAt(distance, express.name, value);
} else {
globals.assign(express.name, value);
}
return value;
}
@Override
public Object visitGetPropsExpress(Express.GetProps props) {
Object object = evaluate(props.object);
if (object instanceof DisInstance) {
return ((DisInstance)object).get(props.name);
}
if (object instanceof DisSample) {
return ((DisSample)object).get(props.name);
}
if (object instanceof DisTaste) {
return ((DisTaste)object).get(props.name);
}
throw new RuntimeError(props.name, "Property '" + props.name + "' unavailable. Make sure you are using a proper instance.");
}
@Override
public Object visitSetPropsExpress(Express.SetProps props) {
Object object = evaluate(props.object);
if (!(object instanceof DisInstance) &&
!(object instanceof DisSample) &&
!(object instanceof DisTaste)) {
throw new RuntimeError(props.name, "Only instances of Object, Enumeration, or Forms contain indexable fields");
}
Object value = evaluate(props.value);
if (object instanceof DisInstance) {
((DisInstance)object).set(props.name, value);
}
if (object instanceof DisSample) {
((DisSample)object).set(props.name, value);
}
if (object instanceof DisTaste) {
((DisTaste)object).set(props.name, value);
}
return value;
}
@Override
public Object visitSelfExpress(Express.Self self) {
return findVar(self.keyword, self);
}
@Override
public Void visitExpressionStatement(Statement.Expression statement) {
evaluate(statement.expression);
return null;
}
@Override
public Void visitOperationStatement(Statement.Operation statement) {
DisOp operation = new DisOp(statement, fields, false);
fields.define(statement.name.lexeme, operation);
return null;
}
@Override
public Void visitWhenStatement(Statement.When statement) {
boolean met = false;
if (isTruthful(evaluate(statement.condition))) {
met = true;
execute(statement.thenBranch);
} else {
for (Statement.Or orStatement : statement.orBranches) {
if (isTruthful(evaluate(orStatement.condition)) && !met) {
met = true;
execute(orStatement.orBranch);
}
}
}
if (statement.elseBranch != null && !met) {
execute(statement.elseBranch);
}
return null;
}
@Override
public Void visitOrStatement(Statement.Or statement) {
if (isTruthful(evaluate(statement.condition))) {
execute(statement.orBranch);
}
return null;
}
@Override
public Void visitWhileStatement(Statement.While statement) {
while (isTruthful(evaluate(statement.condition))) { execute(statement.body); }
return null;
}
@Override
public Void visitPrintStatement(Statement.Print statement) {
Object value = evaluate(statement.expression);
System.out.println(asString(value));
return null;
}
@Override
public Void visitReturnStatement(Statement.Return statement) {
Object value = null;
if(statement.value != null) { value = evaluate(statement.value); }
throw new Return(value);
}
@Override
public Void visitObjStatement(Statement.Obj object) {
fields.define(object.name.lexeme, null);
Map<String, DisOp> methods = new HashMap<>();
for (Statement.Operation method : object.body) {
DisOp op = new DisOp(method, fields, method.name.lexeme.equals("init"));
methods.put(method.name.lexeme, op);
}
DisObj obj = new DisObj(object.name.lexeme, methods);
fields.assign(object.name, obj);
return null;
}
@Override
public Void visitEnumStatement(Statement.Enum enumstmnt) {
fields.define(enumstmnt.name.lexeme, null);
Integer i = 0;
Map<String, Integer> elements = new HashMap<>();
for (Express.Variable element : enumstmnt.elements) {
elements.put(element.name.lexeme, i);
i++;
}
DisEnum enums = new DisEnum(enumstmnt.name.lexeme, elements);
fields.assign(enumstmnt.name, enums);
return null;
}
@Override
public Void visitFormStatement(Statement.Form form) {
fields.define(form.name.lexeme, null);
Map<String, Object> members = new HashMap<>();
for (Statement.Variable member : form.members) {
Object value = evaluate(member.initial);
members.put(member.name.lexeme, value);
}
DisForm formt = new DisForm(form.name.lexeme, members);
fields.assign(form.name, formt);
return null;
}
@Override
public Void visitBodyStatement(Statement.Body statement) {
executeBlock(statement.statements, new Field(fields));
return null;
}
@Override
public Void visitVariableStatement(Statement.Variable statement) {
Object value = null;
if (statement.initial != null ) { value = evaluate(statement.initial); }
fields.define(statement.name.lexeme, value);
return null;
}
private boolean isTruthful(Object object) {
if (object == null /*|| object.length < 1*/) { return false; }
if (object instanceof Boolean) { return (boolean)object; }
return true;
}
private boolean isEqual(Object a, Object b){
if (a == null && b == null) { return true; }
if (a == null) { return false; }
return a.equals(b);
}
private String asString(Object object) {
if (object == null) { return "none"; }
if (object instanceof Double || object instanceof Integer) {
String text = object.toString();
if (text.endsWith(".0")) {
text = text.substring(0, text.length() - 2);
}
return text;
}
return object.toString();
}
void resolve(Express express, int depth) {
relatives.put(express, depth);
}
private Object evaluate(Express express) {
return express.accept(this);
}
private void execute(Statement statement) {
statement.accept(this);
}
private Object findVar(Token name, Express express) {
Integer distance = relatives.get(express);
if(distance != null){
return fields.getAt(distance, name.lexeme);
} else {
return globals.get(name);
}
}
void executeBlock(List<Statement> statements, Field field) {
Field previous = this.fields;
try {
this.fields = field;
for (Statement statement : statements) { execute(statement); }
} finally { this.fields = previous; }
}
private void checkNumberOperand(Token operator, Object operand) {
if (operand instanceof Double || operand instanceof Integer) {
return;
}
throw new RuntimeError(operator, "Operand must be a number.");
}
private void checkNumberOperands(Token operator, Object left, Object right) {
if ((left instanceof Integer || left instanceof Double) &&
(right instanceof Integer || right instanceof Double)) {
return;
}
throw new RuntimeError(operator, "Operands must be numbers.");
}
}