Reader class has been added
This commit is contained in:
parent
53cd9560a3
commit
7f06e14ec9
|
@ -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<Node> 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<Node> nodes = Reader.read(new FileInputStream(filePath));
|
||||
|
||||
for (Node n : nodes) {
|
||||
n.eval(rootEnv);
|
||||
n.eval(rootScope);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue