transcendental-lisp/src/function/builtin/special/LAMBDA.java

66 lines
2.0 KiB
Java

package function.builtin.special;
import function.*;
import function.builtin.cons.LIST;
import sexpression.*;
public class LAMBDA extends LispFunction {
public static boolean isLambdaExpression(SExpression sexpr) {
if (sexpr.consp()) {
SExpression first = ((Cons) sexpr).getCar();
return "LAMBDA".equals(first.toString());
}
return false;
}
public static UserDefinedFunction createFunction(Cons lambdaExpression) {
SExpression cdr = lambdaExpression.getCdr();
ArgumentValidator lambdaValidator = new ArgumentValidator("LAMBDA|create|");
lambdaValidator.setEveryArgumentExpectedType(Cons.class);
lambdaValidator.validate(LIST.makeList(cdr));
LambdaExpression lambda = new LAMBDA().call((Cons) cdr);
return lambda.getFunction();
}
private ArgumentValidator argumentValidator;
private ArgumentValidator lambdaListValidator;
public LAMBDA() {
this.argumentValidator = new ArgumentValidator("LAMBDA");
this.argumentValidator.setFirstArgumentExpectedType(Cons.class);
this.argumentValidator.setMinimumNumberOfArguments(1);
this.lambdaListValidator = new ArgumentValidator("LAMBDA|lambda-list|");
this.lambdaListValidator.setEveryArgumentExpectedType(Symbol.class);
}
public LambdaExpression call(Cons argumentList) {
argumentValidator.validate(argumentList);
SExpression car = argumentList.getCar();
Cons lambdaList = (Cons) car;
Cons body = (Cons) argumentList.getCdr();
lambdaListValidator.validate(lambdaList);
UserDefinedFunction function = new UserDefinedFunction(":LAMBDA", lambdaList, body);
return new LambdaExpression(makeOriginalLambdaExpression(argumentList), function);
}
private Cons makeOriginalLambdaExpression(Cons argumentList) {
return new Cons(new Symbol("LAMBDA"), argumentList);
}
public boolean evaluateArguments() {
return false;
}
}