package function.builtin.math;

import function.*;
import sexpression.*;

@FunctionNames({ "+" })
public class PLUS extends LispFunction {

    private ArgumentValidator argumentValidator;
    private MathFunction mathFunction;

    public PLUS() {
        this.argumentValidator = new ArgumentValidator("+");
        this.argumentValidator.setEveryArgumentExpectedType(LispNumber.class);
        this.mathFunction = new MathFunction(number -> number, this::add);
    }

    public LispNumber call(Cons argumentList) {
        argumentValidator.validate(argumentList);

        return mathFunction.callTailRecursive(new Cons(LispNumber.ZERO, argumentList));
    }

    private LispNumber add(LispNumber number1, LispNumber number2) {
        return new LispNumber(number1.getValue().add(number2.getValue()));
    }

}
