From 81250ad76bbd336a262da71f4f71d6a37b68bd74 Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Sat, 29 Jan 2022 18:29:45 -0500 Subject: Add for and while loop support (w/o declarations in `for`) We can now loop and do stuff, yay! However, we don't yet allow declarations inside the for-loop initializer, since that is a bit more annoying to implement. --- examples/loops.cup | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 examples/loops.cup (limited to 'examples') diff --git a/examples/loops.cup b/examples/loops.cup new file mode 100644 index 0000000..6c4f3a7 --- /dev/null +++ b/examples/loops.cup @@ -0,0 +1,22 @@ +fn main(): int { + let sum1: int = 0; + let sum2: int = 0; + + let N: int = 10; + let i: int = 0; + + for (i = 0; i <= N; i = i + 1) { + sum1 = sum1 + i; + } + + i = 0; + while (i <= N) { + sum2 = sum2 + i; + i = i + 1; + } + + if (sum1 == sum2 && sum1 == 55) { + return 0; + } + return 1; +} \ No newline at end of file -- cgit v1.2.3