Allow DEFUN and LAMBDA with an empty body

This commit is contained in:
Mike Cifelli 2017-02-04 15:38:47 -05:00
parent 0a5228d5a7
commit a9a47be6cd
5 changed files with 22 additions and 7 deletions

View File

@ -37,7 +37,7 @@ public class UserDefinedFunction extends LispFunction {
SymbolTable callingScope = executionContext.getScope(); SymbolTable callingScope = executionContext.getScope();
executionContext.setScope(functionScope); executionContext.setScope(functionScope);
SExpression lastEvaluation = null; SExpression lastEvaluation = Nil.getInstance();
for (Cons expression = body; expression.consp(); expression = (Cons) expression.getCdr()) for (Cons expression = body; expression.consp(); expression = (Cons) expression.getCdr())
lastEvaluation = EVAL.eval(expression.getCar()); lastEvaluation = EVAL.eval(expression.getCar());

View File

@ -17,7 +17,7 @@ public class DEFUN extends LispFunction {
public DEFUN() { public DEFUN() {
this.argumentValidator = new ArgumentValidator("DEFUN"); this.argumentValidator = new ArgumentValidator("DEFUN");
this.argumentValidator.setMinimumNumberOfArguments(3); this.argumentValidator.setMinimumNumberOfArguments(2);
this.argumentValidator.setFirstArgumentExpectedType(Symbol.class); this.argumentValidator.setFirstArgumentExpectedType(Symbol.class);
this.lambdaListIsListValidator = new ArgumentValidator("DEFUN|lambda-list|"); this.lambdaListIsListValidator = new ArgumentValidator("DEFUN|lambda-list|");

View File

@ -34,7 +34,7 @@ public class LAMBDA extends LispFunction {
public LAMBDA() { public LAMBDA() {
this.argumentValidator = new ArgumentValidator("LAMBDA"); this.argumentValidator = new ArgumentValidator("LAMBDA");
this.argumentValidator.setFirstArgumentExpectedType(Cons.class); this.argumentValidator.setFirstArgumentExpectedType(Cons.class);
this.argumentValidator.setMinimumNumberOfArguments(2); this.argumentValidator.setMinimumNumberOfArguments(1);
this.lambdaListValidator = new ArgumentValidator("LAMBDA|lambda-list|"); this.lambdaListValidator = new ArgumentValidator("LAMBDA|lambda-list|");
this.lambdaListValidator.setEveryArgumentExpectedType(Symbol.class); this.lambdaListValidator.setEveryArgumentExpectedType(Symbol.class);

View File

@ -26,7 +26,15 @@ public class DEFUNTester {
@Test @Test
public void testDefun() { public void testDefun() {
String input = "(defun f () nil)"; String input = "(defun f () t)";
assertSExpressionsMatch(parseString("f"), evaluateString(input));
assertSExpressionsMatch(parseString("t"), evaluateString("(f)"));
}
@Test
public void testDefunWithEmptyBody() {
String input = "(defun f ())";
assertSExpressionsMatch(parseString("f"), evaluateString(input)); assertSExpressionsMatch(parseString("f"), evaluateString(input));
assertSExpressionsMatch(parseString("()"), evaluateString("(f)")); assertSExpressionsMatch(parseString("()"), evaluateString("(f)"));
@ -68,8 +76,8 @@ public class DEFUNTester {
} }
@Test(expected = TooFewArgumentsException.class) @Test(expected = TooFewArgumentsException.class)
public void testApplyWithTooFewArguments() { public void testDefunWithTooFewArguments() {
evaluateString("(defun x ())"); evaluateString("(defun x)");
} }
} }

View File

@ -17,6 +17,13 @@ public class LAMBDATester {
assertSExpressionsMatch(parseString("(LAMBDA (X) X)"), evaluateString(input)); assertSExpressionsMatch(parseString("(LAMBDA (X) X)"), evaluateString(input));
} }
@Test
public void testLambdaWithNoBody() {
String input = "(lambda ())";
assertSExpressionsMatch(parseString("(LAMBDA ())"), evaluateString(input));
}
@Test @Test
public void lambdaExpressionIsLambdaExpression() { public void lambdaExpressionIsLambdaExpression() {
Cons lambdaExpression = new Cons(new Symbol("LAMBDA"), Cons lambdaExpression = new Cons(new Symbol("LAMBDA"),
@ -74,7 +81,7 @@ public class LAMBDATester {
@Test(expected = TooFewArgumentsException.class) @Test(expected = TooFewArgumentsException.class)
public void testLambdaWithTooFewArguments() { public void testLambdaWithTooFewArguments() {
evaluateString("(lambda ())"); evaluateString("(lambda)");
} }
@Test @Test