transcendental-lisp/eval/EQ.java

46 lines
1.3 KiB
Java
Raw Normal View History

2016-12-07 14:16:45 -05:00
/*
* Name: Mike Cifelli
* Course: CIS 443 - Programming Languages
* Assignment: Lisp Interpreter 1
*/
package eval;
import parser.*;
/**
* <code>EQ</code> represents the EQ function in Lisp.
*/
public class EQ extends LispFunction {
// The number of arguments that EQ takes.
private static final int NUM_ARGS = 2;
public SExpression call(Cons argList) {
// retrieve the number of arguments passed to EQ
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("EQ"), argList);
String errMsg = "too " +
((argListLength > NUM_ARGS) ? "many" : "few") +
" arguments given to EQ: " + originalSExpr;
throw new RuntimeException(errMsg);
}
SExpression argOne = argList.getCar(); // first argument
Cons cdr = (Cons) argList.getCdr();
SExpression argTwo = cdr.getCar(); // second argumnet
if (argOne.atomp() && argTwo.atomp()) {
return ((argOne.toString().equals(argTwo.toString()))
? Symbol.T : Nil.getUniqueInstance());
}
return ((argOne == argTwo) ? Symbol.T : Nil.getUniqueInstance());
}
}