我的任务是用 Java 编写一种命令行。
它旨在测试编译器的某些部分。您会看到一个提示,可以键入“read_source”、“parse”、“build_ast”、“ast2cfg”、“print_cfg”等命令。
Java中是否有任何库可以帮助我构建解释器(或repl?)。我知道一个 python 模块可以做我想要的:cmd
我自己写了类似的东西:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
interface Command {
public abstract void action(String[] parameters);
}
public class Repl {
private String prompt = "\n$ ";
private HashMap<String, Command> commands;
private Command nullCommand = null;
private Command defaultCommand = null;
public Repl() {
commands = new HashMap<String, Command>();
}
public void setPrompt(String prompt) {
this.prompt = prompt;
}
public void setDefaultCommand(Command defaultCommand) {
this.defaultCommand = defaultCommand;
}
public void setNullCommand(Command nullCommand) {
this.nullCommand = nullCommand;
}
public void addCommand(String name, Command command) {
commands.put(name, command);
}
public void runRepl() {
if (nullCommand == null) {
System.err.println("There is no 'nullCommand' specified");
return;
}
if (defaultCommand == null) {
System.err.println("There is no 'defaultCommand' specified");
return;
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
String commandName;
String[] parameters;
while (true) {
System.out.print(prompt);
commandName = reader.readLine();
if (commandName == null) {
nullCommand.action(null);
} else {
parameters = commandName.trim().split("\\s+");
Command com = commands.get(parameters[0]);
if (com != null) {
com.action(parameters);
} else {
defaultCommand.action(parameters);
}
}
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("Internal error within compiler: stopping compilation");
System.exit(1);
}
}
/**
* Exapmpe:
*/
public static void main(String[] args) {
Repl repl = new Repl();
repl.addCommand("printparams", new Command() {
@Override
public void action(String[] parameters) {
System.out.println(Arrays.toString(parameters));
}
});
repl.addCommand("add", new Command() {
@Override
public void action(String[] parameters) {
if (parameters.length == 3) { // {"add", "2", "3"}
try {
int x = Integer.parseInt(parameters[1]);
int y = Integer.parseInt(parameters[2]);
System.out.println("Ergebnis: " + (x + y));
} catch (NumberFormatException e) {
System.out.println("Arguments have to be integers");
}
} else {
System.out.println("There have to be two arguments");
}
}
});
Command helpcommand = new Command() {
@Override
public void action(String[] parameters) {
System.out.println("There is: 'help', 'printparams [PARAMS...]', 'exit' and 'add INTEGER INTEGER'");
}
};
repl.addCommand("help", helpcommand);
repl.setDefaultCommand(helpcommand);
Command exitcommand = new Command() {
@Override
public void action(String[] parameters) {
System.out.println("Bye!");
System.exit(0);
}
};
repl.addCommand("exit", exitcommand);
repl.setNullCommand(exitcommand);
repl.runRepl();
}
}
它可以工作,但它会很好,例如,如果根据参数解析的方式生成用于添加两个更多数字的“用法:添加整数整数 [整数...]”等使用模式,那么添加新命令并确保它们始终一致的工作更少。
我也在问自己这是否过度设计。你会建议只写一个这样的循环吗?:
while(true) {
String command = getUserInput();
if (command.equals("dothis")) {
dothis1();
someMember = dothis2();
} else if (command.equals("dothat"))
dothat();
} else {
printHelp();
}
}