2016-12-19 13:05:53 -05:00
|
|
|
package function.builtin;
|
2016-12-07 16:38:26 -05:00
|
|
|
|
2016-12-19 13:05:53 -05:00
|
|
|
import function.*;
|
2016-12-15 15:33:48 -05:00
|
|
|
import sexpression.*;
|
2016-12-07 16:38:26 -05:00
|
|
|
|
|
|
|
public class APPLY extends LispFunction {
|
|
|
|
|
2016-12-22 10:32:48 -05:00
|
|
|
private ArgumentValidator argumentValidator;
|
|
|
|
|
2016-12-07 16:38:26 -05:00
|
|
|
public static SExpression apply(Cons argList) {
|
|
|
|
return new APPLY().call(argList);
|
|
|
|
}
|
|
|
|
|
2016-12-22 10:32:48 -05:00
|
|
|
public APPLY() {
|
|
|
|
this.argumentValidator = new ArgumentValidator("APPLY");
|
2016-12-22 16:55:25 -05:00
|
|
|
this.argumentValidator.setExactNumberOfArguments(2);
|
|
|
|
this.argumentValidator.setTrailingArgumentExpectedType(Cons.class);
|
2016-12-22 10:32:48 -05:00
|
|
|
}
|
2016-12-07 16:38:26 -05:00
|
|
|
|
2016-12-23 10:53:11 -05:00
|
|
|
public SExpression call(Cons argumentList) {
|
|
|
|
argumentValidator.validate(argumentList);
|
2016-12-07 16:38:26 -05:00
|
|
|
|
2016-12-23 10:53:11 -05:00
|
|
|
Cons cdr = (Cons) argumentList.getCdr();
|
|
|
|
SExpression functionName = argumentList.getCar();
|
|
|
|
SExpression functionArguments = cdr.getCar();
|
2016-12-22 16:55:25 -05:00
|
|
|
LispFunction function = EVAL.lookupFunctionOrLambda(functionName);
|
2016-12-07 16:38:26 -05:00
|
|
|
|
2016-12-23 10:53:11 -05:00
|
|
|
return function.call((Cons) functionArguments);
|
2016-12-07 16:38:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|