-
Notifications
You must be signed in to change notification settings - Fork 0
/
REPL.java
44 lines (38 loc) · 1.41 KB
/
REPL.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
import java.util.*;
import codeanalysis.*;
// EP2: 31:01
class REPL {
static void printColorText(String color, String text) {
System.out.println(Colors.ANSI_RED + text + Colors.ANSI_RESET);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean showTree = false;
while (true) {
System.out.print(">");
String str = sc.nextLine();
if (str.isEmpty())
break;
else if (str.equals("#showTree")) {
showTree = !showTree;
printColorText(Colors.ANSI_GREEN, showTree ? "Enabled Tree Printing" : "Disabled Tree Priniting");
continue;
} else if (str.equals("#cls")) {
System.out.print("\033\143");
continue;
}
var syntaxTree = SyntaxTree.parse(str);
if (showTree) PrettyPrint.pprint(syntaxTree.get_root(), "", showTree);
if (!syntaxTree.get_diagnostics().isEmpty()) {
for (String error : syntaxTree.get_diagnostics()) {
printColorText(Colors.ANSI_RED, error);
}
} else {
Evaluator e = new Evaluator(syntaxTree.get_root());
var res = e.evaluate();
printColorText(Colors.ANSI_GREEN, "Result : " + res);
}
}
sc.close();
}
}