All the initial special forms has been done

This commit is contained in:
Sameer Rahmani 2019-12-18 17:45:43 +00:00
parent 3e35e1fe8a
commit e3b6a054b4
13 changed files with 71 additions and 14 deletions

View File

@ -7,4 +7,9 @@ public class FalseNode extends Node {
public Object eval(Scope scope) {
return false;
}
@Override
public String toString() {
return "false";
}
}

View File

@ -39,6 +39,11 @@ public class FnSpecialForm extends SpecialForm {
return output;
}
@Override
public String toString() {
return "Function@" + System.identityHashCode(this);
}
};
}
}

View File

@ -5,4 +5,9 @@ public class FunctionNode extends Node {
public Object eval(Scope scope) {
return this;
}
@Override
public String toString() {
return "Function@" + this;
}
}

View File

@ -94,4 +94,17 @@ public class ListNode<T extends Node> extends Node implements Iterable<T> {
}
};
}
public String toString() {
if (this.length == 0) {
return "()";
}
String output = "(" + this.first();
for(Node x : this.rest()) {
output = output + " " + x.toString();
}
return output + ")";
}
}

View File

@ -31,9 +31,10 @@ public class Main {
if (inputData == null) break;
ByteArrayInputStream inputStream = new ByteArrayInputStream(inputData.getBytes());
ListNode<Node> nodes = Reader.read(inputStream);
try {
ListNode<Node> nodes = Reader.read(inputStream);
Object result = ListNode.EMPTY;
for (Node n : nodes) {
@ -42,7 +43,7 @@ public class Main {
if (result != ListNode.EMPTY) {
System.out.print(";; ");
System.out.println(result);
System.out.println(result.toString());
}
}
catch(Exception e) {

View File

@ -7,4 +7,10 @@ public class NilNode extends Node {
public Object eval(Scope scope) {
return this;
}
@Override
public String toString() {
return "nil";
}
}

View File

@ -9,6 +9,7 @@ public abstract class Node {
return this == n;
}
@Override
public String toString() {
String className = this.getClass().getName();
return String.format("%s<%s>", className, System.identityHashCode(this));

View File

@ -11,4 +11,9 @@ public class NumberNode extends Node {
public Object eval(Scope scope) {
return this.value;
}
}
@Override
public String toString() {
return String.format("%s", this.value);
}
}

View File

@ -1,13 +1,21 @@
package serene.simple;
public class QuoteSpecialForm extends SpecialForm {
public QuoteSpecialForm(ListNode node) {
// public QuoteSpecialForm(Node node) {
// this.x = node;
// }
public QuoteSpecialForm(ListNode<Node> node) throws SereneException {
super(node);
if (this.node.rest().length != 1) {
throw new SereneException("quote expects only one arguement");
}
}
@Override
public Object eval(final Scope scope) {
return this.node;
return this.node.rest().first();
}
}

View File

@ -10,7 +10,7 @@ import java.util.List;
public class Reader {
public static Node readNode(PushbackReader inputStream) throws IOException {
public static Node readNode(PushbackReader inputStream) throws IOException, IllegalArgumentException, SereneException {
char c = (char) inputStream.read();
inputStream.unread(c);
@ -28,11 +28,11 @@ public class Reader {
}
}
public static ListNode<Node> read(InputStream inputStream) throws IOException {
public static ListNode<Node> read(InputStream inputStream) throws IOException, IllegalArgumentException, SereneException {
return read(new PushbackReader(new InputStreamReader(inputStream)));
}
public static ListNode<Node> read(PushbackReader inputStream) throws IOException {
public static ListNode<Node> read(PushbackReader inputStream) throws IOException, IllegalArgumentException, SereneException {
List<Node> nodes = new ArrayList<Node>();
skipWhiteSpaces(inputStream);
@ -47,7 +47,7 @@ public class Reader {
return ListNode.list(nodes);
}
private static Node readList(PushbackReader inputStream) throws IOException {
private static Node readList(PushbackReader inputStream) throws IOException, SereneException {
char opening = (char) inputStream.read();
assert opening == '(' : "Lists must start with a '('";

View File

@ -7,13 +7,15 @@ public class SpecialForm extends Node {
private static final SymbolNode IF = new SymbolNode("if");
private static final SymbolNode QUOTE = new SymbolNode("quote");
public final ListNode<Node> node;
public ListNode<Node> node;
public SpecialForm() {}
public SpecialForm(ListNode<Node> node) {
this.node = node;
}
public static Node check(ListNode<Node> l) {
public static Node check(ListNode<Node> l) throws SereneException {
if (l == ListNode.EMPTY) {
return l;
} else if (l.first().equals(DEF)) {
@ -30,7 +32,7 @@ public class SpecialForm extends Node {
}
@Override
public Object eval(Scope scope) {
public Object eval(Scope scope) throws SereneException {
throw new SereneException("Can't use SpecialForm directly");
}
}

View File

@ -26,7 +26,8 @@ public class SymbolNode extends Node {
return scope.lookupSymbol(this.name);
}
@Override
public String toString() {
return String.format("Symbol<%s>", this.name);
return this.name;
}
}

View File

@ -5,4 +5,9 @@ public class TrueNode extends Node {
public Object eval(Scope scope) {
return true;
}
@Override
public String toString() {
return "true";
}
}