Refactor table code / update kotlin version
This commit is contained in:
parent
a2f8086b8e
commit
fd322a385f
2
pom.xml
2
pom.xml
|
@ -10,7 +10,7 @@
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<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>
|
<junit5.version>5.1.0</junit5.version>
|
||||||
<skipTests>false</skipTests>
|
<skipTests>false</skipTests>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
|
@ -15,14 +15,9 @@ object ExecutionContext {
|
||||||
var isRecur: Boolean = false
|
var isRecur: Boolean = false
|
||||||
private set
|
private set
|
||||||
|
|
||||||
val isInFunctionCall: Boolean
|
fun isInFunctionCall() = !functionCalls.empty()
|
||||||
get() = !functionCalls.empty()
|
fun isRecurInitializing() = functionCalls.peek().isRecurInitializing
|
||||||
|
fun getCurrentFunction() = functionCalls.peek().lispFunction
|
||||||
val currentFunction: LispFunction
|
|
||||||
get() = functionCalls.peek().lispFunction
|
|
||||||
|
|
||||||
val isRecurInitializing: Boolean
|
|
||||||
get() = functionCalls.peek().isRecurInitializing
|
|
||||||
|
|
||||||
fun clearContext() {
|
fun clearContext() {
|
||||||
scope = SymbolTable(NullSymbolTable)
|
scope = SymbolTable(NullSymbolTable)
|
||||||
|
@ -31,7 +26,7 @@ object ExecutionContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun restoreGlobalScope() {
|
fun restoreGlobalScope() {
|
||||||
while (!scope.isGlobal)
|
while (!scope.isGlobal())
|
||||||
scope = scope.parent ?: NullSymbolTable
|
scope = scope.parent ?: NullSymbolTable
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,23 +64,14 @@ object ExecutionContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setRecurInitializing() {
|
fun setRecurInitializing() {
|
||||||
functionCalls.peek().setRecurInitializing()
|
functionCalls.peek().isRecurInitializing = true
|
||||||
}
|
}
|
||||||
|
|
||||||
fun clearRecurInitializing() {
|
fun clearRecurInitializing() {
|
||||||
functionCalls.peek().clearRecurInitializing()
|
functionCalls.peek().isRecurInitializing = false
|
||||||
}
|
}
|
||||||
|
|
||||||
class LispFunctionRecurInfo(val lispFunction: LispFunction) {
|
private class LispFunctionRecurInfo(val lispFunction: LispFunction) {
|
||||||
var isRecurInitializing: Boolean = false
|
var isRecurInitializing: Boolean = false
|
||||||
private set
|
|
||||||
|
|
||||||
fun setRecurInitializing() {
|
|
||||||
isRecurInitializing = true
|
|
||||||
}
|
|
||||||
|
|
||||||
fun clearRecurInitializing() {
|
|
||||||
isRecurInitializing = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,28 +12,22 @@ import kotlin.collections.Map.Entry
|
||||||
|
|
||||||
open class SymbolTable @JvmOverloads constructor(open val parent: SymbolTable? = NullSymbolTable) : Iterable<SymbolTable> {
|
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 table: HashMap<String, SExpression> = HashMap()
|
||||||
|
|
||||||
private val sortedBindings: Set<Entry<String, SExpression>>
|
|
||||||
get() = TreeMap(table).entries
|
|
||||||
|
|
||||||
override fun iterator(): Iterator<SymbolTable> = SymbolTableIterator(this)
|
override fun iterator(): Iterator<SymbolTable> = SymbolTableIterator(this)
|
||||||
|
operator fun contains(symbolName: String) = symbolName in table
|
||||||
operator fun contains(symbolName: String) = table.containsKey(symbolName)
|
|
||||||
|
|
||||||
operator fun get(symbolName: String) = table[symbolName]
|
operator fun get(symbolName: String) = table[symbolName]
|
||||||
|
|
||||||
operator fun set(symbolName: String, value: SExpression) {
|
operator fun set(symbolName: String, value: SExpression) {
|
||||||
table[symbolName] = value
|
table[symbolName] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun isGlobal() = parent === NullSymbolTable
|
||||||
|
|
||||||
fun toList(): Cons {
|
fun toList(): Cons {
|
||||||
var context: Cons = NIL
|
var context: Cons = NIL
|
||||||
|
|
||||||
for (binding in sortedBindings)
|
for (binding in TreeMap(table).entries)
|
||||||
context = append(context, makeList(makeSymbolValuePair(binding)))
|
context = append(context, makeList(makeSymbolValuePair(binding)))
|
||||||
|
|
||||||
return context
|
return context
|
||||||
|
@ -43,11 +37,11 @@ open class SymbolTable @JvmOverloads constructor(open val parent: SymbolTable? =
|
||||||
Cons(Symbol(binding.key), makeList(binding.value))
|
Cons(Symbol(binding.key), makeList(binding.value))
|
||||||
|
|
||||||
object NullSymbolTable : SymbolTable(null) {
|
object NullSymbolTable : SymbolTable(null) {
|
||||||
override val parent: SymbolTable
|
override val parent = this
|
||||||
get() = this
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class SymbolTableIterator(private var symbolTable: SymbolTable) : AbstractIterator<SymbolTable>() {
|
private class SymbolTableIterator(private var symbolTable: SymbolTable) : AbstractIterator<SymbolTable>() {
|
||||||
|
|
||||||
override fun computeNext() {
|
override fun computeNext() {
|
||||||
when (symbolTable) {
|
when (symbolTable) {
|
||||||
is NullSymbolTable -> done()
|
is NullSymbolTable -> done()
|
||||||
|
|
|
@ -92,9 +92,9 @@ class ExecutionContextTest {
|
||||||
val scope3 = SymbolTable(scope2)
|
val scope3 = SymbolTable(scope2)
|
||||||
executionContext.scope = scope3
|
executionContext.scope = scope3
|
||||||
|
|
||||||
assertThat(executionContext.scope.isGlobal).isFalse()
|
assertThat(executionContext.scope.isGlobal()).isFalse()
|
||||||
executionContext.restoreGlobalScope()
|
executionContext.restoreGlobalScope()
|
||||||
assertThat(executionContext.scope.isGlobal).isTrue()
|
assertThat(executionContext.scope.isGlobal()).isTrue()
|
||||||
assertThat(executionContext.scope).isEqualTo(global)
|
assertThat(executionContext.scope).isEqualTo(global)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ import org.junit.jupiter.api.Test
|
||||||
import org.junit.jupiter.api.TestInstance
|
import org.junit.jupiter.api.TestInstance
|
||||||
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
|
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
|
||||||
import sexpression.Cons
|
import sexpression.Cons
|
||||||
|
import sexpression.LispString
|
||||||
import sexpression.Nil
|
import sexpression.Nil
|
||||||
import sexpression.Nil.NIL
|
import sexpression.Nil.NIL
|
||||||
import sexpression.Symbol.T
|
import sexpression.Symbol.T
|
||||||
|
@ -25,8 +26,8 @@ import java.util.HashSet
|
||||||
class FunctionTableTest {
|
class FunctionTableTest {
|
||||||
|
|
||||||
@FunctionNames("GOOD")
|
@FunctionNames("GOOD")
|
||||||
class GoodFunction(name: String) : LispFunction() {
|
class GoodFunction(private val name: String) : LispFunction() {
|
||||||
override fun call(argumentList: Cons): Nil = NIL
|
override fun call(argumentList: Cons): LispString = LispString(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
@FunctionNames("BAD")
|
@FunctionNames("BAD")
|
||||||
|
|
Loading…
Reference in New Issue