1
0
mirror of https://github.com/KarolS/millfork.git synced 2025-01-27 11:30:19 +00:00

Division by zero warnings

This commit is contained in:
Karol Stasiak 2019-06-23 19:44:34 +02:00
parent 7194bc1f1c
commit 088b28989f
2 changed files with 13 additions and 2 deletions

View File

@ -273,6 +273,11 @@ abstract class AbstractStatementPreprocessor(ctx: CompilationContext, statements
case LiteralExpression(0, _) => true
case _ => false
}) ctx.log.warn("Multiplication by zero.", params.head.position)
case FunctionCallExpression("/" | "/=" | "%%" | "%%=", params) =>
if (params.tail.exists {
case LiteralExpression(0, _) => true
case _ => false
}) ctx.log.warn("Division by zero.", params.head.position)
case FunctionCallExpression("<<" | ">>" | "<<'" | "<<=" | ">>=" | "<<'=" | ">>>>", List(lhs@_, LiteralExpression(0, _))) =>
ctx.log.warn("Shift by zero.", lhs.position)
case _ =>

View File

@ -145,8 +145,14 @@ abstract class AbstractAssembler[T <: AbstractCode](private val program: Program
case MathOperator.And => l & r
case MathOperator.Exor => l ^ r
case MathOperator.Or => l | r
case MathOperator.Divide => l / r
case MathOperator.Modulo => l % r
case MathOperator.Divide => if (r == 0) {
log.error("Constant division by zero")
0
} else l / r
case MathOperator.Modulo => if (r == 0) {
log.error("Constant division by zero")
0
} else l % r
}
}
}