59 lines
1.9 KiB
Java
59 lines
1.9 KiB
Java
/*
|
|
* Name: Mike Cifelli
|
|
* Course: CIS 443 - Programming Languages
|
|
* Assignment: Lisp Interpreter 1
|
|
*/
|
|
|
|
package eval;
|
|
|
|
import parser.*;
|
|
|
|
/**
|
|
* <code>EQUAL</code> represents the EQUAL function in Lisp.
|
|
*/
|
|
public class EQUAL extends LispFunction {
|
|
|
|
// The number of arguments that EQUAL takes.
|
|
private static final int NUM_ARGS = 2;
|
|
|
|
public SExpression call(Cons argList) {
|
|
// retrieve the number of arguments passed to EQUAL
|
|
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("EQUAL"), argList);
|
|
String errMsg = "too " +
|
|
((argListLength > NUM_ARGS) ? "many" : "few") +
|
|
" arguments given to EQUAL: " + originalSExpr;
|
|
|
|
throw new RuntimeException(errMsg);
|
|
}
|
|
|
|
SExpression argOne = argList.getCar(); // first argument
|
|
Cons cdr = (Cons) argList.getCdr();
|
|
SExpression argTwo = cdr.getCar(); // second argumnet
|
|
|
|
if (argOne.consp() && argTwo.consp()) {
|
|
Cons listOne = (Cons) argOne;
|
|
Cons listTwo = (Cons) argTwo;
|
|
SExpression listOneCar = listOne.getCar();
|
|
SExpression listTwoCar = listTwo.getCar();
|
|
SExpression listOneCdr = listOne.getCdr();
|
|
SExpression listTwoCdr = listTwo.getCdr();
|
|
|
|
SExpression carEqual =
|
|
call(new Cons(listOneCar, LIST.makeList(listTwoCar)));
|
|
SExpression cdrEqual =
|
|
call(new Cons(listOneCdr, LIST.makeList(listTwoCdr)));
|
|
|
|
return (((carEqual == Symbol.T) && (cdrEqual == Symbol.T))
|
|
? Symbol.T : Nil.getUniqueInstance());
|
|
}
|
|
|
|
return ((argOne.toString().equals(argTwo.toString()))
|
|
? Symbol.T : Nil.getUniqueInstance());
|
|
}
|
|
|
|
}
|