This repository was archived by the owner on Jul 21, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterpreter.java
More file actions
380 lines (348 loc) · 11.4 KB
/
Copy pathInterpreter.java
File metadata and controls
380 lines (348 loc) · 11.4 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
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
package aether;
import aether.ast.*;
import aether.lexer.Token;
import java.util.ArrayList;
import java.util.List;
/**
* Executes Aether programs by traversing the Abstract Syntax Tree (AST)
* utilizing the Visitor pattern. Resolves variables, evaluates expressions,
* and maintains environment scopes.
*/
public class Interpreter implements AST.Visitor<Object> {
/**
* The active execution scope holding variables and values.
*/
private Environment environment = new Environment();
/**
* Interprets a list of parsed statements, catching and logging runtime errors.
*
* @param statements the list of AST statements to execute
*/
public void interpret(List<AST.Stmt> statements) {
try {
for (AST.Stmt statement : statements) {
if (statement != null) {
execute(statement);
}
}
} catch (RuntimeException error) {
System.err.println("Runtime Error: " + error.getMessage());
}
}
/**
* Helper method to execute a statement node.
*
* @param stmt the statement to execute
*/
private void execute(AST.Stmt stmt) {
stmt.accept(this);
}
/**
* Helper method to evaluate an expression node.
*
* @param expr the expression to evaluate
* @return the runtime value resulting from evaluation
*/
private Object evaluate(AST.Expr expr) {
return expr.accept(this);
}
/**
* Visits a variable declaration statement. Evaluates the initializer if present
* and registers the variable in the environment.
*
* @param stmt the variable declaration statement
* @return null
*/
@Override
public Object visitVarDeclStmt(VarDecl stmt) {
Object value = null;
if (stmt.initializer() != null) {
value = evaluate(stmt.initializer());
}
environment.define(stmt.name().lexeme(), value);
return null;
}
/**
* Visits an expression statement. Evaluates the wrapped expression.
*
* @param stmt the expression statement
* @return null
*/
@Override
public Object visitExpressionStmt(ExpressionStmt stmt) {
evaluate(stmt.expression());
return null;
}
/**
* Visits a block statement. Creates a nested local scope and executes all inner statements.
*
* @param stmt the block statement
* @return null
*/
@Override
public Object visitBlockStmt(Block stmt) {
executeBlock(stmt.statements(), new Environment(environment));
return null;
}
/**
* Executes a list of statements in the context of a specific Environment scope.
* Guarantees restoration of the original environment upon completion.
*
* @param statements the statements inside the block
* @param env the new local Environment scope
*/
public void executeBlock(List<AST.Stmt> statements, Environment env) {
Environment previous = this.environment;
try {
this.environment = env;
for (AST.Stmt statement : statements) {
if (statement != null) {
execute(statement);
}
}
} finally {
this.environment = previous;
}
}
/**
* Visits an if (flux) statement. Evaluates the condition and branches execution accordingly.
*
* @param stmt the flux statement
* @return null
*/
@Override
public Object visitFluxStmt(Flux stmt) {
if (isTruthy(evaluate(stmt.condition()))) {
if (stmt.thenBranch() != null) {
execute(stmt.thenBranch());
}
} else if (stmt.elseBranch() != null) {
execute(stmt.elseBranch());
}
return null;
}
/**
* Visits a loop (cycle) statement. Executes the loop body repeatedly while the condition evaluates to truthy.
*
* @param stmt the cycle statement
* @return null
*/
@Override
public Object visitCycleStmt(Cycle stmt) {
while (isTruthy(evaluate(stmt.condition()))) {
if (stmt.body() != null) {
execute(stmt.body());
}
}
return null;
}
/**
* Visits a reveal (print) statement. Outputs the stringified representation of the evaluated expression.
*
* @param stmt the reveal statement
* @return null
*/
@Override
public Object visitRevealStmt(Reveal stmt) {
Object value = evaluate(stmt.expression());
System.out.println(stringify(value));
return null;
}
/**
* Visits an assignment expression. Evaluates the value and updates the variable in the environment.
*
* @param expr the assignment expression
* @return the assigned value
*/
@Override
public Object visitAssignExpr(Assign expr) {
Object value = evaluate(expr.value());
environment.assign(expr.name(), value);
return value;
}
/**
* Visits a variable lookup expression. Retrieves its value from the environment.
*
* @param expr the variable lookup expression
* @return the value of the variable
*/
@Override
public Object visitVariableExpr(Variable expr) {
return environment.get(expr.name());
}
/**
* Visits a literal expression. Returns the literal value directly.
*
* @param expr the literal expression
* @return the literal value
*/
@Override
public Object visitLiteralExpr(Literal expr) {
return expr.value();
}
/**
* Visits an array literal expression. Evaluates all element expressions.
*
* @param expr the array literal expression
* @return a List containing evaluated element values
*/
@Override
public Object visitArrayLiteralExpr(ArrayLiteral expr) {
List<Object> elements = new ArrayList<>();
for (AST.Expr element : expr.elements()) {
elements.add(evaluate(element));
}
return elements;
}
/**
* Visits a unary expression (! or -). Evaluates the right operand and applies the operator.
*
* @param expr the unary expression
* @return the evaluated unary result
*/
@Override
public Object visitUnaryExpr(Unary expr) {
Object right = evaluate(expr.right());
switch (expr.operator().type()) {
case BANG -> {
return !isTruthy(right);
}
case MINUS -> {
checkNumberOperand(expr.operator(), right);
return -(double) right;
}
default -> {
}
}
return null;
}
/**
* Visits a binary expression (+, -, *, /, comparisons, equalities).
* Evaluates both operands and applies the operation.
*
* @param expr the binary expression
* @return the evaluated binary result
*/
@Override
public Object visitBinaryExpr(Binary expr) {
Object left = evaluate(expr.left());
Object right = evaluate(expr.right());
switch (expr.operator().type()) {
case GREATER -> {
checkNumberOperands(expr.operator(), left, right);
return (double) left > (double) right;
}
case GREATER_EQUAL -> {
checkNumberOperands(expr.operator(), left, right);
return (double) left >= (double) right;
}
case LESS -> {
checkNumberOperands(expr.operator(), left, right);
return (double) left < (double) right;
}
case LESS_EQUAL -> {
checkNumberOperands(expr.operator(), left, right);
return (double) left <= (double) right;
}
case BANG_EQUAL -> {
return !isEqual(left, right);
}
case EQUAL_EQUAL -> {
return isEqual(left, right);
}
case MINUS -> {
checkNumberOperands(expr.operator(), left, right);
return (double) left - (double) right;
}
case PLUS -> {
if (left instanceof Double d1 && right instanceof Double d2) {
return d1 + d2;
}
if (left instanceof String s1 && right instanceof String s2) {
return s1 + s2;
}
throw new RuntimeException("Operands must be two numbers or two strings at line " + expr.operator().line() + ".");
}
case SLASH -> {
checkNumberOperands(expr.operator(), left, right);
double divisor = (double) right;
if (divisor == 0) {
throw new RuntimeException("Division by zero at line " + expr.operator().line() + ".");
}
return (double) left / divisor;
}
case STAR -> {
checkNumberOperands(expr.operator(), left, right);
return (double) left * (double) right;
}
default -> {
}
}
return null;
}
/**
* Checks if the evaluated value is truthy (non-null and not Boolean.FALSE).
*
* @param object the object to check
* @return true if truthy, false otherwise
*/
private boolean isTruthy(Object object) {
if (object == null) return false;
if (object instanceof Boolean b) return b;
return true;
}
/**
* Checks if two values are equal.
* Handles null values safely.
*
* @param a first operand
* @param b second operand
* @return true if equal, false otherwise
*/
private boolean isEqual(Object a, Object b) {
if (a == null && b == null) return true;
if (a == null) return false;
return a.equals(b);
}
/**
* Validates that the operand is a number.
*
* @param operator the unary operator token (for line reporting)
* @param operand the operand to check
* @throws RuntimeException if operand is not a number
*/
private void checkNumberOperand(Token operator, Object operand) {
if (operand instanceof Double) return;
throw new RuntimeException("Operand must be a number at line " + operator.line() + ".");
}
/**
* Validates that both operands are numbers.
*
* @param operator the binary operator token
* @param left the left operand
* @param right the right operand
* @throws RuntimeException if either operand is not a number
*/
private void checkNumberOperands(Token operator, Object left, Object right) {
if (left instanceof Double && right instanceof Double) return;
throw new RuntimeException("Operands must be numbers at line " + operator.line() + ".");
}
/**
* Converts a runtime object to its string representation for output.
* Truncates decimal parts (.0) for whole numbers.
*
* @param object the runtime value
* @return the stringified value
*/
private String stringify(Object object) {
if (object == null) return "nil";
if (object instanceof Double d) {
String text = d.toString();
if (text.endsWith(".0")) {
text = text.substring(0, text.length() - 2);
}
return text;
}
return object.toString();
}
}