refactor: Herstructurering
This commit is contained in:
parent
1acd1cfb67
commit
e3c84e1761
33 changed files with 290 additions and 178 deletions
38
src/prolog/ast/terms/Variable.kt
Normal file
38
src/prolog/ast/terms/Variable.kt
Normal file
|
@ -0,0 +1,38 @@
|
|||
package prolog.ast.terms
|
||||
|
||||
import java.util.*
|
||||
|
||||
data class Variable(val name: String) : Term {
|
||||
private var alias: Optional<Term> = Optional.empty()
|
||||
|
||||
fun alias(): Optional<Term> {
|
||||
return alias
|
||||
}
|
||||
|
||||
fun bind(term: Term): Optional<Term> {
|
||||
if (alias.isEmpty) {
|
||||
alias = Optional.of(term)
|
||||
}
|
||||
|
||||
return alias
|
||||
}
|
||||
|
||||
fun unbind() {
|
||||
alias = Optional.empty()
|
||||
}
|
||||
|
||||
override fun compareTo(other: Term): Int {
|
||||
return when (other) {
|
||||
is Variable -> name.compareTo(other.name)
|
||||
// Variables are always less than atoms
|
||||
else -> -1
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return when {
|
||||
alias.isPresent -> "$name: ${alias.get()}"
|
||||
else -> name
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue