diff --git a/src/function/builtin/APPLY.java b/src/function/builtin/APPLY.java index ca35f92..4a9797c 100644 --- a/src/function/builtin/APPLY.java +++ b/src/function/builtin/APPLY.java @@ -20,9 +20,9 @@ public class APPLY extends LispFunction { public SExpression call(Cons argumentList) { argumentValidator.validate(argumentList); - Cons cdr = (Cons) argumentList.getRest(); + Cons rest = (Cons) argumentList.getRest(); SExpression functionName = argumentList.getFirst(); - SExpression functionArguments = cdr.getFirst(); + SExpression functionArguments = rest.getFirst(); LispFunction function = EVAL.lookupFunctionOrLambda(functionName); return function.call((Cons) functionArguments); diff --git a/src/function/builtin/EVAL.java b/src/function/builtin/EVAL.java index a73a6b4..b8aa08b 100644 --- a/src/function/builtin/EVAL.java +++ b/src/function/builtin/EVAL.java @@ -98,10 +98,10 @@ public class EVAL extends LispFunction { if (arguments.nullp()) return Nil.getInstance(); - SExpression car = eval(arguments.getFirst()); - SExpression cdr = arguments.getRest(); + SExpression first = eval(arguments.getFirst()); + SExpression rest = arguments.getRest(); - return new Cons(car, evaluateArgList((Cons) cdr)); + return new Cons(first, evaluateArgList((Cons) rest)); } public static class UndefinedFunctionException extends LispException { diff --git a/src/function/builtin/cons/CONS.java b/src/function/builtin/cons/CONS.java index 30deaa1..013fed6 100644 --- a/src/function/builtin/cons/CONS.java +++ b/src/function/builtin/cons/CONS.java @@ -15,9 +15,9 @@ public class CONS extends LispFunction { public Cons call(Cons argumentList) { argumentValidator.validate(argumentList); - Cons cdr = (Cons) argumentList.getRest(); + Cons rest = (Cons) argumentList.getRest(); SExpression firstArgument = argumentList.getFirst(); - SExpression secondArgument = cdr.getFirst(); + SExpression secondArgument = rest.getFirst(); return new Cons(firstArgument, secondArgument); } diff --git a/src/function/builtin/cons/CAR.java b/src/function/builtin/cons/FIRST.java similarity index 78% rename from src/function/builtin/cons/CAR.java rename to src/function/builtin/cons/FIRST.java index 1dad001..7a9a206 100644 --- a/src/function/builtin/cons/CAR.java +++ b/src/function/builtin/cons/FIRST.java @@ -3,12 +3,12 @@ package function.builtin.cons; import function.*; import sexpression.*; -public class CAR extends LispFunction { +public class FIRST extends LispFunction { private ArgumentValidator argumentValidator; - public CAR() { - this.argumentValidator = new ArgumentValidator("CAR"); + public FIRST() { + this.argumentValidator = new ArgumentValidator("FIRST"); this.argumentValidator.setExactNumberOfArguments(1); this.argumentValidator.setEveryArgumentExpectedType(Cons.class); } diff --git a/src/function/builtin/cons/CDR.java b/src/function/builtin/cons/REST.java similarity index 78% rename from src/function/builtin/cons/CDR.java rename to src/function/builtin/cons/REST.java index 6f0f4fb..34fceb1 100644 --- a/src/function/builtin/cons/CDR.java +++ b/src/function/builtin/cons/REST.java @@ -3,12 +3,12 @@ package function.builtin.cons; import function.*; import sexpression.*; -public class CDR extends LispFunction { +public class REST extends LispFunction { private ArgumentValidator argumentValidator; - public CDR() { - this.argumentValidator = new ArgumentValidator("CDR"); + public REST() { + this.argumentValidator = new ArgumentValidator("REST"); this.argumentValidator.setExactNumberOfArguments(1); this.argumentValidator.setEveryArgumentExpectedType(Cons.class); } diff --git a/src/function/builtin/predicate/EQ.java b/src/function/builtin/predicate/EQ.java index c958162..7e6604b 100644 --- a/src/function/builtin/predicate/EQ.java +++ b/src/function/builtin/predicate/EQ.java @@ -15,9 +15,9 @@ public class EQ extends LispFunction { public SExpression call(Cons argumentList) { argumentValidator.validate(argumentList); - Cons cdr = (Cons) argumentList.getRest(); + Cons rest = (Cons) argumentList.getRest(); SExpression firstArgument = argumentList.getFirst(); - SExpression secondArgument = cdr.getFirst(); + SExpression secondArgument = rest.getFirst(); return eq(firstArgument, secondArgument); } diff --git a/src/function/builtin/predicate/EQUAL.java b/src/function/builtin/predicate/EQUAL.java index fa9ea8f..f3503a2 100644 --- a/src/function/builtin/predicate/EQUAL.java +++ b/src/function/builtin/predicate/EQUAL.java @@ -20,24 +20,24 @@ public class EQUAL extends LispFunction { } private SExpression callRecursive(Cons argumentList) { - Cons cdr = (Cons) argumentList.getRest(); + Cons rest = (Cons) argumentList.getRest(); SExpression firstArgument = argumentList.getFirst(); - SExpression secondArgument = cdr.getFirst(); + SExpression secondArgument = rest.getFirst(); if (!isListPair(firstArgument, secondArgument)) return equal(firstArgument, secondArgument); Cons listOne = (Cons) firstArgument; Cons listTwo = (Cons) secondArgument; - SExpression listOneCar = listOne.getFirst(); - SExpression listTwoCar = listTwo.getFirst(); - SExpression listOneCdr = listOne.getRest(); - SExpression listTwoCdr = listTwo.getRest(); + SExpression listOneFirst = listOne.getFirst(); + SExpression listTwoFirst = listTwo.getFirst(); + SExpression listOneRest = listOne.getRest(); + SExpression listTwoRest = listTwo.getRest(); - SExpression carEqual = callRecursive(makeArgumentList(listOneCar, listTwoCar)); - SExpression cdrEqual = callRecursive(makeArgumentList(listOneCdr, listTwoCdr)); + SExpression firstEqual = callRecursive(makeArgumentList(listOneFirst, listTwoFirst)); + SExpression restEqual = callRecursive(makeArgumentList(listOneRest, listTwoRest)); - return logicalConjunction(carEqual, cdrEqual); + return logicalConjunction(firstEqual, restEqual); } private boolean isListPair(SExpression firstArgument, SExpression secondArgument) { @@ -52,16 +52,16 @@ public class EQUAL extends LispFunction { return firstArgument.toString().equals(secondArgument.toString()); } - private Cons makeArgumentList(SExpression listOneCar, SExpression listTwoCar) { - return new Cons(listOneCar, LIST.makeList(listTwoCar)); + private Cons makeArgumentList(SExpression listOneFirst, SExpression listTwoFirst) { + return new Cons(listOneFirst, LIST.makeList(listTwoFirst)); } - private SExpression logicalConjunction(SExpression carEqual, SExpression cdrEqual) { - return bothAreTrue(carEqual, cdrEqual) ? Symbol.T : Nil.getInstance(); + private SExpression logicalConjunction(SExpression firstEqual, SExpression restEqual) { + return bothAreTrue(firstEqual, restEqual) ? Symbol.T : Nil.getInstance(); } - private boolean bothAreTrue(SExpression carEqual, SExpression cdrEqual) { - return (carEqual == Symbol.T) && (cdrEqual == Symbol.T); + private boolean bothAreTrue(SExpression firstEqual, SExpression restEqual) { + return (firstEqual == Symbol.T) && (restEqual == Symbol.T); } } diff --git a/src/function/builtin/special/COND.java b/src/function/builtin/special/COND.java index a2e991c..87bdeee 100644 --- a/src/function/builtin/special/COND.java +++ b/src/function/builtin/special/COND.java @@ -43,7 +43,7 @@ public class COND extends LispFunction { SExpression lastResultValue = test; for (SExpression result = clause.getRest(); result.consp(); result = advanceCons(result)) - lastResultValue = eval(getCar(result)); + lastResultValue = eval(getFirst(result)); return lastResultValue; } @@ -52,7 +52,7 @@ public class COND extends LispFunction { return ((Cons) knownCons).getRest(); } - private SExpression getCar(SExpression knownCons) { + private SExpression getFirst(SExpression knownCons) { return ((Cons) knownCons).getFirst(); } diff --git a/src/function/builtin/special/LAMBDA.java b/src/function/builtin/special/LAMBDA.java index adb2912..d64d911 100644 --- a/src/function/builtin/special/LAMBDA.java +++ b/src/function/builtin/special/LAMBDA.java @@ -1,7 +1,8 @@ package function.builtin.special; +import static function.builtin.cons.LIST.makeList; + import function.*; -import function.builtin.cons.LIST; import sexpression.*; public class LAMBDA extends LispFunction { @@ -17,13 +18,13 @@ public class LAMBDA extends LispFunction { } public static UserDefinedFunction createFunction(Cons lambdaExpression) { - SExpression cdr = lambdaExpression.getRest(); + SExpression rest = lambdaExpression.getRest(); ArgumentValidator lambdaValidator = new ArgumentValidator("LAMBDA|create|"); lambdaValidator.setEveryArgumentExpectedType(Cons.class); - lambdaValidator.validate(LIST.makeList(cdr)); + lambdaValidator.validate(makeList(rest)); - LambdaExpression lambda = new LAMBDA().call((Cons) cdr); + LambdaExpression lambda = new LAMBDA().call((Cons) rest); return lambda.getFunction(); } @@ -43,8 +44,8 @@ public class LAMBDA extends LispFunction { public LambdaExpression call(Cons argumentList) { argumentValidator.validate(argumentList); - SExpression car = argumentList.getFirst(); - Cons lambdaList = (Cons) car; + SExpression first = argumentList.getFirst(); + Cons lambdaList = (Cons) first; Cons body = (Cons) argumentList.getRest(); lambdaListValidator.validate(lambdaList); diff --git a/src/function/builtin/special/SETF.java b/src/function/builtin/special/SETF.java index 9b8f725..224962b 100644 --- a/src/function/builtin/special/SETF.java +++ b/src/function/builtin/special/SETF.java @@ -21,9 +21,9 @@ public class SETF extends LispFunction { public SExpression call(Cons argumentList) { argumentValidator.validate(argumentList); - Cons cdr = (Cons) argumentList.getRest(); + Cons rest = (Cons) argumentList.getRest(); SExpression symbol = argumentList.getFirst(); - SExpression value = eval(cdr.getFirst()); + SExpression value = eval(rest.getFirst()); SymbolTable table = findScopeOfSymbol(symbol); table.put(symbol.toString(), value); diff --git a/src/sexpression/Nil.java b/src/sexpression/Nil.java index 71da8d5..0fe3c44 100644 --- a/src/sexpression/Nil.java +++ b/src/sexpression/Nil.java @@ -33,14 +33,14 @@ public class Nil extends Cons { } /** - * The car of NIL can not be changed. + * The first of NIL can not be changed. */ - public void setFirst(SExpression newCar) {} + public void setFirst(SExpression first) {} /** - * The cdr of NIL can not be changed. + * The rest of NIL can not be changed. */ - public void setRest(SExpression newCdr) {} + public void setRest(SExpression rest) {} public String toString() { return "NIL"; diff --git a/src/table/FunctionTable.java b/src/table/FunctionTable.java index fc70e18..a68ca7d 100644 --- a/src/table/FunctionTable.java +++ b/src/table/FunctionTable.java @@ -48,8 +48,8 @@ public class FunctionTable { functionTable.put("AND", new AND()); functionTable.put("APPLY", new APPLY()); functionTable.put("ATOM", new ATOM()); - functionTable.put("CAR", new CAR()); - functionTable.put("CDR", new CDR()); + functionTable.put("CAR", new FIRST()); + functionTable.put("CDR", new REST()); functionTable.put("COND", new COND()); functionTable.put("CONS", new CONS()); functionTable.put("DEFUN", new DEFUN()); @@ -57,7 +57,7 @@ public class FunctionTable { functionTable.put("EQUAL", new EQUAL()); functionTable.put("EVAL", new EVAL()); functionTable.put("EXIT", new EXIT()); - functionTable.put("FIRST", new CAR()); + functionTable.put("FIRST", new FIRST()); functionTable.put("FUNCALL", new FUNCALL()); functionTable.put("GREATERP", new GREATERP()); functionTable.put("IF", new IF()); @@ -71,7 +71,7 @@ public class FunctionTable { functionTable.put("OR", new OR()); functionTable.put("PRINT", new PRINT()); functionTable.put("QUOTE", new QUOTE()); - functionTable.put("REST", new CDR()); + functionTable.put("REST", new REST()); functionTable.put("SETF", new SETF()); functionTable.put("SYMBOL-FUNCTION", new SYMBOL_FUNCTION()); } diff --git a/src/token/Token.java b/src/token/Token.java index 90a9451..fe8af36 100644 --- a/src/token/Token.java +++ b/src/token/Token.java @@ -31,12 +31,12 @@ public abstract class Token { // sExprTail ::= RIGHT_PAREN | sExpr sExprTail public SExpression parseSExpressionTail(Supplier getNextToken) { - SExpression car = parseSExpression(getNextToken); + SExpression first = parseSExpression(getNextToken); Token nextToken = getNextToken.get(); - SExpression cdr = nextToken.parseSExpressionTail(getNextToken); + SExpression rest = nextToken.parseSExpressionTail(getNextToken); - return new Cons(car, cdr); + return new Cons(first, rest); } } diff --git a/test/function/builtin/cons/CARTester.java b/test/function/builtin/cons/CARTester.java deleted file mode 100644 index 5d2d7fe..0000000 --- a/test/function/builtin/cons/CARTester.java +++ /dev/null @@ -1,47 +0,0 @@ -package function.builtin.cons; - -import static testutil.TestUtilities.*; - -import org.junit.Test; - -import function.ArgumentValidator.*; - -public class CARTester { - - @Test - public void testCarWithNil() { - String input = "(car nil)"; - - assertSExpressionsMatch(parseString("()"), evaluateString(input)); - } - - @Test - public void testCarWithList() { - String input = "(car '(1 2 3))"; - - assertSExpressionsMatch(parseString("1"), evaluateString(input)); - } - - @Test - public void testNestedCarWithList() { - String input = "(car (car '((1 2) 3)))"; - - assertSExpressionsMatch(parseString("1"), evaluateString(input)); - } - - @Test(expected = BadArgumentTypeException.class) - public void testCarWithNonList() { - evaluateString("(car 'x)"); - } - - @Test(expected = TooManyArgumentsException.class) - public void testCarWithTooManyArguments() { - evaluateString("(car '(1 2) '(1 2) \"oh\")"); - } - - @Test(expected = TooFewArgumentsException.class) - public void testCarWithTooFewArguments() { - evaluateString("(car)"); - } - -} diff --git a/test/function/builtin/cons/CONSTester.java b/test/function/builtin/cons/CONSTester.java index 4cc868b..22e1eb2 100644 --- a/test/function/builtin/cons/CONSTester.java +++ b/test/function/builtin/cons/CONSTester.java @@ -24,7 +24,7 @@ public class CONSTester { } @Test - public void testConsWithListAsCdr() { + public void testConsWithListAsRest() { String input = "(cons 1 '(2 3))"; assertSExpressionsMatch(parseString("(1 2 3)"), evaluateString(input)); diff --git a/test/function/builtin/cons/FIRSTTester.java b/test/function/builtin/cons/FIRSTTester.java new file mode 100644 index 0000000..5809f94 --- /dev/null +++ b/test/function/builtin/cons/FIRSTTester.java @@ -0,0 +1,54 @@ +package function.builtin.cons; + +import static testutil.TestUtilities.*; + +import org.junit.Test; + +import function.ArgumentValidator.*; + +public class FIRSTTester { + + @Test + public void firstOfNil() { + String input = "(first nil)"; + + assertSExpressionsMatch(parseString("()"), evaluateString(input)); + } + + @Test + public void firstOfList() { + String input = "(first '(1 2 3))"; + + assertSExpressionsMatch(parseString("1"), evaluateString(input)); + } + + @Test + public void carOfList() { + String input = "(car '(1 2 3))"; + + assertSExpressionsMatch(parseString("1"), evaluateString(input)); + } + + @Test + public void nestedFirstOfList() { + String input = "(first (first '((1 2) 3)))"; + + assertSExpressionsMatch(parseString("1"), evaluateString(input)); + } + + @Test(expected = BadArgumentTypeException.class) + public void firstOfSymbol() { + evaluateString("(first 'x)"); + } + + @Test(expected = TooManyArgumentsException.class) + public void firstWithTooManyArguments() { + evaluateString("(first '(1 2) '(1 2) \"oh\")"); + } + + @Test(expected = TooFewArgumentsException.class) + public void firstWithTooFewArguments() { + evaluateString("(first)"); + } + +} diff --git a/test/function/builtin/cons/CDRTester.java b/test/function/builtin/cons/RESTTester.java similarity index 50% rename from test/function/builtin/cons/CDRTester.java rename to test/function/builtin/cons/RESTTester.java index 22b27dd..aaa71c1 100644 --- a/test/function/builtin/cons/CDRTester.java +++ b/test/function/builtin/cons/RESTTester.java @@ -6,42 +6,49 @@ import org.junit.Test; import function.ArgumentValidator.*; -public class CDRTester { +public class RESTTester { @Test - public void testCdrWithNil() { - String input = "(cdr nil)"; + public void restOfNil() { + String input = "(rest nil)"; assertSExpressionsMatch(parseString("()"), evaluateString(input)); } @Test - public void testCdrWithList() { + public void restOfList() { + String input = "(rest '(1 2 3))"; + + assertSExpressionsMatch(parseString("(2 3)"), evaluateString(input)); + } + + @Test + public void cdrOfList() { String input = "(cdr '(1 2 3))"; assertSExpressionsMatch(parseString("(2 3)"), evaluateString(input)); } @Test - public void testNestedCdrWithList() { - String input = "(cdr (cdr '(1 2 3)))"; + public void nestedRestOfList() { + String input = "(rest (rest '(1 2 3)))"; assertSExpressionsMatch(parseString("(3)"), evaluateString(input)); } @Test(expected = BadArgumentTypeException.class) - public void testCdrWithNonList() { - evaluateString("(cdr 'x)"); + public void restOfSymbol() { + evaluateString("(rest 'x)"); } @Test(expected = TooManyArgumentsException.class) - public void testCdrWithTooManyArguments() { - evaluateString("(cdr '(1 2) '(1 2) \"oh\")"); + public void restWithTooManyArguments() { + evaluateString("(rest '(1 2) '(1 2) \"oh\")"); } @Test(expected = TooFewArgumentsException.class) - public void testCdrWithTooFewArguments() { - evaluateString("(cdr)"); + public void restWithTooFewArguments() { + evaluateString("(rest)"); } } diff --git a/test/sexpression/SExpressionTester.java b/test/sexpression/SExpressionTester.java index eee01f5..d768ce0 100644 --- a/test/sexpression/SExpressionTester.java +++ b/test/sexpression/SExpressionTester.java @@ -72,7 +72,7 @@ public class SExpressionTester { } @Test - public void testConsWithNonListCdrToString() { + public void testImproperListToString() { String expected = "(A . B)"; Cons list = new Cons(new Symbol("A"), new Symbol("B")); @@ -105,17 +105,17 @@ public class SExpressionTester { } @Test - public void testCarOfNilIsNil() { + public void testFirstOfNilIsNil() { assertEquals(Nil.getInstance(), Nil.getInstance().getFirst()); } @Test - public void testCdrOfNilIsNil() { + public void testRestOfNilIsNil() { assertEquals(Nil.getInstance(), Nil.getInstance().getRest()); } @Test - public void afterSettingCarOfNil_ShouldStillBeNil() { + public void afterSettingFirstOfNil_ShouldStillBeNil() { Cons nil = Nil.getInstance(); nil.setFirst(new LispNumber("2")); @@ -123,7 +123,7 @@ public class SExpressionTester { } @Test - public void afterSettingCdrOfNil_ShouldStillBeNil() { + public void afterSettingRestOfNil_ShouldStillBeNil() { Cons nil = Nil.getInstance(); nil.setRest(new LispNumber("2"));