From 4d334c160032f2ce897a9b4d8edea2bd0206b5f9 Mon Sep 17 00:00:00 2001 From: Tibo De Peuter Date: Mon, 5 May 2025 21:12:08 +0200 Subject: [PATCH] Repl trouble --- src/io/IoHandler.kt | 3 +- src/io/Terminal.kt | 11 ++--- src/repl/Repl.kt | 45 ++++++++++--------- .../prolog/builtins/DatabaseOperatorsTests.kt | 7 +-- 4 files changed, 35 insertions(+), 31 deletions(-) diff --git a/src/io/IoHandler.kt b/src/io/IoHandler.kt index c5706a1..f12deb7 100644 --- a/src/io/IoHandler.kt +++ b/src/io/IoHandler.kt @@ -3,7 +3,8 @@ package io interface IoHandler { fun prompt( message: String, - hint: () -> String = { "Please enter a valid input." } + hint: () -> String = { "Please enter a valid input." }, + check: (String) -> Boolean = { true } ): String fun say(message: String) diff --git a/src/io/Terminal.kt b/src/io/Terminal.kt index 1b9df94..d20c8e8 100644 --- a/src/io/Terminal.kt +++ b/src/io/Terminal.kt @@ -20,15 +20,16 @@ class Terminal( override fun prompt( message: String, - hint: () -> String + hint: () -> String, + check: (String) -> Boolean, ): String { say("$message ") - var input: String = readLine() - while (input.isBlank()) { + var input: String = readLine().trim() + while (!check(input)) { say(hint(), error) - input = readLine() + input += readLine().trim() } - return input + return input.trim() } override fun say(message: String) { diff --git a/src/repl/Repl.kt b/src/repl/Repl.kt index 5e8b0d4..073d76e 100644 --- a/src/repl/Repl.kt +++ b/src/repl/Repl.kt @@ -29,40 +29,45 @@ class Repl { } private fun query(): Answers { - val queryString = io.prompt("?-", { "| " }) + val queryString = io.prompt("?-", { "| " }, { it.endsWith(".") }) val simpleQuery = parser.parse(queryString) val query = preprocessor.preprocess(simpleQuery) return query.satisfy(emptyMap()) } private fun printAnswers(answers: Answers) { - val knownCommands = setOf(";", "a", ".", "h") - val iterator = answers.iterator() if (!iterator.hasNext()) { io.say("false.\n") - } else { - io.say(prettyPrint(iterator.next())) + return + } - while (iterator.hasNext()) { - var command = io.prompt("") + io.say(prettyPrint(iterator.next())) - while (command !in knownCommands) { - io.say("Unknown action: $command (h for help)\n") - command = io.prompt("Action?") + while (true) { + when (io.prompt("")) { + ";" -> { + try { + io.say(prettyPrint(iterator.next())) + } catch (_: NoSuchElementException) { + break + } } - when (command) { - ";" -> { - io.say(prettyPrint(iterator.next())) - } - "a" -> return - "." -> return - "h" -> { - help() - io.say("Action?") - } + "a" -> return + "." -> { + io.checkNewLine() + return + } + "h" -> { + help() + io.say("Action?") + } + + else -> { + io.say("Unknown action: (h for help)\n") + io.say("Action?") } } } diff --git a/tests/prolog/builtins/DatabaseOperatorsTests.kt b/tests/prolog/builtins/DatabaseOperatorsTests.kt index 8e96085..aa5b7b1 100644 --- a/tests/prolog/builtins/DatabaseOperatorsTests.kt +++ b/tests/prolog/builtins/DatabaseOperatorsTests.kt @@ -170,11 +170,9 @@ class DatabaseOperatorsTests { assertTrue(answer.isSuccess, "Expected success") assertTrue(answer.getOrNull()!!.isEmpty(), "Expected no substitutions") - assertTrue(result.hasNext(), "Expected more results") assertEquals(2, predicate.clauses.size, "Expected 2 clauses") assertTrue(result.next().isSuccess) - assertTrue(result.hasNext(), "Expected more results") assertTrue(result.next().isSuccess) assertFalse(result.hasNext(), "Expected more results") @@ -208,7 +206,6 @@ class DatabaseOperatorsTests { assertTrue(subs.isNotEmpty(), "Expected substitutions") assertTrue(Variable("X") in subs, "Expected variable X") assertEquals(Atom("b"), subs[Variable("X")], "Expected b") - assertTrue(result.hasNext(), "Expected more results") assertEquals(2, predicate.clauses.size, "Expected 2 clauses") answer = result.next() @@ -218,7 +215,6 @@ class DatabaseOperatorsTests { assertTrue(subs.isNotEmpty(), "Expected substitutions") assertTrue(Variable("X") in subs, "Expected variable X") assertEquals(Atom("c"), subs[Variable("X")], "Expected c") - assertTrue(result.hasNext(), "Expected more results") assertEquals(1, predicate.clauses.size, "Expected 1 clause") answer = result.next() @@ -228,8 +224,9 @@ class DatabaseOperatorsTests { assertTrue(subs.isNotEmpty(), "Expected substitutions") assertTrue(Variable("X") in subs, "Expected variable X") assertEquals(Atom("d"), subs[Variable("X")], "Expected d") - assertFalse(result.hasNext(), "Expected no more results") assertEquals(0, predicate.clauses.size, "Expected no clauses") + + assertFalse(result.hasNext(), "Expected no more results") } @Test