commit 7c32d95ffdf77b982b1d50ee42c90cf8fbdd7643
parent c2fe4336688db47d7ff5538e8f964a4a01960b99
Author: Eyal Sawady <ecs@d2evs.net>
Date: Thu, 14 Jan 2021 15:57:01 -0500
Add loop test
And subsume control test into loop test
Diffstat:
3 files changed, 106 insertions(+), 49 deletions(-)
diff --git a/tests/12-control.ha b/tests/12-control.ha
@@ -1,48 +0,0 @@
-fn rt::compile(src: str) int;
-
-export fn main() void = {
- for (let i: int = 0; i < 1; i += 1) {
- let j: int = 0;
- for (j < 10) {
- j += 1;
- if (j == 5) {
- break;
- };
- };
- assert(j == 5, "Failed to break out of while");
- };
- let done: bool = false;
- for (!done) {
- for (let i: int = 0; i < 10; i += 1) {
- if (i == 5) {
- true;
- continue;
- };
- assert(i != 5, "Broken continue");
- if (i >= 7) {
- break;
- };
- assert(i < 7, "Broken break");
- };
- done = true;
- };
- let i = 0;
- :outer for (i < 10) {
- :inner for (let j = 0; j < 7; j += 1) {
- :outer for (true) {
- break :outer;
- };
- i += 1;
- if (j == 6) {
- continue :inner;
- };
- assert(j != 6);
- if (i > 7) {
- break :outer;
- };
- };
- };
- assert(i == 8);
- assert(rt::compile("fn test() void = { :foo for (true) { break :bar; }; };") != 0);
- assert(rt::compile("fn test() void = { break :bar; };") != 0);
-};
diff --git a/tests/12-loops.ha b/tests/12-loops.ha
@@ -0,0 +1,105 @@
+fn rt::compile(src: str) int;
+
+fn scope() void = {
+ let x = 0;
+ for (let i = 1; i == 1; i += 1) {
+ for (true) {
+ assert(x == 0);
+ assert(i == 1);
+ break;
+ };
+ };
+ assert(rt::compile("fn test() void = { for (true) { let x = 10; }; x; };") != 0);
+ // To make sure that the afterthought is part of the loop's scope
+ for (let i = 0; true; (if (true) { break; })) true;
+};
+
+fn conditional() void = {
+ let i = 1;
+ for (i < 10) {
+ i *= 2;
+ };
+ assert(i == 16);
+};
+
+fn afterthought() void = {
+ let i = 1;
+ for (i < 5; i += 1) {
+ i *= 2;
+ };
+ assert(i == 7);
+};
+
+fn binding() void = {
+ let x = 0;
+ for (let i = 0; i < 10; i += 1) {
+ i *= 2;
+ x += 1;
+ };
+ assert(x == 4);
+};
+
+fn _break() void = {
+ let x = 0;
+ for (let i = 0; i < 1; i += 1) {
+ let j = 0;
+ for (j < 10) {
+ j += 1;
+ if (j == 5) {
+ break;
+ };
+ };
+ assert(j == 5);
+ x += 1;
+ };
+ assert(x == 1);
+};
+
+fn _continue() void = {
+ let done = false;
+ let x = 0;
+ for (!done) {
+ for (let i = 0; i < 10; i += 1) {
+ if (i == 5) {
+ continue;
+ };
+ assert(i != 5);
+ };
+ done = true;
+ x += 1;
+ };
+ assert(x == 1);
+};
+
+fn label() void = {
+ let i = 0;
+ :outer for (i < 10) {
+ :inner for (let j = 0; j < 7; j += 1) {
+ :outer for (true) {
+ break :outer;
+ };
+ i += 1;
+ if (j == 6) {
+ continue :inner;
+ };
+ assert(j != 6);
+ if (i > 7) {
+ break :outer;
+ };
+ };
+ };
+ assert(i == 8);
+ assert(rt::compile("fn test() void = { :foo for (true) { break :bar; }; };") != 0);
+ assert(rt::compile("fn test() void = { for (true) { break :bar; }; };") != 0);
+ assert(rt::compile("fn test() void = { break :bar; };") != 0);
+};
+
+export fn main() void = {
+ scope();
+ conditional();
+ afterthought();
+ binding();
+ _break();
+ _continue();
+ label();
+};
diff --git a/tests/configure b/tests/configure
@@ -15,7 +15,7 @@ tests() {
09-funcs \
10-binarithms \
11-globals \
- 12-control
+ 12-loops
do
cat <<EOF
tests/$t: libhart.a tests/$t.ha