package sexpression;

@DisplayName("list")
public class Cons extends SExpression {

    private SExpression first;
    private SExpression rest;

    public Cons(SExpression first, SExpression rest) {
        this.first = first;
        this.rest = rest;
    }

    public SExpression getFirst() {
        return first;
    }

    public SExpression getRest() {
        return rest;
    }

    public void setFirst(SExpression first) {
        this.first = first;
    }

    public void setRest(SExpression rest) {
        this.rest = rest;
    }

    @Override
    public boolean isCons() {
        return true;
    }

    @Override
    public String toString() {
        return ("(" + toStringAux());
    }

    private String toStringAux() {
        if (rest.isNull())
            return (first.toString() + ")");
        else if (rest.isCons())
            return (first.toString() + " " + ((Cons) rest).toStringAux());

        return (first.toString() + " . " + rest.toString() + ")");
    }

}
