From fd322a385f9ff00257742b08d191dfa16e034e71 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Sat, 24 Mar 2018 10:33:21 -0400 Subject: [PATCH] Refactor table code / update kotlin version --- pom.xml | 2 +- src/main/kotlin/table/ExecutionContext.kt | 28 +++++-------------- src/main/kotlin/table/SymbolTable.kt | 18 ++++-------- src/test/kotlin/table/ExecutionContextTest.kt | 4 +-- src/test/kotlin/table/FunctionTableTest.kt | 5 ++-- 5 files changed, 19 insertions(+), 38 deletions(-) diff --git a/pom.xml b/pom.xml index d473dad..c05f3ef 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ UTF-8 - 1.2.30 + 1.2.31 5.1.0 false diff --git a/src/main/kotlin/table/ExecutionContext.kt b/src/main/kotlin/table/ExecutionContext.kt index 7b12b8e..aec7a42 100644 --- a/src/main/kotlin/table/ExecutionContext.kt +++ b/src/main/kotlin/table/ExecutionContext.kt @@ -15,14 +15,9 @@ object ExecutionContext { var isRecur: Boolean = false private set - val isInFunctionCall: Boolean - get() = !functionCalls.empty() - - val currentFunction: LispFunction - get() = functionCalls.peek().lispFunction - - val isRecurInitializing: Boolean - get() = functionCalls.peek().isRecurInitializing + fun isInFunctionCall() = !functionCalls.empty() + fun isRecurInitializing() = functionCalls.peek().isRecurInitializing + fun getCurrentFunction() = functionCalls.peek().lispFunction fun clearContext() { scope = SymbolTable(NullSymbolTable) @@ -31,7 +26,7 @@ object ExecutionContext { } fun restoreGlobalScope() { - while (!scope.isGlobal) + while (!scope.isGlobal()) scope = scope.parent ?: NullSymbolTable } @@ -69,23 +64,14 @@ object ExecutionContext { } fun setRecurInitializing() { - functionCalls.peek().setRecurInitializing() + functionCalls.peek().isRecurInitializing = true } fun clearRecurInitializing() { - functionCalls.peek().clearRecurInitializing() + functionCalls.peek().isRecurInitializing = false } - class LispFunctionRecurInfo(val lispFunction: LispFunction) { + private class LispFunctionRecurInfo(val lispFunction: LispFunction) { var isRecurInitializing: Boolean = false - private set - - fun setRecurInitializing() { - isRecurInitializing = true - } - - fun clearRecurInitializing() { - isRecurInitializing = false - } } } diff --git a/src/main/kotlin/table/SymbolTable.kt b/src/main/kotlin/table/SymbolTable.kt index 9738758..fc5e51b 100644 --- a/src/main/kotlin/table/SymbolTable.kt +++ b/src/main/kotlin/table/SymbolTable.kt @@ -12,28 +12,22 @@ import kotlin.collections.Map.Entry open class SymbolTable @JvmOverloads constructor(open val parent: SymbolTable? = NullSymbolTable) : Iterable { - val isGlobal: Boolean - get() = parent === NullSymbolTable - private val table: HashMap = HashMap() - private val sortedBindings: Set> - get() = TreeMap(table).entries - override fun iterator(): Iterator = SymbolTableIterator(this) - - operator fun contains(symbolName: String) = table.containsKey(symbolName) - + operator fun contains(symbolName: String) = symbolName in table operator fun get(symbolName: String) = table[symbolName] operator fun set(symbolName: String, value: SExpression) { table[symbolName] = value } + fun isGlobal() = parent === NullSymbolTable + fun toList(): Cons { var context: Cons = NIL - for (binding in sortedBindings) + for (binding in TreeMap(table).entries) context = append(context, makeList(makeSymbolValuePair(binding))) return context @@ -43,11 +37,11 @@ open class SymbolTable @JvmOverloads constructor(open val parent: SymbolTable? = Cons(Symbol(binding.key), makeList(binding.value)) object NullSymbolTable : SymbolTable(null) { - override val parent: SymbolTable - get() = this + override val parent = this } private class SymbolTableIterator(private var symbolTable: SymbolTable) : AbstractIterator() { + override fun computeNext() { when (symbolTable) { is NullSymbolTable -> done() diff --git a/src/test/kotlin/table/ExecutionContextTest.kt b/src/test/kotlin/table/ExecutionContextTest.kt index a4ded4f..84b06f8 100644 --- a/src/test/kotlin/table/ExecutionContextTest.kt +++ b/src/test/kotlin/table/ExecutionContextTest.kt @@ -92,9 +92,9 @@ class ExecutionContextTest { val scope3 = SymbolTable(scope2) executionContext.scope = scope3 - assertThat(executionContext.scope.isGlobal).isFalse() + assertThat(executionContext.scope.isGlobal()).isFalse() executionContext.restoreGlobalScope() - assertThat(executionContext.scope.isGlobal).isTrue() + assertThat(executionContext.scope.isGlobal()).isTrue() assertThat(executionContext.scope).isEqualTo(global) } } diff --git a/src/test/kotlin/table/FunctionTableTest.kt b/src/test/kotlin/table/FunctionTableTest.kt index 27ec5dd..acab1dd 100644 --- a/src/test/kotlin/table/FunctionTableTest.kt +++ b/src/test/kotlin/table/FunctionTableTest.kt @@ -11,6 +11,7 @@ import org.junit.jupiter.api.Test import org.junit.jupiter.api.TestInstance import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS import sexpression.Cons +import sexpression.LispString import sexpression.Nil import sexpression.Nil.NIL import sexpression.Symbol.T @@ -25,8 +26,8 @@ import java.util.HashSet class FunctionTableTest { @FunctionNames("GOOD") - class GoodFunction(name: String) : LispFunction() { - override fun call(argumentList: Cons): Nil = NIL + class GoodFunction(private val name: String) : LispFunction() { + override fun call(argumentList: Cons): LispString = LispString(name) } @FunctionNames("BAD")