From 99d608c2db1250b2e02142ed8c1fab4b13db4376 Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Fri, 11 Sep 2020 21:15:23 +0200 Subject: [PATCH] fix presentation of variables in :repl --- .../it/unibo/tuprolog/ui/repl/TuPrologCmd.kt | 25 ++++++++++++------- .../unibo/tuprolog/ui/repl/TuPrologUtils.kt | 15 +++++------ 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/repl/src/commonMain/kotlin/it/unibo/tuprolog/ui/repl/TuPrologCmd.kt b/repl/src/commonMain/kotlin/it/unibo/tuprolog/ui/repl/TuPrologCmd.kt index d452c584e..ffa2cb990 100644 --- a/repl/src/commonMain/kotlin/it/unibo/tuprolog/ui/repl/TuPrologCmd.kt +++ b/repl/src/commonMain/kotlin/it/unibo/tuprolog/ui/repl/TuPrologCmd.kt @@ -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 @@ -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 { @@ -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 + ) } } } @@ -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) + } + ) } } diff --git a/repl/src/commonMain/kotlin/it/unibo/tuprolog/ui/repl/TuPrologUtils.kt b/repl/src/commonMain/kotlin/it/unibo/tuprolog/ui/repl/TuPrologUtils.kt index f8b3bc11a..d7d74d730 100644 --- a/repl/src/commonMain/kotlin/it/unibo/tuprolog/ui/repl/TuPrologUtils.kt +++ b/repl/src/commonMain/kotlin/it/unibo/tuprolog/ui/repl/TuPrologUtils.kt @@ -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") @@ -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() {