package function.builtin;

import static function.builtin.APPLY.apply;
import static function.builtin.cons.LIST.makeList;

import function.ArgumentValidator;
import function.FunctionNames;
import function.LispFunction;
import sexpression.Cons;
import sexpression.SExpression;

@FunctionNames({ "FUNCALL", "CALL" })
public class FUNCALL extends LispFunction {

    private ArgumentValidator argumentValidator;

    public FUNCALL(String name) {
        this.argumentValidator = new ArgumentValidator(name);
        this.argumentValidator.setMinimumNumberOfArguments(1);
    }

    @Override
    public SExpression call(Cons argumentList) {
        argumentValidator.validate(argumentList);
        Cons applyArgs = new Cons(argumentList.getFirst(), makeList(argumentList.getRest()));

        return apply(applyArgs);
    }

}
