transcendental-lisp/src/function/builtin/MULTIPLY.java

32 lines
766 B
Java
Raw Normal View History

2016-12-19 13:05:53 -05:00
package function.builtin;
2016-12-07 16:38:26 -05:00
import java.math.BigInteger;
2016-12-19 13:05:53 -05:00
import function.LispFunction;
import sexpression.*;
2016-12-07 16:38:26 -05:00
/**
* <code>MULTIPLY</code> represents the '*' function in Lisp.
*/
public class MULTIPLY extends LispFunction {
public LispNumber call(Cons argList) {
if (argList.nullp()) {
return new LispNumber(BigInteger.ONE);
2016-12-07 16:38:26 -05:00
}
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().multiply(num2.getValue()));
2016-12-07 16:38:26 -05:00
}
throw new RuntimeException("*: " + argFirst + " is not a number");
}
}