Refactor code

This commit is contained in:
Mike Cifelli 2017-11-18 10:36:24 -05:00
parent e0e726d6c0
commit 1995b23f42
3 changed files with 35 additions and 12 deletions

View File

@ -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) {

View File

@ -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();
}

View File

@ -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());
}
}