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

64 lines
1.7 KiB
Java
Raw Normal View History

package serene.simple;
import java.io.IOException;
2019-12-14 02:42:25 +00:00
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();
2019-12-14 02:42:25 +00:00
return;
}
runSerene(args[0]);
}
2019-12-14 02:42:25 +00:00
private static void startRepl() throws IOException {
2019-12-10 17:28:58 +00:00
Scope rootScope = Scope.getRootScope();
2019-12-14 22:05:53 +00:00
Console console = System.console();
2019-12-14 02:42:25 +00:00
System.out.println("Serene 'simple' v0.1.0");
while(true) {
String inputData = console.readLine("serene-> ");
2019-12-14 02:42:25 +00:00
if (inputData == null) break;
ByteArrayInputStream inputStream = new ByteArrayInputStream(inputData.getBytes());
ListNode<Node> nodes = Reader.read(inputStream);
2019-12-10 17:28:58 +00:00
try {
Object result = ListNode.EMPTY;
for (Node n : nodes) {
result = n.eval(rootScope);
}
2019-12-10 17:28:58 +00:00
if (result != ListNode.EMPTY) {
System.out.print(";; ");
System.out.println(result);
}
2019-12-10 17:28:58 +00:00
}
catch(Exception e) {
System.out.println("Error: ");
e.printStackTrace(System.out);
2019-12-10 17:28:58 +00:00
}
}
}
private static void runSerene(String filePath) throws IOException {
2019-12-10 17:28:58 +00:00
Scope rootScope = Scope.getRootScope();
ListNode<Node> nodes = Reader.read(new FileInputStream(filePath));
for (Node n : nodes) {
2019-12-10 17:28:58 +00:00
n.eval(rootScope);
}
}
}