transcendental-lisp/src/main/LispMain.java

135 lines
4.7 KiB
Java
Raw Normal View History

package main;
import static terminal.LispTerminal.END_OF_SEGMENT;
import java.io.*;
2017-02-11 13:33:34 -05:00
import java.util.function.Function;
import com.googlecode.lanterna.terminal.*;
import interpreter.*;
2017-03-17 16:07:25 -04:00
import terminal.LispTerminal;
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";
private PipedInputStream inputReader;
private PipedOutputStream inputWriter;
private PipedInputStream outputReader;
private PipedOutputStream outputWriter;
private LispTerminal lispTerminal;
2017-02-11 13:33:34 -05:00
private LispMain() throws IOException {
inputReader = new PipedInputStream();
inputWriter = new PipedOutputStream(inputReader);
outputReader = new PipedInputStream();
outputWriter = new PipedOutputStream(outputReader);
lispTerminal = new LispTerminal(createIOSafeTerminal(), inputWriter, outputReader);
}
private IOSafeTerminal createIOSafeTerminal() throws IOException {
return IOSafeTerminalAdapter.createRuntimeExceptionConvertingAdapter(new DefaultTerminalFactory().createTerminal());
}
public static void main(String[] args) throws IOException {
new LispMain().run(args);
}
2017-03-17 16:07:25 -04:00
private void run(String[] args) {
if (args.length == 0)
lispTerminal.run();
2017-03-17 16:07:25 -04:00
LispInterpreter interpreter = buildInterpreter(args);
interpreter.interpret();
}
private LispInterpreter buildInterpreter(String[] args) {
LispInterpreterBuilder builder = LispInterpreterBuilderImpl.getInstance();
2017-02-11 13:33:34 -05:00
configureInput(args, builder);
configureOutput(args, builder);
configureTerminatingFunctions(args, builder);
configureDecorators(args, builder);
return builder.build();
}
private void configureTerminatingFunctions(String[] args, LispInterpreterBuilder builder) {
if (args.length > 0) {
builder.setTerminationFunction(() -> System.exit(0));
builder.setErrorTerminationFunction(() -> System.exit(1));
} else {
builder.setTerminationFunction(this::shutdown);
builder.setErrorTerminationFunction(this::shutdown);
}
}
private void configureDecorators(String[] args, LispInterpreterBuilder builder) {
if (args.length > 0) {
builder.setOutputDecorator(makeColorDecorator(ANSI_GREEN));
builder.setValueOutputDecorator(makeColorDecorator(ANSI_GREEN));
builder.setWarningOutputDecorator(makeColorDecorator(ANSI_YELLOW));
builder.setErrorOutputDecorator(makeColorDecorator(ANSI_RED));
builder.setCriticalOutputDecorator(makeColorDecorator(ANSI_PURPLE));
} else {
builder.setOutputDecorator(makeInteractiveDecorator(ANSI_GREEN));
builder.setValueOutputDecorator(makeInteractiveDecorator(ANSI_GREEN));
builder.setWarningOutputDecorator(makeInteractiveDecorator(ANSI_YELLOW));
builder.setErrorOutputDecorator(makeInteractiveDecorator(ANSI_RED));
builder.setCriticalOutputDecorator(makeInteractiveDecorator(ANSI_PURPLE));
}
}
private void shutdown() {
try {
lispTerminal.finish();
outputWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void configureOutput(String[] args, LispInterpreterBuilder builder) {
if (args.length > 0) {
builder.setOutput(System.out);
builder.setErrorOutput(System.err);
} else {
PrintStream outputStream = new PrintStream(outputWriter);
builder.setOutput(outputStream);
builder.setErrorOutput(outputStream);
}
}
private void configureInput(String[] args, LispInterpreterBuilder builder) {
if (args.length > 0)
builder.useFile(args[0]);
else
builder.setInput(inputReader, "stdin");
}
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;
}
};
}
private static Function<String, String> makeInteractiveDecorator(String color) {
return new Function<String, String>() {
@Override
public String apply(String s) {
return s + END_OF_SEGMENT;
}
};
}
}