2019-12-12 00:17:41 +00:00
|
|
|
package serene.simple;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2019-12-14 02:42:25 +00:00
|
|
|
import java.util.Iterator;
|
2019-12-12 00:17:41 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.function.Function;
|
2019-12-14 02:42:25 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
import serene.simple.builtin.AFn;
|
2019-12-14 02:42:25 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
import static java.util.Arrays.asList;
|
2019-12-14 02:42:25 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
public class ListNode<T extends Node> extends Node implements Iterable<T> {
|
|
|
|
public static final ListNode<?> EMPTY = new ListNode<>();
|
|
|
|
|
|
|
|
public final T first;
|
|
|
|
public final ListNode<T> rest;
|
|
|
|
public final int length;
|
|
|
|
|
|
|
|
public ListNode() {
|
|
|
|
this.first = null;
|
|
|
|
this.rest = null;
|
|
|
|
this.length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ListNode(T f, ListNode<T> r) {
|
|
|
|
this.first = f;
|
|
|
|
this.rest = r;
|
|
|
|
this.length = r.length + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SafeVarargs
|
|
|
|
public static <T extends Node> ListNode<T> list(T... objs) {
|
|
|
|
return list(asList(objs));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T extends Node> ListNode<T> list(List<T> objs) {
|
|
|
|
ListNode<T> l = (ListNode<T>) EMPTY;
|
|
|
|
for (int i = objs.size() - 1; i >= 0; i--) {
|
|
|
|
l = l.cons(objs.get(i));
|
2019-12-14 02:42:25 +00:00
|
|
|
}
|
2019-12-19 22:57:04 +00:00
|
|
|
return l;
|
|
|
|
}
|
2019-12-14 02:42:25 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
public ListNode<T> cons(T node) {
|
|
|
|
return new ListNode<T>(node, this);
|
|
|
|
}
|
2019-12-14 02:42:25 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
@Override
|
|
|
|
public Object eval(BaseScope scope) {
|
|
|
|
Object f = this.first().eval(scope);
|
|
|
|
List<Object> args = new ArrayList<Object>();
|
2019-12-14 02:42:25 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
for (T node : this.rest()) {
|
|
|
|
args.add(node.eval(scope));
|
2019-12-14 02:42:25 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
if (f instanceof AFn) {
|
|
|
|
return evalBuiltin(scope, (AFn) f, args);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return evalFn(scope, (Function<Object, Object>) f, args);
|
2019-12-14 02:42:25 +00:00
|
|
|
}
|
2019-12-19 22:57:04 +00:00
|
|
|
}
|
2019-12-12 00:17:41 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
public Object evalBuiltin(BaseScope scope, AFn fn, List<Object> args) {
|
|
|
|
fn.setArguments(args);
|
|
|
|
return fn.eval(scope);
|
|
|
|
}
|
2019-12-12 00:17:41 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
public Object evalFn(BaseScope scope, Function<Object, Object> fn, List<Object> args) {
|
|
|
|
return fn.apply(args.toArray());
|
|
|
|
}
|
2019-12-14 02:42:25 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
public T first() {
|
|
|
|
if (this != EMPTY) {
|
|
|
|
return this.first;
|
2019-12-14 02:42:25 +00:00
|
|
|
}
|
2019-12-19 22:57:04 +00:00
|
|
|
return null;
|
|
|
|
}
|
2019-12-14 02:42:25 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
public ListNode<T> rest() {
|
|
|
|
if (this != EMPTY) {
|
|
|
|
return this.rest;
|
2019-12-14 02:42:25 +00:00
|
|
|
}
|
2019-12-19 22:57:04 +00:00
|
|
|
return (ListNode<T>) EMPTY;
|
|
|
|
}
|
2019-12-14 02:42:25 +00:00
|
|
|
|
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
@Override
|
|
|
|
public Iterator<T> iterator() {
|
|
|
|
return new Iterator<T>() {
|
|
|
|
private ListNode<T> l = ListNode.this;
|
2019-12-18 17:45:43 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
@Override
|
|
|
|
public boolean hasNext() {
|
|
|
|
return this.l != EMPTY;
|
|
|
|
}
|
2019-12-18 17:45:43 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
@Override
|
|
|
|
public T next() {
|
|
|
|
if (this.l == EMPTY) {
|
|
|
|
return null;
|
2019-12-18 17:45:43 +00:00
|
|
|
}
|
2019-12-19 22:57:04 +00:00
|
|
|
T first = this.l.first;
|
|
|
|
this.l = this.l.rest;
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void remove() {
|
|
|
|
throw new SereneException("Iterator is immutable");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public String toString() {
|
|
|
|
if (this.length == 0) {
|
|
|
|
return "()";
|
|
|
|
}
|
2019-12-18 17:45:43 +00:00
|
|
|
|
2019-12-19 22:57:04 +00:00
|
|
|
String output = "(" + this.first();
|
|
|
|
for(Node x : this.rest()) {
|
|
|
|
output = output + " " + x.toString();
|
2019-12-18 17:45:43 +00:00
|
|
|
}
|
2019-12-19 22:57:04 +00:00
|
|
|
|
|
|
|
return output + ")";
|
|
|
|
}
|
2019-12-12 00:17:41 +00:00
|
|
|
}
|