transcendental-lisp/src/function/builtin/COND.java

58 lines
1.7 KiB
Java
Raw Normal View History

2016-12-19 13:05:53 -05:00
package function.builtin;
2016-12-07 16:38:26 -05:00
2016-12-19 13:05:53 -05:00
import function.LispFunction;
import sexpression.*;
2016-12-07 16:38:26 -05:00
public class COND extends LispFunction {
public SExpression call(Cons argList) {
if (argList.nullp()) {
// return NIL if there are were no arguments passed to COND
return Nil.getUniqueInstance();
}
SExpression argCar = argList.getCar(); // first clause
Cons argCdr = (Cons) argList.getCdr(); // list of remaining clauses
2016-12-07 16:38:26 -05:00
// make sure the first clause is a list and is not NIL
if (argCar.consp()) {
Cons clause = (Cons) argCar;
SExpression test = EVAL.eval(clause.getCar());
if (test != Nil.getUniqueInstance()) {
// the car of this clause is true, so we evaluate its cdr
SExpression cdr = clause.getCdr();
SExpression retval = test;
// evaluate all the S-expressions in the cdr of the clause
while (cdr.consp()) {
retval = EVAL.eval(((Cons) cdr).getCar());
cdr = ((Cons) cdr).getCdr();
}
// return the value of the last S-expression evaluated
return retval;
}
// the car of this clause is false, so we test any remaining
// clauses
// check if the list of remaining clauses is a list and is not NIL
if (argCdr.consp()) {
return call(argCdr);
}
// there are no remaining clauses, so we return NIL
return Nil.getUniqueInstance();
}
throw new RuntimeException("COND: clause " + argCar + " should be a list");
2016-12-07 16:38:26 -05:00
}
public boolean evaluateArguments() {
return false;
}
}