Repl trouble
This commit is contained in:
parent
fd16c4cedc
commit
4d334c1600
4 changed files with 35 additions and 31 deletions
|
@ -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)
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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?")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue