Skip to content

Commit

Permalink
add factory method TypeError.forArgumentList
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed Aug 31, 2020
1 parent 0ac0c26 commit 75c50a2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ class TypeError(

// TODO: 16/01/2020 test factories

fun forArgumentList(
context: ExecutionContext,
procedure: Signature,
expectedType: Expected,
actualValue: Term,
index: Int? = null
) = TypeError(
message = "Argument ${index
?: ""} of `${procedure.toIndicator()}` should be a list of `$expectedType`, but `$actualValue` has been provided instead",
context = context,
expectedType = expectedType,
actualValue = actualValue,
extraData = actualValue
)

fun forArgument(
context: ExecutionContext,
procedure: Signature,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import it.unibo.tuprolog.solve.exception.error.DomainError
import it.unibo.tuprolog.solve.exception.error.InstantiationError
import it.unibo.tuprolog.solve.exception.error.TypeError
import org.gciatto.kt.math.BigInteger
import it.unibo.tuprolog.core.List as LogicList

/**
* Wrapper class for [Primitive] implementation
Expand Down Expand Up @@ -142,6 +143,12 @@ abstract class PrimitiveWrapper<C : ExecutionContext> : AbstractWrapper<Primitiv
else -> this
}

fun <C : ExecutionContext> Solve.Request<C>.ensuringArgumentIsList(index: Int): Solve.Request<C> =
when (val arg = arguments[index]) {
!is LogicList -> throw TypeError.forArgument(context, signature, TypeError.Expected.LIST, arg, index)
else -> this
}

fun <C : ExecutionContext> Solve.Request<C>.ensuringArgumentIsNonNegativeInteger(index: Int): Solve.Request<C> =
arguments[index].let { arg ->
when {
Expand Down

0 comments on commit 75c50a2

Please sign in to comment.