diff --git a/src/core/loopNext.go b/src/core/loopNext.go index 7b5fa8b..6dae710 100644 --- a/src/core/loopNext.go +++ b/src/core/loopNext.go @@ -11,12 +11,13 @@ func (f *Function) loopNext(loop *Loop) { endOfLoopValue, _, _ := f.findIdentifier(loop.IteratorName) one := f.Append(&ssa.Int{Int: 1}) - nextIteration := f.Append(&ssa.BinaryOp{ + nextIteration := &ssa.BinaryOp{ Op: token.Add, Left: endOfLoopValue, Right: one, - }) + } + f.Block().Append(nextIteration) f.Block().Identify(loop.IteratorName, nextIteration) } diff --git a/tests/loop-count-down.q b/tests/loop-count-down.q new file mode 100644 index 0000000..12b8a00 --- /dev/null +++ b/tests/loop-count-down.q @@ -0,0 +1,14 @@ +main() { + n := 10 + x := 0 + + loop { + if n == 0 { + assert x == 10 + return + } + + x += 1 + n -= 1 + } +} \ No newline at end of file diff --git a/tests/loop-count.q b/tests/loop-count.q index 12b8a00..a539510 100644 --- a/tests/loop-count.q +++ b/tests/loop-count.q @@ -1,14 +1,9 @@ main() { - n := 10 - x := 0 + x := 1 - loop { - if n == 0 { - assert x == 10 - return - } - - x += 1 - n -= 1 + loop i := 0..4 { + x += i } + + assert x == 7 } \ No newline at end of file diff --git a/tests/tests_test.go b/tests/tests_test.go index ff694b7..8abd021 100644 --- a/tests/tests_test.go +++ b/tests/tests_test.go @@ -65,6 +65,7 @@ var tests = []run{ {"loop-counter-tail", nil, "", "10\n9\n8\n7\n6\n5\n4\n3\n2\n1\n0\n", 0}, {"loop-counter-head", nil, "", "9\n8\n7\n6\n5\n4\n3\n2\n1\n0\n", 0}, {"loop-count", nil, "", "", 0}, + {"loop-count-down", nil, "", "", 0}, {"loop-write", nil, "", "..........", 0}, {"loop-optimize-single-iterator", nil, "", "", 0}, {"loop-limit", nil, "", "", 0},