diff --git a/.classpath b/.classpath index b52a479..5e3662d 100644 --- a/.classpath +++ b/.classpath @@ -2,11 +2,13 @@ + + diff --git a/.gitignore b/.gitignore index a61292a..c553637 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ build/ doc/ +fitnesse/FitNesseRoot/files jar/ +*.zip *.swp diff --git a/acceptance/fixture/LispInterpreterFixture.java b/acceptance/fixture/LispInterpreterFixture.java new file mode 100644 index 0000000..50d53e7 --- /dev/null +++ b/acceptance/fixture/LispInterpreterFixture.java @@ -0,0 +1,48 @@ +package fixture; + +import java.io.*; + +import environment.RuntimeEnvironment; +import interpreter.*; + +public class LispInterpreterFixture { + + private ByteArrayOutputStream outputStream; + private RuntimeEnvironment environment; + private LispInterpreter interpreter; + + public LispInterpreterFixture() throws IOException { + this.outputStream = new ByteArrayOutputStream(); + this.environment = RuntimeEnvironment.getInstance(); + this.interpreter = buildInterpreter(); + } + + private LispInterpreter buildInterpreter() { + LispInterpreterBuilder builder = LispInterpreterBuilderImpl.getInstance(); + builder.setInputName("fitnesse"); + builder.setOutput(new PrintStream(outputStream)); + builder.setErrorOutput(new PrintStream(outputStream)); + builder.setNotInteractive(); + builder.setTerminationFunction(() -> System.exit(0)); + builder.setErrorTerminationFunction(() -> System.exit(1)); + + return builder.build(); + } + + public void input(String input) throws IOException { + environment.setInput(new ByteArrayInputStream(input.getBytes())); + interpreter.interpret(); + } + + public String output() { + String output = outputStream.toString(); + discardOutput(); + + return output; + } + + public void discardOutput() { + outputStream.reset(); + } + +} diff --git a/fitnesse/fitnesse-standalone.jar b/fitnesse/fitnesse-standalone.jar new file mode 100644 index 0000000..1effc39 Binary files /dev/null and b/fitnesse/fitnesse-standalone.jar differ diff --git a/src/interpreter/InteractiveLispInterpreter.java b/src/interpreter/InteractiveLispInterpreter.java index 14080a0..95cd1f5 100644 --- a/src/interpreter/InteractiveLispInterpreter.java +++ b/src/interpreter/InteractiveLispInterpreter.java @@ -7,26 +7,26 @@ public class InteractiveLispInterpreter extends LispInterpreter { @Override protected void printGreeting() { - environment.getOutput().println(GREETING); - environment.getOutput().println(); + output.println(GREETING); + output.println(); } @Override protected void displayPrompt() { - environment.getOutput().print(PROMPT); + output.print(PROMPT); } @Override protected void erasePrompt() { for (int i = 0; i < PROMPT.length(); i++) { - environment.getOutput().print("\b"); + output.print("\b"); } } @Override protected void printFarewell() { - environment.getOutput().println(); - environment.getOutput().println(); + output.println(); + output.println(); } } diff --git a/src/interpreter/LispInterpreter.java b/src/interpreter/LispInterpreter.java index 3462ec9..90c884a 100644 --- a/src/interpreter/LispInterpreter.java +++ b/src/interpreter/LispInterpreter.java @@ -2,6 +2,8 @@ package interpreter; import static function.builtin.EVAL.eval; +import java.io.PrintStream; + import environment.RuntimeEnvironment; import error.*; import parser.LispParser; @@ -11,15 +13,17 @@ public class LispInterpreter { protected RuntimeEnvironment environment; protected ErrorManager errorManager; + protected PrintStream output; private LispParser parser; public LispInterpreter() { this.environment = RuntimeEnvironment.getInstance(); this.errorManager = this.environment.getErrorManager(); - this.parser = new LispParser(this.environment.getInput(), this.environment.getInputName()); + this.output = environment.getOutput(); } public void interpret() { + createParser(); printGreeting(); for (displayPrompt(); !parser.isEof(); displayPrompt()) @@ -28,6 +32,10 @@ public class LispInterpreter { printFarewell(); } + private void createParser() { + parser = new LispParser(environment.getInput(), environment.getInputName()); + } + protected void printGreeting() {} protected void displayPrompt() {} @@ -46,13 +54,13 @@ public class LispInterpreter { String result = environment.decorateValueOutput(String.valueOf(eval(sExpression))); erasePrompt(); - environment.getOutput().println(result); + output.println(result); } protected void erasePrompt() {} protected void printFarewell() { - environment.getOutput().println(); + output.println(); } } diff --git a/src/interpreter/LispInterpreterBuilder.java b/src/interpreter/LispInterpreterBuilder.java index cad2659..6a8f8d9 100644 --- a/src/interpreter/LispInterpreterBuilder.java +++ b/src/interpreter/LispInterpreterBuilder.java @@ -16,6 +16,8 @@ public interface LispInterpreterBuilder { void setTerminationFunction(Runnable terminationFunction); void setErrorTerminationFunction(Runnable errorTerminationFunction); + + void setNotInteractive(); void useFile(String fileName); diff --git a/src/interpreter/LispInterpreterBuilderImpl.java b/src/interpreter/LispInterpreterBuilderImpl.java index 8cf7e17..eb87d60 100644 --- a/src/interpreter/LispInterpreterBuilderImpl.java +++ b/src/interpreter/LispInterpreterBuilderImpl.java @@ -27,12 +27,14 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder { private Function criticalOutputDecorator; private RuntimeEnvironment environment; private boolean isInteractive; + private boolean isUseFile; private boolean isBuilt; private LispInterpreterBuilderImpl() { this.environment = RuntimeEnvironment.getInstance(); this.inputName = ""; this.isInteractive = true; + this.isUseFile = false; this.isBuilt = false; this.outputDecorator = s -> s; this.valueOutputDecorator = s -> s; @@ -73,11 +75,17 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder { } @Override - public void useFile(String fileName) { - this.inputName = fileName; + public void setNotInteractive() { this.isInteractive = false; } + @Override + public void useFile(String fileName) { + this.isUseFile = true; + this.setInputName(fileName); + this.setNotInteractive(); + } + @Override public void setOutputDecorator(Function decorator) { this.outputDecorator = decorator; @@ -148,7 +156,7 @@ public class LispInterpreterBuilderImpl implements LispInterpreterBuilder { } private InputStream getInputStream() throws FileNotFoundException { - return isInteractive ? inputStream : new FileInputStream(inputName); + return isUseFile ? new FileInputStream(inputName) : inputStream; } private LispInterpreter createInterpreter() {