diff options
| author | Mustafa Quraish <[email protected]> | 2022-01-29 18:29:45 -0500 |
|---|---|---|
| committer | Mustafa Quraish <[email protected]> | 2022-01-29 18:29:45 -0500 |
| commit | 81250ad76bbd336a262da71f4f71d6a37b68bd74 (patch) | |
| tree | b4f94acfa2f46e0e87e49aca97ba72a76b459a23 /tests/loops.sh | |
| parent | Implement blocks (lexically scoped) and conditionals (diff) | |
| download | cup-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 'tests/loops.sh')
| -rwxr-xr-x | tests/loops.sh | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/tests/loops.sh b/tests/loops.sh new file mode 100755 index 0000000..3fea60f --- /dev/null +++ b/tests/loops.sh @@ -0,0 +1,118 @@ +#!/bin/bash + +. tests/common.sh + +set -e + +echo -n "- While loops: " +assert_exit_status_stdin 5 <<EOF +fn main() { + while (1) { + return 5; + } + return 3; +} +EOF + +assert_exit_status_stdin 3 <<EOF +fn main() { + while (0) { + return 5; + } + return 3; +} +EOF + +assert_exit_status_stdin 10 <<EOF +fn main() { + let sum: int = 0; + while (sum < 10) { + sum = sum + 1; + } + return sum; +} +EOF + +assert_exit_status_stdin 55 <<EOF +fn main() { + let sum: int = 0; + let N: int = 10; + let i: int = 0; + while (i <= N) { + sum = sum + i; + i = i + 1; + } + return sum; +} +EOF +echo " OK" + +echo -n "- For loops: " +assert_exit_status_stdin 5 <<EOF +fn main() { + for (;;) { + return 5; + } + return 3; +} +EOF + +assert_exit_status_stdin 3 <<EOF +fn main() { + for (;0;) { + return 5; + } + return 3; +} +EOF + +assert_exit_status_stdin 55 <<EOF +fn main() { + let sum: int = 0; + let i: int; + for (i = 0; i <= 10; i = i + 1) { + sum = sum + i; + } + return sum; +} +EOF + +assert_exit_status_stdin 55 <<EOF +fn main() { + let sum: int = 0; + let i: int = 0; + for (; i <= 10; i = i + 1) { + sum = sum + i; + } + return sum; +} +EOF + +assert_exit_status_stdin 45 <<EOF +fn main() { + let sum: int = 0; + let i: int = 0; + for (;i < 10;) { + sum = sum + i; + i = i + 1; + } + return sum; +} +EOF + +assert_exit_status_stdin 55 <<EOF +fn main() { + let sum: int = 0; + let i: int = 0; + for (;;) { + sum = sum + i; + i = i + 1; + if (i == 11) { + return sum; + } + } + // unreachable, but we don't catch this error yet + return -1; +} +EOF +echo " OK" |