Repl trouble

This commit is contained in:
Tibo De Peuter 2025-05-05 21:12:08 +02:00
parent fd16c4cedc
commit 4d334c1600
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
4 changed files with 35 additions and 31 deletions

View file

@ -3,7 +3,8 @@ package io
interface IoHandler { interface IoHandler {
fun prompt( fun prompt(
message: String, message: String,
hint: () -> String = { "Please enter a valid input." } hint: () -> String = { "Please enter a valid input." },
check: (String) -> Boolean = { true }
): String ): String
fun say(message: String) fun say(message: String)

View file

@ -20,15 +20,16 @@ class Terminal(
override fun prompt( override fun prompt(
message: String, message: String,
hint: () -> String hint: () -> String,
check: (String) -> Boolean,
): String { ): String {
say("$message ") say("$message ")
var input: String = readLine() var input: String = readLine().trim()
while (input.isBlank()) { while (!check(input)) {
say(hint(), error) say(hint(), error)
input = readLine() input += readLine().trim()
} }
return input return input.trim()
} }
override fun say(message: String) { override fun say(message: String) {

View file

@ -29,40 +29,45 @@ class Repl {
} }
private fun query(): Answers { private fun query(): Answers {
val queryString = io.prompt("?-", { "| " }) val queryString = io.prompt("?-", { "| " }, { it.endsWith(".") })
val simpleQuery = parser.parse(queryString) val simpleQuery = parser.parse(queryString)
val query = preprocessor.preprocess(simpleQuery) val query = preprocessor.preprocess(simpleQuery)
return query.satisfy(emptyMap()) return query.satisfy(emptyMap())
} }
private fun printAnswers(answers: Answers) { private fun printAnswers(answers: Answers) {
val knownCommands = setOf(";", "a", ".", "h")
val iterator = answers.iterator() val iterator = answers.iterator()
if (!iterator.hasNext()) { if (!iterator.hasNext()) {
io.say("false.\n") io.say("false.\n")
} else { return
io.say(prettyPrint(iterator.next()))
while (iterator.hasNext()) {
var command = io.prompt("")
while (command !in knownCommands) {
io.say("Unknown action: $command (h for help)\n")
command = io.prompt("Action?")
} }
when (command) { io.say(prettyPrint(iterator.next()))
while (true) {
when (io.prompt("")) {
";" -> { ";" -> {
try {
io.say(prettyPrint(iterator.next())) io.say(prettyPrint(iterator.next()))
} catch (_: NoSuchElementException) {
break
} }
}
"a" -> return "a" -> return
"." -> return "." -> {
io.checkNewLine()
return
}
"h" -> { "h" -> {
help() help()
io.say("Action?") io.say("Action?")
} }
else -> {
io.say("Unknown action: (h for help)\n")
io.say("Action?")
} }
} }
} }

View file

@ -170,11 +170,9 @@ class DatabaseOperatorsTests {
assertTrue(answer.isSuccess, "Expected success") assertTrue(answer.isSuccess, "Expected success")
assertTrue(answer.getOrNull()!!.isEmpty(), "Expected no substitutions") assertTrue(answer.getOrNull()!!.isEmpty(), "Expected no substitutions")
assertTrue(result.hasNext(), "Expected more results")
assertEquals(2, predicate.clauses.size, "Expected 2 clauses") assertEquals(2, predicate.clauses.size, "Expected 2 clauses")
assertTrue(result.next().isSuccess) assertTrue(result.next().isSuccess)
assertTrue(result.hasNext(), "Expected more results")
assertTrue(result.next().isSuccess) assertTrue(result.next().isSuccess)
assertFalse(result.hasNext(), "Expected more results") assertFalse(result.hasNext(), "Expected more results")
@ -208,7 +206,6 @@ class DatabaseOperatorsTests {
assertTrue(subs.isNotEmpty(), "Expected substitutions") assertTrue(subs.isNotEmpty(), "Expected substitutions")
assertTrue(Variable("X") in subs, "Expected variable X") assertTrue(Variable("X") in subs, "Expected variable X")
assertEquals(Atom("b"), subs[Variable("X")], "Expected b") assertEquals(Atom("b"), subs[Variable("X")], "Expected b")
assertTrue(result.hasNext(), "Expected more results")
assertEquals(2, predicate.clauses.size, "Expected 2 clauses") assertEquals(2, predicate.clauses.size, "Expected 2 clauses")
answer = result.next() answer = result.next()
@ -218,7 +215,6 @@ class DatabaseOperatorsTests {
assertTrue(subs.isNotEmpty(), "Expected substitutions") assertTrue(subs.isNotEmpty(), "Expected substitutions")
assertTrue(Variable("X") in subs, "Expected variable X") assertTrue(Variable("X") in subs, "Expected variable X")
assertEquals(Atom("c"), subs[Variable("X")], "Expected c") assertEquals(Atom("c"), subs[Variable("X")], "Expected c")
assertTrue(result.hasNext(), "Expected more results")
assertEquals(1, predicate.clauses.size, "Expected 1 clause") assertEquals(1, predicate.clauses.size, "Expected 1 clause")
answer = result.next() answer = result.next()
@ -228,8 +224,9 @@ class DatabaseOperatorsTests {
assertTrue(subs.isNotEmpty(), "Expected substitutions") assertTrue(subs.isNotEmpty(), "Expected substitutions")
assertTrue(Variable("X") in subs, "Expected variable X") assertTrue(Variable("X") in subs, "Expected variable X")
assertEquals(Atom("d"), subs[Variable("X")], "Expected d") assertEquals(Atom("d"), subs[Variable("X")], "Expected d")
assertFalse(result.hasNext(), "Expected no more results")
assertEquals(0, predicate.clauses.size, "Expected no clauses") assertEquals(0, predicate.clauses.size, "Expected no clauses")
assertFalse(result.hasNext(), "Expected no more results")
} }
@Test @Test