serene-simple/src/main/java/serene/simple/Main.java

77 lines
1.8 KiB
Java

package serene.simple;
import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.Console;
import java.io.FileInputStream;
public class Main {
private static void isJava8() throws RuntimeException {
if (System.getProperty("java.vm.specification.version").equals("1.8"))
throw new RuntimeException("Minimume required version of JDK is 1.8");
}
public static void main(String[] args) throws IOException, RuntimeException {
Main.isJava8();
if (args.length == 0) {
startRepl();
return;
}
runSerene(args[0]);
}
private static void startRepl() throws IOException {
BaseScope rootScope = new RootScope();
Console console = System.console();
System.out.println("Serene 'simple' v0.1.0");
while(true) {
String inputData = console.readLine("serene-> ");
//String inputData = "(fn (x) x)";
if (inputData == null) break;
ByteArrayInputStream inputStream = new ByteArrayInputStream(inputData.getBytes());
try {
ListNode<Node> nodes = Reader.read(inputStream);
Object result = ListNode.EMPTY;
for (Node n : nodes) {
result = n.eval(rootScope);
}
if (result != ListNode.EMPTY) {
System.out.print(";; ");
if (result == null) {
System.out.println("nil");
}
else {
System.out.println(result.toString());
}
}
}
catch(Exception e) {
System.out.println("Error: ");
e.printStackTrace(System.out);
}
}
}
private static void runSerene(String filePath) throws IOException {
BaseScope rootScope = new RootScope();
ListNode<Node> nodes = Reader.read(new FileInputStream(filePath));
for (Node n : nodes) {
n.eval(rootScope);
}
}
}