From 568fce3c1104c4767a67aecc5015744fa7053a77 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Sun, 21 Jan 2018 10:32:55 -0500 Subject: [PATCH] Load lisp language files automatically --- fitnesse/FitNesseRoot/RecentChanges.wiki | 4 ++- .../TranscendentalLisp/FinanceUnitTests.wiki | 4 +-- .../TranscendentalLisp/SetUp.wiki | 2 +- .../TranscendentalLisp/SuiteSetUp.wiki | 3 -- lisp/finance/interest-compounder.lisp | 2 -- lisp/lang/dlambda-test.lisp | 1 - lisp/lang/dlambda.lisp | 2 -- lisp/object/composition.lisp | 2 -- lisp/object/this.lisp | 2 -- lisp/random/structure.lisp | 3 -- lisp/unit/unit-tester.lisp | 2 -- lisp/util/list-builder.lisp | 2 -- pom.xml | 6 ++++ src/interpreter/LispInterpreter.java | 31 +++++++++++++++++++ src/interpreter/LispInterpreterBuilder.java | 2 ++ .../LispInterpreterBuilderImpl.java | 15 +++++++++ src/main/LispMain.java | 4 +++ .../fixture/LispInterpreterFixture.java | 23 ++++++++------ test/main/MainTest.java | 13 ++++---- 19 files changed, 83 insertions(+), 40 deletions(-) diff --git a/fitnesse/FitNesseRoot/RecentChanges.wiki b/fitnesse/FitNesseRoot/RecentChanges.wiki index b8bef6f..33636f9 100644 --- a/fitnesse/FitNesseRoot/RecentChanges.wiki +++ b/fitnesse/FitNesseRoot/RecentChanges.wiki @@ -1,8 +1,10 @@ +|TranscendentalLisp.FinanceUnitTests||10:16:07 Sun, Jan 21, 2018| +|TranscendentalLisp.SetUp||10:15:35 Sun, Jan 21, 2018| +|TranscendentalLisp.SuiteSetUp||10:12:22 Sun, Jan 21, 2018| |TranscendentalLisp.Recursion||15:06:26 Sun, Nov 26, 2017| |TranscendentalLisp||16:15:14 Fri, Mar 17, 2017| |TranscendentalLisp.Macros||10:10:15 Mon, Mar 13, 2017| |TranscendentalLisp.MacroTests||10:07:00 Mon, Mar 13, 2017| -|TranscendentalLisp.FinanceUnitTests||11:07:42 Thu, Mar 09, 2017| |TranscendentalLisp.LangUnitTests||11:04:17 Thu, Mar 09, 2017| |TranscendentalLisp.LexicalClosures||16:39:59 Tue, Mar 07, 2017| |FrontPage||10:19:45 Tue, Mar 07, 2017| diff --git a/fitnesse/FitNesseRoot/TranscendentalLisp/FinanceUnitTests.wiki b/fitnesse/FitNesseRoot/TranscendentalLisp/FinanceUnitTests.wiki index 33b776b..9d74089 100644 --- a/fitnesse/FitNesseRoot/TranscendentalLisp/FinanceUnitTests.wiki +++ b/fitnesse/FitNesseRoot/TranscendentalLisp/FinanceUnitTests.wiki @@ -4,11 +4,11 @@ Test Unit tests for the interest-compounder class. | script | lisp interpreter fixture | -| clear functions and symbols | +| refresh interpreter | | check | evaluate file | lisp/finance/interest-compounder-test.lisp | =~/\nT$/ | Unit tests for the compound-interest function. | script | lisp interpreter fixture | -| clear functions and symbols | +| refresh interpreter | | check | evaluate file | lisp/finance/compound-interest-test.lisp | =~/\nT$/ | diff --git a/fitnesse/FitNesseRoot/TranscendentalLisp/SetUp.wiki b/fitnesse/FitNesseRoot/TranscendentalLisp/SetUp.wiki index 1375dec..b9349e4 100644 --- a/fitnesse/FitNesseRoot/TranscendentalLisp/SetUp.wiki +++ b/fitnesse/FitNesseRoot/TranscendentalLisp/SetUp.wiki @@ -2,4 +2,4 @@ Test --- | script | lisp interpreter fixture | -| clear functions and symbols | +| refresh interpreter | diff --git a/fitnesse/FitNesseRoot/TranscendentalLisp/SuiteSetUp.wiki b/fitnesse/FitNesseRoot/TranscendentalLisp/SuiteSetUp.wiki index 099b392..b21b786 100644 --- a/fitnesse/FitNesseRoot/TranscendentalLisp/SuiteSetUp.wiki +++ b/fitnesse/FitNesseRoot/TranscendentalLisp/SuiteSetUp.wiki @@ -3,6 +3,3 @@ Test --- | import | | acceptance.fixture | - -| script | lisp interpreter fixture | -| build interpreter | diff --git a/lisp/finance/interest-compounder.lisp b/lisp/finance/interest-compounder.lisp index ccbb2ca..558aa2b 100644 --- a/lisp/finance/interest-compounder.lisp +++ b/lisp/finance/interest-compounder.lisp @@ -1,5 +1,3 @@ -(load "../lang/dlambda.lisp") - (let ((static)) (setq static diff --git a/lisp/lang/dlambda-test.lisp b/lisp/lang/dlambda-test.lisp index 0c0748d..94a81bc 100644 --- a/lisp/lang/dlambda-test.lisp +++ b/lisp/lang/dlambda-test.lisp @@ -1,5 +1,4 @@ (load "../unit/unit-tester.lisp") -(load "dlambda.lisp") (setq assertions (unit-tester-assertions)) diff --git a/lisp/lang/dlambda.lisp b/lisp/lang/dlambda.lisp index 9f11c4b..4d67997 100644 --- a/lisp/lang/dlambda.lisp +++ b/lisp/lang/dlambda.lisp @@ -1,5 +1,3 @@ -(load "functions.lisp") - ;; This is based on the dlambda macro presented in "Let Over Lambda" by Doug Hoyte. (defmacro dlambda (&rest methods) diff --git a/lisp/object/composition.lisp b/lisp/object/composition.lisp index b857f6a..6706325 100644 --- a/lisp/object/composition.lisp +++ b/lisp/object/composition.lisp @@ -1,5 +1,3 @@ -(load "../lang/dlambda.lisp") - (defun counter (initial-count) (let ((count initial-count)) diff --git a/lisp/object/this.lisp b/lisp/object/this.lisp index a0d8de0..bd960de 100644 --- a/lisp/object/this.lisp +++ b/lisp/object/this.lisp @@ -1,5 +1,3 @@ -(load "../lang/dlambda.lisp") - (defun counter (initial-count) (let ((this) (name) (count initial-count)) diff --git a/lisp/random/structure.lisp b/lisp/random/structure.lisp index 93bc049..c4328ba 100644 --- a/lisp/random/structure.lisp +++ b/lisp/random/structure.lisp @@ -1,6 +1,3 @@ -(load "../lang/dlambda.lisp") -(load "../lang/functions.lisp") - (defmacro keys (&rest fields) `(let ,(mapcar 'list fields) (dlambda diff --git a/lisp/unit/unit-tester.lisp b/lisp/unit/unit-tester.lisp index ac44b73..dc40eb9 100644 --- a/lisp/unit/unit-tester.lisp +++ b/lisp/unit/unit-tester.lisp @@ -1,5 +1,3 @@ -(load "../lang/dlambda.lisp") - (let ((private-static) (public-static)) (defun unit-tester-assertions () diff --git a/lisp/util/list-builder.lisp b/lisp/util/list-builder.lisp index 11b4151..5779291 100644 --- a/lisp/util/list-builder.lisp +++ b/lisp/util/list-builder.lisp @@ -1,5 +1,3 @@ -(load "../lang/functions.lisp") - (defun list-doubler (seed times-to-double) (if (< times-to-double 1) seed (recur (append seed seed) (- times-to-double 1)))) diff --git a/pom.xml b/pom.xml index 3de3c43..9b252cd 100644 --- a/pom.xml +++ b/pom.xml @@ -12,6 +12,12 @@ src test + + + + lisp/lang + + diff --git a/src/interpreter/LispInterpreter.java b/src/interpreter/LispInterpreter.java index 34e9d95..36f6a92 100644 --- a/src/interpreter/LispInterpreter.java +++ b/src/interpreter/LispInterpreter.java @@ -2,6 +2,9 @@ package interpreter; import static function.builtin.EVAL.eval; +import java.io.InputStream; +import java.util.List; + import environment.RuntimeEnvironment; import error.LispException; import parser.LispParser; @@ -16,6 +19,15 @@ public class LispInterpreter { this.environment = RuntimeEnvironment.getInstance(); } + public void interpretLanguageFiles(List languageFiles) { + for (LanguageFile file : languageFiles) { + LispParser languageParser = new LispParser(file.getInputStream(), file.getName()); + + while (!languageParser.isEof()) + eval(languageParser.getNextSExpression()); + } + } + public void interpret() { createParser(); @@ -60,4 +72,23 @@ public class LispInterpreter { environment.getOutput().flush(); } + public static class LanguageFile { + + private InputStream inputStream; + private String name; + + public LanguageFile(InputStream inputStream, String name) { + this.inputStream = inputStream; + this.name = "lang:" + name; + } + + public InputStream getInputStream() { + return inputStream; + } + + public String getName() { + return name; + } + } + } diff --git a/src/interpreter/LispInterpreterBuilder.java b/src/interpreter/LispInterpreterBuilder.java index 3a3939d..c21a02c 100644 --- a/src/interpreter/LispInterpreterBuilder.java +++ b/src/interpreter/LispInterpreterBuilder.java @@ -20,6 +20,8 @@ public interface LispInterpreterBuilder { void useFile(String fileName); + void setLanguageFileNames(String... languageFiles); + void setPromptDecorator(Function decorator); void setValueOutputDecorator(Function decorator); diff --git a/src/interpreter/LispInterpreterBuilderImpl.java b/src/interpreter/LispInterpreterBuilderImpl.java index f7084dd..5524074 100644 --- a/src/interpreter/LispInterpreterBuilderImpl.java +++ b/src/interpreter/LispInterpreterBuilderImpl.java @@ -6,11 +6,14 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; import java.util.function.Function; import environment.RuntimeEnvironment; import error.CriticalLispException; import error.ErrorManager; +import interpreter.LispInterpreter.LanguageFile; public class LispInterpreterBuilderImpl implements LispInterpreterBuilder { @@ -26,6 +29,7 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder { private PrintStream errorOutputStream; private Runnable terminationFunction; private Runnable errorTerminationFunction; + private List languageFiles; private Function promptDecorator; private Function valueOutputDecorator; private Function warningOutputDecorator; @@ -42,6 +46,7 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder { this.isInteractive = true; this.isFileBased = false; this.isBuilt = false; + this.languageFiles = new ArrayList<>(); this.promptDecorator = s -> s; this.valueOutputDecorator = s -> s; this.warningOutputDecorator = s -> s; @@ -88,6 +93,15 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder { this.setNotInteractive(); } + @Override + public void setLanguageFileNames(String... languageFileNames) { + ClassLoader classLoader = getClass().getClassLoader(); + languageFiles = new ArrayList<>(); + + for (String fileName : languageFileNames) + languageFiles.add(new LanguageFile(classLoader.getResourceAsStream(fileName), fileName)); + } + @Override public void setPromptDecorator(Function decorator) { this.promptDecorator = decorator; @@ -126,6 +140,7 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder { private LispInterpreter buildInterpreter() { configureRuntimeEnvironment(); LispInterpreter lispInterpreter = createInterpreter(); + lispInterpreter.interpretLanguageFiles(languageFiles); isBuilt = true; return lispInterpreter; diff --git a/src/main/LispMain.java b/src/main/LispMain.java index 80b0574..837af64 100644 --- a/src/main/LispMain.java +++ b/src/main/LispMain.java @@ -31,6 +31,8 @@ public class LispMain { public static final String ANSI_YELLOW = "\u001B[33m"; public static final String ANSI_PURPLE = "\u001B[35m"; + public static final String[] LANGUAGE_FILE_NAMES = new String[] { "functions.lisp", "dlambda.lisp" }; + public static void main(String[] arguments) { LispMain lispMain = new LispMain(); @@ -106,6 +108,7 @@ public class LispMain { builder.setErrorOutput(output); builder.setTerminationFunction(this::shutdownTerminal); builder.setErrorTerminationFunction(this::shutdownTerminal); + builder.setLanguageFileNames(LANGUAGE_FILE_NAMES); builder.setPromptDecorator(s -> s + END_OF_SEGMENT); builder.setValueOutputDecorator(makeColorDecorator(ANSI_GREEN)); builder.setWarningOutputDecorator(makeColorDecorator(ANSI_YELLOW)); @@ -141,6 +144,7 @@ public class LispMain { builder.setErrorOutput(System.err); builder.setTerminationFunction(() -> System.exit(0)); builder.setErrorTerminationFunction(() -> System.exit(1)); + builder.setLanguageFileNames(LANGUAGE_FILE_NAMES); builder.setValueOutputDecorator(makeColorDecorator(ANSI_GREEN)); builder.setWarningOutputDecorator(makeColorDecorator(ANSI_YELLOW)); builder.setErrorOutputDecorator(makeColorDecorator(ANSI_RED)); diff --git a/test/acceptance/fixture/LispInterpreterFixture.java b/test/acceptance/fixture/LispInterpreterFixture.java index e6ce777..0845c92 100644 --- a/test/acceptance/fixture/LispInterpreterFixture.java +++ b/test/acceptance/fixture/LispInterpreterFixture.java @@ -1,5 +1,6 @@ package acceptance.fixture; +import static main.LispMain.LANGUAGE_FILE_NAMES; import static table.FunctionTable.resetFunctionTable; import static util.Path.getPathPrefix; @@ -22,11 +23,23 @@ public class LispInterpreterFixture { private static RuntimeEnvironment environment = RuntimeEnvironment.getInstance(); private static LispInterpreter interpreter = null; + public static void refreshInterpreter() { + cleanUp(); + buildInterpreter(); + } + + public static void cleanUp() { + resetFunctionTable(); + executionContext.clearContext(); + environment.reset(); + } + public static void buildInterpreter() { LispInterpreterBuilder builder = new LispInterpreterBuilderImpl() {}; builder.setOutput(new PrintStream(outputStream)); builder.setErrorOutput(new PrintStream(outputStream)); builder.setNotInteractive(); + builder.setLanguageFileNames(LANGUAGE_FILE_NAMES); builder.setTerminationFunction(LispInterpreterFixture::terminate); builder.setErrorTerminationFunction(LispInterpreterFixture::terminateFromError); @@ -39,16 +52,6 @@ public class LispInterpreterFixture { throw new RuntimeException("Error Termination"); } - public static void cleanUp() { - clearFunctionsAndSymbols(); - environment.reset(); - } - - public static void clearFunctionsAndSymbols() { - resetFunctionTable(); - executionContext.clearContext(); - } - public String evaluateText(String input) { environment.setInputName("fitnesse"); environment.setInput(new ByteArrayInputStream(input.getBytes())); diff --git a/test/main/MainTest.java b/test/main/MainTest.java index 7ef6d74..8c89602 100644 --- a/test/main/MainTest.java +++ b/test/main/MainTest.java @@ -12,8 +12,6 @@ import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.util.concurrent.CountDownLatch; -import org.junit.After; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.contrib.java.lang.system.ExpectedSystemExit; @@ -26,8 +24,9 @@ import environment.RuntimeEnvironment; import interpreter.LispInterpreterBuilderImpl; import terminal.TerminalConfiguration; import terminal.VirtualTerminalInteractor; +import testutil.SymbolAndFunctionCleaner; -public class MainTest { +public class MainTest extends SymbolAndFunctionCleaner { private RuntimeEnvironment environment; private CountDownLatch latch; @@ -89,13 +88,13 @@ public class MainTest { @Rule public SystemOutRule systemOutRule = new SystemOutRule().enableLog().mute(); - @Before - public void setUp() { + @Override + public void additionalSetUp() { environment.reset(); } - @After - public void tearDown() { + @Override + public void additionalTearDown() { environment.reset(); }