test(0051): pin expression bounds at either end of range tokens
x+2..=42 (expression start, 39 iterations summing 897), x+2<..<x*21 (expressions both ends, 5..41), 0..x*3 (expression end). Expression parsing stops at the range lexeme from either side, so any expression works in either position — now pinned.
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
// Range bound markers: each side of `..` takes `=` (inclusive) or `<`
|
// Range bound markers: each side of `..` takes `=` (inclusive) or `<`
|
||||||
// (exclusive); defaults are start-inclusive, end-exclusive (`a..b` == `a=..<b`).
|
// (exclusive); defaults are start-inclusive, end-exclusive (`a..b` == `a=..<b`).
|
||||||
// Covers the full matrix, open ranges with start markers, comptime unrolling,
|
// Covers the full matrix, open ranges with start markers, comptime unrolling,
|
||||||
// runtime bounds, and that `<` / `<<` comparisons still lex normally.
|
// runtime bounds, arbitrary expressions at EITHER end (expression parsing
|
||||||
|
// stops at the range token), and that `<` / `<<` comparisons still lex
|
||||||
|
// normally.
|
||||||
|
|
||||||
#import "modules/std.sx";
|
#import "modules/std.sx";
|
||||||
|
|
||||||
@@ -39,6 +41,19 @@ main :: () -> s32 {
|
|||||||
for lo<..=hi (i) { print("{} ", i); }
|
for lo<..=hi (i) { print("{} ", i); }
|
||||||
print("| lo<..=hi\n");
|
print("| lo<..=hi\n");
|
||||||
|
|
||||||
|
// Arbitrary expressions at either end of the range token.
|
||||||
|
x := 2;
|
||||||
|
n := 0;
|
||||||
|
sum := 0;
|
||||||
|
for x+2..=42 (e) { n += 1; sum += e; } // expression start: 4 .. 42
|
||||||
|
print("x+2..=42: n={} sum={}\n", n, sum);
|
||||||
|
n2 := 0;
|
||||||
|
for x+2<..<x*21 (e) => n2 += 1; // both ends: 5 .. 41
|
||||||
|
print("x+2<..<x*21: n2={}\n", n2);
|
||||||
|
n3 := 0;
|
||||||
|
for 0..x*3 (i) => n3 += 1; // expression end: 0 .. 5
|
||||||
|
print("0..x*3: n3={}\n", n3);
|
||||||
|
|
||||||
// Comparison operators still lex normally.
|
// Comparison operators still lex normally.
|
||||||
a := 3;
|
a := 3;
|
||||||
if a < 5 { print("cmp ok\n"); }
|
if a < 5 { print("cmp ok\n"); }
|
||||||
|
|||||||
@@ -8,5 +8,8 @@
|
|||||||
10@5 20@6 30@7 | xs, 5=..
|
10@5 20@6 30@7 | xs, 5=..
|
||||||
inline 0<..=3 sum=6
|
inline 0<..=3 sum=6
|
||||||
2 3 4 | lo<..=hi
|
2 3 4 | lo<..=hi
|
||||||
|
x+2..=42: n=39 sum=897
|
||||||
|
x+2<..<x*21: n2=37
|
||||||
|
0..x*3: n3=6
|
||||||
cmp ok
|
cmp ok
|
||||||
shl=6
|
shl=6
|
||||||
|
|||||||
Reference in New Issue
Block a user