47 lines
1.2 KiB
Java
47 lines
1.2 KiB
Java
|
/*
|
||
|
* Name: Mike Cifelli
|
||
|
* Course: CIS 443 - Programming Languages
|
||
|
* Assignment: Lisp Interpreter 1
|
||
|
*/
|
||
|
|
||
|
package eval;
|
||
|
|
||
|
import parser.*;
|
||
|
|
||
|
/**
|
||
|
* <code>CAR</code> 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");
|
||
|
}
|
||
|
|
||
|
}
|