blob: 5381d4515fd51d6882fc313111a3b622377e5532 (
plain)
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
|
package edu.brown.cs.student.term.repl;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* REPL Class maps input to proper class and function for that command.
*/
public class REPL {
private Map<String, Command> commands;
/**
* Constructor for a REPL object.
* @param userCommands - map of string to Command object for REPL to run
*/
public REPL(HashMap<String, Command> userCommands) {
commands = userCommands;
}
/**
* Gets the current map of commands the REPL supports.
* @return the command map
*/
public Map<String, Command> getReplCommandMap() {
return commands; }
/**
* Reads user input, maps it to the correct command, and continues.
*/
public void runREPL() {
BufferedReader commandHandler = new BufferedReader(new InputStreamReader(System.in));
try {
String command = commandHandler.readLine();
while (command != null) {
String[] commandPieces = command.split("\\s+(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
if (commands.containsKey(commandPieces[0])) {
Command desiredCommand = commands.get(commandPieces[0]);
String[] arguments = Arrays.copyOfRange(commandPieces, 1, commandPieces.length);
desiredCommand.run(arguments);
} else {
System.out.println("ERROR: Sorry, command not recognized! Please try again.");
}
command = commandHandler.readLine();
}
} catch (IOException e) {
//might wanna change this later
System.out.println("ERROR: Could not locate specified file!");
}
}
}
|