All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.it.unibo.tuprolog.solve.stdlib.primitive.Throw.kt Maven / Gradle / Ivy

There is a newer version: 0.17.4
Show newest version
package it.unibo.tuprolog.solve.stdlib.primitive

import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.solve.ExecutionContext
import it.unibo.tuprolog.solve.exception.PrologError
import it.unibo.tuprolog.solve.exception.TuPrologRuntimeException
import it.unibo.tuprolog.solve.exception.error.MessageError
import it.unibo.tuprolog.solve.primitive.Solve
import it.unibo.tuprolog.solve.primitive.UnaryPredicate

object Throw : UnaryPredicate("throw") {
    private fun handleError(context: ExecutionContext, error: Term): TuPrologRuntimeException =
        when {
            error is Struct && error.functor == "error" && error.arity in 1..2 -> {
                PrologError.of(
                    context = context,
                    type = error[0] as Struct,
                    extraData = if (error.arity > 1) error[1] else null
                )
            }
            else -> MessageError.of(error, context)
        }

    override fun Solve.Request.computeAll(first: Term): Sequence {
        return sequenceOf(
            ensuringAllArgumentsAreInstantiated()
                .replyException(
                    handleError(context, arguments[0])
                )
        )
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy