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")
@JvmStatic
fun main(arguments: Array<String>) {
val lispMain = LispMain()
with(LispMain()) {
if (arguments.isEmpty())
lispMain.runInteractive()
runInteractive()
else
lispMain.runWithFile(arguments[0])
runWithFile(arguments[0])
}
}
}
}

View File

@ -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<out SExpression>?) : LispException() {
expectedType: Class<out SExpression>) : 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'"
}
}

View File

@ -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 {

View File

@ -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"
}
}

View File

@ -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

View File

@ -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()

View File

@ -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()