Restore main function

- Remove MessageFormat usages in kotlin where possible
 - Code cleanup
This commit is contained in:
Mike Cifelli 2018-10-20 10:09:53 -04:00
parent 75f02a89cc
commit 347857fbb5
7 changed files with 49 additions and 70 deletions

View File

@ -96,13 +96,14 @@ class LispMain constructor(var configuration: TerminalConfiguration = TerminalCo
val LANGUAGE_FILE_NAMES = arrayOf("functions.lisp", "dlambda.lisp") val LANGUAGE_FILE_NAMES = arrayOf("functions.lisp", "dlambda.lisp")
@JvmStatic
fun main(arguments: Array<String>) { fun main(arguments: Array<String>) {
val lispMain = LispMain() with(LispMain()) {
if (arguments.isEmpty()) if (arguments.isEmpty())
lispMain.runInteractive() runInteractive()
else else
lispMain.runWithFile(arguments[0]) runWithFile(arguments[0])
}
} }
} }
} }

View File

@ -6,7 +6,6 @@ import sexpression.Cons
import sexpression.DisplayName import sexpression.DisplayName
import sexpression.SExpression import sexpression.SExpression
import java.math.BigInteger import java.math.BigInteger
import java.text.MessageFormat.format
class ArgumentValidator(private val functionName: String) { class ArgumentValidator(private val functionName: String) {
@ -120,37 +119,25 @@ class ArgumentValidator(private val functionName: String) {
class TooFewArgumentsException(functionName: String, argumentList: Cons) : LispException() { class TooFewArgumentsException(functionName: String, argumentList: Cons) : LispException() {
override val message: String by lazy { override val message = "too few arguments given to $functionName: $argumentList"
format("too few arguments given to {0}: {1}", functionName, argumentList)
}
} }
class TooManyArgumentsException(functionName: String, argumentList: Cons) : LispException() { class TooManyArgumentsException(functionName: String, argumentList: Cons) : LispException() {
override val message: String by lazy { override val message = "too many arguments given to $functionName: $argumentList"
format("too many arguments given to {0}: {1}", functionName, argumentList)
}
} }
class DottedArgumentListException(functionName: String, argumentList: Cons) : LispException() { class DottedArgumentListException(functionName: String, argumentList: Cons) : LispException() {
override val message: String by lazy { override val message = "dotted argument list given to $functionName: $argumentList"
format("dotted argument list given to {0}: {1}", functionName, argumentList)
}
} }
class BadArgumentTypeException(functionName: String, class BadArgumentTypeException(functionName: String,
argument: SExpression, argument: SExpression,
expectedType: Class<out SExpression>?) : LispException() { expectedType: Class<out SExpression>) : LispException() {
private val expectedTypeName: String by lazy { private val expectedTypeName = expectedType.getAnnotation(DisplayName::class.java)?.value ?: "unknown"
val displayName = expectedType?.getAnnotation(DisplayName::class.java)
displayName?.value ?: "unknown" override val message = "$functionName: $argument is not the expected type of '$expectedTypeName'"
}
override val message: String by lazy {
format("{0}: {1} is not the expected type of ''{2}''", functionName, argument.toString(), expectedTypeName)
}
} }
} }

View File

@ -8,7 +8,6 @@ import sexpression.SExpression
import sexpression.Symbol import sexpression.Symbol
import table.ExecutionContext import table.ExecutionContext
import table.SymbolTable import table.SymbolTable
import java.text.MessageFormat.format
open class UserDefinedFunction(private val name: String, lambdaList: Cons, private val body: Cons) : LispFunction() { 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) } private fun evaluateBody() = body.fold(Nil as SExpression) { _, cons -> eval(cons.first) }
class IllegalKeywordRestPositionException(private val functionName: String, class IllegalKeywordRestPositionException(functionName: String, parameters: Cons) : LispException() {
private val parameters: Cons) : LispException() {
override val message: String by lazy { override val message =
format("unexpected parameters following ''&rest'' in definition of {0}: {1}", functionName, parameters) "unexpected parameters following '$KEYWORD_REST' in definition of $functionName: $parameters"
}
} }
companion object { companion object {

View File

@ -1,19 +1,15 @@
package sexpression package sexpression
import error.LispException import error.LispException
import java.math.BigInteger import java.math.BigInteger
import java.text.MessageFormat.format
@DisplayName("number") @DisplayName("number")
class LispNumber : Atom { class LispNumber : Atom {
var value: BigInteger? = null
private set
override val isNumber = true override val isNumber = true
val value: BigInteger
constructor(text: String) : super(text.replaceFirst("^0+(?!$)".toRegex(), "")) { constructor(text: String) : super(text.replaceFirst("^0+(?!$)".toRegex(), "")) {
try { try {
this.value = BigInteger(text) this.value = BigInteger(text)
@ -26,13 +22,13 @@ class LispNumber : Atom {
this.value = value 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 { companion object {
val ZERO = LispNumber(BigInteger.ZERO) val ZERO = LispNumber(BigInteger.ZERO)
val ONE = LispNumber(BigInteger.ONE) val ONE = LispNumber(BigInteger.ONE)
} }
class InvalidNumberException(text: String) : LispException() {
override val message = "$text is not a valid integer"
}
} }

View File

@ -20,24 +20,22 @@ import java.util.concurrent.Executors
class LispTerminal(configuration: TerminalConfiguration) { 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 inputLine = ""
private var outputSegment = "" private var outputSegment = ""
private var isStopped = false private var isStopped = false
private var originColumn = 0 private var originColumn = 0
private var originRow = 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 { init {
setOriginToCurrentPosition() setOriginToCurrentPosition()
terminal.addResizeListener { _, _ -> resize() }
} }
@Synchronized @Synchronized

View File

@ -16,10 +16,10 @@ import java.util.Arrays
object TestUtilities { object TestUtilities {
@JvmStatic() @JvmStatic
fun createInputStreamFromString(string: String) = ByteArrayInputStream(string.toByteArray()) fun createInputStreamFromString(string: String) = ByteArrayInputStream(string.toByteArray())
@JvmStatic() @JvmStatic
fun createIOExceptionThrowingInputStream() = object : InputStream() { fun createIOExceptionThrowingInputStream() = object : InputStream() {
override fun read(): Int { override fun read(): Int {
@ -31,7 +31,7 @@ object TestUtilities {
} }
} }
@JvmStatic() @JvmStatic
fun createIOExceptionThrowingOutputStream() = object : OutputStream() { fun createIOExceptionThrowingOutputStream() = object : OutputStream() {
override fun write(b: ByteArray) { override fun write(b: ByteArray) {
@ -51,17 +51,17 @@ object TestUtilities {
} }
} }
@JvmStatic() @JvmStatic
fun evaluateString(input: String): SExpression = eval(parseString(input)) fun evaluateString(input: String): SExpression = eval(parseString(input))
@JvmStatic() @JvmStatic
fun parseString(input: String): SExpression { fun parseString(input: String): SExpression {
val stringInputStream = TestUtilities.createInputStreamFromString(input) val stringInputStream = TestUtilities.createInputStreamFromString(input)
return LispParser(stringInputStream, "testFile").nextSExpression() return LispParser(stringInputStream, "testFile").nextSExpression()
} }
@JvmStatic() @JvmStatic
fun makeList(vararg expressionList: SExpression): Cons { fun makeList(vararg expressionList: SExpression): Cons {
if (expressionList.isEmpty()) if (expressionList.isEmpty())
return Nil return Nil
@ -71,17 +71,17 @@ object TestUtilities {
return Cons(expressionList[0], rest) return Cons(expressionList[0], rest)
} }
@JvmStatic() @JvmStatic
fun assertSExpressionsMatch(expected: SExpression, actual: SExpression) { fun assertSExpressionsMatch(expected: SExpression, actual: SExpression) {
assertThat(actual.toString()).isEqualTo(expected.toString()) assertThat(actual.toString()).isEqualTo(expected.toString())
} }
@JvmStatic() @JvmStatic
fun assertSExpressionsDoNotMatch(unexpected: SExpression, actual: SExpression) { fun assertSExpressionsDoNotMatch(unexpected: SExpression, actual: SExpression) {
assertThat(actual.toString()).isNotEqualTo(unexpected.toString()) assertThat(actual.toString()).isNotEqualTo(unexpected.toString())
} }
@JvmStatic() @JvmStatic
fun assertIsErrorWithMessage(e: LispException) { fun assertIsErrorWithMessage(e: LispException) {
assertThat(e.severity).isEqualTo(ERROR) assertThat(e.severity).isEqualTo(ERROR)
assertThat(e.message).isNotEmpty() assertThat(e.message).isNotEmpty()

View File

@ -7,7 +7,7 @@ import sexpression.Symbol.Companion.T
object TypeAssertions { object TypeAssertions {
@JvmStatic() @JvmStatic
fun assertList(sExpression: SExpression) { fun assertList(sExpression: SExpression) {
assertThat(sExpression.isAtom).isFalse() assertThat(sExpression.isAtom).isFalse()
assertThat(sExpression.isCons).isTrue() assertThat(sExpression.isCons).isTrue()
@ -22,7 +22,7 @@ object TypeAssertions {
assertThat(sExpression.isAtSign).isFalse() assertThat(sExpression.isAtSign).isFalse()
} }
@JvmStatic() @JvmStatic
fun assertNil(sExpression: SExpression) { fun assertNil(sExpression: SExpression) {
assertThat(sExpression).isEqualTo(Nil) assertThat(sExpression).isEqualTo(Nil)
@ -39,7 +39,7 @@ object TypeAssertions {
assertThat(sExpression.isAtSign).isFalse() assertThat(sExpression.isAtSign).isFalse()
} }
@JvmStatic() @JvmStatic
fun assertNumber(sExpression: SExpression) { fun assertNumber(sExpression: SExpression) {
assertThat(sExpression.isAtom).isTrue() assertThat(sExpression.isAtom).isTrue()
assertThat(sExpression.isCons).isFalse() assertThat(sExpression.isCons).isFalse()
@ -54,7 +54,7 @@ object TypeAssertions {
assertThat(sExpression.isAtSign).isFalse() assertThat(sExpression.isAtSign).isFalse()
} }
@JvmStatic() @JvmStatic
fun assertString(sExpression: SExpression) { fun assertString(sExpression: SExpression) {
assertThat(sExpression.isAtom).isTrue() assertThat(sExpression.isAtom).isTrue()
assertThat(sExpression.isCons).isFalse() assertThat(sExpression.isCons).isFalse()
@ -69,7 +69,7 @@ object TypeAssertions {
assertThat(sExpression.isAtSign).isFalse() assertThat(sExpression.isAtSign).isFalse()
} }
@JvmStatic() @JvmStatic
fun assertSymbol(sExpression: SExpression) { fun assertSymbol(sExpression: SExpression) {
assertThat(sExpression.isAtom).isTrue() assertThat(sExpression.isAtom).isTrue()
assertThat(sExpression.isCons).isFalse() assertThat(sExpression.isCons).isFalse()
@ -84,12 +84,12 @@ object TypeAssertions {
assertThat(sExpression.isAtSign).isFalse() assertThat(sExpression.isAtSign).isFalse()
} }
@JvmStatic() @JvmStatic
fun assertT(sExpression: SExpression) { fun assertT(sExpression: SExpression) {
assertThat(sExpression).isEqualTo(T) assertThat(sExpression).isEqualTo(T)
} }
@JvmStatic() @JvmStatic
fun assertBackTickExpression(sExpression: SExpression) { fun assertBackTickExpression(sExpression: SExpression) {
assertThat(sExpression.isAtom).isFalse() assertThat(sExpression.isAtom).isFalse()
assertThat(sExpression.isCons).isFalse() assertThat(sExpression.isCons).isFalse()
@ -104,7 +104,7 @@ object TypeAssertions {
assertThat(sExpression.isAtSign).isFalse() assertThat(sExpression.isAtSign).isFalse()
} }
@JvmStatic() @JvmStatic
fun assertCommaExpression(sExpression: SExpression) { fun assertCommaExpression(sExpression: SExpression) {
assertThat(sExpression.isAtom).isFalse() assertThat(sExpression.isAtom).isFalse()
assertThat(sExpression.isCons).isFalse() assertThat(sExpression.isCons).isFalse()
@ -119,7 +119,7 @@ object TypeAssertions {
assertThat(sExpression.isAtSign).isFalse() assertThat(sExpression.isAtSign).isFalse()
} }
@JvmStatic() @JvmStatic
fun assertAtSignExpression(sExpression: SExpression) { fun assertAtSignExpression(sExpression: SExpression) {
assertThat(sExpression.isAtom).isFalse() assertThat(sExpression.isAtom).isFalse()
assertThat(sExpression.isCons).isFalse() assertThat(sExpression.isCons).isFalse()