Refactor table code / update kotlin version

This commit is contained in:
Mike Cifelli 2018-03-24 10:33:21 -04:00
parent a2f8086b8e
commit fd322a385f
5 changed files with 19 additions and 38 deletions

View File

@ -10,7 +10,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.2.30</kotlin.version>
<kotlin.version>1.2.31</kotlin.version>
<junit5.version>5.1.0</junit5.version>
<skipTests>false</skipTests>
</properties>

View File

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

View File

@ -12,28 +12,22 @@ import kotlin.collections.Map.Entry
open class SymbolTable @JvmOverloads constructor(open val parent: SymbolTable? = NullSymbolTable) : Iterable<SymbolTable> {
val isGlobal: Boolean
get() = parent === NullSymbolTable
private val table: HashMap<String, SExpression> = HashMap()
private val sortedBindings: Set<Entry<String, SExpression>>
get() = TreeMap(table).entries
override fun iterator(): Iterator<SymbolTable> = 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<SymbolTable>() {
override fun computeNext() {
when (symbolTable) {
is NullSymbolTable -> done()

View File

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

View File

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