Use function alias in validation messages

Resolves #9
This commit is contained in:
Mike Cifelli 2017-03-07 10:59:51 -05:00
parent 368f22a02b
commit 5f10396af8
39 changed files with 85 additions and 90 deletions

View File

@ -15,8 +15,8 @@ public class APPLY extends LispFunction {
return lookupFunction("APPLY").call(argumentList);
}
public APPLY() {
this.argumentValidator = new ArgumentValidator("APPLY");
public APPLY(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setExactNumberOfArguments(2);
this.argumentValidator.setTrailingArgumentExpectedType(Cons.class);
}

View File

@ -53,8 +53,8 @@ public class EVAL extends LispFunction {
private ArgumentValidator argumentValidator;
public EVAL() {
this.argumentValidator = new ArgumentValidator("EVAL");
public EVAL(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setExactNumberOfArguments(1);
}

View File

@ -12,8 +12,8 @@ public class EXIT extends LispFunction {
private ArgumentValidator argumentValidator;
private RuntimeEnvironment environment;
public EXIT() {
this.argumentValidator = new ArgumentValidator("EXIT");
public EXIT(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setMaximumNumberOfArguments(0);
this.environment = RuntimeEnvironment.getInstance();
}

View File

@ -11,8 +11,8 @@ public class FUNCALL extends LispFunction {
private ArgumentValidator argumentValidator;
public FUNCALL() {
this.argumentValidator = new ArgumentValidator("FUNCALL");
public FUNCALL(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setMinimumNumberOfArguments(1);
}

View File

@ -18,16 +18,15 @@ import sexpression.*;
@FunctionNames({ "LOAD" })
public class LOAD extends LispFunction {
private static Stack<String> pathPrefixes = new Stack<>();
private ArgumentValidator argumentValidator;
private RuntimeEnvironment environment;
private Stack<String> pathPrefixes;
public LOAD() {
this.argumentValidator = new ArgumentValidator("LOAD");
public LOAD(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setExactNumberOfArguments(1);
this.argumentValidator.setEveryArgumentExpectedType(LispString.class);
this.environment = RuntimeEnvironment.getInstance();
this.pathPrefixes = new Stack<>();
}
public SExpression call(Cons argumentList) {

View File

@ -10,8 +10,8 @@ public class PRINT extends LispFunction {
private ArgumentValidator argumentValidator;
private RuntimeEnvironment environment;
public PRINT() {
this.argumentValidator = new ArgumentValidator("PRINT");
public PRINT(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setExactNumberOfArguments(1);
this.environment = RuntimeEnvironment.getInstance();
}

View File

@ -16,8 +16,8 @@ public class SET extends LispFunction {
private ArgumentValidator argumentValidator;
private ExecutionContext executionContext;
public SET() {
this.argumentValidator = new ArgumentValidator("SET");
public SET(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setExactNumberOfArguments(2);
this.argumentValidator.setFirstArgumentExpectedType(Symbol.class);
this.executionContext = ExecutionContext.getInstance();

View File

@ -13,8 +13,8 @@ public class SYMBOL_FUNCTION extends LispFunction {
private ArgumentValidator argumentValidator;
public SYMBOL_FUNCTION() {
this.argumentValidator = new ArgumentValidator("SYMBOL-FUNCTION");
public SYMBOL_FUNCTION(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setExactNumberOfArguments(1);
this.argumentValidator.setEveryArgumentExpectedType(Symbol.class);
}

View File

@ -8,8 +8,8 @@ public class CONS extends LispFunction {
private ArgumentValidator argumentValidator;
public CONS() {
this.argumentValidator = new ArgumentValidator("CONS");
public CONS(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setExactNumberOfArguments(2);
}

View File

@ -8,8 +8,8 @@ public class FIRST extends LispFunction {
private ArgumentValidator argumentValidator;
public FIRST() {
this.argumentValidator = new ArgumentValidator("FIRST");
public FIRST(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setExactNumberOfArguments(1);
this.argumentValidator.setEveryArgumentExpectedType(Cons.class);
}

View File

@ -11,7 +11,7 @@ import sexpression.*;
public class LENGTH extends LispFunction {
public static BigInteger getLength(Cons list) {
LENGTH lengthFunction = new LENGTH();
LENGTH lengthFunction = new LENGTH("LENGTH");
LispNumber length = lengthFunction.callWithoutArgumentValidation(makeList(list));
return length.getValue();
@ -19,8 +19,8 @@ public class LENGTH extends LispFunction {
private ArgumentValidator argumentValidator;
public LENGTH() {
this.argumentValidator = new ArgumentValidator("LENGTH");
public LENGTH(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setExactNumberOfArguments(1);
this.argumentValidator.setEveryArgumentExpectedType(Cons.class);
}

View File

@ -14,8 +14,8 @@ public class LIST extends LispFunction {
private ArgumentValidator argumentValidator;
public LIST() {
this.argumentValidator = new ArgumentValidator("LIST");
public LIST(String name) {
this.argumentValidator = new ArgumentValidator(name);
}
public Cons call(Cons argumentList) {

View File

@ -8,8 +8,8 @@ public class REST extends LispFunction {
private ArgumentValidator argumentValidator;
public REST() {
this.argumentValidator = new ArgumentValidator("REST");
public REST(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setExactNumberOfArguments(1);
this.argumentValidator.setEveryArgumentExpectedType(Cons.class);
}

View File

@ -11,8 +11,8 @@ public class DIVIDE extends LispFunction {
private ArgumentValidator argumentValidator;
private MathFunction mathFunction;
public DIVIDE() {
this.argumentValidator = new ArgumentValidator("/");
public DIVIDE(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setMinimumNumberOfArguments(1);
this.argumentValidator.setEveryArgumentExpectedType(LispNumber.class);
this.mathFunction = new MathFunction(this::getReciprocal, this::divide);

View File

@ -11,8 +11,8 @@ public class MINUS extends LispFunction {
private ArgumentValidator argumentValidator;
private MathFunction mathFunction;
public MINUS() {
this.argumentValidator = new ArgumentValidator("-");
public MINUS(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setMinimumNumberOfArguments(1);
this.argumentValidator.setEveryArgumentExpectedType(LispNumber.class);
this.mathFunction = new MathFunction(this::additiveInverse, this::subtract);

View File

@ -11,8 +11,8 @@ public class MULTIPLY extends LispFunction {
private ArgumentValidator argumentValidator;
private MathFunction mathFunction;
public MULTIPLY() {
this.argumentValidator = new ArgumentValidator("*");
public MULTIPLY(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setEveryArgumentExpectedType(LispNumber.class);
this.mathFunction = new MathFunction(number -> number, this::multiply);
}

View File

@ -11,8 +11,8 @@ public class PLUS extends LispFunction {
private ArgumentValidator argumentValidator;
private MathFunction mathFunction;
public PLUS() {
this.argumentValidator = new ArgumentValidator("+");
public PLUS(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setEveryArgumentExpectedType(LispNumber.class);
this.mathFunction = new MathFunction(number -> number, this::add);
}

View File

@ -11,8 +11,8 @@ public class ATOM extends LispFunction {
private ArgumentValidator argumentValidator;
public ATOM() {
this.argumentValidator = new ArgumentValidator("ATOM");
public ATOM(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setExactNumberOfArguments(1);
}

View File

@ -11,8 +11,8 @@ public class EQ extends LispFunction {
private ArgumentValidator argumentValidator;
public EQ() {
this.argumentValidator = new ArgumentValidator("EQ");
public EQ(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setExactNumberOfArguments(2);
}

View File

@ -15,8 +15,8 @@ public class EQUAL extends LispFunction {
private ArgumentValidator argumentValidator;
public EQUAL() {
this.argumentValidator = new ArgumentValidator("EQUAL");
public EQUAL(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setExactNumberOfArguments(2);
}

View File

@ -11,8 +11,8 @@ public class LISTP extends LispFunction {
private ArgumentValidator argumentValidator;
public LISTP() {
this.argumentValidator = new ArgumentValidator("LISTP");
public LISTP(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setExactNumberOfArguments(1);
}

View File

@ -11,8 +11,8 @@ public class NULL extends LispFunction {
private ArgumentValidator argumentValidator;
public NULL() {
this.argumentValidator = new ArgumentValidator("NULL");
public NULL(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setExactNumberOfArguments(1);
}

View File

@ -11,8 +11,8 @@ public class NUMERIC_EQUAL extends LispFunction {
private ArgumentValidator argumentValidator;
public NUMERIC_EQUAL() {
this.argumentValidator = new ArgumentValidator("=");
public NUMERIC_EQUAL(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setMinimumNumberOfArguments(1);
this.argumentValidator.setEveryArgumentExpectedType(LispNumber.class);
}

View File

@ -11,8 +11,8 @@ public class NUMERIC_GREATER extends LispFunction {
private ArgumentValidator argumentValidator;
public NUMERIC_GREATER() {
this.argumentValidator = new ArgumentValidator(">");
public NUMERIC_GREATER(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setMinimumNumberOfArguments(1);
this.argumentValidator.setEveryArgumentExpectedType(LispNumber.class);
}

View File

@ -11,8 +11,8 @@ public class NUMERIC_LESS extends LispFunction {
private ArgumentValidator argumentValidator;
public NUMERIC_LESS() {
this.argumentValidator = new ArgumentValidator("<");
public NUMERIC_LESS(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setMinimumNumberOfArguments(1);
this.argumentValidator.setEveryArgumentExpectedType(LispNumber.class);
}

View File

@ -11,8 +11,8 @@ public class AND extends LispSpecialFunction {
private ArgumentValidator argumentValidator;
public AND() {
this.argumentValidator = new ArgumentValidator("AND");
public AND(String name) {
this.argumentValidator = new ArgumentValidator(name);
}
public SExpression call(Cons argumentList) {

View File

@ -13,8 +13,8 @@ public class CASE extends LispSpecialFunction {
private ArgumentValidator argumentValidator;
public CASE() {
this.argumentValidator = new ArgumentValidator("CASE");
public CASE(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setMinimumNumberOfArguments(1);
this.argumentValidator.setTrailingArgumentExpectedType(Cons.class);
this.argumentValidator.setTrailingArgumentExcludedType(Nil.class);

View File

@ -11,8 +11,8 @@ public class COND extends LispSpecialFunction {
private ArgumentValidator argumentValidator;
public COND() {
this.argumentValidator = new ArgumentValidator("COND");
public COND(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setEveryArgumentExpectedType(Cons.class);
this.argumentValidator.setEveryArgumentExcludedType(Nil.class);
}

View File

@ -6,8 +6,8 @@ import sexpression.*;
@FunctionNames({ "DEFINE-MACRO" })
public class DEFINE_MACRO extends Define {
public DEFINE_MACRO() {
super("DEFINE-MACRO");
public DEFINE_MACRO(String name) {
super(name);
}
@Override

View File

@ -6,8 +6,8 @@ import sexpression.*;
@FunctionNames({ "DEFUN" })
public class DEFUN extends Define {
public DEFUN() {
super("DEFUN");
public DEFUN(String name) {
super(name);
}
@Override

View File

@ -10,8 +10,8 @@ public class IF extends LispSpecialFunction {
private ArgumentValidator argumentValidator;
public IF() {
this.argumentValidator = new ArgumentValidator("IF");
public IF(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setMinimumNumberOfArguments(2);
this.argumentValidator.setMaximumNumberOfArguments(3);
}

View File

@ -25,7 +25,7 @@ public class LAMBDA extends LispSpecialFunction {
lambdaValidator.setEveryArgumentExpectedType(Cons.class);
lambdaValidator.validate(makeList(rest));
LambdaExpression lambda = new LAMBDA().call((Cons) rest);
LambdaExpression lambda = new LAMBDA("LAMBDA").call((Cons) rest);
return lambda.getFunction();
}
@ -33,12 +33,12 @@ public class LAMBDA extends LispSpecialFunction {
private ArgumentValidator argumentValidator;
private ArgumentValidator lambdaListValidator;
public LAMBDA() {
this.argumentValidator = new ArgumentValidator("LAMBDA");
public LAMBDA(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setFirstArgumentExpectedType(Cons.class);
this.argumentValidator.setMinimumNumberOfArguments(1);
this.lambdaListValidator = new ArgumentValidator("LAMBDA|lambda-list|");
this.lambdaListValidator = new ArgumentValidator(name + "|lambda-list|");
this.lambdaListValidator.setEveryArgumentExpectedType(Symbol.class);
}

View File

@ -15,15 +15,15 @@ public class LET extends LispSpecialFunction {
private ArgumentValidator pairValidator;
private ExecutionContext executionContext;
public LET() {
this.argumentValidator = new ArgumentValidator("LET");
public LET(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setMinimumNumberOfArguments(1);
this.argumentValidator.setFirstArgumentExpectedType(Cons.class);
this.variableDefinitionListValidator = new ArgumentValidator("LET|pair-list|");
this.variableDefinitionListValidator = new ArgumentValidator(name + "|pair-list|");
this.variableDefinitionListValidator.setEveryArgumentExpectedType(Cons.class);
this.pairValidator = new ArgumentValidator("LET|pair|");
this.pairValidator = new ArgumentValidator(name + "|pair|");
this.pairValidator.setMinimumNumberOfArguments(1);
this.pairValidator.setMaximumNumberOfArguments(2);
this.pairValidator.setFirstArgumentExpectedType(Symbol.class);

View File

@ -10,8 +10,8 @@ public class OR extends LispSpecialFunction {
private ArgumentValidator argumentValidator;
public OR() {
this.argumentValidator = new ArgumentValidator("OR");
public OR(String name) {
this.argumentValidator = new ArgumentValidator(name);
}
public SExpression call(Cons argumentList) {

View File

@ -11,8 +11,8 @@ public class PROGN extends LispSpecialFunction {
private ArgumentValidator argumentValidator;
public PROGN() {
this.argumentValidator = new ArgumentValidator("PROGN");
public PROGN(String name) {
this.argumentValidator = new ArgumentValidator(name);
}
public SExpression call(Cons argumentList) {

View File

@ -8,8 +8,8 @@ public class QUOTE extends LispSpecialFunction {
private ArgumentValidator argumentValidator;
public QUOTE() {
this.argumentValidator = new ArgumentValidator("QUOTE");
public QUOTE(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setExactNumberOfArguments(1);
}

View File

@ -12,8 +12,8 @@ public class SETF extends LispSpecialFunction {
private ArgumentValidator argumentValidator;
public SETF() {
this.argumentValidator = new ArgumentValidator("SETF");
public SETF(String name) {
this.argumentValidator = new ArgumentValidator(name);
this.argumentValidator.setExactNumberOfArguments(2);
this.argumentValidator.setFirstArgumentExpectedType(Symbol.class);
}

View File

@ -110,17 +110,15 @@ public class FunctionTable {
}
private void addAllFunctionNamesToTable(Class<? extends LispFunction> function, String[] names) {
LispFunction instance = createInstance(function);
for (String name : names)
table.put(name, instance);
table.put(name, createInstance(function, name));
}
private LispFunction createInstance(Class<? extends LispFunction> function) {
private LispFunction createInstance(Class<? extends LispFunction> function, String name) {
LispFunction instance = null;
try {
instance = function.getConstructor().newInstance();
instance = function.getConstructor(String.class).newInstance(name);
} catch (Exception e) {
throw new LispFunctionInstantiationException(function.getName());
}

View File

@ -19,6 +19,8 @@ public class FunctionTableTester {
@FunctionNames({ "GOOD" })
public static class GoodFunction extends LispFunction {
public GoodFunction(String name) {}
@Override
public SExpression call(Cons argList) {
return NIL;
@ -28,10 +30,6 @@ public class FunctionTableTester {
@FunctionNames({ "BAD" })
public static class BadFunction extends LispFunction {
public BadFunction() {
throw new IllegalArgumentException("bad function");
}
@Override
public SExpression call(Cons argList) {
return NIL;