diff --git a/src/main/java/serene/simple/Main.java b/src/main/java/serene/simple/Main.java index bfbcfff..ab608ee 100644 --- a/src/main/java/serene/simple/Main.java +++ b/src/main/java/serene/simple/Main.java @@ -1,6 +1,3 @@ -/* - * This Java source file was generated by the Gradle 'init' task. - */ package serene.simple; import java.io.IOException; @@ -18,7 +15,7 @@ public class Main { } private static void startRepl() { - Environment rootEnv = Environment.getBaseEnvironment(); + Scope rootScope = Scope.getRootScope(); Console console = System.console(); @@ -29,18 +26,27 @@ public class Main { ByteArrayInputStream inputStream = new ByteArrayInputStream(inputData.getBytes()); ListNode nodes = Reader.read(inputStream); - ... + + Object result = ListNode.EMPTY; + + for (Node n : nodes) { + result = n.eval(rootScope); + } + + if (result != ListNode.EMPTY) { + System.out.println(result); + } } } private static void runSerene(String filePath) throws IOException { - Environment rootEnv = Environment.getBaseEnvironment(); + Scope rootScope = Scope.getRootScope(); ListNode nodes = Reader.read(new FileInputStream(filePath)); for (Node n : nodes) { - n.eval(rootEnv); + n.eval(rootScope); } } } diff --git a/src/main/java/serene/simple/Reader.java b/src/main/java/serene/simple/Reader.java new file mode 100644 index 0000000..aa21514 --- /dev/null +++ b/src/main/java/serene/simple/Reader.java @@ -0,0 +1,25 @@ +package serene.simple; + +import java.io.IOException; +import java.io.PushbackReader; + + +public class Reader { + public static Node readNode(PushbackReader inputStream) throws IOException { + char c = (char) inputStream.read(); + inputStream.unread(c); + + if (c == "(") { + return readList(inputStream); + } + else if (Character.isDigit(c)) { + return readNumber(inputStream); + } + else if (c == ")") { + throw new IllegalArgumentException("Unmatch paranthesis.") + } + else { + return readSymbol(inputStream); + } + } +}