mirror of
https://github.com/KarolS/millfork.git
synced 2024-10-01 07:59:30 +00:00
Comparison improvements
This commit is contained in:
parent
c5e01d5117
commit
c520bbb698
@ -290,6 +290,9 @@ object BuiltIns {
|
|||||||
def compileByteComparison(ctx: CompilationContext, compType: ComparisonType.Value, lhs: Expression, rhs: Expression, branches: BranchSpec): List[AssemblyLine] = {
|
def compileByteComparison(ctx: CompilationContext, compType: ComparisonType.Value, lhs: Expression, rhs: Expression, branches: BranchSpec): List[AssemblyLine] = {
|
||||||
val env = ctx.env
|
val env = ctx.env
|
||||||
val b = env.get[Type]("byte")
|
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 firstParamCompiled = MlCompiler.compile(ctx, lhs, Some(b -> RegisterVariable(Register.A, b)), NoBranching)
|
||||||
val maybeConstant = env.eval(rhs)
|
val maybeConstant = env.eval(rhs)
|
||||||
maybeConstant match {
|
maybeConstant match {
|
||||||
|
@ -1157,6 +1157,24 @@ object MlCompiler {
|
|||||||
case List(_) | Nil =>
|
case List(_) | Nil =>
|
||||||
ErrorReporting.fatal("")
|
ErrorReporting.fatal("")
|
||||||
case _ =>
|
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 {
|
val conjunction = params.init.zip(params.tail).map {
|
||||||
case (l, r) => FunctionCallExpression(operator, List(l, r))
|
case (l, r) => FunctionCallExpression(operator, List(l, r))
|
||||||
}.reduceLeft((a, b) => FunctionCallExpression("&&", List(a, b)))
|
}.reduceLeft((a, b) => FunctionCallExpression("&&", List(a, b)))
|
||||||
|
@ -293,4 +293,23 @@ class ComparisonSuite extends FunSuite with Matchers {
|
|||||||
| }
|
| }
|
||||||
""".stripMargin)(_.readWord(0xc000) should equal(6))
|
""".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))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user