Skip to content

Commit

Permalink
fix presentation of variables in :repl
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed Sep 11, 2020
1 parent d6ee266 commit 99d608c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import it.unibo.tuprolog.core.parsing.ParseException
import it.unibo.tuprolog.core.parsing.parse
import it.unibo.tuprolog.solve.Solver
import it.unibo.tuprolog.solve.TimeDuration
import it.unibo.tuprolog.solve.channel.OutputChannel
import it.unibo.tuprolog.solve.classicWithDefaultBuiltins
import it.unibo.tuprolog.theory.Theory

Expand Down Expand Up @@ -43,7 +44,6 @@ class TuPrologCmd : CliktCommand(
if (subcommand == null) {
readEvalPrintLoop(solve)
}
// nota: se subcommand è diverso da null, il controllo fluisce automaticamente al metodo run di subcommand
}

private fun loadTheory(): Theory {
Expand All @@ -55,13 +55,15 @@ class TuPrologCmd : CliktCommand(
TermUi.echo("# Successfully loaded ${t.size} clauses from $file")
theory += t
} catch (e: ParseException) {
TermUi.echo("""
|Error while parsing theory file: $file
| Message: ${e.message}
| Line : ${e.line}
| Column : ${e.column}
| Clause : ${e.clauseIndex}
""".trimMargin(), err = true)
TermUi.echo(
"""
|# Error while parsing theory file: $file
|# Message: ${e.message}
|# Line : ${e.line}
|# Column : ${e.column}
|# Clause : ${e.clauseIndex}
""".trimMargin(), err = true
)
}
}
}
Expand Down Expand Up @@ -91,7 +93,12 @@ class TuPrologCmd : CliktCommand(
fun getSolver(): Solver {
TermUi.echo("# 2P-Kt version ${Info.VERSION}")
val theory: Theory = this.loadTheory()
return Solver.classicWithDefaultBuiltins(staticKb = theory)
return Solver.classicWithDefaultBuiltins(
staticKb = theory,
warnings = OutputChannel.of {
TermUi.echo("# ${it.message}", err = true)
}
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ object TuPrologUtils {
}

private fun printYesSolution(sol: Solution.Yes, operatorSet: OperatorSet) {
TermUi.echo("yes: ${sol.solvedQuery.format(prettyExpressions(operatorSet))}.")
val formatter = prettyExpressions(operatorSet)
TermUi.echo("yes: ${sol.solvedQuery.format(formatter)}.")
if (sol.substitution.isNotEmpty()) {
val sep = "\n "
val substitutions = sol.substitution.entries.joinToString(sep) {
val prettyVariable = it.key.format(TermFormatter.prettyVariables())
val prettyValue = it.value.format(prettyExpressions(operatorSet))
val substitutions = sol.substitution.entries.joinToString("\n ") {
val prettyVariable = it.key.format(formatter)
val prettyValue = it.value.format(formatter)
"$prettyVariable = $prettyValue"
}
TermUi.echo(" $substitutions")
Expand All @@ -57,14 +57,15 @@ object TuPrologUtils {
TermUi.echo("halt: ${ex.message?.trim()}")
}
val sep = "\n at "
val stacktrace = ex.prologStackTrace.joinToString(sep) { it.format(prettyExpressions(operatorSet)) }
val formatter = prettyExpressions(operatorSet)
val stacktrace = ex.prologStackTrace.joinToString(sep) { it.format(formatter) }
TermUi.echo(" at $stacktrace")
}
}
}

fun printParseException(e: ParseException) {
TermUi.echo(e.message?.capitalize(), err = true)
TermUi.echo("# ${e.message?.capitalize()}", err = true)
}

private fun printEndOfSolutions() {
Expand Down

0 comments on commit 99d608c

Please sign in to comment.