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

36 lines
1.0 KiB
Java

package function.builtin;
import static function.builtin.EVAL.lookupFunctionOrLambda;
import static table.FunctionTable.lookupFunction;
import function.*;
import sexpression.*;
@FunctionNames({ "APPLY" })
public class APPLY extends LispFunction {
private ArgumentValidator argumentValidator;
public static SExpression apply(Cons argumentList) {
return lookupFunction("APPLY").call(argumentList);
}
public APPLY() {
this.argumentValidator = new ArgumentValidator("APPLY");
this.argumentValidator.setExactNumberOfArguments(2);
this.argumentValidator.setTrailingArgumentExpectedType(Cons.class);
}
public SExpression call(Cons argumentList) {
argumentValidator.validate(argumentList);
Cons rest = (Cons) argumentList.getRest();
Cons functionArguments = (Cons) rest.getFirst();
SExpression functionName = argumentList.getFirst();
LispFunction function = lookupFunctionOrLambda(functionName);
return function.call(functionArguments);
}
}