transcendental-lisp/eval/EQUALSP.java

56 lines
1.5 KiB
Java

/*
* Name: Mike Cifelli
* Course: CIS 443 - Programming Languages
* Assignment: Lisp Interpreter 1
*/
package eval;
import parser.*;
/**
* <code>EQUALSP</code> represents the '=' function in Lisp.
*/
public class EQUALSP extends LispFunction {
public SExpression call(Cons argList) {
// make sure we have received at least one argument
if (argList.nullp()) {
Cons originalSExpr = new Cons(new Symbol("="), argList);
throw new RuntimeException("too few arguments given to =: " +
originalSExpr);
}
SExpression firstArg = argList.getCar();
Cons argRest = (Cons) argList.getCdr();
// make sure that the first argument is a number
if (firstArg.numberp()) {
LispNumber num1 = (LispNumber) firstArg;
if (argRest.nullp()) {
return Symbol.T;
}
SExpression secondArg = argRest.getCar();
// make sure that the second argument is a number as well
if (secondArg.numberp()) {
LispNumber num2 = (LispNumber) secondArg;
if (num1.getValue() == num2.getValue()) {
return call(argRest);
}
return Nil.getUniqueInstance();
}
throw new RuntimeException("=: " + secondArg + " is not a number");
}
throw new RuntimeException("=: " + firstArg + " is not a number");
}
}