Verified examples
Programs from the Lane test suite with their expected standard output and exit codes, grouped by feature.
These are programs from the compiler's test suite (1668 programs: 556 that build and run, 1112 that the compiler must refuse). Each is shown with its expected standard output and exit code. A test passes only when both match exactly. The 556 that run are grouped by what they test; the refused ones are in the error reference.
Notes for reading them:
- A program whose
mainreturnsi32reports its result as the process exit code. Exit codes are shown as signed 32-bit numbers, so-128is0xffffff80. - Many programs that print start with the helper below, which is left out of the listings. It writes a
u64as 16 lowercase hex digits and a newline usingWriteFilefrom kernel32 (it uses the Windows API, like the standard library). - Programs of one feature are generated variants; two of them differ in their constants.
@lib("kernel32") ext *nul GetStdHandle(u32 id);
@lib("kernel32") ext i32 WriteFile(*nul file, *u64 buf, u32 len, *u32 written, *nul overlapped);
u64 hex_digit(u64 v) {
u64 d = v & 15;
if d < 10 { ret d + '0'; }
ret d - 10 + 'a';
}
nul hex(u64 v) {
u64 high = 0;
u64 low = 0;
for i in u64(0)..8 {
high |= hex_digit(v >> (60 - 4 * i)) << (8 * i);
low |= hex_digit(v >> (28 - 4 * i)) << (8 * i);
}
u64 newline = '\n';
u32 written = 0;
*nul console = GetStdHandle(0xffff_fff5);
WriteFile(console, &high, 8, &written, nil);
WriteFile(console, &low, 8, &written, nil);
WriteFile(console, &newline, 1, &written, nil);
}
#Arithmetic, literals and pointers
#Arithmetic wraps and respects the sign
63 cases build and run. Two are shown.
Example 1
i32 main() { i32 a = 5; ret -a; }
Verified: builds and runs. Exit code -5 (0xfffffffb). No stdout.
Example 2
i32 main() { i32 a = 5; ret ~a; }
Verified: builds and runs. Exit code -6 (0xfffffffa). No stdout.
#Bool and system writes through a pointer
3 cases build and run. Two are shown.
Example 1
@lib("kernel32") ext *nul GetStdHandle(u32 id);
@lib("kernel32") ext i32 WriteFile(*nul file, *u8 buf, u32 len, *i32 written, *nul overlapped);
i32 main() {
[]u8 s = "";
i32 n = -1;
WriteFile(GetStdHandle(0xffff_fff5), s.ptr, 0, &n, nil);
if n == 0 { ret 1; }
ret 2;
}
Verified: builds and runs. Exit code 1. No stdout.
Example 2
@lib("kernel32") ext nul RtlMoveMemory(*i32 dst, *i32 src, usz len);
i32 main() {
i32 a = -5;
i32 b = 0;
RtlMoveMemory(&b, &a, 4);
i32 r = 0;
if b == a { r += 1; }
if b / 2 == -2 { r += 10; }
i32 n = 100;
RtlMoveMemory(&n, &b, 4);
for i in n..0 { r += 100; }
ret r;
}
Verified: builds and runs. Exit code 511. No stdout.
#Character literals are integers
22 cases build and run. Two are shown.
Example 1
i32 main() { ret 'z'; }
Verified: builds and runs. Exit code 122. No stdout.
Example 2
i32 main() { ret '"'; }
Verified: builds and runs. Exit code 34. No stdout.
#Compound assignment reads the variable after the right side
1 case builds and runs. One is shown.
Example 1
@lib("kernel32") ext *nul GetStdHandle(u32 id);
@lib("kernel32") ext i32 WriteFile(*nul file, *u8 buf, u32 len, *u32 written, *nul overlapped);
i32 main() {
[]u8 s = "12345";
u32 n = 1000;
n += u32(WriteFile(GetStdHandle(0xffff_fff5), s.ptr, u32(s.len), &n, nil));
ret i32(n);
}
Verified: builds and runs. Exit code 6. Stdout:
12345
#Division by zero stops the program
4 cases build and run. Two are shown.
Example 1
i32 main() { u32 a = 5; u32 b = 0; ret i32(a / b); }
Verified: builds and runs. Exit code -1073741676 (0xc0000094). No stdout.
Example 2
i32 main() { u32 a = 5; u32 b = 0; ret i32(a % b); }
Verified: builds and runs. Exit code -1073741676 (0xc0000094). No stdout.
#Hex helper prints sixteen digits
1 case builds and runs. One is shown.
Example 1
nul main() { hex(0); hex(0x0123_4567_89ab_cdef); hex(0xfedc_ba98_7654_3210); hex(u64(i8(-1))); }
Verified: part of a program that builds and runs. For the whole program, exit code 0. Stdout:
0000000000000000
0123456789abcdef
fedcba9876543210
ffffffffffffffff
#Literals take their type from the other operand or the context
35 cases build and run. Two are shown.
Example 1
i32 main() { ret ~0; }
Verified: builds and runs. Exit code -1 (0xffffffff). No stdout.
Example 2
i32 main() { ret - -5; }
Verified: builds and runs. Exit code 5. No stdout.
#Logic and equality truth tables
1 case builds and runs. One is shown.
Example 1
i32 table(bool a, bool b) {
i32 bits = 0;
if a && b { bits |= 1; }
if a || b { bits |= 2; }
if a == b { bits |= 4; }
if a != b { bits |= 8; }
if !a { bits |= 16; }
bool both = a && b;
bool either = a || b;
if both == either { bits |= 32; }
ret bits;
}
i32 main() {
ret table(false, false) + table(false, true) * 100 + table(true, false) * 10000
+ table(true, true) * 1000000;
}
Verified: builds and runs. Exit code 39102652. No stdout.
#Logic short circuits
1 case builds and runs. One is shown.
Example 1
use io;
bool t([]u8 name) { io.print(name); ret true; }
bool f([]u8 name) { io.print(name); ret false; }
nul main() {
if f("a") && t("b") { io.print("!"); }
io.print("|");
if t("c") && f("d") { io.print("!"); }
io.print("|");
if t("e") && t("f") { io.print("+"); }
io.print("|");
if t("g") || f("h") { io.print("+"); }
io.print("|");
if f("i") || t("j") { io.print("+"); }
io.print("|");
if f("k") || f("l") { io.print("!"); }
io.print("|");
if t("m") || f("n") && f("o") { io.print("+"); }
io.print("|");
if f("p") && f("q") || t("r") { io.print("+"); }
io.print("|");
if (f("s") || t("u")) && !f("v") { io.print("+"); }
io.print("|");
bool x = f("w") && t("x");
bool y = t("y") || f("z");
if !x && y { io.print("+"); }
io.print("\n");
}
Verified: builds and runs. Exit code 0. Stdout:
a|cd|ef+|g+|ij+|kl|m+|pr+|suv+|wy+
#Nested expressions keep their temporaries
1 case builds and runs. One is shown.
Example 1
u64 sq(u64 v) { ret v * v; }
u64 mix(u64 x, u64 y) { ret (x ^ (y << 7)) + (y >> 3); }
nul show(u64 a, u64 b, u64 c, u64 d) {
hex((a + b) * (c - d) ^ ((a << 3) | (d >> 1)));
hex(a - (b - (c - (d - (a - (b - c))))));
hex(sq(a) + sq(b) * (sq(c) - sq(d)));
hex(mix(mix(a, b), mix(c, mix(d, a))));
hex((a * b + c * d) / (d | 1) % (sq(c) | 1));
}
nul main() {
show(1, 2, 3, 4);
show(18446744073709551615, 9223372036854775808, 7, 0);
show(81985529216486895, 18364758544493064720, 6510615555426900570, 3);
show(10, 1000, 100000, 10000000);
}
Verified: part of a program that builds and runs. For the whole program, exit code 0. Stdout: Lines 1 to 8 of 20.
fffffffffffffff7
0000000000000000
ffffffffffffffe5
00000000002110c3
0000000000000002
8000000000000001
000000000000000c
0000000000000001
#Operands are evaluated left to right
1 case builds and runs. One is shown.
Example 1
use io;
i32 v([]u8 name, i32 value) { io.print(name); ret value; }
i32 three(i32 a, i32 b, i32 c) { ret a * 100 + b * 10 + c; }
i32 main() {
i32 r = v("a", 100) - v("b", 30) - v("c", 20);
r += v("d", 1) * (v("e", 2) + v("f", 3));
if v("g", 1) < v("h", 2) { r += 100; }
r = 1000 - v("i", r);
r = r + three(v("j", 1), v("k", 2) + v("l", 0), v("m", 3)) / v("n", 3);
ret r;
}
Verified: builds and runs. Exit code 886. Stdout:
abcdefghijklmn
#Operators on i16
1 case builds and runs. The smallest has 182190 characters after the shared helper, too long to list here.
#Operators on i32
1 case builds and runs. The smallest has 212350 characters after the shared helper, too long to list here.
#Operators on i64
1 case builds and runs. The smallest has 266638 characters after the shared helper, too long to list here.
#Operators on i8
1 case builds and runs. The smallest has 148444 characters after the shared helper, too long to list here.
#Operators on isz
1 case builds and runs. The smallest has 266638 characters after the shared helper, too long to list here.
#Operators on u16
1 case builds and runs. The smallest has 108164 characters after the shared helper, too long to list here.
#Operators on u32
1 case builds and runs. The smallest has 125744 characters after the shared helper, too long to list here.
#Operators on u64
1 case builds and runs. The smallest has 159146 characters after the shared helper, too long to list here.
#Operators on u8
1 case builds and runs. The smallest has 86212 characters after the shared helper, too long to list here.
#Operators on usz
1 case builds and runs. The smallest has 159146 characters after the shared helper, too long to list here.
#Pointers compare for equality
2 cases build and run. Two are shown.
Example 1
@lib("kernel32") ext *nul GetStdHandle(u32 id);
i32 main() { if GetStdHandle(0xffff_fff5) == GetStdHandle(0xffff_fff5) { ret 1; } ret 2; }
Verified: builds and runs. Exit code 1. No stdout.
Example 2
i32 main() {
u32 a = 1; u32 b = 1;
*u32 p = &a; *u32 q = &b; *u32 r = &a; *u32 none = nil;
i32 bits = 0;
if p == r { bits |= 1; }
if p != q { bits |= 2; }
if p == q { bits |= 4; }
if p != r { bits |= 8; }
if none == nil { bits |= 16; }
if nil != p { bits |= 32; }
if nil == p { bits |= 64; }
if &a == p && &b != p { bits |= 128; }
ret bits;
}
Verified: builds and runs. Exit code 179. No stdout.
#Written through a pointer i16
1 case builds and runs. The smallest has 1847 characters after the shared helper, too long to list here.
#Written through a pointer i32
1 case builds and runs. The smallest has 1871 characters after the shared helper, too long to list here.
#Written through a pointer i8
1 case builds and runs. The smallest has 1824 characters after the shared helper, too long to list here.
#Written through a pointer u16
1 case builds and runs. The smallest has 1838 characters after the shared helper, too long to list here.
#Written through a pointer u32
1 case builds and runs. The smallest has 1858 characters after the shared helper, too long to list here.
#Written through a pointer u8
1 case builds and runs. The smallest has 1817 characters after the shared helper, too long to list here.
#Control flow and functions
#Blocks scope their variables
3 cases build and run. Two are shown.
Example 1
nul main() { { } { { } } }
Verified: builds and runs. Exit code 0. No stdout.
Example 2
i32 main() {
i32 total = 0;
for i in i32(0)..4 { i32 fresh = 1; fresh += i; total += fresh; }
ret total;
}
Verified: builds and runs. Exit code 10. No stdout.
#Condition loops and endless loops
5 cases build and run. Two are shown.
Example 1
i32 main() { u32 n = 0; for n > 5 { ret 1; } ret 2; }
Verified: builds and runs. Exit code 2. No stdout.
Example 2
i32 main() { u32 n = 0; for { n += 3; if n > 10 { ret i32(n); } } }
Verified: builds and runs. Exit code 12. No stdout.
#Endless loops need no return
3 cases build and run. Two are shown.
Example 1
i32 f() { for { for { brk; } } } nul main() { }
Verified: builds and runs. Exit code 0. No stdout.
Example 2
i32 f(bool c) { for { if c { ret 1; } } } nul main() { }
Verified: builds and runs. Exit code 0. No stdout.
#Functions that return on every path
17 cases build and run. Two are shown.
Example 1
use io;
nul early(bool leave) { io.print("a"); if leave { ret; } io.print("b"); }
nul main() { early(true); early(false); for { io.print("c"); ret; } }
Verified: builds and runs. Exit code 0. Stdout:
aabc
Example 2
i32 sign(i32 n) { if n < 0 { ret -1; } elif n == 0 { ret 0; } else { ret 1; } }
i32 first_over(u32 limit) { u32 n = 1; for { if n > limit { ret i32(n); } n *= 2; } }
i32 nested(bool a, bool b) { if a { if b { ret 3; } else { ret 2; } } else { { ret 1; } } }
i32 after_loop(u32 n) { for i in u32(0)..n { if i == 3 { ret 30; } } ret 40; }
i32 inner_brk(u32 n) { u32 k = 0; for { for { k += 1; if k % 4 == 0 { brk; } } if k > n { ret i32(k); } } }
i32 unreachable_tail() { ret 7; ret 8; }
i32 passable_after_ret(u32 n) { ret 5; u32 a = n; a += 1; }
i32 passable_after_if(bool c) { if c { ret 1; } else { ret 2; } u32 dead = 3; dead += 1; }
i32 passable_after_loop() { for { ret 6; } u32 dead = 3; }
i32 main() { ret sign(0); }
Verified: part of a program that builds and runs. For the whole program, exit code 0. No stdout.
#If else chains take exactly one branch
3 cases build and run. Two are shown.
Example 1
i32 pick(i32 n) { i32 r = 9; if n == 1 { r = 1; } elif n == 2 { r = 2; } ret r; }
i32 main() { ret pick(1) * 100 + pick(2) * 10 + pick(3); }
Verified: builds and runs. Exit code 129. No stdout.
Example 2
i32 main() {
i32 r = 0;
if true { r += 1; }
if false { r += 10; }
if false { r += 100; } else { r += 1000; }
if false { r += 10000; } elif true { r += 100000; } else { r += 1000000; }
if true { if false { r += 2; } else { if true { r += 4; } } }
ret r;
}
Verified: builds and runs. Exit code 101005. No stdout.
#Nested loops with brk and nxt
4 cases build and run. Two are shown.
Example 1
u64 six(u64 a, u64 b, u64 c, u64 d, u64 e, u64 f) { ret a + b * 2 + c * 3 + d * 4 + e * 5 + f * 6; }
i32 main() {
u64 s = 0;
for i in u64(0)..10 { s += six(i, i + 1, i + 2, i + 3, i + 4, i + 5) - six(1, 1, 1, 1, 1, i); }
ret i32(s);
}
Verified: builds and runs. Exit code 1225. No stdout.
Example 2
i32 main() {
u32 count = 0;
for i in u32(0)..10 {
for j in u32(0)..10 {
{ if j == 3 { { brk; } } }
count += 1;
}
}
ret i32(count);
}
Verified: builds and runs. Exit code 30. No stdout.
#Program collatz steps
1 case builds and runs. One is shown.
Example 1
use io;
nul num_digit(u64 d) {
if d == 0 { io.print("0"); }
elif d == 1 { io.print("1"); }
elif d == 2 { io.print("2"); }
elif d == 3 { io.print("3"); }
elif d == 4 { io.print("4"); }
elif d == 5 { io.print("5"); }
elif d == 6 { io.print("6"); }
elif d == 7 { io.print("7"); }
elif d == 8 { io.print("8"); }
else { io.print("9"); }
}
nul num(u64 n) {
if n >= 10 { num(n / 10); }
num_digit(n % 10);
}
u64 steps(u64 start) {
u64 n = start;
u64 count = 0;
for n != 1 {
if n % 2 == 0 { n /= 2; } else { n = 3 * n + 1; }
count += 1;
}
ret count;
}
nul show(u64 start) { num(steps(start)); io.print(" "); }
nul main() {
show(1); show(2); show(3); show(6); show(7); show(27); show(97); show(871);
io.print("\n");
}
Verified: builds and runs. Exit code 0. Stdout:
0 1 7 8 16 111 118 178
#Program fibonacci
2 cases build and run. Two are shown.
Example 1
u32 fib(u32 n) { if n < 2 { ret n; } ret fib(n - 1) + fib(n - 2); }
i32 main() { ret i32(fib(20)); }
Verified: builds and runs. Exit code 6765. No stdout.
Example 2
u64 fib(u32 n) {
u64 a = 0;
u64 b = 1;
for i in 0..n { u64 next = a + b; a = b; b = next; }
ret a;
}
nul main() { for n in u32(0)..=93 { hex(fib(n)); } }
Verified: part of a program that builds and runs. For the whole program, exit code 0. Stdout: Lines 1 to 8 of 94.
0000000000000000
0000000000000001
0000000000000001
0000000000000002
0000000000000003
0000000000000005
0000000000000008
000000000000000d
#Program fizzbuzz
1 case builds and runs. One is shown.
Example 1
use io;
nul num_digit(u64 d) {
if d == 0 { io.print("0"); }
elif d == 1 { io.print("1"); }
elif d == 2 { io.print("2"); }
elif d == 3 { io.print("3"); }
elif d == 4 { io.print("4"); }
elif d == 5 { io.print("5"); }
elif d == 6 { io.print("6"); }
elif d == 7 { io.print("7"); }
elif d == 8 { io.print("8"); }
else { io.print("9"); }
}
nul num(u64 n) {
if n >= 10 { num(n / 10); }
num_digit(n % 10);
}
nul main() {
for i in u64(1)..=100 {
if i % 15 == 0 { io.print("FizzBuzz"); }
elif i % 3 == 0 { io.print("Fizz"); }
elif i % 5 == 0 { io.print("Buzz"); }
else { num(i); }
io.print("\n");
}
}
Verified: builds and runs. Exit code 0. Stdout: Lines 1 to 8 of 100.
1
2
Fizz
4
Buzz
Fizz
7
8
#Program gcd
1 case builds and runs. One is shown.
Example 1
u64 gcd(u64 a, u64 b) {
u64 x = a;
u64 y = b;
for y != 0 { u64 t = x % y; x = y; y = t; }
ret x;
}
i64 gcd_rec(i64 a, i64 b) { if b == 0 { ret a; } ret gcd_rec(b, a % b); }
nul main() {
hex(gcd(48, 18));
hex(gcd(18, 48));
hex(gcd(1071, 462));
hex(gcd(17, 5));
hex(gcd(0, 5));
hex(gcd(5, 0));
hex(gcd(1099511627776, 103079215104));
hex(gcd(18446744073709551615, 3));
hex(gcd(1000000007, 998244353));
hex(u64(gcd_rec(1071, 462))); hex(u64(gcd_rec(270, 192)));
}
Verified: part of a program that builds and runs. For the whole program, exit code 0. Stdout: Lines 1 to 8 of 11.
0000000000000006
0000000000000006
0000000000000015
0000000000000001
0000000000000005
0000000000000005
0000000800000000
0000000000000003
#Range bounds are evaluated once
1 case builds and runs. One is shown.
Example 1
use io;
u32 bound([]u8 name, u32 v) { io.print(name); ret v; }
nul main() {
u32 n = 3;
for i in bound("a", 0)..bound("b", n) { n += 10; io.print("."); }
for i in bound("c", 1)..=bound("d", 2) { io.print(":"); }
for i in bound("e", 2)..bound("f", 2) { io.print("!"); }
io.print("\n");
}
Verified: builds and runs. Exit code 0. Stdout:
ab...cd::ef
#Range loops visit exactly their range
27 cases build and run. Two are shown.
Example 1
i32 main() { i32 n = 0; for i in i32(3)..-3 { n += 1; } ret n; }
Verified: builds and runs. Exit code 0. No stdout.
Example 2
i32 main() { u32 n = 0; for i in u32(5)..5 { n += 1; } ret i32(n); }
Verified: builds and runs. Exit code 0. No stdout.
#Temporaries stay live across nested calls
1 case builds and runs. One is shown.
Example 1
i32 f(i32 a, i32 b, i32 c) { ret a + 2 * b + 3 * c; }
i32 g(i32 a, i32 b) { ret 10 * a - b; }
i32 main() {
i32 x = 1000;
ret x + f(2, g(4, 5), 6 * 7) * 3 - (100 - g(f(1, 2, 3), 9));
}
Verified: builds and runs. Exit code 1625. No stdout.
#Enums and match
#Enum values and conversions
1 case builds and runs. The smallest has 2255 characters after the shared helper, too long to list here.
#Match patterns on every integer type
1 case builds and runs. The smallest has 12752 characters after the shared helper, too long to list here.
#Program bytecode interpreter
5 cases build and run. The smallest has 2064 characters after the shared helper, too long to list here.
#Program classify every byte
1 case builds and runs. One is shown.
Example 1
enum Class { digit, lower, upper, space, punct, other }
Class classify(u8 b) {
match b {
'0'..='9' { ret .digit; }
'a'..='z' { ret .lower; }
'A'..='Z' { ret .upper; }
' ', '\t', '\n', '\r' { ret .space; }
'!'..='/', ':'..='@', '['..='`', '{'..='~' { ret .punct; }
else { ret .other; }
}
}
nul main() {
[6]u32 counts;
u64 h = 0;
for b in u32(0)..256 {
Class c = classify(u8(b));
counts[usz(u32(c))] += 1;
h = h * 7 + u64(u32(c)) + 1;
}
for i in usz(0)..6 { hex(u64(counts[i])); }
hex(h);
hex(u64(u32(classify('a')))); hex(u64(u32(classify('`')))); hex(u64(u32(classify(127))));
}
Verified: part of a program that builds and runs. For the whole program, exit code 0. Stdout: Lines 1 to 8 of 10.
000000000000000a
000000000000001a
000000000000001a
0000000000000004
0000000000000020
000000000000009e
9509af2cfa3c7520
0000000000000001
#Program lexer fills a token table
1 case builds and runs. The smallest has 2688 characters after the shared helper, too long to list here.
#Program ship steered by enum matches
1 case builds and runs. One is shown.
Example 1
enum Dir { north, east, south, west }
rec Ship { Dir facing; i32 x; i32 y; u32 moves; }
Dir right_of(Dir d) {
match d {
.north { ret .east; }
.east { ret .south; }
.south { ret .west; }
.west { ret .north; }
}
}
nul advance(wr Ship s, i32 n) {
match s.facing {
.north { s.y += n; }
.east { s.x += n; }
.south { s.y -= n; }
.west { s.x -= n; }
}
s.moves += 1;
}
nul main() {
Ship s;
u64 seed = 0x2545_f491_4f6c_dd1d;
for i in u32(0)..5000 {
seed ^= seed << 13; seed ^= seed >> 7; seed ^= seed << 17;
u64 r = seed >> 33;
if r % 3 == 0 { s.facing = right_of(s.facing); }
else { advance(wr s, i32(r % 10) - 3); }
if i % 1000 == 999 {
hex(u64(u32(s.x))); hex(u64(u32(s.y))); hex(u64(u32(s.facing))); hex(u64(s.moves));
}
}
}
Verified: part of a program that builds and runs. For the whole program, exit code 0. Stdout: Lines 1 to 8 of 20.
00000000ffffffd9
000000000000000c
0000000000000001
00000000000002a3
00000000ffffffc0
000000000000001b
0000000000000000
0000000000000548
#Memory: arrays, frames and element sizes
#A frame of half a megabyte works
1 case builds and runs. One is shown.
Example 1
u64 big() {
[524288]u8 a;
hex(1);
u64 sum = 0;
for i in usz(0)..524288 { sum += u64(a[i]); }
a[0] = 3; a[262144] = 5; a[524287] = 7;
hex(u64(a[0]) + u64(a[262144]) + u64(a[524287]) + sum);
hex(u64(a.len));
ret 99;
}
nul main() { hex(big()); hex(big()); }
Verified: part of a program that builds and runs. For the whole program, exit code 0. Stdout:
0000000000000001
000000000000000f
0000000000080000
0000000000000063
0000000000000001
000000000000000f
0000000000080000
0000000000000063
#Bool elements are single bytes
1 case builds and runs. One is shown.
Example 1
nul main() {
[4]bool b;
b[1] = true; b[3] = true;
for i in usz(0)..4 { if b[i] { hex(1); } else { hex(0); } }
*bool p = &b[0];
*p = true;
if b[0] && b[1] && !b[2] && b[3] { hex(7); } else { hex(8); }
*p = false;
b[2] = *p == b[2];
[]u8 raw = slice(cast(&b[0]), 4);
for i in usz(0)..4 { hex(u64(raw[i])); }
}
Verified: part of a program that builds and runs. For the whole program, exit code 0. Stdout: Lines 1 to 8 of 9.
0000000000000000
0000000000000001
0000000000000000
0000000000000001
0000000000000007
0000000000000000
0000000000000001
0000000000000001
#Element stride is the size of the element
1 case builds and runs. The smallest has 2966 characters after the shared helper, too long to list here.
#Elements and pointers use the size of their type
10 cases build and run. Two are shown.
Example 1
nul main() {
[3]u8 a;
[]u8 raw = slice(cast(&a[0]), 3);
for i in usz(0)..3 { raw[i] = 0xaa; }
a[0] = 0;
a[2] = 255;
hex(u64(a[0])); hex(u64(a[1])); hex(u64(a[2]));
a[1] = 255; a[1] += 1; hex(u64(a[1]));
a[1] = 0; a[1] -= 1; hex(u64(a[1]));
a[1] = 255; a[1] *= 3; hex(u64(a[1]));
a[1] = 5; a[1] <<= 3; hex(u64(a[1]));
*u8 p = &a[1];
*p = 0; hex(u64(*p)); hex(u64(a[1]));
*p += 1; hex(u64(a[1]));
u8 x = 255;
*u8 q = &x;
*q -= 1; hex(u64(x));
for i in usz(0)..3 { hex(u64(raw[i])); }
}
Verified: part of a program that builds and runs. For the whole program, exit code 0. Stdout: Lines 1 to 8 of 14.
0000000000000000
00000000000000aa
00000000000000ff
0000000000000000
00000000000000ff
00000000000000fd
0000000000000028
0000000000000000
Example 2
nul main() {
[3]i8 a;
[]u8 raw = slice(cast(&a[0]), 3);
for i in usz(0)..3 { raw[i] = 0xaa; }
a[0] = -128;
a[2] = 127;
hex(u64(a[0])); hex(u64(a[1])); hex(u64(a[2]));
a[1] = 127; a[1] += 1; hex(u64(a[1]));
a[1] = -128; a[1] -= 1; hex(u64(a[1]));
a[1] = 127; a[1] *= 3; hex(u64(a[1]));
a[1] = 5; a[1] <<= 3; hex(u64(a[1]));
*i8 p = &a[1];
*p = -128; hex(u64(*p)); hex(u64(a[1]));
*p += 1; hex(u64(a[1]));
i8 x = 127;
*i8 q = &x;
*q -= 1; hex(u64(x));
for i in usz(0)..3 { hex(u64(raw[i])); }
}
Verified: part of a program that builds and runs. For the whole program, exit code 0. Stdout: Lines 1 to 8 of 14.
ffffffffffffff80
ffffffffffffffaa
000000000000007f
ffffffffffffff80
000000000000007f
000000000000007d
0000000000000028
ffffffffffffff80
#Frames around page boundaries
18 cases build and run. One is shown.
Example 1
use io;
@lib("kernel32") ext *nul GetStdHandle(u32 id);
@lib("kernel32") ext i32 WriteFile(*nul file, *u64 buf, u32 len, *u32 written, *nul overlapped);
u64 hex_digit(u64 v) {
u64 d = v & 15;
if d < 10 { ret d + '0'; }
ret d - 10 + 'a';
}
nul hex(u64 v) {
u64 high = 0;
u64 low = 0;
for i in u64(0)..8 {
high |= hex_digit(v >> (60 - 4 * i)) << (8 * i);
low |= hex_digit(v >> (28 - 4 * i)) << (8 * i);
}
u64 newline = '\n';
u32 written = 0;
*nul console = GetStdHandle(0xffff_fff5);
WriteFile(console, &high, 8, &written, nil);
WriteFile(console, &low, 8, &written, nil);
WriteFile(console, &newline, 1, &written, nil);
}
u64 twice(u64 v) { ret v * 2; }
u64 f() {
[3900]u8 a;
a[0] = 1;
a[3899] = 2;
io.print("[");
u64 r = twice(u64(a[0]) + u64(a[3899]));
io.print("]");
hex(r);
ret r + u64(a.len);
}
nul main() { hex(f()); }
Verified: builds and runs. Exit code 0. Stdout:
[]0000000000000006
0000000000000f42
#Main may have a big frame
1 case builds and runs. One is shown.
Example 1
i32 main() {
[300000]u8 a;
a[0] = 3; a[299999] = 4;
ret i32(a[0]) * 10 + i32(a[299999]) + i32(a.len);
}
Verified: builds and runs. Exit code 300034. No stdout.
#Pointers above four gigabytes survive every access
1 case builds and runs. One is shown.
Example 1
nul put(wr *u8 q, *u8 v) { q = v; }
nul make(out *u8 q) { q = "out".ptr; }
*u8 pick([2]*u8 a, usz i) { ret a[i]; }
nul main() {
[2]*u8 p;
p[0] = "abc".ptr;
p[1] = "xyz".ptr;
hex(u64(*p[0]));
hex(u64(*p[1]));
usz a = cast(p[1]);
hex(u64(a >> 32));
hex(u64(*pick(p, 0)));
**u8 pp = &p[0];
hex(u64(**pp));
*pp = p[1];
hex(u64(*p[0]));
*pp = "q".ptr;
hex(u64(*p[0]));
*u8 q;
put(wr q, "wrx".ptr);
hex(u64(*q));
make(out q);
hex(u64(*q));
put(wr p[1], "z".ptr);
hex(u64(*p[1]));
*u8 local = "loc".ptr;
hex(u64(*local));
[2][]u8 s;
s[1] = "slice";
hex(u64(s[1][0]));
usz b = cast(s[1].ptr);
hex(u64(b >> 32));
}
Verified: part of a program that builds and runs. For the whole program, exit code 0. Stdout: Lines 1 to 8 of 13.
0000000000000061
0000000000000078
0000000000000001
0000000000000061
0000000000000061
0000000000000078
0000000000000071
0000000000000077
#Variables without a value are zero
1 case builds and runs. One is shown.
Example 1
nul dirty() {
[64]u64 a;
for i in usz(0)..64 { a[i] = 0xffff_ffff_ffff_ffff; }
ret;
}
nul clean() {
u8 b8; i16 c16; u32 d32; i64 e64; usz f;
bool flag; *u32 p; []u8 s; *nul n;
[5]u8 arr; [2][3]u16 m; [3][]u8 parts; [2]*u8 ptrs; [2]bool flags;
hex(u64(b8)); hex(u64(c16)); hex(u64(d32)); hex(u64(e64)); hex(u64(f));
if flag { hex(1); } else { hex(0); }
if p == nil { hex(0); } else { hex(1); }
if n == nil { hex(0); } else { hex(1); }
hex(u64(s.len));
if s.ptr == nil { hex(0); } else { hex(1); }
u64 sum = 0;
for i in usz(0)..5 { sum |= u64(arr[i]); }
hex(sum);
for r in usz(0)..2 { for c in usz(0)..3 { sum |= u64(m[r][c]); } }
hex(sum);
for i in usz(0)..3 { hex(u64(parts[i].len)); if parts[i].ptr == nil { hex(0); } else { hex(1); } }
for i in usz(0)..2 { if ptrs[i] == nil { hex(0); } else { hex(1); } if flags[i] { hex(1); } else { hex(0); } }
}
nul main() {
dirty();
clean();
for i in u32(0)..3 {
[4]u32 t;
u8 b;
hex(u64(t[0] | t[1] | t[2] | t[3]) + u64(b));
t[0] = 7; t[3] = 9; b = 0xff;
}
}
Verified: part of a program that builds and runs. For the whole program, exit code 0. Stdout: Lines 1 to 8 of 25.
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
#Zeroing covers exactly the array
1 case builds and runs. The smallest has 4928 characters after the shared helper, too long to list here.
#Records: layout and copying
#Copying a record copies exactly its size
24 cases build and run. One is shown.
Example 1
rec R { [1]u8 b; }
u64 weigh(R r) {
u64 t = 0;
for i in usz(0)..r.b.len { t += u64(r.b[i]) * u64(i + 1); }
ret t;
}
nul copy_over(wr R dst, R src) { dst = src; }
nul main() {
[3]R trio;
for i in usz(0)..1 { trio[0].b[i] = u8(i * 7 + 1); trio[1].b[i] = 0x55; }
trio[1] = trio[0];
hex(weigh(trio[0])); hex(weigh(trio[1])); hex(weigh(trio[2]));
R c = trio[1];
c.b[0] += 1;
hex(weigh(c)); hex(weigh(trio[1]));
trio[2] = c;
hex(weigh(trio[2])); hex(weigh(trio[0]));
*R p = &trio[0];
*R q = &trio[2];
*q = *p;
hex(weigh(trio[2]));
R d;
d = trio[0];
hex(weigh(d));
copy_over(wr d, c);
hex(weigh(d));
}
Verified: part of a program that builds and runs. For the whole program, exit code 0. Stdout: Lines 1 to 8 of 10.
0000000000000001
0000000000000001
0000000000000000
0000000000000002
0000000000000001
0000000000000002
0000000000000001
0000000000000001
#Layouts follow the c rules
17 cases build and run. Two are shown.
Example 1
enum Color { r, g, b }
rec A { u8 a; u32 b; u8 c; }
rec B { u8 a; u8 b; u16 c; u8 d; }
rec C { u64 a; u8 b; }
rec D { u8 a; u64 b; u16 c; }
rec E { bool f; i16 g; i64 h; }
rec F { u8 a; [3]u16 arr; u8 z; }
rec G { []u8 s; u8 t; }
rec H { u8 a; A inner; u8 b; }
rec I { u8 a; Color c; u8 d; }
rec J { *u8 p; u8 q; }
rec K { u8 a; [3]B arr; }
rec L { u8 a; }
rec M { u16 a; u8 b; }
rec N { u8 x; M m; u8 y; }
rec O { [3]u8 raw; u32 w; [5]u8 tail; }
rec P { isz a; usz b; i8 c; }
rec Q { [2][3]u16 grid; u8 z; }
u64 peek([]u8 raw, usz off, usz n) {
u64 v = 0;
for i in usz(0)..n { v |= u64(raw[off + i]) << (8 * u64(i)); }
ret v;
}
u64 sum([]u8 raw) {
u64 t = 0;
for i in usz(0)..raw.len { t += u64(raw[i]); }
ret t;
}
nul clear(wr []u8 raw) { for i in usz(0)..raw.len { raw[i] = 0; } }
nul main() {
L r;
[]u8 raw = slice(cast(&r), size_of(L));
hex(size_of(L));
hex(align_of(L));
r.a = 0x1;
hex(peek(raw, 0, 1));
hex(sum(raw));
clear(wr raw);
}
Verified: part of a program that builds and runs. For the whole program, exit code 0. Stdout:
0000000000000001
0000000000000001
0000000000000001
0000000000000001
Example 2
enum Color { r, g, b }
rec A { u8 a; u32 b; u8 c; }
rec B { u8 a; u8 b; u16 c; u8 d; }
rec C { u64 a; u8 b; }
rec D { u8 a; u64 b; u16 c; }
rec E { bool f; i16 g; i64 h; }
rec F { u8 a; [3]u16 arr; u8 z; }
rec G { []u8 s; u8 t; }
rec H { u8 a; A inner; u8 b; }
rec I { u8 a; Color c; u8 d; }
rec J { *u8 p; u8 q; }
rec K { u8 a; [3]B arr; }
rec L { u8 a; }
rec M { u16 a; u8 b; }
rec N { u8 x; M m; u8 y; }
rec O { [3]u8 raw; u32 w; [5]u8 tail; }
rec P { isz a; usz b; i8 c; }
rec Q { [2][3]u16 grid; u8 z; }
u64 peek([]u8 raw, usz off, usz n) {
u64 v = 0;
for i in usz(0)..n { v |= u64(raw[off + i]) << (8 * u64(i)); }
ret v;
}
u64 sum([]u8 raw) {
u64 t = 0;
for i in usz(0)..raw.len { t += u64(raw[i]); }
ret t;
}
nul clear(wr []u8 raw) { for i in usz(0)..raw.len { raw[i] = 0; } }
nul main() {
M r;
[]u8 raw = slice(cast(&r), size_of(M));
hex(size_of(M));
hex(align_of(M));
r.a = 0x201;
hex(peek(raw, 0, 2));
hex(sum(raw));
clear(wr raw);
r.b = 0x1;
hex(peek(raw, 2, 1));
hex(sum(raw));
clear(wr raw);
}
Verified: part of a program that builds and runs. For the whole program, exit code 0. Stdout:
0000000000000004
0000000000000002
0000000000000201
0000000000000003
0000000000000001
0000000000000001
#Record variables start zeroed
1 case builds and runs. One is shown.
Example 1
enum Color { r, g, b }
rec A { u8 a; u32 b; u8 c; }
rec B { u8 a; u8 b; u16 c; u8 d; }
rec C { u64 a; u8 b; }
rec D { u8 a; u64 b; u16 c; }
rec E { bool f; i16 g; i64 h; }
rec F { u8 a; [3]u16 arr; u8 z; }
rec G { []u8 s; u8 t; }
rec H { u8 a; A inner; u8 b; }
rec I { u8 a; Color c; u8 d; }
rec J { *u8 p; u8 q; }
rec K { u8 a; [3]B arr; }
rec L { u8 a; }
rec M { u16 a; u8 b; }
rec N { u8 x; M m; u8 y; }
rec O { [3]u8 raw; u32 w; [5]u8 tail; }
rec P { isz a; usz b; i8 c; }
rec Q { [2][3]u16 grid; u8 z; }
u64 sum(*u8 p, usz n) {
[]u8 raw = slice(p, n);
u64 t = 0;
for i in usz(0)..n { t += u64(raw[i]); }
ret t;
}
nul main() {
for k in u32(0)..3 {
D d;
hex(sum(cast(&d), size_of(D)));
d.a = 0xff; d.b = 0xffff_ffff_ffff_ffff; d.c = 0xffff;
hex(sum(cast(&d), size_of(D)));
}
G g;
hex(u64(g.s.len)); hex(u64(g.t));
if g.s.ptr == nil { hex(1); } else { hex(0); }
K k;
hex(sum(cast(&k), size_of(K)));
}
Verified: part of a program that builds and runs. For the whole program, exit code 0. Stdout: Lines 1 to 8 of 10.
0000000000000000
0000000000000af5
0000000000000000
0000000000000af5
0000000000000000
0000000000000af5
0000000000000000
0000000000000000
#Size and alignment of the scalars
1 case builds and runs. One is shown.
Example 1
enum Color { r, g, b }
rec A { u8 a; u32 b; u8 c; }
rec B { u8 a; u8 b; u16 c; u8 d; }
rec C { u64 a; u8 b; }
rec D { u8 a; u64 b; u16 c; }
rec E { bool f; i16 g; i64 h; }
rec F { u8 a; [3]u16 arr; u8 z; }
rec G { []u8 s; u8 t; }
rec H { u8 a; A inner; u8 b; }
rec I { u8 a; Color c; u8 d; }
rec J { *u8 p; u8 q; }
rec K { u8 a; [3]B arr; }
rec L { u8 a; }
rec M { u16 a; u8 b; }
rec N { u8 x; M m; u8 y; }
rec O { [3]u8 raw; u32 w; [5]u8 tail; }
rec P { isz a; usz b; i8 c; }
rec Q { [2][3]u16 grid; u8 z; }
nul main() {
hex(size_of(u8)); hex(align_of(u8));
hex(size_of(i8)); hex(align_of(i8));
hex(size_of(bool)); hex(align_of(bool));
hex(size_of(u16)); hex(align_of(u16));
hex(size_of(i16)); hex(align_of(i16));
hex(size_of(u32)); hex(align_of(u32));
hex(size_of(i32)); hex(align_of(i32));
hex(size_of(u64)); hex(align_of(u64));
hex(size_of(i64)); hex(align_of(i64));
hex(size_of(usz)); hex(align_of(usz));
hex(size_of(isz)); hex(align_of(isz));
hex(size_of(Color)); hex(align_of(Color));
}
Verified: part of a program that builds and runs. For the whole program, exit code 0. Stdout: Lines 1 to 8 of 24.
0000000000000001
0000000000000001
0000000000000001
0000000000000001
0000000000000001
0000000000000001
0000000000000002
0000000000000002
#Refusals by feature
Cases the compiler must refuse, by what they test. The error reference shows a program for every distinct message.
| Feature | Refused programs |
|---|---|
| Every predeclared name is refused everywhere | 152 |
| Operator errors name the exact position | 58 |
| Errors name the exact position | 41 |
| Control flow errors name the exact position | 41 |
| Constant expressions that are wrong | 35 |
| One step past either end is an error | 33 |
| Unsupported constructs are refused | 28 |
| Everything else is private | 28 |
| Predeclared names cannot be declared as anything | 27 |
| Pointer arithmetic errors | 26 |
| A named variable may not appear in another argument | 25 |
| Cast and slice errors | 23 |
| Declarations of records and enums | 23 |
| Constants in functions that are wrong | 22 |
| Match patterns that are wrong | 22 |
| Indexing needs an array or a slice and a usz | 21 |
| Fields that do not exist | 21 |
| Records in calls and assignments | 19 |
| Out slice arguments are checked at the call | 18 |
| An array of records behind a pointer or slice is held to the limit | 17 |
| Errors say what is wrong where | 17 |
| Out parameters must be assigned on every path | 17 |
| Out slices cannot be read before they are assigned | 17 |
| Enums in expressions that are wrong | 17 |
| Errors next to an operand without a type | 17 |
| Array types and sizes | 16 |
| A wr or out argument must be a writable place | 15 |
| Constant calls that are wrong | 15 |
| Error wording | 15 |
| Match patterns that overlap | 15 |
| Read only records | 15 |
| Programs past the limit are refused by the build | 14 |
| Out parameters cannot be read before they are assigned | 14 |
| Two operands that need a type report the first | 13 |
| Compound pointer assignment errors | 13 |
| Pointers need a typed pointee | 12 |
| Match subjects and missing arms | 12 |
| Read only parameters cannot be written | 11 |
| Two arguments may not name the same variable | 10 |
| Out slices must be assigned on every path | 10 |
| Constant enum expressions that are wrong | 10 |
| Bool converts to an integer and not back | 9 |
| Array lengths that are wrong | 9 |
| Things declared in other modules must be pub | 9 |
| Containing a record by value is still refused | 9 |
| A cycle is reported at its entry whatever its length | 8 |
| Read only parameters cannot be passed on as wr or out | 8 |
| The mode at a call must match the parameter | 8 |
| The operand with a type is the one named when it is unsuitable | 8 |
| Constants that depend on themselves | 7 |
| Match in the analyses of returns and outs | 7 |
| Read only parameters have no address | 6 |
| A record cannot contain itself | 6 |
| Types that do not exist | 6 |
| Another call is not a writable argument | 5 |
| Wr and in slices are still not assignable | 4 |
| Records that cannot be returned or cross the c abi | 4 |
| A sub slice of something that is no place is not writable | 3 |
| A cycle is an error whatever the values | 3 |
| Slice of a pointer as a wr argument is checked | 3 |
| Errors inside a module are reported in that module | 3 |
| Modules next to the root file | 2 |
| Cycles through one entry are reported once | 2 |
| An operand that takes its type from the other one keeps the order | 2 |
| Slice parameters cannot be reassigned | 2 |
| Every broken function is reported in source order | 1 |
| A long chain into a cycle is one error | 1 |
| Deep expressions in a chain of constants | 1 |
| Independent cycles are all reported | 1 |