Resolves #14 - Load now keeps track of directories

This commit is contained in:
Mike Cifelli 2017-03-03 12:56:06 -05:00
parent ba53448c9d
commit 503b19a5e1
10 changed files with 48 additions and 9 deletions

View File

@ -4,6 +4,4 @@ Test
Unit tests for the dlambda special function. Unit tests for the dlambda special function.
| script | lisp interpreter fixture | | script | lisp interpreter fixture |
| show | evaluate | (load "lisp/unit-test.lisp") |
| show | evaluate | (load "lisp/dlambda.lisp") |
| show | evaluate | (load "lisp/dlambda-test.lisp") | | show | evaluate | (load "lisp/dlambda-test.lisp") |

View File

@ -4,7 +4,6 @@ Test
Shows object composition, a default method, and two different ways of referencing objects. Shows object composition, a default method, and two different ways of referencing objects.
| script | lisp interpreter fixture | | script | lisp interpreter fixture |
| show | evaluate | (load "lisp/dlambda.lisp") |
| show | evaluate | (load "lisp/fruit-counter.lisp") | | show | evaluate | (load "lisp/fruit-counter.lisp") |
| check | evaluate | (my-counter :inc-apples) | 1 | | check | evaluate | (my-counter :inc-apples) | 1 |
| check | evaluate | (my-counter :inc-apples) | 2 | | check | evaluate | (my-counter :inc-apples) | 2 |

View File

@ -1,6 +1,7 @@
|LispInterpreter.ExternalFunctionUnitTests||12:36:10 Fri, Mar 03, 2017|
|LispInterpreter.ObjectComposition||12:36:01 Fri, Mar 03, 2017|
|LispInterpreter.DlambdaUnitTests||10:16:42 Fri, Mar 03, 2017| |LispInterpreter.DlambdaUnitTests||10:16:42 Fri, Mar 03, 2017|
|LispInterpreter.dlambda||10:12:59 Fri, Mar 03, 2017| |LispInterpreter.dlambda||10:12:59 Fri, Mar 03, 2017|
|LispInterpreter.ObjectComposition||09:05:15 Fri, Mar 03, 2017|
|LispInterpreter||09:04:51 Fri, Mar 03, 2017| |LispInterpreter||09:04:51 Fri, Mar 03, 2017|
|LispInterpreter.LexicalClosures||16:31:40 Thu, Mar 02, 2017| |LispInterpreter.LexicalClosures||16:31:40 Thu, Mar 02, 2017|
|LispInterpreter.LexicalClosure||16:26:49 Thu, Mar 02, 2017| |LispInterpreter.LexicalClosure||16:26:49 Thu, Mar 02, 2017|

View File

@ -6,6 +6,7 @@ import static sexpression.Symbol.T;
import java.io.*; import java.io.*;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.Stack;
import environment.RuntimeEnvironment; import environment.RuntimeEnvironment;
import error.*; import error.*;
@ -18,12 +19,14 @@ public class LOAD extends LispFunction {
private ArgumentValidator argumentValidator; private ArgumentValidator argumentValidator;
private ErrorManager errorManager; private ErrorManager errorManager;
private Stack<String> pathPrefixes;
public LOAD() { public LOAD() {
this.argumentValidator = new ArgumentValidator("LOAD"); this.argumentValidator = new ArgumentValidator("LOAD");
this.argumentValidator.setExactNumberOfArguments(1); this.argumentValidator.setExactNumberOfArguments(1);
this.argumentValidator.setEveryArgumentExpectedType(LispString.class); this.argumentValidator.setEveryArgumentExpectedType(LispString.class);
this.errorManager = RuntimeEnvironment.getInstance().getErrorManager(); this.errorManager = RuntimeEnvironment.getInstance().getErrorManager();
this.pathPrefixes = new Stack<>();
} }
public SExpression call(Cons argumentList) { public SExpression call(Cons argumentList) {
@ -40,13 +43,18 @@ public class LOAD extends LispFunction {
} }
private SExpression processFile(String fileName) { private SExpression processFile(String fileName) {
boolean wasSuccessful = false; boolean isSuccessful = false;
LispParser parser = attemptToCreateParserOnFile(fileName); String prefixedFileName = prefixFileNameIfNecessary(fileName);
LispParser parser = attemptToCreateParserOnFile(prefixedFileName);
if (parser != null) if (parser != null)
wasSuccessful = isSuccessfulEvaluation(parser); isSuccessful = isSuccessfulEvaluationWithPathPrefix(prefixedFileName, parser);
return wasSuccessful ? T : NIL; return isSuccessful ? T : NIL;
}
private String prefixFileNameIfNecessary(String fileName) {
return pathPrefixes.empty() ? fileName : pathPrefixes.peek() + fileName;
} }
private LispParser attemptToCreateParserOnFile(String fileName) { private LispParser attemptToCreateParserOnFile(String fileName) {
@ -61,6 +69,18 @@ public class LOAD extends LispFunction {
return parser; return parser;
} }
private boolean isSuccessfulEvaluationWithPathPrefix(String prefixedFileName, LispParser parser) {
pathPrefixes.push(getPathPrefix(prefixedFileName));
boolean isSuccessful = isSuccessfulEvaluation(parser);
pathPrefixes.pop();
return isSuccessful;
}
private String getPathPrefix(String fileName) {
return fileName.substring(0, fileName.lastIndexOf(File.separator) + 1);
}
private boolean isSuccessfulEvaluation(LispParser parser) { private boolean isSuccessfulEvaluation(LispParser parser) {
while (!parser.isEof()) { while (!parser.isEof()) {
try { try {

View File

@ -33,7 +33,7 @@ public class LOADTester {
} }
private void assertNothingPrinted() { private void assertNothingPrinted() {
assertTrue(outputStream.toByteArray().length == 0); assertTrue(errorOutputStream.toByteArray().length == 0);
assertTrue(outputStream.toByteArray().length == 0); assertTrue(outputStream.toByteArray().length == 0);
} }
@ -74,6 +74,22 @@ public class LOADTester {
assertWarningMessagePrinted(); assertWarningMessagePrinted();
} }
@Test
public void nestedLoadsInTheSameDirectory() {
String input = "(load \"test/function/builtin/test-files/nested/nested.lisp\")";
assertT(evaluateString(input));
assertNothingPrinted();
}
@Test
public void nestedLoadsInDifferentDirectories() {
String input = "(load \"test/function/builtin/test-files/nested/one/load-one.lisp\")";
assertT(evaluateString(input));
assertNothingPrinted();
}
@Test(expected = BadArgumentTypeException.class) @Test(expected = BadArgumentTypeException.class)
public void loadWithBadArgumentType() { public void loadWithBadArgumentType() {
evaluateString("(load '1)"); evaluateString("(load '1)");

View File

@ -0,0 +1 @@
"included"

View File

@ -0,0 +1 @@
(load "included.lisp")

View File

@ -0,0 +1 @@
(load "two/three/load-three.lisp")

View File

@ -0,0 +1 @@
(load "../../nested.lisp")

View File

@ -0,0 +1 @@
(load "../load-two.lisp")