1
0
mirror of https://github.com/KarolS/millfork.git synced 2024-07-05 09:28:54 +00:00

Fix for...downto loops

This commit is contained in:
Karol Stasiak 2018-10-11 23:59:50 +02:00
parent 6b43f73f57
commit df71435c78
2 changed files with 27 additions and 1 deletions

View File

@ -229,6 +229,8 @@ abstract class AbstractStatementCompiler[T <: AbstractCode] {
names)
))
case (ForDirection.DownTo, _, _) =>
// TODO: smarter countdown if end is not a constant
val endMinusOne = SumExpression(List(true -> f.end, false -> LiteralExpression(1, 1)), decimal = false).pos(p)
compile(ctx, List(
Assignment(vex, f.start).pos(p),
IfStatement(
@ -236,7 +238,7 @@ abstract class AbstractStatementCompiler[T <: AbstractCode] {
List(DoWhileStatement(
f.body,
List(decrement),
FunctionCallExpression("!=", List(vex, f.end)).pos(p),
FunctionCallExpression("!=", List(vex, endMinusOne)).pos(p),
names
).pos(p)),
Nil)

View File

@ -77,6 +77,30 @@ class ForLoopSuite extends FunSuite with Matchers {
}
}
test("For-downto 3") {
EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp)(
"""
| array output [55] @$c000
| void main () {
| byte i
| output[0] = 0
| output[1] = 0
| output[5] = 0
| output[6] = 0
| for i,5,downto,1 {
| stuff()
| output[i] += 1
| }
| }
| noinline void stuff() {}
""".stripMargin){m =>
m.readByte(0xc000) should equal(0)
m.readByte(0xc001) should equal(1)
m.readByte(0xc005) should equal(1)
m.readByte(0xc006) should equal(0)
}
}
test("For-until") {
EmuCrossPlatformBenchmarkRun(Cpu.Mos, Cpu.Z80, Cpu.Intel8080, Cpu.Sharp)(
"""