From 347857fbb50c701667e49ac375aec0c69da814ca Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Sat, 20 Oct 2018 10:09:53 -0400 Subject: [PATCH] Restore main function - Remove MessageFormat usages in kotlin where possible - Code cleanup --- src/main/kotlin/application/LispMain.kt | 13 +++++----- src/main/kotlin/function/ArgumentValidator.kt | 25 +++++-------------- .../kotlin/function/UserDefinedFunction.kt | 9 +++---- src/main/kotlin/sexpression/LispNumber.kt | 18 ++++++------- src/main/kotlin/terminal/LispTerminal.kt | 18 ++++++------- src/test/kotlin/testutil/TestUtilities.kt | 18 ++++++------- src/test/kotlin/testutil/TypeAssertions.kt | 18 ++++++------- 7 files changed, 49 insertions(+), 70 deletions(-) diff --git a/src/main/kotlin/application/LispMain.kt b/src/main/kotlin/application/LispMain.kt index f4be730..415688e 100644 --- a/src/main/kotlin/application/LispMain.kt +++ b/src/main/kotlin/application/LispMain.kt @@ -96,13 +96,14 @@ class LispMain constructor(var configuration: TerminalConfiguration = TerminalCo val LANGUAGE_FILE_NAMES = arrayOf("functions.lisp", "dlambda.lisp") + @JvmStatic fun main(arguments: Array) { - val lispMain = LispMain() - - if (arguments.isEmpty()) - lispMain.runInteractive() - else - lispMain.runWithFile(arguments[0]) + with(LispMain()) { + if (arguments.isEmpty()) + runInteractive() + else + runWithFile(arguments[0]) + } } } } diff --git a/src/main/kotlin/function/ArgumentValidator.kt b/src/main/kotlin/function/ArgumentValidator.kt index 35cc2ed..96bd864 100644 --- a/src/main/kotlin/function/ArgumentValidator.kt +++ b/src/main/kotlin/function/ArgumentValidator.kt @@ -6,7 +6,6 @@ import sexpression.Cons import sexpression.DisplayName import sexpression.SExpression import java.math.BigInteger -import java.text.MessageFormat.format class ArgumentValidator(private val functionName: String) { @@ -120,37 +119,25 @@ class ArgumentValidator(private val functionName: String) { class TooFewArgumentsException(functionName: String, argumentList: Cons) : LispException() { - override val message: String by lazy { - format("too few arguments given to {0}: {1}", functionName, argumentList) - } + override val message = "too few arguments given to $functionName: $argumentList" } class TooManyArgumentsException(functionName: String, argumentList: Cons) : LispException() { - override val message: String by lazy { - format("too many arguments given to {0}: {1}", functionName, argumentList) - } + override val message = "too many arguments given to $functionName: $argumentList" } class DottedArgumentListException(functionName: String, argumentList: Cons) : LispException() { - override val message: String by lazy { - format("dotted argument list given to {0}: {1}", functionName, argumentList) - } + override val message = "dotted argument list given to $functionName: $argumentList" } class BadArgumentTypeException(functionName: String, argument: SExpression, - expectedType: Class?) : LispException() { + expectedType: Class) : LispException() { - private val expectedTypeName: String by lazy { - val displayName = expectedType?.getAnnotation(DisplayName::class.java) + private val expectedTypeName = expectedType.getAnnotation(DisplayName::class.java)?.value ?: "unknown" - displayName?.value ?: "unknown" - } - - override val message: String by lazy { - format("{0}: {1} is not the expected type of ''{2}''", functionName, argument.toString(), expectedTypeName) - } + override val message = "$functionName: $argument is not the expected type of '$expectedTypeName'" } } diff --git a/src/main/kotlin/function/UserDefinedFunction.kt b/src/main/kotlin/function/UserDefinedFunction.kt index 828fdab..e8de240 100644 --- a/src/main/kotlin/function/UserDefinedFunction.kt +++ b/src/main/kotlin/function/UserDefinedFunction.kt @@ -8,7 +8,6 @@ import sexpression.SExpression import sexpression.Symbol import table.ExecutionContext import table.SymbolTable -import java.text.MessageFormat.format open class UserDefinedFunction(private val name: String, lambdaList: Cons, private val body: Cons) : LispFunction() { @@ -98,12 +97,10 @@ open class UserDefinedFunction(private val name: String, lambdaList: Cons, priva private fun evaluateBody() = body.fold(Nil as SExpression) { _, cons -> eval(cons.first) } - class IllegalKeywordRestPositionException(private val functionName: String, - private val parameters: Cons) : LispException() { + class IllegalKeywordRestPositionException(functionName: String, parameters: Cons) : LispException() { - override val message: String by lazy { - format("unexpected parameters following ''&rest'' in definition of {0}: {1}", functionName, parameters) - } + override val message = + "unexpected parameters following '$KEYWORD_REST' in definition of $functionName: $parameters" } companion object { diff --git a/src/main/kotlin/sexpression/LispNumber.kt b/src/main/kotlin/sexpression/LispNumber.kt index b961e2d..e0ef649 100644 --- a/src/main/kotlin/sexpression/LispNumber.kt +++ b/src/main/kotlin/sexpression/LispNumber.kt @@ -1,19 +1,15 @@ package sexpression import error.LispException - import java.math.BigInteger -import java.text.MessageFormat.format - @DisplayName("number") class LispNumber : Atom { - var value: BigInteger? = null - private set - override val isNumber = true + val value: BigInteger + constructor(text: String) : super(text.replaceFirst("^0+(?!$)".toRegex(), "")) { try { this.value = BigInteger(text) @@ -26,13 +22,13 @@ class LispNumber : Atom { this.value = value } - class InvalidNumberException(private val text: String) : LispException() { - override val message: String - get() = format("{0} is not a valid integer", text) - } - companion object { val ZERO = LispNumber(BigInteger.ZERO) val ONE = LispNumber(BigInteger.ONE) } + + class InvalidNumberException(text: String) : LispException() { + + override val message = "$text is not a valid integer" + } } diff --git a/src/main/kotlin/terminal/LispTerminal.kt b/src/main/kotlin/terminal/LispTerminal.kt index 64cb3b5..c25cd41 100644 --- a/src/main/kotlin/terminal/LispTerminal.kt +++ b/src/main/kotlin/terminal/LispTerminal.kt @@ -20,24 +20,22 @@ import java.util.concurrent.Executors class LispTerminal(configuration: TerminalConfiguration) { - private val terminal = configuration.terminal!!.apply { - addResizeListener { _, _ -> resize() } - } - - private val inputWriter = SafeOutputStream(configuration.inputWriter) - private val outputReader = SafeInputStream(configuration.outputReader) - private val controlSequenceHandler = ControlSequenceHandler() - private val history = TerminalHistory() - private val executorService = Executors.newFixedThreadPool(2) - private var terminalSize = terminal.terminalSize private var inputLine = "" private var outputSegment = "" private var isStopped = false private var originColumn = 0 private var originRow = 0 + private val inputWriter = SafeOutputStream(configuration.inputWriter) + private val outputReader = SafeInputStream(configuration.outputReader) + private val controlSequenceHandler = ControlSequenceHandler() + private val history = TerminalHistory() + private val executorService = Executors.newFixedThreadPool(2) + private val terminal = configuration.terminal!! + private var terminalSize = terminal.terminalSize init { setOriginToCurrentPosition() + terminal.addResizeListener { _, _ -> resize() } } @Synchronized diff --git a/src/test/kotlin/testutil/TestUtilities.kt b/src/test/kotlin/testutil/TestUtilities.kt index 6e75a3c..5bfdb7f 100644 --- a/src/test/kotlin/testutil/TestUtilities.kt +++ b/src/test/kotlin/testutil/TestUtilities.kt @@ -16,10 +16,10 @@ import java.util.Arrays object TestUtilities { - @JvmStatic() + @JvmStatic fun createInputStreamFromString(string: String) = ByteArrayInputStream(string.toByteArray()) - @JvmStatic() + @JvmStatic fun createIOExceptionThrowingInputStream() = object : InputStream() { override fun read(): Int { @@ -31,7 +31,7 @@ object TestUtilities { } } - @JvmStatic() + @JvmStatic fun createIOExceptionThrowingOutputStream() = object : OutputStream() { override fun write(b: ByteArray) { @@ -51,17 +51,17 @@ object TestUtilities { } } - @JvmStatic() + @JvmStatic fun evaluateString(input: String): SExpression = eval(parseString(input)) - @JvmStatic() + @JvmStatic fun parseString(input: String): SExpression { val stringInputStream = TestUtilities.createInputStreamFromString(input) return LispParser(stringInputStream, "testFile").nextSExpression() } - @JvmStatic() + @JvmStatic fun makeList(vararg expressionList: SExpression): Cons { if (expressionList.isEmpty()) return Nil @@ -71,17 +71,17 @@ object TestUtilities { return Cons(expressionList[0], rest) } - @JvmStatic() + @JvmStatic fun assertSExpressionsMatch(expected: SExpression, actual: SExpression) { assertThat(actual.toString()).isEqualTo(expected.toString()) } - @JvmStatic() + @JvmStatic fun assertSExpressionsDoNotMatch(unexpected: SExpression, actual: SExpression) { assertThat(actual.toString()).isNotEqualTo(unexpected.toString()) } - @JvmStatic() + @JvmStatic fun assertIsErrorWithMessage(e: LispException) { assertThat(e.severity).isEqualTo(ERROR) assertThat(e.message).isNotEmpty() diff --git a/src/test/kotlin/testutil/TypeAssertions.kt b/src/test/kotlin/testutil/TypeAssertions.kt index 5390aaa..75005da 100644 --- a/src/test/kotlin/testutil/TypeAssertions.kt +++ b/src/test/kotlin/testutil/TypeAssertions.kt @@ -7,7 +7,7 @@ import sexpression.Symbol.Companion.T object TypeAssertions { - @JvmStatic() + @JvmStatic fun assertList(sExpression: SExpression) { assertThat(sExpression.isAtom).isFalse() assertThat(sExpression.isCons).isTrue() @@ -22,7 +22,7 @@ object TypeAssertions { assertThat(sExpression.isAtSign).isFalse() } - @JvmStatic() + @JvmStatic fun assertNil(sExpression: SExpression) { assertThat(sExpression).isEqualTo(Nil) @@ -39,7 +39,7 @@ object TypeAssertions { assertThat(sExpression.isAtSign).isFalse() } - @JvmStatic() + @JvmStatic fun assertNumber(sExpression: SExpression) { assertThat(sExpression.isAtom).isTrue() assertThat(sExpression.isCons).isFalse() @@ -54,7 +54,7 @@ object TypeAssertions { assertThat(sExpression.isAtSign).isFalse() } - @JvmStatic() + @JvmStatic fun assertString(sExpression: SExpression) { assertThat(sExpression.isAtom).isTrue() assertThat(sExpression.isCons).isFalse() @@ -69,7 +69,7 @@ object TypeAssertions { assertThat(sExpression.isAtSign).isFalse() } - @JvmStatic() + @JvmStatic fun assertSymbol(sExpression: SExpression) { assertThat(sExpression.isAtom).isTrue() assertThat(sExpression.isCons).isFalse() @@ -84,12 +84,12 @@ object TypeAssertions { assertThat(sExpression.isAtSign).isFalse() } - @JvmStatic() + @JvmStatic fun assertT(sExpression: SExpression) { assertThat(sExpression).isEqualTo(T) } - @JvmStatic() + @JvmStatic fun assertBackTickExpression(sExpression: SExpression) { assertThat(sExpression.isAtom).isFalse() assertThat(sExpression.isCons).isFalse() @@ -104,7 +104,7 @@ object TypeAssertions { assertThat(sExpression.isAtSign).isFalse() } - @JvmStatic() + @JvmStatic fun assertCommaExpression(sExpression: SExpression) { assertThat(sExpression.isAtom).isFalse() assertThat(sExpression.isCons).isFalse() @@ -119,7 +119,7 @@ object TypeAssertions { assertThat(sExpression.isAtSign).isFalse() } - @JvmStatic() + @JvmStatic fun assertAtSignExpression(sExpression: SExpression) { assertThat(sExpression.isAtom).isFalse() assertThat(sExpression.isCons).isFalse()