aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorMustafa Quraish <[email protected]>2022-01-29 18:29:45 -0500
committerMustafa Quraish <[email protected]>2022-01-29 18:29:45 -0500
commit81250ad76bbd336a262da71f4f71d6a37b68bd74 (patch)
treeb4f94acfa2f46e0e87e49aca97ba72a76b459a23 /examples
parentImplement blocks (lexically scoped) and conditionals (diff)
downloadcup-81250ad76bbd336a262da71f4f71d6a37b68bd74.tar.xz
cup-81250ad76bbd336a262da71f4f71d6a37b68bd74.zip
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.
Diffstat (limited to 'examples')
-rw-r--r--examples/loops.cup22
1 files changed, 22 insertions, 0 deletions
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