64 lines
1.7 KiB
Java
64 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-> ");
|
|
|
|
if (inputData == null) break;
|
|
|
|
ByteArrayInputStream inputStream = new ByteArrayInputStream(inputData.getBytes());
|
|
ListNode<Node> nodes = Reader.read(inputStream);
|
|
|
|
try {
|
|
Object result = ListNode.EMPTY;
|
|
for (Node n : nodes) {
|
|
result = n.eval(rootScope);
|
|
}
|
|
|
|
if (result != ListNode.EMPTY) {
|
|
System.out.print(";; ");
|
|
System.out.println(result);
|
|
}
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
}
|