Files
sx/examples/0051-basic-for-range-bounds.sx
agra fd14ab5694 lang: range bound markers — '=' inclusive / '<' exclusive on either side of '..'
Each side of '..' takes an optional bound marker, defaulting to
start-inclusive, end-exclusive (a..b == a=..<b; a..=b stays the short
end-inclusive spelling):

    for 0<..<N (i) { }   // 1 .. N-1   (both exclusive)
    for 0=..=N (i) { }   // 0 .. N     (both inclusive)
    for 0<..=N (i) { }   // 1 .. N
    for 0..<N  (i) { }   // 0 .. N-1   (explicit default)
    for xs, 2<.. (x, i)  // open range, exclusive start: i = 3, 4, ...

The nine lexemes are single tokens (maximal munch on '<'/'='/'..'), so
expression parsing never sees the leading marker as a comparison; '<',
'<<', '<=', '==', '=>' lex unchanged. An explicit end marker makes the
end expression mandatory; open forms are a.. / a<.. / a=... Works in
runtime, multi-iterable, and inline-for headers.

Regression: examples/0051-basic-for-range-bounds.sx (full matrix, open
start-marked ranges, comptime unroll, runtime bounds, lexer
non-regression); 1152's pinned message generalized.
2026-06-10 20:55:31 +03:00

49 lines
1.4 KiB
Plaintext

// Range bound markers: each side of `..` takes `=` (inclusive) or `<`
// (exclusive); defaults are start-inclusive, end-exclusive (`a..b` == `a=..<b`).
// Covers the full matrix, open ranges with start markers, comptime unrolling,
// runtime bounds, and that `<` / `<<` comparisons still lex normally.
#import "modules/std.sx";
main :: () -> s32 {
for 0<..<5 (i) { print("{} ", i); }
print("| 0<..<5\n");
for 0=..=5 (i) { print("{} ", i); }
print("| 0=..=5\n");
for 0<..=5 (i) { print("{} ", i); }
print("| 0<..=5\n");
for 0=..<5 (i) { print("{} ", i); }
print("| 0=..<5\n");
for 0..<5 (i) { print("{} ", i); }
print("| 0..<5\n");
for 0..=5 (i) { print("{} ", i); }
print("| 0..=5\n");
// Exclusive-start open range following a bounded first iterable.
xs : [3]s64 = .[10, 20, 30];
for xs, 2<.. (x, i) { print("{}@{} ", x, i); }
print("| xs, 2<..\n");
// Explicit inclusive-start open form (synonym of `5..`).
for xs, 5=.. (x, i) { print("{}@{} ", x, i); }
print("| xs, 5=..\n");
// Comptime-unrolled with markers.
s := 0;
inline for 0<..=3 (i) { s += i; }
print("inline 0<..=3 sum={}\n", s);
// Runtime bounds with markers.
lo := 1;
hi := 4;
for lo<..=hi (i) { print("{} ", i); }
print("| lo<..=hi\n");
// Comparison operators still lex normally.
a := 3;
if a < 5 { print("cmp ok\n"); }
b := a << 1;
print("shl={}\n", b);
0
}