2016-12-17 10:19:18 -05:00
|
|
|
package main;
|
|
|
|
|
2017-02-11 13:33:34 -05:00
|
|
|
import java.util.function.Function;
|
|
|
|
|
2016-12-17 10:19:18 -05:00
|
|
|
import interpreter.*;
|
|
|
|
|
|
|
|
public class LispMain {
|
|
|
|
|
2017-02-11 13:33:34 -05:00
|
|
|
private static final String ANSI_RESET = "\u001B[0m";
|
|
|
|
private static final String ANSI_RED = "\u001B[31m";
|
|
|
|
private static final String ANSI_GREEN = "\u001B[32m";
|
|
|
|
private static final String ANSI_YELLOW = "\u001B[33m";
|
|
|
|
private static final String ANSI_PURPLE = "\u001B[35m";
|
|
|
|
|
2016-12-17 10:19:18 -05:00
|
|
|
private LispMain() {}
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
2017-01-17 13:54:21 -05:00
|
|
|
LispInterpreter interpreter = buildInterpreter(args);
|
2016-12-17 10:19:18 -05:00
|
|
|
interpreter.interpret();
|
|
|
|
}
|
|
|
|
|
2017-01-17 13:54:21 -05:00
|
|
|
private static LispInterpreter buildInterpreter(String[] args) {
|
2017-02-06 13:39:05 -05:00
|
|
|
LispInterpreterBuilder builder = LispInterpreterBuilderImpl.getInstance();
|
2017-02-11 13:33:34 -05:00
|
|
|
configureInput(args, builder);
|
2017-01-17 13:54:21 -05:00
|
|
|
builder.setOutput(System.out);
|
|
|
|
builder.setErrorOutput(System.err);
|
2017-01-18 16:25:09 -05:00
|
|
|
builder.setTerminationFunction(() -> System.exit(0));
|
|
|
|
builder.setErrorTerminationFunction(() -> System.exit(1));
|
2017-02-11 13:33:34 -05:00
|
|
|
builder.setValueOutputDecorator(makeColorDecorator(ANSI_GREEN));
|
|
|
|
builder.setWarningOutputDecorator(makeColorDecorator(ANSI_YELLOW));
|
|
|
|
builder.setErrorOutputDecorator(makeColorDecorator(ANSI_RED));
|
|
|
|
builder.setCriticalOutputDecorator(makeColorDecorator(ANSI_PURPLE));
|
2016-12-17 10:19:18 -05:00
|
|
|
|
2017-02-09 11:00:23 -05:00
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void configureInput(String[] args, LispInterpreterBuilder builder) {
|
2017-01-17 13:54:21 -05:00
|
|
|
if (args.length > 0)
|
|
|
|
builder.useFile(args[0]);
|
2017-02-09 11:00:23 -05:00
|
|
|
else {
|
|
|
|
builder.setInputName("stdin");
|
|
|
|
builder.setInput(System.in);
|
|
|
|
}
|
2016-12-17 10:19:18 -05:00
|
|
|
}
|
|
|
|
|
2017-02-11 13:33:34 -05:00
|
|
|
private static Function<String, String> makeColorDecorator(String color) {
|
|
|
|
return new Function<String, String>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String apply(String s) {
|
|
|
|
return color + s + ANSI_RESET;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-12-17 10:19:18 -05:00
|
|
|
}
|