From 1995b23f42f0c57a10d0f2f0552a3c2ce6b7e093 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Sat, 18 Nov 2017 10:36:24 -0500 Subject: [PATCH] Refactor code --- src/function/builtin/EVAL.java | 12 ++++++++---- src/function/builtin/special/RECUR.java | 19 +++++++++++-------- test/function/builtin/special/RECURTest.java | 16 ++++++++++++++++ 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/function/builtin/EVAL.java b/src/function/builtin/EVAL.java index 559dcce..d351753 100644 --- a/src/function/builtin/EVAL.java +++ b/src/function/builtin/EVAL.java @@ -29,7 +29,7 @@ public class EVAL extends LispFunction { Cons argumentList = makeList(sExpression); try { - return lookupFunction("EVAL").call(argumentList); + return lookupEval().call(argumentList); } catch (LispException e) { executionContext.restoreGlobalScope(); throw e; @@ -37,11 +37,15 @@ public class EVAL extends LispFunction { } public static SExpression applyFunction(LispFunction function, Cons argumentList) { - return ((EVAL) lookupFunction("EVAL")).applyFunctionWithoutEvaluatingArguments(function, argumentList); + return lookupEval().applyFunctionWithoutEvaluatingArguments(function, argumentList); } - public static Cons evalRecurArgumentList(Cons argumentList) { - return ((EVAL) lookupFunction("EVAL")).evaluateArgumentList(argumentList); + public static Cons evaluateFunctionArgumentList(Cons argumentList) { + return lookupEval().evaluateArgumentList(argumentList); + } + + private static EVAL lookupEval() { + return (EVAL) lookupFunction("EVAL"); } public static LispFunction lookupFunctionOrLambda(SExpression functionExpression) { diff --git a/src/function/builtin/special/RECUR.java b/src/function/builtin/special/RECUR.java index ecb0f52..551e840 100644 --- a/src/function/builtin/special/RECUR.java +++ b/src/function/builtin/special/RECUR.java @@ -1,6 +1,6 @@ package function.builtin.special; -import static function.builtin.EVAL.evalRecurArgumentList; +import static function.builtin.EVAL.evaluateFunctionArgumentList; import error.LispException; import function.ArgumentValidator; @@ -23,12 +23,7 @@ public class RECUR extends LispSpecialFunction { @Override public SExpression call(Cons argumentList) { - if (executionContext.isRecurInitializing()) - throw new NestedRecurException(); - - if (!executionContext.isInFunctionCall()) - throw new RecurOutsideOfFunctionException(); - + verifyValidRecurCall(); argumentValidator.validate(argumentList); Cons recurArguments = getRecurArguments(argumentList); executionContext.setRecur(); @@ -36,6 +31,14 @@ public class RECUR extends LispSpecialFunction { return recurArguments; } + private void verifyValidRecurCall() { + if (executionContext.isRecurInitializing()) + throw new NestedRecurException(); + + if (!executionContext.isInFunctionCall()) + throw new RecurOutsideOfFunctionException(); + } + private Cons getRecurArguments(Cons argumentList) { Cons recurArguments = argumentList; @@ -43,7 +46,7 @@ public class RECUR extends LispSpecialFunction { executionContext.setRecurInitializing(); if (isRecurArgumentListEvaluated()) - recurArguments = evalRecurArgumentList(argumentList); + recurArguments = evaluateFunctionArgumentList(argumentList); } finally { executionContext.clearRecurInitializing(); } diff --git a/test/function/builtin/special/RECURTest.java b/test/function/builtin/special/RECURTest.java index 8fc1e22..4cd6280 100644 --- a/test/function/builtin/special/RECURTest.java +++ b/test/function/builtin/special/RECURTest.java @@ -1,6 +1,7 @@ package function.builtin.special; import static org.junit.Assert.fail; +import static testutil.TestUtilities.assertIsErrorWithMessage; import static testutil.TestUtilities.assertSExpressionsMatch; import static testutil.TestUtilities.evaluateString; import static testutil.TestUtilities.parseString; @@ -165,4 +166,19 @@ public class RECURTest extends SymbolAndFunctionCleaner { assertSExpressionsMatch(parseString("PASS"), evaluateString("(nested-tail)")); } + @Test + public void nestedRecurException_HasCorrectAttributes() { + assertIsErrorWithMessage(new NestedRecurException()); + } + + @Test + public void recrOutsideOfFunctionException_HasCorrectAttributes() { + assertIsErrorWithMessage(new RecurOutsideOfFunctionException()); + } + + @Test + public void recurNotInTailPositionException_HasCorrectAttributes() { + assertIsErrorWithMessage(new RecurNotInTailPositionException()); + } + }