Skip to content

Commit

Permalink
Merge branch 'develop' into feature/solve-streams-timeouts-alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed Jun 12, 2020
2 parents cada251 + aac4bb5 commit dd2bd6d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import it.unibo.tuprolog.solve.ExecutionContext
import it.unibo.tuprolog.solve.Signature
import it.unibo.tuprolog.solve.exception.error.EvaluationError
import it.unibo.tuprolog.solve.exception.error.TypeError
import kotlin.jvm.JvmStatic

/**
* Base class to implement arithmetic functions
Expand All @@ -17,46 +16,36 @@ abstract class MathFunction : FunctionWrapper<ExecutionContext> {
constructor(signature: Signature) : super(signature)
constructor(name: String, arity: Int, vararg: Boolean = false) : super(name, arity, vararg)

companion object {

/** Utility function to throw int overflow math error */
@JvmStatic
protected fun throwIntOverflowError(context: ExecutionContext): Nothing =
throw EvaluationError(context = context, errorType = EvaluationError.Type.INT_OVERFLOW)

/** Utility function to throw float overflow math error */
@JvmStatic
protected fun throwFloatOverflowError(context: ExecutionContext): Nothing =
throw EvaluationError(context = context, errorType = EvaluationError.Type.FLOAT_OVERFLOW)

/** Utility function to throw underflow math error */
@JvmStatic
protected fun throwUnderflowError(context: ExecutionContext): Nothing =
throw EvaluationError(context = context, errorType = EvaluationError.Type.UNDERFLOW)

/** Utility function to throw zero division math error */
@JvmStatic
protected fun throwZeroDivisorError(context: ExecutionContext): Nothing =
throw EvaluationError(context = context, errorType = EvaluationError.Type.ZERO_DIVISOR)

/** Utility function to throw undefined math error */
@JvmStatic
protected fun throwUndefinedError(context: ExecutionContext): Nothing =
throw EvaluationError(context = context, errorType = EvaluationError.Type.UNDEFINED)

/** Utility function to throw a TypeError for operators requiring only integers as parameters */
@JvmStatic
protected fun throwTypeErrorBecauseOnlyIntegersAccepted(
opName: String,
actualValue: Term,
context: ExecutionContext
): Nothing =
throw TypeError(
"Operator `$opName` accepts only integers!",
context = context,
expectedType = TypeError.Expected.INTEGER,
actualValue = actualValue
)
}
/** Utility function to throw int overflow math error */
protected fun throwIntOverflowError(context: ExecutionContext): Nothing =
throw EvaluationError(context = context, errorType = EvaluationError.Type.INT_OVERFLOW)

/** Utility function to throw float overflow math error */
protected fun throwFloatOverflowError(context: ExecutionContext): Nothing =
throw EvaluationError(context = context, errorType = EvaluationError.Type.FLOAT_OVERFLOW)

/** Utility function to throw underflow math error */
protected fun throwUnderflowError(context: ExecutionContext): Nothing =
throw EvaluationError(context = context, errorType = EvaluationError.Type.UNDERFLOW)

/** Utility function to throw zero division math error */
protected fun throwZeroDivisorError(context: ExecutionContext): Nothing =
throw EvaluationError(context = context, errorType = EvaluationError.Type.ZERO_DIVISOR)

/** Utility function to throw undefined math error */
protected fun throwUndefinedError(context: ExecutionContext): Nothing =
throw EvaluationError(context = context, errorType = EvaluationError.Type.UNDEFINED)

/** Utility function to throw a TypeError for operators requiring only integers as parameters */
protected fun throwTypeErrorBecauseOnlyIntegersAccepted(
opName: String,
actualValue: Term,
context: ExecutionContext
): Nothing = throw TypeError(
"Operator `$opName` accepts only integers!",
context = context,
expectedType = TypeError.Expected.INTEGER,
actualValue = actualValue
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ object FloatingPointDivision : BinaryMathFunction("/") {
/** Implements common behaviour for Integer and Real */
private fun commonBehaviour(dividend: BigDecimal, divisor: BigDecimal, context: ExecutionContext): Real =
// TODO: 25/10/2019 "float_overflow" and "underflow" checks missing (see the standard)
when (divisor) {
BigDecimal.ZERO -> throwZeroDivisorError(context)
else -> Numeric.of(dividend.div(divisor, MathContext())!!)
if (divisor.compareTo(BigDecimal.ZERO) == 0) {
throwZeroDivisorError(context)
} else {
Numeric.of(dividend.div(divisor, MathContext())!!)
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ object IntegerDivision : IntegersBinaryMathFunction("//") {

override fun mathFunction(integer1: Integer, integer2: Integer, context: ExecutionContext): Numeric =
// TODO: 25/10/2019 "int_overflow" checks missing (see the standard)
when (integer2.value) {
BigInteger.ZERO -> throwZeroDivisorError(context)
else -> Numeric.of(integer1.value / integer2.value)
if (integer2.value.compareTo(BigInteger.ZERO) == 0) {
throwZeroDivisorError(context)
} else {
Numeric.of(integer1.value / integer2.value)
}

}

0 comments on commit dd2bd6d

Please sign in to comment.