35 lines
767 B
Java
35 lines
767 B
Java
/*
|
|
* Name: Mike Cifelli
|
|
* Course: CIS 443 - Programming Languages
|
|
* Assignment: Lisp Interpreter 1
|
|
*/
|
|
|
|
package eval;
|
|
|
|
import parser.*;
|
|
|
|
/**
|
|
* <code>PLUS</code> represents the '+' function in Lisp.
|
|
*/
|
|
public class PLUS extends LispFunction {
|
|
|
|
public LispNumber call(Cons argList) {
|
|
if (argList.nullp()) {
|
|
return new LispNumber(0);
|
|
}
|
|
|
|
SExpression argFirst = argList.getCar();
|
|
Cons argRest = (Cons) argList.getCdr();
|
|
|
|
if (argFirst.numberp()) {
|
|
LispNumber num1 = (LispNumber) argFirst;
|
|
LispNumber num2 = call(argRest);
|
|
|
|
return new LispNumber(num1.getValue() + num2.getValue());
|
|
}
|
|
|
|
throw new RuntimeException("+: " + argFirst + " is not a number");
|
|
}
|
|
|
|
}
|