diff --git a/extend.lisp b/lisp/extend.lisp similarity index 100% rename from extend.lisp rename to lisp/extend.lisp diff --git a/fact.lisp b/lisp/fact.lisp similarity index 100% rename from fact.lisp rename to lisp/fact.lisp diff --git a/reverse.lisp b/lisp/reverse.lisp similarity index 100% rename from reverse.lisp rename to lisp/reverse.lisp diff --git a/test.lisp b/lisp/test.lisp similarity index 100% rename from test.lisp rename to lisp/test.lisp diff --git a/src/main/LispInterpreter.java b/src/main/LispInterpreter.java index 140ecac..154e1fe 100644 --- a/src/main/LispInterpreter.java +++ b/src/main/LispInterpreter.java @@ -21,7 +21,7 @@ import java.text.MessageFormat; */ public class LispInterpreter { - private static final String GREETING = "SUNY Potsdam Lisp Interpreter - Version 1.0.1"; + private static final String GREETING = "SUNY Potsdam Lisp Interpreter - Version 4.4.3"; private static final String PROMPT = "~ "; public static final String ANSI_RESET = "\u001B[0m"; diff --git a/src/scanner/LispFilterStream.java b/src/scanner/LispFilterInputStream.java similarity index 86% rename from src/scanner/LispFilterStream.java rename to src/scanner/LispFilterInputStream.java index 3758442..18b0794 100644 --- a/src/scanner/LispFilterStream.java +++ b/src/scanner/LispFilterInputStream.java @@ -5,14 +5,14 @@ import java.io.FilterInputStream; import java.io.IOException; /** - * Replaces Lisp comments with newlines in an input stream. + * Removes Lisp comments from an input stream. */ -public class LispFilterStream extends FilterInputStream { +public class LispFilterInputStream extends FilterInputStream { private boolean inQuote; private int nextCharacter; - public LispFilterStream(InputStream in) { + public LispFilterInputStream(InputStream in) { super(in); inQuote = false; nextCharacter = 0; diff --git a/src/scanner/LispScanner.java b/src/scanner/LispScanner.java index df6df9b..e1b0c61 100644 --- a/src/scanner/LispScanner.java +++ b/src/scanner/LispScanner.java @@ -18,7 +18,7 @@ import java.io.IOException; */ public class LispScanner { - private LispFilterStream inStream; + private LispFilterInputStream inStream; private Token currToken; private String fileName; private int line; @@ -35,7 +35,7 @@ public class LispScanner { * the name of the file that in is reading from */ public LispScanner(InputStream in, String fileName) { - this.inStream = new LispFilterStream(new BufferedInputStream(in)); + this.inStream = new LispFilterInputStream(new BufferedInputStream(in)); this.currToken = null; this.fileName = fileName; this.line = 1; diff --git a/test/scanner/LispFilterInputStreamTester.java b/test/scanner/LispFilterInputStreamTester.java new file mode 100644 index 0000000..5776ad2 --- /dev/null +++ b/test/scanner/LispFilterInputStreamTester.java @@ -0,0 +1,85 @@ +package scanner; + +import static org.junit.Assert.assertEquals; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; + +import org.junit.Before; +import org.junit.Test; + +public class LispFilterInputStreamTester { + + @Test + public void oneComment_Removed() throws IOException { + String input = ";comment"; + String expectedResult = ""; + assertEquals(expectedResult, getLispFilterInputStreamResult(input)); + } + + @Test + public void multipleComments_Removed() throws IOException { + String input = ";comment1\n;comment2\n;comment3"; + String expectedResult = "\n\n"; + assertEquals(expectedResult, getLispFilterInputStreamResult(input)); + } + + @Test + public void nil_NotRemoved() throws IOException { + String input = "()"; + String expectedResult = "()"; + assertEquals(expectedResult, getLispFilterInputStreamResult(input)); + } + + @Test + public void interiorComment_Removed() throws IOException { + String input = "(;this is a comment\n)"; + String expectedResult = "(\n)"; + assertEquals(expectedResult, getLispFilterInputStreamResult(input)); + } + + @Test + public void commentInString_NotRemoved() throws IOException { + String input = "\"string;this should remain\""; + assertEquals(input, getLispFilterInputStreamResult(input)); + } + + @Test + public void commentInStringWithNewline_NotRemoved() throws IOException { + String input = "\"string;this should\n remain\""; + assertEquals(input, getLispFilterInputStreamResult(input)); + } + + @Test + public void manyCommentsWithStatements_OnlyCommentsRemoved() throws IOException { + String input = ";first comment \n '(1 2 3) \n ;second comment \n (defun add1 (x) (+ x 1)) ;third comment"; + String expectedResult = "\n '(1 2 3) \n \n (defun add1 (x) (+ x 1)) "; + assertEquals(expectedResult, getLispFilterInputStreamResult(input)); + } + + private String getLispFilterInputStreamResult(String inputString) throws IOException { + InputStream stringInputStream = createInputStreamFromString(inputString); + LispFilterInputStream lispFilterInputStream = new LispFilterInputStream(stringInputStream); + + return readInputStreamIntoString(lispFilterInputStream); + } + + private InputStream createInputStreamFromString(String string) { + return new ByteArrayInputStream(string.getBytes()); + } + + private String readInputStreamIntoString(InputStream inputStream) throws IOException { + StringBuilder stringBuilder = new StringBuilder(); + int c = inputStream.read(); + + while (c != -1) { + stringBuilder.append((char) c); + c = inputStream.read(); + } + + return stringBuilder.toString(); + } + +} diff --git a/test/scanner/LispFilterStreamTester.java b/test/scanner/LispFilterStreamTester.java deleted file mode 100644 index bf27687..0000000 --- a/test/scanner/LispFilterStreamTester.java +++ /dev/null @@ -1,22 +0,0 @@ -package scanner; - -import static org.junit.Assert.*; - -import org.junit.Before; -import org.junit.Test; - -public class LispFilterStreamTester { - - private LispFilterStream lispFilterStream; - - @Before - public void setUp() throws Exception { - lispFilterStream = new LispFilterStream(null); - } - - @Test - public void testRead() { - fail("Not yet implemented"); - } - -}