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

67 lines
1.7 KiB
Java

package serene.simple;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.Console;
import java.io.FileInputStream;
public class Main {
public static void main(String[] args) throws IOException {
if (args.length == 0) {
startRepl();
return;
}
runSerene(args[0]);
}
private static void startRepl() throws IOException {
Scope rootScope = Scope.getRootScope();
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(";; ");
System.out.println(result.toString());
}
}
catch(Exception e) {
System.out.println("Error: ");
e.printStackTrace(System.out);
}
}
}
private static void runSerene(String filePath) throws IOException {
Scope rootScope = Scope.getRootScope();
ListNode<Node> nodes = Reader.read(new FileInputStream(filePath));
for (Node n : nodes) {
n.eval(rootScope);
}
}
}