forked from mkhan45/RustScript
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Repl.java
126 lines (118 loc) · 3.3 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
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
import java.util.Scanner;
import java.util.Stack;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.File;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.NoSuchElementException;
import core.Interpreter;
/**
* @author Mikail Khan <mikail@mikail-khan.com>, William Rågstad
* <william.ragstad@gmail.com>
*
* The REPL program lets you interact with the RustScript programming
* language and run commands as you go.
*/
public class Repl {
public static void main(String[] args) {
run();
}
private static String currentDirectory;
private static HashMap<String, Consumer<String[]>> commands = new HashMap<String, Consumer<String[]>>() {
{
put("pwd", (args) -> {
System.out.println("pwd: " + currentDirectory);
});
put("cd", (args) -> {
if (args.length > 1) {
System.out.println("cd: too many arguments");
} else if (args.length == 1) {
Path to = Paths.get(currentDirectory).resolve(args[0]);
if (to.toFile().exists() && Files.isDirectory(to)) {
currentDirectory = to.normalize().toAbsolutePath().toString();
} else {
System.out.println("cd: no such directory");
}
} else {
System.out.println("cd: missing operand");
}
});
put("ls", (args) -> {
if (args.length > 0) {
System.out.println("ls: no arguments expected");
} else {
File currentFile = new File(currentDirectory);
Stack<String> files = new Stack<String>();
Supplier<String> file = () -> {
if (files.isEmpty())
return "";
String f = files.pop();
return f == null ? "" : f;
};
for (File f : currentFile.listFiles()) {
if (f.isDirectory()) {
files.push(f.getName() + "/");
} else {
files.push(f.getName());
}
if (files.size() >= 3) {
System.out.printf("%-22s%-22s%-22s\n", file.get(), file.get(), file.get());
}
}
if (!files.isEmpty()) {
// Push the last three files off the stack
System.out.printf("%-22s%-22s%-22s\n", file.get(), file.get(), file.get());
}
}
});
}
};
private static boolean tryRunCommand(String expr) {
ArrayList<String> args = new ArrayList<>(Arrays.asList(expr.trim().split(" ")));
String command = args.remove(0);
if (commands.containsKey(command)) {
commands.get(command).accept(args.toArray(new String[args.size()]));
return true;
}
return false;
}
public static void run() {
currentDirectory = System.getProperty("user.dir");
Interpreter i;
try {
i = new Interpreter();
} catch (Exception e) {
System.out.println(e.getMessage());
return;
}
Scanner sc = new Scanner(System.in, getSupportedCharset());
for (;;) {
try {
System.out.print("> ");
String expr = sc.nextLine();
if (!tryRunCommand(expr)) {
i.executeAll(expr, currentDirectory);
}
} catch (NoSuchElementException e) {
System.out.println(e.getMessage());
sc.close();
break;
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
private static String getSupportedCharset() {
switch (System.getProperty("os.name")) {
case "Windows 10":
return "Cp850";
default:
return "UTF-8";
}
}
}