2016-12-17 10:19:18 -05:00
|
|
|
package main;
|
|
|
|
|
2017-03-19 12:54:35 -04:00
|
|
|
import static terminal.LispTerminal.END_OF_SEGMENT;
|
|
|
|
|
2017-03-18 16:49:46 -04:00
|
|
|
import java.io.*;
|
2017-02-11 13:33:34 -05:00
|
|
|
import java.util.function.Function;
|
|
|
|
|
2017-03-19 12:54:35 -04:00
|
|
|
import com.googlecode.lanterna.terminal.*;
|
|
|
|
|
2016-12-17 10:19:18 -05:00
|
|
|
import interpreter.*;
|
2017-03-17 16:07:25 -04:00
|
|
|
import terminal.LispTerminal;
|
2016-12-17 10:19:18 -05:00
|
|
|
|
|
|
|
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";
|
2017-03-18 16:49:46 -04:00
|
|
|
private PipedInputStream inputReader;
|
|
|
|
private PipedOutputStream inputWriter;
|
|
|
|
private PipedInputStream outputReader;
|
|
|
|
private PipedOutputStream outputWriter;
|
2017-03-19 12:54:35 -04:00
|
|
|
private LispTerminal lispTerminal;
|
2017-02-11 13:33:34 -05:00
|
|
|
|
2017-03-18 16:49:46 -04:00
|
|
|
private LispMain() throws IOException {
|
|
|
|
inputReader = new PipedInputStream();
|
|
|
|
inputWriter = new PipedOutputStream(inputReader);
|
|
|
|
outputReader = new PipedInputStream();
|
|
|
|
outputWriter = new PipedOutputStream(outputReader);
|
2017-03-19 12:54:35 -04:00
|
|
|
lispTerminal = new LispTerminal(createIOSafeTerminal(), inputWriter, outputReader);
|
2017-03-18 16:49:46 -04:00
|
|
|
}
|
|
|
|
|
2017-03-19 12:54:35 -04:00
|
|
|
private IOSafeTerminal createIOSafeTerminal() throws IOException {
|
|
|
|
return IOSafeTerminalAdapter.createRuntimeExceptionConvertingAdapter(new DefaultTerminalFactory().createTerminal());
|
|
|
|
}
|
2016-12-17 10:19:18 -05:00
|
|
|
|
2017-03-19 12:54:35 -04:00
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
|
new LispMain().run(args);
|
2017-03-18 16:49:46 -04:00
|
|
|
}
|
2017-03-17 16:07:25 -04:00
|
|
|
|
2017-03-18 16:49:46 -04:00
|
|
|
private void run(String[] args) {
|
|
|
|
if (args.length == 0)
|
2017-03-21 09:25:40 -04:00
|
|
|
lispTerminal.start();
|
2017-03-17 16:07:25 -04:00
|
|
|
|
2017-03-18 16:49:46 -04:00
|
|
|
LispInterpreter interpreter = buildInterpreter(args);
|
|
|
|
interpreter.interpret();
|
2016-12-17 10:19:18 -05:00
|
|
|
}
|
|
|
|
|
2017-03-18 16:49:46 -04:00
|
|
|
private 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-03-18 16:49:46 -04:00
|
|
|
configureOutput(args, builder);
|
|
|
|
configureTerminatingFunctions(args, builder);
|
|
|
|
configureDecorators(args, builder);
|
2016-12-17 10:19:18 -05:00
|
|
|
|
2017-02-09 11:00:23 -05:00
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
2017-03-18 16:49:46 -04:00
|
|
|
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));
|
2017-03-19 17:17:34 -04:00
|
|
|
builder.setValueOutputDecorator(s -> s);
|
|
|
|
builder.setWarningOutputDecorator(s -> s);
|
|
|
|
builder.setErrorOutputDecorator(s -> s);
|
|
|
|
builder.setCriticalOutputDecorator(s -> s);
|
2017-03-18 16:49:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void shutdown() {
|
|
|
|
try {
|
2017-03-21 09:25:40 -04:00
|
|
|
lispTerminal.stop();
|
2017-03-18 16:49:46 -04:00
|
|
|
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) {
|
2017-01-17 13:54:21 -05:00
|
|
|
if (args.length > 0)
|
|
|
|
builder.useFile(args[0]);
|
2017-02-26 12:31:27 -05:00
|
|
|
else
|
2017-03-18 16:49:46 -04:00
|
|
|
builder.setInput(inputReader, "stdin");
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-03-18 16:49:46 -04:00
|
|
|
private static Function<String, String> makeInteractiveDecorator(String color) {
|
|
|
|
return new Function<String, String>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String apply(String s) {
|
2017-03-19 12:54:35 -04:00
|
|
|
return s + END_OF_SEGMENT;
|
2017-03-18 16:49:46 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-12-17 10:19:18 -05:00
|
|
|
}
|