Skip to content

Commit 7704355

Browse files
committed
feat!: implement for i in X...Y and make upper bound exclusive
The reason the upper bound was made exclusive is because the original compiler had this behavior. Keeping the upper bound inclusive would've effectively "broken" one of the Kit basic features.
1 parent 0bf9a07 commit 7704355

19 files changed

Lines changed: 192 additions & 9 deletions

ROADMAP.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Goal: lower a well-defined subset of Kit ("Kit Core") to portable C99.
3030
- [X] `if` expressions / statements
3131
- [ ] `while` and `for` loops
3232
- [X] Basic implementation
33-
- [ ] `for i in X...Y`
33+
- [x] `for i in X...Y`
3434
- [ ] [`kit.iterator`](https://kitlang.dev/examples/#kititerator)
3535
- [X] Function calls
3636
- [X] `return`

examples/range_edge_cases.kit

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
include "stdio.h";
2+
3+
function main() {
4+
printf("Testing range edge cases:\n");
5+
6+
printf("Empty range 5...1: ");
7+
for i in 5...1 {
8+
printf("%d ", i);
9+
}
10+
printf("\n");
11+
12+
printf("Single element 3...3: ");
13+
for i in 3...3 {
14+
printf("%d ", i);
15+
}
16+
printf("\n");
17+
18+
printf("Zero range 0...0: ");
19+
for i in 0...0 {
20+
printf("%d ", i);
21+
}
22+
printf("\n");
23+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Testing range edge cases:
2+
Empty range 5...1:
3+
Single element 3...3:
4+
Zero range 0...0:

examples/range_for.kit.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Testing range for loops:
2+
Count 1 to 5: 1 2 3 4 5
3+
Count 10 to 15: 10 11 12 13 14 15
4+
Count 5 to 1:
5+
Single element 7: 7
6+
Expression range 2*2...3*3: 4 5 6 7 8 9
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
include "stdio.h";
2+
3+
function main() {
4+
printf("Testing range for loops:\n");
5+
6+
printf("Count 1 to 5: ");
7+
for i in 1...5 {
8+
printf("%d ", i);
9+
}
10+
printf("\n");
11+
12+
var start = 10;
13+
var end = 15;
14+
printf("Count 10 to 15: ");
15+
for i in start...end {
16+
printf("%d ", i);
17+
}
18+
printf("\n");
19+
20+
printf("Count 5 to 1: ");
21+
for i in 5...1 {
22+
printf("%d ", i);
23+
}
24+
printf("\n");
25+
26+
printf("Single element 7: ");
27+
for i in 7...7 {
28+
printf("%d ", i);
29+
}
30+
printf("\n");
31+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Testing range for loops:
2+
Count 1 to 5: 1 2 3 4
3+
Count 10 to 15: 10 11 12 13 14
4+
Count 5 to 1:
5+
Single element 7:

examples/range_for_simple.kit

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
include "stdio.h";
2+
3+
function main() {
4+
printf("Testing range for loops:\n");
5+
printf("Count 1 to 5: ");
6+
for i in 1...5 {
7+
printf("%d ", i);
8+
}
9+
printf("\n");
10+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Testing range for loops:
2+
Count 1 to 5: 1 2 3 4

examples/range_with_variables.kit

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
include "stdio.h";
2+
3+
function main() {
4+
printf("Testing range limitations:\n");
5+
6+
printf("Variables in range: ");
7+
var start = 1;
8+
var end = 3;
9+
for i in start...end {
10+
printf("%d ", i);
11+
}
12+
printf("\n");
13+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Testing range limitations:
2+
Variables in range: 1 2

0 commit comments

Comments
 (0)