1
0
mirror of https://github.com/KarolS/millfork.git synced 2024-06-30 21:29:36 +00:00

Comparison improvements

This commit is contained in:
Karol Stasiak 2018-01-20 01:30:46 +01:00
parent c5e01d5117
commit c520bbb698
3 changed files with 40 additions and 0 deletions

View File

@ -290,6 +290,9 @@ object BuiltIns {
def compileByteComparison(ctx: CompilationContext, compType: ComparisonType.Value, lhs: Expression, rhs: Expression, branches: BranchSpec): List[AssemblyLine] = {
val env = ctx.env
val b = env.get[Type]("byte")
if (simplicity(env, lhs) >= 'J' && simplicity(env, rhs) < 'J') {
return compileByteComparison(ctx, ComparisonType.flip(compType), rhs, lhs, branches)
}
val firstParamCompiled = MlCompiler.compile(ctx, lhs, Some(b -> RegisterVariable(Register.A, b)), NoBranching)
val maybeConstant = env.eval(rhs)
maybeConstant match {

View File

@ -1157,6 +1157,24 @@ object MlCompiler {
case List(_) | Nil =>
ErrorReporting.fatal("")
case _ =>
params.tail.init.foreach { e =>
if (ctx.env.eval(e).isEmpty) e match {
case VariableExpression(_) =>
case LiteralExpression(_, _) =>
case IndexedExpression(_, VariableExpression(_)) =>
case IndexedExpression(_, LiteralExpression(_, _)) =>
case IndexedExpression(_, SumExpression(List(
(_, LiteralExpression(_, _)),
(false, VariableExpression(_))
), false)) =>
case IndexedExpression(_, SumExpression(List(
(false, VariableExpression(_)),
(_, LiteralExpression(_, _))
), false)) =>
case _ =>
ErrorReporting.warn("A complex expression may be evaluated multiple times", ctx.options, e.position)
}
}
val conjunction = params.init.zip(params.tail).map {
case (l, r) => FunctionCallExpression(operator, List(l, r))
}.reduceLeft((a, b) => FunctionCallExpression("&&", List(a, b)))

View File

@ -293,4 +293,23 @@ class ComparisonSuite extends FunSuite with Matchers {
| }
""".stripMargin)(_.readWord(0xc000) should equal(6))
}
test("Warnings") {
EmuBenchmarkRun(
"""
| byte output @$c000
| void main () {
| output = 5
| if 2 <= three() <= 4 {
| output += 1
| }
| if 2 <= three() <= 2 {
| output += 78
| }
| }
| byte three() {
| return 3
| }
""".stripMargin)(_.readWord(0xc000) should equal(6))
}
}