Skip to content

Commit

Permalink
make ad-hoc OutputChannel for warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed Aug 28, 2020
1 parent 12ad9de commit b6d6847
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ interface ExecutionContext : ExecutionContextAware {
stdIn: InputChannel<String> = this.standardInput ?: InputChannel.stdIn(),
stdOut: OutputChannel<String> = this.standardOutput ?: OutputChannel.stdOut(),
stdErr: OutputChannel<String> = this.standardError ?: OutputChannel.stdErr(),
warnings: OutputChannel<PrologWarning> = this.warnings ?: OutputChannel.stdErr()
warnings: OutputChannel<PrologWarning> = this.warnings ?: OutputChannel.warn()
): Solver

@JsName("apply")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface ExecutionContextAware {
return mapOf(
STDOUT to OutputChannel.stdOut<String>(),
STDERR to OutputChannel.stdErr<String>(),
WARNINGS to OutputChannel.stdErr<PrologWarning>()
WARNINGS to OutputChannel.warn()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ interface SolverFactory {

@JsName("defaultWarningsChannel")
val defaultWarningsChannel: OutputChannel<PrologWarning>
get() = OutputChannel.stdErr()
get() = OutputChannel.warn()

@JsName("solver")
fun solverOf(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package it.unibo.tuprolog.solve.channel

import it.unibo.tuprolog.solve.exception.PrologWarning

internal expect fun stdin(): InputChannel<String>

internal expect fun <T> stdout(): OutputChannel<T>

internal expect fun <T> stderr(): OutputChannel<T>
internal expect fun <T> stderr(): OutputChannel<T>

internal expect fun warning(): OutputChannel<PrologWarning>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.unibo.tuprolog.solve.channel

import it.unibo.tuprolog.solve.channel.impl.OutputChannelFromFunction
import it.unibo.tuprolog.solve.exception.PrologWarning
import kotlin.js.JsName
import kotlin.jvm.JvmStatic

Expand All @@ -14,6 +15,10 @@ interface OutputChannel<T> : Channel<T> {
@JsName("stdErr")
fun <T> stdErr(): OutputChannel<T> = stderr()

@JvmStatic
@JsName("warning")
fun warn(): OutputChannel<PrologWarning> = warning()

@JvmStatic
@JsName("of")
fun <T> of(consumer: (T) -> Unit): OutputChannel<T> = OutputChannelFromFunction(consumer)
Expand Down
26 changes: 11 additions & 15 deletions solve/src/jsMain/kotlin/it/unibo/tuprolog/solve/channel/Channels.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package it.unibo.tuprolog.solve.channel

internal actual fun stdin(): InputChannel<String> {
return InputChannel.of {
throw IllegalStateException("No default implementation of stdin on JS")
}
}
import it.unibo.tuprolog.solve.exception.PrologWarning

internal actual fun <T> stderr(): OutputChannel<T> {
return OutputChannel.of {
console.error(it)
}
}
internal actual fun stdin(): InputChannel<String> =
InputChannel.of { throw IllegalStateException("No default implementation of stdin on JS") }

internal actual fun <T> stdout(): OutputChannel<T> {
return OutputChannel.of {
print(it)
}
}
internal actual fun <T> stderr(): OutputChannel<T> =
OutputChannel.of { console.error(it) }

internal actual fun <T> stdout(): OutputChannel<T> =
OutputChannel.of { print(it) }

internal actual fun warning(): OutputChannel<PrologWarning> =
OutputChannel.of { console.warn(it.message) }
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package it.unibo.tuprolog.solve.channel

import it.unibo.tuprolog.solve.exception.PrologWarning

internal actual fun stdin(): InputChannel<String> {
return InputStreamChannel(System.`in`)
}
Expand All @@ -9,5 +11,9 @@ internal actual fun <T> stderr(): OutputChannel<T> {
}

internal actual fun <T> stdout(): OutputChannel<T> {
return OutputChannel.of { System.out.print(it) }
return OutputChannel.of { print(it) }
}

internal actual fun warning(): OutputChannel<PrologWarning> {
return OutputChannel.of { System.err.println(it.message) }
}

0 comments on commit b6d6847

Please sign in to comment.