diff --git a/Makefile b/Makefile index 43cc887..e0767e2 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,2 @@ repl: - gradle compileJava && rlwrap java -cp bin/main/ serene.simple.Main + gradle compileJava && rlwrap java -cp build/classes/java/main/ serene.simple.Main diff --git a/src/main/java/serene/simple/FnSpecialForm.java b/src/main/java/serene/simple/FnSpecialForm.java index 3c635df..8a63e29 100644 --- a/src/main/java/serene/simple/FnSpecialForm.java +++ b/src/main/java/serene/simple/FnSpecialForm.java @@ -14,12 +14,9 @@ public class FnSpecialForm extends SpecialForm { return new Function() { @Override - public Object apply(Object arg) { - Object args[] = {arg}; - return this.apply(args); - } + public Object apply(Object arguments) { + Object[] args = (Object[]) arguments; - public Object apply(Object... args) { Scope scope = new Scope(parentScope); if (args.length != formalParams.length) { throw new RuntimeException(String.format("Wrong number of arguments. Expected: %s, Got: %s.", @@ -39,6 +36,7 @@ public class FnSpecialForm extends SpecialForm { for (Node node : body) { output = node.eval(scope); } + return output; } }; diff --git a/src/main/java/serene/simple/ListNode.java b/src/main/java/serene/simple/ListNode.java index d56e45f..dc680cc 100644 --- a/src/main/java/serene/simple/ListNode.java +++ b/src/main/java/serene/simple/ListNode.java @@ -44,16 +44,16 @@ public class ListNode extends Node implements Iterable { @Override public Object eval(Scope scope) { - Function f = (Function) this.first().eval(scope); + Function f = (Function) this.first().eval(scope); List args = new ArrayList(); - for (T node : this.rest) { + for (T node : this.rest()) { args.add(node.eval(scope)); } return f.apply(args.toArray()); } - public T first() { + public T first() { if (this != EMPTY) { return this.first; } diff --git a/src/main/java/serene/simple/Main.java b/src/main/java/serene/simple/Main.java index 5725208..6b62aff 100644 --- a/src/main/java/serene/simple/Main.java +++ b/src/main/java/serene/simple/Main.java @@ -26,6 +26,7 @@ public class Main { System.out.println("Serene 'simple' v0.1.0"); while(true) { String inputData = console.readLine("serene-> "); + //String inputData = "(fn (x) x)"; if (inputData == null) break; @@ -34,6 +35,7 @@ public class Main { try { Object result = ListNode.EMPTY; + for (Node n : nodes) { result = n.eval(rootScope); } diff --git a/src/main/java/serene/simple/Reader.java b/src/main/java/serene/simple/Reader.java index f2b9908..b4147ef 100644 --- a/src/main/java/serene/simple/Reader.java +++ b/src/main/java/serene/simple/Reader.java @@ -76,7 +76,7 @@ public class Reader { while (true) { int ch = inputStream.read(); - if(isWhiteSpace(ch) || ch == -1) { + if(isWhiteSpace(ch) || isClosingChar(ch) || ch == -1) { inputStream.unread(ch); break; }; @@ -100,16 +100,20 @@ public class Reader { } private static void skipWhiteSpaces(PushbackReader inputStream) throws IOException { - int ch = inputStream.read(); - while (isWhiteSpace(ch)) { - ch = inputStream.read(); - } - inputStream.unread(ch); + int ch = inputStream.read(); + + while (isWhiteSpace(ch)) { + ch = inputStream.read(); + } + inputStream.unread(ch); } private static boolean isWhiteSpace(int ch) { - return (ch == ' ' || ch == '\t' || - ch == '\f' || ch == '\r' || - ch == '\n'); + return (ch == ' ' || ch == '\t' || ch == '\f' || ch == '\r' || ch == '\n'); + } + + private static boolean isClosingChar(int ch) { + return (ch == ')' || ch == ']' || + ch == '}'); } }