package function.builtin; import function.LispFunction; import sexpression.*; /** * CAR represents the CAR function in Lisp. */ public class CAR extends LispFunction { // The number of arguments that CAR takes. private static final int NUM_ARGS = 1; public SExpression call(Cons argList) { // retrieve the number of arguments passed to CAR int argListLength = LENGTH.getLength(argList); // make sure we have received the proper number of arguments if (argListLength != NUM_ARGS) { Cons originalSExpr = new Cons(new Symbol("CAR"), argList); String errMsg = "too " + ((argListLength > NUM_ARGS) ? "many" : "few") + " arguments given to CAR: " + originalSExpr; throw new RuntimeException(errMsg); } SExpression argCar = argList.getCar(); // make sure that the argument is a list if (argCar.listp()) { Cons arg = (Cons) argCar; return arg.getCar(); } // the argument is not a list throw new RuntimeException("CAR: " + argCar + " is not a list"); } }