All the initial special forms has been done
This commit is contained in:
parent
3e35e1fe8a
commit
e3b6a054b4
|
@ -7,4 +7,9 @@ public class FalseNode extends Node {
|
||||||
public Object eval(Scope scope) {
|
public Object eval(Scope scope) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "false";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,6 +39,11 @@ public class FnSpecialForm extends SpecialForm {
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Function@" + System.identityHashCode(this);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,4 +5,9 @@ public class FunctionNode extends Node {
|
||||||
public Object eval(Scope scope) {
|
public Object eval(Scope scope) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Function@" + this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 + ")";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,9 +31,10 @@ public class Main {
|
||||||
if (inputData == null) break;
|
if (inputData == null) break;
|
||||||
|
|
||||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(inputData.getBytes());
|
ByteArrayInputStream inputStream = new ByteArrayInputStream(inputData.getBytes());
|
||||||
ListNode<Node> nodes = Reader.read(inputStream);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
ListNode<Node> nodes = Reader.read(inputStream);
|
||||||
Object result = ListNode.EMPTY;
|
Object result = ListNode.EMPTY;
|
||||||
|
|
||||||
for (Node n : nodes) {
|
for (Node n : nodes) {
|
||||||
|
@ -42,7 +43,7 @@ public class Main {
|
||||||
|
|
||||||
if (result != ListNode.EMPTY) {
|
if (result != ListNode.EMPTY) {
|
||||||
System.out.print(";; ");
|
System.out.print(";; ");
|
||||||
System.out.println(result);
|
System.out.println(result.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch(Exception e) {
|
catch(Exception e) {
|
||||||
|
|
|
@ -7,4 +7,10 @@ public class NilNode extends Node {
|
||||||
public Object eval(Scope scope) {
|
public Object eval(Scope scope) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "nil";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ public abstract class Node {
|
||||||
return this == n;
|
return this == n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String className = this.getClass().getName();
|
String className = this.getClass().getName();
|
||||||
return String.format("%s<%s>", className, System.identityHashCode(this));
|
return String.format("%s<%s>", className, System.identityHashCode(this));
|
||||||
|
|
|
@ -11,4 +11,9 @@ public class NumberNode extends Node {
|
||||||
public Object eval(Scope scope) {
|
public Object eval(Scope scope) {
|
||||||
return this.value;
|
return this.value;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return String.format("%s", this.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,13 +1,21 @@
|
||||||
package serene.simple;
|
package serene.simple;
|
||||||
|
|
||||||
|
|
||||||
public class QuoteSpecialForm extends SpecialForm {
|
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);
|
super(node);
|
||||||
|
|
||||||
|
if (this.node.rest().length != 1) {
|
||||||
|
throw new SereneException("quote expects only one arguement");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object eval(final Scope scope) {
|
public Object eval(final Scope scope) {
|
||||||
return this.node;
|
return this.node.rest().first();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
public class Reader {
|
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();
|
char c = (char) inputStream.read();
|
||||||
inputStream.unread(c);
|
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)));
|
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>();
|
List<Node> nodes = new ArrayList<Node>();
|
||||||
skipWhiteSpaces(inputStream);
|
skipWhiteSpaces(inputStream);
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@ public class Reader {
|
||||||
return ListNode.list(nodes);
|
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();
|
char opening = (char) inputStream.read();
|
||||||
assert opening == '(' : "Lists must start with a '('";
|
assert opening == '(' : "Lists must start with a '('";
|
||||||
|
|
||||||
|
|
|
@ -7,13 +7,15 @@ public class SpecialForm extends Node {
|
||||||
private static final SymbolNode IF = new SymbolNode("if");
|
private static final SymbolNode IF = new SymbolNode("if");
|
||||||
private static final SymbolNode QUOTE = new SymbolNode("quote");
|
private static final SymbolNode QUOTE = new SymbolNode("quote");
|
||||||
|
|
||||||
public final ListNode<Node> node;
|
public ListNode<Node> node;
|
||||||
|
|
||||||
|
public SpecialForm() {}
|
||||||
|
|
||||||
public SpecialForm(ListNode<Node> node) {
|
public SpecialForm(ListNode<Node> node) {
|
||||||
this.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) {
|
if (l == ListNode.EMPTY) {
|
||||||
return l;
|
return l;
|
||||||
} else if (l.first().equals(DEF)) {
|
} else if (l.first().equals(DEF)) {
|
||||||
|
@ -30,7 +32,7 @@ public class SpecialForm extends Node {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object eval(Scope scope) {
|
public Object eval(Scope scope) throws SereneException {
|
||||||
throw new SereneException("Can't use SpecialForm directly");
|
throw new SereneException("Can't use SpecialForm directly");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,8 @@ public class SymbolNode extends Node {
|
||||||
return scope.lookupSymbol(this.name);
|
return scope.lookupSymbol(this.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("Symbol<%s>", this.name);
|
return this.name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,4 +5,9 @@ public class TrueNode extends Node {
|
||||||
public Object eval(Scope scope) {
|
public Object eval(Scope scope) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "true";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue