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); Cons argumentList = makeList(sExpression);
try { try {
return lookupFunction("EVAL").call(argumentList); return lookupEval().call(argumentList);
} catch (LispException e) { } catch (LispException e) {
executionContext.restoreGlobalScope(); executionContext.restoreGlobalScope();
throw e; throw e;
@ -37,11 +37,15 @@ public class EVAL extends LispFunction {
} }
public static SExpression applyFunction(LispFunction function, Cons argumentList) { 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) { public static Cons evaluateFunctionArgumentList(Cons argumentList) {
return ((EVAL) lookupFunction("EVAL")).evaluateArgumentList(argumentList); return lookupEval().evaluateArgumentList(argumentList);
}
private static EVAL lookupEval() {
return (EVAL) lookupFunction("EVAL");
} }
public static LispFunction lookupFunctionOrLambda(SExpression functionExpression) { public static LispFunction lookupFunctionOrLambda(SExpression functionExpression) {

View File

@ -1,6 +1,6 @@
package function.builtin.special; package function.builtin.special;
import static function.builtin.EVAL.evalRecurArgumentList; import static function.builtin.EVAL.evaluateFunctionArgumentList;
import error.LispException; import error.LispException;
import function.ArgumentValidator; import function.ArgumentValidator;
@ -23,12 +23,7 @@ public class RECUR extends LispSpecialFunction {
@Override @Override
public SExpression call(Cons argumentList) { public SExpression call(Cons argumentList) {
if (executionContext.isRecurInitializing()) verifyValidRecurCall();
throw new NestedRecurException();
if (!executionContext.isInFunctionCall())
throw new RecurOutsideOfFunctionException();
argumentValidator.validate(argumentList); argumentValidator.validate(argumentList);
Cons recurArguments = getRecurArguments(argumentList); Cons recurArguments = getRecurArguments(argumentList);
executionContext.setRecur(); executionContext.setRecur();
@ -36,6 +31,14 @@ public class RECUR extends LispSpecialFunction {
return recurArguments; return recurArguments;
} }
private void verifyValidRecurCall() {
if (executionContext.isRecurInitializing())
throw new NestedRecurException();
if (!executionContext.isInFunctionCall())
throw new RecurOutsideOfFunctionException();
}
private Cons getRecurArguments(Cons argumentList) { private Cons getRecurArguments(Cons argumentList) {
Cons recurArguments = argumentList; Cons recurArguments = argumentList;
@ -43,7 +46,7 @@ public class RECUR extends LispSpecialFunction {
executionContext.setRecurInitializing(); executionContext.setRecurInitializing();
if (isRecurArgumentListEvaluated()) if (isRecurArgumentListEvaluated())
recurArguments = evalRecurArgumentList(argumentList); recurArguments = evaluateFunctionArgumentList(argumentList);
} finally { } finally {
executionContext.clearRecurInitializing(); executionContext.clearRecurInitializing();
} }

View File

@ -1,6 +1,7 @@
package function.builtin.special; package function.builtin.special;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static testutil.TestUtilities.assertIsErrorWithMessage;
import static testutil.TestUtilities.assertSExpressionsMatch; import static testutil.TestUtilities.assertSExpressionsMatch;
import static testutil.TestUtilities.evaluateString; import static testutil.TestUtilities.evaluateString;
import static testutil.TestUtilities.parseString; import static testutil.TestUtilities.parseString;
@ -165,4 +166,19 @@ public class RECURTest extends SymbolAndFunctionCleaner {
assertSExpressionsMatch(parseString("PASS"), evaluateString("(nested-tail)")); 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());
}
} }