diff --git a/src/main/kotlin/interpreter/LispInterpreterBuilder.kt b/src/main/kotlin/interpreter/LispInterpreterBuilder.kt index f57b0ff..cccb58b 100644 --- a/src/main/kotlin/interpreter/LispInterpreterBuilder.kt +++ b/src/main/kotlin/interpreter/LispInterpreterBuilder.kt @@ -9,14 +9,13 @@ import java.io.FileInputStream import java.io.FileNotFoundException import java.io.InputStream import java.io.PrintStream -import java.util.ArrayList object LispInterpreterBuilder { private var inputName = "" private var isInteractive = true private var isFileBased = false - private var languageFiles = ArrayList() + private var languageFiles = mutableListOf() private var inputStream: InputStream? = null private var outputStream: PrintStream? = null private var errorOutputStream: PrintStream? = null @@ -36,7 +35,7 @@ object LispInterpreterBuilder { inputName = "" isInteractive = true isFileBased = false - languageFiles = ArrayList() + languageFiles = mutableListOf() inputStream = null outputStream = null errorOutputStream = null @@ -74,7 +73,7 @@ object LispInterpreterBuilder { fun setLanguageFileNames(vararg languageFiles: String) { val classLoader = javaClass.classLoader - this.languageFiles = ArrayList() + this.languageFiles = mutableListOf() for (fileName in languageFiles) this.languageFiles.add(LanguageFile(classLoader.getResourceAsStream(fileName), fileName)) diff --git a/src/main/kotlin/scanner/LispScanner.kt b/src/main/kotlin/scanner/LispScanner.kt index 3abd401..2cce7a4 100644 --- a/src/main/kotlin/scanner/LispScanner.kt +++ b/src/main/kotlin/scanner/LispScanner.kt @@ -11,7 +11,6 @@ import util.Characters.NEWLINE import util.Characters.isIdentifierCharacter import util.Characters.isNumberPrefix import java.io.InputStream -import java.lang.Character.isWhitespace /** * Converts a stream of bytes into a stream of Lisp tokens. @@ -28,7 +27,7 @@ class LispScanner(inputStream: InputStream, fileName: String) { val currentCharacter = c.toChar() positionTracker.incrementColumn() - if (!isWhitespace(currentCharacter)) + if (!currentCharacter.isWhitespace()) return createTokenFromCharacter(currentCharacter) else if (currentCharacter == NEWLINE) positionTracker.incrementLine() diff --git a/src/main/kotlin/table/FunctionTable.kt b/src/main/kotlin/table/FunctionTable.kt index 10b970d..6dc7cfa 100644 --- a/src/main/kotlin/table/FunctionTable.kt +++ b/src/main/kotlin/table/FunctionTable.kt @@ -50,63 +50,61 @@ import function.builtin.special.PROGN import function.builtin.special.QUOTE import function.builtin.special.RECUR import function.builtin.special.SETQ -import java.util.HashMap -import java.util.HashSet object FunctionTable { - private var table = HashMap() - private val allBuiltIns = HashSet>() + private val allBuiltIns = setOf( + AND::class.java, + APPEND::class.java, + APPLY::class.java, + ATOM::class.java, + CASE::class.java, + COND::class.java, + CONS::class.java, + DEFINE_SPECIAL::class.java, + DEFMACRO::class.java, + DEFUN::class.java, + DIVIDE::class.java, + EQ::class.java, + EQUAL::class.java, + NumericEqual::class.java, + EVAL::class.java, + Exit::class.java, + FIRST::class.java, + FUNCALL::class.java, + FUSE::class.java, + GENSYM::class.java, + GENSYM_EQUAL::class.java, + NUMERIC_GREATER::class.java, + IF::class.java, + Lambda::class.java, + LENGTH::class.java, + NUMERIC_LESS::class.java, + LET::class.java, + LET_STAR::class.java, + LIST::class.java, + LISTP::class.java, + LOAD::class.java, + MINUS::class.java, + MODULO::class.java, + MULTIPLY::class.java, + NULL::class.java, + OR::class.java, + PLUS::class.java, + PRINT::class.java, + PROGN::class.java, + QUOTE::class.java, + RECUR::class.java, + REMAINDER::class.java, + REST::class.java, + SET::class.java, + SETQ::class.java, + SYMBOL_FUNCTION::class.java, + SYMBOLS::class.java) + + private val table = mutableMapOf() init { - allBuiltIns.add(AND::class.java) - allBuiltIns.add(APPEND::class.java) - allBuiltIns.add(APPLY::class.java) - allBuiltIns.add(ATOM::class.java) - allBuiltIns.add(CASE::class.java) - allBuiltIns.add(COND::class.java) - allBuiltIns.add(CONS::class.java) - allBuiltIns.add(DEFINE_SPECIAL::class.java) - allBuiltIns.add(DEFMACRO::class.java) - allBuiltIns.add(DEFUN::class.java) - allBuiltIns.add(DIVIDE::class.java) - allBuiltIns.add(EQ::class.java) - allBuiltIns.add(EQUAL::class.java) - allBuiltIns.add(NumericEqual::class.java) - allBuiltIns.add(EVAL::class.java) - allBuiltIns.add(Exit::class.java) - allBuiltIns.add(FIRST::class.java) - allBuiltIns.add(FUNCALL::class.java) - allBuiltIns.add(FUSE::class.java) - allBuiltIns.add(GENSYM::class.java) - allBuiltIns.add(GENSYM_EQUAL::class.java) - allBuiltIns.add(NUMERIC_GREATER::class.java) - allBuiltIns.add(IF::class.java) - allBuiltIns.add(Lambda::class.java) - allBuiltIns.add(LENGTH::class.java) - allBuiltIns.add(NUMERIC_LESS::class.java) - allBuiltIns.add(LET::class.java) - allBuiltIns.add(LET_STAR::class.java) - allBuiltIns.add(LIST::class.java) - allBuiltIns.add(LISTP::class.java) - allBuiltIns.add(LOAD::class.java) - allBuiltIns.add(MINUS::class.java) - allBuiltIns.add(MODULO::class.java) - allBuiltIns.add(MULTIPLY::class.java) - allBuiltIns.add(NULL::class.java) - allBuiltIns.add(OR::class.java) - allBuiltIns.add(PLUS::class.java) - allBuiltIns.add(PRINT::class.java) - allBuiltIns.add(PROGN::class.java) - allBuiltIns.add(QUOTE::class.java) - allBuiltIns.add(RECUR::class.java) - allBuiltIns.add(REMAINDER::class.java) - allBuiltIns.add(REST::class.java) - allBuiltIns.add(SET::class.java) - allBuiltIns.add(SETQ::class.java) - allBuiltIns.add(SYMBOL_FUNCTION::class.java) - allBuiltIns.add(SYMBOLS::class.java) - initializeFunctionTable(allBuiltIns) } diff --git a/src/main/kotlin/table/SymbolTable.kt b/src/main/kotlin/table/SymbolTable.kt index fc5e51b..eea1a27 100644 --- a/src/main/kotlin/table/SymbolTable.kt +++ b/src/main/kotlin/table/SymbolTable.kt @@ -6,13 +6,11 @@ import sexpression.Cons import sexpression.Nil.NIL import sexpression.SExpression import sexpression.Symbol -import java.util.HashMap -import java.util.TreeMap import kotlin.collections.Map.Entry open class SymbolTable @JvmOverloads constructor(open val parent: SymbolTable? = NullSymbolTable) : Iterable { - private val table: HashMap = HashMap() + private val table = mutableMapOf() override fun iterator(): Iterator = SymbolTableIterator(this) operator fun contains(symbolName: String) = symbolName in table @@ -27,7 +25,7 @@ open class SymbolTable @JvmOverloads constructor(open val parent: SymbolTable? = fun toList(): Cons { var context: Cons = NIL - for (binding in TreeMap(table).entries) + for (binding in table.toSortedMap().entries) context = append(context, makeList(makeSymbolValuePair(binding))) return context diff --git a/src/main/kotlin/util/Characters.kt b/src/main/kotlin/util/Characters.kt index 40dabb0..f818758 100644 --- a/src/main/kotlin/util/Characters.kt +++ b/src/main/kotlin/util/Characters.kt @@ -1,7 +1,5 @@ package util -import java.util.HashSet - object Characters { const val EOF = -1 @@ -24,24 +22,21 @@ object Characters { const val UNICODE_ESCAPE = '\u001B' const val UNICODE_NULL = '\u0000' - private val illegalIdentifierCharacters = HashSet() + private val illegalIdentifierCharacters = + setOf(AT_SIGN, + BACKQUOTE, + BACKSLASH, + COMMA, + DOUBLE_QUOTE, + HASH, + LEFT_PARENTHESIS, + LEFT_SQUARE_BRACKET, + PERIOD, + RIGHT_PARENTHESIS, + RIGHT_SQUARE_BRACKET, + SEMICOLON, + SINGLE_QUOTE) - init { - illegalIdentifierCharacters.add(AT_SIGN) - illegalIdentifierCharacters.add(BACKQUOTE) - illegalIdentifierCharacters.add(BACKSLASH) - illegalIdentifierCharacters.add(COMMA) - illegalIdentifierCharacters.add(DOUBLE_QUOTE) - illegalIdentifierCharacters.add(HASH) - illegalIdentifierCharacters.add(LEFT_PARENTHESIS) - illegalIdentifierCharacters.add(LEFT_SQUARE_BRACKET) - illegalIdentifierCharacters.add(PERIOD) - illegalIdentifierCharacters.add(RIGHT_PARENTHESIS) - illegalIdentifierCharacters.add(RIGHT_SQUARE_BRACKET) - illegalIdentifierCharacters.add(SEMICOLON) - illegalIdentifierCharacters.add(SINGLE_QUOTE) - } - - fun isIdentifierCharacter(c: Char) = !Character.isWhitespace(c) && !illegalIdentifierCharacters.contains(c) + fun isIdentifierCharacter(c: Char) = !c.isWhitespace() && !illegalIdentifierCharacters.contains(c) fun isNumberPrefix(c: Char) = c == DASH || c == PLUS } diff --git a/src/test/kotlin/environment/RuntimeEnvironmentTest.kt b/src/test/kotlin/environment/RuntimeEnvironmentTest.kt index e1d21d9..55e0582 100644 --- a/src/test/kotlin/environment/RuntimeEnvironmentTest.kt +++ b/src/test/kotlin/environment/RuntimeEnvironmentTest.kt @@ -7,7 +7,6 @@ import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.junit.jupiter.api.TestInstance import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS -import java.util.HashSet @TestInstance(PER_CLASS) class RuntimeEnvironmentTest { @@ -17,7 +16,7 @@ class RuntimeEnvironmentTest { private const val TERMINATED_EXCEPTIONALLY = "TERMINATED_EXCEPTIONALLY" } - private val indicatorSet: MutableSet = HashSet() + private val indicatorSet = mutableSetOf() @BeforeEach fun setUp() { diff --git a/src/test/kotlin/error/ErrorManagerTest.kt b/src/test/kotlin/error/ErrorManagerTest.kt index 886d884..5d02b12 100644 --- a/src/test/kotlin/error/ErrorManagerTest.kt +++ b/src/test/kotlin/error/ErrorManagerTest.kt @@ -10,7 +10,6 @@ import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import java.io.ByteArrayOutputStream import java.io.PrintStream -import java.util.HashSet class ErrorManagerTest { @@ -19,7 +18,7 @@ class ErrorManagerTest { private const val MESSAGE = "message" } - private val indicatorSet: MutableSet = HashSet() + private val indicatorSet = mutableSetOf() private val errorOutputStream: ByteArrayOutputStream = ByteArrayOutputStream() private val outputStream: ByteArrayOutputStream = ByteArrayOutputStream() diff --git a/src/test/kotlin/function/builtin/ExitTest.kt b/src/test/kotlin/function/builtin/ExitTest.kt index 5cc669f..d7a51f5 100644 --- a/src/test/kotlin/function/builtin/ExitTest.kt +++ b/src/test/kotlin/function/builtin/ExitTest.kt @@ -9,7 +9,6 @@ import org.junit.jupiter.api.TestInstance import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS import testutil.SymbolAndFunctionCleaner import testutil.TestUtilities.evaluateString -import java.util.HashSet @TestInstance(PER_CLASS) class ExitTest : SymbolAndFunctionCleaner() { @@ -18,7 +17,7 @@ class ExitTest : SymbolAndFunctionCleaner() { private const val TERMINATED = "terminated" } - private var indicatorSet = HashSet() + private val indicatorSet = mutableSetOf() private fun assertTerminated() { assertThat(indicatorSet.contains(TERMINATED)).isTrue() diff --git a/src/test/kotlin/interpreter/LispInterpreterTest.kt b/src/test/kotlin/interpreter/LispInterpreterTest.kt index e5c2972..cd47e72 100644 --- a/src/test/kotlin/interpreter/LispInterpreterTest.kt +++ b/src/test/kotlin/interpreter/LispInterpreterTest.kt @@ -11,7 +11,6 @@ import org.junit.Test import testutil.TestUtilities.createInputStreamFromString import java.io.ByteArrayOutputStream import java.io.PrintStream -import java.util.HashSet class LispInterpreterTest { @@ -20,9 +19,9 @@ class LispInterpreterTest { private val FILE = LispInterpreterTest::class.java.getResource("file.lisp").file } - private var indicatorSet = HashSet() - private var outputStream = ByteArrayOutputStream() - private var errorOutputStream = ByteArrayOutputStream() + private val indicatorSet = mutableSetOf() + private val outputStream = ByteArrayOutputStream() + private val errorOutputStream = ByteArrayOutputStream() private fun setCommonFeatures() { LispInterpreterBuilder.setOutput(PrintStream(outputStream)) diff --git a/src/test/kotlin/scanner/LispScannerTypeTest.kt b/src/test/kotlin/scanner/LispScannerTypeTest.kt index f467408..d9a1089 100644 --- a/src/test/kotlin/scanner/LispScannerTypeTest.kt +++ b/src/test/kotlin/scanner/LispScannerTypeTest.kt @@ -20,7 +20,6 @@ import token.QuotedString import token.RightParenthesis import token.Token import token.TokenFactory.BadCharacterException -import java.util.ArrayList class LispScannerTypeTest { @@ -38,7 +37,7 @@ class LispScannerTypeTest { @Before fun setUp() { - expectedTypes = ArrayList() + expectedTypes = mutableListOf() } @Test diff --git a/src/test/kotlin/table/FunctionTableTest.kt b/src/test/kotlin/table/FunctionTableTest.kt index 3d510af..675ec6e 100644 --- a/src/test/kotlin/table/FunctionTableTest.kt +++ b/src/test/kotlin/table/FunctionTableTest.kt @@ -20,7 +20,6 @@ import table.FunctionTable.defineFunction import table.FunctionTable.isAlreadyDefined import table.FunctionTable.lookupFunction import table.FunctionTable.resetFunctionTable -import java.util.HashSet @TestInstance(PER_CLASS) class FunctionTableTest { @@ -101,8 +100,7 @@ class FunctionTableTest { @Test fun `reset function table with custom built-ins`() { - val goodBuiltIns = HashSet>() - goodBuiltIns.add(GoodFunction::class.java) + val goodBuiltIns = setOf(GoodFunction::class.java) resetFunctionTable(goodBuiltIns) @@ -112,8 +110,7 @@ class FunctionTableTest { @Test fun `unable to initialize a built-in function`() { - val badBuiltIns = HashSet>() - badBuiltIns.add(BadFunction::class.java) + val badBuiltIns = setOf(BadFunction::class.java) assertThrows(LispFunctionInstantiationException::class.java) { resetFunctionTable(badBuiltIns) } } @@ -128,8 +125,7 @@ class FunctionTableTest { @Test fun `built-in without a name doesn't throw an exception`() { - val namelessBuiltins = HashSet>() - namelessBuiltins.add(UglyFunction::class.java) + val namelessBuiltins = setOf(UglyFunction::class.java) resetFunctionTable(namelessBuiltins)