-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniInterpreter.java
More file actions
95 lines (86 loc) · 3.37 KB
/
MiniInterpreter.java
File metadata and controls
95 lines (86 loc) · 3.37 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
/*
* Bonus Challenge (Optional): Mini Interpreter
Build a simple interpreter to evaluate let variable declarations
and if conditions from input strings.
*/
import java.util.*;
public class MiniInterpreter {
private final Map<String, Integer> variables = new HashMap<>();
public void execute(String line) {
line = line.trim();
if (line.startsWith("let ")) {
// let x = 5
String[] parts = line.substring(4).split("=");
String var = parts[0].trim();
int val = evalExpr(parts[1].trim());
variables.put(var, val);
} else if (line.startsWith("if ")) {
// if x > 3 then y = 10 else y = 20
int thenIdx = line.indexOf("then");
int elseIdx = line.indexOf("else");
String cond = line.substring(3, thenIdx).trim();
String thenPart = line.substring(thenIdx + 4, elseIdx).trim();
String elsePart = line.substring(elseIdx + 4).trim();
boolean condResult = evalCondition(cond);
execute(condResult ? thenPart : elsePart);
} else if (line.startsWith("print ")) {
// print y
String var = line.substring(6).trim();
System.out.println(variables.getOrDefault(var, null));
} else if (line.contains("=")) {
// y = 10
String[] parts = line.split("=");
String var = parts[0].trim();
int val = evalExpr(parts[1].trim());
variables.put(var, val);
}
}
// Evaluate simple integer expressions or variable names
private int evalExpr(String expr) {
expr = expr.trim();
if (variables.containsKey(expr)) return variables.get(expr);
return Integer.parseInt(expr);
}
// Evaluate simple conditions like x > 3, y == 10, etc.
private boolean evalCondition(String cond) {
cond = cond.trim();
if (cond.contains(">=")) {
String[] p = cond.split(">=");
return evalExpr(p[0]) >= evalExpr(p[1]);
} else if (cond.contains("<=")) {
String[] p = cond.split("<=");
return evalExpr(p[0]) <= evalExpr(p[1]);
} else if (cond.contains(">")) {
String[] p = cond.split(">");
return evalExpr(p[0]) > evalExpr(p[1]);
} else if (cond.contains("<")) {
String[] p = cond.split("<");
return evalExpr(p[0]) < evalExpr(p[1]);
} else if (cond.contains("==")) {
String[] p = cond.split("==");
return evalExpr(p[0]) == evalExpr(p[1]);
} else if (cond.contains("!=")) {
String[] p = cond.split("!=");
return evalExpr(p[0]) != evalExpr(p[1]);
}
throw new IllegalArgumentException("Invalid condition: " + cond);
}
// Example usage and test cases
public static void main(String[] args) {
MiniInterpreter interpreter = new MiniInterpreter();
interpreter.execute("let x = 5");
interpreter.execute("if x > 3 then y = 10 else y = 20");
interpreter.execute("print y"); // Output: 10
interpreter.execute("let a = 2");
interpreter.execute("if a == 2 then b = 100 else b = 200");
interpreter.execute("print b"); // Output: 100
interpreter.execute("if x < 3 then z = 1 else z = 2");
interpreter.execute("print z"); // Output: 2
}
}
/*
Sample Output:
10
100
2
*/