Checkpoint

This commit is contained in:
Tibo De Peuter 2025-05-01 17:13:35 +02:00
parent 43b364044e
commit 9db1c66781
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
34 changed files with 746 additions and 194 deletions

View file

@ -6,14 +6,15 @@ import io.Terminal
import parser.ReplParser
import prolog.Answer
import prolog.Answers
import prolog.Program
class Repl {
private val io = Terminal()
private val parser = ReplParser()
private val preprocessor = Preprocessor()
fun start() {
io.say("Prolog REPL. Type '^D' to quit.\n")
init {
welcome()
while (true) {
try {
printAnswers(query())
@ -23,15 +24,19 @@ class Repl {
}
}
fun query(): Answers {
private fun welcome() {
io.checkNewLine()
io.say("Prolog REPL. Type '^D' to quit.\n")
}
private fun query(): Answers {
val queryString = io.prompt("?-", { "| " })
val simpleQuery = parser.parse(queryString)
val query = preprocessor.preprocess(simpleQuery)
Logger.debug("Satisfying query: $query")
return query.satisfy(emptyMap())
}
fun printAnswers(answers: Answers) {
private fun printAnswers(answers: Answers) {
val knownCommands = setOf(";", "a", ".", "h")
val iterator = answers.iterator()
@ -68,7 +73,7 @@ class Repl {
io.say("\n")
}
fun help(): String {
private fun help(): String {
io.say("Commands:\n")
io.say(" ; find next solution\n")
io.say(" a abort\n")
@ -77,12 +82,13 @@ class Repl {
return ""
}
fun prettyPrint(result: Answer): String {
private fun prettyPrint(result: Answer): String {
result.fold(
onSuccess = {
val subs = result.getOrNull()!!
if (subs.isEmpty()) {
return "true."
io.checkNewLine()
return "true.\n"
}
return subs.entries.joinToString(",\n") { "${it.key} = ${it.value}" }
},