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;
|
2017-03-22 11:26:53 -04:00
|
|
|
import terminal.SafeStream.SafeOutputStream;
|
2017-03-21 12:07:22 -04:00
|
|
|
import terminal.SafeStream.UncheckedIOException;
|
2016-12-17 10:19:18 -05:00
|
|
|
|
|
|
|
public class LispMain {
|
|
|
|
|
2017-03-21 15:03:16 -04:00
|
|
|
private static final String GREETING = "Transcendental Lisp - Version 1.0.1";
|
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-21 12:07:22 -04:00
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
|
LispMain lispMain = new LispMain();
|
|
|
|
|
|
|
|
if (args.length == 0)
|
|
|
|
lispMain.runInteractive();
|
|
|
|
else
|
|
|
|
lispMain.runWithFile(args[0]);
|
|
|
|
}
|
|
|
|
|
2017-03-18 16:49:46 -04:00
|
|
|
private PipedInputStream inputReader;
|
|
|
|
private PipedOutputStream inputWriter;
|
|
|
|
private PipedInputStream outputReader;
|
|
|
|
private PipedOutputStream outputWriter;
|
2017-03-21 15:03:16 -04:00
|
|
|
private PrintStream outputStream;
|
2017-03-22 11:26:53 -04:00
|
|
|
private SafeOutputStream safeOutputWriter;
|
2017-03-19 12:54:35 -04:00
|
|
|
private LispTerminal lispTerminal;
|
2017-02-11 13:33:34 -05:00
|
|
|
|
2017-03-21 15:03:16 -04:00
|
|
|
private void runInteractive() {
|
|
|
|
initializeTerminalAndStreams();
|
|
|
|
printGreeting();
|
|
|
|
lispTerminal.start();
|
|
|
|
buildInteractiveInterpreter().interpret();
|
|
|
|
outputStream.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeTerminalAndStreams() {
|
|
|
|
try {
|
|
|
|
initalizeTerminalAndStreamsWithException();
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new UncheckedIOException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initalizeTerminalAndStreamsWithException() throws IOException {
|
|
|
|
inputReader = new PipedInputStream();
|
|
|
|
inputWriter = new PipedOutputStream(inputReader);
|
|
|
|
outputReader = new PipedInputStream();
|
|
|
|
outputWriter = new PipedOutputStream(outputReader);
|
|
|
|
outputStream = new PrintStream(outputWriter);
|
2017-03-22 11:26:53 -04:00
|
|
|
safeOutputWriter = new SafeOutputStream(outputWriter);
|
2017-03-21 15:03:16 -04:00
|
|
|
lispTerminal = new LispTerminal(createIOSafeTerminal(), inputWriter, outputReader);
|
|
|
|
}
|
|
|
|
|
2017-03-19 12:54:35 -04:00
|
|
|
private IOSafeTerminal createIOSafeTerminal() throws IOException {
|
2017-03-21 12:07:22 -04:00
|
|
|
Terminal defaultTerminal = new DefaultTerminalFactory().createTerminal();
|
2016-12-17 10:19:18 -05:00
|
|
|
|
2017-03-21 12:07:22 -04:00
|
|
|
return IOSafeTerminalAdapter.createRuntimeExceptionConvertingAdapter(defaultTerminal);
|
2017-03-18 16:49:46 -04:00
|
|
|
}
|
2017-03-17 16:07:25 -04:00
|
|
|
|
2017-03-21 15:03:16 -04:00
|
|
|
private void printGreeting() {
|
|
|
|
outputStream.println(GREETING);
|
|
|
|
outputStream.println();
|
2016-12-17 10:19:18 -05:00
|
|
|
}
|
|
|
|
|
2017-03-21 12:07:22 -04:00
|
|
|
private LispInterpreter buildInteractiveInterpreter() {
|
2017-02-06 13:39:05 -05:00
|
|
|
LispInterpreterBuilder builder = LispInterpreterBuilderImpl.getInstance();
|
2017-03-21 12:07:22 -04:00
|
|
|
builder.setInput(inputReader, "terminal");
|
|
|
|
builder.setOutput(outputStream);
|
|
|
|
builder.setErrorOutput(outputStream);
|
|
|
|
builder.setTerminationFunction(this::shutdown);
|
|
|
|
builder.setErrorTerminationFunction(this::shutdown);
|
2017-03-22 11:26:53 -04:00
|
|
|
builder.setPromptDecorator(s -> s + END_OF_SEGMENT);
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-03-21 12:07:22 -04:00
|
|
|
private void shutdown() {
|
2017-03-21 15:03:16 -04:00
|
|
|
outputStream.println();
|
|
|
|
outputStream.println(END_OF_SEGMENT);
|
2017-03-21 12:07:22 -04:00
|
|
|
lispTerminal.stop();
|
|
|
|
safeOutputWriter.close();
|
2017-03-18 16:49:46 -04:00
|
|
|
}
|
|
|
|
|
2017-03-21 12:07:22 -04:00
|
|
|
private void runWithFile(String fileName) {
|
|
|
|
buildFileInterpreter(fileName).interpret();
|
2016-12-17 10:19:18 -05:00
|
|
|
}
|
|
|
|
|
2017-03-21 12:07:22 -04:00
|
|
|
private LispInterpreter buildFileInterpreter(String fileName) {
|
|
|
|
LispInterpreterBuilder builder = LispInterpreterBuilderImpl.getInstance();
|
|
|
|
builder.useFile(fileName);
|
|
|
|
builder.setOutput(System.out);
|
|
|
|
builder.setErrorOutput(System.err);
|
|
|
|
builder.setTerminationFunction(() -> System.exit(0));
|
|
|
|
builder.setErrorTerminationFunction(() -> System.exit(1));
|
|
|
|
builder.setValueOutputDecorator(makeColorDecorator(ANSI_GREEN));
|
|
|
|
builder.setWarningOutputDecorator(makeColorDecorator(ANSI_YELLOW));
|
|
|
|
builder.setErrorOutputDecorator(makeColorDecorator(ANSI_RED));
|
|
|
|
builder.setCriticalOutputDecorator(makeColorDecorator(ANSI_PURPLE));
|
2017-02-11 13:33:34 -05:00
|
|
|
|
2017-03-21 12:07:22 -04:00
|
|
|
return builder.build();
|
2017-02-11 13:33:34 -05:00
|
|
|
}
|
|
|
|
|
2017-03-21 12:07:22 -04:00
|
|
|
private static Function<String, String> makeColorDecorator(String color) {
|
2017-03-18 16:49:46 -04:00
|
|
|
return new Function<String, String>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String apply(String s) {
|
2017-03-21 12:07:22 -04:00
|
|
|
return color + s + ANSI_RESET;
|
2017-03-18 16:49:46 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-12-17 10:19:18 -05:00
|
|
|
}
|